2007-07-26 Michael Snyder <msnyder@access-company.com>
[binutils.git] / bfd / elf32-m68k.c
blobffa1668ada49e66ec27703d8ec715a4e35d4a8bf
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 3 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,
20 MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/m68k.h"
28 #include "opcode/m68k.h"
30 static reloc_howto_type *reloc_type_lookup
31 PARAMS ((bfd *, bfd_reloc_code_real_type));
32 static void rtype_to_howto
33 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
34 static struct bfd_hash_entry *elf_m68k_link_hash_newfunc
35 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
36 static struct bfd_link_hash_table *elf_m68k_link_hash_table_create
37 PARAMS ((bfd *));
38 static bfd_boolean elf_m68k_check_relocs
39 PARAMS ((bfd *, struct bfd_link_info *, asection *,
40 const Elf_Internal_Rela *));
41 static bfd_boolean elf_m68k_adjust_dynamic_symbol
42 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
43 static bfd_boolean elf_m68k_size_dynamic_sections
44 PARAMS ((bfd *, struct bfd_link_info *));
45 static bfd_boolean elf_m68k_discard_copies
46 PARAMS ((struct elf_link_hash_entry *, PTR));
47 static bfd_boolean elf_m68k_relocate_section
48 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
49 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
50 static bfd_boolean elf_m68k_finish_dynamic_symbol
51 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
52 Elf_Internal_Sym *));
53 static bfd_boolean elf_m68k_finish_dynamic_sections
54 PARAMS ((bfd *, struct bfd_link_info *));
56 static bfd_boolean elf32_m68k_set_private_flags
57 PARAMS ((bfd *, flagword));
58 static bfd_boolean elf32_m68k_merge_private_bfd_data
59 PARAMS ((bfd *, bfd *));
60 static bfd_boolean elf32_m68k_print_private_bfd_data
61 PARAMS ((bfd *, PTR));
62 static enum elf_reloc_type_class elf32_m68k_reloc_type_class
63 PARAMS ((const Elf_Internal_Rela *));
65 static reloc_howto_type howto_table[] = {
66 HOWTO(R_68K_NONE, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_NONE", FALSE, 0, 0x00000000,FALSE),
67 HOWTO(R_68K_32, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32", FALSE, 0, 0xffffffff,FALSE),
68 HOWTO(R_68K_16, 0, 1,16, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16", FALSE, 0, 0x0000ffff,FALSE),
69 HOWTO(R_68K_8, 0, 0, 8, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8", FALSE, 0, 0x000000ff,FALSE),
70 HOWTO(R_68K_PC32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32", FALSE, 0, 0xffffffff,TRUE),
71 HOWTO(R_68K_PC16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC16", FALSE, 0, 0x0000ffff,TRUE),
72 HOWTO(R_68K_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC8", FALSE, 0, 0x000000ff,TRUE),
73 HOWTO(R_68K_GOT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32", FALSE, 0, 0xffffffff,TRUE),
74 HOWTO(R_68K_GOT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16", FALSE, 0, 0x0000ffff,TRUE),
75 HOWTO(R_68K_GOT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8", FALSE, 0, 0x000000ff,TRUE),
76 HOWTO(R_68K_GOT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O", FALSE, 0, 0xffffffff,FALSE),
77 HOWTO(R_68K_GOT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16O", FALSE, 0, 0x0000ffff,FALSE),
78 HOWTO(R_68K_GOT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8O", FALSE, 0, 0x000000ff,FALSE),
79 HOWTO(R_68K_PLT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32", FALSE, 0, 0xffffffff,TRUE),
80 HOWTO(R_68K_PLT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16", FALSE, 0, 0x0000ffff,TRUE),
81 HOWTO(R_68K_PLT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8", FALSE, 0, 0x000000ff,TRUE),
82 HOWTO(R_68K_PLT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O", FALSE, 0, 0xffffffff,FALSE),
83 HOWTO(R_68K_PLT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16O", FALSE, 0, 0x0000ffff,FALSE),
84 HOWTO(R_68K_PLT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8O", FALSE, 0, 0x000000ff,FALSE),
85 HOWTO(R_68K_COPY, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_COPY", FALSE, 0, 0xffffffff,FALSE),
86 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),
87 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),
88 HOWTO(R_68K_RELATIVE, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_RELATIVE", FALSE, 0, 0xffffffff,FALSE),
89 /* GNU extension to record C++ vtable hierarchy. */
90 HOWTO (R_68K_GNU_VTINHERIT, /* type */
91 0, /* rightshift */
92 2, /* size (0 = byte, 1 = short, 2 = long) */
93 0, /* bitsize */
94 FALSE, /* pc_relative */
95 0, /* bitpos */
96 complain_overflow_dont, /* complain_on_overflow */
97 NULL, /* special_function */
98 "R_68K_GNU_VTINHERIT", /* name */
99 FALSE, /* partial_inplace */
100 0, /* src_mask */
101 0, /* dst_mask */
102 FALSE),
103 /* GNU extension to record C++ vtable member usage. */
104 HOWTO (R_68K_GNU_VTENTRY, /* type */
105 0, /* rightshift */
106 2, /* size (0 = byte, 1 = short, 2 = long) */
107 0, /* bitsize */
108 FALSE, /* pc_relative */
109 0, /* bitpos */
110 complain_overflow_dont, /* complain_on_overflow */
111 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
112 "R_68K_GNU_VTENTRY", /* name */
113 FALSE, /* partial_inplace */
114 0, /* src_mask */
115 0, /* dst_mask */
116 FALSE),
119 static void
120 rtype_to_howto (abfd, cache_ptr, dst)
121 bfd *abfd ATTRIBUTE_UNUSED;
122 arelent *cache_ptr;
123 Elf_Internal_Rela *dst;
125 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K_max);
126 cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
129 #define elf_info_to_howto rtype_to_howto
131 static const struct
133 bfd_reloc_code_real_type bfd_val;
134 int elf_val;
135 } reloc_map[] = {
136 { BFD_RELOC_NONE, R_68K_NONE },
137 { BFD_RELOC_32, R_68K_32 },
138 { BFD_RELOC_16, R_68K_16 },
139 { BFD_RELOC_8, R_68K_8 },
140 { BFD_RELOC_32_PCREL, R_68K_PC32 },
141 { BFD_RELOC_16_PCREL, R_68K_PC16 },
142 { BFD_RELOC_8_PCREL, R_68K_PC8 },
143 { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
144 { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
145 { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
146 { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
147 { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
148 { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
149 { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
150 { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
151 { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
152 { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
153 { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
154 { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
155 { BFD_RELOC_NONE, R_68K_COPY },
156 { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
157 { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
158 { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
159 { BFD_RELOC_CTOR, R_68K_32 },
160 { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
161 { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
164 static reloc_howto_type *
165 reloc_type_lookup (abfd, code)
166 bfd *abfd ATTRIBUTE_UNUSED;
167 bfd_reloc_code_real_type code;
169 unsigned int i;
170 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
172 if (reloc_map[i].bfd_val == code)
173 return &howto_table[reloc_map[i].elf_val];
175 return 0;
178 static reloc_howto_type *
179 reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
181 unsigned int i;
183 for (i = 0; i < sizeof (howto_table) / sizeof (howto_table[0]); i++)
184 if (howto_table[i].name != NULL
185 && strcasecmp (howto_table[i].name, r_name) == 0)
186 return &howto_table[i];
188 return NULL;
191 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
192 #define bfd_elf32_bfd_reloc_name_lookup reloc_name_lookup
193 #define ELF_ARCH bfd_arch_m68k
195 /* Functions for the m68k ELF linker. */
197 /* The name of the dynamic interpreter. This is put in the .interp
198 section. */
200 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
202 /* Describes one of the various PLT styles. */
204 struct elf_m68k_plt_info
206 /* The size of each PLT entry. */
207 bfd_vma size;
209 /* The template for the first PLT entry. */
210 const bfd_byte *plt0_entry;
212 /* Offsets of fields in PLT0_ENTRY that require R_68K_PC32 relocations.
213 The comments by each member indicate the value that the relocation
214 is against. */
215 struct {
216 unsigned int got4; /* .got + 4 */
217 unsigned int got8; /* .got + 8 */
218 } plt0_relocs;
220 /* The template for a symbol's PLT entry. */
221 const bfd_byte *symbol_entry;
223 /* Offsets of fields in SYMBOL_ENTRY that require R_68K_PC32 relocations.
224 The comments by each member indicate the value that the relocation
225 is against. */
226 struct {
227 unsigned int got; /* the symbol's .got.plt entry */
228 unsigned int plt; /* .plt */
229 } symbol_relocs;
231 /* The offset of the resolver stub from the start of SYMBOL_ENTRY.
232 The stub starts with "move.l #relocoffset,%d0". */
233 bfd_vma symbol_resolve_entry;
236 /* The size in bytes of an entry in the procedure linkage table. */
238 #define PLT_ENTRY_SIZE 20
240 /* The first entry in a procedure linkage table looks like this. See
241 the SVR4 ABI m68k supplement to see how this works. */
243 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
245 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
246 0, 0, 0, 2, /* + (.got + 4) - . */
247 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
248 0, 0, 0, 2, /* + (.got + 8) - . */
249 0, 0, 0, 0 /* pad out to 20 bytes. */
252 /* Subsequent entries in a procedure linkage table look like this. */
254 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
256 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
257 0, 0, 0, 2, /* + (.got.plt entry) - . */
258 0x2f, 0x3c, /* move.l #offset,-(%sp) */
259 0, 0, 0, 0, /* + reloc index */
260 0x60, 0xff, /* bra.l .plt */
261 0, 0, 0, 0 /* + .plt - . */
264 static const struct elf_m68k_plt_info elf_m68k_plt_info = {
265 PLT_ENTRY_SIZE,
266 elf_m68k_plt0_entry, { 4, 12 },
267 elf_m68k_plt_entry, { 4, 16 }, 8
270 #define ISAB_PLT_ENTRY_SIZE 24
272 static const bfd_byte elf_isab_plt0_entry[ISAB_PLT_ENTRY_SIZE] =
274 0x20, 0x3c, /* move.l #offset,%d0 */
275 0, 0, 0, 0, /* + (.got + 4) - . */
276 0x2f, 0x3b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),-(%sp) */
277 0x20, 0x3c, /* move.l #offset,%d0 */
278 0, 0, 0, 0, /* + (.got + 8) - . */
279 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
280 0x4e, 0xd0, /* jmp (%a0) */
281 0x4e, 0x71 /* nop */
284 /* Subsequent entries in a procedure linkage table look like this. */
286 static const bfd_byte elf_isab_plt_entry[ISAB_PLT_ENTRY_SIZE] =
288 0x20, 0x3c, /* move.l #offset,%d0 */
289 0, 0, 0, 0, /* + (.got.plt entry) - . */
290 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
291 0x4e, 0xd0, /* jmp (%a0) */
292 0x2f, 0x3c, /* move.l #offset,-(%sp) */
293 0, 0, 0, 0, /* + reloc index */
294 0x60, 0xff, /* bra.l .plt */
295 0, 0, 0, 0 /* + .plt - . */
298 static const struct elf_m68k_plt_info elf_isab_plt_info = {
299 ISAB_PLT_ENTRY_SIZE,
300 elf_isab_plt0_entry, { 2, 12 },
301 elf_isab_plt_entry, { 2, 20 }, 12
304 #define ISAC_PLT_ENTRY_SIZE 24
306 static const bfd_byte elf_isac_plt0_entry[ISAC_PLT_ENTRY_SIZE] =
308 0x20, 0x3c, /* move.l #offset,%d0 */
309 0, 0, 0, 0, /* replaced with .got + 4 - . */
310 0x2e, 0xbb, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),(%sp) */
311 0x20, 0x3c, /* move.l #offset,%d0 */
312 0, 0, 0, 0, /* replaced with .got + 8 - . */
313 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
314 0x4e, 0xd0, /* jmp (%a0) */
315 0x4e, 0x71 /* nop */
318 /* Subsequent entries in a procedure linkage table look like this. */
320 static const bfd_byte elf_isac_plt_entry[ISAC_PLT_ENTRY_SIZE] =
322 0x20, 0x3c, /* move.l #offset,%d0 */
323 0, 0, 0, 0, /* replaced with (.got entry) - . */
324 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
325 0x4e, 0xd0, /* jmp (%a0) */
326 0x2f, 0x3c, /* move.l #offset,-(%sp) */
327 0, 0, 0, 0, /* replaced with offset into relocation table */
328 0x61, 0xff, /* bsr.l .plt */
329 0, 0, 0, 0 /* replaced with .plt - . */
332 static const struct elf_m68k_plt_info elf_isac_plt_info = {
333 ISAC_PLT_ENTRY_SIZE,
334 elf_isac_plt0_entry, { 2, 12},
335 elf_isac_plt_entry, { 2, 20 }, 12
338 #define CPU32_PLT_ENTRY_SIZE 24
339 /* Procedure linkage table entries for the cpu32 */
340 static const bfd_byte elf_cpu32_plt0_entry[CPU32_PLT_ENTRY_SIZE] =
342 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
343 0, 0, 0, 2, /* + (.got + 4) - . */
344 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
345 0, 0, 0, 2, /* + (.got + 8) - . */
346 0x4e, 0xd1, /* jmp %a1@ */
347 0, 0, 0, 0, /* pad out to 24 bytes. */
348 0, 0
351 static const bfd_byte elf_cpu32_plt_entry[CPU32_PLT_ENTRY_SIZE] =
353 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
354 0, 0, 0, 2, /* + (.got.plt entry) - . */
355 0x4e, 0xd1, /* jmp %a1@ */
356 0x2f, 0x3c, /* move.l #offset,-(%sp) */
357 0, 0, 0, 0, /* + reloc index */
358 0x60, 0xff, /* bra.l .plt */
359 0, 0, 0, 0, /* + .plt - . */
360 0, 0
363 static const struct elf_m68k_plt_info elf_cpu32_plt_info = {
364 CPU32_PLT_ENTRY_SIZE,
365 elf_cpu32_plt0_entry, { 4, 12 },
366 elf_cpu32_plt_entry, { 4, 18 }, 10
369 /* The m68k linker needs to keep track of the number of relocs that it
370 decides to copy in check_relocs for each symbol. This is so that it
371 can discard PC relative relocs if it doesn't need them when linking
372 with -Bsymbolic. We store the information in a field extending the
373 regular ELF linker hash table. */
375 /* This structure keeps track of the number of PC relative relocs we have
376 copied for a given symbol. */
378 struct elf_m68k_pcrel_relocs_copied
380 /* Next section. */
381 struct elf_m68k_pcrel_relocs_copied *next;
382 /* A section in dynobj. */
383 asection *section;
384 /* Number of relocs copied in this section. */
385 bfd_size_type count;
388 /* m68k ELF linker hash entry. */
390 struct elf_m68k_link_hash_entry
392 struct elf_link_hash_entry root;
394 /* Number of PC relative relocs copied for this symbol. */
395 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
398 #define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
400 /* m68k ELF linker hash table. */
402 struct elf_m68k_link_hash_table
404 struct elf_link_hash_table root;
406 /* Small local sym to section mapping cache. */
407 struct sym_sec_cache sym_sec;
409 /* The PLT format used by this link, or NULL if the format has not
410 yet been chosen. */
411 const struct elf_m68k_plt_info *plt_info;
414 /* Get the m68k ELF linker hash table from a link_info structure. */
416 #define elf_m68k_hash_table(p) \
417 ((struct elf_m68k_link_hash_table *) (p)->hash)
419 /* Create an entry in an m68k ELF linker hash table. */
421 static struct bfd_hash_entry *
422 elf_m68k_link_hash_newfunc (entry, table, string)
423 struct bfd_hash_entry *entry;
424 struct bfd_hash_table *table;
425 const char *string;
427 struct bfd_hash_entry *ret = entry;
429 /* Allocate the structure if it has not already been allocated by a
430 subclass. */
431 if (ret == NULL)
432 ret = bfd_hash_allocate (table,
433 sizeof (struct elf_m68k_link_hash_entry));
434 if (ret == NULL)
435 return ret;
437 /* Call the allocation method of the superclass. */
438 ret = _bfd_elf_link_hash_newfunc (ret, table, string);
439 if (ret != NULL)
440 elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
442 return ret;
445 /* Create an m68k ELF linker hash table. */
447 static struct bfd_link_hash_table *
448 elf_m68k_link_hash_table_create (abfd)
449 bfd *abfd;
451 struct elf_m68k_link_hash_table *ret;
452 bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
454 ret = (struct elf_m68k_link_hash_table *) bfd_malloc (amt);
455 if (ret == (struct elf_m68k_link_hash_table *) NULL)
456 return NULL;
458 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
459 elf_m68k_link_hash_newfunc,
460 sizeof (struct elf_m68k_link_hash_entry)))
462 free (ret);
463 return NULL;
466 ret->sym_sec.abfd = NULL;
467 ret->plt_info = NULL;
469 return &ret->root.root;
472 /* Set the right machine number. */
474 static bfd_boolean
475 elf32_m68k_object_p (bfd *abfd)
477 unsigned int mach = 0;
478 unsigned features = 0;
479 flagword eflags = elf_elfheader (abfd)->e_flags;
481 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
482 features |= m68000;
483 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
484 features |= cpu32;
485 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
486 features |= fido_a;
487 else
489 switch (eflags & EF_M68K_CF_ISA_MASK)
491 case EF_M68K_CF_ISA_A_NODIV:
492 features |= mcfisa_a;
493 break;
494 case EF_M68K_CF_ISA_A:
495 features |= mcfisa_a|mcfhwdiv;
496 break;
497 case EF_M68K_CF_ISA_A_PLUS:
498 features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
499 break;
500 case EF_M68K_CF_ISA_B_NOUSP:
501 features |= mcfisa_a|mcfisa_b|mcfhwdiv;
502 break;
503 case EF_M68K_CF_ISA_B:
504 features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
505 break;
506 case EF_M68K_CF_ISA_C:
507 features |= mcfisa_a|mcfisa_c|mcfhwdiv|mcfusp;
508 break;
510 switch (eflags & EF_M68K_CF_MAC_MASK)
512 case EF_M68K_CF_MAC:
513 features |= mcfmac;
514 break;
515 case EF_M68K_CF_EMAC:
516 features |= mcfemac;
517 break;
519 if (eflags & EF_M68K_CF_FLOAT)
520 features |= cfloat;
523 mach = bfd_m68k_features_to_mach (features);
524 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
526 return TRUE;
529 /* Keep m68k-specific flags in the ELF header. */
530 static bfd_boolean
531 elf32_m68k_set_private_flags (abfd, flags)
532 bfd *abfd;
533 flagword flags;
535 elf_elfheader (abfd)->e_flags = flags;
536 elf_flags_init (abfd) = TRUE;
537 return TRUE;
540 /* Merge backend specific data from an object file to the output
541 object file when linking. */
542 static bfd_boolean
543 elf32_m68k_merge_private_bfd_data (ibfd, obfd)
544 bfd *ibfd;
545 bfd *obfd;
547 flagword out_flags;
548 flagword in_flags;
549 flagword out_isa;
550 flagword in_isa;
551 const bfd_arch_info_type *arch_info;
553 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
554 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
555 return FALSE;
557 /* Get the merged machine. This checks for incompatibility between
558 Coldfire & non-Coldfire flags, incompability between different
559 Coldfire ISAs, and incompability between different MAC types. */
560 arch_info = bfd_arch_get_compatible (ibfd, obfd, FALSE);
561 if (!arch_info)
562 return FALSE;
564 bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
566 in_flags = elf_elfheader (ibfd)->e_flags;
567 if (!elf_flags_init (obfd))
569 elf_flags_init (obfd) = TRUE;
570 out_flags = in_flags;
572 else
574 out_flags = elf_elfheader (obfd)->e_flags;
575 unsigned int variant_mask;
577 if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
578 variant_mask = 0;
579 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
580 variant_mask = 0;
581 else if ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
582 variant_mask = 0;
583 else
584 variant_mask = EF_M68K_CF_ISA_MASK;
586 in_isa = (in_flags & variant_mask);
587 out_isa = (out_flags & variant_mask);
588 if (in_isa > out_isa)
589 out_flags ^= in_isa ^ out_isa;
590 if (((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32
591 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
592 || ((in_flags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO
593 && (out_flags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32))
594 out_flags = EF_M68K_FIDO;
595 else
596 out_flags |= in_flags ^ in_isa;
598 elf_elfheader (obfd)->e_flags = out_flags;
600 return TRUE;
603 /* Display the flags field. */
604 static bfd_boolean
605 elf32_m68k_print_private_bfd_data (abfd, ptr)
606 bfd *abfd;
607 PTR ptr;
609 FILE *file = (FILE *) ptr;
610 flagword eflags = elf_elfheader (abfd)->e_flags;
612 BFD_ASSERT (abfd != NULL && ptr != NULL);
614 /* Print normal ELF private data. */
615 _bfd_elf_print_private_bfd_data (abfd, ptr);
617 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
619 /* xgettext:c-format */
620 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
622 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_M68000)
623 fprintf (file, " [m68000]");
624 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CPU32)
625 fprintf (file, " [cpu32]");
626 else if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_FIDO)
627 fprintf (file, " [fido]");
628 else
630 if ((eflags & EF_M68K_ARCH_MASK) == EF_M68K_CFV4E)
631 fprintf (file, " [cfv4e]");
633 if (eflags & EF_M68K_CF_ISA_MASK)
635 char const *isa = _("unknown");
636 char const *mac = _("unknown");
637 char const *additional = "";
639 switch (eflags & EF_M68K_CF_ISA_MASK)
641 case EF_M68K_CF_ISA_A_NODIV:
642 isa = "A";
643 additional = " [nodiv]";
644 break;
645 case EF_M68K_CF_ISA_A:
646 isa = "A";
647 break;
648 case EF_M68K_CF_ISA_A_PLUS:
649 isa = "A+";
650 break;
651 case EF_M68K_CF_ISA_B_NOUSP:
652 isa = "B";
653 additional = " [nousp]";
654 break;
655 case EF_M68K_CF_ISA_B:
656 isa = "B";
657 break;
658 case EF_M68K_CF_ISA_C:
659 isa = "C";
660 break;
662 fprintf (file, " [isa %s]%s", isa, additional);
663 if (eflags & EF_M68K_CF_FLOAT)
664 fprintf (file, " [float]");
665 switch (eflags & EF_M68K_CF_MAC_MASK)
667 case 0:
668 mac = NULL;
669 break;
670 case EF_M68K_CF_MAC:
671 mac = "mac";
672 break;
673 case EF_M68K_CF_EMAC:
674 mac = "emac";
675 break;
677 if (mac)
678 fprintf (file, " [%s]", mac);
682 fputc ('\n', file);
684 return TRUE;
686 /* Look through the relocs for a section during the first phase, and
687 allocate space in the global offset table or procedure linkage
688 table. */
690 static bfd_boolean
691 elf_m68k_check_relocs (abfd, info, sec, relocs)
692 bfd *abfd;
693 struct bfd_link_info *info;
694 asection *sec;
695 const Elf_Internal_Rela *relocs;
697 bfd *dynobj;
698 Elf_Internal_Shdr *symtab_hdr;
699 struct elf_link_hash_entry **sym_hashes;
700 bfd_signed_vma *local_got_refcounts;
701 const Elf_Internal_Rela *rel;
702 const Elf_Internal_Rela *rel_end;
703 asection *sgot;
704 asection *srelgot;
705 asection *sreloc;
707 if (info->relocatable)
708 return TRUE;
710 dynobj = elf_hash_table (info)->dynobj;
711 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
712 sym_hashes = elf_sym_hashes (abfd);
713 local_got_refcounts = elf_local_got_refcounts (abfd);
715 sgot = NULL;
716 srelgot = NULL;
717 sreloc = NULL;
719 rel_end = relocs + sec->reloc_count;
720 for (rel = relocs; rel < rel_end; rel++)
722 unsigned long r_symndx;
723 struct elf_link_hash_entry *h;
725 r_symndx = ELF32_R_SYM (rel->r_info);
727 if (r_symndx < symtab_hdr->sh_info)
728 h = NULL;
729 else
731 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
732 while (h->root.type == bfd_link_hash_indirect
733 || h->root.type == bfd_link_hash_warning)
734 h = (struct elf_link_hash_entry *) h->root.u.i.link;
737 switch (ELF32_R_TYPE (rel->r_info))
739 case R_68K_GOT8:
740 case R_68K_GOT16:
741 case R_68K_GOT32:
742 if (h != NULL
743 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
744 break;
745 /* Fall through. */
746 case R_68K_GOT8O:
747 case R_68K_GOT16O:
748 case R_68K_GOT32O:
749 /* This symbol requires a global offset table entry. */
751 if (dynobj == NULL)
753 /* Create the .got section. */
754 elf_hash_table (info)->dynobj = dynobj = abfd;
755 if (!_bfd_elf_create_got_section (dynobj, info))
756 return FALSE;
759 if (sgot == NULL)
761 sgot = bfd_get_section_by_name (dynobj, ".got");
762 BFD_ASSERT (sgot != NULL);
765 if (srelgot == NULL
766 && (h != NULL || info->shared))
768 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
769 if (srelgot == NULL)
771 srelgot = bfd_make_section_with_flags (dynobj,
772 ".rela.got",
773 (SEC_ALLOC
774 | SEC_LOAD
775 | SEC_HAS_CONTENTS
776 | SEC_IN_MEMORY
777 | SEC_LINKER_CREATED
778 | SEC_READONLY));
779 if (srelgot == NULL
780 || !bfd_set_section_alignment (dynobj, srelgot, 2))
781 return FALSE;
785 if (h != NULL)
787 if (h->got.refcount == 0)
789 /* Make sure this symbol is output as a dynamic symbol. */
790 if (h->dynindx == -1
791 && !h->forced_local)
793 if (!bfd_elf_link_record_dynamic_symbol (info, h))
794 return FALSE;
797 /* Allocate space in the .got section. */
798 sgot->size += 4;
799 /* Allocate relocation space. */
800 srelgot->size += sizeof (Elf32_External_Rela);
802 h->got.refcount++;
804 else
806 /* This is a global offset table entry for a local symbol. */
807 if (local_got_refcounts == NULL)
809 bfd_size_type size;
811 size = symtab_hdr->sh_info;
812 size *= sizeof (bfd_signed_vma);
813 local_got_refcounts = ((bfd_signed_vma *)
814 bfd_zalloc (abfd, size));
815 if (local_got_refcounts == NULL)
816 return FALSE;
817 elf_local_got_refcounts (abfd) = local_got_refcounts;
819 if (local_got_refcounts[r_symndx] == 0)
821 sgot->size += 4;
822 if (info->shared)
824 /* If we are generating a shared object, we need to
825 output a R_68K_RELATIVE reloc so that the dynamic
826 linker can adjust this GOT entry. */
827 srelgot->size += sizeof (Elf32_External_Rela);
830 local_got_refcounts[r_symndx]++;
832 break;
834 case R_68K_PLT8:
835 case R_68K_PLT16:
836 case R_68K_PLT32:
837 /* This symbol requires a procedure linkage table entry. We
838 actually build the entry in adjust_dynamic_symbol,
839 because this might be a case of linking PIC code which is
840 never referenced by a dynamic object, in which case we
841 don't need to generate a procedure linkage table entry
842 after all. */
844 /* If this is a local symbol, we resolve it directly without
845 creating a procedure linkage table entry. */
846 if (h == NULL)
847 continue;
849 h->needs_plt = 1;
850 h->plt.refcount++;
851 break;
853 case R_68K_PLT8O:
854 case R_68K_PLT16O:
855 case R_68K_PLT32O:
856 /* This symbol requires a procedure linkage table entry. */
858 if (h == NULL)
860 /* It does not make sense to have this relocation for a
861 local symbol. FIXME: does it? How to handle it if
862 it does make sense? */
863 bfd_set_error (bfd_error_bad_value);
864 return FALSE;
867 /* Make sure this symbol is output as a dynamic symbol. */
868 if (h->dynindx == -1
869 && !h->forced_local)
871 if (!bfd_elf_link_record_dynamic_symbol (info, h))
872 return FALSE;
875 h->needs_plt = 1;
876 h->plt.refcount++;
877 break;
879 case R_68K_PC8:
880 case R_68K_PC16:
881 case R_68K_PC32:
882 /* If we are creating a shared library and this is not a local
883 symbol, we need to copy the reloc into the shared library.
884 However when linking with -Bsymbolic and this is a global
885 symbol which is defined in an object we are including in the
886 link (i.e., DEF_REGULAR is set), then we can resolve the
887 reloc directly. At this point we have not seen all the input
888 files, so it is possible that DEF_REGULAR is not set now but
889 will be set later (it is never cleared). We account for that
890 possibility below by storing information in the
891 pcrel_relocs_copied field of the hash table entry. */
892 if (!(info->shared
893 && (sec->flags & SEC_ALLOC) != 0
894 && h != NULL
895 && (!info->symbolic
896 || h->root.type == bfd_link_hash_defweak
897 || !h->def_regular)))
899 if (h != NULL)
901 /* Make sure a plt entry is created for this symbol if
902 it turns out to be a function defined by a dynamic
903 object. */
904 h->plt.refcount++;
906 break;
908 /* Fall through. */
909 case R_68K_8:
910 case R_68K_16:
911 case R_68K_32:
912 if (h != NULL)
914 /* Make sure a plt entry is created for this symbol if it
915 turns out to be a function defined by a dynamic object. */
916 h->plt.refcount++;
919 /* If we are creating a shared library, we need to copy the
920 reloc into the shared library. */
921 if (info->shared
922 && (sec->flags & SEC_ALLOC) != 0)
924 /* When creating a shared object, we must copy these
925 reloc types into the output file. We create a reloc
926 section in dynobj and make room for this reloc. */
927 if (sreloc == NULL)
929 const char *name;
931 name = (bfd_elf_string_from_elf_section
932 (abfd,
933 elf_elfheader (abfd)->e_shstrndx,
934 elf_section_data (sec)->rel_hdr.sh_name));
935 if (name == NULL)
936 return FALSE;
938 BFD_ASSERT (CONST_STRNEQ (name, ".rela")
939 && strcmp (bfd_get_section_name (abfd, sec),
940 name + 5) == 0);
942 sreloc = bfd_get_section_by_name (dynobj, name);
943 if (sreloc == NULL)
945 sreloc = bfd_make_section_with_flags (dynobj,
946 name,
947 (SEC_ALLOC
948 | SEC_LOAD
949 | SEC_HAS_CONTENTS
950 | SEC_IN_MEMORY
951 | SEC_LINKER_CREATED
952 | SEC_READONLY));
953 if (sreloc == NULL
954 || !bfd_set_section_alignment (dynobj, sreloc, 2))
955 return FALSE;
957 elf_section_data (sec)->sreloc = sreloc;
960 if (sec->flags & SEC_READONLY
961 /* Don't set DF_TEXTREL yet for PC relative
962 relocations, they might be discarded later. */
963 && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
964 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
965 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
966 info->flags |= DF_TEXTREL;
968 sreloc->size += sizeof (Elf32_External_Rela);
970 /* We count the number of PC relative relocations we have
971 entered for this symbol, so that we can discard them
972 again if, in the -Bsymbolic case, the symbol is later
973 defined by a regular object, or, in the normal shared
974 case, the symbol is forced to be local. Note that this
975 function is only called if we are using an m68kelf linker
976 hash table, which means that h is really a pointer to an
977 elf_m68k_link_hash_entry. */
978 if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
979 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
980 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
982 struct elf_m68k_pcrel_relocs_copied *p;
983 struct elf_m68k_pcrel_relocs_copied **head;
985 if (h != NULL)
987 struct elf_m68k_link_hash_entry *eh
988 = elf_m68k_hash_entry (h);
989 head = &eh->pcrel_relocs_copied;
991 else
993 asection *s;
994 void *vpp;
996 s = (bfd_section_from_r_symndx
997 (abfd, &elf_m68k_hash_table (info)->sym_sec,
998 sec, r_symndx));
999 if (s == NULL)
1000 return FALSE;
1002 vpp = &elf_section_data (s)->local_dynrel;
1003 head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
1006 for (p = *head; p != NULL; p = p->next)
1007 if (p->section == sreloc)
1008 break;
1010 if (p == NULL)
1012 p = ((struct elf_m68k_pcrel_relocs_copied *)
1013 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
1014 if (p == NULL)
1015 return FALSE;
1016 p->next = *head;
1017 *head = p;
1018 p->section = sreloc;
1019 p->count = 0;
1022 ++p->count;
1026 break;
1028 /* This relocation describes the C++ object vtable hierarchy.
1029 Reconstruct it for later use during GC. */
1030 case R_68K_GNU_VTINHERIT:
1031 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1032 return FALSE;
1033 break;
1035 /* This relocation describes which C++ vtable entries are actually
1036 used. Record for later use during GC. */
1037 case R_68K_GNU_VTENTRY:
1038 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1039 return FALSE;
1040 break;
1042 default:
1043 break;
1047 return TRUE;
1050 /* Return the section that should be marked against GC for a given
1051 relocation. */
1053 static asection *
1054 elf_m68k_gc_mark_hook (asection *sec,
1055 struct bfd_link_info *info,
1056 Elf_Internal_Rela *rel,
1057 struct elf_link_hash_entry *h,
1058 Elf_Internal_Sym *sym)
1060 if (h != NULL)
1061 switch (ELF32_R_TYPE (rel->r_info))
1063 case R_68K_GNU_VTINHERIT:
1064 case R_68K_GNU_VTENTRY:
1065 return NULL;
1068 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1071 /* Update the got entry reference counts for the section being removed. */
1073 static bfd_boolean
1074 elf_m68k_gc_sweep_hook (bfd *abfd,
1075 struct bfd_link_info *info,
1076 asection *sec,
1077 const Elf_Internal_Rela *relocs)
1079 Elf_Internal_Shdr *symtab_hdr;
1080 struct elf_link_hash_entry **sym_hashes;
1081 bfd_signed_vma *local_got_refcounts;
1082 const Elf_Internal_Rela *rel, *relend;
1083 bfd *dynobj;
1084 asection *sgot;
1085 asection *srelgot;
1087 dynobj = elf_hash_table (info)->dynobj;
1088 if (dynobj == NULL)
1089 return TRUE;
1091 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1092 sym_hashes = elf_sym_hashes (abfd);
1093 local_got_refcounts = elf_local_got_refcounts (abfd);
1095 sgot = bfd_get_section_by_name (dynobj, ".got");
1096 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1098 relend = relocs + sec->reloc_count;
1099 for (rel = relocs; rel < relend; rel++)
1101 unsigned long r_symndx;
1102 struct elf_link_hash_entry *h = NULL;
1104 r_symndx = ELF32_R_SYM (rel->r_info);
1105 if (r_symndx >= symtab_hdr->sh_info)
1107 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1108 while (h->root.type == bfd_link_hash_indirect
1109 || h->root.type == bfd_link_hash_warning)
1110 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1113 switch (ELF32_R_TYPE (rel->r_info))
1115 case R_68K_GOT8:
1116 case R_68K_GOT16:
1117 case R_68K_GOT32:
1118 case R_68K_GOT8O:
1119 case R_68K_GOT16O:
1120 case R_68K_GOT32O:
1121 if (h != NULL)
1123 if (h->got.refcount > 0)
1125 --h->got.refcount;
1126 if (h->got.refcount == 0)
1128 /* We don't need the .got entry any more. */
1129 sgot->size -= 4;
1130 srelgot->size -= sizeof (Elf32_External_Rela);
1134 else if (local_got_refcounts != NULL)
1136 if (local_got_refcounts[r_symndx] > 0)
1138 --local_got_refcounts[r_symndx];
1139 if (local_got_refcounts[r_symndx] == 0)
1141 /* We don't need the .got entry any more. */
1142 sgot->size -= 4;
1143 if (info->shared)
1144 srelgot->size -= sizeof (Elf32_External_Rela);
1148 break;
1150 case R_68K_PLT8:
1151 case R_68K_PLT16:
1152 case R_68K_PLT32:
1153 case R_68K_PLT8O:
1154 case R_68K_PLT16O:
1155 case R_68K_PLT32O:
1156 case R_68K_PC8:
1157 case R_68K_PC16:
1158 case R_68K_PC32:
1159 case R_68K_8:
1160 case R_68K_16:
1161 case R_68K_32:
1162 if (h != NULL)
1164 if (h->plt.refcount > 0)
1165 --h->plt.refcount;
1167 break;
1169 default:
1170 break;
1174 return TRUE;
1177 /* Return the type of PLT associated with OUTPUT_BFD. */
1179 static const struct elf_m68k_plt_info *
1180 elf_m68k_get_plt_info (bfd *output_bfd)
1182 unsigned int features;
1184 features = bfd_m68k_mach_to_features (bfd_get_mach (output_bfd));
1185 if (features & cpu32)
1186 return &elf_cpu32_plt_info;
1187 if (features & mcfisa_b)
1188 return &elf_isab_plt_info;
1189 if (features & mcfisa_c)
1190 return &elf_isac_plt_info;
1191 return &elf_m68k_plt_info;
1194 /* This function is called after all the input files have been read,
1195 and the input sections have been assigned to output sections.
1196 It's a convenient place to determine the PLT style. */
1198 static bfd_boolean
1199 elf_m68k_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
1201 elf_m68k_hash_table (info)->plt_info = elf_m68k_get_plt_info (output_bfd);
1202 return TRUE;
1205 /* Adjust a symbol defined by a dynamic object and referenced by a
1206 regular object. The current definition is in some section of the
1207 dynamic object, but we're not including those sections. We have to
1208 change the definition to something the rest of the link can
1209 understand. */
1211 static bfd_boolean
1212 elf_m68k_adjust_dynamic_symbol (info, h)
1213 struct bfd_link_info *info;
1214 struct elf_link_hash_entry *h;
1216 struct elf_m68k_link_hash_table *htab;
1217 bfd *dynobj;
1218 asection *s;
1220 htab = elf_m68k_hash_table (info);
1221 dynobj = elf_hash_table (info)->dynobj;
1223 /* Make sure we know what is going on here. */
1224 BFD_ASSERT (dynobj != NULL
1225 && (h->needs_plt
1226 || h->u.weakdef != NULL
1227 || (h->def_dynamic
1228 && h->ref_regular
1229 && !h->def_regular)));
1231 /* If this is a function, put it in the procedure linkage table. We
1232 will fill in the contents of the procedure linkage table later,
1233 when we know the address of the .got section. */
1234 if (h->type == STT_FUNC
1235 || h->needs_plt)
1237 if ((h->plt.refcount <= 0
1238 || SYMBOL_CALLS_LOCAL (info, h)
1239 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1240 && h->root.type == bfd_link_hash_undefweak))
1241 /* We must always create the plt entry if it was referenced
1242 by a PLTxxO relocation. In this case we already recorded
1243 it as a dynamic symbol. */
1244 && h->dynindx == -1)
1246 /* This case can occur if we saw a PLTxx reloc in an input
1247 file, but the symbol was never referred to by a dynamic
1248 object, or if all references were garbage collected. In
1249 such a case, we don't actually need to build a procedure
1250 linkage table, and we can just do a PCxx reloc instead. */
1251 h->plt.offset = (bfd_vma) -1;
1252 h->needs_plt = 0;
1253 return TRUE;
1256 /* Make sure this symbol is output as a dynamic symbol. */
1257 if (h->dynindx == -1
1258 && !h->forced_local)
1260 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1261 return FALSE;
1264 s = bfd_get_section_by_name (dynobj, ".plt");
1265 BFD_ASSERT (s != NULL);
1267 /* If this is the first .plt entry, make room for the special
1268 first entry. */
1269 if (s->size == 0)
1270 s->size = htab->plt_info->size;
1272 /* If this symbol is not defined in a regular file, and we are
1273 not generating a shared library, then set the symbol to this
1274 location in the .plt. This is required to make function
1275 pointers compare as equal between the normal executable and
1276 the shared library. */
1277 if (!info->shared
1278 && !h->def_regular)
1280 h->root.u.def.section = s;
1281 h->root.u.def.value = s->size;
1284 h->plt.offset = s->size;
1286 /* Make room for this entry. */
1287 s->size += htab->plt_info->size;
1289 /* We also need to make an entry in the .got.plt section, which
1290 will be placed in the .got section by the linker script. */
1291 s = bfd_get_section_by_name (dynobj, ".got.plt");
1292 BFD_ASSERT (s != NULL);
1293 s->size += 4;
1295 /* We also need to make an entry in the .rela.plt section. */
1296 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1297 BFD_ASSERT (s != NULL);
1298 s->size += sizeof (Elf32_External_Rela);
1300 return TRUE;
1303 /* Reinitialize the plt offset now that it is not used as a reference
1304 count any more. */
1305 h->plt.offset = (bfd_vma) -1;
1307 /* If this is a weak symbol, and there is a real definition, the
1308 processor independent code will have arranged for us to see the
1309 real definition first, and we can just use the same value. */
1310 if (h->u.weakdef != NULL)
1312 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1313 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1314 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1315 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1316 return TRUE;
1319 /* This is a reference to a symbol defined by a dynamic object which
1320 is not a function. */
1322 /* If we are creating a shared library, we must presume that the
1323 only references to the symbol are via the global offset table.
1324 For such cases we need not do anything here; the relocations will
1325 be handled correctly by relocate_section. */
1326 if (info->shared)
1327 return TRUE;
1329 if (h->size == 0)
1331 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1332 h->root.root.string);
1333 return TRUE;
1336 /* We must allocate the symbol in our .dynbss section, which will
1337 become part of the .bss section of the executable. There will be
1338 an entry for this symbol in the .dynsym section. The dynamic
1339 object will contain position independent code, so all references
1340 from the dynamic object to this symbol will go through the global
1341 offset table. The dynamic linker will use the .dynsym entry to
1342 determine the address it must put in the global offset table, so
1343 both the dynamic object and the regular object will refer to the
1344 same memory location for the variable. */
1346 s = bfd_get_section_by_name (dynobj, ".dynbss");
1347 BFD_ASSERT (s != NULL);
1349 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1350 copy the initial value out of the dynamic object and into the
1351 runtime process image. We need to remember the offset into the
1352 .rela.bss section we are going to use. */
1353 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1355 asection *srel;
1357 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1358 BFD_ASSERT (srel != NULL);
1359 srel->size += sizeof (Elf32_External_Rela);
1360 h->needs_copy = 1;
1363 return _bfd_elf_adjust_dynamic_copy (h, s);
1366 /* Set the sizes of the dynamic sections. */
1368 static bfd_boolean
1369 elf_m68k_size_dynamic_sections (output_bfd, info)
1370 bfd *output_bfd ATTRIBUTE_UNUSED;
1371 struct bfd_link_info *info;
1373 bfd *dynobj;
1374 asection *s;
1375 bfd_boolean plt;
1376 bfd_boolean relocs;
1378 dynobj = elf_hash_table (info)->dynobj;
1379 BFD_ASSERT (dynobj != NULL);
1381 if (elf_hash_table (info)->dynamic_sections_created)
1383 /* Set the contents of the .interp section to the interpreter. */
1384 if (info->executable)
1386 s = bfd_get_section_by_name (dynobj, ".interp");
1387 BFD_ASSERT (s != NULL);
1388 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1389 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1392 else
1394 /* We may have created entries in the .rela.got section.
1395 However, if we are not creating the dynamic sections, we will
1396 not actually use these entries. Reset the size of .rela.got,
1397 which will cause it to get stripped from the output file
1398 below. */
1399 s = bfd_get_section_by_name (dynobj, ".rela.got");
1400 if (s != NULL)
1401 s->size = 0;
1404 /* If this is a -Bsymbolic shared link, then we need to discard all
1405 PC relative relocs against symbols defined in a regular object.
1406 For the normal shared case we discard the PC relative relocs
1407 against symbols that have become local due to visibility changes.
1408 We allocated space for them in the check_relocs routine, but we
1409 will not fill them in in the relocate_section routine. */
1410 if (info->shared)
1411 elf_link_hash_traverse (elf_hash_table (info),
1412 elf_m68k_discard_copies,
1413 (PTR) info);
1415 /* The check_relocs and adjust_dynamic_symbol entry points have
1416 determined the sizes of the various dynamic sections. Allocate
1417 memory for them. */
1418 plt = FALSE;
1419 relocs = FALSE;
1420 for (s = dynobj->sections; s != NULL; s = s->next)
1422 const char *name;
1424 if ((s->flags & SEC_LINKER_CREATED) == 0)
1425 continue;
1427 /* It's OK to base decisions on the section name, because none
1428 of the dynobj section names depend upon the input files. */
1429 name = bfd_get_section_name (dynobj, s);
1431 if (strcmp (name, ".plt") == 0)
1433 /* Remember whether there is a PLT. */
1434 plt = s->size != 0;
1436 else if (CONST_STRNEQ (name, ".rela"))
1438 if (s->size != 0)
1440 relocs = TRUE;
1442 /* We use the reloc_count field as a counter if we need
1443 to copy relocs into the output file. */
1444 s->reloc_count = 0;
1447 else if (! CONST_STRNEQ (name, ".got")
1448 && strcmp (name, ".dynbss") != 0)
1450 /* It's not one of our sections, so don't allocate space. */
1451 continue;
1454 if (s->size == 0)
1456 /* If we don't need this section, strip it from the
1457 output file. This is mostly to handle .rela.bss and
1458 .rela.plt. We must create both sections in
1459 create_dynamic_sections, because they must be created
1460 before the linker maps input sections to output
1461 sections. The linker does that before
1462 adjust_dynamic_symbol is called, and it is that
1463 function which decides whether anything needs to go
1464 into these sections. */
1465 s->flags |= SEC_EXCLUDE;
1466 continue;
1469 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1470 continue;
1472 /* Allocate memory for the section contents. */
1473 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1474 Unused entries should be reclaimed before the section's contents
1475 are written out, but at the moment this does not happen. Thus in
1476 order to prevent writing out garbage, we initialise the section's
1477 contents to zero. */
1478 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1479 if (s->contents == NULL)
1480 return FALSE;
1483 if (elf_hash_table (info)->dynamic_sections_created)
1485 /* Add some entries to the .dynamic section. We fill in the
1486 values later, in elf_m68k_finish_dynamic_sections, but we
1487 must add the entries now so that we get the correct size for
1488 the .dynamic section. The DT_DEBUG entry is filled in by the
1489 dynamic linker and used by the debugger. */
1490 #define add_dynamic_entry(TAG, VAL) \
1491 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1493 if (!info->shared)
1495 if (!add_dynamic_entry (DT_DEBUG, 0))
1496 return FALSE;
1499 if (plt)
1501 if (!add_dynamic_entry (DT_PLTGOT, 0)
1502 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1503 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1504 || !add_dynamic_entry (DT_JMPREL, 0))
1505 return FALSE;
1508 if (relocs)
1510 if (!add_dynamic_entry (DT_RELA, 0)
1511 || !add_dynamic_entry (DT_RELASZ, 0)
1512 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1513 return FALSE;
1516 if ((info->flags & DF_TEXTREL) != 0)
1518 if (!add_dynamic_entry (DT_TEXTREL, 0))
1519 return FALSE;
1522 #undef add_dynamic_entry
1524 return TRUE;
1527 /* This function is called via elf_link_hash_traverse if we are
1528 creating a shared object. In the -Bsymbolic case it discards the
1529 space allocated to copy PC relative relocs against symbols which
1530 are defined in regular objects. For the normal shared case, it
1531 discards space for pc-relative relocs that have become local due to
1532 symbol visibility changes. We allocated space for them in the
1533 check_relocs routine, but we won't fill them in in the
1534 relocate_section routine.
1536 We also check whether any of the remaining relocations apply
1537 against a readonly section, and set the DF_TEXTREL flag in this
1538 case. */
1540 static bfd_boolean
1541 elf_m68k_discard_copies (h, inf)
1542 struct elf_link_hash_entry *h;
1543 PTR inf;
1545 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1546 struct elf_m68k_pcrel_relocs_copied *s;
1548 if (h->root.type == bfd_link_hash_warning)
1549 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1551 if (!h->def_regular
1552 || (!info->symbolic
1553 && !h->forced_local))
1555 if ((info->flags & DF_TEXTREL) == 0)
1557 /* Look for relocations against read-only sections. */
1558 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1559 s != NULL;
1560 s = s->next)
1561 if ((s->section->flags & SEC_READONLY) != 0)
1563 info->flags |= DF_TEXTREL;
1564 break;
1568 return TRUE;
1571 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1572 s != NULL;
1573 s = s->next)
1574 s->section->size -= s->count * sizeof (Elf32_External_Rela);
1576 return TRUE;
1579 /* Relocate an M68K ELF section. */
1581 static bfd_boolean
1582 elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1583 contents, relocs, local_syms, local_sections)
1584 bfd *output_bfd;
1585 struct bfd_link_info *info;
1586 bfd *input_bfd;
1587 asection *input_section;
1588 bfd_byte *contents;
1589 Elf_Internal_Rela *relocs;
1590 Elf_Internal_Sym *local_syms;
1591 asection **local_sections;
1593 bfd *dynobj;
1594 Elf_Internal_Shdr *symtab_hdr;
1595 struct elf_link_hash_entry **sym_hashes;
1596 bfd_vma *local_got_offsets;
1597 asection *sgot;
1598 asection *splt;
1599 asection *sreloc;
1600 Elf_Internal_Rela *rel;
1601 Elf_Internal_Rela *relend;
1603 dynobj = elf_hash_table (info)->dynobj;
1604 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1605 sym_hashes = elf_sym_hashes (input_bfd);
1606 local_got_offsets = elf_local_got_offsets (input_bfd);
1608 sgot = NULL;
1609 splt = NULL;
1610 sreloc = NULL;
1612 rel = relocs;
1613 relend = relocs + input_section->reloc_count;
1614 for (; rel < relend; rel++)
1616 int r_type;
1617 reloc_howto_type *howto;
1618 unsigned long r_symndx;
1619 struct elf_link_hash_entry *h;
1620 Elf_Internal_Sym *sym;
1621 asection *sec;
1622 bfd_vma relocation;
1623 bfd_boolean unresolved_reloc;
1624 bfd_reloc_status_type r;
1626 r_type = ELF32_R_TYPE (rel->r_info);
1627 if (r_type < 0 || r_type >= (int) R_68K_max)
1629 bfd_set_error (bfd_error_bad_value);
1630 return FALSE;
1632 howto = howto_table + r_type;
1634 r_symndx = ELF32_R_SYM (rel->r_info);
1636 h = NULL;
1637 sym = NULL;
1638 sec = NULL;
1639 unresolved_reloc = FALSE;
1641 if (r_symndx < symtab_hdr->sh_info)
1643 sym = local_syms + r_symndx;
1644 sec = local_sections[r_symndx];
1645 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1647 else
1649 bfd_boolean warned;
1651 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1652 r_symndx, symtab_hdr, sym_hashes,
1653 h, sec, relocation,
1654 unresolved_reloc, warned);
1657 if (sec != NULL && elf_discarded_section (sec))
1659 /* For relocs against symbols from removed linkonce sections,
1660 or sections discarded by a linker script, we just want the
1661 section contents zeroed. Avoid any special processing. */
1662 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1663 rel->r_info = 0;
1664 rel->r_addend = 0;
1665 continue;
1668 if (info->relocatable)
1669 continue;
1671 switch (r_type)
1673 case R_68K_GOT8:
1674 case R_68K_GOT16:
1675 case R_68K_GOT32:
1676 /* Relocation is to the address of the entry for this symbol
1677 in the global offset table. */
1678 if (h != NULL
1679 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1680 break;
1681 /* Fall through. */
1682 case R_68K_GOT8O:
1683 case R_68K_GOT16O:
1684 case R_68K_GOT32O:
1685 /* Relocation is the offset of the entry for this symbol in
1686 the global offset table. */
1689 bfd_vma off;
1691 if (sgot == NULL)
1693 sgot = bfd_get_section_by_name (dynobj, ".got");
1694 BFD_ASSERT (sgot != NULL);
1697 if (h != NULL)
1699 bfd_boolean dyn;
1701 off = h->got.offset;
1702 BFD_ASSERT (off != (bfd_vma) -1);
1704 dyn = elf_hash_table (info)->dynamic_sections_created;
1705 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1706 || (info->shared
1707 && (info->symbolic
1708 || h->dynindx == -1
1709 || h->forced_local)
1710 && h->def_regular))
1712 /* This is actually a static link, or it is a
1713 -Bsymbolic link and the symbol is defined
1714 locally, or the symbol was forced to be local
1715 because of a version file.. We must initialize
1716 this entry in the global offset table. Since
1717 the offset must always be a multiple of 4, we
1718 use the least significant bit to record whether
1719 we have initialized it already.
1721 When doing a dynamic link, we create a .rela.got
1722 relocation entry to initialize the value. This
1723 is done in the finish_dynamic_symbol routine. */
1724 if ((off & 1) != 0)
1725 off &= ~1;
1726 else
1728 bfd_put_32 (output_bfd, relocation,
1729 sgot->contents + off);
1730 h->got.offset |= 1;
1733 else
1734 unresolved_reloc = FALSE;
1736 else
1738 BFD_ASSERT (local_got_offsets != NULL
1739 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1741 off = local_got_offsets[r_symndx];
1743 /* The offset must always be a multiple of 4. We use
1744 the least significant bit to record whether we have
1745 already generated the necessary reloc. */
1746 if ((off & 1) != 0)
1747 off &= ~1;
1748 else
1750 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1752 if (info->shared)
1754 asection *s;
1755 Elf_Internal_Rela outrel;
1756 bfd_byte *loc;
1758 s = bfd_get_section_by_name (dynobj, ".rela.got");
1759 BFD_ASSERT (s != NULL);
1761 outrel.r_offset = (sgot->output_section->vma
1762 + sgot->output_offset
1763 + off);
1764 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1765 outrel.r_addend = relocation;
1766 loc = s->contents;
1767 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1768 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1771 local_got_offsets[r_symndx] |= 1;
1775 relocation = sgot->output_offset + off;
1776 if (r_type == R_68K_GOT8O
1777 || r_type == R_68K_GOT16O
1778 || r_type == R_68K_GOT32O)
1780 /* This relocation does not use the addend. */
1781 rel->r_addend = 0;
1783 else
1784 relocation += sgot->output_section->vma;
1786 break;
1788 case R_68K_PLT8:
1789 case R_68K_PLT16:
1790 case R_68K_PLT32:
1791 /* Relocation is to the entry for this symbol in the
1792 procedure linkage table. */
1794 /* Resolve a PLTxx reloc against a local symbol directly,
1795 without using the procedure linkage table. */
1796 if (h == NULL)
1797 break;
1799 if (h->plt.offset == (bfd_vma) -1
1800 || !elf_hash_table (info)->dynamic_sections_created)
1802 /* We didn't make a PLT entry for this symbol. This
1803 happens when statically linking PIC code, or when
1804 using -Bsymbolic. */
1805 break;
1808 if (splt == NULL)
1810 splt = bfd_get_section_by_name (dynobj, ".plt");
1811 BFD_ASSERT (splt != NULL);
1814 relocation = (splt->output_section->vma
1815 + splt->output_offset
1816 + h->plt.offset);
1817 unresolved_reloc = FALSE;
1818 break;
1820 case R_68K_PLT8O:
1821 case R_68K_PLT16O:
1822 case R_68K_PLT32O:
1823 /* Relocation is the offset of the entry for this symbol in
1824 the procedure linkage table. */
1825 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1827 if (splt == NULL)
1829 splt = bfd_get_section_by_name (dynobj, ".plt");
1830 BFD_ASSERT (splt != NULL);
1833 relocation = h->plt.offset;
1834 unresolved_reloc = FALSE;
1836 /* This relocation does not use the addend. */
1837 rel->r_addend = 0;
1839 break;
1841 case R_68K_PC8:
1842 case R_68K_PC16:
1843 case R_68K_PC32:
1844 if (h == NULL
1845 || (info->shared
1846 && h->forced_local))
1847 break;
1848 /* Fall through. */
1849 case R_68K_8:
1850 case R_68K_16:
1851 case R_68K_32:
1852 if (info->shared
1853 && r_symndx != 0
1854 && (input_section->flags & SEC_ALLOC) != 0
1855 && (h == NULL
1856 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1857 || h->root.type != bfd_link_hash_undefweak)
1858 && ((r_type != R_68K_PC8
1859 && r_type != R_68K_PC16
1860 && r_type != R_68K_PC32)
1861 || (h != NULL
1862 && h->dynindx != -1
1863 && (!info->symbolic
1864 || !h->def_regular))))
1866 Elf_Internal_Rela outrel;
1867 bfd_byte *loc;
1868 bfd_boolean skip, relocate;
1870 /* When generating a shared object, these relocations
1871 are copied into the output file to be resolved at run
1872 time. */
1874 skip = FALSE;
1875 relocate = FALSE;
1877 outrel.r_offset =
1878 _bfd_elf_section_offset (output_bfd, info, input_section,
1879 rel->r_offset);
1880 if (outrel.r_offset == (bfd_vma) -1)
1881 skip = TRUE;
1882 else if (outrel.r_offset == (bfd_vma) -2)
1883 skip = TRUE, relocate = TRUE;
1884 outrel.r_offset += (input_section->output_section->vma
1885 + input_section->output_offset);
1887 if (skip)
1888 memset (&outrel, 0, sizeof outrel);
1889 else if (h != NULL
1890 && h->dynindx != -1
1891 && (r_type == R_68K_PC8
1892 || r_type == R_68K_PC16
1893 || r_type == R_68K_PC32
1894 || !info->shared
1895 || !info->symbolic
1896 || !h->def_regular))
1898 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1899 outrel.r_addend = rel->r_addend;
1901 else
1903 /* This symbol is local, or marked to become local. */
1904 outrel.r_addend = relocation + rel->r_addend;
1906 if (r_type == R_68K_32)
1908 relocate = TRUE;
1909 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1911 else
1913 long indx;
1915 if (bfd_is_abs_section (sec))
1916 indx = 0;
1917 else if (sec == NULL || sec->owner == NULL)
1919 bfd_set_error (bfd_error_bad_value);
1920 return FALSE;
1922 else
1924 asection *osec;
1926 /* We are turning this relocation into one
1927 against a section symbol. It would be
1928 proper to subtract the symbol's value,
1929 osec->vma, from the emitted reloc addend,
1930 but ld.so expects buggy relocs. */
1931 osec = sec->output_section;
1932 indx = elf_section_data (osec)->dynindx;
1933 if (indx == 0)
1935 struct elf_link_hash_table *htab;
1936 htab = elf_hash_table (info);
1937 osec = htab->text_index_section;
1938 indx = elf_section_data (osec)->dynindx;
1940 BFD_ASSERT (indx != 0);
1943 outrel.r_info = ELF32_R_INFO (indx, r_type);
1947 sreloc = elf_section_data (input_section)->sreloc;
1948 if (sreloc == NULL)
1949 abort ();
1951 loc = sreloc->contents;
1952 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1953 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1955 /* This reloc will be computed at runtime, so there's no
1956 need to do anything now, except for R_68K_32
1957 relocations that have been turned into
1958 R_68K_RELATIVE. */
1959 if (!relocate)
1960 continue;
1963 break;
1965 case R_68K_GNU_VTINHERIT:
1966 case R_68K_GNU_VTENTRY:
1967 /* These are no-ops in the end. */
1968 continue;
1970 default:
1971 break;
1974 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1975 because such sections are not SEC_ALLOC and thus ld.so will
1976 not process them. */
1977 if (unresolved_reloc
1978 && !((input_section->flags & SEC_DEBUGGING) != 0
1979 && h->def_dynamic))
1981 (*_bfd_error_handler)
1982 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1983 input_bfd,
1984 input_section,
1985 (long) rel->r_offset,
1986 howto->name,
1987 h->root.root.string);
1988 return FALSE;
1991 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1992 contents, rel->r_offset,
1993 relocation, rel->r_addend);
1995 if (r != bfd_reloc_ok)
1997 const char *name;
1999 if (h != NULL)
2000 name = h->root.root.string;
2001 else
2003 name = bfd_elf_string_from_elf_section (input_bfd,
2004 symtab_hdr->sh_link,
2005 sym->st_name);
2006 if (name == NULL)
2007 return FALSE;
2008 if (*name == '\0')
2009 name = bfd_section_name (input_bfd, sec);
2012 if (r == bfd_reloc_overflow)
2014 if (!(info->callbacks->reloc_overflow
2015 (info, (h ? &h->root : NULL), name, howto->name,
2016 (bfd_vma) 0, input_bfd, input_section,
2017 rel->r_offset)))
2018 return FALSE;
2020 else
2022 (*_bfd_error_handler)
2023 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2024 input_bfd, input_section,
2025 (long) rel->r_offset, name, (int) r);
2026 return FALSE;
2031 return TRUE;
2034 /* Install an M_68K_PC32 relocation against VALUE at offset OFFSET
2035 into section SEC. */
2037 static void
2038 elf_m68k_install_pc32 (asection *sec, bfd_vma offset, bfd_vma value)
2040 /* Make VALUE PC-relative. */
2041 value -= sec->output_section->vma + offset;
2043 /* Apply any in-place addend. */
2044 value += bfd_get_32 (sec->owner, sec->contents + offset);
2046 bfd_put_32 (sec->owner, value, sec->contents + offset);
2049 /* Finish up dynamic symbol handling. We set the contents of various
2050 dynamic sections here. */
2052 static bfd_boolean
2053 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
2054 bfd *output_bfd;
2055 struct bfd_link_info *info;
2056 struct elf_link_hash_entry *h;
2057 Elf_Internal_Sym *sym;
2059 bfd *dynobj;
2061 dynobj = elf_hash_table (info)->dynobj;
2063 if (h->plt.offset != (bfd_vma) -1)
2065 const struct elf_m68k_plt_info *plt_info;
2066 asection *splt;
2067 asection *sgot;
2068 asection *srela;
2069 bfd_vma plt_index;
2070 bfd_vma got_offset;
2071 Elf_Internal_Rela rela;
2072 bfd_byte *loc;
2074 /* This symbol has an entry in the procedure linkage table. Set
2075 it up. */
2077 BFD_ASSERT (h->dynindx != -1);
2079 plt_info = elf_m68k_hash_table (info)->plt_info;
2080 splt = bfd_get_section_by_name (dynobj, ".plt");
2081 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2082 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2083 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
2085 /* Get the index in the procedure linkage table which
2086 corresponds to this symbol. This is the index of this symbol
2087 in all the symbols for which we are making plt entries. The
2088 first entry in the procedure linkage table is reserved. */
2089 plt_index = (h->plt.offset / plt_info->size) - 1;
2091 /* Get the offset into the .got table of the entry that
2092 corresponds to this function. Each .got entry is 4 bytes.
2093 The first three are reserved. */
2094 got_offset = (plt_index + 3) * 4;
2096 memcpy (splt->contents + h->plt.offset,
2097 plt_info->symbol_entry,
2098 plt_info->size);
2100 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.got,
2101 (sgot->output_section->vma
2102 + sgot->output_offset
2103 + got_offset));
2105 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
2106 splt->contents
2107 + h->plt.offset
2108 + plt_info->symbol_resolve_entry + 2);
2110 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.plt,
2111 splt->output_section->vma);
2113 /* Fill in the entry in the global offset table. */
2114 bfd_put_32 (output_bfd,
2115 (splt->output_section->vma
2116 + splt->output_offset
2117 + h->plt.offset
2118 + plt_info->symbol_resolve_entry),
2119 sgot->contents + got_offset);
2121 /* Fill in the entry in the .rela.plt section. */
2122 rela.r_offset = (sgot->output_section->vma
2123 + sgot->output_offset
2124 + got_offset);
2125 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
2126 rela.r_addend = 0;
2127 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
2128 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2130 if (!h->def_regular)
2132 /* Mark the symbol as undefined, rather than as defined in
2133 the .plt section. Leave the value alone. */
2134 sym->st_shndx = SHN_UNDEF;
2138 if (h->got.offset != (bfd_vma) -1)
2140 asection *sgot;
2141 asection *srela;
2142 Elf_Internal_Rela rela;
2143 bfd_byte *loc;
2145 /* This symbol has an entry in the global offset table. Set it
2146 up. */
2148 sgot = bfd_get_section_by_name (dynobj, ".got");
2149 srela = bfd_get_section_by_name (dynobj, ".rela.got");
2150 BFD_ASSERT (sgot != NULL && srela != NULL);
2152 rela.r_offset = (sgot->output_section->vma
2153 + sgot->output_offset
2154 + (h->got.offset &~ (bfd_vma) 1));
2156 /* If this is a -Bsymbolic link, and the symbol is defined
2157 locally, we just want to emit a RELATIVE reloc. Likewise if
2158 the symbol was forced to be local because of a version file.
2159 The entry in the global offset table will already have been
2160 initialized in the relocate_section function. */
2161 if (info->shared
2162 && (info->symbolic
2163 || h->dynindx == -1
2164 || h->forced_local)
2165 && h->def_regular)
2167 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
2168 rela.r_addend = bfd_get_signed_32 (output_bfd,
2169 (sgot->contents
2170 + (h->got.offset &~ (bfd_vma) 1)));
2172 else
2174 bfd_put_32 (output_bfd, (bfd_vma) 0,
2175 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
2176 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
2177 rela.r_addend = 0;
2180 loc = srela->contents;
2181 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2182 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2185 if (h->needs_copy)
2187 asection *s;
2188 Elf_Internal_Rela rela;
2189 bfd_byte *loc;
2191 /* This symbol needs a copy reloc. Set it up. */
2193 BFD_ASSERT (h->dynindx != -1
2194 && (h->root.type == bfd_link_hash_defined
2195 || h->root.type == bfd_link_hash_defweak));
2197 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2198 ".rela.bss");
2199 BFD_ASSERT (s != NULL);
2201 rela.r_offset = (h->root.u.def.value
2202 + h->root.u.def.section->output_section->vma
2203 + h->root.u.def.section->output_offset);
2204 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
2205 rela.r_addend = 0;
2206 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2207 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2210 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2211 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2212 || h == elf_hash_table (info)->hgot)
2213 sym->st_shndx = SHN_ABS;
2215 return TRUE;
2218 /* Finish up the dynamic sections. */
2220 static bfd_boolean
2221 elf_m68k_finish_dynamic_sections (output_bfd, info)
2222 bfd *output_bfd;
2223 struct bfd_link_info *info;
2225 bfd *dynobj;
2226 asection *sgot;
2227 asection *sdyn;
2229 dynobj = elf_hash_table (info)->dynobj;
2231 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2232 BFD_ASSERT (sgot != NULL);
2233 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2235 if (elf_hash_table (info)->dynamic_sections_created)
2237 asection *splt;
2238 Elf32_External_Dyn *dyncon, *dynconend;
2240 splt = bfd_get_section_by_name (dynobj, ".plt");
2241 BFD_ASSERT (splt != NULL && sdyn != NULL);
2243 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2244 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2245 for (; dyncon < dynconend; dyncon++)
2247 Elf_Internal_Dyn dyn;
2248 const char *name;
2249 asection *s;
2251 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2253 switch (dyn.d_tag)
2255 default:
2256 break;
2258 case DT_PLTGOT:
2259 name = ".got";
2260 goto get_vma;
2261 case DT_JMPREL:
2262 name = ".rela.plt";
2263 get_vma:
2264 s = bfd_get_section_by_name (output_bfd, name);
2265 BFD_ASSERT (s != NULL);
2266 dyn.d_un.d_ptr = s->vma;
2267 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2268 break;
2270 case DT_PLTRELSZ:
2271 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2272 BFD_ASSERT (s != NULL);
2273 dyn.d_un.d_val = s->size;
2274 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2275 break;
2277 case DT_RELASZ:
2278 /* The procedure linkage table relocs (DT_JMPREL) should
2279 not be included in the overall relocs (DT_RELA).
2280 Therefore, we override the DT_RELASZ entry here to
2281 make it not include the JMPREL relocs. Since the
2282 linker script arranges for .rela.plt to follow all
2283 other relocation sections, we don't have to worry
2284 about changing the DT_RELA entry. */
2285 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2286 if (s != NULL)
2287 dyn.d_un.d_val -= s->size;
2288 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2289 break;
2293 /* Fill in the first entry in the procedure linkage table. */
2294 if (splt->size > 0)
2296 const struct elf_m68k_plt_info *plt_info;
2298 plt_info = elf_m68k_hash_table (info)->plt_info;
2299 memcpy (splt->contents, plt_info->plt0_entry, plt_info->size);
2301 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got4,
2302 (sgot->output_section->vma
2303 + sgot->output_offset
2304 + 4));
2306 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got8,
2307 (sgot->output_section->vma
2308 + sgot->output_offset
2309 + 8));
2311 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2312 = plt_info->size;
2316 /* Fill in the first three entries in the global offset table. */
2317 if (sgot->size > 0)
2319 if (sdyn == NULL)
2320 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2321 else
2322 bfd_put_32 (output_bfd,
2323 sdyn->output_section->vma + sdyn->output_offset,
2324 sgot->contents);
2325 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2326 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2329 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2331 return TRUE;
2334 /* Given a .data section and a .emreloc in-memory section, store
2335 relocation information into the .emreloc section which can be
2336 used at runtime to relocate the section. This is called by the
2337 linker when the --embedded-relocs switch is used. This is called
2338 after the add_symbols entry point has been called for all the
2339 objects, and before the final_link entry point is called. */
2341 bfd_boolean
2342 bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2343 bfd *abfd;
2344 struct bfd_link_info *info;
2345 asection *datasec;
2346 asection *relsec;
2347 char **errmsg;
2349 Elf_Internal_Shdr *symtab_hdr;
2350 Elf_Internal_Sym *isymbuf = NULL;
2351 Elf_Internal_Rela *internal_relocs = NULL;
2352 Elf_Internal_Rela *irel, *irelend;
2353 bfd_byte *p;
2354 bfd_size_type amt;
2356 BFD_ASSERT (! info->relocatable);
2358 *errmsg = NULL;
2360 if (datasec->reloc_count == 0)
2361 return TRUE;
2363 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2365 /* Get a copy of the native relocations. */
2366 internal_relocs = (_bfd_elf_link_read_relocs
2367 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2368 info->keep_memory));
2369 if (internal_relocs == NULL)
2370 goto error_return;
2372 amt = (bfd_size_type) datasec->reloc_count * 12;
2373 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
2374 if (relsec->contents == NULL)
2375 goto error_return;
2377 p = relsec->contents;
2379 irelend = internal_relocs + datasec->reloc_count;
2380 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2382 asection *targetsec;
2384 /* We are going to write a four byte longword into the runtime
2385 reloc section. The longword will be the address in the data
2386 section which must be relocated. It is followed by the name
2387 of the target section NUL-padded or truncated to 8
2388 characters. */
2390 /* We can only relocate absolute longword relocs at run time. */
2391 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2393 *errmsg = _("unsupported reloc type");
2394 bfd_set_error (bfd_error_bad_value);
2395 goto error_return;
2398 /* Get the target section referred to by the reloc. */
2399 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2401 /* A local symbol. */
2402 Elf_Internal_Sym *isym;
2404 /* Read this BFD's local symbols if we haven't done so already. */
2405 if (isymbuf == NULL)
2407 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2408 if (isymbuf == NULL)
2409 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2410 symtab_hdr->sh_info, 0,
2411 NULL, NULL, NULL);
2412 if (isymbuf == NULL)
2413 goto error_return;
2416 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2417 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2419 else
2421 unsigned long indx;
2422 struct elf_link_hash_entry *h;
2424 /* An external symbol. */
2425 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2426 h = elf_sym_hashes (abfd)[indx];
2427 BFD_ASSERT (h != NULL);
2428 if (h->root.type == bfd_link_hash_defined
2429 || h->root.type == bfd_link_hash_defweak)
2430 targetsec = h->root.u.def.section;
2431 else
2432 targetsec = NULL;
2435 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2436 memset (p + 4, 0, 8);
2437 if (targetsec != NULL)
2438 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
2441 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2442 free (isymbuf);
2443 if (internal_relocs != NULL
2444 && elf_section_data (datasec)->relocs != internal_relocs)
2445 free (internal_relocs);
2446 return TRUE;
2448 error_return:
2449 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2450 free (isymbuf);
2451 if (internal_relocs != NULL
2452 && elf_section_data (datasec)->relocs != internal_relocs)
2453 free (internal_relocs);
2454 return FALSE;
2457 static enum elf_reloc_type_class
2458 elf32_m68k_reloc_type_class (rela)
2459 const Elf_Internal_Rela *rela;
2461 switch ((int) ELF32_R_TYPE (rela->r_info))
2463 case R_68K_RELATIVE:
2464 return reloc_class_relative;
2465 case R_68K_JMP_SLOT:
2466 return reloc_class_plt;
2467 case R_68K_COPY:
2468 return reloc_class_copy;
2469 default:
2470 return reloc_class_normal;
2474 /* Return address for Ith PLT stub in section PLT, for relocation REL
2475 or (bfd_vma) -1 if it should not be included. */
2477 static bfd_vma
2478 elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
2479 const arelent *rel ATTRIBUTE_UNUSED)
2481 return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
2484 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
2485 #define TARGET_BIG_NAME "elf32-m68k"
2486 #define ELF_MACHINE_CODE EM_68K
2487 #define ELF_MAXPAGESIZE 0x2000
2488 #define elf_backend_create_dynamic_sections \
2489 _bfd_elf_create_dynamic_sections
2490 #define bfd_elf32_bfd_link_hash_table_create \
2491 elf_m68k_link_hash_table_create
2492 #define bfd_elf32_bfd_final_link bfd_elf_gc_common_final_link
2494 #define elf_backend_check_relocs elf_m68k_check_relocs
2495 #define elf_backend_always_size_sections \
2496 elf_m68k_always_size_sections
2497 #define elf_backend_adjust_dynamic_symbol \
2498 elf_m68k_adjust_dynamic_symbol
2499 #define elf_backend_size_dynamic_sections \
2500 elf_m68k_size_dynamic_sections
2501 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
2502 #define elf_backend_relocate_section elf_m68k_relocate_section
2503 #define elf_backend_finish_dynamic_symbol \
2504 elf_m68k_finish_dynamic_symbol
2505 #define elf_backend_finish_dynamic_sections \
2506 elf_m68k_finish_dynamic_sections
2507 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
2508 #define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
2509 #define bfd_elf32_bfd_merge_private_bfd_data \
2510 elf32_m68k_merge_private_bfd_data
2511 #define bfd_elf32_bfd_set_private_flags \
2512 elf32_m68k_set_private_flags
2513 #define bfd_elf32_bfd_print_private_bfd_data \
2514 elf32_m68k_print_private_bfd_data
2515 #define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
2516 #define elf_backend_plt_sym_val elf_m68k_plt_sym_val
2517 #define elf_backend_object_p elf32_m68k_object_p
2519 #define elf_backend_can_gc_sections 1
2520 #define elf_backend_can_refcount 1
2521 #define elf_backend_want_got_plt 1
2522 #define elf_backend_plt_readonly 1
2523 #define elf_backend_want_plt_sym 0
2524 #define elf_backend_got_header_size 12
2525 #define elf_backend_rela_normal 1
2527 #include "elf32-target.h"