PR binutils/11889
[binutils.git] / bfd / elf64-x86-64.c
blobb0bc5c012513d694789a11ccdbbab4d8a01b1e70
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_pid
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_program
336 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
337 elf_tdata (abfd)->core_command
338 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
341 /* Note that for some reason, a spurious space is tacked
342 onto the end of the args in some (at least one anyway)
343 implementations, so strip it off if it exists. */
346 char *command = elf_tdata (abfd)->core_command;
347 int n = strlen (command);
349 if (0 < n && command[n - 1] == ' ')
350 command[n - 1] = '\0';
353 return TRUE;
356 /* Functions for the x86-64 ELF linker. */
358 /* The name of the dynamic interpreter. This is put in the .interp
359 section. */
361 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
363 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
364 copying dynamic variables from a shared lib into an app's dynbss
365 section, and instead use a dynamic relocation to point into the
366 shared lib. */
367 #define ELIMINATE_COPY_RELOCS 1
369 /* The size in bytes of an entry in the global offset table. */
371 #define GOT_ENTRY_SIZE 8
373 /* The size in bytes of an entry in the procedure linkage table. */
375 #define PLT_ENTRY_SIZE 16
377 /* The first entry in a procedure linkage table looks like this. See the
378 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
380 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
382 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
383 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
384 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
387 /* Subsequent entries in a procedure linkage table look like this. */
389 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
391 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
392 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
393 0x68, /* pushq immediate */
394 0, 0, 0, 0, /* replaced with index into relocation table. */
395 0xe9, /* jmp relative */
396 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
399 /* x86-64 ELF linker hash entry. */
401 struct elf64_x86_64_link_hash_entry
403 struct elf_link_hash_entry elf;
405 /* Track dynamic relocs copied for this symbol. */
406 struct elf_dyn_relocs *dyn_relocs;
408 #define GOT_UNKNOWN 0
409 #define GOT_NORMAL 1
410 #define GOT_TLS_GD 2
411 #define GOT_TLS_IE 3
412 #define GOT_TLS_GDESC 4
413 #define GOT_TLS_GD_BOTH_P(type) \
414 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
415 #define GOT_TLS_GD_P(type) \
416 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
417 #define GOT_TLS_GDESC_P(type) \
418 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
419 #define GOT_TLS_GD_ANY_P(type) \
420 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
421 unsigned char tls_type;
423 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
424 starting at the end of the jump table. */
425 bfd_vma tlsdesc_got;
428 #define elf64_x86_64_hash_entry(ent) \
429 ((struct elf64_x86_64_link_hash_entry *)(ent))
431 struct elf64_x86_64_obj_tdata
433 struct elf_obj_tdata root;
435 /* tls_type for each local got entry. */
436 char *local_got_tls_type;
438 /* GOTPLT entries for TLS descriptors. */
439 bfd_vma *local_tlsdesc_gotent;
442 #define elf64_x86_64_tdata(abfd) \
443 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
445 #define elf64_x86_64_local_got_tls_type(abfd) \
446 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
448 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
449 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
451 #define is_x86_64_elf(bfd) \
452 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
453 && elf_tdata (bfd) != NULL \
454 && elf_object_id (bfd) == X86_64_ELF_DATA)
456 static bfd_boolean
457 elf64_x86_64_mkobject (bfd *abfd)
459 return bfd_elf_allocate_object (abfd, sizeof (struct elf64_x86_64_obj_tdata),
460 X86_64_ELF_DATA);
463 /* x86-64 ELF linker hash table. */
465 struct elf64_x86_64_link_hash_table
467 struct elf_link_hash_table elf;
469 /* Short-cuts to get to dynamic linker sections. */
470 asection *sdynbss;
471 asection *srelbss;
473 union
475 bfd_signed_vma refcount;
476 bfd_vma offset;
477 } tls_ld_got;
479 /* The amount of space used by the jump slots in the GOT. */
480 bfd_vma sgotplt_jump_table_size;
482 /* Small local sym cache. */
483 struct sym_cache sym_cache;
485 /* _TLS_MODULE_BASE_ symbol. */
486 struct bfd_link_hash_entry *tls_module_base;
488 /* Used by local STT_GNU_IFUNC symbols. */
489 htab_t loc_hash_table;
490 void * loc_hash_memory;
492 /* The offset into splt of the PLT entry for the TLS descriptor
493 resolver. Special values are 0, if not necessary (or not found
494 to be necessary yet), and -1 if needed but not determined
495 yet. */
496 bfd_vma tlsdesc_plt;
497 /* The offset into sgot of the GOT entry used by the PLT entry
498 above. */
499 bfd_vma tlsdesc_got;
502 /* Get the x86-64 ELF linker hash table from a link_info structure. */
504 #define elf64_x86_64_hash_table(p) \
505 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
506 == X86_64_ELF_DATA ? ((struct elf64_x86_64_link_hash_table *) ((p)->hash)) : NULL)
508 #define elf64_x86_64_compute_jump_table_size(htab) \
509 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
511 /* Create an entry in an x86-64 ELF linker hash table. */
513 static struct bfd_hash_entry *
514 elf64_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
515 struct bfd_hash_table *table,
516 const char *string)
518 /* Allocate the structure if it has not already been allocated by a
519 subclass. */
520 if (entry == NULL)
522 entry = (struct bfd_hash_entry *)
523 bfd_hash_allocate (table,
524 sizeof (struct elf64_x86_64_link_hash_entry));
525 if (entry == NULL)
526 return entry;
529 /* Call the allocation method of the superclass. */
530 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
531 if (entry != NULL)
533 struct elf64_x86_64_link_hash_entry *eh;
535 eh = (struct elf64_x86_64_link_hash_entry *) entry;
536 eh->dyn_relocs = NULL;
537 eh->tls_type = GOT_UNKNOWN;
538 eh->tlsdesc_got = (bfd_vma) -1;
541 return entry;
544 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
545 for local symbol so that we can handle local STT_GNU_IFUNC symbols
546 as global symbol. We reuse indx and dynstr_index for local symbol
547 hash since they aren't used by global symbols in this backend. */
549 static hashval_t
550 elf64_x86_64_local_htab_hash (const void *ptr)
552 struct elf_link_hash_entry *h
553 = (struct elf_link_hash_entry *) ptr;
554 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
557 /* Compare local hash entries. */
559 static int
560 elf64_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
562 struct elf_link_hash_entry *h1
563 = (struct elf_link_hash_entry *) ptr1;
564 struct elf_link_hash_entry *h2
565 = (struct elf_link_hash_entry *) ptr2;
567 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
570 /* Find and/or create a hash entry for local symbol. */
572 static struct elf_link_hash_entry *
573 elf64_x86_64_get_local_sym_hash (struct elf64_x86_64_link_hash_table *htab,
574 bfd *abfd, const Elf_Internal_Rela *rel,
575 bfd_boolean create)
577 struct elf64_x86_64_link_hash_entry e, *ret;
578 asection *sec = abfd->sections;
579 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
580 ELF64_R_SYM (rel->r_info));
581 void **slot;
583 e.elf.indx = sec->id;
584 e.elf.dynstr_index = ELF64_R_SYM (rel->r_info);
585 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
586 create ? INSERT : NO_INSERT);
588 if (!slot)
589 return NULL;
591 if (*slot)
593 ret = (struct elf64_x86_64_link_hash_entry *) *slot;
594 return &ret->elf;
597 ret = (struct elf64_x86_64_link_hash_entry *)
598 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
599 sizeof (struct elf64_x86_64_link_hash_entry));
600 if (ret)
602 memset (ret, 0, sizeof (*ret));
603 ret->elf.indx = sec->id;
604 ret->elf.dynstr_index = ELF64_R_SYM (rel->r_info);
605 ret->elf.dynindx = -1;
606 *slot = ret;
608 return &ret->elf;
611 /* Create an X86-64 ELF linker hash table. */
613 static struct bfd_link_hash_table *
614 elf64_x86_64_link_hash_table_create (bfd *abfd)
616 struct elf64_x86_64_link_hash_table *ret;
617 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
619 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
620 if (ret == NULL)
621 return NULL;
623 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
624 elf64_x86_64_link_hash_newfunc,
625 sizeof (struct elf64_x86_64_link_hash_entry),
626 X86_64_ELF_DATA))
628 free (ret);
629 return NULL;
632 ret->sdynbss = NULL;
633 ret->srelbss = NULL;
634 ret->sym_cache.abfd = NULL;
635 ret->tlsdesc_plt = 0;
636 ret->tlsdesc_got = 0;
637 ret->tls_ld_got.refcount = 0;
638 ret->sgotplt_jump_table_size = 0;
639 ret->tls_module_base = NULL;
641 ret->loc_hash_table = htab_try_create (1024,
642 elf64_x86_64_local_htab_hash,
643 elf64_x86_64_local_htab_eq,
644 NULL);
645 ret->loc_hash_memory = objalloc_create ();
646 if (!ret->loc_hash_table || !ret->loc_hash_memory)
648 free (ret);
649 return NULL;
652 return &ret->elf.root;
655 /* Destroy an X86-64 ELF linker hash table. */
657 static void
658 elf64_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
660 struct elf64_x86_64_link_hash_table *htab
661 = (struct elf64_x86_64_link_hash_table *) hash;
663 if (htab->loc_hash_table)
664 htab_delete (htab->loc_hash_table);
665 if (htab->loc_hash_memory)
666 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
667 _bfd_generic_link_hash_table_free (hash);
670 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
671 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
672 hash table. */
674 static bfd_boolean
675 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
677 struct elf64_x86_64_link_hash_table *htab;
679 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
680 return FALSE;
682 htab = elf64_x86_64_hash_table (info);
683 if (htab == NULL)
684 return FALSE;
686 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
687 if (!info->shared)
688 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
690 if (!htab->sdynbss
691 || (!info->shared && !htab->srelbss))
692 abort ();
694 return TRUE;
697 /* Copy the extra info we tack onto an elf_link_hash_entry. */
699 static void
700 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
701 struct elf_link_hash_entry *dir,
702 struct elf_link_hash_entry *ind)
704 struct elf64_x86_64_link_hash_entry *edir, *eind;
706 edir = (struct elf64_x86_64_link_hash_entry *) dir;
707 eind = (struct elf64_x86_64_link_hash_entry *) ind;
709 if (eind->dyn_relocs != NULL)
711 if (edir->dyn_relocs != NULL)
713 struct elf_dyn_relocs **pp;
714 struct elf_dyn_relocs *p;
716 /* Add reloc counts against the indirect sym to the direct sym
717 list. Merge any entries against the same section. */
718 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
720 struct elf_dyn_relocs *q;
722 for (q = edir->dyn_relocs; q != NULL; q = q->next)
723 if (q->sec == p->sec)
725 q->pc_count += p->pc_count;
726 q->count += p->count;
727 *pp = p->next;
728 break;
730 if (q == NULL)
731 pp = &p->next;
733 *pp = edir->dyn_relocs;
736 edir->dyn_relocs = eind->dyn_relocs;
737 eind->dyn_relocs = NULL;
740 if (ind->root.type == bfd_link_hash_indirect
741 && dir->got.refcount <= 0)
743 edir->tls_type = eind->tls_type;
744 eind->tls_type = GOT_UNKNOWN;
747 if (ELIMINATE_COPY_RELOCS
748 && ind->root.type != bfd_link_hash_indirect
749 && dir->dynamic_adjusted)
751 /* If called to transfer flags for a weakdef during processing
752 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
753 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
754 dir->ref_dynamic |= ind->ref_dynamic;
755 dir->ref_regular |= ind->ref_regular;
756 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
757 dir->needs_plt |= ind->needs_plt;
758 dir->pointer_equality_needed |= ind->pointer_equality_needed;
760 else
761 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
764 static bfd_boolean
765 elf64_x86_64_elf_object_p (bfd *abfd)
767 /* Set the right machine number for an x86-64 elf64 file. */
768 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
769 return TRUE;
772 typedef union
774 unsigned char c[2];
775 uint16_t i;
777 x86_64_opcode16;
779 typedef union
781 unsigned char c[4];
782 uint32_t i;
784 x86_64_opcode32;
786 /* Return TRUE if the TLS access code sequence support transition
787 from R_TYPE. */
789 static bfd_boolean
790 elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec,
791 bfd_byte *contents,
792 Elf_Internal_Shdr *symtab_hdr,
793 struct elf_link_hash_entry **sym_hashes,
794 unsigned int r_type,
795 const Elf_Internal_Rela *rel,
796 const Elf_Internal_Rela *relend)
798 unsigned int val;
799 unsigned long r_symndx;
800 struct elf_link_hash_entry *h;
801 bfd_vma offset;
803 /* Get the section contents. */
804 if (contents == NULL)
806 if (elf_section_data (sec)->this_hdr.contents != NULL)
807 contents = elf_section_data (sec)->this_hdr.contents;
808 else
810 /* FIXME: How to better handle error condition? */
811 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
812 return FALSE;
814 /* Cache the section contents for elf_link_input_bfd. */
815 elf_section_data (sec)->this_hdr.contents = contents;
819 offset = rel->r_offset;
820 switch (r_type)
822 case R_X86_64_TLSGD:
823 case R_X86_64_TLSLD:
824 if ((rel + 1) >= relend)
825 return FALSE;
827 if (r_type == R_X86_64_TLSGD)
829 /* Check transition from GD access model. Only
830 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
831 .word 0x6666; rex64; call __tls_get_addr
832 can transit to different access model. */
834 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } },
835 call = { { 0x66, 0x66, 0x48, 0xe8 } };
836 if (offset < 4
837 || (offset + 12) > sec->size
838 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i
839 || bfd_get_32 (abfd, contents + offset + 4) != call.i)
840 return FALSE;
842 else
844 /* Check transition from LD access model. Only
845 leaq foo@tlsld(%rip), %rdi;
846 call __tls_get_addr
847 can transit to different access model. */
849 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
850 x86_64_opcode32 op;
852 if (offset < 3 || (offset + 9) > sec->size)
853 return FALSE;
855 op.i = bfd_get_32 (abfd, contents + offset - 3);
856 op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
857 if (op.i != ld.i)
858 return FALSE;
861 r_symndx = ELF64_R_SYM (rel[1].r_info);
862 if (r_symndx < symtab_hdr->sh_info)
863 return FALSE;
865 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
866 /* Use strncmp to check __tls_get_addr since __tls_get_addr
867 may be versioned. */
868 return (h != NULL
869 && h->root.root.string != NULL
870 && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
871 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
872 && (strncmp (h->root.root.string,
873 "__tls_get_addr", 14) == 0));
875 case R_X86_64_GOTTPOFF:
876 /* Check transition from IE access model:
877 movq foo@gottpoff(%rip), %reg
878 addq foo@gottpoff(%rip), %reg
881 if (offset < 3 || (offset + 4) > sec->size)
882 return FALSE;
884 val = bfd_get_8 (abfd, contents + offset - 3);
885 if (val != 0x48 && val != 0x4c)
886 return FALSE;
888 val = bfd_get_8 (abfd, contents + offset - 2);
889 if (val != 0x8b && val != 0x03)
890 return FALSE;
892 val = bfd_get_8 (abfd, contents + offset - 1);
893 return (val & 0xc7) == 5;
895 case R_X86_64_GOTPC32_TLSDESC:
896 /* Check transition from GDesc access model:
897 leaq x@tlsdesc(%rip), %rax
899 Make sure it's a leaq adding rip to a 32-bit offset
900 into any register, although it's probably almost always
901 going to be rax. */
903 if (offset < 3 || (offset + 4) > sec->size)
904 return FALSE;
906 val = bfd_get_8 (abfd, contents + offset - 3);
907 if ((val & 0xfb) != 0x48)
908 return FALSE;
910 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
911 return FALSE;
913 val = bfd_get_8 (abfd, contents + offset - 1);
914 return (val & 0xc7) == 0x05;
916 case R_X86_64_TLSDESC_CALL:
917 /* Check transition from GDesc access model:
918 call *x@tlsdesc(%rax)
920 if (offset + 2 <= sec->size)
922 /* Make sure that it's a call *x@tlsdesc(%rax). */
923 static x86_64_opcode16 call = { { 0xff, 0x10 } };
924 return bfd_get_16 (abfd, contents + offset) == call.i;
927 return FALSE;
929 default:
930 abort ();
934 /* Return TRUE if the TLS access transition is OK or no transition
935 will be performed. Update R_TYPE if there is a transition. */
937 static bfd_boolean
938 elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
939 asection *sec, bfd_byte *contents,
940 Elf_Internal_Shdr *symtab_hdr,
941 struct elf_link_hash_entry **sym_hashes,
942 unsigned int *r_type, int tls_type,
943 const Elf_Internal_Rela *rel,
944 const Elf_Internal_Rela *relend,
945 struct elf_link_hash_entry *h,
946 unsigned long r_symndx)
948 unsigned int from_type = *r_type;
949 unsigned int to_type = from_type;
950 bfd_boolean check = TRUE;
952 /* Skip TLS transition for functions. */
953 if (h != NULL
954 && (h->type == STT_FUNC
955 || h->type == STT_GNU_IFUNC))
956 return TRUE;
958 switch (from_type)
960 case R_X86_64_TLSGD:
961 case R_X86_64_GOTPC32_TLSDESC:
962 case R_X86_64_TLSDESC_CALL:
963 case R_X86_64_GOTTPOFF:
964 if (info->executable)
966 if (h == NULL)
967 to_type = R_X86_64_TPOFF32;
968 else
969 to_type = R_X86_64_GOTTPOFF;
972 /* When we are called from elf64_x86_64_relocate_section,
973 CONTENTS isn't NULL and there may be additional transitions
974 based on TLS_TYPE. */
975 if (contents != NULL)
977 unsigned int new_to_type = to_type;
979 if (info->executable
980 && h != NULL
981 && h->dynindx == -1
982 && tls_type == GOT_TLS_IE)
983 new_to_type = R_X86_64_TPOFF32;
985 if (to_type == R_X86_64_TLSGD
986 || to_type == R_X86_64_GOTPC32_TLSDESC
987 || to_type == R_X86_64_TLSDESC_CALL)
989 if (tls_type == GOT_TLS_IE)
990 new_to_type = R_X86_64_GOTTPOFF;
993 /* We checked the transition before when we were called from
994 elf64_x86_64_check_relocs. We only want to check the new
995 transition which hasn't been checked before. */
996 check = new_to_type != to_type && from_type == to_type;
997 to_type = new_to_type;
1000 break;
1002 case R_X86_64_TLSLD:
1003 if (info->executable)
1004 to_type = R_X86_64_TPOFF32;
1005 break;
1007 default:
1008 return TRUE;
1011 /* Return TRUE if there is no transition. */
1012 if (from_type == to_type)
1013 return TRUE;
1015 /* Check if the transition can be performed. */
1016 if (check
1017 && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
1018 symtab_hdr, sym_hashes,
1019 from_type, rel, relend))
1021 reloc_howto_type *from, *to;
1022 const char *name;
1024 from = elf64_x86_64_rtype_to_howto (abfd, from_type);
1025 to = elf64_x86_64_rtype_to_howto (abfd, to_type);
1027 if (h)
1028 name = h->root.root.string;
1029 else
1031 struct elf64_x86_64_link_hash_table *htab;
1033 htab = elf64_x86_64_hash_table (info);
1034 if (htab == NULL)
1035 name = "*unknown*";
1036 else
1038 Elf_Internal_Sym *isym;
1040 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1041 abfd, r_symndx);
1042 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1046 (*_bfd_error_handler)
1047 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1048 "in section `%A' failed"),
1049 abfd, sec, from->name, to->name, name,
1050 (unsigned long) rel->r_offset);
1051 bfd_set_error (bfd_error_bad_value);
1052 return FALSE;
1055 *r_type = to_type;
1056 return TRUE;
1059 /* Look through the relocs for a section during the first phase, and
1060 calculate needed space in the global offset table, procedure
1061 linkage table, and dynamic reloc sections. */
1063 static bfd_boolean
1064 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1065 asection *sec,
1066 const Elf_Internal_Rela *relocs)
1068 struct elf64_x86_64_link_hash_table *htab;
1069 Elf_Internal_Shdr *symtab_hdr;
1070 struct elf_link_hash_entry **sym_hashes;
1071 const Elf_Internal_Rela *rel;
1072 const Elf_Internal_Rela *rel_end;
1073 asection *sreloc;
1075 if (info->relocatable)
1076 return TRUE;
1078 BFD_ASSERT (is_x86_64_elf (abfd));
1080 htab = elf64_x86_64_hash_table (info);
1081 if (htab == NULL)
1082 return FALSE;
1084 symtab_hdr = &elf_symtab_hdr (abfd);
1085 sym_hashes = elf_sym_hashes (abfd);
1087 sreloc = NULL;
1089 rel_end = relocs + sec->reloc_count;
1090 for (rel = relocs; rel < rel_end; rel++)
1092 unsigned int r_type;
1093 unsigned long r_symndx;
1094 struct elf_link_hash_entry *h;
1095 Elf_Internal_Sym *isym;
1096 const char *name;
1098 r_symndx = ELF64_R_SYM (rel->r_info);
1099 r_type = ELF64_R_TYPE (rel->r_info);
1101 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1103 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1104 abfd, r_symndx);
1105 return FALSE;
1108 if (r_symndx < symtab_hdr->sh_info)
1110 /* A local symbol. */
1111 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1112 abfd, r_symndx);
1113 if (isym == NULL)
1114 return FALSE;
1116 /* Check relocation against local STT_GNU_IFUNC symbol. */
1117 if (ELF64_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1119 h = elf64_x86_64_get_local_sym_hash (htab, abfd, rel,
1120 TRUE);
1121 if (h == NULL)
1122 return FALSE;
1124 /* Fake a STT_GNU_IFUNC symbol. */
1125 h->type = STT_GNU_IFUNC;
1126 h->def_regular = 1;
1127 h->ref_regular = 1;
1128 h->forced_local = 1;
1129 h->root.type = bfd_link_hash_defined;
1131 else
1132 h = NULL;
1134 else
1136 isym = NULL;
1137 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1138 while (h->root.type == bfd_link_hash_indirect
1139 || h->root.type == bfd_link_hash_warning)
1140 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1143 if (h != NULL)
1145 /* Create the ifunc sections for static executables. If we
1146 never see an indirect function symbol nor we are building
1147 a static executable, those sections will be empty and
1148 won't appear in output. */
1149 switch (r_type)
1151 default:
1152 break;
1154 case R_X86_64_32S:
1155 case R_X86_64_32:
1156 case R_X86_64_64:
1157 case R_X86_64_PC32:
1158 case R_X86_64_PC64:
1159 case R_X86_64_PLT32:
1160 case R_X86_64_GOTPCREL:
1161 case R_X86_64_GOTPCREL64:
1162 if (!_bfd_elf_create_ifunc_sections (abfd, info))
1163 return FALSE;
1164 break;
1167 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1168 it here if it is defined in a non-shared object. */
1169 if (h->type == STT_GNU_IFUNC
1170 && h->def_regular)
1172 /* It is referenced by a non-shared object. */
1173 h->ref_regular = 1;
1174 h->needs_plt = 1;
1176 /* STT_GNU_IFUNC symbol must go through PLT. */
1177 h->plt.refcount += 1;
1179 /* STT_GNU_IFUNC needs dynamic sections. */
1180 if (htab->elf.dynobj == NULL)
1181 htab->elf.dynobj = abfd;
1183 switch (r_type)
1185 default:
1186 if (h->root.root.string)
1187 name = h->root.root.string;
1188 else
1189 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1190 NULL);
1191 (*_bfd_error_handler)
1192 (_("%B: relocation %s against STT_GNU_IFUNC "
1193 "symbol `%s' isn't handled by %s"), abfd,
1194 x86_64_elf_howto_table[r_type].name,
1195 name, __FUNCTION__);
1196 bfd_set_error (bfd_error_bad_value);
1197 return FALSE;
1199 case R_X86_64_64:
1200 h->non_got_ref = 1;
1201 h->pointer_equality_needed = 1;
1202 if (info->shared)
1204 /* We must copy these reloc types into the output
1205 file. Create a reloc section in dynobj and
1206 make room for this reloc. */
1207 sreloc = _bfd_elf_create_ifunc_dyn_reloc
1208 (abfd, info, sec, sreloc,
1209 &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs);
1210 if (sreloc == NULL)
1211 return FALSE;
1213 break;
1215 case R_X86_64_32S:
1216 case R_X86_64_32:
1217 case R_X86_64_PC32:
1218 case R_X86_64_PC64:
1219 h->non_got_ref = 1;
1220 if (r_type != R_X86_64_PC32
1221 && r_type != R_X86_64_PC64)
1222 h->pointer_equality_needed = 1;
1223 break;
1225 case R_X86_64_PLT32:
1226 break;
1228 case R_X86_64_GOTPCREL:
1229 case R_X86_64_GOTPCREL64:
1230 h->got.refcount += 1;
1231 if (htab->elf.sgot == NULL
1232 && !_bfd_elf_create_got_section (htab->elf.dynobj,
1233 info))
1234 return FALSE;
1235 break;
1238 continue;
1242 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1243 symtab_hdr, sym_hashes,
1244 &r_type, GOT_UNKNOWN,
1245 rel, rel_end, h, r_symndx))
1246 return FALSE;
1248 switch (r_type)
1250 case R_X86_64_TLSLD:
1251 htab->tls_ld_got.refcount += 1;
1252 goto create_got;
1254 case R_X86_64_TPOFF32:
1255 if (!info->executable)
1257 if (h)
1258 name = h->root.root.string;
1259 else
1260 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1261 NULL);
1262 (*_bfd_error_handler)
1263 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1264 abfd,
1265 x86_64_elf_howto_table[r_type].name, name);
1266 bfd_set_error (bfd_error_bad_value);
1267 return FALSE;
1269 break;
1271 case R_X86_64_GOTTPOFF:
1272 if (!info->executable)
1273 info->flags |= DF_STATIC_TLS;
1274 /* Fall through */
1276 case R_X86_64_GOT32:
1277 case R_X86_64_GOTPCREL:
1278 case R_X86_64_TLSGD:
1279 case R_X86_64_GOT64:
1280 case R_X86_64_GOTPCREL64:
1281 case R_X86_64_GOTPLT64:
1282 case R_X86_64_GOTPC32_TLSDESC:
1283 case R_X86_64_TLSDESC_CALL:
1284 /* This symbol requires a global offset table entry. */
1286 int tls_type, old_tls_type;
1288 switch (r_type)
1290 default: tls_type = GOT_NORMAL; break;
1291 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1292 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1293 case R_X86_64_GOTPC32_TLSDESC:
1294 case R_X86_64_TLSDESC_CALL:
1295 tls_type = GOT_TLS_GDESC; break;
1298 if (h != NULL)
1300 if (r_type == R_X86_64_GOTPLT64)
1302 /* This relocation indicates that we also need
1303 a PLT entry, as this is a function. We don't need
1304 a PLT entry for local symbols. */
1305 h->needs_plt = 1;
1306 h->plt.refcount += 1;
1308 h->got.refcount += 1;
1309 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1311 else
1313 bfd_signed_vma *local_got_refcounts;
1315 /* This is a global offset table entry for a local symbol. */
1316 local_got_refcounts = elf_local_got_refcounts (abfd);
1317 if (local_got_refcounts == NULL)
1319 bfd_size_type size;
1321 size = symtab_hdr->sh_info;
1322 size *= sizeof (bfd_signed_vma)
1323 + sizeof (bfd_vma) + sizeof (char);
1324 local_got_refcounts = ((bfd_signed_vma *)
1325 bfd_zalloc (abfd, size));
1326 if (local_got_refcounts == NULL)
1327 return FALSE;
1328 elf_local_got_refcounts (abfd) = local_got_refcounts;
1329 elf64_x86_64_local_tlsdesc_gotent (abfd)
1330 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1331 elf64_x86_64_local_got_tls_type (abfd)
1332 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1334 local_got_refcounts[r_symndx] += 1;
1335 old_tls_type
1336 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1339 /* If a TLS symbol is accessed using IE at least once,
1340 there is no point to use dynamic model for it. */
1341 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1342 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1343 || tls_type != GOT_TLS_IE))
1345 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1346 tls_type = old_tls_type;
1347 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1348 && GOT_TLS_GD_ANY_P (tls_type))
1349 tls_type |= old_tls_type;
1350 else
1352 if (h)
1353 name = h->root.root.string;
1354 else
1355 name = bfd_elf_sym_name (abfd, symtab_hdr,
1356 isym, NULL);
1357 (*_bfd_error_handler)
1358 (_("%B: '%s' accessed both as normal and thread local symbol"),
1359 abfd, name);
1360 return FALSE;
1364 if (old_tls_type != tls_type)
1366 if (h != NULL)
1367 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1368 else
1369 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1372 /* Fall through */
1374 case R_X86_64_GOTOFF64:
1375 case R_X86_64_GOTPC32:
1376 case R_X86_64_GOTPC64:
1377 create_got:
1378 if (htab->elf.sgot == NULL)
1380 if (htab->elf.dynobj == NULL)
1381 htab->elf.dynobj = abfd;
1382 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1383 info))
1384 return FALSE;
1386 break;
1388 case R_X86_64_PLT32:
1389 /* This symbol requires a procedure linkage table entry. We
1390 actually build the entry in adjust_dynamic_symbol,
1391 because this might be a case of linking PIC code which is
1392 never referenced by a dynamic object, in which case we
1393 don't need to generate a procedure linkage table entry
1394 after all. */
1396 /* If this is a local symbol, we resolve it directly without
1397 creating a procedure linkage table entry. */
1398 if (h == NULL)
1399 continue;
1401 h->needs_plt = 1;
1402 h->plt.refcount += 1;
1403 break;
1405 case R_X86_64_PLTOFF64:
1406 /* This tries to form the 'address' of a function relative
1407 to GOT. For global symbols we need a PLT entry. */
1408 if (h != NULL)
1410 h->needs_plt = 1;
1411 h->plt.refcount += 1;
1413 goto create_got;
1415 case R_X86_64_8:
1416 case R_X86_64_16:
1417 case R_X86_64_32:
1418 case R_X86_64_32S:
1419 /* Let's help debug shared library creation. These relocs
1420 cannot be used in shared libs. Don't error out for
1421 sections we don't care about, such as debug sections or
1422 non-constant sections. */
1423 if (info->shared
1424 && (sec->flags & SEC_ALLOC) != 0
1425 && (sec->flags & SEC_READONLY) != 0)
1427 if (h)
1428 name = h->root.root.string;
1429 else
1430 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1431 (*_bfd_error_handler)
1432 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1433 abfd, x86_64_elf_howto_table[r_type].name, name);
1434 bfd_set_error (bfd_error_bad_value);
1435 return FALSE;
1437 /* Fall through. */
1439 case R_X86_64_PC8:
1440 case R_X86_64_PC16:
1441 case R_X86_64_PC32:
1442 case R_X86_64_PC64:
1443 case R_X86_64_64:
1444 if (h != NULL && info->executable)
1446 /* If this reloc is in a read-only section, we might
1447 need a copy reloc. We can't check reliably at this
1448 stage whether the section is read-only, as input
1449 sections have not yet been mapped to output sections.
1450 Tentatively set the flag for now, and correct in
1451 adjust_dynamic_symbol. */
1452 h->non_got_ref = 1;
1454 /* We may need a .plt entry if the function this reloc
1455 refers to is in a shared lib. */
1456 h->plt.refcount += 1;
1457 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1458 h->pointer_equality_needed = 1;
1461 /* If we are creating a shared library, and this is a reloc
1462 against a global symbol, or a non PC relative reloc
1463 against a local symbol, then we need to copy the reloc
1464 into the shared library. However, if we are linking with
1465 -Bsymbolic, we do not need to copy a reloc against a
1466 global symbol which is defined in an object we are
1467 including in the link (i.e., DEF_REGULAR is set). At
1468 this point we have not seen all the input files, so it is
1469 possible that DEF_REGULAR is not set now but will be set
1470 later (it is never cleared). In case of a weak definition,
1471 DEF_REGULAR may be cleared later by a strong definition in
1472 a shared library. We account for that possibility below by
1473 storing information in the relocs_copied field of the hash
1474 table entry. A similar situation occurs when creating
1475 shared libraries and symbol visibility changes render the
1476 symbol local.
1478 If on the other hand, we are creating an executable, we
1479 may need to keep relocations for symbols satisfied by a
1480 dynamic library if we manage to avoid copy relocs for the
1481 symbol. */
1482 if ((info->shared
1483 && (sec->flags & SEC_ALLOC) != 0
1484 && (! IS_X86_64_PCREL_TYPE (r_type)
1485 || (h != NULL
1486 && (! SYMBOLIC_BIND (info, h)
1487 || h->root.type == bfd_link_hash_defweak
1488 || !h->def_regular))))
1489 || (ELIMINATE_COPY_RELOCS
1490 && !info->shared
1491 && (sec->flags & SEC_ALLOC) != 0
1492 && h != NULL
1493 && (h->root.type == bfd_link_hash_defweak
1494 || !h->def_regular)))
1496 struct elf_dyn_relocs *p;
1497 struct elf_dyn_relocs **head;
1499 /* We must copy these reloc types into the output file.
1500 Create a reloc section in dynobj and make room for
1501 this reloc. */
1502 if (sreloc == NULL)
1504 if (htab->elf.dynobj == NULL)
1505 htab->elf.dynobj = abfd;
1507 sreloc = _bfd_elf_make_dynamic_reloc_section
1508 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
1510 if (sreloc == NULL)
1511 return FALSE;
1514 /* If this is a global symbol, we count the number of
1515 relocations we need for this symbol. */
1516 if (h != NULL)
1518 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1520 else
1522 /* Track dynamic relocs needed for local syms too.
1523 We really need local syms available to do this
1524 easily. Oh well. */
1525 asection *s;
1526 void **vpp;
1528 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1529 abfd, r_symndx);
1530 if (isym == NULL)
1531 return FALSE;
1533 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1534 if (s == NULL)
1535 s = sec;
1537 /* Beware of type punned pointers vs strict aliasing
1538 rules. */
1539 vpp = &(elf_section_data (s)->local_dynrel);
1540 head = (struct elf_dyn_relocs **)vpp;
1543 p = *head;
1544 if (p == NULL || p->sec != sec)
1546 bfd_size_type amt = sizeof *p;
1548 p = ((struct elf_dyn_relocs *)
1549 bfd_alloc (htab->elf.dynobj, amt));
1550 if (p == NULL)
1551 return FALSE;
1552 p->next = *head;
1553 *head = p;
1554 p->sec = sec;
1555 p->count = 0;
1556 p->pc_count = 0;
1559 p->count += 1;
1560 if (IS_X86_64_PCREL_TYPE (r_type))
1561 p->pc_count += 1;
1563 break;
1565 /* This relocation describes the C++ object vtable hierarchy.
1566 Reconstruct it for later use during GC. */
1567 case R_X86_64_GNU_VTINHERIT:
1568 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1569 return FALSE;
1570 break;
1572 /* This relocation describes which C++ vtable entries are actually
1573 used. Record for later use during GC. */
1574 case R_X86_64_GNU_VTENTRY:
1575 BFD_ASSERT (h != NULL);
1576 if (h != NULL
1577 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1578 return FALSE;
1579 break;
1581 default:
1582 break;
1586 return TRUE;
1589 /* Return the section that should be marked against GC for a given
1590 relocation. */
1592 static asection *
1593 elf64_x86_64_gc_mark_hook (asection *sec,
1594 struct bfd_link_info *info,
1595 Elf_Internal_Rela *rel,
1596 struct elf_link_hash_entry *h,
1597 Elf_Internal_Sym *sym)
1599 if (h != NULL)
1600 switch (ELF64_R_TYPE (rel->r_info))
1602 case R_X86_64_GNU_VTINHERIT:
1603 case R_X86_64_GNU_VTENTRY:
1604 return NULL;
1607 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1610 /* Update the got entry reference counts for the section being removed. */
1612 static bfd_boolean
1613 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1614 asection *sec,
1615 const Elf_Internal_Rela *relocs)
1617 struct elf64_x86_64_link_hash_table *htab;
1618 Elf_Internal_Shdr *symtab_hdr;
1619 struct elf_link_hash_entry **sym_hashes;
1620 bfd_signed_vma *local_got_refcounts;
1621 const Elf_Internal_Rela *rel, *relend;
1623 if (info->relocatable)
1624 return TRUE;
1626 htab = elf64_x86_64_hash_table (info);
1627 if (htab == NULL)
1628 return FALSE;
1630 elf_section_data (sec)->local_dynrel = NULL;
1632 symtab_hdr = &elf_symtab_hdr (abfd);
1633 sym_hashes = elf_sym_hashes (abfd);
1634 local_got_refcounts = elf_local_got_refcounts (abfd);
1636 relend = relocs + sec->reloc_count;
1637 for (rel = relocs; rel < relend; rel++)
1639 unsigned long r_symndx;
1640 unsigned int r_type;
1641 struct elf_link_hash_entry *h = NULL;
1643 r_symndx = ELF64_R_SYM (rel->r_info);
1644 if (r_symndx >= symtab_hdr->sh_info)
1646 struct elf64_x86_64_link_hash_entry *eh;
1647 struct elf_dyn_relocs **pp;
1648 struct elf_dyn_relocs *p;
1650 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1651 while (h->root.type == bfd_link_hash_indirect
1652 || h->root.type == bfd_link_hash_warning)
1653 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1654 eh = (struct elf64_x86_64_link_hash_entry *) h;
1656 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1657 if (p->sec == sec)
1659 /* Everything must go for SEC. */
1660 *pp = p->next;
1661 break;
1664 else
1666 /* A local symbol. */
1667 Elf_Internal_Sym *isym;
1669 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1670 abfd, r_symndx);
1672 /* Check relocation against local STT_GNU_IFUNC symbol. */
1673 if (isym != NULL
1674 && ELF64_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1676 h = elf64_x86_64_get_local_sym_hash (htab, abfd, rel,
1677 FALSE);
1678 if (h == NULL)
1679 abort ();
1683 r_type = ELF64_R_TYPE (rel->r_info);
1684 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1685 symtab_hdr, sym_hashes,
1686 &r_type, GOT_UNKNOWN,
1687 rel, relend, h, r_symndx))
1688 return FALSE;
1690 switch (r_type)
1692 case R_X86_64_TLSLD:
1693 if (htab->tls_ld_got.refcount > 0)
1694 htab->tls_ld_got.refcount -= 1;
1695 break;
1697 case R_X86_64_TLSGD:
1698 case R_X86_64_GOTPC32_TLSDESC:
1699 case R_X86_64_TLSDESC_CALL:
1700 case R_X86_64_GOTTPOFF:
1701 case R_X86_64_GOT32:
1702 case R_X86_64_GOTPCREL:
1703 case R_X86_64_GOT64:
1704 case R_X86_64_GOTPCREL64:
1705 case R_X86_64_GOTPLT64:
1706 if (h != NULL)
1708 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1709 h->plt.refcount -= 1;
1710 if (h->got.refcount > 0)
1711 h->got.refcount -= 1;
1712 if (h->type == STT_GNU_IFUNC)
1714 if (h->plt.refcount > 0)
1715 h->plt.refcount -= 1;
1718 else if (local_got_refcounts != NULL)
1720 if (local_got_refcounts[r_symndx] > 0)
1721 local_got_refcounts[r_symndx] -= 1;
1723 break;
1725 case R_X86_64_8:
1726 case R_X86_64_16:
1727 case R_X86_64_32:
1728 case R_X86_64_64:
1729 case R_X86_64_32S:
1730 case R_X86_64_PC8:
1731 case R_X86_64_PC16:
1732 case R_X86_64_PC32:
1733 case R_X86_64_PC64:
1734 if (info->shared)
1735 break;
1736 /* Fall thru */
1738 case R_X86_64_PLT32:
1739 case R_X86_64_PLTOFF64:
1740 if (h != NULL)
1742 if (h->plt.refcount > 0)
1743 h->plt.refcount -= 1;
1745 break;
1747 default:
1748 break;
1752 return TRUE;
1755 /* Adjust a symbol defined by a dynamic object and referenced by a
1756 regular object. The current definition is in some section of the
1757 dynamic object, but we're not including those sections. We have to
1758 change the definition to something the rest of the link can
1759 understand. */
1761 static bfd_boolean
1762 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1763 struct elf_link_hash_entry *h)
1765 struct elf64_x86_64_link_hash_table *htab;
1766 asection *s;
1768 /* STT_GNU_IFUNC symbol must go through PLT. */
1769 if (h->type == STT_GNU_IFUNC)
1771 if (h->plt.refcount <= 0)
1773 h->plt.offset = (bfd_vma) -1;
1774 h->needs_plt = 0;
1776 return TRUE;
1779 /* If this is a function, put it in the procedure linkage table. We
1780 will fill in the contents of the procedure linkage table later,
1781 when we know the address of the .got section. */
1782 if (h->type == STT_FUNC
1783 || h->needs_plt)
1785 if (h->plt.refcount <= 0
1786 || SYMBOL_CALLS_LOCAL (info, h)
1787 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1788 && h->root.type == bfd_link_hash_undefweak))
1790 /* This case can occur if we saw a PLT32 reloc in an input
1791 file, but the symbol was never referred to by a dynamic
1792 object, or if all references were garbage collected. In
1793 such a case, we don't actually need to build a procedure
1794 linkage table, and we can just do a PC32 reloc instead. */
1795 h->plt.offset = (bfd_vma) -1;
1796 h->needs_plt = 0;
1799 return TRUE;
1801 else
1802 /* It's possible that we incorrectly decided a .plt reloc was
1803 needed for an R_X86_64_PC32 reloc to a non-function sym in
1804 check_relocs. We can't decide accurately between function and
1805 non-function syms in check-relocs; Objects loaded later in
1806 the link may change h->type. So fix it now. */
1807 h->plt.offset = (bfd_vma) -1;
1809 /* If this is a weak symbol, and there is a real definition, the
1810 processor independent code will have arranged for us to see the
1811 real definition first, and we can just use the same value. */
1812 if (h->u.weakdef != NULL)
1814 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1815 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1816 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1817 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1818 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1819 h->non_got_ref = h->u.weakdef->non_got_ref;
1820 return TRUE;
1823 /* This is a reference to a symbol defined by a dynamic object which
1824 is not a function. */
1826 /* If we are creating a shared library, we must presume that the
1827 only references to the symbol are via the global offset table.
1828 For such cases we need not do anything here; the relocations will
1829 be handled correctly by relocate_section. */
1830 if (info->shared)
1831 return TRUE;
1833 /* If there are no references to this symbol that do not use the
1834 GOT, we don't need to generate a copy reloc. */
1835 if (!h->non_got_ref)
1836 return TRUE;
1838 /* If -z nocopyreloc was given, we won't generate them either. */
1839 if (info->nocopyreloc)
1841 h->non_got_ref = 0;
1842 return TRUE;
1845 if (ELIMINATE_COPY_RELOCS)
1847 struct elf64_x86_64_link_hash_entry * eh;
1848 struct elf_dyn_relocs *p;
1850 eh = (struct elf64_x86_64_link_hash_entry *) h;
1851 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1853 s = p->sec->output_section;
1854 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1855 break;
1858 /* If we didn't find any dynamic relocs in read-only sections, then
1859 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1860 if (p == NULL)
1862 h->non_got_ref = 0;
1863 return TRUE;
1867 if (h->size == 0)
1869 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1870 h->root.root.string);
1871 return TRUE;
1874 /* We must allocate the symbol in our .dynbss section, which will
1875 become part of the .bss section of the executable. There will be
1876 an entry for this symbol in the .dynsym section. The dynamic
1877 object will contain position independent code, so all references
1878 from the dynamic object to this symbol will go through the global
1879 offset table. The dynamic linker will use the .dynsym entry to
1880 determine the address it must put in the global offset table, so
1881 both the dynamic object and the regular object will refer to the
1882 same memory location for the variable. */
1884 htab = elf64_x86_64_hash_table (info);
1885 if (htab == NULL)
1886 return FALSE;
1888 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1889 to copy the initial value out of the dynamic object and into the
1890 runtime process image. */
1891 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1893 htab->srelbss->size += sizeof (Elf64_External_Rela);
1894 h->needs_copy = 1;
1897 s = htab->sdynbss;
1899 return _bfd_elf_adjust_dynamic_copy (h, s);
1902 /* Allocate space in .plt, .got and associated reloc sections for
1903 dynamic relocs. */
1905 static bfd_boolean
1906 elf64_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1908 struct bfd_link_info *info;
1909 struct elf64_x86_64_link_hash_table *htab;
1910 struct elf64_x86_64_link_hash_entry *eh;
1911 struct elf_dyn_relocs *p;
1913 if (h->root.type == bfd_link_hash_indirect)
1914 return TRUE;
1916 if (h->root.type == bfd_link_hash_warning)
1917 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1918 eh = (struct elf64_x86_64_link_hash_entry *) h;
1920 info = (struct bfd_link_info *) inf;
1921 htab = elf64_x86_64_hash_table (info);
1922 if (htab == NULL)
1923 return FALSE;
1925 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1926 here if it is defined and referenced in a non-shared object. */
1927 if (h->type == STT_GNU_IFUNC
1928 && h->def_regular)
1929 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
1930 &eh->dyn_relocs,
1931 PLT_ENTRY_SIZE,
1932 GOT_ENTRY_SIZE);
1933 else if (htab->elf.dynamic_sections_created
1934 && h->plt.refcount > 0)
1936 /* Make sure this symbol is output as a dynamic symbol.
1937 Undefined weak syms won't yet be marked as dynamic. */
1938 if (h->dynindx == -1
1939 && !h->forced_local)
1941 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1942 return FALSE;
1945 if (info->shared
1946 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1948 asection *s = htab->elf.splt;
1950 /* If this is the first .plt entry, make room for the special
1951 first entry. */
1952 if (s->size == 0)
1953 s->size += PLT_ENTRY_SIZE;
1955 h->plt.offset = s->size;
1957 /* If this symbol is not defined in a regular file, and we are
1958 not generating a shared library, then set the symbol to this
1959 location in the .plt. This is required to make function
1960 pointers compare as equal between the normal executable and
1961 the shared library. */
1962 if (! info->shared
1963 && !h->def_regular)
1965 h->root.u.def.section = s;
1966 h->root.u.def.value = h->plt.offset;
1969 /* Make room for this entry. */
1970 s->size += PLT_ENTRY_SIZE;
1972 /* We also need to make an entry in the .got.plt section, which
1973 will be placed in the .got section by the linker script. */
1974 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1976 /* We also need to make an entry in the .rela.plt section. */
1977 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1978 htab->elf.srelplt->reloc_count++;
1980 else
1982 h->plt.offset = (bfd_vma) -1;
1983 h->needs_plt = 0;
1986 else
1988 h->plt.offset = (bfd_vma) -1;
1989 h->needs_plt = 0;
1992 eh->tlsdesc_got = (bfd_vma) -1;
1994 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1995 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1996 if (h->got.refcount > 0
1997 && info->executable
1998 && h->dynindx == -1
1999 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
2001 h->got.offset = (bfd_vma) -1;
2003 else if (h->got.refcount > 0)
2005 asection *s;
2006 bfd_boolean dyn;
2007 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2009 /* Make sure this symbol is output as a dynamic symbol.
2010 Undefined weak syms won't yet be marked as dynamic. */
2011 if (h->dynindx == -1
2012 && !h->forced_local)
2014 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2015 return FALSE;
2018 if (GOT_TLS_GDESC_P (tls_type))
2020 eh->tlsdesc_got = htab->elf.sgotplt->size
2021 - elf64_x86_64_compute_jump_table_size (htab);
2022 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2023 h->got.offset = (bfd_vma) -2;
2025 if (! GOT_TLS_GDESC_P (tls_type)
2026 || GOT_TLS_GD_P (tls_type))
2028 s = htab->elf.sgot;
2029 h->got.offset = s->size;
2030 s->size += GOT_ENTRY_SIZE;
2031 if (GOT_TLS_GD_P (tls_type))
2032 s->size += GOT_ENTRY_SIZE;
2034 dyn = htab->elf.dynamic_sections_created;
2035 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2036 and two if global.
2037 R_X86_64_GOTTPOFF needs one dynamic relocation. */
2038 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2039 || tls_type == GOT_TLS_IE)
2040 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2041 else if (GOT_TLS_GD_P (tls_type))
2042 htab->elf.srelgot->size += 2 * sizeof (Elf64_External_Rela);
2043 else if (! GOT_TLS_GDESC_P (tls_type)
2044 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2045 || h->root.type != bfd_link_hash_undefweak)
2046 && (info->shared
2047 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2048 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2049 if (GOT_TLS_GDESC_P (tls_type))
2051 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
2052 htab->tlsdesc_plt = (bfd_vma) -1;
2055 else
2056 h->got.offset = (bfd_vma) -1;
2058 if (eh->dyn_relocs == NULL)
2059 return TRUE;
2061 /* In the shared -Bsymbolic case, discard space allocated for
2062 dynamic pc-relative relocs against symbols which turn out to be
2063 defined in regular objects. For the normal shared case, discard
2064 space for pc-relative relocs that have become local due to symbol
2065 visibility changes. */
2067 if (info->shared)
2069 /* Relocs that use pc_count are those that appear on a call
2070 insn, or certain REL relocs that can generated via assembly.
2071 We want calls to protected symbols to resolve directly to the
2072 function rather than going via the plt. If people want
2073 function pointer comparisons to work as expected then they
2074 should avoid writing weird assembly. */
2075 if (SYMBOL_CALLS_LOCAL (info, h))
2077 struct elf_dyn_relocs **pp;
2079 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2081 p->count -= p->pc_count;
2082 p->pc_count = 0;
2083 if (p->count == 0)
2084 *pp = p->next;
2085 else
2086 pp = &p->next;
2090 /* Also discard relocs on undefined weak syms with non-default
2091 visibility. */
2092 if (eh->dyn_relocs != NULL
2093 && h->root.type == bfd_link_hash_undefweak)
2095 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2096 eh->dyn_relocs = NULL;
2098 /* Make sure undefined weak symbols are output as a dynamic
2099 symbol in PIEs. */
2100 else if (h->dynindx == -1
2101 && ! h->forced_local
2102 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2103 return FALSE;
2107 else if (ELIMINATE_COPY_RELOCS)
2109 /* For the non-shared case, discard space for relocs against
2110 symbols which turn out to need copy relocs or are not
2111 dynamic. */
2113 if (!h->non_got_ref
2114 && ((h->def_dynamic
2115 && !h->def_regular)
2116 || (htab->elf.dynamic_sections_created
2117 && (h->root.type == bfd_link_hash_undefweak
2118 || h->root.type == bfd_link_hash_undefined))))
2120 /* Make sure this symbol is output as a dynamic symbol.
2121 Undefined weak syms won't yet be marked as dynamic. */
2122 if (h->dynindx == -1
2123 && ! h->forced_local
2124 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2125 return FALSE;
2127 /* If that succeeded, we know we'll be keeping all the
2128 relocs. */
2129 if (h->dynindx != -1)
2130 goto keep;
2133 eh->dyn_relocs = NULL;
2135 keep: ;
2138 /* Finally, allocate space. */
2139 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2141 asection * sreloc;
2143 sreloc = elf_section_data (p->sec)->sreloc;
2145 BFD_ASSERT (sreloc != NULL);
2147 sreloc->size += p->count * sizeof (Elf64_External_Rela);
2150 return TRUE;
2153 /* Allocate space in .plt, .got and associated reloc sections for
2154 local dynamic relocs. */
2156 static bfd_boolean
2157 elf64_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
2159 struct elf_link_hash_entry *h
2160 = (struct elf_link_hash_entry *) *slot;
2162 if (h->type != STT_GNU_IFUNC
2163 || !h->def_regular
2164 || !h->ref_regular
2165 || !h->forced_local
2166 || h->root.type != bfd_link_hash_defined)
2167 abort ();
2169 return elf64_x86_64_allocate_dynrelocs (h, inf);
2172 /* Find any dynamic relocs that apply to read-only sections. */
2174 static bfd_boolean
2175 elf64_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2177 struct elf64_x86_64_link_hash_entry *eh;
2178 struct elf_dyn_relocs *p;
2180 if (h->root.type == bfd_link_hash_warning)
2181 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2183 eh = (struct elf64_x86_64_link_hash_entry *) h;
2184 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2186 asection *s = p->sec->output_section;
2188 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2190 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2192 info->flags |= DF_TEXTREL;
2194 /* Not an error, just cut short the traversal. */
2195 return FALSE;
2198 return TRUE;
2201 /* Set the sizes of the dynamic sections. */
2203 static bfd_boolean
2204 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2205 struct bfd_link_info *info)
2207 struct elf64_x86_64_link_hash_table *htab;
2208 bfd *dynobj;
2209 asection *s;
2210 bfd_boolean relocs;
2211 bfd *ibfd;
2213 htab = elf64_x86_64_hash_table (info);
2214 if (htab == NULL)
2215 return FALSE;
2217 dynobj = htab->elf.dynobj;
2218 if (dynobj == NULL)
2219 abort ();
2221 if (htab->elf.dynamic_sections_created)
2223 /* Set the contents of the .interp section to the interpreter. */
2224 if (info->executable)
2226 s = bfd_get_section_by_name (dynobj, ".interp");
2227 if (s == NULL)
2228 abort ();
2229 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2230 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2234 /* Set up .got offsets for local syms, and space for local dynamic
2235 relocs. */
2236 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2238 bfd_signed_vma *local_got;
2239 bfd_signed_vma *end_local_got;
2240 char *local_tls_type;
2241 bfd_vma *local_tlsdesc_gotent;
2242 bfd_size_type locsymcount;
2243 Elf_Internal_Shdr *symtab_hdr;
2244 asection *srel;
2246 if (! is_x86_64_elf (ibfd))
2247 continue;
2249 for (s = ibfd->sections; s != NULL; s = s->next)
2251 struct elf_dyn_relocs *p;
2253 for (p = (struct elf_dyn_relocs *)
2254 (elf_section_data (s)->local_dynrel);
2255 p != NULL;
2256 p = p->next)
2258 if (!bfd_is_abs_section (p->sec)
2259 && bfd_is_abs_section (p->sec->output_section))
2261 /* Input section has been discarded, either because
2262 it is a copy of a linkonce section or due to
2263 linker script /DISCARD/, so we'll be discarding
2264 the relocs too. */
2266 else if (p->count != 0)
2268 srel = elf_section_data (p->sec)->sreloc;
2269 srel->size += p->count * sizeof (Elf64_External_Rela);
2270 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2271 info->flags |= DF_TEXTREL;
2276 local_got = elf_local_got_refcounts (ibfd);
2277 if (!local_got)
2278 continue;
2280 symtab_hdr = &elf_symtab_hdr (ibfd);
2281 locsymcount = symtab_hdr->sh_info;
2282 end_local_got = local_got + locsymcount;
2283 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
2284 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
2285 s = htab->elf.sgot;
2286 srel = htab->elf.srelgot;
2287 for (; local_got < end_local_got;
2288 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2290 *local_tlsdesc_gotent = (bfd_vma) -1;
2291 if (*local_got > 0)
2293 if (GOT_TLS_GDESC_P (*local_tls_type))
2295 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2296 - elf64_x86_64_compute_jump_table_size (htab);
2297 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2298 *local_got = (bfd_vma) -2;
2300 if (! GOT_TLS_GDESC_P (*local_tls_type)
2301 || GOT_TLS_GD_P (*local_tls_type))
2303 *local_got = s->size;
2304 s->size += GOT_ENTRY_SIZE;
2305 if (GOT_TLS_GD_P (*local_tls_type))
2306 s->size += GOT_ENTRY_SIZE;
2308 if (info->shared
2309 || GOT_TLS_GD_ANY_P (*local_tls_type)
2310 || *local_tls_type == GOT_TLS_IE)
2312 if (GOT_TLS_GDESC_P (*local_tls_type))
2314 htab->elf.srelplt->size
2315 += sizeof (Elf64_External_Rela);
2316 htab->tlsdesc_plt = (bfd_vma) -1;
2318 if (! GOT_TLS_GDESC_P (*local_tls_type)
2319 || GOT_TLS_GD_P (*local_tls_type))
2320 srel->size += sizeof (Elf64_External_Rela);
2323 else
2324 *local_got = (bfd_vma) -1;
2328 if (htab->tls_ld_got.refcount > 0)
2330 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2331 relocs. */
2332 htab->tls_ld_got.offset = htab->elf.sgot->size;
2333 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
2334 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2336 else
2337 htab->tls_ld_got.offset = -1;
2339 /* Allocate global sym .plt and .got entries, and space for global
2340 sym dynamic relocs. */
2341 elf_link_hash_traverse (&htab->elf, elf64_x86_64_allocate_dynrelocs,
2342 info);
2344 /* Allocate .plt and .got entries, and space for local symbols. */
2345 htab_traverse (htab->loc_hash_table,
2346 elf64_x86_64_allocate_local_dynrelocs,
2347 info);
2349 /* For every jump slot reserved in the sgotplt, reloc_count is
2350 incremented. However, when we reserve space for TLS descriptors,
2351 it's not incremented, so in order to compute the space reserved
2352 for them, it suffices to multiply the reloc count by the jump
2353 slot size. */
2354 if (htab->elf.srelplt)
2355 htab->sgotplt_jump_table_size
2356 = elf64_x86_64_compute_jump_table_size (htab);
2358 if (htab->tlsdesc_plt)
2360 /* If we're not using lazy TLS relocations, don't generate the
2361 PLT and GOT entries they require. */
2362 if ((info->flags & DF_BIND_NOW))
2363 htab->tlsdesc_plt = 0;
2364 else
2366 htab->tlsdesc_got = htab->elf.sgot->size;
2367 htab->elf.sgot->size += GOT_ENTRY_SIZE;
2368 /* Reserve room for the initial entry.
2369 FIXME: we could probably do away with it in this case. */
2370 if (htab->elf.splt->size == 0)
2371 htab->elf.splt->size += PLT_ENTRY_SIZE;
2372 htab->tlsdesc_plt = htab->elf.splt->size;
2373 htab->elf.splt->size += PLT_ENTRY_SIZE;
2377 if (htab->elf.sgotplt)
2379 /* Don't allocate .got.plt section if there are no GOT nor PLT
2380 entries. */
2381 if ((htab->elf.sgotplt->size
2382 == get_elf_backend_data (output_bfd)->got_header_size)
2383 && (htab->elf.splt == NULL
2384 || htab->elf.splt->size == 0)
2385 && (htab->elf.sgot == NULL
2386 || htab->elf.sgot->size == 0)
2387 && (htab->elf.iplt == NULL
2388 || htab->elf.iplt->size == 0)
2389 && (htab->elf.igotplt == NULL
2390 || htab->elf.igotplt->size == 0))
2391 htab->elf.sgotplt->size = 0;
2394 /* We now have determined the sizes of the various dynamic sections.
2395 Allocate memory for them. */
2396 relocs = FALSE;
2397 for (s = dynobj->sections; s != NULL; s = s->next)
2399 if ((s->flags & SEC_LINKER_CREATED) == 0)
2400 continue;
2402 if (s == htab->elf.splt
2403 || s == htab->elf.sgot
2404 || s == htab->elf.sgotplt
2405 || s == htab->elf.iplt
2406 || s == htab->elf.igotplt
2407 || s == htab->sdynbss)
2409 /* Strip this section if we don't need it; see the
2410 comment below. */
2412 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2414 if (s->size != 0 && s != htab->elf.srelplt)
2415 relocs = TRUE;
2417 /* We use the reloc_count field as a counter if we need
2418 to copy relocs into the output file. */
2419 if (s != htab->elf.srelplt)
2420 s->reloc_count = 0;
2422 else
2424 /* It's not one of our sections, so don't allocate space. */
2425 continue;
2428 if (s->size == 0)
2430 /* If we don't need this section, strip it from the
2431 output file. This is mostly to handle .rela.bss and
2432 .rela.plt. We must create both sections in
2433 create_dynamic_sections, because they must be created
2434 before the linker maps input sections to output
2435 sections. The linker does that before
2436 adjust_dynamic_symbol is called, and it is that
2437 function which decides whether anything needs to go
2438 into these sections. */
2440 s->flags |= SEC_EXCLUDE;
2441 continue;
2444 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2445 continue;
2447 /* Allocate memory for the section contents. We use bfd_zalloc
2448 here in case unused entries are not reclaimed before the
2449 section's contents are written out. This should not happen,
2450 but this way if it does, we get a R_X86_64_NONE reloc instead
2451 of garbage. */
2452 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2453 if (s->contents == NULL)
2454 return FALSE;
2457 if (htab->elf.dynamic_sections_created)
2459 /* Add some entries to the .dynamic section. We fill in the
2460 values later, in elf64_x86_64_finish_dynamic_sections, but we
2461 must add the entries now so that we get the correct size for
2462 the .dynamic section. The DT_DEBUG entry is filled in by the
2463 dynamic linker and used by the debugger. */
2464 #define add_dynamic_entry(TAG, VAL) \
2465 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2467 if (info->executable)
2469 if (!add_dynamic_entry (DT_DEBUG, 0))
2470 return FALSE;
2473 if (htab->elf.splt->size != 0)
2475 if (!add_dynamic_entry (DT_PLTGOT, 0)
2476 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2477 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2478 || !add_dynamic_entry (DT_JMPREL, 0))
2479 return FALSE;
2481 if (htab->tlsdesc_plt
2482 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2483 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2484 return FALSE;
2487 if (relocs)
2489 if (!add_dynamic_entry (DT_RELA, 0)
2490 || !add_dynamic_entry (DT_RELASZ, 0)
2491 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2492 return FALSE;
2494 /* If any dynamic relocs apply to a read-only section,
2495 then we need a DT_TEXTREL entry. */
2496 if ((info->flags & DF_TEXTREL) == 0)
2497 elf_link_hash_traverse (&htab->elf,
2498 elf64_x86_64_readonly_dynrelocs,
2499 info);
2501 if ((info->flags & DF_TEXTREL) != 0)
2503 if (!add_dynamic_entry (DT_TEXTREL, 0))
2504 return FALSE;
2508 #undef add_dynamic_entry
2510 return TRUE;
2513 static bfd_boolean
2514 elf64_x86_64_always_size_sections (bfd *output_bfd,
2515 struct bfd_link_info *info)
2517 asection *tls_sec = elf_hash_table (info)->tls_sec;
2519 if (tls_sec)
2521 struct elf_link_hash_entry *tlsbase;
2523 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2524 "_TLS_MODULE_BASE_",
2525 FALSE, FALSE, FALSE);
2527 if (tlsbase && tlsbase->type == STT_TLS)
2529 struct elf64_x86_64_link_hash_table *htab;
2530 struct bfd_link_hash_entry *bh = NULL;
2531 const struct elf_backend_data *bed
2532 = get_elf_backend_data (output_bfd);
2534 htab = elf64_x86_64_hash_table (info);
2535 if (htab == NULL)
2536 return FALSE;
2538 if (!(_bfd_generic_link_add_one_symbol
2539 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2540 tls_sec, 0, NULL, FALSE,
2541 bed->collect, &bh)))
2542 return FALSE;
2544 htab->tls_module_base = bh;
2546 tlsbase = (struct elf_link_hash_entry *)bh;
2547 tlsbase->def_regular = 1;
2548 tlsbase->other = STV_HIDDEN;
2549 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2553 return TRUE;
2556 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2557 executables. Rather than setting it to the beginning of the TLS
2558 section, we have to set it to the end. This function may be called
2559 multiple times, it is idempotent. */
2561 static void
2562 elf64_x86_64_set_tls_module_base (struct bfd_link_info *info)
2564 struct elf64_x86_64_link_hash_table *htab;
2565 struct bfd_link_hash_entry *base;
2567 if (!info->executable)
2568 return;
2570 htab = elf64_x86_64_hash_table (info);
2571 if (htab == NULL)
2572 return;
2574 base = htab->tls_module_base;
2575 if (base == NULL)
2576 return;
2578 base->u.def.value = htab->elf.tls_size;
2581 /* Return the base VMA address which should be subtracted from real addresses
2582 when resolving @dtpoff relocation.
2583 This is PT_TLS segment p_vaddr. */
2585 static bfd_vma
2586 elf64_x86_64_dtpoff_base (struct bfd_link_info *info)
2588 /* If tls_sec is NULL, we should have signalled an error already. */
2589 if (elf_hash_table (info)->tls_sec == NULL)
2590 return 0;
2591 return elf_hash_table (info)->tls_sec->vma;
2594 /* Return the relocation value for @tpoff relocation
2595 if STT_TLS virtual address is ADDRESS. */
2597 static bfd_vma
2598 elf64_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2600 struct elf_link_hash_table *htab = elf_hash_table (info);
2602 /* If tls_segment is NULL, we should have signalled an error already. */
2603 if (htab->tls_sec == NULL)
2604 return 0;
2605 return address - htab->tls_size - htab->tls_sec->vma;
2608 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2609 branch? */
2611 static bfd_boolean
2612 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2614 /* Opcode Instruction
2615 0xe8 call
2616 0xe9 jump
2617 0x0f 0x8x conditional jump */
2618 return ((offset > 0
2619 && (contents [offset - 1] == 0xe8
2620 || contents [offset - 1] == 0xe9))
2621 || (offset > 1
2622 && contents [offset - 2] == 0x0f
2623 && (contents [offset - 1] & 0xf0) == 0x80));
2626 static void
2627 elf64_x86_64_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
2629 bfd_byte *loc = s->contents;
2630 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2631 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2632 <= s->contents + s->size);
2633 bfd_elf64_swap_reloca_out (abfd, rel, loc);
2636 /* Relocate an x86_64 ELF section. */
2638 static bfd_boolean
2639 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2640 bfd *input_bfd, asection *input_section,
2641 bfd_byte *contents, Elf_Internal_Rela *relocs,
2642 Elf_Internal_Sym *local_syms,
2643 asection **local_sections)
2645 struct elf64_x86_64_link_hash_table *htab;
2646 Elf_Internal_Shdr *symtab_hdr;
2647 struct elf_link_hash_entry **sym_hashes;
2648 bfd_vma *local_got_offsets;
2649 bfd_vma *local_tlsdesc_gotents;
2650 Elf_Internal_Rela *rel;
2651 Elf_Internal_Rela *relend;
2653 BFD_ASSERT (is_x86_64_elf (input_bfd));
2655 htab = elf64_x86_64_hash_table (info);
2656 if (htab == NULL)
2657 return FALSE;
2658 symtab_hdr = &elf_symtab_hdr (input_bfd);
2659 sym_hashes = elf_sym_hashes (input_bfd);
2660 local_got_offsets = elf_local_got_offsets (input_bfd);
2661 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2663 elf64_x86_64_set_tls_module_base (info);
2665 rel = relocs;
2666 relend = relocs + input_section->reloc_count;
2667 for (; rel < relend; rel++)
2669 unsigned int r_type;
2670 reloc_howto_type *howto;
2671 unsigned long r_symndx;
2672 struct elf_link_hash_entry *h;
2673 Elf_Internal_Sym *sym;
2674 asection *sec;
2675 bfd_vma off, offplt;
2676 bfd_vma relocation;
2677 bfd_boolean unresolved_reloc;
2678 bfd_reloc_status_type r;
2679 int tls_type;
2680 asection *base_got;
2682 r_type = ELF64_R_TYPE (rel->r_info);
2683 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2684 || r_type == (int) R_X86_64_GNU_VTENTRY)
2685 continue;
2687 if (r_type >= R_X86_64_max)
2689 bfd_set_error (bfd_error_bad_value);
2690 return FALSE;
2693 howto = x86_64_elf_howto_table + r_type;
2694 r_symndx = ELF64_R_SYM (rel->r_info);
2695 h = NULL;
2696 sym = NULL;
2697 sec = NULL;
2698 unresolved_reloc = FALSE;
2699 if (r_symndx < symtab_hdr->sh_info)
2701 sym = local_syms + r_symndx;
2702 sec = local_sections[r_symndx];
2704 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2705 &sec, rel);
2707 /* Relocate against local STT_GNU_IFUNC symbol. */
2708 if (!info->relocatable
2709 && ELF64_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2711 h = elf64_x86_64_get_local_sym_hash (htab, input_bfd,
2712 rel, FALSE);
2713 if (h == NULL)
2714 abort ();
2716 /* Set STT_GNU_IFUNC symbol value. */
2717 h->root.u.def.value = sym->st_value;
2718 h->root.u.def.section = sec;
2721 else
2723 bfd_boolean warned ATTRIBUTE_UNUSED;
2725 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2726 r_symndx, symtab_hdr, sym_hashes,
2727 h, sec, relocation,
2728 unresolved_reloc, warned);
2731 if (sec != NULL && elf_discarded_section (sec))
2732 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2733 rel, relend, howto, contents);
2735 if (info->relocatable)
2736 continue;
2738 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2739 it here if it is defined in a non-shared object. */
2740 if (h != NULL
2741 && h->type == STT_GNU_IFUNC
2742 && h->def_regular)
2744 asection *plt;
2745 bfd_vma plt_index;
2746 const char *name;
2748 if ((input_section->flags & SEC_ALLOC) == 0
2749 || h->plt.offset == (bfd_vma) -1)
2750 abort ();
2752 /* STT_GNU_IFUNC symbol must go through PLT. */
2753 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2754 relocation = (plt->output_section->vma
2755 + plt->output_offset + h->plt.offset);
2757 switch (r_type)
2759 default:
2760 if (h->root.root.string)
2761 name = h->root.root.string;
2762 else
2763 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2764 NULL);
2765 (*_bfd_error_handler)
2766 (_("%B: relocation %s against STT_GNU_IFUNC "
2767 "symbol `%s' isn't handled by %s"), input_bfd,
2768 x86_64_elf_howto_table[r_type].name,
2769 name, __FUNCTION__);
2770 bfd_set_error (bfd_error_bad_value);
2771 return FALSE;
2773 case R_X86_64_32S:
2774 if (info->shared)
2775 abort ();
2776 goto do_relocation;
2778 case R_X86_64_64:
2779 if (rel->r_addend != 0)
2781 if (h->root.root.string)
2782 name = h->root.root.string;
2783 else
2784 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2785 sym, NULL);
2786 (*_bfd_error_handler)
2787 (_("%B: relocation %s against STT_GNU_IFUNC "
2788 "symbol `%s' has non-zero addend: %d"),
2789 input_bfd, x86_64_elf_howto_table[r_type].name,
2790 name, rel->r_addend);
2791 bfd_set_error (bfd_error_bad_value);
2792 return FALSE;
2795 /* Generate dynamic relcoation only when there is a
2796 non-GOF reference in a shared object. */
2797 if (info->shared && h->non_got_ref)
2799 Elf_Internal_Rela outrel;
2800 asection *sreloc;
2802 /* Need a dynamic relocation to get the real function
2803 address. */
2804 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2805 info,
2806 input_section,
2807 rel->r_offset);
2808 if (outrel.r_offset == (bfd_vma) -1
2809 || outrel.r_offset == (bfd_vma) -2)
2810 abort ();
2812 outrel.r_offset += (input_section->output_section->vma
2813 + input_section->output_offset);
2815 if (h->dynindx == -1
2816 || h->forced_local
2817 || info->executable)
2819 /* This symbol is resolved locally. */
2820 outrel.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
2821 outrel.r_addend = (h->root.u.def.value
2822 + h->root.u.def.section->output_section->vma
2823 + h->root.u.def.section->output_offset);
2825 else
2827 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2828 outrel.r_addend = 0;
2831 sreloc = htab->elf.irelifunc;
2832 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
2834 /* If this reloc is against an external symbol, we
2835 do not want to fiddle with the addend. Otherwise,
2836 we need to include the symbol value so that it
2837 becomes an addend for the dynamic reloc. For an
2838 internal symbol, we have updated addend. */
2839 continue;
2842 case R_X86_64_32:
2843 case R_X86_64_PC32:
2844 case R_X86_64_PC64:
2845 case R_X86_64_PLT32:
2846 goto do_relocation;
2848 case R_X86_64_GOTPCREL:
2849 case R_X86_64_GOTPCREL64:
2850 base_got = htab->elf.sgot;
2851 off = h->got.offset;
2853 if (base_got == NULL)
2854 abort ();
2856 if (off == (bfd_vma) -1)
2858 /* We can't use h->got.offset here to save state, or
2859 even just remember the offset, as finish_dynamic_symbol
2860 would use that as offset into .got. */
2862 if (htab->elf.splt != NULL)
2864 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2865 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2866 base_got = htab->elf.sgotplt;
2868 else
2870 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2871 off = plt_index * GOT_ENTRY_SIZE;
2872 base_got = htab->elf.igotplt;
2875 if (h->dynindx == -1
2876 || h->forced_local
2877 || info->symbolic)
2879 /* This references the local defitionion. We must
2880 initialize this entry in the global offset table.
2881 Since the offset must always be a multiple of 8,
2882 we use the least significant bit to record
2883 whether we have initialized it already.
2885 When doing a dynamic link, we create a .rela.got
2886 relocation entry to initialize the value. This
2887 is done in the finish_dynamic_symbol routine. */
2888 if ((off & 1) != 0)
2889 off &= ~1;
2890 else
2892 bfd_put_64 (output_bfd, relocation,
2893 base_got->contents + off);
2894 /* Note that this is harmless for the GOTPLT64
2895 case, as -1 | 1 still is -1. */
2896 h->got.offset |= 1;
2901 relocation = (base_got->output_section->vma
2902 + base_got->output_offset + off);
2904 if (r_type != R_X86_64_GOTPCREL
2905 && r_type != R_X86_64_GOTPCREL64)
2907 asection *gotplt;
2908 if (htab->elf.splt != NULL)
2909 gotplt = htab->elf.sgotplt;
2910 else
2911 gotplt = htab->elf.igotplt;
2912 relocation -= (gotplt->output_section->vma
2913 - gotplt->output_offset);
2916 goto do_relocation;
2920 /* When generating a shared object, the relocations handled here are
2921 copied into the output file to be resolved at run time. */
2922 switch (r_type)
2924 case R_X86_64_GOT32:
2925 case R_X86_64_GOT64:
2926 /* Relocation is to the entry for this symbol in the global
2927 offset table. */
2928 case R_X86_64_GOTPCREL:
2929 case R_X86_64_GOTPCREL64:
2930 /* Use global offset table entry as symbol value. */
2931 case R_X86_64_GOTPLT64:
2932 /* This is the same as GOT64 for relocation purposes, but
2933 indicates the existence of a PLT entry. The difficulty is,
2934 that we must calculate the GOT slot offset from the PLT
2935 offset, if this symbol got a PLT entry (it was global).
2936 Additionally if it's computed from the PLT entry, then that
2937 GOT offset is relative to .got.plt, not to .got. */
2938 base_got = htab->elf.sgot;
2940 if (htab->elf.sgot == NULL)
2941 abort ();
2943 if (h != NULL)
2945 bfd_boolean dyn;
2947 off = h->got.offset;
2948 if (h->needs_plt
2949 && h->plt.offset != (bfd_vma)-1
2950 && off == (bfd_vma)-1)
2952 /* We can't use h->got.offset here to save
2953 state, or even just remember the offset, as
2954 finish_dynamic_symbol would use that as offset into
2955 .got. */
2956 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2957 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2958 base_got = htab->elf.sgotplt;
2961 dyn = htab->elf.dynamic_sections_created;
2963 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2964 || (info->shared
2965 && SYMBOL_REFERENCES_LOCAL (info, h))
2966 || (ELF_ST_VISIBILITY (h->other)
2967 && h->root.type == bfd_link_hash_undefweak))
2969 /* This is actually a static link, or it is a -Bsymbolic
2970 link and the symbol is defined locally, or the symbol
2971 was forced to be local because of a version file. We
2972 must initialize this entry in the global offset table.
2973 Since the offset must always be a multiple of 8, we
2974 use the least significant bit to record whether we
2975 have initialized it already.
2977 When doing a dynamic link, we create a .rela.got
2978 relocation entry to initialize the value. This is
2979 done in the finish_dynamic_symbol routine. */
2980 if ((off & 1) != 0)
2981 off &= ~1;
2982 else
2984 bfd_put_64 (output_bfd, relocation,
2985 base_got->contents + off);
2986 /* Note that this is harmless for the GOTPLT64 case,
2987 as -1 | 1 still is -1. */
2988 h->got.offset |= 1;
2991 else
2992 unresolved_reloc = FALSE;
2994 else
2996 if (local_got_offsets == NULL)
2997 abort ();
2999 off = local_got_offsets[r_symndx];
3001 /* The offset must always be a multiple of 8. We use
3002 the least significant bit to record whether we have
3003 already generated the necessary reloc. */
3004 if ((off & 1) != 0)
3005 off &= ~1;
3006 else
3008 bfd_put_64 (output_bfd, relocation,
3009 base_got->contents + off);
3011 if (info->shared)
3013 asection *s;
3014 Elf_Internal_Rela outrel;
3016 /* We need to generate a R_X86_64_RELATIVE reloc
3017 for the dynamic linker. */
3018 s = htab->elf.srelgot;
3019 if (s == NULL)
3020 abort ();
3022 outrel.r_offset = (base_got->output_section->vma
3023 + base_got->output_offset
3024 + off);
3025 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3026 outrel.r_addend = relocation;
3027 elf64_x86_64_append_rela (output_bfd, s, &outrel);
3030 local_got_offsets[r_symndx] |= 1;
3034 if (off >= (bfd_vma) -2)
3035 abort ();
3037 relocation = base_got->output_section->vma
3038 + base_got->output_offset + off;
3039 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
3040 relocation -= htab->elf.sgotplt->output_section->vma
3041 - htab->elf.sgotplt->output_offset;
3043 break;
3045 case R_X86_64_GOTOFF64:
3046 /* Relocation is relative to the start of the global offset
3047 table. */
3049 /* Check to make sure it isn't a protected function symbol
3050 for shared library since it may not be local when used
3051 as function address. */
3052 if (info->shared
3053 && h
3054 && h->def_regular
3055 && h->type == STT_FUNC
3056 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3058 (*_bfd_error_handler)
3059 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3060 input_bfd, h->root.root.string);
3061 bfd_set_error (bfd_error_bad_value);
3062 return FALSE;
3065 /* Note that sgot is not involved in this
3066 calculation. We always want the start of .got.plt. If we
3067 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3068 permitted by the ABI, we might have to change this
3069 calculation. */
3070 relocation -= htab->elf.sgotplt->output_section->vma
3071 + htab->elf.sgotplt->output_offset;
3072 break;
3074 case R_X86_64_GOTPC32:
3075 case R_X86_64_GOTPC64:
3076 /* Use global offset table as symbol value. */
3077 relocation = htab->elf.sgotplt->output_section->vma
3078 + htab->elf.sgotplt->output_offset;
3079 unresolved_reloc = FALSE;
3080 break;
3082 case R_X86_64_PLTOFF64:
3083 /* Relocation is PLT entry relative to GOT. For local
3084 symbols it's the symbol itself relative to GOT. */
3085 if (h != NULL
3086 /* See PLT32 handling. */
3087 && h->plt.offset != (bfd_vma) -1
3088 && htab->elf.splt != NULL)
3090 relocation = (htab->elf.splt->output_section->vma
3091 + htab->elf.splt->output_offset
3092 + h->plt.offset);
3093 unresolved_reloc = FALSE;
3096 relocation -= htab->elf.sgotplt->output_section->vma
3097 + htab->elf.sgotplt->output_offset;
3098 break;
3100 case R_X86_64_PLT32:
3101 /* Relocation is to the entry for this symbol in the
3102 procedure linkage table. */
3104 /* Resolve a PLT32 reloc against a local symbol directly,
3105 without using the procedure linkage table. */
3106 if (h == NULL)
3107 break;
3109 if (h->plt.offset == (bfd_vma) -1
3110 || htab->elf.splt == NULL)
3112 /* We didn't make a PLT entry for this symbol. This
3113 happens when statically linking PIC code, or when
3114 using -Bsymbolic. */
3115 break;
3118 relocation = (htab->elf.splt->output_section->vma
3119 + htab->elf.splt->output_offset
3120 + h->plt.offset);
3121 unresolved_reloc = FALSE;
3122 break;
3124 case R_X86_64_PC8:
3125 case R_X86_64_PC16:
3126 case R_X86_64_PC32:
3127 if (info->shared
3128 && (input_section->flags & SEC_ALLOC) != 0
3129 && (input_section->flags & SEC_READONLY) != 0
3130 && h != NULL)
3132 bfd_boolean fail = FALSE;
3133 bfd_boolean branch
3134 = (r_type == R_X86_64_PC32
3135 && is_32bit_relative_branch (contents, rel->r_offset));
3137 if (SYMBOL_REFERENCES_LOCAL (info, h))
3139 /* Symbol is referenced locally. Make sure it is
3140 defined locally or for a branch. */
3141 fail = !h->def_regular && !branch;
3143 else
3145 /* Symbol isn't referenced locally. We only allow
3146 branch to symbol with non-default visibility. */
3147 fail = (!branch
3148 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3151 if (fail)
3153 const char *fmt;
3154 const char *v;
3155 const char *pic = "";
3157 switch (ELF_ST_VISIBILITY (h->other))
3159 case STV_HIDDEN:
3160 v = _("hidden symbol");
3161 break;
3162 case STV_INTERNAL:
3163 v = _("internal symbol");
3164 break;
3165 case STV_PROTECTED:
3166 v = _("protected symbol");
3167 break;
3168 default:
3169 v = _("symbol");
3170 pic = _("; recompile with -fPIC");
3171 break;
3174 if (h->def_regular)
3175 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3176 else
3177 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3179 (*_bfd_error_handler) (fmt, input_bfd,
3180 x86_64_elf_howto_table[r_type].name,
3181 v, h->root.root.string, pic);
3182 bfd_set_error (bfd_error_bad_value);
3183 return FALSE;
3186 /* Fall through. */
3188 case R_X86_64_8:
3189 case R_X86_64_16:
3190 case R_X86_64_32:
3191 case R_X86_64_PC64:
3192 case R_X86_64_64:
3193 /* FIXME: The ABI says the linker should make sure the value is
3194 the same when it's zeroextended to 64 bit. */
3196 if ((input_section->flags & SEC_ALLOC) == 0)
3197 break;
3199 if ((info->shared
3200 && (h == NULL
3201 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3202 || h->root.type != bfd_link_hash_undefweak)
3203 && (! IS_X86_64_PCREL_TYPE (r_type)
3204 || ! SYMBOL_CALLS_LOCAL (info, h)))
3205 || (ELIMINATE_COPY_RELOCS
3206 && !info->shared
3207 && h != NULL
3208 && h->dynindx != -1
3209 && !h->non_got_ref
3210 && ((h->def_dynamic
3211 && !h->def_regular)
3212 || h->root.type == bfd_link_hash_undefweak
3213 || h->root.type == bfd_link_hash_undefined)))
3215 Elf_Internal_Rela outrel;
3216 bfd_boolean skip, relocate;
3217 asection *sreloc;
3219 /* When generating a shared object, these relocations
3220 are copied into the output file to be resolved at run
3221 time. */
3222 skip = FALSE;
3223 relocate = FALSE;
3225 outrel.r_offset =
3226 _bfd_elf_section_offset (output_bfd, info, input_section,
3227 rel->r_offset);
3228 if (outrel.r_offset == (bfd_vma) -1)
3229 skip = TRUE;
3230 else if (outrel.r_offset == (bfd_vma) -2)
3231 skip = TRUE, relocate = TRUE;
3233 outrel.r_offset += (input_section->output_section->vma
3234 + input_section->output_offset);
3236 if (skip)
3237 memset (&outrel, 0, sizeof outrel);
3239 /* h->dynindx may be -1 if this symbol was marked to
3240 become local. */
3241 else if (h != NULL
3242 && h->dynindx != -1
3243 && (IS_X86_64_PCREL_TYPE (r_type)
3244 || ! info->shared
3245 || ! SYMBOLIC_BIND (info, h)
3246 || ! h->def_regular))
3248 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3249 outrel.r_addend = rel->r_addend;
3251 else
3253 /* This symbol is local, or marked to become local. */
3254 if (r_type == R_X86_64_64)
3256 relocate = TRUE;
3257 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3258 outrel.r_addend = relocation + rel->r_addend;
3260 else
3262 long sindx;
3264 if (bfd_is_abs_section (sec))
3265 sindx = 0;
3266 else if (sec == NULL || sec->owner == NULL)
3268 bfd_set_error (bfd_error_bad_value);
3269 return FALSE;
3271 else
3273 asection *osec;
3275 /* We are turning this relocation into one
3276 against a section symbol. It would be
3277 proper to subtract the symbol's value,
3278 osec->vma, from the emitted reloc addend,
3279 but ld.so expects buggy relocs. */
3280 osec = sec->output_section;
3281 sindx = elf_section_data (osec)->dynindx;
3282 if (sindx == 0)
3284 asection *oi = htab->elf.text_index_section;
3285 sindx = elf_section_data (oi)->dynindx;
3287 BFD_ASSERT (sindx != 0);
3290 outrel.r_info = ELF64_R_INFO (sindx, r_type);
3291 outrel.r_addend = relocation + rel->r_addend;
3295 sreloc = elf_section_data (input_section)->sreloc;
3297 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3299 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3301 /* If this reloc is against an external symbol, we do
3302 not want to fiddle with the addend. Otherwise, we
3303 need to include the symbol value so that it becomes
3304 an addend for the dynamic reloc. */
3305 if (! relocate)
3306 continue;
3309 break;
3311 case R_X86_64_TLSGD:
3312 case R_X86_64_GOTPC32_TLSDESC:
3313 case R_X86_64_TLSDESC_CALL:
3314 case R_X86_64_GOTTPOFF:
3315 tls_type = GOT_UNKNOWN;
3316 if (h == NULL && local_got_offsets)
3317 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3318 else if (h != NULL)
3319 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
3321 if (! elf64_x86_64_tls_transition (info, input_bfd,
3322 input_section, contents,
3323 symtab_hdr, sym_hashes,
3324 &r_type, tls_type, rel,
3325 relend, h, r_symndx))
3326 return FALSE;
3328 if (r_type == R_X86_64_TPOFF32)
3330 bfd_vma roff = rel->r_offset;
3332 BFD_ASSERT (! unresolved_reloc);
3334 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3336 /* GD->LE transition.
3337 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3338 .word 0x6666; rex64; call __tls_get_addr
3339 Change it into:
3340 movq %fs:0, %rax
3341 leaq foo@tpoff(%rax), %rax */
3342 memcpy (contents + roff - 4,
3343 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3344 16);
3345 bfd_put_32 (output_bfd,
3346 elf64_x86_64_tpoff (info, relocation),
3347 contents + roff + 8);
3348 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3349 rel++;
3350 continue;
3352 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3354 /* GDesc -> LE transition.
3355 It's originally something like:
3356 leaq x@tlsdesc(%rip), %rax
3358 Change it to:
3359 movl $x@tpoff, %rax. */
3361 unsigned int val, type;
3363 type = bfd_get_8 (input_bfd, contents + roff - 3);
3364 val = bfd_get_8 (input_bfd, contents + roff - 1);
3365 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3366 contents + roff - 3);
3367 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3368 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3369 contents + roff - 1);
3370 bfd_put_32 (output_bfd,
3371 elf64_x86_64_tpoff (info, relocation),
3372 contents + roff);
3373 continue;
3375 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3377 /* GDesc -> LE transition.
3378 It's originally:
3379 call *(%rax)
3380 Turn it into:
3381 xchg %ax,%ax. */
3382 bfd_put_8 (output_bfd, 0x66, contents + roff);
3383 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3384 continue;
3386 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3388 /* IE->LE transition:
3389 Originally it can be one of:
3390 movq foo@gottpoff(%rip), %reg
3391 addq foo@gottpoff(%rip), %reg
3392 We change it into:
3393 movq $foo, %reg
3394 leaq foo(%reg), %reg
3395 addq $foo, %reg. */
3397 unsigned int val, type, reg;
3399 val = bfd_get_8 (input_bfd, contents + roff - 3);
3400 type = bfd_get_8 (input_bfd, contents + roff - 2);
3401 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3402 reg >>= 3;
3403 if (type == 0x8b)
3405 /* movq */
3406 if (val == 0x4c)
3407 bfd_put_8 (output_bfd, 0x49,
3408 contents + roff - 3);
3409 bfd_put_8 (output_bfd, 0xc7,
3410 contents + roff - 2);
3411 bfd_put_8 (output_bfd, 0xc0 | reg,
3412 contents + roff - 1);
3414 else if (reg == 4)
3416 /* addq -> addq - addressing with %rsp/%r12 is
3417 special */
3418 if (val == 0x4c)
3419 bfd_put_8 (output_bfd, 0x49,
3420 contents + roff - 3);
3421 bfd_put_8 (output_bfd, 0x81,
3422 contents + roff - 2);
3423 bfd_put_8 (output_bfd, 0xc0 | reg,
3424 contents + roff - 1);
3426 else
3428 /* addq -> leaq */
3429 if (val == 0x4c)
3430 bfd_put_8 (output_bfd, 0x4d,
3431 contents + roff - 3);
3432 bfd_put_8 (output_bfd, 0x8d,
3433 contents + roff - 2);
3434 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3435 contents + roff - 1);
3437 bfd_put_32 (output_bfd,
3438 elf64_x86_64_tpoff (info, relocation),
3439 contents + roff);
3440 continue;
3442 else
3443 BFD_ASSERT (FALSE);
3446 if (htab->elf.sgot == NULL)
3447 abort ();
3449 if (h != NULL)
3451 off = h->got.offset;
3452 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
3454 else
3456 if (local_got_offsets == NULL)
3457 abort ();
3459 off = local_got_offsets[r_symndx];
3460 offplt = local_tlsdesc_gotents[r_symndx];
3463 if ((off & 1) != 0)
3464 off &= ~1;
3465 else
3467 Elf_Internal_Rela outrel;
3468 int dr_type, indx;
3469 asection *sreloc;
3471 if (htab->elf.srelgot == NULL)
3472 abort ();
3474 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3476 if (GOT_TLS_GDESC_P (tls_type))
3478 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
3479 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3480 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3481 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3482 + htab->elf.sgotplt->output_offset
3483 + offplt
3484 + htab->sgotplt_jump_table_size);
3485 sreloc = htab->elf.srelplt;
3486 if (indx == 0)
3487 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3488 else
3489 outrel.r_addend = 0;
3490 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3493 sreloc = htab->elf.srelgot;
3495 outrel.r_offset = (htab->elf.sgot->output_section->vma
3496 + htab->elf.sgot->output_offset + off);
3498 if (GOT_TLS_GD_P (tls_type))
3499 dr_type = R_X86_64_DTPMOD64;
3500 else if (GOT_TLS_GDESC_P (tls_type))
3501 goto dr_done;
3502 else
3503 dr_type = R_X86_64_TPOFF64;
3505 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3506 outrel.r_addend = 0;
3507 if ((dr_type == R_X86_64_TPOFF64
3508 || dr_type == R_X86_64_TLSDESC) && indx == 0)
3509 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3510 outrel.r_info = ELF64_R_INFO (indx, dr_type);
3512 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3514 if (GOT_TLS_GD_P (tls_type))
3516 if (indx == 0)
3518 BFD_ASSERT (! unresolved_reloc);
3519 bfd_put_64 (output_bfd,
3520 relocation - elf64_x86_64_dtpoff_base (info),
3521 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3523 else
3525 bfd_put_64 (output_bfd, 0,
3526 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3527 outrel.r_info = ELF64_R_INFO (indx,
3528 R_X86_64_DTPOFF64);
3529 outrel.r_offset += GOT_ENTRY_SIZE;
3530 elf64_x86_64_append_rela (output_bfd, sreloc,
3531 &outrel);
3535 dr_done:
3536 if (h != NULL)
3537 h->got.offset |= 1;
3538 else
3539 local_got_offsets[r_symndx] |= 1;
3542 if (off >= (bfd_vma) -2
3543 && ! GOT_TLS_GDESC_P (tls_type))
3544 abort ();
3545 if (r_type == ELF64_R_TYPE (rel->r_info))
3547 if (r_type == R_X86_64_GOTPC32_TLSDESC
3548 || r_type == R_X86_64_TLSDESC_CALL)
3549 relocation = htab->elf.sgotplt->output_section->vma
3550 + htab->elf.sgotplt->output_offset
3551 + offplt + htab->sgotplt_jump_table_size;
3552 else
3553 relocation = htab->elf.sgot->output_section->vma
3554 + htab->elf.sgot->output_offset + off;
3555 unresolved_reloc = FALSE;
3557 else
3559 bfd_vma roff = rel->r_offset;
3561 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3563 /* GD->IE transition.
3564 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3565 .word 0x6666; rex64; call __tls_get_addr@plt
3566 Change it into:
3567 movq %fs:0, %rax
3568 addq foo@gottpoff(%rip), %rax */
3569 memcpy (contents + roff - 4,
3570 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3571 16);
3573 relocation = (htab->elf.sgot->output_section->vma
3574 + htab->elf.sgot->output_offset + off
3575 - roff
3576 - input_section->output_section->vma
3577 - input_section->output_offset
3578 - 12);
3579 bfd_put_32 (output_bfd, relocation,
3580 contents + roff + 8);
3581 /* Skip R_X86_64_PLT32. */
3582 rel++;
3583 continue;
3585 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3587 /* GDesc -> IE transition.
3588 It's originally something like:
3589 leaq x@tlsdesc(%rip), %rax
3591 Change it to:
3592 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
3594 /* Now modify the instruction as appropriate. To
3595 turn a leaq into a movq in the form we use it, it
3596 suffices to change the second byte from 0x8d to
3597 0x8b. */
3598 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3600 bfd_put_32 (output_bfd,
3601 htab->elf.sgot->output_section->vma
3602 + htab->elf.sgot->output_offset + off
3603 - rel->r_offset
3604 - input_section->output_section->vma
3605 - input_section->output_offset
3606 - 4,
3607 contents + roff);
3608 continue;
3610 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3612 /* GDesc -> IE transition.
3613 It's originally:
3614 call *(%rax)
3616 Change it to:
3617 xchg %ax, %ax. */
3619 bfd_put_8 (output_bfd, 0x66, contents + roff);
3620 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3621 continue;
3623 else
3624 BFD_ASSERT (FALSE);
3626 break;
3628 case R_X86_64_TLSLD:
3629 if (! elf64_x86_64_tls_transition (info, input_bfd,
3630 input_section, contents,
3631 symtab_hdr, sym_hashes,
3632 &r_type, GOT_UNKNOWN,
3633 rel, relend, h, r_symndx))
3634 return FALSE;
3636 if (r_type != R_X86_64_TLSLD)
3638 /* LD->LE transition:
3639 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3640 We change it into:
3641 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3643 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3644 memcpy (contents + rel->r_offset - 3,
3645 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3646 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3647 rel++;
3648 continue;
3651 if (htab->elf.sgot == NULL)
3652 abort ();
3654 off = htab->tls_ld_got.offset;
3655 if (off & 1)
3656 off &= ~1;
3657 else
3659 Elf_Internal_Rela outrel;
3661 if (htab->elf.srelgot == NULL)
3662 abort ();
3664 outrel.r_offset = (htab->elf.sgot->output_section->vma
3665 + htab->elf.sgot->output_offset + off);
3667 bfd_put_64 (output_bfd, 0,
3668 htab->elf.sgot->contents + off);
3669 bfd_put_64 (output_bfd, 0,
3670 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3671 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3672 outrel.r_addend = 0;
3673 elf64_x86_64_append_rela (output_bfd, htab->elf.srelgot,
3674 &outrel);
3675 htab->tls_ld_got.offset |= 1;
3677 relocation = htab->elf.sgot->output_section->vma
3678 + htab->elf.sgot->output_offset + off;
3679 unresolved_reloc = FALSE;
3680 break;
3682 case R_X86_64_DTPOFF32:
3683 if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
3684 relocation -= elf64_x86_64_dtpoff_base (info);
3685 else
3686 relocation = elf64_x86_64_tpoff (info, relocation);
3687 break;
3689 case R_X86_64_TPOFF32:
3690 BFD_ASSERT (info->executable);
3691 relocation = elf64_x86_64_tpoff (info, relocation);
3692 break;
3694 default:
3695 break;
3698 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3699 because such sections are not SEC_ALLOC and thus ld.so will
3700 not process them. */
3701 if (unresolved_reloc
3702 && !((input_section->flags & SEC_DEBUGGING) != 0
3703 && h->def_dynamic))
3704 (*_bfd_error_handler)
3705 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3706 input_bfd,
3707 input_section,
3708 (long) rel->r_offset,
3709 howto->name,
3710 h->root.root.string);
3712 do_relocation:
3713 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3714 contents, rel->r_offset,
3715 relocation, rel->r_addend);
3717 if (r != bfd_reloc_ok)
3719 const char *name;
3721 if (h != NULL)
3722 name = h->root.root.string;
3723 else
3725 name = bfd_elf_string_from_elf_section (input_bfd,
3726 symtab_hdr->sh_link,
3727 sym->st_name);
3728 if (name == NULL)
3729 return FALSE;
3730 if (*name == '\0')
3731 name = bfd_section_name (input_bfd, sec);
3734 if (r == bfd_reloc_overflow)
3736 if (! ((*info->callbacks->reloc_overflow)
3737 (info, (h ? &h->root : NULL), name, howto->name,
3738 (bfd_vma) 0, input_bfd, input_section,
3739 rel->r_offset)))
3740 return FALSE;
3742 else
3744 (*_bfd_error_handler)
3745 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3746 input_bfd, input_section,
3747 (long) rel->r_offset, name, (int) r);
3748 return FALSE;
3753 return TRUE;
3756 /* Finish up dynamic symbol handling. We set the contents of various
3757 dynamic sections here. */
3759 static bfd_boolean
3760 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3761 struct bfd_link_info *info,
3762 struct elf_link_hash_entry *h,
3763 Elf_Internal_Sym *sym)
3765 struct elf64_x86_64_link_hash_table *htab;
3767 htab = elf64_x86_64_hash_table (info);
3768 if (htab == NULL)
3769 return FALSE;
3771 if (h->plt.offset != (bfd_vma) -1)
3773 bfd_vma plt_index;
3774 bfd_vma got_offset;
3775 Elf_Internal_Rela rela;
3776 bfd_byte *loc;
3777 asection *plt, *gotplt, *relplt;
3779 /* When building a static executable, use .iplt, .igot.plt and
3780 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3781 if (htab->elf.splt != NULL)
3783 plt = htab->elf.splt;
3784 gotplt = htab->elf.sgotplt;
3785 relplt = htab->elf.srelplt;
3787 else
3789 plt = htab->elf.iplt;
3790 gotplt = htab->elf.igotplt;
3791 relplt = htab->elf.irelplt;
3794 /* This symbol has an entry in the procedure linkage table. Set
3795 it up. */
3796 if ((h->dynindx == -1
3797 && !((h->forced_local || info->executable)
3798 && h->def_regular
3799 && h->type == STT_GNU_IFUNC))
3800 || plt == NULL
3801 || gotplt == NULL
3802 || relplt == NULL)
3803 abort ();
3805 /* Get the index in the procedure linkage table which
3806 corresponds to this symbol. This is the index of this symbol
3807 in all the symbols for which we are making plt entries. The
3808 first entry in the procedure linkage table is reserved.
3810 Get the offset into the .got table of the entry that
3811 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3812 bytes. The first three are reserved for the dynamic linker.
3814 For static executables, we don't reserve anything. */
3816 if (plt == htab->elf.splt)
3818 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3819 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3821 else
3823 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3824 got_offset = plt_index * GOT_ENTRY_SIZE;
3827 /* Fill in the entry in the procedure linkage table. */
3828 memcpy (plt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3829 PLT_ENTRY_SIZE);
3831 /* Insert the relocation positions of the plt section. The magic
3832 numbers at the end of the statements are the positions of the
3833 relocations in the plt section. */
3834 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3835 instruction uses 6 bytes, subtract this value. */
3836 bfd_put_32 (output_bfd,
3837 (gotplt->output_section->vma
3838 + gotplt->output_offset
3839 + got_offset
3840 - plt->output_section->vma
3841 - plt->output_offset
3842 - h->plt.offset
3843 - 6),
3844 plt->contents + h->plt.offset + 2);
3846 /* Don't fill PLT entry for static executables. */
3847 if (plt == htab->elf.splt)
3849 /* Put relocation index. */
3850 bfd_put_32 (output_bfd, plt_index,
3851 plt->contents + h->plt.offset + 7);
3852 /* Put offset for jmp .PLT0. */
3853 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3854 plt->contents + h->plt.offset + 12);
3857 /* Fill in the entry in the global offset table, initially this
3858 points to the pushq instruction in the PLT which is at offset 6. */
3859 bfd_put_64 (output_bfd, (plt->output_section->vma
3860 + plt->output_offset
3861 + h->plt.offset + 6),
3862 gotplt->contents + got_offset);
3864 /* Fill in the entry in the .rela.plt section. */
3865 rela.r_offset = (gotplt->output_section->vma
3866 + gotplt->output_offset
3867 + got_offset);
3868 if (h->dynindx == -1
3869 || ((info->executable
3870 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3871 && h->def_regular
3872 && h->type == STT_GNU_IFUNC))
3874 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3875 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
3876 rela.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
3877 rela.r_addend = (h->root.u.def.value
3878 + h->root.u.def.section->output_section->vma
3879 + h->root.u.def.section->output_offset);
3881 else
3883 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3884 rela.r_addend = 0;
3886 loc = relplt->contents + plt_index * sizeof (Elf64_External_Rela);
3887 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3889 if (!h->def_regular)
3891 /* Mark the symbol as undefined, rather than as defined in
3892 the .plt section. Leave the value if there were any
3893 relocations where pointer equality matters (this is a clue
3894 for the dynamic linker, to make function pointer
3895 comparisons work between an application and shared
3896 library), otherwise set it to zero. If a function is only
3897 called from a binary, there is no need to slow down
3898 shared libraries because of that. */
3899 sym->st_shndx = SHN_UNDEF;
3900 if (!h->pointer_equality_needed)
3901 sym->st_value = 0;
3905 if (h->got.offset != (bfd_vma) -1
3906 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3907 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3909 Elf_Internal_Rela rela;
3911 /* This symbol has an entry in the global offset table. Set it
3912 up. */
3913 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3914 abort ();
3916 rela.r_offset = (htab->elf.sgot->output_section->vma
3917 + htab->elf.sgot->output_offset
3918 + (h->got.offset &~ (bfd_vma) 1));
3920 /* If this is a static link, or it is a -Bsymbolic link and the
3921 symbol is defined locally or was forced to be local because
3922 of a version file, we just want to emit a RELATIVE reloc.
3923 The entry in the global offset table will already have been
3924 initialized in the relocate_section function. */
3925 if (h->def_regular
3926 && h->type == STT_GNU_IFUNC)
3928 if (info->shared)
3930 /* Generate R_X86_64_GLOB_DAT. */
3931 goto do_glob_dat;
3933 else
3935 asection *plt;
3937 if (!h->pointer_equality_needed)
3938 abort ();
3940 /* For non-shared object, we can't use .got.plt, which
3941 contains the real function addres if we need pointer
3942 equality. We load the GOT entry with the PLT entry. */
3943 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3944 bfd_put_64 (output_bfd, (plt->output_section->vma
3945 + plt->output_offset
3946 + h->plt.offset),
3947 htab->elf.sgot->contents + h->got.offset);
3948 return TRUE;
3951 else if (info->shared
3952 && SYMBOL_REFERENCES_LOCAL (info, h))
3954 if (!h->def_regular)
3955 return FALSE;
3956 BFD_ASSERT((h->got.offset & 1) != 0);
3957 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3958 rela.r_addend = (h->root.u.def.value
3959 + h->root.u.def.section->output_section->vma
3960 + h->root.u.def.section->output_offset);
3962 else
3964 BFD_ASSERT((h->got.offset & 1) == 0);
3965 do_glob_dat:
3966 bfd_put_64 (output_bfd, (bfd_vma) 0,
3967 htab->elf.sgot->contents + h->got.offset);
3968 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3969 rela.r_addend = 0;
3972 elf64_x86_64_append_rela (output_bfd, htab->elf.srelgot, &rela);
3975 if (h->needs_copy)
3977 Elf_Internal_Rela rela;
3979 /* This symbol needs a copy reloc. Set it up. */
3981 if (h->dynindx == -1
3982 || (h->root.type != bfd_link_hash_defined
3983 && h->root.type != bfd_link_hash_defweak)
3984 || htab->srelbss == NULL)
3985 abort ();
3987 rela.r_offset = (h->root.u.def.value
3988 + h->root.u.def.section->output_section->vma
3989 + h->root.u.def.section->output_offset);
3990 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3991 rela.r_addend = 0;
3992 elf64_x86_64_append_rela (output_bfd, htab->srelbss, &rela);
3995 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
3996 be NULL for local symbols. */
3997 if (sym != NULL
3998 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
3999 || h == htab->elf.hgot))
4000 sym->st_shndx = SHN_ABS;
4002 return TRUE;
4005 /* Finish up local dynamic symbol handling. We set the contents of
4006 various dynamic sections here. */
4008 static bfd_boolean
4009 elf64_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
4011 struct elf_link_hash_entry *h
4012 = (struct elf_link_hash_entry *) *slot;
4013 struct bfd_link_info *info
4014 = (struct bfd_link_info *) inf;
4016 return elf64_x86_64_finish_dynamic_symbol (info->output_bfd,
4017 info, h, NULL);
4020 /* Used to decide how to sort relocs in an optimal manner for the
4021 dynamic linker, before writing them out. */
4023 static enum elf_reloc_type_class
4024 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
4026 switch ((int) ELF64_R_TYPE (rela->r_info))
4028 case R_X86_64_RELATIVE:
4029 return reloc_class_relative;
4030 case R_X86_64_JUMP_SLOT:
4031 return reloc_class_plt;
4032 case R_X86_64_COPY:
4033 return reloc_class_copy;
4034 default:
4035 return reloc_class_normal;
4039 /* Finish up the dynamic sections. */
4041 static bfd_boolean
4042 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
4044 struct elf64_x86_64_link_hash_table *htab;
4045 bfd *dynobj;
4046 asection *sdyn;
4048 htab = elf64_x86_64_hash_table (info);
4049 if (htab == NULL)
4050 return FALSE;
4052 dynobj = htab->elf.dynobj;
4053 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4055 if (htab->elf.dynamic_sections_created)
4057 Elf64_External_Dyn *dyncon, *dynconend;
4059 if (sdyn == NULL || htab->elf.sgot == NULL)
4060 abort ();
4062 dyncon = (Elf64_External_Dyn *) sdyn->contents;
4063 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
4064 for (; dyncon < dynconend; dyncon++)
4066 Elf_Internal_Dyn dyn;
4067 asection *s;
4069 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4071 switch (dyn.d_tag)
4073 default:
4074 continue;
4076 case DT_PLTGOT:
4077 s = htab->elf.sgotplt;
4078 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4079 break;
4081 case DT_JMPREL:
4082 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
4083 break;
4085 case DT_PLTRELSZ:
4086 s = htab->elf.srelplt->output_section;
4087 dyn.d_un.d_val = s->size;
4088 break;
4090 case DT_RELASZ:
4091 /* The procedure linkage table relocs (DT_JMPREL) should
4092 not be included in the overall relocs (DT_RELA).
4093 Therefore, we override the DT_RELASZ entry here to
4094 make it not include the JMPREL relocs. Since the
4095 linker script arranges for .rela.plt to follow all
4096 other relocation sections, we don't have to worry
4097 about changing the DT_RELA entry. */
4098 if (htab->elf.srelplt != NULL)
4100 s = htab->elf.srelplt->output_section;
4101 dyn.d_un.d_val -= s->size;
4103 break;
4105 case DT_TLSDESC_PLT:
4106 s = htab->elf.splt;
4107 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4108 + htab->tlsdesc_plt;
4109 break;
4111 case DT_TLSDESC_GOT:
4112 s = htab->elf.sgot;
4113 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4114 + htab->tlsdesc_got;
4115 break;
4118 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4121 /* Fill in the special first entry in the procedure linkage table. */
4122 if (htab->elf.splt && htab->elf.splt->size > 0)
4124 /* Fill in the first entry in the procedure linkage table. */
4125 memcpy (htab->elf.splt->contents, elf64_x86_64_plt0_entry,
4126 PLT_ENTRY_SIZE);
4127 /* Add offset for pushq GOT+8(%rip), since the instruction
4128 uses 6 bytes subtract this value. */
4129 bfd_put_32 (output_bfd,
4130 (htab->elf.sgotplt->output_section->vma
4131 + htab->elf.sgotplt->output_offset
4133 - htab->elf.splt->output_section->vma
4134 - htab->elf.splt->output_offset
4135 - 6),
4136 htab->elf.splt->contents + 2);
4137 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4138 the end of the instruction. */
4139 bfd_put_32 (output_bfd,
4140 (htab->elf.sgotplt->output_section->vma
4141 + htab->elf.sgotplt->output_offset
4142 + 16
4143 - htab->elf.splt->output_section->vma
4144 - htab->elf.splt->output_offset
4145 - 12),
4146 htab->elf.splt->contents + 8);
4148 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize =
4149 PLT_ENTRY_SIZE;
4151 if (htab->tlsdesc_plt)
4153 bfd_put_64 (output_bfd, (bfd_vma) 0,
4154 htab->elf.sgot->contents + htab->tlsdesc_got);
4156 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
4157 elf64_x86_64_plt0_entry,
4158 PLT_ENTRY_SIZE);
4160 /* Add offset for pushq GOT+8(%rip), since the
4161 instruction uses 6 bytes subtract this value. */
4162 bfd_put_32 (output_bfd,
4163 (htab->elf.sgotplt->output_section->vma
4164 + htab->elf.sgotplt->output_offset
4166 - htab->elf.splt->output_section->vma
4167 - htab->elf.splt->output_offset
4168 - htab->tlsdesc_plt
4169 - 6),
4170 htab->elf.splt->contents + htab->tlsdesc_plt + 2);
4171 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4172 htab->tlsdesc_got. The 12 is the offset to the end of
4173 the instruction. */
4174 bfd_put_32 (output_bfd,
4175 (htab->elf.sgot->output_section->vma
4176 + htab->elf.sgot->output_offset
4177 + htab->tlsdesc_got
4178 - htab->elf.splt->output_section->vma
4179 - htab->elf.splt->output_offset
4180 - htab->tlsdesc_plt
4181 - 12),
4182 htab->elf.splt->contents + htab->tlsdesc_plt + 8);
4187 if (htab->elf.sgotplt)
4189 /* Fill in the first three entries in the global offset table. */
4190 if (htab->elf.sgotplt->size > 0)
4192 /* Set the first entry in the global offset table to the address of
4193 the dynamic section. */
4194 if (sdyn == NULL)
4195 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4196 else
4197 bfd_put_64 (output_bfd,
4198 sdyn->output_section->vma + sdyn->output_offset,
4199 htab->elf.sgotplt->contents);
4200 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4201 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4202 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4205 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
4206 GOT_ENTRY_SIZE;
4209 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4210 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
4211 = GOT_ENTRY_SIZE;
4213 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4214 htab_traverse (htab->loc_hash_table,
4215 elf64_x86_64_finish_local_dynamic_symbol,
4216 info);
4218 return TRUE;
4221 /* Return address for Ith PLT stub in section PLT, for relocation REL
4222 or (bfd_vma) -1 if it should not be included. */
4224 static bfd_vma
4225 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4226 const arelent *rel ATTRIBUTE_UNUSED)
4228 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4231 /* Handle an x86-64 specific section when reading an object file. This
4232 is called when elfcode.h finds a section with an unknown type. */
4234 static bfd_boolean
4235 elf64_x86_64_section_from_shdr (bfd *abfd,
4236 Elf_Internal_Shdr *hdr,
4237 const char *name,
4238 int shindex)
4240 if (hdr->sh_type != SHT_X86_64_UNWIND)
4241 return FALSE;
4243 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4244 return FALSE;
4246 return TRUE;
4249 /* Hook called by the linker routine which adds symbols from an object
4250 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4251 of .bss. */
4253 static bfd_boolean
4254 elf64_x86_64_add_symbol_hook (bfd *abfd,
4255 struct bfd_link_info *info,
4256 Elf_Internal_Sym *sym,
4257 const char **namep ATTRIBUTE_UNUSED,
4258 flagword *flagsp ATTRIBUTE_UNUSED,
4259 asection **secp,
4260 bfd_vma *valp)
4262 asection *lcomm;
4264 switch (sym->st_shndx)
4266 case SHN_X86_64_LCOMMON:
4267 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4268 if (lcomm == NULL)
4270 lcomm = bfd_make_section_with_flags (abfd,
4271 "LARGE_COMMON",
4272 (SEC_ALLOC
4273 | SEC_IS_COMMON
4274 | SEC_LINKER_CREATED));
4275 if (lcomm == NULL)
4276 return FALSE;
4277 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4279 *secp = lcomm;
4280 *valp = sym->st_size;
4281 return TRUE;
4284 if ((abfd->flags & DYNAMIC) == 0
4285 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4286 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4288 return TRUE;
4292 /* Given a BFD section, try to locate the corresponding ELF section
4293 index. */
4295 static bfd_boolean
4296 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4297 asection *sec, int *index_return)
4299 if (sec == &_bfd_elf_large_com_section)
4301 *index_return = SHN_X86_64_LCOMMON;
4302 return TRUE;
4304 return FALSE;
4307 /* Process a symbol. */
4309 static void
4310 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4311 asymbol *asym)
4313 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4315 switch (elfsym->internal_elf_sym.st_shndx)
4317 case SHN_X86_64_LCOMMON:
4318 asym->section = &_bfd_elf_large_com_section;
4319 asym->value = elfsym->internal_elf_sym.st_size;
4320 /* Common symbol doesn't set BSF_GLOBAL. */
4321 asym->flags &= ~BSF_GLOBAL;
4322 break;
4326 static bfd_boolean
4327 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
4329 return (sym->st_shndx == SHN_COMMON
4330 || sym->st_shndx == SHN_X86_64_LCOMMON);
4333 static unsigned int
4334 elf64_x86_64_common_section_index (asection *sec)
4336 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4337 return SHN_COMMON;
4338 else
4339 return SHN_X86_64_LCOMMON;
4342 static asection *
4343 elf64_x86_64_common_section (asection *sec)
4345 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4346 return bfd_com_section_ptr;
4347 else
4348 return &_bfd_elf_large_com_section;
4351 static bfd_boolean
4352 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4353 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4354 struct elf_link_hash_entry *h,
4355 Elf_Internal_Sym *sym,
4356 asection **psec,
4357 bfd_vma *pvalue ATTRIBUTE_UNUSED,
4358 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4359 bfd_boolean *skip ATTRIBUTE_UNUSED,
4360 bfd_boolean *override ATTRIBUTE_UNUSED,
4361 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4362 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4363 bfd_boolean *newdef ATTRIBUTE_UNUSED,
4364 bfd_boolean *newdyn,
4365 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4366 bfd_boolean *newweak ATTRIBUTE_UNUSED,
4367 bfd *abfd ATTRIBUTE_UNUSED,
4368 asection **sec,
4369 bfd_boolean *olddef ATTRIBUTE_UNUSED,
4370 bfd_boolean *olddyn,
4371 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4372 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4373 bfd *oldbfd,
4374 asection **oldsec)
4376 /* A normal common symbol and a large common symbol result in a
4377 normal common symbol. We turn the large common symbol into a
4378 normal one. */
4379 if (!*olddyn
4380 && h->root.type == bfd_link_hash_common
4381 && !*newdyn
4382 && bfd_is_com_section (*sec)
4383 && *oldsec != *sec)
4385 if (sym->st_shndx == SHN_COMMON
4386 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4388 h->root.u.c.p->section
4389 = bfd_make_section_old_way (oldbfd, "COMMON");
4390 h->root.u.c.p->section->flags = SEC_ALLOC;
4392 else if (sym->st_shndx == SHN_X86_64_LCOMMON
4393 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4394 *psec = *sec = bfd_com_section_ptr;
4397 return TRUE;
4400 static int
4401 elf64_x86_64_additional_program_headers (bfd *abfd,
4402 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4404 asection *s;
4405 int count = 0;
4407 /* Check to see if we need a large readonly segment. */
4408 s = bfd_get_section_by_name (abfd, ".lrodata");
4409 if (s && (s->flags & SEC_LOAD))
4410 count++;
4412 /* Check to see if we need a large data segment. Since .lbss sections
4413 is placed right after the .bss section, there should be no need for
4414 a large data segment just because of .lbss. */
4415 s = bfd_get_section_by_name (abfd, ".ldata");
4416 if (s && (s->flags & SEC_LOAD))
4417 count++;
4419 return count;
4422 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4424 static bfd_boolean
4425 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4427 if (h->plt.offset != (bfd_vma) -1
4428 && !h->def_regular
4429 && !h->pointer_equality_needed)
4430 return FALSE;
4432 return _bfd_elf_hash_symbol (h);
4435 static const struct bfd_elf_special_section
4436 elf64_x86_64_special_sections[]=
4438 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4439 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4440 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4441 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4442 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4443 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4444 { NULL, 0, 0, 0, 0 }
4447 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
4448 #define TARGET_LITTLE_NAME "elf64-x86-64"
4449 #define ELF_ARCH bfd_arch_i386
4450 #define ELF_MACHINE_CODE EM_X86_64
4451 #define ELF_MAXPAGESIZE 0x200000
4452 #define ELF_MINPAGESIZE 0x1000
4453 #define ELF_COMMONPAGESIZE 0x1000
4455 #define elf_backend_can_gc_sections 1
4456 #define elf_backend_can_refcount 1
4457 #define elf_backend_want_got_plt 1
4458 #define elf_backend_plt_readonly 1
4459 #define elf_backend_want_plt_sym 0
4460 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
4461 #define elf_backend_rela_normal 1
4463 #define elf_info_to_howto elf64_x86_64_info_to_howto
4465 #define bfd_elf64_bfd_link_hash_table_create \
4466 elf64_x86_64_link_hash_table_create
4467 #define bfd_elf64_bfd_link_hash_table_free \
4468 elf64_x86_64_link_hash_table_free
4469 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
4470 #define bfd_elf64_bfd_reloc_name_lookup \
4471 elf64_x86_64_reloc_name_lookup
4473 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
4474 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4475 #define elf_backend_check_relocs elf64_x86_64_check_relocs
4476 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
4477 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
4478 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
4479 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
4480 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
4481 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
4482 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
4483 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
4484 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
4485 #define elf_backend_relocate_section elf64_x86_64_relocate_section
4486 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
4487 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
4488 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4489 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
4490 #define elf_backend_object_p elf64_x86_64_elf_object_p
4491 #define bfd_elf64_mkobject elf64_x86_64_mkobject
4493 #define elf_backend_section_from_shdr \
4494 elf64_x86_64_section_from_shdr
4496 #define elf_backend_section_from_bfd_section \
4497 elf64_x86_64_elf_section_from_bfd_section
4498 #define elf_backend_add_symbol_hook \
4499 elf64_x86_64_add_symbol_hook
4500 #define elf_backend_symbol_processing \
4501 elf64_x86_64_symbol_processing
4502 #define elf_backend_common_section_index \
4503 elf64_x86_64_common_section_index
4504 #define elf_backend_common_section \
4505 elf64_x86_64_common_section
4506 #define elf_backend_common_definition \
4507 elf64_x86_64_common_definition
4508 #define elf_backend_merge_symbol \
4509 elf64_x86_64_merge_symbol
4510 #define elf_backend_special_sections \
4511 elf64_x86_64_special_sections
4512 #define elf_backend_additional_program_headers \
4513 elf64_x86_64_additional_program_headers
4514 #define elf_backend_hash_symbol \
4515 elf64_x86_64_hash_symbol
4517 #undef elf_backend_post_process_headers
4518 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4520 #include "elf64-target.h"
4522 /* FreeBSD support. */
4524 #undef TARGET_LITTLE_SYM
4525 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
4526 #undef TARGET_LITTLE_NAME
4527 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
4529 #undef ELF_OSABI
4530 #define ELF_OSABI ELFOSABI_FREEBSD
4532 #undef elf64_bed
4533 #define elf64_bed elf64_x86_64_fbsd_bed
4535 #include "elf64-target.h"
4537 /* Solaris 2 support. */
4539 #undef TARGET_LITTLE_SYM
4540 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_sol2_vec
4541 #undef TARGET_LITTLE_NAME
4542 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
4544 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4545 objects won't be recognized. */
4546 #undef ELF_OSABI
4548 #undef elf64_bed
4549 #define elf64_bed elf64_x86_64_sol2_bed
4551 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4553 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4554 File, p.63. */
4555 #undef elf_backend_want_plt_sym
4556 #define elf_backend_want_plt_sym 1
4558 #include "elf64-target.h"
4560 /* Intel L1OM support. */
4562 static bfd_boolean
4563 elf64_l1om_elf_object_p (bfd *abfd)
4565 /* Set the right machine number for an L1OM elf64 file. */
4566 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
4567 return TRUE;
4570 #undef TARGET_LITTLE_SYM
4571 #define TARGET_LITTLE_SYM bfd_elf64_l1om_vec
4572 #undef TARGET_LITTLE_NAME
4573 #define TARGET_LITTLE_NAME "elf64-l1om"
4574 #undef ELF_ARCH
4575 #define ELF_ARCH bfd_arch_l1om
4577 #undef ELF_MACHINE_CODE
4578 #define ELF_MACHINE_CODE EM_L1OM
4580 #undef ELF_OSABI
4582 #undef elf64_bed
4583 #define elf64_bed elf64_l1om_bed
4585 #undef elf_backend_object_p
4586 #define elf_backend_object_p elf64_l1om_elf_object_p
4588 #undef elf_backend_post_process_headers
4590 #include "elf64-target.h"
4592 /* FreeBSD L1OM support. */
4594 #undef TARGET_LITTLE_SYM
4595 #define TARGET_LITTLE_SYM bfd_elf64_l1om_freebsd_vec
4596 #undef TARGET_LITTLE_NAME
4597 #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
4599 #undef ELF_OSABI
4600 #define ELF_OSABI ELFOSABI_FREEBSD
4602 #undef elf64_bed
4603 #define elf64_bed elf64_l1om_fbsd_bed
4605 #undef elf_backend_post_process_headers
4606 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4608 #include "elf64-target.h"