1 /* X86-64 specific support for ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 Free Software Foundation, Inc.
5 Contributed by Jan Hubicka <jh@suse.cz>.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
30 #include "bfd_stdint.h"
34 #include "libiberty.h"
36 #include "elf/x86-64.h"
43 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
44 #define MINUS_ONE (~ (bfd_vma) 0)
46 /* Since both 32-bit and 64-bit x86-64 encode relocation type in the
47 identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
48 relocation type. We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
49 since they are the same. */
51 #define ABI_64_P(abfd) \
52 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
54 /* The relocation "howto" table. Order of fields:
55 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
56 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
57 static reloc_howto_type x86_64_elf_howto_table[] =
59 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
60 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
62 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
65 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
66 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
68 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
69 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
71 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
72 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
74 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
75 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
77 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
78 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
80 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
81 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
83 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
84 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
86 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
87 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
89 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
90 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
92 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
93 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
95 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
96 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
97 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
98 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
99 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
100 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
101 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
102 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
103 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
104 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
106 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
107 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
109 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
110 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
112 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
113 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
115 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
116 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
118 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
119 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
121 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
122 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
124 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
125 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
127 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
128 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
130 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
131 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
132 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
133 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
134 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
135 FALSE, 0xffffffff, 0xffffffff, TRUE),
136 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
137 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
139 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
140 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
142 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
143 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
144 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
145 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
146 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
148 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
149 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
151 HOWTO(R_X86_64_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
152 bfd_elf_generic_reloc, "R_X86_64_SIZE32", FALSE, 0xffffffff, 0xffffffff,
154 HOWTO(R_X86_64_SIZE64, 0, 4, 64, FALSE, 0, complain_overflow_unsigned,
155 bfd_elf_generic_reloc, "R_X86_64_SIZE64", FALSE, MINUS_ONE, MINUS_ONE,
157 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
158 complain_overflow_bitfield, bfd_elf_generic_reloc,
159 "R_X86_64_GOTPC32_TLSDESC",
160 FALSE, 0xffffffff, 0xffffffff, TRUE),
161 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
162 complain_overflow_dont, bfd_elf_generic_reloc,
163 "R_X86_64_TLSDESC_CALL",
165 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
166 complain_overflow_bitfield, bfd_elf_generic_reloc,
168 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
169 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
170 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
172 HOWTO(R_X86_64_RELATIVE64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
173 bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", FALSE, MINUS_ONE,
175 HOWTO(R_X86_64_PC32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
176 bfd_elf_generic_reloc, "R_X86_64_PC32_BND", FALSE, 0xffffffff, 0xffffffff,
178 HOWTO(R_X86_64_PLT32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
179 bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", FALSE, 0xffffffff, 0xffffffff,
182 /* We have a gap in the reloc numbers here.
183 R_X86_64_standard counts the number up to this point, and
184 R_X86_64_vt_offset is the value to subtract from a reloc type of
185 R_X86_64_GNU_VT* to form an index into this table. */
186 #define R_X86_64_standard (R_X86_64_PLT32_BND + 1)
187 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
189 /* GNU extension to record C++ vtable hierarchy. */
190 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
191 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
193 /* GNU extension to record C++ vtable member usage. */
194 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
195 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
198 /* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
199 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
200 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
204 #define IS_X86_64_PCREL_TYPE(TYPE) \
205 ( ((TYPE) == R_X86_64_PC8) \
206 || ((TYPE) == R_X86_64_PC16) \
207 || ((TYPE) == R_X86_64_PC32) \
208 || ((TYPE) == R_X86_64_PC32_BND) \
209 || ((TYPE) == R_X86_64_PC64))
211 /* Map BFD relocs to the x86_64 elf relocs. */
214 bfd_reloc_code_real_type bfd_reloc_val;
215 unsigned char elf_reloc_val;
218 static const struct elf_reloc_map x86_64_reloc_map[] =
220 { BFD_RELOC_NONE, R_X86_64_NONE, },
221 { BFD_RELOC_64, R_X86_64_64, },
222 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
223 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
224 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
225 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
226 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
227 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
228 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
229 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
230 { BFD_RELOC_32, R_X86_64_32, },
231 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
232 { BFD_RELOC_16, R_X86_64_16, },
233 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
234 { BFD_RELOC_8, R_X86_64_8, },
235 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
236 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
237 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
238 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
239 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
240 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
241 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
242 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
243 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
244 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
245 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
246 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
247 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
248 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
249 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
250 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
251 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
252 { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
253 { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
254 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
255 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
256 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
257 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
258 { BFD_RELOC_X86_64_PC32_BND, R_X86_64_PC32_BND,},
259 { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND,},
260 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
261 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
264 static reloc_howto_type *
265 elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
269 if (r_type == (unsigned int) R_X86_64_32)
274 i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
276 else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
277 || r_type >= (unsigned int) R_X86_64_max)
279 if (r_type >= (unsigned int) R_X86_64_standard)
281 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
283 r_type = R_X86_64_NONE;
288 i = r_type - (unsigned int) R_X86_64_vt_offset;
289 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
290 return &x86_64_elf_howto_table[i];
293 /* Given a BFD reloc type, return a HOWTO structure. */
294 static reloc_howto_type *
295 elf_x86_64_reloc_type_lookup (bfd *abfd,
296 bfd_reloc_code_real_type code)
300 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
303 if (x86_64_reloc_map[i].bfd_reloc_val == code)
304 return elf_x86_64_rtype_to_howto (abfd,
305 x86_64_reloc_map[i].elf_reloc_val);
310 static reloc_howto_type *
311 elf_x86_64_reloc_name_lookup (bfd *abfd,
316 if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
318 /* Get x32 R_X86_64_32. */
319 reloc_howto_type *reloc
320 = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
321 BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
325 for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
326 if (x86_64_elf_howto_table[i].name != NULL
327 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
328 return &x86_64_elf_howto_table[i];
333 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
336 elf_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
337 Elf_Internal_Rela *dst)
341 r_type = ELF32_R_TYPE (dst->r_info);
342 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
343 BFD_ASSERT (r_type == cache_ptr->howto->type);
346 /* Support for core dump NOTE sections. */
348 elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
353 switch (note->descsz)
358 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
360 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
363 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
371 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
373 elf_tdata (abfd)->core->signal
374 = bfd_get_16 (abfd, note->descdata + 12);
377 elf_tdata (abfd)->core->lwpid
378 = bfd_get_32 (abfd, note->descdata + 32);
387 /* Make a ".reg/999" section. */
388 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
389 size, note->descpos + offset);
393 elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
395 switch (note->descsz)
400 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
401 elf_tdata (abfd)->core->pid
402 = bfd_get_32 (abfd, note->descdata + 12);
403 elf_tdata (abfd)->core->program
404 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
405 elf_tdata (abfd)->core->command
406 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
409 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
410 elf_tdata (abfd)->core->pid
411 = bfd_get_32 (abfd, note->descdata + 24);
412 elf_tdata (abfd)->core->program
413 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
414 elf_tdata (abfd)->core->command
415 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
418 /* Note that for some reason, a spurious space is tacked
419 onto the end of the args in some (at least one anyway)
420 implementations, so strip it off if it exists. */
423 char *command = elf_tdata (abfd)->core->command;
424 int n = strlen (command);
426 if (0 < n && command[n - 1] == ' ')
427 command[n - 1] = '\0';
435 elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
438 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
440 const char *fname, *psargs;
451 va_start (ap, note_type);
452 fname = va_arg (ap, const char *);
453 psargs = va_arg (ap, const char *);
456 if (bed->s->elfclass == ELFCLASS32)
459 memset (&data, 0, sizeof (data));
460 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
461 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
462 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
463 &data, sizeof (data));
468 memset (&data, 0, sizeof (data));
469 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
470 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
471 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
472 &data, sizeof (data));
477 va_start (ap, note_type);
478 pid = va_arg (ap, long);
479 cursig = va_arg (ap, int);
480 gregs = va_arg (ap, const void *);
483 if (bed->s->elfclass == ELFCLASS32)
485 if (bed->elf_machine_code == EM_X86_64)
487 prstatusx32_t prstat;
488 memset (&prstat, 0, sizeof (prstat));
490 prstat.pr_cursig = cursig;
491 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
492 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
493 &prstat, sizeof (prstat));
498 memset (&prstat, 0, sizeof (prstat));
500 prstat.pr_cursig = cursig;
501 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
502 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
503 &prstat, sizeof (prstat));
509 memset (&prstat, 0, sizeof (prstat));
511 prstat.pr_cursig = cursig;
512 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
513 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
514 &prstat, sizeof (prstat));
521 /* Functions for the x86-64 ELF linker. */
523 /* The name of the dynamic interpreter. This is put in the .interp
526 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
527 #define ELF32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
529 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
530 copying dynamic variables from a shared lib into an app's dynbss
531 section, and instead use a dynamic relocation to point into the
533 #define ELIMINATE_COPY_RELOCS 1
535 /* The size in bytes of an entry in the global offset table. */
537 #define GOT_ENTRY_SIZE 8
539 /* The size in bytes of an entry in the procedure linkage table. */
541 #define PLT_ENTRY_SIZE 16
543 /* The first entry in a procedure linkage table looks like this. See the
544 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
546 static const bfd_byte elf_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
548 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
549 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
550 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
553 /* Subsequent entries in a procedure linkage table look like this. */
555 static const bfd_byte elf_x86_64_plt_entry[PLT_ENTRY_SIZE] =
557 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
558 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
559 0x68, /* pushq immediate */
560 0, 0, 0, 0, /* replaced with index into relocation table. */
561 0xe9, /* jmp relative */
562 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
565 /* .eh_frame covering the .plt section. */
567 static const bfd_byte elf_x86_64_eh_frame_plt[] =
569 #define PLT_CIE_LENGTH 20
570 #define PLT_FDE_LENGTH 36
571 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
572 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
573 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
574 0, 0, 0, 0, /* CIE ID */
576 'z', 'R', 0, /* Augmentation string */
577 1, /* Code alignment factor */
578 0x78, /* Data alignment factor */
579 16, /* Return address column */
580 1, /* Augmentation size */
581 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
582 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
583 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
584 DW_CFA_nop, DW_CFA_nop,
586 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
587 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
588 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
589 0, 0, 0, 0, /* .plt size goes here */
590 0, /* Augmentation size */
591 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
592 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
593 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
594 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
595 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
596 11, /* Block length */
597 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
598 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
599 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
600 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
601 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
604 /* Architecture-specific backend data for x86-64. */
606 struct elf_x86_64_backend_data
608 /* Templates for the initial PLT entry and for subsequent entries. */
609 const bfd_byte *plt0_entry;
610 const bfd_byte *plt_entry;
611 unsigned int plt_entry_size; /* Size of each PLT entry. */
613 /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
614 unsigned int plt0_got1_offset;
615 unsigned int plt0_got2_offset;
617 /* Offset of the end of the PC-relative instruction containing
619 unsigned int plt0_got2_insn_end;
621 /* Offsets into plt_entry that are to be replaced with... */
622 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
623 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
624 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
626 /* Length of the PC-relative instruction containing plt_got_offset. */
627 unsigned int plt_got_insn_size;
629 /* Offset of the end of the PC-relative jump to plt0_entry. */
630 unsigned int plt_plt_insn_end;
632 /* Offset into plt_entry where the initial value of the GOT entry points. */
633 unsigned int plt_lazy_offset;
635 /* .eh_frame covering the .plt section. */
636 const bfd_byte *eh_frame_plt;
637 unsigned int eh_frame_plt_size;
640 #define get_elf_x86_64_arch_data(bed) \
641 ((const struct elf_x86_64_backend_data *) (bed)->arch_data)
643 #define get_elf_x86_64_backend_data(abfd) \
644 get_elf_x86_64_arch_data (get_elf_backend_data (abfd))
646 #define GET_PLT_ENTRY_SIZE(abfd) \
647 get_elf_x86_64_backend_data (abfd)->plt_entry_size
649 /* These are the standard parameters. */
650 static const struct elf_x86_64_backend_data elf_x86_64_arch_bed =
652 elf_x86_64_plt0_entry, /* plt0_entry */
653 elf_x86_64_plt_entry, /* plt_entry */
654 sizeof (elf_x86_64_plt_entry), /* plt_entry_size */
655 2, /* plt0_got1_offset */
656 8, /* plt0_got2_offset */
657 12, /* plt0_got2_insn_end */
658 2, /* plt_got_offset */
659 7, /* plt_reloc_offset */
660 12, /* plt_plt_offset */
661 6, /* plt_got_insn_size */
662 PLT_ENTRY_SIZE, /* plt_plt_insn_end */
663 6, /* plt_lazy_offset */
664 elf_x86_64_eh_frame_plt, /* eh_frame_plt */
665 sizeof (elf_x86_64_eh_frame_plt), /* eh_frame_plt_size */
668 #define elf_backend_arch_data &elf_x86_64_arch_bed
670 /* x86-64 ELF linker hash entry. */
672 struct elf_x86_64_link_hash_entry
674 struct elf_link_hash_entry elf;
676 /* Track dynamic relocs copied for this symbol. */
677 struct elf_dyn_relocs *dyn_relocs;
679 #define GOT_UNKNOWN 0
683 #define GOT_TLS_GDESC 4
684 #define GOT_TLS_GD_BOTH_P(type) \
685 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
686 #define GOT_TLS_GD_P(type) \
687 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
688 #define GOT_TLS_GDESC_P(type) \
689 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
690 #define GOT_TLS_GD_ANY_P(type) \
691 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
692 unsigned char tls_type;
694 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
695 starting at the end of the jump table. */
699 #define elf_x86_64_hash_entry(ent) \
700 ((struct elf_x86_64_link_hash_entry *)(ent))
702 struct elf_x86_64_obj_tdata
704 struct elf_obj_tdata root;
706 /* tls_type for each local got entry. */
707 char *local_got_tls_type;
709 /* GOTPLT entries for TLS descriptors. */
710 bfd_vma *local_tlsdesc_gotent;
713 #define elf_x86_64_tdata(abfd) \
714 ((struct elf_x86_64_obj_tdata *) (abfd)->tdata.any)
716 #define elf_x86_64_local_got_tls_type(abfd) \
717 (elf_x86_64_tdata (abfd)->local_got_tls_type)
719 #define elf_x86_64_local_tlsdesc_gotent(abfd) \
720 (elf_x86_64_tdata (abfd)->local_tlsdesc_gotent)
722 #define is_x86_64_elf(bfd) \
723 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
724 && elf_tdata (bfd) != NULL \
725 && elf_object_id (bfd) == X86_64_ELF_DATA)
728 elf_x86_64_mkobject (bfd *abfd)
730 return bfd_elf_allocate_object (abfd, sizeof (struct elf_x86_64_obj_tdata),
734 /* x86-64 ELF linker hash table. */
736 struct elf_x86_64_link_hash_table
738 struct elf_link_hash_table elf;
740 /* Short-cuts to get to dynamic linker sections. */
743 asection *plt_eh_frame;
747 bfd_signed_vma refcount;
751 /* The amount of space used by the jump slots in the GOT. */
752 bfd_vma sgotplt_jump_table_size;
754 /* Small local sym cache. */
755 struct sym_cache sym_cache;
757 bfd_vma (*r_info) (bfd_vma, bfd_vma);
758 bfd_vma (*r_sym) (bfd_vma);
759 unsigned int pointer_r_type;
760 const char *dynamic_interpreter;
761 int dynamic_interpreter_size;
763 /* _TLS_MODULE_BASE_ symbol. */
764 struct bfd_link_hash_entry *tls_module_base;
766 /* Used by local STT_GNU_IFUNC symbols. */
767 htab_t loc_hash_table;
768 void * loc_hash_memory;
770 /* The offset into splt of the PLT entry for the TLS descriptor
771 resolver. Special values are 0, if not necessary (or not found
772 to be necessary yet), and -1 if needed but not determined
775 /* The offset into sgot of the GOT entry used by the PLT entry
779 /* The index of the next R_X86_64_JUMP_SLOT entry in .rela.plt. */
780 bfd_vma next_jump_slot_index;
781 /* The index of the next R_X86_64_IRELATIVE entry in .rela.plt. */
782 bfd_vma next_irelative_index;
785 /* Get the x86-64 ELF linker hash table from a link_info structure. */
787 #define elf_x86_64_hash_table(p) \
788 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
789 == X86_64_ELF_DATA ? ((struct elf_x86_64_link_hash_table *) ((p)->hash)) : NULL)
791 #define elf_x86_64_compute_jump_table_size(htab) \
792 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
794 /* Create an entry in an x86-64 ELF linker hash table. */
796 static struct bfd_hash_entry *
797 elf_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
798 struct bfd_hash_table *table,
801 /* Allocate the structure if it has not already been allocated by a
805 entry = (struct bfd_hash_entry *)
806 bfd_hash_allocate (table,
807 sizeof (struct elf_x86_64_link_hash_entry));
812 /* Call the allocation method of the superclass. */
813 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
816 struct elf_x86_64_link_hash_entry *eh;
818 eh = (struct elf_x86_64_link_hash_entry *) entry;
819 eh->dyn_relocs = NULL;
820 eh->tls_type = GOT_UNKNOWN;
821 eh->tlsdesc_got = (bfd_vma) -1;
827 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
828 for local symbol so that we can handle local STT_GNU_IFUNC symbols
829 as global symbol. We reuse indx and dynstr_index for local symbol
830 hash since they aren't used by global symbols in this backend. */
833 elf_x86_64_local_htab_hash (const void *ptr)
835 struct elf_link_hash_entry *h
836 = (struct elf_link_hash_entry *) ptr;
837 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
840 /* Compare local hash entries. */
843 elf_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
845 struct elf_link_hash_entry *h1
846 = (struct elf_link_hash_entry *) ptr1;
847 struct elf_link_hash_entry *h2
848 = (struct elf_link_hash_entry *) ptr2;
850 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
853 /* Find and/or create a hash entry for local symbol. */
855 static struct elf_link_hash_entry *
856 elf_x86_64_get_local_sym_hash (struct elf_x86_64_link_hash_table *htab,
857 bfd *abfd, const Elf_Internal_Rela *rel,
860 struct elf_x86_64_link_hash_entry e, *ret;
861 asection *sec = abfd->sections;
862 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
863 htab->r_sym (rel->r_info));
866 e.elf.indx = sec->id;
867 e.elf.dynstr_index = htab->r_sym (rel->r_info);
868 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
869 create ? INSERT : NO_INSERT);
876 ret = (struct elf_x86_64_link_hash_entry *) *slot;
880 ret = (struct elf_x86_64_link_hash_entry *)
881 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
882 sizeof (struct elf_x86_64_link_hash_entry));
885 memset (ret, 0, sizeof (*ret));
886 ret->elf.indx = sec->id;
887 ret->elf.dynstr_index = htab->r_sym (rel->r_info);
888 ret->elf.dynindx = -1;
894 /* Create an X86-64 ELF linker hash table. */
896 static struct bfd_link_hash_table *
897 elf_x86_64_link_hash_table_create (bfd *abfd)
899 struct elf_x86_64_link_hash_table *ret;
900 bfd_size_type amt = sizeof (struct elf_x86_64_link_hash_table);
902 ret = (struct elf_x86_64_link_hash_table *) bfd_zmalloc (amt);
906 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
907 elf_x86_64_link_hash_newfunc,
908 sizeof (struct elf_x86_64_link_hash_entry),
917 ret->r_info = elf64_r_info;
918 ret->r_sym = elf64_r_sym;
919 ret->pointer_r_type = R_X86_64_64;
920 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
921 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
925 ret->r_info = elf32_r_info;
926 ret->r_sym = elf32_r_sym;
927 ret->pointer_r_type = R_X86_64_32;
928 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
929 ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
932 ret->loc_hash_table = htab_try_create (1024,
933 elf_x86_64_local_htab_hash,
934 elf_x86_64_local_htab_eq,
936 ret->loc_hash_memory = objalloc_create ();
937 if (!ret->loc_hash_table || !ret->loc_hash_memory)
943 return &ret->elf.root;
946 /* Destroy an X86-64 ELF linker hash table. */
949 elf_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
951 struct elf_x86_64_link_hash_table *htab
952 = (struct elf_x86_64_link_hash_table *) hash;
954 if (htab->loc_hash_table)
955 htab_delete (htab->loc_hash_table);
956 if (htab->loc_hash_memory)
957 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
958 _bfd_elf_link_hash_table_free (hash);
961 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
962 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
966 elf_x86_64_create_dynamic_sections (bfd *dynobj,
967 struct bfd_link_info *info)
969 struct elf_x86_64_link_hash_table *htab;
971 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
974 htab = elf_x86_64_hash_table (info);
978 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
980 htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
983 || (!info->shared && !htab->srelbss))
986 if (!info->no_ld_generated_unwind_info
987 && htab->plt_eh_frame == NULL
988 && htab->elf.splt != NULL)
990 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
991 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
992 | SEC_LINKER_CREATED);
994 = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags);
995 if (htab->plt_eh_frame == NULL
996 || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 3))
1002 /* Copy the extra info we tack onto an elf_link_hash_entry. */
1005 elf_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
1006 struct elf_link_hash_entry *dir,
1007 struct elf_link_hash_entry *ind)
1009 struct elf_x86_64_link_hash_entry *edir, *eind;
1011 edir = (struct elf_x86_64_link_hash_entry *) dir;
1012 eind = (struct elf_x86_64_link_hash_entry *) ind;
1014 if (eind->dyn_relocs != NULL)
1016 if (edir->dyn_relocs != NULL)
1018 struct elf_dyn_relocs **pp;
1019 struct elf_dyn_relocs *p;
1021 /* Add reloc counts against the indirect sym to the direct sym
1022 list. Merge any entries against the same section. */
1023 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
1025 struct elf_dyn_relocs *q;
1027 for (q = edir->dyn_relocs; q != NULL; q = q->next)
1028 if (q->sec == p->sec)
1030 q->pc_count += p->pc_count;
1031 q->count += p->count;
1038 *pp = edir->dyn_relocs;
1041 edir->dyn_relocs = eind->dyn_relocs;
1042 eind->dyn_relocs = NULL;
1045 if (ind->root.type == bfd_link_hash_indirect
1046 && dir->got.refcount <= 0)
1048 edir->tls_type = eind->tls_type;
1049 eind->tls_type = GOT_UNKNOWN;
1052 if (ELIMINATE_COPY_RELOCS
1053 && ind->root.type != bfd_link_hash_indirect
1054 && dir->dynamic_adjusted)
1056 /* If called to transfer flags for a weakdef during processing
1057 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
1058 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
1059 dir->ref_dynamic |= ind->ref_dynamic;
1060 dir->ref_regular |= ind->ref_regular;
1061 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
1062 dir->needs_plt |= ind->needs_plt;
1063 dir->pointer_equality_needed |= ind->pointer_equality_needed;
1066 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
1070 elf64_x86_64_elf_object_p (bfd *abfd)
1072 /* Set the right machine number for an x86-64 elf64 file. */
1073 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
1078 elf32_x86_64_elf_object_p (bfd *abfd)
1080 /* Set the right machine number for an x86-64 elf32 file. */
1081 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1085 /* Return TRUE if the TLS access code sequence support transition
1089 elf_x86_64_check_tls_transition (bfd *abfd,
1090 struct bfd_link_info *info,
1093 Elf_Internal_Shdr *symtab_hdr,
1094 struct elf_link_hash_entry **sym_hashes,
1095 unsigned int r_type,
1096 const Elf_Internal_Rela *rel,
1097 const Elf_Internal_Rela *relend)
1100 unsigned long r_symndx;
1101 bfd_boolean largepic = FALSE;
1102 struct elf_link_hash_entry *h;
1104 struct elf_x86_64_link_hash_table *htab;
1106 /* Get the section contents. */
1107 if (contents == NULL)
1109 if (elf_section_data (sec)->this_hdr.contents != NULL)
1110 contents = elf_section_data (sec)->this_hdr.contents;
1113 /* FIXME: How to better handle error condition? */
1114 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1117 /* Cache the section contents for elf_link_input_bfd. */
1118 elf_section_data (sec)->this_hdr.contents = contents;
1122 htab = elf_x86_64_hash_table (info);
1123 offset = rel->r_offset;
1126 case R_X86_64_TLSGD:
1127 case R_X86_64_TLSLD:
1128 if ((rel + 1) >= relend)
1131 if (r_type == R_X86_64_TLSGD)
1133 /* Check transition from GD access model. For 64bit, only
1134 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1135 .word 0x6666; rex64; call __tls_get_addr
1136 can transit to different access model. For 32bit, only
1137 leaq foo@tlsgd(%rip), %rdi
1138 .word 0x6666; rex64; call __tls_get_addr
1139 can transit to different access model. For largepic
1141 leaq foo@tlsgd(%rip), %rdi
1142 movabsq $__tls_get_addr@pltoff, %rax
1146 static const unsigned char call[] = { 0x66, 0x66, 0x48, 0xe8 };
1147 static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1149 if ((offset + 12) > sec->size)
1152 if (memcmp (contents + offset + 4, call, 4) != 0)
1154 if (!ABI_64_P (abfd)
1155 || (offset + 19) > sec->size
1157 || memcmp (contents + offset - 3, leaq + 1, 3) != 0
1158 || memcmp (contents + offset + 4, "\x48\xb8", 2) != 0
1159 || memcmp (contents + offset + 14, "\x48\x01\xd8\xff\xd0", 5)
1164 else if (ABI_64_P (abfd))
1167 || memcmp (contents + offset - 4, leaq, 4) != 0)
1173 || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
1179 /* Check transition from LD access model. Only
1180 leaq foo@tlsld(%rip), %rdi;
1182 can transit to different access model. For largepic
1184 leaq foo@tlsld(%rip), %rdi
1185 movabsq $__tls_get_addr@pltoff, %rax
1189 static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
1191 if (offset < 3 || (offset + 9) > sec->size)
1194 if (memcmp (contents + offset - 3, lea, 3) != 0)
1197 if (0xe8 != *(contents + offset + 4))
1199 if (!ABI_64_P (abfd)
1200 || (offset + 19) > sec->size
1201 || memcmp (contents + offset + 4, "\x48\xb8", 2) != 0
1202 || memcmp (contents + offset + 14, "\x48\x01\xd8\xff\xd0", 5)
1209 r_symndx = htab->r_sym (rel[1].r_info);
1210 if (r_symndx < symtab_hdr->sh_info)
1213 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1214 /* Use strncmp to check __tls_get_addr since __tls_get_addr
1215 may be versioned. */
1217 && h->root.root.string != NULL
1219 ? ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLTOFF64
1220 : (ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PC32
1221 || ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLT32))
1222 && (strncmp (h->root.root.string,
1223 "__tls_get_addr", 14) == 0));
1225 case R_X86_64_GOTTPOFF:
1226 /* Check transition from IE access model:
1227 mov foo@gottpoff(%rip), %reg
1228 add foo@gottpoff(%rip), %reg
1231 /* Check REX prefix first. */
1232 if (offset >= 3 && (offset + 4) <= sec->size)
1234 val = bfd_get_8 (abfd, contents + offset - 3);
1235 if (val != 0x48 && val != 0x4c)
1237 /* X32 may have 0x44 REX prefix or no REX prefix. */
1238 if (ABI_64_P (abfd))
1244 /* X32 may not have any REX prefix. */
1245 if (ABI_64_P (abfd))
1247 if (offset < 2 || (offset + 3) > sec->size)
1251 val = bfd_get_8 (abfd, contents + offset - 2);
1252 if (val != 0x8b && val != 0x03)
1255 val = bfd_get_8 (abfd, contents + offset - 1);
1256 return (val & 0xc7) == 5;
1258 case R_X86_64_GOTPC32_TLSDESC:
1259 /* Check transition from GDesc access model:
1260 leaq x@tlsdesc(%rip), %rax
1262 Make sure it's a leaq adding rip to a 32-bit offset
1263 into any register, although it's probably almost always
1266 if (offset < 3 || (offset + 4) > sec->size)
1269 val = bfd_get_8 (abfd, contents + offset - 3);
1270 if ((val & 0xfb) != 0x48)
1273 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1276 val = bfd_get_8 (abfd, contents + offset - 1);
1277 return (val & 0xc7) == 0x05;
1279 case R_X86_64_TLSDESC_CALL:
1280 /* Check transition from GDesc access model:
1281 call *x@tlsdesc(%rax)
1283 if (offset + 2 <= sec->size)
1285 /* Make sure that it's a call *x@tlsdesc(%rax). */
1286 static const unsigned char call[] = { 0xff, 0x10 };
1287 return memcmp (contents + offset, call, 2) == 0;
1297 /* Return TRUE if the TLS access transition is OK or no transition
1298 will be performed. Update R_TYPE if there is a transition. */
1301 elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1302 asection *sec, bfd_byte *contents,
1303 Elf_Internal_Shdr *symtab_hdr,
1304 struct elf_link_hash_entry **sym_hashes,
1305 unsigned int *r_type, int tls_type,
1306 const Elf_Internal_Rela *rel,
1307 const Elf_Internal_Rela *relend,
1308 struct elf_link_hash_entry *h,
1309 unsigned long r_symndx)
1311 unsigned int from_type = *r_type;
1312 unsigned int to_type = from_type;
1313 bfd_boolean check = TRUE;
1315 /* Skip TLS transition for functions. */
1317 && (h->type == STT_FUNC
1318 || h->type == STT_GNU_IFUNC))
1323 case R_X86_64_TLSGD:
1324 case R_X86_64_GOTPC32_TLSDESC:
1325 case R_X86_64_TLSDESC_CALL:
1326 case R_X86_64_GOTTPOFF:
1327 if (info->executable)
1330 to_type = R_X86_64_TPOFF32;
1332 to_type = R_X86_64_GOTTPOFF;
1335 /* When we are called from elf_x86_64_relocate_section,
1336 CONTENTS isn't NULL and there may be additional transitions
1337 based on TLS_TYPE. */
1338 if (contents != NULL)
1340 unsigned int new_to_type = to_type;
1342 if (info->executable
1345 && tls_type == GOT_TLS_IE)
1346 new_to_type = R_X86_64_TPOFF32;
1348 if (to_type == R_X86_64_TLSGD
1349 || to_type == R_X86_64_GOTPC32_TLSDESC
1350 || to_type == R_X86_64_TLSDESC_CALL)
1352 if (tls_type == GOT_TLS_IE)
1353 new_to_type = R_X86_64_GOTTPOFF;
1356 /* We checked the transition before when we were called from
1357 elf_x86_64_check_relocs. We only want to check the new
1358 transition which hasn't been checked before. */
1359 check = new_to_type != to_type && from_type == to_type;
1360 to_type = new_to_type;
1365 case R_X86_64_TLSLD:
1366 if (info->executable)
1367 to_type = R_X86_64_TPOFF32;
1374 /* Return TRUE if there is no transition. */
1375 if (from_type == to_type)
1378 /* Check if the transition can be performed. */
1380 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1381 symtab_hdr, sym_hashes,
1382 from_type, rel, relend))
1384 reloc_howto_type *from, *to;
1387 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1388 to = elf_x86_64_rtype_to_howto (abfd, to_type);
1391 name = h->root.root.string;
1394 struct elf_x86_64_link_hash_table *htab;
1396 htab = elf_x86_64_hash_table (info);
1401 Elf_Internal_Sym *isym;
1403 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1405 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1409 (*_bfd_error_handler)
1410 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1411 "in section `%A' failed"),
1412 abfd, sec, from->name, to->name, name,
1413 (unsigned long) rel->r_offset);
1414 bfd_set_error (bfd_error_bad_value);
1422 /* Look through the relocs for a section during the first phase, and
1423 calculate needed space in the global offset table, procedure
1424 linkage table, and dynamic reloc sections. */
1427 elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1429 const Elf_Internal_Rela *relocs)
1431 struct elf_x86_64_link_hash_table *htab;
1432 Elf_Internal_Shdr *symtab_hdr;
1433 struct elf_link_hash_entry **sym_hashes;
1434 const Elf_Internal_Rela *rel;
1435 const Elf_Internal_Rela *rel_end;
1438 if (info->relocatable)
1441 BFD_ASSERT (is_x86_64_elf (abfd));
1443 htab = elf_x86_64_hash_table (info);
1447 symtab_hdr = &elf_symtab_hdr (abfd);
1448 sym_hashes = elf_sym_hashes (abfd);
1452 rel_end = relocs + sec->reloc_count;
1453 for (rel = relocs; rel < rel_end; rel++)
1455 unsigned int r_type;
1456 unsigned long r_symndx;
1457 struct elf_link_hash_entry *h;
1458 Elf_Internal_Sym *isym;
1460 bfd_boolean size_reloc;
1462 r_symndx = htab->r_sym (rel->r_info);
1463 r_type = ELF32_R_TYPE (rel->r_info);
1465 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1467 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1472 if (r_symndx < symtab_hdr->sh_info)
1474 /* A local symbol. */
1475 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1480 /* Check relocation against local STT_GNU_IFUNC symbol. */
1481 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1483 h = elf_x86_64_get_local_sym_hash (htab, abfd, rel,
1488 /* Fake a STT_GNU_IFUNC symbol. */
1489 h->type = STT_GNU_IFUNC;
1492 h->forced_local = 1;
1493 h->root.type = bfd_link_hash_defined;
1501 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1502 while (h->root.type == bfd_link_hash_indirect
1503 || h->root.type == bfd_link_hash_warning)
1504 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1507 /* Check invalid x32 relocations. */
1508 if (!ABI_64_P (abfd))
1514 case R_X86_64_DTPOFF64:
1515 case R_X86_64_TPOFF64:
1517 case R_X86_64_GOTOFF64:
1518 case R_X86_64_GOT64:
1519 case R_X86_64_GOTPCREL64:
1520 case R_X86_64_GOTPC64:
1521 case R_X86_64_GOTPLT64:
1522 case R_X86_64_PLTOFF64:
1525 name = h->root.root.string;
1527 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1529 (*_bfd_error_handler)
1530 (_("%B: relocation %s against symbol `%s' isn't "
1531 "supported in x32 mode"), abfd,
1532 x86_64_elf_howto_table[r_type].name, name);
1533 bfd_set_error (bfd_error_bad_value);
1541 /* Create the ifunc sections for static executables. If we
1542 never see an indirect function symbol nor we are building
1543 a static executable, those sections will be empty and
1544 won't appear in output. */
1554 case R_X86_64_PC32_BND:
1556 case R_X86_64_PLT32:
1557 case R_X86_64_PLT32_BND:
1558 case R_X86_64_GOTPCREL:
1559 case R_X86_64_GOTPCREL64:
1560 if (htab->elf.dynobj == NULL)
1561 htab->elf.dynobj = abfd;
1562 if (!_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
1567 /* It is referenced by a non-shared object. */
1569 h->root.non_ir_ref = 1;
1572 if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
1573 symtab_hdr, sym_hashes,
1574 &r_type, GOT_UNKNOWN,
1575 rel, rel_end, h, r_symndx))
1580 case R_X86_64_TLSLD:
1581 htab->tls_ld_got.refcount += 1;
1584 case R_X86_64_TPOFF32:
1585 if (!info->executable && ABI_64_P (abfd))
1588 name = h->root.root.string;
1590 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1592 (*_bfd_error_handler)
1593 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1595 x86_64_elf_howto_table[r_type].name, name);
1596 bfd_set_error (bfd_error_bad_value);
1601 case R_X86_64_GOTTPOFF:
1602 if (!info->executable)
1603 info->flags |= DF_STATIC_TLS;
1606 case R_X86_64_GOT32:
1607 case R_X86_64_GOTPCREL:
1608 case R_X86_64_TLSGD:
1609 case R_X86_64_GOT64:
1610 case R_X86_64_GOTPCREL64:
1611 case R_X86_64_GOTPLT64:
1612 case R_X86_64_GOTPC32_TLSDESC:
1613 case R_X86_64_TLSDESC_CALL:
1614 /* This symbol requires a global offset table entry. */
1616 int tls_type, old_tls_type;
1620 default: tls_type = GOT_NORMAL; break;
1621 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1622 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1623 case R_X86_64_GOTPC32_TLSDESC:
1624 case R_X86_64_TLSDESC_CALL:
1625 tls_type = GOT_TLS_GDESC; break;
1630 if (r_type == R_X86_64_GOTPLT64)
1632 /* This relocation indicates that we also need
1633 a PLT entry, as this is a function. We don't need
1634 a PLT entry for local symbols. */
1636 h->plt.refcount += 1;
1638 h->got.refcount += 1;
1639 old_tls_type = elf_x86_64_hash_entry (h)->tls_type;
1643 bfd_signed_vma *local_got_refcounts;
1645 /* This is a global offset table entry for a local symbol. */
1646 local_got_refcounts = elf_local_got_refcounts (abfd);
1647 if (local_got_refcounts == NULL)
1651 size = symtab_hdr->sh_info;
1652 size *= sizeof (bfd_signed_vma)
1653 + sizeof (bfd_vma) + sizeof (char);
1654 local_got_refcounts = ((bfd_signed_vma *)
1655 bfd_zalloc (abfd, size));
1656 if (local_got_refcounts == NULL)
1658 elf_local_got_refcounts (abfd) = local_got_refcounts;
1659 elf_x86_64_local_tlsdesc_gotent (abfd)
1660 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1661 elf_x86_64_local_got_tls_type (abfd)
1662 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1664 local_got_refcounts[r_symndx] += 1;
1666 = elf_x86_64_local_got_tls_type (abfd) [r_symndx];
1669 /* If a TLS symbol is accessed using IE at least once,
1670 there is no point to use dynamic model for it. */
1671 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1672 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1673 || tls_type != GOT_TLS_IE))
1675 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1676 tls_type = old_tls_type;
1677 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1678 && GOT_TLS_GD_ANY_P (tls_type))
1679 tls_type |= old_tls_type;
1683 name = h->root.root.string;
1685 name = bfd_elf_sym_name (abfd, symtab_hdr,
1687 (*_bfd_error_handler)
1688 (_("%B: '%s' accessed both as normal and thread local symbol"),
1690 bfd_set_error (bfd_error_bad_value);
1695 if (old_tls_type != tls_type)
1698 elf_x86_64_hash_entry (h)->tls_type = tls_type;
1700 elf_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1705 case R_X86_64_GOTOFF64:
1706 case R_X86_64_GOTPC32:
1707 case R_X86_64_GOTPC64:
1709 if (htab->elf.sgot == NULL)
1711 if (htab->elf.dynobj == NULL)
1712 htab->elf.dynobj = abfd;
1713 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1719 case R_X86_64_PLT32:
1720 case R_X86_64_PLT32_BND:
1721 /* This symbol requires a procedure linkage table entry. We
1722 actually build the entry in adjust_dynamic_symbol,
1723 because this might be a case of linking PIC code which is
1724 never referenced by a dynamic object, in which case we
1725 don't need to generate a procedure linkage table entry
1728 /* If this is a local symbol, we resolve it directly without
1729 creating a procedure linkage table entry. */
1734 h->plt.refcount += 1;
1737 case R_X86_64_PLTOFF64:
1738 /* This tries to form the 'address' of a function relative
1739 to GOT. For global symbols we need a PLT entry. */
1743 h->plt.refcount += 1;
1747 case R_X86_64_SIZE32:
1748 case R_X86_64_SIZE64:
1753 if (!ABI_64_P (abfd))
1758 /* Let's help debug shared library creation. These relocs
1759 cannot be used in shared libs. Don't error out for
1760 sections we don't care about, such as debug sections or
1761 non-constant sections. */
1763 && (sec->flags & SEC_ALLOC) != 0
1764 && (sec->flags & SEC_READONLY) != 0)
1767 name = h->root.root.string;
1769 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1770 (*_bfd_error_handler)
1771 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1772 abfd, x86_64_elf_howto_table[r_type].name, name);
1773 bfd_set_error (bfd_error_bad_value);
1781 case R_X86_64_PC32_BND:
1785 if (h != NULL && info->executable)
1787 /* If this reloc is in a read-only section, we might
1788 need a copy reloc. We can't check reliably at this
1789 stage whether the section is read-only, as input
1790 sections have not yet been mapped to output sections.
1791 Tentatively set the flag for now, and correct in
1792 adjust_dynamic_symbol. */
1795 /* We may need a .plt entry if the function this reloc
1796 refers to is in a shared lib. */
1797 h->plt.refcount += 1;
1798 if (r_type != R_X86_64_PC32
1799 && r_type != R_X86_64_PC32_BND
1800 && r_type != R_X86_64_PC64)
1801 h->pointer_equality_needed = 1;
1806 /* If we are creating a shared library, and this is a reloc
1807 against a global symbol, or a non PC relative reloc
1808 against a local symbol, then we need to copy the reloc
1809 into the shared library. However, if we are linking with
1810 -Bsymbolic, we do not need to copy a reloc against a
1811 global symbol which is defined in an object we are
1812 including in the link (i.e., DEF_REGULAR is set). At
1813 this point we have not seen all the input files, so it is
1814 possible that DEF_REGULAR is not set now but will be set
1815 later (it is never cleared). In case of a weak definition,
1816 DEF_REGULAR may be cleared later by a strong definition in
1817 a shared library. We account for that possibility below by
1818 storing information in the relocs_copied field of the hash
1819 table entry. A similar situation occurs when creating
1820 shared libraries and symbol visibility changes render the
1823 If on the other hand, we are creating an executable, we
1824 may need to keep relocations for symbols satisfied by a
1825 dynamic library if we manage to avoid copy relocs for the
1828 && (sec->flags & SEC_ALLOC) != 0
1829 && (! IS_X86_64_PCREL_TYPE (r_type)
1831 && (! SYMBOLIC_BIND (info, h)
1832 || h->root.type == bfd_link_hash_defweak
1833 || !h->def_regular))))
1834 || (ELIMINATE_COPY_RELOCS
1836 && (sec->flags & SEC_ALLOC) != 0
1838 && (h->root.type == bfd_link_hash_defweak
1839 || !h->def_regular)))
1841 struct elf_dyn_relocs *p;
1842 struct elf_dyn_relocs **head;
1844 /* We must copy these reloc types into the output file.
1845 Create a reloc section in dynobj and make room for
1849 if (htab->elf.dynobj == NULL)
1850 htab->elf.dynobj = abfd;
1852 sreloc = _bfd_elf_make_dynamic_reloc_section
1853 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
1854 abfd, /*rela?*/ TRUE);
1860 /* If this is a global symbol, we count the number of
1861 relocations we need for this symbol. */
1864 head = &((struct elf_x86_64_link_hash_entry *) h)->dyn_relocs;
1868 /* Track dynamic relocs needed for local syms too.
1869 We really need local syms available to do this
1874 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1879 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1883 /* Beware of type punned pointers vs strict aliasing
1885 vpp = &(elf_section_data (s)->local_dynrel);
1886 head = (struct elf_dyn_relocs **)vpp;
1890 if (p == NULL || p->sec != sec)
1892 bfd_size_type amt = sizeof *p;
1894 p = ((struct elf_dyn_relocs *)
1895 bfd_alloc (htab->elf.dynobj, amt));
1906 /* Count size relocation as PC-relative relocation. */
1907 if (IS_X86_64_PCREL_TYPE (r_type) || size_reloc)
1912 /* This relocation describes the C++ object vtable hierarchy.
1913 Reconstruct it for later use during GC. */
1914 case R_X86_64_GNU_VTINHERIT:
1915 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1919 /* This relocation describes which C++ vtable entries are actually
1920 used. Record for later use during GC. */
1921 case R_X86_64_GNU_VTENTRY:
1922 BFD_ASSERT (h != NULL);
1924 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1936 /* Return the section that should be marked against GC for a given
1940 elf_x86_64_gc_mark_hook (asection *sec,
1941 struct bfd_link_info *info,
1942 Elf_Internal_Rela *rel,
1943 struct elf_link_hash_entry *h,
1944 Elf_Internal_Sym *sym)
1947 switch (ELF32_R_TYPE (rel->r_info))
1949 case R_X86_64_GNU_VTINHERIT:
1950 case R_X86_64_GNU_VTENTRY:
1954 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1957 /* Update the got entry reference counts for the section being removed. */
1960 elf_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1962 const Elf_Internal_Rela *relocs)
1964 struct elf_x86_64_link_hash_table *htab;
1965 Elf_Internal_Shdr *symtab_hdr;
1966 struct elf_link_hash_entry **sym_hashes;
1967 bfd_signed_vma *local_got_refcounts;
1968 const Elf_Internal_Rela *rel, *relend;
1970 if (info->relocatable)
1973 htab = elf_x86_64_hash_table (info);
1977 elf_section_data (sec)->local_dynrel = NULL;
1979 symtab_hdr = &elf_symtab_hdr (abfd);
1980 sym_hashes = elf_sym_hashes (abfd);
1981 local_got_refcounts = elf_local_got_refcounts (abfd);
1983 htab = elf_x86_64_hash_table (info);
1984 relend = relocs + sec->reloc_count;
1985 for (rel = relocs; rel < relend; rel++)
1987 unsigned long r_symndx;
1988 unsigned int r_type;
1989 struct elf_link_hash_entry *h = NULL;
1991 r_symndx = htab->r_sym (rel->r_info);
1992 if (r_symndx >= symtab_hdr->sh_info)
1994 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1995 while (h->root.type == bfd_link_hash_indirect
1996 || h->root.type == bfd_link_hash_warning)
1997 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2001 /* A local symbol. */
2002 Elf_Internal_Sym *isym;
2004 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2007 /* Check relocation against local STT_GNU_IFUNC symbol. */
2009 && ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
2011 h = elf_x86_64_get_local_sym_hash (htab, abfd, rel, FALSE);
2019 struct elf_x86_64_link_hash_entry *eh;
2020 struct elf_dyn_relocs **pp;
2021 struct elf_dyn_relocs *p;
2023 eh = (struct elf_x86_64_link_hash_entry *) h;
2025 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
2028 /* Everything must go for SEC. */
2034 r_type = ELF32_R_TYPE (rel->r_info);
2035 if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
2036 symtab_hdr, sym_hashes,
2037 &r_type, GOT_UNKNOWN,
2038 rel, relend, h, r_symndx))
2043 case R_X86_64_TLSLD:
2044 if (htab->tls_ld_got.refcount > 0)
2045 htab->tls_ld_got.refcount -= 1;
2048 case R_X86_64_TLSGD:
2049 case R_X86_64_GOTPC32_TLSDESC:
2050 case R_X86_64_TLSDESC_CALL:
2051 case R_X86_64_GOTTPOFF:
2052 case R_X86_64_GOT32:
2053 case R_X86_64_GOTPCREL:
2054 case R_X86_64_GOT64:
2055 case R_X86_64_GOTPCREL64:
2056 case R_X86_64_GOTPLT64:
2059 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
2060 h->plt.refcount -= 1;
2061 if (h->got.refcount > 0)
2062 h->got.refcount -= 1;
2063 if (h->type == STT_GNU_IFUNC)
2065 if (h->plt.refcount > 0)
2066 h->plt.refcount -= 1;
2069 else if (local_got_refcounts != NULL)
2071 if (local_got_refcounts[r_symndx] > 0)
2072 local_got_refcounts[r_symndx] -= 1;
2084 case R_X86_64_PC32_BND:
2086 case R_X86_64_SIZE32:
2087 case R_X86_64_SIZE64:
2089 && (h == NULL || h->type != STT_GNU_IFUNC))
2093 case R_X86_64_PLT32:
2094 case R_X86_64_PLT32_BND:
2095 case R_X86_64_PLTOFF64:
2098 if (h->plt.refcount > 0)
2099 h->plt.refcount -= 1;
2111 /* Adjust a symbol defined by a dynamic object and referenced by a
2112 regular object. The current definition is in some section of the
2113 dynamic object, but we're not including those sections. We have to
2114 change the definition to something the rest of the link can
2118 elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
2119 struct elf_link_hash_entry *h)
2121 struct elf_x86_64_link_hash_table *htab;
2123 struct elf_x86_64_link_hash_entry *eh;
2124 struct elf_dyn_relocs *p;
2126 /* STT_GNU_IFUNC symbol must go through PLT. */
2127 if (h->type == STT_GNU_IFUNC)
2129 /* All local STT_GNU_IFUNC references must be treate as local
2130 calls via local PLT. */
2132 && SYMBOL_CALLS_LOCAL (info, h))
2134 bfd_size_type pc_count = 0, count = 0;
2135 struct elf_dyn_relocs **pp;
2137 eh = (struct elf_x86_64_link_hash_entry *) h;
2138 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2140 pc_count += p->pc_count;
2141 p->count -= p->pc_count;
2150 if (pc_count || count)
2154 if (h->plt.refcount <= 0)
2155 h->plt.refcount = 1;
2157 h->plt.refcount += 1;
2161 if (h->plt.refcount <= 0)
2163 h->plt.offset = (bfd_vma) -1;
2169 /* If this is a function, put it in the procedure linkage table. We
2170 will fill in the contents of the procedure linkage table later,
2171 when we know the address of the .got section. */
2172 if (h->type == STT_FUNC
2175 if (h->plt.refcount <= 0
2176 || SYMBOL_CALLS_LOCAL (info, h)
2177 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2178 && h->root.type == bfd_link_hash_undefweak))
2180 /* This case can occur if we saw a PLT32 reloc in an input
2181 file, but the symbol was never referred to by a dynamic
2182 object, or if all references were garbage collected. In
2183 such a case, we don't actually need to build a procedure
2184 linkage table, and we can just do a PC32 reloc instead. */
2185 h->plt.offset = (bfd_vma) -1;
2192 /* It's possible that we incorrectly decided a .plt reloc was
2193 needed for an R_X86_64_PC32 reloc to a non-function sym in
2194 check_relocs. We can't decide accurately between function and
2195 non-function syms in check-relocs; Objects loaded later in
2196 the link may change h->type. So fix it now. */
2197 h->plt.offset = (bfd_vma) -1;
2199 /* If this is a weak symbol, and there is a real definition, the
2200 processor independent code will have arranged for us to see the
2201 real definition first, and we can just use the same value. */
2202 if (h->u.weakdef != NULL)
2204 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2205 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2206 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2207 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2208 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
2209 h->non_got_ref = h->u.weakdef->non_got_ref;
2213 /* This is a reference to a symbol defined by a dynamic object which
2214 is not a function. */
2216 /* If we are creating a shared library, we must presume that the
2217 only references to the symbol are via the global offset table.
2218 For such cases we need not do anything here; the relocations will
2219 be handled correctly by relocate_section. */
2223 /* If there are no references to this symbol that do not use the
2224 GOT, we don't need to generate a copy reloc. */
2225 if (!h->non_got_ref)
2228 /* If -z nocopyreloc was given, we won't generate them either. */
2229 if (info->nocopyreloc)
2235 if (ELIMINATE_COPY_RELOCS)
2237 eh = (struct elf_x86_64_link_hash_entry *) h;
2238 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2240 s = p->sec->output_section;
2241 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2245 /* If we didn't find any dynamic relocs in read-only sections, then
2246 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2254 /* We must allocate the symbol in our .dynbss section, which will
2255 become part of the .bss section of the executable. There will be
2256 an entry for this symbol in the .dynsym section. The dynamic
2257 object will contain position independent code, so all references
2258 from the dynamic object to this symbol will go through the global
2259 offset table. The dynamic linker will use the .dynsym entry to
2260 determine the address it must put in the global offset table, so
2261 both the dynamic object and the regular object will refer to the
2262 same memory location for the variable. */
2264 htab = elf_x86_64_hash_table (info);
2268 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
2269 to copy the initial value out of the dynamic object and into the
2270 runtime process image. */
2271 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2273 const struct elf_backend_data *bed;
2274 bed = get_elf_backend_data (info->output_bfd);
2275 htab->srelbss->size += bed->s->sizeof_rela;
2281 return _bfd_elf_adjust_dynamic_copy (h, s);
2284 /* Allocate space in .plt, .got and associated reloc sections for
2288 elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2290 struct bfd_link_info *info;
2291 struct elf_x86_64_link_hash_table *htab;
2292 struct elf_x86_64_link_hash_entry *eh;
2293 struct elf_dyn_relocs *p;
2294 const struct elf_backend_data *bed;
2295 unsigned int plt_entry_size;
2297 if (h->root.type == bfd_link_hash_indirect)
2300 eh = (struct elf_x86_64_link_hash_entry *) h;
2302 info = (struct bfd_link_info *) inf;
2303 htab = elf_x86_64_hash_table (info);
2306 bed = get_elf_backend_data (info->output_bfd);
2307 plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
2309 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2310 here if it is defined and referenced in a non-shared object. */
2311 if (h->type == STT_GNU_IFUNC
2313 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
2318 else if (htab->elf.dynamic_sections_created
2319 && h->plt.refcount > 0)
2321 /* Make sure this symbol is output as a dynamic symbol.
2322 Undefined weak syms won't yet be marked as dynamic. */
2323 if (h->dynindx == -1
2324 && !h->forced_local)
2326 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2331 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2333 asection *s = htab->elf.splt;
2335 /* If this is the first .plt entry, make room for the special
2338 s->size += plt_entry_size;
2340 h->plt.offset = s->size;
2342 /* If this symbol is not defined in a regular file, and we are
2343 not generating a shared library, then set the symbol to this
2344 location in the .plt. This is required to make function
2345 pointers compare as equal between the normal executable and
2346 the shared library. */
2350 h->root.u.def.section = s;
2351 h->root.u.def.value = h->plt.offset;
2354 /* Make room for this entry. */
2355 s->size += plt_entry_size;
2357 /* We also need to make an entry in the .got.plt section, which
2358 will be placed in the .got section by the linker script. */
2359 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
2361 /* We also need to make an entry in the .rela.plt section. */
2362 htab->elf.srelplt->size += bed->s->sizeof_rela;
2363 htab->elf.srelplt->reloc_count++;
2367 h->plt.offset = (bfd_vma) -1;
2373 h->plt.offset = (bfd_vma) -1;
2377 eh->tlsdesc_got = (bfd_vma) -1;
2379 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
2380 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
2381 if (h->got.refcount > 0
2384 && elf_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
2386 h->got.offset = (bfd_vma) -1;
2388 else if (h->got.refcount > 0)
2392 int tls_type = elf_x86_64_hash_entry (h)->tls_type;
2394 /* Make sure this symbol is output as a dynamic symbol.
2395 Undefined weak syms won't yet be marked as dynamic. */
2396 if (h->dynindx == -1
2397 && !h->forced_local)
2399 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2403 if (GOT_TLS_GDESC_P (tls_type))
2405 eh->tlsdesc_got = htab->elf.sgotplt->size
2406 - elf_x86_64_compute_jump_table_size (htab);
2407 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2408 h->got.offset = (bfd_vma) -2;
2410 if (! GOT_TLS_GDESC_P (tls_type)
2411 || GOT_TLS_GD_P (tls_type))
2414 h->got.offset = s->size;
2415 s->size += GOT_ENTRY_SIZE;
2416 if (GOT_TLS_GD_P (tls_type))
2417 s->size += GOT_ENTRY_SIZE;
2419 dyn = htab->elf.dynamic_sections_created;
2420 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2422 R_X86_64_GOTTPOFF needs one dynamic relocation. */
2423 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2424 || tls_type == GOT_TLS_IE)
2425 htab->elf.srelgot->size += bed->s->sizeof_rela;
2426 else if (GOT_TLS_GD_P (tls_type))
2427 htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
2428 else if (! GOT_TLS_GDESC_P (tls_type)
2429 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2430 || h->root.type != bfd_link_hash_undefweak)
2432 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2433 htab->elf.srelgot->size += bed->s->sizeof_rela;
2434 if (GOT_TLS_GDESC_P (tls_type))
2436 htab->elf.srelplt->size += bed->s->sizeof_rela;
2437 htab->tlsdesc_plt = (bfd_vma) -1;
2441 h->got.offset = (bfd_vma) -1;
2443 if (eh->dyn_relocs == NULL)
2446 /* In the shared -Bsymbolic case, discard space allocated for
2447 dynamic pc-relative relocs against symbols which turn out to be
2448 defined in regular objects. For the normal shared case, discard
2449 space for pc-relative relocs that have become local due to symbol
2450 visibility changes. */
2454 /* Relocs that use pc_count are those that appear on a call
2455 insn, or certain REL relocs that can generated via assembly.
2456 We want calls to protected symbols to resolve directly to the
2457 function rather than going via the plt. If people want
2458 function pointer comparisons to work as expected then they
2459 should avoid writing weird assembly. */
2460 if (SYMBOL_CALLS_LOCAL (info, h))
2462 struct elf_dyn_relocs **pp;
2464 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2466 p->count -= p->pc_count;
2475 /* Also discard relocs on undefined weak syms with non-default
2477 if (eh->dyn_relocs != NULL
2478 && h->root.type == bfd_link_hash_undefweak)
2480 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2481 eh->dyn_relocs = NULL;
2483 /* Make sure undefined weak symbols are output as a dynamic
2485 else if (h->dynindx == -1
2486 && ! h->forced_local
2487 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2492 else if (ELIMINATE_COPY_RELOCS)
2494 /* For the non-shared case, discard space for relocs against
2495 symbols which turn out to need copy relocs or are not
2501 || (htab->elf.dynamic_sections_created
2502 && (h->root.type == bfd_link_hash_undefweak
2503 || h->root.type == bfd_link_hash_undefined))))
2505 /* Make sure this symbol is output as a dynamic symbol.
2506 Undefined weak syms won't yet be marked as dynamic. */
2507 if (h->dynindx == -1
2508 && ! h->forced_local
2509 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2512 /* If that succeeded, we know we'll be keeping all the
2514 if (h->dynindx != -1)
2518 eh->dyn_relocs = NULL;
2523 /* Finally, allocate space. */
2524 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2528 sreloc = elf_section_data (p->sec)->sreloc;
2530 BFD_ASSERT (sreloc != NULL);
2532 sreloc->size += p->count * bed->s->sizeof_rela;
2538 /* Allocate space in .plt, .got and associated reloc sections for
2539 local dynamic relocs. */
2542 elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
2544 struct elf_link_hash_entry *h
2545 = (struct elf_link_hash_entry *) *slot;
2547 if (h->type != STT_GNU_IFUNC
2551 || h->root.type != bfd_link_hash_defined)
2554 return elf_x86_64_allocate_dynrelocs (h, inf);
2557 /* Find any dynamic relocs that apply to read-only sections. */
2560 elf_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h,
2563 struct elf_x86_64_link_hash_entry *eh;
2564 struct elf_dyn_relocs *p;
2566 /* Skip local IFUNC symbols. */
2567 if (h->forced_local && h->type == STT_GNU_IFUNC)
2570 eh = (struct elf_x86_64_link_hash_entry *) h;
2571 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2573 asection *s = p->sec->output_section;
2575 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2577 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2579 info->flags |= DF_TEXTREL;
2581 if (info->warn_shared_textrel && info->shared)
2582 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'.\n"),
2583 p->sec->owner, h->root.root.string,
2586 /* Not an error, just cut short the traversal. */
2594 mov foo@GOTPCREL(%rip), %reg
2597 with the local symbol, foo. */
2600 elf_x86_64_convert_mov_to_lea (bfd *abfd, asection *sec,
2601 struct bfd_link_info *link_info)
2603 Elf_Internal_Shdr *symtab_hdr;
2604 Elf_Internal_Rela *internal_relocs;
2605 Elf_Internal_Rela *irel, *irelend;
2607 struct elf_x86_64_link_hash_table *htab;
2608 bfd_boolean changed_contents;
2609 bfd_boolean changed_relocs;
2610 bfd_signed_vma *local_got_refcounts;
2612 /* Don't even try to convert non-ELF outputs. */
2613 if (!is_elf_hash_table (link_info->hash))
2616 /* Nothing to do if there are no codes, no relocations or no output. */
2617 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
2618 || sec->reloc_count == 0
2619 || discarded_section (sec))
2622 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2624 /* Load the relocations for this section. */
2625 internal_relocs = (_bfd_elf_link_read_relocs
2626 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
2627 link_info->keep_memory));
2628 if (internal_relocs == NULL)
2631 htab = elf_x86_64_hash_table (link_info);
2632 changed_contents = FALSE;
2633 changed_relocs = FALSE;
2634 local_got_refcounts = elf_local_got_refcounts (abfd);
2636 /* Get the section contents. */
2637 if (elf_section_data (sec)->this_hdr.contents != NULL)
2638 contents = elf_section_data (sec)->this_hdr.contents;
2641 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2645 irelend = internal_relocs + sec->reloc_count;
2646 for (irel = internal_relocs; irel < irelend; irel++)
2648 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
2649 unsigned int r_symndx = htab->r_sym (irel->r_info);
2651 struct elf_link_hash_entry *h;
2653 if (r_type != R_X86_64_GOTPCREL)
2656 /* Get the symbol referred to by the reloc. */
2657 if (r_symndx < symtab_hdr->sh_info)
2659 Elf_Internal_Sym *isym;
2661 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2664 /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation. */
2665 if (ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC
2666 && bfd_get_8 (input_bfd,
2667 contents + irel->r_offset - 2) == 0x8b)
2669 bfd_put_8 (output_bfd, 0x8d,
2670 contents + irel->r_offset - 2);
2671 irel->r_info = htab->r_info (r_symndx, R_X86_64_PC32);
2672 if (local_got_refcounts != NULL
2673 && local_got_refcounts[r_symndx] > 0)
2674 local_got_refcounts[r_symndx] -= 1;
2675 changed_contents = TRUE;
2676 changed_relocs = TRUE;
2681 indx = r_symndx - symtab_hdr->sh_info;
2682 h = elf_sym_hashes (abfd)[indx];
2683 BFD_ASSERT (h != NULL);
2685 while (h->root.type == bfd_link_hash_indirect
2686 || h->root.type == bfd_link_hash_warning)
2687 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2689 /* STT_GNU_IFUNC must keep R_X86_64_GOTPCREL relocation. We also
2690 avoid optimizing _DYNAMIC since ld.so may use its link-time
2693 && h->type != STT_GNU_IFUNC
2694 && h != htab->elf.hdynamic
2695 && SYMBOL_REFERENCES_LOCAL (link_info, h)
2696 && bfd_get_8 (input_bfd,
2697 contents + irel->r_offset - 2) == 0x8b)
2699 bfd_put_8 (output_bfd, 0x8d,
2700 contents + irel->r_offset - 2);
2701 irel->r_info = htab->r_info (r_symndx, R_X86_64_PC32);
2702 if (h->got.refcount > 0)
2703 h->got.refcount -= 1;
2704 changed_contents = TRUE;
2705 changed_relocs = TRUE;
2709 if (contents != NULL
2710 && elf_section_data (sec)->this_hdr.contents != contents)
2712 if (!changed_contents && !link_info->keep_memory)
2716 /* Cache the section contents for elf_link_input_bfd. */
2717 elf_section_data (sec)->this_hdr.contents = contents;
2721 if (elf_section_data (sec)->relocs != internal_relocs)
2723 if (!changed_relocs)
2724 free (internal_relocs);
2726 elf_section_data (sec)->relocs = internal_relocs;
2732 if (contents != NULL
2733 && elf_section_data (sec)->this_hdr.contents != contents)
2735 if (internal_relocs != NULL
2736 && elf_section_data (sec)->relocs != internal_relocs)
2737 free (internal_relocs);
2741 /* Set the sizes of the dynamic sections. */
2744 elf_x86_64_size_dynamic_sections (bfd *output_bfd,
2745 struct bfd_link_info *info)
2747 struct elf_x86_64_link_hash_table *htab;
2752 const struct elf_backend_data *bed;
2754 htab = elf_x86_64_hash_table (info);
2757 bed = get_elf_backend_data (output_bfd);
2759 dynobj = htab->elf.dynobj;
2763 if (htab->elf.dynamic_sections_created)
2765 /* Set the contents of the .interp section to the interpreter. */
2766 if (info->executable)
2768 s = bfd_get_linker_section (dynobj, ".interp");
2771 s->size = htab->dynamic_interpreter_size;
2772 s->contents = (unsigned char *) htab->dynamic_interpreter;
2776 /* Set up .got offsets for local syms, and space for local dynamic
2778 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2780 bfd_signed_vma *local_got;
2781 bfd_signed_vma *end_local_got;
2782 char *local_tls_type;
2783 bfd_vma *local_tlsdesc_gotent;
2784 bfd_size_type locsymcount;
2785 Elf_Internal_Shdr *symtab_hdr;
2788 if (! is_x86_64_elf (ibfd))
2791 for (s = ibfd->sections; s != NULL; s = s->next)
2793 struct elf_dyn_relocs *p;
2795 if (!elf_x86_64_convert_mov_to_lea (ibfd, s, info))
2798 for (p = (struct elf_dyn_relocs *)
2799 (elf_section_data (s)->local_dynrel);
2803 if (!bfd_is_abs_section (p->sec)
2804 && bfd_is_abs_section (p->sec->output_section))
2806 /* Input section has been discarded, either because
2807 it is a copy of a linkonce section or due to
2808 linker script /DISCARD/, so we'll be discarding
2811 else if (p->count != 0)
2813 srel = elf_section_data (p->sec)->sreloc;
2814 srel->size += p->count * bed->s->sizeof_rela;
2815 if ((p->sec->output_section->flags & SEC_READONLY) != 0
2816 && (info->flags & DF_TEXTREL) == 0)
2818 info->flags |= DF_TEXTREL;
2819 if (info->warn_shared_textrel && info->shared)
2820 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'.\n"),
2821 p->sec->owner, p->sec);
2827 local_got = elf_local_got_refcounts (ibfd);
2831 symtab_hdr = &elf_symtab_hdr (ibfd);
2832 locsymcount = symtab_hdr->sh_info;
2833 end_local_got = local_got + locsymcount;
2834 local_tls_type = elf_x86_64_local_got_tls_type (ibfd);
2835 local_tlsdesc_gotent = elf_x86_64_local_tlsdesc_gotent (ibfd);
2837 srel = htab->elf.srelgot;
2838 for (; local_got < end_local_got;
2839 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2841 *local_tlsdesc_gotent = (bfd_vma) -1;
2844 if (GOT_TLS_GDESC_P (*local_tls_type))
2846 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2847 - elf_x86_64_compute_jump_table_size (htab);
2848 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2849 *local_got = (bfd_vma) -2;
2851 if (! GOT_TLS_GDESC_P (*local_tls_type)
2852 || GOT_TLS_GD_P (*local_tls_type))
2854 *local_got = s->size;
2855 s->size += GOT_ENTRY_SIZE;
2856 if (GOT_TLS_GD_P (*local_tls_type))
2857 s->size += GOT_ENTRY_SIZE;
2860 || GOT_TLS_GD_ANY_P (*local_tls_type)
2861 || *local_tls_type == GOT_TLS_IE)
2863 if (GOT_TLS_GDESC_P (*local_tls_type))
2865 htab->elf.srelplt->size
2866 += bed->s->sizeof_rela;
2867 htab->tlsdesc_plt = (bfd_vma) -1;
2869 if (! GOT_TLS_GDESC_P (*local_tls_type)
2870 || GOT_TLS_GD_P (*local_tls_type))
2871 srel->size += bed->s->sizeof_rela;
2875 *local_got = (bfd_vma) -1;
2879 if (htab->tls_ld_got.refcount > 0)
2881 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2883 htab->tls_ld_got.offset = htab->elf.sgot->size;
2884 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
2885 htab->elf.srelgot->size += bed->s->sizeof_rela;
2888 htab->tls_ld_got.offset = -1;
2890 /* Allocate global sym .plt and .got entries, and space for global
2891 sym dynamic relocs. */
2892 elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
2895 /* Allocate .plt and .got entries, and space for local symbols. */
2896 htab_traverse (htab->loc_hash_table,
2897 elf_x86_64_allocate_local_dynrelocs,
2900 /* For every jump slot reserved in the sgotplt, reloc_count is
2901 incremented. However, when we reserve space for TLS descriptors,
2902 it's not incremented, so in order to compute the space reserved
2903 for them, it suffices to multiply the reloc count by the jump
2906 PR ld/13302: We start next_irelative_index at the end of .rela.plt
2907 so that R_X86_64_IRELATIVE entries come last. */
2908 if (htab->elf.srelplt)
2910 htab->sgotplt_jump_table_size
2911 = elf_x86_64_compute_jump_table_size (htab);
2912 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
2914 else if (htab->elf.irelplt)
2915 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
2917 if (htab->tlsdesc_plt)
2919 /* If we're not using lazy TLS relocations, don't generate the
2920 PLT and GOT entries they require. */
2921 if ((info->flags & DF_BIND_NOW))
2922 htab->tlsdesc_plt = 0;
2925 htab->tlsdesc_got = htab->elf.sgot->size;
2926 htab->elf.sgot->size += GOT_ENTRY_SIZE;
2927 /* Reserve room for the initial entry.
2928 FIXME: we could probably do away with it in this case. */
2929 if (htab->elf.splt->size == 0)
2930 htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
2931 htab->tlsdesc_plt = htab->elf.splt->size;
2932 htab->elf.splt->size += GET_PLT_ENTRY_SIZE (output_bfd);
2936 if (htab->elf.sgotplt)
2938 /* Don't allocate .got.plt section if there are no GOT nor PLT
2939 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
2940 if ((htab->elf.hgot == NULL
2941 || !htab->elf.hgot->ref_regular_nonweak)
2942 && (htab->elf.sgotplt->size
2943 == get_elf_backend_data (output_bfd)->got_header_size)
2944 && (htab->elf.splt == NULL
2945 || htab->elf.splt->size == 0)
2946 && (htab->elf.sgot == NULL
2947 || htab->elf.sgot->size == 0)
2948 && (htab->elf.iplt == NULL
2949 || htab->elf.iplt->size == 0)
2950 && (htab->elf.igotplt == NULL
2951 || htab->elf.igotplt->size == 0))
2952 htab->elf.sgotplt->size = 0;
2955 if (htab->plt_eh_frame != NULL
2956 && htab->elf.splt != NULL
2957 && htab->elf.splt->size != 0
2958 && !bfd_is_abs_section (htab->elf.splt->output_section)
2959 && _bfd_elf_eh_frame_present (info))
2961 const struct elf_x86_64_backend_data *arch_data
2962 = get_elf_x86_64_arch_data (bed);
2963 htab->plt_eh_frame->size = arch_data->eh_frame_plt_size;
2966 /* We now have determined the sizes of the various dynamic sections.
2967 Allocate memory for them. */
2969 for (s = dynobj->sections; s != NULL; s = s->next)
2971 if ((s->flags & SEC_LINKER_CREATED) == 0)
2974 if (s == htab->elf.splt
2975 || s == htab->elf.sgot
2976 || s == htab->elf.sgotplt
2977 || s == htab->elf.iplt
2978 || s == htab->elf.igotplt
2979 || s == htab->plt_eh_frame
2980 || s == htab->sdynbss)
2982 /* Strip this section if we don't need it; see the
2985 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2987 if (s->size != 0 && s != htab->elf.srelplt)
2990 /* We use the reloc_count field as a counter if we need
2991 to copy relocs into the output file. */
2992 if (s != htab->elf.srelplt)
2997 /* It's not one of our sections, so don't allocate space. */
3003 /* If we don't need this section, strip it from the
3004 output file. This is mostly to handle .rela.bss and
3005 .rela.plt. We must create both sections in
3006 create_dynamic_sections, because they must be created
3007 before the linker maps input sections to output
3008 sections. The linker does that before
3009 adjust_dynamic_symbol is called, and it is that
3010 function which decides whether anything needs to go
3011 into these sections. */
3013 s->flags |= SEC_EXCLUDE;
3017 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3020 /* Allocate memory for the section contents. We use bfd_zalloc
3021 here in case unused entries are not reclaimed before the
3022 section's contents are written out. This should not happen,
3023 but this way if it does, we get a R_X86_64_NONE reloc instead
3025 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3026 if (s->contents == NULL)
3030 if (htab->plt_eh_frame != NULL
3031 && htab->plt_eh_frame->contents != NULL)
3033 const struct elf_x86_64_backend_data *arch_data
3034 = get_elf_x86_64_arch_data (bed);
3036 memcpy (htab->plt_eh_frame->contents,
3037 arch_data->eh_frame_plt, htab->plt_eh_frame->size);
3038 bfd_put_32 (dynobj, htab->elf.splt->size,
3039 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3042 if (htab->elf.dynamic_sections_created)
3044 /* Add some entries to the .dynamic section. We fill in the
3045 values later, in elf_x86_64_finish_dynamic_sections, but we
3046 must add the entries now so that we get the correct size for
3047 the .dynamic section. The DT_DEBUG entry is filled in by the
3048 dynamic linker and used by the debugger. */
3049 #define add_dynamic_entry(TAG, VAL) \
3050 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3052 if (info->executable)
3054 if (!add_dynamic_entry (DT_DEBUG, 0))
3058 if (htab->elf.splt->size != 0)
3060 if (!add_dynamic_entry (DT_PLTGOT, 0)
3061 || !add_dynamic_entry (DT_PLTRELSZ, 0)
3062 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3063 || !add_dynamic_entry (DT_JMPREL, 0))
3066 if (htab->tlsdesc_plt
3067 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
3068 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
3074 if (!add_dynamic_entry (DT_RELA, 0)
3075 || !add_dynamic_entry (DT_RELASZ, 0)
3076 || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
3079 /* If any dynamic relocs apply to a read-only section,
3080 then we need a DT_TEXTREL entry. */
3081 if ((info->flags & DF_TEXTREL) == 0)
3082 elf_link_hash_traverse (&htab->elf,
3083 elf_x86_64_readonly_dynrelocs,
3086 if ((info->flags & DF_TEXTREL) != 0)
3088 if (!add_dynamic_entry (DT_TEXTREL, 0))
3093 #undef add_dynamic_entry
3099 elf_x86_64_always_size_sections (bfd *output_bfd,
3100 struct bfd_link_info *info)
3102 asection *tls_sec = elf_hash_table (info)->tls_sec;
3106 struct elf_link_hash_entry *tlsbase;
3108 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
3109 "_TLS_MODULE_BASE_",
3110 FALSE, FALSE, FALSE);
3112 if (tlsbase && tlsbase->type == STT_TLS)
3114 struct elf_x86_64_link_hash_table *htab;
3115 struct bfd_link_hash_entry *bh = NULL;
3116 const struct elf_backend_data *bed
3117 = get_elf_backend_data (output_bfd);
3119 htab = elf_x86_64_hash_table (info);
3123 if (!(_bfd_generic_link_add_one_symbol
3124 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
3125 tls_sec, 0, NULL, FALSE,
3126 bed->collect, &bh)))
3129 htab->tls_module_base = bh;
3131 tlsbase = (struct elf_link_hash_entry *)bh;
3132 tlsbase->def_regular = 1;
3133 tlsbase->other = STV_HIDDEN;
3134 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
3141 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
3142 executables. Rather than setting it to the beginning of the TLS
3143 section, we have to set it to the end. This function may be called
3144 multiple times, it is idempotent. */
3147 elf_x86_64_set_tls_module_base (struct bfd_link_info *info)
3149 struct elf_x86_64_link_hash_table *htab;
3150 struct bfd_link_hash_entry *base;
3152 if (!info->executable)
3155 htab = elf_x86_64_hash_table (info);
3159 base = htab->tls_module_base;
3163 base->u.def.value = htab->elf.tls_size;
3166 /* Return the base VMA address which should be subtracted from real addresses
3167 when resolving @dtpoff relocation.
3168 This is PT_TLS segment p_vaddr. */
3171 elf_x86_64_dtpoff_base (struct bfd_link_info *info)
3173 /* If tls_sec is NULL, we should have signalled an error already. */
3174 if (elf_hash_table (info)->tls_sec == NULL)
3176 return elf_hash_table (info)->tls_sec->vma;
3179 /* Return the relocation value for @tpoff relocation
3180 if STT_TLS virtual address is ADDRESS. */
3183 elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
3185 struct elf_link_hash_table *htab = elf_hash_table (info);
3186 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3187 bfd_vma static_tls_size;
3189 /* If tls_segment is NULL, we should have signalled an error already. */
3190 if (htab->tls_sec == NULL)
3193 /* Consider special static TLS alignment requirements. */
3194 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3195 return address - static_tls_size - htab->tls_sec->vma;
3198 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
3202 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
3204 /* Opcode Instruction
3207 0x0f 0x8x conditional jump */
3209 && (contents [offset - 1] == 0xe8
3210 || contents [offset - 1] == 0xe9))
3212 && contents [offset - 2] == 0x0f
3213 && (contents [offset - 1] & 0xf0) == 0x80));
3216 /* Relocate an x86_64 ELF section. */
3219 elf_x86_64_relocate_section (bfd *output_bfd,
3220 struct bfd_link_info *info,
3222 asection *input_section,
3224 Elf_Internal_Rela *relocs,
3225 Elf_Internal_Sym *local_syms,
3226 asection **local_sections)
3228 struct elf_x86_64_link_hash_table *htab;
3229 Elf_Internal_Shdr *symtab_hdr;
3230 struct elf_link_hash_entry **sym_hashes;
3231 bfd_vma *local_got_offsets;
3232 bfd_vma *local_tlsdesc_gotents;
3233 Elf_Internal_Rela *rel;
3234 Elf_Internal_Rela *relend;
3235 const unsigned int plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd);
3237 BFD_ASSERT (is_x86_64_elf (input_bfd));
3239 htab = elf_x86_64_hash_table (info);
3242 symtab_hdr = &elf_symtab_hdr (input_bfd);
3243 sym_hashes = elf_sym_hashes (input_bfd);
3244 local_got_offsets = elf_local_got_offsets (input_bfd);
3245 local_tlsdesc_gotents = elf_x86_64_local_tlsdesc_gotent (input_bfd);
3247 elf_x86_64_set_tls_module_base (info);
3250 relend = relocs + input_section->reloc_count;
3251 for (; rel < relend; rel++)
3253 unsigned int r_type;
3254 reloc_howto_type *howto;
3255 unsigned long r_symndx;
3256 struct elf_link_hash_entry *h;
3257 Elf_Internal_Sym *sym;
3259 bfd_vma off, offplt;
3261 bfd_boolean unresolved_reloc;
3262 bfd_reloc_status_type r;
3267 r_type = ELF32_R_TYPE (rel->r_info);
3268 if (r_type == (int) R_X86_64_GNU_VTINHERIT
3269 || r_type == (int) R_X86_64_GNU_VTENTRY)
3272 if (r_type >= (int) R_X86_64_standard)
3274 (*_bfd_error_handler)
3275 (_("%B: unrecognized relocation (0x%x) in section `%A'"),
3276 input_bfd, input_section, r_type);
3277 bfd_set_error (bfd_error_bad_value);
3281 if (r_type != (int) R_X86_64_32
3282 || ABI_64_P (output_bfd))
3283 howto = x86_64_elf_howto_table + r_type;
3285 howto = (x86_64_elf_howto_table
3286 + ARRAY_SIZE (x86_64_elf_howto_table) - 1);
3287 r_symndx = htab->r_sym (rel->r_info);
3291 unresolved_reloc = FALSE;
3292 if (r_symndx < symtab_hdr->sh_info)
3294 sym = local_syms + r_symndx;
3295 sec = local_sections[r_symndx];
3297 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
3299 st_size = sym->st_size;
3301 /* Relocate against local STT_GNU_IFUNC symbol. */
3302 if (!info->relocatable
3303 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3305 h = elf_x86_64_get_local_sym_hash (htab, input_bfd,
3310 /* Set STT_GNU_IFUNC symbol value. */
3311 h->root.u.def.value = sym->st_value;
3312 h->root.u.def.section = sec;
3317 bfd_boolean warned ATTRIBUTE_UNUSED;
3319 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3320 r_symndx, symtab_hdr, sym_hashes,
3322 unresolved_reloc, warned);
3326 if (sec != NULL && discarded_section (sec))
3327 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3328 rel, 1, relend, howto, 0, contents);
3330 if (info->relocatable)
3333 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
3335 if (r_type == R_X86_64_64)
3337 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
3338 zero-extend it to 64bit if addend is zero. */
3339 r_type = R_X86_64_32;
3340 memset (contents + rel->r_offset + 4, 0, 4);
3342 else if (r_type == R_X86_64_SIZE64)
3344 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
3345 zero-extend it to 64bit if addend is zero. */
3346 r_type = R_X86_64_SIZE32;
3347 memset (contents + rel->r_offset + 4, 0, 4);
3351 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3352 it here if it is defined in a non-shared object. */
3354 && h->type == STT_GNU_IFUNC
3361 if ((input_section->flags & SEC_ALLOC) == 0
3362 || h->plt.offset == (bfd_vma) -1)
3365 /* STT_GNU_IFUNC symbol must go through PLT. */
3366 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3367 relocation = (plt->output_section->vma
3368 + plt->output_offset + h->plt.offset);
3373 if (h->root.root.string)
3374 name = h->root.root.string;
3376 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3378 (*_bfd_error_handler)
3379 (_("%B: relocation %s against STT_GNU_IFUNC "
3380 "symbol `%s' isn't handled by %s"), input_bfd,
3381 x86_64_elf_howto_table[r_type].name,
3382 name, __FUNCTION__);
3383 bfd_set_error (bfd_error_bad_value);
3392 if (ABI_64_P (output_bfd))
3396 if (rel->r_addend != 0)
3398 if (h->root.root.string)
3399 name = h->root.root.string;
3401 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3403 (*_bfd_error_handler)
3404 (_("%B: relocation %s against STT_GNU_IFUNC "
3405 "symbol `%s' has non-zero addend: %d"),
3406 input_bfd, x86_64_elf_howto_table[r_type].name,
3407 name, rel->r_addend);
3408 bfd_set_error (bfd_error_bad_value);
3412 /* Generate dynamic relcoation only when there is a
3413 non-GOT reference in a shared object. */
3414 if (info->shared && h->non_got_ref)
3416 Elf_Internal_Rela outrel;
3419 /* Need a dynamic relocation to get the real function
3421 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3425 if (outrel.r_offset == (bfd_vma) -1
3426 || outrel.r_offset == (bfd_vma) -2)
3429 outrel.r_offset += (input_section->output_section->vma
3430 + input_section->output_offset);
3432 if (h->dynindx == -1
3434 || info->executable)
3436 /* This symbol is resolved locally. */
3437 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
3438 outrel.r_addend = (h->root.u.def.value
3439 + h->root.u.def.section->output_section->vma
3440 + h->root.u.def.section->output_offset);
3444 outrel.r_info = htab->r_info (h->dynindx, r_type);
3445 outrel.r_addend = 0;
3448 sreloc = htab->elf.irelifunc;
3449 elf_append_rela (output_bfd, sreloc, &outrel);
3451 /* If this reloc is against an external symbol, we
3452 do not want to fiddle with the addend. Otherwise,
3453 we need to include the symbol value so that it
3454 becomes an addend for the dynamic reloc. For an
3455 internal symbol, we have updated addend. */
3460 case R_X86_64_PC32_BND:
3462 case R_X86_64_PLT32:
3463 case R_X86_64_PLT32_BND:
3466 case R_X86_64_GOTPCREL:
3467 case R_X86_64_GOTPCREL64:
3468 base_got = htab->elf.sgot;
3469 off = h->got.offset;
3471 if (base_got == NULL)
3474 if (off == (bfd_vma) -1)
3476 /* We can't use h->got.offset here to save state, or
3477 even just remember the offset, as finish_dynamic_symbol
3478 would use that as offset into .got. */
3480 if (htab->elf.splt != NULL)
3482 plt_index = h->plt.offset / plt_entry_size - 1;
3483 off = (plt_index + 3) * GOT_ENTRY_SIZE;
3484 base_got = htab->elf.sgotplt;
3488 plt_index = h->plt.offset / plt_entry_size;
3489 off = plt_index * GOT_ENTRY_SIZE;
3490 base_got = htab->elf.igotplt;
3493 if (h->dynindx == -1
3497 /* This references the local defitionion. We must
3498 initialize this entry in the global offset table.
3499 Since the offset must always be a multiple of 8,
3500 we use the least significant bit to record
3501 whether we have initialized it already.
3503 When doing a dynamic link, we create a .rela.got
3504 relocation entry to initialize the value. This
3505 is done in the finish_dynamic_symbol routine. */
3510 bfd_put_64 (output_bfd, relocation,
3511 base_got->contents + off);
3512 /* Note that this is harmless for the GOTPLT64
3513 case, as -1 | 1 still is -1. */
3519 relocation = (base_got->output_section->vma
3520 + base_got->output_offset + off);
3526 /* When generating a shared object, the relocations handled here are
3527 copied into the output file to be resolved at run time. */
3530 case R_X86_64_GOT32:
3531 case R_X86_64_GOT64:
3532 /* Relocation is to the entry for this symbol in the global
3534 case R_X86_64_GOTPCREL:
3535 case R_X86_64_GOTPCREL64:
3536 /* Use global offset table entry as symbol value. */
3537 case R_X86_64_GOTPLT64:
3538 /* This is the same as GOT64 for relocation purposes, but
3539 indicates the existence of a PLT entry. The difficulty is,
3540 that we must calculate the GOT slot offset from the PLT
3541 offset, if this symbol got a PLT entry (it was global).
3542 Additionally if it's computed from the PLT entry, then that
3543 GOT offset is relative to .got.plt, not to .got. */
3544 base_got = htab->elf.sgot;
3546 if (htab->elf.sgot == NULL)
3553 off = h->got.offset;
3555 && h->plt.offset != (bfd_vma)-1
3556 && off == (bfd_vma)-1)
3558 /* We can't use h->got.offset here to save
3559 state, or even just remember the offset, as
3560 finish_dynamic_symbol would use that as offset into
3562 bfd_vma plt_index = h->plt.offset / plt_entry_size - 1;
3563 off = (plt_index + 3) * GOT_ENTRY_SIZE;
3564 base_got = htab->elf.sgotplt;
3567 dyn = htab->elf.dynamic_sections_created;
3569 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3571 && SYMBOL_REFERENCES_LOCAL (info, h))
3572 || (ELF_ST_VISIBILITY (h->other)
3573 && h->root.type == bfd_link_hash_undefweak))
3575 /* This is actually a static link, or it is a -Bsymbolic
3576 link and the symbol is defined locally, or the symbol
3577 was forced to be local because of a version file. We
3578 must initialize this entry in the global offset table.
3579 Since the offset must always be a multiple of 8, we
3580 use the least significant bit to record whether we
3581 have initialized it already.
3583 When doing a dynamic link, we create a .rela.got
3584 relocation entry to initialize the value. This is
3585 done in the finish_dynamic_symbol routine. */
3590 bfd_put_64 (output_bfd, relocation,
3591 base_got->contents + off);
3592 /* Note that this is harmless for the GOTPLT64 case,
3593 as -1 | 1 still is -1. */
3598 unresolved_reloc = FALSE;
3602 if (local_got_offsets == NULL)
3605 off = local_got_offsets[r_symndx];
3607 /* The offset must always be a multiple of 8. We use
3608 the least significant bit to record whether we have
3609 already generated the necessary reloc. */
3614 bfd_put_64 (output_bfd, relocation,
3615 base_got->contents + off);
3620 Elf_Internal_Rela outrel;
3622 /* We need to generate a R_X86_64_RELATIVE reloc
3623 for the dynamic linker. */
3624 s = htab->elf.srelgot;
3628 outrel.r_offset = (base_got->output_section->vma
3629 + base_got->output_offset
3631 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3632 outrel.r_addend = relocation;
3633 elf_append_rela (output_bfd, s, &outrel);
3636 local_got_offsets[r_symndx] |= 1;
3640 if (off >= (bfd_vma) -2)
3643 relocation = base_got->output_section->vma
3644 + base_got->output_offset + off;
3645 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
3646 relocation -= htab->elf.sgotplt->output_section->vma
3647 - htab->elf.sgotplt->output_offset;
3651 case R_X86_64_GOTOFF64:
3652 /* Relocation is relative to the start of the global offset
3655 /* Check to make sure it isn't a protected function symbol
3656 for shared library since it may not be local when used
3657 as function address. */
3658 if (!info->executable
3660 && !SYMBOLIC_BIND (info, h)
3662 && h->type == STT_FUNC
3663 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3665 (*_bfd_error_handler)
3666 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3667 input_bfd, h->root.root.string);
3668 bfd_set_error (bfd_error_bad_value);
3672 /* Note that sgot is not involved in this
3673 calculation. We always want the start of .got.plt. If we
3674 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3675 permitted by the ABI, we might have to change this
3677 relocation -= htab->elf.sgotplt->output_section->vma
3678 + htab->elf.sgotplt->output_offset;
3681 case R_X86_64_GOTPC32:
3682 case R_X86_64_GOTPC64:
3683 /* Use global offset table as symbol value. */
3684 relocation = htab->elf.sgotplt->output_section->vma
3685 + htab->elf.sgotplt->output_offset;
3686 unresolved_reloc = FALSE;
3689 case R_X86_64_PLTOFF64:
3690 /* Relocation is PLT entry relative to GOT. For local
3691 symbols it's the symbol itself relative to GOT. */
3693 /* See PLT32 handling. */
3694 && h->plt.offset != (bfd_vma) -1
3695 && htab->elf.splt != NULL)
3697 relocation = (htab->elf.splt->output_section->vma
3698 + htab->elf.splt->output_offset
3700 unresolved_reloc = FALSE;
3703 relocation -= htab->elf.sgotplt->output_section->vma
3704 + htab->elf.sgotplt->output_offset;
3707 case R_X86_64_PLT32:
3708 case R_X86_64_PLT32_BND:
3709 /* Relocation is to the entry for this symbol in the
3710 procedure linkage table. */
3712 /* Resolve a PLT32 reloc against a local symbol directly,
3713 without using the procedure linkage table. */
3717 if (h->plt.offset == (bfd_vma) -1
3718 || htab->elf.splt == NULL)
3720 /* We didn't make a PLT entry for this symbol. This
3721 happens when statically linking PIC code, or when
3722 using -Bsymbolic. */
3726 relocation = (htab->elf.splt->output_section->vma
3727 + htab->elf.splt->output_offset
3729 unresolved_reloc = FALSE;
3732 case R_X86_64_SIZE32:
3733 case R_X86_64_SIZE64:
3734 /* Set to symbol size. */
3735 relocation = st_size;
3741 case R_X86_64_PC32_BND:
3743 && (input_section->flags & SEC_ALLOC) != 0
3744 && (input_section->flags & SEC_READONLY) != 0
3747 bfd_boolean fail = FALSE;
3749 = ((r_type == R_X86_64_PC32
3750 || r_type == R_X86_64_PC32_BND)
3751 && is_32bit_relative_branch (contents, rel->r_offset));
3753 if (SYMBOL_REFERENCES_LOCAL (info, h))
3755 /* Symbol is referenced locally. Make sure it is
3756 defined locally or for a branch. */
3757 fail = !h->def_regular && !branch;
3761 /* Symbol isn't referenced locally. We only allow
3762 branch to symbol with non-default visibility. */
3764 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3771 const char *pic = "";
3773 switch (ELF_ST_VISIBILITY (h->other))
3776 v = _("hidden symbol");
3779 v = _("internal symbol");
3782 v = _("protected symbol");
3786 pic = _("; recompile with -fPIC");
3791 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3793 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3795 (*_bfd_error_handler) (fmt, input_bfd,
3796 x86_64_elf_howto_table[r_type].name,
3797 v, h->root.root.string, pic);
3798 bfd_set_error (bfd_error_bad_value);
3809 /* FIXME: The ABI says the linker should make sure the value is
3810 the same when it's zeroextended to 64 bit. */
3813 if ((input_section->flags & SEC_ALLOC) == 0)
3818 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3819 || h->root.type != bfd_link_hash_undefweak)
3820 && ((! IS_X86_64_PCREL_TYPE (r_type)
3821 && r_type != R_X86_64_SIZE32
3822 && r_type != R_X86_64_SIZE64)
3823 || ! SYMBOL_CALLS_LOCAL (info, h)))
3824 || (ELIMINATE_COPY_RELOCS
3831 || h->root.type == bfd_link_hash_undefweak
3832 || h->root.type == bfd_link_hash_undefined)))
3834 Elf_Internal_Rela outrel;
3835 bfd_boolean skip, relocate;
3838 /* When generating a shared object, these relocations
3839 are copied into the output file to be resolved at run
3845 _bfd_elf_section_offset (output_bfd, info, input_section,
3847 if (outrel.r_offset == (bfd_vma) -1)
3849 else if (outrel.r_offset == (bfd_vma) -2)
3850 skip = TRUE, relocate = TRUE;
3852 outrel.r_offset += (input_section->output_section->vma
3853 + input_section->output_offset);
3856 memset (&outrel, 0, sizeof outrel);
3858 /* h->dynindx may be -1 if this symbol was marked to
3862 && (IS_X86_64_PCREL_TYPE (r_type)
3864 || ! SYMBOLIC_BIND (info, h)
3865 || ! h->def_regular))
3867 outrel.r_info = htab->r_info (h->dynindx, r_type);
3868 outrel.r_addend = rel->r_addend;
3872 /* This symbol is local, or marked to become local. */
3873 if (r_type == htab->pointer_r_type)
3876 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3877 outrel.r_addend = relocation + rel->r_addend;
3879 else if (r_type == R_X86_64_64
3880 && !ABI_64_P (output_bfd))
3883 outrel.r_info = htab->r_info (0,
3884 R_X86_64_RELATIVE64);
3885 outrel.r_addend = relocation + rel->r_addend;
3886 /* Check addend overflow. */
3887 if ((outrel.r_addend & 0x80000000)
3888 != (rel->r_addend & 0x80000000))
3891 int addend = rel->r_addend;
3892 if (h && h->root.root.string)
3893 name = h->root.root.string;
3895 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3898 (*_bfd_error_handler)
3899 (_("%B: addend -0x%x in relocation %s against "
3900 "symbol `%s' at 0x%lx in section `%A' is "
3902 input_bfd, input_section, addend,
3903 x86_64_elf_howto_table[r_type].name,
3904 name, (unsigned long) rel->r_offset);
3906 (*_bfd_error_handler)
3907 (_("%B: addend 0x%x in relocation %s against "
3908 "symbol `%s' at 0x%lx in section `%A' is "
3910 input_bfd, input_section, addend,
3911 x86_64_elf_howto_table[r_type].name,
3912 name, (unsigned long) rel->r_offset);
3913 bfd_set_error (bfd_error_bad_value);
3921 if (bfd_is_abs_section (sec))
3923 else if (sec == NULL || sec->owner == NULL)
3925 bfd_set_error (bfd_error_bad_value);
3932 /* We are turning this relocation into one
3933 against a section symbol. It would be
3934 proper to subtract the symbol's value,
3935 osec->vma, from the emitted reloc addend,
3936 but ld.so expects buggy relocs. */
3937 osec = sec->output_section;
3938 sindx = elf_section_data (osec)->dynindx;
3941 asection *oi = htab->elf.text_index_section;
3942 sindx = elf_section_data (oi)->dynindx;
3944 BFD_ASSERT (sindx != 0);
3947 outrel.r_info = htab->r_info (sindx, r_type);
3948 outrel.r_addend = relocation + rel->r_addend;
3952 sreloc = elf_section_data (input_section)->sreloc;
3954 if (sreloc == NULL || sreloc->contents == NULL)
3956 r = bfd_reloc_notsupported;
3957 goto check_relocation_error;
3960 elf_append_rela (output_bfd, sreloc, &outrel);
3962 /* If this reloc is against an external symbol, we do
3963 not want to fiddle with the addend. Otherwise, we
3964 need to include the symbol value so that it becomes
3965 an addend for the dynamic reloc. */
3972 case R_X86_64_TLSGD:
3973 case R_X86_64_GOTPC32_TLSDESC:
3974 case R_X86_64_TLSDESC_CALL:
3975 case R_X86_64_GOTTPOFF:
3976 tls_type = GOT_UNKNOWN;
3977 if (h == NULL && local_got_offsets)
3978 tls_type = elf_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3980 tls_type = elf_x86_64_hash_entry (h)->tls_type;
3982 if (! elf_x86_64_tls_transition (info, input_bfd,
3983 input_section, contents,
3984 symtab_hdr, sym_hashes,
3985 &r_type, tls_type, rel,
3986 relend, h, r_symndx))
3989 if (r_type == R_X86_64_TPOFF32)
3991 bfd_vma roff = rel->r_offset;
3993 BFD_ASSERT (! unresolved_reloc);
3995 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3997 /* GD->LE transition. For 64bit, change
3998 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3999 .word 0x6666; rex64; call __tls_get_addr
4002 leaq foo@tpoff(%rax), %rax
4004 leaq foo@tlsgd(%rip), %rdi
4005 .word 0x6666; rex64; call __tls_get_addr
4008 leaq foo@tpoff(%rax), %rax
4009 For largepic, change:
4010 leaq foo@tlsgd(%rip), %rdi
4011 movabsq $__tls_get_addr@pltoff, %rax
4016 leaq foo@tpoff(%rax), %rax
4017 nopw 0x0(%rax,%rax,1) */
4019 if (ABI_64_P (output_bfd)
4020 && contents[roff + 5] == (bfd_byte) '\xb8')
4022 memcpy (contents + roff - 3,
4023 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
4024 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
4027 else if (ABI_64_P (output_bfd))
4028 memcpy (contents + roff - 4,
4029 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4032 memcpy (contents + roff - 3,
4033 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4035 bfd_put_32 (output_bfd,
4036 elf_x86_64_tpoff (info, relocation),
4037 contents + roff + 8 + largepic);
4038 /* Skip R_X86_64_PC32/R_X86_64_PLT32/R_X86_64_PLTOFF64. */
4042 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
4044 /* GDesc -> LE transition.
4045 It's originally something like:
4046 leaq x@tlsdesc(%rip), %rax
4049 movl $x@tpoff, %rax. */
4051 unsigned int val, type;
4053 type = bfd_get_8 (input_bfd, contents + roff - 3);
4054 val = bfd_get_8 (input_bfd, contents + roff - 1);
4055 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
4056 contents + roff - 3);
4057 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
4058 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4059 contents + roff - 1);
4060 bfd_put_32 (output_bfd,
4061 elf_x86_64_tpoff (info, relocation),
4065 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
4067 /* GDesc -> LE transition.
4072 bfd_put_8 (output_bfd, 0x66, contents + roff);
4073 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4076 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
4078 /* IE->LE transition:
4079 Originally it can be one of:
4080 movq foo@gottpoff(%rip), %reg
4081 addq foo@gottpoff(%rip), %reg
4084 leaq foo(%reg), %reg
4087 unsigned int val, type, reg;
4089 val = bfd_get_8 (input_bfd, contents + roff - 3);
4090 type = bfd_get_8 (input_bfd, contents + roff - 2);
4091 reg = bfd_get_8 (input_bfd, contents + roff - 1);
4097 bfd_put_8 (output_bfd, 0x49,
4098 contents + roff - 3);
4099 else if (!ABI_64_P (output_bfd) && val == 0x44)
4100 bfd_put_8 (output_bfd, 0x41,
4101 contents + roff - 3);
4102 bfd_put_8 (output_bfd, 0xc7,
4103 contents + roff - 2);
4104 bfd_put_8 (output_bfd, 0xc0 | reg,
4105 contents + roff - 1);
4109 /* addq -> addq - addressing with %rsp/%r12 is
4112 bfd_put_8 (output_bfd, 0x49,
4113 contents + roff - 3);
4114 else if (!ABI_64_P (output_bfd) && val == 0x44)
4115 bfd_put_8 (output_bfd, 0x41,
4116 contents + roff - 3);
4117 bfd_put_8 (output_bfd, 0x81,
4118 contents + roff - 2);
4119 bfd_put_8 (output_bfd, 0xc0 | reg,
4120 contents + roff - 1);
4126 bfd_put_8 (output_bfd, 0x4d,
4127 contents + roff - 3);
4128 else if (!ABI_64_P (output_bfd) && val == 0x44)
4129 bfd_put_8 (output_bfd, 0x45,
4130 contents + roff - 3);
4131 bfd_put_8 (output_bfd, 0x8d,
4132 contents + roff - 2);
4133 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
4134 contents + roff - 1);
4136 bfd_put_32 (output_bfd,
4137 elf_x86_64_tpoff (info, relocation),
4145 if (htab->elf.sgot == NULL)
4150 off = h->got.offset;
4151 offplt = elf_x86_64_hash_entry (h)->tlsdesc_got;
4155 if (local_got_offsets == NULL)
4158 off = local_got_offsets[r_symndx];
4159 offplt = local_tlsdesc_gotents[r_symndx];
4166 Elf_Internal_Rela outrel;
4170 if (htab->elf.srelgot == NULL)
4173 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4175 if (GOT_TLS_GDESC_P (tls_type))
4177 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
4178 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
4179 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
4180 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4181 + htab->elf.sgotplt->output_offset
4183 + htab->sgotplt_jump_table_size);
4184 sreloc = htab->elf.srelplt;
4186 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
4188 outrel.r_addend = 0;
4189 elf_append_rela (output_bfd, sreloc, &outrel);
4192 sreloc = htab->elf.srelgot;
4194 outrel.r_offset = (htab->elf.sgot->output_section->vma
4195 + htab->elf.sgot->output_offset + off);
4197 if (GOT_TLS_GD_P (tls_type))
4198 dr_type = R_X86_64_DTPMOD64;
4199 else if (GOT_TLS_GDESC_P (tls_type))
4202 dr_type = R_X86_64_TPOFF64;
4204 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
4205 outrel.r_addend = 0;
4206 if ((dr_type == R_X86_64_TPOFF64
4207 || dr_type == R_X86_64_TLSDESC) && indx == 0)
4208 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
4209 outrel.r_info = htab->r_info (indx, dr_type);
4211 elf_append_rela (output_bfd, sreloc, &outrel);
4213 if (GOT_TLS_GD_P (tls_type))
4217 BFD_ASSERT (! unresolved_reloc);
4218 bfd_put_64 (output_bfd,
4219 relocation - elf_x86_64_dtpoff_base (info),
4220 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4224 bfd_put_64 (output_bfd, 0,
4225 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4226 outrel.r_info = htab->r_info (indx,
4228 outrel.r_offset += GOT_ENTRY_SIZE;
4229 elf_append_rela (output_bfd, sreloc,
4238 local_got_offsets[r_symndx] |= 1;
4241 if (off >= (bfd_vma) -2
4242 && ! GOT_TLS_GDESC_P (tls_type))
4244 if (r_type == ELF32_R_TYPE (rel->r_info))
4246 if (r_type == R_X86_64_GOTPC32_TLSDESC
4247 || r_type == R_X86_64_TLSDESC_CALL)
4248 relocation = htab->elf.sgotplt->output_section->vma
4249 + htab->elf.sgotplt->output_offset
4250 + offplt + htab->sgotplt_jump_table_size;
4252 relocation = htab->elf.sgot->output_section->vma
4253 + htab->elf.sgot->output_offset + off;
4254 unresolved_reloc = FALSE;
4258 bfd_vma roff = rel->r_offset;
4260 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
4262 /* GD->IE transition. For 64bit, change
4263 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4264 .word 0x6666; rex64; call __tls_get_addr@plt
4267 addq foo@gottpoff(%rip), %rax
4269 leaq foo@tlsgd(%rip), %rdi
4270 .word 0x6666; rex64; call __tls_get_addr@plt
4273 addq foo@gottpoff(%rip), %rax
4274 For largepic, change:
4275 leaq foo@tlsgd(%rip), %rdi
4276 movabsq $__tls_get_addr@pltoff, %rax
4281 addq foo@gottpoff(%rax), %rax
4282 nopw 0x0(%rax,%rax,1) */
4284 if (ABI_64_P (output_bfd)
4285 && contents[roff + 5] == (bfd_byte) '\xb8')
4287 memcpy (contents + roff - 3,
4288 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
4289 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
4292 else if (ABI_64_P (output_bfd))
4293 memcpy (contents + roff - 4,
4294 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4297 memcpy (contents + roff - 3,
4298 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
4301 relocation = (htab->elf.sgot->output_section->vma
4302 + htab->elf.sgot->output_offset + off
4305 - input_section->output_section->vma
4306 - input_section->output_offset
4308 bfd_put_32 (output_bfd, relocation,
4309 contents + roff + 8 + largepic);
4310 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
4314 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
4316 /* GDesc -> IE transition.
4317 It's originally something like:
4318 leaq x@tlsdesc(%rip), %rax
4321 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
4323 /* Now modify the instruction as appropriate. To
4324 turn a leaq into a movq in the form we use it, it
4325 suffices to change the second byte from 0x8d to
4327 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
4329 bfd_put_32 (output_bfd,
4330 htab->elf.sgot->output_section->vma
4331 + htab->elf.sgot->output_offset + off
4333 - input_section->output_section->vma
4334 - input_section->output_offset
4339 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
4341 /* GDesc -> IE transition.
4348 bfd_put_8 (output_bfd, 0x66, contents + roff);
4349 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4357 case R_X86_64_TLSLD:
4358 if (! elf_x86_64_tls_transition (info, input_bfd,
4359 input_section, contents,
4360 symtab_hdr, sym_hashes,
4361 &r_type, GOT_UNKNOWN,
4362 rel, relend, h, r_symndx))
4365 if (r_type != R_X86_64_TLSLD)
4367 /* LD->LE transition:
4368 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
4369 For 64bit, we change it into:
4370 .word 0x6666; .byte 0x66; movq %fs:0, %rax.
4371 For 32bit, we change it into:
4372 nopl 0x0(%rax); movl %fs:0, %eax.
4373 For largepic, change:
4374 leaq foo@tlsgd(%rip), %rdi
4375 movabsq $__tls_get_addr@pltoff, %rax
4379 data32 data32 data32 nopw %cs:0x0(%rax,%rax,1)
4382 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
4383 if (ABI_64_P (output_bfd)
4384 && contents[rel->r_offset + 5] == (bfd_byte) '\xb8')
4385 memcpy (contents + rel->r_offset - 3,
4386 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
4387 "\x64\x48\x8b\x04\x25\0\0\0", 22);
4388 else if (ABI_64_P (output_bfd))
4389 memcpy (contents + rel->r_offset - 3,
4390 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
4392 memcpy (contents + rel->r_offset - 3,
4393 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
4394 /* Skip R_X86_64_PC32/R_X86_64_PLT32/R_X86_64_PLTOFF64. */
4399 if (htab->elf.sgot == NULL)
4402 off = htab->tls_ld_got.offset;
4407 Elf_Internal_Rela outrel;
4409 if (htab->elf.srelgot == NULL)
4412 outrel.r_offset = (htab->elf.sgot->output_section->vma
4413 + htab->elf.sgot->output_offset + off);
4415 bfd_put_64 (output_bfd, 0,
4416 htab->elf.sgot->contents + off);
4417 bfd_put_64 (output_bfd, 0,
4418 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4419 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
4420 outrel.r_addend = 0;
4421 elf_append_rela (output_bfd, htab->elf.srelgot,
4423 htab->tls_ld_got.offset |= 1;
4425 relocation = htab->elf.sgot->output_section->vma
4426 + htab->elf.sgot->output_offset + off;
4427 unresolved_reloc = FALSE;
4430 case R_X86_64_DTPOFF32:
4431 if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
4432 relocation -= elf_x86_64_dtpoff_base (info);
4434 relocation = elf_x86_64_tpoff (info, relocation);
4437 case R_X86_64_TPOFF32:
4438 case R_X86_64_TPOFF64:
4439 BFD_ASSERT (info->executable);
4440 relocation = elf_x86_64_tpoff (info, relocation);
4443 case R_X86_64_DTPOFF64:
4444 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
4445 relocation -= elf_x86_64_dtpoff_base (info);
4452 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4453 because such sections are not SEC_ALLOC and thus ld.so will
4454 not process them. */
4455 if (unresolved_reloc
4456 && !((input_section->flags & SEC_DEBUGGING) != 0
4458 && _bfd_elf_section_offset (output_bfd, info, input_section,
4459 rel->r_offset) != (bfd_vma) -1)
4461 (*_bfd_error_handler)
4462 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4465 (long) rel->r_offset,
4467 h->root.root.string);
4472 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4473 contents, rel->r_offset,
4474 relocation, rel->r_addend);
4476 check_relocation_error:
4477 if (r != bfd_reloc_ok)
4482 name = h->root.root.string;
4485 name = bfd_elf_string_from_elf_section (input_bfd,
4486 symtab_hdr->sh_link,
4491 name = bfd_section_name (input_bfd, sec);
4494 if (r == bfd_reloc_overflow)
4496 if (! ((*info->callbacks->reloc_overflow)
4497 (info, (h ? &h->root : NULL), name, howto->name,
4498 (bfd_vma) 0, input_bfd, input_section,
4504 (*_bfd_error_handler)
4505 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
4506 input_bfd, input_section,
4507 (long) rel->r_offset, name, (int) r);
4516 /* Finish up dynamic symbol handling. We set the contents of various
4517 dynamic sections here. */
4520 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
4521 struct bfd_link_info *info,
4522 struct elf_link_hash_entry *h,
4523 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
4525 struct elf_x86_64_link_hash_table *htab;
4526 const struct elf_x86_64_backend_data *const abed
4527 = get_elf_x86_64_backend_data (output_bfd);
4529 htab = elf_x86_64_hash_table (info);
4533 if (h->plt.offset != (bfd_vma) -1)
4537 Elf_Internal_Rela rela;
4539 asection *plt, *gotplt, *relplt;
4540 const struct elf_backend_data *bed;
4542 /* When building a static executable, use .iplt, .igot.plt and
4543 .rela.iplt sections for STT_GNU_IFUNC symbols. */
4544 if (htab->elf.splt != NULL)
4546 plt = htab->elf.splt;
4547 gotplt = htab->elf.sgotplt;
4548 relplt = htab->elf.srelplt;
4552 plt = htab->elf.iplt;
4553 gotplt = htab->elf.igotplt;
4554 relplt = htab->elf.irelplt;
4557 /* This symbol has an entry in the procedure linkage table. Set
4559 if ((h->dynindx == -1
4560 && !((h->forced_local || info->executable)
4562 && h->type == STT_GNU_IFUNC))
4568 /* Get the index in the procedure linkage table which
4569 corresponds to this symbol. This is the index of this symbol
4570 in all the symbols for which we are making plt entries. The
4571 first entry in the procedure linkage table is reserved.
4573 Get the offset into the .got table of the entry that
4574 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
4575 bytes. The first three are reserved for the dynamic linker.
4577 For static executables, we don't reserve anything. */
4579 if (plt == htab->elf.splt)
4581 got_offset = h->plt.offset / abed->plt_entry_size - 1;
4582 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
4586 got_offset = h->plt.offset / abed->plt_entry_size;
4587 got_offset = got_offset * GOT_ENTRY_SIZE;
4590 /* Fill in the entry in the procedure linkage table. */
4591 memcpy (plt->contents + h->plt.offset, abed->plt_entry,
4592 abed->plt_entry_size);
4594 /* Insert the relocation positions of the plt section. */
4596 /* Put offset the PC-relative instruction referring to the GOT entry,
4597 subtracting the size of that instruction. */
4598 bfd_put_32 (output_bfd,
4599 (gotplt->output_section->vma
4600 + gotplt->output_offset
4602 - plt->output_section->vma
4603 - plt->output_offset
4605 - abed->plt_got_insn_size),
4606 plt->contents + h->plt.offset + abed->plt_got_offset);
4608 /* Fill in the entry in the global offset table, initially this
4609 points to the second part of the PLT entry. */
4610 bfd_put_64 (output_bfd, (plt->output_section->vma
4611 + plt->output_offset
4612 + h->plt.offset + abed->plt_lazy_offset),
4613 gotplt->contents + got_offset);
4615 /* Fill in the entry in the .rela.plt section. */
4616 rela.r_offset = (gotplt->output_section->vma
4617 + gotplt->output_offset
4619 if (h->dynindx == -1
4620 || ((info->executable
4621 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4623 && h->type == STT_GNU_IFUNC))
4625 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4626 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
4627 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4628 rela.r_addend = (h->root.u.def.value
4629 + h->root.u.def.section->output_section->vma
4630 + h->root.u.def.section->output_offset);
4631 /* R_X86_64_IRELATIVE comes last. */
4632 plt_index = htab->next_irelative_index--;
4636 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
4638 plt_index = htab->next_jump_slot_index++;
4641 /* Don't fill PLT entry for static executables. */
4642 if (plt == htab->elf.splt)
4644 /* Put relocation index. */
4645 bfd_put_32 (output_bfd, plt_index,
4646 plt->contents + h->plt.offset + abed->plt_reloc_offset);
4647 /* Put offset for jmp .PLT0. */
4648 bfd_put_32 (output_bfd, - (h->plt.offset + abed->plt_plt_insn_end),
4649 plt->contents + h->plt.offset + abed->plt_plt_offset);
4652 bed = get_elf_backend_data (output_bfd);
4653 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
4654 bed->s->swap_reloca_out (output_bfd, &rela, loc);
4656 if (!h->def_regular)
4658 /* Mark the symbol as undefined, rather than as defined in
4659 the .plt section. Leave the value if there were any
4660 relocations where pointer equality matters (this is a clue
4661 for the dynamic linker, to make function pointer
4662 comparisons work between an application and shared
4663 library), otherwise set it to zero. If a function is only
4664 called from a binary, there is no need to slow down
4665 shared libraries because of that. */
4666 sym->st_shndx = SHN_UNDEF;
4667 if (!h->pointer_equality_needed)
4672 if (h->got.offset != (bfd_vma) -1
4673 && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h)->tls_type)
4674 && elf_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
4676 Elf_Internal_Rela rela;
4678 /* This symbol has an entry in the global offset table. Set it
4680 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4683 rela.r_offset = (htab->elf.sgot->output_section->vma
4684 + htab->elf.sgot->output_offset
4685 + (h->got.offset &~ (bfd_vma) 1));
4687 /* If this is a static link, or it is a -Bsymbolic link and the
4688 symbol is defined locally or was forced to be local because
4689 of a version file, we just want to emit a RELATIVE reloc.
4690 The entry in the global offset table will already have been
4691 initialized in the relocate_section function. */
4693 && h->type == STT_GNU_IFUNC)
4697 /* Generate R_X86_64_GLOB_DAT. */
4704 if (!h->pointer_equality_needed)
4707 /* For non-shared object, we can't use .got.plt, which
4708 contains the real function addres if we need pointer
4709 equality. We load the GOT entry with the PLT entry. */
4710 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4711 bfd_put_64 (output_bfd, (plt->output_section->vma
4712 + plt->output_offset
4714 htab->elf.sgot->contents + h->got.offset);
4718 else if (info->shared
4719 && SYMBOL_REFERENCES_LOCAL (info, h))
4721 if (!h->def_regular)
4723 BFD_ASSERT((h->got.offset & 1) != 0);
4724 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4725 rela.r_addend = (h->root.u.def.value
4726 + h->root.u.def.section->output_section->vma
4727 + h->root.u.def.section->output_offset);
4731 BFD_ASSERT((h->got.offset & 1) == 0);
4733 bfd_put_64 (output_bfd, (bfd_vma) 0,
4734 htab->elf.sgot->contents + h->got.offset);
4735 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
4739 elf_append_rela (output_bfd, htab->elf.srelgot, &rela);
4744 Elf_Internal_Rela rela;
4746 /* This symbol needs a copy reloc. Set it up. */
4748 if (h->dynindx == -1
4749 || (h->root.type != bfd_link_hash_defined
4750 && h->root.type != bfd_link_hash_defweak)
4751 || htab->srelbss == NULL)
4754 rela.r_offset = (h->root.u.def.value
4755 + h->root.u.def.section->output_section->vma
4756 + h->root.u.def.section->output_offset);
4757 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
4759 elf_append_rela (output_bfd, htab->srelbss, &rela);
4765 /* Finish up local dynamic symbol handling. We set the contents of
4766 various dynamic sections here. */
4769 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
4771 struct elf_link_hash_entry *h
4772 = (struct elf_link_hash_entry *) *slot;
4773 struct bfd_link_info *info
4774 = (struct bfd_link_info *) inf;
4776 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
4780 /* Used to decide how to sort relocs in an optimal manner for the
4781 dynamic linker, before writing them out. */
4783 static enum elf_reloc_type_class
4784 elf_x86_64_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
4785 const asection *rel_sec ATTRIBUTE_UNUSED,
4786 const Elf_Internal_Rela *rela)
4788 switch ((int) ELF32_R_TYPE (rela->r_info))
4790 case R_X86_64_RELATIVE:
4791 case R_X86_64_RELATIVE64:
4792 return reloc_class_relative;
4793 case R_X86_64_JUMP_SLOT:
4794 return reloc_class_plt;
4796 return reloc_class_copy;
4798 return reloc_class_normal;
4802 /* Finish up the dynamic sections. */
4805 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
4806 struct bfd_link_info *info)
4808 struct elf_x86_64_link_hash_table *htab;
4811 const struct elf_x86_64_backend_data *const abed
4812 = get_elf_x86_64_backend_data (output_bfd);
4814 htab = elf_x86_64_hash_table (info);
4818 dynobj = htab->elf.dynobj;
4819 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4821 if (htab->elf.dynamic_sections_created)
4823 bfd_byte *dyncon, *dynconend;
4824 const struct elf_backend_data *bed;
4825 bfd_size_type sizeof_dyn;
4827 if (sdyn == NULL || htab->elf.sgot == NULL)
4830 bed = get_elf_backend_data (dynobj);
4831 sizeof_dyn = bed->s->sizeof_dyn;
4832 dyncon = sdyn->contents;
4833 dynconend = sdyn->contents + sdyn->size;
4834 for (; dyncon < dynconend; dyncon += sizeof_dyn)
4836 Elf_Internal_Dyn dyn;
4839 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
4847 s = htab->elf.sgotplt;
4848 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4852 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
4856 s = htab->elf.srelplt->output_section;
4857 dyn.d_un.d_val = s->size;
4861 /* The procedure linkage table relocs (DT_JMPREL) should
4862 not be included in the overall relocs (DT_RELA).
4863 Therefore, we override the DT_RELASZ entry here to
4864 make it not include the JMPREL relocs. Since the
4865 linker script arranges for .rela.plt to follow all
4866 other relocation sections, we don't have to worry
4867 about changing the DT_RELA entry. */
4868 if (htab->elf.srelplt != NULL)
4870 s = htab->elf.srelplt->output_section;
4871 dyn.d_un.d_val -= s->size;
4875 case DT_TLSDESC_PLT:
4877 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4878 + htab->tlsdesc_plt;
4881 case DT_TLSDESC_GOT:
4883 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4884 + htab->tlsdesc_got;
4888 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
4891 /* Fill in the special first entry in the procedure linkage table. */
4892 if (htab->elf.splt && htab->elf.splt->size > 0)
4894 /* Fill in the first entry in the procedure linkage table. */
4895 memcpy (htab->elf.splt->contents,
4896 abed->plt0_entry, abed->plt_entry_size);
4897 /* Add offset for pushq GOT+8(%rip), since the instruction
4898 uses 6 bytes subtract this value. */
4899 bfd_put_32 (output_bfd,
4900 (htab->elf.sgotplt->output_section->vma
4901 + htab->elf.sgotplt->output_offset
4903 - htab->elf.splt->output_section->vma
4904 - htab->elf.splt->output_offset
4906 htab->elf.splt->contents + abed->plt0_got1_offset);
4907 /* Add offset for the PC-relative instruction accessing GOT+16,
4908 subtracting the offset to the end of that instruction. */
4909 bfd_put_32 (output_bfd,
4910 (htab->elf.sgotplt->output_section->vma
4911 + htab->elf.sgotplt->output_offset
4913 - htab->elf.splt->output_section->vma
4914 - htab->elf.splt->output_offset
4915 - abed->plt0_got2_insn_end),
4916 htab->elf.splt->contents + abed->plt0_got2_offset);
4918 elf_section_data (htab->elf.splt->output_section)
4919 ->this_hdr.sh_entsize = abed->plt_entry_size;
4921 if (htab->tlsdesc_plt)
4923 bfd_put_64 (output_bfd, (bfd_vma) 0,
4924 htab->elf.sgot->contents + htab->tlsdesc_got);
4926 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
4927 abed->plt0_entry, abed->plt_entry_size);
4929 /* Add offset for pushq GOT+8(%rip), since the
4930 instruction uses 6 bytes subtract this value. */
4931 bfd_put_32 (output_bfd,
4932 (htab->elf.sgotplt->output_section->vma
4933 + htab->elf.sgotplt->output_offset
4935 - htab->elf.splt->output_section->vma
4936 - htab->elf.splt->output_offset
4939 htab->elf.splt->contents
4940 + htab->tlsdesc_plt + abed->plt0_got1_offset);
4941 /* Add offset for the PC-relative instruction accessing GOT+TDG,
4942 where TGD stands for htab->tlsdesc_got, subtracting the offset
4943 to the end of that instruction. */
4944 bfd_put_32 (output_bfd,
4945 (htab->elf.sgot->output_section->vma
4946 + htab->elf.sgot->output_offset
4948 - htab->elf.splt->output_section->vma
4949 - htab->elf.splt->output_offset
4951 - abed->plt0_got2_insn_end),
4952 htab->elf.splt->contents
4953 + htab->tlsdesc_plt + abed->plt0_got2_offset);
4958 if (htab->elf.sgotplt)
4960 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4962 (*_bfd_error_handler)
4963 (_("discarded output section: `%A'"), htab->elf.sgotplt);
4967 /* Fill in the first three entries in the global offset table. */
4968 if (htab->elf.sgotplt->size > 0)
4970 /* Set the first entry in the global offset table to the address of
4971 the dynamic section. */
4973 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4975 bfd_put_64 (output_bfd,
4976 sdyn->output_section->vma + sdyn->output_offset,
4977 htab->elf.sgotplt->contents);
4978 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4979 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4980 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4983 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
4987 /* Adjust .eh_frame for .plt section. */
4988 if (htab->plt_eh_frame != NULL
4989 && htab->plt_eh_frame->contents != NULL)
4991 if (htab->elf.splt != NULL
4992 && htab->elf.splt->size != 0
4993 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
4994 && htab->elf.splt->output_section != NULL
4995 && htab->plt_eh_frame->output_section != NULL)
4997 bfd_vma plt_start = htab->elf.splt->output_section->vma;
4998 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
4999 + htab->plt_eh_frame->output_offset
5000 + PLT_FDE_START_OFFSET;
5001 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
5002 htab->plt_eh_frame->contents
5003 + PLT_FDE_START_OFFSET);
5005 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
5007 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
5009 htab->plt_eh_frame->contents))
5014 if (htab->elf.sgot && htab->elf.sgot->size > 0)
5015 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
5018 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
5019 htab_traverse (htab->loc_hash_table,
5020 elf_x86_64_finish_local_dynamic_symbol,
5026 /* Return address for Ith PLT stub in section PLT, for relocation REL
5027 or (bfd_vma) -1 if it should not be included. */
5030 elf_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
5031 const arelent *rel ATTRIBUTE_UNUSED)
5033 return plt->vma + (i + 1) * GET_PLT_ENTRY_SIZE (plt->owner);
5036 /* Handle an x86-64 specific section when reading an object file. This
5037 is called when elfcode.h finds a section with an unknown type. */
5040 elf_x86_64_section_from_shdr (bfd *abfd,
5041 Elf_Internal_Shdr *hdr,
5045 if (hdr->sh_type != SHT_X86_64_UNWIND)
5048 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5054 /* Hook called by the linker routine which adds symbols from an object
5055 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
5059 elf_x86_64_add_symbol_hook (bfd *abfd,
5060 struct bfd_link_info *info,
5061 Elf_Internal_Sym *sym,
5062 const char **namep ATTRIBUTE_UNUSED,
5063 flagword *flagsp ATTRIBUTE_UNUSED,
5069 switch (sym->st_shndx)
5071 case SHN_X86_64_LCOMMON:
5072 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
5075 lcomm = bfd_make_section_with_flags (abfd,
5079 | SEC_LINKER_CREATED));
5082 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
5085 *valp = sym->st_size;
5089 if ((abfd->flags & DYNAMIC) == 0
5090 && (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
5091 || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE))
5092 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
5098 /* Given a BFD section, try to locate the corresponding ELF section
5102 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5103 asection *sec, int *index_return)
5105 if (sec == &_bfd_elf_large_com_section)
5107 *index_return = SHN_X86_64_LCOMMON;
5113 /* Process a symbol. */
5116 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5119 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5121 switch (elfsym->internal_elf_sym.st_shndx)
5123 case SHN_X86_64_LCOMMON:
5124 asym->section = &_bfd_elf_large_com_section;
5125 asym->value = elfsym->internal_elf_sym.st_size;
5126 /* Common symbol doesn't set BSF_GLOBAL. */
5127 asym->flags &= ~BSF_GLOBAL;
5133 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
5135 return (sym->st_shndx == SHN_COMMON
5136 || sym->st_shndx == SHN_X86_64_LCOMMON);
5140 elf_x86_64_common_section_index (asection *sec)
5142 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5145 return SHN_X86_64_LCOMMON;
5149 elf_x86_64_common_section (asection *sec)
5151 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5152 return bfd_com_section_ptr;
5154 return &_bfd_elf_large_com_section;
5158 elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
5159 const Elf_Internal_Sym *sym,
5164 const asection *oldsec)
5166 /* A normal common symbol and a large common symbol result in a
5167 normal common symbol. We turn the large common symbol into a
5170 && h->root.type == bfd_link_hash_common
5172 && bfd_is_com_section (*psec)
5175 if (sym->st_shndx == SHN_COMMON
5176 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
5178 h->root.u.c.p->section
5179 = bfd_make_section_old_way (oldbfd, "COMMON");
5180 h->root.u.c.p->section->flags = SEC_ALLOC;
5182 else if (sym->st_shndx == SHN_X86_64_LCOMMON
5183 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
5184 *psec = bfd_com_section_ptr;
5191 elf_x86_64_additional_program_headers (bfd *abfd,
5192 struct bfd_link_info *info ATTRIBUTE_UNUSED)
5197 /* Check to see if we need a large readonly segment. */
5198 s = bfd_get_section_by_name (abfd, ".lrodata");
5199 if (s && (s->flags & SEC_LOAD))
5202 /* Check to see if we need a large data segment. Since .lbss sections
5203 is placed right after the .bss section, there should be no need for
5204 a large data segment just because of .lbss. */
5205 s = bfd_get_section_by_name (abfd, ".ldata");
5206 if (s && (s->flags & SEC_LOAD))
5212 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
5215 elf_x86_64_hash_symbol (struct elf_link_hash_entry *h)
5217 if (h->plt.offset != (bfd_vma) -1
5219 && !h->pointer_equality_needed)
5222 return _bfd_elf_hash_symbol (h);
5225 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
5228 elf_x86_64_relocs_compatible (const bfd_target *input,
5229 const bfd_target *output)
5231 return ((xvec_get_elf_backend_data (input)->s->elfclass
5232 == xvec_get_elf_backend_data (output)->s->elfclass)
5233 && _bfd_elf_relocs_compatible (input, output));
5236 static const struct bfd_elf_special_section
5237 elf_x86_64_special_sections[]=
5239 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5240 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5241 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
5242 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5243 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5244 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5245 { NULL, 0, 0, 0, 0 }
5248 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
5249 #define TARGET_LITTLE_NAME "elf64-x86-64"
5250 #define ELF_ARCH bfd_arch_i386
5251 #define ELF_TARGET_ID X86_64_ELF_DATA
5252 #define ELF_MACHINE_CODE EM_X86_64
5253 #define ELF_MAXPAGESIZE 0x200000
5254 #define ELF_MINPAGESIZE 0x1000
5255 #define ELF_COMMONPAGESIZE 0x1000
5257 #define elf_backend_can_gc_sections 1
5258 #define elf_backend_can_refcount 1
5259 #define elf_backend_want_got_plt 1
5260 #define elf_backend_plt_readonly 1
5261 #define elf_backend_want_plt_sym 0
5262 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
5263 #define elf_backend_rela_normal 1
5264 #define elf_backend_plt_alignment 4
5266 #define elf_info_to_howto elf_x86_64_info_to_howto
5268 #define bfd_elf64_bfd_link_hash_table_create \
5269 elf_x86_64_link_hash_table_create
5270 #define bfd_elf64_bfd_link_hash_table_free \
5271 elf_x86_64_link_hash_table_free
5272 #define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
5273 #define bfd_elf64_bfd_reloc_name_lookup \
5274 elf_x86_64_reloc_name_lookup
5276 #define elf_backend_adjust_dynamic_symbol elf_x86_64_adjust_dynamic_symbol
5277 #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
5278 #define elf_backend_check_relocs elf_x86_64_check_relocs
5279 #define elf_backend_copy_indirect_symbol elf_x86_64_copy_indirect_symbol
5280 #define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
5281 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
5282 #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
5283 #define elf_backend_gc_mark_hook elf_x86_64_gc_mark_hook
5284 #define elf_backend_gc_sweep_hook elf_x86_64_gc_sweep_hook
5285 #define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
5286 #define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
5288 #define elf_backend_write_core_note elf_x86_64_write_core_note
5290 #define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
5291 #define elf_backend_relocate_section elf_x86_64_relocate_section
5292 #define elf_backend_size_dynamic_sections elf_x86_64_size_dynamic_sections
5293 #define elf_backend_always_size_sections elf_x86_64_always_size_sections
5294 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5295 #define elf_backend_plt_sym_val elf_x86_64_plt_sym_val
5296 #define elf_backend_object_p elf64_x86_64_elf_object_p
5297 #define bfd_elf64_mkobject elf_x86_64_mkobject
5299 #define elf_backend_section_from_shdr \
5300 elf_x86_64_section_from_shdr
5302 #define elf_backend_section_from_bfd_section \
5303 elf_x86_64_elf_section_from_bfd_section
5304 #define elf_backend_add_symbol_hook \
5305 elf_x86_64_add_symbol_hook
5306 #define elf_backend_symbol_processing \
5307 elf_x86_64_symbol_processing
5308 #define elf_backend_common_section_index \
5309 elf_x86_64_common_section_index
5310 #define elf_backend_common_section \
5311 elf_x86_64_common_section
5312 #define elf_backend_common_definition \
5313 elf_x86_64_common_definition
5314 #define elf_backend_merge_symbol \
5315 elf_x86_64_merge_symbol
5316 #define elf_backend_special_sections \
5317 elf_x86_64_special_sections
5318 #define elf_backend_additional_program_headers \
5319 elf_x86_64_additional_program_headers
5320 #define elf_backend_hash_symbol \
5321 elf_x86_64_hash_symbol
5323 #define elf_backend_post_process_headers _bfd_elf_set_osabi
5325 #include "elf64-target.h"
5327 /* FreeBSD support. */
5329 #undef TARGET_LITTLE_SYM
5330 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
5331 #undef TARGET_LITTLE_NAME
5332 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
5335 #define ELF_OSABI ELFOSABI_FREEBSD
5338 #define elf64_bed elf64_x86_64_fbsd_bed
5340 #include "elf64-target.h"
5342 /* Solaris 2 support. */
5344 #undef TARGET_LITTLE_SYM
5345 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_sol2_vec
5346 #undef TARGET_LITTLE_NAME
5347 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
5349 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5350 objects won't be recognized. */
5354 #define elf64_bed elf64_x86_64_sol2_bed
5356 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
5358 #undef elf_backend_static_tls_alignment
5359 #define elf_backend_static_tls_alignment 16
5361 /* The Solaris 2 ABI requires a plt symbol on all platforms.
5363 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5365 #undef elf_backend_want_plt_sym
5366 #define elf_backend_want_plt_sym 1
5368 #include "elf64-target.h"
5370 /* Native Client support. */
5373 elf64_x86_64_nacl_elf_object_p (bfd *abfd)
5375 /* Set the right machine number for a NaCl x86-64 ELF64 file. */
5376 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64_nacl);
5380 #undef TARGET_LITTLE_SYM
5381 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_nacl_vec
5382 #undef TARGET_LITTLE_NAME
5383 #define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
5385 #define elf64_bed elf64_x86_64_nacl_bed
5387 #undef ELF_MAXPAGESIZE
5388 #undef ELF_MINPAGESIZE
5389 #undef ELF_COMMONPAGESIZE
5390 #define ELF_MAXPAGESIZE 0x10000
5391 #define ELF_MINPAGESIZE 0x10000
5392 #define ELF_COMMONPAGESIZE 0x10000
5394 /* Restore defaults. */
5396 #undef elf_backend_static_tls_alignment
5397 #undef elf_backend_want_plt_sym
5398 #define elf_backend_want_plt_sym 0
5400 /* NaCl uses substantially different PLT entries for the same effects. */
5402 #undef elf_backend_plt_alignment
5403 #define elf_backend_plt_alignment 5
5404 #define NACL_PLT_ENTRY_SIZE 64
5405 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
5407 static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
5409 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
5410 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
5411 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5412 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5413 0x41, 0xff, 0xe3, /* jmpq *%r11 */
5415 /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
5416 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1) */
5418 /* 32 bytes of nop to pad out to the standard size. */
5419 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5420 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5421 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5422 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5423 0x66, /* excess data32 prefix */
5427 static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
5429 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
5430 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
5431 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
5432 0x41, 0xff, 0xe3, /* jmpq *%r11 */
5434 /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
5435 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5436 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5438 /* Lazy GOT entries point here (32-byte aligned). */
5439 0x68, /* pushq immediate */
5440 0, 0, 0, 0, /* replaced with index into relocation table. */
5441 0xe9, /* jmp relative */
5442 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
5444 /* 22 bytes of nop to pad out to the standard size. */
5445 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data32 prefixes */
5446 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
5447 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
5450 /* .eh_frame covering the .plt section. */
5452 static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
5454 #if (PLT_CIE_LENGTH != 20 \
5455 || PLT_FDE_LENGTH != 36 \
5456 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
5457 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
5458 # error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
5460 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
5461 0, 0, 0, 0, /* CIE ID */
5462 1, /* CIE version */
5463 'z', 'R', 0, /* Augmentation string */
5464 1, /* Code alignment factor */
5465 0x78, /* Data alignment factor */
5466 16, /* Return address column */
5467 1, /* Augmentation size */
5468 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
5469 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
5470 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
5471 DW_CFA_nop, DW_CFA_nop,
5473 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
5474 PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
5475 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
5476 0, 0, 0, 0, /* .plt size goes here */
5477 0, /* Augmentation size */
5478 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
5479 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
5480 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
5481 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
5482 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
5483 13, /* Block length */
5484 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
5485 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
5486 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
5487 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
5488 DW_CFA_nop, DW_CFA_nop
5491 static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
5493 elf_x86_64_nacl_plt0_entry, /* plt0_entry */
5494 elf_x86_64_nacl_plt_entry, /* plt_entry */
5495 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
5496 2, /* plt0_got1_offset */
5497 9, /* plt0_got2_offset */
5498 13, /* plt0_got2_insn_end */
5499 3, /* plt_got_offset */
5500 33, /* plt_reloc_offset */
5501 38, /* plt_plt_offset */
5502 7, /* plt_got_insn_size */
5503 42, /* plt_plt_insn_end */
5504 32, /* plt_lazy_offset */
5505 elf_x86_64_nacl_eh_frame_plt, /* eh_frame_plt */
5506 sizeof (elf_x86_64_nacl_eh_frame_plt), /* eh_frame_plt_size */
5509 #undef elf_backend_arch_data
5510 #define elf_backend_arch_data &elf_x86_64_nacl_arch_bed
5512 #undef elf_backend_object_p
5513 #define elf_backend_object_p elf64_x86_64_nacl_elf_object_p
5514 #undef elf_backend_modify_segment_map
5515 #define elf_backend_modify_segment_map nacl_modify_segment_map
5516 #undef elf_backend_modify_program_headers
5517 #define elf_backend_modify_program_headers nacl_modify_program_headers
5518 #undef elf_backend_final_write_processing
5519 #define elf_backend_final_write_processing nacl_final_write_processing
5521 #include "elf64-target.h"
5523 /* Native Client x32 support. */
5526 elf32_x86_64_nacl_elf_object_p (bfd *abfd)
5528 /* Set the right machine number for a NaCl x86-64 ELF32 file. */
5529 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32_nacl);
5533 #undef TARGET_LITTLE_SYM
5534 #define TARGET_LITTLE_SYM bfd_elf32_x86_64_nacl_vec
5535 #undef TARGET_LITTLE_NAME
5536 #define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
5538 #define elf32_bed elf32_x86_64_nacl_bed
5540 #define bfd_elf32_bfd_link_hash_table_create \
5541 elf_x86_64_link_hash_table_create
5542 #define bfd_elf32_bfd_link_hash_table_free \
5543 elf_x86_64_link_hash_table_free
5544 #define bfd_elf32_bfd_reloc_type_lookup \
5545 elf_x86_64_reloc_type_lookup
5546 #define bfd_elf32_bfd_reloc_name_lookup \
5547 elf_x86_64_reloc_name_lookup
5548 #define bfd_elf32_mkobject \
5551 #undef elf_backend_object_p
5552 #define elf_backend_object_p \
5553 elf32_x86_64_nacl_elf_object_p
5555 #undef elf_backend_bfd_from_remote_memory
5556 #define elf_backend_bfd_from_remote_memory \
5557 _bfd_elf32_bfd_from_remote_memory
5559 #undef elf_backend_size_info
5560 #define elf_backend_size_info \
5561 _bfd_elf32_size_info
5563 #include "elf32-target.h"
5565 /* Restore defaults. */
5566 #undef elf_backend_object_p
5567 #define elf_backend_object_p elf64_x86_64_elf_object_p
5568 #undef elf_backend_bfd_from_remote_memory
5569 #undef elf_backend_size_info
5570 #undef elf_backend_modify_segment_map
5571 #undef elf_backend_modify_program_headers
5572 #undef elf_backend_final_write_processing
5574 /* Intel L1OM support. */
5577 elf64_l1om_elf_object_p (bfd *abfd)
5579 /* Set the right machine number for an L1OM elf64 file. */
5580 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
5584 #undef TARGET_LITTLE_SYM
5585 #define TARGET_LITTLE_SYM bfd_elf64_l1om_vec
5586 #undef TARGET_LITTLE_NAME
5587 #define TARGET_LITTLE_NAME "elf64-l1om"
5589 #define ELF_ARCH bfd_arch_l1om
5591 #undef ELF_MACHINE_CODE
5592 #define ELF_MACHINE_CODE EM_L1OM
5597 #define elf64_bed elf64_l1om_bed
5599 #undef elf_backend_object_p
5600 #define elf_backend_object_p elf64_l1om_elf_object_p
5602 /* Restore defaults. */
5603 #undef ELF_MAXPAGESIZE
5604 #undef ELF_MINPAGESIZE
5605 #undef ELF_COMMONPAGESIZE
5606 #define ELF_MAXPAGESIZE 0x200000
5607 #define ELF_MINPAGESIZE 0x1000
5608 #define ELF_COMMONPAGESIZE 0x1000
5609 #undef elf_backend_plt_alignment
5610 #define elf_backend_plt_alignment 4
5611 #undef elf_backend_arch_data
5612 #define elf_backend_arch_data &elf_x86_64_arch_bed
5614 #include "elf64-target.h"
5616 /* FreeBSD L1OM support. */
5618 #undef TARGET_LITTLE_SYM
5619 #define TARGET_LITTLE_SYM bfd_elf64_l1om_freebsd_vec
5620 #undef TARGET_LITTLE_NAME
5621 #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
5624 #define ELF_OSABI ELFOSABI_FREEBSD
5627 #define elf64_bed elf64_l1om_fbsd_bed
5629 #include "elf64-target.h"
5631 /* Intel K1OM support. */
5634 elf64_k1om_elf_object_p (bfd *abfd)
5636 /* Set the right machine number for an K1OM elf64 file. */
5637 bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
5641 #undef TARGET_LITTLE_SYM
5642 #define TARGET_LITTLE_SYM bfd_elf64_k1om_vec
5643 #undef TARGET_LITTLE_NAME
5644 #define TARGET_LITTLE_NAME "elf64-k1om"
5646 #define ELF_ARCH bfd_arch_k1om
5648 #undef ELF_MACHINE_CODE
5649 #define ELF_MACHINE_CODE EM_K1OM
5654 #define elf64_bed elf64_k1om_bed
5656 #undef elf_backend_object_p
5657 #define elf_backend_object_p elf64_k1om_elf_object_p
5659 #undef elf_backend_static_tls_alignment
5661 #undef elf_backend_want_plt_sym
5662 #define elf_backend_want_plt_sym 0
5664 #include "elf64-target.h"
5666 /* FreeBSD K1OM support. */
5668 #undef TARGET_LITTLE_SYM
5669 #define TARGET_LITTLE_SYM bfd_elf64_k1om_freebsd_vec
5670 #undef TARGET_LITTLE_NAME
5671 #define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
5674 #define ELF_OSABI ELFOSABI_FREEBSD
5677 #define elf64_bed elf64_k1om_fbsd_bed
5679 #include "elf64-target.h"
5681 /* 32bit x86-64 support. */
5683 #undef TARGET_LITTLE_SYM
5684 #define TARGET_LITTLE_SYM bfd_elf32_x86_64_vec
5685 #undef TARGET_LITTLE_NAME
5686 #define TARGET_LITTLE_NAME "elf32-x86-64"
5690 #define ELF_ARCH bfd_arch_i386
5692 #undef ELF_MACHINE_CODE
5693 #define ELF_MACHINE_CODE EM_X86_64
5697 #undef elf_backend_object_p
5698 #define elf_backend_object_p \
5699 elf32_x86_64_elf_object_p
5701 #undef elf_backend_bfd_from_remote_memory
5702 #define elf_backend_bfd_from_remote_memory \
5703 _bfd_elf32_bfd_from_remote_memory
5705 #undef elf_backend_size_info
5706 #define elf_backend_size_info \
5707 _bfd_elf32_size_info
5709 #include "elf32-target.h"