1/* Motorola 68k series support for 32-bit ELF
2   Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3   2004, 2005, 2006 Free Software Foundation, Inc.
4
5   This file is part of BFD, the Binary File Descriptor library.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program; if not, write to the Free Software
19   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "bfdlink.h"
24#include "libbfd.h"
25#include "elf-bfd.h"
26#include "elf/m68k.h"
27#include "opcode/m68k.h"
28
29static reloc_howto_type *reloc_type_lookup
30  PARAMS ((bfd *, bfd_reloc_code_real_type));
31static void rtype_to_howto
32  PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
33static struct bfd_hash_entry *elf_m68k_link_hash_newfunc
34  PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
35static struct bfd_link_hash_table *elf_m68k_link_hash_table_create
36  PARAMS ((bfd *));
37static bfd_boolean elf_m68k_check_relocs
38  PARAMS ((bfd *, struct bfd_link_info *, asection *,
39	   const Elf_Internal_Rela *));
40static asection *elf_m68k_gc_mark_hook
41  PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
42	   struct elf_link_hash_entry *, Elf_Internal_Sym *));
43static bfd_boolean elf_m68k_gc_sweep_hook
44  PARAMS ((bfd *, struct bfd_link_info *, asection *,
45	   const Elf_Internal_Rela *));
46static bfd_boolean elf_m68k_adjust_dynamic_symbol
47  PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
48static bfd_boolean elf_m68k_size_dynamic_sections
49  PARAMS ((bfd *, struct bfd_link_info *));
50static bfd_boolean elf_m68k_discard_copies
51  PARAMS ((struct elf_link_hash_entry *, PTR));
52static bfd_boolean elf_m68k_relocate_section
53  PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
54	   Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
55static bfd_boolean elf_m68k_finish_dynamic_symbol
56  PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
57	   Elf_Internal_Sym *));
58static bfd_boolean elf_m68k_finish_dynamic_sections
59  PARAMS ((bfd *, struct bfd_link_info *));
60
61static bfd_boolean elf32_m68k_set_private_flags
62  PARAMS ((bfd *, flagword));
63static bfd_boolean elf32_m68k_merge_private_bfd_data
64  PARAMS ((bfd *, bfd *));
65static bfd_boolean elf32_m68k_print_private_bfd_data
66  PARAMS ((bfd *, PTR));
67static enum elf_reloc_type_class elf32_m68k_reloc_type_class
68  PARAMS ((const Elf_Internal_Rela *));
69
70static reloc_howto_type howto_table[] = {
71  HOWTO(R_68K_NONE,       0, 0, 0, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_NONE",      FALSE, 0, 0x00000000,FALSE),
72  HOWTO(R_68K_32,         0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32",        FALSE, 0, 0xffffffff,FALSE),
73  HOWTO(R_68K_16,         0, 1,16, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16",        FALSE, 0, 0x0000ffff,FALSE),
74  HOWTO(R_68K_8,          0, 0, 8, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8",         FALSE, 0, 0x000000ff,FALSE),
75  HOWTO(R_68K_PC32,       0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32",      FALSE, 0, 0xffffffff,TRUE),
76  HOWTO(R_68K_PC16,       0, 1,16, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PC16",      FALSE, 0, 0x0000ffff,TRUE),
77  HOWTO(R_68K_PC8,        0, 0, 8, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PC8",       FALSE, 0, 0x000000ff,TRUE),
78  HOWTO(R_68K_GOT32,      0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32",     FALSE, 0, 0xffffffff,TRUE),
79  HOWTO(R_68K_GOT16,      0, 1,16, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT16",     FALSE, 0, 0x0000ffff,TRUE),
80  HOWTO(R_68K_GOT8,       0, 0, 8, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT8",      FALSE, 0, 0x000000ff,TRUE),
81  HOWTO(R_68K_GOT32O,     0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O",    FALSE, 0, 0xffffffff,FALSE),
82  HOWTO(R_68K_GOT16O,     0, 1,16, FALSE,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT16O",    FALSE, 0, 0x0000ffff,FALSE),
83  HOWTO(R_68K_GOT8O,      0, 0, 8, FALSE,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_GOT8O",     FALSE, 0, 0x000000ff,FALSE),
84  HOWTO(R_68K_PLT32,      0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32",     FALSE, 0, 0xffffffff,TRUE),
85  HOWTO(R_68K_PLT16,      0, 1,16, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT16",     FALSE, 0, 0x0000ffff,TRUE),
86  HOWTO(R_68K_PLT8,       0, 0, 8, TRUE, 0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT8",      FALSE, 0, 0x000000ff,TRUE),
87  HOWTO(R_68K_PLT32O,     0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O",    FALSE, 0, 0xffffffff,FALSE),
88  HOWTO(R_68K_PLT16O,     0, 1,16, FALSE,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT16O",    FALSE, 0, 0x0000ffff,FALSE),
89  HOWTO(R_68K_PLT8O,      0, 0, 8, FALSE,0, complain_overflow_signed,   bfd_elf_generic_reloc, "R_68K_PLT8O",     FALSE, 0, 0x000000ff,FALSE),
90  HOWTO(R_68K_COPY,       0, 0, 0, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_COPY",      FALSE, 0, 0xffffffff,FALSE),
91  HOWTO(R_68K_GLOB_DAT,   0, 2,32, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_GLOB_DAT",  FALSE, 0, 0xffffffff,FALSE),
92  HOWTO(R_68K_JMP_SLOT,   0, 2,32, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_JMP_SLOT",  FALSE, 0, 0xffffffff,FALSE),
93  HOWTO(R_68K_RELATIVE,   0, 2,32, FALSE,0, complain_overflow_dont,     bfd_elf_generic_reloc, "R_68K_RELATIVE",  FALSE, 0, 0xffffffff,FALSE),
94  /* GNU extension to record C++ vtable hierarchy.  */
95  HOWTO (R_68K_GNU_VTINHERIT,	/* type */
96	 0,			/* rightshift */
97	 2,			/* size (0 = byte, 1 = short, 2 = long) */
98	 0,			/* bitsize */
99	 FALSE,			/* pc_relative */
100	 0,			/* bitpos */
101	 complain_overflow_dont, /* complain_on_overflow */
102	 NULL,			/* special_function */
103	 "R_68K_GNU_VTINHERIT",	/* name */
104	 FALSE,			/* partial_inplace */
105	 0,			/* src_mask */
106	 0,			/* dst_mask */
107	 FALSE),
108  /* GNU extension to record C++ vtable member usage.  */
109  HOWTO (R_68K_GNU_VTENTRY,	/* type */
110	 0,			/* rightshift */
111	 2,			/* size (0 = byte, 1 = short, 2 = long) */
112	 0,			/* bitsize */
113	 FALSE,			/* pc_relative */
114	 0,			/* bitpos */
115	 complain_overflow_dont, /* complain_on_overflow */
116	 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
117	 "R_68K_GNU_VTENTRY",	/* name */
118	 FALSE,			/* partial_inplace */
119	 0,			/* src_mask */
120	 0,			/* dst_mask */
121	 FALSE),
122};
123
124static void
125rtype_to_howto (abfd, cache_ptr, dst)
126     bfd *abfd ATTRIBUTE_UNUSED;
127     arelent *cache_ptr;
128     Elf_Internal_Rela *dst;
129{
130  BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K_max);
131  cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
132}
133
134#define elf_info_to_howto rtype_to_howto
135
136static const struct
137{
138  bfd_reloc_code_real_type bfd_val;
139  int elf_val;
140} reloc_map[] = {
141  { BFD_RELOC_NONE, R_68K_NONE },
142  { BFD_RELOC_32, R_68K_32 },
143  { BFD_RELOC_16, R_68K_16 },
144  { BFD_RELOC_8, R_68K_8 },
145  { BFD_RELOC_32_PCREL, R_68K_PC32 },
146  { BFD_RELOC_16_PCREL, R_68K_PC16 },
147  { BFD_RELOC_8_PCREL, R_68K_PC8 },
148  { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
149  { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
150  { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
151  { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
152  { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
153  { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
154  { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
155  { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
156  { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
157  { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
158  { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
159  { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
160  { BFD_RELOC_NONE, R_68K_COPY },
161  { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
162  { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
163  { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
164  { BFD_RELOC_CTOR, R_68K_32 },
165  { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
166  { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
167};
168
169static reloc_howto_type *
170reloc_type_lookup (abfd, code)
171     bfd *abfd ATTRIBUTE_UNUSED;
172     bfd_reloc_code_real_type code;
173{
174  unsigned int i;
175  for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
176    {
177      if (reloc_map[i].bfd_val == code)
178	return &howto_table[reloc_map[i].elf_val];
179    }
180  return 0;
181}
182
183#define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
184#define ELF_ARCH bfd_arch_m68k
185
186/* Functions for the m68k ELF linker.  */
187
188/* The name of the dynamic interpreter.  This is put in the .interp
189   section.  */
190
191#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
192
193/* The size in bytes of an entry in the procedure linkage table.  */
194
195#define PLT_ENTRY_SIZE 20
196
197/* The first entry in a procedure linkage table looks like this.  See
198   the SVR4 ABI m68k supplement to see how this works.  */
199
200static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
201{
202  0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
203  0, 0, 0, 0,		  /* replaced with offset to .got + 4.  */
204  0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
205  0, 0, 0, 0,		  /* replaced with offset to .got + 8.  */
206  0, 0, 0, 0		  /* pad out to 20 bytes.  */
207};
208
209/* Subsequent entries in a procedure linkage table look like this.  */
210
211static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
212{
213  0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
214  0, 0, 0, 0,		  /* replaced with offset to symbol's .got entry.  */
215  0x2f, 0x3c,		  /* move.l #offset,-(%sp) */
216  0, 0, 0, 0,		  /* replaced with offset into relocation table.  */
217  0x60, 0xff,		  /* bra.l .plt */
218  0, 0, 0, 0		  /* replaced with offset to start of .plt.  */
219};
220
221
222#define CFV4E_PLT_ENTRY_SIZE 24
223
224#define CFV4E_FLAG(abfd)  (elf_elfheader (abfd)->e_flags & EF_M68K_CFV4E)
225
226static const bfd_byte elf_cfv4e_plt0_entry[CFV4E_PLT_ENTRY_SIZE] =
227{
228  0x20, 0x3c,
229  0, 0, 0, 0,             /* Replaced with offset to .got + 4.  */
230  0x2f, 0x3b, 0x08, 0xfa, /* move.l (%pc,addr),-(%sp) */
231  0x20, 0x3c,
232  0, 0, 0, 0,             /* Replaced with offset to .got + 8.  */
233  0x20, 0x7b, 0x08, 0x00, /* move.l (%pc,%d0:l), %a0 */
234  0x4e, 0xd0,             /* jmp (%a0) */
235  0x4e, 0x71		  /* nop */
236};
237
238/* Subsequent entries in a procedure linkage table look like this.  */
239
240static const bfd_byte elf_cfv4e_plt_entry[CFV4E_PLT_ENTRY_SIZE] =
241{
242  0x20, 0x3c,
243  0, 0, 0, 0,             /* Replaced with offset to symbol's .got entry.  */
244  0x20, 0x7b, 0x08, 0x00, /* move.l (%pc,%d0:l), %a0 */
245  0x4e, 0xd0,             /* jmp (%a0) */
246  0x2f, 0x3c,             /* move.l #offset,-(%sp) */
247  0, 0, 0, 0,             /* Replaced with offset into relocation table.  */
248  0x60, 0xff,             /* bra.l .plt */
249  0, 0, 0, 0              /* Replaced with offset to start of .plt.  */
250};
251
252#define CPU32_FLAG(abfd)  (elf_elfheader (abfd)->e_flags & EF_M68K_CPU32)
253
254#define PLT_CPU32_ENTRY_SIZE 24
255/* Procedure linkage table entries for the cpu32 */
256static const bfd_byte elf_cpu32_plt0_entry[PLT_CPU32_ENTRY_SIZE] =
257{
258  0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
259  0, 0, 0, 0,             /* replaced with offset to .got + 4.  */
260  0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
261  0, 0, 0, 0,             /* replace with offset to .got +8.  */
262  0x4e, 0xd1,             /* jmp %a1@ */
263  0, 0, 0, 0,             /* pad out to 24 bytes.  */
264  0, 0
265};
266
267static const bfd_byte elf_cpu32_plt_entry[PLT_CPU32_ENTRY_SIZE] =
268{
269  0x22, 0x7b, 0x01, 0x70,  /* moveal %pc@(0xc), %a1 */
270  0, 0, 0, 0,              /* replaced with offset to symbol's .got entry.  */
271  0x4e, 0xd1,              /* jmp %a1@ */
272  0x2f, 0x3c,              /* move.l #offset,-(%sp) */
273  0, 0, 0, 0,              /* replaced with offset into relocation table.  */
274  0x60, 0xff,              /* bra.l .plt */
275  0, 0, 0, 0,              /* replaced with offset to start of .plt.  */
276  0, 0
277};
278
279/* The m68k linker needs to keep track of the number of relocs that it
280   decides to copy in check_relocs for each symbol.  This is so that it
281   can discard PC relative relocs if it doesn't need them when linking
282   with -Bsymbolic.  We store the information in a field extending the
283   regular ELF linker hash table.  */
284
285/* This structure keeps track of the number of PC relative relocs we have
286   copied for a given symbol.  */
287
288struct elf_m68k_pcrel_relocs_copied
289{
290  /* Next section.  */
291  struct elf_m68k_pcrel_relocs_copied *next;
292  /* A section in dynobj.  */
293  asection *section;
294  /* Number of relocs copied in this section.  */
295  bfd_size_type count;
296};
297
298/* m68k ELF linker hash entry.  */
299
300struct elf_m68k_link_hash_entry
301{
302  struct elf_link_hash_entry root;
303
304  /* Number of PC relative relocs copied for this symbol.  */
305  struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
306};
307
308#define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
309
310/* m68k ELF linker hash table.  */
311
312struct elf_m68k_link_hash_table
313{
314  struct elf_link_hash_table root;
315
316  /* Small local sym to section mapping cache.  */
317  struct sym_sec_cache sym_sec;
318};
319
320/* Get the m68k ELF linker hash table from a link_info structure.  */
321
322#define elf_m68k_hash_table(p) \
323  ((struct elf_m68k_link_hash_table *) (p)->hash)
324
325/* Create an entry in an m68k ELF linker hash table.  */
326
327static struct bfd_hash_entry *
328elf_m68k_link_hash_newfunc (entry, table, string)
329     struct bfd_hash_entry *entry;
330     struct bfd_hash_table *table;
331     const char *string;
332{
333  struct bfd_hash_entry *ret = entry;
334
335  /* Allocate the structure if it has not already been allocated by a
336     subclass.  */
337  if (ret == NULL)
338    ret = bfd_hash_allocate (table,
339			     sizeof (struct elf_m68k_link_hash_entry));
340  if (ret == NULL)
341    return ret;
342
343  /* Call the allocation method of the superclass.  */
344  ret = _bfd_elf_link_hash_newfunc (ret, table, string);
345  if (ret != NULL)
346    elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
347
348  return ret;
349}
350
351/* Create an m68k ELF linker hash table.  */
352
353static struct bfd_link_hash_table *
354elf_m68k_link_hash_table_create (abfd)
355     bfd *abfd;
356{
357  struct elf_m68k_link_hash_table *ret;
358  bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
359
360  ret = (struct elf_m68k_link_hash_table *) bfd_malloc (amt);
361  if (ret == (struct elf_m68k_link_hash_table *) NULL)
362    return NULL;
363
364  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
365				      elf_m68k_link_hash_newfunc,
366				      sizeof (struct elf_m68k_link_hash_entry)))
367    {
368      free (ret);
369      return NULL;
370    }
371
372  ret->sym_sec.abfd = NULL;
373
374  return &ret->root.root;
375}
376
377/* Set the right machine number.  */
378
379static bfd_boolean
380elf32_m68k_object_p (bfd *abfd)
381{
382  unsigned int mach = 0;
383  unsigned features = 0;
384  flagword eflags = elf_elfheader (abfd)->e_flags;
385
386  if (eflags & EF_M68K_M68000)
387    features |= m68000;
388  else if (eflags & EF_M68K_CPU32)
389    features |= cpu32;
390  else if (eflags & EF_M68K_ISA_MASK)
391    {
392      switch (eflags & EF_M68K_ISA_MASK)
393	{
394	case EF_M68K_ISA_A_NODIV:
395	  features |= mcfisa_a;
396	  break;
397	case EF_M68K_ISA_A:
398	  features |= mcfisa_a|mcfhwdiv;
399	  break;
400	case EF_M68K_ISA_A_PLUS:
401	  features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
402	  break;
403	case EF_M68K_ISA_B_NOUSP:
404	  features |= mcfisa_a|mcfisa_b|mcfhwdiv;
405	  break;
406	case EF_M68K_ISA_B:
407	  features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
408	  break;
409	}
410      switch (eflags & EF_M68K_MAC_MASK)
411	{
412	case EF_M68K_MAC:
413	  features |= mcfmac;
414	  break;
415	case EF_M68K_EMAC:
416	  features |= mcfemac;
417	  break;
418	}
419      if (eflags & EF_M68K_FLOAT)
420	features |= cfloat;
421    }
422
423  mach = bfd_m68k_features_to_mach (features);
424  bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
425
426  return TRUE;
427}
428
429/* Keep m68k-specific flags in the ELF header.  */
430static bfd_boolean
431elf32_m68k_set_private_flags (abfd, flags)
432     bfd *abfd;
433     flagword flags;
434{
435  elf_elfheader (abfd)->e_flags = flags;
436  elf_flags_init (abfd) = TRUE;
437  return TRUE;
438}
439
440/* Merge backend specific data from an object file to the output
441   object file when linking.  */
442static bfd_boolean
443elf32_m68k_merge_private_bfd_data (ibfd, obfd)
444     bfd *ibfd;
445     bfd *obfd;
446{
447  flagword out_flags;
448  flagword in_flags;
449  flagword out_isa;
450  flagword in_isa;
451  const bfd_arch_info_type *arch_info;
452
453  if (   bfd_get_flavour (ibfd) != bfd_target_elf_flavour
454      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
455    return FALSE;
456
457  /* Get the merged machine.  This checks for incompatibility between
458     Coldfire & non-Coldfire flags, incompability between different
459     Coldfire ISAs, and incompability between different MAC types.  */
460  arch_info = bfd_arch_get_compatible (ibfd, obfd, FALSE);
461  if (!arch_info)
462    return FALSE;
463
464  bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
465
466  in_flags = elf_elfheader (ibfd)->e_flags;
467  if (!elf_flags_init (obfd))
468    {
469      elf_flags_init (obfd) = TRUE;
470      out_flags = in_flags;
471    }
472  else
473    {
474      out_flags = elf_elfheader (obfd)->e_flags;
475      in_isa = (in_flags & EF_M68K_ISA_MASK);
476      out_isa = (out_flags & EF_M68K_ISA_MASK);
477      if (in_isa > out_isa)
478	out_flags ^= in_isa ^ out_isa;
479      out_flags |= in_flags ^ in_isa;
480    }
481  elf_elfheader (obfd)->e_flags = out_flags;
482
483  return TRUE;
484}
485
486/* Display the flags field.  */
487static bfd_boolean
488elf32_m68k_print_private_bfd_data (abfd, ptr)
489     bfd *abfd;
490     PTR ptr;
491{
492  FILE *file = (FILE *) ptr;
493  flagword eflags = elf_elfheader (abfd)->e_flags;
494
495  BFD_ASSERT (abfd != NULL && ptr != NULL);
496
497  /* Print normal ELF private data.  */
498  _bfd_elf_print_private_bfd_data (abfd, ptr);
499
500  /* Ignore init flag - it may not be set, despite the flags field containing valid data.  */
501
502  /* xgettext:c-format */
503  fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
504
505  if (eflags & EF_M68K_CPU32)
506    fprintf (file, " [cpu32]");
507
508  if (eflags & EF_M68K_M68000)
509    fprintf (file, " [m68000]");
510
511  if (eflags & EF_M68K_CFV4E)
512    fprintf (file, " [cfv4e]");
513
514  if (eflags & EF_M68K_ISA_MASK)
515    {
516      char const *isa = _("unknown");
517      char const *mac = _("unknown");
518      char const *additional = "";
519
520      switch (eflags & EF_M68K_ISA_MASK)
521	{
522	case EF_M68K_ISA_A_NODIV:
523	  isa = "A";
524	  additional = " [nodiv]";
525	  break;
526	case EF_M68K_ISA_A:
527	  isa = "A";
528	  break;
529	case EF_M68K_ISA_A_PLUS:
530	  isa = "A+";
531	  break;
532	case EF_M68K_ISA_B_NOUSP:
533	  isa = "B";
534	  additional = " [nousp]";
535	  break;
536	case EF_M68K_ISA_B:
537	  isa = "B";
538	  break;
539	}
540      fprintf (file, " [isa %s]%s", isa, additional);
541      if (eflags & EF_M68K_FLOAT)
542	fprintf (file, " [float]");
543      switch (eflags & EF_M68K_MAC_MASK)
544	{
545	case 0:
546	  mac = NULL;
547	  break;
548	case EF_M68K_MAC:
549	  mac = "mac";
550	  break;
551	case EF_M68K_EMAC:
552	  mac = "emac";
553	  break;
554	}
555      if (mac)
556	fprintf (file, " [%s]", mac);
557    }
558
559  fputc ('\n', file);
560
561  return TRUE;
562}
563/* Look through the relocs for a section during the first phase, and
564   allocate space in the global offset table or procedure linkage
565   table.  */
566
567static bfd_boolean
568elf_m68k_check_relocs (abfd, info, sec, relocs)
569     bfd *abfd;
570     struct bfd_link_info *info;
571     asection *sec;
572     const Elf_Internal_Rela *relocs;
573{
574  bfd *dynobj;
575  Elf_Internal_Shdr *symtab_hdr;
576  struct elf_link_hash_entry **sym_hashes;
577  bfd_signed_vma *local_got_refcounts;
578  const Elf_Internal_Rela *rel;
579  const Elf_Internal_Rela *rel_end;
580  asection *sgot;
581  asection *srelgot;
582  asection *sreloc;
583
584  if (info->relocatable)
585    return TRUE;
586
587  dynobj = elf_hash_table (info)->dynobj;
588  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
589  sym_hashes = elf_sym_hashes (abfd);
590  local_got_refcounts = elf_local_got_refcounts (abfd);
591
592  sgot = NULL;
593  srelgot = NULL;
594  sreloc = NULL;
595
596  rel_end = relocs + sec->reloc_count;
597  for (rel = relocs; rel < rel_end; rel++)
598    {
599      unsigned long r_symndx;
600      struct elf_link_hash_entry *h;
601
602      r_symndx = ELF32_R_SYM (rel->r_info);
603
604      if (r_symndx < symtab_hdr->sh_info)
605	h = NULL;
606      else
607	{
608	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
609	  while (h->root.type == bfd_link_hash_indirect
610		 || h->root.type == bfd_link_hash_warning)
611	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
612	}
613
614      switch (ELF32_R_TYPE (rel->r_info))
615	{
616	case R_68K_GOT8:
617	case R_68K_GOT16:
618	case R_68K_GOT32:
619	  if (h != NULL
620	      && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
621	    break;
622	  /* Fall through.  */
623	case R_68K_GOT8O:
624	case R_68K_GOT16O:
625	case R_68K_GOT32O:
626	  /* This symbol requires a global offset table entry.  */
627
628	  if (dynobj == NULL)
629	    {
630	      /* Create the .got section.  */
631	      elf_hash_table (info)->dynobj = dynobj = abfd;
632	      if (!_bfd_elf_create_got_section (dynobj, info))
633		return FALSE;
634	    }
635
636	  if (sgot == NULL)
637	    {
638	      sgot = bfd_get_section_by_name (dynobj, ".got");
639	      BFD_ASSERT (sgot != NULL);
640	    }
641
642	  if (srelgot == NULL
643	      && (h != NULL || info->shared))
644	    {
645	      srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
646	      if (srelgot == NULL)
647		{
648		  srelgot = bfd_make_section_with_flags (dynobj,
649							 ".rela.got",
650							 (SEC_ALLOC
651							  | SEC_LOAD
652							  | SEC_HAS_CONTENTS
653							  | SEC_IN_MEMORY
654							  | SEC_LINKER_CREATED
655							  | SEC_READONLY));
656		  if (srelgot == NULL
657		      || !bfd_set_section_alignment (dynobj, srelgot, 2))
658		    return FALSE;
659		}
660	    }
661
662	  if (h != NULL)
663	    {
664	      if (h->got.refcount == 0)
665		{
666		  /* Make sure this symbol is output as a dynamic symbol.  */
667		  if (h->dynindx == -1
668		      && !h->forced_local)
669		    {
670		      if (!bfd_elf_link_record_dynamic_symbol (info, h))
671			return FALSE;
672		    }
673
674		  /* Allocate space in the .got section.  */
675		  sgot->size += 4;
676		  /* Allocate relocation space.  */
677		  srelgot->size += sizeof (Elf32_External_Rela);
678		}
679	      h->got.refcount++;
680	    }
681	  else
682	    {
683	      /* This is a global offset table entry for a local symbol.  */
684	      if (local_got_refcounts == NULL)
685		{
686		  bfd_size_type size;
687
688		  size = symtab_hdr->sh_info;
689		  size *= sizeof (bfd_signed_vma);
690		  local_got_refcounts = ((bfd_signed_vma *)
691					 bfd_zalloc (abfd, size));
692		  if (local_got_refcounts == NULL)
693		    return FALSE;
694		  elf_local_got_refcounts (abfd) = local_got_refcounts;
695		}
696	      if (local_got_refcounts[r_symndx] == 0)
697		{
698		  sgot->size += 4;
699		  if (info->shared)
700		    {
701		      /* If we are generating a shared object, we need to
702			 output a R_68K_RELATIVE reloc so that the dynamic
703			 linker can adjust this GOT entry.  */
704		      srelgot->size += sizeof (Elf32_External_Rela);
705		    }
706		}
707	      local_got_refcounts[r_symndx]++;
708	    }
709	  break;
710
711	case R_68K_PLT8:
712	case R_68K_PLT16:
713	case R_68K_PLT32:
714	  /* This symbol requires a procedure linkage table entry.  We
715	     actually build the entry in adjust_dynamic_symbol,
716             because this might be a case of linking PIC code which is
717             never referenced by a dynamic object, in which case we
718             don't need to generate a procedure linkage table entry
719             after all.  */
720
721	  /* If this is a local symbol, we resolve it directly without
722	     creating a procedure linkage table entry.  */
723	  if (h == NULL)
724	    continue;
725
726	  h->needs_plt = 1;
727	  h->plt.refcount++;
728	  break;
729
730	case R_68K_PLT8O:
731	case R_68K_PLT16O:
732	case R_68K_PLT32O:
733	  /* This symbol requires a procedure linkage table entry.  */
734
735	  if (h == NULL)
736	    {
737	      /* It does not make sense to have this relocation for a
738		 local symbol.  FIXME: does it?  How to handle it if
739		 it does make sense?  */
740	      bfd_set_error (bfd_error_bad_value);
741	      return FALSE;
742	    }
743
744	  /* Make sure this symbol is output as a dynamic symbol.  */
745	  if (h->dynindx == -1
746	      && !h->forced_local)
747	    {
748	      if (!bfd_elf_link_record_dynamic_symbol (info, h))
749		return FALSE;
750	    }
751
752	  h->needs_plt = 1;
753	  h->plt.refcount++;
754	  break;
755
756	case R_68K_PC8:
757	case R_68K_PC16:
758	case R_68K_PC32:
759	  /* If we are creating a shared library and this is not a local
760	     symbol, we need to copy the reloc into the shared library.
761	     However when linking with -Bsymbolic and this is a global
762	     symbol which is defined in an object we are including in the
763	     link (i.e., DEF_REGULAR is set), then we can resolve the
764	     reloc directly.  At this point we have not seen all the input
765	     files, so it is possible that DEF_REGULAR is not set now but
766	     will be set later (it is never cleared).  We account for that
767	     possibility below by storing information in the
768	     pcrel_relocs_copied field of the hash table entry.  */
769	  if (!(info->shared
770		&& (sec->flags & SEC_ALLOC) != 0
771		&& h != NULL
772		&& (!info->symbolic
773		    || h->root.type == bfd_link_hash_defweak
774		    || !h->def_regular)))
775	    {
776	      if (h != NULL)
777		{
778		  /* Make sure a plt entry is created for this symbol if
779		     it turns out to be a function defined by a dynamic
780		     object.  */
781		  h->plt.refcount++;
782		}
783	      break;
784	    }
785	  /* Fall through.  */
786	case R_68K_8:
787	case R_68K_16:
788	case R_68K_32:
789	  if (h != NULL)
790	    {
791	      /* Make sure a plt entry is created for this symbol if it
792		 turns out to be a function defined by a dynamic object.  */
793	      h->plt.refcount++;
794	    }
795
796	  /* If we are creating a shared library, we need to copy the
797	     reloc into the shared library.  */
798	  if (info->shared
799	      && (sec->flags & SEC_ALLOC) != 0)
800	    {
801	      /* When creating a shared object, we must copy these
802		 reloc types into the output file.  We create a reloc
803		 section in dynobj and make room for this reloc.  */
804	      if (sreloc == NULL)
805		{
806		  const char *name;
807
808		  name = (bfd_elf_string_from_elf_section
809			  (abfd,
810			   elf_elfheader (abfd)->e_shstrndx,
811			   elf_section_data (sec)->rel_hdr.sh_name));
812		  if (name == NULL)
813		    return FALSE;
814
815		  BFD_ASSERT (strncmp (name, ".rela", 5) == 0
816			      && strcmp (bfd_get_section_name (abfd, sec),
817					 name + 5) == 0);
818
819		  sreloc = bfd_get_section_by_name (dynobj, name);
820		  if (sreloc == NULL)
821		    {
822		      sreloc = bfd_make_section_with_flags (dynobj,
823							    name,
824							    (SEC_ALLOC
825							     | SEC_LOAD
826							     | SEC_HAS_CONTENTS
827							     | SEC_IN_MEMORY
828							     | SEC_LINKER_CREATED
829							     | SEC_READONLY));
830		      if (sreloc == NULL
831			  || !bfd_set_section_alignment (dynobj, sreloc, 2))
832			return FALSE;
833		    }
834		  elf_section_data (sec)->sreloc = sreloc;
835		}
836
837	      if (sec->flags & SEC_READONLY
838		  /* Don't set DF_TEXTREL yet for PC relative
839		     relocations, they might be discarded later.  */
840		  && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
841		       || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
842		       || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
843		    info->flags |= DF_TEXTREL;
844
845	      sreloc->size += sizeof (Elf32_External_Rela);
846
847	      /* We count the number of PC relative relocations we have
848		 entered for this symbol, so that we can discard them
849		 again if, in the -Bsymbolic case, the symbol is later
850		 defined by a regular object, or, in the normal shared
851		 case, the symbol is forced to be local.  Note that this
852		 function is only called if we are using an m68kelf linker
853		 hash table, which means that h is really a pointer to an
854		 elf_m68k_link_hash_entry.  */
855	      if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
856		  || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
857		  || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
858		{
859		  struct elf_m68k_pcrel_relocs_copied *p;
860		  struct elf_m68k_pcrel_relocs_copied **head;
861
862		  if (h != NULL)
863		    {
864		      struct elf_m68k_link_hash_entry *eh
865			= elf_m68k_hash_entry (h);
866		      head = &eh->pcrel_relocs_copied;
867		    }
868		  else
869		    {
870		      asection *s;
871		      void *vpp;
872
873		      s = (bfd_section_from_r_symndx
874			   (abfd, &elf_m68k_hash_table (info)->sym_sec,
875			    sec, r_symndx));
876		      if (s == NULL)
877			return FALSE;
878
879		      vpp = &elf_section_data (s)->local_dynrel;
880		      head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
881		    }
882
883		  for (p = *head; p != NULL; p = p->next)
884		    if (p->section == sreloc)
885		      break;
886
887		  if (p == NULL)
888		    {
889		      p = ((struct elf_m68k_pcrel_relocs_copied *)
890			   bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
891		      if (p == NULL)
892			return FALSE;
893		      p->next = *head;
894		      *head = p;
895		      p->section = sreloc;
896		      p->count = 0;
897		    }
898
899		  ++p->count;
900		}
901	    }
902
903	  break;
904
905	  /* This relocation describes the C++ object vtable hierarchy.
906	     Reconstruct it for later use during GC.  */
907	case R_68K_GNU_VTINHERIT:
908	  if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
909	    return FALSE;
910	  break;
911
912	  /* This relocation describes which C++ vtable entries are actually
913	     used.  Record for later use during GC.  */
914	case R_68K_GNU_VTENTRY:
915	  if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
916	    return FALSE;
917	  break;
918
919	default:
920	  break;
921	}
922    }
923
924  return TRUE;
925}
926
927/* Return the section that should be marked against GC for a given
928   relocation.  */
929
930static asection *
931elf_m68k_gc_mark_hook (sec, info, rel, h, sym)
932     asection *sec;
933     struct bfd_link_info *info ATTRIBUTE_UNUSED;
934     Elf_Internal_Rela *rel;
935     struct elf_link_hash_entry *h;
936     Elf_Internal_Sym *sym;
937{
938  if (h != NULL)
939    {
940      switch (ELF32_R_TYPE (rel->r_info))
941	{
942	case R_68K_GNU_VTINHERIT:
943	case R_68K_GNU_VTENTRY:
944	  break;
945
946	default:
947	  switch (h->root.type)
948	    {
949	    default:
950	      break;
951
952	    case bfd_link_hash_defined:
953	    case bfd_link_hash_defweak:
954	      return h->root.u.def.section;
955
956	    case bfd_link_hash_common:
957	      return h->root.u.c.p->section;
958	    }
959	}
960    }
961  else
962    return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
963
964  return NULL;
965}
966
967/* Update the got entry reference counts for the section being removed.  */
968
969static bfd_boolean
970elf_m68k_gc_sweep_hook (abfd, info, sec, relocs)
971     bfd *abfd;
972     struct bfd_link_info *info;
973     asection *sec;
974     const Elf_Internal_Rela *relocs;
975{
976  Elf_Internal_Shdr *symtab_hdr;
977  struct elf_link_hash_entry **sym_hashes;
978  bfd_signed_vma *local_got_refcounts;
979  const Elf_Internal_Rela *rel, *relend;
980  bfd *dynobj;
981  asection *sgot;
982  asection *srelgot;
983
984  dynobj = elf_hash_table (info)->dynobj;
985  if (dynobj == NULL)
986    return TRUE;
987
988  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
989  sym_hashes = elf_sym_hashes (abfd);
990  local_got_refcounts = elf_local_got_refcounts (abfd);
991
992  sgot = bfd_get_section_by_name (dynobj, ".got");
993  srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
994
995  relend = relocs + sec->reloc_count;
996  for (rel = relocs; rel < relend; rel++)
997    {
998      unsigned long r_symndx;
999      struct elf_link_hash_entry *h = NULL;
1000
1001      r_symndx = ELF32_R_SYM (rel->r_info);
1002      if (r_symndx >= symtab_hdr->sh_info)
1003	{
1004	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1005	  while (h->root.type == bfd_link_hash_indirect
1006		 || h->root.type == bfd_link_hash_warning)
1007	    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1008	}
1009
1010      switch (ELF32_R_TYPE (rel->r_info))
1011	{
1012	case R_68K_GOT8:
1013	case R_68K_GOT16:
1014	case R_68K_GOT32:
1015	case R_68K_GOT8O:
1016	case R_68K_GOT16O:
1017	case R_68K_GOT32O:
1018	  if (h != NULL)
1019	    {
1020	      if (h->got.refcount > 0)
1021		{
1022		  --h->got.refcount;
1023		  if (h->got.refcount == 0)
1024		    {
1025		      /* We don't need the .got entry any more.  */
1026		      sgot->size -= 4;
1027		      srelgot->size -= sizeof (Elf32_External_Rela);
1028		    }
1029		}
1030	    }
1031	  else if (local_got_refcounts != NULL)
1032	    {
1033	      if (local_got_refcounts[r_symndx] > 0)
1034		{
1035		  --local_got_refcounts[r_symndx];
1036		  if (local_got_refcounts[r_symndx] == 0)
1037		    {
1038		      /* We don't need the .got entry any more.  */
1039		      sgot->size -= 4;
1040		      if (info->shared)
1041			srelgot->size -= sizeof (Elf32_External_Rela);
1042		    }
1043		}
1044	    }
1045	  break;
1046
1047	case R_68K_PLT8:
1048	case R_68K_PLT16:
1049	case R_68K_PLT32:
1050	case R_68K_PLT8O:
1051	case R_68K_PLT16O:
1052	case R_68K_PLT32O:
1053	case R_68K_PC8:
1054	case R_68K_PC16:
1055	case R_68K_PC32:
1056	case R_68K_8:
1057	case R_68K_16:
1058	case R_68K_32:
1059	  if (h != NULL)
1060	    {
1061	      if (h->plt.refcount > 0)
1062		--h->plt.refcount;
1063	    }
1064	  break;
1065
1066	default:
1067	  break;
1068	}
1069    }
1070
1071  return TRUE;
1072}
1073
1074/* Adjust a symbol defined by a dynamic object and referenced by a
1075   regular object.  The current definition is in some section of the
1076   dynamic object, but we're not including those sections.  We have to
1077   change the definition to something the rest of the link can
1078   understand.  */
1079
1080static bfd_boolean
1081elf_m68k_adjust_dynamic_symbol (info, h)
1082     struct bfd_link_info *info;
1083     struct elf_link_hash_entry *h;
1084{
1085  bfd *dynobj;
1086  asection *s;
1087  unsigned int power_of_two;
1088
1089  dynobj = elf_hash_table (info)->dynobj;
1090
1091  /* Make sure we know what is going on here.  */
1092  BFD_ASSERT (dynobj != NULL
1093	      && (h->needs_plt
1094		  || h->u.weakdef != NULL
1095		  || (h->def_dynamic
1096		      && h->ref_regular
1097		      && !h->def_regular)));
1098
1099  /* If this is a function, put it in the procedure linkage table.  We
1100     will fill in the contents of the procedure linkage table later,
1101     when we know the address of the .got section.  */
1102  if (h->type == STT_FUNC
1103      || h->needs_plt)
1104    {
1105      if ((h->plt.refcount <= 0
1106           || SYMBOL_CALLS_LOCAL (info, h)
1107	   || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1108	       && h->root.type == bfd_link_hash_undefweak))
1109	  /* We must always create the plt entry if it was referenced
1110	     by a PLTxxO relocation.  In this case we already recorded
1111	     it as a dynamic symbol.  */
1112	  && h->dynindx == -1)
1113	{
1114	  /* This case can occur if we saw a PLTxx reloc in an input
1115	     file, but the symbol was never referred to by a dynamic
1116	     object, or if all references were garbage collected.  In
1117	     such a case, we don't actually need to build a procedure
1118	     linkage table, and we can just do a PCxx reloc instead.  */
1119	  h->plt.offset = (bfd_vma) -1;
1120	  h->needs_plt = 0;
1121	  return TRUE;
1122	}
1123
1124      /* Make sure this symbol is output as a dynamic symbol.  */
1125      if (h->dynindx == -1
1126	  && !h->forced_local)
1127	{
1128	  if (! bfd_elf_link_record_dynamic_symbol (info, h))
1129	    return FALSE;
1130	}
1131
1132      s = bfd_get_section_by_name (dynobj, ".plt");
1133      BFD_ASSERT (s != NULL);
1134
1135      /* If this is the first .plt entry, make room for the special
1136	 first entry.  */
1137      if (s->size == 0)
1138	{
1139	  if (CPU32_FLAG (dynobj))
1140	    s->size += PLT_CPU32_ENTRY_SIZE;
1141	  else if (CFV4E_FLAG (dynobj))
1142	    s->size += CFV4E_PLT_ENTRY_SIZE;
1143	  else
1144	    s->size += PLT_ENTRY_SIZE;
1145	}
1146
1147      /* If this symbol is not defined in a regular file, and we are
1148	 not generating a shared library, then set the symbol to this
1149	 location in the .plt.  This is required to make function
1150	 pointers compare as equal between the normal executable and
1151	 the shared library.  */
1152      if (!info->shared
1153	  && !h->def_regular)
1154	{
1155	  h->root.u.def.section = s;
1156	  h->root.u.def.value = s->size;
1157	}
1158
1159      h->plt.offset = s->size;
1160
1161      /* Make room for this entry.  */
1162      if (CPU32_FLAG (dynobj))
1163        s->size += PLT_CPU32_ENTRY_SIZE;
1164      else if (CFV4E_FLAG (dynobj))
1165	s->size += CFV4E_PLT_ENTRY_SIZE;
1166      else
1167        s->size += PLT_ENTRY_SIZE;
1168
1169      /* We also need to make an entry in the .got.plt section, which
1170	 will be placed in the .got section by the linker script.  */
1171      s = bfd_get_section_by_name (dynobj, ".got.plt");
1172      BFD_ASSERT (s != NULL);
1173      s->size += 4;
1174
1175      /* We also need to make an entry in the .rela.plt section.  */
1176      s = bfd_get_section_by_name (dynobj, ".rela.plt");
1177      BFD_ASSERT (s != NULL);
1178      s->size += sizeof (Elf32_External_Rela);
1179
1180      return TRUE;
1181    }
1182
1183  /* Reinitialize the plt offset now that it is not used as a reference
1184     count any more.  */
1185  h->plt.offset = (bfd_vma) -1;
1186
1187  /* If this is a weak symbol, and there is a real definition, the
1188     processor independent code will have arranged for us to see the
1189     real definition first, and we can just use the same value.  */
1190  if (h->u.weakdef != NULL)
1191    {
1192      BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1193		  || h->u.weakdef->root.type == bfd_link_hash_defweak);
1194      h->root.u.def.section = h->u.weakdef->root.u.def.section;
1195      h->root.u.def.value = h->u.weakdef->root.u.def.value;
1196      return TRUE;
1197    }
1198
1199  /* This is a reference to a symbol defined by a dynamic object which
1200     is not a function.  */
1201
1202  /* If we are creating a shared library, we must presume that the
1203     only references to the symbol are via the global offset table.
1204     For such cases we need not do anything here; the relocations will
1205     be handled correctly by relocate_section.  */
1206  if (info->shared)
1207    return TRUE;
1208
1209  if (h->size == 0)
1210    {
1211      (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1212			     h->root.root.string);
1213      return TRUE;
1214    }
1215
1216  /* We must allocate the symbol in our .dynbss section, which will
1217     become part of the .bss section of the executable.  There will be
1218     an entry for this symbol in the .dynsym section.  The dynamic
1219     object will contain position independent code, so all references
1220     from the dynamic object to this symbol will go through the global
1221     offset table.  The dynamic linker will use the .dynsym entry to
1222     determine the address it must put in the global offset table, so
1223     both the dynamic object and the regular object will refer to the
1224     same memory location for the variable.  */
1225
1226  s = bfd_get_section_by_name (dynobj, ".dynbss");
1227  BFD_ASSERT (s != NULL);
1228
1229  /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1230     copy the initial value out of the dynamic object and into the
1231     runtime process image.  We need to remember the offset into the
1232     .rela.bss section we are going to use.  */
1233  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1234    {
1235      asection *srel;
1236
1237      srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1238      BFD_ASSERT (srel != NULL);
1239      srel->size += sizeof (Elf32_External_Rela);
1240      h->needs_copy = 1;
1241    }
1242
1243  /* We need to figure out the alignment required for this symbol.  I
1244     have no idea how ELF linkers handle this.  */
1245  power_of_two = bfd_log2 (h->size);
1246  if (power_of_two > 3)
1247    power_of_two = 3;
1248
1249  /* Apply the required alignment.  */
1250  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1251  if (power_of_two > bfd_get_section_alignment (dynobj, s))
1252    {
1253      if (!bfd_set_section_alignment (dynobj, s, power_of_two))
1254	return FALSE;
1255    }
1256
1257  /* Define the symbol as being at this point in the section.  */
1258  h->root.u.def.section = s;
1259  h->root.u.def.value = s->size;
1260
1261  /* Increment the section size to make room for the symbol.  */
1262  s->size += h->size;
1263
1264  return TRUE;
1265}
1266
1267/* Set the sizes of the dynamic sections.  */
1268
1269static bfd_boolean
1270elf_m68k_size_dynamic_sections (output_bfd, info)
1271     bfd *output_bfd ATTRIBUTE_UNUSED;
1272     struct bfd_link_info *info;
1273{
1274  bfd *dynobj;
1275  asection *s;
1276  bfd_boolean plt;
1277  bfd_boolean relocs;
1278
1279  dynobj = elf_hash_table (info)->dynobj;
1280  BFD_ASSERT (dynobj != NULL);
1281
1282  if (elf_hash_table (info)->dynamic_sections_created)
1283    {
1284      /* Set the contents of the .interp section to the interpreter.  */
1285      if (info->executable && !info->static_link)
1286	{
1287	  s = bfd_get_section_by_name (dynobj, ".interp");
1288	  BFD_ASSERT (s != NULL);
1289	  s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1290	  s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1291	}
1292    }
1293  else
1294    {
1295      /* We may have created entries in the .rela.got section.
1296	 However, if we are not creating the dynamic sections, we will
1297	 not actually use these entries.  Reset the size of .rela.got,
1298	 which will cause it to get stripped from the output file
1299	 below.  */
1300      s = bfd_get_section_by_name (dynobj, ".rela.got");
1301      if (s != NULL)
1302	s->size = 0;
1303    }
1304
1305  /* If this is a -Bsymbolic shared link, then we need to discard all
1306     PC relative relocs against symbols defined in a regular object.
1307     For the normal shared case we discard the PC relative relocs
1308     against symbols that have become local due to visibility changes.
1309     We allocated space for them in the check_relocs routine, but we
1310     will not fill them in in the relocate_section routine.  */
1311  if (info->shared)
1312    elf_link_hash_traverse (elf_hash_table (info),
1313			    elf_m68k_discard_copies,
1314			    (PTR) info);
1315
1316  /* The check_relocs and adjust_dynamic_symbol entry points have
1317     determined the sizes of the various dynamic sections.  Allocate
1318     memory for them.  */
1319  plt = FALSE;
1320  relocs = FALSE;
1321  for (s = dynobj->sections; s != NULL; s = s->next)
1322    {
1323      const char *name;
1324
1325      if ((s->flags & SEC_LINKER_CREATED) == 0)
1326	continue;
1327
1328      /* It's OK to base decisions on the section name, because none
1329	 of the dynobj section names depend upon the input files.  */
1330      name = bfd_get_section_name (dynobj, s);
1331
1332      if (strcmp (name, ".plt") == 0)
1333	{
1334	  /* Remember whether there is a PLT.  */
1335	  plt = s->size != 0;
1336	}
1337      else if (strncmp (name, ".rela", 5) == 0)
1338	{
1339	  if (s->size != 0)
1340	    {
1341	      relocs = TRUE;
1342
1343	      /* We use the reloc_count field as a counter if we need
1344		 to copy relocs into the output file.  */
1345	      s->reloc_count = 0;
1346	    }
1347	}
1348      else if (strncmp (name, ".got", 4) != 0
1349	       && strcmp (name, ".dynbss") != 0)
1350	{
1351	  /* It's not one of our sections, so don't allocate space.  */
1352	  continue;
1353	}
1354
1355      if (s->size == 0)
1356	{
1357	  /* If we don't need this section, strip it from the
1358	     output file.  This is mostly to handle .rela.bss and
1359	     .rela.plt.  We must create both sections in
1360	     create_dynamic_sections, because they must be created
1361	     before the linker maps input sections to output
1362	     sections.  The linker does that before
1363	     adjust_dynamic_symbol is called, and it is that
1364	     function which decides whether anything needs to go
1365	     into these sections.  */
1366	  s->flags |= SEC_EXCLUDE;
1367	  continue;
1368	}
1369
1370      if ((s->flags & SEC_HAS_CONTENTS) == 0)
1371	continue;
1372
1373      /* Allocate memory for the section contents.  */
1374      /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1375	 Unused entries should be reclaimed before the section's contents
1376	 are written out, but at the moment this does not happen.  Thus in
1377	 order to prevent writing out garbage, we initialise the section's
1378	 contents to zero.  */
1379      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1380      if (s->contents == NULL)
1381	return FALSE;
1382    }
1383
1384  if (elf_hash_table (info)->dynamic_sections_created)
1385    {
1386      /* Add some entries to the .dynamic section.  We fill in the
1387	 values later, in elf_m68k_finish_dynamic_sections, but we
1388	 must add the entries now so that we get the correct size for
1389	 the .dynamic section.  The DT_DEBUG entry is filled in by the
1390	 dynamic linker and used by the debugger.  */
1391#define add_dynamic_entry(TAG, VAL) \
1392  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1393
1394      if (!info->shared)
1395	{
1396	  if (!add_dynamic_entry (DT_DEBUG, 0))
1397	    return FALSE;
1398	}
1399
1400      if (plt)
1401	{
1402	  if (!add_dynamic_entry (DT_PLTGOT, 0)
1403	      || !add_dynamic_entry (DT_PLTRELSZ, 0)
1404	      || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1405	      || !add_dynamic_entry (DT_JMPREL, 0))
1406	    return FALSE;
1407	}
1408
1409      if (relocs)
1410	{
1411	  if (!add_dynamic_entry (DT_RELA, 0)
1412	      || !add_dynamic_entry (DT_RELASZ, 0)
1413	      || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1414	    return FALSE;
1415	}
1416
1417      if ((info->flags & DF_TEXTREL) != 0)
1418	{
1419	  if (!add_dynamic_entry (DT_TEXTREL, 0))
1420	    return FALSE;
1421	}
1422    }
1423#undef add_dynamic_entry
1424
1425  return TRUE;
1426}
1427
1428/* This function is called via elf_link_hash_traverse if we are
1429   creating a shared object.  In the -Bsymbolic case it discards the
1430   space allocated to copy PC relative relocs against symbols which
1431   are defined in regular objects.  For the normal shared case, it
1432   discards space for pc-relative relocs that have become local due to
1433   symbol visibility changes.  We allocated space for them in the
1434   check_relocs routine, but we won't fill them in in the
1435   relocate_section routine.
1436
1437   We also check whether any of the remaining relocations apply
1438   against a readonly section, and set the DF_TEXTREL flag in this
1439   case.  */
1440
1441static bfd_boolean
1442elf_m68k_discard_copies (h, inf)
1443     struct elf_link_hash_entry *h;
1444     PTR inf;
1445{
1446  struct bfd_link_info *info = (struct bfd_link_info *) inf;
1447  struct elf_m68k_pcrel_relocs_copied *s;
1448
1449  if (h->root.type == bfd_link_hash_warning)
1450    h = (struct elf_link_hash_entry *) h->root.u.i.link;
1451
1452  if (!h->def_regular
1453      || (!info->symbolic
1454	  && !h->forced_local))
1455    {
1456      if ((info->flags & DF_TEXTREL) == 0)
1457	{
1458	  /* Look for relocations against read-only sections.  */
1459	  for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1460	       s != NULL;
1461	       s = s->next)
1462	    if ((s->section->flags & SEC_READONLY) != 0)
1463	      {
1464		info->flags |= DF_TEXTREL;
1465		break;
1466	      }
1467	}
1468
1469      return TRUE;
1470    }
1471
1472  for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1473       s != NULL;
1474       s = s->next)
1475    s->section->size -= s->count * sizeof (Elf32_External_Rela);
1476
1477  return TRUE;
1478}
1479
1480/* Relocate an M68K ELF section.  */
1481
1482static bfd_boolean
1483elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1484			   contents, relocs, local_syms, local_sections)
1485     bfd *output_bfd;
1486     struct bfd_link_info *info;
1487     bfd *input_bfd;
1488     asection *input_section;
1489     bfd_byte *contents;
1490     Elf_Internal_Rela *relocs;
1491     Elf_Internal_Sym *local_syms;
1492     asection **local_sections;
1493{
1494  bfd *dynobj;
1495  Elf_Internal_Shdr *symtab_hdr;
1496  struct elf_link_hash_entry **sym_hashes;
1497  bfd_vma *local_got_offsets;
1498  asection *sgot;
1499  asection *splt;
1500  asection *sreloc;
1501  Elf_Internal_Rela *rel;
1502  Elf_Internal_Rela *relend;
1503
1504  if (info->relocatable)
1505    return TRUE;
1506
1507  dynobj = elf_hash_table (info)->dynobj;
1508  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1509  sym_hashes = elf_sym_hashes (input_bfd);
1510  local_got_offsets = elf_local_got_offsets (input_bfd);
1511
1512  sgot = NULL;
1513  splt = NULL;
1514  sreloc = NULL;
1515
1516  rel = relocs;
1517  relend = relocs + input_section->reloc_count;
1518  for (; rel < relend; rel++)
1519    {
1520      int r_type;
1521      reloc_howto_type *howto;
1522      unsigned long r_symndx;
1523      struct elf_link_hash_entry *h;
1524      Elf_Internal_Sym *sym;
1525      asection *sec;
1526      bfd_vma relocation;
1527      bfd_boolean unresolved_reloc;
1528      bfd_reloc_status_type r;
1529
1530      r_type = ELF32_R_TYPE (rel->r_info);
1531      if (r_type < 0 || r_type >= (int) R_68K_max)
1532	{
1533	  bfd_set_error (bfd_error_bad_value);
1534	  return FALSE;
1535	}
1536      howto = howto_table + r_type;
1537
1538      r_symndx = ELF32_R_SYM (rel->r_info);
1539
1540      h = NULL;
1541      sym = NULL;
1542      sec = NULL;
1543      unresolved_reloc = FALSE;
1544
1545      if (r_symndx < symtab_hdr->sh_info)
1546	{
1547	  sym = local_syms + r_symndx;
1548	  sec = local_sections[r_symndx];
1549	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1550	}
1551      else
1552	{
1553	  bfd_boolean warned;
1554
1555	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1556				   r_symndx, symtab_hdr, sym_hashes,
1557				   h, sec, relocation,
1558				   unresolved_reloc, warned);
1559	}
1560
1561      switch (r_type)
1562	{
1563	case R_68K_GOT8:
1564	case R_68K_GOT16:
1565	case R_68K_GOT32:
1566	  /* Relocation is to the address of the entry for this symbol
1567	     in the global offset table.  */
1568	  if (h != NULL
1569	      && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1570	    break;
1571	  /* Fall through.  */
1572	case R_68K_GOT8O:
1573	case R_68K_GOT16O:
1574	case R_68K_GOT32O:
1575	  /* Relocation is the offset of the entry for this symbol in
1576	     the global offset table.  */
1577
1578	  {
1579	    bfd_vma off;
1580
1581	    if (sgot == NULL)
1582	      {
1583		sgot = bfd_get_section_by_name (dynobj, ".got");
1584		BFD_ASSERT (sgot != NULL);
1585	      }
1586
1587	    if (h != NULL)
1588	      {
1589		bfd_boolean dyn;
1590
1591		off = h->got.offset;
1592		BFD_ASSERT (off != (bfd_vma) -1);
1593
1594		dyn = elf_hash_table (info)->dynamic_sections_created;
1595		if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1596		    || (info->shared
1597			&& (info->symbolic
1598			    || h->dynindx == -1
1599			    || h->forced_local)
1600			&& h->def_regular))
1601		  {
1602		    /* This is actually a static link, or it is a
1603		       -Bsymbolic link and the symbol is defined
1604		       locally, or the symbol was forced to be local
1605		       because of a version file..  We must initialize
1606		       this entry in the global offset table.  Since
1607		       the offset must always be a multiple of 4, we
1608		       use the least significant bit to record whether
1609		       we have initialized it already.
1610
1611		       When doing a dynamic link, we create a .rela.got
1612		       relocation entry to initialize the value.  This
1613		       is done in the finish_dynamic_symbol routine.  */
1614		    if ((off & 1) != 0)
1615		      off &= ~1;
1616		    else
1617		      {
1618			bfd_put_32 (output_bfd, relocation,
1619				    sgot->contents + off);
1620			h->got.offset |= 1;
1621		      }
1622		  }
1623		else
1624		  unresolved_reloc = FALSE;
1625	      }
1626	    else
1627	      {
1628		BFD_ASSERT (local_got_offsets != NULL
1629			    && local_got_offsets[r_symndx] != (bfd_vma) -1);
1630
1631		off = local_got_offsets[r_symndx];
1632
1633		/* The offset must always be a multiple of 4.  We use
1634		   the least significant bit to record whether we have
1635		   already generated the necessary reloc.  */
1636		if ((off & 1) != 0)
1637		  off &= ~1;
1638		else
1639		  {
1640		    bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1641
1642		    if (info->shared)
1643		      {
1644			asection *s;
1645			Elf_Internal_Rela outrel;
1646			bfd_byte *loc;
1647
1648			s = bfd_get_section_by_name (dynobj, ".rela.got");
1649			BFD_ASSERT (s != NULL);
1650
1651			outrel.r_offset = (sgot->output_section->vma
1652					   + sgot->output_offset
1653					   + off);
1654			outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1655			outrel.r_addend = relocation;
1656			loc = s->contents;
1657			loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1658			bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1659		      }
1660
1661		    local_got_offsets[r_symndx] |= 1;
1662		  }
1663	      }
1664
1665	    relocation = sgot->output_offset + off;
1666	    if (r_type == R_68K_GOT8O
1667		|| r_type == R_68K_GOT16O
1668		|| r_type == R_68K_GOT32O)
1669	      {
1670		/* This relocation does not use the addend.  */
1671		rel->r_addend = 0;
1672	      }
1673	    else
1674	      relocation += sgot->output_section->vma;
1675	  }
1676	  break;
1677
1678	case R_68K_PLT8:
1679	case R_68K_PLT16:
1680	case R_68K_PLT32:
1681	  /* Relocation is to the entry for this symbol in the
1682	     procedure linkage table.  */
1683
1684	  /* Resolve a PLTxx reloc against a local symbol directly,
1685	     without using the procedure linkage table.  */
1686	  if (h == NULL)
1687	    break;
1688
1689	  if (h->plt.offset == (bfd_vma) -1
1690	      || !elf_hash_table (info)->dynamic_sections_created)
1691	    {
1692	      /* We didn't make a PLT entry for this symbol.  This
1693		 happens when statically linking PIC code, or when
1694		 using -Bsymbolic.  */
1695	      break;
1696	    }
1697
1698	  if (splt == NULL)
1699	    {
1700	      splt = bfd_get_section_by_name (dynobj, ".plt");
1701	      BFD_ASSERT (splt != NULL);
1702	    }
1703
1704	  relocation = (splt->output_section->vma
1705			+ splt->output_offset
1706			+ h->plt.offset);
1707	  unresolved_reloc = FALSE;
1708	  break;
1709
1710	case R_68K_PLT8O:
1711	case R_68K_PLT16O:
1712	case R_68K_PLT32O:
1713	  /* Relocation is the offset of the entry for this symbol in
1714	     the procedure linkage table.  */
1715	  BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1716
1717	  if (splt == NULL)
1718	    {
1719	      splt = bfd_get_section_by_name (dynobj, ".plt");
1720	      BFD_ASSERT (splt != NULL);
1721	    }
1722
1723	  relocation = h->plt.offset;
1724	  unresolved_reloc = FALSE;
1725
1726	  /* This relocation does not use the addend.  */
1727	  rel->r_addend = 0;
1728
1729	  break;
1730
1731	case R_68K_PC8:
1732	case R_68K_PC16:
1733	case R_68K_PC32:
1734	  if (h == NULL
1735	      || (info->shared
1736		  && h->forced_local))
1737	    break;
1738	  /* Fall through.  */
1739	case R_68K_8:
1740	case R_68K_16:
1741	case R_68K_32:
1742	  if (info->shared
1743	      && r_symndx != 0
1744	      && (input_section->flags & SEC_ALLOC) != 0
1745	      && (h == NULL
1746		  || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1747		  || h->root.type != bfd_link_hash_undefweak)
1748	      && ((r_type != R_68K_PC8
1749		   && r_type != R_68K_PC16
1750		   && r_type != R_68K_PC32)
1751		  || (h != NULL
1752		      && h->dynindx != -1
1753		      && (!info->symbolic
1754			  || !h->def_regular))))
1755	    {
1756	      Elf_Internal_Rela outrel;
1757	      bfd_byte *loc;
1758	      bfd_boolean skip, relocate;
1759
1760	      /* When generating a shared object, these relocations
1761		 are copied into the output file to be resolved at run
1762		 time.  */
1763
1764	      skip = FALSE;
1765	      relocate = FALSE;
1766
1767	      outrel.r_offset =
1768		_bfd_elf_section_offset (output_bfd, info, input_section,
1769					 rel->r_offset);
1770	      if (outrel.r_offset == (bfd_vma) -1)
1771		skip = TRUE;
1772	      else if (outrel.r_offset == (bfd_vma) -2)
1773		skip = TRUE, relocate = TRUE;
1774	      outrel.r_offset += (input_section->output_section->vma
1775				  + input_section->output_offset);
1776
1777	      if (skip)
1778		memset (&outrel, 0, sizeof outrel);
1779	      else if (h != NULL
1780		       && h->dynindx != -1
1781		       && (r_type == R_68K_PC8
1782			   || r_type == R_68K_PC16
1783			   || r_type == R_68K_PC32
1784			   || !info->shared
1785			   || !info->symbolic
1786			   || !h->def_regular))
1787		{
1788		  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1789		  outrel.r_addend = rel->r_addend;
1790		}
1791	      else
1792		{
1793		  /* This symbol is local, or marked to become local.  */
1794		  if (r_type == R_68K_32)
1795		    {
1796		      relocate = TRUE;
1797		      outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1798		      outrel.r_addend = relocation + rel->r_addend;
1799		    }
1800		  else
1801		    {
1802		      long indx;
1803
1804		      if (bfd_is_abs_section (sec))
1805			indx = 0;
1806		      else if (sec == NULL || sec->owner == NULL)
1807			{
1808			  bfd_set_error (bfd_error_bad_value);
1809			  return FALSE;
1810			}
1811		      else
1812			{
1813			  asection *osec;
1814
1815			  osec = sec->output_section;
1816			  indx = elf_section_data (osec)->dynindx;
1817			  BFD_ASSERT (indx > 0);
1818			}
1819
1820		      outrel.r_info = ELF32_R_INFO (indx, r_type);
1821		      outrel.r_addend = relocation + rel->r_addend;
1822		    }
1823		}
1824
1825	      sreloc = elf_section_data (input_section)->sreloc;
1826	      if (sreloc == NULL)
1827		abort ();
1828
1829	      loc = sreloc->contents;
1830	      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1831	      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1832
1833	      /* This reloc will be computed at runtime, so there's no
1834                 need to do anything now, except for R_68K_32
1835                 relocations that have been turned into
1836                 R_68K_RELATIVE.  */
1837	      if (!relocate)
1838		continue;
1839	    }
1840
1841	  break;
1842
1843	case R_68K_GNU_VTINHERIT:
1844	case R_68K_GNU_VTENTRY:
1845	  /* These are no-ops in the end.  */
1846	  continue;
1847
1848	default:
1849	  break;
1850	}
1851
1852      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1853	 because such sections are not SEC_ALLOC and thus ld.so will
1854	 not process them.  */
1855      if (unresolved_reloc
1856	  && !((input_section->flags & SEC_DEBUGGING) != 0
1857	       && h->def_dynamic))
1858	{
1859	  (*_bfd_error_handler)
1860	    (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1861	     input_bfd,
1862	     input_section,
1863	     (long) rel->r_offset,
1864	     howto->name,
1865	     h->root.root.string);
1866	  return FALSE;
1867	}
1868
1869      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1870				    contents, rel->r_offset,
1871				    relocation, rel->r_addend);
1872
1873      if (r != bfd_reloc_ok)
1874	{
1875	  const char *name;
1876
1877	  if (h != NULL)
1878	    name = h->root.root.string;
1879	  else
1880	    {
1881	      name = bfd_elf_string_from_elf_section (input_bfd,
1882						      symtab_hdr->sh_link,
1883						      sym->st_name);
1884	      if (name == NULL)
1885		return FALSE;
1886	      if (*name == '\0')
1887		name = bfd_section_name (input_bfd, sec);
1888	    }
1889
1890	  if (r == bfd_reloc_overflow)
1891	    {
1892	      if (!(info->callbacks->reloc_overflow
1893		    (info, (h ? &h->root : NULL), name, howto->name,
1894		     (bfd_vma) 0, input_bfd, input_section,
1895		     rel->r_offset)))
1896		return FALSE;
1897	    }
1898	  else
1899	    {
1900	      (*_bfd_error_handler)
1901		(_("%B(%A+0x%lx): reloc against `%s': error %d"),
1902		 input_bfd, input_section,
1903		 (long) rel->r_offset, name, (int) r);
1904	      return FALSE;
1905	    }
1906	}
1907    }
1908
1909  return TRUE;
1910}
1911
1912/* Finish up dynamic symbol handling.  We set the contents of various
1913   dynamic sections here.  */
1914
1915static bfd_boolean
1916elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
1917     bfd *output_bfd;
1918     struct bfd_link_info *info;
1919     struct elf_link_hash_entry *h;
1920     Elf_Internal_Sym *sym;
1921{
1922  bfd *dynobj;
1923  int plt_off1, plt_off2, plt_off3;
1924
1925  dynobj = elf_hash_table (info)->dynobj;
1926
1927  if (h->plt.offset != (bfd_vma) -1)
1928    {
1929      asection *splt;
1930      asection *sgot;
1931      asection *srela;
1932      bfd_vma plt_index;
1933      bfd_vma got_offset;
1934      Elf_Internal_Rela rela;
1935      bfd_byte *loc;
1936
1937      /* This symbol has an entry in the procedure linkage table.  Set
1938	 it up.  */
1939
1940      BFD_ASSERT (h->dynindx != -1);
1941
1942      splt = bfd_get_section_by_name (dynobj, ".plt");
1943      sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1944      srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1945      BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1946
1947      /* Get the index in the procedure linkage table which
1948	 corresponds to this symbol.  This is the index of this symbol
1949	 in all the symbols for which we are making plt entries.  The
1950	 first entry in the procedure linkage table is reserved.  */
1951      if (CPU32_FLAG (output_bfd))
1952        plt_index = (h->plt.offset / PLT_CPU32_ENTRY_SIZE) - 1;
1953      else if (CFV4E_FLAG (output_bfd))
1954	plt_index = (h->plt.offset / CFV4E_PLT_ENTRY_SIZE) - 1;
1955      else
1956        plt_index = (h->plt.offset / PLT_ENTRY_SIZE) - 1;
1957
1958      /* Get the offset into the .got table of the entry that
1959	 corresponds to this function.  Each .got entry is 4 bytes.
1960	 The first three are reserved.  */
1961      got_offset = (plt_index + 3) * 4;
1962
1963      if (CPU32_FLAG (output_bfd))
1964        {
1965          /* Fill in the entry in the procedure linkage table.  */
1966          memcpy (splt->contents + h->plt.offset, elf_cpu32_plt_entry,
1967	          PLT_CPU32_ENTRY_SIZE);
1968          plt_off1 = 4;
1969          plt_off2 = 12;
1970          plt_off3 = 18;
1971        }
1972      else if (CFV4E_FLAG (output_bfd))
1973        {
1974          memcpy (splt->contents + h->plt.offset, elf_cfv4e_plt_entry,
1975	          CFV4E_PLT_ENTRY_SIZE);
1976          plt_off1 = 2;
1977          plt_off2 = 14;
1978          plt_off3 = 20;
1979	}
1980      else
1981        {
1982          /* Fill in the entry in the procedure linkage table.  */
1983          memcpy (splt->contents + h->plt.offset, elf_m68k_plt_entry,
1984	          PLT_ENTRY_SIZE);
1985          plt_off1 = 4;
1986          plt_off2 = 10;
1987          plt_off3 = 16;
1988        }
1989
1990      /* The offset is relative to the first extension word.  */
1991      bfd_put_32 (output_bfd,
1992		  sgot->output_section->vma
1993		  + sgot->output_offset
1994		  + got_offset
1995		  - (splt->output_section->vma
1996		     + h->plt.offset
1997		     + (CFV4E_FLAG (output_bfd) ? 8 : 2)),
1998		  splt->contents + h->plt.offset + plt_off1);
1999
2000      bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
2001		  splt->contents + h->plt.offset + plt_off2);
2002      bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3),
2003		  splt->contents + h->plt.offset + plt_off3);
2004
2005      /* Fill in the entry in the global offset table.  */
2006      bfd_put_32 (output_bfd,
2007		  (splt->output_section->vma
2008		   + splt->output_offset
2009		   + h->plt.offset
2010		   + (CFV4E_FLAG (output_bfd) ? 12 : 8)),
2011		  sgot->contents + got_offset);
2012
2013      /* Fill in the entry in the .rela.plt section.  */
2014      rela.r_offset = (sgot->output_section->vma
2015		       + sgot->output_offset
2016		       + got_offset);
2017      rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
2018      rela.r_addend = 0;
2019      loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
2020      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2021
2022      if (!h->def_regular)
2023	{
2024	  /* Mark the symbol as undefined, rather than as defined in
2025	     the .plt section.  Leave the value alone.  */
2026	  sym->st_shndx = SHN_UNDEF;
2027	}
2028    }
2029
2030  if (h->got.offset != (bfd_vma) -1)
2031    {
2032      asection *sgot;
2033      asection *srela;
2034      Elf_Internal_Rela rela;
2035      bfd_byte *loc;
2036
2037      /* This symbol has an entry in the global offset table.  Set it
2038	 up.  */
2039
2040      sgot = bfd_get_section_by_name (dynobj, ".got");
2041      srela = bfd_get_section_by_name (dynobj, ".rela.got");
2042      BFD_ASSERT (sgot != NULL && srela != NULL);
2043
2044      rela.r_offset = (sgot->output_section->vma
2045		       + sgot->output_offset
2046		       + (h->got.offset &~ (bfd_vma) 1));
2047
2048      /* If this is a -Bsymbolic link, and the symbol is defined
2049	 locally, we just want to emit a RELATIVE reloc.  Likewise if
2050	 the symbol was forced to be local because of a version file.
2051	 The entry in the global offset table will already have been
2052	 initialized in the relocate_section function.  */
2053      if (info->shared
2054	  && (info->symbolic
2055	      || h->dynindx == -1
2056	      || h->forced_local)
2057	  && h->def_regular)
2058	{
2059	  rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
2060	  rela.r_addend = bfd_get_signed_32 (output_bfd,
2061					     (sgot->contents
2062					      + (h->got.offset &~ (bfd_vma) 1)));
2063	}
2064      else
2065	{
2066	  bfd_put_32 (output_bfd, (bfd_vma) 0,
2067		      sgot->contents + (h->got.offset &~ (bfd_vma) 1));
2068	  rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
2069	  rela.r_addend = 0;
2070	}
2071
2072      loc = srela->contents;
2073      loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2074      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2075    }
2076
2077  if (h->needs_copy)
2078    {
2079      asection *s;
2080      Elf_Internal_Rela rela;
2081      bfd_byte *loc;
2082
2083      /* This symbol needs a copy reloc.  Set it up.  */
2084
2085      BFD_ASSERT (h->dynindx != -1
2086		  && (h->root.type == bfd_link_hash_defined
2087		      || h->root.type == bfd_link_hash_defweak));
2088
2089      s = bfd_get_section_by_name (h->root.u.def.section->owner,
2090				   ".rela.bss");
2091      BFD_ASSERT (s != NULL);
2092
2093      rela.r_offset = (h->root.u.def.value
2094		       + h->root.u.def.section->output_section->vma
2095		       + h->root.u.def.section->output_offset);
2096      rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
2097      rela.r_addend = 0;
2098      loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2099      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2100    }
2101
2102  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
2103  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2104      || h == elf_hash_table (info)->hgot)
2105    sym->st_shndx = SHN_ABS;
2106
2107  return TRUE;
2108}
2109
2110/* Finish up the dynamic sections.  */
2111
2112static bfd_boolean
2113elf_m68k_finish_dynamic_sections (output_bfd, info)
2114     bfd *output_bfd;
2115     struct bfd_link_info *info;
2116{
2117  bfd *dynobj;
2118  asection *sgot;
2119  asection *sdyn;
2120
2121  dynobj = elf_hash_table (info)->dynobj;
2122
2123  sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2124  BFD_ASSERT (sgot != NULL);
2125  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2126
2127  if (elf_hash_table (info)->dynamic_sections_created)
2128    {
2129      asection *splt;
2130      Elf32_External_Dyn *dyncon, *dynconend;
2131
2132      splt = bfd_get_section_by_name (dynobj, ".plt");
2133      BFD_ASSERT (splt != NULL && sdyn != NULL);
2134
2135      dyncon = (Elf32_External_Dyn *) sdyn->contents;
2136      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2137      for (; dyncon < dynconend; dyncon++)
2138	{
2139	  Elf_Internal_Dyn dyn;
2140	  const char *name;
2141	  asection *s;
2142
2143	  bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2144
2145	  switch (dyn.d_tag)
2146	    {
2147	    default:
2148	      break;
2149
2150	    case DT_PLTGOT:
2151	      name = ".got";
2152	      goto get_vma;
2153	    case DT_JMPREL:
2154	      name = ".rela.plt";
2155	    get_vma:
2156	      s = bfd_get_section_by_name (output_bfd, name);
2157	      BFD_ASSERT (s != NULL);
2158	      dyn.d_un.d_ptr = s->vma;
2159	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2160	      break;
2161
2162	    case DT_PLTRELSZ:
2163	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2164	      BFD_ASSERT (s != NULL);
2165	      dyn.d_un.d_val = s->size;
2166	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2167	      break;
2168
2169	    case DT_RELASZ:
2170	      /* The procedure linkage table relocs (DT_JMPREL) should
2171		 not be included in the overall relocs (DT_RELA).
2172		 Therefore, we override the DT_RELASZ entry here to
2173		 make it not include the JMPREL relocs.  Since the
2174		 linker script arranges for .rela.plt to follow all
2175		 other relocation sections, we don't have to worry
2176		 about changing the DT_RELA entry.  */
2177	      s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2178	      if (s != NULL)
2179		dyn.d_un.d_val -= s->size;
2180	      bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2181	      break;
2182	    }
2183	}
2184
2185      /* Fill in the first entry in the procedure linkage table.  */
2186      if (splt->size > 0)
2187	{
2188	  if (CFV4E_FLAG (output_bfd))
2189           {
2190	     memcpy (splt->contents, elf_cfv4e_plt0_entry, CFV4E_PLT_ENTRY_SIZE);
2191             bfd_put_32 (output_bfd,
2192                         (sgot->output_section->vma
2193                          + sgot->output_offset + 4
2194                          - (splt->output_section->vma + 2)),
2195                         splt->contents + 2);
2196             bfd_put_32 (output_bfd,
2197                         (sgot->output_section->vma
2198                          + sgot->output_offset + 8
2199                          - (splt->output_section->vma + 10) - 8),
2200                         splt->contents + 12);
2201             elf_section_data (splt->output_section)->this_hdr.sh_entsize
2202	       = CFV4E_PLT_ENTRY_SIZE;
2203           }
2204	  else if (CPU32_FLAG (output_bfd))
2205            {
2206              memcpy (splt->contents, elf_cpu32_plt0_entry, PLT_CPU32_ENTRY_SIZE);
2207	      bfd_put_32 (output_bfd,
2208		          (sgot->output_section->vma
2209		           + sgot->output_offset + 4
2210		           - (splt->output_section->vma + 2)),
2211		          splt->contents + 4);
2212	      bfd_put_32 (output_bfd,
2213		          (sgot->output_section->vma
2214		           + sgot->output_offset + 8
2215		           - (splt->output_section->vma + 10)),
2216		          splt->contents + 12);
2217              elf_section_data (splt->output_section)->this_hdr.sh_entsize
2218               = PLT_CPU32_ENTRY_SIZE;
2219            }
2220          else
2221            {
2222	      memcpy (splt->contents, elf_m68k_plt0_entry, PLT_ENTRY_SIZE);
2223	      bfd_put_32 (output_bfd,
2224		          (sgot->output_section->vma
2225		           + sgot->output_offset + 4
2226		           - (splt->output_section->vma + 2)),
2227		          splt->contents + 4);
2228	      bfd_put_32 (output_bfd,
2229		          (sgot->output_section->vma
2230		           + sgot->output_offset + 8
2231		           - (splt->output_section->vma + 10)),
2232		          splt->contents + 12);
2233              elf_section_data (splt->output_section)->this_hdr.sh_entsize
2234               = PLT_ENTRY_SIZE;
2235            }
2236	}
2237    }
2238
2239  /* Fill in the first three entries in the global offset table.  */
2240  if (sgot->size > 0)
2241    {
2242      if (sdyn == NULL)
2243	bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2244      else
2245	bfd_put_32 (output_bfd,
2246		    sdyn->output_section->vma + sdyn->output_offset,
2247		    sgot->contents);
2248      bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2249      bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2250    }
2251
2252  elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2253
2254  return TRUE;
2255}
2256
2257/* Given a .data section and a .emreloc in-memory section, store
2258   relocation information into the .emreloc section which can be
2259   used at runtime to relocate the section.  This is called by the
2260   linker when the --embedded-relocs switch is used.  This is called
2261   after the add_symbols entry point has been called for all the
2262   objects, and before the final_link entry point is called.  */
2263
2264bfd_boolean
2265bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2266     bfd *abfd;
2267     struct bfd_link_info *info;
2268     asection *datasec;
2269     asection *relsec;
2270     char **errmsg;
2271{
2272  Elf_Internal_Shdr *symtab_hdr;
2273  Elf_Internal_Sym *isymbuf = NULL;
2274  Elf_Internal_Rela *internal_relocs = NULL;
2275  Elf_Internal_Rela *irel, *irelend;
2276  bfd_byte *p;
2277  bfd_size_type amt;
2278
2279  BFD_ASSERT (! info->relocatable);
2280
2281  *errmsg = NULL;
2282
2283  if (datasec->reloc_count == 0)
2284    return TRUE;
2285
2286  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2287
2288  /* Get a copy of the native relocations.  */
2289  internal_relocs = (_bfd_elf_link_read_relocs
2290		     (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2291		      info->keep_memory));
2292  if (internal_relocs == NULL)
2293    goto error_return;
2294
2295  amt = (bfd_size_type) datasec->reloc_count * 12;
2296  relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
2297  if (relsec->contents == NULL)
2298    goto error_return;
2299
2300  p = relsec->contents;
2301
2302  irelend = internal_relocs + datasec->reloc_count;
2303  for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2304    {
2305      asection *targetsec;
2306
2307      /* We are going to write a four byte longword into the runtime
2308       reloc section.  The longword will be the address in the data
2309       section which must be relocated.  It is followed by the name
2310       of the target section NUL-padded or truncated to 8
2311       characters.  */
2312
2313      /* We can only relocate absolute longword relocs at run time.  */
2314      if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2315	{
2316	  *errmsg = _("unsupported reloc type");
2317	  bfd_set_error (bfd_error_bad_value);
2318	  goto error_return;
2319	}
2320
2321      /* Get the target section referred to by the reloc.  */
2322      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2323	{
2324	  /* A local symbol.  */
2325	  Elf_Internal_Sym *isym;
2326
2327	  /* Read this BFD's local symbols if we haven't done so already.  */
2328	  if (isymbuf == NULL)
2329	    {
2330	      isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2331	      if (isymbuf == NULL)
2332		isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2333						symtab_hdr->sh_info, 0,
2334						NULL, NULL, NULL);
2335	      if (isymbuf == NULL)
2336		goto error_return;
2337	    }
2338
2339	  isym = isymbuf + ELF32_R_SYM (irel->r_info);
2340	  targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2341	}
2342      else
2343	{
2344	  unsigned long indx;
2345	  struct elf_link_hash_entry *h;
2346
2347	  /* An external symbol.  */
2348	  indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2349	  h = elf_sym_hashes (abfd)[indx];
2350	  BFD_ASSERT (h != NULL);
2351	  if (h->root.type == bfd_link_hash_defined
2352	      || h->root.type == bfd_link_hash_defweak)
2353	    targetsec = h->root.u.def.section;
2354	  else
2355	    targetsec = NULL;
2356	}
2357
2358      bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2359      memset (p + 4, 0, 8);
2360      if (targetsec != NULL)
2361	strncpy ((char *) p + 4, targetsec->output_section->name, 8);
2362    }
2363
2364  if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2365    free (isymbuf);
2366  if (internal_relocs != NULL
2367      && elf_section_data (datasec)->relocs != internal_relocs)
2368    free (internal_relocs);
2369  return TRUE;
2370
2371error_return:
2372  if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2373    free (isymbuf);
2374  if (internal_relocs != NULL
2375      && elf_section_data (datasec)->relocs != internal_relocs)
2376    free (internal_relocs);
2377  return FALSE;
2378}
2379
2380static enum elf_reloc_type_class
2381elf32_m68k_reloc_type_class (rela)
2382     const Elf_Internal_Rela *rela;
2383{
2384  switch ((int) ELF32_R_TYPE (rela->r_info))
2385    {
2386    case R_68K_RELATIVE:
2387      return reloc_class_relative;
2388    case R_68K_JMP_SLOT:
2389      return reloc_class_plt;
2390    case R_68K_COPY:
2391      return reloc_class_copy;
2392    default:
2393      return reloc_class_normal;
2394    }
2395}
2396
2397/* Return address for Ith PLT stub in section PLT, for relocation REL
2398   or (bfd_vma) -1 if it should not be included.  */
2399
2400static bfd_vma
2401elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
2402		      const arelent *rel ATTRIBUTE_UNUSED)
2403{
2404  if (CPU32_FLAG (plt->owner))
2405    return plt->vma + (i + 1) * PLT_CPU32_ENTRY_SIZE;
2406  return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
2407}
2408
2409#define TARGET_BIG_SYM			bfd_elf32_m68k_vec
2410#define TARGET_BIG_NAME			"elf32-m68k"
2411#define ELF_MACHINE_CODE		EM_68K
2412#define ELF_MAXPAGESIZE			0x2000
2413#define elf_backend_create_dynamic_sections \
2414					_bfd_elf_create_dynamic_sections
2415#define bfd_elf32_bfd_link_hash_table_create \
2416					elf_m68k_link_hash_table_create
2417#define bfd_elf32_bfd_final_link	bfd_elf_gc_common_final_link
2418
2419#define elf_backend_check_relocs	elf_m68k_check_relocs
2420#define elf_backend_adjust_dynamic_symbol \
2421					elf_m68k_adjust_dynamic_symbol
2422#define elf_backend_size_dynamic_sections \
2423					elf_m68k_size_dynamic_sections
2424#define elf_backend_relocate_section	elf_m68k_relocate_section
2425#define elf_backend_finish_dynamic_symbol \
2426					elf_m68k_finish_dynamic_symbol
2427#define elf_backend_finish_dynamic_sections \
2428					elf_m68k_finish_dynamic_sections
2429#define elf_backend_gc_mark_hook	elf_m68k_gc_mark_hook
2430#define elf_backend_gc_sweep_hook	elf_m68k_gc_sweep_hook
2431#define bfd_elf32_bfd_merge_private_bfd_data \
2432                                        elf32_m68k_merge_private_bfd_data
2433#define bfd_elf32_bfd_set_private_flags \
2434                                        elf32_m68k_set_private_flags
2435#define bfd_elf32_bfd_print_private_bfd_data \
2436                                        elf32_m68k_print_private_bfd_data
2437#define elf_backend_reloc_type_class	elf32_m68k_reloc_type_class
2438#define elf_backend_plt_sym_val		elf_m68k_plt_sym_val
2439#define elf_backend_object_p		elf32_m68k_object_p
2440
2441#define elf_backend_can_gc_sections 1
2442#define elf_backend_can_refcount 1
2443#define elf_backend_want_got_plt 1
2444#define elf_backend_plt_readonly 1
2445#define elf_backend_want_plt_sym 0
2446#define elf_backend_got_header_size	12
2447#define elf_backend_rela_normal		1
2448
2449#include "elf32-target.h"
2450