2007-06-01 Steve Ellcey <sje@cup.hp.com>
[binutils.git] / bfd / elf32-m68k.c
blob82fad32fdd2a91ca57ea1de790d235d1d2aed785
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 "sysdep.h"
22 #include "bfd.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 ISAC_PLT_ENTRY_SIZE 24
305 static const bfd_byte elf_isac_plt0_entry[ISAC_PLT_ENTRY_SIZE] =
307 0x20, 0x3c, /* move.l #offset,%d0 */
308 0, 0, 0, 0, /* replaced with .got + 4 - . */
309 0x2e, 0xbb, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),(%sp) */
310 0x20, 0x3c, /* move.l #offset,%d0 */
311 0, 0, 0, 0, /* replaced with .got + 8 - . */
312 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
313 0x4e, 0xd0, /* jmp (%a0) */
314 0x4e, 0x71 /* nop */
317 /* Subsequent entries in a procedure linkage table look like this. */
319 static const bfd_byte elf_isac_plt_entry[ISAC_PLT_ENTRY_SIZE] =
321 0x20, 0x3c, /* move.l #offset,%d0 */
322 0, 0, 0, 0, /* replaced with (.got entry) - . */
323 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
324 0x4e, 0xd0, /* jmp (%a0) */
325 0x2f, 0x3c, /* move.l #offset,-(%sp) */
326 0, 0, 0, 0, /* replaced with offset into relocation table */
327 0x61, 0xff, /* bsr.l .plt */
328 0, 0, 0, 0 /* replaced with .plt - . */
331 static const struct elf_m68k_plt_info elf_isac_plt_info = {
332 ISAC_PLT_ENTRY_SIZE,
333 elf_isac_plt0_entry, { 2, 12},
334 elf_isac_plt_entry, { 2, 20 }, 12
337 #define CPU32_PLT_ENTRY_SIZE 24
338 /* Procedure linkage table entries for the cpu32 */
339 static const bfd_byte elf_cpu32_plt0_entry[CPU32_PLT_ENTRY_SIZE] =
341 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
342 0, 0, 0, 2, /* + (.got + 4) - . */
343 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
344 0, 0, 0, 2, /* + (.got + 8) - . */
345 0x4e, 0xd1, /* jmp %a1@ */
346 0, 0, 0, 0, /* pad out to 24 bytes. */
347 0, 0
350 static const bfd_byte elf_cpu32_plt_entry[CPU32_PLT_ENTRY_SIZE] =
352 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
353 0, 0, 0, 2, /* + (.got.plt entry) - . */
354 0x4e, 0xd1, /* jmp %a1@ */
355 0x2f, 0x3c, /* move.l #offset,-(%sp) */
356 0, 0, 0, 0, /* + reloc index */
357 0x60, 0xff, /* bra.l .plt */
358 0, 0, 0, 0, /* + .plt - . */
359 0, 0
362 static const struct elf_m68k_plt_info elf_cpu32_plt_info = {
363 CPU32_PLT_ENTRY_SIZE,
364 elf_cpu32_plt0_entry, { 4, 12 },
365 elf_cpu32_plt_entry, { 4, 18 }, 10
368 /* The m68k linker needs to keep track of the number of relocs that it
369 decides to copy in check_relocs for each symbol. This is so that it
370 can discard PC relative relocs if it doesn't need them when linking
371 with -Bsymbolic. We store the information in a field extending the
372 regular ELF linker hash table. */
374 /* This structure keeps track of the number of PC relative relocs we have
375 copied for a given symbol. */
377 struct elf_m68k_pcrel_relocs_copied
379 /* Next section. */
380 struct elf_m68k_pcrel_relocs_copied *next;
381 /* A section in dynobj. */
382 asection *section;
383 /* Number of relocs copied in this section. */
384 bfd_size_type count;
387 /* m68k ELF linker hash entry. */
389 struct elf_m68k_link_hash_entry
391 struct elf_link_hash_entry root;
393 /* Number of PC relative relocs copied for this symbol. */
394 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
397 #define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
399 /* m68k ELF linker hash table. */
401 struct elf_m68k_link_hash_table
403 struct elf_link_hash_table root;
405 /* Small local sym to section mapping cache. */
406 struct sym_sec_cache sym_sec;
408 /* The PLT format used by this link, or NULL if the format has not
409 yet been chosen. */
410 const struct elf_m68k_plt_info *plt_info;
413 /* Get the m68k ELF linker hash table from a link_info structure. */
415 #define elf_m68k_hash_table(p) \
416 ((struct elf_m68k_link_hash_table *) (p)->hash)
418 /* Create an entry in an m68k ELF linker hash table. */
420 static struct bfd_hash_entry *
421 elf_m68k_link_hash_newfunc (entry, table, string)
422 struct bfd_hash_entry *entry;
423 struct bfd_hash_table *table;
424 const char *string;
426 struct bfd_hash_entry *ret = entry;
428 /* Allocate the structure if it has not already been allocated by a
429 subclass. */
430 if (ret == NULL)
431 ret = bfd_hash_allocate (table,
432 sizeof (struct elf_m68k_link_hash_entry));
433 if (ret == NULL)
434 return ret;
436 /* Call the allocation method of the superclass. */
437 ret = _bfd_elf_link_hash_newfunc (ret, table, string);
438 if (ret != NULL)
439 elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
441 return ret;
444 /* Create an m68k ELF linker hash table. */
446 static struct bfd_link_hash_table *
447 elf_m68k_link_hash_table_create (abfd)
448 bfd *abfd;
450 struct elf_m68k_link_hash_table *ret;
451 bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
453 ret = (struct elf_m68k_link_hash_table *) bfd_malloc (amt);
454 if (ret == (struct elf_m68k_link_hash_table *) NULL)
455 return NULL;
457 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
458 elf_m68k_link_hash_newfunc,
459 sizeof (struct elf_m68k_link_hash_entry)))
461 free (ret);
462 return NULL;
465 ret->sym_sec.abfd = NULL;
466 ret->plt_info = NULL;
468 return &ret->root.root;
471 /* Set the right machine number. */
473 static bfd_boolean
474 elf32_m68k_object_p (bfd *abfd)
476 unsigned int mach = 0;
477 unsigned features = 0;
478 flagword eflags = elf_elfheader (abfd)->e_flags;
480 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
481 features |= m68000;
482 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
483 features |= cpu32;
484 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
485 features |= fido_a;
486 else
488 switch (eflags & EF_M68K_CF_ISA_MASK)
490 case EF_M68K_CF_ISA_A_NODIV:
491 features |= mcfisa_a;
492 break;
493 case EF_M68K_CF_ISA_A:
494 features |= mcfisa_a|mcfhwdiv;
495 break;
496 case EF_M68K_CF_ISA_A_PLUS:
497 features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
498 break;
499 case EF_M68K_CF_ISA_B_NOUSP:
500 features |= mcfisa_a|mcfisa_b|mcfhwdiv;
501 break;
502 case EF_M68K_CF_ISA_B:
503 features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
504 break;
505 case EF_M68K_CF_ISA_C:
506 features |= mcfisa_a|mcfisa_c|mcfhwdiv|mcfusp;
507 break;
509 switch (eflags & EF_M68K_CF_MAC_MASK)
511 case EF_M68K_CF_MAC:
512 features |= mcfmac;
513 break;
514 case EF_M68K_CF_EMAC:
515 features |= mcfemac;
516 break;
518 if (eflags & EF_M68K_CF_FLOAT)
519 features |= cfloat;
522 mach = bfd_m68k_features_to_mach (features);
523 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
525 return TRUE;
528 /* Keep m68k-specific flags in the ELF header. */
529 static bfd_boolean
530 elf32_m68k_set_private_flags (abfd, flags)
531 bfd *abfd;
532 flagword flags;
534 elf_elfheader (abfd)->e_flags = flags;
535 elf_flags_init (abfd) = TRUE;
536 return TRUE;
539 /* Merge backend specific data from an object file to the output
540 object file when linking. */
541 static bfd_boolean
542 elf32_m68k_merge_private_bfd_data (ibfd, obfd)
543 bfd *ibfd;
544 bfd *obfd;
546 flagword out_flags;
547 flagword in_flags;
548 flagword out_isa;
549 flagword in_isa;
550 const bfd_arch_info_type *arch_info;
552 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
553 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
554 return FALSE;
556 /* Get the merged machine. This checks for incompatibility between
557 Coldfire & non-Coldfire flags, incompability between different
558 Coldfire ISAs, and incompability between different MAC types. */
559 arch_info = bfd_arch_get_compatible (ibfd, obfd, FALSE);
560 if (!arch_info)
561 return FALSE;
563 bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
565 in_flags = elf_elfheader (ibfd)->e_flags;
566 if (!elf_flags_init (obfd))
568 elf_flags_init (obfd) = TRUE;
569 out_flags = in_flags;
571 else
573 out_flags = elf_elfheader (obfd)->e_flags;
574 unsigned int variant_mask;
576 if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
577 variant_mask = 0;
578 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
579 variant_mask = 0;
580 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
581 variant_mask = 0;
582 else
583 variant_mask = EF_M68K_CF_ISA_MASK;
585 in_isa = (in_flags & variant_mask);
586 out_isa = (out_flags & variant_mask);
587 if (in_isa > out_isa)
588 out_flags ^= in_isa ^ out_isa;
589 if (((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32
590 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
591 || ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO
592 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32))
593 out_flags = EF_M68K_FIDO;
594 else
595 out_flags |= in_flags ^ in_isa;
597 elf_elfheader (obfd)->e_flags = out_flags;
599 return TRUE;
602 /* Display the flags field. */
603 static bfd_boolean
604 elf32_m68k_print_private_bfd_data (abfd, ptr)
605 bfd *abfd;
606 PTR ptr;
608 FILE *file = (FILE *) ptr;
609 flagword eflags = elf_elfheader (abfd)->e_flags;
611 BFD_ASSERT (abfd != NULL && ptr != NULL);
613 /* Print normal ELF private data. */
614 _bfd_elf_print_private_bfd_data (abfd, ptr);
616 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
618 /* xgettext:c-format */
619 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
621 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
622 fprintf (file, " [m68000]");
623 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
624 fprintf (file, " [cpu32]");
625 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
626 fprintf (file, " [fido]");
627 else
629 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CFV4E)
630 fprintf (file, " [cfv4e]");
632 if (eflags & EF_M68K_CF_ISA_MASK)
634 char const *isa = _("unknown");
635 char const *mac = _("unknown");
636 char const *additional = "";
638 switch (eflags & EF_M68K_CF_ISA_MASK)
640 case EF_M68K_CF_ISA_A_NODIV:
641 isa = "A";
642 additional = " [nodiv]";
643 break;
644 case EF_M68K_CF_ISA_A:
645 isa = "A";
646 break;
647 case EF_M68K_CF_ISA_A_PLUS:
648 isa = "A+";
649 break;
650 case EF_M68K_CF_ISA_B_NOUSP:
651 isa = "B";
652 additional = " [nousp]";
653 break;
654 case EF_M68K_CF_ISA_B:
655 isa = "B";
656 break;
657 case EF_M68K_CF_ISA_C:
658 isa = "C";
659 break;
661 fprintf (file, " [isa %s]%s", isa, additional);
662 if (eflags & EF_M68K_CF_FLOAT)
663 fprintf (file, " [float]");
664 switch (eflags & EF_M68K_CF_MAC_MASK)
666 case 0:
667 mac = NULL;
668 break;
669 case EF_M68K_CF_MAC:
670 mac = "mac";
671 break;
672 case EF_M68K_CF_EMAC:
673 mac = "emac";
674 break;
676 if (mac)
677 fprintf (file, " [%s]", mac);
681 fputc ('\n', file);
683 return TRUE;
685 /* Look through the relocs for a section during the first phase, and
686 allocate space in the global offset table or procedure linkage
687 table. */
689 static bfd_boolean
690 elf_m68k_check_relocs (abfd, info, sec, relocs)
691 bfd *abfd;
692 struct bfd_link_info *info;
693 asection *sec;
694 const Elf_Internal_Rela *relocs;
696 bfd *dynobj;
697 Elf_Internal_Shdr *symtab_hdr;
698 struct elf_link_hash_entry **sym_hashes;
699 bfd_signed_vma *local_got_refcounts;
700 const Elf_Internal_Rela *rel;
701 const Elf_Internal_Rela *rel_end;
702 asection *sgot;
703 asection *srelgot;
704 asection *sreloc;
706 if (info->relocatable)
707 return TRUE;
709 dynobj = elf_hash_table (info)->dynobj;
710 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
711 sym_hashes = elf_sym_hashes (abfd);
712 local_got_refcounts = elf_local_got_refcounts (abfd);
714 sgot = NULL;
715 srelgot = NULL;
716 sreloc = NULL;
718 rel_end = relocs + sec->reloc_count;
719 for (rel = relocs; rel < rel_end; rel++)
721 unsigned long r_symndx;
722 struct elf_link_hash_entry *h;
724 r_symndx = ELF32_R_SYM (rel->r_info);
726 if (r_symndx < symtab_hdr->sh_info)
727 h = NULL;
728 else
730 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
731 while (h->root.type == bfd_link_hash_indirect
732 || h->root.type == bfd_link_hash_warning)
733 h = (struct elf_link_hash_entry *) h->root.u.i.link;
736 switch (ELF32_R_TYPE (rel->r_info))
738 case R_68K_GOT8:
739 case R_68K_GOT16:
740 case R_68K_GOT32:
741 if (h != NULL
742 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
743 break;
744 /* Fall through. */
745 case R_68K_GOT8O:
746 case R_68K_GOT16O:
747 case R_68K_GOT32O:
748 /* This symbol requires a global offset table entry. */
750 if (dynobj == NULL)
752 /* Create the .got section. */
753 elf_hash_table (info)->dynobj = dynobj = abfd;
754 if (!_bfd_elf_create_got_section (dynobj, info))
755 return FALSE;
758 if (sgot == NULL)
760 sgot = bfd_get_section_by_name (dynobj, ".got");
761 BFD_ASSERT (sgot != NULL);
764 if (srelgot == NULL
765 && (h != NULL || info->shared))
767 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
768 if (srelgot == NULL)
770 srelgot = bfd_make_section_with_flags (dynobj,
771 ".rela.got",
772 (SEC_ALLOC
773 | SEC_LOAD
774 | SEC_HAS_CONTENTS
775 | SEC_IN_MEMORY
776 | SEC_LINKER_CREATED
777 | SEC_READONLY));
778 if (srelgot == NULL
779 || !bfd_set_section_alignment (dynobj, srelgot, 2))
780 return FALSE;
784 if (h != NULL)
786 if (h->got.refcount == 0)
788 /* Make sure this symbol is output as a dynamic symbol. */
789 if (h->dynindx == -1
790 && !h->forced_local)
792 if (!bfd_elf_link_record_dynamic_symbol (info, h))
793 return FALSE;
796 /* Allocate space in the .got section. */
797 sgot->size += 4;
798 /* Allocate relocation space. */
799 srelgot->size += sizeof (Elf32_External_Rela);
801 h->got.refcount++;
803 else
805 /* This is a global offset table entry for a local symbol. */
806 if (local_got_refcounts == NULL)
808 bfd_size_type size;
810 size = symtab_hdr->sh_info;
811 size *= sizeof (bfd_signed_vma);
812 local_got_refcounts = ((bfd_signed_vma *)
813 bfd_zalloc (abfd, size));
814 if (local_got_refcounts == NULL)
815 return FALSE;
816 elf_local_got_refcounts (abfd) = local_got_refcounts;
818 if (local_got_refcounts[r_symndx] == 0)
820 sgot->size += 4;
821 if (info->shared)
823 /* If we are generating a shared object, we need to
824 output a R_68K_RELATIVE reloc so that the dynamic
825 linker can adjust this GOT entry. */
826 srelgot->size += sizeof (Elf32_External_Rela);
829 local_got_refcounts[r_symndx]++;
831 break;
833 case R_68K_PLT8:
834 case R_68K_PLT16:
835 case R_68K_PLT32:
836 /* This symbol requires a procedure linkage table entry. We
837 actually build the entry in adjust_dynamic_symbol,
838 because this might be a case of linking PIC code which is
839 never referenced by a dynamic object, in which case we
840 don't need to generate a procedure linkage table entry
841 after all. */
843 /* If this is a local symbol, we resolve it directly without
844 creating a procedure linkage table entry. */
845 if (h == NULL)
846 continue;
848 h->needs_plt = 1;
849 h->plt.refcount++;
850 break;
852 case R_68K_PLT8O:
853 case R_68K_PLT16O:
854 case R_68K_PLT32O:
855 /* This symbol requires a procedure linkage table entry. */
857 if (h == NULL)
859 /* It does not make sense to have this relocation for a
860 local symbol. FIXME: does it? How to handle it if
861 it does make sense? */
862 bfd_set_error (bfd_error_bad_value);
863 return FALSE;
866 /* Make sure this symbol is output as a dynamic symbol. */
867 if (h->dynindx == -1
868 && !h->forced_local)
870 if (!bfd_elf_link_record_dynamic_symbol (info, h))
871 return FALSE;
874 h->needs_plt = 1;
875 h->plt.refcount++;
876 break;
878 case R_68K_PC8:
879 case R_68K_PC16:
880 case R_68K_PC32:
881 /* If we are creating a shared library and this is not a local
882 symbol, we need to copy the reloc into the shared library.
883 However when linking with -Bsymbolic and this is a global
884 symbol which is defined in an object we are including in the
885 link (i.e., DEF_REGULAR is set), then we can resolve the
886 reloc directly. At this point we have not seen all the input
887 files, so it is possible that DEF_REGULAR is not set now but
888 will be set later (it is never cleared). We account for that
889 possibility below by storing information in the
890 pcrel_relocs_copied field of the hash table entry. */
891 if (!(info->shared
892 && (sec->flags & SEC_ALLOC) != 0
893 && h != NULL
894 && (!info->symbolic
895 || h->root.type == bfd_link_hash_defweak
896 || !h->def_regular)))
898 if (h != NULL)
900 /* Make sure a plt entry is created for this symbol if
901 it turns out to be a function defined by a dynamic
902 object. */
903 h->plt.refcount++;
905 break;
907 /* Fall through. */
908 case R_68K_8:
909 case R_68K_16:
910 case R_68K_32:
911 if (h != NULL)
913 /* Make sure a plt entry is created for this symbol if it
914 turns out to be a function defined by a dynamic object. */
915 h->plt.refcount++;
918 /* If we are creating a shared library, we need to copy the
919 reloc into the shared library. */
920 if (info->shared
921 && (sec->flags & SEC_ALLOC) != 0)
923 /* When creating a shared object, we must copy these
924 reloc types into the output file. We create a reloc
925 section in dynobj and make room for this reloc. */
926 if (sreloc == NULL)
928 const char *name;
930 name = (bfd_elf_string_from_elf_section
931 (abfd,
932 elf_elfheader (abfd)->e_shstrndx,
933 elf_section_data (sec)->rel_hdr.sh_name));
934 if (name == NULL)
935 return FALSE;
937 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
938 && strcmp (bfd_get_section_name (abfd, sec),
939 name + 5) == 0);
941 sreloc = bfd_get_section_by_name (dynobj, name);
942 if (sreloc == NULL)
944 sreloc = bfd_make_section_with_flags (dynobj,
945 name,
946 (SEC_ALLOC
947 | SEC_LOAD
948 | SEC_HAS_CONTENTS
949 | SEC_IN_MEMORY
950 | SEC_LINKER_CREATED
951 | SEC_READONLY));
952 if (sreloc == NULL
953 || !bfd_set_section_alignment (dynobj, sreloc, 2))
954 return FALSE;
956 elf_section_data (sec)->sreloc = sreloc;
959 if (sec->flags & SEC_READONLY
960 /* Don't set DF_TEXTREL yet for PC relative
961 relocations, they might be discarded later. */
962 && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
963 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
964 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
965 info->flags |= DF_TEXTREL;
967 sreloc->size += sizeof (Elf32_External_Rela);
969 /* We count the number of PC relative relocations we have
970 entered for this symbol, so that we can discard them
971 again if, in the -Bsymbolic case, the symbol is later
972 defined by a regular object, or, in the normal shared
973 case, the symbol is forced to be local. Note that this
974 function is only called if we are using an m68kelf linker
975 hash table, which means that h is really a pointer to an
976 elf_m68k_link_hash_entry. */
977 if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
978 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
979 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
981 struct elf_m68k_pcrel_relocs_copied *p;
982 struct elf_m68k_pcrel_relocs_copied **head;
984 if (h != NULL)
986 struct elf_m68k_link_hash_entry *eh
987 = elf_m68k_hash_entry (h);
988 head = &eh->pcrel_relocs_copied;
990 else
992 asection *s;
993 void *vpp;
995 s = (bfd_section_from_r_symndx
996 (abfd, &elf_m68k_hash_table (info)->sym_sec,
997 sec, r_symndx));
998 if (s == NULL)
999 return FALSE;
1001 vpp = &elf_section_data (s)->local_dynrel;
1002 head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
1005 for (p = *head; p != NULL; p = p->next)
1006 if (p->section == sreloc)
1007 break;
1009 if (p == NULL)
1011 p = ((struct elf_m68k_pcrel_relocs_copied *)
1012 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
1013 if (p == NULL)
1014 return FALSE;
1015 p->next = *head;
1016 *head = p;
1017 p->section = sreloc;
1018 p->count = 0;
1021 ++p->count;
1025 break;
1027 /* This relocation describes the C++ object vtable hierarchy.
1028 Reconstruct it for later use during GC. */
1029 case R_68K_GNU_VTINHERIT:
1030 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1031 return FALSE;
1032 break;
1034 /* This relocation describes which C++ vtable entries are actually
1035 used. Record for later use during GC. */
1036 case R_68K_GNU_VTENTRY:
1037 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1038 return FALSE;
1039 break;
1041 default:
1042 break;
1046 return TRUE;
1049 /* Return the section that should be marked against GC for a given
1050 relocation. */
1052 static asection *
1053 elf_m68k_gc_mark_hook (asection *sec,
1054 struct bfd_link_info *info,
1055 Elf_Internal_Rela *rel,
1056 struct elf_link_hash_entry *h,
1057 Elf_Internal_Sym *sym)
1059 if (h != NULL)
1060 switch (ELF32_R_TYPE (rel->r_info))
1062 case R_68K_GNU_VTINHERIT:
1063 case R_68K_GNU_VTENTRY:
1064 return NULL;
1067 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1070 /* Update the got entry reference counts for the section being removed. */
1072 static bfd_boolean
1073 elf_m68k_gc_sweep_hook (bfd *abfd,
1074 struct bfd_link_info *info,
1075 asection *sec,
1076 const Elf_Internal_Rela *relocs)
1078 Elf_Internal_Shdr *symtab_hdr;
1079 struct elf_link_hash_entry **sym_hashes;
1080 bfd_signed_vma *local_got_refcounts;
1081 const Elf_Internal_Rela *rel, *relend;
1082 bfd *dynobj;
1083 asection *sgot;
1084 asection *srelgot;
1086 dynobj = elf_hash_table (info)->dynobj;
1087 if (dynobj == NULL)
1088 return TRUE;
1090 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1091 sym_hashes = elf_sym_hashes (abfd);
1092 local_got_refcounts = elf_local_got_refcounts (abfd);
1094 sgot = bfd_get_section_by_name (dynobj, ".got");
1095 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1097 relend = relocs + sec->reloc_count;
1098 for (rel = relocs; rel < relend; rel++)
1100 unsigned long r_symndx;
1101 struct elf_link_hash_entry *h = NULL;
1103 r_symndx = ELF32_R_SYM (rel->r_info);
1104 if (r_symndx >= symtab_hdr->sh_info)
1106 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1107 while (h->root.type == bfd_link_hash_indirect
1108 || h->root.type == bfd_link_hash_warning)
1109 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1112 switch (ELF32_R_TYPE (rel->r_info))
1114 case R_68K_GOT8:
1115 case R_68K_GOT16:
1116 case R_68K_GOT32:
1117 case R_68K_GOT8O:
1118 case R_68K_GOT16O:
1119 case R_68K_GOT32O:
1120 if (h != NULL)
1122 if (h->got.refcount > 0)
1124 --h->got.refcount;
1125 if (h->got.refcount == 0)
1127 /* We don't need the .got entry any more. */
1128 sgot->size -= 4;
1129 srelgot->size -= sizeof (Elf32_External_Rela);
1133 else if (local_got_refcounts != NULL)
1135 if (local_got_refcounts[r_symndx] > 0)
1137 --local_got_refcounts[r_symndx];
1138 if (local_got_refcounts[r_symndx] == 0)
1140 /* We don't need the .got entry any more. */
1141 sgot->size -= 4;
1142 if (info->shared)
1143 srelgot->size -= sizeof (Elf32_External_Rela);
1147 break;
1149 case R_68K_PLT8:
1150 case R_68K_PLT16:
1151 case R_68K_PLT32:
1152 case R_68K_PLT8O:
1153 case R_68K_PLT16O:
1154 case R_68K_PLT32O:
1155 case R_68K_PC8:
1156 case R_68K_PC16:
1157 case R_68K_PC32:
1158 case R_68K_8:
1159 case R_68K_16:
1160 case R_68K_32:
1161 if (h != NULL)
1163 if (h->plt.refcount > 0)
1164 --h->plt.refcount;
1166 break;
1168 default:
1169 break;
1173 return TRUE;
1176 /* Return the type of PLT associated with OUTPUT_BFD. */
1178 static const struct elf_m68k_plt_info *
1179 elf_m68k_get_plt_info (bfd *output_bfd)
1181 unsigned int features;
1183 features = bfd_m68k_mach_to_features (bfd_get_mach (output_bfd));
1184 if (features & cpu32)
1185 return &elf_cpu32_plt_info;
1186 if (features & mcfisa_b)
1187 return &elf_isab_plt_info;
1188 if (features & mcfisa_c)
1189 return &elf_isac_plt_info;
1190 return &elf_m68k_plt_info;
1193 /* This function is called after all the input files have been read,
1194 and the input sections have been assigned to output sections.
1195 It's a convenient place to determine the PLT style. */
1197 static bfd_boolean
1198 elf_m68k_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
1200 elf_m68k_hash_table (info)->plt_info = elf_m68k_get_plt_info (output_bfd);
1201 return TRUE;
1204 /* Adjust a symbol defined by a dynamic object and referenced by a
1205 regular object. The current definition is in some section of the
1206 dynamic object, but we're not including those sections. We have to
1207 change the definition to something the rest of the link can
1208 understand. */
1210 static bfd_boolean
1211 elf_m68k_adjust_dynamic_symbol (info, h)
1212 struct bfd_link_info *info;
1213 struct elf_link_hash_entry *h;
1215 struct elf_m68k_link_hash_table *htab;
1216 bfd *dynobj;
1217 asection *s;
1219 htab = elf_m68k_hash_table (info);
1220 dynobj = elf_hash_table (info)->dynobj;
1222 /* Make sure we know what is going on here. */
1223 BFD_ASSERT (dynobj != NULL
1224 && (h->needs_plt
1225 || h->u.weakdef != NULL
1226 || (h->def_dynamic
1227 && h->ref_regular
1228 && !h->def_regular)));
1230 /* If this is a function, put it in the procedure linkage table. We
1231 will fill in the contents of the procedure linkage table later,
1232 when we know the address of the .got section. */
1233 if (h->type == STT_FUNC
1234 || h->needs_plt)
1236 if ((h->plt.refcount <= 0
1237 || SYMBOL_CALLS_LOCAL (info, h)
1238 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1239 && h->root.type == bfd_link_hash_undefweak))
1240 /* We must always create the plt entry if it was referenced
1241 by a PLTxxO relocation. In this case we already recorded
1242 it as a dynamic symbol. */
1243 && h->dynindx == -1)
1245 /* This case can occur if we saw a PLTxx reloc in an input
1246 file, but the symbol was never referred to by a dynamic
1247 object, or if all references were garbage collected. In
1248 such a case, we don't actually need to build a procedure
1249 linkage table, and we can just do a PCxx reloc instead. */
1250 h->plt.offset = (bfd_vma) -1;
1251 h->needs_plt = 0;
1252 return TRUE;
1255 /* Make sure this symbol is output as a dynamic symbol. */
1256 if (h->dynindx == -1
1257 && !h->forced_local)
1259 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1260 return FALSE;
1263 s = bfd_get_section_by_name (dynobj, ".plt");
1264 BFD_ASSERT (s != NULL);
1266 /* If this is the first .plt entry, make room for the special
1267 first entry. */
1268 if (s->size == 0)
1269 s->size = htab->plt_info->size;
1271 /* If this symbol is not defined in a regular file, and we are
1272 not generating a shared library, then set the symbol to this
1273 location in the .plt. This is required to make function
1274 pointers compare as equal between the normal executable and
1275 the shared library. */
1276 if (!info->shared
1277 && !h->def_regular)
1279 h->root.u.def.section = s;
1280 h->root.u.def.value = s->size;
1283 h->plt.offset = s->size;
1285 /* Make room for this entry. */
1286 s->size += htab->plt_info->size;
1288 /* We also need to make an entry in the .got.plt section, which
1289 will be placed in the .got section by the linker script. */
1290 s = bfd_get_section_by_name (dynobj, ".got.plt");
1291 BFD_ASSERT (s != NULL);
1292 s->size += 4;
1294 /* We also need to make an entry in the .rela.plt section. */
1295 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1296 BFD_ASSERT (s != NULL);
1297 s->size += sizeof (Elf32_External_Rela);
1299 return TRUE;
1302 /* Reinitialize the plt offset now that it is not used as a reference
1303 count any more. */
1304 h->plt.offset = (bfd_vma) -1;
1306 /* If this is a weak symbol, and there is a real definition, the
1307 processor independent code will have arranged for us to see the
1308 real definition first, and we can just use the same value. */
1309 if (h->u.weakdef != NULL)
1311 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1312 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1313 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1314 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1315 return TRUE;
1318 /* This is a reference to a symbol defined by a dynamic object which
1319 is not a function. */
1321 /* If we are creating a shared library, we must presume that the
1322 only references to the symbol are via the global offset table.
1323 For such cases we need not do anything here; the relocations will
1324 be handled correctly by relocate_section. */
1325 if (info->shared)
1326 return TRUE;
1328 if (h->size == 0)
1330 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1331 h->root.root.string);
1332 return TRUE;
1335 /* We must allocate the symbol in our .dynbss section, which will
1336 become part of the .bss section of the executable. There will be
1337 an entry for this symbol in the .dynsym section. The dynamic
1338 object will contain position independent code, so all references
1339 from the dynamic object to this symbol will go through the global
1340 offset table. The dynamic linker will use the .dynsym entry to
1341 determine the address it must put in the global offset table, so
1342 both the dynamic object and the regular object will refer to the
1343 same memory location for the variable. */
1345 s = bfd_get_section_by_name (dynobj, ".dynbss");
1346 BFD_ASSERT (s != NULL);
1348 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1349 copy the initial value out of the dynamic object and into the
1350 runtime process image. We need to remember the offset into the
1351 .rela.bss section we are going to use. */
1352 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1354 asection *srel;
1356 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1357 BFD_ASSERT (srel != NULL);
1358 srel->size += sizeof (Elf32_External_Rela);
1359 h->needs_copy = 1;
1362 return _bfd_elf_adjust_dynamic_copy (h, s);
1365 /* Set the sizes of the dynamic sections. */
1367 static bfd_boolean
1368 elf_m68k_size_dynamic_sections (output_bfd, info)
1369 bfd *output_bfd ATTRIBUTE_UNUSED;
1370 struct bfd_link_info *info;
1372 bfd *dynobj;
1373 asection *s;
1374 bfd_boolean plt;
1375 bfd_boolean relocs;
1377 dynobj = elf_hash_table (info)->dynobj;
1378 BFD_ASSERT (dynobj != NULL);
1380 if (elf_hash_table (info)->dynamic_sections_created)
1382 /* Set the contents of the .interp section to the interpreter. */
1383 if (info->executable)
1385 s = bfd_get_section_by_name (dynobj, ".interp");
1386 BFD_ASSERT (s != NULL);
1387 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1388 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1391 else
1393 /* We may have created entries in the .rela.got section.
1394 However, if we are not creating the dynamic sections, we will
1395 not actually use these entries. Reset the size of .rela.got,
1396 which will cause it to get stripped from the output file
1397 below. */
1398 s = bfd_get_section_by_name (dynobj, ".rela.got");
1399 if (s != NULL)
1400 s->size = 0;
1403 /* If this is a -Bsymbolic shared link, then we need to discard all
1404 PC relative relocs against symbols defined in a regular object.
1405 For the normal shared case we discard the PC relative relocs
1406 against symbols that have become local due to visibility changes.
1407 We allocated space for them in the check_relocs routine, but we
1408 will not fill them in in the relocate_section routine. */
1409 if (info->shared)
1410 elf_link_hash_traverse (elf_hash_table (info),
1411 elf_m68k_discard_copies,
1412 (PTR) info);
1414 /* The check_relocs and adjust_dynamic_symbol entry points have
1415 determined the sizes of the various dynamic sections. Allocate
1416 memory for them. */
1417 plt = FALSE;
1418 relocs = FALSE;
1419 for (s = dynobj->sections; s != NULL; s = s->next)
1421 const char *name;
1423 if ((s->flags & SEC_LINKER_CREATED) == 0)
1424 continue;
1426 /* It's OK to base decisions on the section name, because none
1427 of the dynobj section names depend upon the input files. */
1428 name = bfd_get_section_name (dynobj, s);
1430 if (strcmp (name, ".plt") == 0)
1432 /* Remember whether there is a PLT. */
1433 plt = s->size != 0;
1435 else if (CONST_STRNEQ (name, ".rela"))
1437 if (s->size != 0)
1439 relocs = TRUE;
1441 /* We use the reloc_count field as a counter if we need
1442 to copy relocs into the output file. */
1443 s->reloc_count = 0;
1446 else if (! CONST_STRNEQ (name, ".got")
1447 && strcmp (name, ".dynbss") != 0)
1449 /* It's not one of our sections, so don't allocate space. */
1450 continue;
1453 if (s->size == 0)
1455 /* If we don't need this section, strip it from the
1456 output file. This is mostly to handle .rela.bss and
1457 .rela.plt. We must create both sections in
1458 create_dynamic_sections, because they must be created
1459 before the linker maps input sections to output
1460 sections. The linker does that before
1461 adjust_dynamic_symbol is called, and it is that
1462 function which decides whether anything needs to go
1463 into these sections. */
1464 s->flags |= SEC_EXCLUDE;
1465 continue;
1468 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1469 continue;
1471 /* Allocate memory for the section contents. */
1472 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1473 Unused entries should be reclaimed before the section's contents
1474 are written out, but at the moment this does not happen. Thus in
1475 order to prevent writing out garbage, we initialise the section's
1476 contents to zero. */
1477 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1478 if (s->contents == NULL)
1479 return FALSE;
1482 if (elf_hash_table (info)->dynamic_sections_created)
1484 /* Add some entries to the .dynamic section. We fill in the
1485 values later, in elf_m68k_finish_dynamic_sections, but we
1486 must add the entries now so that we get the correct size for
1487 the .dynamic section. The DT_DEBUG entry is filled in by the
1488 dynamic linker and used by the debugger. */
1489 #define add_dynamic_entry(TAG, VAL) \
1490 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1492 if (!info->shared)
1494 if (!add_dynamic_entry (DT_DEBUG, 0))
1495 return FALSE;
1498 if (plt)
1500 if (!add_dynamic_entry (DT_PLTGOT, 0)
1501 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1502 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1503 || !add_dynamic_entry (DT_JMPREL, 0))
1504 return FALSE;
1507 if (relocs)
1509 if (!add_dynamic_entry (DT_RELA, 0)
1510 || !add_dynamic_entry (DT_RELASZ, 0)
1511 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1512 return FALSE;
1515 if ((info->flags & DF_TEXTREL) != 0)
1517 if (!add_dynamic_entry (DT_TEXTREL, 0))
1518 return FALSE;
1521 #undef add_dynamic_entry
1523 return TRUE;
1526 /* This function is called via elf_link_hash_traverse if we are
1527 creating a shared object. In the -Bsymbolic case it discards the
1528 space allocated to copy PC relative relocs against symbols which
1529 are defined in regular objects. For the normal shared case, it
1530 discards space for pc-relative relocs that have become local due to
1531 symbol visibility changes. We allocated space for them in the
1532 check_relocs routine, but we won't fill them in in the
1533 relocate_section routine.
1535 We also check whether any of the remaining relocations apply
1536 against a readonly section, and set the DF_TEXTREL flag in this
1537 case. */
1539 static bfd_boolean
1540 elf_m68k_discard_copies (h, inf)
1541 struct elf_link_hash_entry *h;
1542 PTR inf;
1544 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1545 struct elf_m68k_pcrel_relocs_copied *s;
1547 if (h->root.type == bfd_link_hash_warning)
1548 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1550 if (!h->def_regular
1551 || (!info->symbolic
1552 && !h->forced_local))
1554 if ((info->flags & DF_TEXTREL) == 0)
1556 /* Look for relocations against read-only sections. */
1557 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1558 s != NULL;
1559 s = s->next)
1560 if ((s->section->flags & SEC_READONLY) != 0)
1562 info->flags |= DF_TEXTREL;
1563 break;
1567 return TRUE;
1570 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1571 s != NULL;
1572 s = s->next)
1573 s->section->size -= s->count * sizeof (Elf32_External_Rela);
1575 return TRUE;
1578 /* Relocate an M68K ELF section. */
1580 static bfd_boolean
1581 elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1582 contents, relocs, local_syms, local_sections)
1583 bfd *output_bfd;
1584 struct bfd_link_info *info;
1585 bfd *input_bfd;
1586 asection *input_section;
1587 bfd_byte *contents;
1588 Elf_Internal_Rela *relocs;
1589 Elf_Internal_Sym *local_syms;
1590 asection **local_sections;
1592 bfd *dynobj;
1593 Elf_Internal_Shdr *symtab_hdr;
1594 struct elf_link_hash_entry **sym_hashes;
1595 bfd_vma *local_got_offsets;
1596 asection *sgot;
1597 asection *splt;
1598 asection *sreloc;
1599 Elf_Internal_Rela *rel;
1600 Elf_Internal_Rela *relend;
1602 dynobj = elf_hash_table (info)->dynobj;
1603 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1604 sym_hashes = elf_sym_hashes (input_bfd);
1605 local_got_offsets = elf_local_got_offsets (input_bfd);
1607 sgot = NULL;
1608 splt = NULL;
1609 sreloc = NULL;
1611 rel = relocs;
1612 relend = relocs + input_section->reloc_count;
1613 for (; rel < relend; rel++)
1615 int r_type;
1616 reloc_howto_type *howto;
1617 unsigned long r_symndx;
1618 struct elf_link_hash_entry *h;
1619 Elf_Internal_Sym *sym;
1620 asection *sec;
1621 bfd_vma relocation;
1622 bfd_boolean unresolved_reloc;
1623 bfd_reloc_status_type r;
1625 r_type = ELF32_R_TYPE (rel->r_info);
1626 if (r_type < 0 || r_type >= (int) R_68K_max)
1628 bfd_set_error (bfd_error_bad_value);
1629 return FALSE;
1631 howto = howto_table + r_type;
1633 r_symndx = ELF32_R_SYM (rel->r_info);
1635 h = NULL;
1636 sym = NULL;
1637 sec = NULL;
1638 unresolved_reloc = FALSE;
1640 if (r_symndx < symtab_hdr->sh_info)
1642 sym = local_syms + r_symndx;
1643 sec = local_sections[r_symndx];
1644 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1646 else
1648 bfd_boolean warned;
1650 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1651 r_symndx, symtab_hdr, sym_hashes,
1652 h, sec, relocation,
1653 unresolved_reloc, warned);
1656 if (sec != NULL && elf_discarded_section (sec))
1658 /* For relocs against symbols from removed linkonce sections,
1659 or sections discarded by a linker script, we just want the
1660 section contents zeroed. Avoid any special processing. */
1661 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1662 rel->r_info = 0;
1663 rel->r_addend = 0;
1664 continue;
1667 if (info->relocatable)
1668 continue;
1670 switch (r_type)
1672 case R_68K_GOT8:
1673 case R_68K_GOT16:
1674 case R_68K_GOT32:
1675 /* Relocation is to the address of the entry for this symbol
1676 in the global offset table. */
1677 if (h != NULL
1678 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1679 break;
1680 /* Fall through. */
1681 case R_68K_GOT8O:
1682 case R_68K_GOT16O:
1683 case R_68K_GOT32O:
1684 /* Relocation is the offset of the entry for this symbol in
1685 the global offset table. */
1688 bfd_vma off;
1690 if (sgot == NULL)
1692 sgot = bfd_get_section_by_name (dynobj, ".got");
1693 BFD_ASSERT (sgot != NULL);
1696 if (h != NULL)
1698 bfd_boolean dyn;
1700 off = h->got.offset;
1701 BFD_ASSERT (off != (bfd_vma) -1);
1703 dyn = elf_hash_table (info)->dynamic_sections_created;
1704 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1705 || (info->shared
1706 && (info->symbolic
1707 || h->dynindx == -1
1708 || h->forced_local)
1709 && h->def_regular))
1711 /* This is actually a static link, or it is a
1712 -Bsymbolic link and the symbol is defined
1713 locally, or the symbol was forced to be local
1714 because of a version file.. We must initialize
1715 this entry in the global offset table. Since
1716 the offset must always be a multiple of 4, we
1717 use the least significant bit to record whether
1718 we have initialized it already.
1720 When doing a dynamic link, we create a .rela.got
1721 relocation entry to initialize the value. This
1722 is done in the finish_dynamic_symbol routine. */
1723 if ((off & 1) != 0)
1724 off &= ~1;
1725 else
1727 bfd_put_32 (output_bfd, relocation,
1728 sgot->contents + off);
1729 h->got.offset |= 1;
1732 else
1733 unresolved_reloc = FALSE;
1735 else
1737 BFD_ASSERT (local_got_offsets != NULL
1738 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1740 off = local_got_offsets[r_symndx];
1742 /* The offset must always be a multiple of 4. We use
1743 the least significant bit to record whether we have
1744 already generated the necessary reloc. */
1745 if ((off & 1) != 0)
1746 off &= ~1;
1747 else
1749 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1751 if (info->shared)
1753 asection *s;
1754 Elf_Internal_Rela outrel;
1755 bfd_byte *loc;
1757 s = bfd_get_section_by_name (dynobj, ".rela.got");
1758 BFD_ASSERT (s != NULL);
1760 outrel.r_offset = (sgot->output_section->vma
1761 + sgot->output_offset
1762 + off);
1763 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1764 outrel.r_addend = relocation;
1765 loc = s->contents;
1766 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1767 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1770 local_got_offsets[r_symndx] |= 1;
1774 relocation = sgot->output_offset + off;
1775 if (r_type == R_68K_GOT8O
1776 || r_type == R_68K_GOT16O
1777 || r_type == R_68K_GOT32O)
1779 /* This relocation does not use the addend. */
1780 rel->r_addend = 0;
1782 else
1783 relocation += sgot->output_section->vma;
1785 break;
1787 case R_68K_PLT8:
1788 case R_68K_PLT16:
1789 case R_68K_PLT32:
1790 /* Relocation is to the entry for this symbol in the
1791 procedure linkage table. */
1793 /* Resolve a PLTxx reloc against a local symbol directly,
1794 without using the procedure linkage table. */
1795 if (h == NULL)
1796 break;
1798 if (h->plt.offset == (bfd_vma) -1
1799 || !elf_hash_table (info)->dynamic_sections_created)
1801 /* We didn't make a PLT entry for this symbol. This
1802 happens when statically linking PIC code, or when
1803 using -Bsymbolic. */
1804 break;
1807 if (splt == NULL)
1809 splt = bfd_get_section_by_name (dynobj, ".plt");
1810 BFD_ASSERT (splt != NULL);
1813 relocation = (splt->output_section->vma
1814 + splt->output_offset
1815 + h->plt.offset);
1816 unresolved_reloc = FALSE;
1817 break;
1819 case R_68K_PLT8O:
1820 case R_68K_PLT16O:
1821 case R_68K_PLT32O:
1822 /* Relocation is the offset of the entry for this symbol in
1823 the procedure linkage table. */
1824 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1826 if (splt == NULL)
1828 splt = bfd_get_section_by_name (dynobj, ".plt");
1829 BFD_ASSERT (splt != NULL);
1832 relocation = h->plt.offset;
1833 unresolved_reloc = FALSE;
1835 /* This relocation does not use the addend. */
1836 rel->r_addend = 0;
1838 break;
1840 case R_68K_PC8:
1841 case R_68K_PC16:
1842 case R_68K_PC32:
1843 if (h == NULL
1844 || (info->shared
1845 && h->forced_local))
1846 break;
1847 /* Fall through. */
1848 case R_68K_8:
1849 case R_68K_16:
1850 case R_68K_32:
1851 if (info->shared
1852 && r_symndx != 0
1853 && (input_section->flags & SEC_ALLOC) != 0
1854 && (h == NULL
1855 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1856 || h->root.type != bfd_link_hash_undefweak)
1857 && ((r_type != R_68K_PC8
1858 && r_type != R_68K_PC16
1859 && r_type != R_68K_PC32)
1860 || (h != NULL
1861 && h->dynindx != -1
1862 && (!info->symbolic
1863 || !h->def_regular))))
1865 Elf_Internal_Rela outrel;
1866 bfd_byte *loc;
1867 bfd_boolean skip, relocate;
1869 /* When generating a shared object, these relocations
1870 are copied into the output file to be resolved at run
1871 time. */
1873 skip = FALSE;
1874 relocate = FALSE;
1876 outrel.r_offset =
1877 _bfd_elf_section_offset (output_bfd, info, input_section,
1878 rel->r_offset);
1879 if (outrel.r_offset == (bfd_vma) -1)
1880 skip = TRUE;
1881 else if (outrel.r_offset == (bfd_vma) -2)
1882 skip = TRUE, relocate = TRUE;
1883 outrel.r_offset += (input_section->output_section->vma
1884 + input_section->output_offset);
1886 if (skip)
1887 memset (&outrel, 0, sizeof outrel);
1888 else if (h != NULL
1889 && h->dynindx != -1
1890 && (r_type == R_68K_PC8
1891 || r_type == R_68K_PC16
1892 || r_type == R_68K_PC32
1893 || !info->shared
1894 || !info->symbolic
1895 || !h->def_regular))
1897 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1898 outrel.r_addend = rel->r_addend;
1900 else
1902 /* This symbol is local, or marked to become local. */
1903 outrel.r_addend = relocation + rel->r_addend;
1905 if (r_type == R_68K_32)
1907 relocate = TRUE;
1908 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1910 else
1912 long indx;
1914 if (bfd_is_abs_section (sec))
1915 indx = 0;
1916 else if (sec == NULL || sec->owner == NULL)
1918 bfd_set_error (bfd_error_bad_value);
1919 return FALSE;
1921 else
1923 asection *osec;
1925 /* We are turning this relocation into one
1926 against a section symbol. It would be
1927 proper to subtract the symbol's value,
1928 osec->vma, from the emitted reloc addend,
1929 but ld.so expects buggy relocs. */
1930 osec = sec->output_section;
1931 indx = elf_section_data (osec)->dynindx;
1932 if (indx == 0)
1934 struct elf_link_hash_table *htab;
1935 htab = elf_hash_table (info);
1936 osec = htab->text_index_section;
1937 indx = elf_section_data (osec)->dynindx;
1939 BFD_ASSERT (indx != 0);
1942 outrel.r_info = ELF32_R_INFO (indx, r_type);
1946 sreloc = elf_section_data (input_section)->sreloc;
1947 if (sreloc == NULL)
1948 abort ();
1950 loc = sreloc->contents;
1951 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1952 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1954 /* This reloc will be computed at runtime, so there's no
1955 need to do anything now, except for R_68K_32
1956 relocations that have been turned into
1957 R_68K_RELATIVE. */
1958 if (!relocate)
1959 continue;
1962 break;
1964 case R_68K_GNU_VTINHERIT:
1965 case R_68K_GNU_VTENTRY:
1966 /* These are no-ops in the end. */
1967 continue;
1969 default:
1970 break;
1973 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1974 because such sections are not SEC_ALLOC and thus ld.so will
1975 not process them. */
1976 if (unresolved_reloc
1977 && !((input_section->flags & SEC_DEBUGGING) != 0
1978 && h->def_dynamic))
1980 (*_bfd_error_handler)
1981 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1982 input_bfd,
1983 input_section,
1984 (long) rel->r_offset,
1985 howto->name,
1986 h->root.root.string);
1987 return FALSE;
1990 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1991 contents, rel->r_offset,
1992 relocation, rel->r_addend);
1994 if (r != bfd_reloc_ok)
1996 const char *name;
1998 if (h != NULL)
1999 name = h->root.root.string;
2000 else
2002 name = bfd_elf_string_from_elf_section (input_bfd,
2003 symtab_hdr->sh_link,
2004 sym->st_name);
2005 if (name == NULL)
2006 return FALSE;
2007 if (*name == '\0')
2008 name = bfd_section_name (input_bfd, sec);
2011 if (r == bfd_reloc_overflow)
2013 if (!(info->callbacks->reloc_overflow
2014 (info, (h ? &h->root : NULL), name, howto->name,
2015 (bfd_vma) 0, input_bfd, input_section,
2016 rel->r_offset)))
2017 return FALSE;
2019 else
2021 (*_bfd_error_handler)
2022 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2023 input_bfd, input_section,
2024 (long) rel->r_offset, name, (int) r);
2025 return FALSE;
2030 return TRUE;
2033 /* Install an M_68K_PC32 relocation against VALUE at offset OFFSET
2034 into section SEC. */
2036 static void
2037 elf_m68k_install_pc32 (asection *sec, bfd_vma offset, bfd_vma value)
2039 /* Make VALUE PC-relative. */
2040 value -= sec->output_section->vma + offset;
2042 /* Apply any in-place addend. */
2043 value += bfd_get_32 (sec->owner, sec->contents + offset);
2045 bfd_put_32 (sec->owner, value, sec->contents + offset);
2048 /* Finish up dynamic symbol handling. We set the contents of various
2049 dynamic sections here. */
2051 static bfd_boolean
2052 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
2053 bfd *output_bfd;
2054 struct bfd_link_info *info;
2055 struct elf_link_hash_entry *h;
2056 Elf_Internal_Sym *sym;
2058 bfd *dynobj;
2060 dynobj = elf_hash_table (info)->dynobj;
2062 if (h->plt.offset != (bfd_vma) -1)
2064 const struct elf_m68k_plt_info *plt_info;
2065 asection *splt;
2066 asection *sgot;
2067 asection *srela;
2068 bfd_vma plt_index;
2069 bfd_vma got_offset;
2070 Elf_Internal_Rela rela;
2071 bfd_byte *loc;
2073 /* This symbol has an entry in the procedure linkage table. Set
2074 it up. */
2076 BFD_ASSERT (h->dynindx != -1);
2078 plt_info = elf_m68k_hash_table (info)->plt_info;
2079 splt = bfd_get_section_by_name (dynobj, ".plt");
2080 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2081 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2082 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
2084 /* Get the index in the procedure linkage table which
2085 corresponds to this symbol. This is the index of this symbol
2086 in all the symbols for which we are making plt entries. The
2087 first entry in the procedure linkage table is reserved. */
2088 plt_index = (h->plt.offset / plt_info->size) - 1;
2090 /* Get the offset into the .got table of the entry that
2091 corresponds to this function. Each .got entry is 4 bytes.
2092 The first three are reserved. */
2093 got_offset = (plt_index + 3) * 4;
2095 memcpy (splt->contents + h->plt.offset,
2096 plt_info->symbol_entry,
2097 plt_info->size);
2099 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.got,
2100 (sgot->output_section->vma
2101 + sgot->output_offset
2102 + got_offset));
2104 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
2105 splt->contents
2106 + h->plt.offset
2107 + plt_info->symbol_resolve_entry + 2);
2109 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.plt,
2110 splt->output_section->vma);
2112 /* Fill in the entry in the global offset table. */
2113 bfd_put_32 (output_bfd,
2114 (splt->output_section->vma
2115 + splt->output_offset
2116 + h->plt.offset
2117 + plt_info->symbol_resolve_entry),
2118 sgot->contents + got_offset);
2120 /* Fill in the entry in the .rela.plt section. */
2121 rela.r_offset = (sgot->output_section->vma
2122 + sgot->output_offset
2123 + got_offset);
2124 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
2125 rela.r_addend = 0;
2126 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
2127 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2129 if (!h->def_regular)
2131 /* Mark the symbol as undefined, rather than as defined in
2132 the .plt section. Leave the value alone. */
2133 sym->st_shndx = SHN_UNDEF;
2137 if (h->got.offset != (bfd_vma) -1)
2139 asection *sgot;
2140 asection *srela;
2141 Elf_Internal_Rela rela;
2142 bfd_byte *loc;
2144 /* This symbol has an entry in the global offset table. Set it
2145 up. */
2147 sgot = bfd_get_section_by_name (dynobj, ".got");
2148 srela = bfd_get_section_by_name (dynobj, ".rela.got");
2149 BFD_ASSERT (sgot != NULL && srela != NULL);
2151 rela.r_offset = (sgot->output_section->vma
2152 + sgot->output_offset
2153 + (h->got.offset &~ (bfd_vma) 1));
2155 /* If this is a -Bsymbolic link, and the symbol is defined
2156 locally, we just want to emit a RELATIVE reloc. Likewise if
2157 the symbol was forced to be local because of a version file.
2158 The entry in the global offset table will already have been
2159 initialized in the relocate_section function. */
2160 if (info->shared
2161 && (info->symbolic
2162 || h->dynindx == -1
2163 || h->forced_local)
2164 && h->def_regular)
2166 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
2167 rela.r_addend = bfd_get_signed_32 (output_bfd,
2168 (sgot->contents
2169 + (h->got.offset &~ (bfd_vma) 1)));
2171 else
2173 bfd_put_32 (output_bfd, (bfd_vma) 0,
2174 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
2175 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
2176 rela.r_addend = 0;
2179 loc = srela->contents;
2180 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2181 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2184 if (h->needs_copy)
2186 asection *s;
2187 Elf_Internal_Rela rela;
2188 bfd_byte *loc;
2190 /* This symbol needs a copy reloc. Set it up. */
2192 BFD_ASSERT (h->dynindx != -1
2193 && (h->root.type == bfd_link_hash_defined
2194 || h->root.type == bfd_link_hash_defweak));
2196 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2197 ".rela.bss");
2198 BFD_ASSERT (s != NULL);
2200 rela.r_offset = (h->root.u.def.value
2201 + h->root.u.def.section->output_section->vma
2202 + h->root.u.def.section->output_offset);
2203 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
2204 rela.r_addend = 0;
2205 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2206 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2209 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2210 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2211 || h == elf_hash_table (info)->hgot)
2212 sym->st_shndx = SHN_ABS;
2214 return TRUE;
2217 /* Finish up the dynamic sections. */
2219 static bfd_boolean
2220 elf_m68k_finish_dynamic_sections (output_bfd, info)
2221 bfd *output_bfd;
2222 struct bfd_link_info *info;
2224 bfd *dynobj;
2225 asection *sgot;
2226 asection *sdyn;
2228 dynobj = elf_hash_table (info)->dynobj;
2230 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2231 BFD_ASSERT (sgot != NULL);
2232 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2234 if (elf_hash_table (info)->dynamic_sections_created)
2236 asection *splt;
2237 Elf32_External_Dyn *dyncon, *dynconend;
2239 splt = bfd_get_section_by_name (dynobj, ".plt");
2240 BFD_ASSERT (splt != NULL && sdyn != NULL);
2242 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2243 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2244 for (; dyncon < dynconend; dyncon++)
2246 Elf_Internal_Dyn dyn;
2247 const char *name;
2248 asection *s;
2250 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2252 switch (dyn.d_tag)
2254 default:
2255 break;
2257 case DT_PLTGOT:
2258 name = ".got";
2259 goto get_vma;
2260 case DT_JMPREL:
2261 name = ".rela.plt";
2262 get_vma:
2263 s = bfd_get_section_by_name (output_bfd, name);
2264 BFD_ASSERT (s != NULL);
2265 dyn.d_un.d_ptr = s->vma;
2266 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2267 break;
2269 case DT_PLTRELSZ:
2270 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2271 BFD_ASSERT (s != NULL);
2272 dyn.d_un.d_val = s->size;
2273 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2274 break;
2276 case DT_RELASZ:
2277 /* The procedure linkage table relocs (DT_JMPREL) should
2278 not be included in the overall relocs (DT_RELA).
2279 Therefore, we override the DT_RELASZ entry here to
2280 make it not include the JMPREL relocs. Since the
2281 linker script arranges for .rela.plt to follow all
2282 other relocation sections, we don't have to worry
2283 about changing the DT_RELA entry. */
2284 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2285 if (s != NULL)
2286 dyn.d_un.d_val -= s->size;
2287 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2288 break;
2292 /* Fill in the first entry in the procedure linkage table. */
2293 if (splt->size > 0)
2295 const struct elf_m68k_plt_info *plt_info;
2297 plt_info = elf_m68k_hash_table (info)->plt_info;
2298 memcpy (splt->contents, plt_info->plt0_entry, plt_info->size);
2300 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got4,
2301 (sgot->output_section->vma
2302 + sgot->output_offset
2303 + 4));
2305 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got8,
2306 (sgot->output_section->vma
2307 + sgot->output_offset
2308 + 8));
2310 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2311 = plt_info->size;
2315 /* Fill in the first three entries in the global offset table. */
2316 if (sgot->size > 0)
2318 if (sdyn == NULL)
2319 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2320 else
2321 bfd_put_32 (output_bfd,
2322 sdyn->output_section->vma + sdyn->output_offset,
2323 sgot->contents);
2324 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2325 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2328 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2330 return TRUE;
2333 /* Given a .data section and a .emreloc in-memory section, store
2334 relocation information into the .emreloc section which can be
2335 used at runtime to relocate the section. This is called by the
2336 linker when the --embedded-relocs switch is used. This is called
2337 after the add_symbols entry point has been called for all the
2338 objects, and before the final_link entry point is called. */
2340 bfd_boolean
2341 bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2342 bfd *abfd;
2343 struct bfd_link_info *info;
2344 asection *datasec;
2345 asection *relsec;
2346 char **errmsg;
2348 Elf_Internal_Shdr *symtab_hdr;
2349 Elf_Internal_Sym *isymbuf = NULL;
2350 Elf_Internal_Rela *internal_relocs = NULL;
2351 Elf_Internal_Rela *irel, *irelend;
2352 bfd_byte *p;
2353 bfd_size_type amt;
2355 BFD_ASSERT (! info->relocatable);
2357 *errmsg = NULL;
2359 if (datasec->reloc_count == 0)
2360 return TRUE;
2362 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2364 /* Get a copy of the native relocations. */
2365 internal_relocs = (_bfd_elf_link_read_relocs
2366 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2367 info->keep_memory));
2368 if (internal_relocs == NULL)
2369 goto error_return;
2371 amt = (bfd_size_type) datasec->reloc_count * 12;
2372 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
2373 if (relsec->contents == NULL)
2374 goto error_return;
2376 p = relsec->contents;
2378 irelend = internal_relocs + datasec->reloc_count;
2379 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2381 asection *targetsec;
2383 /* We are going to write a four byte longword into the runtime
2384 reloc section. The longword will be the address in the data
2385 section which must be relocated. It is followed by the name
2386 of the target section NUL-padded or truncated to 8
2387 characters. */
2389 /* We can only relocate absolute longword relocs at run time. */
2390 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2392 *errmsg = _("unsupported reloc type");
2393 bfd_set_error (bfd_error_bad_value);
2394 goto error_return;
2397 /* Get the target section referred to by the reloc. */
2398 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2400 /* A local symbol. */
2401 Elf_Internal_Sym *isym;
2403 /* Read this BFD's local symbols if we haven't done so already. */
2404 if (isymbuf == NULL)
2406 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2407 if (isymbuf == NULL)
2408 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2409 symtab_hdr->sh_info, 0,
2410 NULL, NULL, NULL);
2411 if (isymbuf == NULL)
2412 goto error_return;
2415 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2416 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2418 else
2420 unsigned long indx;
2421 struct elf_link_hash_entry *h;
2423 /* An external symbol. */
2424 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2425 h = elf_sym_hashes (abfd)[indx];
2426 BFD_ASSERT (h != NULL);
2427 if (h->root.type == bfd_link_hash_defined
2428 || h->root.type == bfd_link_hash_defweak)
2429 targetsec = h->root.u.def.section;
2430 else
2431 targetsec = NULL;
2434 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2435 memset (p + 4, 0, 8);
2436 if (targetsec != NULL)
2437 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
2440 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2441 free (isymbuf);
2442 if (internal_relocs != NULL
2443 && elf_section_data (datasec)->relocs != internal_relocs)
2444 free (internal_relocs);
2445 return TRUE;
2447 error_return:
2448 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2449 free (isymbuf);
2450 if (internal_relocs != NULL
2451 && elf_section_data (datasec)->relocs != internal_relocs)
2452 free (internal_relocs);
2453 return FALSE;
2456 static enum elf_reloc_type_class
2457 elf32_m68k_reloc_type_class (rela)
2458 const Elf_Internal_Rela *rela;
2460 switch ((int) ELF32_R_TYPE (rela->r_info))
2462 case R_68K_RELATIVE:
2463 return reloc_class_relative;
2464 case R_68K_JMP_SLOT:
2465 return reloc_class_plt;
2466 case R_68K_COPY:
2467 return reloc_class_copy;
2468 default:
2469 return reloc_class_normal;
2473 /* Return address for Ith PLT stub in section PLT, for relocation REL
2474 or (bfd_vma) -1 if it should not be included. */
2476 static bfd_vma
2477 elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
2478 const arelent *rel ATTRIBUTE_UNUSED)
2480 return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
2483 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
2484 #define TARGET_BIG_NAME "elf32-m68k"
2485 #define ELF_MACHINE_CODE EM_68K
2486 #define ELF_MAXPAGESIZE 0x2000
2487 #define elf_backend_create_dynamic_sections \
2488 _bfd_elf_create_dynamic_sections
2489 #define bfd_elf32_bfd_link_hash_table_create \
2490 elf_m68k_link_hash_table_create
2491 #define bfd_elf32_bfd_final_link bfd_elf_gc_common_final_link
2493 #define elf_backend_check_relocs elf_m68k_check_relocs
2494 #define elf_backend_always_size_sections \
2495 elf_m68k_always_size_sections
2496 #define elf_backend_adjust_dynamic_symbol \
2497 elf_m68k_adjust_dynamic_symbol
2498 #define elf_backend_size_dynamic_sections \
2499 elf_m68k_size_dynamic_sections
2500 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
2501 #define elf_backend_relocate_section elf_m68k_relocate_section
2502 #define elf_backend_finish_dynamic_symbol \
2503 elf_m68k_finish_dynamic_symbol
2504 #define elf_backend_finish_dynamic_sections \
2505 elf_m68k_finish_dynamic_sections
2506 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
2507 #define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
2508 #define bfd_elf32_bfd_merge_private_bfd_data \
2509 elf32_m68k_merge_private_bfd_data
2510 #define bfd_elf32_bfd_set_private_flags \
2511 elf32_m68k_set_private_flags
2512 #define bfd_elf32_bfd_print_private_bfd_data \
2513 elf32_m68k_print_private_bfd_data
2514 #define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
2515 #define elf_backend_plt_sym_val elf_m68k_plt_sym_val
2516 #define elf_backend_object_p elf32_m68k_object_p
2518 #define elf_backend_can_gc_sections 1
2519 #define elf_backend_can_refcount 1
2520 #define elf_backend_want_got_plt 1
2521 #define elf_backend_plt_readonly 1
2522 #define elf_backend_want_plt_sym 0
2523 #define elf_backend_got_header_size 12
2524 #define elf_backend_rela_normal 1
2526 #include "elf32-target.h"