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