* gas/sparc/sparc.exp (sparc_elf_setup): New.
[binutils.git] / bfd / elf64-x86-64.c
blob9c4a9d4ec18a5ccaa0feef9f7a9814f4ece1e35c
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
27 #include "elf/x86-64.h"
29 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
30 #define MINUS_ONE (~ (bfd_vma) 0)
32 /* The relocation "howto" table. Order of fields:
33 type, size, bitsize, pc_relative, complain_on_overflow,
34 special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */
35 static reloc_howto_type x86_64_elf_howto_table[] =
37 HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
38 bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0x00000000, 0x00000000,
39 false),
40 HOWTO(R_X86_64_64, 0, 4, 64, false, 0, complain_overflow_bitfield,
41 bfd_elf_generic_reloc, "R_X86_64_64", false, MINUS_ONE, MINUS_ONE,
42 false),
43 HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed,
44 bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0xffffffff, 0xffffffff,
45 true),
46 HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed,
47 bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0xffffffff, 0xffffffff,
48 false),
49 HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed,
50 bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0xffffffff, 0xffffffff,
51 true),
52 HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield,
53 bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0xffffffff, 0xffffffff,
54 false),
55 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, false, 0, complain_overflow_bitfield,
56 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, MINUS_ONE,
57 MINUS_ONE, false),
58 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, false, 0, complain_overflow_bitfield,
59 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, MINUS_ONE,
60 MINUS_ONE, false),
61 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, false, 0, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, MINUS_ONE,
63 MINUS_ONE, false),
64 HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true,0 , complain_overflow_signed,
65 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0xffffffff,
66 0xffffffff, true),
67 HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned,
68 bfd_elf_generic_reloc, "R_X86_64_32", false, 0xffffffff, 0xffffffff,
69 false),
70 HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed,
71 bfd_elf_generic_reloc, "R_X86_64_32S", false, 0xffffffff, 0xffffffff,
72 false),
73 HOWTO(R_X86_64_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
74 bfd_elf_generic_reloc, "R_X86_64_16", false, 0xffff, 0xffff, false),
75 HOWTO(R_X86_64_PC16,0, 1, 16, true, 0, complain_overflow_bitfield,
76 bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0xffff, 0xffff, true),
77 HOWTO(R_X86_64_8, 0, 0, 8, false, 0, complain_overflow_signed,
78 bfd_elf_generic_reloc, "R_X86_64_8", false, 0xff, 0xff, false),
79 HOWTO(R_X86_64_PC8, 0, 0, 8, true, 0, complain_overflow_signed,
80 bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0xff, 0xff, true),
82 /* GNU extension to record C++ vtable hierarchy. */
83 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, false, 0, complain_overflow_dont,
84 NULL, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
86 /* GNU extension to record C++ vtable member usage. */
87 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, false, 0, complain_overflow_dont,
88 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0,
89 false)
92 /* Map BFD relocs to the x86_64 elf relocs. */
93 struct elf_reloc_map
95 bfd_reloc_code_real_type bfd_reloc_val;
96 unsigned char elf_reloc_val;
99 static const struct elf_reloc_map x86_64_reloc_map[] =
101 { BFD_RELOC_NONE, R_X86_64_NONE, },
102 { BFD_RELOC_64, R_X86_64_64, },
103 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
104 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
105 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
106 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
107 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
108 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
109 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
110 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
111 { BFD_RELOC_32, R_X86_64_32, },
112 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
113 { BFD_RELOC_16, R_X86_64_16, },
114 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
115 { BFD_RELOC_8, R_X86_64_8, },
116 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
117 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
118 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
121 static reloc_howto_type *elf64_x86_64_reloc_type_lookup
122 PARAMS ((bfd *, bfd_reloc_code_real_type));
123 static void elf64_x86_64_info_to_howto
124 PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
125 static boolean elf64_x86_64_grok_prstatus
126 PARAMS ((bfd *, Elf_Internal_Note *));
127 static boolean elf64_x86_64_grok_psinfo
128 PARAMS ((bfd *, Elf_Internal_Note *));
129 static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create
130 PARAMS ((bfd *));
131 static boolean elf64_x86_64_elf_object_p PARAMS ((bfd *abfd));
132 static boolean create_got_section
133 PARAMS((bfd *, struct bfd_link_info *));
134 static boolean elf64_x86_64_create_dynamic_sections
135 PARAMS((bfd *, struct bfd_link_info *));
136 static void elf64_x86_64_copy_indirect_symbol
137 PARAMS ((struct elf_backend_data *, struct elf_link_hash_entry *,
138 struct elf_link_hash_entry *));
139 static boolean elf64_x86_64_check_relocs
140 PARAMS ((bfd *, struct bfd_link_info *, asection *sec,
141 const Elf_Internal_Rela *));
142 static asection *elf64_x86_64_gc_mark_hook
143 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
144 struct elf_link_hash_entry *, Elf_Internal_Sym *));
146 static boolean elf64_x86_64_gc_sweep_hook
147 PARAMS ((bfd *, struct bfd_link_info *, asection *,
148 const Elf_Internal_Rela *));
150 static struct bfd_hash_entry *link_hash_newfunc
151 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
152 static boolean elf64_x86_64_adjust_dynamic_symbol
153 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
155 static boolean allocate_dynrelocs
156 PARAMS ((struct elf_link_hash_entry *, PTR));
157 static boolean readonly_dynrelocs
158 PARAMS ((struct elf_link_hash_entry *, PTR));
159 static boolean elf64_x86_64_size_dynamic_sections
160 PARAMS ((bfd *, struct bfd_link_info *));
161 static boolean elf64_x86_64_relocate_section
162 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
163 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
164 static boolean elf64_x86_64_finish_dynamic_symbol
165 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
166 Elf_Internal_Sym *sym));
167 static boolean elf64_x86_64_finish_dynamic_sections
168 PARAMS ((bfd *, struct bfd_link_info *));
169 static enum elf_reloc_type_class elf64_x86_64_reloc_type_class
170 PARAMS ((const Elf_Internal_Rela *));
172 /* Given a BFD reloc type, return a HOWTO structure. */
173 static reloc_howto_type *
174 elf64_x86_64_reloc_type_lookup (abfd, code)
175 bfd *abfd ATTRIBUTE_UNUSED;
176 bfd_reloc_code_real_type code;
178 unsigned int i;
179 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
180 i++)
182 if (x86_64_reloc_map[i].bfd_reloc_val == code)
183 return &x86_64_elf_howto_table[i];
185 return 0;
188 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
190 static void
191 elf64_x86_64_info_to_howto (abfd, cache_ptr, dst)
192 bfd *abfd ATTRIBUTE_UNUSED;
193 arelent *cache_ptr;
194 Elf64_Internal_Rela *dst;
196 unsigned r_type, i;
198 r_type = ELF64_R_TYPE (dst->r_info);
199 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
201 BFD_ASSERT (r_type <= (unsigned int) R_X86_64_PC8);
202 i = r_type;
204 else
206 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
207 i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_PC8 - 1);
209 cache_ptr->howto = &x86_64_elf_howto_table[i];
210 BFD_ASSERT (r_type == cache_ptr->howto->type);
213 /* Support for core dump NOTE sections. */
214 static boolean
215 elf64_x86_64_grok_prstatus (abfd, note)
216 bfd *abfd;
217 Elf_Internal_Note *note;
219 int offset;
220 size_t raw_size;
222 switch (note->descsz)
224 default:
225 return false;
227 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
228 /* pr_cursig */
229 elf_tdata (abfd)->core_signal
230 = bfd_get_16 (abfd, note->descdata + 12);
232 /* pr_pid */
233 elf_tdata (abfd)->core_pid
234 = bfd_get_32 (abfd, note->descdata + 32);
236 /* pr_reg */
237 offset = 112;
238 raw_size = 216;
240 break;
243 /* Make a ".reg/999" section. */
244 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
245 raw_size, note->descpos + offset);
248 static boolean
249 elf64_x86_64_grok_psinfo (abfd, note)
250 bfd *abfd;
251 Elf_Internal_Note *note;
253 switch (note->descsz)
255 default:
256 return false;
258 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
259 elf_tdata (abfd)->core_program
260 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
261 elf_tdata (abfd)->core_command
262 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
265 /* Note that for some reason, a spurious space is tacked
266 onto the end of the args in some (at least one anyway)
267 implementations, so strip it off if it exists. */
270 char *command = elf_tdata (abfd)->core_command;
271 int n = strlen (command);
273 if (0 < n && command[n - 1] == ' ')
274 command[n - 1] = '\0';
277 return true;
280 /* Functions for the x86-64 ELF linker. */
282 /* The name of the dynamic interpreter. This is put in the .interp
283 section. */
285 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
287 /* The size in bytes of an entry in the global offset table. */
289 #define GOT_ENTRY_SIZE 8
291 /* The size in bytes of an entry in the procedure linkage table. */
293 #define PLT_ENTRY_SIZE 16
295 /* The first entry in a procedure linkage table looks like this. See the
296 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
298 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
300 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
301 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
302 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
305 /* Subsequent entries in a procedure linkage table look like this. */
307 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
309 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
310 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
311 0x68, /* pushq immediate */
312 0, 0, 0, 0, /* replaced with index into relocation table. */
313 0xe9, /* jmp relative */
314 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
317 /* The x86-64 linker needs to keep track of the number of relocs that
318 it decides to copy as dynamic relocs in check_relocs for each symbol.
319 This is so that it can later discard them if they are found to be
320 unnecessary. We store the information in a field extending the
321 regular ELF linker hash table. */
323 struct elf64_x86_64_dyn_relocs
325 /* Next section. */
326 struct elf64_x86_64_dyn_relocs *next;
328 /* The input section of the reloc. */
329 asection *sec;
331 /* Total number of relocs copied for the input section. */
332 bfd_size_type count;
334 /* Number of pc-relative relocs copied for the input section. */
335 bfd_size_type pc_count;
338 /* x86-64 ELF linker hash entry. */
340 struct elf64_x86_64_link_hash_entry
342 struct elf_link_hash_entry elf;
344 /* Track dynamic relocs copied for this symbol. */
345 struct elf64_x86_64_dyn_relocs *dyn_relocs;
348 /* x86-64 ELF linker hash table. */
350 struct elf64_x86_64_link_hash_table
352 struct elf_link_hash_table elf;
354 /* Short-cuts to get to dynamic linker sections. */
355 asection *sgot;
356 asection *sgotplt;
357 asection *srelgot;
358 asection *splt;
359 asection *srelplt;
360 asection *sdynbss;
361 asection *srelbss;
363 /* Small local sym to section mapping cache. */
364 struct sym_sec_cache sym_sec;
367 /* Get the x86-64 ELF linker hash table from a link_info structure. */
369 #define elf64_x86_64_hash_table(p) \
370 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
372 /* Create an entry in an x86-64 ELF linker hash table. */
374 static struct bfd_hash_entry *
375 link_hash_newfunc (entry, table, string)
376 struct bfd_hash_entry *entry;
377 struct bfd_hash_table *table;
378 const char *string;
380 /* Allocate the structure if it has not already been allocated by a
381 subclass. */
382 if (entry == NULL)
384 entry = bfd_hash_allocate (table,
385 sizeof (struct elf64_x86_64_link_hash_entry));
386 if (entry == NULL)
387 return entry;
390 /* Call the allocation method of the superclass. */
391 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
392 if (entry != NULL)
394 struct elf64_x86_64_link_hash_entry *eh;
396 eh = (struct elf64_x86_64_link_hash_entry *) entry;
397 eh->dyn_relocs = NULL;
400 return entry;
403 /* Create an X86-64 ELF linker hash table. */
405 static struct bfd_link_hash_table *
406 elf64_x86_64_link_hash_table_create (abfd)
407 bfd *abfd;
409 struct elf64_x86_64_link_hash_table *ret;
410 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
412 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
413 if (ret == NULL)
414 return NULL;
416 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
418 free (ret);
419 return NULL;
422 ret->sgot = NULL;
423 ret->sgotplt = NULL;
424 ret->srelgot = NULL;
425 ret->splt = NULL;
426 ret->srelplt = NULL;
427 ret->sdynbss = NULL;
428 ret->srelbss = NULL;
429 ret->sym_sec.abfd = NULL;
431 return &ret->elf.root;
434 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
435 shortcuts to them in our hash table. */
437 static boolean
438 create_got_section (dynobj, info)
439 bfd *dynobj;
440 struct bfd_link_info *info;
442 struct elf64_x86_64_link_hash_table *htab;
444 if (! _bfd_elf_create_got_section (dynobj, info))
445 return false;
447 htab = elf64_x86_64_hash_table (info);
448 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
449 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
450 if (!htab->sgot || !htab->sgotplt)
451 abort ();
453 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
454 if (htab->srelgot == NULL
455 || ! bfd_set_section_flags (dynobj, htab->srelgot,
456 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
457 | SEC_IN_MEMORY | SEC_LINKER_CREATED
458 | SEC_READONLY))
459 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
460 return false;
461 return true;
464 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
465 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
466 hash table. */
468 static boolean
469 elf64_x86_64_create_dynamic_sections (dynobj, info)
470 bfd *dynobj;
471 struct bfd_link_info *info;
473 struct elf64_x86_64_link_hash_table *htab;
475 htab = elf64_x86_64_hash_table (info);
476 if (!htab->sgot && !create_got_section (dynobj, info))
477 return false;
479 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
480 return false;
482 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
483 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
484 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
485 if (!info->shared)
486 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
488 if (!htab->splt || !htab->srelplt || !htab->sdynbss
489 || (!info->shared && !htab->srelbss))
490 abort ();
492 return true;
495 /* Copy the extra info we tack onto an elf_link_hash_entry. */
497 static void
498 elf64_x86_64_copy_indirect_symbol (bed, dir, ind)
499 struct elf_backend_data *bed;
500 struct elf_link_hash_entry *dir, *ind;
502 struct elf64_x86_64_link_hash_entry *edir, *eind;
504 edir = (struct elf64_x86_64_link_hash_entry *) dir;
505 eind = (struct elf64_x86_64_link_hash_entry *) ind;
507 if (eind->dyn_relocs != NULL)
509 if (edir->dyn_relocs != NULL)
511 struct elf64_x86_64_dyn_relocs **pp;
512 struct elf64_x86_64_dyn_relocs *p;
514 if (ind->root.type == bfd_link_hash_indirect)
515 abort ();
517 /* Add reloc counts against the weak sym to the strong sym
518 list. Merge any entries against the same section. */
519 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
521 struct elf64_x86_64_dyn_relocs *q;
523 for (q = edir->dyn_relocs; q != NULL; q = q->next)
524 if (q->sec == p->sec)
526 q->pc_count += p->pc_count;
527 q->count += p->count;
528 *pp = p->next;
529 break;
531 if (q == NULL)
532 pp = &p->next;
534 *pp = edir->dyn_relocs;
537 edir->dyn_relocs = eind->dyn_relocs;
538 eind->dyn_relocs = NULL;
541 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
544 static boolean
545 elf64_x86_64_elf_object_p (abfd)
546 bfd *abfd;
548 /* Set the right machine number for an x86-64 elf64 file. */
549 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
550 return true;
553 /* Look through the relocs for a section during the first phase, and
554 calculate needed space in the global offset table, procedure
555 linkage table, and dynamic reloc sections. */
557 static boolean
558 elf64_x86_64_check_relocs (abfd, info, sec, relocs)
559 bfd *abfd;
560 struct bfd_link_info *info;
561 asection *sec;
562 const Elf_Internal_Rela *relocs;
564 struct elf64_x86_64_link_hash_table *htab;
565 Elf_Internal_Shdr *symtab_hdr;
566 struct elf_link_hash_entry **sym_hashes;
567 const Elf_Internal_Rela *rel;
568 const Elf_Internal_Rela *rel_end;
569 asection *sreloc;
571 if (info->relocateable)
572 return true;
574 htab = elf64_x86_64_hash_table (info);
575 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
576 sym_hashes = elf_sym_hashes (abfd);
578 sreloc = NULL;
580 rel_end = relocs + sec->reloc_count;
581 for (rel = relocs; rel < rel_end; rel++)
583 unsigned long r_symndx;
584 struct elf_link_hash_entry *h;
586 r_symndx = ELF64_R_SYM (rel->r_info);
588 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
590 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
591 bfd_archive_filename (abfd),
592 r_symndx);
593 return false;
596 if (r_symndx < symtab_hdr->sh_info)
597 h = NULL;
598 else
599 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
601 switch (ELF64_R_TYPE (rel->r_info))
603 case R_X86_64_GOT32:
604 case R_X86_64_GOTPCREL:
605 /* This symbol requires a global offset table entry. */
606 if (h != NULL)
608 h->got.refcount += 1;
610 else
612 bfd_signed_vma *local_got_refcounts;
614 /* This is a global offset table entry for a local symbol. */
615 local_got_refcounts = elf_local_got_refcounts (abfd);
616 if (local_got_refcounts == NULL)
618 bfd_size_type size;
620 size = symtab_hdr->sh_info;
621 size *= sizeof (bfd_signed_vma);
622 local_got_refcounts = ((bfd_signed_vma *)
623 bfd_zalloc (abfd, size));
624 if (local_got_refcounts == NULL)
625 return false;
626 elf_local_got_refcounts (abfd) = local_got_refcounts;
628 local_got_refcounts[r_symndx] += 1;
630 /* Fall through */
632 //case R_X86_64_GOTPCREL:
633 if (htab->sgot == NULL)
635 if (htab->elf.dynobj == NULL)
636 htab->elf.dynobj = abfd;
637 if (!create_got_section (htab->elf.dynobj, info))
638 return false;
640 break;
642 case R_X86_64_PLT32:
643 /* This symbol requires a procedure linkage table entry. We
644 actually build the entry in adjust_dynamic_symbol,
645 because this might be a case of linking PIC code which is
646 never referenced by a dynamic object, in which case we
647 don't need to generate a procedure linkage table entry
648 after all. */
650 /* If this is a local symbol, we resolve it directly without
651 creating a procedure linkage table entry. */
652 if (h == NULL)
653 continue;
655 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
656 h->plt.refcount += 1;
657 break;
659 case R_X86_64_8:
660 case R_X86_64_16:
661 case R_X86_64_32:
662 case R_X86_64_32S:
663 /* Let's help debug shared library creation. These relocs
664 cannot be used in shared libs. Don't error out for
665 sections we don't care about, such as debug sections or
666 non-constant sections. */
667 if (info->shared
668 && (sec->flags & SEC_ALLOC) != 0
669 && (sec->flags & SEC_READONLY) != 0)
671 (*_bfd_error_handler)
672 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
673 bfd_archive_filename (abfd),
674 x86_64_elf_howto_table[ELF64_R_TYPE (rel->r_info)].name);
675 bfd_set_error (bfd_error_bad_value);
676 return false;
678 /* Fall through. */
680 case R_X86_64_PC8:
681 case R_X86_64_PC16:
682 case R_X86_64_PC32:
683 case R_X86_64_64:
684 if (h != NULL && !info->shared)
686 /* If this reloc is in a read-only section, we might
687 need a copy reloc. We can't check reliably at this
688 stage whether the section is read-only, as input
689 sections have not yet been mapped to output sections.
690 Tentatively set the flag for now, and correct in
691 adjust_dynamic_symbol. */
692 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
694 /* We may need a .plt entry if the function this reloc
695 refers to is in a shared lib. */
696 h->plt.refcount += 1;
699 /* If we are creating a shared library, and this is a reloc
700 against a global symbol, or a non PC relative reloc
701 against a local symbol, then we need to copy the reloc
702 into the shared library. However, if we are linking with
703 -Bsymbolic, we do not need to copy a reloc against a
704 global symbol which is defined in an object we are
705 including in the link (i.e., DEF_REGULAR is set). At
706 this point we have not seen all the input files, so it is
707 possible that DEF_REGULAR is not set now but will be set
708 later (it is never cleared). In case of a weak definition,
709 DEF_REGULAR may be cleared later by a strong definition in
710 a shared library. We account for that possibility below by
711 storing information in the relocs_copied field of the hash
712 table entry. A similar situation occurs when creating
713 shared libraries and symbol visibility changes render the
714 symbol local.
716 If on the other hand, we are creating an executable, we
717 may need to keep relocations for symbols satisfied by a
718 dynamic library if we manage to avoid copy relocs for the
719 symbol. */
720 if ((info->shared
721 && (sec->flags & SEC_ALLOC) != 0
722 && (((ELF64_R_TYPE (rel->r_info) != R_X86_64_PC8)
723 && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC16)
724 && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32))
725 || (h != NULL
726 && (! info->symbolic
727 || h->root.type == bfd_link_hash_defweak
728 || (h->elf_link_hash_flags
729 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
730 || (!info->shared
731 && (sec->flags & SEC_ALLOC) != 0
732 && h != NULL
733 && (h->root.type == bfd_link_hash_defweak
734 || (h->elf_link_hash_flags
735 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
737 struct elf64_x86_64_dyn_relocs *p;
738 struct elf64_x86_64_dyn_relocs **head;
740 /* We must copy these reloc types into the output file.
741 Create a reloc section in dynobj and make room for
742 this reloc. */
743 if (sreloc == NULL)
745 const char *name;
746 bfd *dynobj;
748 name = (bfd_elf_string_from_elf_section
749 (abfd,
750 elf_elfheader (abfd)->e_shstrndx,
751 elf_section_data (sec)->rel_hdr.sh_name));
752 if (name == NULL)
753 return false;
755 if (strncmp (name, ".rela", 5) != 0
756 || strcmp (bfd_get_section_name (abfd, sec),
757 name + 5) != 0)
759 (*_bfd_error_handler)
760 (_("%s: bad relocation section name `%s\'"),
761 bfd_archive_filename (abfd), name);
764 if (htab->elf.dynobj == NULL)
765 htab->elf.dynobj = abfd;
767 dynobj = htab->elf.dynobj;
769 sreloc = bfd_get_section_by_name (dynobj, name);
770 if (sreloc == NULL)
772 flagword flags;
774 sreloc = bfd_make_section (dynobj, name);
775 flags = (SEC_HAS_CONTENTS | SEC_READONLY
776 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
777 if ((sec->flags & SEC_ALLOC) != 0)
778 flags |= SEC_ALLOC | SEC_LOAD;
779 if (sreloc == NULL
780 || ! bfd_set_section_flags (dynobj, sreloc, flags)
781 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
782 return false;
784 elf_section_data (sec)->sreloc = sreloc;
787 /* If this is a global symbol, we count the number of
788 relocations we need for this symbol. */
789 if (h != NULL)
791 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
793 else
795 /* Track dynamic relocs needed for local syms too.
796 We really need local syms available to do this
797 easily. Oh well. */
799 asection *s;
800 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
801 sec, r_symndx);
802 if (s == NULL)
803 return false;
805 head = ((struct elf64_x86_64_dyn_relocs **)
806 &elf_section_data (s)->local_dynrel);
809 p = *head;
810 if (p == NULL || p->sec != sec)
812 bfd_size_type amt = sizeof *p;
813 p = ((struct elf64_x86_64_dyn_relocs *)
814 bfd_alloc (htab->elf.dynobj, amt));
815 if (p == NULL)
816 return false;
817 p->next = *head;
818 *head = p;
819 p->sec = sec;
820 p->count = 0;
821 p->pc_count = 0;
824 p->count += 1;
825 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8
826 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16
827 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)
828 p->pc_count += 1;
830 break;
832 /* This relocation describes the C++ object vtable hierarchy.
833 Reconstruct it for later use during GC. */
834 case R_X86_64_GNU_VTINHERIT:
835 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
836 return false;
837 break;
839 /* This relocation describes which C++ vtable entries are actually
840 used. Record for later use during GC. */
841 case R_X86_64_GNU_VTENTRY:
842 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
843 return false;
844 break;
846 default:
847 break;
851 return true;
854 /* Return the section that should be marked against GC for a given
855 relocation. */
857 static asection *
858 elf64_x86_64_gc_mark_hook (sec, info, rel, h, sym)
859 asection *sec;
860 struct bfd_link_info *info ATTRIBUTE_UNUSED;
861 Elf_Internal_Rela *rel;
862 struct elf_link_hash_entry *h;
863 Elf_Internal_Sym *sym;
865 if (h != NULL)
867 switch (ELF64_R_TYPE (rel->r_info))
869 case R_X86_64_GNU_VTINHERIT:
870 case R_X86_64_GNU_VTENTRY:
871 break;
873 default:
874 switch (h->root.type)
876 case bfd_link_hash_defined:
877 case bfd_link_hash_defweak:
878 return h->root.u.def.section;
880 case bfd_link_hash_common:
881 return h->root.u.c.p->section;
883 default:
884 break;
888 else
889 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
891 return NULL;
894 /* Update the got entry reference counts for the section being removed. */
896 static boolean
897 elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs)
898 bfd *abfd;
899 struct bfd_link_info *info;
900 asection *sec;
901 const Elf_Internal_Rela *relocs;
903 Elf_Internal_Shdr *symtab_hdr;
904 struct elf_link_hash_entry **sym_hashes;
905 bfd_signed_vma *local_got_refcounts;
906 const Elf_Internal_Rela *rel, *relend;
907 unsigned long r_symndx;
908 struct elf_link_hash_entry *h;
910 elf_section_data (sec)->local_dynrel = NULL;
912 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
913 sym_hashes = elf_sym_hashes (abfd);
914 local_got_refcounts = elf_local_got_refcounts (abfd);
916 relend = relocs + sec->reloc_count;
917 for (rel = relocs; rel < relend; rel++)
918 switch (ELF64_R_TYPE (rel->r_info))
920 case R_X86_64_GOT32:
921 case R_X86_64_GOTPCREL:
922 r_symndx = ELF64_R_SYM (rel->r_info);
923 if (r_symndx >= symtab_hdr->sh_info)
925 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
926 if (h->got.refcount > 0)
927 h->got.refcount -= 1;
929 else if (local_got_refcounts != NULL)
931 if (local_got_refcounts[r_symndx] > 0)
932 local_got_refcounts[r_symndx] -= 1;
934 break;
936 case R_X86_64_8:
937 case R_X86_64_16:
938 case R_X86_64_32:
939 case R_X86_64_64:
940 case R_X86_64_32S:
941 case R_X86_64_PC8:
942 case R_X86_64_PC16:
943 case R_X86_64_PC32:
944 r_symndx = ELF64_R_SYM (rel->r_info);
945 if (r_symndx >= symtab_hdr->sh_info)
947 struct elf64_x86_64_link_hash_entry *eh;
948 struct elf64_x86_64_dyn_relocs **pp;
949 struct elf64_x86_64_dyn_relocs *p;
951 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
953 if (!info->shared && h->plt.refcount > 0)
954 h->plt.refcount -= 1;
956 eh = (struct elf64_x86_64_link_hash_entry *) h;
958 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
959 if (p->sec == sec)
961 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8
962 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16
963 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)
964 p->pc_count -= 1;
965 p->count -= 1;
966 if (p->count == 0)
967 *pp = p->next;
968 break;
971 break;
974 case R_X86_64_PLT32:
975 r_symndx = ELF64_R_SYM (rel->r_info);
976 if (r_symndx >= symtab_hdr->sh_info)
978 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
979 if (h->plt.refcount > 0)
980 h->plt.refcount -= 1;
982 break;
984 default:
985 break;
988 return true;
991 /* Adjust a symbol defined by a dynamic object and referenced by a
992 regular object. The current definition is in some section of the
993 dynamic object, but we're not including those sections. We have to
994 change the definition to something the rest of the link can
995 understand. */
997 static boolean
998 elf64_x86_64_adjust_dynamic_symbol (info, h)
999 struct bfd_link_info *info;
1000 struct elf_link_hash_entry *h;
1002 struct elf64_x86_64_link_hash_table *htab;
1003 struct elf64_x86_64_link_hash_entry * eh;
1004 struct elf64_x86_64_dyn_relocs *p;
1005 asection *s;
1006 unsigned int power_of_two;
1008 /* If this is a function, put it in the procedure linkage table. We
1009 will fill in the contents of the procedure linkage table later,
1010 when we know the address of the .got section. */
1011 if (h->type == STT_FUNC
1012 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1014 if (h->plt.refcount <= 0
1015 || (! info->shared
1016 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1017 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1018 && h->root.type != bfd_link_hash_undefweak
1019 && h->root.type != bfd_link_hash_undefined))
1021 /* This case can occur if we saw a PLT32 reloc in an input
1022 file, but the symbol was never referred to by a dynamic
1023 object, or if all references were garbage collected. In
1024 such a case, we don't actually need to build a procedure
1025 linkage table, and we can just do a PC32 reloc instead. */
1026 h->plt.offset = (bfd_vma) -1;
1027 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1030 return true;
1032 else
1033 /* It's possible that we incorrectly decided a .plt reloc was
1034 needed for an R_X86_64_PC32 reloc to a non-function sym in
1035 check_relocs. We can't decide accurately between function and
1036 non-function syms in check-relocs; Objects loaded later in
1037 the link may change h->type. So fix it now. */
1038 h->plt.offset = (bfd_vma) -1;
1040 /* If this is a weak symbol, and there is a real definition, the
1041 processor independent code will have arranged for us to see the
1042 real definition first, and we can just use the same value. */
1043 if (h->weakdef != NULL)
1045 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1046 || h->weakdef->root.type == bfd_link_hash_defweak);
1047 h->root.u.def.section = h->weakdef->root.u.def.section;
1048 h->root.u.def.value = h->weakdef->root.u.def.value;
1049 return true;
1052 /* This is a reference to a symbol defined by a dynamic object which
1053 is not a function. */
1055 /* If we are creating a shared library, we must presume that the
1056 only references to the symbol are via the global offset table.
1057 For such cases we need not do anything here; the relocations will
1058 be handled correctly by relocate_section. */
1059 if (info->shared)
1060 return true;
1062 /* If there are no references to this symbol that do not use the
1063 GOT, we don't need to generate a copy reloc. */
1064 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1065 return true;
1067 /* If -z nocopyreloc was given, we won't generate them either. */
1068 if (info->nocopyreloc)
1070 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1071 return true;
1074 eh = (struct elf64_x86_64_link_hash_entry *) h;
1075 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1077 s = p->sec->output_section;
1078 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1079 break;
1082 /* If we didn't find any dynamic relocs in read-only sections, then
1083 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1084 if (p == NULL)
1086 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1087 return true;
1090 /* We must allocate the symbol in our .dynbss section, which will
1091 become part of the .bss section of the executable. There will be
1092 an entry for this symbol in the .dynsym section. The dynamic
1093 object will contain position independent code, so all references
1094 from the dynamic object to this symbol will go through the global
1095 offset table. The dynamic linker will use the .dynsym entry to
1096 determine the address it must put in the global offset table, so
1097 both the dynamic object and the regular object will refer to the
1098 same memory location for the variable. */
1100 htab = elf64_x86_64_hash_table (info);
1102 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1103 to copy the initial value out of the dynamic object and into the
1104 runtime process image. */
1105 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1107 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
1108 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1111 /* We need to figure out the alignment required for this symbol. I
1112 have no idea how ELF linkers handle this. 16-bytes is the size
1113 of the largest type that requires hard alignment -- long double. */
1114 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1115 this construct. */
1116 power_of_two = bfd_log2 (h->size);
1117 if (power_of_two > 4)
1118 power_of_two = 4;
1120 /* Apply the required alignment. */
1121 s = htab->sdynbss;
1122 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
1123 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1125 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1126 return false;
1129 /* Define the symbol as being at this point in the section. */
1130 h->root.u.def.section = s;
1131 h->root.u.def.value = s->_raw_size;
1133 /* Increment the section size to make room for the symbol. */
1134 s->_raw_size += h->size;
1136 return true;
1139 /* This is the condition under which elf64_x86_64_finish_dynamic_symbol
1140 will be called from elflink.h. If elflink.h doesn't call our
1141 finish_dynamic_symbol routine, we'll need to do something about
1142 initializing any .plt and .got entries in elf64_x86_64_relocate_section. */
1143 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1144 ((DYN) \
1145 && ((INFO)->shared \
1146 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1147 && ((H)->dynindx != -1 \
1148 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1150 /* Allocate space in .plt, .got and associated reloc sections for
1151 dynamic relocs. */
1153 static boolean
1154 allocate_dynrelocs (h, inf)
1155 struct elf_link_hash_entry *h;
1156 PTR inf;
1158 struct bfd_link_info *info;
1159 struct elf64_x86_64_link_hash_table *htab;
1160 struct elf64_x86_64_link_hash_entry *eh;
1161 struct elf64_x86_64_dyn_relocs *p;
1163 if (h->root.type == bfd_link_hash_indirect)
1164 return true;
1166 if (h->root.type == bfd_link_hash_warning)
1167 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1169 info = (struct bfd_link_info *) inf;
1170 htab = elf64_x86_64_hash_table (info);
1172 if (htab->elf.dynamic_sections_created
1173 && h->plt.refcount > 0)
1175 /* Make sure this symbol is output as a dynamic symbol.
1176 Undefined weak syms won't yet be marked as dynamic. */
1177 if (h->dynindx == -1
1178 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1180 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1181 return false;
1184 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
1186 asection *s = htab->splt;
1188 /* If this is the first .plt entry, make room for the special
1189 first entry. */
1190 if (s->_raw_size == 0)
1191 s->_raw_size += PLT_ENTRY_SIZE;
1193 h->plt.offset = s->_raw_size;
1195 /* If this symbol is not defined in a regular file, and we are
1196 not generating a shared library, then set the symbol to this
1197 location in the .plt. This is required to make function
1198 pointers compare as equal between the normal executable and
1199 the shared library. */
1200 if (! info->shared
1201 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1203 h->root.u.def.section = s;
1204 h->root.u.def.value = h->plt.offset;
1207 /* Make room for this entry. */
1208 s->_raw_size += PLT_ENTRY_SIZE;
1210 /* We also need to make an entry in the .got.plt section, which
1211 will be placed in the .got section by the linker script. */
1212 htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
1214 /* We also need to make an entry in the .rela.plt section. */
1215 htab->srelplt->_raw_size += sizeof (Elf64_External_Rela);
1217 else
1219 h->plt.offset = (bfd_vma) -1;
1220 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1223 else
1225 h->plt.offset = (bfd_vma) -1;
1226 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1229 if (h->got.refcount > 0)
1231 asection *s;
1232 boolean dyn;
1234 /* Make sure this symbol is output as a dynamic symbol.
1235 Undefined weak syms won't yet be marked as dynamic. */
1236 if (h->dynindx == -1
1237 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1239 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1240 return false;
1243 s = htab->sgot;
1244 h->got.offset = s->_raw_size;
1245 s->_raw_size += GOT_ENTRY_SIZE;
1246 dyn = htab->elf.dynamic_sections_created;
1247 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
1248 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
1250 else
1251 h->got.offset = (bfd_vma) -1;
1253 eh = (struct elf64_x86_64_link_hash_entry *) h;
1254 if (eh->dyn_relocs == NULL)
1255 return true;
1257 /* In the shared -Bsymbolic case, discard space allocated for
1258 dynamic pc-relative relocs against symbols which turn out to be
1259 defined in regular objects. For the normal shared case, discard
1260 space for pc-relative relocs that have become local due to symbol
1261 visibility changes. */
1263 if (info->shared)
1265 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1266 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1267 || info->symbolic))
1269 struct elf64_x86_64_dyn_relocs **pp;
1271 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1273 p->count -= p->pc_count;
1274 p->pc_count = 0;
1275 if (p->count == 0)
1276 *pp = p->next;
1277 else
1278 pp = &p->next;
1282 else
1284 /* For the non-shared case, discard space for relocs against
1285 symbols which turn out to need copy relocs or are not
1286 dynamic. */
1288 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1289 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1290 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1291 || (htab->elf.dynamic_sections_created
1292 && (h->root.type == bfd_link_hash_undefweak
1293 || h->root.type == bfd_link_hash_undefined))))
1295 /* Make sure this symbol is output as a dynamic symbol.
1296 Undefined weak syms won't yet be marked as dynamic. */
1297 if (h->dynindx == -1
1298 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1300 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1301 return false;
1304 /* If that succeeded, we know we'll be keeping all the
1305 relocs. */
1306 if (h->dynindx != -1)
1307 goto keep;
1310 eh->dyn_relocs = NULL;
1312 keep: ;
1315 /* Finally, allocate space. */
1316 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1318 asection *sreloc = elf_section_data (p->sec)->sreloc;
1319 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
1322 return true;
1325 /* Find any dynamic relocs that apply to read-only sections. */
1327 static boolean
1328 readonly_dynrelocs (h, inf)
1329 struct elf_link_hash_entry *h;
1330 PTR inf;
1332 struct elf64_x86_64_link_hash_entry *eh;
1333 struct elf64_x86_64_dyn_relocs *p;
1335 if (h->root.type == bfd_link_hash_warning)
1336 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1338 eh = (struct elf64_x86_64_link_hash_entry *) h;
1339 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1341 asection *s = p->sec->output_section;
1343 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1345 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1347 info->flags |= DF_TEXTREL;
1349 /* Not an error, just cut short the traversal. */
1350 return false;
1353 return true;
1356 /* Set the sizes of the dynamic sections. */
1358 static boolean
1359 elf64_x86_64_size_dynamic_sections (output_bfd, info)
1360 bfd *output_bfd ATTRIBUTE_UNUSED;
1361 struct bfd_link_info *info;
1363 struct elf64_x86_64_link_hash_table *htab;
1364 bfd *dynobj;
1365 asection *s;
1366 boolean relocs;
1367 bfd *ibfd;
1369 htab = elf64_x86_64_hash_table (info);
1370 dynobj = htab->elf.dynobj;
1371 if (dynobj == NULL)
1372 abort ();
1374 if (htab->elf.dynamic_sections_created)
1376 /* Set the contents of the .interp section to the interpreter. */
1377 if (! info->shared)
1379 s = bfd_get_section_by_name (dynobj, ".interp");
1380 if (s == NULL)
1381 abort ();
1382 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1383 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1387 /* Set up .got offsets for local syms, and space for local dynamic
1388 relocs. */
1389 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1391 bfd_signed_vma *local_got;
1392 bfd_signed_vma *end_local_got;
1393 bfd_size_type locsymcount;
1394 Elf_Internal_Shdr *symtab_hdr;
1395 asection *srel;
1397 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1398 continue;
1400 for (s = ibfd->sections; s != NULL; s = s->next)
1402 struct elf64_x86_64_dyn_relocs *p;
1404 for (p = *((struct elf64_x86_64_dyn_relocs **)
1405 &elf_section_data (s)->local_dynrel);
1406 p != NULL;
1407 p = p->next)
1409 if (!bfd_is_abs_section (p->sec)
1410 && bfd_is_abs_section (p->sec->output_section))
1412 /* Input section has been discarded, either because
1413 it is a copy of a linkonce section or due to
1414 linker script /DISCARD/, so we'll be discarding
1415 the relocs too. */
1417 else if (p->count != 0)
1419 srel = elf_section_data (p->sec)->sreloc;
1420 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
1421 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1422 info->flags |= DF_TEXTREL;
1428 local_got = elf_local_got_refcounts (ibfd);
1429 if (!local_got)
1430 continue;
1432 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1433 locsymcount = symtab_hdr->sh_info;
1434 end_local_got = local_got + locsymcount;
1435 s = htab->sgot;
1436 srel = htab->srelgot;
1437 for (; local_got < end_local_got; ++local_got)
1439 if (*local_got > 0)
1441 *local_got = s->_raw_size;
1442 s->_raw_size += GOT_ENTRY_SIZE;
1443 if (info->shared)
1444 srel->_raw_size += sizeof (Elf64_External_Rela);
1446 else
1447 *local_got = (bfd_vma) -1;
1451 /* Allocate global sym .plt and .got entries, and space for global
1452 sym dynamic relocs. */
1453 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1455 /* We now have determined the sizes of the various dynamic sections.
1456 Allocate memory for them. */
1457 relocs = false;
1458 for (s = dynobj->sections; s != NULL; s = s->next)
1460 if ((s->flags & SEC_LINKER_CREATED) == 0)
1461 continue;
1463 if (s == htab->splt
1464 || s == htab->sgot
1465 || s == htab->sgotplt)
1467 /* Strip this section if we don't need it; see the
1468 comment below. */
1470 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1472 if (s->_raw_size != 0 && s != htab->srelplt)
1473 relocs = true;
1475 /* We use the reloc_count field as a counter if we need
1476 to copy relocs into the output file. */
1477 s->reloc_count = 0;
1479 else
1481 /* It's not one of our sections, so don't allocate space. */
1482 continue;
1485 if (s->_raw_size == 0)
1487 /* If we don't need this section, strip it from the
1488 output file. This is mostly to handle .rela.bss and
1489 .rela.plt. We must create both sections in
1490 create_dynamic_sections, because they must be created
1491 before the linker maps input sections to output
1492 sections. The linker does that before
1493 adjust_dynamic_symbol is called, and it is that
1494 function which decides whether anything needs to go
1495 into these sections. */
1497 _bfd_strip_section_from_output (info, s);
1498 continue;
1501 /* Allocate memory for the section contents. We use bfd_zalloc
1502 here in case unused entries are not reclaimed before the
1503 section's contents are written out. This should not happen,
1504 but this way if it does, we get a R_X86_64_NONE reloc instead
1505 of garbage. */
1506 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1507 if (s->contents == NULL)
1508 return false;
1511 if (htab->elf.dynamic_sections_created)
1513 /* Add some entries to the .dynamic section. We fill in the
1514 values later, in elf64_x86_64_finish_dynamic_sections, but we
1515 must add the entries now so that we get the correct size for
1516 the .dynamic section. The DT_DEBUG entry is filled in by the
1517 dynamic linker and used by the debugger. */
1518 #define add_dynamic_entry(TAG, VAL) \
1519 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1521 if (! info->shared)
1523 if (!add_dynamic_entry (DT_DEBUG, 0))
1524 return false;
1527 if (htab->splt->_raw_size != 0)
1529 if (!add_dynamic_entry (DT_PLTGOT, 0)
1530 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1531 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1532 || !add_dynamic_entry (DT_JMPREL, 0))
1533 return false;
1536 if (relocs)
1538 if (!add_dynamic_entry (DT_RELA, 0)
1539 || !add_dynamic_entry (DT_RELASZ, 0)
1540 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1541 return false;
1543 /* If any dynamic relocs apply to a read-only section,
1544 then we need a DT_TEXTREL entry. */
1545 if ((info->flags & DF_TEXTREL) == 0)
1546 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1547 (PTR) info);
1549 if ((info->flags & DF_TEXTREL) != 0)
1551 if (!add_dynamic_entry (DT_TEXTREL, 0))
1552 return false;
1556 #undef add_dynamic_entry
1558 return true;
1561 /* Relocate an x86_64 ELF section. */
1563 static boolean
1564 elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
1565 contents, relocs, local_syms, local_sections)
1566 bfd *output_bfd;
1567 struct bfd_link_info *info;
1568 bfd *input_bfd;
1569 asection *input_section;
1570 bfd_byte *contents;
1571 Elf_Internal_Rela *relocs;
1572 Elf_Internal_Sym *local_syms;
1573 asection **local_sections;
1575 struct elf64_x86_64_link_hash_table *htab;
1576 Elf_Internal_Shdr *symtab_hdr;
1577 struct elf_link_hash_entry **sym_hashes;
1578 bfd_vma *local_got_offsets;
1579 Elf_Internal_Rela *rel;
1580 Elf_Internal_Rela *relend;
1582 if (info->relocateable)
1583 return true;
1585 htab = elf64_x86_64_hash_table (info);
1586 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1587 sym_hashes = elf_sym_hashes (input_bfd);
1588 local_got_offsets = elf_local_got_offsets (input_bfd);
1590 rel = relocs;
1591 relend = relocs + input_section->reloc_count;
1592 for (; rel < relend; rel++)
1594 int r_type;
1595 reloc_howto_type *howto;
1596 unsigned long r_symndx;
1597 struct elf_link_hash_entry *h;
1598 Elf_Internal_Sym *sym;
1599 asection *sec;
1600 bfd_vma off;
1601 bfd_vma relocation;
1602 boolean unresolved_reloc;
1603 bfd_reloc_status_type r;
1605 r_type = ELF64_R_TYPE (rel->r_info);
1606 if (r_type == (int) R_X86_64_GNU_VTINHERIT
1607 || r_type == (int) R_X86_64_GNU_VTENTRY)
1608 continue;
1610 if (r_type < 0 || r_type >= R_X86_64_max)
1612 bfd_set_error (bfd_error_bad_value);
1613 return false;
1616 howto = x86_64_elf_howto_table + r_type;
1617 r_symndx = ELF64_R_SYM (rel->r_info);
1618 h = NULL;
1619 sym = NULL;
1620 sec = NULL;
1621 unresolved_reloc = false;
1622 if (r_symndx < symtab_hdr->sh_info)
1624 sym = local_syms + r_symndx;
1625 sec = local_sections[r_symndx];
1627 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1629 else
1631 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1632 while (h->root.type == bfd_link_hash_indirect
1633 || h->root.type == bfd_link_hash_warning)
1634 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1636 if (h->root.type == bfd_link_hash_defined
1637 || h->root.type == bfd_link_hash_defweak)
1639 sec = h->root.u.def.section;
1640 if (sec->output_section == NULL)
1642 /* Set a flag that will be cleared later if we find a
1643 relocation value for this symbol. output_section
1644 is typically NULL for symbols satisfied by a shared
1645 library. */
1646 unresolved_reloc = true;
1647 relocation = 0;
1649 else
1650 relocation = (h->root.u.def.value
1651 + sec->output_section->vma
1652 + sec->output_offset);
1654 else if (h->root.type == bfd_link_hash_undefweak)
1655 relocation = 0;
1656 else if (info->shared
1657 && (!info->symbolic || info->allow_shlib_undefined)
1658 && !info->no_undefined
1659 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1660 relocation = 0;
1661 else
1663 if (! ((*info->callbacks->undefined_symbol)
1664 (info, h->root.root.string, input_bfd,
1665 input_section, rel->r_offset,
1666 (!info->shared || info->no_undefined
1667 || ELF_ST_VISIBILITY (h->other)))))
1668 return false;
1669 relocation = 0;
1672 /* When generating a shared object, the relocations handled here are
1673 copied into the output file to be resolved at run time. */
1674 switch (r_type)
1676 case R_X86_64_GOT32:
1677 /* Relocation is to the entry for this symbol in the global
1678 offset table. */
1679 case R_X86_64_GOTPCREL:
1680 /* Use global offset table as symbol value. */
1681 if (htab->sgot == NULL)
1682 abort ();
1684 if (h != NULL)
1686 boolean dyn;
1688 off = h->got.offset;
1689 dyn = htab->elf.dynamic_sections_created;
1691 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
1692 || (info->shared
1693 && (info->symbolic
1694 || h->dynindx == -1
1695 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
1696 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1698 /* This is actually a static link, or it is a -Bsymbolic
1699 link and the symbol is defined locally, or the symbol
1700 was forced to be local because of a version file. We
1701 must initialize this entry in the global offset table.
1702 Since the offset must always be a multiple of 8, we
1703 use the least significant bit to record whether we
1704 have initialized it already.
1706 When doing a dynamic link, we create a .rela.got
1707 relocation entry to initialize the value. This is
1708 done in the finish_dynamic_symbol routine. */
1709 if ((off & 1) != 0)
1710 off &= ~1;
1711 else
1713 bfd_put_64 (output_bfd, relocation,
1714 htab->sgot->contents + off);
1715 h->got.offset |= 1;
1718 else
1719 unresolved_reloc = false;
1721 else
1723 if (local_got_offsets == NULL)
1724 abort ();
1726 off = local_got_offsets[r_symndx];
1728 /* The offset must always be a multiple of 8. We use
1729 the least significant bit to record whether we have
1730 already generated the necessary reloc. */
1731 if ((off & 1) != 0)
1732 off &= ~1;
1733 else
1735 bfd_put_64 (output_bfd, relocation,
1736 htab->sgot->contents + off);
1738 if (info->shared)
1740 asection *srelgot;
1741 Elf_Internal_Rela outrel;
1742 Elf64_External_Rela *loc;
1744 /* We need to generate a R_X86_64_RELATIVE reloc
1745 for the dynamic linker. */
1746 srelgot = htab->srelgot;
1747 if (srelgot == NULL)
1748 abort ();
1750 outrel.r_offset = (htab->sgot->output_section->vma
1751 + htab->sgot->output_offset
1752 + off);
1753 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1754 outrel.r_addend = relocation;
1755 loc = (Elf64_External_Rela *) srelgot->contents;
1756 loc += srelgot->reloc_count++;
1757 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1760 local_got_offsets[r_symndx] |= 1;
1764 if (off >= (bfd_vma) -2)
1765 abort ();
1767 relocation = htab->sgot->output_offset + off;
1768 if (r_type == R_X86_64_GOTPCREL)
1769 relocation += htab->sgot->output_section->vma;
1771 break;
1773 case R_X86_64_PLT32:
1774 /* Relocation is to the entry for this symbol in the
1775 procedure linkage table. */
1777 /* Resolve a PLT32 reloc against a local symbol directly,
1778 without using the procedure linkage table. */
1779 if (h == NULL)
1780 break;
1782 if (h->plt.offset == (bfd_vma) -1
1783 || htab->splt == NULL)
1785 /* We didn't make a PLT entry for this symbol. This
1786 happens when statically linking PIC code, or when
1787 using -Bsymbolic. */
1788 break;
1791 relocation = (htab->splt->output_section->vma
1792 + htab->splt->output_offset
1793 + h->plt.offset);
1794 unresolved_reloc = false;
1795 break;
1797 case R_X86_64_PC8:
1798 case R_X86_64_PC16:
1799 case R_X86_64_PC32:
1800 case R_X86_64_8:
1801 case R_X86_64_16:
1802 case R_X86_64_32:
1803 case R_X86_64_64:
1804 /* FIXME: The ABI says the linker should make sure the value is
1805 the same when it's zeroextended to 64 bit. */
1807 /* r_symndx will be zero only for relocs against symbols
1808 from removed linkonce sections, or sections discarded by
1809 a linker script. */
1810 if (r_symndx == 0
1811 || (input_section->flags & SEC_ALLOC) == 0)
1812 break;
1814 if ((info->shared
1815 && ((r_type != R_X86_64_PC8
1816 && r_type != R_X86_64_PC16
1817 && r_type != R_X86_64_PC32)
1818 || (h != NULL
1819 && h->dynindx != -1
1820 && (! info->symbolic
1821 || (h->elf_link_hash_flags
1822 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1823 || (!info->shared
1824 && h != NULL
1825 && h->dynindx != -1
1826 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1827 && (((h->elf_link_hash_flags
1828 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1829 && (h->elf_link_hash_flags
1830 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1831 || h->root.type == bfd_link_hash_undefweak
1832 || h->root.type == bfd_link_hash_undefined)))
1834 Elf_Internal_Rela outrel;
1835 boolean skip, relocate;
1836 asection *sreloc;
1837 Elf64_External_Rela *loc;
1839 /* When generating a shared object, these relocations
1840 are copied into the output file to be resolved at run
1841 time. */
1843 skip = false;
1844 relocate = false;
1846 outrel.r_offset =
1847 _bfd_elf_section_offset (output_bfd, info, input_section,
1848 rel->r_offset);
1849 if (outrel.r_offset == (bfd_vma) -1)
1850 skip = true;
1851 else if (outrel.r_offset == (bfd_vma) -2)
1852 skip = true, relocate = true;
1854 outrel.r_offset += (input_section->output_section->vma
1855 + input_section->output_offset);
1857 if (skip)
1858 memset (&outrel, 0, sizeof outrel);
1860 /* h->dynindx may be -1 if this symbol was marked to
1861 become local. */
1862 else if (h != NULL
1863 && h->dynindx != -1
1864 && (r_type == R_X86_64_PC8
1865 || r_type == R_X86_64_PC16
1866 || r_type == R_X86_64_PC32
1867 || !info->shared
1868 || !info->symbolic
1869 || (h->elf_link_hash_flags
1870 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1872 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
1873 outrel.r_addend = rel->r_addend;
1875 else
1877 /* This symbol is local, or marked to become local. */
1878 if (r_type == R_X86_64_64)
1880 relocate = true;
1881 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1882 outrel.r_addend = relocation + rel->r_addend;
1884 else
1886 long sindx;
1888 if (h == NULL)
1889 sec = local_sections[r_symndx];
1890 else
1892 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1893 || (h->root.type
1894 == bfd_link_hash_defweak));
1895 sec = h->root.u.def.section;
1897 if (sec != NULL && bfd_is_abs_section (sec))
1898 sindx = 0;
1899 else if (sec == NULL || sec->owner == NULL)
1901 bfd_set_error (bfd_error_bad_value);
1902 return false;
1904 else
1906 asection *osec;
1908 osec = sec->output_section;
1909 sindx = elf_section_data (osec)->dynindx;
1910 BFD_ASSERT (sindx > 0);
1913 outrel.r_info = ELF64_R_INFO (sindx, r_type);
1914 outrel.r_addend = relocation + rel->r_addend;
1918 sreloc = elf_section_data (input_section)->sreloc;
1919 if (sreloc == NULL)
1920 abort ();
1922 loc = (Elf64_External_Rela *) sreloc->contents;
1923 loc += sreloc->reloc_count++;
1924 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1926 /* If this reloc is against an external symbol, we do
1927 not want to fiddle with the addend. Otherwise, we
1928 need to include the symbol value so that it becomes
1929 an addend for the dynamic reloc. */
1930 if (! relocate)
1931 continue;
1934 break;
1936 default:
1937 break;
1940 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1941 because such sections are not SEC_ALLOC and thus ld.so will
1942 not process them. */
1943 if (unresolved_reloc
1944 && !((input_section->flags & SEC_DEBUGGING) != 0
1945 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
1946 (*_bfd_error_handler)
1947 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
1948 bfd_archive_filename (input_bfd),
1949 bfd_get_section_name (input_bfd, input_section),
1950 (long) rel->r_offset,
1951 h->root.root.string);
1953 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1954 contents, rel->r_offset,
1955 relocation, rel->r_addend);
1957 if (r != bfd_reloc_ok)
1959 const char *name;
1961 if (h != NULL)
1962 name = h->root.root.string;
1963 else
1965 name = bfd_elf_string_from_elf_section (input_bfd,
1966 symtab_hdr->sh_link,
1967 sym->st_name);
1968 if (name == NULL)
1969 return false;
1970 if (*name == '\0')
1971 name = bfd_section_name (input_bfd, sec);
1974 if (r == bfd_reloc_overflow)
1977 if (! ((*info->callbacks->reloc_overflow)
1978 (info, name, howto->name, (bfd_vma) 0,
1979 input_bfd, input_section, rel->r_offset)))
1980 return false;
1982 else
1984 (*_bfd_error_handler)
1985 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
1986 bfd_archive_filename (input_bfd),
1987 bfd_get_section_name (input_bfd, input_section),
1988 (long) rel->r_offset, name, (int) r);
1989 return false;
1994 return true;
1997 /* Finish up dynamic symbol handling. We set the contents of various
1998 dynamic sections here. */
2000 static boolean
2001 elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym)
2002 bfd *output_bfd;
2003 struct bfd_link_info *info;
2004 struct elf_link_hash_entry *h;
2005 Elf_Internal_Sym *sym;
2007 struct elf64_x86_64_link_hash_table *htab;
2009 htab = elf64_x86_64_hash_table (info);
2011 if (h->plt.offset != (bfd_vma) -1)
2013 bfd_vma plt_index;
2014 bfd_vma got_offset;
2015 Elf_Internal_Rela rela;
2016 Elf64_External_Rela *loc;
2018 /* This symbol has an entry in the procedure linkage table. Set
2019 it up. */
2021 if (h->dynindx == -1
2022 || htab->splt == NULL
2023 || htab->sgotplt == NULL
2024 || htab->srelplt == NULL)
2025 abort ();
2027 /* Get the index in the procedure linkage table which
2028 corresponds to this symbol. This is the index of this symbol
2029 in all the symbols for which we are making plt entries. The
2030 first entry in the procedure linkage table is reserved. */
2031 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2033 /* Get the offset into the .got table of the entry that
2034 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
2035 bytes. The first three are reserved for the dynamic linker. */
2036 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2038 /* Fill in the entry in the procedure linkage table. */
2039 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
2040 PLT_ENTRY_SIZE);
2042 /* Insert the relocation positions of the plt section. The magic
2043 numbers at the end of the statements are the positions of the
2044 relocations in the plt section. */
2045 /* Put offset for jmp *name@GOTPCREL(%rip), since the
2046 instruction uses 6 bytes, subtract this value. */
2047 bfd_put_32 (output_bfd,
2048 (htab->sgotplt->output_section->vma
2049 + htab->sgotplt->output_offset
2050 + got_offset
2051 - htab->splt->output_section->vma
2052 - htab->splt->output_offset
2053 - h->plt.offset
2054 - 6),
2055 htab->splt->contents + h->plt.offset + 2);
2056 /* Put relocation index. */
2057 bfd_put_32 (output_bfd, plt_index,
2058 htab->splt->contents + h->plt.offset + 7);
2059 /* Put offset for jmp .PLT0. */
2060 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
2061 htab->splt->contents + h->plt.offset + 12);
2063 /* Fill in the entry in the global offset table, initially this
2064 points to the pushq instruction in the PLT which is at offset 6. */
2065 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
2066 + htab->splt->output_offset
2067 + h->plt.offset + 6),
2068 htab->sgotplt->contents + got_offset);
2070 /* Fill in the entry in the .rela.plt section. */
2071 rela.r_offset = (htab->sgotplt->output_section->vma
2072 + htab->sgotplt->output_offset
2073 + got_offset);
2074 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
2075 rela.r_addend = 0;
2076 loc = (Elf64_External_Rela *) htab->srelplt->contents + plt_index;
2077 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2079 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2081 /* Mark the symbol as undefined, rather than as defined in
2082 the .plt section. Leave the value alone. This is a clue
2083 for the dynamic linker, to make function pointer
2084 comparisons work between an application and shared
2085 library. */
2086 sym->st_shndx = SHN_UNDEF;
2090 if (h->got.offset != (bfd_vma) -1)
2092 Elf_Internal_Rela rela;
2093 Elf64_External_Rela *loc;
2095 /* This symbol has an entry in the global offset table. Set it
2096 up. */
2098 if (htab->sgot == NULL || htab->srelgot == NULL)
2099 abort ();
2101 rela.r_offset = (htab->sgot->output_section->vma
2102 + htab->sgot->output_offset
2103 + (h->got.offset &~ (bfd_vma) 1));
2105 /* If this is a static link, or it is a -Bsymbolic link and the
2106 symbol is defined locally or was forced to be local because
2107 of a version file, we just want to emit a RELATIVE reloc.
2108 The entry in the global offset table will already have been
2109 initialized in the relocate_section function. */
2110 if (info->shared
2111 && (info->symbolic
2112 || h->dynindx == -1
2113 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2114 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2116 BFD_ASSERT((h->got.offset & 1) != 0);
2117 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2118 rela.r_addend = (h->root.u.def.value
2119 + h->root.u.def.section->output_section->vma
2120 + h->root.u.def.section->output_offset);
2122 else
2124 BFD_ASSERT((h->got.offset & 1) == 0);
2125 bfd_put_64 (output_bfd, (bfd_vma) 0,
2126 htab->sgot->contents + h->got.offset);
2127 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
2128 rela.r_addend = 0;
2131 loc = (Elf64_External_Rela *) htab->srelgot->contents;
2132 loc += htab->srelgot->reloc_count++;
2133 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2136 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2138 Elf_Internal_Rela rela;
2139 Elf64_External_Rela *loc;
2141 /* This symbol needs a copy reloc. Set it up. */
2143 if (h->dynindx == -1
2144 || (h->root.type != bfd_link_hash_defined
2145 && h->root.type != bfd_link_hash_defweak)
2146 || htab->srelbss == NULL)
2147 abort ();
2149 rela.r_offset = (h->root.u.def.value
2150 + h->root.u.def.section->output_section->vma
2151 + h->root.u.def.section->output_offset);
2152 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
2153 rela.r_addend = 0;
2154 loc = (Elf64_External_Rela *) htab->srelbss->contents;
2155 loc += htab->srelbss->reloc_count++;
2156 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2159 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2160 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2161 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2162 sym->st_shndx = SHN_ABS;
2164 return true;
2167 /* Used to decide how to sort relocs in an optimal manner for the
2168 dynamic linker, before writing them out. */
2170 static enum elf_reloc_type_class
2171 elf64_x86_64_reloc_type_class (rela)
2172 const Elf_Internal_Rela *rela;
2174 switch ((int) ELF64_R_TYPE (rela->r_info))
2176 case R_X86_64_RELATIVE:
2177 return reloc_class_relative;
2178 case R_X86_64_JUMP_SLOT:
2179 return reloc_class_plt;
2180 case R_X86_64_COPY:
2181 return reloc_class_copy;
2182 default:
2183 return reloc_class_normal;
2187 /* Finish up the dynamic sections. */
2189 static boolean
2190 elf64_x86_64_finish_dynamic_sections (output_bfd, info)
2191 bfd *output_bfd;
2192 struct bfd_link_info *info;
2194 struct elf64_x86_64_link_hash_table *htab;
2195 bfd *dynobj;
2196 asection *sdyn;
2198 htab = elf64_x86_64_hash_table (info);
2199 dynobj = htab->elf.dynobj;
2200 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2202 if (htab->elf.dynamic_sections_created)
2204 Elf64_External_Dyn *dyncon, *dynconend;
2206 if (sdyn == NULL || htab->sgot == NULL)
2207 abort ();
2209 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2210 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2211 for (; dyncon < dynconend; dyncon++)
2213 Elf_Internal_Dyn dyn;
2214 asection *s;
2216 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2218 switch (dyn.d_tag)
2220 default:
2221 continue;
2223 case DT_PLTGOT:
2224 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
2225 break;
2227 case DT_JMPREL:
2228 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2229 break;
2231 case DT_PLTRELSZ:
2232 s = htab->srelplt->output_section;
2233 if (s->_cooked_size != 0)
2234 dyn.d_un.d_val = s->_cooked_size;
2235 else
2236 dyn.d_un.d_val = s->_raw_size;
2237 break;
2239 case DT_RELASZ:
2240 /* The procedure linkage table relocs (DT_JMPREL) should
2241 not be included in the overall relocs (DT_RELA).
2242 Therefore, we override the DT_RELASZ entry here to
2243 make it not include the JMPREL relocs. Since the
2244 linker script arranges for .rela.plt to follow all
2245 other relocation sections, we don't have to worry
2246 about changing the DT_RELA entry. */
2247 if (htab->srelplt != NULL)
2249 s = htab->srelplt->output_section;
2250 if (s->_cooked_size != 0)
2251 dyn.d_un.d_val -= s->_cooked_size;
2252 else
2253 dyn.d_un.d_val -= s->_raw_size;
2255 break;
2258 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2261 /* Fill in the special first entry in the procedure linkage table. */
2262 if (htab->splt && htab->splt->_raw_size > 0)
2264 /* Fill in the first entry in the procedure linkage table. */
2265 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
2266 PLT_ENTRY_SIZE);
2267 /* Add offset for pushq GOT+8(%rip), since the instruction
2268 uses 6 bytes subtract this value. */
2269 bfd_put_32 (output_bfd,
2270 (htab->sgotplt->output_section->vma
2271 + htab->sgotplt->output_offset
2273 - htab->splt->output_section->vma
2274 - htab->splt->output_offset
2275 - 6),
2276 htab->splt->contents + 2);
2277 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
2278 the end of the instruction. */
2279 bfd_put_32 (output_bfd,
2280 (htab->sgotplt->output_section->vma
2281 + htab->sgotplt->output_offset
2282 + 16
2283 - htab->splt->output_section->vma
2284 - htab->splt->output_offset
2285 - 12),
2286 htab->splt->contents + 8);
2288 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
2289 PLT_ENTRY_SIZE;
2293 if (htab->sgotplt)
2295 /* Fill in the first three entries in the global offset table. */
2296 if (htab->sgotplt->_raw_size > 0)
2298 /* Set the first entry in the global offset table to the address of
2299 the dynamic section. */
2300 if (sdyn == NULL)
2301 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
2302 else
2303 bfd_put_64 (output_bfd,
2304 sdyn->output_section->vma + sdyn->output_offset,
2305 htab->sgotplt->contents);
2306 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
2307 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
2308 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
2311 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
2312 GOT_ENTRY_SIZE;
2315 return true;
2319 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
2320 #define TARGET_LITTLE_NAME "elf64-x86-64"
2321 #define ELF_ARCH bfd_arch_i386
2322 #define ELF_MACHINE_CODE EM_X86_64
2323 #define ELF_MAXPAGESIZE 0x100000
2325 #define elf_backend_can_gc_sections 1
2326 #define elf_backend_can_refcount 1
2327 #define elf_backend_want_got_plt 1
2328 #define elf_backend_plt_readonly 1
2329 #define elf_backend_want_plt_sym 0
2330 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
2331 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
2332 #define elf_backend_rela_normal 1
2334 #define elf_info_to_howto elf64_x86_64_info_to_howto
2336 #define bfd_elf64_bfd_link_hash_table_create \
2337 elf64_x86_64_link_hash_table_create
2338 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
2340 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
2341 #define elf_backend_check_relocs elf64_x86_64_check_relocs
2342 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
2343 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
2344 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
2345 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
2346 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
2347 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
2348 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
2349 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
2350 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
2351 #define elf_backend_relocate_section elf64_x86_64_relocate_section
2352 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
2353 #define elf_backend_object_p elf64_x86_64_elf_object_p
2355 #include "elf64-target.h"