1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004 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. */
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,
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,
43 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
44 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
46 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
47 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
49 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
50 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
52 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
53 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
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,
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,
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,
64 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
65 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
67 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
68 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
70 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
71 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
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),
81 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
82 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
84 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
85 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
87 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
90 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
91 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
93 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
94 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
96 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
97 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
99 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
100 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
102 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
103 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
106 /* GNU extension to record C++ vtable hierarchy. */
107 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
108 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
110 /* GNU extension to record C++ vtable member usage. */
111 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
112 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
116 /* Map BFD relocs to the x86_64 elf relocs. */
119 bfd_reloc_code_real_type bfd_reloc_val;
120 unsigned char elf_reloc_val;
123 static const struct elf_reloc_map x86_64_reloc_map[] =
125 { BFD_RELOC_NONE, R_X86_64_NONE, },
126 { BFD_RELOC_64, R_X86_64_64, },
127 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
128 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
129 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
130 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
131 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
132 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
133 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
134 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
135 { BFD_RELOC_32, R_X86_64_32, },
136 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
137 { BFD_RELOC_16, R_X86_64_16, },
138 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
139 { BFD_RELOC_8, R_X86_64_8, },
140 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
141 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
142 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
143 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
144 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
145 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
146 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
147 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
148 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
149 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
150 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
154 /* Given a BFD reloc type, return a HOWTO structure. */
155 static reloc_howto_type *
156 elf64_x86_64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
157 bfd_reloc_code_real_type code)
161 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
164 if (x86_64_reloc_map[i].bfd_reloc_val == code)
165 return &x86_64_elf_howto_table[i];
170 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
173 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
174 Elf_Internal_Rela *dst)
178 r_type = ELF64_R_TYPE (dst->r_info);
179 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
181 BFD_ASSERT (r_type <= (unsigned int) R_X86_64_TPOFF32);
186 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
187 i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_TPOFF32 - 1);
189 cache_ptr->howto = &x86_64_elf_howto_table[i];
190 BFD_ASSERT (r_type == cache_ptr->howto->type);
193 /* Support for core dump NOTE sections. */
195 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
200 switch (note->descsz)
205 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
207 elf_tdata (abfd)->core_signal
208 = bfd_get_16 (abfd, note->descdata + 12);
211 elf_tdata (abfd)->core_pid
212 = bfd_get_32 (abfd, note->descdata + 32);
221 /* Make a ".reg/999" section. */
222 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
223 raw_size, note->descpos + offset);
227 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
229 switch (note->descsz)
234 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
235 elf_tdata (abfd)->core_program
236 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
237 elf_tdata (abfd)->core_command
238 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
241 /* Note that for some reason, a spurious space is tacked
242 onto the end of the args in some (at least one anyway)
243 implementations, so strip it off if it exists. */
246 char *command = elf_tdata (abfd)->core_command;
247 int n = strlen (command);
249 if (0 < n && command[n - 1] == ' ')
250 command[n - 1] = '\0';
256 /* Functions for the x86-64 ELF linker. */
258 /* The name of the dynamic interpreter. This is put in the .interp
261 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
263 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
264 copying dynamic variables from a shared lib into an app's dynbss
265 section, and instead use a dynamic relocation to point into the
267 #define ELIMINATE_COPY_RELOCS 1
269 /* The size in bytes of an entry in the global offset table. */
271 #define GOT_ENTRY_SIZE 8
273 /* The size in bytes of an entry in the procedure linkage table. */
275 #define PLT_ENTRY_SIZE 16
277 /* The first entry in a procedure linkage table looks like this. See the
278 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
280 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
282 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
283 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
284 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
287 /* Subsequent entries in a procedure linkage table look like this. */
289 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
291 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
292 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
293 0x68, /* pushq immediate */
294 0, 0, 0, 0, /* replaced with index into relocation table. */
295 0xe9, /* jmp relative */
296 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
299 /* The x86-64 linker needs to keep track of the number of relocs that
300 it decides to copy as dynamic relocs in check_relocs for each symbol.
301 This is so that it can later discard them if they are found to be
302 unnecessary. We store the information in a field extending the
303 regular ELF linker hash table. */
305 struct elf64_x86_64_dyn_relocs
308 struct elf64_x86_64_dyn_relocs *next;
310 /* The input section of the reloc. */
313 /* Total number of relocs copied for the input section. */
316 /* Number of pc-relative relocs copied for the input section. */
317 bfd_size_type pc_count;
320 /* x86-64 ELF linker hash entry. */
322 struct elf64_x86_64_link_hash_entry
324 struct elf_link_hash_entry elf;
326 /* Track dynamic relocs copied for this symbol. */
327 struct elf64_x86_64_dyn_relocs *dyn_relocs;
329 #define GOT_UNKNOWN 0
333 unsigned char tls_type;
336 #define elf64_x86_64_hash_entry(ent) \
337 ((struct elf64_x86_64_link_hash_entry *)(ent))
339 struct elf64_x86_64_obj_tdata
341 struct elf_obj_tdata root;
343 /* tls_type for each local got entry. */
344 char *local_got_tls_type;
347 #define elf64_x86_64_tdata(abfd) \
348 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
350 #define elf64_x86_64_local_got_tls_type(abfd) \
351 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
354 /* x86-64 ELF linker hash table. */
356 struct elf64_x86_64_link_hash_table
358 struct elf_link_hash_table elf;
360 /* Short-cuts to get to dynamic linker sections. */
370 bfd_signed_vma refcount;
374 /* Small local sym to section mapping cache. */
375 struct sym_sec_cache sym_sec;
378 /* Get the x86-64 ELF linker hash table from a link_info structure. */
380 #define elf64_x86_64_hash_table(p) \
381 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
383 /* Create an entry in an x86-64 ELF linker hash table. */
385 static struct bfd_hash_entry *
386 link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
389 /* Allocate the structure if it has not already been allocated by a
393 entry = bfd_hash_allocate (table,
394 sizeof (struct elf64_x86_64_link_hash_entry));
399 /* Call the allocation method of the superclass. */
400 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
403 struct elf64_x86_64_link_hash_entry *eh;
405 eh = (struct elf64_x86_64_link_hash_entry *) entry;
406 eh->dyn_relocs = NULL;
407 eh->tls_type = GOT_UNKNOWN;
413 /* Create an X86-64 ELF linker hash table. */
415 static struct bfd_link_hash_table *
416 elf64_x86_64_link_hash_table_create (bfd *abfd)
418 struct elf64_x86_64_link_hash_table *ret;
419 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
421 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
425 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
438 ret->sym_sec.abfd = NULL;
439 ret->tls_ld_got.refcount = 0;
441 return &ret->elf.root;
444 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
445 shortcuts to them in our hash table. */
448 create_got_section (bfd *dynobj, struct bfd_link_info *info)
450 struct elf64_x86_64_link_hash_table *htab;
452 if (! _bfd_elf_create_got_section (dynobj, info))
455 htab = elf64_x86_64_hash_table (info);
456 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
457 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
458 if (!htab->sgot || !htab->sgotplt)
461 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
462 if (htab->srelgot == NULL
463 || ! bfd_set_section_flags (dynobj, htab->srelgot,
464 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
465 | SEC_IN_MEMORY | SEC_LINKER_CREATED
467 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
472 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
473 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
477 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
479 struct elf64_x86_64_link_hash_table *htab;
481 htab = elf64_x86_64_hash_table (info);
482 if (!htab->sgot && !create_got_section (dynobj, info))
485 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
488 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
489 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
490 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
492 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
494 if (!htab->splt || !htab->srelplt || !htab->sdynbss
495 || (!info->shared && !htab->srelbss))
501 /* Copy the extra info we tack onto an elf_link_hash_entry. */
504 elf64_x86_64_copy_indirect_symbol (const struct elf_backend_data *bed,
505 struct elf_link_hash_entry *dir,
506 struct elf_link_hash_entry *ind)
508 struct elf64_x86_64_link_hash_entry *edir, *eind;
510 edir = (struct elf64_x86_64_link_hash_entry *) dir;
511 eind = (struct elf64_x86_64_link_hash_entry *) ind;
513 if (eind->dyn_relocs != NULL)
515 if (edir->dyn_relocs != NULL)
517 struct elf64_x86_64_dyn_relocs **pp;
518 struct elf64_x86_64_dyn_relocs *p;
520 if (ind->root.type == bfd_link_hash_indirect)
523 /* Add reloc counts against the weak sym to the strong sym
524 list. Merge any entries against the same section. */
525 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
527 struct elf64_x86_64_dyn_relocs *q;
529 for (q = edir->dyn_relocs; q != NULL; q = q->next)
530 if (q->sec == p->sec)
532 q->pc_count += p->pc_count;
533 q->count += p->count;
540 *pp = edir->dyn_relocs;
543 edir->dyn_relocs = eind->dyn_relocs;
544 eind->dyn_relocs = NULL;
547 if (ind->root.type == bfd_link_hash_indirect
548 && dir->got.refcount <= 0)
550 edir->tls_type = eind->tls_type;
551 eind->tls_type = GOT_UNKNOWN;
554 if (ELIMINATE_COPY_RELOCS
555 && ind->root.type != bfd_link_hash_indirect
556 && (dir->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
557 /* If called to transfer flags for a weakdef during processing
558 of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
559 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
560 dir->elf_link_hash_flags |=
561 (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
562 | ELF_LINK_HASH_REF_REGULAR
563 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
564 | ELF_LINK_HASH_NEEDS_PLT));
566 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
570 elf64_x86_64_mkobject (bfd *abfd)
572 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
573 abfd->tdata.any = bfd_zalloc (abfd, amt);
574 if (abfd->tdata.any == NULL)
580 elf64_x86_64_elf_object_p (bfd *abfd)
582 /* Set the right machine number for an x86-64 elf64 file. */
583 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
588 elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
596 case R_X86_64_GOTTPOFF:
598 return R_X86_64_TPOFF32;
599 return R_X86_64_GOTTPOFF;
601 return R_X86_64_TPOFF32;
607 /* Look through the relocs for a section during the first phase, and
608 calculate needed space in the global offset table, procedure
609 linkage table, and dynamic reloc sections. */
612 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
613 const Elf_Internal_Rela *relocs)
615 struct elf64_x86_64_link_hash_table *htab;
616 Elf_Internal_Shdr *symtab_hdr;
617 struct elf_link_hash_entry **sym_hashes;
618 const Elf_Internal_Rela *rel;
619 const Elf_Internal_Rela *rel_end;
622 if (info->relocatable)
625 htab = elf64_x86_64_hash_table (info);
626 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
627 sym_hashes = elf_sym_hashes (abfd);
631 rel_end = relocs + sec->reloc_count;
632 for (rel = relocs; rel < rel_end; rel++)
635 unsigned long r_symndx;
636 struct elf_link_hash_entry *h;
638 r_symndx = ELF64_R_SYM (rel->r_info);
639 r_type = ELF64_R_TYPE (rel->r_info);
641 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
643 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
644 bfd_archive_filename (abfd),
649 if (r_symndx < symtab_hdr->sh_info)
652 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
654 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
658 htab->tls_ld_got.refcount += 1;
661 case R_X86_64_TPOFF32:
664 (*_bfd_error_handler)
665 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
666 bfd_archive_filename (abfd),
667 x86_64_elf_howto_table[r_type].name);
668 bfd_set_error (bfd_error_bad_value);
673 case R_X86_64_GOTTPOFF:
675 info->flags |= DF_STATIC_TLS;
679 case R_X86_64_GOTPCREL:
681 /* This symbol requires a global offset table entry. */
683 int tls_type, old_tls_type;
687 default: tls_type = GOT_NORMAL; break;
688 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
689 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
694 h->got.refcount += 1;
695 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
699 bfd_signed_vma *local_got_refcounts;
701 /* This is a global offset table entry for a local symbol. */
702 local_got_refcounts = elf_local_got_refcounts (abfd);
703 if (local_got_refcounts == NULL)
707 size = symtab_hdr->sh_info;
708 size *= sizeof (bfd_signed_vma) + sizeof (char);
709 local_got_refcounts = ((bfd_signed_vma *)
710 bfd_zalloc (abfd, size));
711 if (local_got_refcounts == NULL)
713 elf_local_got_refcounts (abfd) = local_got_refcounts;
714 elf64_x86_64_local_got_tls_type (abfd)
715 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
717 local_got_refcounts[r_symndx] += 1;
719 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
722 /* If a TLS symbol is accessed using IE at least once,
723 there is no point to use dynamic model for it. */
724 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
725 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
727 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
728 tls_type = old_tls_type;
731 (*_bfd_error_handler)
732 (_("%s: %s' accessed both as normal and thread local symbol"),
733 bfd_archive_filename (abfd),
734 h ? h->root.root.string : "<local>");
739 if (old_tls_type != tls_type)
742 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
744 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
749 //case R_X86_64_GOTPCREL:
751 if (htab->sgot == NULL)
753 if (htab->elf.dynobj == NULL)
754 htab->elf.dynobj = abfd;
755 if (!create_got_section (htab->elf.dynobj, info))
761 /* This symbol requires a procedure linkage table entry. We
762 actually build the entry in adjust_dynamic_symbol,
763 because this might be a case of linking PIC code which is
764 never referenced by a dynamic object, in which case we
765 don't need to generate a procedure linkage table entry
768 /* If this is a local symbol, we resolve it directly without
769 creating a procedure linkage table entry. */
773 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
774 h->plt.refcount += 1;
781 /* Let's help debug shared library creation. These relocs
782 cannot be used in shared libs. Don't error out for
783 sections we don't care about, such as debug sections or
784 non-constant sections. */
786 && (sec->flags & SEC_ALLOC) != 0
787 && (sec->flags & SEC_READONLY) != 0)
789 (*_bfd_error_handler)
790 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
791 bfd_archive_filename (abfd),
792 x86_64_elf_howto_table[r_type].name);
793 bfd_set_error (bfd_error_bad_value);
802 if (h != NULL && !info->shared)
804 /* If this reloc is in a read-only section, we might
805 need a copy reloc. We can't check reliably at this
806 stage whether the section is read-only, as input
807 sections have not yet been mapped to output sections.
808 Tentatively set the flag for now, and correct in
809 adjust_dynamic_symbol. */
810 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
812 /* We may need a .plt entry if the function this reloc
813 refers to is in a shared lib. */
814 h->plt.refcount += 1;
817 /* If we are creating a shared library, and this is a reloc
818 against a global symbol, or a non PC relative reloc
819 against a local symbol, then we need to copy the reloc
820 into the shared library. However, if we are linking with
821 -Bsymbolic, we do not need to copy a reloc against a
822 global symbol which is defined in an object we are
823 including in the link (i.e., DEF_REGULAR is set). At
824 this point we have not seen all the input files, so it is
825 possible that DEF_REGULAR is not set now but will be set
826 later (it is never cleared). In case of a weak definition,
827 DEF_REGULAR may be cleared later by a strong definition in
828 a shared library. We account for that possibility below by
829 storing information in the relocs_copied field of the hash
830 table entry. A similar situation occurs when creating
831 shared libraries and symbol visibility changes render the
834 If on the other hand, we are creating an executable, we
835 may need to keep relocations for symbols satisfied by a
836 dynamic library if we manage to avoid copy relocs for the
839 && (sec->flags & SEC_ALLOC) != 0
840 && (((r_type != R_X86_64_PC8)
841 && (r_type != R_X86_64_PC16)
842 && (r_type != R_X86_64_PC32))
845 || h->root.type == bfd_link_hash_defweak
846 || (h->elf_link_hash_flags
847 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
848 || (ELIMINATE_COPY_RELOCS
850 && (sec->flags & SEC_ALLOC) != 0
852 && (h->root.type == bfd_link_hash_defweak
853 || (h->elf_link_hash_flags
854 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
856 struct elf64_x86_64_dyn_relocs *p;
857 struct elf64_x86_64_dyn_relocs **head;
859 /* We must copy these reloc types into the output file.
860 Create a reloc section in dynobj and make room for
867 name = (bfd_elf_string_from_elf_section
869 elf_elfheader (abfd)->e_shstrndx,
870 elf_section_data (sec)->rel_hdr.sh_name));
874 if (strncmp (name, ".rela", 5) != 0
875 || strcmp (bfd_get_section_name (abfd, sec),
878 (*_bfd_error_handler)
879 (_("%s: bad relocation section name `%s\'"),
880 bfd_archive_filename (abfd), name);
883 if (htab->elf.dynobj == NULL)
884 htab->elf.dynobj = abfd;
886 dynobj = htab->elf.dynobj;
888 sreloc = bfd_get_section_by_name (dynobj, name);
893 sreloc = bfd_make_section (dynobj, name);
894 flags = (SEC_HAS_CONTENTS | SEC_READONLY
895 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
896 if ((sec->flags & SEC_ALLOC) != 0)
897 flags |= SEC_ALLOC | SEC_LOAD;
899 || ! bfd_set_section_flags (dynobj, sreloc, flags)
900 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
903 elf_section_data (sec)->sreloc = sreloc;
906 /* If this is a global symbol, we count the number of
907 relocations we need for this symbol. */
910 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
914 /* Track dynamic relocs needed for local syms too.
915 We really need local syms available to do this
919 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
924 head = ((struct elf64_x86_64_dyn_relocs **)
925 &elf_section_data (s)->local_dynrel);
929 if (p == NULL || p->sec != sec)
931 bfd_size_type amt = sizeof *p;
932 p = ((struct elf64_x86_64_dyn_relocs *)
933 bfd_alloc (htab->elf.dynobj, amt));
944 if (r_type == R_X86_64_PC8
945 || r_type == R_X86_64_PC16
946 || r_type == R_X86_64_PC32)
951 /* This relocation describes the C++ object vtable hierarchy.
952 Reconstruct it for later use during GC. */
953 case R_X86_64_GNU_VTINHERIT:
954 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
958 /* This relocation describes which C++ vtable entries are actually
959 used. Record for later use during GC. */
960 case R_X86_64_GNU_VTENTRY:
961 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
973 /* Return the section that should be marked against GC for a given
977 elf64_x86_64_gc_mark_hook (asection *sec,
978 struct bfd_link_info *info ATTRIBUTE_UNUSED,
979 Elf_Internal_Rela *rel,
980 struct elf_link_hash_entry *h,
981 Elf_Internal_Sym *sym)
985 switch (ELF64_R_TYPE (rel->r_info))
987 case R_X86_64_GNU_VTINHERIT:
988 case R_X86_64_GNU_VTENTRY:
992 switch (h->root.type)
994 case bfd_link_hash_defined:
995 case bfd_link_hash_defweak:
996 return h->root.u.def.section;
998 case bfd_link_hash_common:
999 return h->root.u.c.p->section;
1007 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1012 /* Update the got entry reference counts for the section being removed. */
1015 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1016 asection *sec, const Elf_Internal_Rela *relocs)
1018 Elf_Internal_Shdr *symtab_hdr;
1019 struct elf_link_hash_entry **sym_hashes;
1020 bfd_signed_vma *local_got_refcounts;
1021 const Elf_Internal_Rela *rel, *relend;
1023 elf_section_data (sec)->local_dynrel = NULL;
1025 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1026 sym_hashes = elf_sym_hashes (abfd);
1027 local_got_refcounts = elf_local_got_refcounts (abfd);
1029 relend = relocs + sec->reloc_count;
1030 for (rel = relocs; rel < relend; rel++)
1032 unsigned long r_symndx;
1033 unsigned int r_type;
1034 struct elf_link_hash_entry *h = NULL;
1036 r_symndx = ELF64_R_SYM (rel->r_info);
1037 if (r_symndx >= symtab_hdr->sh_info)
1039 struct elf64_x86_64_link_hash_entry *eh;
1040 struct elf64_x86_64_dyn_relocs **pp;
1041 struct elf64_x86_64_dyn_relocs *p;
1043 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1044 eh = (struct elf64_x86_64_link_hash_entry *) h;
1046 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1049 /* Everything must go for SEC. */
1055 r_type = ELF64_R_TYPE (rel->r_info);
1056 r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
1059 case R_X86_64_TLSLD:
1060 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1061 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1064 case R_X86_64_TLSGD:
1065 case R_X86_64_GOTTPOFF:
1066 case R_X86_64_GOT32:
1067 case R_X86_64_GOTPCREL:
1070 if (h->got.refcount > 0)
1071 h->got.refcount -= 1;
1073 else if (local_got_refcounts != NULL)
1075 if (local_got_refcounts[r_symndx] > 0)
1076 local_got_refcounts[r_symndx] -= 1;
1092 case R_X86_64_PLT32:
1095 if (h->plt.refcount > 0)
1096 h->plt.refcount -= 1;
1108 /* Adjust a symbol defined by a dynamic object and referenced by a
1109 regular object. The current definition is in some section of the
1110 dynamic object, but we're not including those sections. We have to
1111 change the definition to something the rest of the link can
1115 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1116 struct elf_link_hash_entry *h)
1118 struct elf64_x86_64_link_hash_table *htab;
1120 unsigned int power_of_two;
1122 /* If this is a function, put it in the procedure linkage table. We
1123 will fill in the contents of the procedure linkage table later,
1124 when we know the address of the .got section. */
1125 if (h->type == STT_FUNC
1126 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1128 if (h->plt.refcount <= 0
1129 || SYMBOL_CALLS_LOCAL (info, h)
1130 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1131 && h->root.type == bfd_link_hash_undefweak))
1133 /* This case can occur if we saw a PLT32 reloc in an input
1134 file, but the symbol was never referred to by a dynamic
1135 object, or if all references were garbage collected. In
1136 such a case, we don't actually need to build a procedure
1137 linkage table, and we can just do a PC32 reloc instead. */
1138 h->plt.offset = (bfd_vma) -1;
1139 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1145 /* It's possible that we incorrectly decided a .plt reloc was
1146 needed for an R_X86_64_PC32 reloc to a non-function sym in
1147 check_relocs. We can't decide accurately between function and
1148 non-function syms in check-relocs; Objects loaded later in
1149 the link may change h->type. So fix it now. */
1150 h->plt.offset = (bfd_vma) -1;
1152 /* If this is a weak symbol, and there is a real definition, the
1153 processor independent code will have arranged for us to see the
1154 real definition first, and we can just use the same value. */
1155 if (h->weakdef != NULL)
1157 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1158 || h->weakdef->root.type == bfd_link_hash_defweak);
1159 h->root.u.def.section = h->weakdef->root.u.def.section;
1160 h->root.u.def.value = h->weakdef->root.u.def.value;
1161 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1162 h->elf_link_hash_flags
1163 = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
1164 | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
1168 /* This is a reference to a symbol defined by a dynamic object which
1169 is not a function. */
1171 /* If we are creating a shared library, we must presume that the
1172 only references to the symbol are via the global offset table.
1173 For such cases we need not do anything here; the relocations will
1174 be handled correctly by relocate_section. */
1178 /* If there are no references to this symbol that do not use the
1179 GOT, we don't need to generate a copy reloc. */
1180 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1183 /* If -z nocopyreloc was given, we won't generate them either. */
1184 if (info->nocopyreloc)
1186 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1190 if (ELIMINATE_COPY_RELOCS)
1192 struct elf64_x86_64_link_hash_entry * eh;
1193 struct elf64_x86_64_dyn_relocs *p;
1195 eh = (struct elf64_x86_64_link_hash_entry *) h;
1196 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1198 s = p->sec->output_section;
1199 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1203 /* If we didn't find any dynamic relocs in read-only sections, then
1204 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1207 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1212 /* We must allocate the symbol in our .dynbss section, which will
1213 become part of the .bss section of the executable. There will be
1214 an entry for this symbol in the .dynsym section. The dynamic
1215 object will contain position independent code, so all references
1216 from the dynamic object to this symbol will go through the global
1217 offset table. The dynamic linker will use the .dynsym entry to
1218 determine the address it must put in the global offset table, so
1219 both the dynamic object and the regular object will refer to the
1220 same memory location for the variable. */
1222 htab = elf64_x86_64_hash_table (info);
1224 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1225 to copy the initial value out of the dynamic object and into the
1226 runtime process image. */
1227 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1229 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
1230 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1233 /* We need to figure out the alignment required for this symbol. I
1234 have no idea how ELF linkers handle this. 16-bytes is the size
1235 of the largest type that requires hard alignment -- long double. */
1236 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1238 power_of_two = bfd_log2 (h->size);
1239 if (power_of_two > 4)
1242 /* Apply the required alignment. */
1244 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
1245 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1247 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1251 /* Define the symbol as being at this point in the section. */
1252 h->root.u.def.section = s;
1253 h->root.u.def.value = s->_raw_size;
1255 /* Increment the section size to make room for the symbol. */
1256 s->_raw_size += h->size;
1261 /* Allocate space in .plt, .got and associated reloc sections for
1265 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1267 struct bfd_link_info *info;
1268 struct elf64_x86_64_link_hash_table *htab;
1269 struct elf64_x86_64_link_hash_entry *eh;
1270 struct elf64_x86_64_dyn_relocs *p;
1272 if (h->root.type == bfd_link_hash_indirect)
1275 if (h->root.type == bfd_link_hash_warning)
1276 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1278 info = (struct bfd_link_info *) inf;
1279 htab = elf64_x86_64_hash_table (info);
1281 if (htab->elf.dynamic_sections_created
1282 && h->plt.refcount > 0)
1284 /* Make sure this symbol is output as a dynamic symbol.
1285 Undefined weak syms won't yet be marked as dynamic. */
1286 if (h->dynindx == -1
1287 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1289 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1294 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1296 asection *s = htab->splt;
1298 /* If this is the first .plt entry, make room for the special
1300 if (s->_raw_size == 0)
1301 s->_raw_size += PLT_ENTRY_SIZE;
1303 h->plt.offset = s->_raw_size;
1305 /* If this symbol is not defined in a regular file, and we are
1306 not generating a shared library, then set the symbol to this
1307 location in the .plt. This is required to make function
1308 pointers compare as equal between the normal executable and
1309 the shared library. */
1311 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1313 h->root.u.def.section = s;
1314 h->root.u.def.value = h->plt.offset;
1317 /* Make room for this entry. */
1318 s->_raw_size += PLT_ENTRY_SIZE;
1320 /* We also need to make an entry in the .got.plt section, which
1321 will be placed in the .got section by the linker script. */
1322 htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
1324 /* We also need to make an entry in the .rela.plt section. */
1325 htab->srelplt->_raw_size += sizeof (Elf64_External_Rela);
1329 h->plt.offset = (bfd_vma) -1;
1330 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1335 h->plt.offset = (bfd_vma) -1;
1336 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1339 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1340 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1341 if (h->got.refcount > 0
1344 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1345 h->got.offset = (bfd_vma) -1;
1346 else if (h->got.refcount > 0)
1350 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1352 /* Make sure this symbol is output as a dynamic symbol.
1353 Undefined weak syms won't yet be marked as dynamic. */
1354 if (h->dynindx == -1
1355 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1357 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1362 h->got.offset = s->_raw_size;
1363 s->_raw_size += GOT_ENTRY_SIZE;
1364 /* R_X86_64_TLSGD needs 2 consecutive GOT slots. */
1365 if (tls_type == GOT_TLS_GD)
1366 s->_raw_size += GOT_ENTRY_SIZE;
1367 dyn = htab->elf.dynamic_sections_created;
1368 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1370 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1371 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1372 || tls_type == GOT_TLS_IE)
1373 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
1374 else if (tls_type == GOT_TLS_GD)
1375 htab->srelgot->_raw_size += 2 * sizeof (Elf64_External_Rela);
1376 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1377 || h->root.type != bfd_link_hash_undefweak)
1379 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1380 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
1383 h->got.offset = (bfd_vma) -1;
1385 eh = (struct elf64_x86_64_link_hash_entry *) h;
1386 if (eh->dyn_relocs == NULL)
1389 /* In the shared -Bsymbolic case, discard space allocated for
1390 dynamic pc-relative relocs against symbols which turn out to be
1391 defined in regular objects. For the normal shared case, discard
1392 space for pc-relative relocs that have become local due to symbol
1393 visibility changes. */
1397 /* Relocs that use pc_count are those that appear on a call
1398 insn, or certain REL relocs that can generated via assembly.
1399 We want calls to protected symbols to resolve directly to the
1400 function rather than going via the plt. If people want
1401 function pointer comparisons to work as expected then they
1402 should avoid writing weird assembly. */
1403 if (SYMBOL_CALLS_LOCAL (info, h))
1405 struct elf64_x86_64_dyn_relocs **pp;
1407 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1409 p->count -= p->pc_count;
1418 /* Also discard relocs on undefined weak syms with non-default
1420 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1421 && h->root.type == bfd_link_hash_undefweak)
1422 eh->dyn_relocs = NULL;
1424 else if (ELIMINATE_COPY_RELOCS)
1426 /* For the non-shared case, discard space for relocs against
1427 symbols which turn out to need copy relocs or are not
1430 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1431 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1432 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1433 || (htab->elf.dynamic_sections_created
1434 && (h->root.type == bfd_link_hash_undefweak
1435 || h->root.type == bfd_link_hash_undefined))))
1437 /* Make sure this symbol is output as a dynamic symbol.
1438 Undefined weak syms won't yet be marked as dynamic. */
1439 if (h->dynindx == -1
1440 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1442 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1446 /* If that succeeded, we know we'll be keeping all the
1448 if (h->dynindx != -1)
1452 eh->dyn_relocs = NULL;
1457 /* Finally, allocate space. */
1458 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1460 asection *sreloc = elf_section_data (p->sec)->sreloc;
1461 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
1467 /* Find any dynamic relocs that apply to read-only sections. */
1470 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1472 struct elf64_x86_64_link_hash_entry *eh;
1473 struct elf64_x86_64_dyn_relocs *p;
1475 if (h->root.type == bfd_link_hash_warning)
1476 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1478 eh = (struct elf64_x86_64_link_hash_entry *) h;
1479 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1481 asection *s = p->sec->output_section;
1483 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1485 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1487 info->flags |= DF_TEXTREL;
1489 /* Not an error, just cut short the traversal. */
1496 /* Set the sizes of the dynamic sections. */
1499 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1500 struct bfd_link_info *info)
1502 struct elf64_x86_64_link_hash_table *htab;
1508 htab = elf64_x86_64_hash_table (info);
1509 dynobj = htab->elf.dynobj;
1513 if (htab->elf.dynamic_sections_created)
1515 /* Set the contents of the .interp section to the interpreter. */
1516 if (info->executable)
1518 s = bfd_get_section_by_name (dynobj, ".interp");
1521 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1522 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1526 /* Set up .got offsets for local syms, and space for local dynamic
1528 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1530 bfd_signed_vma *local_got;
1531 bfd_signed_vma *end_local_got;
1532 char *local_tls_type;
1533 bfd_size_type locsymcount;
1534 Elf_Internal_Shdr *symtab_hdr;
1537 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1540 for (s = ibfd->sections; s != NULL; s = s->next)
1542 struct elf64_x86_64_dyn_relocs *p;
1544 for (p = *((struct elf64_x86_64_dyn_relocs **)
1545 &elf_section_data (s)->local_dynrel);
1549 if (!bfd_is_abs_section (p->sec)
1550 && bfd_is_abs_section (p->sec->output_section))
1552 /* Input section has been discarded, either because
1553 it is a copy of a linkonce section or due to
1554 linker script /DISCARD/, so we'll be discarding
1557 else if (p->count != 0)
1559 srel = elf_section_data (p->sec)->sreloc;
1560 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
1561 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1562 info->flags |= DF_TEXTREL;
1568 local_got = elf_local_got_refcounts (ibfd);
1572 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1573 locsymcount = symtab_hdr->sh_info;
1574 end_local_got = local_got + locsymcount;
1575 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
1577 srel = htab->srelgot;
1578 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1582 *local_got = s->_raw_size;
1583 s->_raw_size += GOT_ENTRY_SIZE;
1584 if (*local_tls_type == GOT_TLS_GD)
1585 s->_raw_size += GOT_ENTRY_SIZE;
1587 || *local_tls_type == GOT_TLS_GD
1588 || *local_tls_type == GOT_TLS_IE)
1589 srel->_raw_size += sizeof (Elf64_External_Rela);
1592 *local_got = (bfd_vma) -1;
1596 if (htab->tls_ld_got.refcount > 0)
1598 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1600 htab->tls_ld_got.offset = htab->sgot->_raw_size;
1601 htab->sgot->_raw_size += 2 * GOT_ENTRY_SIZE;
1602 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
1605 htab->tls_ld_got.offset = -1;
1607 /* Allocate global sym .plt and .got entries, and space for global
1608 sym dynamic relocs. */
1609 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1611 /* We now have determined the sizes of the various dynamic sections.
1612 Allocate memory for them. */
1614 for (s = dynobj->sections; s != NULL; s = s->next)
1616 if ((s->flags & SEC_LINKER_CREATED) == 0)
1621 || s == htab->sgotplt)
1623 /* Strip this section if we don't need it; see the
1626 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1628 if (s->_raw_size != 0 && s != htab->srelplt)
1631 /* We use the reloc_count field as a counter if we need
1632 to copy relocs into the output file. */
1637 /* It's not one of our sections, so don't allocate space. */
1641 if (s->_raw_size == 0)
1643 /* If we don't need this section, strip it from the
1644 output file. This is mostly to handle .rela.bss and
1645 .rela.plt. We must create both sections in
1646 create_dynamic_sections, because they must be created
1647 before the linker maps input sections to output
1648 sections. The linker does that before
1649 adjust_dynamic_symbol is called, and it is that
1650 function which decides whether anything needs to go
1651 into these sections. */
1653 _bfd_strip_section_from_output (info, s);
1657 /* Allocate memory for the section contents. We use bfd_zalloc
1658 here in case unused entries are not reclaimed before the
1659 section's contents are written out. This should not happen,
1660 but this way if it does, we get a R_X86_64_NONE reloc instead
1662 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1663 if (s->contents == NULL)
1667 if (htab->elf.dynamic_sections_created)
1669 /* Add some entries to the .dynamic section. We fill in the
1670 values later, in elf64_x86_64_finish_dynamic_sections, but we
1671 must add the entries now so that we get the correct size for
1672 the .dynamic section. The DT_DEBUG entry is filled in by the
1673 dynamic linker and used by the debugger. */
1674 #define add_dynamic_entry(TAG, VAL) \
1675 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1677 if (info->executable)
1679 if (!add_dynamic_entry (DT_DEBUG, 0))
1683 if (htab->splt->_raw_size != 0)
1685 if (!add_dynamic_entry (DT_PLTGOT, 0)
1686 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1687 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1688 || !add_dynamic_entry (DT_JMPREL, 0))
1694 if (!add_dynamic_entry (DT_RELA, 0)
1695 || !add_dynamic_entry (DT_RELASZ, 0)
1696 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1699 /* If any dynamic relocs apply to a read-only section,
1700 then we need a DT_TEXTREL entry. */
1701 if ((info->flags & DF_TEXTREL) == 0)
1702 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1705 if ((info->flags & DF_TEXTREL) != 0)
1707 if (!add_dynamic_entry (DT_TEXTREL, 0))
1712 #undef add_dynamic_entry
1717 /* Return the base VMA address which should be subtracted from real addresses
1718 when resolving @dtpoff relocation.
1719 This is PT_TLS segment p_vaddr. */
1722 dtpoff_base (struct bfd_link_info *info)
1724 /* If tls_sec is NULL, we should have signalled an error already. */
1725 if (elf_hash_table (info)->tls_sec == NULL)
1727 return elf_hash_table (info)->tls_sec->vma;
1730 /* Return the relocation value for @tpoff relocation
1731 if STT_TLS virtual address is ADDRESS. */
1734 tpoff (struct bfd_link_info *info, bfd_vma address)
1736 struct elf_link_hash_table *htab = elf_hash_table (info);
1738 /* If tls_segment is NULL, we should have signalled an error already. */
1739 if (htab->tls_sec == NULL)
1741 return address - htab->tls_size - htab->tls_sec->vma;
1744 /* Relocate an x86_64 ELF section. */
1747 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
1748 bfd *input_bfd, asection *input_section,
1749 bfd_byte *contents, Elf_Internal_Rela *relocs,
1750 Elf_Internal_Sym *local_syms,
1751 asection **local_sections)
1753 struct elf64_x86_64_link_hash_table *htab;
1754 Elf_Internal_Shdr *symtab_hdr;
1755 struct elf_link_hash_entry **sym_hashes;
1756 bfd_vma *local_got_offsets;
1757 Elf_Internal_Rela *rel;
1758 Elf_Internal_Rela *relend;
1760 if (info->relocatable)
1763 htab = elf64_x86_64_hash_table (info);
1764 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1765 sym_hashes = elf_sym_hashes (input_bfd);
1766 local_got_offsets = elf_local_got_offsets (input_bfd);
1769 relend = relocs + input_section->reloc_count;
1770 for (; rel < relend; rel++)
1772 unsigned int r_type;
1773 reloc_howto_type *howto;
1774 unsigned long r_symndx;
1775 struct elf_link_hash_entry *h;
1776 Elf_Internal_Sym *sym;
1780 bfd_boolean unresolved_reloc;
1781 bfd_reloc_status_type r;
1784 r_type = ELF64_R_TYPE (rel->r_info);
1785 if (r_type == (int) R_X86_64_GNU_VTINHERIT
1786 || r_type == (int) R_X86_64_GNU_VTENTRY)
1789 if (r_type >= R_X86_64_max)
1791 bfd_set_error (bfd_error_bad_value);
1795 howto = x86_64_elf_howto_table + r_type;
1796 r_symndx = ELF64_R_SYM (rel->r_info);
1800 unresolved_reloc = FALSE;
1801 if (r_symndx < symtab_hdr->sh_info)
1803 sym = local_syms + r_symndx;
1804 sec = local_sections[r_symndx];
1806 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1812 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1813 r_symndx, symtab_hdr, sym_hashes,
1815 unresolved_reloc, warned);
1817 /* When generating a shared object, the relocations handled here are
1818 copied into the output file to be resolved at run time. */
1821 case R_X86_64_GOT32:
1822 /* Relocation is to the entry for this symbol in the global
1824 case R_X86_64_GOTPCREL:
1825 /* Use global offset table as symbol value. */
1826 if (htab->sgot == NULL)
1833 off = h->got.offset;
1834 dyn = htab->elf.dynamic_sections_created;
1836 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1838 && SYMBOL_REFERENCES_LOCAL (info, h))
1839 || (ELF_ST_VISIBILITY (h->other)
1840 && h->root.type == bfd_link_hash_undefweak))
1842 /* This is actually a static link, or it is a -Bsymbolic
1843 link and the symbol is defined locally, or the symbol
1844 was forced to be local because of a version file. We
1845 must initialize this entry in the global offset table.
1846 Since the offset must always be a multiple of 8, we
1847 use the least significant bit to record whether we
1848 have initialized it already.
1850 When doing a dynamic link, we create a .rela.got
1851 relocation entry to initialize the value. This is
1852 done in the finish_dynamic_symbol routine. */
1857 bfd_put_64 (output_bfd, relocation,
1858 htab->sgot->contents + off);
1863 unresolved_reloc = FALSE;
1867 if (local_got_offsets == NULL)
1870 off = local_got_offsets[r_symndx];
1872 /* The offset must always be a multiple of 8. We use
1873 the least significant bit to record whether we have
1874 already generated the necessary reloc. */
1879 bfd_put_64 (output_bfd, relocation,
1880 htab->sgot->contents + off);
1885 Elf_Internal_Rela outrel;
1888 /* We need to generate a R_X86_64_RELATIVE reloc
1889 for the dynamic linker. */
1894 outrel.r_offset = (htab->sgot->output_section->vma
1895 + htab->sgot->output_offset
1897 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1898 outrel.r_addend = relocation;
1900 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
1901 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1904 local_got_offsets[r_symndx] |= 1;
1908 if (off >= (bfd_vma) -2)
1911 relocation = htab->sgot->output_offset + off;
1912 if (r_type == R_X86_64_GOTPCREL)
1913 relocation += htab->sgot->output_section->vma;
1917 case R_X86_64_PLT32:
1918 /* Relocation is to the entry for this symbol in the
1919 procedure linkage table. */
1921 /* Resolve a PLT32 reloc against a local symbol directly,
1922 without using the procedure linkage table. */
1926 if (h->plt.offset == (bfd_vma) -1
1927 || htab->splt == NULL)
1929 /* We didn't make a PLT entry for this symbol. This
1930 happens when statically linking PIC code, or when
1931 using -Bsymbolic. */
1935 relocation = (htab->splt->output_section->vma
1936 + htab->splt->output_offset
1938 unresolved_reloc = FALSE;
1948 /* FIXME: The ABI says the linker should make sure the value is
1949 the same when it's zeroextended to 64 bit. */
1951 /* r_symndx will be zero only for relocs against symbols
1952 from removed linkonce sections, or sections discarded by
1955 || (input_section->flags & SEC_ALLOC) == 0)
1960 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1961 || h->root.type != bfd_link_hash_undefweak)
1962 && ((r_type != R_X86_64_PC8
1963 && r_type != R_X86_64_PC16
1964 && r_type != R_X86_64_PC32)
1965 || !SYMBOL_CALLS_LOCAL (info, h)))
1966 || (ELIMINATE_COPY_RELOCS
1970 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1971 && (((h->elf_link_hash_flags
1972 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1973 && (h->elf_link_hash_flags
1974 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1975 || h->root.type == bfd_link_hash_undefweak
1976 || h->root.type == bfd_link_hash_undefined)))
1978 Elf_Internal_Rela outrel;
1980 bfd_boolean skip, relocate;
1983 /* When generating a shared object, these relocations
1984 are copied into the output file to be resolved at run
1990 _bfd_elf_section_offset (output_bfd, info, input_section,
1992 if (outrel.r_offset == (bfd_vma) -1)
1994 else if (outrel.r_offset == (bfd_vma) -2)
1995 skip = TRUE, relocate = TRUE;
1997 outrel.r_offset += (input_section->output_section->vma
1998 + input_section->output_offset);
2001 memset (&outrel, 0, sizeof outrel);
2003 /* h->dynindx may be -1 if this symbol was marked to
2007 && (r_type == R_X86_64_PC8
2008 || r_type == R_X86_64_PC16
2009 || r_type == R_X86_64_PC32
2012 || (h->elf_link_hash_flags
2013 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2015 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2016 outrel.r_addend = rel->r_addend;
2020 /* This symbol is local, or marked to become local. */
2021 if (r_type == R_X86_64_64)
2024 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2025 outrel.r_addend = relocation + rel->r_addend;
2031 if (bfd_is_abs_section (sec))
2033 else if (sec == NULL || sec->owner == NULL)
2035 bfd_set_error (bfd_error_bad_value);
2042 osec = sec->output_section;
2043 sindx = elf_section_data (osec)->dynindx;
2044 BFD_ASSERT (sindx > 0);
2047 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2048 outrel.r_addend = relocation + rel->r_addend;
2052 sreloc = elf_section_data (input_section)->sreloc;
2056 loc = sreloc->contents;
2057 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2058 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2060 /* If this reloc is against an external symbol, we do
2061 not want to fiddle with the addend. Otherwise, we
2062 need to include the symbol value so that it becomes
2063 an addend for the dynamic reloc. */
2070 case R_X86_64_TLSGD:
2071 case R_X86_64_GOTTPOFF:
2072 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
2073 tls_type = GOT_UNKNOWN;
2074 if (h == NULL && local_got_offsets)
2075 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2078 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2079 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2080 r_type = R_X86_64_TPOFF32;
2082 if (r_type == R_X86_64_TLSGD)
2084 if (tls_type == GOT_TLS_IE)
2085 r_type = R_X86_64_GOTTPOFF;
2088 if (r_type == R_X86_64_TPOFF32)
2090 BFD_ASSERT (! unresolved_reloc);
2091 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2094 static unsigned char tlsgd[8]
2095 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2097 /* GD->LE transition.
2098 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2099 .word 0x6666; rex64; call __tls_get_addr@plt
2102 leaq foo@tpoff(%rax), %rax */
2103 BFD_ASSERT (rel->r_offset >= 4);
2104 for (i = 0; i < 4; i++)
2105 BFD_ASSERT (bfd_get_8 (input_bfd,
2106 contents + rel->r_offset - 4 + i)
2108 BFD_ASSERT (rel->r_offset + 12 <= input_section->_raw_size);
2109 for (i = 0; i < 4; i++)
2110 BFD_ASSERT (bfd_get_8 (input_bfd,
2111 contents + rel->r_offset + 4 + i)
2113 BFD_ASSERT (rel + 1 < relend);
2114 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2115 memcpy (contents + rel->r_offset - 4,
2116 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2118 bfd_put_32 (output_bfd, tpoff (info, relocation),
2119 contents + rel->r_offset + 8);
2120 /* Skip R_X86_64_PLT32. */
2126 unsigned int val, type, reg;
2128 /* IE->LE transition:
2129 Originally it can be one of:
2130 movq foo@gottpoff(%rip), %reg
2131 addq foo@gottpoff(%rip), %reg
2134 leaq foo(%reg), %reg
2136 BFD_ASSERT (rel->r_offset >= 3);
2137 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2138 BFD_ASSERT (val == 0x48 || val == 0x4c);
2139 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2140 BFD_ASSERT (type == 0x8b || type == 0x03);
2141 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2142 BFD_ASSERT ((reg & 0xc7) == 5);
2144 BFD_ASSERT (rel->r_offset + 4 <= input_section->_raw_size);
2149 bfd_put_8 (output_bfd, 0x49,
2150 contents + rel->r_offset - 3);
2151 bfd_put_8 (output_bfd, 0xc7,
2152 contents + rel->r_offset - 2);
2153 bfd_put_8 (output_bfd, 0xc0 | reg,
2154 contents + rel->r_offset - 1);
2158 /* addq -> addq - addressing with %rsp/%r12 is
2161 bfd_put_8 (output_bfd, 0x49,
2162 contents + rel->r_offset - 3);
2163 bfd_put_8 (output_bfd, 0x81,
2164 contents + rel->r_offset - 2);
2165 bfd_put_8 (output_bfd, 0xc0 | reg,
2166 contents + rel->r_offset - 1);
2172 bfd_put_8 (output_bfd, 0x4d,
2173 contents + rel->r_offset - 3);
2174 bfd_put_8 (output_bfd, 0x8d,
2175 contents + rel->r_offset - 2);
2176 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2177 contents + rel->r_offset - 1);
2179 bfd_put_32 (output_bfd, tpoff (info, relocation),
2180 contents + rel->r_offset);
2185 if (htab->sgot == NULL)
2189 off = h->got.offset;
2192 if (local_got_offsets == NULL)
2195 off = local_got_offsets[r_symndx];
2202 Elf_Internal_Rela outrel;
2206 if (htab->srelgot == NULL)
2209 outrel.r_offset = (htab->sgot->output_section->vma
2210 + htab->sgot->output_offset + off);
2212 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2213 if (r_type == R_X86_64_TLSGD)
2214 dr_type = R_X86_64_DTPMOD64;
2216 dr_type = R_X86_64_TPOFF64;
2218 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2219 outrel.r_addend = 0;
2220 if (dr_type == R_X86_64_TPOFF64 && indx == 0)
2221 outrel.r_addend = relocation - dtpoff_base (info);
2222 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2224 loc = htab->srelgot->contents;
2225 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2226 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2228 if (r_type == R_X86_64_TLSGD)
2232 BFD_ASSERT (! unresolved_reloc);
2233 bfd_put_64 (output_bfd,
2234 relocation - dtpoff_base (info),
2235 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2239 bfd_put_64 (output_bfd, 0,
2240 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2241 outrel.r_info = ELF64_R_INFO (indx,
2243 outrel.r_offset += GOT_ENTRY_SIZE;
2244 htab->srelgot->reloc_count++;
2245 loc += sizeof (Elf64_External_Rela);
2246 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2253 local_got_offsets[r_symndx] |= 1;
2256 if (off >= (bfd_vma) -2)
2258 if (r_type == ELF64_R_TYPE (rel->r_info))
2260 relocation = htab->sgot->output_section->vma
2261 + htab->sgot->output_offset + off;
2262 unresolved_reloc = FALSE;
2267 static unsigned char tlsgd[8]
2268 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2270 /* GD->IE transition.
2271 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2272 .word 0x6666; rex64; call __tls_get_addr@plt
2275 addq foo@gottpoff(%rip), %rax */
2276 BFD_ASSERT (rel->r_offset >= 4);
2277 for (i = 0; i < 4; i++)
2278 BFD_ASSERT (bfd_get_8 (input_bfd,
2279 contents + rel->r_offset - 4 + i)
2281 BFD_ASSERT (rel->r_offset + 12 <= input_section->_raw_size);
2282 for (i = 0; i < 4; i++)
2283 BFD_ASSERT (bfd_get_8 (input_bfd,
2284 contents + rel->r_offset + 4 + i)
2286 BFD_ASSERT (rel + 1 < relend);
2287 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2288 memcpy (contents + rel->r_offset - 4,
2289 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2292 relocation = (htab->sgot->output_section->vma
2293 + htab->sgot->output_offset + off
2295 - input_section->output_section->vma
2296 - input_section->output_offset
2298 bfd_put_32 (output_bfd, relocation,
2299 contents + rel->r_offset + 8);
2300 /* Skip R_X86_64_PLT32. */
2306 case R_X86_64_TLSLD:
2309 /* LD->LE transition:
2311 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2313 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2314 BFD_ASSERT (rel->r_offset >= 3);
2315 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2317 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2319 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2321 BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size);
2322 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2324 BFD_ASSERT (rel + 1 < relend);
2325 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2326 memcpy (contents + rel->r_offset - 3,
2327 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2328 /* Skip R_X86_64_PLT32. */
2333 if (htab->sgot == NULL)
2336 off = htab->tls_ld_got.offset;
2341 Elf_Internal_Rela outrel;
2344 if (htab->srelgot == NULL)
2347 outrel.r_offset = (htab->sgot->output_section->vma
2348 + htab->sgot->output_offset + off);
2350 bfd_put_64 (output_bfd, 0,
2351 htab->sgot->contents + off);
2352 bfd_put_64 (output_bfd, 0,
2353 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2354 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2355 outrel.r_addend = 0;
2356 loc = htab->srelgot->contents;
2357 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2358 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2359 htab->tls_ld_got.offset |= 1;
2361 relocation = htab->sgot->output_section->vma
2362 + htab->sgot->output_offset + off;
2363 unresolved_reloc = FALSE;
2366 case R_X86_64_DTPOFF32:
2367 if (info->shared || (input_section->flags & SEC_CODE) == 0)
2368 relocation -= dtpoff_base (info);
2370 relocation = tpoff (info, relocation);
2373 case R_X86_64_TPOFF32:
2374 BFD_ASSERT (! info->shared);
2375 relocation = tpoff (info, relocation);
2382 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2383 because such sections are not SEC_ALLOC and thus ld.so will
2384 not process them. */
2385 if (unresolved_reloc
2386 && !((input_section->flags & SEC_DEBUGGING) != 0
2387 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2388 (*_bfd_error_handler)
2389 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
2390 bfd_archive_filename (input_bfd),
2391 bfd_get_section_name (input_bfd, input_section),
2392 (long) rel->r_offset,
2393 h->root.root.string);
2395 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2396 contents, rel->r_offset,
2397 relocation, rel->r_addend);
2399 if (r != bfd_reloc_ok)
2404 name = h->root.root.string;
2407 name = bfd_elf_string_from_elf_section (input_bfd,
2408 symtab_hdr->sh_link,
2413 name = bfd_section_name (input_bfd, sec);
2416 if (r == bfd_reloc_overflow)
2419 if (! ((*info->callbacks->reloc_overflow)
2420 (info, name, howto->name, (bfd_vma) 0,
2421 input_bfd, input_section, rel->r_offset)))
2426 (*_bfd_error_handler)
2427 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
2428 bfd_archive_filename (input_bfd),
2429 bfd_get_section_name (input_bfd, input_section),
2430 (long) rel->r_offset, name, (int) r);
2439 /* Finish up dynamic symbol handling. We set the contents of various
2440 dynamic sections here. */
2443 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
2444 struct bfd_link_info *info,
2445 struct elf_link_hash_entry *h,
2446 Elf_Internal_Sym *sym)
2448 struct elf64_x86_64_link_hash_table *htab;
2450 htab = elf64_x86_64_hash_table (info);
2452 if (h->plt.offset != (bfd_vma) -1)
2456 Elf_Internal_Rela rela;
2459 /* This symbol has an entry in the procedure linkage table. Set
2461 if (h->dynindx == -1
2462 || htab->splt == NULL
2463 || htab->sgotplt == NULL
2464 || htab->srelplt == NULL)
2467 /* Get the index in the procedure linkage table which
2468 corresponds to this symbol. This is the index of this symbol
2469 in all the symbols for which we are making plt entries. The
2470 first entry in the procedure linkage table is reserved. */
2471 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2473 /* Get the offset into the .got table of the entry that
2474 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
2475 bytes. The first three are reserved for the dynamic linker. */
2476 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2478 /* Fill in the entry in the procedure linkage table. */
2479 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
2482 /* Insert the relocation positions of the plt section. The magic
2483 numbers at the end of the statements are the positions of the
2484 relocations in the plt section. */
2485 /* Put offset for jmp *name@GOTPCREL(%rip), since the
2486 instruction uses 6 bytes, subtract this value. */
2487 bfd_put_32 (output_bfd,
2488 (htab->sgotplt->output_section->vma
2489 + htab->sgotplt->output_offset
2491 - htab->splt->output_section->vma
2492 - htab->splt->output_offset
2495 htab->splt->contents + h->plt.offset + 2);
2496 /* Put relocation index. */
2497 bfd_put_32 (output_bfd, plt_index,
2498 htab->splt->contents + h->plt.offset + 7);
2499 /* Put offset for jmp .PLT0. */
2500 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
2501 htab->splt->contents + h->plt.offset + 12);
2503 /* Fill in the entry in the global offset table, initially this
2504 points to the pushq instruction in the PLT which is at offset 6. */
2505 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
2506 + htab->splt->output_offset
2507 + h->plt.offset + 6),
2508 htab->sgotplt->contents + got_offset);
2510 /* Fill in the entry in the .rela.plt section. */
2511 rela.r_offset = (htab->sgotplt->output_section->vma
2512 + htab->sgotplt->output_offset
2514 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
2516 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
2517 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2519 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2521 /* Mark the symbol as undefined, rather than as defined in
2522 the .plt section. Leave the value alone. This is a clue
2523 for the dynamic linker, to make function pointer
2524 comparisons work between an application and shared
2526 sym->st_shndx = SHN_UNDEF;
2530 if (h->got.offset != (bfd_vma) -1
2531 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_GD
2532 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
2534 Elf_Internal_Rela rela;
2537 /* This symbol has an entry in the global offset table. Set it
2539 if (htab->sgot == NULL || htab->srelgot == NULL)
2542 rela.r_offset = (htab->sgot->output_section->vma
2543 + htab->sgot->output_offset
2544 + (h->got.offset &~ (bfd_vma) 1));
2546 /* If this is a static link, or it is a -Bsymbolic link and the
2547 symbol is defined locally or was forced to be local because
2548 of a version file, we just want to emit a RELATIVE reloc.
2549 The entry in the global offset table will already have been
2550 initialized in the relocate_section function. */
2552 && SYMBOL_REFERENCES_LOCAL (info, h))
2554 BFD_ASSERT((h->got.offset & 1) != 0);
2555 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2556 rela.r_addend = (h->root.u.def.value
2557 + h->root.u.def.section->output_section->vma
2558 + h->root.u.def.section->output_offset);
2562 BFD_ASSERT((h->got.offset & 1) == 0);
2563 bfd_put_64 (output_bfd, (bfd_vma) 0,
2564 htab->sgot->contents + h->got.offset);
2565 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
2569 loc = htab->srelgot->contents;
2570 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2571 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2574 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2576 Elf_Internal_Rela rela;
2579 /* This symbol needs a copy reloc. Set it up. */
2581 if (h->dynindx == -1
2582 || (h->root.type != bfd_link_hash_defined
2583 && h->root.type != bfd_link_hash_defweak)
2584 || htab->srelbss == NULL)
2587 rela.r_offset = (h->root.u.def.value
2588 + h->root.u.def.section->output_section->vma
2589 + h->root.u.def.section->output_offset);
2590 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
2592 loc = htab->srelbss->contents;
2593 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
2594 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2597 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2598 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2599 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2600 sym->st_shndx = SHN_ABS;
2605 /* Used to decide how to sort relocs in an optimal manner for the
2606 dynamic linker, before writing them out. */
2608 static enum elf_reloc_type_class
2609 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
2611 switch ((int) ELF64_R_TYPE (rela->r_info))
2613 case R_X86_64_RELATIVE:
2614 return reloc_class_relative;
2615 case R_X86_64_JUMP_SLOT:
2616 return reloc_class_plt;
2618 return reloc_class_copy;
2620 return reloc_class_normal;
2624 /* Finish up the dynamic sections. */
2627 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
2629 struct elf64_x86_64_link_hash_table *htab;
2633 htab = elf64_x86_64_hash_table (info);
2634 dynobj = htab->elf.dynobj;
2635 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2637 if (htab->elf.dynamic_sections_created)
2639 Elf64_External_Dyn *dyncon, *dynconend;
2641 if (sdyn == NULL || htab->sgot == NULL)
2644 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2645 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2646 for (; dyncon < dynconend; dyncon++)
2648 Elf_Internal_Dyn dyn;
2651 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2659 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
2663 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2667 s = htab->srelplt->output_section;
2668 if (s->_cooked_size != 0)
2669 dyn.d_un.d_val = s->_cooked_size;
2671 dyn.d_un.d_val = s->_raw_size;
2675 /* The procedure linkage table relocs (DT_JMPREL) should
2676 not be included in the overall relocs (DT_RELA).
2677 Therefore, we override the DT_RELASZ entry here to
2678 make it not include the JMPREL relocs. Since the
2679 linker script arranges for .rela.plt to follow all
2680 other relocation sections, we don't have to worry
2681 about changing the DT_RELA entry. */
2682 if (htab->srelplt != NULL)
2684 s = htab->srelplt->output_section;
2685 if (s->_cooked_size != 0)
2686 dyn.d_un.d_val -= s->_cooked_size;
2688 dyn.d_un.d_val -= s->_raw_size;
2693 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2696 /* Fill in the special first entry in the procedure linkage table. */
2697 if (htab->splt && htab->splt->_raw_size > 0)
2699 /* Fill in the first entry in the procedure linkage table. */
2700 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
2702 /* Add offset for pushq GOT+8(%rip), since the instruction
2703 uses 6 bytes subtract this value. */
2704 bfd_put_32 (output_bfd,
2705 (htab->sgotplt->output_section->vma
2706 + htab->sgotplt->output_offset
2708 - htab->splt->output_section->vma
2709 - htab->splt->output_offset
2711 htab->splt->contents + 2);
2712 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
2713 the end of the instruction. */
2714 bfd_put_32 (output_bfd,
2715 (htab->sgotplt->output_section->vma
2716 + htab->sgotplt->output_offset
2718 - htab->splt->output_section->vma
2719 - htab->splt->output_offset
2721 htab->splt->contents + 8);
2723 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
2730 /* Fill in the first three entries in the global offset table. */
2731 if (htab->sgotplt->_raw_size > 0)
2733 /* Set the first entry in the global offset table to the address of
2734 the dynamic section. */
2736 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
2738 bfd_put_64 (output_bfd,
2739 sdyn->output_section->vma + sdyn->output_offset,
2740 htab->sgotplt->contents);
2741 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
2742 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
2743 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
2746 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
2754 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
2755 #define TARGET_LITTLE_NAME "elf64-x86-64"
2756 #define ELF_ARCH bfd_arch_i386
2757 #define ELF_MACHINE_CODE EM_X86_64
2758 #define ELF_MAXPAGESIZE 0x100000
2760 #define elf_backend_can_gc_sections 1
2761 #define elf_backend_can_refcount 1
2762 #define elf_backend_want_got_plt 1
2763 #define elf_backend_plt_readonly 1
2764 #define elf_backend_want_plt_sym 0
2765 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
2766 #define elf_backend_rela_normal 1
2768 #define elf_info_to_howto elf64_x86_64_info_to_howto
2770 #define bfd_elf64_bfd_link_hash_table_create \
2771 elf64_x86_64_link_hash_table_create
2772 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
2774 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
2775 #define elf_backend_check_relocs elf64_x86_64_check_relocs
2776 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
2777 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
2778 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
2779 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
2780 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
2781 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
2782 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
2783 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
2784 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
2785 #define elf_backend_relocate_section elf64_x86_64_relocate_section
2786 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
2787 #define elf_backend_object_p elf64_x86_64_elf_object_p
2788 #define bfd_elf64_mkobject elf64_x86_64_mkobject
2790 #include "elf64-target.h"