* elf32-m68k.c (elf32_m68k_check_relocs): Set DF_TEXTREL if the
[binutils.git] / bfd / elf32-m68k.c
blob6dc5f5838ee7a860f89c13c0f715e5fbe3877997
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 ((int));
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;
344 ret = ((struct elf_m68k_link_hash_table *)
345 bfd_alloc (abfd, sizeof (struct elf_m68k_link_hash_table)));
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 == -1)
539 h->got.refcount = 1;
541 /* Make sure this symbol is output as a dynamic symbol. */
542 if (h->dynindx == -1)
544 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
545 return false;
548 /* Allocate space in the .got section. */
549 sgot->_raw_size += 4;
550 /* Allocate relocation space. */
551 srelgot->_raw_size += sizeof (Elf32_External_Rela);
553 else
554 h->got.refcount++;
556 else
558 /* This is a global offset table entry for a local symbol. */
559 if (local_got_refcounts == NULL)
561 size_t size;
563 size = symtab_hdr->sh_info * sizeof (bfd_signed_vma);
564 local_got_refcounts = ((bfd_signed_vma *)
565 bfd_alloc (abfd, size));
566 if (local_got_refcounts == NULL)
567 return false;
568 elf_local_got_refcounts (abfd) = local_got_refcounts;
569 memset (local_got_refcounts, -1, size);
571 if (local_got_refcounts[r_symndx] == -1)
573 local_got_refcounts[r_symndx] = 1;
575 sgot->_raw_size += 4;
576 if (info->shared)
578 /* If we are generating a shared object, we need to
579 output a R_68K_RELATIVE reloc so that the dynamic
580 linker can adjust this GOT entry. */
581 srelgot->_raw_size += sizeof (Elf32_External_Rela);
584 else
585 local_got_refcounts[r_symndx]++;
587 break;
589 case R_68K_PLT8:
590 case R_68K_PLT16:
591 case R_68K_PLT32:
592 /* This symbol requires a procedure linkage table entry. We
593 actually build the entry in adjust_dynamic_symbol,
594 because this might be a case of linking PIC code which is
595 never referenced by a dynamic object, in which case we
596 don't need to generate a procedure linkage table entry
597 after all. */
599 /* If this is a local symbol, we resolve it directly without
600 creating a procedure linkage table entry. */
601 if (h == NULL)
602 continue;
604 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
605 if (h->plt.refcount == -1)
606 h->plt.refcount = 1;
607 else
608 h->plt.refcount++;
609 break;
611 case R_68K_PLT8O:
612 case R_68K_PLT16O:
613 case R_68K_PLT32O:
614 /* This symbol requires a procedure linkage table entry. */
616 if (h == NULL)
618 /* It does not make sense to have this relocation for a
619 local symbol. FIXME: does it? How to handle it if
620 it does make sense? */
621 bfd_set_error (bfd_error_bad_value);
622 return false;
625 /* Make sure this symbol is output as a dynamic symbol. */
626 if (h->dynindx == -1)
628 if (!bfd_elf32_link_record_dynamic_symbol (info, h))
629 return false;
632 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
633 if (h->plt.refcount == -1)
634 h->plt.refcount = 1;
635 else
636 h->plt.refcount++;
637 break;
639 case R_68K_PC8:
640 case R_68K_PC16:
641 case R_68K_PC32:
642 /* If we are creating a shared library and this is not a local
643 symbol, we need to copy the reloc into the shared library.
644 However when linking with -Bsymbolic and this is a global
645 symbol which is defined in an object we are including in the
646 link (i.e., DEF_REGULAR is set), then we can resolve the
647 reloc directly. At this point we have not seen all the input
648 files, so it is possible that DEF_REGULAR is not set now but
649 will be set later (it is never cleared). We account for that
650 possibility below by storing information in the
651 pcrel_relocs_copied field of the hash table entry. */
652 if (!(info->shared
653 && (sec->flags & SEC_ALLOC) != 0
654 && h != NULL
655 && (!info->symbolic
656 || (h->elf_link_hash_flags
657 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
659 if (h != NULL)
661 /* Make sure a plt entry is created for this symbol if
662 it turns out to be a function defined by a dynamic
663 object. */
664 if (h->plt.refcount == -1)
665 h->plt.refcount = 1;
666 else
667 h->plt.refcount++;
669 break;
671 /* Fall through. */
672 case R_68K_8:
673 case R_68K_16:
674 case R_68K_32:
675 if (h != NULL)
677 /* Make sure a plt entry is created for this symbol if it
678 turns out to be a function defined by a dynamic object. */
679 if (h->plt.refcount == -1)
680 h->plt.refcount = 1;
681 else
682 h->plt.refcount++;
685 /* If we are creating a shared library, we need to copy the
686 reloc into the shared library. */
687 if (info->shared
688 && (sec->flags & SEC_ALLOC) != 0)
690 /* When creating a shared object, we must copy these
691 reloc types into the output file. We create a reloc
692 section in dynobj and make room for this reloc. */
693 if (sreloc == NULL)
695 const char *name;
697 name = (bfd_elf_string_from_elf_section
698 (abfd,
699 elf_elfheader (abfd)->e_shstrndx,
700 elf_section_data (sec)->rel_hdr.sh_name));
701 if (name == NULL)
702 return false;
704 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
705 && strcmp (bfd_get_section_name (abfd, sec),
706 name + 5) == 0);
708 sreloc = bfd_get_section_by_name (dynobj, name);
709 if (sreloc == NULL)
711 sreloc = bfd_make_section (dynobj, name);
712 if (sreloc == NULL
713 || !bfd_set_section_flags (dynobj, sreloc,
714 (SEC_ALLOC
715 | SEC_LOAD
716 | SEC_HAS_CONTENTS
717 | SEC_IN_MEMORY
718 | SEC_LINKER_CREATED
719 | SEC_READONLY))
720 || !bfd_set_section_alignment (dynobj, sreloc, 2))
721 return false;
723 if (sec->flags & SEC_READONLY)
724 info->flags |= DF_TEXTREL;
727 sreloc->_raw_size += sizeof (Elf32_External_Rela);
729 /* If we are linking with -Bsymbolic, we count the number of
730 PC relative relocations we have entered for this symbol,
731 so that we can discard them again if the symbol is later
732 defined by a regular object. Note that this function is
733 only called if we are using an m68kelf linker hash table,
734 which means that h is really a pointer to an
735 elf_m68k_link_hash_entry. */
736 if ((ELF32_R_TYPE (rel->r_info) == R_68K_PC8
737 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
738 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
739 && info->symbolic)
741 struct elf_m68k_link_hash_entry *eh;
742 struct elf_m68k_pcrel_relocs_copied *p;
744 eh = (struct elf_m68k_link_hash_entry *) h;
746 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
747 if (p->section == sreloc)
748 break;
750 if (p == NULL)
752 p = ((struct elf_m68k_pcrel_relocs_copied *)
753 bfd_alloc (dynobj, sizeof *p));
754 if (p == NULL)
755 return false;
756 p->next = eh->pcrel_relocs_copied;
757 eh->pcrel_relocs_copied = p;
758 p->section = sreloc;
759 p->count = 0;
762 ++p->count;
766 break;
768 /* This relocation describes the C++ object vtable hierarchy.
769 Reconstruct it for later use during GC. */
770 case R_68K_GNU_VTINHERIT:
771 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
772 return false;
773 break;
775 /* This relocation describes which C++ vtable entries are actually
776 used. Record for later use during GC. */
777 case R_68K_GNU_VTENTRY:
778 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
779 return false;
780 break;
782 default:
783 break;
787 return true;
790 /* Return the section that should be marked against GC for a given
791 relocation. */
793 static asection *
794 elf_m68k_gc_mark_hook (abfd, info, rel, h, sym)
795 bfd *abfd;
796 struct bfd_link_info *info ATTRIBUTE_UNUSED;
797 Elf_Internal_Rela *rel;
798 struct elf_link_hash_entry *h;
799 Elf_Internal_Sym *sym;
801 if (h != NULL)
803 switch (ELF32_R_TYPE (rel->r_info))
805 case R_68K_GNU_VTINHERIT:
806 case R_68K_GNU_VTENTRY:
807 break;
809 default:
810 switch (h->root.type)
812 default:
813 break;
815 case bfd_link_hash_defined:
816 case bfd_link_hash_defweak:
817 return h->root.u.def.section;
819 case bfd_link_hash_common:
820 return h->root.u.c.p->section;
824 else
826 if (!(elf_bad_symtab (abfd)
827 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
828 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
829 && sym->st_shndx != SHN_COMMON))
831 return bfd_section_from_elf_index (abfd, sym->st_shndx);
835 return NULL;
838 /* Update the got entry reference counts for the section being removed. */
840 static boolean
841 elf_m68k_gc_sweep_hook (abfd, info, sec, relocs)
842 bfd *abfd;
843 struct bfd_link_info *info;
844 asection *sec;
845 const Elf_Internal_Rela *relocs;
847 Elf_Internal_Shdr *symtab_hdr;
848 struct elf_link_hash_entry **sym_hashes;
849 bfd_signed_vma *local_got_refcounts;
850 const Elf_Internal_Rela *rel, *relend;
851 unsigned long r_symndx;
852 struct elf_link_hash_entry *h;
853 bfd *dynobj;
854 asection *sgot;
855 asection *srelgot;
857 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
858 sym_hashes = elf_sym_hashes (abfd);
859 local_got_refcounts = elf_local_got_refcounts (abfd);
861 dynobj = elf_hash_table (info)->dynobj;
862 if (dynobj == NULL)
863 return true;
865 sgot = bfd_get_section_by_name (dynobj, ".got");
866 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
868 relend = relocs + sec->reloc_count;
869 for (rel = relocs; rel < relend; rel++)
871 switch (ELF32_R_TYPE (rel->r_info))
873 case R_68K_GOT8:
874 case R_68K_GOT16:
875 case R_68K_GOT32:
876 case R_68K_GOT8O:
877 case R_68K_GOT16O:
878 case R_68K_GOT32O:
879 r_symndx = ELF32_R_SYM (rel->r_info);
880 if (r_symndx >= symtab_hdr->sh_info)
882 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
883 if (h->got.refcount > 0)
885 --h->got.refcount;
886 if (h->got.refcount == 0)
888 /* We don't need the .got entry any more. */
889 sgot->_raw_size -= 4;
890 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
894 else if (local_got_refcounts != NULL)
896 if (local_got_refcounts[r_symndx] > 0)
898 --local_got_refcounts[r_symndx];
899 if (local_got_refcounts[r_symndx] == 0)
901 /* We don't need the .got entry any more. */
902 sgot->_raw_size -= 4;
903 if (info->shared)
904 srelgot->_raw_size -= sizeof (Elf32_External_Rela);
908 break;
910 case R_68K_PLT8:
911 case R_68K_PLT16:
912 case R_68K_PLT32:
913 case R_68K_PLT8O:
914 case R_68K_PLT16O:
915 case R_68K_PLT32O:
916 case R_68K_PC8:
917 case R_68K_PC16:
918 case R_68K_PC32:
919 case R_68K_8:
920 case R_68K_16:
921 case R_68K_32:
922 r_symndx = ELF32_R_SYM (rel->r_info);
923 if (r_symndx >= symtab_hdr->sh_info)
925 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
926 if (h->plt.refcount > 0)
927 --h->plt.refcount;
929 break;
931 default:
932 break;
936 return true;
939 /* Adjust a symbol defined by a dynamic object and referenced by a
940 regular object. The current definition is in some section of the
941 dynamic object, but we're not including those sections. We have to
942 change the definition to something the rest of the link can
943 understand. */
945 static boolean
946 elf_m68k_adjust_dynamic_symbol (info, h)
947 struct bfd_link_info *info;
948 struct elf_link_hash_entry *h;
950 bfd *dynobj;
951 asection *s;
952 unsigned int power_of_two;
954 dynobj = elf_hash_table (info)->dynobj;
956 /* Make sure we know what is going on here. */
957 BFD_ASSERT (dynobj != NULL
958 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
959 || h->weakdef != NULL
960 || ((h->elf_link_hash_flags
961 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
962 && (h->elf_link_hash_flags
963 & ELF_LINK_HASH_REF_REGULAR) != 0
964 && (h->elf_link_hash_flags
965 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
967 /* If this is a function, put it in the procedure linkage table. We
968 will fill in the contents of the procedure linkage table later,
969 when we know the address of the .got section. */
970 if (h->type == STT_FUNC
971 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
973 if (! info->shared
974 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
975 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
976 /* We must always create the plt entry if it was referenced
977 by a PLTxxO relocation. In this case we already recorded
978 it as a dynamic symbol. */
979 && h->dynindx == -1)
981 /* This case can occur if we saw a PLTxx reloc in an input
982 file, but the symbol was never referred to by a dynamic
983 object. In such a case, we don't actually need to build
984 a procedure linkage table, and we can just do a PCxx
985 reloc instead. */
986 BFD_ASSERT ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0);
987 h->plt.offset = (bfd_vma) -1;
988 return true;
991 /* GC may have rendered this entry unused. */
992 if (h->plt.refcount <= 0)
994 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
995 h->plt.offset = (bfd_vma) -1;
996 return true;
999 /* Make sure this symbol is output as a dynamic symbol. */
1000 if (h->dynindx == -1)
1002 if (! bfd_elf32_link_record_dynamic_symbol (info, h))
1003 return false;
1006 s = bfd_get_section_by_name (dynobj, ".plt");
1007 BFD_ASSERT (s != NULL);
1009 /* If this is the first .plt entry, make room for the special
1010 first entry. */
1011 if (s->_raw_size == 0)
1013 if (CPU32_FLAG (dynobj))
1014 s->_raw_size += PLT_CPU32_ENTRY_SIZE;
1015 else
1016 s->_raw_size += PLT_ENTRY_SIZE;
1019 /* If this symbol is not defined in a regular file, and we are
1020 not generating a shared library, then set the symbol to this
1021 location in the .plt. This is required to make function
1022 pointers compare as equal between the normal executable and
1023 the shared library. */
1024 if (!info->shared
1025 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1027 h->root.u.def.section = s;
1028 h->root.u.def.value = s->_raw_size;
1031 h->plt.offset = s->_raw_size;
1033 /* Make room for this entry. */
1034 if (CPU32_FLAG (dynobj))
1035 s->_raw_size += PLT_CPU32_ENTRY_SIZE;
1036 else
1037 s->_raw_size += PLT_ENTRY_SIZE;
1039 /* We also need to make an entry in the .got.plt section, which
1040 will be placed in the .got section by the linker script. */
1042 s = bfd_get_section_by_name (dynobj, ".got.plt");
1043 BFD_ASSERT (s != NULL);
1044 s->_raw_size += 4;
1046 /* We also need to make an entry in the .rela.plt section. */
1048 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1049 BFD_ASSERT (s != NULL);
1050 s->_raw_size += sizeof (Elf32_External_Rela);
1052 return true;
1055 /* Reinitialize the plt offset now that it is not used as a reference
1056 count any more. */
1057 h->plt.offset = (bfd_vma) -1;
1059 /* If this is a weak symbol, and there is a real definition, the
1060 processor independent code will have arranged for us to see the
1061 real definition first, and we can just use the same value. */
1062 if (h->weakdef != NULL)
1064 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1065 || h->weakdef->root.type == bfd_link_hash_defweak);
1066 h->root.u.def.section = h->weakdef->root.u.def.section;
1067 h->root.u.def.value = h->weakdef->root.u.def.value;
1068 return true;
1071 /* This is a reference to a symbol defined by a dynamic object which
1072 is not a function. */
1074 /* If we are creating a shared library, we must presume that the
1075 only references to the symbol are via the global offset table.
1076 For such cases we need not do anything here; the relocations will
1077 be handled correctly by relocate_section. */
1078 if (info->shared)
1079 return true;
1081 /* We must allocate the symbol in our .dynbss section, which will
1082 become part of the .bss section of the executable. There will be
1083 an entry for this symbol in the .dynsym section. The dynamic
1084 object will contain position independent code, so all references
1085 from the dynamic object to this symbol will go through the global
1086 offset table. The dynamic linker will use the .dynsym entry to
1087 determine the address it must put in the global offset table, so
1088 both the dynamic object and the regular object will refer to the
1089 same memory location for the variable. */
1091 s = bfd_get_section_by_name (dynobj, ".dynbss");
1092 BFD_ASSERT (s != NULL);
1094 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1095 copy the initial value out of the dynamic object and into the
1096 runtime process image. We need to remember the offset into the
1097 .rela.bss section we are going to use. */
1098 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1100 asection *srel;
1102 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1103 BFD_ASSERT (srel != NULL);
1104 srel->_raw_size += sizeof (Elf32_External_Rela);
1105 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1108 /* We need to figure out the alignment required for this symbol. I
1109 have no idea how ELF linkers handle this. */
1110 power_of_two = bfd_log2 (h->size);
1111 if (power_of_two > 3)
1112 power_of_two = 3;
1114 /* Apply the required alignment. */
1115 s->_raw_size = BFD_ALIGN (s->_raw_size,
1116 (bfd_size_type) (1 << power_of_two));
1117 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1119 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
1120 return false;
1123 /* Define the symbol as being at this point in the section. */
1124 h->root.u.def.section = s;
1125 h->root.u.def.value = s->_raw_size;
1127 /* Increment the section size to make room for the symbol. */
1128 s->_raw_size += h->size;
1130 return true;
1133 /* Set the sizes of the dynamic sections. */
1135 static boolean
1136 elf_m68k_size_dynamic_sections (output_bfd, info)
1137 bfd *output_bfd ATTRIBUTE_UNUSED;
1138 struct bfd_link_info *info;
1140 bfd *dynobj;
1141 asection *s;
1142 boolean plt;
1143 boolean relocs;
1145 dynobj = elf_hash_table (info)->dynobj;
1146 BFD_ASSERT (dynobj != NULL);
1148 if (elf_hash_table (info)->dynamic_sections_created)
1150 /* Set the contents of the .interp section to the interpreter. */
1151 if (!info->shared)
1153 s = bfd_get_section_by_name (dynobj, ".interp");
1154 BFD_ASSERT (s != NULL);
1155 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1156 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1159 else
1161 /* We may have created entries in the .rela.got section.
1162 However, if we are not creating the dynamic sections, we will
1163 not actually use these entries. Reset the size of .rela.got,
1164 which will cause it to get stripped from the output file
1165 below. */
1166 s = bfd_get_section_by_name (dynobj, ".rela.got");
1167 if (s != NULL)
1168 s->_raw_size = 0;
1171 /* If this is a -Bsymbolic shared link, then we need to discard all PC
1172 relative relocs against symbols defined in a regular object. We
1173 allocated space for them in the check_relocs routine, but we will not
1174 fill them in in the relocate_section routine. */
1175 if (info->shared && info->symbolic)
1176 elf_m68k_link_hash_traverse (elf_m68k_hash_table (info),
1177 elf_m68k_discard_copies,
1178 (PTR) NULL);
1180 /* The check_relocs and adjust_dynamic_symbol entry points have
1181 determined the sizes of the various dynamic sections. Allocate
1182 memory for them. */
1183 plt = false;
1184 relocs = false;
1185 for (s = dynobj->sections; s != NULL; s = s->next)
1187 const char *name;
1188 boolean strip;
1190 if ((s->flags & SEC_LINKER_CREATED) == 0)
1191 continue;
1193 /* It's OK to base decisions on the section name, because none
1194 of the dynobj section names depend upon the input files. */
1195 name = bfd_get_section_name (dynobj, s);
1197 strip = false;
1199 if (strcmp (name, ".plt") == 0)
1201 if (s->_raw_size == 0)
1203 /* Strip this section if we don't need it; see the
1204 comment below. */
1205 strip = true;
1207 else
1209 /* Remember whether there is a PLT. */
1210 plt = true;
1213 else if (strncmp (name, ".rela", 5) == 0)
1215 if (s->_raw_size == 0)
1217 /* If we don't need this section, strip it from the
1218 output file. This is mostly to handle .rela.bss and
1219 .rela.plt. We must create both sections in
1220 create_dynamic_sections, because they must be created
1221 before the linker maps input sections to output
1222 sections. The linker does that before
1223 adjust_dynamic_symbol is called, and it is that
1224 function which decides whether anything needs to go
1225 into these sections. */
1226 strip = true;
1228 else
1230 relocs = true;
1232 /* We use the reloc_count field as a counter if we need
1233 to copy relocs into the output file. */
1234 s->reloc_count = 0;
1237 else if (strncmp (name, ".got", 4) != 0)
1239 /* It's not one of our sections, so don't allocate space. */
1240 continue;
1243 if (strip)
1245 _bfd_strip_section_from_output (info, s);
1246 continue;
1249 /* Allocate memory for the section contents. */
1250 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1251 Unused entries should be reclaimed before the section's contents
1252 are written out, but at the moment this does not happen. Thus in
1253 order to prevent writing out garbage, we initialise the section's
1254 contents to zero. */
1255 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1256 if (s->contents == NULL && s->_raw_size != 0)
1257 return false;
1260 if (elf_hash_table (info)->dynamic_sections_created)
1262 /* Add some entries to the .dynamic section. We fill in the
1263 values later, in elf_m68k_finish_dynamic_sections, but we
1264 must add the entries now so that we get the correct size for
1265 the .dynamic section. The DT_DEBUG entry is filled in by the
1266 dynamic linker and used by the debugger. */
1267 if (!info->shared)
1269 if (!bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0))
1270 return false;
1273 if (plt)
1275 if (!bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)
1276 || !bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1277 || !bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1278 || !bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0))
1279 return false;
1282 if (relocs)
1284 if (!bfd_elf32_add_dynamic_entry (info, DT_RELA, 0)
1285 || !bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0)
1286 || !bfd_elf32_add_dynamic_entry (info, DT_RELAENT,
1287 sizeof (Elf32_External_Rela)))
1288 return false;
1291 if ((info->flags & DF_TEXTREL) != 0)
1293 if (!bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0))
1294 return false;
1298 return true;
1301 /* This function is called via elf_m68k_link_hash_traverse if we are
1302 creating a shared object with -Bsymbolic. It discards the space
1303 allocated to copy PC relative relocs against symbols which are defined
1304 in regular objects. We allocated space for them in the check_relocs
1305 routine, but we won't fill them in in the relocate_section routine. */
1307 static boolean
1308 elf_m68k_discard_copies (h, ignore)
1309 struct elf_m68k_link_hash_entry *h;
1310 PTR ignore ATTRIBUTE_UNUSED;
1312 struct elf_m68k_pcrel_relocs_copied *s;
1314 /* We only discard relocs for symbols defined in a regular object. */
1315 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1316 return true;
1318 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1319 s->section->_raw_size -= s->count * sizeof (Elf32_External_Rela);
1321 return true;
1324 /* Relocate an M68K ELF section. */
1326 static boolean
1327 elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1328 contents, relocs, local_syms, local_sections)
1329 bfd *output_bfd;
1330 struct bfd_link_info *info;
1331 bfd *input_bfd;
1332 asection *input_section;
1333 bfd_byte *contents;
1334 Elf_Internal_Rela *relocs;
1335 Elf_Internal_Sym *local_syms;
1336 asection **local_sections;
1338 bfd *dynobj;
1339 Elf_Internal_Shdr *symtab_hdr;
1340 struct elf_link_hash_entry **sym_hashes;
1341 bfd_vma *local_got_offsets;
1342 asection *sgot;
1343 asection *splt;
1344 asection *sreloc;
1345 Elf_Internal_Rela *rel;
1346 Elf_Internal_Rela *relend;
1348 dynobj = elf_hash_table (info)->dynobj;
1349 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1350 sym_hashes = elf_sym_hashes (input_bfd);
1351 local_got_offsets = elf_local_got_offsets (input_bfd);
1353 sgot = NULL;
1354 splt = NULL;
1355 sreloc = NULL;
1357 rel = relocs;
1358 relend = relocs + input_section->reloc_count;
1359 for (; rel < relend; rel++)
1361 int r_type;
1362 reloc_howto_type *howto;
1363 unsigned long r_symndx;
1364 struct elf_link_hash_entry *h;
1365 Elf_Internal_Sym *sym;
1366 asection *sec;
1367 bfd_vma relocation;
1368 bfd_reloc_status_type r;
1370 r_type = ELF32_R_TYPE (rel->r_info);
1371 if (r_type < 0 || r_type >= (int) R_68K_max)
1373 bfd_set_error (bfd_error_bad_value);
1374 return false;
1376 howto = howto_table + r_type;
1378 r_symndx = ELF32_R_SYM (rel->r_info);
1380 if (info->relocateable)
1382 /* This is a relocateable link. We don't have to change
1383 anything, unless the reloc is against a section symbol,
1384 in which case we have to adjust according to where the
1385 section symbol winds up in the output section. */
1386 if (r_symndx < symtab_hdr->sh_info)
1388 sym = local_syms + r_symndx;
1389 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1391 sec = local_sections[r_symndx];
1392 rel->r_addend += sec->output_offset + sym->st_value;
1396 continue;
1399 /* This is a final link. */
1400 h = NULL;
1401 sym = NULL;
1402 sec = NULL;
1403 if (r_symndx < symtab_hdr->sh_info)
1405 sym = local_syms + r_symndx;
1406 sec = local_sections[r_symndx];
1407 relocation = (sec->output_section->vma
1408 + sec->output_offset
1409 + sym->st_value);
1411 else
1413 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1414 while (h->root.type == bfd_link_hash_indirect
1415 || h->root.type == bfd_link_hash_warning)
1416 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1417 if (h->root.type == bfd_link_hash_defined
1418 || h->root.type == bfd_link_hash_defweak)
1420 sec = h->root.u.def.section;
1421 if (((r_type == R_68K_PLT8
1422 || r_type == R_68K_PLT16
1423 || r_type == R_68K_PLT32
1424 || r_type == R_68K_PLT8O
1425 || r_type == R_68K_PLT16O
1426 || r_type == R_68K_PLT32O)
1427 && h->plt.offset != (bfd_vma) -1
1428 && elf_hash_table (info)->dynamic_sections_created)
1429 || ((r_type == R_68K_GOT8O
1430 || r_type == R_68K_GOT16O
1431 || r_type == R_68K_GOT32O
1432 || ((r_type == R_68K_GOT8
1433 || r_type == R_68K_GOT16
1434 || r_type == R_68K_GOT32)
1435 && strcmp (h->root.root.string,
1436 "_GLOBAL_OFFSET_TABLE_") != 0))
1437 && elf_hash_table (info)->dynamic_sections_created
1438 && (! info->shared
1439 || (! info->symbolic && h->dynindx != -1)
1440 || (h->elf_link_hash_flags
1441 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1442 || (info->shared
1443 && ((! info->symbolic && h->dynindx != -1)
1444 || (h->elf_link_hash_flags
1445 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1446 && ((input_section->flags & SEC_ALLOC) != 0
1447 /* DWARF will emit R_68K_32 relocations in its
1448 sections against symbols defined externally
1449 in shared libraries. We can't do anything
1450 with them here. */
1451 || ((input_section->flags & SEC_DEBUGGING) != 0
1452 && (h->elf_link_hash_flags
1453 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
1454 && (r_type == R_68K_8
1455 || r_type == R_68K_16
1456 || r_type == R_68K_32
1457 || r_type == R_68K_PC8
1458 || r_type == R_68K_PC16
1459 || r_type == R_68K_PC32)))
1461 /* In these cases, we don't need the relocation
1462 value. We check specially because in some
1463 obscure cases sec->output_section will be NULL. */
1464 relocation = 0;
1466 else
1467 relocation = (h->root.u.def.value
1468 + sec->output_section->vma
1469 + sec->output_offset);
1471 else if (h->root.type == bfd_link_hash_undefweak)
1472 relocation = 0;
1473 else if (info->shared && !info->symbolic
1474 && !info->no_undefined
1475 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1476 relocation = 0;
1477 else
1479 if (!(info->callbacks->undefined_symbol
1480 (info, h->root.root.string, input_bfd,
1481 input_section, rel->r_offset,
1482 (!info->shared || info->no_undefined
1483 || ELF_ST_VISIBILITY (h->other)))))
1484 return false;
1485 relocation = 0;
1489 switch (r_type)
1491 case R_68K_GOT8:
1492 case R_68K_GOT16:
1493 case R_68K_GOT32:
1494 /* Relocation is to the address of the entry for this symbol
1495 in the global offset table. */
1496 if (h != NULL
1497 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1498 break;
1499 /* Fall through. */
1500 case R_68K_GOT8O:
1501 case R_68K_GOT16O:
1502 case R_68K_GOT32O:
1503 /* Relocation is the offset of the entry for this symbol in
1504 the global offset table. */
1507 bfd_vma off;
1509 if (sgot == NULL)
1511 sgot = bfd_get_section_by_name (dynobj, ".got");
1512 BFD_ASSERT (sgot != NULL);
1515 if (h != NULL)
1517 off = h->got.offset;
1518 BFD_ASSERT (off != (bfd_vma) -1);
1520 if (!elf_hash_table (info)->dynamic_sections_created
1521 || (info->shared
1522 && (info->symbolic || h->dynindx == -1)
1523 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1525 /* This is actually a static link, or it is a
1526 -Bsymbolic link and the symbol is defined
1527 locally, or the symbol was forced to be local
1528 because of a version file.. We must initialize
1529 this entry in the global offset table. Since
1530 the offset must always be a multiple of 4, we
1531 use the least significant bit to record whether
1532 we have initialized it already.
1534 When doing a dynamic link, we create a .rela.got
1535 relocation entry to initialize the value. This
1536 is done in the finish_dynamic_symbol routine. */
1537 if ((off & 1) != 0)
1538 off &= ~1;
1539 else
1541 bfd_put_32 (output_bfd, relocation,
1542 sgot->contents + off);
1543 h->got.offset |= 1;
1547 else
1549 BFD_ASSERT (local_got_offsets != NULL
1550 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1552 off = local_got_offsets[r_symndx];
1554 /* The offset must always be a multiple of 4. We use
1555 the least significant bit to record whether we have
1556 already generated the necessary reloc. */
1557 if ((off & 1) != 0)
1558 off &= ~1;
1559 else
1561 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1563 if (info->shared)
1565 asection *srelgot;
1566 Elf_Internal_Rela outrel;
1568 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1569 BFD_ASSERT (srelgot != NULL);
1571 outrel.r_offset = (sgot->output_section->vma
1572 + sgot->output_offset
1573 + off);
1574 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1575 outrel.r_addend = relocation;
1576 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1577 (((Elf32_External_Rela *)
1578 srelgot->contents)
1579 + srelgot->reloc_count));
1580 ++srelgot->reloc_count;
1583 local_got_offsets[r_symndx] |= 1;
1587 relocation = sgot->output_offset + off;
1588 if (r_type == R_68K_GOT8O
1589 || r_type == R_68K_GOT16O
1590 || r_type == R_68K_GOT32O)
1592 /* This relocation does not use the addend. */
1593 rel->r_addend = 0;
1595 else
1596 relocation += sgot->output_section->vma;
1598 break;
1600 case R_68K_PLT8:
1601 case R_68K_PLT16:
1602 case R_68K_PLT32:
1603 /* Relocation is to the entry for this symbol in the
1604 procedure linkage table. */
1606 /* Resolve a PLTxx reloc against a local symbol directly,
1607 without using the procedure linkage table. */
1608 if (h == NULL)
1609 break;
1611 if (h->plt.offset == (bfd_vma) -1
1612 || !elf_hash_table (info)->dynamic_sections_created)
1614 /* We didn't make a PLT entry for this symbol. This
1615 happens when statically linking PIC code, or when
1616 using -Bsymbolic. */
1617 break;
1620 if (splt == NULL)
1622 splt = bfd_get_section_by_name (dynobj, ".plt");
1623 BFD_ASSERT (splt != NULL);
1626 relocation = (splt->output_section->vma
1627 + splt->output_offset
1628 + h->plt.offset);
1629 break;
1631 case R_68K_PLT8O:
1632 case R_68K_PLT16O:
1633 case R_68K_PLT32O:
1634 /* Relocation is the offset of the entry for this symbol in
1635 the procedure linkage table. */
1636 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1638 if (splt == NULL)
1640 splt = bfd_get_section_by_name (dynobj, ".plt");
1641 BFD_ASSERT (splt != NULL);
1644 relocation = h->plt.offset;
1646 /* This relocation does not use the addend. */
1647 rel->r_addend = 0;
1649 break;
1651 case R_68K_PC8:
1652 case R_68K_PC16:
1653 case R_68K_PC32:
1654 if (h == NULL)
1655 break;
1656 /* Fall through. */
1657 case R_68K_8:
1658 case R_68K_16:
1659 case R_68K_32:
1660 if (info->shared
1661 && (input_section->flags & SEC_ALLOC) != 0
1662 && ((r_type != R_68K_PC8
1663 && r_type != R_68K_PC16
1664 && r_type != R_68K_PC32)
1665 || (!info->symbolic
1666 || (h->elf_link_hash_flags
1667 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
1669 Elf_Internal_Rela outrel;
1670 boolean skip, relocate;
1672 /* When generating a shared object, these relocations
1673 are copied into the output file to be resolved at run
1674 time. */
1676 if (sreloc == NULL)
1678 const char *name;
1680 name = (bfd_elf_string_from_elf_section
1681 (input_bfd,
1682 elf_elfheader (input_bfd)->e_shstrndx,
1683 elf_section_data (input_section)->rel_hdr.sh_name));
1684 if (name == NULL)
1685 return false;
1687 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1688 && strcmp (bfd_get_section_name (input_bfd,
1689 input_section),
1690 name + 5) == 0);
1692 sreloc = bfd_get_section_by_name (dynobj, name);
1693 BFD_ASSERT (sreloc != NULL);
1696 skip = false;
1698 if (elf_section_data (input_section)->stab_info == NULL)
1699 outrel.r_offset = rel->r_offset;
1700 else
1702 bfd_vma off;
1704 off = (_bfd_stab_section_offset
1705 (output_bfd, &elf_hash_table (info)->stab_info,
1706 input_section,
1707 &elf_section_data (input_section)->stab_info,
1708 rel->r_offset));
1709 if (off == (bfd_vma) -1)
1710 skip = true;
1711 outrel.r_offset = off;
1714 outrel.r_offset += (input_section->output_section->vma
1715 + input_section->output_offset);
1717 if (skip)
1719 memset (&outrel, 0, sizeof outrel);
1720 relocate = false;
1722 /* h->dynindx may be -1 if the symbol was marked to
1723 become local. */
1724 else if (h != NULL
1725 && ((! info->symbolic && h->dynindx != -1)
1726 || (h->elf_link_hash_flags
1727 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1729 BFD_ASSERT (h->dynindx != -1);
1730 relocate = false;
1731 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1732 outrel.r_addend = relocation + rel->r_addend;
1734 else
1736 if (r_type == R_68K_32)
1738 relocate = true;
1739 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1740 outrel.r_addend = relocation + rel->r_addend;
1742 else
1744 long indx;
1746 if (h == NULL)
1747 sec = local_sections[r_symndx];
1748 else
1750 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1751 || (h->root.type
1752 == bfd_link_hash_defweak));
1753 sec = h->root.u.def.section;
1755 if (sec != NULL && bfd_is_abs_section (sec))
1756 indx = 0;
1757 else if (sec == NULL || sec->owner == NULL)
1759 bfd_set_error (bfd_error_bad_value);
1760 return false;
1762 else
1764 asection *osec;
1766 osec = sec->output_section;
1767 indx = elf_section_data (osec)->dynindx;
1768 BFD_ASSERT (indx > 0);
1771 relocate = false;
1772 outrel.r_info = ELF32_R_INFO (indx, r_type);
1773 outrel.r_addend = relocation + rel->r_addend;
1777 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1778 (((Elf32_External_Rela *)
1779 sreloc->contents)
1780 + sreloc->reloc_count));
1781 ++sreloc->reloc_count;
1783 /* This reloc will be computed at runtime, so there's no
1784 need to do anything now, except for R_68K_32
1785 relocations that have been turned into
1786 R_68K_RELATIVE. */
1787 if (!relocate)
1788 continue;
1791 break;
1793 case R_68K_GNU_VTINHERIT:
1794 case R_68K_GNU_VTENTRY:
1795 /* These are no-ops in the end. */
1796 continue;
1798 default:
1799 break;
1802 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1803 contents, rel->r_offset,
1804 relocation, rel->r_addend);
1806 if (r != bfd_reloc_ok)
1808 switch (r)
1810 default:
1811 case bfd_reloc_outofrange:
1812 abort ();
1813 case bfd_reloc_overflow:
1815 const char *name;
1817 if (h != NULL)
1818 name = h->root.root.string;
1819 else
1821 name = bfd_elf_string_from_elf_section (input_bfd,
1822 symtab_hdr->sh_link,
1823 sym->st_name);
1824 if (name == NULL)
1825 return false;
1826 if (*name == '\0')
1827 name = bfd_section_name (input_bfd, sec);
1829 if (!(info->callbacks->reloc_overflow
1830 (info, name, howto->name, (bfd_vma) 0,
1831 input_bfd, input_section, rel->r_offset)))
1832 return false;
1834 break;
1839 return true;
1842 /* Finish up dynamic symbol handling. We set the contents of various
1843 dynamic sections here. */
1845 static boolean
1846 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
1847 bfd *output_bfd;
1848 struct bfd_link_info *info;
1849 struct elf_link_hash_entry *h;
1850 Elf_Internal_Sym *sym;
1852 bfd *dynobj;
1853 int plt_off1, plt_off2, plt_off3;
1855 dynobj = elf_hash_table (info)->dynobj;
1857 if (h->plt.offset != (bfd_vma) -1)
1859 asection *splt;
1860 asection *sgot;
1861 asection *srela;
1862 bfd_vma plt_index;
1863 bfd_vma got_offset;
1864 Elf_Internal_Rela rela;
1866 /* This symbol has an entry in the procedure linkage table. Set
1867 it up. */
1869 BFD_ASSERT (h->dynindx != -1);
1871 splt = bfd_get_section_by_name (dynobj, ".plt");
1872 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1873 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1874 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1876 /* Get the index in the procedure linkage table which
1877 corresponds to this symbol. This is the index of this symbol
1878 in all the symbols for which we are making plt entries. The
1879 first entry in the procedure linkage table is reserved. */
1880 if ( CPU32_FLAG (output_bfd))
1881 plt_index = h->plt.offset / PLT_CPU32_ENTRY_SIZE - 1;
1882 else
1883 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1885 /* Get the offset into the .got table of the entry that
1886 corresponds to this function. Each .got entry is 4 bytes.
1887 The first three are reserved. */
1888 got_offset = (plt_index + 3) * 4;
1890 if ( CPU32_FLAG (output_bfd))
1892 /* Fill in the entry in the procedure linkage table. */
1893 memcpy (splt->contents + h->plt.offset, elf_cpu32_plt_entry,
1894 PLT_CPU32_ENTRY_SIZE);
1895 plt_off1 = 4;
1896 plt_off2 = 12;
1897 plt_off3 = 18;
1899 else
1901 /* Fill in the entry in the procedure linkage table. */
1902 memcpy (splt->contents + h->plt.offset, elf_m68k_plt_entry,
1903 PLT_ENTRY_SIZE);
1904 plt_off1 = 4;
1905 plt_off2 = 10;
1906 plt_off3 = 16;
1909 /* The offset is relative to the first extension word. */
1910 bfd_put_32 (output_bfd,
1911 (sgot->output_section->vma
1912 + sgot->output_offset
1913 + got_offset
1914 - (splt->output_section->vma
1915 + h->plt.offset + 2)),
1916 splt->contents + h->plt.offset + plt_off1);
1918 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
1919 splt->contents + h->plt.offset + plt_off2);
1920 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3),
1921 splt->contents + h->plt.offset + plt_off3);
1923 /* Fill in the entry in the global offset table. */
1924 bfd_put_32 (output_bfd,
1925 (splt->output_section->vma
1926 + splt->output_offset
1927 + h->plt.offset
1928 + 8),
1929 sgot->contents + got_offset);
1931 /* Fill in the entry in the .rela.plt section. */
1932 rela.r_offset = (sgot->output_section->vma
1933 + sgot->output_offset
1934 + got_offset);
1935 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
1936 rela.r_addend = 0;
1937 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1938 ((Elf32_External_Rela *) srela->contents
1939 + plt_index));
1941 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1943 /* Mark the symbol as undefined, rather than as defined in
1944 the .plt section. Leave the value alone. */
1945 sym->st_shndx = SHN_UNDEF;
1949 if (h->got.offset != (bfd_vma) -1)
1951 asection *sgot;
1952 asection *srela;
1953 Elf_Internal_Rela rela;
1955 /* This symbol has an entry in the global offset table. Set it
1956 up. */
1958 sgot = bfd_get_section_by_name (dynobj, ".got");
1959 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1960 BFD_ASSERT (sgot != NULL && srela != NULL);
1962 rela.r_offset = (sgot->output_section->vma
1963 + sgot->output_offset
1964 + (h->got.offset &~ 1));
1966 /* If this is a -Bsymbolic link, and the symbol is defined
1967 locally, we just want to emit a RELATIVE reloc. Likewise if
1968 the symbol was forced to be local because of a version file.
1969 The entry in the global offset table will already have been
1970 initialized in the relocate_section function. */
1971 if (info->shared
1972 && (info->symbolic || h->dynindx == -1)
1973 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
1975 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1976 rela.r_addend = bfd_get_signed_32 (output_bfd,
1977 (sgot->contents
1978 + (h->got.offset & ~1)));
1980 else
1982 bfd_put_32 (output_bfd, (bfd_vma) 0,
1983 sgot->contents + (h->got.offset & ~1));
1984 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
1985 rela.r_addend = 0;
1988 bfd_elf32_swap_reloca_out (output_bfd, &rela,
1989 ((Elf32_External_Rela *) srela->contents
1990 + srela->reloc_count));
1991 ++srela->reloc_count;
1994 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1996 asection *s;
1997 Elf_Internal_Rela rela;
1999 /* This symbol needs a copy reloc. Set it up. */
2001 BFD_ASSERT (h->dynindx != -1
2002 && (h->root.type == bfd_link_hash_defined
2003 || h->root.type == bfd_link_hash_defweak));
2005 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2006 ".rela.bss");
2007 BFD_ASSERT (s != NULL);
2009 rela.r_offset = (h->root.u.def.value
2010 + h->root.u.def.section->output_section->vma
2011 + h->root.u.def.section->output_offset);
2012 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
2013 rela.r_addend = 0;
2014 bfd_elf32_swap_reloca_out (output_bfd, &rela,
2015 ((Elf32_External_Rela *) s->contents
2016 + s->reloc_count));
2017 ++s->reloc_count;
2020 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2021 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2022 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2023 sym->st_shndx = SHN_ABS;
2025 return true;
2028 /* Finish up the dynamic sections. */
2030 static boolean
2031 elf_m68k_finish_dynamic_sections (output_bfd, info)
2032 bfd *output_bfd;
2033 struct bfd_link_info *info;
2035 bfd *dynobj;
2036 asection *sgot;
2037 asection *sdyn;
2039 dynobj = elf_hash_table (info)->dynobj;
2041 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2042 BFD_ASSERT (sgot != NULL);
2043 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2045 if (elf_hash_table (info)->dynamic_sections_created)
2047 asection *splt;
2048 Elf32_External_Dyn *dyncon, *dynconend;
2050 splt = bfd_get_section_by_name (dynobj, ".plt");
2051 BFD_ASSERT (splt != NULL && sdyn != NULL);
2053 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2054 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2055 for (; dyncon < dynconend; dyncon++)
2057 Elf_Internal_Dyn dyn;
2058 const char *name;
2059 asection *s;
2061 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2063 switch (dyn.d_tag)
2065 default:
2066 break;
2068 case DT_PLTGOT:
2069 name = ".got";
2070 goto get_vma;
2071 case DT_JMPREL:
2072 name = ".rela.plt";
2073 get_vma:
2074 s = bfd_get_section_by_name (output_bfd, name);
2075 BFD_ASSERT (s != NULL);
2076 dyn.d_un.d_ptr = s->vma;
2077 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2078 break;
2080 case DT_PLTRELSZ:
2081 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2082 BFD_ASSERT (s != NULL);
2083 if (s->_cooked_size != 0)
2084 dyn.d_un.d_val = s->_cooked_size;
2085 else
2086 dyn.d_un.d_val = s->_raw_size;
2087 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2088 break;
2090 case DT_RELASZ:
2091 /* The procedure linkage table relocs (DT_JMPREL) should
2092 not be included in the overall relocs (DT_RELA).
2093 Therefore, we override the DT_RELASZ entry here to
2094 make it not include the JMPREL relocs. Since the
2095 linker script arranges for .rela.plt to follow all
2096 other relocation sections, we don't have to worry
2097 about changing the DT_RELA entry. */
2098 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2099 if (s != NULL)
2101 if (s->_cooked_size != 0)
2102 dyn.d_un.d_val -= s->_cooked_size;
2103 else
2104 dyn.d_un.d_val -= s->_raw_size;
2106 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2107 break;
2111 /* Fill in the first entry in the procedure linkage table. */
2112 if (splt->_raw_size > 0)
2114 if (!CPU32_FLAG (output_bfd))
2116 memcpy (splt->contents, elf_m68k_plt0_entry, PLT_ENTRY_SIZE);
2117 bfd_put_32 (output_bfd,
2118 (sgot->output_section->vma
2119 + sgot->output_offset + 4
2120 - (splt->output_section->vma + 2)),
2121 splt->contents + 4);
2122 bfd_put_32 (output_bfd,
2123 (sgot->output_section->vma
2124 + sgot->output_offset + 8
2125 - (splt->output_section->vma + 10)),
2126 splt->contents + 12);
2127 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2128 = PLT_ENTRY_SIZE;
2130 else /* cpu32 */
2132 memcpy (splt->contents, elf_cpu32_plt0_entry, PLT_CPU32_ENTRY_SIZE);
2133 bfd_put_32 (output_bfd,
2134 (sgot->output_section->vma
2135 + sgot->output_offset + 4
2136 - (splt->output_section->vma + 2)),
2137 splt->contents + 4);
2138 bfd_put_32 (output_bfd,
2139 (sgot->output_section->vma
2140 + sgot->output_offset + 8
2141 - (splt->output_section->vma + 10)),
2142 splt->contents + 12);
2143 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2144 = PLT_CPU32_ENTRY_SIZE;
2149 /* Fill in the first three entries in the global offset table. */
2150 if (sgot->_raw_size > 0)
2152 if (sdyn == NULL)
2153 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2154 else
2155 bfd_put_32 (output_bfd,
2156 sdyn->output_section->vma + sdyn->output_offset,
2157 sgot->contents);
2158 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2159 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2162 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2164 return true;
2167 /* Given a .data section and a .emreloc in-memory section, store
2168 relocation information into the .emreloc section which can be
2169 used at runtime to relocate the section. This is called by the
2170 linker when the --embedded-relocs switch is used. This is called
2171 after the add_symbols entry point has been called for all the
2172 objects, and before the final_link entry point is called. */
2174 boolean
2175 bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2176 bfd *abfd;
2177 struct bfd_link_info *info;
2178 asection *datasec;
2179 asection *relsec;
2180 char **errmsg;
2182 Elf_Internal_Shdr *symtab_hdr;
2183 Elf32_External_Sym *extsyms;
2184 Elf32_External_Sym *free_extsyms = NULL;
2185 Elf_Internal_Rela *internal_relocs;
2186 Elf_Internal_Rela *free_relocs = NULL;
2187 Elf_Internal_Rela *irel, *irelend;
2188 bfd_byte *p;
2190 BFD_ASSERT (! info->relocateable);
2192 *errmsg = NULL;
2194 if (datasec->reloc_count == 0)
2195 return true;
2197 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2198 /* Read this BFD's symbols if we haven't done so already, or get the cached
2199 copy if it exists. */
2200 if (symtab_hdr->contents != NULL)
2201 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2202 else
2204 /* Go get them off disk. */
2205 if (info->keep_memory)
2206 extsyms = ((Elf32_External_Sym *)
2207 bfd_alloc (abfd, symtab_hdr->sh_size));
2208 else
2209 extsyms = ((Elf32_External_Sym *)
2210 bfd_malloc (symtab_hdr->sh_size));
2211 if (extsyms == NULL)
2212 goto error_return;
2213 if (! info->keep_memory)
2214 free_extsyms = extsyms;
2215 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2216 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
2217 != symtab_hdr->sh_size))
2218 goto error_return;
2219 if (info->keep_memory)
2220 symtab_hdr->contents = extsyms;
2223 /* Get a copy of the native relocations. */
2224 internal_relocs = (_bfd_elf32_link_read_relocs
2225 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2226 info->keep_memory));
2227 if (internal_relocs == NULL)
2228 goto error_return;
2229 if (! info->keep_memory)
2230 free_relocs = internal_relocs;
2232 relsec->contents = (bfd_byte *) bfd_alloc (abfd, datasec->reloc_count * 12);
2233 if (relsec->contents == NULL)
2234 goto error_return;
2236 p = relsec->contents;
2238 irelend = internal_relocs + datasec->reloc_count;
2239 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2241 asection *targetsec;
2243 /* We are going to write a four byte longword into the runtime
2244 reloc section. The longword will be the address in the data
2245 section which must be relocated. It is followed by the name
2246 of the target section NUL-padded or truncated to 8
2247 characters. */
2249 /* We can only relocate absolute longword relocs at run time. */
2250 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2252 *errmsg = _("unsupported reloc type");
2253 bfd_set_error (bfd_error_bad_value);
2254 goto error_return;
2257 /* Get the target section referred to by the reloc. */
2258 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2260 Elf_Internal_Sym isym;
2262 /* A local symbol. */
2263 bfd_elf32_swap_symbol_in (abfd,
2264 extsyms + ELF32_R_SYM (irel->r_info),
2265 &isym);
2267 targetsec = bfd_section_from_elf_index (abfd, isym.st_shndx);
2269 else
2271 unsigned long indx;
2272 struct elf_link_hash_entry *h;
2274 /* An external symbol. */
2275 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2276 h = elf_sym_hashes (abfd)[indx];
2277 BFD_ASSERT (h != NULL);
2278 if (h->root.type == bfd_link_hash_defined
2279 || h->root.type == bfd_link_hash_defweak)
2280 targetsec = h->root.u.def.section;
2281 else
2282 targetsec = NULL;
2285 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2286 memset (p + 4, 0, 8);
2287 if (targetsec != NULL)
2288 strncpy (p + 4, targetsec->output_section->name, 8);
2291 if (free_extsyms != NULL)
2292 free (free_extsyms);
2293 if (free_relocs != NULL)
2294 free (free_relocs);
2295 return true;
2297 error_return:
2298 if (free_extsyms != NULL)
2299 free (free_extsyms);
2300 if (free_relocs != NULL)
2301 free (free_relocs);
2302 return false;
2305 static enum elf_reloc_type_class
2306 elf32_m68k_reloc_type_class (type)
2307 int type;
2309 switch (type)
2311 case R_68K_RELATIVE:
2312 return reloc_class_relative;
2313 case R_68K_JMP_SLOT:
2314 return reloc_class_plt;
2315 case R_68K_COPY:
2316 return reloc_class_copy;
2317 default:
2318 return reloc_class_normal;
2322 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
2323 #define TARGET_BIG_NAME "elf32-m68k"
2324 #define ELF_MACHINE_CODE EM_68K
2325 #define ELF_MAXPAGESIZE 0x2000
2326 #define elf_backend_create_dynamic_sections \
2327 _bfd_elf_create_dynamic_sections
2328 #define bfd_elf32_bfd_link_hash_table_create \
2329 elf_m68k_link_hash_table_create
2330 #define bfd_elf32_bfd_final_link _bfd_elf32_gc_common_final_link
2332 #define elf_backend_check_relocs elf_m68k_check_relocs
2333 #define elf_backend_adjust_dynamic_symbol \
2334 elf_m68k_adjust_dynamic_symbol
2335 #define elf_backend_size_dynamic_sections \
2336 elf_m68k_size_dynamic_sections
2337 #define elf_backend_relocate_section elf_m68k_relocate_section
2338 #define elf_backend_finish_dynamic_symbol \
2339 elf_m68k_finish_dynamic_symbol
2340 #define elf_backend_finish_dynamic_sections \
2341 elf_m68k_finish_dynamic_sections
2342 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
2343 #define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
2344 #define bfd_elf32_bfd_copy_private_bfd_data \
2345 elf32_m68k_copy_private_bfd_data
2346 #define bfd_elf32_bfd_merge_private_bfd_data \
2347 elf32_m68k_merge_private_bfd_data
2348 #define bfd_elf32_bfd_set_private_flags \
2349 elf32_m68k_set_private_flags
2350 #define bfd_elf32_bfd_print_private_bfd_data \
2351 elf32_m68k_print_private_bfd_data
2352 #define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
2354 #define elf_backend_can_gc_sections 1
2355 #define elf_backend_want_got_plt 1
2356 #define elf_backend_plt_readonly 1
2357 #define elf_backend_want_plt_sym 0
2358 #define elf_backend_got_header_size 12
2360 #include "elf32-target.h"