* configure.tgt (i[3-7]86-*-solaris2*, i[3-7]86-*-solaris*): Correct
[binutils.git] / bfd / elf32-m68k.c
blobe8bb275f6faef89bee20824fe8bd29bd3cdf7dc3
1 /* Motorola 68k series support for 32-bit ELF
2 Copyright 1993, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/m68k.h"
27 #include "opcode/m68k.h"
29 static reloc_howto_type *reloc_type_lookup
30 PARAMS ((bfd *, bfd_reloc_code_real_type));
31 static void rtype_to_howto
32 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
33 static struct bfd_hash_entry *elf_m68k_link_hash_newfunc
34 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
35 static struct bfd_link_hash_table *elf_m68k_link_hash_table_create
36 PARAMS ((bfd *));
37 static bfd_boolean elf_m68k_check_relocs
38 PARAMS ((bfd *, struct bfd_link_info *, asection *,
39 const Elf_Internal_Rela *));
40 static asection *elf_m68k_gc_mark_hook
41 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
42 struct elf_link_hash_entry *, Elf_Internal_Sym *));
43 static bfd_boolean elf_m68k_gc_sweep_hook
44 PARAMS ((bfd *, struct bfd_link_info *, asection *,
45 const Elf_Internal_Rela *));
46 static bfd_boolean elf_m68k_adjust_dynamic_symbol
47 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
48 static bfd_boolean elf_m68k_size_dynamic_sections
49 PARAMS ((bfd *, struct bfd_link_info *));
50 static bfd_boolean elf_m68k_discard_copies
51 PARAMS ((struct elf_link_hash_entry *, PTR));
52 static bfd_boolean elf_m68k_relocate_section
53 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
54 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
55 static bfd_boolean elf_m68k_finish_dynamic_symbol
56 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
57 Elf_Internal_Sym *));
58 static bfd_boolean elf_m68k_finish_dynamic_sections
59 PARAMS ((bfd *, struct bfd_link_info *));
61 static bfd_boolean elf32_m68k_set_private_flags
62 PARAMS ((bfd *, flagword));
63 static bfd_boolean elf32_m68k_merge_private_bfd_data
64 PARAMS ((bfd *, bfd *));
65 static bfd_boolean elf32_m68k_print_private_bfd_data
66 PARAMS ((bfd *, PTR));
67 static enum elf_reloc_type_class elf32_m68k_reloc_type_class
68 PARAMS ((const Elf_Internal_Rela *));
70 static reloc_howto_type howto_table[] = {
71 HOWTO(R_68K_NONE, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_NONE", FALSE, 0, 0x00000000,FALSE),
72 HOWTO(R_68K_32, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_32", FALSE, 0, 0xffffffff,FALSE),
73 HOWTO(R_68K_16, 0, 1,16, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_16", FALSE, 0, 0x0000ffff,FALSE),
74 HOWTO(R_68K_8, 0, 0, 8, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_8", FALSE, 0, 0x000000ff,FALSE),
75 HOWTO(R_68K_PC32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PC32", FALSE, 0, 0xffffffff,TRUE),
76 HOWTO(R_68K_PC16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC16", FALSE, 0, 0x0000ffff,TRUE),
77 HOWTO(R_68K_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PC8", FALSE, 0, 0x000000ff,TRUE),
78 HOWTO(R_68K_GOT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32", FALSE, 0, 0xffffffff,TRUE),
79 HOWTO(R_68K_GOT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16", FALSE, 0, 0x0000ffff,TRUE),
80 HOWTO(R_68K_GOT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8", FALSE, 0, 0x000000ff,TRUE),
81 HOWTO(R_68K_GOT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_GOT32O", FALSE, 0, 0xffffffff,FALSE),
82 HOWTO(R_68K_GOT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT16O", FALSE, 0, 0x0000ffff,FALSE),
83 HOWTO(R_68K_GOT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_GOT8O", FALSE, 0, 0x000000ff,FALSE),
84 HOWTO(R_68K_PLT32, 0, 2,32, TRUE, 0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32", FALSE, 0, 0xffffffff,TRUE),
85 HOWTO(R_68K_PLT16, 0, 1,16, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16", FALSE, 0, 0x0000ffff,TRUE),
86 HOWTO(R_68K_PLT8, 0, 0, 8, TRUE, 0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8", FALSE, 0, 0x000000ff,TRUE),
87 HOWTO(R_68K_PLT32O, 0, 2,32, FALSE,0, complain_overflow_bitfield, bfd_elf_generic_reloc, "R_68K_PLT32O", FALSE, 0, 0xffffffff,FALSE),
88 HOWTO(R_68K_PLT16O, 0, 1,16, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT16O", FALSE, 0, 0x0000ffff,FALSE),
89 HOWTO(R_68K_PLT8O, 0, 0, 8, FALSE,0, complain_overflow_signed, bfd_elf_generic_reloc, "R_68K_PLT8O", FALSE, 0, 0x000000ff,FALSE),
90 HOWTO(R_68K_COPY, 0, 0, 0, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_COPY", FALSE, 0, 0xffffffff,FALSE),
91 HOWTO(R_68K_GLOB_DAT, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_GLOB_DAT", FALSE, 0, 0xffffffff,FALSE),
92 HOWTO(R_68K_JMP_SLOT, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_JMP_SLOT", FALSE, 0, 0xffffffff,FALSE),
93 HOWTO(R_68K_RELATIVE, 0, 2,32, FALSE,0, complain_overflow_dont, bfd_elf_generic_reloc, "R_68K_RELATIVE", FALSE, 0, 0xffffffff,FALSE),
94 /* GNU extension to record C++ vtable hierarchy. */
95 HOWTO (R_68K_GNU_VTINHERIT, /* type */
96 0, /* rightshift */
97 2, /* size (0 = byte, 1 = short, 2 = long) */
98 0, /* bitsize */
99 FALSE, /* pc_relative */
100 0, /* bitpos */
101 complain_overflow_dont, /* complain_on_overflow */
102 NULL, /* special_function */
103 "R_68K_GNU_VTINHERIT", /* name */
104 FALSE, /* partial_inplace */
105 0, /* src_mask */
106 0, /* dst_mask */
107 FALSE),
108 /* GNU extension to record C++ vtable member usage. */
109 HOWTO (R_68K_GNU_VTENTRY, /* type */
110 0, /* rightshift */
111 2, /* size (0 = byte, 1 = short, 2 = long) */
112 0, /* bitsize */
113 FALSE, /* pc_relative */
114 0, /* bitpos */
115 complain_overflow_dont, /* complain_on_overflow */
116 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
117 "R_68K_GNU_VTENTRY", /* name */
118 FALSE, /* partial_inplace */
119 0, /* src_mask */
120 0, /* dst_mask */
121 FALSE),
124 static void
125 rtype_to_howto (abfd, cache_ptr, dst)
126 bfd *abfd ATTRIBUTE_UNUSED;
127 arelent *cache_ptr;
128 Elf_Internal_Rela *dst;
130 BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_68K_max);
131 cache_ptr->howto = &howto_table[ELF32_R_TYPE(dst->r_info)];
134 #define elf_info_to_howto rtype_to_howto
136 static const struct
138 bfd_reloc_code_real_type bfd_val;
139 int elf_val;
140 } reloc_map[] = {
141 { BFD_RELOC_NONE, R_68K_NONE },
142 { BFD_RELOC_32, R_68K_32 },
143 { BFD_RELOC_16, R_68K_16 },
144 { BFD_RELOC_8, R_68K_8 },
145 { BFD_RELOC_32_PCREL, R_68K_PC32 },
146 { BFD_RELOC_16_PCREL, R_68K_PC16 },
147 { BFD_RELOC_8_PCREL, R_68K_PC8 },
148 { BFD_RELOC_32_GOT_PCREL, R_68K_GOT32 },
149 { BFD_RELOC_16_GOT_PCREL, R_68K_GOT16 },
150 { BFD_RELOC_8_GOT_PCREL, R_68K_GOT8 },
151 { BFD_RELOC_32_GOTOFF, R_68K_GOT32O },
152 { BFD_RELOC_16_GOTOFF, R_68K_GOT16O },
153 { BFD_RELOC_8_GOTOFF, R_68K_GOT8O },
154 { BFD_RELOC_32_PLT_PCREL, R_68K_PLT32 },
155 { BFD_RELOC_16_PLT_PCREL, R_68K_PLT16 },
156 { BFD_RELOC_8_PLT_PCREL, R_68K_PLT8 },
157 { BFD_RELOC_32_PLTOFF, R_68K_PLT32O },
158 { BFD_RELOC_16_PLTOFF, R_68K_PLT16O },
159 { BFD_RELOC_8_PLTOFF, R_68K_PLT8O },
160 { BFD_RELOC_NONE, R_68K_COPY },
161 { BFD_RELOC_68K_GLOB_DAT, R_68K_GLOB_DAT },
162 { BFD_RELOC_68K_JMP_SLOT, R_68K_JMP_SLOT },
163 { BFD_RELOC_68K_RELATIVE, R_68K_RELATIVE },
164 { BFD_RELOC_CTOR, R_68K_32 },
165 { BFD_RELOC_VTABLE_INHERIT, R_68K_GNU_VTINHERIT },
166 { BFD_RELOC_VTABLE_ENTRY, R_68K_GNU_VTENTRY },
169 static reloc_howto_type *
170 reloc_type_lookup (abfd, code)
171 bfd *abfd ATTRIBUTE_UNUSED;
172 bfd_reloc_code_real_type code;
174 unsigned int i;
175 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
177 if (reloc_map[i].bfd_val == code)
178 return &howto_table[reloc_map[i].elf_val];
180 return 0;
183 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
184 #define ELF_ARCH bfd_arch_m68k
186 /* Functions for the m68k ELF linker. */
188 /* The name of the dynamic interpreter. This is put in the .interp
189 section. */
191 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
193 /* Describes one of the various PLT styles. */
195 struct elf_m68k_plt_info
197 /* The size of each PLT entry. */
198 bfd_vma size;
200 /* The template for the first PLT entry. */
201 const bfd_byte *plt0_entry;
203 /* Offsets of fields in PLT0_ENTRY that require R_68K_PC32 relocations.
204 The comments by each member indicate the value that the relocation
205 is against. */
206 struct {
207 unsigned int got4; /* .got + 4 */
208 unsigned int got8; /* .got + 8 */
209 } plt0_relocs;
211 /* The template for a symbol's PLT entry. */
212 const bfd_byte *symbol_entry;
214 /* Offsets of fields in SYMBOL_ENTRY that require R_68K_PC32 relocations.
215 The comments by each member indicate the value that the relocation
216 is against. */
217 struct {
218 unsigned int got; /* the symbol's .got.plt entry */
219 unsigned int plt; /* .plt */
220 } symbol_relocs;
222 /* The offset of the resolver stub from the start of SYMBOL_ENTRY.
223 The stub starts with "move.l #relocoffset,%d0". */
224 bfd_vma symbol_resolve_entry;
227 /* The size in bytes of an entry in the procedure linkage table. */
229 #define PLT_ENTRY_SIZE 20
231 /* The first entry in a procedure linkage table looks like this. See
232 the SVR4 ABI m68k supplement to see how this works. */
234 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
236 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
237 0, 0, 0, 2, /* + (.got + 4) - . */
238 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
239 0, 0, 0, 2, /* + (.got + 8) - . */
240 0, 0, 0, 0 /* pad out to 20 bytes. */
243 /* Subsequent entries in a procedure linkage table look like this. */
245 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
247 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
248 0, 0, 0, 2, /* + (.got.plt entry) - . */
249 0x2f, 0x3c, /* move.l #offset,-(%sp) */
250 0, 0, 0, 0, /* + reloc index */
251 0x60, 0xff, /* bra.l .plt */
252 0, 0, 0, 0 /* + .plt - . */
255 static const struct elf_m68k_plt_info elf_m68k_plt_info = {
256 PLT_ENTRY_SIZE,
257 elf_m68k_plt0_entry, { 4, 12 },
258 elf_m68k_plt_entry, { 4, 16 }, 8
261 #define ISAB_PLT_ENTRY_SIZE 24
263 static const bfd_byte elf_isab_plt0_entry[ISAB_PLT_ENTRY_SIZE] =
265 0x20, 0x3c, /* move.l #offset,%d0 */
266 0, 0, 0, 0, /* + (.got + 4) - . */
267 0x2f, 0x3b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l),-(%sp) */
268 0x20, 0x3c, /* move.l #offset,%d0 */
269 0, 0, 0, 0, /* + (.got + 8) - . */
270 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
271 0x4e, 0xd0, /* jmp (%a0) */
272 0x4e, 0x71 /* nop */
275 /* Subsequent entries in a procedure linkage table look like this. */
277 static const bfd_byte elf_isab_plt_entry[ISAB_PLT_ENTRY_SIZE] =
279 0x20, 0x3c, /* move.l #offset,%d0 */
280 0, 0, 0, 0, /* + (.got.plt entry) - . */
281 0x20, 0x7b, 0x08, 0xfa, /* move.l (-6,%pc,%d0:l), %a0 */
282 0x4e, 0xd0, /* jmp (%a0) */
283 0x2f, 0x3c, /* move.l #offset,-(%sp) */
284 0, 0, 0, 0, /* + reloc index */
285 0x60, 0xff, /* bra.l .plt */
286 0, 0, 0, 0 /* + .plt - . */
289 static const struct elf_m68k_plt_info elf_isab_plt_info = {
290 ISAB_PLT_ENTRY_SIZE,
291 elf_isab_plt0_entry, { 2, 12 },
292 elf_isab_plt_entry, { 2, 20 }, 12
295 #define CPU32_PLT_ENTRY_SIZE 24
296 /* Procedure linkage table entries for the cpu32 */
297 static const bfd_byte elf_cpu32_plt0_entry[CPU32_PLT_ENTRY_SIZE] =
299 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
300 0, 0, 0, 2, /* + (.got + 4) - . */
301 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
302 0, 0, 0, 2, /* + (.got + 8) - . */
303 0x4e, 0xd1, /* jmp %a1@ */
304 0, 0, 0, 0, /* pad out to 24 bytes. */
305 0, 0
308 static const bfd_byte elf_cpu32_plt_entry[CPU32_PLT_ENTRY_SIZE] =
310 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
311 0, 0, 0, 2, /* + (.got.plt entry) - . */
312 0x4e, 0xd1, /* jmp %a1@ */
313 0x2f, 0x3c, /* move.l #offset,-(%sp) */
314 0, 0, 0, 0, /* + reloc index */
315 0x60, 0xff, /* bra.l .plt */
316 0, 0, 0, 0, /* + .plt - . */
317 0, 0
320 static const struct elf_m68k_plt_info elf_cpu32_plt_info = {
321 CPU32_PLT_ENTRY_SIZE,
322 elf_cpu32_plt0_entry, { 4, 12 },
323 elf_cpu32_plt_entry, { 4, 18 }, 10
326 /* The m68k linker needs to keep track of the number of relocs that it
327 decides to copy in check_relocs for each symbol. This is so that it
328 can discard PC relative relocs if it doesn't need them when linking
329 with -Bsymbolic. We store the information in a field extending the
330 regular ELF linker hash table. */
332 /* This structure keeps track of the number of PC relative relocs we have
333 copied for a given symbol. */
335 struct elf_m68k_pcrel_relocs_copied
337 /* Next section. */
338 struct elf_m68k_pcrel_relocs_copied *next;
339 /* A section in dynobj. */
340 asection *section;
341 /* Number of relocs copied in this section. */
342 bfd_size_type count;
345 /* m68k ELF linker hash entry. */
347 struct elf_m68k_link_hash_entry
349 struct elf_link_hash_entry root;
351 /* Number of PC relative relocs copied for this symbol. */
352 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
355 #define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
357 /* m68k ELF linker hash table. */
359 struct elf_m68k_link_hash_table
361 struct elf_link_hash_table root;
363 /* Small local sym to section mapping cache. */
364 struct sym_sec_cache sym_sec;
366 /* The PLT format used by this link, or NULL if the format has not
367 yet been chosen. */
368 const struct elf_m68k_plt_info *plt_info;
371 /* Get the m68k ELF linker hash table from a link_info structure. */
373 #define elf_m68k_hash_table(p) \
374 ((struct elf_m68k_link_hash_table *) (p)->hash)
376 /* Create an entry in an m68k ELF linker hash table. */
378 static struct bfd_hash_entry *
379 elf_m68k_link_hash_newfunc (entry, table, string)
380 struct bfd_hash_entry *entry;
381 struct bfd_hash_table *table;
382 const char *string;
384 struct bfd_hash_entry *ret = entry;
386 /* Allocate the structure if it has not already been allocated by a
387 subclass. */
388 if (ret == NULL)
389 ret = bfd_hash_allocate (table,
390 sizeof (struct elf_m68k_link_hash_entry));
391 if (ret == NULL)
392 return ret;
394 /* Call the allocation method of the superclass. */
395 ret = _bfd_elf_link_hash_newfunc (ret, table, string);
396 if (ret != NULL)
397 elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
399 return ret;
402 /* Create an m68k ELF linker hash table. */
404 static struct bfd_link_hash_table *
405 elf_m68k_link_hash_table_create (abfd)
406 bfd *abfd;
408 struct elf_m68k_link_hash_table *ret;
409 bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
411 ret = (struct elf_m68k_link_hash_table *) bfd_malloc (amt);
412 if (ret == (struct elf_m68k_link_hash_table *) NULL)
413 return NULL;
415 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
416 elf_m68k_link_hash_newfunc,
417 sizeof (struct elf_m68k_link_hash_entry)))
419 free (ret);
420 return NULL;
423 ret->sym_sec.abfd = NULL;
424 ret->plt_info = NULL;
426 return &ret->root.root;
429 /* Set the right machine number. */
431 static bfd_boolean
432 elf32_m68k_object_p (bfd *abfd)
434 unsigned int mach = 0;
435 unsigned features = 0;
436 flagword eflags = elf_elfheader (abfd)->e_flags;
438 if (eflags & EF_M68K_M68000)
439 features |= m68000;
440 else if (eflags & EF_M68K_CPU32)
441 features |= cpu32;
442 else if (eflags & EF_M68K_ISA_MASK)
444 switch (eflags & EF_M68K_ISA_MASK)
446 case EF_M68K_ISA_A_NODIV:
447 features |= mcfisa_a;
448 break;
449 case EF_M68K_ISA_A:
450 features |= mcfisa_a|mcfhwdiv;
451 break;
452 case EF_M68K_ISA_A_PLUS:
453 features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
454 break;
455 case EF_M68K_ISA_B_NOUSP:
456 features |= mcfisa_a|mcfisa_b|mcfhwdiv;
457 break;
458 case EF_M68K_ISA_B:
459 features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
460 break;
462 switch (eflags & EF_M68K_MAC_MASK)
464 case EF_M68K_MAC:
465 features |= mcfmac;
466 break;
467 case EF_M68K_EMAC:
468 features |= mcfemac;
469 break;
471 if (eflags & EF_M68K_FLOAT)
472 features |= cfloat;
475 mach = bfd_m68k_features_to_mach (features);
476 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
478 return TRUE;
481 /* Keep m68k-specific flags in the ELF header. */
482 static bfd_boolean
483 elf32_m68k_set_private_flags (abfd, flags)
484 bfd *abfd;
485 flagword flags;
487 elf_elfheader (abfd)->e_flags = flags;
488 elf_flags_init (abfd) = TRUE;
489 return TRUE;
492 /* Merge backend specific data from an object file to the output
493 object file when linking. */
494 static bfd_boolean
495 elf32_m68k_merge_private_bfd_data (ibfd, obfd)
496 bfd *ibfd;
497 bfd *obfd;
499 flagword out_flags;
500 flagword in_flags;
501 flagword out_isa;
502 flagword in_isa;
503 const bfd_arch_info_type *arch_info;
505 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
506 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
507 return FALSE;
509 /* Get the merged machine. This checks for incompatibility between
510 Coldfire & non-Coldfire flags, incompability between different
511 Coldfire ISAs, and incompability between different MAC types. */
512 arch_info = bfd_arch_get_compatible (ibfd, obfd, FALSE);
513 if (!arch_info)
514 return FALSE;
516 bfd_set_arch_mach (obfd, bfd_arch_m68k, arch_info->mach);
518 in_flags = elf_elfheader (ibfd)->e_flags;
519 if (!elf_flags_init (obfd))
521 elf_flags_init (obfd) = TRUE;
522 out_flags = in_flags;
524 else
526 out_flags = elf_elfheader (obfd)->e_flags;
527 in_isa = (in_flags & EF_M68K_ISA_MASK);
528 out_isa = (out_flags & EF_M68K_ISA_MASK);
529 if (in_isa > out_isa)
530 out_flags ^= in_isa ^ out_isa;
531 out_flags |= in_flags ^ in_isa;
533 elf_elfheader (obfd)->e_flags = out_flags;
535 return TRUE;
538 /* Display the flags field. */
539 static bfd_boolean
540 elf32_m68k_print_private_bfd_data (abfd, ptr)
541 bfd *abfd;
542 PTR ptr;
544 FILE *file = (FILE *) ptr;
545 flagword eflags = elf_elfheader (abfd)->e_flags;
547 BFD_ASSERT (abfd != NULL && ptr != NULL);
549 /* Print normal ELF private data. */
550 _bfd_elf_print_private_bfd_data (abfd, ptr);
552 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
554 /* xgettext:c-format */
555 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
557 if (eflags & EF_M68K_CPU32)
558 fprintf (file, " [cpu32]");
560 if (eflags & EF_M68K_M68000)
561 fprintf (file, " [m68000]");
563 if (eflags & EF_M68K_CFV4E)
564 fprintf (file, " [cfv4e]");
566 if (eflags & EF_M68K_ISA_MASK)
568 char const *isa = _("unknown");
569 char const *mac = _("unknown");
570 char const *additional = "";
572 switch (eflags & EF_M68K_ISA_MASK)
574 case EF_M68K_ISA_A_NODIV:
575 isa = "A";
576 additional = " [nodiv]";
577 break;
578 case EF_M68K_ISA_A:
579 isa = "A";
580 break;
581 case EF_M68K_ISA_A_PLUS:
582 isa = "A+";
583 break;
584 case EF_M68K_ISA_B_NOUSP:
585 isa = "B";
586 additional = " [nousp]";
587 break;
588 case EF_M68K_ISA_B:
589 isa = "B";
590 break;
592 fprintf (file, " [isa %s]%s", isa, additional);
593 if (eflags & EF_M68K_FLOAT)
594 fprintf (file, " [float]");
595 switch (eflags & EF_M68K_MAC_MASK)
597 case 0:
598 mac = NULL;
599 break;
600 case EF_M68K_MAC:
601 mac = "mac";
602 break;
603 case EF_M68K_EMAC:
604 mac = "emac";
605 break;
607 if (mac)
608 fprintf (file, " [%s]", mac);
611 fputc ('\n', file);
613 return TRUE;
615 /* Look through the relocs for a section during the first phase, and
616 allocate space in the global offset table or procedure linkage
617 table. */
619 static bfd_boolean
620 elf_m68k_check_relocs (abfd, info, sec, relocs)
621 bfd *abfd;
622 struct bfd_link_info *info;
623 asection *sec;
624 const Elf_Internal_Rela *relocs;
626 bfd *dynobj;
627 Elf_Internal_Shdr *symtab_hdr;
628 struct elf_link_hash_entry **sym_hashes;
629 bfd_signed_vma *local_got_refcounts;
630 const Elf_Internal_Rela *rel;
631 const Elf_Internal_Rela *rel_end;
632 asection *sgot;
633 asection *srelgot;
634 asection *sreloc;
636 if (info->relocatable)
637 return TRUE;
639 dynobj = elf_hash_table (info)->dynobj;
640 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
641 sym_hashes = elf_sym_hashes (abfd);
642 local_got_refcounts = elf_local_got_refcounts (abfd);
644 sgot = NULL;
645 srelgot = NULL;
646 sreloc = NULL;
648 rel_end = relocs + sec->reloc_count;
649 for (rel = relocs; rel < rel_end; rel++)
651 unsigned long r_symndx;
652 struct elf_link_hash_entry *h;
654 r_symndx = ELF32_R_SYM (rel->r_info);
656 if (r_symndx < symtab_hdr->sh_info)
657 h = NULL;
658 else
660 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
661 while (h->root.type == bfd_link_hash_indirect
662 || h->root.type == bfd_link_hash_warning)
663 h = (struct elf_link_hash_entry *) h->root.u.i.link;
666 switch (ELF32_R_TYPE (rel->r_info))
668 case R_68K_GOT8:
669 case R_68K_GOT16:
670 case R_68K_GOT32:
671 if (h != NULL
672 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
673 break;
674 /* Fall through. */
675 case R_68K_GOT8O:
676 case R_68K_GOT16O:
677 case R_68K_GOT32O:
678 /* This symbol requires a global offset table entry. */
680 if (dynobj == NULL)
682 /* Create the .got section. */
683 elf_hash_table (info)->dynobj = dynobj = abfd;
684 if (!_bfd_elf_create_got_section (dynobj, info))
685 return FALSE;
688 if (sgot == NULL)
690 sgot = bfd_get_section_by_name (dynobj, ".got");
691 BFD_ASSERT (sgot != NULL);
694 if (srelgot == NULL
695 && (h != NULL || info->shared))
697 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
698 if (srelgot == NULL)
700 srelgot = bfd_make_section_with_flags (dynobj,
701 ".rela.got",
702 (SEC_ALLOC
703 | SEC_LOAD
704 | SEC_HAS_CONTENTS
705 | SEC_IN_MEMORY
706 | SEC_LINKER_CREATED
707 | SEC_READONLY));
708 if (srelgot == NULL
709 || !bfd_set_section_alignment (dynobj, srelgot, 2))
710 return FALSE;
714 if (h != NULL)
716 if (h->got.refcount == 0)
718 /* Make sure this symbol is output as a dynamic symbol. */
719 if (h->dynindx == -1
720 && !h->forced_local)
722 if (!bfd_elf_link_record_dynamic_symbol (info, h))
723 return FALSE;
726 /* Allocate space in the .got section. */
727 sgot->size += 4;
728 /* Allocate relocation space. */
729 srelgot->size += sizeof (Elf32_External_Rela);
731 h->got.refcount++;
733 else
735 /* This is a global offset table entry for a local symbol. */
736 if (local_got_refcounts == NULL)
738 bfd_size_type size;
740 size = symtab_hdr->sh_info;
741 size *= sizeof (bfd_signed_vma);
742 local_got_refcounts = ((bfd_signed_vma *)
743 bfd_zalloc (abfd, size));
744 if (local_got_refcounts == NULL)
745 return FALSE;
746 elf_local_got_refcounts (abfd) = local_got_refcounts;
748 if (local_got_refcounts[r_symndx] == 0)
750 sgot->size += 4;
751 if (info->shared)
753 /* If we are generating a shared object, we need to
754 output a R_68K_RELATIVE reloc so that the dynamic
755 linker can adjust this GOT entry. */
756 srelgot->size += sizeof (Elf32_External_Rela);
759 local_got_refcounts[r_symndx]++;
761 break;
763 case R_68K_PLT8:
764 case R_68K_PLT16:
765 case R_68K_PLT32:
766 /* This symbol requires a procedure linkage table entry. We
767 actually build the entry in adjust_dynamic_symbol,
768 because this might be a case of linking PIC code which is
769 never referenced by a dynamic object, in which case we
770 don't need to generate a procedure linkage table entry
771 after all. */
773 /* If this is a local symbol, we resolve it directly without
774 creating a procedure linkage table entry. */
775 if (h == NULL)
776 continue;
778 h->needs_plt = 1;
779 h->plt.refcount++;
780 break;
782 case R_68K_PLT8O:
783 case R_68K_PLT16O:
784 case R_68K_PLT32O:
785 /* This symbol requires a procedure linkage table entry. */
787 if (h == NULL)
789 /* It does not make sense to have this relocation for a
790 local symbol. FIXME: does it? How to handle it if
791 it does make sense? */
792 bfd_set_error (bfd_error_bad_value);
793 return FALSE;
796 /* Make sure this symbol is output as a dynamic symbol. */
797 if (h->dynindx == -1
798 && !h->forced_local)
800 if (!bfd_elf_link_record_dynamic_symbol (info, h))
801 return FALSE;
804 h->needs_plt = 1;
805 h->plt.refcount++;
806 break;
808 case R_68K_PC8:
809 case R_68K_PC16:
810 case R_68K_PC32:
811 /* If we are creating a shared library and this is not a local
812 symbol, we need to copy the reloc into the shared library.
813 However when linking with -Bsymbolic and this is a global
814 symbol which is defined in an object we are including in the
815 link (i.e., DEF_REGULAR is set), then we can resolve the
816 reloc directly. At this point we have not seen all the input
817 files, so it is possible that DEF_REGULAR is not set now but
818 will be set later (it is never cleared). We account for that
819 possibility below by storing information in the
820 pcrel_relocs_copied field of the hash table entry. */
821 if (!(info->shared
822 && (sec->flags & SEC_ALLOC) != 0
823 && h != NULL
824 && (!info->symbolic
825 || h->root.type == bfd_link_hash_defweak
826 || !h->def_regular)))
828 if (h != NULL)
830 /* Make sure a plt entry is created for this symbol if
831 it turns out to be a function defined by a dynamic
832 object. */
833 h->plt.refcount++;
835 break;
837 /* Fall through. */
838 case R_68K_8:
839 case R_68K_16:
840 case R_68K_32:
841 if (h != NULL)
843 /* Make sure a plt entry is created for this symbol if it
844 turns out to be a function defined by a dynamic object. */
845 h->plt.refcount++;
848 /* If we are creating a shared library, we need to copy the
849 reloc into the shared library. */
850 if (info->shared
851 && (sec->flags & SEC_ALLOC) != 0)
853 /* When creating a shared object, we must copy these
854 reloc types into the output file. We create a reloc
855 section in dynobj and make room for this reloc. */
856 if (sreloc == NULL)
858 const char *name;
860 name = (bfd_elf_string_from_elf_section
861 (abfd,
862 elf_elfheader (abfd)->e_shstrndx,
863 elf_section_data (sec)->rel_hdr.sh_name));
864 if (name == NULL)
865 return FALSE;
867 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
868 && strcmp (bfd_get_section_name (abfd, sec),
869 name + 5) == 0);
871 sreloc = bfd_get_section_by_name (dynobj, name);
872 if (sreloc == NULL)
874 sreloc = bfd_make_section_with_flags (dynobj,
875 name,
876 (SEC_ALLOC
877 | SEC_LOAD
878 | SEC_HAS_CONTENTS
879 | SEC_IN_MEMORY
880 | SEC_LINKER_CREATED
881 | SEC_READONLY));
882 if (sreloc == NULL
883 || !bfd_set_section_alignment (dynobj, sreloc, 2))
884 return FALSE;
886 elf_section_data (sec)->sreloc = sreloc;
889 if (sec->flags & SEC_READONLY
890 /* Don't set DF_TEXTREL yet for PC relative
891 relocations, they might be discarded later. */
892 && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
893 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
894 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
895 info->flags |= DF_TEXTREL;
897 sreloc->size += sizeof (Elf32_External_Rela);
899 /* We count the number of PC relative relocations we have
900 entered for this symbol, so that we can discard them
901 again if, in the -Bsymbolic case, the symbol is later
902 defined by a regular object, or, in the normal shared
903 case, the symbol is forced to be local. Note that this
904 function is only called if we are using an m68kelf linker
905 hash table, which means that h is really a pointer to an
906 elf_m68k_link_hash_entry. */
907 if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
908 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
909 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
911 struct elf_m68k_pcrel_relocs_copied *p;
912 struct elf_m68k_pcrel_relocs_copied **head;
914 if (h != NULL)
916 struct elf_m68k_link_hash_entry *eh
917 = elf_m68k_hash_entry (h);
918 head = &eh->pcrel_relocs_copied;
920 else
922 asection *s;
923 void *vpp;
925 s = (bfd_section_from_r_symndx
926 (abfd, &elf_m68k_hash_table (info)->sym_sec,
927 sec, r_symndx));
928 if (s == NULL)
929 return FALSE;
931 vpp = &elf_section_data (s)->local_dynrel;
932 head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
935 for (p = *head; p != NULL; p = p->next)
936 if (p->section == sreloc)
937 break;
939 if (p == NULL)
941 p = ((struct elf_m68k_pcrel_relocs_copied *)
942 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
943 if (p == NULL)
944 return FALSE;
945 p->next = *head;
946 *head = p;
947 p->section = sreloc;
948 p->count = 0;
951 ++p->count;
955 break;
957 /* This relocation describes the C++ object vtable hierarchy.
958 Reconstruct it for later use during GC. */
959 case R_68K_GNU_VTINHERIT:
960 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
961 return FALSE;
962 break;
964 /* This relocation describes which C++ vtable entries are actually
965 used. Record for later use during GC. */
966 case R_68K_GNU_VTENTRY:
967 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
968 return FALSE;
969 break;
971 default:
972 break;
976 return TRUE;
979 /* Return the section that should be marked against GC for a given
980 relocation. */
982 static asection *
983 elf_m68k_gc_mark_hook (sec, info, rel, h, sym)
984 asection *sec;
985 struct bfd_link_info *info ATTRIBUTE_UNUSED;
986 Elf_Internal_Rela *rel;
987 struct elf_link_hash_entry *h;
988 Elf_Internal_Sym *sym;
990 if (h != NULL)
992 switch (ELF32_R_TYPE (rel->r_info))
994 case R_68K_GNU_VTINHERIT:
995 case R_68K_GNU_VTENTRY:
996 break;
998 default:
999 switch (h->root.type)
1001 default:
1002 break;
1004 case bfd_link_hash_defined:
1005 case bfd_link_hash_defweak:
1006 return h->root.u.def.section;
1008 case bfd_link_hash_common:
1009 return h->root.u.c.p->section;
1013 else
1014 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1016 return NULL;
1019 /* Update the got entry reference counts for the section being removed. */
1021 static bfd_boolean
1022 elf_m68k_gc_sweep_hook (abfd, info, sec, relocs)
1023 bfd *abfd;
1024 struct bfd_link_info *info;
1025 asection *sec;
1026 const Elf_Internal_Rela *relocs;
1028 Elf_Internal_Shdr *symtab_hdr;
1029 struct elf_link_hash_entry **sym_hashes;
1030 bfd_signed_vma *local_got_refcounts;
1031 const Elf_Internal_Rela *rel, *relend;
1032 bfd *dynobj;
1033 asection *sgot;
1034 asection *srelgot;
1036 dynobj = elf_hash_table (info)->dynobj;
1037 if (dynobj == NULL)
1038 return TRUE;
1040 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1041 sym_hashes = elf_sym_hashes (abfd);
1042 local_got_refcounts = elf_local_got_refcounts (abfd);
1044 sgot = bfd_get_section_by_name (dynobj, ".got");
1045 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1047 relend = relocs + sec->reloc_count;
1048 for (rel = relocs; rel < relend; rel++)
1050 unsigned long r_symndx;
1051 struct elf_link_hash_entry *h = NULL;
1053 r_symndx = ELF32_R_SYM (rel->r_info);
1054 if (r_symndx >= symtab_hdr->sh_info)
1056 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1057 while (h->root.type == bfd_link_hash_indirect
1058 || h->root.type == bfd_link_hash_warning)
1059 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1062 switch (ELF32_R_TYPE (rel->r_info))
1064 case R_68K_GOT8:
1065 case R_68K_GOT16:
1066 case R_68K_GOT32:
1067 case R_68K_GOT8O:
1068 case R_68K_GOT16O:
1069 case R_68K_GOT32O:
1070 if (h != NULL)
1072 if (h->got.refcount > 0)
1074 --h->got.refcount;
1075 if (h->got.refcount == 0)
1077 /* We don't need the .got entry any more. */
1078 sgot->size -= 4;
1079 srelgot->size -= sizeof (Elf32_External_Rela);
1083 else if (local_got_refcounts != NULL)
1085 if (local_got_refcounts[r_symndx] > 0)
1087 --local_got_refcounts[r_symndx];
1088 if (local_got_refcounts[r_symndx] == 0)
1090 /* We don't need the .got entry any more. */
1091 sgot->size -= 4;
1092 if (info->shared)
1093 srelgot->size -= sizeof (Elf32_External_Rela);
1097 break;
1099 case R_68K_PLT8:
1100 case R_68K_PLT16:
1101 case R_68K_PLT32:
1102 case R_68K_PLT8O:
1103 case R_68K_PLT16O:
1104 case R_68K_PLT32O:
1105 case R_68K_PC8:
1106 case R_68K_PC16:
1107 case R_68K_PC32:
1108 case R_68K_8:
1109 case R_68K_16:
1110 case R_68K_32:
1111 if (h != NULL)
1113 if (h->plt.refcount > 0)
1114 --h->plt.refcount;
1116 break;
1118 default:
1119 break;
1123 return TRUE;
1126 /* Return the type of PLT associated with OUTPUT_BFD. */
1128 static const struct elf_m68k_plt_info *
1129 elf_m68k_get_plt_info (bfd *output_bfd)
1131 unsigned int features;
1133 features = bfd_m68k_mach_to_features (bfd_get_mach (output_bfd));
1134 if (features & cpu32)
1135 return &elf_cpu32_plt_info;
1136 if (features & mcfisa_b)
1137 return &elf_isab_plt_info;
1138 return &elf_m68k_plt_info;
1141 /* This function is called after all the input files have been read,
1142 and the input sections have been assigned to output sections.
1143 It's a convenient place to determine the PLT style. */
1145 static bfd_boolean
1146 elf_m68k_always_size_sections (bfd *output_bfd, struct bfd_link_info *info)
1148 elf_m68k_hash_table (info)->plt_info = elf_m68k_get_plt_info (output_bfd);
1149 return TRUE;
1152 /* Adjust a symbol defined by a dynamic object and referenced by a
1153 regular object. The current definition is in some section of the
1154 dynamic object, but we're not including those sections. We have to
1155 change the definition to something the rest of the link can
1156 understand. */
1158 static bfd_boolean
1159 elf_m68k_adjust_dynamic_symbol (info, h)
1160 struct bfd_link_info *info;
1161 struct elf_link_hash_entry *h;
1163 struct elf_m68k_link_hash_table *htab;
1164 bfd *dynobj;
1165 asection *s;
1166 unsigned int power_of_two;
1168 htab = elf_m68k_hash_table (info);
1169 dynobj = elf_hash_table (info)->dynobj;
1171 /* Make sure we know what is going on here. */
1172 BFD_ASSERT (dynobj != NULL
1173 && (h->needs_plt
1174 || h->u.weakdef != NULL
1175 || (h->def_dynamic
1176 && h->ref_regular
1177 && !h->def_regular)));
1179 /* If this is a function, put it in the procedure linkage table. We
1180 will fill in the contents of the procedure linkage table later,
1181 when we know the address of the .got section. */
1182 if (h->type == STT_FUNC
1183 || h->needs_plt)
1185 if ((h->plt.refcount <= 0
1186 || SYMBOL_CALLS_LOCAL (info, h)
1187 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1188 && h->root.type == bfd_link_hash_undefweak))
1189 /* We must always create the plt entry if it was referenced
1190 by a PLTxxO relocation. In this case we already recorded
1191 it as a dynamic symbol. */
1192 && h->dynindx == -1)
1194 /* This case can occur if we saw a PLTxx reloc in an input
1195 file, but the symbol was never referred to by a dynamic
1196 object, or if all references were garbage collected. In
1197 such a case, we don't actually need to build a procedure
1198 linkage table, and we can just do a PCxx reloc instead. */
1199 h->plt.offset = (bfd_vma) -1;
1200 h->needs_plt = 0;
1201 return TRUE;
1204 /* Make sure this symbol is output as a dynamic symbol. */
1205 if (h->dynindx == -1
1206 && !h->forced_local)
1208 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1209 return FALSE;
1212 s = bfd_get_section_by_name (dynobj, ".plt");
1213 BFD_ASSERT (s != NULL);
1215 /* If this is the first .plt entry, make room for the special
1216 first entry. */
1217 if (s->size == 0)
1218 s->size = htab->plt_info->size;
1220 /* If this symbol is not defined in a regular file, and we are
1221 not generating a shared library, then set the symbol to this
1222 location in the .plt. This is required to make function
1223 pointers compare as equal between the normal executable and
1224 the shared library. */
1225 if (!info->shared
1226 && !h->def_regular)
1228 h->root.u.def.section = s;
1229 h->root.u.def.value = s->size;
1232 h->plt.offset = s->size;
1234 /* Make room for this entry. */
1235 s->size += htab->plt_info->size;
1237 /* We also need to make an entry in the .got.plt section, which
1238 will be placed in the .got section by the linker script. */
1239 s = bfd_get_section_by_name (dynobj, ".got.plt");
1240 BFD_ASSERT (s != NULL);
1241 s->size += 4;
1243 /* We also need to make an entry in the .rela.plt section. */
1244 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1245 BFD_ASSERT (s != NULL);
1246 s->size += sizeof (Elf32_External_Rela);
1248 return TRUE;
1251 /* Reinitialize the plt offset now that it is not used as a reference
1252 count any more. */
1253 h->plt.offset = (bfd_vma) -1;
1255 /* If this is a weak symbol, and there is a real definition, the
1256 processor independent code will have arranged for us to see the
1257 real definition first, and we can just use the same value. */
1258 if (h->u.weakdef != NULL)
1260 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1261 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1262 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1263 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1264 return TRUE;
1267 /* This is a reference to a symbol defined by a dynamic object which
1268 is not a function. */
1270 /* If we are creating a shared library, we must presume that the
1271 only references to the symbol are via the global offset table.
1272 For such cases we need not do anything here; the relocations will
1273 be handled correctly by relocate_section. */
1274 if (info->shared)
1275 return TRUE;
1277 if (h->size == 0)
1279 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1280 h->root.root.string);
1281 return TRUE;
1284 /* We must allocate the symbol in our .dynbss section, which will
1285 become part of the .bss section of the executable. There will be
1286 an entry for this symbol in the .dynsym section. The dynamic
1287 object will contain position independent code, so all references
1288 from the dynamic object to this symbol will go through the global
1289 offset table. The dynamic linker will use the .dynsym entry to
1290 determine the address it must put in the global offset table, so
1291 both the dynamic object and the regular object will refer to the
1292 same memory location for the variable. */
1294 s = bfd_get_section_by_name (dynobj, ".dynbss");
1295 BFD_ASSERT (s != NULL);
1297 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1298 copy the initial value out of the dynamic object and into the
1299 runtime process image. We need to remember the offset into the
1300 .rela.bss section we are going to use. */
1301 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1303 asection *srel;
1305 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1306 BFD_ASSERT (srel != NULL);
1307 srel->size += sizeof (Elf32_External_Rela);
1308 h->needs_copy = 1;
1311 /* We need to figure out the alignment required for this symbol. I
1312 have no idea how ELF linkers handle this. */
1313 power_of_two = bfd_log2 (h->size);
1314 if (power_of_two > 3)
1315 power_of_two = 3;
1317 /* Apply the required alignment. */
1318 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1319 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1321 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
1322 return FALSE;
1325 /* Define the symbol as being at this point in the section. */
1326 h->root.u.def.section = s;
1327 h->root.u.def.value = s->size;
1329 /* Increment the section size to make room for the symbol. */
1330 s->size += h->size;
1332 return TRUE;
1335 /* Set the sizes of the dynamic sections. */
1337 static bfd_boolean
1338 elf_m68k_size_dynamic_sections (output_bfd, info)
1339 bfd *output_bfd ATTRIBUTE_UNUSED;
1340 struct bfd_link_info *info;
1342 bfd *dynobj;
1343 asection *s;
1344 bfd_boolean plt;
1345 bfd_boolean relocs;
1347 dynobj = elf_hash_table (info)->dynobj;
1348 BFD_ASSERT (dynobj != NULL);
1350 if (elf_hash_table (info)->dynamic_sections_created)
1352 /* Set the contents of the .interp section to the interpreter. */
1353 if (info->executable)
1355 s = bfd_get_section_by_name (dynobj, ".interp");
1356 BFD_ASSERT (s != NULL);
1357 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1358 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1361 else
1363 /* We may have created entries in the .rela.got section.
1364 However, if we are not creating the dynamic sections, we will
1365 not actually use these entries. Reset the size of .rela.got,
1366 which will cause it to get stripped from the output file
1367 below. */
1368 s = bfd_get_section_by_name (dynobj, ".rela.got");
1369 if (s != NULL)
1370 s->size = 0;
1373 /* If this is a -Bsymbolic shared link, then we need to discard all
1374 PC relative relocs against symbols defined in a regular object.
1375 For the normal shared case we discard the PC relative relocs
1376 against symbols that have become local due to visibility changes.
1377 We allocated space for them in the check_relocs routine, but we
1378 will not fill them in in the relocate_section routine. */
1379 if (info->shared)
1380 elf_link_hash_traverse (elf_hash_table (info),
1381 elf_m68k_discard_copies,
1382 (PTR) info);
1384 /* The check_relocs and adjust_dynamic_symbol entry points have
1385 determined the sizes of the various dynamic sections. Allocate
1386 memory for them. */
1387 plt = FALSE;
1388 relocs = FALSE;
1389 for (s = dynobj->sections; s != NULL; s = s->next)
1391 const char *name;
1393 if ((s->flags & SEC_LINKER_CREATED) == 0)
1394 continue;
1396 /* It's OK to base decisions on the section name, because none
1397 of the dynobj section names depend upon the input files. */
1398 name = bfd_get_section_name (dynobj, s);
1400 if (strcmp (name, ".plt") == 0)
1402 /* Remember whether there is a PLT. */
1403 plt = s->size != 0;
1405 else if (strncmp (name, ".rela", 5) == 0)
1407 if (s->size != 0)
1409 relocs = TRUE;
1411 /* We use the reloc_count field as a counter if we need
1412 to copy relocs into the output file. */
1413 s->reloc_count = 0;
1416 else if (strncmp (name, ".got", 4) != 0
1417 && strcmp (name, ".dynbss") != 0)
1419 /* It's not one of our sections, so don't allocate space. */
1420 continue;
1423 if (s->size == 0)
1425 /* If we don't need this section, strip it from the
1426 output file. This is mostly to handle .rela.bss and
1427 .rela.plt. We must create both sections in
1428 create_dynamic_sections, because they must be created
1429 before the linker maps input sections to output
1430 sections. The linker does that before
1431 adjust_dynamic_symbol is called, and it is that
1432 function which decides whether anything needs to go
1433 into these sections. */
1434 s->flags |= SEC_EXCLUDE;
1435 continue;
1438 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1439 continue;
1441 /* Allocate memory for the section contents. */
1442 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1443 Unused entries should be reclaimed before the section's contents
1444 are written out, but at the moment this does not happen. Thus in
1445 order to prevent writing out garbage, we initialise the section's
1446 contents to zero. */
1447 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1448 if (s->contents == NULL)
1449 return FALSE;
1452 if (elf_hash_table (info)->dynamic_sections_created)
1454 /* Add some entries to the .dynamic section. We fill in the
1455 values later, in elf_m68k_finish_dynamic_sections, but we
1456 must add the entries now so that we get the correct size for
1457 the .dynamic section. The DT_DEBUG entry is filled in by the
1458 dynamic linker and used by the debugger. */
1459 #define add_dynamic_entry(TAG, VAL) \
1460 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1462 if (!info->shared)
1464 if (!add_dynamic_entry (DT_DEBUG, 0))
1465 return FALSE;
1468 if (plt)
1470 if (!add_dynamic_entry (DT_PLTGOT, 0)
1471 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1472 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1473 || !add_dynamic_entry (DT_JMPREL, 0))
1474 return FALSE;
1477 if (relocs)
1479 if (!add_dynamic_entry (DT_RELA, 0)
1480 || !add_dynamic_entry (DT_RELASZ, 0)
1481 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1482 return FALSE;
1485 if ((info->flags & DF_TEXTREL) != 0)
1487 if (!add_dynamic_entry (DT_TEXTREL, 0))
1488 return FALSE;
1491 #undef add_dynamic_entry
1493 return TRUE;
1496 /* This function is called via elf_link_hash_traverse if we are
1497 creating a shared object. In the -Bsymbolic case it discards the
1498 space allocated to copy PC relative relocs against symbols which
1499 are defined in regular objects. For the normal shared case, it
1500 discards space for pc-relative relocs that have become local due to
1501 symbol visibility changes. We allocated space for them in the
1502 check_relocs routine, but we won't fill them in in the
1503 relocate_section routine.
1505 We also check whether any of the remaining relocations apply
1506 against a readonly section, and set the DF_TEXTREL flag in this
1507 case. */
1509 static bfd_boolean
1510 elf_m68k_discard_copies (h, inf)
1511 struct elf_link_hash_entry *h;
1512 PTR inf;
1514 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1515 struct elf_m68k_pcrel_relocs_copied *s;
1517 if (h->root.type == bfd_link_hash_warning)
1518 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1520 if (!h->def_regular
1521 || (!info->symbolic
1522 && !h->forced_local))
1524 if ((info->flags & DF_TEXTREL) == 0)
1526 /* Look for relocations against read-only sections. */
1527 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1528 s != NULL;
1529 s = s->next)
1530 if ((s->section->flags & SEC_READONLY) != 0)
1532 info->flags |= DF_TEXTREL;
1533 break;
1537 return TRUE;
1540 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1541 s != NULL;
1542 s = s->next)
1543 s->section->size -= s->count * sizeof (Elf32_External_Rela);
1545 return TRUE;
1548 /* Relocate an M68K ELF section. */
1550 static bfd_boolean
1551 elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1552 contents, relocs, local_syms, local_sections)
1553 bfd *output_bfd;
1554 struct bfd_link_info *info;
1555 bfd *input_bfd;
1556 asection *input_section;
1557 bfd_byte *contents;
1558 Elf_Internal_Rela *relocs;
1559 Elf_Internal_Sym *local_syms;
1560 asection **local_sections;
1562 bfd *dynobj;
1563 Elf_Internal_Shdr *symtab_hdr;
1564 struct elf_link_hash_entry **sym_hashes;
1565 bfd_vma *local_got_offsets;
1566 asection *sgot;
1567 asection *splt;
1568 asection *sreloc;
1569 Elf_Internal_Rela *rel;
1570 Elf_Internal_Rela *relend;
1572 if (info->relocatable)
1573 return TRUE;
1575 dynobj = elf_hash_table (info)->dynobj;
1576 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1577 sym_hashes = elf_sym_hashes (input_bfd);
1578 local_got_offsets = elf_local_got_offsets (input_bfd);
1580 sgot = NULL;
1581 splt = NULL;
1582 sreloc = NULL;
1584 rel = relocs;
1585 relend = relocs + input_section->reloc_count;
1586 for (; rel < relend; rel++)
1588 int r_type;
1589 reloc_howto_type *howto;
1590 unsigned long r_symndx;
1591 struct elf_link_hash_entry *h;
1592 Elf_Internal_Sym *sym;
1593 asection *sec;
1594 bfd_vma relocation;
1595 bfd_boolean unresolved_reloc;
1596 bfd_reloc_status_type r;
1598 r_type = ELF32_R_TYPE (rel->r_info);
1599 if (r_type < 0 || r_type >= (int) R_68K_max)
1601 bfd_set_error (bfd_error_bad_value);
1602 return FALSE;
1604 howto = howto_table + r_type;
1606 r_symndx = ELF32_R_SYM (rel->r_info);
1608 h = NULL;
1609 sym = NULL;
1610 sec = NULL;
1611 unresolved_reloc = FALSE;
1613 if (r_symndx < symtab_hdr->sh_info)
1615 sym = local_syms + r_symndx;
1616 sec = local_sections[r_symndx];
1617 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1619 else
1621 bfd_boolean warned;
1623 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1624 r_symndx, symtab_hdr, sym_hashes,
1625 h, sec, relocation,
1626 unresolved_reloc, warned);
1629 switch (r_type)
1631 case R_68K_GOT8:
1632 case R_68K_GOT16:
1633 case R_68K_GOT32:
1634 /* Relocation is to the address of the entry for this symbol
1635 in the global offset table. */
1636 if (h != NULL
1637 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1638 break;
1639 /* Fall through. */
1640 case R_68K_GOT8O:
1641 case R_68K_GOT16O:
1642 case R_68K_GOT32O:
1643 /* Relocation is the offset of the entry for this symbol in
1644 the global offset table. */
1647 bfd_vma off;
1649 if (sgot == NULL)
1651 sgot = bfd_get_section_by_name (dynobj, ".got");
1652 BFD_ASSERT (sgot != NULL);
1655 if (h != NULL)
1657 bfd_boolean dyn;
1659 off = h->got.offset;
1660 BFD_ASSERT (off != (bfd_vma) -1);
1662 dyn = elf_hash_table (info)->dynamic_sections_created;
1663 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1664 || (info->shared
1665 && (info->symbolic
1666 || h->dynindx == -1
1667 || h->forced_local)
1668 && h->def_regular))
1670 /* This is actually a static link, or it is a
1671 -Bsymbolic link and the symbol is defined
1672 locally, or the symbol was forced to be local
1673 because of a version file.. We must initialize
1674 this entry in the global offset table. Since
1675 the offset must always be a multiple of 4, we
1676 use the least significant bit to record whether
1677 we have initialized it already.
1679 When doing a dynamic link, we create a .rela.got
1680 relocation entry to initialize the value. This
1681 is done in the finish_dynamic_symbol routine. */
1682 if ((off & 1) != 0)
1683 off &= ~1;
1684 else
1686 bfd_put_32 (output_bfd, relocation,
1687 sgot->contents + off);
1688 h->got.offset |= 1;
1691 else
1692 unresolved_reloc = FALSE;
1694 else
1696 BFD_ASSERT (local_got_offsets != NULL
1697 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1699 off = local_got_offsets[r_symndx];
1701 /* The offset must always be a multiple of 4. We use
1702 the least significant bit to record whether we have
1703 already generated the necessary reloc. */
1704 if ((off & 1) != 0)
1705 off &= ~1;
1706 else
1708 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1710 if (info->shared)
1712 asection *s;
1713 Elf_Internal_Rela outrel;
1714 bfd_byte *loc;
1716 s = bfd_get_section_by_name (dynobj, ".rela.got");
1717 BFD_ASSERT (s != NULL);
1719 outrel.r_offset = (sgot->output_section->vma
1720 + sgot->output_offset
1721 + off);
1722 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1723 outrel.r_addend = relocation;
1724 loc = s->contents;
1725 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1726 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1729 local_got_offsets[r_symndx] |= 1;
1733 relocation = sgot->output_offset + off;
1734 if (r_type == R_68K_GOT8O
1735 || r_type == R_68K_GOT16O
1736 || r_type == R_68K_GOT32O)
1738 /* This relocation does not use the addend. */
1739 rel->r_addend = 0;
1741 else
1742 relocation += sgot->output_section->vma;
1744 break;
1746 case R_68K_PLT8:
1747 case R_68K_PLT16:
1748 case R_68K_PLT32:
1749 /* Relocation is to the entry for this symbol in the
1750 procedure linkage table. */
1752 /* Resolve a PLTxx reloc against a local symbol directly,
1753 without using the procedure linkage table. */
1754 if (h == NULL)
1755 break;
1757 if (h->plt.offset == (bfd_vma) -1
1758 || !elf_hash_table (info)->dynamic_sections_created)
1760 /* We didn't make a PLT entry for this symbol. This
1761 happens when statically linking PIC code, or when
1762 using -Bsymbolic. */
1763 break;
1766 if (splt == NULL)
1768 splt = bfd_get_section_by_name (dynobj, ".plt");
1769 BFD_ASSERT (splt != NULL);
1772 relocation = (splt->output_section->vma
1773 + splt->output_offset
1774 + h->plt.offset);
1775 unresolved_reloc = FALSE;
1776 break;
1778 case R_68K_PLT8O:
1779 case R_68K_PLT16O:
1780 case R_68K_PLT32O:
1781 /* Relocation is the offset of the entry for this symbol in
1782 the procedure linkage table. */
1783 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1785 if (splt == NULL)
1787 splt = bfd_get_section_by_name (dynobj, ".plt");
1788 BFD_ASSERT (splt != NULL);
1791 relocation = h->plt.offset;
1792 unresolved_reloc = FALSE;
1794 /* This relocation does not use the addend. */
1795 rel->r_addend = 0;
1797 break;
1799 case R_68K_PC8:
1800 case R_68K_PC16:
1801 case R_68K_PC32:
1802 if (h == NULL
1803 || (info->shared
1804 && h->forced_local))
1805 break;
1806 /* Fall through. */
1807 case R_68K_8:
1808 case R_68K_16:
1809 case R_68K_32:
1810 if (info->shared
1811 && r_symndx != 0
1812 && (input_section->flags & SEC_ALLOC) != 0
1813 && (h == NULL
1814 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1815 || h->root.type != bfd_link_hash_undefweak)
1816 && ((r_type != R_68K_PC8
1817 && r_type != R_68K_PC16
1818 && r_type != R_68K_PC32)
1819 || (h != NULL
1820 && h->dynindx != -1
1821 && (!info->symbolic
1822 || !h->def_regular))))
1824 Elf_Internal_Rela outrel;
1825 bfd_byte *loc;
1826 bfd_boolean skip, relocate;
1828 /* When generating a shared object, these relocations
1829 are copied into the output file to be resolved at run
1830 time. */
1832 skip = FALSE;
1833 relocate = FALSE;
1835 outrel.r_offset =
1836 _bfd_elf_section_offset (output_bfd, info, input_section,
1837 rel->r_offset);
1838 if (outrel.r_offset == (bfd_vma) -1)
1839 skip = TRUE;
1840 else if (outrel.r_offset == (bfd_vma) -2)
1841 skip = TRUE, relocate = TRUE;
1842 outrel.r_offset += (input_section->output_section->vma
1843 + input_section->output_offset);
1845 if (skip)
1846 memset (&outrel, 0, sizeof outrel);
1847 else if (h != NULL
1848 && h->dynindx != -1
1849 && (r_type == R_68K_PC8
1850 || r_type == R_68K_PC16
1851 || r_type == R_68K_PC32
1852 || !info->shared
1853 || !info->symbolic
1854 || !h->def_regular))
1856 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1857 outrel.r_addend = rel->r_addend;
1859 else
1861 /* This symbol is local, or marked to become local. */
1862 if (r_type == R_68K_32)
1864 relocate = TRUE;
1865 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1866 outrel.r_addend = relocation + rel->r_addend;
1868 else
1870 long indx;
1872 if (bfd_is_abs_section (sec))
1873 indx = 0;
1874 else if (sec == NULL || sec->owner == NULL)
1876 bfd_set_error (bfd_error_bad_value);
1877 return FALSE;
1879 else
1881 asection *osec;
1883 osec = sec->output_section;
1884 indx = elf_section_data (osec)->dynindx;
1885 BFD_ASSERT (indx > 0);
1888 outrel.r_info = ELF32_R_INFO (indx, r_type);
1889 outrel.r_addend = relocation + rel->r_addend;
1893 sreloc = elf_section_data (input_section)->sreloc;
1894 if (sreloc == NULL)
1895 abort ();
1897 loc = sreloc->contents;
1898 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1899 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1901 /* This reloc will be computed at runtime, so there's no
1902 need to do anything now, except for R_68K_32
1903 relocations that have been turned into
1904 R_68K_RELATIVE. */
1905 if (!relocate)
1906 continue;
1909 break;
1911 case R_68K_GNU_VTINHERIT:
1912 case R_68K_GNU_VTENTRY:
1913 /* These are no-ops in the end. */
1914 continue;
1916 default:
1917 break;
1920 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1921 because such sections are not SEC_ALLOC and thus ld.so will
1922 not process them. */
1923 if (unresolved_reloc
1924 && !((input_section->flags & SEC_DEBUGGING) != 0
1925 && h->def_dynamic))
1927 (*_bfd_error_handler)
1928 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1929 input_bfd,
1930 input_section,
1931 (long) rel->r_offset,
1932 howto->name,
1933 h->root.root.string);
1934 return FALSE;
1937 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1938 contents, rel->r_offset,
1939 relocation, rel->r_addend);
1941 if (r != bfd_reloc_ok)
1943 const char *name;
1945 if (h != NULL)
1946 name = h->root.root.string;
1947 else
1949 name = bfd_elf_string_from_elf_section (input_bfd,
1950 symtab_hdr->sh_link,
1951 sym->st_name);
1952 if (name == NULL)
1953 return FALSE;
1954 if (*name == '\0')
1955 name = bfd_section_name (input_bfd, sec);
1958 if (r == bfd_reloc_overflow)
1960 if (!(info->callbacks->reloc_overflow
1961 (info, (h ? &h->root : NULL), name, howto->name,
1962 (bfd_vma) 0, input_bfd, input_section,
1963 rel->r_offset)))
1964 return FALSE;
1966 else
1968 (*_bfd_error_handler)
1969 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
1970 input_bfd, input_section,
1971 (long) rel->r_offset, name, (int) r);
1972 return FALSE;
1977 return TRUE;
1980 /* Install an M_68K_PC32 relocation against VALUE at offset OFFSET
1981 into section SEC. */
1983 static void
1984 elf_m68k_install_pc32 (asection *sec, bfd_vma offset, bfd_vma value)
1986 /* Make VALUE PC-relative. */
1987 value -= sec->output_section->vma + offset;
1989 /* Apply any in-place addend. */
1990 value += bfd_get_32 (sec->owner, sec->contents + offset);
1992 bfd_put_32 (sec->owner, value, sec->contents + offset);
1995 /* Finish up dynamic symbol handling. We set the contents of various
1996 dynamic sections here. */
1998 static bfd_boolean
1999 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
2000 bfd *output_bfd;
2001 struct bfd_link_info *info;
2002 struct elf_link_hash_entry *h;
2003 Elf_Internal_Sym *sym;
2005 bfd *dynobj;
2007 dynobj = elf_hash_table (info)->dynobj;
2009 if (h->plt.offset != (bfd_vma) -1)
2011 const struct elf_m68k_plt_info *plt_info;
2012 asection *splt;
2013 asection *sgot;
2014 asection *srela;
2015 bfd_vma plt_index;
2016 bfd_vma got_offset;
2017 Elf_Internal_Rela rela;
2018 bfd_byte *loc;
2020 /* This symbol has an entry in the procedure linkage table. Set
2021 it up. */
2023 BFD_ASSERT (h->dynindx != -1);
2025 plt_info = elf_m68k_hash_table (info)->plt_info;
2026 splt = bfd_get_section_by_name (dynobj, ".plt");
2027 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2028 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2029 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
2031 /* Get the index in the procedure linkage table which
2032 corresponds to this symbol. This is the index of this symbol
2033 in all the symbols for which we are making plt entries. The
2034 first entry in the procedure linkage table is reserved. */
2035 plt_index = (h->plt.offset / plt_info->size) - 1;
2037 /* Get the offset into the .got table of the entry that
2038 corresponds to this function. Each .got entry is 4 bytes.
2039 The first three are reserved. */
2040 got_offset = (plt_index + 3) * 4;
2042 memcpy (splt->contents + h->plt.offset,
2043 plt_info->symbol_entry,
2044 plt_info->size);
2046 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.got,
2047 (sgot->output_section->vma
2048 + sgot->output_offset
2049 + got_offset));
2051 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
2052 splt->contents
2053 + h->plt.offset
2054 + plt_info->symbol_resolve_entry + 2);
2056 elf_m68k_install_pc32 (splt, h->plt.offset + plt_info->symbol_relocs.plt,
2057 splt->output_section->vma);
2059 /* Fill in the entry in the global offset table. */
2060 bfd_put_32 (output_bfd,
2061 (splt->output_section->vma
2062 + splt->output_offset
2063 + h->plt.offset
2064 + plt_info->symbol_resolve_entry),
2065 sgot->contents + got_offset);
2067 /* Fill in the entry in the .rela.plt section. */
2068 rela.r_offset = (sgot->output_section->vma
2069 + sgot->output_offset
2070 + got_offset);
2071 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
2072 rela.r_addend = 0;
2073 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
2074 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2076 if (!h->def_regular)
2078 /* Mark the symbol as undefined, rather than as defined in
2079 the .plt section. Leave the value alone. */
2080 sym->st_shndx = SHN_UNDEF;
2084 if (h->got.offset != (bfd_vma) -1)
2086 asection *sgot;
2087 asection *srela;
2088 Elf_Internal_Rela rela;
2089 bfd_byte *loc;
2091 /* This symbol has an entry in the global offset table. Set it
2092 up. */
2094 sgot = bfd_get_section_by_name (dynobj, ".got");
2095 srela = bfd_get_section_by_name (dynobj, ".rela.got");
2096 BFD_ASSERT (sgot != NULL && srela != NULL);
2098 rela.r_offset = (sgot->output_section->vma
2099 + sgot->output_offset
2100 + (h->got.offset &~ (bfd_vma) 1));
2102 /* If this is a -Bsymbolic link, and the symbol is defined
2103 locally, we just want to emit a RELATIVE reloc. Likewise if
2104 the symbol was forced to be local because of a version file.
2105 The entry in the global offset table will already have been
2106 initialized in the relocate_section function. */
2107 if (info->shared
2108 && (info->symbolic
2109 || h->dynindx == -1
2110 || h->forced_local)
2111 && h->def_regular)
2113 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
2114 rela.r_addend = bfd_get_signed_32 (output_bfd,
2115 (sgot->contents
2116 + (h->got.offset &~ (bfd_vma) 1)));
2118 else
2120 bfd_put_32 (output_bfd, (bfd_vma) 0,
2121 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
2122 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
2123 rela.r_addend = 0;
2126 loc = srela->contents;
2127 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2128 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2131 if (h->needs_copy)
2133 asection *s;
2134 Elf_Internal_Rela rela;
2135 bfd_byte *loc;
2137 /* This symbol needs a copy reloc. Set it up. */
2139 BFD_ASSERT (h->dynindx != -1
2140 && (h->root.type == bfd_link_hash_defined
2141 || h->root.type == bfd_link_hash_defweak));
2143 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2144 ".rela.bss");
2145 BFD_ASSERT (s != NULL);
2147 rela.r_offset = (h->root.u.def.value
2148 + h->root.u.def.section->output_section->vma
2149 + h->root.u.def.section->output_offset);
2150 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
2151 rela.r_addend = 0;
2152 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2153 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2156 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2157 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2158 || h == elf_hash_table (info)->hgot)
2159 sym->st_shndx = SHN_ABS;
2161 return TRUE;
2164 /* Finish up the dynamic sections. */
2166 static bfd_boolean
2167 elf_m68k_finish_dynamic_sections (output_bfd, info)
2168 bfd *output_bfd;
2169 struct bfd_link_info *info;
2171 bfd *dynobj;
2172 asection *sgot;
2173 asection *sdyn;
2175 dynobj = elf_hash_table (info)->dynobj;
2177 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2178 BFD_ASSERT (sgot != NULL);
2179 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2181 if (elf_hash_table (info)->dynamic_sections_created)
2183 asection *splt;
2184 Elf32_External_Dyn *dyncon, *dynconend;
2186 splt = bfd_get_section_by_name (dynobj, ".plt");
2187 BFD_ASSERT (splt != NULL && sdyn != NULL);
2189 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2190 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2191 for (; dyncon < dynconend; dyncon++)
2193 Elf_Internal_Dyn dyn;
2194 const char *name;
2195 asection *s;
2197 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2199 switch (dyn.d_tag)
2201 default:
2202 break;
2204 case DT_PLTGOT:
2205 name = ".got";
2206 goto get_vma;
2207 case DT_JMPREL:
2208 name = ".rela.plt";
2209 get_vma:
2210 s = bfd_get_section_by_name (output_bfd, name);
2211 BFD_ASSERT (s != NULL);
2212 dyn.d_un.d_ptr = s->vma;
2213 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2214 break;
2216 case DT_PLTRELSZ:
2217 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2218 BFD_ASSERT (s != NULL);
2219 dyn.d_un.d_val = s->size;
2220 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2221 break;
2223 case DT_RELASZ:
2224 /* The procedure linkage table relocs (DT_JMPREL) should
2225 not be included in the overall relocs (DT_RELA).
2226 Therefore, we override the DT_RELASZ entry here to
2227 make it not include the JMPREL relocs. Since the
2228 linker script arranges for .rela.plt to follow all
2229 other relocation sections, we don't have to worry
2230 about changing the DT_RELA entry. */
2231 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2232 if (s != NULL)
2233 dyn.d_un.d_val -= s->size;
2234 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2235 break;
2239 /* Fill in the first entry in the procedure linkage table. */
2240 if (splt->size > 0)
2242 const struct elf_m68k_plt_info *plt_info;
2244 plt_info = elf_m68k_hash_table (info)->plt_info;
2245 memcpy (splt->contents, plt_info->plt0_entry, plt_info->size);
2247 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got4,
2248 (sgot->output_section->vma
2249 + sgot->output_offset
2250 + 4));
2252 elf_m68k_install_pc32 (splt, plt_info->plt0_relocs.got8,
2253 (sgot->output_section->vma
2254 + sgot->output_offset
2255 + 8));
2257 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2258 = plt_info->size;
2262 /* Fill in the first three entries in the global offset table. */
2263 if (sgot->size > 0)
2265 if (sdyn == NULL)
2266 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2267 else
2268 bfd_put_32 (output_bfd,
2269 sdyn->output_section->vma + sdyn->output_offset,
2270 sgot->contents);
2271 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2272 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2275 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2277 return TRUE;
2280 /* Given a .data section and a .emreloc in-memory section, store
2281 relocation information into the .emreloc section which can be
2282 used at runtime to relocate the section. This is called by the
2283 linker when the --embedded-relocs switch is used. This is called
2284 after the add_symbols entry point has been called for all the
2285 objects, and before the final_link entry point is called. */
2287 bfd_boolean
2288 bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2289 bfd *abfd;
2290 struct bfd_link_info *info;
2291 asection *datasec;
2292 asection *relsec;
2293 char **errmsg;
2295 Elf_Internal_Shdr *symtab_hdr;
2296 Elf_Internal_Sym *isymbuf = NULL;
2297 Elf_Internal_Rela *internal_relocs = NULL;
2298 Elf_Internal_Rela *irel, *irelend;
2299 bfd_byte *p;
2300 bfd_size_type amt;
2302 BFD_ASSERT (! info->relocatable);
2304 *errmsg = NULL;
2306 if (datasec->reloc_count == 0)
2307 return TRUE;
2309 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2311 /* Get a copy of the native relocations. */
2312 internal_relocs = (_bfd_elf_link_read_relocs
2313 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2314 info->keep_memory));
2315 if (internal_relocs == NULL)
2316 goto error_return;
2318 amt = (bfd_size_type) datasec->reloc_count * 12;
2319 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
2320 if (relsec->contents == NULL)
2321 goto error_return;
2323 p = relsec->contents;
2325 irelend = internal_relocs + datasec->reloc_count;
2326 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2328 asection *targetsec;
2330 /* We are going to write a four byte longword into the runtime
2331 reloc section. The longword will be the address in the data
2332 section which must be relocated. It is followed by the name
2333 of the target section NUL-padded or truncated to 8
2334 characters. */
2336 /* We can only relocate absolute longword relocs at run time. */
2337 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2339 *errmsg = _("unsupported reloc type");
2340 bfd_set_error (bfd_error_bad_value);
2341 goto error_return;
2344 /* Get the target section referred to by the reloc. */
2345 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2347 /* A local symbol. */
2348 Elf_Internal_Sym *isym;
2350 /* Read this BFD's local symbols if we haven't done so already. */
2351 if (isymbuf == NULL)
2353 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2354 if (isymbuf == NULL)
2355 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2356 symtab_hdr->sh_info, 0,
2357 NULL, NULL, NULL);
2358 if (isymbuf == NULL)
2359 goto error_return;
2362 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2363 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2365 else
2367 unsigned long indx;
2368 struct elf_link_hash_entry *h;
2370 /* An external symbol. */
2371 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2372 h = elf_sym_hashes (abfd)[indx];
2373 BFD_ASSERT (h != NULL);
2374 if (h->root.type == bfd_link_hash_defined
2375 || h->root.type == bfd_link_hash_defweak)
2376 targetsec = h->root.u.def.section;
2377 else
2378 targetsec = NULL;
2381 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2382 memset (p + 4, 0, 8);
2383 if (targetsec != NULL)
2384 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
2387 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2388 free (isymbuf);
2389 if (internal_relocs != NULL
2390 && elf_section_data (datasec)->relocs != internal_relocs)
2391 free (internal_relocs);
2392 return TRUE;
2394 error_return:
2395 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2396 free (isymbuf);
2397 if (internal_relocs != NULL
2398 && elf_section_data (datasec)->relocs != internal_relocs)
2399 free (internal_relocs);
2400 return FALSE;
2403 static enum elf_reloc_type_class
2404 elf32_m68k_reloc_type_class (rela)
2405 const Elf_Internal_Rela *rela;
2407 switch ((int) ELF32_R_TYPE (rela->r_info))
2409 case R_68K_RELATIVE:
2410 return reloc_class_relative;
2411 case R_68K_JMP_SLOT:
2412 return reloc_class_plt;
2413 case R_68K_COPY:
2414 return reloc_class_copy;
2415 default:
2416 return reloc_class_normal;
2420 /* Return address for Ith PLT stub in section PLT, for relocation REL
2421 or (bfd_vma) -1 if it should not be included. */
2423 static bfd_vma
2424 elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
2425 const arelent *rel ATTRIBUTE_UNUSED)
2427 return plt->vma + (i + 1) * elf_m68k_get_plt_info (plt->owner)->size;
2430 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
2431 #define TARGET_BIG_NAME "elf32-m68k"
2432 #define ELF_MACHINE_CODE EM_68K
2433 #define ELF_MAXPAGESIZE 0x2000
2434 #define elf_backend_create_dynamic_sections \
2435 _bfd_elf_create_dynamic_sections
2436 #define bfd_elf32_bfd_link_hash_table_create \
2437 elf_m68k_link_hash_table_create
2438 #define bfd_elf32_bfd_final_link bfd_elf_gc_common_final_link
2440 #define elf_backend_check_relocs elf_m68k_check_relocs
2441 #define elf_backend_always_size_sections \
2442 elf_m68k_always_size_sections
2443 #define elf_backend_adjust_dynamic_symbol \
2444 elf_m68k_adjust_dynamic_symbol
2445 #define elf_backend_size_dynamic_sections \
2446 elf_m68k_size_dynamic_sections
2447 #define elf_backend_relocate_section elf_m68k_relocate_section
2448 #define elf_backend_finish_dynamic_symbol \
2449 elf_m68k_finish_dynamic_symbol
2450 #define elf_backend_finish_dynamic_sections \
2451 elf_m68k_finish_dynamic_sections
2452 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
2453 #define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
2454 #define bfd_elf32_bfd_merge_private_bfd_data \
2455 elf32_m68k_merge_private_bfd_data
2456 #define bfd_elf32_bfd_set_private_flags \
2457 elf32_m68k_set_private_flags
2458 #define bfd_elf32_bfd_print_private_bfd_data \
2459 elf32_m68k_print_private_bfd_data
2460 #define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
2461 #define elf_backend_plt_sym_val elf_m68k_plt_sym_val
2462 #define elf_backend_object_p elf32_m68k_object_p
2464 #define elf_backend_can_gc_sections 1
2465 #define elf_backend_can_refcount 1
2466 #define elf_backend_want_got_plt 1
2467 #define elf_backend_plt_readonly 1
2468 #define elf_backend_want_plt_sym 0
2469 #define elf_backend_got_header_size 12
2470 #define elf_backend_rela_normal 1
2472 #include "elf32-target.h"