PR gas/11507
[binutils.git] / bfd / elf64-x86-64.c
blob3a24ccae578f2cb9f609f787d6b434c7a5a3ff93
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 ret->elf.plt.offset = (bfd_vma) -1;
607 ret->elf.got.offset = (bfd_vma) -1;
608 *slot = ret;
610 return &ret->elf;
613 /* Create an X86-64 ELF linker hash table. */
615 static struct bfd_link_hash_table *
616 elf64_x86_64_link_hash_table_create (bfd *abfd)
618 struct elf64_x86_64_link_hash_table *ret;
619 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
621 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
622 if (ret == NULL)
623 return NULL;
625 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
626 elf64_x86_64_link_hash_newfunc,
627 sizeof (struct elf64_x86_64_link_hash_entry),
628 X86_64_ELF_DATA))
630 free (ret);
631 return NULL;
634 ret->sdynbss = NULL;
635 ret->srelbss = NULL;
636 ret->sym_cache.abfd = NULL;
637 ret->tlsdesc_plt = 0;
638 ret->tlsdesc_got = 0;
639 ret->tls_ld_got.refcount = 0;
640 ret->sgotplt_jump_table_size = 0;
641 ret->tls_module_base = NULL;
643 ret->loc_hash_table = htab_try_create (1024,
644 elf64_x86_64_local_htab_hash,
645 elf64_x86_64_local_htab_eq,
646 NULL);
647 ret->loc_hash_memory = objalloc_create ();
648 if (!ret->loc_hash_table || !ret->loc_hash_memory)
650 free (ret);
651 return NULL;
654 return &ret->elf.root;
657 /* Destroy an X86-64 ELF linker hash table. */
659 static void
660 elf64_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
662 struct elf64_x86_64_link_hash_table *htab
663 = (struct elf64_x86_64_link_hash_table *) hash;
665 if (htab->loc_hash_table)
666 htab_delete (htab->loc_hash_table);
667 if (htab->loc_hash_memory)
668 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
669 _bfd_generic_link_hash_table_free (hash);
672 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
673 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
674 hash table. */
676 static bfd_boolean
677 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
679 struct elf64_x86_64_link_hash_table *htab;
681 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
682 return FALSE;
684 htab = elf64_x86_64_hash_table (info);
685 if (htab == NULL)
686 return FALSE;
688 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
689 if (!info->shared)
690 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
692 if (!htab->sdynbss
693 || (!info->shared && !htab->srelbss))
694 abort ();
696 return TRUE;
699 /* Copy the extra info we tack onto an elf_link_hash_entry. */
701 static void
702 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
703 struct elf_link_hash_entry *dir,
704 struct elf_link_hash_entry *ind)
706 struct elf64_x86_64_link_hash_entry *edir, *eind;
708 edir = (struct elf64_x86_64_link_hash_entry *) dir;
709 eind = (struct elf64_x86_64_link_hash_entry *) ind;
711 if (eind->dyn_relocs != NULL)
713 if (edir->dyn_relocs != NULL)
715 struct elf_dyn_relocs **pp;
716 struct elf_dyn_relocs *p;
718 /* Add reloc counts against the indirect sym to the direct sym
719 list. Merge any entries against the same section. */
720 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
722 struct elf_dyn_relocs *q;
724 for (q = edir->dyn_relocs; q != NULL; q = q->next)
725 if (q->sec == p->sec)
727 q->pc_count += p->pc_count;
728 q->count += p->count;
729 *pp = p->next;
730 break;
732 if (q == NULL)
733 pp = &p->next;
735 *pp = edir->dyn_relocs;
738 edir->dyn_relocs = eind->dyn_relocs;
739 eind->dyn_relocs = NULL;
742 if (ind->root.type == bfd_link_hash_indirect
743 && dir->got.refcount <= 0)
745 edir->tls_type = eind->tls_type;
746 eind->tls_type = GOT_UNKNOWN;
749 if (ELIMINATE_COPY_RELOCS
750 && ind->root.type != bfd_link_hash_indirect
751 && dir->dynamic_adjusted)
753 /* If called to transfer flags for a weakdef during processing
754 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
755 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
756 dir->ref_dynamic |= ind->ref_dynamic;
757 dir->ref_regular |= ind->ref_regular;
758 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
759 dir->needs_plt |= ind->needs_plt;
760 dir->pointer_equality_needed |= ind->pointer_equality_needed;
762 else
763 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
766 static bfd_boolean
767 elf64_x86_64_elf_object_p (bfd *abfd)
769 /* Set the right machine number for an x86-64 elf64 file. */
770 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
771 return TRUE;
774 typedef union
776 unsigned char c[2];
777 uint16_t i;
779 x86_64_opcode16;
781 typedef union
783 unsigned char c[4];
784 uint32_t i;
786 x86_64_opcode32;
788 /* Return TRUE if the TLS access code sequence support transition
789 from R_TYPE. */
791 static bfd_boolean
792 elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec,
793 bfd_byte *contents,
794 Elf_Internal_Shdr *symtab_hdr,
795 struct elf_link_hash_entry **sym_hashes,
796 unsigned int r_type,
797 const Elf_Internal_Rela *rel,
798 const Elf_Internal_Rela *relend)
800 unsigned int val;
801 unsigned long r_symndx;
802 struct elf_link_hash_entry *h;
803 bfd_vma offset;
805 /* Get the section contents. */
806 if (contents == NULL)
808 if (elf_section_data (sec)->this_hdr.contents != NULL)
809 contents = elf_section_data (sec)->this_hdr.contents;
810 else
812 /* FIXME: How to better handle error condition? */
813 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
814 return FALSE;
816 /* Cache the section contents for elf_link_input_bfd. */
817 elf_section_data (sec)->this_hdr.contents = contents;
821 offset = rel->r_offset;
822 switch (r_type)
824 case R_X86_64_TLSGD:
825 case R_X86_64_TLSLD:
826 if ((rel + 1) >= relend)
827 return FALSE;
829 if (r_type == R_X86_64_TLSGD)
831 /* Check transition from GD access model. Only
832 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
833 .word 0x6666; rex64; call __tls_get_addr
834 can transit to different access model. */
836 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } },
837 call = { { 0x66, 0x66, 0x48, 0xe8 } };
838 if (offset < 4
839 || (offset + 12) > sec->size
840 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i
841 || bfd_get_32 (abfd, contents + offset + 4) != call.i)
842 return FALSE;
844 else
846 /* Check transition from LD access model. Only
847 leaq foo@tlsld(%rip), %rdi;
848 call __tls_get_addr
849 can transit to different access model. */
851 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
852 x86_64_opcode32 op;
854 if (offset < 3 || (offset + 9) > sec->size)
855 return FALSE;
857 op.i = bfd_get_32 (abfd, contents + offset - 3);
858 op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
859 if (op.i != ld.i)
860 return FALSE;
863 r_symndx = ELF64_R_SYM (rel[1].r_info);
864 if (r_symndx < symtab_hdr->sh_info)
865 return FALSE;
867 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
868 /* Use strncmp to check __tls_get_addr since __tls_get_addr
869 may be versioned. */
870 return (h != NULL
871 && h->root.root.string != NULL
872 && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
873 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
874 && (strncmp (h->root.root.string,
875 "__tls_get_addr", 14) == 0));
877 case R_X86_64_GOTTPOFF:
878 /* Check transition from IE access model:
879 movq foo@gottpoff(%rip), %reg
880 addq foo@gottpoff(%rip), %reg
883 if (offset < 3 || (offset + 4) > sec->size)
884 return FALSE;
886 val = bfd_get_8 (abfd, contents + offset - 3);
887 if (val != 0x48 && val != 0x4c)
888 return FALSE;
890 val = bfd_get_8 (abfd, contents + offset - 2);
891 if (val != 0x8b && val != 0x03)
892 return FALSE;
894 val = bfd_get_8 (abfd, contents + offset - 1);
895 return (val & 0xc7) == 5;
897 case R_X86_64_GOTPC32_TLSDESC:
898 /* Check transition from GDesc access model:
899 leaq x@tlsdesc(%rip), %rax
901 Make sure it's a leaq adding rip to a 32-bit offset
902 into any register, although it's probably almost always
903 going to be rax. */
905 if (offset < 3 || (offset + 4) > sec->size)
906 return FALSE;
908 val = bfd_get_8 (abfd, contents + offset - 3);
909 if ((val & 0xfb) != 0x48)
910 return FALSE;
912 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
913 return FALSE;
915 val = bfd_get_8 (abfd, contents + offset - 1);
916 return (val & 0xc7) == 0x05;
918 case R_X86_64_TLSDESC_CALL:
919 /* Check transition from GDesc access model:
920 call *x@tlsdesc(%rax)
922 if (offset + 2 <= sec->size)
924 /* Make sure that it's a call *x@tlsdesc(%rax). */
925 static x86_64_opcode16 call = { { 0xff, 0x10 } };
926 return bfd_get_16 (abfd, contents + offset) == call.i;
929 return FALSE;
931 default:
932 abort ();
936 /* Return TRUE if the TLS access transition is OK or no transition
937 will be performed. Update R_TYPE if there is a transition. */
939 static bfd_boolean
940 elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
941 asection *sec, bfd_byte *contents,
942 Elf_Internal_Shdr *symtab_hdr,
943 struct elf_link_hash_entry **sym_hashes,
944 unsigned int *r_type, int tls_type,
945 const Elf_Internal_Rela *rel,
946 const Elf_Internal_Rela *relend,
947 struct elf_link_hash_entry *h,
948 unsigned long r_symndx)
950 unsigned int from_type = *r_type;
951 unsigned int to_type = from_type;
952 bfd_boolean check = TRUE;
954 switch (from_type)
956 case R_X86_64_TLSGD:
957 case R_X86_64_GOTPC32_TLSDESC:
958 case R_X86_64_TLSDESC_CALL:
959 case R_X86_64_GOTTPOFF:
960 if (info->executable)
962 if (h == NULL)
963 to_type = R_X86_64_TPOFF32;
964 else
965 to_type = R_X86_64_GOTTPOFF;
968 /* When we are called from elf64_x86_64_relocate_section,
969 CONTENTS isn't NULL and there may be additional transitions
970 based on TLS_TYPE. */
971 if (contents != NULL)
973 unsigned int new_to_type = to_type;
975 if (info->executable
976 && h != NULL
977 && h->dynindx == -1
978 && tls_type == GOT_TLS_IE)
979 new_to_type = R_X86_64_TPOFF32;
981 if (to_type == R_X86_64_TLSGD
982 || to_type == R_X86_64_GOTPC32_TLSDESC
983 || to_type == R_X86_64_TLSDESC_CALL)
985 if (tls_type == GOT_TLS_IE)
986 new_to_type = R_X86_64_GOTTPOFF;
989 /* We checked the transition before when we were called from
990 elf64_x86_64_check_relocs. We only want to check the new
991 transition which hasn't been checked before. */
992 check = new_to_type != to_type && from_type == to_type;
993 to_type = new_to_type;
996 break;
998 case R_X86_64_TLSLD:
999 if (info->executable)
1000 to_type = R_X86_64_TPOFF32;
1001 break;
1003 default:
1004 return TRUE;
1007 /* Return TRUE if there is no transition. */
1008 if (from_type == to_type)
1009 return TRUE;
1011 /* Check if the transition can be performed. */
1012 if (check
1013 && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
1014 symtab_hdr, sym_hashes,
1015 from_type, rel, relend))
1017 reloc_howto_type *from, *to;
1018 const char *name;
1020 from = elf64_x86_64_rtype_to_howto (abfd, from_type);
1021 to = elf64_x86_64_rtype_to_howto (abfd, to_type);
1023 if (h)
1024 name = h->root.root.string;
1025 else
1027 struct elf64_x86_64_link_hash_table *htab;
1029 htab = elf64_x86_64_hash_table (info);
1030 if (htab == NULL)
1031 name = "*unknown*";
1032 else
1034 Elf_Internal_Sym *isym;
1036 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1037 abfd, r_symndx);
1038 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1042 (*_bfd_error_handler)
1043 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1044 "in section `%A' failed"),
1045 abfd, sec, from->name, to->name, name,
1046 (unsigned long) rel->r_offset);
1047 bfd_set_error (bfd_error_bad_value);
1048 return FALSE;
1051 *r_type = to_type;
1052 return TRUE;
1055 /* Look through the relocs for a section during the first phase, and
1056 calculate needed space in the global offset table, procedure
1057 linkage table, and dynamic reloc sections. */
1059 static bfd_boolean
1060 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1061 asection *sec,
1062 const Elf_Internal_Rela *relocs)
1064 struct elf64_x86_64_link_hash_table *htab;
1065 Elf_Internal_Shdr *symtab_hdr;
1066 struct elf_link_hash_entry **sym_hashes;
1067 const Elf_Internal_Rela *rel;
1068 const Elf_Internal_Rela *rel_end;
1069 asection *sreloc;
1071 if (info->relocatable)
1072 return TRUE;
1074 BFD_ASSERT (is_x86_64_elf (abfd));
1076 htab = elf64_x86_64_hash_table (info);
1077 if (htab == NULL)
1078 return FALSE;
1080 symtab_hdr = &elf_symtab_hdr (abfd);
1081 sym_hashes = elf_sym_hashes (abfd);
1083 sreloc = NULL;
1085 rel_end = relocs + sec->reloc_count;
1086 for (rel = relocs; rel < rel_end; rel++)
1088 unsigned int r_type;
1089 unsigned long r_symndx;
1090 struct elf_link_hash_entry *h;
1091 Elf_Internal_Sym *isym;
1092 const char *name;
1094 r_symndx = ELF64_R_SYM (rel->r_info);
1095 r_type = ELF64_R_TYPE (rel->r_info);
1097 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1099 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1100 abfd, r_symndx);
1101 return FALSE;
1104 if (r_symndx < symtab_hdr->sh_info)
1106 /* A local symbol. */
1107 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1108 abfd, r_symndx);
1109 if (isym == NULL)
1110 return FALSE;
1112 /* Check relocation against local STT_GNU_IFUNC symbol. */
1113 if (ELF64_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1115 h = elf64_x86_64_get_local_sym_hash (htab, abfd, rel,
1116 TRUE);
1117 if (h == NULL)
1118 return FALSE;
1120 /* Fake a STT_GNU_IFUNC symbol. */
1121 h->type = STT_GNU_IFUNC;
1122 h->def_regular = 1;
1123 h->ref_regular = 1;
1124 h->forced_local = 1;
1125 h->root.type = bfd_link_hash_defined;
1127 else
1128 h = NULL;
1130 else
1132 isym = NULL;
1133 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1134 while (h->root.type == bfd_link_hash_indirect
1135 || h->root.type == bfd_link_hash_warning)
1136 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1139 if (h != NULL)
1141 /* Create the ifunc sections for static executables. If we
1142 never see an indirect function symbol nor we are building
1143 a static executable, those sections will be empty and
1144 won't appear in output. */
1145 switch (r_type)
1147 default:
1148 break;
1150 case R_X86_64_32S:
1151 case R_X86_64_32:
1152 case R_X86_64_64:
1153 case R_X86_64_PC32:
1154 case R_X86_64_PC64:
1155 case R_X86_64_PLT32:
1156 case R_X86_64_GOTPCREL:
1157 case R_X86_64_GOTPCREL64:
1158 if (!_bfd_elf_create_ifunc_sections (abfd, info))
1159 return FALSE;
1160 break;
1163 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1164 it here if it is defined in a non-shared object. */
1165 if (h->type == STT_GNU_IFUNC
1166 && h->def_regular)
1168 /* It is referenced by a non-shared object. */
1169 h->ref_regular = 1;
1170 h->needs_plt = 1;
1172 /* STT_GNU_IFUNC symbol must go through PLT. */
1173 h->plt.refcount += 1;
1175 /* STT_GNU_IFUNC needs dynamic sections. */
1176 if (htab->elf.dynobj == NULL)
1177 htab->elf.dynobj = abfd;
1179 switch (r_type)
1181 default:
1182 if (h->root.root.string)
1183 name = h->root.root.string;
1184 else
1185 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1186 NULL);
1187 (*_bfd_error_handler)
1188 (_("%B: relocation %s against STT_GNU_IFUNC "
1189 "symbol `%s' isn't handled by %s"), abfd,
1190 x86_64_elf_howto_table[r_type].name,
1191 name, __FUNCTION__);
1192 bfd_set_error (bfd_error_bad_value);
1193 return FALSE;
1195 case R_X86_64_64:
1196 h->non_got_ref = 1;
1197 h->pointer_equality_needed = 1;
1198 if (info->shared)
1200 /* We must copy these reloc types into the output
1201 file. Create a reloc section in dynobj and
1202 make room for this reloc. */
1203 sreloc = _bfd_elf_create_ifunc_dyn_reloc
1204 (abfd, info, sec, sreloc,
1205 &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs);
1206 if (sreloc == NULL)
1207 return FALSE;
1209 break;
1211 case R_X86_64_32S:
1212 case R_X86_64_32:
1213 case R_X86_64_PC32:
1214 case R_X86_64_PC64:
1215 h->non_got_ref = 1;
1216 if (r_type != R_X86_64_PC32
1217 && r_type != R_X86_64_PC64)
1218 h->pointer_equality_needed = 1;
1219 break;
1221 case R_X86_64_PLT32:
1222 break;
1224 case R_X86_64_GOTPCREL:
1225 case R_X86_64_GOTPCREL64:
1226 h->got.refcount += 1;
1227 if (htab->elf.sgot == NULL
1228 && !_bfd_elf_create_got_section (htab->elf.dynobj,
1229 info))
1230 return FALSE;
1231 break;
1234 continue;
1238 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1239 symtab_hdr, sym_hashes,
1240 &r_type, GOT_UNKNOWN,
1241 rel, rel_end, h, r_symndx))
1242 return FALSE;
1244 switch (r_type)
1246 case R_X86_64_TLSLD:
1247 htab->tls_ld_got.refcount += 1;
1248 goto create_got;
1250 case R_X86_64_TPOFF32:
1251 if (!info->executable)
1253 if (h)
1254 name = h->root.root.string;
1255 else
1256 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1257 NULL);
1258 (*_bfd_error_handler)
1259 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1260 abfd,
1261 x86_64_elf_howto_table[r_type].name, name);
1262 bfd_set_error (bfd_error_bad_value);
1263 return FALSE;
1265 break;
1267 case R_X86_64_GOTTPOFF:
1268 if (!info->executable)
1269 info->flags |= DF_STATIC_TLS;
1270 /* Fall through */
1272 case R_X86_64_GOT32:
1273 case R_X86_64_GOTPCREL:
1274 case R_X86_64_TLSGD:
1275 case R_X86_64_GOT64:
1276 case R_X86_64_GOTPCREL64:
1277 case R_X86_64_GOTPLT64:
1278 case R_X86_64_GOTPC32_TLSDESC:
1279 case R_X86_64_TLSDESC_CALL:
1280 /* This symbol requires a global offset table entry. */
1282 int tls_type, old_tls_type;
1284 switch (r_type)
1286 default: tls_type = GOT_NORMAL; break;
1287 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1288 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1289 case R_X86_64_GOTPC32_TLSDESC:
1290 case R_X86_64_TLSDESC_CALL:
1291 tls_type = GOT_TLS_GDESC; break;
1294 if (h != NULL)
1296 if (r_type == R_X86_64_GOTPLT64)
1298 /* This relocation indicates that we also need
1299 a PLT entry, as this is a function. We don't need
1300 a PLT entry for local symbols. */
1301 h->needs_plt = 1;
1302 h->plt.refcount += 1;
1304 h->got.refcount += 1;
1305 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1307 else
1309 bfd_signed_vma *local_got_refcounts;
1311 /* This is a global offset table entry for a local symbol. */
1312 local_got_refcounts = elf_local_got_refcounts (abfd);
1313 if (local_got_refcounts == NULL)
1315 bfd_size_type size;
1317 size = symtab_hdr->sh_info;
1318 size *= sizeof (bfd_signed_vma)
1319 + sizeof (bfd_vma) + sizeof (char);
1320 local_got_refcounts = ((bfd_signed_vma *)
1321 bfd_zalloc (abfd, size));
1322 if (local_got_refcounts == NULL)
1323 return FALSE;
1324 elf_local_got_refcounts (abfd) = local_got_refcounts;
1325 elf64_x86_64_local_tlsdesc_gotent (abfd)
1326 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1327 elf64_x86_64_local_got_tls_type (abfd)
1328 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1330 local_got_refcounts[r_symndx] += 1;
1331 old_tls_type
1332 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1335 /* If a TLS symbol is accessed using IE at least once,
1336 there is no point to use dynamic model for it. */
1337 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1338 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1339 || tls_type != GOT_TLS_IE))
1341 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1342 tls_type = old_tls_type;
1343 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1344 && GOT_TLS_GD_ANY_P (tls_type))
1345 tls_type |= old_tls_type;
1346 else
1348 if (h)
1349 name = h->root.root.string;
1350 else
1351 name = bfd_elf_sym_name (abfd, symtab_hdr,
1352 isym, NULL);
1353 (*_bfd_error_handler)
1354 (_("%B: '%s' accessed both as normal and thread local symbol"),
1355 abfd, name);
1356 return FALSE;
1360 if (old_tls_type != tls_type)
1362 if (h != NULL)
1363 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1364 else
1365 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1368 /* Fall through */
1370 case R_X86_64_GOTOFF64:
1371 case R_X86_64_GOTPC32:
1372 case R_X86_64_GOTPC64:
1373 create_got:
1374 if (htab->elf.sgot == NULL)
1376 if (htab->elf.dynobj == NULL)
1377 htab->elf.dynobj = abfd;
1378 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1379 info))
1380 return FALSE;
1382 break;
1384 case R_X86_64_PLT32:
1385 /* This symbol requires a procedure linkage table entry. We
1386 actually build the entry in adjust_dynamic_symbol,
1387 because this might be a case of linking PIC code which is
1388 never referenced by a dynamic object, in which case we
1389 don't need to generate a procedure linkage table entry
1390 after all. */
1392 /* If this is a local symbol, we resolve it directly without
1393 creating a procedure linkage table entry. */
1394 if (h == NULL)
1395 continue;
1397 h->needs_plt = 1;
1398 h->plt.refcount += 1;
1399 break;
1401 case R_X86_64_PLTOFF64:
1402 /* This tries to form the 'address' of a function relative
1403 to GOT. For global symbols we need a PLT entry. */
1404 if (h != NULL)
1406 h->needs_plt = 1;
1407 h->plt.refcount += 1;
1409 goto create_got;
1411 case R_X86_64_8:
1412 case R_X86_64_16:
1413 case R_X86_64_32:
1414 case R_X86_64_32S:
1415 /* Let's help debug shared library creation. These relocs
1416 cannot be used in shared libs. Don't error out for
1417 sections we don't care about, such as debug sections or
1418 non-constant sections. */
1419 if (info->shared
1420 && (sec->flags & SEC_ALLOC) != 0
1421 && (sec->flags & SEC_READONLY) != 0)
1423 if (h)
1424 name = h->root.root.string;
1425 else
1426 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1427 (*_bfd_error_handler)
1428 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1429 abfd, x86_64_elf_howto_table[r_type].name, name);
1430 bfd_set_error (bfd_error_bad_value);
1431 return FALSE;
1433 /* Fall through. */
1435 case R_X86_64_PC8:
1436 case R_X86_64_PC16:
1437 case R_X86_64_PC32:
1438 case R_X86_64_PC64:
1439 case R_X86_64_64:
1440 if (h != NULL && info->executable)
1442 /* If this reloc is in a read-only section, we might
1443 need a copy reloc. We can't check reliably at this
1444 stage whether the section is read-only, as input
1445 sections have not yet been mapped to output sections.
1446 Tentatively set the flag for now, and correct in
1447 adjust_dynamic_symbol. */
1448 h->non_got_ref = 1;
1450 /* We may need a .plt entry if the function this reloc
1451 refers to is in a shared lib. */
1452 h->plt.refcount += 1;
1453 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1454 h->pointer_equality_needed = 1;
1457 /* If we are creating a shared library, and this is a reloc
1458 against a global symbol, or a non PC relative reloc
1459 against a local symbol, then we need to copy the reloc
1460 into the shared library. However, if we are linking with
1461 -Bsymbolic, we do not need to copy a reloc against a
1462 global symbol which is defined in an object we are
1463 including in the link (i.e., DEF_REGULAR is set). At
1464 this point we have not seen all the input files, so it is
1465 possible that DEF_REGULAR is not set now but will be set
1466 later (it is never cleared). In case of a weak definition,
1467 DEF_REGULAR may be cleared later by a strong definition in
1468 a shared library. We account for that possibility below by
1469 storing information in the relocs_copied field of the hash
1470 table entry. A similar situation occurs when creating
1471 shared libraries and symbol visibility changes render the
1472 symbol local.
1474 If on the other hand, we are creating an executable, we
1475 may need to keep relocations for symbols satisfied by a
1476 dynamic library if we manage to avoid copy relocs for the
1477 symbol. */
1478 if ((info->shared
1479 && (sec->flags & SEC_ALLOC) != 0
1480 && (! IS_X86_64_PCREL_TYPE (r_type)
1481 || (h != NULL
1482 && (! SYMBOLIC_BIND (info, h)
1483 || h->root.type == bfd_link_hash_defweak
1484 || !h->def_regular))))
1485 || (ELIMINATE_COPY_RELOCS
1486 && !info->shared
1487 && (sec->flags & SEC_ALLOC) != 0
1488 && h != NULL
1489 && (h->root.type == bfd_link_hash_defweak
1490 || !h->def_regular)))
1492 struct elf_dyn_relocs *p;
1493 struct elf_dyn_relocs **head;
1495 /* We must copy these reloc types into the output file.
1496 Create a reloc section in dynobj and make room for
1497 this reloc. */
1498 if (sreloc == NULL)
1500 if (htab->elf.dynobj == NULL)
1501 htab->elf.dynobj = abfd;
1503 sreloc = _bfd_elf_make_dynamic_reloc_section
1504 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
1506 if (sreloc == NULL)
1507 return FALSE;
1510 /* If this is a global symbol, we count the number of
1511 relocations we need for this symbol. */
1512 if (h != NULL)
1514 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1516 else
1518 /* Track dynamic relocs needed for local syms too.
1519 We really need local syms available to do this
1520 easily. Oh well. */
1521 asection *s;
1522 void **vpp;
1524 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1525 abfd, r_symndx);
1526 if (isym == NULL)
1527 return FALSE;
1529 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1530 if (s == NULL)
1531 s = sec;
1533 /* Beware of type punned pointers vs strict aliasing
1534 rules. */
1535 vpp = &(elf_section_data (s)->local_dynrel);
1536 head = (struct elf_dyn_relocs **)vpp;
1539 p = *head;
1540 if (p == NULL || p->sec != sec)
1542 bfd_size_type amt = sizeof *p;
1544 p = ((struct elf_dyn_relocs *)
1545 bfd_alloc (htab->elf.dynobj, amt));
1546 if (p == NULL)
1547 return FALSE;
1548 p->next = *head;
1549 *head = p;
1550 p->sec = sec;
1551 p->count = 0;
1552 p->pc_count = 0;
1555 p->count += 1;
1556 if (IS_X86_64_PCREL_TYPE (r_type))
1557 p->pc_count += 1;
1559 break;
1561 /* This relocation describes the C++ object vtable hierarchy.
1562 Reconstruct it for later use during GC. */
1563 case R_X86_64_GNU_VTINHERIT:
1564 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1565 return FALSE;
1566 break;
1568 /* This relocation describes which C++ vtable entries are actually
1569 used. Record for later use during GC. */
1570 case R_X86_64_GNU_VTENTRY:
1571 BFD_ASSERT (h != NULL);
1572 if (h != NULL
1573 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1574 return FALSE;
1575 break;
1577 default:
1578 break;
1582 return TRUE;
1585 /* Return the section that should be marked against GC for a given
1586 relocation. */
1588 static asection *
1589 elf64_x86_64_gc_mark_hook (asection *sec,
1590 struct bfd_link_info *info,
1591 Elf_Internal_Rela *rel,
1592 struct elf_link_hash_entry *h,
1593 Elf_Internal_Sym *sym)
1595 if (h != NULL)
1596 switch (ELF64_R_TYPE (rel->r_info))
1598 case R_X86_64_GNU_VTINHERIT:
1599 case R_X86_64_GNU_VTENTRY:
1600 return NULL;
1603 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1606 /* Update the got entry reference counts for the section being removed. */
1608 static bfd_boolean
1609 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1610 asection *sec,
1611 const Elf_Internal_Rela *relocs)
1613 struct elf64_x86_64_link_hash_table *htab;
1614 Elf_Internal_Shdr *symtab_hdr;
1615 struct elf_link_hash_entry **sym_hashes;
1616 bfd_signed_vma *local_got_refcounts;
1617 const Elf_Internal_Rela *rel, *relend;
1619 if (info->relocatable)
1620 return TRUE;
1622 htab = elf64_x86_64_hash_table (info);
1623 if (htab == NULL)
1624 return FALSE;
1626 elf_section_data (sec)->local_dynrel = NULL;
1628 symtab_hdr = &elf_symtab_hdr (abfd);
1629 sym_hashes = elf_sym_hashes (abfd);
1630 local_got_refcounts = elf_local_got_refcounts (abfd);
1632 relend = relocs + sec->reloc_count;
1633 for (rel = relocs; rel < relend; rel++)
1635 unsigned long r_symndx;
1636 unsigned int r_type;
1637 struct elf_link_hash_entry *h = NULL;
1639 r_symndx = ELF64_R_SYM (rel->r_info);
1640 if (r_symndx >= symtab_hdr->sh_info)
1642 struct elf64_x86_64_link_hash_entry *eh;
1643 struct elf_dyn_relocs **pp;
1644 struct elf_dyn_relocs *p;
1646 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1647 while (h->root.type == bfd_link_hash_indirect
1648 || h->root.type == bfd_link_hash_warning)
1649 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1650 eh = (struct elf64_x86_64_link_hash_entry *) h;
1652 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1653 if (p->sec == sec)
1655 /* Everything must go for SEC. */
1656 *pp = p->next;
1657 break;
1661 r_type = ELF64_R_TYPE (rel->r_info);
1662 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1663 symtab_hdr, sym_hashes,
1664 &r_type, GOT_UNKNOWN,
1665 rel, relend, h, r_symndx))
1666 return FALSE;
1668 switch (r_type)
1670 case R_X86_64_TLSLD:
1671 if (htab->tls_ld_got.refcount > 0)
1672 htab->tls_ld_got.refcount -= 1;
1673 break;
1675 case R_X86_64_TLSGD:
1676 case R_X86_64_GOTPC32_TLSDESC:
1677 case R_X86_64_TLSDESC_CALL:
1678 case R_X86_64_GOTTPOFF:
1679 case R_X86_64_GOT32:
1680 case R_X86_64_GOTPCREL:
1681 case R_X86_64_GOT64:
1682 case R_X86_64_GOTPCREL64:
1683 case R_X86_64_GOTPLT64:
1684 if (h != NULL)
1686 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1687 h->plt.refcount -= 1;
1688 if (h->got.refcount > 0)
1689 h->got.refcount -= 1;
1691 else if (local_got_refcounts != NULL)
1693 if (local_got_refcounts[r_symndx] > 0)
1694 local_got_refcounts[r_symndx] -= 1;
1696 break;
1698 case R_X86_64_8:
1699 case R_X86_64_16:
1700 case R_X86_64_32:
1701 case R_X86_64_64:
1702 case R_X86_64_32S:
1703 case R_X86_64_PC8:
1704 case R_X86_64_PC16:
1705 case R_X86_64_PC32:
1706 case R_X86_64_PC64:
1707 if (info->shared)
1708 break;
1709 /* Fall thru */
1711 case R_X86_64_PLT32:
1712 case R_X86_64_PLTOFF64:
1713 if (h != NULL)
1715 if (h->plt.refcount > 0)
1716 h->plt.refcount -= 1;
1718 break;
1720 default:
1721 break;
1725 return TRUE;
1728 /* Adjust a symbol defined by a dynamic object and referenced by a
1729 regular object. The current definition is in some section of the
1730 dynamic object, but we're not including those sections. We have to
1731 change the definition to something the rest of the link can
1732 understand. */
1734 static bfd_boolean
1735 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1736 struct elf_link_hash_entry *h)
1738 struct elf64_x86_64_link_hash_table *htab;
1739 asection *s;
1741 /* STT_GNU_IFUNC symbol must go through PLT. */
1742 if (h->type == STT_GNU_IFUNC)
1744 if (h->plt.refcount <= 0)
1746 h->plt.offset = (bfd_vma) -1;
1747 h->needs_plt = 0;
1749 return TRUE;
1752 /* If this is a function, put it in the procedure linkage table. We
1753 will fill in the contents of the procedure linkage table later,
1754 when we know the address of the .got section. */
1755 if (h->type == STT_FUNC
1756 || h->needs_plt)
1758 if (h->plt.refcount <= 0
1759 || SYMBOL_CALLS_LOCAL (info, h)
1760 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1761 && h->root.type == bfd_link_hash_undefweak))
1763 /* This case can occur if we saw a PLT32 reloc in an input
1764 file, but the symbol was never referred to by a dynamic
1765 object, or if all references were garbage collected. In
1766 such a case, we don't actually need to build a procedure
1767 linkage table, and we can just do a PC32 reloc instead. */
1768 h->plt.offset = (bfd_vma) -1;
1769 h->needs_plt = 0;
1772 return TRUE;
1774 else
1775 /* It's possible that we incorrectly decided a .plt reloc was
1776 needed for an R_X86_64_PC32 reloc to a non-function sym in
1777 check_relocs. We can't decide accurately between function and
1778 non-function syms in check-relocs; Objects loaded later in
1779 the link may change h->type. So fix it now. */
1780 h->plt.offset = (bfd_vma) -1;
1782 /* If this is a weak symbol, and there is a real definition, the
1783 processor independent code will have arranged for us to see the
1784 real definition first, and we can just use the same value. */
1785 if (h->u.weakdef != NULL)
1787 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1788 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1789 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1790 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1791 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1792 h->non_got_ref = h->u.weakdef->non_got_ref;
1793 return TRUE;
1796 /* This is a reference to a symbol defined by a dynamic object which
1797 is not a function. */
1799 /* If we are creating a shared library, we must presume that the
1800 only references to the symbol are via the global offset table.
1801 For such cases we need not do anything here; the relocations will
1802 be handled correctly by relocate_section. */
1803 if (info->shared)
1804 return TRUE;
1806 /* If there are no references to this symbol that do not use the
1807 GOT, we don't need to generate a copy reloc. */
1808 if (!h->non_got_ref)
1809 return TRUE;
1811 /* If -z nocopyreloc was given, we won't generate them either. */
1812 if (info->nocopyreloc)
1814 h->non_got_ref = 0;
1815 return TRUE;
1818 if (ELIMINATE_COPY_RELOCS)
1820 struct elf64_x86_64_link_hash_entry * eh;
1821 struct elf_dyn_relocs *p;
1823 eh = (struct elf64_x86_64_link_hash_entry *) h;
1824 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1826 s = p->sec->output_section;
1827 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1828 break;
1831 /* If we didn't find any dynamic relocs in read-only sections, then
1832 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1833 if (p == NULL)
1835 h->non_got_ref = 0;
1836 return TRUE;
1840 if (h->size == 0)
1842 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1843 h->root.root.string);
1844 return TRUE;
1847 /* We must allocate the symbol in our .dynbss section, which will
1848 become part of the .bss section of the executable. There will be
1849 an entry for this symbol in the .dynsym section. The dynamic
1850 object will contain position independent code, so all references
1851 from the dynamic object to this symbol will go through the global
1852 offset table. The dynamic linker will use the .dynsym entry to
1853 determine the address it must put in the global offset table, so
1854 both the dynamic object and the regular object will refer to the
1855 same memory location for the variable. */
1857 htab = elf64_x86_64_hash_table (info);
1858 if (htab == NULL)
1859 return FALSE;
1861 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1862 to copy the initial value out of the dynamic object and into the
1863 runtime process image. */
1864 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1866 htab->srelbss->size += sizeof (Elf64_External_Rela);
1867 h->needs_copy = 1;
1870 s = htab->sdynbss;
1872 return _bfd_elf_adjust_dynamic_copy (h, s);
1875 /* Allocate space in .plt, .got and associated reloc sections for
1876 dynamic relocs. */
1878 static bfd_boolean
1879 elf64_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1881 struct bfd_link_info *info;
1882 struct elf64_x86_64_link_hash_table *htab;
1883 struct elf64_x86_64_link_hash_entry *eh;
1884 struct elf_dyn_relocs *p;
1886 if (h->root.type == bfd_link_hash_indirect)
1887 return TRUE;
1889 if (h->root.type == bfd_link_hash_warning)
1890 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1891 eh = (struct elf64_x86_64_link_hash_entry *) h;
1893 info = (struct bfd_link_info *) inf;
1894 htab = elf64_x86_64_hash_table (info);
1895 if (htab == NULL)
1896 return FALSE;
1898 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1899 here if it is defined and referenced in a non-shared object. */
1900 if (h->type == STT_GNU_IFUNC
1901 && h->def_regular)
1902 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
1903 &eh->dyn_relocs,
1904 PLT_ENTRY_SIZE,
1905 GOT_ENTRY_SIZE);
1906 else if (htab->elf.dynamic_sections_created
1907 && h->plt.refcount > 0)
1909 /* Make sure this symbol is output as a dynamic symbol.
1910 Undefined weak syms won't yet be marked as dynamic. */
1911 if (h->dynindx == -1
1912 && !h->forced_local)
1914 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1915 return FALSE;
1918 if (info->shared
1919 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1921 asection *s = htab->elf.splt;
1923 /* If this is the first .plt entry, make room for the special
1924 first entry. */
1925 if (s->size == 0)
1926 s->size += PLT_ENTRY_SIZE;
1928 h->plt.offset = s->size;
1930 /* If this symbol is not defined in a regular file, and we are
1931 not generating a shared library, then set the symbol to this
1932 location in the .plt. This is required to make function
1933 pointers compare as equal between the normal executable and
1934 the shared library. */
1935 if (! info->shared
1936 && !h->def_regular)
1938 h->root.u.def.section = s;
1939 h->root.u.def.value = h->plt.offset;
1942 /* Make room for this entry. */
1943 s->size += PLT_ENTRY_SIZE;
1945 /* We also need to make an entry in the .got.plt section, which
1946 will be placed in the .got section by the linker script. */
1947 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1949 /* We also need to make an entry in the .rela.plt section. */
1950 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1951 htab->elf.srelplt->reloc_count++;
1953 else
1955 h->plt.offset = (bfd_vma) -1;
1956 h->needs_plt = 0;
1959 else
1961 h->plt.offset = (bfd_vma) -1;
1962 h->needs_plt = 0;
1965 eh->tlsdesc_got = (bfd_vma) -1;
1967 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1968 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1969 if (h->got.refcount > 0
1970 && info->executable
1971 && h->dynindx == -1
1972 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1974 h->got.offset = (bfd_vma) -1;
1976 else if (h->got.refcount > 0)
1978 asection *s;
1979 bfd_boolean dyn;
1980 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1982 /* Make sure this symbol is output as a dynamic symbol.
1983 Undefined weak syms won't yet be marked as dynamic. */
1984 if (h->dynindx == -1
1985 && !h->forced_local)
1987 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1988 return FALSE;
1991 if (GOT_TLS_GDESC_P (tls_type))
1993 eh->tlsdesc_got = htab->elf.sgotplt->size
1994 - elf64_x86_64_compute_jump_table_size (htab);
1995 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
1996 h->got.offset = (bfd_vma) -2;
1998 if (! GOT_TLS_GDESC_P (tls_type)
1999 || GOT_TLS_GD_P (tls_type))
2001 s = htab->elf.sgot;
2002 h->got.offset = s->size;
2003 s->size += GOT_ENTRY_SIZE;
2004 if (GOT_TLS_GD_P (tls_type))
2005 s->size += GOT_ENTRY_SIZE;
2007 dyn = htab->elf.dynamic_sections_created;
2008 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2009 and two if global.
2010 R_X86_64_GOTTPOFF needs one dynamic relocation. */
2011 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2012 || tls_type == GOT_TLS_IE)
2013 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2014 else if (GOT_TLS_GD_P (tls_type))
2015 htab->elf.srelgot->size += 2 * sizeof (Elf64_External_Rela);
2016 else if (! GOT_TLS_GDESC_P (tls_type)
2017 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2018 || h->root.type != bfd_link_hash_undefweak)
2019 && (info->shared
2020 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2021 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2022 if (GOT_TLS_GDESC_P (tls_type))
2024 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
2025 htab->tlsdesc_plt = (bfd_vma) -1;
2028 else
2029 h->got.offset = (bfd_vma) -1;
2031 if (eh->dyn_relocs == NULL)
2032 return TRUE;
2034 /* In the shared -Bsymbolic case, discard space allocated for
2035 dynamic pc-relative relocs against symbols which turn out to be
2036 defined in regular objects. For the normal shared case, discard
2037 space for pc-relative relocs that have become local due to symbol
2038 visibility changes. */
2040 if (info->shared)
2042 /* Relocs that use pc_count are those that appear on a call
2043 insn, or certain REL relocs that can generated via assembly.
2044 We want calls to protected symbols to resolve directly to the
2045 function rather than going via the plt. If people want
2046 function pointer comparisons to work as expected then they
2047 should avoid writing weird assembly. */
2048 if (SYMBOL_CALLS_LOCAL (info, h))
2050 struct elf_dyn_relocs **pp;
2052 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2054 p->count -= p->pc_count;
2055 p->pc_count = 0;
2056 if (p->count == 0)
2057 *pp = p->next;
2058 else
2059 pp = &p->next;
2063 /* Also discard relocs on undefined weak syms with non-default
2064 visibility. */
2065 if (eh->dyn_relocs != NULL
2066 && h->root.type == bfd_link_hash_undefweak)
2068 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2069 eh->dyn_relocs = NULL;
2071 /* Make sure undefined weak symbols are output as a dynamic
2072 symbol in PIEs. */
2073 else if (h->dynindx == -1
2074 && ! h->forced_local
2075 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2076 return FALSE;
2080 else if (ELIMINATE_COPY_RELOCS)
2082 /* For the non-shared case, discard space for relocs against
2083 symbols which turn out to need copy relocs or are not
2084 dynamic. */
2086 if (!h->non_got_ref
2087 && ((h->def_dynamic
2088 && !h->def_regular)
2089 || (htab->elf.dynamic_sections_created
2090 && (h->root.type == bfd_link_hash_undefweak
2091 || h->root.type == bfd_link_hash_undefined))))
2093 /* Make sure this symbol is output as a dynamic symbol.
2094 Undefined weak syms won't yet be marked as dynamic. */
2095 if (h->dynindx == -1
2096 && ! h->forced_local
2097 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2098 return FALSE;
2100 /* If that succeeded, we know we'll be keeping all the
2101 relocs. */
2102 if (h->dynindx != -1)
2103 goto keep;
2106 eh->dyn_relocs = NULL;
2108 keep: ;
2111 /* Finally, allocate space. */
2112 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2114 asection * sreloc;
2116 sreloc = elf_section_data (p->sec)->sreloc;
2118 BFD_ASSERT (sreloc != NULL);
2120 sreloc->size += p->count * sizeof (Elf64_External_Rela);
2123 return TRUE;
2126 /* Allocate space in .plt, .got and associated reloc sections for
2127 local dynamic relocs. */
2129 static bfd_boolean
2130 elf64_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
2132 struct elf_link_hash_entry *h
2133 = (struct elf_link_hash_entry *) *slot;
2135 if (h->type != STT_GNU_IFUNC
2136 || !h->def_regular
2137 || !h->ref_regular
2138 || !h->forced_local
2139 || h->root.type != bfd_link_hash_defined)
2140 abort ();
2142 return elf64_x86_64_allocate_dynrelocs (h, inf);
2145 /* Find any dynamic relocs that apply to read-only sections. */
2147 static bfd_boolean
2148 elf64_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2150 struct elf64_x86_64_link_hash_entry *eh;
2151 struct elf_dyn_relocs *p;
2153 if (h->root.type == bfd_link_hash_warning)
2154 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2156 eh = (struct elf64_x86_64_link_hash_entry *) h;
2157 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2159 asection *s = p->sec->output_section;
2161 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2163 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2165 info->flags |= DF_TEXTREL;
2167 /* Not an error, just cut short the traversal. */
2168 return FALSE;
2171 return TRUE;
2174 /* Set the sizes of the dynamic sections. */
2176 static bfd_boolean
2177 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2178 struct bfd_link_info *info)
2180 struct elf64_x86_64_link_hash_table *htab;
2181 bfd *dynobj;
2182 asection *s;
2183 bfd_boolean relocs;
2184 bfd *ibfd;
2186 htab = elf64_x86_64_hash_table (info);
2187 if (htab == NULL)
2188 return FALSE;
2190 dynobj = htab->elf.dynobj;
2191 if (dynobj == NULL)
2192 abort ();
2194 if (htab->elf.dynamic_sections_created)
2196 /* Set the contents of the .interp section to the interpreter. */
2197 if (info->executable)
2199 s = bfd_get_section_by_name (dynobj, ".interp");
2200 if (s == NULL)
2201 abort ();
2202 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2203 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2207 /* Set up .got offsets for local syms, and space for local dynamic
2208 relocs. */
2209 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2211 bfd_signed_vma *local_got;
2212 bfd_signed_vma *end_local_got;
2213 char *local_tls_type;
2214 bfd_vma *local_tlsdesc_gotent;
2215 bfd_size_type locsymcount;
2216 Elf_Internal_Shdr *symtab_hdr;
2217 asection *srel;
2219 if (! is_x86_64_elf (ibfd))
2220 continue;
2222 for (s = ibfd->sections; s != NULL; s = s->next)
2224 struct elf_dyn_relocs *p;
2226 for (p = (struct elf_dyn_relocs *)
2227 (elf_section_data (s)->local_dynrel);
2228 p != NULL;
2229 p = p->next)
2231 if (!bfd_is_abs_section (p->sec)
2232 && bfd_is_abs_section (p->sec->output_section))
2234 /* Input section has been discarded, either because
2235 it is a copy of a linkonce section or due to
2236 linker script /DISCARD/, so we'll be discarding
2237 the relocs too. */
2239 else if (p->count != 0)
2241 srel = elf_section_data (p->sec)->sreloc;
2242 srel->size += p->count * sizeof (Elf64_External_Rela);
2243 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2244 info->flags |= DF_TEXTREL;
2249 local_got = elf_local_got_refcounts (ibfd);
2250 if (!local_got)
2251 continue;
2253 symtab_hdr = &elf_symtab_hdr (ibfd);
2254 locsymcount = symtab_hdr->sh_info;
2255 end_local_got = local_got + locsymcount;
2256 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
2257 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
2258 s = htab->elf.sgot;
2259 srel = htab->elf.srelgot;
2260 for (; local_got < end_local_got;
2261 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2263 *local_tlsdesc_gotent = (bfd_vma) -1;
2264 if (*local_got > 0)
2266 if (GOT_TLS_GDESC_P (*local_tls_type))
2268 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2269 - elf64_x86_64_compute_jump_table_size (htab);
2270 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2271 *local_got = (bfd_vma) -2;
2273 if (! GOT_TLS_GDESC_P (*local_tls_type)
2274 || GOT_TLS_GD_P (*local_tls_type))
2276 *local_got = s->size;
2277 s->size += GOT_ENTRY_SIZE;
2278 if (GOT_TLS_GD_P (*local_tls_type))
2279 s->size += GOT_ENTRY_SIZE;
2281 if (info->shared
2282 || GOT_TLS_GD_ANY_P (*local_tls_type)
2283 || *local_tls_type == GOT_TLS_IE)
2285 if (GOT_TLS_GDESC_P (*local_tls_type))
2287 htab->elf.srelplt->size
2288 += sizeof (Elf64_External_Rela);
2289 htab->tlsdesc_plt = (bfd_vma) -1;
2291 if (! GOT_TLS_GDESC_P (*local_tls_type)
2292 || GOT_TLS_GD_P (*local_tls_type))
2293 srel->size += sizeof (Elf64_External_Rela);
2296 else
2297 *local_got = (bfd_vma) -1;
2301 if (htab->tls_ld_got.refcount > 0)
2303 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2304 relocs. */
2305 htab->tls_ld_got.offset = htab->elf.sgot->size;
2306 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
2307 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2309 else
2310 htab->tls_ld_got.offset = -1;
2312 /* Allocate global sym .plt and .got entries, and space for global
2313 sym dynamic relocs. */
2314 elf_link_hash_traverse (&htab->elf, elf64_x86_64_allocate_dynrelocs,
2315 info);
2317 /* Allocate .plt and .got entries, and space for local symbols. */
2318 htab_traverse (htab->loc_hash_table,
2319 elf64_x86_64_allocate_local_dynrelocs,
2320 info);
2322 /* For every jump slot reserved in the sgotplt, reloc_count is
2323 incremented. However, when we reserve space for TLS descriptors,
2324 it's not incremented, so in order to compute the space reserved
2325 for them, it suffices to multiply the reloc count by the jump
2326 slot size. */
2327 if (htab->elf.srelplt)
2328 htab->sgotplt_jump_table_size
2329 = elf64_x86_64_compute_jump_table_size (htab);
2331 if (htab->tlsdesc_plt)
2333 /* If we're not using lazy TLS relocations, don't generate the
2334 PLT and GOT entries they require. */
2335 if ((info->flags & DF_BIND_NOW))
2336 htab->tlsdesc_plt = 0;
2337 else
2339 htab->tlsdesc_got = htab->elf.sgot->size;
2340 htab->elf.sgot->size += GOT_ENTRY_SIZE;
2341 /* Reserve room for the initial entry.
2342 FIXME: we could probably do away with it in this case. */
2343 if (htab->elf.splt->size == 0)
2344 htab->elf.splt->size += PLT_ENTRY_SIZE;
2345 htab->tlsdesc_plt = htab->elf.splt->size;
2346 htab->elf.splt->size += PLT_ENTRY_SIZE;
2350 /* We now have determined the sizes of the various dynamic sections.
2351 Allocate memory for them. */
2352 relocs = FALSE;
2353 for (s = dynobj->sections; s != NULL; s = s->next)
2355 if ((s->flags & SEC_LINKER_CREATED) == 0)
2356 continue;
2358 if (s == htab->elf.splt
2359 || s == htab->elf.sgot
2360 || s == htab->elf.sgotplt
2361 || s == htab->elf.iplt
2362 || s == htab->elf.igotplt
2363 || s == htab->sdynbss)
2365 /* Strip this section if we don't need it; see the
2366 comment below. */
2368 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2370 if (s->size != 0 && s != htab->elf.srelplt)
2371 relocs = TRUE;
2373 /* We use the reloc_count field as a counter if we need
2374 to copy relocs into the output file. */
2375 if (s != htab->elf.srelplt)
2376 s->reloc_count = 0;
2378 else
2380 /* It's not one of our sections, so don't allocate space. */
2381 continue;
2384 if (s->size == 0)
2386 /* If we don't need this section, strip it from the
2387 output file. This is mostly to handle .rela.bss and
2388 .rela.plt. We must create both sections in
2389 create_dynamic_sections, because they must be created
2390 before the linker maps input sections to output
2391 sections. The linker does that before
2392 adjust_dynamic_symbol is called, and it is that
2393 function which decides whether anything needs to go
2394 into these sections. */
2396 s->flags |= SEC_EXCLUDE;
2397 continue;
2400 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2401 continue;
2403 /* Allocate memory for the section contents. We use bfd_zalloc
2404 here in case unused entries are not reclaimed before the
2405 section's contents are written out. This should not happen,
2406 but this way if it does, we get a R_X86_64_NONE reloc instead
2407 of garbage. */
2408 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2409 if (s->contents == NULL)
2410 return FALSE;
2413 if (htab->elf.dynamic_sections_created)
2415 /* Add some entries to the .dynamic section. We fill in the
2416 values later, in elf64_x86_64_finish_dynamic_sections, but we
2417 must add the entries now so that we get the correct size for
2418 the .dynamic section. The DT_DEBUG entry is filled in by the
2419 dynamic linker and used by the debugger. */
2420 #define add_dynamic_entry(TAG, VAL) \
2421 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2423 if (info->executable)
2425 if (!add_dynamic_entry (DT_DEBUG, 0))
2426 return FALSE;
2429 if (htab->elf.splt->size != 0)
2431 if (!add_dynamic_entry (DT_PLTGOT, 0)
2432 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2433 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2434 || !add_dynamic_entry (DT_JMPREL, 0))
2435 return FALSE;
2437 if (htab->tlsdesc_plt
2438 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2439 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2440 return FALSE;
2443 if (relocs)
2445 if (!add_dynamic_entry (DT_RELA, 0)
2446 || !add_dynamic_entry (DT_RELASZ, 0)
2447 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2448 return FALSE;
2450 /* If any dynamic relocs apply to a read-only section,
2451 then we need a DT_TEXTREL entry. */
2452 if ((info->flags & DF_TEXTREL) == 0)
2453 elf_link_hash_traverse (&htab->elf,
2454 elf64_x86_64_readonly_dynrelocs,
2455 info);
2457 if ((info->flags & DF_TEXTREL) != 0)
2459 if (!add_dynamic_entry (DT_TEXTREL, 0))
2460 return FALSE;
2464 #undef add_dynamic_entry
2466 return TRUE;
2469 static bfd_boolean
2470 elf64_x86_64_always_size_sections (bfd *output_bfd,
2471 struct bfd_link_info *info)
2473 asection *tls_sec = elf_hash_table (info)->tls_sec;
2475 if (tls_sec)
2477 struct elf_link_hash_entry *tlsbase;
2479 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2480 "_TLS_MODULE_BASE_",
2481 FALSE, FALSE, FALSE);
2483 if (tlsbase && tlsbase->type == STT_TLS)
2485 struct elf64_x86_64_link_hash_table *htab;
2486 struct bfd_link_hash_entry *bh = NULL;
2487 const struct elf_backend_data *bed
2488 = get_elf_backend_data (output_bfd);
2490 htab = elf64_x86_64_hash_table (info);
2491 if (htab == NULL)
2492 return FALSE;
2494 if (!(_bfd_generic_link_add_one_symbol
2495 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2496 tls_sec, 0, NULL, FALSE,
2497 bed->collect, &bh)))
2498 return FALSE;
2500 htab->tls_module_base = bh;
2502 tlsbase = (struct elf_link_hash_entry *)bh;
2503 tlsbase->def_regular = 1;
2504 tlsbase->other = STV_HIDDEN;
2505 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2509 return TRUE;
2512 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2513 executables. Rather than setting it to the beginning of the TLS
2514 section, we have to set it to the end. This function may be called
2515 multiple times, it is idempotent. */
2517 static void
2518 elf64_x86_64_set_tls_module_base (struct bfd_link_info *info)
2520 struct elf64_x86_64_link_hash_table *htab;
2521 struct bfd_link_hash_entry *base;
2523 if (!info->executable)
2524 return;
2526 htab = elf64_x86_64_hash_table (info);
2527 if (htab == NULL)
2528 return;
2530 base = htab->tls_module_base;
2531 if (base == NULL)
2532 return;
2534 base->u.def.value = htab->elf.tls_size;
2537 /* Return the base VMA address which should be subtracted from real addresses
2538 when resolving @dtpoff relocation.
2539 This is PT_TLS segment p_vaddr. */
2541 static bfd_vma
2542 elf64_x86_64_dtpoff_base (struct bfd_link_info *info)
2544 /* If tls_sec is NULL, we should have signalled an error already. */
2545 if (elf_hash_table (info)->tls_sec == NULL)
2546 return 0;
2547 return elf_hash_table (info)->tls_sec->vma;
2550 /* Return the relocation value for @tpoff relocation
2551 if STT_TLS virtual address is ADDRESS. */
2553 static bfd_vma
2554 elf64_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2556 struct elf_link_hash_table *htab = elf_hash_table (info);
2558 /* If tls_segment is NULL, we should have signalled an error already. */
2559 if (htab->tls_sec == NULL)
2560 return 0;
2561 return address - htab->tls_size - htab->tls_sec->vma;
2564 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2565 branch? */
2567 static bfd_boolean
2568 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2570 /* Opcode Instruction
2571 0xe8 call
2572 0xe9 jump
2573 0x0f 0x8x conditional jump */
2574 return ((offset > 0
2575 && (contents [offset - 1] == 0xe8
2576 || contents [offset - 1] == 0xe9))
2577 || (offset > 1
2578 && contents [offset - 2] == 0x0f
2579 && (contents [offset - 1] & 0xf0) == 0x80));
2582 static void
2583 elf64_x86_64_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
2585 bfd_byte *loc = s->contents;
2586 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2587 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2588 <= s->contents + s->size);
2589 bfd_elf64_swap_reloca_out (abfd, rel, loc);
2592 /* Relocate an x86_64 ELF section. */
2594 static bfd_boolean
2595 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2596 bfd *input_bfd, asection *input_section,
2597 bfd_byte *contents, Elf_Internal_Rela *relocs,
2598 Elf_Internal_Sym *local_syms,
2599 asection **local_sections)
2601 struct elf64_x86_64_link_hash_table *htab;
2602 Elf_Internal_Shdr *symtab_hdr;
2603 struct elf_link_hash_entry **sym_hashes;
2604 bfd_vma *local_got_offsets;
2605 bfd_vma *local_tlsdesc_gotents;
2606 Elf_Internal_Rela *rel;
2607 Elf_Internal_Rela *relend;
2609 BFD_ASSERT (is_x86_64_elf (input_bfd));
2611 htab = elf64_x86_64_hash_table (info);
2612 if (htab == NULL)
2613 return FALSE;
2614 symtab_hdr = &elf_symtab_hdr (input_bfd);
2615 sym_hashes = elf_sym_hashes (input_bfd);
2616 local_got_offsets = elf_local_got_offsets (input_bfd);
2617 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2619 elf64_x86_64_set_tls_module_base (info);
2621 rel = relocs;
2622 relend = relocs + input_section->reloc_count;
2623 for (; rel < relend; rel++)
2625 unsigned int r_type;
2626 reloc_howto_type *howto;
2627 unsigned long r_symndx;
2628 struct elf_link_hash_entry *h;
2629 Elf_Internal_Sym *sym;
2630 asection *sec;
2631 bfd_vma off, offplt;
2632 bfd_vma relocation;
2633 bfd_boolean unresolved_reloc;
2634 bfd_reloc_status_type r;
2635 int tls_type;
2636 asection *base_got;
2638 r_type = ELF64_R_TYPE (rel->r_info);
2639 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2640 || r_type == (int) R_X86_64_GNU_VTENTRY)
2641 continue;
2643 if (r_type >= R_X86_64_max)
2645 bfd_set_error (bfd_error_bad_value);
2646 return FALSE;
2649 howto = x86_64_elf_howto_table + r_type;
2650 r_symndx = ELF64_R_SYM (rel->r_info);
2651 h = NULL;
2652 sym = NULL;
2653 sec = NULL;
2654 unresolved_reloc = FALSE;
2655 if (r_symndx < symtab_hdr->sh_info)
2657 sym = local_syms + r_symndx;
2658 sec = local_sections[r_symndx];
2660 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2661 &sec, rel);
2663 /* Relocate against local STT_GNU_IFUNC symbol. */
2664 if (!info->relocatable
2665 && ELF64_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2667 h = elf64_x86_64_get_local_sym_hash (htab, input_bfd,
2668 rel, FALSE);
2669 if (h == NULL)
2670 abort ();
2672 /* Set STT_GNU_IFUNC symbol value. */
2673 h->root.u.def.value = sym->st_value;
2674 h->root.u.def.section = sec;
2677 else
2679 bfd_boolean warned ATTRIBUTE_UNUSED;
2681 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2682 r_symndx, symtab_hdr, sym_hashes,
2683 h, sec, relocation,
2684 unresolved_reloc, warned);
2687 if (sec != NULL && elf_discarded_section (sec))
2689 /* For relocs against symbols from removed linkonce sections,
2690 or sections discarded by a linker script, we just want the
2691 section contents zeroed. Avoid any special processing. */
2692 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2693 rel->r_info = 0;
2694 rel->r_addend = 0;
2695 continue;
2698 if (info->relocatable)
2699 continue;
2701 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2702 it here if it is defined in a non-shared object. */
2703 if (h != NULL
2704 && h->type == STT_GNU_IFUNC
2705 && h->def_regular)
2707 asection *plt;
2708 bfd_vma plt_index;
2709 const char *name;
2711 if ((input_section->flags & SEC_ALLOC) == 0
2712 || h->plt.offset == (bfd_vma) -1)
2713 abort ();
2715 /* STT_GNU_IFUNC symbol must go through PLT. */
2716 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2717 relocation = (plt->output_section->vma
2718 + plt->output_offset + h->plt.offset);
2720 switch (r_type)
2722 default:
2723 if (h->root.root.string)
2724 name = h->root.root.string;
2725 else
2726 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2727 NULL);
2728 (*_bfd_error_handler)
2729 (_("%B: relocation %s against STT_GNU_IFUNC "
2730 "symbol `%s' isn't handled by %s"), input_bfd,
2731 x86_64_elf_howto_table[r_type].name,
2732 name, __FUNCTION__);
2733 bfd_set_error (bfd_error_bad_value);
2734 return FALSE;
2736 case R_X86_64_32S:
2737 if (info->shared)
2738 abort ();
2739 goto do_relocation;
2741 case R_X86_64_64:
2742 if (rel->r_addend != 0)
2744 if (h->root.root.string)
2745 name = h->root.root.string;
2746 else
2747 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2748 sym, NULL);
2749 (*_bfd_error_handler)
2750 (_("%B: relocation %s against STT_GNU_IFUNC "
2751 "symbol `%s' has non-zero addend: %d"),
2752 input_bfd, x86_64_elf_howto_table[r_type].name,
2753 name, rel->r_addend);
2754 bfd_set_error (bfd_error_bad_value);
2755 return FALSE;
2758 /* Generate dynamic relcoation only when there is a
2759 non-GOF reference in a shared object. */
2760 if (info->shared && h->non_got_ref)
2762 Elf_Internal_Rela outrel;
2763 asection *sreloc;
2765 /* Need a dynamic relocation to get the real function
2766 address. */
2767 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2768 info,
2769 input_section,
2770 rel->r_offset);
2771 if (outrel.r_offset == (bfd_vma) -1
2772 || outrel.r_offset == (bfd_vma) -2)
2773 abort ();
2775 outrel.r_offset += (input_section->output_section->vma
2776 + input_section->output_offset);
2778 if (h->dynindx == -1
2779 || h->forced_local
2780 || info->executable)
2782 /* This symbol is resolved locally. */
2783 outrel.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
2784 outrel.r_addend = (h->root.u.def.value
2785 + h->root.u.def.section->output_section->vma
2786 + h->root.u.def.section->output_offset);
2788 else
2790 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2791 outrel.r_addend = 0;
2794 sreloc = htab->elf.irelifunc;
2795 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
2797 /* If this reloc is against an external symbol, we
2798 do not want to fiddle with the addend. Otherwise,
2799 we need to include the symbol value so that it
2800 becomes an addend for the dynamic reloc. For an
2801 internal symbol, we have updated addend. */
2802 continue;
2805 case R_X86_64_32:
2806 case R_X86_64_PC32:
2807 case R_X86_64_PC64:
2808 case R_X86_64_PLT32:
2809 goto do_relocation;
2811 case R_X86_64_GOTPCREL:
2812 case R_X86_64_GOTPCREL64:
2813 base_got = htab->elf.sgot;
2814 off = h->got.offset;
2816 if (base_got == NULL)
2817 abort ();
2819 if (off == (bfd_vma) -1)
2821 /* We can't use h->got.offset here to save state, or
2822 even just remember the offset, as finish_dynamic_symbol
2823 would use that as offset into .got. */
2825 if (htab->elf.splt != NULL)
2827 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2828 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2829 base_got = htab->elf.sgotplt;
2831 else
2833 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2834 off = plt_index * GOT_ENTRY_SIZE;
2835 base_got = htab->elf.igotplt;
2838 if (h->dynindx == -1
2839 || h->forced_local
2840 || info->symbolic)
2842 /* This references the local defitionion. We must
2843 initialize this entry in the global offset table.
2844 Since the offset must always be a multiple of 8,
2845 we use the least significant bit to record
2846 whether we have initialized it already.
2848 When doing a dynamic link, we create a .rela.got
2849 relocation entry to initialize the value. This
2850 is done in the finish_dynamic_symbol routine. */
2851 if ((off & 1) != 0)
2852 off &= ~1;
2853 else
2855 bfd_put_64 (output_bfd, relocation,
2856 base_got->contents + off);
2857 /* Note that this is harmless for the GOTPLT64
2858 case, as -1 | 1 still is -1. */
2859 h->got.offset |= 1;
2864 relocation = (base_got->output_section->vma
2865 + base_got->output_offset + off);
2867 if (r_type != R_X86_64_GOTPCREL
2868 && r_type != R_X86_64_GOTPCREL64)
2870 asection *gotplt;
2871 if (htab->elf.splt != NULL)
2872 gotplt = htab->elf.sgotplt;
2873 else
2874 gotplt = htab->elf.igotplt;
2875 relocation -= (gotplt->output_section->vma
2876 - gotplt->output_offset);
2879 goto do_relocation;
2883 /* When generating a shared object, the relocations handled here are
2884 copied into the output file to be resolved at run time. */
2885 switch (r_type)
2887 case R_X86_64_GOT32:
2888 case R_X86_64_GOT64:
2889 /* Relocation is to the entry for this symbol in the global
2890 offset table. */
2891 case R_X86_64_GOTPCREL:
2892 case R_X86_64_GOTPCREL64:
2893 /* Use global offset table entry as symbol value. */
2894 case R_X86_64_GOTPLT64:
2895 /* This is the same as GOT64 for relocation purposes, but
2896 indicates the existence of a PLT entry. The difficulty is,
2897 that we must calculate the GOT slot offset from the PLT
2898 offset, if this symbol got a PLT entry (it was global).
2899 Additionally if it's computed from the PLT entry, then that
2900 GOT offset is relative to .got.plt, not to .got. */
2901 base_got = htab->elf.sgot;
2903 if (htab->elf.sgot == NULL)
2904 abort ();
2906 if (h != NULL)
2908 bfd_boolean dyn;
2910 off = h->got.offset;
2911 if (h->needs_plt
2912 && h->plt.offset != (bfd_vma)-1
2913 && off == (bfd_vma)-1)
2915 /* We can't use h->got.offset here to save
2916 state, or even just remember the offset, as
2917 finish_dynamic_symbol would use that as offset into
2918 .got. */
2919 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2920 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2921 base_got = htab->elf.sgotplt;
2924 dyn = htab->elf.dynamic_sections_created;
2926 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2927 || (info->shared
2928 && SYMBOL_REFERENCES_LOCAL (info, h))
2929 || (ELF_ST_VISIBILITY (h->other)
2930 && h->root.type == bfd_link_hash_undefweak))
2932 /* This is actually a static link, or it is a -Bsymbolic
2933 link and the symbol is defined locally, or the symbol
2934 was forced to be local because of a version file. We
2935 must initialize this entry in the global offset table.
2936 Since the offset must always be a multiple of 8, we
2937 use the least significant bit to record whether we
2938 have initialized it already.
2940 When doing a dynamic link, we create a .rela.got
2941 relocation entry to initialize the value. This is
2942 done in the finish_dynamic_symbol routine. */
2943 if ((off & 1) != 0)
2944 off &= ~1;
2945 else
2947 bfd_put_64 (output_bfd, relocation,
2948 base_got->contents + off);
2949 /* Note that this is harmless for the GOTPLT64 case,
2950 as -1 | 1 still is -1. */
2951 h->got.offset |= 1;
2954 else
2955 unresolved_reloc = FALSE;
2957 else
2959 if (local_got_offsets == NULL)
2960 abort ();
2962 off = local_got_offsets[r_symndx];
2964 /* The offset must always be a multiple of 8. We use
2965 the least significant bit to record whether we have
2966 already generated the necessary reloc. */
2967 if ((off & 1) != 0)
2968 off &= ~1;
2969 else
2971 bfd_put_64 (output_bfd, relocation,
2972 base_got->contents + off);
2974 if (info->shared)
2976 asection *s;
2977 Elf_Internal_Rela outrel;
2979 /* We need to generate a R_X86_64_RELATIVE reloc
2980 for the dynamic linker. */
2981 s = htab->elf.srelgot;
2982 if (s == NULL)
2983 abort ();
2985 outrel.r_offset = (base_got->output_section->vma
2986 + base_got->output_offset
2987 + off);
2988 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2989 outrel.r_addend = relocation;
2990 elf64_x86_64_append_rela (output_bfd, s, &outrel);
2993 local_got_offsets[r_symndx] |= 1;
2997 if (off >= (bfd_vma) -2)
2998 abort ();
3000 relocation = base_got->output_section->vma
3001 + base_got->output_offset + off;
3002 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
3003 relocation -= htab->elf.sgotplt->output_section->vma
3004 - htab->elf.sgotplt->output_offset;
3006 break;
3008 case R_X86_64_GOTOFF64:
3009 /* Relocation is relative to the start of the global offset
3010 table. */
3012 /* Check to make sure it isn't a protected function symbol
3013 for shared library since it may not be local when used
3014 as function address. */
3015 if (info->shared
3016 && h
3017 && h->def_regular
3018 && h->type == STT_FUNC
3019 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3021 (*_bfd_error_handler)
3022 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3023 input_bfd, h->root.root.string);
3024 bfd_set_error (bfd_error_bad_value);
3025 return FALSE;
3028 /* Note that sgot is not involved in this
3029 calculation. We always want the start of .got.plt. If we
3030 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3031 permitted by the ABI, we might have to change this
3032 calculation. */
3033 relocation -= htab->elf.sgotplt->output_section->vma
3034 + htab->elf.sgotplt->output_offset;
3035 break;
3037 case R_X86_64_GOTPC32:
3038 case R_X86_64_GOTPC64:
3039 /* Use global offset table as symbol value. */
3040 relocation = htab->elf.sgotplt->output_section->vma
3041 + htab->elf.sgotplt->output_offset;
3042 unresolved_reloc = FALSE;
3043 break;
3045 case R_X86_64_PLTOFF64:
3046 /* Relocation is PLT entry relative to GOT. For local
3047 symbols it's the symbol itself relative to GOT. */
3048 if (h != NULL
3049 /* See PLT32 handling. */
3050 && h->plt.offset != (bfd_vma) -1
3051 && htab->elf.splt != NULL)
3053 relocation = (htab->elf.splt->output_section->vma
3054 + htab->elf.splt->output_offset
3055 + h->plt.offset);
3056 unresolved_reloc = FALSE;
3059 relocation -= htab->elf.sgotplt->output_section->vma
3060 + htab->elf.sgotplt->output_offset;
3061 break;
3063 case R_X86_64_PLT32:
3064 /* Relocation is to the entry for this symbol in the
3065 procedure linkage table. */
3067 /* Resolve a PLT32 reloc against a local symbol directly,
3068 without using the procedure linkage table. */
3069 if (h == NULL)
3070 break;
3072 if (h->plt.offset == (bfd_vma) -1
3073 || htab->elf.splt == NULL)
3075 /* We didn't make a PLT entry for this symbol. This
3076 happens when statically linking PIC code, or when
3077 using -Bsymbolic. */
3078 break;
3081 relocation = (htab->elf.splt->output_section->vma
3082 + htab->elf.splt->output_offset
3083 + h->plt.offset);
3084 unresolved_reloc = FALSE;
3085 break;
3087 case R_X86_64_PC8:
3088 case R_X86_64_PC16:
3089 case R_X86_64_PC32:
3090 if (info->shared
3091 && (input_section->flags & SEC_ALLOC) != 0
3092 && (input_section->flags & SEC_READONLY) != 0
3093 && h != NULL)
3095 bfd_boolean fail = FALSE;
3096 bfd_boolean branch
3097 = (r_type == R_X86_64_PC32
3098 && is_32bit_relative_branch (contents, rel->r_offset));
3100 if (SYMBOL_REFERENCES_LOCAL (info, h))
3102 /* Symbol is referenced locally. Make sure it is
3103 defined locally or for a branch. */
3104 fail = !h->def_regular && !branch;
3106 else
3108 /* Symbol isn't referenced locally. We only allow
3109 branch to symbol with non-default visibility. */
3110 fail = (!branch
3111 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3114 if (fail)
3116 const char *fmt;
3117 const char *v;
3118 const char *pic = "";
3120 switch (ELF_ST_VISIBILITY (h->other))
3122 case STV_HIDDEN:
3123 v = _("hidden symbol");
3124 break;
3125 case STV_INTERNAL:
3126 v = _("internal symbol");
3127 break;
3128 case STV_PROTECTED:
3129 v = _("protected symbol");
3130 break;
3131 default:
3132 v = _("symbol");
3133 pic = _("; recompile with -fPIC");
3134 break;
3137 if (h->def_regular)
3138 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3139 else
3140 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3142 (*_bfd_error_handler) (fmt, input_bfd,
3143 x86_64_elf_howto_table[r_type].name,
3144 v, h->root.root.string, pic);
3145 bfd_set_error (bfd_error_bad_value);
3146 return FALSE;
3149 /* Fall through. */
3151 case R_X86_64_8:
3152 case R_X86_64_16:
3153 case R_X86_64_32:
3154 case R_X86_64_PC64:
3155 case R_X86_64_64:
3156 /* FIXME: The ABI says the linker should make sure the value is
3157 the same when it's zeroextended to 64 bit. */
3159 if ((input_section->flags & SEC_ALLOC) == 0)
3160 break;
3162 if ((info->shared
3163 && (h == NULL
3164 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3165 || h->root.type != bfd_link_hash_undefweak)
3166 && (! IS_X86_64_PCREL_TYPE (r_type)
3167 || ! SYMBOL_CALLS_LOCAL (info, h)))
3168 || (ELIMINATE_COPY_RELOCS
3169 && !info->shared
3170 && h != NULL
3171 && h->dynindx != -1
3172 && !h->non_got_ref
3173 && ((h->def_dynamic
3174 && !h->def_regular)
3175 || h->root.type == bfd_link_hash_undefweak
3176 || h->root.type == bfd_link_hash_undefined)))
3178 Elf_Internal_Rela outrel;
3179 bfd_boolean skip, relocate;
3180 asection *sreloc;
3182 /* When generating a shared object, these relocations
3183 are copied into the output file to be resolved at run
3184 time. */
3185 skip = FALSE;
3186 relocate = FALSE;
3188 outrel.r_offset =
3189 _bfd_elf_section_offset (output_bfd, info, input_section,
3190 rel->r_offset);
3191 if (outrel.r_offset == (bfd_vma) -1)
3192 skip = TRUE;
3193 else if (outrel.r_offset == (bfd_vma) -2)
3194 skip = TRUE, relocate = TRUE;
3196 outrel.r_offset += (input_section->output_section->vma
3197 + input_section->output_offset);
3199 if (skip)
3200 memset (&outrel, 0, sizeof outrel);
3202 /* h->dynindx may be -1 if this symbol was marked to
3203 become local. */
3204 else if (h != NULL
3205 && h->dynindx != -1
3206 && (IS_X86_64_PCREL_TYPE (r_type)
3207 || ! info->shared
3208 || ! SYMBOLIC_BIND (info, h)
3209 || ! h->def_regular))
3211 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3212 outrel.r_addend = rel->r_addend;
3214 else
3216 /* This symbol is local, or marked to become local. */
3217 if (r_type == R_X86_64_64)
3219 relocate = TRUE;
3220 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3221 outrel.r_addend = relocation + rel->r_addend;
3223 else
3225 long sindx;
3227 if (bfd_is_abs_section (sec))
3228 sindx = 0;
3229 else if (sec == NULL || sec->owner == NULL)
3231 bfd_set_error (bfd_error_bad_value);
3232 return FALSE;
3234 else
3236 asection *osec;
3238 /* We are turning this relocation into one
3239 against a section symbol. It would be
3240 proper to subtract the symbol's value,
3241 osec->vma, from the emitted reloc addend,
3242 but ld.so expects buggy relocs. */
3243 osec = sec->output_section;
3244 sindx = elf_section_data (osec)->dynindx;
3245 if (sindx == 0)
3247 asection *oi = htab->elf.text_index_section;
3248 sindx = elf_section_data (oi)->dynindx;
3250 BFD_ASSERT (sindx != 0);
3253 outrel.r_info = ELF64_R_INFO (sindx, r_type);
3254 outrel.r_addend = relocation + rel->r_addend;
3258 sreloc = elf_section_data (input_section)->sreloc;
3260 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3262 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3264 /* If this reloc is against an external symbol, we do
3265 not want to fiddle with the addend. Otherwise, we
3266 need to include the symbol value so that it becomes
3267 an addend for the dynamic reloc. */
3268 if (! relocate)
3269 continue;
3272 break;
3274 case R_X86_64_TLSGD:
3275 case R_X86_64_GOTPC32_TLSDESC:
3276 case R_X86_64_TLSDESC_CALL:
3277 case R_X86_64_GOTTPOFF:
3278 tls_type = GOT_UNKNOWN;
3279 if (h == NULL && local_got_offsets)
3280 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3281 else if (h != NULL)
3282 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
3284 if (! elf64_x86_64_tls_transition (info, input_bfd,
3285 input_section, contents,
3286 symtab_hdr, sym_hashes,
3287 &r_type, tls_type, rel,
3288 relend, h, r_symndx))
3289 return FALSE;
3291 if (r_type == R_X86_64_TPOFF32)
3293 bfd_vma roff = rel->r_offset;
3295 BFD_ASSERT (! unresolved_reloc);
3297 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3299 /* GD->LE transition.
3300 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3301 .word 0x6666; rex64; call __tls_get_addr
3302 Change it into:
3303 movq %fs:0, %rax
3304 leaq foo@tpoff(%rax), %rax */
3305 memcpy (contents + roff - 4,
3306 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3307 16);
3308 bfd_put_32 (output_bfd,
3309 elf64_x86_64_tpoff (info, relocation),
3310 contents + roff + 8);
3311 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3312 rel++;
3313 continue;
3315 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3317 /* GDesc -> LE transition.
3318 It's originally something like:
3319 leaq x@tlsdesc(%rip), %rax
3321 Change it to:
3322 movl $x@tpoff, %rax. */
3324 unsigned int val, type;
3326 type = bfd_get_8 (input_bfd, contents + roff - 3);
3327 val = bfd_get_8 (input_bfd, contents + roff - 1);
3328 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3329 contents + roff - 3);
3330 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3331 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3332 contents + roff - 1);
3333 bfd_put_32 (output_bfd,
3334 elf64_x86_64_tpoff (info, relocation),
3335 contents + roff);
3336 continue;
3338 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3340 /* GDesc -> LE transition.
3341 It's originally:
3342 call *(%rax)
3343 Turn it into:
3344 xchg %ax,%ax. */
3345 bfd_put_8 (output_bfd, 0x66, contents + roff);
3346 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3347 continue;
3349 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3351 /* IE->LE transition:
3352 Originally it can be one of:
3353 movq foo@gottpoff(%rip), %reg
3354 addq foo@gottpoff(%rip), %reg
3355 We change it into:
3356 movq $foo, %reg
3357 leaq foo(%reg), %reg
3358 addq $foo, %reg. */
3360 unsigned int val, type, reg;
3362 val = bfd_get_8 (input_bfd, contents + roff - 3);
3363 type = bfd_get_8 (input_bfd, contents + roff - 2);
3364 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3365 reg >>= 3;
3366 if (type == 0x8b)
3368 /* movq */
3369 if (val == 0x4c)
3370 bfd_put_8 (output_bfd, 0x49,
3371 contents + roff - 3);
3372 bfd_put_8 (output_bfd, 0xc7,
3373 contents + roff - 2);
3374 bfd_put_8 (output_bfd, 0xc0 | reg,
3375 contents + roff - 1);
3377 else if (reg == 4)
3379 /* addq -> addq - addressing with %rsp/%r12 is
3380 special */
3381 if (val == 0x4c)
3382 bfd_put_8 (output_bfd, 0x49,
3383 contents + roff - 3);
3384 bfd_put_8 (output_bfd, 0x81,
3385 contents + roff - 2);
3386 bfd_put_8 (output_bfd, 0xc0 | reg,
3387 contents + roff - 1);
3389 else
3391 /* addq -> leaq */
3392 if (val == 0x4c)
3393 bfd_put_8 (output_bfd, 0x4d,
3394 contents + roff - 3);
3395 bfd_put_8 (output_bfd, 0x8d,
3396 contents + roff - 2);
3397 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3398 contents + roff - 1);
3400 bfd_put_32 (output_bfd,
3401 elf64_x86_64_tpoff (info, relocation),
3402 contents + roff);
3403 continue;
3405 else
3406 BFD_ASSERT (FALSE);
3409 if (htab->elf.sgot == NULL)
3410 abort ();
3412 if (h != NULL)
3414 off = h->got.offset;
3415 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
3417 else
3419 if (local_got_offsets == NULL)
3420 abort ();
3422 off = local_got_offsets[r_symndx];
3423 offplt = local_tlsdesc_gotents[r_symndx];
3426 if ((off & 1) != 0)
3427 off &= ~1;
3428 else
3430 Elf_Internal_Rela outrel;
3431 int dr_type, indx;
3432 asection *sreloc;
3434 if (htab->elf.srelgot == NULL)
3435 abort ();
3437 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3439 if (GOT_TLS_GDESC_P (tls_type))
3441 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
3442 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3443 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3444 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3445 + htab->elf.sgotplt->output_offset
3446 + offplt
3447 + htab->sgotplt_jump_table_size);
3448 sreloc = htab->elf.srelplt;
3449 if (indx == 0)
3450 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3451 else
3452 outrel.r_addend = 0;
3453 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3456 sreloc = htab->elf.srelgot;
3458 outrel.r_offset = (htab->elf.sgot->output_section->vma
3459 + htab->elf.sgot->output_offset + off);
3461 if (GOT_TLS_GD_P (tls_type))
3462 dr_type = R_X86_64_DTPMOD64;
3463 else if (GOT_TLS_GDESC_P (tls_type))
3464 goto dr_done;
3465 else
3466 dr_type = R_X86_64_TPOFF64;
3468 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3469 outrel.r_addend = 0;
3470 if ((dr_type == R_X86_64_TPOFF64
3471 || dr_type == R_X86_64_TLSDESC) && indx == 0)
3472 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3473 outrel.r_info = ELF64_R_INFO (indx, dr_type);
3475 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3477 if (GOT_TLS_GD_P (tls_type))
3479 if (indx == 0)
3481 BFD_ASSERT (! unresolved_reloc);
3482 bfd_put_64 (output_bfd,
3483 relocation - elf64_x86_64_dtpoff_base (info),
3484 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3486 else
3488 bfd_put_64 (output_bfd, 0,
3489 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3490 outrel.r_info = ELF64_R_INFO (indx,
3491 R_X86_64_DTPOFF64);
3492 outrel.r_offset += GOT_ENTRY_SIZE;
3493 elf64_x86_64_append_rela (output_bfd, sreloc,
3494 &outrel);
3498 dr_done:
3499 if (h != NULL)
3500 h->got.offset |= 1;
3501 else
3502 local_got_offsets[r_symndx] |= 1;
3505 if (off >= (bfd_vma) -2
3506 && ! GOT_TLS_GDESC_P (tls_type))
3507 abort ();
3508 if (r_type == ELF64_R_TYPE (rel->r_info))
3510 if (r_type == R_X86_64_GOTPC32_TLSDESC
3511 || r_type == R_X86_64_TLSDESC_CALL)
3512 relocation = htab->elf.sgotplt->output_section->vma
3513 + htab->elf.sgotplt->output_offset
3514 + offplt + htab->sgotplt_jump_table_size;
3515 else
3516 relocation = htab->elf.sgot->output_section->vma
3517 + htab->elf.sgot->output_offset + off;
3518 unresolved_reloc = FALSE;
3520 else
3522 bfd_vma roff = rel->r_offset;
3524 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3526 /* GD->IE transition.
3527 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3528 .word 0x6666; rex64; call __tls_get_addr@plt
3529 Change it into:
3530 movq %fs:0, %rax
3531 addq foo@gottpoff(%rip), %rax */
3532 memcpy (contents + roff - 4,
3533 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3534 16);
3536 relocation = (htab->elf.sgot->output_section->vma
3537 + htab->elf.sgot->output_offset + off
3538 - roff
3539 - input_section->output_section->vma
3540 - input_section->output_offset
3541 - 12);
3542 bfd_put_32 (output_bfd, relocation,
3543 contents + roff + 8);
3544 /* Skip R_X86_64_PLT32. */
3545 rel++;
3546 continue;
3548 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3550 /* GDesc -> IE transition.
3551 It's originally something like:
3552 leaq x@tlsdesc(%rip), %rax
3554 Change it to:
3555 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
3557 /* Now modify the instruction as appropriate. To
3558 turn a leaq into a movq in the form we use it, it
3559 suffices to change the second byte from 0x8d to
3560 0x8b. */
3561 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3563 bfd_put_32 (output_bfd,
3564 htab->elf.sgot->output_section->vma
3565 + htab->elf.sgot->output_offset + off
3566 - rel->r_offset
3567 - input_section->output_section->vma
3568 - input_section->output_offset
3569 - 4,
3570 contents + roff);
3571 continue;
3573 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3575 /* GDesc -> IE transition.
3576 It's originally:
3577 call *(%rax)
3579 Change it to:
3580 xchg %ax, %ax. */
3582 bfd_put_8 (output_bfd, 0x66, contents + roff);
3583 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3584 continue;
3586 else
3587 BFD_ASSERT (FALSE);
3589 break;
3591 case R_X86_64_TLSLD:
3592 if (! elf64_x86_64_tls_transition (info, input_bfd,
3593 input_section, contents,
3594 symtab_hdr, sym_hashes,
3595 &r_type, GOT_UNKNOWN,
3596 rel, relend, h, r_symndx))
3597 return FALSE;
3599 if (r_type != R_X86_64_TLSLD)
3601 /* LD->LE transition:
3602 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3603 We change it into:
3604 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3606 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3607 memcpy (contents + rel->r_offset - 3,
3608 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3609 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3610 rel++;
3611 continue;
3614 if (htab->elf.sgot == NULL)
3615 abort ();
3617 off = htab->tls_ld_got.offset;
3618 if (off & 1)
3619 off &= ~1;
3620 else
3622 Elf_Internal_Rela outrel;
3624 if (htab->elf.srelgot == NULL)
3625 abort ();
3627 outrel.r_offset = (htab->elf.sgot->output_section->vma
3628 + htab->elf.sgot->output_offset + off);
3630 bfd_put_64 (output_bfd, 0,
3631 htab->elf.sgot->contents + off);
3632 bfd_put_64 (output_bfd, 0,
3633 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3634 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3635 outrel.r_addend = 0;
3636 elf64_x86_64_append_rela (output_bfd, htab->elf.srelgot,
3637 &outrel);
3638 htab->tls_ld_got.offset |= 1;
3640 relocation = htab->elf.sgot->output_section->vma
3641 + htab->elf.sgot->output_offset + off;
3642 unresolved_reloc = FALSE;
3643 break;
3645 case R_X86_64_DTPOFF32:
3646 if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
3647 relocation -= elf64_x86_64_dtpoff_base (info);
3648 else
3649 relocation = elf64_x86_64_tpoff (info, relocation);
3650 break;
3652 case R_X86_64_TPOFF32:
3653 BFD_ASSERT (info->executable);
3654 relocation = elf64_x86_64_tpoff (info, relocation);
3655 break;
3657 default:
3658 break;
3661 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3662 because such sections are not SEC_ALLOC and thus ld.so will
3663 not process them. */
3664 if (unresolved_reloc
3665 && !((input_section->flags & SEC_DEBUGGING) != 0
3666 && h->def_dynamic))
3667 (*_bfd_error_handler)
3668 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3669 input_bfd,
3670 input_section,
3671 (long) rel->r_offset,
3672 howto->name,
3673 h->root.root.string);
3675 do_relocation:
3676 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3677 contents, rel->r_offset,
3678 relocation, rel->r_addend);
3680 if (r != bfd_reloc_ok)
3682 const char *name;
3684 if (h != NULL)
3685 name = h->root.root.string;
3686 else
3688 name = bfd_elf_string_from_elf_section (input_bfd,
3689 symtab_hdr->sh_link,
3690 sym->st_name);
3691 if (name == NULL)
3692 return FALSE;
3693 if (*name == '\0')
3694 name = bfd_section_name (input_bfd, sec);
3697 if (r == bfd_reloc_overflow)
3699 if (! ((*info->callbacks->reloc_overflow)
3700 (info, (h ? &h->root : NULL), name, howto->name,
3701 (bfd_vma) 0, input_bfd, input_section,
3702 rel->r_offset)))
3703 return FALSE;
3705 else
3707 (*_bfd_error_handler)
3708 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3709 input_bfd, input_section,
3710 (long) rel->r_offset, name, (int) r);
3711 return FALSE;
3716 return TRUE;
3719 /* Finish up dynamic symbol handling. We set the contents of various
3720 dynamic sections here. */
3722 static bfd_boolean
3723 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3724 struct bfd_link_info *info,
3725 struct elf_link_hash_entry *h,
3726 Elf_Internal_Sym *sym)
3728 struct elf64_x86_64_link_hash_table *htab;
3730 htab = elf64_x86_64_hash_table (info);
3731 if (htab == NULL)
3732 return FALSE;
3734 if (h->plt.offset != (bfd_vma) -1)
3736 bfd_vma plt_index;
3737 bfd_vma got_offset;
3738 Elf_Internal_Rela rela;
3739 bfd_byte *loc;
3740 asection *plt, *gotplt, *relplt;
3742 /* When building a static executable, use .iplt, .igot.plt and
3743 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3744 if (htab->elf.splt != NULL)
3746 plt = htab->elf.splt;
3747 gotplt = htab->elf.sgotplt;
3748 relplt = htab->elf.srelplt;
3750 else
3752 plt = htab->elf.iplt;
3753 gotplt = htab->elf.igotplt;
3754 relplt = htab->elf.irelplt;
3757 /* This symbol has an entry in the procedure linkage table. Set
3758 it up. */
3759 if ((h->dynindx == -1
3760 && !((h->forced_local || info->executable)
3761 && h->def_regular
3762 && h->type == STT_GNU_IFUNC))
3763 || plt == NULL
3764 || gotplt == NULL
3765 || relplt == NULL)
3766 abort ();
3768 /* Get the index in the procedure linkage table which
3769 corresponds to this symbol. This is the index of this symbol
3770 in all the symbols for which we are making plt entries. The
3771 first entry in the procedure linkage table is reserved.
3773 Get the offset into the .got table of the entry that
3774 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3775 bytes. The first three are reserved for the dynamic linker.
3777 For static executables, we don't reserve anything. */
3779 if (plt == htab->elf.splt)
3781 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3782 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3784 else
3786 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3787 got_offset = plt_index * GOT_ENTRY_SIZE;
3790 /* Fill in the entry in the procedure linkage table. */
3791 memcpy (plt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3792 PLT_ENTRY_SIZE);
3794 /* Insert the relocation positions of the plt section. The magic
3795 numbers at the end of the statements are the positions of the
3796 relocations in the plt section. */
3797 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3798 instruction uses 6 bytes, subtract this value. */
3799 bfd_put_32 (output_bfd,
3800 (gotplt->output_section->vma
3801 + gotplt->output_offset
3802 + got_offset
3803 - plt->output_section->vma
3804 - plt->output_offset
3805 - h->plt.offset
3806 - 6),
3807 plt->contents + h->plt.offset + 2);
3809 /* Don't fill PLT entry for static executables. */
3810 if (plt == htab->elf.splt)
3812 /* Put relocation index. */
3813 bfd_put_32 (output_bfd, plt_index,
3814 plt->contents + h->plt.offset + 7);
3815 /* Put offset for jmp .PLT0. */
3816 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3817 plt->contents + h->plt.offset + 12);
3820 /* Fill in the entry in the global offset table, initially this
3821 points to the pushq instruction in the PLT which is at offset 6. */
3822 bfd_put_64 (output_bfd, (plt->output_section->vma
3823 + plt->output_offset
3824 + h->plt.offset + 6),
3825 gotplt->contents + got_offset);
3827 /* Fill in the entry in the .rela.plt section. */
3828 rela.r_offset = (gotplt->output_section->vma
3829 + gotplt->output_offset
3830 + got_offset);
3831 if (h->dynindx == -1
3832 || ((info->executable
3833 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3834 && h->def_regular
3835 && h->type == STT_GNU_IFUNC))
3837 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3838 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
3839 rela.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
3840 rela.r_addend = (h->root.u.def.value
3841 + h->root.u.def.section->output_section->vma
3842 + h->root.u.def.section->output_offset);
3844 else
3846 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3847 rela.r_addend = 0;
3849 loc = relplt->contents + plt_index * sizeof (Elf64_External_Rela);
3850 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3852 if (!h->def_regular)
3854 /* Mark the symbol as undefined, rather than as defined in
3855 the .plt section. Leave the value if there were any
3856 relocations where pointer equality matters (this is a clue
3857 for the dynamic linker, to make function pointer
3858 comparisons work between an application and shared
3859 library), otherwise set it to zero. If a function is only
3860 called from a binary, there is no need to slow down
3861 shared libraries because of that. */
3862 sym->st_shndx = SHN_UNDEF;
3863 if (!h->pointer_equality_needed)
3864 sym->st_value = 0;
3868 if (h->got.offset != (bfd_vma) -1
3869 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3870 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3872 Elf_Internal_Rela rela;
3874 /* This symbol has an entry in the global offset table. Set it
3875 up. */
3876 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3877 abort ();
3879 rela.r_offset = (htab->elf.sgot->output_section->vma
3880 + htab->elf.sgot->output_offset
3881 + (h->got.offset &~ (bfd_vma) 1));
3883 /* If this is a static link, or it is a -Bsymbolic link and the
3884 symbol is defined locally or was forced to be local because
3885 of a version file, we just want to emit a RELATIVE reloc.
3886 The entry in the global offset table will already have been
3887 initialized in the relocate_section function. */
3888 if (h->def_regular
3889 && h->type == STT_GNU_IFUNC)
3891 if (info->shared)
3893 /* Generate R_X86_64_GLOB_DAT. */
3894 goto do_glob_dat;
3896 else
3898 asection *plt;
3900 if (!h->pointer_equality_needed)
3901 abort ();
3903 /* For non-shared object, we can't use .got.plt, which
3904 contains the real function addres if we need pointer
3905 equality. We load the GOT entry with the PLT entry. */
3906 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3907 bfd_put_64 (output_bfd, (plt->output_section->vma
3908 + plt->output_offset
3909 + h->plt.offset),
3910 htab->elf.sgot->contents + h->got.offset);
3911 return TRUE;
3914 else if (info->shared
3915 && SYMBOL_REFERENCES_LOCAL (info, h))
3917 if (!h->def_regular)
3918 return FALSE;
3919 BFD_ASSERT((h->got.offset & 1) != 0);
3920 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3921 rela.r_addend = (h->root.u.def.value
3922 + h->root.u.def.section->output_section->vma
3923 + h->root.u.def.section->output_offset);
3925 else
3927 BFD_ASSERT((h->got.offset & 1) == 0);
3928 do_glob_dat:
3929 bfd_put_64 (output_bfd, (bfd_vma) 0,
3930 htab->elf.sgot->contents + h->got.offset);
3931 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3932 rela.r_addend = 0;
3935 elf64_x86_64_append_rela (output_bfd, htab->elf.srelgot, &rela);
3938 if (h->needs_copy)
3940 Elf_Internal_Rela rela;
3942 /* This symbol needs a copy reloc. Set it up. */
3944 if (h->dynindx == -1
3945 || (h->root.type != bfd_link_hash_defined
3946 && h->root.type != bfd_link_hash_defweak)
3947 || htab->srelbss == NULL)
3948 abort ();
3950 rela.r_offset = (h->root.u.def.value
3951 + h->root.u.def.section->output_section->vma
3952 + h->root.u.def.section->output_offset);
3953 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3954 rela.r_addend = 0;
3955 elf64_x86_64_append_rela (output_bfd, htab->srelbss, &rela);
3958 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
3959 be NULL for local symbols. */
3960 if (sym != NULL
3961 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
3962 || h == htab->elf.hgot))
3963 sym->st_shndx = SHN_ABS;
3965 return TRUE;
3968 /* Finish up local dynamic symbol handling. We set the contents of
3969 various dynamic sections here. */
3971 static bfd_boolean
3972 elf64_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
3974 struct elf_link_hash_entry *h
3975 = (struct elf_link_hash_entry *) *slot;
3976 struct bfd_link_info *info
3977 = (struct bfd_link_info *) inf;
3979 return elf64_x86_64_finish_dynamic_symbol (info->output_bfd,
3980 info, h, NULL);
3983 /* Used to decide how to sort relocs in an optimal manner for the
3984 dynamic linker, before writing them out. */
3986 static enum elf_reloc_type_class
3987 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
3989 switch ((int) ELF64_R_TYPE (rela->r_info))
3991 case R_X86_64_RELATIVE:
3992 return reloc_class_relative;
3993 case R_X86_64_JUMP_SLOT:
3994 return reloc_class_plt;
3995 case R_X86_64_COPY:
3996 return reloc_class_copy;
3997 default:
3998 return reloc_class_normal;
4002 /* Finish up the dynamic sections. */
4004 static bfd_boolean
4005 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
4007 struct elf64_x86_64_link_hash_table *htab;
4008 bfd *dynobj;
4009 asection *sdyn;
4011 htab = elf64_x86_64_hash_table (info);
4012 if (htab == NULL)
4013 return FALSE;
4015 dynobj = htab->elf.dynobj;
4016 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4018 if (htab->elf.dynamic_sections_created)
4020 Elf64_External_Dyn *dyncon, *dynconend;
4022 if (sdyn == NULL || htab->elf.sgot == NULL)
4023 abort ();
4025 dyncon = (Elf64_External_Dyn *) sdyn->contents;
4026 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
4027 for (; dyncon < dynconend; dyncon++)
4029 Elf_Internal_Dyn dyn;
4030 asection *s;
4032 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4034 switch (dyn.d_tag)
4036 default:
4037 continue;
4039 case DT_PLTGOT:
4040 s = htab->elf.sgotplt;
4041 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4042 break;
4044 case DT_JMPREL:
4045 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
4046 break;
4048 case DT_PLTRELSZ:
4049 s = htab->elf.srelplt->output_section;
4050 dyn.d_un.d_val = s->size;
4051 break;
4053 case DT_RELASZ:
4054 /* The procedure linkage table relocs (DT_JMPREL) should
4055 not be included in the overall relocs (DT_RELA).
4056 Therefore, we override the DT_RELASZ entry here to
4057 make it not include the JMPREL relocs. Since the
4058 linker script arranges for .rela.plt to follow all
4059 other relocation sections, we don't have to worry
4060 about changing the DT_RELA entry. */
4061 if (htab->elf.srelplt != NULL)
4063 s = htab->elf.srelplt->output_section;
4064 dyn.d_un.d_val -= s->size;
4066 break;
4068 case DT_TLSDESC_PLT:
4069 s = htab->elf.splt;
4070 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4071 + htab->tlsdesc_plt;
4072 break;
4074 case DT_TLSDESC_GOT:
4075 s = htab->elf.sgot;
4076 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4077 + htab->tlsdesc_got;
4078 break;
4081 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4084 /* Fill in the special first entry in the procedure linkage table. */
4085 if (htab->elf.splt && htab->elf.splt->size > 0)
4087 /* Fill in the first entry in the procedure linkage table. */
4088 memcpy (htab->elf.splt->contents, elf64_x86_64_plt0_entry,
4089 PLT_ENTRY_SIZE);
4090 /* Add offset for pushq GOT+8(%rip), since the instruction
4091 uses 6 bytes subtract this value. */
4092 bfd_put_32 (output_bfd,
4093 (htab->elf.sgotplt->output_section->vma
4094 + htab->elf.sgotplt->output_offset
4096 - htab->elf.splt->output_section->vma
4097 - htab->elf.splt->output_offset
4098 - 6),
4099 htab->elf.splt->contents + 2);
4100 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4101 the end of the instruction. */
4102 bfd_put_32 (output_bfd,
4103 (htab->elf.sgotplt->output_section->vma
4104 + htab->elf.sgotplt->output_offset
4105 + 16
4106 - htab->elf.splt->output_section->vma
4107 - htab->elf.splt->output_offset
4108 - 12),
4109 htab->elf.splt->contents + 8);
4111 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize =
4112 PLT_ENTRY_SIZE;
4114 if (htab->tlsdesc_plt)
4116 bfd_put_64 (output_bfd, (bfd_vma) 0,
4117 htab->elf.sgot->contents + htab->tlsdesc_got);
4119 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
4120 elf64_x86_64_plt0_entry,
4121 PLT_ENTRY_SIZE);
4123 /* Add offset for pushq GOT+8(%rip), since the
4124 instruction uses 6 bytes subtract this value. */
4125 bfd_put_32 (output_bfd,
4126 (htab->elf.sgotplt->output_section->vma
4127 + htab->elf.sgotplt->output_offset
4129 - htab->elf.splt->output_section->vma
4130 - htab->elf.splt->output_offset
4131 - htab->tlsdesc_plt
4132 - 6),
4133 htab->elf.splt->contents + htab->tlsdesc_plt + 2);
4134 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4135 htab->tlsdesc_got. The 12 is the offset to the end of
4136 the instruction. */
4137 bfd_put_32 (output_bfd,
4138 (htab->elf.sgot->output_section->vma
4139 + htab->elf.sgot->output_offset
4140 + htab->tlsdesc_got
4141 - htab->elf.splt->output_section->vma
4142 - htab->elf.splt->output_offset
4143 - htab->tlsdesc_plt
4144 - 12),
4145 htab->elf.splt->contents + htab->tlsdesc_plt + 8);
4150 if (htab->elf.sgotplt)
4152 /* Fill in the first three entries in the global offset table. */
4153 if (htab->elf.sgotplt->size > 0)
4155 /* Set the first entry in the global offset table to the address of
4156 the dynamic section. */
4157 if (sdyn == NULL)
4158 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4159 else
4160 bfd_put_64 (output_bfd,
4161 sdyn->output_section->vma + sdyn->output_offset,
4162 htab->elf.sgotplt->contents);
4163 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4164 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4165 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4168 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
4169 GOT_ENTRY_SIZE;
4172 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4173 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
4174 = GOT_ENTRY_SIZE;
4176 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4177 htab_traverse (htab->loc_hash_table,
4178 elf64_x86_64_finish_local_dynamic_symbol,
4179 info);
4181 return TRUE;
4184 /* Return address for Ith PLT stub in section PLT, for relocation REL
4185 or (bfd_vma) -1 if it should not be included. */
4187 static bfd_vma
4188 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4189 const arelent *rel ATTRIBUTE_UNUSED)
4191 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4194 /* Handle an x86-64 specific section when reading an object file. This
4195 is called when elfcode.h finds a section with an unknown type. */
4197 static bfd_boolean
4198 elf64_x86_64_section_from_shdr (bfd *abfd,
4199 Elf_Internal_Shdr *hdr,
4200 const char *name,
4201 int shindex)
4203 if (hdr->sh_type != SHT_X86_64_UNWIND)
4204 return FALSE;
4206 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4207 return FALSE;
4209 return TRUE;
4212 /* Hook called by the linker routine which adds symbols from an object
4213 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4214 of .bss. */
4216 static bfd_boolean
4217 elf64_x86_64_add_symbol_hook (bfd *abfd,
4218 struct bfd_link_info *info,
4219 Elf_Internal_Sym *sym,
4220 const char **namep ATTRIBUTE_UNUSED,
4221 flagword *flagsp ATTRIBUTE_UNUSED,
4222 asection **secp,
4223 bfd_vma *valp)
4225 asection *lcomm;
4227 switch (sym->st_shndx)
4229 case SHN_X86_64_LCOMMON:
4230 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4231 if (lcomm == NULL)
4233 lcomm = bfd_make_section_with_flags (abfd,
4234 "LARGE_COMMON",
4235 (SEC_ALLOC
4236 | SEC_IS_COMMON
4237 | SEC_LINKER_CREATED));
4238 if (lcomm == NULL)
4239 return FALSE;
4240 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4242 *secp = lcomm;
4243 *valp = sym->st_size;
4244 return TRUE;
4247 if ((abfd->flags & DYNAMIC) == 0
4248 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4249 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4251 return TRUE;
4255 /* Given a BFD section, try to locate the corresponding ELF section
4256 index. */
4258 static bfd_boolean
4259 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4260 asection *sec, int *index_return)
4262 if (sec == &_bfd_elf_large_com_section)
4264 *index_return = SHN_X86_64_LCOMMON;
4265 return TRUE;
4267 return FALSE;
4270 /* Process a symbol. */
4272 static void
4273 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4274 asymbol *asym)
4276 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4278 switch (elfsym->internal_elf_sym.st_shndx)
4280 case SHN_X86_64_LCOMMON:
4281 asym->section = &_bfd_elf_large_com_section;
4282 asym->value = elfsym->internal_elf_sym.st_size;
4283 /* Common symbol doesn't set BSF_GLOBAL. */
4284 asym->flags &= ~BSF_GLOBAL;
4285 break;
4289 static bfd_boolean
4290 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
4292 return (sym->st_shndx == SHN_COMMON
4293 || sym->st_shndx == SHN_X86_64_LCOMMON);
4296 static unsigned int
4297 elf64_x86_64_common_section_index (asection *sec)
4299 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4300 return SHN_COMMON;
4301 else
4302 return SHN_X86_64_LCOMMON;
4305 static asection *
4306 elf64_x86_64_common_section (asection *sec)
4308 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4309 return bfd_com_section_ptr;
4310 else
4311 return &_bfd_elf_large_com_section;
4314 static bfd_boolean
4315 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4316 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4317 struct elf_link_hash_entry *h,
4318 Elf_Internal_Sym *sym,
4319 asection **psec,
4320 bfd_vma *pvalue ATTRIBUTE_UNUSED,
4321 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4322 bfd_boolean *skip ATTRIBUTE_UNUSED,
4323 bfd_boolean *override ATTRIBUTE_UNUSED,
4324 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4325 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4326 bfd_boolean *newdef ATTRIBUTE_UNUSED,
4327 bfd_boolean *newdyn,
4328 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4329 bfd_boolean *newweak ATTRIBUTE_UNUSED,
4330 bfd *abfd ATTRIBUTE_UNUSED,
4331 asection **sec,
4332 bfd_boolean *olddef ATTRIBUTE_UNUSED,
4333 bfd_boolean *olddyn,
4334 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4335 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4336 bfd *oldbfd,
4337 asection **oldsec)
4339 /* A normal common symbol and a large common symbol result in a
4340 normal common symbol. We turn the large common symbol into a
4341 normal one. */
4342 if (!*olddyn
4343 && h->root.type == bfd_link_hash_common
4344 && !*newdyn
4345 && bfd_is_com_section (*sec)
4346 && *oldsec != *sec)
4348 if (sym->st_shndx == SHN_COMMON
4349 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4351 h->root.u.c.p->section
4352 = bfd_make_section_old_way (oldbfd, "COMMON");
4353 h->root.u.c.p->section->flags = SEC_ALLOC;
4355 else if (sym->st_shndx == SHN_X86_64_LCOMMON
4356 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4357 *psec = *sec = bfd_com_section_ptr;
4360 return TRUE;
4363 static int
4364 elf64_x86_64_additional_program_headers (bfd *abfd,
4365 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4367 asection *s;
4368 int count = 0;
4370 /* Check to see if we need a large readonly segment. */
4371 s = bfd_get_section_by_name (abfd, ".lrodata");
4372 if (s && (s->flags & SEC_LOAD))
4373 count++;
4375 /* Check to see if we need a large data segment. Since .lbss sections
4376 is placed right after the .bss section, there should be no need for
4377 a large data segment just because of .lbss. */
4378 s = bfd_get_section_by_name (abfd, ".ldata");
4379 if (s && (s->flags & SEC_LOAD))
4380 count++;
4382 return count;
4385 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4387 static bfd_boolean
4388 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4390 if (h->plt.offset != (bfd_vma) -1
4391 && !h->def_regular
4392 && !h->pointer_equality_needed)
4393 return FALSE;
4395 return _bfd_elf_hash_symbol (h);
4398 static const struct bfd_elf_special_section
4399 elf64_x86_64_special_sections[]=
4401 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4402 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4403 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4404 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4405 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4406 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4407 { NULL, 0, 0, 0, 0 }
4410 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
4411 #define TARGET_LITTLE_NAME "elf64-x86-64"
4412 #define ELF_ARCH bfd_arch_i386
4413 #define ELF_MACHINE_CODE EM_X86_64
4414 #define ELF_MAXPAGESIZE 0x200000
4415 #define ELF_MINPAGESIZE 0x1000
4416 #define ELF_COMMONPAGESIZE 0x1000
4418 #define elf_backend_can_gc_sections 1
4419 #define elf_backend_can_refcount 1
4420 #define elf_backend_want_got_plt 1
4421 #define elf_backend_plt_readonly 1
4422 #define elf_backend_want_plt_sym 0
4423 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
4424 #define elf_backend_rela_normal 1
4426 #define elf_info_to_howto elf64_x86_64_info_to_howto
4428 #define bfd_elf64_bfd_link_hash_table_create \
4429 elf64_x86_64_link_hash_table_create
4430 #define bfd_elf64_bfd_link_hash_table_free \
4431 elf64_x86_64_link_hash_table_free
4432 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
4433 #define bfd_elf64_bfd_reloc_name_lookup \
4434 elf64_x86_64_reloc_name_lookup
4436 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
4437 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4438 #define elf_backend_check_relocs elf64_x86_64_check_relocs
4439 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
4440 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
4441 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
4442 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
4443 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
4444 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
4445 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
4446 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
4447 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
4448 #define elf_backend_relocate_section elf64_x86_64_relocate_section
4449 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
4450 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
4451 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4452 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
4453 #define elf_backend_object_p elf64_x86_64_elf_object_p
4454 #define bfd_elf64_mkobject elf64_x86_64_mkobject
4456 #define elf_backend_section_from_shdr \
4457 elf64_x86_64_section_from_shdr
4459 #define elf_backend_section_from_bfd_section \
4460 elf64_x86_64_elf_section_from_bfd_section
4461 #define elf_backend_add_symbol_hook \
4462 elf64_x86_64_add_symbol_hook
4463 #define elf_backend_symbol_processing \
4464 elf64_x86_64_symbol_processing
4465 #define elf_backend_common_section_index \
4466 elf64_x86_64_common_section_index
4467 #define elf_backend_common_section \
4468 elf64_x86_64_common_section
4469 #define elf_backend_common_definition \
4470 elf64_x86_64_common_definition
4471 #define elf_backend_merge_symbol \
4472 elf64_x86_64_merge_symbol
4473 #define elf_backend_special_sections \
4474 elf64_x86_64_special_sections
4475 #define elf_backend_additional_program_headers \
4476 elf64_x86_64_additional_program_headers
4477 #define elf_backend_hash_symbol \
4478 elf64_x86_64_hash_symbol
4480 #undef elf_backend_post_process_headers
4481 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4483 #include "elf64-target.h"
4485 /* FreeBSD support. */
4487 #undef TARGET_LITTLE_SYM
4488 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
4489 #undef TARGET_LITTLE_NAME
4490 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
4492 #undef ELF_OSABI
4493 #define ELF_OSABI ELFOSABI_FREEBSD
4495 #undef elf64_bed
4496 #define elf64_bed elf64_x86_64_fbsd_bed
4498 #include "elf64-target.h"
4500 /* Solaris 2 support. */
4502 #undef TARGET_LITTLE_SYM
4503 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_sol2_vec
4504 #undef TARGET_LITTLE_NAME
4505 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
4507 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4508 objects won't be recognized. */
4509 #undef ELF_OSABI
4511 #undef elf64_bed
4512 #define elf64_bed elf64_x86_64_sol2_bed
4514 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4516 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4517 File, p.63. */
4518 #undef elf_backend_want_plt_sym
4519 #define elf_backend_want_plt_sym 1
4521 #include "elf64-target.h"
4523 /* Intel L1OM support. */
4525 static bfd_boolean
4526 elf64_l1om_elf_object_p (bfd *abfd)
4528 /* Set the right machine number for an L1OM elf64 file. */
4529 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
4530 return TRUE;
4533 #undef TARGET_LITTLE_SYM
4534 #define TARGET_LITTLE_SYM bfd_elf64_l1om_vec
4535 #undef TARGET_LITTLE_NAME
4536 #define TARGET_LITTLE_NAME "elf64-l1om"
4537 #undef ELF_ARCH
4538 #define ELF_ARCH bfd_arch_l1om
4540 #undef ELF_MACHINE_CODE
4541 #define ELF_MACHINE_CODE EM_L1OM
4543 #undef ELF_OSABI
4545 #undef elf64_bed
4546 #define elf64_bed elf64_l1om_bed
4548 #undef elf_backend_object_p
4549 #define elf_backend_object_p elf64_l1om_elf_object_p
4551 #undef elf_backend_post_process_headers
4553 #include "elf64-target.h"
4555 /* FreeBSD L1OM support. */
4557 #undef TARGET_LITTLE_SYM
4558 #define TARGET_LITTLE_SYM bfd_elf64_l1om_freebsd_vec
4559 #undef TARGET_LITTLE_NAME
4560 #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
4562 #undef ELF_OSABI
4563 #define ELF_OSABI ELFOSABI_FREEBSD
4565 #undef elf64_bed
4566 #define elf64_bed elf64_l1om_fbsd_bed
4568 #undef elf_backend_post_process_headers
4569 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4571 #include "elf64-target.h"