* config/tc-mips.c (macro)[ldd_std]: Fix the relaxation variant
[binutils.git] / bfd / elf64-x86-64.c
blob677d9c457bea05b4ee830be6a3019d9b5282a55e
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "bfd_stdint.h"
29 #include "objalloc.h"
30 #include "hashtab.h"
32 #include "elf/x86-64.h"
34 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
35 #define MINUS_ONE (~ (bfd_vma) 0)
37 /* The relocation "howto" table. Order of fields:
38 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
39 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
40 static reloc_howto_type x86_64_elf_howto_table[] =
42 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
43 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
44 FALSE),
45 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
46 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
47 FALSE),
48 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
49 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
50 TRUE),
51 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
52 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
53 FALSE),
54 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
55 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
56 TRUE),
57 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
58 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
59 FALSE),
60 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
61 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
62 MINUS_ONE, FALSE),
63 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
64 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
65 MINUS_ONE, FALSE),
66 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
67 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
68 MINUS_ONE, FALSE),
69 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
70 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
71 0xffffffff, TRUE),
72 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
73 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
74 FALSE),
75 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
76 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
77 FALSE),
78 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
79 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
80 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
81 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
82 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
84 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
85 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
86 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
87 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
88 MINUS_ONE, FALSE),
89 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
90 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
91 MINUS_ONE, FALSE),
92 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
93 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
94 MINUS_ONE, FALSE),
95 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
96 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
97 0xffffffff, TRUE),
98 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
99 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
100 0xffffffff, TRUE),
101 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
102 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
103 0xffffffff, FALSE),
104 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
105 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
106 0xffffffff, TRUE),
107 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
108 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
109 0xffffffff, FALSE),
110 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
111 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
112 TRUE),
113 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
114 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
115 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
116 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
117 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
118 FALSE, 0xffffffff, 0xffffffff, TRUE),
119 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
120 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
121 FALSE),
122 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
123 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
124 MINUS_ONE, TRUE),
125 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
126 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
127 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
128 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
129 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
130 MINUS_ONE, FALSE),
131 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
132 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
133 MINUS_ONE, FALSE),
134 EMPTY_HOWTO (32),
135 EMPTY_HOWTO (33),
136 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
137 complain_overflow_bitfield, bfd_elf_generic_reloc,
138 "R_X86_64_GOTPC32_TLSDESC",
139 FALSE, 0xffffffff, 0xffffffff, TRUE),
140 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
141 complain_overflow_dont, bfd_elf_generic_reloc,
142 "R_X86_64_TLSDESC_CALL",
143 FALSE, 0, 0, FALSE),
144 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
145 complain_overflow_bitfield, bfd_elf_generic_reloc,
146 "R_X86_64_TLSDESC",
147 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
148 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
149 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
150 MINUS_ONE, FALSE),
152 /* We have a gap in the reloc numbers here.
153 R_X86_64_standard counts the number up to this point, and
154 R_X86_64_vt_offset is the value to subtract from a reloc type of
155 R_X86_64_GNU_VT* to form an index into this table. */
156 #define R_X86_64_standard (R_X86_64_IRELATIVE + 1)
157 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
159 /* GNU extension to record C++ vtable hierarchy. */
160 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
161 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
163 /* GNU extension to record C++ vtable member usage. */
164 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
165 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
166 FALSE)
169 #define IS_X86_64_PCREL_TYPE(TYPE) \
170 ( ((TYPE) == R_X86_64_PC8) \
171 || ((TYPE) == R_X86_64_PC16) \
172 || ((TYPE) == R_X86_64_PC32) \
173 || ((TYPE) == R_X86_64_PC64))
175 /* Map BFD relocs to the x86_64 elf relocs. */
176 struct elf_reloc_map
178 bfd_reloc_code_real_type bfd_reloc_val;
179 unsigned char elf_reloc_val;
182 static const struct elf_reloc_map x86_64_reloc_map[] =
184 { BFD_RELOC_NONE, R_X86_64_NONE, },
185 { BFD_RELOC_64, R_X86_64_64, },
186 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
187 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
188 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
189 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
190 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
191 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
192 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
193 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
194 { BFD_RELOC_32, R_X86_64_32, },
195 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
196 { BFD_RELOC_16, R_X86_64_16, },
197 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
198 { BFD_RELOC_8, R_X86_64_8, },
199 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
200 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
201 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
202 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
203 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
204 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
205 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
206 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
207 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
208 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
209 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
210 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
211 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
212 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
213 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
214 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
215 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
216 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
217 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
218 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
219 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
220 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
221 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
224 static reloc_howto_type *
225 elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
227 unsigned i;
229 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
230 || r_type >= (unsigned int) R_X86_64_max)
232 if (r_type >= (unsigned int) R_X86_64_standard)
234 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
235 abfd, (int) r_type);
236 r_type = R_X86_64_NONE;
238 i = r_type;
240 else
241 i = r_type - (unsigned int) R_X86_64_vt_offset;
242 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
243 return &x86_64_elf_howto_table[i];
246 /* Given a BFD reloc type, return a HOWTO structure. */
247 static reloc_howto_type *
248 elf64_x86_64_reloc_type_lookup (bfd *abfd,
249 bfd_reloc_code_real_type code)
251 unsigned int i;
253 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
254 i++)
256 if (x86_64_reloc_map[i].bfd_reloc_val == code)
257 return elf64_x86_64_rtype_to_howto (abfd,
258 x86_64_reloc_map[i].elf_reloc_val);
260 return 0;
263 static reloc_howto_type *
264 elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
265 const char *r_name)
267 unsigned int i;
269 for (i = 0;
270 i < (sizeof (x86_64_elf_howto_table)
271 / sizeof (x86_64_elf_howto_table[0]));
272 i++)
273 if (x86_64_elf_howto_table[i].name != NULL
274 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
275 return &x86_64_elf_howto_table[i];
277 return NULL;
280 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
282 static void
283 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
284 Elf_Internal_Rela *dst)
286 unsigned r_type;
288 r_type = ELF64_R_TYPE (dst->r_info);
289 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
290 BFD_ASSERT (r_type == cache_ptr->howto->type);
293 /* Support for core dump NOTE sections. */
294 static bfd_boolean
295 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
297 int offset;
298 size_t size;
300 switch (note->descsz)
302 default:
303 return FALSE;
305 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
306 /* pr_cursig */
307 elf_tdata (abfd)->core_signal
308 = bfd_get_16 (abfd, note->descdata + 12);
310 /* pr_pid */
311 elf_tdata (abfd)->core_lwpid
312 = bfd_get_32 (abfd, note->descdata + 32);
314 /* pr_reg */
315 offset = 112;
316 size = 216;
318 break;
321 /* Make a ".reg/999" section. */
322 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
323 size, note->descpos + offset);
326 static bfd_boolean
327 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
329 switch (note->descsz)
331 default:
332 return FALSE;
334 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
335 elf_tdata (abfd)->core_pid
336 = bfd_get_32 (abfd, note->descdata + 24);
337 elf_tdata (abfd)->core_program
338 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
339 elf_tdata (abfd)->core_command
340 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
343 /* Note that for some reason, a spurious space is tacked
344 onto the end of the args in some (at least one anyway)
345 implementations, so strip it off if it exists. */
348 char *command = elf_tdata (abfd)->core_command;
349 int n = strlen (command);
351 if (0 < n && command[n - 1] == ' ')
352 command[n - 1] = '\0';
355 return TRUE;
358 /* Functions for the x86-64 ELF linker. */
360 /* The name of the dynamic interpreter. This is put in the .interp
361 section. */
363 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
365 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
366 copying dynamic variables from a shared lib into an app's dynbss
367 section, and instead use a dynamic relocation to point into the
368 shared lib. */
369 #define ELIMINATE_COPY_RELOCS 1
371 /* The size in bytes of an entry in the global offset table. */
373 #define GOT_ENTRY_SIZE 8
375 /* The size in bytes of an entry in the procedure linkage table. */
377 #define PLT_ENTRY_SIZE 16
379 /* The first entry in a procedure linkage table looks like this. See the
380 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
382 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
384 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
385 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
386 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
389 /* Subsequent entries in a procedure linkage table look like this. */
391 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
393 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
394 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
395 0x68, /* pushq immediate */
396 0, 0, 0, 0, /* replaced with index into relocation table. */
397 0xe9, /* jmp relative */
398 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
401 /* x86-64 ELF linker hash entry. */
403 struct elf64_x86_64_link_hash_entry
405 struct elf_link_hash_entry elf;
407 /* Track dynamic relocs copied for this symbol. */
408 struct elf_dyn_relocs *dyn_relocs;
410 #define GOT_UNKNOWN 0
411 #define GOT_NORMAL 1
412 #define GOT_TLS_GD 2
413 #define GOT_TLS_IE 3
414 #define GOT_TLS_GDESC 4
415 #define GOT_TLS_GD_BOTH_P(type) \
416 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
417 #define GOT_TLS_GD_P(type) \
418 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
419 #define GOT_TLS_GDESC_P(type) \
420 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
421 #define GOT_TLS_GD_ANY_P(type) \
422 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
423 unsigned char tls_type;
425 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
426 starting at the end of the jump table. */
427 bfd_vma tlsdesc_got;
430 #define elf64_x86_64_hash_entry(ent) \
431 ((struct elf64_x86_64_link_hash_entry *)(ent))
433 struct elf64_x86_64_obj_tdata
435 struct elf_obj_tdata root;
437 /* tls_type for each local got entry. */
438 char *local_got_tls_type;
440 /* GOTPLT entries for TLS descriptors. */
441 bfd_vma *local_tlsdesc_gotent;
444 #define elf64_x86_64_tdata(abfd) \
445 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
447 #define elf64_x86_64_local_got_tls_type(abfd) \
448 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
450 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
451 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
453 #define is_x86_64_elf(bfd) \
454 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
455 && elf_tdata (bfd) != NULL \
456 && elf_object_id (bfd) == X86_64_ELF_DATA)
458 static bfd_boolean
459 elf64_x86_64_mkobject (bfd *abfd)
461 return bfd_elf_allocate_object (abfd, sizeof (struct elf64_x86_64_obj_tdata),
462 X86_64_ELF_DATA);
465 /* x86-64 ELF linker hash table. */
467 struct elf64_x86_64_link_hash_table
469 struct elf_link_hash_table elf;
471 /* Short-cuts to get to dynamic linker sections. */
472 asection *sdynbss;
473 asection *srelbss;
475 union
477 bfd_signed_vma refcount;
478 bfd_vma offset;
479 } tls_ld_got;
481 /* The amount of space used by the jump slots in the GOT. */
482 bfd_vma sgotplt_jump_table_size;
484 /* Small local sym cache. */
485 struct sym_cache sym_cache;
487 /* _TLS_MODULE_BASE_ symbol. */
488 struct bfd_link_hash_entry *tls_module_base;
490 /* Used by local STT_GNU_IFUNC symbols. */
491 htab_t loc_hash_table;
492 void * loc_hash_memory;
494 /* The offset into splt of the PLT entry for the TLS descriptor
495 resolver. Special values are 0, if not necessary (or not found
496 to be necessary yet), and -1 if needed but not determined
497 yet. */
498 bfd_vma tlsdesc_plt;
499 /* The offset into sgot of the GOT entry used by the PLT entry
500 above. */
501 bfd_vma tlsdesc_got;
504 /* Get the x86-64 ELF linker hash table from a link_info structure. */
506 #define elf64_x86_64_hash_table(p) \
507 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
508 == X86_64_ELF_DATA ? ((struct elf64_x86_64_link_hash_table *) ((p)->hash)) : NULL)
510 #define elf64_x86_64_compute_jump_table_size(htab) \
511 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
513 /* Create an entry in an x86-64 ELF linker hash table. */
515 static struct bfd_hash_entry *
516 elf64_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
517 struct bfd_hash_table *table,
518 const char *string)
520 /* Allocate the structure if it has not already been allocated by a
521 subclass. */
522 if (entry == NULL)
524 entry = (struct bfd_hash_entry *)
525 bfd_hash_allocate (table,
526 sizeof (struct elf64_x86_64_link_hash_entry));
527 if (entry == NULL)
528 return entry;
531 /* Call the allocation method of the superclass. */
532 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
533 if (entry != NULL)
535 struct elf64_x86_64_link_hash_entry *eh;
537 eh = (struct elf64_x86_64_link_hash_entry *) entry;
538 eh->dyn_relocs = NULL;
539 eh->tls_type = GOT_UNKNOWN;
540 eh->tlsdesc_got = (bfd_vma) -1;
543 return entry;
546 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
547 for local symbol so that we can handle local STT_GNU_IFUNC symbols
548 as global symbol. We reuse indx and dynstr_index for local symbol
549 hash since they aren't used by global symbols in this backend. */
551 static hashval_t
552 elf64_x86_64_local_htab_hash (const void *ptr)
554 struct elf_link_hash_entry *h
555 = (struct elf_link_hash_entry *) ptr;
556 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
559 /* Compare local hash entries. */
561 static int
562 elf64_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
564 struct elf_link_hash_entry *h1
565 = (struct elf_link_hash_entry *) ptr1;
566 struct elf_link_hash_entry *h2
567 = (struct elf_link_hash_entry *) ptr2;
569 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
572 /* Find and/or create a hash entry for local symbol. */
574 static struct elf_link_hash_entry *
575 elf64_x86_64_get_local_sym_hash (struct elf64_x86_64_link_hash_table *htab,
576 bfd *abfd, const Elf_Internal_Rela *rel,
577 bfd_boolean create)
579 struct elf64_x86_64_link_hash_entry e, *ret;
580 asection *sec = abfd->sections;
581 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
582 ELF64_R_SYM (rel->r_info));
583 void **slot;
585 e.elf.indx = sec->id;
586 e.elf.dynstr_index = ELF64_R_SYM (rel->r_info);
587 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
588 create ? INSERT : NO_INSERT);
590 if (!slot)
591 return NULL;
593 if (*slot)
595 ret = (struct elf64_x86_64_link_hash_entry *) *slot;
596 return &ret->elf;
599 ret = (struct elf64_x86_64_link_hash_entry *)
600 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
601 sizeof (struct elf64_x86_64_link_hash_entry));
602 if (ret)
604 memset (ret, 0, sizeof (*ret));
605 ret->elf.indx = sec->id;
606 ret->elf.dynstr_index = ELF64_R_SYM (rel->r_info);
607 ret->elf.dynindx = -1;
608 *slot = ret;
610 return &ret->elf;
613 /* Create an X86-64 ELF linker hash table. */
615 static struct bfd_link_hash_table *
616 elf64_x86_64_link_hash_table_create (bfd *abfd)
618 struct elf64_x86_64_link_hash_table *ret;
619 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
621 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
622 if (ret == NULL)
623 return NULL;
625 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
626 elf64_x86_64_link_hash_newfunc,
627 sizeof (struct elf64_x86_64_link_hash_entry),
628 X86_64_ELF_DATA))
630 free (ret);
631 return NULL;
634 ret->sdynbss = NULL;
635 ret->srelbss = NULL;
636 ret->sym_cache.abfd = NULL;
637 ret->tlsdesc_plt = 0;
638 ret->tlsdesc_got = 0;
639 ret->tls_ld_got.refcount = 0;
640 ret->sgotplt_jump_table_size = 0;
641 ret->tls_module_base = NULL;
643 ret->loc_hash_table = htab_try_create (1024,
644 elf64_x86_64_local_htab_hash,
645 elf64_x86_64_local_htab_eq,
646 NULL);
647 ret->loc_hash_memory = objalloc_create ();
648 if (!ret->loc_hash_table || !ret->loc_hash_memory)
650 free (ret);
651 return NULL;
654 return &ret->elf.root;
657 /* Destroy an X86-64 ELF linker hash table. */
659 static void
660 elf64_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
662 struct elf64_x86_64_link_hash_table *htab
663 = (struct elf64_x86_64_link_hash_table *) hash;
665 if (htab->loc_hash_table)
666 htab_delete (htab->loc_hash_table);
667 if (htab->loc_hash_memory)
668 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
669 _bfd_generic_link_hash_table_free (hash);
672 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
673 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
674 hash table. */
676 static bfd_boolean
677 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
679 struct elf64_x86_64_link_hash_table *htab;
681 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
682 return FALSE;
684 htab = elf64_x86_64_hash_table (info);
685 if (htab == NULL)
686 return FALSE;
688 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
689 if (!info->shared)
690 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
692 if (!htab->sdynbss
693 || (!info->shared && !htab->srelbss))
694 abort ();
696 return TRUE;
699 /* Copy the extra info we tack onto an elf_link_hash_entry. */
701 static void
702 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
703 struct elf_link_hash_entry *dir,
704 struct elf_link_hash_entry *ind)
706 struct elf64_x86_64_link_hash_entry *edir, *eind;
708 edir = (struct elf64_x86_64_link_hash_entry *) dir;
709 eind = (struct elf64_x86_64_link_hash_entry *) ind;
711 if (eind->dyn_relocs != NULL)
713 if (edir->dyn_relocs != NULL)
715 struct elf_dyn_relocs **pp;
716 struct elf_dyn_relocs *p;
718 /* Add reloc counts against the indirect sym to the direct sym
719 list. Merge any entries against the same section. */
720 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
722 struct elf_dyn_relocs *q;
724 for (q = edir->dyn_relocs; q != NULL; q = q->next)
725 if (q->sec == p->sec)
727 q->pc_count += p->pc_count;
728 q->count += p->count;
729 *pp = p->next;
730 break;
732 if (q == NULL)
733 pp = &p->next;
735 *pp = edir->dyn_relocs;
738 edir->dyn_relocs = eind->dyn_relocs;
739 eind->dyn_relocs = NULL;
742 if (ind->root.type == bfd_link_hash_indirect
743 && dir->got.refcount <= 0)
745 edir->tls_type = eind->tls_type;
746 eind->tls_type = GOT_UNKNOWN;
749 if (ELIMINATE_COPY_RELOCS
750 && ind->root.type != bfd_link_hash_indirect
751 && dir->dynamic_adjusted)
753 /* If called to transfer flags for a weakdef during processing
754 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
755 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
756 dir->ref_dynamic |= ind->ref_dynamic;
757 dir->ref_regular |= ind->ref_regular;
758 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
759 dir->needs_plt |= ind->needs_plt;
760 dir->pointer_equality_needed |= ind->pointer_equality_needed;
762 else
763 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
766 static bfd_boolean
767 elf64_x86_64_elf_object_p (bfd *abfd)
769 /* Set the right machine number for an x86-64 elf64 file. */
770 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
771 return TRUE;
774 typedef union
776 unsigned char c[2];
777 uint16_t i;
779 x86_64_opcode16;
781 typedef union
783 unsigned char c[4];
784 uint32_t i;
786 x86_64_opcode32;
788 /* Return TRUE if the TLS access code sequence support transition
789 from R_TYPE. */
791 static bfd_boolean
792 elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec,
793 bfd_byte *contents,
794 Elf_Internal_Shdr *symtab_hdr,
795 struct elf_link_hash_entry **sym_hashes,
796 unsigned int r_type,
797 const Elf_Internal_Rela *rel,
798 const Elf_Internal_Rela *relend)
800 unsigned int val;
801 unsigned long r_symndx;
802 struct elf_link_hash_entry *h;
803 bfd_vma offset;
805 /* Get the section contents. */
806 if (contents == NULL)
808 if (elf_section_data (sec)->this_hdr.contents != NULL)
809 contents = elf_section_data (sec)->this_hdr.contents;
810 else
812 /* FIXME: How to better handle error condition? */
813 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
814 return FALSE;
816 /* Cache the section contents for elf_link_input_bfd. */
817 elf_section_data (sec)->this_hdr.contents = contents;
821 offset = rel->r_offset;
822 switch (r_type)
824 case R_X86_64_TLSGD:
825 case R_X86_64_TLSLD:
826 if ((rel + 1) >= relend)
827 return FALSE;
829 if (r_type == R_X86_64_TLSGD)
831 /* Check transition from GD access model. Only
832 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
833 .word 0x6666; rex64; call __tls_get_addr
834 can transit to different access model. */
836 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } },
837 call = { { 0x66, 0x66, 0x48, 0xe8 } };
838 if (offset < 4
839 || (offset + 12) > sec->size
840 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i
841 || bfd_get_32 (abfd, contents + offset + 4) != call.i)
842 return FALSE;
844 else
846 /* Check transition from LD access model. Only
847 leaq foo@tlsld(%rip), %rdi;
848 call __tls_get_addr
849 can transit to different access model. */
851 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
852 x86_64_opcode32 op;
854 if (offset < 3 || (offset + 9) > sec->size)
855 return FALSE;
857 op.i = bfd_get_32 (abfd, contents + offset - 3);
858 op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
859 if (op.i != ld.i)
860 return FALSE;
863 r_symndx = ELF64_R_SYM (rel[1].r_info);
864 if (r_symndx < symtab_hdr->sh_info)
865 return FALSE;
867 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
868 /* Use strncmp to check __tls_get_addr since __tls_get_addr
869 may be versioned. */
870 return (h != NULL
871 && h->root.root.string != NULL
872 && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
873 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
874 && (strncmp (h->root.root.string,
875 "__tls_get_addr", 14) == 0));
877 case R_X86_64_GOTTPOFF:
878 /* Check transition from IE access model:
879 movq foo@gottpoff(%rip), %reg
880 addq foo@gottpoff(%rip), %reg
883 if (offset < 3 || (offset + 4) > sec->size)
884 return FALSE;
886 val = bfd_get_8 (abfd, contents + offset - 3);
887 if (val != 0x48 && val != 0x4c)
888 return FALSE;
890 val = bfd_get_8 (abfd, contents + offset - 2);
891 if (val != 0x8b && val != 0x03)
892 return FALSE;
894 val = bfd_get_8 (abfd, contents + offset - 1);
895 return (val & 0xc7) == 5;
897 case R_X86_64_GOTPC32_TLSDESC:
898 /* Check transition from GDesc access model:
899 leaq x@tlsdesc(%rip), %rax
901 Make sure it's a leaq adding rip to a 32-bit offset
902 into any register, although it's probably almost always
903 going to be rax. */
905 if (offset < 3 || (offset + 4) > sec->size)
906 return FALSE;
908 val = bfd_get_8 (abfd, contents + offset - 3);
909 if ((val & 0xfb) != 0x48)
910 return FALSE;
912 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
913 return FALSE;
915 val = bfd_get_8 (abfd, contents + offset - 1);
916 return (val & 0xc7) == 0x05;
918 case R_X86_64_TLSDESC_CALL:
919 /* Check transition from GDesc access model:
920 call *x@tlsdesc(%rax)
922 if (offset + 2 <= sec->size)
924 /* Make sure that it's a call *x@tlsdesc(%rax). */
925 static x86_64_opcode16 call = { { 0xff, 0x10 } };
926 return bfd_get_16 (abfd, contents + offset) == call.i;
929 return FALSE;
931 default:
932 abort ();
936 /* Return TRUE if the TLS access transition is OK or no transition
937 will be performed. Update R_TYPE if there is a transition. */
939 static bfd_boolean
940 elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
941 asection *sec, bfd_byte *contents,
942 Elf_Internal_Shdr *symtab_hdr,
943 struct elf_link_hash_entry **sym_hashes,
944 unsigned int *r_type, int tls_type,
945 const Elf_Internal_Rela *rel,
946 const Elf_Internal_Rela *relend,
947 struct elf_link_hash_entry *h,
948 unsigned long r_symndx)
950 unsigned int from_type = *r_type;
951 unsigned int to_type = from_type;
952 bfd_boolean check = TRUE;
954 /* Skip TLS transition for functions. */
955 if (h != NULL
956 && (h->type == STT_FUNC
957 || h->type == STT_GNU_IFUNC))
958 return TRUE;
960 switch (from_type)
962 case R_X86_64_TLSGD:
963 case R_X86_64_GOTPC32_TLSDESC:
964 case R_X86_64_TLSDESC_CALL:
965 case R_X86_64_GOTTPOFF:
966 if (info->executable)
968 if (h == NULL)
969 to_type = R_X86_64_TPOFF32;
970 else
971 to_type = R_X86_64_GOTTPOFF;
974 /* When we are called from elf64_x86_64_relocate_section,
975 CONTENTS isn't NULL and there may be additional transitions
976 based on TLS_TYPE. */
977 if (contents != NULL)
979 unsigned int new_to_type = to_type;
981 if (info->executable
982 && h != NULL
983 && h->dynindx == -1
984 && tls_type == GOT_TLS_IE)
985 new_to_type = R_X86_64_TPOFF32;
987 if (to_type == R_X86_64_TLSGD
988 || to_type == R_X86_64_GOTPC32_TLSDESC
989 || to_type == R_X86_64_TLSDESC_CALL)
991 if (tls_type == GOT_TLS_IE)
992 new_to_type = R_X86_64_GOTTPOFF;
995 /* We checked the transition before when we were called from
996 elf64_x86_64_check_relocs. We only want to check the new
997 transition which hasn't been checked before. */
998 check = new_to_type != to_type && from_type == to_type;
999 to_type = new_to_type;
1002 break;
1004 case R_X86_64_TLSLD:
1005 if (info->executable)
1006 to_type = R_X86_64_TPOFF32;
1007 break;
1009 default:
1010 return TRUE;
1013 /* Return TRUE if there is no transition. */
1014 if (from_type == to_type)
1015 return TRUE;
1017 /* Check if the transition can be performed. */
1018 if (check
1019 && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
1020 symtab_hdr, sym_hashes,
1021 from_type, rel, relend))
1023 reloc_howto_type *from, *to;
1024 const char *name;
1026 from = elf64_x86_64_rtype_to_howto (abfd, from_type);
1027 to = elf64_x86_64_rtype_to_howto (abfd, to_type);
1029 if (h)
1030 name = h->root.root.string;
1031 else
1033 struct elf64_x86_64_link_hash_table *htab;
1035 htab = elf64_x86_64_hash_table (info);
1036 if (htab == NULL)
1037 name = "*unknown*";
1038 else
1040 Elf_Internal_Sym *isym;
1042 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1043 abfd, r_symndx);
1044 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1048 (*_bfd_error_handler)
1049 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1050 "in section `%A' failed"),
1051 abfd, sec, from->name, to->name, name,
1052 (unsigned long) rel->r_offset);
1053 bfd_set_error (bfd_error_bad_value);
1054 return FALSE;
1057 *r_type = to_type;
1058 return TRUE;
1061 /* Look through the relocs for a section during the first phase, and
1062 calculate needed space in the global offset table, procedure
1063 linkage table, and dynamic reloc sections. */
1065 static bfd_boolean
1066 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1067 asection *sec,
1068 const Elf_Internal_Rela *relocs)
1070 struct elf64_x86_64_link_hash_table *htab;
1071 Elf_Internal_Shdr *symtab_hdr;
1072 struct elf_link_hash_entry **sym_hashes;
1073 const Elf_Internal_Rela *rel;
1074 const Elf_Internal_Rela *rel_end;
1075 asection *sreloc;
1077 if (info->relocatable)
1078 return TRUE;
1080 BFD_ASSERT (is_x86_64_elf (abfd));
1082 htab = elf64_x86_64_hash_table (info);
1083 if (htab == NULL)
1084 return FALSE;
1086 symtab_hdr = &elf_symtab_hdr (abfd);
1087 sym_hashes = elf_sym_hashes (abfd);
1089 sreloc = NULL;
1091 rel_end = relocs + sec->reloc_count;
1092 for (rel = relocs; rel < rel_end; rel++)
1094 unsigned int r_type;
1095 unsigned long r_symndx;
1096 struct elf_link_hash_entry *h;
1097 Elf_Internal_Sym *isym;
1098 const char *name;
1100 r_symndx = ELF64_R_SYM (rel->r_info);
1101 r_type = ELF64_R_TYPE (rel->r_info);
1103 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1105 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1106 abfd, r_symndx);
1107 return FALSE;
1110 if (r_symndx < symtab_hdr->sh_info)
1112 /* A local symbol. */
1113 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1114 abfd, r_symndx);
1115 if (isym == NULL)
1116 return FALSE;
1118 /* Check relocation against local STT_GNU_IFUNC symbol. */
1119 if (ELF64_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1121 h = elf64_x86_64_get_local_sym_hash (htab, abfd, rel,
1122 TRUE);
1123 if (h == NULL)
1124 return FALSE;
1126 /* Fake a STT_GNU_IFUNC symbol. */
1127 h->type = STT_GNU_IFUNC;
1128 h->def_regular = 1;
1129 h->ref_regular = 1;
1130 h->forced_local = 1;
1131 h->root.type = bfd_link_hash_defined;
1133 else
1134 h = NULL;
1136 else
1138 isym = NULL;
1139 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1140 while (h->root.type == bfd_link_hash_indirect
1141 || h->root.type == bfd_link_hash_warning)
1142 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1145 if (h != NULL)
1147 /* Create the ifunc sections for static executables. If we
1148 never see an indirect function symbol nor we are building
1149 a static executable, those sections will be empty and
1150 won't appear in output. */
1151 switch (r_type)
1153 default:
1154 break;
1156 case R_X86_64_32S:
1157 case R_X86_64_32:
1158 case R_X86_64_64:
1159 case R_X86_64_PC32:
1160 case R_X86_64_PC64:
1161 case R_X86_64_PLT32:
1162 case R_X86_64_GOTPCREL:
1163 case R_X86_64_GOTPCREL64:
1164 if (!_bfd_elf_create_ifunc_sections (abfd, info))
1165 return FALSE;
1166 break;
1169 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1170 it here if it is defined in a non-shared object. */
1171 if (h->type == STT_GNU_IFUNC
1172 && h->def_regular)
1174 /* It is referenced by a non-shared object. */
1175 h->ref_regular = 1;
1176 h->needs_plt = 1;
1178 /* STT_GNU_IFUNC symbol must go through PLT. */
1179 h->plt.refcount += 1;
1181 /* STT_GNU_IFUNC needs dynamic sections. */
1182 if (htab->elf.dynobj == NULL)
1183 htab->elf.dynobj = abfd;
1185 switch (r_type)
1187 default:
1188 if (h->root.root.string)
1189 name = h->root.root.string;
1190 else
1191 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1192 NULL);
1193 (*_bfd_error_handler)
1194 (_("%B: relocation %s against STT_GNU_IFUNC "
1195 "symbol `%s' isn't handled by %s"), abfd,
1196 x86_64_elf_howto_table[r_type].name,
1197 name, __FUNCTION__);
1198 bfd_set_error (bfd_error_bad_value);
1199 return FALSE;
1201 case R_X86_64_64:
1202 h->non_got_ref = 1;
1203 h->pointer_equality_needed = 1;
1204 if (info->shared)
1206 /* We must copy these reloc types into the output
1207 file. Create a reloc section in dynobj and
1208 make room for this reloc. */
1209 sreloc = _bfd_elf_create_ifunc_dyn_reloc
1210 (abfd, info, sec, sreloc,
1211 &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs);
1212 if (sreloc == NULL)
1213 return FALSE;
1215 break;
1217 case R_X86_64_32S:
1218 case R_X86_64_32:
1219 case R_X86_64_PC32:
1220 case R_X86_64_PC64:
1221 h->non_got_ref = 1;
1222 if (r_type != R_X86_64_PC32
1223 && r_type != R_X86_64_PC64)
1224 h->pointer_equality_needed = 1;
1225 break;
1227 case R_X86_64_PLT32:
1228 break;
1230 case R_X86_64_GOTPCREL:
1231 case R_X86_64_GOTPCREL64:
1232 h->got.refcount += 1;
1233 if (htab->elf.sgot == NULL
1234 && !_bfd_elf_create_got_section (htab->elf.dynobj,
1235 info))
1236 return FALSE;
1237 break;
1240 continue;
1244 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1245 symtab_hdr, sym_hashes,
1246 &r_type, GOT_UNKNOWN,
1247 rel, rel_end, h, r_symndx))
1248 return FALSE;
1250 switch (r_type)
1252 case R_X86_64_TLSLD:
1253 htab->tls_ld_got.refcount += 1;
1254 goto create_got;
1256 case R_X86_64_TPOFF32:
1257 if (!info->executable)
1259 if (h)
1260 name = h->root.root.string;
1261 else
1262 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1263 NULL);
1264 (*_bfd_error_handler)
1265 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1266 abfd,
1267 x86_64_elf_howto_table[r_type].name, name);
1268 bfd_set_error (bfd_error_bad_value);
1269 return FALSE;
1271 break;
1273 case R_X86_64_GOTTPOFF:
1274 if (!info->executable)
1275 info->flags |= DF_STATIC_TLS;
1276 /* Fall through */
1278 case R_X86_64_GOT32:
1279 case R_X86_64_GOTPCREL:
1280 case R_X86_64_TLSGD:
1281 case R_X86_64_GOT64:
1282 case R_X86_64_GOTPCREL64:
1283 case R_X86_64_GOTPLT64:
1284 case R_X86_64_GOTPC32_TLSDESC:
1285 case R_X86_64_TLSDESC_CALL:
1286 /* This symbol requires a global offset table entry. */
1288 int tls_type, old_tls_type;
1290 switch (r_type)
1292 default: tls_type = GOT_NORMAL; break;
1293 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1294 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1295 case R_X86_64_GOTPC32_TLSDESC:
1296 case R_X86_64_TLSDESC_CALL:
1297 tls_type = GOT_TLS_GDESC; break;
1300 if (h != NULL)
1302 if (r_type == R_X86_64_GOTPLT64)
1304 /* This relocation indicates that we also need
1305 a PLT entry, as this is a function. We don't need
1306 a PLT entry for local symbols. */
1307 h->needs_plt = 1;
1308 h->plt.refcount += 1;
1310 h->got.refcount += 1;
1311 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1313 else
1315 bfd_signed_vma *local_got_refcounts;
1317 /* This is a global offset table entry for a local symbol. */
1318 local_got_refcounts = elf_local_got_refcounts (abfd);
1319 if (local_got_refcounts == NULL)
1321 bfd_size_type size;
1323 size = symtab_hdr->sh_info;
1324 size *= sizeof (bfd_signed_vma)
1325 + sizeof (bfd_vma) + sizeof (char);
1326 local_got_refcounts = ((bfd_signed_vma *)
1327 bfd_zalloc (abfd, size));
1328 if (local_got_refcounts == NULL)
1329 return FALSE;
1330 elf_local_got_refcounts (abfd) = local_got_refcounts;
1331 elf64_x86_64_local_tlsdesc_gotent (abfd)
1332 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1333 elf64_x86_64_local_got_tls_type (abfd)
1334 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1336 local_got_refcounts[r_symndx] += 1;
1337 old_tls_type
1338 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1341 /* If a TLS symbol is accessed using IE at least once,
1342 there is no point to use dynamic model for it. */
1343 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1344 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1345 || tls_type != GOT_TLS_IE))
1347 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1348 tls_type = old_tls_type;
1349 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1350 && GOT_TLS_GD_ANY_P (tls_type))
1351 tls_type |= old_tls_type;
1352 else
1354 if (h)
1355 name = h->root.root.string;
1356 else
1357 name = bfd_elf_sym_name (abfd, symtab_hdr,
1358 isym, NULL);
1359 (*_bfd_error_handler)
1360 (_("%B: '%s' accessed both as normal and thread local symbol"),
1361 abfd, name);
1362 return FALSE;
1366 if (old_tls_type != tls_type)
1368 if (h != NULL)
1369 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1370 else
1371 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1374 /* Fall through */
1376 case R_X86_64_GOTOFF64:
1377 case R_X86_64_GOTPC32:
1378 case R_X86_64_GOTPC64:
1379 create_got:
1380 if (htab->elf.sgot == NULL)
1382 if (htab->elf.dynobj == NULL)
1383 htab->elf.dynobj = abfd;
1384 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1385 info))
1386 return FALSE;
1388 break;
1390 case R_X86_64_PLT32:
1391 /* This symbol requires a procedure linkage table entry. We
1392 actually build the entry in adjust_dynamic_symbol,
1393 because this might be a case of linking PIC code which is
1394 never referenced by a dynamic object, in which case we
1395 don't need to generate a procedure linkage table entry
1396 after all. */
1398 /* If this is a local symbol, we resolve it directly without
1399 creating a procedure linkage table entry. */
1400 if (h == NULL)
1401 continue;
1403 h->needs_plt = 1;
1404 h->plt.refcount += 1;
1405 break;
1407 case R_X86_64_PLTOFF64:
1408 /* This tries to form the 'address' of a function relative
1409 to GOT. For global symbols we need a PLT entry. */
1410 if (h != NULL)
1412 h->needs_plt = 1;
1413 h->plt.refcount += 1;
1415 goto create_got;
1417 case R_X86_64_8:
1418 case R_X86_64_16:
1419 case R_X86_64_32:
1420 case R_X86_64_32S:
1421 /* Let's help debug shared library creation. These relocs
1422 cannot be used in shared libs. Don't error out for
1423 sections we don't care about, such as debug sections or
1424 non-constant sections. */
1425 if (info->shared
1426 && (sec->flags & SEC_ALLOC) != 0
1427 && (sec->flags & SEC_READONLY) != 0)
1429 if (h)
1430 name = h->root.root.string;
1431 else
1432 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1433 (*_bfd_error_handler)
1434 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1435 abfd, x86_64_elf_howto_table[r_type].name, name);
1436 bfd_set_error (bfd_error_bad_value);
1437 return FALSE;
1439 /* Fall through. */
1441 case R_X86_64_PC8:
1442 case R_X86_64_PC16:
1443 case R_X86_64_PC32:
1444 case R_X86_64_PC64:
1445 case R_X86_64_64:
1446 if (h != NULL && info->executable)
1448 /* If this reloc is in a read-only section, we might
1449 need a copy reloc. We can't check reliably at this
1450 stage whether the section is read-only, as input
1451 sections have not yet been mapped to output sections.
1452 Tentatively set the flag for now, and correct in
1453 adjust_dynamic_symbol. */
1454 h->non_got_ref = 1;
1456 /* We may need a .plt entry if the function this reloc
1457 refers to is in a shared lib. */
1458 h->plt.refcount += 1;
1459 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1460 h->pointer_equality_needed = 1;
1463 /* If we are creating a shared library, and this is a reloc
1464 against a global symbol, or a non PC relative reloc
1465 against a local symbol, then we need to copy the reloc
1466 into the shared library. However, if we are linking with
1467 -Bsymbolic, we do not need to copy a reloc against a
1468 global symbol which is defined in an object we are
1469 including in the link (i.e., DEF_REGULAR is set). At
1470 this point we have not seen all the input files, so it is
1471 possible that DEF_REGULAR is not set now but will be set
1472 later (it is never cleared). In case of a weak definition,
1473 DEF_REGULAR may be cleared later by a strong definition in
1474 a shared library. We account for that possibility below by
1475 storing information in the relocs_copied field of the hash
1476 table entry. A similar situation occurs when creating
1477 shared libraries and symbol visibility changes render the
1478 symbol local.
1480 If on the other hand, we are creating an executable, we
1481 may need to keep relocations for symbols satisfied by a
1482 dynamic library if we manage to avoid copy relocs for the
1483 symbol. */
1484 if ((info->shared
1485 && (sec->flags & SEC_ALLOC) != 0
1486 && (! IS_X86_64_PCREL_TYPE (r_type)
1487 || (h != NULL
1488 && (! SYMBOLIC_BIND (info, h)
1489 || h->root.type == bfd_link_hash_defweak
1490 || !h->def_regular))))
1491 || (ELIMINATE_COPY_RELOCS
1492 && !info->shared
1493 && (sec->flags & SEC_ALLOC) != 0
1494 && h != NULL
1495 && (h->root.type == bfd_link_hash_defweak
1496 || !h->def_regular)))
1498 struct elf_dyn_relocs *p;
1499 struct elf_dyn_relocs **head;
1501 /* We must copy these reloc types into the output file.
1502 Create a reloc section in dynobj and make room for
1503 this reloc. */
1504 if (sreloc == NULL)
1506 if (htab->elf.dynobj == NULL)
1507 htab->elf.dynobj = abfd;
1509 sreloc = _bfd_elf_make_dynamic_reloc_section
1510 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
1512 if (sreloc == NULL)
1513 return FALSE;
1516 /* If this is a global symbol, we count the number of
1517 relocations we need for this symbol. */
1518 if (h != NULL)
1520 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1522 else
1524 /* Track dynamic relocs needed for local syms too.
1525 We really need local syms available to do this
1526 easily. Oh well. */
1527 asection *s;
1528 void **vpp;
1530 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1531 abfd, r_symndx);
1532 if (isym == NULL)
1533 return FALSE;
1535 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1536 if (s == NULL)
1537 s = sec;
1539 /* Beware of type punned pointers vs strict aliasing
1540 rules. */
1541 vpp = &(elf_section_data (s)->local_dynrel);
1542 head = (struct elf_dyn_relocs **)vpp;
1545 p = *head;
1546 if (p == NULL || p->sec != sec)
1548 bfd_size_type amt = sizeof *p;
1550 p = ((struct elf_dyn_relocs *)
1551 bfd_alloc (htab->elf.dynobj, amt));
1552 if (p == NULL)
1553 return FALSE;
1554 p->next = *head;
1555 *head = p;
1556 p->sec = sec;
1557 p->count = 0;
1558 p->pc_count = 0;
1561 p->count += 1;
1562 if (IS_X86_64_PCREL_TYPE (r_type))
1563 p->pc_count += 1;
1565 break;
1567 /* This relocation describes the C++ object vtable hierarchy.
1568 Reconstruct it for later use during GC. */
1569 case R_X86_64_GNU_VTINHERIT:
1570 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1571 return FALSE;
1572 break;
1574 /* This relocation describes which C++ vtable entries are actually
1575 used. Record for later use during GC. */
1576 case R_X86_64_GNU_VTENTRY:
1577 BFD_ASSERT (h != NULL);
1578 if (h != NULL
1579 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1580 return FALSE;
1581 break;
1583 default:
1584 break;
1588 return TRUE;
1591 /* Return the section that should be marked against GC for a given
1592 relocation. */
1594 static asection *
1595 elf64_x86_64_gc_mark_hook (asection *sec,
1596 struct bfd_link_info *info,
1597 Elf_Internal_Rela *rel,
1598 struct elf_link_hash_entry *h,
1599 Elf_Internal_Sym *sym)
1601 if (h != NULL)
1602 switch (ELF64_R_TYPE (rel->r_info))
1604 case R_X86_64_GNU_VTINHERIT:
1605 case R_X86_64_GNU_VTENTRY:
1606 return NULL;
1609 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1612 /* Update the got entry reference counts for the section being removed. */
1614 static bfd_boolean
1615 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1616 asection *sec,
1617 const Elf_Internal_Rela *relocs)
1619 struct elf64_x86_64_link_hash_table *htab;
1620 Elf_Internal_Shdr *symtab_hdr;
1621 struct elf_link_hash_entry **sym_hashes;
1622 bfd_signed_vma *local_got_refcounts;
1623 const Elf_Internal_Rela *rel, *relend;
1625 if (info->relocatable)
1626 return TRUE;
1628 htab = elf64_x86_64_hash_table (info);
1629 if (htab == NULL)
1630 return FALSE;
1632 elf_section_data (sec)->local_dynrel = NULL;
1634 symtab_hdr = &elf_symtab_hdr (abfd);
1635 sym_hashes = elf_sym_hashes (abfd);
1636 local_got_refcounts = elf_local_got_refcounts (abfd);
1638 relend = relocs + sec->reloc_count;
1639 for (rel = relocs; rel < relend; rel++)
1641 unsigned long r_symndx;
1642 unsigned int r_type;
1643 struct elf_link_hash_entry *h = NULL;
1645 r_symndx = ELF64_R_SYM (rel->r_info);
1646 if (r_symndx >= symtab_hdr->sh_info)
1648 struct elf64_x86_64_link_hash_entry *eh;
1649 struct elf_dyn_relocs **pp;
1650 struct elf_dyn_relocs *p;
1652 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1653 while (h->root.type == bfd_link_hash_indirect
1654 || h->root.type == bfd_link_hash_warning)
1655 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1656 eh = (struct elf64_x86_64_link_hash_entry *) h;
1658 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1659 if (p->sec == sec)
1661 /* Everything must go for SEC. */
1662 *pp = p->next;
1663 break;
1666 else
1668 /* A local symbol. */
1669 Elf_Internal_Sym *isym;
1671 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1672 abfd, r_symndx);
1674 /* Check relocation against local STT_GNU_IFUNC symbol. */
1675 if (isym != NULL
1676 && ELF64_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1678 h = elf64_x86_64_get_local_sym_hash (htab, abfd, rel,
1679 FALSE);
1680 if (h == NULL)
1681 abort ();
1685 r_type = ELF64_R_TYPE (rel->r_info);
1686 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1687 symtab_hdr, sym_hashes,
1688 &r_type, GOT_UNKNOWN,
1689 rel, relend, h, r_symndx))
1690 return FALSE;
1692 switch (r_type)
1694 case R_X86_64_TLSLD:
1695 if (htab->tls_ld_got.refcount > 0)
1696 htab->tls_ld_got.refcount -= 1;
1697 break;
1699 case R_X86_64_TLSGD:
1700 case R_X86_64_GOTPC32_TLSDESC:
1701 case R_X86_64_TLSDESC_CALL:
1702 case R_X86_64_GOTTPOFF:
1703 case R_X86_64_GOT32:
1704 case R_X86_64_GOTPCREL:
1705 case R_X86_64_GOT64:
1706 case R_X86_64_GOTPCREL64:
1707 case R_X86_64_GOTPLT64:
1708 if (h != NULL)
1710 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1711 h->plt.refcount -= 1;
1712 if (h->got.refcount > 0)
1713 h->got.refcount -= 1;
1714 if (h->type == STT_GNU_IFUNC)
1716 if (h->plt.refcount > 0)
1717 h->plt.refcount -= 1;
1720 else if (local_got_refcounts != NULL)
1722 if (local_got_refcounts[r_symndx] > 0)
1723 local_got_refcounts[r_symndx] -= 1;
1725 break;
1727 case R_X86_64_8:
1728 case R_X86_64_16:
1729 case R_X86_64_32:
1730 case R_X86_64_64:
1731 case R_X86_64_32S:
1732 case R_X86_64_PC8:
1733 case R_X86_64_PC16:
1734 case R_X86_64_PC32:
1735 case R_X86_64_PC64:
1736 if (info->shared)
1737 break;
1738 /* Fall thru */
1740 case R_X86_64_PLT32:
1741 case R_X86_64_PLTOFF64:
1742 if (h != NULL)
1744 if (h->plt.refcount > 0)
1745 h->plt.refcount -= 1;
1747 break;
1749 default:
1750 break;
1754 return TRUE;
1757 /* Adjust a symbol defined by a dynamic object and referenced by a
1758 regular object. The current definition is in some section of the
1759 dynamic object, but we're not including those sections. We have to
1760 change the definition to something the rest of the link can
1761 understand. */
1763 static bfd_boolean
1764 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1765 struct elf_link_hash_entry *h)
1767 struct elf64_x86_64_link_hash_table *htab;
1768 asection *s;
1770 /* STT_GNU_IFUNC symbol must go through PLT. */
1771 if (h->type == STT_GNU_IFUNC)
1773 if (h->plt.refcount <= 0)
1775 h->plt.offset = (bfd_vma) -1;
1776 h->needs_plt = 0;
1778 return TRUE;
1781 /* If this is a function, put it in the procedure linkage table. We
1782 will fill in the contents of the procedure linkage table later,
1783 when we know the address of the .got section. */
1784 if (h->type == STT_FUNC
1785 || h->needs_plt)
1787 if (h->plt.refcount <= 0
1788 || SYMBOL_CALLS_LOCAL (info, h)
1789 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1790 && h->root.type == bfd_link_hash_undefweak))
1792 /* This case can occur if we saw a PLT32 reloc in an input
1793 file, but the symbol was never referred to by a dynamic
1794 object, or if all references were garbage collected. In
1795 such a case, we don't actually need to build a procedure
1796 linkage table, and we can just do a PC32 reloc instead. */
1797 h->plt.offset = (bfd_vma) -1;
1798 h->needs_plt = 0;
1801 return TRUE;
1803 else
1804 /* It's possible that we incorrectly decided a .plt reloc was
1805 needed for an R_X86_64_PC32 reloc to a non-function sym in
1806 check_relocs. We can't decide accurately between function and
1807 non-function syms in check-relocs; Objects loaded later in
1808 the link may change h->type. So fix it now. */
1809 h->plt.offset = (bfd_vma) -1;
1811 /* If this is a weak symbol, and there is a real definition, the
1812 processor independent code will have arranged for us to see the
1813 real definition first, and we can just use the same value. */
1814 if (h->u.weakdef != NULL)
1816 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1817 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1818 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1819 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1820 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1821 h->non_got_ref = h->u.weakdef->non_got_ref;
1822 return TRUE;
1825 /* This is a reference to a symbol defined by a dynamic object which
1826 is not a function. */
1828 /* If we are creating a shared library, we must presume that the
1829 only references to the symbol are via the global offset table.
1830 For such cases we need not do anything here; the relocations will
1831 be handled correctly by relocate_section. */
1832 if (info->shared)
1833 return TRUE;
1835 /* If there are no references to this symbol that do not use the
1836 GOT, we don't need to generate a copy reloc. */
1837 if (!h->non_got_ref)
1838 return TRUE;
1840 /* If -z nocopyreloc was given, we won't generate them either. */
1841 if (info->nocopyreloc)
1843 h->non_got_ref = 0;
1844 return TRUE;
1847 if (ELIMINATE_COPY_RELOCS)
1849 struct elf64_x86_64_link_hash_entry * eh;
1850 struct elf_dyn_relocs *p;
1852 eh = (struct elf64_x86_64_link_hash_entry *) h;
1853 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1855 s = p->sec->output_section;
1856 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1857 break;
1860 /* If we didn't find any dynamic relocs in read-only sections, then
1861 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1862 if (p == NULL)
1864 h->non_got_ref = 0;
1865 return TRUE;
1869 if (h->size == 0)
1871 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1872 h->root.root.string);
1873 return TRUE;
1876 /* We must allocate the symbol in our .dynbss section, which will
1877 become part of the .bss section of the executable. There will be
1878 an entry for this symbol in the .dynsym section. The dynamic
1879 object will contain position independent code, so all references
1880 from the dynamic object to this symbol will go through the global
1881 offset table. The dynamic linker will use the .dynsym entry to
1882 determine the address it must put in the global offset table, so
1883 both the dynamic object and the regular object will refer to the
1884 same memory location for the variable. */
1886 htab = elf64_x86_64_hash_table (info);
1887 if (htab == NULL)
1888 return FALSE;
1890 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1891 to copy the initial value out of the dynamic object and into the
1892 runtime process image. */
1893 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1895 htab->srelbss->size += sizeof (Elf64_External_Rela);
1896 h->needs_copy = 1;
1899 s = htab->sdynbss;
1901 return _bfd_elf_adjust_dynamic_copy (h, s);
1904 /* Allocate space in .plt, .got and associated reloc sections for
1905 dynamic relocs. */
1907 static bfd_boolean
1908 elf64_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1910 struct bfd_link_info *info;
1911 struct elf64_x86_64_link_hash_table *htab;
1912 struct elf64_x86_64_link_hash_entry *eh;
1913 struct elf_dyn_relocs *p;
1915 if (h->root.type == bfd_link_hash_indirect)
1916 return TRUE;
1918 if (h->root.type == bfd_link_hash_warning)
1919 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1920 eh = (struct elf64_x86_64_link_hash_entry *) h;
1922 info = (struct bfd_link_info *) inf;
1923 htab = elf64_x86_64_hash_table (info);
1924 if (htab == NULL)
1925 return FALSE;
1927 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1928 here if it is defined and referenced in a non-shared object. */
1929 if (h->type == STT_GNU_IFUNC
1930 && h->def_regular)
1931 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
1932 &eh->dyn_relocs,
1933 PLT_ENTRY_SIZE,
1934 GOT_ENTRY_SIZE);
1935 else if (htab->elf.dynamic_sections_created
1936 && h->plt.refcount > 0)
1938 /* Make sure this symbol is output as a dynamic symbol.
1939 Undefined weak syms won't yet be marked as dynamic. */
1940 if (h->dynindx == -1
1941 && !h->forced_local)
1943 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1944 return FALSE;
1947 if (info->shared
1948 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1950 asection *s = htab->elf.splt;
1952 /* If this is the first .plt entry, make room for the special
1953 first entry. */
1954 if (s->size == 0)
1955 s->size += PLT_ENTRY_SIZE;
1957 h->plt.offset = s->size;
1959 /* If this symbol is not defined in a regular file, and we are
1960 not generating a shared library, then set the symbol to this
1961 location in the .plt. This is required to make function
1962 pointers compare as equal between the normal executable and
1963 the shared library. */
1964 if (! info->shared
1965 && !h->def_regular)
1967 h->root.u.def.section = s;
1968 h->root.u.def.value = h->plt.offset;
1971 /* Make room for this entry. */
1972 s->size += PLT_ENTRY_SIZE;
1974 /* We also need to make an entry in the .got.plt section, which
1975 will be placed in the .got section by the linker script. */
1976 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1978 /* We also need to make an entry in the .rela.plt section. */
1979 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1980 htab->elf.srelplt->reloc_count++;
1982 else
1984 h->plt.offset = (bfd_vma) -1;
1985 h->needs_plt = 0;
1988 else
1990 h->plt.offset = (bfd_vma) -1;
1991 h->needs_plt = 0;
1994 eh->tlsdesc_got = (bfd_vma) -1;
1996 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1997 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1998 if (h->got.refcount > 0
1999 && info->executable
2000 && h->dynindx == -1
2001 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
2003 h->got.offset = (bfd_vma) -1;
2005 else if (h->got.refcount > 0)
2007 asection *s;
2008 bfd_boolean dyn;
2009 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2011 /* Make sure this symbol is output as a dynamic symbol.
2012 Undefined weak syms won't yet be marked as dynamic. */
2013 if (h->dynindx == -1
2014 && !h->forced_local)
2016 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2017 return FALSE;
2020 if (GOT_TLS_GDESC_P (tls_type))
2022 eh->tlsdesc_got = htab->elf.sgotplt->size
2023 - elf64_x86_64_compute_jump_table_size (htab);
2024 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2025 h->got.offset = (bfd_vma) -2;
2027 if (! GOT_TLS_GDESC_P (tls_type)
2028 || GOT_TLS_GD_P (tls_type))
2030 s = htab->elf.sgot;
2031 h->got.offset = s->size;
2032 s->size += GOT_ENTRY_SIZE;
2033 if (GOT_TLS_GD_P (tls_type))
2034 s->size += GOT_ENTRY_SIZE;
2036 dyn = htab->elf.dynamic_sections_created;
2037 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2038 and two if global.
2039 R_X86_64_GOTTPOFF needs one dynamic relocation. */
2040 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2041 || tls_type == GOT_TLS_IE)
2042 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2043 else if (GOT_TLS_GD_P (tls_type))
2044 htab->elf.srelgot->size += 2 * sizeof (Elf64_External_Rela);
2045 else if (! GOT_TLS_GDESC_P (tls_type)
2046 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2047 || h->root.type != bfd_link_hash_undefweak)
2048 && (info->shared
2049 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2050 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2051 if (GOT_TLS_GDESC_P (tls_type))
2053 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
2054 htab->tlsdesc_plt = (bfd_vma) -1;
2057 else
2058 h->got.offset = (bfd_vma) -1;
2060 if (eh->dyn_relocs == NULL)
2061 return TRUE;
2063 /* In the shared -Bsymbolic case, discard space allocated for
2064 dynamic pc-relative relocs against symbols which turn out to be
2065 defined in regular objects. For the normal shared case, discard
2066 space for pc-relative relocs that have become local due to symbol
2067 visibility changes. */
2069 if (info->shared)
2071 /* Relocs that use pc_count are those that appear on a call
2072 insn, or certain REL relocs that can generated via assembly.
2073 We want calls to protected symbols to resolve directly to the
2074 function rather than going via the plt. If people want
2075 function pointer comparisons to work as expected then they
2076 should avoid writing weird assembly. */
2077 if (SYMBOL_CALLS_LOCAL (info, h))
2079 struct elf_dyn_relocs **pp;
2081 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2083 p->count -= p->pc_count;
2084 p->pc_count = 0;
2085 if (p->count == 0)
2086 *pp = p->next;
2087 else
2088 pp = &p->next;
2092 /* Also discard relocs on undefined weak syms with non-default
2093 visibility. */
2094 if (eh->dyn_relocs != NULL
2095 && h->root.type == bfd_link_hash_undefweak)
2097 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2098 eh->dyn_relocs = NULL;
2100 /* Make sure undefined weak symbols are output as a dynamic
2101 symbol in PIEs. */
2102 else if (h->dynindx == -1
2103 && ! h->forced_local
2104 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2105 return FALSE;
2109 else if (ELIMINATE_COPY_RELOCS)
2111 /* For the non-shared case, discard space for relocs against
2112 symbols which turn out to need copy relocs or are not
2113 dynamic. */
2115 if (!h->non_got_ref
2116 && ((h->def_dynamic
2117 && !h->def_regular)
2118 || (htab->elf.dynamic_sections_created
2119 && (h->root.type == bfd_link_hash_undefweak
2120 || h->root.type == bfd_link_hash_undefined))))
2122 /* Make sure this symbol is output as a dynamic symbol.
2123 Undefined weak syms won't yet be marked as dynamic. */
2124 if (h->dynindx == -1
2125 && ! h->forced_local
2126 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2127 return FALSE;
2129 /* If that succeeded, we know we'll be keeping all the
2130 relocs. */
2131 if (h->dynindx != -1)
2132 goto keep;
2135 eh->dyn_relocs = NULL;
2137 keep: ;
2140 /* Finally, allocate space. */
2141 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2143 asection * sreloc;
2145 sreloc = elf_section_data (p->sec)->sreloc;
2147 BFD_ASSERT (sreloc != NULL);
2149 sreloc->size += p->count * sizeof (Elf64_External_Rela);
2152 return TRUE;
2155 /* Allocate space in .plt, .got and associated reloc sections for
2156 local dynamic relocs. */
2158 static bfd_boolean
2159 elf64_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
2161 struct elf_link_hash_entry *h
2162 = (struct elf_link_hash_entry *) *slot;
2164 if (h->type != STT_GNU_IFUNC
2165 || !h->def_regular
2166 || !h->ref_regular
2167 || !h->forced_local
2168 || h->root.type != bfd_link_hash_defined)
2169 abort ();
2171 return elf64_x86_64_allocate_dynrelocs (h, inf);
2174 /* Find any dynamic relocs that apply to read-only sections. */
2176 static bfd_boolean
2177 elf64_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2179 struct elf64_x86_64_link_hash_entry *eh;
2180 struct elf_dyn_relocs *p;
2182 if (h->root.type == bfd_link_hash_warning)
2183 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2185 eh = (struct elf64_x86_64_link_hash_entry *) h;
2186 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2188 asection *s = p->sec->output_section;
2190 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2192 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2194 info->flags |= DF_TEXTREL;
2196 /* Not an error, just cut short the traversal. */
2197 return FALSE;
2200 return TRUE;
2203 /* Set the sizes of the dynamic sections. */
2205 static bfd_boolean
2206 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2207 struct bfd_link_info *info)
2209 struct elf64_x86_64_link_hash_table *htab;
2210 bfd *dynobj;
2211 asection *s;
2212 bfd_boolean relocs;
2213 bfd *ibfd;
2215 htab = elf64_x86_64_hash_table (info);
2216 if (htab == NULL)
2217 return FALSE;
2219 dynobj = htab->elf.dynobj;
2220 if (dynobj == NULL)
2221 abort ();
2223 if (htab->elf.dynamic_sections_created)
2225 /* Set the contents of the .interp section to the interpreter. */
2226 if (info->executable)
2228 s = bfd_get_section_by_name (dynobj, ".interp");
2229 if (s == NULL)
2230 abort ();
2231 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2232 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2236 /* Set up .got offsets for local syms, and space for local dynamic
2237 relocs. */
2238 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2240 bfd_signed_vma *local_got;
2241 bfd_signed_vma *end_local_got;
2242 char *local_tls_type;
2243 bfd_vma *local_tlsdesc_gotent;
2244 bfd_size_type locsymcount;
2245 Elf_Internal_Shdr *symtab_hdr;
2246 asection *srel;
2248 if (! is_x86_64_elf (ibfd))
2249 continue;
2251 for (s = ibfd->sections; s != NULL; s = s->next)
2253 struct elf_dyn_relocs *p;
2255 for (p = (struct elf_dyn_relocs *)
2256 (elf_section_data (s)->local_dynrel);
2257 p != NULL;
2258 p = p->next)
2260 if (!bfd_is_abs_section (p->sec)
2261 && bfd_is_abs_section (p->sec->output_section))
2263 /* Input section has been discarded, either because
2264 it is a copy of a linkonce section or due to
2265 linker script /DISCARD/, so we'll be discarding
2266 the relocs too. */
2268 else if (p->count != 0)
2270 srel = elf_section_data (p->sec)->sreloc;
2271 srel->size += p->count * sizeof (Elf64_External_Rela);
2272 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2273 info->flags |= DF_TEXTREL;
2278 local_got = elf_local_got_refcounts (ibfd);
2279 if (!local_got)
2280 continue;
2282 symtab_hdr = &elf_symtab_hdr (ibfd);
2283 locsymcount = symtab_hdr->sh_info;
2284 end_local_got = local_got + locsymcount;
2285 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
2286 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
2287 s = htab->elf.sgot;
2288 srel = htab->elf.srelgot;
2289 for (; local_got < end_local_got;
2290 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2292 *local_tlsdesc_gotent = (bfd_vma) -1;
2293 if (*local_got > 0)
2295 if (GOT_TLS_GDESC_P (*local_tls_type))
2297 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2298 - elf64_x86_64_compute_jump_table_size (htab);
2299 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2300 *local_got = (bfd_vma) -2;
2302 if (! GOT_TLS_GDESC_P (*local_tls_type)
2303 || GOT_TLS_GD_P (*local_tls_type))
2305 *local_got = s->size;
2306 s->size += GOT_ENTRY_SIZE;
2307 if (GOT_TLS_GD_P (*local_tls_type))
2308 s->size += GOT_ENTRY_SIZE;
2310 if (info->shared
2311 || GOT_TLS_GD_ANY_P (*local_tls_type)
2312 || *local_tls_type == GOT_TLS_IE)
2314 if (GOT_TLS_GDESC_P (*local_tls_type))
2316 htab->elf.srelplt->size
2317 += sizeof (Elf64_External_Rela);
2318 htab->tlsdesc_plt = (bfd_vma) -1;
2320 if (! GOT_TLS_GDESC_P (*local_tls_type)
2321 || GOT_TLS_GD_P (*local_tls_type))
2322 srel->size += sizeof (Elf64_External_Rela);
2325 else
2326 *local_got = (bfd_vma) -1;
2330 if (htab->tls_ld_got.refcount > 0)
2332 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2333 relocs. */
2334 htab->tls_ld_got.offset = htab->elf.sgot->size;
2335 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
2336 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2338 else
2339 htab->tls_ld_got.offset = -1;
2341 /* Allocate global sym .plt and .got entries, and space for global
2342 sym dynamic relocs. */
2343 elf_link_hash_traverse (&htab->elf, elf64_x86_64_allocate_dynrelocs,
2344 info);
2346 /* Allocate .plt and .got entries, and space for local symbols. */
2347 htab_traverse (htab->loc_hash_table,
2348 elf64_x86_64_allocate_local_dynrelocs,
2349 info);
2351 /* For every jump slot reserved in the sgotplt, reloc_count is
2352 incremented. However, when we reserve space for TLS descriptors,
2353 it's not incremented, so in order to compute the space reserved
2354 for them, it suffices to multiply the reloc count by the jump
2355 slot size. */
2356 if (htab->elf.srelplt)
2357 htab->sgotplt_jump_table_size
2358 = elf64_x86_64_compute_jump_table_size (htab);
2360 if (htab->tlsdesc_plt)
2362 /* If we're not using lazy TLS relocations, don't generate the
2363 PLT and GOT entries they require. */
2364 if ((info->flags & DF_BIND_NOW))
2365 htab->tlsdesc_plt = 0;
2366 else
2368 htab->tlsdesc_got = htab->elf.sgot->size;
2369 htab->elf.sgot->size += GOT_ENTRY_SIZE;
2370 /* Reserve room for the initial entry.
2371 FIXME: we could probably do away with it in this case. */
2372 if (htab->elf.splt->size == 0)
2373 htab->elf.splt->size += PLT_ENTRY_SIZE;
2374 htab->tlsdesc_plt = htab->elf.splt->size;
2375 htab->elf.splt->size += PLT_ENTRY_SIZE;
2379 if (htab->elf.sgotplt)
2381 struct elf_link_hash_entry *got;
2382 got = elf_link_hash_lookup (elf_hash_table (info),
2383 "_GLOBAL_OFFSET_TABLE_",
2384 FALSE, FALSE, FALSE);
2386 /* Don't allocate .got.plt section if there are no GOT nor PLT
2387 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
2388 if ((got == NULL
2389 || !got->ref_regular_nonweak)
2390 && (htab->elf.sgotplt->size
2391 == get_elf_backend_data (output_bfd)->got_header_size)
2392 && (htab->elf.splt == NULL
2393 || htab->elf.splt->size == 0)
2394 && (htab->elf.sgot == NULL
2395 || htab->elf.sgot->size == 0)
2396 && (htab->elf.iplt == NULL
2397 || htab->elf.iplt->size == 0)
2398 && (htab->elf.igotplt == NULL
2399 || htab->elf.igotplt->size == 0))
2400 htab->elf.sgotplt->size = 0;
2403 /* We now have determined the sizes of the various dynamic sections.
2404 Allocate memory for them. */
2405 relocs = FALSE;
2406 for (s = dynobj->sections; s != NULL; s = s->next)
2408 if ((s->flags & SEC_LINKER_CREATED) == 0)
2409 continue;
2411 if (s == htab->elf.splt
2412 || s == htab->elf.sgot
2413 || s == htab->elf.sgotplt
2414 || s == htab->elf.iplt
2415 || s == htab->elf.igotplt
2416 || s == htab->sdynbss)
2418 /* Strip this section if we don't need it; see the
2419 comment below. */
2421 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2423 if (s->size != 0 && s != htab->elf.srelplt)
2424 relocs = TRUE;
2426 /* We use the reloc_count field as a counter if we need
2427 to copy relocs into the output file. */
2428 if (s != htab->elf.srelplt)
2429 s->reloc_count = 0;
2431 else
2433 /* It's not one of our sections, so don't allocate space. */
2434 continue;
2437 if (s->size == 0)
2439 /* If we don't need this section, strip it from the
2440 output file. This is mostly to handle .rela.bss and
2441 .rela.plt. We must create both sections in
2442 create_dynamic_sections, because they must be created
2443 before the linker maps input sections to output
2444 sections. The linker does that before
2445 adjust_dynamic_symbol is called, and it is that
2446 function which decides whether anything needs to go
2447 into these sections. */
2449 s->flags |= SEC_EXCLUDE;
2450 continue;
2453 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2454 continue;
2456 /* Allocate memory for the section contents. We use bfd_zalloc
2457 here in case unused entries are not reclaimed before the
2458 section's contents are written out. This should not happen,
2459 but this way if it does, we get a R_X86_64_NONE reloc instead
2460 of garbage. */
2461 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2462 if (s->contents == NULL)
2463 return FALSE;
2466 if (htab->elf.dynamic_sections_created)
2468 /* Add some entries to the .dynamic section. We fill in the
2469 values later, in elf64_x86_64_finish_dynamic_sections, but we
2470 must add the entries now so that we get the correct size for
2471 the .dynamic section. The DT_DEBUG entry is filled in by the
2472 dynamic linker and used by the debugger. */
2473 #define add_dynamic_entry(TAG, VAL) \
2474 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2476 if (info->executable)
2478 if (!add_dynamic_entry (DT_DEBUG, 0))
2479 return FALSE;
2482 if (htab->elf.splt->size != 0)
2484 if (!add_dynamic_entry (DT_PLTGOT, 0)
2485 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2486 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2487 || !add_dynamic_entry (DT_JMPREL, 0))
2488 return FALSE;
2490 if (htab->tlsdesc_plt
2491 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2492 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2493 return FALSE;
2496 if (relocs)
2498 if (!add_dynamic_entry (DT_RELA, 0)
2499 || !add_dynamic_entry (DT_RELASZ, 0)
2500 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2501 return FALSE;
2503 /* If any dynamic relocs apply to a read-only section,
2504 then we need a DT_TEXTREL entry. */
2505 if ((info->flags & DF_TEXTREL) == 0)
2506 elf_link_hash_traverse (&htab->elf,
2507 elf64_x86_64_readonly_dynrelocs,
2508 info);
2510 if ((info->flags & DF_TEXTREL) != 0)
2512 if (!add_dynamic_entry (DT_TEXTREL, 0))
2513 return FALSE;
2517 #undef add_dynamic_entry
2519 return TRUE;
2522 static bfd_boolean
2523 elf64_x86_64_always_size_sections (bfd *output_bfd,
2524 struct bfd_link_info *info)
2526 asection *tls_sec = elf_hash_table (info)->tls_sec;
2528 if (tls_sec)
2530 struct elf_link_hash_entry *tlsbase;
2532 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2533 "_TLS_MODULE_BASE_",
2534 FALSE, FALSE, FALSE);
2536 if (tlsbase && tlsbase->type == STT_TLS)
2538 struct elf64_x86_64_link_hash_table *htab;
2539 struct bfd_link_hash_entry *bh = NULL;
2540 const struct elf_backend_data *bed
2541 = get_elf_backend_data (output_bfd);
2543 htab = elf64_x86_64_hash_table (info);
2544 if (htab == NULL)
2545 return FALSE;
2547 if (!(_bfd_generic_link_add_one_symbol
2548 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2549 tls_sec, 0, NULL, FALSE,
2550 bed->collect, &bh)))
2551 return FALSE;
2553 htab->tls_module_base = bh;
2555 tlsbase = (struct elf_link_hash_entry *)bh;
2556 tlsbase->def_regular = 1;
2557 tlsbase->other = STV_HIDDEN;
2558 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2562 return TRUE;
2565 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2566 executables. Rather than setting it to the beginning of the TLS
2567 section, we have to set it to the end. This function may be called
2568 multiple times, it is idempotent. */
2570 static void
2571 elf64_x86_64_set_tls_module_base (struct bfd_link_info *info)
2573 struct elf64_x86_64_link_hash_table *htab;
2574 struct bfd_link_hash_entry *base;
2576 if (!info->executable)
2577 return;
2579 htab = elf64_x86_64_hash_table (info);
2580 if (htab == NULL)
2581 return;
2583 base = htab->tls_module_base;
2584 if (base == NULL)
2585 return;
2587 base->u.def.value = htab->elf.tls_size;
2590 /* Return the base VMA address which should be subtracted from real addresses
2591 when resolving @dtpoff relocation.
2592 This is PT_TLS segment p_vaddr. */
2594 static bfd_vma
2595 elf64_x86_64_dtpoff_base (struct bfd_link_info *info)
2597 /* If tls_sec is NULL, we should have signalled an error already. */
2598 if (elf_hash_table (info)->tls_sec == NULL)
2599 return 0;
2600 return elf_hash_table (info)->tls_sec->vma;
2603 /* Return the relocation value for @tpoff relocation
2604 if STT_TLS virtual address is ADDRESS. */
2606 static bfd_vma
2607 elf64_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2609 struct elf_link_hash_table *htab = elf_hash_table (info);
2611 /* If tls_segment is NULL, we should have signalled an error already. */
2612 if (htab->tls_sec == NULL)
2613 return 0;
2614 return address - htab->tls_size - htab->tls_sec->vma;
2617 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2618 branch? */
2620 static bfd_boolean
2621 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2623 /* Opcode Instruction
2624 0xe8 call
2625 0xe9 jump
2626 0x0f 0x8x conditional jump */
2627 return ((offset > 0
2628 && (contents [offset - 1] == 0xe8
2629 || contents [offset - 1] == 0xe9))
2630 || (offset > 1
2631 && contents [offset - 2] == 0x0f
2632 && (contents [offset - 1] & 0xf0) == 0x80));
2635 static void
2636 elf64_x86_64_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
2638 bfd_byte *loc = s->contents;
2639 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2640 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2641 <= s->contents + s->size);
2642 bfd_elf64_swap_reloca_out (abfd, rel, loc);
2645 /* Relocate an x86_64 ELF section. */
2647 static bfd_boolean
2648 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2649 bfd *input_bfd, asection *input_section,
2650 bfd_byte *contents, Elf_Internal_Rela *relocs,
2651 Elf_Internal_Sym *local_syms,
2652 asection **local_sections)
2654 struct elf64_x86_64_link_hash_table *htab;
2655 Elf_Internal_Shdr *symtab_hdr;
2656 struct elf_link_hash_entry **sym_hashes;
2657 bfd_vma *local_got_offsets;
2658 bfd_vma *local_tlsdesc_gotents;
2659 Elf_Internal_Rela *rel;
2660 Elf_Internal_Rela *relend;
2662 BFD_ASSERT (is_x86_64_elf (input_bfd));
2664 htab = elf64_x86_64_hash_table (info);
2665 if (htab == NULL)
2666 return FALSE;
2667 symtab_hdr = &elf_symtab_hdr (input_bfd);
2668 sym_hashes = elf_sym_hashes (input_bfd);
2669 local_got_offsets = elf_local_got_offsets (input_bfd);
2670 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2672 elf64_x86_64_set_tls_module_base (info);
2674 rel = relocs;
2675 relend = relocs + input_section->reloc_count;
2676 for (; rel < relend; rel++)
2678 unsigned int r_type;
2679 reloc_howto_type *howto;
2680 unsigned long r_symndx;
2681 struct elf_link_hash_entry *h;
2682 Elf_Internal_Sym *sym;
2683 asection *sec;
2684 bfd_vma off, offplt;
2685 bfd_vma relocation;
2686 bfd_boolean unresolved_reloc;
2687 bfd_reloc_status_type r;
2688 int tls_type;
2689 asection *base_got;
2691 r_type = ELF64_R_TYPE (rel->r_info);
2692 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2693 || r_type == (int) R_X86_64_GNU_VTENTRY)
2694 continue;
2696 if (r_type >= R_X86_64_max)
2698 bfd_set_error (bfd_error_bad_value);
2699 return FALSE;
2702 howto = x86_64_elf_howto_table + r_type;
2703 r_symndx = ELF64_R_SYM (rel->r_info);
2704 h = NULL;
2705 sym = NULL;
2706 sec = NULL;
2707 unresolved_reloc = FALSE;
2708 if (r_symndx < symtab_hdr->sh_info)
2710 sym = local_syms + r_symndx;
2711 sec = local_sections[r_symndx];
2713 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2714 &sec, rel);
2716 /* Relocate against local STT_GNU_IFUNC symbol. */
2717 if (!info->relocatable
2718 && ELF64_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2720 h = elf64_x86_64_get_local_sym_hash (htab, input_bfd,
2721 rel, FALSE);
2722 if (h == NULL)
2723 abort ();
2725 /* Set STT_GNU_IFUNC symbol value. */
2726 h->root.u.def.value = sym->st_value;
2727 h->root.u.def.section = sec;
2730 else
2732 bfd_boolean warned ATTRIBUTE_UNUSED;
2734 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2735 r_symndx, symtab_hdr, sym_hashes,
2736 h, sec, relocation,
2737 unresolved_reloc, warned);
2740 if (sec != NULL && elf_discarded_section (sec))
2741 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2742 rel, relend, howto, contents);
2744 if (info->relocatable)
2745 continue;
2747 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2748 it here if it is defined in a non-shared object. */
2749 if (h != NULL
2750 && h->type == STT_GNU_IFUNC
2751 && h->def_regular)
2753 asection *plt;
2754 bfd_vma plt_index;
2755 const char *name;
2757 if ((input_section->flags & SEC_ALLOC) == 0
2758 || h->plt.offset == (bfd_vma) -1)
2759 abort ();
2761 /* STT_GNU_IFUNC symbol must go through PLT. */
2762 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2763 relocation = (plt->output_section->vma
2764 + plt->output_offset + h->plt.offset);
2766 switch (r_type)
2768 default:
2769 if (h->root.root.string)
2770 name = h->root.root.string;
2771 else
2772 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2773 NULL);
2774 (*_bfd_error_handler)
2775 (_("%B: relocation %s against STT_GNU_IFUNC "
2776 "symbol `%s' isn't handled by %s"), input_bfd,
2777 x86_64_elf_howto_table[r_type].name,
2778 name, __FUNCTION__);
2779 bfd_set_error (bfd_error_bad_value);
2780 return FALSE;
2782 case R_X86_64_32S:
2783 if (info->shared)
2784 abort ();
2785 goto do_relocation;
2787 case R_X86_64_64:
2788 if (rel->r_addend != 0)
2790 if (h->root.root.string)
2791 name = h->root.root.string;
2792 else
2793 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2794 sym, NULL);
2795 (*_bfd_error_handler)
2796 (_("%B: relocation %s against STT_GNU_IFUNC "
2797 "symbol `%s' has non-zero addend: %d"),
2798 input_bfd, x86_64_elf_howto_table[r_type].name,
2799 name, rel->r_addend);
2800 bfd_set_error (bfd_error_bad_value);
2801 return FALSE;
2804 /* Generate dynamic relcoation only when there is a
2805 non-GOF reference in a shared object. */
2806 if (info->shared && h->non_got_ref)
2808 Elf_Internal_Rela outrel;
2809 asection *sreloc;
2811 /* Need a dynamic relocation to get the real function
2812 address. */
2813 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2814 info,
2815 input_section,
2816 rel->r_offset);
2817 if (outrel.r_offset == (bfd_vma) -1
2818 || outrel.r_offset == (bfd_vma) -2)
2819 abort ();
2821 outrel.r_offset += (input_section->output_section->vma
2822 + input_section->output_offset);
2824 if (h->dynindx == -1
2825 || h->forced_local
2826 || info->executable)
2828 /* This symbol is resolved locally. */
2829 outrel.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
2830 outrel.r_addend = (h->root.u.def.value
2831 + h->root.u.def.section->output_section->vma
2832 + h->root.u.def.section->output_offset);
2834 else
2836 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2837 outrel.r_addend = 0;
2840 sreloc = htab->elf.irelifunc;
2841 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
2843 /* If this reloc is against an external symbol, we
2844 do not want to fiddle with the addend. Otherwise,
2845 we need to include the symbol value so that it
2846 becomes an addend for the dynamic reloc. For an
2847 internal symbol, we have updated addend. */
2848 continue;
2851 case R_X86_64_32:
2852 case R_X86_64_PC32:
2853 case R_X86_64_PC64:
2854 case R_X86_64_PLT32:
2855 goto do_relocation;
2857 case R_X86_64_GOTPCREL:
2858 case R_X86_64_GOTPCREL64:
2859 base_got = htab->elf.sgot;
2860 off = h->got.offset;
2862 if (base_got == NULL)
2863 abort ();
2865 if (off == (bfd_vma) -1)
2867 /* We can't use h->got.offset here to save state, or
2868 even just remember the offset, as finish_dynamic_symbol
2869 would use that as offset into .got. */
2871 if (htab->elf.splt != NULL)
2873 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2874 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2875 base_got = htab->elf.sgotplt;
2877 else
2879 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2880 off = plt_index * GOT_ENTRY_SIZE;
2881 base_got = htab->elf.igotplt;
2884 if (h->dynindx == -1
2885 || h->forced_local
2886 || info->symbolic)
2888 /* This references the local defitionion. We must
2889 initialize this entry in the global offset table.
2890 Since the offset must always be a multiple of 8,
2891 we use the least significant bit to record
2892 whether we have initialized it already.
2894 When doing a dynamic link, we create a .rela.got
2895 relocation entry to initialize the value. This
2896 is done in the finish_dynamic_symbol routine. */
2897 if ((off & 1) != 0)
2898 off &= ~1;
2899 else
2901 bfd_put_64 (output_bfd, relocation,
2902 base_got->contents + off);
2903 /* Note that this is harmless for the GOTPLT64
2904 case, as -1 | 1 still is -1. */
2905 h->got.offset |= 1;
2910 relocation = (base_got->output_section->vma
2911 + base_got->output_offset + off);
2913 if (r_type != R_X86_64_GOTPCREL
2914 && r_type != R_X86_64_GOTPCREL64)
2916 asection *gotplt;
2917 if (htab->elf.splt != NULL)
2918 gotplt = htab->elf.sgotplt;
2919 else
2920 gotplt = htab->elf.igotplt;
2921 relocation -= (gotplt->output_section->vma
2922 - gotplt->output_offset);
2925 goto do_relocation;
2929 /* When generating a shared object, the relocations handled here are
2930 copied into the output file to be resolved at run time. */
2931 switch (r_type)
2933 case R_X86_64_GOT32:
2934 case R_X86_64_GOT64:
2935 /* Relocation is to the entry for this symbol in the global
2936 offset table. */
2937 case R_X86_64_GOTPCREL:
2938 case R_X86_64_GOTPCREL64:
2939 /* Use global offset table entry as symbol value. */
2940 case R_X86_64_GOTPLT64:
2941 /* This is the same as GOT64 for relocation purposes, but
2942 indicates the existence of a PLT entry. The difficulty is,
2943 that we must calculate the GOT slot offset from the PLT
2944 offset, if this symbol got a PLT entry (it was global).
2945 Additionally if it's computed from the PLT entry, then that
2946 GOT offset is relative to .got.plt, not to .got. */
2947 base_got = htab->elf.sgot;
2949 if (htab->elf.sgot == NULL)
2950 abort ();
2952 if (h != NULL)
2954 bfd_boolean dyn;
2956 off = h->got.offset;
2957 if (h->needs_plt
2958 && h->plt.offset != (bfd_vma)-1
2959 && off == (bfd_vma)-1)
2961 /* We can't use h->got.offset here to save
2962 state, or even just remember the offset, as
2963 finish_dynamic_symbol would use that as offset into
2964 .got. */
2965 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2966 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2967 base_got = htab->elf.sgotplt;
2970 dyn = htab->elf.dynamic_sections_created;
2972 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2973 || (info->shared
2974 && SYMBOL_REFERENCES_LOCAL (info, h))
2975 || (ELF_ST_VISIBILITY (h->other)
2976 && h->root.type == bfd_link_hash_undefweak))
2978 /* This is actually a static link, or it is a -Bsymbolic
2979 link and the symbol is defined locally, or the symbol
2980 was forced to be local because of a version file. We
2981 must initialize this entry in the global offset table.
2982 Since the offset must always be a multiple of 8, we
2983 use the least significant bit to record whether we
2984 have initialized it already.
2986 When doing a dynamic link, we create a .rela.got
2987 relocation entry to initialize the value. This is
2988 done in the finish_dynamic_symbol routine. */
2989 if ((off & 1) != 0)
2990 off &= ~1;
2991 else
2993 bfd_put_64 (output_bfd, relocation,
2994 base_got->contents + off);
2995 /* Note that this is harmless for the GOTPLT64 case,
2996 as -1 | 1 still is -1. */
2997 h->got.offset |= 1;
3000 else
3001 unresolved_reloc = FALSE;
3003 else
3005 if (local_got_offsets == NULL)
3006 abort ();
3008 off = local_got_offsets[r_symndx];
3010 /* The offset must always be a multiple of 8. We use
3011 the least significant bit to record whether we have
3012 already generated the necessary reloc. */
3013 if ((off & 1) != 0)
3014 off &= ~1;
3015 else
3017 bfd_put_64 (output_bfd, relocation,
3018 base_got->contents + off);
3020 if (info->shared)
3022 asection *s;
3023 Elf_Internal_Rela outrel;
3025 /* We need to generate a R_X86_64_RELATIVE reloc
3026 for the dynamic linker. */
3027 s = htab->elf.srelgot;
3028 if (s == NULL)
3029 abort ();
3031 outrel.r_offset = (base_got->output_section->vma
3032 + base_got->output_offset
3033 + off);
3034 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3035 outrel.r_addend = relocation;
3036 elf64_x86_64_append_rela (output_bfd, s, &outrel);
3039 local_got_offsets[r_symndx] |= 1;
3043 if (off >= (bfd_vma) -2)
3044 abort ();
3046 relocation = base_got->output_section->vma
3047 + base_got->output_offset + off;
3048 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
3049 relocation -= htab->elf.sgotplt->output_section->vma
3050 - htab->elf.sgotplt->output_offset;
3052 break;
3054 case R_X86_64_GOTOFF64:
3055 /* Relocation is relative to the start of the global offset
3056 table. */
3058 /* Check to make sure it isn't a protected function symbol
3059 for shared library since it may not be local when used
3060 as function address. */
3061 if (info->shared
3062 && h
3063 && h->def_regular
3064 && h->type == STT_FUNC
3065 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3067 (*_bfd_error_handler)
3068 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3069 input_bfd, h->root.root.string);
3070 bfd_set_error (bfd_error_bad_value);
3071 return FALSE;
3074 /* Note that sgot is not involved in this
3075 calculation. We always want the start of .got.plt. If we
3076 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3077 permitted by the ABI, we might have to change this
3078 calculation. */
3079 relocation -= htab->elf.sgotplt->output_section->vma
3080 + htab->elf.sgotplt->output_offset;
3081 break;
3083 case R_X86_64_GOTPC32:
3084 case R_X86_64_GOTPC64:
3085 /* Use global offset table as symbol value. */
3086 relocation = htab->elf.sgotplt->output_section->vma
3087 + htab->elf.sgotplt->output_offset;
3088 unresolved_reloc = FALSE;
3089 break;
3091 case R_X86_64_PLTOFF64:
3092 /* Relocation is PLT entry relative to GOT. For local
3093 symbols it's the symbol itself relative to GOT. */
3094 if (h != NULL
3095 /* See PLT32 handling. */
3096 && h->plt.offset != (bfd_vma) -1
3097 && htab->elf.splt != NULL)
3099 relocation = (htab->elf.splt->output_section->vma
3100 + htab->elf.splt->output_offset
3101 + h->plt.offset);
3102 unresolved_reloc = FALSE;
3105 relocation -= htab->elf.sgotplt->output_section->vma
3106 + htab->elf.sgotplt->output_offset;
3107 break;
3109 case R_X86_64_PLT32:
3110 /* Relocation is to the entry for this symbol in the
3111 procedure linkage table. */
3113 /* Resolve a PLT32 reloc against a local symbol directly,
3114 without using the procedure linkage table. */
3115 if (h == NULL)
3116 break;
3118 if (h->plt.offset == (bfd_vma) -1
3119 || htab->elf.splt == NULL)
3121 /* We didn't make a PLT entry for this symbol. This
3122 happens when statically linking PIC code, or when
3123 using -Bsymbolic. */
3124 break;
3127 relocation = (htab->elf.splt->output_section->vma
3128 + htab->elf.splt->output_offset
3129 + h->plt.offset);
3130 unresolved_reloc = FALSE;
3131 break;
3133 case R_X86_64_PC8:
3134 case R_X86_64_PC16:
3135 case R_X86_64_PC32:
3136 if (info->shared
3137 && (input_section->flags & SEC_ALLOC) != 0
3138 && (input_section->flags & SEC_READONLY) != 0
3139 && h != NULL)
3141 bfd_boolean fail = FALSE;
3142 bfd_boolean branch
3143 = (r_type == R_X86_64_PC32
3144 && is_32bit_relative_branch (contents, rel->r_offset));
3146 if (SYMBOL_REFERENCES_LOCAL (info, h))
3148 /* Symbol is referenced locally. Make sure it is
3149 defined locally or for a branch. */
3150 fail = !h->def_regular && !branch;
3152 else
3154 /* Symbol isn't referenced locally. We only allow
3155 branch to symbol with non-default visibility. */
3156 fail = (!branch
3157 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3160 if (fail)
3162 const char *fmt;
3163 const char *v;
3164 const char *pic = "";
3166 switch (ELF_ST_VISIBILITY (h->other))
3168 case STV_HIDDEN:
3169 v = _("hidden symbol");
3170 break;
3171 case STV_INTERNAL:
3172 v = _("internal symbol");
3173 break;
3174 case STV_PROTECTED:
3175 v = _("protected symbol");
3176 break;
3177 default:
3178 v = _("symbol");
3179 pic = _("; recompile with -fPIC");
3180 break;
3183 if (h->def_regular)
3184 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3185 else
3186 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3188 (*_bfd_error_handler) (fmt, input_bfd,
3189 x86_64_elf_howto_table[r_type].name,
3190 v, h->root.root.string, pic);
3191 bfd_set_error (bfd_error_bad_value);
3192 return FALSE;
3195 /* Fall through. */
3197 case R_X86_64_8:
3198 case R_X86_64_16:
3199 case R_X86_64_32:
3200 case R_X86_64_PC64:
3201 case R_X86_64_64:
3202 /* FIXME: The ABI says the linker should make sure the value is
3203 the same when it's zeroextended to 64 bit. */
3205 if ((input_section->flags & SEC_ALLOC) == 0)
3206 break;
3208 if ((info->shared
3209 && (h == NULL
3210 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3211 || h->root.type != bfd_link_hash_undefweak)
3212 && (! IS_X86_64_PCREL_TYPE (r_type)
3213 || ! SYMBOL_CALLS_LOCAL (info, h)))
3214 || (ELIMINATE_COPY_RELOCS
3215 && !info->shared
3216 && h != NULL
3217 && h->dynindx != -1
3218 && !h->non_got_ref
3219 && ((h->def_dynamic
3220 && !h->def_regular)
3221 || h->root.type == bfd_link_hash_undefweak
3222 || h->root.type == bfd_link_hash_undefined)))
3224 Elf_Internal_Rela outrel;
3225 bfd_boolean skip, relocate;
3226 asection *sreloc;
3228 /* When generating a shared object, these relocations
3229 are copied into the output file to be resolved at run
3230 time. */
3231 skip = FALSE;
3232 relocate = FALSE;
3234 outrel.r_offset =
3235 _bfd_elf_section_offset (output_bfd, info, input_section,
3236 rel->r_offset);
3237 if (outrel.r_offset == (bfd_vma) -1)
3238 skip = TRUE;
3239 else if (outrel.r_offset == (bfd_vma) -2)
3240 skip = TRUE, relocate = TRUE;
3242 outrel.r_offset += (input_section->output_section->vma
3243 + input_section->output_offset);
3245 if (skip)
3246 memset (&outrel, 0, sizeof outrel);
3248 /* h->dynindx may be -1 if this symbol was marked to
3249 become local. */
3250 else if (h != NULL
3251 && h->dynindx != -1
3252 && (IS_X86_64_PCREL_TYPE (r_type)
3253 || ! info->shared
3254 || ! SYMBOLIC_BIND (info, h)
3255 || ! h->def_regular))
3257 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3258 outrel.r_addend = rel->r_addend;
3260 else
3262 /* This symbol is local, or marked to become local. */
3263 if (r_type == R_X86_64_64)
3265 relocate = TRUE;
3266 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3267 outrel.r_addend = relocation + rel->r_addend;
3269 else
3271 long sindx;
3273 if (bfd_is_abs_section (sec))
3274 sindx = 0;
3275 else if (sec == NULL || sec->owner == NULL)
3277 bfd_set_error (bfd_error_bad_value);
3278 return FALSE;
3280 else
3282 asection *osec;
3284 /* We are turning this relocation into one
3285 against a section symbol. It would be
3286 proper to subtract the symbol's value,
3287 osec->vma, from the emitted reloc addend,
3288 but ld.so expects buggy relocs. */
3289 osec = sec->output_section;
3290 sindx = elf_section_data (osec)->dynindx;
3291 if (sindx == 0)
3293 asection *oi = htab->elf.text_index_section;
3294 sindx = elf_section_data (oi)->dynindx;
3296 BFD_ASSERT (sindx != 0);
3299 outrel.r_info = ELF64_R_INFO (sindx, r_type);
3300 outrel.r_addend = relocation + rel->r_addend;
3304 sreloc = elf_section_data (input_section)->sreloc;
3306 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3308 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3310 /* If this reloc is against an external symbol, we do
3311 not want to fiddle with the addend. Otherwise, we
3312 need to include the symbol value so that it becomes
3313 an addend for the dynamic reloc. */
3314 if (! relocate)
3315 continue;
3318 break;
3320 case R_X86_64_TLSGD:
3321 case R_X86_64_GOTPC32_TLSDESC:
3322 case R_X86_64_TLSDESC_CALL:
3323 case R_X86_64_GOTTPOFF:
3324 tls_type = GOT_UNKNOWN;
3325 if (h == NULL && local_got_offsets)
3326 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3327 else if (h != NULL)
3328 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
3330 if (! elf64_x86_64_tls_transition (info, input_bfd,
3331 input_section, contents,
3332 symtab_hdr, sym_hashes,
3333 &r_type, tls_type, rel,
3334 relend, h, r_symndx))
3335 return FALSE;
3337 if (r_type == R_X86_64_TPOFF32)
3339 bfd_vma roff = rel->r_offset;
3341 BFD_ASSERT (! unresolved_reloc);
3343 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3345 /* GD->LE transition.
3346 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3347 .word 0x6666; rex64; call __tls_get_addr
3348 Change it into:
3349 movq %fs:0, %rax
3350 leaq foo@tpoff(%rax), %rax */
3351 memcpy (contents + roff - 4,
3352 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3353 16);
3354 bfd_put_32 (output_bfd,
3355 elf64_x86_64_tpoff (info, relocation),
3356 contents + roff + 8);
3357 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3358 rel++;
3359 continue;
3361 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3363 /* GDesc -> LE transition.
3364 It's originally something like:
3365 leaq x@tlsdesc(%rip), %rax
3367 Change it to:
3368 movl $x@tpoff, %rax. */
3370 unsigned int val, type;
3372 type = bfd_get_8 (input_bfd, contents + roff - 3);
3373 val = bfd_get_8 (input_bfd, contents + roff - 1);
3374 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3375 contents + roff - 3);
3376 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3377 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3378 contents + roff - 1);
3379 bfd_put_32 (output_bfd,
3380 elf64_x86_64_tpoff (info, relocation),
3381 contents + roff);
3382 continue;
3384 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3386 /* GDesc -> LE transition.
3387 It's originally:
3388 call *(%rax)
3389 Turn it into:
3390 xchg %ax,%ax. */
3391 bfd_put_8 (output_bfd, 0x66, contents + roff);
3392 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3393 continue;
3395 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3397 /* IE->LE transition:
3398 Originally it can be one of:
3399 movq foo@gottpoff(%rip), %reg
3400 addq foo@gottpoff(%rip), %reg
3401 We change it into:
3402 movq $foo, %reg
3403 leaq foo(%reg), %reg
3404 addq $foo, %reg. */
3406 unsigned int val, type, reg;
3408 val = bfd_get_8 (input_bfd, contents + roff - 3);
3409 type = bfd_get_8 (input_bfd, contents + roff - 2);
3410 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3411 reg >>= 3;
3412 if (type == 0x8b)
3414 /* movq */
3415 if (val == 0x4c)
3416 bfd_put_8 (output_bfd, 0x49,
3417 contents + roff - 3);
3418 bfd_put_8 (output_bfd, 0xc7,
3419 contents + roff - 2);
3420 bfd_put_8 (output_bfd, 0xc0 | reg,
3421 contents + roff - 1);
3423 else if (reg == 4)
3425 /* addq -> addq - addressing with %rsp/%r12 is
3426 special */
3427 if (val == 0x4c)
3428 bfd_put_8 (output_bfd, 0x49,
3429 contents + roff - 3);
3430 bfd_put_8 (output_bfd, 0x81,
3431 contents + roff - 2);
3432 bfd_put_8 (output_bfd, 0xc0 | reg,
3433 contents + roff - 1);
3435 else
3437 /* addq -> leaq */
3438 if (val == 0x4c)
3439 bfd_put_8 (output_bfd, 0x4d,
3440 contents + roff - 3);
3441 bfd_put_8 (output_bfd, 0x8d,
3442 contents + roff - 2);
3443 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3444 contents + roff - 1);
3446 bfd_put_32 (output_bfd,
3447 elf64_x86_64_tpoff (info, relocation),
3448 contents + roff);
3449 continue;
3451 else
3452 BFD_ASSERT (FALSE);
3455 if (htab->elf.sgot == NULL)
3456 abort ();
3458 if (h != NULL)
3460 off = h->got.offset;
3461 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
3463 else
3465 if (local_got_offsets == NULL)
3466 abort ();
3468 off = local_got_offsets[r_symndx];
3469 offplt = local_tlsdesc_gotents[r_symndx];
3472 if ((off & 1) != 0)
3473 off &= ~1;
3474 else
3476 Elf_Internal_Rela outrel;
3477 int dr_type, indx;
3478 asection *sreloc;
3480 if (htab->elf.srelgot == NULL)
3481 abort ();
3483 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3485 if (GOT_TLS_GDESC_P (tls_type))
3487 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
3488 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3489 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3490 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3491 + htab->elf.sgotplt->output_offset
3492 + offplt
3493 + htab->sgotplt_jump_table_size);
3494 sreloc = htab->elf.srelplt;
3495 if (indx == 0)
3496 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3497 else
3498 outrel.r_addend = 0;
3499 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3502 sreloc = htab->elf.srelgot;
3504 outrel.r_offset = (htab->elf.sgot->output_section->vma
3505 + htab->elf.sgot->output_offset + off);
3507 if (GOT_TLS_GD_P (tls_type))
3508 dr_type = R_X86_64_DTPMOD64;
3509 else if (GOT_TLS_GDESC_P (tls_type))
3510 goto dr_done;
3511 else
3512 dr_type = R_X86_64_TPOFF64;
3514 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3515 outrel.r_addend = 0;
3516 if ((dr_type == R_X86_64_TPOFF64
3517 || dr_type == R_X86_64_TLSDESC) && indx == 0)
3518 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3519 outrel.r_info = ELF64_R_INFO (indx, dr_type);
3521 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3523 if (GOT_TLS_GD_P (tls_type))
3525 if (indx == 0)
3527 BFD_ASSERT (! unresolved_reloc);
3528 bfd_put_64 (output_bfd,
3529 relocation - elf64_x86_64_dtpoff_base (info),
3530 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3532 else
3534 bfd_put_64 (output_bfd, 0,
3535 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3536 outrel.r_info = ELF64_R_INFO (indx,
3537 R_X86_64_DTPOFF64);
3538 outrel.r_offset += GOT_ENTRY_SIZE;
3539 elf64_x86_64_append_rela (output_bfd, sreloc,
3540 &outrel);
3544 dr_done:
3545 if (h != NULL)
3546 h->got.offset |= 1;
3547 else
3548 local_got_offsets[r_symndx] |= 1;
3551 if (off >= (bfd_vma) -2
3552 && ! GOT_TLS_GDESC_P (tls_type))
3553 abort ();
3554 if (r_type == ELF64_R_TYPE (rel->r_info))
3556 if (r_type == R_X86_64_GOTPC32_TLSDESC
3557 || r_type == R_X86_64_TLSDESC_CALL)
3558 relocation = htab->elf.sgotplt->output_section->vma
3559 + htab->elf.sgotplt->output_offset
3560 + offplt + htab->sgotplt_jump_table_size;
3561 else
3562 relocation = htab->elf.sgot->output_section->vma
3563 + htab->elf.sgot->output_offset + off;
3564 unresolved_reloc = FALSE;
3566 else
3568 bfd_vma roff = rel->r_offset;
3570 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3572 /* GD->IE transition.
3573 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3574 .word 0x6666; rex64; call __tls_get_addr@plt
3575 Change it into:
3576 movq %fs:0, %rax
3577 addq foo@gottpoff(%rip), %rax */
3578 memcpy (contents + roff - 4,
3579 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3580 16);
3582 relocation = (htab->elf.sgot->output_section->vma
3583 + htab->elf.sgot->output_offset + off
3584 - roff
3585 - input_section->output_section->vma
3586 - input_section->output_offset
3587 - 12);
3588 bfd_put_32 (output_bfd, relocation,
3589 contents + roff + 8);
3590 /* Skip R_X86_64_PLT32. */
3591 rel++;
3592 continue;
3594 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3596 /* GDesc -> IE transition.
3597 It's originally something like:
3598 leaq x@tlsdesc(%rip), %rax
3600 Change it to:
3601 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
3603 /* Now modify the instruction as appropriate. To
3604 turn a leaq into a movq in the form we use it, it
3605 suffices to change the second byte from 0x8d to
3606 0x8b. */
3607 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3609 bfd_put_32 (output_bfd,
3610 htab->elf.sgot->output_section->vma
3611 + htab->elf.sgot->output_offset + off
3612 - rel->r_offset
3613 - input_section->output_section->vma
3614 - input_section->output_offset
3615 - 4,
3616 contents + roff);
3617 continue;
3619 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3621 /* GDesc -> IE transition.
3622 It's originally:
3623 call *(%rax)
3625 Change it to:
3626 xchg %ax, %ax. */
3628 bfd_put_8 (output_bfd, 0x66, contents + roff);
3629 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3630 continue;
3632 else
3633 BFD_ASSERT (FALSE);
3635 break;
3637 case R_X86_64_TLSLD:
3638 if (! elf64_x86_64_tls_transition (info, input_bfd,
3639 input_section, contents,
3640 symtab_hdr, sym_hashes,
3641 &r_type, GOT_UNKNOWN,
3642 rel, relend, h, r_symndx))
3643 return FALSE;
3645 if (r_type != R_X86_64_TLSLD)
3647 /* LD->LE transition:
3648 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3649 We change it into:
3650 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3652 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3653 memcpy (contents + rel->r_offset - 3,
3654 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3655 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3656 rel++;
3657 continue;
3660 if (htab->elf.sgot == NULL)
3661 abort ();
3663 off = htab->tls_ld_got.offset;
3664 if (off & 1)
3665 off &= ~1;
3666 else
3668 Elf_Internal_Rela outrel;
3670 if (htab->elf.srelgot == NULL)
3671 abort ();
3673 outrel.r_offset = (htab->elf.sgot->output_section->vma
3674 + htab->elf.sgot->output_offset + off);
3676 bfd_put_64 (output_bfd, 0,
3677 htab->elf.sgot->contents + off);
3678 bfd_put_64 (output_bfd, 0,
3679 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3680 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3681 outrel.r_addend = 0;
3682 elf64_x86_64_append_rela (output_bfd, htab->elf.srelgot,
3683 &outrel);
3684 htab->tls_ld_got.offset |= 1;
3686 relocation = htab->elf.sgot->output_section->vma
3687 + htab->elf.sgot->output_offset + off;
3688 unresolved_reloc = FALSE;
3689 break;
3691 case R_X86_64_DTPOFF32:
3692 if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
3693 relocation -= elf64_x86_64_dtpoff_base (info);
3694 else
3695 relocation = elf64_x86_64_tpoff (info, relocation);
3696 break;
3698 case R_X86_64_TPOFF32:
3699 BFD_ASSERT (info->executable);
3700 relocation = elf64_x86_64_tpoff (info, relocation);
3701 break;
3703 default:
3704 break;
3707 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3708 because such sections are not SEC_ALLOC and thus ld.so will
3709 not process them. */
3710 if (unresolved_reloc
3711 && !((input_section->flags & SEC_DEBUGGING) != 0
3712 && h->def_dynamic))
3713 (*_bfd_error_handler)
3714 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3715 input_bfd,
3716 input_section,
3717 (long) rel->r_offset,
3718 howto->name,
3719 h->root.root.string);
3721 do_relocation:
3722 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3723 contents, rel->r_offset,
3724 relocation, rel->r_addend);
3726 if (r != bfd_reloc_ok)
3728 const char *name;
3730 if (h != NULL)
3731 name = h->root.root.string;
3732 else
3734 name = bfd_elf_string_from_elf_section (input_bfd,
3735 symtab_hdr->sh_link,
3736 sym->st_name);
3737 if (name == NULL)
3738 return FALSE;
3739 if (*name == '\0')
3740 name = bfd_section_name (input_bfd, sec);
3743 if (r == bfd_reloc_overflow)
3745 if (! ((*info->callbacks->reloc_overflow)
3746 (info, (h ? &h->root : NULL), name, howto->name,
3747 (bfd_vma) 0, input_bfd, input_section,
3748 rel->r_offset)))
3749 return FALSE;
3751 else
3753 (*_bfd_error_handler)
3754 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3755 input_bfd, input_section,
3756 (long) rel->r_offset, name, (int) r);
3757 return FALSE;
3762 return TRUE;
3765 /* Finish up dynamic symbol handling. We set the contents of various
3766 dynamic sections here. */
3768 static bfd_boolean
3769 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3770 struct bfd_link_info *info,
3771 struct elf_link_hash_entry *h,
3772 Elf_Internal_Sym *sym)
3774 struct elf64_x86_64_link_hash_table *htab;
3776 htab = elf64_x86_64_hash_table (info);
3777 if (htab == NULL)
3778 return FALSE;
3780 if (h->plt.offset != (bfd_vma) -1)
3782 bfd_vma plt_index;
3783 bfd_vma got_offset;
3784 Elf_Internal_Rela rela;
3785 bfd_byte *loc;
3786 asection *plt, *gotplt, *relplt;
3788 /* When building a static executable, use .iplt, .igot.plt and
3789 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3790 if (htab->elf.splt != NULL)
3792 plt = htab->elf.splt;
3793 gotplt = htab->elf.sgotplt;
3794 relplt = htab->elf.srelplt;
3796 else
3798 plt = htab->elf.iplt;
3799 gotplt = htab->elf.igotplt;
3800 relplt = htab->elf.irelplt;
3803 /* This symbol has an entry in the procedure linkage table. Set
3804 it up. */
3805 if ((h->dynindx == -1
3806 && !((h->forced_local || info->executable)
3807 && h->def_regular
3808 && h->type == STT_GNU_IFUNC))
3809 || plt == NULL
3810 || gotplt == NULL
3811 || relplt == NULL)
3812 abort ();
3814 /* Get the index in the procedure linkage table which
3815 corresponds to this symbol. This is the index of this symbol
3816 in all the symbols for which we are making plt entries. The
3817 first entry in the procedure linkage table is reserved.
3819 Get the offset into the .got table of the entry that
3820 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3821 bytes. The first three are reserved for the dynamic linker.
3823 For static executables, we don't reserve anything. */
3825 if (plt == htab->elf.splt)
3827 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3828 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3830 else
3832 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3833 got_offset = plt_index * GOT_ENTRY_SIZE;
3836 /* Fill in the entry in the procedure linkage table. */
3837 memcpy (plt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3838 PLT_ENTRY_SIZE);
3840 /* Insert the relocation positions of the plt section. The magic
3841 numbers at the end of the statements are the positions of the
3842 relocations in the plt section. */
3843 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3844 instruction uses 6 bytes, subtract this value. */
3845 bfd_put_32 (output_bfd,
3846 (gotplt->output_section->vma
3847 + gotplt->output_offset
3848 + got_offset
3849 - plt->output_section->vma
3850 - plt->output_offset
3851 - h->plt.offset
3852 - 6),
3853 plt->contents + h->plt.offset + 2);
3855 /* Don't fill PLT entry for static executables. */
3856 if (plt == htab->elf.splt)
3858 /* Put relocation index. */
3859 bfd_put_32 (output_bfd, plt_index,
3860 plt->contents + h->plt.offset + 7);
3861 /* Put offset for jmp .PLT0. */
3862 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3863 plt->contents + h->plt.offset + 12);
3866 /* Fill in the entry in the global offset table, initially this
3867 points to the pushq instruction in the PLT which is at offset 6. */
3868 bfd_put_64 (output_bfd, (plt->output_section->vma
3869 + plt->output_offset
3870 + h->plt.offset + 6),
3871 gotplt->contents + got_offset);
3873 /* Fill in the entry in the .rela.plt section. */
3874 rela.r_offset = (gotplt->output_section->vma
3875 + gotplt->output_offset
3876 + got_offset);
3877 if (h->dynindx == -1
3878 || ((info->executable
3879 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3880 && h->def_regular
3881 && h->type == STT_GNU_IFUNC))
3883 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3884 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
3885 rela.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
3886 rela.r_addend = (h->root.u.def.value
3887 + h->root.u.def.section->output_section->vma
3888 + h->root.u.def.section->output_offset);
3890 else
3892 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3893 rela.r_addend = 0;
3895 loc = relplt->contents + plt_index * sizeof (Elf64_External_Rela);
3896 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3898 if (!h->def_regular)
3900 /* Mark the symbol as undefined, rather than as defined in
3901 the .plt section. Leave the value if there were any
3902 relocations where pointer equality matters (this is a clue
3903 for the dynamic linker, to make function pointer
3904 comparisons work between an application and shared
3905 library), otherwise set it to zero. If a function is only
3906 called from a binary, there is no need to slow down
3907 shared libraries because of that. */
3908 sym->st_shndx = SHN_UNDEF;
3909 if (!h->pointer_equality_needed)
3910 sym->st_value = 0;
3914 if (h->got.offset != (bfd_vma) -1
3915 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3916 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3918 Elf_Internal_Rela rela;
3920 /* This symbol has an entry in the global offset table. Set it
3921 up. */
3922 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3923 abort ();
3925 rela.r_offset = (htab->elf.sgot->output_section->vma
3926 + htab->elf.sgot->output_offset
3927 + (h->got.offset &~ (bfd_vma) 1));
3929 /* If this is a static link, or it is a -Bsymbolic link and the
3930 symbol is defined locally or was forced to be local because
3931 of a version file, we just want to emit a RELATIVE reloc.
3932 The entry in the global offset table will already have been
3933 initialized in the relocate_section function. */
3934 if (h->def_regular
3935 && h->type == STT_GNU_IFUNC)
3937 if (info->shared)
3939 /* Generate R_X86_64_GLOB_DAT. */
3940 goto do_glob_dat;
3942 else
3944 asection *plt;
3946 if (!h->pointer_equality_needed)
3947 abort ();
3949 /* For non-shared object, we can't use .got.plt, which
3950 contains the real function addres if we need pointer
3951 equality. We load the GOT entry with the PLT entry. */
3952 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3953 bfd_put_64 (output_bfd, (plt->output_section->vma
3954 + plt->output_offset
3955 + h->plt.offset),
3956 htab->elf.sgot->contents + h->got.offset);
3957 return TRUE;
3960 else if (info->shared
3961 && SYMBOL_REFERENCES_LOCAL (info, h))
3963 if (!h->def_regular)
3964 return FALSE;
3965 BFD_ASSERT((h->got.offset & 1) != 0);
3966 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3967 rela.r_addend = (h->root.u.def.value
3968 + h->root.u.def.section->output_section->vma
3969 + h->root.u.def.section->output_offset);
3971 else
3973 BFD_ASSERT((h->got.offset & 1) == 0);
3974 do_glob_dat:
3975 bfd_put_64 (output_bfd, (bfd_vma) 0,
3976 htab->elf.sgot->contents + h->got.offset);
3977 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3978 rela.r_addend = 0;
3981 elf64_x86_64_append_rela (output_bfd, htab->elf.srelgot, &rela);
3984 if (h->needs_copy)
3986 Elf_Internal_Rela rela;
3988 /* This symbol needs a copy reloc. Set it up. */
3990 if (h->dynindx == -1
3991 || (h->root.type != bfd_link_hash_defined
3992 && h->root.type != bfd_link_hash_defweak)
3993 || htab->srelbss == NULL)
3994 abort ();
3996 rela.r_offset = (h->root.u.def.value
3997 + h->root.u.def.section->output_section->vma
3998 + h->root.u.def.section->output_offset);
3999 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
4000 rela.r_addend = 0;
4001 elf64_x86_64_append_rela (output_bfd, htab->srelbss, &rela);
4004 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
4005 be NULL for local symbols. */
4006 if (sym != NULL
4007 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4008 || h == htab->elf.hgot))
4009 sym->st_shndx = SHN_ABS;
4011 return TRUE;
4014 /* Finish up local dynamic symbol handling. We set the contents of
4015 various dynamic sections here. */
4017 static bfd_boolean
4018 elf64_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
4020 struct elf_link_hash_entry *h
4021 = (struct elf_link_hash_entry *) *slot;
4022 struct bfd_link_info *info
4023 = (struct bfd_link_info *) inf;
4025 return elf64_x86_64_finish_dynamic_symbol (info->output_bfd,
4026 info, h, NULL);
4029 /* Used to decide how to sort relocs in an optimal manner for the
4030 dynamic linker, before writing them out. */
4032 static enum elf_reloc_type_class
4033 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
4035 switch ((int) ELF64_R_TYPE (rela->r_info))
4037 case R_X86_64_RELATIVE:
4038 return reloc_class_relative;
4039 case R_X86_64_JUMP_SLOT:
4040 return reloc_class_plt;
4041 case R_X86_64_COPY:
4042 return reloc_class_copy;
4043 default:
4044 return reloc_class_normal;
4048 /* Finish up the dynamic sections. */
4050 static bfd_boolean
4051 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
4053 struct elf64_x86_64_link_hash_table *htab;
4054 bfd *dynobj;
4055 asection *sdyn;
4057 htab = elf64_x86_64_hash_table (info);
4058 if (htab == NULL)
4059 return FALSE;
4061 dynobj = htab->elf.dynobj;
4062 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4064 if (htab->elf.dynamic_sections_created)
4066 Elf64_External_Dyn *dyncon, *dynconend;
4068 if (sdyn == NULL || htab->elf.sgot == NULL)
4069 abort ();
4071 dyncon = (Elf64_External_Dyn *) sdyn->contents;
4072 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
4073 for (; dyncon < dynconend; dyncon++)
4075 Elf_Internal_Dyn dyn;
4076 asection *s;
4078 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4080 switch (dyn.d_tag)
4082 default:
4083 continue;
4085 case DT_PLTGOT:
4086 s = htab->elf.sgotplt;
4087 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4088 break;
4090 case DT_JMPREL:
4091 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
4092 break;
4094 case DT_PLTRELSZ:
4095 s = htab->elf.srelplt->output_section;
4096 dyn.d_un.d_val = s->size;
4097 break;
4099 case DT_RELASZ:
4100 /* The procedure linkage table relocs (DT_JMPREL) should
4101 not be included in the overall relocs (DT_RELA).
4102 Therefore, we override the DT_RELASZ entry here to
4103 make it not include the JMPREL relocs. Since the
4104 linker script arranges for .rela.plt to follow all
4105 other relocation sections, we don't have to worry
4106 about changing the DT_RELA entry. */
4107 if (htab->elf.srelplt != NULL)
4109 s = htab->elf.srelplt->output_section;
4110 dyn.d_un.d_val -= s->size;
4112 break;
4114 case DT_TLSDESC_PLT:
4115 s = htab->elf.splt;
4116 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4117 + htab->tlsdesc_plt;
4118 break;
4120 case DT_TLSDESC_GOT:
4121 s = htab->elf.sgot;
4122 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4123 + htab->tlsdesc_got;
4124 break;
4127 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4130 /* Fill in the special first entry in the procedure linkage table. */
4131 if (htab->elf.splt && htab->elf.splt->size > 0)
4133 /* Fill in the first entry in the procedure linkage table. */
4134 memcpy (htab->elf.splt->contents, elf64_x86_64_plt0_entry,
4135 PLT_ENTRY_SIZE);
4136 /* Add offset for pushq GOT+8(%rip), since the instruction
4137 uses 6 bytes subtract this value. */
4138 bfd_put_32 (output_bfd,
4139 (htab->elf.sgotplt->output_section->vma
4140 + htab->elf.sgotplt->output_offset
4142 - htab->elf.splt->output_section->vma
4143 - htab->elf.splt->output_offset
4144 - 6),
4145 htab->elf.splt->contents + 2);
4146 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4147 the end of the instruction. */
4148 bfd_put_32 (output_bfd,
4149 (htab->elf.sgotplt->output_section->vma
4150 + htab->elf.sgotplt->output_offset
4151 + 16
4152 - htab->elf.splt->output_section->vma
4153 - htab->elf.splt->output_offset
4154 - 12),
4155 htab->elf.splt->contents + 8);
4157 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize =
4158 PLT_ENTRY_SIZE;
4160 if (htab->tlsdesc_plt)
4162 bfd_put_64 (output_bfd, (bfd_vma) 0,
4163 htab->elf.sgot->contents + htab->tlsdesc_got);
4165 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
4166 elf64_x86_64_plt0_entry,
4167 PLT_ENTRY_SIZE);
4169 /* Add offset for pushq GOT+8(%rip), since the
4170 instruction uses 6 bytes subtract this value. */
4171 bfd_put_32 (output_bfd,
4172 (htab->elf.sgotplt->output_section->vma
4173 + htab->elf.sgotplt->output_offset
4175 - htab->elf.splt->output_section->vma
4176 - htab->elf.splt->output_offset
4177 - htab->tlsdesc_plt
4178 - 6),
4179 htab->elf.splt->contents + htab->tlsdesc_plt + 2);
4180 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4181 htab->tlsdesc_got. The 12 is the offset to the end of
4182 the instruction. */
4183 bfd_put_32 (output_bfd,
4184 (htab->elf.sgot->output_section->vma
4185 + htab->elf.sgot->output_offset
4186 + htab->tlsdesc_got
4187 - htab->elf.splt->output_section->vma
4188 - htab->elf.splt->output_offset
4189 - htab->tlsdesc_plt
4190 - 12),
4191 htab->elf.splt->contents + htab->tlsdesc_plt + 8);
4196 if (htab->elf.sgotplt)
4198 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4200 (*_bfd_error_handler)
4201 (_("discarded output section: `%A'"), htab->elf.sgotplt);
4202 return FALSE;
4205 /* Fill in the first three entries in the global offset table. */
4206 if (htab->elf.sgotplt->size > 0)
4208 /* Set the first entry in the global offset table to the address of
4209 the dynamic section. */
4210 if (sdyn == NULL)
4211 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4212 else
4213 bfd_put_64 (output_bfd,
4214 sdyn->output_section->vma + sdyn->output_offset,
4215 htab->elf.sgotplt->contents);
4216 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4217 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4218 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4221 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
4222 GOT_ENTRY_SIZE;
4225 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4226 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
4227 = GOT_ENTRY_SIZE;
4229 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4230 htab_traverse (htab->loc_hash_table,
4231 elf64_x86_64_finish_local_dynamic_symbol,
4232 info);
4234 return TRUE;
4237 /* Return address for Ith PLT stub in section PLT, for relocation REL
4238 or (bfd_vma) -1 if it should not be included. */
4240 static bfd_vma
4241 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4242 const arelent *rel ATTRIBUTE_UNUSED)
4244 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4247 /* Handle an x86-64 specific section when reading an object file. This
4248 is called when elfcode.h finds a section with an unknown type. */
4250 static bfd_boolean
4251 elf64_x86_64_section_from_shdr (bfd *abfd,
4252 Elf_Internal_Shdr *hdr,
4253 const char *name,
4254 int shindex)
4256 if (hdr->sh_type != SHT_X86_64_UNWIND)
4257 return FALSE;
4259 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4260 return FALSE;
4262 return TRUE;
4265 /* Hook called by the linker routine which adds symbols from an object
4266 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4267 of .bss. */
4269 static bfd_boolean
4270 elf64_x86_64_add_symbol_hook (bfd *abfd,
4271 struct bfd_link_info *info,
4272 Elf_Internal_Sym *sym,
4273 const char **namep ATTRIBUTE_UNUSED,
4274 flagword *flagsp ATTRIBUTE_UNUSED,
4275 asection **secp,
4276 bfd_vma *valp)
4278 asection *lcomm;
4280 switch (sym->st_shndx)
4282 case SHN_X86_64_LCOMMON:
4283 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4284 if (lcomm == NULL)
4286 lcomm = bfd_make_section_with_flags (abfd,
4287 "LARGE_COMMON",
4288 (SEC_ALLOC
4289 | SEC_IS_COMMON
4290 | SEC_LINKER_CREATED));
4291 if (lcomm == NULL)
4292 return FALSE;
4293 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4295 *secp = lcomm;
4296 *valp = sym->st_size;
4297 return TRUE;
4300 if ((abfd->flags & DYNAMIC) == 0
4301 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4302 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4304 return TRUE;
4308 /* Given a BFD section, try to locate the corresponding ELF section
4309 index. */
4311 static bfd_boolean
4312 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4313 asection *sec, int *index_return)
4315 if (sec == &_bfd_elf_large_com_section)
4317 *index_return = SHN_X86_64_LCOMMON;
4318 return TRUE;
4320 return FALSE;
4323 /* Process a symbol. */
4325 static void
4326 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4327 asymbol *asym)
4329 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4331 switch (elfsym->internal_elf_sym.st_shndx)
4333 case SHN_X86_64_LCOMMON:
4334 asym->section = &_bfd_elf_large_com_section;
4335 asym->value = elfsym->internal_elf_sym.st_size;
4336 /* Common symbol doesn't set BSF_GLOBAL. */
4337 asym->flags &= ~BSF_GLOBAL;
4338 break;
4342 static bfd_boolean
4343 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
4345 return (sym->st_shndx == SHN_COMMON
4346 || sym->st_shndx == SHN_X86_64_LCOMMON);
4349 static unsigned int
4350 elf64_x86_64_common_section_index (asection *sec)
4352 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4353 return SHN_COMMON;
4354 else
4355 return SHN_X86_64_LCOMMON;
4358 static asection *
4359 elf64_x86_64_common_section (asection *sec)
4361 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4362 return bfd_com_section_ptr;
4363 else
4364 return &_bfd_elf_large_com_section;
4367 static bfd_boolean
4368 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4369 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4370 struct elf_link_hash_entry *h,
4371 Elf_Internal_Sym *sym,
4372 asection **psec,
4373 bfd_vma *pvalue ATTRIBUTE_UNUSED,
4374 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4375 bfd_boolean *skip ATTRIBUTE_UNUSED,
4376 bfd_boolean *override ATTRIBUTE_UNUSED,
4377 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4378 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4379 bfd_boolean *newdef ATTRIBUTE_UNUSED,
4380 bfd_boolean *newdyn,
4381 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4382 bfd_boolean *newweak ATTRIBUTE_UNUSED,
4383 bfd *abfd ATTRIBUTE_UNUSED,
4384 asection **sec,
4385 bfd_boolean *olddef ATTRIBUTE_UNUSED,
4386 bfd_boolean *olddyn,
4387 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4388 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4389 bfd *oldbfd,
4390 asection **oldsec)
4392 /* A normal common symbol and a large common symbol result in a
4393 normal common symbol. We turn the large common symbol into a
4394 normal one. */
4395 if (!*olddyn
4396 && h->root.type == bfd_link_hash_common
4397 && !*newdyn
4398 && bfd_is_com_section (*sec)
4399 && *oldsec != *sec)
4401 if (sym->st_shndx == SHN_COMMON
4402 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4404 h->root.u.c.p->section
4405 = bfd_make_section_old_way (oldbfd, "COMMON");
4406 h->root.u.c.p->section->flags = SEC_ALLOC;
4408 else if (sym->st_shndx == SHN_X86_64_LCOMMON
4409 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4410 *psec = *sec = bfd_com_section_ptr;
4413 return TRUE;
4416 static int
4417 elf64_x86_64_additional_program_headers (bfd *abfd,
4418 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4420 asection *s;
4421 int count = 0;
4423 /* Check to see if we need a large readonly segment. */
4424 s = bfd_get_section_by_name (abfd, ".lrodata");
4425 if (s && (s->flags & SEC_LOAD))
4426 count++;
4428 /* Check to see if we need a large data segment. Since .lbss sections
4429 is placed right after the .bss section, there should be no need for
4430 a large data segment just because of .lbss. */
4431 s = bfd_get_section_by_name (abfd, ".ldata");
4432 if (s && (s->flags & SEC_LOAD))
4433 count++;
4435 return count;
4438 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4440 static bfd_boolean
4441 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4443 if (h->plt.offset != (bfd_vma) -1
4444 && !h->def_regular
4445 && !h->pointer_equality_needed)
4446 return FALSE;
4448 return _bfd_elf_hash_symbol (h);
4451 static const struct bfd_elf_special_section
4452 elf64_x86_64_special_sections[]=
4454 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4455 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4456 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4457 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4458 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4459 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4460 { NULL, 0, 0, 0, 0 }
4463 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
4464 #define TARGET_LITTLE_NAME "elf64-x86-64"
4465 #define ELF_ARCH bfd_arch_i386
4466 #define ELF_TARGET_ID X86_64_ELF_DATA
4467 #define ELF_MACHINE_CODE EM_X86_64
4468 #define ELF_MAXPAGESIZE 0x200000
4469 #define ELF_MINPAGESIZE 0x1000
4470 #define ELF_COMMONPAGESIZE 0x1000
4472 #define elf_backend_can_gc_sections 1
4473 #define elf_backend_can_refcount 1
4474 #define elf_backend_want_got_plt 1
4475 #define elf_backend_plt_readonly 1
4476 #define elf_backend_want_plt_sym 0
4477 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
4478 #define elf_backend_rela_normal 1
4480 #define elf_info_to_howto elf64_x86_64_info_to_howto
4482 #define bfd_elf64_bfd_link_hash_table_create \
4483 elf64_x86_64_link_hash_table_create
4484 #define bfd_elf64_bfd_link_hash_table_free \
4485 elf64_x86_64_link_hash_table_free
4486 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
4487 #define bfd_elf64_bfd_reloc_name_lookup \
4488 elf64_x86_64_reloc_name_lookup
4490 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
4491 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4492 #define elf_backend_check_relocs elf64_x86_64_check_relocs
4493 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
4494 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
4495 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
4496 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
4497 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
4498 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
4499 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
4500 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
4501 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
4502 #define elf_backend_relocate_section elf64_x86_64_relocate_section
4503 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
4504 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
4505 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4506 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
4507 #define elf_backend_object_p elf64_x86_64_elf_object_p
4508 #define bfd_elf64_mkobject elf64_x86_64_mkobject
4510 #define elf_backend_section_from_shdr \
4511 elf64_x86_64_section_from_shdr
4513 #define elf_backend_section_from_bfd_section \
4514 elf64_x86_64_elf_section_from_bfd_section
4515 #define elf_backend_add_symbol_hook \
4516 elf64_x86_64_add_symbol_hook
4517 #define elf_backend_symbol_processing \
4518 elf64_x86_64_symbol_processing
4519 #define elf_backend_common_section_index \
4520 elf64_x86_64_common_section_index
4521 #define elf_backend_common_section \
4522 elf64_x86_64_common_section
4523 #define elf_backend_common_definition \
4524 elf64_x86_64_common_definition
4525 #define elf_backend_merge_symbol \
4526 elf64_x86_64_merge_symbol
4527 #define elf_backend_special_sections \
4528 elf64_x86_64_special_sections
4529 #define elf_backend_additional_program_headers \
4530 elf64_x86_64_additional_program_headers
4531 #define elf_backend_hash_symbol \
4532 elf64_x86_64_hash_symbol
4534 #undef elf_backend_post_process_headers
4535 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4537 #include "elf64-target.h"
4539 /* FreeBSD support. */
4541 #undef TARGET_LITTLE_SYM
4542 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
4543 #undef TARGET_LITTLE_NAME
4544 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
4546 #undef ELF_OSABI
4547 #define ELF_OSABI ELFOSABI_FREEBSD
4549 #undef elf64_bed
4550 #define elf64_bed elf64_x86_64_fbsd_bed
4552 #include "elf64-target.h"
4554 /* Solaris 2 support. */
4556 #undef TARGET_LITTLE_SYM
4557 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_sol2_vec
4558 #undef TARGET_LITTLE_NAME
4559 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
4561 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4562 objects won't be recognized. */
4563 #undef ELF_OSABI
4565 #undef elf64_bed
4566 #define elf64_bed elf64_x86_64_sol2_bed
4568 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4570 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4571 File, p.63. */
4572 #undef elf_backend_want_plt_sym
4573 #define elf_backend_want_plt_sym 1
4575 #include "elf64-target.h"
4577 /* Intel L1OM support. */
4579 static bfd_boolean
4580 elf64_l1om_elf_object_p (bfd *abfd)
4582 /* Set the right machine number for an L1OM elf64 file. */
4583 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
4584 return TRUE;
4587 #undef TARGET_LITTLE_SYM
4588 #define TARGET_LITTLE_SYM bfd_elf64_l1om_vec
4589 #undef TARGET_LITTLE_NAME
4590 #define TARGET_LITTLE_NAME "elf64-l1om"
4591 #undef ELF_ARCH
4592 #define ELF_ARCH bfd_arch_l1om
4594 #undef ELF_MACHINE_CODE
4595 #define ELF_MACHINE_CODE EM_L1OM
4597 #undef ELF_OSABI
4599 #undef elf64_bed
4600 #define elf64_bed elf64_l1om_bed
4602 #undef elf_backend_object_p
4603 #define elf_backend_object_p elf64_l1om_elf_object_p
4605 #undef elf_backend_post_process_headers
4607 #include "elf64-target.h"
4609 /* FreeBSD L1OM support. */
4611 #undef TARGET_LITTLE_SYM
4612 #define TARGET_LITTLE_SYM bfd_elf64_l1om_freebsd_vec
4613 #undef TARGET_LITTLE_NAME
4614 #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
4616 #undef ELF_OSABI
4617 #define ELF_OSABI ELFOSABI_FREEBSD
4619 #undef elf64_bed
4620 #define elf64_bed elf64_l1om_fbsd_bed
4622 #undef elf_backend_post_process_headers
4623 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4625 #include "elf64-target.h"