* ld-elf/note-1.s: Increase .foo size.
[binutils.git] / bfd / elf32-m68k.c
blob948a9960f055725a8a94fe06ee4bc909a2d17e27
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, 2007 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 bfd_boolean elf_m68k_adjust_dynamic_symbol
41 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
42 static bfd_boolean elf_m68k_size_dynamic_sections
43 PARAMS ((bfd *, struct bfd_link_info *));
44 static bfd_boolean elf_m68k_discard_copies
45 PARAMS ((struct elf_link_hash_entry *, PTR));
46 static bfd_boolean elf_m68k_relocate_section
47 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
48 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
49 static bfd_boolean elf_m68k_finish_dynamic_symbol
50 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
51 Elf_Internal_Sym *));
52 static bfd_boolean elf_m68k_finish_dynamic_sections
53 PARAMS ((bfd *, struct bfd_link_info *));
55 static bfd_boolean elf32_m68k_set_private_flags
56 PARAMS ((bfd *, flagword));
57 static bfd_boolean elf32_m68k_merge_private_bfd_data
58 PARAMS ((bfd *, bfd *));
59 static bfd_boolean elf32_m68k_print_private_bfd_data
60 PARAMS ((bfd *, PTR));
61 static enum elf_reloc_type_class elf32_m68k_reloc_type_class
62 PARAMS ((const Elf_Internal_Rela *));
64 static reloc_howto_type howto_table[] = {
65 HOWTO(R_68K_NONE, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_NONE", FALSE, 0, 0x00000000,FALSE),
66 HOWTO(R_68K_32, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32", FALSE, 0, 0xffffffff,FALSE),
67 HOWTO(R_68K_16, 0, 1,16, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16", FALSE, 0, 0x0000ffff,FALSE),
68 HOWTO(R_68K_8, 0, 0, 8, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8", FALSE, 0, 0x000000ff,FALSE),
69 HOWTO(R_68K_PC32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32", FALSE, 0, 0xffffffff,TRUE),
70 HOWTO(R_68K_PC16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC16", FALSE, 0, 0x0000ffff,TRUE),
71 HOWTO(R_68K_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC8", FALSE, 0, 0x000000ff,TRUE),
72 HOWTO(R_68K_GOT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32", FALSE, 0, 0xffffffff,TRUE),
73 HOWTO(R_68K_GOT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16", FALSE, 0, 0x0000ffff,TRUE),
74 HOWTO(R_68K_GOT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8", FALSE, 0, 0x000000ff,TRUE),
75 HOWTO(R_68K_GOT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O", FALSE, 0, 0xffffffff,FALSE),
76 HOWTO(R_68K_GOT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16O", FALSE, 0, 0x0000ffff,FALSE),
77 HOWTO(R_68K_GOT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8O", FALSE, 0, 0x000000ff,FALSE),
78 HOWTO(R_68K_PLT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32", FALSE, 0, 0xffffffff,TRUE),
79 HOWTO(R_68K_PLT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16", FALSE, 0, 0x0000ffff,TRUE),
80 HOWTO(R_68K_PLT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8", FALSE, 0, 0x000000ff,TRUE),
81 HOWTO(R_68K_PLT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O", FALSE, 0, 0xffffffff,FALSE),
82 HOWTO(R_68K_PLT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16O", FALSE, 0, 0x0000ffff,FALSE),
83 HOWTO(R_68K_PLT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8O", FALSE, 0, 0x000000ff,FALSE),
84 HOWTO(R_68K_COPY, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_COPY", FALSE, 0, 0xffffffff,FALSE),
85 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),
86 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),
87 HOWTO(R_68K_RELATIVE, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_RELATIVE", FALSE, 0, 0xffffffff,FALSE),
88 /* GNU extension to record C++ vtable hierarchy. */
89 HOWTO (R_68K_GNU_VTINHERIT, /* type */
90 0, /* rightshift */
91 2, /* size (0 = byte, 1 = short, 2 = long) */
92 0, /* bitsize */
93 FALSE, /* pc_relative */
94 0, /* bitpos */
95 complain_overflow_dont, /* complain_on_overflow */
96 NULL, /* special_function */
97 "R_68K_GNU_VTINHERIT", /* name */
98 FALSE, /* partial_inplace */
99 0, /* src_mask */
100 0, /* dst_mask */
101 FALSE),
102 /* GNU extension to record C++ vtable member usage. */
103 HOWTO (R_68K_GNU_VTENTRY, /* type */
104 0, /* rightshift */
105 2, /* size (0 = byte, 1 = short, 2 = long) */
106 0, /* bitsize */
107 FALSE, /* pc_relative */
108 0, /* bitpos */
109 complain_overflow_dont, /* complain_on_overflow */
110 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
111 "R_68K_GNU_VTENTRY", /* name */
112 FALSE, /* partial_inplace */
113 0, /* src_mask */
114 0, /* dst_mask */
115 FALSE),
118 static void
119 rtype_to_howto (abfd, cache_ptr, dst)
120 bfd *abfd ATTRIBUTE_UNUSED;
121 arelent *cache_ptr;
122 Elf_Internal_Rela *dst;
124 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K_max);
125 cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
128 #define elf_info_to_howto rtype_to_howto
130 static const struct
132 bfd_reloc_code_real_type bfd_val;
133 int elf_val;
134 } reloc_map[] = {
135 { BFD_RELOC_NONE, R_68K_NONE },
136 { BFD_RELOC_32, R_68K_32 },
137 { BFD_RELOC_16, R_68K_16 },
138 { BFD_RELOC_8, R_68K_8 },
139 { BFD_RELOC_32_PCREL, R_68K_PC32 },
140 { BFD_RELOC_16_PCREL, R_68K_PC16 },
141 { BFD_RELOC_8_PCREL, R_68K_PC8 },
142 { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
143 { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
144 { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
145 { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
146 { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
147 { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
148 { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
149 { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
150 { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
151 { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
152 { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
153 { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
154 { BFD_RELOC_NONE, R_68K_COPY },
155 { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
156 { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
157 { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
158 { BFD_RELOC_CTOR, R_68K_32 },
159 { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
160 { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
163 static reloc_howto_type *
164 reloc_type_lookup (abfd, code)
165 bfd *abfd ATTRIBUTE_UNUSED;
166 bfd_reloc_code_real_type code;
168 unsigned int i;
169 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
171 if (reloc_map[i].bfd_val == code)
172 return &howto_table[reloc_map[i].elf_val];
174 return 0;
177 static reloc_howto_type *
178 reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
180 unsigned int i;
182 for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++)
183 if (howto_table[i].name != NULL
184 && strcasecmp (howto_table[i].name, r_name) == 0)
185 return &howto_table[i];
187 return NULL;
190 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
191 #define bfd_elf32_bfd_reloc_name_lookup reloc_name_lookup
192 #define ELF_ARCH bfd_arch_m68k
194 /* Functions for the m68k ELF linker. */
196 /* The name of the dynamic interpreter. This is put in the .interp
197 section. */
199 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
201 /* Describes one of the various PLT styles. */
203 struct elf_m68k_plt_info
205 /* The size of each PLT entry. */
206 bfd_vma size;
208 /* The template for the first PLT entry. */
209 const bfd_byte *plt0_entry;
211 /* Offsets of fields in PLT0_ENTRY that require R_68K_PC32 relocations.
212 The comments by each member indicate the value that the relocation
213 is against. */
214 struct {
215 unsigned int got4; /* .got + 4 */
216 unsigned int got8; /* .got + 8 */
217 } plt0_relocs;
219 /* The template for a symbol's PLT entry. */
220 const bfd_byte *symbol_entry;
222 /* Offsets of fields in SYMBOL_ENTRY that require R_68K_PC32 relocations.
223 The comments by each member indicate the value that the relocation
224 is against. */
225 struct {
226 unsigned int got; /* the symbol's .got.plt entry */
227 unsigned int plt; /* .plt */
228 } symbol_relocs;
230 /* The offset of the resolver stub from the start of SYMBOL_ENTRY.
231 The stub starts with "move.l #relocoffset,%d0". */
232 bfd_vma symbol_resolve_entry;
235 /* The size in bytes of an entry in the procedure linkage table. */
237 #define PLT_ENTRY_SIZE 20
239 /* The first entry in a procedure linkage table looks like this. See
240 the SVR4 ABI m68k supplement to see how this works. */
242 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
244 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
245 0, 0, 0, 2, /* + (.got + 4) - . */
246 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
247 0, 0, 0, 2, /* + (.got + 8) - . */
248 0, 0, 0, 0 /* pad out to 20 bytes. */
251 /* Subsequent entries in a procedure linkage table look like this. */
253 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
255 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
256 0, 0, 0, 2, /* + (.got.plt entry) - . */
257 0x2f, 0x3c, /* move.l #offset,-(%sp) */
258 0, 0, 0, 0, /* + reloc index */
259 0x60, 0xff, /* bra.l .plt */
260 0, 0, 0, 0 /* + .plt - . */
263 static const struct elf_m68k_plt_info elf_m68k_plt_info = {
264 PLT_ENTRY_SIZE,
265 elf_m68k_plt0_entry, { 4, 12 },
266 elf_m68k_plt_entry, { 4, 16 }, 8
269 #define ISAB_PLT_ENTRY_SIZE 24
271 static const bfd_byte elf_isab_plt0_entry[ISAB_PLT_ENTRY_SIZE] =
273 0x20, 0x3c, /* move.l #offset,%d0 */
274 0, 0, 0, 0, /* + (.got + 4) - . */
275 0x2f, 0x3b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),-(%sp) */
276 0x20, 0x3c, /* move.l #offset,%d0 */
277 0, 0, 0, 0, /* + (.got + 8) - . */
278 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
279 0x4e, 0xd0, /* jmp (%a0) */
280 0x4e, 0x71 /* nop */
283 /* Subsequent entries in a procedure linkage table look like this. */
285 static const bfd_byte elf_isab_plt_entry[ISAB_PLT_ENTRY_SIZE] =
287 0x20, 0x3c, /* move.l #offset,%d0 */
288 0, 0, 0, 0, /* + (.got.plt entry) - . */
289 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
290 0x4e, 0xd0, /* jmp (%a0) */
291 0x2f, 0x3c, /* move.l #offset,-(%sp) */
292 0, 0, 0, 0, /* + reloc index */
293 0x60, 0xff, /* bra.l .plt */
294 0, 0, 0, 0 /* + .plt - . */
297 static const struct elf_m68k_plt_info elf_isab_plt_info = {
298 ISAB_PLT_ENTRY_SIZE,
299 elf_isab_plt0_entry, { 2, 12 },
300 elf_isab_plt_entry, { 2, 20 }, 12
303 #define CPU32_PLT_ENTRY_SIZE 24
304 /* Procedure linkage table entries for the cpu32 */
305 static const bfd_byte elf_cpu32_plt0_entry[CPU32_PLT_ENTRY_SIZE] =
307 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
308 0, 0, 0, 2, /* + (.got + 4) - . */
309 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
310 0, 0, 0, 2, /* + (.got + 8) - . */
311 0x4e, 0xd1, /* jmp %a1@ */
312 0, 0, 0, 0, /* pad out to 24 bytes. */
313 0, 0
316 static const bfd_byte elf_cpu32_plt_entry[CPU32_PLT_ENTRY_SIZE] =
318 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
319 0, 0, 0, 2, /* + (.got.plt entry) - . */
320 0x4e, 0xd1, /* jmp %a1@ */
321 0x2f, 0x3c, /* move.l #offset,-(%sp) */
322 0, 0, 0, 0, /* + reloc index */
323 0x60, 0xff, /* bra.l .plt */
324 0, 0, 0, 0, /* + .plt - . */
325 0, 0
328 static const struct elf_m68k_plt_info elf_cpu32_plt_info = {
329 CPU32_PLT_ENTRY_SIZE,
330 elf_cpu32_plt0_entry, { 4, 12 },
331 elf_cpu32_plt_entry, { 4, 18 }, 10
334 /* The m68k linker needs to keep track of the number of relocs that it
335 decides to copy in check_relocs for each symbol. This is so that it
336 can discard PC relative relocs if it doesn't need them when linking
337 with -Bsymbolic. We store the information in a field extending the
338 regular ELF linker hash table. */
340 /* This structure keeps track of the number of PC relative relocs we have
341 copied for a given symbol. */
343 struct elf_m68k_pcrel_relocs_copied
345 /* Next section. */
346 struct elf_m68k_pcrel_relocs_copied *next;
347 /* A section in dynobj. */
348 asection *section;
349 /* Number of relocs copied in this section. */
350 bfd_size_type count;
353 /* m68k ELF linker hash entry. */
355 struct elf_m68k_link_hash_entry
357 struct elf_link_hash_entry root;
359 /* Number of PC relative relocs copied for this symbol. */
360 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
363 #define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
365 /* m68k ELF linker hash table. */
367 struct elf_m68k_link_hash_table
369 struct elf_link_hash_table root;
371 /* Small local sym to section mapping cache. */
372 struct sym_sec_cache sym_sec;
374 /* The PLT format used by this link, or NULL if the format has not
375 yet been chosen. */
376 const struct elf_m68k_plt_info *plt_info;
379 /* Get the m68k ELF linker hash table from a link_info structure. */
381 #define elf_m68k_hash_table(p) \
382 ((struct elf_m68k_link_hash_table *) (p)->hash)
384 /* Create an entry in an m68k ELF linker hash table. */
386 static struct bfd_hash_entry *
387 elf_m68k_link_hash_newfunc (entry, table, string)
388 struct bfd_hash_entry *entry;
389 struct bfd_hash_table *table;
390 const char *string;
392 struct bfd_hash_entry *ret = entry;
394 /* Allocate the structure if it has not already been allocated by a
395 subclass. */
396 if (ret == NULL)
397 ret = bfd_hash_allocate (table,
398 sizeof (struct elf_m68k_link_hash_entry));
399 if (ret == NULL)
400 return ret;
402 /* Call the allocation method of the superclass. */
403 ret = _bfd_elf_link_hash_newfunc (ret, table, string);
404 if (ret != NULL)
405 elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
407 return ret;
410 /* Create an m68k ELF linker hash table. */
412 static struct bfd_link_hash_table *
413 elf_m68k_link_hash_table_create (abfd)
414 bfd *abfd;
416 struct elf_m68k_link_hash_table *ret;
417 bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
419 ret = (struct elf_m68k_link_hash_table *) bfd_malloc (amt);
420 if (ret == (struct elf_m68k_link_hash_table *) NULL)
421 return NULL;
423 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
424 elf_m68k_link_hash_newfunc,
425 sizeof (struct elf_m68k_link_hash_entry)))
427 free (ret);
428 return NULL;
431 ret->sym_sec.abfd = NULL;
432 ret->plt_info = NULL;
434 return &ret->root.root;
437 /* Set the right machine number. */
439 static bfd_boolean
440 elf32_m68k_object_p (bfd *abfd)
442 unsigned int mach = 0;
443 unsigned features = 0;
444 flagword eflags = elf_elfheader (abfd)->e_flags;
446 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
447 features |= m68000;
448 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
449 features |= cpu32;
450 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
451 features |= fido_a;
452 else
454 switch (eflags & EF_M68K_CF_ISA_MASK)
456 case EF_M68K_CF_ISA_A_NODIV:
457 features |= mcfisa_a;
458 break;
459 case EF_M68K_CF_ISA_A:
460 features |= mcfisa_a|mcfhwdiv;
461 break;
462 case EF_M68K_CF_ISA_A_PLUS:
463 features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
464 break;
465 case EF_M68K_CF_ISA_B_NOUSP:
466 features |= mcfisa_a|mcfisa_b|mcfhwdiv;
467 break;
468 case EF_M68K_CF_ISA_B:
469 features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
470 break;
472 switch (eflags & EF_M68K_CF_MAC_MASK)
474 case EF_M68K_CF_MAC:
475 features |= mcfmac;
476 break;
477 case EF_M68K_CF_EMAC:
478 features |= mcfemac;
479 break;
481 if (eflags & EF_M68K_CF_FLOAT)
482 features |= cfloat;
485 mach = bfd_m68k_features_to_mach (features);
486 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
488 return TRUE;
491 /* Keep m68k-specific flags in the ELF header. */
492 static bfd_boolean
493 elf32_m68k_set_private_flags (abfd, flags)
494 bfd *abfd;
495 flagword flags;
497 elf_elfheader (abfd)->e_flags = flags;
498 elf_flags_init (abfd) = TRUE;
499 return TRUE;
502 /* Merge backend specific data from an object file to the output
503 object file when linking. */
504 static bfd_boolean
505 elf32_m68k_merge_private_bfd_data (ibfd, obfd)
506 bfd *ibfd;
507 bfd *obfd;
509 flagword out_flags;
510 flagword in_flags;
511 flagword out_isa;
512 flagword in_isa;
513 const bfd_arch_info_type *arch_info;
515 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
516 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
517 return FALSE;
519 /* Get the merged machine. This checks for incompatibility between
520 Coldfire & non-Coldfire flags, incompability between different
521 Coldfire ISAs, and incompability between different MAC types. */
522 arch_info = bfd_arch_get_compatible (ibfd, obfd, FALSE);
523 if (!arch_info)
524 return FALSE;
526 bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
528 in_flags = elf_elfheader (ibfd)->e_flags;
529 if (!elf_flags_init (obfd))
531 elf_flags_init (obfd) = TRUE;
532 out_flags = in_flags;
534 else
536 out_flags = elf_elfheader (obfd)->e_flags;
537 unsigned int variant_mask;
539 if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
540 variant_mask = 0;
541 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
542 variant_mask = 0;
543 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
544 variant_mask = 0;
545 else
546 variant_mask = EF_M68K_CF_ISA_MASK;
548 in_isa = (in_flags & variant_mask);
549 out_isa = (out_flags & variant_mask);
550 if (in_isa > out_isa)
551 out_flags ^= in_isa ^ out_isa;
552 if (((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32
553 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
554 || ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO
555 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32))
556 out_flags = EF_M68K_FIDO;
557 else
558 out_flags |= in_flags ^ in_isa;
560 elf_elfheader (obfd)->e_flags = out_flags;
562 return TRUE;
565 /* Display the flags field. */
566 static bfd_boolean
567 elf32_m68k_print_private_bfd_data (abfd, ptr)
568 bfd *abfd;
569 PTR ptr;
571 FILE *file = (FILE *) ptr;
572 flagword eflags = elf_elfheader (abfd)->e_flags;
574 BFD_ASSERT (abfd != NULL && ptr != NULL);
576 /* Print normal ELF private data. */
577 _bfd_elf_print_private_bfd_data (abfd, ptr);
579 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
581 /* xgettext:c-format */
582 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
584 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
585 fprintf (file, " [m68000]");
586 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
587 fprintf (file, " [cpu32]");
588 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
589 fprintf (file, " [fido]");
590 else
592 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CFV4E)
593 fprintf (file, " [cfv4e]");
595 if (eflags & EF_M68K_CF_ISA_MASK)
597 char const *isa = _("unknown");
598 char const *mac = _("unknown");
599 char const *additional = "";
601 switch (eflags & EF_M68K_CF_ISA_MASK)
603 case EF_M68K_CF_ISA_A_NODIV:
604 isa = "A";
605 additional = " [nodiv]";
606 break;
607 case EF_M68K_CF_ISA_A:
608 isa = "A";
609 break;
610 case EF_M68K_CF_ISA_A_PLUS:
611 isa = "A+";
612 break;
613 case EF_M68K_CF_ISA_B_NOUSP:
614 isa = "B";
615 additional = " [nousp]";
616 break;
617 case EF_M68K_CF_ISA_B:
618 isa = "B";
619 break;
621 fprintf (file, " [isa %s]%s", isa, additional);
622 if (eflags & EF_M68K_CF_FLOAT)
623 fprintf (file, " [float]");
624 switch (eflags & EF_M68K_CF_MAC_MASK)
626 case 0:
627 mac = NULL;
628 break;
629 case EF_M68K_CF_MAC:
630 mac = "mac";
631 break;
632 case EF_M68K_CF_EMAC:
633 mac = "emac";
634 break;
636 if (mac)
637 fprintf (file, " [%s]", mac);
641 fputc ('\n', file);
643 return TRUE;
645 /* Look through the relocs for a section during the first phase, and
646 allocate space in the global offset table or procedure linkage
647 table. */
649 static bfd_boolean
650 elf_m68k_check_relocs (abfd, info, sec, relocs)
651 bfd *abfd;
652 struct bfd_link_info *info;
653 asection *sec;
654 const Elf_Internal_Rela *relocs;
656 bfd *dynobj;
657 Elf_Internal_Shdr *symtab_hdr;
658 struct elf_link_hash_entry **sym_hashes;
659 bfd_signed_vma *local_got_refcounts;
660 const Elf_Internal_Rela *rel;
661 const Elf_Internal_Rela *rel_end;
662 asection *sgot;
663 asection *srelgot;
664 asection *sreloc;
666 if (info->relocatable)
667 return TRUE;
669 dynobj = elf_hash_table (info)->dynobj;
670 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
671 sym_hashes = elf_sym_hashes (abfd);
672 local_got_refcounts = elf_local_got_refcounts (abfd);
674 sgot = NULL;
675 srelgot = NULL;
676 sreloc = NULL;
678 rel_end = relocs + sec->reloc_count;
679 for (rel = relocs; rel < rel_end; rel++)
681 unsigned long r_symndx;
682 struct elf_link_hash_entry *h;
684 r_symndx = ELF32_R_SYM (rel->r_info);
686 if (r_symndx < symtab_hdr->sh_info)
687 h = NULL;
688 else
690 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
691 while (h->root.type == bfd_link_hash_indirect
692 || h->root.type == bfd_link_hash_warning)
693 h = (struct elf_link_hash_entry *) h->root.u.i.link;
696 switch (ELF32_R_TYPE (rel->r_info))
698 case R_68K_GOT8:
699 case R_68K_GOT16:
700 case R_68K_GOT32:
701 if (h != NULL
702 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
703 break;
704 /* Fall through. */
705 case R_68K_GOT8O:
706 case R_68K_GOT16O:
707 case R_68K_GOT32O:
708 /* This symbol requires a global offset table entry. */
710 if (dynobj == NULL)
712 /* Create the .got section. */
713 elf_hash_table (info)->dynobj = dynobj = abfd;
714 if (!_bfd_elf_create_got_section (dynobj, info))
715 return FALSE;
718 if (sgot == NULL)
720 sgot = bfd_get_section_by_name (dynobj, ".got");
721 BFD_ASSERT (sgot != NULL);
724 if (srelgot == NULL
725 && (h != NULL || info->shared))
727 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
728 if (srelgot == NULL)
730 srelgot = bfd_make_section_with_flags (dynobj,
731 ".rela.got",
732 (SEC_ALLOC
733 | SEC_LOAD
734 | SEC_HAS_CONTENTS
735 | SEC_IN_MEMORY
736 | SEC_LINKER_CREATED
737 | SEC_READONLY));
738 if (srelgot == NULL
739 || !bfd_set_section_alignment (dynobj, srelgot, 2))
740 return FALSE;
744 if (h != NULL)
746 if (h->got.refcount == 0)
748 /* Make sure this symbol is output as a dynamic symbol. */
749 if (h->dynindx == -1
750 && !h->forced_local)
752 if (!bfd_elf_link_record_dynamic_symbol (info, h))
753 return FALSE;
756 /* Allocate space in the .got section. */
757 sgot->size += 4;
758 /* Allocate relocation space. */
759 srelgot->size += sizeof (Elf32_External_Rela);
761 h->got.refcount++;
763 else
765 /* This is a global offset table entry for a local symbol. */
766 if (local_got_refcounts == NULL)
768 bfd_size_type size;
770 size = symtab_hdr->sh_info;
771 size *= sizeof (bfd_signed_vma);
772 local_got_refcounts = ((bfd_signed_vma *)
773 bfd_zalloc (abfd, size));
774 if (local_got_refcounts == NULL)
775 return FALSE;
776 elf_local_got_refcounts (abfd) = local_got_refcounts;
778 if (local_got_refcounts[r_symndx] == 0)
780 sgot->size += 4;
781 if (info->shared)
783 /* If we are generating a shared object, we need to
784 output a R_68K_RELATIVE reloc so that the dynamic
785 linker can adjust this GOT entry. */
786 srelgot->size += sizeof (Elf32_External_Rela);
789 local_got_refcounts[r_symndx]++;
791 break;
793 case R_68K_PLT8:
794 case R_68K_PLT16:
795 case R_68K_PLT32:
796 /* This symbol requires a procedure linkage table entry. We
797 actually build the entry in adjust_dynamic_symbol,
798 because this might be a case of linking PIC code which is
799 never referenced by a dynamic object, in which case we
800 don't need to generate a procedure linkage table entry
801 after all. */
803 /* If this is a local symbol, we resolve it directly without
804 creating a procedure linkage table entry. */
805 if (h == NULL)
806 continue;
808 h->needs_plt = 1;
809 h->plt.refcount++;
810 break;
812 case R_68K_PLT8O:
813 case R_68K_PLT16O:
814 case R_68K_PLT32O:
815 /* This symbol requires a procedure linkage table entry. */
817 if (h == NULL)
819 /* It does not make sense to have this relocation for a
820 local symbol. FIXME: does it? How to handle it if
821 it does make sense? */
822 bfd_set_error (bfd_error_bad_value);
823 return FALSE;
826 /* Make sure this symbol is output as a dynamic symbol. */
827 if (h->dynindx == -1
828 && !h->forced_local)
830 if (!bfd_elf_link_record_dynamic_symbol (info, h))
831 return FALSE;
834 h->needs_plt = 1;
835 h->plt.refcount++;
836 break;
838 case R_68K_PC8:
839 case R_68K_PC16:
840 case R_68K_PC32:
841 /* If we are creating a shared library and this is not a local
842 symbol, we need to copy the reloc into the shared library.
843 However when linking with -Bsymbolic and this is a global
844 symbol which is defined in an object we are including in the
845 link (i.e., DEF_REGULAR is set), then we can resolve the
846 reloc directly. At this point we have not seen all the input
847 files, so it is possible that DEF_REGULAR is not set now but
848 will be set later (it is never cleared). We account for that
849 possibility below by storing information in the
850 pcrel_relocs_copied field of the hash table entry. */
851 if (!(info->shared
852 && (sec->flags & SEC_ALLOC) != 0
853 && h != NULL
854 && (!info->symbolic
855 || h->root.type == bfd_link_hash_defweak
856 || !h->def_regular)))
858 if (h != NULL)
860 /* Make sure a plt entry is created for this symbol if
861 it turns out to be a function defined by a dynamic
862 object. */
863 h->plt.refcount++;
865 break;
867 /* Fall through. */
868 case R_68K_8:
869 case R_68K_16:
870 case R_68K_32:
871 if (h != NULL)
873 /* Make sure a plt entry is created for this symbol if it
874 turns out to be a function defined by a dynamic object. */
875 h->plt.refcount++;
878 /* If we are creating a shared library, we need to copy the
879 reloc into the shared library. */
880 if (info->shared
881 && (sec->flags & SEC_ALLOC) != 0)
883 /* When creating a shared object, we must copy these
884 reloc types into the output file. We create a reloc
885 section in dynobj and make room for this reloc. */
886 if (sreloc == NULL)
888 const char *name;
890 name = (bfd_elf_string_from_elf_section
891 (abfd,
892 elf_elfheader (abfd)->e_shstrndx,
893 elf_section_data (sec)->rel_hdr.sh_name));
894 if (name == NULL)
895 return FALSE;
897 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
898 && strcmp (bfd_get_section_name (abfd, sec),
899 name + 5) == 0);
901 sreloc = bfd_get_section_by_name (dynobj, name);
902 if (sreloc == NULL)
904 sreloc = bfd_make_section_with_flags (dynobj,
905 name,
906 (SEC_ALLOC
907 | SEC_LOAD
908 | SEC_HAS_CONTENTS
909 | SEC_IN_MEMORY
910 | SEC_LINKER_CREATED
911 | SEC_READONLY));
912 if (sreloc == NULL
913 || !bfd_set_section_alignment (dynobj, sreloc, 2))
914 return FALSE;
916 elf_section_data (sec)->sreloc = sreloc;
919 if (sec->flags & SEC_READONLY
920 /* Don't set DF_TEXTREL yet for PC relative
921 relocations, they might be discarded later. */
922 && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
923 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
924 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
925 info->flags |= DF_TEXTREL;
927 sreloc->size += sizeof (Elf32_External_Rela);
929 /* We count the number of PC relative relocations we have
930 entered for this symbol, so that we can discard them
931 again if, in the -Bsymbolic case, the symbol is later
932 defined by a regular object, or, in the normal shared
933 case, the symbol is forced to be local. Note that this
934 function is only called if we are using an m68kelf linker
935 hash table, which means that h is really a pointer to an
936 elf_m68k_link_hash_entry. */
937 if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
938 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
939 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
941 struct elf_m68k_pcrel_relocs_copied *p;
942 struct elf_m68k_pcrel_relocs_copied **head;
944 if (h != NULL)
946 struct elf_m68k_link_hash_entry *eh
947 = elf_m68k_hash_entry (h);
948 head = &eh->pcrel_relocs_copied;
950 else
952 asection *s;
953 void *vpp;
955 s = (bfd_section_from_r_symndx
956 (abfd, &elf_m68k_hash_table (info)->sym_sec,
957 sec, r_symndx));
958 if (s == NULL)
959 return FALSE;
961 vpp = &elf_section_data (s)->local_dynrel;
962 head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
965 for (p = *head; p != NULL; p = p->next)
966 if (p->section == sreloc)
967 break;
969 if (p == NULL)
971 p = ((struct elf_m68k_pcrel_relocs_copied *)
972 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
973 if (p == NULL)
974 return FALSE;
975 p->next = *head;
976 *head = p;
977 p->section = sreloc;
978 p->count = 0;
981 ++p->count;
985 break;
987 /* This relocation describes the C++ object vtable hierarchy.
988 Reconstruct it for later use during GC. */
989 case R_68K_GNU_VTINHERIT:
990 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
991 return FALSE;
992 break;
994 /* This relocation describes which C++ vtable entries are actually
995 used. Record for later use during GC. */
996 case R_68K_GNU_VTENTRY:
997 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
998 return FALSE;
999 break;
1001 default:
1002 break;
1006 return TRUE;
1009 /* Return the section that should be marked against GC for a given
1010 relocation. */
1012 static asection *
1013 elf_m68k_gc_mark_hook (asection *sec,
1014 struct bfd_link_info *info,
1015 Elf_Internal_Rela *rel,
1016 struct elf_link_hash_entry *h,
1017 Elf_Internal_Sym *sym)
1019 if (h != NULL)
1020 switch (ELF32_R_TYPE (rel->r_info))
1022 case R_68K_GNU_VTINHERIT:
1023 case R_68K_GNU_VTENTRY:
1024 return NULL;
1027 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1030 /* Update the got entry reference counts for the section being removed. */
1032 static bfd_boolean
1033 elf_m68k_gc_sweep_hook (bfd *abfd,
1034 struct bfd_link_info *info,
1035 asection *sec,
1036 const Elf_Internal_Rela *relocs)
1038 Elf_Internal_Shdr *symtab_hdr;
1039 struct elf_link_hash_entry **sym_hashes;
1040 bfd_signed_vma *local_got_refcounts;
1041 const Elf_Internal_Rela *rel, *relend;
1042 bfd *dynobj;
1043 asection *sgot;
1044 asection *srelgot;
1046 dynobj = elf_hash_table (info)->dynobj;
1047 if (dynobj == NULL)
1048 return TRUE;
1050 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1051 sym_hashes = elf_sym_hashes (abfd);
1052 local_got_refcounts = elf_local_got_refcounts (abfd);
1054 sgot = bfd_get_section_by_name (dynobj, ".got");
1055 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1057 relend = relocs + sec->reloc_count;
1058 for (rel = relocs; rel < relend; rel++)
1060 unsigned long r_symndx;
1061 struct elf_link_hash_entry *h = NULL;
1063 r_symndx = ELF32_R_SYM (rel->r_info);
1064 if (r_symndx >= symtab_hdr->sh_info)
1066 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1067 while (h->root.type == bfd_link_hash_indirect
1068 || h->root.type == bfd_link_hash_warning)
1069 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1072 switch (ELF32_R_TYPE (rel->r_info))
1074 case R_68K_GOT8:
1075 case R_68K_GOT16:
1076 case R_68K_GOT32:
1077 case R_68K_GOT8O:
1078 case R_68K_GOT16O:
1079 case R_68K_GOT32O:
1080 if (h != NULL)
1082 if (h->got.refcount > 0)
1084 --h->got.refcount;
1085 if (h->got.refcount == 0)
1087 /* We don't need the .got entry any more. */
1088 sgot->size -= 4;
1089 srelgot->size -= sizeof (Elf32_External_Rela);
1093 else if (local_got_refcounts != NULL)
1095 if (local_got_refcounts[r_symndx] > 0)
1097 --local_got_refcounts[r_symndx];
1098 if (local_got_refcounts[r_symndx] == 0)
1100 /* We don't need the .got entry any more. */
1101 sgot->size -= 4;
1102 if (info->shared)
1103 srelgot->size -= sizeof (Elf32_External_Rela);
1107 break;
1109 case R_68K_PLT8:
1110 case R_68K_PLT16:
1111 case R_68K_PLT32:
1112 case R_68K_PLT8O:
1113 case R_68K_PLT16O:
1114 case R_68K_PLT32O:
1115 case R_68K_PC8:
1116 case R_68K_PC16:
1117 case R_68K_PC32:
1118 case R_68K_8:
1119 case R_68K_16:
1120 case R_68K_32:
1121 if (h != NULL)
1123 if (h->plt.refcount > 0)
1124 --h->plt.refcount;
1126 break;
1128 default:
1129 break;
1133 return TRUE;
1136 /* Return the type of PLT associated with OUTPUT_BFD. */
1138 static const struct elf_m68k_plt_info *
1139 elf_m68k_get_plt_info (bfd *output_bfd)
1141 unsigned int features;
1143 features = bfd_m68k_mach_to_features (bfd_get_mach (output_bfd));
1144 if (features & cpu32)
1145 return &elf_cpu32_plt_info;
1146 if (features & mcfisa_b)
1147 return &elf_isab_plt_info;
1148 return &elf_m68k_plt_info;
1151 /* This function is called after all the input files have been read,
1152 and the input sections have been assigned to output sections.
1153 It's a convenient place to determine the PLT style. */
1155 static bfd_boolean
1156 elf_m68k_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
1158 elf_m68k_hash_table (info)->plt_info = elf_m68k_get_plt_info (output_bfd);
1159 return TRUE;
1162 /* Adjust a symbol defined by a dynamic object and referenced by a
1163 regular object. The current definition is in some section of the
1164 dynamic object, but we're not including those sections. We have to
1165 change the definition to something the rest of the link can
1166 understand. */
1168 static bfd_boolean
1169 elf_m68k_adjust_dynamic_symbol (info, h)
1170 struct bfd_link_info *info;
1171 struct elf_link_hash_entry *h;
1173 struct elf_m68k_link_hash_table *htab;
1174 bfd *dynobj;
1175 asection *s;
1176 unsigned int power_of_two;
1178 htab = elf_m68k_hash_table (info);
1179 dynobj = elf_hash_table (info)->dynobj;
1181 /* Make sure we know what is going on here. */
1182 BFD_ASSERT (dynobj != NULL
1183 && (h->needs_plt
1184 || h->u.weakdef != NULL
1185 || (h->def_dynamic
1186 && h->ref_regular
1187 && !h->def_regular)));
1189 /* If this is a function, put it in the procedure linkage table. We
1190 will fill in the contents of the procedure linkage table later,
1191 when we know the address of the .got section. */
1192 if (h->type == STT_FUNC
1193 || h->needs_plt)
1195 if ((h->plt.refcount <= 0
1196 || SYMBOL_CALLS_LOCAL (info, h)
1197 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1198 && h->root.type == bfd_link_hash_undefweak))
1199 /* We must always create the plt entry if it was referenced
1200 by a PLTxxO relocation. In this case we already recorded
1201 it as a dynamic symbol. */
1202 && h->dynindx == -1)
1204 /* This case can occur if we saw a PLTxx reloc in an input
1205 file, but the symbol was never referred to by a dynamic
1206 object, or if all references were garbage collected. In
1207 such a case, we don't actually need to build a procedure
1208 linkage table, and we can just do a PCxx reloc instead. */
1209 h->plt.offset = (bfd_vma) -1;
1210 h->needs_plt = 0;
1211 return TRUE;
1214 /* Make sure this symbol is output as a dynamic symbol. */
1215 if (h->dynindx == -1
1216 && !h->forced_local)
1218 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1219 return FALSE;
1222 s = bfd_get_section_by_name (dynobj, ".plt");
1223 BFD_ASSERT (s != NULL);
1225 /* If this is the first .plt entry, make room for the special
1226 first entry. */
1227 if (s->size == 0)
1228 s->size = htab->plt_info->size;
1230 /* If this symbol is not defined in a regular file, and we are
1231 not generating a shared library, then set the symbol to this
1232 location in the .plt. This is required to make function
1233 pointers compare as equal between the normal executable and
1234 the shared library. */
1235 if (!info->shared
1236 && !h->def_regular)
1238 h->root.u.def.section = s;
1239 h->root.u.def.value = s->size;
1242 h->plt.offset = s->size;
1244 /* Make room for this entry. */
1245 s->size += htab->plt_info->size;
1247 /* We also need to make an entry in the .got.plt section, which
1248 will be placed in the .got section by the linker script. */
1249 s = bfd_get_section_by_name (dynobj, ".got.plt");
1250 BFD_ASSERT (s != NULL);
1251 s->size += 4;
1253 /* We also need to make an entry in the .rela.plt section. */
1254 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1255 BFD_ASSERT (s != NULL);
1256 s->size += sizeof (Elf32_External_Rela);
1258 return TRUE;
1261 /* Reinitialize the plt offset now that it is not used as a reference
1262 count any more. */
1263 h->plt.offset = (bfd_vma) -1;
1265 /* If this is a weak symbol, and there is a real definition, the
1266 processor independent code will have arranged for us to see the
1267 real definition first, and we can just use the same value. */
1268 if (h->u.weakdef != NULL)
1270 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1271 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1272 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1273 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1274 return TRUE;
1277 /* This is a reference to a symbol defined by a dynamic object which
1278 is not a function. */
1280 /* If we are creating a shared library, we must presume that the
1281 only references to the symbol are via the global offset table.
1282 For such cases we need not do anything here; the relocations will
1283 be handled correctly by relocate_section. */
1284 if (info->shared)
1285 return TRUE;
1287 if (h->size == 0)
1289 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1290 h->root.root.string);
1291 return TRUE;
1294 /* We must allocate the symbol in our .dynbss section, which will
1295 become part of the .bss section of the executable. There will be
1296 an entry for this symbol in the .dynsym section. The dynamic
1297 object will contain position independent code, so all references
1298 from the dynamic object to this symbol will go through the global
1299 offset table. The dynamic linker will use the .dynsym entry to
1300 determine the address it must put in the global offset table, so
1301 both the dynamic object and the regular object will refer to the
1302 same memory location for the variable. */
1304 s = bfd_get_section_by_name (dynobj, ".dynbss");
1305 BFD_ASSERT (s != NULL);
1307 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1308 copy the initial value out of the dynamic object and into the
1309 runtime process image. We need to remember the offset into the
1310 .rela.bss section we are going to use. */
1311 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1313 asection *srel;
1315 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1316 BFD_ASSERT (srel != NULL);
1317 srel->size += sizeof (Elf32_External_Rela);
1318 h->needs_copy = 1;
1321 /* We need to figure out the alignment required for this symbol. I
1322 have no idea how ELF linkers handle this. */
1323 power_of_two = bfd_log2 (h->size);
1324 if (power_of_two > 3)
1325 power_of_two = 3;
1327 /* Apply the required alignment. */
1328 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1329 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1331 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
1332 return FALSE;
1335 /* Define the symbol as being at this point in the section. */
1336 h->root.u.def.section = s;
1337 h->root.u.def.value = s->size;
1339 /* Increment the section size to make room for the symbol. */
1340 s->size += h->size;
1342 return TRUE;
1345 /* Set the sizes of the dynamic sections. */
1347 static bfd_boolean
1348 elf_m68k_size_dynamic_sections (output_bfd, info)
1349 bfd *output_bfd ATTRIBUTE_UNUSED;
1350 struct bfd_link_info *info;
1352 bfd *dynobj;
1353 asection *s;
1354 bfd_boolean plt;
1355 bfd_boolean relocs;
1357 dynobj = elf_hash_table (info)->dynobj;
1358 BFD_ASSERT (dynobj != NULL);
1360 if (elf_hash_table (info)->dynamic_sections_created)
1362 /* Set the contents of the .interp section to the interpreter. */
1363 if (info->executable)
1365 s = bfd_get_section_by_name (dynobj, ".interp");
1366 BFD_ASSERT (s != NULL);
1367 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1368 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1371 else
1373 /* We may have created entries in the .rela.got section.
1374 However, if we are not creating the dynamic sections, we will
1375 not actually use these entries. Reset the size of .rela.got,
1376 which will cause it to get stripped from the output file
1377 below. */
1378 s = bfd_get_section_by_name (dynobj, ".rela.got");
1379 if (s != NULL)
1380 s->size = 0;
1383 /* If this is a -Bsymbolic shared link, then we need to discard all
1384 PC relative relocs against symbols defined in a regular object.
1385 For the normal shared case we discard the PC relative relocs
1386 against symbols that have become local due to visibility changes.
1387 We allocated space for them in the check_relocs routine, but we
1388 will not fill them in in the relocate_section routine. */
1389 if (info->shared)
1390 elf_link_hash_traverse (elf_hash_table (info),
1391 elf_m68k_discard_copies,
1392 (PTR) info);
1394 /* The check_relocs and adjust_dynamic_symbol entry points have
1395 determined the sizes of the various dynamic sections. Allocate
1396 memory for them. */
1397 plt = FALSE;
1398 relocs = FALSE;
1399 for (s = dynobj->sections; s != NULL; s = s->next)
1401 const char *name;
1403 if ((s->flags & SEC_LINKER_CREATED) == 0)
1404 continue;
1406 /* It's OK to base decisions on the section name, because none
1407 of the dynobj section names depend upon the input files. */
1408 name = bfd_get_section_name (dynobj, s);
1410 if (strcmp (name, ".plt") == 0)
1412 /* Remember whether there is a PLT. */
1413 plt = s->size != 0;
1415 else if (CONST_STRNEQ (name, ".rela"))
1417 if (s->size != 0)
1419 relocs = TRUE;
1421 /* We use the reloc_count field as a counter if we need
1422 to copy relocs into the output file. */
1423 s->reloc_count = 0;
1426 else if (! CONST_STRNEQ (name, ".got")
1427 && strcmp (name, ".dynbss") != 0)
1429 /* It's not one of our sections, so don't allocate space. */
1430 continue;
1433 if (s->size == 0)
1435 /* If we don't need this section, strip it from the
1436 output file. This is mostly to handle .rela.bss and
1437 .rela.plt. We must create both sections in
1438 create_dynamic_sections, because they must be created
1439 before the linker maps input sections to output
1440 sections. The linker does that before
1441 adjust_dynamic_symbol is called, and it is that
1442 function which decides whether anything needs to go
1443 into these sections. */
1444 s->flags |= SEC_EXCLUDE;
1445 continue;
1448 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1449 continue;
1451 /* Allocate memory for the section contents. */
1452 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1453 Unused entries should be reclaimed before the section's contents
1454 are written out, but at the moment this does not happen. Thus in
1455 order to prevent writing out garbage, we initialise the section's
1456 contents to zero. */
1457 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1458 if (s->contents == NULL)
1459 return FALSE;
1462 if (elf_hash_table (info)->dynamic_sections_created)
1464 /* Add some entries to the .dynamic section. We fill in the
1465 values later, in elf_m68k_finish_dynamic_sections, but we
1466 must add the entries now so that we get the correct size for
1467 the .dynamic section. The DT_DEBUG entry is filled in by the
1468 dynamic linker and used by the debugger. */
1469 #define add_dynamic_entry(TAG, VAL) \
1470 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1472 if (!info->shared)
1474 if (!add_dynamic_entry (DT_DEBUG, 0))
1475 return FALSE;
1478 if (plt)
1480 if (!add_dynamic_entry (DT_PLTGOT, 0)
1481 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1482 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1483 || !add_dynamic_entry (DT_JMPREL, 0))
1484 return FALSE;
1487 if (relocs)
1489 if (!add_dynamic_entry (DT_RELA, 0)
1490 || !add_dynamic_entry (DT_RELASZ, 0)
1491 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1492 return FALSE;
1495 if ((info->flags & DF_TEXTREL) != 0)
1497 if (!add_dynamic_entry (DT_TEXTREL, 0))
1498 return FALSE;
1501 #undef add_dynamic_entry
1503 return TRUE;
1506 /* This function is called via elf_link_hash_traverse if we are
1507 creating a shared object. In the -Bsymbolic case it discards the
1508 space allocated to copy PC relative relocs against symbols which
1509 are defined in regular objects. For the normal shared case, it
1510 discards space for pc-relative relocs that have become local due to
1511 symbol visibility changes. We allocated space for them in the
1512 check_relocs routine, but we won't fill them in in the
1513 relocate_section routine.
1515 We also check whether any of the remaining relocations apply
1516 against a readonly section, and set the DF_TEXTREL flag in this
1517 case. */
1519 static bfd_boolean
1520 elf_m68k_discard_copies (h, inf)
1521 struct elf_link_hash_entry *h;
1522 PTR inf;
1524 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1525 struct elf_m68k_pcrel_relocs_copied *s;
1527 if (h->root.type == bfd_link_hash_warning)
1528 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1530 if (!h->def_regular
1531 || (!info->symbolic
1532 && !h->forced_local))
1534 if ((info->flags & DF_TEXTREL) == 0)
1536 /* Look for relocations against read-only sections. */
1537 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1538 s != NULL;
1539 s = s->next)
1540 if ((s->section->flags & SEC_READONLY) != 0)
1542 info->flags |= DF_TEXTREL;
1543 break;
1547 return TRUE;
1550 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1551 s != NULL;
1552 s = s->next)
1553 s->section->size -= s->count * sizeof (Elf32_External_Rela);
1555 return TRUE;
1558 /* Relocate an M68K ELF section. */
1560 static bfd_boolean
1561 elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1562 contents, relocs, local_syms, local_sections)
1563 bfd *output_bfd;
1564 struct bfd_link_info *info;
1565 bfd *input_bfd;
1566 asection *input_section;
1567 bfd_byte *contents;
1568 Elf_Internal_Rela *relocs;
1569 Elf_Internal_Sym *local_syms;
1570 asection **local_sections;
1572 bfd *dynobj;
1573 Elf_Internal_Shdr *symtab_hdr;
1574 struct elf_link_hash_entry **sym_hashes;
1575 bfd_vma *local_got_offsets;
1576 asection *sgot;
1577 asection *splt;
1578 asection *sreloc;
1579 Elf_Internal_Rela *rel;
1580 Elf_Internal_Rela *relend;
1582 dynobj = elf_hash_table (info)->dynobj;
1583 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1584 sym_hashes = elf_sym_hashes (input_bfd);
1585 local_got_offsets = elf_local_got_offsets (input_bfd);
1587 sgot = NULL;
1588 splt = NULL;
1589 sreloc = NULL;
1591 rel = relocs;
1592 relend = relocs + input_section->reloc_count;
1593 for (; rel < relend; rel++)
1595 int r_type;
1596 reloc_howto_type *howto;
1597 unsigned long r_symndx;
1598 struct elf_link_hash_entry *h;
1599 Elf_Internal_Sym *sym;
1600 asection *sec;
1601 bfd_vma relocation;
1602 bfd_boolean unresolved_reloc;
1603 bfd_reloc_status_type r;
1605 r_type = ELF32_R_TYPE (rel->r_info);
1606 if (r_type < 0 || r_type >= (int) R_68K_max)
1608 bfd_set_error (bfd_error_bad_value);
1609 return FALSE;
1611 howto = howto_table + r_type;
1613 r_symndx = ELF32_R_SYM (rel->r_info);
1615 h = NULL;
1616 sym = NULL;
1617 sec = NULL;
1618 unresolved_reloc = FALSE;
1620 if (r_symndx < symtab_hdr->sh_info)
1622 sym = local_syms + r_symndx;
1623 sec = local_sections[r_symndx];
1624 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1626 else
1628 bfd_boolean warned;
1630 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1631 r_symndx, symtab_hdr, sym_hashes,
1632 h, sec, relocation,
1633 unresolved_reloc, warned);
1636 if (sec != NULL && elf_discarded_section (sec))
1638 /* For relocs against symbols from removed linkonce sections,
1639 or sections discarded by a linker script, we just want the
1640 section contents zeroed. Avoid any special processing. */
1641 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1642 rel->r_info = 0;
1643 rel->r_addend = 0;
1644 continue;
1647 if (info->relocatable)
1648 continue;
1650 switch (r_type)
1652 case R_68K_GOT8:
1653 case R_68K_GOT16:
1654 case R_68K_GOT32:
1655 /* Relocation is to the address of the entry for this symbol
1656 in the global offset table. */
1657 if (h != NULL
1658 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1659 break;
1660 /* Fall through. */
1661 case R_68K_GOT8O:
1662 case R_68K_GOT16O:
1663 case R_68K_GOT32O:
1664 /* Relocation is the offset of the entry for this symbol in
1665 the global offset table. */
1668 bfd_vma off;
1670 if (sgot == NULL)
1672 sgot = bfd_get_section_by_name (dynobj, ".got");
1673 BFD_ASSERT (sgot != NULL);
1676 if (h != NULL)
1678 bfd_boolean dyn;
1680 off = h->got.offset;
1681 BFD_ASSERT (off != (bfd_vma) -1);
1683 dyn = elf_hash_table (info)->dynamic_sections_created;
1684 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1685 || (info->shared
1686 && (info->symbolic
1687 || h->dynindx == -1
1688 || h->forced_local)
1689 && h->def_regular))
1691 /* This is actually a static link, or it is a
1692 -Bsymbolic link and the symbol is defined
1693 locally, or the symbol was forced to be local
1694 because of a version file.. We must initialize
1695 this entry in the global offset table. Since
1696 the offset must always be a multiple of 4, we
1697 use the least significant bit to record whether
1698 we have initialized it already.
1700 When doing a dynamic link, we create a .rela.got
1701 relocation entry to initialize the value. This
1702 is done in the finish_dynamic_symbol routine. */
1703 if ((off & 1) != 0)
1704 off &= ~1;
1705 else
1707 bfd_put_32 (output_bfd, relocation,
1708 sgot->contents + off);
1709 h->got.offset |= 1;
1712 else
1713 unresolved_reloc = FALSE;
1715 else
1717 BFD_ASSERT (local_got_offsets != NULL
1718 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1720 off = local_got_offsets[r_symndx];
1722 /* The offset must always be a multiple of 4. We use
1723 the least significant bit to record whether we have
1724 already generated the necessary reloc. */
1725 if ((off & 1) != 0)
1726 off &= ~1;
1727 else
1729 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1731 if (info->shared)
1733 asection *s;
1734 Elf_Internal_Rela outrel;
1735 bfd_byte *loc;
1737 s = bfd_get_section_by_name (dynobj, ".rela.got");
1738 BFD_ASSERT (s != NULL);
1740 outrel.r_offset = (sgot->output_section->vma
1741 + sgot->output_offset
1742 + off);
1743 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1744 outrel.r_addend = relocation;
1745 loc = s->contents;
1746 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1747 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1750 local_got_offsets[r_symndx] |= 1;
1754 relocation = sgot->output_offset + off;
1755 if (r_type == R_68K_GOT8O
1756 || r_type == R_68K_GOT16O
1757 || r_type == R_68K_GOT32O)
1759 /* This relocation does not use the addend. */
1760 rel->r_addend = 0;
1762 else
1763 relocation += sgot->output_section->vma;
1765 break;
1767 case R_68K_PLT8:
1768 case R_68K_PLT16:
1769 case R_68K_PLT32:
1770 /* Relocation is to the entry for this symbol in the
1771 procedure linkage table. */
1773 /* Resolve a PLTxx reloc against a local symbol directly,
1774 without using the procedure linkage table. */
1775 if (h == NULL)
1776 break;
1778 if (h->plt.offset == (bfd_vma) -1
1779 || !elf_hash_table (info)->dynamic_sections_created)
1781 /* We didn't make a PLT entry for this symbol. This
1782 happens when statically linking PIC code, or when
1783 using -Bsymbolic. */
1784 break;
1787 if (splt == NULL)
1789 splt = bfd_get_section_by_name (dynobj, ".plt");
1790 BFD_ASSERT (splt != NULL);
1793 relocation = (splt->output_section->vma
1794 + splt->output_offset
1795 + h->plt.offset);
1796 unresolved_reloc = FALSE;
1797 break;
1799 case R_68K_PLT8O:
1800 case R_68K_PLT16O:
1801 case R_68K_PLT32O:
1802 /* Relocation is the offset of the entry for this symbol in
1803 the procedure linkage table. */
1804 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1806 if (splt == NULL)
1808 splt = bfd_get_section_by_name (dynobj, ".plt");
1809 BFD_ASSERT (splt != NULL);
1812 relocation = h->plt.offset;
1813 unresolved_reloc = FALSE;
1815 /* This relocation does not use the addend. */
1816 rel->r_addend = 0;
1818 break;
1820 case R_68K_PC8:
1821 case R_68K_PC16:
1822 case R_68K_PC32:
1823 if (h == NULL
1824 || (info->shared
1825 && h->forced_local))
1826 break;
1827 /* Fall through. */
1828 case R_68K_8:
1829 case R_68K_16:
1830 case R_68K_32:
1831 if (info->shared
1832 && r_symndx != 0
1833 && (input_section->flags & SEC_ALLOC) != 0
1834 && (h == NULL
1835 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1836 || h->root.type != bfd_link_hash_undefweak)
1837 && ((r_type != R_68K_PC8
1838 && r_type != R_68K_PC16
1839 && r_type != R_68K_PC32)
1840 || (h != NULL
1841 && h->dynindx != -1
1842 && (!info->symbolic
1843 || !h->def_regular))))
1845 Elf_Internal_Rela outrel;
1846 bfd_byte *loc;
1847 bfd_boolean skip, relocate;
1849 /* When generating a shared object, these relocations
1850 are copied into the output file to be resolved at run
1851 time. */
1853 skip = FALSE;
1854 relocate = FALSE;
1856 outrel.r_offset =
1857 _bfd_elf_section_offset (output_bfd, info, input_section,
1858 rel->r_offset);
1859 if (outrel.r_offset == (bfd_vma) -1)
1860 skip = TRUE;
1861 else if (outrel.r_offset == (bfd_vma) -2)
1862 skip = TRUE, relocate = TRUE;
1863 outrel.r_offset += (input_section->output_section->vma
1864 + input_section->output_offset);
1866 if (skip)
1867 memset (&outrel, 0, sizeof outrel);
1868 else if (h != NULL
1869 && h->dynindx != -1
1870 && (r_type == R_68K_PC8
1871 || r_type == R_68K_PC16
1872 || r_type == R_68K_PC32
1873 || !info->shared
1874 || !info->symbolic
1875 || !h->def_regular))
1877 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1878 outrel.r_addend = rel->r_addend;
1880 else
1882 /* This symbol is local, or marked to become local. */
1883 outrel.r_addend = relocation + rel->r_addend;
1885 if (r_type == R_68K_32)
1887 relocate = TRUE;
1888 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1890 else
1892 long indx;
1894 if (bfd_is_abs_section (sec))
1895 indx = 0;
1896 else if (sec == NULL || sec->owner == NULL)
1898 bfd_set_error (bfd_error_bad_value);
1899 return FALSE;
1901 else
1903 asection *osec;
1905 /* We are turning this relocation into one
1906 against a section symbol. It would be
1907 proper to subtract the symbol's value,
1908 osec->vma, from the emitted reloc addend,
1909 but ld.so expects buggy relocs. */
1910 osec = sec->output_section;
1911 indx = elf_section_data (osec)->dynindx;
1912 if (indx == 0)
1914 struct elf_link_hash_table *htab;
1915 htab = elf_hash_table (info);
1916 osec = htab->text_index_section;
1917 indx = elf_section_data (osec)->dynindx;
1919 BFD_ASSERT (indx != 0);
1922 outrel.r_info = ELF32_R_INFO (indx, r_type);
1926 sreloc = elf_section_data (input_section)->sreloc;
1927 if (sreloc == NULL)
1928 abort ();
1930 loc = sreloc->contents;
1931 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1932 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1934 /* This reloc will be computed at runtime, so there's no
1935 need to do anything now, except for R_68K_32
1936 relocations that have been turned into
1937 R_68K_RELATIVE. */
1938 if (!relocate)
1939 continue;
1942 break;
1944 case R_68K_GNU_VTINHERIT:
1945 case R_68K_GNU_VTENTRY:
1946 /* These are no-ops in the end. */
1947 continue;
1949 default:
1950 break;
1953 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1954 because such sections are not SEC_ALLOC and thus ld.so will
1955 not process them. */
1956 if (unresolved_reloc
1957 && !((input_section->flags & SEC_DEBUGGING) != 0
1958 && h->def_dynamic))
1960 (*_bfd_error_handler)
1961 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1962 input_bfd,
1963 input_section,
1964 (long) rel->r_offset,
1965 howto->name,
1966 h->root.root.string);
1967 return FALSE;
1970 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1971 contents, rel->r_offset,
1972 relocation, rel->r_addend);
1974 if (r != bfd_reloc_ok)
1976 const char *name;
1978 if (h != NULL)
1979 name = h->root.root.string;
1980 else
1982 name = bfd_elf_string_from_elf_section (input_bfd,
1983 symtab_hdr->sh_link,
1984 sym->st_name);
1985 if (name == NULL)
1986 return FALSE;
1987 if (*name == '\0')
1988 name = bfd_section_name (input_bfd, sec);
1991 if (r == bfd_reloc_overflow)
1993 if (!(info->callbacks->reloc_overflow
1994 (info, (h ? &h->root : NULL), name, howto->name,
1995 (bfd_vma) 0, input_bfd, input_section,
1996 rel->r_offset)))
1997 return FALSE;
1999 else
2001 (*_bfd_error_handler)
2002 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2003 input_bfd, input_section,
2004 (long) rel->r_offset, name, (int) r);
2005 return FALSE;
2010 return TRUE;
2013 /* Install an M_68K_PC32 relocation against VALUE at offset OFFSET
2014 into section SEC. */
2016 static void
2017 elf_m68k_install_pc32 (asection *sec, bfd_vma offset, bfd_vma value)
2019 /* Make VALUE PC-relative. */
2020 value -= sec->output_section->vma + offset;
2022 /* Apply any in-place addend. */
2023 value += bfd_get_32 (sec->owner, sec->contents + offset);
2025 bfd_put_32 (sec->owner, value, sec->contents + offset);
2028 /* Finish up dynamic symbol handling. We set the contents of various
2029 dynamic sections here. */
2031 static bfd_boolean
2032 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
2033 bfd *output_bfd;
2034 struct bfd_link_info *info;
2035 struct elf_link_hash_entry *h;
2036 Elf_Internal_Sym *sym;
2038 bfd *dynobj;
2040 dynobj = elf_hash_table (info)->dynobj;
2042 if (h->plt.offset != (bfd_vma) -1)
2044 const struct elf_m68k_plt_info *plt_info;
2045 asection *splt;
2046 asection *sgot;
2047 asection *srela;
2048 bfd_vma plt_index;
2049 bfd_vma got_offset;
2050 Elf_Internal_Rela rela;
2051 bfd_byte *loc;
2053 /* This symbol has an entry in the procedure linkage table. Set
2054 it up. */
2056 BFD_ASSERT (h->dynindx != -1);
2058 plt_info = elf_m68k_hash_table (info)->plt_info;
2059 splt = bfd_get_section_by_name (dynobj, ".plt");
2060 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2061 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2062 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
2064 /* Get the index in the procedure linkage table which
2065 corresponds to this symbol. This is the index of this symbol
2066 in all the symbols for which we are making plt entries. The
2067 first entry in the procedure linkage table is reserved. */
2068 plt_index = (h->plt.offset / plt_info->size) - 1;
2070 /* Get the offset into the .got table of the entry that
2071 corresponds to this function. Each .got entry is 4 bytes.
2072 The first three are reserved. */
2073 got_offset = (plt_index + 3) * 4;
2075 memcpy (splt->contents + h->plt.offset,
2076 plt_info->symbol_entry,
2077 plt_info->size);
2079 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.got,
2080 (sgot->output_section->vma
2081 + sgot->output_offset
2082 + got_offset));
2084 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
2085 splt->contents
2086 + h->plt.offset
2087 + plt_info->symbol_resolve_entry + 2);
2089 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.plt,
2090 splt->output_section->vma);
2092 /* Fill in the entry in the global offset table. */
2093 bfd_put_32 (output_bfd,
2094 (splt->output_section->vma
2095 + splt->output_offset
2096 + h->plt.offset
2097 + plt_info->symbol_resolve_entry),
2098 sgot->contents + got_offset);
2100 /* Fill in the entry in the .rela.plt section. */
2101 rela.r_offset = (sgot->output_section->vma
2102 + sgot->output_offset
2103 + got_offset);
2104 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
2105 rela.r_addend = 0;
2106 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
2107 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2109 if (!h->def_regular)
2111 /* Mark the symbol as undefined, rather than as defined in
2112 the .plt section. Leave the value alone. */
2113 sym->st_shndx = SHN_UNDEF;
2117 if (h->got.offset != (bfd_vma) -1)
2119 asection *sgot;
2120 asection *srela;
2121 Elf_Internal_Rela rela;
2122 bfd_byte *loc;
2124 /* This symbol has an entry in the global offset table. Set it
2125 up. */
2127 sgot = bfd_get_section_by_name (dynobj, ".got");
2128 srela = bfd_get_section_by_name (dynobj, ".rela.got");
2129 BFD_ASSERT (sgot != NULL && srela != NULL);
2131 rela.r_offset = (sgot->output_section->vma
2132 + sgot->output_offset
2133 + (h->got.offset &~ (bfd_vma) 1));
2135 /* If this is a -Bsymbolic link, and the symbol is defined
2136 locally, we just want to emit a RELATIVE reloc. Likewise if
2137 the symbol was forced to be local because of a version file.
2138 The entry in the global offset table will already have been
2139 initialized in the relocate_section function. */
2140 if (info->shared
2141 && (info->symbolic
2142 || h->dynindx == -1
2143 || h->forced_local)
2144 && h->def_regular)
2146 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
2147 rela.r_addend = bfd_get_signed_32 (output_bfd,
2148 (sgot->contents
2149 + (h->got.offset &~ (bfd_vma) 1)));
2151 else
2153 bfd_put_32 (output_bfd, (bfd_vma) 0,
2154 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
2155 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
2156 rela.r_addend = 0;
2159 loc = srela->contents;
2160 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2161 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2164 if (h->needs_copy)
2166 asection *s;
2167 Elf_Internal_Rela rela;
2168 bfd_byte *loc;
2170 /* This symbol needs a copy reloc. Set it up. */
2172 BFD_ASSERT (h->dynindx != -1
2173 && (h->root.type == bfd_link_hash_defined
2174 || h->root.type == bfd_link_hash_defweak));
2176 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2177 ".rela.bss");
2178 BFD_ASSERT (s != NULL);
2180 rela.r_offset = (h->root.u.def.value
2181 + h->root.u.def.section->output_section->vma
2182 + h->root.u.def.section->output_offset);
2183 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
2184 rela.r_addend = 0;
2185 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2186 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2189 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2190 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2191 || h == elf_hash_table (info)->hgot)
2192 sym->st_shndx = SHN_ABS;
2194 return TRUE;
2197 /* Finish up the dynamic sections. */
2199 static bfd_boolean
2200 elf_m68k_finish_dynamic_sections (output_bfd, info)
2201 bfd *output_bfd;
2202 struct bfd_link_info *info;
2204 bfd *dynobj;
2205 asection *sgot;
2206 asection *sdyn;
2208 dynobj = elf_hash_table (info)->dynobj;
2210 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2211 BFD_ASSERT (sgot != NULL);
2212 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2214 if (elf_hash_table (info)->dynamic_sections_created)
2216 asection *splt;
2217 Elf32_External_Dyn *dyncon, *dynconend;
2219 splt = bfd_get_section_by_name (dynobj, ".plt");
2220 BFD_ASSERT (splt != NULL && sdyn != NULL);
2222 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2223 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2224 for (; dyncon < dynconend; dyncon++)
2226 Elf_Internal_Dyn dyn;
2227 const char *name;
2228 asection *s;
2230 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2232 switch (dyn.d_tag)
2234 default:
2235 break;
2237 case DT_PLTGOT:
2238 name = ".got";
2239 goto get_vma;
2240 case DT_JMPREL:
2241 name = ".rela.plt";
2242 get_vma:
2243 s = bfd_get_section_by_name (output_bfd, name);
2244 BFD_ASSERT (s != NULL);
2245 dyn.d_un.d_ptr = s->vma;
2246 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2247 break;
2249 case DT_PLTRELSZ:
2250 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2251 BFD_ASSERT (s != NULL);
2252 dyn.d_un.d_val = s->size;
2253 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2254 break;
2256 case DT_RELASZ:
2257 /* The procedure linkage table relocs (DT_JMPREL) should
2258 not be included in the overall relocs (DT_RELA).
2259 Therefore, we override the DT_RELASZ entry here to
2260 make it not include the JMPREL relocs. Since the
2261 linker script arranges for .rela.plt to follow all
2262 other relocation sections, we don't have to worry
2263 about changing the DT_RELA entry. */
2264 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2265 if (s != NULL)
2266 dyn.d_un.d_val -= s->size;
2267 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2268 break;
2272 /* Fill in the first entry in the procedure linkage table. */
2273 if (splt->size > 0)
2275 const struct elf_m68k_plt_info *plt_info;
2277 plt_info = elf_m68k_hash_table (info)->plt_info;
2278 memcpy (splt->contents, plt_info->plt0_entry, plt_info->size);
2280 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got4,
2281 (sgot->output_section->vma
2282 + sgot->output_offset
2283 + 4));
2285 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got8,
2286 (sgot->output_section->vma
2287 + sgot->output_offset
2288 + 8));
2290 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2291 = plt_info->size;
2295 /* Fill in the first three entries in the global offset table. */
2296 if (sgot->size > 0)
2298 if (sdyn == NULL)
2299 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2300 else
2301 bfd_put_32 (output_bfd,
2302 sdyn->output_section->vma + sdyn->output_offset,
2303 sgot->contents);
2304 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2305 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2308 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2310 return TRUE;
2313 /* Given a .data section and a .emreloc in-memory section, store
2314 relocation information into the .emreloc section which can be
2315 used at runtime to relocate the section. This is called by the
2316 linker when the --embedded-relocs switch is used. This is called
2317 after the add_symbols entry point has been called for all the
2318 objects, and before the final_link entry point is called. */
2320 bfd_boolean
2321 bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2322 bfd *abfd;
2323 struct bfd_link_info *info;
2324 asection *datasec;
2325 asection *relsec;
2326 char **errmsg;
2328 Elf_Internal_Shdr *symtab_hdr;
2329 Elf_Internal_Sym *isymbuf = NULL;
2330 Elf_Internal_Rela *internal_relocs = NULL;
2331 Elf_Internal_Rela *irel, *irelend;
2332 bfd_byte *p;
2333 bfd_size_type amt;
2335 BFD_ASSERT (! info->relocatable);
2337 *errmsg = NULL;
2339 if (datasec->reloc_count == 0)
2340 return TRUE;
2342 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2344 /* Get a copy of the native relocations. */
2345 internal_relocs = (_bfd_elf_link_read_relocs
2346 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2347 info->keep_memory));
2348 if (internal_relocs == NULL)
2349 goto error_return;
2351 amt = (bfd_size_type) datasec->reloc_count * 12;
2352 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
2353 if (relsec->contents == NULL)
2354 goto error_return;
2356 p = relsec->contents;
2358 irelend = internal_relocs + datasec->reloc_count;
2359 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2361 asection *targetsec;
2363 /* We are going to write a four byte longword into the runtime
2364 reloc section. The longword will be the address in the data
2365 section which must be relocated. It is followed by the name
2366 of the target section NUL-padded or truncated to 8
2367 characters. */
2369 /* We can only relocate absolute longword relocs at run time. */
2370 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2372 *errmsg = _("unsupported reloc type");
2373 bfd_set_error (bfd_error_bad_value);
2374 goto error_return;
2377 /* Get the target section referred to by the reloc. */
2378 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2380 /* A local symbol. */
2381 Elf_Internal_Sym *isym;
2383 /* Read this BFD's local symbols if we haven't done so already. */
2384 if (isymbuf == NULL)
2386 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2387 if (isymbuf == NULL)
2388 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2389 symtab_hdr->sh_info, 0,
2390 NULL, NULL, NULL);
2391 if (isymbuf == NULL)
2392 goto error_return;
2395 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2396 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2398 else
2400 unsigned long indx;
2401 struct elf_link_hash_entry *h;
2403 /* An external symbol. */
2404 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2405 h = elf_sym_hashes (abfd)[indx];
2406 BFD_ASSERT (h != NULL);
2407 if (h->root.type == bfd_link_hash_defined
2408 || h->root.type == bfd_link_hash_defweak)
2409 targetsec = h->root.u.def.section;
2410 else
2411 targetsec = NULL;
2414 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2415 memset (p + 4, 0, 8);
2416 if (targetsec != NULL)
2417 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
2420 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2421 free (isymbuf);
2422 if (internal_relocs != NULL
2423 && elf_section_data (datasec)->relocs != internal_relocs)
2424 free (internal_relocs);
2425 return TRUE;
2427 error_return:
2428 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2429 free (isymbuf);
2430 if (internal_relocs != NULL
2431 && elf_section_data (datasec)->relocs != internal_relocs)
2432 free (internal_relocs);
2433 return FALSE;
2436 static enum elf_reloc_type_class
2437 elf32_m68k_reloc_type_class (rela)
2438 const Elf_Internal_Rela *rela;
2440 switch ((int) ELF32_R_TYPE (rela->r_info))
2442 case R_68K_RELATIVE:
2443 return reloc_class_relative;
2444 case R_68K_JMP_SLOT:
2445 return reloc_class_plt;
2446 case R_68K_COPY:
2447 return reloc_class_copy;
2448 default:
2449 return reloc_class_normal;
2453 /* Return address for Ith PLT stub in section PLT, for relocation REL
2454 or (bfd_vma) -1 if it should not be included. */
2456 static bfd_vma
2457 elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
2458 const arelent *rel ATTRIBUTE_UNUSED)
2460 return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
2463 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
2464 #define TARGET_BIG_NAME "elf32-m68k"
2465 #define ELF_MACHINE_CODE EM_68K
2466 #define ELF_MAXPAGESIZE 0x2000
2467 #define elf_backend_create_dynamic_sections \
2468 _bfd_elf_create_dynamic_sections
2469 #define bfd_elf32_bfd_link_hash_table_create \
2470 elf_m68k_link_hash_table_create
2471 #define bfd_elf32_bfd_final_link bfd_elf_gc_common_final_link
2473 #define elf_backend_check_relocs elf_m68k_check_relocs
2474 #define elf_backend_always_size_sections \
2475 elf_m68k_always_size_sections
2476 #define elf_backend_adjust_dynamic_symbol \
2477 elf_m68k_adjust_dynamic_symbol
2478 #define elf_backend_size_dynamic_sections \
2479 elf_m68k_size_dynamic_sections
2480 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
2481 #define elf_backend_relocate_section elf_m68k_relocate_section
2482 #define elf_backend_finish_dynamic_symbol \
2483 elf_m68k_finish_dynamic_symbol
2484 #define elf_backend_finish_dynamic_sections \
2485 elf_m68k_finish_dynamic_sections
2486 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
2487 #define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
2488 #define bfd_elf32_bfd_merge_private_bfd_data \
2489 elf32_m68k_merge_private_bfd_data
2490 #define bfd_elf32_bfd_set_private_flags \
2491 elf32_m68k_set_private_flags
2492 #define bfd_elf32_bfd_print_private_bfd_data \
2493 elf32_m68k_print_private_bfd_data
2494 #define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
2495 #define elf_backend_plt_sym_val elf_m68k_plt_sym_val
2496 #define elf_backend_object_p elf32_m68k_object_p
2498 #define elf_backend_can_gc_sections 1
2499 #define elf_backend_can_refcount 1
2500 #define elf_backend_want_got_plt 1
2501 #define elf_backend_plt_readonly 1
2502 #define elf_backend_want_plt_sym 0
2503 #define elf_backend_got_header_size 12
2504 #define elf_backend_rela_normal 1
2506 #include "elf32-target.h"