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_next_tls_desc_index(htab) \
1076 ((struct elf_x86_64_link_hash_table *) (htab))->next_tls_desc_index
1078 #define elf_x86_64_srelplt2(htab) \
1079 ((struct elf_x86_64_link_hash_table *) (htab))->srelplt2
1081 #define elf_x86_64_plt(htab) \
1082 ((struct elf_x86_64_link_hash_table *) (htab))->plt
1084 #define elf_x86_64_lazy_plt(htab) \
1085 ((struct elf_x86_64_link_hash_table *) (htab))->lazy_plt
1087 #define elf_x86_64_non_lazy_plt(htab) \
1088 ((struct elf_x86_64_link_hash_table *) (htab))->non_lazy_plt
1090 #define elf_x86_64_compute_jump_table_size(htab) \
1091 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
1093 /* Create an X86-64 ELF linker hash table. */
1095 static struct bfd_link_hash_table *
1096 elf_x86_64_link_hash_table_create (bfd *abfd)
1098 struct elf_x86_link_hash_table *ret;
1099 bfd_size_type amt = sizeof (struct elf_x86_64_link_hash_table);
1101 ret = (struct elf_x86_link_hash_table *) bfd_zmalloc (amt);
1105 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1106 _bfd_x86_elf_link_hash_newfunc,
1107 sizeof (struct elf_x86_link_hash_entry),
1114 if (ABI_64_P (abfd))
1116 ret->r_info = elf64_r_info;
1117 ret->r_sym = elf64_r_sym;
1118 ret->pointer_r_type = R_X86_64_64;
1119 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
1120 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
1124 ret->r_info = elf32_r_info;
1125 ret->r_sym = elf32_r_sym;
1126 ret->pointer_r_type = R_X86_64_32;
1127 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
1128 ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
1131 ret->tls_get_addr = "__tls_get_addr";
1132 ret->loc_hash_table = htab_try_create (1024,
1133 _bfd_x86_elf_local_htab_hash,
1134 _bfd_x86_elf_local_htab_eq,
1136 ret->loc_hash_memory = objalloc_create ();
1137 if (!ret->loc_hash_table || !ret->loc_hash_memory)
1139 _bfd_x86_elf_link_hash_table_free (abfd);
1142 ret->elf.root.hash_table_free = _bfd_x86_elf_link_hash_table_free;
1144 return &ret->elf.root;
1148 elf64_x86_64_elf_object_p (bfd *abfd)
1150 /* Set the right machine number for an x86-64 elf64 file. */
1151 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
1156 elf32_x86_64_elf_object_p (bfd *abfd)
1158 /* Set the right machine number for an x86-64 elf32 file. */
1159 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1163 /* Return TRUE if the TLS access code sequence support transition
1167 elf_x86_64_check_tls_transition (bfd *abfd,
1168 struct bfd_link_info *info,
1171 Elf_Internal_Shdr *symtab_hdr,
1172 struct elf_link_hash_entry **sym_hashes,
1173 unsigned int r_type,
1174 const Elf_Internal_Rela *rel,
1175 const Elf_Internal_Rela *relend)
1178 unsigned long r_symndx;
1179 bfd_boolean largepic = FALSE;
1180 struct elf_link_hash_entry *h;
1182 struct elf_x86_link_hash_table *htab;
1184 bfd_boolean indirect_call;
1186 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1187 offset = rel->r_offset;
1190 case R_X86_64_TLSGD:
1191 case R_X86_64_TLSLD:
1192 if ((rel + 1) >= relend)
1195 if (r_type == R_X86_64_TLSGD)
1197 /* Check transition from GD access model. For 64bit, only
1198 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1199 .word 0x6666; rex64; call __tls_get_addr@PLT
1201 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1203 call *__tls_get_addr@GOTPCREL(%rip)
1204 which may be converted to
1205 addr32 call __tls_get_addr
1206 can transit to different access model. For 32bit, only
1207 leaq foo@tlsgd(%rip), %rdi
1208 .word 0x6666; rex64; call __tls_get_addr@PLT
1210 leaq foo@tlsgd(%rip), %rdi
1212 call *__tls_get_addr@GOTPCREL(%rip)
1213 which may be converted to
1214 addr32 call __tls_get_addr
1215 can transit to different access model. For largepic,
1217 leaq foo@tlsgd(%rip), %rdi
1218 movabsq $__tls_get_addr@pltoff, %rax
1222 leaq foo@tlsgd(%rip), %rdi
1223 movabsq $__tls_get_addr@pltoff, %rax
1227 static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1229 if ((offset + 12) > sec->size)
1232 call = contents + offset + 4;
1234 || !((call[1] == 0x48
1242 && call[3] == 0xe8)))
1244 if (!ABI_64_P (abfd)
1245 || (offset + 19) > sec->size
1247 || memcmp (call - 7, leaq + 1, 3) != 0
1248 || memcmp (call, "\x48\xb8", 2) != 0
1252 || !((call[10] == 0x48 && call[12] == 0xd8)
1253 || (call[10] == 0x4c && call[12] == 0xf8)))
1257 else if (ABI_64_P (abfd))
1260 || memcmp (contents + offset - 4, leaq, 4) != 0)
1266 || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
1269 indirect_call = call[2] == 0xff;
1273 /* Check transition from LD access model. Only
1274 leaq foo@tlsld(%rip), %rdi;
1275 call __tls_get_addr@PLT
1277 leaq foo@tlsld(%rip), %rdi;
1278 call *__tls_get_addr@GOTPCREL(%rip)
1279 which may be converted to
1280 addr32 call __tls_get_addr
1281 can transit to different access model. For largepic
1283 leaq foo@tlsld(%rip), %rdi
1284 movabsq $__tls_get_addr@pltoff, %rax
1288 leaq foo@tlsld(%rip), %rdi
1289 movabsq $__tls_get_addr@pltoff, %rax
1293 static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
1295 if (offset < 3 || (offset + 9) > sec->size)
1298 if (memcmp (contents + offset - 3, lea, 3) != 0)
1301 call = contents + offset + 4;
1302 if (!(call[0] == 0xe8
1303 || (call[0] == 0xff && call[1] == 0x15)
1304 || (call[0] == 0x67 && call[1] == 0xe8)))
1306 if (!ABI_64_P (abfd)
1307 || (offset + 19) > sec->size
1308 || memcmp (call, "\x48\xb8", 2) != 0
1312 || !((call[10] == 0x48 && call[12] == 0xd8)
1313 || (call[10] == 0x4c && call[12] == 0xf8)))
1317 indirect_call = call[0] == 0xff;
1320 r_symndx = htab->r_sym (rel[1].r_info);
1321 if (r_symndx < symtab_hdr->sh_info)
1324 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1326 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
1329 return ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLTOFF64;
1330 else if (indirect_call)
1331 return ELF32_R_TYPE (rel[1].r_info) == R_X86_64_GOTPCRELX;
1333 return (ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PC32
1334 || ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
1336 case R_X86_64_GOTTPOFF:
1337 /* Check transition from IE access model:
1338 mov foo@gottpoff(%rip), %reg
1339 add foo@gottpoff(%rip), %reg
1342 /* Check REX prefix first. */
1343 if (offset >= 3 && (offset + 4) <= sec->size)
1345 val = bfd_get_8 (abfd, contents + offset - 3);
1346 if (val != 0x48 && val != 0x4c)
1348 /* X32 may have 0x44 REX prefix or no REX prefix. */
1349 if (ABI_64_P (abfd))
1355 /* X32 may not have any REX prefix. */
1356 if (ABI_64_P (abfd))
1358 if (offset < 2 || (offset + 3) > sec->size)
1362 val = bfd_get_8 (abfd, contents + offset - 2);
1363 if (val != 0x8b && val != 0x03)
1366 val = bfd_get_8 (abfd, contents + offset - 1);
1367 return (val & 0xc7) == 5;
1369 case R_X86_64_GOTPC32_TLSDESC:
1370 /* Check transition from GDesc access model:
1371 leaq x@tlsdesc(%rip), %rax
1373 Make sure it's a leaq adding rip to a 32-bit offset
1374 into any register, although it's probably almost always
1377 if (offset < 3 || (offset + 4) > sec->size)
1380 val = bfd_get_8 (abfd, contents + offset - 3);
1381 if ((val & 0xfb) != 0x48)
1384 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1387 val = bfd_get_8 (abfd, contents + offset - 1);
1388 return (val & 0xc7) == 0x05;
1390 case R_X86_64_TLSDESC_CALL:
1391 /* Check transition from GDesc access model:
1392 call *x@tlsdesc(%rax)
1394 if (offset + 2 <= sec->size)
1396 /* Make sure that it's a call *x@tlsdesc(%rax). */
1397 call = contents + offset;
1398 return call[0] == 0xff && call[1] == 0x10;
1408 /* Return TRUE if the TLS access transition is OK or no transition
1409 will be performed. Update R_TYPE if there is a transition. */
1412 elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1413 asection *sec, bfd_byte *contents,
1414 Elf_Internal_Shdr *symtab_hdr,
1415 struct elf_link_hash_entry **sym_hashes,
1416 unsigned int *r_type, int tls_type,
1417 const Elf_Internal_Rela *rel,
1418 const Elf_Internal_Rela *relend,
1419 struct elf_link_hash_entry *h,
1420 unsigned long r_symndx,
1421 bfd_boolean from_relocate_section)
1423 unsigned int from_type = *r_type;
1424 unsigned int to_type = from_type;
1425 bfd_boolean check = TRUE;
1427 /* Skip TLS transition for functions. */
1429 && (h->type == STT_FUNC
1430 || h->type == STT_GNU_IFUNC))
1435 case R_X86_64_TLSGD:
1436 case R_X86_64_GOTPC32_TLSDESC:
1437 case R_X86_64_TLSDESC_CALL:
1438 case R_X86_64_GOTTPOFF:
1439 if (bfd_link_executable (info))
1442 to_type = R_X86_64_TPOFF32;
1444 to_type = R_X86_64_GOTTPOFF;
1447 /* When we are called from elf_x86_64_relocate_section, there may
1448 be additional transitions based on TLS_TYPE. */
1449 if (from_relocate_section)
1451 unsigned int new_to_type = to_type;
1453 if (bfd_link_executable (info)
1456 && tls_type == GOT_TLS_IE)
1457 new_to_type = R_X86_64_TPOFF32;
1459 if (to_type == R_X86_64_TLSGD
1460 || to_type == R_X86_64_GOTPC32_TLSDESC
1461 || to_type == R_X86_64_TLSDESC_CALL)
1463 if (tls_type == GOT_TLS_IE)
1464 new_to_type = R_X86_64_GOTTPOFF;
1467 /* We checked the transition before when we were called from
1468 elf_x86_64_check_relocs. We only want to check the new
1469 transition which hasn't been checked before. */
1470 check = new_to_type != to_type && from_type == to_type;
1471 to_type = new_to_type;
1476 case R_X86_64_TLSLD:
1477 if (bfd_link_executable (info))
1478 to_type = R_X86_64_TPOFF32;
1485 /* Return TRUE if there is no transition. */
1486 if (from_type == to_type)
1489 /* Check if the transition can be performed. */
1491 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1492 symtab_hdr, sym_hashes,
1493 from_type, rel, relend))
1495 reloc_howto_type *from, *to;
1498 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1499 to = elf_x86_64_rtype_to_howto (abfd, to_type);
1502 name = h->root.root.string;
1505 struct elf_x86_link_hash_table *htab;
1507 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1512 Elf_Internal_Sym *isym;
1514 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1516 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1521 /* xgettext:c-format */
1522 (_("%B: TLS transition from %s to %s against `%s' at %#Lx "
1523 "in section `%A' failed"),
1524 abfd, from->name, to->name, name, rel->r_offset, sec);
1525 bfd_set_error (bfd_error_bad_value);
1533 /* Rename some of the generic section flags to better document how they
1535 #define need_convert_load sec_flg0
1536 #define check_relocs_failed sec_flg1
1539 elf_x86_64_need_pic (struct bfd_link_info *info,
1540 bfd *input_bfd, asection *sec,
1541 struct elf_link_hash_entry *h,
1542 Elf_Internal_Shdr *symtab_hdr,
1543 Elf_Internal_Sym *isym,
1544 reloc_howto_type *howto)
1547 const char *und = "";
1548 const char *pic = "";
1554 name = h->root.root.string;
1555 switch (ELF_ST_VISIBILITY (h->other))
1558 v = _("hidden symbol ");
1561 v = _("internal symbol ");
1564 v = _("protected symbol ");
1567 if (((struct elf_x86_link_hash_entry *) h)->def_protected)
1568 v = _("protected symbol ");
1571 pic = _("; recompile with -fPIC");
1575 if (!h->def_regular && !h->def_dynamic)
1576 und = _("undefined ");
1580 name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
1581 pic = _("; recompile with -fPIC");
1584 if (bfd_link_dll (info))
1585 object = _("a shared object");
1586 else if (bfd_link_pie (info))
1587 object = _("a PIE object");
1589 object = _("a PDE object");
1591 /* xgettext:c-format */
1592 _bfd_error_handler (_("%B: relocation %s against %s%s`%s' can "
1593 "not be used when making %s%s"),
1594 input_bfd, howto->name, und, v, name,
1596 bfd_set_error (bfd_error_bad_value);
1597 sec->check_relocs_failed = 1;
1601 /* With the local symbol, foo, we convert
1602 mov foo@GOTPCREL(%rip), %reg
1606 call/jmp *foo@GOTPCREL(%rip)
1608 nop call foo/jmp foo nop
1609 When PIC is false, convert
1610 test %reg, foo@GOTPCREL(%rip)
1614 binop foo@GOTPCREL(%rip), %reg
1617 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1621 elf_x86_64_convert_load_reloc (bfd *abfd, asection *sec,
1623 Elf_Internal_Rela *irel,
1624 struct elf_link_hash_entry *h,
1625 bfd_boolean *converted,
1626 struct bfd_link_info *link_info)
1628 struct elf_x86_link_hash_table *htab;
1630 bfd_boolean require_reloc_pc32;
1632 bfd_boolean to_reloc_pc32;
1635 bfd_signed_vma raddend;
1636 unsigned int opcode;
1638 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
1639 unsigned int r_symndx;
1641 bfd_vma roff = irel->r_offset;
1643 if (roff < (r_type == R_X86_64_REX_GOTPCRELX ? 3 : 2))
1646 raddend = irel->r_addend;
1647 /* Addend for 32-bit PC-relative relocation must be -4. */
1651 htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
1652 is_pic = bfd_link_pic (link_info);
1654 relocx = (r_type == R_X86_64_GOTPCRELX
1655 || r_type == R_X86_64_REX_GOTPCRELX);
1657 /* TRUE if we can convert only to R_X86_64_PC32. Enable it for
1660 = link_info->disable_target_specific_optimizations > 1;
1662 r_symndx = htab->r_sym (irel->r_info);
1664 opcode = bfd_get_8 (abfd, contents + roff - 2);
1666 /* Convert mov to lea since it has been done for a while. */
1669 /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
1670 for call, jmp or one of adc, add, and, cmp, or, sbb, sub,
1671 test, xor instructions. */
1676 /* We convert only to R_X86_64_PC32:
1678 2. R_X86_64_GOTPCREL since we can't modify REX byte.
1679 3. require_reloc_pc32 is true.
1682 to_reloc_pc32 = (opcode == 0xff
1684 || require_reloc_pc32
1687 /* Get the symbol referred to by the reloc. */
1690 Elf_Internal_Sym *isym
1691 = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
1693 /* Skip relocation against undefined symbols. */
1694 if (isym->st_shndx == SHN_UNDEF)
1697 symtype = ELF_ST_TYPE (isym->st_info);
1699 if (isym->st_shndx == SHN_ABS)
1700 tsec = bfd_abs_section_ptr;
1701 else if (isym->st_shndx == SHN_COMMON)
1702 tsec = bfd_com_section_ptr;
1703 else if (isym->st_shndx == SHN_X86_64_LCOMMON)
1704 tsec = &_bfd_elf_large_com_section;
1706 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1708 toff = isym->st_value;
1712 /* Undefined weak symbol is only bound locally in executable
1713 and its reference is resolved as 0 without relocation
1714 overflow. We can only perform this optimization for
1715 GOTPCRELX relocations since we need to modify REX byte.
1716 It is OK convert mov with R_X86_64_GOTPCREL to
1718 if ((relocx || opcode == 0x8b)
1719 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (link_info,
1722 elf_x86_hash_entry (h)))
1726 /* Skip for branch instructions since R_X86_64_PC32
1728 if (require_reloc_pc32)
1733 /* For non-branch instructions, we can convert to
1734 R_X86_64_32/R_X86_64_32S since we know if there
1736 to_reloc_pc32 = FALSE;
1739 /* Since we don't know the current PC when PIC is true,
1740 we can't convert to R_X86_64_PC32. */
1741 if (to_reloc_pc32 && is_pic)
1746 /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1747 ld.so may use its link-time address. */
1748 else if (h->start_stop
1750 || h->root.type == bfd_link_hash_defined
1751 || h->root.type == bfd_link_hash_defweak)
1752 && h != htab->elf.hdynamic
1753 && SYMBOL_REFERENCES_LOCAL (link_info, h)))
1755 /* bfd_link_hash_new or bfd_link_hash_undefined is
1756 set by an assignment in a linker script in
1757 bfd_elf_record_link_assignment. start_stop is set
1758 on __start_SECNAME/__stop_SECNAME which mark section
1762 && (h->root.type == bfd_link_hash_new
1763 || h->root.type == bfd_link_hash_undefined
1764 || ((h->root.type == bfd_link_hash_defined
1765 || h->root.type == bfd_link_hash_defweak)
1766 && h->root.u.def.section == bfd_und_section_ptr))))
1768 /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
1769 if (require_reloc_pc32)
1773 tsec = h->root.u.def.section;
1774 toff = h->root.u.def.value;
1781 /* Don't convert GOTPCREL relocation against large section. */
1782 if (elf_section_data (tsec) != NULL
1783 && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
1786 /* We can only estimate relocation overflow for R_X86_64_PC32. */
1790 if (tsec->sec_info_type == SEC_INFO_TYPE_MERGE)
1792 /* At this stage in linking, no SEC_MERGE symbol has been
1793 adjusted, so all references to such symbols need to be
1794 passed through _bfd_merged_section_offset. (Later, in
1795 relocate_section, all SEC_MERGE symbols *except* for
1796 section symbols have been adjusted.)
1798 gas may reduce relocations against symbols in SEC_MERGE
1799 sections to a relocation against the section symbol when
1800 the original addend was zero. When the reloc is against
1801 a section symbol we should include the addend in the
1802 offset passed to _bfd_merged_section_offset, since the
1803 location of interest is the original symbol. On the
1804 other hand, an access to "sym+addend" where "sym" is not
1805 a section symbol should not include the addend; Such an
1806 access is presumed to be an offset from "sym"; The
1807 location of interest is just "sym". */
1808 if (symtype == STT_SECTION)
1811 toff = _bfd_merged_section_offset (abfd, &tsec,
1812 elf_section_data (tsec)->sec_info,
1815 if (symtype != STT_SECTION)
1821 /* Don't convert if R_X86_64_PC32 relocation overflows. */
1822 if (tsec->output_section == sec->output_section)
1824 if ((toff - roff + 0x80000000) > 0xffffffff)
1829 bfd_signed_vma distance;
1831 /* At this point, we don't know the load addresses of TSEC
1832 section nor SEC section. We estimate the distrance between
1833 SEC and TSEC. We store the estimated distances in the
1834 compressed_size field of the output section, which is only
1835 used to decompress the compressed input section. */
1836 if (sec->output_section->compressed_size == 0)
1839 bfd_size_type size = 0;
1840 for (asect = link_info->output_bfd->sections;
1842 asect = asect->next)
1843 /* Skip debug sections since compressed_size is used to
1844 compress debug sections. */
1845 if ((asect->flags & SEC_DEBUGGING) == 0)
1848 for (i = asect->map_head.s;
1852 size = align_power (size, i->alignment_power);
1855 asect->compressed_size = size;
1859 /* Don't convert GOTPCREL relocations if TSEC isn't placed
1861 distance = (tsec->output_section->compressed_size
1862 - sec->output_section->compressed_size);
1866 /* Take PT_GNU_RELRO segment into account by adding
1868 if ((toff + distance + get_elf_backend_data (abfd)->maxpagesize
1869 - roff + 0x80000000) > 0xffffffff)
1876 /* We have "call/jmp *foo@GOTPCREL(%rip)". */
1881 /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
1883 modrm = bfd_get_8 (abfd, contents + roff - 1);
1886 /* Convert to "jmp foo nop". */
1889 nop_offset = irel->r_offset + 3;
1890 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1891 irel->r_offset -= 1;
1892 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1896 struct elf_x86_link_hash_entry *eh
1897 = (struct elf_x86_link_hash_entry *) h;
1899 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1902 /* To support TLS optimization, always use addr32 prefix for
1903 "call *__tls_get_addr@GOTPCREL(%rip)". */
1904 if (eh && eh->tls_get_addr)
1907 nop_offset = irel->r_offset - 2;
1911 nop = link_info->call_nop_byte;
1912 if (link_info->call_nop_as_suffix)
1914 nop_offset = irel->r_offset + 3;
1915 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1916 irel->r_offset -= 1;
1917 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1920 nop_offset = irel->r_offset - 2;
1923 bfd_put_8 (abfd, nop, contents + nop_offset);
1924 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1925 r_type = R_X86_64_PC32;
1930 unsigned int rex_mask = REX_R;
1932 if (r_type == R_X86_64_REX_GOTPCRELX)
1933 rex = bfd_get_8 (abfd, contents + roff - 3);
1941 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1942 "lea foo(%rip), %reg". */
1944 r_type = R_X86_64_PC32;
1948 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1949 "mov $foo, %reg". */
1951 modrm = bfd_get_8 (abfd, contents + roff - 1);
1952 modrm = 0xc0 | (modrm & 0x38) >> 3;
1953 if ((rex & REX_W) != 0
1954 && ABI_64_P (link_info->output_bfd))
1956 /* Keep the REX_W bit in REX byte for LP64. */
1957 r_type = R_X86_64_32S;
1958 goto rewrite_modrm_rex;
1962 /* If the REX_W bit in REX byte isn't needed,
1963 use R_X86_64_32 and clear the W bit to avoid
1964 sign-extend imm32 to imm64. */
1965 r_type = R_X86_64_32;
1966 /* Clear the W bit in REX byte. */
1968 goto rewrite_modrm_rex;
1974 /* R_X86_64_PC32 isn't supported. */
1978 modrm = bfd_get_8 (abfd, contents + roff - 1);
1981 /* Convert "test %reg, foo@GOTPCREL(%rip)" to
1982 "test $foo, %reg". */
1983 modrm = 0xc0 | (modrm & 0x38) >> 3;
1988 /* Convert "binop foo@GOTPCREL(%rip), %reg" to
1989 "binop $foo, %reg". */
1990 modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
1994 /* Use R_X86_64_32 with 32-bit operand to avoid relocation
1995 overflow when sign-extending imm32 to imm64. */
1996 r_type = (rex & REX_W) != 0 ? R_X86_64_32S : R_X86_64_32;
1999 bfd_put_8 (abfd, modrm, contents + roff - 1);
2003 /* Move the R bit to the B bit in REX byte. */
2004 rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
2005 bfd_put_8 (abfd, rex, contents + roff - 3);
2008 /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
2012 bfd_put_8 (abfd, opcode, contents + roff - 2);
2015 irel->r_info = htab->r_info (r_symndx, r_type);
2022 /* Look through the relocs for a section during the first phase, and
2023 calculate needed space in the global offset table, procedure
2024 linkage table, and dynamic reloc sections. */
2027 elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
2029 const Elf_Internal_Rela *relocs)
2031 struct elf_x86_link_hash_table *htab;
2032 Elf_Internal_Shdr *symtab_hdr;
2033 struct elf_link_hash_entry **sym_hashes;
2034 const Elf_Internal_Rela *rel;
2035 const Elf_Internal_Rela *rel_end;
2039 if (bfd_link_relocatable (info))
2042 /* Don't do anything special with non-loaded, non-alloced sections.
2043 In particular, any relocs in such sections should not affect GOT
2044 and PLT reference counting (ie. we don't allow them to create GOT
2045 or PLT entries), there's no possibility or desire to optimize TLS
2046 relocs, and there's not much point in propagating relocs to shared
2047 libs that the dynamic linker won't relocate. */
2048 if ((sec->flags & SEC_ALLOC) == 0)
2051 BFD_ASSERT (is_x86_64_elf (abfd));
2053 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2056 sec->check_relocs_failed = 1;
2060 /* Get the section contents. */
2061 if (elf_section_data (sec)->this_hdr.contents != NULL)
2062 contents = elf_section_data (sec)->this_hdr.contents;
2063 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2065 sec->check_relocs_failed = 1;
2069 symtab_hdr = &elf_symtab_hdr (abfd);
2070 sym_hashes = elf_sym_hashes (abfd);
2074 rel_end = relocs + sec->reloc_count;
2075 for (rel = relocs; rel < rel_end; rel++)
2077 unsigned int r_type;
2078 unsigned int r_symndx;
2079 struct elf_link_hash_entry *h;
2080 struct elf_x86_link_hash_entry *eh;
2081 Elf_Internal_Sym *isym;
2083 bfd_boolean size_reloc;
2085 r_symndx = htab->r_sym (rel->r_info);
2086 r_type = ELF32_R_TYPE (rel->r_info);
2088 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
2090 /* xgettext:c-format */
2091 _bfd_error_handler (_("%B: bad symbol index: %d"),
2096 if (r_symndx < symtab_hdr->sh_info)
2098 /* A local symbol. */
2099 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2104 /* Check relocation against local STT_GNU_IFUNC symbol. */
2105 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
2107 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel,
2112 /* Fake a STT_GNU_IFUNC symbol. */
2113 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
2115 h->type = STT_GNU_IFUNC;
2118 h->forced_local = 1;
2119 h->root.type = bfd_link_hash_defined;
2127 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2128 while (h->root.type == bfd_link_hash_indirect
2129 || h->root.type == bfd_link_hash_warning)
2130 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2133 /* Check invalid x32 relocations. */
2134 if (!ABI_64_P (abfd))
2140 case R_X86_64_DTPOFF64:
2141 case R_X86_64_TPOFF64:
2143 case R_X86_64_GOTOFF64:
2144 case R_X86_64_GOT64:
2145 case R_X86_64_GOTPCREL64:
2146 case R_X86_64_GOTPC64:
2147 case R_X86_64_GOTPLT64:
2148 case R_X86_64_PLTOFF64:
2151 name = h->root.root.string;
2153 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2156 /* xgettext:c-format */
2157 (_("%B: relocation %s against symbol `%s' isn't "
2158 "supported in x32 mode"), abfd,
2159 x86_64_elf_howto_table[r_type].name, name);
2160 bfd_set_error (bfd_error_bad_value);
2168 /* It is referenced by a non-shared object. */
2170 h->root.non_ir_ref_regular = 1;
2172 if (h->type == STT_GNU_IFUNC)
2173 elf_tdata (info->output_bfd)->has_gnu_symbols
2174 |= elf_gnu_symbol_ifunc;
2177 if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
2178 symtab_hdr, sym_hashes,
2179 &r_type, GOT_UNKNOWN,
2180 rel, rel_end, h, r_symndx, FALSE))
2183 eh = (struct elf_x86_link_hash_entry *) h;
2186 case R_X86_64_TLSLD:
2187 htab->tls_ld_or_ldm_got.refcount += 1;
2190 case R_X86_64_TPOFF32:
2191 if (!bfd_link_executable (info) && ABI_64_P (abfd))
2192 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2193 &x86_64_elf_howto_table[r_type]);
2195 eh->has_got_reloc = 1;
2198 case R_X86_64_GOTTPOFF:
2199 if (!bfd_link_executable (info))
2200 info->flags |= DF_STATIC_TLS;
2203 case R_X86_64_GOT32:
2204 case R_X86_64_GOTPCREL:
2205 case R_X86_64_GOTPCRELX:
2206 case R_X86_64_REX_GOTPCRELX:
2207 case R_X86_64_TLSGD:
2208 case R_X86_64_GOT64:
2209 case R_X86_64_GOTPCREL64:
2210 case R_X86_64_GOTPLT64:
2211 case R_X86_64_GOTPC32_TLSDESC:
2212 case R_X86_64_TLSDESC_CALL:
2213 /* This symbol requires a global offset table entry. */
2215 int tls_type, old_tls_type;
2219 default: tls_type = GOT_NORMAL; break;
2220 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
2221 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
2222 case R_X86_64_GOTPC32_TLSDESC:
2223 case R_X86_64_TLSDESC_CALL:
2224 tls_type = GOT_TLS_GDESC; break;
2229 h->got.refcount += 1;
2230 old_tls_type = eh->tls_type;
2234 bfd_signed_vma *local_got_refcounts;
2236 /* This is a global offset table entry for a local symbol. */
2237 local_got_refcounts = elf_local_got_refcounts (abfd);
2238 if (local_got_refcounts == NULL)
2242 size = symtab_hdr->sh_info;
2243 size *= sizeof (bfd_signed_vma)
2244 + sizeof (bfd_vma) + sizeof (char);
2245 local_got_refcounts = ((bfd_signed_vma *)
2246 bfd_zalloc (abfd, size));
2247 if (local_got_refcounts == NULL)
2249 elf_local_got_refcounts (abfd) = local_got_refcounts;
2250 elf_x86_local_tlsdesc_gotent (abfd)
2251 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
2252 elf_x86_local_got_tls_type (abfd)
2253 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
2255 local_got_refcounts[r_symndx] += 1;
2257 = elf_x86_local_got_tls_type (abfd) [r_symndx];
2260 /* If a TLS symbol is accessed using IE at least once,
2261 there is no point to use dynamic model for it. */
2262 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2263 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2264 || tls_type != GOT_TLS_IE))
2266 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
2267 tls_type = old_tls_type;
2268 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2269 && GOT_TLS_GD_ANY_P (tls_type))
2270 tls_type |= old_tls_type;
2274 name = h->root.root.string;
2276 name = bfd_elf_sym_name (abfd, symtab_hdr,
2279 /* xgettext:c-format */
2280 (_("%B: '%s' accessed both as normal and"
2281 " thread local symbol"),
2283 bfd_set_error (bfd_error_bad_value);
2288 if (old_tls_type != tls_type)
2291 eh->tls_type = tls_type;
2293 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
2298 case R_X86_64_GOTOFF64:
2299 case R_X86_64_GOTPC32:
2300 case R_X86_64_GOTPC64:
2303 eh->has_got_reloc = 1;
2306 case R_X86_64_PLT32:
2307 case R_X86_64_PLT32_BND:
2308 /* This symbol requires a procedure linkage table entry. We
2309 actually build the entry in adjust_dynamic_symbol,
2310 because this might be a case of linking PIC code which is
2311 never referenced by a dynamic object, in which case we
2312 don't need to generate a procedure linkage table entry
2315 /* If this is a local symbol, we resolve it directly without
2316 creating a procedure linkage table entry. */
2320 eh->has_got_reloc = 1;
2322 h->plt.refcount += 1;
2325 case R_X86_64_PLTOFF64:
2326 /* This tries to form the 'address' of a function relative
2327 to GOT. For global symbols we need a PLT entry. */
2331 h->plt.refcount += 1;
2335 case R_X86_64_SIZE32:
2336 case R_X86_64_SIZE64:
2341 if (!ABI_64_P (abfd))
2347 /* Check relocation overflow as these relocs may lead to
2348 run-time relocation overflow. Don't error out for
2349 sections we don't care about, such as debug sections or
2350 when relocation overflow check is disabled. */
2351 if (!info->no_reloc_overflow_check
2352 && (bfd_link_pic (info)
2353 || (bfd_link_executable (info)
2357 && (sec->flags & SEC_READONLY) == 0)))
2358 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2359 &x86_64_elf_howto_table[r_type]);
2365 case R_X86_64_PC32_BND:
2369 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2370 eh->has_non_got_reloc = 1;
2371 /* We are called after all symbols have been resolved. Only
2372 relocation against STT_GNU_IFUNC symbol must go through
2375 && (bfd_link_executable (info)
2376 || h->type == STT_GNU_IFUNC))
2378 /* If this reloc is in a read-only section, we might
2379 need a copy reloc. We can't check reliably at this
2380 stage whether the section is read-only, as input
2381 sections have not yet been mapped to output sections.
2382 Tentatively set the flag for now, and correct in
2383 adjust_dynamic_symbol. */
2386 /* We may need a .plt entry if the symbol is a function
2387 defined in a shared lib or is a STT_GNU_IFUNC function
2388 referenced from the code or read-only section. */
2390 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2391 h->plt.refcount += 1;
2393 if (r_type == R_X86_64_PC32)
2395 /* Since something like ".long foo - ." may be used
2396 as pointer, make sure that PLT is used if foo is
2397 a function defined in a shared library. */
2398 if ((sec->flags & SEC_CODE) == 0)
2399 h->pointer_equality_needed = 1;
2401 else if (r_type != R_X86_64_PC32_BND
2402 && r_type != R_X86_64_PC64)
2404 h->pointer_equality_needed = 1;
2405 /* At run-time, R_X86_64_64 can be resolved for both
2406 x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2407 can only be resolved for x32. */
2408 if ((sec->flags & SEC_READONLY) == 0
2409 && (r_type == R_X86_64_64
2410 || (!ABI_64_P (abfd)
2411 && (r_type == R_X86_64_32
2412 || r_type == R_X86_64_32S))))
2413 eh->func_pointer_refcount += 1;
2419 /* If we are creating a shared library, and this is a reloc
2420 against a global symbol, or a non PC relative reloc
2421 against a local symbol, then we need to copy the reloc
2422 into the shared library. However, if we are linking with
2423 -Bsymbolic, we do not need to copy a reloc against a
2424 global symbol which is defined in an object we are
2425 including in the link (i.e., DEF_REGULAR is set). At
2426 this point we have not seen all the input files, so it is
2427 possible that DEF_REGULAR is not set now but will be set
2428 later (it is never cleared). In case of a weak definition,
2429 DEF_REGULAR may be cleared later by a strong definition in
2430 a shared library. We account for that possibility below by
2431 storing information in the relocs_copied field of the hash
2432 table entry. A similar situation occurs when creating
2433 shared libraries and symbol visibility changes render the
2436 If on the other hand, we are creating an executable, we
2437 may need to keep relocations for symbols satisfied by a
2438 dynamic library if we manage to avoid copy relocs for the
2441 Generate dynamic pointer relocation against STT_GNU_IFUNC
2442 symbol in the non-code section. */
2443 if ((bfd_link_pic (info)
2444 && (! IS_X86_64_PCREL_TYPE (r_type)
2446 && (! (bfd_link_pie (info)
2447 || SYMBOLIC_BIND (info, h))
2448 || h->root.type == bfd_link_hash_defweak
2449 || !h->def_regular))))
2451 && h->type == STT_GNU_IFUNC
2452 && r_type == htab->pointer_r_type
2453 && (sec->flags & SEC_CODE) == 0)
2454 || (ELIMINATE_COPY_RELOCS
2455 && !bfd_link_pic (info)
2457 && (h->root.type == bfd_link_hash_defweak
2458 || !h->def_regular)))
2460 struct elf_dyn_relocs *p;
2461 struct elf_dyn_relocs **head;
2463 /* We must copy these reloc types into the output file.
2464 Create a reloc section in dynobj and make room for
2468 sreloc = _bfd_elf_make_dynamic_reloc_section
2469 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
2470 abfd, /*rela?*/ TRUE);
2476 /* If this is a global symbol, we count the number of
2477 relocations we need for this symbol. */
2479 head = &eh->dyn_relocs;
2482 /* Track dynamic relocs needed for local syms too.
2483 We really need local syms available to do this
2488 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
2493 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2497 /* Beware of type punned pointers vs strict aliasing
2499 vpp = &(elf_section_data (s)->local_dynrel);
2500 head = (struct elf_dyn_relocs **)vpp;
2504 if (p == NULL || p->sec != sec)
2506 bfd_size_type amt = sizeof *p;
2508 p = ((struct elf_dyn_relocs *)
2509 bfd_alloc (htab->elf.dynobj, amt));
2520 /* Count size relocation as PC-relative relocation. */
2521 if (IS_X86_64_PCREL_TYPE (r_type) || size_reloc)
2526 /* This relocation describes the C++ object vtable hierarchy.
2527 Reconstruct it for later use during GC. */
2528 case R_X86_64_GNU_VTINHERIT:
2529 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2533 /* This relocation describes which C++ vtable entries are actually
2534 used. Record for later use during GC. */
2535 case R_X86_64_GNU_VTENTRY:
2536 BFD_ASSERT (h != NULL);
2538 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2546 if ((r_type == R_X86_64_GOTPCREL
2547 || r_type == R_X86_64_GOTPCRELX
2548 || r_type == R_X86_64_REX_GOTPCRELX)
2549 && (h == NULL || h->type != STT_GNU_IFUNC))
2550 sec->need_convert_load = 1;
2553 if (elf_section_data (sec)->this_hdr.contents != contents)
2555 if (!info->keep_memory)
2559 /* Cache the section contents for elf_link_input_bfd. */
2560 elf_section_data (sec)->this_hdr.contents = contents;
2567 if (elf_section_data (sec)->this_hdr.contents != contents)
2569 sec->check_relocs_failed = 1;
2573 /* Return the section that should be marked against GC for a given
2577 elf_x86_64_gc_mark_hook (asection *sec,
2578 struct bfd_link_info *info,
2579 Elf_Internal_Rela *rel,
2580 struct elf_link_hash_entry *h,
2581 Elf_Internal_Sym *sym)
2584 switch (ELF32_R_TYPE (rel->r_info))
2586 case R_X86_64_GNU_VTINHERIT:
2587 case R_X86_64_GNU_VTENTRY:
2591 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
2594 /* Adjust a symbol defined by a dynamic object and referenced by a
2595 regular object. The current definition is in some section of the
2596 dynamic object, but we're not including those sections. We have to
2597 change the definition to something the rest of the link can
2601 elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
2602 struct elf_link_hash_entry *h)
2604 struct elf_x86_link_hash_table *htab;
2606 struct elf_x86_link_hash_entry *eh;
2607 struct elf_dyn_relocs *p;
2609 /* STT_GNU_IFUNC symbol must go through PLT. */
2610 if (h->type == STT_GNU_IFUNC)
2612 /* All local STT_GNU_IFUNC references must be treate as local
2613 calls via local PLT. */
2615 && SYMBOL_CALLS_LOCAL (info, h))
2617 bfd_size_type pc_count = 0, count = 0;
2618 struct elf_dyn_relocs **pp;
2620 eh = (struct elf_x86_link_hash_entry *) h;
2621 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2623 pc_count += p->pc_count;
2624 p->count -= p->pc_count;
2633 if (pc_count || count)
2638 /* Increment PLT reference count only for PC-relative
2641 if (h->plt.refcount <= 0)
2642 h->plt.refcount = 1;
2644 h->plt.refcount += 1;
2649 if (h->plt.refcount <= 0)
2651 h->plt.offset = (bfd_vma) -1;
2657 /* If this is a function, put it in the procedure linkage table. We
2658 will fill in the contents of the procedure linkage table later,
2659 when we know the address of the .got section. */
2660 if (h->type == STT_FUNC
2663 if (h->plt.refcount <= 0
2664 || SYMBOL_CALLS_LOCAL (info, h)
2665 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2666 && h->root.type == bfd_link_hash_undefweak))
2668 /* This case can occur if we saw a PLT32 reloc in an input
2669 file, but the symbol was never referred to by a dynamic
2670 object, or if all references were garbage collected. In
2671 such a case, we don't actually need to build a procedure
2672 linkage table, and we can just do a PC32 reloc instead. */
2673 h->plt.offset = (bfd_vma) -1;
2680 /* It's possible that we incorrectly decided a .plt reloc was
2681 needed for an R_X86_64_PC32 reloc to a non-function sym in
2682 check_relocs. We can't decide accurately between function and
2683 non-function syms in check-relocs; Objects loaded later in
2684 the link may change h->type. So fix it now. */
2685 h->plt.offset = (bfd_vma) -1;
2687 eh = (struct elf_x86_link_hash_entry *) h;
2689 /* If this is a weak symbol, and there is a real definition, the
2690 processor independent code will have arranged for us to see the
2691 real definition first, and we can just use the same value. */
2692 if (h->u.weakdef != NULL)
2694 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2695 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2696 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2697 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2698 if (ELIMINATE_COPY_RELOCS
2699 || info->nocopyreloc
2700 || SYMBOL_NO_COPYRELOC (info, eh))
2702 h->non_got_ref = h->u.weakdef->non_got_ref;
2703 eh->needs_copy = h->u.weakdef->needs_copy;
2708 /* This is a reference to a symbol defined by a dynamic object which
2709 is not a function. */
2711 /* If we are creating a shared library, we must presume that the
2712 only references to the symbol are via the global offset table.
2713 For such cases we need not do anything here; the relocations will
2714 be handled correctly by relocate_section. */
2715 if (!bfd_link_executable (info))
2718 /* If there are no references to this symbol that do not use the
2719 GOT, we don't need to generate a copy reloc. */
2720 if (!h->non_got_ref)
2723 /* If -z nocopyreloc was given, we won't generate them either. */
2724 if (info->nocopyreloc || SYMBOL_NO_COPYRELOC (info, eh))
2730 if (ELIMINATE_COPY_RELOCS)
2732 eh = (struct elf_x86_link_hash_entry *) h;
2733 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2735 s = p->sec->output_section;
2736 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2740 /* If we didn't find any dynamic relocs in read-only sections, then
2741 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2749 /* We must allocate the symbol in our .dynbss section, which will
2750 become part of the .bss section of the executable. There will be
2751 an entry for this symbol in the .dynsym section. The dynamic
2752 object will contain position independent code, so all references
2753 from the dynamic object to this symbol will go through the global
2754 offset table. The dynamic linker will use the .dynsym entry to
2755 determine the address it must put in the global offset table, so
2756 both the dynamic object and the regular object will refer to the
2757 same memory location for the variable. */
2759 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2763 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
2764 to copy the initial value out of the dynamic object and into the
2765 runtime process image. */
2766 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2768 s = htab->elf.sdynrelro;
2769 srel = htab->elf.sreldynrelro;
2773 s = htab->elf.sdynbss;
2774 srel = htab->elf.srelbss;
2776 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2778 const struct elf_backend_data *bed;
2779 bed = get_elf_backend_data (info->output_bfd);
2780 srel->size += bed->s->sizeof_rela;
2784 return _bfd_elf_adjust_dynamic_copy (info, h, s);
2787 /* Allocate space in .plt, .got and associated reloc sections for
2791 elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2793 struct bfd_link_info *info;
2794 struct elf_x86_link_hash_table *htab;
2795 struct elf_x86_link_hash_entry *eh;
2796 struct elf_dyn_relocs *p;
2797 const struct elf_backend_data *bed;
2798 unsigned int plt_entry_size;
2799 bfd_boolean resolved_to_zero;
2800 const struct elf_x86_64_plt_layout *plt_layout;
2801 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt_layout;
2803 if (h->root.type == bfd_link_hash_indirect)
2806 eh = (struct elf_x86_link_hash_entry *) h;
2808 info = (struct bfd_link_info *) inf;
2809 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2812 bed = get_elf_backend_data (info->output_bfd);
2814 plt_layout = &elf_x86_64_plt (htab);
2815 non_lazy_plt_layout = elf_x86_64_non_lazy_plt (htab);
2816 plt_entry_size = plt_layout->plt_entry_size;
2818 resolved_to_zero = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
2823 /* We can't use the GOT PLT if pointer equality is needed since
2824 finish_dynamic_symbol won't clear symbol value and the dynamic
2825 linker won't update the GOT slot. We will get into an infinite
2826 loop at run-time. */
2827 if (htab->plt_got != NULL
2828 && h->type != STT_GNU_IFUNC
2829 && !h->pointer_equality_needed
2830 && h->plt.refcount > 0
2831 && h->got.refcount > 0)
2833 /* Don't use the regular PLT if there are both GOT and GOTPLT
2835 h->plt.offset = (bfd_vma) -1;
2837 /* Use the GOT PLT. */
2838 eh->plt_got.refcount = 1;
2841 /* Clear the reference count of function pointer relocations if
2842 symbol isn't a normal function. */
2843 if (h->type != STT_FUNC)
2844 eh->func_pointer_refcount = 0;
2846 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2847 here if it is defined and referenced in a non-shared object. */
2848 if (h->type == STT_GNU_IFUNC
2851 if (_bfd_elf_allocate_ifunc_dyn_relocs (info, h,
2853 &htab->readonly_dynrelocs_against_ifunc,
2855 (plt_layout->has_plt0
2857 GOT_ENTRY_SIZE, TRUE))
2859 asection *s = htab->plt_second;
2860 if (h->plt.offset != (bfd_vma) -1 && s != NULL)
2862 /* Use the second PLT section if it is created. */
2863 eh->plt_second.offset = s->size;
2865 /* Make room for this entry in the second PLT section. */
2866 s->size += non_lazy_plt_layout->plt_entry_size;
2874 /* Don't create the PLT entry if there are only function pointer
2875 relocations which can be resolved at run-time. */
2876 else if (htab->elf.dynamic_sections_created
2877 && (h->plt.refcount > eh->func_pointer_refcount
2878 || eh->plt_got.refcount > 0))
2880 bfd_boolean use_plt_got = eh->plt_got.refcount > 0;
2882 /* Clear the reference count of function pointer relocations
2884 eh->func_pointer_refcount = 0;
2886 /* Make sure this symbol is output as a dynamic symbol.
2887 Undefined weak syms won't yet be marked as dynamic. */
2888 if (h->dynindx == -1
2890 && !resolved_to_zero
2891 && h->root.type == bfd_link_hash_undefweak)
2893 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2897 if (bfd_link_pic (info)
2898 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2900 asection *s = htab->elf.splt;
2901 asection *second_s = htab->plt_second;
2902 asection *got_s = htab->plt_got;
2904 /* If this is the first .plt entry, make room for the special
2905 first entry. The .plt section is used by prelink to undo
2906 prelinking for dynamic relocations. */
2908 s->size = plt_layout->has_plt0 * plt_entry_size;
2911 eh->plt_got.offset = got_s->size;
2914 h->plt.offset = s->size;
2916 eh->plt_second.offset = second_s->size;
2919 /* If this symbol is not defined in a regular file, and we are
2920 not generating a shared library, then set the symbol to this
2921 location in the .plt. This is required to make function
2922 pointers compare as equal between the normal executable and
2923 the shared library. */
2924 if (! bfd_link_pic (info)
2929 /* We need to make a call to the entry of the GOT PLT
2930 instead of regular PLT entry. */
2931 h->root.u.def.section = got_s;
2932 h->root.u.def.value = eh->plt_got.offset;
2938 /* We need to make a call to the entry of the
2939 second PLT instead of regular PLT entry. */
2940 h->root.u.def.section = second_s;
2941 h->root.u.def.value = eh->plt_second.offset;
2945 h->root.u.def.section = s;
2946 h->root.u.def.value = h->plt.offset;
2951 /* Make room for this entry. */
2953 got_s->size += non_lazy_plt_layout->plt_entry_size;
2956 s->size += plt_entry_size;
2958 second_s->size += non_lazy_plt_layout->plt_entry_size;
2960 /* We also need to make an entry in the .got.plt section,
2961 which will be placed in the .got section by the linker
2963 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
2965 /* There should be no PLT relocation against resolved
2966 undefined weak symbol in executable. */
2967 if (!resolved_to_zero)
2969 /* We also need to make an entry in the .rela.plt
2971 htab->elf.srelplt->size += bed->s->sizeof_rela;
2972 htab->elf.srelplt->reloc_count++;
2978 eh->plt_got.offset = (bfd_vma) -1;
2979 h->plt.offset = (bfd_vma) -1;
2985 eh->plt_got.offset = (bfd_vma) -1;
2986 h->plt.offset = (bfd_vma) -1;
2990 eh->tlsdesc_got = (bfd_vma) -1;
2992 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
2993 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
2994 if (h->got.refcount > 0
2995 && bfd_link_executable (info)
2997 && elf_x86_hash_entry (h)->tls_type == GOT_TLS_IE)
2999 h->got.offset = (bfd_vma) -1;
3001 else if (h->got.refcount > 0)
3005 int tls_type = elf_x86_hash_entry (h)->tls_type;
3007 /* Make sure this symbol is output as a dynamic symbol.
3008 Undefined weak syms won't yet be marked as dynamic. */
3009 if (h->dynindx == -1
3011 && !resolved_to_zero
3012 && h->root.type == bfd_link_hash_undefweak)
3014 if (! bfd_elf_link_record_dynamic_symbol (info, h))
3018 if (GOT_TLS_GDESC_P (tls_type))
3020 eh->tlsdesc_got = htab->elf.sgotplt->size
3021 - elf_x86_64_compute_jump_table_size (htab);
3022 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
3023 h->got.offset = (bfd_vma) -2;
3025 if (! GOT_TLS_GDESC_P (tls_type)
3026 || GOT_TLS_GD_P (tls_type))
3029 h->got.offset = s->size;
3030 s->size += GOT_ENTRY_SIZE;
3031 if (GOT_TLS_GD_P (tls_type))
3032 s->size += GOT_ENTRY_SIZE;
3034 dyn = htab->elf.dynamic_sections_created;
3035 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
3036 and two if global. R_X86_64_GOTTPOFF needs one dynamic
3037 relocation. No dynamic relocation against resolved undefined
3038 weak symbol in executable. */
3039 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
3040 || tls_type == GOT_TLS_IE)
3041 htab->elf.srelgot->size += bed->s->sizeof_rela;
3042 else if (GOT_TLS_GD_P (tls_type))
3043 htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
3044 else if (! GOT_TLS_GDESC_P (tls_type)
3045 && ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3046 && !resolved_to_zero)
3047 || h->root.type != bfd_link_hash_undefweak)
3048 && (bfd_link_pic (info)
3049 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3050 htab->elf.srelgot->size += bed->s->sizeof_rela;
3051 if (GOT_TLS_GDESC_P (tls_type))
3053 htab->elf.srelplt->size += bed->s->sizeof_rela;
3054 htab->tlsdesc_plt = (bfd_vma) -1;
3058 h->got.offset = (bfd_vma) -1;
3060 if (eh->dyn_relocs == NULL)
3063 /* In the shared -Bsymbolic case, discard space allocated for
3064 dynamic pc-relative relocs against symbols which turn out to be
3065 defined in regular objects. For the normal shared case, discard
3066 space for pc-relative relocs that have become local due to symbol
3067 visibility changes. */
3069 if (bfd_link_pic (info))
3071 /* Relocs that use pc_count are those that appear on a call
3072 insn, or certain REL relocs that can generated via assembly.
3073 We want calls to protected symbols to resolve directly to the
3074 function rather than going via the plt. If people want
3075 function pointer comparisons to work as expected then they
3076 should avoid writing weird assembly. */
3077 if (SYMBOL_CALLS_LOCAL (info, h))
3079 struct elf_dyn_relocs **pp;
3081 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3083 p->count -= p->pc_count;
3092 /* Also discard relocs on undefined weak syms with non-default
3093 visibility or in PIE. */
3094 if (eh->dyn_relocs != NULL)
3096 if (h->root.type == bfd_link_hash_undefweak)
3098 /* Undefined weak symbol is never bound locally in shared
3100 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3101 || resolved_to_zero)
3102 eh->dyn_relocs = NULL;
3103 else if (h->dynindx == -1
3104 && ! h->forced_local
3105 && ! bfd_elf_link_record_dynamic_symbol (info, h))
3108 /* For PIE, discard space for pc-relative relocs against
3109 symbols which turn out to need copy relocs. */
3110 else if (bfd_link_executable (info)
3111 && (h->needs_copy || eh->needs_copy)
3115 struct elf_dyn_relocs **pp;
3117 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
3119 if (p->pc_count != 0)
3127 else if (ELIMINATE_COPY_RELOCS)
3129 /* For the non-shared case, discard space for relocs against
3130 symbols which turn out to need copy relocs or are not
3131 dynamic. Keep dynamic relocations for run-time function
3132 pointer initialization. */
3134 if ((!h->non_got_ref
3135 || eh->func_pointer_refcount > 0
3136 || (h->root.type == bfd_link_hash_undefweak
3137 && !resolved_to_zero))
3140 || (htab->elf.dynamic_sections_created
3141 && (h->root.type == bfd_link_hash_undefweak
3142 || h->root.type == bfd_link_hash_undefined))))
3144 /* Make sure this symbol is output as a dynamic symbol.
3145 Undefined weak syms won't yet be marked as dynamic. */
3146 if (h->dynindx == -1
3147 && ! h->forced_local
3148 && ! resolved_to_zero
3149 && h->root.type == bfd_link_hash_undefweak
3150 && ! bfd_elf_link_record_dynamic_symbol (info, h))
3153 /* If that succeeded, we know we'll be keeping all the
3155 if (h->dynindx != -1)
3159 eh->dyn_relocs = NULL;
3160 eh->func_pointer_refcount = 0;
3165 /* Finally, allocate space. */
3166 for (p = eh->dyn_relocs; p != NULL; p = p->next)
3170 sreloc = elf_section_data (p->sec)->sreloc;
3172 BFD_ASSERT (sreloc != NULL);
3174 sreloc->size += p->count * bed->s->sizeof_rela;
3180 /* Allocate space in .plt, .got and associated reloc sections for
3181 local dynamic relocs. */
3184 elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
3186 struct elf_link_hash_entry *h
3187 = (struct elf_link_hash_entry *) *slot;
3189 if (h->type != STT_GNU_IFUNC
3193 || h->root.type != bfd_link_hash_defined)
3196 return elf_x86_64_allocate_dynrelocs (h, inf);
3199 /* Convert load via the GOT slot to load immediate. */
3202 elf_x86_64_convert_load (bfd *abfd, asection *sec,
3203 struct bfd_link_info *link_info)
3205 Elf_Internal_Shdr *symtab_hdr;
3206 Elf_Internal_Rela *internal_relocs;
3207 Elf_Internal_Rela *irel, *irelend;
3209 struct elf_x86_link_hash_table *htab;
3210 bfd_boolean changed;
3211 bfd_signed_vma *local_got_refcounts;
3213 /* Don't even try to convert non-ELF outputs. */
3214 if (!is_elf_hash_table (link_info->hash))
3217 /* Nothing to do if there is no need or no output. */
3218 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC)
3219 || sec->need_convert_load == 0
3220 || bfd_is_abs_section (sec->output_section))
3223 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3225 /* Load the relocations for this section. */
3226 internal_relocs = (_bfd_elf_link_read_relocs
3227 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
3228 link_info->keep_memory));
3229 if (internal_relocs == NULL)
3233 htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
3234 local_got_refcounts = elf_local_got_refcounts (abfd);
3236 /* Get the section contents. */
3237 if (elf_section_data (sec)->this_hdr.contents != NULL)
3238 contents = elf_section_data (sec)->this_hdr.contents;
3241 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3245 irelend = internal_relocs + sec->reloc_count;
3246 for (irel = internal_relocs; irel < irelend; irel++)
3248 unsigned int r_type = ELF32_R_TYPE (irel->r_info);
3249 unsigned int r_symndx;
3250 struct elf_link_hash_entry *h;
3251 bfd_boolean converted;
3253 if (r_type != R_X86_64_GOTPCRELX
3254 && r_type != R_X86_64_REX_GOTPCRELX
3255 && r_type != R_X86_64_GOTPCREL)
3258 r_symndx = htab->r_sym (irel->r_info);
3259 if (r_symndx < symtab_hdr->sh_info)
3260 h = _bfd_elf_x86_get_local_sym_hash (htab, sec->owner,
3261 (const Elf_Internal_Rela *) irel,
3265 h = elf_sym_hashes (abfd)[r_symndx - symtab_hdr->sh_info];
3266 while (h->root.type == bfd_link_hash_indirect
3267 || h->root.type == bfd_link_hash_warning)
3268 h = (struct elf_link_hash_entry *) h->root.u.i.link;
3271 /* STT_GNU_IFUNC must keep GOTPCREL relocations. */
3272 if (h != NULL && h->type == STT_GNU_IFUNC)
3276 if (!elf_x86_64_convert_load_reloc (abfd, sec, contents, irel, h,
3277 &converted, link_info))
3282 changed = converted;
3285 if (h->got.refcount > 0)
3286 h->got.refcount -= 1;
3290 if (local_got_refcounts != NULL
3291 && local_got_refcounts[r_symndx] > 0)
3292 local_got_refcounts[r_symndx] -= 1;
3297 if (contents != NULL
3298 && elf_section_data (sec)->this_hdr.contents != contents)
3300 if (!changed && !link_info->keep_memory)
3304 /* Cache the section contents for elf_link_input_bfd. */
3305 elf_section_data (sec)->this_hdr.contents = contents;
3309 if (elf_section_data (sec)->relocs != internal_relocs)
3312 free (internal_relocs);
3314 elf_section_data (sec)->relocs = internal_relocs;
3320 if (contents != NULL
3321 && elf_section_data (sec)->this_hdr.contents != contents)
3323 if (internal_relocs != NULL
3324 && elf_section_data (sec)->relocs != internal_relocs)
3325 free (internal_relocs);
3329 /* Set the sizes of the dynamic sections. */
3332 elf_x86_64_size_dynamic_sections (bfd *output_bfd,
3333 struct bfd_link_info *info)
3335 struct elf_x86_link_hash_table *htab;
3340 const struct elf_backend_data *bed;
3341 const struct elf_x86_64_plt_layout *plt_layout;
3342 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt_layout;
3344 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
3347 bed = get_elf_backend_data (output_bfd);
3349 dynobj = htab->elf.dynobj;
3353 plt_layout = &elf_x86_64_plt (htab);
3354 non_lazy_plt_layout = elf_x86_64_non_lazy_plt (htab);
3356 /* Set up .got offsets for local syms, and space for local dynamic
3358 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
3360 bfd_signed_vma *local_got;
3361 bfd_signed_vma *end_local_got;
3362 char *local_tls_type;
3363 bfd_vma *local_tlsdesc_gotent;
3364 bfd_size_type locsymcount;
3365 Elf_Internal_Shdr *symtab_hdr;
3368 if (! is_x86_64_elf (ibfd))
3371 for (s = ibfd->sections; s != NULL; s = s->next)
3373 struct elf_dyn_relocs *p;
3375 if (!elf_x86_64_convert_load (ibfd, s, info))
3378 for (p = (struct elf_dyn_relocs *)
3379 (elf_section_data (s)->local_dynrel);
3383 if (!bfd_is_abs_section (p->sec)
3384 && bfd_is_abs_section (p->sec->output_section))
3386 /* Input section has been discarded, either because
3387 it is a copy of a linkonce section or due to
3388 linker script /DISCARD/, so we'll be discarding
3391 else if (p->count != 0)
3393 srel = elf_section_data (p->sec)->sreloc;
3394 srel->size += p->count * bed->s->sizeof_rela;
3395 if ((p->sec->output_section->flags & SEC_READONLY) != 0
3396 && (info->flags & DF_TEXTREL) == 0)
3398 info->flags |= DF_TEXTREL;
3399 if ((info->warn_shared_textrel && bfd_link_pic (info))
3400 || info->error_textrel)
3401 /* xgettext:c-format */
3402 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'\n"),
3403 p->sec->owner, p->sec);
3409 local_got = elf_local_got_refcounts (ibfd);
3413 symtab_hdr = &elf_symtab_hdr (ibfd);
3414 locsymcount = symtab_hdr->sh_info;
3415 end_local_got = local_got + locsymcount;
3416 local_tls_type = elf_x86_local_got_tls_type (ibfd);
3417 local_tlsdesc_gotent = elf_x86_local_tlsdesc_gotent (ibfd);
3419 srel = htab->elf.srelgot;
3420 for (; local_got < end_local_got;
3421 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
3423 *local_tlsdesc_gotent = (bfd_vma) -1;
3426 if (GOT_TLS_GDESC_P (*local_tls_type))
3428 *local_tlsdesc_gotent = htab->elf.sgotplt->size
3429 - elf_x86_64_compute_jump_table_size (htab);
3430 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
3431 *local_got = (bfd_vma) -2;
3433 if (! GOT_TLS_GDESC_P (*local_tls_type)
3434 || GOT_TLS_GD_P (*local_tls_type))
3436 *local_got = s->size;
3437 s->size += GOT_ENTRY_SIZE;
3438 if (GOT_TLS_GD_P (*local_tls_type))
3439 s->size += GOT_ENTRY_SIZE;
3441 if (bfd_link_pic (info)
3442 || GOT_TLS_GD_ANY_P (*local_tls_type)
3443 || *local_tls_type == GOT_TLS_IE)
3445 if (GOT_TLS_GDESC_P (*local_tls_type))
3447 htab->elf.srelplt->size
3448 += bed->s->sizeof_rela;
3449 htab->tlsdesc_plt = (bfd_vma) -1;
3451 if (! GOT_TLS_GDESC_P (*local_tls_type)
3452 || GOT_TLS_GD_P (*local_tls_type))
3453 srel->size += bed->s->sizeof_rela;
3457 *local_got = (bfd_vma) -1;
3461 if (htab->tls_ld_or_ldm_got.refcount > 0)
3463 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
3465 htab->tls_ld_or_ldm_got.offset = htab->elf.sgot->size;
3466 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
3467 htab->elf.srelgot->size += bed->s->sizeof_rela;
3470 htab->tls_ld_or_ldm_got.offset = -1;
3472 /* Allocate global sym .plt and .got entries, and space for global
3473 sym dynamic relocs. */
3474 elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
3477 /* Allocate .plt and .got entries, and space for local symbols. */
3478 htab_traverse (htab->loc_hash_table,
3479 elf_x86_64_allocate_local_dynrelocs,
3482 /* For every jump slot reserved in the sgotplt, reloc_count is
3483 incremented. However, when we reserve space for TLS descriptors,
3484 it's not incremented, so in order to compute the space reserved
3485 for them, it suffices to multiply the reloc count by the jump
3488 PR ld/13302: We start next_irelative_index at the end of .rela.plt
3489 so that R_X86_64_IRELATIVE entries come last. */
3490 if (htab->elf.srelplt)
3492 htab->sgotplt_jump_table_size
3493 = elf_x86_64_compute_jump_table_size (htab);
3494 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1;
3496 else if (htab->elf.irelplt)
3497 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1;
3499 if (htab->tlsdesc_plt)
3501 /* If we're not using lazy TLS relocations, don't generate the
3502 PLT and GOT entries they require. */
3503 if ((info->flags & DF_BIND_NOW))
3504 htab->tlsdesc_plt = 0;
3507 htab->tlsdesc_got = htab->elf.sgot->size;
3508 htab->elf.sgot->size += GOT_ENTRY_SIZE;
3509 /* Reserve room for the initial entry.
3510 FIXME: we could probably do away with it in this case. */
3511 if (htab->elf.splt->size == 0)
3512 htab->elf.splt->size = plt_layout->plt_entry_size;
3513 htab->tlsdesc_plt = htab->elf.splt->size;
3514 htab->elf.splt->size += plt_layout->plt_entry_size;
3518 if (htab->elf.sgotplt)
3520 /* Don't allocate .got.plt section if there are no GOT nor PLT
3521 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
3522 if ((htab->elf.hgot == NULL
3523 || !htab->elf.hgot->ref_regular_nonweak)
3524 && (htab->elf.sgotplt->size
3525 == get_elf_backend_data (output_bfd)->got_header_size)
3526 && (htab->elf.splt == NULL
3527 || htab->elf.splt->size == 0)
3528 && (htab->elf.sgot == NULL
3529 || htab->elf.sgot->size == 0)
3530 && (htab->elf.iplt == NULL
3531 || htab->elf.iplt->size == 0)
3532 && (htab->elf.igotplt == NULL
3533 || htab->elf.igotplt->size == 0))
3534 htab->elf.sgotplt->size = 0;
3537 if (_bfd_elf_eh_frame_present (info))
3539 if (htab->plt_eh_frame != NULL
3540 && htab->elf.splt != NULL
3541 && htab->elf.splt->size != 0
3542 && !bfd_is_abs_section (htab->elf.splt->output_section))
3543 htab->plt_eh_frame->size = plt_layout->eh_frame_plt_size;
3545 if (htab->plt_got_eh_frame != NULL
3546 && htab->plt_got != NULL
3547 && htab->plt_got->size != 0
3548 && !bfd_is_abs_section (htab->plt_got->output_section))
3549 htab->plt_got_eh_frame->size
3550 = non_lazy_plt_layout->eh_frame_plt_size;
3552 /* Unwind info for the second PLT and .plt.got sections are
3554 if (htab->plt_second_eh_frame != NULL
3555 && htab->plt_second != NULL
3556 && htab->plt_second->size != 0
3557 && !bfd_is_abs_section (htab->plt_second->output_section))
3558 htab->plt_second_eh_frame->size
3559 = non_lazy_plt_layout->eh_frame_plt_size;
3562 /* We now have determined the sizes of the various dynamic sections.
3563 Allocate memory for them. */
3565 for (s = dynobj->sections; s != NULL; s = s->next)
3567 if ((s->flags & SEC_LINKER_CREATED) == 0)
3570 if (s == htab->elf.splt
3571 || s == htab->elf.sgot
3572 || s == htab->elf.sgotplt
3573 || s == htab->elf.iplt
3574 || s == htab->elf.igotplt
3575 || s == htab->plt_second
3576 || s == htab->plt_got
3577 || s == htab->plt_eh_frame
3578 || s == htab->plt_got_eh_frame
3579 || s == htab->plt_second_eh_frame
3580 || s == htab->elf.sdynbss
3581 || s == htab->elf.sdynrelro)
3583 /* Strip this section if we don't need it; see the
3586 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
3588 if (s->size != 0 && s != htab->elf.srelplt)
3591 /* We use the reloc_count field as a counter if we need
3592 to copy relocs into the output file. */
3593 if (s != htab->elf.srelplt)
3598 /* It's not one of our sections, so don't allocate space. */
3604 /* If we don't need this section, strip it from the
3605 output file. This is mostly to handle .rela.bss and
3606 .rela.plt. We must create both sections in
3607 create_dynamic_sections, because they must be created
3608 before the linker maps input sections to output
3609 sections. The linker does that before
3610 adjust_dynamic_symbol is called, and it is that
3611 function which decides whether anything needs to go
3612 into these sections. */
3614 s->flags |= SEC_EXCLUDE;
3618 if ((s->flags & SEC_HAS_CONTENTS) == 0)
3621 /* Allocate memory for the section contents. We use bfd_zalloc
3622 here in case unused entries are not reclaimed before the
3623 section's contents are written out. This should not happen,
3624 but this way if it does, we get a R_X86_64_NONE reloc instead
3626 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3627 if (s->contents == NULL)
3631 if (htab->plt_eh_frame != NULL
3632 && htab->plt_eh_frame->contents != NULL)
3634 memcpy (htab->plt_eh_frame->contents,
3635 plt_layout->eh_frame_plt, htab->plt_eh_frame->size);
3636 bfd_put_32 (dynobj, htab->elf.splt->size,
3637 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET);
3640 if (htab->plt_got_eh_frame != NULL
3641 && htab->plt_got_eh_frame->contents != NULL)
3643 memcpy (htab->plt_got_eh_frame->contents,
3644 non_lazy_plt_layout->eh_frame_plt,
3645 htab->plt_got_eh_frame->size);
3646 bfd_put_32 (dynobj, htab->plt_got->size,
3647 (htab->plt_got_eh_frame->contents
3648 + PLT_FDE_LEN_OFFSET));
3651 if (htab->plt_second_eh_frame != NULL
3652 && htab->plt_second_eh_frame->contents != NULL)
3654 memcpy (htab->plt_second_eh_frame->contents,
3655 non_lazy_plt_layout->eh_frame_plt,
3656 htab->plt_second_eh_frame->size);
3657 bfd_put_32 (dynobj, htab->plt_second->size,
3658 (htab->plt_second_eh_frame->contents
3659 + PLT_FDE_LEN_OFFSET));
3662 if (htab->elf.dynamic_sections_created)
3664 /* Add some entries to the .dynamic section. We fill in the
3665 values later, in elf_x86_64_finish_dynamic_sections, but we
3666 must add the entries now so that we get the correct size for
3667 the .dynamic section. The DT_DEBUG entry is filled in by the
3668 dynamic linker and used by the debugger. */
3669 #define add_dynamic_entry(TAG, VAL) \
3670 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
3672 if (bfd_link_executable (info))
3674 if (!add_dynamic_entry (DT_DEBUG, 0))
3678 if (htab->elf.splt->size != 0)
3680 /* DT_PLTGOT is used by prelink even if there is no PLT
3682 if (!add_dynamic_entry (DT_PLTGOT, 0))
3686 if (htab->elf.srelplt->size != 0)
3688 if (!add_dynamic_entry (DT_PLTRELSZ, 0)
3689 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
3690 || !add_dynamic_entry (DT_JMPREL, 0))
3694 if (htab->tlsdesc_plt
3695 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
3696 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
3701 if (!add_dynamic_entry (DT_RELA, 0)
3702 || !add_dynamic_entry (DT_RELASZ, 0)
3703 || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
3706 /* If any dynamic relocs apply to a read-only section,
3707 then we need a DT_TEXTREL entry. */
3708 if ((info->flags & DF_TEXTREL) == 0)
3709 elf_link_hash_traverse (&htab->elf,
3710 _bfd_x86_elf_readonly_dynrelocs,
3713 if ((info->flags & DF_TEXTREL) != 0)
3715 if (htab->readonly_dynrelocs_against_ifunc)
3717 info->callbacks->einfo
3718 (_("%P%X: read-only segment has dynamic IFUNC relocations; recompile with -fPIC\n"));
3719 bfd_set_error (bfd_error_bad_value);
3723 if (!add_dynamic_entry (DT_TEXTREL, 0))
3728 #undef add_dynamic_entry
3733 /* Return the relocation value for @tpoff relocation
3734 if STT_TLS virtual address is ADDRESS. */
3737 elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
3739 struct elf_link_hash_table *htab = elf_hash_table (info);
3740 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
3741 bfd_vma static_tls_size;
3743 /* If tls_segment is NULL, we should have signalled an error already. */
3744 if (htab->tls_sec == NULL)
3747 /* Consider special static TLS alignment requirements. */
3748 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
3749 return address - static_tls_size - htab->tls_sec->vma;
3752 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
3756 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
3758 /* Opcode Instruction
3761 0x0f 0x8x conditional jump */
3763 && (contents [offset - 1] == 0xe8
3764 || contents [offset - 1] == 0xe9))
3766 && contents [offset - 2] == 0x0f
3767 && (contents [offset - 1] & 0xf0) == 0x80));
3770 /* Relocate an x86_64 ELF section. */
3773 elf_x86_64_relocate_section (bfd *output_bfd,
3774 struct bfd_link_info *info,
3776 asection *input_section,
3778 Elf_Internal_Rela *relocs,
3779 Elf_Internal_Sym *local_syms,
3780 asection **local_sections)
3782 struct elf_x86_link_hash_table *htab;
3783 Elf_Internal_Shdr *symtab_hdr;
3784 struct elf_link_hash_entry **sym_hashes;
3785 bfd_vma *local_got_offsets;
3786 bfd_vma *local_tlsdesc_gotents;
3787 Elf_Internal_Rela *rel;
3788 Elf_Internal_Rela *wrel;
3789 Elf_Internal_Rela *relend;
3790 unsigned int plt_entry_size;
3792 BFD_ASSERT (is_x86_64_elf (input_bfd));
3794 /* Skip if check_relocs failed. */
3795 if (input_section->check_relocs_failed)
3798 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
3801 plt_entry_size = elf_x86_64_plt (htab).plt_entry_size;
3802 symtab_hdr = &elf_symtab_hdr (input_bfd);
3803 sym_hashes = elf_sym_hashes (input_bfd);
3804 local_got_offsets = elf_local_got_offsets (input_bfd);
3805 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
3807 _bfd_x86_elf_set_tls_module_base (info);
3809 rel = wrel = relocs;
3810 relend = relocs + input_section->reloc_count;
3811 for (; rel < relend; wrel++, rel++)
3813 unsigned int r_type;
3814 reloc_howto_type *howto;
3815 unsigned long r_symndx;
3816 struct elf_link_hash_entry *h;
3817 struct elf_x86_link_hash_entry *eh;
3818 Elf_Internal_Sym *sym;
3820 bfd_vma off, offplt, plt_offset;
3822 bfd_boolean unresolved_reloc;
3823 bfd_reloc_status_type r;
3825 asection *base_got, *resolved_plt;
3827 bfd_boolean resolved_to_zero;
3828 bfd_boolean relative_reloc;
3830 r_type = ELF32_R_TYPE (rel->r_info);
3831 if (r_type == (int) R_X86_64_GNU_VTINHERIT
3832 || r_type == (int) R_X86_64_GNU_VTENTRY)
3839 if (r_type >= (int) R_X86_64_standard)
3840 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
3842 if (r_type != (int) R_X86_64_32
3843 || ABI_64_P (output_bfd))
3844 howto = x86_64_elf_howto_table + r_type;
3846 howto = (x86_64_elf_howto_table
3847 + ARRAY_SIZE (x86_64_elf_howto_table) - 1);
3848 r_symndx = htab->r_sym (rel->r_info);
3852 unresolved_reloc = FALSE;
3853 if (r_symndx < symtab_hdr->sh_info)
3855 sym = local_syms + r_symndx;
3856 sec = local_sections[r_symndx];
3858 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
3860 st_size = sym->st_size;
3862 /* Relocate against local STT_GNU_IFUNC symbol. */
3863 if (!bfd_link_relocatable (info)
3864 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
3866 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd,
3871 /* Set STT_GNU_IFUNC symbol value. */
3872 h->root.u.def.value = sym->st_value;
3873 h->root.u.def.section = sec;
3878 bfd_boolean warned ATTRIBUTE_UNUSED;
3879 bfd_boolean ignored ATTRIBUTE_UNUSED;
3881 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
3882 r_symndx, symtab_hdr, sym_hashes,
3884 unresolved_reloc, warned, ignored);
3888 if (sec != NULL && discarded_section (sec))
3890 _bfd_clear_contents (howto, input_bfd, input_section,
3891 contents + rel->r_offset);
3892 wrel->r_offset = rel->r_offset;
3896 /* For ld -r, remove relocations in debug sections against
3897 sections defined in discarded sections. Not done for
3898 eh_frame editing code expects to be present. */
3899 if (bfd_link_relocatable (info)
3900 && (input_section->flags & SEC_DEBUGGING))
3906 if (bfd_link_relocatable (info))
3913 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
3915 if (r_type == R_X86_64_64)
3917 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
3918 zero-extend it to 64bit if addend is zero. */
3919 r_type = R_X86_64_32;
3920 memset (contents + rel->r_offset + 4, 0, 4);
3922 else if (r_type == R_X86_64_SIZE64)
3924 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
3925 zero-extend it to 64bit if addend is zero. */
3926 r_type = R_X86_64_SIZE32;
3927 memset (contents + rel->r_offset + 4, 0, 4);
3931 eh = (struct elf_x86_link_hash_entry *) h;
3933 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3934 it here if it is defined in a non-shared object. */
3936 && h->type == STT_GNU_IFUNC
3942 if ((input_section->flags & SEC_ALLOC) == 0)
3944 /* Dynamic relocs are not propagated for SEC_DEBUGGING
3945 sections because such sections are not SEC_ALLOC and
3946 thus ld.so will not process them. */
3947 if ((input_section->flags & SEC_DEBUGGING) != 0)
3957 case R_X86_64_GOTPCREL:
3958 case R_X86_64_GOTPCRELX:
3959 case R_X86_64_REX_GOTPCRELX:
3960 case R_X86_64_GOTPCREL64:
3961 base_got = htab->elf.sgot;
3962 off = h->got.offset;
3964 if (base_got == NULL)
3967 if (off == (bfd_vma) -1)
3969 /* We can't use h->got.offset here to save state, or
3970 even just remember the offset, as finish_dynamic_symbol
3971 would use that as offset into .got. */
3973 if (h->plt.offset == (bfd_vma) -1)
3976 if (htab->elf.splt != NULL)
3978 plt_index = (h->plt.offset / plt_entry_size
3979 - elf_x86_64_plt (htab).has_plt0);
3980 off = (plt_index + 3) * GOT_ENTRY_SIZE;
3981 base_got = htab->elf.sgotplt;
3985 plt_index = h->plt.offset / plt_entry_size;
3986 off = plt_index * GOT_ENTRY_SIZE;
3987 base_got = htab->elf.igotplt;
3990 if (h->dynindx == -1
3994 /* This references the local defitionion. We must
3995 initialize this entry in the global offset table.
3996 Since the offset must always be a multiple of 8,
3997 we use the least significant bit to record
3998 whether we have initialized it already.
4000 When doing a dynamic link, we create a .rela.got
4001 relocation entry to initialize the value. This
4002 is done in the finish_dynamic_symbol routine. */
4007 bfd_put_64 (output_bfd, relocation,
4008 base_got->contents + off);
4009 /* Note that this is harmless for the GOTPLT64
4010 case, as -1 | 1 still is -1. */
4016 relocation = (base_got->output_section->vma
4017 + base_got->output_offset + off);
4022 if (h->plt.offset == (bfd_vma) -1)
4024 /* Handle static pointers of STT_GNU_IFUNC symbols. */
4025 if (r_type == htab->pointer_r_type
4026 && (input_section->flags & SEC_CODE) == 0)
4027 goto do_ifunc_pointer;
4028 goto bad_ifunc_reloc;
4031 /* STT_GNU_IFUNC symbol must go through PLT. */
4032 if (htab->elf.splt != NULL)
4034 if (htab->plt_second != NULL)
4036 resolved_plt = htab->plt_second;
4037 plt_offset = eh->plt_second.offset;
4041 resolved_plt = htab->elf.splt;
4042 plt_offset = h->plt.offset;
4047 resolved_plt = htab->elf.iplt;
4048 plt_offset = h->plt.offset;
4051 relocation = (resolved_plt->output_section->vma
4052 + resolved_plt->output_offset + plt_offset);
4058 if (h->root.root.string)
4059 name = h->root.root.string;
4061 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
4064 /* xgettext:c-format */
4065 (_("%B: relocation %s against STT_GNU_IFUNC "
4066 "symbol `%s' isn't supported"), input_bfd,
4068 bfd_set_error (bfd_error_bad_value);
4072 if (bfd_link_pic (info))
4077 if (ABI_64_P (output_bfd))
4082 if (rel->r_addend != 0)
4084 if (h->root.root.string)
4085 name = h->root.root.string;
4087 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4090 /* xgettext:c-format */
4091 (_("%B: relocation %s against STT_GNU_IFUNC "
4092 "symbol `%s' has non-zero addend: %Ld"),
4093 input_bfd, howto->name, name, rel->r_addend);
4094 bfd_set_error (bfd_error_bad_value);
4098 /* Generate dynamic relcoation only when there is a
4099 non-GOT reference in a shared object or there is no
4101 if ((bfd_link_pic (info) && h->non_got_ref)
4102 || h->plt.offset == (bfd_vma) -1)
4104 Elf_Internal_Rela outrel;
4107 /* Need a dynamic relocation to get the real function
4109 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
4113 if (outrel.r_offset == (bfd_vma) -1
4114 || outrel.r_offset == (bfd_vma) -2)
4117 outrel.r_offset += (input_section->output_section->vma
4118 + input_section->output_offset);
4120 if (h->dynindx == -1
4122 || bfd_link_executable (info))
4124 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
4125 h->root.root.string,
4126 h->root.u.def.section->owner);
4128 /* This symbol is resolved locally. */
4129 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4130 outrel.r_addend = (h->root.u.def.value
4131 + h->root.u.def.section->output_section->vma
4132 + h->root.u.def.section->output_offset);
4136 outrel.r_info = htab->r_info (h->dynindx, r_type);
4137 outrel.r_addend = 0;
4140 /* Dynamic relocations are stored in
4141 1. .rela.ifunc section in PIC object.
4142 2. .rela.got section in dynamic executable.
4143 3. .rela.iplt section in static executable. */
4144 if (bfd_link_pic (info))
4145 sreloc = htab->elf.irelifunc;
4146 else if (htab->elf.splt != NULL)
4147 sreloc = htab->elf.srelgot;
4149 sreloc = htab->elf.irelplt;
4150 elf_append_rela (output_bfd, sreloc, &outrel);
4152 /* If this reloc is against an external symbol, we
4153 do not want to fiddle with the addend. Otherwise,
4154 we need to include the symbol value so that it
4155 becomes an addend for the dynamic reloc. For an
4156 internal symbol, we have updated addend. */
4161 case R_X86_64_PC32_BND:
4163 case R_X86_64_PLT32:
4164 case R_X86_64_PLT32_BND:
4169 resolved_to_zero = (eh != NULL
4170 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
4175 /* When generating a shared object, the relocations handled here are
4176 copied into the output file to be resolved at run time. */
4179 case R_X86_64_GOT32:
4180 case R_X86_64_GOT64:
4181 /* Relocation is to the entry for this symbol in the global
4183 case R_X86_64_GOTPCREL:
4184 case R_X86_64_GOTPCRELX:
4185 case R_X86_64_REX_GOTPCRELX:
4186 case R_X86_64_GOTPCREL64:
4187 /* Use global offset table entry as symbol value. */
4188 case R_X86_64_GOTPLT64:
4189 /* This is obsolete and treated the same as GOT64. */
4190 base_got = htab->elf.sgot;
4192 if (htab->elf.sgot == NULL)
4195 relative_reloc = FALSE;
4200 off = h->got.offset;
4202 && h->plt.offset != (bfd_vma)-1
4203 && off == (bfd_vma)-1)
4205 /* We can't use h->got.offset here to save
4206 state, or even just remember the offset, as
4207 finish_dynamic_symbol would use that as offset into
4209 bfd_vma plt_index = (h->plt.offset / plt_entry_size
4210 - elf_x86_64_plt (htab).has_plt0);
4211 off = (plt_index + 3) * GOT_ENTRY_SIZE;
4212 base_got = htab->elf.sgotplt;
4215 dyn = htab->elf.dynamic_sections_created;
4217 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
4218 || (bfd_link_pic (info)
4219 && SYMBOL_REFERENCES_LOCAL (info, h))
4220 || (ELF_ST_VISIBILITY (h->other)
4221 && h->root.type == bfd_link_hash_undefweak))
4223 /* This is actually a static link, or it is a -Bsymbolic
4224 link and the symbol is defined locally, or the symbol
4225 was forced to be local because of a version file. We
4226 must initialize this entry in the global offset table.
4227 Since the offset must always be a multiple of 8, we
4228 use the least significant bit to record whether we
4229 have initialized it already.
4231 When doing a dynamic link, we create a .rela.got
4232 relocation entry to initialize the value. This is
4233 done in the finish_dynamic_symbol routine. */
4238 bfd_put_64 (output_bfd, relocation,
4239 base_got->contents + off);
4240 /* Note that this is harmless for the GOTPLT64 case,
4241 as -1 | 1 still is -1. */
4244 if (h->dynindx == -1
4246 && h->root.type != bfd_link_hash_undefweak
4247 && bfd_link_pic (info))
4249 /* If this symbol isn't dynamic in PIC,
4250 generate R_X86_64_RELATIVE here. */
4251 eh->no_finish_dynamic_symbol = 1;
4252 relative_reloc = TRUE;
4257 unresolved_reloc = FALSE;
4261 if (local_got_offsets == NULL)
4264 off = local_got_offsets[r_symndx];
4266 /* The offset must always be a multiple of 8. We use
4267 the least significant bit to record whether we have
4268 already generated the necessary reloc. */
4273 bfd_put_64 (output_bfd, relocation,
4274 base_got->contents + off);
4275 local_got_offsets[r_symndx] |= 1;
4277 if (bfd_link_pic (info))
4278 relative_reloc = TRUE;
4285 Elf_Internal_Rela outrel;
4287 /* We need to generate a R_X86_64_RELATIVE reloc
4288 for the dynamic linker. */
4289 s = htab->elf.srelgot;
4293 outrel.r_offset = (base_got->output_section->vma
4294 + base_got->output_offset
4296 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4297 outrel.r_addend = relocation;
4298 elf_append_rela (output_bfd, s, &outrel);
4301 if (off >= (bfd_vma) -2)
4304 relocation = base_got->output_section->vma
4305 + base_got->output_offset + off;
4306 if (r_type != R_X86_64_GOTPCREL
4307 && r_type != R_X86_64_GOTPCRELX
4308 && r_type != R_X86_64_REX_GOTPCRELX
4309 && r_type != R_X86_64_GOTPCREL64)
4310 relocation -= htab->elf.sgotplt->output_section->vma
4311 - htab->elf.sgotplt->output_offset;
4315 case R_X86_64_GOTOFF64:
4316 /* Relocation is relative to the start of the global offset
4319 /* Check to make sure it isn't a protected function or data
4320 symbol for shared library since it may not be local when
4321 used as function address or with copy relocation. We also
4322 need to make sure that a symbol is referenced locally. */
4323 if (bfd_link_pic (info) && h)
4325 if (!h->def_regular)
4329 switch (ELF_ST_VISIBILITY (h->other))
4332 v = _("hidden symbol");
4335 v = _("internal symbol");
4338 v = _("protected symbol");
4346 /* xgettext:c-format */
4347 (_("%B: relocation R_X86_64_GOTOFF64 against undefined %s"
4348 " `%s' can not be used when making a shared object"),
4349 input_bfd, v, h->root.root.string);
4350 bfd_set_error (bfd_error_bad_value);
4353 else if (!bfd_link_executable (info)
4354 && !SYMBOL_REFERENCES_LOCAL (info, h)
4355 && (h->type == STT_FUNC
4356 || h->type == STT_OBJECT)
4357 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
4360 /* xgettext:c-format */
4361 (_("%B: relocation R_X86_64_GOTOFF64 against protected %s"
4362 " `%s' can not be used when making a shared object"),
4364 h->type == STT_FUNC ? "function" : "data",
4365 h->root.root.string);
4366 bfd_set_error (bfd_error_bad_value);
4371 /* Note that sgot is not involved in this
4372 calculation. We always want the start of .got.plt. If we
4373 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
4374 permitted by the ABI, we might have to change this
4376 relocation -= htab->elf.sgotplt->output_section->vma
4377 + htab->elf.sgotplt->output_offset;
4380 case R_X86_64_GOTPC32:
4381 case R_X86_64_GOTPC64:
4382 /* Use global offset table as symbol value. */
4383 relocation = htab->elf.sgotplt->output_section->vma
4384 + htab->elf.sgotplt->output_offset;
4385 unresolved_reloc = FALSE;
4388 case R_X86_64_PLTOFF64:
4389 /* Relocation is PLT entry relative to GOT. For local
4390 symbols it's the symbol itself relative to GOT. */
4392 /* See PLT32 handling. */
4393 && (h->plt.offset != (bfd_vma) -1
4394 || eh->plt_got.offset != (bfd_vma) -1)
4395 && htab->elf.splt != NULL)
4397 if (eh->plt_got.offset != (bfd_vma) -1)
4399 /* Use the GOT PLT. */
4400 resolved_plt = htab->plt_got;
4401 plt_offset = eh->plt_got.offset;
4403 else if (htab->plt_second != NULL)
4405 resolved_plt = htab->plt_second;
4406 plt_offset = eh->plt_second.offset;
4410 resolved_plt = htab->elf.splt;
4411 plt_offset = h->plt.offset;
4414 relocation = (resolved_plt->output_section->vma
4415 + resolved_plt->output_offset
4417 unresolved_reloc = FALSE;
4420 relocation -= htab->elf.sgotplt->output_section->vma
4421 + htab->elf.sgotplt->output_offset;
4424 case R_X86_64_PLT32:
4425 case R_X86_64_PLT32_BND:
4426 /* Relocation is to the entry for this symbol in the
4427 procedure linkage table. */
4429 /* Resolve a PLT32 reloc against a local symbol directly,
4430 without using the procedure linkage table. */
4434 if ((h->plt.offset == (bfd_vma) -1
4435 && eh->plt_got.offset == (bfd_vma) -1)
4436 || htab->elf.splt == NULL)
4438 /* We didn't make a PLT entry for this symbol. This
4439 happens when statically linking PIC code, or when
4440 using -Bsymbolic. */
4444 if (h->plt.offset != (bfd_vma) -1)
4446 if (htab->plt_second != NULL)
4448 resolved_plt = htab->plt_second;
4449 plt_offset = eh->plt_second.offset;
4453 resolved_plt = htab->elf.splt;
4454 plt_offset = h->plt.offset;
4459 /* Use the GOT PLT. */
4460 resolved_plt = htab->plt_got;
4461 plt_offset = eh->plt_got.offset;
4464 relocation = (resolved_plt->output_section->vma
4465 + resolved_plt->output_offset
4467 unresolved_reloc = FALSE;
4470 case R_X86_64_SIZE32:
4471 case R_X86_64_SIZE64:
4472 /* Set to symbol size. */
4473 relocation = st_size;
4479 case R_X86_64_PC32_BND:
4480 /* Don't complain about -fPIC if the symbol is undefined when
4481 building executable unless it is unresolved weak symbol or
4482 -z nocopyreloc is used. */
4483 if ((input_section->flags & SEC_ALLOC) != 0
4484 && (input_section->flags & SEC_READONLY) != 0
4486 && ((bfd_link_executable (info)
4487 && ((h->root.type == bfd_link_hash_undefweak
4488 && !resolved_to_zero)
4489 || ((info->nocopyreloc
4490 || (eh->def_protected
4491 && elf_has_no_copy_on_protected (h->root.u.def.section->owner)))
4493 && !(h->root.u.def.section->flags & SEC_CODE))))
4494 || bfd_link_dll (info)))
4496 bfd_boolean fail = FALSE;
4498 = ((r_type == R_X86_64_PC32
4499 || r_type == R_X86_64_PC32_BND)
4500 && is_32bit_relative_branch (contents, rel->r_offset));
4502 if (SYMBOL_REFERENCES_LOCAL (info, h))
4504 /* Symbol is referenced locally. Make sure it is
4505 defined locally or for a branch. */
4506 fail = (!(h->def_regular || ELF_COMMON_DEF_P (h))
4509 else if (!(bfd_link_pie (info)
4510 && (h->needs_copy || eh->needs_copy)))
4512 /* Symbol doesn't need copy reloc and isn't referenced
4513 locally. We only allow branch to symbol with
4514 non-default visibility. */
4516 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
4520 return elf_x86_64_need_pic (info, input_bfd, input_section,
4521 h, NULL, NULL, howto);
4530 /* FIXME: The ABI says the linker should make sure the value is
4531 the same when it's zeroextended to 64 bit. */
4534 if ((input_section->flags & SEC_ALLOC) == 0)
4537 /* Don't copy a pc-relative relocation into the output file
4538 if the symbol needs copy reloc or the symbol is undefined
4539 when building executable. Copy dynamic function pointer
4540 relocations. Don't generate dynamic relocations against
4541 resolved undefined weak symbols in PIE. */
4542 if ((bfd_link_pic (info)
4543 && !(bfd_link_pie (info)
4547 || h->root.type == bfd_link_hash_undefined)
4548 && (IS_X86_64_PCREL_TYPE (r_type)
4549 || r_type == R_X86_64_SIZE32
4550 || r_type == R_X86_64_SIZE64))
4552 || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4553 && !resolved_to_zero)
4554 || h->root.type != bfd_link_hash_undefweak))
4555 && ((! IS_X86_64_PCREL_TYPE (r_type)
4556 && r_type != R_X86_64_SIZE32
4557 && r_type != R_X86_64_SIZE64)
4558 || ! SYMBOL_CALLS_LOCAL (info, h)))
4559 || (ELIMINATE_COPY_RELOCS
4560 && !bfd_link_pic (info)
4564 || eh->func_pointer_refcount > 0
4565 || (h->root.type == bfd_link_hash_undefweak
4566 && !resolved_to_zero))
4567 && ((h->def_dynamic && !h->def_regular)
4568 /* Undefined weak symbol is bound locally when
4570 || h->root.type == bfd_link_hash_undefined)))
4572 Elf_Internal_Rela outrel;
4573 bfd_boolean skip, relocate;
4576 /* When generating a shared object, these relocations
4577 are copied into the output file to be resolved at run
4583 _bfd_elf_section_offset (output_bfd, info, input_section,
4585 if (outrel.r_offset == (bfd_vma) -1)
4587 else if (outrel.r_offset == (bfd_vma) -2)
4588 skip = TRUE, relocate = TRUE;
4590 outrel.r_offset += (input_section->output_section->vma
4591 + input_section->output_offset);
4594 memset (&outrel, 0, sizeof outrel);
4596 /* h->dynindx may be -1 if this symbol was marked to
4600 && (IS_X86_64_PCREL_TYPE (r_type)
4601 || !(bfd_link_executable (info)
4602 || SYMBOLIC_BIND (info, h))
4603 || ! h->def_regular))
4605 outrel.r_info = htab->r_info (h->dynindx, r_type);
4606 outrel.r_addend = rel->r_addend;
4610 /* This symbol is local, or marked to become local.
4611 When relocation overflow check is disabled, we
4612 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
4613 if (r_type == htab->pointer_r_type
4614 || (r_type == R_X86_64_32
4615 && info->no_reloc_overflow_check))
4618 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4619 outrel.r_addend = relocation + rel->r_addend;
4621 else if (r_type == R_X86_64_64
4622 && !ABI_64_P (output_bfd))
4625 outrel.r_info = htab->r_info (0,
4626 R_X86_64_RELATIVE64);
4627 outrel.r_addend = relocation + rel->r_addend;
4628 /* Check addend overflow. */
4629 if ((outrel.r_addend & 0x80000000)
4630 != (rel->r_addend & 0x80000000))
4633 int addend = rel->r_addend;
4634 if (h && h->root.root.string)
4635 name = h->root.root.string;
4637 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4640 /* xgettext:c-format */
4641 (_("%B: addend %s%#x in relocation %s against "
4642 "symbol `%s' at %#Lx in section `%A' is "
4644 input_bfd, addend < 0 ? "-" : "", addend,
4645 howto->name, name, rel->r_offset, input_section);
4646 bfd_set_error (bfd_error_bad_value);
4654 if (bfd_is_abs_section (sec))
4656 else if (sec == NULL || sec->owner == NULL)
4658 bfd_set_error (bfd_error_bad_value);
4665 /* We are turning this relocation into one
4666 against a section symbol. It would be
4667 proper to subtract the symbol's value,
4668 osec->vma, from the emitted reloc addend,
4669 but ld.so expects buggy relocs. */
4670 osec = sec->output_section;
4671 sindx = elf_section_data (osec)->dynindx;
4674 asection *oi = htab->elf.text_index_section;
4675 sindx = elf_section_data (oi)->dynindx;
4677 BFD_ASSERT (sindx != 0);
4680 outrel.r_info = htab->r_info (sindx, r_type);
4681 outrel.r_addend = relocation + rel->r_addend;
4685 sreloc = elf_section_data (input_section)->sreloc;
4687 if (sreloc == NULL || sreloc->contents == NULL)
4689 r = bfd_reloc_notsupported;
4690 goto check_relocation_error;
4693 elf_append_rela (output_bfd, sreloc, &outrel);
4695 /* If this reloc is against an external symbol, we do
4696 not want to fiddle with the addend. Otherwise, we
4697 need to include the symbol value so that it becomes
4698 an addend for the dynamic reloc. */
4705 case R_X86_64_TLSGD:
4706 case R_X86_64_GOTPC32_TLSDESC:
4707 case R_X86_64_TLSDESC_CALL:
4708 case R_X86_64_GOTTPOFF:
4709 tls_type = GOT_UNKNOWN;
4710 if (h == NULL && local_got_offsets)
4711 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
4713 tls_type = elf_x86_hash_entry (h)->tls_type;
4715 if (! elf_x86_64_tls_transition (info, input_bfd,
4716 input_section, contents,
4717 symtab_hdr, sym_hashes,
4718 &r_type, tls_type, rel,
4719 relend, h, r_symndx, TRUE))
4722 if (r_type == R_X86_64_TPOFF32)
4724 bfd_vma roff = rel->r_offset;
4726 BFD_ASSERT (! unresolved_reloc);
4728 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
4730 /* GD->LE transition. For 64bit, change
4731 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4732 .word 0x6666; rex64; call __tls_get_addr@PLT
4734 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
4736 call *__tls_get_addr@GOTPCREL(%rip)
4737 which may be converted to
4738 addr32 call __tls_get_addr
4741 leaq foo@tpoff(%rax), %rax
4743 leaq foo@tlsgd(%rip), %rdi
4744 .word 0x6666; rex64; call __tls_get_addr@PLT
4746 leaq foo@tlsgd(%rip), %rdi
4748 call *__tls_get_addr@GOTPCREL(%rip)
4749 which may be converted to
4750 addr32 call __tls_get_addr
4753 leaq foo@tpoff(%rax), %rax
4754 For largepic, change:
4755 leaq foo@tlsgd(%rip), %rdi
4756 movabsq $__tls_get_addr@pltoff, %rax
4761 leaq foo@tpoff(%rax), %rax
4762 nopw 0x0(%rax,%rax,1) */
4764 if (ABI_64_P (output_bfd))
4766 if (contents[roff + 5] == 0xb8)
4768 memcpy (contents + roff - 3,
4769 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
4770 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
4774 memcpy (contents + roff - 4,
4775 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4779 memcpy (contents + roff - 3,
4780 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
4782 bfd_put_32 (output_bfd,
4783 elf_x86_64_tpoff (info, relocation),
4784 contents + roff + 8 + largepic);
4785 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
4786 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
4791 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
4793 /* GDesc -> LE transition.
4794 It's originally something like:
4795 leaq x@tlsdesc(%rip), %rax
4798 movl $x@tpoff, %rax. */
4800 unsigned int val, type;
4802 type = bfd_get_8 (input_bfd, contents + roff - 3);
4803 val = bfd_get_8 (input_bfd, contents + roff - 1);
4804 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
4805 contents + roff - 3);
4806 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
4807 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
4808 contents + roff - 1);
4809 bfd_put_32 (output_bfd,
4810 elf_x86_64_tpoff (info, relocation),
4814 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
4816 /* GDesc -> LE transition.
4821 bfd_put_8 (output_bfd, 0x66, contents + roff);
4822 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4825 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
4827 /* IE->LE transition:
4828 For 64bit, originally it can be one of:
4829 movq foo@gottpoff(%rip), %reg
4830 addq foo@gottpoff(%rip), %reg
4833 leaq foo(%reg), %reg
4835 For 32bit, originally it can be one of:
4836 movq foo@gottpoff(%rip), %reg
4837 addl foo@gottpoff(%rip), %reg
4840 leal foo(%reg), %reg
4843 unsigned int val, type, reg;
4846 val = bfd_get_8 (input_bfd, contents + roff - 3);
4849 type = bfd_get_8 (input_bfd, contents + roff - 2);
4850 reg = bfd_get_8 (input_bfd, contents + roff - 1);
4856 bfd_put_8 (output_bfd, 0x49,
4857 contents + roff - 3);
4858 else if (!ABI_64_P (output_bfd) && val == 0x44)
4859 bfd_put_8 (output_bfd, 0x41,
4860 contents + roff - 3);
4861 bfd_put_8 (output_bfd, 0xc7,
4862 contents + roff - 2);
4863 bfd_put_8 (output_bfd, 0xc0 | reg,
4864 contents + roff - 1);
4868 /* addq/addl -> addq/addl - addressing with %rsp/%r12
4871 bfd_put_8 (output_bfd, 0x49,
4872 contents + roff - 3);
4873 else if (!ABI_64_P (output_bfd) && val == 0x44)
4874 bfd_put_8 (output_bfd, 0x41,
4875 contents + roff - 3);
4876 bfd_put_8 (output_bfd, 0x81,
4877 contents + roff - 2);
4878 bfd_put_8 (output_bfd, 0xc0 | reg,
4879 contents + roff - 1);
4883 /* addq/addl -> leaq/leal */
4885 bfd_put_8 (output_bfd, 0x4d,
4886 contents + roff - 3);
4887 else if (!ABI_64_P (output_bfd) && val == 0x44)
4888 bfd_put_8 (output_bfd, 0x45,
4889 contents + roff - 3);
4890 bfd_put_8 (output_bfd, 0x8d,
4891 contents + roff - 2);
4892 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
4893 contents + roff - 1);
4895 bfd_put_32 (output_bfd,
4896 elf_x86_64_tpoff (info, relocation),
4904 if (htab->elf.sgot == NULL)
4909 off = h->got.offset;
4910 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
4914 if (local_got_offsets == NULL)
4917 off = local_got_offsets[r_symndx];
4918 offplt = local_tlsdesc_gotents[r_symndx];
4925 Elf_Internal_Rela outrel;
4929 if (htab->elf.srelgot == NULL)
4932 indx = h && h->dynindx != -1 ? h->dynindx : 0;
4934 if (GOT_TLS_GDESC_P (tls_type))
4936 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
4937 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
4938 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
4939 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
4940 + htab->elf.sgotplt->output_offset
4942 + htab->sgotplt_jump_table_size);
4943 sreloc = htab->elf.srelplt;
4945 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
4947 outrel.r_addend = 0;
4948 elf_append_rela (output_bfd, sreloc, &outrel);
4951 sreloc = htab->elf.srelgot;
4953 outrel.r_offset = (htab->elf.sgot->output_section->vma
4954 + htab->elf.sgot->output_offset + off);
4956 if (GOT_TLS_GD_P (tls_type))
4957 dr_type = R_X86_64_DTPMOD64;
4958 else if (GOT_TLS_GDESC_P (tls_type))
4961 dr_type = R_X86_64_TPOFF64;
4963 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
4964 outrel.r_addend = 0;
4965 if ((dr_type == R_X86_64_TPOFF64
4966 || dr_type == R_X86_64_TLSDESC) && indx == 0)
4967 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
4968 outrel.r_info = htab->r_info (indx, dr_type);
4970 elf_append_rela (output_bfd, sreloc, &outrel);
4972 if (GOT_TLS_GD_P (tls_type))
4976 BFD_ASSERT (! unresolved_reloc);
4977 bfd_put_64 (output_bfd,
4978 relocation - _bfd_x86_elf_dtpoff_base (info),
4979 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4983 bfd_put_64 (output_bfd, 0,
4984 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4985 outrel.r_info = htab->r_info (indx,
4987 outrel.r_offset += GOT_ENTRY_SIZE;
4988 elf_append_rela (output_bfd, sreloc,
4997 local_got_offsets[r_symndx] |= 1;
5000 if (off >= (bfd_vma) -2
5001 && ! GOT_TLS_GDESC_P (tls_type))
5003 if (r_type == ELF32_R_TYPE (rel->r_info))
5005 if (r_type == R_X86_64_GOTPC32_TLSDESC
5006 || r_type == R_X86_64_TLSDESC_CALL)
5007 relocation = htab->elf.sgotplt->output_section->vma
5008 + htab->elf.sgotplt->output_offset
5009 + offplt + htab->sgotplt_jump_table_size;
5011 relocation = htab->elf.sgot->output_section->vma
5012 + htab->elf.sgot->output_offset + off;
5013 unresolved_reloc = FALSE;
5017 bfd_vma roff = rel->r_offset;
5019 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
5021 /* GD->IE transition. For 64bit, change
5022 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5023 .word 0x6666; rex64; call __tls_get_addr@PLT
5025 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5027 call *__tls_get_addr@GOTPCREL(%rip
5028 which may be converted to
5029 addr32 call __tls_get_addr
5032 addq foo@gottpoff(%rip), %rax
5034 leaq foo@tlsgd(%rip), %rdi
5035 .word 0x6666; rex64; call __tls_get_addr@PLT
5037 leaq foo@tlsgd(%rip), %rdi
5039 call *__tls_get_addr@GOTPCREL(%rip)
5040 which may be converted to
5041 addr32 call __tls_get_addr
5044 addq foo@gottpoff(%rip), %rax
5045 For largepic, change:
5046 leaq foo@tlsgd(%rip), %rdi
5047 movabsq $__tls_get_addr@pltoff, %rax
5052 addq foo@gottpoff(%rax), %rax
5053 nopw 0x0(%rax,%rax,1) */
5055 if (ABI_64_P (output_bfd))
5057 if (contents[roff + 5] == 0xb8)
5059 memcpy (contents + roff - 3,
5060 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
5061 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
5065 memcpy (contents + roff - 4,
5066 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5070 memcpy (contents + roff - 3,
5071 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5074 relocation = (htab->elf.sgot->output_section->vma
5075 + htab->elf.sgot->output_offset + off
5078 - input_section->output_section->vma
5079 - input_section->output_offset
5081 bfd_put_32 (output_bfd, relocation,
5082 contents + roff + 8 + largepic);
5083 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
5088 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
5090 /* GDesc -> IE transition.
5091 It's originally something like:
5092 leaq x@tlsdesc(%rip), %rax
5095 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
5097 /* Now modify the instruction as appropriate. To
5098 turn a leaq into a movq in the form we use it, it
5099 suffices to change the second byte from 0x8d to
5101 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
5103 bfd_put_32 (output_bfd,
5104 htab->elf.sgot->output_section->vma
5105 + htab->elf.sgot->output_offset + off
5107 - input_section->output_section->vma
5108 - input_section->output_offset
5113 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
5115 /* GDesc -> IE transition.
5122 bfd_put_8 (output_bfd, 0x66, contents + roff);
5123 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5131 case R_X86_64_TLSLD:
5132 if (! elf_x86_64_tls_transition (info, input_bfd,
5133 input_section, contents,
5134 symtab_hdr, sym_hashes,
5135 &r_type, GOT_UNKNOWN, rel,
5136 relend, h, r_symndx, TRUE))
5139 if (r_type != R_X86_64_TLSLD)
5141 /* LD->LE transition:
5142 leaq foo@tlsld(%rip), %rdi
5143 call __tls_get_addr@PLT
5144 For 64bit, we change it into:
5145 .word 0x6666; .byte 0x66; movq %fs:0, %rax
5146 For 32bit, we change it into:
5147 nopl 0x0(%rax); movl %fs:0, %eax
5149 leaq foo@tlsld(%rip), %rdi;
5150 call *__tls_get_addr@GOTPCREL(%rip)
5151 which may be converted to
5152 addr32 call __tls_get_addr
5153 For 64bit, we change it into:
5154 .word 0x6666; .word 0x6666; movq %fs:0, %rax
5155 For 32bit, we change it into:
5156 nopw 0x0(%rax); movl %fs:0, %eax
5157 For largepic, change:
5158 leaq foo@tlsgd(%rip), %rdi
5159 movabsq $__tls_get_addr@pltoff, %rax
5163 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
5166 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
5167 if (ABI_64_P (output_bfd))
5169 if (contents[rel->r_offset + 5] == 0xb8)
5170 memcpy (contents + rel->r_offset - 3,
5171 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
5172 "\x64\x48\x8b\x04\x25\0\0\0", 22);
5173 else if (contents[rel->r_offset + 4] == 0xff
5174 || contents[rel->r_offset + 4] == 0x67)
5175 memcpy (contents + rel->r_offset - 3,
5176 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
5179 memcpy (contents + rel->r_offset - 3,
5180 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
5184 if (contents[rel->r_offset + 4] == 0xff)
5185 memcpy (contents + rel->r_offset - 3,
5186 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
5189 memcpy (contents + rel->r_offset - 3,
5190 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
5192 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
5193 and R_X86_64_PLTOFF64. */
5199 if (htab->elf.sgot == NULL)
5202 off = htab->tls_ld_or_ldm_got.offset;
5207 Elf_Internal_Rela outrel;
5209 if (htab->elf.srelgot == NULL)
5212 outrel.r_offset = (htab->elf.sgot->output_section->vma
5213 + htab->elf.sgot->output_offset + off);
5215 bfd_put_64 (output_bfd, 0,
5216 htab->elf.sgot->contents + off);
5217 bfd_put_64 (output_bfd, 0,
5218 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
5219 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
5220 outrel.r_addend = 0;
5221 elf_append_rela (output_bfd, htab->elf.srelgot,
5223 htab->tls_ld_or_ldm_got.offset |= 1;
5225 relocation = htab->elf.sgot->output_section->vma
5226 + htab->elf.sgot->output_offset + off;
5227 unresolved_reloc = FALSE;
5230 case R_X86_64_DTPOFF32:
5231 if (!bfd_link_executable (info)
5232 || (input_section->flags & SEC_CODE) == 0)
5233 relocation -= _bfd_x86_elf_dtpoff_base (info);
5235 relocation = elf_x86_64_tpoff (info, relocation);
5238 case R_X86_64_TPOFF32:
5239 case R_X86_64_TPOFF64:
5240 BFD_ASSERT (bfd_link_executable (info));
5241 relocation = elf_x86_64_tpoff (info, relocation);
5244 case R_X86_64_DTPOFF64:
5245 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
5246 relocation -= _bfd_x86_elf_dtpoff_base (info);
5253 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5254 because such sections are not SEC_ALLOC and thus ld.so will
5255 not process them. */
5256 if (unresolved_reloc
5257 && !((input_section->flags & SEC_DEBUGGING) != 0
5259 && _bfd_elf_section_offset (output_bfd, info, input_section,
5260 rel->r_offset) != (bfd_vma) -1)
5265 sec = h->root.u.def.section;
5266 if ((info->nocopyreloc
5267 || (eh->def_protected
5268 && elf_has_no_copy_on_protected (h->root.u.def.section->owner)))
5269 && !(h->root.u.def.section->flags & SEC_CODE))
5270 return elf_x86_64_need_pic (info, input_bfd, input_section,
5271 h, NULL, NULL, howto);
5276 /* xgettext:c-format */
5277 (_("%B(%A+%#Lx): unresolvable %s relocation against symbol `%s'"),
5282 h->root.root.string);
5288 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5289 contents, rel->r_offset,
5290 relocation, rel->r_addend);
5292 check_relocation_error:
5293 if (r != bfd_reloc_ok)
5298 name = h->root.root.string;
5301 name = bfd_elf_string_from_elf_section (input_bfd,
5302 symtab_hdr->sh_link,
5307 name = bfd_section_name (input_bfd, sec);
5310 if (r == bfd_reloc_overflow)
5311 (*info->callbacks->reloc_overflow)
5312 (info, (h ? &h->root : NULL), name, howto->name,
5313 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5317 /* xgettext:c-format */
5318 (_("%B(%A+%#Lx): reloc against `%s': error %d"),
5319 input_bfd, input_section,
5320 rel->r_offset, name, (int) r);
5331 Elf_Internal_Shdr *rel_hdr;
5332 size_t deleted = rel - wrel;
5334 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
5335 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5336 if (rel_hdr->sh_size == 0)
5338 /* It is too late to remove an empty reloc section. Leave
5340 ??? What is wrong with an empty section??? */
5341 rel_hdr->sh_size = rel_hdr->sh_entsize;
5344 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5345 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5346 input_section->reloc_count -= deleted;
5352 /* Finish up dynamic symbol handling. We set the contents of various
5353 dynamic sections here. */
5356 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
5357 struct bfd_link_info *info,
5358 struct elf_link_hash_entry *h,
5359 Elf_Internal_Sym *sym)
5361 struct elf_x86_link_hash_table *htab;
5362 bfd_boolean use_plt_second;
5363 struct elf_x86_link_hash_entry *eh;
5364 bfd_boolean local_undefweak;
5365 const struct elf_x86_64_plt_layout *plt_layout;
5366 const struct elf_x86_64_lazy_plt_layout *lazy_plt_layout;
5367 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt_layout;
5369 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
5373 /* Use the second PLT section only if there is .plt section. */
5374 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
5376 eh = (struct elf_x86_link_hash_entry *) h;
5377 if (eh->no_finish_dynamic_symbol)
5380 plt_layout = &elf_x86_64_plt (htab);
5381 lazy_plt_layout = elf_x86_64_lazy_plt (htab);
5382 non_lazy_plt_layout = elf_x86_64_non_lazy_plt (htab);
5384 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5385 resolved undefined weak symbols in executable so that their
5386 references have value 0 at run-time. */
5387 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
5392 if (h->plt.offset != (bfd_vma) -1)
5395 bfd_vma got_offset, plt_offset;
5396 Elf_Internal_Rela rela;
5398 asection *plt, *gotplt, *relplt, *resolved_plt;
5399 const struct elf_backend_data *bed;
5400 bfd_vma plt_got_pcrel_offset;
5402 /* When building a static executable, use .iplt, .igot.plt and
5403 .rela.iplt sections for STT_GNU_IFUNC symbols. */
5404 if (htab->elf.splt != NULL)
5406 plt = htab->elf.splt;
5407 gotplt = htab->elf.sgotplt;
5408 relplt = htab->elf.srelplt;
5412 plt = htab->elf.iplt;
5413 gotplt = htab->elf.igotplt;
5414 relplt = htab->elf.irelplt;
5417 /* This symbol has an entry in the procedure linkage table. Set
5419 if ((h->dynindx == -1
5421 && !((h->forced_local || bfd_link_executable (info))
5423 && h->type == STT_GNU_IFUNC))
5429 /* Get the index in the procedure linkage table which
5430 corresponds to this symbol. This is the index of this symbol
5431 in all the symbols for which we are making plt entries. The
5432 first entry in the procedure linkage table is reserved.
5434 Get the offset into the .got table of the entry that
5435 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
5436 bytes. The first three are reserved for the dynamic linker.
5438 For static executables, we don't reserve anything. */
5440 if (plt == htab->elf.splt)
5442 got_offset = (h->plt.offset / plt_layout->plt_entry_size
5443 - plt_layout->has_plt0);
5444 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
5448 got_offset = h->plt.offset / plt_layout->plt_entry_size;
5449 got_offset = got_offset * GOT_ENTRY_SIZE;
5452 /* Fill in the entry in the procedure linkage table. */
5453 memcpy (plt->contents + h->plt.offset, plt_layout->plt_entry,
5454 plt_layout->plt_entry_size);
5457 memcpy (htab->plt_second->contents + eh->plt_second.offset,
5458 non_lazy_plt_layout->plt_entry,
5459 non_lazy_plt_layout->plt_entry_size);
5461 resolved_plt = htab->plt_second;
5462 plt_offset = eh->plt_second.offset;
5467 plt_offset = h->plt.offset;
5470 /* Insert the relocation positions of the plt section. */
5472 /* Put offset the PC-relative instruction referring to the GOT entry,
5473 subtracting the size of that instruction. */
5474 plt_got_pcrel_offset = (gotplt->output_section->vma
5475 + gotplt->output_offset
5477 - resolved_plt->output_section->vma
5478 - resolved_plt->output_offset
5480 - plt_layout->plt_got_insn_size);
5482 /* Check PC-relative offset overflow in PLT entry. */
5483 if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
5484 /* xgettext:c-format */
5485 info->callbacks->einfo (_("%F%B: PC-relative offset overflow in PLT entry for `%s'\n"),
5486 output_bfd, h->root.root.string);
5488 bfd_put_32 (output_bfd, plt_got_pcrel_offset,
5489 (resolved_plt->contents + plt_offset
5490 + plt_layout->plt_got_offset));
5492 /* Fill in the entry in the global offset table, initially this
5493 points to the second part of the PLT entry. Leave the entry
5494 as zero for undefined weak symbol in PIE. No PLT relocation
5495 against undefined weak symbol in PIE. */
5496 if (!local_undefweak)
5498 if (plt_layout->has_plt0)
5499 bfd_put_64 (output_bfd, (plt->output_section->vma
5500 + plt->output_offset
5502 + lazy_plt_layout->plt_lazy_offset),
5503 gotplt->contents + got_offset);
5505 /* Fill in the entry in the .rela.plt section. */
5506 rela.r_offset = (gotplt->output_section->vma
5507 + gotplt->output_offset
5509 if (h->dynindx == -1
5510 || ((bfd_link_executable (info)
5511 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5513 && h->type == STT_GNU_IFUNC))
5515 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
5516 h->root.root.string,
5517 h->root.u.def.section->owner);
5519 /* If an STT_GNU_IFUNC symbol is locally defined, generate
5520 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
5521 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
5522 rela.r_addend = (h->root.u.def.value
5523 + h->root.u.def.section->output_section->vma
5524 + h->root.u.def.section->output_offset);
5525 /* R_X86_64_IRELATIVE comes last. */
5526 plt_index = htab->next_irelative_index--;
5530 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
5532 plt_index = htab->next_jump_slot_index++;
5535 /* Don't fill the second and third slots in PLT entry for
5536 static executables nor without PLT0. */
5537 if (plt == htab->elf.splt && plt_layout->has_plt0)
5540 = h->plt.offset + lazy_plt_layout->plt_plt_insn_end;
5542 /* Put relocation index. */
5543 bfd_put_32 (output_bfd, plt_index,
5544 (plt->contents + h->plt.offset
5545 + lazy_plt_layout->plt_reloc_offset));
5547 /* Put offset for jmp .PLT0 and check for overflow. We don't
5548 check relocation index for overflow since branch displacement
5549 will overflow first. */
5550 if (plt0_offset > 0x80000000)
5551 /* xgettext:c-format */
5552 info->callbacks->einfo (_("%F%B: branch displacement overflow in PLT entry for `%s'\n"),
5553 output_bfd, h->root.root.string);
5554 bfd_put_32 (output_bfd, - plt0_offset,
5555 (plt->contents + h->plt.offset
5556 + lazy_plt_layout->plt_plt_offset));
5559 bed = get_elf_backend_data (output_bfd);
5560 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
5561 bed->s->swap_reloca_out (output_bfd, &rela, loc);
5564 else if (eh->plt_got.offset != (bfd_vma) -1)
5566 bfd_vma got_offset, plt_offset;
5567 asection *plt, *got;
5568 bfd_boolean got_after_plt;
5569 int32_t got_pcrel_offset;
5571 /* Set the entry in the GOT procedure linkage table. */
5572 plt = htab->plt_got;
5573 got = htab->elf.sgot;
5574 got_offset = h->got.offset;
5576 if (got_offset == (bfd_vma) -1
5577 || (h->type == STT_GNU_IFUNC && h->def_regular)
5582 /* Use the non-lazy PLT entry template for the GOT PLT since they
5583 are the identical. */
5584 /* Fill in the entry in the GOT procedure linkage table. */
5585 plt_offset = eh->plt_got.offset;
5586 memcpy (plt->contents + plt_offset,
5587 non_lazy_plt_layout->plt_entry,
5588 non_lazy_plt_layout->plt_entry_size);
5590 /* Put offset the PC-relative instruction referring to the GOT
5591 entry, subtracting the size of that instruction. */
5592 got_pcrel_offset = (got->output_section->vma
5593 + got->output_offset
5595 - plt->output_section->vma
5596 - plt->output_offset
5598 - non_lazy_plt_layout->plt_got_insn_size);
5600 /* Check PC-relative offset overflow in GOT PLT entry. */
5601 got_after_plt = got->output_section->vma > plt->output_section->vma;
5602 if ((got_after_plt && got_pcrel_offset < 0)
5603 || (!got_after_plt && got_pcrel_offset > 0))
5604 /* xgettext:c-format */
5605 info->callbacks->einfo (_("%F%B: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
5606 output_bfd, h->root.root.string);
5608 bfd_put_32 (output_bfd, got_pcrel_offset,
5609 (plt->contents + plt_offset
5610 + non_lazy_plt_layout->plt_got_offset));
5613 if (!local_undefweak
5615 && (h->plt.offset != (bfd_vma) -1
5616 || eh->plt_got.offset != (bfd_vma) -1))
5618 /* Mark the symbol as undefined, rather than as defined in
5619 the .plt section. Leave the value if there were any
5620 relocations where pointer equality matters (this is a clue
5621 for the dynamic linker, to make function pointer
5622 comparisons work between an application and shared
5623 library), otherwise set it to zero. If a function is only
5624 called from a binary, there is no need to slow down
5625 shared libraries because of that. */
5626 sym->st_shndx = SHN_UNDEF;
5627 if (!h->pointer_equality_needed)
5631 /* Don't generate dynamic GOT relocation against undefined weak
5632 symbol in executable. */
5633 if (h->got.offset != (bfd_vma) -1
5634 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
5635 && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
5636 && !local_undefweak)
5638 Elf_Internal_Rela rela;
5639 asection *relgot = htab->elf.srelgot;
5641 /* This symbol has an entry in the global offset table. Set it
5643 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
5646 rela.r_offset = (htab->elf.sgot->output_section->vma
5647 + htab->elf.sgot->output_offset
5648 + (h->got.offset &~ (bfd_vma) 1));
5650 /* If this is a static link, or it is a -Bsymbolic link and the
5651 symbol is defined locally or was forced to be local because
5652 of a version file, we just want to emit a RELATIVE reloc.
5653 The entry in the global offset table will already have been
5654 initialized in the relocate_section function. */
5656 && h->type == STT_GNU_IFUNC)
5658 if (h->plt.offset == (bfd_vma) -1)
5660 /* STT_GNU_IFUNC is referenced without PLT. */
5661 if (htab->elf.splt == NULL)
5663 /* use .rel[a].iplt section to store .got relocations
5664 in static executable. */
5665 relgot = htab->elf.irelplt;
5667 if (SYMBOL_REFERENCES_LOCAL (info, h))
5669 info->callbacks->minfo (_("Local IFUNC function `%s' in %B\n"),
5671 h->root.root.string,
5672 h->root.u.def.section->owner);
5674 rela.r_info = htab->r_info (0,
5675 R_X86_64_IRELATIVE);
5676 rela.r_addend = (h->root.u.def.value
5677 + h->root.u.def.section->output_section->vma
5678 + h->root.u.def.section->output_offset);
5683 else if (bfd_link_pic (info))
5685 /* Generate R_X86_64_GLOB_DAT. */
5693 if (!h->pointer_equality_needed)
5696 /* For non-shared object, we can't use .got.plt, which
5697 contains the real function addres if we need pointer
5698 equality. We load the GOT entry with the PLT entry. */
5699 if (htab->plt_second != NULL)
5701 plt = htab->plt_second;
5702 plt_offset = eh->plt_second.offset;
5706 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
5707 plt_offset = h->plt.offset;
5709 bfd_put_64 (output_bfd, (plt->output_section->vma
5710 + plt->output_offset
5712 htab->elf.sgot->contents + h->got.offset);
5716 else if (bfd_link_pic (info)
5717 && SYMBOL_REFERENCES_LOCAL (info, h))
5719 if (!h->def_regular)
5721 BFD_ASSERT((h->got.offset & 1) != 0);
5722 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
5723 rela.r_addend = (h->root.u.def.value
5724 + h->root.u.def.section->output_section->vma
5725 + h->root.u.def.section->output_offset);
5729 BFD_ASSERT((h->got.offset & 1) == 0);
5731 bfd_put_64 (output_bfd, (bfd_vma) 0,
5732 htab->elf.sgot->contents + h->got.offset);
5733 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
5737 elf_append_rela (output_bfd, relgot, &rela);
5742 Elf_Internal_Rela rela;
5745 /* This symbol needs a copy reloc. Set it up. */
5747 if (h->dynindx == -1
5748 || (h->root.type != bfd_link_hash_defined
5749 && h->root.type != bfd_link_hash_defweak)
5750 || htab->elf.srelbss == NULL
5751 || htab->elf.sreldynrelro == NULL)
5754 rela.r_offset = (h->root.u.def.value
5755 + h->root.u.def.section->output_section->vma
5756 + h->root.u.def.section->output_offset);
5757 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
5759 if (h->root.u.def.section == htab->elf.sdynrelro)
5760 s = htab->elf.sreldynrelro;
5762 s = htab->elf.srelbss;
5763 elf_append_rela (output_bfd, s, &rela);
5769 /* Finish up local dynamic symbol handling. We set the contents of
5770 various dynamic sections here. */
5773 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
5775 struct elf_link_hash_entry *h
5776 = (struct elf_link_hash_entry *) *slot;
5777 struct bfd_link_info *info
5778 = (struct bfd_link_info *) inf;
5780 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5784 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
5785 here since undefined weak symbol may not be dynamic and may not be
5786 called for elf_x86_64_finish_dynamic_symbol. */
5789 elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
5792 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
5793 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5795 if (h->root.type != bfd_link_hash_undefweak
5796 || h->dynindx != -1)
5799 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
5803 /* Used to decide how to sort relocs in an optimal manner for the
5804 dynamic linker, before writing them out. */
5806 static enum elf_reloc_type_class
5807 elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
5808 const asection *rel_sec ATTRIBUTE_UNUSED,
5809 const Elf_Internal_Rela *rela)
5811 bfd *abfd = info->output_bfd;
5812 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5813 struct elf_x86_link_hash_table *htab
5814 = elf_x86_hash_table (info, X86_64_ELF_DATA);
5816 if (htab->elf.dynsym != NULL
5817 && htab->elf.dynsym->contents != NULL)
5819 /* Check relocation against STT_GNU_IFUNC symbol if there are
5821 unsigned long r_symndx = htab->r_sym (rela->r_info);
5822 if (r_symndx != STN_UNDEF)
5824 Elf_Internal_Sym sym;
5825 if (!bed->s->swap_symbol_in (abfd,
5826 (htab->elf.dynsym->contents
5827 + r_symndx * bed->s->sizeof_sym),
5831 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
5832 return reloc_class_ifunc;
5836 switch ((int) ELF32_R_TYPE (rela->r_info))
5838 case R_X86_64_IRELATIVE:
5839 return reloc_class_ifunc;
5840 case R_X86_64_RELATIVE:
5841 case R_X86_64_RELATIVE64:
5842 return reloc_class_relative;
5843 case R_X86_64_JUMP_SLOT:
5844 return reloc_class_plt;
5846 return reloc_class_copy;
5848 return reloc_class_normal;
5852 /* Finish up the dynamic sections. */
5855 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
5856 struct bfd_link_info *info)
5858 struct elf_x86_link_hash_table *htab;
5862 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
5866 dynobj = htab->elf.dynobj;
5867 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5869 if (htab->elf.dynamic_sections_created)
5871 bfd_byte *dyncon, *dynconend;
5872 const struct elf_backend_data *bed;
5873 bfd_size_type sizeof_dyn;
5874 const struct elf_x86_64_plt_layout *plt_layout;
5875 const struct elf_x86_64_lazy_plt_layout *lazy_plt_layout;
5876 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt_layout;
5878 if (sdyn == NULL || htab->elf.sgot == NULL)
5881 bed = get_elf_backend_data (dynobj);
5882 sizeof_dyn = bed->s->sizeof_dyn;
5883 dyncon = sdyn->contents;
5884 dynconend = sdyn->contents + sdyn->size;
5885 for (; dyncon < dynconend; dyncon += sizeof_dyn)
5887 Elf_Internal_Dyn dyn;
5890 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
5898 s = htab->elf.sgotplt;
5899 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5903 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
5907 s = htab->elf.srelplt->output_section;
5908 dyn.d_un.d_val = s->size;
5911 case DT_TLSDESC_PLT:
5913 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
5914 + htab->tlsdesc_plt;
5917 case DT_TLSDESC_GOT:
5919 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
5920 + htab->tlsdesc_got;
5924 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
5927 plt_layout = &elf_x86_64_plt (htab);
5928 lazy_plt_layout = elf_x86_64_lazy_plt (htab);
5929 non_lazy_plt_layout = elf_x86_64_non_lazy_plt (htab);
5931 if (htab->elf.splt && htab->elf.splt->size > 0)
5933 elf_section_data (htab->elf.splt->output_section)
5934 ->this_hdr.sh_entsize = plt_layout->plt_entry_size;
5936 if (plt_layout->has_plt0)
5938 /* Fill in the special first entry in the procedure linkage
5940 memcpy (htab->elf.splt->contents,
5941 lazy_plt_layout->plt0_entry,
5942 lazy_plt_layout->plt_entry_size);
5943 /* Add offset for pushq GOT+8(%rip), since the instruction
5944 uses 6 bytes subtract this value. */
5945 bfd_put_32 (output_bfd,
5946 (htab->elf.sgotplt->output_section->vma
5947 + htab->elf.sgotplt->output_offset
5949 - htab->elf.splt->output_section->vma
5950 - htab->elf.splt->output_offset
5952 (htab->elf.splt->contents
5953 + lazy_plt_layout->plt0_got1_offset));
5954 /* Add offset for the PC-relative instruction accessing
5955 GOT+16, subtracting the offset to the end of that
5957 bfd_put_32 (output_bfd,
5958 (htab->elf.sgotplt->output_section->vma
5959 + htab->elf.sgotplt->output_offset
5961 - htab->elf.splt->output_section->vma
5962 - htab->elf.splt->output_offset
5963 - lazy_plt_layout->plt0_got2_insn_end),
5964 (htab->elf.splt->contents
5965 + lazy_plt_layout->plt0_got2_offset));
5967 if (htab->tlsdesc_plt)
5969 bfd_put_64 (output_bfd, (bfd_vma) 0,
5970 htab->elf.sgot->contents + htab->tlsdesc_got);
5972 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
5973 lazy_plt_layout->plt0_entry,
5974 lazy_plt_layout->plt_entry_size);
5976 /* Add offset for pushq GOT+8(%rip), since the
5977 instruction uses 6 bytes subtract this value. */
5978 bfd_put_32 (output_bfd,
5979 (htab->elf.sgotplt->output_section->vma
5980 + htab->elf.sgotplt->output_offset
5982 - htab->elf.splt->output_section->vma
5983 - htab->elf.splt->output_offset
5986 (htab->elf.splt->contents
5988 + lazy_plt_layout->plt0_got1_offset));
5989 /* Add offset for the PC-relative instruction accessing
5990 GOT+TDG, where TDG stands for htab->tlsdesc_got,
5991 subtracting the offset to the end of that
5993 bfd_put_32 (output_bfd,
5994 (htab->elf.sgot->output_section->vma
5995 + htab->elf.sgot->output_offset
5997 - htab->elf.splt->output_section->vma
5998 - htab->elf.splt->output_offset
6000 - lazy_plt_layout->plt0_got2_insn_end),
6001 (htab->elf.splt->contents
6003 + lazy_plt_layout->plt0_got2_offset));
6008 if (htab->plt_got != NULL && htab->plt_got->size > 0)
6009 elf_section_data (htab->plt_got->output_section)
6010 ->this_hdr.sh_entsize = non_lazy_plt_layout->plt_entry_size;
6012 if (htab->plt_second != NULL && htab->plt_second->size > 0)
6013 elf_section_data (htab->plt_second->output_section)
6014 ->this_hdr.sh_entsize = non_lazy_plt_layout->plt_entry_size;
6017 /* GOT is always created in setup_gnu_properties. But it may not be
6019 if (htab->elf.sgotplt && htab->elf.sgotplt->size > 0)
6021 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
6024 (_("discarded output section: `%A'"), htab->elf.sgotplt);
6028 /* Set the first entry in the global offset table to the address of
6029 the dynamic section. */
6031 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
6033 bfd_put_64 (output_bfd,
6034 sdyn->output_section->vma + sdyn->output_offset,
6035 htab->elf.sgotplt->contents);
6036 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
6037 bfd_put_64 (output_bfd, (bfd_vma) 0,
6038 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
6039 bfd_put_64 (output_bfd, (bfd_vma) 0,
6040 htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
6042 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize
6046 /* Adjust .eh_frame for .plt section. */
6047 if (htab->plt_eh_frame != NULL
6048 && htab->plt_eh_frame->contents != NULL)
6050 if (htab->elf.splt != NULL
6051 && htab->elf.splt->size != 0
6052 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
6053 && htab->elf.splt->output_section != NULL
6054 && htab->plt_eh_frame->output_section != NULL)
6056 bfd_vma plt_start = htab->elf.splt->output_section->vma;
6057 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
6058 + htab->plt_eh_frame->output_offset
6059 + PLT_FDE_START_OFFSET;
6060 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6061 htab->plt_eh_frame->contents
6062 + PLT_FDE_START_OFFSET);
6064 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
6066 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6068 htab->plt_eh_frame->contents))
6073 /* Adjust .eh_frame for .plt.got section. */
6074 if (htab->plt_got_eh_frame != NULL
6075 && htab->plt_got_eh_frame->contents != NULL)
6077 if (htab->plt_got != NULL
6078 && htab->plt_got->size != 0
6079 && (htab->plt_got->flags & SEC_EXCLUDE) == 0
6080 && htab->plt_got->output_section != NULL
6081 && htab->plt_got_eh_frame->output_section != NULL)
6083 bfd_vma plt_start = htab->plt_got->output_section->vma;
6084 bfd_vma eh_frame_start = htab->plt_got_eh_frame->output_section->vma
6085 + htab->plt_got_eh_frame->output_offset
6086 + PLT_FDE_START_OFFSET;
6087 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6088 htab->plt_got_eh_frame->contents
6089 + PLT_FDE_START_OFFSET);
6091 if (htab->plt_got_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
6093 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6094 htab->plt_got_eh_frame,
6095 htab->plt_got_eh_frame->contents))
6100 /* Adjust .eh_frame for the second PLT section. */
6101 if (htab->plt_second_eh_frame != NULL
6102 && htab->plt_second_eh_frame->contents != NULL)
6104 if (htab->plt_second != NULL
6105 && htab->plt_second->size != 0
6106 && (htab->plt_second->flags & SEC_EXCLUDE) == 0
6107 && htab->plt_second->output_section != NULL
6108 && htab->plt_second_eh_frame->output_section != NULL)
6110 bfd_vma plt_start = htab->plt_second->output_section->vma;
6111 bfd_vma eh_frame_start
6112 = (htab->plt_second_eh_frame->output_section->vma
6113 + htab->plt_second_eh_frame->output_offset
6114 + PLT_FDE_START_OFFSET);
6115 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6116 htab->plt_second_eh_frame->contents
6117 + PLT_FDE_START_OFFSET);
6119 if (htab->plt_second_eh_frame->sec_info_type
6120 == SEC_INFO_TYPE_EH_FRAME)
6122 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6123 htab->plt_second_eh_frame,
6124 htab->plt_second_eh_frame->contents))
6129 if (htab->elf.sgot && htab->elf.sgot->size > 0)
6130 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
6133 /* Fill PLT entries for undefined weak symbols in PIE. */
6134 if (bfd_link_pie (info))
6135 bfd_hash_traverse (&info->hash->table,
6136 elf_x86_64_pie_finish_undefweak_symbol,
6142 /* Fill PLT/GOT entries and allocate dynamic relocations for local
6143 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
6144 It has to be done before elf_link_sort_relocs is called so that
6145 dynamic relocations are properly sorted. */
6148 elf_x86_64_output_arch_local_syms
6149 (bfd *output_bfd ATTRIBUTE_UNUSED,
6150 struct bfd_link_info *info,
6151 void *flaginfo ATTRIBUTE_UNUSED,
6152 int (*func) (void *, const char *,
6155 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
6157 struct elf_x86_link_hash_table *htab
6158 = elf_x86_hash_table (info, X86_64_ELF_DATA);
6162 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
6163 htab_traverse (htab->loc_hash_table,
6164 elf_x86_64_finish_local_dynamic_symbol,
6170 enum elf_x86_64_plt_type
6174 plt_second = 1 << 1,
6178 struct elf_x86_64_plt
6183 enum elf_x86_64_plt_type type;
6184 unsigned int plt_got_offset;
6185 unsigned int plt_got_insn_size;
6186 unsigned int plt_entry_size;
6190 /* Forward declaration. */
6191 static const struct elf_x86_64_lazy_plt_layout elf_x86_64_nacl_plt;
6193 /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
6194 dynamic relocations. */
6197 elf_x86_64_get_synthetic_symtab (bfd *abfd,
6198 long symcount ATTRIBUTE_UNUSED,
6199 asymbol **syms ATTRIBUTE_UNUSED,
6204 long size, count, i, n, len;
6206 unsigned int plt_got_offset, plt_entry_size, plt_got_insn_size;
6208 bfd_byte *plt_contents;
6209 long dynrelcount, relsize;
6210 arelent **dynrelbuf, *p;
6211 const struct elf_x86_64_lazy_plt_layout *lazy_plt;
6212 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt;
6213 const struct elf_x86_64_lazy_plt_layout *lazy_bnd_plt;
6214 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_bnd_plt;
6215 const struct elf_x86_64_lazy_plt_layout *lazy_ibt_plt;
6216 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_ibt_plt;
6219 enum elf_x86_64_plt_type plt_type;
6220 struct elf_x86_64_plt plts[] =
6222 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
6223 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
6224 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
6225 { ".plt.bnd", NULL, NULL, plt_second, 0, 0, 0, 0 },
6226 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
6231 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
6234 if (dynsymcount <= 0)
6237 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
6241 if (get_elf_x86_64_backend_data (abfd)->os == is_normal)
6243 lazy_plt = &elf_x86_64_lazy_plt;
6244 non_lazy_plt = &elf_x86_64_non_lazy_plt;
6245 lazy_bnd_plt = &elf_x86_64_lazy_bnd_plt;
6246 non_lazy_bnd_plt = &elf_x86_64_non_lazy_bnd_plt;
6247 if (ABI_64_P (abfd))
6249 lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
6250 non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
6254 lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
6255 non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
6260 lazy_plt = &elf_x86_64_nacl_plt;
6261 non_lazy_plt = NULL;
6262 lazy_bnd_plt = NULL;
6263 non_lazy_bnd_plt = NULL;
6264 lazy_ibt_plt = NULL;
6265 non_lazy_ibt_plt = NULL;
6269 for (j = 0; plts[j].name != NULL; j++)
6271 plt = bfd_get_section_by_name (abfd, plts[j].name);
6272 if (plt == NULL || plt->size == 0)
6275 /* Get the PLT section contents. */
6276 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
6277 if (plt_contents == NULL)
6279 if (!bfd_get_section_contents (abfd, (asection *) plt,
6280 plt_contents, 0, plt->size))
6282 free (plt_contents);
6286 /* Check what kind of PLT it is. */
6287 plt_type = plt_unknown;
6288 if (plts[j].type == plt_unknown
6289 && (plt->size >= (lazy_plt->plt_entry_size
6290 + lazy_plt->plt_entry_size)))
6292 /* Match lazy PLT first. Need to check the first two
6294 if ((memcmp (plt_contents, lazy_plt->plt0_entry,
6295 lazy_plt->plt0_got1_offset) == 0)
6296 && (memcmp (plt_contents + 6, lazy_plt->plt0_entry + 6,
6298 plt_type = plt_lazy;
6299 else if (lazy_bnd_plt != NULL
6300 && (memcmp (plt_contents, lazy_bnd_plt->plt0_entry,
6301 lazy_bnd_plt->plt0_got1_offset) == 0)
6302 && (memcmp (plt_contents + 6,
6303 lazy_bnd_plt->plt0_entry + 6, 3) == 0))
6305 plt_type = plt_lazy | plt_second;
6306 /* The fist entry in the lazy IBT PLT is the same as the
6308 if ((memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
6309 lazy_ibt_plt->plt_entry,
6310 lazy_ibt_plt->plt_got_offset) == 0))
6311 lazy_plt = lazy_ibt_plt;
6313 lazy_plt = lazy_bnd_plt;
6317 if (non_lazy_plt != NULL
6318 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
6319 && plt->size >= non_lazy_plt->plt_entry_size)
6321 /* Match non-lazy PLT. */
6322 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
6323 non_lazy_plt->plt_got_offset) == 0)
6324 plt_type = plt_non_lazy;
6327 if (plt_type == plt_unknown || plt_type == plt_second)
6329 if (non_lazy_bnd_plt != NULL
6330 && plt->size >= non_lazy_bnd_plt->plt_entry_size
6331 && (memcmp (plt_contents, non_lazy_bnd_plt->plt_entry,
6332 non_lazy_bnd_plt->plt_got_offset) == 0))
6334 /* Match BND PLT. */
6335 plt_type = plt_second;
6336 non_lazy_plt = non_lazy_bnd_plt;
6338 else if (non_lazy_ibt_plt != NULL
6339 && plt->size >= non_lazy_ibt_plt->plt_entry_size
6340 && (memcmp (plt_contents,
6341 non_lazy_ibt_plt->plt_entry,
6342 non_lazy_ibt_plt->plt_got_offset) == 0))
6344 /* Match IBT PLT. */
6345 plt_type = plt_second;
6346 non_lazy_plt = non_lazy_ibt_plt;
6350 if (plt_type == plt_unknown)
6352 free (plt_contents);
6357 plts[j].type = plt_type;
6359 if ((plt_type & plt_lazy))
6361 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
6362 plts[j].plt_got_insn_size = lazy_plt->plt_got_insn_size;
6363 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
6364 /* Skip PLT0 in lazy PLT. */
6369 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
6370 plts[j].plt_got_insn_size = non_lazy_plt->plt_got_insn_size;
6371 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
6375 /* Skip lazy PLT when the second PLT is used. */
6376 if (plt_type == (plt_lazy | plt_second))
6380 n = plt->size / plts[j].plt_entry_size;
6385 plts[j].contents = plt_contents;
6391 dynrelbuf = (arelent **) bfd_malloc (relsize);
6392 if (dynrelbuf == NULL)
6395 dynrelcount = bfd_canonicalize_dynamic_reloc (abfd, dynrelbuf,
6398 /* Sort the relocs by address. */
6399 qsort (dynrelbuf, dynrelcount, sizeof (arelent *),
6400 _bfd_x86_elf_compare_relocs);
6402 size = count * sizeof (asymbol);
6404 /* Allocate space for @plt suffixes. */
6406 for (i = 0; i < dynrelcount; i++)
6409 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
6411 size += sizeof ("+0x") - 1 + 8 + 8 * ABI_64_P (abfd);
6414 s = *ret = (asymbol *) bfd_zmalloc (size);
6418 /* Check for each PLT section. */
6419 names = (char *) (s + count);
6422 for (j = 0; plts[j].name != NULL; j++)
6423 if ((plt_contents = plts[j].contents) != NULL)
6428 plt_got_offset = plts[j].plt_got_offset;
6429 plt_got_insn_size = plts[j].plt_got_insn_size;
6430 plt_entry_size = plts[j].plt_entry_size;
6434 if ((plts[j].type & plt_lazy))
6436 /* Skip PLT0 in lazy PLT. */
6438 offset = plt_entry_size;
6446 /* Check each PLT entry against dynamic relocations. */
6447 for (; k < plts[j].count; k++)
6453 /* Get the PC-relative offset, a signed 32-bit integer. */
6454 off = H_GET_32 (abfd, (plt_contents + offset
6456 got_vma = plt->vma + offset + off + plt_got_insn_size;
6458 /* Binary search. */
6462 while ((min + 1) < max)
6466 mid = (min + max) / 2;
6468 if (got_vma > r->address)
6470 else if (got_vma < r->address)
6479 /* Skip unknown relocation. PR 17512: file: bc9d6cf5. */
6480 if (got_vma == p->address
6482 && (p->howto->type == R_X86_64_JUMP_SLOT
6483 || p->howto->type == R_X86_64_GLOB_DAT
6484 || p->howto->type == R_X86_64_IRELATIVE))
6486 *s = **p->sym_ptr_ptr;
6487 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL
6488 set. Since we are defining a symbol, ensure one
6490 if ((s->flags & BSF_LOCAL) == 0)
6491 s->flags |= BSF_GLOBAL;
6492 s->flags |= BSF_SYNTHETIC;
6493 /* This is no longer a section symbol. */
6494 s->flags &= ~BSF_SECTION_SYM;
6496 s->the_bfd = plt->owner;
6500 len = strlen ((*p->sym_ptr_ptr)->name);
6501 memcpy (names, (*p->sym_ptr_ptr)->name, len);
6507 memcpy (names, "+0x", sizeof ("+0x") - 1);
6508 names += sizeof ("+0x") - 1;
6509 bfd_sprintf_vma (abfd, buf, p->addend);
6510 for (a = buf; *a == '0'; ++a)
6513 memcpy (names, a, size);
6516 memcpy (names, "@plt", sizeof ("@plt"));
6517 names += sizeof ("@plt");
6521 offset += plt_entry_size;
6525 /* PLT entries with R_X86_64_TLSDESC relocations are skipped. */
6534 for (j = 0; plts[j].name != NULL; j++)
6535 if (plts[j].contents != NULL)
6536 free (plts[j].contents);
6543 /* Handle an x86-64 specific section when reading an object file. This
6544 is called when elfcode.h finds a section with an unknown type. */
6547 elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
6548 const char *name, int shindex)
6550 if (hdr->sh_type != SHT_X86_64_UNWIND)
6553 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6559 /* Hook called by the linker routine which adds symbols from an object
6560 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
6564 elf_x86_64_add_symbol_hook (bfd *abfd,
6565 struct bfd_link_info *info ATTRIBUTE_UNUSED,
6566 Elf_Internal_Sym *sym,
6567 const char **namep ATTRIBUTE_UNUSED,
6568 flagword *flagsp ATTRIBUTE_UNUSED,
6574 switch (sym->st_shndx)
6576 case SHN_X86_64_LCOMMON:
6577 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
6580 lcomm = bfd_make_section_with_flags (abfd,
6584 | SEC_LINKER_CREATED));
6587 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
6590 *valp = sym->st_size;
6598 /* Given a BFD section, try to locate the corresponding ELF section
6602 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6603 asection *sec, int *index_return)
6605 if (sec == &_bfd_elf_large_com_section)
6607 *index_return = SHN_X86_64_LCOMMON;
6613 /* Process a symbol. */
6616 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
6619 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
6621 switch (elfsym->internal_elf_sym.st_shndx)
6623 case SHN_X86_64_LCOMMON:
6624 asym->section = &_bfd_elf_large_com_section;
6625 asym->value = elfsym->internal_elf_sym.st_size;
6626 /* Common symbol doesn't set BSF_GLOBAL. */
6627 asym->flags &= ~BSF_GLOBAL;
6633 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
6635 return (sym->st_shndx == SHN_COMMON
6636 || sym->st_shndx == SHN_X86_64_LCOMMON);
6640 elf_x86_64_common_section_index (asection *sec)
6642 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6645 return SHN_X86_64_LCOMMON;
6649 elf_x86_64_common_section (asection *sec)
6651 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6652 return bfd_com_section_ptr;
6654 return &_bfd_elf_large_com_section;
6658 elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
6659 const Elf_Internal_Sym *sym,
6664 const asection *oldsec)
6666 /* A normal common symbol and a large common symbol result in a
6667 normal common symbol. We turn the large common symbol into a
6670 && h->root.type == bfd_link_hash_common
6672 && bfd_is_com_section (*psec)
6675 if (sym->st_shndx == SHN_COMMON
6676 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
6678 h->root.u.c.p->section
6679 = bfd_make_section_old_way (oldbfd, "COMMON");
6680 h->root.u.c.p->section->flags = SEC_ALLOC;
6682 else if (sym->st_shndx == SHN_X86_64_LCOMMON
6683 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
6684 *psec = bfd_com_section_ptr;
6691 elf_x86_64_additional_program_headers (bfd *abfd,
6692 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6697 /* Check to see if we need a large readonly segment. */
6698 s = bfd_get_section_by_name (abfd, ".lrodata");
6699 if (s && (s->flags & SEC_LOAD))
6702 /* Check to see if we need a large data segment. Since .lbss sections
6703 is placed right after the .bss section, there should be no need for
6704 a large data segment just because of .lbss. */
6705 s = bfd_get_section_by_name (abfd, ".ldata");
6706 if (s && (s->flags & SEC_LOAD))
6712 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
6715 elf_x86_64_relocs_compatible (const bfd_target *input,
6716 const bfd_target *output)
6718 return ((xvec_get_elf_backend_data (input)->s->elfclass
6719 == xvec_get_elf_backend_data (output)->s->elfclass)
6720 && _bfd_elf_relocs_compatible (input, output));
6723 /* Set up x86-64 GNU properties. Return the first relocatable ELF input
6724 with GNU properties if found. Otherwise, return NULL. */
6727 elf_x86_64_link_setup_gnu_properties (struct bfd_link_info *info)
6729 bfd_boolean normal_target;
6730 bfd_boolean lazy_plt;
6731 asection *sec, *pltsec;
6733 bfd_boolean use_ibt_plt;
6734 unsigned int plt_alignment, features;
6735 struct elf_x86_link_hash_table *htab;
6739 struct elf_x86_64_plt_layout *plt_layout;
6740 const struct elf_x86_64_lazy_plt_layout *lazy_plt_layout;
6741 const struct elf_x86_64_non_lazy_plt_layout *non_lazy_plt_layout;
6745 features = GNU_PROPERTY_X86_FEATURE_1_IBT;
6747 features |= GNU_PROPERTY_X86_FEATURE_1_SHSTK;
6749 /* Find a normal input file with GNU property note. */
6750 for (pbfd = info->input_bfds;
6752 pbfd = pbfd->link.next)
6753 if (bfd_get_flavour (pbfd) == bfd_target_elf_flavour
6754 && bfd_count_sections (pbfd) != 0)
6758 if (elf_properties (pbfd) != NULL)
6762 if (ebfd != NULL && features)
6764 /* If features is set, add GNU_PROPERTY_X86_FEATURE_1_IBT and
6765 GNU_PROPERTY_X86_FEATURE_1_SHSTK. */
6766 prop = _bfd_elf_get_property (ebfd,
6767 GNU_PROPERTY_X86_FEATURE_1_AND,
6769 prop->u.number |= features;
6770 prop->pr_kind = property_number;
6772 /* Create the GNU property note section if needed. */
6775 sec = bfd_make_section_with_flags (ebfd,
6776 NOTE_GNU_PROPERTY_SECTION_NAME,
6784 info->callbacks->einfo (_("%F: failed to create GNU property section\n"));
6786 if (!bfd_set_section_alignment (ebfd, sec,
6787 ABI_64_P (ebfd) ? 3 : 2))
6790 info->callbacks->einfo (_("%F%A: failed to align section\n"),
6794 elf_section_type (sec) = SHT_NOTE;
6798 pbfd = _bfd_elf_link_setup_gnu_properties (info);
6800 if (bfd_link_relocatable (info))
6803 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
6807 use_ibt_plt = info->ibtplt || info->ibt;
6808 if (!use_ibt_plt && pbfd != NULL)
6810 /* Check if GNU_PROPERTY_X86_FEATURE_1_IBT is on. */
6811 elf_property_list *p;
6813 /* The property list is sorted in order of type. */
6814 for (p = elf_properties (pbfd); p; p = p->next)
6816 if (GNU_PROPERTY_X86_FEATURE_1_AND == p->property.pr_type)
6818 use_ibt_plt = !!(p->property.u.number
6819 & GNU_PROPERTY_X86_FEATURE_1_IBT);
6822 else if (GNU_PROPERTY_X86_FEATURE_1_AND < p->property.pr_type)
6827 dynobj = htab->elf.dynobj;
6829 /* Set htab->elf.dynobj here so that there is no need to check and
6830 set it in check_relocs. */
6835 htab->elf.dynobj = pbfd;
6842 /* Find a normal input file to hold linker created
6844 for (abfd = info->input_bfds;
6846 abfd = abfd->link.next)
6847 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
6849 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0)
6851 htab->elf.dynobj = abfd;
6858 plt_layout = &elf_x86_64_plt (htab);
6860 /* Even when lazy binding is disabled by "-z now", the PLT0 entry may
6861 still be used with LD_AUDIT or LD_PROFILE if PLT entry is used for
6862 canonical function address. */
6863 plt_layout->has_plt0 = 1;
6865 if (get_elf_x86_64_backend_data (info->output_bfd)->os
6870 if (ABI_64_P (dynobj))
6872 elf_x86_64_lazy_plt (htab)
6873 = &elf_x86_64_lazy_ibt_plt;
6874 elf_x86_64_non_lazy_plt (htab)
6875 = &elf_x86_64_non_lazy_ibt_plt;
6879 elf_x86_64_lazy_plt (htab)
6880 = &elf_x32_lazy_ibt_plt;
6881 elf_x86_64_non_lazy_plt (htab)
6882 = &elf_x32_non_lazy_ibt_plt;
6885 else if (info->bndplt)
6887 elf_x86_64_lazy_plt (htab) = &elf_x86_64_lazy_bnd_plt;
6888 elf_x86_64_non_lazy_plt (htab) = &elf_x86_64_non_lazy_bnd_plt;
6892 elf_x86_64_lazy_plt (htab) = &elf_x86_64_lazy_plt;
6893 elf_x86_64_non_lazy_plt (htab) = &elf_x86_64_non_lazy_plt;
6895 normal_target = TRUE;
6899 elf_x86_64_lazy_plt (htab) = &elf_x86_64_nacl_plt;
6900 elf_x86_64_non_lazy_plt (htab) = NULL;
6901 normal_target = FALSE;
6904 lazy_plt_layout = elf_x86_64_lazy_plt (htab);
6905 non_lazy_plt_layout = elf_x86_64_non_lazy_plt (htab);
6907 pltsec = htab->elf.splt;
6909 /* If the non-lazy PLT is available, use it for all PLT entries if
6910 there are no PLT0 or no .plt section. */
6911 if (non_lazy_plt_layout != NULL
6912 && (!plt_layout->has_plt0 || pltsec == NULL))
6915 plt_layout->plt_entry
6916 = non_lazy_plt_layout->plt_entry;
6917 plt_layout->plt_entry_size
6918 = non_lazy_plt_layout->plt_entry_size;
6919 plt_layout->plt_got_offset
6920 = non_lazy_plt_layout->plt_got_offset;
6921 plt_layout->plt_got_insn_size
6922 = non_lazy_plt_layout->plt_got_insn_size;
6923 plt_layout->eh_frame_plt_size
6924 = non_lazy_plt_layout->eh_frame_plt_size;
6925 plt_layout->eh_frame_plt
6926 = non_lazy_plt_layout->eh_frame_plt;
6931 plt_layout->plt_entry
6932 = lazy_plt_layout->plt_entry;
6933 plt_layout->plt_entry_size
6934 = lazy_plt_layout->plt_entry_size;
6935 plt_layout->plt_got_offset
6936 = lazy_plt_layout->plt_got_offset;
6937 plt_layout->plt_got_insn_size
6938 = lazy_plt_layout->plt_got_insn_size;
6939 plt_layout->eh_frame_plt_size
6940 = lazy_plt_layout->eh_frame_plt_size;
6941 plt_layout->eh_frame_plt
6942 = lazy_plt_layout->eh_frame_plt;
6945 /* Return if there are no normal input files. */
6949 /* Since create_dynamic_sections isn't always called, but GOT
6950 relocations need GOT relocations, create them here so that we
6951 don't need to do it in check_relocs. */
6952 if (htab->elf.sgot == NULL
6953 && !_bfd_elf_create_got_section (dynobj, info))
6954 info->callbacks->einfo (_("%F: failed to create GOT sections\n"));
6956 /* Align .got and .got.plt sections to their entry size. Do it here
6957 instead of in create_dynamic_sections so that they are always
6958 properly aligned even if create_dynamic_sections isn't called. */
6959 sec = htab->elf.sgot;
6960 if (!bfd_set_section_alignment (dynobj, sec, 3))
6961 goto error_alignment;
6963 sec = htab->elf.sgotplt;
6964 if (!bfd_set_section_alignment (dynobj, sec, 3))
6965 goto error_alignment;
6967 /* Create the ifunc sections here so that check_relocs can be
6969 if (!_bfd_elf_create_ifunc_sections (dynobj, info))
6970 info->callbacks->einfo (_("%F: failed to create ifunc sections\n"));
6972 plt_alignment = bfd_log2 (plt_layout->plt_entry_size);
6976 /* Whe creating executable, set the contents of the .interp
6977 section to the interpreter. */
6978 if (bfd_link_executable (info) && !info->nointerp)
6980 asection *s = bfd_get_linker_section (dynobj, ".interp");
6983 s->size = htab->dynamic_interpreter_size;
6984 s->contents = (unsigned char *) htab->dynamic_interpreter;
6988 /* Don't change PLT section alignment for NaCl since it uses
6989 64-byte PLT entry and sets PLT section alignment to 32
6990 bytes. Don't create additional PLT sections for NaCl. */
6993 const struct elf_backend_data *bed
6994 = get_elf_backend_data (dynobj);
6995 flagword pltflags = (bed->dynamic_sec_flags
7000 unsigned int non_lazy_plt_alignment
7001 = bfd_log2 (non_lazy_plt_layout->plt_entry_size);
7004 if (!bfd_set_section_alignment (sec->owner, sec,
7006 goto error_alignment;
7008 /* Create the GOT procedure linkage table. */
7009 sec = bfd_make_section_anyway_with_flags (dynobj,
7013 info->callbacks->einfo (_("%F: failed to create GOT PLT section\n"));
7015 if (!bfd_set_section_alignment (dynobj, sec,
7016 non_lazy_plt_alignment))
7017 goto error_alignment;
7019 htab->plt_got = sec;
7027 /* Create the second PLT for Intel IBT support. IBT
7028 PLT is supported only for non-NaCl target and is
7029 is needed only for lazy binding. */
7030 sec = bfd_make_section_anyway_with_flags (dynobj,
7034 info->callbacks->einfo (_("%F: failed to create IBT-enabled PLT section\n"));
7036 if (!bfd_set_section_alignment (dynobj, sec,
7038 goto error_alignment;
7040 else if (info->bndplt && ABI_64_P (dynobj))
7042 /* Create the second PLT for Intel MPX support. MPX
7043 PLT is supported only for non-NaCl target in 64-bit
7044 mode and is needed only for lazy binding. */
7045 sec = bfd_make_section_anyway_with_flags (dynobj,
7049 info->callbacks->einfo (_("%F: failed to create BND PLT section\n"));
7051 if (!bfd_set_section_alignment (dynobj, sec,
7052 non_lazy_plt_alignment))
7053 goto error_alignment;
7056 htab->plt_second = sec;
7060 if (!info->no_ld_generated_unwind_info)
7062 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
7063 | SEC_HAS_CONTENTS | SEC_IN_MEMORY
7064 | SEC_LINKER_CREATED);
7066 sec = bfd_make_section_anyway_with_flags (dynobj,
7070 info->callbacks->einfo (_("%F: failed to create PLT .eh_frame section\n"));
7072 if (!bfd_set_section_alignment (dynobj, sec,
7073 ABI_64_P (dynobj) ? 3 : 2))
7074 goto error_alignment;
7076 htab->plt_eh_frame = sec;
7078 if (htab->plt_got != NULL)
7080 sec = bfd_make_section_anyway_with_flags (dynobj,
7084 info->callbacks->einfo (_("%F: failed to create GOT PLT .eh_frame section\n"));
7086 if (!bfd_set_section_alignment (dynobj, sec,
7087 ABI_64_P (dynobj) ? 3 : 2))
7088 goto error_alignment;
7090 htab->plt_got_eh_frame = sec;
7093 if (htab->plt_second != NULL)
7095 sec = bfd_make_section_anyway_with_flags (dynobj,
7099 info->callbacks->einfo (_("%F: failed to create BND PLT .eh_frame section\n"));
7101 if (!bfd_set_section_alignment (dynobj, sec, 3))
7102 goto error_alignment;
7104 htab->plt_second_eh_frame = sec;
7111 /* The .iplt section is used for IFUNC symbols in static
7113 sec = htab->elf.iplt;
7115 && !bfd_set_section_alignment (sec->owner, sec,
7117 goto error_alignment;
7123 static const struct bfd_elf_special_section
7124 elf_x86_64_special_sections[]=
7126 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
7127 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
7128 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
7129 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
7130 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
7131 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
7132 { NULL, 0, 0, 0, 0 }
7135 #define TARGET_LITTLE_SYM x86_64_elf64_vec
7136 #define TARGET_LITTLE_NAME "elf64-x86-64"
7137 #define ELF_ARCH bfd_arch_i386
7138 #define ELF_TARGET_ID X86_64_ELF_DATA
7139 #define ELF_MACHINE_CODE EM_X86_64
7140 #define ELF_MAXPAGESIZE 0x200000
7141 #define ELF_MINPAGESIZE 0x1000
7142 #define ELF_COMMONPAGESIZE 0x1000
7144 #define elf_backend_can_gc_sections 1
7145 #define elf_backend_can_refcount 1
7146 #define elf_backend_want_got_plt 1
7147 #define elf_backend_plt_readonly 1
7148 #define elf_backend_want_plt_sym 0
7149 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
7150 #define elf_backend_rela_normal 1
7151 #define elf_backend_plt_alignment 4
7152 #define elf_backend_extern_protected_data 1
7153 #define elf_backend_caches_rawsize 1
7154 #define elf_backend_dtrel_excludes_plt 1
7155 #define elf_backend_want_dynrelro 1
7157 #define elf_info_to_howto elf_x86_64_info_to_howto
7159 #define bfd_elf64_bfd_link_hash_table_create \
7160 elf_x86_64_link_hash_table_create
7161 #define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
7162 #define bfd_elf64_bfd_reloc_name_lookup \
7163 elf_x86_64_reloc_name_lookup
7165 #define elf_backend_adjust_dynamic_symbol elf_x86_64_adjust_dynamic_symbol
7166 #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
7167 #define elf_backend_check_relocs elf_x86_64_check_relocs
7168 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
7169 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
7170 #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
7171 #define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
7172 #define elf_backend_gc_mark_hook elf_x86_64_gc_mark_hook
7173 #define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
7174 #define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
7176 #define elf_backend_write_core_note elf_x86_64_write_core_note
7178 #define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
7179 #define elf_backend_relocate_section elf_x86_64_relocate_section
7180 #define elf_backend_size_dynamic_sections elf_x86_64_size_dynamic_sections
7181 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
7182 #define elf_backend_object_p elf64_x86_64_elf_object_p
7183 #define bfd_elf64_mkobject elf_x86_64_mkobject
7184 #define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
7186 #define elf_backend_section_from_shdr \
7187 elf_x86_64_section_from_shdr
7189 #define elf_backend_section_from_bfd_section \
7190 elf_x86_64_elf_section_from_bfd_section
7191 #define elf_backend_add_symbol_hook \
7192 elf_x86_64_add_symbol_hook
7193 #define elf_backend_symbol_processing \
7194 elf_x86_64_symbol_processing
7195 #define elf_backend_common_section_index \
7196 elf_x86_64_common_section_index
7197 #define elf_backend_common_section \
7198 elf_x86_64_common_section
7199 #define elf_backend_common_definition \
7200 elf_x86_64_common_definition
7201 #define elf_backend_merge_symbol \
7202 elf_x86_64_merge_symbol
7203 #define elf_backend_special_sections \
7204 elf_x86_64_special_sections
7205 #define elf_backend_additional_program_headers \
7206 elf_x86_64_additional_program_headers
7207 #define elf_backend_setup_gnu_properties \
7208 elf_x86_64_link_setup_gnu_properties
7210 #include "elf64-target.h"
7212 /* CloudABI support. */
7214 #undef TARGET_LITTLE_SYM
7215 #define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
7216 #undef TARGET_LITTLE_NAME
7217 #define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
7220 #define ELF_OSABI ELFOSABI_CLOUDABI
7223 #define elf64_bed elf64_x86_64_cloudabi_bed
7225 #include "elf64-target.h"
7227 /* FreeBSD support. */
7229 #undef TARGET_LITTLE_SYM
7230 #define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
7231 #undef TARGET_LITTLE_NAME
7232 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
7235 #define ELF_OSABI ELFOSABI_FREEBSD
7238 #define elf64_bed elf64_x86_64_fbsd_bed
7240 #include "elf64-target.h"
7242 /* Solaris 2 support. */
7244 #undef TARGET_LITTLE_SYM
7245 #define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
7246 #undef TARGET_LITTLE_NAME
7247 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
7249 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
7250 objects won't be recognized. */
7254 #define elf64_bed elf64_x86_64_sol2_bed
7256 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
7258 #undef elf_backend_static_tls_alignment
7259 #define elf_backend_static_tls_alignment 16
7261 /* The Solaris 2 ABI requires a plt symbol on all platforms.
7263 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
7265 #undef elf_backend_want_plt_sym
7266 #define elf_backend_want_plt_sym 1
7268 #undef elf_backend_strtab_flags
7269 #define elf_backend_strtab_flags SHF_STRINGS
7272 elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
7273 bfd *obfd ATTRIBUTE_UNUSED,
7274 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
7275 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
7277 /* PR 19938: FIXME: Need to add code for setting the sh_info
7278 and sh_link fields of Solaris specific section types. */
7282 #undef elf_backend_copy_special_section_fields
7283 #define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
7285 #include "elf64-target.h"
7287 /* Native Client support. */
7290 elf64_x86_64_nacl_elf_object_p (bfd *abfd)
7292 /* Set the right machine number for a NaCl x86-64 ELF64 file. */
7293 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64_nacl);
7297 #undef TARGET_LITTLE_SYM
7298 #define TARGET_LITTLE_SYM x86_64_elf64_nacl_vec
7299 #undef TARGET_LITTLE_NAME
7300 #define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
7302 #define elf64_bed elf64_x86_64_nacl_bed
7304 #undef ELF_MAXPAGESIZE
7305 #undef ELF_MINPAGESIZE
7306 #undef ELF_COMMONPAGESIZE
7307 #define ELF_MAXPAGESIZE 0x10000
7308 #define ELF_MINPAGESIZE 0x10000
7309 #define ELF_COMMONPAGESIZE 0x10000
7311 /* Restore defaults. */
7313 #undef elf_backend_static_tls_alignment
7314 #undef elf_backend_want_plt_sym
7315 #define elf_backend_want_plt_sym 0
7316 #undef elf_backend_strtab_flags
7317 #undef elf_backend_copy_special_section_fields
7319 /* NaCl uses substantially different PLT entries for the same effects. */
7321 #undef elf_backend_plt_alignment
7322 #define elf_backend_plt_alignment 5
7323 #define NACL_PLT_ENTRY_SIZE 64
7324 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
7326 static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
7328 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
7329 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
7330 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
7331 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
7332 0x41, 0xff, 0xe3, /* jmpq *%r11 */
7334 /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
7335 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1) */
7337 /* 32 bytes of nop to pad out to the standard size. */
7338 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7339 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7340 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7341 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7342 0x66, /* excess data16 prefix */
7346 static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
7348 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
7349 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
7350 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
7351 0x41, 0xff, 0xe3, /* jmpq *%r11 */
7353 /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
7354 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7355 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7357 /* Lazy GOT entries point here (32-byte aligned). */
7358 0x68, /* pushq immediate */
7359 0, 0, 0, 0, /* replaced with index into relocation table. */
7360 0xe9, /* jmp relative */
7361 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
7363 /* 22 bytes of nop to pad out to the standard size. */
7364 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
7365 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
7366 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
7369 /* .eh_frame covering the .plt section. */
7371 static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
7373 #if (PLT_CIE_LENGTH != 20 \
7374 || PLT_FDE_LENGTH != 36 \
7375 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
7376 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
7377 # error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
7379 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
7380 0, 0, 0, 0, /* CIE ID */
7381 1, /* CIE version */
7382 'z', 'R', 0, /* Augmentation string */
7383 1, /* Code alignment factor */
7384 0x78, /* Data alignment factor */
7385 16, /* Return address column */
7386 1, /* Augmentation size */
7387 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
7388 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
7389 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
7390 DW_CFA_nop, DW_CFA_nop,
7392 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
7393 PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
7394 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
7395 0, 0, 0, 0, /* .plt size goes here */
7396 0, /* Augmentation size */
7397 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
7398 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
7399 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
7400 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
7401 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
7402 13, /* Block length */
7403 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
7404 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
7405 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
7406 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
7407 DW_CFA_nop, DW_CFA_nop
7410 static const struct elf_x86_64_lazy_plt_layout elf_x86_64_nacl_plt =
7412 elf_x86_64_nacl_plt0_entry, /* plt0_entry */
7413 elf_x86_64_nacl_plt_entry, /* plt_entry */
7414 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
7415 2, /* plt0_got1_offset */
7416 9, /* plt0_got2_offset */
7417 13, /* plt0_got2_insn_end */
7418 3, /* plt_got_offset */
7419 33, /* plt_reloc_offset */
7420 38, /* plt_plt_offset */
7421 7, /* plt_got_insn_size */
7422 42, /* plt_plt_insn_end */
7423 32, /* plt_lazy_offset */
7424 elf_x86_64_nacl_eh_frame_plt, /* eh_frame_plt */
7425 sizeof (elf_x86_64_nacl_eh_frame_plt) /* eh_frame_plt_size */
7428 static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
7433 #undef elf_backend_arch_data
7434 #define elf_backend_arch_data &elf_x86_64_nacl_arch_bed
7436 #undef elf_backend_object_p
7437 #define elf_backend_object_p elf64_x86_64_nacl_elf_object_p
7438 #undef elf_backend_modify_segment_map
7439 #define elf_backend_modify_segment_map nacl_modify_segment_map
7440 #undef elf_backend_modify_program_headers
7441 #define elf_backend_modify_program_headers nacl_modify_program_headers
7442 #undef elf_backend_final_write_processing
7443 #define elf_backend_final_write_processing nacl_final_write_processing
7445 #include "elf64-target.h"
7447 /* Native Client x32 support. */
7450 elf32_x86_64_nacl_elf_object_p (bfd *abfd)
7452 /* Set the right machine number for a NaCl x86-64 ELF32 file. */
7453 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32_nacl);
7457 #undef TARGET_LITTLE_SYM
7458 #define TARGET_LITTLE_SYM x86_64_elf32_nacl_vec
7459 #undef TARGET_LITTLE_NAME
7460 #define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
7462 #define elf32_bed elf32_x86_64_nacl_bed
7464 #define bfd_elf32_bfd_link_hash_table_create \
7465 elf_x86_64_link_hash_table_create
7466 #define bfd_elf32_bfd_reloc_type_lookup \
7467 elf_x86_64_reloc_type_lookup
7468 #define bfd_elf32_bfd_reloc_name_lookup \
7469 elf_x86_64_reloc_name_lookup
7470 #define bfd_elf32_mkobject \
7472 #define bfd_elf32_get_synthetic_symtab \
7473 elf_x86_64_get_synthetic_symtab
7475 #undef elf_backend_object_p
7476 #define elf_backend_object_p \
7477 elf32_x86_64_nacl_elf_object_p
7479 #undef elf_backend_bfd_from_remote_memory
7480 #define elf_backend_bfd_from_remote_memory \
7481 _bfd_elf32_bfd_from_remote_memory
7483 #undef elf_backend_size_info
7484 #define elf_backend_size_info \
7485 _bfd_elf32_size_info
7487 #include "elf32-target.h"
7489 /* Restore defaults. */
7490 #undef elf_backend_object_p
7491 #define elf_backend_object_p elf64_x86_64_elf_object_p
7492 #undef elf_backend_bfd_from_remote_memory
7493 #undef elf_backend_size_info
7494 #undef elf_backend_modify_segment_map
7495 #undef elf_backend_modify_program_headers
7496 #undef elf_backend_final_write_processing
7498 /* Intel L1OM support. */
7501 elf64_l1om_elf_object_p (bfd *abfd)
7503 /* Set the right machine number for an L1OM elf64 file. */
7504 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
7508 #undef TARGET_LITTLE_SYM
7509 #define TARGET_LITTLE_SYM l1om_elf64_vec
7510 #undef TARGET_LITTLE_NAME
7511 #define TARGET_LITTLE_NAME "elf64-l1om"
7513 #define ELF_ARCH bfd_arch_l1om
7515 #undef ELF_MACHINE_CODE
7516 #define ELF_MACHINE_CODE EM_L1OM
7521 #define elf64_bed elf64_l1om_bed
7523 #undef elf_backend_object_p
7524 #define elf_backend_object_p elf64_l1om_elf_object_p
7526 /* Restore defaults. */
7527 #undef ELF_MAXPAGESIZE
7528 #undef ELF_MINPAGESIZE
7529 #undef ELF_COMMONPAGESIZE
7530 #define ELF_MAXPAGESIZE 0x200000
7531 #define ELF_MINPAGESIZE 0x1000
7532 #define ELF_COMMONPAGESIZE 0x1000
7533 #undef elf_backend_plt_alignment
7534 #define elf_backend_plt_alignment 4
7535 #undef elf_backend_arch_data
7536 #define elf_backend_arch_data &elf_x86_64_arch_bed
7538 #include "elf64-target.h"
7540 /* FreeBSD L1OM support. */
7542 #undef TARGET_LITTLE_SYM
7543 #define TARGET_LITTLE_SYM l1om_elf64_fbsd_vec
7544 #undef TARGET_LITTLE_NAME
7545 #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
7548 #define ELF_OSABI ELFOSABI_FREEBSD
7551 #define elf64_bed elf64_l1om_fbsd_bed
7553 #include "elf64-target.h"
7555 /* Intel K1OM support. */
7558 elf64_k1om_elf_object_p (bfd *abfd)
7560 /* Set the right machine number for an K1OM elf64 file. */
7561 bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
7565 #undef TARGET_LITTLE_SYM
7566 #define TARGET_LITTLE_SYM k1om_elf64_vec
7567 #undef TARGET_LITTLE_NAME
7568 #define TARGET_LITTLE_NAME "elf64-k1om"
7570 #define ELF_ARCH bfd_arch_k1om
7572 #undef ELF_MACHINE_CODE
7573 #define ELF_MACHINE_CODE EM_K1OM
7578 #define elf64_bed elf64_k1om_bed
7580 #undef elf_backend_object_p
7581 #define elf_backend_object_p elf64_k1om_elf_object_p
7583 #undef elf_backend_static_tls_alignment
7585 #undef elf_backend_want_plt_sym
7586 #define elf_backend_want_plt_sym 0
7588 #include "elf64-target.h"
7590 /* FreeBSD K1OM support. */
7592 #undef TARGET_LITTLE_SYM
7593 #define TARGET_LITTLE_SYM k1om_elf64_fbsd_vec
7594 #undef TARGET_LITTLE_NAME
7595 #define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
7598 #define ELF_OSABI ELFOSABI_FREEBSD
7601 #define elf64_bed elf64_k1om_fbsd_bed
7603 #include "elf64-target.h"
7605 /* 32bit x86-64 support. */
7607 #undef TARGET_LITTLE_SYM
7608 #define TARGET_LITTLE_SYM x86_64_elf32_vec
7609 #undef TARGET_LITTLE_NAME
7610 #define TARGET_LITTLE_NAME "elf32-x86-64"
7614 #define ELF_ARCH bfd_arch_i386
7616 #undef ELF_MACHINE_CODE
7617 #define ELF_MACHINE_CODE EM_X86_64
7621 #undef elf_backend_object_p
7622 #define elf_backend_object_p \
7623 elf32_x86_64_elf_object_p
7625 #undef elf_backend_bfd_from_remote_memory
7626 #define elf_backend_bfd_from_remote_memory \
7627 _bfd_elf32_bfd_from_remote_memory
7629 #undef elf_backend_size_info
7630 #define elf_backend_size_info \
7631 _bfd_elf32_size_info
7633 #include "elf32-target.h"