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