5e69df5c9b0f42011ef410e6720608af012b2cc2
[deliverable/binutils-gdb.git] / bfd / elf32-m68k.c
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.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
20
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"
28
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 *));
60
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 *));
69
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),
122 };
123
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;
129 {
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)];
132 }
133
134 #define elf_info_to_howto rtype_to_howto
135
136 static const struct
137 {
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 },
167 };
168
169 static reloc_howto_type *
170 reloc_type_lookup (abfd, code)
171 bfd *abfd ATTRIBUTE_UNUSED;
172 bfd_reloc_code_real_type code;
173 {
174 unsigned int i;
175 for (i = 0; i < sizeof (reloc_map) / sizeof (reloc_map[0]); i++)
176 {
177 if (reloc_map[i].bfd_val == code)
178 return &howto_table[reloc_map[i].elf_val];
179 }
180 return 0;
181 }
182
183 #define bfd_elf32_bfd_reloc_type_lookup reloc_type_lookup
184 #define ELF_ARCH bfd_arch_m68k
185 \f
186 /* Functions for the m68k ELF linker. */
187
188 /* The name of the dynamic interpreter. This is put in the .interp
189 section. */
190
191 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
192
193 /* The size in bytes of an entry in the procedure linkage table. */
194
195 #define PLT_ENTRY_SIZE 20
196
197 /* The first entry in a procedure linkage table looks like this. See
198 the SVR4 ABI m68k supplement to see how this works. */
199
200 static const bfd_byte elf_m68k_plt0_entry[PLT_ENTRY_SIZE] =
201 {
202 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
203 0, 0, 0, 0, /* replaced with offset to .got + 4. */
204 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,addr]) */
205 0, 0, 0, 0, /* replaced with offset to .got + 8. */
206 0, 0, 0, 0 /* pad out to 20 bytes. */
207 };
208
209 /* Subsequent entries in a procedure linkage table look like this. */
210
211 static const bfd_byte elf_m68k_plt_entry[PLT_ENTRY_SIZE] =
212 {
213 0x4e, 0xfb, 0x01, 0x71, /* jmp ([%pc,symbol@GOTPC]) */
214 0, 0, 0, 0, /* replaced with offset to symbol's .got entry. */
215 0x2f, 0x3c, /* move.l #offset,-(%sp) */
216 0, 0, 0, 0, /* replaced with offset into relocation table. */
217 0x60, 0xff, /* bra.l .plt */
218 0, 0, 0, 0 /* replaced with offset to start of .plt. */
219 };
220
221
222 #define CFV4E_PLT_ENTRY_SIZE 24
223
224 #define CFV4E_FLAG(abfd) (elf_elfheader (abfd)->e_flags & EF_M68K_CFV4E)
225
226 static const bfd_byte elf_cfv4e_plt0_entry[CFV4E_PLT_ENTRY_SIZE] =
227 {
228 0x20, 0x3c,
229 0, 0, 0, 0, /* Replaced with offset to .got + 4. */
230 0x2f, 0x3b, 0x08, 0xfa, /* move.l (%pc,addr),-(%sp) */
231 0x20, 0x3c,
232 0, 0, 0, 0, /* Replaced with offset to .got + 8. */
233 0x20, 0x7b, 0x08, 0x00, /* move.l (%pc,%d0:l), %a0 */
234 0x4e, 0xd0, /* jmp (%a0) */
235 0x4e, 0x71 /* nop */
236 };
237
238 /* Subsequent entries in a procedure linkage table look like this. */
239
240 static const bfd_byte elf_cfv4e_plt_entry[CFV4E_PLT_ENTRY_SIZE] =
241 {
242 0x20, 0x3c,
243 0, 0, 0, 0, /* Replaced with offset to symbol's .got entry. */
244 0x20, 0x7b, 0x08, 0x00, /* move.l (%pc,%d0:l), %a0 */
245 0x4e, 0xd0, /* jmp (%a0) */
246 0x2f, 0x3c, /* move.l #offset,-(%sp) */
247 0, 0, 0, 0, /* Replaced with offset into relocation table. */
248 0x60, 0xff, /* bra.l .plt */
249 0, 0, 0, 0 /* Replaced with offset to start of .plt. */
250 };
251
252 #define CPU32_FLAG(abfd) (elf_elfheader (abfd)->e_flags & EF_M68K_CPU32)
253
254 #define PLT_CPU32_ENTRY_SIZE 24
255 /* Procedure linkage table entries for the cpu32 */
256 static const bfd_byte elf_cpu32_plt0_entry[PLT_CPU32_ENTRY_SIZE] =
257 {
258 0x2f, 0x3b, 0x01, 0x70, /* move.l (%pc,addr),-(%sp) */
259 0, 0, 0, 0, /* replaced with offset to .got + 4. */
260 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
261 0, 0, 0, 0, /* replace with offset to .got +8. */
262 0x4e, 0xd1, /* jmp %a1@ */
263 0, 0, 0, 0, /* pad out to 24 bytes. */
264 0, 0
265 };
266
267 static const bfd_byte elf_cpu32_plt_entry[PLT_CPU32_ENTRY_SIZE] =
268 {
269 0x22, 0x7b, 0x01, 0x70, /* moveal %pc@(0xc), %a1 */
270 0, 0, 0, 0, /* replaced with offset to symbol's .got entry. */
271 0x4e, 0xd1, /* jmp %a1@ */
272 0x2f, 0x3c, /* move.l #offset,-(%sp) */
273 0, 0, 0, 0, /* replaced with offset into relocation table. */
274 0x60, 0xff, /* bra.l .plt */
275 0, 0, 0, 0, /* replaced with offset to start of .plt. */
276 0, 0
277 };
278
279 /* The m68k linker needs to keep track of the number of relocs that it
280 decides to copy in check_relocs for each symbol. This is so that it
281 can discard PC relative relocs if it doesn't need them when linking
282 with -Bsymbolic. We store the information in a field extending the
283 regular ELF linker hash table. */
284
285 /* This structure keeps track of the number of PC relative relocs we have
286 copied for a given symbol. */
287
288 struct elf_m68k_pcrel_relocs_copied
289 {
290 /* Next section. */
291 struct elf_m68k_pcrel_relocs_copied *next;
292 /* A section in dynobj. */
293 asection *section;
294 /* Number of relocs copied in this section. */
295 bfd_size_type count;
296 };
297
298 /* m68k ELF linker hash entry. */
299
300 struct elf_m68k_link_hash_entry
301 {
302 struct elf_link_hash_entry root;
303
304 /* Number of PC relative relocs copied for this symbol. */
305 struct elf_m68k_pcrel_relocs_copied *pcrel_relocs_copied;
306 };
307
308 #define elf_m68k_hash_entry(ent) ((struct elf_m68k_link_hash_entry *) (ent))
309
310 /* m68k ELF linker hash table. */
311
312 struct elf_m68k_link_hash_table
313 {
314 struct elf_link_hash_table root;
315
316 /* Small local sym to section mapping cache. */
317 struct sym_sec_cache sym_sec;
318 };
319
320 /* Get the m68k ELF linker hash table from a link_info structure. */
321
322 #define elf_m68k_hash_table(p) \
323 ((struct elf_m68k_link_hash_table *) (p)->hash)
324
325 /* Create an entry in an m68k ELF linker hash table. */
326
327 static struct bfd_hash_entry *
328 elf_m68k_link_hash_newfunc (entry, table, string)
329 struct bfd_hash_entry *entry;
330 struct bfd_hash_table *table;
331 const char *string;
332 {
333 struct bfd_hash_entry *ret = entry;
334
335 /* Allocate the structure if it has not already been allocated by a
336 subclass. */
337 if (ret == NULL)
338 ret = bfd_hash_allocate (table,
339 sizeof (struct elf_m68k_link_hash_entry));
340 if (ret == NULL)
341 return ret;
342
343 /* Call the allocation method of the superclass. */
344 ret = _bfd_elf_link_hash_newfunc (ret, table, string);
345 if (ret != NULL)
346 elf_m68k_hash_entry (ret)->pcrel_relocs_copied = NULL;
347
348 return ret;
349 }
350
351 /* Create an m68k ELF linker hash table. */
352
353 static struct bfd_link_hash_table *
354 elf_m68k_link_hash_table_create (abfd)
355 bfd *abfd;
356 {
357 struct elf_m68k_link_hash_table *ret;
358 bfd_size_type amt = sizeof (struct elf_m68k_link_hash_table);
359
360 ret = (struct elf_m68k_link_hash_table *) bfd_malloc (amt);
361 if (ret == (struct elf_m68k_link_hash_table *) NULL)
362 return NULL;
363
364 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
365 elf_m68k_link_hash_newfunc,
366 sizeof (struct elf_m68k_link_hash_entry)))
367 {
368 free (ret);
369 return NULL;
370 }
371
372 ret->sym_sec.abfd = NULL;
373
374 return &ret->root.root;
375 }
376
377 /* Set the right machine number. */
378
379 static bfd_boolean
380 elf32_m68k_object_p (bfd *abfd)
381 {
382 unsigned int mach = 0;
383 unsigned features = 0;
384 flagword eflags = elf_elfheader (abfd)->e_flags;
385
386 if (eflags & EF_M68K_M68000)
387 features |= m68000;
388 else if (eflags & EF_M68K_CPU32)
389 features |= cpu32;
390 else if (eflags & EF_M68K_ISA_MASK)
391 {
392 switch (eflags & EF_M68K_ISA_MASK)
393 {
394 case EF_M68K_ISA_A_NODIV:
395 features |= mcfisa_a;
396 break;
397 case EF_M68K_ISA_A:
398 features |= mcfisa_a|mcfhwdiv;
399 break;
400 case EF_M68K_ISA_A_PLUS:
401 features |= mcfisa_a|mcfisa_aa|mcfhwdiv|mcfusp;
402 break;
403 case EF_M68K_ISA_B_NOUSP:
404 features |= mcfisa_a|mcfisa_b|mcfhwdiv;
405 break;
406 case EF_M68K_ISA_B:
407 features |= mcfisa_a|mcfisa_b|mcfhwdiv|mcfusp;
408 break;
409 }
410 switch (eflags & EF_M68K_MAC_MASK)
411 {
412 case EF_M68K_MAC:
413 features |= mcfmac;
414 break;
415 case EF_M68K_EMAC:
416 features |= mcfemac;
417 break;
418 }
419 if (eflags & EF_M68K_FLOAT)
420 features |= cfloat;
421 }
422
423 mach = bfd_m68k_features_to_mach (features);
424 bfd_default_set_arch_mach (abfd, bfd_arch_m68k, mach);
425
426 return TRUE;
427 }
428
429 /* Keep m68k-specific flags in the ELF header. */
430 static bfd_boolean
431 elf32_m68k_set_private_flags (abfd, flags)
432 bfd *abfd;
433 flagword flags;
434 {
435 elf_elfheader (abfd)->e_flags = flags;
436 elf_flags_init (abfd) = TRUE;
437 return TRUE;
438 }
439
440 /* Merge backend specific data from an object file to the output
441 object file when linking. */
442 static bfd_boolean
443 elf32_m68k_merge_private_bfd_data (ibfd, obfd)
444 bfd *ibfd;
445 bfd *obfd;
446 {
447 flagword out_flags;
448 flagword in_flags;
449 unsigned in_mach, out_mach;
450
451 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
452 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
453 return FALSE;
454
455 in_mach = bfd_get_mach (ibfd);
456 out_mach = bfd_get_mach (obfd);
457 if (!out_mach || !in_mach)
458 /* One is unknown, copy the input machine. */
459 out_mach = in_mach;
460 else if (in_mach != out_mach)
461 {
462 if (in_mach <= bfd_mach_m68060 && out_mach <= bfd_mach_m68060)
463 {
464 /* Merge m68k machine. */
465 if (in_mach > out_mach)
466 out_mach = in_mach;
467 }
468 else if (in_mach >= bfd_mach_mcf_isa_a_nodiv
469 && out_mach >= bfd_mach_mcf_isa_a_nodiv)
470 /* Merge cf machine. */
471 out_mach = bfd_m68k_features_to_mach
472 (bfd_m68k_mach_to_features (in_mach)
473 | bfd_m68k_mach_to_features (out_mach));
474 else
475 /* They are incompatible. */
476 return FALSE;
477 }
478 bfd_set_arch_mach (obfd, bfd_arch_m68k, out_mach);
479
480 in_flags = elf_elfheader (ibfd)->e_flags;
481 out_flags = elf_elfheader (obfd)->e_flags;
482
483 if (!elf_flags_init (obfd))
484 {
485 elf_flags_init (obfd) = TRUE;
486 out_flags = in_flags;
487 }
488 else
489 {
490 flagword isa_in = in_flags & EF_M68K_ISA_MASK;
491 flagword isa_out = out_flags & EF_M68K_ISA_MASK;
492
493
494 /* Copy legacy flags. */
495 out_flags |= in_flags & (EF_M68K_CPU32 | EF_M68K_M68000 | EF_M68K_CFV4E);
496
497 if ((isa_in | isa_out)
498 && ((in_flags | out_flags) & (EF_M68K_CPU32 | EF_M68K_M68000)))
499 /* Mixing m68k and cf is not allowed */
500 return FALSE;
501
502 if (isa_in)
503 {
504 if (isa_out)
505 {
506 if (isa_out == EF_M68K_ISA_A_PLUS
507 && (isa_in == EF_M68K_ISA_B_NOUSP
508 || isa_in == EF_M68K_ISA_B))
509 /* Cannot mix A+ and B */
510 return FALSE;
511 if (isa_in == EF_M68K_ISA_A_PLUS
512 && (isa_out == EF_M68K_ISA_B_NOUSP
513 || isa_out == EF_M68K_ISA_B))
514 /* Cannot mix B and A+ */
515 return FALSE;
516
517 if (isa_in > isa_out)
518 out_flags ^= isa_in ^ isa_out;
519
520 out_flags |= in_flags & EF_M68K_FLOAT;
521 if (in_flags & EF_M68K_MAC_MASK)
522 {
523 if (!(out_flags & EF_M68K_MAC_MASK))
524 out_flags |= in_flags & EF_M68K_MAC_MASK;
525 else if ((out_flags & EF_M68K_MAC_MASK)
526 != (in_flags & EF_M68K_MAC_MASK))
527 /* Cannot mix MACs */
528 return FALSE;
529 }
530 }
531 else
532 {
533 /* Copy the coldfire bits. */
534 out_flags &= ~EF_M68K_CF_MASK;
535 out_flags |= in_flags & EF_M68K_CF_MASK;
536 }
537 }
538 }
539 elf_elfheader (obfd)->e_flags = out_flags;
540
541 return TRUE;
542 }
543
544 /* Display the flags field. */
545 static bfd_boolean
546 elf32_m68k_print_private_bfd_data (abfd, ptr)
547 bfd *abfd;
548 PTR ptr;
549 {
550 FILE *file = (FILE *) ptr;
551 flagword eflags = elf_elfheader (abfd)->e_flags;
552
553 BFD_ASSERT (abfd != NULL && ptr != NULL);
554
555 /* Print normal ELF private data. */
556 _bfd_elf_print_private_bfd_data (abfd, ptr);
557
558 /* Ignore init flag - it may not be set, despite the flags field containing valid data. */
559
560 /* xgettext:c-format */
561 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
562
563 if (eflags & EF_M68K_CPU32)
564 fprintf (file, " [cpu32]");
565
566 if (eflags & EF_M68K_M68000)
567 fprintf (file, " [m68000]");
568
569 if (eflags & EF_M68K_CFV4E)
570 fprintf (file, " [cfv4e]");
571
572 if (eflags & EF_M68K_ISA_MASK)
573 {
574 char const *isa = _("unknown");
575 char const *mac = _("unknown");
576 char const *additional = "";
577
578 switch (eflags & EF_M68K_ISA_MASK)
579 {
580 case EF_M68K_ISA_A_NODIV:
581 isa = "A";
582 additional = " [nodiv]";
583 break;
584 case EF_M68K_ISA_A:
585 isa = "A";
586 break;
587 case EF_M68K_ISA_A_PLUS:
588 isa = "A+";
589 break;
590 case EF_M68K_ISA_B_NOUSP:
591 isa = "B";
592 additional = " [nousp]";
593 break;
594 case EF_M68K_ISA_B:
595 isa = "B";
596 break;
597 }
598 fprintf (file, " [isa %s]%s", isa, additional);
599 if (eflags & EF_M68K_FLOAT)
600 fprintf (file, " [float]");
601 switch (eflags & EF_M68K_MAC_MASK)
602 {
603 case 0:
604 mac = NULL;
605 break;
606 case EF_M68K_MAC:
607 mac = "mac";
608 break;
609 case EF_M68K_EMAC:
610 mac = "emac";
611 break;
612 }
613 if (mac)
614 fprintf (file, " [%s]", mac);
615 }
616
617 fputc ('\n', file);
618
619 return TRUE;
620 }
621 /* Look through the relocs for a section during the first phase, and
622 allocate space in the global offset table or procedure linkage
623 table. */
624
625 static bfd_boolean
626 elf_m68k_check_relocs (abfd, info, sec, relocs)
627 bfd *abfd;
628 struct bfd_link_info *info;
629 asection *sec;
630 const Elf_Internal_Rela *relocs;
631 {
632 bfd *dynobj;
633 Elf_Internal_Shdr *symtab_hdr;
634 struct elf_link_hash_entry **sym_hashes;
635 bfd_signed_vma *local_got_refcounts;
636 const Elf_Internal_Rela *rel;
637 const Elf_Internal_Rela *rel_end;
638 asection *sgot;
639 asection *srelgot;
640 asection *sreloc;
641
642 if (info->relocatable)
643 return TRUE;
644
645 dynobj = elf_hash_table (info)->dynobj;
646 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
647 sym_hashes = elf_sym_hashes (abfd);
648 local_got_refcounts = elf_local_got_refcounts (abfd);
649
650 sgot = NULL;
651 srelgot = NULL;
652 sreloc = NULL;
653
654 rel_end = relocs + sec->reloc_count;
655 for (rel = relocs; rel < rel_end; rel++)
656 {
657 unsigned long r_symndx;
658 struct elf_link_hash_entry *h;
659
660 r_symndx = ELF32_R_SYM (rel->r_info);
661
662 if (r_symndx < symtab_hdr->sh_info)
663 h = NULL;
664 else
665 {
666 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
667 while (h->root.type == bfd_link_hash_indirect
668 || h->root.type == bfd_link_hash_warning)
669 h = (struct elf_link_hash_entry *) h->root.u.i.link;
670 }
671
672 switch (ELF32_R_TYPE (rel->r_info))
673 {
674 case R_68K_GOT8:
675 case R_68K_GOT16:
676 case R_68K_GOT32:
677 if (h != NULL
678 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
679 break;
680 /* Fall through. */
681 case R_68K_GOT8O:
682 case R_68K_GOT16O:
683 case R_68K_GOT32O:
684 /* This symbol requires a global offset table entry. */
685
686 if (dynobj == NULL)
687 {
688 /* Create the .got section. */
689 elf_hash_table (info)->dynobj = dynobj = abfd;
690 if (!_bfd_elf_create_got_section (dynobj, info))
691 return FALSE;
692 }
693
694 if (sgot == NULL)
695 {
696 sgot = bfd_get_section_by_name (dynobj, ".got");
697 BFD_ASSERT (sgot != NULL);
698 }
699
700 if (srelgot == NULL
701 && (h != NULL || info->shared))
702 {
703 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
704 if (srelgot == NULL)
705 {
706 srelgot = bfd_make_section_with_flags (dynobj,
707 ".rela.got",
708 (SEC_ALLOC
709 | SEC_LOAD
710 | SEC_HAS_CONTENTS
711 | SEC_IN_MEMORY
712 | SEC_LINKER_CREATED
713 | SEC_READONLY));
714 if (srelgot == NULL
715 || !bfd_set_section_alignment (dynobj, srelgot, 2))
716 return FALSE;
717 }
718 }
719
720 if (h != NULL)
721 {
722 if (h->got.refcount == 0)
723 {
724 /* Make sure this symbol is output as a dynamic symbol. */
725 if (h->dynindx == -1
726 && !h->forced_local)
727 {
728 if (!bfd_elf_link_record_dynamic_symbol (info, h))
729 return FALSE;
730 }
731
732 /* Allocate space in the .got section. */
733 sgot->size += 4;
734 /* Allocate relocation space. */
735 srelgot->size += sizeof (Elf32_External_Rela);
736 }
737 h->got.refcount++;
738 }
739 else
740 {
741 /* This is a global offset table entry for a local symbol. */
742 if (local_got_refcounts == NULL)
743 {
744 bfd_size_type size;
745
746 size = symtab_hdr->sh_info;
747 size *= sizeof (bfd_signed_vma);
748 local_got_refcounts = ((bfd_signed_vma *)
749 bfd_zalloc (abfd, size));
750 if (local_got_refcounts == NULL)
751 return FALSE;
752 elf_local_got_refcounts (abfd) = local_got_refcounts;
753 }
754 if (local_got_refcounts[r_symndx] == 0)
755 {
756 sgot->size += 4;
757 if (info->shared)
758 {
759 /* If we are generating a shared object, we need to
760 output a R_68K_RELATIVE reloc so that the dynamic
761 linker can adjust this GOT entry. */
762 srelgot->size += sizeof (Elf32_External_Rela);
763 }
764 }
765 local_got_refcounts[r_symndx]++;
766 }
767 break;
768
769 case R_68K_PLT8:
770 case R_68K_PLT16:
771 case R_68K_PLT32:
772 /* This symbol requires a procedure linkage table entry. We
773 actually build the entry in adjust_dynamic_symbol,
774 because this might be a case of linking PIC code which is
775 never referenced by a dynamic object, in which case we
776 don't need to generate a procedure linkage table entry
777 after all. */
778
779 /* If this is a local symbol, we resolve it directly without
780 creating a procedure linkage table entry. */
781 if (h == NULL)
782 continue;
783
784 h->needs_plt = 1;
785 h->plt.refcount++;
786 break;
787
788 case R_68K_PLT8O:
789 case R_68K_PLT16O:
790 case R_68K_PLT32O:
791 /* This symbol requires a procedure linkage table entry. */
792
793 if (h == NULL)
794 {
795 /* It does not make sense to have this relocation for a
796 local symbol. FIXME: does it? How to handle it if
797 it does make sense? */
798 bfd_set_error (bfd_error_bad_value);
799 return FALSE;
800 }
801
802 /* Make sure this symbol is output as a dynamic symbol. */
803 if (h->dynindx == -1
804 && !h->forced_local)
805 {
806 if (!bfd_elf_link_record_dynamic_symbol (info, h))
807 return FALSE;
808 }
809
810 h->needs_plt = 1;
811 h->plt.refcount++;
812 break;
813
814 case R_68K_PC8:
815 case R_68K_PC16:
816 case R_68K_PC32:
817 /* If we are creating a shared library and this is not a local
818 symbol, we need to copy the reloc into the shared library.
819 However when linking with -Bsymbolic and this is a global
820 symbol which is defined in an object we are including in the
821 link (i.e., DEF_REGULAR is set), then we can resolve the
822 reloc directly. At this point we have not seen all the input
823 files, so it is possible that DEF_REGULAR is not set now but
824 will be set later (it is never cleared). We account for that
825 possibility below by storing information in the
826 pcrel_relocs_copied field of the hash table entry. */
827 if (!(info->shared
828 && (sec->flags & SEC_ALLOC) != 0
829 && h != NULL
830 && (!info->symbolic
831 || h->root.type == bfd_link_hash_defweak
832 || !h->def_regular)))
833 {
834 if (h != NULL)
835 {
836 /* Make sure a plt entry is created for this symbol if
837 it turns out to be a function defined by a dynamic
838 object. */
839 h->plt.refcount++;
840 }
841 break;
842 }
843 /* Fall through. */
844 case R_68K_8:
845 case R_68K_16:
846 case R_68K_32:
847 if (h != NULL)
848 {
849 /* Make sure a plt entry is created for this symbol if it
850 turns out to be a function defined by a dynamic object. */
851 h->plt.refcount++;
852 }
853
854 /* If we are creating a shared library, we need to copy the
855 reloc into the shared library. */
856 if (info->shared
857 && (sec->flags & SEC_ALLOC) != 0)
858 {
859 /* When creating a shared object, we must copy these
860 reloc types into the output file. We create a reloc
861 section in dynobj and make room for this reloc. */
862 if (sreloc == NULL)
863 {
864 const char *name;
865
866 name = (bfd_elf_string_from_elf_section
867 (abfd,
868 elf_elfheader (abfd)->e_shstrndx,
869 elf_section_data (sec)->rel_hdr.sh_name));
870 if (name == NULL)
871 return FALSE;
872
873 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
874 && strcmp (bfd_get_section_name (abfd, sec),
875 name + 5) == 0);
876
877 sreloc = bfd_get_section_by_name (dynobj, name);
878 if (sreloc == NULL)
879 {
880 sreloc = bfd_make_section_with_flags (dynobj,
881 name,
882 (SEC_ALLOC
883 | SEC_LOAD
884 | SEC_HAS_CONTENTS
885 | SEC_IN_MEMORY
886 | SEC_LINKER_CREATED
887 | SEC_READONLY));
888 if (sreloc == NULL
889 || !bfd_set_section_alignment (dynobj, sreloc, 2))
890 return FALSE;
891 }
892 elf_section_data (sec)->sreloc = sreloc;
893 }
894
895 if (sec->flags & SEC_READONLY
896 /* Don't set DF_TEXTREL yet for PC relative
897 relocations, they might be discarded later. */
898 && !(ELF32_R_TYPE (rel->r_info) == R_68K_PC8
899 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
900 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32))
901 info->flags |= DF_TEXTREL;
902
903 sreloc->size += sizeof (Elf32_External_Rela);
904
905 /* We count the number of PC relative relocations we have
906 entered for this symbol, so that we can discard them
907 again if, in the -Bsymbolic case, the symbol is later
908 defined by a regular object, or, in the normal shared
909 case, the symbol is forced to be local. Note that this
910 function is only called if we are using an m68kelf linker
911 hash table, which means that h is really a pointer to an
912 elf_m68k_link_hash_entry. */
913 if (ELF32_R_TYPE (rel->r_info) == R_68K_PC8
914 || ELF32_R_TYPE (rel->r_info) == R_68K_PC16
915 || ELF32_R_TYPE (rel->r_info) == R_68K_PC32)
916 {
917 struct elf_m68k_pcrel_relocs_copied *p;
918 struct elf_m68k_pcrel_relocs_copied **head;
919
920 if (h != NULL)
921 {
922 struct elf_m68k_link_hash_entry *eh
923 = elf_m68k_hash_entry (h);
924 head = &eh->pcrel_relocs_copied;
925 }
926 else
927 {
928 asection *s;
929 void *vpp;
930
931 s = (bfd_section_from_r_symndx
932 (abfd, &elf_m68k_hash_table (info)->sym_sec,
933 sec, r_symndx));
934 if (s == NULL)
935 return FALSE;
936
937 vpp = &elf_section_data (s)->local_dynrel;
938 head = (struct elf_m68k_pcrel_relocs_copied **) vpp;
939 }
940
941 for (p = *head; p != NULL; p = p->next)
942 if (p->section == sreloc)
943 break;
944
945 if (p == NULL)
946 {
947 p = ((struct elf_m68k_pcrel_relocs_copied *)
948 bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
949 if (p == NULL)
950 return FALSE;
951 p->next = *head;
952 *head = p;
953 p->section = sreloc;
954 p->count = 0;
955 }
956
957 ++p->count;
958 }
959 }
960
961 break;
962
963 /* This relocation describes the C++ object vtable hierarchy.
964 Reconstruct it for later use during GC. */
965 case R_68K_GNU_VTINHERIT:
966 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
967 return FALSE;
968 break;
969
970 /* This relocation describes which C++ vtable entries are actually
971 used. Record for later use during GC. */
972 case R_68K_GNU_VTENTRY:
973 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
974 return FALSE;
975 break;
976
977 default:
978 break;
979 }
980 }
981
982 return TRUE;
983 }
984
985 /* Return the section that should be marked against GC for a given
986 relocation. */
987
988 static asection *
989 elf_m68k_gc_mark_hook (sec, info, rel, h, sym)
990 asection *sec;
991 struct bfd_link_info *info ATTRIBUTE_UNUSED;
992 Elf_Internal_Rela *rel;
993 struct elf_link_hash_entry *h;
994 Elf_Internal_Sym *sym;
995 {
996 if (h != NULL)
997 {
998 switch (ELF32_R_TYPE (rel->r_info))
999 {
1000 case R_68K_GNU_VTINHERIT:
1001 case R_68K_GNU_VTENTRY:
1002 break;
1003
1004 default:
1005 switch (h->root.type)
1006 {
1007 default:
1008 break;
1009
1010 case bfd_link_hash_defined:
1011 case bfd_link_hash_defweak:
1012 return h->root.u.def.section;
1013
1014 case bfd_link_hash_common:
1015 return h->root.u.c.p->section;
1016 }
1017 }
1018 }
1019 else
1020 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1021
1022 return NULL;
1023 }
1024
1025 /* Update the got entry reference counts for the section being removed. */
1026
1027 static bfd_boolean
1028 elf_m68k_gc_sweep_hook (abfd, info, sec, relocs)
1029 bfd *abfd;
1030 struct bfd_link_info *info;
1031 asection *sec;
1032 const Elf_Internal_Rela *relocs;
1033 {
1034 Elf_Internal_Shdr *symtab_hdr;
1035 struct elf_link_hash_entry **sym_hashes;
1036 bfd_signed_vma *local_got_refcounts;
1037 const Elf_Internal_Rela *rel, *relend;
1038 bfd *dynobj;
1039 asection *sgot;
1040 asection *srelgot;
1041
1042 dynobj = elf_hash_table (info)->dynobj;
1043 if (dynobj == NULL)
1044 return TRUE;
1045
1046 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1047 sym_hashes = elf_sym_hashes (abfd);
1048 local_got_refcounts = elf_local_got_refcounts (abfd);
1049
1050 sgot = bfd_get_section_by_name (dynobj, ".got");
1051 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1052
1053 relend = relocs + sec->reloc_count;
1054 for (rel = relocs; rel < relend; rel++)
1055 {
1056 unsigned long r_symndx;
1057 struct elf_link_hash_entry *h = NULL;
1058
1059 r_symndx = ELF32_R_SYM (rel->r_info);
1060 if (r_symndx >= symtab_hdr->sh_info)
1061 {
1062 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1063 while (h->root.type == bfd_link_hash_indirect
1064 || h->root.type == bfd_link_hash_warning)
1065 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1066 }
1067
1068 switch (ELF32_R_TYPE (rel->r_info))
1069 {
1070 case R_68K_GOT8:
1071 case R_68K_GOT16:
1072 case R_68K_GOT32:
1073 case R_68K_GOT8O:
1074 case R_68K_GOT16O:
1075 case R_68K_GOT32O:
1076 if (h != NULL)
1077 {
1078 if (h->got.refcount > 0)
1079 {
1080 --h->got.refcount;
1081 if (h->got.refcount == 0)
1082 {
1083 /* We don't need the .got entry any more. */
1084 sgot->size -= 4;
1085 srelgot->size -= sizeof (Elf32_External_Rela);
1086 }
1087 }
1088 }
1089 else if (local_got_refcounts != NULL)
1090 {
1091 if (local_got_refcounts[r_symndx] > 0)
1092 {
1093 --local_got_refcounts[r_symndx];
1094 if (local_got_refcounts[r_symndx] == 0)
1095 {
1096 /* We don't need the .got entry any more. */
1097 sgot->size -= 4;
1098 if (info->shared)
1099 srelgot->size -= sizeof (Elf32_External_Rela);
1100 }
1101 }
1102 }
1103 break;
1104
1105 case R_68K_PLT8:
1106 case R_68K_PLT16:
1107 case R_68K_PLT32:
1108 case R_68K_PLT8O:
1109 case R_68K_PLT16O:
1110 case R_68K_PLT32O:
1111 case R_68K_PC8:
1112 case R_68K_PC16:
1113 case R_68K_PC32:
1114 case R_68K_8:
1115 case R_68K_16:
1116 case R_68K_32:
1117 if (h != NULL)
1118 {
1119 if (h->plt.refcount > 0)
1120 --h->plt.refcount;
1121 }
1122 break;
1123
1124 default:
1125 break;
1126 }
1127 }
1128
1129 return TRUE;
1130 }
1131
1132 /* Adjust a symbol defined by a dynamic object and referenced by a
1133 regular object. The current definition is in some section of the
1134 dynamic object, but we're not including those sections. We have to
1135 change the definition to something the rest of the link can
1136 understand. */
1137
1138 static bfd_boolean
1139 elf_m68k_adjust_dynamic_symbol (info, h)
1140 struct bfd_link_info *info;
1141 struct elf_link_hash_entry *h;
1142 {
1143 bfd *dynobj;
1144 asection *s;
1145 unsigned int power_of_two;
1146
1147 dynobj = elf_hash_table (info)->dynobj;
1148
1149 /* Make sure we know what is going on here. */
1150 BFD_ASSERT (dynobj != NULL
1151 && (h->needs_plt
1152 || h->u.weakdef != NULL
1153 || (h->def_dynamic
1154 && h->ref_regular
1155 && !h->def_regular)));
1156
1157 /* If this is a function, put it in the procedure linkage table. We
1158 will fill in the contents of the procedure linkage table later,
1159 when we know the address of the .got section. */
1160 if (h->type == STT_FUNC
1161 || h->needs_plt)
1162 {
1163 if ((h->plt.refcount <= 0
1164 || SYMBOL_CALLS_LOCAL (info, h)
1165 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1166 && h->root.type == bfd_link_hash_undefweak))
1167 /* We must always create the plt entry if it was referenced
1168 by a PLTxxO relocation. In this case we already recorded
1169 it as a dynamic symbol. */
1170 && h->dynindx == -1)
1171 {
1172 /* This case can occur if we saw a PLTxx reloc in an input
1173 file, but the symbol was never referred to by a dynamic
1174 object, or if all references were garbage collected. In
1175 such a case, we don't actually need to build a procedure
1176 linkage table, and we can just do a PCxx reloc instead. */
1177 h->plt.offset = (bfd_vma) -1;
1178 h->needs_plt = 0;
1179 return TRUE;
1180 }
1181
1182 /* Make sure this symbol is output as a dynamic symbol. */
1183 if (h->dynindx == -1
1184 && !h->forced_local)
1185 {
1186 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1187 return FALSE;
1188 }
1189
1190 s = bfd_get_section_by_name (dynobj, ".plt");
1191 BFD_ASSERT (s != NULL);
1192
1193 /* If this is the first .plt entry, make room for the special
1194 first entry. */
1195 if (s->size == 0)
1196 {
1197 if (CPU32_FLAG (dynobj))
1198 s->size += PLT_CPU32_ENTRY_SIZE;
1199 else if (CFV4E_FLAG (dynobj))
1200 s->size += CFV4E_PLT_ENTRY_SIZE;
1201 else
1202 s->size += PLT_ENTRY_SIZE;
1203 }
1204
1205 /* If this symbol is not defined in a regular file, and we are
1206 not generating a shared library, then set the symbol to this
1207 location in the .plt. This is required to make function
1208 pointers compare as equal between the normal executable and
1209 the shared library. */
1210 if (!info->shared
1211 && !h->def_regular)
1212 {
1213 h->root.u.def.section = s;
1214 h->root.u.def.value = s->size;
1215 }
1216
1217 h->plt.offset = s->size;
1218
1219 /* Make room for this entry. */
1220 if (CPU32_FLAG (dynobj))
1221 s->size += PLT_CPU32_ENTRY_SIZE;
1222 else if (CFV4E_FLAG (dynobj))
1223 s->size += CFV4E_PLT_ENTRY_SIZE;
1224 else
1225 s->size += PLT_ENTRY_SIZE;
1226
1227 /* We also need to make an entry in the .got.plt section, which
1228 will be placed in the .got section by the linker script. */
1229 s = bfd_get_section_by_name (dynobj, ".got.plt");
1230 BFD_ASSERT (s != NULL);
1231 s->size += 4;
1232
1233 /* We also need to make an entry in the .rela.plt section. */
1234 s = bfd_get_section_by_name (dynobj, ".rela.plt");
1235 BFD_ASSERT (s != NULL);
1236 s->size += sizeof (Elf32_External_Rela);
1237
1238 return TRUE;
1239 }
1240
1241 /* Reinitialize the plt offset now that it is not used as a reference
1242 count any more. */
1243 h->plt.offset = (bfd_vma) -1;
1244
1245 /* If this is a weak symbol, and there is a real definition, the
1246 processor independent code will have arranged for us to see the
1247 real definition first, and we can just use the same value. */
1248 if (h->u.weakdef != NULL)
1249 {
1250 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1251 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1252 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1253 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1254 return TRUE;
1255 }
1256
1257 /* This is a reference to a symbol defined by a dynamic object which
1258 is not a function. */
1259
1260 /* If we are creating a shared library, we must presume that the
1261 only references to the symbol are via the global offset table.
1262 For such cases we need not do anything here; the relocations will
1263 be handled correctly by relocate_section. */
1264 if (info->shared)
1265 return TRUE;
1266
1267 if (h->size == 0)
1268 {
1269 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1270 h->root.root.string);
1271 return TRUE;
1272 }
1273
1274 /* We must allocate the symbol in our .dynbss section, which will
1275 become part of the .bss section of the executable. There will be
1276 an entry for this symbol in the .dynsym section. The dynamic
1277 object will contain position independent code, so all references
1278 from the dynamic object to this symbol will go through the global
1279 offset table. The dynamic linker will use the .dynsym entry to
1280 determine the address it must put in the global offset table, so
1281 both the dynamic object and the regular object will refer to the
1282 same memory location for the variable. */
1283
1284 s = bfd_get_section_by_name (dynobj, ".dynbss");
1285 BFD_ASSERT (s != NULL);
1286
1287 /* We must generate a R_68K_COPY reloc to tell the dynamic linker to
1288 copy the initial value out of the dynamic object and into the
1289 runtime process image. We need to remember the offset into the
1290 .rela.bss section we are going to use. */
1291 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1292 {
1293 asection *srel;
1294
1295 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
1296 BFD_ASSERT (srel != NULL);
1297 srel->size += sizeof (Elf32_External_Rela);
1298 h->needs_copy = 1;
1299 }
1300
1301 /* We need to figure out the alignment required for this symbol. I
1302 have no idea how ELF linkers handle this. */
1303 power_of_two = bfd_log2 (h->size);
1304 if (power_of_two > 3)
1305 power_of_two = 3;
1306
1307 /* Apply the required alignment. */
1308 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1309 if (power_of_two > bfd_get_section_alignment (dynobj, s))
1310 {
1311 if (!bfd_set_section_alignment (dynobj, s, power_of_two))
1312 return FALSE;
1313 }
1314
1315 /* Define the symbol as being at this point in the section. */
1316 h->root.u.def.section = s;
1317 h->root.u.def.value = s->size;
1318
1319 /* Increment the section size to make room for the symbol. */
1320 s->size += h->size;
1321
1322 return TRUE;
1323 }
1324
1325 /* Set the sizes of the dynamic sections. */
1326
1327 static bfd_boolean
1328 elf_m68k_size_dynamic_sections (output_bfd, info)
1329 bfd *output_bfd ATTRIBUTE_UNUSED;
1330 struct bfd_link_info *info;
1331 {
1332 bfd *dynobj;
1333 asection *s;
1334 bfd_boolean plt;
1335 bfd_boolean relocs;
1336
1337 dynobj = elf_hash_table (info)->dynobj;
1338 BFD_ASSERT (dynobj != NULL);
1339
1340 if (elf_hash_table (info)->dynamic_sections_created)
1341 {
1342 /* Set the contents of the .interp section to the interpreter. */
1343 if (info->executable)
1344 {
1345 s = bfd_get_section_by_name (dynobj, ".interp");
1346 BFD_ASSERT (s != NULL);
1347 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1348 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1349 }
1350 }
1351 else
1352 {
1353 /* We may have created entries in the .rela.got section.
1354 However, if we are not creating the dynamic sections, we will
1355 not actually use these entries. Reset the size of .rela.got,
1356 which will cause it to get stripped from the output file
1357 below. */
1358 s = bfd_get_section_by_name (dynobj, ".rela.got");
1359 if (s != NULL)
1360 s->size = 0;
1361 }
1362
1363 /* If this is a -Bsymbolic shared link, then we need to discard all
1364 PC relative relocs against symbols defined in a regular object.
1365 For the normal shared case we discard the PC relative relocs
1366 against symbols that have become local due to visibility changes.
1367 We allocated space for them in the check_relocs routine, but we
1368 will not fill them in in the relocate_section routine. */
1369 if (info->shared)
1370 elf_link_hash_traverse (elf_hash_table (info),
1371 elf_m68k_discard_copies,
1372 (PTR) info);
1373
1374 /* The check_relocs and adjust_dynamic_symbol entry points have
1375 determined the sizes of the various dynamic sections. Allocate
1376 memory for them. */
1377 plt = FALSE;
1378 relocs = FALSE;
1379 for (s = dynobj->sections; s != NULL; s = s->next)
1380 {
1381 const char *name;
1382
1383 if ((s->flags & SEC_LINKER_CREATED) == 0)
1384 continue;
1385
1386 /* It's OK to base decisions on the section name, because none
1387 of the dynobj section names depend upon the input files. */
1388 name = bfd_get_section_name (dynobj, s);
1389
1390 if (strcmp (name, ".plt") == 0)
1391 {
1392 /* Remember whether there is a PLT. */
1393 plt = s->size != 0;
1394 }
1395 else if (strncmp (name, ".rela", 5) == 0)
1396 {
1397 if (s->size != 0)
1398 {
1399 relocs = TRUE;
1400
1401 /* We use the reloc_count field as a counter if we need
1402 to copy relocs into the output file. */
1403 s->reloc_count = 0;
1404 }
1405 }
1406 else if (strncmp (name, ".got", 4) != 0
1407 && strcmp (name, ".dynbss") != 0)
1408 {
1409 /* It's not one of our sections, so don't allocate space. */
1410 continue;
1411 }
1412
1413 if (s->size == 0)
1414 {
1415 /* If we don't need this section, strip it from the
1416 output file. This is mostly to handle .rela.bss and
1417 .rela.plt. We must create both sections in
1418 create_dynamic_sections, because they must be created
1419 before the linker maps input sections to output
1420 sections. The linker does that before
1421 adjust_dynamic_symbol is called, and it is that
1422 function which decides whether anything needs to go
1423 into these sections. */
1424 s->flags |= SEC_EXCLUDE;
1425 continue;
1426 }
1427
1428 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1429 continue;
1430
1431 /* Allocate memory for the section contents. */
1432 /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
1433 Unused entries should be reclaimed before the section's contents
1434 are written out, but at the moment this does not happen. Thus in
1435 order to prevent writing out garbage, we initialise the section's
1436 contents to zero. */
1437 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1438 if (s->contents == NULL)
1439 return FALSE;
1440 }
1441
1442 if (elf_hash_table (info)->dynamic_sections_created)
1443 {
1444 /* Add some entries to the .dynamic section. We fill in the
1445 values later, in elf_m68k_finish_dynamic_sections, but we
1446 must add the entries now so that we get the correct size for
1447 the .dynamic section. The DT_DEBUG entry is filled in by the
1448 dynamic linker and used by the debugger. */
1449 #define add_dynamic_entry(TAG, VAL) \
1450 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1451
1452 if (!info->shared)
1453 {
1454 if (!add_dynamic_entry (DT_DEBUG, 0))
1455 return FALSE;
1456 }
1457
1458 if (plt)
1459 {
1460 if (!add_dynamic_entry (DT_PLTGOT, 0)
1461 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1462 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1463 || !add_dynamic_entry (DT_JMPREL, 0))
1464 return FALSE;
1465 }
1466
1467 if (relocs)
1468 {
1469 if (!add_dynamic_entry (DT_RELA, 0)
1470 || !add_dynamic_entry (DT_RELASZ, 0)
1471 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela)))
1472 return FALSE;
1473 }
1474
1475 if ((info->flags & DF_TEXTREL) != 0)
1476 {
1477 if (!add_dynamic_entry (DT_TEXTREL, 0))
1478 return FALSE;
1479 }
1480 }
1481 #undef add_dynamic_entry
1482
1483 return TRUE;
1484 }
1485
1486 /* This function is called via elf_link_hash_traverse if we are
1487 creating a shared object. In the -Bsymbolic case it discards the
1488 space allocated to copy PC relative relocs against symbols which
1489 are defined in regular objects. For the normal shared case, it
1490 discards space for pc-relative relocs that have become local due to
1491 symbol visibility changes. We allocated space for them in the
1492 check_relocs routine, but we won't fill them in in the
1493 relocate_section routine.
1494
1495 We also check whether any of the remaining relocations apply
1496 against a readonly section, and set the DF_TEXTREL flag in this
1497 case. */
1498
1499 static bfd_boolean
1500 elf_m68k_discard_copies (h, inf)
1501 struct elf_link_hash_entry *h;
1502 PTR inf;
1503 {
1504 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1505 struct elf_m68k_pcrel_relocs_copied *s;
1506
1507 if (h->root.type == bfd_link_hash_warning)
1508 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1509
1510 if (!h->def_regular
1511 || (!info->symbolic
1512 && !h->forced_local))
1513 {
1514 if ((info->flags & DF_TEXTREL) == 0)
1515 {
1516 /* Look for relocations against read-only sections. */
1517 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1518 s != NULL;
1519 s = s->next)
1520 if ((s->section->flags & SEC_READONLY) != 0)
1521 {
1522 info->flags |= DF_TEXTREL;
1523 break;
1524 }
1525 }
1526
1527 return TRUE;
1528 }
1529
1530 for (s = elf_m68k_hash_entry (h)->pcrel_relocs_copied;
1531 s != NULL;
1532 s = s->next)
1533 s->section->size -= s->count * sizeof (Elf32_External_Rela);
1534
1535 return TRUE;
1536 }
1537
1538 /* Relocate an M68K ELF section. */
1539
1540 static bfd_boolean
1541 elf_m68k_relocate_section (output_bfd, info, input_bfd, input_section,
1542 contents, relocs, local_syms, local_sections)
1543 bfd *output_bfd;
1544 struct bfd_link_info *info;
1545 bfd *input_bfd;
1546 asection *input_section;
1547 bfd_byte *contents;
1548 Elf_Internal_Rela *relocs;
1549 Elf_Internal_Sym *local_syms;
1550 asection **local_sections;
1551 {
1552 bfd *dynobj;
1553 Elf_Internal_Shdr *symtab_hdr;
1554 struct elf_link_hash_entry **sym_hashes;
1555 bfd_vma *local_got_offsets;
1556 asection *sgot;
1557 asection *splt;
1558 asection *sreloc;
1559 Elf_Internal_Rela *rel;
1560 Elf_Internal_Rela *relend;
1561
1562 if (info->relocatable)
1563 return TRUE;
1564
1565 dynobj = elf_hash_table (info)->dynobj;
1566 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1567 sym_hashes = elf_sym_hashes (input_bfd);
1568 local_got_offsets = elf_local_got_offsets (input_bfd);
1569
1570 sgot = NULL;
1571 splt = NULL;
1572 sreloc = NULL;
1573
1574 rel = relocs;
1575 relend = relocs + input_section->reloc_count;
1576 for (; rel < relend; rel++)
1577 {
1578 int r_type;
1579 reloc_howto_type *howto;
1580 unsigned long r_symndx;
1581 struct elf_link_hash_entry *h;
1582 Elf_Internal_Sym *sym;
1583 asection *sec;
1584 bfd_vma relocation;
1585 bfd_boolean unresolved_reloc;
1586 bfd_reloc_status_type r;
1587
1588 r_type = ELF32_R_TYPE (rel->r_info);
1589 if (r_type < 0 || r_type >= (int) R_68K_max)
1590 {
1591 bfd_set_error (bfd_error_bad_value);
1592 return FALSE;
1593 }
1594 howto = howto_table + r_type;
1595
1596 r_symndx = ELF32_R_SYM (rel->r_info);
1597
1598 h = NULL;
1599 sym = NULL;
1600 sec = NULL;
1601 unresolved_reloc = FALSE;
1602
1603 if (r_symndx < symtab_hdr->sh_info)
1604 {
1605 sym = local_syms + r_symndx;
1606 sec = local_sections[r_symndx];
1607 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1608 }
1609 else
1610 {
1611 bfd_boolean warned;
1612
1613 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1614 r_symndx, symtab_hdr, sym_hashes,
1615 h, sec, relocation,
1616 unresolved_reloc, warned);
1617 }
1618
1619 switch (r_type)
1620 {
1621 case R_68K_GOT8:
1622 case R_68K_GOT16:
1623 case R_68K_GOT32:
1624 /* Relocation is to the address of the entry for this symbol
1625 in the global offset table. */
1626 if (h != NULL
1627 && strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1628 break;
1629 /* Fall through. */
1630 case R_68K_GOT8O:
1631 case R_68K_GOT16O:
1632 case R_68K_GOT32O:
1633 /* Relocation is the offset of the entry for this symbol in
1634 the global offset table. */
1635
1636 {
1637 bfd_vma off;
1638
1639 if (sgot == NULL)
1640 {
1641 sgot = bfd_get_section_by_name (dynobj, ".got");
1642 BFD_ASSERT (sgot != NULL);
1643 }
1644
1645 if (h != NULL)
1646 {
1647 bfd_boolean dyn;
1648
1649 off = h->got.offset;
1650 BFD_ASSERT (off != (bfd_vma) -1);
1651
1652 dyn = elf_hash_table (info)->dynamic_sections_created;
1653 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1654 || (info->shared
1655 && (info->symbolic
1656 || h->dynindx == -1
1657 || h->forced_local)
1658 && h->def_regular))
1659 {
1660 /* This is actually a static link, or it is a
1661 -Bsymbolic link and the symbol is defined
1662 locally, or the symbol was forced to be local
1663 because of a version file.. We must initialize
1664 this entry in the global offset table. Since
1665 the offset must always be a multiple of 4, we
1666 use the least significant bit to record whether
1667 we have initialized it already.
1668
1669 When doing a dynamic link, we create a .rela.got
1670 relocation entry to initialize the value. This
1671 is done in the finish_dynamic_symbol routine. */
1672 if ((off & 1) != 0)
1673 off &= ~1;
1674 else
1675 {
1676 bfd_put_32 (output_bfd, relocation,
1677 sgot->contents + off);
1678 h->got.offset |= 1;
1679 }
1680 }
1681 else
1682 unresolved_reloc = FALSE;
1683 }
1684 else
1685 {
1686 BFD_ASSERT (local_got_offsets != NULL
1687 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1688
1689 off = local_got_offsets[r_symndx];
1690
1691 /* The offset must always be a multiple of 4. We use
1692 the least significant bit to record whether we have
1693 already generated the necessary reloc. */
1694 if ((off & 1) != 0)
1695 off &= ~1;
1696 else
1697 {
1698 bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1699
1700 if (info->shared)
1701 {
1702 asection *s;
1703 Elf_Internal_Rela outrel;
1704 bfd_byte *loc;
1705
1706 s = bfd_get_section_by_name (dynobj, ".rela.got");
1707 BFD_ASSERT (s != NULL);
1708
1709 outrel.r_offset = (sgot->output_section->vma
1710 + sgot->output_offset
1711 + off);
1712 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1713 outrel.r_addend = relocation;
1714 loc = s->contents;
1715 loc += s->reloc_count++ * sizeof (Elf32_External_Rela);
1716 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1717 }
1718
1719 local_got_offsets[r_symndx] |= 1;
1720 }
1721 }
1722
1723 relocation = sgot->output_offset + off;
1724 if (r_type == R_68K_GOT8O
1725 || r_type == R_68K_GOT16O
1726 || r_type == R_68K_GOT32O)
1727 {
1728 /* This relocation does not use the addend. */
1729 rel->r_addend = 0;
1730 }
1731 else
1732 relocation += sgot->output_section->vma;
1733 }
1734 break;
1735
1736 case R_68K_PLT8:
1737 case R_68K_PLT16:
1738 case R_68K_PLT32:
1739 /* Relocation is to the entry for this symbol in the
1740 procedure linkage table. */
1741
1742 /* Resolve a PLTxx reloc against a local symbol directly,
1743 without using the procedure linkage table. */
1744 if (h == NULL)
1745 break;
1746
1747 if (h->plt.offset == (bfd_vma) -1
1748 || !elf_hash_table (info)->dynamic_sections_created)
1749 {
1750 /* We didn't make a PLT entry for this symbol. This
1751 happens when statically linking PIC code, or when
1752 using -Bsymbolic. */
1753 break;
1754 }
1755
1756 if (splt == NULL)
1757 {
1758 splt = bfd_get_section_by_name (dynobj, ".plt");
1759 BFD_ASSERT (splt != NULL);
1760 }
1761
1762 relocation = (splt->output_section->vma
1763 + splt->output_offset
1764 + h->plt.offset);
1765 unresolved_reloc = FALSE;
1766 break;
1767
1768 case R_68K_PLT8O:
1769 case R_68K_PLT16O:
1770 case R_68K_PLT32O:
1771 /* Relocation is the offset of the entry for this symbol in
1772 the procedure linkage table. */
1773 BFD_ASSERT (h != NULL && h->plt.offset != (bfd_vma) -1);
1774
1775 if (splt == NULL)
1776 {
1777 splt = bfd_get_section_by_name (dynobj, ".plt");
1778 BFD_ASSERT (splt != NULL);
1779 }
1780
1781 relocation = h->plt.offset;
1782 unresolved_reloc = FALSE;
1783
1784 /* This relocation does not use the addend. */
1785 rel->r_addend = 0;
1786
1787 break;
1788
1789 case R_68K_PC8:
1790 case R_68K_PC16:
1791 case R_68K_PC32:
1792 if (h == NULL
1793 || (info->shared
1794 && h->forced_local))
1795 break;
1796 /* Fall through. */
1797 case R_68K_8:
1798 case R_68K_16:
1799 case R_68K_32:
1800 if (info->shared
1801 && r_symndx != 0
1802 && (input_section->flags & SEC_ALLOC) != 0
1803 && (h == NULL
1804 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1805 || h->root.type != bfd_link_hash_undefweak)
1806 && ((r_type != R_68K_PC8
1807 && r_type != R_68K_PC16
1808 && r_type != R_68K_PC32)
1809 || (h != NULL
1810 && h->dynindx != -1
1811 && (!info->symbolic
1812 || !h->def_regular))))
1813 {
1814 Elf_Internal_Rela outrel;
1815 bfd_byte *loc;
1816 bfd_boolean skip, relocate;
1817
1818 /* When generating a shared object, these relocations
1819 are copied into the output file to be resolved at run
1820 time. */
1821
1822 skip = FALSE;
1823 relocate = FALSE;
1824
1825 outrel.r_offset =
1826 _bfd_elf_section_offset (output_bfd, info, input_section,
1827 rel->r_offset);
1828 if (outrel.r_offset == (bfd_vma) -1)
1829 skip = TRUE;
1830 else if (outrel.r_offset == (bfd_vma) -2)
1831 skip = TRUE, relocate = TRUE;
1832 outrel.r_offset += (input_section->output_section->vma
1833 + input_section->output_offset);
1834
1835 if (skip)
1836 memset (&outrel, 0, sizeof outrel);
1837 else if (h != NULL
1838 && h->dynindx != -1
1839 && (r_type == R_68K_PC8
1840 || r_type == R_68K_PC16
1841 || r_type == R_68K_PC32
1842 || !info->shared
1843 || !info->symbolic
1844 || !h->def_regular))
1845 {
1846 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1847 outrel.r_addend = rel->r_addend;
1848 }
1849 else
1850 {
1851 /* This symbol is local, or marked to become local. */
1852 if (r_type == R_68K_32)
1853 {
1854 relocate = TRUE;
1855 outrel.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
1856 outrel.r_addend = relocation + rel->r_addend;
1857 }
1858 else
1859 {
1860 long indx;
1861
1862 if (bfd_is_abs_section (sec))
1863 indx = 0;
1864 else if (sec == NULL || sec->owner == NULL)
1865 {
1866 bfd_set_error (bfd_error_bad_value);
1867 return FALSE;
1868 }
1869 else
1870 {
1871 asection *osec;
1872
1873 osec = sec->output_section;
1874 indx = elf_section_data (osec)->dynindx;
1875 BFD_ASSERT (indx > 0);
1876 }
1877
1878 outrel.r_info = ELF32_R_INFO (indx, r_type);
1879 outrel.r_addend = relocation + rel->r_addend;
1880 }
1881 }
1882
1883 sreloc = elf_section_data (input_section)->sreloc;
1884 if (sreloc == NULL)
1885 abort ();
1886
1887 loc = sreloc->contents;
1888 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1889 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1890
1891 /* This reloc will be computed at runtime, so there's no
1892 need to do anything now, except for R_68K_32
1893 relocations that have been turned into
1894 R_68K_RELATIVE. */
1895 if (!relocate)
1896 continue;
1897 }
1898
1899 break;
1900
1901 case R_68K_GNU_VTINHERIT:
1902 case R_68K_GNU_VTENTRY:
1903 /* These are no-ops in the end. */
1904 continue;
1905
1906 default:
1907 break;
1908 }
1909
1910 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1911 because such sections are not SEC_ALLOC and thus ld.so will
1912 not process them. */
1913 if (unresolved_reloc
1914 && !((input_section->flags & SEC_DEBUGGING) != 0
1915 && h->def_dynamic))
1916 {
1917 (*_bfd_error_handler)
1918 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1919 input_bfd,
1920 input_section,
1921 (long) rel->r_offset,
1922 howto->name,
1923 h->root.root.string);
1924 return FALSE;
1925 }
1926
1927 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1928 contents, rel->r_offset,
1929 relocation, rel->r_addend);
1930
1931 if (r != bfd_reloc_ok)
1932 {
1933 const char *name;
1934
1935 if (h != NULL)
1936 name = h->root.root.string;
1937 else
1938 {
1939 name = bfd_elf_string_from_elf_section (input_bfd,
1940 symtab_hdr->sh_link,
1941 sym->st_name);
1942 if (name == NULL)
1943 return FALSE;
1944 if (*name == '\0')
1945 name = bfd_section_name (input_bfd, sec);
1946 }
1947
1948 if (r == bfd_reloc_overflow)
1949 {
1950 if (!(info->callbacks->reloc_overflow
1951 (info, (h ? &h->root : NULL), name, howto->name,
1952 (bfd_vma) 0, input_bfd, input_section,
1953 rel->r_offset)))
1954 return FALSE;
1955 }
1956 else
1957 {
1958 (*_bfd_error_handler)
1959 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
1960 input_bfd, input_section,
1961 (long) rel->r_offset, name, (int) r);
1962 return FALSE;
1963 }
1964 }
1965 }
1966
1967 return TRUE;
1968 }
1969
1970 /* Finish up dynamic symbol handling. We set the contents of various
1971 dynamic sections here. */
1972
1973 static bfd_boolean
1974 elf_m68k_finish_dynamic_symbol (output_bfd, info, h, sym)
1975 bfd *output_bfd;
1976 struct bfd_link_info *info;
1977 struct elf_link_hash_entry *h;
1978 Elf_Internal_Sym *sym;
1979 {
1980 bfd *dynobj;
1981 int plt_off1, plt_off2, plt_off3;
1982
1983 dynobj = elf_hash_table (info)->dynobj;
1984
1985 if (h->plt.offset != (bfd_vma) -1)
1986 {
1987 asection *splt;
1988 asection *sgot;
1989 asection *srela;
1990 bfd_vma plt_index;
1991 bfd_vma got_offset;
1992 Elf_Internal_Rela rela;
1993 bfd_byte *loc;
1994
1995 /* This symbol has an entry in the procedure linkage table. Set
1996 it up. */
1997
1998 BFD_ASSERT (h->dynindx != -1);
1999
2000 splt = bfd_get_section_by_name (dynobj, ".plt");
2001 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2002 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
2003 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
2004
2005 /* Get the index in the procedure linkage table which
2006 corresponds to this symbol. This is the index of this symbol
2007 in all the symbols for which we are making plt entries. The
2008 first entry in the procedure linkage table is reserved. */
2009 if (CPU32_FLAG (output_bfd))
2010 plt_index = (h->plt.offset / PLT_CPU32_ENTRY_SIZE) - 1;
2011 else if (CFV4E_FLAG (output_bfd))
2012 plt_index = (h->plt.offset / CFV4E_PLT_ENTRY_SIZE) - 1;
2013 else
2014 plt_index = (h->plt.offset / PLT_ENTRY_SIZE) - 1;
2015
2016 /* Get the offset into the .got table of the entry that
2017 corresponds to this function. Each .got entry is 4 bytes.
2018 The first three are reserved. */
2019 got_offset = (plt_index + 3) * 4;
2020
2021 if (CPU32_FLAG (output_bfd))
2022 {
2023 /* Fill in the entry in the procedure linkage table. */
2024 memcpy (splt->contents + h->plt.offset, elf_cpu32_plt_entry,
2025 PLT_CPU32_ENTRY_SIZE);
2026 plt_off1 = 4;
2027 plt_off2 = 12;
2028 plt_off3 = 18;
2029 }
2030 else if (CFV4E_FLAG (output_bfd))
2031 {
2032 memcpy (splt->contents + h->plt.offset, elf_cfv4e_plt_entry,
2033 CFV4E_PLT_ENTRY_SIZE);
2034 plt_off1 = 2;
2035 plt_off2 = 14;
2036 plt_off3 = 20;
2037 }
2038 else
2039 {
2040 /* Fill in the entry in the procedure linkage table. */
2041 memcpy (splt->contents + h->plt.offset, elf_m68k_plt_entry,
2042 PLT_ENTRY_SIZE);
2043 plt_off1 = 4;
2044 plt_off2 = 10;
2045 plt_off3 = 16;
2046 }
2047
2048 /* The offset is relative to the first extension word. */
2049 bfd_put_32 (output_bfd,
2050 sgot->output_section->vma
2051 + sgot->output_offset
2052 + got_offset
2053 - (splt->output_section->vma
2054 + h->plt.offset
2055 + (CFV4E_FLAG (output_bfd) ? 8 : 2)),
2056 splt->contents + h->plt.offset + plt_off1);
2057
2058 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
2059 splt->contents + h->plt.offset + plt_off2);
2060 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3),
2061 splt->contents + h->plt.offset + plt_off3);
2062
2063 /* Fill in the entry in the global offset table. */
2064 bfd_put_32 (output_bfd,
2065 (splt->output_section->vma
2066 + splt->output_offset
2067 + h->plt.offset
2068 + (CFV4E_FLAG (output_bfd) ? 12 : 8)),
2069 sgot->contents + got_offset);
2070
2071 /* Fill in the entry in the .rela.plt section. */
2072 rela.r_offset = (sgot->output_section->vma
2073 + sgot->output_offset
2074 + got_offset);
2075 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_JMP_SLOT);
2076 rela.r_addend = 0;
2077 loc = srela->contents + plt_index * sizeof (Elf32_External_Rela);
2078 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2079
2080 if (!h->def_regular)
2081 {
2082 /* Mark the symbol as undefined, rather than as defined in
2083 the .plt section. Leave the value alone. */
2084 sym->st_shndx = SHN_UNDEF;
2085 }
2086 }
2087
2088 if (h->got.offset != (bfd_vma) -1)
2089 {
2090 asection *sgot;
2091 asection *srela;
2092 Elf_Internal_Rela rela;
2093 bfd_byte *loc;
2094
2095 /* This symbol has an entry in the global offset table. Set it
2096 up. */
2097
2098 sgot = bfd_get_section_by_name (dynobj, ".got");
2099 srela = bfd_get_section_by_name (dynobj, ".rela.got");
2100 BFD_ASSERT (sgot != NULL && srela != NULL);
2101
2102 rela.r_offset = (sgot->output_section->vma
2103 + sgot->output_offset
2104 + (h->got.offset &~ (bfd_vma) 1));
2105
2106 /* If this is a -Bsymbolic link, and the symbol is defined
2107 locally, we just want to emit a RELATIVE reloc. Likewise if
2108 the symbol was forced to be local because of a version file.
2109 The entry in the global offset table will already have been
2110 initialized in the relocate_section function. */
2111 if (info->shared
2112 && (info->symbolic
2113 || h->dynindx == -1
2114 || h->forced_local)
2115 && h->def_regular)
2116 {
2117 rela.r_info = ELF32_R_INFO (0, R_68K_RELATIVE);
2118 rela.r_addend = bfd_get_signed_32 (output_bfd,
2119 (sgot->contents
2120 + (h->got.offset &~ (bfd_vma) 1)));
2121 }
2122 else
2123 {
2124 bfd_put_32 (output_bfd, (bfd_vma) 0,
2125 sgot->contents + (h->got.offset &~ (bfd_vma) 1));
2126 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_GLOB_DAT);
2127 rela.r_addend = 0;
2128 }
2129
2130 loc = srela->contents;
2131 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2132 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2133 }
2134
2135 if (h->needs_copy)
2136 {
2137 asection *s;
2138 Elf_Internal_Rela rela;
2139 bfd_byte *loc;
2140
2141 /* This symbol needs a copy reloc. Set it up. */
2142
2143 BFD_ASSERT (h->dynindx != -1
2144 && (h->root.type == bfd_link_hash_defined
2145 || h->root.type == bfd_link_hash_defweak));
2146
2147 s = bfd_get_section_by_name (h->root.u.def.section->owner,
2148 ".rela.bss");
2149 BFD_ASSERT (s != NULL);
2150
2151 rela.r_offset = (h->root.u.def.value
2152 + h->root.u.def.section->output_section->vma
2153 + h->root.u.def.section->output_offset);
2154 rela.r_info = ELF32_R_INFO (h->dynindx, R_68K_COPY);
2155 rela.r_addend = 0;
2156 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2157 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2158 }
2159
2160 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2161 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2162 || h == elf_hash_table (info)->hgot)
2163 sym->st_shndx = SHN_ABS;
2164
2165 return TRUE;
2166 }
2167
2168 /* Finish up the dynamic sections. */
2169
2170 static bfd_boolean
2171 elf_m68k_finish_dynamic_sections (output_bfd, info)
2172 bfd *output_bfd;
2173 struct bfd_link_info *info;
2174 {
2175 bfd *dynobj;
2176 asection *sgot;
2177 asection *sdyn;
2178
2179 dynobj = elf_hash_table (info)->dynobj;
2180
2181 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
2182 BFD_ASSERT (sgot != NULL);
2183 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2184
2185 if (elf_hash_table (info)->dynamic_sections_created)
2186 {
2187 asection *splt;
2188 Elf32_External_Dyn *dyncon, *dynconend;
2189
2190 splt = bfd_get_section_by_name (dynobj, ".plt");
2191 BFD_ASSERT (splt != NULL && sdyn != NULL);
2192
2193 dyncon = (Elf32_External_Dyn *) sdyn->contents;
2194 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2195 for (; dyncon < dynconend; dyncon++)
2196 {
2197 Elf_Internal_Dyn dyn;
2198 const char *name;
2199 asection *s;
2200
2201 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2202
2203 switch (dyn.d_tag)
2204 {
2205 default:
2206 break;
2207
2208 case DT_PLTGOT:
2209 name = ".got";
2210 goto get_vma;
2211 case DT_JMPREL:
2212 name = ".rela.plt";
2213 get_vma:
2214 s = bfd_get_section_by_name (output_bfd, name);
2215 BFD_ASSERT (s != NULL);
2216 dyn.d_un.d_ptr = s->vma;
2217 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2218 break;
2219
2220 case DT_PLTRELSZ:
2221 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2222 BFD_ASSERT (s != NULL);
2223 dyn.d_un.d_val = s->size;
2224 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2225 break;
2226
2227 case DT_RELASZ:
2228 /* The procedure linkage table relocs (DT_JMPREL) should
2229 not be included in the overall relocs (DT_RELA).
2230 Therefore, we override the DT_RELASZ entry here to
2231 make it not include the JMPREL relocs. Since the
2232 linker script arranges for .rela.plt to follow all
2233 other relocation sections, we don't have to worry
2234 about changing the DT_RELA entry. */
2235 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
2236 if (s != NULL)
2237 dyn.d_un.d_val -= s->size;
2238 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2239 break;
2240 }
2241 }
2242
2243 /* Fill in the first entry in the procedure linkage table. */
2244 if (splt->size > 0)
2245 {
2246 if (CFV4E_FLAG (output_bfd))
2247 {
2248 memcpy (splt->contents, elf_cfv4e_plt0_entry, CFV4E_PLT_ENTRY_SIZE);
2249 bfd_put_32 (output_bfd,
2250 (sgot->output_section->vma
2251 + sgot->output_offset + 4
2252 - (splt->output_section->vma + 2)),
2253 splt->contents + 2);
2254 bfd_put_32 (output_bfd,
2255 (sgot->output_section->vma
2256 + sgot->output_offset + 8
2257 - (splt->output_section->vma + 10) - 8),
2258 splt->contents + 12);
2259 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2260 = CFV4E_PLT_ENTRY_SIZE;
2261 }
2262 else if (CPU32_FLAG (output_bfd))
2263 {
2264 memcpy (splt->contents, elf_cpu32_plt0_entry, PLT_CPU32_ENTRY_SIZE);
2265 bfd_put_32 (output_bfd,
2266 (sgot->output_section->vma
2267 + sgot->output_offset + 4
2268 - (splt->output_section->vma + 2)),
2269 splt->contents + 4);
2270 bfd_put_32 (output_bfd,
2271 (sgot->output_section->vma
2272 + sgot->output_offset + 8
2273 - (splt->output_section->vma + 10)),
2274 splt->contents + 12);
2275 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2276 = PLT_CPU32_ENTRY_SIZE;
2277 }
2278 else
2279 {
2280 memcpy (splt->contents, elf_m68k_plt0_entry, PLT_ENTRY_SIZE);
2281 bfd_put_32 (output_bfd,
2282 (sgot->output_section->vma
2283 + sgot->output_offset + 4
2284 - (splt->output_section->vma + 2)),
2285 splt->contents + 4);
2286 bfd_put_32 (output_bfd,
2287 (sgot->output_section->vma
2288 + sgot->output_offset + 8
2289 - (splt->output_section->vma + 10)),
2290 splt->contents + 12);
2291 elf_section_data (splt->output_section)->this_hdr.sh_entsize
2292 = PLT_ENTRY_SIZE;
2293 }
2294 }
2295 }
2296
2297 /* Fill in the first three entries in the global offset table. */
2298 if (sgot->size > 0)
2299 {
2300 if (sdyn == NULL)
2301 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2302 else
2303 bfd_put_32 (output_bfd,
2304 sdyn->output_section->vma + sdyn->output_offset,
2305 sgot->contents);
2306 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2307 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2308 }
2309
2310 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2311
2312 return TRUE;
2313 }
2314
2315 /* Given a .data section and a .emreloc in-memory section, store
2316 relocation information into the .emreloc section which can be
2317 used at runtime to relocate the section. This is called by the
2318 linker when the --embedded-relocs switch is used. This is called
2319 after the add_symbols entry point has been called for all the
2320 objects, and before the final_link entry point is called. */
2321
2322 bfd_boolean
2323 bfd_m68k_elf32_create_embedded_relocs (abfd, info, datasec, relsec, errmsg)
2324 bfd *abfd;
2325 struct bfd_link_info *info;
2326 asection *datasec;
2327 asection *relsec;
2328 char **errmsg;
2329 {
2330 Elf_Internal_Shdr *symtab_hdr;
2331 Elf_Internal_Sym *isymbuf = NULL;
2332 Elf_Internal_Rela *internal_relocs = NULL;
2333 Elf_Internal_Rela *irel, *irelend;
2334 bfd_byte *p;
2335 bfd_size_type amt;
2336
2337 BFD_ASSERT (! info->relocatable);
2338
2339 *errmsg = NULL;
2340
2341 if (datasec->reloc_count == 0)
2342 return TRUE;
2343
2344 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2345
2346 /* Get a copy of the native relocations. */
2347 internal_relocs = (_bfd_elf_link_read_relocs
2348 (abfd, datasec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2349 info->keep_memory));
2350 if (internal_relocs == NULL)
2351 goto error_return;
2352
2353 amt = (bfd_size_type) datasec->reloc_count * 12;
2354 relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
2355 if (relsec->contents == NULL)
2356 goto error_return;
2357
2358 p = relsec->contents;
2359
2360 irelend = internal_relocs + datasec->reloc_count;
2361 for (irel = internal_relocs; irel < irelend; irel++, p += 12)
2362 {
2363 asection *targetsec;
2364
2365 /* We are going to write a four byte longword into the runtime
2366 reloc section. The longword will be the address in the data
2367 section which must be relocated. It is followed by the name
2368 of the target section NUL-padded or truncated to 8
2369 characters. */
2370
2371 /* We can only relocate absolute longword relocs at run time. */
2372 if (ELF32_R_TYPE (irel->r_info) != (int) R_68K_32)
2373 {
2374 *errmsg = _("unsupported reloc type");
2375 bfd_set_error (bfd_error_bad_value);
2376 goto error_return;
2377 }
2378
2379 /* Get the target section referred to by the reloc. */
2380 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2381 {
2382 /* A local symbol. */
2383 Elf_Internal_Sym *isym;
2384
2385 /* Read this BFD's local symbols if we haven't done so already. */
2386 if (isymbuf == NULL)
2387 {
2388 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2389 if (isymbuf == NULL)
2390 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2391 symtab_hdr->sh_info, 0,
2392 NULL, NULL, NULL);
2393 if (isymbuf == NULL)
2394 goto error_return;
2395 }
2396
2397 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2398 targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2399 }
2400 else
2401 {
2402 unsigned long indx;
2403 struct elf_link_hash_entry *h;
2404
2405 /* An external symbol. */
2406 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2407 h = elf_sym_hashes (abfd)[indx];
2408 BFD_ASSERT (h != NULL);
2409 if (h->root.type == bfd_link_hash_defined
2410 || h->root.type == bfd_link_hash_defweak)
2411 targetsec = h->root.u.def.section;
2412 else
2413 targetsec = NULL;
2414 }
2415
2416 bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
2417 memset (p + 4, 0, 8);
2418 if (targetsec != NULL)
2419 strncpy ((char *) p + 4, targetsec->output_section->name, 8);
2420 }
2421
2422 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2423 free (isymbuf);
2424 if (internal_relocs != NULL
2425 && elf_section_data (datasec)->relocs != internal_relocs)
2426 free (internal_relocs);
2427 return TRUE;
2428
2429 error_return:
2430 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2431 free (isymbuf);
2432 if (internal_relocs != NULL
2433 && elf_section_data (datasec)->relocs != internal_relocs)
2434 free (internal_relocs);
2435 return FALSE;
2436 }
2437
2438 static enum elf_reloc_type_class
2439 elf32_m68k_reloc_type_class (rela)
2440 const Elf_Internal_Rela *rela;
2441 {
2442 switch ((int) ELF32_R_TYPE (rela->r_info))
2443 {
2444 case R_68K_RELATIVE:
2445 return reloc_class_relative;
2446 case R_68K_JMP_SLOT:
2447 return reloc_class_plt;
2448 case R_68K_COPY:
2449 return reloc_class_copy;
2450 default:
2451 return reloc_class_normal;
2452 }
2453 }
2454
2455 /* Return address for Ith PLT stub in section PLT, for relocation REL
2456 or (bfd_vma) -1 if it should not be included. */
2457
2458 static bfd_vma
2459 elf_m68k_plt_sym_val (bfd_vma i, const asection *plt,
2460 const arelent *rel ATTRIBUTE_UNUSED)
2461 {
2462 if (CPU32_FLAG (plt->owner))
2463 return plt->vma + (i + 1) * PLT_CPU32_ENTRY_SIZE;
2464 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
2465 }
2466
2467 #define TARGET_BIG_SYM bfd_elf32_m68k_vec
2468 #define TARGET_BIG_NAME "elf32-m68k"
2469 #define ELF_MACHINE_CODE EM_68K
2470 #define ELF_MAXPAGESIZE 0x2000
2471 #define elf_backend_create_dynamic_sections \
2472 _bfd_elf_create_dynamic_sections
2473 #define bfd_elf32_bfd_link_hash_table_create \
2474 elf_m68k_link_hash_table_create
2475 #define bfd_elf32_bfd_final_link bfd_elf_gc_common_final_link
2476
2477 #define elf_backend_check_relocs elf_m68k_check_relocs
2478 #define elf_backend_adjust_dynamic_symbol \
2479 elf_m68k_adjust_dynamic_symbol
2480 #define elf_backend_size_dynamic_sections \
2481 elf_m68k_size_dynamic_sections
2482 #define elf_backend_relocate_section elf_m68k_relocate_section
2483 #define elf_backend_finish_dynamic_symbol \
2484 elf_m68k_finish_dynamic_symbol
2485 #define elf_backend_finish_dynamic_sections \
2486 elf_m68k_finish_dynamic_sections
2487 #define elf_backend_gc_mark_hook elf_m68k_gc_mark_hook
2488 #define elf_backend_gc_sweep_hook elf_m68k_gc_sweep_hook
2489 #define bfd_elf32_bfd_merge_private_bfd_data \
2490 elf32_m68k_merge_private_bfd_data
2491 #define bfd_elf32_bfd_set_private_flags \
2492 elf32_m68k_set_private_flags
2493 #define bfd_elf32_bfd_print_private_bfd_data \
2494 elf32_m68k_print_private_bfd_data
2495 #define elf_backend_reloc_type_class elf32_m68k_reloc_type_class
2496 #define elf_backend_plt_sym_val elf_m68k_plt_sym_val
2497 #define elf_backend_object_p elf32_m68k_object_p
2498
2499 #define elf_backend_can_gc_sections 1
2500 #define elf_backend_can_refcount 1
2501 #define elf_backend_want_got_plt 1
2502 #define elf_backend_plt_readonly 1
2503 #define elf_backend_want_plt_sym 0
2504 #define elf_backend_got_header_size 12
2505 #define elf_backend_rela_normal 1
2506
2507 #include "elf32-target.h"
This page took 0.083973 seconds and 3 git commands to generate.