Support for more than 64k ELF sections.
[binutils.git] / bfd / elf32-m68k.c
blobd0675628431a806a8fa476ed17930b5c7832a949
1 /* Motorola 68k series support for 32-bit ELF
2 Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
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"
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 *, Elf32_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 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 ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
41 struct elf_link_hash_entry *, Elf_Internal_Sym *));
42 static boolean elf_m68k_gc_sweep_hook
43 PARAMS ((bfd *, struct bfd_link_info *, asection *,
44 const Elf_Internal_Rela *));
45 static boolean elf_m68k_adjust_dynamic_symbol
46 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
47 static boolean elf_m68k_size_dynamic_sections
48 PARAMS ((bfd *, struct bfd_link_info *));
49 static boolean elf_m68k_relocate_section
50 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
51 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
52 static boolean elf_m68k_finish_dynamic_symbol
53 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
54 Elf_Internal_Sym *));
55 static boolean elf_m68k_finish_dynamic_sections
56 PARAMS ((bfd *, struct bfd_link_info *));
58 static boolean elf32_m68k_set_private_flags
59 PARAMS ((bfd *, flagword));
60 static boolean elf32_m68k_copy_private_bfd_data
61 PARAMS ((bfd *, bfd *));
62 static boolean elf32_m68k_merge_private_bfd_data
63 PARAMS ((bfd *, bfd *));
64 static 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 *));
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),
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;
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)];
133 #define elf_info_to_howto rtype_to_howto
135 static const struct
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 },
168 static reloc_howto_type *
169 reloc_type_lookup (abfd, code)
170 bfd *abfd ATTRIBUTE_UNUSED;
171 bfd_reloc_code_real_type code;
173 unsigned int i;
174 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
176 if (reloc_map[i].bfd_val == code)
177 return &howto_table[reloc_map[i].elf_val];
179 return 0;
182 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
183 #define ELF_ARCH bfd_arch_m68k
184 /* end code generated by elf.el */
186 #define USE_RELA
188 /* Functions for the m68k ELF linker. */
190 /* The name of the dynamic interpreter. This is put in the .interp
191 section. */
193 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
195 /* The size in bytes of an entry in the procedure linkage table. */
197 #define PLT_ENTRY_SIZE 20
199 /* The first entry in a procedure linkage table looks like this. See
200 the SVR4 ABI m68k supplement to see how this works. */
202 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
204 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
205 0, 0, 0, 0, /* replaced with offset to .got + 4. */
206 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
207 0, 0, 0, 0, /* replaced with offset to .got + 8. */
208 0, 0, 0, 0 /* pad out to 20 bytes. */
211 /* Subsequent entries in a procedure linkage table look like this. */
213 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
215 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
216 0, 0, 0, 0, /* replaced with offset to symbol's .got entry. */
217 0x2f, 0x3c, /* move.l #offset,-(%sp) */
218 0, 0, 0, 0, /* replaced with offset into relocation table. */
219 0x60, 0xff, /* bra.l .plt */
220 0, 0, 0, 0 /* replaced with offset to start of .plt. */
223 #define CPU32_FLAG(abfd) (elf_elfheader (abfd)->e_flags & EF_CPU32)
225 #define PLT_CPU32_ENTRY_SIZE 24
226 /* Procedure linkage table entries for the cpu32 */
227 static const bfd_byte elf_cpu32_plt0_entry[PLT_CPU32_ENTRY_SIZE] =
229 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
230 0, 0, 0, 0, /* replaced with offset to .got + 4. */
231 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
232 0, 0, 0, 0, /* replace with offset to .got +8. */
233 0x4e, 0xd1, /* jmp %a1@ */
234 0, 0, 0, 0, /* pad out to 24 bytes. */
235 0, 0
238 static const bfd_byte elf_cpu32_plt_entry[PLT_CPU32_ENTRY_SIZE] =
240 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
241 0, 0, 0, 0, /* replaced with offset to symbol's .got entry. */
242 0x4e, 0xd1, /* jmp %a1@ */
243 0x2f, 0x3c, /* move.l #offset,-(%sp) */
244 0, 0, 0, 0, /* replaced with offset into relocation table. */
245 0x60, 0xff, /* bra.l .plt */
246 0, 0, 0, 0, /* replaced with offset to start of .plt. */
247 0, 0
250 /* The m68k linker needs to keep track of the number of relocs that it
251 decides to copy in check_relocs for each symbol. This is so that it
252 can discard PC relative relocs if it doesn't need them when linking
253 with -Bsymbolic. We store the information in a field extending the
254 regular ELF linker hash table. */
256 /* This structure keeps track of the number of PC relative relocs we have
257 copied for a given symbol. */
259 struct elf_m68k_pcrel_relocs_copied
261 /* Next section. */
262 struct elf_m68k_pcrel_relocs_copied *next;
263 /* A section in dynobj. */
264 asection *section;
265 /* Number of relocs copied in this section. */
266 bfd_size_type count;
269 /* m68k ELF linker hash entry. */
271 struct elf_m68k_link_hash_entry
273 struct elf_link_hash_entry root;
275 /* Number of PC relative relocs copied for this symbol. */
276 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
279 /* m68k ELF linker hash table. */
281 struct elf_m68k_link_hash_table
283 struct elf_link_hash_table root;
286 /* Declare this now that the above structures are defined. */
288 static boolean elf_m68k_discard_copies
289 PARAMS ((struct elf_m68k_link_hash_entry *, PTR));
291 /* Traverse an m68k ELF linker hash table. */
293 #define elf_m68k_link_hash_traverse(table, func, info) \
294 (elf_link_hash_traverse \
295 (&(table)->root, \
296 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
297 (info)))
299 /* Get the m68k ELF linker hash table from a link_info structure. */
301 #define elf_m68k_hash_table(p) \
302 ((struct elf_m68k_link_hash_table *) (p)->hash)
304 /* Create an entry in an m68k ELF linker hash table. */
306 static struct bfd_hash_entry *
307 elf_m68k_link_hash_newfunc (entry, table, string)
308 struct bfd_hash_entry *entry;
309 struct bfd_hash_table *table;
310 const char *string;
312 struct elf_m68k_link_hash_entry *ret =
313 (struct elf_m68k_link_hash_entry *) entry;
315 /* Allocate the structure if it has not already been allocated by a
316 subclass. */
317 if (ret == (struct elf_m68k_link_hash_entry *) NULL)
318 ret = ((struct elf_m68k_link_hash_entry *)
319 bfd_hash_allocate (table,
320 sizeof (struct elf_m68k_link_hash_entry)));
321 if (ret == (struct elf_m68k_link_hash_entry *) NULL)
322 return (struct bfd_hash_entry *) ret;
324 /* Call the allocation method of the superclass. */
325 ret = ((struct elf_m68k_link_hash_entry *)
326 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
327 table, string));
328 if (ret != (struct elf_m68k_link_hash_entry *) NULL)
330 ret->pcrel_relocs_copied = NULL;
333 return (struct bfd_hash_entry *) ret;
336 /* Create an m68k ELF linker hash table. */
338 static struct bfd_link_hash_table *
339 elf_m68k_link_hash_table_create (abfd)
340 bfd *abfd;
342 struct elf_m68k_link_hash_table *ret;
343 bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
345 ret = (struct elf_m68k_link_hash_table *) bfd_alloc (abfd, amt);
346 if (ret == (struct elf_m68k_link_hash_table *) NULL)
347 return NULL;
349 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
350 elf_m68k_link_hash_newfunc))
352 bfd_release (abfd, ret);
353 return NULL;
356 return &ret->root.root;
359 /* Keep m68k-specific flags in the ELF header */
360 static boolean
361 elf32_m68k_set_private_flags (abfd, flags)
362 bfd *abfd;
363 flagword flags;
365 elf_elfheader (abfd)->e_flags = flags;
366 elf_flags_init (abfd) = true;
367 return true;
370 /* Copy m68k-specific data from one module to another */
371 static boolean
372 elf32_m68k_copy_private_bfd_data (ibfd, obfd)
373 bfd *ibfd;
374 bfd *obfd;
376 flagword in_flags;
378 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
379 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
380 return true;
382 in_flags = elf_elfheader (ibfd)->e_flags;
384 elf_elfheader (obfd)->e_flags = in_flags;
385 elf_flags_init (obfd) = true;
387 return true;
390 /* Merge backend specific data from an object file to the output
391 object file when linking. */
392 static boolean
393 elf32_m68k_merge_private_bfd_data (ibfd, obfd)
394 bfd *ibfd;
395 bfd *obfd;
397 flagword out_flags;
398 flagword in_flags;
400 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
401 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
402 return true;
404 in_flags = elf_elfheader (ibfd)->e_flags;
405 out_flags = elf_elfheader (obfd)->e_flags;
407 if (!elf_flags_init (obfd))
409 elf_flags_init (obfd) = true;
410 elf_elfheader (obfd)->e_flags = in_flags;
413 return true;
416 /* Display the flags field */
417 static boolean
418 elf32_m68k_print_private_bfd_data (abfd, ptr)
419 bfd *abfd;
420 PTR ptr;
422 FILE *file = (FILE *) ptr;
424 BFD_ASSERT (abfd != NULL && ptr != NULL);
426 /* Print normal ELF private data. */
427 _bfd_elf_print_private_bfd_data (abfd, ptr);
429 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
431 /* xgettext:c-format */
432 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
434 if (elf_elfheader (abfd)->e_flags & EF_CPU32)
435 fprintf (file, _ (" [cpu32]"));
437 fputc ('\n', file);
439 return true;
441 /* Look through the relocs for a section during the first phase, and
442 allocate space in the global offset table or procedure linkage
443 table. */
445 static boolean
446 elf_m68k_check_relocs (abfd, info, sec, relocs)
447 bfd *abfd;
448 struct bfd_link_info *info;
449 asection *sec;
450 const Elf_Internal_Rela *relocs;
452 bfd *dynobj;
453 Elf_Internal_Shdr *symtab_hdr;
454 struct elf_link_hash_entry **sym_hashes;
455 bfd_signed_vma *local_got_refcounts;
456 const Elf_Internal_Rela *rel;
457 const Elf_Internal_Rela *rel_end;
458 asection *sgot;
459 asection *srelgot;
460 asection *sreloc;
462 if (info->relocateable)
463 return true;
465 dynobj = elf_hash_table (info)->dynobj;
466 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
467 sym_hashes = elf_sym_hashes (abfd);
468 local_got_refcounts = elf_local_got_refcounts (abfd);
470 sgot = NULL;
471 srelgot = NULL;
472 sreloc = NULL;
474 rel_end = relocs + sec->reloc_count;
475 for (rel = relocs; rel < rel_end; rel++)
477 unsigned long r_symndx;
478 struct elf_link_hash_entry *h;
480 r_symndx = ELF32_R_SYM (rel->r_info);
482 if (r_symndx < symtab_hdr->sh_info)
483 h = NULL;
484 else
485 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
487 switch (ELF32_R_TYPE (rel->r_info))
489 case R_68K_GOT8:
490 case R_68K_GOT16:
491 case R_68K_GOT32:
492 if (h != NULL
493 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
494 break;
495 /* Fall through. */
496 case R_68K_GOT8O:
497 case R_68K_GOT16O:
498 case R_68K_GOT32O:
499 /* This symbol requires a global offset table entry. */
501 if (dynobj == NULL)
503 /* Create the .got section. */
504 elf_hash_table (info)->dynobj = dynobj = abfd;
505 if (!_bfd_elf_create_got_section (dynobj, info))
506 return false;
509 if (sgot == NULL)
511 sgot = bfd_get_section_by_name (dynobj, ".got");
512 BFD_ASSERT (sgot != NULL);
515 if (srelgot == NULL
516 && (h != NULL || info->shared))
518 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
519 if (srelgot == NULL)
521 srelgot = bfd_make_section (dynobj, ".rela.got");
522 if (srelgot == NULL
523 || !bfd_set_section_flags (dynobj, srelgot,
524 (SEC_ALLOC
525 | SEC_LOAD
526 | SEC_HAS_CONTENTS
527 | SEC_IN_MEMORY
528 | SEC_LINKER_CREATED
529 | SEC_READONLY))
530 || !bfd_set_section_alignment (dynobj, srelgot, 2))
531 return false;
535 if (h != NULL)
537 if (h->got.refcount == 0)
539 /* Make sure this symbol is output as a dynamic symbol. */
540 if (h->dynindx == -1)
542 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
543 return false;
546 /* Allocate space in the .got section. */
547 sgot->_raw_size += 4;
548 /* Allocate relocation space. */
549 srelgot->_raw_size += sizeof (Elf32_External_Rela);
551 h->got.refcount++;
553 else
555 /* This is a global offset table entry for a local symbol. */
556 if (local_got_refcounts == NULL)
558 bfd_size_type size;
560 size = symtab_hdr->sh_info;
561 size *= sizeof (bfd_signed_vma);
562 local_got_refcounts = ((bfd_signed_vma *)
563 bfd_zalloc (abfd, size));
564 if (local_got_refcounts == NULL)
565 return false;
566 elf_local_got_refcounts (abfd) = local_got_refcounts;
568 if (local_got_refcounts[r_symndx] == 0)
570 sgot->_raw_size += 4;
571 if (info->shared)
573 /* If we are generating a shared object, we need to
574 output a R_68K_RELATIVE reloc so that the dynamic
575 linker can adjust this GOT entry. */
576 srelgot->_raw_size += sizeof (Elf32_External_Rela);
579 local_got_refcounts[r_symndx]++;
581 break;
583 case R_68K_PLT8:
584 case R_68K_PLT16:
585 case R_68K_PLT32:
586 /* This symbol requires a procedure linkage table entry. We
587 actually build the entry in adjust_dynamic_symbol,
588 because this might be a case of linking PIC code which is
589 never referenced by a dynamic object, in which case we
590 don't need to generate a procedure linkage table entry
591 after all. */
593 /* If this is a local symbol, we resolve it directly without
594 creating a procedure linkage table entry. */
595 if (h == NULL)
596 continue;
598 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
599 h->plt.refcount++;
600 break;
602 case R_68K_PLT8O:
603 case R_68K_PLT16O:
604 case R_68K_PLT32O:
605 /* This symbol requires a procedure linkage table entry. */
607 if (h == NULL)
609 /* It does not make sense to have this relocation for a
610 local symbol. FIXME: does it? How to handle it if
611 it does make sense? */
612 bfd_set_error (bfd_error_bad_value);
613 return false;
616 /* Make sure this symbol is output as a dynamic symbol. */
617 if (h->dynindx == -1)
619 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
620 return false;
623 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
624 h->plt.refcount++;
625 break;
627 case R_68K_PC8:
628 case R_68K_PC16:
629 case R_68K_PC32:
630 /* If we are creating a shared library and this is not a local
631 symbol, we need to copy the reloc into the shared library.
632 However when linking with -Bsymbolic and this is a global
633 symbol which is defined in an object we are including in the
634 link (i.e., DEF_REGULAR is set), then we can resolve the
635 reloc directly. At this point we have not seen all the input
636 files, so it is possible that DEF_REGULAR is not set now but
637 will be set later (it is never cleared). We account for that
638 possibility below by storing information in the
639 pcrel_relocs_copied field of the hash table entry. */
640 if (!(info->shared
641 && (sec->flags & SEC_ALLOC) != 0
642 && h != NULL
643 && (!info->symbolic
644 || (h->elf_link_hash_flags
645 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
647 if (h != NULL)
649 /* Make sure a plt entry is created for this symbol if
650 it turns out to be a function defined by a dynamic
651 object. */
652 h->plt.refcount++;
654 break;
656 /* Fall through. */
657 case R_68K_8:
658 case R_68K_16:
659 case R_68K_32:
660 if (h != NULL)
662 /* Make sure a plt entry is created for this symbol if it
663 turns out to be a function defined by a dynamic object. */
664 h->plt.refcount++;
667 /* If we are creating a shared library, we need to copy the
668 reloc into the shared library. */
669 if (info->shared
670 && (sec->flags & SEC_ALLOC) != 0)
672 /* When creating a shared object, we must copy these
673 reloc types into the output file. We create a reloc
674 section in dynobj and make room for this reloc. */
675 if (sreloc == NULL)
677 const char *name;
679 name = (bfd_elf_string_from_elf_section
680 (abfd,
681 elf_elfheader (abfd)->e_shstrndx,
682 elf_section_data (sec)->rel_hdr.sh_name));
683 if (name == NULL)
684 return false;
686 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
687 && strcmp (bfd_get_section_name (abfd, sec),
688 name + 5) == 0);
690 sreloc = bfd_get_section_by_name (dynobj, name);
691 if (sreloc == NULL)
693 sreloc = bfd_make_section (dynobj, name);
694 if (sreloc == NULL
695 || !bfd_set_section_flags (dynobj, sreloc,
696 (SEC_ALLOC
697 | SEC_LOAD
698 | SEC_HAS_CONTENTS
699 | SEC_IN_MEMORY
700 | SEC_LINKER_CREATED
701 | SEC_READONLY))
702 || !bfd_set_section_alignment (dynobj, sreloc, 2))
703 return false;
705 if (sec->flags & SEC_READONLY)
706 info->flags |= DF_TEXTREL;
709 sreloc->_raw_size += sizeof (Elf32_External_Rela);
711 /* If we are linking with -Bsymbolic, we count the number of
712 PC relative relocations we have entered for this symbol,
713 so that we can discard them again if the symbol is later
714 defined by a regular object. Note that this function is
715 only called if we are using an m68kelf linker hash table,
716 which means that h is really a pointer to an
717 elf_m68k_link_hash_entry. */
718 if ((ELF32_R_TYPE (rel->r_info) == R_68K_PC8
719 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
720 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
721 && info->symbolic)
723 struct elf_m68k_link_hash_entry *eh;
724 struct elf_m68k_pcrel_relocs_copied *p;
726 eh = (struct elf_m68k_link_hash_entry *) h;
728 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
729 if (p->section == sreloc)
730 break;
732 if (p == NULL)
734 p = ((struct elf_m68k_pcrel_relocs_copied *)
735 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
736 if (p == NULL)
737 return false;
738 p->next = eh->pcrel_relocs_copied;
739 eh->pcrel_relocs_copied = p;
740 p->section = sreloc;
741 p->count = 0;
744 ++p->count;
748 break;
750 /* This relocation describes the C++ object vtable hierarchy.
751 Reconstruct it for later use during GC. */
752 case R_68K_GNU_VTINHERIT:
753 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
754 return false;
755 break;
757 /* This relocation describes which C++ vtable entries are actually
758 used. Record for later use during GC. */
759 case R_68K_GNU_VTENTRY:
760 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
761 return false;
762 break;
764 default:
765 break;
769 return true;
772 /* Return the section that should be marked against GC for a given
773 relocation. */
775 static asection *
776 elf_m68k_gc_mark_hook (abfd, info, rel, h, sym)
777 bfd *abfd;
778 struct bfd_link_info *info ATTRIBUTE_UNUSED;
779 Elf_Internal_Rela *rel;
780 struct elf_link_hash_entry *h;
781 Elf_Internal_Sym *sym;
783 if (h != NULL)
785 switch (ELF32_R_TYPE (rel->r_info))
787 case R_68K_GNU_VTINHERIT:
788 case R_68K_GNU_VTENTRY:
789 break;
791 default:
792 switch (h->root.type)
794 default:
795 break;
797 case bfd_link_hash_defined:
798 case bfd_link_hash_defweak:
799 return h->root.u.def.section;
801 case bfd_link_hash_common:
802 return h->root.u.c.p->section;
806 else
808 return bfd_section_from_elf_index (abfd, sym->st_shndx);
811 return NULL;
814 /* Update the got entry reference counts for the section being removed. */
816 static boolean
817 elf_m68k_gc_sweep_hook (abfd, info, sec, relocs)
818 bfd *abfd;
819 struct bfd_link_info *info;
820 asection *sec;
821 const Elf_Internal_Rela *relocs;
823 Elf_Internal_Shdr *symtab_hdr;
824 struct elf_link_hash_entry **sym_hashes;
825 bfd_signed_vma *local_got_refcounts;
826 const Elf_Internal_Rela *rel, *relend;
827 unsigned long r_symndx;
828 struct elf_link_hash_entry *h;
829 bfd *dynobj;
830 asection *sgot;
831 asection *srelgot;
833 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
834 sym_hashes = elf_sym_hashes (abfd);
835 local_got_refcounts = elf_local_got_refcounts (abfd);
837 dynobj = elf_hash_table (info)->dynobj;
838 if (dynobj == NULL)
839 return true;
841 sgot = bfd_get_section_by_name (dynobj, ".got");
842 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
844 relend = relocs + sec->reloc_count;
845 for (rel = relocs; rel < relend; rel++)
847 switch (ELF32_R_TYPE (rel->r_info))
849 case R_68K_GOT8:
850 case R_68K_GOT16:
851 case R_68K_GOT32:
852 case R_68K_GOT8O:
853 case R_68K_GOT16O:
854 case R_68K_GOT32O:
855 r_symndx = ELF32_R_SYM (rel->r_info);
856 if (r_symndx >= symtab_hdr->sh_info)
858 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
859 if (h->got.refcount > 0)
861 --h->got.refcount;
862 if (h->got.refcount == 0)
864 /* We don't need the .got entry any more. */
865 sgot->_raw_size -= 4;
866 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
870 else if (local_got_refcounts != NULL)
872 if (local_got_refcounts[r_symndx] > 0)
874 --local_got_refcounts[r_symndx];
875 if (local_got_refcounts[r_symndx] == 0)
877 /* We don't need the .got entry any more. */
878 sgot->_raw_size -= 4;
879 if (info->shared)
880 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
884 break;
886 case R_68K_PLT8:
887 case R_68K_PLT16:
888 case R_68K_PLT32:
889 case R_68K_PLT8O:
890 case R_68K_PLT16O:
891 case R_68K_PLT32O:
892 case R_68K_PC8:
893 case R_68K_PC16:
894 case R_68K_PC32:
895 case R_68K_8:
896 case R_68K_16:
897 case R_68K_32:
898 r_symndx = ELF32_R_SYM (rel->r_info);
899 if (r_symndx >= symtab_hdr->sh_info)
901 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
902 if (h->plt.refcount > 0)
903 --h->plt.refcount;
905 break;
907 default:
908 break;
912 return true;
915 /* Adjust a symbol defined by a dynamic object and referenced by a
916 regular object. The current definition is in some section of the
917 dynamic object, but we're not including those sections. We have to
918 change the definition to something the rest of the link can
919 understand. */
921 static boolean
922 elf_m68k_adjust_dynamic_symbol (info, h)
923 struct bfd_link_info *info;
924 struct elf_link_hash_entry *h;
926 bfd *dynobj;
927 asection *s;
928 unsigned int power_of_two;
930 dynobj = elf_hash_table (info)->dynobj;
932 /* Make sure we know what is going on here. */
933 BFD_ASSERT (dynobj != NULL
934 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
935 || h->weakdef != NULL
936 || ((h->elf_link_hash_flags
937 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
938 && (h->elf_link_hash_flags
939 & ELF_LINK_HASH_REF_REGULAR) != 0
940 && (h->elf_link_hash_flags
941 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
943 /* If this is a function, put it in the procedure linkage table. We
944 will fill in the contents of the procedure linkage table later,
945 when we know the address of the .got section. */
946 if (h->type == STT_FUNC
947 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
949 if (! info->shared
950 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
951 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
952 /* We must always create the plt entry if it was referenced
953 by a PLTxxO relocation. In this case we already recorded
954 it as a dynamic symbol. */
955 && h->dynindx == -1)
957 /* This case can occur if we saw a PLTxx reloc in an input
958 file, but the symbol was never referred to by a dynamic
959 object. In such a case, we don't actually need to build
960 a procedure linkage table, and we can just do a PCxx
961 reloc instead. */
962 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
963 h->plt.offset = (bfd_vma) -1;
964 return true;
967 /* GC may have rendered this entry unused. */
968 if (h->plt.refcount <= 0)
970 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
971 h->plt.offset = (bfd_vma) -1;
972 return true;
975 /* Make sure this symbol is output as a dynamic symbol. */
976 if (h->dynindx == -1)
978 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
979 return false;
982 s = bfd_get_section_by_name (dynobj, ".plt");
983 BFD_ASSERT (s != NULL);
985 /* If this is the first .plt entry, make room for the special
986 first entry. */
987 if (s->_raw_size == 0)
989 if (CPU32_FLAG (dynobj))
990 s->_raw_size += PLT_CPU32_ENTRY_SIZE;
991 else
992 s->_raw_size += PLT_ENTRY_SIZE;
995 /* If this symbol is not defined in a regular file, and we are
996 not generating a shared library, then set the symbol to this
997 location in the .plt. This is required to make function
998 pointers compare as equal between the normal executable and
999 the shared library. */
1000 if (!info->shared
1001 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1003 h->root.u.def.section = s;
1004 h->root.u.def.value = s->_raw_size;
1007 h->plt.offset = s->_raw_size;
1009 /* Make room for this entry. */
1010 if (CPU32_FLAG (dynobj))
1011 s->_raw_size += PLT_CPU32_ENTRY_SIZE;
1012 else
1013 s->_raw_size += PLT_ENTRY_SIZE;
1015 /* We also need to make an entry in the .got.plt section, which
1016 will be placed in the .got section by the linker script. */
1018 s = bfd_get_section_by_name (dynobj, ".got.plt");
1019 BFD_ASSERT (s != NULL);
1020 s->_raw_size += 4;
1022 /* We also need to make an entry in the .rela.plt section. */
1024 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1025 BFD_ASSERT (s != NULL);
1026 s->_raw_size += sizeof (Elf32_External_Rela);
1028 return true;
1031 /* Reinitialize the plt offset now that it is not used as a reference
1032 count any more. */
1033 h->plt.offset = (bfd_vma) -1;
1035 /* If this is a weak symbol, and there is a real definition, the
1036 processor independent code will have arranged for us to see the
1037 real definition first, and we can just use the same value. */
1038 if (h->weakdef != NULL)
1040 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1041 || h->weakdef->root.type == bfd_link_hash_defweak);
1042 h->root.u.def.section = h->weakdef->root.u.def.section;
1043 h->root.u.def.value = h->weakdef->root.u.def.value;
1044 return true;
1047 /* This is a reference to a symbol defined by a dynamic object which
1048 is not a function. */
1050 /* If we are creating a shared library, we must presume that the
1051 only references to the symbol are via the global offset table.
1052 For such cases we need not do anything here; the relocations will
1053 be handled correctly by relocate_section. */
1054 if (info->shared)
1055 return true;
1057 /* We must allocate the symbol in our .dynbss section, which will
1058 become part of the .bss section of the executable. There will be
1059 an entry for this symbol in the .dynsym section. The dynamic
1060 object will contain position independent code, so all references
1061 from the dynamic object to this symbol will go through the global
1062 offset table. The dynamic linker will use the .dynsym entry to
1063 determine the address it must put in the global offset table, so
1064 both the dynamic object and the regular object will refer to the
1065 same memory location for the variable. */
1067 s = bfd_get_section_by_name (dynobj, ".dynbss");
1068 BFD_ASSERT (s != NULL);
1070 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1071 copy the initial value out of the dynamic object and into the
1072 runtime process image. We need to remember the offset into the
1073 .rela.bss section we are going to use. */
1074 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1076 asection *srel;
1078 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1079 BFD_ASSERT (srel != NULL);
1080 srel->_raw_size += sizeof (Elf32_External_Rela);
1081 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1084 /* We need to figure out the alignment required for this symbol. I
1085 have no idea how ELF linkers handle this. */
1086 power_of_two = bfd_log2 (h->size);
1087 if (power_of_two > 3)
1088 power_of_two = 3;
1090 /* Apply the required alignment. */
1091 s->_raw_size = BFD_ALIGN (s->_raw_size,
1092 (bfd_size_type) (1 << power_of_two));
1093 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1095 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
1096 return false;
1099 /* Define the symbol as being at this point in the section. */
1100 h->root.u.def.section = s;
1101 h->root.u.def.value = s->_raw_size;
1103 /* Increment the section size to make room for the symbol. */
1104 s->_raw_size += h->size;
1106 return true;
1109 /* Set the sizes of the dynamic sections. */
1111 static boolean
1112 elf_m68k_size_dynamic_sections (output_bfd, info)
1113 bfd *output_bfd ATTRIBUTE_UNUSED;
1114 struct bfd_link_info *info;
1116 bfd *dynobj;
1117 asection *s;
1118 boolean plt;
1119 boolean relocs;
1121 dynobj = elf_hash_table (info)->dynobj;
1122 BFD_ASSERT (dynobj != NULL);
1124 if (elf_hash_table (info)->dynamic_sections_created)
1126 /* Set the contents of the .interp section to the interpreter. */
1127 if (!info->shared)
1129 s = bfd_get_section_by_name (dynobj, ".interp");
1130 BFD_ASSERT (s != NULL);
1131 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1132 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1135 else
1137 /* We may have created entries in the .rela.got section.
1138 However, if we are not creating the dynamic sections, we will
1139 not actually use these entries. Reset the size of .rela.got,
1140 which will cause it to get stripped from the output file
1141 below. */
1142 s = bfd_get_section_by_name (dynobj, ".rela.got");
1143 if (s != NULL)
1144 s->_raw_size = 0;
1147 /* If this is a -Bsymbolic shared link, then we need to discard all PC
1148 relative relocs against symbols defined in a regular object. We
1149 allocated space for them in the check_relocs routine, but we will not
1150 fill them in in the relocate_section routine. */
1151 if (info->shared && info->symbolic)
1152 elf_m68k_link_hash_traverse (elf_m68k_hash_table (info),
1153 elf_m68k_discard_copies,
1154 (PTR) NULL);
1156 /* The check_relocs and adjust_dynamic_symbol entry points have
1157 determined the sizes of the various dynamic sections. Allocate
1158 memory for them. */
1159 plt = false;
1160 relocs = false;
1161 for (s = dynobj->sections; s != NULL; s = s->next)
1163 const char *name;
1164 boolean strip;
1166 if ((s->flags & SEC_LINKER_CREATED) == 0)
1167 continue;
1169 /* It's OK to base decisions on the section name, because none
1170 of the dynobj section names depend upon the input files. */
1171 name = bfd_get_section_name (dynobj, s);
1173 strip = false;
1175 if (strcmp (name, ".plt") == 0)
1177 if (s->_raw_size == 0)
1179 /* Strip this section if we don't need it; see the
1180 comment below. */
1181 strip = true;
1183 else
1185 /* Remember whether there is a PLT. */
1186 plt = true;
1189 else if (strncmp (name, ".rela", 5) == 0)
1191 if (s->_raw_size == 0)
1193 /* If we don't need this section, strip it from the
1194 output file. This is mostly to handle .rela.bss and
1195 .rela.plt. We must create both sections in
1196 create_dynamic_sections, because they must be created
1197 before the linker maps input sections to output
1198 sections. The linker does that before
1199 adjust_dynamic_symbol is called, and it is that
1200 function which decides whether anything needs to go
1201 into these sections. */
1202 strip = true;
1204 else
1206 relocs = true;
1208 /* We use the reloc_count field as a counter if we need
1209 to copy relocs into the output file. */
1210 s->reloc_count = 0;
1213 else if (strncmp (name, ".got", 4) != 0)
1215 /* It's not one of our sections, so don't allocate space. */
1216 continue;
1219 if (strip)
1221 _bfd_strip_section_from_output (info, s);
1222 continue;
1225 /* Allocate memory for the section contents. */
1226 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1227 Unused entries should be reclaimed before the section's contents
1228 are written out, but at the moment this does not happen. Thus in
1229 order to prevent writing out garbage, we initialise the section's
1230 contents to zero. */
1231 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1232 if (s->contents == NULL && s->_raw_size != 0)
1233 return false;
1236 if (elf_hash_table (info)->dynamic_sections_created)
1238 /* Add some entries to the .dynamic section. We fill in the
1239 values later, in elf_m68k_finish_dynamic_sections, but we
1240 must add the entries now so that we get the correct size for
1241 the .dynamic section. The DT_DEBUG entry is filled in by the
1242 dynamic linker and used by the debugger. */
1243 #define add_dynamic_entry(TAG, VAL) \
1244 bfd_elf32_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1246 if (!info->shared)
1248 if (!add_dynamic_entry (DT_DEBUG, 0))
1249 return false;
1252 if (plt)
1254 if (!add_dynamic_entry (DT_PLTGOT, 0)
1255 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1256 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1257 || !add_dynamic_entry (DT_JMPREL, 0))
1258 return false;
1261 if (relocs)
1263 if (!add_dynamic_entry (DT_RELA, 0)
1264 || !add_dynamic_entry (DT_RELASZ, 0)
1265 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1266 return false;
1269 if ((info->flags & DF_TEXTREL) != 0)
1271 if (!add_dynamic_entry (DT_TEXTREL, 0))
1272 return false;
1275 #undef add_dynamic_entry
1277 return true;
1280 /* This function is called via elf_m68k_link_hash_traverse if we are
1281 creating a shared object with -Bsymbolic. It discards the space
1282 allocated to copy PC relative relocs against symbols which are defined
1283 in regular objects. We allocated space for them in the check_relocs
1284 routine, but we won't fill them in in the relocate_section routine. */
1286 static boolean
1287 elf_m68k_discard_copies (h, ignore)
1288 struct elf_m68k_link_hash_entry *h;
1289 PTR ignore ATTRIBUTE_UNUSED;
1291 struct elf_m68k_pcrel_relocs_copied *s;
1293 /* We only discard relocs for symbols defined in a regular object. */
1294 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1295 return true;
1297 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1298 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
1300 return true;
1303 /* Relocate an M68K ELF section. */
1305 static boolean
1306 elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1307 contents, relocs, local_syms, local_sections)
1308 bfd *output_bfd;
1309 struct bfd_link_info *info;
1310 bfd *input_bfd;
1311 asection *input_section;
1312 bfd_byte *contents;
1313 Elf_Internal_Rela *relocs;
1314 Elf_Internal_Sym *local_syms;
1315 asection **local_sections;
1317 bfd *dynobj;
1318 Elf_Internal_Shdr *symtab_hdr;
1319 struct elf_link_hash_entry **sym_hashes;
1320 bfd_vma *local_got_offsets;
1321 asection *sgot;
1322 asection *splt;
1323 asection *sreloc;
1324 Elf_Internal_Rela *rel;
1325 Elf_Internal_Rela *relend;
1327 dynobj = elf_hash_table (info)->dynobj;
1328 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1329 sym_hashes = elf_sym_hashes (input_bfd);
1330 local_got_offsets = elf_local_got_offsets (input_bfd);
1332 sgot = NULL;
1333 splt = NULL;
1334 sreloc = NULL;
1336 rel = relocs;
1337 relend = relocs + input_section->reloc_count;
1338 for (; rel < relend; rel++)
1340 int r_type;
1341 reloc_howto_type *howto;
1342 unsigned long r_symndx;
1343 struct elf_link_hash_entry *h;
1344 Elf_Internal_Sym *sym;
1345 asection *sec;
1346 bfd_vma relocation;
1347 bfd_reloc_status_type r;
1349 r_type = ELF32_R_TYPE (rel->r_info);
1350 if (r_type < 0 || r_type >= (int) R_68K_max)
1352 bfd_set_error (bfd_error_bad_value);
1353 return false;
1355 howto = howto_table + r_type;
1357 r_symndx = ELF32_R_SYM (rel->r_info);
1359 if (info->relocateable)
1361 /* This is a relocateable link. We don't have to change
1362 anything, unless the reloc is against a section symbol,
1363 in which case we have to adjust according to where the
1364 section symbol winds up in the output section. */
1365 if (r_symndx < symtab_hdr->sh_info)
1367 sym = local_syms + r_symndx;
1368 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1370 sec = local_sections[r_symndx];
1371 rel->r_addend += sec->output_offset + sym->st_value;
1375 continue;
1378 /* This is a final link. */
1379 h = NULL;
1380 sym = NULL;
1381 sec = NULL;
1382 if (r_symndx < symtab_hdr->sh_info)
1384 sym = local_syms + r_symndx;
1385 sec = local_sections[r_symndx];
1386 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1388 else
1390 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1391 while (h->root.type == bfd_link_hash_indirect
1392 || h->root.type == bfd_link_hash_warning)
1393 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1394 if (h->root.type == bfd_link_hash_defined
1395 || h->root.type == bfd_link_hash_defweak)
1397 sec = h->root.u.def.section;
1398 if (((r_type == R_68K_PLT8
1399 || r_type == R_68K_PLT16
1400 || r_type == R_68K_PLT32
1401 || r_type == R_68K_PLT8O
1402 || r_type == R_68K_PLT16O
1403 || r_type == R_68K_PLT32O)
1404 && h->plt.offset != (bfd_vma) -1
1405 && elf_hash_table (info)->dynamic_sections_created)
1406 || ((r_type == R_68K_GOT8O
1407 || r_type == R_68K_GOT16O
1408 || r_type == R_68K_GOT32O
1409 || ((r_type == R_68K_GOT8
1410 || r_type == R_68K_GOT16
1411 || r_type == R_68K_GOT32)
1412 && strcmp (h->root.root.string,
1413 "_GLOBAL_OFFSET_TABLE_") != 0))
1414 && elf_hash_table (info)->dynamic_sections_created
1415 && (! info->shared
1416 || (! info->symbolic && h->dynindx != -1)
1417 || (h->elf_link_hash_flags
1418 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1419 || (info->shared
1420 && ((! info->symbolic && h->dynindx != -1)
1421 || (h->elf_link_hash_flags
1422 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1423 && ((input_section->flags & SEC_ALLOC) != 0
1424 /* DWARF will emit R_68K_32 relocations in its
1425 sections against symbols defined externally
1426 in shared libraries. We can't do anything
1427 with them here. */
1428 || ((input_section->flags & SEC_DEBUGGING) != 0
1429 && (h->elf_link_hash_flags
1430 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
1431 && (r_type == R_68K_8
1432 || r_type == R_68K_16
1433 || r_type == R_68K_32
1434 || r_type == R_68K_PC8
1435 || r_type == R_68K_PC16
1436 || r_type == R_68K_PC32)))
1438 /* In these cases, we don't need the relocation
1439 value. We check specially because in some
1440 obscure cases sec->output_section will be NULL. */
1441 relocation = 0;
1443 else
1444 relocation = (h->root.u.def.value
1445 + sec->output_section->vma
1446 + sec->output_offset);
1448 else if (h->root.type == bfd_link_hash_undefweak)
1449 relocation = 0;
1450 else if (info->shared
1451 && (!info->symbolic || info->allow_shlib_undefined)
1452 && !info->no_undefined
1453 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1454 relocation = 0;
1455 else
1457 if (!(info->callbacks->undefined_symbol
1458 (info, h->root.root.string, input_bfd,
1459 input_section, rel->r_offset,
1460 (!info->shared || info->no_undefined
1461 || ELF_ST_VISIBILITY (h->other)))))
1462 return false;
1463 relocation = 0;
1467 switch (r_type)
1469 case R_68K_GOT8:
1470 case R_68K_GOT16:
1471 case R_68K_GOT32:
1472 /* Relocation is to the address of the entry for this symbol
1473 in the global offset table. */
1474 if (h != NULL
1475 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1476 break;
1477 /* Fall through. */
1478 case R_68K_GOT8O:
1479 case R_68K_GOT16O:
1480 case R_68K_GOT32O:
1481 /* Relocation is the offset of the entry for this symbol in
1482 the global offset table. */
1485 bfd_vma off;
1487 if (sgot == NULL)
1489 sgot = bfd_get_section_by_name (dynobj, ".got");
1490 BFD_ASSERT (sgot != NULL);
1493 if (h != NULL)
1495 off = h->got.offset;
1496 BFD_ASSERT (off != (bfd_vma) -1);
1498 if (!elf_hash_table (info)->dynamic_sections_created
1499 || (info->shared
1500 && (info->symbolic || h->dynindx == -1)
1501 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1503 /* This is actually a static link, or it is a
1504 -Bsymbolic link and the symbol is defined
1505 locally, or the symbol was forced to be local
1506 because of a version file.. We must initialize
1507 this entry in the global offset table. Since
1508 the offset must always be a multiple of 4, we
1509 use the least significant bit to record whether
1510 we have initialized it already.
1512 When doing a dynamic link, we create a .rela.got
1513 relocation entry to initialize the value. This
1514 is done in the finish_dynamic_symbol routine. */
1515 if ((off & 1) != 0)
1516 off &= ~1;
1517 else
1519 bfd_put_32 (output_bfd, relocation,
1520 sgot->contents + off);
1521 h->got.offset |= 1;
1525 else
1527 BFD_ASSERT (local_got_offsets != NULL
1528 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1530 off = local_got_offsets[r_symndx];
1532 /* The offset must always be a multiple of 4. We use
1533 the least significant bit to record whether we have
1534 already generated the necessary reloc. */
1535 if ((off & 1) != 0)
1536 off &= ~1;
1537 else
1539 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1541 if (info->shared)
1543 asection *srelgot;
1544 Elf_Internal_Rela outrel;
1546 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1547 BFD_ASSERT (srelgot != NULL);
1549 outrel.r_offset = (sgot->output_section->vma
1550 + sgot->output_offset
1551 + off);
1552 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1553 outrel.r_addend = relocation;
1554 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1555 (((Elf32_External_Rela *)
1556 srelgot->contents)
1557 + srelgot->reloc_count));
1558 ++srelgot->reloc_count;
1561 local_got_offsets[r_symndx] |= 1;
1565 relocation = sgot->output_offset + off;
1566 if (r_type == R_68K_GOT8O
1567 || r_type == R_68K_GOT16O
1568 || r_type == R_68K_GOT32O)
1570 /* This relocation does not use the addend. */
1571 rel->r_addend = 0;
1573 else
1574 relocation += sgot->output_section->vma;
1576 break;
1578 case R_68K_PLT8:
1579 case R_68K_PLT16:
1580 case R_68K_PLT32:
1581 /* Relocation is to the entry for this symbol in the
1582 procedure linkage table. */
1584 /* Resolve a PLTxx reloc against a local symbol directly,
1585 without using the procedure linkage table. */
1586 if (h == NULL)
1587 break;
1589 if (h->plt.offset == (bfd_vma) -1
1590 || !elf_hash_table (info)->dynamic_sections_created)
1592 /* We didn't make a PLT entry for this symbol. This
1593 happens when statically linking PIC code, or when
1594 using -Bsymbolic. */
1595 break;
1598 if (splt == NULL)
1600 splt = bfd_get_section_by_name (dynobj, ".plt");
1601 BFD_ASSERT (splt != NULL);
1604 relocation = (splt->output_section->vma
1605 + splt->output_offset
1606 + h->plt.offset);
1607 break;
1609 case R_68K_PLT8O:
1610 case R_68K_PLT16O:
1611 case R_68K_PLT32O:
1612 /* Relocation is the offset of the entry for this symbol in
1613 the procedure linkage table. */
1614 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1616 if (splt == NULL)
1618 splt = bfd_get_section_by_name (dynobj, ".plt");
1619 BFD_ASSERT (splt != NULL);
1622 relocation = h->plt.offset;
1624 /* This relocation does not use the addend. */
1625 rel->r_addend = 0;
1627 break;
1629 case R_68K_PC8:
1630 case R_68K_PC16:
1631 case R_68K_PC32:
1632 if (h == NULL)
1633 break;
1634 /* Fall through. */
1635 case R_68K_8:
1636 case R_68K_16:
1637 case R_68K_32:
1638 if (info->shared
1639 && r_symndx != 0
1640 && (input_section->flags & SEC_ALLOC) != 0
1641 && ((r_type != R_68K_PC8
1642 && r_type != R_68K_PC16
1643 && r_type != R_68K_PC32)
1644 || (!info->symbolic
1645 || (h->elf_link_hash_flags
1646 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1648 Elf_Internal_Rela outrel;
1649 boolean skip, relocate;
1651 /* When generating a shared object, these relocations
1652 are copied into the output file to be resolved at run
1653 time. */
1655 if (sreloc == NULL)
1657 const char *name;
1659 name = (bfd_elf_string_from_elf_section
1660 (input_bfd,
1661 elf_elfheader (input_bfd)->e_shstrndx,
1662 elf_section_data (input_section)->rel_hdr.sh_name));
1663 if (name == NULL)
1664 return false;
1666 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1667 && strcmp (bfd_get_section_name (input_bfd,
1668 input_section),
1669 name + 5) == 0);
1671 sreloc = bfd_get_section_by_name (dynobj, name);
1672 BFD_ASSERT (sreloc != NULL);
1675 skip = false;
1677 outrel.r_offset =
1678 _bfd_elf_section_offset (output_bfd, info, input_section,
1679 rel->r_offset);
1680 if (outrel.r_offset == (bfd_vma) -1)
1681 skip = true;
1682 outrel.r_offset += (input_section->output_section->vma
1683 + input_section->output_offset);
1685 if (skip)
1687 memset (&outrel, 0, sizeof outrel);
1688 relocate = false;
1690 /* h->dynindx may be -1 if the symbol was marked to
1691 become local. */
1692 else if (h != NULL
1693 && ((! info->symbolic && h->dynindx != -1)
1694 || (h->elf_link_hash_flags
1695 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1697 BFD_ASSERT (h->dynindx != -1);
1698 relocate = false;
1699 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1700 outrel.r_addend = relocation + rel->r_addend;
1702 else
1704 if (r_type == R_68K_32)
1706 relocate = true;
1707 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1708 outrel.r_addend = relocation + rel->r_addend;
1710 else
1712 long indx;
1714 if (h == NULL)
1715 sec = local_sections[r_symndx];
1716 else
1718 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1719 || (h->root.type
1720 == bfd_link_hash_defweak));
1721 sec = h->root.u.def.section;
1723 if (sec != NULL && bfd_is_abs_section (sec))
1724 indx = 0;
1725 else if (sec == NULL || sec->owner == NULL)
1727 bfd_set_error (bfd_error_bad_value);
1728 return false;
1730 else
1732 asection *osec;
1734 osec = sec->output_section;
1735 indx = elf_section_data (osec)->dynindx;
1736 BFD_ASSERT (indx > 0);
1739 relocate = false;
1740 outrel.r_info = ELF32_R_INFO (indx, r_type);
1741 outrel.r_addend = relocation + rel->r_addend;
1745 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1746 (((Elf32_External_Rela *)
1747 sreloc->contents)
1748 + sreloc->reloc_count));
1749 ++sreloc->reloc_count;
1751 /* This reloc will be computed at runtime, so there's no
1752 need to do anything now, except for R_68K_32
1753 relocations that have been turned into
1754 R_68K_RELATIVE. */
1755 if (!relocate)
1756 continue;
1759 break;
1761 case R_68K_GNU_VTINHERIT:
1762 case R_68K_GNU_VTENTRY:
1763 /* These are no-ops in the end. */
1764 continue;
1766 default:
1767 break;
1770 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1771 contents, rel->r_offset,
1772 relocation, rel->r_addend);
1774 if (r != bfd_reloc_ok)
1776 switch (r)
1778 default:
1779 case bfd_reloc_outofrange:
1780 abort ();
1781 case bfd_reloc_overflow:
1783 const char *name;
1785 if (h != NULL)
1786 name = h->root.root.string;
1787 else
1789 name = bfd_elf_string_from_elf_section (input_bfd,
1790 symtab_hdr->sh_link,
1791 sym->st_name);
1792 if (name == NULL)
1793 return false;
1794 if (*name == '\0')
1795 name = bfd_section_name (input_bfd, sec);
1797 if (!(info->callbacks->reloc_overflow
1798 (info, name, howto->name, (bfd_vma) 0,
1799 input_bfd, input_section, rel->r_offset)))
1800 return false;
1802 break;
1807 return true;
1810 /* Finish up dynamic symbol handling. We set the contents of various
1811 dynamic sections here. */
1813 static boolean
1814 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
1815 bfd *output_bfd;
1816 struct bfd_link_info *info;
1817 struct elf_link_hash_entry *h;
1818 Elf_Internal_Sym *sym;
1820 bfd *dynobj;
1821 int plt_off1, plt_off2, plt_off3;
1823 dynobj = elf_hash_table (info)->dynobj;
1825 if (h->plt.offset != (bfd_vma) -1)
1827 asection *splt;
1828 asection *sgot;
1829 asection *srela;
1830 bfd_vma plt_index;
1831 bfd_vma got_offset;
1832 Elf_Internal_Rela rela;
1834 /* This symbol has an entry in the procedure linkage table. Set
1835 it up. */
1837 BFD_ASSERT (h->dynindx != -1);
1839 splt = bfd_get_section_by_name (dynobj, ".plt");
1840 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1841 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1842 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1844 /* Get the index in the procedure linkage table which
1845 corresponds to this symbol. This is the index of this symbol
1846 in all the symbols for which we are making plt entries. The
1847 first entry in the procedure linkage table is reserved. */
1848 if ( CPU32_FLAG (output_bfd))
1849 plt_index = h->plt.offset / PLT_CPU32_ENTRY_SIZE - 1;
1850 else
1851 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1853 /* Get the offset into the .got table of the entry that
1854 corresponds to this function. Each .got entry is 4 bytes.
1855 The first three are reserved. */
1856 got_offset = (plt_index + 3) * 4;
1858 if ( CPU32_FLAG (output_bfd))
1860 /* Fill in the entry in the procedure linkage table. */
1861 memcpy (splt->contents + h->plt.offset, elf_cpu32_plt_entry,
1862 PLT_CPU32_ENTRY_SIZE);
1863 plt_off1 = 4;
1864 plt_off2 = 12;
1865 plt_off3 = 18;
1867 else
1869 /* Fill in the entry in the procedure linkage table. */
1870 memcpy (splt->contents + h->plt.offset, elf_m68k_plt_entry,
1871 PLT_ENTRY_SIZE);
1872 plt_off1 = 4;
1873 plt_off2 = 10;
1874 plt_off3 = 16;
1877 /* The offset is relative to the first extension word. */
1878 bfd_put_32 (output_bfd,
1879 (sgot->output_section->vma
1880 + sgot->output_offset
1881 + got_offset
1882 - (splt->output_section->vma
1883 + h->plt.offset + 2)),
1884 splt->contents + h->plt.offset + plt_off1);
1886 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
1887 splt->contents + h->plt.offset + plt_off2);
1888 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3),
1889 splt->contents + h->plt.offset + plt_off3);
1891 /* Fill in the entry in the global offset table. */
1892 bfd_put_32 (output_bfd,
1893 (splt->output_section->vma
1894 + splt->output_offset
1895 + h->plt.offset
1896 + 8),
1897 sgot->contents + got_offset);
1899 /* Fill in the entry in the .rela.plt section. */
1900 rela.r_offset = (sgot->output_section->vma
1901 + sgot->output_offset
1902 + got_offset);
1903 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
1904 rela.r_addend = 0;
1905 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1906 ((Elf32_External_Rela *) srela->contents
1907 + plt_index));
1909 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1911 /* Mark the symbol as undefined, rather than as defined in
1912 the .plt section. Leave the value alone. */
1913 sym->st_shndx = SHN_UNDEF;
1917 if (h->got.offset != (bfd_vma) -1)
1919 asection *sgot;
1920 asection *srela;
1921 Elf_Internal_Rela rela;
1923 /* This symbol has an entry in the global offset table. Set it
1924 up. */
1926 sgot = bfd_get_section_by_name (dynobj, ".got");
1927 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1928 BFD_ASSERT (sgot != NULL && srela != NULL);
1930 rela.r_offset = (sgot->output_section->vma
1931 + sgot->output_offset
1932 + (h->got.offset &~ (bfd_vma) 1));
1934 /* If this is a -Bsymbolic link, and the symbol is defined
1935 locally, we just want to emit a RELATIVE reloc. Likewise if
1936 the symbol was forced to be local because of a version file.
1937 The entry in the global offset table will already have been
1938 initialized in the relocate_section function. */
1939 if (info->shared
1940 && (info->symbolic || h->dynindx == -1)
1941 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1943 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1944 rela.r_addend = bfd_get_signed_32 (output_bfd,
1945 (sgot->contents
1946 + (h->got.offset &~ (bfd_vma) 1)));
1948 else
1950 bfd_put_32 (output_bfd, (bfd_vma) 0,
1951 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
1952 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
1953 rela.r_addend = 0;
1956 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1957 ((Elf32_External_Rela *) srela->contents
1958 + srela->reloc_count));
1959 ++srela->reloc_count;
1962 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1964 asection *s;
1965 Elf_Internal_Rela rela;
1967 /* This symbol needs a copy reloc. Set it up. */
1969 BFD_ASSERT (h->dynindx != -1
1970 && (h->root.type == bfd_link_hash_defined
1971 || h->root.type == bfd_link_hash_defweak));
1973 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1974 ".rela.bss");
1975 BFD_ASSERT (s != NULL);
1977 rela.r_offset = (h->root.u.def.value
1978 + h->root.u.def.section->output_section->vma
1979 + h->root.u.def.section->output_offset);
1980 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
1981 rela.r_addend = 0;
1982 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1983 ((Elf32_External_Rela *) s->contents
1984 + s->reloc_count));
1985 ++s->reloc_count;
1988 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1989 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1990 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1991 sym->st_shndx = SHN_ABS;
1993 return true;
1996 /* Finish up the dynamic sections. */
1998 static boolean
1999 elf_m68k_finish_dynamic_sections (output_bfd, info)
2000 bfd *output_bfd;
2001 struct bfd_link_info *info;
2003 bfd *dynobj;
2004 asection *sgot;
2005 asection *sdyn;
2007 dynobj = elf_hash_table (info)->dynobj;
2009 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2010 BFD_ASSERT (sgot != NULL);
2011 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2013 if (elf_hash_table (info)->dynamic_sections_created)
2015 asection *splt;
2016 Elf32_External_Dyn *dyncon, *dynconend;
2018 splt = bfd_get_section_by_name (dynobj, ".plt");
2019 BFD_ASSERT (splt != NULL && sdyn != NULL);
2021 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2022 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2023 for (; dyncon < dynconend; dyncon++)
2025 Elf_Internal_Dyn dyn;
2026 const char *name;
2027 asection *s;
2029 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2031 switch (dyn.d_tag)
2033 default:
2034 break;
2036 case DT_PLTGOT:
2037 name = ".got";
2038 goto get_vma;
2039 case DT_JMPREL:
2040 name = ".rela.plt";
2041 get_vma:
2042 s = bfd_get_section_by_name (output_bfd, name);
2043 BFD_ASSERT (s != NULL);
2044 dyn.d_un.d_ptr = s->vma;
2045 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2046 break;
2048 case DT_PLTRELSZ:
2049 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2050 BFD_ASSERT (s != NULL);
2051 if (s->_cooked_size != 0)
2052 dyn.d_un.d_val = s->_cooked_size;
2053 else
2054 dyn.d_un.d_val = s->_raw_size;
2055 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2056 break;
2058 case DT_RELASZ:
2059 /* The procedure linkage table relocs (DT_JMPREL) should
2060 not be included in the overall relocs (DT_RELA).
2061 Therefore, we override the DT_RELASZ entry here to
2062 make it not include the JMPREL relocs. Since the
2063 linker script arranges for .rela.plt to follow all
2064 other relocation sections, we don't have to worry
2065 about changing the DT_RELA entry. */
2066 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2067 if (s != NULL)
2069 if (s->_cooked_size != 0)
2070 dyn.d_un.d_val -= s->_cooked_size;
2071 else
2072 dyn.d_un.d_val -= s->_raw_size;
2074 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2075 break;
2079 /* Fill in the first entry in the procedure linkage table. */
2080 if (splt->_raw_size > 0)
2082 if (!CPU32_FLAG (output_bfd))
2084 memcpy (splt->contents, elf_m68k_plt0_entry, PLT_ENTRY_SIZE);
2085 bfd_put_32 (output_bfd,
2086 (sgot->output_section->vma
2087 + sgot->output_offset + 4
2088 - (splt->output_section->vma + 2)),
2089 splt->contents + 4);
2090 bfd_put_32 (output_bfd,
2091 (sgot->output_section->vma
2092 + sgot->output_offset + 8
2093 - (splt->output_section->vma + 10)),
2094 splt->contents + 12);
2095 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2096 = PLT_ENTRY_SIZE;
2098 else /* cpu32 */
2100 memcpy (splt->contents, elf_cpu32_plt0_entry, PLT_CPU32_ENTRY_SIZE);
2101 bfd_put_32 (output_bfd,
2102 (sgot->output_section->vma
2103 + sgot->output_offset + 4
2104 - (splt->output_section->vma + 2)),
2105 splt->contents + 4);
2106 bfd_put_32 (output_bfd,
2107 (sgot->output_section->vma
2108 + sgot->output_offset + 8
2109 - (splt->output_section->vma + 10)),
2110 splt->contents + 12);
2111 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2112 = PLT_CPU32_ENTRY_SIZE;
2117 /* Fill in the first three entries in the global offset table. */
2118 if (sgot->_raw_size > 0)
2120 if (sdyn == NULL)
2121 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2122 else
2123 bfd_put_32 (output_bfd,
2124 sdyn->output_section->vma + sdyn->output_offset,
2125 sgot->contents);
2126 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2127 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2130 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2132 return true;
2135 /* Given a .data section and a .emreloc in-memory section, store
2136 relocation information into the .emreloc section which can be
2137 used at runtime to relocate the section. This is called by the
2138 linker when the --embedded-relocs switch is used. This is called
2139 after the add_symbols entry point has been called for all the
2140 objects, and before the final_link entry point is called. */
2142 boolean
2143 bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2144 bfd *abfd;
2145 struct bfd_link_info *info;
2146 asection *datasec;
2147 asection *relsec;
2148 char **errmsg;
2150 Elf_Internal_Shdr *symtab_hdr;
2151 Elf_Internal_Shdr *shndx_hdr;
2152 Elf32_External_Sym *extsyms;
2153 Elf32_External_Sym *free_extsyms = NULL;
2154 Elf_External_Sym_Shndx *shndx_buf = NULL;
2155 Elf_Internal_Rela *internal_relocs;
2156 Elf_Internal_Rela *free_relocs = NULL;
2157 Elf_Internal_Rela *irel, *irelend;
2158 bfd_byte *p;
2159 bfd_size_type amt;
2161 BFD_ASSERT (! info->relocateable);
2163 *errmsg = NULL;
2165 if (datasec->reloc_count == 0)
2166 return true;
2168 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2169 /* Read this BFD's symbols if we haven't done so already, or get the cached
2170 copy if it exists. */
2171 if (symtab_hdr->contents != NULL)
2172 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2173 else
2175 /* Go get them off disk. */
2176 amt = symtab_hdr->sh_info * sizeof (Elf32_External_Sym);
2177 if (info->keep_memory)
2178 extsyms = (Elf32_External_Sym *) bfd_alloc (abfd, amt);
2179 else
2180 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
2181 if (extsyms == NULL)
2182 goto error_return;
2183 if (! info->keep_memory)
2184 free_extsyms = extsyms;
2185 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2186 || bfd_bread (extsyms, amt, abfd) != amt)
2187 goto error_return;
2188 if (info->keep_memory)
2189 symtab_hdr->contents = (unsigned char *) extsyms;
2192 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
2193 if (shndx_hdr->sh_size != 0)
2195 amt = symtab_hdr->sh_info * sizeof (Elf_External_Sym_Shndx);
2196 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
2197 if (shndx_buf == NULL)
2198 goto error_return;
2199 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
2200 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
2201 goto error_return;
2204 /* Get a copy of the native relocations. */
2205 internal_relocs = (_bfd_elf32_link_read_relocs
2206 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2207 info->keep_memory));
2208 if (internal_relocs == NULL)
2209 goto error_return;
2210 if (! info->keep_memory)
2211 free_relocs = internal_relocs;
2213 amt = (bfd_size_type) datasec->reloc_count * 12;
2214 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
2215 if (relsec->contents == NULL)
2216 goto error_return;
2218 p = relsec->contents;
2220 irelend = internal_relocs + datasec->reloc_count;
2221 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2223 asection *targetsec;
2225 /* We are going to write a four byte longword into the runtime
2226 reloc section. The longword will be the address in the data
2227 section which must be relocated. It is followed by the name
2228 of the target section NUL-padded or truncated to 8
2229 characters. */
2231 /* We can only relocate absolute longword relocs at run time. */
2232 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2234 *errmsg = _("unsupported reloc type");
2235 bfd_set_error (bfd_error_bad_value);
2236 goto error_return;
2239 /* Get the target section referred to by the reloc. */
2240 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2242 Elf32_External_Sym *esym;
2243 Elf_External_Sym_Shndx *shndx;
2244 Elf_Internal_Sym isym;
2246 /* A local symbol. */
2247 esym = extsyms + ELF32_R_SYM (irel->r_info);
2248 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (irel->r_info) : 0);
2249 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
2251 targetsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
2253 else
2255 unsigned long indx;
2256 struct elf_link_hash_entry *h;
2258 /* An external symbol. */
2259 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2260 h = elf_sym_hashes (abfd)[indx];
2261 BFD_ASSERT (h != NULL);
2262 if (h->root.type == bfd_link_hash_defined
2263 || h->root.type == bfd_link_hash_defweak)
2264 targetsec = h->root.u.def.section;
2265 else
2266 targetsec = NULL;
2269 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2270 memset (p + 4, 0, 8);
2271 if (targetsec != NULL)
2272 strncpy (p + 4, targetsec->output_section->name, 8);
2275 if (shndx_buf != NULL)
2276 free (shndx_buf);
2277 if (free_extsyms != NULL)
2278 free (free_extsyms);
2279 if (free_relocs != NULL)
2280 free (free_relocs);
2281 return true;
2283 error_return:
2284 if (shndx_buf != NULL)
2285 free (shndx_buf);
2286 if (free_extsyms != NULL)
2287 free (free_extsyms);
2288 if (free_relocs != NULL)
2289 free (free_relocs);
2290 return false;
2293 static enum elf_reloc_type_class
2294 elf32_m68k_reloc_type_class (rela)
2295 const Elf_Internal_Rela *rela;
2297 switch ((int) ELF32_R_TYPE (rela->r_info))
2299 case R_68K_RELATIVE:
2300 return reloc_class_relative;
2301 case R_68K_JMP_SLOT:
2302 return reloc_class_plt;
2303 case R_68K_COPY:
2304 return reloc_class_copy;
2305 default:
2306 return reloc_class_normal;
2310 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
2311 #define TARGET_BIG_NAME "elf32-m68k"
2312 #define ELF_MACHINE_CODE EM_68K
2313 #define ELF_MAXPAGESIZE 0x2000
2314 #define elf_backend_create_dynamic_sections \
2315 _bfd_elf_create_dynamic_sections
2316 #define bfd_elf32_bfd_link_hash_table_create \
2317 elf_m68k_link_hash_table_create
2318 #define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
2320 #define elf_backend_check_relocs elf_m68k_check_relocs
2321 #define elf_backend_adjust_dynamic_symbol \
2322 elf_m68k_adjust_dynamic_symbol
2323 #define elf_backend_size_dynamic_sections \
2324 elf_m68k_size_dynamic_sections
2325 #define elf_backend_relocate_section elf_m68k_relocate_section
2326 #define elf_backend_finish_dynamic_symbol \
2327 elf_m68k_finish_dynamic_symbol
2328 #define elf_backend_finish_dynamic_sections \
2329 elf_m68k_finish_dynamic_sections
2330 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
2331 #define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
2332 #define bfd_elf32_bfd_copy_private_bfd_data \
2333 elf32_m68k_copy_private_bfd_data
2334 #define bfd_elf32_bfd_merge_private_bfd_data \
2335 elf32_m68k_merge_private_bfd_data
2336 #define bfd_elf32_bfd_set_private_flags \
2337 elf32_m68k_set_private_flags
2338 #define bfd_elf32_bfd_print_private_bfd_data \
2339 elf32_m68k_print_private_bfd_data
2340 #define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
2342 #define elf_backend_can_gc_sections 1
2343 #define elf_backend_can_refcount 1
2344 #define elf_backend_want_got_plt 1
2345 #define elf_backend_plt_readonly 1
2346 #define elf_backend_want_plt_sym 0
2347 #define elf_backend_got_header_size 12
2349 #include "elf32-target.h"