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