1 /* X86-64 specific support for ELF
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
22 #include "elfxx-x86.h"
25 #include "libiberty.h"
27 #include "opcode/i386.h"
28 #include "elf/x86-64.h"
35 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
36 #define MINUS_ONE (~ (bfd_vma) 0)
38 /* Since both 32-bit and 64-bit x86-64 encode relocation type in the
39 identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
40 relocation type. We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
41 since they are the same. */
43 #define ABI_64_P(abfd) \
44 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
46 /* The relocation "howto" table. Order of fields:
47 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
48 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
49 static reloc_howto_type x86_64_elf_howto_table[] =
51 HOWTO(R_X86_64_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
52 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
54 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
55 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
57 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
58 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
60 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
61 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
63 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
64 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
66 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
67 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
69 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
70 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
72 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
73 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
75 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
76 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
78 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
79 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
81 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
82 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
84 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
85 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
87 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
89 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
90 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
91 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
92 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
93 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
94 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
95 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
96 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
98 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
99 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
101 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
102 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
104 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
105 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
107 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
108 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
110 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
111 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
113 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
114 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
116 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
117 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
119 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
120 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
122 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
123 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
124 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
125 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
126 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
127 FALSE, 0xffffffff, 0xffffffff, TRUE),
128 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
129 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
131 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
132 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
134 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
135 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
136 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
137 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
138 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
140 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
141 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
143 HOWTO(R_X86_64_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
144 bfd_elf_generic_reloc, "R_X86_64_SIZE32", FALSE, 0xffffffff, 0xffffffff,
146 HOWTO(R_X86_64_SIZE64, 0, 4, 64, FALSE, 0, complain_overflow_unsigned,
147 bfd_elf_generic_reloc, "R_X86_64_SIZE64", FALSE, MINUS_ONE, MINUS_ONE,
149 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
150 complain_overflow_bitfield, bfd_elf_generic_reloc,
151 "R_X86_64_GOTPC32_TLSDESC",
152 FALSE, 0xffffffff, 0xffffffff, TRUE),
153 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
154 complain_overflow_dont, bfd_elf_generic_reloc,
155 "R_X86_64_TLSDESC_CALL",
157 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
158 complain_overflow_bitfield, bfd_elf_generic_reloc,
160 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
161 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
162 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
164 HOWTO(R_X86_64_RELATIVE64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
165 bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", FALSE, MINUS_ONE,
167 HOWTO(R_X86_64_PC32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
168 bfd_elf_generic_reloc, "R_X86_64_PC32_BND", FALSE, 0xffffffff, 0xffffffff,
170 HOWTO(R_X86_64_PLT32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
171 bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", FALSE, 0xffffffff, 0xffffffff,
173 HOWTO(R_X86_64_GOTPCRELX, 0, 2, 32, TRUE, 0, complain_overflow_signed,
174 bfd_elf_generic_reloc, "R_X86_64_GOTPCRELX", FALSE, 0xffffffff,
176 HOWTO(R_X86_64_REX_GOTPCRELX, 0, 2, 32, TRUE, 0, complain_overflow_signed,
177 bfd_elf_generic_reloc, "R_X86_64_REX_GOTPCRELX", FALSE, 0xffffffff,
180 /* We have a gap in the reloc numbers here.
181 R_X86_64_standard counts the number up to this point, and
182 R_X86_64_vt_offset is the value to subtract from a reloc type of
183 R_X86_64_GNU_VT* to form an index into this table. */
184 #define R_X86_64_standard (R_X86_64_REX_GOTPCRELX + 1)
185 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
187 /* GNU extension to record C++ vtable hierarchy. */
188 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
189 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
191 /* GNU extension to record C++ vtable member usage. */
192 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
193 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
196 /* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
197 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
198 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
202 #define IS_X86_64_PCREL_TYPE(TYPE) \
203 ( ((TYPE) == R_X86_64_PC8) \
204 || ((TYPE) == R_X86_64_PC16) \
205 || ((TYPE) == R_X86_64_PC32) \
206 || ((TYPE) == R_X86_64_PC32_BND) \
207 || ((TYPE) == R_X86_64_PC64))
209 /* Map BFD relocs to the x86_64 elf relocs. */
212 bfd_reloc_code_real_type bfd_reloc_val;
213 unsigned char elf_reloc_val;
216 static const struct elf_reloc_map x86_64_reloc_map[] =
218 { BFD_RELOC_NONE, R_X86_64_NONE, },
219 { BFD_RELOC_64, R_X86_64_64, },
220 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
221 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
222 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
223 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
224 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
225 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
226 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
227 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
228 { BFD_RELOC_32, R_X86_64_32, },
229 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
230 { BFD_RELOC_16, R_X86_64_16, },
231 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
232 { BFD_RELOC_8, R_X86_64_8, },
233 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
234 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
235 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
236 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
237 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
238 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
239 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
240 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
241 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
242 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
243 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
244 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
245 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
246 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
247 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
248 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
249 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
250 { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
251 { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
252 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
253 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
254 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
255 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
256 { BFD_RELOC_X86_64_PC32_BND, R_X86_64_PC32_BND, },
257 { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND, },
258 { BFD_RELOC_X86_64_GOTPCRELX, R_X86_64_GOTPCRELX, },
259 { BFD_RELOC_X86_64_REX_GOTPCRELX, R_X86_64_REX_GOTPCRELX, },
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 /* xgettext:c-format */
282 _bfd_error_handler (_("%B: invalid relocation type %d"),
284 r_type = R_X86_64_NONE;
289 i = r_type - (unsigned int) R_X86_64_vt_offset;
290 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
291 return &x86_64_elf_howto_table[i];
294 /* Given a BFD reloc type, return a HOWTO structure. */
295 static reloc_howto_type *
296 elf_x86_64_reloc_type_lookup (bfd *abfd,
297 bfd_reloc_code_real_type code)
301 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
304 if (x86_64_reloc_map[i].bfd_reloc_val == code)
305 return elf_x86_64_rtype_to_howto (abfd,
306 x86_64_reloc_map[i].elf_reloc_val);
311 static reloc_howto_type *
312 elf_x86_64_reloc_name_lookup (bfd *abfd,
317 if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
319 /* Get x32 R_X86_64_32. */
320 reloc_howto_type *reloc
321 = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
322 BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
326 for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
327 if (x86_64_elf_howto_table[i].name != NULL
328 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
329 return &x86_64_elf_howto_table[i];
334 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
337 elf_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
338 Elf_Internal_Rela *dst)
342 r_type = ELF32_R_TYPE (dst->r_info);
343 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
344 BFD_ASSERT (r_type == cache_ptr->howto->type);
347 /* Support for core dump NOTE sections. */
349 elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
354 switch (note->descsz)
359 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
361 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
364 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
372 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
374 elf_tdata (abfd)->core->signal
375 = bfd_get_16 (abfd, note->descdata + 12);
378 elf_tdata (abfd)->core->lwpid
379 = bfd_get_32 (abfd, note->descdata + 32);
388 /* Make a ".reg/999" section. */
389 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
390 size, note->descpos + offset);
394 elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
396 switch (note->descsz)
401 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
402 elf_tdata (abfd)->core->pid
403 = bfd_get_32 (abfd, note->descdata + 12);
404 elf_tdata (abfd)->core->program
405 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
406 elf_tdata (abfd)->core->command
407 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
410 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
411 elf_tdata (abfd)->core->pid
412 = bfd_get_32 (abfd, note->descdata + 24);
413 elf_tdata (abfd)->core->program
414 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
415 elf_tdata (abfd)->core->command
416 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
419 /* Note that for some reason, a spurious space is tacked
420 onto the end of the args in some (at least one anyway)
421 implementations, so strip it off if it exists. */
424 char *command = elf_tdata (abfd)->core->command;
425 int n = strlen (command);
427 if (0 < n && command[n - 1] == ' ')
428 command[n - 1] = '\0';
436 elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
439 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
441 const char *fname, *psargs;
452 va_start (ap, note_type);
453 fname = va_arg (ap, const char *);
454 psargs = va_arg (ap, const char *);
457 if (bed->s->elfclass == ELFCLASS32)
460 memset (&data, 0, sizeof (data));
461 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
462 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
463 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
464 &data, sizeof (data));
469 memset (&data, 0, sizeof (data));
470 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
471 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
472 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
473 &data, sizeof (data));
478 va_start (ap, note_type);
479 pid = va_arg (ap, long);
480 cursig = va_arg (ap, int);
481 gregs = va_arg (ap, const void *);
484 if (bed->s->elfclass == ELFCLASS32)
486 if (bed->elf_machine_code == EM_X86_64)
488 prstatusx32_t prstat;
489 memset (&prstat, 0, sizeof (prstat));
491 prstat.pr_cursig = cursig;
492 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
493 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
494 &prstat, sizeof (prstat));
499 memset (&prstat, 0, sizeof (prstat));
501 prstat.pr_cursig = cursig;
502 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
503 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
504 &prstat, sizeof (prstat));
510 memset (&prstat, 0, sizeof (prstat));
512 prstat.pr_cursig = cursig;
513 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
514 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
515 &prstat, sizeof (prstat));
522 /* Functions for the x86-64 ELF linker. */
524 /* The name of the dynamic interpreter. This is put in the .interp
527 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
528 #define ELF32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
530 /* The size in bytes of an entry in the global offset table. */
532 #define GOT_ENTRY_SIZE 8
534 /* The size in bytes of an entry in the lazy procedure linkage table. */
536 #define LAZY_PLT_ENTRY_SIZE 16
538 /* The size in bytes of an entry in the non-lazy procedure linkage
541 #define NON_LAZY_PLT_ENTRY_SIZE 8
543 /* The first entry in a lazy procedure linkage table looks like this.
544 See the SVR4 ABI i386 supplement and the x86-64 ABI to see how this
547 static const bfd_byte elf_x86_64_lazy_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
549 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
550 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
551 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
554 /* Subsequent entries in a lazy procedure linkage table look like this. */
556 static const bfd_byte elf_x86_64_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
558 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
559 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
560 0x68, /* pushq immediate */
561 0, 0, 0, 0, /* replaced with index into relocation table. */
562 0xe9, /* jmp relative */
563 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
566 /* The first entry in a lazy procedure linkage table with BND prefix
569 static const bfd_byte elf_x86_64_lazy_bnd_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
571 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
572 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
573 0x0f, 0x1f, 0 /* nopl (%rax) */
576 /* Subsequent entries for branches with BND prefx in a lazy procedure
577 linkage table look like this. */
579 static const bfd_byte elf_x86_64_lazy_bnd_plt_entry[LAZY_PLT_ENTRY_SIZE] =
581 0x68, 0, 0, 0, 0, /* pushq immediate */
582 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
583 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
586 /* The first entry in the IBT-enabled lazy procedure linkage table is the
587 the same as the lazy PLT with BND prefix so that bound registers are
588 preserved when control is passed to dynamic linker. Subsequent
589 entries for a IBT-enabled lazy procedure linkage table look like
592 static const bfd_byte elf_x86_64_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
594 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
595 0x68, 0, 0, 0, 0, /* pushq immediate */
596 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
600 /* The first entry in the x32 IBT-enabled lazy procedure linkage table
601 is the same as the normal lazy PLT. Subsequent entries for an
602 x32 IBT-enabled lazy procedure linkage table look like this. */
604 static const bfd_byte elf_x32_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
606 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
607 0x68, 0, 0, 0, 0, /* pushq immediate */
608 0xe9, 0, 0, 0, 0, /* jmpq relative */
609 0x66, 0x90 /* xchg %ax,%ax */
612 /* Entries in the non-lazey procedure linkage table look like this. */
614 static const bfd_byte elf_x86_64_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
616 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
617 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
618 0x66, 0x90 /* xchg %ax,%ax */
621 /* Entries for branches with BND prefix in the non-lazey procedure
622 linkage table look like this. */
624 static const bfd_byte elf_x86_64_non_lazy_bnd_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
626 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
627 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
631 /* Entries for branches with IBT-enabled in the non-lazey procedure
632 linkage table look like this. They have the same size as the lazy
635 static const bfd_byte elf_x86_64_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
637 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
638 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
639 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
640 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopl 0x0(%rax,%rax,1) */
643 /* Entries for branches with IBT-enabled in the x32 non-lazey procedure
644 linkage table look like this. They have the same size as the lazy
647 static const bfd_byte elf_x32_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
649 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
650 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
651 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
652 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
655 /* .eh_frame covering the lazy .plt section. */
657 static const bfd_byte elf_x86_64_eh_frame_lazy_plt[] =
659 #define PLT_CIE_LENGTH 20
660 #define PLT_FDE_LENGTH 36
661 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
662 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
663 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
664 0, 0, 0, 0, /* CIE ID */
666 'z', 'R', 0, /* Augmentation string */
667 1, /* Code alignment factor */
668 0x78, /* Data alignment factor */
669 16, /* Return address column */
670 1, /* Augmentation size */
671 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
672 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
673 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
674 DW_CFA_nop, DW_CFA_nop,
676 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
677 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
678 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
679 0, 0, 0, 0, /* .plt size goes here */
680 0, /* Augmentation size */
681 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
682 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
683 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
684 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
685 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
686 11, /* Block length */
687 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
688 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
689 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
690 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
691 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
694 /* .eh_frame covering the lazy BND .plt section. */
696 static const bfd_byte elf_x86_64_eh_frame_lazy_bnd_plt[] =
698 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
699 0, 0, 0, 0, /* CIE ID */
701 'z', 'R', 0, /* Augmentation string */
702 1, /* Code alignment factor */
703 0x78, /* Data alignment factor */
704 16, /* Return address column */
705 1, /* Augmentation size */
706 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
707 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
708 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
709 DW_CFA_nop, DW_CFA_nop,
711 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
712 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
713 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
714 0, 0, 0, 0, /* .plt size goes here */
715 0, /* Augmentation size */
716 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
717 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
718 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
719 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
720 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
721 11, /* Block length */
722 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
723 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
724 DW_OP_lit15, DW_OP_and, DW_OP_lit5, DW_OP_ge,
725 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
726 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
729 /* .eh_frame covering the lazy .plt section with IBT-enabled. */
731 static const bfd_byte elf_x86_64_eh_frame_lazy_ibt_plt[] =
733 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
734 0, 0, 0, 0, /* CIE ID */
736 'z', 'R', 0, /* Augmentation string */
737 1, /* Code alignment factor */
738 0x78, /* Data alignment factor */
739 16, /* Return address column */
740 1, /* Augmentation size */
741 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
742 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
743 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
744 DW_CFA_nop, DW_CFA_nop,
746 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
747 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
748 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
749 0, 0, 0, 0, /* .plt size goes here */
750 0, /* Augmentation size */
751 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
752 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
753 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
754 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
755 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
756 11, /* Block length */
757 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
758 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
759 DW_OP_lit15, DW_OP_and, DW_OP_lit10, DW_OP_ge,
760 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
761 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
764 /* .eh_frame covering the x32 lazy .plt section with IBT-enabled. */
766 static const bfd_byte elf_x32_eh_frame_lazy_ibt_plt[] =
768 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
769 0, 0, 0, 0, /* CIE ID */
771 'z', 'R', 0, /* Augmentation string */
772 1, /* Code alignment factor */
773 0x78, /* Data alignment factor */
774 16, /* Return address column */
775 1, /* Augmentation size */
776 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
777 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
778 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
779 DW_CFA_nop, DW_CFA_nop,
781 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
782 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
783 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
784 0, 0, 0, 0, /* .plt size goes here */
785 0, /* Augmentation size */
786 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
787 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
788 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
789 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
790 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
791 11, /* Block length */
792 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
793 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
794 DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
795 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
796 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
799 /* .eh_frame covering the non-lazy .plt section. */
801 static const bfd_byte elf_x86_64_eh_frame_non_lazy_plt[] =
803 #define PLT_GOT_FDE_LENGTH 20
804 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
805 0, 0, 0, 0, /* CIE ID */
807 'z', 'R', 0, /* Augmentation string */
808 1, /* Code alignment factor */
809 0x78, /* Data alignment factor */
810 16, /* Return address column */
811 1, /* Augmentation size */
812 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
813 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
814 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
815 DW_CFA_nop, DW_CFA_nop,
817 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
818 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
819 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
820 0, 0, 0, 0, /* non-lazy .plt size goes here */
821 0, /* Augmentation size */
822 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop,
823 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
826 struct elf_x86_64_lazy_plt_layout
828 /* Templates for the initial PLT entry and for subsequent entries. */
829 const bfd_byte *plt0_entry;
830 const bfd_byte *plt_entry;
831 unsigned int plt_entry_size; /* Size of each PLT entry. */
833 /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */
834 unsigned int plt0_got1_offset;
835 unsigned int plt0_got2_offset;
837 /* Offset of the end of the PC-relative instruction containing
839 unsigned int plt0_got2_insn_end;
841 /* Offsets into plt_entry that are to be replaced with... */
842 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
843 unsigned int plt_reloc_offset; /* ... offset into relocation table. */
844 unsigned int plt_plt_offset; /* ... offset to start of .plt. */
846 /* Length of the PC-relative instruction containing plt_got_offset. */
847 unsigned int plt_got_insn_size;
849 /* Offset of the end of the PC-relative jump to plt0_entry. */
850 unsigned int plt_plt_insn_end;
852 /* Offset into plt_entry where the initial value of the GOT entry points. */
853 unsigned int plt_lazy_offset;
855 /* .eh_frame covering the lazy .plt section. */
856 const bfd_byte *eh_frame_plt;
857 unsigned int eh_frame_plt_size;
860 struct elf_x86_64_non_lazy_plt_layout
862 /* Template for the lazy PLT entries. */
863 const bfd_byte *plt_entry;
864 unsigned int plt_entry_size; /* Size of each PLT entry. */
866 /* Offsets into plt_entry that are to be replaced with... */
867 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
869 /* Length of the PC-relative instruction containing plt_got_offset. */
870 unsigned int plt_got_insn_size;
872 /* .eh_frame covering the non-lazy .plt section. */
873 const bfd_byte *eh_frame_plt;
874 unsigned int eh_frame_plt_size;
877 struct elf_x86_64_plt_layout
879 /* Template for the PLT entries. */
880 const bfd_byte *plt_entry;
881 unsigned int plt_entry_size; /* Size of each PLT entry. */
884 unsigned int has_plt0;
886 /* Offsets into plt_entry that are to be replaced with... */
887 unsigned int plt_got_offset; /* ... address of this symbol in .got. */
889 /* Length of the PC-relative instruction containing plt_got_offset. */
890 unsigned int plt_got_insn_size;
892 /* .eh_frame covering the .plt section. */
893 const bfd_byte *eh_frame_plt;
894 unsigned int eh_frame_plt_size;
897 /* Architecture-specific backend data for x86-64. */
899 struct elf_x86_64_backend_data
909 #define get_elf_x86_64_arch_data(bed) \
910 ((const struct elf_x86_64_backend_data *) (bed)->arch_data)
912 #define get_elf_x86_64_backend_data(abfd) \
913 get_elf_x86_64_arch_data (get_elf_backend_data (abfd))
915 /* These are the standard parameters. */
916 static const struct elf_x86_64_lazy_plt_layout elf_x86_64_lazy_plt =
918 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
919 elf_x86_64_lazy_plt_entry, /* plt_entry */
920 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
921 2, /* plt0_got1_offset */
922 8, /* plt0_got2_offset */
923 12, /* plt0_got2_insn_end */
924 2, /* plt_got_offset */
925 7, /* plt_reloc_offset */
926 12, /* plt_plt_offset */
927 6, /* plt_got_insn_size */
928 LAZY_PLT_ENTRY_SIZE, /* plt_plt_insn_end */
929 6, /* plt_lazy_offset */
930 elf_x86_64_eh_frame_lazy_plt, /* eh_frame_plt */
931 sizeof (elf_x86_64_eh_frame_lazy_plt) /* eh_frame_plt_size */
934 static const struct elf_x86_64_non_lazy_plt_layout elf_x86_64_non_lazy_plt =
936 elf_x86_64_non_lazy_plt_entry, /* plt_entry */
937 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
938 2, /* plt_got_offset */
939 6, /* plt_got_insn_size */
940 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
941 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
944 static const struct elf_x86_64_lazy_plt_layout elf_x86_64_lazy_bnd_plt =
946 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
947 elf_x86_64_lazy_bnd_plt_entry, /* plt_entry */
948 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
949 2, /* plt0_got1_offset */
950 1+8, /* plt0_got2_offset */
951 1+12, /* plt0_got2_insn_end */
952 1+2, /* plt_got_offset */
953 1, /* plt_reloc_offset */
954 7, /* plt_plt_offset */
955 1+6, /* plt_got_insn_size */
956 11, /* plt_plt_insn_end */
957 0, /* plt_lazy_offset */
958 elf_x86_64_eh_frame_lazy_bnd_plt, /* eh_frame_plt */
959 sizeof (elf_x86_64_eh_frame_lazy_bnd_plt) /* eh_frame_plt_size */
962 static const struct elf_x86_64_non_lazy_plt_layout elf_x86_64_non_lazy_bnd_plt =
964 elf_x86_64_non_lazy_bnd_plt_entry, /* plt_entry */
965 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
966 1+2, /* plt_got_offset */
967 1+6, /* plt_got_insn_size */
968 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
969 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
972 static const struct elf_x86_64_lazy_plt_layout elf_x86_64_lazy_ibt_plt =
974 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
975 elf_x86_64_lazy_ibt_plt_entry, /* plt_entry */
976 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
977 2, /* plt0_got1_offset */
978 1+8, /* plt0_got2_offset */
979 1+12, /* plt0_got2_insn_end */
980 4+1+2, /* plt_got_offset */
981 4+1, /* plt_reloc_offset */
982 4+1+6, /* plt_plt_offset */
983 4+1+6, /* plt_got_insn_size */
984 4+1+5+5, /* plt_plt_insn_end */
985 0, /* plt_lazy_offset */
986 elf_x86_64_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
987 sizeof (elf_x86_64_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
990 static const struct elf_x86_64_lazy_plt_layout elf_x32_lazy_ibt_plt =
992 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
993 elf_x32_lazy_ibt_plt_entry, /* plt_entry */
994 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
995 2, /* plt0_got1_offset */
996 8, /* plt0_got2_offset */
997 12, /* plt0_got2_insn_end */
998 4+2, /* plt_got_offset */
999 4+1, /* plt_reloc_offset */
1000 4+6, /* plt_plt_offset */
1001 4+6, /* plt_got_insn_size */
1002 4+5+5, /* plt_plt_insn_end */
1003 0, /* plt_lazy_offset */
1004 elf_x32_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
1005 sizeof (elf_x32_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
1008 static const struct elf_x86_64_non_lazy_plt_layout elf_x86_64_non_lazy_ibt_plt =
1010 elf_x86_64_non_lazy_ibt_plt_entry, /* plt_entry */
1011 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1012 4+1+2, /* plt_got_offset */
1013 4+1+6, /* plt_got_insn_size */
1014 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
1015 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1018 static const struct elf_x86_64_non_lazy_plt_layout elf_x32_non_lazy_ibt_plt =
1020 elf_x32_non_lazy_ibt_plt_entry, /* plt_entry */
1021 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1022 4+2, /* plt_got_offset */
1023 4+6, /* plt_got_insn_size */
1024 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
1025 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1028 static const struct elf_x86_64_backend_data elf_x86_64_arch_bed =
1033 #define elf_backend_arch_data &elf_x86_64_arch_bed
1035 /* Values in tls_type of x86 ELF linker hash entry. */
1036 #define GOT_TLS_IE 3
1037 #define GOT_TLS_GDESC 4
1038 #define GOT_TLS_GD_BOTH_P(type) \
1039 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
1040 #define GOT_TLS_GD_P(type) \
1041 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
1042 #define GOT_TLS_GDESC_P(type) \
1043 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
1044 #define GOT_TLS_GD_ANY_P(type) \
1045 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
1047 #define is_x86_64_elf(bfd) \
1048 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1049 && elf_tdata (bfd) != NULL \
1050 && elf_object_id (bfd) == X86_64_ELF_DATA)
1053 elf_x86_64_mkobject (bfd *abfd)
1055 return bfd_elf_allocate_object (abfd, sizeof (struct elf_x86_obj_tdata),
1059 /* x86-64 ELF linker hash table. */
1061 struct elf_x86_64_link_hash_table
1063 struct elf_x86_link_hash_table x86;
1065 /* Parameters describing PLT generation, lazy or non-lazy. */
1066 struct elf_x86_64_plt_layout plt;
1068 /* Parameters describing lazy PLT generation. */
1069 const struct elf_x86_64_lazy_plt_layout *lazy_plt;
1071 /* Parameters describing non-lazy PLT generation. */
1072 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt;
1075 #define elf_x86_64_plt(htab) \
1076 ((struct elf_x86_64_link_hash_table *) (htab))->plt
1078 #define elf_x86_64_lazy_plt(htab) \
1079 ((struct elf_x86_64_link_hash_table *) (htab))->lazy_plt
1081 #define elf_x86_64_non_lazy_plt(htab) \
1082 ((struct elf_x86_64_link_hash_table *) (htab))->non_lazy_plt
1084 #define elf_x86_64_compute_jump_table_size(htab) \
1085 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
1087 /* Create an X86-64 ELF linker hash table. */
1089 static struct bfd_link_hash_table *
1090 elf_x86_64_link_hash_table_create (bfd *abfd)
1092 struct elf_x86_link_hash_table *ret;
1093 bfd_size_type amt = sizeof (struct elf_x86_64_link_hash_table);
1095 ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt);
1099 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1100 _bfd_x86_elf_link_hash_newfunc,
1101 sizeof (struct elf_x86_link_hash_entry),
1108 if (ABI_64_P (abfd))
1110 ret->r_info = elf64_r_info;
1111 ret->r_sym = elf64_r_sym;
1112 ret->pointer_r_type = R_X86_64_64;
1113 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
1114 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
1118 ret->r_info = elf32_r_info;
1119 ret->r_sym = elf32_r_sym;
1120 ret->pointer_r_type = R_X86_64_32;
1121 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
1122 ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
1125 ret->tls_get_addr = "__tls_get_addr";
1126 ret->loc_hash_table = htab_try_create (1024,
1127 _bfd_x86_elf_local_htab_hash,
1128 _bfd_x86_elf_local_htab_eq,
1130 ret->loc_hash_memory = objalloc_create ();
1131 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1133 _bfd_x86_elf_link_hash_table_free (abfd);
1136 ret->elf.root.hash_table_free = _bfd_x86_elf_link_hash_table_free;
1138 return &ret->elf.root;
1142 elf64_x86_64_elf_object_p (bfd *abfd)
1144 /* Set the right machine number for an x86-64 elf64 file. */
1145 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
1150 elf32_x86_64_elf_object_p (bfd *abfd)
1152 /* Set the right machine number for an x86-64 elf32 file. */
1153 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1157 /* Return TRUE if the TLS access code sequence support transition
1161 elf_x86_64_check_tls_transition (bfd *abfd,
1162 struct bfd_link_info *info,
1165 Elf_Internal_Shdr *symtab_hdr,
1166 struct elf_link_hash_entry **sym_hashes,
1167 unsigned int r_type,
1168 const Elf_Internal_Rela *rel,
1169 const Elf_Internal_Rela *relend)
1172 unsigned long r_symndx;
1173 bfd_boolean largepic = FALSE;
1174 struct elf_link_hash_entry *h;
1176 struct elf_x86_link_hash_table *htab;
1178 bfd_boolean indirect_call;
1180 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1181 offset = rel->r_offset;
1184 case R_X86_64_TLSGD:
1185 case R_X86_64_TLSLD:
1186 if ((rel + 1) >= relend)
1189 if (r_type == R_X86_64_TLSGD)
1191 /* Check transition from GD access model. For 64bit, only
1192 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1193 .word 0x6666; rex64; call __tls_get_addr@PLT
1195 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1197 call *__tls_get_addr@GOTPCREL(%rip)
1198 which may be converted to
1199 addr32 call __tls_get_addr
1200 can transit to different access model. For 32bit, only
1201 leaq foo@tlsgd(%rip), %rdi
1202 .word 0x6666; rex64; call __tls_get_addr@PLT
1204 leaq foo@tlsgd(%rip), %rdi
1206 call *__tls_get_addr@GOTPCREL(%rip)
1207 which may be converted to
1208 addr32 call __tls_get_addr
1209 can transit to different access model. For largepic,
1211 leaq foo@tlsgd(%rip), %rdi
1212 movabsq $__tls_get_addr@pltoff, %rax
1216 leaq foo@tlsgd(%rip), %rdi
1217 movabsq $__tls_get_addr@pltoff, %rax
1221 static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1223 if ((offset + 12) > sec->size)
1226 call = contents + offset + 4;
1228 || !((call[1] == 0x48
1236 && call[3] == 0xe8)))
1238 if (!ABI_64_P (abfd)
1239 || (offset + 19) > sec->size
1241 || memcmp (call - 7, leaq + 1, 3) != 0
1242 || memcmp (call, "\x48\xb8", 2) != 0
1246 || !((call[10] == 0x48 && call[12] == 0xd8)
1247 || (call[10] == 0x4c && call[12] == 0xf8)))
1251 else if (ABI_64_P (abfd))
1254 || memcmp (contents + offset - 4, leaq, 4) != 0)
1260 || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
1263 indirect_call = call[2] == 0xff;
1267 /* Check transition from LD access model. Only
1268 leaq foo@tlsld(%rip), %rdi;
1269 call __tls_get_addr@PLT
1271 leaq foo@tlsld(%rip), %rdi;
1272 call *__tls_get_addr@GOTPCREL(%rip)
1273 which may be converted to
1274 addr32 call __tls_get_addr
1275 can transit to different access model. For largepic
1277 leaq foo@tlsld(%rip), %rdi
1278 movabsq $__tls_get_addr@pltoff, %rax
1282 leaq foo@tlsld(%rip), %rdi
1283 movabsq $__tls_get_addr@pltoff, %rax
1287 static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
1289 if (offset < 3 || (offset + 9) > sec->size)
1292 if (memcmp (contents + offset - 3, lea, 3) != 0)
1295 call = contents + offset + 4;
1296 if (!(call[0] == 0xe8
1297 || (call[0] == 0xff && call[1] == 0x15)
1298 || (call[0] == 0x67 && call[1] == 0xe8)))
1300 if (!ABI_64_P (abfd)
1301 || (offset + 19) > sec->size
1302 || memcmp (call, "\x48\xb8", 2) != 0
1306 || !((call[10] == 0x48 && call[12] == 0xd8)
1307 || (call[10] == 0x4c && call[12] == 0xf8)))
1311 indirect_call = call[0] == 0xff;
1314 r_symndx = htab->r_sym (rel[1].r_info);
1315 if (r_symndx < symtab_hdr->sh_info)
1318 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1320 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
1323 return ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLTOFF64;
1324 else if (indirect_call)
1325 return ELF32_R_TYPE (rel[1].r_info) == R_X86_64_GOTPCRELX;
1327 return (ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PC32
1328 || ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
1330 case R_X86_64_GOTTPOFF:
1331 /* Check transition from IE access model:
1332 mov foo@gottpoff(%rip), %reg
1333 add foo@gottpoff(%rip), %reg
1336 /* Check REX prefix first. */
1337 if (offset >= 3 && (offset + 4) <= sec->size)
1339 val = bfd_get_8 (abfd, contents + offset - 3);
1340 if (val != 0x48 && val != 0x4c)
1342 /* X32 may have 0x44 REX prefix or no REX prefix. */
1343 if (ABI_64_P (abfd))
1349 /* X32 may not have any REX prefix. */
1350 if (ABI_64_P (abfd))
1352 if (offset < 2 || (offset + 3) > sec->size)
1356 val = bfd_get_8 (abfd, contents + offset - 2);
1357 if (val != 0x8b && val != 0x03)
1360 val = bfd_get_8 (abfd, contents + offset - 1);
1361 return (val & 0xc7) == 5;
1363 case R_X86_64_GOTPC32_TLSDESC:
1364 /* Check transition from GDesc access model:
1365 leaq x@tlsdesc(%rip), %rax
1367 Make sure it's a leaq adding rip to a 32-bit offset
1368 into any register, although it's probably almost always
1371 if (offset < 3 || (offset + 4) > sec->size)
1374 val = bfd_get_8 (abfd, contents + offset - 3);
1375 if ((val & 0xfb) != 0x48)
1378 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1381 val = bfd_get_8 (abfd, contents + offset - 1);
1382 return (val & 0xc7) == 0x05;
1384 case R_X86_64_TLSDESC_CALL:
1385 /* Check transition from GDesc access model:
1386 call *x@tlsdesc(%rax)
1388 if (offset + 2 <= sec->size)
1390 /* Make sure that it's a call *x@tlsdesc(%rax). */
1391 call = contents + offset;
1392 return call[0] == 0xff && call[1] == 0x10;
1402 /* Return TRUE if the TLS access transition is OK or no transition
1403 will be performed. Update R_TYPE if there is a transition. */
1406 elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1407 asection *sec, bfd_byte *contents,
1408 Elf_Internal_Shdr *symtab_hdr,
1409 struct elf_link_hash_entry **sym_hashes,
1410 unsigned int *r_type, int tls_type,
1411 const Elf_Internal_Rela *rel,
1412 const Elf_Internal_Rela *relend,
1413 struct elf_link_hash_entry *h,
1414 unsigned long r_symndx,
1415 bfd_boolean from_relocate_section)
1417 unsigned int from_type = *r_type;
1418 unsigned int to_type = from_type;
1419 bfd_boolean check = TRUE;
1421 /* Skip TLS transition for functions. */
1423 && (h->type == STT_FUNC
1424 || h->type == STT_GNU_IFUNC))
1429 case R_X86_64_TLSGD:
1430 case R_X86_64_GOTPC32_TLSDESC:
1431 case R_X86_64_TLSDESC_CALL:
1432 case R_X86_64_GOTTPOFF:
1433 if (bfd_link_executable (info))
1436 to_type = R_X86_64_TPOFF32;
1438 to_type = R_X86_64_GOTTPOFF;
1441 /* When we are called from elf_x86_64_relocate_section, there may
1442 be additional transitions based on TLS_TYPE. */
1443 if (from_relocate_section)
1445 unsigned int new_to_type = to_type;
1447 if (bfd_link_executable (info)
1450 && tls_type == GOT_TLS_IE)
1451 new_to_type = R_X86_64_TPOFF32;
1453 if (to_type == R_X86_64_TLSGD
1454 || to_type == R_X86_64_GOTPC32_TLSDESC
1455 || to_type == R_X86_64_TLSDESC_CALL)
1457 if (tls_type == GOT_TLS_IE)
1458 new_to_type = R_X86_64_GOTTPOFF;
1461 /* We checked the transition before when we were called from
1462 elf_x86_64_check_relocs. We only want to check the new
1463 transition which hasn't been checked before. */
1464 check = new_to_type != to_type && from_type == to_type;
1465 to_type = new_to_type;
1470 case R_X86_64_TLSLD:
1471 if (bfd_link_executable (info))
1472 to_type = R_X86_64_TPOFF32;
1479 /* Return TRUE if there is no transition. */
1480 if (from_type == to_type)
1483 /* Check if the transition can be performed. */
1485 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1486 symtab_hdr, sym_hashes,
1487 from_type, rel, relend))
1489 reloc_howto_type *from, *to;
1492 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1493 to = elf_x86_64_rtype_to_howto (abfd, to_type);
1496 name = h->root.root.string;
1499 struct elf_x86_link_hash_table *htab;
1501 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1506 Elf_Internal_Sym *isym;
1508 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1510 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1515 /* xgettext:c-format */
1516 (_("%B: TLS transition from %s to %s against `%s' at %#Lx "
1517 "in section `%A' failed"),
1518 abfd, from->name, to->name, name, rel->r_offset, sec);
1519 bfd_set_error (bfd_error_bad_value);
1527 /* Rename some of the generic section flags to better document how they
1529 #define need_convert_load sec_flg0
1530 #define check_relocs_failed sec_flg1
1533 elf_x86_64_need_pic (struct bfd_link_info *info,
1534 bfd *input_bfd, asection *sec,
1535 struct elf_link_hash_entry *h,
1536 Elf_Internal_Shdr *symtab_hdr,
1537 Elf_Internal_Sym *isym,
1538 reloc_howto_type *howto)
1541 const char *und = "";
1542 const char *pic = "";
1548 name = h->root.root.string;
1549 switch (ELF_ST_VISIBILITY (h->other))
1552 v = _("hidden symbol ");
1555 v = _("internal symbol ");
1558 v = _("protected symbol ");
1561 if (((struct elf_x86_link_hash_entry *) h)->def_protected)
1562 v = _("protected symbol ");
1565 pic = _("; recompile with -fPIC");
1569 if (!h->def_regular && !h->def_dynamic)
1570 und = _("undefined ");
1574 name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
1575 pic = _("; recompile with -fPIC");
1578 if (bfd_link_dll (info))
1579 object = _("a shared object");
1580 else if (bfd_link_pie (info))
1581 object = _("a PIE object");
1583 object = _("a PDE object");
1585 /* xgettext:c-format */
1586 _bfd_error_handler (_("%B: relocation %s against %s%s`%s' can "
1587 "not be used when making %s%s"),
1588 input_bfd, howto->name, und, v, name,
1590 bfd_set_error (bfd_error_bad_value);
1591 sec->check_relocs_failed = 1;
1595 /* With the local symbol, foo, we convert
1596 mov foo@GOTPCREL(%rip), %reg
1600 call/jmp *foo@GOTPCREL(%rip)
1602 nop call foo/jmp foo nop
1603 When PIC is false, convert
1604 test %reg, foo@GOTPCREL(%rip)
1608 binop foo@GOTPCREL(%rip), %reg
1611 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1615 elf_x86_64_convert_load_reloc (bfd *abfd, asection *sec,
1617 Elf_Internal_Rela *irel,
1618 struct elf_link_hash_entry *h,
1619 bfd_boolean *converted,
1620 struct bfd_link_info *link_info)
1622 struct elf_x86_link_hash_table *htab;
1624 bfd_boolean require_reloc_pc32;
1626 bfd_boolean to_reloc_pc32;
1629 bfd_signed_vma raddend;
1630 unsigned int opcode;
1632 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
1633 unsigned int r_symndx;
1635 bfd_vma roff = irel->r_offset;
1637 if (roff < (r_type == R_X86_64_REX_GOTPCRELX ? 3 : 2))
1640 raddend = irel->r_addend;
1641 /* Addend for 32-bit PC-relative relocation must be -4. */
1645 htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
1646 is_pic = bfd_link_pic (link_info);
1648 relocx = (r_type == R_X86_64_GOTPCRELX
1649 || r_type == R_X86_64_REX_GOTPCRELX);
1651 /* TRUE if we can convert only to R_X86_64_PC32. Enable it for
1654 = link_info->disable_target_specific_optimizations > 1;
1656 r_symndx = htab->r_sym (irel->r_info);
1658 opcode = bfd_get_8 (abfd, contents + roff - 2);
1660 /* Convert mov to lea since it has been done for a while. */
1663 /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
1664 for call, jmp or one of adc, add, and, cmp, or, sbb, sub,
1665 test, xor instructions. */
1670 /* We convert only to R_X86_64_PC32:
1672 2. R_X86_64_GOTPCREL since we can't modify REX byte.
1673 3. require_reloc_pc32 is true.
1676 to_reloc_pc32 = (opcode == 0xff
1678 || require_reloc_pc32
1681 /* Get the symbol referred to by the reloc. */
1684 Elf_Internal_Sym *isym
1685 = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
1687 /* Skip relocation against undefined symbols. */
1688 if (isym->st_shndx == SHN_UNDEF)
1691 symtype = ELF_ST_TYPE (isym->st_info);
1693 if (isym->st_shndx == SHN_ABS)
1694 tsec = bfd_abs_section_ptr;
1695 else if (isym->st_shndx == SHN_COMMON)
1696 tsec = bfd_com_section_ptr;
1697 else if (isym->st_shndx == SHN_X86_64_LCOMMON)
1698 tsec = &_bfd_elf_large_com_section;
1700 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1702 toff = isym->st_value;
1706 /* Undefined weak symbol is only bound locally in executable
1707 and its reference is resolved as 0 without relocation
1708 overflow. We can only perform this optimization for
1709 GOTPCRELX relocations since we need to modify REX byte.
1710 It is OK convert mov with R_X86_64_GOTPCREL to
1712 if ((relocx || opcode == 0x8b)
1713 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info,
1716 elf_x86_hash_entry (h)))
1720 /* Skip for branch instructions since R_X86_64_PC32
1722 if (require_reloc_pc32)
1727 /* For non-branch instructions, we can convert to
1728 R_X86_64_32/R_X86_64_32S since we know if there
1730 to_reloc_pc32 = FALSE;
1733 /* Since we don't know the current PC when PIC is true,
1734 we can't convert to R_X86_64_PC32. */
1735 if (to_reloc_pc32 && is_pic)
1740 /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1741 ld.so may use its link-time address. */
1742 else if (h->start_stop
1744 || h->root.type == bfd_link_hash_defined
1745 || h->root.type == bfd_link_hash_defweak)
1746 && h != htab->elf.hdynamic
1747 && SYMBOL_REFERENCES_LOCAL (link_info, h)))
1749 /* bfd_link_hash_new or bfd_link_hash_undefined is
1750 set by an assignment in a linker script in
1751 bfd_elf_record_link_assignment. start_stop is set
1752 on __start_SECNAME/__stop_SECNAME which mark section
1756 && (h->root.type == bfd_link_hash_new
1757 || h->root.type == bfd_link_hash_undefined
1758 || ((h->root.type == bfd_link_hash_defined
1759 || h->root.type == bfd_link_hash_defweak)
1760 && h->root.u.def.section == bfd_und_section_ptr))))
1762 /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
1763 if (require_reloc_pc32)
1767 tsec = h->root.u.def.section;
1768 toff = h->root.u.def.value;
1775 /* Don't convert GOTPCREL relocation against large section. */
1776 if (elf_section_data (tsec) != NULL
1777 && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
1780 /* We can only estimate relocation overflow for R_X86_64_PC32. */
1784 if (tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
1786 /* At this stage in linking, no SEC_MERGE symbol has been
1787 adjusted, so all references to such symbols need to be
1788 passed through _bfd_merged_section_offset. (Later, in
1789 relocate_section, all SEC_MERGE symbols *except* for
1790 section symbols have been adjusted.)
1792 gas may reduce relocations against symbols in SEC_MERGE
1793 sections to a relocation against the section symbol when
1794 the original addend was zero. When the reloc is against
1795 a section symbol we should include the addend in the
1796 offset passed to _bfd_merged_section_offset, since the
1797 location of interest is the original symbol. On the
1798 other hand, an access to "sym+addend" where "sym" is not
1799 a section symbol should not include the addend; Such an
1800 access is presumed to be an offset from "sym"; The
1801 location of interest is just "sym". */
1802 if (symtype == STT_SECTION)
1805 toff = _bfd_merged_section_offset (abfd, &tsec,
1806 elf_section_data (tsec)->sec_info,
1809 if (symtype != STT_SECTION)
1815 /* Don't convert if R_X86_64_PC32 relocation overflows. */
1816 if (tsec->output_section == sec->output_section)
1818 if ((toff - roff + 0x80000000) > 0xffffffff)
1823 bfd_signed_vma distance;
1825 /* At this point, we don't know the load addresses of TSEC
1826 section nor SEC section. We estimate the distrance between
1827 SEC and TSEC. We store the estimated distances in the
1828 compressed_size field of the output section, which is only
1829 used to decompress the compressed input section. */
1830 if (sec->output_section->compressed_size == 0)
1833 bfd_size_type size = 0;
1834 for (asect = link_info->output_bfd->sections;
1836 asect = asect->next)
1837 /* Skip debug sections since compressed_size is used to
1838 compress debug sections. */
1839 if ((asect->flags & SEC_DEBUGGING) == 0)
1842 for (i = asect->map_head.s;
1846 size = align_power (size, i->alignment_power);
1849 asect->compressed_size = size;
1853 /* Don't convert GOTPCREL relocations if TSEC isn't placed
1855 distance = (tsec->output_section->compressed_size
1856 - sec->output_section->compressed_size);
1860 /* Take PT_GNU_RELRO segment into account by adding
1862 if ((toff + distance + get_elf_backend_data (abfd)->maxpagesize
1863 - roff + 0x80000000) > 0xffffffff)
1870 /* We have "call/jmp *foo@GOTPCREL(%rip)". */
1875 /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
1877 modrm = bfd_get_8 (abfd, contents + roff - 1);
1880 /* Convert to "jmp foo nop". */
1883 nop_offset = irel->r_offset + 3;
1884 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1885 irel->r_offset -= 1;
1886 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1890 struct elf_x86_link_hash_entry *eh
1891 = (struct elf_x86_link_hash_entry *) h;
1893 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1896 /* To support TLS optimization, always use addr32 prefix for
1897 "call *__tls_get_addr@GOTPCREL(%rip)". */
1898 if (eh && eh->tls_get_addr)
1901 nop_offset = irel->r_offset - 2;
1905 nop = link_info->call_nop_byte;
1906 if (link_info->call_nop_as_suffix)
1908 nop_offset = irel->r_offset + 3;
1909 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1910 irel->r_offset -= 1;
1911 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1914 nop_offset = irel->r_offset - 2;
1917 bfd_put_8 (abfd, nop, contents + nop_offset);
1918 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1919 r_type = R_X86_64_PC32;
1924 unsigned int rex_mask = REX_R;
1926 if (r_type == R_X86_64_REX_GOTPCRELX)
1927 rex = bfd_get_8 (abfd, contents + roff - 3);
1935 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1936 "lea foo(%rip), %reg". */
1938 r_type = R_X86_64_PC32;
1942 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1943 "mov $foo, %reg". */
1945 modrm = bfd_get_8 (abfd, contents + roff - 1);
1946 modrm = 0xc0 | (modrm & 0x38) >> 3;
1947 if ((rex & REX_W) != 0
1948 && ABI_64_P (link_info->output_bfd))
1950 /* Keep the REX_W bit in REX byte for LP64. */
1951 r_type = R_X86_64_32S;
1952 goto rewrite_modrm_rex;
1956 /* If the REX_W bit in REX byte isn't needed,
1957 use R_X86_64_32 and clear the W bit to avoid
1958 sign-extend imm32 to imm64. */
1959 r_type = R_X86_64_32;
1960 /* Clear the W bit in REX byte. */
1962 goto rewrite_modrm_rex;
1968 /* R_X86_64_PC32 isn't supported. */
1972 modrm = bfd_get_8 (abfd, contents + roff - 1);
1975 /* Convert "test %reg, foo@GOTPCREL(%rip)" to
1976 "test $foo, %reg". */
1977 modrm = 0xc0 | (modrm & 0x38) >> 3;
1982 /* Convert "binop foo@GOTPCREL(%rip), %reg" to
1983 "binop $foo, %reg". */
1984 modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
1988 /* Use R_X86_64_32 with 32-bit operand to avoid relocation
1989 overflow when sign-extending imm32 to imm64. */
1990 r_type = (rex & REX_W) != 0 ? R_X86_64_32S : R_X86_64_32;
1993 bfd_put_8 (abfd, modrm, contents + roff - 1);
1997 /* Move the R bit to the B bit in REX byte. */
1998 rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
1999 bfd_put_8 (abfd, rex, contents + roff - 3);
2002 /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
2006 bfd_put_8 (abfd, opcode, contents + roff - 2);
2009 irel->r_info = htab->r_info (r_symndx, r_type);
2016 /* Look through the relocs for a section during the first phase, and
2017 calculate needed space in the global offset table, procedure
2018 linkage table, and dynamic reloc sections. */
2021 elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
2023 const Elf_Internal_Rela *relocs)
2025 struct elf_x86_link_hash_table *htab;
2026 Elf_Internal_Shdr *symtab_hdr;
2027 struct elf_link_hash_entry **sym_hashes;
2028 const Elf_Internal_Rela *rel;
2029 const Elf_Internal_Rela *rel_end;
2033 if (bfd_link_relocatable (info))
2036 /* Don't do anything special with non-loaded, non-alloced sections.
2037 In particular, any relocs in such sections should not affect GOT
2038 and PLT reference counting (ie. we don't allow them to create GOT
2039 or PLT entries), there's no possibility or desire to optimize TLS
2040 relocs, and there's not much point in propagating relocs to shared
2041 libs that the dynamic linker won't relocate. */
2042 if ((sec->flags & SEC_ALLOC) == 0)
2045 BFD_ASSERT (is_x86_64_elf (abfd));
2047 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2050 sec->check_relocs_failed = 1;
2054 /* Get the section contents. */
2055 if (elf_section_data (sec)->this_hdr.contents != NULL)
2056 contents = elf_section_data (sec)->this_hdr.contents;
2057 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2059 sec->check_relocs_failed = 1;
2063 symtab_hdr = &elf_symtab_hdr (abfd);
2064 sym_hashes = elf_sym_hashes (abfd);
2068 rel_end = relocs + sec->reloc_count;
2069 for (rel = relocs; rel < rel_end; rel++)
2071 unsigned int r_type;
2072 unsigned int r_symndx;
2073 struct elf_link_hash_entry *h;
2074 struct elf_x86_link_hash_entry *eh;
2075 Elf_Internal_Sym *isym;
2077 bfd_boolean size_reloc;
2079 r_symndx = htab->r_sym (rel->r_info);
2080 r_type = ELF32_R_TYPE (rel->r_info);
2082 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
2084 /* xgettext:c-format */
2085 _bfd_error_handler (_("%B: bad symbol index: %d"),
2090 if (r_symndx < symtab_hdr->sh_info)
2092 /* A local symbol. */
2093 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2098 /* Check relocation against local STT_GNU_IFUNC symbol. */
2099 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
2101 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel,
2106 /* Fake a STT_GNU_IFUNC symbol. */
2107 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
2109 h->type = STT_GNU_IFUNC;
2112 h->forced_local = 1;
2113 h->root.type = bfd_link_hash_defined;
2121 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2122 while (h->root.type == bfd_link_hash_indirect
2123 || h->root.type == bfd_link_hash_warning)
2124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2127 /* Check invalid x32 relocations. */
2128 if (!ABI_64_P (abfd))
2134 case R_X86_64_DTPOFF64:
2135 case R_X86_64_TPOFF64:
2137 case R_X86_64_GOTOFF64:
2138 case R_X86_64_GOT64:
2139 case R_X86_64_GOTPCREL64:
2140 case R_X86_64_GOTPC64:
2141 case R_X86_64_GOTPLT64:
2142 case R_X86_64_PLTOFF64:
2145 name = h->root.root.string;
2147 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2150 /* xgettext:c-format */
2151 (_("%B: relocation %s against symbol `%s' isn't "
2152 "supported in x32 mode"), abfd,
2153 x86_64_elf_howto_table[r_type].name, name);
2154 bfd_set_error (bfd_error_bad_value);
2162 /* It is referenced by a non-shared object. */
2164 h->root.non_ir_ref_regular = 1;
2166 if (h->type == STT_GNU_IFUNC)
2167 elf_tdata (info->output_bfd)->has_gnu_symbols
2168 |= elf_gnu_symbol_ifunc;
2171 if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
2172 symtab_hdr, sym_hashes,
2173 &r_type, GOT_UNKNOWN,
2174 rel, rel_end, h, r_symndx, FALSE))
2177 eh = (struct elf_x86_link_hash_entry *) h;
2180 case R_X86_64_TLSLD:
2181 htab->tls_ld_or_ldm_got.refcount += 1;
2184 case R_X86_64_TPOFF32:
2185 if (!bfd_link_executable (info) && ABI_64_P (abfd))
2186 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2187 &x86_64_elf_howto_table[r_type]);
2189 eh->has_got_reloc = 1;
2192 case R_X86_64_GOTTPOFF:
2193 if (!bfd_link_executable (info))
2194 info->flags |= DF_STATIC_TLS;
2197 case R_X86_64_GOT32:
2198 case R_X86_64_GOTPCREL:
2199 case R_X86_64_GOTPCRELX:
2200 case R_X86_64_REX_GOTPCRELX:
2201 case R_X86_64_TLSGD:
2202 case R_X86_64_GOT64:
2203 case R_X86_64_GOTPCREL64:
2204 case R_X86_64_GOTPLT64:
2205 case R_X86_64_GOTPC32_TLSDESC:
2206 case R_X86_64_TLSDESC_CALL:
2207 /* This symbol requires a global offset table entry. */
2209 int tls_type, old_tls_type;
2213 default: tls_type = GOT_NORMAL; break;
2214 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
2215 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
2216 case R_X86_64_GOTPC32_TLSDESC:
2217 case R_X86_64_TLSDESC_CALL:
2218 tls_type = GOT_TLS_GDESC; break;
2223 h->got.refcount += 1;
2224 old_tls_type = eh->tls_type;
2228 bfd_signed_vma *local_got_refcounts;
2230 /* This is a global offset table entry for a local symbol. */
2231 local_got_refcounts = elf_local_got_refcounts (abfd);
2232 if (local_got_refcounts == NULL)
2236 size = symtab_hdr->sh_info;
2237 size *= sizeof (bfd_signed_vma)
2238 + sizeof (bfd_vma) + sizeof (char);
2239 local_got_refcounts = ((bfd_signed_vma *)
2240 bfd_zalloc (abfd, size));
2241 if (local_got_refcounts == NULL)
2243 elf_local_got_refcounts (abfd) = local_got_refcounts;
2244 elf_x86_local_tlsdesc_gotent (abfd)
2245 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
2246 elf_x86_local_got_tls_type (abfd)
2247 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
2249 local_got_refcounts[r_symndx] += 1;
2251 = elf_x86_local_got_tls_type (abfd) [r_symndx];
2254 /* If a TLS symbol is accessed using IE at least once,
2255 there is no point to use dynamic model for it. */
2256 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2257 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2258 || tls_type != GOT_TLS_IE))
2260 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
2261 tls_type = old_tls_type;
2262 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2263 && GOT_TLS_GD_ANY_P (tls_type))
2264 tls_type |= old_tls_type;
2268 name = h->root.root.string;
2270 name = bfd_elf_sym_name (abfd, symtab_hdr,
2273 /* xgettext:c-format */
2274 (_("%B: '%s' accessed both as normal and"
2275 " thread local symbol"),
2277 bfd_set_error (bfd_error_bad_value);
2282 if (old_tls_type != tls_type)
2285 eh->tls_type = tls_type;
2287 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
2292 case R_X86_64_GOTOFF64:
2293 case R_X86_64_GOTPC32:
2294 case R_X86_64_GOTPC64:
2297 eh->has_got_reloc = 1;
2300 case R_X86_64_PLT32:
2301 case R_X86_64_PLT32_BND:
2302 /* This symbol requires a procedure linkage table entry. We
2303 actually build the entry in adjust_dynamic_symbol,
2304 because this might be a case of linking PIC code which is
2305 never referenced by a dynamic object, in which case we
2306 don't need to generate a procedure linkage table entry
2309 /* If this is a local symbol, we resolve it directly without
2310 creating a procedure linkage table entry. */
2314 eh->has_got_reloc = 1;
2316 h->plt.refcount += 1;
2319 case R_X86_64_PLTOFF64:
2320 /* This tries to form the 'address' of a function relative
2321 to GOT. For global symbols we need a PLT entry. */
2325 h->plt.refcount += 1;
2329 case R_X86_64_SIZE32:
2330 case R_X86_64_SIZE64:
2335 if (!ABI_64_P (abfd))
2341 /* Check relocation overflow as these relocs may lead to
2342 run-time relocation overflow. Don't error out for
2343 sections we don't care about, such as debug sections or
2344 when relocation overflow check is disabled. */
2345 if (!info->no_reloc_overflow_check
2346 && (bfd_link_pic (info)
2347 || (bfd_link_executable (info)
2351 && (sec->flags & SEC_READONLY) == 0)))
2352 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2353 &x86_64_elf_howto_table[r_type]);
2359 case R_X86_64_PC32_BND:
2363 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2364 eh->has_non_got_reloc = 1;
2365 /* We are called after all symbols have been resolved. Only
2366 relocation against STT_GNU_IFUNC symbol must go through
2369 && (bfd_link_executable (info)
2370 || h->type == STT_GNU_IFUNC))
2372 /* If this reloc is in a read-only section, we might
2373 need a copy reloc. We can't check reliably at this
2374 stage whether the section is read-only, as input
2375 sections have not yet been mapped to output sections.
2376 Tentatively set the flag for now, and correct in
2377 adjust_dynamic_symbol. */
2380 /* We may need a .plt entry if the symbol is a function
2381 defined in a shared lib or is a STT_GNU_IFUNC function
2382 referenced from the code or read-only section. */
2384 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2385 h->plt.refcount += 1;
2387 if (r_type == R_X86_64_PC32)
2389 /* Since something like ".long foo - ." may be used
2390 as pointer, make sure that PLT is used if foo is
2391 a function defined in a shared library. */
2392 if ((sec->flags & SEC_CODE) == 0)
2393 h->pointer_equality_needed = 1;
2395 else if (r_type != R_X86_64_PC32_BND
2396 && r_type != R_X86_64_PC64)
2398 h->pointer_equality_needed = 1;
2399 /* At run-time, R_X86_64_64 can be resolved for both
2400 x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2401 can only be resolved for x32. */
2402 if ((sec->flags & SEC_READONLY) == 0
2403 && (r_type == R_X86_64_64
2404 || (!ABI_64_P (abfd)
2405 && (r_type == R_X86_64_32
2406 || r_type == R_X86_64_32S))))
2407 eh->func_pointer_refcount += 1;
2413 /* If we are creating a shared library, and this is a reloc
2414 against a global symbol, or a non PC relative reloc
2415 against a local symbol, then we need to copy the reloc
2416 into the shared library. However, if we are linking with
2417 -Bsymbolic, we do not need to copy a reloc against a
2418 global symbol which is defined in an object we are
2419 including in the link (i.e., DEF_REGULAR is set). At
2420 this point we have not seen all the input files, so it is
2421 possible that DEF_REGULAR is not set now but will be set
2422 later (it is never cleared). In case of a weak definition,
2423 DEF_REGULAR may be cleared later by a strong definition in
2424 a shared library. We account for that possibility below by
2425 storing information in the relocs_copied field of the hash
2426 table entry. A similar situation occurs when creating
2427 shared libraries and symbol visibility changes render the
2430 If on the other hand, we are creating an executable, we
2431 may need to keep relocations for symbols satisfied by a
2432 dynamic library if we manage to avoid copy relocs for the
2435 Generate dynamic pointer relocation against STT_GNU_IFUNC
2436 symbol in the non-code section. */
2437 if ((bfd_link_pic (info)
2438 && (! IS_X86_64_PCREL_TYPE (r_type)
2440 && (! (bfd_link_pie (info)
2441 || SYMBOLIC_BIND (info, h))
2442 || h->root.type == bfd_link_hash_defweak
2443 || !h->def_regular))))
2445 && h->type == STT_GNU_IFUNC
2446 && r_type == htab->pointer_r_type
2447 && (sec->flags & SEC_CODE) == 0)
2448 || (ELIMINATE_COPY_RELOCS
2449 && !bfd_link_pic (info)
2451 && (h->root.type == bfd_link_hash_defweak
2452 || !h->def_regular)))
2454 struct elf_dyn_relocs *p;
2455 struct elf_dyn_relocs **head;
2457 /* We must copy these reloc types into the output file.
2458 Create a reloc section in dynobj and make room for
2462 sreloc = _bfd_elf_make_dynamic_reloc_section
2463 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
2464 abfd, /*rela?*/ TRUE);
2470 /* If this is a global symbol, we count the number of
2471 relocations we need for this symbol. */
2473 head = &eh->dyn_relocs;
2476 /* Track dynamic relocs needed for local syms too.
2477 We really need local syms available to do this
2482 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2487 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2491 /* Beware of type punned pointers vs strict aliasing
2493 vpp = &(elf_section_data (s)->local_dynrel);
2494 head = (struct elf_dyn_relocs **)vpp;
2498 if (p == NULL || p->sec != sec)
2500 bfd_size_type amt = sizeof *p;
2502 p = ((struct elf_dyn_relocs *)
2503 bfd_alloc (htab->elf.dynobj, amt));
2514 /* Count size relocation as PC-relative relocation. */
2515 if (IS_X86_64_PCREL_TYPE (r_type) || size_reloc)
2520 /* This relocation describes the C++ object vtable hierarchy.
2521 Reconstruct it for later use during GC. */
2522 case R_X86_64_GNU_VTINHERIT:
2523 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2527 /* This relocation describes which C++ vtable entries are actually
2528 used. Record for later use during GC. */
2529 case R_X86_64_GNU_VTENTRY:
2530 BFD_ASSERT (h != NULL);
2532 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2540 if ((r_type == R_X86_64_GOTPCREL
2541 || r_type == R_X86_64_GOTPCRELX
2542 || r_type == R_X86_64_REX_GOTPCRELX)
2543 && (h == NULL || h->type != STT_GNU_IFUNC))
2544 sec->need_convert_load = 1;
2547 if (elf_section_data (sec)->this_hdr.contents != contents)
2549 if (!info->keep_memory)
2553 /* Cache the section contents for elf_link_input_bfd. */
2554 elf_section_data (sec)->this_hdr.contents = contents;
2561 if (elf_section_data (sec)->this_hdr.contents != contents)
2563 sec->check_relocs_failed = 1;
2567 /* Return the section that should be marked against GC for a given
2571 elf_x86_64_gc_mark_hook (asection *sec,
2572 struct bfd_link_info *info,
2573 Elf_Internal_Rela *rel,
2574 struct elf_link_hash_entry *h,
2575 Elf_Internal_Sym *sym)
2578 switch (ELF32_R_TYPE (rel->r_info))
2580 case R_X86_64_GNU_VTINHERIT:
2581 case R_X86_64_GNU_VTENTRY:
2585 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2588 /* Adjust a symbol defined by a dynamic object and referenced by a
2589 regular object. The current definition is in some section of the
2590 dynamic object, but we're not including those sections. We have to
2591 change the definition to something the rest of the link can
2595 elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
2596 struct elf_link_hash_entry *h)
2598 struct elf_x86_link_hash_table *htab;
2600 struct elf_x86_link_hash_entry *eh;
2601 struct elf_dyn_relocs *p;
2603 /* STT_GNU_IFUNC symbol must go through PLT. */
2604 if (h->type == STT_GNU_IFUNC)
2606 /* All local STT_GNU_IFUNC references must be treate as local
2607 calls via local PLT. */
2609 && SYMBOL_CALLS_LOCAL (info, h))
2611 bfd_size_type pc_count = 0, count = 0;
2612 struct elf_dyn_relocs **pp;
2614 eh = (struct elf_x86_link_hash_entry *) h;
2615 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2617 pc_count += p->pc_count;
2618 p->count -= p->pc_count;
2627 if (pc_count || count)
2632 /* Increment PLT reference count only for PC-relative
2635 if (h->plt.refcount <= 0)
2636 h->plt.refcount = 1;
2638 h->plt.refcount += 1;
2643 if (h->plt.refcount <= 0)
2645 h->plt.offset = (bfd_vma) -1;
2651 /* If this is a function, put it in the procedure linkage table. We
2652 will fill in the contents of the procedure linkage table later,
2653 when we know the address of the .got section. */
2654 if (h->type == STT_FUNC
2657 if (h->plt.refcount <= 0
2658 || SYMBOL_CALLS_LOCAL (info, h)
2659 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2660 && h->root.type == bfd_link_hash_undefweak))
2662 /* This case can occur if we saw a PLT32 reloc in an input
2663 file, but the symbol was never referred to by a dynamic
2664 object, or if all references were garbage collected. In
2665 such a case, we don't actually need to build a procedure
2666 linkage table, and we can just do a PC32 reloc instead. */
2667 h->plt.offset = (bfd_vma) -1;
2674 /* It's possible that we incorrectly decided a .plt reloc was
2675 needed for an R_X86_64_PC32 reloc to a non-function sym in
2676 check_relocs. We can't decide accurately between function and
2677 non-function syms in check-relocs; Objects loaded later in
2678 the link may change h->type. So fix it now. */
2679 h->plt.offset = (bfd_vma) -1;
2681 eh = (struct elf_x86_link_hash_entry *) h;
2683 /* If this is a weak symbol, and there is a real definition, the
2684 processor independent code will have arranged for us to see the
2685 real definition first, and we can just use the same value. */
2686 if (h->u.weakdef != NULL)
2688 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2689 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2690 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2691 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2692 if (ELIMINATE_COPY_RELOCS
2693 || info->nocopyreloc
2694 || SYMBOL_NO_COPYRELOC (info, eh))
2696 h->non_got_ref = h->u.weakdef->non_got_ref;
2697 eh->needs_copy = h->u.weakdef->needs_copy;
2702 /* This is a reference to a symbol defined by a dynamic object which
2703 is not a function. */
2705 /* If we are creating a shared library, we must presume that the
2706 only references to the symbol are via the global offset table.
2707 For such cases we need not do anything here; the relocations will
2708 be handled correctly by relocate_section. */
2709 if (!bfd_link_executable (info))
2712 /* If there are no references to this symbol that do not use the
2713 GOT, we don't need to generate a copy reloc. */
2714 if (!h->non_got_ref)
2717 /* If -z nocopyreloc was given, we won't generate them either. */
2718 if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
2724 if (ELIMINATE_COPY_RELOCS)
2726 eh = (struct elf_x86_link_hash_entry *) h;
2727 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2729 s = p->sec->output_section;
2730 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2734 /* If we didn't find any dynamic relocs in read-only sections, then
2735 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2743 /* We must allocate the symbol in our .dynbss section, which will
2744 become part of the .bss section of the executable. There will be
2745 an entry for this symbol in the .dynsym section. The dynamic
2746 object will contain position independent code, so all references
2747 from the dynamic object to this symbol will go through the global
2748 offset table. The dynamic linker will use the .dynsym entry to
2749 determine the address it must put in the global offset table, so
2750 both the dynamic object and the regular object will refer to the
2751 same memory location for the variable. */
2753 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2757 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
2758 to copy the initial value out of the dynamic object and into the
2759 runtime process image. */
2760 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2762 s = htab->elf.sdynrelro;
2763 srel = htab->elf.sreldynrelro;
2767 s = htab->elf.sdynbss;
2768 srel = htab->elf.srelbss;
2770 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2772 const struct elf_backend_data *bed;
2773 bed = get_elf_backend_data (info->output_bfd);
2774 srel->size += bed->s->sizeof_rela;
2778 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2781 /* Allocate space in .plt, .got and associated reloc sections for
2785 elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2787 struct bfd_link_info *info;
2788 struct elf_x86_link_hash_table *htab;
2789 struct elf_x86_link_hash_entry *eh;
2790 struct elf_dyn_relocs *p;
2791 const struct elf_backend_data *bed;
2792 unsigned int plt_entry_size;
2793 bfd_boolean resolved_to_zero;
2794 const struct elf_x86_64_plt_layout *plt_layout;
2795 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt_layout;
2797 if (h->root.type == bfd_link_hash_indirect)
2800 eh = (struct elf_x86_link_hash_entry *) h;
2802 info = (struct bfd_link_info *) inf;
2803 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2806 bed = get_elf_backend_data (info->output_bfd);
2808 plt_layout = &elf_x86_64_plt (htab);
2809 non_lazy_plt_layout = elf_x86_64_non_lazy_plt (htab);
2810 plt_entry_size = plt_layout->plt_entry_size;
2812 resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2817 /* We can't use the GOT PLT if pointer equality is needed since
2818 finish_dynamic_symbol won't clear symbol value and the dynamic
2819 linker won't update the GOT slot. We will get into an infinite
2820 loop at run-time. */
2821 if (htab->plt_got != NULL
2822 && h->type != STT_GNU_IFUNC
2823 && !h->pointer_equality_needed
2824 && h->plt.refcount > 0
2825 && h->got.refcount > 0)
2827 /* Don't use the regular PLT if there are both GOT and GOTPLT
2829 h->plt.offset = (bfd_vma) -1;
2831 /* Use the GOT PLT. */
2832 eh->plt_got.refcount = 1;
2835 /* Clear the reference count of function pointer relocations if
2836 symbol isn't a normal function. */
2837 if (h->type != STT_FUNC)
2838 eh->func_pointer_refcount = 0;
2840 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2841 here if it is defined and referenced in a non-shared object. */
2842 if (h->type == STT_GNU_IFUNC
2845 if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h,
2847 &htab->readonly_dynrelocs_against_ifunc,
2849 (plt_layout->has_plt0
2851 GOT_ENTRY_SIZE, TRUE))
2853 asection *s = htab->plt_second;
2854 if (h->plt.offset != (bfd_vma) -1 && s != NULL)
2856 /* Use the second PLT section if it is created. */
2857 eh->plt_second.offset = s->size;
2859 /* Make room for this entry in the second PLT section. */
2860 s->size += non_lazy_plt_layout->plt_entry_size;
2868 /* Don't create the PLT entry if there are only function pointer
2869 relocations which can be resolved at run-time. */
2870 else if (htab->elf.dynamic_sections_created
2871 && (h->plt.refcount > eh->func_pointer_refcount
2872 || eh->plt_got.refcount > 0))
2874 bfd_boolean use_plt_got = eh->plt_got.refcount > 0;
2876 /* Clear the reference count of function pointer relocations
2878 eh->func_pointer_refcount = 0;
2880 /* Make sure this symbol is output as a dynamic symbol.
2881 Undefined weak syms won't yet be marked as dynamic. */
2882 if (h->dynindx == -1
2884 && !resolved_to_zero
2885 && h->root.type == bfd_link_hash_undefweak)
2887 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2891 if (bfd_link_pic (info)
2892 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2894 asection *s = htab->elf.splt;
2895 asection *second_s = htab->plt_second;
2896 asection *got_s = htab->plt_got;
2898 /* If this is the first .plt entry, make room for the special
2899 first entry. The .plt section is used by prelink to undo
2900 prelinking for dynamic relocations. */
2902 s->size = plt_layout->has_plt0 * plt_entry_size;
2905 eh->plt_got.offset = got_s->size;
2908 h->plt.offset = s->size;
2910 eh->plt_second.offset = second_s->size;
2913 /* If this symbol is not defined in a regular file, and we are
2914 not generating a shared library, then set the symbol to this
2915 location in the .plt. This is required to make function
2916 pointers compare as equal between the normal executable and
2917 the shared library. */
2918 if (! bfd_link_pic (info)
2923 /* We need to make a call to the entry of the GOT PLT
2924 instead of regular PLT entry. */
2925 h->root.u.def.section = got_s;
2926 h->root.u.def.value = eh->plt_got.offset;
2932 /* We need to make a call to the entry of the
2933 second PLT instead of regular PLT entry. */
2934 h->root.u.def.section = second_s;
2935 h->root.u.def.value = eh->plt_second.offset;
2939 h->root.u.def.section = s;
2940 h->root.u.def.value = h->plt.offset;
2945 /* Make room for this entry. */
2947 got_s->size += non_lazy_plt_layout->plt_entry_size;
2950 s->size += plt_entry_size;
2952 second_s->size += non_lazy_plt_layout->plt_entry_size;
2954 /* We also need to make an entry in the .got.plt section,
2955 which will be placed in the .got section by the linker
2957 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
2959 /* There should be no PLT relocation against resolved
2960 undefined weak symbol in executable. */
2961 if (!resolved_to_zero)
2963 /* We also need to make an entry in the .rela.plt
2965 htab->elf.srelplt->size += bed->s->sizeof_rela;
2966 htab->elf.srelplt->reloc_count++;
2972 eh->plt_got.offset = (bfd_vma) -1;
2973 h->plt.offset = (bfd_vma) -1;
2979 eh->plt_got.offset = (bfd_vma) -1;
2980 h->plt.offset = (bfd_vma) -1;
2984 eh->tlsdesc_got = (bfd_vma) -1;
2986 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
2987 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
2988 if (h->got.refcount > 0
2989 && bfd_link_executable (info)
2991 && elf_x86_hash_entry (h)->tls_type == GOT_TLS_IE)
2993 h->got.offset = (bfd_vma) -1;
2995 else if (h->got.refcount > 0)
2999 int tls_type = elf_x86_hash_entry (h)->tls_type;
3001 /* Make sure this symbol is output as a dynamic symbol.
3002 Undefined weak syms won't yet be marked as dynamic. */
3003 if (h->dynindx == -1
3005 && !resolved_to_zero
3006 && h->root.type == bfd_link_hash_undefweak)
3008 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3012 if (GOT_TLS_GDESC_P (tls_type))
3014 eh->tlsdesc_got = htab->elf.sgotplt->size
3015 - elf_x86_64_compute_jump_table_size (htab);
3016 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
3017 h->got.offset = (bfd_vma) -2;
3019 if (! GOT_TLS_GDESC_P (tls_type)
3020 || GOT_TLS_GD_P (tls_type))
3023 h->got.offset = s->size;
3024 s->size += GOT_ENTRY_SIZE;
3025 if (GOT_TLS_GD_P (tls_type))
3026 s->size += GOT_ENTRY_SIZE;
3028 dyn = htab->elf.dynamic_sections_created;
3029 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
3030 and two if global. R_X86_64_GOTTPOFF needs one dynamic
3031 relocation. No dynamic relocation against resolved undefined
3032 weak symbol in executable. */
3033 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
3034 || tls_type == GOT_TLS_IE)
3035 htab->elf.srelgot->size += bed->s->sizeof_rela;
3036 else if (GOT_TLS_GD_P (tls_type))
3037 htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
3038 else if (! GOT_TLS_GDESC_P (tls_type)
3039 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3040 && !resolved_to_zero)
3041 || h->root.type != bfd_link_hash_undefweak)
3042 && (bfd_link_pic (info)
3043 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3044 htab->elf.srelgot->size += bed->s->sizeof_rela;
3045 if (GOT_TLS_GDESC_P (tls_type))
3047 htab->elf.srelplt->size += bed->s->sizeof_rela;
3048 htab->tlsdesc_plt = (bfd_vma) -1;
3052 h->got.offset = (bfd_vma) -1;
3054 if (eh->dyn_relocs == NULL)
3057 /* In the shared -Bsymbolic case, discard space allocated for
3058 dynamic pc-relative relocs against symbols which turn out to be
3059 defined in regular objects. For the normal shared case, discard
3060 space for pc-relative relocs that have become local due to symbol
3061 visibility changes. */
3063 if (bfd_link_pic (info))
3065 /* Relocs that use pc_count are those that appear on a call
3066 insn, or certain REL relocs that can generated via assembly.
3067 We want calls to protected symbols to resolve directly to the
3068 function rather than going via the plt. If people want
3069 function pointer comparisons to work as expected then they
3070 should avoid writing weird assembly. */
3071 if (SYMBOL_CALLS_LOCAL (info, h))
3073 struct elf_dyn_relocs **pp;
3075 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3077 p->count -= p->pc_count;
3086 /* Also discard relocs on undefined weak syms with non-default
3087 visibility or in PIE. */
3088 if (eh->dyn_relocs != NULL)
3090 if (h->root.type == bfd_link_hash_undefweak)
3092 /* Undefined weak symbol is never bound locally in shared
3094 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3095 || resolved_to_zero)
3096 eh->dyn_relocs = NULL;
3097 else if (h->dynindx == -1
3098 && ! h->forced_local
3099 && ! bfd_elf_link_record_dynamic_symbol (info, h))
3102 /* For PIE, discard space for pc-relative relocs against
3103 symbols which turn out to need copy relocs. */
3104 else if (bfd_link_executable (info)
3105 && (h->needs_copy || eh->needs_copy)
3109 struct elf_dyn_relocs **pp;
3111 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3113 if (p->pc_count != 0)
3121 else if (ELIMINATE_COPY_RELOCS)
3123 /* For the non-shared case, discard space for relocs against
3124 symbols which turn out to need copy relocs or are not
3125 dynamic. Keep dynamic relocations for run-time function
3126 pointer initialization. */
3128 if ((!h->non_got_ref
3129 || eh->func_pointer_refcount > 0
3130 || (h->root.type == bfd_link_hash_undefweak
3131 && !resolved_to_zero))
3134 || (htab->elf.dynamic_sections_created
3135 && (h->root.type == bfd_link_hash_undefweak
3136 || h->root.type == bfd_link_hash_undefined))))
3138 /* Make sure this symbol is output as a dynamic symbol.
3139 Undefined weak syms won't yet be marked as dynamic. */
3140 if (h->dynindx == -1
3141 && ! h->forced_local
3142 && ! resolved_to_zero
3143 && h->root.type == bfd_link_hash_undefweak
3144 && ! bfd_elf_link_record_dynamic_symbol (info, h))
3147 /* If that succeeded, we know we'll be keeping all the
3149 if (h->dynindx != -1)
3153 eh->dyn_relocs = NULL;
3154 eh->func_pointer_refcount = 0;
3159 /* Finally, allocate space. */
3160 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3164 sreloc = elf_section_data (p->sec)->sreloc;
3166 BFD_ASSERT (sreloc != NULL);
3168 sreloc->size += p->count * bed->s->sizeof_rela;
3174 /* Allocate space in .plt, .got and associated reloc sections for
3175 local dynamic relocs. */
3178 elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
3180 struct elf_link_hash_entry *h
3181 = (struct elf_link_hash_entry *) *slot;
3183 if (h->type != STT_GNU_IFUNC
3187 || h->root.type != bfd_link_hash_defined)
3190 return elf_x86_64_allocate_dynrelocs (h, inf);
3193 /* Convert load via the GOT slot to load immediate. */
3196 elf_x86_64_convert_load (bfd *abfd, asection *sec,
3197 struct bfd_link_info *link_info)
3199 Elf_Internal_Shdr *symtab_hdr;
3200 Elf_Internal_Rela *internal_relocs;
3201 Elf_Internal_Rela *irel, *irelend;
3203 struct elf_x86_link_hash_table *htab;
3204 bfd_boolean changed;
3205 bfd_signed_vma *local_got_refcounts;
3207 /* Don't even try to convert non-ELF outputs. */
3208 if (!is_elf_hash_table (link_info->hash))
3211 /* Nothing to do if there is no need or no output. */
3212 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
3213 || sec->need_convert_load == 0
3214 || bfd_is_abs_section (sec->output_section))
3217 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3219 /* Load the relocations for this section. */
3220 internal_relocs = (_bfd_elf_link_read_relocs
3221 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
3222 link_info->keep_memory));
3223 if (internal_relocs == NULL)
3227 htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
3228 local_got_refcounts = elf_local_got_refcounts (abfd);
3230 /* Get the section contents. */
3231 if (elf_section_data (sec)->this_hdr.contents != NULL)
3232 contents = elf_section_data (sec)->this_hdr.contents;
3235 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3239 irelend = internal_relocs + sec->reloc_count;
3240 for (irel = internal_relocs; irel < irelend; irel++)
3242 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
3243 unsigned int r_symndx;
3244 struct elf_link_hash_entry *h;
3245 bfd_boolean converted;
3247 if (r_type != R_X86_64_GOTPCRELX
3248 && r_type != R_X86_64_REX_GOTPCRELX
3249 && r_type != R_X86_64_GOTPCREL)
3252 r_symndx = htab->r_sym (irel->r_info);
3253 if (r_symndx < symtab_hdr->sh_info)
3254 h = _bfd_elf_x86_get_local_sym_hash (htab, sec->owner,
3255 (const Elf_Internal_Rela *) irel,
3259 h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
3260 while (h->root.type == bfd_link_hash_indirect
3261 || h->root.type == bfd_link_hash_warning)
3262 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3265 /* STT_GNU_IFUNC must keep GOTPCREL relocations. */
3266 if (h != NULL && h->type == STT_GNU_IFUNC)
3270 if (!elf_x86_64_convert_load_reloc (abfd, sec, contents, irel, h,
3271 &converted, link_info))
3276 changed = converted;
3279 if (h->got.refcount > 0)
3280 h->got.refcount -= 1;
3284 if (local_got_refcounts != NULL
3285 && local_got_refcounts[r_symndx] > 0)
3286 local_got_refcounts[r_symndx] -= 1;
3291 if (contents != NULL
3292 && elf_section_data (sec)->this_hdr.contents != contents)
3294 if (!changed && !link_info->keep_memory)
3298 /* Cache the section contents for elf_link_input_bfd. */
3299 elf_section_data (sec)->this_hdr.contents = contents;
3303 if (elf_section_data (sec)->relocs != internal_relocs)
3306 free (internal_relocs);
3308 elf_section_data (sec)->relocs = internal_relocs;
3314 if (contents != NULL
3315 && elf_section_data (sec)->this_hdr.contents != contents)
3317 if (internal_relocs != NULL
3318 && elf_section_data (sec)->relocs != internal_relocs)
3319 free (internal_relocs);
3323 /* Set the sizes of the dynamic sections. */
3326 elf_x86_64_size_dynamic_sections (bfd *output_bfd,
3327 struct bfd_link_info *info)
3329 struct elf_x86_link_hash_table *htab;
3334 const struct elf_backend_data *bed;
3335 const struct elf_x86_64_plt_layout *plt_layout;
3336 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt_layout;
3338 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
3341 bed = get_elf_backend_data (output_bfd);
3343 dynobj = htab->elf.dynobj;
3347 plt_layout = &elf_x86_64_plt (htab);
3348 non_lazy_plt_layout = elf_x86_64_non_lazy_plt (htab);
3350 /* Set up .got offsets for local syms, and space for local dynamic
3352 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3354 bfd_signed_vma *local_got;
3355 bfd_signed_vma *end_local_got;
3356 char *local_tls_type;
3357 bfd_vma *local_tlsdesc_gotent;
3358 bfd_size_type locsymcount;
3359 Elf_Internal_Shdr *symtab_hdr;
3362 if (! is_x86_64_elf (ibfd))
3365 for (s = ibfd->sections; s != NULL; s = s->next)
3367 struct elf_dyn_relocs *p;
3369 if (!elf_x86_64_convert_load (ibfd, s, info))
3372 for (p = (struct elf_dyn_relocs *)
3373 (elf_section_data (s)->local_dynrel);
3377 if (!bfd_is_abs_section (p->sec)
3378 && bfd_is_abs_section (p->sec->output_section))
3380 /* Input section has been discarded, either because
3381 it is a copy of a linkonce section or due to
3382 linker script /DISCARD/, so we'll be discarding
3385 else if (p->count != 0)
3387 srel = elf_section_data (p->sec)->sreloc;
3388 srel->size += p->count * bed->s->sizeof_rela;
3389 if ((p->sec->output_section->flags & SEC_READONLY) != 0
3390 && (info->flags & DF_TEXTREL) == 0)
3392 info->flags |= DF_TEXTREL;
3393 if ((info->warn_shared_textrel && bfd_link_pic (info))
3394 || info->error_textrel)
3395 /* xgettext:c-format */
3396 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
3397 p->sec->owner, p->sec);
3403 local_got = elf_local_got_refcounts (ibfd);
3407 symtab_hdr = &elf_symtab_hdr (ibfd);
3408 locsymcount = symtab_hdr->sh_info;
3409 end_local_got = local_got + locsymcount;
3410 local_tls_type = elf_x86_local_got_tls_type (ibfd);
3411 local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
3413 srel = htab->elf.srelgot;
3414 for (; local_got < end_local_got;
3415 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
3417 *local_tlsdesc_gotent = (bfd_vma) -1;
3420 if (GOT_TLS_GDESC_P (*local_tls_type))
3422 *local_tlsdesc_gotent = htab->elf.sgotplt->size
3423 - elf_x86_64_compute_jump_table_size (htab);
3424 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
3425 *local_got = (bfd_vma) -2;
3427 if (! GOT_TLS_GDESC_P (*local_tls_type)
3428 || GOT_TLS_GD_P (*local_tls_type))
3430 *local_got = s->size;
3431 s->size += GOT_ENTRY_SIZE;
3432 if (GOT_TLS_GD_P (*local_tls_type))
3433 s->size += GOT_ENTRY_SIZE;
3435 if (bfd_link_pic (info)
3436 || GOT_TLS_GD_ANY_P (*local_tls_type)
3437 || *local_tls_type == GOT_TLS_IE)
3439 if (GOT_TLS_GDESC_P (*local_tls_type))
3441 htab->elf.srelplt->size
3442 += bed->s->sizeof_rela;
3443 htab->tlsdesc_plt = (bfd_vma) -1;
3445 if (! GOT_TLS_GDESC_P (*local_tls_type)
3446 || GOT_TLS_GD_P (*local_tls_type))
3447 srel->size += bed->s->sizeof_rela;
3451 *local_got = (bfd_vma) -1;
3455 if (htab->tls_ld_or_ldm_got.refcount > 0)
3457 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
3459 htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
3460 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
3461 htab->elf.srelgot->size += bed->s->sizeof_rela;
3464 htab->tls_ld_or_ldm_got.offset = -1;
3466 /* Allocate global sym .plt and .got entries, and space for global
3467 sym dynamic relocs. */
3468 elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
3471 /* Allocate .plt and .got entries, and space for local symbols. */
3472 htab_traverse (htab->loc_hash_table,
3473 elf_x86_64_allocate_local_dynrelocs,
3476 /* For every jump slot reserved in the sgotplt, reloc_count is
3477 incremented. However, when we reserve space for TLS descriptors,
3478 it's not incremented, so in order to compute the space reserved
3479 for them, it suffices to multiply the reloc count by the jump
3482 PR ld/13302: We start next_irelative_index at the end of .rela.plt
3483 so that R_X86_64_IRELATIVE entries come last. */
3484 if (htab->elf.srelplt)
3486 htab->sgotplt_jump_table_size
3487 = elf_x86_64_compute_jump_table_size (htab);
3488 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
3490 else if (htab->elf.irelplt)
3491 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
3493 if (htab->tlsdesc_plt)
3495 /* If we're not using lazy TLS relocations, don't generate the
3496 PLT and GOT entries they require. */
3497 if ((info->flags & DF_BIND_NOW))
3498 htab->tlsdesc_plt = 0;
3501 htab->tlsdesc_got = htab->elf.sgot->size;
3502 htab->elf.sgot->size += GOT_ENTRY_SIZE;
3503 /* Reserve room for the initial entry.
3504 FIXME: we could probably do away with it in this case. */
3505 if (htab->elf.splt->size == 0)
3506 htab->elf.splt->size = plt_layout->plt_entry_size;
3507 htab->tlsdesc_plt = htab->elf.splt->size;
3508 htab->elf.splt->size += plt_layout->plt_entry_size;
3512 if (htab->elf.sgotplt)
3514 /* Don't allocate .got.plt section if there are no GOT nor PLT
3515 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
3516 if ((htab->elf.hgot == NULL
3517 || !htab->elf.hgot->ref_regular_nonweak)
3518 && (htab->elf.sgotplt->size
3519 == get_elf_backend_data (output_bfd)->got_header_size)
3520 && (htab->elf.splt == NULL
3521 || htab->elf.splt->size == 0)
3522 && (htab->elf.sgot == NULL
3523 || htab->elf.sgot->size == 0)
3524 && (htab->elf.iplt == NULL
3525 || htab->elf.iplt->size == 0)
3526 && (htab->elf.igotplt == NULL
3527 || htab->elf.igotplt->size == 0))
3528 htab->elf.sgotplt->size = 0;
3531 if (_bfd_elf_eh_frame_present (info))
3533 if (htab->plt_eh_frame != NULL
3534 && htab->elf.splt != NULL
3535 && htab->elf.splt->size != 0
3536 && !bfd_is_abs_section (htab->elf.splt->output_section))
3537 htab->plt_eh_frame->size = plt_layout->eh_frame_plt_size;
3539 if (htab->plt_got_eh_frame != NULL
3540 && htab->plt_got != NULL
3541 && htab->plt_got->size != 0
3542 && !bfd_is_abs_section (htab->plt_got->output_section))
3543 htab->plt_got_eh_frame->size
3544 = non_lazy_plt_layout->eh_frame_plt_size;
3546 /* Unwind info for the second PLT and .plt.got sections are
3548 if (htab->plt_second_eh_frame != NULL
3549 && htab->plt_second != NULL
3550 && htab->plt_second->size != 0
3551 && !bfd_is_abs_section (htab->plt_second->output_section))
3552 htab->plt_second_eh_frame->size
3553 = non_lazy_plt_layout->eh_frame_plt_size;
3556 /* We now have determined the sizes of the various dynamic sections.
3557 Allocate memory for them. */
3559 for (s = dynobj->sections; s != NULL; s = s->next)
3561 if ((s->flags & SEC_LINKER_CREATED) == 0)
3564 if (s == htab->elf.splt
3565 || s == htab->elf.sgot
3566 || s == htab->elf.sgotplt
3567 || s == htab->elf.iplt
3568 || s == htab->elf.igotplt
3569 || s == htab->plt_second
3570 || s == htab->plt_got
3571 || s == htab->plt_eh_frame
3572 || s == htab->plt_got_eh_frame
3573 || s == htab->plt_second_eh_frame
3574 || s == htab->elf.sdynbss
3575 || s == htab->elf.sdynrelro)
3577 /* Strip this section if we don't need it; see the
3580 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
3582 if (s->size != 0 && s != htab->elf.srelplt)
3585 /* We use the reloc_count field as a counter if we need
3586 to copy relocs into the output file. */
3587 if (s != htab->elf.srelplt)
3592 /* It's not one of our sections, so don't allocate space. */
3598 /* If we don't need this section, strip it from the
3599 output file. This is mostly to handle .rela.bss and
3600 .rela.plt. We must create both sections in
3601 create_dynamic_sections, because they must be created
3602 before the linker maps input sections to output
3603 sections. The linker does that before
3604 adjust_dynamic_symbol is called, and it is that
3605 function which decides whether anything needs to go
3606 into these sections. */
3608 s->flags |= SEC_EXCLUDE;
3612 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3615 /* Allocate memory for the section contents. We use bfd_zalloc
3616 here in case unused entries are not reclaimed before the
3617 section's contents are written out. This should not happen,
3618 but this way if it does, we get a R_X86_64_NONE reloc instead
3620 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3621 if (s->contents == NULL)
3625 if (htab->plt_eh_frame != NULL
3626 && htab->plt_eh_frame->contents != NULL)
3628 memcpy (htab->plt_eh_frame->contents,
3629 plt_layout->eh_frame_plt, htab->plt_eh_frame->size);
3630 bfd_put_32 (dynobj, htab->elf.splt->size,
3631 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3634 if (htab->plt_got_eh_frame != NULL
3635 && htab->plt_got_eh_frame->contents != NULL)
3637 memcpy (htab->plt_got_eh_frame->contents,
3638 non_lazy_plt_layout->eh_frame_plt,
3639 htab->plt_got_eh_frame->size);
3640 bfd_put_32 (dynobj, htab->plt_got->size,
3641 (htab->plt_got_eh_frame->contents
3642 + PLT_FDE_LEN_OFFSET));
3645 if (htab->plt_second_eh_frame != NULL
3646 && htab->plt_second_eh_frame->contents != NULL)
3648 memcpy (htab->plt_second_eh_frame->contents,
3649 non_lazy_plt_layout->eh_frame_plt,
3650 htab->plt_second_eh_frame->size);
3651 bfd_put_32 (dynobj, htab->plt_second->size,
3652 (htab->plt_second_eh_frame->contents
3653 + PLT_FDE_LEN_OFFSET));
3656 if (htab->elf.dynamic_sections_created)
3658 /* Add some entries to the .dynamic section. We fill in the
3659 values later, in elf_x86_64_finish_dynamic_sections, but we
3660 must add the entries now so that we get the correct size for
3661 the .dynamic section. The DT_DEBUG entry is filled in by the
3662 dynamic linker and used by the debugger. */
3663 #define add_dynamic_entry(TAG, VAL) \
3664 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3666 if (bfd_link_executable (info))
3668 if (!add_dynamic_entry (DT_DEBUG, 0))
3672 if (htab->elf.splt->size != 0)
3674 /* DT_PLTGOT is used by prelink even if there is no PLT
3676 if (!add_dynamic_entry (DT_PLTGOT, 0))
3680 if (htab->elf.srelplt->size != 0)
3682 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3683 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3684 || !add_dynamic_entry (DT_JMPREL, 0))
3688 if (htab->tlsdesc_plt
3689 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
3690 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
3695 if (!add_dynamic_entry (DT_RELA, 0)
3696 || !add_dynamic_entry (DT_RELASZ, 0)
3697 || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
3700 /* If any dynamic relocs apply to a read-only section,
3701 then we need a DT_TEXTREL entry. */
3702 if ((info->flags & DF_TEXTREL) == 0)
3703 elf_link_hash_traverse (&htab->elf,
3704 _bfd_x86_elf_readonly_dynrelocs,
3707 if ((info->flags & DF_TEXTREL) != 0)
3709 if (htab->readonly_dynrelocs_against_ifunc)
3711 info->callbacks->einfo
3712 (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
3713 bfd_set_error (bfd_error_bad_value);
3717 if (!add_dynamic_entry (DT_TEXTREL, 0))
3722 #undef add_dynamic_entry
3727 /* Return the relocation value for @tpoff relocation
3728 if STT_TLS virtual address is ADDRESS. */
3731 elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
3733 struct elf_link_hash_table *htab = elf_hash_table (info);
3734 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3735 bfd_vma static_tls_size;
3737 /* If tls_segment is NULL, we should have signalled an error already. */
3738 if (htab->tls_sec == NULL)
3741 /* Consider special static TLS alignment requirements. */
3742 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3743 return address - static_tls_size - htab->tls_sec->vma;
3746 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
3750 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
3752 /* Opcode Instruction
3755 0x0f 0x8x conditional jump */
3757 && (contents [offset - 1] == 0xe8
3758 || contents [offset - 1] == 0xe9))
3760 && contents [offset - 2] == 0x0f
3761 && (contents [offset - 1] & 0xf0) == 0x80));
3764 /* Relocate an x86_64 ELF section. */
3767 elf_x86_64_relocate_section (bfd *output_bfd,
3768 struct bfd_link_info *info,
3770 asection *input_section,
3772 Elf_Internal_Rela *relocs,
3773 Elf_Internal_Sym *local_syms,
3774 asection **local_sections)
3776 struct elf_x86_link_hash_table *htab;
3777 Elf_Internal_Shdr *symtab_hdr;
3778 struct elf_link_hash_entry **sym_hashes;
3779 bfd_vma *local_got_offsets;
3780 bfd_vma *local_tlsdesc_gotents;
3781 Elf_Internal_Rela *rel;
3782 Elf_Internal_Rela *wrel;
3783 Elf_Internal_Rela *relend;
3784 unsigned int plt_entry_size;
3786 BFD_ASSERT (is_x86_64_elf (input_bfd));
3788 /* Skip if check_relocs failed. */
3789 if (input_section->check_relocs_failed)
3792 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
3795 plt_entry_size = elf_x86_64_plt (htab).plt_entry_size;
3796 symtab_hdr = &elf_symtab_hdr (input_bfd);
3797 sym_hashes = elf_sym_hashes (input_bfd);
3798 local_got_offsets = elf_local_got_offsets (input_bfd);
3799 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
3801 _bfd_x86_elf_set_tls_module_base (info);
3803 rel = wrel = relocs;
3804 relend = relocs + input_section->reloc_count;
3805 for (; rel < relend; wrel++, rel++)
3807 unsigned int r_type;
3808 reloc_howto_type *howto;
3809 unsigned long r_symndx;
3810 struct elf_link_hash_entry *h;
3811 struct elf_x86_link_hash_entry *eh;
3812 Elf_Internal_Sym *sym;
3814 bfd_vma off, offplt, plt_offset;
3816 bfd_boolean unresolved_reloc;
3817 bfd_reloc_status_type r;
3819 asection *base_got, *resolved_plt;
3821 bfd_boolean resolved_to_zero;
3822 bfd_boolean relative_reloc;
3824 r_type = ELF32_R_TYPE (rel->r_info);
3825 if (r_type == (int) R_X86_64_GNU_VTINHERIT
3826 || r_type == (int) R_X86_64_GNU_VTENTRY)
3833 if (r_type >= (int) R_X86_64_standard)
3834 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
3836 if (r_type != (int) R_X86_64_32
3837 || ABI_64_P (output_bfd))
3838 howto = x86_64_elf_howto_table + r_type;
3840 howto = (x86_64_elf_howto_table
3841 + ARRAY_SIZE (x86_64_elf_howto_table) - 1);
3842 r_symndx = htab->r_sym (rel->r_info);
3846 unresolved_reloc = FALSE;
3847 if (r_symndx < symtab_hdr->sh_info)
3849 sym = local_syms + r_symndx;
3850 sec = local_sections[r_symndx];
3852 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
3854 st_size = sym->st_size;
3856 /* Relocate against local STT_GNU_IFUNC symbol. */
3857 if (!bfd_link_relocatable (info)
3858 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3860 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd,
3865 /* Set STT_GNU_IFUNC symbol value. */
3866 h->root.u.def.value = sym->st_value;
3867 h->root.u.def.section = sec;
3872 bfd_boolean warned ATTRIBUTE_UNUSED;
3873 bfd_boolean ignored ATTRIBUTE_UNUSED;
3875 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3876 r_symndx, symtab_hdr, sym_hashes,
3878 unresolved_reloc, warned, ignored);
3882 if (sec != NULL && discarded_section (sec))
3884 _bfd_clear_contents (howto, input_bfd, input_section,
3885 contents + rel->r_offset);
3886 wrel->r_offset = rel->r_offset;
3890 /* For ld -r, remove relocations in debug sections against
3891 sections defined in discarded sections. Not done for
3892 eh_frame editing code expects to be present. */
3893 if (bfd_link_relocatable (info)
3894 && (input_section->flags & SEC_DEBUGGING))
3900 if (bfd_link_relocatable (info))
3907 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
3909 if (r_type == R_X86_64_64)
3911 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
3912 zero-extend it to 64bit if addend is zero. */
3913 r_type = R_X86_64_32;
3914 memset (contents + rel->r_offset + 4, 0, 4);
3916 else if (r_type == R_X86_64_SIZE64)
3918 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
3919 zero-extend it to 64bit if addend is zero. */
3920 r_type = R_X86_64_SIZE32;
3921 memset (contents + rel->r_offset + 4, 0, 4);
3925 eh = (struct elf_x86_link_hash_entry *) h;
3927 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3928 it here if it is defined in a non-shared object. */
3930 && h->type == STT_GNU_IFUNC
3936 if ((input_section->flags & SEC_ALLOC) == 0)
3938 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3939 sections because such sections are not SEC_ALLOC and
3940 thus ld.so will not process them. */
3941 if ((input_section->flags & SEC_DEBUGGING) != 0)
3951 case R_X86_64_GOTPCREL:
3952 case R_X86_64_GOTPCRELX:
3953 case R_X86_64_REX_GOTPCRELX:
3954 case R_X86_64_GOTPCREL64:
3955 base_got = htab->elf.sgot;
3956 off = h->got.offset;
3958 if (base_got == NULL)
3961 if (off == (bfd_vma) -1)
3963 /* We can't use h->got.offset here to save state, or
3964 even just remember the offset, as finish_dynamic_symbol
3965 would use that as offset into .got. */
3967 if (h->plt.offset == (bfd_vma) -1)
3970 if (htab->elf.splt != NULL)
3972 plt_index = (h->plt.offset / plt_entry_size
3973 - elf_x86_64_plt (htab).has_plt0);
3974 off = (plt_index + 3) * GOT_ENTRY_SIZE;
3975 base_got = htab->elf.sgotplt;
3979 plt_index = h->plt.offset / plt_entry_size;
3980 off = plt_index * GOT_ENTRY_SIZE;
3981 base_got = htab->elf.igotplt;
3984 if (h->dynindx == -1
3988 /* This references the local defitionion. We must
3989 initialize this entry in the global offset table.
3990 Since the offset must always be a multiple of 8,
3991 we use the least significant bit to record
3992 whether we have initialized it already.
3994 When doing a dynamic link, we create a .rela.got
3995 relocation entry to initialize the value. This
3996 is done in the finish_dynamic_symbol routine. */
4001 bfd_put_64 (output_bfd, relocation,
4002 base_got->contents + off);
4003 /* Note that this is harmless for the GOTPLT64
4004 case, as -1 | 1 still is -1. */
4010 relocation = (base_got->output_section->vma
4011 + base_got->output_offset + off);
4016 if (h->plt.offset == (bfd_vma) -1)
4018 /* Handle static pointers of STT_GNU_IFUNC symbols. */
4019 if (r_type == htab->pointer_r_type
4020 && (input_section->flags & SEC_CODE) == 0)
4021 goto do_ifunc_pointer;
4022 goto bad_ifunc_reloc;
4025 /* STT_GNU_IFUNC symbol must go through PLT. */
4026 if (htab->elf.splt != NULL)
4028 if (htab->plt_second != NULL)
4030 resolved_plt = htab->plt_second;
4031 plt_offset = eh->plt_second.offset;
4035 resolved_plt = htab->elf.splt;
4036 plt_offset = h->plt.offset;
4041 resolved_plt = htab->elf.iplt;
4042 plt_offset = h->plt.offset;
4045 relocation = (resolved_plt->output_section->vma
4046 + resolved_plt->output_offset + plt_offset);
4052 if (h->root.root.string)
4053 name = h->root.root.string;
4055 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4058 /* xgettext:c-format */
4059 (_("%B: relocation %s against STT_GNU_IFUNC "
4060 "symbol `%s' isn't supported"), input_bfd,
4062 bfd_set_error (bfd_error_bad_value);
4066 if (bfd_link_pic (info))
4071 if (ABI_64_P (output_bfd))
4076 if (rel->r_addend != 0)
4078 if (h->root.root.string)
4079 name = h->root.root.string;
4081 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4084 /* xgettext:c-format */
4085 (_("%B: relocation %s against STT_GNU_IFUNC "
4086 "symbol `%s' has non-zero addend: %Ld"),
4087 input_bfd, howto->name, name, rel->r_addend);
4088 bfd_set_error (bfd_error_bad_value);
4092 /* Generate dynamic relcoation only when there is a
4093 non-GOT reference in a shared object or there is no
4095 if ((bfd_link_pic (info) && h->non_got_ref)
4096 || h->plt.offset == (bfd_vma) -1)
4098 Elf_Internal_Rela outrel;
4101 /* Need a dynamic relocation to get the real function
4103 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
4107 if (outrel.r_offset == (bfd_vma) -1
4108 || outrel.r_offset == (bfd_vma) -2)
4111 outrel.r_offset += (input_section->output_section->vma
4112 + input_section->output_offset);
4114 if (h->dynindx == -1
4116 || bfd_link_executable (info))
4118 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
4119 h->root.root.string,
4120 h->root.u.def.section->owner);
4122 /* This symbol is resolved locally. */
4123 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4124 outrel.r_addend = (h->root.u.def.value
4125 + h->root.u.def.section->output_section->vma
4126 + h->root.u.def.section->output_offset);
4130 outrel.r_info = htab->r_info (h->dynindx, r_type);
4131 outrel.r_addend = 0;
4134 /* Dynamic relocations are stored in
4135 1. .rela.ifunc section in PIC object.
4136 2. .rela.got section in dynamic executable.
4137 3. .rela.iplt section in static executable. */
4138 if (bfd_link_pic (info))
4139 sreloc = htab->elf.irelifunc;
4140 else if (htab->elf.splt != NULL)
4141 sreloc = htab->elf.srelgot;
4143 sreloc = htab->elf.irelplt;
4144 elf_append_rela (output_bfd, sreloc, &outrel);
4146 /* If this reloc is against an external symbol, we
4147 do not want to fiddle with the addend. Otherwise,
4148 we need to include the symbol value so that it
4149 becomes an addend for the dynamic reloc. For an
4150 internal symbol, we have updated addend. */
4155 case R_X86_64_PC32_BND:
4157 case R_X86_64_PLT32:
4158 case R_X86_64_PLT32_BND:
4163 resolved_to_zero = (eh != NULL
4164 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
4169 /* When generating a shared object, the relocations handled here are
4170 copied into the output file to be resolved at run time. */
4173 case R_X86_64_GOT32:
4174 case R_X86_64_GOT64:
4175 /* Relocation is to the entry for this symbol in the global
4177 case R_X86_64_GOTPCREL:
4178 case R_X86_64_GOTPCRELX:
4179 case R_X86_64_REX_GOTPCRELX:
4180 case R_X86_64_GOTPCREL64:
4181 /* Use global offset table entry as symbol value. */
4182 case R_X86_64_GOTPLT64:
4183 /* This is obsolete and treated the same as GOT64. */
4184 base_got = htab->elf.sgot;
4186 if (htab->elf.sgot == NULL)
4189 relative_reloc = FALSE;
4194 off = h->got.offset;
4196 && h->plt.offset != (bfd_vma)-1
4197 && off == (bfd_vma)-1)
4199 /* We can't use h->got.offset here to save
4200 state, or even just remember the offset, as
4201 finish_dynamic_symbol would use that as offset into
4203 bfd_vma plt_index = (h->plt.offset / plt_entry_size
4204 - elf_x86_64_plt (htab).has_plt0);
4205 off = (plt_index + 3) * GOT_ENTRY_SIZE;
4206 base_got = htab->elf.sgotplt;
4209 dyn = htab->elf.dynamic_sections_created;
4211 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
4212 || (bfd_link_pic (info)
4213 && SYMBOL_REFERENCES_LOCAL (info, h))
4214 || (ELF_ST_VISIBILITY (h->other)
4215 && h->root.type == bfd_link_hash_undefweak))
4217 /* This is actually a static link, or it is a -Bsymbolic
4218 link and the symbol is defined locally, or the symbol
4219 was forced to be local because of a version file. We
4220 must initialize this entry in the global offset table.
4221 Since the offset must always be a multiple of 8, we
4222 use the least significant bit to record whether we
4223 have initialized it already.
4225 When doing a dynamic link, we create a .rela.got
4226 relocation entry to initialize the value. This is
4227 done in the finish_dynamic_symbol routine. */
4232 bfd_put_64 (output_bfd, relocation,
4233 base_got->contents + off);
4234 /* Note that this is harmless for the GOTPLT64 case,
4235 as -1 | 1 still is -1. */
4238 if (h->dynindx == -1
4240 && h->root.type != bfd_link_hash_undefweak
4241 && bfd_link_pic (info))
4243 /* If this symbol isn't dynamic in PIC,
4244 generate R_X86_64_RELATIVE here. */
4245 eh->no_finish_dynamic_symbol = 1;
4246 relative_reloc = TRUE;
4251 unresolved_reloc = FALSE;
4255 if (local_got_offsets == NULL)
4258 off = local_got_offsets[r_symndx];
4260 /* The offset must always be a multiple of 8. We use
4261 the least significant bit to record whether we have
4262 already generated the necessary reloc. */
4267 bfd_put_64 (output_bfd, relocation,
4268 base_got->contents + off);
4269 local_got_offsets[r_symndx] |= 1;
4271 if (bfd_link_pic (info))
4272 relative_reloc = TRUE;
4279 Elf_Internal_Rela outrel;
4281 /* We need to generate a R_X86_64_RELATIVE reloc
4282 for the dynamic linker. */
4283 s = htab->elf.srelgot;
4287 outrel.r_offset = (base_got->output_section->vma
4288 + base_got->output_offset
4290 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4291 outrel.r_addend = relocation;
4292 elf_append_rela (output_bfd, s, &outrel);
4295 if (off >= (bfd_vma) -2)
4298 relocation = base_got->output_section->vma
4299 + base_got->output_offset + off;
4300 if (r_type != R_X86_64_GOTPCREL
4301 && r_type != R_X86_64_GOTPCRELX
4302 && r_type != R_X86_64_REX_GOTPCRELX
4303 && r_type != R_X86_64_GOTPCREL64)
4304 relocation -= htab->elf.sgotplt->output_section->vma
4305 - htab->elf.sgotplt->output_offset;
4309 case R_X86_64_GOTOFF64:
4310 /* Relocation is relative to the start of the global offset
4313 /* Check to make sure it isn't a protected function or data
4314 symbol for shared library since it may not be local when
4315 used as function address or with copy relocation. We also
4316 need to make sure that a symbol is referenced locally. */
4317 if (bfd_link_pic (info) && h)
4319 if (!h->def_regular)
4323 switch (ELF_ST_VISIBILITY (h->other))
4326 v = _("hidden symbol");
4329 v = _("internal symbol");
4332 v = _("protected symbol");
4340 /* xgettext:c-format */
4341 (_("%B: relocation R_X86_64_GOTOFF64 against undefined %s"
4342 " `%s' can not be used when making a shared object"),
4343 input_bfd, v, h->root.root.string);
4344 bfd_set_error (bfd_error_bad_value);
4347 else if (!bfd_link_executable (info)
4348 && !SYMBOL_REFERENCES_LOCAL (info, h)
4349 && (h->type == STT_FUNC
4350 || h->type == STT_OBJECT)
4351 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
4354 /* xgettext:c-format */
4355 (_("%B: relocation R_X86_64_GOTOFF64 against protected %s"
4356 " `%s' can not be used when making a shared object"),
4358 h->type == STT_FUNC ? "function" : "data",
4359 h->root.root.string);
4360 bfd_set_error (bfd_error_bad_value);
4365 /* Note that sgot is not involved in this
4366 calculation. We always want the start of .got.plt. If we
4367 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
4368 permitted by the ABI, we might have to change this
4370 relocation -= htab->elf.sgotplt->output_section->vma
4371 + htab->elf.sgotplt->output_offset;
4374 case R_X86_64_GOTPC32:
4375 case R_X86_64_GOTPC64:
4376 /* Use global offset table as symbol value. */
4377 relocation = htab->elf.sgotplt->output_section->vma
4378 + htab->elf.sgotplt->output_offset;
4379 unresolved_reloc = FALSE;
4382 case R_X86_64_PLTOFF64:
4383 /* Relocation is PLT entry relative to GOT. For local
4384 symbols it's the symbol itself relative to GOT. */
4386 /* See PLT32 handling. */
4387 && (h->plt.offset != (bfd_vma) -1
4388 || eh->plt_got.offset != (bfd_vma) -1)
4389 && htab->elf.splt != NULL)
4391 if (eh->plt_got.offset != (bfd_vma) -1)
4393 /* Use the GOT PLT. */
4394 resolved_plt = htab->plt_got;
4395 plt_offset = eh->plt_got.offset;
4397 else if (htab->plt_second != NULL)
4399 resolved_plt = htab->plt_second;
4400 plt_offset = eh->plt_second.offset;
4404 resolved_plt = htab->elf.splt;
4405 plt_offset = h->plt.offset;
4408 relocation = (resolved_plt->output_section->vma
4409 + resolved_plt->output_offset
4411 unresolved_reloc = FALSE;
4414 relocation -= htab->elf.sgotplt->output_section->vma
4415 + htab->elf.sgotplt->output_offset;
4418 case R_X86_64_PLT32:
4419 case R_X86_64_PLT32_BND:
4420 /* Relocation is to the entry for this symbol in the
4421 procedure linkage table. */
4423 /* Resolve a PLT32 reloc against a local symbol directly,
4424 without using the procedure linkage table. */
4428 if ((h->plt.offset == (bfd_vma) -1
4429 && eh->plt_got.offset == (bfd_vma) -1)
4430 || htab->elf.splt == NULL)
4432 /* We didn't make a PLT entry for this symbol. This
4433 happens when statically linking PIC code, or when
4434 using -Bsymbolic. */
4438 if (h->plt.offset != (bfd_vma) -1)
4440 if (htab->plt_second != NULL)
4442 resolved_plt = htab->plt_second;
4443 plt_offset = eh->plt_second.offset;
4447 resolved_plt = htab->elf.splt;
4448 plt_offset = h->plt.offset;
4453 /* Use the GOT PLT. */
4454 resolved_plt = htab->plt_got;
4455 plt_offset = eh->plt_got.offset;
4458 relocation = (resolved_plt->output_section->vma
4459 + resolved_plt->output_offset
4461 unresolved_reloc = FALSE;
4464 case R_X86_64_SIZE32:
4465 case R_X86_64_SIZE64:
4466 /* Set to symbol size. */
4467 relocation = st_size;
4473 case R_X86_64_PC32_BND:
4474 /* Don't complain about -fPIC if the symbol is undefined when
4475 building executable unless it is unresolved weak symbol or
4476 -z nocopyreloc is used. */
4477 if ((input_section->flags & SEC_ALLOC) != 0
4478 && (input_section->flags & SEC_READONLY) != 0
4480 && ((bfd_link_executable (info)
4481 && ((h->root.type == bfd_link_hash_undefweak
4482 && !resolved_to_zero)
4483 || ((info->nocopyreloc
4484 || (eh->def_protected
4485 && elf_has_no_copy_on_protected (h->root.u.def.section->owner)))
4487 && !(h->root.u.def.section->flags & SEC_CODE))))
4488 || bfd_link_dll (info)))
4490 bfd_boolean fail = FALSE;
4492 = ((r_type == R_X86_64_PC32
4493 || r_type == R_X86_64_PC32_BND)
4494 && is_32bit_relative_branch (contents, rel->r_offset));
4496 if (SYMBOL_REFERENCES_LOCAL (info, h))
4498 /* Symbol is referenced locally. Make sure it is
4499 defined locally or for a branch. */
4500 fail = (!(h->def_regular || ELF_COMMON_DEF_P (h))
4503 else if (!(bfd_link_pie (info)
4504 && (h->needs_copy || eh->needs_copy)))
4506 /* Symbol doesn't need copy reloc and isn't referenced
4507 locally. We only allow branch to symbol with
4508 non-default visibility. */
4510 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
4514 return elf_x86_64_need_pic (info, input_bfd, input_section,
4515 h, NULL, NULL, howto);
4524 /* FIXME: The ABI says the linker should make sure the value is
4525 the same when it's zeroextended to 64 bit. */
4528 if ((input_section->flags & SEC_ALLOC) == 0)
4531 /* Don't copy a pc-relative relocation into the output file
4532 if the symbol needs copy reloc or the symbol is undefined
4533 when building executable. Copy dynamic function pointer
4534 relocations. Don't generate dynamic relocations against
4535 resolved undefined weak symbols in PIE. */
4536 if ((bfd_link_pic (info)
4537 && !(bfd_link_pie (info)
4541 || h->root.type == bfd_link_hash_undefined)
4542 && (IS_X86_64_PCREL_TYPE (r_type)
4543 || r_type == R_X86_64_SIZE32
4544 || r_type == R_X86_64_SIZE64))
4546 || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4547 && !resolved_to_zero)
4548 || h->root.type != bfd_link_hash_undefweak))
4549 && ((! IS_X86_64_PCREL_TYPE (r_type)
4550 && r_type != R_X86_64_SIZE32
4551 && r_type != R_X86_64_SIZE64)
4552 || ! SYMBOL_CALLS_LOCAL (info, h)))
4553 || (ELIMINATE_COPY_RELOCS
4554 && !bfd_link_pic (info)
4558 || eh->func_pointer_refcount > 0
4559 || (h->root.type == bfd_link_hash_undefweak
4560 && !resolved_to_zero))
4561 && ((h->def_dynamic && !h->def_regular)
4562 /* Undefined weak symbol is bound locally when
4564 || h->root.type == bfd_link_hash_undefined)))
4566 Elf_Internal_Rela outrel;
4567 bfd_boolean skip, relocate;
4570 /* When generating a shared object, these relocations
4571 are copied into the output file to be resolved at run
4577 _bfd_elf_section_offset (output_bfd, info, input_section,
4579 if (outrel.r_offset == (bfd_vma) -1)
4581 else if (outrel.r_offset == (bfd_vma) -2)
4582 skip = TRUE, relocate = TRUE;
4584 outrel.r_offset += (input_section->output_section->vma
4585 + input_section->output_offset);
4588 memset (&outrel, 0, sizeof outrel);
4590 /* h->dynindx may be -1 if this symbol was marked to
4594 && (IS_X86_64_PCREL_TYPE (r_type)
4595 || !(bfd_link_executable (info)
4596 || SYMBOLIC_BIND (info, h))
4597 || ! h->def_regular))
4599 outrel.r_info = htab->r_info (h->dynindx, r_type);
4600 outrel.r_addend = rel->r_addend;
4604 /* This symbol is local, or marked to become local.
4605 When relocation overflow check is disabled, we
4606 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
4607 if (r_type == htab->pointer_r_type
4608 || (r_type == R_X86_64_32
4609 && info->no_reloc_overflow_check))
4612 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4613 outrel.r_addend = relocation + rel->r_addend;
4615 else if (r_type == R_X86_64_64
4616 && !ABI_64_P (output_bfd))
4619 outrel.r_info = htab->r_info (0,
4620 R_X86_64_RELATIVE64);
4621 outrel.r_addend = relocation + rel->r_addend;
4622 /* Check addend overflow. */
4623 if ((outrel.r_addend & 0x80000000)
4624 != (rel->r_addend & 0x80000000))
4627 int addend = rel->r_addend;
4628 if (h && h->root.root.string)
4629 name = h->root.root.string;
4631 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4634 /* xgettext:c-format */
4635 (_("%B: addend %s%#x in relocation %s against "
4636 "symbol `%s' at %#Lx in section `%A' is "
4638 input_bfd, addend < 0 ? "-" : "", addend,
4639 howto->name, name, rel->r_offset, input_section);
4640 bfd_set_error (bfd_error_bad_value);
4648 if (bfd_is_abs_section (sec))
4650 else if (sec == NULL || sec->owner == NULL)
4652 bfd_set_error (bfd_error_bad_value);
4659 /* We are turning this relocation into one
4660 against a section symbol. It would be
4661 proper to subtract the symbol's value,
4662 osec->vma, from the emitted reloc addend,
4663 but ld.so expects buggy relocs. */
4664 osec = sec->output_section;
4665 sindx = elf_section_data (osec)->dynindx;
4668 asection *oi = htab->elf.text_index_section;
4669 sindx = elf_section_data (oi)->dynindx;
4671 BFD_ASSERT (sindx != 0);
4674 outrel.r_info = htab->r_info (sindx, r_type);
4675 outrel.r_addend = relocation + rel->r_addend;
4679 sreloc = elf_section_data (input_section)->sreloc;
4681 if (sreloc == NULL || sreloc->contents == NULL)
4683 r = bfd_reloc_notsupported;
4684 goto check_relocation_error;
4687 elf_append_rela (output_bfd, sreloc, &outrel);
4689 /* If this reloc is against an external symbol, we do
4690 not want to fiddle with the addend. Otherwise, we
4691 need to include the symbol value so that it becomes
4692 an addend for the dynamic reloc. */
4699 case R_X86_64_TLSGD:
4700 case R_X86_64_GOTPC32_TLSDESC:
4701 case R_X86_64_TLSDESC_CALL:
4702 case R_X86_64_GOTTPOFF:
4703 tls_type = GOT_UNKNOWN;
4704 if (h == NULL && local_got_offsets)
4705 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
4707 tls_type = elf_x86_hash_entry (h)->tls_type;
4709 if (! elf_x86_64_tls_transition (info, input_bfd,
4710 input_section, contents,
4711 symtab_hdr, sym_hashes,
4712 &r_type, tls_type, rel,
4713 relend, h, r_symndx, TRUE))
4716 if (r_type == R_X86_64_TPOFF32)
4718 bfd_vma roff = rel->r_offset;
4720 BFD_ASSERT (! unresolved_reloc);
4722 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
4724 /* GD->LE transition. For 64bit, change
4725 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4726 .word 0x6666; rex64; call __tls_get_addr@PLT
4728 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4730 call *__tls_get_addr@GOTPCREL(%rip)
4731 which may be converted to
4732 addr32 call __tls_get_addr
4735 leaq foo@tpoff(%rax), %rax
4737 leaq foo@tlsgd(%rip), %rdi
4738 .word 0x6666; rex64; call __tls_get_addr@PLT
4740 leaq foo@tlsgd(%rip), %rdi
4742 call *__tls_get_addr@GOTPCREL(%rip)
4743 which may be converted to
4744 addr32 call __tls_get_addr
4747 leaq foo@tpoff(%rax), %rax
4748 For largepic, change:
4749 leaq foo@tlsgd(%rip), %rdi
4750 movabsq $__tls_get_addr@pltoff, %rax
4755 leaq foo@tpoff(%rax), %rax
4756 nopw 0x0(%rax,%rax,1) */
4758 if (ABI_64_P (output_bfd))
4760 if (contents[roff + 5] == 0xb8)
4762 memcpy (contents + roff - 3,
4763 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
4764 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
4768 memcpy (contents + roff - 4,
4769 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4773 memcpy (contents + roff - 3,
4774 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4776 bfd_put_32 (output_bfd,
4777 elf_x86_64_tpoff (info, relocation),
4778 contents + roff + 8 + largepic);
4779 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
4780 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
4785 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
4787 /* GDesc -> LE transition.
4788 It's originally something like:
4789 leaq x@tlsdesc(%rip), %rax
4792 movl $x@tpoff, %rax. */
4794 unsigned int val, type;
4796 type = bfd_get_8 (input_bfd, contents + roff - 3);
4797 val = bfd_get_8 (input_bfd, contents + roff - 1);
4798 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
4799 contents + roff - 3);
4800 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
4801 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4802 contents + roff - 1);
4803 bfd_put_32 (output_bfd,
4804 elf_x86_64_tpoff (info, relocation),
4808 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
4810 /* GDesc -> LE transition.
4815 bfd_put_8 (output_bfd, 0x66, contents + roff);
4816 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4819 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
4821 /* IE->LE transition:
4822 For 64bit, originally it can be one of:
4823 movq foo@gottpoff(%rip), %reg
4824 addq foo@gottpoff(%rip), %reg
4827 leaq foo(%reg), %reg
4829 For 32bit, originally it can be one of:
4830 movq foo@gottpoff(%rip), %reg
4831 addl foo@gottpoff(%rip), %reg
4834 leal foo(%reg), %reg
4837 unsigned int val, type, reg;
4840 val = bfd_get_8 (input_bfd, contents + roff - 3);
4843 type = bfd_get_8 (input_bfd, contents + roff - 2);
4844 reg = bfd_get_8 (input_bfd, contents + roff - 1);
4850 bfd_put_8 (output_bfd, 0x49,
4851 contents + roff - 3);
4852 else if (!ABI_64_P (output_bfd) && val == 0x44)
4853 bfd_put_8 (output_bfd, 0x41,
4854 contents + roff - 3);
4855 bfd_put_8 (output_bfd, 0xc7,
4856 contents + roff - 2);
4857 bfd_put_8 (output_bfd, 0xc0 | reg,
4858 contents + roff - 1);
4862 /* addq/addl -> addq/addl - addressing with %rsp/%r12
4865 bfd_put_8 (output_bfd, 0x49,
4866 contents + roff - 3);
4867 else if (!ABI_64_P (output_bfd) && val == 0x44)
4868 bfd_put_8 (output_bfd, 0x41,
4869 contents + roff - 3);
4870 bfd_put_8 (output_bfd, 0x81,
4871 contents + roff - 2);
4872 bfd_put_8 (output_bfd, 0xc0 | reg,
4873 contents + roff - 1);
4877 /* addq/addl -> leaq/leal */
4879 bfd_put_8 (output_bfd, 0x4d,
4880 contents + roff - 3);
4881 else if (!ABI_64_P (output_bfd) && val == 0x44)
4882 bfd_put_8 (output_bfd, 0x45,
4883 contents + roff - 3);
4884 bfd_put_8 (output_bfd, 0x8d,
4885 contents + roff - 2);
4886 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
4887 contents + roff - 1);
4889 bfd_put_32 (output_bfd,
4890 elf_x86_64_tpoff (info, relocation),
4898 if (htab->elf.sgot == NULL)
4903 off = h->got.offset;
4904 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
4908 if (local_got_offsets == NULL)
4911 off = local_got_offsets[r_symndx];
4912 offplt = local_tlsdesc_gotents[r_symndx];
4919 Elf_Internal_Rela outrel;
4923 if (htab->elf.srelgot == NULL)
4926 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4928 if (GOT_TLS_GDESC_P (tls_type))
4930 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
4931 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
4932 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
4933 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4934 + htab->elf.sgotplt->output_offset
4936 + htab->sgotplt_jump_table_size);
4937 sreloc = htab->elf.srelplt;
4939 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
4941 outrel.r_addend = 0;
4942 elf_append_rela (output_bfd, sreloc, &outrel);
4945 sreloc = htab->elf.srelgot;
4947 outrel.r_offset = (htab->elf.sgot->output_section->vma
4948 + htab->elf.sgot->output_offset + off);
4950 if (GOT_TLS_GD_P (tls_type))
4951 dr_type = R_X86_64_DTPMOD64;
4952 else if (GOT_TLS_GDESC_P (tls_type))
4955 dr_type = R_X86_64_TPOFF64;
4957 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
4958 outrel.r_addend = 0;
4959 if ((dr_type == R_X86_64_TPOFF64
4960 || dr_type == R_X86_64_TLSDESC) && indx == 0)
4961 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
4962 outrel.r_info = htab->r_info (indx, dr_type);
4964 elf_append_rela (output_bfd, sreloc, &outrel);
4966 if (GOT_TLS_GD_P (tls_type))
4970 BFD_ASSERT (! unresolved_reloc);
4971 bfd_put_64 (output_bfd,
4972 relocation - _bfd_x86_elf_dtpoff_base (info),
4973 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4977 bfd_put_64 (output_bfd, 0,
4978 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4979 outrel.r_info = htab->r_info (indx,
4981 outrel.r_offset += GOT_ENTRY_SIZE;
4982 elf_append_rela (output_bfd, sreloc,
4991 local_got_offsets[r_symndx] |= 1;
4994 if (off >= (bfd_vma) -2
4995 && ! GOT_TLS_GDESC_P (tls_type))
4997 if (r_type == ELF32_R_TYPE (rel->r_info))
4999 if (r_type == R_X86_64_GOTPC32_TLSDESC
5000 || r_type == R_X86_64_TLSDESC_CALL)
5001 relocation = htab->elf.sgotplt->output_section->vma
5002 + htab->elf.sgotplt->output_offset
5003 + offplt + htab->sgotplt_jump_table_size;
5005 relocation = htab->elf.sgot->output_section->vma
5006 + htab->elf.sgot->output_offset + off;
5007 unresolved_reloc = FALSE;
5011 bfd_vma roff = rel->r_offset;
5013 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
5015 /* GD->IE transition. For 64bit, change
5016 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5017 .word 0x6666; rex64; call __tls_get_addr@PLT
5019 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5021 call *__tls_get_addr@GOTPCREL(%rip
5022 which may be converted to
5023 addr32 call __tls_get_addr
5026 addq foo@gottpoff(%rip), %rax
5028 leaq foo@tlsgd(%rip), %rdi
5029 .word 0x6666; rex64; call __tls_get_addr@PLT
5031 leaq foo@tlsgd(%rip), %rdi
5033 call *__tls_get_addr@GOTPCREL(%rip)
5034 which may be converted to
5035 addr32 call __tls_get_addr
5038 addq foo@gottpoff(%rip), %rax
5039 For largepic, change:
5040 leaq foo@tlsgd(%rip), %rdi
5041 movabsq $__tls_get_addr@pltoff, %rax
5046 addq foo@gottpoff(%rax), %rax
5047 nopw 0x0(%rax,%rax,1) */
5049 if (ABI_64_P (output_bfd))
5051 if (contents[roff + 5] == 0xb8)
5053 memcpy (contents + roff - 3,
5054 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
5055 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
5059 memcpy (contents + roff - 4,
5060 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5064 memcpy (contents + roff - 3,
5065 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5068 relocation = (htab->elf.sgot->output_section->vma
5069 + htab->elf.sgot->output_offset + off
5072 - input_section->output_section->vma
5073 - input_section->output_offset
5075 bfd_put_32 (output_bfd, relocation,
5076 contents + roff + 8 + largepic);
5077 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
5082 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
5084 /* GDesc -> IE transition.
5085 It's originally something like:
5086 leaq x@tlsdesc(%rip), %rax
5089 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
5091 /* Now modify the instruction as appropriate. To
5092 turn a leaq into a movq in the form we use it, it
5093 suffices to change the second byte from 0x8d to
5095 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
5097 bfd_put_32 (output_bfd,
5098 htab->elf.sgot->output_section->vma
5099 + htab->elf.sgot->output_offset + off
5101 - input_section->output_section->vma
5102 - input_section->output_offset
5107 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
5109 /* GDesc -> IE transition.
5116 bfd_put_8 (output_bfd, 0x66, contents + roff);
5117 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5125 case R_X86_64_TLSLD:
5126 if (! elf_x86_64_tls_transition (info, input_bfd,
5127 input_section, contents,
5128 symtab_hdr, sym_hashes,
5129 &r_type, GOT_UNKNOWN, rel,
5130 relend, h, r_symndx, TRUE))
5133 if (r_type != R_X86_64_TLSLD)
5135 /* LD->LE transition:
5136 leaq foo@tlsld(%rip), %rdi
5137 call __tls_get_addr@PLT
5138 For 64bit, we change it into:
5139 .word 0x6666; .byte 0x66; movq %fs:0, %rax
5140 For 32bit, we change it into:
5141 nopl 0x0(%rax); movl %fs:0, %eax
5143 leaq foo@tlsld(%rip), %rdi;
5144 call *__tls_get_addr@GOTPCREL(%rip)
5145 which may be converted to
5146 addr32 call __tls_get_addr
5147 For 64bit, we change it into:
5148 .word 0x6666; .word 0x6666; movq %fs:0, %rax
5149 For 32bit, we change it into:
5150 nopw 0x0(%rax); movl %fs:0, %eax
5151 For largepic, change:
5152 leaq foo@tlsgd(%rip), %rdi
5153 movabsq $__tls_get_addr@pltoff, %rax
5157 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
5160 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
5161 if (ABI_64_P (output_bfd))
5163 if (contents[rel->r_offset + 5] == 0xb8)
5164 memcpy (contents + rel->r_offset - 3,
5165 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
5166 "\x64\x48\x8b\x04\x25\0\0\0", 22);
5167 else if (contents[rel->r_offset + 4] == 0xff
5168 || contents[rel->r_offset + 4] == 0x67)
5169 memcpy (contents + rel->r_offset - 3,
5170 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
5173 memcpy (contents + rel->r_offset - 3,
5174 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
5178 if (contents[rel->r_offset + 4] == 0xff)
5179 memcpy (contents + rel->r_offset - 3,
5180 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
5183 memcpy (contents + rel->r_offset - 3,
5184 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
5186 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
5187 and R_X86_64_PLTOFF64. */
5193 if (htab->elf.sgot == NULL)
5196 off = htab->tls_ld_or_ldm_got.offset;
5201 Elf_Internal_Rela outrel;
5203 if (htab->elf.srelgot == NULL)
5206 outrel.r_offset = (htab->elf.sgot->output_section->vma
5207 + htab->elf.sgot->output_offset + off);
5209 bfd_put_64 (output_bfd, 0,
5210 htab->elf.sgot->contents + off);
5211 bfd_put_64 (output_bfd, 0,
5212 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
5213 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
5214 outrel.r_addend = 0;
5215 elf_append_rela (output_bfd, htab->elf.srelgot,
5217 htab->tls_ld_or_ldm_got.offset |= 1;
5219 relocation = htab->elf.sgot->output_section->vma
5220 + htab->elf.sgot->output_offset + off;
5221 unresolved_reloc = FALSE;
5224 case R_X86_64_DTPOFF32:
5225 if (!bfd_link_executable (info)
5226 || (input_section->flags & SEC_CODE) == 0)
5227 relocation -= _bfd_x86_elf_dtpoff_base (info);
5229 relocation = elf_x86_64_tpoff (info, relocation);
5232 case R_X86_64_TPOFF32:
5233 case R_X86_64_TPOFF64:
5234 BFD_ASSERT (bfd_link_executable (info));
5235 relocation = elf_x86_64_tpoff (info, relocation);
5238 case R_X86_64_DTPOFF64:
5239 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
5240 relocation -= _bfd_x86_elf_dtpoff_base (info);
5247 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5248 because such sections are not SEC_ALLOC and thus ld.so will
5249 not process them. */
5250 if (unresolved_reloc
5251 && !((input_section->flags & SEC_DEBUGGING) != 0
5253 && _bfd_elf_section_offset (output_bfd, info, input_section,
5254 rel->r_offset) != (bfd_vma) -1)
5259 sec = h->root.u.def.section;
5260 if ((info->nocopyreloc
5261 || (eh->def_protected
5262 && elf_has_no_copy_on_protected (h->root.u.def.section->owner)))
5263 && !(h->root.u.def.section->flags & SEC_CODE))
5264 return elf_x86_64_need_pic (info, input_bfd, input_section,
5265 h, NULL, NULL, howto);
5270 /* xgettext:c-format */
5271 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
5276 h->root.root.string);
5282 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5283 contents, rel->r_offset,
5284 relocation, rel->r_addend);
5286 check_relocation_error:
5287 if (r != bfd_reloc_ok)
5292 name = h->root.root.string;
5295 name = bfd_elf_string_from_elf_section (input_bfd,
5296 symtab_hdr->sh_link,
5301 name = bfd_section_name (input_bfd, sec);
5304 if (r == bfd_reloc_overflow)
5305 (*info->callbacks->reloc_overflow)
5306 (info, (h ? &h->root : NULL), name, howto->name,
5307 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5311 /* xgettext:c-format */
5312 (_("%B(%A+%#Lx): reloc against `%s': error %d"),
5313 input_bfd, input_section,
5314 rel->r_offset, name, (int) r);
5325 Elf_Internal_Shdr *rel_hdr;
5326 size_t deleted = rel - wrel;
5328 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
5329 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5330 if (rel_hdr->sh_size == 0)
5332 /* It is too late to remove an empty reloc section. Leave
5334 ??? What is wrong with an empty section??? */
5335 rel_hdr->sh_size = rel_hdr->sh_entsize;
5338 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5339 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5340 input_section->reloc_count -= deleted;
5346 /* Finish up dynamic symbol handling. We set the contents of various
5347 dynamic sections here. */
5350 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
5351 struct bfd_link_info *info,
5352 struct elf_link_hash_entry *h,
5353 Elf_Internal_Sym *sym)
5355 struct elf_x86_link_hash_table *htab;
5356 bfd_boolean use_plt_second;
5357 struct elf_x86_link_hash_entry *eh;
5358 bfd_boolean local_undefweak;
5359 const struct elf_x86_64_plt_layout *plt_layout;
5360 const struct elf_x86_64_lazy_plt_layout *lazy_plt_layout;
5361 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt_layout;
5363 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
5367 /* Use the second PLT section only if there is .plt section. */
5368 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
5370 eh = (struct elf_x86_link_hash_entry *) h;
5371 if (eh->no_finish_dynamic_symbol)
5374 plt_layout = &elf_x86_64_plt (htab);
5375 lazy_plt_layout = elf_x86_64_lazy_plt (htab);
5376 non_lazy_plt_layout = elf_x86_64_non_lazy_plt (htab);
5378 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5379 resolved undefined weak symbols in executable so that their
5380 references have value 0 at run-time. */
5381 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
5386 if (h->plt.offset != (bfd_vma) -1)
5389 bfd_vma got_offset, plt_offset;
5390 Elf_Internal_Rela rela;
5392 asection *plt, *gotplt, *relplt, *resolved_plt;
5393 const struct elf_backend_data *bed;
5394 bfd_vma plt_got_pcrel_offset;
5396 /* When building a static executable, use .iplt, .igot.plt and
5397 .rela.iplt sections for STT_GNU_IFUNC symbols. */
5398 if (htab->elf.splt != NULL)
5400 plt = htab->elf.splt;
5401 gotplt = htab->elf.sgotplt;
5402 relplt = htab->elf.srelplt;
5406 plt = htab->elf.iplt;
5407 gotplt = htab->elf.igotplt;
5408 relplt = htab->elf.irelplt;
5411 /* This symbol has an entry in the procedure linkage table. Set
5413 if ((h->dynindx == -1
5415 && !((h->forced_local || bfd_link_executable (info))
5417 && h->type == STT_GNU_IFUNC))
5423 /* Get the index in the procedure linkage table which
5424 corresponds to this symbol. This is the index of this symbol
5425 in all the symbols for which we are making plt entries. The
5426 first entry in the procedure linkage table is reserved.
5428 Get the offset into the .got table of the entry that
5429 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
5430 bytes. The first three are reserved for the dynamic linker.
5432 For static executables, we don't reserve anything. */
5434 if (plt == htab->elf.splt)
5436 got_offset = (h->plt.offset / plt_layout->plt_entry_size
5437 - plt_layout->has_plt0);
5438 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
5442 got_offset = h->plt.offset / plt_layout->plt_entry_size;
5443 got_offset = got_offset * GOT_ENTRY_SIZE;
5446 /* Fill in the entry in the procedure linkage table. */
5447 memcpy (plt->contents + h->plt.offset, plt_layout->plt_entry,
5448 plt_layout->plt_entry_size);
5451 memcpy (htab->plt_second->contents + eh->plt_second.offset,
5452 non_lazy_plt_layout->plt_entry,
5453 non_lazy_plt_layout->plt_entry_size);
5455 resolved_plt = htab->plt_second;
5456 plt_offset = eh->plt_second.offset;
5461 plt_offset = h->plt.offset;
5464 /* Insert the relocation positions of the plt section. */
5466 /* Put offset the PC-relative instruction referring to the GOT entry,
5467 subtracting the size of that instruction. */
5468 plt_got_pcrel_offset = (gotplt->output_section->vma
5469 + gotplt->output_offset
5471 - resolved_plt->output_section->vma
5472 - resolved_plt->output_offset
5474 - plt_layout->plt_got_insn_size);
5476 /* Check PC-relative offset overflow in PLT entry. */
5477 if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
5478 /* xgettext:c-format */
5479 info->callbacks->einfo (_("%F%B: PC-relative offset overflow in PLT entry for `%s'\n"),
5480 output_bfd, h->root.root.string);
5482 bfd_put_32 (output_bfd, plt_got_pcrel_offset,
5483 (resolved_plt->contents + plt_offset
5484 + plt_layout->plt_got_offset));
5486 /* Fill in the entry in the global offset table, initially this
5487 points to the second part of the PLT entry. Leave the entry
5488 as zero for undefined weak symbol in PIE. No PLT relocation
5489 against undefined weak symbol in PIE. */
5490 if (!local_undefweak)
5492 if (plt_layout->has_plt0)
5493 bfd_put_64 (output_bfd, (plt->output_section->vma
5494 + plt->output_offset
5496 + lazy_plt_layout->plt_lazy_offset),
5497 gotplt->contents + got_offset);
5499 /* Fill in the entry in the .rela.plt section. */
5500 rela.r_offset = (gotplt->output_section->vma
5501 + gotplt->output_offset
5503 if (h->dynindx == -1
5504 || ((bfd_link_executable (info)
5505 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5507 && h->type == STT_GNU_IFUNC))
5509 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
5510 h->root.root.string,
5511 h->root.u.def.section->owner);
5513 /* If an STT_GNU_IFUNC symbol is locally defined, generate
5514 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
5515 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
5516 rela.r_addend = (h->root.u.def.value
5517 + h->root.u.def.section->output_section->vma
5518 + h->root.u.def.section->output_offset);
5519 /* R_X86_64_IRELATIVE comes last. */
5520 plt_index = htab->next_irelative_index--;
5524 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
5526 plt_index = htab->next_jump_slot_index++;
5529 /* Don't fill the second and third slots in PLT entry for
5530 static executables nor without PLT0. */
5531 if (plt == htab->elf.splt && plt_layout->has_plt0)
5534 = h->plt.offset + lazy_plt_layout->plt_plt_insn_end;
5536 /* Put relocation index. */
5537 bfd_put_32 (output_bfd, plt_index,
5538 (plt->contents + h->plt.offset
5539 + lazy_plt_layout->plt_reloc_offset));
5541 /* Put offset for jmp .PLT0 and check for overflow. We don't
5542 check relocation index for overflow since branch displacement
5543 will overflow first. */
5544 if (plt0_offset > 0x80000000)
5545 /* xgettext:c-format */
5546 info->callbacks->einfo (_("%F%B: branch displacement overflow in PLT entry for `%s'\n"),
5547 output_bfd, h->root.root.string);
5548 bfd_put_32 (output_bfd, - plt0_offset,
5549 (plt->contents + h->plt.offset
5550 + lazy_plt_layout->plt_plt_offset));
5553 bed = get_elf_backend_data (output_bfd);
5554 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
5555 bed->s->swap_reloca_out (output_bfd, &rela, loc);
5558 else if (eh->plt_got.offset != (bfd_vma) -1)
5560 bfd_vma got_offset, plt_offset;
5561 asection *plt, *got;
5562 bfd_boolean got_after_plt;
5563 int32_t got_pcrel_offset;
5565 /* Set the entry in the GOT procedure linkage table. */
5566 plt = htab->plt_got;
5567 got = htab->elf.sgot;
5568 got_offset = h->got.offset;
5570 if (got_offset == (bfd_vma) -1
5571 || (h->type == STT_GNU_IFUNC && h->def_regular)
5576 /* Use the non-lazy PLT entry template for the GOT PLT since they
5577 are the identical. */
5578 /* Fill in the entry in the GOT procedure linkage table. */
5579 plt_offset = eh->plt_got.offset;
5580 memcpy (plt->contents + plt_offset,
5581 non_lazy_plt_layout->plt_entry,
5582 non_lazy_plt_layout->plt_entry_size);
5584 /* Put offset the PC-relative instruction referring to the GOT
5585 entry, subtracting the size of that instruction. */
5586 got_pcrel_offset = (got->output_section->vma
5587 + got->output_offset
5589 - plt->output_section->vma
5590 - plt->output_offset
5592 - non_lazy_plt_layout->plt_got_insn_size);
5594 /* Check PC-relative offset overflow in GOT PLT entry. */
5595 got_after_plt = got->output_section->vma > plt->output_section->vma;
5596 if ((got_after_plt && got_pcrel_offset < 0)
5597 || (!got_after_plt && got_pcrel_offset > 0))
5598 /* xgettext:c-format */
5599 info->callbacks->einfo (_("%F%B: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
5600 output_bfd, h->root.root.string);
5602 bfd_put_32 (output_bfd, got_pcrel_offset,
5603 (plt->contents + plt_offset
5604 + non_lazy_plt_layout->plt_got_offset));
5607 if (!local_undefweak
5609 && (h->plt.offset != (bfd_vma) -1
5610 || eh->plt_got.offset != (bfd_vma) -1))
5612 /* Mark the symbol as undefined, rather than as defined in
5613 the .plt section. Leave the value if there were any
5614 relocations where pointer equality matters (this is a clue
5615 for the dynamic linker, to make function pointer
5616 comparisons work between an application and shared
5617 library), otherwise set it to zero. If a function is only
5618 called from a binary, there is no need to slow down
5619 shared libraries because of that. */
5620 sym->st_shndx = SHN_UNDEF;
5621 if (!h->pointer_equality_needed)
5625 /* Don't generate dynamic GOT relocation against undefined weak
5626 symbol in executable. */
5627 if (h->got.offset != (bfd_vma) -1
5628 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
5629 && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
5630 && !local_undefweak)
5632 Elf_Internal_Rela rela;
5633 asection *relgot = htab->elf.srelgot;
5635 /* This symbol has an entry in the global offset table. Set it
5637 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
5640 rela.r_offset = (htab->elf.sgot->output_section->vma
5641 + htab->elf.sgot->output_offset
5642 + (h->got.offset &~ (bfd_vma) 1));
5644 /* If this is a static link, or it is a -Bsymbolic link and the
5645 symbol is defined locally or was forced to be local because
5646 of a version file, we just want to emit a RELATIVE reloc.
5647 The entry in the global offset table will already have been
5648 initialized in the relocate_section function. */
5650 && h->type == STT_GNU_IFUNC)
5652 if (h->plt.offset == (bfd_vma) -1)
5654 /* STT_GNU_IFUNC is referenced without PLT. */
5655 if (htab->elf.splt == NULL)
5657 /* use .rel[a].iplt section to store .got relocations
5658 in static executable. */
5659 relgot = htab->elf.irelplt;
5661 if (SYMBOL_REFERENCES_LOCAL (info, h))
5663 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
5665 h->root.root.string,
5666 h->root.u.def.section->owner);
5668 rela.r_info = htab->r_info (0,
5669 R_X86_64_IRELATIVE);
5670 rela.r_addend = (h->root.u.def.value
5671 + h->root.u.def.section->output_section->vma
5672 + h->root.u.def.section->output_offset);
5677 else if (bfd_link_pic (info))
5679 /* Generate R_X86_64_GLOB_DAT. */
5687 if (!h->pointer_equality_needed)
5690 /* For non-shared object, we can't use .got.plt, which
5691 contains the real function addres if we need pointer
5692 equality. We load the GOT entry with the PLT entry. */
5693 if (htab->plt_second != NULL)
5695 plt = htab->plt_second;
5696 plt_offset = eh->plt_second.offset;
5700 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
5701 plt_offset = h->plt.offset;
5703 bfd_put_64 (output_bfd, (plt->output_section->vma
5704 + plt->output_offset
5706 htab->elf.sgot->contents + h->got.offset);
5710 else if (bfd_link_pic (info)
5711 && SYMBOL_REFERENCES_LOCAL (info, h))
5713 if (!h->def_regular)
5715 BFD_ASSERT((h->got.offset & 1) != 0);
5716 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
5717 rela.r_addend = (h->root.u.def.value
5718 + h->root.u.def.section->output_section->vma
5719 + h->root.u.def.section->output_offset);
5723 BFD_ASSERT((h->got.offset & 1) == 0);
5725 bfd_put_64 (output_bfd, (bfd_vma) 0,
5726 htab->elf.sgot->contents + h->got.offset);
5727 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
5731 elf_append_rela (output_bfd, relgot, &rela);
5736 Elf_Internal_Rela rela;
5739 /* This symbol needs a copy reloc. Set it up. */
5741 if (h->dynindx == -1
5742 || (h->root.type != bfd_link_hash_defined
5743 && h->root.type != bfd_link_hash_defweak)
5744 || htab->elf.srelbss == NULL
5745 || htab->elf.sreldynrelro == NULL)
5748 rela.r_offset = (h->root.u.def.value
5749 + h->root.u.def.section->output_section->vma
5750 + h->root.u.def.section->output_offset);
5751 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
5753 if (h->root.u.def.section == htab->elf.sdynrelro)
5754 s = htab->elf.sreldynrelro;
5756 s = htab->elf.srelbss;
5757 elf_append_rela (output_bfd, s, &rela);
5763 /* Finish up local dynamic symbol handling. We set the contents of
5764 various dynamic sections here. */
5767 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
5769 struct elf_link_hash_entry *h
5770 = (struct elf_link_hash_entry *) *slot;
5771 struct bfd_link_info *info
5772 = (struct bfd_link_info *) inf;
5774 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5778 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
5779 here since undefined weak symbol may not be dynamic and may not be
5780 called for elf_x86_64_finish_dynamic_symbol. */
5783 elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
5786 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
5787 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5789 if (h->root.type != bfd_link_hash_undefweak
5790 || h->dynindx != -1)
5793 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5797 /* Used to decide how to sort relocs in an optimal manner for the
5798 dynamic linker, before writing them out. */
5800 static enum elf_reloc_type_class
5801 elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
5802 const asection *rel_sec ATTRIBUTE_UNUSED,
5803 const Elf_Internal_Rela *rela)
5805 bfd *abfd = info->output_bfd;
5806 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5807 struct elf_x86_link_hash_table *htab
5808 = elf_x86_hash_table (info, X86_64_ELF_DATA);
5810 if (htab->elf.dynsym != NULL
5811 && htab->elf.dynsym->contents != NULL)
5813 /* Check relocation against STT_GNU_IFUNC symbol if there are
5815 unsigned long r_symndx = htab->r_sym (rela->r_info);
5816 if (r_symndx != STN_UNDEF)
5818 Elf_Internal_Sym sym;
5819 if (!bed->s->swap_symbol_in (abfd,
5820 (htab->elf.dynsym->contents
5821 + r_symndx * bed->s->sizeof_sym),
5825 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
5826 return reloc_class_ifunc;
5830 switch ((int) ELF32_R_TYPE (rela->r_info))
5832 case R_X86_64_IRELATIVE:
5833 return reloc_class_ifunc;
5834 case R_X86_64_RELATIVE:
5835 case R_X86_64_RELATIVE64:
5836 return reloc_class_relative;
5837 case R_X86_64_JUMP_SLOT:
5838 return reloc_class_plt;
5840 return reloc_class_copy;
5842 return reloc_class_normal;
5846 /* Finish up the dynamic sections. */
5849 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
5850 struct bfd_link_info *info)
5852 struct elf_x86_link_hash_table *htab;
5856 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
5860 dynobj = htab->elf.dynobj;
5861 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5863 if (htab->elf.dynamic_sections_created)
5865 bfd_byte *dyncon, *dynconend;
5866 const struct elf_backend_data *bed;
5867 bfd_size_type sizeof_dyn;
5868 const struct elf_x86_64_plt_layout *plt_layout;
5869 const struct elf_x86_64_lazy_plt_layout *lazy_plt_layout;
5870 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt_layout;
5872 if (sdyn == NULL || htab->elf.sgot == NULL)
5875 bed = get_elf_backend_data (dynobj);
5876 sizeof_dyn = bed->s->sizeof_dyn;
5877 dyncon = sdyn->contents;
5878 dynconend = sdyn->contents + sdyn->size;
5879 for (; dyncon < dynconend; dyncon += sizeof_dyn)
5881 Elf_Internal_Dyn dyn;
5884 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
5892 s = htab->elf.sgotplt;
5893 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5897 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
5901 s = htab->elf.srelplt->output_section;
5902 dyn.d_un.d_val = s->size;
5905 case DT_TLSDESC_PLT:
5907 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
5908 + htab->tlsdesc_plt;
5911 case DT_TLSDESC_GOT:
5913 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
5914 + htab->tlsdesc_got;
5918 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
5921 plt_layout = &elf_x86_64_plt (htab);
5922 lazy_plt_layout = elf_x86_64_lazy_plt (htab);
5923 non_lazy_plt_layout = elf_x86_64_non_lazy_plt (htab);
5925 if (htab->elf.splt && htab->elf.splt->size > 0)
5927 elf_section_data (htab->elf.splt->output_section)
5928 ->this_hdr.sh_entsize = plt_layout->plt_entry_size;
5930 if (plt_layout->has_plt0)
5932 /* Fill in the special first entry in the procedure linkage
5934 memcpy (htab->elf.splt->contents,
5935 lazy_plt_layout->plt0_entry,
5936 lazy_plt_layout->plt_entry_size);
5937 /* Add offset for pushq GOT+8(%rip), since the instruction
5938 uses 6 bytes subtract this value. */
5939 bfd_put_32 (output_bfd,
5940 (htab->elf.sgotplt->output_section->vma
5941 + htab->elf.sgotplt->output_offset
5943 - htab->elf.splt->output_section->vma
5944 - htab->elf.splt->output_offset
5946 (htab->elf.splt->contents
5947 + lazy_plt_layout->plt0_got1_offset));
5948 /* Add offset for the PC-relative instruction accessing
5949 GOT+16, subtracting the offset to the end of that
5951 bfd_put_32 (output_bfd,
5952 (htab->elf.sgotplt->output_section->vma
5953 + htab->elf.sgotplt->output_offset
5955 - htab->elf.splt->output_section->vma
5956 - htab->elf.splt->output_offset
5957 - lazy_plt_layout->plt0_got2_insn_end),
5958 (htab->elf.splt->contents
5959 + lazy_plt_layout->plt0_got2_offset));
5961 if (htab->tlsdesc_plt)
5963 bfd_put_64 (output_bfd, (bfd_vma) 0,
5964 htab->elf.sgot->contents + htab->tlsdesc_got);
5966 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
5967 lazy_plt_layout->plt0_entry,
5968 lazy_plt_layout->plt_entry_size);
5970 /* Add offset for pushq GOT+8(%rip), since the
5971 instruction uses 6 bytes subtract this value. */
5972 bfd_put_32 (output_bfd,
5973 (htab->elf.sgotplt->output_section->vma
5974 + htab->elf.sgotplt->output_offset
5976 - htab->elf.splt->output_section->vma
5977 - htab->elf.splt->output_offset
5980 (htab->elf.splt->contents
5982 + lazy_plt_layout->plt0_got1_offset));
5983 /* Add offset for the PC-relative instruction accessing
5984 GOT+TDG, where TDG stands for htab->tlsdesc_got,
5985 subtracting the offset to the end of that
5987 bfd_put_32 (output_bfd,
5988 (htab->elf.sgot->output_section->vma
5989 + htab->elf.sgot->output_offset
5991 - htab->elf.splt->output_section->vma
5992 - htab->elf.splt->output_offset
5994 - lazy_plt_layout->plt0_got2_insn_end),
5995 (htab->elf.splt->contents
5997 + lazy_plt_layout->plt0_got2_offset));
6002 if (htab->plt_got != NULL && htab->plt_got->size > 0)
6003 elf_section_data (htab->plt_got->output_section)
6004 ->this_hdr.sh_entsize = non_lazy_plt_layout->plt_entry_size;
6006 if (htab->plt_second != NULL && htab->plt_second->size > 0)
6007 elf_section_data (htab->plt_second->output_section)
6008 ->this_hdr.sh_entsize = non_lazy_plt_layout->plt_entry_size;
6011 /* GOT is always created in setup_gnu_properties. But it may not be
6013 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
6015 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
6018 (_("discarded output section: `%A'"), htab->elf.sgotplt);
6022 /* Set the first entry in the global offset table to the address of
6023 the dynamic section. */
6025 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
6027 bfd_put_64 (output_bfd,
6028 sdyn->output_section->vma + sdyn->output_offset,
6029 htab->elf.sgotplt->contents);
6030 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
6031 bfd_put_64 (output_bfd, (bfd_vma) 0,
6032 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
6033 bfd_put_64 (output_bfd, (bfd_vma) 0,
6034 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
6036 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
6040 /* Adjust .eh_frame for .plt section. */
6041 if (htab->plt_eh_frame != NULL
6042 && htab->plt_eh_frame->contents != NULL)
6044 if (htab->elf.splt != NULL
6045 && htab->elf.splt->size != 0
6046 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
6047 && htab->elf.splt->output_section != NULL
6048 && htab->plt_eh_frame->output_section != NULL)
6050 bfd_vma plt_start = htab->elf.splt->output_section->vma;
6051 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
6052 + htab->plt_eh_frame->output_offset
6053 + PLT_FDE_START_OFFSET;
6054 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6055 htab->plt_eh_frame->contents
6056 + PLT_FDE_START_OFFSET);
6058 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
6060 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6062 htab->plt_eh_frame->contents))
6067 /* Adjust .eh_frame for .plt.got section. */
6068 if (htab->plt_got_eh_frame != NULL
6069 && htab->plt_got_eh_frame->contents != NULL)
6071 if (htab->plt_got != NULL
6072 && htab->plt_got->size != 0
6073 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
6074 && htab->plt_got->output_section != NULL
6075 && htab->plt_got_eh_frame->output_section != NULL)
6077 bfd_vma plt_start = htab->plt_got->output_section->vma;
6078 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
6079 + htab->plt_got_eh_frame->output_offset
6080 + PLT_FDE_START_OFFSET;
6081 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6082 htab->plt_got_eh_frame->contents
6083 + PLT_FDE_START_OFFSET);
6085 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
6087 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6088 htab->plt_got_eh_frame,
6089 htab->plt_got_eh_frame->contents))
6094 /* Adjust .eh_frame for the second PLT section. */
6095 if (htab->plt_second_eh_frame != NULL
6096 && htab->plt_second_eh_frame->contents != NULL)
6098 if (htab->plt_second != NULL
6099 && htab->plt_second->size != 0
6100 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
6101 && htab->plt_second->output_section != NULL
6102 && htab->plt_second_eh_frame->output_section != NULL)
6104 bfd_vma plt_start = htab->plt_second->output_section->vma;
6105 bfd_vma eh_frame_start
6106 = (htab->plt_second_eh_frame->output_section->vma
6107 + htab->plt_second_eh_frame->output_offset
6108 + PLT_FDE_START_OFFSET);
6109 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6110 htab->plt_second_eh_frame->contents
6111 + PLT_FDE_START_OFFSET);
6113 if (htab->plt_second_eh_frame->sec_info_type
6114 == SEC_INFO_TYPE_EH_FRAME)
6116 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6117 htab->plt_second_eh_frame,
6118 htab->plt_second_eh_frame->contents))
6123 if (htab->elf.sgot && htab->elf.sgot->size > 0)
6124 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
6127 /* Fill PLT entries for undefined weak symbols in PIE. */
6128 if (bfd_link_pie (info))
6129 bfd_hash_traverse (&info->hash->table,
6130 elf_x86_64_pie_finish_undefweak_symbol,
6136 /* Fill PLT/GOT entries and allocate dynamic relocations for local
6137 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
6138 It has to be done before elf_link_sort_relocs is called so that
6139 dynamic relocations are properly sorted. */
6142 elf_x86_64_output_arch_local_syms
6143 (bfd *output_bfd ATTRIBUTE_UNUSED,
6144 struct bfd_link_info *info,
6145 void *flaginfo ATTRIBUTE_UNUSED,
6146 int (*func) (void *, const char *,
6149 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
6151 struct elf_x86_link_hash_table *htab
6152 = elf_x86_hash_table (info, X86_64_ELF_DATA);
6156 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
6157 htab_traverse (htab->loc_hash_table,
6158 elf_x86_64_finish_local_dynamic_symbol,
6164 enum elf_x86_64_plt_type
6168 plt_second = 1 << 1,
6172 struct elf_x86_64_plt
6177 enum elf_x86_64_plt_type type;
6178 unsigned int plt_got_offset;
6179 unsigned int plt_got_insn_size;
6180 unsigned int plt_entry_size;
6184 /* Forward declaration. */
6185 static const struct elf_x86_64_lazy_plt_layout elf_x86_64_nacl_plt;
6187 /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
6188 dynamic relocations. */
6191 elf_x86_64_get_synthetic_symtab (bfd *abfd,
6192 long symcount ATTRIBUTE_UNUSED,
6193 asymbol **syms ATTRIBUTE_UNUSED,
6198 long size, count, i, n, len;
6200 unsigned int plt_got_offset, plt_entry_size, plt_got_insn_size;
6202 bfd_byte *plt_contents;
6203 long dynrelcount, relsize;
6204 arelent **dynrelbuf, *p;
6205 const struct elf_x86_64_lazy_plt_layout *lazy_plt;
6206 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt;
6207 const struct elf_x86_64_lazy_plt_layout *lazy_bnd_plt;
6208 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_bnd_plt;
6209 const struct elf_x86_64_lazy_plt_layout *lazy_ibt_plt;
6210 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_ibt_plt;
6213 enum elf_x86_64_plt_type plt_type;
6214 struct elf_x86_64_plt plts[] =
6216 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
6217 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
6218 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
6219 { ".plt.bnd", NULL, NULL, plt_second, 0, 0, 0, 0 },
6220 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
6225 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
6228 if (dynsymcount <= 0)
6231 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
6235 if (get_elf_x86_64_backend_data (abfd)->os == is_normal)
6237 lazy_plt = &elf_x86_64_lazy_plt;
6238 non_lazy_plt = &elf_x86_64_non_lazy_plt;
6239 lazy_bnd_plt = &elf_x86_64_lazy_bnd_plt;
6240 non_lazy_bnd_plt = &elf_x86_64_non_lazy_bnd_plt;
6241 if (ABI_64_P (abfd))
6243 lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
6244 non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
6248 lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
6249 non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
6254 lazy_plt = &elf_x86_64_nacl_plt;
6255 non_lazy_plt = NULL;
6256 lazy_bnd_plt = NULL;
6257 non_lazy_bnd_plt = NULL;
6258 lazy_ibt_plt = NULL;
6259 non_lazy_ibt_plt = NULL;
6263 for (j = 0; plts[j].name != NULL; j++)
6265 plt = bfd_get_section_by_name (abfd, plts[j].name);
6266 if (plt == NULL || plt->size == 0)
6269 /* Get the PLT section contents. */
6270 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
6271 if (plt_contents == NULL)
6273 if (!bfd_get_section_contents (abfd, (asection *) plt,
6274 plt_contents, 0, plt->size))
6276 free (plt_contents);
6280 /* Check what kind of PLT it is. */
6281 plt_type = plt_unknown;
6282 if (plts[j].type == plt_unknown
6283 && (plt->size >= (lazy_plt->plt_entry_size
6284 + lazy_plt->plt_entry_size)))
6286 /* Match lazy PLT first. Need to check the first two
6288 if ((memcmp (plt_contents, lazy_plt->plt0_entry,
6289 lazy_plt->plt0_got1_offset) == 0)
6290 && (memcmp (plt_contents + 6, lazy_plt->plt0_entry + 6,
6292 plt_type = plt_lazy;
6293 else if (lazy_bnd_plt != NULL
6294 && (memcmp (plt_contents, lazy_bnd_plt->plt0_entry,
6295 lazy_bnd_plt->plt0_got1_offset) == 0)
6296 && (memcmp (plt_contents + 6,
6297 lazy_bnd_plt->plt0_entry + 6, 3) == 0))
6299 plt_type = plt_lazy | plt_second;
6300 /* The fist entry in the lazy IBT PLT is the same as the
6302 if ((memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
6303 lazy_ibt_plt->plt_entry,
6304 lazy_ibt_plt->plt_got_offset) == 0))
6305 lazy_plt = lazy_ibt_plt;
6307 lazy_plt = lazy_bnd_plt;
6311 if (non_lazy_plt != NULL
6312 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
6313 && plt->size >= non_lazy_plt->plt_entry_size)
6315 /* Match non-lazy PLT. */
6316 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
6317 non_lazy_plt->plt_got_offset) == 0)
6318 plt_type = plt_non_lazy;
6321 if (plt_type == plt_unknown || plt_type == plt_second)
6323 if (non_lazy_bnd_plt != NULL
6324 && plt->size >= non_lazy_bnd_plt->plt_entry_size
6325 && (memcmp (plt_contents, non_lazy_bnd_plt->plt_entry,
6326 non_lazy_bnd_plt->plt_got_offset) == 0))
6328 /* Match BND PLT. */
6329 plt_type = plt_second;
6330 non_lazy_plt = non_lazy_bnd_plt;
6332 else if (non_lazy_ibt_plt != NULL
6333 && plt->size >= non_lazy_ibt_plt->plt_entry_size
6334 && (memcmp (plt_contents,
6335 non_lazy_ibt_plt->plt_entry,
6336 non_lazy_ibt_plt->plt_got_offset) == 0))
6338 /* Match IBT PLT. */
6339 plt_type = plt_second;
6340 non_lazy_plt = non_lazy_ibt_plt;
6344 if (plt_type == plt_unknown)
6346 free (plt_contents);
6351 plts[j].type = plt_type;
6353 if ((plt_type & plt_lazy))
6355 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
6356 plts[j].plt_got_insn_size = lazy_plt->plt_got_insn_size;
6357 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
6358 /* Skip PLT0 in lazy PLT. */
6363 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
6364 plts[j].plt_got_insn_size = non_lazy_plt->plt_got_insn_size;
6365 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
6369 /* Skip lazy PLT when the second PLT is used. */
6370 if (plt_type == (plt_lazy | plt_second))
6374 n = plt->size / plts[j].plt_entry_size;
6379 plts[j].contents = plt_contents;
6385 dynrelbuf = (arelent **) bfd_malloc (relsize);
6386 if (dynrelbuf == NULL)
6389 dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
6392 /* Sort the relocs by address. */
6393 qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
6394 _bfd_x86_elf_compare_relocs);
6396 size = count * sizeof (asymbol);
6398 /* Allocate space for @plt suffixes. */
6400 for (i = 0; i < dynrelcount; i++)
6403 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
6405 size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
6408 s = *ret = (asymbol *) bfd_zmalloc (size);
6412 /* Check for each PLT section. */
6413 names = (char *) (s + count);
6416 for (j = 0; plts[j].name != NULL; j++)
6417 if ((plt_contents = plts[j].contents) != NULL)
6422 plt_got_offset = plts[j].plt_got_offset;
6423 plt_got_insn_size = plts[j].plt_got_insn_size;
6424 plt_entry_size = plts[j].plt_entry_size;
6428 if ((plts[j].type & plt_lazy))
6430 /* Skip PLT0 in lazy PLT. */
6432 offset = plt_entry_size;
6440 /* Check each PLT entry against dynamic relocations. */
6441 for (; k < plts[j].count; k++)
6447 /* Get the PC-relative offset, a signed 32-bit integer. */
6448 off = H_GET_32 (abfd, (plt_contents + offset
6450 got_vma = plt->vma + offset + off + plt_got_insn_size;
6452 /* Binary search. */
6456 while ((min + 1) < max)
6460 mid = (min + max) / 2;
6462 if (got_vma > r->address)
6464 else if (got_vma < r->address)
6473 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
6474 if (got_vma == p->address
6476 && (p->howto->type == R_X86_64_JUMP_SLOT
6477 || p->howto->type == R_X86_64_GLOB_DAT
6478 || p->howto->type == R_X86_64_IRELATIVE))
6480 *s = **p->sym_ptr_ptr;
6481 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
6482 set. Since we are defining a symbol, ensure one
6484 if ((s->flags & BSF_LOCAL) == 0)
6485 s->flags |= BSF_GLOBAL;
6486 s->flags |= BSF_SYNTHETIC;
6487 /* This is no longer a section symbol. */
6488 s->flags &= ~BSF_SECTION_SYM;
6490 s->the_bfd = plt->owner;
6494 len = strlen ((*p->sym_ptr_ptr)->name);
6495 memcpy (names, (*p->sym_ptr_ptr)->name, len);
6501 memcpy (names, "+0x", sizeof ("+0x") - 1);
6502 names += sizeof ("+0x") - 1;
6503 bfd_sprintf_vma (abfd, buf, p->addend);
6504 for (a = buf; *a == '0'; ++a)
6507 memcpy (names, a, size);
6510 memcpy (names, "@plt", sizeof ("@plt"));
6511 names += sizeof ("@plt");
6515 offset += plt_entry_size;
6519 /* PLT entries with R_X86_64_TLSDESC relocations are skipped. */
6528 for (j = 0; plts[j].name != NULL; j++)
6529 if (plts[j].contents != NULL)
6530 free (plts[j].contents);
6537 /* Handle an x86-64 specific section when reading an object file. This
6538 is called when elfcode.h finds a section with an unknown type. */
6541 elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
6542 const char *name, int shindex)
6544 if (hdr->sh_type != SHT_X86_64_UNWIND)
6547 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6553 /* Hook called by the linker routine which adds symbols from an object
6554 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
6558 elf_x86_64_add_symbol_hook (bfd *abfd,
6559 struct bfd_link_info *info ATTRIBUTE_UNUSED,
6560 Elf_Internal_Sym *sym,
6561 const char **namep ATTRIBUTE_UNUSED,
6562 flagword *flagsp ATTRIBUTE_UNUSED,
6568 switch (sym->st_shndx)
6570 case SHN_X86_64_LCOMMON:
6571 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
6574 lcomm = bfd_make_section_with_flags (abfd,
6578 | SEC_LINKER_CREATED));
6581 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
6584 *valp = sym->st_size;
6592 /* Given a BFD section, try to locate the corresponding ELF section
6596 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6597 asection *sec, int *index_return)
6599 if (sec == &_bfd_elf_large_com_section)
6601 *index_return = SHN_X86_64_LCOMMON;
6607 /* Process a symbol. */
6610 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
6613 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
6615 switch (elfsym->internal_elf_sym.st_shndx)
6617 case SHN_X86_64_LCOMMON:
6618 asym->section = &_bfd_elf_large_com_section;
6619 asym->value = elfsym->internal_elf_sym.st_size;
6620 /* Common symbol doesn't set BSF_GLOBAL. */
6621 asym->flags &= ~BSF_GLOBAL;
6627 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
6629 return (sym->st_shndx == SHN_COMMON
6630 || sym->st_shndx == SHN_X86_64_LCOMMON);
6634 elf_x86_64_common_section_index (asection *sec)
6636 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6639 return SHN_X86_64_LCOMMON;
6643 elf_x86_64_common_section (asection *sec)
6645 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6646 return bfd_com_section_ptr;
6648 return &_bfd_elf_large_com_section;
6652 elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
6653 const Elf_Internal_Sym *sym,
6658 const asection *oldsec)
6660 /* A normal common symbol and a large common symbol result in a
6661 normal common symbol. We turn the large common symbol into a
6664 && h->root.type == bfd_link_hash_common
6666 && bfd_is_com_section (*psec)
6669 if (sym->st_shndx == SHN_COMMON
6670 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
6672 h->root.u.c.p->section
6673 = bfd_make_section_old_way (oldbfd, "COMMON");
6674 h->root.u.c.p->section->flags = SEC_ALLOC;
6676 else if (sym->st_shndx == SHN_X86_64_LCOMMON
6677 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
6678 *psec = bfd_com_section_ptr;
6685 elf_x86_64_additional_program_headers (bfd *abfd,
6686 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6691 /* Check to see if we need a large readonly segment. */
6692 s = bfd_get_section_by_name (abfd, ".lrodata");
6693 if (s && (s->flags & SEC_LOAD))
6696 /* Check to see if we need a large data segment. Since .lbss sections
6697 is placed right after the .bss section, there should be no need for
6698 a large data segment just because of .lbss. */
6699 s = bfd_get_section_by_name (abfd, ".ldata");
6700 if (s && (s->flags & SEC_LOAD))
6706 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
6709 elf_x86_64_relocs_compatible (const bfd_target *input,
6710 const bfd_target *output)
6712 return ((xvec_get_elf_backend_data (input)->s->elfclass
6713 == xvec_get_elf_backend_data (output)->s->elfclass)
6714 && _bfd_elf_relocs_compatible (input, output));
6717 /* Set up x86-64 GNU properties. Return the first relocatable ELF input
6718 with GNU properties if found. Otherwise, return NULL. */
6721 elf_x86_64_link_setup_gnu_properties (struct bfd_link_info *info)
6723 bfd_boolean normal_target;
6724 bfd_boolean lazy_plt;
6725 asection *sec, *pltsec;
6727 bfd_boolean use_ibt_plt;
6728 unsigned int plt_alignment, features;
6729 struct elf_x86_link_hash_table *htab;
6733 struct elf_x86_64_plt_layout *plt_layout;
6734 const struct elf_x86_64_lazy_plt_layout *lazy_plt_layout;
6735 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt_layout;
6739 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
6741 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
6743 /* Find a normal input file with GNU property note. */
6744 for (pbfd = info->input_bfds;
6746 pbfd = pbfd->link.next)
6747 if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
6748 && bfd_count_sections (pbfd) != 0)
6752 if (elf_properties (pbfd) != NULL)
6756 if (ebfd != NULL && features)
6758 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and
6759 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
6760 prop = _bfd_elf_get_property (ebfd,
6761 GNU_PROPERTY_X86_FEATURE_1_AND,
6763 prop->u.number |= features;
6764 prop->pr_kind = property_number;
6766 /* Create the GNU property note section if needed. */
6769 sec = bfd_make_section_with_flags (ebfd,
6770 NOTE_GNU_PROPERTY_SECTION_NAME,
6778 info->callbacks->einfo (_("%F: failed to create GNU property section\n"));
6780 if (!bfd_set_section_alignment (ebfd, sec,
6781 ABI_64_P (ebfd) ? 3 : 2))
6784 info->callbacks->einfo (_("%F%A: failed to align section\n"),
6788 elf_section_type (sec) = SHT_NOTE;
6792 pbfd = _bfd_elf_link_setup_gnu_properties (info);
6794 if (bfd_link_relocatable (info))
6797 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
6801 use_ibt_plt = info->ibtplt || info->ibt;
6802 if (!use_ibt_plt && pbfd != NULL)
6804 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
6805 elf_property_list *p;
6807 /* The property list is sorted in order of type. */
6808 for (p = elf_properties (pbfd); p; p = p->next)
6810 if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
6812 use_ibt_plt = !!(p->property.u.number
6813 & GNU_PROPERTY_X86_FEATURE_1_IBT);
6816 else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
6821 dynobj = htab->elf.dynobj;
6823 /* Set htab->elf.dynobj here so that there is no need to check and
6824 set it in check_relocs. */
6829 htab->elf.dynobj = pbfd;
6836 /* Find a normal input file to hold linker created
6838 for (abfd = info->input_bfds;
6840 abfd = abfd->link.next)
6841 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6843 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6845 htab->elf.dynobj = abfd;
6852 plt_layout = &elf_x86_64_plt (htab);
6854 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
6855 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
6856 canonical function address. */
6857 plt_layout->has_plt0 = 1;
6859 if (get_elf_x86_64_backend_data (info->output_bfd)->os
6864 if (ABI_64_P (dynobj))
6866 elf_x86_64_lazy_plt (htab)
6867 = &elf_x86_64_lazy_ibt_plt;
6868 elf_x86_64_non_lazy_plt (htab)
6869 = &elf_x86_64_non_lazy_ibt_plt;
6873 elf_x86_64_lazy_plt (htab)
6874 = &elf_x32_lazy_ibt_plt;
6875 elf_x86_64_non_lazy_plt (htab)
6876 = &elf_x32_non_lazy_ibt_plt;
6879 else if (info->bndplt)
6881 elf_x86_64_lazy_plt (htab) = &elf_x86_64_lazy_bnd_plt;
6882 elf_x86_64_non_lazy_plt (htab) = &elf_x86_64_non_lazy_bnd_plt;
6886 elf_x86_64_lazy_plt (htab) = &elf_x86_64_lazy_plt;
6887 elf_x86_64_non_lazy_plt (htab) = &elf_x86_64_non_lazy_plt;
6889 normal_target = TRUE;
6893 elf_x86_64_lazy_plt (htab) = &elf_x86_64_nacl_plt;
6894 elf_x86_64_non_lazy_plt (htab) = NULL;
6895 normal_target = FALSE;
6898 lazy_plt_layout = elf_x86_64_lazy_plt (htab);
6899 non_lazy_plt_layout = elf_x86_64_non_lazy_plt (htab);
6901 pltsec = htab->elf.splt;
6903 /* If the non-lazy PLT is available, use it for all PLT entries if
6904 there are no PLT0 or no .plt section. */
6905 if (non_lazy_plt_layout != NULL
6906 && (!plt_layout->has_plt0 || pltsec == NULL))
6909 plt_layout->plt_entry
6910 = non_lazy_plt_layout->plt_entry;
6911 plt_layout->plt_entry_size
6912 = non_lazy_plt_layout->plt_entry_size;
6913 plt_layout->plt_got_offset
6914 = non_lazy_plt_layout->plt_got_offset;
6915 plt_layout->plt_got_insn_size
6916 = non_lazy_plt_layout->plt_got_insn_size;
6917 plt_layout->eh_frame_plt_size
6918 = non_lazy_plt_layout->eh_frame_plt_size;
6919 plt_layout->eh_frame_plt
6920 = non_lazy_plt_layout->eh_frame_plt;
6925 plt_layout->plt_entry
6926 = lazy_plt_layout->plt_entry;
6927 plt_layout->plt_entry_size
6928 = lazy_plt_layout->plt_entry_size;
6929 plt_layout->plt_got_offset
6930 = lazy_plt_layout->plt_got_offset;
6931 plt_layout->plt_got_insn_size
6932 = lazy_plt_layout->plt_got_insn_size;
6933 plt_layout->eh_frame_plt_size
6934 = lazy_plt_layout->eh_frame_plt_size;
6935 plt_layout->eh_frame_plt
6936 = lazy_plt_layout->eh_frame_plt;
6939 /* Return if there are no normal input files. */
6943 /* Since create_dynamic_sections isn't always called, but GOT
6944 relocations need GOT relocations, create them here so that we
6945 don't need to do it in check_relocs. */
6946 if (htab->elf.sgot == NULL
6947 && !_bfd_elf_create_got_section (dynobj, info))
6948 info->callbacks->einfo (_("%F: failed to create GOT sections\n"));
6950 /* Align .got and .got.plt sections to their entry size. Do it here
6951 instead of in create_dynamic_sections so that they are always
6952 properly aligned even if create_dynamic_sections isn't called. */
6953 sec = htab->elf.sgot;
6954 if (!bfd_set_section_alignment (dynobj, sec, 3))
6955 goto error_alignment;
6957 sec = htab->elf.sgotplt;
6958 if (!bfd_set_section_alignment (dynobj, sec, 3))
6959 goto error_alignment;
6961 /* Create the ifunc sections here so that check_relocs can be
6963 if (!_bfd_elf_create_ifunc_sections (dynobj, info))
6964 info->callbacks->einfo (_("%F: failed to create ifunc sections\n"));
6966 plt_alignment = bfd_log2 (plt_layout->plt_entry_size);
6970 /* Whe creating executable, set the contents of the .interp
6971 section to the interpreter. */
6972 if (bfd_link_executable (info) && !info->nointerp)
6974 asection *s = bfd_get_linker_section (dynobj, ".interp");
6977 s->size = htab->dynamic_interpreter_size;
6978 s->contents = (unsigned char *) htab->dynamic_interpreter;
6982 /* Don't change PLT section alignment for NaCl since it uses
6983 64-byte PLT entry and sets PLT section alignment to 32
6984 bytes. Don't create additional PLT sections for NaCl. */
6987 const struct elf_backend_data *bed
6988 = get_elf_backend_data (dynobj);
6989 flagword pltflags = (bed->dynamic_sec_flags
6994 unsigned int non_lazy_plt_alignment
6995 = bfd_log2 (non_lazy_plt_layout->plt_entry_size);
6998 if (!bfd_set_section_alignment (sec->owner, sec,
7000 goto error_alignment;
7002 /* Create the GOT procedure linkage table. */
7003 sec = bfd_make_section_anyway_with_flags (dynobj,
7007 info->callbacks->einfo (_("%F: failed to create GOT PLT section\n"));
7009 if (!bfd_set_section_alignment (dynobj, sec,
7010 non_lazy_plt_alignment))
7011 goto error_alignment;
7013 htab->plt_got = sec;
7021 /* Create the second PLT for Intel IBT support. IBT
7022 PLT is supported only for non-NaCl target and is
7023 is needed only for lazy binding. */
7024 sec = bfd_make_section_anyway_with_flags (dynobj,
7028 info->callbacks->einfo (_("%F: failed to create IBT-enabled PLT section\n"));
7030 if (!bfd_set_section_alignment (dynobj, sec,
7032 goto error_alignment;
7034 else if (info->bndplt && ABI_64_P (dynobj))
7036 /* Create the second PLT for Intel MPX support. MPX
7037 PLT is supported only for non-NaCl target in 64-bit
7038 mode and is needed only for lazy binding. */
7039 sec = bfd_make_section_anyway_with_flags (dynobj,
7043 info->callbacks->einfo (_("%F: failed to create BND PLT section\n"));
7045 if (!bfd_set_section_alignment (dynobj, sec,
7046 non_lazy_plt_alignment))
7047 goto error_alignment;
7050 htab->plt_second = sec;
7054 if (!info->no_ld_generated_unwind_info)
7056 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
7057 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7058 | SEC_LINKER_CREATED);
7060 sec = bfd_make_section_anyway_with_flags (dynobj,
7064 info->callbacks->einfo (_("%F: failed to create PLT .eh_frame section\n"));
7066 if (!bfd_set_section_alignment (dynobj, sec,
7067 ABI_64_P (dynobj) ? 3 : 2))
7068 goto error_alignment;
7070 htab->plt_eh_frame = sec;
7072 if (htab->plt_got != NULL)
7074 sec = bfd_make_section_anyway_with_flags (dynobj,
7078 info->callbacks->einfo (_("%F: failed to create GOT PLT .eh_frame section\n"));
7080 if (!bfd_set_section_alignment (dynobj, sec,
7081 ABI_64_P (dynobj) ? 3 : 2))
7082 goto error_alignment;
7084 htab->plt_got_eh_frame = sec;
7087 if (htab->plt_second != NULL)
7089 sec = bfd_make_section_anyway_with_flags (dynobj,
7093 info->callbacks->einfo (_("%F: failed to create BND PLT .eh_frame section\n"));
7095 if (!bfd_set_section_alignment (dynobj, sec, 3))
7096 goto error_alignment;
7098 htab->plt_second_eh_frame = sec;
7105 /* The .iplt section is used for IFUNC symbols in static
7107 sec = htab->elf.iplt;
7109 && !bfd_set_section_alignment (sec->owner, sec,
7111 goto error_alignment;
7117 static const struct bfd_elf_special_section
7118 elf_x86_64_special_sections[]=
7120 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
7121 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
7122 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
7123 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
7124 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
7125 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
7126 { NULL, 0, 0, 0, 0 }
7129 #define TARGET_LITTLE_SYM x86_64_elf64_vec
7130 #define TARGET_LITTLE_NAME "elf64-x86-64"
7131 #define ELF_ARCH bfd_arch_i386
7132 #define ELF_TARGET_ID X86_64_ELF_DATA
7133 #define ELF_MACHINE_CODE EM_X86_64
7134 #define ELF_MAXPAGESIZE 0x200000
7135 #define ELF_MINPAGESIZE 0x1000
7136 #define ELF_COMMONPAGESIZE 0x1000
7138 #define elf_backend_can_gc_sections 1
7139 #define elf_backend_can_refcount 1
7140 #define elf_backend_want_got_plt 1
7141 #define elf_backend_plt_readonly 1
7142 #define elf_backend_want_plt_sym 0
7143 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
7144 #define elf_backend_rela_normal 1
7145 #define elf_backend_plt_alignment 4
7146 #define elf_backend_extern_protected_data 1
7147 #define elf_backend_caches_rawsize 1
7148 #define elf_backend_dtrel_excludes_plt 1
7149 #define elf_backend_want_dynrelro 1
7151 #define elf_info_to_howto elf_x86_64_info_to_howto
7153 #define bfd_elf64_bfd_link_hash_table_create \
7154 elf_x86_64_link_hash_table_create
7155 #define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
7156 #define bfd_elf64_bfd_reloc_name_lookup \
7157 elf_x86_64_reloc_name_lookup
7159 #define elf_backend_adjust_dynamic_symbol elf_x86_64_adjust_dynamic_symbol
7160 #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
7161 #define elf_backend_check_relocs elf_x86_64_check_relocs
7162 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
7163 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
7164 #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
7165 #define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
7166 #define elf_backend_gc_mark_hook elf_x86_64_gc_mark_hook
7167 #define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
7168 #define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
7170 #define elf_backend_write_core_note elf_x86_64_write_core_note
7172 #define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
7173 #define elf_backend_relocate_section elf_x86_64_relocate_section
7174 #define elf_backend_size_dynamic_sections elf_x86_64_size_dynamic_sections
7175 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
7176 #define elf_backend_object_p elf64_x86_64_elf_object_p
7177 #define bfd_elf64_mkobject elf_x86_64_mkobject
7178 #define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
7180 #define elf_backend_section_from_shdr \
7181 elf_x86_64_section_from_shdr
7183 #define elf_backend_section_from_bfd_section \
7184 elf_x86_64_elf_section_from_bfd_section
7185 #define elf_backend_add_symbol_hook \
7186 elf_x86_64_add_symbol_hook
7187 #define elf_backend_symbol_processing \
7188 elf_x86_64_symbol_processing
7189 #define elf_backend_common_section_index \
7190 elf_x86_64_common_section_index
7191 #define elf_backend_common_section \
7192 elf_x86_64_common_section
7193 #define elf_backend_common_definition \
7194 elf_x86_64_common_definition
7195 #define elf_backend_merge_symbol \
7196 elf_x86_64_merge_symbol
7197 #define elf_backend_special_sections \
7198 elf_x86_64_special_sections
7199 #define elf_backend_additional_program_headers \
7200 elf_x86_64_additional_program_headers
7201 #define elf_backend_setup_gnu_properties \
7202 elf_x86_64_link_setup_gnu_properties
7204 #include "elf64-target.h"
7206 /* CloudABI support. */
7208 #undef TARGET_LITTLE_SYM
7209 #define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
7210 #undef TARGET_LITTLE_NAME
7211 #define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
7214 #define ELF_OSABI ELFOSABI_CLOUDABI
7217 #define elf64_bed elf64_x86_64_cloudabi_bed
7219 #include "elf64-target.h"
7221 /* FreeBSD support. */
7223 #undef TARGET_LITTLE_SYM
7224 #define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
7225 #undef TARGET_LITTLE_NAME
7226 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
7229 #define ELF_OSABI ELFOSABI_FREEBSD
7232 #define elf64_bed elf64_x86_64_fbsd_bed
7234 #include "elf64-target.h"
7236 /* Solaris 2 support. */
7238 #undef TARGET_LITTLE_SYM
7239 #define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
7240 #undef TARGET_LITTLE_NAME
7241 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
7243 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
7244 objects won't be recognized. */
7248 #define elf64_bed elf64_x86_64_sol2_bed
7250 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
7252 #undef elf_backend_static_tls_alignment
7253 #define elf_backend_static_tls_alignment 16
7255 /* The Solaris 2 ABI requires a plt symbol on all platforms.
7257 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
7259 #undef elf_backend_want_plt_sym
7260 #define elf_backend_want_plt_sym 1
7262 #undef elf_backend_strtab_flags
7263 #define elf_backend_strtab_flags SHF_STRINGS
7266 elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
7267 bfd *obfd ATTRIBUTE_UNUSED,
7268 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
7269 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
7271 /* PR 19938: FIXME: Need to add code for setting the sh_info
7272 and sh_link fields of Solaris specific section types. */
7276 #undef elf_backend_copy_special_section_fields
7277 #define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
7279 #include "elf64-target.h"
7281 /* Native Client support. */
7284 elf64_x86_64_nacl_elf_object_p (bfd *abfd)
7286 /* Set the right machine number for a NaCl x86-64 ELF64 file. */
7287 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64_nacl);
7291 #undef TARGET_LITTLE_SYM
7292 #define TARGET_LITTLE_SYM x86_64_elf64_nacl_vec
7293 #undef TARGET_LITTLE_NAME
7294 #define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
7296 #define elf64_bed elf64_x86_64_nacl_bed
7298 #undef ELF_MAXPAGESIZE
7299 #undef ELF_MINPAGESIZE
7300 #undef ELF_COMMONPAGESIZE
7301 #define ELF_MAXPAGESIZE 0x10000
7302 #define ELF_MINPAGESIZE 0x10000
7303 #define ELF_COMMONPAGESIZE 0x10000
7305 /* Restore defaults. */
7307 #undef elf_backend_static_tls_alignment
7308 #undef elf_backend_want_plt_sym
7309 #define elf_backend_want_plt_sym 0
7310 #undef elf_backend_strtab_flags
7311 #undef elf_backend_copy_special_section_fields
7313 /* NaCl uses substantially different PLT entries for the same effects. */
7315 #undef elf_backend_plt_alignment
7316 #define elf_backend_plt_alignment 5
7317 #define NACL_PLT_ENTRY_SIZE 64
7318 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
7320 static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
7322 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
7323 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
7324 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
7325 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
7326 0x41, 0xff, 0xe3, /* jmpq *%r11 */
7328 /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
7329 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1) */
7331 /* 32 bytes of nop to pad out to the standard size. */
7332 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7333 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7334 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7335 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7336 0x66, /* excess data16 prefix */
7340 static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
7342 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
7343 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
7344 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
7345 0x41, 0xff, 0xe3, /* jmpq *%r11 */
7347 /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
7348 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7349 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7351 /* Lazy GOT entries point here (32-byte aligned). */
7352 0x68, /* pushq immediate */
7353 0, 0, 0, 0, /* replaced with index into relocation table. */
7354 0xe9, /* jmp relative */
7355 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
7357 /* 22 bytes of nop to pad out to the standard size. */
7358 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7359 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7360 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
7363 /* .eh_frame covering the .plt section. */
7365 static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
7367 #if (PLT_CIE_LENGTH != 20 \
7368 || PLT_FDE_LENGTH != 36 \
7369 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
7370 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
7371 # error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
7373 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
7374 0, 0, 0, 0, /* CIE ID */
7375 1, /* CIE version */
7376 'z', 'R', 0, /* Augmentation string */
7377 1, /* Code alignment factor */
7378 0x78, /* Data alignment factor */
7379 16, /* Return address column */
7380 1, /* Augmentation size */
7381 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
7382 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
7383 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
7384 DW_CFA_nop, DW_CFA_nop,
7386 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
7387 PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
7388 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
7389 0, 0, 0, 0, /* .plt size goes here */
7390 0, /* Augmentation size */
7391 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
7392 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
7393 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
7394 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
7395 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
7396 13, /* Block length */
7397 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
7398 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
7399 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
7400 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
7401 DW_CFA_nop, DW_CFA_nop
7404 static const struct elf_x86_64_lazy_plt_layout elf_x86_64_nacl_plt =
7406 elf_x86_64_nacl_plt0_entry, /* plt0_entry */
7407 elf_x86_64_nacl_plt_entry, /* plt_entry */
7408 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
7409 2, /* plt0_got1_offset */
7410 9, /* plt0_got2_offset */
7411 13, /* plt0_got2_insn_end */
7412 3, /* plt_got_offset */
7413 33, /* plt_reloc_offset */
7414 38, /* plt_plt_offset */
7415 7, /* plt_got_insn_size */
7416 42, /* plt_plt_insn_end */
7417 32, /* plt_lazy_offset */
7418 elf_x86_64_nacl_eh_frame_plt, /* eh_frame_plt */
7419 sizeof (elf_x86_64_nacl_eh_frame_plt) /* eh_frame_plt_size */
7422 static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
7427 #undef elf_backend_arch_data
7428 #define elf_backend_arch_data &elf_x86_64_nacl_arch_bed
7430 #undef elf_backend_object_p
7431 #define elf_backend_object_p elf64_x86_64_nacl_elf_object_p
7432 #undef elf_backend_modify_segment_map
7433 #define elf_backend_modify_segment_map nacl_modify_segment_map
7434 #undef elf_backend_modify_program_headers
7435 #define elf_backend_modify_program_headers nacl_modify_program_headers
7436 #undef elf_backend_final_write_processing
7437 #define elf_backend_final_write_processing nacl_final_write_processing
7439 #include "elf64-target.h"
7441 /* Native Client x32 support. */
7444 elf32_x86_64_nacl_elf_object_p (bfd *abfd)
7446 /* Set the right machine number for a NaCl x86-64 ELF32 file. */
7447 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32_nacl);
7451 #undef TARGET_LITTLE_SYM
7452 #define TARGET_LITTLE_SYM x86_64_elf32_nacl_vec
7453 #undef TARGET_LITTLE_NAME
7454 #define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
7456 #define elf32_bed elf32_x86_64_nacl_bed
7458 #define bfd_elf32_bfd_link_hash_table_create \
7459 elf_x86_64_link_hash_table_create
7460 #define bfd_elf32_bfd_reloc_type_lookup \
7461 elf_x86_64_reloc_type_lookup
7462 #define bfd_elf32_bfd_reloc_name_lookup \
7463 elf_x86_64_reloc_name_lookup
7464 #define bfd_elf32_mkobject \
7466 #define bfd_elf32_get_synthetic_symtab \
7467 elf_x86_64_get_synthetic_symtab
7469 #undef elf_backend_object_p
7470 #define elf_backend_object_p \
7471 elf32_x86_64_nacl_elf_object_p
7473 #undef elf_backend_bfd_from_remote_memory
7474 #define elf_backend_bfd_from_remote_memory \
7475 _bfd_elf32_bfd_from_remote_memory
7477 #undef elf_backend_size_info
7478 #define elf_backend_size_info \
7479 _bfd_elf32_size_info
7481 #include "elf32-target.h"
7483 /* Restore defaults. */
7484 #undef elf_backend_object_p
7485 #define elf_backend_object_p elf64_x86_64_elf_object_p
7486 #undef elf_backend_bfd_from_remote_memory
7487 #undef elf_backend_size_info
7488 #undef elf_backend_modify_segment_map
7489 #undef elf_backend_modify_program_headers
7490 #undef elf_backend_final_write_processing
7492 /* Intel L1OM support. */
7495 elf64_l1om_elf_object_p (bfd *abfd)
7497 /* Set the right machine number for an L1OM elf64 file. */
7498 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
7502 #undef TARGET_LITTLE_SYM
7503 #define TARGET_LITTLE_SYM l1om_elf64_vec
7504 #undef TARGET_LITTLE_NAME
7505 #define TARGET_LITTLE_NAME "elf64-l1om"
7507 #define ELF_ARCH bfd_arch_l1om
7509 #undef ELF_MACHINE_CODE
7510 #define ELF_MACHINE_CODE EM_L1OM
7515 #define elf64_bed elf64_l1om_bed
7517 #undef elf_backend_object_p
7518 #define elf_backend_object_p elf64_l1om_elf_object_p
7520 /* Restore defaults. */
7521 #undef ELF_MAXPAGESIZE
7522 #undef ELF_MINPAGESIZE
7523 #undef ELF_COMMONPAGESIZE
7524 #define ELF_MAXPAGESIZE 0x200000
7525 #define ELF_MINPAGESIZE 0x1000
7526 #define ELF_COMMONPAGESIZE 0x1000
7527 #undef elf_backend_plt_alignment
7528 #define elf_backend_plt_alignment 4
7529 #undef elf_backend_arch_data
7530 #define elf_backend_arch_data &elf_x86_64_arch_bed
7532 #include "elf64-target.h"
7534 /* FreeBSD L1OM support. */
7536 #undef TARGET_LITTLE_SYM
7537 #define TARGET_LITTLE_SYM l1om_elf64_fbsd_vec
7538 #undef TARGET_LITTLE_NAME
7539 #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
7542 #define ELF_OSABI ELFOSABI_FREEBSD
7545 #define elf64_bed elf64_l1om_fbsd_bed
7547 #include "elf64-target.h"
7549 /* Intel K1OM support. */
7552 elf64_k1om_elf_object_p (bfd *abfd)
7554 /* Set the right machine number for an K1OM elf64 file. */
7555 bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
7559 #undef TARGET_LITTLE_SYM
7560 #define TARGET_LITTLE_SYM k1om_elf64_vec
7561 #undef TARGET_LITTLE_NAME
7562 #define TARGET_LITTLE_NAME "elf64-k1om"
7564 #define ELF_ARCH bfd_arch_k1om
7566 #undef ELF_MACHINE_CODE
7567 #define ELF_MACHINE_CODE EM_K1OM
7572 #define elf64_bed elf64_k1om_bed
7574 #undef elf_backend_object_p
7575 #define elf_backend_object_p elf64_k1om_elf_object_p
7577 #undef elf_backend_static_tls_alignment
7579 #undef elf_backend_want_plt_sym
7580 #define elf_backend_want_plt_sym 0
7582 #include "elf64-target.h"
7584 /* FreeBSD K1OM support. */
7586 #undef TARGET_LITTLE_SYM
7587 #define TARGET_LITTLE_SYM k1om_elf64_fbsd_vec
7588 #undef TARGET_LITTLE_NAME
7589 #define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
7592 #define ELF_OSABI ELFOSABI_FREEBSD
7595 #define elf64_bed elf64_k1om_fbsd_bed
7597 #include "elf64-target.h"
7599 /* 32bit x86-64 support. */
7601 #undef TARGET_LITTLE_SYM
7602 #define TARGET_LITTLE_SYM x86_64_elf32_vec
7603 #undef TARGET_LITTLE_NAME
7604 #define TARGET_LITTLE_NAME "elf32-x86-64"
7608 #define ELF_ARCH bfd_arch_i386
7610 #undef ELF_MACHINE_CODE
7611 #define ELF_MACHINE_CODE EM_X86_64
7615 #undef elf_backend_object_p
7616 #define elf_backend_object_p \
7617 elf32_x86_64_elf_object_p
7619 #undef elf_backend_bfd_from_remote_memory
7620 #define elf_backend_bfd_from_remote_memory \
7621 _bfd_elf32_bfd_from_remote_memory
7623 #undef elf_backend_size_info
7624 #define elf_backend_size_info \
7625 _bfd_elf32_size_info
7627 #include "elf32-target.h"