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.
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"
30 #include "elf/x86-64.h"
32 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
33 #define MINUS_ONE (~ (bfd_vma) 0)
35 /* The relocation "howto" table. Order of fields:
36 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
37 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
38 static reloc_howto_type x86_64_elf_howto_table[] =
40 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
41 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
43 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
44 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
46 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
47 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
49 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
50 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
52 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
53 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
55 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
56 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
58 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
59 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
61 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
64 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
65 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
67 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
68 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
70 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
71 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
73 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
74 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
76 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
78 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
79 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
80 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
81 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
82 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
83 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
84 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
85 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
87 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
90 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
91 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
93 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
94 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
96 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
97 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
99 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
100 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
102 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
103 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
105 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
106 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
108 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
109 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
111 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
112 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
113 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
114 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
115 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
116 FALSE, 0xffffffff, 0xffffffff, TRUE),
117 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
118 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
120 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
121 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
123 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
124 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
125 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
126 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
127 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
129 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
130 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
134 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
135 complain_overflow_bitfield, bfd_elf_generic_reloc,
136 "R_X86_64_GOTPC32_TLSDESC",
137 FALSE, 0xffffffff, 0xffffffff, TRUE),
138 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
139 complain_overflow_dont, bfd_elf_generic_reloc,
140 "R_X86_64_TLSDESC_CALL",
142 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
143 complain_overflow_bitfield, bfd_elf_generic_reloc,
145 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
146 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
147 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
150 /* We have a gap in the reloc numbers here.
151 R_X86_64_standard counts the number up to this point, and
152 R_X86_64_vt_offset is the value to subtract from a reloc type of
153 R_X86_64_GNU_VT* to form an index into this table. */
154 #define R_X86_64_standard (R_X86_64_IRELATIVE + 1)
155 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
157 /* GNU extension to record C++ vtable hierarchy. */
158 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
159 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
161 /* GNU extension to record C++ vtable member usage. */
162 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
163 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
167 #define IS_X86_64_PCREL_TYPE(TYPE) \
168 ( ((TYPE) == R_X86_64_PC8) \
169 || ((TYPE) == R_X86_64_PC16) \
170 || ((TYPE) == R_X86_64_PC32) \
171 || ((TYPE) == R_X86_64_PC64))
173 /* Map BFD relocs to the x86_64 elf relocs. */
176 bfd_reloc_code_real_type bfd_reloc_val;
177 unsigned char elf_reloc_val;
180 static const struct elf_reloc_map x86_64_reloc_map[] =
182 { BFD_RELOC_NONE, R_X86_64_NONE, },
183 { BFD_RELOC_64, R_X86_64_64, },
184 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
185 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
186 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
187 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
188 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
189 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
190 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
191 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
192 { BFD_RELOC_32, R_X86_64_32, },
193 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
194 { BFD_RELOC_16, R_X86_64_16, },
195 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
196 { BFD_RELOC_8, R_X86_64_8, },
197 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
198 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
199 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
200 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
201 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
202 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
203 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
204 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
205 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
206 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
207 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
208 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
209 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
210 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
211 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
212 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
213 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
214 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
215 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
216 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
217 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
218 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
219 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
222 static reloc_howto_type *
223 elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
227 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
228 || r_type >= (unsigned int) R_X86_64_max)
230 if (r_type >= (unsigned int) R_X86_64_standard)
232 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
234 r_type = R_X86_64_NONE;
239 i = r_type - (unsigned int) R_X86_64_vt_offset;
240 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
241 return &x86_64_elf_howto_table[i];
244 /* Given a BFD reloc type, return a HOWTO structure. */
245 static reloc_howto_type *
246 elf64_x86_64_reloc_type_lookup (bfd *abfd,
247 bfd_reloc_code_real_type code)
251 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
254 if (x86_64_reloc_map[i].bfd_reloc_val == code)
255 return elf64_x86_64_rtype_to_howto (abfd,
256 x86_64_reloc_map[i].elf_reloc_val);
261 static reloc_howto_type *
262 elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
268 i < (sizeof (x86_64_elf_howto_table)
269 / sizeof (x86_64_elf_howto_table[0]));
271 if (x86_64_elf_howto_table[i].name != NULL
272 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
273 return &x86_64_elf_howto_table[i];
278 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
281 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
282 Elf_Internal_Rela *dst)
286 r_type = ELF64_R_TYPE (dst->r_info);
287 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
288 BFD_ASSERT (r_type == cache_ptr->howto->type);
291 /* Support for core dump NOTE sections. */
293 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
298 switch (note->descsz)
303 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
305 elf_tdata (abfd)->core_signal
306 = bfd_get_16 (abfd, note->descdata + 12);
309 elf_tdata (abfd)->core_pid
310 = bfd_get_32 (abfd, note->descdata + 32);
319 /* Make a ".reg/999" section. */
320 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
321 size, note->descpos + offset);
325 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
327 switch (note->descsz)
332 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
333 elf_tdata (abfd)->core_program
334 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
335 elf_tdata (abfd)->core_command
336 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
339 /* Note that for some reason, a spurious space is tacked
340 onto the end of the args in some (at least one anyway)
341 implementations, so strip it off if it exists. */
344 char *command = elf_tdata (abfd)->core_command;
345 int n = strlen (command);
347 if (0 < n && command[n - 1] == ' ')
348 command[n - 1] = '\0';
354 /* Functions for the x86-64 ELF linker. */
356 /* The name of the dynamic interpreter. This is put in the .interp
359 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
361 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
362 copying dynamic variables from a shared lib into an app's dynbss
363 section, and instead use a dynamic relocation to point into the
365 #define ELIMINATE_COPY_RELOCS 1
367 /* The size in bytes of an entry in the global offset table. */
369 #define GOT_ENTRY_SIZE 8
371 /* The size in bytes of an entry in the procedure linkage table. */
373 #define PLT_ENTRY_SIZE 16
375 /* The first entry in a procedure linkage table looks like this. See the
376 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
378 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
380 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
381 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
382 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
385 /* Subsequent entries in a procedure linkage table look like this. */
387 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
389 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
390 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
391 0x68, /* pushq immediate */
392 0, 0, 0, 0, /* replaced with index into relocation table. */
393 0xe9, /* jmp relative */
394 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
397 /* The x86-64 linker needs to keep track of the number of relocs that
398 it decides to copy as dynamic relocs in check_relocs for each symbol.
399 This is so that it can later discard them if they are found to be
400 unnecessary. We store the information in a field extending the
401 regular ELF linker hash table. */
403 struct elf64_x86_64_dyn_relocs
406 struct elf64_x86_64_dyn_relocs *next;
408 /* The input section of the reloc. */
411 /* Total number of relocs copied for the input section. */
414 /* Number of pc-relative relocs copied for the input section. */
415 bfd_size_type pc_count;
418 /* x86-64 ELF linker hash entry. */
420 struct elf64_x86_64_link_hash_entry
422 struct elf_link_hash_entry elf;
424 /* Track dynamic relocs copied for this symbol. */
425 struct elf64_x86_64_dyn_relocs *dyn_relocs;
427 #define GOT_UNKNOWN 0
431 #define GOT_TLS_GDESC 4
432 #define GOT_TLS_GD_BOTH_P(type) \
433 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
434 #define GOT_TLS_GD_P(type) \
435 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
436 #define GOT_TLS_GDESC_P(type) \
437 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
438 #define GOT_TLS_GD_ANY_P(type) \
439 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
440 unsigned char tls_type;
442 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
443 starting at the end of the jump table. */
447 #define elf64_x86_64_hash_entry(ent) \
448 ((struct elf64_x86_64_link_hash_entry *)(ent))
450 struct elf64_x86_64_obj_tdata
452 struct elf_obj_tdata root;
454 /* tls_type for each local got entry. */
455 char *local_got_tls_type;
457 /* GOTPLT entries for TLS descriptors. */
458 bfd_vma *local_tlsdesc_gotent;
461 #define elf64_x86_64_tdata(abfd) \
462 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
464 #define elf64_x86_64_local_got_tls_type(abfd) \
465 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
467 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
468 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
470 #define is_x86_64_elf(bfd) \
471 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
472 && elf_tdata (bfd) != NULL \
473 && elf_object_id (bfd) == X86_64_ELF_TDATA)
476 elf64_x86_64_mkobject (bfd *abfd)
478 return bfd_elf_allocate_object (abfd, sizeof (struct elf64_x86_64_obj_tdata),
482 /* x86-64 ELF linker hash table. */
484 struct elf64_x86_64_link_hash_table
486 struct elf_link_hash_table elf;
488 /* Short-cuts to get to dynamic linker sections. */
500 /* The offset into splt of the PLT entry for the TLS descriptor
501 resolver. Special values are 0, if not necessary (or not found
502 to be necessary yet), and -1 if needed but not determined
505 /* The offset into sgot of the GOT entry used by the PLT entry
510 bfd_signed_vma refcount;
514 /* The amount of space used by the jump slots in the GOT. */
515 bfd_vma sgotplt_jump_table_size;
517 /* Small local sym to section mapping cache. */
518 struct sym_sec_cache sym_sec;
520 /* _TLS_MODULE_BASE_ symbol. */
521 struct bfd_link_hash_entry *tls_module_base;
524 /* Get the x86-64 ELF linker hash table from a link_info structure. */
526 #define elf64_x86_64_hash_table(p) \
527 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
529 #define elf64_x86_64_compute_jump_table_size(htab) \
530 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
532 /* Create an entry in an x86-64 ELF linker hash table. */
534 static struct bfd_hash_entry *
535 elf64_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
536 struct bfd_hash_table *table,
539 /* Allocate the structure if it has not already been allocated by a
543 entry = bfd_hash_allocate (table,
544 sizeof (struct elf64_x86_64_link_hash_entry));
549 /* Call the allocation method of the superclass. */
550 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
553 struct elf64_x86_64_link_hash_entry *eh;
555 eh = (struct elf64_x86_64_link_hash_entry *) entry;
556 eh->dyn_relocs = NULL;
557 eh->tls_type = GOT_UNKNOWN;
558 eh->tlsdesc_got = (bfd_vma) -1;
564 /* Create an X86-64 ELF linker hash table. */
566 static struct bfd_link_hash_table *
567 elf64_x86_64_link_hash_table_create (bfd *abfd)
569 struct elf64_x86_64_link_hash_table *ret;
570 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
572 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
576 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
577 elf64_x86_64_link_hash_newfunc,
578 sizeof (struct elf64_x86_64_link_hash_entry)))
594 ret->sym_sec.abfd = NULL;
595 ret->tlsdesc_plt = 0;
596 ret->tlsdesc_got = 0;
597 ret->tls_ld_got.refcount = 0;
598 ret->sgotplt_jump_table_size = 0;
599 ret->tls_module_base = NULL;
601 return &ret->elf.root;
604 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
605 shortcuts to them in our hash table. */
608 elf64_x86_64_create_got_section (bfd *dynobj, struct bfd_link_info *info)
610 struct elf64_x86_64_link_hash_table *htab;
612 if (! _bfd_elf_create_got_section (dynobj, info))
615 htab = elf64_x86_64_hash_table (info);
616 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
617 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
618 if (!htab->sgot || !htab->sgotplt)
621 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
622 (SEC_ALLOC | SEC_LOAD
627 if (htab->srelgot == NULL
628 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
633 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
634 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
638 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
640 struct elf64_x86_64_link_hash_table *htab;
642 htab = elf64_x86_64_hash_table (info);
643 if (!htab->sgot && !elf64_x86_64_create_got_section (dynobj, info))
646 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
649 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
650 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
651 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
653 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
655 if (!htab->splt || !htab->srelplt || !htab->sdynbss
656 || (!info->shared && !htab->srelbss))
662 /* Copy the extra info we tack onto an elf_link_hash_entry. */
665 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
666 struct elf_link_hash_entry *dir,
667 struct elf_link_hash_entry *ind)
669 struct elf64_x86_64_link_hash_entry *edir, *eind;
671 edir = (struct elf64_x86_64_link_hash_entry *) dir;
672 eind = (struct elf64_x86_64_link_hash_entry *) ind;
674 if (eind->dyn_relocs != NULL)
676 if (edir->dyn_relocs != NULL)
678 struct elf64_x86_64_dyn_relocs **pp;
679 struct elf64_x86_64_dyn_relocs *p;
681 /* Add reloc counts against the indirect sym to the direct sym
682 list. Merge any entries against the same section. */
683 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
685 struct elf64_x86_64_dyn_relocs *q;
687 for (q = edir->dyn_relocs; q != NULL; q = q->next)
688 if (q->sec == p->sec)
690 q->pc_count += p->pc_count;
691 q->count += p->count;
698 *pp = edir->dyn_relocs;
701 edir->dyn_relocs = eind->dyn_relocs;
702 eind->dyn_relocs = NULL;
705 if (ind->root.type == bfd_link_hash_indirect
706 && dir->got.refcount <= 0)
708 edir->tls_type = eind->tls_type;
709 eind->tls_type = GOT_UNKNOWN;
712 if (ELIMINATE_COPY_RELOCS
713 && ind->root.type != bfd_link_hash_indirect
714 && dir->dynamic_adjusted)
716 /* If called to transfer flags for a weakdef during processing
717 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
718 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
719 dir->ref_dynamic |= ind->ref_dynamic;
720 dir->ref_regular |= ind->ref_regular;
721 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
722 dir->needs_plt |= ind->needs_plt;
723 dir->pointer_equality_needed |= ind->pointer_equality_needed;
726 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
730 elf64_x86_64_elf_object_p (bfd *abfd)
732 /* Set the right machine number for an x86-64 elf64 file. */
733 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
751 /* Return TRUE if the TLS access code sequence support transition
755 elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec,
757 Elf_Internal_Shdr *symtab_hdr,
758 struct elf_link_hash_entry **sym_hashes,
760 const Elf_Internal_Rela *rel,
761 const Elf_Internal_Rela *relend)
764 unsigned long r_symndx;
765 struct elf_link_hash_entry *h;
768 /* Get the section contents. */
769 if (contents == NULL)
771 if (elf_section_data (sec)->this_hdr.contents != NULL)
772 contents = elf_section_data (sec)->this_hdr.contents;
775 /* FIXME: How to better handle error condition? */
776 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
779 /* Cache the section contents for elf_link_input_bfd. */
780 elf_section_data (sec)->this_hdr.contents = contents;
784 offset = rel->r_offset;
789 if ((rel + 1) >= relend)
792 if (r_type == R_X86_64_TLSGD)
794 /* Check transition from GD access model. Only
795 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
796 .word 0x6666; rex64; call __tls_get_addr
797 can transit to different access model. */
799 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } },
800 call = { { 0x66, 0x66, 0x48, 0xe8 } };
802 || (offset + 12) > sec->size
803 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i
804 || bfd_get_32 (abfd, contents + offset + 4) != call.i)
809 /* Check transition from LD access model. Only
810 leaq foo@tlsld(%rip), %rdi;
812 can transit to different access model. */
814 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
817 if (offset < 3 || (offset + 9) > sec->size)
820 op.i = bfd_get_32 (abfd, contents + offset - 3);
821 op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
826 r_symndx = ELF64_R_SYM (rel[1].r_info);
827 if (r_symndx < symtab_hdr->sh_info)
830 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
831 /* Use strncmp to check __tls_get_addr since __tls_get_addr
834 && h->root.root.string != NULL
835 && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
836 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
837 && (strncmp (h->root.root.string,
838 "__tls_get_addr", 14) == 0));
840 case R_X86_64_GOTTPOFF:
841 /* Check transition from IE access model:
842 movq foo@gottpoff(%rip), %reg
843 addq foo@gottpoff(%rip), %reg
846 if (offset < 3 || (offset + 4) > sec->size)
849 val = bfd_get_8 (abfd, contents + offset - 3);
850 if (val != 0x48 && val != 0x4c)
853 val = bfd_get_8 (abfd, contents + offset - 2);
854 if (val != 0x8b && val != 0x03)
857 val = bfd_get_8 (abfd, contents + offset - 1);
858 return (val & 0xc7) == 5;
860 case R_X86_64_GOTPC32_TLSDESC:
861 /* Check transition from GDesc access model:
862 leaq x@tlsdesc(%rip), %rax
864 Make sure it's a leaq adding rip to a 32-bit offset
865 into any register, although it's probably almost always
868 if (offset < 3 || (offset + 4) > sec->size)
871 val = bfd_get_8 (abfd, contents + offset - 3);
872 if ((val & 0xfb) != 0x48)
875 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
878 val = bfd_get_8 (abfd, contents + offset - 1);
879 return (val & 0xc7) == 0x05;
881 case R_X86_64_TLSDESC_CALL:
882 /* Check transition from GDesc access model:
883 call *x@tlsdesc(%rax)
885 if (offset + 2 <= sec->size)
887 /* Make sure that it's a call *x@tlsdesc(%rax). */
888 static x86_64_opcode16 call = { { 0xff, 0x10 } };
889 return bfd_get_16 (abfd, contents + offset) == call.i;
899 /* Return TRUE if the TLS access transition is OK or no transition
900 will be performed. Update R_TYPE if there is a transition. */
903 elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
904 asection *sec, bfd_byte *contents,
905 Elf_Internal_Shdr *symtab_hdr,
906 struct elf_link_hash_entry **sym_hashes,
907 unsigned int *r_type, int tls_type,
908 const Elf_Internal_Rela *rel,
909 const Elf_Internal_Rela *relend,
910 struct elf_link_hash_entry *h)
912 unsigned int from_type = *r_type;
913 unsigned int to_type = from_type;
914 bfd_boolean check = TRUE;
919 case R_X86_64_GOTPC32_TLSDESC:
920 case R_X86_64_TLSDESC_CALL:
921 case R_X86_64_GOTTPOFF:
925 to_type = R_X86_64_TPOFF32;
927 to_type = R_X86_64_GOTTPOFF;
930 /* When we are called from elf64_x86_64_relocate_section,
931 CONTENTS isn't NULL and there may be additional transitions
932 based on TLS_TYPE. */
933 if (contents != NULL)
935 unsigned int new_to_type = to_type;
940 && tls_type == GOT_TLS_IE)
941 new_to_type = R_X86_64_TPOFF32;
943 if (to_type == R_X86_64_TLSGD
944 || to_type == R_X86_64_GOTPC32_TLSDESC
945 || to_type == R_X86_64_TLSDESC_CALL)
947 if (tls_type == GOT_TLS_IE)
948 new_to_type = R_X86_64_GOTTPOFF;
951 /* We checked the transition before when we were called from
952 elf64_x86_64_check_relocs. We only want to check the new
953 transition which hasn't been checked before. */
954 check = new_to_type != to_type && from_type == to_type;
955 to_type = new_to_type;
962 to_type = R_X86_64_TPOFF32;
969 /* Return TRUE if there is no transition. */
970 if (from_type == to_type)
973 /* Check if the transition can be performed. */
975 && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
976 symtab_hdr, sym_hashes,
977 from_type, rel, relend))
979 reloc_howto_type *from, *to;
981 from = elf64_x86_64_rtype_to_howto (abfd, from_type);
982 to = elf64_x86_64_rtype_to_howto (abfd, to_type);
984 (*_bfd_error_handler)
985 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
986 "in section `%A' failed"),
987 abfd, sec, from->name, to->name,
988 h ? h->root.root.string : "a local symbol",
989 (unsigned long) rel->r_offset);
990 bfd_set_error (bfd_error_bad_value);
998 /* Look through the relocs for a section during the first phase, and
999 calculate needed space in the global offset table, procedure
1000 linkage table, and dynamic reloc sections. */
1003 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1005 const Elf_Internal_Rela *relocs)
1007 struct elf64_x86_64_link_hash_table *htab;
1008 Elf_Internal_Shdr *symtab_hdr;
1009 struct elf_link_hash_entry **sym_hashes;
1010 const Elf_Internal_Rela *rel;
1011 const Elf_Internal_Rela *rel_end;
1014 if (info->relocatable)
1017 BFD_ASSERT (is_x86_64_elf (abfd));
1019 htab = elf64_x86_64_hash_table (info);
1020 symtab_hdr = &elf_symtab_hdr (abfd);
1021 sym_hashes = elf_sym_hashes (abfd);
1025 rel_end = relocs + sec->reloc_count;
1026 for (rel = relocs; rel < rel_end; rel++)
1028 unsigned int r_type;
1029 unsigned long r_symndx;
1030 struct elf_link_hash_entry *h;
1032 r_symndx = ELF64_R_SYM (rel->r_info);
1033 r_type = ELF64_R_TYPE (rel->r_info);
1035 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1037 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1042 if (r_symndx < symtab_hdr->sh_info)
1046 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1047 while (h->root.type == bfd_link_hash_indirect
1048 || h->root.type == bfd_link_hash_warning)
1049 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1051 /* Create the ifunc sections for static executables. If we
1052 never see an indirect function symbol nor we are building
1053 a static executable, those sections will be empty and
1054 won't appear in output. */
1065 case R_X86_64_PLT32:
1066 case R_X86_64_GOTPCREL:
1067 case R_X86_64_GOTPCREL64:
1068 if (!info->shared && htab->iplt == NULL)
1070 if (!_bfd_elf_create_static_ifunc_sections (abfd,
1074 htab->iplt = bfd_get_section_by_name (abfd, ".iplt");
1075 htab->irelplt = bfd_get_section_by_name (abfd,
1077 htab->igotplt = bfd_get_section_by_name (abfd,
1087 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1088 it here if it is defined in a non-shared object. */
1089 if (h->type == STT_GNU_IFUNC
1092 /* It is referenced by a non-shared object. */
1095 /* STT_GNU_IFUNC symbol must go through PLT. */
1096 h->plt.refcount += 1;
1098 /* STT_GNU_IFUNC needs dynamic sections. */
1099 if (htab->elf.dynobj == NULL)
1100 htab->elf.dynobj = abfd;
1105 (*_bfd_error_handler)
1106 (_("%B: relocation %s against STT_GNU_IFUNC "
1107 "symbol `%s' isn't handled by %s"), abfd,
1108 x86_64_elf_howto_table[r_type].name,
1109 h->root.root.string, __FUNCTION__);
1110 bfd_set_error (bfd_error_bad_value);
1119 if (r_type != R_X86_64_PC32
1120 && r_type != R_X86_64_PC64)
1121 h->pointer_equality_needed = 1;
1124 case R_X86_64_PLT32:
1127 case R_X86_64_GOTPCREL:
1128 case R_X86_64_GOTPCREL64:
1129 h->got.refcount += 1;
1130 if (htab->sgot == NULL
1131 && !elf64_x86_64_create_got_section (htab->elf.dynobj,
1141 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1142 symtab_hdr, sym_hashes,
1143 &r_type, GOT_UNKNOWN,
1149 case R_X86_64_TLSLD:
1150 htab->tls_ld_got.refcount += 1;
1153 case R_X86_64_TPOFF32:
1156 (*_bfd_error_handler)
1157 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1159 x86_64_elf_howto_table[r_type].name,
1160 (h) ? h->root.root.string : "a local symbol");
1161 bfd_set_error (bfd_error_bad_value);
1166 case R_X86_64_GOTTPOFF:
1168 info->flags |= DF_STATIC_TLS;
1171 case R_X86_64_GOT32:
1172 case R_X86_64_GOTPCREL:
1173 case R_X86_64_TLSGD:
1174 case R_X86_64_GOT64:
1175 case R_X86_64_GOTPCREL64:
1176 case R_X86_64_GOTPLT64:
1177 case R_X86_64_GOTPC32_TLSDESC:
1178 case R_X86_64_TLSDESC_CALL:
1179 /* This symbol requires a global offset table entry. */
1181 int tls_type, old_tls_type;
1185 default: tls_type = GOT_NORMAL; break;
1186 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1187 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1188 case R_X86_64_GOTPC32_TLSDESC:
1189 case R_X86_64_TLSDESC_CALL:
1190 tls_type = GOT_TLS_GDESC; break;
1195 if (r_type == R_X86_64_GOTPLT64)
1197 /* This relocation indicates that we also need
1198 a PLT entry, as this is a function. We don't need
1199 a PLT entry for local symbols. */
1201 h->plt.refcount += 1;
1203 h->got.refcount += 1;
1204 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1208 bfd_signed_vma *local_got_refcounts;
1210 /* This is a global offset table entry for a local symbol. */
1211 local_got_refcounts = elf_local_got_refcounts (abfd);
1212 if (local_got_refcounts == NULL)
1216 size = symtab_hdr->sh_info;
1217 size *= sizeof (bfd_signed_vma)
1218 + sizeof (bfd_vma) + sizeof (char);
1219 local_got_refcounts = ((bfd_signed_vma *)
1220 bfd_zalloc (abfd, size));
1221 if (local_got_refcounts == NULL)
1223 elf_local_got_refcounts (abfd) = local_got_refcounts;
1224 elf64_x86_64_local_tlsdesc_gotent (abfd)
1225 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1226 elf64_x86_64_local_got_tls_type (abfd)
1227 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1229 local_got_refcounts[r_symndx] += 1;
1231 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1234 /* If a TLS symbol is accessed using IE at least once,
1235 there is no point to use dynamic model for it. */
1236 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1237 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1238 || tls_type != GOT_TLS_IE))
1240 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1241 tls_type = old_tls_type;
1242 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1243 && GOT_TLS_GD_ANY_P (tls_type))
1244 tls_type |= old_tls_type;
1247 (*_bfd_error_handler)
1248 (_("%B: '%s' accessed both as normal and thread local symbol"),
1249 abfd, h ? h->root.root.string : "<local>");
1254 if (old_tls_type != tls_type)
1257 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1259 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1264 case R_X86_64_GOTOFF64:
1265 case R_X86_64_GOTPC32:
1266 case R_X86_64_GOTPC64:
1268 if (htab->sgot == NULL)
1270 if (htab->elf.dynobj == NULL)
1271 htab->elf.dynobj = abfd;
1272 if (!elf64_x86_64_create_got_section (htab->elf.dynobj,
1278 case R_X86_64_PLT32:
1279 /* This symbol requires a procedure linkage table entry. We
1280 actually build the entry in adjust_dynamic_symbol,
1281 because this might be a case of linking PIC code which is
1282 never referenced by a dynamic object, in which case we
1283 don't need to generate a procedure linkage table entry
1286 /* If this is a local symbol, we resolve it directly without
1287 creating a procedure linkage table entry. */
1292 h->plt.refcount += 1;
1295 case R_X86_64_PLTOFF64:
1296 /* This tries to form the 'address' of a function relative
1297 to GOT. For global symbols we need a PLT entry. */
1301 h->plt.refcount += 1;
1309 /* Let's help debug shared library creation. These relocs
1310 cannot be used in shared libs. Don't error out for
1311 sections we don't care about, such as debug sections or
1312 non-constant sections. */
1314 && (sec->flags & SEC_ALLOC) != 0
1315 && (sec->flags & SEC_READONLY) != 0)
1317 (*_bfd_error_handler)
1318 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1320 x86_64_elf_howto_table[r_type].name,
1321 (h) ? h->root.root.string : "a local symbol");
1322 bfd_set_error (bfd_error_bad_value);
1332 if (h != NULL && !info->shared)
1334 /* If this reloc is in a read-only section, we might
1335 need a copy reloc. We can't check reliably at this
1336 stage whether the section is read-only, as input
1337 sections have not yet been mapped to output sections.
1338 Tentatively set the flag for now, and correct in
1339 adjust_dynamic_symbol. */
1342 /* We may need a .plt entry if the function this reloc
1343 refers to is in a shared lib. */
1344 h->plt.refcount += 1;
1345 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1346 h->pointer_equality_needed = 1;
1349 /* If we are creating a shared library, and this is a reloc
1350 against a global symbol, or a non PC relative reloc
1351 against a local symbol, then we need to copy the reloc
1352 into the shared library. However, if we are linking with
1353 -Bsymbolic, we do not need to copy a reloc against a
1354 global symbol which is defined in an object we are
1355 including in the link (i.e., DEF_REGULAR is set). At
1356 this point we have not seen all the input files, so it is
1357 possible that DEF_REGULAR is not set now but will be set
1358 later (it is never cleared). In case of a weak definition,
1359 DEF_REGULAR may be cleared later by a strong definition in
1360 a shared library. We account for that possibility below by
1361 storing information in the relocs_copied field of the hash
1362 table entry. A similar situation occurs when creating
1363 shared libraries and symbol visibility changes render the
1366 If on the other hand, we are creating an executable, we
1367 may need to keep relocations for symbols satisfied by a
1368 dynamic library if we manage to avoid copy relocs for the
1371 && (sec->flags & SEC_ALLOC) != 0
1372 && (! IS_X86_64_PCREL_TYPE (r_type)
1374 && (! SYMBOLIC_BIND (info, h)
1375 || h->root.type == bfd_link_hash_defweak
1376 || !h->def_regular))))
1377 || (ELIMINATE_COPY_RELOCS
1379 && (sec->flags & SEC_ALLOC) != 0
1381 && (h->root.type == bfd_link_hash_defweak
1382 || !h->def_regular)))
1384 struct elf64_x86_64_dyn_relocs *p;
1385 struct elf64_x86_64_dyn_relocs **head;
1387 /* We must copy these reloc types into the output file.
1388 Create a reloc section in dynobj and make room for
1392 if (htab->elf.dynobj == NULL)
1393 htab->elf.dynobj = abfd;
1395 sreloc = _bfd_elf_make_dynamic_reloc_section
1396 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
1402 /* If this is a global symbol, we count the number of
1403 relocations we need for this symbol. */
1406 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1411 /* Track dynamic relocs needed for local syms too.
1412 We really need local syms available to do this
1416 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1421 /* Beware of type punned pointers vs strict aliasing
1423 vpp = &(elf_section_data (s)->local_dynrel);
1424 head = (struct elf64_x86_64_dyn_relocs **)vpp;
1428 if (p == NULL || p->sec != sec)
1430 bfd_size_type amt = sizeof *p;
1432 p = ((struct elf64_x86_64_dyn_relocs *)
1433 bfd_alloc (htab->elf.dynobj, amt));
1444 if (IS_X86_64_PCREL_TYPE (r_type))
1449 /* This relocation describes the C++ object vtable hierarchy.
1450 Reconstruct it for later use during GC. */
1451 case R_X86_64_GNU_VTINHERIT:
1452 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1456 /* This relocation describes which C++ vtable entries are actually
1457 used. Record for later use during GC. */
1458 case R_X86_64_GNU_VTENTRY:
1459 BFD_ASSERT (h != NULL);
1461 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1473 /* Return the section that should be marked against GC for a given
1477 elf64_x86_64_gc_mark_hook (asection *sec,
1478 struct bfd_link_info *info,
1479 Elf_Internal_Rela *rel,
1480 struct elf_link_hash_entry *h,
1481 Elf_Internal_Sym *sym)
1484 switch (ELF64_R_TYPE (rel->r_info))
1486 case R_X86_64_GNU_VTINHERIT:
1487 case R_X86_64_GNU_VTENTRY:
1491 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1494 /* Update the got entry reference counts for the section being removed. */
1497 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1499 const Elf_Internal_Rela *relocs)
1501 Elf_Internal_Shdr *symtab_hdr;
1502 struct elf_link_hash_entry **sym_hashes;
1503 bfd_signed_vma *local_got_refcounts;
1504 const Elf_Internal_Rela *rel, *relend;
1506 if (info->relocatable)
1509 elf_section_data (sec)->local_dynrel = NULL;
1511 symtab_hdr = &elf_symtab_hdr (abfd);
1512 sym_hashes = elf_sym_hashes (abfd);
1513 local_got_refcounts = elf_local_got_refcounts (abfd);
1515 relend = relocs + sec->reloc_count;
1516 for (rel = relocs; rel < relend; rel++)
1518 unsigned long r_symndx;
1519 unsigned int r_type;
1520 struct elf_link_hash_entry *h = NULL;
1522 r_symndx = ELF64_R_SYM (rel->r_info);
1523 if (r_symndx >= symtab_hdr->sh_info)
1525 struct elf64_x86_64_link_hash_entry *eh;
1526 struct elf64_x86_64_dyn_relocs **pp;
1527 struct elf64_x86_64_dyn_relocs *p;
1529 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1530 while (h->root.type == bfd_link_hash_indirect
1531 || h->root.type == bfd_link_hash_warning)
1532 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1533 eh = (struct elf64_x86_64_link_hash_entry *) h;
1535 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1538 /* Everything must go for SEC. */
1544 r_type = ELF64_R_TYPE (rel->r_info);
1545 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1546 symtab_hdr, sym_hashes,
1547 &r_type, GOT_UNKNOWN,
1553 case R_X86_64_TLSLD:
1554 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1555 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1558 case R_X86_64_TLSGD:
1559 case R_X86_64_GOTPC32_TLSDESC:
1560 case R_X86_64_TLSDESC_CALL:
1561 case R_X86_64_GOTTPOFF:
1562 case R_X86_64_GOT32:
1563 case R_X86_64_GOTPCREL:
1564 case R_X86_64_GOT64:
1565 case R_X86_64_GOTPCREL64:
1566 case R_X86_64_GOTPLT64:
1569 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1570 h->plt.refcount -= 1;
1571 if (h->got.refcount > 0)
1572 h->got.refcount -= 1;
1574 else if (local_got_refcounts != NULL)
1576 if (local_got_refcounts[r_symndx] > 0)
1577 local_got_refcounts[r_symndx] -= 1;
1594 case R_X86_64_PLT32:
1595 case R_X86_64_PLTOFF64:
1598 if (h->plt.refcount > 0)
1599 h->plt.refcount -= 1;
1611 /* Adjust a symbol defined by a dynamic object and referenced by a
1612 regular object. The current definition is in some section of the
1613 dynamic object, but we're not including those sections. We have to
1614 change the definition to something the rest of the link can
1618 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1619 struct elf_link_hash_entry *h)
1621 struct elf64_x86_64_link_hash_table *htab;
1624 /* STT_GNU_IFUNC symbol must go through PLT. */
1625 if (h->type == STT_GNU_IFUNC)
1627 if (h->plt.refcount <= 0)
1629 h->plt.offset = (bfd_vma) -1;
1635 /* If this is a function, put it in the procedure linkage table. We
1636 will fill in the contents of the procedure linkage table later,
1637 when we know the address of the .got section. */
1638 if (h->type == STT_FUNC
1641 if (h->plt.refcount <= 0
1642 || SYMBOL_CALLS_LOCAL (info, h)
1643 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1644 && h->root.type == bfd_link_hash_undefweak))
1646 /* This case can occur if we saw a PLT32 reloc in an input
1647 file, but the symbol was never referred to by a dynamic
1648 object, or if all references were garbage collected. In
1649 such a case, we don't actually need to build a procedure
1650 linkage table, and we can just do a PC32 reloc instead. */
1651 h->plt.offset = (bfd_vma) -1;
1658 /* It's possible that we incorrectly decided a .plt reloc was
1659 needed for an R_X86_64_PC32 reloc to a non-function sym in
1660 check_relocs. We can't decide accurately between function and
1661 non-function syms in check-relocs; Objects loaded later in
1662 the link may change h->type. So fix it now. */
1663 h->plt.offset = (bfd_vma) -1;
1665 /* If this is a weak symbol, and there is a real definition, the
1666 processor independent code will have arranged for us to see the
1667 real definition first, and we can just use the same value. */
1668 if (h->u.weakdef != NULL)
1670 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1671 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1672 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1673 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1674 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1675 h->non_got_ref = h->u.weakdef->non_got_ref;
1679 /* This is a reference to a symbol defined by a dynamic object which
1680 is not a function. */
1682 /* If we are creating a shared library, we must presume that the
1683 only references to the symbol are via the global offset table.
1684 For such cases we need not do anything here; the relocations will
1685 be handled correctly by relocate_section. */
1689 /* If there are no references to this symbol that do not use the
1690 GOT, we don't need to generate a copy reloc. */
1691 if (!h->non_got_ref)
1694 /* If -z nocopyreloc was given, we won't generate them either. */
1695 if (info->nocopyreloc)
1701 if (ELIMINATE_COPY_RELOCS)
1703 struct elf64_x86_64_link_hash_entry * eh;
1704 struct elf64_x86_64_dyn_relocs *p;
1706 eh = (struct elf64_x86_64_link_hash_entry *) h;
1707 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1709 s = p->sec->output_section;
1710 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1714 /* If we didn't find any dynamic relocs in read-only sections, then
1715 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1725 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1726 h->root.root.string);
1730 /* We must allocate the symbol in our .dynbss section, which will
1731 become part of the .bss section of the executable. There will be
1732 an entry for this symbol in the .dynsym section. The dynamic
1733 object will contain position independent code, so all references
1734 from the dynamic object to this symbol will go through the global
1735 offset table. The dynamic linker will use the .dynsym entry to
1736 determine the address it must put in the global offset table, so
1737 both the dynamic object and the regular object will refer to the
1738 same memory location for the variable. */
1740 htab = elf64_x86_64_hash_table (info);
1742 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1743 to copy the initial value out of the dynamic object and into the
1744 runtime process image. */
1745 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1747 htab->srelbss->size += sizeof (Elf64_External_Rela);
1753 return _bfd_elf_adjust_dynamic_copy (h, s);
1756 /* Allocate space in .plt, .got and associated reloc sections for
1760 elf64_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1762 struct bfd_link_info *info;
1763 struct elf64_x86_64_link_hash_table *htab;
1764 struct elf64_x86_64_link_hash_entry *eh;
1765 struct elf64_x86_64_dyn_relocs *p;
1767 if (h->root.type == bfd_link_hash_indirect)
1770 if (h->root.type == bfd_link_hash_warning)
1771 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1772 eh = (struct elf64_x86_64_link_hash_entry *) h;
1774 info = (struct bfd_link_info *) inf;
1775 htab = elf64_x86_64_hash_table (info);
1777 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1778 here if it is defined and referenced in a non-shared object. */
1779 if (h->type == STT_GNU_IFUNC
1782 asection *plt, *gotplt, *relplt;
1784 /* Return and discard space for dynamic relocations against it if
1785 it is never referenced in a non-shared object. */
1786 if (!h->ref_regular)
1788 if (h->plt.refcount > 0
1789 || h->got.refcount > 0)
1791 h->got.offset = (bfd_vma) -1;
1792 eh->dyn_relocs = NULL;
1796 if (h->plt.refcount <= 0)
1799 /* When building a static executable, use .iplt, .igot.plt and
1800 .rela.iplt sections for STT_GNU_IFUNC symbols. */
1801 if (htab->splt != 0)
1804 gotplt = htab->sgotplt;
1805 relplt = htab->srelplt;
1807 /* If this is the first .plt entry, make room for the special
1810 plt->size += PLT_ENTRY_SIZE;
1815 gotplt = htab->igotplt;
1816 relplt = htab->irelplt;
1819 /* Don't update value of STT_GNU_IFUNC symbol to PLT. We need
1820 the original value for R_X86_64_IRELATIVE. */
1821 h->plt.offset = plt->size;
1823 /* Make room for this entry in the .plt/.iplt section. */
1824 plt->size += PLT_ENTRY_SIZE;
1826 /* We also need to make an entry in the .got.plt/.got.iplt
1827 section, which will be placed in the .got section by the
1829 gotplt->size += GOT_ENTRY_SIZE;
1831 /* We also need to make an entry in the .rela.plt/.rela.iplt
1833 relplt->size += sizeof (Elf64_External_Rela);
1834 relplt->reloc_count++;
1836 /* No need for dynamic relocation for local STT_GNU_IFUNC symbol.
1837 Discard space for relocations against it. */
1838 if (h->dynindx == -1 || h->forced_local)
1839 eh->dyn_relocs = NULL;
1841 /* STT_GNU_IFUNC symbol uses .got.plt, not .got. But for
1842 shared library, we must go through GOT and we can't
1843 use R_X86_64_IRELATIVE unless it is forced local. */
1847 h->got.refcount = 0;
1849 else if (htab->elf.dynamic_sections_created
1850 && h->plt.refcount > 0)
1852 /* Make sure this symbol is output as a dynamic symbol.
1853 Undefined weak syms won't yet be marked as dynamic. */
1854 if (h->dynindx == -1
1855 && !h->forced_local)
1857 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1862 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1864 asection *s = htab->splt;
1866 /* If this is the first .plt entry, make room for the special
1869 s->size += PLT_ENTRY_SIZE;
1871 h->plt.offset = s->size;
1873 /* If this symbol is not defined in a regular file, and we are
1874 not generating a shared library, then set the symbol to this
1875 location in the .plt. This is required to make function
1876 pointers compare as equal between the normal executable and
1877 the shared library. */
1881 h->root.u.def.section = s;
1882 h->root.u.def.value = h->plt.offset;
1885 /* Make room for this entry. */
1886 s->size += PLT_ENTRY_SIZE;
1888 /* We also need to make an entry in the .got.plt section, which
1889 will be placed in the .got section by the linker script. */
1890 htab->sgotplt->size += GOT_ENTRY_SIZE;
1892 /* We also need to make an entry in the .rela.plt section. */
1893 htab->srelplt->size += sizeof (Elf64_External_Rela);
1894 htab->srelplt->reloc_count++;
1898 h->plt.offset = (bfd_vma) -1;
1904 h->plt.offset = (bfd_vma) -1;
1908 eh->tlsdesc_got = (bfd_vma) -1;
1910 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1911 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1912 if (h->got.refcount > 0
1915 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1917 h->got.offset = (bfd_vma) -1;
1919 else if (h->got.refcount > 0)
1923 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1925 /* Make sure this symbol is output as a dynamic symbol.
1926 Undefined weak syms won't yet be marked as dynamic. */
1927 if (h->dynindx == -1
1928 && !h->forced_local)
1930 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1934 if (GOT_TLS_GDESC_P (tls_type))
1936 eh->tlsdesc_got = htab->sgotplt->size
1937 - elf64_x86_64_compute_jump_table_size (htab);
1938 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1939 h->got.offset = (bfd_vma) -2;
1941 if (! GOT_TLS_GDESC_P (tls_type)
1942 || GOT_TLS_GD_P (tls_type))
1945 h->got.offset = s->size;
1946 s->size += GOT_ENTRY_SIZE;
1947 if (GOT_TLS_GD_P (tls_type))
1948 s->size += GOT_ENTRY_SIZE;
1950 dyn = htab->elf.dynamic_sections_created;
1951 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1953 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1954 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1955 || tls_type == GOT_TLS_IE)
1956 htab->srelgot->size += sizeof (Elf64_External_Rela);
1957 else if (GOT_TLS_GD_P (tls_type))
1958 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
1959 else if (! GOT_TLS_GDESC_P (tls_type)
1960 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1961 || h->root.type != bfd_link_hash_undefweak)
1963 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1964 htab->srelgot->size += sizeof (Elf64_External_Rela);
1965 if (GOT_TLS_GDESC_P (tls_type))
1967 htab->srelplt->size += sizeof (Elf64_External_Rela);
1968 htab->tlsdesc_plt = (bfd_vma) -1;
1972 h->got.offset = (bfd_vma) -1;
1974 if (eh->dyn_relocs == NULL)
1977 /* In the shared -Bsymbolic case, discard space allocated for
1978 dynamic pc-relative relocs against symbols which turn out to be
1979 defined in regular objects. For the normal shared case, discard
1980 space for pc-relative relocs that have become local due to symbol
1981 visibility changes. */
1985 /* Relocs that use pc_count are those that appear on a call
1986 insn, or certain REL relocs that can generated via assembly.
1987 We want calls to protected symbols to resolve directly to the
1988 function rather than going via the plt. If people want
1989 function pointer comparisons to work as expected then they
1990 should avoid writing weird assembly. */
1991 if (SYMBOL_CALLS_LOCAL (info, h))
1993 struct elf64_x86_64_dyn_relocs **pp;
1995 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1997 p->count -= p->pc_count;
2006 /* Also discard relocs on undefined weak syms with non-default
2008 if (eh->dyn_relocs != NULL
2009 && h->root.type == bfd_link_hash_undefweak)
2011 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2012 eh->dyn_relocs = NULL;
2014 /* Make sure undefined weak symbols are output as a dynamic
2016 else if (h->dynindx == -1
2017 && ! h->forced_local
2018 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2023 else if (ELIMINATE_COPY_RELOCS)
2025 /* For the non-shared case, discard space for relocs against
2026 symbols which turn out to need copy relocs or are not
2032 || (htab->elf.dynamic_sections_created
2033 && (h->root.type == bfd_link_hash_undefweak
2034 || h->root.type == bfd_link_hash_undefined))))
2036 /* Make sure this symbol is output as a dynamic symbol.
2037 Undefined weak syms won't yet be marked as dynamic. */
2038 if (h->dynindx == -1
2039 && ! h->forced_local
2040 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2043 /* If that succeeded, we know we'll be keeping all the
2045 if (h->dynindx != -1)
2049 eh->dyn_relocs = NULL;
2054 /* Finally, allocate space. */
2055 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2059 sreloc = elf_section_data (p->sec)->sreloc;
2061 BFD_ASSERT (sreloc != NULL);
2063 sreloc->size += p->count * sizeof (Elf64_External_Rela);
2069 /* Find any dynamic relocs that apply to read-only sections. */
2072 elf64_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2074 struct elf64_x86_64_link_hash_entry *eh;
2075 struct elf64_x86_64_dyn_relocs *p;
2077 if (h->root.type == bfd_link_hash_warning)
2078 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2080 eh = (struct elf64_x86_64_link_hash_entry *) h;
2081 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2083 asection *s = p->sec->output_section;
2085 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2087 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2089 info->flags |= DF_TEXTREL;
2091 /* Not an error, just cut short the traversal. */
2098 /* Set the sizes of the dynamic sections. */
2101 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2102 struct bfd_link_info *info)
2104 struct elf64_x86_64_link_hash_table *htab;
2110 htab = elf64_x86_64_hash_table (info);
2111 dynobj = htab->elf.dynobj;
2115 if (htab->elf.dynamic_sections_created)
2117 /* Set the contents of the .interp section to the interpreter. */
2118 if (info->executable)
2120 s = bfd_get_section_by_name (dynobj, ".interp");
2123 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2124 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2128 /* Set up .got offsets for local syms, and space for local dynamic
2130 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2132 bfd_signed_vma *local_got;
2133 bfd_signed_vma *end_local_got;
2134 char *local_tls_type;
2135 bfd_vma *local_tlsdesc_gotent;
2136 bfd_size_type locsymcount;
2137 Elf_Internal_Shdr *symtab_hdr;
2140 if (! is_x86_64_elf (ibfd))
2143 for (s = ibfd->sections; s != NULL; s = s->next)
2145 struct elf64_x86_64_dyn_relocs *p;
2147 for (p = (struct elf64_x86_64_dyn_relocs *)
2148 (elf_section_data (s)->local_dynrel);
2152 if (!bfd_is_abs_section (p->sec)
2153 && bfd_is_abs_section (p->sec->output_section))
2155 /* Input section has been discarded, either because
2156 it is a copy of a linkonce section or due to
2157 linker script /DISCARD/, so we'll be discarding
2160 else if (p->count != 0)
2162 srel = elf_section_data (p->sec)->sreloc;
2163 srel->size += p->count * sizeof (Elf64_External_Rela);
2164 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2165 info->flags |= DF_TEXTREL;
2170 local_got = elf_local_got_refcounts (ibfd);
2174 symtab_hdr = &elf_symtab_hdr (ibfd);
2175 locsymcount = symtab_hdr->sh_info;
2176 end_local_got = local_got + locsymcount;
2177 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
2178 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
2180 srel = htab->srelgot;
2181 for (; local_got < end_local_got;
2182 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2184 *local_tlsdesc_gotent = (bfd_vma) -1;
2187 if (GOT_TLS_GDESC_P (*local_tls_type))
2189 *local_tlsdesc_gotent = htab->sgotplt->size
2190 - elf64_x86_64_compute_jump_table_size (htab);
2191 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
2192 *local_got = (bfd_vma) -2;
2194 if (! GOT_TLS_GDESC_P (*local_tls_type)
2195 || GOT_TLS_GD_P (*local_tls_type))
2197 *local_got = s->size;
2198 s->size += GOT_ENTRY_SIZE;
2199 if (GOT_TLS_GD_P (*local_tls_type))
2200 s->size += GOT_ENTRY_SIZE;
2203 || GOT_TLS_GD_ANY_P (*local_tls_type)
2204 || *local_tls_type == GOT_TLS_IE)
2206 if (GOT_TLS_GDESC_P (*local_tls_type))
2208 htab->srelplt->size += sizeof (Elf64_External_Rela);
2209 htab->tlsdesc_plt = (bfd_vma) -1;
2211 if (! GOT_TLS_GDESC_P (*local_tls_type)
2212 || GOT_TLS_GD_P (*local_tls_type))
2213 srel->size += sizeof (Elf64_External_Rela);
2217 *local_got = (bfd_vma) -1;
2221 if (htab->tls_ld_got.refcount > 0)
2223 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2225 htab->tls_ld_got.offset = htab->sgot->size;
2226 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
2227 htab->srelgot->size += sizeof (Elf64_External_Rela);
2230 htab->tls_ld_got.offset = -1;
2232 /* Allocate global sym .plt and .got entries, and space for global
2233 sym dynamic relocs. */
2234 elf_link_hash_traverse (&htab->elf, elf64_x86_64_allocate_dynrelocs,
2237 /* For every jump slot reserved in the sgotplt, reloc_count is
2238 incremented. However, when we reserve space for TLS descriptors,
2239 it's not incremented, so in order to compute the space reserved
2240 for them, it suffices to multiply the reloc count by the jump
2243 htab->sgotplt_jump_table_size
2244 = elf64_x86_64_compute_jump_table_size (htab);
2246 if (htab->tlsdesc_plt)
2248 /* If we're not using lazy TLS relocations, don't generate the
2249 PLT and GOT entries they require. */
2250 if ((info->flags & DF_BIND_NOW))
2251 htab->tlsdesc_plt = 0;
2254 htab->tlsdesc_got = htab->sgot->size;
2255 htab->sgot->size += GOT_ENTRY_SIZE;
2256 /* Reserve room for the initial entry.
2257 FIXME: we could probably do away with it in this case. */
2258 if (htab->splt->size == 0)
2259 htab->splt->size += PLT_ENTRY_SIZE;
2260 htab->tlsdesc_plt = htab->splt->size;
2261 htab->splt->size += PLT_ENTRY_SIZE;
2265 /* We now have determined the sizes of the various dynamic sections.
2266 Allocate memory for them. */
2268 for (s = dynobj->sections; s != NULL; s = s->next)
2270 if ((s->flags & SEC_LINKER_CREATED) == 0)
2275 || s == htab->sgotplt
2277 || s == htab->igotplt
2278 || s == htab->sdynbss)
2280 /* Strip this section if we don't need it; see the
2283 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2285 if (s->size != 0 && s != htab->srelplt)
2288 /* We use the reloc_count field as a counter if we need
2289 to copy relocs into the output file. */
2290 if (s != htab->srelplt)
2295 /* It's not one of our sections, so don't allocate space. */
2301 /* If we don't need this section, strip it from the
2302 output file. This is mostly to handle .rela.bss and
2303 .rela.plt. We must create both sections in
2304 create_dynamic_sections, because they must be created
2305 before the linker maps input sections to output
2306 sections. The linker does that before
2307 adjust_dynamic_symbol is called, and it is that
2308 function which decides whether anything needs to go
2309 into these sections. */
2311 s->flags |= SEC_EXCLUDE;
2315 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2318 /* Allocate memory for the section contents. We use bfd_zalloc
2319 here in case unused entries are not reclaimed before the
2320 section's contents are written out. This should not happen,
2321 but this way if it does, we get a R_X86_64_NONE reloc instead
2323 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2324 if (s->contents == NULL)
2328 if (htab->elf.dynamic_sections_created)
2330 /* Add some entries to the .dynamic section. We fill in the
2331 values later, in elf64_x86_64_finish_dynamic_sections, but we
2332 must add the entries now so that we get the correct size for
2333 the .dynamic section. The DT_DEBUG entry is filled in by the
2334 dynamic linker and used by the debugger. */
2335 #define add_dynamic_entry(TAG, VAL) \
2336 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2338 if (info->executable)
2340 if (!add_dynamic_entry (DT_DEBUG, 0))
2344 if (htab->splt->size != 0)
2346 if (!add_dynamic_entry (DT_PLTGOT, 0)
2347 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2348 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2349 || !add_dynamic_entry (DT_JMPREL, 0))
2352 if (htab->tlsdesc_plt
2353 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2354 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2360 if (!add_dynamic_entry (DT_RELA, 0)
2361 || !add_dynamic_entry (DT_RELASZ, 0)
2362 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2365 /* If any dynamic relocs apply to a read-only section,
2366 then we need a DT_TEXTREL entry. */
2367 if ((info->flags & DF_TEXTREL) == 0)
2368 elf_link_hash_traverse (&htab->elf,
2369 elf64_x86_64_readonly_dynrelocs,
2372 if ((info->flags & DF_TEXTREL) != 0)
2374 if (!add_dynamic_entry (DT_TEXTREL, 0))
2379 #undef add_dynamic_entry
2385 elf64_x86_64_always_size_sections (bfd *output_bfd,
2386 struct bfd_link_info *info)
2388 asection *tls_sec = elf_hash_table (info)->tls_sec;
2392 struct elf_link_hash_entry *tlsbase;
2394 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2395 "_TLS_MODULE_BASE_",
2396 FALSE, FALSE, FALSE);
2398 if (tlsbase && tlsbase->type == STT_TLS)
2400 struct bfd_link_hash_entry *bh = NULL;
2401 const struct elf_backend_data *bed
2402 = get_elf_backend_data (output_bfd);
2404 if (!(_bfd_generic_link_add_one_symbol
2405 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2406 tls_sec, 0, NULL, FALSE,
2407 bed->collect, &bh)))
2410 elf64_x86_64_hash_table (info)->tls_module_base = bh;
2412 tlsbase = (struct elf_link_hash_entry *)bh;
2413 tlsbase->def_regular = 1;
2414 tlsbase->other = STV_HIDDEN;
2415 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2422 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2423 executables. Rather than setting it to the beginning of the TLS
2424 section, we have to set it to the end. This function may be called
2425 multiple times, it is idempotent. */
2428 elf64_x86_64_set_tls_module_base (struct bfd_link_info *info)
2430 struct bfd_link_hash_entry *base;
2432 if (!info->executable)
2435 base = elf64_x86_64_hash_table (info)->tls_module_base;
2440 base->u.def.value = elf_hash_table (info)->tls_size;
2443 /* Return the base VMA address which should be subtracted from real addresses
2444 when resolving @dtpoff relocation.
2445 This is PT_TLS segment p_vaddr. */
2448 elf64_x86_64_dtpoff_base (struct bfd_link_info *info)
2450 /* If tls_sec is NULL, we should have signalled an error already. */
2451 if (elf_hash_table (info)->tls_sec == NULL)
2453 return elf_hash_table (info)->tls_sec->vma;
2456 /* Return the relocation value for @tpoff relocation
2457 if STT_TLS virtual address is ADDRESS. */
2460 elf64_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2462 struct elf_link_hash_table *htab = elf_hash_table (info);
2464 /* If tls_segment is NULL, we should have signalled an error already. */
2465 if (htab->tls_sec == NULL)
2467 return address - htab->tls_size - htab->tls_sec->vma;
2470 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2474 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2476 /* Opcode Instruction
2479 0x0f 0x8x conditional jump */
2481 && (contents [offset - 1] == 0xe8
2482 || contents [offset - 1] == 0xe9))
2484 && contents [offset - 2] == 0x0f
2485 && (contents [offset - 1] & 0xf0) == 0x80));
2488 /* Relocate an x86_64 ELF section. */
2491 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2492 bfd *input_bfd, asection *input_section,
2493 bfd_byte *contents, Elf_Internal_Rela *relocs,
2494 Elf_Internal_Sym *local_syms,
2495 asection **local_sections)
2497 struct elf64_x86_64_link_hash_table *htab;
2498 Elf_Internal_Shdr *symtab_hdr;
2499 struct elf_link_hash_entry **sym_hashes;
2500 bfd_vma *local_got_offsets;
2501 bfd_vma *local_tlsdesc_gotents;
2502 Elf_Internal_Rela *rel;
2503 Elf_Internal_Rela *relend;
2505 BFD_ASSERT (is_x86_64_elf (input_bfd));
2507 htab = elf64_x86_64_hash_table (info);
2508 symtab_hdr = &elf_symtab_hdr (input_bfd);
2509 sym_hashes = elf_sym_hashes (input_bfd);
2510 local_got_offsets = elf_local_got_offsets (input_bfd);
2511 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2513 elf64_x86_64_set_tls_module_base (info);
2516 relend = relocs + input_section->reloc_count;
2517 for (; rel < relend; rel++)
2519 unsigned int r_type;
2520 reloc_howto_type *howto;
2521 unsigned long r_symndx;
2522 struct elf_link_hash_entry *h;
2523 Elf_Internal_Sym *sym;
2525 bfd_vma off, offplt;
2527 bfd_boolean unresolved_reloc;
2528 bfd_reloc_status_type r;
2532 r_type = ELF64_R_TYPE (rel->r_info);
2533 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2534 || r_type == (int) R_X86_64_GNU_VTENTRY)
2537 if (r_type >= R_X86_64_max)
2539 bfd_set_error (bfd_error_bad_value);
2543 howto = x86_64_elf_howto_table + r_type;
2544 r_symndx = ELF64_R_SYM (rel->r_info);
2548 unresolved_reloc = FALSE;
2549 if (r_symndx < symtab_hdr->sh_info)
2551 sym = local_syms + r_symndx;
2552 sec = local_sections[r_symndx];
2554 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2560 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2561 r_symndx, symtab_hdr, sym_hashes,
2563 unresolved_reloc, warned);
2566 if (sec != NULL && elf_discarded_section (sec))
2568 /* For relocs against symbols from removed linkonce sections,
2569 or sections discarded by a linker script, we just want the
2570 section contents zeroed. Avoid any special processing. */
2571 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2577 if (info->relocatable)
2580 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2581 it here if it is defined in a non-shared object. */
2583 && h->type == STT_GNU_IFUNC
2589 if ((input_section->flags & SEC_ALLOC) == 0
2590 || h->plt.offset == (bfd_vma) -1)
2593 /* STT_GNU_IFUNC symbol must go through PLT. */
2594 plt = htab->splt ? htab->splt : htab->iplt;
2595 relocation = (plt->output_section->vma
2596 + plt->output_offset + h->plt.offset);
2601 (*_bfd_error_handler)
2602 (_("%B: relocation %s against STT_GNU_IFUNC "
2603 "symbol `%s' isn't handled by %s"), input_bfd,
2604 x86_64_elf_howto_table[r_type].name,
2605 h->root.root.string, __FUNCTION__);
2606 bfd_set_error (bfd_error_bad_value);
2610 if (!info->executable)
2617 case R_X86_64_PLT32:
2620 case R_X86_64_GOTPCREL:
2621 case R_X86_64_GOTPCREL64:
2622 base_got = htab->sgot;
2623 off = h->got.offset;
2625 if (base_got == NULL)
2628 if (off == (bfd_vma) -1)
2630 /* We can't use h->got.offset here to save state, or
2631 even just remember the offset, as finish_dynamic_symbol
2632 would use that as offset into .got. */
2634 if (htab->splt != NULL)
2636 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2637 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2638 base_got = htab->sgotplt;
2642 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2643 off = plt_index * GOT_ENTRY_SIZE;
2644 base_got = htab->igotplt;
2647 if (h->dynindx == -1
2651 /* This references the local defitionion. We must
2652 initialize this entry in the global offset table.
2653 Since the offset must always be a multiple of 8,
2654 we use the least significant bit to record
2655 whether we have initialized it already.
2657 When doing a dynamic link, we create a .rela.got
2658 relocation entry to initialize the value. This
2659 is done in the finish_dynamic_symbol routine. */
2664 bfd_put_64 (output_bfd, relocation,
2665 base_got->contents + off);
2666 /* Note that this is harmless for the GOTPLT64
2667 case, as -1 | 1 still is -1. */
2673 relocation = (base_got->output_section->vma
2674 + base_got->output_offset + off);
2676 if (r_type != R_X86_64_GOTPCREL
2677 && r_type != R_X86_64_GOTPCREL64)
2680 if (htab->splt != NULL)
2681 gotplt = htab->sgotplt;
2683 gotplt = htab->igotplt;
2684 relocation -= (gotplt->output_section->vma
2685 - gotplt->output_offset);
2692 /* When generating a shared object, the relocations handled here are
2693 copied into the output file to be resolved at run time. */
2696 case R_X86_64_GOT32:
2697 case R_X86_64_GOT64:
2698 /* Relocation is to the entry for this symbol in the global
2700 case R_X86_64_GOTPCREL:
2701 case R_X86_64_GOTPCREL64:
2702 /* Use global offset table entry as symbol value. */
2703 case R_X86_64_GOTPLT64:
2704 /* This is the same as GOT64 for relocation purposes, but
2705 indicates the existence of a PLT entry. The difficulty is,
2706 that we must calculate the GOT slot offset from the PLT
2707 offset, if this symbol got a PLT entry (it was global).
2708 Additionally if it's computed from the PLT entry, then that
2709 GOT offset is relative to .got.plt, not to .got. */
2710 base_got = htab->sgot;
2712 if (htab->sgot == NULL)
2719 off = h->got.offset;
2721 && h->plt.offset != (bfd_vma)-1
2722 && off == (bfd_vma)-1)
2724 /* We can't use h->got.offset here to save
2725 state, or even just remember the offset, as
2726 finish_dynamic_symbol would use that as offset into
2728 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2729 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2730 base_got = htab->sgotplt;
2733 dyn = htab->elf.dynamic_sections_created;
2735 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2737 && SYMBOL_REFERENCES_LOCAL (info, h))
2738 || (ELF_ST_VISIBILITY (h->other)
2739 && h->root.type == bfd_link_hash_undefweak))
2741 /* This is actually a static link, or it is a -Bsymbolic
2742 link and the symbol is defined locally, or the symbol
2743 was forced to be local because of a version file. We
2744 must initialize this entry in the global offset table.
2745 Since the offset must always be a multiple of 8, we
2746 use the least significant bit to record whether we
2747 have initialized it already.
2749 When doing a dynamic link, we create a .rela.got
2750 relocation entry to initialize the value. This is
2751 done in the finish_dynamic_symbol routine. */
2756 bfd_put_64 (output_bfd, relocation,
2757 base_got->contents + off);
2758 /* Note that this is harmless for the GOTPLT64 case,
2759 as -1 | 1 still is -1. */
2764 unresolved_reloc = FALSE;
2768 if (local_got_offsets == NULL)
2771 off = local_got_offsets[r_symndx];
2773 /* The offset must always be a multiple of 8. We use
2774 the least significant bit to record whether we have
2775 already generated the necessary reloc. */
2780 bfd_put_64 (output_bfd, relocation,
2781 base_got->contents + off);
2786 Elf_Internal_Rela outrel;
2789 /* We need to generate a R_X86_64_RELATIVE reloc
2790 for the dynamic linker. */
2795 outrel.r_offset = (base_got->output_section->vma
2796 + base_got->output_offset
2798 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2799 outrel.r_addend = relocation;
2801 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2802 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2805 local_got_offsets[r_symndx] |= 1;
2809 if (off >= (bfd_vma) -2)
2812 relocation = base_got->output_section->vma
2813 + base_got->output_offset + off;
2814 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
2815 relocation -= htab->sgotplt->output_section->vma
2816 - htab->sgotplt->output_offset;
2820 case R_X86_64_GOTOFF64:
2821 /* Relocation is relative to the start of the global offset
2824 /* Check to make sure it isn't a protected function symbol
2825 for shared library since it may not be local when used
2826 as function address. */
2830 && h->type == STT_FUNC
2831 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2833 (*_bfd_error_handler)
2834 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2835 input_bfd, h->root.root.string);
2836 bfd_set_error (bfd_error_bad_value);
2840 /* Note that sgot is not involved in this
2841 calculation. We always want the start of .got.plt. If we
2842 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2843 permitted by the ABI, we might have to change this
2845 relocation -= htab->sgotplt->output_section->vma
2846 + htab->sgotplt->output_offset;
2849 case R_X86_64_GOTPC32:
2850 case R_X86_64_GOTPC64:
2851 /* Use global offset table as symbol value. */
2852 relocation = htab->sgotplt->output_section->vma
2853 + htab->sgotplt->output_offset;
2854 unresolved_reloc = FALSE;
2857 case R_X86_64_PLTOFF64:
2858 /* Relocation is PLT entry relative to GOT. For local
2859 symbols it's the symbol itself relative to GOT. */
2861 /* See PLT32 handling. */
2862 && h->plt.offset != (bfd_vma) -1
2863 && htab->splt != NULL)
2865 relocation = (htab->splt->output_section->vma
2866 + htab->splt->output_offset
2868 unresolved_reloc = FALSE;
2871 relocation -= htab->sgotplt->output_section->vma
2872 + htab->sgotplt->output_offset;
2875 case R_X86_64_PLT32:
2876 /* Relocation is to the entry for this symbol in the
2877 procedure linkage table. */
2879 /* Resolve a PLT32 reloc against a local symbol directly,
2880 without using the procedure linkage table. */
2884 if (h->plt.offset == (bfd_vma) -1
2885 || htab->splt == NULL)
2887 /* We didn't make a PLT entry for this symbol. This
2888 happens when statically linking PIC code, or when
2889 using -Bsymbolic. */
2893 relocation = (htab->splt->output_section->vma
2894 + htab->splt->output_offset
2896 unresolved_reloc = FALSE;
2903 && (input_section->flags & SEC_ALLOC) != 0
2904 && (input_section->flags & SEC_READONLY) != 0
2907 bfd_boolean fail = FALSE;
2909 = (r_type == R_X86_64_PC32
2910 && is_32bit_relative_branch (contents, rel->r_offset));
2912 if (SYMBOL_REFERENCES_LOCAL (info, h))
2914 /* Symbol is referenced locally. Make sure it is
2915 defined locally or for a branch. */
2916 fail = !h->def_regular && !branch;
2920 /* Symbol isn't referenced locally. We only allow
2921 branch to symbol with non-default visibility. */
2923 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
2930 const char *pic = "";
2932 switch (ELF_ST_VISIBILITY (h->other))
2935 v = _("hidden symbol");
2938 v = _("internal symbol");
2941 v = _("protected symbol");
2945 pic = _("; recompile with -fPIC");
2950 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
2952 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
2954 (*_bfd_error_handler) (fmt, input_bfd,
2955 x86_64_elf_howto_table[r_type].name,
2956 v, h->root.root.string, pic);
2957 bfd_set_error (bfd_error_bad_value);
2968 /* FIXME: The ABI says the linker should make sure the value is
2969 the same when it's zeroextended to 64 bit. */
2971 if ((input_section->flags & SEC_ALLOC) == 0)
2976 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2977 || h->root.type != bfd_link_hash_undefweak)
2978 && (! IS_X86_64_PCREL_TYPE (r_type)
2979 || ! SYMBOL_CALLS_LOCAL (info, h)))
2980 || (ELIMINATE_COPY_RELOCS
2987 || h->root.type == bfd_link_hash_undefweak
2988 || h->root.type == bfd_link_hash_undefined)))
2990 Elf_Internal_Rela outrel;
2992 bfd_boolean skip, relocate;
2995 /* When generating a shared object, these relocations
2996 are copied into the output file to be resolved at run
3002 _bfd_elf_section_offset (output_bfd, info, input_section,
3004 if (outrel.r_offset == (bfd_vma) -1)
3006 else if (outrel.r_offset == (bfd_vma) -2)
3007 skip = TRUE, relocate = TRUE;
3009 outrel.r_offset += (input_section->output_section->vma
3010 + input_section->output_offset);
3013 memset (&outrel, 0, sizeof outrel);
3015 /* h->dynindx may be -1 if this symbol was marked to
3019 && (IS_X86_64_PCREL_TYPE (r_type)
3021 || ! SYMBOLIC_BIND (info, h)
3022 || ! h->def_regular))
3024 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3025 outrel.r_addend = rel->r_addend;
3029 /* This symbol is local, or marked to become local. */
3030 if (r_type == R_X86_64_64)
3033 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3034 outrel.r_addend = relocation + rel->r_addend;
3040 if (bfd_is_abs_section (sec))
3042 else if (sec == NULL || sec->owner == NULL)
3044 bfd_set_error (bfd_error_bad_value);
3051 /* We are turning this relocation into one
3052 against a section symbol. It would be
3053 proper to subtract the symbol's value,
3054 osec->vma, from the emitted reloc addend,
3055 but ld.so expects buggy relocs. */
3056 osec = sec->output_section;
3057 sindx = elf_section_data (osec)->dynindx;
3060 asection *oi = htab->elf.text_index_section;
3061 sindx = elf_section_data (oi)->dynindx;
3063 BFD_ASSERT (sindx != 0);
3066 outrel.r_info = ELF64_R_INFO (sindx, r_type);
3067 outrel.r_addend = relocation + rel->r_addend;
3071 sreloc = elf_section_data (input_section)->sreloc;
3073 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3075 loc = sreloc->contents;
3076 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3077 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3079 /* If this reloc is against an external symbol, we do
3080 not want to fiddle with the addend. Otherwise, we
3081 need to include the symbol value so that it becomes
3082 an addend for the dynamic reloc. */
3089 case R_X86_64_TLSGD:
3090 case R_X86_64_GOTPC32_TLSDESC:
3091 case R_X86_64_TLSDESC_CALL:
3092 case R_X86_64_GOTTPOFF:
3093 tls_type = GOT_UNKNOWN;
3094 if (h == NULL && local_got_offsets)
3095 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3097 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
3099 if (! elf64_x86_64_tls_transition (info, input_bfd,
3100 input_section, contents,
3101 symtab_hdr, sym_hashes,
3102 &r_type, tls_type, rel,
3106 if (r_type == R_X86_64_TPOFF32)
3108 bfd_vma roff = rel->r_offset;
3110 BFD_ASSERT (! unresolved_reloc);
3112 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3114 /* GD->LE transition.
3115 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3116 .word 0x6666; rex64; call __tls_get_addr
3119 leaq foo@tpoff(%rax), %rax */
3120 memcpy (contents + roff - 4,
3121 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3123 bfd_put_32 (output_bfd,
3124 elf64_x86_64_tpoff (info, relocation),
3125 contents + roff + 8);
3126 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3130 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3132 /* GDesc -> LE transition.
3133 It's originally something like:
3134 leaq x@tlsdesc(%rip), %rax
3140 unsigned int val, type, type2;
3142 type = bfd_get_8 (input_bfd, contents + roff - 3);
3143 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3144 val = bfd_get_8 (input_bfd, contents + roff - 1);
3145 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3146 contents + roff - 3);
3147 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3148 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3149 contents + roff - 1);
3150 bfd_put_32 (output_bfd,
3151 elf64_x86_64_tpoff (info, relocation),
3155 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3157 /* GDesc -> LE transition.
3162 bfd_put_8 (output_bfd, 0x66, contents + roff);
3163 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3166 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3168 /* IE->LE transition:
3169 Originally it can be one of:
3170 movq foo@gottpoff(%rip), %reg
3171 addq foo@gottpoff(%rip), %reg
3174 leaq foo(%reg), %reg
3177 unsigned int val, type, reg;
3179 val = bfd_get_8 (input_bfd, contents + roff - 3);
3180 type = bfd_get_8 (input_bfd, contents + roff - 2);
3181 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3187 bfd_put_8 (output_bfd, 0x49,
3188 contents + roff - 3);
3189 bfd_put_8 (output_bfd, 0xc7,
3190 contents + roff - 2);
3191 bfd_put_8 (output_bfd, 0xc0 | reg,
3192 contents + roff - 1);
3196 /* addq -> addq - addressing with %rsp/%r12 is
3199 bfd_put_8 (output_bfd, 0x49,
3200 contents + roff - 3);
3201 bfd_put_8 (output_bfd, 0x81,
3202 contents + roff - 2);
3203 bfd_put_8 (output_bfd, 0xc0 | reg,
3204 contents + roff - 1);
3210 bfd_put_8 (output_bfd, 0x4d,
3211 contents + roff - 3);
3212 bfd_put_8 (output_bfd, 0x8d,
3213 contents + roff - 2);
3214 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3215 contents + roff - 1);
3217 bfd_put_32 (output_bfd,
3218 elf64_x86_64_tpoff (info, relocation),
3226 if (htab->sgot == NULL)
3231 off = h->got.offset;
3232 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
3236 if (local_got_offsets == NULL)
3239 off = local_got_offsets[r_symndx];
3240 offplt = local_tlsdesc_gotents[r_symndx];
3247 Elf_Internal_Rela outrel;
3252 if (htab->srelgot == NULL)
3255 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3257 if (GOT_TLS_GDESC_P (tls_type))
3259 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
3260 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3261 + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
3262 outrel.r_offset = (htab->sgotplt->output_section->vma
3263 + htab->sgotplt->output_offset
3265 + htab->sgotplt_jump_table_size);
3266 sreloc = htab->srelplt;
3267 loc = sreloc->contents;
3268 loc += sreloc->reloc_count++
3269 * sizeof (Elf64_External_Rela);
3270 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3271 <= sreloc->contents + sreloc->size);
3273 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3275 outrel.r_addend = 0;
3276 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3279 sreloc = htab->srelgot;
3281 outrel.r_offset = (htab->sgot->output_section->vma
3282 + htab->sgot->output_offset + off);
3284 if (GOT_TLS_GD_P (tls_type))
3285 dr_type = R_X86_64_DTPMOD64;
3286 else if (GOT_TLS_GDESC_P (tls_type))
3289 dr_type = R_X86_64_TPOFF64;
3291 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
3292 outrel.r_addend = 0;
3293 if ((dr_type == R_X86_64_TPOFF64
3294 || dr_type == R_X86_64_TLSDESC) && indx == 0)
3295 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3296 outrel.r_info = ELF64_R_INFO (indx, dr_type);
3298 loc = sreloc->contents;
3299 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3300 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3301 <= sreloc->contents + sreloc->size);
3302 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3304 if (GOT_TLS_GD_P (tls_type))
3308 BFD_ASSERT (! unresolved_reloc);
3309 bfd_put_64 (output_bfd,
3310 relocation - elf64_x86_64_dtpoff_base (info),
3311 htab->sgot->contents + off + GOT_ENTRY_SIZE);
3315 bfd_put_64 (output_bfd, 0,
3316 htab->sgot->contents + off + GOT_ENTRY_SIZE);
3317 outrel.r_info = ELF64_R_INFO (indx,
3319 outrel.r_offset += GOT_ENTRY_SIZE;
3320 sreloc->reloc_count++;
3321 loc += sizeof (Elf64_External_Rela);
3322 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3323 <= sreloc->contents + sreloc->size);
3324 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3332 local_got_offsets[r_symndx] |= 1;
3335 if (off >= (bfd_vma) -2
3336 && ! GOT_TLS_GDESC_P (tls_type))
3338 if (r_type == ELF64_R_TYPE (rel->r_info))
3340 if (r_type == R_X86_64_GOTPC32_TLSDESC
3341 || r_type == R_X86_64_TLSDESC_CALL)
3342 relocation = htab->sgotplt->output_section->vma
3343 + htab->sgotplt->output_offset
3344 + offplt + htab->sgotplt_jump_table_size;
3346 relocation = htab->sgot->output_section->vma
3347 + htab->sgot->output_offset + off;
3348 unresolved_reloc = FALSE;
3352 bfd_vma roff = rel->r_offset;
3354 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3356 /* GD->IE transition.
3357 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3358 .word 0x6666; rex64; call __tls_get_addr@plt
3361 addq foo@gottpoff(%rip), %rax */
3362 memcpy (contents + roff - 4,
3363 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3366 relocation = (htab->sgot->output_section->vma
3367 + htab->sgot->output_offset + off
3369 - input_section->output_section->vma
3370 - input_section->output_offset
3372 bfd_put_32 (output_bfd, relocation,
3373 contents + roff + 8);
3374 /* Skip R_X86_64_PLT32. */
3378 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3380 /* GDesc -> IE transition.
3381 It's originally something like:
3382 leaq x@tlsdesc(%rip), %rax
3385 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3388 unsigned int val, type, type2;
3390 type = bfd_get_8 (input_bfd, contents + roff - 3);
3391 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3392 val = bfd_get_8 (input_bfd, contents + roff - 1);
3394 /* Now modify the instruction as appropriate. To
3395 turn a leaq into a movq in the form we use it, it
3396 suffices to change the second byte from 0x8d to
3398 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3400 bfd_put_32 (output_bfd,
3401 htab->sgot->output_section->vma
3402 + htab->sgot->output_offset + off
3404 - input_section->output_section->vma
3405 - input_section->output_offset
3410 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3412 /* GDesc -> IE transition.
3419 unsigned int val, type;
3421 type = bfd_get_8 (input_bfd, contents + roff);
3422 val = bfd_get_8 (input_bfd, contents + roff + 1);
3423 bfd_put_8 (output_bfd, 0x66, contents + roff);
3424 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3432 case R_X86_64_TLSLD:
3433 if (! elf64_x86_64_tls_transition (info, input_bfd,
3434 input_section, contents,
3435 symtab_hdr, sym_hashes,
3436 &r_type, GOT_UNKNOWN,
3440 if (r_type != R_X86_64_TLSLD)
3442 /* LD->LE transition:
3443 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3445 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3447 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3448 memcpy (contents + rel->r_offset - 3,
3449 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3450 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3455 if (htab->sgot == NULL)
3458 off = htab->tls_ld_got.offset;
3463 Elf_Internal_Rela outrel;
3466 if (htab->srelgot == NULL)
3469 outrel.r_offset = (htab->sgot->output_section->vma
3470 + htab->sgot->output_offset + off);
3472 bfd_put_64 (output_bfd, 0,
3473 htab->sgot->contents + off);
3474 bfd_put_64 (output_bfd, 0,
3475 htab->sgot->contents + off + GOT_ENTRY_SIZE);
3476 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3477 outrel.r_addend = 0;
3478 loc = htab->srelgot->contents;
3479 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3480 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3481 htab->tls_ld_got.offset |= 1;
3483 relocation = htab->sgot->output_section->vma
3484 + htab->sgot->output_offset + off;
3485 unresolved_reloc = FALSE;
3488 case R_X86_64_DTPOFF32:
3489 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3490 relocation -= elf64_x86_64_dtpoff_base (info);
3492 relocation = elf64_x86_64_tpoff (info, relocation);
3495 case R_X86_64_TPOFF32:
3496 BFD_ASSERT (! info->shared);
3497 relocation = elf64_x86_64_tpoff (info, relocation);
3504 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3505 because such sections are not SEC_ALLOC and thus ld.so will
3506 not process them. */
3507 if (unresolved_reloc
3508 && !((input_section->flags & SEC_DEBUGGING) != 0
3510 (*_bfd_error_handler)
3511 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3514 (long) rel->r_offset,
3516 h->root.root.string);
3519 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3520 contents, rel->r_offset,
3521 relocation, rel->r_addend);
3523 if (r != bfd_reloc_ok)
3528 name = h->root.root.string;
3531 name = bfd_elf_string_from_elf_section (input_bfd,
3532 symtab_hdr->sh_link,
3537 name = bfd_section_name (input_bfd, sec);
3540 if (r == bfd_reloc_overflow)
3542 if (! ((*info->callbacks->reloc_overflow)
3543 (info, (h ? &h->root : NULL), name, howto->name,
3544 (bfd_vma) 0, input_bfd, input_section,
3550 (*_bfd_error_handler)
3551 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3552 input_bfd, input_section,
3553 (long) rel->r_offset, name, (int) r);
3562 /* Finish up dynamic symbol handling. We set the contents of various
3563 dynamic sections here. */
3566 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3567 struct bfd_link_info *info,
3568 struct elf_link_hash_entry *h,
3569 Elf_Internal_Sym *sym)
3571 struct elf64_x86_64_link_hash_table *htab;
3573 htab = elf64_x86_64_hash_table (info);
3575 if (h->plt.offset != (bfd_vma) -1)
3579 Elf_Internal_Rela rela;
3581 asection *plt, *gotplt, *relplt;
3583 /* When building a static executable, use .iplt, .igot.plt and
3584 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3585 if (htab->splt != 0)
3588 gotplt = htab->sgotplt;
3589 relplt = htab->srelplt;
3594 gotplt = htab->igotplt;
3595 relplt = htab->irelplt;
3598 /* This symbol has an entry in the procedure linkage table. Set
3600 if ((h->dynindx == -1
3601 && !((h->forced_local || info->executable)
3603 && h->type == STT_GNU_IFUNC))
3609 /* Get the index in the procedure linkage table which
3610 corresponds to this symbol. This is the index of this symbol
3611 in all the symbols for which we are making plt entries. The
3612 first entry in the procedure linkage table is reserved.
3614 Get the offset into the .got table of the entry that
3615 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3616 bytes. The first three are reserved for the dynamic linker.
3618 For static executables, we don't reserve anything. */
3620 if (plt == htab->splt)
3622 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3623 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3627 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3628 got_offset = plt_index * GOT_ENTRY_SIZE;
3631 /* Fill in the entry in the procedure linkage table. */
3632 memcpy (plt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3635 /* Insert the relocation positions of the plt section. The magic
3636 numbers at the end of the statements are the positions of the
3637 relocations in the plt section. */
3638 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3639 instruction uses 6 bytes, subtract this value. */
3640 bfd_put_32 (output_bfd,
3641 (gotplt->output_section->vma
3642 + gotplt->output_offset
3644 - plt->output_section->vma
3645 - plt->output_offset
3648 plt->contents + h->plt.offset + 2);
3650 /* Don't fill PLT entry for static executables. */
3651 if (plt == htab->splt)
3653 /* Put relocation index. */
3654 bfd_put_32 (output_bfd, plt_index,
3655 plt->contents + h->plt.offset + 7);
3656 /* Put offset for jmp .PLT0. */
3657 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3658 plt->contents + h->plt.offset + 12);
3661 /* Fill in the entry in the global offset table, initially this
3662 points to the pushq instruction in the PLT which is at offset 6. */
3663 bfd_put_64 (output_bfd, (plt->output_section->vma
3664 + plt->output_offset
3665 + h->plt.offset + 6),
3666 gotplt->contents + got_offset);
3668 /* Fill in the entry in the .rela.plt section. */
3669 rela.r_offset = (gotplt->output_section->vma
3670 + gotplt->output_offset
3672 if (h->dynindx == -1
3673 || ((info->executable
3674 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3676 && h->type == STT_GNU_IFUNC))
3678 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3679 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
3680 rela.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
3681 rela.r_addend = (h->root.u.def.value
3682 + h->root.u.def.section->output_section->vma
3683 + h->root.u.def.section->output_offset);
3687 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3690 loc = relplt->contents + plt_index * sizeof (Elf64_External_Rela);
3691 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3693 if (!h->def_regular)
3695 /* Mark the symbol as undefined, rather than as defined in
3696 the .plt section. Leave the value if there were any
3697 relocations where pointer equality matters (this is a clue
3698 for the dynamic linker, to make function pointer
3699 comparisons work between an application and shared
3700 library), otherwise set it to zero. If a function is only
3701 called from a binary, there is no need to slow down
3702 shared libraries because of that. */
3703 sym->st_shndx = SHN_UNDEF;
3704 if (!h->pointer_equality_needed)
3709 if (h->got.offset != (bfd_vma) -1
3710 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3711 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3713 Elf_Internal_Rela rela;
3716 /* This symbol has an entry in the global offset table. Set it
3718 if (htab->sgot == NULL || htab->srelgot == NULL)
3721 rela.r_offset = (htab->sgot->output_section->vma
3722 + htab->sgot->output_offset
3723 + (h->got.offset &~ (bfd_vma) 1));
3725 /* If this is a static link, or it is a -Bsymbolic link and the
3726 symbol is defined locally or was forced to be local because
3727 of a version file, we just want to emit a RELATIVE reloc.
3728 The entry in the global offset table will already have been
3729 initialized in the relocate_section function. */
3731 && SYMBOL_REFERENCES_LOCAL (info, h))
3733 if (!h->def_regular)
3735 BFD_ASSERT((h->got.offset & 1) != 0);
3736 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3737 rela.r_addend = (h->root.u.def.value
3738 + h->root.u.def.section->output_section->vma
3739 + h->root.u.def.section->output_offset);
3743 BFD_ASSERT((h->got.offset & 1) == 0);
3744 bfd_put_64 (output_bfd, (bfd_vma) 0,
3745 htab->sgot->contents + h->got.offset);
3746 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3750 loc = htab->srelgot->contents;
3751 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3752 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3757 Elf_Internal_Rela rela;
3760 /* This symbol needs a copy reloc. Set it up. */
3762 if (h->dynindx == -1
3763 || (h->root.type != bfd_link_hash_defined
3764 && h->root.type != bfd_link_hash_defweak)
3765 || htab->srelbss == NULL)
3768 rela.r_offset = (h->root.u.def.value
3769 + h->root.u.def.section->output_section->vma
3770 + h->root.u.def.section->output_offset);
3771 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3773 loc = htab->srelbss->contents;
3774 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
3775 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3778 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3779 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3780 || h == htab->elf.hgot)
3781 sym->st_shndx = SHN_ABS;
3786 /* Used to decide how to sort relocs in an optimal manner for the
3787 dynamic linker, before writing them out. */
3789 static enum elf_reloc_type_class
3790 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
3792 switch ((int) ELF64_R_TYPE (rela->r_info))
3794 case R_X86_64_RELATIVE:
3795 return reloc_class_relative;
3796 case R_X86_64_JUMP_SLOT:
3797 return reloc_class_plt;
3799 return reloc_class_copy;
3801 return reloc_class_normal;
3805 /* Finish up the dynamic sections. */
3808 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3810 struct elf64_x86_64_link_hash_table *htab;
3814 htab = elf64_x86_64_hash_table (info);
3815 dynobj = htab->elf.dynobj;
3816 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3818 if (htab->elf.dynamic_sections_created)
3820 Elf64_External_Dyn *dyncon, *dynconend;
3822 if (sdyn == NULL || htab->sgot == NULL)
3825 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3826 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3827 for (; dyncon < dynconend; dyncon++)
3829 Elf_Internal_Dyn dyn;
3832 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3841 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3845 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3849 s = htab->srelplt->output_section;
3850 dyn.d_un.d_val = s->size;
3854 /* The procedure linkage table relocs (DT_JMPREL) should
3855 not be included in the overall relocs (DT_RELA).
3856 Therefore, we override the DT_RELASZ entry here to
3857 make it not include the JMPREL relocs. Since the
3858 linker script arranges for .rela.plt to follow all
3859 other relocation sections, we don't have to worry
3860 about changing the DT_RELA entry. */
3861 if (htab->srelplt != NULL)
3863 s = htab->srelplt->output_section;
3864 dyn.d_un.d_val -= s->size;
3868 case DT_TLSDESC_PLT:
3870 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3871 + htab->tlsdesc_plt;
3874 case DT_TLSDESC_GOT:
3876 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3877 + htab->tlsdesc_got;
3881 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3884 /* Fill in the special first entry in the procedure linkage table. */
3885 if (htab->splt && htab->splt->size > 0)
3887 /* Fill in the first entry in the procedure linkage table. */
3888 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
3890 /* Add offset for pushq GOT+8(%rip), since the instruction
3891 uses 6 bytes subtract this value. */
3892 bfd_put_32 (output_bfd,
3893 (htab->sgotplt->output_section->vma
3894 + htab->sgotplt->output_offset
3896 - htab->splt->output_section->vma
3897 - htab->splt->output_offset
3899 htab->splt->contents + 2);
3900 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3901 the end of the instruction. */
3902 bfd_put_32 (output_bfd,
3903 (htab->sgotplt->output_section->vma
3904 + htab->sgotplt->output_offset
3906 - htab->splt->output_section->vma
3907 - htab->splt->output_offset
3909 htab->splt->contents + 8);
3911 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
3914 if (htab->tlsdesc_plt)
3916 bfd_put_64 (output_bfd, (bfd_vma) 0,
3917 htab->sgot->contents + htab->tlsdesc_got);
3919 memcpy (htab->splt->contents + htab->tlsdesc_plt,
3920 elf64_x86_64_plt0_entry,
3923 /* Add offset for pushq GOT+8(%rip), since the
3924 instruction uses 6 bytes subtract this value. */
3925 bfd_put_32 (output_bfd,
3926 (htab->sgotplt->output_section->vma
3927 + htab->sgotplt->output_offset
3929 - htab->splt->output_section->vma
3930 - htab->splt->output_offset
3933 htab->splt->contents + htab->tlsdesc_plt + 2);
3934 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3935 htab->tlsdesc_got. The 12 is the offset to the end of
3937 bfd_put_32 (output_bfd,
3938 (htab->sgot->output_section->vma
3939 + htab->sgot->output_offset
3941 - htab->splt->output_section->vma
3942 - htab->splt->output_offset
3945 htab->splt->contents + htab->tlsdesc_plt + 8);
3952 /* Fill in the first three entries in the global offset table. */
3953 if (htab->sgotplt->size > 0)
3955 /* Set the first entry in the global offset table to the address of
3956 the dynamic section. */
3958 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
3960 bfd_put_64 (output_bfd,
3961 sdyn->output_section->vma + sdyn->output_offset,
3962 htab->sgotplt->contents);
3963 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3964 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
3965 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
3968 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
3972 if (htab->sgot && htab->sgot->size > 0)
3973 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
3979 /* Return address for Ith PLT stub in section PLT, for relocation REL
3980 or (bfd_vma) -1 if it should not be included. */
3983 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
3984 const arelent *rel ATTRIBUTE_UNUSED)
3986 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3989 /* Handle an x86-64 specific section when reading an object file. This
3990 is called when elfcode.h finds a section with an unknown type. */
3993 elf64_x86_64_section_from_shdr (bfd *abfd,
3994 Elf_Internal_Shdr *hdr,
3998 if (hdr->sh_type != SHT_X86_64_UNWIND)
4001 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4007 /* Hook called by the linker routine which adds symbols from an object
4008 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4012 elf64_x86_64_add_symbol_hook (bfd *abfd,
4013 struct bfd_link_info *info,
4014 Elf_Internal_Sym *sym,
4015 const char **namep ATTRIBUTE_UNUSED,
4016 flagword *flagsp ATTRIBUTE_UNUSED,
4022 switch (sym->st_shndx)
4024 case SHN_X86_64_LCOMMON:
4025 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4028 lcomm = bfd_make_section_with_flags (abfd,
4032 | SEC_LINKER_CREATED));
4035 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4038 *valp = sym->st_size;
4042 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4043 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4049 /* Given a BFD section, try to locate the corresponding ELF section
4053 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4054 asection *sec, int *index)
4056 if (sec == &_bfd_elf_large_com_section)
4058 *index = SHN_X86_64_LCOMMON;
4064 /* Process a symbol. */
4067 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4070 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4072 switch (elfsym->internal_elf_sym.st_shndx)
4074 case SHN_X86_64_LCOMMON:
4075 asym->section = &_bfd_elf_large_com_section;
4076 asym->value = elfsym->internal_elf_sym.st_size;
4077 /* Common symbol doesn't set BSF_GLOBAL. */
4078 asym->flags &= ~BSF_GLOBAL;
4084 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
4086 return (sym->st_shndx == SHN_COMMON
4087 || sym->st_shndx == SHN_X86_64_LCOMMON);
4091 elf64_x86_64_common_section_index (asection *sec)
4093 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4096 return SHN_X86_64_LCOMMON;
4100 elf64_x86_64_common_section (asection *sec)
4102 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4103 return bfd_com_section_ptr;
4105 return &_bfd_elf_large_com_section;
4109 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4110 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4111 struct elf_link_hash_entry *h,
4112 Elf_Internal_Sym *sym,
4114 bfd_vma *pvalue ATTRIBUTE_UNUSED,
4115 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4116 bfd_boolean *skip ATTRIBUTE_UNUSED,
4117 bfd_boolean *override ATTRIBUTE_UNUSED,
4118 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4119 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4120 bfd_boolean *newdef ATTRIBUTE_UNUSED,
4121 bfd_boolean *newdyn,
4122 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4123 bfd_boolean *newweak ATTRIBUTE_UNUSED,
4124 bfd *abfd ATTRIBUTE_UNUSED,
4126 bfd_boolean *olddef ATTRIBUTE_UNUSED,
4127 bfd_boolean *olddyn,
4128 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4129 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4133 /* A normal common symbol and a large common symbol result in a
4134 normal common symbol. We turn the large common symbol into a
4137 && h->root.type == bfd_link_hash_common
4139 && bfd_is_com_section (*sec)
4142 if (sym->st_shndx == SHN_COMMON
4143 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4145 h->root.u.c.p->section
4146 = bfd_make_section_old_way (oldbfd, "COMMON");
4147 h->root.u.c.p->section->flags = SEC_ALLOC;
4149 else if (sym->st_shndx == SHN_X86_64_LCOMMON
4150 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4151 *psec = *sec = bfd_com_section_ptr;
4158 elf64_x86_64_additional_program_headers (bfd *abfd,
4159 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4164 /* Check to see if we need a large readonly segment. */
4165 s = bfd_get_section_by_name (abfd, ".lrodata");
4166 if (s && (s->flags & SEC_LOAD))
4169 /* Check to see if we need a large data segment. Since .lbss sections
4170 is placed right after the .bss section, there should be no need for
4171 a large data segment just because of .lbss. */
4172 s = bfd_get_section_by_name (abfd, ".ldata");
4173 if (s && (s->flags & SEC_LOAD))
4179 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4182 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4184 if (h->plt.offset != (bfd_vma) -1
4186 && !h->pointer_equality_needed)
4189 return _bfd_elf_hash_symbol (h);
4192 static const struct bfd_elf_special_section
4193 elf64_x86_64_special_sections[]=
4195 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4196 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4197 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4198 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4199 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4200 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4201 { NULL, 0, 0, 0, 0 }
4204 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
4205 #define TARGET_LITTLE_NAME "elf64-x86-64"
4206 #define ELF_ARCH bfd_arch_i386
4207 #define ELF_MACHINE_CODE EM_X86_64
4208 #define ELF_MAXPAGESIZE 0x200000
4209 #define ELF_MINPAGESIZE 0x1000
4210 #define ELF_COMMONPAGESIZE 0x1000
4212 #define elf_backend_can_gc_sections 1
4213 #define elf_backend_can_refcount 1
4214 #define elf_backend_want_got_plt 1
4215 #define elf_backend_plt_readonly 1
4216 #define elf_backend_want_plt_sym 0
4217 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
4218 #define elf_backend_rela_normal 1
4220 #define elf_info_to_howto elf64_x86_64_info_to_howto
4222 #define bfd_elf64_bfd_link_hash_table_create \
4223 elf64_x86_64_link_hash_table_create
4224 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
4225 #define bfd_elf64_bfd_reloc_name_lookup \
4226 elf64_x86_64_reloc_name_lookup
4228 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
4229 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4230 #define elf_backend_check_relocs elf64_x86_64_check_relocs
4231 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
4232 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
4233 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
4234 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
4235 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
4236 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
4237 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
4238 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
4239 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
4240 #define elf_backend_relocate_section elf64_x86_64_relocate_section
4241 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
4242 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
4243 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4244 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
4245 #define elf_backend_object_p elf64_x86_64_elf_object_p
4246 #define bfd_elf64_mkobject elf64_x86_64_mkobject
4248 #define elf_backend_section_from_shdr \
4249 elf64_x86_64_section_from_shdr
4251 #define elf_backend_section_from_bfd_section \
4252 elf64_x86_64_elf_section_from_bfd_section
4253 #define elf_backend_add_symbol_hook \
4254 elf64_x86_64_add_symbol_hook
4255 #define elf_backend_symbol_processing \
4256 elf64_x86_64_symbol_processing
4257 #define elf_backend_common_section_index \
4258 elf64_x86_64_common_section_index
4259 #define elf_backend_common_section \
4260 elf64_x86_64_common_section
4261 #define elf_backend_common_definition \
4262 elf64_x86_64_common_definition
4263 #define elf_backend_merge_symbol \
4264 elf64_x86_64_merge_symbol
4265 #define elf_backend_special_sections \
4266 elf64_x86_64_special_sections
4267 #define elf_backend_additional_program_headers \
4268 elf64_x86_64_additional_program_headers
4269 #define elf_backend_hash_symbol \
4270 elf64_x86_64_hash_symbol
4272 #undef elf_backend_post_process_headers
4273 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4275 #include "elf64-target.h"
4277 /* FreeBSD support. */
4279 #undef TARGET_LITTLE_SYM
4280 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
4281 #undef TARGET_LITTLE_NAME
4282 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
4285 #define ELF_OSABI ELFOSABI_FREEBSD
4288 #define elf64_bed elf64_x86_64_fbsd_bed
4290 #include "elf64-target.h"