2001-03-22 Philip Blundell <philb@gnu.org>
[binutils.git] / bfd / elf32-m68k.c
blob78a0814eddfe70b49191153dfd0afca3412a8b10
1 /* Motorola 68k series support for 32-bit ELF
2 Copyright 1993, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "bfdlink.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/m68k.h"
27 static reloc_howto_type *reloc_type_lookup
28 PARAMS ((bfd *, bfd_reloc_code_real_type));
29 static void rtype_to_howto
30 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
31 static struct bfd_hash_entry *elf_m68k_link_hash_newfunc
32 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
33 static struct bfd_link_hash_table *elf_m68k_link_hash_table_create
34 PARAMS ((bfd *));
35 static boolean elf_m68k_check_relocs
36 PARAMS ((bfd *, struct bfd_link_info *, asection *,
37 const Elf_Internal_Rela *));
38 static asection *elf_m68k_gc_mark_hook
39 PARAMS ((bfd *, struct bfd_link_info *, Elf_Internal_Rela *,
40 struct elf_link_hash_entry *, Elf_Internal_Sym *));
41 static boolean elf_m68k_gc_sweep_hook
42 PARAMS ((bfd *, struct bfd_link_info *, asection *,
43 const Elf_Internal_Rela *));
44 static boolean elf_m68k_adjust_dynamic_symbol
45 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
46 static boolean elf_m68k_size_dynamic_sections
47 PARAMS ((bfd *, struct bfd_link_info *));
48 static boolean elf_m68k_relocate_section
49 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
50 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
51 static boolean elf_m68k_finish_dynamic_symbol
52 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
53 Elf_Internal_Sym *));
54 static boolean elf_m68k_finish_dynamic_sections
55 PARAMS ((bfd *, struct bfd_link_info *));
57 static boolean elf32_m68k_set_private_flags
58 PARAMS ((bfd *, flagword));
59 static boolean elf32_m68k_copy_private_bfd_data
60 PARAMS ((bfd *, bfd *));
61 static boolean elf32_m68k_merge_private_bfd_data
62 PARAMS ((bfd *, bfd *));
63 static boolean elf32_m68k_print_private_bfd_data
64 PARAMS ((bfd *, PTR));
66 static reloc_howto_type howto_table[] = {
67 HOWTO(R_68K_NONE, 0, 0, 0, false,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_NONE", false, 0, 0x00000000,false),
68 HOWTO(R_68K_32, 0, 2,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32", false, 0, 0xffffffff,false),
69 HOWTO(R_68K_16, 0, 1,16, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16", false, 0, 0x0000ffff,false),
70 HOWTO(R_68K_8, 0, 0, 8, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8", false, 0, 0x000000ff,false),
71 HOWTO(R_68K_PC32, 0, 2,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32", false, 0, 0xffffffff,true),
72 HOWTO(R_68K_PC16, 0, 1,16, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC16", false, 0, 0x0000ffff,true),
73 HOWTO(R_68K_PC8, 0, 0, 8, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC8", false, 0, 0x000000ff,true),
74 HOWTO(R_68K_GOT32, 0, 2,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32", false, 0, 0xffffffff,true),
75 HOWTO(R_68K_GOT16, 0, 1,16, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16", false, 0, 0x0000ffff,true),
76 HOWTO(R_68K_GOT8, 0, 0, 8, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8", false, 0, 0x000000ff,true),
77 HOWTO(R_68K_GOT32O, 0, 2,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O", false, 0, 0xffffffff,false),
78 HOWTO(R_68K_GOT16O, 0, 1,16, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16O", false, 0, 0x0000ffff,false),
79 HOWTO(R_68K_GOT8O, 0, 0, 8, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8O", false, 0, 0x000000ff,false),
80 HOWTO(R_68K_PLT32, 0, 2,32, true, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32", false, 0, 0xffffffff,true),
81 HOWTO(R_68K_PLT16, 0, 1,16, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16", false, 0, 0x0000ffff,true),
82 HOWTO(R_68K_PLT8, 0, 0, 8, true, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8", false, 0, 0x000000ff,true),
83 HOWTO(R_68K_PLT32O, 0, 2,32, false,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O", false, 0, 0xffffffff,false),
84 HOWTO(R_68K_PLT16O, 0, 1,16, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16O", false, 0, 0x0000ffff,false),
85 HOWTO(R_68K_PLT8O, 0, 0, 8, false,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8O", false, 0, 0x000000ff,false),
86 HOWTO(R_68K_COPY, 0, 0, 0, false,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_COPY", false, 0, 0xffffffff,false),
87 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),
88 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),
89 HOWTO(R_68K_RELATIVE, 0, 2,32, false,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_RELATIVE", false, 0, 0xffffffff,false),
90 /* GNU extension to record C++ vtable hierarchy */
91 HOWTO (R_68K_GNU_VTINHERIT, /* type */
92 0, /* rightshift */
93 2, /* size (0 = byte, 1 = short, 2 = long) */
94 0, /* bitsize */
95 false, /* pc_relative */
96 0, /* bitpos */
97 complain_overflow_dont, /* complain_on_overflow */
98 NULL, /* special_function */
99 "R_68K_GNU_VTINHERIT", /* name */
100 false, /* partial_inplace */
101 0, /* src_mask */
102 0, /* dst_mask */
103 false),
104 /* GNU extension to record C++ vtable member usage */
105 HOWTO (R_68K_GNU_VTENTRY, /* type */
106 0, /* rightshift */
107 2, /* size (0 = byte, 1 = short, 2 = long) */
108 0, /* bitsize */
109 false, /* pc_relative */
110 0, /* bitpos */
111 complain_overflow_dont, /* complain_on_overflow */
112 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
113 "R_68K_GNU_VTENTRY", /* name */
114 false, /* partial_inplace */
115 0, /* src_mask */
116 0, /* dst_mask */
117 false),
120 static void
121 rtype_to_howto (abfd, cache_ptr, dst)
122 bfd *abfd ATTRIBUTE_UNUSED;
123 arelent *cache_ptr;
124 Elf_Internal_Rela *dst;
126 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K_max);
127 cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
130 #define elf_info_to_howto rtype_to_howto
132 static const struct
134 bfd_reloc_code_real_type bfd_val;
135 int elf_val;
136 } reloc_map[] = {
137 { BFD_RELOC_NONE, R_68K_NONE },
138 { BFD_RELOC_32, R_68K_32 },
139 { BFD_RELOC_16, R_68K_16 },
140 { BFD_RELOC_8, R_68K_8 },
141 { BFD_RELOC_32_PCREL, R_68K_PC32 },
142 { BFD_RELOC_16_PCREL, R_68K_PC16 },
143 { BFD_RELOC_8_PCREL, R_68K_PC8 },
144 { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
145 { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
146 { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
147 { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
148 { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
149 { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
150 { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
151 { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
152 { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
153 { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
154 { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
155 { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
156 { BFD_RELOC_NONE, R_68K_COPY },
157 { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
158 { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
159 { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
160 { BFD_RELOC_CTOR, R_68K_32 },
161 { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
162 { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
165 static reloc_howto_type *
166 reloc_type_lookup (abfd, code)
167 bfd *abfd ATTRIBUTE_UNUSED;
168 bfd_reloc_code_real_type code;
170 unsigned int i;
171 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
173 if (reloc_map[i].bfd_val == code)
174 return &howto_table[reloc_map[i].elf_val];
176 return 0;
179 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
180 #define ELF_ARCH bfd_arch_m68k
181 /* end code generated by elf.el */
183 #define USE_RELA
185 /* Functions for the m68k ELF linker. */
187 /* The name of the dynamic interpreter. This is put in the .interp
188 section. */
190 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
192 /* The size in bytes of an entry in the procedure linkage table. */
194 #define PLT_ENTRY_SIZE 20
196 /* The first entry in a procedure linkage table looks like this. See
197 the SVR4 ABI m68k supplement to see how this works. */
199 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
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. */
208 /* Subsequent entries in a procedure linkage table look like this. */
210 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
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. */
220 #define CPU32_FLAG(abfd) (elf_elfheader (abfd)->e_flags & EF_CPU32)
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] =
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
235 static const bfd_byte elf_cpu32_plt_entry[PLT_CPU32_ENTRY_SIZE] =
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
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. */
253 /* This structure keeps track of the number of PC relative relocs we have
254 copied for a given symbol. */
256 struct elf_m68k_pcrel_relocs_copied
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;
266 /* m68k ELF linker hash entry. */
268 struct elf_m68k_link_hash_entry
270 struct elf_link_hash_entry root;
272 /* Number of PC relative relocs copied for this symbol. */
273 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
276 /* m68k ELF linker hash table. */
278 struct elf_m68k_link_hash_table
280 struct elf_link_hash_table root;
283 /* Declare this now that the above structures are defined. */
285 static boolean elf_m68k_discard_copies
286 PARAMS ((struct elf_m68k_link_hash_entry *, PTR));
288 /* Traverse an m68k ELF linker hash table. */
290 #define elf_m68k_link_hash_traverse(table, func, info) \
291 (elf_link_hash_traverse \
292 (&(table)->root, \
293 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
294 (info)))
296 /* Get the m68k ELF linker hash table from a link_info structure. */
298 #define elf_m68k_hash_table(p) \
299 ((struct elf_m68k_link_hash_table *) (p)->hash)
301 /* Create an entry in an m68k ELF linker hash table. */
303 static struct bfd_hash_entry *
304 elf_m68k_link_hash_newfunc (entry, table, string)
305 struct bfd_hash_entry *entry;
306 struct bfd_hash_table *table;
307 const char *string;
309 struct elf_m68k_link_hash_entry *ret =
310 (struct elf_m68k_link_hash_entry *) entry;
312 /* Allocate the structure if it has not already been allocated by a
313 subclass. */
314 if (ret == (struct elf_m68k_link_hash_entry *) NULL)
315 ret = ((struct elf_m68k_link_hash_entry *)
316 bfd_hash_allocate (table,
317 sizeof (struct elf_m68k_link_hash_entry)));
318 if (ret == (struct elf_m68k_link_hash_entry *) NULL)
319 return (struct bfd_hash_entry *) ret;
321 /* Call the allocation method of the superclass. */
322 ret = ((struct elf_m68k_link_hash_entry *)
323 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
324 table, string));
325 if (ret != (struct elf_m68k_link_hash_entry *) NULL)
327 ret->pcrel_relocs_copied = NULL;
330 return (struct bfd_hash_entry *) ret;
333 /* Create an m68k ELF linker hash table. */
335 static struct bfd_link_hash_table *
336 elf_m68k_link_hash_table_create (abfd)
337 bfd *abfd;
339 struct elf_m68k_link_hash_table *ret;
341 ret = ((struct elf_m68k_link_hash_table *)
342 bfd_alloc (abfd, sizeof (struct elf_m68k_link_hash_table)));
343 if (ret == (struct elf_m68k_link_hash_table *) NULL)
344 return NULL;
346 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
347 elf_m68k_link_hash_newfunc))
349 bfd_release (abfd, ret);
350 return NULL;
353 return &ret->root.root;
356 /* Keep m68k-specific flags in the ELF header */
357 static boolean
358 elf32_m68k_set_private_flags (abfd, flags)
359 bfd *abfd;
360 flagword flags;
362 elf_elfheader (abfd)->e_flags = flags;
363 elf_flags_init (abfd) = true;
364 return true;
367 /* Copy m68k-specific data from one module to another */
368 static boolean
369 elf32_m68k_copy_private_bfd_data (ibfd, obfd)
370 bfd *ibfd;
371 bfd *obfd;
373 flagword in_flags;
375 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
376 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
377 return true;
379 in_flags = elf_elfheader (ibfd)->e_flags;
381 elf_elfheader (obfd)->e_flags = in_flags;
382 elf_flags_init (obfd) = true;
384 return true;
387 /* Merge backend specific data from an object file to the output
388 object file when linking. */
389 static boolean
390 elf32_m68k_merge_private_bfd_data (ibfd, obfd)
391 bfd *ibfd;
392 bfd *obfd;
394 flagword out_flags;
395 flagword in_flags;
397 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
398 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
399 return true;
401 in_flags = elf_elfheader (ibfd)->e_flags;
402 out_flags = elf_elfheader (obfd)->e_flags;
404 if (!elf_flags_init (obfd))
406 elf_flags_init (obfd) = true;
407 elf_elfheader (obfd)->e_flags = in_flags;
410 return true;
413 /* Display the flags field */
414 static boolean
415 elf32_m68k_print_private_bfd_data (abfd, ptr)
416 bfd *abfd;
417 PTR ptr;
419 FILE *file = (FILE *) ptr;
421 BFD_ASSERT (abfd != NULL && ptr != NULL);
423 /* Print normal ELF private data. */
424 _bfd_elf_print_private_bfd_data (abfd, ptr);
426 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
428 /* xgettext:c-format */
429 fprintf (file, _ ("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
431 if (elf_elfheader (abfd)->e_flags & EF_CPU32)
432 fprintf (file, _ (" [cpu32]"));
434 fputc ('\n', file);
436 return true;
438 /* Look through the relocs for a section during the first phase, and
439 allocate space in the global offset table or procedure linkage
440 table. */
442 static boolean
443 elf_m68k_check_relocs (abfd, info, sec, relocs)
444 bfd *abfd;
445 struct bfd_link_info *info;
446 asection *sec;
447 const Elf_Internal_Rela *relocs;
449 bfd *dynobj;
450 Elf_Internal_Shdr *symtab_hdr;
451 struct elf_link_hash_entry **sym_hashes;
452 bfd_signed_vma *local_got_refcounts;
453 const Elf_Internal_Rela *rel;
454 const Elf_Internal_Rela *rel_end;
455 asection *sgot;
456 asection *srelgot;
457 asection *sreloc;
459 if (info->relocateable)
460 return true;
462 dynobj = elf_hash_table (info)->dynobj;
463 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
464 sym_hashes = elf_sym_hashes (abfd);
465 local_got_refcounts = elf_local_got_refcounts (abfd);
467 sgot = NULL;
468 srelgot = NULL;
469 sreloc = NULL;
471 rel_end = relocs + sec->reloc_count;
472 for (rel = relocs; rel < rel_end; rel++)
474 unsigned long r_symndx;
475 struct elf_link_hash_entry *h;
477 r_symndx = ELF32_R_SYM (rel->r_info);
479 if (r_symndx < symtab_hdr->sh_info)
480 h = NULL;
481 else
482 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
484 switch (ELF32_R_TYPE (rel->r_info))
486 case R_68K_GOT8:
487 case R_68K_GOT16:
488 case R_68K_GOT32:
489 if (h != NULL
490 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
491 break;
492 /* Fall through. */
493 case R_68K_GOT8O:
494 case R_68K_GOT16O:
495 case R_68K_GOT32O:
496 /* This symbol requires a global offset table entry. */
498 if (dynobj == NULL)
500 /* Create the .got section. */
501 elf_hash_table (info)->dynobj = dynobj = abfd;
502 if (!_bfd_elf_create_got_section (dynobj, info))
503 return false;
506 if (sgot == NULL)
508 sgot = bfd_get_section_by_name (dynobj, ".got");
509 BFD_ASSERT (sgot != NULL);
512 if (srelgot == NULL
513 && (h != NULL || info->shared))
515 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
516 if (srelgot == NULL)
518 srelgot = bfd_make_section (dynobj, ".rela.got");
519 if (srelgot == NULL
520 || !bfd_set_section_flags (dynobj, srelgot,
521 (SEC_ALLOC
522 | SEC_LOAD
523 | SEC_HAS_CONTENTS
524 | SEC_IN_MEMORY
525 | SEC_LINKER_CREATED
526 | SEC_READONLY))
527 || !bfd_set_section_alignment (dynobj, srelgot, 2))
528 return false;
532 if (h != NULL)
534 if (h->got.refcount == -1)
536 h->got.refcount = 1;
538 /* Make sure this symbol is output as a dynamic symbol. */
539 if (h->dynindx == -1)
541 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
542 return false;
545 /* Allocate space in the .got section. */
546 sgot->_raw_size += 4;
547 /* Allocate relocation space. */
548 srelgot->_raw_size += sizeof (Elf32_External_Rela);
550 else
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 size_t size;
560 size = symtab_hdr->sh_info * sizeof (bfd_signed_vma);
561 local_got_refcounts = ((bfd_signed_vma *)
562 bfd_alloc (abfd, size));
563 if (local_got_refcounts == NULL)
564 return false;
565 elf_local_got_refcounts (abfd) = local_got_refcounts;
566 memset (local_got_refcounts, -1, size);
568 if (local_got_refcounts[r_symndx] == -1)
570 local_got_refcounts[r_symndx] = 1;
572 sgot->_raw_size += 4;
573 if (info->shared)
575 /* If we are generating a shared object, we need to
576 output a R_68K_RELATIVE reloc so that the dynamic
577 linker can adjust this GOT entry. */
578 srelgot->_raw_size += sizeof (Elf32_External_Rela);
581 else
582 local_got_refcounts[r_symndx]++;
584 break;
586 case R_68K_PLT8:
587 case R_68K_PLT16:
588 case R_68K_PLT32:
589 /* This symbol requires a procedure linkage table entry. We
590 actually build the entry in adjust_dynamic_symbol,
591 because this might be a case of linking PIC code which is
592 never referenced by a dynamic object, in which case we
593 don't need to generate a procedure linkage table entry
594 after all. */
596 /* If this is a local symbol, we resolve it directly without
597 creating a procedure linkage table entry. */
598 if (h == NULL)
599 continue;
601 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
602 if (h->plt.refcount == -1)
603 h->plt.refcount = 1;
604 else
605 h->plt.refcount++;
606 break;
608 case R_68K_PLT8O:
609 case R_68K_PLT16O:
610 case R_68K_PLT32O:
611 /* This symbol requires a procedure linkage table entry. */
613 if (h == NULL)
615 /* It does not make sense to have this relocation for a
616 local symbol. FIXME: does it? How to handle it if
617 it does make sense? */
618 bfd_set_error (bfd_error_bad_value);
619 return false;
622 /* Make sure this symbol is output as a dynamic symbol. */
623 if (h->dynindx == -1)
625 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
626 return false;
629 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
630 if (h->plt.refcount == -1)
631 h->plt.refcount = 1;
632 else
633 h->plt.refcount++;
634 break;
636 case R_68K_PC8:
637 case R_68K_PC16:
638 case R_68K_PC32:
639 /* If we are creating a shared library and this is not a local
640 symbol, we need to copy the reloc into the shared library.
641 However when linking with -Bsymbolic and this is a global
642 symbol which is defined in an object we are including in the
643 link (i.e., DEF_REGULAR is set), then we can resolve the
644 reloc directly. At this point we have not seen all the input
645 files, so it is possible that DEF_REGULAR is not set now but
646 will be set later (it is never cleared). We account for that
647 possibility below by storing information in the
648 pcrel_relocs_copied field of the hash table entry. */
649 if (!(info->shared
650 && (sec->flags & SEC_ALLOC) != 0
651 && h != NULL
652 && (!info->symbolic
653 || (h->elf_link_hash_flags
654 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
656 if (h != NULL)
658 /* Make sure a plt entry is created for this symbol if
659 it turns out to be a function defined by a dynamic
660 object. */
661 if (h->plt.refcount == -1)
662 h->plt.refcount = 1;
663 else
664 h->plt.refcount++;
666 break;
668 /* Fall through. */
669 case R_68K_8:
670 case R_68K_16:
671 case R_68K_32:
672 if (h != NULL)
674 /* Make sure a plt entry is created for this symbol if it
675 turns out to be a function defined by a dynamic object. */
676 if (h->plt.refcount == -1)
677 h->plt.refcount = 1;
678 else
679 h->plt.refcount++;
682 /* If we are creating a shared library, we need to copy the
683 reloc into the shared library. */
684 if (info->shared
685 && (sec->flags & SEC_ALLOC) != 0)
687 /* When creating a shared object, we must copy these
688 reloc types into the output file. We create a reloc
689 section in dynobj and make room for this reloc. */
690 if (sreloc == NULL)
692 const char *name;
694 name = (bfd_elf_string_from_elf_section
695 (abfd,
696 elf_elfheader (abfd)->e_shstrndx,
697 elf_section_data (sec)->rel_hdr.sh_name));
698 if (name == NULL)
699 return false;
701 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
702 && strcmp (bfd_get_section_name (abfd, sec),
703 name + 5) == 0);
705 sreloc = bfd_get_section_by_name (dynobj, name);
706 if (sreloc == NULL)
708 sreloc = bfd_make_section (dynobj, name);
709 if (sreloc == NULL
710 || !bfd_set_section_flags (dynobj, sreloc,
711 (SEC_ALLOC
712 | SEC_LOAD
713 | SEC_HAS_CONTENTS
714 | SEC_IN_MEMORY
715 | SEC_LINKER_CREATED
716 | SEC_READONLY))
717 || !bfd_set_section_alignment (dynobj, sreloc, 2))
718 return false;
722 sreloc->_raw_size += sizeof (Elf32_External_Rela);
724 /* If we are linking with -Bsymbolic, we count the number of
725 PC relative relocations we have entered for this symbol,
726 so that we can discard them again if the symbol is later
727 defined by a regular object. Note that this function is
728 only called if we are using an m68kelf linker hash table,
729 which means that h is really a pointer to an
730 elf_m68k_link_hash_entry. */
731 if ((ELF32_R_TYPE (rel->r_info) == R_68K_PC8
732 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
733 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
734 && info->symbolic)
736 struct elf_m68k_link_hash_entry *eh;
737 struct elf_m68k_pcrel_relocs_copied *p;
739 eh = (struct elf_m68k_link_hash_entry *) h;
741 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
742 if (p->section == sreloc)
743 break;
745 if (p == NULL)
747 p = ((struct elf_m68k_pcrel_relocs_copied *)
748 bfd_alloc (dynobj, sizeof *p));
749 if (p == NULL)
750 return false;
751 p->next = eh->pcrel_relocs_copied;
752 eh->pcrel_relocs_copied = p;
753 p->section = sreloc;
754 p->count = 0;
757 ++p->count;
761 break;
763 /* This relocation describes the C++ object vtable hierarchy.
764 Reconstruct it for later use during GC. */
765 case R_68K_GNU_VTINHERIT:
766 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
767 return false;
768 break;
770 /* This relocation describes which C++ vtable entries are actually
771 used. Record for later use during GC. */
772 case R_68K_GNU_VTENTRY:
773 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
774 return false;
775 break;
777 default:
778 break;
782 return true;
785 /* Return the section that should be marked against GC for a given
786 relocation. */
788 static asection *
789 elf_m68k_gc_mark_hook (abfd, info, rel, h, sym)
790 bfd *abfd;
791 struct bfd_link_info *info ATTRIBUTE_UNUSED;
792 Elf_Internal_Rela *rel;
793 struct elf_link_hash_entry *h;
794 Elf_Internal_Sym *sym;
796 if (h != NULL)
798 switch (ELF32_R_TYPE (rel->r_info))
800 case R_68K_GNU_VTINHERIT:
801 case R_68K_GNU_VTENTRY:
802 break;
804 default:
805 switch (h->root.type)
807 default:
808 break;
810 case bfd_link_hash_defined:
811 case bfd_link_hash_defweak:
812 return h->root.u.def.section;
814 case bfd_link_hash_common:
815 return h->root.u.c.p->section;
819 else
821 if (!(elf_bad_symtab (abfd)
822 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
823 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
824 && sym->st_shndx != SHN_COMMON))
826 return bfd_section_from_elf_index (abfd, sym->st_shndx);
830 return NULL;
833 /* Update the got entry reference counts for the section being removed. */
835 static boolean
836 elf_m68k_gc_sweep_hook (abfd, info, sec, relocs)
837 bfd *abfd;
838 struct bfd_link_info *info;
839 asection *sec;
840 const Elf_Internal_Rela *relocs;
842 Elf_Internal_Shdr *symtab_hdr;
843 struct elf_link_hash_entry **sym_hashes;
844 bfd_signed_vma *local_got_refcounts;
845 const Elf_Internal_Rela *rel, *relend;
846 unsigned long r_symndx;
847 struct elf_link_hash_entry *h;
848 bfd *dynobj;
849 asection *sgot;
850 asection *srelgot;
852 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
853 sym_hashes = elf_sym_hashes (abfd);
854 local_got_refcounts = elf_local_got_refcounts (abfd);
856 dynobj = elf_hash_table (info)->dynobj;
857 if (dynobj == NULL)
858 return true;
860 sgot = bfd_get_section_by_name (dynobj, ".got");
861 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
863 relend = relocs + sec->reloc_count;
864 for (rel = relocs; rel < relend; rel++)
866 switch (ELF32_R_TYPE (rel->r_info))
868 case R_68K_GOT8:
869 case R_68K_GOT16:
870 case R_68K_GOT32:
871 case R_68K_GOT8O:
872 case R_68K_GOT16O:
873 case R_68K_GOT32O:
874 r_symndx = ELF32_R_SYM (rel->r_info);
875 if (r_symndx >= symtab_hdr->sh_info)
877 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
878 if (h->got.refcount > 0)
880 --h->got.refcount;
881 if (h->got.refcount == 0)
883 /* We don't need the .got entry any more. */
884 sgot->_raw_size -= 4;
885 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
889 else if (local_got_refcounts != NULL)
891 if (local_got_refcounts[r_symndx] > 0)
893 --local_got_refcounts[r_symndx];
894 if (local_got_refcounts[r_symndx] == 0)
896 /* We don't need the .got entry any more. */
897 sgot->_raw_size -= 4;
898 if (info->shared)
899 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
903 break;
905 case R_68K_PLT8:
906 case R_68K_PLT16:
907 case R_68K_PLT32:
908 case R_68K_PLT8O:
909 case R_68K_PLT16O:
910 case R_68K_PLT32O:
911 case R_68K_PC8:
912 case R_68K_PC16:
913 case R_68K_PC32:
914 case R_68K_8:
915 case R_68K_16:
916 case R_68K_32:
917 r_symndx = ELF32_R_SYM (rel->r_info);
918 if (r_symndx >= symtab_hdr->sh_info)
920 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
921 if (h->plt.refcount > 0)
922 --h->plt.refcount;
924 break;
926 default:
927 break;
931 return true;
934 /* Adjust a symbol defined by a dynamic object and referenced by a
935 regular object. The current definition is in some section of the
936 dynamic object, but we're not including those sections. We have to
937 change the definition to something the rest of the link can
938 understand. */
940 static boolean
941 elf_m68k_adjust_dynamic_symbol (info, h)
942 struct bfd_link_info *info;
943 struct elf_link_hash_entry *h;
945 bfd *dynobj;
946 asection *s;
947 unsigned int power_of_two;
949 dynobj = elf_hash_table (info)->dynobj;
951 /* Make sure we know what is going on here. */
952 BFD_ASSERT (dynobj != NULL
953 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
954 || h->weakdef != NULL
955 || ((h->elf_link_hash_flags
956 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
957 && (h->elf_link_hash_flags
958 & ELF_LINK_HASH_REF_REGULAR) != 0
959 && (h->elf_link_hash_flags
960 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
962 /* If this is a function, put it in the procedure linkage table. We
963 will fill in the contents of the procedure linkage table later,
964 when we know the address of the .got section. */
965 if (h->type == STT_FUNC
966 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
968 if (! info->shared
969 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
970 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
971 /* We must always create the plt entry if it was referenced
972 by a PLTxxO relocation. In this case we already recorded
973 it as a dynamic symbol. */
974 && h->dynindx == -1)
976 /* This case can occur if we saw a PLTxx reloc in an input
977 file, but the symbol was never referred to by a dynamic
978 object. In such a case, we don't actually need to build
979 a procedure linkage table, and we can just do a PCxx
980 reloc instead. */
981 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
982 h->plt.offset = (bfd_vma) -1;
983 return true;
986 /* GC may have rendered this entry unused. */
987 if (h->plt.refcount <= 0)
989 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
990 h->plt.offset = (bfd_vma) -1;
991 return true;
994 /* Make sure this symbol is output as a dynamic symbol. */
995 if (h->dynindx == -1)
997 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
998 return false;
1001 s = bfd_get_section_by_name (dynobj, ".plt");
1002 BFD_ASSERT (s != NULL);
1004 /* If this is the first .plt entry, make room for the special
1005 first entry. */
1006 if (s->_raw_size == 0)
1008 if (CPU32_FLAG (dynobj))
1009 s->_raw_size += PLT_CPU32_ENTRY_SIZE;
1010 else
1011 s->_raw_size += PLT_ENTRY_SIZE;
1014 /* If this symbol is not defined in a regular file, and we are
1015 not generating a shared library, then set the symbol to this
1016 location in the .plt. This is required to make function
1017 pointers compare as equal between the normal executable and
1018 the shared library. */
1019 if (!info->shared
1020 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1022 h->root.u.def.section = s;
1023 h->root.u.def.value = s->_raw_size;
1026 h->plt.offset = s->_raw_size;
1028 /* Make room for this entry. */
1029 if (CPU32_FLAG (dynobj))
1030 s->_raw_size += PLT_CPU32_ENTRY_SIZE;
1031 else
1032 s->_raw_size += PLT_ENTRY_SIZE;
1034 /* We also need to make an entry in the .got.plt section, which
1035 will be placed in the .got section by the linker script. */
1037 s = bfd_get_section_by_name (dynobj, ".got.plt");
1038 BFD_ASSERT (s != NULL);
1039 s->_raw_size += 4;
1041 /* We also need to make an entry in the .rela.plt section. */
1043 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1044 BFD_ASSERT (s != NULL);
1045 s->_raw_size += sizeof (Elf32_External_Rela);
1047 return true;
1050 /* Reinitialize the plt offset now that it is not used as a reference
1051 count any more. */
1052 h->plt.offset = (bfd_vma) -1;
1054 /* If this is a weak symbol, and there is a real definition, the
1055 processor independent code will have arranged for us to see the
1056 real definition first, and we can just use the same value. */
1057 if (h->weakdef != NULL)
1059 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1060 || h->weakdef->root.type == bfd_link_hash_defweak);
1061 h->root.u.def.section = h->weakdef->root.u.def.section;
1062 h->root.u.def.value = h->weakdef->root.u.def.value;
1063 return true;
1066 /* This is a reference to a symbol defined by a dynamic object which
1067 is not a function. */
1069 /* If we are creating a shared library, we must presume that the
1070 only references to the symbol are via the global offset table.
1071 For such cases we need not do anything here; the relocations will
1072 be handled correctly by relocate_section. */
1073 if (info->shared)
1074 return true;
1076 /* We must allocate the symbol in our .dynbss section, which will
1077 become part of the .bss section of the executable. There will be
1078 an entry for this symbol in the .dynsym section. The dynamic
1079 object will contain position independent code, so all references
1080 from the dynamic object to this symbol will go through the global
1081 offset table. The dynamic linker will use the .dynsym entry to
1082 determine the address it must put in the global offset table, so
1083 both the dynamic object and the regular object will refer to the
1084 same memory location for the variable. */
1086 s = bfd_get_section_by_name (dynobj, ".dynbss");
1087 BFD_ASSERT (s != NULL);
1089 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1090 copy the initial value out of the dynamic object and into the
1091 runtime process image. We need to remember the offset into the
1092 .rela.bss section we are going to use. */
1093 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1095 asection *srel;
1097 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1098 BFD_ASSERT (srel != NULL);
1099 srel->_raw_size += sizeof (Elf32_External_Rela);
1100 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1103 /* We need to figure out the alignment required for this symbol. I
1104 have no idea how ELF linkers handle this. */
1105 power_of_two = bfd_log2 (h->size);
1106 if (power_of_two > 3)
1107 power_of_two = 3;
1109 /* Apply the required alignment. */
1110 s->_raw_size = BFD_ALIGN (s->_raw_size,
1111 (bfd_size_type) (1 << power_of_two));
1112 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1114 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
1115 return false;
1118 /* Define the symbol as being at this point in the section. */
1119 h->root.u.def.section = s;
1120 h->root.u.def.value = s->_raw_size;
1122 /* Increment the section size to make room for the symbol. */
1123 s->_raw_size += h->size;
1125 return true;
1128 /* Set the sizes of the dynamic sections. */
1130 static boolean
1131 elf_m68k_size_dynamic_sections (output_bfd, info)
1132 bfd *output_bfd;
1133 struct bfd_link_info *info;
1135 bfd *dynobj;
1136 asection *s;
1137 boolean plt;
1138 boolean relocs;
1139 boolean reltext;
1141 dynobj = elf_hash_table (info)->dynobj;
1142 BFD_ASSERT (dynobj != NULL);
1144 if (elf_hash_table (info)->dynamic_sections_created)
1146 /* Set the contents of the .interp section to the interpreter. */
1147 if (!info->shared)
1149 s = bfd_get_section_by_name (dynobj, ".interp");
1150 BFD_ASSERT (s != NULL);
1151 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1152 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1155 else
1157 /* We may have created entries in the .rela.got section.
1158 However, if we are not creating the dynamic sections, we will
1159 not actually use these entries. Reset the size of .rela.got,
1160 which will cause it to get stripped from the output file
1161 below. */
1162 s = bfd_get_section_by_name (dynobj, ".rela.got");
1163 if (s != NULL)
1164 s->_raw_size = 0;
1167 /* If this is a -Bsymbolic shared link, then we need to discard all PC
1168 relative relocs against symbols defined in a regular object. We
1169 allocated space for them in the check_relocs routine, but we will not
1170 fill them in in the relocate_section routine. */
1171 if (info->shared && info->symbolic)
1172 elf_m68k_link_hash_traverse (elf_m68k_hash_table (info),
1173 elf_m68k_discard_copies,
1174 (PTR) NULL);
1176 /* The check_relocs and adjust_dynamic_symbol entry points have
1177 determined the sizes of the various dynamic sections. Allocate
1178 memory for them. */
1179 plt = false;
1180 relocs = false;
1181 reltext = false;
1182 for (s = dynobj->sections; s != NULL; s = s->next)
1184 const char *name;
1185 boolean strip;
1187 if ((s->flags & SEC_LINKER_CREATED) == 0)
1188 continue;
1190 /* It's OK to base decisions on the section name, because none
1191 of the dynobj section names depend upon the input files. */
1192 name = bfd_get_section_name (dynobj, s);
1194 strip = false;
1196 if (strcmp (name, ".plt") == 0)
1198 if (s->_raw_size == 0)
1200 /* Strip this section if we don't need it; see the
1201 comment below. */
1202 strip = true;
1204 else
1206 /* Remember whether there is a PLT. */
1207 plt = true;
1210 else if (strncmp (name, ".rela", 5) == 0)
1212 if (s->_raw_size == 0)
1214 /* If we don't need this section, strip it from the
1215 output file. This is mostly to handle .rela.bss and
1216 .rela.plt. We must create both sections in
1217 create_dynamic_sections, because they must be created
1218 before the linker maps input sections to output
1219 sections. The linker does that before
1220 adjust_dynamic_symbol is called, and it is that
1221 function which decides whether anything needs to go
1222 into these sections. */
1223 strip = true;
1225 else
1227 asection *target;
1229 /* Remember whether there are any reloc sections other
1230 than .rela.plt. */
1231 if (strcmp (name, ".rela.plt") != 0)
1233 const char *outname;
1235 relocs = true;
1237 /* If this relocation section applies to a read only
1238 section, then we probably need a DT_TEXTREL
1239 entry. .rela.plt is actually associated with
1240 .got.plt, which is never readonly. */
1241 outname = bfd_get_section_name (output_bfd,
1242 s->output_section);
1243 target = bfd_get_section_by_name (output_bfd, outname + 5);
1244 if (target != NULL
1245 && (target->flags & SEC_READONLY) != 0
1246 && (target->flags & SEC_ALLOC) != 0)
1247 reltext = true;
1250 /* We use the reloc_count field as a counter if we need
1251 to copy relocs into the output file. */
1252 s->reloc_count = 0;
1255 else if (strncmp (name, ".got", 4) != 0)
1257 /* It's not one of our sections, so don't allocate space. */
1258 continue;
1261 if (strip)
1263 _bfd_strip_section_from_output (info, s);
1264 continue;
1267 /* Allocate memory for the section contents. */
1268 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1269 Unused entries should be reclaimed before the section's contents
1270 are written out, but at the moment this does not happen. Thus in
1271 order to prevent writing out garbage, we initialise the section's
1272 contents to zero. */
1273 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1274 if (s->contents == NULL && s->_raw_size != 0)
1275 return false;
1278 if (elf_hash_table (info)->dynamic_sections_created)
1280 /* Add some entries to the .dynamic section. We fill in the
1281 values later, in elf_m68k_finish_dynamic_sections, but we
1282 must add the entries now so that we get the correct size for
1283 the .dynamic section. The DT_DEBUG entry is filled in by the
1284 dynamic linker and used by the debugger. */
1285 if (!info->shared)
1287 if (!bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
1288 return false;
1291 if (plt)
1293 if (!bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
1294 || !bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1295 || !bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1296 || !bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
1297 return false;
1300 if (relocs)
1302 if (!bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
1303 || !bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
1304 || !bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
1305 sizeof (Elf32_External_Rela)))
1306 return false;
1309 if (reltext)
1311 if (!bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
1312 return false;
1313 info->flags |= DF_TEXTREL;
1317 return true;
1320 /* This function is called via elf_m68k_link_hash_traverse if we are
1321 creating a shared object with -Bsymbolic. It discards the space
1322 allocated to copy PC relative relocs against symbols which are defined
1323 in regular objects. We allocated space for them in the check_relocs
1324 routine, but we won't fill them in in the relocate_section routine. */
1326 static boolean
1327 elf_m68k_discard_copies (h, ignore)
1328 struct elf_m68k_link_hash_entry *h;
1329 PTR ignore ATTRIBUTE_UNUSED;
1331 struct elf_m68k_pcrel_relocs_copied *s;
1333 /* We only discard relocs for symbols defined in a regular object. */
1334 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1335 return true;
1337 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1338 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
1340 return true;
1343 /* Relocate an M68K ELF section. */
1345 static boolean
1346 elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1347 contents, relocs, local_syms, local_sections)
1348 bfd *output_bfd;
1349 struct bfd_link_info *info;
1350 bfd *input_bfd;
1351 asection *input_section;
1352 bfd_byte *contents;
1353 Elf_Internal_Rela *relocs;
1354 Elf_Internal_Sym *local_syms;
1355 asection **local_sections;
1357 bfd *dynobj;
1358 Elf_Internal_Shdr *symtab_hdr;
1359 struct elf_link_hash_entry **sym_hashes;
1360 bfd_vma *local_got_offsets;
1361 asection *sgot;
1362 asection *splt;
1363 asection *sreloc;
1364 Elf_Internal_Rela *rel;
1365 Elf_Internal_Rela *relend;
1367 dynobj = elf_hash_table (info)->dynobj;
1368 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1369 sym_hashes = elf_sym_hashes (input_bfd);
1370 local_got_offsets = elf_local_got_offsets (input_bfd);
1372 sgot = NULL;
1373 splt = NULL;
1374 sreloc = NULL;
1376 rel = relocs;
1377 relend = relocs + input_section->reloc_count;
1378 for (; rel < relend; rel++)
1380 int r_type;
1381 reloc_howto_type *howto;
1382 unsigned long r_symndx;
1383 struct elf_link_hash_entry *h;
1384 Elf_Internal_Sym *sym;
1385 asection *sec;
1386 bfd_vma relocation;
1387 bfd_reloc_status_type r;
1389 r_type = ELF32_R_TYPE (rel->r_info);
1390 if (r_type < 0 || r_type >= (int) R_68K_max)
1392 bfd_set_error (bfd_error_bad_value);
1393 return false;
1395 howto = howto_table + r_type;
1397 r_symndx = ELF32_R_SYM (rel->r_info);
1399 if (info->relocateable)
1401 /* This is a relocateable link. We don't have to change
1402 anything, unless the reloc is against a section symbol,
1403 in which case we have to adjust according to where the
1404 section symbol winds up in the output section. */
1405 if (r_symndx < symtab_hdr->sh_info)
1407 sym = local_syms + r_symndx;
1408 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1410 sec = local_sections[r_symndx];
1411 rel->r_addend += sec->output_offset + sym->st_value;
1415 continue;
1418 /* This is a final link. */
1419 h = NULL;
1420 sym = NULL;
1421 sec = NULL;
1422 if (r_symndx < symtab_hdr->sh_info)
1424 sym = local_syms + r_symndx;
1425 sec = local_sections[r_symndx];
1426 relocation = (sec->output_section->vma
1427 + sec->output_offset
1428 + sym->st_value);
1430 else
1432 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1433 while (h->root.type == bfd_link_hash_indirect
1434 || h->root.type == bfd_link_hash_warning)
1435 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1436 if (h->root.type == bfd_link_hash_defined
1437 || h->root.type == bfd_link_hash_defweak)
1439 sec = h->root.u.def.section;
1440 if (((r_type == R_68K_PLT8
1441 || r_type == R_68K_PLT16
1442 || r_type == R_68K_PLT32
1443 || r_type == R_68K_PLT8O
1444 || r_type == R_68K_PLT16O
1445 || r_type == R_68K_PLT32O)
1446 && h->plt.offset != (bfd_vma) -1
1447 && elf_hash_table (info)->dynamic_sections_created)
1448 || ((r_type == R_68K_GOT8O
1449 || r_type == R_68K_GOT16O
1450 || r_type == R_68K_GOT32O
1451 || ((r_type == R_68K_GOT8
1452 || r_type == R_68K_GOT16
1453 || r_type == R_68K_GOT32)
1454 && strcmp (h->root.root.string,
1455 "_GLOBAL_OFFSET_TABLE_") != 0))
1456 && elf_hash_table (info)->dynamic_sections_created
1457 && (! info->shared
1458 || (! info->symbolic && h->dynindx != -1)
1459 || (h->elf_link_hash_flags
1460 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1461 || (info->shared
1462 && ((! info->symbolic && h->dynindx != -1)
1463 || (h->elf_link_hash_flags
1464 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1465 && ((input_section->flags & SEC_ALLOC) != 0
1466 /* DWARF will emit R_68K_32 relocations in its
1467 sections against symbols defined externally
1468 in shared libraries. We can't do anything
1469 with them here. */
1470 || ((input_section->flags & SEC_DEBUGGING) != 0
1471 && (h->elf_link_hash_flags
1472 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
1473 && (r_type == R_68K_8
1474 || r_type == R_68K_16
1475 || r_type == R_68K_32
1476 || r_type == R_68K_PC8
1477 || r_type == R_68K_PC16
1478 || r_type == R_68K_PC32)))
1480 /* In these cases, we don't need the relocation
1481 value. We check specially because in some
1482 obscure cases sec->output_section will be NULL. */
1483 relocation = 0;
1485 else
1486 relocation = (h->root.u.def.value
1487 + sec->output_section->vma
1488 + sec->output_offset);
1490 else if (h->root.type == bfd_link_hash_undefweak)
1491 relocation = 0;
1492 else if (info->shared && !info->symbolic
1493 && !info->no_undefined
1494 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1495 relocation = 0;
1496 else
1498 if (!(info->callbacks->undefined_symbol
1499 (info, h->root.root.string, input_bfd,
1500 input_section, rel->r_offset,
1501 (!info->shared || info->no_undefined
1502 || ELF_ST_VISIBILITY (h->other)))))
1503 return false;
1504 relocation = 0;
1508 switch (r_type)
1510 case R_68K_GOT8:
1511 case R_68K_GOT16:
1512 case R_68K_GOT32:
1513 /* Relocation is to the address of the entry for this symbol
1514 in the global offset table. */
1515 if (h != NULL
1516 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1517 break;
1518 /* Fall through. */
1519 case R_68K_GOT8O:
1520 case R_68K_GOT16O:
1521 case R_68K_GOT32O:
1522 /* Relocation is the offset of the entry for this symbol in
1523 the global offset table. */
1526 bfd_vma off;
1528 if (sgot == NULL)
1530 sgot = bfd_get_section_by_name (dynobj, ".got");
1531 BFD_ASSERT (sgot != NULL);
1534 if (h != NULL)
1536 off = h->got.offset;
1537 BFD_ASSERT (off != (bfd_vma) -1);
1539 if (!elf_hash_table (info)->dynamic_sections_created
1540 || (info->shared
1541 && (info->symbolic || h->dynindx == -1)
1542 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1544 /* This is actually a static link, or it is a
1545 -Bsymbolic link and the symbol is defined
1546 locally, or the symbol was forced to be local
1547 because of a version file.. We must initialize
1548 this entry in the global offset table. Since
1549 the offset must always be a multiple of 4, we
1550 use the least significant bit to record whether
1551 we have initialized it already.
1553 When doing a dynamic link, we create a .rela.got
1554 relocation entry to initialize the value. This
1555 is done in the finish_dynamic_symbol routine. */
1556 if ((off & 1) != 0)
1557 off &= ~1;
1558 else
1560 bfd_put_32 (output_bfd, relocation,
1561 sgot->contents + off);
1562 h->got.offset |= 1;
1566 else
1568 BFD_ASSERT (local_got_offsets != NULL
1569 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1571 off = local_got_offsets[r_symndx];
1573 /* The offset must always be a multiple of 4. We use
1574 the least significant bit to record whether we have
1575 already generated the necessary reloc. */
1576 if ((off & 1) != 0)
1577 off &= ~1;
1578 else
1580 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1582 if (info->shared)
1584 asection *srelgot;
1585 Elf_Internal_Rela outrel;
1587 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1588 BFD_ASSERT (srelgot != NULL);
1590 outrel.r_offset = (sgot->output_section->vma
1591 + sgot->output_offset
1592 + off);
1593 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1594 outrel.r_addend = relocation;
1595 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1596 (((Elf32_External_Rela *)
1597 srelgot->contents)
1598 + srelgot->reloc_count));
1599 ++srelgot->reloc_count;
1602 local_got_offsets[r_symndx] |= 1;
1606 relocation = sgot->output_offset + off;
1607 if (r_type == R_68K_GOT8O
1608 || r_type == R_68K_GOT16O
1609 || r_type == R_68K_GOT32O)
1611 /* This relocation does not use the addend. */
1612 rel->r_addend = 0;
1614 else
1615 relocation += sgot->output_section->vma;
1617 break;
1619 case R_68K_PLT8:
1620 case R_68K_PLT16:
1621 case R_68K_PLT32:
1622 /* Relocation is to the entry for this symbol in the
1623 procedure linkage table. */
1625 /* Resolve a PLTxx reloc against a local symbol directly,
1626 without using the procedure linkage table. */
1627 if (h == NULL)
1628 break;
1630 if (h->plt.offset == (bfd_vma) -1
1631 || !elf_hash_table (info)->dynamic_sections_created)
1633 /* We didn't make a PLT entry for this symbol. This
1634 happens when statically linking PIC code, or when
1635 using -Bsymbolic. */
1636 break;
1639 if (splt == NULL)
1641 splt = bfd_get_section_by_name (dynobj, ".plt");
1642 BFD_ASSERT (splt != NULL);
1645 relocation = (splt->output_section->vma
1646 + splt->output_offset
1647 + h->plt.offset);
1648 break;
1650 case R_68K_PLT8O:
1651 case R_68K_PLT16O:
1652 case R_68K_PLT32O:
1653 /* Relocation is the offset of the entry for this symbol in
1654 the procedure linkage table. */
1655 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1657 if (splt == NULL)
1659 splt = bfd_get_section_by_name (dynobj, ".plt");
1660 BFD_ASSERT (splt != NULL);
1663 relocation = h->plt.offset;
1665 /* This relocation does not use the addend. */
1666 rel->r_addend = 0;
1668 break;
1670 case R_68K_PC8:
1671 case R_68K_PC16:
1672 case R_68K_PC32:
1673 if (h == NULL)
1674 break;
1675 /* Fall through. */
1676 case R_68K_8:
1677 case R_68K_16:
1678 case R_68K_32:
1679 if (info->shared
1680 && (input_section->flags & SEC_ALLOC) != 0
1681 && ((r_type != R_68K_PC8
1682 && r_type != R_68K_PC16
1683 && r_type != R_68K_PC32)
1684 || (!info->symbolic
1685 || (h->elf_link_hash_flags
1686 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1688 Elf_Internal_Rela outrel;
1689 boolean skip, relocate;
1691 /* When generating a shared object, these relocations
1692 are copied into the output file to be resolved at run
1693 time. */
1695 if (sreloc == NULL)
1697 const char *name;
1699 name = (bfd_elf_string_from_elf_section
1700 (input_bfd,
1701 elf_elfheader (input_bfd)->e_shstrndx,
1702 elf_section_data (input_section)->rel_hdr.sh_name));
1703 if (name == NULL)
1704 return false;
1706 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1707 && strcmp (bfd_get_section_name (input_bfd,
1708 input_section),
1709 name + 5) == 0);
1711 sreloc = bfd_get_section_by_name (dynobj, name);
1712 BFD_ASSERT (sreloc != NULL);
1715 skip = false;
1717 if (elf_section_data (input_section)->stab_info == NULL)
1718 outrel.r_offset = rel->r_offset;
1719 else
1721 bfd_vma off;
1723 off = (_bfd_stab_section_offset
1724 (output_bfd, &elf_hash_table (info)->stab_info,
1725 input_section,
1726 &elf_section_data (input_section)->stab_info,
1727 rel->r_offset));
1728 if (off == (bfd_vma) -1)
1729 skip = true;
1730 outrel.r_offset = off;
1733 outrel.r_offset += (input_section->output_section->vma
1734 + input_section->output_offset);
1736 if (skip)
1738 memset (&outrel, 0, sizeof outrel);
1739 relocate = false;
1741 /* h->dynindx may be -1 if the symbol was marked to
1742 become local. */
1743 else if (h != NULL
1744 && ((! info->symbolic && h->dynindx != -1)
1745 || (h->elf_link_hash_flags
1746 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1748 BFD_ASSERT (h->dynindx != -1);
1749 relocate = false;
1750 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1751 outrel.r_addend = relocation + rel->r_addend;
1753 else
1755 if (r_type == R_68K_32)
1757 relocate = true;
1758 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1759 outrel.r_addend = relocation + rel->r_addend;
1761 else
1763 long indx;
1765 if (h == NULL)
1766 sec = local_sections[r_symndx];
1767 else
1769 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1770 || (h->root.type
1771 == bfd_link_hash_defweak));
1772 sec = h->root.u.def.section;
1774 if (sec != NULL && bfd_is_abs_section (sec))
1775 indx = 0;
1776 else if (sec == NULL || sec->owner == NULL)
1778 bfd_set_error (bfd_error_bad_value);
1779 return false;
1781 else
1783 asection *osec;
1785 osec = sec->output_section;
1786 indx = elf_section_data (osec)->dynindx;
1787 BFD_ASSERT (indx > 0);
1790 relocate = false;
1791 outrel.r_info = ELF32_R_INFO (indx, r_type);
1792 outrel.r_addend = relocation + rel->r_addend;
1796 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1797 (((Elf32_External_Rela *)
1798 sreloc->contents)
1799 + sreloc->reloc_count));
1800 ++sreloc->reloc_count;
1802 /* This reloc will be computed at runtime, so there's no
1803 need to do anything now, except for R_68K_32
1804 relocations that have been turned into
1805 R_68K_RELATIVE. */
1806 if (!relocate)
1807 continue;
1810 break;
1812 case R_68K_GNU_VTINHERIT:
1813 case R_68K_GNU_VTENTRY:
1814 /* These are no-ops in the end. */
1815 continue;
1817 default:
1818 break;
1821 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1822 contents, rel->r_offset,
1823 relocation, rel->r_addend);
1825 if (r != bfd_reloc_ok)
1827 switch (r)
1829 default:
1830 case bfd_reloc_outofrange:
1831 abort ();
1832 case bfd_reloc_overflow:
1834 const char *name;
1836 if (h != NULL)
1837 name = h->root.root.string;
1838 else
1840 name = bfd_elf_string_from_elf_section (input_bfd,
1841 symtab_hdr->sh_link,
1842 sym->st_name);
1843 if (name == NULL)
1844 return false;
1845 if (*name == '\0')
1846 name = bfd_section_name (input_bfd, sec);
1848 if (!(info->callbacks->reloc_overflow
1849 (info, name, howto->name, (bfd_vma) 0,
1850 input_bfd, input_section, rel->r_offset)))
1851 return false;
1853 break;
1858 return true;
1861 /* Finish up dynamic symbol handling. We set the contents of various
1862 dynamic sections here. */
1864 static boolean
1865 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
1866 bfd *output_bfd;
1867 struct bfd_link_info *info;
1868 struct elf_link_hash_entry *h;
1869 Elf_Internal_Sym *sym;
1871 bfd *dynobj;
1872 int plt_off1, plt_off2, plt_off3;
1874 dynobj = elf_hash_table (info)->dynobj;
1876 if (h->plt.offset != (bfd_vma) -1)
1878 asection *splt;
1879 asection *sgot;
1880 asection *srela;
1881 bfd_vma plt_index;
1882 bfd_vma got_offset;
1883 Elf_Internal_Rela rela;
1885 /* This symbol has an entry in the procedure linkage table. Set
1886 it up. */
1888 BFD_ASSERT (h->dynindx != -1);
1890 splt = bfd_get_section_by_name (dynobj, ".plt");
1891 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1892 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1893 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1895 /* Get the index in the procedure linkage table which
1896 corresponds to this symbol. This is the index of this symbol
1897 in all the symbols for which we are making plt entries. The
1898 first entry in the procedure linkage table is reserved. */
1899 if ( CPU32_FLAG (output_bfd))
1900 plt_index = h->plt.offset / PLT_CPU32_ENTRY_SIZE - 1;
1901 else
1902 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1904 /* Get the offset into the .got table of the entry that
1905 corresponds to this function. Each .got entry is 4 bytes.
1906 The first three are reserved. */
1907 got_offset = (plt_index + 3) * 4;
1909 if ( CPU32_FLAG (output_bfd))
1911 /* Fill in the entry in the procedure linkage table. */
1912 memcpy (splt->contents + h->plt.offset, elf_cpu32_plt_entry,
1913 PLT_CPU32_ENTRY_SIZE);
1914 plt_off1 = 4;
1915 plt_off2 = 12;
1916 plt_off3 = 18;
1918 else
1920 /* Fill in the entry in the procedure linkage table. */
1921 memcpy (splt->contents + h->plt.offset, elf_m68k_plt_entry,
1922 PLT_ENTRY_SIZE);
1923 plt_off1 = 4;
1924 plt_off2 = 10;
1925 plt_off3 = 16;
1928 /* The offset is relative to the first extension word. */
1929 bfd_put_32 (output_bfd,
1930 (sgot->output_section->vma
1931 + sgot->output_offset
1932 + got_offset
1933 - (splt->output_section->vma
1934 + h->plt.offset + 2)),
1935 splt->contents + h->plt.offset + plt_off1);
1937 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
1938 splt->contents + h->plt.offset + plt_off2);
1939 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3),
1940 splt->contents + h->plt.offset + plt_off3);
1942 /* Fill in the entry in the global offset table. */
1943 bfd_put_32 (output_bfd,
1944 (splt->output_section->vma
1945 + splt->output_offset
1946 + h->plt.offset
1947 + 8),
1948 sgot->contents + got_offset);
1950 /* Fill in the entry in the .rela.plt section. */
1951 rela.r_offset = (sgot->output_section->vma
1952 + sgot->output_offset
1953 + got_offset);
1954 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
1955 rela.r_addend = 0;
1956 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1957 ((Elf32_External_Rela *) srela->contents
1958 + plt_index));
1960 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1962 /* Mark the symbol as undefined, rather than as defined in
1963 the .plt section. Leave the value alone. */
1964 sym->st_shndx = SHN_UNDEF;
1968 if (h->got.offset != (bfd_vma) -1)
1970 asection *sgot;
1971 asection *srela;
1972 Elf_Internal_Rela rela;
1974 /* This symbol has an entry in the global offset table. Set it
1975 up. */
1977 sgot = bfd_get_section_by_name (dynobj, ".got");
1978 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1979 BFD_ASSERT (sgot != NULL && srela != NULL);
1981 rela.r_offset = (sgot->output_section->vma
1982 + sgot->output_offset
1983 + (h->got.offset &~ 1));
1985 /* If this is a -Bsymbolic link, and the symbol is defined
1986 locally, we just want to emit a RELATIVE reloc. Likewise if
1987 the symbol was forced to be local because of a version file.
1988 The entry in the global offset table will already have been
1989 initialized in the relocate_section function. */
1990 if (info->shared
1991 && (info->symbolic || h->dynindx == -1)
1992 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1994 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1995 rela.r_addend = bfd_get_signed_32 (output_bfd,
1996 (sgot->contents
1997 + (h->got.offset & ~1)));
1999 else
2001 bfd_put_32 (output_bfd, (bfd_vma) 0,
2002 sgot->contents + (h->got.offset & ~1));
2003 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
2004 rela.r_addend = 0;
2007 bfd_elf32_swap_reloca_out (output_bfd, &rela,
2008 ((Elf32_External_Rela *) srela->contents
2009 + srela->reloc_count));
2010 ++srela->reloc_count;
2013 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2015 asection *s;
2016 Elf_Internal_Rela rela;
2018 /* This symbol needs a copy reloc. Set it up. */
2020 BFD_ASSERT (h->dynindx != -1
2021 && (h->root.type == bfd_link_hash_defined
2022 || h->root.type == bfd_link_hash_defweak));
2024 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2025 ".rela.bss");
2026 BFD_ASSERT (s != NULL);
2028 rela.r_offset = (h->root.u.def.value
2029 + h->root.u.def.section->output_section->vma
2030 + h->root.u.def.section->output_offset);
2031 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
2032 rela.r_addend = 0;
2033 bfd_elf32_swap_reloca_out (output_bfd, &rela,
2034 ((Elf32_External_Rela *) s->contents
2035 + s->reloc_count));
2036 ++s->reloc_count;
2039 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2040 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2041 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2042 sym->st_shndx = SHN_ABS;
2044 return true;
2047 /* Finish up the dynamic sections. */
2049 static boolean
2050 elf_m68k_finish_dynamic_sections (output_bfd, info)
2051 bfd *output_bfd;
2052 struct bfd_link_info *info;
2054 bfd *dynobj;
2055 asection *sgot;
2056 asection *sdyn;
2058 dynobj = elf_hash_table (info)->dynobj;
2060 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2061 BFD_ASSERT (sgot != NULL);
2062 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2064 if (elf_hash_table (info)->dynamic_sections_created)
2066 asection *splt;
2067 Elf32_External_Dyn *dyncon, *dynconend;
2069 splt = bfd_get_section_by_name (dynobj, ".plt");
2070 BFD_ASSERT (splt != NULL && sdyn != NULL);
2072 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2073 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2074 for (; dyncon < dynconend; dyncon++)
2076 Elf_Internal_Dyn dyn;
2077 const char *name;
2078 asection *s;
2080 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2082 switch (dyn.d_tag)
2084 default:
2085 break;
2087 case DT_PLTGOT:
2088 name = ".got";
2089 goto get_vma;
2090 case DT_JMPREL:
2091 name = ".rela.plt";
2092 get_vma:
2093 s = bfd_get_section_by_name (output_bfd, name);
2094 BFD_ASSERT (s != NULL);
2095 dyn.d_un.d_ptr = s->vma;
2096 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2097 break;
2099 case DT_PLTRELSZ:
2100 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2101 BFD_ASSERT (s != NULL);
2102 if (s->_cooked_size != 0)
2103 dyn.d_un.d_val = s->_cooked_size;
2104 else
2105 dyn.d_un.d_val = s->_raw_size;
2106 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2107 break;
2109 case DT_RELASZ:
2110 /* The procedure linkage table relocs (DT_JMPREL) should
2111 not be included in the overall relocs (DT_RELA).
2112 Therefore, we override the DT_RELASZ entry here to
2113 make it not include the JMPREL relocs. Since the
2114 linker script arranges for .rela.plt to follow all
2115 other relocation sections, we don't have to worry
2116 about changing the DT_RELA entry. */
2117 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2118 if (s != NULL)
2120 if (s->_cooked_size != 0)
2121 dyn.d_un.d_val -= s->_cooked_size;
2122 else
2123 dyn.d_un.d_val -= s->_raw_size;
2125 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2126 break;
2130 /* Fill in the first entry in the procedure linkage table. */
2131 if (splt->_raw_size > 0)
2133 if (!CPU32_FLAG (output_bfd))
2135 memcpy (splt->contents, elf_m68k_plt0_entry, PLT_ENTRY_SIZE);
2136 bfd_put_32 (output_bfd,
2137 (sgot->output_section->vma
2138 + sgot->output_offset + 4
2139 - (splt->output_section->vma + 2)),
2140 splt->contents + 4);
2141 bfd_put_32 (output_bfd,
2142 (sgot->output_section->vma
2143 + sgot->output_offset + 8
2144 - (splt->output_section->vma + 10)),
2145 splt->contents + 12);
2146 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2147 = PLT_ENTRY_SIZE;
2149 else /* cpu32 */
2151 memcpy (splt->contents, elf_cpu32_plt0_entry, PLT_CPU32_ENTRY_SIZE);
2152 bfd_put_32 (output_bfd,
2153 (sgot->output_section->vma
2154 + sgot->output_offset + 4
2155 - (splt->output_section->vma + 2)),
2156 splt->contents + 4);
2157 bfd_put_32 (output_bfd,
2158 (sgot->output_section->vma
2159 + sgot->output_offset + 8
2160 - (splt->output_section->vma + 10)),
2161 splt->contents + 12);
2162 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2163 = PLT_CPU32_ENTRY_SIZE;
2168 /* Fill in the first three entries in the global offset table. */
2169 if (sgot->_raw_size > 0)
2171 if (sdyn == NULL)
2172 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2173 else
2174 bfd_put_32 (output_bfd,
2175 sdyn->output_section->vma + sdyn->output_offset,
2176 sgot->contents);
2177 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2178 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2181 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2183 return true;
2186 /* Given a .data section and a .emreloc in-memory section, store
2187 relocation information into the .emreloc section which can be
2188 used at runtime to relocate the section. This is called by the
2189 linker when the --embedded-relocs switch is used. This is called
2190 after the add_symbols entry point has been called for all the
2191 objects, and before the final_link entry point is called. */
2193 boolean
2194 bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2195 bfd *abfd;
2196 struct bfd_link_info *info;
2197 asection *datasec;
2198 asection *relsec;
2199 char **errmsg;
2201 Elf_Internal_Shdr *symtab_hdr;
2202 Elf32_External_Sym *extsyms;
2203 Elf32_External_Sym *free_extsyms = NULL;
2204 Elf_Internal_Rela *internal_relocs;
2205 Elf_Internal_Rela *free_relocs = NULL;
2206 Elf_Internal_Rela *irel, *irelend;
2207 bfd_byte *p;
2209 BFD_ASSERT (! info->relocateable);
2211 *errmsg = NULL;
2213 if (datasec->reloc_count == 0)
2214 return true;
2216 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2217 /* Read this BFD's symbols if we haven't done so already, or get the cached
2218 copy if it exists. */
2219 if (symtab_hdr->contents != NULL)
2220 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2221 else
2223 /* Go get them off disk. */
2224 if (info->keep_memory)
2225 extsyms = ((Elf32_External_Sym *)
2226 bfd_alloc (abfd, symtab_hdr->sh_size));
2227 else
2228 extsyms = ((Elf32_External_Sym *)
2229 bfd_malloc (symtab_hdr->sh_size));
2230 if (extsyms == NULL)
2231 goto error_return;
2232 if (! info->keep_memory)
2233 free_extsyms = extsyms;
2234 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2235 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
2236 != symtab_hdr->sh_size))
2237 goto error_return;
2238 if (info->keep_memory)
2239 symtab_hdr->contents = extsyms;
2242 /* Get a copy of the native relocations. */
2243 internal_relocs = (_bfd_elf32_link_read_relocs
2244 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2245 info->keep_memory));
2246 if (internal_relocs == NULL)
2247 goto error_return;
2248 if (! info->keep_memory)
2249 free_relocs = internal_relocs;
2251 relsec->contents = (bfd_byte *) bfd_alloc (abfd, datasec->reloc_count * 12);
2252 if (relsec->contents == NULL)
2253 goto error_return;
2255 p = relsec->contents;
2257 irelend = internal_relocs + datasec->reloc_count;
2258 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2260 asection *targetsec;
2262 /* We are going to write a four byte longword into the runtime
2263 reloc section. The longword will be the address in the data
2264 section which must be relocated. It is followed by the name
2265 of the target section NUL-padded or truncated to 8
2266 characters. */
2268 /* We can only relocate absolute longword relocs at run time. */
2269 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2271 *errmsg = _("unsupported reloc type");
2272 bfd_set_error (bfd_error_bad_value);
2273 goto error_return;
2276 /* Get the target section referred to by the reloc. */
2277 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2279 Elf_Internal_Sym isym;
2281 /* A local symbol. */
2282 bfd_elf32_swap_symbol_in (abfd,
2283 extsyms + ELF32_R_SYM (irel->r_info),
2284 &isym);
2286 targetsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
2288 else
2290 unsigned long indx;
2291 struct elf_link_hash_entry *h;
2293 /* An external symbol. */
2294 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2295 h = elf_sym_hashes (abfd)[indx];
2296 BFD_ASSERT (h != NULL);
2297 if (h->root.type == bfd_link_hash_defined
2298 || h->root.type == bfd_link_hash_defweak)
2299 targetsec = h->root.u.def.section;
2300 else
2301 targetsec = NULL;
2304 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2305 memset (p + 4, 0, 8);
2306 if (targetsec != NULL)
2307 strncpy (p + 4, targetsec->output_section->name, 8);
2310 if (free_extsyms != NULL)
2311 free (free_extsyms);
2312 if (free_relocs != NULL)
2313 free (free_relocs);
2314 return true;
2316 error_return:
2317 if (free_extsyms != NULL)
2318 free (free_extsyms);
2319 if (free_relocs != NULL)
2320 free (free_relocs);
2321 return false;
2324 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
2325 #define TARGET_BIG_NAME "elf32-m68k"
2326 #define ELF_MACHINE_CODE EM_68K
2327 #define ELF_MAXPAGESIZE 0x2000
2328 #define elf_backend_create_dynamic_sections \
2329 _bfd_elf_create_dynamic_sections
2330 #define bfd_elf32_bfd_link_hash_table_create \
2331 elf_m68k_link_hash_table_create
2332 #define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
2334 #define elf_backend_check_relocs elf_m68k_check_relocs
2335 #define elf_backend_adjust_dynamic_symbol \
2336 elf_m68k_adjust_dynamic_symbol
2337 #define elf_backend_size_dynamic_sections \
2338 elf_m68k_size_dynamic_sections
2339 #define elf_backend_relocate_section elf_m68k_relocate_section
2340 #define elf_backend_finish_dynamic_symbol \
2341 elf_m68k_finish_dynamic_symbol
2342 #define elf_backend_finish_dynamic_sections \
2343 elf_m68k_finish_dynamic_sections
2344 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
2345 #define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
2346 #define bfd_elf32_bfd_copy_private_bfd_data \
2347 elf32_m68k_copy_private_bfd_data
2348 #define bfd_elf32_bfd_merge_private_bfd_data \
2349 elf32_m68k_merge_private_bfd_data
2350 #define bfd_elf32_bfd_set_private_flags \
2351 elf32_m68k_set_private_flags
2352 #define bfd_elf32_bfd_print_private_bfd_data \
2353 elf32_m68k_print_private_bfd_data
2355 #define elf_backend_can_gc_sections 1
2356 #define elf_backend_want_got_plt 1
2357 #define elf_backend_plt_readonly 1
2358 #define elf_backend_want_plt_sym 0
2359 #define elf_backend_got_header_size 12
2361 #include "elf32-target.h"