Add documentation for the MIPS assembler's -march=from-abi command line option
[binutils-gdb.git] / bfd / elf64-x86-64.c
blob3b7a8aeee117b7eb0c2bdf97d90c11eb3dd19732
1 /* X86-64 specific support for ELF
2 Copyright (C) 2000-2023 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"
23 #include "dwarf2.h"
24 #include "libiberty.h"
25 #include "sframe.h"
27 #include "opcode/i386.h"
29 #ifdef CORE_HEADER
30 #include <stdarg.h>
31 #include CORE_HEADER
32 #endif
34 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
35 #define MINUS_ONE (~ (bfd_vma) 0)
37 /* Since both 32-bit and 64-bit x86-64 encode relocation type in the
38 identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
39 relocation type. We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
40 since they are the same. */
42 /* The relocation "howto" table. Order of fields:
43 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
44 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
45 static reloc_howto_type x86_64_elf_howto_table[] =
47 HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
48 bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0, 0x00000000,
49 false),
50 HOWTO(R_X86_64_64, 0, 8, 64, false, 0, complain_overflow_dont,
51 bfd_elf_generic_reloc, "R_X86_64_64", false, 0, MINUS_ONE,
52 false),
53 HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed,
54 bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0, 0xffffffff,
55 true),
56 HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed,
57 bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0, 0xffffffff,
58 false),
59 HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed,
60 bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0, 0xffffffff,
61 true),
62 HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0, 0xffffffff,
64 false),
65 HOWTO(R_X86_64_GLOB_DAT, 0, 8, 64, false, 0, complain_overflow_dont,
66 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, 0, MINUS_ONE,
67 false),
68 HOWTO(R_X86_64_JUMP_SLOT, 0, 8, 64, false, 0, complain_overflow_dont,
69 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, 0, MINUS_ONE,
70 false),
71 HOWTO(R_X86_64_RELATIVE, 0, 8, 64, false, 0, complain_overflow_dont,
72 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, 0, MINUS_ONE,
73 false),
74 HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true, 0, complain_overflow_signed,
75 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0, 0xffffffff,
76 true),
77 HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned,
78 bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
79 false),
80 HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed,
81 bfd_elf_generic_reloc, "R_X86_64_32S", false, 0, 0xffffffff,
82 false),
83 HOWTO(R_X86_64_16, 0, 2, 16, false, 0, complain_overflow_bitfield,
84 bfd_elf_generic_reloc, "R_X86_64_16", false, 0, 0xffff, false),
85 HOWTO(R_X86_64_PC16, 0, 2, 16, true, 0, complain_overflow_bitfield,
86 bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0, 0xffff, true),
87 HOWTO(R_X86_64_8, 0, 1, 8, false, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_X86_64_8", false, 0, 0xff, false),
89 HOWTO(R_X86_64_PC8, 0, 1, 8, true, 0, complain_overflow_signed,
90 bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0, 0xff, true),
91 HOWTO(R_X86_64_DTPMOD64, 0, 8, 64, false, 0, complain_overflow_dont,
92 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", false, 0, MINUS_ONE,
93 false),
94 HOWTO(R_X86_64_DTPOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
95 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", false, 0, MINUS_ONE,
96 false),
97 HOWTO(R_X86_64_TPOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
98 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", false, 0, MINUS_ONE,
99 false),
100 HOWTO(R_X86_64_TLSGD, 0, 4, 32, true, 0, complain_overflow_signed,
101 bfd_elf_generic_reloc, "R_X86_64_TLSGD", false, 0, 0xffffffff,
102 true),
103 HOWTO(R_X86_64_TLSLD, 0, 4, 32, true, 0, complain_overflow_signed,
104 bfd_elf_generic_reloc, "R_X86_64_TLSLD", false, 0, 0xffffffff,
105 true),
106 HOWTO(R_X86_64_DTPOFF32, 0, 4, 32, false, 0, complain_overflow_signed,
107 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", false, 0, 0xffffffff,
108 false),
109 HOWTO(R_X86_64_GOTTPOFF, 0, 4, 32, true, 0, complain_overflow_signed,
110 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", false, 0, 0xffffffff,
111 true),
112 HOWTO(R_X86_64_TPOFF32, 0, 4, 32, false, 0, complain_overflow_signed,
113 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", false, 0, 0xffffffff,
114 false),
115 HOWTO(R_X86_64_PC64, 0, 8, 64, true, 0, complain_overflow_dont,
116 bfd_elf_generic_reloc, "R_X86_64_PC64", false, 0, MINUS_ONE,
117 true),
118 HOWTO(R_X86_64_GOTOFF64, 0, 8, 64, false, 0, complain_overflow_dont,
119 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64", false, 0, MINUS_ONE,
120 false),
121 HOWTO(R_X86_64_GOTPC32, 0, 4, 32, true, 0, complain_overflow_signed,
122 bfd_elf_generic_reloc, "R_X86_64_GOTPC32", false, 0, 0xffffffff,
123 true),
124 HOWTO(R_X86_64_GOT64, 0, 8, 64, false, 0, complain_overflow_signed,
125 bfd_elf_generic_reloc, "R_X86_64_GOT64", false, 0, MINUS_ONE,
126 false),
127 HOWTO(R_X86_64_GOTPCREL64, 0, 8, 64, true, 0, complain_overflow_signed,
128 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", false, 0, MINUS_ONE,
129 true),
130 HOWTO(R_X86_64_GOTPC64, 0, 8, 64, true, 0, complain_overflow_signed,
131 bfd_elf_generic_reloc, "R_X86_64_GOTPC64", false, 0, MINUS_ONE,
132 true),
133 HOWTO(R_X86_64_GOTPLT64, 0, 8, 64, false, 0, complain_overflow_signed,
134 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", false, 0, MINUS_ONE,
135 false),
136 HOWTO(R_X86_64_PLTOFF64, 0, 8, 64, false, 0, complain_overflow_signed,
137 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", false, 0, MINUS_ONE,
138 false),
139 HOWTO(R_X86_64_SIZE32, 0, 4, 32, false, 0, complain_overflow_unsigned,
140 bfd_elf_generic_reloc, "R_X86_64_SIZE32", false, 0, 0xffffffff,
141 false),
142 HOWTO(R_X86_64_SIZE64, 0, 8, 64, false, 0, complain_overflow_dont,
143 bfd_elf_generic_reloc, "R_X86_64_SIZE64", false, 0, MINUS_ONE,
144 false),
145 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 4, 32, true, 0,
146 complain_overflow_bitfield, bfd_elf_generic_reloc,
147 "R_X86_64_GOTPC32_TLSDESC", false, 0, 0xffffffff, true),
148 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, false, 0,
149 complain_overflow_dont, bfd_elf_generic_reloc,
150 "R_X86_64_TLSDESC_CALL",
151 false, 0, 0, false),
152 HOWTO(R_X86_64_TLSDESC, 0, 8, 64, false, 0,
153 complain_overflow_dont, bfd_elf_generic_reloc,
154 "R_X86_64_TLSDESC", false, 0, MINUS_ONE, false),
155 HOWTO(R_X86_64_IRELATIVE, 0, 8, 64, false, 0, complain_overflow_dont,
156 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", false, 0, MINUS_ONE,
157 false),
158 HOWTO(R_X86_64_RELATIVE64, 0, 8, 64, false, 0, complain_overflow_dont,
159 bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", false, 0, MINUS_ONE,
160 false),
161 HOWTO(R_X86_64_PC32_BND, 0, 4, 32, true, 0, complain_overflow_signed,
162 bfd_elf_generic_reloc, "R_X86_64_PC32_BND", false, 0, 0xffffffff,
163 true),
164 HOWTO(R_X86_64_PLT32_BND, 0, 4, 32, true, 0, complain_overflow_signed,
165 bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", false, 0, 0xffffffff,
166 true),
167 HOWTO(R_X86_64_GOTPCRELX, 0, 4, 32, true, 0, complain_overflow_signed,
168 bfd_elf_generic_reloc, "R_X86_64_GOTPCRELX", false, 0, 0xffffffff,
169 true),
170 HOWTO(R_X86_64_REX_GOTPCRELX, 0, 4, 32, true, 0, complain_overflow_signed,
171 bfd_elf_generic_reloc, "R_X86_64_REX_GOTPCRELX", false, 0, 0xffffffff,
172 true),
174 /* We have a gap in the reloc numbers here.
175 R_X86_64_standard counts the number up to this point, and
176 R_X86_64_vt_offset is the value to subtract from a reloc type of
177 R_X86_64_GNU_VT* to form an index into this table. */
178 #define R_X86_64_standard (R_X86_64_REX_GOTPCRELX + 1)
179 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
181 /* GNU extension to record C++ vtable hierarchy. */
182 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 8, 0, false, 0, complain_overflow_dont,
183 NULL, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
185 /* GNU extension to record C++ vtable member usage. */
186 HOWTO (R_X86_64_GNU_VTENTRY, 0, 8, 0, false, 0, complain_overflow_dont,
187 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0,
188 false),
190 /* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
191 HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_bitfield,
192 bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
193 false)
196 /* Map BFD relocs to the x86_64 elf relocs. */
197 struct elf_reloc_map
199 bfd_reloc_code_real_type bfd_reloc_val;
200 unsigned char elf_reloc_val;
203 static const struct elf_reloc_map x86_64_reloc_map[] =
205 { BFD_RELOC_NONE, R_X86_64_NONE, },
206 { BFD_RELOC_64, R_X86_64_64, },
207 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
208 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
209 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
210 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
211 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
212 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
213 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
214 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
215 { BFD_RELOC_32, R_X86_64_32, },
216 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
217 { BFD_RELOC_16, R_X86_64_16, },
218 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
219 { BFD_RELOC_8, R_X86_64_8, },
220 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
221 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
222 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
223 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
224 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
225 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
226 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
227 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
228 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
229 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
230 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
231 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
232 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
233 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
234 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
235 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
236 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
237 { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
238 { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
239 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
240 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
241 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
242 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
243 { BFD_RELOC_X86_64_PC32_BND, R_X86_64_PC32_BND, },
244 { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND, },
245 { BFD_RELOC_X86_64_GOTPCRELX, R_X86_64_GOTPCRELX, },
246 { BFD_RELOC_X86_64_REX_GOTPCRELX, R_X86_64_REX_GOTPCRELX, },
247 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
248 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
251 static reloc_howto_type *
252 elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
254 unsigned i;
256 if (r_type == (unsigned int) R_X86_64_32)
258 if (ABI_64_P (abfd))
259 i = r_type;
260 else
261 i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
263 else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
264 || r_type >= (unsigned int) R_X86_64_max)
266 if (r_type >= (unsigned int) R_X86_64_standard)
268 /* xgettext:c-format */
269 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
270 abfd, r_type);
271 bfd_set_error (bfd_error_bad_value);
272 return NULL;
274 i = r_type;
276 else
277 i = r_type - (unsigned int) R_X86_64_vt_offset;
278 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
279 return &x86_64_elf_howto_table[i];
282 /* Given a BFD reloc type, return a HOWTO structure. */
283 static reloc_howto_type *
284 elf_x86_64_reloc_type_lookup (bfd *abfd,
285 bfd_reloc_code_real_type code)
287 unsigned int i;
289 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
290 i++)
292 if (x86_64_reloc_map[i].bfd_reloc_val == code)
293 return elf_x86_64_rtype_to_howto (abfd,
294 x86_64_reloc_map[i].elf_reloc_val);
296 return NULL;
299 static reloc_howto_type *
300 elf_x86_64_reloc_name_lookup (bfd *abfd,
301 const char *r_name)
303 unsigned int i;
305 if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
307 /* Get x32 R_X86_64_32. */
308 reloc_howto_type *reloc
309 = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
310 BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
311 return reloc;
314 for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
315 if (x86_64_elf_howto_table[i].name != NULL
316 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
317 return &x86_64_elf_howto_table[i];
319 return NULL;
322 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
324 static bool
325 elf_x86_64_info_to_howto (bfd *abfd, arelent *cache_ptr,
326 Elf_Internal_Rela *dst)
328 unsigned r_type;
330 r_type = ELF32_R_TYPE (dst->r_info);
331 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
332 if (cache_ptr->howto == NULL)
333 return false;
334 BFD_ASSERT (r_type == cache_ptr->howto->type || cache_ptr->howto->type == R_X86_64_NONE);
335 return true;
338 /* Support for core dump NOTE sections. */
339 static bool
340 elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
342 int offset;
343 size_t size;
345 switch (note->descsz)
347 default:
348 return false;
350 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
351 /* pr_cursig */
352 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
354 /* pr_pid */
355 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
357 /* pr_reg */
358 offset = 72;
359 size = 216;
361 break;
363 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
364 /* pr_cursig */
365 elf_tdata (abfd)->core->signal
366 = bfd_get_16 (abfd, note->descdata + 12);
368 /* pr_pid */
369 elf_tdata (abfd)->core->lwpid
370 = bfd_get_32 (abfd, note->descdata + 32);
372 /* pr_reg */
373 offset = 112;
374 size = 216;
376 break;
379 /* Make a ".reg/999" section. */
380 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
381 size, note->descpos + offset);
384 static bool
385 elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
387 switch (note->descsz)
389 default:
390 return false;
392 case 124:
393 /* sizeof (struct elf_external_linux_prpsinfo32_ugid16). */
394 elf_tdata (abfd)->core->pid
395 = bfd_get_32 (abfd, note->descdata + 12);
396 elf_tdata (abfd)->core->program
397 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
398 elf_tdata (abfd)->core->command
399 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
400 break;
402 case 128:
403 /* sizeof (struct elf_external_linux_prpsinfo32_ugid32). */
404 elf_tdata (abfd)->core->pid
405 = bfd_get_32 (abfd, note->descdata + 12);
406 elf_tdata (abfd)->core->program
407 = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
408 elf_tdata (abfd)->core->command
409 = _bfd_elfcore_strndup (abfd, note->descdata + 48, 80);
410 break;
412 case 136:
413 /* sizeof (struct elf_prpsinfo) on Linux/x86_64. */
414 elf_tdata (abfd)->core->pid
415 = bfd_get_32 (abfd, note->descdata + 24);
416 elf_tdata (abfd)->core->program
417 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
418 elf_tdata (abfd)->core->command
419 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
422 /* Note that for some reason, a spurious space is tacked
423 onto the end of the args in some (at least one anyway)
424 implementations, so strip it off if it exists. */
427 char *command = elf_tdata (abfd)->core->command;
428 int n = strlen (command);
430 if (0 < n && command[n - 1] == ' ')
431 command[n - 1] = '\0';
434 return true;
437 #ifdef CORE_HEADER
438 # if GCC_VERSION >= 8000
439 # pragma GCC diagnostic push
440 # pragma GCC diagnostic ignored "-Wstringop-truncation"
441 # endif
442 static char *
443 elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
444 int note_type, ...)
446 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
447 va_list ap;
448 const char *fname, *psargs;
449 long pid;
450 int cursig;
451 const void *gregs;
453 switch (note_type)
455 default:
456 return NULL;
458 case NT_PRPSINFO:
459 va_start (ap, note_type);
460 fname = va_arg (ap, const char *);
461 psargs = va_arg (ap, const char *);
462 va_end (ap);
464 if (bed->s->elfclass == ELFCLASS32)
466 prpsinfo32_t data;
467 memset (&data, 0, sizeof (data));
468 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
469 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
470 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
471 &data, sizeof (data));
473 else
475 prpsinfo64_t data;
476 memset (&data, 0, sizeof (data));
477 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
478 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
479 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
480 &data, sizeof (data));
482 /* NOTREACHED */
484 case NT_PRSTATUS:
485 va_start (ap, note_type);
486 pid = va_arg (ap, long);
487 cursig = va_arg (ap, int);
488 gregs = va_arg (ap, const void *);
489 va_end (ap);
491 if (bed->s->elfclass == ELFCLASS32)
493 if (bed->elf_machine_code == EM_X86_64)
495 prstatusx32_t prstat;
496 memset (&prstat, 0, sizeof (prstat));
497 prstat.pr_pid = pid;
498 prstat.pr_cursig = cursig;
499 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
500 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
501 &prstat, sizeof (prstat));
503 else
505 prstatus32_t prstat;
506 memset (&prstat, 0, sizeof (prstat));
507 prstat.pr_pid = pid;
508 prstat.pr_cursig = cursig;
509 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
510 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
511 &prstat, sizeof (prstat));
514 else
516 prstatus64_t prstat;
517 memset (&prstat, 0, sizeof (prstat));
518 prstat.pr_pid = pid;
519 prstat.pr_cursig = cursig;
520 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
521 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
522 &prstat, sizeof (prstat));
525 /* NOTREACHED */
527 # if GCC_VERSION >= 8000
528 # pragma GCC diagnostic pop
529 # endif
530 #endif
532 /* Functions for the x86-64 ELF linker. */
534 /* The size in bytes of an entry in the global offset table. */
536 #define GOT_ENTRY_SIZE 8
538 /* The size in bytes of an entry in the lazy procedure linkage table. */
540 #define LAZY_PLT_ENTRY_SIZE 16
542 /* The size in bytes of an entry in the non-lazy procedure linkage
543 table. */
545 #define NON_LAZY_PLT_ENTRY_SIZE 8
547 /* The first entry in a lazy procedure linkage table looks like this.
548 See the SVR4 ABI i386 supplement and the x86-64 ABI to see how this
549 works. */
551 static const bfd_byte elf_x86_64_lazy_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
553 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
554 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
555 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
558 /* Subsequent entries in a lazy procedure linkage table look like this. */
560 static const bfd_byte elf_x86_64_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
562 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
563 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
564 0x68, /* pushq immediate */
565 0, 0, 0, 0, /* replaced with index into relocation table. */
566 0xe9, /* jmp relative */
567 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
570 /* The first entry in a lazy procedure linkage table with BND prefix
571 like this. */
573 static const bfd_byte elf_x86_64_lazy_bnd_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
575 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
576 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
577 0x0f, 0x1f, 0 /* nopl (%rax) */
580 /* Subsequent entries for branches with BND prefx in a lazy procedure
581 linkage table look like this. */
583 static const bfd_byte elf_x86_64_lazy_bnd_plt_entry[LAZY_PLT_ENTRY_SIZE] =
585 0x68, 0, 0, 0, 0, /* pushq immediate */
586 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
587 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
590 /* The first entry in the IBT-enabled lazy procedure linkage table is the
591 the same as the lazy PLT with BND prefix so that bound registers are
592 preserved when control is passed to dynamic linker. Subsequent
593 entries for a IBT-enabled lazy procedure linkage table look like
594 this. */
596 static const bfd_byte elf_x86_64_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
598 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
599 0x68, 0, 0, 0, 0, /* pushq immediate */
600 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
601 0x90 /* nop */
604 /* The first entry in the x32 IBT-enabled lazy procedure linkage table
605 is the same as the normal lazy PLT. Subsequent entries for an
606 x32 IBT-enabled lazy procedure linkage table look like this. */
608 static const bfd_byte elf_x32_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
610 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
611 0x68, 0, 0, 0, 0, /* pushq immediate */
612 0xe9, 0, 0, 0, 0, /* jmpq relative */
613 0x66, 0x90 /* xchg %ax,%ax */
616 /* Entries in the non-lazey procedure linkage table look like this. */
618 static const bfd_byte elf_x86_64_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
620 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
621 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
622 0x66, 0x90 /* xchg %ax,%ax */
625 /* Entries for branches with BND prefix in the non-lazey procedure
626 linkage table look like this. */
628 static const bfd_byte elf_x86_64_non_lazy_bnd_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
630 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
631 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
632 0x90 /* nop */
635 /* Entries for branches with IBT-enabled in the non-lazey procedure
636 linkage table look like this. They have the same size as the lazy
637 PLT entry. */
639 static const bfd_byte elf_x86_64_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
641 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
642 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
643 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
644 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopl 0x0(%rax,%rax,1) */
647 /* Entries for branches with IBT-enabled in the x32 non-lazey procedure
648 linkage table look like this. They have the same size as the lazy
649 PLT entry. */
651 static const bfd_byte elf_x32_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
653 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
654 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
655 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
656 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
659 /* The TLSDESC entry in a lazy procedure linkage table. */
660 static const bfd_byte elf_x86_64_tlsdesc_plt_entry[LAZY_PLT_ENTRY_SIZE] =
662 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
663 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
664 0xff, 0x25, 16, 0, 0, 0 /* jmpq *GOT+TDG(%rip) */
667 /* .eh_frame covering the lazy .plt section. */
669 static const bfd_byte elf_x86_64_eh_frame_lazy_plt[] =
671 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
672 0, 0, 0, 0, /* CIE ID */
673 1, /* CIE version */
674 'z', 'R', 0, /* Augmentation string */
675 1, /* Code alignment factor */
676 0x78, /* Data alignment factor */
677 16, /* Return address column */
678 1, /* Augmentation size */
679 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
680 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
681 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
682 DW_CFA_nop, DW_CFA_nop,
684 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
685 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
686 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
687 0, 0, 0, 0, /* .plt size goes here */
688 0, /* Augmentation size */
689 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
690 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
691 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
692 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
693 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
694 11, /* Block length */
695 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
696 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
697 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
698 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
699 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
702 /* .eh_frame covering the lazy BND .plt section. */
704 static const bfd_byte elf_x86_64_eh_frame_lazy_bnd_plt[] =
706 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
707 0, 0, 0, 0, /* CIE ID */
708 1, /* CIE version */
709 'z', 'R', 0, /* Augmentation string */
710 1, /* Code alignment factor */
711 0x78, /* Data alignment factor */
712 16, /* Return address column */
713 1, /* Augmentation size */
714 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
715 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
716 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
717 DW_CFA_nop, DW_CFA_nop,
719 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
720 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
721 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
722 0, 0, 0, 0, /* .plt size goes here */
723 0, /* Augmentation size */
724 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
725 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
726 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
727 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
728 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
729 11, /* Block length */
730 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
731 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
732 DW_OP_lit15, DW_OP_and, DW_OP_lit5, DW_OP_ge,
733 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
734 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
737 /* .eh_frame covering the lazy .plt section with IBT-enabled. */
739 static const bfd_byte elf_x86_64_eh_frame_lazy_ibt_plt[] =
741 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
742 0, 0, 0, 0, /* CIE ID */
743 1, /* CIE version */
744 'z', 'R', 0, /* Augmentation string */
745 1, /* Code alignment factor */
746 0x78, /* Data alignment factor */
747 16, /* Return address column */
748 1, /* Augmentation size */
749 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
750 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
751 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
752 DW_CFA_nop, DW_CFA_nop,
754 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
755 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
756 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
757 0, 0, 0, 0, /* .plt size goes here */
758 0, /* Augmentation size */
759 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
760 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
761 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
762 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
763 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
764 11, /* Block length */
765 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
766 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
767 DW_OP_lit15, DW_OP_and, DW_OP_lit10, DW_OP_ge,
768 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
769 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
772 /* .eh_frame covering the x32 lazy .plt section with IBT-enabled. */
774 static const bfd_byte elf_x32_eh_frame_lazy_ibt_plt[] =
776 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
777 0, 0, 0, 0, /* CIE ID */
778 1, /* CIE version */
779 'z', 'R', 0, /* Augmentation string */
780 1, /* Code alignment factor */
781 0x78, /* Data alignment factor */
782 16, /* Return address column */
783 1, /* Augmentation size */
784 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
785 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
786 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
787 DW_CFA_nop, DW_CFA_nop,
789 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
790 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
791 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
792 0, 0, 0, 0, /* .plt size goes here */
793 0, /* Augmentation size */
794 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
795 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
796 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
797 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
798 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
799 11, /* Block length */
800 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
801 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
802 DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
803 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
804 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
807 /* .eh_frame covering the non-lazy .plt section. */
809 static const bfd_byte elf_x86_64_eh_frame_non_lazy_plt[] =
811 #define PLT_GOT_FDE_LENGTH 20
812 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
813 0, 0, 0, 0, /* CIE ID */
814 1, /* CIE version */
815 'z', 'R', 0, /* Augmentation string */
816 1, /* Code alignment factor */
817 0x78, /* Data alignment factor */
818 16, /* Return address column */
819 1, /* Augmentation size */
820 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
821 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
822 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
823 DW_CFA_nop, DW_CFA_nop,
825 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
826 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
827 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
828 0, 0, 0, 0, /* non-lazy .plt size goes here */
829 0, /* Augmentation size */
830 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop,
831 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
834 static const sframe_frame_row_entry elf_x86_64_sframe_null_fre =
837 {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
838 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
841 /* .sframe FRE covering the .plt section entry. */
842 static const sframe_frame_row_entry elf_x86_64_sframe_plt0_fre1 =
844 0, /* SFrame FRE start address. */
845 {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
846 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
849 /* .sframe FRE covering the .plt section entry. */
850 static const sframe_frame_row_entry elf_x86_64_sframe_plt0_fre2 =
852 6, /* SFrame FRE start address. */
853 {24, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
854 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
857 /* .sframe FRE covering the .plt section entry. */
858 static const sframe_frame_row_entry elf_x86_64_sframe_pltn_fre1 =
860 0, /* SFrame FRE start address. */
861 {8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
862 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
865 /* .sframe FRE covering the .plt section entry. */
866 static const sframe_frame_row_entry elf_x86_64_sframe_pltn_fre2 =
868 11, /* SFrame FRE start address. */
869 {16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
870 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
873 /* .sframe FRE covering the second .plt section entry. */
874 static const sframe_frame_row_entry elf_x86_64_sframe_sec_pltn_fre1 =
876 0, /* SFrame FRE start address. */
877 {8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, /* 12 bytes. */
878 SFRAME_V1_FRE_INFO (SFRAME_BASE_REG_SP, 1, SFRAME_FRE_OFFSET_1B) /* FRE info. */
881 /* SFrame helper object for non-lazy PLT. Also used for IBT enabled PLT. */
882 static const struct elf_x86_sframe_plt elf_x86_64_sframe_non_lazy_plt =
884 LAZY_PLT_ENTRY_SIZE,
885 2, /* Number of FREs for PLT0. */
886 /* Array of SFrame FREs for plt0. */
887 { &elf_x86_64_sframe_plt0_fre1, &elf_x86_64_sframe_plt0_fre2 },
888 LAZY_PLT_ENTRY_SIZE,
889 1, /* Number of FREs for PLTn. */
890 /* Array of SFrame FREs for plt. */
891 { &elf_x86_64_sframe_sec_pltn_fre1, &elf_x86_64_sframe_null_fre },
893 0, /* There is no second PLT necessary. */
894 { &elf_x86_64_sframe_null_fre }
897 /* SFrame helper object for lazy PLT. Also used for IBT enabled PLT. */
898 static const struct elf_x86_sframe_plt elf_x86_64_sframe_plt =
900 LAZY_PLT_ENTRY_SIZE,
901 2, /* Number of FREs for PLT0. */
902 /* Array of SFrame FREs for plt0. */
903 { &elf_x86_64_sframe_plt0_fre1, &elf_x86_64_sframe_plt0_fre2 },
904 LAZY_PLT_ENTRY_SIZE,
905 2, /* Number of FREs for PLTn. */
906 /* Array of SFrame FREs for plt. */
907 { &elf_x86_64_sframe_pltn_fre1, &elf_x86_64_sframe_pltn_fre2 },
908 NON_LAZY_PLT_ENTRY_SIZE,
909 1, /* Number of FREs for PLTn for second PLT. */
910 /* FREs for second plt (stack trace info for .plt.got is
911 identical). Used when IBT or non-lazy PLT is in effect. */
912 { &elf_x86_64_sframe_sec_pltn_fre1 }
915 /* These are the standard parameters. */
916 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_plt =
918 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
919 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
920 elf_x86_64_lazy_plt_entry, /* plt_entry */
921 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
922 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
923 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
924 6, /* plt_tlsdesc_got1_offset */
925 12, /* plt_tlsdesc_got2_offset */
926 10, /* plt_tlsdesc_got1_insn_end */
927 16, /* plt_tlsdesc_got2_insn_end */
928 2, /* plt0_got1_offset */
929 8, /* plt0_got2_offset */
930 12, /* plt0_got2_insn_end */
931 2, /* plt_got_offset */
932 7, /* plt_reloc_offset */
933 12, /* plt_plt_offset */
934 6, /* plt_got_insn_size */
935 LAZY_PLT_ENTRY_SIZE, /* plt_plt_insn_end */
936 6, /* plt_lazy_offset */
937 elf_x86_64_lazy_plt0_entry, /* pic_plt0_entry */
938 elf_x86_64_lazy_plt_entry, /* pic_plt_entry */
939 elf_x86_64_eh_frame_lazy_plt, /* eh_frame_plt */
940 sizeof (elf_x86_64_eh_frame_lazy_plt) /* eh_frame_plt_size */
943 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_plt =
945 elf_x86_64_non_lazy_plt_entry, /* plt_entry */
946 elf_x86_64_non_lazy_plt_entry, /* pic_plt_entry */
947 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
948 2, /* plt_got_offset */
949 6, /* plt_got_insn_size */
950 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
951 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
954 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_bnd_plt =
956 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
957 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
958 elf_x86_64_lazy_bnd_plt_entry, /* plt_entry */
959 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
960 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
961 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
962 6, /* plt_tlsdesc_got1_offset */
963 12, /* plt_tlsdesc_got2_offset */
964 10, /* plt_tlsdesc_got1_insn_end */
965 16, /* plt_tlsdesc_got2_insn_end */
966 2, /* plt0_got1_offset */
967 1+8, /* plt0_got2_offset */
968 1+12, /* plt0_got2_insn_end */
969 1+2, /* plt_got_offset */
970 1, /* plt_reloc_offset */
971 7, /* plt_plt_offset */
972 1+6, /* plt_got_insn_size */
973 11, /* plt_plt_insn_end */
974 0, /* plt_lazy_offset */
975 elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
976 elf_x86_64_lazy_bnd_plt_entry, /* pic_plt_entry */
977 elf_x86_64_eh_frame_lazy_bnd_plt, /* eh_frame_plt */
978 sizeof (elf_x86_64_eh_frame_lazy_bnd_plt) /* eh_frame_plt_size */
981 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_bnd_plt =
983 elf_x86_64_non_lazy_bnd_plt_entry, /* plt_entry */
984 elf_x86_64_non_lazy_bnd_plt_entry, /* pic_plt_entry */
985 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
986 1+2, /* plt_got_offset */
987 1+6, /* plt_got_insn_size */
988 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
989 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
992 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_ibt_plt =
994 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
995 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
996 elf_x86_64_lazy_ibt_plt_entry, /* plt_entry */
997 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
998 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
999 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
1000 6, /* plt_tlsdesc_got1_offset */
1001 12, /* plt_tlsdesc_got2_offset */
1002 10, /* plt_tlsdesc_got1_insn_end */
1003 16, /* plt_tlsdesc_got2_insn_end */
1004 2, /* plt0_got1_offset */
1005 1+8, /* plt0_got2_offset */
1006 1+12, /* plt0_got2_insn_end */
1007 4+1+2, /* plt_got_offset */
1008 4+1, /* plt_reloc_offset */
1009 4+1+6, /* plt_plt_offset */
1010 4+1+6, /* plt_got_insn_size */
1011 4+1+5+5, /* plt_plt_insn_end */
1012 0, /* plt_lazy_offset */
1013 elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
1014 elf_x86_64_lazy_ibt_plt_entry, /* pic_plt_entry */
1015 elf_x86_64_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
1016 sizeof (elf_x86_64_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
1019 static const struct elf_x86_lazy_plt_layout elf_x32_lazy_ibt_plt =
1021 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
1022 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
1023 elf_x32_lazy_ibt_plt_entry, /* plt_entry */
1024 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1025 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
1026 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
1027 6, /* plt_tlsdesc_got1_offset */
1028 12, /* plt_tlsdesc_got2_offset */
1029 10, /* plt_tlsdesc_got1_insn_end */
1030 16, /* plt_tlsdesc_got2_insn_end */
1031 2, /* plt0_got1_offset */
1032 8, /* plt0_got2_offset */
1033 12, /* plt0_got2_insn_end */
1034 4+2, /* plt_got_offset */
1035 4+1, /* plt_reloc_offset */
1036 4+6, /* plt_plt_offset */
1037 4+6, /* plt_got_insn_size */
1038 4+5+5, /* plt_plt_insn_end */
1039 0, /* plt_lazy_offset */
1040 elf_x86_64_lazy_plt0_entry, /* pic_plt0_entry */
1041 elf_x32_lazy_ibt_plt_entry, /* pic_plt_entry */
1042 elf_x32_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
1043 sizeof (elf_x32_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
1046 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_ibt_plt =
1048 elf_x86_64_non_lazy_ibt_plt_entry, /* plt_entry */
1049 elf_x86_64_non_lazy_ibt_plt_entry, /* pic_plt_entry */
1050 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1051 4+1+2, /* plt_got_offset */
1052 4+1+6, /* plt_got_insn_size */
1053 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
1054 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1057 static const struct elf_x86_non_lazy_plt_layout elf_x32_non_lazy_ibt_plt =
1059 elf_x32_non_lazy_ibt_plt_entry, /* plt_entry */
1060 elf_x32_non_lazy_ibt_plt_entry, /* pic_plt_entry */
1061 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
1062 4+2, /* plt_got_offset */
1063 4+6, /* plt_got_insn_size */
1064 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
1065 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
1068 static bool
1069 elf64_x86_64_elf_object_p (bfd *abfd)
1071 /* Set the right machine number for an x86-64 elf64 file. */
1072 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
1073 return true;
1076 static bool
1077 elf32_x86_64_elf_object_p (bfd *abfd)
1079 /* Set the right machine number for an x86-64 elf32 file. */
1080 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
1081 return true;
1084 /* Return TRUE if the TLS access code sequence support transition
1085 from R_TYPE. */
1087 static bool
1088 elf_x86_64_check_tls_transition (bfd *abfd,
1089 struct bfd_link_info *info,
1090 asection *sec,
1091 bfd_byte *contents,
1092 Elf_Internal_Shdr *symtab_hdr,
1093 struct elf_link_hash_entry **sym_hashes,
1094 unsigned int r_type,
1095 const Elf_Internal_Rela *rel,
1096 const Elf_Internal_Rela *relend)
1098 unsigned int val;
1099 unsigned long r_symndx;
1100 bool largepic = false;
1101 struct elf_link_hash_entry *h;
1102 bfd_vma offset;
1103 struct elf_x86_link_hash_table *htab;
1104 bfd_byte *call;
1105 bool indirect_call;
1107 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1108 offset = rel->r_offset;
1109 switch (r_type)
1111 case R_X86_64_TLSGD:
1112 case R_X86_64_TLSLD:
1113 if ((rel + 1) >= relend)
1114 return false;
1116 if (r_type == R_X86_64_TLSGD)
1118 /* Check transition from GD access model. For 64bit, only
1119 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1120 .word 0x6666; rex64; call __tls_get_addr@PLT
1122 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1123 .byte 0x66; rex64
1124 call *__tls_get_addr@GOTPCREL(%rip)
1125 which may be converted to
1126 addr32 call __tls_get_addr
1127 can transit to different access model. For 32bit, only
1128 leaq foo@tlsgd(%rip), %rdi
1129 .word 0x6666; rex64; call __tls_get_addr@PLT
1131 leaq foo@tlsgd(%rip), %rdi
1132 .byte 0x66; rex64
1133 call *__tls_get_addr@GOTPCREL(%rip)
1134 which may be converted to
1135 addr32 call __tls_get_addr
1136 can transit to different access model. For largepic,
1137 we also support:
1138 leaq foo@tlsgd(%rip), %rdi
1139 movabsq $__tls_get_addr@pltoff, %rax
1140 addq $r15, %rax
1141 call *%rax
1143 leaq foo@tlsgd(%rip), %rdi
1144 movabsq $__tls_get_addr@pltoff, %rax
1145 addq $rbx, %rax
1146 call *%rax */
1148 static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1150 if ((offset + 12) > sec->size)
1151 return false;
1153 call = contents + offset + 4;
1154 if (call[0] != 0x66
1155 || !((call[1] == 0x48
1156 && call[2] == 0xff
1157 && call[3] == 0x15)
1158 || (call[1] == 0x48
1159 && call[2] == 0x67
1160 && call[3] == 0xe8)
1161 || (call[1] == 0x66
1162 && call[2] == 0x48
1163 && call[3] == 0xe8)))
1165 if (!ABI_64_P (abfd)
1166 || (offset + 19) > sec->size
1167 || offset < 3
1168 || memcmp (call - 7, leaq + 1, 3) != 0
1169 || memcmp (call, "\x48\xb8", 2) != 0
1170 || call[11] != 0x01
1171 || call[13] != 0xff
1172 || call[14] != 0xd0
1173 || !((call[10] == 0x48 && call[12] == 0xd8)
1174 || (call[10] == 0x4c && call[12] == 0xf8)))
1175 return false;
1176 largepic = true;
1178 else if (ABI_64_P (abfd))
1180 if (offset < 4
1181 || memcmp (contents + offset - 4, leaq, 4) != 0)
1182 return false;
1184 else
1186 if (offset < 3
1187 || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
1188 return false;
1190 indirect_call = call[2] == 0xff;
1192 else
1194 /* Check transition from LD access model. Only
1195 leaq foo@tlsld(%rip), %rdi;
1196 call __tls_get_addr@PLT
1198 leaq foo@tlsld(%rip), %rdi;
1199 call *__tls_get_addr@GOTPCREL(%rip)
1200 which may be converted to
1201 addr32 call __tls_get_addr
1202 can transit to different access model. For largepic
1203 we also support:
1204 leaq foo@tlsld(%rip), %rdi
1205 movabsq $__tls_get_addr@pltoff, %rax
1206 addq $r15, %rax
1207 call *%rax
1209 leaq foo@tlsld(%rip), %rdi
1210 movabsq $__tls_get_addr@pltoff, %rax
1211 addq $rbx, %rax
1212 call *%rax */
1214 static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
1216 if (offset < 3 || (offset + 9) > sec->size)
1217 return false;
1219 if (memcmp (contents + offset - 3, lea, 3) != 0)
1220 return false;
1222 call = contents + offset + 4;
1223 if (!(call[0] == 0xe8
1224 || (call[0] == 0xff && call[1] == 0x15)
1225 || (call[0] == 0x67 && call[1] == 0xe8)))
1227 if (!ABI_64_P (abfd)
1228 || (offset + 19) > sec->size
1229 || memcmp (call, "\x48\xb8", 2) != 0
1230 || call[11] != 0x01
1231 || call[13] != 0xff
1232 || call[14] != 0xd0
1233 || !((call[10] == 0x48 && call[12] == 0xd8)
1234 || (call[10] == 0x4c && call[12] == 0xf8)))
1235 return false;
1236 largepic = true;
1238 indirect_call = call[0] == 0xff;
1241 r_symndx = htab->r_sym (rel[1].r_info);
1242 if (r_symndx < symtab_hdr->sh_info)
1243 return false;
1245 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1246 if (h == NULL
1247 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
1248 return false;
1249 else
1251 r_type = (ELF32_R_TYPE (rel[1].r_info)
1252 & ~R_X86_64_converted_reloc_bit);
1253 if (largepic)
1254 return r_type == R_X86_64_PLTOFF64;
1255 else if (indirect_call)
1256 return (r_type == R_X86_64_GOTPCRELX || r_type == R_X86_64_GOTPCREL);
1257 else
1258 return (r_type == R_X86_64_PC32 || r_type == R_X86_64_PLT32);
1261 case R_X86_64_GOTTPOFF:
1262 /* Check transition from IE access model:
1263 mov foo@gottpoff(%rip), %reg
1264 add foo@gottpoff(%rip), %reg
1267 /* Check REX prefix first. */
1268 if (offset >= 3 && (offset + 4) <= sec->size)
1270 val = bfd_get_8 (abfd, contents + offset - 3);
1271 if (val != 0x48 && val != 0x4c)
1273 /* X32 may have 0x44 REX prefix or no REX prefix. */
1274 if (ABI_64_P (abfd))
1275 return false;
1278 else
1280 /* X32 may not have any REX prefix. */
1281 if (ABI_64_P (abfd))
1282 return false;
1283 if (offset < 2 || (offset + 3) > sec->size)
1284 return false;
1287 val = bfd_get_8 (abfd, contents + offset - 2);
1288 if (val != 0x8b && val != 0x03)
1289 return false;
1291 val = bfd_get_8 (abfd, contents + offset - 1);
1292 return (val & 0xc7) == 5;
1294 case R_X86_64_GOTPC32_TLSDESC:
1295 /* Check transition from GDesc access model:
1296 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
1297 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
1299 Make sure it's a leaq adding rip to a 32-bit offset
1300 into any register, although it's probably almost always
1301 going to be rax. */
1303 if (offset < 3 || (offset + 4) > sec->size)
1304 return false;
1306 val = bfd_get_8 (abfd, contents + offset - 3);
1307 val &= 0xfb;
1308 if (val != 0x48 && (ABI_64_P (abfd) || val != 0x40))
1309 return false;
1311 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1312 return false;
1314 val = bfd_get_8 (abfd, contents + offset - 1);
1315 return (val & 0xc7) == 0x05;
1317 case R_X86_64_TLSDESC_CALL:
1318 /* Check transition from GDesc access model:
1319 call *x@tlsdesc(%rax) <--- LP64 mode.
1320 call *x@tlsdesc(%eax) <--- X32 mode.
1322 if (offset + 2 <= sec->size)
1324 unsigned int prefix;
1325 call = contents + offset;
1326 prefix = 0;
1327 if (!ABI_64_P (abfd))
1329 /* Check for call *x@tlsdesc(%eax). */
1330 if (call[0] == 0x67)
1332 prefix = 1;
1333 if (offset + 3 > sec->size)
1334 return false;
1337 /* Make sure that it's a call *x@tlsdesc(%rax). */
1338 return call[prefix] == 0xff && call[1 + prefix] == 0x10;
1341 return false;
1343 default:
1344 abort ();
1348 /* Return TRUE if the TLS access transition is OK or no transition
1349 will be performed. Update R_TYPE if there is a transition. */
1351 static bool
1352 elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1353 asection *sec, bfd_byte *contents,
1354 Elf_Internal_Shdr *symtab_hdr,
1355 struct elf_link_hash_entry **sym_hashes,
1356 unsigned int *r_type, int tls_type,
1357 const Elf_Internal_Rela *rel,
1358 const Elf_Internal_Rela *relend,
1359 struct elf_link_hash_entry *h,
1360 unsigned long r_symndx,
1361 bool from_relocate_section)
1363 unsigned int from_type = *r_type;
1364 unsigned int to_type = from_type;
1365 bool check = true;
1367 /* Skip TLS transition for functions. */
1368 if (h != NULL
1369 && (h->type == STT_FUNC
1370 || h->type == STT_GNU_IFUNC))
1371 return true;
1373 switch (from_type)
1375 case R_X86_64_TLSGD:
1376 case R_X86_64_GOTPC32_TLSDESC:
1377 case R_X86_64_TLSDESC_CALL:
1378 case R_X86_64_GOTTPOFF:
1379 if (bfd_link_executable (info))
1381 if (h == NULL)
1382 to_type = R_X86_64_TPOFF32;
1383 else
1384 to_type = R_X86_64_GOTTPOFF;
1387 /* When we are called from elf_x86_64_relocate_section, there may
1388 be additional transitions based on TLS_TYPE. */
1389 if (from_relocate_section)
1391 unsigned int new_to_type = to_type;
1393 if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
1394 new_to_type = R_X86_64_TPOFF32;
1396 if (to_type == R_X86_64_TLSGD
1397 || to_type == R_X86_64_GOTPC32_TLSDESC
1398 || to_type == R_X86_64_TLSDESC_CALL)
1400 if (tls_type == GOT_TLS_IE)
1401 new_to_type = R_X86_64_GOTTPOFF;
1404 /* We checked the transition before when we were called from
1405 elf_x86_64_scan_relocs. We only want to check the new
1406 transition which hasn't been checked before. */
1407 check = new_to_type != to_type && from_type == to_type;
1408 to_type = new_to_type;
1411 break;
1413 case R_X86_64_TLSLD:
1414 if (bfd_link_executable (info))
1415 to_type = R_X86_64_TPOFF32;
1416 break;
1418 default:
1419 return true;
1422 /* Return TRUE if there is no transition. */
1423 if (from_type == to_type)
1424 return true;
1426 /* Check if the transition can be performed. */
1427 if (check
1428 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1429 symtab_hdr, sym_hashes,
1430 from_type, rel, relend))
1432 reloc_howto_type *from, *to;
1433 const char *name;
1435 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1436 to = elf_x86_64_rtype_to_howto (abfd, to_type);
1438 if (from == NULL || to == NULL)
1439 return false;
1441 if (h)
1442 name = h->root.root.string;
1443 else
1445 struct elf_x86_link_hash_table *htab;
1447 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1448 if (htab == NULL)
1449 name = "*unknown*";
1450 else
1452 Elf_Internal_Sym *isym;
1454 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1455 abfd, r_symndx);
1456 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1460 _bfd_error_handler
1461 /* xgettext:c-format */
1462 (_("%pB: TLS transition from %s to %s against `%s' at %#" PRIx64
1463 " in section `%pA' failed"),
1464 abfd, from->name, to->name, name, (uint64_t) rel->r_offset, sec);
1465 bfd_set_error (bfd_error_bad_value);
1466 return false;
1469 *r_type = to_type;
1470 return true;
1473 static bool
1474 elf_x86_64_need_pic (struct bfd_link_info *info,
1475 bfd *input_bfd, asection *sec,
1476 struct elf_link_hash_entry *h,
1477 Elf_Internal_Shdr *symtab_hdr,
1478 Elf_Internal_Sym *isym,
1479 reloc_howto_type *howto)
1481 const char *v = "";
1482 const char *und = "";
1483 const char *pic = "";
1484 const char *object;
1486 const char *name;
1487 if (h)
1489 name = h->root.root.string;
1490 switch (ELF_ST_VISIBILITY (h->other))
1492 case STV_HIDDEN:
1493 v = _("hidden symbol ");
1494 break;
1495 case STV_INTERNAL:
1496 v = _("internal symbol ");
1497 break;
1498 case STV_PROTECTED:
1499 v = _("protected symbol ");
1500 break;
1501 default:
1502 if (((struct elf_x86_link_hash_entry *) h)->def_protected)
1503 v = _("protected symbol ");
1504 else
1505 v = _("symbol ");
1506 pic = NULL;
1507 break;
1510 if (!SYMBOL_DEFINED_NON_SHARED_P (h) && !h->def_dynamic)
1511 und = _("undefined ");
1513 else
1515 name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
1516 pic = NULL;
1519 if (bfd_link_dll (info))
1521 object = _("a shared object");
1522 if (!pic)
1523 pic = _("; recompile with -fPIC");
1525 else
1527 if (bfd_link_pie (info))
1528 object = _("a PIE object");
1529 else
1530 object = _("a PDE object");
1531 if (!pic)
1532 pic = _("; recompile with -fPIE");
1535 /* xgettext:c-format */
1536 _bfd_error_handler (_("%pB: relocation %s against %s%s`%s' can "
1537 "not be used when making %s%s"),
1538 input_bfd, howto->name, und, v, name,
1539 object, pic);
1540 bfd_set_error (bfd_error_bad_value);
1541 sec->check_relocs_failed = 1;
1542 return false;
1545 /* With the local symbol, foo, we convert
1546 mov foo@GOTPCREL(%rip), %reg
1548 lea foo(%rip), %reg
1549 and convert
1550 call/jmp *foo@GOTPCREL(%rip)
1552 nop call foo/jmp foo nop
1553 When PIC is false, convert
1554 test %reg, foo@GOTPCREL(%rip)
1556 test $foo, %reg
1557 and convert
1558 binop foo@GOTPCREL(%rip), %reg
1560 binop $foo, %reg
1561 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1562 instructions. */
1564 static bool
1565 elf_x86_64_convert_load_reloc (bfd *abfd,
1566 bfd_byte *contents,
1567 unsigned int *r_type_p,
1568 Elf_Internal_Rela *irel,
1569 struct elf_link_hash_entry *h,
1570 bool *converted,
1571 struct bfd_link_info *link_info)
1573 struct elf_x86_link_hash_table *htab;
1574 bool is_pic;
1575 bool no_overflow;
1576 bool relocx;
1577 bool to_reloc_pc32;
1578 bool abs_symbol;
1579 bool local_ref;
1580 asection *tsec;
1581 bfd_signed_vma raddend;
1582 unsigned int opcode;
1583 unsigned int modrm;
1584 unsigned int r_type = *r_type_p;
1585 unsigned int r_symndx;
1586 bfd_vma roff = irel->r_offset;
1587 bfd_vma abs_relocation;
1589 if (roff < (r_type == R_X86_64_REX_GOTPCRELX ? 3 : 2))
1590 return true;
1592 raddend = irel->r_addend;
1593 /* Addend for 32-bit PC-relative relocation must be -4. */
1594 if (raddend != -4)
1595 return true;
1597 htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
1598 is_pic = bfd_link_pic (link_info);
1600 relocx = (r_type == R_X86_64_GOTPCRELX
1601 || r_type == R_X86_64_REX_GOTPCRELX);
1603 /* TRUE if --no-relax is used. */
1604 no_overflow = link_info->disable_target_specific_optimizations > 1;
1606 r_symndx = htab->r_sym (irel->r_info);
1608 opcode = bfd_get_8 (abfd, contents + roff - 2);
1610 /* Convert mov to lea since it has been done for a while. */
1611 if (opcode != 0x8b)
1613 /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
1614 for call, jmp or one of adc, add, and, cmp, or, sbb, sub,
1615 test, xor instructions. */
1616 if (!relocx)
1617 return true;
1620 /* We convert only to R_X86_64_PC32:
1621 1. Branch.
1622 2. R_X86_64_GOTPCREL since we can't modify REX byte.
1623 3. no_overflow is true.
1624 4. PIC.
1626 to_reloc_pc32 = (opcode == 0xff
1627 || !relocx
1628 || no_overflow
1629 || is_pic);
1631 abs_symbol = false;
1632 abs_relocation = 0;
1634 /* Get the symbol referred to by the reloc. */
1635 if (h == NULL)
1637 Elf_Internal_Sym *isym
1638 = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
1640 /* Skip relocation against undefined symbols. */
1641 if (isym->st_shndx == SHN_UNDEF)
1642 return true;
1644 local_ref = true;
1645 if (isym->st_shndx == SHN_ABS)
1647 tsec = bfd_abs_section_ptr;
1648 abs_symbol = true;
1649 abs_relocation = isym->st_value;
1651 else if (isym->st_shndx == SHN_COMMON)
1652 tsec = bfd_com_section_ptr;
1653 else if (isym->st_shndx == SHN_X86_64_LCOMMON)
1654 tsec = &_bfd_elf_large_com_section;
1655 else
1656 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1658 else
1660 /* Undefined weak symbol is only bound locally in executable
1661 and its reference is resolved as 0 without relocation
1662 overflow. We can only perform this optimization for
1663 GOTPCRELX relocations since we need to modify REX byte.
1664 It is OK convert mov with R_X86_64_GOTPCREL to
1665 R_X86_64_PC32. */
1666 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
1668 abs_symbol = ABS_SYMBOL_P (h);
1669 abs_relocation = h->root.u.def.value;
1671 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1672 local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1673 if ((relocx || opcode == 0x8b)
1674 && (h->root.type == bfd_link_hash_undefweak
1675 && !eh->linker_def
1676 && local_ref))
1678 if (opcode == 0xff)
1680 /* Skip for branch instructions since R_X86_64_PC32
1681 may overflow. */
1682 if (no_overflow)
1683 return true;
1685 else if (relocx)
1687 /* For non-branch instructions, we can convert to
1688 R_X86_64_32/R_X86_64_32S since we know if there
1689 is a REX byte. */
1690 to_reloc_pc32 = false;
1693 /* Since we don't know the current PC when PIC is true,
1694 we can't convert to R_X86_64_PC32. */
1695 if (to_reloc_pc32 && is_pic)
1696 return true;
1698 goto convert;
1700 /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1701 ld.so may use its link-time address. */
1702 else if (h->start_stop
1703 || eh->linker_def
1704 || ((h->def_regular
1705 || h->root.type == bfd_link_hash_defined
1706 || h->root.type == bfd_link_hash_defweak)
1707 && h != htab->elf.hdynamic
1708 && local_ref))
1710 /* bfd_link_hash_new or bfd_link_hash_undefined is
1711 set by an assignment in a linker script in
1712 bfd_elf_record_link_assignment. start_stop is set
1713 on __start_SECNAME/__stop_SECNAME which mark section
1714 SECNAME. */
1715 if (h->start_stop
1716 || eh->linker_def
1717 || (h->def_regular
1718 && (h->root.type == bfd_link_hash_new
1719 || h->root.type == bfd_link_hash_undefined
1720 || ((h->root.type == bfd_link_hash_defined
1721 || h->root.type == bfd_link_hash_defweak)
1722 && h->root.u.def.section == bfd_und_section_ptr))))
1724 /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
1725 if (no_overflow)
1726 return true;
1727 goto convert;
1729 tsec = h->root.u.def.section;
1731 else
1732 return true;
1735 /* Don't convert GOTPCREL relocation against large section. */
1736 if (elf_section_data (tsec) != NULL
1737 && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
1738 return true;
1740 /* Skip since R_X86_64_PC32/R_X86_64_32/R_X86_64_32S may overflow. */
1741 if (no_overflow)
1742 return true;
1744 convert:
1745 if (opcode == 0xff)
1747 /* We have "call/jmp *foo@GOTPCREL(%rip)". */
1748 unsigned int nop;
1749 unsigned int disp;
1750 bfd_vma nop_offset;
1752 /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
1753 R_X86_64_PC32. */
1754 modrm = bfd_get_8 (abfd, contents + roff - 1);
1755 if (modrm == 0x25)
1757 /* Convert to "jmp foo nop". */
1758 modrm = 0xe9;
1759 nop = NOP_OPCODE;
1760 nop_offset = irel->r_offset + 3;
1761 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1762 irel->r_offset -= 1;
1763 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1765 else
1767 struct elf_x86_link_hash_entry *eh
1768 = (struct elf_x86_link_hash_entry *) h;
1770 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1771 is a nop prefix. */
1772 modrm = 0xe8;
1773 /* To support TLS optimization, always use addr32 prefix for
1774 "call *__tls_get_addr@GOTPCREL(%rip)". */
1775 if (eh && eh->tls_get_addr)
1777 nop = 0x67;
1778 nop_offset = irel->r_offset - 2;
1780 else
1782 nop = htab->params->call_nop_byte;
1783 if (htab->params->call_nop_as_suffix)
1785 nop_offset = irel->r_offset + 3;
1786 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1787 irel->r_offset -= 1;
1788 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1790 else
1791 nop_offset = irel->r_offset - 2;
1794 bfd_put_8 (abfd, nop, contents + nop_offset);
1795 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1796 r_type = R_X86_64_PC32;
1798 else
1800 unsigned int rex;
1801 unsigned int rex_mask = REX_R;
1803 if (r_type == R_X86_64_REX_GOTPCRELX)
1804 rex = bfd_get_8 (abfd, contents + roff - 3);
1805 else
1806 rex = 0;
1808 if (opcode == 0x8b)
1810 if (abs_symbol && local_ref && relocx)
1811 to_reloc_pc32 = false;
1813 if (to_reloc_pc32)
1815 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1816 "lea foo(%rip), %reg". */
1817 opcode = 0x8d;
1818 r_type = R_X86_64_PC32;
1820 else
1822 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1823 "mov $foo, %reg". */
1824 opcode = 0xc7;
1825 modrm = bfd_get_8 (abfd, contents + roff - 1);
1826 modrm = 0xc0 | (modrm & 0x38) >> 3;
1827 if ((rex & REX_W) != 0
1828 && ABI_64_P (link_info->output_bfd))
1830 /* Keep the REX_W bit in REX byte for LP64. */
1831 r_type = R_X86_64_32S;
1832 goto rewrite_modrm_rex;
1834 else
1836 /* If the REX_W bit in REX byte isn't needed,
1837 use R_X86_64_32 and clear the W bit to avoid
1838 sign-extend imm32 to imm64. */
1839 r_type = R_X86_64_32;
1840 /* Clear the W bit in REX byte. */
1841 rex_mask |= REX_W;
1842 goto rewrite_modrm_rex;
1846 else
1848 /* R_X86_64_PC32 isn't supported. */
1849 if (to_reloc_pc32)
1850 return true;
1852 modrm = bfd_get_8 (abfd, contents + roff - 1);
1853 if (opcode == 0x85)
1855 /* Convert "test %reg, foo@GOTPCREL(%rip)" to
1856 "test $foo, %reg". */
1857 modrm = 0xc0 | (modrm & 0x38) >> 3;
1858 opcode = 0xf7;
1860 else
1862 /* Convert "binop foo@GOTPCREL(%rip), %reg" to
1863 "binop $foo, %reg". */
1864 modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
1865 opcode = 0x81;
1868 /* Use R_X86_64_32 with 32-bit operand to avoid relocation
1869 overflow when sign-extending imm32 to imm64. */
1870 r_type = (rex & REX_W) != 0 ? R_X86_64_32S : R_X86_64_32;
1872 rewrite_modrm_rex:
1873 if (abs_relocation)
1875 /* Check if R_X86_64_32S/R_X86_64_32 fits. */
1876 if (r_type == R_X86_64_32S)
1878 if ((abs_relocation + 0x80000000) > 0xffffffff)
1879 return true;
1881 else
1883 if (abs_relocation > 0xffffffff)
1884 return true;
1888 bfd_put_8 (abfd, modrm, contents + roff - 1);
1890 if (rex)
1892 /* Move the R bit to the B bit in REX byte. */
1893 rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
1894 bfd_put_8 (abfd, rex, contents + roff - 3);
1897 /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
1898 irel->r_addend = 0;
1901 bfd_put_8 (abfd, opcode, contents + roff - 2);
1904 *r_type_p = r_type;
1905 irel->r_info = htab->r_info (r_symndx,
1906 r_type | R_X86_64_converted_reloc_bit);
1908 *converted = true;
1910 return true;
1913 /* Look through the relocs for a section during the first phase, and
1914 calculate needed space in the global offset table, and procedure
1915 linkage table. */
1917 static bool
1918 elf_x86_64_scan_relocs (bfd *abfd, struct bfd_link_info *info,
1919 asection *sec,
1920 const Elf_Internal_Rela *relocs)
1922 struct elf_x86_link_hash_table *htab;
1923 Elf_Internal_Shdr *symtab_hdr;
1924 struct elf_link_hash_entry **sym_hashes;
1925 const Elf_Internal_Rela *rel;
1926 const Elf_Internal_Rela *rel_end;
1927 bfd_byte *contents;
1928 bool converted;
1930 if (bfd_link_relocatable (info))
1931 return true;
1933 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1934 if (htab == NULL)
1936 sec->check_relocs_failed = 1;
1937 return false;
1940 BFD_ASSERT (is_x86_elf (abfd, htab));
1942 /* Get the section contents. */
1943 if (elf_section_data (sec)->this_hdr.contents != NULL)
1944 contents = elf_section_data (sec)->this_hdr.contents;
1945 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1947 sec->check_relocs_failed = 1;
1948 return false;
1951 symtab_hdr = &elf_symtab_hdr (abfd);
1952 sym_hashes = elf_sym_hashes (abfd);
1954 converted = false;
1956 rel_end = relocs + sec->reloc_count;
1957 for (rel = relocs; rel < rel_end; rel++)
1959 unsigned int r_type;
1960 unsigned int r_symndx;
1961 struct elf_link_hash_entry *h;
1962 struct elf_x86_link_hash_entry *eh;
1963 Elf_Internal_Sym *isym;
1964 const char *name;
1965 bool size_reloc;
1966 bool converted_reloc;
1967 bool no_dynreloc;
1969 r_symndx = htab->r_sym (rel->r_info);
1970 r_type = ELF32_R_TYPE (rel->r_info);
1972 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1974 /* xgettext:c-format */
1975 _bfd_error_handler (_("%pB: bad symbol index: %d"),
1976 abfd, r_symndx);
1977 goto error_return;
1980 if (r_symndx < symtab_hdr->sh_info)
1982 /* A local symbol. */
1983 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1984 abfd, r_symndx);
1985 if (isym == NULL)
1986 goto error_return;
1988 /* Check relocation against local STT_GNU_IFUNC symbol. */
1989 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1991 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel,
1992 true);
1993 if (h == NULL)
1994 goto error_return;
1996 /* Fake a STT_GNU_IFUNC symbol. */
1997 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1998 isym, NULL);
1999 h->type = STT_GNU_IFUNC;
2000 h->def_regular = 1;
2001 h->ref_regular = 1;
2002 h->forced_local = 1;
2003 h->root.type = bfd_link_hash_defined;
2005 else
2006 h = NULL;
2008 else
2010 isym = NULL;
2011 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2012 while (h->root.type == bfd_link_hash_indirect
2013 || h->root.type == bfd_link_hash_warning)
2014 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2017 /* Check invalid x32 relocations. */
2018 if (!ABI_64_P (abfd))
2019 switch (r_type)
2021 default:
2022 break;
2024 case R_X86_64_DTPOFF64:
2025 case R_X86_64_TPOFF64:
2026 case R_X86_64_PC64:
2027 case R_X86_64_GOTOFF64:
2028 case R_X86_64_GOT64:
2029 case R_X86_64_GOTPCREL64:
2030 case R_X86_64_GOTPC64:
2031 case R_X86_64_GOTPLT64:
2032 case R_X86_64_PLTOFF64:
2034 if (h)
2035 name = h->root.root.string;
2036 else
2037 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
2038 NULL);
2039 _bfd_error_handler
2040 /* xgettext:c-format */
2041 (_("%pB: relocation %s against symbol `%s' isn't "
2042 "supported in x32 mode"), abfd,
2043 x86_64_elf_howto_table[r_type].name, name);
2044 bfd_set_error (bfd_error_bad_value);
2045 goto error_return;
2047 break;
2050 eh = (struct elf_x86_link_hash_entry *) h;
2052 if (h != NULL)
2054 /* It is referenced by a non-shared object. */
2055 h->ref_regular = 1;
2058 converted_reloc = false;
2059 if ((r_type == R_X86_64_GOTPCREL
2060 || r_type == R_X86_64_GOTPCRELX
2061 || r_type == R_X86_64_REX_GOTPCRELX)
2062 && (h == NULL || h->type != STT_GNU_IFUNC))
2064 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
2065 if (!elf_x86_64_convert_load_reloc (abfd, contents, &r_type,
2066 irel, h, &converted_reloc,
2067 info))
2068 goto error_return;
2070 if (converted_reloc)
2071 converted = true;
2074 if (!_bfd_elf_x86_valid_reloc_p (sec, info, htab, rel, h, isym,
2075 symtab_hdr, &no_dynreloc))
2076 return false;
2078 if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
2079 symtab_hdr, sym_hashes,
2080 &r_type, GOT_UNKNOWN,
2081 rel, rel_end, h, r_symndx, false))
2082 goto error_return;
2084 /* Check if _GLOBAL_OFFSET_TABLE_ is referenced. */
2085 if (h == htab->elf.hgot)
2086 htab->got_referenced = true;
2088 switch (r_type)
2090 case R_X86_64_TLSLD:
2091 htab->tls_ld_or_ldm_got.refcount = 1;
2092 goto create_got;
2094 case R_X86_64_TPOFF32:
2095 if (!bfd_link_executable (info) && ABI_64_P (abfd))
2096 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2097 &x86_64_elf_howto_table[r_type]);
2098 if (eh != NULL)
2099 eh->zero_undefweak &= 0x2;
2100 break;
2102 case R_X86_64_GOTTPOFF:
2103 if (!bfd_link_executable (info))
2104 info->flags |= DF_STATIC_TLS;
2105 /* Fall through */
2107 case R_X86_64_GOT32:
2108 case R_X86_64_GOTPCREL:
2109 case R_X86_64_GOTPCRELX:
2110 case R_X86_64_REX_GOTPCRELX:
2111 case R_X86_64_TLSGD:
2112 case R_X86_64_GOT64:
2113 case R_X86_64_GOTPCREL64:
2114 case R_X86_64_GOTPLT64:
2115 case R_X86_64_GOTPC32_TLSDESC:
2116 case R_X86_64_TLSDESC_CALL:
2117 /* This symbol requires a global offset table entry. */
2119 int tls_type, old_tls_type;
2121 switch (r_type)
2123 default:
2124 tls_type = GOT_NORMAL;
2125 if (h)
2127 if (ABS_SYMBOL_P (h))
2128 tls_type = GOT_ABS;
2130 else if (isym->st_shndx == SHN_ABS)
2131 tls_type = GOT_ABS;
2132 break;
2133 case R_X86_64_TLSGD:
2134 tls_type = GOT_TLS_GD;
2135 break;
2136 case R_X86_64_GOTTPOFF:
2137 tls_type = GOT_TLS_IE;
2138 break;
2139 case R_X86_64_GOTPC32_TLSDESC:
2140 case R_X86_64_TLSDESC_CALL:
2141 tls_type = GOT_TLS_GDESC;
2142 break;
2145 if (h != NULL)
2147 h->got.refcount = 1;
2148 old_tls_type = eh->tls_type;
2150 else
2152 bfd_signed_vma *local_got_refcounts;
2154 if (!elf_x86_allocate_local_got_info (abfd,
2155 symtab_hdr->sh_info))
2156 goto error_return;
2158 /* This is a global offset table entry for a local symbol. */
2159 local_got_refcounts = elf_local_got_refcounts (abfd);
2160 local_got_refcounts[r_symndx] = 1;
2161 old_tls_type
2162 = elf_x86_local_got_tls_type (abfd) [r_symndx];
2165 /* If a TLS symbol is accessed using IE at least once,
2166 there is no point to use dynamic model for it. */
2167 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2168 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2169 || tls_type != GOT_TLS_IE))
2171 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
2172 tls_type = old_tls_type;
2173 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2174 && GOT_TLS_GD_ANY_P (tls_type))
2175 tls_type |= old_tls_type;
2176 else
2178 if (h)
2179 name = h->root.root.string;
2180 else
2181 name = bfd_elf_sym_name (abfd, symtab_hdr,
2182 isym, NULL);
2183 _bfd_error_handler
2184 /* xgettext:c-format */
2185 (_("%pB: '%s' accessed both as normal and"
2186 " thread local symbol"),
2187 abfd, name);
2188 bfd_set_error (bfd_error_bad_value);
2189 goto error_return;
2193 if (old_tls_type != tls_type)
2195 if (eh != NULL)
2196 eh->tls_type = tls_type;
2197 else
2198 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
2201 /* Fall through */
2203 case R_X86_64_GOTOFF64:
2204 case R_X86_64_GOTPC32:
2205 case R_X86_64_GOTPC64:
2206 create_got:
2207 if (eh != NULL)
2208 eh->zero_undefweak &= 0x2;
2209 break;
2211 case R_X86_64_PLT32:
2212 /* This symbol requires a procedure linkage table entry. We
2213 actually build the entry in adjust_dynamic_symbol,
2214 because this might be a case of linking PIC code which is
2215 never referenced by a dynamic object, in which case we
2216 don't need to generate a procedure linkage table entry
2217 after all. */
2219 /* If this is a local symbol, we resolve it directly without
2220 creating a procedure linkage table entry. */
2221 if (h == NULL)
2222 continue;
2224 eh->zero_undefweak &= 0x2;
2225 h->needs_plt = 1;
2226 h->plt.refcount = 1;
2227 break;
2229 case R_X86_64_PLTOFF64:
2230 /* This tries to form the 'address' of a function relative
2231 to GOT. For global symbols we need a PLT entry. */
2232 if (h != NULL)
2234 h->needs_plt = 1;
2235 h->plt.refcount = 1;
2237 goto create_got;
2239 case R_X86_64_SIZE32:
2240 case R_X86_64_SIZE64:
2241 size_reloc = true;
2242 goto do_size;
2244 case R_X86_64_32:
2245 if (!ABI_64_P (abfd))
2246 goto pointer;
2247 /* Fall through. */
2248 case R_X86_64_8:
2249 case R_X86_64_16:
2250 case R_X86_64_32S:
2251 /* Check relocation overflow as these relocs may lead to
2252 run-time relocation overflow. Don't error out for
2253 sections we don't care about, such as debug sections or
2254 when relocation overflow check is disabled. */
2255 if (!htab->params->no_reloc_overflow_check
2256 && !converted_reloc
2257 && (bfd_link_pic (info)
2258 || (bfd_link_executable (info)
2259 && h != NULL
2260 && !h->def_regular
2261 && h->def_dynamic
2262 && (sec->flags & SEC_READONLY) == 0)))
2263 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2264 &x86_64_elf_howto_table[r_type]);
2265 /* Fall through. */
2267 case R_X86_64_PC8:
2268 case R_X86_64_PC16:
2269 case R_X86_64_PC32:
2270 case R_X86_64_PC64:
2271 case R_X86_64_64:
2272 pointer:
2273 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2274 eh->zero_undefweak |= 0x2;
2275 /* We are called after all symbols have been resolved. Only
2276 relocation against STT_GNU_IFUNC symbol must go through
2277 PLT. */
2278 if (h != NULL
2279 && (bfd_link_executable (info)
2280 || h->type == STT_GNU_IFUNC))
2282 bool func_pointer_ref = false;
2284 if (r_type == R_X86_64_PC32)
2286 /* Since something like ".long foo - ." may be used
2287 as pointer, make sure that PLT is used if foo is
2288 a function defined in a shared library. */
2289 if ((sec->flags & SEC_CODE) == 0)
2291 h->pointer_equality_needed = 1;
2292 if (bfd_link_pie (info)
2293 && h->type == STT_FUNC
2294 && !h->def_regular
2295 && h->def_dynamic)
2297 h->needs_plt = 1;
2298 h->plt.refcount = 1;
2302 else if (r_type != R_X86_64_PC64)
2304 /* At run-time, R_X86_64_64 can be resolved for both
2305 x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2306 can only be resolved for x32. Function pointer
2307 reference doesn't need PLT for pointer equality. */
2308 if ((sec->flags & SEC_READONLY) == 0
2309 && (r_type == R_X86_64_64
2310 || (!ABI_64_P (abfd)
2311 && (r_type == R_X86_64_32
2312 || r_type == R_X86_64_32S))))
2313 func_pointer_ref = true;
2315 /* IFUNC symbol needs pointer equality in PDE so that
2316 function pointer reference will be resolved to its
2317 PLT entry directly. */
2318 if (!func_pointer_ref
2319 || (bfd_link_pde (info)
2320 && h->type == STT_GNU_IFUNC))
2321 h->pointer_equality_needed = 1;
2324 if (!func_pointer_ref)
2326 /* If this reloc is in a read-only section, we might
2327 need a copy reloc. We can't check reliably at this
2328 stage whether the section is read-only, as input
2329 sections have not yet been mapped to output sections.
2330 Tentatively set the flag for now, and correct in
2331 adjust_dynamic_symbol. */
2332 h->non_got_ref = 1;
2334 if (!elf_has_indirect_extern_access (sec->owner))
2335 eh->non_got_ref_without_indirect_extern_access = 1;
2337 /* We may need a .plt entry if the symbol is a function
2338 defined in a shared lib or is a function referenced
2339 from the code or read-only section. */
2340 if (!h->def_regular
2341 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2342 h->plt.refcount = 1;
2344 if (htab->elf.target_os != is_solaris
2345 && h->pointer_equality_needed
2346 && h->type == STT_FUNC
2347 && eh->def_protected
2348 && !SYMBOL_DEFINED_NON_SHARED_P (h)
2349 && h->def_dynamic)
2351 /* Disallow non-canonical reference to canonical
2352 protected function. */
2353 _bfd_error_handler
2354 /* xgettext:c-format */
2355 (_("%pB: non-canonical reference to canonical "
2356 "protected function `%s' in %pB"),
2357 abfd, h->root.root.string,
2358 h->root.u.def.section->owner);
2359 bfd_set_error (bfd_error_bad_value);
2360 goto error_return;
2365 size_reloc = false;
2366 do_size:
2367 if (!no_dynreloc
2368 && NEED_DYNAMIC_RELOCATION_P (true, info, true, h, sec,
2369 r_type,
2370 htab->pointer_r_type))
2372 struct elf_dyn_relocs *p;
2373 struct elf_dyn_relocs **head;
2375 /* If this is a global symbol, we count the number of
2376 relocations we need for this symbol. */
2377 if (h != NULL)
2378 head = &h->dyn_relocs;
2379 else
2381 /* Track dynamic relocs needed for local syms too.
2382 We really need local syms available to do this
2383 easily. Oh well. */
2384 asection *s;
2385 void **vpp;
2387 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
2388 abfd, r_symndx);
2389 if (isym == NULL)
2390 goto error_return;
2392 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2393 if (s == NULL)
2394 s = sec;
2396 /* Beware of type punned pointers vs strict aliasing
2397 rules. */
2398 vpp = &(elf_section_data (s)->local_dynrel);
2399 head = (struct elf_dyn_relocs **)vpp;
2402 p = *head;
2403 if (p == NULL || p->sec != sec)
2405 size_t amt = sizeof *p;
2407 p = ((struct elf_dyn_relocs *)
2408 bfd_alloc (htab->elf.dynobj, amt));
2409 if (p == NULL)
2410 goto error_return;
2411 p->next = *head;
2412 *head = p;
2413 p->sec = sec;
2414 p->count = 0;
2415 p->pc_count = 0;
2418 p->count += 1;
2419 /* Count size relocation as PC-relative relocation. */
2420 if (X86_PCREL_TYPE_P (true, r_type) || size_reloc)
2421 p->pc_count += 1;
2423 break;
2425 /* This relocation describes the C++ object vtable hierarchy.
2426 Reconstruct it for later use during GC. */
2427 case R_X86_64_GNU_VTINHERIT:
2428 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2429 goto error_return;
2430 break;
2432 /* This relocation describes which C++ vtable entries are actually
2433 used. Record for later use during GC. */
2434 case R_X86_64_GNU_VTENTRY:
2435 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2436 goto error_return;
2437 break;
2439 default:
2440 break;
2444 if (elf_section_data (sec)->this_hdr.contents != contents)
2446 if (!converted && !_bfd_link_keep_memory (info))
2447 free (contents);
2448 else
2450 /* Cache the section contents for elf_link_input_bfd if any
2451 load is converted or --no-keep-memory isn't used. */
2452 elf_section_data (sec)->this_hdr.contents = contents;
2453 info->cache_size += sec->size;
2457 /* Cache relocations if any load is converted. */
2458 if (elf_section_data (sec)->relocs != relocs && converted)
2459 elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
2461 return true;
2463 error_return:
2464 if (elf_section_data (sec)->this_hdr.contents != contents)
2465 free (contents);
2466 sec->check_relocs_failed = 1;
2467 return false;
2470 static bool
2471 elf_x86_64_always_size_sections (bfd *output_bfd,
2472 struct bfd_link_info *info)
2474 bfd *abfd;
2476 /* Scan relocations after rel_from_abs has been set on __ehdr_start. */
2477 for (abfd = info->input_bfds;
2478 abfd != (bfd *) NULL;
2479 abfd = abfd->link.next)
2480 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour
2481 && !_bfd_elf_link_iterate_on_relocs (abfd, info,
2482 elf_x86_64_scan_relocs))
2483 return false;
2485 return _bfd_x86_elf_always_size_sections (output_bfd, info);
2488 /* Return the relocation value for @tpoff relocation
2489 if STT_TLS virtual address is ADDRESS. */
2491 static bfd_vma
2492 elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2494 struct elf_link_hash_table *htab = elf_hash_table (info);
2495 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2496 bfd_vma static_tls_size;
2498 /* If tls_segment is NULL, we should have signalled an error already. */
2499 if (htab->tls_sec == NULL)
2500 return 0;
2502 /* Consider special static TLS alignment requirements. */
2503 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2504 return address - static_tls_size - htab->tls_sec->vma;
2507 /* Relocate an x86_64 ELF section. */
2509 static int
2510 elf_x86_64_relocate_section (bfd *output_bfd,
2511 struct bfd_link_info *info,
2512 bfd *input_bfd,
2513 asection *input_section,
2514 bfd_byte *contents,
2515 Elf_Internal_Rela *relocs,
2516 Elf_Internal_Sym *local_syms,
2517 asection **local_sections)
2519 struct elf_x86_link_hash_table *htab;
2520 Elf_Internal_Shdr *symtab_hdr;
2521 struct elf_link_hash_entry **sym_hashes;
2522 bfd_vma *local_got_offsets;
2523 bfd_vma *local_tlsdesc_gotents;
2524 Elf_Internal_Rela *rel;
2525 Elf_Internal_Rela *wrel;
2526 Elf_Internal_Rela *relend;
2527 unsigned int plt_entry_size;
2528 bool status;
2530 /* Skip if check_relocs or scan_relocs failed. */
2531 if (input_section->check_relocs_failed)
2532 return false;
2534 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2535 if (htab == NULL)
2536 return false;
2538 if (!is_x86_elf (input_bfd, htab))
2540 bfd_set_error (bfd_error_wrong_format);
2541 return false;
2544 plt_entry_size = htab->plt.plt_entry_size;
2545 symtab_hdr = &elf_symtab_hdr (input_bfd);
2546 sym_hashes = elf_sym_hashes (input_bfd);
2547 local_got_offsets = elf_local_got_offsets (input_bfd);
2548 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2550 _bfd_x86_elf_set_tls_module_base (info);
2552 status = true;
2553 rel = wrel = relocs;
2554 relend = relocs + input_section->reloc_count;
2555 for (; rel < relend; wrel++, rel++)
2557 unsigned int r_type, r_type_tls;
2558 reloc_howto_type *howto;
2559 unsigned long r_symndx;
2560 struct elf_link_hash_entry *h;
2561 struct elf_x86_link_hash_entry *eh;
2562 Elf_Internal_Sym *sym;
2563 asection *sec;
2564 bfd_vma off, offplt, plt_offset;
2565 bfd_vma relocation;
2566 bool unresolved_reloc;
2567 bfd_reloc_status_type r;
2568 int tls_type;
2569 asection *base_got, *resolved_plt;
2570 bfd_vma st_size;
2571 bool resolved_to_zero;
2572 bool relative_reloc;
2573 bool converted_reloc;
2574 bool need_copy_reloc_in_pie;
2575 bool no_copyreloc_p;
2577 r_type = ELF32_R_TYPE (rel->r_info);
2578 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2579 || r_type == (int) R_X86_64_GNU_VTENTRY)
2581 if (wrel != rel)
2582 *wrel = *rel;
2583 continue;
2586 r_symndx = htab->r_sym (rel->r_info);
2587 converted_reloc = (r_type & R_X86_64_converted_reloc_bit) != 0;
2588 if (converted_reloc)
2590 r_type &= ~R_X86_64_converted_reloc_bit;
2591 rel->r_info = htab->r_info (r_symndx, r_type);
2594 howto = elf_x86_64_rtype_to_howto (input_bfd, r_type);
2595 if (howto == NULL)
2596 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2598 h = NULL;
2599 sym = NULL;
2600 sec = NULL;
2601 unresolved_reloc = false;
2602 if (r_symndx < symtab_hdr->sh_info)
2604 sym = local_syms + r_symndx;
2605 sec = local_sections[r_symndx];
2607 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2608 &sec, rel);
2609 st_size = sym->st_size;
2611 /* Relocate against local STT_GNU_IFUNC symbol. */
2612 if (!bfd_link_relocatable (info)
2613 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2615 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd,
2616 rel, false);
2617 if (h == NULL)
2618 abort ();
2620 /* Set STT_GNU_IFUNC symbol value. */
2621 h->root.u.def.value = sym->st_value;
2622 h->root.u.def.section = sec;
2625 else
2627 bool warned ATTRIBUTE_UNUSED;
2628 bool ignored ATTRIBUTE_UNUSED;
2630 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2631 r_symndx, symtab_hdr, sym_hashes,
2632 h, sec, relocation,
2633 unresolved_reloc, warned, ignored);
2634 st_size = h->size;
2637 if (sec != NULL && discarded_section (sec))
2639 _bfd_clear_contents (howto, input_bfd, input_section,
2640 contents, rel->r_offset);
2641 wrel->r_offset = rel->r_offset;
2642 wrel->r_info = 0;
2643 wrel->r_addend = 0;
2645 /* For ld -r, remove relocations in debug sections against
2646 sections defined in discarded sections. Not done for
2647 eh_frame editing code expects to be present. */
2648 if (bfd_link_relocatable (info)
2649 && (input_section->flags & SEC_DEBUGGING))
2650 wrel--;
2652 continue;
2655 if (bfd_link_relocatable (info))
2657 if (wrel != rel)
2658 *wrel = *rel;
2659 continue;
2662 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
2664 if (r_type == R_X86_64_64)
2666 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
2667 zero-extend it to 64bit if addend is zero. */
2668 r_type = R_X86_64_32;
2669 memset (contents + rel->r_offset + 4, 0, 4);
2671 else if (r_type == R_X86_64_SIZE64)
2673 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
2674 zero-extend it to 64bit if addend is zero. */
2675 r_type = R_X86_64_SIZE32;
2676 memset (contents + rel->r_offset + 4, 0, 4);
2680 eh = (struct elf_x86_link_hash_entry *) h;
2682 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2683 it here if it is defined in a non-shared object. */
2684 if (h != NULL
2685 && h->type == STT_GNU_IFUNC
2686 && h->def_regular)
2688 bfd_vma plt_index;
2689 const char *name;
2691 if ((input_section->flags & SEC_ALLOC) == 0)
2693 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2694 STT_GNU_IFUNC symbol as STT_FUNC. */
2695 if (elf_section_type (input_section) == SHT_NOTE)
2696 goto skip_ifunc;
2697 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2698 sections because such sections are not SEC_ALLOC and
2699 thus ld.so will not process them. */
2700 if ((input_section->flags & SEC_DEBUGGING) != 0)
2701 continue;
2702 abort ();
2705 switch (r_type)
2707 default:
2708 break;
2710 case R_X86_64_GOTPCREL:
2711 case R_X86_64_GOTPCRELX:
2712 case R_X86_64_REX_GOTPCRELX:
2713 case R_X86_64_GOTPCREL64:
2714 base_got = htab->elf.sgot;
2715 off = h->got.offset;
2717 if (base_got == NULL)
2718 abort ();
2720 if (off == (bfd_vma) -1)
2722 /* We can't use h->got.offset here to save state, or
2723 even just remember the offset, as finish_dynamic_symbol
2724 would use that as offset into .got. */
2726 if (h->plt.offset == (bfd_vma) -1)
2727 abort ();
2729 if (htab->elf.splt != NULL)
2731 plt_index = (h->plt.offset / plt_entry_size
2732 - htab->plt.has_plt0);
2733 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2734 base_got = htab->elf.sgotplt;
2736 else
2738 plt_index = h->plt.offset / plt_entry_size;
2739 off = plt_index * GOT_ENTRY_SIZE;
2740 base_got = htab->elf.igotplt;
2743 if (h->dynindx == -1
2744 || h->forced_local
2745 || info->symbolic)
2747 /* This references the local defitionion. We must
2748 initialize this entry in the global offset table.
2749 Since the offset must always be a multiple of 8,
2750 we use the least significant bit to record
2751 whether we have initialized it already.
2753 When doing a dynamic link, we create a .rela.got
2754 relocation entry to initialize the value. This
2755 is done in the finish_dynamic_symbol routine. */
2756 if ((off & 1) != 0)
2757 off &= ~1;
2758 else
2760 bfd_put_64 (output_bfd, relocation,
2761 base_got->contents + off);
2762 /* Note that this is harmless for the GOTPLT64
2763 case, as -1 | 1 still is -1. */
2764 h->got.offset |= 1;
2769 relocation = (base_got->output_section->vma
2770 + base_got->output_offset + off);
2772 goto do_relocation;
2775 if (h->plt.offset == (bfd_vma) -1)
2777 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2778 if (r_type == htab->pointer_r_type
2779 && (input_section->flags & SEC_CODE) == 0)
2780 goto do_ifunc_pointer;
2781 goto bad_ifunc_reloc;
2784 /* STT_GNU_IFUNC symbol must go through PLT. */
2785 if (htab->elf.splt != NULL)
2787 if (htab->plt_second != NULL)
2789 resolved_plt = htab->plt_second;
2790 plt_offset = eh->plt_second.offset;
2792 else
2794 resolved_plt = htab->elf.splt;
2795 plt_offset = h->plt.offset;
2798 else
2800 resolved_plt = htab->elf.iplt;
2801 plt_offset = h->plt.offset;
2804 relocation = (resolved_plt->output_section->vma
2805 + resolved_plt->output_offset + plt_offset);
2807 switch (r_type)
2809 default:
2810 bad_ifunc_reloc:
2811 if (h->root.root.string)
2812 name = h->root.root.string;
2813 else
2814 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2815 NULL);
2816 _bfd_error_handler
2817 /* xgettext:c-format */
2818 (_("%pB: relocation %s against STT_GNU_IFUNC "
2819 "symbol `%s' isn't supported"), input_bfd,
2820 howto->name, name);
2821 bfd_set_error (bfd_error_bad_value);
2822 return false;
2824 case R_X86_64_32S:
2825 if (bfd_link_pic (info))
2826 abort ();
2827 goto do_relocation;
2829 case R_X86_64_32:
2830 if (ABI_64_P (output_bfd))
2831 goto do_relocation;
2832 /* FALLTHROUGH */
2833 case R_X86_64_64:
2834 do_ifunc_pointer:
2835 if (rel->r_addend != 0)
2837 if (h->root.root.string)
2838 name = h->root.root.string;
2839 else
2840 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2841 sym, NULL);
2842 _bfd_error_handler
2843 /* xgettext:c-format */
2844 (_("%pB: relocation %s against STT_GNU_IFUNC "
2845 "symbol `%s' has non-zero addend: %" PRId64),
2846 input_bfd, howto->name, name, (int64_t) rel->r_addend);
2847 bfd_set_error (bfd_error_bad_value);
2848 return false;
2851 /* Generate dynamic relcoation only when there is a
2852 non-GOT reference in a shared object or there is no
2853 PLT. */
2854 if ((bfd_link_pic (info) && h->non_got_ref)
2855 || h->plt.offset == (bfd_vma) -1)
2857 Elf_Internal_Rela outrel;
2858 asection *sreloc;
2860 /* Need a dynamic relocation to get the real function
2861 address. */
2862 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2863 info,
2864 input_section,
2865 rel->r_offset);
2866 if (outrel.r_offset == (bfd_vma) -1
2867 || outrel.r_offset == (bfd_vma) -2)
2868 abort ();
2870 outrel.r_offset += (input_section->output_section->vma
2871 + input_section->output_offset);
2873 if (POINTER_LOCAL_IFUNC_P (info, h))
2875 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
2876 h->root.root.string,
2877 h->root.u.def.section->owner);
2879 /* This symbol is resolved locally. */
2880 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
2881 outrel.r_addend = (h->root.u.def.value
2882 + h->root.u.def.section->output_section->vma
2883 + h->root.u.def.section->output_offset);
2885 if (htab->params->report_relative_reloc)
2886 _bfd_x86_elf_link_report_relative_reloc
2887 (info, input_section, h, sym,
2888 "R_X86_64_IRELATIVE", &outrel);
2890 else
2892 outrel.r_info = htab->r_info (h->dynindx, r_type);
2893 outrel.r_addend = 0;
2896 /* Dynamic relocations are stored in
2897 1. .rela.ifunc section in PIC object.
2898 2. .rela.got section in dynamic executable.
2899 3. .rela.iplt section in static executable. */
2900 if (bfd_link_pic (info))
2901 sreloc = htab->elf.irelifunc;
2902 else if (htab->elf.splt != NULL)
2903 sreloc = htab->elf.srelgot;
2904 else
2905 sreloc = htab->elf.irelplt;
2906 elf_append_rela (output_bfd, sreloc, &outrel);
2908 /* If this reloc is against an external symbol, we
2909 do not want to fiddle with the addend. Otherwise,
2910 we need to include the symbol value so that it
2911 becomes an addend for the dynamic reloc. For an
2912 internal symbol, we have updated addend. */
2913 continue;
2915 /* FALLTHROUGH */
2916 case R_X86_64_PC32:
2917 case R_X86_64_PC64:
2918 case R_X86_64_PLT32:
2919 goto do_relocation;
2923 skip_ifunc:
2924 resolved_to_zero = (eh != NULL
2925 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
2927 /* When generating a shared object, the relocations handled here are
2928 copied into the output file to be resolved at run time. */
2929 switch (r_type)
2931 case R_X86_64_GOT32:
2932 case R_X86_64_GOT64:
2933 /* Relocation is to the entry for this symbol in the global
2934 offset table. */
2935 case R_X86_64_GOTPCREL:
2936 case R_X86_64_GOTPCRELX:
2937 case R_X86_64_REX_GOTPCRELX:
2938 case R_X86_64_GOTPCREL64:
2939 /* Use global offset table entry as symbol value. */
2940 case R_X86_64_GOTPLT64:
2941 /* This is obsolete and treated the same as GOT64. */
2942 base_got = htab->elf.sgot;
2944 if (htab->elf.sgot == NULL)
2945 abort ();
2947 relative_reloc = false;
2948 if (h != NULL)
2950 off = h->got.offset;
2951 if (h->needs_plt
2952 && h->plt.offset != (bfd_vma)-1
2953 && off == (bfd_vma)-1)
2955 /* We can't use h->got.offset here to save
2956 state, or even just remember the offset, as
2957 finish_dynamic_symbol would use that as offset into
2958 .got. */
2959 bfd_vma plt_index = (h->plt.offset / plt_entry_size
2960 - htab->plt.has_plt0);
2961 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2962 base_got = htab->elf.sgotplt;
2965 if (RESOLVED_LOCALLY_P (info, h, htab))
2967 /* We must initialize this entry in the global offset
2968 table. Since the offset must always be a multiple
2969 of 8, we use the least significant bit to record
2970 whether we have initialized it already.
2972 When doing a dynamic link, we create a .rela.got
2973 relocation entry to initialize the value. This is
2974 done in the finish_dynamic_symbol routine. */
2975 if ((off & 1) != 0)
2976 off &= ~1;
2977 else
2979 bfd_put_64 (output_bfd, relocation,
2980 base_got->contents + off);
2981 /* Note that this is harmless for the GOTPLT64 case,
2982 as -1 | 1 still is -1. */
2983 h->got.offset |= 1;
2985 /* NB: Don't generate relative relocation here if
2986 it has been generated by DT_RELR. */
2987 if (!info->enable_dt_relr
2988 && GENERATE_RELATIVE_RELOC_P (info, h))
2990 /* If this symbol isn't dynamic in PIC,
2991 generate R_X86_64_RELATIVE here. */
2992 eh->no_finish_dynamic_symbol = 1;
2993 relative_reloc = true;
2997 else
2998 unresolved_reloc = false;
3000 else
3002 if (local_got_offsets == NULL)
3003 abort ();
3005 off = local_got_offsets[r_symndx];
3007 /* The offset must always be a multiple of 8. We use
3008 the least significant bit to record whether we have
3009 already generated the necessary reloc. */
3010 if ((off & 1) != 0)
3011 off &= ~1;
3012 else
3014 bfd_put_64 (output_bfd, relocation,
3015 base_got->contents + off);
3016 local_got_offsets[r_symndx] |= 1;
3018 /* NB: GOTPCREL relocations against local absolute
3019 symbol store relocation value in the GOT slot
3020 without relative relocation. Don't generate
3021 relative relocation here if it has been generated
3022 by DT_RELR. */
3023 if (!info->enable_dt_relr
3024 && bfd_link_pic (info)
3025 && !(sym->st_shndx == SHN_ABS
3026 && (r_type == R_X86_64_GOTPCREL
3027 || r_type == R_X86_64_GOTPCRELX
3028 || r_type == R_X86_64_REX_GOTPCRELX)))
3029 relative_reloc = true;
3033 if (relative_reloc)
3035 asection *s;
3036 Elf_Internal_Rela outrel;
3038 /* We need to generate a R_X86_64_RELATIVE reloc
3039 for the dynamic linker. */
3040 s = htab->elf.srelgot;
3041 if (s == NULL)
3042 abort ();
3044 outrel.r_offset = (base_got->output_section->vma
3045 + base_got->output_offset
3046 + off);
3047 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3048 outrel.r_addend = relocation;
3050 if (htab->params->report_relative_reloc)
3051 _bfd_x86_elf_link_report_relative_reloc
3052 (info, input_section, h, sym, "R_X86_64_RELATIVE",
3053 &outrel);
3055 elf_append_rela (output_bfd, s, &outrel);
3058 if (off >= (bfd_vma) -2)
3059 abort ();
3061 relocation = base_got->output_section->vma
3062 + base_got->output_offset + off;
3063 if (r_type != R_X86_64_GOTPCREL
3064 && r_type != R_X86_64_GOTPCRELX
3065 && r_type != R_X86_64_REX_GOTPCRELX
3066 && r_type != R_X86_64_GOTPCREL64)
3067 relocation -= htab->elf.sgotplt->output_section->vma
3068 - htab->elf.sgotplt->output_offset;
3070 break;
3072 case R_X86_64_GOTOFF64:
3073 /* Relocation is relative to the start of the global offset
3074 table. */
3076 /* Check to make sure it isn't a protected function or data
3077 symbol for shared library since it may not be local when
3078 used as function address or with copy relocation. We also
3079 need to make sure that a symbol is referenced locally. */
3080 if (bfd_link_pic (info) && h)
3082 if (!h->def_regular)
3084 const char *v;
3086 switch (ELF_ST_VISIBILITY (h->other))
3088 case STV_HIDDEN:
3089 v = _("hidden symbol");
3090 break;
3091 case STV_INTERNAL:
3092 v = _("internal symbol");
3093 break;
3094 case STV_PROTECTED:
3095 v = _("protected symbol");
3096 break;
3097 default:
3098 v = _("symbol");
3099 break;
3102 _bfd_error_handler
3103 /* xgettext:c-format */
3104 (_("%pB: relocation R_X86_64_GOTOFF64 against undefined %s"
3105 " `%s' can not be used when making a shared object"),
3106 input_bfd, v, h->root.root.string);
3107 bfd_set_error (bfd_error_bad_value);
3108 return false;
3110 else if (!bfd_link_executable (info)
3111 && !SYMBOL_REFERENCES_LOCAL_P (info, h)
3112 && (h->type == STT_FUNC
3113 || h->type == STT_OBJECT)
3114 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3116 _bfd_error_handler
3117 /* xgettext:c-format */
3118 (_("%pB: relocation R_X86_64_GOTOFF64 against protected %s"
3119 " `%s' can not be used when making a shared object"),
3120 input_bfd,
3121 h->type == STT_FUNC ? "function" : "data",
3122 h->root.root.string);
3123 bfd_set_error (bfd_error_bad_value);
3124 return false;
3128 /* Note that sgot is not involved in this
3129 calculation. We always want the start of .got.plt. If we
3130 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3131 permitted by the ABI, we might have to change this
3132 calculation. */
3133 relocation -= htab->elf.sgotplt->output_section->vma
3134 + htab->elf.sgotplt->output_offset;
3135 break;
3137 case R_X86_64_GOTPC32:
3138 case R_X86_64_GOTPC64:
3139 /* Use global offset table as symbol value. */
3140 relocation = htab->elf.sgotplt->output_section->vma
3141 + htab->elf.sgotplt->output_offset;
3142 unresolved_reloc = false;
3143 break;
3145 case R_X86_64_PLTOFF64:
3146 /* Relocation is PLT entry relative to GOT. For local
3147 symbols it's the symbol itself relative to GOT. */
3148 if (h != NULL
3149 /* See PLT32 handling. */
3150 && (h->plt.offset != (bfd_vma) -1
3151 || eh->plt_got.offset != (bfd_vma) -1)
3152 && htab->elf.splt != NULL)
3154 if (eh->plt_got.offset != (bfd_vma) -1)
3156 /* Use the GOT PLT. */
3157 resolved_plt = htab->plt_got;
3158 plt_offset = eh->plt_got.offset;
3160 else if (htab->plt_second != NULL)
3162 resolved_plt = htab->plt_second;
3163 plt_offset = eh->plt_second.offset;
3165 else
3167 resolved_plt = htab->elf.splt;
3168 plt_offset = h->plt.offset;
3171 relocation = (resolved_plt->output_section->vma
3172 + resolved_plt->output_offset
3173 + plt_offset);
3174 unresolved_reloc = false;
3177 relocation -= htab->elf.sgotplt->output_section->vma
3178 + htab->elf.sgotplt->output_offset;
3179 break;
3181 case R_X86_64_PLT32:
3182 /* Relocation is to the entry for this symbol in the
3183 procedure linkage table. */
3185 /* Resolve a PLT32 reloc against a local symbol directly,
3186 without using the procedure linkage table. */
3187 if (h == NULL)
3188 break;
3190 if ((h->plt.offset == (bfd_vma) -1
3191 && eh->plt_got.offset == (bfd_vma) -1)
3192 || htab->elf.splt == NULL)
3194 /* We didn't make a PLT entry for this symbol. This
3195 happens when statically linking PIC code, or when
3196 using -Bsymbolic. */
3197 break;
3200 use_plt:
3201 if (h->plt.offset != (bfd_vma) -1)
3203 if (htab->plt_second != NULL)
3205 resolved_plt = htab->plt_second;
3206 plt_offset = eh->plt_second.offset;
3208 else
3210 resolved_plt = htab->elf.splt;
3211 plt_offset = h->plt.offset;
3214 else
3216 /* Use the GOT PLT. */
3217 resolved_plt = htab->plt_got;
3218 plt_offset = eh->plt_got.offset;
3221 relocation = (resolved_plt->output_section->vma
3222 + resolved_plt->output_offset
3223 + plt_offset);
3224 unresolved_reloc = false;
3225 break;
3227 case R_X86_64_SIZE32:
3228 case R_X86_64_SIZE64:
3229 /* Set to symbol size. */
3230 relocation = st_size;
3231 goto direct;
3233 case R_X86_64_PC8:
3234 case R_X86_64_PC16:
3235 case R_X86_64_PC32:
3236 /* Don't complain about -fPIC if the symbol is undefined when
3237 building executable unless it is unresolved weak symbol,
3238 references a dynamic definition in PIE or -z nocopyreloc
3239 is used. */
3240 no_copyreloc_p
3241 = (info->nocopyreloc
3242 || (h != NULL
3243 && !h->root.linker_def
3244 && !h->root.ldscript_def
3245 && eh->def_protected));
3247 if ((input_section->flags & SEC_ALLOC) != 0
3248 && (input_section->flags & SEC_READONLY) != 0
3249 && h != NULL
3250 && ((bfd_link_executable (info)
3251 && ((h->root.type == bfd_link_hash_undefweak
3252 && (eh == NULL
3253 || !UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
3254 eh)))
3255 || (bfd_link_pie (info)
3256 && !SYMBOL_DEFINED_NON_SHARED_P (h)
3257 && h->def_dynamic)
3258 || (no_copyreloc_p
3259 && h->def_dynamic
3260 && !(h->root.u.def.section->flags & SEC_CODE))))
3261 || (bfd_link_pie (info)
3262 && h->root.type == bfd_link_hash_undefweak)
3263 || bfd_link_dll (info)))
3265 bool fail = false;
3266 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3268 /* Symbol is referenced locally. Make sure it is
3269 defined locally. */
3270 fail = !SYMBOL_DEFINED_NON_SHARED_P (h);
3272 else if (bfd_link_pie (info))
3274 /* We can only use PC-relative relocations in PIE
3275 from non-code sections. */
3276 if (h->root.type == bfd_link_hash_undefweak
3277 || (h->type == STT_FUNC
3278 && (sec->flags & SEC_CODE) != 0))
3279 fail = true;
3281 else if (no_copyreloc_p || bfd_link_dll (info))
3283 /* Symbol doesn't need copy reloc and isn't
3284 referenced locally. Don't allow PC-relative
3285 relocations against default and protected
3286 symbols since address of protected function
3287 and location of protected data may not be in
3288 the shared object. */
3289 fail = (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3290 || ELF_ST_VISIBILITY (h->other) == STV_PROTECTED);
3293 if (fail)
3294 return elf_x86_64_need_pic (info, input_bfd, input_section,
3295 h, NULL, NULL, howto);
3297 /* Since x86-64 has PC-relative PLT, we can use PLT in PIE
3298 as function address. */
3299 else if (h != NULL
3300 && (input_section->flags & SEC_CODE) == 0
3301 && bfd_link_pie (info)
3302 && h->type == STT_FUNC
3303 && !h->def_regular
3304 && h->def_dynamic)
3305 goto use_plt;
3306 /* Fall through. */
3308 case R_X86_64_8:
3309 case R_X86_64_16:
3310 case R_X86_64_32:
3311 case R_X86_64_PC64:
3312 case R_X86_64_64:
3313 /* FIXME: The ABI says the linker should make sure the value is
3314 the same when it's zeroextended to 64 bit. */
3316 direct:
3317 if ((input_section->flags & SEC_ALLOC) == 0)
3318 break;
3320 need_copy_reloc_in_pie = (bfd_link_pie (info)
3321 && h != NULL
3322 && (h->needs_copy
3323 || eh->needs_copy
3324 || (h->root.type
3325 == bfd_link_hash_undefined))
3326 && (X86_PCREL_TYPE_P (true, r_type)
3327 || X86_SIZE_TYPE_P (true,
3328 r_type)));
3330 if (GENERATE_DYNAMIC_RELOCATION_P (true, info, eh, r_type, sec,
3331 need_copy_reloc_in_pie,
3332 resolved_to_zero, false))
3334 Elf_Internal_Rela outrel;
3335 bool skip, relocate;
3336 bool generate_dynamic_reloc = true;
3337 asection *sreloc;
3338 const char *relative_reloc_name = NULL;
3340 /* When generating a shared object, these relocations
3341 are copied into the output file to be resolved at run
3342 time. */
3343 skip = false;
3344 relocate = false;
3346 outrel.r_offset =
3347 _bfd_elf_section_offset (output_bfd, info, input_section,
3348 rel->r_offset);
3349 if (outrel.r_offset == (bfd_vma) -1)
3350 skip = true;
3351 else if (outrel.r_offset == (bfd_vma) -2)
3352 skip = true, relocate = true;
3354 outrel.r_offset += (input_section->output_section->vma
3355 + input_section->output_offset);
3357 if (skip)
3358 memset (&outrel, 0, sizeof outrel);
3360 else if (COPY_INPUT_RELOC_P (true, info, h, r_type))
3362 outrel.r_info = htab->r_info (h->dynindx, r_type);
3363 outrel.r_addend = rel->r_addend;
3365 else
3367 /* This symbol is local, or marked to become local.
3368 When relocation overflow check is disabled, we
3369 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
3370 if (r_type == htab->pointer_r_type
3371 || (r_type == R_X86_64_32
3372 && htab->params->no_reloc_overflow_check))
3374 relocate = true;
3375 /* NB: Don't generate relative relocation here if
3376 it has been generated by DT_RELR. */
3377 if (info->enable_dt_relr)
3378 generate_dynamic_reloc = false;
3379 else
3381 outrel.r_info =
3382 htab->r_info (0, R_X86_64_RELATIVE);
3383 outrel.r_addend = relocation + rel->r_addend;
3384 relative_reloc_name = "R_X86_64_RELATIVE";
3387 else if (r_type == R_X86_64_64
3388 && !ABI_64_P (output_bfd))
3390 relocate = true;
3391 outrel.r_info = htab->r_info (0,
3392 R_X86_64_RELATIVE64);
3393 outrel.r_addend = relocation + rel->r_addend;
3394 relative_reloc_name = "R_X86_64_RELATIVE64";
3395 /* Check addend overflow. */
3396 if ((outrel.r_addend & 0x80000000)
3397 != (rel->r_addend & 0x80000000))
3399 const char *name;
3400 int addend = rel->r_addend;
3401 if (h && h->root.root.string)
3402 name = h->root.root.string;
3403 else
3404 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3405 sym, NULL);
3406 _bfd_error_handler
3407 /* xgettext:c-format */
3408 (_("%pB: addend %s%#x in relocation %s against "
3409 "symbol `%s' at %#" PRIx64
3410 " in section `%pA' is out of range"),
3411 input_bfd, addend < 0 ? "-" : "", addend,
3412 howto->name, name, (uint64_t) rel->r_offset,
3413 input_section);
3414 bfd_set_error (bfd_error_bad_value);
3415 return false;
3418 else
3420 long sindx;
3422 if (bfd_is_abs_section (sec))
3423 sindx = 0;
3424 else if (sec == NULL || sec->owner == NULL)
3426 bfd_set_error (bfd_error_bad_value);
3427 return false;
3429 else
3431 asection *osec;
3433 /* We are turning this relocation into one
3434 against a section symbol. It would be
3435 proper to subtract the symbol's value,
3436 osec->vma, from the emitted reloc addend,
3437 but ld.so expects buggy relocs. */
3438 osec = sec->output_section;
3439 sindx = elf_section_data (osec)->dynindx;
3440 if (sindx == 0)
3442 asection *oi = htab->elf.text_index_section;
3443 sindx = elf_section_data (oi)->dynindx;
3445 BFD_ASSERT (sindx != 0);
3448 outrel.r_info = htab->r_info (sindx, r_type);
3449 outrel.r_addend = relocation + rel->r_addend;
3453 if (generate_dynamic_reloc)
3455 sreloc = elf_section_data (input_section)->sreloc;
3457 if (sreloc == NULL || sreloc->contents == NULL)
3459 r = bfd_reloc_notsupported;
3460 goto check_relocation_error;
3463 if (relative_reloc_name
3464 && htab->params->report_relative_reloc)
3465 _bfd_x86_elf_link_report_relative_reloc
3466 (info, input_section, h, sym,
3467 relative_reloc_name, &outrel);
3469 elf_append_rela (output_bfd, sreloc, &outrel);
3472 /* If this reloc is against an external symbol, we do
3473 not want to fiddle with the addend. Otherwise, we
3474 need to include the symbol value so that it becomes
3475 an addend for the dynamic reloc. */
3476 if (! relocate)
3477 continue;
3480 break;
3482 case R_X86_64_TLSGD:
3483 case R_X86_64_GOTPC32_TLSDESC:
3484 case R_X86_64_TLSDESC_CALL:
3485 case R_X86_64_GOTTPOFF:
3486 tls_type = GOT_UNKNOWN;
3487 if (h == NULL && local_got_offsets)
3488 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
3489 else if (h != NULL)
3490 tls_type = elf_x86_hash_entry (h)->tls_type;
3492 r_type_tls = r_type;
3493 if (! elf_x86_64_tls_transition (info, input_bfd,
3494 input_section, contents,
3495 symtab_hdr, sym_hashes,
3496 &r_type_tls, tls_type, rel,
3497 relend, h, r_symndx, true))
3498 return false;
3500 if (r_type_tls == R_X86_64_TPOFF32)
3502 bfd_vma roff = rel->r_offset;
3504 if (roff >= input_section->size)
3505 goto corrupt_input;
3507 BFD_ASSERT (! unresolved_reloc);
3509 if (r_type == R_X86_64_TLSGD)
3511 /* GD->LE transition. For 64bit, change
3512 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3513 .word 0x6666; rex64; call __tls_get_addr@PLT
3515 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3516 .byte 0x66; rex64
3517 call *__tls_get_addr@GOTPCREL(%rip)
3518 which may be converted to
3519 addr32 call __tls_get_addr
3520 into:
3521 movq %fs:0, %rax
3522 leaq foo@tpoff(%rax), %rax
3523 For 32bit, change
3524 leaq foo@tlsgd(%rip), %rdi
3525 .word 0x6666; rex64; call __tls_get_addr@PLT
3527 leaq foo@tlsgd(%rip), %rdi
3528 .byte 0x66; rex64
3529 call *__tls_get_addr@GOTPCREL(%rip)
3530 which may be converted to
3531 addr32 call __tls_get_addr
3532 into:
3533 movl %fs:0, %eax
3534 leaq foo@tpoff(%rax), %rax
3535 For largepic, change:
3536 leaq foo@tlsgd(%rip), %rdi
3537 movabsq $__tls_get_addr@pltoff, %rax
3538 addq %r15, %rax
3539 call *%rax
3540 into:
3541 movq %fs:0, %rax
3542 leaq foo@tpoff(%rax), %rax
3543 nopw 0x0(%rax,%rax,1) */
3544 int largepic = 0;
3545 if (ABI_64_P (output_bfd))
3547 if (roff + 5 >= input_section->size)
3548 goto corrupt_input;
3549 if (contents[roff + 5] == 0xb8)
3551 if (roff < 3
3552 || (roff - 3 + 22) > input_section->size)
3554 corrupt_input:
3555 info->callbacks->einfo
3556 (_("%F%P: corrupt input: %pB\n"),
3557 input_bfd);
3558 return false;
3560 memcpy (contents + roff - 3,
3561 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
3562 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3563 largepic = 1;
3565 else
3567 if (roff < 4
3568 || (roff - 4 + 16) > input_section->size)
3569 goto corrupt_input;
3570 memcpy (contents + roff - 4,
3571 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3572 16);
3575 else
3577 if (roff < 3
3578 || (roff - 3 + 15) > input_section->size)
3579 goto corrupt_input;
3580 memcpy (contents + roff - 3,
3581 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3582 15);
3585 if (roff + 8 + largepic >= input_section->size)
3586 goto corrupt_input;
3588 bfd_put_32 (output_bfd,
3589 elf_x86_64_tpoff (info, relocation),
3590 contents + roff + 8 + largepic);
3591 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
3592 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
3593 rel++;
3594 wrel++;
3595 continue;
3597 else if (r_type == R_X86_64_GOTPC32_TLSDESC)
3599 /* GDesc -> LE transition.
3600 It's originally something like:
3601 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
3602 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
3604 Change it to:
3605 movq $x@tpoff, %rax <--- LP64 mode.
3606 rex movl $x@tpoff, %eax <--- X32 mode.
3609 unsigned int val, type;
3611 if (roff < 3)
3612 goto corrupt_input;
3613 type = bfd_get_8 (input_bfd, contents + roff - 3);
3614 val = bfd_get_8 (input_bfd, contents + roff - 1);
3615 bfd_put_8 (output_bfd,
3616 (type & 0x48) | ((type >> 2) & 1),
3617 contents + roff - 3);
3618 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3619 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3620 contents + roff - 1);
3621 bfd_put_32 (output_bfd,
3622 elf_x86_64_tpoff (info, relocation),
3623 contents + roff);
3624 continue;
3626 else if (r_type == R_X86_64_TLSDESC_CALL)
3628 /* GDesc -> LE transition.
3629 It's originally:
3630 call *(%rax) <--- LP64 mode.
3631 call *(%eax) <--- X32 mode.
3632 Turn it into:
3633 xchg %ax,%ax <-- LP64 mode.
3634 nopl (%rax) <-- X32 mode.
3636 unsigned int prefix = 0;
3637 if (!ABI_64_P (input_bfd))
3639 /* Check for call *x@tlsdesc(%eax). */
3640 if (contents[roff] == 0x67)
3641 prefix = 1;
3643 if (prefix)
3645 if (roff + 2 >= input_section->size)
3646 goto corrupt_input;
3648 bfd_put_8 (output_bfd, 0x0f, contents + roff);
3649 bfd_put_8 (output_bfd, 0x1f, contents + roff + 1);
3650 bfd_put_8 (output_bfd, 0x00, contents + roff + 2);
3652 else
3654 if (roff + 1 >= input_section->size)
3655 goto corrupt_input;
3657 bfd_put_8 (output_bfd, 0x66, contents + roff);
3658 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3660 continue;
3662 else if (r_type == R_X86_64_GOTTPOFF)
3664 /* IE->LE transition:
3665 For 64bit, originally it can be one of:
3666 movq foo@gottpoff(%rip), %reg
3667 addq foo@gottpoff(%rip), %reg
3668 We change it into:
3669 movq $foo, %reg
3670 leaq foo(%reg), %reg
3671 addq $foo, %reg.
3672 For 32bit, originally it can be one of:
3673 movq foo@gottpoff(%rip), %reg
3674 addl foo@gottpoff(%rip), %reg
3675 We change it into:
3676 movq $foo, %reg
3677 leal foo(%reg), %reg
3678 addl $foo, %reg. */
3680 unsigned int val, type, reg;
3682 if (roff >= 3)
3683 val = bfd_get_8 (input_bfd, contents + roff - 3);
3684 else
3686 if (roff < 2)
3687 goto corrupt_input;
3688 val = 0;
3690 type = bfd_get_8 (input_bfd, contents + roff - 2);
3691 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3692 reg >>= 3;
3693 if (type == 0x8b)
3695 /* movq */
3696 if (val == 0x4c)
3698 if (roff < 3)
3699 goto corrupt_input;
3700 bfd_put_8 (output_bfd, 0x49,
3701 contents + roff - 3);
3703 else if (!ABI_64_P (output_bfd) && val == 0x44)
3705 if (roff < 3)
3706 goto corrupt_input;
3707 bfd_put_8 (output_bfd, 0x41,
3708 contents + roff - 3);
3710 bfd_put_8 (output_bfd, 0xc7,
3711 contents + roff - 2);
3712 bfd_put_8 (output_bfd, 0xc0 | reg,
3713 contents + roff - 1);
3715 else if (reg == 4)
3717 /* addq/addl -> addq/addl - addressing with %rsp/%r12
3718 is special */
3719 if (val == 0x4c)
3721 if (roff < 3)
3722 goto corrupt_input;
3723 bfd_put_8 (output_bfd, 0x49,
3724 contents + roff - 3);
3726 else if (!ABI_64_P (output_bfd) && val == 0x44)
3728 if (roff < 3)
3729 goto corrupt_input;
3730 bfd_put_8 (output_bfd, 0x41,
3731 contents + roff - 3);
3733 bfd_put_8 (output_bfd, 0x81,
3734 contents + roff - 2);
3735 bfd_put_8 (output_bfd, 0xc0 | reg,
3736 contents + roff - 1);
3738 else
3740 /* addq/addl -> leaq/leal */
3741 if (val == 0x4c)
3743 if (roff < 3)
3744 goto corrupt_input;
3745 bfd_put_8 (output_bfd, 0x4d,
3746 contents + roff - 3);
3748 else if (!ABI_64_P (output_bfd) && val == 0x44)
3750 if (roff < 3)
3751 goto corrupt_input;
3752 bfd_put_8 (output_bfd, 0x45,
3753 contents + roff - 3);
3755 bfd_put_8 (output_bfd, 0x8d,
3756 contents + roff - 2);
3757 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3758 contents + roff - 1);
3760 bfd_put_32 (output_bfd,
3761 elf_x86_64_tpoff (info, relocation),
3762 contents + roff);
3763 continue;
3765 else
3766 BFD_ASSERT (false);
3769 if (htab->elf.sgot == NULL)
3770 abort ();
3772 if (h != NULL)
3774 off = h->got.offset;
3775 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3777 else
3779 if (local_got_offsets == NULL)
3780 abort ();
3782 off = local_got_offsets[r_symndx];
3783 offplt = local_tlsdesc_gotents[r_symndx];
3786 if ((off & 1) != 0)
3787 off &= ~1;
3788 else
3790 Elf_Internal_Rela outrel;
3791 int dr_type, indx;
3792 asection *sreloc;
3794 if (htab->elf.srelgot == NULL)
3795 abort ();
3797 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3799 if (GOT_TLS_GDESC_P (tls_type))
3801 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
3802 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3803 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3804 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3805 + htab->elf.sgotplt->output_offset
3806 + offplt
3807 + htab->sgotplt_jump_table_size);
3808 sreloc = htab->elf.srelplt;
3809 if (indx == 0)
3810 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
3811 else
3812 outrel.r_addend = 0;
3813 elf_append_rela (output_bfd, sreloc, &outrel);
3816 sreloc = htab->elf.srelgot;
3818 outrel.r_offset = (htab->elf.sgot->output_section->vma
3819 + htab->elf.sgot->output_offset + off);
3821 if (GOT_TLS_GD_P (tls_type))
3822 dr_type = R_X86_64_DTPMOD64;
3823 else if (GOT_TLS_GDESC_P (tls_type))
3824 goto dr_done;
3825 else
3826 dr_type = R_X86_64_TPOFF64;
3828 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3829 outrel.r_addend = 0;
3830 if ((dr_type == R_X86_64_TPOFF64
3831 || dr_type == R_X86_64_TLSDESC) && indx == 0)
3832 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
3833 outrel.r_info = htab->r_info (indx, dr_type);
3835 elf_append_rela (output_bfd, sreloc, &outrel);
3837 if (GOT_TLS_GD_P (tls_type))
3839 if (indx == 0)
3841 BFD_ASSERT (! unresolved_reloc);
3842 bfd_put_64 (output_bfd,
3843 relocation - _bfd_x86_elf_dtpoff_base (info),
3844 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3846 else
3848 bfd_put_64 (output_bfd, 0,
3849 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3850 outrel.r_info = htab->r_info (indx,
3851 R_X86_64_DTPOFF64);
3852 outrel.r_offset += GOT_ENTRY_SIZE;
3853 elf_append_rela (output_bfd, sreloc,
3854 &outrel);
3858 dr_done:
3859 if (h != NULL)
3860 h->got.offset |= 1;
3861 else
3862 local_got_offsets[r_symndx] |= 1;
3865 if (off >= (bfd_vma) -2
3866 && ! GOT_TLS_GDESC_P (tls_type))
3867 abort ();
3868 if (r_type_tls == r_type)
3870 if (r_type == R_X86_64_GOTPC32_TLSDESC
3871 || r_type == R_X86_64_TLSDESC_CALL)
3872 relocation = htab->elf.sgotplt->output_section->vma
3873 + htab->elf.sgotplt->output_offset
3874 + offplt + htab->sgotplt_jump_table_size;
3875 else
3876 relocation = htab->elf.sgot->output_section->vma
3877 + htab->elf.sgot->output_offset + off;
3878 unresolved_reloc = false;
3880 else
3882 bfd_vma roff = rel->r_offset;
3884 if (r_type == R_X86_64_TLSGD)
3886 /* GD->IE transition. For 64bit, change
3887 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3888 .word 0x6666; rex64; call __tls_get_addr@PLT
3890 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3891 .byte 0x66; rex64
3892 call *__tls_get_addr@GOTPCREL(%rip
3893 which may be converted to
3894 addr32 call __tls_get_addr
3895 into:
3896 movq %fs:0, %rax
3897 addq foo@gottpoff(%rip), %rax
3898 For 32bit, change
3899 leaq foo@tlsgd(%rip), %rdi
3900 .word 0x6666; rex64; call __tls_get_addr@PLT
3902 leaq foo@tlsgd(%rip), %rdi
3903 .byte 0x66; rex64;
3904 call *__tls_get_addr@GOTPCREL(%rip)
3905 which may be converted to
3906 addr32 call __tls_get_addr
3907 into:
3908 movl %fs:0, %eax
3909 addq foo@gottpoff(%rip), %rax
3910 For largepic, change:
3911 leaq foo@tlsgd(%rip), %rdi
3912 movabsq $__tls_get_addr@pltoff, %rax
3913 addq %r15, %rax
3914 call *%rax
3915 into:
3916 movq %fs:0, %rax
3917 addq foo@gottpoff(%rax), %rax
3918 nopw 0x0(%rax,%rax,1) */
3919 int largepic = 0;
3920 if (ABI_64_P (output_bfd))
3922 if (contents[roff + 5] == 0xb8)
3924 if (roff < 3
3925 || (roff - 3 + 22) > input_section->size)
3926 goto corrupt_input;
3927 memcpy (contents + roff - 3,
3928 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
3929 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3930 largepic = 1;
3932 else
3934 if (roff < 4
3935 || (roff - 4 + 16) > input_section->size)
3936 goto corrupt_input;
3937 memcpy (contents + roff - 4,
3938 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3939 16);
3942 else
3944 if (roff < 3
3945 || (roff - 3 + 15) > input_section->size)
3946 goto corrupt_input;
3947 memcpy (contents + roff - 3,
3948 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3949 15);
3952 relocation = (htab->elf.sgot->output_section->vma
3953 + htab->elf.sgot->output_offset + off
3954 - roff
3955 - largepic
3956 - input_section->output_section->vma
3957 - input_section->output_offset
3958 - 12);
3959 bfd_put_32 (output_bfd, relocation,
3960 contents + roff + 8 + largepic);
3961 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
3962 rel++;
3963 wrel++;
3964 continue;
3966 else if (r_type == R_X86_64_GOTPC32_TLSDESC)
3968 /* GDesc -> IE transition.
3969 It's originally something like:
3970 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
3971 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
3973 Change it to:
3974 # before xchg %ax,%ax in LP64 mode.
3975 movq x@gottpoff(%rip), %rax
3976 # before nopl (%rax) in X32 mode.
3977 rex movl x@gottpoff(%rip), %eax
3980 /* Now modify the instruction as appropriate. To
3981 turn a lea into a mov in the form we use it, it
3982 suffices to change the second byte from 0x8d to
3983 0x8b. */
3984 if (roff < 2)
3985 goto corrupt_input;
3986 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3988 bfd_put_32 (output_bfd,
3989 htab->elf.sgot->output_section->vma
3990 + htab->elf.sgot->output_offset + off
3991 - rel->r_offset
3992 - input_section->output_section->vma
3993 - input_section->output_offset
3994 - 4,
3995 contents + roff);
3996 continue;
3998 else if (r_type == R_X86_64_TLSDESC_CALL)
4000 /* GDesc -> IE transition.
4001 It's originally:
4002 call *(%rax) <--- LP64 mode.
4003 call *(%eax) <--- X32 mode.
4005 Change it to:
4006 xchg %ax, %ax <-- LP64 mode.
4007 nopl (%rax) <-- X32 mode.
4010 unsigned int prefix = 0;
4011 if (!ABI_64_P (input_bfd))
4013 /* Check for call *x@tlsdesc(%eax). */
4014 if (contents[roff] == 0x67)
4015 prefix = 1;
4017 if (prefix)
4019 bfd_put_8 (output_bfd, 0x0f, contents + roff);
4020 bfd_put_8 (output_bfd, 0x1f, contents + roff + 1);
4021 bfd_put_8 (output_bfd, 0x00, contents + roff + 2);
4023 else
4025 bfd_put_8 (output_bfd, 0x66, contents + roff);
4026 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
4028 continue;
4030 else
4031 BFD_ASSERT (false);
4033 break;
4035 case R_X86_64_TLSLD:
4036 if (! elf_x86_64_tls_transition (info, input_bfd,
4037 input_section, contents,
4038 symtab_hdr, sym_hashes,
4039 &r_type, GOT_UNKNOWN, rel,
4040 relend, h, r_symndx, true))
4041 return false;
4043 if (r_type != R_X86_64_TLSLD)
4045 /* LD->LE transition:
4046 leaq foo@tlsld(%rip), %rdi
4047 call __tls_get_addr@PLT
4048 For 64bit, we change it into:
4049 .word 0x6666; .byte 0x66; movq %fs:0, %rax
4050 For 32bit, we change it into:
4051 nopl 0x0(%rax); movl %fs:0, %eax
4053 leaq foo@tlsld(%rip), %rdi;
4054 call *__tls_get_addr@GOTPCREL(%rip)
4055 which may be converted to
4056 addr32 call __tls_get_addr
4057 For 64bit, we change it into:
4058 .word 0x6666; .word 0x6666; movq %fs:0, %rax
4059 For 32bit, we change it into:
4060 nopw 0x0(%rax); movl %fs:0, %eax
4061 For largepic, change:
4062 leaq foo@tlsgd(%rip), %rdi
4063 movabsq $__tls_get_addr@pltoff, %rax
4064 addq %rbx, %rax
4065 call *%rax
4066 into
4067 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
4068 movq %fs:0, %eax */
4070 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
4071 if (ABI_64_P (output_bfd))
4073 if ((rel->r_offset + 5) >= input_section->size)
4074 goto corrupt_input;
4075 if (contents[rel->r_offset + 5] == 0xb8)
4077 if (rel->r_offset < 3
4078 || (rel->r_offset - 3 + 22) > input_section->size)
4079 goto corrupt_input;
4080 memcpy (contents + rel->r_offset - 3,
4081 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
4082 "\x64\x48\x8b\x04\x25\0\0\0", 22);
4084 else if (contents[rel->r_offset + 4] == 0xff
4085 || contents[rel->r_offset + 4] == 0x67)
4087 if (rel->r_offset < 3
4088 || (rel->r_offset - 3 + 13) > input_section->size)
4089 goto corrupt_input;
4090 memcpy (contents + rel->r_offset - 3,
4091 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
4092 13);
4095 else
4097 if (rel->r_offset < 3
4098 || (rel->r_offset - 3 + 12) > input_section->size)
4099 goto corrupt_input;
4100 memcpy (contents + rel->r_offset - 3,
4101 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
4104 else
4106 if ((rel->r_offset + 4) >= input_section->size)
4107 goto corrupt_input;
4108 if (contents[rel->r_offset + 4] == 0xff)
4110 if (rel->r_offset < 3
4111 || (rel->r_offset - 3 + 13) > input_section->size)
4112 goto corrupt_input;
4113 memcpy (contents + rel->r_offset - 3,
4114 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
4115 13);
4117 else
4119 if (rel->r_offset < 3
4120 || (rel->r_offset - 3 + 12) > input_section->size)
4121 goto corrupt_input;
4122 memcpy (contents + rel->r_offset - 3,
4123 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
4126 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
4127 and R_X86_64_PLTOFF64. */
4128 rel++;
4129 wrel++;
4130 continue;
4133 if (htab->elf.sgot == NULL)
4134 abort ();
4136 off = htab->tls_ld_or_ldm_got.offset;
4137 if (off & 1)
4138 off &= ~1;
4139 else
4141 Elf_Internal_Rela outrel;
4143 if (htab->elf.srelgot == NULL)
4144 abort ();
4146 outrel.r_offset = (htab->elf.sgot->output_section->vma
4147 + htab->elf.sgot->output_offset + off);
4149 bfd_put_64 (output_bfd, 0,
4150 htab->elf.sgot->contents + off);
4151 bfd_put_64 (output_bfd, 0,
4152 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4153 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
4154 outrel.r_addend = 0;
4155 elf_append_rela (output_bfd, htab->elf.srelgot,
4156 &outrel);
4157 htab->tls_ld_or_ldm_got.offset |= 1;
4159 relocation = htab->elf.sgot->output_section->vma
4160 + htab->elf.sgot->output_offset + off;
4161 unresolved_reloc = false;
4162 break;
4164 case R_X86_64_DTPOFF32:
4165 if (!bfd_link_executable (info)
4166 || (input_section->flags & SEC_CODE) == 0)
4167 relocation -= _bfd_x86_elf_dtpoff_base (info);
4168 else
4169 relocation = elf_x86_64_tpoff (info, relocation);
4170 break;
4172 case R_X86_64_TPOFF32:
4173 case R_X86_64_TPOFF64:
4174 BFD_ASSERT (bfd_link_executable (info));
4175 relocation = elf_x86_64_tpoff (info, relocation);
4176 break;
4178 case R_X86_64_DTPOFF64:
4179 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
4180 relocation -= _bfd_x86_elf_dtpoff_base (info);
4181 break;
4183 default:
4184 break;
4187 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4188 because such sections are not SEC_ALLOC and thus ld.so will
4189 not process them. */
4190 if (unresolved_reloc
4191 && !((input_section->flags & SEC_DEBUGGING) != 0
4192 && h->def_dynamic)
4193 && _bfd_elf_section_offset (output_bfd, info, input_section,
4194 rel->r_offset) != (bfd_vma) -1)
4196 switch (r_type)
4198 case R_X86_64_32S:
4199 sec = h->root.u.def.section;
4200 if ((info->nocopyreloc || eh->def_protected)
4201 && !(h->root.u.def.section->flags & SEC_CODE))
4202 return elf_x86_64_need_pic (info, input_bfd, input_section,
4203 h, NULL, NULL, howto);
4204 /* Fall through. */
4206 default:
4207 _bfd_error_handler
4208 /* xgettext:c-format */
4209 (_("%pB(%pA+%#" PRIx64 "): "
4210 "unresolvable %s relocation against symbol `%s'"),
4211 input_bfd,
4212 input_section,
4213 (uint64_t) rel->r_offset,
4214 howto->name,
4215 h->root.root.string);
4216 return false;
4220 do_relocation:
4221 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4222 contents, rel->r_offset,
4223 relocation, rel->r_addend);
4225 check_relocation_error:
4226 if (r != bfd_reloc_ok)
4228 const char *name;
4230 if (h != NULL)
4231 name = h->root.root.string;
4232 else
4234 name = bfd_elf_string_from_elf_section (input_bfd,
4235 symtab_hdr->sh_link,
4236 sym->st_name);
4237 if (name == NULL)
4238 return false;
4239 if (*name == '\0')
4240 name = bfd_section_name (sec);
4243 if (r == bfd_reloc_overflow)
4245 if (converted_reloc)
4247 info->callbacks->einfo
4248 ("%X%H:", input_bfd, input_section, rel->r_offset);
4249 info->callbacks->einfo
4250 (_(" failed to convert GOTPCREL relocation against "
4251 "'%s'; relink with --no-relax\n"),
4252 name);
4253 status = false;
4254 continue;
4256 (*info->callbacks->reloc_overflow)
4257 (info, (h ? &h->root : NULL), name, howto->name,
4258 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4260 else
4262 _bfd_error_handler
4263 /* xgettext:c-format */
4264 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
4265 input_bfd, input_section,
4266 (uint64_t) rel->r_offset, name, (int) r);
4267 return false;
4271 if (wrel != rel)
4272 *wrel = *rel;
4275 if (wrel != rel)
4277 Elf_Internal_Shdr *rel_hdr;
4278 size_t deleted = rel - wrel;
4280 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
4281 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4282 if (rel_hdr->sh_size == 0)
4284 /* It is too late to remove an empty reloc section. Leave
4285 one NONE reloc.
4286 ??? What is wrong with an empty section??? */
4287 rel_hdr->sh_size = rel_hdr->sh_entsize;
4288 deleted -= 1;
4290 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
4291 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4292 input_section->reloc_count -= deleted;
4295 return status;
4298 /* Finish up dynamic symbol handling. We set the contents of various
4299 dynamic sections here. */
4301 static bool
4302 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
4303 struct bfd_link_info *info,
4304 struct elf_link_hash_entry *h,
4305 Elf_Internal_Sym *sym)
4307 struct elf_x86_link_hash_table *htab;
4308 bool use_plt_second;
4309 struct elf_x86_link_hash_entry *eh;
4310 bool local_undefweak;
4312 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4313 if (htab == NULL)
4314 return false;
4316 /* Use the second PLT section only if there is .plt section. */
4317 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
4319 eh = (struct elf_x86_link_hash_entry *) h;
4320 if (eh->no_finish_dynamic_symbol)
4321 abort ();
4323 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
4324 resolved undefined weak symbols in executable so that their
4325 references have value 0 at run-time. */
4326 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
4328 if (h->plt.offset != (bfd_vma) -1)
4330 bfd_vma plt_index;
4331 bfd_vma got_offset, plt_offset;
4332 Elf_Internal_Rela rela;
4333 bfd_byte *loc;
4334 asection *plt, *gotplt, *relplt, *resolved_plt;
4335 const struct elf_backend_data *bed;
4336 bfd_vma plt_got_pcrel_offset;
4338 /* When building a static executable, use .iplt, .igot.plt and
4339 .rela.iplt sections for STT_GNU_IFUNC symbols. */
4340 if (htab->elf.splt != NULL)
4342 plt = htab->elf.splt;
4343 gotplt = htab->elf.sgotplt;
4344 relplt = htab->elf.srelplt;
4346 else
4348 plt = htab->elf.iplt;
4349 gotplt = htab->elf.igotplt;
4350 relplt = htab->elf.irelplt;
4353 VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
4355 /* Get the index in the procedure linkage table which
4356 corresponds to this symbol. This is the index of this symbol
4357 in all the symbols for which we are making plt entries. The
4358 first entry in the procedure linkage table is reserved.
4360 Get the offset into the .got table of the entry that
4361 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
4362 bytes. The first three are reserved for the dynamic linker.
4364 For static executables, we don't reserve anything. */
4366 if (plt == htab->elf.splt)
4368 got_offset = (h->plt.offset / htab->plt.plt_entry_size
4369 - htab->plt.has_plt0);
4370 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
4372 else
4374 got_offset = h->plt.offset / htab->plt.plt_entry_size;
4375 got_offset = got_offset * GOT_ENTRY_SIZE;
4378 /* Fill in the entry in the procedure linkage table. */
4379 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
4380 htab->plt.plt_entry_size);
4381 if (use_plt_second)
4383 memcpy (htab->plt_second->contents + eh->plt_second.offset,
4384 htab->non_lazy_plt->plt_entry,
4385 htab->non_lazy_plt->plt_entry_size);
4387 resolved_plt = htab->plt_second;
4388 plt_offset = eh->plt_second.offset;
4390 else
4392 resolved_plt = plt;
4393 plt_offset = h->plt.offset;
4396 /* Insert the relocation positions of the plt section. */
4398 /* Put offset the PC-relative instruction referring to the GOT entry,
4399 subtracting the size of that instruction. */
4400 plt_got_pcrel_offset = (gotplt->output_section->vma
4401 + gotplt->output_offset
4402 + got_offset
4403 - resolved_plt->output_section->vma
4404 - resolved_plt->output_offset
4405 - plt_offset
4406 - htab->plt.plt_got_insn_size);
4408 /* Check PC-relative offset overflow in PLT entry. */
4409 if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
4410 /* xgettext:c-format */
4411 info->callbacks->einfo (_("%F%pB: PC-relative offset overflow in PLT entry for `%s'\n"),
4412 output_bfd, h->root.root.string);
4414 bfd_put_32 (output_bfd, plt_got_pcrel_offset,
4415 (resolved_plt->contents + plt_offset
4416 + htab->plt.plt_got_offset));
4418 /* Fill in the entry in the global offset table, initially this
4419 points to the second part of the PLT entry. Leave the entry
4420 as zero for undefined weak symbol in PIE. No PLT relocation
4421 against undefined weak symbol in PIE. */
4422 if (!local_undefweak)
4424 if (htab->plt.has_plt0)
4425 bfd_put_64 (output_bfd, (plt->output_section->vma
4426 + plt->output_offset
4427 + h->plt.offset
4428 + htab->lazy_plt->plt_lazy_offset),
4429 gotplt->contents + got_offset);
4431 /* Fill in the entry in the .rela.plt section. */
4432 rela.r_offset = (gotplt->output_section->vma
4433 + gotplt->output_offset
4434 + got_offset);
4435 if (PLT_LOCAL_IFUNC_P (info, h))
4437 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
4438 h->root.root.string,
4439 h->root.u.def.section->owner);
4441 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4442 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
4443 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4444 rela.r_addend = (h->root.u.def.value
4445 + h->root.u.def.section->output_section->vma
4446 + h->root.u.def.section->output_offset);
4448 if (htab->params->report_relative_reloc)
4449 _bfd_x86_elf_link_report_relative_reloc
4450 (info, relplt, h, sym, "R_X86_64_IRELATIVE", &rela);
4452 /* R_X86_64_IRELATIVE comes last. */
4453 plt_index = htab->next_irelative_index--;
4455 else
4457 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
4458 if (htab->params->mark_plt)
4459 rela.r_addend = (resolved_plt->output_section->vma
4460 + plt_offset
4461 + htab->plt.plt_indirect_branch_offset);
4462 else
4463 rela.r_addend = 0;
4464 plt_index = htab->next_jump_slot_index++;
4467 /* Don't fill the second and third slots in PLT entry for
4468 static executables nor without PLT0. */
4469 if (plt == htab->elf.splt && htab->plt.has_plt0)
4471 bfd_vma plt0_offset
4472 = h->plt.offset + htab->lazy_plt->plt_plt_insn_end;
4474 /* Put relocation index. */
4475 bfd_put_32 (output_bfd, plt_index,
4476 (plt->contents + h->plt.offset
4477 + htab->lazy_plt->plt_reloc_offset));
4479 /* Put offset for jmp .PLT0 and check for overflow. We don't
4480 check relocation index for overflow since branch displacement
4481 will overflow first. */
4482 if (plt0_offset > 0x80000000)
4483 /* xgettext:c-format */
4484 info->callbacks->einfo (_("%F%pB: branch displacement overflow in PLT entry for `%s'\n"),
4485 output_bfd, h->root.root.string);
4486 bfd_put_32 (output_bfd, - plt0_offset,
4487 (plt->contents + h->plt.offset
4488 + htab->lazy_plt->plt_plt_offset));
4491 bed = get_elf_backend_data (output_bfd);
4492 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
4493 bed->s->swap_reloca_out (output_bfd, &rela, loc);
4496 else if (eh->plt_got.offset != (bfd_vma) -1)
4498 bfd_vma got_offset, plt_offset;
4499 asection *plt, *got;
4500 bool got_after_plt;
4501 int32_t got_pcrel_offset;
4503 /* Set the entry in the GOT procedure linkage table. */
4504 plt = htab->plt_got;
4505 got = htab->elf.sgot;
4506 got_offset = h->got.offset;
4508 if (got_offset == (bfd_vma) -1
4509 || (h->type == STT_GNU_IFUNC && h->def_regular)
4510 || plt == NULL
4511 || got == NULL)
4512 abort ();
4514 /* Use the non-lazy PLT entry template for the GOT PLT since they
4515 are the identical. */
4516 /* Fill in the entry in the GOT procedure linkage table. */
4517 plt_offset = eh->plt_got.offset;
4518 memcpy (plt->contents + plt_offset,
4519 htab->non_lazy_plt->plt_entry,
4520 htab->non_lazy_plt->plt_entry_size);
4522 /* Put offset the PC-relative instruction referring to the GOT
4523 entry, subtracting the size of that instruction. */
4524 got_pcrel_offset = (got->output_section->vma
4525 + got->output_offset
4526 + got_offset
4527 - plt->output_section->vma
4528 - plt->output_offset
4529 - plt_offset
4530 - htab->non_lazy_plt->plt_got_insn_size);
4532 /* Check PC-relative offset overflow in GOT PLT entry. */
4533 got_after_plt = got->output_section->vma > plt->output_section->vma;
4534 if ((got_after_plt && got_pcrel_offset < 0)
4535 || (!got_after_plt && got_pcrel_offset > 0))
4536 /* xgettext:c-format */
4537 info->callbacks->einfo (_("%F%pB: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
4538 output_bfd, h->root.root.string);
4540 bfd_put_32 (output_bfd, got_pcrel_offset,
4541 (plt->contents + plt_offset
4542 + htab->non_lazy_plt->plt_got_offset));
4545 if (!local_undefweak
4546 && !h->def_regular
4547 && (h->plt.offset != (bfd_vma) -1
4548 || eh->plt_got.offset != (bfd_vma) -1))
4550 /* Mark the symbol as undefined, rather than as defined in
4551 the .plt section. Leave the value if there were any
4552 relocations where pointer equality matters (this is a clue
4553 for the dynamic linker, to make function pointer
4554 comparisons work between an application and shared
4555 library), otherwise set it to zero. If a function is only
4556 called from a binary, there is no need to slow down
4557 shared libraries because of that. */
4558 sym->st_shndx = SHN_UNDEF;
4559 if (!h->pointer_equality_needed)
4560 sym->st_value = 0;
4563 _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
4565 /* Don't generate dynamic GOT relocation against undefined weak
4566 symbol in executable. */
4567 if (h->got.offset != (bfd_vma) -1
4568 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
4569 && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
4570 && !local_undefweak)
4572 Elf_Internal_Rela rela;
4573 asection *relgot = htab->elf.srelgot;
4574 const char *relative_reloc_name = NULL;
4575 bool generate_dynamic_reloc = true;
4577 /* This symbol has an entry in the global offset table. Set it
4578 up. */
4579 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4580 abort ();
4582 rela.r_offset = (htab->elf.sgot->output_section->vma
4583 + htab->elf.sgot->output_offset
4584 + (h->got.offset &~ (bfd_vma) 1));
4586 /* If this is a static link, or it is a -Bsymbolic link and the
4587 symbol is defined locally or was forced to be local because
4588 of a version file, we just want to emit a RELATIVE reloc.
4589 The entry in the global offset table will already have been
4590 initialized in the relocate_section function. */
4591 if (h->def_regular
4592 && h->type == STT_GNU_IFUNC)
4594 if (h->plt.offset == (bfd_vma) -1)
4596 /* STT_GNU_IFUNC is referenced without PLT. */
4597 if (htab->elf.splt == NULL)
4599 /* use .rel[a].iplt section to store .got relocations
4600 in static executable. */
4601 relgot = htab->elf.irelplt;
4603 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
4605 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
4606 h->root.root.string,
4607 h->root.u.def.section->owner);
4609 rela.r_info = htab->r_info (0,
4610 R_X86_64_IRELATIVE);
4611 rela.r_addend = (h->root.u.def.value
4612 + h->root.u.def.section->output_section->vma
4613 + h->root.u.def.section->output_offset);
4614 relative_reloc_name = "R_X86_64_IRELATIVE";
4616 else
4617 goto do_glob_dat;
4619 else if (bfd_link_pic (info))
4621 /* Generate R_X86_64_GLOB_DAT. */
4622 goto do_glob_dat;
4624 else
4626 asection *plt;
4627 bfd_vma plt_offset;
4629 if (!h->pointer_equality_needed)
4630 abort ();
4632 /* For non-shared object, we can't use .got.plt, which
4633 contains the real function addres if we need pointer
4634 equality. We load the GOT entry with the PLT entry. */
4635 if (htab->plt_second != NULL)
4637 plt = htab->plt_second;
4638 plt_offset = eh->plt_second.offset;
4640 else
4642 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4643 plt_offset = h->plt.offset;
4645 bfd_put_64 (output_bfd, (plt->output_section->vma
4646 + plt->output_offset
4647 + plt_offset),
4648 htab->elf.sgot->contents + h->got.offset);
4649 return true;
4652 else if (bfd_link_pic (info)
4653 && SYMBOL_REFERENCES_LOCAL_P (info, h))
4655 if (!SYMBOL_DEFINED_NON_SHARED_P (h))
4656 return false;
4657 BFD_ASSERT((h->got.offset & 1) != 0);
4658 if (info->enable_dt_relr)
4659 generate_dynamic_reloc = false;
4660 else
4662 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4663 rela.r_addend = (h->root.u.def.value
4664 + h->root.u.def.section->output_section->vma
4665 + h->root.u.def.section->output_offset);
4666 relative_reloc_name = "R_X86_64_RELATIVE";
4669 else
4671 BFD_ASSERT((h->got.offset & 1) == 0);
4672 do_glob_dat:
4673 bfd_put_64 (output_bfd, (bfd_vma) 0,
4674 htab->elf.sgot->contents + h->got.offset);
4675 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
4676 rela.r_addend = 0;
4679 if (generate_dynamic_reloc)
4681 if (relative_reloc_name != NULL
4682 && htab->params->report_relative_reloc)
4683 _bfd_x86_elf_link_report_relative_reloc
4684 (info, relgot, h, sym, relative_reloc_name, &rela);
4686 elf_append_rela (output_bfd, relgot, &rela);
4690 if (h->needs_copy)
4692 Elf_Internal_Rela rela;
4693 asection *s;
4695 /* This symbol needs a copy reloc. Set it up. */
4696 VERIFY_COPY_RELOC (h, htab)
4698 rela.r_offset = (h->root.u.def.value
4699 + h->root.u.def.section->output_section->vma
4700 + h->root.u.def.section->output_offset);
4701 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
4702 rela.r_addend = 0;
4703 if (h->root.u.def.section == htab->elf.sdynrelro)
4704 s = htab->elf.sreldynrelro;
4705 else
4706 s = htab->elf.srelbss;
4707 elf_append_rela (output_bfd, s, &rela);
4710 return true;
4713 /* Finish up local dynamic symbol handling. We set the contents of
4714 various dynamic sections here. */
4716 static int
4717 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
4719 struct elf_link_hash_entry *h
4720 = (struct elf_link_hash_entry *) *slot;
4721 struct bfd_link_info *info
4722 = (struct bfd_link_info *) inf;
4724 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
4725 info, h, NULL);
4728 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4729 here since undefined weak symbol may not be dynamic and may not be
4730 called for elf_x86_64_finish_dynamic_symbol. */
4732 static bool
4733 elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4734 void *inf)
4736 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4737 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4739 if (h->root.type != bfd_link_hash_undefweak
4740 || h->dynindx != -1)
4741 return true;
4743 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
4744 info, h, NULL);
4747 /* Used to decide how to sort relocs in an optimal manner for the
4748 dynamic linker, before writing them out. */
4750 static enum elf_reloc_type_class
4751 elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
4752 const asection *rel_sec ATTRIBUTE_UNUSED,
4753 const Elf_Internal_Rela *rela)
4755 bfd *abfd = info->output_bfd;
4756 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4757 struct elf_x86_link_hash_table *htab
4758 = elf_x86_hash_table (info, X86_64_ELF_DATA);
4760 if (htab->elf.dynsym != NULL
4761 && htab->elf.dynsym->contents != NULL)
4763 /* Check relocation against STT_GNU_IFUNC symbol if there are
4764 dynamic symbols. */
4765 unsigned long r_symndx = htab->r_sym (rela->r_info);
4766 if (r_symndx != STN_UNDEF)
4768 Elf_Internal_Sym sym;
4769 if (!bed->s->swap_symbol_in (abfd,
4770 (htab->elf.dynsym->contents
4771 + r_symndx * bed->s->sizeof_sym),
4772 0, &sym))
4773 abort ();
4775 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4776 return reloc_class_ifunc;
4780 switch ((int) ELF32_R_TYPE (rela->r_info))
4782 case R_X86_64_IRELATIVE:
4783 return reloc_class_ifunc;
4784 case R_X86_64_RELATIVE:
4785 case R_X86_64_RELATIVE64:
4786 return reloc_class_relative;
4787 case R_X86_64_JUMP_SLOT:
4788 return reloc_class_plt;
4789 case R_X86_64_COPY:
4790 return reloc_class_copy;
4791 default:
4792 return reloc_class_normal;
4796 /* Finish up the dynamic sections. */
4798 static bool
4799 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
4800 struct bfd_link_info *info)
4802 struct elf_x86_link_hash_table *htab;
4804 htab = _bfd_x86_elf_finish_dynamic_sections (output_bfd, info);
4805 if (htab == NULL)
4806 return false;
4808 if (! htab->elf.dynamic_sections_created)
4809 return true;
4811 if (htab->elf.splt && htab->elf.splt->size > 0)
4813 if (bfd_is_abs_section (htab->elf.splt->output_section))
4815 info->callbacks->einfo
4816 (_("%F%P: discarded output section: `%pA'\n"),
4817 htab->elf.splt);
4818 return false;
4821 elf_section_data (htab->elf.splt->output_section)
4822 ->this_hdr.sh_entsize = htab->plt.plt_entry_size;
4824 if (htab->plt.has_plt0)
4826 /* Fill in the special first entry in the procedure linkage
4827 table. */
4828 memcpy (htab->elf.splt->contents,
4829 htab->lazy_plt->plt0_entry,
4830 htab->lazy_plt->plt0_entry_size);
4831 /* Add offset for pushq GOT+8(%rip), since the instruction
4832 uses 6 bytes subtract this value. */
4833 bfd_put_32 (output_bfd,
4834 (htab->elf.sgotplt->output_section->vma
4835 + htab->elf.sgotplt->output_offset
4837 - htab->elf.splt->output_section->vma
4838 - htab->elf.splt->output_offset
4839 - 6),
4840 (htab->elf.splt->contents
4841 + htab->lazy_plt->plt0_got1_offset));
4842 /* Add offset for the PC-relative instruction accessing
4843 GOT+16, subtracting the offset to the end of that
4844 instruction. */
4845 bfd_put_32 (output_bfd,
4846 (htab->elf.sgotplt->output_section->vma
4847 + htab->elf.sgotplt->output_offset
4848 + 16
4849 - htab->elf.splt->output_section->vma
4850 - htab->elf.splt->output_offset
4851 - htab->lazy_plt->plt0_got2_insn_end),
4852 (htab->elf.splt->contents
4853 + htab->lazy_plt->plt0_got2_offset));
4856 if (htab->elf.tlsdesc_plt)
4858 bfd_put_64 (output_bfd, (bfd_vma) 0,
4859 htab->elf.sgot->contents + htab->elf.tlsdesc_got);
4861 memcpy (htab->elf.splt->contents + htab->elf.tlsdesc_plt,
4862 htab->lazy_plt->plt_tlsdesc_entry,
4863 htab->lazy_plt->plt_tlsdesc_entry_size);
4865 /* Add offset for pushq GOT+8(%rip), since ENDBR64 uses 4
4866 bytes and the instruction uses 6 bytes, subtract these
4867 values. */
4868 bfd_put_32 (output_bfd,
4869 (htab->elf.sgotplt->output_section->vma
4870 + htab->elf.sgotplt->output_offset
4872 - htab->elf.splt->output_section->vma
4873 - htab->elf.splt->output_offset
4874 - htab->elf.tlsdesc_plt
4875 - htab->lazy_plt->plt_tlsdesc_got1_insn_end),
4876 (htab->elf.splt->contents
4877 + htab->elf.tlsdesc_plt
4878 + htab->lazy_plt->plt_tlsdesc_got1_offset));
4879 /* Add offset for indirect branch via GOT+TDG, where TDG
4880 stands for htab->tlsdesc_got, subtracting the offset
4881 to the end of that instruction. */
4882 bfd_put_32 (output_bfd,
4883 (htab->elf.sgot->output_section->vma
4884 + htab->elf.sgot->output_offset
4885 + htab->elf.tlsdesc_got
4886 - htab->elf.splt->output_section->vma
4887 - htab->elf.splt->output_offset
4888 - htab->elf.tlsdesc_plt
4889 - htab->lazy_plt->plt_tlsdesc_got2_insn_end),
4890 (htab->elf.splt->contents
4891 + htab->elf.tlsdesc_plt
4892 + htab->lazy_plt->plt_tlsdesc_got2_offset));
4896 /* Fill PLT entries for undefined weak symbols in PIE. */
4897 if (bfd_link_pie (info))
4898 bfd_hash_traverse (&info->hash->table,
4899 elf_x86_64_pie_finish_undefweak_symbol,
4900 info);
4902 return true;
4905 /* Fill PLT/GOT entries and allocate dynamic relocations for local
4906 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4907 It has to be done before elf_link_sort_relocs is called so that
4908 dynamic relocations are properly sorted. */
4910 static bool
4911 elf_x86_64_output_arch_local_syms
4912 (bfd *output_bfd ATTRIBUTE_UNUSED,
4913 struct bfd_link_info *info,
4914 void *flaginfo ATTRIBUTE_UNUSED,
4915 int (*func) (void *, const char *,
4916 Elf_Internal_Sym *,
4917 asection *,
4918 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4920 struct elf_x86_link_hash_table *htab
4921 = elf_x86_hash_table (info, X86_64_ELF_DATA);
4922 if (htab == NULL)
4923 return false;
4925 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4926 htab_traverse (htab->loc_hash_table,
4927 elf_x86_64_finish_local_dynamic_symbol,
4928 info);
4930 return true;
4933 /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4934 dynamic relocations. */
4936 static long
4937 elf_x86_64_get_synthetic_symtab (bfd *abfd,
4938 long symcount ATTRIBUTE_UNUSED,
4939 asymbol **syms ATTRIBUTE_UNUSED,
4940 long dynsymcount,
4941 asymbol **dynsyms,
4942 asymbol **ret)
4944 long count, i, n;
4945 int j;
4946 bfd_byte *plt_contents;
4947 long relsize;
4948 const struct elf_x86_lazy_plt_layout *lazy_plt;
4949 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4950 const struct elf_x86_lazy_plt_layout *lazy_bnd_plt;
4951 const struct elf_x86_non_lazy_plt_layout *non_lazy_bnd_plt;
4952 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4953 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4954 const struct elf_x86_lazy_plt_layout *x32_lazy_ibt_plt;
4955 const struct elf_x86_non_lazy_plt_layout *x32_non_lazy_ibt_plt;
4956 asection *plt;
4957 enum elf_x86_plt_type plt_type;
4958 struct elf_x86_plt plts[] =
4960 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4961 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4962 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4963 { ".plt.bnd", NULL, NULL, plt_second, 0, 0, 0, 0 },
4964 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4967 *ret = NULL;
4969 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4970 return 0;
4972 if (dynsymcount <= 0)
4973 return 0;
4975 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4976 if (relsize <= 0)
4977 return -1;
4979 lazy_plt = &elf_x86_64_lazy_plt;
4980 non_lazy_plt = &elf_x86_64_non_lazy_plt;
4981 lazy_bnd_plt = &elf_x86_64_lazy_bnd_plt;
4982 non_lazy_bnd_plt = &elf_x86_64_non_lazy_bnd_plt;
4983 if (ABI_64_P (abfd))
4985 lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
4986 non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
4987 x32_lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
4988 x32_non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
4990 else
4992 lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
4993 non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
4994 x32_lazy_ibt_plt = NULL;
4995 x32_non_lazy_ibt_plt = NULL;
4998 count = 0;
4999 for (j = 0; plts[j].name != NULL; j++)
5001 plt = bfd_get_section_by_name (abfd, plts[j].name);
5002 if (plt == NULL
5003 || plt->size == 0
5004 || (plt->flags & SEC_HAS_CONTENTS) == 0)
5005 continue;
5007 /* Get the PLT section contents. */
5008 if (!bfd_malloc_and_get_section (abfd, plt, &plt_contents))
5009 break;
5011 /* Check what kind of PLT it is. */
5012 plt_type = plt_unknown;
5013 if (plts[j].type == plt_unknown
5014 && (plt->size >= (lazy_plt->plt_entry_size
5015 + lazy_plt->plt_entry_size)))
5017 /* Match lazy PLT first. Need to check the first two
5018 instructions. */
5019 if ((memcmp (plt_contents, lazy_plt->plt0_entry,
5020 lazy_plt->plt0_got1_offset) == 0)
5021 && (memcmp (plt_contents + 6, lazy_plt->plt0_entry + 6,
5022 2) == 0))
5024 if (x32_lazy_ibt_plt != NULL
5025 && (memcmp (plt_contents
5026 + x32_lazy_ibt_plt->plt_entry_size,
5027 x32_lazy_ibt_plt->plt_entry,
5028 x32_lazy_ibt_plt->plt_got_offset) == 0))
5030 /* The fist entry in the x32 lazy IBT PLT is the same
5031 as the lazy PLT. */
5032 plt_type = plt_lazy | plt_second;
5033 lazy_plt = x32_lazy_ibt_plt;
5035 else
5036 plt_type = plt_lazy;
5038 else if (lazy_bnd_plt != NULL
5039 && (memcmp (plt_contents, lazy_bnd_plt->plt0_entry,
5040 lazy_bnd_plt->plt0_got1_offset) == 0)
5041 && (memcmp (plt_contents + 6,
5042 lazy_bnd_plt->plt0_entry + 6, 3) == 0))
5044 plt_type = plt_lazy | plt_second;
5045 /* The fist entry in the lazy IBT PLT is the same as the
5046 lazy BND PLT. */
5047 if ((memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
5048 lazy_ibt_plt->plt_entry,
5049 lazy_ibt_plt->plt_got_offset) == 0))
5050 lazy_plt = lazy_ibt_plt;
5051 else
5052 lazy_plt = lazy_bnd_plt;
5056 if (non_lazy_plt != NULL
5057 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
5058 && plt->size >= non_lazy_plt->plt_entry_size)
5060 /* Match non-lazy PLT. */
5061 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
5062 non_lazy_plt->plt_got_offset) == 0)
5063 plt_type = plt_non_lazy;
5066 if (plt_type == plt_unknown || plt_type == plt_second)
5068 if (non_lazy_bnd_plt != NULL
5069 && plt->size >= non_lazy_bnd_plt->plt_entry_size
5070 && (memcmp (plt_contents, non_lazy_bnd_plt->plt_entry,
5071 non_lazy_bnd_plt->plt_got_offset) == 0))
5073 /* Match BND PLT. */
5074 plt_type = plt_second;
5075 non_lazy_plt = non_lazy_bnd_plt;
5077 else if (non_lazy_ibt_plt != NULL
5078 && plt->size >= non_lazy_ibt_plt->plt_entry_size
5079 && (memcmp (plt_contents,
5080 non_lazy_ibt_plt->plt_entry,
5081 non_lazy_ibt_plt->plt_got_offset) == 0))
5083 /* Match IBT PLT. */
5084 plt_type = plt_second;
5085 non_lazy_plt = non_lazy_ibt_plt;
5087 else if (x32_non_lazy_ibt_plt != NULL
5088 && plt->size >= x32_non_lazy_ibt_plt->plt_entry_size
5089 && (memcmp (plt_contents,
5090 x32_non_lazy_ibt_plt->plt_entry,
5091 x32_non_lazy_ibt_plt->plt_got_offset) == 0))
5093 /* Match x32 IBT PLT. */
5094 plt_type = plt_second;
5095 non_lazy_plt = x32_non_lazy_ibt_plt;
5099 if (plt_type == plt_unknown)
5101 free (plt_contents);
5102 continue;
5105 plts[j].sec = plt;
5106 plts[j].type = plt_type;
5108 if ((plt_type & plt_lazy))
5110 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
5111 plts[j].plt_got_insn_size = lazy_plt->plt_got_insn_size;
5112 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
5113 /* Skip PLT0 in lazy PLT. */
5114 i = 1;
5116 else
5118 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
5119 plts[j].plt_got_insn_size = non_lazy_plt->plt_got_insn_size;
5120 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
5121 i = 0;
5124 /* Skip lazy PLT when the second PLT is used. */
5125 if (plt_type == (plt_lazy | plt_second))
5126 plts[j].count = 0;
5127 else
5129 n = plt->size / plts[j].plt_entry_size;
5130 plts[j].count = n;
5131 count += n - i;
5134 plts[j].contents = plt_contents;
5137 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
5138 (bfd_vma) 0, plts, dynsyms,
5139 ret);
5142 /* Handle an x86-64 specific section when reading an object file. This
5143 is called when elfcode.h finds a section with an unknown type. */
5145 static bool
5146 elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
5147 const char *name, int shindex)
5149 if (hdr->sh_type != SHT_X86_64_UNWIND)
5150 return false;
5152 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
5153 return false;
5155 return true;
5158 /* Hook called by the linker routine which adds symbols from an object
5159 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
5160 of .bss. */
5162 static bool
5163 elf_x86_64_add_symbol_hook (bfd *abfd,
5164 struct bfd_link_info *info ATTRIBUTE_UNUSED,
5165 Elf_Internal_Sym *sym,
5166 const char **namep ATTRIBUTE_UNUSED,
5167 flagword *flagsp ATTRIBUTE_UNUSED,
5168 asection **secp,
5169 bfd_vma *valp)
5171 asection *lcomm;
5173 switch (sym->st_shndx)
5175 case SHN_X86_64_LCOMMON:
5176 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
5177 if (lcomm == NULL)
5179 lcomm = bfd_make_section_with_flags (abfd,
5180 "LARGE_COMMON",
5181 (SEC_ALLOC
5182 | SEC_IS_COMMON
5183 | SEC_LINKER_CREATED));
5184 if (lcomm == NULL)
5185 return false;
5186 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
5188 *secp = lcomm;
5189 *valp = sym->st_size;
5190 return true;
5193 return true;
5197 /* Given a BFD section, try to locate the corresponding ELF section
5198 index. */
5200 static bool
5201 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5202 asection *sec, int *index_return)
5204 if (sec == &_bfd_elf_large_com_section)
5206 *index_return = SHN_X86_64_LCOMMON;
5207 return true;
5209 return false;
5212 /* Process a symbol. */
5214 static void
5215 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5216 asymbol *asym)
5218 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5220 switch (elfsym->internal_elf_sym.st_shndx)
5222 case SHN_X86_64_LCOMMON:
5223 asym->section = &_bfd_elf_large_com_section;
5224 asym->value = elfsym->internal_elf_sym.st_size;
5225 /* Common symbol doesn't set BSF_GLOBAL. */
5226 asym->flags &= ~BSF_GLOBAL;
5227 break;
5231 static bool
5232 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
5234 return (sym->st_shndx == SHN_COMMON
5235 || sym->st_shndx == SHN_X86_64_LCOMMON);
5238 static unsigned int
5239 elf_x86_64_common_section_index (asection *sec)
5241 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5242 return SHN_COMMON;
5243 else
5244 return SHN_X86_64_LCOMMON;
5247 static asection *
5248 elf_x86_64_common_section (asection *sec)
5250 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5251 return bfd_com_section_ptr;
5252 else
5253 return &_bfd_elf_large_com_section;
5256 static bool
5257 elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
5258 const Elf_Internal_Sym *sym,
5259 asection **psec,
5260 bool newdef,
5261 bool olddef,
5262 bfd *oldbfd,
5263 const asection *oldsec)
5265 /* A normal common symbol and a large common symbol result in a
5266 normal common symbol. We turn the large common symbol into a
5267 normal one. */
5268 if (!olddef
5269 && h->root.type == bfd_link_hash_common
5270 && !newdef
5271 && bfd_is_com_section (*psec)
5272 && oldsec != *psec)
5274 if (sym->st_shndx == SHN_COMMON
5275 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
5277 h->root.u.c.p->section
5278 = bfd_make_section_old_way (oldbfd, "COMMON");
5279 h->root.u.c.p->section->flags = SEC_ALLOC;
5281 else if (sym->st_shndx == SHN_X86_64_LCOMMON
5282 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
5283 *psec = bfd_com_section_ptr;
5286 return true;
5289 static bool
5290 elf_x86_64_section_flags (const Elf_Internal_Shdr *hdr)
5292 if ((hdr->sh_flags & SHF_X86_64_LARGE) != 0)
5293 hdr->bfd_section->flags |= SEC_ELF_LARGE;
5295 return true;
5298 static bool
5299 elf_x86_64_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
5300 Elf_Internal_Shdr *hdr, asection *sec)
5302 if (sec->flags & SEC_ELF_LARGE)
5303 hdr->sh_flags |= SHF_X86_64_LARGE;
5305 return true;
5308 static bool
5309 elf_x86_64_copy_private_section_data (bfd *ibfd, asection *isec,
5310 bfd *obfd, asection *osec)
5312 if (!_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec))
5313 return false;
5315 /* objcopy --set-section-flags without "large" drops SHF_X86_64_LARGE. */
5316 if (ibfd != obfd)
5317 elf_section_flags (osec) &= ~SHF_X86_64_LARGE;
5319 return true;
5322 static int
5323 elf_x86_64_additional_program_headers (bfd *abfd,
5324 struct bfd_link_info *info ATTRIBUTE_UNUSED)
5326 asection *s;
5327 int count = 0;
5329 /* Check to see if we need a large readonly segment. */
5330 s = bfd_get_section_by_name (abfd, ".lrodata");
5331 if (s && (s->flags & SEC_LOAD))
5332 count++;
5334 /* Check to see if we need a large data segment. Since .lbss sections
5335 is placed right after the .bss section, there should be no need for
5336 a large data segment just because of .lbss. */
5337 s = bfd_get_section_by_name (abfd, ".ldata");
5338 if (s && (s->flags & SEC_LOAD))
5339 count++;
5341 return count;
5344 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
5346 static bool
5347 elf_x86_64_relocs_compatible (const bfd_target *input,
5348 const bfd_target *output)
5350 return ((xvec_get_elf_backend_data (input)->s->elfclass
5351 == xvec_get_elf_backend_data (output)->s->elfclass)
5352 && _bfd_elf_relocs_compatible (input, output));
5355 /* Set up x86-64 GNU properties. Return the first relocatable ELF input
5356 with GNU properties if found. Otherwise, return NULL. */
5358 static bfd *
5359 elf_x86_64_link_setup_gnu_properties (struct bfd_link_info *info)
5361 struct elf_x86_init_table init_table;
5362 const struct elf_backend_data *bed;
5363 struct elf_x86_link_hash_table *htab;
5365 if ((int) R_X86_64_standard >= (int) R_X86_64_converted_reloc_bit
5366 || (int) R_X86_64_max <= (int) R_X86_64_converted_reloc_bit
5367 || ((int) (R_X86_64_GNU_VTINHERIT | R_X86_64_converted_reloc_bit)
5368 != (int) R_X86_64_GNU_VTINHERIT)
5369 || ((int) (R_X86_64_GNU_VTENTRY | R_X86_64_converted_reloc_bit)
5370 != (int) R_X86_64_GNU_VTENTRY))
5371 abort ();
5373 /* This is unused for x86-64. */
5374 init_table.plt0_pad_byte = 0x90;
5376 bed = get_elf_backend_data (info->output_bfd);
5377 htab = elf_x86_hash_table (info, bed->target_id);
5378 if (!htab)
5379 abort ();
5381 init_table.lazy_plt = &elf_x86_64_lazy_plt;
5382 init_table.non_lazy_plt = &elf_x86_64_non_lazy_plt;
5384 init_table.lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
5385 init_table.non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
5387 if (ABI_64_P (info->output_bfd))
5389 init_table.sframe_lazy_plt = &elf_x86_64_sframe_plt;
5390 init_table.sframe_non_lazy_plt = &elf_x86_64_sframe_non_lazy_plt;
5391 init_table.sframe_lazy_ibt_plt = &elf_x86_64_sframe_plt;
5392 init_table.sframe_non_lazy_ibt_plt = &elf_x86_64_sframe_non_lazy_plt;
5394 else
5396 /* SFrame is not supported for non AMD64. */
5397 init_table.sframe_lazy_plt = NULL;
5398 init_table.sframe_non_lazy_plt = NULL;
5401 if (ABI_64_P (info->output_bfd))
5403 init_table.r_info = elf64_r_info;
5404 init_table.r_sym = elf64_r_sym;
5406 else
5408 init_table.r_info = elf32_r_info;
5409 init_table.r_sym = elf32_r_sym;
5412 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
5415 static const struct bfd_elf_special_section
5416 elf_x86_64_special_sections[]=
5418 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5419 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5420 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
5421 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5422 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5423 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5424 { NULL, 0, 0, 0, 0 }
5427 #define TARGET_LITTLE_SYM x86_64_elf64_vec
5428 #define TARGET_LITTLE_NAME "elf64-x86-64"
5429 #define ELF_ARCH bfd_arch_i386
5430 #define ELF_TARGET_ID X86_64_ELF_DATA
5431 #define ELF_MACHINE_CODE EM_X86_64
5432 #define ELF_MAXPAGESIZE 0x1000
5433 #define ELF_COMMONPAGESIZE 0x1000
5435 #define elf_backend_can_gc_sections 1
5436 #define elf_backend_can_refcount 1
5437 #define elf_backend_want_got_plt 1
5438 #define elf_backend_plt_readonly 1
5439 #define elf_backend_want_plt_sym 0
5440 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
5441 #define elf_backend_rela_normal 1
5442 #define elf_backend_plt_alignment 4
5443 #define elf_backend_caches_rawsize 1
5444 #define elf_backend_dtrel_excludes_plt 1
5445 #define elf_backend_want_dynrelro 1
5447 #define elf_info_to_howto elf_x86_64_info_to_howto
5449 #define bfd_elf64_bfd_copy_private_section_data \
5450 elf_x86_64_copy_private_section_data
5451 #define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
5452 #define bfd_elf64_bfd_reloc_name_lookup \
5453 elf_x86_64_reloc_name_lookup
5455 #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
5456 #define elf_backend_always_size_sections elf_x86_64_always_size_sections
5457 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
5458 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
5459 #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
5460 #define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
5461 #define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
5462 #define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
5463 #ifdef CORE_HEADER
5464 #define elf_backend_write_core_note elf_x86_64_write_core_note
5465 #endif
5466 #define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
5467 #define elf_backend_relocate_section elf_x86_64_relocate_section
5468 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5469 #define elf_backend_object_p elf64_x86_64_elf_object_p
5470 #define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
5472 #define elf_backend_section_from_shdr \
5473 elf_x86_64_section_from_shdr
5475 #define elf_backend_section_from_bfd_section \
5476 elf_x86_64_elf_section_from_bfd_section
5477 #define elf_backend_add_symbol_hook \
5478 elf_x86_64_add_symbol_hook
5479 #define elf_backend_symbol_processing \
5480 elf_x86_64_symbol_processing
5481 #define elf_backend_common_section_index \
5482 elf_x86_64_common_section_index
5483 #define elf_backend_common_section \
5484 elf_x86_64_common_section
5485 #define elf_backend_common_definition \
5486 elf_x86_64_common_definition
5487 #define elf_backend_merge_symbol \
5488 elf_x86_64_merge_symbol
5489 #define elf_backend_special_sections \
5490 elf_x86_64_special_sections
5491 #define elf_backend_section_flags elf_x86_64_section_flags
5492 #define elf_backend_fake_sections elf_x86_64_fake_sections
5493 #define elf_backend_additional_program_headers \
5494 elf_x86_64_additional_program_headers
5495 #define elf_backend_setup_gnu_properties \
5496 elf_x86_64_link_setup_gnu_properties
5497 #define elf_backend_hide_symbol \
5498 _bfd_x86_elf_hide_symbol
5500 #undef elf64_bed
5501 #define elf64_bed elf64_x86_64_bed
5503 #include "elf64-target.h"
5505 /* CloudABI support. */
5507 #undef TARGET_LITTLE_SYM
5508 #define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
5509 #undef TARGET_LITTLE_NAME
5510 #define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
5512 #undef ELF_OSABI
5513 #define ELF_OSABI ELFOSABI_CLOUDABI
5515 #undef elf64_bed
5516 #define elf64_bed elf64_x86_64_cloudabi_bed
5518 #include "elf64-target.h"
5520 /* FreeBSD support. */
5522 #undef TARGET_LITTLE_SYM
5523 #define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
5524 #undef TARGET_LITTLE_NAME
5525 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
5527 #undef ELF_OSABI
5528 #define ELF_OSABI ELFOSABI_FREEBSD
5530 #undef elf64_bed
5531 #define elf64_bed elf64_x86_64_fbsd_bed
5533 #include "elf64-target.h"
5535 /* Solaris 2 support. */
5537 #undef TARGET_LITTLE_SYM
5538 #define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
5539 #undef TARGET_LITTLE_NAME
5540 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
5542 #undef ELF_TARGET_OS
5543 #define ELF_TARGET_OS is_solaris
5545 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5546 objects won't be recognized. */
5547 #undef ELF_OSABI
5549 #undef elf64_bed
5550 #define elf64_bed elf64_x86_64_sol2_bed
5552 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
5553 boundary. */
5554 #undef elf_backend_static_tls_alignment
5555 #define elf_backend_static_tls_alignment 16
5557 /* The Solaris 2 ABI requires a plt symbol on all platforms.
5559 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5560 File, p.63. */
5561 #undef elf_backend_want_plt_sym
5562 #define elf_backend_want_plt_sym 1
5564 #undef elf_backend_strtab_flags
5565 #define elf_backend_strtab_flags SHF_STRINGS
5567 static bool
5568 elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
5569 bfd *obfd ATTRIBUTE_UNUSED,
5570 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
5571 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
5573 /* PR 19938: FIXME: Need to add code for setting the sh_info
5574 and sh_link fields of Solaris specific section types. */
5575 return false;
5578 #undef elf_backend_copy_special_section_fields
5579 #define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
5581 #include "elf64-target.h"
5583 /* Restore defaults. */
5584 #undef ELF_OSABI
5585 #undef elf_backend_static_tls_alignment
5586 #undef elf_backend_want_plt_sym
5587 #define elf_backend_want_plt_sym 0
5588 #undef elf_backend_strtab_flags
5589 #undef elf_backend_copy_special_section_fields
5591 /* 32bit x86-64 support. */
5593 #undef TARGET_LITTLE_SYM
5594 #define TARGET_LITTLE_SYM x86_64_elf32_vec
5595 #undef TARGET_LITTLE_NAME
5596 #define TARGET_LITTLE_NAME "elf32-x86-64"
5597 #undef elf32_bed
5598 #define elf32_bed elf32_x86_64_bed
5600 #undef ELF_ARCH
5601 #define ELF_ARCH bfd_arch_i386
5603 #undef ELF_MACHINE_CODE
5604 #define ELF_MACHINE_CODE EM_X86_64
5606 #undef ELF_TARGET_OS
5607 #undef ELF_OSABI
5609 #define bfd_elf32_bfd_copy_private_section_data \
5610 elf_x86_64_copy_private_section_data
5611 #define bfd_elf32_bfd_reloc_type_lookup \
5612 elf_x86_64_reloc_type_lookup
5613 #define bfd_elf32_bfd_reloc_name_lookup \
5614 elf_x86_64_reloc_name_lookup
5615 #define bfd_elf32_get_synthetic_symtab \
5616 elf_x86_64_get_synthetic_symtab
5618 #undef elf_backend_object_p
5619 #define elf_backend_object_p \
5620 elf32_x86_64_elf_object_p
5622 #undef elf_backend_bfd_from_remote_memory
5623 #define elf_backend_bfd_from_remote_memory \
5624 _bfd_elf32_bfd_from_remote_memory
5626 #undef elf_backend_size_info
5627 #define elf_backend_size_info \
5628 _bfd_elf32_size_info
5630 #include "elf32-target.h"