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