* elf.c (sym_is_global): Return a bfd_boolean.
[binutils.git] / bfd / elf32-m68k.c
bloba60623ce5627bfdc6f6c94bf28c8916e20f379f7
1 /* Motorola 68k series support for 32-bit ELF
2 Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006 Free Software Foundation, Inc.
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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"
27 #include "opcode/m68k.h"
29 static reloc_howto_type *reloc_type_lookup
30 PARAMS ((bfd *, bfd_reloc_code_real_type));
31 static void rtype_to_howto
32 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
33 static struct bfd_hash_entry *elf_m68k_link_hash_newfunc
34 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
35 static struct bfd_link_hash_table *elf_m68k_link_hash_table_create
36 PARAMS ((bfd *));
37 static bfd_boolean elf_m68k_check_relocs
38 PARAMS ((bfd *, struct bfd_link_info *, asection *,
39 const Elf_Internal_Rela *));
40 static asection *elf_m68k_gc_mark_hook
41 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
42 struct elf_link_hash_entry *, Elf_Internal_Sym *));
43 static bfd_boolean elf_m68k_gc_sweep_hook
44 PARAMS ((bfd *, struct bfd_link_info *, asection *,
45 const Elf_Internal_Rela *));
46 static bfd_boolean elf_m68k_adjust_dynamic_symbol
47 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
48 static bfd_boolean elf_m68k_size_dynamic_sections
49 PARAMS ((bfd *, struct bfd_link_info *));
50 static bfd_boolean elf_m68k_discard_copies
51 PARAMS ((struct elf_link_hash_entry *, PTR));
52 static bfd_boolean elf_m68k_relocate_section
53 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
54 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
55 static bfd_boolean elf_m68k_finish_dynamic_symbol
56 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
57 Elf_Internal_Sym *));
58 static bfd_boolean elf_m68k_finish_dynamic_sections
59 PARAMS ((bfd *, struct bfd_link_info *));
61 static bfd_boolean elf32_m68k_set_private_flags
62 PARAMS ((bfd *, flagword));
63 static bfd_boolean elf32_m68k_merge_private_bfd_data
64 PARAMS ((bfd *, bfd *));
65 static bfd_boolean elf32_m68k_print_private_bfd_data
66 PARAMS ((bfd *, PTR));
67 static enum elf_reloc_type_class elf32_m68k_reloc_type_class
68 PARAMS ((const Elf_Internal_Rela *));
70 static reloc_howto_type howto_table[] = {
71 HOWTO(R_68K_NONE, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_NONE", FALSE, 0, 0x00000000,FALSE),
72 HOWTO(R_68K_32, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32", FALSE, 0, 0xffffffff,FALSE),
73 HOWTO(R_68K_16, 0, 1,16, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16", FALSE, 0, 0x0000ffff,FALSE),
74 HOWTO(R_68K_8, 0, 0, 8, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8", FALSE, 0, 0x000000ff,FALSE),
75 HOWTO(R_68K_PC32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32", FALSE, 0, 0xffffffff,TRUE),
76 HOWTO(R_68K_PC16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC16", FALSE, 0, 0x0000ffff,TRUE),
77 HOWTO(R_68K_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC8", FALSE, 0, 0x000000ff,TRUE),
78 HOWTO(R_68K_GOT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32", FALSE, 0, 0xffffffff,TRUE),
79 HOWTO(R_68K_GOT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16", FALSE, 0, 0x0000ffff,TRUE),
80 HOWTO(R_68K_GOT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8", FALSE, 0, 0x000000ff,TRUE),
81 HOWTO(R_68K_GOT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O", FALSE, 0, 0xffffffff,FALSE),
82 HOWTO(R_68K_GOT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16O", FALSE, 0, 0x0000ffff,FALSE),
83 HOWTO(R_68K_GOT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8O", FALSE, 0, 0x000000ff,FALSE),
84 HOWTO(R_68K_PLT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32", FALSE, 0, 0xffffffff,TRUE),
85 HOWTO(R_68K_PLT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16", FALSE, 0, 0x0000ffff,TRUE),
86 HOWTO(R_68K_PLT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8", FALSE, 0, 0x000000ff,TRUE),
87 HOWTO(R_68K_PLT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O", FALSE, 0, 0xffffffff,FALSE),
88 HOWTO(R_68K_PLT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16O", FALSE, 0, 0x0000ffff,FALSE),
89 HOWTO(R_68K_PLT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8O", FALSE, 0, 0x000000ff,FALSE),
90 HOWTO(R_68K_COPY, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_COPY", FALSE, 0, 0xffffffff,FALSE),
91 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),
92 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),
93 HOWTO(R_68K_RELATIVE, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_RELATIVE", FALSE, 0, 0xffffffff,FALSE),
94 /* GNU extension to record C++ vtable hierarchy. */
95 HOWTO (R_68K_GNU_VTINHERIT, /* type */
96 0, /* rightshift */
97 2, /* size (0 = byte, 1 = short, 2 = long) */
98 0, /* bitsize */
99 FALSE, /* pc_relative */
100 0, /* bitpos */
101 complain_overflow_dont, /* complain_on_overflow */
102 NULL, /* special_function */
103 "R_68K_GNU_VTINHERIT", /* name */
104 FALSE, /* partial_inplace */
105 0, /* src_mask */
106 0, /* dst_mask */
107 FALSE),
108 /* GNU extension to record C++ vtable member usage. */
109 HOWTO (R_68K_GNU_VTENTRY, /* type */
110 0, /* rightshift */
111 2, /* size (0 = byte, 1 = short, 2 = long) */
112 0, /* bitsize */
113 FALSE, /* pc_relative */
114 0, /* bitpos */
115 complain_overflow_dont, /* complain_on_overflow */
116 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
117 "R_68K_GNU_VTENTRY", /* name */
118 FALSE, /* partial_inplace */
119 0, /* src_mask */
120 0, /* dst_mask */
121 FALSE),
124 static void
125 rtype_to_howto (abfd, cache_ptr, dst)
126 bfd *abfd ATTRIBUTE_UNUSED;
127 arelent *cache_ptr;
128 Elf_Internal_Rela *dst;
130 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K_max);
131 cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
134 #define elf_info_to_howto rtype_to_howto
136 static const struct
138 bfd_reloc_code_real_type bfd_val;
139 int elf_val;
140 } reloc_map[] = {
141 { BFD_RELOC_NONE, R_68K_NONE },
142 { BFD_RELOC_32, R_68K_32 },
143 { BFD_RELOC_16, R_68K_16 },
144 { BFD_RELOC_8, R_68K_8 },
145 { BFD_RELOC_32_PCREL, R_68K_PC32 },
146 { BFD_RELOC_16_PCREL, R_68K_PC16 },
147 { BFD_RELOC_8_PCREL, R_68K_PC8 },
148 { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
149 { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
150 { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
151 { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
152 { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
153 { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
154 { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
155 { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
156 { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
157 { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
158 { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
159 { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
160 { BFD_RELOC_NONE, R_68K_COPY },
161 { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
162 { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
163 { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
164 { BFD_RELOC_CTOR, R_68K_32 },
165 { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
166 { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
169 static reloc_howto_type *
170 reloc_type_lookup (abfd, code)
171 bfd *abfd ATTRIBUTE_UNUSED;
172 bfd_reloc_code_real_type code;
174 unsigned int i;
175 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
177 if (reloc_map[i].bfd_val == code)
178 return &howto_table[reloc_map[i].elf_val];
180 return 0;
183 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
184 #define ELF_ARCH bfd_arch_m68k
186 /* Functions for the m68k ELF linker. */
188 /* The name of the dynamic interpreter. This is put in the .interp
189 section. */
191 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
193 /* An instruction that contains a 32-bit PC-relative offset. */
195 struct elf_m68k_pcrel_insn
197 /* The address of the instruction. */
198 bfd_vma addr;
200 /* The offset of the PC-relative offset. */
201 bfd_vma field_offset;
204 /* Describes one of the PLT styles. */
206 struct elf_m68k_plt_info
208 /* The size of each entry. */
209 bfd_vma size;
211 /* The template for the first PLT entry. */
212 const bfd_byte *plt0_entry;
214 /* The PLT0_ENTRY instructions that contain PC-relative
215 .got + 4 and .got + 8 fields. */
216 struct elf_m68k_pcrel_insn plt0_got4_ptr;
217 struct elf_m68k_pcrel_insn plt0_got8_ptr;
219 /* The template for a symbol's PLT entry. */
220 const bfd_byte *symbol_entry;
222 /* The SYMBOL_ENTRY instruction that contains the PC-relative
223 .got.plt field. */
224 struct elf_m68k_pcrel_insn symbol_got_ptr;
226 /* The offset of the resolver stub from the start of SYMBOL_ENTRY. */
227 bfd_vma symbol_resolve_entry;
229 /* The SYMBOL_ENTRY instruction that branches to .plt. */
230 struct elf_m68k_pcrel_insn symbol_plt_ptr;
233 /* The size in bytes of an entry in the procedure linkage table. */
235 #define PLT_ENTRY_SIZE 20
237 /* The first entry in a procedure linkage table looks like this. See
238 the SVR4 ABI m68k supplement to see how this works. */
240 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
242 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
243 0, 0, 0, 0, /* replaced with offset to .got + 4. */
244 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
245 0, 0, 0, 0, /* replaced with offset to .got + 8. */
246 0, 0, 0, 0 /* pad out to 20 bytes. */
249 /* Subsequent entries in a procedure linkage table look like this. */
251 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
253 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
254 0, 0, 0, 0, /* replaced with offset to symbol's .got entry. */
255 0x2f, 0x3c, /* move.l #offset,-(%sp) */
256 0, 0, 0, 0, /* replaced with offset into relocation table. */
257 0x60, 0xff, /* bra.l .plt */
258 0, 0, 0, 0 /* replaced with offset to start of .plt. */
261 static const struct elf_m68k_plt_info elf_m68k_plt_info = {
262 PLT_ENTRY_SIZE,
263 elf_m68k_plt0_entry, { 0, 4 }, { 8, 4 },
264 elf_m68k_plt_entry, { 0, 4 }, 8, { 14, 2 }
267 #define ISAB_PLT_ENTRY_SIZE 24
269 static const bfd_byte elf_isab_plt0_entry[ISAB_PLT_ENTRY_SIZE] =
271 0x20, 0x3c, /* move.l #offset,%d0 */
272 0, 0, 0, 0, /* replaced with .got + 4 - . */
273 0x2f, 0x3b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),-(%sp) */
274 0x20, 0x3c, /* move.l #offset,%d0 */
275 0, 0, 0, 0, /* replaced with .got + 8 - . */
276 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
277 0x4e, 0xd0, /* jmp (%a0) */
278 0x4e, 0x71 /* nop */
281 /* Subsequent entries in a procedure linkage table look like this. */
283 static const bfd_byte elf_isab_plt_entry[ISAB_PLT_ENTRY_SIZE] =
285 0x20, 0x3c, /* move.l #offset,%d0 */
286 0, 0, 0, 0, /* replaced with (.got entry) - . */
287 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
288 0x4e, 0xd0, /* jmp (%a0) */
289 0x2f, 0x3c, /* move.l #offset,-(%sp) */
290 0, 0, 0, 0, /* replaced with offset into relocation table */
291 0x60, 0xff, /* bra.l .plt */
292 0, 0, 0, 0 /* replaced with .plt - . */
295 static const struct elf_m68k_plt_info elf_isab_plt_info = {
296 ISAB_PLT_ENTRY_SIZE,
297 elf_isab_plt0_entry, { 0, 2 }, { 10, 2 },
298 elf_isab_plt_entry, { 0, 2 }, 12, { 18, 2 }
301 #define CPU32_PLT_ENTRY_SIZE 24
302 /* Procedure linkage table entries for the cpu32 */
303 static const bfd_byte elf_cpu32_plt0_entry[CPU32_PLT_ENTRY_SIZE] =
305 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
306 0, 0, 0, 0, /* replaced with offset to .got + 4. */
307 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
308 0, 0, 0, 0, /* replace with offset to .got +8. */
309 0x4e, 0xd1, /* jmp %a1@ */
310 0, 0, 0, 0, /* pad out to 24 bytes. */
311 0, 0
314 static const bfd_byte elf_cpu32_plt_entry[CPU32_PLT_ENTRY_SIZE] =
316 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
317 0, 0, 0, 0, /* replaced with offset to symbol's .got entry. */
318 0x4e, 0xd1, /* jmp %a1@ */
319 0x2f, 0x3c, /* move.l #offset,-(%sp) */
320 0, 0, 0, 0, /* replaced with offset into relocation table. */
321 0x60, 0xff, /* bra.l .plt */
322 0, 0, 0, 0, /* replaced with offset to start of .plt. */
323 0, 0
326 static const struct elf_m68k_plt_info elf_cpu32_plt_info = {
327 CPU32_PLT_ENTRY_SIZE,
328 elf_cpu32_plt0_entry, { 0, 4 }, { 8, 4 },
329 elf_cpu32_plt_entry, { 0, 4 }, 10, { 16, 2 }
332 /* The m68k linker needs to keep track of the number of relocs that it
333 decides to copy in check_relocs for each symbol. This is so that it
334 can discard PC relative relocs if it doesn't need them when linking
335 with -Bsymbolic. We store the information in a field extending the
336 regular ELF linker hash table. */
338 /* This structure keeps track of the number of PC relative relocs we have
339 copied for a given symbol. */
341 struct elf_m68k_pcrel_relocs_copied
343 /* Next section. */
344 struct elf_m68k_pcrel_relocs_copied *next;
345 /* A section in dynobj. */
346 asection *section;
347 /* Number of relocs copied in this section. */
348 bfd_size_type count;
351 /* m68k ELF linker hash entry. */
353 struct elf_m68k_link_hash_entry
355 struct elf_link_hash_entry root;
357 /* Number of PC relative relocs copied for this symbol. */
358 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
361 #define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
363 /* m68k ELF linker hash table. */
365 struct elf_m68k_link_hash_table
367 struct elf_link_hash_table root;
369 /* Small local sym to section mapping cache. */
370 struct sym_sec_cache sym_sec;
372 /* The PLT format used by this link, or NULL if the format has not
373 yet been chosen. */
374 const struct elf_m68k_plt_info *plt_info;
377 /* Get the m68k ELF linker hash table from a link_info structure. */
379 #define elf_m68k_hash_table(p) \
380 ((struct elf_m68k_link_hash_table *) (p)->hash)
382 /* Create an entry in an m68k ELF linker hash table. */
384 static struct bfd_hash_entry *
385 elf_m68k_link_hash_newfunc (entry, table, string)
386 struct bfd_hash_entry *entry;
387 struct bfd_hash_table *table;
388 const char *string;
390 struct bfd_hash_entry *ret = entry;
392 /* Allocate the structure if it has not already been allocated by a
393 subclass. */
394 if (ret == NULL)
395 ret = bfd_hash_allocate (table,
396 sizeof (struct elf_m68k_link_hash_entry));
397 if (ret == NULL)
398 return ret;
400 /* Call the allocation method of the superclass. */
401 ret = _bfd_elf_link_hash_newfunc (ret, table, string);
402 if (ret != NULL)
403 elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
405 return ret;
408 /* Create an m68k ELF linker hash table. */
410 static struct bfd_link_hash_table *
411 elf_m68k_link_hash_table_create (abfd)
412 bfd *abfd;
414 struct elf_m68k_link_hash_table *ret;
415 bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
417 ret = (struct elf_m68k_link_hash_table *) bfd_malloc (amt);
418 if (ret == (struct elf_m68k_link_hash_table *) NULL)
419 return NULL;
421 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
422 elf_m68k_link_hash_newfunc,
423 sizeof (struct elf_m68k_link_hash_entry)))
425 free (ret);
426 return NULL;
429 ret->sym_sec.abfd = NULL;
430 ret->plt_info = NULL;
432 return &ret->root.root;
435 /* Set the right machine number. */
437 static bfd_boolean
438 elf32_m68k_object_p (bfd *abfd)
440 unsigned int mach = 0;
441 unsigned features = 0;
442 flagword eflags = elf_elfheader (abfd)->e_flags;
444 if (eflags & EF_M68K_M68000)
445 features |= m68000;
446 else if (eflags & EF_M68K_CPU32)
447 features |= cpu32;
448 else if (eflags & EF_M68K_ISA_MASK)
450 switch (eflags & EF_M68K_ISA_MASK)
452 case EF_M68K_ISA_A_NODIV:
453 features |= mcfisa_a;
454 break;
455 case EF_M68K_ISA_A:
456 features |= mcfisa_a|mcfhwdiv;
457 break;
458 case EF_M68K_ISA_A_PLUS:
459 features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
460 break;
461 case EF_M68K_ISA_B_NOUSP:
462 features |= mcfisa_a|mcfisa_b|mcfhwdiv;
463 break;
464 case EF_M68K_ISA_B:
465 features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
466 break;
468 switch (eflags & EF_M68K_MAC_MASK)
470 case EF_M68K_MAC:
471 features |= mcfmac;
472 break;
473 case EF_M68K_EMAC:
474 features |= mcfemac;
475 break;
477 if (eflags & EF_M68K_FLOAT)
478 features |= cfloat;
481 mach = bfd_m68k_features_to_mach (features);
482 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
484 return TRUE;
487 /* Keep m68k-specific flags in the ELF header. */
488 static bfd_boolean
489 elf32_m68k_set_private_flags (abfd, flags)
490 bfd *abfd;
491 flagword flags;
493 elf_elfheader (abfd)->e_flags = flags;
494 elf_flags_init (abfd) = TRUE;
495 return TRUE;
498 /* Merge backend specific data from an object file to the output
499 object file when linking. */
500 static bfd_boolean
501 elf32_m68k_merge_private_bfd_data (ibfd, obfd)
502 bfd *ibfd;
503 bfd *obfd;
505 flagword out_flags;
506 flagword in_flags;
507 flagword out_isa;
508 flagword in_isa;
509 const bfd_arch_info_type *arch_info;
511 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
512 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
513 return FALSE;
515 /* Get the merged machine. This checks for incompatibility between
516 Coldfire & non-Coldfire flags, incompability between different
517 Coldfire ISAs, and incompability between different MAC types. */
518 arch_info = bfd_arch_get_compatible (ibfd, obfd, FALSE);
519 if (!arch_info)
520 return FALSE;
522 bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
524 in_flags = elf_elfheader (ibfd)->e_flags;
525 if (!elf_flags_init (obfd))
527 elf_flags_init (obfd) = TRUE;
528 out_flags = in_flags;
530 else
532 out_flags = elf_elfheader (obfd)->e_flags;
533 in_isa = (in_flags & EF_M68K_ISA_MASK);
534 out_isa = (out_flags & EF_M68K_ISA_MASK);
535 if (in_isa > out_isa)
536 out_flags ^= in_isa ^ out_isa;
537 out_flags |= in_flags ^ in_isa;
539 elf_elfheader (obfd)->e_flags = out_flags;
541 return TRUE;
544 /* Display the flags field. */
545 static bfd_boolean
546 elf32_m68k_print_private_bfd_data (abfd, ptr)
547 bfd *abfd;
548 PTR ptr;
550 FILE *file = (FILE *) ptr;
551 flagword eflags = elf_elfheader (abfd)->e_flags;
553 BFD_ASSERT (abfd != NULL && ptr != NULL);
555 /* Print normal ELF private data. */
556 _bfd_elf_print_private_bfd_data (abfd, ptr);
558 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
560 /* xgettext:c-format */
561 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
563 if (eflags & EF_M68K_CPU32)
564 fprintf (file, " [cpu32]");
566 if (eflags & EF_M68K_M68000)
567 fprintf (file, " [m68000]");
569 if (eflags & EF_M68K_CFV4E)
570 fprintf (file, " [cfv4e]");
572 if (eflags & EF_M68K_ISA_MASK)
574 char const *isa = _("unknown");
575 char const *mac = _("unknown");
576 char const *additional = "";
578 switch (eflags & EF_M68K_ISA_MASK)
580 case EF_M68K_ISA_A_NODIV:
581 isa = "A";
582 additional = " [nodiv]";
583 break;
584 case EF_M68K_ISA_A:
585 isa = "A";
586 break;
587 case EF_M68K_ISA_A_PLUS:
588 isa = "A+";
589 break;
590 case EF_M68K_ISA_B_NOUSP:
591 isa = "B";
592 additional = " [nousp]";
593 break;
594 case EF_M68K_ISA_B:
595 isa = "B";
596 break;
598 fprintf (file, " [isa %s]%s", isa, additional);
599 if (eflags & EF_M68K_FLOAT)
600 fprintf (file, " [float]");
601 switch (eflags & EF_M68K_MAC_MASK)
603 case 0:
604 mac = NULL;
605 break;
606 case EF_M68K_MAC:
607 mac = "mac";
608 break;
609 case EF_M68K_EMAC:
610 mac = "emac";
611 break;
613 if (mac)
614 fprintf (file, " [%s]", mac);
617 fputc ('\n', file);
619 return TRUE;
621 /* Look through the relocs for a section during the first phase, and
622 allocate space in the global offset table or procedure linkage
623 table. */
625 static bfd_boolean
626 elf_m68k_check_relocs (abfd, info, sec, relocs)
627 bfd *abfd;
628 struct bfd_link_info *info;
629 asection *sec;
630 const Elf_Internal_Rela *relocs;
632 bfd *dynobj;
633 Elf_Internal_Shdr *symtab_hdr;
634 struct elf_link_hash_entry **sym_hashes;
635 bfd_signed_vma *local_got_refcounts;
636 const Elf_Internal_Rela *rel;
637 const Elf_Internal_Rela *rel_end;
638 asection *sgot;
639 asection *srelgot;
640 asection *sreloc;
642 if (info->relocatable)
643 return TRUE;
645 dynobj = elf_hash_table (info)->dynobj;
646 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
647 sym_hashes = elf_sym_hashes (abfd);
648 local_got_refcounts = elf_local_got_refcounts (abfd);
650 sgot = NULL;
651 srelgot = NULL;
652 sreloc = NULL;
654 rel_end = relocs + sec->reloc_count;
655 for (rel = relocs; rel < rel_end; rel++)
657 unsigned long r_symndx;
658 struct elf_link_hash_entry *h;
660 r_symndx = ELF32_R_SYM (rel->r_info);
662 if (r_symndx < symtab_hdr->sh_info)
663 h = NULL;
664 else
666 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
667 while (h->root.type == bfd_link_hash_indirect
668 || h->root.type == bfd_link_hash_warning)
669 h = (struct elf_link_hash_entry *) h->root.u.i.link;
672 switch (ELF32_R_TYPE (rel->r_info))
674 case R_68K_GOT8:
675 case R_68K_GOT16:
676 case R_68K_GOT32:
677 if (h != NULL
678 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
679 break;
680 /* Fall through. */
681 case R_68K_GOT8O:
682 case R_68K_GOT16O:
683 case R_68K_GOT32O:
684 /* This symbol requires a global offset table entry. */
686 if (dynobj == NULL)
688 /* Create the .got section. */
689 elf_hash_table (info)->dynobj = dynobj = abfd;
690 if (!_bfd_elf_create_got_section (dynobj, info))
691 return FALSE;
694 if (sgot == NULL)
696 sgot = bfd_get_section_by_name (dynobj, ".got");
697 BFD_ASSERT (sgot != NULL);
700 if (srelgot == NULL
701 && (h != NULL || info->shared))
703 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
704 if (srelgot == NULL)
706 srelgot = bfd_make_section_with_flags (dynobj,
707 ".rela.got",
708 (SEC_ALLOC
709 | SEC_LOAD
710 | SEC_HAS_CONTENTS
711 | SEC_IN_MEMORY
712 | SEC_LINKER_CREATED
713 | SEC_READONLY));
714 if (srelgot == NULL
715 || !bfd_set_section_alignment (dynobj, srelgot, 2))
716 return FALSE;
720 if (h != NULL)
722 if (h->got.refcount == 0)
724 /* Make sure this symbol is output as a dynamic symbol. */
725 if (h->dynindx == -1
726 && !h->forced_local)
728 if (!bfd_elf_link_record_dynamic_symbol (info, h))
729 return FALSE;
732 /* Allocate space in the .got section. */
733 sgot->size += 4;
734 /* Allocate relocation space. */
735 srelgot->size += sizeof (Elf32_External_Rela);
737 h->got.refcount++;
739 else
741 /* This is a global offset table entry for a local symbol. */
742 if (local_got_refcounts == NULL)
744 bfd_size_type size;
746 size = symtab_hdr->sh_info;
747 size *= sizeof (bfd_signed_vma);
748 local_got_refcounts = ((bfd_signed_vma *)
749 bfd_zalloc (abfd, size));
750 if (local_got_refcounts == NULL)
751 return FALSE;
752 elf_local_got_refcounts (abfd) = local_got_refcounts;
754 if (local_got_refcounts[r_symndx] == 0)
756 sgot->size += 4;
757 if (info->shared)
759 /* If we are generating a shared object, we need to
760 output a R_68K_RELATIVE reloc so that the dynamic
761 linker can adjust this GOT entry. */
762 srelgot->size += sizeof (Elf32_External_Rela);
765 local_got_refcounts[r_symndx]++;
767 break;
769 case R_68K_PLT8:
770 case R_68K_PLT16:
771 case R_68K_PLT32:
772 /* This symbol requires a procedure linkage table entry. We
773 actually build the entry in adjust_dynamic_symbol,
774 because this might be a case of linking PIC code which is
775 never referenced by a dynamic object, in which case we
776 don't need to generate a procedure linkage table entry
777 after all. */
779 /* If this is a local symbol, we resolve it directly without
780 creating a procedure linkage table entry. */
781 if (h == NULL)
782 continue;
784 h->needs_plt = 1;
785 h->plt.refcount++;
786 break;
788 case R_68K_PLT8O:
789 case R_68K_PLT16O:
790 case R_68K_PLT32O:
791 /* This symbol requires a procedure linkage table entry. */
793 if (h == NULL)
795 /* It does not make sense to have this relocation for a
796 local symbol. FIXME: does it? How to handle it if
797 it does make sense? */
798 bfd_set_error (bfd_error_bad_value);
799 return FALSE;
802 /* Make sure this symbol is output as a dynamic symbol. */
803 if (h->dynindx == -1
804 && !h->forced_local)
806 if (!bfd_elf_link_record_dynamic_symbol (info, h))
807 return FALSE;
810 h->needs_plt = 1;
811 h->plt.refcount++;
812 break;
814 case R_68K_PC8:
815 case R_68K_PC16:
816 case R_68K_PC32:
817 /* If we are creating a shared library and this is not a local
818 symbol, we need to copy the reloc into the shared library.
819 However when linking with -Bsymbolic and this is a global
820 symbol which is defined in an object we are including in the
821 link (i.e., DEF_REGULAR is set), then we can resolve the
822 reloc directly. At this point we have not seen all the input
823 files, so it is possible that DEF_REGULAR is not set now but
824 will be set later (it is never cleared). We account for that
825 possibility below by storing information in the
826 pcrel_relocs_copied field of the hash table entry. */
827 if (!(info->shared
828 && (sec->flags & SEC_ALLOC) != 0
829 && h != NULL
830 && (!info->symbolic
831 || h->root.type == bfd_link_hash_defweak
832 || !h->def_regular)))
834 if (h != NULL)
836 /* Make sure a plt entry is created for this symbol if
837 it turns out to be a function defined by a dynamic
838 object. */
839 h->plt.refcount++;
841 break;
843 /* Fall through. */
844 case R_68K_8:
845 case R_68K_16:
846 case R_68K_32:
847 if (h != NULL)
849 /* Make sure a plt entry is created for this symbol if it
850 turns out to be a function defined by a dynamic object. */
851 h->plt.refcount++;
854 /* If we are creating a shared library, we need to copy the
855 reloc into the shared library. */
856 if (info->shared
857 && (sec->flags & SEC_ALLOC) != 0)
859 /* When creating a shared object, we must copy these
860 reloc types into the output file. We create a reloc
861 section in dynobj and make room for this reloc. */
862 if (sreloc == NULL)
864 const char *name;
866 name = (bfd_elf_string_from_elf_section
867 (abfd,
868 elf_elfheader (abfd)->e_shstrndx,
869 elf_section_data (sec)->rel_hdr.sh_name));
870 if (name == NULL)
871 return FALSE;
873 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
874 && strcmp (bfd_get_section_name (abfd, sec),
875 name + 5) == 0);
877 sreloc = bfd_get_section_by_name (dynobj, name);
878 if (sreloc == NULL)
880 sreloc = bfd_make_section_with_flags (dynobj,
881 name,
882 (SEC_ALLOC
883 | SEC_LOAD
884 | SEC_HAS_CONTENTS
885 | SEC_IN_MEMORY
886 | SEC_LINKER_CREATED
887 | SEC_READONLY));
888 if (sreloc == NULL
889 || !bfd_set_section_alignment (dynobj, sreloc, 2))
890 return FALSE;
892 elf_section_data (sec)->sreloc = sreloc;
895 if (sec->flags & SEC_READONLY
896 /* Don't set DF_TEXTREL yet for PC relative
897 relocations, they might be discarded later. */
898 && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
899 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
900 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
901 info->flags |= DF_TEXTREL;
903 sreloc->size += sizeof (Elf32_External_Rela);
905 /* We count the number of PC relative relocations we have
906 entered for this symbol, so that we can discard them
907 again if, in the -Bsymbolic case, the symbol is later
908 defined by a regular object, or, in the normal shared
909 case, the symbol is forced to be local. Note that this
910 function is only called if we are using an m68kelf linker
911 hash table, which means that h is really a pointer to an
912 elf_m68k_link_hash_entry. */
913 if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
914 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
915 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
917 struct elf_m68k_pcrel_relocs_copied *p;
918 struct elf_m68k_pcrel_relocs_copied **head;
920 if (h != NULL)
922 struct elf_m68k_link_hash_entry *eh
923 = elf_m68k_hash_entry (h);
924 head = &eh->pcrel_relocs_copied;
926 else
928 asection *s;
929 void *vpp;
931 s = (bfd_section_from_r_symndx
932 (abfd, &elf_m68k_hash_table (info)->sym_sec,
933 sec, r_symndx));
934 if (s == NULL)
935 return FALSE;
937 vpp = &elf_section_data (s)->local_dynrel;
938 head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
941 for (p = *head; p != NULL; p = p->next)
942 if (p->section == sreloc)
943 break;
945 if (p == NULL)
947 p = ((struct elf_m68k_pcrel_relocs_copied *)
948 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
949 if (p == NULL)
950 return FALSE;
951 p->next = *head;
952 *head = p;
953 p->section = sreloc;
954 p->count = 0;
957 ++p->count;
961 break;
963 /* This relocation describes the C++ object vtable hierarchy.
964 Reconstruct it for later use during GC. */
965 case R_68K_GNU_VTINHERIT:
966 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
967 return FALSE;
968 break;
970 /* This relocation describes which C++ vtable entries are actually
971 used. Record for later use during GC. */
972 case R_68K_GNU_VTENTRY:
973 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
974 return FALSE;
975 break;
977 default:
978 break;
982 return TRUE;
985 /* Return the section that should be marked against GC for a given
986 relocation. */
988 static asection *
989 elf_m68k_gc_mark_hook (sec, info, rel, h, sym)
990 asection *sec;
991 struct bfd_link_info *info ATTRIBUTE_UNUSED;
992 Elf_Internal_Rela *rel;
993 struct elf_link_hash_entry *h;
994 Elf_Internal_Sym *sym;
996 if (h != NULL)
998 switch (ELF32_R_TYPE (rel->r_info))
1000 case R_68K_GNU_VTINHERIT:
1001 case R_68K_GNU_VTENTRY:
1002 break;
1004 default:
1005 switch (h->root.type)
1007 default:
1008 break;
1010 case bfd_link_hash_defined:
1011 case bfd_link_hash_defweak:
1012 return h->root.u.def.section;
1014 case bfd_link_hash_common:
1015 return h->root.u.c.p->section;
1019 else
1020 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1022 return NULL;
1025 /* Update the got entry reference counts for the section being removed. */
1027 static bfd_boolean
1028 elf_m68k_gc_sweep_hook (abfd, info, sec, relocs)
1029 bfd *abfd;
1030 struct bfd_link_info *info;
1031 asection *sec;
1032 const Elf_Internal_Rela *relocs;
1034 Elf_Internal_Shdr *symtab_hdr;
1035 struct elf_link_hash_entry **sym_hashes;
1036 bfd_signed_vma *local_got_refcounts;
1037 const Elf_Internal_Rela *rel, *relend;
1038 bfd *dynobj;
1039 asection *sgot;
1040 asection *srelgot;
1042 dynobj = elf_hash_table (info)->dynobj;
1043 if (dynobj == NULL)
1044 return TRUE;
1046 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1047 sym_hashes = elf_sym_hashes (abfd);
1048 local_got_refcounts = elf_local_got_refcounts (abfd);
1050 sgot = bfd_get_section_by_name (dynobj, ".got");
1051 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1053 relend = relocs + sec->reloc_count;
1054 for (rel = relocs; rel < relend; rel++)
1056 unsigned long r_symndx;
1057 struct elf_link_hash_entry *h = NULL;
1059 r_symndx = ELF32_R_SYM (rel->r_info);
1060 if (r_symndx >= symtab_hdr->sh_info)
1062 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1063 while (h->root.type == bfd_link_hash_indirect
1064 || h->root.type == bfd_link_hash_warning)
1065 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1068 switch (ELF32_R_TYPE (rel->r_info))
1070 case R_68K_GOT8:
1071 case R_68K_GOT16:
1072 case R_68K_GOT32:
1073 case R_68K_GOT8O:
1074 case R_68K_GOT16O:
1075 case R_68K_GOT32O:
1076 if (h != NULL)
1078 if (h->got.refcount > 0)
1080 --h->got.refcount;
1081 if (h->got.refcount == 0)
1083 /* We don't need the .got entry any more. */
1084 sgot->size -= 4;
1085 srelgot->size -= sizeof (Elf32_External_Rela);
1089 else if (local_got_refcounts != NULL)
1091 if (local_got_refcounts[r_symndx] > 0)
1093 --local_got_refcounts[r_symndx];
1094 if (local_got_refcounts[r_symndx] == 0)
1096 /* We don't need the .got entry any more. */
1097 sgot->size -= 4;
1098 if (info->shared)
1099 srelgot->size -= sizeof (Elf32_External_Rela);
1103 break;
1105 case R_68K_PLT8:
1106 case R_68K_PLT16:
1107 case R_68K_PLT32:
1108 case R_68K_PLT8O:
1109 case R_68K_PLT16O:
1110 case R_68K_PLT32O:
1111 case R_68K_PC8:
1112 case R_68K_PC16:
1113 case R_68K_PC32:
1114 case R_68K_8:
1115 case R_68K_16:
1116 case R_68K_32:
1117 if (h != NULL)
1119 if (h->plt.refcount > 0)
1120 --h->plt.refcount;
1122 break;
1124 default:
1125 break;
1129 return TRUE;
1132 /* Return the type of PLT associated with OUTPUT_BFD. */
1134 static const struct elf_m68k_plt_info *
1135 elf_m68k_get_plt_info (bfd *output_bfd)
1137 unsigned int features;
1139 features = bfd_m68k_mach_to_features (bfd_get_mach (output_bfd));
1140 if (features & cpu32)
1141 return &elf_cpu32_plt_info;
1142 if (features & mcfisa_b)
1143 return &elf_isab_plt_info;
1144 return &elf_m68k_plt_info;
1147 /* This function is called after all the input files have been read,
1148 and the input sections have been assigned to output sections.
1149 It's a convenient place to determine the PLT style. */
1151 static bfd_boolean
1152 elf_m68k_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
1154 elf_m68k_hash_table (info)->plt_info = elf_m68k_get_plt_info (output_bfd);
1155 return TRUE;
1158 /* Adjust a symbol defined by a dynamic object and referenced by a
1159 regular object. The current definition is in some section of the
1160 dynamic object, but we're not including those sections. We have to
1161 change the definition to something the rest of the link can
1162 understand. */
1164 static bfd_boolean
1165 elf_m68k_adjust_dynamic_symbol (info, h)
1166 struct bfd_link_info *info;
1167 struct elf_link_hash_entry *h;
1169 struct elf_m68k_link_hash_table *htab;
1170 bfd *dynobj;
1171 asection *s;
1172 unsigned int power_of_two;
1174 htab = elf_m68k_hash_table (info);
1175 dynobj = elf_hash_table (info)->dynobj;
1177 /* Make sure we know what is going on here. */
1178 BFD_ASSERT (dynobj != NULL
1179 && (h->needs_plt
1180 || h->u.weakdef != NULL
1181 || (h->def_dynamic
1182 && h->ref_regular
1183 && !h->def_regular)));
1185 /* If this is a function, put it in the procedure linkage table. We
1186 will fill in the contents of the procedure linkage table later,
1187 when we know the address of the .got section. */
1188 if (h->type == STT_FUNC
1189 || h->needs_plt)
1191 if ((h->plt.refcount <= 0
1192 || SYMBOL_CALLS_LOCAL (info, h)
1193 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1194 && h->root.type == bfd_link_hash_undefweak))
1195 /* We must always create the plt entry if it was referenced
1196 by a PLTxxO relocation. In this case we already recorded
1197 it as a dynamic symbol. */
1198 && h->dynindx == -1)
1200 /* This case can occur if we saw a PLTxx reloc in an input
1201 file, but the symbol was never referred to by a dynamic
1202 object, or if all references were garbage collected. In
1203 such a case, we don't actually need to build a procedure
1204 linkage table, and we can just do a PCxx reloc instead. */
1205 h->plt.offset = (bfd_vma) -1;
1206 h->needs_plt = 0;
1207 return TRUE;
1210 /* Make sure this symbol is output as a dynamic symbol. */
1211 if (h->dynindx == -1
1212 && !h->forced_local)
1214 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1215 return FALSE;
1218 s = bfd_get_section_by_name (dynobj, ".plt");
1219 BFD_ASSERT (s != NULL);
1221 /* If this is the first .plt entry, make room for the special
1222 first entry. */
1223 if (s->size == 0)
1224 s->size = htab->plt_info->size;
1226 /* If this symbol is not defined in a regular file, and we are
1227 not generating a shared library, then set the symbol to this
1228 location in the .plt. This is required to make function
1229 pointers compare as equal between the normal executable and
1230 the shared library. */
1231 if (!info->shared
1232 && !h->def_regular)
1234 h->root.u.def.section = s;
1235 h->root.u.def.value = s->size;
1238 h->plt.offset = s->size;
1240 /* Make room for this entry. */
1241 s->size += htab->plt_info->size;
1243 /* We also need to make an entry in the .got.plt section, which
1244 will be placed in the .got section by the linker script. */
1245 s = bfd_get_section_by_name (dynobj, ".got.plt");
1246 BFD_ASSERT (s != NULL);
1247 s->size += 4;
1249 /* We also need to make an entry in the .rela.plt section. */
1250 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1251 BFD_ASSERT (s != NULL);
1252 s->size += sizeof (Elf32_External_Rela);
1254 return TRUE;
1257 /* Reinitialize the plt offset now that it is not used as a reference
1258 count any more. */
1259 h->plt.offset = (bfd_vma) -1;
1261 /* If this is a weak symbol, and there is a real definition, the
1262 processor independent code will have arranged for us to see the
1263 real definition first, and we can just use the same value. */
1264 if (h->u.weakdef != NULL)
1266 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1267 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1268 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1269 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1270 return TRUE;
1273 /* This is a reference to a symbol defined by a dynamic object which
1274 is not a function. */
1276 /* If we are creating a shared library, we must presume that the
1277 only references to the symbol are via the global offset table.
1278 For such cases we need not do anything here; the relocations will
1279 be handled correctly by relocate_section. */
1280 if (info->shared)
1281 return TRUE;
1283 if (h->size == 0)
1285 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1286 h->root.root.string);
1287 return TRUE;
1290 /* We must allocate the symbol in our .dynbss section, which will
1291 become part of the .bss section of the executable. There will be
1292 an entry for this symbol in the .dynsym section. The dynamic
1293 object will contain position independent code, so all references
1294 from the dynamic object to this symbol will go through the global
1295 offset table. The dynamic linker will use the .dynsym entry to
1296 determine the address it must put in the global offset table, so
1297 both the dynamic object and the regular object will refer to the
1298 same memory location for the variable. */
1300 s = bfd_get_section_by_name (dynobj, ".dynbss");
1301 BFD_ASSERT (s != NULL);
1303 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1304 copy the initial value out of the dynamic object and into the
1305 runtime process image. We need to remember the offset into the
1306 .rela.bss section we are going to use. */
1307 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1309 asection *srel;
1311 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1312 BFD_ASSERT (srel != NULL);
1313 srel->size += sizeof (Elf32_External_Rela);
1314 h->needs_copy = 1;
1317 /* We need to figure out the alignment required for this symbol. I
1318 have no idea how ELF linkers handle this. */
1319 power_of_two = bfd_log2 (h->size);
1320 if (power_of_two > 3)
1321 power_of_two = 3;
1323 /* Apply the required alignment. */
1324 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1325 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1327 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
1328 return FALSE;
1331 /* Define the symbol as being at this point in the section. */
1332 h->root.u.def.section = s;
1333 h->root.u.def.value = s->size;
1335 /* Increment the section size to make room for the symbol. */
1336 s->size += h->size;
1338 return TRUE;
1341 /* Set the sizes of the dynamic sections. */
1343 static bfd_boolean
1344 elf_m68k_size_dynamic_sections (output_bfd, info)
1345 bfd *output_bfd ATTRIBUTE_UNUSED;
1346 struct bfd_link_info *info;
1348 bfd *dynobj;
1349 asection *s;
1350 bfd_boolean plt;
1351 bfd_boolean relocs;
1353 dynobj = elf_hash_table (info)->dynobj;
1354 BFD_ASSERT (dynobj != NULL);
1356 if (elf_hash_table (info)->dynamic_sections_created)
1358 /* Set the contents of the .interp section to the interpreter. */
1359 if (info->executable)
1361 s = bfd_get_section_by_name (dynobj, ".interp");
1362 BFD_ASSERT (s != NULL);
1363 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1364 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1367 else
1369 /* We may have created entries in the .rela.got section.
1370 However, if we are not creating the dynamic sections, we will
1371 not actually use these entries. Reset the size of .rela.got,
1372 which will cause it to get stripped from the output file
1373 below. */
1374 s = bfd_get_section_by_name (dynobj, ".rela.got");
1375 if (s != NULL)
1376 s->size = 0;
1379 /* If this is a -Bsymbolic shared link, then we need to discard all
1380 PC relative relocs against symbols defined in a regular object.
1381 For the normal shared case we discard the PC relative relocs
1382 against symbols that have become local due to visibility changes.
1383 We allocated space for them in the check_relocs routine, but we
1384 will not fill them in in the relocate_section routine. */
1385 if (info->shared)
1386 elf_link_hash_traverse (elf_hash_table (info),
1387 elf_m68k_discard_copies,
1388 (PTR) info);
1390 /* The check_relocs and adjust_dynamic_symbol entry points have
1391 determined the sizes of the various dynamic sections. Allocate
1392 memory for them. */
1393 plt = FALSE;
1394 relocs = FALSE;
1395 for (s = dynobj->sections; s != NULL; s = s->next)
1397 const char *name;
1399 if ((s->flags & SEC_LINKER_CREATED) == 0)
1400 continue;
1402 /* It's OK to base decisions on the section name, because none
1403 of the dynobj section names depend upon the input files. */
1404 name = bfd_get_section_name (dynobj, s);
1406 if (strcmp (name, ".plt") == 0)
1408 /* Remember whether there is a PLT. */
1409 plt = s->size != 0;
1411 else if (strncmp (name, ".rela", 5) == 0)
1413 if (s->size != 0)
1415 relocs = TRUE;
1417 /* We use the reloc_count field as a counter if we need
1418 to copy relocs into the output file. */
1419 s->reloc_count = 0;
1422 else if (strncmp (name, ".got", 4) != 0
1423 && strcmp (name, ".dynbss") != 0)
1425 /* It's not one of our sections, so don't allocate space. */
1426 continue;
1429 if (s->size == 0)
1431 /* If we don't need this section, strip it from the
1432 output file. This is mostly to handle .rela.bss and
1433 .rela.plt. We must create both sections in
1434 create_dynamic_sections, because they must be created
1435 before the linker maps input sections to output
1436 sections. The linker does that before
1437 adjust_dynamic_symbol is called, and it is that
1438 function which decides whether anything needs to go
1439 into these sections. */
1440 s->flags |= SEC_EXCLUDE;
1441 continue;
1444 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1445 continue;
1447 /* Allocate memory for the section contents. */
1448 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1449 Unused entries should be reclaimed before the section's contents
1450 are written out, but at the moment this does not happen. Thus in
1451 order to prevent writing out garbage, we initialise the section's
1452 contents to zero. */
1453 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1454 if (s->contents == NULL)
1455 return FALSE;
1458 if (elf_hash_table (info)->dynamic_sections_created)
1460 /* Add some entries to the .dynamic section. We fill in the
1461 values later, in elf_m68k_finish_dynamic_sections, but we
1462 must add the entries now so that we get the correct size for
1463 the .dynamic section. The DT_DEBUG entry is filled in by the
1464 dynamic linker and used by the debugger. */
1465 #define add_dynamic_entry(TAG, VAL) \
1466 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1468 if (!info->shared)
1470 if (!add_dynamic_entry (DT_DEBUG, 0))
1471 return FALSE;
1474 if (plt)
1476 if (!add_dynamic_entry (DT_PLTGOT, 0)
1477 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1478 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1479 || !add_dynamic_entry (DT_JMPREL, 0))
1480 return FALSE;
1483 if (relocs)
1485 if (!add_dynamic_entry (DT_RELA, 0)
1486 || !add_dynamic_entry (DT_RELASZ, 0)
1487 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1488 return FALSE;
1491 if ((info->flags & DF_TEXTREL) != 0)
1493 if (!add_dynamic_entry (DT_TEXTREL, 0))
1494 return FALSE;
1497 #undef add_dynamic_entry
1499 return TRUE;
1502 /* This function is called via elf_link_hash_traverse if we are
1503 creating a shared object. In the -Bsymbolic case it discards the
1504 space allocated to copy PC relative relocs against symbols which
1505 are defined in regular objects. For the normal shared case, it
1506 discards space for pc-relative relocs that have become local due to
1507 symbol visibility changes. We allocated space for them in the
1508 check_relocs routine, but we won't fill them in in the
1509 relocate_section routine.
1511 We also check whether any of the remaining relocations apply
1512 against a readonly section, and set the DF_TEXTREL flag in this
1513 case. */
1515 static bfd_boolean
1516 elf_m68k_discard_copies (h, inf)
1517 struct elf_link_hash_entry *h;
1518 PTR inf;
1520 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1521 struct elf_m68k_pcrel_relocs_copied *s;
1523 if (h->root.type == bfd_link_hash_warning)
1524 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1526 if (!h->def_regular
1527 || (!info->symbolic
1528 && !h->forced_local))
1530 if ((info->flags & DF_TEXTREL) == 0)
1532 /* Look for relocations against read-only sections. */
1533 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1534 s != NULL;
1535 s = s->next)
1536 if ((s->section->flags & SEC_READONLY) != 0)
1538 info->flags |= DF_TEXTREL;
1539 break;
1543 return TRUE;
1546 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1547 s != NULL;
1548 s = s->next)
1549 s->section->size -= s->count * sizeof (Elf32_External_Rela);
1551 return TRUE;
1554 /* Relocate an M68K ELF section. */
1556 static bfd_boolean
1557 elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1558 contents, relocs, local_syms, local_sections)
1559 bfd *output_bfd;
1560 struct bfd_link_info *info;
1561 bfd *input_bfd;
1562 asection *input_section;
1563 bfd_byte *contents;
1564 Elf_Internal_Rela *relocs;
1565 Elf_Internal_Sym *local_syms;
1566 asection **local_sections;
1568 bfd *dynobj;
1569 Elf_Internal_Shdr *symtab_hdr;
1570 struct elf_link_hash_entry **sym_hashes;
1571 bfd_vma *local_got_offsets;
1572 asection *sgot;
1573 asection *splt;
1574 asection *sreloc;
1575 Elf_Internal_Rela *rel;
1576 Elf_Internal_Rela *relend;
1578 if (info->relocatable)
1579 return TRUE;
1581 dynobj = elf_hash_table (info)->dynobj;
1582 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1583 sym_hashes = elf_sym_hashes (input_bfd);
1584 local_got_offsets = elf_local_got_offsets (input_bfd);
1586 sgot = NULL;
1587 splt = NULL;
1588 sreloc = NULL;
1590 rel = relocs;
1591 relend = relocs + input_section->reloc_count;
1592 for (; rel < relend; rel++)
1594 int r_type;
1595 reloc_howto_type *howto;
1596 unsigned long r_symndx;
1597 struct elf_link_hash_entry *h;
1598 Elf_Internal_Sym *sym;
1599 asection *sec;
1600 bfd_vma relocation;
1601 bfd_boolean unresolved_reloc;
1602 bfd_reloc_status_type r;
1604 r_type = ELF32_R_TYPE (rel->r_info);
1605 if (r_type < 0 || r_type >= (int) R_68K_max)
1607 bfd_set_error (bfd_error_bad_value);
1608 return FALSE;
1610 howto = howto_table + r_type;
1612 r_symndx = ELF32_R_SYM (rel->r_info);
1614 h = NULL;
1615 sym = NULL;
1616 sec = NULL;
1617 unresolved_reloc = FALSE;
1619 if (r_symndx < symtab_hdr->sh_info)
1621 sym = local_syms + r_symndx;
1622 sec = local_sections[r_symndx];
1623 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1625 else
1627 bfd_boolean warned;
1629 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1630 r_symndx, symtab_hdr, sym_hashes,
1631 h, sec, relocation,
1632 unresolved_reloc, warned);
1635 switch (r_type)
1637 case R_68K_GOT8:
1638 case R_68K_GOT16:
1639 case R_68K_GOT32:
1640 /* Relocation is to the address of the entry for this symbol
1641 in the global offset table. */
1642 if (h != NULL
1643 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1644 break;
1645 /* Fall through. */
1646 case R_68K_GOT8O:
1647 case R_68K_GOT16O:
1648 case R_68K_GOT32O:
1649 /* Relocation is the offset of the entry for this symbol in
1650 the global offset table. */
1653 bfd_vma off;
1655 if (sgot == NULL)
1657 sgot = bfd_get_section_by_name (dynobj, ".got");
1658 BFD_ASSERT (sgot != NULL);
1661 if (h != NULL)
1663 bfd_boolean dyn;
1665 off = h->got.offset;
1666 BFD_ASSERT (off != (bfd_vma) -1);
1668 dyn = elf_hash_table (info)->dynamic_sections_created;
1669 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1670 || (info->shared
1671 && (info->symbolic
1672 || h->dynindx == -1
1673 || h->forced_local)
1674 && h->def_regular))
1676 /* This is actually a static link, or it is a
1677 -Bsymbolic link and the symbol is defined
1678 locally, or the symbol was forced to be local
1679 because of a version file.. We must initialize
1680 this entry in the global offset table. Since
1681 the offset must always be a multiple of 4, we
1682 use the least significant bit to record whether
1683 we have initialized it already.
1685 When doing a dynamic link, we create a .rela.got
1686 relocation entry to initialize the value. This
1687 is done in the finish_dynamic_symbol routine. */
1688 if ((off & 1) != 0)
1689 off &= ~1;
1690 else
1692 bfd_put_32 (output_bfd, relocation,
1693 sgot->contents + off);
1694 h->got.offset |= 1;
1697 else
1698 unresolved_reloc = FALSE;
1700 else
1702 BFD_ASSERT (local_got_offsets != NULL
1703 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1705 off = local_got_offsets[r_symndx];
1707 /* The offset must always be a multiple of 4. We use
1708 the least significant bit to record whether we have
1709 already generated the necessary reloc. */
1710 if ((off & 1) != 0)
1711 off &= ~1;
1712 else
1714 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1716 if (info->shared)
1718 asection *s;
1719 Elf_Internal_Rela outrel;
1720 bfd_byte *loc;
1722 s = bfd_get_section_by_name (dynobj, ".rela.got");
1723 BFD_ASSERT (s != NULL);
1725 outrel.r_offset = (sgot->output_section->vma
1726 + sgot->output_offset
1727 + off);
1728 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1729 outrel.r_addend = relocation;
1730 loc = s->contents;
1731 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1732 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1735 local_got_offsets[r_symndx] |= 1;
1739 relocation = sgot->output_offset + off;
1740 if (r_type == R_68K_GOT8O
1741 || r_type == R_68K_GOT16O
1742 || r_type == R_68K_GOT32O)
1744 /* This relocation does not use the addend. */
1745 rel->r_addend = 0;
1747 else
1748 relocation += sgot->output_section->vma;
1750 break;
1752 case R_68K_PLT8:
1753 case R_68K_PLT16:
1754 case R_68K_PLT32:
1755 /* Relocation is to the entry for this symbol in the
1756 procedure linkage table. */
1758 /* Resolve a PLTxx reloc against a local symbol directly,
1759 without using the procedure linkage table. */
1760 if (h == NULL)
1761 break;
1763 if (h->plt.offset == (bfd_vma) -1
1764 || !elf_hash_table (info)->dynamic_sections_created)
1766 /* We didn't make a PLT entry for this symbol. This
1767 happens when statically linking PIC code, or when
1768 using -Bsymbolic. */
1769 break;
1772 if (splt == NULL)
1774 splt = bfd_get_section_by_name (dynobj, ".plt");
1775 BFD_ASSERT (splt != NULL);
1778 relocation = (splt->output_section->vma
1779 + splt->output_offset
1780 + h->plt.offset);
1781 unresolved_reloc = FALSE;
1782 break;
1784 case R_68K_PLT8O:
1785 case R_68K_PLT16O:
1786 case R_68K_PLT32O:
1787 /* Relocation is the offset of the entry for this symbol in
1788 the procedure linkage table. */
1789 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1791 if (splt == NULL)
1793 splt = bfd_get_section_by_name (dynobj, ".plt");
1794 BFD_ASSERT (splt != NULL);
1797 relocation = h->plt.offset;
1798 unresolved_reloc = FALSE;
1800 /* This relocation does not use the addend. */
1801 rel->r_addend = 0;
1803 break;
1805 case R_68K_PC8:
1806 case R_68K_PC16:
1807 case R_68K_PC32:
1808 if (h == NULL
1809 || (info->shared
1810 && h->forced_local))
1811 break;
1812 /* Fall through. */
1813 case R_68K_8:
1814 case R_68K_16:
1815 case R_68K_32:
1816 if (info->shared
1817 && r_symndx != 0
1818 && (input_section->flags & SEC_ALLOC) != 0
1819 && (h == NULL
1820 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1821 || h->root.type != bfd_link_hash_undefweak)
1822 && ((r_type != R_68K_PC8
1823 && r_type != R_68K_PC16
1824 && r_type != R_68K_PC32)
1825 || (h != NULL
1826 && h->dynindx != -1
1827 && (!info->symbolic
1828 || !h->def_regular))))
1830 Elf_Internal_Rela outrel;
1831 bfd_byte *loc;
1832 bfd_boolean skip, relocate;
1834 /* When generating a shared object, these relocations
1835 are copied into the output file to be resolved at run
1836 time. */
1838 skip = FALSE;
1839 relocate = FALSE;
1841 outrel.r_offset =
1842 _bfd_elf_section_offset (output_bfd, info, input_section,
1843 rel->r_offset);
1844 if (outrel.r_offset == (bfd_vma) -1)
1845 skip = TRUE;
1846 else if (outrel.r_offset == (bfd_vma) -2)
1847 skip = TRUE, relocate = TRUE;
1848 outrel.r_offset += (input_section->output_section->vma
1849 + input_section->output_offset);
1851 if (skip)
1852 memset (&outrel, 0, sizeof outrel);
1853 else if (h != NULL
1854 && h->dynindx != -1
1855 && (r_type == R_68K_PC8
1856 || r_type == R_68K_PC16
1857 || r_type == R_68K_PC32
1858 || !info->shared
1859 || !info->symbolic
1860 || !h->def_regular))
1862 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1863 outrel.r_addend = rel->r_addend;
1865 else
1867 /* This symbol is local, or marked to become local. */
1868 if (r_type == R_68K_32)
1870 relocate = TRUE;
1871 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1872 outrel.r_addend = relocation + rel->r_addend;
1874 else
1876 long indx;
1878 if (bfd_is_abs_section (sec))
1879 indx = 0;
1880 else if (sec == NULL || sec->owner == NULL)
1882 bfd_set_error (bfd_error_bad_value);
1883 return FALSE;
1885 else
1887 asection *osec;
1889 osec = sec->output_section;
1890 indx = elf_section_data (osec)->dynindx;
1891 BFD_ASSERT (indx > 0);
1894 outrel.r_info = ELF32_R_INFO (indx, r_type);
1895 outrel.r_addend = relocation + rel->r_addend;
1899 sreloc = elf_section_data (input_section)->sreloc;
1900 if (sreloc == NULL)
1901 abort ();
1903 loc = sreloc->contents;
1904 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1905 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1907 /* This reloc will be computed at runtime, so there's no
1908 need to do anything now, except for R_68K_32
1909 relocations that have been turned into
1910 R_68K_RELATIVE. */
1911 if (!relocate)
1912 continue;
1915 break;
1917 case R_68K_GNU_VTINHERIT:
1918 case R_68K_GNU_VTENTRY:
1919 /* These are no-ops in the end. */
1920 continue;
1922 default:
1923 break;
1926 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1927 because such sections are not SEC_ALLOC and thus ld.so will
1928 not process them. */
1929 if (unresolved_reloc
1930 && !((input_section->flags & SEC_DEBUGGING) != 0
1931 && h->def_dynamic))
1933 (*_bfd_error_handler)
1934 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1935 input_bfd,
1936 input_section,
1937 (long) rel->r_offset,
1938 howto->name,
1939 h->root.root.string);
1940 return FALSE;
1943 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1944 contents, rel->r_offset,
1945 relocation, rel->r_addend);
1947 if (r != bfd_reloc_ok)
1949 const char *name;
1951 if (h != NULL)
1952 name = h->root.root.string;
1953 else
1955 name = bfd_elf_string_from_elf_section (input_bfd,
1956 symtab_hdr->sh_link,
1957 sym->st_name);
1958 if (name == NULL)
1959 return FALSE;
1960 if (*name == '\0')
1961 name = bfd_section_name (input_bfd, sec);
1964 if (r == bfd_reloc_overflow)
1966 if (!(info->callbacks->reloc_overflow
1967 (info, (h ? &h->root : NULL), name, howto->name,
1968 (bfd_vma) 0, input_bfd, input_section,
1969 rel->r_offset)))
1970 return FALSE;
1972 else
1974 (*_bfd_error_handler)
1975 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
1976 input_bfd, input_section,
1977 (long) rel->r_offset, name, (int) r);
1978 return FALSE;
1983 return TRUE;
1986 /* Make the PC-relative field of INSN point to VALUE. The instruction
1987 is located in section SEC and is displaced by an extra OFFSET bytes
1988 from the start of that section. (This offset applies on top of the
1989 one in INSN itself.) */
1991 static void
1992 elf_m68k_install_pcrel_field (asection *sec, bfd_vma offset,
1993 const struct elf_m68k_pcrel_insn *insn,
1994 bfd_vma value)
1996 bfd_put_32 (sec->owner,
1997 value - (sec->output_section->vma + offset + insn->addr + 2),
1998 sec->contents + offset + insn->addr + insn->field_offset);
2001 /* Finish up dynamic symbol handling. We set the contents of various
2002 dynamic sections here. */
2004 static bfd_boolean
2005 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
2006 bfd *output_bfd;
2007 struct bfd_link_info *info;
2008 struct elf_link_hash_entry *h;
2009 Elf_Internal_Sym *sym;
2011 bfd *dynobj;
2013 dynobj = elf_hash_table (info)->dynobj;
2015 if (h->plt.offset != (bfd_vma) -1)
2017 const struct elf_m68k_plt_info *plt_info;
2018 asection *splt;
2019 asection *sgot;
2020 asection *srela;
2021 bfd_vma plt_index;
2022 bfd_vma got_offset;
2023 Elf_Internal_Rela rela;
2024 bfd_byte *loc;
2026 /* This symbol has an entry in the procedure linkage table. Set
2027 it up. */
2029 BFD_ASSERT (h->dynindx != -1);
2031 plt_info = elf_m68k_hash_table (info)->plt_info;
2032 splt = bfd_get_section_by_name (dynobj, ".plt");
2033 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2034 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2035 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
2037 /* Get the index in the procedure linkage table which
2038 corresponds to this symbol. This is the index of this symbol
2039 in all the symbols for which we are making plt entries. The
2040 first entry in the procedure linkage table is reserved. */
2041 plt_index = (h->plt.offset / plt_info->size) - 1;
2043 /* Get the offset into the .got table of the entry that
2044 corresponds to this function. Each .got entry is 4 bytes.
2045 The first three are reserved. */
2046 got_offset = (plt_index + 3) * 4;
2048 memcpy (splt->contents + h->plt.offset,
2049 plt_info->symbol_entry,
2050 plt_info->size);
2052 elf_m68k_install_pcrel_field (splt, h->plt.offset,
2053 &plt_info->symbol_got_ptr,
2054 (sgot->output_section->vma
2055 + sgot->output_offset
2056 + got_offset));
2058 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
2059 splt->contents
2060 + h->plt.offset
2061 + plt_info->symbol_resolve_entry + 2);
2063 elf_m68k_install_pcrel_field (splt, h->plt.offset,
2064 &plt_info->symbol_plt_ptr,
2065 splt->output_section->vma);
2067 /* Fill in the entry in the global offset table. */
2068 bfd_put_32 (output_bfd,
2069 (splt->output_section->vma
2070 + splt->output_offset
2071 + h->plt.offset
2072 + plt_info->symbol_resolve_entry),
2073 sgot->contents + got_offset);
2075 /* Fill in the entry in the .rela.plt section. */
2076 rela.r_offset = (sgot->output_section->vma
2077 + sgot->output_offset
2078 + got_offset);
2079 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
2080 rela.r_addend = 0;
2081 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
2082 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2084 if (!h->def_regular)
2086 /* Mark the symbol as undefined, rather than as defined in
2087 the .plt section. Leave the value alone. */
2088 sym->st_shndx = SHN_UNDEF;
2092 if (h->got.offset != (bfd_vma) -1)
2094 asection *sgot;
2095 asection *srela;
2096 Elf_Internal_Rela rela;
2097 bfd_byte *loc;
2099 /* This symbol has an entry in the global offset table. Set it
2100 up. */
2102 sgot = bfd_get_section_by_name (dynobj, ".got");
2103 srela = bfd_get_section_by_name (dynobj, ".rela.got");
2104 BFD_ASSERT (sgot != NULL && srela != NULL);
2106 rela.r_offset = (sgot->output_section->vma
2107 + sgot->output_offset
2108 + (h->got.offset &~ (bfd_vma) 1));
2110 /* If this is a -Bsymbolic link, and the symbol is defined
2111 locally, we just want to emit a RELATIVE reloc. Likewise if
2112 the symbol was forced to be local because of a version file.
2113 The entry in the global offset table will already have been
2114 initialized in the relocate_section function. */
2115 if (info->shared
2116 && (info->symbolic
2117 || h->dynindx == -1
2118 || h->forced_local)
2119 && h->def_regular)
2121 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
2122 rela.r_addend = bfd_get_signed_32 (output_bfd,
2123 (sgot->contents
2124 + (h->got.offset &~ (bfd_vma) 1)));
2126 else
2128 bfd_put_32 (output_bfd, (bfd_vma) 0,
2129 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
2130 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
2131 rela.r_addend = 0;
2134 loc = srela->contents;
2135 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2136 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2139 if (h->needs_copy)
2141 asection *s;
2142 Elf_Internal_Rela rela;
2143 bfd_byte *loc;
2145 /* This symbol needs a copy reloc. Set it up. */
2147 BFD_ASSERT (h->dynindx != -1
2148 && (h->root.type == bfd_link_hash_defined
2149 || h->root.type == bfd_link_hash_defweak));
2151 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2152 ".rela.bss");
2153 BFD_ASSERT (s != NULL);
2155 rela.r_offset = (h->root.u.def.value
2156 + h->root.u.def.section->output_section->vma
2157 + h->root.u.def.section->output_offset);
2158 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
2159 rela.r_addend = 0;
2160 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2161 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2164 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2165 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2166 || h == elf_hash_table (info)->hgot)
2167 sym->st_shndx = SHN_ABS;
2169 return TRUE;
2172 /* Finish up the dynamic sections. */
2174 static bfd_boolean
2175 elf_m68k_finish_dynamic_sections (output_bfd, info)
2176 bfd *output_bfd;
2177 struct bfd_link_info *info;
2179 bfd *dynobj;
2180 asection *sgot;
2181 asection *sdyn;
2183 dynobj = elf_hash_table (info)->dynobj;
2185 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2186 BFD_ASSERT (sgot != NULL);
2187 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2189 if (elf_hash_table (info)->dynamic_sections_created)
2191 asection *splt;
2192 Elf32_External_Dyn *dyncon, *dynconend;
2194 splt = bfd_get_section_by_name (dynobj, ".plt");
2195 BFD_ASSERT (splt != NULL && sdyn != NULL);
2197 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2198 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2199 for (; dyncon < dynconend; dyncon++)
2201 Elf_Internal_Dyn dyn;
2202 const char *name;
2203 asection *s;
2205 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2207 switch (dyn.d_tag)
2209 default:
2210 break;
2212 case DT_PLTGOT:
2213 name = ".got";
2214 goto get_vma;
2215 case DT_JMPREL:
2216 name = ".rela.plt";
2217 get_vma:
2218 s = bfd_get_section_by_name (output_bfd, name);
2219 BFD_ASSERT (s != NULL);
2220 dyn.d_un.d_ptr = s->vma;
2221 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2222 break;
2224 case DT_PLTRELSZ:
2225 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2226 BFD_ASSERT (s != NULL);
2227 dyn.d_un.d_val = s->size;
2228 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2229 break;
2231 case DT_RELASZ:
2232 /* The procedure linkage table relocs (DT_JMPREL) should
2233 not be included in the overall relocs (DT_RELA).
2234 Therefore, we override the DT_RELASZ entry here to
2235 make it not include the JMPREL relocs. Since the
2236 linker script arranges for .rela.plt to follow all
2237 other relocation sections, we don't have to worry
2238 about changing the DT_RELA entry. */
2239 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2240 if (s != NULL)
2241 dyn.d_un.d_val -= s->size;
2242 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2243 break;
2247 /* Fill in the first entry in the procedure linkage table. */
2248 if (splt->size > 0)
2250 const struct elf_m68k_plt_info *plt_info;
2252 plt_info = elf_m68k_hash_table (info)->plt_info;
2253 memcpy (splt->contents, plt_info->plt0_entry, plt_info->size);
2255 elf_m68k_install_pcrel_field (splt, 0, &plt_info->plt0_got4_ptr,
2256 (sgot->output_section->vma
2257 + sgot->output_offset
2258 + 4));
2260 elf_m68k_install_pcrel_field (splt, 0, &plt_info->plt0_got8_ptr,
2261 (sgot->output_section->vma
2262 + sgot->output_offset
2263 + 8));
2265 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2266 = plt_info->size;
2270 /* Fill in the first three entries in the global offset table. */
2271 if (sgot->size > 0)
2273 if (sdyn == NULL)
2274 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2275 else
2276 bfd_put_32 (output_bfd,
2277 sdyn->output_section->vma + sdyn->output_offset,
2278 sgot->contents);
2279 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2280 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2283 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2285 return TRUE;
2288 /* Given a .data section and a .emreloc in-memory section, store
2289 relocation information into the .emreloc section which can be
2290 used at runtime to relocate the section. This is called by the
2291 linker when the --embedded-relocs switch is used. This is called
2292 after the add_symbols entry point has been called for all the
2293 objects, and before the final_link entry point is called. */
2295 bfd_boolean
2296 bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2297 bfd *abfd;
2298 struct bfd_link_info *info;
2299 asection *datasec;
2300 asection *relsec;
2301 char **errmsg;
2303 Elf_Internal_Shdr *symtab_hdr;
2304 Elf_Internal_Sym *isymbuf = NULL;
2305 Elf_Internal_Rela *internal_relocs = NULL;
2306 Elf_Internal_Rela *irel, *irelend;
2307 bfd_byte *p;
2308 bfd_size_type amt;
2310 BFD_ASSERT (! info->relocatable);
2312 *errmsg = NULL;
2314 if (datasec->reloc_count == 0)
2315 return TRUE;
2317 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2319 /* Get a copy of the native relocations. */
2320 internal_relocs = (_bfd_elf_link_read_relocs
2321 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2322 info->keep_memory));
2323 if (internal_relocs == NULL)
2324 goto error_return;
2326 amt = (bfd_size_type) datasec->reloc_count * 12;
2327 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
2328 if (relsec->contents == NULL)
2329 goto error_return;
2331 p = relsec->contents;
2333 irelend = internal_relocs + datasec->reloc_count;
2334 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2336 asection *targetsec;
2338 /* We are going to write a four byte longword into the runtime
2339 reloc section. The longword will be the address in the data
2340 section which must be relocated. It is followed by the name
2341 of the target section NUL-padded or truncated to 8
2342 characters. */
2344 /* We can only relocate absolute longword relocs at run time. */
2345 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2347 *errmsg = _("unsupported reloc type");
2348 bfd_set_error (bfd_error_bad_value);
2349 goto error_return;
2352 /* Get the target section referred to by the reloc. */
2353 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2355 /* A local symbol. */
2356 Elf_Internal_Sym *isym;
2358 /* Read this BFD's local symbols if we haven't done so already. */
2359 if (isymbuf == NULL)
2361 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2362 if (isymbuf == NULL)
2363 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2364 symtab_hdr->sh_info, 0,
2365 NULL, NULL, NULL);
2366 if (isymbuf == NULL)
2367 goto error_return;
2370 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2371 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2373 else
2375 unsigned long indx;
2376 struct elf_link_hash_entry *h;
2378 /* An external symbol. */
2379 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2380 h = elf_sym_hashes (abfd)[indx];
2381 BFD_ASSERT (h != NULL);
2382 if (h->root.type == bfd_link_hash_defined
2383 || h->root.type == bfd_link_hash_defweak)
2384 targetsec = h->root.u.def.section;
2385 else
2386 targetsec = NULL;
2389 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2390 memset (p + 4, 0, 8);
2391 if (targetsec != NULL)
2392 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
2395 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2396 free (isymbuf);
2397 if (internal_relocs != NULL
2398 && elf_section_data (datasec)->relocs != internal_relocs)
2399 free (internal_relocs);
2400 return TRUE;
2402 error_return:
2403 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2404 free (isymbuf);
2405 if (internal_relocs != NULL
2406 && elf_section_data (datasec)->relocs != internal_relocs)
2407 free (internal_relocs);
2408 return FALSE;
2411 static enum elf_reloc_type_class
2412 elf32_m68k_reloc_type_class (rela)
2413 const Elf_Internal_Rela *rela;
2415 switch ((int) ELF32_R_TYPE (rela->r_info))
2417 case R_68K_RELATIVE:
2418 return reloc_class_relative;
2419 case R_68K_JMP_SLOT:
2420 return reloc_class_plt;
2421 case R_68K_COPY:
2422 return reloc_class_copy;
2423 default:
2424 return reloc_class_normal;
2428 /* Return address for Ith PLT stub in section PLT, for relocation REL
2429 or (bfd_vma) -1 if it should not be included. */
2431 static bfd_vma
2432 elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
2433 const arelent *rel ATTRIBUTE_UNUSED)
2435 return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
2438 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
2439 #define TARGET_BIG_NAME "elf32-m68k"
2440 #define ELF_MACHINE_CODE EM_68K
2441 #define ELF_MAXPAGESIZE 0x2000
2442 #define elf_backend_create_dynamic_sections \
2443 _bfd_elf_create_dynamic_sections
2444 #define bfd_elf32_bfd_link_hash_table_create \
2445 elf_m68k_link_hash_table_create
2446 #define bfd_elf32_bfd_final_link bfd_elf_gc_common_final_link
2448 #define elf_backend_check_relocs elf_m68k_check_relocs
2449 #define elf_backend_always_size_sections \
2450 elf_m68k_always_size_sections
2451 #define elf_backend_adjust_dynamic_symbol \
2452 elf_m68k_adjust_dynamic_symbol
2453 #define elf_backend_size_dynamic_sections \
2454 elf_m68k_size_dynamic_sections
2455 #define elf_backend_relocate_section elf_m68k_relocate_section
2456 #define elf_backend_finish_dynamic_symbol \
2457 elf_m68k_finish_dynamic_symbol
2458 #define elf_backend_finish_dynamic_sections \
2459 elf_m68k_finish_dynamic_sections
2460 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
2461 #define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
2462 #define bfd_elf32_bfd_merge_private_bfd_data \
2463 elf32_m68k_merge_private_bfd_data
2464 #define bfd_elf32_bfd_set_private_flags \
2465 elf32_m68k_set_private_flags
2466 #define bfd_elf32_bfd_print_private_bfd_data \
2467 elf32_m68k_print_private_bfd_data
2468 #define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
2469 #define elf_backend_plt_sym_val elf_m68k_plt_sym_val
2470 #define elf_backend_object_p elf32_m68k_object_p
2472 #define elf_backend_can_gc_sections 1
2473 #define elf_backend_can_refcount 1
2474 #define elf_backend_want_got_plt 1
2475 #define elf_backend_plt_readonly 1
2476 #define elf_backend_want_plt_sym 0
2477 #define elf_backend_got_header_size 12
2478 #define elf_backend_rela_normal 1
2480 #include "elf32-target.h"