2009-07-02 Tristan Gingold <gingold@adacore.com>
[binutils.git] / bfd / elf64-x86-64.c
blob274b8746b7df98c21de53626c839d1b996ab3a1c
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
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_TDATA)
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_TDATA);
463 /* x86-64 ELF linker hash table. */
465 struct elf64_x86_64_link_hash_table
467 struct elf_link_hash_table elf;
469 /* Short-cuts to get to dynamic linker sections. */
470 asection *sdynbss;
471 asection *srelbss;
473 /* The offset into splt of the PLT entry for the TLS descriptor
474 resolver. Special values are 0, if not necessary (or not found
475 to be necessary yet), and -1 if needed but not determined
476 yet. */
477 bfd_vma tlsdesc_plt;
478 /* The offset into sgot of the GOT entry used by the PLT entry
479 above. */
480 bfd_vma tlsdesc_got;
482 union {
483 bfd_signed_vma refcount;
484 bfd_vma offset;
485 } tls_ld_got;
487 /* The amount of space used by the jump slots in the GOT. */
488 bfd_vma sgotplt_jump_table_size;
490 /* Small local sym cache. */
491 struct sym_cache sym_cache;
493 /* _TLS_MODULE_BASE_ symbol. */
494 struct bfd_link_hash_entry *tls_module_base;
496 /* Used by local STT_GNU_IFUNC symbols. */
497 htab_t loc_hash_table;
498 void *loc_hash_memory;
501 /* Get the x86-64 ELF linker hash table from a link_info structure. */
503 #define elf64_x86_64_hash_table(p) \
504 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
506 #define elf64_x86_64_compute_jump_table_size(htab) \
507 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
509 /* Create an entry in an x86-64 ELF linker hash table. */
511 static struct bfd_hash_entry *
512 elf64_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
513 struct bfd_hash_table *table,
514 const char *string)
516 /* Allocate the structure if it has not already been allocated by a
517 subclass. */
518 if (entry == NULL)
520 entry = bfd_hash_allocate (table,
521 sizeof (struct elf64_x86_64_link_hash_entry));
522 if (entry == NULL)
523 return entry;
526 /* Call the allocation method of the superclass. */
527 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
528 if (entry != NULL)
530 struct elf64_x86_64_link_hash_entry *eh;
532 eh = (struct elf64_x86_64_link_hash_entry *) entry;
533 eh->dyn_relocs = NULL;
534 eh->tls_type = GOT_UNKNOWN;
535 eh->tlsdesc_got = (bfd_vma) -1;
538 return entry;
541 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
542 for local symbol so that we can handle local STT_GNU_IFUNC symbols
543 as global symbol. We reuse indx and dynstr_index for local symbol
544 hash since they aren't used by global symbols in this backend. */
546 static hashval_t
547 elf64_x86_64_local_htab_hash (const void *ptr)
549 struct elf_link_hash_entry *h
550 = (struct elf_link_hash_entry *) ptr;
551 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
554 /* Compare local hash entries. */
556 static int
557 elf64_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
559 struct elf_link_hash_entry *h1
560 = (struct elf_link_hash_entry *) ptr1;
561 struct elf_link_hash_entry *h2
562 = (struct elf_link_hash_entry *) ptr2;
564 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
567 /* Find and/or create a hash entry for local symbol. */
569 static struct elf_link_hash_entry *
570 elf64_x86_64_get_local_sym_hash (struct elf64_x86_64_link_hash_table *htab,
571 bfd *abfd, const Elf_Internal_Rela *rel,
572 bfd_boolean create)
574 struct elf64_x86_64_link_hash_entry e, *ret;
575 asection *sec = abfd->sections;
576 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
577 ELF64_R_SYM (rel->r_info));
578 void **slot;
580 e.elf.indx = sec->id;
581 e.elf.dynstr_index = ELF64_R_SYM (rel->r_info);
582 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
583 create ? INSERT : NO_INSERT);
585 if (!slot)
586 return NULL;
588 if (*slot)
590 ret = (struct elf64_x86_64_link_hash_entry *) *slot;
591 return &ret->elf;
594 ret = (struct elf64_x86_64_link_hash_entry *)
595 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
596 sizeof (struct elf64_x86_64_link_hash_entry));
597 if (ret)
599 memset (ret, 0, sizeof (*ret));
600 ret->elf.indx = sec->id;
601 ret->elf.dynstr_index = ELF64_R_SYM (rel->r_info);
602 ret->elf.dynindx = -1;
603 ret->elf.plt.offset = (bfd_vma) -1;
604 ret->elf.got.offset = (bfd_vma) -1;
605 *slot = ret;
607 return &ret->elf;
610 /* Create an X86-64 ELF linker hash table. */
612 static struct bfd_link_hash_table *
613 elf64_x86_64_link_hash_table_create (bfd *abfd)
615 struct elf64_x86_64_link_hash_table *ret;
616 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
618 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
619 if (ret == NULL)
620 return NULL;
622 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
623 elf64_x86_64_link_hash_newfunc,
624 sizeof (struct elf64_x86_64_link_hash_entry)))
626 free (ret);
627 return NULL;
630 ret->sdynbss = NULL;
631 ret->srelbss = NULL;
632 ret->sym_cache.abfd = NULL;
633 ret->tlsdesc_plt = 0;
634 ret->tlsdesc_got = 0;
635 ret->tls_ld_got.refcount = 0;
636 ret->sgotplt_jump_table_size = 0;
637 ret->tls_module_base = NULL;
639 ret->loc_hash_table = htab_try_create (1024,
640 elf64_x86_64_local_htab_hash,
641 elf64_x86_64_local_htab_eq,
642 NULL);
643 ret->loc_hash_memory = objalloc_create ();
644 if (!ret->loc_hash_table || !ret->loc_hash_memory)
646 free (ret);
647 return NULL;
650 return &ret->elf.root;
653 /* Destroy an X86-64 ELF linker hash table. */
655 static void
656 elf64_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
658 struct elf64_x86_64_link_hash_table *htab
659 = (struct elf64_x86_64_link_hash_table *) hash;
661 if (htab->loc_hash_table)
662 htab_delete (htab->loc_hash_table);
663 if (htab->loc_hash_memory)
664 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
665 _bfd_generic_link_hash_table_free (hash);
668 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
669 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
670 hash table. */
672 static bfd_boolean
673 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
675 struct elf64_x86_64_link_hash_table *htab;
677 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
678 return FALSE;
680 htab = elf64_x86_64_hash_table (info);
681 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
682 if (!info->shared)
683 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
685 if (!htab->sdynbss
686 || (!info->shared && !htab->srelbss))
687 abort ();
689 return TRUE;
692 /* Copy the extra info we tack onto an elf_link_hash_entry. */
694 static void
695 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
696 struct elf_link_hash_entry *dir,
697 struct elf_link_hash_entry *ind)
699 struct elf64_x86_64_link_hash_entry *edir, *eind;
701 edir = (struct elf64_x86_64_link_hash_entry *) dir;
702 eind = (struct elf64_x86_64_link_hash_entry *) ind;
704 if (eind->dyn_relocs != NULL)
706 if (edir->dyn_relocs != NULL)
708 struct elf_dyn_relocs **pp;
709 struct elf_dyn_relocs *p;
711 /* Add reloc counts against the indirect sym to the direct sym
712 list. Merge any entries against the same section. */
713 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
715 struct elf_dyn_relocs *q;
717 for (q = edir->dyn_relocs; q != NULL; q = q->next)
718 if (q->sec == p->sec)
720 q->pc_count += p->pc_count;
721 q->count += p->count;
722 *pp = p->next;
723 break;
725 if (q == NULL)
726 pp = &p->next;
728 *pp = edir->dyn_relocs;
731 edir->dyn_relocs = eind->dyn_relocs;
732 eind->dyn_relocs = NULL;
735 if (ind->root.type == bfd_link_hash_indirect
736 && dir->got.refcount <= 0)
738 edir->tls_type = eind->tls_type;
739 eind->tls_type = GOT_UNKNOWN;
742 if (ELIMINATE_COPY_RELOCS
743 && ind->root.type != bfd_link_hash_indirect
744 && dir->dynamic_adjusted)
746 /* If called to transfer flags for a weakdef during processing
747 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
748 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
749 dir->ref_dynamic |= ind->ref_dynamic;
750 dir->ref_regular |= ind->ref_regular;
751 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
752 dir->needs_plt |= ind->needs_plt;
753 dir->pointer_equality_needed |= ind->pointer_equality_needed;
755 else
756 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
759 static bfd_boolean
760 elf64_x86_64_elf_object_p (bfd *abfd)
762 /* Set the right machine number for an x86-64 elf64 file. */
763 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
764 return TRUE;
767 typedef union
769 unsigned char c[2];
770 uint16_t i;
772 x86_64_opcode16;
774 typedef union
776 unsigned char c[4];
777 uint32_t i;
779 x86_64_opcode32;
781 /* Return TRUE if the TLS access code sequence support transition
782 from R_TYPE. */
784 static bfd_boolean
785 elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec,
786 bfd_byte *contents,
787 Elf_Internal_Shdr *symtab_hdr,
788 struct elf_link_hash_entry **sym_hashes,
789 unsigned int r_type,
790 const Elf_Internal_Rela *rel,
791 const Elf_Internal_Rela *relend)
793 unsigned int val;
794 unsigned long r_symndx;
795 struct elf_link_hash_entry *h;
796 bfd_vma offset;
798 /* Get the section contents. */
799 if (contents == NULL)
801 if (elf_section_data (sec)->this_hdr.contents != NULL)
802 contents = elf_section_data (sec)->this_hdr.contents;
803 else
805 /* FIXME: How to better handle error condition? */
806 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
807 return FALSE;
809 /* Cache the section contents for elf_link_input_bfd. */
810 elf_section_data (sec)->this_hdr.contents = contents;
814 offset = rel->r_offset;
815 switch (r_type)
817 case R_X86_64_TLSGD:
818 case R_X86_64_TLSLD:
819 if ((rel + 1) >= relend)
820 return FALSE;
822 if (r_type == R_X86_64_TLSGD)
824 /* Check transition from GD access model. Only
825 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
826 .word 0x6666; rex64; call __tls_get_addr
827 can transit to different access model. */
829 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } },
830 call = { { 0x66, 0x66, 0x48, 0xe8 } };
831 if (offset < 4
832 || (offset + 12) > sec->size
833 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i
834 || bfd_get_32 (abfd, contents + offset + 4) != call.i)
835 return FALSE;
837 else
839 /* Check transition from LD access model. Only
840 leaq foo@tlsld(%rip), %rdi;
841 call __tls_get_addr
842 can transit to different access model. */
844 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
845 x86_64_opcode32 op;
847 if (offset < 3 || (offset + 9) > sec->size)
848 return FALSE;
850 op.i = bfd_get_32 (abfd, contents + offset - 3);
851 op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
852 if (op.i != ld.i)
853 return FALSE;
856 r_symndx = ELF64_R_SYM (rel[1].r_info);
857 if (r_symndx < symtab_hdr->sh_info)
858 return FALSE;
860 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
861 /* Use strncmp to check __tls_get_addr since __tls_get_addr
862 may be versioned. */
863 return (h != NULL
864 && h->root.root.string != NULL
865 && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
866 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
867 && (strncmp (h->root.root.string,
868 "__tls_get_addr", 14) == 0));
870 case R_X86_64_GOTTPOFF:
871 /* Check transition from IE access model:
872 movq foo@gottpoff(%rip), %reg
873 addq foo@gottpoff(%rip), %reg
876 if (offset < 3 || (offset + 4) > sec->size)
877 return FALSE;
879 val = bfd_get_8 (abfd, contents + offset - 3);
880 if (val != 0x48 && val != 0x4c)
881 return FALSE;
883 val = bfd_get_8 (abfd, contents + offset - 2);
884 if (val != 0x8b && val != 0x03)
885 return FALSE;
887 val = bfd_get_8 (abfd, contents + offset - 1);
888 return (val & 0xc7) == 5;
890 case R_X86_64_GOTPC32_TLSDESC:
891 /* Check transition from GDesc access model:
892 leaq x@tlsdesc(%rip), %rax
894 Make sure it's a leaq adding rip to a 32-bit offset
895 into any register, although it's probably almost always
896 going to be rax. */
898 if (offset < 3 || (offset + 4) > sec->size)
899 return FALSE;
901 val = bfd_get_8 (abfd, contents + offset - 3);
902 if ((val & 0xfb) != 0x48)
903 return FALSE;
905 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
906 return FALSE;
908 val = bfd_get_8 (abfd, contents + offset - 1);
909 return (val & 0xc7) == 0x05;
911 case R_X86_64_TLSDESC_CALL:
912 /* Check transition from GDesc access model:
913 call *x@tlsdesc(%rax)
915 if (offset + 2 <= sec->size)
917 /* Make sure that it's a call *x@tlsdesc(%rax). */
918 static x86_64_opcode16 call = { { 0xff, 0x10 } };
919 return bfd_get_16 (abfd, contents + offset) == call.i;
922 return FALSE;
924 default:
925 abort ();
929 /* Return TRUE if the TLS access transition is OK or no transition
930 will be performed. Update R_TYPE if there is a transition. */
932 static bfd_boolean
933 elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
934 asection *sec, bfd_byte *contents,
935 Elf_Internal_Shdr *symtab_hdr,
936 struct elf_link_hash_entry **sym_hashes,
937 unsigned int *r_type, int tls_type,
938 const Elf_Internal_Rela *rel,
939 const Elf_Internal_Rela *relend,
940 struct elf_link_hash_entry *h,
941 unsigned long r_symndx)
943 unsigned int from_type = *r_type;
944 unsigned int to_type = from_type;
945 bfd_boolean check = TRUE;
947 switch (from_type)
949 case R_X86_64_TLSGD:
950 case R_X86_64_GOTPC32_TLSDESC:
951 case R_X86_64_TLSDESC_CALL:
952 case R_X86_64_GOTTPOFF:
953 if (!info->shared)
955 if (h == NULL)
956 to_type = R_X86_64_TPOFF32;
957 else
958 to_type = R_X86_64_GOTTPOFF;
961 /* When we are called from elf64_x86_64_relocate_section,
962 CONTENTS isn't NULL and there may be additional transitions
963 based on TLS_TYPE. */
964 if (contents != NULL)
966 unsigned int new_to_type = to_type;
968 if (!info->shared
969 && h != NULL
970 && h->dynindx == -1
971 && tls_type == GOT_TLS_IE)
972 new_to_type = R_X86_64_TPOFF32;
974 if (to_type == R_X86_64_TLSGD
975 || to_type == R_X86_64_GOTPC32_TLSDESC
976 || to_type == R_X86_64_TLSDESC_CALL)
978 if (tls_type == GOT_TLS_IE)
979 new_to_type = R_X86_64_GOTTPOFF;
982 /* We checked the transition before when we were called from
983 elf64_x86_64_check_relocs. We only want to check the new
984 transition which hasn't been checked before. */
985 check = new_to_type != to_type && from_type == to_type;
986 to_type = new_to_type;
989 break;
991 case R_X86_64_TLSLD:
992 if (!info->shared)
993 to_type = R_X86_64_TPOFF32;
994 break;
996 default:
997 return TRUE;
1000 /* Return TRUE if there is no transition. */
1001 if (from_type == to_type)
1002 return TRUE;
1004 /* Check if the transition can be performed. */
1005 if (check
1006 && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
1007 symtab_hdr, sym_hashes,
1008 from_type, rel, relend))
1010 reloc_howto_type *from, *to;
1011 const char *name;
1013 from = elf64_x86_64_rtype_to_howto (abfd, from_type);
1014 to = elf64_x86_64_rtype_to_howto (abfd, to_type);
1016 if (h)
1017 name = h->root.root.string;
1018 else
1020 Elf_Internal_Sym *isym;
1021 struct elf64_x86_64_link_hash_table *htab;
1022 htab = elf64_x86_64_hash_table (info);
1023 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1024 abfd, r_symndx);
1025 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1028 (*_bfd_error_handler)
1029 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1030 "in section `%A' failed"),
1031 abfd, sec, from->name, to->name, name,
1032 (unsigned long) rel->r_offset);
1033 bfd_set_error (bfd_error_bad_value);
1034 return FALSE;
1037 *r_type = to_type;
1038 return TRUE;
1041 /* Look through the relocs for a section during the first phase, and
1042 calculate needed space in the global offset table, procedure
1043 linkage table, and dynamic reloc sections. */
1045 static bfd_boolean
1046 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1047 asection *sec,
1048 const Elf_Internal_Rela *relocs)
1050 struct elf64_x86_64_link_hash_table *htab;
1051 Elf_Internal_Shdr *symtab_hdr;
1052 struct elf_link_hash_entry **sym_hashes;
1053 const Elf_Internal_Rela *rel;
1054 const Elf_Internal_Rela *rel_end;
1055 asection *sreloc;
1057 if (info->relocatable)
1058 return TRUE;
1060 BFD_ASSERT (is_x86_64_elf (abfd));
1062 htab = elf64_x86_64_hash_table (info);
1063 symtab_hdr = &elf_symtab_hdr (abfd);
1064 sym_hashes = elf_sym_hashes (abfd);
1066 sreloc = NULL;
1068 rel_end = relocs + sec->reloc_count;
1069 for (rel = relocs; rel < rel_end; rel++)
1071 unsigned int r_type;
1072 unsigned long r_symndx;
1073 struct elf_link_hash_entry *h;
1074 Elf_Internal_Sym *isym;
1075 const char *name;
1077 r_symndx = ELF64_R_SYM (rel->r_info);
1078 r_type = ELF64_R_TYPE (rel->r_info);
1080 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1082 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1083 abfd, r_symndx);
1084 return FALSE;
1087 if (r_symndx < symtab_hdr->sh_info)
1089 /* A local symbol. */
1090 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1091 abfd, r_symndx);
1092 if (isym == NULL)
1093 return FALSE;
1095 /* Check relocation against local STT_GNU_IFUNC symbol. */
1096 if (ELF64_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1098 h = elf64_x86_64_get_local_sym_hash (htab, abfd, rel,
1099 TRUE);
1100 if (h == NULL)
1101 return FALSE;
1103 /* Fake a STT_GNU_IFUNC symbol. */
1104 h->type = STT_GNU_IFUNC;
1105 h->def_regular = 1;
1106 h->ref_regular = 1;
1107 h->forced_local = 1;
1108 h->root.type = bfd_link_hash_defined;
1110 else
1111 h = NULL;
1113 else
1115 isym = NULL;
1116 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1117 while (h->root.type == bfd_link_hash_indirect
1118 || h->root.type == bfd_link_hash_warning)
1119 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1122 if (h != NULL)
1124 /* Create the ifunc sections for static executables. If we
1125 never see an indirect function symbol nor we are building
1126 a static executable, those sections will be empty and
1127 won't appear in output. */
1128 switch (r_type)
1130 default:
1131 break;
1133 case R_X86_64_32S:
1134 case R_X86_64_32:
1135 case R_X86_64_64:
1136 case R_X86_64_PC32:
1137 case R_X86_64_PC64:
1138 case R_X86_64_PLT32:
1139 case R_X86_64_GOTPCREL:
1140 case R_X86_64_GOTPCREL64:
1141 if (!_bfd_elf_create_ifunc_sections (abfd, info))
1142 return FALSE;
1143 break;
1146 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1147 it here if it is defined in a non-shared object. */
1148 if (h->type == STT_GNU_IFUNC
1149 && h->def_regular)
1151 /* It is referenced by a non-shared object. */
1152 h->ref_regular = 1;
1154 /* STT_GNU_IFUNC symbol must go through PLT. */
1155 h->plt.refcount += 1;
1157 /* STT_GNU_IFUNC needs dynamic sections. */
1158 if (htab->elf.dynobj == NULL)
1159 htab->elf.dynobj = abfd;
1161 switch (r_type)
1163 default:
1164 if (h->root.root.string)
1165 name = h->root.root.string;
1166 else
1167 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1168 NULL);
1169 (*_bfd_error_handler)
1170 (_("%B: relocation %s against STT_GNU_IFUNC "
1171 "symbol `%s' isn't handled by %s"), abfd,
1172 x86_64_elf_howto_table[r_type].name,
1173 name, __FUNCTION__);
1174 bfd_set_error (bfd_error_bad_value);
1175 return FALSE;
1177 case R_X86_64_64:
1178 h->non_got_ref = 1;
1179 h->pointer_equality_needed = 1;
1180 if (info->shared)
1182 /* We must copy these reloc types into the output
1183 file. Create a reloc section in dynobj and
1184 make room for this reloc. */
1185 sreloc = _bfd_elf_create_ifunc_dyn_reloc
1186 (abfd, info, sec, sreloc,
1187 &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs);
1188 if (sreloc == NULL)
1189 return FALSE;
1191 break;
1193 case R_X86_64_32S:
1194 case R_X86_64_32:
1195 case R_X86_64_PC32:
1196 case R_X86_64_PC64:
1197 h->non_got_ref = 1;
1198 if (r_type != R_X86_64_PC32
1199 && r_type != R_X86_64_PC64)
1200 h->pointer_equality_needed = 1;
1201 break;
1203 case R_X86_64_PLT32:
1204 break;
1206 case R_X86_64_GOTPCREL:
1207 case R_X86_64_GOTPCREL64:
1208 h->got.refcount += 1;
1209 if (htab->elf.sgot == NULL
1210 && !_bfd_elf_create_got_section (htab->elf.dynobj,
1211 info))
1212 return FALSE;
1213 break;
1216 continue;
1220 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1221 symtab_hdr, sym_hashes,
1222 &r_type, GOT_UNKNOWN,
1223 rel, rel_end, h, r_symndx))
1224 return FALSE;
1226 switch (r_type)
1228 case R_X86_64_TLSLD:
1229 htab->tls_ld_got.refcount += 1;
1230 goto create_got;
1232 case R_X86_64_TPOFF32:
1233 if (info->shared)
1235 if (h)
1236 name = h->root.root.string;
1237 else
1238 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1239 NULL);
1240 (*_bfd_error_handler)
1241 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1242 abfd,
1243 x86_64_elf_howto_table[r_type].name, name);
1244 bfd_set_error (bfd_error_bad_value);
1245 return FALSE;
1247 break;
1249 case R_X86_64_GOTTPOFF:
1250 if (info->shared)
1251 info->flags |= DF_STATIC_TLS;
1252 /* Fall through */
1254 case R_X86_64_GOT32:
1255 case R_X86_64_GOTPCREL:
1256 case R_X86_64_TLSGD:
1257 case R_X86_64_GOT64:
1258 case R_X86_64_GOTPCREL64:
1259 case R_X86_64_GOTPLT64:
1260 case R_X86_64_GOTPC32_TLSDESC:
1261 case R_X86_64_TLSDESC_CALL:
1262 /* This symbol requires a global offset table entry. */
1264 int tls_type, old_tls_type;
1266 switch (r_type)
1268 default: tls_type = GOT_NORMAL; break;
1269 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1270 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1271 case R_X86_64_GOTPC32_TLSDESC:
1272 case R_X86_64_TLSDESC_CALL:
1273 tls_type = GOT_TLS_GDESC; break;
1276 if (h != NULL)
1278 if (r_type == R_X86_64_GOTPLT64)
1280 /* This relocation indicates that we also need
1281 a PLT entry, as this is a function. We don't need
1282 a PLT entry for local symbols. */
1283 h->needs_plt = 1;
1284 h->plt.refcount += 1;
1286 h->got.refcount += 1;
1287 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1289 else
1291 bfd_signed_vma *local_got_refcounts;
1293 /* This is a global offset table entry for a local symbol. */
1294 local_got_refcounts = elf_local_got_refcounts (abfd);
1295 if (local_got_refcounts == NULL)
1297 bfd_size_type size;
1299 size = symtab_hdr->sh_info;
1300 size *= sizeof (bfd_signed_vma)
1301 + sizeof (bfd_vma) + sizeof (char);
1302 local_got_refcounts = ((bfd_signed_vma *)
1303 bfd_zalloc (abfd, size));
1304 if (local_got_refcounts == NULL)
1305 return FALSE;
1306 elf_local_got_refcounts (abfd) = local_got_refcounts;
1307 elf64_x86_64_local_tlsdesc_gotent (abfd)
1308 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1309 elf64_x86_64_local_got_tls_type (abfd)
1310 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1312 local_got_refcounts[r_symndx] += 1;
1313 old_tls_type
1314 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1317 /* If a TLS symbol is accessed using IE at least once,
1318 there is no point to use dynamic model for it. */
1319 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1320 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1321 || tls_type != GOT_TLS_IE))
1323 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1324 tls_type = old_tls_type;
1325 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1326 && GOT_TLS_GD_ANY_P (tls_type))
1327 tls_type |= old_tls_type;
1328 else
1330 if (h)
1331 name = h->root.root.string;
1332 else
1333 name = bfd_elf_sym_name (abfd, symtab_hdr,
1334 isym, NULL);
1335 (*_bfd_error_handler)
1336 (_("%B: '%s' accessed both as normal and thread local symbol"),
1337 abfd, name);
1338 return FALSE;
1342 if (old_tls_type != tls_type)
1344 if (h != NULL)
1345 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1346 else
1347 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1350 /* Fall through */
1352 case R_X86_64_GOTOFF64:
1353 case R_X86_64_GOTPC32:
1354 case R_X86_64_GOTPC64:
1355 create_got:
1356 if (htab->elf.sgot == NULL)
1358 if (htab->elf.dynobj == NULL)
1359 htab->elf.dynobj = abfd;
1360 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1361 info))
1362 return FALSE;
1364 break;
1366 case R_X86_64_PLT32:
1367 /* This symbol requires a procedure linkage table entry. We
1368 actually build the entry in adjust_dynamic_symbol,
1369 because this might be a case of linking PIC code which is
1370 never referenced by a dynamic object, in which case we
1371 don't need to generate a procedure linkage table entry
1372 after all. */
1374 /* If this is a local symbol, we resolve it directly without
1375 creating a procedure linkage table entry. */
1376 if (h == NULL)
1377 continue;
1379 h->needs_plt = 1;
1380 h->plt.refcount += 1;
1381 break;
1383 case R_X86_64_PLTOFF64:
1384 /* This tries to form the 'address' of a function relative
1385 to GOT. For global symbols we need a PLT entry. */
1386 if (h != NULL)
1388 h->needs_plt = 1;
1389 h->plt.refcount += 1;
1391 goto create_got;
1393 case R_X86_64_8:
1394 case R_X86_64_16:
1395 case R_X86_64_32:
1396 case R_X86_64_32S:
1397 /* Let's help debug shared library creation. These relocs
1398 cannot be used in shared libs. Don't error out for
1399 sections we don't care about, such as debug sections or
1400 non-constant sections. */
1401 if (info->shared
1402 && (sec->flags & SEC_ALLOC) != 0
1403 && (sec->flags & SEC_READONLY) != 0)
1405 if (h)
1406 name = h->root.root.string;
1407 else
1408 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1409 (*_bfd_error_handler)
1410 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1411 abfd, x86_64_elf_howto_table[r_type].name, name);
1412 bfd_set_error (bfd_error_bad_value);
1413 return FALSE;
1415 /* Fall through. */
1417 case R_X86_64_PC8:
1418 case R_X86_64_PC16:
1419 case R_X86_64_PC32:
1420 case R_X86_64_PC64:
1421 case R_X86_64_64:
1422 if (h != NULL && info->executable)
1424 /* If this reloc is in a read-only section, we might
1425 need a copy reloc. We can't check reliably at this
1426 stage whether the section is read-only, as input
1427 sections have not yet been mapped to output sections.
1428 Tentatively set the flag for now, and correct in
1429 adjust_dynamic_symbol. */
1430 h->non_got_ref = 1;
1432 /* We may need a .plt entry if the function this reloc
1433 refers to is in a shared lib. */
1434 h->plt.refcount += 1;
1435 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1436 h->pointer_equality_needed = 1;
1439 /* If we are creating a shared library, and this is a reloc
1440 against a global symbol, or a non PC relative reloc
1441 against a local symbol, then we need to copy the reloc
1442 into the shared library. However, if we are linking with
1443 -Bsymbolic, we do not need to copy a reloc against a
1444 global symbol which is defined in an object we are
1445 including in the link (i.e., DEF_REGULAR is set). At
1446 this point we have not seen all the input files, so it is
1447 possible that DEF_REGULAR is not set now but will be set
1448 later (it is never cleared). In case of a weak definition,
1449 DEF_REGULAR may be cleared later by a strong definition in
1450 a shared library. We account for that possibility below by
1451 storing information in the relocs_copied field of the hash
1452 table entry. A similar situation occurs when creating
1453 shared libraries and symbol visibility changes render the
1454 symbol local.
1456 If on the other hand, we are creating an executable, we
1457 may need to keep relocations for symbols satisfied by a
1458 dynamic library if we manage to avoid copy relocs for the
1459 symbol. */
1460 if ((info->shared
1461 && (sec->flags & SEC_ALLOC) != 0
1462 && (! IS_X86_64_PCREL_TYPE (r_type)
1463 || (h != NULL
1464 && (! SYMBOLIC_BIND (info, h)
1465 || h->root.type == bfd_link_hash_defweak
1466 || !h->def_regular))))
1467 || (ELIMINATE_COPY_RELOCS
1468 && !info->shared
1469 && (sec->flags & SEC_ALLOC) != 0
1470 && h != NULL
1471 && (h->root.type == bfd_link_hash_defweak
1472 || !h->def_regular)))
1474 struct elf_dyn_relocs *p;
1475 struct elf_dyn_relocs **head;
1477 /* We must copy these reloc types into the output file.
1478 Create a reloc section in dynobj and make room for
1479 this reloc. */
1480 if (sreloc == NULL)
1482 if (htab->elf.dynobj == NULL)
1483 htab->elf.dynobj = abfd;
1485 sreloc = _bfd_elf_make_dynamic_reloc_section
1486 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
1488 if (sreloc == NULL)
1489 return FALSE;
1492 /* If this is a global symbol, we count the number of
1493 relocations we need for this symbol. */
1494 if (h != NULL)
1496 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1498 else
1500 /* Track dynamic relocs needed for local syms too.
1501 We really need local syms available to do this
1502 easily. Oh well. */
1503 asection *s;
1504 void **vpp;
1505 Elf_Internal_Sym *isym;
1507 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1508 abfd, r_symndx);
1509 if (isym == NULL)
1510 return FALSE;
1512 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1513 if (s == NULL)
1514 s = sec;
1516 /* Beware of type punned pointers vs strict aliasing
1517 rules. */
1518 vpp = &(elf_section_data (s)->local_dynrel);
1519 head = (struct elf_dyn_relocs **)vpp;
1522 p = *head;
1523 if (p == NULL || p->sec != sec)
1525 bfd_size_type amt = sizeof *p;
1527 p = ((struct elf_dyn_relocs *)
1528 bfd_alloc (htab->elf.dynobj, amt));
1529 if (p == NULL)
1530 return FALSE;
1531 p->next = *head;
1532 *head = p;
1533 p->sec = sec;
1534 p->count = 0;
1535 p->pc_count = 0;
1538 p->count += 1;
1539 if (IS_X86_64_PCREL_TYPE (r_type))
1540 p->pc_count += 1;
1542 break;
1544 /* This relocation describes the C++ object vtable hierarchy.
1545 Reconstruct it for later use during GC. */
1546 case R_X86_64_GNU_VTINHERIT:
1547 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1548 return FALSE;
1549 break;
1551 /* This relocation describes which C++ vtable entries are actually
1552 used. Record for later use during GC. */
1553 case R_X86_64_GNU_VTENTRY:
1554 BFD_ASSERT (h != NULL);
1555 if (h != NULL
1556 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1557 return FALSE;
1558 break;
1560 default:
1561 break;
1565 return TRUE;
1568 /* Return the section that should be marked against GC for a given
1569 relocation. */
1571 static asection *
1572 elf64_x86_64_gc_mark_hook (asection *sec,
1573 struct bfd_link_info *info,
1574 Elf_Internal_Rela *rel,
1575 struct elf_link_hash_entry *h,
1576 Elf_Internal_Sym *sym)
1578 if (h != NULL)
1579 switch (ELF64_R_TYPE (rel->r_info))
1581 case R_X86_64_GNU_VTINHERIT:
1582 case R_X86_64_GNU_VTENTRY:
1583 return NULL;
1586 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1589 /* Update the got entry reference counts for the section being removed. */
1591 static bfd_boolean
1592 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1593 asection *sec,
1594 const Elf_Internal_Rela *relocs)
1596 Elf_Internal_Shdr *symtab_hdr;
1597 struct elf_link_hash_entry **sym_hashes;
1598 bfd_signed_vma *local_got_refcounts;
1599 const Elf_Internal_Rela *rel, *relend;
1601 if (info->relocatable)
1602 return TRUE;
1604 elf_section_data (sec)->local_dynrel = NULL;
1606 symtab_hdr = &elf_symtab_hdr (abfd);
1607 sym_hashes = elf_sym_hashes (abfd);
1608 local_got_refcounts = elf_local_got_refcounts (abfd);
1610 relend = relocs + sec->reloc_count;
1611 for (rel = relocs; rel < relend; rel++)
1613 unsigned long r_symndx;
1614 unsigned int r_type;
1615 struct elf_link_hash_entry *h = NULL;
1617 r_symndx = ELF64_R_SYM (rel->r_info);
1618 if (r_symndx >= symtab_hdr->sh_info)
1620 struct elf64_x86_64_link_hash_entry *eh;
1621 struct elf_dyn_relocs **pp;
1622 struct elf_dyn_relocs *p;
1624 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1625 while (h->root.type == bfd_link_hash_indirect
1626 || h->root.type == bfd_link_hash_warning)
1627 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1628 eh = (struct elf64_x86_64_link_hash_entry *) h;
1630 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1631 if (p->sec == sec)
1633 /* Everything must go for SEC. */
1634 *pp = p->next;
1635 break;
1639 r_type = ELF64_R_TYPE (rel->r_info);
1640 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1641 symtab_hdr, sym_hashes,
1642 &r_type, GOT_UNKNOWN,
1643 rel, relend, h, r_symndx))
1644 return FALSE;
1646 switch (r_type)
1648 case R_X86_64_TLSLD:
1649 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1650 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1651 break;
1653 case R_X86_64_TLSGD:
1654 case R_X86_64_GOTPC32_TLSDESC:
1655 case R_X86_64_TLSDESC_CALL:
1656 case R_X86_64_GOTTPOFF:
1657 case R_X86_64_GOT32:
1658 case R_X86_64_GOTPCREL:
1659 case R_X86_64_GOT64:
1660 case R_X86_64_GOTPCREL64:
1661 case R_X86_64_GOTPLT64:
1662 if (h != NULL)
1664 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1665 h->plt.refcount -= 1;
1666 if (h->got.refcount > 0)
1667 h->got.refcount -= 1;
1669 else if (local_got_refcounts != NULL)
1671 if (local_got_refcounts[r_symndx] > 0)
1672 local_got_refcounts[r_symndx] -= 1;
1674 break;
1676 case R_X86_64_8:
1677 case R_X86_64_16:
1678 case R_X86_64_32:
1679 case R_X86_64_64:
1680 case R_X86_64_32S:
1681 case R_X86_64_PC8:
1682 case R_X86_64_PC16:
1683 case R_X86_64_PC32:
1684 case R_X86_64_PC64:
1685 if (info->shared)
1686 break;
1687 /* Fall thru */
1689 case R_X86_64_PLT32:
1690 case R_X86_64_PLTOFF64:
1691 if (h != NULL)
1693 if (h->plt.refcount > 0)
1694 h->plt.refcount -= 1;
1696 break;
1698 default:
1699 break;
1703 return TRUE;
1706 /* Adjust a symbol defined by a dynamic object and referenced by a
1707 regular object. The current definition is in some section of the
1708 dynamic object, but we're not including those sections. We have to
1709 change the definition to something the rest of the link can
1710 understand. */
1712 static bfd_boolean
1713 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1714 struct elf_link_hash_entry *h)
1716 struct elf64_x86_64_link_hash_table *htab;
1717 asection *s;
1719 /* STT_GNU_IFUNC symbol must go through PLT. */
1720 if (h->type == STT_GNU_IFUNC)
1722 if (h->plt.refcount <= 0)
1724 h->plt.offset = (bfd_vma) -1;
1725 h->needs_plt = 0;
1727 return TRUE;
1730 /* If this is a function, put it in the procedure linkage table. We
1731 will fill in the contents of the procedure linkage table later,
1732 when we know the address of the .got section. */
1733 if (h->type == STT_FUNC
1734 || h->needs_plt)
1736 if (h->plt.refcount <= 0
1737 || SYMBOL_CALLS_LOCAL (info, h)
1738 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1739 && h->root.type == bfd_link_hash_undefweak))
1741 /* This case can occur if we saw a PLT32 reloc in an input
1742 file, but the symbol was never referred to by a dynamic
1743 object, or if all references were garbage collected. In
1744 such a case, we don't actually need to build a procedure
1745 linkage table, and we can just do a PC32 reloc instead. */
1746 h->plt.offset = (bfd_vma) -1;
1747 h->needs_plt = 0;
1750 return TRUE;
1752 else
1753 /* It's possible that we incorrectly decided a .plt reloc was
1754 needed for an R_X86_64_PC32 reloc to a non-function sym in
1755 check_relocs. We can't decide accurately between function and
1756 non-function syms in check-relocs; Objects loaded later in
1757 the link may change h->type. So fix it now. */
1758 h->plt.offset = (bfd_vma) -1;
1760 /* If this is a weak symbol, and there is a real definition, the
1761 processor independent code will have arranged for us to see the
1762 real definition first, and we can just use the same value. */
1763 if (h->u.weakdef != NULL)
1765 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1766 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1767 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1768 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1769 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1770 h->non_got_ref = h->u.weakdef->non_got_ref;
1771 return TRUE;
1774 /* This is a reference to a symbol defined by a dynamic object which
1775 is not a function. */
1777 /* If we are creating a shared library, we must presume that the
1778 only references to the symbol are via the global offset table.
1779 For such cases we need not do anything here; the relocations will
1780 be handled correctly by relocate_section. */
1781 if (info->shared)
1782 return TRUE;
1784 /* If there are no references to this symbol that do not use the
1785 GOT, we don't need to generate a copy reloc. */
1786 if (!h->non_got_ref)
1787 return TRUE;
1789 /* If -z nocopyreloc was given, we won't generate them either. */
1790 if (info->nocopyreloc)
1792 h->non_got_ref = 0;
1793 return TRUE;
1796 if (ELIMINATE_COPY_RELOCS)
1798 struct elf64_x86_64_link_hash_entry * eh;
1799 struct elf_dyn_relocs *p;
1801 eh = (struct elf64_x86_64_link_hash_entry *) h;
1802 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1804 s = p->sec->output_section;
1805 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1806 break;
1809 /* If we didn't find any dynamic relocs in read-only sections, then
1810 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1811 if (p == NULL)
1813 h->non_got_ref = 0;
1814 return TRUE;
1818 if (h->size == 0)
1820 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1821 h->root.root.string);
1822 return TRUE;
1825 /* We must allocate the symbol in our .dynbss section, which will
1826 become part of the .bss section of the executable. There will be
1827 an entry for this symbol in the .dynsym section. The dynamic
1828 object will contain position independent code, so all references
1829 from the dynamic object to this symbol will go through the global
1830 offset table. The dynamic linker will use the .dynsym entry to
1831 determine the address it must put in the global offset table, so
1832 both the dynamic object and the regular object will refer to the
1833 same memory location for the variable. */
1835 htab = elf64_x86_64_hash_table (info);
1837 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1838 to copy the initial value out of the dynamic object and into the
1839 runtime process image. */
1840 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1842 htab->srelbss->size += sizeof (Elf64_External_Rela);
1843 h->needs_copy = 1;
1846 s = htab->sdynbss;
1848 return _bfd_elf_adjust_dynamic_copy (h, s);
1851 /* Allocate space in .plt, .got and associated reloc sections for
1852 dynamic relocs. */
1854 static bfd_boolean
1855 elf64_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1857 struct bfd_link_info *info;
1858 struct elf64_x86_64_link_hash_table *htab;
1859 struct elf64_x86_64_link_hash_entry *eh;
1860 struct elf_dyn_relocs *p;
1862 if (h->root.type == bfd_link_hash_indirect)
1863 return TRUE;
1865 if (h->root.type == bfd_link_hash_warning)
1866 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1867 eh = (struct elf64_x86_64_link_hash_entry *) h;
1869 info = (struct bfd_link_info *) inf;
1870 htab = elf64_x86_64_hash_table (info);
1872 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1873 here if it is defined and referenced in a non-shared object. */
1874 if (h->type == STT_GNU_IFUNC
1875 && h->def_regular)
1876 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
1877 &eh->dyn_relocs,
1878 PLT_ENTRY_SIZE,
1879 GOT_ENTRY_SIZE);
1880 else if (htab->elf.dynamic_sections_created
1881 && h->plt.refcount > 0)
1883 /* Make sure this symbol is output as a dynamic symbol.
1884 Undefined weak syms won't yet be marked as dynamic. */
1885 if (h->dynindx == -1
1886 && !h->forced_local)
1888 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1889 return FALSE;
1892 if (info->shared
1893 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1895 asection *s = htab->elf.splt;
1897 /* If this is the first .plt entry, make room for the special
1898 first entry. */
1899 if (s->size == 0)
1900 s->size += PLT_ENTRY_SIZE;
1902 h->plt.offset = s->size;
1904 /* If this symbol is not defined in a regular file, and we are
1905 not generating a shared library, then set the symbol to this
1906 location in the .plt. This is required to make function
1907 pointers compare as equal between the normal executable and
1908 the shared library. */
1909 if (! info->shared
1910 && !h->def_regular)
1912 h->root.u.def.section = s;
1913 h->root.u.def.value = h->plt.offset;
1916 /* Make room for this entry. */
1917 s->size += PLT_ENTRY_SIZE;
1919 /* We also need to make an entry in the .got.plt section, which
1920 will be placed in the .got section by the linker script. */
1921 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1923 /* We also need to make an entry in the .rela.plt section. */
1924 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1925 htab->elf.srelplt->reloc_count++;
1927 else
1929 h->plt.offset = (bfd_vma) -1;
1930 h->needs_plt = 0;
1933 else
1935 h->plt.offset = (bfd_vma) -1;
1936 h->needs_plt = 0;
1939 eh->tlsdesc_got = (bfd_vma) -1;
1941 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1942 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1943 if (h->got.refcount > 0
1944 && !info->shared
1945 && h->dynindx == -1
1946 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1948 h->got.offset = (bfd_vma) -1;
1950 else if (h->got.refcount > 0)
1952 asection *s;
1953 bfd_boolean dyn;
1954 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1956 /* Make sure this symbol is output as a dynamic symbol.
1957 Undefined weak syms won't yet be marked as dynamic. */
1958 if (h->dynindx == -1
1959 && !h->forced_local)
1961 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1962 return FALSE;
1965 if (GOT_TLS_GDESC_P (tls_type))
1967 eh->tlsdesc_got = htab->elf.sgotplt->size
1968 - elf64_x86_64_compute_jump_table_size (htab);
1969 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
1970 h->got.offset = (bfd_vma) -2;
1972 if (! GOT_TLS_GDESC_P (tls_type)
1973 || GOT_TLS_GD_P (tls_type))
1975 s = htab->elf.sgot;
1976 h->got.offset = s->size;
1977 s->size += GOT_ENTRY_SIZE;
1978 if (GOT_TLS_GD_P (tls_type))
1979 s->size += GOT_ENTRY_SIZE;
1981 dyn = htab->elf.dynamic_sections_created;
1982 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1983 and two if global.
1984 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1985 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1986 || tls_type == GOT_TLS_IE)
1987 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1988 else if (GOT_TLS_GD_P (tls_type))
1989 htab->elf.srelgot->size += 2 * sizeof (Elf64_External_Rela);
1990 else if (! GOT_TLS_GDESC_P (tls_type)
1991 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1992 || h->root.type != bfd_link_hash_undefweak)
1993 && (info->shared
1994 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1995 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1996 if (GOT_TLS_GDESC_P (tls_type))
1998 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1999 htab->tlsdesc_plt = (bfd_vma) -1;
2002 else
2003 h->got.offset = (bfd_vma) -1;
2005 if (eh->dyn_relocs == NULL)
2006 return TRUE;
2008 /* In the shared -Bsymbolic case, discard space allocated for
2009 dynamic pc-relative relocs against symbols which turn out to be
2010 defined in regular objects. For the normal shared case, discard
2011 space for pc-relative relocs that have become local due to symbol
2012 visibility changes. */
2014 if (info->shared)
2016 /* Relocs that use pc_count are those that appear on a call
2017 insn, or certain REL relocs that can generated via assembly.
2018 We want calls to protected symbols to resolve directly to the
2019 function rather than going via the plt. If people want
2020 function pointer comparisons to work as expected then they
2021 should avoid writing weird assembly. */
2022 if (SYMBOL_CALLS_LOCAL (info, h))
2024 struct elf_dyn_relocs **pp;
2026 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2028 p->count -= p->pc_count;
2029 p->pc_count = 0;
2030 if (p->count == 0)
2031 *pp = p->next;
2032 else
2033 pp = &p->next;
2037 /* Also discard relocs on undefined weak syms with non-default
2038 visibility. */
2039 if (eh->dyn_relocs != NULL
2040 && h->root.type == bfd_link_hash_undefweak)
2042 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2043 eh->dyn_relocs = NULL;
2045 /* Make sure undefined weak symbols are output as a dynamic
2046 symbol in PIEs. */
2047 else if (h->dynindx == -1
2048 && ! h->forced_local
2049 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2050 return FALSE;
2054 else if (ELIMINATE_COPY_RELOCS)
2056 /* For the non-shared case, discard space for relocs against
2057 symbols which turn out to need copy relocs or are not
2058 dynamic. */
2060 if (!h->non_got_ref
2061 && ((h->def_dynamic
2062 && !h->def_regular)
2063 || (htab->elf.dynamic_sections_created
2064 && (h->root.type == bfd_link_hash_undefweak
2065 || h->root.type == bfd_link_hash_undefined))))
2067 /* Make sure this symbol is output as a dynamic symbol.
2068 Undefined weak syms won't yet be marked as dynamic. */
2069 if (h->dynindx == -1
2070 && ! h->forced_local
2071 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2072 return FALSE;
2074 /* If that succeeded, we know we'll be keeping all the
2075 relocs. */
2076 if (h->dynindx != -1)
2077 goto keep;
2080 eh->dyn_relocs = NULL;
2082 keep: ;
2085 /* Finally, allocate space. */
2086 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2088 asection * sreloc;
2090 sreloc = elf_section_data (p->sec)->sreloc;
2092 BFD_ASSERT (sreloc != NULL);
2094 sreloc->size += p->count * sizeof (Elf64_External_Rela);
2097 return TRUE;
2100 /* Allocate space in .plt, .got and associated reloc sections for
2101 local dynamic relocs. */
2103 static bfd_boolean
2104 elf64_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
2106 struct elf_link_hash_entry *h
2107 = (struct elf_link_hash_entry *) *slot;
2109 if (h->type != STT_GNU_IFUNC
2110 || !h->def_regular
2111 || !h->ref_regular
2112 || !h->forced_local
2113 || h->root.type != bfd_link_hash_defined)
2114 abort ();
2116 return elf64_x86_64_allocate_dynrelocs (h, inf);
2119 /* Find any dynamic relocs that apply to read-only sections. */
2121 static bfd_boolean
2122 elf64_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2124 struct elf64_x86_64_link_hash_entry *eh;
2125 struct elf_dyn_relocs *p;
2127 if (h->root.type == bfd_link_hash_warning)
2128 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2130 eh = (struct elf64_x86_64_link_hash_entry *) h;
2131 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2133 asection *s = p->sec->output_section;
2135 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2137 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2139 info->flags |= DF_TEXTREL;
2141 /* Not an error, just cut short the traversal. */
2142 return FALSE;
2145 return TRUE;
2148 /* Set the sizes of the dynamic sections. */
2150 static bfd_boolean
2151 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2152 struct bfd_link_info *info)
2154 struct elf64_x86_64_link_hash_table *htab;
2155 bfd *dynobj;
2156 asection *s;
2157 bfd_boolean relocs;
2158 bfd *ibfd;
2160 htab = elf64_x86_64_hash_table (info);
2161 dynobj = htab->elf.dynobj;
2162 if (dynobj == NULL)
2163 abort ();
2165 if (htab->elf.dynamic_sections_created)
2167 /* Set the contents of the .interp section to the interpreter. */
2168 if (info->executable)
2170 s = bfd_get_section_by_name (dynobj, ".interp");
2171 if (s == NULL)
2172 abort ();
2173 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2174 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2178 /* Set up .got offsets for local syms, and space for local dynamic
2179 relocs. */
2180 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2182 bfd_signed_vma *local_got;
2183 bfd_signed_vma *end_local_got;
2184 char *local_tls_type;
2185 bfd_vma *local_tlsdesc_gotent;
2186 bfd_size_type locsymcount;
2187 Elf_Internal_Shdr *symtab_hdr;
2188 asection *srel;
2190 if (! is_x86_64_elf (ibfd))
2191 continue;
2193 for (s = ibfd->sections; s != NULL; s = s->next)
2195 struct elf_dyn_relocs *p;
2197 for (p = (struct elf_dyn_relocs *)
2198 (elf_section_data (s)->local_dynrel);
2199 p != NULL;
2200 p = p->next)
2202 if (!bfd_is_abs_section (p->sec)
2203 && bfd_is_abs_section (p->sec->output_section))
2205 /* Input section has been discarded, either because
2206 it is a copy of a linkonce section or due to
2207 linker script /DISCARD/, so we'll be discarding
2208 the relocs too. */
2210 else if (p->count != 0)
2212 srel = elf_section_data (p->sec)->sreloc;
2213 srel->size += p->count * sizeof (Elf64_External_Rela);
2214 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2215 info->flags |= DF_TEXTREL;
2220 local_got = elf_local_got_refcounts (ibfd);
2221 if (!local_got)
2222 continue;
2224 symtab_hdr = &elf_symtab_hdr (ibfd);
2225 locsymcount = symtab_hdr->sh_info;
2226 end_local_got = local_got + locsymcount;
2227 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
2228 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
2229 s = htab->elf.sgot;
2230 srel = htab->elf.srelgot;
2231 for (; local_got < end_local_got;
2232 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2234 *local_tlsdesc_gotent = (bfd_vma) -1;
2235 if (*local_got > 0)
2237 if (GOT_TLS_GDESC_P (*local_tls_type))
2239 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2240 - elf64_x86_64_compute_jump_table_size (htab);
2241 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2242 *local_got = (bfd_vma) -2;
2244 if (! GOT_TLS_GDESC_P (*local_tls_type)
2245 || GOT_TLS_GD_P (*local_tls_type))
2247 *local_got = s->size;
2248 s->size += GOT_ENTRY_SIZE;
2249 if (GOT_TLS_GD_P (*local_tls_type))
2250 s->size += GOT_ENTRY_SIZE;
2252 if (info->shared
2253 || GOT_TLS_GD_ANY_P (*local_tls_type)
2254 || *local_tls_type == GOT_TLS_IE)
2256 if (GOT_TLS_GDESC_P (*local_tls_type))
2258 htab->elf.srelplt->size
2259 += sizeof (Elf64_External_Rela);
2260 htab->tlsdesc_plt = (bfd_vma) -1;
2262 if (! GOT_TLS_GDESC_P (*local_tls_type)
2263 || GOT_TLS_GD_P (*local_tls_type))
2264 srel->size += sizeof (Elf64_External_Rela);
2267 else
2268 *local_got = (bfd_vma) -1;
2272 if (htab->tls_ld_got.refcount > 0)
2274 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2275 relocs. */
2276 htab->tls_ld_got.offset = htab->elf.sgot->size;
2277 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
2278 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2280 else
2281 htab->tls_ld_got.offset = -1;
2283 /* Allocate global sym .plt and .got entries, and space for global
2284 sym dynamic relocs. */
2285 elf_link_hash_traverse (&htab->elf, elf64_x86_64_allocate_dynrelocs,
2286 info);
2288 /* Allocate .plt and .got entries, and space for local symbols. */
2289 htab_traverse (htab->loc_hash_table,
2290 elf64_x86_64_allocate_local_dynrelocs,
2291 info);
2293 /* For every jump slot reserved in the sgotplt, reloc_count is
2294 incremented. However, when we reserve space for TLS descriptors,
2295 it's not incremented, so in order to compute the space reserved
2296 for them, it suffices to multiply the reloc count by the jump
2297 slot size. */
2298 if (htab->elf.srelplt)
2299 htab->sgotplt_jump_table_size
2300 = elf64_x86_64_compute_jump_table_size (htab);
2302 if (htab->tlsdesc_plt)
2304 /* If we're not using lazy TLS relocations, don't generate the
2305 PLT and GOT entries they require. */
2306 if ((info->flags & DF_BIND_NOW))
2307 htab->tlsdesc_plt = 0;
2308 else
2310 htab->tlsdesc_got = htab->elf.sgot->size;
2311 htab->elf.sgot->size += GOT_ENTRY_SIZE;
2312 /* Reserve room for the initial entry.
2313 FIXME: we could probably do away with it in this case. */
2314 if (htab->elf.splt->size == 0)
2315 htab->elf.splt->size += PLT_ENTRY_SIZE;
2316 htab->tlsdesc_plt = htab->elf.splt->size;
2317 htab->elf.splt->size += PLT_ENTRY_SIZE;
2321 /* We now have determined the sizes of the various dynamic sections.
2322 Allocate memory for them. */
2323 relocs = FALSE;
2324 for (s = dynobj->sections; s != NULL; s = s->next)
2326 if ((s->flags & SEC_LINKER_CREATED) == 0)
2327 continue;
2329 if (s == htab->elf.splt
2330 || s == htab->elf.sgot
2331 || s == htab->elf.sgotplt
2332 || s == htab->elf.iplt
2333 || s == htab->elf.igotplt
2334 || s == htab->sdynbss)
2336 /* Strip this section if we don't need it; see the
2337 comment below. */
2339 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2341 if (s->size != 0 && s != htab->elf.srelplt)
2342 relocs = TRUE;
2344 /* We use the reloc_count field as a counter if we need
2345 to copy relocs into the output file. */
2346 if (s != htab->elf.srelplt)
2347 s->reloc_count = 0;
2349 else
2351 /* It's not one of our sections, so don't allocate space. */
2352 continue;
2355 if (s->size == 0)
2357 /* If we don't need this section, strip it from the
2358 output file. This is mostly to handle .rela.bss and
2359 .rela.plt. We must create both sections in
2360 create_dynamic_sections, because they must be created
2361 before the linker maps input sections to output
2362 sections. The linker does that before
2363 adjust_dynamic_symbol is called, and it is that
2364 function which decides whether anything needs to go
2365 into these sections. */
2367 s->flags |= SEC_EXCLUDE;
2368 continue;
2371 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2372 continue;
2374 /* Allocate memory for the section contents. We use bfd_zalloc
2375 here in case unused entries are not reclaimed before the
2376 section's contents are written out. This should not happen,
2377 but this way if it does, we get a R_X86_64_NONE reloc instead
2378 of garbage. */
2379 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2380 if (s->contents == NULL)
2381 return FALSE;
2384 if (htab->elf.dynamic_sections_created)
2386 /* Add some entries to the .dynamic section. We fill in the
2387 values later, in elf64_x86_64_finish_dynamic_sections, but we
2388 must add the entries now so that we get the correct size for
2389 the .dynamic section. The DT_DEBUG entry is filled in by the
2390 dynamic linker and used by the debugger. */
2391 #define add_dynamic_entry(TAG, VAL) \
2392 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2394 if (info->executable)
2396 if (!add_dynamic_entry (DT_DEBUG, 0))
2397 return FALSE;
2400 if (htab->elf.splt->size != 0)
2402 if (!add_dynamic_entry (DT_PLTGOT, 0)
2403 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2404 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2405 || !add_dynamic_entry (DT_JMPREL, 0))
2406 return FALSE;
2408 if (htab->tlsdesc_plt
2409 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2410 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2411 return FALSE;
2414 if (relocs)
2416 if (!add_dynamic_entry (DT_RELA, 0)
2417 || !add_dynamic_entry (DT_RELASZ, 0)
2418 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2419 return FALSE;
2421 /* If any dynamic relocs apply to a read-only section,
2422 then we need a DT_TEXTREL entry. */
2423 if ((info->flags & DF_TEXTREL) == 0)
2424 elf_link_hash_traverse (&htab->elf,
2425 elf64_x86_64_readonly_dynrelocs,
2426 info);
2428 if ((info->flags & DF_TEXTREL) != 0)
2430 if (!add_dynamic_entry (DT_TEXTREL, 0))
2431 return FALSE;
2435 #undef add_dynamic_entry
2437 return TRUE;
2440 static bfd_boolean
2441 elf64_x86_64_always_size_sections (bfd *output_bfd,
2442 struct bfd_link_info *info)
2444 asection *tls_sec = elf_hash_table (info)->tls_sec;
2446 if (tls_sec)
2448 struct elf_link_hash_entry *tlsbase;
2450 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2451 "_TLS_MODULE_BASE_",
2452 FALSE, FALSE, FALSE);
2454 if (tlsbase && tlsbase->type == STT_TLS)
2456 struct bfd_link_hash_entry *bh = NULL;
2457 const struct elf_backend_data *bed
2458 = get_elf_backend_data (output_bfd);
2460 if (!(_bfd_generic_link_add_one_symbol
2461 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2462 tls_sec, 0, NULL, FALSE,
2463 bed->collect, &bh)))
2464 return FALSE;
2466 elf64_x86_64_hash_table (info)->tls_module_base = bh;
2468 tlsbase = (struct elf_link_hash_entry *)bh;
2469 tlsbase->def_regular = 1;
2470 tlsbase->other = STV_HIDDEN;
2471 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2475 return TRUE;
2478 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2479 executables. Rather than setting it to the beginning of the TLS
2480 section, we have to set it to the end. This function may be called
2481 multiple times, it is idempotent. */
2483 static void
2484 elf64_x86_64_set_tls_module_base (struct bfd_link_info *info)
2486 struct bfd_link_hash_entry *base;
2488 if (!info->executable)
2489 return;
2491 base = elf64_x86_64_hash_table (info)->tls_module_base;
2493 if (!base)
2494 return;
2496 base->u.def.value = elf_hash_table (info)->tls_size;
2499 /* Return the base VMA address which should be subtracted from real addresses
2500 when resolving @dtpoff relocation.
2501 This is PT_TLS segment p_vaddr. */
2503 static bfd_vma
2504 elf64_x86_64_dtpoff_base (struct bfd_link_info *info)
2506 /* If tls_sec is NULL, we should have signalled an error already. */
2507 if (elf_hash_table (info)->tls_sec == NULL)
2508 return 0;
2509 return elf_hash_table (info)->tls_sec->vma;
2512 /* Return the relocation value for @tpoff relocation
2513 if STT_TLS virtual address is ADDRESS. */
2515 static bfd_vma
2516 elf64_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2518 struct elf_link_hash_table *htab = elf_hash_table (info);
2520 /* If tls_segment is NULL, we should have signalled an error already. */
2521 if (htab->tls_sec == NULL)
2522 return 0;
2523 return address - htab->tls_size - htab->tls_sec->vma;
2526 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2527 branch? */
2529 static bfd_boolean
2530 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2532 /* Opcode Instruction
2533 0xe8 call
2534 0xe9 jump
2535 0x0f 0x8x conditional jump */
2536 return ((offset > 0
2537 && (contents [offset - 1] == 0xe8
2538 || contents [offset - 1] == 0xe9))
2539 || (offset > 1
2540 && contents [offset - 2] == 0x0f
2541 && (contents [offset - 1] & 0xf0) == 0x80));
2544 /* Relocate an x86_64 ELF section. */
2546 static bfd_boolean
2547 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2548 bfd *input_bfd, asection *input_section,
2549 bfd_byte *contents, Elf_Internal_Rela *relocs,
2550 Elf_Internal_Sym *local_syms,
2551 asection **local_sections)
2553 struct elf64_x86_64_link_hash_table *htab;
2554 Elf_Internal_Shdr *symtab_hdr;
2555 struct elf_link_hash_entry **sym_hashes;
2556 bfd_vma *local_got_offsets;
2557 bfd_vma *local_tlsdesc_gotents;
2558 Elf_Internal_Rela *rel;
2559 Elf_Internal_Rela *relend;
2561 BFD_ASSERT (is_x86_64_elf (input_bfd));
2563 htab = elf64_x86_64_hash_table (info);
2564 symtab_hdr = &elf_symtab_hdr (input_bfd);
2565 sym_hashes = elf_sym_hashes (input_bfd);
2566 local_got_offsets = elf_local_got_offsets (input_bfd);
2567 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2569 elf64_x86_64_set_tls_module_base (info);
2571 rel = relocs;
2572 relend = relocs + input_section->reloc_count;
2573 for (; rel < relend; rel++)
2575 unsigned int r_type;
2576 reloc_howto_type *howto;
2577 unsigned long r_symndx;
2578 struct elf_link_hash_entry *h;
2579 Elf_Internal_Sym *sym;
2580 asection *sec;
2581 bfd_vma off, offplt;
2582 bfd_vma relocation;
2583 bfd_boolean unresolved_reloc;
2584 bfd_reloc_status_type r;
2585 int tls_type;
2586 asection *base_got;
2588 r_type = ELF64_R_TYPE (rel->r_info);
2589 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2590 || r_type == (int) R_X86_64_GNU_VTENTRY)
2591 continue;
2593 if (r_type >= R_X86_64_max)
2595 bfd_set_error (bfd_error_bad_value);
2596 return FALSE;
2599 howto = x86_64_elf_howto_table + r_type;
2600 r_symndx = ELF64_R_SYM (rel->r_info);
2601 h = NULL;
2602 sym = NULL;
2603 sec = NULL;
2604 unresolved_reloc = FALSE;
2605 if (r_symndx < symtab_hdr->sh_info)
2607 sym = local_syms + r_symndx;
2608 sec = local_sections[r_symndx];
2610 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2611 &sec, rel);
2613 /* Relocate against local STT_GNU_IFUNC symbol. */
2614 if (ELF64_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2616 h = elf64_x86_64_get_local_sym_hash (htab, input_bfd,
2617 rel, FALSE);
2618 if (h == NULL)
2619 abort ();
2621 /* Set STT_GNU_IFUNC symbol value. */
2622 h->root.u.def.value = sym->st_value;
2623 h->root.u.def.section = sec;
2626 else
2628 bfd_boolean warned;
2630 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2631 r_symndx, symtab_hdr, sym_hashes,
2632 h, sec, relocation,
2633 unresolved_reloc, warned);
2636 if (sec != NULL && elf_discarded_section (sec))
2638 /* For relocs against symbols from removed linkonce sections,
2639 or sections discarded by a linker script, we just want the
2640 section contents zeroed. Avoid any special processing. */
2641 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2642 rel->r_info = 0;
2643 rel->r_addend = 0;
2644 continue;
2647 if (info->relocatable)
2648 continue;
2650 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2651 it here if it is defined in a non-shared object. */
2652 if (h != NULL
2653 && h->type == STT_GNU_IFUNC
2654 && h->def_regular)
2656 asection *plt;
2657 bfd_vma plt_index;
2658 const char *name;
2660 if ((input_section->flags & SEC_ALLOC) == 0
2661 || h->plt.offset == (bfd_vma) -1)
2662 abort ();
2664 /* STT_GNU_IFUNC symbol must go through PLT. */
2665 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2666 relocation = (plt->output_section->vma
2667 + plt->output_offset + h->plt.offset);
2669 switch (r_type)
2671 default:
2672 if (h->root.root.string)
2673 name = h->root.root.string;
2674 else
2675 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2676 NULL);
2677 (*_bfd_error_handler)
2678 (_("%B: relocation %s against STT_GNU_IFUNC "
2679 "symbol `%s' isn't handled by %s"), input_bfd,
2680 x86_64_elf_howto_table[r_type].name,
2681 name, __FUNCTION__);
2682 bfd_set_error (bfd_error_bad_value);
2683 return FALSE;
2685 case R_X86_64_32S:
2686 if (info->shared)
2687 abort ();
2688 goto do_relocation;
2690 case R_X86_64_64:
2691 if (rel->r_addend != 0)
2693 if (h->root.root.string)
2694 name = h->root.root.string;
2695 else
2696 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2697 sym, NULL);
2698 (*_bfd_error_handler)
2699 (_("%B: relocation %s against STT_GNU_IFUNC "
2700 "symbol `%s' has non-zero addend: %d"),
2701 input_bfd, x86_64_elf_howto_table[r_type].name,
2702 name, rel->r_addend);
2703 bfd_set_error (bfd_error_bad_value);
2704 return FALSE;
2707 /* Generate dynamic relcoation only when there is a
2708 non-GOF reference in a shared object. */
2709 if (info->shared && h->non_got_ref)
2711 Elf_Internal_Rela outrel;
2712 bfd_byte *loc;
2713 asection *sreloc;
2715 /* Need a dynamic relocation to get the real function
2716 address. */
2717 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2718 info,
2719 input_section,
2720 rel->r_offset);
2721 if (outrel.r_offset == (bfd_vma) -1
2722 || outrel.r_offset == (bfd_vma) -2)
2723 abort ();
2725 outrel.r_offset += (input_section->output_section->vma
2726 + input_section->output_offset);
2728 if (h->dynindx == -1
2729 || h->forced_local
2730 || info->executable)
2732 /* This symbol is resolved locally. */
2733 outrel.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
2734 outrel.r_addend = (h->root.u.def.value
2735 + h->root.u.def.section->output_section->vma
2736 + h->root.u.def.section->output_offset);
2738 else
2740 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2741 outrel.r_addend = 0;
2744 sreloc = htab->elf.irelifunc;
2745 loc = sreloc->contents;
2746 loc += (sreloc->reloc_count++
2747 * sizeof (Elf64_External_Rela));
2748 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2750 /* If this reloc is against an external symbol, we
2751 do not want to fiddle with the addend. Otherwise,
2752 we need to include the symbol value so that it
2753 becomes an addend for the dynamic reloc. For an
2754 internal symbol, we have updated addend. */
2755 continue;
2758 case R_X86_64_32:
2759 case R_X86_64_PC32:
2760 case R_X86_64_PC64:
2761 case R_X86_64_PLT32:
2762 goto do_relocation;
2764 case R_X86_64_GOTPCREL:
2765 case R_X86_64_GOTPCREL64:
2766 base_got = htab->elf.sgot;
2767 off = h->got.offset;
2769 if (base_got == NULL)
2770 abort ();
2772 if (off == (bfd_vma) -1)
2774 /* We can't use h->got.offset here to save state, or
2775 even just remember the offset, as finish_dynamic_symbol
2776 would use that as offset into .got. */
2778 if (htab->elf.splt != NULL)
2780 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2781 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2782 base_got = htab->elf.sgotplt;
2784 else
2786 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2787 off = plt_index * GOT_ENTRY_SIZE;
2788 base_got = htab->elf.igotplt;
2791 if (h->dynindx == -1
2792 || h->forced_local
2793 || info->symbolic)
2795 /* This references the local defitionion. We must
2796 initialize this entry in the global offset table.
2797 Since the offset must always be a multiple of 8,
2798 we use the least significant bit to record
2799 whether we have initialized it already.
2801 When doing a dynamic link, we create a .rela.got
2802 relocation entry to initialize the value. This
2803 is done in the finish_dynamic_symbol routine. */
2804 if ((off & 1) != 0)
2805 off &= ~1;
2806 else
2808 bfd_put_64 (output_bfd, relocation,
2809 base_got->contents + off);
2810 /* Note that this is harmless for the GOTPLT64
2811 case, as -1 | 1 still is -1. */
2812 h->got.offset |= 1;
2817 relocation = (base_got->output_section->vma
2818 + base_got->output_offset + off);
2820 if (r_type != R_X86_64_GOTPCREL
2821 && r_type != R_X86_64_GOTPCREL64)
2823 asection *gotplt;
2824 if (htab->elf.splt != NULL)
2825 gotplt = htab->elf.sgotplt;
2826 else
2827 gotplt = htab->elf.igotplt;
2828 relocation -= (gotplt->output_section->vma
2829 - gotplt->output_offset);
2832 goto do_relocation;
2836 /* When generating a shared object, the relocations handled here are
2837 copied into the output file to be resolved at run time. */
2838 switch (r_type)
2840 case R_X86_64_GOT32:
2841 case R_X86_64_GOT64:
2842 /* Relocation is to the entry for this symbol in the global
2843 offset table. */
2844 case R_X86_64_GOTPCREL:
2845 case R_X86_64_GOTPCREL64:
2846 /* Use global offset table entry as symbol value. */
2847 case R_X86_64_GOTPLT64:
2848 /* This is the same as GOT64 for relocation purposes, but
2849 indicates the existence of a PLT entry. The difficulty is,
2850 that we must calculate the GOT slot offset from the PLT
2851 offset, if this symbol got a PLT entry (it was global).
2852 Additionally if it's computed from the PLT entry, then that
2853 GOT offset is relative to .got.plt, not to .got. */
2854 base_got = htab->elf.sgot;
2856 if (htab->elf.sgot == NULL)
2857 abort ();
2859 if (h != NULL)
2861 bfd_boolean dyn;
2863 off = h->got.offset;
2864 if (h->needs_plt
2865 && h->plt.offset != (bfd_vma)-1
2866 && off == (bfd_vma)-1)
2868 /* We can't use h->got.offset here to save
2869 state, or even just remember the offset, as
2870 finish_dynamic_symbol would use that as offset into
2871 .got. */
2872 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2873 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2874 base_got = htab->elf.sgotplt;
2877 dyn = htab->elf.dynamic_sections_created;
2879 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2880 || (info->shared
2881 && SYMBOL_REFERENCES_LOCAL (info, h))
2882 || (ELF_ST_VISIBILITY (h->other)
2883 && h->root.type == bfd_link_hash_undefweak))
2885 /* This is actually a static link, or it is a -Bsymbolic
2886 link and the symbol is defined locally, or the symbol
2887 was forced to be local because of a version file. We
2888 must initialize this entry in the global offset table.
2889 Since the offset must always be a multiple of 8, we
2890 use the least significant bit to record whether we
2891 have initialized it already.
2893 When doing a dynamic link, we create a .rela.got
2894 relocation entry to initialize the value. This is
2895 done in the finish_dynamic_symbol routine. */
2896 if ((off & 1) != 0)
2897 off &= ~1;
2898 else
2900 bfd_put_64 (output_bfd, relocation,
2901 base_got->contents + off);
2902 /* Note that this is harmless for the GOTPLT64 case,
2903 as -1 | 1 still is -1. */
2904 h->got.offset |= 1;
2907 else
2908 unresolved_reloc = FALSE;
2910 else
2912 if (local_got_offsets == NULL)
2913 abort ();
2915 off = local_got_offsets[r_symndx];
2917 /* The offset must always be a multiple of 8. We use
2918 the least significant bit to record whether we have
2919 already generated the necessary reloc. */
2920 if ((off & 1) != 0)
2921 off &= ~1;
2922 else
2924 bfd_put_64 (output_bfd, relocation,
2925 base_got->contents + off);
2927 if (info->shared)
2929 asection *s;
2930 Elf_Internal_Rela outrel;
2931 bfd_byte *loc;
2933 /* We need to generate a R_X86_64_RELATIVE reloc
2934 for the dynamic linker. */
2935 s = htab->elf.srelgot;
2936 if (s == NULL)
2937 abort ();
2939 outrel.r_offset = (base_got->output_section->vma
2940 + base_got->output_offset
2941 + off);
2942 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2943 outrel.r_addend = relocation;
2944 loc = s->contents;
2945 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2946 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2949 local_got_offsets[r_symndx] |= 1;
2953 if (off >= (bfd_vma) -2)
2954 abort ();
2956 relocation = base_got->output_section->vma
2957 + base_got->output_offset + off;
2958 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
2959 relocation -= htab->elf.sgotplt->output_section->vma
2960 - htab->elf.sgotplt->output_offset;
2962 break;
2964 case R_X86_64_GOTOFF64:
2965 /* Relocation is relative to the start of the global offset
2966 table. */
2968 /* Check to make sure it isn't a protected function symbol
2969 for shared library since it may not be local when used
2970 as function address. */
2971 if (info->shared
2972 && h
2973 && h->def_regular
2974 && h->type == STT_FUNC
2975 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2977 (*_bfd_error_handler)
2978 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2979 input_bfd, h->root.root.string);
2980 bfd_set_error (bfd_error_bad_value);
2981 return FALSE;
2984 /* Note that sgot is not involved in this
2985 calculation. We always want the start of .got.plt. If we
2986 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2987 permitted by the ABI, we might have to change this
2988 calculation. */
2989 relocation -= htab->elf.sgotplt->output_section->vma
2990 + htab->elf.sgotplt->output_offset;
2991 break;
2993 case R_X86_64_GOTPC32:
2994 case R_X86_64_GOTPC64:
2995 /* Use global offset table as symbol value. */
2996 relocation = htab->elf.sgotplt->output_section->vma
2997 + htab->elf.sgotplt->output_offset;
2998 unresolved_reloc = FALSE;
2999 break;
3001 case R_X86_64_PLTOFF64:
3002 /* Relocation is PLT entry relative to GOT. For local
3003 symbols it's the symbol itself relative to GOT. */
3004 if (h != NULL
3005 /* See PLT32 handling. */
3006 && h->plt.offset != (bfd_vma) -1
3007 && htab->elf.splt != NULL)
3009 relocation = (htab->elf.splt->output_section->vma
3010 + htab->elf.splt->output_offset
3011 + h->plt.offset);
3012 unresolved_reloc = FALSE;
3015 relocation -= htab->elf.sgotplt->output_section->vma
3016 + htab->elf.sgotplt->output_offset;
3017 break;
3019 case R_X86_64_PLT32:
3020 /* Relocation is to the entry for this symbol in the
3021 procedure linkage table. */
3023 /* Resolve a PLT32 reloc against a local symbol directly,
3024 without using the procedure linkage table. */
3025 if (h == NULL)
3026 break;
3028 if (h->plt.offset == (bfd_vma) -1
3029 || htab->elf.splt == NULL)
3031 /* We didn't make a PLT entry for this symbol. This
3032 happens when statically linking PIC code, or when
3033 using -Bsymbolic. */
3034 break;
3037 relocation = (htab->elf.splt->output_section->vma
3038 + htab->elf.splt->output_offset
3039 + h->plt.offset);
3040 unresolved_reloc = FALSE;
3041 break;
3043 case R_X86_64_PC8:
3044 case R_X86_64_PC16:
3045 case R_X86_64_PC32:
3046 if (info->shared
3047 && (input_section->flags & SEC_ALLOC) != 0
3048 && (input_section->flags & SEC_READONLY) != 0
3049 && h != NULL)
3051 bfd_boolean fail = FALSE;
3052 bfd_boolean branch
3053 = (r_type == R_X86_64_PC32
3054 && is_32bit_relative_branch (contents, rel->r_offset));
3056 if (SYMBOL_REFERENCES_LOCAL (info, h))
3058 /* Symbol is referenced locally. Make sure it is
3059 defined locally or for a branch. */
3060 fail = !h->def_regular && !branch;
3062 else
3064 /* Symbol isn't referenced locally. We only allow
3065 branch to symbol with non-default visibility. */
3066 fail = (!branch
3067 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3070 if (fail)
3072 const char *fmt;
3073 const char *v;
3074 const char *pic = "";
3076 switch (ELF_ST_VISIBILITY (h->other))
3078 case STV_HIDDEN:
3079 v = _("hidden symbol");
3080 break;
3081 case STV_INTERNAL:
3082 v = _("internal symbol");
3083 break;
3084 case STV_PROTECTED:
3085 v = _("protected symbol");
3086 break;
3087 default:
3088 v = _("symbol");
3089 pic = _("; recompile with -fPIC");
3090 break;
3093 if (h->def_regular)
3094 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3095 else
3096 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3098 (*_bfd_error_handler) (fmt, input_bfd,
3099 x86_64_elf_howto_table[r_type].name,
3100 v, h->root.root.string, pic);
3101 bfd_set_error (bfd_error_bad_value);
3102 return FALSE;
3105 /* Fall through. */
3107 case R_X86_64_8:
3108 case R_X86_64_16:
3109 case R_X86_64_32:
3110 case R_X86_64_PC64:
3111 case R_X86_64_64:
3112 /* FIXME: The ABI says the linker should make sure the value is
3113 the same when it's zeroextended to 64 bit. */
3115 if ((input_section->flags & SEC_ALLOC) == 0)
3116 break;
3118 if ((info->shared
3119 && (h == NULL
3120 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3121 || h->root.type != bfd_link_hash_undefweak)
3122 && (! IS_X86_64_PCREL_TYPE (r_type)
3123 || ! SYMBOL_CALLS_LOCAL (info, h)))
3124 || (ELIMINATE_COPY_RELOCS
3125 && !info->shared
3126 && h != NULL
3127 && h->dynindx != -1
3128 && !h->non_got_ref
3129 && ((h->def_dynamic
3130 && !h->def_regular)
3131 || h->root.type == bfd_link_hash_undefweak
3132 || h->root.type == bfd_link_hash_undefined)))
3134 Elf_Internal_Rela outrel;
3135 bfd_byte *loc;
3136 bfd_boolean skip, relocate;
3137 asection *sreloc;
3139 /* When generating a shared object, these relocations
3140 are copied into the output file to be resolved at run
3141 time. */
3142 skip = FALSE;
3143 relocate = FALSE;
3145 outrel.r_offset =
3146 _bfd_elf_section_offset (output_bfd, info, input_section,
3147 rel->r_offset);
3148 if (outrel.r_offset == (bfd_vma) -1)
3149 skip = TRUE;
3150 else if (outrel.r_offset == (bfd_vma) -2)
3151 skip = TRUE, relocate = TRUE;
3153 outrel.r_offset += (input_section->output_section->vma
3154 + input_section->output_offset);
3156 if (skip)
3157 memset (&outrel, 0, sizeof outrel);
3159 /* h->dynindx may be -1 if this symbol was marked to
3160 become local. */
3161 else if (h != NULL
3162 && h->dynindx != -1
3163 && (IS_X86_64_PCREL_TYPE (r_type)
3164 || ! info->shared
3165 || ! SYMBOLIC_BIND (info, h)
3166 || ! h->def_regular))
3168 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3169 outrel.r_addend = rel->r_addend;
3171 else
3173 /* This symbol is local, or marked to become local. */
3174 if (r_type == R_X86_64_64)
3176 relocate = TRUE;
3177 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3178 outrel.r_addend = relocation + rel->r_addend;
3180 else
3182 long sindx;
3184 if (bfd_is_abs_section (sec))
3185 sindx = 0;
3186 else if (sec == NULL || sec->owner == NULL)
3188 bfd_set_error (bfd_error_bad_value);
3189 return FALSE;
3191 else
3193 asection *osec;
3195 /* We are turning this relocation into one
3196 against a section symbol. It would be
3197 proper to subtract the symbol's value,
3198 osec->vma, from the emitted reloc addend,
3199 but ld.so expects buggy relocs. */
3200 osec = sec->output_section;
3201 sindx = elf_section_data (osec)->dynindx;
3202 if (sindx == 0)
3204 asection *oi = htab->elf.text_index_section;
3205 sindx = elf_section_data (oi)->dynindx;
3207 BFD_ASSERT (sindx != 0);
3210 outrel.r_info = ELF64_R_INFO (sindx, r_type);
3211 outrel.r_addend = relocation + rel->r_addend;
3215 sreloc = elf_section_data (input_section)->sreloc;
3217 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3219 loc = sreloc->contents;
3220 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3221 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3223 /* If this reloc is against an external symbol, we do
3224 not want to fiddle with the addend. Otherwise, we
3225 need to include the symbol value so that it becomes
3226 an addend for the dynamic reloc. */
3227 if (! relocate)
3228 continue;
3231 break;
3233 case R_X86_64_TLSGD:
3234 case R_X86_64_GOTPC32_TLSDESC:
3235 case R_X86_64_TLSDESC_CALL:
3236 case R_X86_64_GOTTPOFF:
3237 tls_type = GOT_UNKNOWN;
3238 if (h == NULL && local_got_offsets)
3239 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3240 else if (h != NULL)
3241 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
3243 if (! elf64_x86_64_tls_transition (info, input_bfd,
3244 input_section, contents,
3245 symtab_hdr, sym_hashes,
3246 &r_type, tls_type, rel,
3247 relend, h, r_symndx))
3248 return FALSE;
3250 if (r_type == R_X86_64_TPOFF32)
3252 bfd_vma roff = rel->r_offset;
3254 BFD_ASSERT (! unresolved_reloc);
3256 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3258 /* GD->LE transition.
3259 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3260 .word 0x6666; rex64; call __tls_get_addr
3261 Change it into:
3262 movq %fs:0, %rax
3263 leaq foo@tpoff(%rax), %rax */
3264 memcpy (contents + roff - 4,
3265 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3266 16);
3267 bfd_put_32 (output_bfd,
3268 elf64_x86_64_tpoff (info, relocation),
3269 contents + roff + 8);
3270 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3271 rel++;
3272 continue;
3274 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3276 /* GDesc -> LE transition.
3277 It's originally something like:
3278 leaq x@tlsdesc(%rip), %rax
3280 Change it to:
3281 movl $x@tpoff, %rax
3284 unsigned int val, type, type2;
3286 type = bfd_get_8 (input_bfd, contents + roff - 3);
3287 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3288 val = bfd_get_8 (input_bfd, contents + roff - 1);
3289 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3290 contents + roff - 3);
3291 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3292 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3293 contents + roff - 1);
3294 bfd_put_32 (output_bfd,
3295 elf64_x86_64_tpoff (info, relocation),
3296 contents + roff);
3297 continue;
3299 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3301 /* GDesc -> LE transition.
3302 It's originally:
3303 call *(%rax)
3304 Turn it into:
3305 xchg %ax,%ax. */
3306 bfd_put_8 (output_bfd, 0x66, contents + roff);
3307 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3308 continue;
3310 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3312 /* IE->LE transition:
3313 Originally it can be one of:
3314 movq foo@gottpoff(%rip), %reg
3315 addq foo@gottpoff(%rip), %reg
3316 We change it into:
3317 movq $foo, %reg
3318 leaq foo(%reg), %reg
3319 addq $foo, %reg. */
3321 unsigned int val, type, reg;
3323 val = bfd_get_8 (input_bfd, contents + roff - 3);
3324 type = bfd_get_8 (input_bfd, contents + roff - 2);
3325 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3326 reg >>= 3;
3327 if (type == 0x8b)
3329 /* movq */
3330 if (val == 0x4c)
3331 bfd_put_8 (output_bfd, 0x49,
3332 contents + roff - 3);
3333 bfd_put_8 (output_bfd, 0xc7,
3334 contents + roff - 2);
3335 bfd_put_8 (output_bfd, 0xc0 | reg,
3336 contents + roff - 1);
3338 else if (reg == 4)
3340 /* addq -> addq - addressing with %rsp/%r12 is
3341 special */
3342 if (val == 0x4c)
3343 bfd_put_8 (output_bfd, 0x49,
3344 contents + roff - 3);
3345 bfd_put_8 (output_bfd, 0x81,
3346 contents + roff - 2);
3347 bfd_put_8 (output_bfd, 0xc0 | reg,
3348 contents + roff - 1);
3350 else
3352 /* addq -> leaq */
3353 if (val == 0x4c)
3354 bfd_put_8 (output_bfd, 0x4d,
3355 contents + roff - 3);
3356 bfd_put_8 (output_bfd, 0x8d,
3357 contents + roff - 2);
3358 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3359 contents + roff - 1);
3361 bfd_put_32 (output_bfd,
3362 elf64_x86_64_tpoff (info, relocation),
3363 contents + roff);
3364 continue;
3366 else
3367 BFD_ASSERT (FALSE);
3370 if (htab->elf.sgot == NULL)
3371 abort ();
3373 if (h != NULL)
3375 off = h->got.offset;
3376 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
3378 else
3380 if (local_got_offsets == NULL)
3381 abort ();
3383 off = local_got_offsets[r_symndx];
3384 offplt = local_tlsdesc_gotents[r_symndx];
3387 if ((off & 1) != 0)
3388 off &= ~1;
3389 else
3391 Elf_Internal_Rela outrel;
3392 bfd_byte *loc;
3393 int dr_type, indx;
3394 asection *sreloc;
3396 if (htab->elf.srelgot == NULL)
3397 abort ();
3399 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3401 if (GOT_TLS_GDESC_P (tls_type))
3403 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
3404 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3405 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3406 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3407 + htab->elf.sgotplt->output_offset
3408 + offplt
3409 + htab->sgotplt_jump_table_size);
3410 sreloc = htab->elf.srelplt;
3411 loc = sreloc->contents;
3412 loc += sreloc->reloc_count++
3413 * sizeof (Elf64_External_Rela);
3414 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3415 <= sreloc->contents + sreloc->size);
3416 if (indx == 0)
3417 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3418 else
3419 outrel.r_addend = 0;
3420 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3423 sreloc = htab->elf.srelgot;
3425 outrel.r_offset = (htab->elf.sgot->output_section->vma
3426 + htab->elf.sgot->output_offset + off);
3428 if (GOT_TLS_GD_P (tls_type))
3429 dr_type = R_X86_64_DTPMOD64;
3430 else if (GOT_TLS_GDESC_P (tls_type))
3431 goto dr_done;
3432 else
3433 dr_type = R_X86_64_TPOFF64;
3435 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3436 outrel.r_addend = 0;
3437 if ((dr_type == R_X86_64_TPOFF64
3438 || dr_type == R_X86_64_TLSDESC) && indx == 0)
3439 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3440 outrel.r_info = ELF64_R_INFO (indx, dr_type);
3442 loc = sreloc->contents;
3443 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3444 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3445 <= sreloc->contents + sreloc->size);
3446 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3448 if (GOT_TLS_GD_P (tls_type))
3450 if (indx == 0)
3452 BFD_ASSERT (! unresolved_reloc);
3453 bfd_put_64 (output_bfd,
3454 relocation - elf64_x86_64_dtpoff_base (info),
3455 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3457 else
3459 bfd_put_64 (output_bfd, 0,
3460 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3461 outrel.r_info = ELF64_R_INFO (indx,
3462 R_X86_64_DTPOFF64);
3463 outrel.r_offset += GOT_ENTRY_SIZE;
3464 sreloc->reloc_count++;
3465 loc += sizeof (Elf64_External_Rela);
3466 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3467 <= sreloc->contents + sreloc->size);
3468 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3472 dr_done:
3473 if (h != NULL)
3474 h->got.offset |= 1;
3475 else
3476 local_got_offsets[r_symndx] |= 1;
3479 if (off >= (bfd_vma) -2
3480 && ! GOT_TLS_GDESC_P (tls_type))
3481 abort ();
3482 if (r_type == ELF64_R_TYPE (rel->r_info))
3484 if (r_type == R_X86_64_GOTPC32_TLSDESC
3485 || r_type == R_X86_64_TLSDESC_CALL)
3486 relocation = htab->elf.sgotplt->output_section->vma
3487 + htab->elf.sgotplt->output_offset
3488 + offplt + htab->sgotplt_jump_table_size;
3489 else
3490 relocation = htab->elf.sgot->output_section->vma
3491 + htab->elf.sgot->output_offset + off;
3492 unresolved_reloc = FALSE;
3494 else
3496 bfd_vma roff = rel->r_offset;
3498 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3500 /* GD->IE transition.
3501 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3502 .word 0x6666; rex64; call __tls_get_addr@plt
3503 Change it into:
3504 movq %fs:0, %rax
3505 addq foo@gottpoff(%rip), %rax */
3506 memcpy (contents + roff - 4,
3507 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3508 16);
3510 relocation = (htab->elf.sgot->output_section->vma
3511 + htab->elf.sgot->output_offset + off
3512 - roff
3513 - input_section->output_section->vma
3514 - input_section->output_offset
3515 - 12);
3516 bfd_put_32 (output_bfd, relocation,
3517 contents + roff + 8);
3518 /* Skip R_X86_64_PLT32. */
3519 rel++;
3520 continue;
3522 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3524 /* GDesc -> IE transition.
3525 It's originally something like:
3526 leaq x@tlsdesc(%rip), %rax
3528 Change it to:
3529 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3532 unsigned int val, type, type2;
3534 type = bfd_get_8 (input_bfd, contents + roff - 3);
3535 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3536 val = bfd_get_8 (input_bfd, contents + roff - 1);
3538 /* Now modify the instruction as appropriate. To
3539 turn a leaq into a movq in the form we use it, it
3540 suffices to change the second byte from 0x8d to
3541 0x8b. */
3542 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3544 bfd_put_32 (output_bfd,
3545 htab->elf.sgot->output_section->vma
3546 + htab->elf.sgot->output_offset + off
3547 - rel->r_offset
3548 - input_section->output_section->vma
3549 - input_section->output_offset
3550 - 4,
3551 contents + roff);
3552 continue;
3554 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3556 /* GDesc -> IE transition.
3557 It's originally:
3558 call *(%rax)
3560 Change it to:
3561 xchg %ax,%ax. */
3563 unsigned int val, type;
3565 type = bfd_get_8 (input_bfd, contents + roff);
3566 val = bfd_get_8 (input_bfd, contents + roff + 1);
3567 bfd_put_8 (output_bfd, 0x66, contents + roff);
3568 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3569 continue;
3571 else
3572 BFD_ASSERT (FALSE);
3574 break;
3576 case R_X86_64_TLSLD:
3577 if (! elf64_x86_64_tls_transition (info, input_bfd,
3578 input_section, contents,
3579 symtab_hdr, sym_hashes,
3580 &r_type, GOT_UNKNOWN,
3581 rel, relend, h, r_symndx))
3582 return FALSE;
3584 if (r_type != R_X86_64_TLSLD)
3586 /* LD->LE transition:
3587 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3588 We change it into:
3589 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3591 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3592 memcpy (contents + rel->r_offset - 3,
3593 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3594 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3595 rel++;
3596 continue;
3599 if (htab->elf.sgot == NULL)
3600 abort ();
3602 off = htab->tls_ld_got.offset;
3603 if (off & 1)
3604 off &= ~1;
3605 else
3607 Elf_Internal_Rela outrel;
3608 bfd_byte *loc;
3610 if (htab->elf.srelgot == NULL)
3611 abort ();
3613 outrel.r_offset = (htab->elf.sgot->output_section->vma
3614 + htab->elf.sgot->output_offset + off);
3616 bfd_put_64 (output_bfd, 0,
3617 htab->elf.sgot->contents + off);
3618 bfd_put_64 (output_bfd, 0,
3619 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3620 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3621 outrel.r_addend = 0;
3622 loc = htab->elf.srelgot->contents;
3623 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3624 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3625 htab->tls_ld_got.offset |= 1;
3627 relocation = htab->elf.sgot->output_section->vma
3628 + htab->elf.sgot->output_offset + off;
3629 unresolved_reloc = FALSE;
3630 break;
3632 case R_X86_64_DTPOFF32:
3633 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3634 relocation -= elf64_x86_64_dtpoff_base (info);
3635 else
3636 relocation = elf64_x86_64_tpoff (info, relocation);
3637 break;
3639 case R_X86_64_TPOFF32:
3640 BFD_ASSERT (! info->shared);
3641 relocation = elf64_x86_64_tpoff (info, relocation);
3642 break;
3644 default:
3645 break;
3648 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3649 because such sections are not SEC_ALLOC and thus ld.so will
3650 not process them. */
3651 if (unresolved_reloc
3652 && !((input_section->flags & SEC_DEBUGGING) != 0
3653 && h->def_dynamic))
3654 (*_bfd_error_handler)
3655 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3656 input_bfd,
3657 input_section,
3658 (long) rel->r_offset,
3659 howto->name,
3660 h->root.root.string);
3662 do_relocation:
3663 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3664 contents, rel->r_offset,
3665 relocation, rel->r_addend);
3667 if (r != bfd_reloc_ok)
3669 const char *name;
3671 if (h != NULL)
3672 name = h->root.root.string;
3673 else
3675 name = bfd_elf_string_from_elf_section (input_bfd,
3676 symtab_hdr->sh_link,
3677 sym->st_name);
3678 if (name == NULL)
3679 return FALSE;
3680 if (*name == '\0')
3681 name = bfd_section_name (input_bfd, sec);
3684 if (r == bfd_reloc_overflow)
3686 if (! ((*info->callbacks->reloc_overflow)
3687 (info, (h ? &h->root : NULL), name, howto->name,
3688 (bfd_vma) 0, input_bfd, input_section,
3689 rel->r_offset)))
3690 return FALSE;
3692 else
3694 (*_bfd_error_handler)
3695 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3696 input_bfd, input_section,
3697 (long) rel->r_offset, name, (int) r);
3698 return FALSE;
3703 return TRUE;
3706 /* Finish up dynamic symbol handling. We set the contents of various
3707 dynamic sections here. */
3709 static bfd_boolean
3710 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3711 struct bfd_link_info *info,
3712 struct elf_link_hash_entry *h,
3713 Elf_Internal_Sym *sym)
3715 struct elf64_x86_64_link_hash_table *htab;
3717 htab = elf64_x86_64_hash_table (info);
3719 if (h->plt.offset != (bfd_vma) -1)
3721 bfd_vma plt_index;
3722 bfd_vma got_offset;
3723 Elf_Internal_Rela rela;
3724 bfd_byte *loc;
3725 asection *plt, *gotplt, *relplt;
3727 /* When building a static executable, use .iplt, .igot.plt and
3728 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3729 if (htab->elf.splt != NULL)
3731 plt = htab->elf.splt;
3732 gotplt = htab->elf.sgotplt;
3733 relplt = htab->elf.srelplt;
3735 else
3737 plt = htab->elf.iplt;
3738 gotplt = htab->elf.igotplt;
3739 relplt = htab->elf.irelplt;
3742 /* This symbol has an entry in the procedure linkage table. Set
3743 it up. */
3744 if ((h->dynindx == -1
3745 && !((h->forced_local || info->executable)
3746 && h->def_regular
3747 && h->type == STT_GNU_IFUNC))
3748 || plt == NULL
3749 || gotplt == NULL
3750 || relplt == NULL)
3751 abort ();
3753 /* Get the index in the procedure linkage table which
3754 corresponds to this symbol. This is the index of this symbol
3755 in all the symbols for which we are making plt entries. The
3756 first entry in the procedure linkage table is reserved.
3758 Get the offset into the .got table of the entry that
3759 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3760 bytes. The first three are reserved for the dynamic linker.
3762 For static executables, we don't reserve anything. */
3764 if (plt == htab->elf.splt)
3766 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3767 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3769 else
3771 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3772 got_offset = plt_index * GOT_ENTRY_SIZE;
3775 /* Fill in the entry in the procedure linkage table. */
3776 memcpy (plt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3777 PLT_ENTRY_SIZE);
3779 /* Insert the relocation positions of the plt section. The magic
3780 numbers at the end of the statements are the positions of the
3781 relocations in the plt section. */
3782 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3783 instruction uses 6 bytes, subtract this value. */
3784 bfd_put_32 (output_bfd,
3785 (gotplt->output_section->vma
3786 + gotplt->output_offset
3787 + got_offset
3788 - plt->output_section->vma
3789 - plt->output_offset
3790 - h->plt.offset
3791 - 6),
3792 plt->contents + h->plt.offset + 2);
3794 /* Don't fill PLT entry for static executables. */
3795 if (plt == htab->elf.splt)
3797 /* Put relocation index. */
3798 bfd_put_32 (output_bfd, plt_index,
3799 plt->contents + h->plt.offset + 7);
3800 /* Put offset for jmp .PLT0. */
3801 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3802 plt->contents + h->plt.offset + 12);
3805 /* Fill in the entry in the global offset table, initially this
3806 points to the pushq instruction in the PLT which is at offset 6. */
3807 bfd_put_64 (output_bfd, (plt->output_section->vma
3808 + plt->output_offset
3809 + h->plt.offset + 6),
3810 gotplt->contents + got_offset);
3812 /* Fill in the entry in the .rela.plt section. */
3813 rela.r_offset = (gotplt->output_section->vma
3814 + gotplt->output_offset
3815 + got_offset);
3816 if (h->dynindx == -1
3817 || ((info->executable
3818 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3819 && h->def_regular
3820 && h->type == STT_GNU_IFUNC))
3822 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3823 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
3824 rela.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
3825 rela.r_addend = (h->root.u.def.value
3826 + h->root.u.def.section->output_section->vma
3827 + h->root.u.def.section->output_offset);
3829 else
3831 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3832 rela.r_addend = 0;
3834 loc = relplt->contents + plt_index * sizeof (Elf64_External_Rela);
3835 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3837 if (!h->def_regular)
3839 /* Mark the symbol as undefined, rather than as defined in
3840 the .plt section. Leave the value if there were any
3841 relocations where pointer equality matters (this is a clue
3842 for the dynamic linker, to make function pointer
3843 comparisons work between an application and shared
3844 library), otherwise set it to zero. If a function is only
3845 called from a binary, there is no need to slow down
3846 shared libraries because of that. */
3847 sym->st_shndx = SHN_UNDEF;
3848 if (!h->pointer_equality_needed)
3849 sym->st_value = 0;
3853 if (h->got.offset != (bfd_vma) -1
3854 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3855 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3857 Elf_Internal_Rela rela;
3858 bfd_byte *loc;
3860 /* This symbol has an entry in the global offset table. Set it
3861 up. */
3862 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3863 abort ();
3865 rela.r_offset = (htab->elf.sgot->output_section->vma
3866 + htab->elf.sgot->output_offset
3867 + (h->got.offset &~ (bfd_vma) 1));
3869 /* If this is a static link, or it is a -Bsymbolic link and the
3870 symbol is defined locally or was forced to be local because
3871 of a version file, we just want to emit a RELATIVE reloc.
3872 The entry in the global offset table will already have been
3873 initialized in the relocate_section function. */
3874 if (h->def_regular
3875 && h->type == STT_GNU_IFUNC)
3877 if (info->shared)
3879 /* Generate R_X86_64_GLOB_DAT. */
3880 goto do_glob_dat;
3882 else
3884 if (!h->pointer_equality_needed)
3885 abort ();
3887 /* For non-shared object, we can't use .got.plt, which
3888 contains the real function addres if we need pointer
3889 equality. We load the GOT entry with the PLT entry. */
3890 asection *plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3891 bfd_put_64 (output_bfd, (plt->output_section->vma
3892 + plt->output_offset
3893 + h->plt.offset),
3894 htab->elf.sgot->contents + h->got.offset);
3895 return TRUE;
3898 else if (info->shared
3899 && SYMBOL_REFERENCES_LOCAL (info, h))
3901 if (!h->def_regular)
3902 return FALSE;
3903 BFD_ASSERT((h->got.offset & 1) != 0);
3904 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3905 rela.r_addend = (h->root.u.def.value
3906 + h->root.u.def.section->output_section->vma
3907 + h->root.u.def.section->output_offset);
3909 else
3911 BFD_ASSERT((h->got.offset & 1) == 0);
3912 do_glob_dat:
3913 bfd_put_64 (output_bfd, (bfd_vma) 0,
3914 htab->elf.sgot->contents + h->got.offset);
3915 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3916 rela.r_addend = 0;
3919 loc = htab->elf.srelgot->contents;
3920 loc += htab->elf.srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3921 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3924 if (h->needs_copy)
3926 Elf_Internal_Rela rela;
3927 bfd_byte *loc;
3929 /* This symbol needs a copy reloc. Set it up. */
3931 if (h->dynindx == -1
3932 || (h->root.type != bfd_link_hash_defined
3933 && h->root.type != bfd_link_hash_defweak)
3934 || htab->srelbss == NULL)
3935 abort ();
3937 rela.r_offset = (h->root.u.def.value
3938 + h->root.u.def.section->output_section->vma
3939 + h->root.u.def.section->output_offset);
3940 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3941 rela.r_addend = 0;
3942 loc = htab->srelbss->contents;
3943 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
3944 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3947 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
3948 be NULL for local symbols. */
3949 if (sym != NULL
3950 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
3951 || h == htab->elf.hgot))
3952 sym->st_shndx = SHN_ABS;
3954 return TRUE;
3957 /* Finish up local dynamic symbol handling. We set the contents of
3958 various dynamic sections here. */
3960 static bfd_boolean
3961 elf64_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
3963 struct elf_link_hash_entry *h
3964 = (struct elf_link_hash_entry *) *slot;
3965 struct bfd_link_info *info
3966 = (struct bfd_link_info *) inf;
3968 return elf64_x86_64_finish_dynamic_symbol (info->output_bfd,
3969 info, h, NULL);
3972 /* Used to decide how to sort relocs in an optimal manner for the
3973 dynamic linker, before writing them out. */
3975 static enum elf_reloc_type_class
3976 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
3978 switch ((int) ELF64_R_TYPE (rela->r_info))
3980 case R_X86_64_RELATIVE:
3981 return reloc_class_relative;
3982 case R_X86_64_JUMP_SLOT:
3983 return reloc_class_plt;
3984 case R_X86_64_COPY:
3985 return reloc_class_copy;
3986 default:
3987 return reloc_class_normal;
3991 /* Finish up the dynamic sections. */
3993 static bfd_boolean
3994 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3996 struct elf64_x86_64_link_hash_table *htab;
3997 bfd *dynobj;
3998 asection *sdyn;
4000 htab = elf64_x86_64_hash_table (info);
4001 dynobj = htab->elf.dynobj;
4002 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4004 if (htab->elf.dynamic_sections_created)
4006 Elf64_External_Dyn *dyncon, *dynconend;
4008 if (sdyn == NULL || htab->elf.sgot == NULL)
4009 abort ();
4011 dyncon = (Elf64_External_Dyn *) sdyn->contents;
4012 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
4013 for (; dyncon < dynconend; dyncon++)
4015 Elf_Internal_Dyn dyn;
4016 asection *s;
4018 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4020 switch (dyn.d_tag)
4022 default:
4023 continue;
4025 case DT_PLTGOT:
4026 s = htab->elf.sgotplt;
4027 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4028 break;
4030 case DT_JMPREL:
4031 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
4032 break;
4034 case DT_PLTRELSZ:
4035 s = htab->elf.srelplt->output_section;
4036 dyn.d_un.d_val = s->size;
4037 break;
4039 case DT_RELASZ:
4040 /* The procedure linkage table relocs (DT_JMPREL) should
4041 not be included in the overall relocs (DT_RELA).
4042 Therefore, we override the DT_RELASZ entry here to
4043 make it not include the JMPREL relocs. Since the
4044 linker script arranges for .rela.plt to follow all
4045 other relocation sections, we don't have to worry
4046 about changing the DT_RELA entry. */
4047 if (htab->elf.srelplt != NULL)
4049 s = htab->elf.srelplt->output_section;
4050 dyn.d_un.d_val -= s->size;
4052 break;
4054 case DT_TLSDESC_PLT:
4055 s = htab->elf.splt;
4056 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4057 + htab->tlsdesc_plt;
4058 break;
4060 case DT_TLSDESC_GOT:
4061 s = htab->elf.sgot;
4062 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4063 + htab->tlsdesc_got;
4064 break;
4067 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4070 /* Fill in the special first entry in the procedure linkage table. */
4071 if (htab->elf.splt && htab->elf.splt->size > 0)
4073 /* Fill in the first entry in the procedure linkage table. */
4074 memcpy (htab->elf.splt->contents, elf64_x86_64_plt0_entry,
4075 PLT_ENTRY_SIZE);
4076 /* Add offset for pushq GOT+8(%rip), since the instruction
4077 uses 6 bytes subtract this value. */
4078 bfd_put_32 (output_bfd,
4079 (htab->elf.sgotplt->output_section->vma
4080 + htab->elf.sgotplt->output_offset
4082 - htab->elf.splt->output_section->vma
4083 - htab->elf.splt->output_offset
4084 - 6),
4085 htab->elf.splt->contents + 2);
4086 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4087 the end of the instruction. */
4088 bfd_put_32 (output_bfd,
4089 (htab->elf.sgotplt->output_section->vma
4090 + htab->elf.sgotplt->output_offset
4091 + 16
4092 - htab->elf.splt->output_section->vma
4093 - htab->elf.splt->output_offset
4094 - 12),
4095 htab->elf.splt->contents + 8);
4097 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize =
4098 PLT_ENTRY_SIZE;
4100 if (htab->tlsdesc_plt)
4102 bfd_put_64 (output_bfd, (bfd_vma) 0,
4103 htab->elf.sgot->contents + htab->tlsdesc_got);
4105 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
4106 elf64_x86_64_plt0_entry,
4107 PLT_ENTRY_SIZE);
4109 /* Add offset for pushq GOT+8(%rip), since the
4110 instruction uses 6 bytes subtract this value. */
4111 bfd_put_32 (output_bfd,
4112 (htab->elf.sgotplt->output_section->vma
4113 + htab->elf.sgotplt->output_offset
4115 - htab->elf.splt->output_section->vma
4116 - htab->elf.splt->output_offset
4117 - htab->tlsdesc_plt
4118 - 6),
4119 htab->elf.splt->contents + htab->tlsdesc_plt + 2);
4120 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4121 htab->tlsdesc_got. The 12 is the offset to the end of
4122 the instruction. */
4123 bfd_put_32 (output_bfd,
4124 (htab->elf.sgot->output_section->vma
4125 + htab->elf.sgot->output_offset
4126 + htab->tlsdesc_got
4127 - htab->elf.splt->output_section->vma
4128 - htab->elf.splt->output_offset
4129 - htab->tlsdesc_plt
4130 - 12),
4131 htab->elf.splt->contents + htab->tlsdesc_plt + 8);
4136 if (htab->elf.sgotplt)
4138 /* Fill in the first three entries in the global offset table. */
4139 if (htab->elf.sgotplt->size > 0)
4141 /* Set the first entry in the global offset table to the address of
4142 the dynamic section. */
4143 if (sdyn == NULL)
4144 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4145 else
4146 bfd_put_64 (output_bfd,
4147 sdyn->output_section->vma + sdyn->output_offset,
4148 htab->elf.sgotplt->contents);
4149 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4150 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4151 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4154 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
4155 GOT_ENTRY_SIZE;
4158 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4159 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
4160 = GOT_ENTRY_SIZE;
4162 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4163 htab_traverse (htab->loc_hash_table,
4164 elf64_x86_64_finish_local_dynamic_symbol,
4165 info);
4167 return TRUE;
4170 /* Return address for Ith PLT stub in section PLT, for relocation REL
4171 or (bfd_vma) -1 if it should not be included. */
4173 static bfd_vma
4174 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4175 const arelent *rel ATTRIBUTE_UNUSED)
4177 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4180 /* Handle an x86-64 specific section when reading an object file. This
4181 is called when elfcode.h finds a section with an unknown type. */
4183 static bfd_boolean
4184 elf64_x86_64_section_from_shdr (bfd *abfd,
4185 Elf_Internal_Shdr *hdr,
4186 const char *name,
4187 int shindex)
4189 if (hdr->sh_type != SHT_X86_64_UNWIND)
4190 return FALSE;
4192 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4193 return FALSE;
4195 return TRUE;
4198 /* Hook called by the linker routine which adds symbols from an object
4199 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4200 of .bss. */
4202 static bfd_boolean
4203 elf64_x86_64_add_symbol_hook (bfd *abfd,
4204 struct bfd_link_info *info,
4205 Elf_Internal_Sym *sym,
4206 const char **namep ATTRIBUTE_UNUSED,
4207 flagword *flagsp ATTRIBUTE_UNUSED,
4208 asection **secp,
4209 bfd_vma *valp)
4211 asection *lcomm;
4213 switch (sym->st_shndx)
4215 case SHN_X86_64_LCOMMON:
4216 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4217 if (lcomm == NULL)
4219 lcomm = bfd_make_section_with_flags (abfd,
4220 "LARGE_COMMON",
4221 (SEC_ALLOC
4222 | SEC_IS_COMMON
4223 | SEC_LINKER_CREATED));
4224 if (lcomm == NULL)
4225 return FALSE;
4226 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4228 *secp = lcomm;
4229 *valp = sym->st_size;
4230 break;
4233 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4234 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4236 return TRUE;
4240 /* Given a BFD section, try to locate the corresponding ELF section
4241 index. */
4243 static bfd_boolean
4244 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4245 asection *sec, int *index)
4247 if (sec == &_bfd_elf_large_com_section)
4249 *index = SHN_X86_64_LCOMMON;
4250 return TRUE;
4252 return FALSE;
4255 /* Process a symbol. */
4257 static void
4258 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4259 asymbol *asym)
4261 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4263 switch (elfsym->internal_elf_sym.st_shndx)
4265 case SHN_X86_64_LCOMMON:
4266 asym->section = &_bfd_elf_large_com_section;
4267 asym->value = elfsym->internal_elf_sym.st_size;
4268 /* Common symbol doesn't set BSF_GLOBAL. */
4269 asym->flags &= ~BSF_GLOBAL;
4270 break;
4274 static bfd_boolean
4275 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
4277 return (sym->st_shndx == SHN_COMMON
4278 || sym->st_shndx == SHN_X86_64_LCOMMON);
4281 static unsigned int
4282 elf64_x86_64_common_section_index (asection *sec)
4284 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4285 return SHN_COMMON;
4286 else
4287 return SHN_X86_64_LCOMMON;
4290 static asection *
4291 elf64_x86_64_common_section (asection *sec)
4293 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4294 return bfd_com_section_ptr;
4295 else
4296 return &_bfd_elf_large_com_section;
4299 static bfd_boolean
4300 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4301 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4302 struct elf_link_hash_entry *h,
4303 Elf_Internal_Sym *sym,
4304 asection **psec,
4305 bfd_vma *pvalue ATTRIBUTE_UNUSED,
4306 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4307 bfd_boolean *skip ATTRIBUTE_UNUSED,
4308 bfd_boolean *override ATTRIBUTE_UNUSED,
4309 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4310 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4311 bfd_boolean *newdef ATTRIBUTE_UNUSED,
4312 bfd_boolean *newdyn,
4313 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4314 bfd_boolean *newweak ATTRIBUTE_UNUSED,
4315 bfd *abfd ATTRIBUTE_UNUSED,
4316 asection **sec,
4317 bfd_boolean *olddef ATTRIBUTE_UNUSED,
4318 bfd_boolean *olddyn,
4319 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4320 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4321 bfd *oldbfd,
4322 asection **oldsec)
4324 /* A normal common symbol and a large common symbol result in a
4325 normal common symbol. We turn the large common symbol into a
4326 normal one. */
4327 if (!*olddyn
4328 && h->root.type == bfd_link_hash_common
4329 && !*newdyn
4330 && bfd_is_com_section (*sec)
4331 && *oldsec != *sec)
4333 if (sym->st_shndx == SHN_COMMON
4334 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4336 h->root.u.c.p->section
4337 = bfd_make_section_old_way (oldbfd, "COMMON");
4338 h->root.u.c.p->section->flags = SEC_ALLOC;
4340 else if (sym->st_shndx == SHN_X86_64_LCOMMON
4341 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4342 *psec = *sec = bfd_com_section_ptr;
4345 return TRUE;
4348 static int
4349 elf64_x86_64_additional_program_headers (bfd *abfd,
4350 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4352 asection *s;
4353 int count = 0;
4355 /* Check to see if we need a large readonly segment. */
4356 s = bfd_get_section_by_name (abfd, ".lrodata");
4357 if (s && (s->flags & SEC_LOAD))
4358 count++;
4360 /* Check to see if we need a large data segment. Since .lbss sections
4361 is placed right after the .bss section, there should be no need for
4362 a large data segment just because of .lbss. */
4363 s = bfd_get_section_by_name (abfd, ".ldata");
4364 if (s && (s->flags & SEC_LOAD))
4365 count++;
4367 return count;
4370 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4372 static bfd_boolean
4373 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4375 if (h->plt.offset != (bfd_vma) -1
4376 && !h->def_regular
4377 && !h->pointer_equality_needed)
4378 return FALSE;
4380 return _bfd_elf_hash_symbol (h);
4383 static const struct bfd_elf_special_section
4384 elf64_x86_64_special_sections[]=
4386 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4387 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4388 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4389 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4390 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4391 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4392 { NULL, 0, 0, 0, 0 }
4395 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
4396 #define TARGET_LITTLE_NAME "elf64-x86-64"
4397 #define ELF_ARCH bfd_arch_i386
4398 #define ELF_MACHINE_CODE EM_X86_64
4399 #define ELF_MAXPAGESIZE 0x200000
4400 #define ELF_MINPAGESIZE 0x1000
4401 #define ELF_COMMONPAGESIZE 0x1000
4403 #define elf_backend_can_gc_sections 1
4404 #define elf_backend_can_refcount 1
4405 #define elf_backend_want_got_plt 1
4406 #define elf_backend_plt_readonly 1
4407 #define elf_backend_want_plt_sym 0
4408 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
4409 #define elf_backend_rela_normal 1
4411 #define elf_info_to_howto elf64_x86_64_info_to_howto
4413 #define bfd_elf64_bfd_link_hash_table_create \
4414 elf64_x86_64_link_hash_table_create
4415 #define bfd_elf64_bfd_link_hash_table_free \
4416 elf64_x86_64_link_hash_table_free
4417 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
4418 #define bfd_elf64_bfd_reloc_name_lookup \
4419 elf64_x86_64_reloc_name_lookup
4421 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
4422 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4423 #define elf_backend_check_relocs elf64_x86_64_check_relocs
4424 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
4425 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
4426 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
4427 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
4428 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
4429 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
4430 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
4431 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
4432 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
4433 #define elf_backend_relocate_section elf64_x86_64_relocate_section
4434 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
4435 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
4436 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4437 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
4438 #define elf_backend_object_p elf64_x86_64_elf_object_p
4439 #define bfd_elf64_mkobject elf64_x86_64_mkobject
4441 #define elf_backend_section_from_shdr \
4442 elf64_x86_64_section_from_shdr
4444 #define elf_backend_section_from_bfd_section \
4445 elf64_x86_64_elf_section_from_bfd_section
4446 #define elf_backend_add_symbol_hook \
4447 elf64_x86_64_add_symbol_hook
4448 #define elf_backend_symbol_processing \
4449 elf64_x86_64_symbol_processing
4450 #define elf_backend_common_section_index \
4451 elf64_x86_64_common_section_index
4452 #define elf_backend_common_section \
4453 elf64_x86_64_common_section
4454 #define elf_backend_common_definition \
4455 elf64_x86_64_common_definition
4456 #define elf_backend_merge_symbol \
4457 elf64_x86_64_merge_symbol
4458 #define elf_backend_special_sections \
4459 elf64_x86_64_special_sections
4460 #define elf_backend_additional_program_headers \
4461 elf64_x86_64_additional_program_headers
4462 #define elf_backend_hash_symbol \
4463 elf64_x86_64_hash_symbol
4465 #undef elf_backend_post_process_headers
4466 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4468 #include "elf64-target.h"
4470 /* FreeBSD support. */
4472 #undef TARGET_LITTLE_SYM
4473 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
4474 #undef TARGET_LITTLE_NAME
4475 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
4477 #undef ELF_OSABI
4478 #define ELF_OSABI ELFOSABI_FREEBSD
4480 #undef elf64_bed
4481 #define elf64_bed elf64_x86_64_fbsd_bed
4483 #include "elf64-target.h"