1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
28 #include "bfd_stdint.h"
32 #include "elf/x86-64.h"
34 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
35 #define MINUS_ONE (~ (bfd_vma) 0)
37 /* The relocation "howto" table. Order of fields:
38 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
39 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
40 static reloc_howto_type x86_64_elf_howto_table[] =
42 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
43 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
45 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
46 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
48 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
49 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
51 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
52 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
54 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
55 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
57 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
58 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
60 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
61 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
63 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
64 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
66 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
67 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
69 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
70 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
72 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
73 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
75 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
76 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
78 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
79 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
80 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
81 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
82 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
84 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
85 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
86 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
87 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
89 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
90 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
92 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
93 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
95 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
96 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
98 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
99 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
101 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
102 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
104 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
105 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
107 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
108 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
110 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
111 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
113 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
114 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
115 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
116 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
117 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
118 FALSE, 0xffffffff, 0xffffffff, TRUE),
119 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
120 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
122 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
123 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
125 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
126 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
127 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
128 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
129 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
131 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
132 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
136 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
137 complain_overflow_bitfield, bfd_elf_generic_reloc,
138 "R_X86_64_GOTPC32_TLSDESC",
139 FALSE, 0xffffffff, 0xffffffff, TRUE),
140 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
141 complain_overflow_dont, bfd_elf_generic_reloc,
142 "R_X86_64_TLSDESC_CALL",
144 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
145 complain_overflow_bitfield, bfd_elf_generic_reloc,
147 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
148 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
149 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
152 /* We have a gap in the reloc numbers here.
153 R_X86_64_standard counts the number up to this point, and
154 R_X86_64_vt_offset is the value to subtract from a reloc type of
155 R_X86_64_GNU_VT* to form an index into this table. */
156 #define R_X86_64_standard (R_X86_64_IRELATIVE + 1)
157 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
159 /* GNU extension to record C++ vtable hierarchy. */
160 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
161 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
163 /* GNU extension to record C++ vtable member usage. */
164 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
165 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
169 #define IS_X86_64_PCREL_TYPE(TYPE) \
170 ( ((TYPE) == R_X86_64_PC8) \
171 || ((TYPE) == R_X86_64_PC16) \
172 || ((TYPE) == R_X86_64_PC32) \
173 || ((TYPE) == R_X86_64_PC64))
175 /* Map BFD relocs to the x86_64 elf relocs. */
178 bfd_reloc_code_real_type bfd_reloc_val;
179 unsigned char elf_reloc_val;
182 static const struct elf_reloc_map x86_64_reloc_map[] =
184 { BFD_RELOC_NONE, R_X86_64_NONE, },
185 { BFD_RELOC_64, R_X86_64_64, },
186 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
187 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
188 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
189 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
190 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
191 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
192 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
193 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
194 { BFD_RELOC_32, R_X86_64_32, },
195 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
196 { BFD_RELOC_16, R_X86_64_16, },
197 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
198 { BFD_RELOC_8, R_X86_64_8, },
199 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
200 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
201 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
202 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
203 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
204 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
205 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
206 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
207 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
208 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
209 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
210 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
211 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
212 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
213 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
214 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
215 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
216 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
217 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
218 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
219 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
220 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
221 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
224 static reloc_howto_type *
225 elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
229 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
230 || r_type >= (unsigned int) R_X86_64_max)
232 if (r_type >= (unsigned int) R_X86_64_standard)
234 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
236 r_type = R_X86_64_NONE;
241 i = r_type - (unsigned int) R_X86_64_vt_offset;
242 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
243 return &x86_64_elf_howto_table[i];
246 /* Given a BFD reloc type, return a HOWTO structure. */
247 static reloc_howto_type *
248 elf64_x86_64_reloc_type_lookup (bfd *abfd,
249 bfd_reloc_code_real_type code)
253 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
256 if (x86_64_reloc_map[i].bfd_reloc_val == code)
257 return elf64_x86_64_rtype_to_howto (abfd,
258 x86_64_reloc_map[i].elf_reloc_val);
263 static reloc_howto_type *
264 elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
270 i < (sizeof (x86_64_elf_howto_table)
271 / sizeof (x86_64_elf_howto_table[0]));
273 if (x86_64_elf_howto_table[i].name != NULL
274 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
275 return &x86_64_elf_howto_table[i];
280 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
283 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
284 Elf_Internal_Rela *dst)
288 r_type = ELF64_R_TYPE (dst->r_info);
289 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
290 BFD_ASSERT (r_type == cache_ptr->howto->type);
293 /* Support for core dump NOTE sections. */
295 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
300 switch (note->descsz)
305 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
307 elf_tdata (abfd)->core_signal
308 = bfd_get_16 (abfd, note->descdata + 12);
311 elf_tdata (abfd)->core_pid
312 = bfd_get_32 (abfd, note->descdata + 32);
321 /* Make a ".reg/999" section. */
322 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
323 size, note->descpos + offset);
327 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
329 switch (note->descsz)
334 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
335 elf_tdata (abfd)->core_program
336 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
337 elf_tdata (abfd)->core_command
338 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
341 /* Note that for some reason, a spurious space is tacked
342 onto the end of the args in some (at least one anyway)
343 implementations, so strip it off if it exists. */
346 char *command = elf_tdata (abfd)->core_command;
347 int n = strlen (command);
349 if (0 < n && command[n - 1] == ' ')
350 command[n - 1] = '\0';
356 /* Functions for the x86-64 ELF linker. */
358 /* The name of the dynamic interpreter. This is put in the .interp
361 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
363 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
364 copying dynamic variables from a shared lib into an app's dynbss
365 section, and instead use a dynamic relocation to point into the
367 #define ELIMINATE_COPY_RELOCS 1
369 /* The size in bytes of an entry in the global offset table. */
371 #define GOT_ENTRY_SIZE 8
373 /* The size in bytes of an entry in the procedure linkage table. */
375 #define PLT_ENTRY_SIZE 16
377 /* The first entry in a procedure linkage table looks like this. See the
378 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
380 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
382 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
383 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
384 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
387 /* Subsequent entries in a procedure linkage table look like this. */
389 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
391 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
392 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
393 0x68, /* pushq immediate */
394 0, 0, 0, 0, /* replaced with index into relocation table. */
395 0xe9, /* jmp relative */
396 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
399 /* x86-64 ELF linker hash entry. */
401 struct elf64_x86_64_link_hash_entry
403 struct elf_link_hash_entry elf;
405 /* Track dynamic relocs copied for this symbol. */
406 struct elf_dyn_relocs *dyn_relocs;
408 #define GOT_UNKNOWN 0
412 #define GOT_TLS_GDESC 4
413 #define GOT_TLS_GD_BOTH_P(type) \
414 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
415 #define GOT_TLS_GD_P(type) \
416 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
417 #define GOT_TLS_GDESC_P(type) \
418 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
419 #define GOT_TLS_GD_ANY_P(type) \
420 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
421 unsigned char tls_type;
423 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
424 starting at the end of the jump table. */
428 #define elf64_x86_64_hash_entry(ent) \
429 ((struct elf64_x86_64_link_hash_entry *)(ent))
431 struct elf64_x86_64_obj_tdata
433 struct elf_obj_tdata root;
435 /* tls_type for each local got entry. */
436 char *local_got_tls_type;
438 /* GOTPLT entries for TLS descriptors. */
439 bfd_vma *local_tlsdesc_gotent;
442 #define elf64_x86_64_tdata(abfd) \
443 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
445 #define elf64_x86_64_local_got_tls_type(abfd) \
446 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
448 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
449 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
451 #define is_x86_64_elf(bfd) \
452 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
453 && elf_tdata (bfd) != NULL \
454 && elf_object_id (bfd) == X86_64_ELF_TDATA)
457 elf64_x86_64_mkobject (bfd *abfd)
459 return bfd_elf_allocate_object (abfd, sizeof (struct elf64_x86_64_obj_tdata),
463 /* x86-64 ELF linker hash table. */
465 struct elf64_x86_64_link_hash_table
467 struct elf_link_hash_table elf;
469 /* Short-cuts to get to dynamic linker sections. */
473 /* The offset into splt of the PLT entry for the TLS descriptor
474 resolver. Special values are 0, if not necessary (or not found
475 to be necessary yet), and -1 if needed but not determined
478 /* The offset into sgot of the GOT entry used by the PLT entry
483 bfd_signed_vma refcount;
487 /* The amount of space used by the jump slots in the GOT. */
488 bfd_vma sgotplt_jump_table_size;
490 /* Small local sym to section mapping cache. */
491 struct sym_sec_cache sym_sec;
493 /* _TLS_MODULE_BASE_ symbol. */
494 struct bfd_link_hash_entry *tls_module_base;
496 /* Used by local STT_GNU_IFUNC symbols. */
497 htab_t loc_hash_table;
498 void *loc_hash_memory;
501 /* Get the x86-64 ELF linker hash table from a link_info structure. */
503 #define elf64_x86_64_hash_table(p) \
504 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
506 #define elf64_x86_64_compute_jump_table_size(htab) \
507 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
509 /* Create an entry in an x86-64 ELF linker hash table. */
511 static struct bfd_hash_entry *
512 elf64_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
513 struct bfd_hash_table *table,
516 /* Allocate the structure if it has not already been allocated by a
520 entry = bfd_hash_allocate (table,
521 sizeof (struct elf64_x86_64_link_hash_entry));
526 /* Call the allocation method of the superclass. */
527 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
530 struct elf64_x86_64_link_hash_entry *eh;
532 eh = (struct elf64_x86_64_link_hash_entry *) entry;
533 eh->dyn_relocs = NULL;
534 eh->tls_type = GOT_UNKNOWN;
535 eh->tlsdesc_got = (bfd_vma) -1;
541 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
542 for local symbol so that we can handle local STT_GNU_IFUNC symbols
543 as global symbol. We reuse indx and dynstr_index for local symbol
544 hash since they aren't used by global symbols in this backend. */
547 elf64_x86_64_local_htab_hash (const void *ptr)
549 struct elf_link_hash_entry *h
550 = (struct elf_link_hash_entry *) ptr;
551 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
554 /* Compare local hash entries. */
557 elf64_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
559 struct elf_link_hash_entry *h1
560 = (struct elf_link_hash_entry *) ptr1;
561 struct elf_link_hash_entry *h2
562 = (struct elf_link_hash_entry *) ptr2;
564 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
567 /* Find and/or create a hash entry for local symbol. */
569 static struct elf_link_hash_entry *
570 elf64_x86_64_get_local_sym_hash (struct elf64_x86_64_link_hash_table *htab,
571 bfd *abfd, const Elf_Internal_Rela *rel,
574 struct elf64_x86_64_link_hash_entry e, *ret;
575 asection *sec = abfd->sections;
576 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
577 ELF64_R_SYM (rel->r_info));
580 e.elf.indx = sec->id;
581 e.elf.dynstr_index = ELF64_R_SYM (rel->r_info);
582 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
583 create ? INSERT : NO_INSERT);
590 ret = (struct elf64_x86_64_link_hash_entry *) *slot;
594 ret = (struct elf64_x86_64_link_hash_entry *)
595 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
596 sizeof (struct elf64_x86_64_link_hash_entry));
599 memset (ret, 0, sizeof (*ret));
600 ret->elf.indx = sec->id;
601 ret->elf.dynstr_index = ELF64_R_SYM (rel->r_info);
602 ret->elf.dynindx = -1;
603 ret->elf.plt.offset = (bfd_vma) -1;
604 ret->elf.got.offset = (bfd_vma) -1;
610 /* Create an X86-64 ELF linker hash table. */
612 static struct bfd_link_hash_table *
613 elf64_x86_64_link_hash_table_create (bfd *abfd)
615 struct elf64_x86_64_link_hash_table *ret;
616 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
618 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
622 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
623 elf64_x86_64_link_hash_newfunc,
624 sizeof (struct elf64_x86_64_link_hash_entry)))
632 ret->sym_sec.abfd = NULL;
633 ret->tlsdesc_plt = 0;
634 ret->tlsdesc_got = 0;
635 ret->tls_ld_got.refcount = 0;
636 ret->sgotplt_jump_table_size = 0;
637 ret->tls_module_base = NULL;
639 ret->loc_hash_table = htab_try_create (1024,
640 elf64_x86_64_local_htab_hash,
641 elf64_x86_64_local_htab_eq,
643 ret->loc_hash_memory = objalloc_create ();
644 if (!ret->loc_hash_table || !ret->loc_hash_memory)
650 return &ret->elf.root;
653 /* Destroy an X86-64 ELF linker hash table. */
656 elf64_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
658 struct elf64_x86_64_link_hash_table *htab
659 = (struct elf64_x86_64_link_hash_table *) hash;
661 if (htab->loc_hash_table)
662 htab_delete (htab->loc_hash_table);
663 if (htab->loc_hash_memory)
664 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
665 _bfd_generic_link_hash_table_free (hash);
668 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
669 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
673 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
675 struct elf64_x86_64_link_hash_table *htab;
677 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
680 htab = elf64_x86_64_hash_table (info);
681 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
683 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
686 || (!info->shared && !htab->srelbss))
692 /* Copy the extra info we tack onto an elf_link_hash_entry. */
695 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
696 struct elf_link_hash_entry *dir,
697 struct elf_link_hash_entry *ind)
699 struct elf64_x86_64_link_hash_entry *edir, *eind;
701 edir = (struct elf64_x86_64_link_hash_entry *) dir;
702 eind = (struct elf64_x86_64_link_hash_entry *) ind;
704 if (eind->dyn_relocs != NULL)
706 if (edir->dyn_relocs != NULL)
708 struct elf_dyn_relocs **pp;
709 struct elf_dyn_relocs *p;
711 /* Add reloc counts against the indirect sym to the direct sym
712 list. Merge any entries against the same section. */
713 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
715 struct elf_dyn_relocs *q;
717 for (q = edir->dyn_relocs; q != NULL; q = q->next)
718 if (q->sec == p->sec)
720 q->pc_count += p->pc_count;
721 q->count += p->count;
728 *pp = edir->dyn_relocs;
731 edir->dyn_relocs = eind->dyn_relocs;
732 eind->dyn_relocs = NULL;
735 if (ind->root.type == bfd_link_hash_indirect
736 && dir->got.refcount <= 0)
738 edir->tls_type = eind->tls_type;
739 eind->tls_type = GOT_UNKNOWN;
742 if (ELIMINATE_COPY_RELOCS
743 && ind->root.type != bfd_link_hash_indirect
744 && dir->dynamic_adjusted)
746 /* If called to transfer flags for a weakdef during processing
747 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
748 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
749 dir->ref_dynamic |= ind->ref_dynamic;
750 dir->ref_regular |= ind->ref_regular;
751 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
752 dir->needs_plt |= ind->needs_plt;
753 dir->pointer_equality_needed |= ind->pointer_equality_needed;
756 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
760 elf64_x86_64_elf_object_p (bfd *abfd)
762 /* Set the right machine number for an x86-64 elf64 file. */
763 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
781 /* Return TRUE if the TLS access code sequence support transition
785 elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec,
787 Elf_Internal_Shdr *symtab_hdr,
788 struct elf_link_hash_entry **sym_hashes,
790 const Elf_Internal_Rela *rel,
791 const Elf_Internal_Rela *relend)
794 unsigned long r_symndx;
795 struct elf_link_hash_entry *h;
798 /* Get the section contents. */
799 if (contents == NULL)
801 if (elf_section_data (sec)->this_hdr.contents != NULL)
802 contents = elf_section_data (sec)->this_hdr.contents;
805 /* FIXME: How to better handle error condition? */
806 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
809 /* Cache the section contents for elf_link_input_bfd. */
810 elf_section_data (sec)->this_hdr.contents = contents;
814 offset = rel->r_offset;
819 if ((rel + 1) >= relend)
822 if (r_type == R_X86_64_TLSGD)
824 /* Check transition from GD access model. Only
825 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
826 .word 0x6666; rex64; call __tls_get_addr
827 can transit to different access model. */
829 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } },
830 call = { { 0x66, 0x66, 0x48, 0xe8 } };
832 || (offset + 12) > sec->size
833 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i
834 || bfd_get_32 (abfd, contents + offset + 4) != call.i)
839 /* Check transition from LD access model. Only
840 leaq foo@tlsld(%rip), %rdi;
842 can transit to different access model. */
844 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
847 if (offset < 3 || (offset + 9) > sec->size)
850 op.i = bfd_get_32 (abfd, contents + offset - 3);
851 op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
856 r_symndx = ELF64_R_SYM (rel[1].r_info);
857 if (r_symndx < symtab_hdr->sh_info)
860 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
861 /* Use strncmp to check __tls_get_addr since __tls_get_addr
864 && h->root.root.string != NULL
865 && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
866 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
867 && (strncmp (h->root.root.string,
868 "__tls_get_addr", 14) == 0));
870 case R_X86_64_GOTTPOFF:
871 /* Check transition from IE access model:
872 movq foo@gottpoff(%rip), %reg
873 addq foo@gottpoff(%rip), %reg
876 if (offset < 3 || (offset + 4) > sec->size)
879 val = bfd_get_8 (abfd, contents + offset - 3);
880 if (val != 0x48 && val != 0x4c)
883 val = bfd_get_8 (abfd, contents + offset - 2);
884 if (val != 0x8b && val != 0x03)
887 val = bfd_get_8 (abfd, contents + offset - 1);
888 return (val & 0xc7) == 5;
890 case R_X86_64_GOTPC32_TLSDESC:
891 /* Check transition from GDesc access model:
892 leaq x@tlsdesc(%rip), %rax
894 Make sure it's a leaq adding rip to a 32-bit offset
895 into any register, although it's probably almost always
898 if (offset < 3 || (offset + 4) > sec->size)
901 val = bfd_get_8 (abfd, contents + offset - 3);
902 if ((val & 0xfb) != 0x48)
905 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
908 val = bfd_get_8 (abfd, contents + offset - 1);
909 return (val & 0xc7) == 0x05;
911 case R_X86_64_TLSDESC_CALL:
912 /* Check transition from GDesc access model:
913 call *x@tlsdesc(%rax)
915 if (offset + 2 <= sec->size)
917 /* Make sure that it's a call *x@tlsdesc(%rax). */
918 static x86_64_opcode16 call = { { 0xff, 0x10 } };
919 return bfd_get_16 (abfd, contents + offset) == call.i;
929 /* Return TRUE if the TLS access transition is OK or no transition
930 will be performed. Update R_TYPE if there is a transition. */
933 elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
934 asection *sec, bfd_byte *contents,
935 Elf_Internal_Shdr *symtab_hdr,
936 struct elf_link_hash_entry **sym_hashes,
937 unsigned int *r_type, int tls_type,
938 const Elf_Internal_Rela *rel,
939 const Elf_Internal_Rela *relend,
940 struct elf_link_hash_entry *h)
942 unsigned int from_type = *r_type;
943 unsigned int to_type = from_type;
944 bfd_boolean check = TRUE;
949 case R_X86_64_GOTPC32_TLSDESC:
950 case R_X86_64_TLSDESC_CALL:
951 case R_X86_64_GOTTPOFF:
955 to_type = R_X86_64_TPOFF32;
957 to_type = R_X86_64_GOTTPOFF;
960 /* When we are called from elf64_x86_64_relocate_section,
961 CONTENTS isn't NULL and there may be additional transitions
962 based on TLS_TYPE. */
963 if (contents != NULL)
965 unsigned int new_to_type = to_type;
970 && tls_type == GOT_TLS_IE)
971 new_to_type = R_X86_64_TPOFF32;
973 if (to_type == R_X86_64_TLSGD
974 || to_type == R_X86_64_GOTPC32_TLSDESC
975 || to_type == R_X86_64_TLSDESC_CALL)
977 if (tls_type == GOT_TLS_IE)
978 new_to_type = R_X86_64_GOTTPOFF;
981 /* We checked the transition before when we were called from
982 elf64_x86_64_check_relocs. We only want to check the new
983 transition which hasn't been checked before. */
984 check = new_to_type != to_type && from_type == to_type;
985 to_type = new_to_type;
992 to_type = R_X86_64_TPOFF32;
999 /* Return TRUE if there is no transition. */
1000 if (from_type == to_type)
1003 /* Check if the transition can be performed. */
1005 && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
1006 symtab_hdr, sym_hashes,
1007 from_type, rel, relend))
1009 reloc_howto_type *from, *to;
1011 from = elf64_x86_64_rtype_to_howto (abfd, from_type);
1012 to = elf64_x86_64_rtype_to_howto (abfd, to_type);
1014 (*_bfd_error_handler)
1015 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1016 "in section `%A' failed"),
1017 abfd, sec, from->name, to->name,
1018 h ? h->root.root.string : "a local symbol",
1019 (unsigned long) rel->r_offset);
1020 bfd_set_error (bfd_error_bad_value);
1028 /* Look through the relocs for a section during the first phase, and
1029 calculate needed space in the global offset table, procedure
1030 linkage table, and dynamic reloc sections. */
1033 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1035 const Elf_Internal_Rela *relocs)
1037 struct elf64_x86_64_link_hash_table *htab;
1038 Elf_Internal_Shdr *symtab_hdr;
1039 struct elf_link_hash_entry **sym_hashes;
1040 const Elf_Internal_Rela *rel;
1041 const Elf_Internal_Rela *rel_end;
1043 Elf_Internal_Sym *isymbuf;
1045 if (info->relocatable)
1048 BFD_ASSERT (is_x86_64_elf (abfd));
1050 htab = elf64_x86_64_hash_table (info);
1051 symtab_hdr = &elf_symtab_hdr (abfd);
1052 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1053 sym_hashes = elf_sym_hashes (abfd);
1057 rel_end = relocs + sec->reloc_count;
1058 for (rel = relocs; rel < rel_end; rel++)
1060 unsigned int r_type;
1061 unsigned long r_symndx;
1062 struct elf_link_hash_entry *h;
1064 r_symndx = ELF64_R_SYM (rel->r_info);
1065 r_type = ELF64_R_TYPE (rel->r_info);
1067 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1069 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1074 if (r_symndx < symtab_hdr->sh_info)
1076 /* A local symbol. */
1077 Elf_Internal_Sym *isym;
1079 /* Read this BFD's local symbols. */
1080 if (isymbuf == NULL)
1082 if (isymbuf == NULL)
1083 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1084 symtab_hdr->sh_info, 0,
1086 if (isymbuf == NULL)
1090 /* Check relocation against local STT_GNU_IFUNC symbol. */
1091 isym = isymbuf + r_symndx;
1092 if (ELF64_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1094 h = elf64_x86_64_get_local_sym_hash (htab, abfd, rel,
1099 /* Fake a STT_GNU_IFUNC symbol. */
1100 h->type = STT_GNU_IFUNC;
1103 h->forced_local = 1;
1104 h->root.type = bfd_link_hash_defined;
1111 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1112 while (h->root.type == bfd_link_hash_indirect
1113 || h->root.type == bfd_link_hash_warning)
1114 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1119 /* Create the ifunc sections for static executables. If we
1120 never see an indirect function symbol nor we are building
1121 a static executable, those sections will be empty and
1122 won't appear in output. */
1133 case R_X86_64_PLT32:
1134 case R_X86_64_GOTPCREL:
1135 case R_X86_64_GOTPCREL64:
1136 if (!_bfd_elf_create_ifunc_sections (abfd, info))
1141 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1142 it here if it is defined in a non-shared object. */
1143 if (h->type == STT_GNU_IFUNC
1146 /* It is referenced by a non-shared object. */
1149 /* STT_GNU_IFUNC symbol must go through PLT. */
1150 h->plt.refcount += 1;
1152 /* STT_GNU_IFUNC needs dynamic sections. */
1153 if (htab->elf.dynobj == NULL)
1154 htab->elf.dynobj = abfd;
1159 (*_bfd_error_handler)
1160 (_("%B: relocation %s against STT_GNU_IFUNC "
1161 "symbol `%s' isn't handled by %s"), abfd,
1162 x86_64_elf_howto_table[r_type].name,
1163 (h->root.root.string
1164 ? h->root.root.string : "a local symbol"),
1166 bfd_set_error (bfd_error_bad_value);
1171 h->pointer_equality_needed = 1;
1174 /* We must copy these reloc types into the output
1175 file. Create a reloc section in dynobj and
1176 make room for this reloc. */
1177 sreloc = _bfd_elf_create_ifunc_dyn_reloc
1178 (abfd, info, sec, sreloc,
1179 &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs);
1190 if (r_type != R_X86_64_PC32
1191 && r_type != R_X86_64_PC64)
1192 h->pointer_equality_needed = 1;
1195 case R_X86_64_PLT32:
1198 case R_X86_64_GOTPCREL:
1199 case R_X86_64_GOTPCREL64:
1200 h->got.refcount += 1;
1201 if (htab->elf.sgot == NULL
1202 && !_bfd_elf_create_got_section (htab->elf.dynobj,
1212 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1213 symtab_hdr, sym_hashes,
1214 &r_type, GOT_UNKNOWN,
1220 case R_X86_64_TLSLD:
1221 htab->tls_ld_got.refcount += 1;
1224 case R_X86_64_TPOFF32:
1227 (*_bfd_error_handler)
1228 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1230 x86_64_elf_howto_table[r_type].name,
1231 (h) ? h->root.root.string : "a local symbol");
1232 bfd_set_error (bfd_error_bad_value);
1237 case R_X86_64_GOTTPOFF:
1239 info->flags |= DF_STATIC_TLS;
1242 case R_X86_64_GOT32:
1243 case R_X86_64_GOTPCREL:
1244 case R_X86_64_TLSGD:
1245 case R_X86_64_GOT64:
1246 case R_X86_64_GOTPCREL64:
1247 case R_X86_64_GOTPLT64:
1248 case R_X86_64_GOTPC32_TLSDESC:
1249 case R_X86_64_TLSDESC_CALL:
1250 /* This symbol requires a global offset table entry. */
1252 int tls_type, old_tls_type;
1256 default: tls_type = GOT_NORMAL; break;
1257 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1258 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1259 case R_X86_64_GOTPC32_TLSDESC:
1260 case R_X86_64_TLSDESC_CALL:
1261 tls_type = GOT_TLS_GDESC; break;
1266 if (r_type == R_X86_64_GOTPLT64)
1268 /* This relocation indicates that we also need
1269 a PLT entry, as this is a function. We don't need
1270 a PLT entry for local symbols. */
1272 h->plt.refcount += 1;
1274 h->got.refcount += 1;
1275 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1279 bfd_signed_vma *local_got_refcounts;
1281 /* This is a global offset table entry for a local symbol. */
1282 local_got_refcounts = elf_local_got_refcounts (abfd);
1283 if (local_got_refcounts == NULL)
1287 size = symtab_hdr->sh_info;
1288 size *= sizeof (bfd_signed_vma)
1289 + sizeof (bfd_vma) + sizeof (char);
1290 local_got_refcounts = ((bfd_signed_vma *)
1291 bfd_zalloc (abfd, size));
1292 if (local_got_refcounts == NULL)
1294 elf_local_got_refcounts (abfd) = local_got_refcounts;
1295 elf64_x86_64_local_tlsdesc_gotent (abfd)
1296 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1297 elf64_x86_64_local_got_tls_type (abfd)
1298 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1300 local_got_refcounts[r_symndx] += 1;
1302 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1305 /* If a TLS symbol is accessed using IE at least once,
1306 there is no point to use dynamic model for it. */
1307 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1308 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1309 || tls_type != GOT_TLS_IE))
1311 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1312 tls_type = old_tls_type;
1313 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1314 && GOT_TLS_GD_ANY_P (tls_type))
1315 tls_type |= old_tls_type;
1318 (*_bfd_error_handler)
1319 (_("%B: '%s' accessed both as normal and thread local symbol"),
1320 abfd, h ? h->root.root.string : "<local>");
1325 if (old_tls_type != tls_type)
1328 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1330 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1335 case R_X86_64_GOTOFF64:
1336 case R_X86_64_GOTPC32:
1337 case R_X86_64_GOTPC64:
1339 if (htab->elf.sgot == NULL)
1341 if (htab->elf.dynobj == NULL)
1342 htab->elf.dynobj = abfd;
1343 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1349 case R_X86_64_PLT32:
1350 /* This symbol requires a procedure linkage table entry. We
1351 actually build the entry in adjust_dynamic_symbol,
1352 because this might be a case of linking PIC code which is
1353 never referenced by a dynamic object, in which case we
1354 don't need to generate a procedure linkage table entry
1357 /* If this is a local symbol, we resolve it directly without
1358 creating a procedure linkage table entry. */
1363 h->plt.refcount += 1;
1366 case R_X86_64_PLTOFF64:
1367 /* This tries to form the 'address' of a function relative
1368 to GOT. For global symbols we need a PLT entry. */
1372 h->plt.refcount += 1;
1380 /* Let's help debug shared library creation. These relocs
1381 cannot be used in shared libs. Don't error out for
1382 sections we don't care about, such as debug sections or
1383 non-constant sections. */
1385 && (sec->flags & SEC_ALLOC) != 0
1386 && (sec->flags & SEC_READONLY) != 0)
1388 (*_bfd_error_handler)
1389 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1391 x86_64_elf_howto_table[r_type].name,
1392 (h) ? h->root.root.string : "a local symbol");
1393 bfd_set_error (bfd_error_bad_value);
1403 if (h != NULL && info->executable)
1405 /* If this reloc is in a read-only section, we might
1406 need a copy reloc. We can't check reliably at this
1407 stage whether the section is read-only, as input
1408 sections have not yet been mapped to output sections.
1409 Tentatively set the flag for now, and correct in
1410 adjust_dynamic_symbol. */
1413 /* We may need a .plt entry if the function this reloc
1414 refers to is in a shared lib. */
1415 h->plt.refcount += 1;
1416 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1417 h->pointer_equality_needed = 1;
1420 /* If we are creating a shared library, and this is a reloc
1421 against a global symbol, or a non PC relative reloc
1422 against a local symbol, then we need to copy the reloc
1423 into the shared library. However, if we are linking with
1424 -Bsymbolic, we do not need to copy a reloc against a
1425 global symbol which is defined in an object we are
1426 including in the link (i.e., DEF_REGULAR is set). At
1427 this point we have not seen all the input files, so it is
1428 possible that DEF_REGULAR is not set now but will be set
1429 later (it is never cleared). In case of a weak definition,
1430 DEF_REGULAR may be cleared later by a strong definition in
1431 a shared library. We account for that possibility below by
1432 storing information in the relocs_copied field of the hash
1433 table entry. A similar situation occurs when creating
1434 shared libraries and symbol visibility changes render the
1437 If on the other hand, we are creating an executable, we
1438 may need to keep relocations for symbols satisfied by a
1439 dynamic library if we manage to avoid copy relocs for the
1442 && (sec->flags & SEC_ALLOC) != 0
1443 && (! IS_X86_64_PCREL_TYPE (r_type)
1445 && (! SYMBOLIC_BIND (info, h)
1446 || h->root.type == bfd_link_hash_defweak
1447 || !h->def_regular))))
1448 || (ELIMINATE_COPY_RELOCS
1450 && (sec->flags & SEC_ALLOC) != 0
1452 && (h->root.type == bfd_link_hash_defweak
1453 || !h->def_regular)))
1455 struct elf_dyn_relocs *p;
1456 struct elf_dyn_relocs **head;
1458 /* We must copy these reloc types into the output file.
1459 Create a reloc section in dynobj and make room for
1463 if (htab->elf.dynobj == NULL)
1464 htab->elf.dynobj = abfd;
1466 sreloc = _bfd_elf_make_dynamic_reloc_section
1467 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
1473 /* If this is a global symbol, we count the number of
1474 relocations we need for this symbol. */
1477 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1482 /* Track dynamic relocs needed for local syms too.
1483 We really need local syms available to do this
1487 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1492 /* Beware of type punned pointers vs strict aliasing
1494 vpp = &(elf_section_data (s)->local_dynrel);
1495 head = (struct elf_dyn_relocs **)vpp;
1499 if (p == NULL || p->sec != sec)
1501 bfd_size_type amt = sizeof *p;
1503 p = ((struct elf_dyn_relocs *)
1504 bfd_alloc (htab->elf.dynobj, amt));
1515 if (IS_X86_64_PCREL_TYPE (r_type))
1520 /* This relocation describes the C++ object vtable hierarchy.
1521 Reconstruct it for later use during GC. */
1522 case R_X86_64_GNU_VTINHERIT:
1523 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1527 /* This relocation describes which C++ vtable entries are actually
1528 used. Record for later use during GC. */
1529 case R_X86_64_GNU_VTENTRY:
1530 BFD_ASSERT (h != NULL);
1532 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1542 && (unsigned char *) isymbuf != symtab_hdr->contents)
1544 if (!info->keep_memory)
1548 /* Cache the symbols for elf_link_input_bfd. */
1549 symtab_hdr->contents = (unsigned char *) isymbuf;
1557 && (unsigned char *) isymbuf != symtab_hdr->contents)
1562 /* Return the section that should be marked against GC for a given
1566 elf64_x86_64_gc_mark_hook (asection *sec,
1567 struct bfd_link_info *info,
1568 Elf_Internal_Rela *rel,
1569 struct elf_link_hash_entry *h,
1570 Elf_Internal_Sym *sym)
1573 switch (ELF64_R_TYPE (rel->r_info))
1575 case R_X86_64_GNU_VTINHERIT:
1576 case R_X86_64_GNU_VTENTRY:
1580 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1583 /* Update the got entry reference counts for the section being removed. */
1586 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1588 const Elf_Internal_Rela *relocs)
1590 Elf_Internal_Shdr *symtab_hdr;
1591 struct elf_link_hash_entry **sym_hashes;
1592 bfd_signed_vma *local_got_refcounts;
1593 const Elf_Internal_Rela *rel, *relend;
1595 if (info->relocatable)
1598 elf_section_data (sec)->local_dynrel = NULL;
1600 symtab_hdr = &elf_symtab_hdr (abfd);
1601 sym_hashes = elf_sym_hashes (abfd);
1602 local_got_refcounts = elf_local_got_refcounts (abfd);
1604 relend = relocs + sec->reloc_count;
1605 for (rel = relocs; rel < relend; rel++)
1607 unsigned long r_symndx;
1608 unsigned int r_type;
1609 struct elf_link_hash_entry *h = NULL;
1611 r_symndx = ELF64_R_SYM (rel->r_info);
1612 if (r_symndx >= symtab_hdr->sh_info)
1614 struct elf64_x86_64_link_hash_entry *eh;
1615 struct elf_dyn_relocs **pp;
1616 struct elf_dyn_relocs *p;
1618 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1619 while (h->root.type == bfd_link_hash_indirect
1620 || h->root.type == bfd_link_hash_warning)
1621 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1622 eh = (struct elf64_x86_64_link_hash_entry *) h;
1624 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1627 /* Everything must go for SEC. */
1633 r_type = ELF64_R_TYPE (rel->r_info);
1634 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1635 symtab_hdr, sym_hashes,
1636 &r_type, GOT_UNKNOWN,
1642 case R_X86_64_TLSLD:
1643 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1644 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1647 case R_X86_64_TLSGD:
1648 case R_X86_64_GOTPC32_TLSDESC:
1649 case R_X86_64_TLSDESC_CALL:
1650 case R_X86_64_GOTTPOFF:
1651 case R_X86_64_GOT32:
1652 case R_X86_64_GOTPCREL:
1653 case R_X86_64_GOT64:
1654 case R_X86_64_GOTPCREL64:
1655 case R_X86_64_GOTPLT64:
1658 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1659 h->plt.refcount -= 1;
1660 if (h->got.refcount > 0)
1661 h->got.refcount -= 1;
1663 else if (local_got_refcounts != NULL)
1665 if (local_got_refcounts[r_symndx] > 0)
1666 local_got_refcounts[r_symndx] -= 1;
1683 case R_X86_64_PLT32:
1684 case R_X86_64_PLTOFF64:
1687 if (h->plt.refcount > 0)
1688 h->plt.refcount -= 1;
1700 /* Adjust a symbol defined by a dynamic object and referenced by a
1701 regular object. The current definition is in some section of the
1702 dynamic object, but we're not including those sections. We have to
1703 change the definition to something the rest of the link can
1707 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1708 struct elf_link_hash_entry *h)
1710 struct elf64_x86_64_link_hash_table *htab;
1713 /* STT_GNU_IFUNC symbol must go through PLT. */
1714 if (h->type == STT_GNU_IFUNC)
1716 if (h->plt.refcount <= 0)
1718 h->plt.offset = (bfd_vma) -1;
1724 /* If this is a function, put it in the procedure linkage table. We
1725 will fill in the contents of the procedure linkage table later,
1726 when we know the address of the .got section. */
1727 if (h->type == STT_FUNC
1730 if (h->plt.refcount <= 0
1731 || SYMBOL_CALLS_LOCAL (info, h)
1732 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1733 && h->root.type == bfd_link_hash_undefweak))
1735 /* This case can occur if we saw a PLT32 reloc in an input
1736 file, but the symbol was never referred to by a dynamic
1737 object, or if all references were garbage collected. In
1738 such a case, we don't actually need to build a procedure
1739 linkage table, and we can just do a PC32 reloc instead. */
1740 h->plt.offset = (bfd_vma) -1;
1747 /* It's possible that we incorrectly decided a .plt reloc was
1748 needed for an R_X86_64_PC32 reloc to a non-function sym in
1749 check_relocs. We can't decide accurately between function and
1750 non-function syms in check-relocs; Objects loaded later in
1751 the link may change h->type. So fix it now. */
1752 h->plt.offset = (bfd_vma) -1;
1754 /* If this is a weak symbol, and there is a real definition, the
1755 processor independent code will have arranged for us to see the
1756 real definition first, and we can just use the same value. */
1757 if (h->u.weakdef != NULL)
1759 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1760 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1761 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1762 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1763 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1764 h->non_got_ref = h->u.weakdef->non_got_ref;
1768 /* This is a reference to a symbol defined by a dynamic object which
1769 is not a function. */
1771 /* If we are creating a shared library, we must presume that the
1772 only references to the symbol are via the global offset table.
1773 For such cases we need not do anything here; the relocations will
1774 be handled correctly by relocate_section. */
1778 /* If there are no references to this symbol that do not use the
1779 GOT, we don't need to generate a copy reloc. */
1780 if (!h->non_got_ref)
1783 /* If -z nocopyreloc was given, we won't generate them either. */
1784 if (info->nocopyreloc)
1790 if (ELIMINATE_COPY_RELOCS)
1792 struct elf64_x86_64_link_hash_entry * eh;
1793 struct elf_dyn_relocs *p;
1795 eh = (struct elf64_x86_64_link_hash_entry *) h;
1796 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1798 s = p->sec->output_section;
1799 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1803 /* If we didn't find any dynamic relocs in read-only sections, then
1804 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1814 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1815 h->root.root.string);
1819 /* We must allocate the symbol in our .dynbss section, which will
1820 become part of the .bss section of the executable. There will be
1821 an entry for this symbol in the .dynsym section. The dynamic
1822 object will contain position independent code, so all references
1823 from the dynamic object to this symbol will go through the global
1824 offset table. The dynamic linker will use the .dynsym entry to
1825 determine the address it must put in the global offset table, so
1826 both the dynamic object and the regular object will refer to the
1827 same memory location for the variable. */
1829 htab = elf64_x86_64_hash_table (info);
1831 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1832 to copy the initial value out of the dynamic object and into the
1833 runtime process image. */
1834 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1836 htab->srelbss->size += sizeof (Elf64_External_Rela);
1842 return _bfd_elf_adjust_dynamic_copy (h, s);
1845 /* Allocate space in .plt, .got and associated reloc sections for
1849 elf64_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1851 struct bfd_link_info *info;
1852 struct elf64_x86_64_link_hash_table *htab;
1853 struct elf64_x86_64_link_hash_entry *eh;
1854 struct elf_dyn_relocs *p;
1856 if (h->root.type == bfd_link_hash_indirect)
1859 if (h->root.type == bfd_link_hash_warning)
1860 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1861 eh = (struct elf64_x86_64_link_hash_entry *) h;
1863 info = (struct bfd_link_info *) inf;
1864 htab = elf64_x86_64_hash_table (info);
1866 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1867 here if it is defined and referenced in a non-shared object. */
1868 if (h->type == STT_GNU_IFUNC
1870 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
1874 else if (htab->elf.dynamic_sections_created
1875 && h->plt.refcount > 0)
1877 /* Make sure this symbol is output as a dynamic symbol.
1878 Undefined weak syms won't yet be marked as dynamic. */
1879 if (h->dynindx == -1
1880 && !h->forced_local)
1882 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1887 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1889 asection *s = htab->elf.splt;
1891 /* If this is the first .plt entry, make room for the special
1894 s->size += PLT_ENTRY_SIZE;
1896 h->plt.offset = s->size;
1898 /* If this symbol is not defined in a regular file, and we are
1899 not generating a shared library, then set the symbol to this
1900 location in the .plt. This is required to make function
1901 pointers compare as equal between the normal executable and
1902 the shared library. */
1906 h->root.u.def.section = s;
1907 h->root.u.def.value = h->plt.offset;
1910 /* Make room for this entry. */
1911 s->size += PLT_ENTRY_SIZE;
1913 /* We also need to make an entry in the .got.plt section, which
1914 will be placed in the .got section by the linker script. */
1915 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1917 /* We also need to make an entry in the .rela.plt section. */
1918 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1919 htab->elf.srelplt->reloc_count++;
1923 h->plt.offset = (bfd_vma) -1;
1929 h->plt.offset = (bfd_vma) -1;
1933 eh->tlsdesc_got = (bfd_vma) -1;
1935 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1936 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1937 if (h->got.refcount > 0
1940 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1942 h->got.offset = (bfd_vma) -1;
1944 else if (h->got.refcount > 0)
1948 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1950 /* Make sure this symbol is output as a dynamic symbol.
1951 Undefined weak syms won't yet be marked as dynamic. */
1952 if (h->dynindx == -1
1953 && !h->forced_local)
1955 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1959 if (GOT_TLS_GDESC_P (tls_type))
1961 eh->tlsdesc_got = htab->elf.sgotplt->size
1962 - elf64_x86_64_compute_jump_table_size (htab);
1963 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
1964 h->got.offset = (bfd_vma) -2;
1966 if (! GOT_TLS_GDESC_P (tls_type)
1967 || GOT_TLS_GD_P (tls_type))
1970 h->got.offset = s->size;
1971 s->size += GOT_ENTRY_SIZE;
1972 if (GOT_TLS_GD_P (tls_type))
1973 s->size += GOT_ENTRY_SIZE;
1975 dyn = htab->elf.dynamic_sections_created;
1976 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1978 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1979 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1980 || tls_type == GOT_TLS_IE)
1981 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1982 else if (GOT_TLS_GD_P (tls_type))
1983 htab->elf.srelgot->size += 2 * sizeof (Elf64_External_Rela);
1984 else if (! GOT_TLS_GDESC_P (tls_type)
1985 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1986 || h->root.type != bfd_link_hash_undefweak)
1988 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1989 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1990 if (GOT_TLS_GDESC_P (tls_type))
1992 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1993 htab->tlsdesc_plt = (bfd_vma) -1;
1997 h->got.offset = (bfd_vma) -1;
1999 if (eh->dyn_relocs == NULL)
2002 /* In the shared -Bsymbolic case, discard space allocated for
2003 dynamic pc-relative relocs against symbols which turn out to be
2004 defined in regular objects. For the normal shared case, discard
2005 space for pc-relative relocs that have become local due to symbol
2006 visibility changes. */
2010 /* Relocs that use pc_count are those that appear on a call
2011 insn, or certain REL relocs that can generated via assembly.
2012 We want calls to protected symbols to resolve directly to the
2013 function rather than going via the plt. If people want
2014 function pointer comparisons to work as expected then they
2015 should avoid writing weird assembly. */
2016 if (SYMBOL_CALLS_LOCAL (info, h))
2018 struct elf_dyn_relocs **pp;
2020 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2022 p->count -= p->pc_count;
2031 /* Also discard relocs on undefined weak syms with non-default
2033 if (eh->dyn_relocs != NULL
2034 && h->root.type == bfd_link_hash_undefweak)
2036 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2037 eh->dyn_relocs = NULL;
2039 /* Make sure undefined weak symbols are output as a dynamic
2041 else if (h->dynindx == -1
2042 && ! h->forced_local
2043 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2048 else if (ELIMINATE_COPY_RELOCS)
2050 /* For the non-shared case, discard space for relocs against
2051 symbols which turn out to need copy relocs or are not
2057 || (htab->elf.dynamic_sections_created
2058 && (h->root.type == bfd_link_hash_undefweak
2059 || h->root.type == bfd_link_hash_undefined))))
2061 /* Make sure this symbol is output as a dynamic symbol.
2062 Undefined weak syms won't yet be marked as dynamic. */
2063 if (h->dynindx == -1
2064 && ! h->forced_local
2065 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2068 /* If that succeeded, we know we'll be keeping all the
2070 if (h->dynindx != -1)
2074 eh->dyn_relocs = NULL;
2079 /* Finally, allocate space. */
2080 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2084 sreloc = elf_section_data (p->sec)->sreloc;
2086 BFD_ASSERT (sreloc != NULL);
2088 sreloc->size += p->count * sizeof (Elf64_External_Rela);
2094 /* Allocate space in .plt, .got and associated reloc sections for
2095 local dynamic relocs. */
2098 elf64_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
2100 struct elf_link_hash_entry *h
2101 = (struct elf_link_hash_entry *) *slot;
2103 if (h->type != STT_GNU_IFUNC
2107 || h->root.type != bfd_link_hash_defined)
2110 return elf64_x86_64_allocate_dynrelocs (h, inf);
2113 /* Find any dynamic relocs that apply to read-only sections. */
2116 elf64_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2118 struct elf64_x86_64_link_hash_entry *eh;
2119 struct elf_dyn_relocs *p;
2121 if (h->root.type == bfd_link_hash_warning)
2122 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2124 eh = (struct elf64_x86_64_link_hash_entry *) h;
2125 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2127 asection *s = p->sec->output_section;
2129 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2131 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2133 info->flags |= DF_TEXTREL;
2135 /* Not an error, just cut short the traversal. */
2142 /* Set the sizes of the dynamic sections. */
2145 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2146 struct bfd_link_info *info)
2148 struct elf64_x86_64_link_hash_table *htab;
2154 htab = elf64_x86_64_hash_table (info);
2155 dynobj = htab->elf.dynobj;
2159 if (htab->elf.dynamic_sections_created)
2161 /* Set the contents of the .interp section to the interpreter. */
2162 if (info->executable)
2164 s = bfd_get_section_by_name (dynobj, ".interp");
2167 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2168 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2172 /* Set up .got offsets for local syms, and space for local dynamic
2174 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2176 bfd_signed_vma *local_got;
2177 bfd_signed_vma *end_local_got;
2178 char *local_tls_type;
2179 bfd_vma *local_tlsdesc_gotent;
2180 bfd_size_type locsymcount;
2181 Elf_Internal_Shdr *symtab_hdr;
2184 if (! is_x86_64_elf (ibfd))
2187 for (s = ibfd->sections; s != NULL; s = s->next)
2189 struct elf_dyn_relocs *p;
2191 for (p = (struct elf_dyn_relocs *)
2192 (elf_section_data (s)->local_dynrel);
2196 if (!bfd_is_abs_section (p->sec)
2197 && bfd_is_abs_section (p->sec->output_section))
2199 /* Input section has been discarded, either because
2200 it is a copy of a linkonce section or due to
2201 linker script /DISCARD/, so we'll be discarding
2204 else if (p->count != 0)
2206 srel = elf_section_data (p->sec)->sreloc;
2207 srel->size += p->count * sizeof (Elf64_External_Rela);
2208 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2209 info->flags |= DF_TEXTREL;
2214 local_got = elf_local_got_refcounts (ibfd);
2218 symtab_hdr = &elf_symtab_hdr (ibfd);
2219 locsymcount = symtab_hdr->sh_info;
2220 end_local_got = local_got + locsymcount;
2221 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
2222 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
2224 srel = htab->elf.srelgot;
2225 for (; local_got < end_local_got;
2226 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2228 *local_tlsdesc_gotent = (bfd_vma) -1;
2231 if (GOT_TLS_GDESC_P (*local_tls_type))
2233 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2234 - elf64_x86_64_compute_jump_table_size (htab);
2235 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2236 *local_got = (bfd_vma) -2;
2238 if (! GOT_TLS_GDESC_P (*local_tls_type)
2239 || GOT_TLS_GD_P (*local_tls_type))
2241 *local_got = s->size;
2242 s->size += GOT_ENTRY_SIZE;
2243 if (GOT_TLS_GD_P (*local_tls_type))
2244 s->size += GOT_ENTRY_SIZE;
2247 || GOT_TLS_GD_ANY_P (*local_tls_type)
2248 || *local_tls_type == GOT_TLS_IE)
2250 if (GOT_TLS_GDESC_P (*local_tls_type))
2252 htab->elf.srelplt->size
2253 += sizeof (Elf64_External_Rela);
2254 htab->tlsdesc_plt = (bfd_vma) -1;
2256 if (! GOT_TLS_GDESC_P (*local_tls_type)
2257 || GOT_TLS_GD_P (*local_tls_type))
2258 srel->size += sizeof (Elf64_External_Rela);
2262 *local_got = (bfd_vma) -1;
2266 if (htab->tls_ld_got.refcount > 0)
2268 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2270 htab->tls_ld_got.offset = htab->elf.sgot->size;
2271 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
2272 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2275 htab->tls_ld_got.offset = -1;
2277 /* Allocate global sym .plt and .got entries, and space for global
2278 sym dynamic relocs. */
2279 elf_link_hash_traverse (&htab->elf, elf64_x86_64_allocate_dynrelocs,
2282 /* Allocate .plt and .got entries, and space for local symbols. */
2283 htab_traverse (htab->loc_hash_table,
2284 elf64_x86_64_allocate_local_dynrelocs,
2287 /* For every jump slot reserved in the sgotplt, reloc_count is
2288 incremented. However, when we reserve space for TLS descriptors,
2289 it's not incremented, so in order to compute the space reserved
2290 for them, it suffices to multiply the reloc count by the jump
2292 if (htab->elf.srelplt)
2293 htab->sgotplt_jump_table_size
2294 = elf64_x86_64_compute_jump_table_size (htab);
2296 if (htab->tlsdesc_plt)
2298 /* If we're not using lazy TLS relocations, don't generate the
2299 PLT and GOT entries they require. */
2300 if ((info->flags & DF_BIND_NOW))
2301 htab->tlsdesc_plt = 0;
2304 htab->tlsdesc_got = htab->elf.sgot->size;
2305 htab->elf.sgot->size += GOT_ENTRY_SIZE;
2306 /* Reserve room for the initial entry.
2307 FIXME: we could probably do away with it in this case. */
2308 if (htab->elf.splt->size == 0)
2309 htab->elf.splt->size += PLT_ENTRY_SIZE;
2310 htab->tlsdesc_plt = htab->elf.splt->size;
2311 htab->elf.splt->size += PLT_ENTRY_SIZE;
2315 /* We now have determined the sizes of the various dynamic sections.
2316 Allocate memory for them. */
2318 for (s = dynobj->sections; s != NULL; s = s->next)
2320 if ((s->flags & SEC_LINKER_CREATED) == 0)
2323 if (s == htab->elf.splt
2324 || s == htab->elf.sgot
2325 || s == htab->elf.sgotplt
2326 || s == htab->elf.iplt
2327 || s == htab->elf.igotplt
2328 || s == htab->sdynbss)
2330 /* Strip this section if we don't need it; see the
2333 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2335 if (s->size != 0 && s != htab->elf.srelplt)
2338 /* We use the reloc_count field as a counter if we need
2339 to copy relocs into the output file. */
2340 if (s != htab->elf.srelplt)
2345 /* It's not one of our sections, so don't allocate space. */
2351 /* If we don't need this section, strip it from the
2352 output file. This is mostly to handle .rela.bss and
2353 .rela.plt. We must create both sections in
2354 create_dynamic_sections, because they must be created
2355 before the linker maps input sections to output
2356 sections. The linker does that before
2357 adjust_dynamic_symbol is called, and it is that
2358 function which decides whether anything needs to go
2359 into these sections. */
2361 s->flags |= SEC_EXCLUDE;
2365 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2368 /* Allocate memory for the section contents. We use bfd_zalloc
2369 here in case unused entries are not reclaimed before the
2370 section's contents are written out. This should not happen,
2371 but this way if it does, we get a R_X86_64_NONE reloc instead
2373 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2374 if (s->contents == NULL)
2378 if (htab->elf.dynamic_sections_created)
2380 /* Add some entries to the .dynamic section. We fill in the
2381 values later, in elf64_x86_64_finish_dynamic_sections, but we
2382 must add the entries now so that we get the correct size for
2383 the .dynamic section. The DT_DEBUG entry is filled in by the
2384 dynamic linker and used by the debugger. */
2385 #define add_dynamic_entry(TAG, VAL) \
2386 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2388 if (info->executable)
2390 if (!add_dynamic_entry (DT_DEBUG, 0))
2394 if (htab->elf.splt->size != 0)
2396 if (!add_dynamic_entry (DT_PLTGOT, 0)
2397 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2398 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2399 || !add_dynamic_entry (DT_JMPREL, 0))
2402 if (htab->tlsdesc_plt
2403 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2404 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2410 if (!add_dynamic_entry (DT_RELA, 0)
2411 || !add_dynamic_entry (DT_RELASZ, 0)
2412 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2415 /* If any dynamic relocs apply to a read-only section,
2416 then we need a DT_TEXTREL entry. */
2417 if ((info->flags & DF_TEXTREL) == 0)
2418 elf_link_hash_traverse (&htab->elf,
2419 elf64_x86_64_readonly_dynrelocs,
2422 if ((info->flags & DF_TEXTREL) != 0)
2424 if (!add_dynamic_entry (DT_TEXTREL, 0))
2429 #undef add_dynamic_entry
2435 elf64_x86_64_always_size_sections (bfd *output_bfd,
2436 struct bfd_link_info *info)
2438 asection *tls_sec = elf_hash_table (info)->tls_sec;
2442 struct elf_link_hash_entry *tlsbase;
2444 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2445 "_TLS_MODULE_BASE_",
2446 FALSE, FALSE, FALSE);
2448 if (tlsbase && tlsbase->type == STT_TLS)
2450 struct bfd_link_hash_entry *bh = NULL;
2451 const struct elf_backend_data *bed
2452 = get_elf_backend_data (output_bfd);
2454 if (!(_bfd_generic_link_add_one_symbol
2455 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2456 tls_sec, 0, NULL, FALSE,
2457 bed->collect, &bh)))
2460 elf64_x86_64_hash_table (info)->tls_module_base = bh;
2462 tlsbase = (struct elf_link_hash_entry *)bh;
2463 tlsbase->def_regular = 1;
2464 tlsbase->other = STV_HIDDEN;
2465 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2472 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2473 executables. Rather than setting it to the beginning of the TLS
2474 section, we have to set it to the end. This function may be called
2475 multiple times, it is idempotent. */
2478 elf64_x86_64_set_tls_module_base (struct bfd_link_info *info)
2480 struct bfd_link_hash_entry *base;
2482 if (!info->executable)
2485 base = elf64_x86_64_hash_table (info)->tls_module_base;
2490 base->u.def.value = elf_hash_table (info)->tls_size;
2493 /* Return the base VMA address which should be subtracted from real addresses
2494 when resolving @dtpoff relocation.
2495 This is PT_TLS segment p_vaddr. */
2498 elf64_x86_64_dtpoff_base (struct bfd_link_info *info)
2500 /* If tls_sec is NULL, we should have signalled an error already. */
2501 if (elf_hash_table (info)->tls_sec == NULL)
2503 return elf_hash_table (info)->tls_sec->vma;
2506 /* Return the relocation value for @tpoff relocation
2507 if STT_TLS virtual address is ADDRESS. */
2510 elf64_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2512 struct elf_link_hash_table *htab = elf_hash_table (info);
2514 /* If tls_segment is NULL, we should have signalled an error already. */
2515 if (htab->tls_sec == NULL)
2517 return address - htab->tls_size - htab->tls_sec->vma;
2520 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2524 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2526 /* Opcode Instruction
2529 0x0f 0x8x conditional jump */
2531 && (contents [offset - 1] == 0xe8
2532 || contents [offset - 1] == 0xe9))
2534 && contents [offset - 2] == 0x0f
2535 && (contents [offset - 1] & 0xf0) == 0x80));
2538 /* Relocate an x86_64 ELF section. */
2541 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2542 bfd *input_bfd, asection *input_section,
2543 bfd_byte *contents, Elf_Internal_Rela *relocs,
2544 Elf_Internal_Sym *local_syms,
2545 asection **local_sections)
2547 struct elf64_x86_64_link_hash_table *htab;
2548 Elf_Internal_Shdr *symtab_hdr;
2549 struct elf_link_hash_entry **sym_hashes;
2550 bfd_vma *local_got_offsets;
2551 bfd_vma *local_tlsdesc_gotents;
2552 Elf_Internal_Rela *rel;
2553 Elf_Internal_Rela *relend;
2555 BFD_ASSERT (is_x86_64_elf (input_bfd));
2557 htab = elf64_x86_64_hash_table (info);
2558 symtab_hdr = &elf_symtab_hdr (input_bfd);
2559 sym_hashes = elf_sym_hashes (input_bfd);
2560 local_got_offsets = elf_local_got_offsets (input_bfd);
2561 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2563 elf64_x86_64_set_tls_module_base (info);
2566 relend = relocs + input_section->reloc_count;
2567 for (; rel < relend; rel++)
2569 unsigned int r_type;
2570 reloc_howto_type *howto;
2571 unsigned long r_symndx;
2572 struct elf_link_hash_entry *h;
2573 Elf_Internal_Sym *sym;
2575 bfd_vma off, offplt;
2577 bfd_boolean unresolved_reloc;
2578 bfd_reloc_status_type r;
2582 r_type = ELF64_R_TYPE (rel->r_info);
2583 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2584 || r_type == (int) R_X86_64_GNU_VTENTRY)
2587 if (r_type >= R_X86_64_max)
2589 bfd_set_error (bfd_error_bad_value);
2593 howto = x86_64_elf_howto_table + r_type;
2594 r_symndx = ELF64_R_SYM (rel->r_info);
2598 unresolved_reloc = FALSE;
2599 if (r_symndx < symtab_hdr->sh_info)
2601 sym = local_syms + r_symndx;
2602 sec = local_sections[r_symndx];
2604 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2607 /* Relocate against local STT_GNU_IFUNC symbol. */
2608 if (ELF64_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2610 h = elf64_x86_64_get_local_sym_hash (htab, input_bfd,
2615 /* Set STT_GNU_IFUNC symbol value. */
2616 h->root.u.def.value = sym->st_value;
2617 h->root.u.def.section = sec;
2624 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2625 r_symndx, symtab_hdr, sym_hashes,
2627 unresolved_reloc, warned);
2630 if (sec != NULL && elf_discarded_section (sec))
2632 /* For relocs against symbols from removed linkonce sections,
2633 or sections discarded by a linker script, we just want the
2634 section contents zeroed. Avoid any special processing. */
2635 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2641 if (info->relocatable)
2644 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2645 it here if it is defined in a non-shared object. */
2647 && h->type == STT_GNU_IFUNC
2653 if ((input_section->flags & SEC_ALLOC) == 0
2654 || h->plt.offset == (bfd_vma) -1)
2657 /* STT_GNU_IFUNC symbol must go through PLT. */
2658 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2659 relocation = (plt->output_section->vma
2660 + plt->output_offset + h->plt.offset);
2665 (*_bfd_error_handler)
2666 (_("%B: relocation %s against STT_GNU_IFUNC "
2667 "symbol `%s' isn't handled by %s"), input_bfd,
2668 x86_64_elf_howto_table[r_type].name,
2669 (h->root.root.string
2670 ? h->root.root.string : "a local symbol"),
2672 bfd_set_error (bfd_error_bad_value);
2681 if (rel->r_addend != 0)
2683 (*_bfd_error_handler)
2684 (_("%B: relocation %s against STT_GNU_IFUNC "
2685 "symbol `%s' has non-zero addend: %d"),
2686 input_bfd, x86_64_elf_howto_table[r_type].name,
2687 (h->root.root.string
2688 ? h->root.root.string : "a local symbol"),
2690 bfd_set_error (bfd_error_bad_value);
2694 /* Generate dynamic relcoation only when there is a
2695 non-GOF reference in a shared object. */
2696 if (info->shared && h->non_got_ref)
2698 Elf_Internal_Rela outrel;
2702 /* Need a dynamic relocation to get the real function
2704 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2708 if (outrel.r_offset == (bfd_vma) -1
2709 || outrel.r_offset == (bfd_vma) -2)
2712 outrel.r_offset += (input_section->output_section->vma
2713 + input_section->output_offset);
2715 if (h->dynindx == -1
2717 || info->executable)
2719 /* This symbol is resolved locally. */
2720 outrel.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
2721 outrel.r_addend = (h->root.u.def.value
2722 + h->root.u.def.section->output_section->vma
2723 + h->root.u.def.section->output_offset);
2727 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2728 outrel.r_addend = 0;
2731 sreloc = htab->elf.irelifunc;
2732 loc = sreloc->contents;
2733 loc += (sreloc->reloc_count++
2734 * sizeof (Elf64_External_Rela));
2735 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2737 /* If this reloc is against an external symbol, we
2738 do not want to fiddle with the addend. Otherwise,
2739 we need to include the symbol value so that it
2740 becomes an addend for the dynamic reloc. For an
2741 internal symbol, we have updated addend. */
2748 case R_X86_64_PLT32:
2751 case R_X86_64_GOTPCREL:
2752 case R_X86_64_GOTPCREL64:
2753 base_got = htab->elf.sgot;
2754 off = h->got.offset;
2756 if (base_got == NULL)
2759 if (off == (bfd_vma) -1)
2761 /* We can't use h->got.offset here to save state, or
2762 even just remember the offset, as finish_dynamic_symbol
2763 would use that as offset into .got. */
2765 if (htab->elf.splt != NULL)
2767 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2768 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2769 base_got = htab->elf.sgotplt;
2773 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2774 off = plt_index * GOT_ENTRY_SIZE;
2775 base_got = htab->elf.igotplt;
2778 if (h->dynindx == -1
2782 /* This references the local defitionion. We must
2783 initialize this entry in the global offset table.
2784 Since the offset must always be a multiple of 8,
2785 we use the least significant bit to record
2786 whether we have initialized it already.
2788 When doing a dynamic link, we create a .rela.got
2789 relocation entry to initialize the value. This
2790 is done in the finish_dynamic_symbol routine. */
2795 bfd_put_64 (output_bfd, relocation,
2796 base_got->contents + off);
2797 /* Note that this is harmless for the GOTPLT64
2798 case, as -1 | 1 still is -1. */
2804 relocation = (base_got->output_section->vma
2805 + base_got->output_offset + off);
2807 if (r_type != R_X86_64_GOTPCREL
2808 && r_type != R_X86_64_GOTPCREL64)
2811 if (htab->elf.splt != NULL)
2812 gotplt = htab->elf.sgotplt;
2814 gotplt = htab->elf.igotplt;
2815 relocation -= (gotplt->output_section->vma
2816 - gotplt->output_offset);
2823 /* When generating a shared object, the relocations handled here are
2824 copied into the output file to be resolved at run time. */
2827 case R_X86_64_GOT32:
2828 case R_X86_64_GOT64:
2829 /* Relocation is to the entry for this symbol in the global
2831 case R_X86_64_GOTPCREL:
2832 case R_X86_64_GOTPCREL64:
2833 /* Use global offset table entry as symbol value. */
2834 case R_X86_64_GOTPLT64:
2835 /* This is the same as GOT64 for relocation purposes, but
2836 indicates the existence of a PLT entry. The difficulty is,
2837 that we must calculate the GOT slot offset from the PLT
2838 offset, if this symbol got a PLT entry (it was global).
2839 Additionally if it's computed from the PLT entry, then that
2840 GOT offset is relative to .got.plt, not to .got. */
2841 base_got = htab->elf.sgot;
2843 if (htab->elf.sgot == NULL)
2850 off = h->got.offset;
2852 && h->plt.offset != (bfd_vma)-1
2853 && off == (bfd_vma)-1)
2855 /* We can't use h->got.offset here to save
2856 state, or even just remember the offset, as
2857 finish_dynamic_symbol would use that as offset into
2859 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2860 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2861 base_got = htab->elf.sgotplt;
2864 dyn = htab->elf.dynamic_sections_created;
2866 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2868 && SYMBOL_REFERENCES_LOCAL (info, h))
2869 || (ELF_ST_VISIBILITY (h->other)
2870 && h->root.type == bfd_link_hash_undefweak))
2872 /* This is actually a static link, or it is a -Bsymbolic
2873 link and the symbol is defined locally, or the symbol
2874 was forced to be local because of a version file. We
2875 must initialize this entry in the global offset table.
2876 Since the offset must always be a multiple of 8, we
2877 use the least significant bit to record whether we
2878 have initialized it already.
2880 When doing a dynamic link, we create a .rela.got
2881 relocation entry to initialize the value. This is
2882 done in the finish_dynamic_symbol routine. */
2887 bfd_put_64 (output_bfd, relocation,
2888 base_got->contents + off);
2889 /* Note that this is harmless for the GOTPLT64 case,
2890 as -1 | 1 still is -1. */
2895 unresolved_reloc = FALSE;
2899 if (local_got_offsets == NULL)
2902 off = local_got_offsets[r_symndx];
2904 /* The offset must always be a multiple of 8. We use
2905 the least significant bit to record whether we have
2906 already generated the necessary reloc. */
2911 bfd_put_64 (output_bfd, relocation,
2912 base_got->contents + off);
2917 Elf_Internal_Rela outrel;
2920 /* We need to generate a R_X86_64_RELATIVE reloc
2921 for the dynamic linker. */
2922 s = htab->elf.srelgot;
2926 outrel.r_offset = (base_got->output_section->vma
2927 + base_got->output_offset
2929 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2930 outrel.r_addend = relocation;
2932 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2933 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2936 local_got_offsets[r_symndx] |= 1;
2940 if (off >= (bfd_vma) -2)
2943 relocation = base_got->output_section->vma
2944 + base_got->output_offset + off;
2945 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
2946 relocation -= htab->elf.sgotplt->output_section->vma
2947 - htab->elf.sgotplt->output_offset;
2951 case R_X86_64_GOTOFF64:
2952 /* Relocation is relative to the start of the global offset
2955 /* Check to make sure it isn't a protected function symbol
2956 for shared library since it may not be local when used
2957 as function address. */
2961 && h->type == STT_FUNC
2962 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2964 (*_bfd_error_handler)
2965 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2966 input_bfd, h->root.root.string);
2967 bfd_set_error (bfd_error_bad_value);
2971 /* Note that sgot is not involved in this
2972 calculation. We always want the start of .got.plt. If we
2973 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2974 permitted by the ABI, we might have to change this
2976 relocation -= htab->elf.sgotplt->output_section->vma
2977 + htab->elf.sgotplt->output_offset;
2980 case R_X86_64_GOTPC32:
2981 case R_X86_64_GOTPC64:
2982 /* Use global offset table as symbol value. */
2983 relocation = htab->elf.sgotplt->output_section->vma
2984 + htab->elf.sgotplt->output_offset;
2985 unresolved_reloc = FALSE;
2988 case R_X86_64_PLTOFF64:
2989 /* Relocation is PLT entry relative to GOT. For local
2990 symbols it's the symbol itself relative to GOT. */
2992 /* See PLT32 handling. */
2993 && h->plt.offset != (bfd_vma) -1
2994 && htab->elf.splt != NULL)
2996 relocation = (htab->elf.splt->output_section->vma
2997 + htab->elf.splt->output_offset
2999 unresolved_reloc = FALSE;
3002 relocation -= htab->elf.sgotplt->output_section->vma
3003 + htab->elf.sgotplt->output_offset;
3006 case R_X86_64_PLT32:
3007 /* Relocation is to the entry for this symbol in the
3008 procedure linkage table. */
3010 /* Resolve a PLT32 reloc against a local symbol directly,
3011 without using the procedure linkage table. */
3015 if (h->plt.offset == (bfd_vma) -1
3016 || htab->elf.splt == NULL)
3018 /* We didn't make a PLT entry for this symbol. This
3019 happens when statically linking PIC code, or when
3020 using -Bsymbolic. */
3024 relocation = (htab->elf.splt->output_section->vma
3025 + htab->elf.splt->output_offset
3027 unresolved_reloc = FALSE;
3034 && (input_section->flags & SEC_ALLOC) != 0
3035 && (input_section->flags & SEC_READONLY) != 0
3038 bfd_boolean fail = FALSE;
3040 = (r_type == R_X86_64_PC32
3041 && is_32bit_relative_branch (contents, rel->r_offset));
3043 if (SYMBOL_REFERENCES_LOCAL (info, h))
3045 /* Symbol is referenced locally. Make sure it is
3046 defined locally or for a branch. */
3047 fail = !h->def_regular && !branch;
3051 /* Symbol isn't referenced locally. We only allow
3052 branch to symbol with non-default visibility. */
3054 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3061 const char *pic = "";
3063 switch (ELF_ST_VISIBILITY (h->other))
3066 v = _("hidden symbol");
3069 v = _("internal symbol");
3072 v = _("protected symbol");
3076 pic = _("; recompile with -fPIC");
3081 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3083 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3085 (*_bfd_error_handler) (fmt, input_bfd,
3086 x86_64_elf_howto_table[r_type].name,
3087 v, h->root.root.string, pic);
3088 bfd_set_error (bfd_error_bad_value);
3099 /* FIXME: The ABI says the linker should make sure the value is
3100 the same when it's zeroextended to 64 bit. */
3102 if ((input_section->flags & SEC_ALLOC) == 0)
3107 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3108 || h->root.type != bfd_link_hash_undefweak)
3109 && (! IS_X86_64_PCREL_TYPE (r_type)
3110 || ! SYMBOL_CALLS_LOCAL (info, h)))
3111 || (ELIMINATE_COPY_RELOCS
3118 || h->root.type == bfd_link_hash_undefweak
3119 || h->root.type == bfd_link_hash_undefined)))
3121 Elf_Internal_Rela outrel;
3123 bfd_boolean skip, relocate;
3126 /* When generating a shared object, these relocations
3127 are copied into the output file to be resolved at run
3133 _bfd_elf_section_offset (output_bfd, info, input_section,
3135 if (outrel.r_offset == (bfd_vma) -1)
3137 else if (outrel.r_offset == (bfd_vma) -2)
3138 skip = TRUE, relocate = TRUE;
3140 outrel.r_offset += (input_section->output_section->vma
3141 + input_section->output_offset);
3144 memset (&outrel, 0, sizeof outrel);
3146 /* h->dynindx may be -1 if this symbol was marked to
3150 && (IS_X86_64_PCREL_TYPE (r_type)
3152 || ! SYMBOLIC_BIND (info, h)
3153 || ! h->def_regular))
3155 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3156 outrel.r_addend = rel->r_addend;
3160 /* This symbol is local, or marked to become local. */
3161 if (r_type == R_X86_64_64)
3164 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3165 outrel.r_addend = relocation + rel->r_addend;
3171 if (bfd_is_abs_section (sec))
3173 else if (sec == NULL || sec->owner == NULL)
3175 bfd_set_error (bfd_error_bad_value);
3182 /* We are turning this relocation into one
3183 against a section symbol. It would be
3184 proper to subtract the symbol's value,
3185 osec->vma, from the emitted reloc addend,
3186 but ld.so expects buggy relocs. */
3187 osec = sec->output_section;
3188 sindx = elf_section_data (osec)->dynindx;
3191 asection *oi = htab->elf.text_index_section;
3192 sindx = elf_section_data (oi)->dynindx;
3194 BFD_ASSERT (sindx != 0);
3197 outrel.r_info = ELF64_R_INFO (sindx, r_type);
3198 outrel.r_addend = relocation + rel->r_addend;
3202 sreloc = elf_section_data (input_section)->sreloc;
3204 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3206 loc = sreloc->contents;
3207 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3208 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3210 /* If this reloc is against an external symbol, we do
3211 not want to fiddle with the addend. Otherwise, we
3212 need to include the symbol value so that it becomes
3213 an addend for the dynamic reloc. */
3220 case R_X86_64_TLSGD:
3221 case R_X86_64_GOTPC32_TLSDESC:
3222 case R_X86_64_TLSDESC_CALL:
3223 case R_X86_64_GOTTPOFF:
3224 tls_type = GOT_UNKNOWN;
3225 if (h == NULL && local_got_offsets)
3226 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3228 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
3230 if (! elf64_x86_64_tls_transition (info, input_bfd,
3231 input_section, contents,
3232 symtab_hdr, sym_hashes,
3233 &r_type, tls_type, rel,
3237 if (r_type == R_X86_64_TPOFF32)
3239 bfd_vma roff = rel->r_offset;
3241 BFD_ASSERT (! unresolved_reloc);
3243 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3245 /* GD->LE transition.
3246 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3247 .word 0x6666; rex64; call __tls_get_addr
3250 leaq foo@tpoff(%rax), %rax */
3251 memcpy (contents + roff - 4,
3252 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3254 bfd_put_32 (output_bfd,
3255 elf64_x86_64_tpoff (info, relocation),
3256 contents + roff + 8);
3257 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3261 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3263 /* GDesc -> LE transition.
3264 It's originally something like:
3265 leaq x@tlsdesc(%rip), %rax
3271 unsigned int val, type, type2;
3273 type = bfd_get_8 (input_bfd, contents + roff - 3);
3274 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3275 val = bfd_get_8 (input_bfd, contents + roff - 1);
3276 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3277 contents + roff - 3);
3278 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3279 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3280 contents + roff - 1);
3281 bfd_put_32 (output_bfd,
3282 elf64_x86_64_tpoff (info, relocation),
3286 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3288 /* GDesc -> LE transition.
3293 bfd_put_8 (output_bfd, 0x66, contents + roff);
3294 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3297 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3299 /* IE->LE transition:
3300 Originally it can be one of:
3301 movq foo@gottpoff(%rip), %reg
3302 addq foo@gottpoff(%rip), %reg
3305 leaq foo(%reg), %reg
3308 unsigned int val, type, reg;
3310 val = bfd_get_8 (input_bfd, contents + roff - 3);
3311 type = bfd_get_8 (input_bfd, contents + roff - 2);
3312 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3318 bfd_put_8 (output_bfd, 0x49,
3319 contents + roff - 3);
3320 bfd_put_8 (output_bfd, 0xc7,
3321 contents + roff - 2);
3322 bfd_put_8 (output_bfd, 0xc0 | reg,
3323 contents + roff - 1);
3327 /* addq -> addq - addressing with %rsp/%r12 is
3330 bfd_put_8 (output_bfd, 0x49,
3331 contents + roff - 3);
3332 bfd_put_8 (output_bfd, 0x81,
3333 contents + roff - 2);
3334 bfd_put_8 (output_bfd, 0xc0 | reg,
3335 contents + roff - 1);
3341 bfd_put_8 (output_bfd, 0x4d,
3342 contents + roff - 3);
3343 bfd_put_8 (output_bfd, 0x8d,
3344 contents + roff - 2);
3345 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3346 contents + roff - 1);
3348 bfd_put_32 (output_bfd,
3349 elf64_x86_64_tpoff (info, relocation),
3357 if (htab->elf.sgot == NULL)
3362 off = h->got.offset;
3363 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
3367 if (local_got_offsets == NULL)
3370 off = local_got_offsets[r_symndx];
3371 offplt = local_tlsdesc_gotents[r_symndx];
3378 Elf_Internal_Rela outrel;
3383 if (htab->elf.srelgot == NULL)
3386 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3388 if (GOT_TLS_GDESC_P (tls_type))
3390 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
3391 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3392 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3393 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3394 + htab->elf.sgotplt->output_offset
3396 + htab->sgotplt_jump_table_size);
3397 sreloc = htab->elf.srelplt;
3398 loc = sreloc->contents;
3399 loc += sreloc->reloc_count++
3400 * sizeof (Elf64_External_Rela);
3401 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3402 <= sreloc->contents + sreloc->size);
3404 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3406 outrel.r_addend = 0;
3407 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3410 sreloc = htab->elf.srelgot;
3412 outrel.r_offset = (htab->elf.sgot->output_section->vma
3413 + htab->elf.sgot->output_offset + off);
3415 if (GOT_TLS_GD_P (tls_type))
3416 dr_type = R_X86_64_DTPMOD64;
3417 else if (GOT_TLS_GDESC_P (tls_type))
3420 dr_type = R_X86_64_TPOFF64;
3422 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3423 outrel.r_addend = 0;
3424 if ((dr_type == R_X86_64_TPOFF64
3425 || dr_type == R_X86_64_TLSDESC) && indx == 0)
3426 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3427 outrel.r_info = ELF64_R_INFO (indx, dr_type);
3429 loc = sreloc->contents;
3430 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3431 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3432 <= sreloc->contents + sreloc->size);
3433 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3435 if (GOT_TLS_GD_P (tls_type))
3439 BFD_ASSERT (! unresolved_reloc);
3440 bfd_put_64 (output_bfd,
3441 relocation - elf64_x86_64_dtpoff_base (info),
3442 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3446 bfd_put_64 (output_bfd, 0,
3447 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3448 outrel.r_info = ELF64_R_INFO (indx,
3450 outrel.r_offset += GOT_ENTRY_SIZE;
3451 sreloc->reloc_count++;
3452 loc += sizeof (Elf64_External_Rela);
3453 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3454 <= sreloc->contents + sreloc->size);
3455 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3463 local_got_offsets[r_symndx] |= 1;
3466 if (off >= (bfd_vma) -2
3467 && ! GOT_TLS_GDESC_P (tls_type))
3469 if (r_type == ELF64_R_TYPE (rel->r_info))
3471 if (r_type == R_X86_64_GOTPC32_TLSDESC
3472 || r_type == R_X86_64_TLSDESC_CALL)
3473 relocation = htab->elf.sgotplt->output_section->vma
3474 + htab->elf.sgotplt->output_offset
3475 + offplt + htab->sgotplt_jump_table_size;
3477 relocation = htab->elf.sgot->output_section->vma
3478 + htab->elf.sgot->output_offset + off;
3479 unresolved_reloc = FALSE;
3483 bfd_vma roff = rel->r_offset;
3485 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3487 /* GD->IE transition.
3488 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3489 .word 0x6666; rex64; call __tls_get_addr@plt
3492 addq foo@gottpoff(%rip), %rax */
3493 memcpy (contents + roff - 4,
3494 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3497 relocation = (htab->elf.sgot->output_section->vma
3498 + htab->elf.sgot->output_offset + off
3500 - input_section->output_section->vma
3501 - input_section->output_offset
3503 bfd_put_32 (output_bfd, relocation,
3504 contents + roff + 8);
3505 /* Skip R_X86_64_PLT32. */
3509 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3511 /* GDesc -> IE transition.
3512 It's originally something like:
3513 leaq x@tlsdesc(%rip), %rax
3516 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3519 unsigned int val, type, type2;
3521 type = bfd_get_8 (input_bfd, contents + roff - 3);
3522 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3523 val = bfd_get_8 (input_bfd, contents + roff - 1);
3525 /* Now modify the instruction as appropriate. To
3526 turn a leaq into a movq in the form we use it, it
3527 suffices to change the second byte from 0x8d to
3529 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3531 bfd_put_32 (output_bfd,
3532 htab->elf.sgot->output_section->vma
3533 + htab->elf.sgot->output_offset + off
3535 - input_section->output_section->vma
3536 - input_section->output_offset
3541 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3543 /* GDesc -> IE transition.
3550 unsigned int val, type;
3552 type = bfd_get_8 (input_bfd, contents + roff);
3553 val = bfd_get_8 (input_bfd, contents + roff + 1);
3554 bfd_put_8 (output_bfd, 0x66, contents + roff);
3555 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3563 case R_X86_64_TLSLD:
3564 if (! elf64_x86_64_tls_transition (info, input_bfd,
3565 input_section, contents,
3566 symtab_hdr, sym_hashes,
3567 &r_type, GOT_UNKNOWN,
3571 if (r_type != R_X86_64_TLSLD)
3573 /* LD->LE transition:
3574 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3576 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3578 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3579 memcpy (contents + rel->r_offset - 3,
3580 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3581 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3586 if (htab->elf.sgot == NULL)
3589 off = htab->tls_ld_got.offset;
3594 Elf_Internal_Rela outrel;
3597 if (htab->elf.srelgot == NULL)
3600 outrel.r_offset = (htab->elf.sgot->output_section->vma
3601 + htab->elf.sgot->output_offset + off);
3603 bfd_put_64 (output_bfd, 0,
3604 htab->elf.sgot->contents + off);
3605 bfd_put_64 (output_bfd, 0,
3606 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3607 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3608 outrel.r_addend = 0;
3609 loc = htab->elf.srelgot->contents;
3610 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3611 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3612 htab->tls_ld_got.offset |= 1;
3614 relocation = htab->elf.sgot->output_section->vma
3615 + htab->elf.sgot->output_offset + off;
3616 unresolved_reloc = FALSE;
3619 case R_X86_64_DTPOFF32:
3620 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3621 relocation -= elf64_x86_64_dtpoff_base (info);
3623 relocation = elf64_x86_64_tpoff (info, relocation);
3626 case R_X86_64_TPOFF32:
3627 BFD_ASSERT (! info->shared);
3628 relocation = elf64_x86_64_tpoff (info, relocation);
3635 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3636 because such sections are not SEC_ALLOC and thus ld.so will
3637 not process them. */
3638 if (unresolved_reloc
3639 && !((input_section->flags & SEC_DEBUGGING) != 0
3641 (*_bfd_error_handler)
3642 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3645 (long) rel->r_offset,
3647 h->root.root.string);
3650 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3651 contents, rel->r_offset,
3652 relocation, rel->r_addend);
3654 if (r != bfd_reloc_ok)
3659 name = h->root.root.string;
3662 name = bfd_elf_string_from_elf_section (input_bfd,
3663 symtab_hdr->sh_link,
3668 name = bfd_section_name (input_bfd, sec);
3671 if (r == bfd_reloc_overflow)
3673 if (! ((*info->callbacks->reloc_overflow)
3674 (info, (h ? &h->root : NULL), name, howto->name,
3675 (bfd_vma) 0, input_bfd, input_section,
3681 (*_bfd_error_handler)
3682 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3683 input_bfd, input_section,
3684 (long) rel->r_offset, name, (int) r);
3693 /* Finish up dynamic symbol handling. We set the contents of various
3694 dynamic sections here. */
3697 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3698 struct bfd_link_info *info,
3699 struct elf_link_hash_entry *h,
3700 Elf_Internal_Sym *sym)
3702 struct elf64_x86_64_link_hash_table *htab;
3704 htab = elf64_x86_64_hash_table (info);
3706 if (h->plt.offset != (bfd_vma) -1)
3710 Elf_Internal_Rela rela;
3712 asection *plt, *gotplt, *relplt;
3714 /* When building a static executable, use .iplt, .igot.plt and
3715 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3716 if (htab->elf.splt != NULL)
3718 plt = htab->elf.splt;
3719 gotplt = htab->elf.sgotplt;
3720 relplt = htab->elf.srelplt;
3724 plt = htab->elf.iplt;
3725 gotplt = htab->elf.igotplt;
3726 relplt = htab->elf.irelplt;
3729 /* This symbol has an entry in the procedure linkage table. Set
3731 if ((h->dynindx == -1
3732 && !((h->forced_local || info->executable)
3734 && h->type == STT_GNU_IFUNC))
3740 /* Get the index in the procedure linkage table which
3741 corresponds to this symbol. This is the index of this symbol
3742 in all the symbols for which we are making plt entries. The
3743 first entry in the procedure linkage table is reserved.
3745 Get the offset into the .got table of the entry that
3746 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3747 bytes. The first three are reserved for the dynamic linker.
3749 For static executables, we don't reserve anything. */
3751 if (plt == htab->elf.splt)
3753 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3754 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3758 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3759 got_offset = plt_index * GOT_ENTRY_SIZE;
3762 /* Fill in the entry in the procedure linkage table. */
3763 memcpy (plt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3766 /* Insert the relocation positions of the plt section. The magic
3767 numbers at the end of the statements are the positions of the
3768 relocations in the plt section. */
3769 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3770 instruction uses 6 bytes, subtract this value. */
3771 bfd_put_32 (output_bfd,
3772 (gotplt->output_section->vma
3773 + gotplt->output_offset
3775 - plt->output_section->vma
3776 - plt->output_offset
3779 plt->contents + h->plt.offset + 2);
3781 /* Don't fill PLT entry for static executables. */
3782 if (plt == htab->elf.splt)
3784 /* Put relocation index. */
3785 bfd_put_32 (output_bfd, plt_index,
3786 plt->contents + h->plt.offset + 7);
3787 /* Put offset for jmp .PLT0. */
3788 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3789 plt->contents + h->plt.offset + 12);
3792 /* Fill in the entry in the global offset table, initially this
3793 points to the pushq instruction in the PLT which is at offset 6. */
3794 bfd_put_64 (output_bfd, (plt->output_section->vma
3795 + plt->output_offset
3796 + h->plt.offset + 6),
3797 gotplt->contents + got_offset);
3799 /* Fill in the entry in the .rela.plt section. */
3800 rela.r_offset = (gotplt->output_section->vma
3801 + gotplt->output_offset
3803 if (h->dynindx == -1
3804 || ((info->executable
3805 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3807 && h->type == STT_GNU_IFUNC))
3809 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3810 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
3811 rela.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
3812 rela.r_addend = (h->root.u.def.value
3813 + h->root.u.def.section->output_section->vma
3814 + h->root.u.def.section->output_offset);
3818 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3821 loc = relplt->contents + plt_index * sizeof (Elf64_External_Rela);
3822 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3824 if (!h->def_regular)
3826 /* Mark the symbol as undefined, rather than as defined in
3827 the .plt section. Leave the value if there were any
3828 relocations where pointer equality matters (this is a clue
3829 for the dynamic linker, to make function pointer
3830 comparisons work between an application and shared
3831 library), otherwise set it to zero. If a function is only
3832 called from a binary, there is no need to slow down
3833 shared libraries because of that. */
3834 sym->st_shndx = SHN_UNDEF;
3835 if (!h->pointer_equality_needed)
3840 if (h->got.offset != (bfd_vma) -1
3841 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3842 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3844 Elf_Internal_Rela rela;
3847 /* This symbol has an entry in the global offset table. Set it
3849 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3852 rela.r_offset = (htab->elf.sgot->output_section->vma
3853 + htab->elf.sgot->output_offset
3854 + (h->got.offset &~ (bfd_vma) 1));
3856 /* If this is a static link, or it is a -Bsymbolic link and the
3857 symbol is defined locally or was forced to be local because
3858 of a version file, we just want to emit a RELATIVE reloc.
3859 The entry in the global offset table will already have been
3860 initialized in the relocate_section function. */
3862 && h->type == STT_GNU_IFUNC)
3866 /* Generate R_X86_64_GLOB_DAT. */
3871 if (!h->pointer_equality_needed)
3874 /* For non-shared object, we can't use .got.plt, which
3875 contains the real function addres if we need pointer
3876 equality. We load the GOT entry with the PLT entry. */
3877 asection *plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3878 bfd_put_64 (output_bfd, (plt->output_section->vma
3879 + plt->output_offset
3881 htab->elf.sgot->contents + h->got.offset);
3885 else if (info->shared
3886 && SYMBOL_REFERENCES_LOCAL (info, h))
3888 if (!h->def_regular)
3890 BFD_ASSERT((h->got.offset & 1) != 0);
3891 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3892 rela.r_addend = (h->root.u.def.value
3893 + h->root.u.def.section->output_section->vma
3894 + h->root.u.def.section->output_offset);
3898 BFD_ASSERT((h->got.offset & 1) == 0);
3900 bfd_put_64 (output_bfd, (bfd_vma) 0,
3901 htab->elf.sgot->contents + h->got.offset);
3902 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3906 loc = htab->elf.srelgot->contents;
3907 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3908 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3913 Elf_Internal_Rela rela;
3916 /* This symbol needs a copy reloc. Set it up. */
3918 if (h->dynindx == -1
3919 || (h->root.type != bfd_link_hash_defined
3920 && h->root.type != bfd_link_hash_defweak)
3921 || htab->srelbss == NULL)
3924 rela.r_offset = (h->root.u.def.value
3925 + h->root.u.def.section->output_section->vma
3926 + h->root.u.def.section->output_offset);
3927 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3929 loc = htab->srelbss->contents;
3930 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
3931 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3934 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
3935 be NULL for local symbols. */
3937 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
3938 || h == htab->elf.hgot))
3939 sym->st_shndx = SHN_ABS;
3944 /* Finish up local dynamic symbol handling. We set the contents of
3945 various dynamic sections here. */
3948 elf64_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
3950 struct elf_link_hash_entry *h
3951 = (struct elf_link_hash_entry *) *slot;
3952 struct bfd_link_info *info
3953 = (struct bfd_link_info *) inf;
3955 return elf64_x86_64_finish_dynamic_symbol (info->output_bfd,
3959 /* Used to decide how to sort relocs in an optimal manner for the
3960 dynamic linker, before writing them out. */
3962 static enum elf_reloc_type_class
3963 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
3965 switch ((int) ELF64_R_TYPE (rela->r_info))
3967 case R_X86_64_RELATIVE:
3968 return reloc_class_relative;
3969 case R_X86_64_JUMP_SLOT:
3970 return reloc_class_plt;
3972 return reloc_class_copy;
3974 return reloc_class_normal;
3978 /* Finish up the dynamic sections. */
3981 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3983 struct elf64_x86_64_link_hash_table *htab;
3987 htab = elf64_x86_64_hash_table (info);
3988 dynobj = htab->elf.dynobj;
3989 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3991 if (htab->elf.dynamic_sections_created)
3993 Elf64_External_Dyn *dyncon, *dynconend;
3995 if (sdyn == NULL || htab->elf.sgot == NULL)
3998 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3999 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
4000 for (; dyncon < dynconend; dyncon++)
4002 Elf_Internal_Dyn dyn;
4005 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4013 s = htab->elf.sgotplt;
4014 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4018 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
4022 s = htab->elf.srelplt->output_section;
4023 dyn.d_un.d_val = s->size;
4027 /* The procedure linkage table relocs (DT_JMPREL) should
4028 not be included in the overall relocs (DT_RELA).
4029 Therefore, we override the DT_RELASZ entry here to
4030 make it not include the JMPREL relocs. Since the
4031 linker script arranges for .rela.plt to follow all
4032 other relocation sections, we don't have to worry
4033 about changing the DT_RELA entry. */
4034 if (htab->elf.srelplt != NULL)
4036 s = htab->elf.srelplt->output_section;
4037 dyn.d_un.d_val -= s->size;
4041 case DT_TLSDESC_PLT:
4043 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4044 + htab->tlsdesc_plt;
4047 case DT_TLSDESC_GOT:
4049 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4050 + htab->tlsdesc_got;
4054 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4057 /* Fill in the special first entry in the procedure linkage table. */
4058 if (htab->elf.splt && htab->elf.splt->size > 0)
4060 /* Fill in the first entry in the procedure linkage table. */
4061 memcpy (htab->elf.splt->contents, elf64_x86_64_plt0_entry,
4063 /* Add offset for pushq GOT+8(%rip), since the instruction
4064 uses 6 bytes subtract this value. */
4065 bfd_put_32 (output_bfd,
4066 (htab->elf.sgotplt->output_section->vma
4067 + htab->elf.sgotplt->output_offset
4069 - htab->elf.splt->output_section->vma
4070 - htab->elf.splt->output_offset
4072 htab->elf.splt->contents + 2);
4073 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4074 the end of the instruction. */
4075 bfd_put_32 (output_bfd,
4076 (htab->elf.sgotplt->output_section->vma
4077 + htab->elf.sgotplt->output_offset
4079 - htab->elf.splt->output_section->vma
4080 - htab->elf.splt->output_offset
4082 htab->elf.splt->contents + 8);
4084 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize =
4087 if (htab->tlsdesc_plt)
4089 bfd_put_64 (output_bfd, (bfd_vma) 0,
4090 htab->elf.sgot->contents + htab->tlsdesc_got);
4092 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
4093 elf64_x86_64_plt0_entry,
4096 /* Add offset for pushq GOT+8(%rip), since the
4097 instruction uses 6 bytes subtract this value. */
4098 bfd_put_32 (output_bfd,
4099 (htab->elf.sgotplt->output_section->vma
4100 + htab->elf.sgotplt->output_offset
4102 - htab->elf.splt->output_section->vma
4103 - htab->elf.splt->output_offset
4106 htab->elf.splt->contents + htab->tlsdesc_plt + 2);
4107 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4108 htab->tlsdesc_got. The 12 is the offset to the end of
4110 bfd_put_32 (output_bfd,
4111 (htab->elf.sgot->output_section->vma
4112 + htab->elf.sgot->output_offset
4114 - htab->elf.splt->output_section->vma
4115 - htab->elf.splt->output_offset
4118 htab->elf.splt->contents + htab->tlsdesc_plt + 8);
4123 if (htab->elf.sgotplt)
4125 /* Fill in the first three entries in the global offset table. */
4126 if (htab->elf.sgotplt->size > 0)
4128 /* Set the first entry in the global offset table to the address of
4129 the dynamic section. */
4131 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4133 bfd_put_64 (output_bfd,
4134 sdyn->output_section->vma + sdyn->output_offset,
4135 htab->elf.sgotplt->contents);
4136 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4137 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4138 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4141 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
4145 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4146 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
4149 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4150 htab_traverse (htab->loc_hash_table,
4151 elf64_x86_64_finish_local_dynamic_symbol,
4157 /* Return address for Ith PLT stub in section PLT, for relocation REL
4158 or (bfd_vma) -1 if it should not be included. */
4161 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4162 const arelent *rel ATTRIBUTE_UNUSED)
4164 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4167 /* Handle an x86-64 specific section when reading an object file. This
4168 is called when elfcode.h finds a section with an unknown type. */
4171 elf64_x86_64_section_from_shdr (bfd *abfd,
4172 Elf_Internal_Shdr *hdr,
4176 if (hdr->sh_type != SHT_X86_64_UNWIND)
4179 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4185 /* Hook called by the linker routine which adds symbols from an object
4186 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4190 elf64_x86_64_add_symbol_hook (bfd *abfd,
4191 struct bfd_link_info *info,
4192 Elf_Internal_Sym *sym,
4193 const char **namep ATTRIBUTE_UNUSED,
4194 flagword *flagsp ATTRIBUTE_UNUSED,
4200 switch (sym->st_shndx)
4202 case SHN_X86_64_LCOMMON:
4203 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4206 lcomm = bfd_make_section_with_flags (abfd,
4210 | SEC_LINKER_CREATED));
4213 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4216 *valp = sym->st_size;
4220 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4221 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4227 /* Given a BFD section, try to locate the corresponding ELF section
4231 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4232 asection *sec, int *index)
4234 if (sec == &_bfd_elf_large_com_section)
4236 *index = SHN_X86_64_LCOMMON;
4242 /* Process a symbol. */
4245 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4248 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4250 switch (elfsym->internal_elf_sym.st_shndx)
4252 case SHN_X86_64_LCOMMON:
4253 asym->section = &_bfd_elf_large_com_section;
4254 asym->value = elfsym->internal_elf_sym.st_size;
4255 /* Common symbol doesn't set BSF_GLOBAL. */
4256 asym->flags &= ~BSF_GLOBAL;
4262 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
4264 return (sym->st_shndx == SHN_COMMON
4265 || sym->st_shndx == SHN_X86_64_LCOMMON);
4269 elf64_x86_64_common_section_index (asection *sec)
4271 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4274 return SHN_X86_64_LCOMMON;
4278 elf64_x86_64_common_section (asection *sec)
4280 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4281 return bfd_com_section_ptr;
4283 return &_bfd_elf_large_com_section;
4287 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4288 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4289 struct elf_link_hash_entry *h,
4290 Elf_Internal_Sym *sym,
4292 bfd_vma *pvalue ATTRIBUTE_UNUSED,
4293 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4294 bfd_boolean *skip ATTRIBUTE_UNUSED,
4295 bfd_boolean *override ATTRIBUTE_UNUSED,
4296 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4297 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4298 bfd_boolean *newdef ATTRIBUTE_UNUSED,
4299 bfd_boolean *newdyn,
4300 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4301 bfd_boolean *newweak ATTRIBUTE_UNUSED,
4302 bfd *abfd ATTRIBUTE_UNUSED,
4304 bfd_boolean *olddef ATTRIBUTE_UNUSED,
4305 bfd_boolean *olddyn,
4306 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4307 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4311 /* A normal common symbol and a large common symbol result in a
4312 normal common symbol. We turn the large common symbol into a
4315 && h->root.type == bfd_link_hash_common
4317 && bfd_is_com_section (*sec)
4320 if (sym->st_shndx == SHN_COMMON
4321 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4323 h->root.u.c.p->section
4324 = bfd_make_section_old_way (oldbfd, "COMMON");
4325 h->root.u.c.p->section->flags = SEC_ALLOC;
4327 else if (sym->st_shndx == SHN_X86_64_LCOMMON
4328 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4329 *psec = *sec = bfd_com_section_ptr;
4336 elf64_x86_64_additional_program_headers (bfd *abfd,
4337 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4342 /* Check to see if we need a large readonly segment. */
4343 s = bfd_get_section_by_name (abfd, ".lrodata");
4344 if (s && (s->flags & SEC_LOAD))
4347 /* Check to see if we need a large data segment. Since .lbss sections
4348 is placed right after the .bss section, there should be no need for
4349 a large data segment just because of .lbss. */
4350 s = bfd_get_section_by_name (abfd, ".ldata");
4351 if (s && (s->flags & SEC_LOAD))
4357 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4360 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4362 if (h->plt.offset != (bfd_vma) -1
4364 && !h->pointer_equality_needed)
4367 return _bfd_elf_hash_symbol (h);
4370 static const struct bfd_elf_special_section
4371 elf64_x86_64_special_sections[]=
4373 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4374 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4375 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4376 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4377 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4378 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4379 { NULL, 0, 0, 0, 0 }
4382 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
4383 #define TARGET_LITTLE_NAME "elf64-x86-64"
4384 #define ELF_ARCH bfd_arch_i386
4385 #define ELF_MACHINE_CODE EM_X86_64
4386 #define ELF_MAXPAGESIZE 0x200000
4387 #define ELF_MINPAGESIZE 0x1000
4388 #define ELF_COMMONPAGESIZE 0x1000
4390 #define elf_backend_can_gc_sections 1
4391 #define elf_backend_can_refcount 1
4392 #define elf_backend_want_got_plt 1
4393 #define elf_backend_plt_readonly 1
4394 #define elf_backend_want_plt_sym 0
4395 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
4396 #define elf_backend_rela_normal 1
4398 #define elf_info_to_howto elf64_x86_64_info_to_howto
4400 #define bfd_elf64_bfd_link_hash_table_create \
4401 elf64_x86_64_link_hash_table_create
4402 #define bfd_elf64_bfd_link_hash_table_free \
4403 elf64_x86_64_link_hash_table_free
4404 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
4405 #define bfd_elf64_bfd_reloc_name_lookup \
4406 elf64_x86_64_reloc_name_lookup
4408 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
4409 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4410 #define elf_backend_check_relocs elf64_x86_64_check_relocs
4411 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
4412 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
4413 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
4414 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
4415 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
4416 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
4417 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
4418 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
4419 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
4420 #define elf_backend_relocate_section elf64_x86_64_relocate_section
4421 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
4422 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
4423 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4424 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
4425 #define elf_backend_object_p elf64_x86_64_elf_object_p
4426 #define bfd_elf64_mkobject elf64_x86_64_mkobject
4428 #define elf_backend_section_from_shdr \
4429 elf64_x86_64_section_from_shdr
4431 #define elf_backend_section_from_bfd_section \
4432 elf64_x86_64_elf_section_from_bfd_section
4433 #define elf_backend_add_symbol_hook \
4434 elf64_x86_64_add_symbol_hook
4435 #define elf_backend_symbol_processing \
4436 elf64_x86_64_symbol_processing
4437 #define elf_backend_common_section_index \
4438 elf64_x86_64_common_section_index
4439 #define elf_backend_common_section \
4440 elf64_x86_64_common_section
4441 #define elf_backend_common_definition \
4442 elf64_x86_64_common_definition
4443 #define elf_backend_merge_symbol \
4444 elf64_x86_64_merge_symbol
4445 #define elf_backend_special_sections \
4446 elf64_x86_64_special_sections
4447 #define elf_backend_additional_program_headers \
4448 elf64_x86_64_additional_program_headers
4449 #define elf_backend_hash_symbol \
4450 elf64_x86_64_hash_symbol
4452 #undef elf_backend_post_process_headers
4453 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4455 #include "elf64-target.h"
4457 /* FreeBSD support. */
4459 #undef TARGET_LITTLE_SYM
4460 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
4461 #undef TARGET_LITTLE_NAME
4462 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
4465 #define ELF_OSABI ELFOSABI_FREEBSD
4468 #define elf64_bed elf64_x86_64_fbsd_bed
4470 #include "elf64-target.h"