source: trunk/src/binutils/bfd/elf32-m68k.c@ 106

Last change on this file since 106 was 10, checked in by bird, 22 years ago

Initial revision

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