Configury changes: update src repository (binutils, gdb, and rda) to use
[binutils.git] / bfd / elf64-x86-64.c
blob42569b78aa6a8a86e8173e26e778d401a8df0c44
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006
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 2 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, MA 02110-1301, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
28 #include "elf/x86-64.h"
30 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
31 #define MINUS_ONE (~ (bfd_vma) 0)
33 /* The relocation "howto" table. Order of fields:
34 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
35 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
36 static reloc_howto_type x86_64_elf_howto_table[] =
38 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
39 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
40 FALSE),
41 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
42 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
43 FALSE),
44 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
45 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
46 TRUE),
47 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
48 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
49 FALSE),
50 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
51 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
52 TRUE),
53 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
54 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
55 FALSE),
56 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
57 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
58 MINUS_ONE, FALSE),
59 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
60 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
61 MINUS_ONE, FALSE),
62 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
64 MINUS_ONE, FALSE),
65 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
66 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
67 0xffffffff, TRUE),
68 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
69 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
70 FALSE),
71 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
72 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
73 FALSE),
74 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
75 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
76 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
78 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
79 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
80 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
81 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
82 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
84 MINUS_ONE, FALSE),
85 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
86 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
87 MINUS_ONE, FALSE),
88 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
89 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
90 MINUS_ONE, FALSE),
91 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
92 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
93 0xffffffff, TRUE),
94 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
95 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
96 0xffffffff, TRUE),
97 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
98 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
99 0xffffffff, FALSE),
100 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
101 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
102 0xffffffff, TRUE),
103 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
104 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
105 0xffffffff, FALSE),
106 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
107 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
108 TRUE),
109 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
110 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
111 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
112 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
113 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
114 FALSE, 0xffffffff, 0xffffffff, TRUE),
115 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
116 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
117 FALSE),
118 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
119 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
120 MINUS_ONE, TRUE),
121 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
122 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
123 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
124 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
125 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
126 MINUS_ONE, FALSE),
127 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
128 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
129 MINUS_ONE, FALSE),
130 EMPTY_HOWTO (32),
131 EMPTY_HOWTO (33),
132 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
133 complain_overflow_bitfield, bfd_elf_generic_reloc,
134 "R_X86_64_GOTPC32_TLSDESC",
135 FALSE, 0xffffffff, 0xffffffff, TRUE),
136 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
137 complain_overflow_dont, bfd_elf_generic_reloc,
138 "R_X86_64_TLSDESC_CALL",
139 FALSE, 0, 0, FALSE),
140 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
141 complain_overflow_bitfield, bfd_elf_generic_reloc,
142 "R_X86_64_TLSDESC",
143 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
145 /* We have a gap in the reloc numbers here.
146 R_X86_64_standard counts the number up to this point, and
147 R_X86_64_vt_offset is the value to subtract from a reloc type of
148 R_X86_64_GNU_VT* to form an index into this table. */
149 #define R_X86_64_standard (R_X86_64_TLSDESC + 1)
150 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
152 /* GNU extension to record C++ vtable hierarchy. */
153 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
154 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
156 /* GNU extension to record C++ vtable member usage. */
157 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
158 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
159 FALSE)
162 /* Map BFD relocs to the x86_64 elf relocs. */
163 struct elf_reloc_map
165 bfd_reloc_code_real_type bfd_reloc_val;
166 unsigned char elf_reloc_val;
169 static const struct elf_reloc_map x86_64_reloc_map[] =
171 { BFD_RELOC_NONE, R_X86_64_NONE, },
172 { BFD_RELOC_64, R_X86_64_64, },
173 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
174 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
175 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
176 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
177 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
178 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
179 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
180 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
181 { BFD_RELOC_32, R_X86_64_32, },
182 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
183 { BFD_RELOC_16, R_X86_64_16, },
184 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
185 { BFD_RELOC_8, R_X86_64_8, },
186 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
187 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
188 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
189 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
190 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
191 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
192 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
193 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
194 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
195 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
196 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
197 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
198 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
199 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
200 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
201 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
202 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
203 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
204 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
205 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
206 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
207 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
210 static reloc_howto_type *
211 elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
213 unsigned i;
215 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
216 || r_type >= (unsigned int) R_X86_64_max)
218 if (r_type >= (unsigned int) R_X86_64_standard)
220 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
221 abfd, (int) r_type);
222 r_type = R_X86_64_NONE;
224 i = r_type;
226 else
227 i = r_type - (unsigned int) R_X86_64_vt_offset;
228 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
229 return &x86_64_elf_howto_table[i];
232 /* Given a BFD reloc type, return a HOWTO structure. */
233 static reloc_howto_type *
234 elf64_x86_64_reloc_type_lookup (bfd *abfd,
235 bfd_reloc_code_real_type code)
237 unsigned int i;
239 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
240 i++)
242 if (x86_64_reloc_map[i].bfd_reloc_val == code)
243 return elf64_x86_64_rtype_to_howto (abfd,
244 x86_64_reloc_map[i].elf_reloc_val);
246 return 0;
249 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
251 static void
252 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
253 Elf_Internal_Rela *dst)
255 unsigned r_type;
257 r_type = ELF64_R_TYPE (dst->r_info);
258 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
259 BFD_ASSERT (r_type == cache_ptr->howto->type);
262 /* Support for core dump NOTE sections. */
263 static bfd_boolean
264 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
266 int offset;
267 size_t size;
269 switch (note->descsz)
271 default:
272 return FALSE;
274 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
275 /* pr_cursig */
276 elf_tdata (abfd)->core_signal
277 = bfd_get_16 (abfd, note->descdata + 12);
279 /* pr_pid */
280 elf_tdata (abfd)->core_pid
281 = bfd_get_32 (abfd, note->descdata + 32);
283 /* pr_reg */
284 offset = 112;
285 size = 216;
287 break;
290 /* Make a ".reg/999" section. */
291 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
292 size, note->descpos + offset);
295 static bfd_boolean
296 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
298 switch (note->descsz)
300 default:
301 return FALSE;
303 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
304 elf_tdata (abfd)->core_program
305 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
306 elf_tdata (abfd)->core_command
307 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
310 /* Note that for some reason, a spurious space is tacked
311 onto the end of the args in some (at least one anyway)
312 implementations, so strip it off if it exists. */
315 char *command = elf_tdata (abfd)->core_command;
316 int n = strlen (command);
318 if (0 < n && command[n - 1] == ' ')
319 command[n - 1] = '\0';
322 return TRUE;
325 /* Functions for the x86-64 ELF linker. */
327 /* The name of the dynamic interpreter. This is put in the .interp
328 section. */
330 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
332 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
333 copying dynamic variables from a shared lib into an app's dynbss
334 section, and instead use a dynamic relocation to point into the
335 shared lib. */
336 #define ELIMINATE_COPY_RELOCS 1
338 /* The size in bytes of an entry in the global offset table. */
340 #define GOT_ENTRY_SIZE 8
342 /* The size in bytes of an entry in the procedure linkage table. */
344 #define PLT_ENTRY_SIZE 16
346 /* The first entry in a procedure linkage table looks like this. See the
347 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
349 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
351 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
352 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
353 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
356 /* Subsequent entries in a procedure linkage table look like this. */
358 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
360 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
361 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
362 0x68, /* pushq immediate */
363 0, 0, 0, 0, /* replaced with index into relocation table. */
364 0xe9, /* jmp relative */
365 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
368 /* The x86-64 linker needs to keep track of the number of relocs that
369 it decides to copy as dynamic relocs in check_relocs for each symbol.
370 This is so that it can later discard them if they are found to be
371 unnecessary. We store the information in a field extending the
372 regular ELF linker hash table. */
374 struct elf64_x86_64_dyn_relocs
376 /* Next section. */
377 struct elf64_x86_64_dyn_relocs *next;
379 /* The input section of the reloc. */
380 asection *sec;
382 /* Total number of relocs copied for the input section. */
383 bfd_size_type count;
385 /* Number of pc-relative relocs copied for the input section. */
386 bfd_size_type pc_count;
389 /* x86-64 ELF linker hash entry. */
391 struct elf64_x86_64_link_hash_entry
393 struct elf_link_hash_entry elf;
395 /* Track dynamic relocs copied for this symbol. */
396 struct elf64_x86_64_dyn_relocs *dyn_relocs;
398 #define GOT_UNKNOWN 0
399 #define GOT_NORMAL 1
400 #define GOT_TLS_GD 2
401 #define GOT_TLS_IE 3
402 #define GOT_TLS_GDESC 4
403 #define GOT_TLS_GD_BOTH_P(type) \
404 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
405 #define GOT_TLS_GD_P(type) \
406 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
407 #define GOT_TLS_GDESC_P(type) \
408 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
409 #define GOT_TLS_GD_ANY_P(type) \
410 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
411 unsigned char tls_type;
413 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
414 starting at the end of the jump table. */
415 bfd_vma tlsdesc_got;
418 #define elf64_x86_64_hash_entry(ent) \
419 ((struct elf64_x86_64_link_hash_entry *)(ent))
421 struct elf64_x86_64_obj_tdata
423 struct elf_obj_tdata root;
425 /* tls_type for each local got entry. */
426 char *local_got_tls_type;
428 /* GOTPLT entries for TLS descriptors. */
429 bfd_vma *local_tlsdesc_gotent;
432 #define elf64_x86_64_tdata(abfd) \
433 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
435 #define elf64_x86_64_local_got_tls_type(abfd) \
436 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
438 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
439 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
441 /* x86-64 ELF linker hash table. */
443 struct elf64_x86_64_link_hash_table
445 struct elf_link_hash_table elf;
447 /* Short-cuts to get to dynamic linker sections. */
448 asection *sgot;
449 asection *sgotplt;
450 asection *srelgot;
451 asection *splt;
452 asection *srelplt;
453 asection *sdynbss;
454 asection *srelbss;
456 /* The offset into splt of the PLT entry for the TLS descriptor
457 resolver. Special values are 0, if not necessary (or not found
458 to be necessary yet), and -1 if needed but not determined
459 yet. */
460 bfd_vma tlsdesc_plt;
461 /* The offset into sgot of the GOT entry used by the PLT entry
462 above. */
463 bfd_vma tlsdesc_got;
465 union {
466 bfd_signed_vma refcount;
467 bfd_vma offset;
468 } tls_ld_got;
470 /* The amount of space used by the jump slots in the GOT. */
471 bfd_vma sgotplt_jump_table_size;
473 /* Small local sym to section mapping cache. */
474 struct sym_sec_cache sym_sec;
477 /* Get the x86-64 ELF linker hash table from a link_info structure. */
479 #define elf64_x86_64_hash_table(p) \
480 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
482 #define elf64_x86_64_compute_jump_table_size(htab) \
483 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
485 /* Create an entry in an x86-64 ELF linker hash table. */
487 static struct bfd_hash_entry *
488 link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
489 const char *string)
491 /* Allocate the structure if it has not already been allocated by a
492 subclass. */
493 if (entry == NULL)
495 entry = bfd_hash_allocate (table,
496 sizeof (struct elf64_x86_64_link_hash_entry));
497 if (entry == NULL)
498 return entry;
501 /* Call the allocation method of the superclass. */
502 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
503 if (entry != NULL)
505 struct elf64_x86_64_link_hash_entry *eh;
507 eh = (struct elf64_x86_64_link_hash_entry *) entry;
508 eh->dyn_relocs = NULL;
509 eh->tls_type = GOT_UNKNOWN;
510 eh->tlsdesc_got = (bfd_vma) -1;
513 return entry;
516 /* Create an X86-64 ELF linker hash table. */
518 static struct bfd_link_hash_table *
519 elf64_x86_64_link_hash_table_create (bfd *abfd)
521 struct elf64_x86_64_link_hash_table *ret;
522 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
524 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
525 if (ret == NULL)
526 return NULL;
528 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
529 sizeof (struct elf64_x86_64_link_hash_entry)))
531 free (ret);
532 return NULL;
535 ret->sgot = NULL;
536 ret->sgotplt = NULL;
537 ret->srelgot = NULL;
538 ret->splt = NULL;
539 ret->srelplt = NULL;
540 ret->sdynbss = NULL;
541 ret->srelbss = NULL;
542 ret->sym_sec.abfd = NULL;
543 ret->tlsdesc_plt = 0;
544 ret->tlsdesc_got = 0;
545 ret->tls_ld_got.refcount = 0;
546 ret->sgotplt_jump_table_size = 0;
548 return &ret->elf.root;
551 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
552 shortcuts to them in our hash table. */
554 static bfd_boolean
555 create_got_section (bfd *dynobj, struct bfd_link_info *info)
557 struct elf64_x86_64_link_hash_table *htab;
559 if (! _bfd_elf_create_got_section (dynobj, info))
560 return FALSE;
562 htab = elf64_x86_64_hash_table (info);
563 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
564 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
565 if (!htab->sgot || !htab->sgotplt)
566 abort ();
568 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
569 (SEC_ALLOC | SEC_LOAD
570 | SEC_HAS_CONTENTS
571 | SEC_IN_MEMORY
572 | SEC_LINKER_CREATED
573 | SEC_READONLY));
574 if (htab->srelgot == NULL
575 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
576 return FALSE;
577 return TRUE;
580 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
581 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
582 hash table. */
584 static bfd_boolean
585 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
587 struct elf64_x86_64_link_hash_table *htab;
589 htab = elf64_x86_64_hash_table (info);
590 if (!htab->sgot && !create_got_section (dynobj, info))
591 return FALSE;
593 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
594 return FALSE;
596 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
597 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
598 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
599 if (!info->shared)
600 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
602 if (!htab->splt || !htab->srelplt || !htab->sdynbss
603 || (!info->shared && !htab->srelbss))
604 abort ();
606 return TRUE;
609 /* Copy the extra info we tack onto an elf_link_hash_entry. */
611 static void
612 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
613 struct elf_link_hash_entry *dir,
614 struct elf_link_hash_entry *ind)
616 struct elf64_x86_64_link_hash_entry *edir, *eind;
618 edir = (struct elf64_x86_64_link_hash_entry *) dir;
619 eind = (struct elf64_x86_64_link_hash_entry *) ind;
621 if (eind->dyn_relocs != NULL)
623 if (edir->dyn_relocs != NULL)
625 struct elf64_x86_64_dyn_relocs **pp;
626 struct elf64_x86_64_dyn_relocs *p;
628 /* Add reloc counts against the indirect sym to the direct sym
629 list. Merge any entries against the same section. */
630 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
632 struct elf64_x86_64_dyn_relocs *q;
634 for (q = edir->dyn_relocs; q != NULL; q = q->next)
635 if (q->sec == p->sec)
637 q->pc_count += p->pc_count;
638 q->count += p->count;
639 *pp = p->next;
640 break;
642 if (q == NULL)
643 pp = &p->next;
645 *pp = edir->dyn_relocs;
648 edir->dyn_relocs = eind->dyn_relocs;
649 eind->dyn_relocs = NULL;
652 if (ind->root.type == bfd_link_hash_indirect
653 && dir->got.refcount <= 0)
655 edir->tls_type = eind->tls_type;
656 eind->tls_type = GOT_UNKNOWN;
659 if (ELIMINATE_COPY_RELOCS
660 && ind->root.type != bfd_link_hash_indirect
661 && dir->dynamic_adjusted)
663 /* If called to transfer flags for a weakdef during processing
664 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
665 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
666 dir->ref_dynamic |= ind->ref_dynamic;
667 dir->ref_regular |= ind->ref_regular;
668 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
669 dir->needs_plt |= ind->needs_plt;
670 dir->pointer_equality_needed |= ind->pointer_equality_needed;
672 else
673 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
676 static bfd_boolean
677 elf64_x86_64_mkobject (bfd *abfd)
679 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
680 abfd->tdata.any = bfd_zalloc (abfd, amt);
681 if (abfd->tdata.any == NULL)
682 return FALSE;
683 return TRUE;
686 static bfd_boolean
687 elf64_x86_64_elf_object_p (bfd *abfd)
689 /* Set the right machine number for an x86-64 elf64 file. */
690 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
691 return TRUE;
694 static int
695 elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
697 if (info->shared)
698 return r_type;
700 switch (r_type)
702 case R_X86_64_TLSGD:
703 case R_X86_64_GOTPC32_TLSDESC:
704 case R_X86_64_TLSDESC_CALL:
705 case R_X86_64_GOTTPOFF:
706 if (is_local)
707 return R_X86_64_TPOFF32;
708 return R_X86_64_GOTTPOFF;
709 case R_X86_64_TLSLD:
710 return R_X86_64_TPOFF32;
713 return r_type;
716 /* Look through the relocs for a section during the first phase, and
717 calculate needed space in the global offset table, procedure
718 linkage table, and dynamic reloc sections. */
720 static bfd_boolean
721 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
722 const Elf_Internal_Rela *relocs)
724 struct elf64_x86_64_link_hash_table *htab;
725 Elf_Internal_Shdr *symtab_hdr;
726 struct elf_link_hash_entry **sym_hashes;
727 const Elf_Internal_Rela *rel;
728 const Elf_Internal_Rela *rel_end;
729 asection *sreloc;
731 if (info->relocatable)
732 return TRUE;
734 htab = elf64_x86_64_hash_table (info);
735 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
736 sym_hashes = elf_sym_hashes (abfd);
738 sreloc = NULL;
740 rel_end = relocs + sec->reloc_count;
741 for (rel = relocs; rel < rel_end; rel++)
743 unsigned int r_type;
744 unsigned long r_symndx;
745 struct elf_link_hash_entry *h;
747 r_symndx = ELF64_R_SYM (rel->r_info);
748 r_type = ELF64_R_TYPE (rel->r_info);
750 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
752 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
753 abfd, r_symndx);
754 return FALSE;
757 if (r_symndx < symtab_hdr->sh_info)
758 h = NULL;
759 else
761 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
762 while (h->root.type == bfd_link_hash_indirect
763 || h->root.type == bfd_link_hash_warning)
764 h = (struct elf_link_hash_entry *) h->root.u.i.link;
767 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
768 switch (r_type)
770 case R_X86_64_TLSLD:
771 htab->tls_ld_got.refcount += 1;
772 goto create_got;
774 case R_X86_64_TPOFF32:
775 if (info->shared)
777 (*_bfd_error_handler)
778 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
779 abfd,
780 x86_64_elf_howto_table[r_type].name,
781 (h) ? h->root.root.string : "a local symbol");
782 bfd_set_error (bfd_error_bad_value);
783 return FALSE;
785 break;
787 case R_X86_64_GOTTPOFF:
788 if (info->shared)
789 info->flags |= DF_STATIC_TLS;
790 /* Fall through */
792 case R_X86_64_GOT32:
793 case R_X86_64_GOTPCREL:
794 case R_X86_64_TLSGD:
795 case R_X86_64_GOT64:
796 case R_X86_64_GOTPCREL64:
797 case R_X86_64_GOTPLT64:
798 case R_X86_64_GOTPC32_TLSDESC:
799 case R_X86_64_TLSDESC_CALL:
800 /* This symbol requires a global offset table entry. */
802 int tls_type, old_tls_type;
804 switch (r_type)
806 default: tls_type = GOT_NORMAL; break;
807 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
808 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
809 case R_X86_64_GOTPC32_TLSDESC:
810 case R_X86_64_TLSDESC_CALL:
811 tls_type = GOT_TLS_GDESC; break;
814 if (h != NULL)
816 if (r_type == R_X86_64_GOTPLT64)
818 /* This relocation indicates that we also need
819 a PLT entry, as this is a function. We don't need
820 a PLT entry for local symbols. */
821 h->needs_plt = 1;
822 h->plt.refcount += 1;
824 h->got.refcount += 1;
825 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
827 else
829 bfd_signed_vma *local_got_refcounts;
831 /* This is a global offset table entry for a local symbol. */
832 local_got_refcounts = elf_local_got_refcounts (abfd);
833 if (local_got_refcounts == NULL)
835 bfd_size_type size;
837 size = symtab_hdr->sh_info;
838 size *= sizeof (bfd_signed_vma)
839 + sizeof (bfd_vma) + sizeof (char);
840 local_got_refcounts = ((bfd_signed_vma *)
841 bfd_zalloc (abfd, size));
842 if (local_got_refcounts == NULL)
843 return FALSE;
844 elf_local_got_refcounts (abfd) = local_got_refcounts;
845 elf64_x86_64_local_tlsdesc_gotent (abfd)
846 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
847 elf64_x86_64_local_got_tls_type (abfd)
848 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
850 local_got_refcounts[r_symndx] += 1;
851 old_tls_type
852 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
855 /* If a TLS symbol is accessed using IE at least once,
856 there is no point to use dynamic model for it. */
857 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
858 && (! GOT_TLS_GD_ANY_P (old_tls_type)
859 || tls_type != GOT_TLS_IE))
861 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
862 tls_type = old_tls_type;
863 else if (GOT_TLS_GD_ANY_P (old_tls_type)
864 && GOT_TLS_GD_ANY_P (tls_type))
865 tls_type |= old_tls_type;
866 else
868 (*_bfd_error_handler)
869 (_("%B: %s' accessed both as normal and thread local symbol"),
870 abfd, h ? h->root.root.string : "<local>");
871 return FALSE;
875 if (old_tls_type != tls_type)
877 if (h != NULL)
878 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
879 else
880 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
883 /* Fall through */
885 case R_X86_64_GOTOFF64:
886 case R_X86_64_GOTPC32:
887 case R_X86_64_GOTPC64:
888 create_got:
889 if (htab->sgot == NULL)
891 if (htab->elf.dynobj == NULL)
892 htab->elf.dynobj = abfd;
893 if (!create_got_section (htab->elf.dynobj, info))
894 return FALSE;
896 break;
898 case R_X86_64_PLT32:
899 /* This symbol requires a procedure linkage table entry. We
900 actually build the entry in adjust_dynamic_symbol,
901 because this might be a case of linking PIC code which is
902 never referenced by a dynamic object, in which case we
903 don't need to generate a procedure linkage table entry
904 after all. */
906 /* If this is a local symbol, we resolve it directly without
907 creating a procedure linkage table entry. */
908 if (h == NULL)
909 continue;
911 h->needs_plt = 1;
912 h->plt.refcount += 1;
913 break;
915 case R_X86_64_PLTOFF64:
916 /* This tries to form the 'address' of a function relative
917 to GOT. For global symbols we need a PLT entry. */
918 if (h != NULL)
920 h->needs_plt = 1;
921 h->plt.refcount += 1;
923 goto create_got;
925 case R_X86_64_8:
926 case R_X86_64_16:
927 case R_X86_64_32:
928 case R_X86_64_32S:
929 /* Let's help debug shared library creation. These relocs
930 cannot be used in shared libs. Don't error out for
931 sections we don't care about, such as debug sections or
932 non-constant sections. */
933 if (info->shared
934 && (sec->flags & SEC_ALLOC) != 0
935 && (sec->flags & SEC_READONLY) != 0)
937 (*_bfd_error_handler)
938 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
939 abfd,
940 x86_64_elf_howto_table[r_type].name,
941 (h) ? h->root.root.string : "a local symbol");
942 bfd_set_error (bfd_error_bad_value);
943 return FALSE;
945 /* Fall through. */
947 case R_X86_64_PC8:
948 case R_X86_64_PC16:
949 case R_X86_64_PC32:
950 case R_X86_64_PC64:
951 case R_X86_64_64:
952 if (h != NULL && !info->shared)
954 /* If this reloc is in a read-only section, we might
955 need a copy reloc. We can't check reliably at this
956 stage whether the section is read-only, as input
957 sections have not yet been mapped to output sections.
958 Tentatively set the flag for now, and correct in
959 adjust_dynamic_symbol. */
960 h->non_got_ref = 1;
962 /* We may need a .plt entry if the function this reloc
963 refers to is in a shared lib. */
964 h->plt.refcount += 1;
965 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
966 h->pointer_equality_needed = 1;
969 /* If we are creating a shared library, and this is a reloc
970 against a global symbol, or a non PC relative reloc
971 against a local symbol, then we need to copy the reloc
972 into the shared library. However, if we are linking with
973 -Bsymbolic, we do not need to copy a reloc against a
974 global symbol which is defined in an object we are
975 including in the link (i.e., DEF_REGULAR is set). At
976 this point we have not seen all the input files, so it is
977 possible that DEF_REGULAR is not set now but will be set
978 later (it is never cleared). In case of a weak definition,
979 DEF_REGULAR may be cleared later by a strong definition in
980 a shared library. We account for that possibility below by
981 storing information in the relocs_copied field of the hash
982 table entry. A similar situation occurs when creating
983 shared libraries and symbol visibility changes render the
984 symbol local.
986 If on the other hand, we are creating an executable, we
987 may need to keep relocations for symbols satisfied by a
988 dynamic library if we manage to avoid copy relocs for the
989 symbol. */
990 if ((info->shared
991 && (sec->flags & SEC_ALLOC) != 0
992 && (((r_type != R_X86_64_PC8)
993 && (r_type != R_X86_64_PC16)
994 && (r_type != R_X86_64_PC32)
995 && (r_type != R_X86_64_PC64))
996 || (h != NULL
997 && (! info->symbolic
998 || h->root.type == bfd_link_hash_defweak
999 || !h->def_regular))))
1000 || (ELIMINATE_COPY_RELOCS
1001 && !info->shared
1002 && (sec->flags & SEC_ALLOC) != 0
1003 && h != NULL
1004 && (h->root.type == bfd_link_hash_defweak
1005 || !h->def_regular)))
1007 struct elf64_x86_64_dyn_relocs *p;
1008 struct elf64_x86_64_dyn_relocs **head;
1010 /* We must copy these reloc types into the output file.
1011 Create a reloc section in dynobj and make room for
1012 this reloc. */
1013 if (sreloc == NULL)
1015 const char *name;
1016 bfd *dynobj;
1018 name = (bfd_elf_string_from_elf_section
1019 (abfd,
1020 elf_elfheader (abfd)->e_shstrndx,
1021 elf_section_data (sec)->rel_hdr.sh_name));
1022 if (name == NULL)
1023 return FALSE;
1025 if (strncmp (name, ".rela", 5) != 0
1026 || strcmp (bfd_get_section_name (abfd, sec),
1027 name + 5) != 0)
1029 (*_bfd_error_handler)
1030 (_("%B: bad relocation section name `%s\'"),
1031 abfd, name);
1034 if (htab->elf.dynobj == NULL)
1035 htab->elf.dynobj = abfd;
1037 dynobj = htab->elf.dynobj;
1039 sreloc = bfd_get_section_by_name (dynobj, name);
1040 if (sreloc == NULL)
1042 flagword flags;
1044 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1045 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1046 if ((sec->flags & SEC_ALLOC) != 0)
1047 flags |= SEC_ALLOC | SEC_LOAD;
1048 sreloc = bfd_make_section_with_flags (dynobj,
1049 name,
1050 flags);
1051 if (sreloc == NULL
1052 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
1053 return FALSE;
1055 elf_section_data (sec)->sreloc = sreloc;
1058 /* If this is a global symbol, we count the number of
1059 relocations we need for this symbol. */
1060 if (h != NULL)
1062 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1064 else
1066 void **vpp;
1067 /* Track dynamic relocs needed for local syms too.
1068 We really need local syms available to do this
1069 easily. Oh well. */
1071 asection *s;
1072 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1073 sec, r_symndx);
1074 if (s == NULL)
1075 return FALSE;
1077 /* Beware of type punned pointers vs strict aliasing
1078 rules. */
1079 vpp = &(elf_section_data (s)->local_dynrel);
1080 head = (struct elf64_x86_64_dyn_relocs **)vpp;
1083 p = *head;
1084 if (p == NULL || p->sec != sec)
1086 bfd_size_type amt = sizeof *p;
1087 p = ((struct elf64_x86_64_dyn_relocs *)
1088 bfd_alloc (htab->elf.dynobj, amt));
1089 if (p == NULL)
1090 return FALSE;
1091 p->next = *head;
1092 *head = p;
1093 p->sec = sec;
1094 p->count = 0;
1095 p->pc_count = 0;
1098 p->count += 1;
1099 if (r_type == R_X86_64_PC8
1100 || r_type == R_X86_64_PC16
1101 || r_type == R_X86_64_PC32
1102 || r_type == R_X86_64_PC64)
1103 p->pc_count += 1;
1105 break;
1107 /* This relocation describes the C++ object vtable hierarchy.
1108 Reconstruct it for later use during GC. */
1109 case R_X86_64_GNU_VTINHERIT:
1110 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1111 return FALSE;
1112 break;
1114 /* This relocation describes which C++ vtable entries are actually
1115 used. Record for later use during GC. */
1116 case R_X86_64_GNU_VTENTRY:
1117 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1118 return FALSE;
1119 break;
1121 default:
1122 break;
1126 return TRUE;
1129 /* Return the section that should be marked against GC for a given
1130 relocation. */
1132 static asection *
1133 elf64_x86_64_gc_mark_hook (asection *sec,
1134 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1135 Elf_Internal_Rela *rel,
1136 struct elf_link_hash_entry *h,
1137 Elf_Internal_Sym *sym)
1139 if (h != NULL)
1141 switch (ELF64_R_TYPE (rel->r_info))
1143 case R_X86_64_GNU_VTINHERIT:
1144 case R_X86_64_GNU_VTENTRY:
1145 break;
1147 default:
1148 switch (h->root.type)
1150 case bfd_link_hash_defined:
1151 case bfd_link_hash_defweak:
1152 return h->root.u.def.section;
1154 case bfd_link_hash_common:
1155 return h->root.u.c.p->section;
1157 default:
1158 break;
1162 else
1163 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1165 return NULL;
1168 /* Update the got entry reference counts for the section being removed. */
1170 static bfd_boolean
1171 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1172 asection *sec, const Elf_Internal_Rela *relocs)
1174 Elf_Internal_Shdr *symtab_hdr;
1175 struct elf_link_hash_entry **sym_hashes;
1176 bfd_signed_vma *local_got_refcounts;
1177 const Elf_Internal_Rela *rel, *relend;
1179 elf_section_data (sec)->local_dynrel = NULL;
1181 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1182 sym_hashes = elf_sym_hashes (abfd);
1183 local_got_refcounts = elf_local_got_refcounts (abfd);
1185 relend = relocs + sec->reloc_count;
1186 for (rel = relocs; rel < relend; rel++)
1188 unsigned long r_symndx;
1189 unsigned int r_type;
1190 struct elf_link_hash_entry *h = NULL;
1192 r_symndx = ELF64_R_SYM (rel->r_info);
1193 if (r_symndx >= symtab_hdr->sh_info)
1195 struct elf64_x86_64_link_hash_entry *eh;
1196 struct elf64_x86_64_dyn_relocs **pp;
1197 struct elf64_x86_64_dyn_relocs *p;
1199 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1200 while (h->root.type == bfd_link_hash_indirect
1201 || h->root.type == bfd_link_hash_warning)
1202 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1203 eh = (struct elf64_x86_64_link_hash_entry *) h;
1205 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1206 if (p->sec == sec)
1208 /* Everything must go for SEC. */
1209 *pp = p->next;
1210 break;
1214 r_type = ELF64_R_TYPE (rel->r_info);
1215 r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
1216 switch (r_type)
1218 case R_X86_64_TLSLD:
1219 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1220 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1221 break;
1223 case R_X86_64_TLSGD:
1224 case R_X86_64_GOTPC32_TLSDESC:
1225 case R_X86_64_TLSDESC_CALL:
1226 case R_X86_64_GOTTPOFF:
1227 case R_X86_64_GOT32:
1228 case R_X86_64_GOTPCREL:
1229 case R_X86_64_GOT64:
1230 case R_X86_64_GOTPCREL64:
1231 case R_X86_64_GOTPLT64:
1232 if (h != NULL)
1234 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1235 h->plt.refcount -= 1;
1236 if (h->got.refcount > 0)
1237 h->got.refcount -= 1;
1239 else if (local_got_refcounts != NULL)
1241 if (local_got_refcounts[r_symndx] > 0)
1242 local_got_refcounts[r_symndx] -= 1;
1244 break;
1246 case R_X86_64_8:
1247 case R_X86_64_16:
1248 case R_X86_64_32:
1249 case R_X86_64_64:
1250 case R_X86_64_32S:
1251 case R_X86_64_PC8:
1252 case R_X86_64_PC16:
1253 case R_X86_64_PC32:
1254 case R_X86_64_PC64:
1255 if (info->shared)
1256 break;
1257 /* Fall thru */
1259 case R_X86_64_PLT32:
1260 case R_X86_64_PLTOFF64:
1261 if (h != NULL)
1263 if (h->plt.refcount > 0)
1264 h->plt.refcount -= 1;
1266 break;
1268 default:
1269 break;
1273 return TRUE;
1276 /* Adjust a symbol defined by a dynamic object and referenced by a
1277 regular object. The current definition is in some section of the
1278 dynamic object, but we're not including those sections. We have to
1279 change the definition to something the rest of the link can
1280 understand. */
1282 static bfd_boolean
1283 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1284 struct elf_link_hash_entry *h)
1286 struct elf64_x86_64_link_hash_table *htab;
1287 asection *s;
1288 unsigned int power_of_two;
1290 /* If this is a function, put it in the procedure linkage table. We
1291 will fill in the contents of the procedure linkage table later,
1292 when we know the address of the .got section. */
1293 if (h->type == STT_FUNC
1294 || h->needs_plt)
1296 if (h->plt.refcount <= 0
1297 || SYMBOL_CALLS_LOCAL (info, h)
1298 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1299 && h->root.type == bfd_link_hash_undefweak))
1301 /* This case can occur if we saw a PLT32 reloc in an input
1302 file, but the symbol was never referred to by a dynamic
1303 object, or if all references were garbage collected. In
1304 such a case, we don't actually need to build a procedure
1305 linkage table, and we can just do a PC32 reloc instead. */
1306 h->plt.offset = (bfd_vma) -1;
1307 h->needs_plt = 0;
1310 return TRUE;
1312 else
1313 /* It's possible that we incorrectly decided a .plt reloc was
1314 needed for an R_X86_64_PC32 reloc to a non-function sym in
1315 check_relocs. We can't decide accurately between function and
1316 non-function syms in check-relocs; Objects loaded later in
1317 the link may change h->type. So fix it now. */
1318 h->plt.offset = (bfd_vma) -1;
1320 /* If this is a weak symbol, and there is a real definition, the
1321 processor independent code will have arranged for us to see the
1322 real definition first, and we can just use the same value. */
1323 if (h->u.weakdef != NULL)
1325 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1326 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1327 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1328 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1329 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1330 h->non_got_ref = h->u.weakdef->non_got_ref;
1331 return TRUE;
1334 /* This is a reference to a symbol defined by a dynamic object which
1335 is not a function. */
1337 /* If we are creating a shared library, we must presume that the
1338 only references to the symbol are via the global offset table.
1339 For such cases we need not do anything here; the relocations will
1340 be handled correctly by relocate_section. */
1341 if (info->shared)
1342 return TRUE;
1344 /* If there are no references to this symbol that do not use the
1345 GOT, we don't need to generate a copy reloc. */
1346 if (!h->non_got_ref)
1347 return TRUE;
1349 /* If -z nocopyreloc was given, we won't generate them either. */
1350 if (info->nocopyreloc)
1352 h->non_got_ref = 0;
1353 return TRUE;
1356 if (ELIMINATE_COPY_RELOCS)
1358 struct elf64_x86_64_link_hash_entry * eh;
1359 struct elf64_x86_64_dyn_relocs *p;
1361 eh = (struct elf64_x86_64_link_hash_entry *) h;
1362 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1364 s = p->sec->output_section;
1365 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1366 break;
1369 /* If we didn't find any dynamic relocs in read-only sections, then
1370 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1371 if (p == NULL)
1373 h->non_got_ref = 0;
1374 return TRUE;
1378 if (h->size == 0)
1380 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1381 h->root.root.string);
1382 return TRUE;
1385 /* We must allocate the symbol in our .dynbss section, which will
1386 become part of the .bss section of the executable. There will be
1387 an entry for this symbol in the .dynsym section. The dynamic
1388 object will contain position independent code, so all references
1389 from the dynamic object to this symbol will go through the global
1390 offset table. The dynamic linker will use the .dynsym entry to
1391 determine the address it must put in the global offset table, so
1392 both the dynamic object and the regular object will refer to the
1393 same memory location for the variable. */
1395 htab = elf64_x86_64_hash_table (info);
1397 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1398 to copy the initial value out of the dynamic object and into the
1399 runtime process image. */
1400 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1402 htab->srelbss->size += sizeof (Elf64_External_Rela);
1403 h->needs_copy = 1;
1406 /* We need to figure out the alignment required for this symbol. I
1407 have no idea how ELF linkers handle this. 16-bytes is the size
1408 of the largest type that requires hard alignment -- long double. */
1409 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1410 this construct. */
1411 power_of_two = bfd_log2 (h->size);
1412 if (power_of_two > 4)
1413 power_of_two = 4;
1415 /* Apply the required alignment. */
1416 s = htab->sdynbss;
1417 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1418 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1420 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1421 return FALSE;
1424 /* Define the symbol as being at this point in the section. */
1425 h->root.u.def.section = s;
1426 h->root.u.def.value = s->size;
1428 /* Increment the section size to make room for the symbol. */
1429 s->size += h->size;
1431 return TRUE;
1434 /* Allocate space in .plt, .got and associated reloc sections for
1435 dynamic relocs. */
1437 static bfd_boolean
1438 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1440 struct bfd_link_info *info;
1441 struct elf64_x86_64_link_hash_table *htab;
1442 struct elf64_x86_64_link_hash_entry *eh;
1443 struct elf64_x86_64_dyn_relocs *p;
1445 if (h->root.type == bfd_link_hash_indirect)
1446 return TRUE;
1448 if (h->root.type == bfd_link_hash_warning)
1449 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1451 info = (struct bfd_link_info *) inf;
1452 htab = elf64_x86_64_hash_table (info);
1454 if (htab->elf.dynamic_sections_created
1455 && h->plt.refcount > 0)
1457 /* Make sure this symbol is output as a dynamic symbol.
1458 Undefined weak syms won't yet be marked as dynamic. */
1459 if (h->dynindx == -1
1460 && !h->forced_local)
1462 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1463 return FALSE;
1466 if (info->shared
1467 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1469 asection *s = htab->splt;
1471 /* If this is the first .plt entry, make room for the special
1472 first entry. */
1473 if (s->size == 0)
1474 s->size += PLT_ENTRY_SIZE;
1476 h->plt.offset = s->size;
1478 /* If this symbol is not defined in a regular file, and we are
1479 not generating a shared library, then set the symbol to this
1480 location in the .plt. This is required to make function
1481 pointers compare as equal between the normal executable and
1482 the shared library. */
1483 if (! info->shared
1484 && !h->def_regular)
1486 h->root.u.def.section = s;
1487 h->root.u.def.value = h->plt.offset;
1490 /* Make room for this entry. */
1491 s->size += PLT_ENTRY_SIZE;
1493 /* We also need to make an entry in the .got.plt section, which
1494 will be placed in the .got section by the linker script. */
1495 htab->sgotplt->size += GOT_ENTRY_SIZE;
1497 /* We also need to make an entry in the .rela.plt section. */
1498 htab->srelplt->size += sizeof (Elf64_External_Rela);
1499 htab->srelplt->reloc_count++;
1501 else
1503 h->plt.offset = (bfd_vma) -1;
1504 h->needs_plt = 0;
1507 else
1509 h->plt.offset = (bfd_vma) -1;
1510 h->needs_plt = 0;
1513 eh = (struct elf64_x86_64_link_hash_entry *) h;
1514 eh->tlsdesc_got = (bfd_vma) -1;
1516 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1517 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1518 if (h->got.refcount > 0
1519 && !info->shared
1520 && h->dynindx == -1
1521 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1522 h->got.offset = (bfd_vma) -1;
1523 else if (h->got.refcount > 0)
1525 asection *s;
1526 bfd_boolean dyn;
1527 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1529 /* Make sure this symbol is output as a dynamic symbol.
1530 Undefined weak syms won't yet be marked as dynamic. */
1531 if (h->dynindx == -1
1532 && !h->forced_local)
1534 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1535 return FALSE;
1538 if (GOT_TLS_GDESC_P (tls_type))
1540 eh->tlsdesc_got = htab->sgotplt->size
1541 - elf64_x86_64_compute_jump_table_size (htab);
1542 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1543 h->got.offset = (bfd_vma) -2;
1545 if (! GOT_TLS_GDESC_P (tls_type)
1546 || GOT_TLS_GD_P (tls_type))
1548 s = htab->sgot;
1549 h->got.offset = s->size;
1550 s->size += GOT_ENTRY_SIZE;
1551 if (GOT_TLS_GD_P (tls_type))
1552 s->size += GOT_ENTRY_SIZE;
1554 dyn = htab->elf.dynamic_sections_created;
1555 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1556 and two if global.
1557 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1558 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1559 || tls_type == GOT_TLS_IE)
1560 htab->srelgot->size += sizeof (Elf64_External_Rela);
1561 else if (GOT_TLS_GD_P (tls_type))
1562 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
1563 else if (! GOT_TLS_GDESC_P (tls_type)
1564 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1565 || h->root.type != bfd_link_hash_undefweak)
1566 && (info->shared
1567 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1568 htab->srelgot->size += sizeof (Elf64_External_Rela);
1569 if (GOT_TLS_GDESC_P (tls_type))
1571 htab->srelplt->size += sizeof (Elf64_External_Rela);
1572 htab->tlsdesc_plt = (bfd_vma) -1;
1575 else
1576 h->got.offset = (bfd_vma) -1;
1578 if (eh->dyn_relocs == NULL)
1579 return TRUE;
1581 /* In the shared -Bsymbolic case, discard space allocated for
1582 dynamic pc-relative relocs against symbols which turn out to be
1583 defined in regular objects. For the normal shared case, discard
1584 space for pc-relative relocs that have become local due to symbol
1585 visibility changes. */
1587 if (info->shared)
1589 /* Relocs that use pc_count are those that appear on a call
1590 insn, or certain REL relocs that can generated via assembly.
1591 We want calls to protected symbols to resolve directly to the
1592 function rather than going via the plt. If people want
1593 function pointer comparisons to work as expected then they
1594 should avoid writing weird assembly. */
1595 if (SYMBOL_CALLS_LOCAL (info, h))
1597 struct elf64_x86_64_dyn_relocs **pp;
1599 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1601 p->count -= p->pc_count;
1602 p->pc_count = 0;
1603 if (p->count == 0)
1604 *pp = p->next;
1605 else
1606 pp = &p->next;
1610 /* Also discard relocs on undefined weak syms with non-default
1611 visibility. */
1612 if (eh->dyn_relocs != NULL
1613 && h->root.type == bfd_link_hash_undefweak)
1615 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1616 eh->dyn_relocs = NULL;
1618 /* Make sure undefined weak symbols are output as a dynamic
1619 symbol in PIEs. */
1620 else if (h->dynindx == -1
1621 && !h->forced_local)
1623 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1624 return FALSE;
1628 else if (ELIMINATE_COPY_RELOCS)
1630 /* For the non-shared case, discard space for relocs against
1631 symbols which turn out to need copy relocs or are not
1632 dynamic. */
1634 if (!h->non_got_ref
1635 && ((h->def_dynamic
1636 && !h->def_regular)
1637 || (htab->elf.dynamic_sections_created
1638 && (h->root.type == bfd_link_hash_undefweak
1639 || h->root.type == bfd_link_hash_undefined))))
1641 /* Make sure this symbol is output as a dynamic symbol.
1642 Undefined weak syms won't yet be marked as dynamic. */
1643 if (h->dynindx == -1
1644 && !h->forced_local)
1646 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1647 return FALSE;
1650 /* If that succeeded, we know we'll be keeping all the
1651 relocs. */
1652 if (h->dynindx != -1)
1653 goto keep;
1656 eh->dyn_relocs = NULL;
1658 keep: ;
1661 /* Finally, allocate space. */
1662 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1664 asection *sreloc = elf_section_data (p->sec)->sreloc;
1665 sreloc->size += p->count * sizeof (Elf64_External_Rela);
1668 return TRUE;
1671 /* Find any dynamic relocs that apply to read-only sections. */
1673 static bfd_boolean
1674 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1676 struct elf64_x86_64_link_hash_entry *eh;
1677 struct elf64_x86_64_dyn_relocs *p;
1679 if (h->root.type == bfd_link_hash_warning)
1680 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1682 eh = (struct elf64_x86_64_link_hash_entry *) h;
1683 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1685 asection *s = p->sec->output_section;
1687 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1689 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1691 info->flags |= DF_TEXTREL;
1693 /* Not an error, just cut short the traversal. */
1694 return FALSE;
1697 return TRUE;
1700 /* Set the sizes of the dynamic sections. */
1702 static bfd_boolean
1703 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1704 struct bfd_link_info *info)
1706 struct elf64_x86_64_link_hash_table *htab;
1707 bfd *dynobj;
1708 asection *s;
1709 bfd_boolean relocs;
1710 bfd *ibfd;
1712 htab = elf64_x86_64_hash_table (info);
1713 dynobj = htab->elf.dynobj;
1714 if (dynobj == NULL)
1715 abort ();
1717 if (htab->elf.dynamic_sections_created)
1719 /* Set the contents of the .interp section to the interpreter. */
1720 if (info->executable)
1722 s = bfd_get_section_by_name (dynobj, ".interp");
1723 if (s == NULL)
1724 abort ();
1725 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1726 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1730 /* Set up .got offsets for local syms, and space for local dynamic
1731 relocs. */
1732 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1734 bfd_signed_vma *local_got;
1735 bfd_signed_vma *end_local_got;
1736 char *local_tls_type;
1737 bfd_vma *local_tlsdesc_gotent;
1738 bfd_size_type locsymcount;
1739 Elf_Internal_Shdr *symtab_hdr;
1740 asection *srel;
1742 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1743 continue;
1745 for (s = ibfd->sections; s != NULL; s = s->next)
1747 struct elf64_x86_64_dyn_relocs *p;
1749 for (p = (struct elf64_x86_64_dyn_relocs *)
1750 (elf_section_data (s)->local_dynrel);
1751 p != NULL;
1752 p = p->next)
1754 if (!bfd_is_abs_section (p->sec)
1755 && bfd_is_abs_section (p->sec->output_section))
1757 /* Input section has been discarded, either because
1758 it is a copy of a linkonce section or due to
1759 linker script /DISCARD/, so we'll be discarding
1760 the relocs too. */
1762 else if (p->count != 0)
1764 srel = elf_section_data (p->sec)->sreloc;
1765 srel->size += p->count * sizeof (Elf64_External_Rela);
1766 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1767 info->flags |= DF_TEXTREL;
1773 local_got = elf_local_got_refcounts (ibfd);
1774 if (!local_got)
1775 continue;
1777 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1778 locsymcount = symtab_hdr->sh_info;
1779 end_local_got = local_got + locsymcount;
1780 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
1781 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
1782 s = htab->sgot;
1783 srel = htab->srelgot;
1784 for (; local_got < end_local_got;
1785 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
1787 *local_tlsdesc_gotent = (bfd_vma) -1;
1788 if (*local_got > 0)
1790 if (GOT_TLS_GDESC_P (*local_tls_type))
1792 *local_tlsdesc_gotent = htab->sgotplt->size
1793 - elf64_x86_64_compute_jump_table_size (htab);
1794 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1795 *local_got = (bfd_vma) -2;
1797 if (! GOT_TLS_GDESC_P (*local_tls_type)
1798 || GOT_TLS_GD_P (*local_tls_type))
1800 *local_got = s->size;
1801 s->size += GOT_ENTRY_SIZE;
1802 if (GOT_TLS_GD_P (*local_tls_type))
1803 s->size += GOT_ENTRY_SIZE;
1805 if (info->shared
1806 || GOT_TLS_GD_ANY_P (*local_tls_type)
1807 || *local_tls_type == GOT_TLS_IE)
1809 if (GOT_TLS_GDESC_P (*local_tls_type))
1811 htab->srelplt->size += sizeof (Elf64_External_Rela);
1812 htab->tlsdesc_plt = (bfd_vma) -1;
1814 if (! GOT_TLS_GDESC_P (*local_tls_type)
1815 || GOT_TLS_GD_P (*local_tls_type))
1816 srel->size += sizeof (Elf64_External_Rela);
1819 else
1820 *local_got = (bfd_vma) -1;
1824 if (htab->tls_ld_got.refcount > 0)
1826 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1827 relocs. */
1828 htab->tls_ld_got.offset = htab->sgot->size;
1829 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
1830 htab->srelgot->size += sizeof (Elf64_External_Rela);
1832 else
1833 htab->tls_ld_got.offset = -1;
1835 /* Allocate global sym .plt and .got entries, and space for global
1836 sym dynamic relocs. */
1837 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1839 /* For every jump slot reserved in the sgotplt, reloc_count is
1840 incremented. However, when we reserve space for TLS descriptors,
1841 it's not incremented, so in order to compute the space reserved
1842 for them, it suffices to multiply the reloc count by the jump
1843 slot size. */
1844 if (htab->srelplt)
1845 htab->sgotplt_jump_table_size
1846 = elf64_x86_64_compute_jump_table_size (htab);
1848 if (htab->tlsdesc_plt)
1850 /* If we're not using lazy TLS relocations, don't generate the
1851 PLT and GOT entries they require. */
1852 if ((info->flags & DF_BIND_NOW))
1853 htab->tlsdesc_plt = 0;
1854 else
1856 htab->tlsdesc_got = htab->sgot->size;
1857 htab->sgot->size += GOT_ENTRY_SIZE;
1858 /* Reserve room for the initial entry.
1859 FIXME: we could probably do away with it in this case. */
1860 if (htab->splt->size == 0)
1861 htab->splt->size += PLT_ENTRY_SIZE;
1862 htab->tlsdesc_plt = htab->splt->size;
1863 htab->splt->size += PLT_ENTRY_SIZE;
1867 /* We now have determined the sizes of the various dynamic sections.
1868 Allocate memory for them. */
1869 relocs = FALSE;
1870 for (s = dynobj->sections; s != NULL; s = s->next)
1872 if ((s->flags & SEC_LINKER_CREATED) == 0)
1873 continue;
1875 if (s == htab->splt
1876 || s == htab->sgot
1877 || s == htab->sgotplt
1878 || s == htab->sdynbss)
1880 /* Strip this section if we don't need it; see the
1881 comment below. */
1883 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1885 if (s->size != 0 && s != htab->srelplt)
1886 relocs = TRUE;
1888 /* We use the reloc_count field as a counter if we need
1889 to copy relocs into the output file. */
1890 if (s != htab->srelplt)
1891 s->reloc_count = 0;
1893 else
1895 /* It's not one of our sections, so don't allocate space. */
1896 continue;
1899 if (s->size == 0)
1901 /* If we don't need this section, strip it from the
1902 output file. This is mostly to handle .rela.bss and
1903 .rela.plt. We must create both sections in
1904 create_dynamic_sections, because they must be created
1905 before the linker maps input sections to output
1906 sections. The linker does that before
1907 adjust_dynamic_symbol is called, and it is that
1908 function which decides whether anything needs to go
1909 into these sections. */
1911 s->flags |= SEC_EXCLUDE;
1912 continue;
1915 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1916 continue;
1918 /* Allocate memory for the section contents. We use bfd_zalloc
1919 here in case unused entries are not reclaimed before the
1920 section's contents are written out. This should not happen,
1921 but this way if it does, we get a R_X86_64_NONE reloc instead
1922 of garbage. */
1923 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1924 if (s->contents == NULL)
1925 return FALSE;
1928 if (htab->elf.dynamic_sections_created)
1930 /* Add some entries to the .dynamic section. We fill in the
1931 values later, in elf64_x86_64_finish_dynamic_sections, but we
1932 must add the entries now so that we get the correct size for
1933 the .dynamic section. The DT_DEBUG entry is filled in by the
1934 dynamic linker and used by the debugger. */
1935 #define add_dynamic_entry(TAG, VAL) \
1936 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1938 if (info->executable)
1940 if (!add_dynamic_entry (DT_DEBUG, 0))
1941 return FALSE;
1944 if (htab->splt->size != 0)
1946 if (!add_dynamic_entry (DT_PLTGOT, 0)
1947 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1948 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1949 || !add_dynamic_entry (DT_JMPREL, 0))
1950 return FALSE;
1952 if (htab->tlsdesc_plt
1953 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
1954 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
1955 return FALSE;
1958 if (relocs)
1960 if (!add_dynamic_entry (DT_RELA, 0)
1961 || !add_dynamic_entry (DT_RELASZ, 0)
1962 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1963 return FALSE;
1965 /* If any dynamic relocs apply to a read-only section,
1966 then we need a DT_TEXTREL entry. */
1967 if ((info->flags & DF_TEXTREL) == 0)
1968 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1969 (PTR) info);
1971 if ((info->flags & DF_TEXTREL) != 0)
1973 if (!add_dynamic_entry (DT_TEXTREL, 0))
1974 return FALSE;
1978 #undef add_dynamic_entry
1980 return TRUE;
1983 static bfd_boolean
1984 elf64_x86_64_always_size_sections (bfd *output_bfd,
1985 struct bfd_link_info *info)
1987 asection *tls_sec = elf_hash_table (info)->tls_sec;
1989 if (tls_sec)
1991 struct elf_link_hash_entry *tlsbase;
1993 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
1994 "_TLS_MODULE_BASE_",
1995 FALSE, FALSE, FALSE);
1997 if (tlsbase && tlsbase->type == STT_TLS)
1999 struct bfd_link_hash_entry *bh = NULL;
2000 const struct elf_backend_data *bed
2001 = get_elf_backend_data (output_bfd);
2003 if (!(_bfd_generic_link_add_one_symbol
2004 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2005 tls_sec, 0, NULL, FALSE,
2006 bed->collect, &bh)))
2007 return FALSE;
2008 tlsbase = (struct elf_link_hash_entry *)bh;
2009 tlsbase->def_regular = 1;
2010 tlsbase->other = STV_HIDDEN;
2011 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2015 return TRUE;
2018 /* Return the base VMA address which should be subtracted from real addresses
2019 when resolving @dtpoff relocation.
2020 This is PT_TLS segment p_vaddr. */
2022 static bfd_vma
2023 dtpoff_base (struct bfd_link_info *info)
2025 /* If tls_sec is NULL, we should have signalled an error already. */
2026 if (elf_hash_table (info)->tls_sec == NULL)
2027 return 0;
2028 return elf_hash_table (info)->tls_sec->vma;
2031 /* Return the relocation value for @tpoff relocation
2032 if STT_TLS virtual address is ADDRESS. */
2034 static bfd_vma
2035 tpoff (struct bfd_link_info *info, bfd_vma address)
2037 struct elf_link_hash_table *htab = elf_hash_table (info);
2039 /* If tls_segment is NULL, we should have signalled an error already. */
2040 if (htab->tls_sec == NULL)
2041 return 0;
2042 return address - htab->tls_size - htab->tls_sec->vma;
2045 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2046 branch? */
2048 static bfd_boolean
2049 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2051 /* Opcode Instruction
2052 0xe8 call
2053 0xe9 jump
2054 0x0f 0x8x conditional jump */
2055 return ((offset > 0
2056 && (contents [offset - 1] == 0xe8
2057 || contents [offset - 1] == 0xe9))
2058 || (offset > 1
2059 && contents [offset - 2] == 0x0f
2060 && (contents [offset - 1] & 0xf0) == 0x80));
2063 /* Relocate an x86_64 ELF section. */
2065 static bfd_boolean
2066 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2067 bfd *input_bfd, asection *input_section,
2068 bfd_byte *contents, Elf_Internal_Rela *relocs,
2069 Elf_Internal_Sym *local_syms,
2070 asection **local_sections)
2072 struct elf64_x86_64_link_hash_table *htab;
2073 Elf_Internal_Shdr *symtab_hdr;
2074 struct elf_link_hash_entry **sym_hashes;
2075 bfd_vma *local_got_offsets;
2076 bfd_vma *local_tlsdesc_gotents;
2077 Elf_Internal_Rela *rel;
2078 Elf_Internal_Rela *relend;
2080 if (info->relocatable)
2081 return TRUE;
2083 htab = elf64_x86_64_hash_table (info);
2084 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2085 sym_hashes = elf_sym_hashes (input_bfd);
2086 local_got_offsets = elf_local_got_offsets (input_bfd);
2087 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2089 rel = relocs;
2090 relend = relocs + input_section->reloc_count;
2091 for (; rel < relend; rel++)
2093 unsigned int r_type;
2094 reloc_howto_type *howto;
2095 unsigned long r_symndx;
2096 struct elf_link_hash_entry *h;
2097 Elf_Internal_Sym *sym;
2098 asection *sec;
2099 bfd_vma off, offplt;
2100 bfd_vma relocation;
2101 bfd_boolean unresolved_reloc;
2102 bfd_reloc_status_type r;
2103 int tls_type;
2105 r_type = ELF64_R_TYPE (rel->r_info);
2106 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2107 || r_type == (int) R_X86_64_GNU_VTENTRY)
2108 continue;
2110 if (r_type >= R_X86_64_max)
2112 bfd_set_error (bfd_error_bad_value);
2113 return FALSE;
2116 howto = x86_64_elf_howto_table + r_type;
2117 r_symndx = ELF64_R_SYM (rel->r_info);
2118 h = NULL;
2119 sym = NULL;
2120 sec = NULL;
2121 unresolved_reloc = FALSE;
2122 if (r_symndx < symtab_hdr->sh_info)
2124 sym = local_syms + r_symndx;
2125 sec = local_sections[r_symndx];
2127 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2129 else
2131 bfd_boolean warned;
2133 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2134 r_symndx, symtab_hdr, sym_hashes,
2135 h, sec, relocation,
2136 unresolved_reloc, warned);
2138 /* When generating a shared object, the relocations handled here are
2139 copied into the output file to be resolved at run time. */
2140 switch (r_type)
2142 asection *base_got;
2143 case R_X86_64_GOT32:
2144 case R_X86_64_GOT64:
2145 /* Relocation is to the entry for this symbol in the global
2146 offset table. */
2147 case R_X86_64_GOTPCREL:
2148 case R_X86_64_GOTPCREL64:
2149 /* Use global offset table entry as symbol value. */
2150 case R_X86_64_GOTPLT64:
2151 /* This is the same as GOT64 for relocation purposes, but
2152 indicates the existence of a PLT entry. The difficulty is,
2153 that we must calculate the GOT slot offset from the PLT
2154 offset, if this symbol got a PLT entry (it was global).
2155 Additionally if it's computed from the PLT entry, then that
2156 GOT offset is relative to .got.plt, not to .got. */
2157 base_got = htab->sgot;
2159 if (htab->sgot == NULL)
2160 abort ();
2162 if (h != NULL)
2164 bfd_boolean dyn;
2166 off = h->got.offset;
2167 if (h->needs_plt
2168 && h->plt.offset != (bfd_vma)-1
2169 && off == (bfd_vma)-1)
2171 /* We can't use h->got.offset here to save
2172 state, or even just remember the offset, as
2173 finish_dynamic_symbol would use that as offset into
2174 .got. */
2175 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2176 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2177 base_got = htab->sgotplt;
2180 dyn = htab->elf.dynamic_sections_created;
2182 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2183 || (info->shared
2184 && SYMBOL_REFERENCES_LOCAL (info, h))
2185 || (ELF_ST_VISIBILITY (h->other)
2186 && h->root.type == bfd_link_hash_undefweak))
2188 /* This is actually a static link, or it is a -Bsymbolic
2189 link and the symbol is defined locally, or the symbol
2190 was forced to be local because of a version file. We
2191 must initialize this entry in the global offset table.
2192 Since the offset must always be a multiple of 8, we
2193 use the least significant bit to record whether we
2194 have initialized it already.
2196 When doing a dynamic link, we create a .rela.got
2197 relocation entry to initialize the value. This is
2198 done in the finish_dynamic_symbol routine. */
2199 if ((off & 1) != 0)
2200 off &= ~1;
2201 else
2203 bfd_put_64 (output_bfd, relocation,
2204 base_got->contents + off);
2205 /* Note that this is harmless for the GOTPLT64 case,
2206 as -1 | 1 still is -1. */
2207 h->got.offset |= 1;
2210 else
2211 unresolved_reloc = FALSE;
2213 else
2215 if (local_got_offsets == NULL)
2216 abort ();
2218 off = local_got_offsets[r_symndx];
2220 /* The offset must always be a multiple of 8. We use
2221 the least significant bit to record whether we have
2222 already generated the necessary reloc. */
2223 if ((off & 1) != 0)
2224 off &= ~1;
2225 else
2227 bfd_put_64 (output_bfd, relocation,
2228 base_got->contents + off);
2230 if (info->shared)
2232 asection *s;
2233 Elf_Internal_Rela outrel;
2234 bfd_byte *loc;
2236 /* We need to generate a R_X86_64_RELATIVE reloc
2237 for the dynamic linker. */
2238 s = htab->srelgot;
2239 if (s == NULL)
2240 abort ();
2242 outrel.r_offset = (base_got->output_section->vma
2243 + base_got->output_offset
2244 + off);
2245 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2246 outrel.r_addend = relocation;
2247 loc = s->contents;
2248 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2249 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2252 local_got_offsets[r_symndx] |= 1;
2256 if (off >= (bfd_vma) -2)
2257 abort ();
2259 relocation = base_got->output_section->vma
2260 + base_got->output_offset + off;
2261 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
2262 relocation -= htab->sgotplt->output_section->vma
2263 - htab->sgotplt->output_offset;
2265 break;
2267 case R_X86_64_GOTOFF64:
2268 /* Relocation is relative to the start of the global offset
2269 table. */
2271 /* Check to make sure it isn't a protected function symbol
2272 for shared library since it may not be local when used
2273 as function address. */
2274 if (info->shared
2275 && h
2276 && h->def_regular
2277 && h->type == STT_FUNC
2278 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2280 (*_bfd_error_handler)
2281 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2282 input_bfd, h->root.root.string);
2283 bfd_set_error (bfd_error_bad_value);
2284 return FALSE;
2287 /* Note that sgot is not involved in this
2288 calculation. We always want the start of .got.plt. If we
2289 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2290 permitted by the ABI, we might have to change this
2291 calculation. */
2292 relocation -= htab->sgotplt->output_section->vma
2293 + htab->sgotplt->output_offset;
2294 break;
2296 case R_X86_64_GOTPC32:
2297 case R_X86_64_GOTPC64:
2298 /* Use global offset table as symbol value. */
2299 relocation = htab->sgotplt->output_section->vma
2300 + htab->sgotplt->output_offset;
2301 unresolved_reloc = FALSE;
2302 break;
2304 case R_X86_64_PLTOFF64:
2305 /* Relocation is PLT entry relative to GOT. For local
2306 symbols it's the symbol itself relative to GOT. */
2307 if (h != NULL
2308 /* See PLT32 handling. */
2309 && h->plt.offset != (bfd_vma) -1
2310 && htab->splt != NULL)
2312 relocation = (htab->splt->output_section->vma
2313 + htab->splt->output_offset
2314 + h->plt.offset);
2315 unresolved_reloc = FALSE;
2318 relocation -= htab->sgotplt->output_section->vma
2319 + htab->sgotplt->output_offset;
2320 break;
2322 case R_X86_64_PLT32:
2323 /* Relocation is to the entry for this symbol in the
2324 procedure linkage table. */
2326 /* Resolve a PLT32 reloc against a local symbol directly,
2327 without using the procedure linkage table. */
2328 if (h == NULL)
2329 break;
2331 if (h->plt.offset == (bfd_vma) -1
2332 || htab->splt == NULL)
2334 /* We didn't make a PLT entry for this symbol. This
2335 happens when statically linking PIC code, or when
2336 using -Bsymbolic. */
2337 break;
2340 relocation = (htab->splt->output_section->vma
2341 + htab->splt->output_offset
2342 + h->plt.offset);
2343 unresolved_reloc = FALSE;
2344 break;
2346 case R_X86_64_PC8:
2347 case R_X86_64_PC16:
2348 case R_X86_64_PC32:
2349 if (info->shared
2350 && !SYMBOL_REFERENCES_LOCAL (info, h)
2351 && (input_section->flags & SEC_ALLOC) != 0
2352 && (input_section->flags & SEC_READONLY) != 0
2353 && (!h->def_regular
2354 || r_type != R_X86_64_PC32
2355 || h->type != STT_FUNC
2356 || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
2357 || !is_32bit_relative_branch (contents,
2358 rel->r_offset)))
2360 if (h->def_regular
2361 && r_type == R_X86_64_PC32
2362 && h->type == STT_FUNC
2363 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2364 (*_bfd_error_handler)
2365 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
2366 input_bfd, h->root.root.string);
2367 else
2368 (*_bfd_error_handler)
2369 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2370 input_bfd, x86_64_elf_howto_table[r_type].name,
2371 h->root.root.string);
2372 bfd_set_error (bfd_error_bad_value);
2373 return FALSE;
2375 /* Fall through. */
2377 case R_X86_64_8:
2378 case R_X86_64_16:
2379 case R_X86_64_32:
2380 case R_X86_64_PC64:
2381 case R_X86_64_64:
2382 /* FIXME: The ABI says the linker should make sure the value is
2383 the same when it's zeroextended to 64 bit. */
2385 /* r_symndx will be zero only for relocs against symbols
2386 from removed linkonce sections, or sections discarded by
2387 a linker script. */
2388 if (r_symndx == 0
2389 || (input_section->flags & SEC_ALLOC) == 0)
2390 break;
2392 if ((info->shared
2393 && (h == NULL
2394 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2395 || h->root.type != bfd_link_hash_undefweak)
2396 && ((r_type != R_X86_64_PC8
2397 && r_type != R_X86_64_PC16
2398 && r_type != R_X86_64_PC32
2399 && r_type != R_X86_64_PC64)
2400 || !SYMBOL_CALLS_LOCAL (info, h)))
2401 || (ELIMINATE_COPY_RELOCS
2402 && !info->shared
2403 && h != NULL
2404 && h->dynindx != -1
2405 && !h->non_got_ref
2406 && ((h->def_dynamic
2407 && !h->def_regular)
2408 || h->root.type == bfd_link_hash_undefweak
2409 || h->root.type == bfd_link_hash_undefined)))
2411 Elf_Internal_Rela outrel;
2412 bfd_byte *loc;
2413 bfd_boolean skip, relocate;
2414 asection *sreloc;
2416 /* When generating a shared object, these relocations
2417 are copied into the output file to be resolved at run
2418 time. */
2419 skip = FALSE;
2420 relocate = FALSE;
2422 outrel.r_offset =
2423 _bfd_elf_section_offset (output_bfd, info, input_section,
2424 rel->r_offset);
2425 if (outrel.r_offset == (bfd_vma) -1)
2426 skip = TRUE;
2427 else if (outrel.r_offset == (bfd_vma) -2)
2428 skip = TRUE, relocate = TRUE;
2430 outrel.r_offset += (input_section->output_section->vma
2431 + input_section->output_offset);
2433 if (skip)
2434 memset (&outrel, 0, sizeof outrel);
2436 /* h->dynindx may be -1 if this symbol was marked to
2437 become local. */
2438 else if (h != NULL
2439 && h->dynindx != -1
2440 && (r_type == R_X86_64_PC8
2441 || r_type == R_X86_64_PC16
2442 || r_type == R_X86_64_PC32
2443 || r_type == R_X86_64_PC64
2444 || !info->shared
2445 || !info->symbolic
2446 || !h->def_regular))
2448 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2449 outrel.r_addend = rel->r_addend;
2451 else
2453 /* This symbol is local, or marked to become local. */
2454 if (r_type == R_X86_64_64)
2456 relocate = TRUE;
2457 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2458 outrel.r_addend = relocation + rel->r_addend;
2460 else
2462 long sindx;
2464 if (bfd_is_abs_section (sec))
2465 sindx = 0;
2466 else if (sec == NULL || sec->owner == NULL)
2468 bfd_set_error (bfd_error_bad_value);
2469 return FALSE;
2471 else
2473 asection *osec;
2475 osec = sec->output_section;
2476 sindx = elf_section_data (osec)->dynindx;
2477 BFD_ASSERT (sindx > 0);
2480 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2481 outrel.r_addend = relocation + rel->r_addend;
2485 sreloc = elf_section_data (input_section)->sreloc;
2486 if (sreloc == NULL)
2487 abort ();
2489 loc = sreloc->contents;
2490 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2491 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2493 /* If this reloc is against an external symbol, we do
2494 not want to fiddle with the addend. Otherwise, we
2495 need to include the symbol value so that it becomes
2496 an addend for the dynamic reloc. */
2497 if (! relocate)
2498 continue;
2501 break;
2503 case R_X86_64_TLSGD:
2504 case R_X86_64_GOTPC32_TLSDESC:
2505 case R_X86_64_TLSDESC_CALL:
2506 case R_X86_64_GOTTPOFF:
2507 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
2508 tls_type = GOT_UNKNOWN;
2509 if (h == NULL && local_got_offsets)
2510 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2511 else if (h != NULL)
2513 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2514 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2515 r_type = R_X86_64_TPOFF32;
2517 if (r_type == R_X86_64_TLSGD
2518 || r_type == R_X86_64_GOTPC32_TLSDESC
2519 || r_type == R_X86_64_TLSDESC_CALL)
2521 if (tls_type == GOT_TLS_IE)
2522 r_type = R_X86_64_GOTTPOFF;
2525 if (r_type == R_X86_64_TPOFF32)
2527 BFD_ASSERT (! unresolved_reloc);
2528 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2530 unsigned int i;
2531 static unsigned char tlsgd[8]
2532 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2534 /* GD->LE transition.
2535 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2536 .word 0x6666; rex64; call __tls_get_addr@plt
2537 Change it into:
2538 movq %fs:0, %rax
2539 leaq foo@tpoff(%rax), %rax */
2540 BFD_ASSERT (rel->r_offset >= 4);
2541 for (i = 0; i < 4; i++)
2542 BFD_ASSERT (bfd_get_8 (input_bfd,
2543 contents + rel->r_offset - 4 + i)
2544 == tlsgd[i]);
2545 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2546 for (i = 0; i < 4; i++)
2547 BFD_ASSERT (bfd_get_8 (input_bfd,
2548 contents + rel->r_offset + 4 + i)
2549 == tlsgd[i+4]);
2550 BFD_ASSERT (rel + 1 < relend);
2551 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2552 memcpy (contents + rel->r_offset - 4,
2553 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2554 16);
2555 bfd_put_32 (output_bfd, tpoff (info, relocation),
2556 contents + rel->r_offset + 8);
2557 /* Skip R_X86_64_PLT32. */
2558 rel++;
2559 continue;
2561 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2563 /* GDesc -> LE transition.
2564 It's originally something like:
2565 leaq x@tlsdesc(%rip), %rax
2567 Change it to:
2568 movl $x@tpoff, %rax
2570 Registers other than %rax may be set up here. */
2572 unsigned int val, type, type2;
2573 bfd_vma roff;
2575 /* First, make sure it's a leaq adding rip to a
2576 32-bit offset into any register, although it's
2577 probably almost always going to be rax. */
2578 roff = rel->r_offset;
2579 BFD_ASSERT (roff >= 3);
2580 type = bfd_get_8 (input_bfd, contents + roff - 3);
2581 BFD_ASSERT ((type & 0xfb) == 0x48);
2582 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2583 BFD_ASSERT (type2 == 0x8d);
2584 val = bfd_get_8 (input_bfd, contents + roff - 1);
2585 BFD_ASSERT ((val & 0xc7) == 0x05);
2586 BFD_ASSERT (roff + 4 <= input_section->size);
2588 /* Now modify the instruction as appropriate. */
2589 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
2590 contents + roff - 3);
2591 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
2592 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2593 contents + roff - 1);
2594 bfd_put_32 (output_bfd, tpoff (info, relocation),
2595 contents + roff);
2596 continue;
2598 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2600 /* GDesc -> LE transition.
2601 It's originally:
2602 call *(%rax)
2603 Turn it into:
2604 nop; nop. */
2606 unsigned int val, type;
2607 bfd_vma roff;
2609 /* First, make sure it's a call *(%rax). */
2610 roff = rel->r_offset;
2611 BFD_ASSERT (roff + 2 <= input_section->size);
2612 type = bfd_get_8 (input_bfd, contents + roff);
2613 BFD_ASSERT (type == 0xff);
2614 val = bfd_get_8 (input_bfd, contents + roff + 1);
2615 BFD_ASSERT (val == 0x10);
2617 /* Now modify the instruction as appropriate. */
2618 bfd_put_8 (output_bfd, 0x90, contents + roff);
2619 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2620 continue;
2622 else
2624 unsigned int val, type, reg;
2626 /* IE->LE transition:
2627 Originally it can be one of:
2628 movq foo@gottpoff(%rip), %reg
2629 addq foo@gottpoff(%rip), %reg
2630 We change it into:
2631 movq $foo, %reg
2632 leaq foo(%reg), %reg
2633 addq $foo, %reg. */
2634 BFD_ASSERT (rel->r_offset >= 3);
2635 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2636 BFD_ASSERT (val == 0x48 || val == 0x4c);
2637 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2638 BFD_ASSERT (type == 0x8b || type == 0x03);
2639 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2640 BFD_ASSERT ((reg & 0xc7) == 5);
2641 reg >>= 3;
2642 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2643 if (type == 0x8b)
2645 /* movq */
2646 if (val == 0x4c)
2647 bfd_put_8 (output_bfd, 0x49,
2648 contents + rel->r_offset - 3);
2649 bfd_put_8 (output_bfd, 0xc7,
2650 contents + rel->r_offset - 2);
2651 bfd_put_8 (output_bfd, 0xc0 | reg,
2652 contents + rel->r_offset - 1);
2654 else if (reg == 4)
2656 /* addq -> addq - addressing with %rsp/%r12 is
2657 special */
2658 if (val == 0x4c)
2659 bfd_put_8 (output_bfd, 0x49,
2660 contents + rel->r_offset - 3);
2661 bfd_put_8 (output_bfd, 0x81,
2662 contents + rel->r_offset - 2);
2663 bfd_put_8 (output_bfd, 0xc0 | reg,
2664 contents + rel->r_offset - 1);
2666 else
2668 /* addq -> leaq */
2669 if (val == 0x4c)
2670 bfd_put_8 (output_bfd, 0x4d,
2671 contents + rel->r_offset - 3);
2672 bfd_put_8 (output_bfd, 0x8d,
2673 contents + rel->r_offset - 2);
2674 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2675 contents + rel->r_offset - 1);
2677 bfd_put_32 (output_bfd, tpoff (info, relocation),
2678 contents + rel->r_offset);
2679 continue;
2683 if (htab->sgot == NULL)
2684 abort ();
2686 if (h != NULL)
2688 off = h->got.offset;
2689 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
2691 else
2693 if (local_got_offsets == NULL)
2694 abort ();
2696 off = local_got_offsets[r_symndx];
2697 offplt = local_tlsdesc_gotents[r_symndx];
2700 if ((off & 1) != 0)
2701 off &= ~1;
2702 else
2704 Elf_Internal_Rela outrel;
2705 bfd_byte *loc;
2706 int dr_type, indx;
2707 asection *sreloc;
2709 if (htab->srelgot == NULL)
2710 abort ();
2712 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2714 if (GOT_TLS_GDESC_P (tls_type))
2716 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
2717 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
2718 + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
2719 outrel.r_offset = (htab->sgotplt->output_section->vma
2720 + htab->sgotplt->output_offset
2721 + offplt
2722 + htab->sgotplt_jump_table_size);
2723 sreloc = htab->srelplt;
2724 loc = sreloc->contents;
2725 loc += sreloc->reloc_count++
2726 * sizeof (Elf64_External_Rela);
2727 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2728 <= sreloc->contents + sreloc->size);
2729 if (indx == 0)
2730 outrel.r_addend = relocation - dtpoff_base (info);
2731 else
2732 outrel.r_addend = 0;
2733 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2736 sreloc = htab->srelgot;
2738 outrel.r_offset = (htab->sgot->output_section->vma
2739 + htab->sgot->output_offset + off);
2741 if (GOT_TLS_GD_P (tls_type))
2742 dr_type = R_X86_64_DTPMOD64;
2743 else if (GOT_TLS_GDESC_P (tls_type))
2744 goto dr_done;
2745 else
2746 dr_type = R_X86_64_TPOFF64;
2748 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2749 outrel.r_addend = 0;
2750 if ((dr_type == R_X86_64_TPOFF64
2751 || dr_type == R_X86_64_TLSDESC) && indx == 0)
2752 outrel.r_addend = relocation - dtpoff_base (info);
2753 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2755 loc = sreloc->contents;
2756 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2757 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2758 <= sreloc->contents + sreloc->size);
2759 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2761 if (GOT_TLS_GD_P (tls_type))
2763 if (indx == 0)
2765 BFD_ASSERT (! unresolved_reloc);
2766 bfd_put_64 (output_bfd,
2767 relocation - dtpoff_base (info),
2768 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2770 else
2772 bfd_put_64 (output_bfd, 0,
2773 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2774 outrel.r_info = ELF64_R_INFO (indx,
2775 R_X86_64_DTPOFF64);
2776 outrel.r_offset += GOT_ENTRY_SIZE;
2777 sreloc->reloc_count++;
2778 loc += sizeof (Elf64_External_Rela);
2779 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2780 <= sreloc->contents + sreloc->size);
2781 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2785 dr_done:
2786 if (h != NULL)
2787 h->got.offset |= 1;
2788 else
2789 local_got_offsets[r_symndx] |= 1;
2792 if (off >= (bfd_vma) -2
2793 && ! GOT_TLS_GDESC_P (tls_type))
2794 abort ();
2795 if (r_type == ELF64_R_TYPE (rel->r_info))
2797 if (r_type == R_X86_64_GOTPC32_TLSDESC
2798 || r_type == R_X86_64_TLSDESC_CALL)
2799 relocation = htab->sgotplt->output_section->vma
2800 + htab->sgotplt->output_offset
2801 + offplt + htab->sgotplt_jump_table_size;
2802 else
2803 relocation = htab->sgot->output_section->vma
2804 + htab->sgot->output_offset + off;
2805 unresolved_reloc = FALSE;
2807 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2809 unsigned int i;
2810 static unsigned char tlsgd[8]
2811 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2813 /* GD->IE transition.
2814 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2815 .word 0x6666; rex64; call __tls_get_addr@plt
2816 Change it into:
2817 movq %fs:0, %rax
2818 addq foo@gottpoff(%rip), %rax */
2819 BFD_ASSERT (rel->r_offset >= 4);
2820 for (i = 0; i < 4; i++)
2821 BFD_ASSERT (bfd_get_8 (input_bfd,
2822 contents + rel->r_offset - 4 + i)
2823 == tlsgd[i]);
2824 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2825 for (i = 0; i < 4; i++)
2826 BFD_ASSERT (bfd_get_8 (input_bfd,
2827 contents + rel->r_offset + 4 + i)
2828 == tlsgd[i+4]);
2829 BFD_ASSERT (rel + 1 < relend);
2830 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2831 memcpy (contents + rel->r_offset - 4,
2832 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2833 16);
2835 relocation = (htab->sgot->output_section->vma
2836 + htab->sgot->output_offset + off
2837 - rel->r_offset
2838 - input_section->output_section->vma
2839 - input_section->output_offset
2840 - 12);
2841 bfd_put_32 (output_bfd, relocation,
2842 contents + rel->r_offset + 8);
2843 /* Skip R_X86_64_PLT32. */
2844 rel++;
2845 continue;
2847 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2849 /* GDesc -> IE transition.
2850 It's originally something like:
2851 leaq x@tlsdesc(%rip), %rax
2853 Change it to:
2854 movq x@gottpoff(%rip), %rax # before nop; nop
2856 Registers other than %rax may be set up here. */
2858 unsigned int val, type, type2;
2859 bfd_vma roff;
2861 /* First, make sure it's a leaq adding rip to a 32-bit
2862 offset into any register, although it's probably
2863 almost always going to be rax. */
2864 roff = rel->r_offset;
2865 BFD_ASSERT (roff >= 3);
2866 type = bfd_get_8 (input_bfd, contents + roff - 3);
2867 BFD_ASSERT ((type & 0xfb) == 0x48);
2868 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2869 BFD_ASSERT (type2 == 0x8d);
2870 val = bfd_get_8 (input_bfd, contents + roff - 1);
2871 BFD_ASSERT ((val & 0xc7) == 0x05);
2872 BFD_ASSERT (roff + 4 <= input_section->size);
2874 /* Now modify the instruction as appropriate. */
2875 /* To turn a leaq into a movq in the form we use it, it
2876 suffices to change the second byte from 0x8d to
2877 0x8b. */
2878 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
2880 bfd_put_32 (output_bfd,
2881 htab->sgot->output_section->vma
2882 + htab->sgot->output_offset + off
2883 - rel->r_offset
2884 - input_section->output_section->vma
2885 - input_section->output_offset
2886 - 4,
2887 contents + roff);
2888 continue;
2890 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2892 /* GDesc -> IE transition.
2893 It's originally:
2894 call *(%rax)
2896 Change it to:
2897 nop; nop. */
2899 unsigned int val, type;
2900 bfd_vma roff;
2902 /* First, make sure it's a call *(%eax). */
2903 roff = rel->r_offset;
2904 BFD_ASSERT (roff + 2 <= input_section->size);
2905 type = bfd_get_8 (input_bfd, contents + roff);
2906 BFD_ASSERT (type == 0xff);
2907 val = bfd_get_8 (input_bfd, contents + roff + 1);
2908 BFD_ASSERT (val == 0x10);
2910 /* Now modify the instruction as appropriate. */
2911 bfd_put_8 (output_bfd, 0x90, contents + roff);
2912 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2914 continue;
2916 else
2917 BFD_ASSERT (FALSE);
2918 break;
2920 case R_X86_64_TLSLD:
2921 if (! info->shared)
2923 /* LD->LE transition:
2924 Ensure it is:
2925 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2926 We change it into:
2927 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2928 BFD_ASSERT (rel->r_offset >= 3);
2929 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2930 == 0x48);
2931 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2932 == 0x8d);
2933 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2934 == 0x3d);
2935 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2936 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2937 == 0xe8);
2938 BFD_ASSERT (rel + 1 < relend);
2939 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2940 memcpy (contents + rel->r_offset - 3,
2941 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2942 /* Skip R_X86_64_PLT32. */
2943 rel++;
2944 continue;
2947 if (htab->sgot == NULL)
2948 abort ();
2950 off = htab->tls_ld_got.offset;
2951 if (off & 1)
2952 off &= ~1;
2953 else
2955 Elf_Internal_Rela outrel;
2956 bfd_byte *loc;
2958 if (htab->srelgot == NULL)
2959 abort ();
2961 outrel.r_offset = (htab->sgot->output_section->vma
2962 + htab->sgot->output_offset + off);
2964 bfd_put_64 (output_bfd, 0,
2965 htab->sgot->contents + off);
2966 bfd_put_64 (output_bfd, 0,
2967 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2968 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2969 outrel.r_addend = 0;
2970 loc = htab->srelgot->contents;
2971 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2972 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2973 htab->tls_ld_got.offset |= 1;
2975 relocation = htab->sgot->output_section->vma
2976 + htab->sgot->output_offset + off;
2977 unresolved_reloc = FALSE;
2978 break;
2980 case R_X86_64_DTPOFF32:
2981 if (info->shared || (input_section->flags & SEC_CODE) == 0)
2982 relocation -= dtpoff_base (info);
2983 else
2984 relocation = tpoff (info, relocation);
2985 break;
2987 case R_X86_64_TPOFF32:
2988 BFD_ASSERT (! info->shared);
2989 relocation = tpoff (info, relocation);
2990 break;
2992 default:
2993 break;
2996 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2997 because such sections are not SEC_ALLOC and thus ld.so will
2998 not process them. */
2999 if (unresolved_reloc
3000 && !((input_section->flags & SEC_DEBUGGING) != 0
3001 && h->def_dynamic))
3002 (*_bfd_error_handler)
3003 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3004 input_bfd,
3005 input_section,
3006 (long) rel->r_offset,
3007 howto->name,
3008 h->root.root.string);
3010 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3011 contents, rel->r_offset,
3012 relocation, rel->r_addend);
3014 if (r != bfd_reloc_ok)
3016 const char *name;
3018 if (h != NULL)
3019 name = h->root.root.string;
3020 else
3022 name = bfd_elf_string_from_elf_section (input_bfd,
3023 symtab_hdr->sh_link,
3024 sym->st_name);
3025 if (name == NULL)
3026 return FALSE;
3027 if (*name == '\0')
3028 name = bfd_section_name (input_bfd, sec);
3031 if (r == bfd_reloc_overflow)
3033 if (h != NULL
3034 && h->root.type == bfd_link_hash_undefweak
3035 && howto->pc_relative)
3036 /* Ignore reloc overflow on branches to undefweak syms. */
3037 continue;
3039 if (! ((*info->callbacks->reloc_overflow)
3040 (info, (h ? &h->root : NULL), name, howto->name,
3041 (bfd_vma) 0, input_bfd, input_section,
3042 rel->r_offset)))
3043 return FALSE;
3045 else
3047 (*_bfd_error_handler)
3048 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3049 input_bfd, input_section,
3050 (long) rel->r_offset, name, (int) r);
3051 return FALSE;
3056 return TRUE;
3059 /* Finish up dynamic symbol handling. We set the contents of various
3060 dynamic sections here. */
3062 static bfd_boolean
3063 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3064 struct bfd_link_info *info,
3065 struct elf_link_hash_entry *h,
3066 Elf_Internal_Sym *sym)
3068 struct elf64_x86_64_link_hash_table *htab;
3070 htab = elf64_x86_64_hash_table (info);
3072 if (h->plt.offset != (bfd_vma) -1)
3074 bfd_vma plt_index;
3075 bfd_vma got_offset;
3076 Elf_Internal_Rela rela;
3077 bfd_byte *loc;
3079 /* This symbol has an entry in the procedure linkage table. Set
3080 it up. */
3081 if (h->dynindx == -1
3082 || htab->splt == NULL
3083 || htab->sgotplt == NULL
3084 || htab->srelplt == NULL)
3085 abort ();
3087 /* Get the index in the procedure linkage table which
3088 corresponds to this symbol. This is the index of this symbol
3089 in all the symbols for which we are making plt entries. The
3090 first entry in the procedure linkage table is reserved. */
3091 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3093 /* Get the offset into the .got table of the entry that
3094 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3095 bytes. The first three are reserved for the dynamic linker. */
3096 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3098 /* Fill in the entry in the procedure linkage table. */
3099 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3100 PLT_ENTRY_SIZE);
3102 /* Insert the relocation positions of the plt section. The magic
3103 numbers at the end of the statements are the positions of the
3104 relocations in the plt section. */
3105 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3106 instruction uses 6 bytes, subtract this value. */
3107 bfd_put_32 (output_bfd,
3108 (htab->sgotplt->output_section->vma
3109 + htab->sgotplt->output_offset
3110 + got_offset
3111 - htab->splt->output_section->vma
3112 - htab->splt->output_offset
3113 - h->plt.offset
3114 - 6),
3115 htab->splt->contents + h->plt.offset + 2);
3116 /* Put relocation index. */
3117 bfd_put_32 (output_bfd, plt_index,
3118 htab->splt->contents + h->plt.offset + 7);
3119 /* Put offset for jmp .PLT0. */
3120 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3121 htab->splt->contents + h->plt.offset + 12);
3123 /* Fill in the entry in the global offset table, initially this
3124 points to the pushq instruction in the PLT which is at offset 6. */
3125 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
3126 + htab->splt->output_offset
3127 + h->plt.offset + 6),
3128 htab->sgotplt->contents + got_offset);
3130 /* Fill in the entry in the .rela.plt section. */
3131 rela.r_offset = (htab->sgotplt->output_section->vma
3132 + htab->sgotplt->output_offset
3133 + got_offset);
3134 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3135 rela.r_addend = 0;
3136 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
3137 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3139 if (!h->def_regular)
3141 /* Mark the symbol as undefined, rather than as defined in
3142 the .plt section. Leave the value if there were any
3143 relocations where pointer equality matters (this is a clue
3144 for the dynamic linker, to make function pointer
3145 comparisons work between an application and shared
3146 library), otherwise set it to zero. If a function is only
3147 called from a binary, there is no need to slow down
3148 shared libraries because of that. */
3149 sym->st_shndx = SHN_UNDEF;
3150 if (!h->pointer_equality_needed)
3151 sym->st_value = 0;
3155 if (h->got.offset != (bfd_vma) -1
3156 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3157 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3159 Elf_Internal_Rela rela;
3160 bfd_byte *loc;
3162 /* This symbol has an entry in the global offset table. Set it
3163 up. */
3164 if (htab->sgot == NULL || htab->srelgot == NULL)
3165 abort ();
3167 rela.r_offset = (htab->sgot->output_section->vma
3168 + htab->sgot->output_offset
3169 + (h->got.offset &~ (bfd_vma) 1));
3171 /* If this is a static link, or it is a -Bsymbolic link and the
3172 symbol is defined locally or was forced to be local because
3173 of a version file, we just want to emit a RELATIVE reloc.
3174 The entry in the global offset table will already have been
3175 initialized in the relocate_section function. */
3176 if (info->shared
3177 && SYMBOL_REFERENCES_LOCAL (info, h))
3179 BFD_ASSERT((h->got.offset & 1) != 0);
3180 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3181 rela.r_addend = (h->root.u.def.value
3182 + h->root.u.def.section->output_section->vma
3183 + h->root.u.def.section->output_offset);
3185 else
3187 BFD_ASSERT((h->got.offset & 1) == 0);
3188 bfd_put_64 (output_bfd, (bfd_vma) 0,
3189 htab->sgot->contents + h->got.offset);
3190 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3191 rela.r_addend = 0;
3194 loc = htab->srelgot->contents;
3195 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3196 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3199 if (h->needs_copy)
3201 Elf_Internal_Rela rela;
3202 bfd_byte *loc;
3204 /* This symbol needs a copy reloc. Set it up. */
3206 if (h->dynindx == -1
3207 || (h->root.type != bfd_link_hash_defined
3208 && h->root.type != bfd_link_hash_defweak)
3209 || htab->srelbss == NULL)
3210 abort ();
3212 rela.r_offset = (h->root.u.def.value
3213 + h->root.u.def.section->output_section->vma
3214 + h->root.u.def.section->output_offset);
3215 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3216 rela.r_addend = 0;
3217 loc = htab->srelbss->contents;
3218 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
3219 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3222 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3223 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3224 || h == htab->elf.hgot)
3225 sym->st_shndx = SHN_ABS;
3227 return TRUE;
3230 /* Used to decide how to sort relocs in an optimal manner for the
3231 dynamic linker, before writing them out. */
3233 static enum elf_reloc_type_class
3234 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
3236 switch ((int) ELF64_R_TYPE (rela->r_info))
3238 case R_X86_64_RELATIVE:
3239 return reloc_class_relative;
3240 case R_X86_64_JUMP_SLOT:
3241 return reloc_class_plt;
3242 case R_X86_64_COPY:
3243 return reloc_class_copy;
3244 default:
3245 return reloc_class_normal;
3249 /* Finish up the dynamic sections. */
3251 static bfd_boolean
3252 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3254 struct elf64_x86_64_link_hash_table *htab;
3255 bfd *dynobj;
3256 asection *sdyn;
3258 htab = elf64_x86_64_hash_table (info);
3259 dynobj = htab->elf.dynobj;
3260 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3262 if (htab->elf.dynamic_sections_created)
3264 Elf64_External_Dyn *dyncon, *dynconend;
3266 if (sdyn == NULL || htab->sgot == NULL)
3267 abort ();
3269 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3270 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3271 for (; dyncon < dynconend; dyncon++)
3273 Elf_Internal_Dyn dyn;
3274 asection *s;
3276 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3278 switch (dyn.d_tag)
3280 default:
3281 continue;
3283 case DT_PLTGOT:
3284 s = htab->sgotplt;
3285 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3286 break;
3288 case DT_JMPREL:
3289 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3290 break;
3292 case DT_PLTRELSZ:
3293 s = htab->srelplt->output_section;
3294 dyn.d_un.d_val = s->size;
3295 break;
3297 case DT_RELASZ:
3298 /* The procedure linkage table relocs (DT_JMPREL) should
3299 not be included in the overall relocs (DT_RELA).
3300 Therefore, we override the DT_RELASZ entry here to
3301 make it not include the JMPREL relocs. Since the
3302 linker script arranges for .rela.plt to follow all
3303 other relocation sections, we don't have to worry
3304 about changing the DT_RELA entry. */
3305 if (htab->srelplt != NULL)
3307 s = htab->srelplt->output_section;
3308 dyn.d_un.d_val -= s->size;
3310 break;
3312 case DT_TLSDESC_PLT:
3313 s = htab->splt;
3314 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3315 + htab->tlsdesc_plt;
3316 break;
3318 case DT_TLSDESC_GOT:
3319 s = htab->sgot;
3320 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3321 + htab->tlsdesc_got;
3322 break;
3325 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3328 /* Fill in the special first entry in the procedure linkage table. */
3329 if (htab->splt && htab->splt->size > 0)
3331 /* Fill in the first entry in the procedure linkage table. */
3332 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
3333 PLT_ENTRY_SIZE);
3334 /* Add offset for pushq GOT+8(%rip), since the instruction
3335 uses 6 bytes subtract this value. */
3336 bfd_put_32 (output_bfd,
3337 (htab->sgotplt->output_section->vma
3338 + htab->sgotplt->output_offset
3340 - htab->splt->output_section->vma
3341 - htab->splt->output_offset
3342 - 6),
3343 htab->splt->contents + 2);
3344 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3345 the end of the instruction. */
3346 bfd_put_32 (output_bfd,
3347 (htab->sgotplt->output_section->vma
3348 + htab->sgotplt->output_offset
3349 + 16
3350 - htab->splt->output_section->vma
3351 - htab->splt->output_offset
3352 - 12),
3353 htab->splt->contents + 8);
3355 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
3356 PLT_ENTRY_SIZE;
3358 if (htab->tlsdesc_plt)
3360 bfd_put_64 (output_bfd, (bfd_vma) 0,
3361 htab->sgot->contents + htab->tlsdesc_got);
3363 memcpy (htab->splt->contents + htab->tlsdesc_plt,
3364 elf64_x86_64_plt0_entry,
3365 PLT_ENTRY_SIZE);
3367 /* Add offset for pushq GOT+8(%rip), since the
3368 instruction uses 6 bytes subtract this value. */
3369 bfd_put_32 (output_bfd,
3370 (htab->sgotplt->output_section->vma
3371 + htab->sgotplt->output_offset
3373 - htab->splt->output_section->vma
3374 - htab->splt->output_offset
3375 - htab->tlsdesc_plt
3376 - 6),
3377 htab->splt->contents + htab->tlsdesc_plt + 2);
3378 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3379 htab->tlsdesc_got. The 12 is the offset to the end of
3380 the instruction. */
3381 bfd_put_32 (output_bfd,
3382 (htab->sgot->output_section->vma
3383 + htab->sgot->output_offset
3384 + htab->tlsdesc_got
3385 - htab->splt->output_section->vma
3386 - htab->splt->output_offset
3387 - htab->tlsdesc_plt
3388 - 12),
3389 htab->splt->contents + htab->tlsdesc_plt + 8);
3394 if (htab->sgotplt)
3396 /* Fill in the first three entries in the global offset table. */
3397 if (htab->sgotplt->size > 0)
3399 /* Set the first entry in the global offset table to the address of
3400 the dynamic section. */
3401 if (sdyn == NULL)
3402 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
3403 else
3404 bfd_put_64 (output_bfd,
3405 sdyn->output_section->vma + sdyn->output_offset,
3406 htab->sgotplt->contents);
3407 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3408 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
3409 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
3412 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
3413 GOT_ENTRY_SIZE;
3416 if (htab->sgot && htab->sgot->size > 0)
3417 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
3418 = GOT_ENTRY_SIZE;
3420 return TRUE;
3423 /* Return address for Ith PLT stub in section PLT, for relocation REL
3424 or (bfd_vma) -1 if it should not be included. */
3426 static bfd_vma
3427 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
3428 const arelent *rel ATTRIBUTE_UNUSED)
3430 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3433 /* Handle an x86-64 specific section when reading an object file. This
3434 is called when elfcode.h finds a section with an unknown type. */
3436 static bfd_boolean
3437 elf64_x86_64_section_from_shdr (bfd *abfd,
3438 Elf_Internal_Shdr *hdr,
3439 const char *name,
3440 int shindex)
3442 if (hdr->sh_type != SHT_X86_64_UNWIND)
3443 return FALSE;
3445 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3446 return FALSE;
3448 return TRUE;
3451 /* Hook called by the linker routine which adds symbols from an object
3452 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3453 of .bss. */
3455 static bfd_boolean
3456 elf64_x86_64_add_symbol_hook (bfd *abfd,
3457 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3458 Elf_Internal_Sym *sym,
3459 const char **namep ATTRIBUTE_UNUSED,
3460 flagword *flagsp ATTRIBUTE_UNUSED,
3461 asection **secp, bfd_vma *valp)
3463 asection *lcomm;
3465 switch (sym->st_shndx)
3467 case SHN_X86_64_LCOMMON:
3468 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
3469 if (lcomm == NULL)
3471 lcomm = bfd_make_section_with_flags (abfd,
3472 "LARGE_COMMON",
3473 (SEC_ALLOC
3474 | SEC_IS_COMMON
3475 | SEC_LINKER_CREATED));
3476 if (lcomm == NULL)
3477 return FALSE;
3478 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
3480 *secp = lcomm;
3481 *valp = sym->st_size;
3482 break;
3484 return TRUE;
3488 /* Given a BFD section, try to locate the corresponding ELF section
3489 index. */
3491 static bfd_boolean
3492 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
3493 asection *sec, int *index)
3495 if (sec == &_bfd_elf_large_com_section)
3497 *index = SHN_X86_64_LCOMMON;
3498 return TRUE;
3500 return FALSE;
3503 /* Process a symbol. */
3505 static void
3506 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
3507 asymbol *asym)
3509 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
3511 switch (elfsym->internal_elf_sym.st_shndx)
3513 case SHN_X86_64_LCOMMON:
3514 asym->section = &_bfd_elf_large_com_section;
3515 asym->value = elfsym->internal_elf_sym.st_size;
3516 /* Common symbol doesn't set BSF_GLOBAL. */
3517 asym->flags &= ~BSF_GLOBAL;
3518 break;
3522 static bfd_boolean
3523 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
3525 return (sym->st_shndx == SHN_COMMON
3526 || sym->st_shndx == SHN_X86_64_LCOMMON);
3529 static unsigned int
3530 elf64_x86_64_common_section_index (asection *sec)
3532 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3533 return SHN_COMMON;
3534 else
3535 return SHN_X86_64_LCOMMON;
3538 static asection *
3539 elf64_x86_64_common_section (asection *sec)
3541 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3542 return bfd_com_section_ptr;
3543 else
3544 return &_bfd_elf_large_com_section;
3547 static bfd_boolean
3548 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3549 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
3550 struct elf_link_hash_entry *h,
3551 Elf_Internal_Sym *sym,
3552 asection **psec,
3553 bfd_vma *pvalue ATTRIBUTE_UNUSED,
3554 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
3555 bfd_boolean *skip ATTRIBUTE_UNUSED,
3556 bfd_boolean *override ATTRIBUTE_UNUSED,
3557 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
3558 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
3559 bfd_boolean *newdef ATTRIBUTE_UNUSED,
3560 bfd_boolean *newdyn,
3561 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
3562 bfd_boolean *newweak ATTRIBUTE_UNUSED,
3563 bfd *abfd ATTRIBUTE_UNUSED,
3564 asection **sec,
3565 bfd_boolean *olddef ATTRIBUTE_UNUSED,
3566 bfd_boolean *olddyn,
3567 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
3568 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
3569 bfd *oldbfd,
3570 asection **oldsec)
3572 /* A normal common symbol and a large common symbol result in a
3573 normal common symbol. We turn the large common symbol into a
3574 normal one. */
3575 if (!*olddyn
3576 && h->root.type == bfd_link_hash_common
3577 && !*newdyn
3578 && bfd_is_com_section (*sec)
3579 && *oldsec != *sec)
3581 if (sym->st_shndx == SHN_COMMON
3582 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
3584 h->root.u.c.p->section
3585 = bfd_make_section_old_way (oldbfd, "COMMON");
3586 h->root.u.c.p->section->flags = SEC_ALLOC;
3588 else if (sym->st_shndx == SHN_X86_64_LCOMMON
3589 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
3590 *psec = *sec = bfd_com_section_ptr;
3593 return TRUE;
3596 static int
3597 elf64_x86_64_additional_program_headers (bfd *abfd)
3599 asection *s;
3600 int count = 0;
3602 /* Check to see if we need a large readonly segment. */
3603 s = bfd_get_section_by_name (abfd, ".lrodata");
3604 if (s && (s->flags & SEC_LOAD))
3605 count++;
3607 /* Check to see if we need a large data segment. Since .lbss sections
3608 is placed right after the .bss section, there should be no need for
3609 a large data segment just because of .lbss. */
3610 s = bfd_get_section_by_name (abfd, ".ldata");
3611 if (s && (s->flags & SEC_LOAD))
3612 count++;
3614 return count;
3617 static const struct bfd_elf_special_section
3618 elf64_x86_64_special_sections[]=
3620 { ".gnu.linkonce.lb", 16, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3621 { ".gnu.linkonce.lr", 16, -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3622 { ".gnu.linkonce.lt", 16, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
3623 { ".lbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3624 { ".ldata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3625 { ".lrodata", 8, -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3626 { NULL, 0, 0, 0, 0 }
3629 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3630 #define TARGET_LITTLE_NAME "elf64-x86-64"
3631 #define ELF_ARCH bfd_arch_i386
3632 #define ELF_MACHINE_CODE EM_X86_64
3633 #define ELF_MAXPAGESIZE 0x200000
3634 #define ELF_MINPAGESIZE 0x1000
3635 #define ELF_COMMONPAGESIZE 0x1000
3637 #define elf_backend_can_gc_sections 1
3638 #define elf_backend_can_refcount 1
3639 #define elf_backend_want_got_plt 1
3640 #define elf_backend_plt_readonly 1
3641 #define elf_backend_want_plt_sym 0
3642 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
3643 #define elf_backend_rela_normal 1
3645 #define elf_info_to_howto elf64_x86_64_info_to_howto
3647 #define bfd_elf64_bfd_link_hash_table_create \
3648 elf64_x86_64_link_hash_table_create
3649 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
3651 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
3652 #define elf_backend_check_relocs elf64_x86_64_check_relocs
3653 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3654 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3655 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
3656 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3657 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3658 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3659 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3660 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
3661 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
3662 #define elf_backend_relocate_section elf64_x86_64_relocate_section
3663 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
3664 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
3665 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
3666 #define elf_backend_object_p elf64_x86_64_elf_object_p
3667 #define bfd_elf64_mkobject elf64_x86_64_mkobject
3669 #define elf_backend_section_from_shdr \
3670 elf64_x86_64_section_from_shdr
3672 #define elf_backend_section_from_bfd_section \
3673 elf64_x86_64_elf_section_from_bfd_section
3674 #define elf_backend_add_symbol_hook \
3675 elf64_x86_64_add_symbol_hook
3676 #define elf_backend_symbol_processing \
3677 elf64_x86_64_symbol_processing
3678 #define elf_backend_common_section_index \
3679 elf64_x86_64_common_section_index
3680 #define elf_backend_common_section \
3681 elf64_x86_64_common_section
3682 #define elf_backend_common_definition \
3683 elf64_x86_64_common_definition
3684 #define elf_backend_merge_symbol \
3685 elf64_x86_64_merge_symbol
3686 #define elf_backend_special_sections \
3687 elf64_x86_64_special_sections
3688 #define elf_backend_additional_program_headers \
3689 elf64_x86_64_additional_program_headers
3691 #include "elf64-target.h"