Part of binutils 2.20 update
[cegcc.git] / cegcc / src / binutils / bfd / elf64-x86-64.c
blob779ad0f80f4dc618747785ee71dc1751d1dc5389
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "bfd_stdint.h"
29 #include "objalloc.h"
30 #include "hashtab.h"
32 #include "elf/x86-64.h"
34 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
35 #define MINUS_ONE (~ (bfd_vma) 0)
37 /* The relocation "howto" table. Order of fields:
38 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
39 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
40 static reloc_howto_type x86_64_elf_howto_table[] =
42 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
43 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
44 FALSE),
45 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
46 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
47 FALSE),
48 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
49 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
50 TRUE),
51 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
52 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
53 FALSE),
54 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
55 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
56 TRUE),
57 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
58 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
59 FALSE),
60 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
61 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
62 MINUS_ONE, FALSE),
63 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
64 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
65 MINUS_ONE, FALSE),
66 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
67 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
68 MINUS_ONE, FALSE),
69 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
70 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
71 0xffffffff, TRUE),
72 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
73 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
74 FALSE),
75 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
76 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
77 FALSE),
78 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
79 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
80 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
81 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
82 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
84 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
85 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
86 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
87 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
88 MINUS_ONE, FALSE),
89 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
90 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
91 MINUS_ONE, FALSE),
92 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
93 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
94 MINUS_ONE, FALSE),
95 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
96 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
97 0xffffffff, TRUE),
98 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
99 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
100 0xffffffff, TRUE),
101 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
102 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
103 0xffffffff, FALSE),
104 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
105 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
106 0xffffffff, TRUE),
107 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
108 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
109 0xffffffff, FALSE),
110 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
111 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
112 TRUE),
113 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
114 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
115 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
116 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
117 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
118 FALSE, 0xffffffff, 0xffffffff, TRUE),
119 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
120 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
121 FALSE),
122 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
123 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
124 MINUS_ONE, TRUE),
125 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
126 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
127 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
128 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
129 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
130 MINUS_ONE, FALSE),
131 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
132 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
133 MINUS_ONE, FALSE),
134 EMPTY_HOWTO (32),
135 EMPTY_HOWTO (33),
136 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
137 complain_overflow_bitfield, bfd_elf_generic_reloc,
138 "R_X86_64_GOTPC32_TLSDESC",
139 FALSE, 0xffffffff, 0xffffffff, TRUE),
140 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
141 complain_overflow_dont, bfd_elf_generic_reloc,
142 "R_X86_64_TLSDESC_CALL",
143 FALSE, 0, 0, FALSE),
144 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
145 complain_overflow_bitfield, bfd_elf_generic_reloc,
146 "R_X86_64_TLSDESC",
147 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
148 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
149 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
150 MINUS_ONE, FALSE),
152 /* We have a gap in the reloc numbers here.
153 R_X86_64_standard counts the number up to this point, and
154 R_X86_64_vt_offset is the value to subtract from a reloc type of
155 R_X86_64_GNU_VT* to form an index into this table. */
156 #define R_X86_64_standard (R_X86_64_IRELATIVE + 1)
157 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
159 /* GNU extension to record C++ vtable hierarchy. */
160 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
161 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
163 /* GNU extension to record C++ vtable member usage. */
164 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
165 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
166 FALSE)
169 #define IS_X86_64_PCREL_TYPE(TYPE) \
170 ( ((TYPE) == R_X86_64_PC8) \
171 || ((TYPE) == R_X86_64_PC16) \
172 || ((TYPE) == R_X86_64_PC32) \
173 || ((TYPE) == R_X86_64_PC64))
175 /* Map BFD relocs to the x86_64 elf relocs. */
176 struct elf_reloc_map
178 bfd_reloc_code_real_type bfd_reloc_val;
179 unsigned char elf_reloc_val;
182 static const struct elf_reloc_map x86_64_reloc_map[] =
184 { BFD_RELOC_NONE, R_X86_64_NONE, },
185 { BFD_RELOC_64, R_X86_64_64, },
186 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
187 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
188 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
189 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
190 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
191 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
192 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
193 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
194 { BFD_RELOC_32, R_X86_64_32, },
195 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
196 { BFD_RELOC_16, R_X86_64_16, },
197 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
198 { BFD_RELOC_8, R_X86_64_8, },
199 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
200 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
201 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
202 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
203 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
204 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
205 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
206 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
207 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
208 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
209 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
210 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
211 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
212 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
213 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
214 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
215 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
216 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
217 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
218 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
219 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
220 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
221 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
224 static reloc_howto_type *
225 elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
227 unsigned i;
229 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
230 || r_type >= (unsigned int) R_X86_64_max)
232 if (r_type >= (unsigned int) R_X86_64_standard)
234 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
235 abfd, (int) r_type);
236 r_type = R_X86_64_NONE;
238 i = r_type;
240 else
241 i = r_type - (unsigned int) R_X86_64_vt_offset;
242 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
243 return &x86_64_elf_howto_table[i];
246 /* Given a BFD reloc type, return a HOWTO structure. */
247 static reloc_howto_type *
248 elf64_x86_64_reloc_type_lookup (bfd *abfd,
249 bfd_reloc_code_real_type code)
251 unsigned int i;
253 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
254 i++)
256 if (x86_64_reloc_map[i].bfd_reloc_val == code)
257 return elf64_x86_64_rtype_to_howto (abfd,
258 x86_64_reloc_map[i].elf_reloc_val);
260 return 0;
263 static reloc_howto_type *
264 elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
265 const char *r_name)
267 unsigned int i;
269 for (i = 0;
270 i < (sizeof (x86_64_elf_howto_table)
271 / sizeof (x86_64_elf_howto_table[0]));
272 i++)
273 if (x86_64_elf_howto_table[i].name != NULL
274 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
275 return &x86_64_elf_howto_table[i];
277 return NULL;
280 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
282 static void
283 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
284 Elf_Internal_Rela *dst)
286 unsigned r_type;
288 r_type = ELF64_R_TYPE (dst->r_info);
289 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
290 BFD_ASSERT (r_type == cache_ptr->howto->type);
293 /* Support for core dump NOTE sections. */
294 static bfd_boolean
295 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
297 int offset;
298 size_t size;
300 switch (note->descsz)
302 default:
303 return FALSE;
305 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
306 /* pr_cursig */
307 elf_tdata (abfd)->core_signal
308 = bfd_get_16 (abfd, note->descdata + 12);
310 /* pr_pid */
311 elf_tdata (abfd)->core_pid
312 = bfd_get_32 (abfd, note->descdata + 32);
314 /* pr_reg */
315 offset = 112;
316 size = 216;
318 break;
321 /* Make a ".reg/999" section. */
322 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
323 size, note->descpos + offset);
326 static bfd_boolean
327 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
329 switch (note->descsz)
331 default:
332 return FALSE;
334 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
335 elf_tdata (abfd)->core_program
336 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
337 elf_tdata (abfd)->core_command
338 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
341 /* Note that for some reason, a spurious space is tacked
342 onto the end of the args in some (at least one anyway)
343 implementations, so strip it off if it exists. */
346 char *command = elf_tdata (abfd)->core_command;
347 int n = strlen (command);
349 if (0 < n && command[n - 1] == ' ')
350 command[n - 1] = '\0';
353 return TRUE;
356 /* Functions for the x86-64 ELF linker. */
358 /* The name of the dynamic interpreter. This is put in the .interp
359 section. */
361 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
363 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
364 copying dynamic variables from a shared lib into an app's dynbss
365 section, and instead use a dynamic relocation to point into the
366 shared lib. */
367 #define ELIMINATE_COPY_RELOCS 1
369 /* The size in bytes of an entry in the global offset table. */
371 #define GOT_ENTRY_SIZE 8
373 /* The size in bytes of an entry in the procedure linkage table. */
375 #define PLT_ENTRY_SIZE 16
377 /* The first entry in a procedure linkage table looks like this. See the
378 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
380 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
382 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
383 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
384 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
387 /* Subsequent entries in a procedure linkage table look like this. */
389 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
391 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
392 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
393 0x68, /* pushq immediate */
394 0, 0, 0, 0, /* replaced with index into relocation table. */
395 0xe9, /* jmp relative */
396 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
399 /* x86-64 ELF linker hash entry. */
401 struct elf64_x86_64_link_hash_entry
403 struct elf_link_hash_entry elf;
405 /* Track dynamic relocs copied for this symbol. */
406 struct elf_dyn_relocs *dyn_relocs;
408 #define GOT_UNKNOWN 0
409 #define GOT_NORMAL 1
410 #define GOT_TLS_GD 2
411 #define GOT_TLS_IE 3
412 #define GOT_TLS_GDESC 4
413 #define GOT_TLS_GD_BOTH_P(type) \
414 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
415 #define GOT_TLS_GD_P(type) \
416 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
417 #define GOT_TLS_GDESC_P(type) \
418 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
419 #define GOT_TLS_GD_ANY_P(type) \
420 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
421 unsigned char tls_type;
423 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
424 starting at the end of the jump table. */
425 bfd_vma tlsdesc_got;
428 #define elf64_x86_64_hash_entry(ent) \
429 ((struct elf64_x86_64_link_hash_entry *)(ent))
431 struct elf64_x86_64_obj_tdata
433 struct elf_obj_tdata root;
435 /* tls_type for each local got entry. */
436 char *local_got_tls_type;
438 /* GOTPLT entries for TLS descriptors. */
439 bfd_vma *local_tlsdesc_gotent;
442 #define elf64_x86_64_tdata(abfd) \
443 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
445 #define elf64_x86_64_local_got_tls_type(abfd) \
446 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
448 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
449 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
451 #define is_x86_64_elf(bfd) \
452 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
453 && elf_tdata (bfd) != NULL \
454 && elf_object_id (bfd) == X86_64_ELF_TDATA)
456 static bfd_boolean
457 elf64_x86_64_mkobject (bfd *abfd)
459 return bfd_elf_allocate_object (abfd, sizeof (struct elf64_x86_64_obj_tdata),
460 X86_64_ELF_TDATA);
463 /* x86-64 ELF linker hash table. */
465 struct elf64_x86_64_link_hash_table
467 struct elf_link_hash_table elf;
469 /* Short-cuts to get to dynamic linker sections. */
470 asection *sdynbss;
471 asection *srelbss;
473 /* The offset into splt of the PLT entry for the TLS descriptor
474 resolver. Special values are 0, if not necessary (or not found
475 to be necessary yet), and -1 if needed but not determined
476 yet. */
477 bfd_vma tlsdesc_plt;
478 /* The offset into sgot of the GOT entry used by the PLT entry
479 above. */
480 bfd_vma tlsdesc_got;
482 union {
483 bfd_signed_vma refcount;
484 bfd_vma offset;
485 } tls_ld_got;
487 /* The amount of space used by the jump slots in the GOT. */
488 bfd_vma sgotplt_jump_table_size;
490 /* Small local sym cache. */
491 struct sym_cache sym_cache;
493 /* _TLS_MODULE_BASE_ symbol. */
494 struct bfd_link_hash_entry *tls_module_base;
496 /* Used by local STT_GNU_IFUNC symbols. */
497 htab_t loc_hash_table;
498 void *loc_hash_memory;
501 /* Get the x86-64 ELF linker hash table from a link_info structure. */
503 #define elf64_x86_64_hash_table(p) \
504 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
506 #define elf64_x86_64_compute_jump_table_size(htab) \
507 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
509 /* Create an entry in an x86-64 ELF linker hash table. */
511 static struct bfd_hash_entry *
512 elf64_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
513 struct bfd_hash_table *table,
514 const char *string)
516 /* Allocate the structure if it has not already been allocated by a
517 subclass. */
518 if (entry == NULL)
520 entry = (struct bfd_hash_entry *)
521 bfd_hash_allocate (table,
522 sizeof (struct elf64_x86_64_link_hash_entry));
523 if (entry == NULL)
524 return entry;
527 /* Call the allocation method of the superclass. */
528 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
529 if (entry != NULL)
531 struct elf64_x86_64_link_hash_entry *eh;
533 eh = (struct elf64_x86_64_link_hash_entry *) entry;
534 eh->dyn_relocs = NULL;
535 eh->tls_type = GOT_UNKNOWN;
536 eh->tlsdesc_got = (bfd_vma) -1;
539 return entry;
542 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
543 for local symbol so that we can handle local STT_GNU_IFUNC symbols
544 as global symbol. We reuse indx and dynstr_index for local symbol
545 hash since they aren't used by global symbols in this backend. */
547 static hashval_t
548 elf64_x86_64_local_htab_hash (const void *ptr)
550 struct elf_link_hash_entry *h
551 = (struct elf_link_hash_entry *) ptr;
552 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
555 /* Compare local hash entries. */
557 static int
558 elf64_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
560 struct elf_link_hash_entry *h1
561 = (struct elf_link_hash_entry *) ptr1;
562 struct elf_link_hash_entry *h2
563 = (struct elf_link_hash_entry *) ptr2;
565 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
568 /* Find and/or create a hash entry for local symbol. */
570 static struct elf_link_hash_entry *
571 elf64_x86_64_get_local_sym_hash (struct elf64_x86_64_link_hash_table *htab,
572 bfd *abfd, const Elf_Internal_Rela *rel,
573 bfd_boolean create)
575 struct elf64_x86_64_link_hash_entry e, *ret;
576 asection *sec = abfd->sections;
577 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
578 ELF64_R_SYM (rel->r_info));
579 void **slot;
581 e.elf.indx = sec->id;
582 e.elf.dynstr_index = ELF64_R_SYM (rel->r_info);
583 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
584 create ? INSERT : NO_INSERT);
586 if (!slot)
587 return NULL;
589 if (*slot)
591 ret = (struct elf64_x86_64_link_hash_entry *) *slot;
592 return &ret->elf;
595 ret = (struct elf64_x86_64_link_hash_entry *)
596 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
597 sizeof (struct elf64_x86_64_link_hash_entry));
598 if (ret)
600 memset (ret, 0, sizeof (*ret));
601 ret->elf.indx = sec->id;
602 ret->elf.dynstr_index = ELF64_R_SYM (rel->r_info);
603 ret->elf.dynindx = -1;
604 ret->elf.plt.offset = (bfd_vma) -1;
605 ret->elf.got.offset = (bfd_vma) -1;
606 *slot = ret;
608 return &ret->elf;
611 /* Create an X86-64 ELF linker hash table. */
613 static struct bfd_link_hash_table *
614 elf64_x86_64_link_hash_table_create (bfd *abfd)
616 struct elf64_x86_64_link_hash_table *ret;
617 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
619 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
620 if (ret == NULL)
621 return NULL;
623 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
624 elf64_x86_64_link_hash_newfunc,
625 sizeof (struct elf64_x86_64_link_hash_entry)))
627 free (ret);
628 return NULL;
631 ret->sdynbss = NULL;
632 ret->srelbss = NULL;
633 ret->sym_cache.abfd = NULL;
634 ret->tlsdesc_plt = 0;
635 ret->tlsdesc_got = 0;
636 ret->tls_ld_got.refcount = 0;
637 ret->sgotplt_jump_table_size = 0;
638 ret->tls_module_base = NULL;
640 ret->loc_hash_table = htab_try_create (1024,
641 elf64_x86_64_local_htab_hash,
642 elf64_x86_64_local_htab_eq,
643 NULL);
644 ret->loc_hash_memory = objalloc_create ();
645 if (!ret->loc_hash_table || !ret->loc_hash_memory)
647 free (ret);
648 return NULL;
651 return &ret->elf.root;
654 /* Destroy an X86-64 ELF linker hash table. */
656 static void
657 elf64_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
659 struct elf64_x86_64_link_hash_table *htab
660 = (struct elf64_x86_64_link_hash_table *) hash;
662 if (htab->loc_hash_table)
663 htab_delete (htab->loc_hash_table);
664 if (htab->loc_hash_memory)
665 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
666 _bfd_generic_link_hash_table_free (hash);
669 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
670 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
671 hash table. */
673 static bfd_boolean
674 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
676 struct elf64_x86_64_link_hash_table *htab;
678 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
679 return FALSE;
681 htab = elf64_x86_64_hash_table (info);
682 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
683 if (!info->shared)
684 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
686 if (!htab->sdynbss
687 || (!info->shared && !htab->srelbss))
688 abort ();
690 return TRUE;
693 /* Copy the extra info we tack onto an elf_link_hash_entry. */
695 static void
696 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
697 struct elf_link_hash_entry *dir,
698 struct elf_link_hash_entry *ind)
700 struct elf64_x86_64_link_hash_entry *edir, *eind;
702 edir = (struct elf64_x86_64_link_hash_entry *) dir;
703 eind = (struct elf64_x86_64_link_hash_entry *) ind;
705 if (eind->dyn_relocs != NULL)
707 if (edir->dyn_relocs != NULL)
709 struct elf_dyn_relocs **pp;
710 struct elf_dyn_relocs *p;
712 /* Add reloc counts against the indirect sym to the direct sym
713 list. Merge any entries against the same section. */
714 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
716 struct elf_dyn_relocs *q;
718 for (q = edir->dyn_relocs; q != NULL; q = q->next)
719 if (q->sec == p->sec)
721 q->pc_count += p->pc_count;
722 q->count += p->count;
723 *pp = p->next;
724 break;
726 if (q == NULL)
727 pp = &p->next;
729 *pp = edir->dyn_relocs;
732 edir->dyn_relocs = eind->dyn_relocs;
733 eind->dyn_relocs = NULL;
736 if (ind->root.type == bfd_link_hash_indirect
737 && dir->got.refcount <= 0)
739 edir->tls_type = eind->tls_type;
740 eind->tls_type = GOT_UNKNOWN;
743 if (ELIMINATE_COPY_RELOCS
744 && ind->root.type != bfd_link_hash_indirect
745 && dir->dynamic_adjusted)
747 /* If called to transfer flags for a weakdef during processing
748 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
749 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
750 dir->ref_dynamic |= ind->ref_dynamic;
751 dir->ref_regular |= ind->ref_regular;
752 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
753 dir->needs_plt |= ind->needs_plt;
754 dir->pointer_equality_needed |= ind->pointer_equality_needed;
756 else
757 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
760 static bfd_boolean
761 elf64_x86_64_elf_object_p (bfd *abfd)
763 /* Set the right machine number for an x86-64 elf64 file. */
764 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
765 return TRUE;
768 typedef union
770 unsigned char c[2];
771 uint16_t i;
773 x86_64_opcode16;
775 typedef union
777 unsigned char c[4];
778 uint32_t i;
780 x86_64_opcode32;
782 /* Return TRUE if the TLS access code sequence support transition
783 from R_TYPE. */
785 static bfd_boolean
786 elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec,
787 bfd_byte *contents,
788 Elf_Internal_Shdr *symtab_hdr,
789 struct elf_link_hash_entry **sym_hashes,
790 unsigned int r_type,
791 const Elf_Internal_Rela *rel,
792 const Elf_Internal_Rela *relend)
794 unsigned int val;
795 unsigned long r_symndx;
796 struct elf_link_hash_entry *h;
797 bfd_vma offset;
799 /* Get the section contents. */
800 if (contents == NULL)
802 if (elf_section_data (sec)->this_hdr.contents != NULL)
803 contents = elf_section_data (sec)->this_hdr.contents;
804 else
806 /* FIXME: How to better handle error condition? */
807 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
808 return FALSE;
810 /* Cache the section contents for elf_link_input_bfd. */
811 elf_section_data (sec)->this_hdr.contents = contents;
815 offset = rel->r_offset;
816 switch (r_type)
818 case R_X86_64_TLSGD:
819 case R_X86_64_TLSLD:
820 if ((rel + 1) >= relend)
821 return FALSE;
823 if (r_type == R_X86_64_TLSGD)
825 /* Check transition from GD access model. Only
826 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
827 .word 0x6666; rex64; call __tls_get_addr
828 can transit to different access model. */
830 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } },
831 call = { { 0x66, 0x66, 0x48, 0xe8 } };
832 if (offset < 4
833 || (offset + 12) > sec->size
834 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i
835 || bfd_get_32 (abfd, contents + offset + 4) != call.i)
836 return FALSE;
838 else
840 /* Check transition from LD access model. Only
841 leaq foo@tlsld(%rip), %rdi;
842 call __tls_get_addr
843 can transit to different access model. */
845 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
846 x86_64_opcode32 op;
848 if (offset < 3 || (offset + 9) > sec->size)
849 return FALSE;
851 op.i = bfd_get_32 (abfd, contents + offset - 3);
852 op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
853 if (op.i != ld.i)
854 return FALSE;
857 r_symndx = ELF64_R_SYM (rel[1].r_info);
858 if (r_symndx < symtab_hdr->sh_info)
859 return FALSE;
861 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
862 /* Use strncmp to check __tls_get_addr since __tls_get_addr
863 may be versioned. */
864 return (h != NULL
865 && h->root.root.string != NULL
866 && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
867 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
868 && (strncmp (h->root.root.string,
869 "__tls_get_addr", 14) == 0));
871 case R_X86_64_GOTTPOFF:
872 /* Check transition from IE access model:
873 movq foo@gottpoff(%rip), %reg
874 addq foo@gottpoff(%rip), %reg
877 if (offset < 3 || (offset + 4) > sec->size)
878 return FALSE;
880 val = bfd_get_8 (abfd, contents + offset - 3);
881 if (val != 0x48 && val != 0x4c)
882 return FALSE;
884 val = bfd_get_8 (abfd, contents + offset - 2);
885 if (val != 0x8b && val != 0x03)
886 return FALSE;
888 val = bfd_get_8 (abfd, contents + offset - 1);
889 return (val & 0xc7) == 5;
891 case R_X86_64_GOTPC32_TLSDESC:
892 /* Check transition from GDesc access model:
893 leaq x@tlsdesc(%rip), %rax
895 Make sure it's a leaq adding rip to a 32-bit offset
896 into any register, although it's probably almost always
897 going to be rax. */
899 if (offset < 3 || (offset + 4) > sec->size)
900 return FALSE;
902 val = bfd_get_8 (abfd, contents + offset - 3);
903 if ((val & 0xfb) != 0x48)
904 return FALSE;
906 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
907 return FALSE;
909 val = bfd_get_8 (abfd, contents + offset - 1);
910 return (val & 0xc7) == 0x05;
912 case R_X86_64_TLSDESC_CALL:
913 /* Check transition from GDesc access model:
914 call *x@tlsdesc(%rax)
916 if (offset + 2 <= sec->size)
918 /* Make sure that it's a call *x@tlsdesc(%rax). */
919 static x86_64_opcode16 call = { { 0xff, 0x10 } };
920 return bfd_get_16 (abfd, contents + offset) == call.i;
923 return FALSE;
925 default:
926 abort ();
930 /* Return TRUE if the TLS access transition is OK or no transition
931 will be performed. Update R_TYPE if there is a transition. */
933 static bfd_boolean
934 elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
935 asection *sec, bfd_byte *contents,
936 Elf_Internal_Shdr *symtab_hdr,
937 struct elf_link_hash_entry **sym_hashes,
938 unsigned int *r_type, int tls_type,
939 const Elf_Internal_Rela *rel,
940 const Elf_Internal_Rela *relend,
941 struct elf_link_hash_entry *h,
942 unsigned long r_symndx)
944 unsigned int from_type = *r_type;
945 unsigned int to_type = from_type;
946 bfd_boolean check = TRUE;
948 switch (from_type)
950 case R_X86_64_TLSGD:
951 case R_X86_64_GOTPC32_TLSDESC:
952 case R_X86_64_TLSDESC_CALL:
953 case R_X86_64_GOTTPOFF:
954 if (info->executable)
956 if (h == NULL)
957 to_type = R_X86_64_TPOFF32;
958 else
959 to_type = R_X86_64_GOTTPOFF;
962 /* When we are called from elf64_x86_64_relocate_section,
963 CONTENTS isn't NULL and there may be additional transitions
964 based on TLS_TYPE. */
965 if (contents != NULL)
967 unsigned int new_to_type = to_type;
969 if (info->executable
970 && h != NULL
971 && h->dynindx == -1
972 && tls_type == GOT_TLS_IE)
973 new_to_type = R_X86_64_TPOFF32;
975 if (to_type == R_X86_64_TLSGD
976 || to_type == R_X86_64_GOTPC32_TLSDESC
977 || to_type == R_X86_64_TLSDESC_CALL)
979 if (tls_type == GOT_TLS_IE)
980 new_to_type = R_X86_64_GOTTPOFF;
983 /* We checked the transition before when we were called from
984 elf64_x86_64_check_relocs. We only want to check the new
985 transition which hasn't been checked before. */
986 check = new_to_type != to_type && from_type == to_type;
987 to_type = new_to_type;
990 break;
992 case R_X86_64_TLSLD:
993 if (info->executable)
994 to_type = R_X86_64_TPOFF32;
995 break;
997 default:
998 return TRUE;
1001 /* Return TRUE if there is no transition. */
1002 if (from_type == to_type)
1003 return TRUE;
1005 /* Check if the transition can be performed. */
1006 if (check
1007 && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
1008 symtab_hdr, sym_hashes,
1009 from_type, rel, relend))
1011 reloc_howto_type *from, *to;
1012 const char *name;
1014 from = elf64_x86_64_rtype_to_howto (abfd, from_type);
1015 to = elf64_x86_64_rtype_to_howto (abfd, to_type);
1017 if (h)
1018 name = h->root.root.string;
1019 else
1021 Elf_Internal_Sym *isym;
1022 struct elf64_x86_64_link_hash_table *htab;
1023 htab = elf64_x86_64_hash_table (info);
1024 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1025 abfd, r_symndx);
1026 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1029 (*_bfd_error_handler)
1030 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1031 "in section `%A' failed"),
1032 abfd, sec, from->name, to->name, name,
1033 (unsigned long) rel->r_offset);
1034 bfd_set_error (bfd_error_bad_value);
1035 return FALSE;
1038 *r_type = to_type;
1039 return TRUE;
1042 /* Look through the relocs for a section during the first phase, and
1043 calculate needed space in the global offset table, procedure
1044 linkage table, and dynamic reloc sections. */
1046 static bfd_boolean
1047 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1048 asection *sec,
1049 const Elf_Internal_Rela *relocs)
1051 struct elf64_x86_64_link_hash_table *htab;
1052 Elf_Internal_Shdr *symtab_hdr;
1053 struct elf_link_hash_entry **sym_hashes;
1054 const Elf_Internal_Rela *rel;
1055 const Elf_Internal_Rela *rel_end;
1056 asection *sreloc;
1058 if (info->relocatable)
1059 return TRUE;
1061 BFD_ASSERT (is_x86_64_elf (abfd));
1063 htab = elf64_x86_64_hash_table (info);
1064 symtab_hdr = &elf_symtab_hdr (abfd);
1065 sym_hashes = elf_sym_hashes (abfd);
1067 sreloc = NULL;
1069 rel_end = relocs + sec->reloc_count;
1070 for (rel = relocs; rel < rel_end; rel++)
1072 unsigned int r_type;
1073 unsigned long r_symndx;
1074 struct elf_link_hash_entry *h;
1075 Elf_Internal_Sym *isym;
1076 const char *name;
1078 r_symndx = ELF64_R_SYM (rel->r_info);
1079 r_type = ELF64_R_TYPE (rel->r_info);
1081 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1083 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1084 abfd, r_symndx);
1085 return FALSE;
1088 if (r_symndx < symtab_hdr->sh_info)
1090 /* A local symbol. */
1091 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1092 abfd, r_symndx);
1093 if (isym == NULL)
1094 return FALSE;
1096 /* Check relocation against local STT_GNU_IFUNC symbol. */
1097 if (ELF64_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1099 h = elf64_x86_64_get_local_sym_hash (htab, abfd, rel,
1100 TRUE);
1101 if (h == NULL)
1102 return FALSE;
1104 /* Fake a STT_GNU_IFUNC symbol. */
1105 h->type = STT_GNU_IFUNC;
1106 h->def_regular = 1;
1107 h->ref_regular = 1;
1108 h->forced_local = 1;
1109 h->root.type = bfd_link_hash_defined;
1111 else
1112 h = NULL;
1114 else
1116 isym = NULL;
1117 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1118 while (h->root.type == bfd_link_hash_indirect
1119 || h->root.type == bfd_link_hash_warning)
1120 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1123 if (h != NULL)
1125 /* Create the ifunc sections for static executables. If we
1126 never see an indirect function symbol nor we are building
1127 a static executable, those sections will be empty and
1128 won't appear in output. */
1129 switch (r_type)
1131 default:
1132 break;
1134 case R_X86_64_32S:
1135 case R_X86_64_32:
1136 case R_X86_64_64:
1137 case R_X86_64_PC32:
1138 case R_X86_64_PC64:
1139 case R_X86_64_PLT32:
1140 case R_X86_64_GOTPCREL:
1141 case R_X86_64_GOTPCREL64:
1142 if (!_bfd_elf_create_ifunc_sections (abfd, info))
1143 return FALSE;
1144 break;
1147 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1148 it here if it is defined in a non-shared object. */
1149 if (h->type == STT_GNU_IFUNC
1150 && h->def_regular)
1152 /* It is referenced by a non-shared object. */
1153 h->ref_regular = 1;
1154 h->needs_plt = 1;
1156 /* STT_GNU_IFUNC symbol must go through PLT. */
1157 h->plt.refcount += 1;
1159 /* STT_GNU_IFUNC needs dynamic sections. */
1160 if (htab->elf.dynobj == NULL)
1161 htab->elf.dynobj = abfd;
1163 switch (r_type)
1165 default:
1166 if (h->root.root.string)
1167 name = h->root.root.string;
1168 else
1169 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1170 NULL);
1171 (*_bfd_error_handler)
1172 (_("%B: relocation %s against STT_GNU_IFUNC "
1173 "symbol `%s' isn't handled by %s"), abfd,
1174 x86_64_elf_howto_table[r_type].name,
1175 name, __FUNCTION__);
1176 bfd_set_error (bfd_error_bad_value);
1177 return FALSE;
1179 case R_X86_64_64:
1180 h->non_got_ref = 1;
1181 h->pointer_equality_needed = 1;
1182 if (info->shared)
1184 /* We must copy these reloc types into the output
1185 file. Create a reloc section in dynobj and
1186 make room for this reloc. */
1187 sreloc = _bfd_elf_create_ifunc_dyn_reloc
1188 (abfd, info, sec, sreloc,
1189 &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs);
1190 if (sreloc == NULL)
1191 return FALSE;
1193 break;
1195 case R_X86_64_32S:
1196 case R_X86_64_32:
1197 case R_X86_64_PC32:
1198 case R_X86_64_PC64:
1199 h->non_got_ref = 1;
1200 if (r_type != R_X86_64_PC32
1201 && r_type != R_X86_64_PC64)
1202 h->pointer_equality_needed = 1;
1203 break;
1205 case R_X86_64_PLT32:
1206 break;
1208 case R_X86_64_GOTPCREL:
1209 case R_X86_64_GOTPCREL64:
1210 h->got.refcount += 1;
1211 if (htab->elf.sgot == NULL
1212 && !_bfd_elf_create_got_section (htab->elf.dynobj,
1213 info))
1214 return FALSE;
1215 break;
1218 continue;
1222 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1223 symtab_hdr, sym_hashes,
1224 &r_type, GOT_UNKNOWN,
1225 rel, rel_end, h, r_symndx))
1226 return FALSE;
1228 switch (r_type)
1230 case R_X86_64_TLSLD:
1231 htab->tls_ld_got.refcount += 1;
1232 goto create_got;
1234 case R_X86_64_TPOFF32:
1235 if (!info->executable)
1237 if (h)
1238 name = h->root.root.string;
1239 else
1240 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1241 NULL);
1242 (*_bfd_error_handler)
1243 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1244 abfd,
1245 x86_64_elf_howto_table[r_type].name, name);
1246 bfd_set_error (bfd_error_bad_value);
1247 return FALSE;
1249 break;
1251 case R_X86_64_GOTTPOFF:
1252 if (!info->executable)
1253 info->flags |= DF_STATIC_TLS;
1254 /* Fall through */
1256 case R_X86_64_GOT32:
1257 case R_X86_64_GOTPCREL:
1258 case R_X86_64_TLSGD:
1259 case R_X86_64_GOT64:
1260 case R_X86_64_GOTPCREL64:
1261 case R_X86_64_GOTPLT64:
1262 case R_X86_64_GOTPC32_TLSDESC:
1263 case R_X86_64_TLSDESC_CALL:
1264 /* This symbol requires a global offset table entry. */
1266 int tls_type, old_tls_type;
1268 switch (r_type)
1270 default: tls_type = GOT_NORMAL; break;
1271 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1272 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1273 case R_X86_64_GOTPC32_TLSDESC:
1274 case R_X86_64_TLSDESC_CALL:
1275 tls_type = GOT_TLS_GDESC; break;
1278 if (h != NULL)
1280 if (r_type == R_X86_64_GOTPLT64)
1282 /* This relocation indicates that we also need
1283 a PLT entry, as this is a function. We don't need
1284 a PLT entry for local symbols. */
1285 h->needs_plt = 1;
1286 h->plt.refcount += 1;
1288 h->got.refcount += 1;
1289 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1291 else
1293 bfd_signed_vma *local_got_refcounts;
1295 /* This is a global offset table entry for a local symbol. */
1296 local_got_refcounts = elf_local_got_refcounts (abfd);
1297 if (local_got_refcounts == NULL)
1299 bfd_size_type size;
1301 size = symtab_hdr->sh_info;
1302 size *= sizeof (bfd_signed_vma)
1303 + sizeof (bfd_vma) + sizeof (char);
1304 local_got_refcounts = ((bfd_signed_vma *)
1305 bfd_zalloc (abfd, size));
1306 if (local_got_refcounts == NULL)
1307 return FALSE;
1308 elf_local_got_refcounts (abfd) = local_got_refcounts;
1309 elf64_x86_64_local_tlsdesc_gotent (abfd)
1310 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1311 elf64_x86_64_local_got_tls_type (abfd)
1312 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1314 local_got_refcounts[r_symndx] += 1;
1315 old_tls_type
1316 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1319 /* If a TLS symbol is accessed using IE at least once,
1320 there is no point to use dynamic model for it. */
1321 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1322 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1323 || tls_type != GOT_TLS_IE))
1325 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1326 tls_type = old_tls_type;
1327 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1328 && GOT_TLS_GD_ANY_P (tls_type))
1329 tls_type |= old_tls_type;
1330 else
1332 if (h)
1333 name = h->root.root.string;
1334 else
1335 name = bfd_elf_sym_name (abfd, symtab_hdr,
1336 isym, NULL);
1337 (*_bfd_error_handler)
1338 (_("%B: '%s' accessed both as normal and thread local symbol"),
1339 abfd, name);
1340 return FALSE;
1344 if (old_tls_type != tls_type)
1346 if (h != NULL)
1347 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1348 else
1349 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1352 /* Fall through */
1354 case R_X86_64_GOTOFF64:
1355 case R_X86_64_GOTPC32:
1356 case R_X86_64_GOTPC64:
1357 create_got:
1358 if (htab->elf.sgot == NULL)
1360 if (htab->elf.dynobj == NULL)
1361 htab->elf.dynobj = abfd;
1362 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1363 info))
1364 return FALSE;
1366 break;
1368 case R_X86_64_PLT32:
1369 /* This symbol requires a procedure linkage table entry. We
1370 actually build the entry in adjust_dynamic_symbol,
1371 because this might be a case of linking PIC code which is
1372 never referenced by a dynamic object, in which case we
1373 don't need to generate a procedure linkage table entry
1374 after all. */
1376 /* If this is a local symbol, we resolve it directly without
1377 creating a procedure linkage table entry. */
1378 if (h == NULL)
1379 continue;
1381 h->needs_plt = 1;
1382 h->plt.refcount += 1;
1383 break;
1385 case R_X86_64_PLTOFF64:
1386 /* This tries to form the 'address' of a function relative
1387 to GOT. For global symbols we need a PLT entry. */
1388 if (h != NULL)
1390 h->needs_plt = 1;
1391 h->plt.refcount += 1;
1393 goto create_got;
1395 case R_X86_64_8:
1396 case R_X86_64_16:
1397 case R_X86_64_32:
1398 case R_X86_64_32S:
1399 /* Let's help debug shared library creation. These relocs
1400 cannot be used in shared libs. Don't error out for
1401 sections we don't care about, such as debug sections or
1402 non-constant sections. */
1403 if (info->shared
1404 && (sec->flags & SEC_ALLOC) != 0
1405 && (sec->flags & SEC_READONLY) != 0)
1407 if (h)
1408 name = h->root.root.string;
1409 else
1410 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1411 (*_bfd_error_handler)
1412 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1413 abfd, x86_64_elf_howto_table[r_type].name, name);
1414 bfd_set_error (bfd_error_bad_value);
1415 return FALSE;
1417 /* Fall through. */
1419 case R_X86_64_PC8:
1420 case R_X86_64_PC16:
1421 case R_X86_64_PC32:
1422 case R_X86_64_PC64:
1423 case R_X86_64_64:
1424 if (h != NULL && info->executable)
1426 /* If this reloc is in a read-only section, we might
1427 need a copy reloc. We can't check reliably at this
1428 stage whether the section is read-only, as input
1429 sections have not yet been mapped to output sections.
1430 Tentatively set the flag for now, and correct in
1431 adjust_dynamic_symbol. */
1432 h->non_got_ref = 1;
1434 /* We may need a .plt entry if the function this reloc
1435 refers to is in a shared lib. */
1436 h->plt.refcount += 1;
1437 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1438 h->pointer_equality_needed = 1;
1441 /* If we are creating a shared library, and this is a reloc
1442 against a global symbol, or a non PC relative reloc
1443 against a local symbol, then we need to copy the reloc
1444 into the shared library. However, if we are linking with
1445 -Bsymbolic, we do not need to copy a reloc against a
1446 global symbol which is defined in an object we are
1447 including in the link (i.e., DEF_REGULAR is set). At
1448 this point we have not seen all the input files, so it is
1449 possible that DEF_REGULAR is not set now but will be set
1450 later (it is never cleared). In case of a weak definition,
1451 DEF_REGULAR may be cleared later by a strong definition in
1452 a shared library. We account for that possibility below by
1453 storing information in the relocs_copied field of the hash
1454 table entry. A similar situation occurs when creating
1455 shared libraries and symbol visibility changes render the
1456 symbol local.
1458 If on the other hand, we are creating an executable, we
1459 may need to keep relocations for symbols satisfied by a
1460 dynamic library if we manage to avoid copy relocs for the
1461 symbol. */
1462 if ((info->shared
1463 && (sec->flags & SEC_ALLOC) != 0
1464 && (! IS_X86_64_PCREL_TYPE (r_type)
1465 || (h != NULL
1466 && (! SYMBOLIC_BIND (info, h)
1467 || h->root.type == bfd_link_hash_defweak
1468 || !h->def_regular))))
1469 || (ELIMINATE_COPY_RELOCS
1470 && !info->shared
1471 && (sec->flags & SEC_ALLOC) != 0
1472 && h != NULL
1473 && (h->root.type == bfd_link_hash_defweak
1474 || !h->def_regular)))
1476 struct elf_dyn_relocs *p;
1477 struct elf_dyn_relocs **head;
1479 /* We must copy these reloc types into the output file.
1480 Create a reloc section in dynobj and make room for
1481 this reloc. */
1482 if (sreloc == NULL)
1484 if (htab->elf.dynobj == NULL)
1485 htab->elf.dynobj = abfd;
1487 sreloc = _bfd_elf_make_dynamic_reloc_section
1488 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
1490 if (sreloc == NULL)
1491 return FALSE;
1494 /* If this is a global symbol, we count the number of
1495 relocations we need for this symbol. */
1496 if (h != NULL)
1498 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1500 else
1502 /* Track dynamic relocs needed for local syms too.
1503 We really need local syms available to do this
1504 easily. Oh well. */
1505 asection *s;
1506 void **vpp;
1507 Elf_Internal_Sym *isym;
1509 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1510 abfd, r_symndx);
1511 if (isym == NULL)
1512 return FALSE;
1514 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1515 if (s == NULL)
1516 s = sec;
1518 /* Beware of type punned pointers vs strict aliasing
1519 rules. */
1520 vpp = &(elf_section_data (s)->local_dynrel);
1521 head = (struct elf_dyn_relocs **)vpp;
1524 p = *head;
1525 if (p == NULL || p->sec != sec)
1527 bfd_size_type amt = sizeof *p;
1529 p = ((struct elf_dyn_relocs *)
1530 bfd_alloc (htab->elf.dynobj, amt));
1531 if (p == NULL)
1532 return FALSE;
1533 p->next = *head;
1534 *head = p;
1535 p->sec = sec;
1536 p->count = 0;
1537 p->pc_count = 0;
1540 p->count += 1;
1541 if (IS_X86_64_PCREL_TYPE (r_type))
1542 p->pc_count += 1;
1544 break;
1546 /* This relocation describes the C++ object vtable hierarchy.
1547 Reconstruct it for later use during GC. */
1548 case R_X86_64_GNU_VTINHERIT:
1549 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1550 return FALSE;
1551 break;
1553 /* This relocation describes which C++ vtable entries are actually
1554 used. Record for later use during GC. */
1555 case R_X86_64_GNU_VTENTRY:
1556 BFD_ASSERT (h != NULL);
1557 if (h != NULL
1558 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1559 return FALSE;
1560 break;
1562 default:
1563 break;
1567 return TRUE;
1570 /* Return the section that should be marked against GC for a given
1571 relocation. */
1573 static asection *
1574 elf64_x86_64_gc_mark_hook (asection *sec,
1575 struct bfd_link_info *info,
1576 Elf_Internal_Rela *rel,
1577 struct elf_link_hash_entry *h,
1578 Elf_Internal_Sym *sym)
1580 if (h != NULL)
1581 switch (ELF64_R_TYPE (rel->r_info))
1583 case R_X86_64_GNU_VTINHERIT:
1584 case R_X86_64_GNU_VTENTRY:
1585 return NULL;
1588 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1591 /* Update the got entry reference counts for the section being removed. */
1593 static bfd_boolean
1594 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1595 asection *sec,
1596 const Elf_Internal_Rela *relocs)
1598 Elf_Internal_Shdr *symtab_hdr;
1599 struct elf_link_hash_entry **sym_hashes;
1600 bfd_signed_vma *local_got_refcounts;
1601 const Elf_Internal_Rela *rel, *relend;
1603 if (info->relocatable)
1604 return TRUE;
1606 elf_section_data (sec)->local_dynrel = NULL;
1608 symtab_hdr = &elf_symtab_hdr (abfd);
1609 sym_hashes = elf_sym_hashes (abfd);
1610 local_got_refcounts = elf_local_got_refcounts (abfd);
1612 relend = relocs + sec->reloc_count;
1613 for (rel = relocs; rel < relend; rel++)
1615 unsigned long r_symndx;
1616 unsigned int r_type;
1617 struct elf_link_hash_entry *h = NULL;
1619 r_symndx = ELF64_R_SYM (rel->r_info);
1620 if (r_symndx >= symtab_hdr->sh_info)
1622 struct elf64_x86_64_link_hash_entry *eh;
1623 struct elf_dyn_relocs **pp;
1624 struct elf_dyn_relocs *p;
1626 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1627 while (h->root.type == bfd_link_hash_indirect
1628 || h->root.type == bfd_link_hash_warning)
1629 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1630 eh = (struct elf64_x86_64_link_hash_entry *) h;
1632 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1633 if (p->sec == sec)
1635 /* Everything must go for SEC. */
1636 *pp = p->next;
1637 break;
1641 r_type = ELF64_R_TYPE (rel->r_info);
1642 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1643 symtab_hdr, sym_hashes,
1644 &r_type, GOT_UNKNOWN,
1645 rel, relend, h, r_symndx))
1646 return FALSE;
1648 switch (r_type)
1650 case R_X86_64_TLSLD:
1651 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1652 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1653 break;
1655 case R_X86_64_TLSGD:
1656 case R_X86_64_GOTPC32_TLSDESC:
1657 case R_X86_64_TLSDESC_CALL:
1658 case R_X86_64_GOTTPOFF:
1659 case R_X86_64_GOT32:
1660 case R_X86_64_GOTPCREL:
1661 case R_X86_64_GOT64:
1662 case R_X86_64_GOTPCREL64:
1663 case R_X86_64_GOTPLT64:
1664 if (h != NULL)
1666 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1667 h->plt.refcount -= 1;
1668 if (h->got.refcount > 0)
1669 h->got.refcount -= 1;
1671 else if (local_got_refcounts != NULL)
1673 if (local_got_refcounts[r_symndx] > 0)
1674 local_got_refcounts[r_symndx] -= 1;
1676 break;
1678 case R_X86_64_8:
1679 case R_X86_64_16:
1680 case R_X86_64_32:
1681 case R_X86_64_64:
1682 case R_X86_64_32S:
1683 case R_X86_64_PC8:
1684 case R_X86_64_PC16:
1685 case R_X86_64_PC32:
1686 case R_X86_64_PC64:
1687 if (info->shared)
1688 break;
1689 /* Fall thru */
1691 case R_X86_64_PLT32:
1692 case R_X86_64_PLTOFF64:
1693 if (h != NULL)
1695 if (h->plt.refcount > 0)
1696 h->plt.refcount -= 1;
1698 break;
1700 default:
1701 break;
1705 return TRUE;
1708 /* Adjust a symbol defined by a dynamic object and referenced by a
1709 regular object. The current definition is in some section of the
1710 dynamic object, but we're not including those sections. We have to
1711 change the definition to something the rest of the link can
1712 understand. */
1714 static bfd_boolean
1715 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1716 struct elf_link_hash_entry *h)
1718 struct elf64_x86_64_link_hash_table *htab;
1719 asection *s;
1721 /* STT_GNU_IFUNC symbol must go through PLT. */
1722 if (h->type == STT_GNU_IFUNC)
1724 if (h->plt.refcount <= 0)
1726 h->plt.offset = (bfd_vma) -1;
1727 h->needs_plt = 0;
1729 return TRUE;
1732 /* If this is a function, put it in the procedure linkage table. We
1733 will fill in the contents of the procedure linkage table later,
1734 when we know the address of the .got section. */
1735 if (h->type == STT_FUNC
1736 || h->needs_plt)
1738 if (h->plt.refcount <= 0
1739 || SYMBOL_CALLS_LOCAL (info, h)
1740 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1741 && h->root.type == bfd_link_hash_undefweak))
1743 /* This case can occur if we saw a PLT32 reloc in an input
1744 file, but the symbol was never referred to by a dynamic
1745 object, or if all references were garbage collected. In
1746 such a case, we don't actually need to build a procedure
1747 linkage table, and we can just do a PC32 reloc instead. */
1748 h->plt.offset = (bfd_vma) -1;
1749 h->needs_plt = 0;
1752 return TRUE;
1754 else
1755 /* It's possible that we incorrectly decided a .plt reloc was
1756 needed for an R_X86_64_PC32 reloc to a non-function sym in
1757 check_relocs. We can't decide accurately between function and
1758 non-function syms in check-relocs; Objects loaded later in
1759 the link may change h->type. So fix it now. */
1760 h->plt.offset = (bfd_vma) -1;
1762 /* If this is a weak symbol, and there is a real definition, the
1763 processor independent code will have arranged for us to see the
1764 real definition first, and we can just use the same value. */
1765 if (h->u.weakdef != NULL)
1767 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1768 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1769 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1770 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1771 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1772 h->non_got_ref = h->u.weakdef->non_got_ref;
1773 return TRUE;
1776 /* This is a reference to a symbol defined by a dynamic object which
1777 is not a function. */
1779 /* If we are creating a shared library, we must presume that the
1780 only references to the symbol are via the global offset table.
1781 For such cases we need not do anything here; the relocations will
1782 be handled correctly by relocate_section. */
1783 if (info->shared)
1784 return TRUE;
1786 /* If there are no references to this symbol that do not use the
1787 GOT, we don't need to generate a copy reloc. */
1788 if (!h->non_got_ref)
1789 return TRUE;
1791 /* If -z nocopyreloc was given, we won't generate them either. */
1792 if (info->nocopyreloc)
1794 h->non_got_ref = 0;
1795 return TRUE;
1798 if (ELIMINATE_COPY_RELOCS)
1800 struct elf64_x86_64_link_hash_entry * eh;
1801 struct elf_dyn_relocs *p;
1803 eh = (struct elf64_x86_64_link_hash_entry *) h;
1804 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1806 s = p->sec->output_section;
1807 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1808 break;
1811 /* If we didn't find any dynamic relocs in read-only sections, then
1812 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1813 if (p == NULL)
1815 h->non_got_ref = 0;
1816 return TRUE;
1820 if (h->size == 0)
1822 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1823 h->root.root.string);
1824 return TRUE;
1827 /* We must allocate the symbol in our .dynbss section, which will
1828 become part of the .bss section of the executable. There will be
1829 an entry for this symbol in the .dynsym section. The dynamic
1830 object will contain position independent code, so all references
1831 from the dynamic object to this symbol will go through the global
1832 offset table. The dynamic linker will use the .dynsym entry to
1833 determine the address it must put in the global offset table, so
1834 both the dynamic object and the regular object will refer to the
1835 same memory location for the variable. */
1837 htab = elf64_x86_64_hash_table (info);
1839 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1840 to copy the initial value out of the dynamic object and into the
1841 runtime process image. */
1842 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1844 htab->srelbss->size += sizeof (Elf64_External_Rela);
1845 h->needs_copy = 1;
1848 s = htab->sdynbss;
1850 return _bfd_elf_adjust_dynamic_copy (h, s);
1853 /* Allocate space in .plt, .got and associated reloc sections for
1854 dynamic relocs. */
1856 static bfd_boolean
1857 elf64_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1859 struct bfd_link_info *info;
1860 struct elf64_x86_64_link_hash_table *htab;
1861 struct elf64_x86_64_link_hash_entry *eh;
1862 struct elf_dyn_relocs *p;
1864 if (h->root.type == bfd_link_hash_indirect)
1865 return TRUE;
1867 if (h->root.type == bfd_link_hash_warning)
1868 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1869 eh = (struct elf64_x86_64_link_hash_entry *) h;
1871 info = (struct bfd_link_info *) inf;
1872 htab = elf64_x86_64_hash_table (info);
1874 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1875 here if it is defined and referenced in a non-shared object. */
1876 if (h->type == STT_GNU_IFUNC
1877 && h->def_regular)
1878 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
1879 &eh->dyn_relocs,
1880 PLT_ENTRY_SIZE,
1881 GOT_ENTRY_SIZE);
1882 else if (htab->elf.dynamic_sections_created
1883 && h->plt.refcount > 0)
1885 /* Make sure this symbol is output as a dynamic symbol.
1886 Undefined weak syms won't yet be marked as dynamic. */
1887 if (h->dynindx == -1
1888 && !h->forced_local)
1890 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1891 return FALSE;
1894 if (info->shared
1895 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1897 asection *s = htab->elf.splt;
1899 /* If this is the first .plt entry, make room for the special
1900 first entry. */
1901 if (s->size == 0)
1902 s->size += PLT_ENTRY_SIZE;
1904 h->plt.offset = s->size;
1906 /* If this symbol is not defined in a regular file, and we are
1907 not generating a shared library, then set the symbol to this
1908 location in the .plt. This is required to make function
1909 pointers compare as equal between the normal executable and
1910 the shared library. */
1911 if (! info->shared
1912 && !h->def_regular)
1914 h->root.u.def.section = s;
1915 h->root.u.def.value = h->plt.offset;
1918 /* Make room for this entry. */
1919 s->size += PLT_ENTRY_SIZE;
1921 /* We also need to make an entry in the .got.plt section, which
1922 will be placed in the .got section by the linker script. */
1923 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
1925 /* We also need to make an entry in the .rela.plt section. */
1926 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
1927 htab->elf.srelplt->reloc_count++;
1929 else
1931 h->plt.offset = (bfd_vma) -1;
1932 h->needs_plt = 0;
1935 else
1937 h->plt.offset = (bfd_vma) -1;
1938 h->needs_plt = 0;
1941 eh->tlsdesc_got = (bfd_vma) -1;
1943 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1944 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1945 if (h->got.refcount > 0
1946 && info->executable
1947 && h->dynindx == -1
1948 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1950 h->got.offset = (bfd_vma) -1;
1952 else if (h->got.refcount > 0)
1954 asection *s;
1955 bfd_boolean dyn;
1956 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1958 /* Make sure this symbol is output as a dynamic symbol.
1959 Undefined weak syms won't yet be marked as dynamic. */
1960 if (h->dynindx == -1
1961 && !h->forced_local)
1963 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1964 return FALSE;
1967 if (GOT_TLS_GDESC_P (tls_type))
1969 eh->tlsdesc_got = htab->elf.sgotplt->size
1970 - elf64_x86_64_compute_jump_table_size (htab);
1971 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
1972 h->got.offset = (bfd_vma) -2;
1974 if (! GOT_TLS_GDESC_P (tls_type)
1975 || GOT_TLS_GD_P (tls_type))
1977 s = htab->elf.sgot;
1978 h->got.offset = s->size;
1979 s->size += GOT_ENTRY_SIZE;
1980 if (GOT_TLS_GD_P (tls_type))
1981 s->size += GOT_ENTRY_SIZE;
1983 dyn = htab->elf.dynamic_sections_created;
1984 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1985 and two if global.
1986 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1987 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1988 || tls_type == GOT_TLS_IE)
1989 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1990 else if (GOT_TLS_GD_P (tls_type))
1991 htab->elf.srelgot->size += 2 * sizeof (Elf64_External_Rela);
1992 else if (! GOT_TLS_GDESC_P (tls_type)
1993 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1994 || h->root.type != bfd_link_hash_undefweak)
1995 && (info->shared
1996 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1997 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
1998 if (GOT_TLS_GDESC_P (tls_type))
2000 htab->elf.srelplt->size += sizeof (Elf64_External_Rela);
2001 htab->tlsdesc_plt = (bfd_vma) -1;
2004 else
2005 h->got.offset = (bfd_vma) -1;
2007 if (eh->dyn_relocs == NULL)
2008 return TRUE;
2010 /* In the shared -Bsymbolic case, discard space allocated for
2011 dynamic pc-relative relocs against symbols which turn out to be
2012 defined in regular objects. For the normal shared case, discard
2013 space for pc-relative relocs that have become local due to symbol
2014 visibility changes. */
2016 if (info->shared)
2018 /* Relocs that use pc_count are those that appear on a call
2019 insn, or certain REL relocs that can generated via assembly.
2020 We want calls to protected symbols to resolve directly to the
2021 function rather than going via the plt. If people want
2022 function pointer comparisons to work as expected then they
2023 should avoid writing weird assembly. */
2024 if (SYMBOL_CALLS_LOCAL (info, h))
2026 struct elf_dyn_relocs **pp;
2028 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2030 p->count -= p->pc_count;
2031 p->pc_count = 0;
2032 if (p->count == 0)
2033 *pp = p->next;
2034 else
2035 pp = &p->next;
2039 /* Also discard relocs on undefined weak syms with non-default
2040 visibility. */
2041 if (eh->dyn_relocs != NULL
2042 && h->root.type == bfd_link_hash_undefweak)
2044 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2045 eh->dyn_relocs = NULL;
2047 /* Make sure undefined weak symbols are output as a dynamic
2048 symbol in PIEs. */
2049 else if (h->dynindx == -1
2050 && ! h->forced_local
2051 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2052 return FALSE;
2056 else if (ELIMINATE_COPY_RELOCS)
2058 /* For the non-shared case, discard space for relocs against
2059 symbols which turn out to need copy relocs or are not
2060 dynamic. */
2062 if (!h->non_got_ref
2063 && ((h->def_dynamic
2064 && !h->def_regular)
2065 || (htab->elf.dynamic_sections_created
2066 && (h->root.type == bfd_link_hash_undefweak
2067 || h->root.type == bfd_link_hash_undefined))))
2069 /* Make sure this symbol is output as a dynamic symbol.
2070 Undefined weak syms won't yet be marked as dynamic. */
2071 if (h->dynindx == -1
2072 && ! h->forced_local
2073 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2074 return FALSE;
2076 /* If that succeeded, we know we'll be keeping all the
2077 relocs. */
2078 if (h->dynindx != -1)
2079 goto keep;
2082 eh->dyn_relocs = NULL;
2084 keep: ;
2087 /* Finally, allocate space. */
2088 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2090 asection * sreloc;
2092 sreloc = elf_section_data (p->sec)->sreloc;
2094 BFD_ASSERT (sreloc != NULL);
2096 sreloc->size += p->count * sizeof (Elf64_External_Rela);
2099 return TRUE;
2102 /* Allocate space in .plt, .got and associated reloc sections for
2103 local dynamic relocs. */
2105 static bfd_boolean
2106 elf64_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
2108 struct elf_link_hash_entry *h
2109 = (struct elf_link_hash_entry *) *slot;
2111 if (h->type != STT_GNU_IFUNC
2112 || !h->def_regular
2113 || !h->ref_regular
2114 || !h->forced_local
2115 || h->root.type != bfd_link_hash_defined)
2116 abort ();
2118 return elf64_x86_64_allocate_dynrelocs (h, inf);
2121 /* Find any dynamic relocs that apply to read-only sections. */
2123 static bfd_boolean
2124 elf64_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2126 struct elf64_x86_64_link_hash_entry *eh;
2127 struct elf_dyn_relocs *p;
2129 if (h->root.type == bfd_link_hash_warning)
2130 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2132 eh = (struct elf64_x86_64_link_hash_entry *) h;
2133 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2135 asection *s = p->sec->output_section;
2137 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2139 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2141 info->flags |= DF_TEXTREL;
2143 /* Not an error, just cut short the traversal. */
2144 return FALSE;
2147 return TRUE;
2150 /* Set the sizes of the dynamic sections. */
2152 static bfd_boolean
2153 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2154 struct bfd_link_info *info)
2156 struct elf64_x86_64_link_hash_table *htab;
2157 bfd *dynobj;
2158 asection *s;
2159 bfd_boolean relocs;
2160 bfd *ibfd;
2162 htab = elf64_x86_64_hash_table (info);
2163 dynobj = htab->elf.dynobj;
2164 if (dynobj == NULL)
2165 abort ();
2167 if (htab->elf.dynamic_sections_created)
2169 /* Set the contents of the .interp section to the interpreter. */
2170 if (info->executable)
2172 s = bfd_get_section_by_name (dynobj, ".interp");
2173 if (s == NULL)
2174 abort ();
2175 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2176 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2180 /* Set up .got offsets for local syms, and space for local dynamic
2181 relocs. */
2182 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2184 bfd_signed_vma *local_got;
2185 bfd_signed_vma *end_local_got;
2186 char *local_tls_type;
2187 bfd_vma *local_tlsdesc_gotent;
2188 bfd_size_type locsymcount;
2189 Elf_Internal_Shdr *symtab_hdr;
2190 asection *srel;
2192 if (! is_x86_64_elf (ibfd))
2193 continue;
2195 for (s = ibfd->sections; s != NULL; s = s->next)
2197 struct elf_dyn_relocs *p;
2199 for (p = (struct elf_dyn_relocs *)
2200 (elf_section_data (s)->local_dynrel);
2201 p != NULL;
2202 p = p->next)
2204 if (!bfd_is_abs_section (p->sec)
2205 && bfd_is_abs_section (p->sec->output_section))
2207 /* Input section has been discarded, either because
2208 it is a copy of a linkonce section or due to
2209 linker script /DISCARD/, so we'll be discarding
2210 the relocs too. */
2212 else if (p->count != 0)
2214 srel = elf_section_data (p->sec)->sreloc;
2215 srel->size += p->count * sizeof (Elf64_External_Rela);
2216 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2217 info->flags |= DF_TEXTREL;
2222 local_got = elf_local_got_refcounts (ibfd);
2223 if (!local_got)
2224 continue;
2226 symtab_hdr = &elf_symtab_hdr (ibfd);
2227 locsymcount = symtab_hdr->sh_info;
2228 end_local_got = local_got + locsymcount;
2229 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
2230 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
2231 s = htab->elf.sgot;
2232 srel = htab->elf.srelgot;
2233 for (; local_got < end_local_got;
2234 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2236 *local_tlsdesc_gotent = (bfd_vma) -1;
2237 if (*local_got > 0)
2239 if (GOT_TLS_GDESC_P (*local_tls_type))
2241 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2242 - elf64_x86_64_compute_jump_table_size (htab);
2243 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2244 *local_got = (bfd_vma) -2;
2246 if (! GOT_TLS_GDESC_P (*local_tls_type)
2247 || GOT_TLS_GD_P (*local_tls_type))
2249 *local_got = s->size;
2250 s->size += GOT_ENTRY_SIZE;
2251 if (GOT_TLS_GD_P (*local_tls_type))
2252 s->size += GOT_ENTRY_SIZE;
2254 if (info->shared
2255 || GOT_TLS_GD_ANY_P (*local_tls_type)
2256 || *local_tls_type == GOT_TLS_IE)
2258 if (GOT_TLS_GDESC_P (*local_tls_type))
2260 htab->elf.srelplt->size
2261 += sizeof (Elf64_External_Rela);
2262 htab->tlsdesc_plt = (bfd_vma) -1;
2264 if (! GOT_TLS_GDESC_P (*local_tls_type)
2265 || GOT_TLS_GD_P (*local_tls_type))
2266 srel->size += sizeof (Elf64_External_Rela);
2269 else
2270 *local_got = (bfd_vma) -1;
2274 if (htab->tls_ld_got.refcount > 0)
2276 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2277 relocs. */
2278 htab->tls_ld_got.offset = htab->elf.sgot->size;
2279 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
2280 htab->elf.srelgot->size += sizeof (Elf64_External_Rela);
2282 else
2283 htab->tls_ld_got.offset = -1;
2285 /* Allocate global sym .plt and .got entries, and space for global
2286 sym dynamic relocs. */
2287 elf_link_hash_traverse (&htab->elf, elf64_x86_64_allocate_dynrelocs,
2288 info);
2290 /* Allocate .plt and .got entries, and space for local symbols. */
2291 htab_traverse (htab->loc_hash_table,
2292 elf64_x86_64_allocate_local_dynrelocs,
2293 info);
2295 /* For every jump slot reserved in the sgotplt, reloc_count is
2296 incremented. However, when we reserve space for TLS descriptors,
2297 it's not incremented, so in order to compute the space reserved
2298 for them, it suffices to multiply the reloc count by the jump
2299 slot size. */
2300 if (htab->elf.srelplt)
2301 htab->sgotplt_jump_table_size
2302 = elf64_x86_64_compute_jump_table_size (htab);
2304 if (htab->tlsdesc_plt)
2306 /* If we're not using lazy TLS relocations, don't generate the
2307 PLT and GOT entries they require. */
2308 if ((info->flags & DF_BIND_NOW))
2309 htab->tlsdesc_plt = 0;
2310 else
2312 htab->tlsdesc_got = htab->elf.sgot->size;
2313 htab->elf.sgot->size += GOT_ENTRY_SIZE;
2314 /* Reserve room for the initial entry.
2315 FIXME: we could probably do away with it in this case. */
2316 if (htab->elf.splt->size == 0)
2317 htab->elf.splt->size += PLT_ENTRY_SIZE;
2318 htab->tlsdesc_plt = htab->elf.splt->size;
2319 htab->elf.splt->size += PLT_ENTRY_SIZE;
2323 /* We now have determined the sizes of the various dynamic sections.
2324 Allocate memory for them. */
2325 relocs = FALSE;
2326 for (s = dynobj->sections; s != NULL; s = s->next)
2328 if ((s->flags & SEC_LINKER_CREATED) == 0)
2329 continue;
2331 if (s == htab->elf.splt
2332 || s == htab->elf.sgot
2333 || s == htab->elf.sgotplt
2334 || s == htab->elf.iplt
2335 || s == htab->elf.igotplt
2336 || s == htab->sdynbss)
2338 /* Strip this section if we don't need it; see the
2339 comment below. */
2341 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2343 if (s->size != 0 && s != htab->elf.srelplt)
2344 relocs = TRUE;
2346 /* We use the reloc_count field as a counter if we need
2347 to copy relocs into the output file. */
2348 if (s != htab->elf.srelplt)
2349 s->reloc_count = 0;
2351 else
2353 /* It's not one of our sections, so don't allocate space. */
2354 continue;
2357 if (s->size == 0)
2359 /* If we don't need this section, strip it from the
2360 output file. This is mostly to handle .rela.bss and
2361 .rela.plt. We must create both sections in
2362 create_dynamic_sections, because they must be created
2363 before the linker maps input sections to output
2364 sections. The linker does that before
2365 adjust_dynamic_symbol is called, and it is that
2366 function which decides whether anything needs to go
2367 into these sections. */
2369 s->flags |= SEC_EXCLUDE;
2370 continue;
2373 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2374 continue;
2376 /* Allocate memory for the section contents. We use bfd_zalloc
2377 here in case unused entries are not reclaimed before the
2378 section's contents are written out. This should not happen,
2379 but this way if it does, we get a R_X86_64_NONE reloc instead
2380 of garbage. */
2381 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2382 if (s->contents == NULL)
2383 return FALSE;
2386 if (htab->elf.dynamic_sections_created)
2388 /* Add some entries to the .dynamic section. We fill in the
2389 values later, in elf64_x86_64_finish_dynamic_sections, but we
2390 must add the entries now so that we get the correct size for
2391 the .dynamic section. The DT_DEBUG entry is filled in by the
2392 dynamic linker and used by the debugger. */
2393 #define add_dynamic_entry(TAG, VAL) \
2394 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2396 if (info->executable)
2398 if (!add_dynamic_entry (DT_DEBUG, 0))
2399 return FALSE;
2402 if (htab->elf.splt->size != 0)
2404 if (!add_dynamic_entry (DT_PLTGOT, 0)
2405 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2406 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2407 || !add_dynamic_entry (DT_JMPREL, 0))
2408 return FALSE;
2410 if (htab->tlsdesc_plt
2411 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2412 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2413 return FALSE;
2416 if (relocs)
2418 if (!add_dynamic_entry (DT_RELA, 0)
2419 || !add_dynamic_entry (DT_RELASZ, 0)
2420 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2421 return FALSE;
2423 /* If any dynamic relocs apply to a read-only section,
2424 then we need a DT_TEXTREL entry. */
2425 if ((info->flags & DF_TEXTREL) == 0)
2426 elf_link_hash_traverse (&htab->elf,
2427 elf64_x86_64_readonly_dynrelocs,
2428 info);
2430 if ((info->flags & DF_TEXTREL) != 0)
2432 if (!add_dynamic_entry (DT_TEXTREL, 0))
2433 return FALSE;
2437 #undef add_dynamic_entry
2439 return TRUE;
2442 static bfd_boolean
2443 elf64_x86_64_always_size_sections (bfd *output_bfd,
2444 struct bfd_link_info *info)
2446 asection *tls_sec = elf_hash_table (info)->tls_sec;
2448 if (tls_sec)
2450 struct elf_link_hash_entry *tlsbase;
2452 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2453 "_TLS_MODULE_BASE_",
2454 FALSE, FALSE, FALSE);
2456 if (tlsbase && tlsbase->type == STT_TLS)
2458 struct bfd_link_hash_entry *bh = NULL;
2459 const struct elf_backend_data *bed
2460 = get_elf_backend_data (output_bfd);
2462 if (!(_bfd_generic_link_add_one_symbol
2463 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2464 tls_sec, 0, NULL, FALSE,
2465 bed->collect, &bh)))
2466 return FALSE;
2468 elf64_x86_64_hash_table (info)->tls_module_base = bh;
2470 tlsbase = (struct elf_link_hash_entry *)bh;
2471 tlsbase->def_regular = 1;
2472 tlsbase->other = STV_HIDDEN;
2473 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2477 return TRUE;
2480 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2481 executables. Rather than setting it to the beginning of the TLS
2482 section, we have to set it to the end. This function may be called
2483 multiple times, it is idempotent. */
2485 static void
2486 elf64_x86_64_set_tls_module_base (struct bfd_link_info *info)
2488 struct bfd_link_hash_entry *base;
2490 if (!info->executable)
2491 return;
2493 base = elf64_x86_64_hash_table (info)->tls_module_base;
2495 if (!base)
2496 return;
2498 base->u.def.value = elf_hash_table (info)->tls_size;
2501 /* Return the base VMA address which should be subtracted from real addresses
2502 when resolving @dtpoff relocation.
2503 This is PT_TLS segment p_vaddr. */
2505 static bfd_vma
2506 elf64_x86_64_dtpoff_base (struct bfd_link_info *info)
2508 /* If tls_sec is NULL, we should have signalled an error already. */
2509 if (elf_hash_table (info)->tls_sec == NULL)
2510 return 0;
2511 return elf_hash_table (info)->tls_sec->vma;
2514 /* Return the relocation value for @tpoff relocation
2515 if STT_TLS virtual address is ADDRESS. */
2517 static bfd_vma
2518 elf64_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2520 struct elf_link_hash_table *htab = elf_hash_table (info);
2522 /* If tls_segment is NULL, we should have signalled an error already. */
2523 if (htab->tls_sec == NULL)
2524 return 0;
2525 return address - htab->tls_size - htab->tls_sec->vma;
2528 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2529 branch? */
2531 static bfd_boolean
2532 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2534 /* Opcode Instruction
2535 0xe8 call
2536 0xe9 jump
2537 0x0f 0x8x conditional jump */
2538 return ((offset > 0
2539 && (contents [offset - 1] == 0xe8
2540 || contents [offset - 1] == 0xe9))
2541 || (offset > 1
2542 && contents [offset - 2] == 0x0f
2543 && (contents [offset - 1] & 0xf0) == 0x80));
2546 static void
2547 elf64_x86_64_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel)
2549 bfd_byte *loc = s->contents;
2550 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2551 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2552 <= s->contents + s->size);
2553 bfd_elf64_swap_reloca_out (abfd, rel, loc);
2556 /* Relocate an x86_64 ELF section. */
2558 static bfd_boolean
2559 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2560 bfd *input_bfd, asection *input_section,
2561 bfd_byte *contents, Elf_Internal_Rela *relocs,
2562 Elf_Internal_Sym *local_syms,
2563 asection **local_sections)
2565 struct elf64_x86_64_link_hash_table *htab;
2566 Elf_Internal_Shdr *symtab_hdr;
2567 struct elf_link_hash_entry **sym_hashes;
2568 bfd_vma *local_got_offsets;
2569 bfd_vma *local_tlsdesc_gotents;
2570 Elf_Internal_Rela *rel;
2571 Elf_Internal_Rela *relend;
2573 BFD_ASSERT (is_x86_64_elf (input_bfd));
2575 htab = elf64_x86_64_hash_table (info);
2576 symtab_hdr = &elf_symtab_hdr (input_bfd);
2577 sym_hashes = elf_sym_hashes (input_bfd);
2578 local_got_offsets = elf_local_got_offsets (input_bfd);
2579 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2581 elf64_x86_64_set_tls_module_base (info);
2583 rel = relocs;
2584 relend = relocs + input_section->reloc_count;
2585 for (; rel < relend; rel++)
2587 unsigned int r_type;
2588 reloc_howto_type *howto;
2589 unsigned long r_symndx;
2590 struct elf_link_hash_entry *h;
2591 Elf_Internal_Sym *sym;
2592 asection *sec;
2593 bfd_vma off, offplt;
2594 bfd_vma relocation;
2595 bfd_boolean unresolved_reloc;
2596 bfd_reloc_status_type r;
2597 int tls_type;
2598 asection *base_got;
2600 r_type = ELF64_R_TYPE (rel->r_info);
2601 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2602 || r_type == (int) R_X86_64_GNU_VTENTRY)
2603 continue;
2605 if (r_type >= R_X86_64_max)
2607 bfd_set_error (bfd_error_bad_value);
2608 return FALSE;
2611 howto = x86_64_elf_howto_table + r_type;
2612 r_symndx = ELF64_R_SYM (rel->r_info);
2613 h = NULL;
2614 sym = NULL;
2615 sec = NULL;
2616 unresolved_reloc = FALSE;
2617 if (r_symndx < symtab_hdr->sh_info)
2619 sym = local_syms + r_symndx;
2620 sec = local_sections[r_symndx];
2622 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2623 &sec, rel);
2625 /* Relocate against local STT_GNU_IFUNC symbol. */
2626 if (!info->relocatable
2627 && ELF64_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2629 h = elf64_x86_64_get_local_sym_hash (htab, input_bfd,
2630 rel, FALSE);
2631 if (h == NULL)
2632 abort ();
2634 /* Set STT_GNU_IFUNC symbol value. */
2635 h->root.u.def.value = sym->st_value;
2636 h->root.u.def.section = sec;
2639 else
2641 bfd_boolean warned;
2643 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2644 r_symndx, symtab_hdr, sym_hashes,
2645 h, sec, relocation,
2646 unresolved_reloc, warned);
2649 if (sec != NULL && elf_discarded_section (sec))
2651 /* For relocs against symbols from removed linkonce sections,
2652 or sections discarded by a linker script, we just want the
2653 section contents zeroed. Avoid any special processing. */
2654 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2655 rel->r_info = 0;
2656 rel->r_addend = 0;
2657 continue;
2660 if (info->relocatable)
2661 continue;
2663 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2664 it here if it is defined in a non-shared object. */
2665 if (h != NULL
2666 && h->type == STT_GNU_IFUNC
2667 && h->def_regular)
2669 asection *plt;
2670 bfd_vma plt_index;
2671 const char *name;
2673 if ((input_section->flags & SEC_ALLOC) == 0
2674 || h->plt.offset == (bfd_vma) -1)
2675 abort ();
2677 /* STT_GNU_IFUNC symbol must go through PLT. */
2678 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
2679 relocation = (plt->output_section->vma
2680 + plt->output_offset + h->plt.offset);
2682 switch (r_type)
2684 default:
2685 if (h->root.root.string)
2686 name = h->root.root.string;
2687 else
2688 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2689 NULL);
2690 (*_bfd_error_handler)
2691 (_("%B: relocation %s against STT_GNU_IFUNC "
2692 "symbol `%s' isn't handled by %s"), input_bfd,
2693 x86_64_elf_howto_table[r_type].name,
2694 name, __FUNCTION__);
2695 bfd_set_error (bfd_error_bad_value);
2696 return FALSE;
2698 case R_X86_64_32S:
2699 if (info->shared)
2700 abort ();
2701 goto do_relocation;
2703 case R_X86_64_64:
2704 if (rel->r_addend != 0)
2706 if (h->root.root.string)
2707 name = h->root.root.string;
2708 else
2709 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2710 sym, NULL);
2711 (*_bfd_error_handler)
2712 (_("%B: relocation %s against STT_GNU_IFUNC "
2713 "symbol `%s' has non-zero addend: %d"),
2714 input_bfd, x86_64_elf_howto_table[r_type].name,
2715 name, rel->r_addend);
2716 bfd_set_error (bfd_error_bad_value);
2717 return FALSE;
2720 /* Generate dynamic relcoation only when there is a
2721 non-GOF reference in a shared object. */
2722 if (info->shared && h->non_got_ref)
2724 Elf_Internal_Rela outrel;
2725 asection *sreloc;
2727 /* Need a dynamic relocation to get the real function
2728 address. */
2729 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2730 info,
2731 input_section,
2732 rel->r_offset);
2733 if (outrel.r_offset == (bfd_vma) -1
2734 || outrel.r_offset == (bfd_vma) -2)
2735 abort ();
2737 outrel.r_offset += (input_section->output_section->vma
2738 + input_section->output_offset);
2740 if (h->dynindx == -1
2741 || h->forced_local
2742 || info->executable)
2744 /* This symbol is resolved locally. */
2745 outrel.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
2746 outrel.r_addend = (h->root.u.def.value
2747 + h->root.u.def.section->output_section->vma
2748 + h->root.u.def.section->output_offset);
2750 else
2752 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2753 outrel.r_addend = 0;
2756 sreloc = htab->elf.irelifunc;
2757 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
2759 /* If this reloc is against an external symbol, we
2760 do not want to fiddle with the addend. Otherwise,
2761 we need to include the symbol value so that it
2762 becomes an addend for the dynamic reloc. For an
2763 internal symbol, we have updated addend. */
2764 continue;
2767 case R_X86_64_32:
2768 case R_X86_64_PC32:
2769 case R_X86_64_PC64:
2770 case R_X86_64_PLT32:
2771 goto do_relocation;
2773 case R_X86_64_GOTPCREL:
2774 case R_X86_64_GOTPCREL64:
2775 base_got = htab->elf.sgot;
2776 off = h->got.offset;
2778 if (base_got == NULL)
2779 abort ();
2781 if (off == (bfd_vma) -1)
2783 /* We can't use h->got.offset here to save state, or
2784 even just remember the offset, as finish_dynamic_symbol
2785 would use that as offset into .got. */
2787 if (htab->elf.splt != NULL)
2789 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2790 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2791 base_got = htab->elf.sgotplt;
2793 else
2795 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2796 off = plt_index * GOT_ENTRY_SIZE;
2797 base_got = htab->elf.igotplt;
2800 if (h->dynindx == -1
2801 || h->forced_local
2802 || info->symbolic)
2804 /* This references the local defitionion. We must
2805 initialize this entry in the global offset table.
2806 Since the offset must always be a multiple of 8,
2807 we use the least significant bit to record
2808 whether we have initialized it already.
2810 When doing a dynamic link, we create a .rela.got
2811 relocation entry to initialize the value. This
2812 is done in the finish_dynamic_symbol routine. */
2813 if ((off & 1) != 0)
2814 off &= ~1;
2815 else
2817 bfd_put_64 (output_bfd, relocation,
2818 base_got->contents + off);
2819 /* Note that this is harmless for the GOTPLT64
2820 case, as -1 | 1 still is -1. */
2821 h->got.offset |= 1;
2826 relocation = (base_got->output_section->vma
2827 + base_got->output_offset + off);
2829 if (r_type != R_X86_64_GOTPCREL
2830 && r_type != R_X86_64_GOTPCREL64)
2832 asection *gotplt;
2833 if (htab->elf.splt != NULL)
2834 gotplt = htab->elf.sgotplt;
2835 else
2836 gotplt = htab->elf.igotplt;
2837 relocation -= (gotplt->output_section->vma
2838 - gotplt->output_offset);
2841 goto do_relocation;
2845 /* When generating a shared object, the relocations handled here are
2846 copied into the output file to be resolved at run time. */
2847 switch (r_type)
2849 case R_X86_64_GOT32:
2850 case R_X86_64_GOT64:
2851 /* Relocation is to the entry for this symbol in the global
2852 offset table. */
2853 case R_X86_64_GOTPCREL:
2854 case R_X86_64_GOTPCREL64:
2855 /* Use global offset table entry as symbol value. */
2856 case R_X86_64_GOTPLT64:
2857 /* This is the same as GOT64 for relocation purposes, but
2858 indicates the existence of a PLT entry. The difficulty is,
2859 that we must calculate the GOT slot offset from the PLT
2860 offset, if this symbol got a PLT entry (it was global).
2861 Additionally if it's computed from the PLT entry, then that
2862 GOT offset is relative to .got.plt, not to .got. */
2863 base_got = htab->elf.sgot;
2865 if (htab->elf.sgot == NULL)
2866 abort ();
2868 if (h != NULL)
2870 bfd_boolean dyn;
2872 off = h->got.offset;
2873 if (h->needs_plt
2874 && h->plt.offset != (bfd_vma)-1
2875 && off == (bfd_vma)-1)
2877 /* We can't use h->got.offset here to save
2878 state, or even just remember the offset, as
2879 finish_dynamic_symbol would use that as offset into
2880 .got. */
2881 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2882 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2883 base_got = htab->elf.sgotplt;
2886 dyn = htab->elf.dynamic_sections_created;
2888 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2889 || (info->shared
2890 && SYMBOL_REFERENCES_LOCAL (info, h))
2891 || (ELF_ST_VISIBILITY (h->other)
2892 && h->root.type == bfd_link_hash_undefweak))
2894 /* This is actually a static link, or it is a -Bsymbolic
2895 link and the symbol is defined locally, or the symbol
2896 was forced to be local because of a version file. We
2897 must initialize this entry in the global offset table.
2898 Since the offset must always be a multiple of 8, we
2899 use the least significant bit to record whether we
2900 have initialized it already.
2902 When doing a dynamic link, we create a .rela.got
2903 relocation entry to initialize the value. This is
2904 done in the finish_dynamic_symbol routine. */
2905 if ((off & 1) != 0)
2906 off &= ~1;
2907 else
2909 bfd_put_64 (output_bfd, relocation,
2910 base_got->contents + off);
2911 /* Note that this is harmless for the GOTPLT64 case,
2912 as -1 | 1 still is -1. */
2913 h->got.offset |= 1;
2916 else
2917 unresolved_reloc = FALSE;
2919 else
2921 if (local_got_offsets == NULL)
2922 abort ();
2924 off = local_got_offsets[r_symndx];
2926 /* The offset must always be a multiple of 8. We use
2927 the least significant bit to record whether we have
2928 already generated the necessary reloc. */
2929 if ((off & 1) != 0)
2930 off &= ~1;
2931 else
2933 bfd_put_64 (output_bfd, relocation,
2934 base_got->contents + off);
2936 if (info->shared)
2938 asection *s;
2939 Elf_Internal_Rela outrel;
2941 /* We need to generate a R_X86_64_RELATIVE reloc
2942 for the dynamic linker. */
2943 s = htab->elf.srelgot;
2944 if (s == NULL)
2945 abort ();
2947 outrel.r_offset = (base_got->output_section->vma
2948 + base_got->output_offset
2949 + off);
2950 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2951 outrel.r_addend = relocation;
2952 elf64_x86_64_append_rela (output_bfd, s, &outrel);
2955 local_got_offsets[r_symndx] |= 1;
2959 if (off >= (bfd_vma) -2)
2960 abort ();
2962 relocation = base_got->output_section->vma
2963 + base_got->output_offset + off;
2964 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
2965 relocation -= htab->elf.sgotplt->output_section->vma
2966 - htab->elf.sgotplt->output_offset;
2968 break;
2970 case R_X86_64_GOTOFF64:
2971 /* Relocation is relative to the start of the global offset
2972 table. */
2974 /* Check to make sure it isn't a protected function symbol
2975 for shared library since it may not be local when used
2976 as function address. */
2977 if (info->shared
2978 && h
2979 && h->def_regular
2980 && h->type == STT_FUNC
2981 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2983 (*_bfd_error_handler)
2984 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2985 input_bfd, h->root.root.string);
2986 bfd_set_error (bfd_error_bad_value);
2987 return FALSE;
2990 /* Note that sgot is not involved in this
2991 calculation. We always want the start of .got.plt. If we
2992 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2993 permitted by the ABI, we might have to change this
2994 calculation. */
2995 relocation -= htab->elf.sgotplt->output_section->vma
2996 + htab->elf.sgotplt->output_offset;
2997 break;
2999 case R_X86_64_GOTPC32:
3000 case R_X86_64_GOTPC64:
3001 /* Use global offset table as symbol value. */
3002 relocation = htab->elf.sgotplt->output_section->vma
3003 + htab->elf.sgotplt->output_offset;
3004 unresolved_reloc = FALSE;
3005 break;
3007 case R_X86_64_PLTOFF64:
3008 /* Relocation is PLT entry relative to GOT. For local
3009 symbols it's the symbol itself relative to GOT. */
3010 if (h != NULL
3011 /* See PLT32 handling. */
3012 && h->plt.offset != (bfd_vma) -1
3013 && htab->elf.splt != NULL)
3015 relocation = (htab->elf.splt->output_section->vma
3016 + htab->elf.splt->output_offset
3017 + h->plt.offset);
3018 unresolved_reloc = FALSE;
3021 relocation -= htab->elf.sgotplt->output_section->vma
3022 + htab->elf.sgotplt->output_offset;
3023 break;
3025 case R_X86_64_PLT32:
3026 /* Relocation is to the entry for this symbol in the
3027 procedure linkage table. */
3029 /* Resolve a PLT32 reloc against a local symbol directly,
3030 without using the procedure linkage table. */
3031 if (h == NULL)
3032 break;
3034 if (h->plt.offset == (bfd_vma) -1
3035 || htab->elf.splt == NULL)
3037 /* We didn't make a PLT entry for this symbol. This
3038 happens when statically linking PIC code, or when
3039 using -Bsymbolic. */
3040 break;
3043 relocation = (htab->elf.splt->output_section->vma
3044 + htab->elf.splt->output_offset
3045 + h->plt.offset);
3046 unresolved_reloc = FALSE;
3047 break;
3049 case R_X86_64_PC8:
3050 case R_X86_64_PC16:
3051 case R_X86_64_PC32:
3052 if (info->shared
3053 && (input_section->flags & SEC_ALLOC) != 0
3054 && (input_section->flags & SEC_READONLY) != 0
3055 && h != NULL)
3057 bfd_boolean fail = FALSE;
3058 bfd_boolean branch
3059 = (r_type == R_X86_64_PC32
3060 && is_32bit_relative_branch (contents, rel->r_offset));
3062 if (SYMBOL_REFERENCES_LOCAL (info, h))
3064 /* Symbol is referenced locally. Make sure it is
3065 defined locally or for a branch. */
3066 fail = !h->def_regular && !branch;
3068 else
3070 /* Symbol isn't referenced locally. We only allow
3071 branch to symbol with non-default visibility. */
3072 fail = (!branch
3073 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3076 if (fail)
3078 const char *fmt;
3079 const char *v;
3080 const char *pic = "";
3082 switch (ELF_ST_VISIBILITY (h->other))
3084 case STV_HIDDEN:
3085 v = _("hidden symbol");
3086 break;
3087 case STV_INTERNAL:
3088 v = _("internal symbol");
3089 break;
3090 case STV_PROTECTED:
3091 v = _("protected symbol");
3092 break;
3093 default:
3094 v = _("symbol");
3095 pic = _("; recompile with -fPIC");
3096 break;
3099 if (h->def_regular)
3100 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3101 else
3102 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3104 (*_bfd_error_handler) (fmt, input_bfd,
3105 x86_64_elf_howto_table[r_type].name,
3106 v, h->root.root.string, pic);
3107 bfd_set_error (bfd_error_bad_value);
3108 return FALSE;
3111 /* Fall through. */
3113 case R_X86_64_8:
3114 case R_X86_64_16:
3115 case R_X86_64_32:
3116 case R_X86_64_PC64:
3117 case R_X86_64_64:
3118 /* FIXME: The ABI says the linker should make sure the value is
3119 the same when it's zeroextended to 64 bit. */
3121 if ((input_section->flags & SEC_ALLOC) == 0)
3122 break;
3124 if ((info->shared
3125 && (h == NULL
3126 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3127 || h->root.type != bfd_link_hash_undefweak)
3128 && (! IS_X86_64_PCREL_TYPE (r_type)
3129 || ! SYMBOL_CALLS_LOCAL (info, h)))
3130 || (ELIMINATE_COPY_RELOCS
3131 && !info->shared
3132 && h != NULL
3133 && h->dynindx != -1
3134 && !h->non_got_ref
3135 && ((h->def_dynamic
3136 && !h->def_regular)
3137 || h->root.type == bfd_link_hash_undefweak
3138 || h->root.type == bfd_link_hash_undefined)))
3140 Elf_Internal_Rela outrel;
3141 bfd_boolean skip, relocate;
3142 asection *sreloc;
3144 /* When generating a shared object, these relocations
3145 are copied into the output file to be resolved at run
3146 time. */
3147 skip = FALSE;
3148 relocate = FALSE;
3150 outrel.r_offset =
3151 _bfd_elf_section_offset (output_bfd, info, input_section,
3152 rel->r_offset);
3153 if (outrel.r_offset == (bfd_vma) -1)
3154 skip = TRUE;
3155 else if (outrel.r_offset == (bfd_vma) -2)
3156 skip = TRUE, relocate = TRUE;
3158 outrel.r_offset += (input_section->output_section->vma
3159 + input_section->output_offset);
3161 if (skip)
3162 memset (&outrel, 0, sizeof outrel);
3164 /* h->dynindx may be -1 if this symbol was marked to
3165 become local. */
3166 else if (h != NULL
3167 && h->dynindx != -1
3168 && (IS_X86_64_PCREL_TYPE (r_type)
3169 || ! info->shared
3170 || ! SYMBOLIC_BIND (info, h)
3171 || ! h->def_regular))
3173 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3174 outrel.r_addend = rel->r_addend;
3176 else
3178 /* This symbol is local, or marked to become local. */
3179 if (r_type == R_X86_64_64)
3181 relocate = TRUE;
3182 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3183 outrel.r_addend = relocation + rel->r_addend;
3185 else
3187 long sindx;
3189 if (bfd_is_abs_section (sec))
3190 sindx = 0;
3191 else if (sec == NULL || sec->owner == NULL)
3193 bfd_set_error (bfd_error_bad_value);
3194 return FALSE;
3196 else
3198 asection *osec;
3200 /* We are turning this relocation into one
3201 against a section symbol. It would be
3202 proper to subtract the symbol's value,
3203 osec->vma, from the emitted reloc addend,
3204 but ld.so expects buggy relocs. */
3205 osec = sec->output_section;
3206 sindx = elf_section_data (osec)->dynindx;
3207 if (sindx == 0)
3209 asection *oi = htab->elf.text_index_section;
3210 sindx = elf_section_data (oi)->dynindx;
3212 BFD_ASSERT (sindx != 0);
3215 outrel.r_info = ELF64_R_INFO (sindx, r_type);
3216 outrel.r_addend = relocation + rel->r_addend;
3220 sreloc = elf_section_data (input_section)->sreloc;
3222 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3224 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3226 /* If this reloc is against an external symbol, we do
3227 not want to fiddle with the addend. Otherwise, we
3228 need to include the symbol value so that it becomes
3229 an addend for the dynamic reloc. */
3230 if (! relocate)
3231 continue;
3234 break;
3236 case R_X86_64_TLSGD:
3237 case R_X86_64_GOTPC32_TLSDESC:
3238 case R_X86_64_TLSDESC_CALL:
3239 case R_X86_64_GOTTPOFF:
3240 tls_type = GOT_UNKNOWN;
3241 if (h == NULL && local_got_offsets)
3242 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3243 else if (h != NULL)
3244 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
3246 if (! elf64_x86_64_tls_transition (info, input_bfd,
3247 input_section, contents,
3248 symtab_hdr, sym_hashes,
3249 &r_type, tls_type, rel,
3250 relend, h, r_symndx))
3251 return FALSE;
3253 if (r_type == R_X86_64_TPOFF32)
3255 bfd_vma roff = rel->r_offset;
3257 BFD_ASSERT (! unresolved_reloc);
3259 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3261 /* GD->LE transition.
3262 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3263 .word 0x6666; rex64; call __tls_get_addr
3264 Change it into:
3265 movq %fs:0, %rax
3266 leaq foo@tpoff(%rax), %rax */
3267 memcpy (contents + roff - 4,
3268 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3269 16);
3270 bfd_put_32 (output_bfd,
3271 elf64_x86_64_tpoff (info, relocation),
3272 contents + roff + 8);
3273 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3274 rel++;
3275 continue;
3277 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3279 /* GDesc -> LE transition.
3280 It's originally something like:
3281 leaq x@tlsdesc(%rip), %rax
3283 Change it to:
3284 movl $x@tpoff, %rax
3287 unsigned int val, type, type2;
3289 type = bfd_get_8 (input_bfd, contents + roff - 3);
3290 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3291 val = bfd_get_8 (input_bfd, contents + roff - 1);
3292 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3293 contents + roff - 3);
3294 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3295 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3296 contents + roff - 1);
3297 bfd_put_32 (output_bfd,
3298 elf64_x86_64_tpoff (info, relocation),
3299 contents + roff);
3300 continue;
3302 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3304 /* GDesc -> LE transition.
3305 It's originally:
3306 call *(%rax)
3307 Turn it into:
3308 xchg %ax,%ax. */
3309 bfd_put_8 (output_bfd, 0x66, contents + roff);
3310 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3311 continue;
3313 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3315 /* IE->LE transition:
3316 Originally it can be one of:
3317 movq foo@gottpoff(%rip), %reg
3318 addq foo@gottpoff(%rip), %reg
3319 We change it into:
3320 movq $foo, %reg
3321 leaq foo(%reg), %reg
3322 addq $foo, %reg. */
3324 unsigned int val, type, reg;
3326 val = bfd_get_8 (input_bfd, contents + roff - 3);
3327 type = bfd_get_8 (input_bfd, contents + roff - 2);
3328 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3329 reg >>= 3;
3330 if (type == 0x8b)
3332 /* movq */
3333 if (val == 0x4c)
3334 bfd_put_8 (output_bfd, 0x49,
3335 contents + roff - 3);
3336 bfd_put_8 (output_bfd, 0xc7,
3337 contents + roff - 2);
3338 bfd_put_8 (output_bfd, 0xc0 | reg,
3339 contents + roff - 1);
3341 else if (reg == 4)
3343 /* addq -> addq - addressing with %rsp/%r12 is
3344 special */
3345 if (val == 0x4c)
3346 bfd_put_8 (output_bfd, 0x49,
3347 contents + roff - 3);
3348 bfd_put_8 (output_bfd, 0x81,
3349 contents + roff - 2);
3350 bfd_put_8 (output_bfd, 0xc0 | reg,
3351 contents + roff - 1);
3353 else
3355 /* addq -> leaq */
3356 if (val == 0x4c)
3357 bfd_put_8 (output_bfd, 0x4d,
3358 contents + roff - 3);
3359 bfd_put_8 (output_bfd, 0x8d,
3360 contents + roff - 2);
3361 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3362 contents + roff - 1);
3364 bfd_put_32 (output_bfd,
3365 elf64_x86_64_tpoff (info, relocation),
3366 contents + roff);
3367 continue;
3369 else
3370 BFD_ASSERT (FALSE);
3373 if (htab->elf.sgot == NULL)
3374 abort ();
3376 if (h != NULL)
3378 off = h->got.offset;
3379 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
3381 else
3383 if (local_got_offsets == NULL)
3384 abort ();
3386 off = local_got_offsets[r_symndx];
3387 offplt = local_tlsdesc_gotents[r_symndx];
3390 if ((off & 1) != 0)
3391 off &= ~1;
3392 else
3394 Elf_Internal_Rela outrel;
3395 int dr_type, indx;
3396 asection *sreloc;
3398 if (htab->elf.srelgot == NULL)
3399 abort ();
3401 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3403 if (GOT_TLS_GDESC_P (tls_type))
3405 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
3406 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3407 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3408 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3409 + htab->elf.sgotplt->output_offset
3410 + offplt
3411 + htab->sgotplt_jump_table_size);
3412 sreloc = htab->elf.srelplt;
3413 if (indx == 0)
3414 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3415 else
3416 outrel.r_addend = 0;
3417 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3420 sreloc = htab->elf.srelgot;
3422 outrel.r_offset = (htab->elf.sgot->output_section->vma
3423 + htab->elf.sgot->output_offset + off);
3425 if (GOT_TLS_GD_P (tls_type))
3426 dr_type = R_X86_64_DTPMOD64;
3427 else if (GOT_TLS_GDESC_P (tls_type))
3428 goto dr_done;
3429 else
3430 dr_type = R_X86_64_TPOFF64;
3432 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3433 outrel.r_addend = 0;
3434 if ((dr_type == R_X86_64_TPOFF64
3435 || dr_type == R_X86_64_TLSDESC) && indx == 0)
3436 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3437 outrel.r_info = ELF64_R_INFO (indx, dr_type);
3439 elf64_x86_64_append_rela (output_bfd, sreloc, &outrel);
3441 if (GOT_TLS_GD_P (tls_type))
3443 if (indx == 0)
3445 BFD_ASSERT (! unresolved_reloc);
3446 bfd_put_64 (output_bfd,
3447 relocation - elf64_x86_64_dtpoff_base (info),
3448 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3450 else
3452 bfd_put_64 (output_bfd, 0,
3453 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3454 outrel.r_info = ELF64_R_INFO (indx,
3455 R_X86_64_DTPOFF64);
3456 outrel.r_offset += GOT_ENTRY_SIZE;
3457 elf64_x86_64_append_rela (output_bfd, sreloc,
3458 &outrel);
3462 dr_done:
3463 if (h != NULL)
3464 h->got.offset |= 1;
3465 else
3466 local_got_offsets[r_symndx] |= 1;
3469 if (off >= (bfd_vma) -2
3470 && ! GOT_TLS_GDESC_P (tls_type))
3471 abort ();
3472 if (r_type == ELF64_R_TYPE (rel->r_info))
3474 if (r_type == R_X86_64_GOTPC32_TLSDESC
3475 || r_type == R_X86_64_TLSDESC_CALL)
3476 relocation = htab->elf.sgotplt->output_section->vma
3477 + htab->elf.sgotplt->output_offset
3478 + offplt + htab->sgotplt_jump_table_size;
3479 else
3480 relocation = htab->elf.sgot->output_section->vma
3481 + htab->elf.sgot->output_offset + off;
3482 unresolved_reloc = FALSE;
3484 else
3486 bfd_vma roff = rel->r_offset;
3488 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3490 /* GD->IE transition.
3491 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3492 .word 0x6666; rex64; call __tls_get_addr@plt
3493 Change it into:
3494 movq %fs:0, %rax
3495 addq foo@gottpoff(%rip), %rax */
3496 memcpy (contents + roff - 4,
3497 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3498 16);
3500 relocation = (htab->elf.sgot->output_section->vma
3501 + htab->elf.sgot->output_offset + off
3502 - roff
3503 - input_section->output_section->vma
3504 - input_section->output_offset
3505 - 12);
3506 bfd_put_32 (output_bfd, relocation,
3507 contents + roff + 8);
3508 /* Skip R_X86_64_PLT32. */
3509 rel++;
3510 continue;
3512 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3514 /* GDesc -> IE transition.
3515 It's originally something like:
3516 leaq x@tlsdesc(%rip), %rax
3518 Change it to:
3519 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3522 unsigned int val, type, type2;
3524 type = bfd_get_8 (input_bfd, contents + roff - 3);
3525 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3526 val = bfd_get_8 (input_bfd, contents + roff - 1);
3528 /* Now modify the instruction as appropriate. To
3529 turn a leaq into a movq in the form we use it, it
3530 suffices to change the second byte from 0x8d to
3531 0x8b. */
3532 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3534 bfd_put_32 (output_bfd,
3535 htab->elf.sgot->output_section->vma
3536 + htab->elf.sgot->output_offset + off
3537 - rel->r_offset
3538 - input_section->output_section->vma
3539 - input_section->output_offset
3540 - 4,
3541 contents + roff);
3542 continue;
3544 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3546 /* GDesc -> IE transition.
3547 It's originally:
3548 call *(%rax)
3550 Change it to:
3551 xchg %ax,%ax. */
3553 unsigned int val, type;
3555 type = bfd_get_8 (input_bfd, contents + roff);
3556 val = bfd_get_8 (input_bfd, contents + roff + 1);
3557 bfd_put_8 (output_bfd, 0x66, contents + roff);
3558 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3559 continue;
3561 else
3562 BFD_ASSERT (FALSE);
3564 break;
3566 case R_X86_64_TLSLD:
3567 if (! elf64_x86_64_tls_transition (info, input_bfd,
3568 input_section, contents,
3569 symtab_hdr, sym_hashes,
3570 &r_type, GOT_UNKNOWN,
3571 rel, relend, h, r_symndx))
3572 return FALSE;
3574 if (r_type != R_X86_64_TLSLD)
3576 /* LD->LE transition:
3577 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3578 We change it into:
3579 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3581 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3582 memcpy (contents + rel->r_offset - 3,
3583 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3584 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3585 rel++;
3586 continue;
3589 if (htab->elf.sgot == NULL)
3590 abort ();
3592 off = htab->tls_ld_got.offset;
3593 if (off & 1)
3594 off &= ~1;
3595 else
3597 Elf_Internal_Rela outrel;
3599 if (htab->elf.srelgot == NULL)
3600 abort ();
3602 outrel.r_offset = (htab->elf.sgot->output_section->vma
3603 + htab->elf.sgot->output_offset + off);
3605 bfd_put_64 (output_bfd, 0,
3606 htab->elf.sgot->contents + off);
3607 bfd_put_64 (output_bfd, 0,
3608 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3609 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3610 outrel.r_addend = 0;
3611 elf64_x86_64_append_rela (output_bfd, htab->elf.srelgot,
3612 &outrel);
3613 htab->tls_ld_got.offset |= 1;
3615 relocation = htab->elf.sgot->output_section->vma
3616 + htab->elf.sgot->output_offset + off;
3617 unresolved_reloc = FALSE;
3618 break;
3620 case R_X86_64_DTPOFF32:
3621 if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
3622 relocation -= elf64_x86_64_dtpoff_base (info);
3623 else
3624 relocation = elf64_x86_64_tpoff (info, relocation);
3625 break;
3627 case R_X86_64_TPOFF32:
3628 BFD_ASSERT (info->executable);
3629 relocation = elf64_x86_64_tpoff (info, relocation);
3630 break;
3632 default:
3633 break;
3636 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3637 because such sections are not SEC_ALLOC and thus ld.so will
3638 not process them. */
3639 if (unresolved_reloc
3640 && !((input_section->flags & SEC_DEBUGGING) != 0
3641 && h->def_dynamic))
3642 (*_bfd_error_handler)
3643 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3644 input_bfd,
3645 input_section,
3646 (long) rel->r_offset,
3647 howto->name,
3648 h->root.root.string);
3650 do_relocation:
3651 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3652 contents, rel->r_offset,
3653 relocation, rel->r_addend);
3655 if (r != bfd_reloc_ok)
3657 const char *name;
3659 if (h != NULL)
3660 name = h->root.root.string;
3661 else
3663 name = bfd_elf_string_from_elf_section (input_bfd,
3664 symtab_hdr->sh_link,
3665 sym->st_name);
3666 if (name == NULL)
3667 return FALSE;
3668 if (*name == '\0')
3669 name = bfd_section_name (input_bfd, sec);
3672 if (r == bfd_reloc_overflow)
3674 if (! ((*info->callbacks->reloc_overflow)
3675 (info, (h ? &h->root : NULL), name, howto->name,
3676 (bfd_vma) 0, input_bfd, input_section,
3677 rel->r_offset)))
3678 return FALSE;
3680 else
3682 (*_bfd_error_handler)
3683 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3684 input_bfd, input_section,
3685 (long) rel->r_offset, name, (int) r);
3686 return FALSE;
3691 return TRUE;
3694 /* Finish up dynamic symbol handling. We set the contents of various
3695 dynamic sections here. */
3697 static bfd_boolean
3698 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3699 struct bfd_link_info *info,
3700 struct elf_link_hash_entry *h,
3701 Elf_Internal_Sym *sym)
3703 struct elf64_x86_64_link_hash_table *htab;
3705 htab = elf64_x86_64_hash_table (info);
3707 if (h->plt.offset != (bfd_vma) -1)
3709 bfd_vma plt_index;
3710 bfd_vma got_offset;
3711 Elf_Internal_Rela rela;
3712 bfd_byte *loc;
3713 asection *plt, *gotplt, *relplt;
3715 /* When building a static executable, use .iplt, .igot.plt and
3716 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3717 if (htab->elf.splt != NULL)
3719 plt = htab->elf.splt;
3720 gotplt = htab->elf.sgotplt;
3721 relplt = htab->elf.srelplt;
3723 else
3725 plt = htab->elf.iplt;
3726 gotplt = htab->elf.igotplt;
3727 relplt = htab->elf.irelplt;
3730 /* This symbol has an entry in the procedure linkage table. Set
3731 it up. */
3732 if ((h->dynindx == -1
3733 && !((h->forced_local || info->executable)
3734 && h->def_regular
3735 && h->type == STT_GNU_IFUNC))
3736 || plt == NULL
3737 || gotplt == NULL
3738 || relplt == NULL)
3739 abort ();
3741 /* Get the index in the procedure linkage table which
3742 corresponds to this symbol. This is the index of this symbol
3743 in all the symbols for which we are making plt entries. The
3744 first entry in the procedure linkage table is reserved.
3746 Get the offset into the .got table of the entry that
3747 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3748 bytes. The first three are reserved for the dynamic linker.
3750 For static executables, we don't reserve anything. */
3752 if (plt == htab->elf.splt)
3754 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3755 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3757 else
3759 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3760 got_offset = plt_index * GOT_ENTRY_SIZE;
3763 /* Fill in the entry in the procedure linkage table. */
3764 memcpy (plt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3765 PLT_ENTRY_SIZE);
3767 /* Insert the relocation positions of the plt section. The magic
3768 numbers at the end of the statements are the positions of the
3769 relocations in the plt section. */
3770 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3771 instruction uses 6 bytes, subtract this value. */
3772 bfd_put_32 (output_bfd,
3773 (gotplt->output_section->vma
3774 + gotplt->output_offset
3775 + got_offset
3776 - plt->output_section->vma
3777 - plt->output_offset
3778 - h->plt.offset
3779 - 6),
3780 plt->contents + h->plt.offset + 2);
3782 /* Don't fill PLT entry for static executables. */
3783 if (plt == htab->elf.splt)
3785 /* Put relocation index. */
3786 bfd_put_32 (output_bfd, plt_index,
3787 plt->contents + h->plt.offset + 7);
3788 /* Put offset for jmp .PLT0. */
3789 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3790 plt->contents + h->plt.offset + 12);
3793 /* Fill in the entry in the global offset table, initially this
3794 points to the pushq instruction in the PLT which is at offset 6. */
3795 bfd_put_64 (output_bfd, (plt->output_section->vma
3796 + plt->output_offset
3797 + h->plt.offset + 6),
3798 gotplt->contents + got_offset);
3800 /* Fill in the entry in the .rela.plt section. */
3801 rela.r_offset = (gotplt->output_section->vma
3802 + gotplt->output_offset
3803 + got_offset);
3804 if (h->dynindx == -1
3805 || ((info->executable
3806 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3807 && h->def_regular
3808 && h->type == STT_GNU_IFUNC))
3810 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3811 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
3812 rela.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
3813 rela.r_addend = (h->root.u.def.value
3814 + h->root.u.def.section->output_section->vma
3815 + h->root.u.def.section->output_offset);
3817 else
3819 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3820 rela.r_addend = 0;
3822 loc = relplt->contents + plt_index * sizeof (Elf64_External_Rela);
3823 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3825 if (!h->def_regular)
3827 /* Mark the symbol as undefined, rather than as defined in
3828 the .plt section. Leave the value if there were any
3829 relocations where pointer equality matters (this is a clue
3830 for the dynamic linker, to make function pointer
3831 comparisons work between an application and shared
3832 library), otherwise set it to zero. If a function is only
3833 called from a binary, there is no need to slow down
3834 shared libraries because of that. */
3835 sym->st_shndx = SHN_UNDEF;
3836 if (!h->pointer_equality_needed)
3837 sym->st_value = 0;
3841 if (h->got.offset != (bfd_vma) -1
3842 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3843 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3845 Elf_Internal_Rela rela;
3847 /* This symbol has an entry in the global offset table. Set it
3848 up. */
3849 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
3850 abort ();
3852 rela.r_offset = (htab->elf.sgot->output_section->vma
3853 + htab->elf.sgot->output_offset
3854 + (h->got.offset &~ (bfd_vma) 1));
3856 /* If this is a static link, or it is a -Bsymbolic link and the
3857 symbol is defined locally or was forced to be local because
3858 of a version file, we just want to emit a RELATIVE reloc.
3859 The entry in the global offset table will already have been
3860 initialized in the relocate_section function. */
3861 if (h->def_regular
3862 && h->type == STT_GNU_IFUNC)
3864 if (info->shared)
3866 /* Generate R_X86_64_GLOB_DAT. */
3867 goto do_glob_dat;
3869 else
3871 asection *plt;
3873 if (!h->pointer_equality_needed)
3874 abort ();
3876 /* For non-shared object, we can't use .got.plt, which
3877 contains the real function addres if we need pointer
3878 equality. We load the GOT entry with the PLT entry. */
3879 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3880 bfd_put_64 (output_bfd, (plt->output_section->vma
3881 + plt->output_offset
3882 + h->plt.offset),
3883 htab->elf.sgot->contents + h->got.offset);
3884 return TRUE;
3887 else if (info->shared
3888 && SYMBOL_REFERENCES_LOCAL (info, h))
3890 if (!h->def_regular)
3891 return FALSE;
3892 BFD_ASSERT((h->got.offset & 1) != 0);
3893 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3894 rela.r_addend = (h->root.u.def.value
3895 + h->root.u.def.section->output_section->vma
3896 + h->root.u.def.section->output_offset);
3898 else
3900 BFD_ASSERT((h->got.offset & 1) == 0);
3901 do_glob_dat:
3902 bfd_put_64 (output_bfd, (bfd_vma) 0,
3903 htab->elf.sgot->contents + h->got.offset);
3904 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3905 rela.r_addend = 0;
3908 elf64_x86_64_append_rela (output_bfd, htab->elf.srelgot, &rela);
3911 if (h->needs_copy)
3913 Elf_Internal_Rela rela;
3915 /* This symbol needs a copy reloc. Set it up. */
3917 if (h->dynindx == -1
3918 || (h->root.type != bfd_link_hash_defined
3919 && h->root.type != bfd_link_hash_defweak)
3920 || htab->srelbss == NULL)
3921 abort ();
3923 rela.r_offset = (h->root.u.def.value
3924 + h->root.u.def.section->output_section->vma
3925 + h->root.u.def.section->output_offset);
3926 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3927 rela.r_addend = 0;
3928 elf64_x86_64_append_rela (output_bfd, htab->srelbss, &rela);
3931 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
3932 be NULL for local symbols. */
3933 if (sym != NULL
3934 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
3935 || h == htab->elf.hgot))
3936 sym->st_shndx = SHN_ABS;
3938 return TRUE;
3941 /* Finish up local dynamic symbol handling. We set the contents of
3942 various dynamic sections here. */
3944 static bfd_boolean
3945 elf64_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
3947 struct elf_link_hash_entry *h
3948 = (struct elf_link_hash_entry *) *slot;
3949 struct bfd_link_info *info
3950 = (struct bfd_link_info *) inf;
3952 return elf64_x86_64_finish_dynamic_symbol (info->output_bfd,
3953 info, h, NULL);
3956 /* Used to decide how to sort relocs in an optimal manner for the
3957 dynamic linker, before writing them out. */
3959 static enum elf_reloc_type_class
3960 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
3962 switch ((int) ELF64_R_TYPE (rela->r_info))
3964 case R_X86_64_RELATIVE:
3965 return reloc_class_relative;
3966 case R_X86_64_JUMP_SLOT:
3967 return reloc_class_plt;
3968 case R_X86_64_COPY:
3969 return reloc_class_copy;
3970 default:
3971 return reloc_class_normal;
3975 /* Finish up the dynamic sections. */
3977 static bfd_boolean
3978 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3980 struct elf64_x86_64_link_hash_table *htab;
3981 bfd *dynobj;
3982 asection *sdyn;
3984 htab = elf64_x86_64_hash_table (info);
3985 dynobj = htab->elf.dynobj;
3986 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3988 if (htab->elf.dynamic_sections_created)
3990 Elf64_External_Dyn *dyncon, *dynconend;
3992 if (sdyn == NULL || htab->elf.sgot == NULL)
3993 abort ();
3995 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3996 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3997 for (; dyncon < dynconend; dyncon++)
3999 Elf_Internal_Dyn dyn;
4000 asection *s;
4002 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4004 switch (dyn.d_tag)
4006 default:
4007 continue;
4009 case DT_PLTGOT:
4010 s = htab->elf.sgotplt;
4011 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4012 break;
4014 case DT_JMPREL:
4015 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
4016 break;
4018 case DT_PLTRELSZ:
4019 s = htab->elf.srelplt->output_section;
4020 dyn.d_un.d_val = s->size;
4021 break;
4023 case DT_RELASZ:
4024 /* The procedure linkage table relocs (DT_JMPREL) should
4025 not be included in the overall relocs (DT_RELA).
4026 Therefore, we override the DT_RELASZ entry here to
4027 make it not include the JMPREL relocs. Since the
4028 linker script arranges for .rela.plt to follow all
4029 other relocation sections, we don't have to worry
4030 about changing the DT_RELA entry. */
4031 if (htab->elf.srelplt != NULL)
4033 s = htab->elf.srelplt->output_section;
4034 dyn.d_un.d_val -= s->size;
4036 break;
4038 case DT_TLSDESC_PLT:
4039 s = htab->elf.splt;
4040 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4041 + htab->tlsdesc_plt;
4042 break;
4044 case DT_TLSDESC_GOT:
4045 s = htab->elf.sgot;
4046 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4047 + htab->tlsdesc_got;
4048 break;
4051 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4054 /* Fill in the special first entry in the procedure linkage table. */
4055 if (htab->elf.splt && htab->elf.splt->size > 0)
4057 /* Fill in the first entry in the procedure linkage table. */
4058 memcpy (htab->elf.splt->contents, elf64_x86_64_plt0_entry,
4059 PLT_ENTRY_SIZE);
4060 /* Add offset for pushq GOT+8(%rip), since the instruction
4061 uses 6 bytes subtract this value. */
4062 bfd_put_32 (output_bfd,
4063 (htab->elf.sgotplt->output_section->vma
4064 + htab->elf.sgotplt->output_offset
4066 - htab->elf.splt->output_section->vma
4067 - htab->elf.splt->output_offset
4068 - 6),
4069 htab->elf.splt->contents + 2);
4070 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4071 the end of the instruction. */
4072 bfd_put_32 (output_bfd,
4073 (htab->elf.sgotplt->output_section->vma
4074 + htab->elf.sgotplt->output_offset
4075 + 16
4076 - htab->elf.splt->output_section->vma
4077 - htab->elf.splt->output_offset
4078 - 12),
4079 htab->elf.splt->contents + 8);
4081 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize =
4082 PLT_ENTRY_SIZE;
4084 if (htab->tlsdesc_plt)
4086 bfd_put_64 (output_bfd, (bfd_vma) 0,
4087 htab->elf.sgot->contents + htab->tlsdesc_got);
4089 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
4090 elf64_x86_64_plt0_entry,
4091 PLT_ENTRY_SIZE);
4093 /* Add offset for pushq GOT+8(%rip), since the
4094 instruction uses 6 bytes subtract this value. */
4095 bfd_put_32 (output_bfd,
4096 (htab->elf.sgotplt->output_section->vma
4097 + htab->elf.sgotplt->output_offset
4099 - htab->elf.splt->output_section->vma
4100 - htab->elf.splt->output_offset
4101 - htab->tlsdesc_plt
4102 - 6),
4103 htab->elf.splt->contents + htab->tlsdesc_plt + 2);
4104 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4105 htab->tlsdesc_got. The 12 is the offset to the end of
4106 the instruction. */
4107 bfd_put_32 (output_bfd,
4108 (htab->elf.sgot->output_section->vma
4109 + htab->elf.sgot->output_offset
4110 + htab->tlsdesc_got
4111 - htab->elf.splt->output_section->vma
4112 - htab->elf.splt->output_offset
4113 - htab->tlsdesc_plt
4114 - 12),
4115 htab->elf.splt->contents + htab->tlsdesc_plt + 8);
4120 if (htab->elf.sgotplt)
4122 /* Fill in the first three entries in the global offset table. */
4123 if (htab->elf.sgotplt->size > 0)
4125 /* Set the first entry in the global offset table to the address of
4126 the dynamic section. */
4127 if (sdyn == NULL)
4128 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4129 else
4130 bfd_put_64 (output_bfd,
4131 sdyn->output_section->vma + sdyn->output_offset,
4132 htab->elf.sgotplt->contents);
4133 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4134 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4135 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4138 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
4139 GOT_ENTRY_SIZE;
4142 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4143 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
4144 = GOT_ENTRY_SIZE;
4146 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4147 htab_traverse (htab->loc_hash_table,
4148 elf64_x86_64_finish_local_dynamic_symbol,
4149 info);
4151 return TRUE;
4154 /* Return address for Ith PLT stub in section PLT, for relocation REL
4155 or (bfd_vma) -1 if it should not be included. */
4157 static bfd_vma
4158 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4159 const arelent *rel ATTRIBUTE_UNUSED)
4161 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4164 /* Handle an x86-64 specific section when reading an object file. This
4165 is called when elfcode.h finds a section with an unknown type. */
4167 static bfd_boolean
4168 elf64_x86_64_section_from_shdr (bfd *abfd,
4169 Elf_Internal_Shdr *hdr,
4170 const char *name,
4171 int shindex)
4173 if (hdr->sh_type != SHT_X86_64_UNWIND)
4174 return FALSE;
4176 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4177 return FALSE;
4179 return TRUE;
4182 /* Hook called by the linker routine which adds symbols from an object
4183 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4184 of .bss. */
4186 static bfd_boolean
4187 elf64_x86_64_add_symbol_hook (bfd *abfd,
4188 struct bfd_link_info *info,
4189 Elf_Internal_Sym *sym,
4190 const char **namep ATTRIBUTE_UNUSED,
4191 flagword *flagsp ATTRIBUTE_UNUSED,
4192 asection **secp,
4193 bfd_vma *valp)
4195 asection *lcomm;
4197 switch (sym->st_shndx)
4199 case SHN_X86_64_LCOMMON:
4200 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4201 if (lcomm == NULL)
4203 lcomm = bfd_make_section_with_flags (abfd,
4204 "LARGE_COMMON",
4205 (SEC_ALLOC
4206 | SEC_IS_COMMON
4207 | SEC_LINKER_CREATED));
4208 if (lcomm == NULL)
4209 return FALSE;
4210 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4212 *secp = lcomm;
4213 *valp = sym->st_size;
4214 break;
4217 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4218 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4220 return TRUE;
4224 /* Given a BFD section, try to locate the corresponding ELF section
4225 index. */
4227 static bfd_boolean
4228 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4229 asection *sec, int *index)
4231 if (sec == &_bfd_elf_large_com_section)
4233 *index = SHN_X86_64_LCOMMON;
4234 return TRUE;
4236 return FALSE;
4239 /* Process a symbol. */
4241 static void
4242 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4243 asymbol *asym)
4245 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4247 switch (elfsym->internal_elf_sym.st_shndx)
4249 case SHN_X86_64_LCOMMON:
4250 asym->section = &_bfd_elf_large_com_section;
4251 asym->value = elfsym->internal_elf_sym.st_size;
4252 /* Common symbol doesn't set BSF_GLOBAL. */
4253 asym->flags &= ~BSF_GLOBAL;
4254 break;
4258 static bfd_boolean
4259 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
4261 return (sym->st_shndx == SHN_COMMON
4262 || sym->st_shndx == SHN_X86_64_LCOMMON);
4265 static unsigned int
4266 elf64_x86_64_common_section_index (asection *sec)
4268 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4269 return SHN_COMMON;
4270 else
4271 return SHN_X86_64_LCOMMON;
4274 static asection *
4275 elf64_x86_64_common_section (asection *sec)
4277 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4278 return bfd_com_section_ptr;
4279 else
4280 return &_bfd_elf_large_com_section;
4283 static bfd_boolean
4284 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4285 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4286 struct elf_link_hash_entry *h,
4287 Elf_Internal_Sym *sym,
4288 asection **psec,
4289 bfd_vma *pvalue ATTRIBUTE_UNUSED,
4290 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4291 bfd_boolean *skip ATTRIBUTE_UNUSED,
4292 bfd_boolean *override ATTRIBUTE_UNUSED,
4293 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4294 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4295 bfd_boolean *newdef ATTRIBUTE_UNUSED,
4296 bfd_boolean *newdyn,
4297 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4298 bfd_boolean *newweak ATTRIBUTE_UNUSED,
4299 bfd *abfd ATTRIBUTE_UNUSED,
4300 asection **sec,
4301 bfd_boolean *olddef ATTRIBUTE_UNUSED,
4302 bfd_boolean *olddyn,
4303 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4304 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4305 bfd *oldbfd,
4306 asection **oldsec)
4308 /* A normal common symbol and a large common symbol result in a
4309 normal common symbol. We turn the large common symbol into a
4310 normal one. */
4311 if (!*olddyn
4312 && h->root.type == bfd_link_hash_common
4313 && !*newdyn
4314 && bfd_is_com_section (*sec)
4315 && *oldsec != *sec)
4317 if (sym->st_shndx == SHN_COMMON
4318 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4320 h->root.u.c.p->section
4321 = bfd_make_section_old_way (oldbfd, "COMMON");
4322 h->root.u.c.p->section->flags = SEC_ALLOC;
4324 else if (sym->st_shndx == SHN_X86_64_LCOMMON
4325 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4326 *psec = *sec = bfd_com_section_ptr;
4329 return TRUE;
4332 static int
4333 elf64_x86_64_additional_program_headers (bfd *abfd,
4334 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4336 asection *s;
4337 int count = 0;
4339 /* Check to see if we need a large readonly segment. */
4340 s = bfd_get_section_by_name (abfd, ".lrodata");
4341 if (s && (s->flags & SEC_LOAD))
4342 count++;
4344 /* Check to see if we need a large data segment. Since .lbss sections
4345 is placed right after the .bss section, there should be no need for
4346 a large data segment just because of .lbss. */
4347 s = bfd_get_section_by_name (abfd, ".ldata");
4348 if (s && (s->flags & SEC_LOAD))
4349 count++;
4351 return count;
4354 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4356 static bfd_boolean
4357 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4359 if (h->plt.offset != (bfd_vma) -1
4360 && !h->def_regular
4361 && !h->pointer_equality_needed)
4362 return FALSE;
4364 return _bfd_elf_hash_symbol (h);
4367 static const struct bfd_elf_special_section
4368 elf64_x86_64_special_sections[]=
4370 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4371 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4372 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4373 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4374 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4375 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4376 { NULL, 0, 0, 0, 0 }
4379 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
4380 #define TARGET_LITTLE_NAME "elf64-x86-64"
4381 #define ELF_ARCH bfd_arch_i386
4382 #define ELF_MACHINE_CODE EM_X86_64
4383 #define ELF_MAXPAGESIZE 0x200000
4384 #define ELF_MINPAGESIZE 0x1000
4385 #define ELF_COMMONPAGESIZE 0x1000
4387 #define elf_backend_can_gc_sections 1
4388 #define elf_backend_can_refcount 1
4389 #define elf_backend_want_got_plt 1
4390 #define elf_backend_plt_readonly 1
4391 #define elf_backend_want_plt_sym 0
4392 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
4393 #define elf_backend_rela_normal 1
4395 #define elf_info_to_howto elf64_x86_64_info_to_howto
4397 #define bfd_elf64_bfd_link_hash_table_create \
4398 elf64_x86_64_link_hash_table_create
4399 #define bfd_elf64_bfd_link_hash_table_free \
4400 elf64_x86_64_link_hash_table_free
4401 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
4402 #define bfd_elf64_bfd_reloc_name_lookup \
4403 elf64_x86_64_reloc_name_lookup
4405 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
4406 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4407 #define elf_backend_check_relocs elf64_x86_64_check_relocs
4408 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
4409 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
4410 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
4411 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
4412 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
4413 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
4414 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
4415 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
4416 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
4417 #define elf_backend_relocate_section elf64_x86_64_relocate_section
4418 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
4419 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
4420 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4421 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
4422 #define elf_backend_object_p elf64_x86_64_elf_object_p
4423 #define bfd_elf64_mkobject elf64_x86_64_mkobject
4425 #define elf_backend_section_from_shdr \
4426 elf64_x86_64_section_from_shdr
4428 #define elf_backend_section_from_bfd_section \
4429 elf64_x86_64_elf_section_from_bfd_section
4430 #define elf_backend_add_symbol_hook \
4431 elf64_x86_64_add_symbol_hook
4432 #define elf_backend_symbol_processing \
4433 elf64_x86_64_symbol_processing
4434 #define elf_backend_common_section_index \
4435 elf64_x86_64_common_section_index
4436 #define elf_backend_common_section \
4437 elf64_x86_64_common_section
4438 #define elf_backend_common_definition \
4439 elf64_x86_64_common_definition
4440 #define elf_backend_merge_symbol \
4441 elf64_x86_64_merge_symbol
4442 #define elf_backend_special_sections \
4443 elf64_x86_64_special_sections
4444 #define elf_backend_additional_program_headers \
4445 elf64_x86_64_additional_program_headers
4446 #define elf_backend_hash_symbol \
4447 elf64_x86_64_hash_symbol
4449 #undef elf_backend_post_process_headers
4450 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4452 #include "elf64-target.h"
4454 /* FreeBSD support. */
4456 #undef TARGET_LITTLE_SYM
4457 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
4458 #undef TARGET_LITTLE_NAME
4459 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
4461 #undef ELF_OSABI
4462 #define ELF_OSABI ELFOSABI_FREEBSD
4464 #undef elf64_bed
4465 #define elf64_bed elf64_x86_64_fbsd_bed
4467 #include "elf64-target.h"
4469 /* Intel L1OM support. */
4471 static bfd_boolean
4472 elf64_l1om_elf_object_p (bfd *abfd)
4474 /* Set the right machine number for an L1OM elf64 file. */
4475 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
4476 return TRUE;
4479 #undef TARGET_LITTLE_SYM
4480 #define TARGET_LITTLE_SYM bfd_elf64_l1om_vec
4481 #undef TARGET_LITTLE_NAME
4482 #define TARGET_LITTLE_NAME "elf64-l1om"
4483 #undef ELF_ARCH
4484 #define ELF_ARCH bfd_arch_l1om
4486 #undef ELF_MACHINE_CODE
4487 #define ELF_MACHINE_CODE EM_L1OM
4489 #undef ELF_OSABI
4491 #undef elf64_bed
4492 #define elf64_bed elf64_l1om_bed
4494 #undef elf_backend_object_p
4495 #define elf_backend_object_p elf64_l1om_elf_object_p
4497 #undef elf_backend_post_process_headers
4499 #include "elf64-target.h"
4501 /* FreeBSD L1OM support. */
4503 #undef TARGET_LITTLE_SYM
4504 #define TARGET_LITTLE_SYM bfd_elf64_l1om_freebsd_vec
4505 #undef TARGET_LITTLE_NAME
4506 #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
4508 #undef ELF_OSABI
4509 #define ELF_OSABI ELFOSABI_FREEBSD
4511 #undef elf64_bed
4512 #define elf64_bed elf64_l1om_fbsd_bed
4514 #undef elf_backend_post_process_headers
4515 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4517 #include "elf64-target.h"