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