readelf: Support RELR in -S and -d and output
[binutils-gdb.git] / bfd / elf64-x86-64.c
blobdc416a7f7120070f384de0e92a7dd4cff0455ad2
1 /* X86-64 specific support for ELF
2 Copyright (C) 2000-2021 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"
26 #include "opcode/i386.h"
27 #include "elf/x86-64.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, 3, 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, 4, 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, 2, 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, 2, 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, 2, 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, 2, 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, 4, 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, 4, 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, 4, 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, 2, 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, 2, 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, 2, 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, 1, 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, 1, 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, 0, 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, 0, 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, 4, 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, 4, 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, 4, 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, 2, 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, 2, 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, 2, 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, 2, 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, 2, 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, 4, 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, 4, 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, 2, 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, 4, 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, 4, 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, 4, 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, 4, 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, 4, 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, 2, 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, 4, 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, 2, 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, 3, 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, 4, 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, 4, 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, 4, 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, 2, 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, 2, 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, 2, 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, 2, 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, 4, 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, 4, 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, 2, 32, false, 0, complain_overflow_bitfield,
192 bfd_elf_generic_reloc, "R_X86_64_32", false, 0, 0xffffffff,
193 false)
196 #define X86_PCREL_TYPE_P(TYPE) \
197 ( ((TYPE) == R_X86_64_PC8) \
198 || ((TYPE) == R_X86_64_PC16) \
199 || ((TYPE) == R_X86_64_PC32) \
200 || ((TYPE) == R_X86_64_PC32_BND) \
201 || ((TYPE) == R_X86_64_PC64))
203 #define X86_SIZE_TYPE_P(TYPE) \
204 ((TYPE) == R_X86_64_SIZE32 || (TYPE) == R_X86_64_SIZE64)
206 /* Map BFD relocs to the x86_64 elf relocs. */
207 struct elf_reloc_map
209 bfd_reloc_code_real_type bfd_reloc_val;
210 unsigned char elf_reloc_val;
213 static const struct elf_reloc_map x86_64_reloc_map[] =
215 { BFD_RELOC_NONE, R_X86_64_NONE, },
216 { BFD_RELOC_64, R_X86_64_64, },
217 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
218 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
219 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
220 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
221 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
222 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
223 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
224 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
225 { BFD_RELOC_32, R_X86_64_32, },
226 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
227 { BFD_RELOC_16, R_X86_64_16, },
228 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
229 { BFD_RELOC_8, R_X86_64_8, },
230 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
231 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
232 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
233 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
234 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
235 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
236 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
237 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
238 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
239 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
240 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
241 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
242 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
243 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
244 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
245 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
246 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
247 { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
248 { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
249 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
250 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
251 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
252 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
253 { BFD_RELOC_X86_64_PC32_BND, R_X86_64_PC32_BND, },
254 { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND, },
255 { BFD_RELOC_X86_64_GOTPCRELX, R_X86_64_GOTPCRELX, },
256 { BFD_RELOC_X86_64_REX_GOTPCRELX, R_X86_64_REX_GOTPCRELX, },
257 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
258 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
261 static reloc_howto_type *
262 elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
264 unsigned i;
266 if (r_type == (unsigned int) R_X86_64_32)
268 if (ABI_64_P (abfd))
269 i = r_type;
270 else
271 i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
273 else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
274 || r_type >= (unsigned int) R_X86_64_max)
276 if (r_type >= (unsigned int) R_X86_64_standard)
278 /* xgettext:c-format */
279 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
280 abfd, r_type);
281 bfd_set_error (bfd_error_bad_value);
282 return NULL;
284 i = r_type;
286 else
287 i = r_type - (unsigned int) R_X86_64_vt_offset;
288 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
289 return &x86_64_elf_howto_table[i];
292 /* Given a BFD reloc type, return a HOWTO structure. */
293 static reloc_howto_type *
294 elf_x86_64_reloc_type_lookup (bfd *abfd,
295 bfd_reloc_code_real_type code)
297 unsigned int i;
299 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
300 i++)
302 if (x86_64_reloc_map[i].bfd_reloc_val == code)
303 return elf_x86_64_rtype_to_howto (abfd,
304 x86_64_reloc_map[i].elf_reloc_val);
306 return NULL;
309 static reloc_howto_type *
310 elf_x86_64_reloc_name_lookup (bfd *abfd,
311 const char *r_name)
313 unsigned int i;
315 if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
317 /* Get x32 R_X86_64_32. */
318 reloc_howto_type *reloc
319 = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
320 BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
321 return reloc;
324 for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
325 if (x86_64_elf_howto_table[i].name != NULL
326 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
327 return &x86_64_elf_howto_table[i];
329 return NULL;
332 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
334 static bool
335 elf_x86_64_info_to_howto (bfd *abfd, arelent *cache_ptr,
336 Elf_Internal_Rela *dst)
338 unsigned r_type;
340 r_type = ELF32_R_TYPE (dst->r_info);
341 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
342 if (cache_ptr->howto == NULL)
343 return false;
344 BFD_ASSERT (r_type == cache_ptr->howto->type || cache_ptr->howto->type == R_X86_64_NONE);
345 return true;
348 /* Support for core dump NOTE sections. */
349 static bool
350 elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
352 int offset;
353 size_t size;
355 switch (note->descsz)
357 default:
358 return false;
360 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
361 /* pr_cursig */
362 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
364 /* pr_pid */
365 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
367 /* pr_reg */
368 offset = 72;
369 size = 216;
371 break;
373 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
374 /* pr_cursig */
375 elf_tdata (abfd)->core->signal
376 = bfd_get_16 (abfd, note->descdata + 12);
378 /* pr_pid */
379 elf_tdata (abfd)->core->lwpid
380 = bfd_get_32 (abfd, note->descdata + 32);
382 /* pr_reg */
383 offset = 112;
384 size = 216;
386 break;
389 /* Make a ".reg/999" section. */
390 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
391 size, note->descpos + offset);
394 static bool
395 elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
397 switch (note->descsz)
399 default:
400 return false;
402 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
403 elf_tdata (abfd)->core->pid
404 = bfd_get_32 (abfd, note->descdata + 12);
405 elf_tdata (abfd)->core->program
406 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
407 elf_tdata (abfd)->core->command
408 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
409 break;
411 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
412 elf_tdata (abfd)->core->pid
413 = bfd_get_32 (abfd, note->descdata + 24);
414 elf_tdata (abfd)->core->program
415 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
416 elf_tdata (abfd)->core->command
417 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
420 /* Note that for some reason, a spurious space is tacked
421 onto the end of the args in some (at least one anyway)
422 implementations, so strip it off if it exists. */
425 char *command = elf_tdata (abfd)->core->command;
426 int n = strlen (command);
428 if (0 < n && command[n - 1] == ' ')
429 command[n - 1] = '\0';
432 return true;
435 #ifdef CORE_HEADER
436 # if GCC_VERSION >= 8000
437 # pragma GCC diagnostic push
438 # pragma GCC diagnostic ignored "-Wstringop-truncation"
439 # endif
440 static char *
441 elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
442 int note_type, ...)
444 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
445 va_list ap;
446 const char *fname, *psargs;
447 long pid;
448 int cursig;
449 const void *gregs;
451 switch (note_type)
453 default:
454 return NULL;
456 case NT_PRPSINFO:
457 va_start (ap, note_type);
458 fname = va_arg (ap, const char *);
459 psargs = va_arg (ap, const char *);
460 va_end (ap);
462 if (bed->s->elfclass == ELFCLASS32)
464 prpsinfo32_t data;
465 memset (&data, 0, sizeof (data));
466 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
467 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
468 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
469 &data, sizeof (data));
471 else
473 prpsinfo64_t data;
474 memset (&data, 0, sizeof (data));
475 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
476 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
477 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
478 &data, sizeof (data));
480 /* NOTREACHED */
482 case NT_PRSTATUS:
483 va_start (ap, note_type);
484 pid = va_arg (ap, long);
485 cursig = va_arg (ap, int);
486 gregs = va_arg (ap, const void *);
487 va_end (ap);
489 if (bed->s->elfclass == ELFCLASS32)
491 if (bed->elf_machine_code == EM_X86_64)
493 prstatusx32_t prstat;
494 memset (&prstat, 0, sizeof (prstat));
495 prstat.pr_pid = pid;
496 prstat.pr_cursig = cursig;
497 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
498 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
499 &prstat, sizeof (prstat));
501 else
503 prstatus32_t prstat;
504 memset (&prstat, 0, sizeof (prstat));
505 prstat.pr_pid = pid;
506 prstat.pr_cursig = cursig;
507 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
508 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
509 &prstat, sizeof (prstat));
512 else
514 prstatus64_t prstat;
515 memset (&prstat, 0, sizeof (prstat));
516 prstat.pr_pid = pid;
517 prstat.pr_cursig = cursig;
518 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
519 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
520 &prstat, sizeof (prstat));
523 /* NOTREACHED */
525 # if GCC_VERSION >= 8000
526 # pragma GCC diagnostic pop
527 # endif
528 #endif
530 /* Functions for the x86-64 ELF linker. */
532 /* The size in bytes of an entry in the global offset table. */
534 #define GOT_ENTRY_SIZE 8
536 /* The size in bytes of an entry in the lazy procedure linkage table. */
538 #define LAZY_PLT_ENTRY_SIZE 16
540 /* The size in bytes of an entry in the non-lazy procedure linkage
541 table. */
543 #define NON_LAZY_PLT_ENTRY_SIZE 8
545 /* The first entry in a lazy procedure linkage table looks like this.
546 See the SVR4 ABI i386 supplement and the x86-64 ABI to see how this
547 works. */
549 static const bfd_byte elf_x86_64_lazy_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
551 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
552 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
553 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
556 /* Subsequent entries in a lazy procedure linkage table look like this. */
558 static const bfd_byte elf_x86_64_lazy_plt_entry[LAZY_PLT_ENTRY_SIZE] =
560 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
561 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
562 0x68, /* pushq immediate */
563 0, 0, 0, 0, /* replaced with index into relocation table. */
564 0xe9, /* jmp relative */
565 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
568 /* The first entry in a lazy procedure linkage table with BND prefix
569 like this. */
571 static const bfd_byte elf_x86_64_lazy_bnd_plt0_entry[LAZY_PLT_ENTRY_SIZE] =
573 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
574 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
575 0x0f, 0x1f, 0 /* nopl (%rax) */
578 /* Subsequent entries for branches with BND prefx in a lazy procedure
579 linkage table look like this. */
581 static const bfd_byte elf_x86_64_lazy_bnd_plt_entry[LAZY_PLT_ENTRY_SIZE] =
583 0x68, 0, 0, 0, 0, /* pushq immediate */
584 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
585 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
588 /* The first entry in the IBT-enabled lazy procedure linkage table is the
589 the same as the lazy PLT with BND prefix so that bound registers are
590 preserved when control is passed to dynamic linker. Subsequent
591 entries for a IBT-enabled lazy procedure linkage table look like
592 this. */
594 static const bfd_byte elf_x86_64_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
596 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
597 0x68, 0, 0, 0, 0, /* pushq immediate */
598 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
599 0x90 /* nop */
602 /* The first entry in the x32 IBT-enabled lazy procedure linkage table
603 is the same as the normal lazy PLT. Subsequent entries for an
604 x32 IBT-enabled lazy procedure linkage table look like this. */
606 static const bfd_byte elf_x32_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
608 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
609 0x68, 0, 0, 0, 0, /* pushq immediate */
610 0xe9, 0, 0, 0, 0, /* jmpq relative */
611 0x66, 0x90 /* xchg %ax,%ax */
614 /* Entries in the non-lazey procedure linkage table look like this. */
616 static const bfd_byte elf_x86_64_non_lazy_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
618 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
619 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
620 0x66, 0x90 /* xchg %ax,%ax */
623 /* Entries for branches with BND prefix in the non-lazey procedure
624 linkage table look like this. */
626 static const bfd_byte elf_x86_64_non_lazy_bnd_plt_entry[NON_LAZY_PLT_ENTRY_SIZE] =
628 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
629 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
630 0x90 /* nop */
633 /* Entries for branches with IBT-enabled in the non-lazey procedure
634 linkage table look like this. They have the same size as the lazy
635 PLT entry. */
637 static const bfd_byte elf_x86_64_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
639 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
640 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
641 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
642 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopl 0x0(%rax,%rax,1) */
645 /* Entries for branches with IBT-enabled in the x32 non-lazey procedure
646 linkage table look like this. They have the same size as the lazy
647 PLT entry. */
649 static const bfd_byte elf_x32_non_lazy_ibt_plt_entry[LAZY_PLT_ENTRY_SIZE] =
651 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
652 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
653 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
654 0x66, 0x0f, 0x1f, 0x44, 0x00, 0x00 /* nopw 0x0(%rax,%rax,1) */
657 /* The TLSDESC entry in a lazy procedure linkage table. */
658 static const bfd_byte elf_x86_64_tlsdesc_plt_entry[LAZY_PLT_ENTRY_SIZE] =
660 0xf3, 0x0f, 0x1e, 0xfa, /* endbr64 */
661 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
662 0xff, 0x25, 16, 0, 0, 0 /* jmpq *GOT+TDG(%rip) */
665 /* .eh_frame covering the lazy .plt section. */
667 static const bfd_byte elf_x86_64_eh_frame_lazy_plt[] =
669 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
670 0, 0, 0, 0, /* CIE ID */
671 1, /* CIE version */
672 'z', 'R', 0, /* Augmentation string */
673 1, /* Code alignment factor */
674 0x78, /* Data alignment factor */
675 16, /* Return address column */
676 1, /* Augmentation size */
677 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
678 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
679 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
680 DW_CFA_nop, DW_CFA_nop,
682 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
683 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
684 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
685 0, 0, 0, 0, /* .plt size goes here */
686 0, /* Augmentation size */
687 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
688 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
689 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
690 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
691 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
692 11, /* Block length */
693 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
694 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
695 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
696 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
697 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
700 /* .eh_frame covering the lazy BND .plt section. */
702 static const bfd_byte elf_x86_64_eh_frame_lazy_bnd_plt[] =
704 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
705 0, 0, 0, 0, /* CIE ID */
706 1, /* CIE version */
707 'z', 'R', 0, /* Augmentation string */
708 1, /* Code alignment factor */
709 0x78, /* Data alignment factor */
710 16, /* Return address column */
711 1, /* Augmentation size */
712 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
713 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
714 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
715 DW_CFA_nop, DW_CFA_nop,
717 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
718 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
719 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
720 0, 0, 0, 0, /* .plt size goes here */
721 0, /* Augmentation size */
722 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
723 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
724 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
725 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
726 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
727 11, /* Block length */
728 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
729 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
730 DW_OP_lit15, DW_OP_and, DW_OP_lit5, DW_OP_ge,
731 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
732 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
735 /* .eh_frame covering the lazy .plt section with IBT-enabled. */
737 static const bfd_byte elf_x86_64_eh_frame_lazy_ibt_plt[] =
739 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
740 0, 0, 0, 0, /* CIE ID */
741 1, /* CIE version */
742 'z', 'R', 0, /* Augmentation string */
743 1, /* Code alignment factor */
744 0x78, /* Data alignment factor */
745 16, /* Return address column */
746 1, /* Augmentation size */
747 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
748 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
749 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
750 DW_CFA_nop, DW_CFA_nop,
752 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
753 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
754 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
755 0, 0, 0, 0, /* .plt size goes here */
756 0, /* Augmentation size */
757 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
758 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
759 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
760 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
761 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
762 11, /* Block length */
763 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
764 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
765 DW_OP_lit15, DW_OP_and, DW_OP_lit10, DW_OP_ge,
766 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
767 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
770 /* .eh_frame covering the x32 lazy .plt section with IBT-enabled. */
772 static const bfd_byte elf_x32_eh_frame_lazy_ibt_plt[] =
774 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
775 0, 0, 0, 0, /* CIE ID */
776 1, /* CIE version */
777 'z', 'R', 0, /* Augmentation string */
778 1, /* Code alignment factor */
779 0x78, /* Data alignment factor */
780 16, /* Return address column */
781 1, /* Augmentation size */
782 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
783 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
784 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
785 DW_CFA_nop, DW_CFA_nop,
787 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
788 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
789 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
790 0, 0, 0, 0, /* .plt size goes here */
791 0, /* Augmentation size */
792 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
793 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
794 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
795 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
796 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
797 11, /* Block length */
798 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
799 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
800 DW_OP_lit15, DW_OP_and, DW_OP_lit9, DW_OP_ge,
801 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
802 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
805 /* .eh_frame covering the non-lazy .plt section. */
807 static const bfd_byte elf_x86_64_eh_frame_non_lazy_plt[] =
809 #define PLT_GOT_FDE_LENGTH 20
810 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
811 0, 0, 0, 0, /* CIE ID */
812 1, /* CIE version */
813 'z', 'R', 0, /* Augmentation string */
814 1, /* Code alignment factor */
815 0x78, /* Data alignment factor */
816 16, /* Return address column */
817 1, /* Augmentation size */
818 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
819 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
820 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
821 DW_CFA_nop, DW_CFA_nop,
823 PLT_GOT_FDE_LENGTH, 0, 0, 0, /* FDE length */
824 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
825 0, 0, 0, 0, /* the start of non-lazy .plt goes here */
826 0, 0, 0, 0, /* non-lazy .plt size goes here */
827 0, /* Augmentation size */
828 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop,
829 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
832 /* These are the standard parameters. */
833 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_plt =
835 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
836 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
837 elf_x86_64_lazy_plt_entry, /* plt_entry */
838 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
839 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
840 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
841 6, /* plt_tlsdesc_got1_offset */
842 12, /* plt_tlsdesc_got2_offset */
843 10, /* plt_tlsdesc_got1_insn_end */
844 16, /* plt_tlsdesc_got2_insn_end */
845 2, /* plt0_got1_offset */
846 8, /* plt0_got2_offset */
847 12, /* plt0_got2_insn_end */
848 2, /* plt_got_offset */
849 7, /* plt_reloc_offset */
850 12, /* plt_plt_offset */
851 6, /* plt_got_insn_size */
852 LAZY_PLT_ENTRY_SIZE, /* plt_plt_insn_end */
853 6, /* plt_lazy_offset */
854 elf_x86_64_lazy_plt0_entry, /* pic_plt0_entry */
855 elf_x86_64_lazy_plt_entry, /* pic_plt_entry */
856 elf_x86_64_eh_frame_lazy_plt, /* eh_frame_plt */
857 sizeof (elf_x86_64_eh_frame_lazy_plt) /* eh_frame_plt_size */
860 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_plt =
862 elf_x86_64_non_lazy_plt_entry, /* plt_entry */
863 elf_x86_64_non_lazy_plt_entry, /* pic_plt_entry */
864 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
865 2, /* plt_got_offset */
866 6, /* plt_got_insn_size */
867 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
868 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
871 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_bnd_plt =
873 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
874 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
875 elf_x86_64_lazy_bnd_plt_entry, /* plt_entry */
876 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
877 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
878 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
879 6, /* plt_tlsdesc_got1_offset */
880 12, /* plt_tlsdesc_got2_offset */
881 10, /* plt_tlsdesc_got1_insn_end */
882 16, /* plt_tlsdesc_got2_insn_end */
883 2, /* plt0_got1_offset */
884 1+8, /* plt0_got2_offset */
885 1+12, /* plt0_got2_insn_end */
886 1+2, /* plt_got_offset */
887 1, /* plt_reloc_offset */
888 7, /* plt_plt_offset */
889 1+6, /* plt_got_insn_size */
890 11, /* plt_plt_insn_end */
891 0, /* plt_lazy_offset */
892 elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
893 elf_x86_64_lazy_bnd_plt_entry, /* pic_plt_entry */
894 elf_x86_64_eh_frame_lazy_bnd_plt, /* eh_frame_plt */
895 sizeof (elf_x86_64_eh_frame_lazy_bnd_plt) /* eh_frame_plt_size */
898 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_bnd_plt =
900 elf_x86_64_non_lazy_bnd_plt_entry, /* plt_entry */
901 elf_x86_64_non_lazy_bnd_plt_entry, /* pic_plt_entry */
902 NON_LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
903 1+2, /* plt_got_offset */
904 1+6, /* plt_got_insn_size */
905 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
906 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
909 static const struct elf_x86_lazy_plt_layout elf_x86_64_lazy_ibt_plt =
911 elf_x86_64_lazy_bnd_plt0_entry, /* plt0_entry */
912 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
913 elf_x86_64_lazy_ibt_plt_entry, /* plt_entry */
914 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
915 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
916 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
917 6, /* plt_tlsdesc_got1_offset */
918 12, /* plt_tlsdesc_got2_offset */
919 10, /* plt_tlsdesc_got1_insn_end */
920 16, /* plt_tlsdesc_got2_insn_end */
921 2, /* plt0_got1_offset */
922 1+8, /* plt0_got2_offset */
923 1+12, /* plt0_got2_insn_end */
924 4+1+2, /* plt_got_offset */
925 4+1, /* plt_reloc_offset */
926 4+1+6, /* plt_plt_offset */
927 4+1+6, /* plt_got_insn_size */
928 4+1+5+5, /* plt_plt_insn_end */
929 0, /* plt_lazy_offset */
930 elf_x86_64_lazy_bnd_plt0_entry, /* pic_plt0_entry */
931 elf_x86_64_lazy_ibt_plt_entry, /* pic_plt_entry */
932 elf_x86_64_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
933 sizeof (elf_x86_64_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
936 static const struct elf_x86_lazy_plt_layout elf_x32_lazy_ibt_plt =
938 elf_x86_64_lazy_plt0_entry, /* plt0_entry */
939 LAZY_PLT_ENTRY_SIZE, /* plt0_entry_size */
940 elf_x32_lazy_ibt_plt_entry, /* plt_entry */
941 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
942 elf_x86_64_tlsdesc_plt_entry, /* plt_tlsdesc_entry */
943 LAZY_PLT_ENTRY_SIZE, /* plt_tlsdesc_entry_size */
944 6, /* plt_tlsdesc_got1_offset */
945 12, /* plt_tlsdesc_got2_offset */
946 10, /* plt_tlsdesc_got1_insn_end */
947 16, /* plt_tlsdesc_got2_insn_end */
948 2, /* plt0_got1_offset */
949 8, /* plt0_got2_offset */
950 12, /* plt0_got2_insn_end */
951 4+2, /* plt_got_offset */
952 4+1, /* plt_reloc_offset */
953 4+6, /* plt_plt_offset */
954 4+6, /* plt_got_insn_size */
955 4+5+5, /* plt_plt_insn_end */
956 0, /* plt_lazy_offset */
957 elf_x86_64_lazy_plt0_entry, /* pic_plt0_entry */
958 elf_x32_lazy_ibt_plt_entry, /* pic_plt_entry */
959 elf_x32_eh_frame_lazy_ibt_plt, /* eh_frame_plt */
960 sizeof (elf_x32_eh_frame_lazy_ibt_plt) /* eh_frame_plt_size */
963 static const struct elf_x86_non_lazy_plt_layout elf_x86_64_non_lazy_ibt_plt =
965 elf_x86_64_non_lazy_ibt_plt_entry, /* plt_entry */
966 elf_x86_64_non_lazy_ibt_plt_entry, /* pic_plt_entry */
967 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
968 4+1+2, /* plt_got_offset */
969 4+1+6, /* plt_got_insn_size */
970 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
971 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
974 static const struct elf_x86_non_lazy_plt_layout elf_x32_non_lazy_ibt_plt =
976 elf_x32_non_lazy_ibt_plt_entry, /* plt_entry */
977 elf_x32_non_lazy_ibt_plt_entry, /* pic_plt_entry */
978 LAZY_PLT_ENTRY_SIZE, /* plt_entry_size */
979 4+2, /* plt_got_offset */
980 4+6, /* plt_got_insn_size */
981 elf_x86_64_eh_frame_non_lazy_plt, /* eh_frame_plt */
982 sizeof (elf_x86_64_eh_frame_non_lazy_plt) /* eh_frame_plt_size */
986 static bool
987 elf64_x86_64_elf_object_p (bfd *abfd)
989 /* Set the right machine number for an x86-64 elf64 file. */
990 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
991 return true;
994 static bool
995 elf32_x86_64_elf_object_p (bfd *abfd)
997 /* Set the right machine number for an x86-64 elf32 file. */
998 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
999 return true;
1002 /* Return TRUE if the TLS access code sequence support transition
1003 from R_TYPE. */
1005 static bool
1006 elf_x86_64_check_tls_transition (bfd *abfd,
1007 struct bfd_link_info *info,
1008 asection *sec,
1009 bfd_byte *contents,
1010 Elf_Internal_Shdr *symtab_hdr,
1011 struct elf_link_hash_entry **sym_hashes,
1012 unsigned int r_type,
1013 const Elf_Internal_Rela *rel,
1014 const Elf_Internal_Rela *relend)
1016 unsigned int val;
1017 unsigned long r_symndx;
1018 bool largepic = false;
1019 struct elf_link_hash_entry *h;
1020 bfd_vma offset;
1021 struct elf_x86_link_hash_table *htab;
1022 bfd_byte *call;
1023 bool indirect_call;
1025 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1026 offset = rel->r_offset;
1027 switch (r_type)
1029 case R_X86_64_TLSGD:
1030 case R_X86_64_TLSLD:
1031 if ((rel + 1) >= relend)
1032 return false;
1034 if (r_type == R_X86_64_TLSGD)
1036 /* Check transition from GD access model. For 64bit, only
1037 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1038 .word 0x6666; rex64; call __tls_get_addr@PLT
1040 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
1041 .byte 0x66; rex64
1042 call *__tls_get_addr@GOTPCREL(%rip)
1043 which may be converted to
1044 addr32 call __tls_get_addr
1045 can transit to different access model. For 32bit, only
1046 leaq foo@tlsgd(%rip), %rdi
1047 .word 0x6666; rex64; call __tls_get_addr@PLT
1049 leaq foo@tlsgd(%rip), %rdi
1050 .byte 0x66; rex64
1051 call *__tls_get_addr@GOTPCREL(%rip)
1052 which may be converted to
1053 addr32 call __tls_get_addr
1054 can transit to different access model. For largepic,
1055 we also support:
1056 leaq foo@tlsgd(%rip), %rdi
1057 movabsq $__tls_get_addr@pltoff, %rax
1058 addq $r15, %rax
1059 call *%rax
1061 leaq foo@tlsgd(%rip), %rdi
1062 movabsq $__tls_get_addr@pltoff, %rax
1063 addq $rbx, %rax
1064 call *%rax */
1066 static const unsigned char leaq[] = { 0x66, 0x48, 0x8d, 0x3d };
1068 if ((offset + 12) > sec->size)
1069 return false;
1071 call = contents + offset + 4;
1072 if (call[0] != 0x66
1073 || !((call[1] == 0x48
1074 && call[2] == 0xff
1075 && call[3] == 0x15)
1076 || (call[1] == 0x48
1077 && call[2] == 0x67
1078 && call[3] == 0xe8)
1079 || (call[1] == 0x66
1080 && call[2] == 0x48
1081 && call[3] == 0xe8)))
1083 if (!ABI_64_P (abfd)
1084 || (offset + 19) > sec->size
1085 || offset < 3
1086 || memcmp (call - 7, leaq + 1, 3) != 0
1087 || memcmp (call, "\x48\xb8", 2) != 0
1088 || call[11] != 0x01
1089 || call[13] != 0xff
1090 || call[14] != 0xd0
1091 || !((call[10] == 0x48 && call[12] == 0xd8)
1092 || (call[10] == 0x4c && call[12] == 0xf8)))
1093 return false;
1094 largepic = true;
1096 else if (ABI_64_P (abfd))
1098 if (offset < 4
1099 || memcmp (contents + offset - 4, leaq, 4) != 0)
1100 return false;
1102 else
1104 if (offset < 3
1105 || memcmp (contents + offset - 3, leaq + 1, 3) != 0)
1106 return false;
1108 indirect_call = call[2] == 0xff;
1110 else
1112 /* Check transition from LD access model. Only
1113 leaq foo@tlsld(%rip), %rdi;
1114 call __tls_get_addr@PLT
1116 leaq foo@tlsld(%rip), %rdi;
1117 call *__tls_get_addr@GOTPCREL(%rip)
1118 which may be converted to
1119 addr32 call __tls_get_addr
1120 can transit to different access model. For largepic
1121 we also support:
1122 leaq foo@tlsld(%rip), %rdi
1123 movabsq $__tls_get_addr@pltoff, %rax
1124 addq $r15, %rax
1125 call *%rax
1127 leaq foo@tlsld(%rip), %rdi
1128 movabsq $__tls_get_addr@pltoff, %rax
1129 addq $rbx, %rax
1130 call *%rax */
1132 static const unsigned char lea[] = { 0x48, 0x8d, 0x3d };
1134 if (offset < 3 || (offset + 9) > sec->size)
1135 return false;
1137 if (memcmp (contents + offset - 3, lea, 3) != 0)
1138 return false;
1140 call = contents + offset + 4;
1141 if (!(call[0] == 0xe8
1142 || (call[0] == 0xff && call[1] == 0x15)
1143 || (call[0] == 0x67 && call[1] == 0xe8)))
1145 if (!ABI_64_P (abfd)
1146 || (offset + 19) > sec->size
1147 || memcmp (call, "\x48\xb8", 2) != 0
1148 || call[11] != 0x01
1149 || call[13] != 0xff
1150 || call[14] != 0xd0
1151 || !((call[10] == 0x48 && call[12] == 0xd8)
1152 || (call[10] == 0x4c && call[12] == 0xf8)))
1153 return false;
1154 largepic = true;
1156 indirect_call = call[0] == 0xff;
1159 r_symndx = htab->r_sym (rel[1].r_info);
1160 if (r_symndx < symtab_hdr->sh_info)
1161 return false;
1163 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1164 if (h == NULL
1165 || !((struct elf_x86_link_hash_entry *) h)->tls_get_addr)
1166 return false;
1167 else
1169 r_type = (ELF32_R_TYPE (rel[1].r_info)
1170 & ~R_X86_64_converted_reloc_bit);
1171 if (largepic)
1172 return r_type == R_X86_64_PLTOFF64;
1173 else if (indirect_call)
1174 return r_type == R_X86_64_GOTPCRELX;
1175 else
1176 return (r_type == R_X86_64_PC32 || r_type == R_X86_64_PLT32);
1179 case R_X86_64_GOTTPOFF:
1180 /* Check transition from IE access model:
1181 mov foo@gottpoff(%rip), %reg
1182 add foo@gottpoff(%rip), %reg
1185 /* Check REX prefix first. */
1186 if (offset >= 3 && (offset + 4) <= sec->size)
1188 val = bfd_get_8 (abfd, contents + offset - 3);
1189 if (val != 0x48 && val != 0x4c)
1191 /* X32 may have 0x44 REX prefix or no REX prefix. */
1192 if (ABI_64_P (abfd))
1193 return false;
1196 else
1198 /* X32 may not have any REX prefix. */
1199 if (ABI_64_P (abfd))
1200 return false;
1201 if (offset < 2 || (offset + 3) > sec->size)
1202 return false;
1205 val = bfd_get_8 (abfd, contents + offset - 2);
1206 if (val != 0x8b && val != 0x03)
1207 return false;
1209 val = bfd_get_8 (abfd, contents + offset - 1);
1210 return (val & 0xc7) == 5;
1212 case R_X86_64_GOTPC32_TLSDESC:
1213 /* Check transition from GDesc access model:
1214 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
1215 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
1217 Make sure it's a leaq adding rip to a 32-bit offset
1218 into any register, although it's probably almost always
1219 going to be rax. */
1221 if (offset < 3 || (offset + 4) > sec->size)
1222 return false;
1224 val = bfd_get_8 (abfd, contents + offset - 3);
1225 val &= 0xfb;
1226 if (val != 0x48 && (ABI_64_P (abfd) || val != 0x40))
1227 return false;
1229 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1230 return false;
1232 val = bfd_get_8 (abfd, contents + offset - 1);
1233 return (val & 0xc7) == 0x05;
1235 case R_X86_64_TLSDESC_CALL:
1236 /* Check transition from GDesc access model:
1237 call *x@tlsdesc(%rax) <--- LP64 mode.
1238 call *x@tlsdesc(%eax) <--- X32 mode.
1240 if (offset + 2 <= sec->size)
1242 unsigned int prefix;
1243 call = contents + offset;
1244 prefix = 0;
1245 if (!ABI_64_P (abfd))
1247 /* Check for call *x@tlsdesc(%eax). */
1248 if (call[0] == 0x67)
1250 prefix = 1;
1251 if (offset + 3 > sec->size)
1252 return false;
1255 /* Make sure that it's a call *x@tlsdesc(%rax). */
1256 return call[prefix] == 0xff && call[1 + prefix] == 0x10;
1259 return false;
1261 default:
1262 abort ();
1266 /* Return TRUE if the TLS access transition is OK or no transition
1267 will be performed. Update R_TYPE if there is a transition. */
1269 static bool
1270 elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1271 asection *sec, bfd_byte *contents,
1272 Elf_Internal_Shdr *symtab_hdr,
1273 struct elf_link_hash_entry **sym_hashes,
1274 unsigned int *r_type, int tls_type,
1275 const Elf_Internal_Rela *rel,
1276 const Elf_Internal_Rela *relend,
1277 struct elf_link_hash_entry *h,
1278 unsigned long r_symndx,
1279 bool from_relocate_section)
1281 unsigned int from_type = *r_type;
1282 unsigned int to_type = from_type;
1283 bool check = true;
1285 /* Skip TLS transition for functions. */
1286 if (h != NULL
1287 && (h->type == STT_FUNC
1288 || h->type == STT_GNU_IFUNC))
1289 return true;
1291 switch (from_type)
1293 case R_X86_64_TLSGD:
1294 case R_X86_64_GOTPC32_TLSDESC:
1295 case R_X86_64_TLSDESC_CALL:
1296 case R_X86_64_GOTTPOFF:
1297 if (bfd_link_executable (info))
1299 if (h == NULL)
1300 to_type = R_X86_64_TPOFF32;
1301 else
1302 to_type = R_X86_64_GOTTPOFF;
1305 /* When we are called from elf_x86_64_relocate_section, there may
1306 be additional transitions based on TLS_TYPE. */
1307 if (from_relocate_section)
1309 unsigned int new_to_type = to_type;
1311 if (TLS_TRANSITION_IE_TO_LE_P (info, h, tls_type))
1312 new_to_type = R_X86_64_TPOFF32;
1314 if (to_type == R_X86_64_TLSGD
1315 || to_type == R_X86_64_GOTPC32_TLSDESC
1316 || to_type == R_X86_64_TLSDESC_CALL)
1318 if (tls_type == GOT_TLS_IE)
1319 new_to_type = R_X86_64_GOTTPOFF;
1322 /* We checked the transition before when we were called from
1323 elf_x86_64_check_relocs. We only want to check the new
1324 transition which hasn't been checked before. */
1325 check = new_to_type != to_type && from_type == to_type;
1326 to_type = new_to_type;
1329 break;
1331 case R_X86_64_TLSLD:
1332 if (bfd_link_executable (info))
1333 to_type = R_X86_64_TPOFF32;
1334 break;
1336 default:
1337 return true;
1340 /* Return TRUE if there is no transition. */
1341 if (from_type == to_type)
1342 return true;
1344 /* Check if the transition can be performed. */
1345 if (check
1346 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1347 symtab_hdr, sym_hashes,
1348 from_type, rel, relend))
1350 reloc_howto_type *from, *to;
1351 const char *name;
1353 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1354 to = elf_x86_64_rtype_to_howto (abfd, to_type);
1356 if (from == NULL || to == NULL)
1357 return false;
1359 if (h)
1360 name = h->root.root.string;
1361 else
1363 struct elf_x86_link_hash_table *htab;
1365 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1366 if (htab == NULL)
1367 name = "*unknown*";
1368 else
1370 Elf_Internal_Sym *isym;
1372 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1373 abfd, r_symndx);
1374 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1378 _bfd_error_handler
1379 /* xgettext:c-format */
1380 (_("%pB: TLS transition from %s to %s against `%s' at %#" PRIx64
1381 " in section `%pA' failed"),
1382 abfd, from->name, to->name, name, (uint64_t) rel->r_offset, sec);
1383 bfd_set_error (bfd_error_bad_value);
1384 return false;
1387 *r_type = to_type;
1388 return true;
1391 /* Rename some of the generic section flags to better document how they
1392 are used here. */
1393 #define check_relocs_failed sec_flg0
1395 static bool
1396 elf_x86_64_need_pic (struct bfd_link_info *info,
1397 bfd *input_bfd, asection *sec,
1398 struct elf_link_hash_entry *h,
1399 Elf_Internal_Shdr *symtab_hdr,
1400 Elf_Internal_Sym *isym,
1401 reloc_howto_type *howto)
1403 const char *v = "";
1404 const char *und = "";
1405 const char *pic = "";
1406 const char *object;
1408 const char *name;
1409 if (h)
1411 name = h->root.root.string;
1412 switch (ELF_ST_VISIBILITY (h->other))
1414 case STV_HIDDEN:
1415 v = _("hidden symbol ");
1416 break;
1417 case STV_INTERNAL:
1418 v = _("internal symbol ");
1419 break;
1420 case STV_PROTECTED:
1421 v = _("protected symbol ");
1422 break;
1423 default:
1424 if (((struct elf_x86_link_hash_entry *) h)->def_protected)
1425 v = _("protected symbol ");
1426 else
1427 v = _("symbol ");
1428 pic = NULL;
1429 break;
1432 if (!SYMBOL_DEFINED_NON_SHARED_P (h) && !h->def_dynamic)
1433 und = _("undefined ");
1435 else
1437 name = bfd_elf_sym_name (input_bfd, symtab_hdr, isym, NULL);
1438 pic = NULL;
1441 if (bfd_link_dll (info))
1443 object = _("a shared object");
1444 if (!pic)
1445 pic = _("; recompile with -fPIC");
1447 else
1449 if (bfd_link_pie (info))
1450 object = _("a PIE object");
1451 else
1452 object = _("a PDE object");
1453 if (!pic)
1454 pic = _("; recompile with -fPIE");
1457 /* xgettext:c-format */
1458 _bfd_error_handler (_("%pB: relocation %s against %s%s`%s' can "
1459 "not be used when making %s%s"),
1460 input_bfd, howto->name, und, v, name,
1461 object, pic);
1462 bfd_set_error (bfd_error_bad_value);
1463 sec->check_relocs_failed = 1;
1464 return false;
1467 /* With the local symbol, foo, we convert
1468 mov foo@GOTPCREL(%rip), %reg
1470 lea foo(%rip), %reg
1471 and convert
1472 call/jmp *foo@GOTPCREL(%rip)
1474 nop call foo/jmp foo nop
1475 When PIC is false, convert
1476 test %reg, foo@GOTPCREL(%rip)
1478 test $foo, %reg
1479 and convert
1480 binop foo@GOTPCREL(%rip), %reg
1482 binop $foo, %reg
1483 where binop is one of adc, add, and, cmp, or, sbb, sub, xor
1484 instructions. */
1486 static bool
1487 elf_x86_64_convert_load_reloc (bfd *abfd,
1488 bfd_byte *contents,
1489 unsigned int *r_type_p,
1490 Elf_Internal_Rela *irel,
1491 struct elf_link_hash_entry *h,
1492 bool *converted,
1493 struct bfd_link_info *link_info)
1495 struct elf_x86_link_hash_table *htab;
1496 bool is_pic;
1497 bool no_overflow;
1498 bool relocx;
1499 bool to_reloc_pc32;
1500 bool abs_symbol;
1501 bool local_ref;
1502 asection *tsec;
1503 bfd_signed_vma raddend;
1504 unsigned int opcode;
1505 unsigned int modrm;
1506 unsigned int r_type = *r_type_p;
1507 unsigned int r_symndx;
1508 bfd_vma roff = irel->r_offset;
1509 bfd_vma abs_relocation;
1511 if (roff < (r_type == R_X86_64_REX_GOTPCRELX ? 3 : 2))
1512 return true;
1514 raddend = irel->r_addend;
1515 /* Addend for 32-bit PC-relative relocation must be -4. */
1516 if (raddend != -4)
1517 return true;
1519 htab = elf_x86_hash_table (link_info, X86_64_ELF_DATA);
1520 is_pic = bfd_link_pic (link_info);
1522 relocx = (r_type == R_X86_64_GOTPCRELX
1523 || r_type == R_X86_64_REX_GOTPCRELX);
1525 /* TRUE if --no-relax is used. */
1526 no_overflow = link_info->disable_target_specific_optimizations > 1;
1528 r_symndx = htab->r_sym (irel->r_info);
1530 opcode = bfd_get_8 (abfd, contents + roff - 2);
1532 /* Convert mov to lea since it has been done for a while. */
1533 if (opcode != 0x8b)
1535 /* Only convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX
1536 for call, jmp or one of adc, add, and, cmp, or, sbb, sub,
1537 test, xor instructions. */
1538 if (!relocx)
1539 return true;
1542 /* We convert only to R_X86_64_PC32:
1543 1. Branch.
1544 2. R_X86_64_GOTPCREL since we can't modify REX byte.
1545 3. no_overflow is true.
1546 4. PIC.
1548 to_reloc_pc32 = (opcode == 0xff
1549 || !relocx
1550 || no_overflow
1551 || is_pic);
1553 abs_symbol = false;
1554 abs_relocation = 0;
1556 /* Get the symbol referred to by the reloc. */
1557 if (h == NULL)
1559 Elf_Internal_Sym *isym
1560 = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
1562 /* Skip relocation against undefined symbols. */
1563 if (isym->st_shndx == SHN_UNDEF)
1564 return true;
1566 local_ref = true;
1567 if (isym->st_shndx == SHN_ABS)
1569 tsec = bfd_abs_section_ptr;
1570 abs_symbol = true;
1571 abs_relocation = isym->st_value;
1573 else if (isym->st_shndx == SHN_COMMON)
1574 tsec = bfd_com_section_ptr;
1575 else if (isym->st_shndx == SHN_X86_64_LCOMMON)
1576 tsec = &_bfd_elf_large_com_section;
1577 else
1578 tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1580 else
1582 /* Undefined weak symbol is only bound locally in executable
1583 and its reference is resolved as 0 without relocation
1584 overflow. We can only perform this optimization for
1585 GOTPCRELX relocations since we need to modify REX byte.
1586 It is OK convert mov with R_X86_64_GOTPCREL to
1587 R_X86_64_PC32. */
1588 struct elf_x86_link_hash_entry *eh = elf_x86_hash_entry (h);
1590 abs_symbol = ABS_SYMBOL_P (h);
1591 abs_relocation = h->root.u.def.value;
1593 /* NB: Also set linker_def via SYMBOL_REFERENCES_LOCAL_P. */
1594 local_ref = SYMBOL_REFERENCES_LOCAL_P (link_info, h);
1595 if ((relocx || opcode == 0x8b)
1596 && (h->root.type == bfd_link_hash_undefweak
1597 && !eh->linker_def
1598 && local_ref))
1600 if (opcode == 0xff)
1602 /* Skip for branch instructions since R_X86_64_PC32
1603 may overflow. */
1604 if (no_overflow)
1605 return true;
1607 else if (relocx)
1609 /* For non-branch instructions, we can convert to
1610 R_X86_64_32/R_X86_64_32S since we know if there
1611 is a REX byte. */
1612 to_reloc_pc32 = false;
1615 /* Since we don't know the current PC when PIC is true,
1616 we can't convert to R_X86_64_PC32. */
1617 if (to_reloc_pc32 && is_pic)
1618 return true;
1620 goto convert;
1622 /* Avoid optimizing GOTPCREL relocations againt _DYNAMIC since
1623 ld.so may use its link-time address. */
1624 else if (h->start_stop
1625 || eh->linker_def
1626 || ((h->def_regular
1627 || h->root.type == bfd_link_hash_defined
1628 || h->root.type == bfd_link_hash_defweak)
1629 && h != htab->elf.hdynamic
1630 && local_ref))
1632 /* bfd_link_hash_new or bfd_link_hash_undefined is
1633 set by an assignment in a linker script in
1634 bfd_elf_record_link_assignment. start_stop is set
1635 on __start_SECNAME/__stop_SECNAME which mark section
1636 SECNAME. */
1637 if (h->start_stop
1638 || eh->linker_def
1639 || (h->def_regular
1640 && (h->root.type == bfd_link_hash_new
1641 || h->root.type == bfd_link_hash_undefined
1642 || ((h->root.type == bfd_link_hash_defined
1643 || h->root.type == bfd_link_hash_defweak)
1644 && h->root.u.def.section == bfd_und_section_ptr))))
1646 /* Skip since R_X86_64_32/R_X86_64_32S may overflow. */
1647 if (no_overflow)
1648 return true;
1649 goto convert;
1651 tsec = h->root.u.def.section;
1653 else
1654 return true;
1657 /* Don't convert GOTPCREL relocation against large section. */
1658 if (elf_section_data (tsec) != NULL
1659 && (elf_section_flags (tsec) & SHF_X86_64_LARGE) != 0)
1660 return true;
1662 /* Skip since R_X86_64_PC32/R_X86_64_32/R_X86_64_32S may overflow. */
1663 if (no_overflow)
1664 return true;
1666 convert:
1667 if (opcode == 0xff)
1669 /* We have "call/jmp *foo@GOTPCREL(%rip)". */
1670 unsigned int nop;
1671 unsigned int disp;
1672 bfd_vma nop_offset;
1674 /* Convert R_X86_64_GOTPCRELX and R_X86_64_REX_GOTPCRELX to
1675 R_X86_64_PC32. */
1676 modrm = bfd_get_8 (abfd, contents + roff - 1);
1677 if (modrm == 0x25)
1679 /* Convert to "jmp foo nop". */
1680 modrm = 0xe9;
1681 nop = NOP_OPCODE;
1682 nop_offset = irel->r_offset + 3;
1683 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1684 irel->r_offset -= 1;
1685 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1687 else
1689 struct elf_x86_link_hash_entry *eh
1690 = (struct elf_x86_link_hash_entry *) h;
1692 /* Convert to "nop call foo". ADDR_PREFIX_OPCODE
1693 is a nop prefix. */
1694 modrm = 0xe8;
1695 /* To support TLS optimization, always use addr32 prefix for
1696 "call *__tls_get_addr@GOTPCREL(%rip)". */
1697 if (eh && eh->tls_get_addr)
1699 nop = 0x67;
1700 nop_offset = irel->r_offset - 2;
1702 else
1704 nop = htab->params->call_nop_byte;
1705 if (htab->params->call_nop_as_suffix)
1707 nop_offset = irel->r_offset + 3;
1708 disp = bfd_get_32 (abfd, contents + irel->r_offset);
1709 irel->r_offset -= 1;
1710 bfd_put_32 (abfd, disp, contents + irel->r_offset);
1712 else
1713 nop_offset = irel->r_offset - 2;
1716 bfd_put_8 (abfd, nop, contents + nop_offset);
1717 bfd_put_8 (abfd, modrm, contents + irel->r_offset - 1);
1718 r_type = R_X86_64_PC32;
1720 else
1722 unsigned int rex;
1723 unsigned int rex_mask = REX_R;
1725 if (r_type == R_X86_64_REX_GOTPCRELX)
1726 rex = bfd_get_8 (abfd, contents + roff - 3);
1727 else
1728 rex = 0;
1730 if (opcode == 0x8b)
1732 if (abs_symbol && local_ref && relocx)
1733 to_reloc_pc32 = false;
1735 if (to_reloc_pc32)
1737 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1738 "lea foo(%rip), %reg". */
1739 opcode = 0x8d;
1740 r_type = R_X86_64_PC32;
1742 else
1744 /* Convert "mov foo@GOTPCREL(%rip), %reg" to
1745 "mov $foo, %reg". */
1746 opcode = 0xc7;
1747 modrm = bfd_get_8 (abfd, contents + roff - 1);
1748 modrm = 0xc0 | (modrm & 0x38) >> 3;
1749 if ((rex & REX_W) != 0
1750 && ABI_64_P (link_info->output_bfd))
1752 /* Keep the REX_W bit in REX byte for LP64. */
1753 r_type = R_X86_64_32S;
1754 goto rewrite_modrm_rex;
1756 else
1758 /* If the REX_W bit in REX byte isn't needed,
1759 use R_X86_64_32 and clear the W bit to avoid
1760 sign-extend imm32 to imm64. */
1761 r_type = R_X86_64_32;
1762 /* Clear the W bit in REX byte. */
1763 rex_mask |= REX_W;
1764 goto rewrite_modrm_rex;
1768 else
1770 /* R_X86_64_PC32 isn't supported. */
1771 if (to_reloc_pc32)
1772 return true;
1774 modrm = bfd_get_8 (abfd, contents + roff - 1);
1775 if (opcode == 0x85)
1777 /* Convert "test %reg, foo@GOTPCREL(%rip)" to
1778 "test $foo, %reg". */
1779 modrm = 0xc0 | (modrm & 0x38) >> 3;
1780 opcode = 0xf7;
1782 else
1784 /* Convert "binop foo@GOTPCREL(%rip), %reg" to
1785 "binop $foo, %reg". */
1786 modrm = 0xc0 | (modrm & 0x38) >> 3 | (opcode & 0x3c);
1787 opcode = 0x81;
1790 /* Use R_X86_64_32 with 32-bit operand to avoid relocation
1791 overflow when sign-extending imm32 to imm64. */
1792 r_type = (rex & REX_W) != 0 ? R_X86_64_32S : R_X86_64_32;
1794 rewrite_modrm_rex:
1795 if (abs_relocation)
1797 /* Check if R_X86_64_32S/R_X86_64_32 fits. */
1798 if (r_type == R_X86_64_32S)
1800 if ((abs_relocation + 0x80000000) > 0xffffffff)
1801 return true;
1803 else
1805 if (abs_relocation > 0xffffffff)
1806 return true;
1810 bfd_put_8 (abfd, modrm, contents + roff - 1);
1812 if (rex)
1814 /* Move the R bit to the B bit in REX byte. */
1815 rex = (rex & ~rex_mask) | (rex & REX_R) >> 2;
1816 bfd_put_8 (abfd, rex, contents + roff - 3);
1819 /* No addend for R_X86_64_32/R_X86_64_32S relocations. */
1820 irel->r_addend = 0;
1823 bfd_put_8 (abfd, opcode, contents + roff - 2);
1826 *r_type_p = r_type;
1827 irel->r_info = htab->r_info (r_symndx,
1828 r_type | R_X86_64_converted_reloc_bit);
1830 *converted = true;
1832 return true;
1835 /* Look through the relocs for a section during the first phase, and
1836 calculate needed space in the global offset table, procedure
1837 linkage table, and dynamic reloc sections. */
1839 static bool
1840 elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1841 asection *sec,
1842 const Elf_Internal_Rela *relocs)
1844 struct elf_x86_link_hash_table *htab;
1845 Elf_Internal_Shdr *symtab_hdr;
1846 struct elf_link_hash_entry **sym_hashes;
1847 const Elf_Internal_Rela *rel;
1848 const Elf_Internal_Rela *rel_end;
1849 asection *sreloc;
1850 bfd_byte *contents;
1851 bool converted;
1853 if (bfd_link_relocatable (info))
1854 return true;
1856 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
1857 if (htab == NULL)
1859 sec->check_relocs_failed = 1;
1860 return false;
1863 BFD_ASSERT (is_x86_elf (abfd, htab));
1865 /* Get the section contents. */
1866 if (elf_section_data (sec)->this_hdr.contents != NULL)
1867 contents = elf_section_data (sec)->this_hdr.contents;
1868 else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1870 sec->check_relocs_failed = 1;
1871 return false;
1874 symtab_hdr = &elf_symtab_hdr (abfd);
1875 sym_hashes = elf_sym_hashes (abfd);
1877 converted = false;
1879 sreloc = NULL;
1881 rel_end = relocs + sec->reloc_count;
1882 for (rel = relocs; rel < rel_end; rel++)
1884 unsigned int r_type;
1885 unsigned int r_symndx;
1886 struct elf_link_hash_entry *h;
1887 struct elf_x86_link_hash_entry *eh;
1888 Elf_Internal_Sym *isym;
1889 const char *name;
1890 bool size_reloc;
1891 bool converted_reloc;
1892 bool no_dynreloc;
1894 r_symndx = htab->r_sym (rel->r_info);
1895 r_type = ELF32_R_TYPE (rel->r_info);
1897 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1899 /* xgettext:c-format */
1900 _bfd_error_handler (_("%pB: bad symbol index: %d"),
1901 abfd, r_symndx);
1902 goto error_return;
1905 if (r_symndx < symtab_hdr->sh_info)
1907 /* A local symbol. */
1908 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
1909 abfd, r_symndx);
1910 if (isym == NULL)
1911 goto error_return;
1913 /* Check relocation against local STT_GNU_IFUNC symbol. */
1914 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1916 h = _bfd_elf_x86_get_local_sym_hash (htab, abfd, rel,
1917 true);
1918 if (h == NULL)
1919 goto error_return;
1921 /* Fake a STT_GNU_IFUNC symbol. */
1922 h->root.root.string = bfd_elf_sym_name (abfd, symtab_hdr,
1923 isym, NULL);
1924 h->type = STT_GNU_IFUNC;
1925 h->def_regular = 1;
1926 h->ref_regular = 1;
1927 h->forced_local = 1;
1928 h->root.type = bfd_link_hash_defined;
1930 else
1931 h = NULL;
1933 else
1935 isym = NULL;
1936 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1937 while (h->root.type == bfd_link_hash_indirect
1938 || h->root.type == bfd_link_hash_warning)
1939 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1942 /* Check invalid x32 relocations. */
1943 if (!ABI_64_P (abfd))
1944 switch (r_type)
1946 default:
1947 break;
1949 case R_X86_64_DTPOFF64:
1950 case R_X86_64_TPOFF64:
1951 case R_X86_64_PC64:
1952 case R_X86_64_GOTOFF64:
1953 case R_X86_64_GOT64:
1954 case R_X86_64_GOTPCREL64:
1955 case R_X86_64_GOTPC64:
1956 case R_X86_64_GOTPLT64:
1957 case R_X86_64_PLTOFF64:
1959 if (h)
1960 name = h->root.root.string;
1961 else
1962 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1963 NULL);
1964 _bfd_error_handler
1965 /* xgettext:c-format */
1966 (_("%pB: relocation %s against symbol `%s' isn't "
1967 "supported in x32 mode"), abfd,
1968 x86_64_elf_howto_table[r_type].name, name);
1969 bfd_set_error (bfd_error_bad_value);
1970 goto error_return;
1972 break;
1975 eh = (struct elf_x86_link_hash_entry *) h;
1977 if (h != NULL)
1979 /* It is referenced by a non-shared object. */
1980 h->ref_regular = 1;
1983 converted_reloc = false;
1984 if ((r_type == R_X86_64_GOTPCREL
1985 || r_type == R_X86_64_GOTPCRELX
1986 || r_type == R_X86_64_REX_GOTPCRELX)
1987 && (h == NULL || h->type != STT_GNU_IFUNC))
1989 Elf_Internal_Rela *irel = (Elf_Internal_Rela *) rel;
1990 if (!elf_x86_64_convert_load_reloc (abfd, contents, &r_type,
1991 irel, h, &converted_reloc,
1992 info))
1993 goto error_return;
1995 if (converted_reloc)
1996 converted = true;
1999 if (!_bfd_elf_x86_valid_reloc_p (sec, info, htab, rel, h, isym,
2000 symtab_hdr, &no_dynreloc))
2001 return false;
2003 if (! elf_x86_64_tls_transition (info, abfd, sec, contents,
2004 symtab_hdr, sym_hashes,
2005 &r_type, GOT_UNKNOWN,
2006 rel, rel_end, h, r_symndx, false))
2007 goto error_return;
2009 /* Check if _GLOBAL_OFFSET_TABLE_ is referenced. */
2010 if (h == htab->elf.hgot)
2011 htab->got_referenced = true;
2013 switch (r_type)
2015 case R_X86_64_TLSLD:
2016 htab->tls_ld_or_ldm_got.refcount = 1;
2017 goto create_got;
2019 case R_X86_64_TPOFF32:
2020 if (!bfd_link_executable (info) && ABI_64_P (abfd))
2021 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2022 &x86_64_elf_howto_table[r_type]);
2023 if (eh != NULL)
2024 eh->zero_undefweak &= 0x2;
2025 break;
2027 case R_X86_64_GOTTPOFF:
2028 if (!bfd_link_executable (info))
2029 info->flags |= DF_STATIC_TLS;
2030 /* Fall through */
2032 case R_X86_64_GOT32:
2033 case R_X86_64_GOTPCREL:
2034 case R_X86_64_GOTPCRELX:
2035 case R_X86_64_REX_GOTPCRELX:
2036 case R_X86_64_TLSGD:
2037 case R_X86_64_GOT64:
2038 case R_X86_64_GOTPCREL64:
2039 case R_X86_64_GOTPLT64:
2040 case R_X86_64_GOTPC32_TLSDESC:
2041 case R_X86_64_TLSDESC_CALL:
2042 /* This symbol requires a global offset table entry. */
2044 int tls_type, old_tls_type;
2046 switch (r_type)
2048 default:
2049 tls_type = GOT_NORMAL;
2050 if (h)
2052 if (ABS_SYMBOL_P (h))
2053 tls_type = GOT_ABS;
2055 else if (isym->st_shndx == SHN_ABS)
2056 tls_type = GOT_ABS;
2057 break;
2058 case R_X86_64_TLSGD:
2059 tls_type = GOT_TLS_GD;
2060 break;
2061 case R_X86_64_GOTTPOFF:
2062 tls_type = GOT_TLS_IE;
2063 break;
2064 case R_X86_64_GOTPC32_TLSDESC:
2065 case R_X86_64_TLSDESC_CALL:
2066 tls_type = GOT_TLS_GDESC;
2067 break;
2070 if (h != NULL)
2072 h->got.refcount = 1;
2073 old_tls_type = eh->tls_type;
2075 else
2077 bfd_signed_vma *local_got_refcounts;
2079 /* This is a global offset table entry for a local symbol. */
2080 local_got_refcounts = elf_local_got_refcounts (abfd);
2081 if (local_got_refcounts == NULL)
2083 bfd_size_type size;
2085 size = symtab_hdr->sh_info;
2086 size *= sizeof (bfd_signed_vma)
2087 + sizeof (bfd_vma) + sizeof (char);
2088 local_got_refcounts = ((bfd_signed_vma *)
2089 bfd_zalloc (abfd, size));
2090 if (local_got_refcounts == NULL)
2091 goto error_return;
2092 elf_local_got_refcounts (abfd) = local_got_refcounts;
2093 elf_x86_local_tlsdesc_gotent (abfd)
2094 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
2095 elf_x86_local_got_tls_type (abfd)
2096 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
2098 local_got_refcounts[r_symndx] = 1;
2099 old_tls_type
2100 = elf_x86_local_got_tls_type (abfd) [r_symndx];
2103 /* If a TLS symbol is accessed using IE at least once,
2104 there is no point to use dynamic model for it. */
2105 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
2106 && (! GOT_TLS_GD_ANY_P (old_tls_type)
2107 || tls_type != GOT_TLS_IE))
2109 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
2110 tls_type = old_tls_type;
2111 else if (GOT_TLS_GD_ANY_P (old_tls_type)
2112 && GOT_TLS_GD_ANY_P (tls_type))
2113 tls_type |= old_tls_type;
2114 else
2116 if (h)
2117 name = h->root.root.string;
2118 else
2119 name = bfd_elf_sym_name (abfd, symtab_hdr,
2120 isym, NULL);
2121 _bfd_error_handler
2122 /* xgettext:c-format */
2123 (_("%pB: '%s' accessed both as normal and"
2124 " thread local symbol"),
2125 abfd, name);
2126 bfd_set_error (bfd_error_bad_value);
2127 goto error_return;
2131 if (old_tls_type != tls_type)
2133 if (eh != NULL)
2134 eh->tls_type = tls_type;
2135 else
2136 elf_x86_local_got_tls_type (abfd) [r_symndx] = tls_type;
2139 /* Fall through */
2141 case R_X86_64_GOTOFF64:
2142 case R_X86_64_GOTPC32:
2143 case R_X86_64_GOTPC64:
2144 create_got:
2145 if (eh != NULL)
2146 eh->zero_undefweak &= 0x2;
2147 break;
2149 case R_X86_64_PLT32:
2150 case R_X86_64_PLT32_BND:
2151 /* This symbol requires a procedure linkage table entry. We
2152 actually build the entry in adjust_dynamic_symbol,
2153 because this might be a case of linking PIC code which is
2154 never referenced by a dynamic object, in which case we
2155 don't need to generate a procedure linkage table entry
2156 after all. */
2158 /* If this is a local symbol, we resolve it directly without
2159 creating a procedure linkage table entry. */
2160 if (h == NULL)
2161 continue;
2163 eh->zero_undefweak &= 0x2;
2164 h->needs_plt = 1;
2165 h->plt.refcount = 1;
2166 break;
2168 case R_X86_64_PLTOFF64:
2169 /* This tries to form the 'address' of a function relative
2170 to GOT. For global symbols we need a PLT entry. */
2171 if (h != NULL)
2173 h->needs_plt = 1;
2174 h->plt.refcount = 1;
2176 goto create_got;
2178 case R_X86_64_SIZE32:
2179 case R_X86_64_SIZE64:
2180 size_reloc = true;
2181 goto do_size;
2183 case R_X86_64_32:
2184 if (!ABI_64_P (abfd))
2185 goto pointer;
2186 /* Fall through. */
2187 case R_X86_64_8:
2188 case R_X86_64_16:
2189 case R_X86_64_32S:
2190 /* Check relocation overflow as these relocs may lead to
2191 run-time relocation overflow. Don't error out for
2192 sections we don't care about, such as debug sections or
2193 when relocation overflow check is disabled. */
2194 if (!htab->params->no_reloc_overflow_check
2195 && !converted_reloc
2196 && (bfd_link_pic (info)
2197 || (bfd_link_executable (info)
2198 && h != NULL
2199 && !h->def_regular
2200 && h->def_dynamic
2201 && (sec->flags & SEC_READONLY) == 0)))
2202 return elf_x86_64_need_pic (info, abfd, sec, h, symtab_hdr, isym,
2203 &x86_64_elf_howto_table[r_type]);
2204 /* Fall through. */
2206 case R_X86_64_PC8:
2207 case R_X86_64_PC16:
2208 case R_X86_64_PC32:
2209 case R_X86_64_PC32_BND:
2210 case R_X86_64_PC64:
2211 case R_X86_64_64:
2212 pointer:
2213 if (eh != NULL && (sec->flags & SEC_CODE) != 0)
2214 eh->zero_undefweak |= 0x2;
2215 /* We are called after all symbols have been resolved. Only
2216 relocation against STT_GNU_IFUNC symbol must go through
2217 PLT. */
2218 if (h != NULL
2219 && (bfd_link_executable (info)
2220 || h->type == STT_GNU_IFUNC))
2222 bool func_pointer_ref = false;
2224 if (r_type == R_X86_64_PC32)
2226 /* Since something like ".long foo - ." may be used
2227 as pointer, make sure that PLT is used if foo is
2228 a function defined in a shared library. */
2229 if ((sec->flags & SEC_CODE) == 0)
2231 h->pointer_equality_needed = 1;
2232 if (bfd_link_pie (info)
2233 && h->type == STT_FUNC
2234 && !h->def_regular
2235 && h->def_dynamic)
2237 h->needs_plt = 1;
2238 h->plt.refcount = 1;
2242 else if (r_type != R_X86_64_PC32_BND
2243 && r_type != R_X86_64_PC64)
2245 h->pointer_equality_needed = 1;
2246 /* At run-time, R_X86_64_64 can be resolved for both
2247 x86-64 and x32. But R_X86_64_32 and R_X86_64_32S
2248 can only be resolved for x32. */
2249 if ((sec->flags & SEC_READONLY) == 0
2250 && (r_type == R_X86_64_64
2251 || (!ABI_64_P (abfd)
2252 && (r_type == R_X86_64_32
2253 || r_type == R_X86_64_32S))))
2254 func_pointer_ref = true;
2257 if (!func_pointer_ref)
2259 /* If this reloc is in a read-only section, we might
2260 need a copy reloc. We can't check reliably at this
2261 stage whether the section is read-only, as input
2262 sections have not yet been mapped to output sections.
2263 Tentatively set the flag for now, and correct in
2264 adjust_dynamic_symbol. */
2265 h->non_got_ref = 1;
2267 if (!elf_has_indirect_extern_access (sec->owner))
2268 eh->non_got_ref_without_indirect_extern_access = 1;
2270 /* We may need a .plt entry if the symbol is a function
2271 defined in a shared lib or is a function referenced
2272 from the code or read-only section. */
2273 if (!h->def_regular
2274 || (sec->flags & (SEC_CODE | SEC_READONLY)) != 0)
2275 h->plt.refcount = 1;
2279 size_reloc = false;
2280 do_size:
2281 if (!no_dynreloc
2282 && NEED_DYNAMIC_RELOCATION_P (info, true, h, sec, r_type,
2283 htab->pointer_r_type))
2285 struct elf_dyn_relocs *p;
2286 struct elf_dyn_relocs **head;
2288 /* We must copy these reloc types into the output file.
2289 Create a reloc section in dynobj and make room for
2290 this reloc. */
2291 if (sreloc == NULL)
2293 sreloc = _bfd_elf_make_dynamic_reloc_section
2294 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
2295 abfd, /*rela?*/ true);
2297 if (sreloc == NULL)
2298 goto error_return;
2301 /* If this is a global symbol, we count the number of
2302 relocations we need for this symbol. */
2303 if (h != NULL)
2304 head = &h->dyn_relocs;
2305 else
2307 /* Track dynamic relocs needed for local syms too.
2308 We really need local syms available to do this
2309 easily. Oh well. */
2310 asection *s;
2311 void **vpp;
2313 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
2314 abfd, r_symndx);
2315 if (isym == NULL)
2316 goto error_return;
2318 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
2319 if (s == NULL)
2320 s = sec;
2322 /* Beware of type punned pointers vs strict aliasing
2323 rules. */
2324 vpp = &(elf_section_data (s)->local_dynrel);
2325 head = (struct elf_dyn_relocs **)vpp;
2328 p = *head;
2329 if (p == NULL || p->sec != sec)
2331 size_t amt = sizeof *p;
2333 p = ((struct elf_dyn_relocs *)
2334 bfd_alloc (htab->elf.dynobj, amt));
2335 if (p == NULL)
2336 goto error_return;
2337 p->next = *head;
2338 *head = p;
2339 p->sec = sec;
2340 p->count = 0;
2341 p->pc_count = 0;
2344 p->count += 1;
2345 /* Count size relocation as PC-relative relocation. */
2346 if (X86_PCREL_TYPE_P (r_type) || size_reloc)
2347 p->pc_count += 1;
2349 break;
2351 /* This relocation describes the C++ object vtable hierarchy.
2352 Reconstruct it for later use during GC. */
2353 case R_X86_64_GNU_VTINHERIT:
2354 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
2355 goto error_return;
2356 break;
2358 /* This relocation describes which C++ vtable entries are actually
2359 used. Record for later use during GC. */
2360 case R_X86_64_GNU_VTENTRY:
2361 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
2362 goto error_return;
2363 break;
2365 default:
2366 break;
2370 if (elf_section_data (sec)->this_hdr.contents != contents)
2372 if (!converted && !_bfd_link_keep_memory (info))
2373 free (contents);
2374 else
2376 /* Cache the section contents for elf_link_input_bfd if any
2377 load is converted or --no-keep-memory isn't used. */
2378 elf_section_data (sec)->this_hdr.contents = contents;
2379 info->cache_size += sec->size;
2383 /* Cache relocations if any load is converted. */
2384 if (elf_section_data (sec)->relocs != relocs && converted)
2385 elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
2387 return true;
2389 error_return:
2390 if (elf_section_data (sec)->this_hdr.contents != contents)
2391 free (contents);
2392 sec->check_relocs_failed = 1;
2393 return false;
2396 /* Return the relocation value for @tpoff relocation
2397 if STT_TLS virtual address is ADDRESS. */
2399 static bfd_vma
2400 elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2402 struct elf_link_hash_table *htab = elf_hash_table (info);
2403 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2404 bfd_vma static_tls_size;
2406 /* If tls_segment is NULL, we should have signalled an error already. */
2407 if (htab->tls_sec == NULL)
2408 return 0;
2410 /* Consider special static TLS alignment requirements. */
2411 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2412 return address - static_tls_size - htab->tls_sec->vma;
2415 /* Relocate an x86_64 ELF section. */
2417 static int
2418 elf_x86_64_relocate_section (bfd *output_bfd,
2419 struct bfd_link_info *info,
2420 bfd *input_bfd,
2421 asection *input_section,
2422 bfd_byte *contents,
2423 Elf_Internal_Rela *relocs,
2424 Elf_Internal_Sym *local_syms,
2425 asection **local_sections)
2427 struct elf_x86_link_hash_table *htab;
2428 Elf_Internal_Shdr *symtab_hdr;
2429 struct elf_link_hash_entry **sym_hashes;
2430 bfd_vma *local_got_offsets;
2431 bfd_vma *local_tlsdesc_gotents;
2432 Elf_Internal_Rela *rel;
2433 Elf_Internal_Rela *wrel;
2434 Elf_Internal_Rela *relend;
2435 unsigned int plt_entry_size;
2436 bool status;
2438 /* Skip if check_relocs failed. */
2439 if (input_section->check_relocs_failed)
2440 return false;
2442 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
2443 if (htab == NULL)
2444 return false;
2446 if (!is_x86_elf (input_bfd, htab))
2448 bfd_set_error (bfd_error_wrong_format);
2449 return false;
2452 plt_entry_size = htab->plt.plt_entry_size;
2453 symtab_hdr = &elf_symtab_hdr (input_bfd);
2454 sym_hashes = elf_sym_hashes (input_bfd);
2455 local_got_offsets = elf_local_got_offsets (input_bfd);
2456 local_tlsdesc_gotents = elf_x86_local_tlsdesc_gotent (input_bfd);
2458 _bfd_x86_elf_set_tls_module_base (info);
2460 status = true;
2461 rel = wrel = relocs;
2462 relend = relocs + input_section->reloc_count;
2463 for (; rel < relend; wrel++, rel++)
2465 unsigned int r_type, r_type_tls;
2466 reloc_howto_type *howto;
2467 unsigned long r_symndx;
2468 struct elf_link_hash_entry *h;
2469 struct elf_x86_link_hash_entry *eh;
2470 Elf_Internal_Sym *sym;
2471 asection *sec;
2472 bfd_vma off, offplt, plt_offset;
2473 bfd_vma relocation;
2474 bool unresolved_reloc;
2475 bfd_reloc_status_type r;
2476 int tls_type;
2477 asection *base_got, *resolved_plt;
2478 bfd_vma st_size;
2479 bool resolved_to_zero;
2480 bool relative_reloc;
2481 bool converted_reloc;
2482 bool need_copy_reloc_in_pie;
2483 bool no_copyreloc_p;
2485 r_type = ELF32_R_TYPE (rel->r_info);
2486 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2487 || r_type == (int) R_X86_64_GNU_VTENTRY)
2489 if (wrel != rel)
2490 *wrel = *rel;
2491 continue;
2494 r_symndx = htab->r_sym (rel->r_info);
2495 converted_reloc = (r_type & R_X86_64_converted_reloc_bit) != 0;
2496 if (converted_reloc)
2498 r_type &= ~R_X86_64_converted_reloc_bit;
2499 rel->r_info = htab->r_info (r_symndx, r_type);
2502 howto = elf_x86_64_rtype_to_howto (input_bfd, r_type);
2503 if (howto == NULL)
2504 return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2506 h = NULL;
2507 sym = NULL;
2508 sec = NULL;
2509 unresolved_reloc = false;
2510 if (r_symndx < symtab_hdr->sh_info)
2512 sym = local_syms + r_symndx;
2513 sec = local_sections[r_symndx];
2515 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2516 &sec, rel);
2517 st_size = sym->st_size;
2519 /* Relocate against local STT_GNU_IFUNC symbol. */
2520 if (!bfd_link_relocatable (info)
2521 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2523 h = _bfd_elf_x86_get_local_sym_hash (htab, input_bfd,
2524 rel, false);
2525 if (h == NULL)
2526 abort ();
2528 /* Set STT_GNU_IFUNC symbol value. */
2529 h->root.u.def.value = sym->st_value;
2530 h->root.u.def.section = sec;
2533 else
2535 bool warned ATTRIBUTE_UNUSED;
2536 bool ignored ATTRIBUTE_UNUSED;
2538 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2539 r_symndx, symtab_hdr, sym_hashes,
2540 h, sec, relocation,
2541 unresolved_reloc, warned, ignored);
2542 st_size = h->size;
2545 if (sec != NULL && discarded_section (sec))
2547 _bfd_clear_contents (howto, input_bfd, input_section,
2548 contents, rel->r_offset);
2549 wrel->r_offset = rel->r_offset;
2550 wrel->r_info = 0;
2551 wrel->r_addend = 0;
2553 /* For ld -r, remove relocations in debug sections against
2554 sections defined in discarded sections. Not done for
2555 eh_frame editing code expects to be present. */
2556 if (bfd_link_relocatable (info)
2557 && (input_section->flags & SEC_DEBUGGING))
2558 wrel--;
2560 continue;
2563 if (bfd_link_relocatable (info))
2565 if (wrel != rel)
2566 *wrel = *rel;
2567 continue;
2570 if (rel->r_addend == 0 && !ABI_64_P (output_bfd))
2572 if (r_type == R_X86_64_64)
2574 /* For x32, treat R_X86_64_64 like R_X86_64_32 and
2575 zero-extend it to 64bit if addend is zero. */
2576 r_type = R_X86_64_32;
2577 memset (contents + rel->r_offset + 4, 0, 4);
2579 else if (r_type == R_X86_64_SIZE64)
2581 /* For x32, treat R_X86_64_SIZE64 like R_X86_64_SIZE32 and
2582 zero-extend it to 64bit if addend is zero. */
2583 r_type = R_X86_64_SIZE32;
2584 memset (contents + rel->r_offset + 4, 0, 4);
2588 eh = (struct elf_x86_link_hash_entry *) h;
2590 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2591 it here if it is defined in a non-shared object. */
2592 if (h != NULL
2593 && h->type == STT_GNU_IFUNC
2594 && h->def_regular)
2596 bfd_vma plt_index;
2597 const char *name;
2599 if ((input_section->flags & SEC_ALLOC) == 0)
2601 /* If this is a SHT_NOTE section without SHF_ALLOC, treat
2602 STT_GNU_IFUNC symbol as STT_FUNC. */
2603 if (elf_section_type (input_section) == SHT_NOTE)
2604 goto skip_ifunc;
2605 /* Dynamic relocs are not propagated for SEC_DEBUGGING
2606 sections because such sections are not SEC_ALLOC and
2607 thus ld.so will not process them. */
2608 if ((input_section->flags & SEC_DEBUGGING) != 0)
2609 continue;
2610 abort ();
2613 switch (r_type)
2615 default:
2616 break;
2618 case R_X86_64_GOTPCREL:
2619 case R_X86_64_GOTPCRELX:
2620 case R_X86_64_REX_GOTPCRELX:
2621 case R_X86_64_GOTPCREL64:
2622 base_got = htab->elf.sgot;
2623 off = h->got.offset;
2625 if (base_got == NULL)
2626 abort ();
2628 if (off == (bfd_vma) -1)
2630 /* We can't use h->got.offset here to save state, or
2631 even just remember the offset, as finish_dynamic_symbol
2632 would use that as offset into .got. */
2634 if (h->plt.offset == (bfd_vma) -1)
2635 abort ();
2637 if (htab->elf.splt != NULL)
2639 plt_index = (h->plt.offset / plt_entry_size
2640 - htab->plt.has_plt0);
2641 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2642 base_got = htab->elf.sgotplt;
2644 else
2646 plt_index = h->plt.offset / plt_entry_size;
2647 off = plt_index * GOT_ENTRY_SIZE;
2648 base_got = htab->elf.igotplt;
2651 if (h->dynindx == -1
2652 || h->forced_local
2653 || info->symbolic)
2655 /* This references the local defitionion. We must
2656 initialize this entry in the global offset table.
2657 Since the offset must always be a multiple of 8,
2658 we use the least significant bit to record
2659 whether we have initialized it already.
2661 When doing a dynamic link, we create a .rela.got
2662 relocation entry to initialize the value. This
2663 is done in the finish_dynamic_symbol routine. */
2664 if ((off & 1) != 0)
2665 off &= ~1;
2666 else
2668 bfd_put_64 (output_bfd, relocation,
2669 base_got->contents + off);
2670 /* Note that this is harmless for the GOTPLT64
2671 case, as -1 | 1 still is -1. */
2672 h->got.offset |= 1;
2677 relocation = (base_got->output_section->vma
2678 + base_got->output_offset + off);
2680 goto do_relocation;
2683 if (h->plt.offset == (bfd_vma) -1)
2685 /* Handle static pointers of STT_GNU_IFUNC symbols. */
2686 if (r_type == htab->pointer_r_type
2687 && (input_section->flags & SEC_CODE) == 0)
2688 goto do_ifunc_pointer;
2689 goto bad_ifunc_reloc;
2692 /* STT_GNU_IFUNC symbol must go through PLT. */
2693 if (htab->elf.splt != NULL)
2695 if (htab->plt_second != NULL)
2697 resolved_plt = htab->plt_second;
2698 plt_offset = eh->plt_second.offset;
2700 else
2702 resolved_plt = htab->elf.splt;
2703 plt_offset = h->plt.offset;
2706 else
2708 resolved_plt = htab->elf.iplt;
2709 plt_offset = h->plt.offset;
2712 relocation = (resolved_plt->output_section->vma
2713 + resolved_plt->output_offset + plt_offset);
2715 switch (r_type)
2717 default:
2718 bad_ifunc_reloc:
2719 if (h->root.root.string)
2720 name = h->root.root.string;
2721 else
2722 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2723 NULL);
2724 _bfd_error_handler
2725 /* xgettext:c-format */
2726 (_("%pB: relocation %s against STT_GNU_IFUNC "
2727 "symbol `%s' isn't supported"), input_bfd,
2728 howto->name, name);
2729 bfd_set_error (bfd_error_bad_value);
2730 return false;
2732 case R_X86_64_32S:
2733 if (bfd_link_pic (info))
2734 abort ();
2735 goto do_relocation;
2737 case R_X86_64_32:
2738 if (ABI_64_P (output_bfd))
2739 goto do_relocation;
2740 /* FALLTHROUGH */
2741 case R_X86_64_64:
2742 do_ifunc_pointer:
2743 if (rel->r_addend != 0)
2745 if (h->root.root.string)
2746 name = h->root.root.string;
2747 else
2748 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
2749 sym, NULL);
2750 _bfd_error_handler
2751 /* xgettext:c-format */
2752 (_("%pB: relocation %s against STT_GNU_IFUNC "
2753 "symbol `%s' has non-zero addend: %" PRId64),
2754 input_bfd, howto->name, name, (int64_t) rel->r_addend);
2755 bfd_set_error (bfd_error_bad_value);
2756 return false;
2759 /* Generate dynamic relcoation only when there is a
2760 non-GOT reference in a shared object or there is no
2761 PLT. */
2762 if ((bfd_link_pic (info) && h->non_got_ref)
2763 || h->plt.offset == (bfd_vma) -1)
2765 Elf_Internal_Rela outrel;
2766 asection *sreloc;
2768 /* Need a dynamic relocation to get the real function
2769 address. */
2770 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2771 info,
2772 input_section,
2773 rel->r_offset);
2774 if (outrel.r_offset == (bfd_vma) -1
2775 || outrel.r_offset == (bfd_vma) -2)
2776 abort ();
2778 outrel.r_offset += (input_section->output_section->vma
2779 + input_section->output_offset);
2781 if (POINTER_LOCAL_IFUNC_P (info, h))
2783 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
2784 h->root.root.string,
2785 h->root.u.def.section->owner);
2787 /* This symbol is resolved locally. */
2788 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
2789 outrel.r_addend = (h->root.u.def.value
2790 + h->root.u.def.section->output_section->vma
2791 + h->root.u.def.section->output_offset);
2793 if (htab->params->report_relative_reloc)
2794 _bfd_x86_elf_link_report_relative_reloc
2795 (info, input_section, h, sym,
2796 "R_X86_64_IRELATIVE", &outrel);
2798 else
2800 outrel.r_info = htab->r_info (h->dynindx, r_type);
2801 outrel.r_addend = 0;
2804 /* Dynamic relocations are stored in
2805 1. .rela.ifunc section in PIC object.
2806 2. .rela.got section in dynamic executable.
2807 3. .rela.iplt section in static executable. */
2808 if (bfd_link_pic (info))
2809 sreloc = htab->elf.irelifunc;
2810 else if (htab->elf.splt != NULL)
2811 sreloc = htab->elf.srelgot;
2812 else
2813 sreloc = htab->elf.irelplt;
2814 elf_append_rela (output_bfd, sreloc, &outrel);
2816 /* If this reloc is against an external symbol, we
2817 do not want to fiddle with the addend. Otherwise,
2818 we need to include the symbol value so that it
2819 becomes an addend for the dynamic reloc. For an
2820 internal symbol, we have updated addend. */
2821 continue;
2823 /* FALLTHROUGH */
2824 case R_X86_64_PC32:
2825 case R_X86_64_PC32_BND:
2826 case R_X86_64_PC64:
2827 case R_X86_64_PLT32:
2828 case R_X86_64_PLT32_BND:
2829 goto do_relocation;
2833 skip_ifunc:
2834 resolved_to_zero = (eh != NULL
2835 && UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh));
2837 /* When generating a shared object, the relocations handled here are
2838 copied into the output file to be resolved at run time. */
2839 switch (r_type)
2841 case R_X86_64_GOT32:
2842 case R_X86_64_GOT64:
2843 /* Relocation is to the entry for this symbol in the global
2844 offset table. */
2845 case R_X86_64_GOTPCREL:
2846 case R_X86_64_GOTPCRELX:
2847 case R_X86_64_REX_GOTPCRELX:
2848 case R_X86_64_GOTPCREL64:
2849 /* Use global offset table entry as symbol value. */
2850 case R_X86_64_GOTPLT64:
2851 /* This is obsolete and treated the same as GOT64. */
2852 base_got = htab->elf.sgot;
2854 if (htab->elf.sgot == NULL)
2855 abort ();
2857 relative_reloc = false;
2858 if (h != NULL)
2860 off = h->got.offset;
2861 if (h->needs_plt
2862 && h->plt.offset != (bfd_vma)-1
2863 && off == (bfd_vma)-1)
2865 /* We can't use h->got.offset here to save
2866 state, or even just remember the offset, as
2867 finish_dynamic_symbol would use that as offset into
2868 .got. */
2869 bfd_vma plt_index = (h->plt.offset / plt_entry_size
2870 - htab->plt.has_plt0);
2871 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2872 base_got = htab->elf.sgotplt;
2875 if (RESOLVED_LOCALLY_P (info, h, htab))
2877 /* We must initialize this entry in the global offset
2878 table. Since the offset must always be a multiple
2879 of 8, we use the least significant bit to record
2880 whether we have initialized it already.
2882 When doing a dynamic link, we create a .rela.got
2883 relocation entry to initialize the value. This is
2884 done in the finish_dynamic_symbol routine. */
2885 if ((off & 1) != 0)
2886 off &= ~1;
2887 else
2889 bfd_put_64 (output_bfd, relocation,
2890 base_got->contents + off);
2891 /* Note that this is harmless for the GOTPLT64 case,
2892 as -1 | 1 still is -1. */
2893 h->got.offset |= 1;
2895 if (GENERATE_RELATIVE_RELOC_P (info, h))
2897 /* If this symbol isn't dynamic in PIC,
2898 generate R_X86_64_RELATIVE here. */
2899 eh->no_finish_dynamic_symbol = 1;
2900 relative_reloc = true;
2904 else
2905 unresolved_reloc = false;
2907 else
2909 if (local_got_offsets == NULL)
2910 abort ();
2912 off = local_got_offsets[r_symndx];
2914 /* The offset must always be a multiple of 8. We use
2915 the least significant bit to record whether we have
2916 already generated the necessary reloc. */
2917 if ((off & 1) != 0)
2918 off &= ~1;
2919 else
2921 bfd_put_64 (output_bfd, relocation,
2922 base_got->contents + off);
2923 local_got_offsets[r_symndx] |= 1;
2925 /* NB: GOTPCREL relocations against local absolute
2926 symbol store relocation value in the GOT slot
2927 without relative relocation. */
2928 if (bfd_link_pic (info)
2929 && !(sym->st_shndx == SHN_ABS
2930 && (r_type == R_X86_64_GOTPCREL
2931 || r_type == R_X86_64_GOTPCRELX
2932 || r_type == R_X86_64_REX_GOTPCRELX)))
2933 relative_reloc = true;
2937 if (relative_reloc)
2939 asection *s;
2940 Elf_Internal_Rela outrel;
2942 /* We need to generate a R_X86_64_RELATIVE reloc
2943 for the dynamic linker. */
2944 s = htab->elf.srelgot;
2945 if (s == NULL)
2946 abort ();
2948 outrel.r_offset = (base_got->output_section->vma
2949 + base_got->output_offset
2950 + off);
2951 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
2952 outrel.r_addend = relocation;
2954 if (htab->params->report_relative_reloc)
2955 _bfd_x86_elf_link_report_relative_reloc
2956 (info, input_section, h, sym, "R_X86_64_RELATIVE",
2957 &outrel);
2959 elf_append_rela (output_bfd, s, &outrel);
2962 if (off >= (bfd_vma) -2)
2963 abort ();
2965 relocation = base_got->output_section->vma
2966 + base_got->output_offset + off;
2967 if (r_type != R_X86_64_GOTPCREL
2968 && r_type != R_X86_64_GOTPCRELX
2969 && r_type != R_X86_64_REX_GOTPCRELX
2970 && r_type != R_X86_64_GOTPCREL64)
2971 relocation -= htab->elf.sgotplt->output_section->vma
2972 - htab->elf.sgotplt->output_offset;
2974 break;
2976 case R_X86_64_GOTOFF64:
2977 /* Relocation is relative to the start of the global offset
2978 table. */
2980 /* Check to make sure it isn't a protected function or data
2981 symbol for shared library since it may not be local when
2982 used as function address or with copy relocation. We also
2983 need to make sure that a symbol is referenced locally. */
2984 if (bfd_link_pic (info) && h)
2986 if (!h->def_regular)
2988 const char *v;
2990 switch (ELF_ST_VISIBILITY (h->other))
2992 case STV_HIDDEN:
2993 v = _("hidden symbol");
2994 break;
2995 case STV_INTERNAL:
2996 v = _("internal symbol");
2997 break;
2998 case STV_PROTECTED:
2999 v = _("protected symbol");
3000 break;
3001 default:
3002 v = _("symbol");
3003 break;
3006 _bfd_error_handler
3007 /* xgettext:c-format */
3008 (_("%pB: relocation R_X86_64_GOTOFF64 against undefined %s"
3009 " `%s' can not be used when making a shared object"),
3010 input_bfd, v, h->root.root.string);
3011 bfd_set_error (bfd_error_bad_value);
3012 return false;
3014 else if (!bfd_link_executable (info)
3015 && !SYMBOL_REFERENCES_LOCAL_P (info, h)
3016 && (h->type == STT_FUNC
3017 || h->type == STT_OBJECT)
3018 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3020 _bfd_error_handler
3021 /* xgettext:c-format */
3022 (_("%pB: relocation R_X86_64_GOTOFF64 against protected %s"
3023 " `%s' can not be used when making a shared object"),
3024 input_bfd,
3025 h->type == STT_FUNC ? "function" : "data",
3026 h->root.root.string);
3027 bfd_set_error (bfd_error_bad_value);
3028 return false;
3032 /* Note that sgot is not involved in this
3033 calculation. We always want the start of .got.plt. If we
3034 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3035 permitted by the ABI, we might have to change this
3036 calculation. */
3037 relocation -= htab->elf.sgotplt->output_section->vma
3038 + htab->elf.sgotplt->output_offset;
3039 break;
3041 case R_X86_64_GOTPC32:
3042 case R_X86_64_GOTPC64:
3043 /* Use global offset table as symbol value. */
3044 relocation = htab->elf.sgotplt->output_section->vma
3045 + htab->elf.sgotplt->output_offset;
3046 unresolved_reloc = false;
3047 break;
3049 case R_X86_64_PLTOFF64:
3050 /* Relocation is PLT entry relative to GOT. For local
3051 symbols it's the symbol itself relative to GOT. */
3052 if (h != NULL
3053 /* See PLT32 handling. */
3054 && (h->plt.offset != (bfd_vma) -1
3055 || eh->plt_got.offset != (bfd_vma) -1)
3056 && htab->elf.splt != NULL)
3058 if (eh->plt_got.offset != (bfd_vma) -1)
3060 /* Use the GOT PLT. */
3061 resolved_plt = htab->plt_got;
3062 plt_offset = eh->plt_got.offset;
3064 else if (htab->plt_second != NULL)
3066 resolved_plt = htab->plt_second;
3067 plt_offset = eh->plt_second.offset;
3069 else
3071 resolved_plt = htab->elf.splt;
3072 plt_offset = h->plt.offset;
3075 relocation = (resolved_plt->output_section->vma
3076 + resolved_plt->output_offset
3077 + plt_offset);
3078 unresolved_reloc = false;
3081 relocation -= htab->elf.sgotplt->output_section->vma
3082 + htab->elf.sgotplt->output_offset;
3083 break;
3085 case R_X86_64_PLT32:
3086 case R_X86_64_PLT32_BND:
3087 /* Relocation is to the entry for this symbol in the
3088 procedure linkage table. */
3090 /* Resolve a PLT32 reloc against a local symbol directly,
3091 without using the procedure linkage table. */
3092 if (h == NULL)
3093 break;
3095 if ((h->plt.offset == (bfd_vma) -1
3096 && eh->plt_got.offset == (bfd_vma) -1)
3097 || htab->elf.splt == NULL)
3099 /* We didn't make a PLT entry for this symbol. This
3100 happens when statically linking PIC code, or when
3101 using -Bsymbolic. */
3102 break;
3105 use_plt:
3106 if (h->plt.offset != (bfd_vma) -1)
3108 if (htab->plt_second != NULL)
3110 resolved_plt = htab->plt_second;
3111 plt_offset = eh->plt_second.offset;
3113 else
3115 resolved_plt = htab->elf.splt;
3116 plt_offset = h->plt.offset;
3119 else
3121 /* Use the GOT PLT. */
3122 resolved_plt = htab->plt_got;
3123 plt_offset = eh->plt_got.offset;
3126 relocation = (resolved_plt->output_section->vma
3127 + resolved_plt->output_offset
3128 + plt_offset);
3129 unresolved_reloc = false;
3130 break;
3132 case R_X86_64_SIZE32:
3133 case R_X86_64_SIZE64:
3134 /* Set to symbol size. */
3135 relocation = st_size;
3136 goto direct;
3138 case R_X86_64_PC8:
3139 case R_X86_64_PC16:
3140 case R_X86_64_PC32:
3141 case R_X86_64_PC32_BND:
3142 /* Don't complain about -fPIC if the symbol is undefined when
3143 building executable unless it is unresolved weak symbol,
3144 references a dynamic definition in PIE or -z nocopyreloc
3145 is used. */
3146 no_copyreloc_p
3147 = (info->nocopyreloc
3148 || (h != NULL
3149 && !h->root.linker_def
3150 && !h->root.ldscript_def
3151 && eh->def_protected
3152 && elf_has_no_copy_on_protected (h->root.u.def.section->owner)));
3154 if ((input_section->flags & SEC_ALLOC) != 0
3155 && (input_section->flags & SEC_READONLY) != 0
3156 && h != NULL
3157 && ((bfd_link_executable (info)
3158 && ((h->root.type == bfd_link_hash_undefweak
3159 && (eh == NULL
3160 || !UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
3161 eh)))
3162 || (bfd_link_pie (info)
3163 && !SYMBOL_DEFINED_NON_SHARED_P (h)
3164 && h->def_dynamic)
3165 || (no_copyreloc_p
3166 && h->def_dynamic
3167 && !(h->root.u.def.section->flags & SEC_CODE))))
3168 || (bfd_link_pie (info)
3169 && h->root.type == bfd_link_hash_undefweak)
3170 || bfd_link_dll (info)))
3172 bool fail = false;
3173 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
3175 /* Symbol is referenced locally. Make sure it is
3176 defined locally. */
3177 fail = !SYMBOL_DEFINED_NON_SHARED_P (h);
3179 else if (bfd_link_pie (info))
3181 /* We can only use PC-relative relocations in PIE
3182 from non-code sections. */
3183 if (h->root.type == bfd_link_hash_undefweak
3184 || (h->type == STT_FUNC
3185 && (sec->flags & SEC_CODE) != 0))
3186 fail = true;
3188 else if (no_copyreloc_p || bfd_link_dll (info))
3190 /* Symbol doesn't need copy reloc and isn't
3191 referenced locally. Don't allow PC-relative
3192 relocations against default and protected
3193 symbols since address of protected function
3194 and location of protected data may not be in
3195 the shared object. */
3196 fail = (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3197 || ELF_ST_VISIBILITY (h->other) == STV_PROTECTED);
3200 if (fail)
3201 return elf_x86_64_need_pic (info, input_bfd, input_section,
3202 h, NULL, NULL, howto);
3204 /* Since x86-64 has PC-relative PLT, we can use PLT in PIE
3205 as function address. */
3206 else if (h != NULL
3207 && (input_section->flags & SEC_CODE) == 0
3208 && bfd_link_pie (info)
3209 && h->type == STT_FUNC
3210 && !h->def_regular
3211 && h->def_dynamic)
3212 goto use_plt;
3213 /* Fall through. */
3215 case R_X86_64_8:
3216 case R_X86_64_16:
3217 case R_X86_64_32:
3218 case R_X86_64_PC64:
3219 case R_X86_64_64:
3220 /* FIXME: The ABI says the linker should make sure the value is
3221 the same when it's zeroextended to 64 bit. */
3223 direct:
3224 if ((input_section->flags & SEC_ALLOC) == 0)
3225 break;
3227 need_copy_reloc_in_pie = (bfd_link_pie (info)
3228 && h != NULL
3229 && (h->needs_copy
3230 || eh->needs_copy
3231 || (h->root.type
3232 == bfd_link_hash_undefined))
3233 && (X86_PCREL_TYPE_P (r_type)
3234 || X86_SIZE_TYPE_P (r_type)));
3236 if (GENERATE_DYNAMIC_RELOCATION_P (info, eh, r_type, sec,
3237 need_copy_reloc_in_pie,
3238 resolved_to_zero, false))
3240 Elf_Internal_Rela outrel;
3241 bool skip, relocate;
3242 asection *sreloc;
3243 const char *relative_reloc_name = NULL;
3245 /* When generating a shared object, these relocations
3246 are copied into the output file to be resolved at run
3247 time. */
3248 skip = false;
3249 relocate = false;
3251 outrel.r_offset =
3252 _bfd_elf_section_offset (output_bfd, info, input_section,
3253 rel->r_offset);
3254 if (outrel.r_offset == (bfd_vma) -1)
3255 skip = true;
3256 else if (outrel.r_offset == (bfd_vma) -2)
3257 skip = true, relocate = true;
3259 outrel.r_offset += (input_section->output_section->vma
3260 + input_section->output_offset);
3262 if (skip)
3263 memset (&outrel, 0, sizeof outrel);
3265 else if (COPY_INPUT_RELOC_P (info, h, r_type))
3267 outrel.r_info = htab->r_info (h->dynindx, r_type);
3268 outrel.r_addend = rel->r_addend;
3270 else
3272 /* This symbol is local, or marked to become local.
3273 When relocation overflow check is disabled, we
3274 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
3275 if (r_type == htab->pointer_r_type
3276 || (r_type == R_X86_64_32
3277 && htab->params->no_reloc_overflow_check))
3279 relocate = true;
3280 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3281 outrel.r_addend = relocation + rel->r_addend;
3282 relative_reloc_name = "R_X86_64_RELATIVE";
3284 else if (r_type == R_X86_64_64
3285 && !ABI_64_P (output_bfd))
3287 relocate = true;
3288 outrel.r_info = htab->r_info (0,
3289 R_X86_64_RELATIVE64);
3290 outrel.r_addend = relocation + rel->r_addend;
3291 relative_reloc_name = "R_X86_64_RELATIVE64";
3292 /* Check addend overflow. */
3293 if ((outrel.r_addend & 0x80000000)
3294 != (rel->r_addend & 0x80000000))
3296 const char *name;
3297 int addend = rel->r_addend;
3298 if (h && h->root.root.string)
3299 name = h->root.root.string;
3300 else
3301 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3302 sym, NULL);
3303 _bfd_error_handler
3304 /* xgettext:c-format */
3305 (_("%pB: addend %s%#x in relocation %s against "
3306 "symbol `%s' at %#" PRIx64
3307 " in section `%pA' is out of range"),
3308 input_bfd, addend < 0 ? "-" : "", addend,
3309 howto->name, name, (uint64_t) rel->r_offset,
3310 input_section);
3311 bfd_set_error (bfd_error_bad_value);
3312 return false;
3315 else
3317 long sindx;
3319 if (bfd_is_abs_section (sec))
3320 sindx = 0;
3321 else if (sec == NULL || sec->owner == NULL)
3323 bfd_set_error (bfd_error_bad_value);
3324 return false;
3326 else
3328 asection *osec;
3330 /* We are turning this relocation into one
3331 against a section symbol. It would be
3332 proper to subtract the symbol's value,
3333 osec->vma, from the emitted reloc addend,
3334 but ld.so expects buggy relocs. */
3335 osec = sec->output_section;
3336 sindx = elf_section_data (osec)->dynindx;
3337 if (sindx == 0)
3339 asection *oi = htab->elf.text_index_section;
3340 sindx = elf_section_data (oi)->dynindx;
3342 BFD_ASSERT (sindx != 0);
3345 outrel.r_info = htab->r_info (sindx, r_type);
3346 outrel.r_addend = relocation + rel->r_addend;
3350 sreloc = elf_section_data (input_section)->sreloc;
3352 if (sreloc == NULL || sreloc->contents == NULL)
3354 r = bfd_reloc_notsupported;
3355 goto check_relocation_error;
3358 if (relative_reloc_name
3359 && htab->params->report_relative_reloc)
3360 _bfd_x86_elf_link_report_relative_reloc
3361 (info, input_section, h, sym, relative_reloc_name,
3362 &outrel);
3364 elf_append_rela (output_bfd, sreloc, &outrel);
3366 /* If this reloc is against an external symbol, we do
3367 not want to fiddle with the addend. Otherwise, we
3368 need to include the symbol value so that it becomes
3369 an addend for the dynamic reloc. */
3370 if (! relocate)
3371 continue;
3374 break;
3376 case R_X86_64_TLSGD:
3377 case R_X86_64_GOTPC32_TLSDESC:
3378 case R_X86_64_TLSDESC_CALL:
3379 case R_X86_64_GOTTPOFF:
3380 tls_type = GOT_UNKNOWN;
3381 if (h == NULL && local_got_offsets)
3382 tls_type = elf_x86_local_got_tls_type (input_bfd) [r_symndx];
3383 else if (h != NULL)
3384 tls_type = elf_x86_hash_entry (h)->tls_type;
3386 r_type_tls = r_type;
3387 if (! elf_x86_64_tls_transition (info, input_bfd,
3388 input_section, contents,
3389 symtab_hdr, sym_hashes,
3390 &r_type_tls, tls_type, rel,
3391 relend, h, r_symndx, true))
3392 return false;
3394 if (r_type_tls == R_X86_64_TPOFF32)
3396 bfd_vma roff = rel->r_offset;
3398 BFD_ASSERT (! unresolved_reloc);
3400 if (r_type == R_X86_64_TLSGD)
3402 /* GD->LE transition. For 64bit, change
3403 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3404 .word 0x6666; rex64; call __tls_get_addr@PLT
3406 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3407 .byte 0x66; rex64
3408 call *__tls_get_addr@GOTPCREL(%rip)
3409 which may be converted to
3410 addr32 call __tls_get_addr
3411 into:
3412 movq %fs:0, %rax
3413 leaq foo@tpoff(%rax), %rax
3414 For 32bit, change
3415 leaq foo@tlsgd(%rip), %rdi
3416 .word 0x6666; rex64; call __tls_get_addr@PLT
3418 leaq foo@tlsgd(%rip), %rdi
3419 .byte 0x66; rex64
3420 call *__tls_get_addr@GOTPCREL(%rip)
3421 which may be converted to
3422 addr32 call __tls_get_addr
3423 into:
3424 movl %fs:0, %eax
3425 leaq foo@tpoff(%rax), %rax
3426 For largepic, change:
3427 leaq foo@tlsgd(%rip), %rdi
3428 movabsq $__tls_get_addr@pltoff, %rax
3429 addq %r15, %rax
3430 call *%rax
3431 into:
3432 movq %fs:0, %rax
3433 leaq foo@tpoff(%rax), %rax
3434 nopw 0x0(%rax,%rax,1) */
3435 int largepic = 0;
3436 if (ABI_64_P (output_bfd))
3438 if (contents[roff + 5] == 0xb8)
3440 if (roff < 3
3441 || (roff - 3 + 22) > input_section->size)
3443 corrupt_input:
3444 info->callbacks->einfo
3445 (_("%F%P: corrupt input: %pB\n"),
3446 input_bfd);
3447 return false;
3449 memcpy (contents + roff - 3,
3450 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
3451 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3452 largepic = 1;
3454 else
3456 if (roff < 4
3457 || (roff - 4 + 16) > input_section->size)
3458 goto corrupt_input;
3459 memcpy (contents + roff - 4,
3460 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3461 16);
3464 else
3466 if (roff < 3
3467 || (roff - 3 + 15) > input_section->size)
3468 goto corrupt_input;
3469 memcpy (contents + roff - 3,
3470 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3471 15);
3473 bfd_put_32 (output_bfd,
3474 elf_x86_64_tpoff (info, relocation),
3475 contents + roff + 8 + largepic);
3476 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
3477 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
3478 rel++;
3479 wrel++;
3480 continue;
3482 else if (r_type == R_X86_64_GOTPC32_TLSDESC)
3484 /* GDesc -> LE transition.
3485 It's originally something like:
3486 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
3487 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
3489 Change it to:
3490 movq $x@tpoff, %rax <--- LP64 mode.
3491 rex movl $x@tpoff, %eax <--- X32 mode.
3494 unsigned int val, type;
3496 if (roff < 3)
3497 goto corrupt_input;
3498 type = bfd_get_8 (input_bfd, contents + roff - 3);
3499 val = bfd_get_8 (input_bfd, contents + roff - 1);
3500 bfd_put_8 (output_bfd,
3501 (type & 0x48) | ((type >> 2) & 1),
3502 contents + roff - 3);
3503 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3504 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3505 contents + roff - 1);
3506 bfd_put_32 (output_bfd,
3507 elf_x86_64_tpoff (info, relocation),
3508 contents + roff);
3509 continue;
3511 else if (r_type == R_X86_64_TLSDESC_CALL)
3513 /* GDesc -> LE transition.
3514 It's originally:
3515 call *(%rax) <--- LP64 mode.
3516 call *(%eax) <--- X32 mode.
3517 Turn it into:
3518 xchg %ax,%ax <-- LP64 mode.
3519 nopl (%rax) <-- X32 mode.
3521 unsigned int prefix = 0;
3522 if (!ABI_64_P (input_bfd))
3524 /* Check for call *x@tlsdesc(%eax). */
3525 if (contents[roff] == 0x67)
3526 prefix = 1;
3528 if (prefix)
3530 bfd_put_8 (output_bfd, 0x0f, contents + roff);
3531 bfd_put_8 (output_bfd, 0x1f, contents + roff + 1);
3532 bfd_put_8 (output_bfd, 0x00, contents + roff + 2);
3534 else
3536 bfd_put_8 (output_bfd, 0x66, contents + roff);
3537 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3539 continue;
3541 else if (r_type == R_X86_64_GOTTPOFF)
3543 /* IE->LE transition:
3544 For 64bit, originally it can be one of:
3545 movq foo@gottpoff(%rip), %reg
3546 addq foo@gottpoff(%rip), %reg
3547 We change it into:
3548 movq $foo, %reg
3549 leaq foo(%reg), %reg
3550 addq $foo, %reg.
3551 For 32bit, originally it can be one of:
3552 movq foo@gottpoff(%rip), %reg
3553 addl foo@gottpoff(%rip), %reg
3554 We change it into:
3555 movq $foo, %reg
3556 leal foo(%reg), %reg
3557 addl $foo, %reg. */
3559 unsigned int val, type, reg;
3561 if (roff >= 3)
3562 val = bfd_get_8 (input_bfd, contents + roff - 3);
3563 else
3565 if (roff < 2)
3566 goto corrupt_input;
3567 val = 0;
3569 type = bfd_get_8 (input_bfd, contents + roff - 2);
3570 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3571 reg >>= 3;
3572 if (type == 0x8b)
3574 /* movq */
3575 if (val == 0x4c)
3577 if (roff < 3)
3578 goto corrupt_input;
3579 bfd_put_8 (output_bfd, 0x49,
3580 contents + roff - 3);
3582 else if (!ABI_64_P (output_bfd) && val == 0x44)
3584 if (roff < 3)
3585 goto corrupt_input;
3586 bfd_put_8 (output_bfd, 0x41,
3587 contents + roff - 3);
3589 bfd_put_8 (output_bfd, 0xc7,
3590 contents + roff - 2);
3591 bfd_put_8 (output_bfd, 0xc0 | reg,
3592 contents + roff - 1);
3594 else if (reg == 4)
3596 /* addq/addl -> addq/addl - addressing with %rsp/%r12
3597 is special */
3598 if (val == 0x4c)
3600 if (roff < 3)
3601 goto corrupt_input;
3602 bfd_put_8 (output_bfd, 0x49,
3603 contents + roff - 3);
3605 else if (!ABI_64_P (output_bfd) && val == 0x44)
3607 if (roff < 3)
3608 goto corrupt_input;
3609 bfd_put_8 (output_bfd, 0x41,
3610 contents + roff - 3);
3612 bfd_put_8 (output_bfd, 0x81,
3613 contents + roff - 2);
3614 bfd_put_8 (output_bfd, 0xc0 | reg,
3615 contents + roff - 1);
3617 else
3619 /* addq/addl -> leaq/leal */
3620 if (val == 0x4c)
3622 if (roff < 3)
3623 goto corrupt_input;
3624 bfd_put_8 (output_bfd, 0x4d,
3625 contents + roff - 3);
3627 else if (!ABI_64_P (output_bfd) && val == 0x44)
3629 if (roff < 3)
3630 goto corrupt_input;
3631 bfd_put_8 (output_bfd, 0x45,
3632 contents + roff - 3);
3634 bfd_put_8 (output_bfd, 0x8d,
3635 contents + roff - 2);
3636 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3637 contents + roff - 1);
3639 bfd_put_32 (output_bfd,
3640 elf_x86_64_tpoff (info, relocation),
3641 contents + roff);
3642 continue;
3644 else
3645 BFD_ASSERT (false);
3648 if (htab->elf.sgot == NULL)
3649 abort ();
3651 if (h != NULL)
3653 off = h->got.offset;
3654 offplt = elf_x86_hash_entry (h)->tlsdesc_got;
3656 else
3658 if (local_got_offsets == NULL)
3659 abort ();
3661 off = local_got_offsets[r_symndx];
3662 offplt = local_tlsdesc_gotents[r_symndx];
3665 if ((off & 1) != 0)
3666 off &= ~1;
3667 else
3669 Elf_Internal_Rela outrel;
3670 int dr_type, indx;
3671 asection *sreloc;
3673 if (htab->elf.srelgot == NULL)
3674 abort ();
3676 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3678 if (GOT_TLS_GDESC_P (tls_type))
3680 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
3681 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3682 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3683 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3684 + htab->elf.sgotplt->output_offset
3685 + offplt
3686 + htab->sgotplt_jump_table_size);
3687 sreloc = htab->elf.srelplt;
3688 if (indx == 0)
3689 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
3690 else
3691 outrel.r_addend = 0;
3692 elf_append_rela (output_bfd, sreloc, &outrel);
3695 sreloc = htab->elf.srelgot;
3697 outrel.r_offset = (htab->elf.sgot->output_section->vma
3698 + htab->elf.sgot->output_offset + off);
3700 if (GOT_TLS_GD_P (tls_type))
3701 dr_type = R_X86_64_DTPMOD64;
3702 else if (GOT_TLS_GDESC_P (tls_type))
3703 goto dr_done;
3704 else
3705 dr_type = R_X86_64_TPOFF64;
3707 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3708 outrel.r_addend = 0;
3709 if ((dr_type == R_X86_64_TPOFF64
3710 || dr_type == R_X86_64_TLSDESC) && indx == 0)
3711 outrel.r_addend = relocation - _bfd_x86_elf_dtpoff_base (info);
3712 outrel.r_info = htab->r_info (indx, dr_type);
3714 elf_append_rela (output_bfd, sreloc, &outrel);
3716 if (GOT_TLS_GD_P (tls_type))
3718 if (indx == 0)
3720 BFD_ASSERT (! unresolved_reloc);
3721 bfd_put_64 (output_bfd,
3722 relocation - _bfd_x86_elf_dtpoff_base (info),
3723 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3725 else
3727 bfd_put_64 (output_bfd, 0,
3728 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3729 outrel.r_info = htab->r_info (indx,
3730 R_X86_64_DTPOFF64);
3731 outrel.r_offset += GOT_ENTRY_SIZE;
3732 elf_append_rela (output_bfd, sreloc,
3733 &outrel);
3737 dr_done:
3738 if (h != NULL)
3739 h->got.offset |= 1;
3740 else
3741 local_got_offsets[r_symndx] |= 1;
3744 if (off >= (bfd_vma) -2
3745 && ! GOT_TLS_GDESC_P (tls_type))
3746 abort ();
3747 if (r_type_tls == r_type)
3749 if (r_type == R_X86_64_GOTPC32_TLSDESC
3750 || r_type == R_X86_64_TLSDESC_CALL)
3751 relocation = htab->elf.sgotplt->output_section->vma
3752 + htab->elf.sgotplt->output_offset
3753 + offplt + htab->sgotplt_jump_table_size;
3754 else
3755 relocation = htab->elf.sgot->output_section->vma
3756 + htab->elf.sgot->output_offset + off;
3757 unresolved_reloc = false;
3759 else
3761 bfd_vma roff = rel->r_offset;
3763 if (r_type == R_X86_64_TLSGD)
3765 /* GD->IE transition. For 64bit, change
3766 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3767 .word 0x6666; rex64; call __tls_get_addr@PLT
3769 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3770 .byte 0x66; rex64
3771 call *__tls_get_addr@GOTPCREL(%rip
3772 which may be converted to
3773 addr32 call __tls_get_addr
3774 into:
3775 movq %fs:0, %rax
3776 addq foo@gottpoff(%rip), %rax
3777 For 32bit, change
3778 leaq foo@tlsgd(%rip), %rdi
3779 .word 0x6666; rex64; call __tls_get_addr@PLT
3781 leaq foo@tlsgd(%rip), %rdi
3782 .byte 0x66; rex64;
3783 call *__tls_get_addr@GOTPCREL(%rip)
3784 which may be converted to
3785 addr32 call __tls_get_addr
3786 into:
3787 movl %fs:0, %eax
3788 addq foo@gottpoff(%rip), %rax
3789 For largepic, change:
3790 leaq foo@tlsgd(%rip), %rdi
3791 movabsq $__tls_get_addr@pltoff, %rax
3792 addq %r15, %rax
3793 call *%rax
3794 into:
3795 movq %fs:0, %rax
3796 addq foo@gottpoff(%rax), %rax
3797 nopw 0x0(%rax,%rax,1) */
3798 int largepic = 0;
3799 if (ABI_64_P (output_bfd))
3801 if (contents[roff + 5] == 0xb8)
3803 if (roff < 3
3804 || (roff - 3 + 22) > input_section->size)
3805 goto corrupt_input;
3806 memcpy (contents + roff - 3,
3807 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
3808 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
3809 largepic = 1;
3811 else
3813 if (roff < 4
3814 || (roff - 4 + 16) > input_section->size)
3815 goto corrupt_input;
3816 memcpy (contents + roff - 4,
3817 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3818 16);
3821 else
3823 if (roff < 3
3824 || (roff - 3 + 15) > input_section->size)
3825 goto corrupt_input;
3826 memcpy (contents + roff - 3,
3827 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3828 15);
3831 relocation = (htab->elf.sgot->output_section->vma
3832 + htab->elf.sgot->output_offset + off
3833 - roff
3834 - largepic
3835 - input_section->output_section->vma
3836 - input_section->output_offset
3837 - 12);
3838 bfd_put_32 (output_bfd, relocation,
3839 contents + roff + 8 + largepic);
3840 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
3841 rel++;
3842 wrel++;
3843 continue;
3845 else if (r_type == R_X86_64_GOTPC32_TLSDESC)
3847 /* GDesc -> IE transition.
3848 It's originally something like:
3849 leaq x@tlsdesc(%rip), %rax <--- LP64 mode.
3850 rex leal x@tlsdesc(%rip), %eax <--- X32 mode.
3852 Change it to:
3853 # before xchg %ax,%ax in LP64 mode.
3854 movq x@gottpoff(%rip), %rax
3855 # before nopl (%rax) in X32 mode.
3856 rex movl x@gottpoff(%rip), %eax
3859 /* Now modify the instruction as appropriate. To
3860 turn a lea into a mov in the form we use it, it
3861 suffices to change the second byte from 0x8d to
3862 0x8b. */
3863 if (roff < 2)
3864 goto corrupt_input;
3865 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3867 bfd_put_32 (output_bfd,
3868 htab->elf.sgot->output_section->vma
3869 + htab->elf.sgot->output_offset + off
3870 - rel->r_offset
3871 - input_section->output_section->vma
3872 - input_section->output_offset
3873 - 4,
3874 contents + roff);
3875 continue;
3877 else if (r_type == R_X86_64_TLSDESC_CALL)
3879 /* GDesc -> IE transition.
3880 It's originally:
3881 call *(%rax) <--- LP64 mode.
3882 call *(%eax) <--- X32 mode.
3884 Change it to:
3885 xchg %ax, %ax <-- LP64 mode.
3886 nopl (%rax) <-- X32 mode.
3889 unsigned int prefix = 0;
3890 if (!ABI_64_P (input_bfd))
3892 /* Check for call *x@tlsdesc(%eax). */
3893 if (contents[roff] == 0x67)
3894 prefix = 1;
3896 if (prefix)
3898 bfd_put_8 (output_bfd, 0x0f, contents + roff);
3899 bfd_put_8 (output_bfd, 0x1f, contents + roff + 1);
3900 bfd_put_8 (output_bfd, 0x00, contents + roff + 2);
3902 else
3904 bfd_put_8 (output_bfd, 0x66, contents + roff);
3905 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3907 continue;
3909 else
3910 BFD_ASSERT (false);
3912 break;
3914 case R_X86_64_TLSLD:
3915 if (! elf_x86_64_tls_transition (info, input_bfd,
3916 input_section, contents,
3917 symtab_hdr, sym_hashes,
3918 &r_type, GOT_UNKNOWN, rel,
3919 relend, h, r_symndx, true))
3920 return false;
3922 if (r_type != R_X86_64_TLSLD)
3924 /* LD->LE transition:
3925 leaq foo@tlsld(%rip), %rdi
3926 call __tls_get_addr@PLT
3927 For 64bit, we change it into:
3928 .word 0x6666; .byte 0x66; movq %fs:0, %rax
3929 For 32bit, we change it into:
3930 nopl 0x0(%rax); movl %fs:0, %eax
3932 leaq foo@tlsld(%rip), %rdi;
3933 call *__tls_get_addr@GOTPCREL(%rip)
3934 which may be converted to
3935 addr32 call __tls_get_addr
3936 For 64bit, we change it into:
3937 .word 0x6666; .word 0x6666; movq %fs:0, %rax
3938 For 32bit, we change it into:
3939 nopw 0x0(%rax); movl %fs:0, %eax
3940 For largepic, change:
3941 leaq foo@tlsgd(%rip), %rdi
3942 movabsq $__tls_get_addr@pltoff, %rax
3943 addq %rbx, %rax
3944 call *%rax
3945 into
3946 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
3947 movq %fs:0, %eax */
3949 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3950 if (ABI_64_P (output_bfd))
3952 if ((rel->r_offset + 5) >= input_section->size)
3953 goto corrupt_input;
3954 if (contents[rel->r_offset + 5] == 0xb8)
3956 if (rel->r_offset < 3
3957 || (rel->r_offset - 3 + 22) > input_section->size)
3958 goto corrupt_input;
3959 memcpy (contents + rel->r_offset - 3,
3960 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
3961 "\x64\x48\x8b\x04\x25\0\0\0", 22);
3963 else if (contents[rel->r_offset + 4] == 0xff
3964 || contents[rel->r_offset + 4] == 0x67)
3966 if (rel->r_offset < 3
3967 || (rel->r_offset - 3 + 13) > input_section->size)
3968 goto corrupt_input;
3969 memcpy (contents + rel->r_offset - 3,
3970 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
3971 13);
3974 else
3976 if (rel->r_offset < 3
3977 || (rel->r_offset - 3 + 12) > input_section->size)
3978 goto corrupt_input;
3979 memcpy (contents + rel->r_offset - 3,
3980 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3983 else
3985 if ((rel->r_offset + 4) >= input_section->size)
3986 goto corrupt_input;
3987 if (contents[rel->r_offset + 4] == 0xff)
3989 if (rel->r_offset < 3
3990 || (rel->r_offset - 3 + 13) > input_section->size)
3991 goto corrupt_input;
3992 memcpy (contents + rel->r_offset - 3,
3993 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
3994 13);
3996 else
3998 if (rel->r_offset < 3
3999 || (rel->r_offset - 3 + 12) > input_section->size)
4000 goto corrupt_input;
4001 memcpy (contents + rel->r_offset - 3,
4002 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
4005 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
4006 and R_X86_64_PLTOFF64. */
4007 rel++;
4008 wrel++;
4009 continue;
4012 if (htab->elf.sgot == NULL)
4013 abort ();
4015 off = htab->tls_ld_or_ldm_got.offset;
4016 if (off & 1)
4017 off &= ~1;
4018 else
4020 Elf_Internal_Rela outrel;
4022 if (htab->elf.srelgot == NULL)
4023 abort ();
4025 outrel.r_offset = (htab->elf.sgot->output_section->vma
4026 + htab->elf.sgot->output_offset + off);
4028 bfd_put_64 (output_bfd, 0,
4029 htab->elf.sgot->contents + off);
4030 bfd_put_64 (output_bfd, 0,
4031 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
4032 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
4033 outrel.r_addend = 0;
4034 elf_append_rela (output_bfd, htab->elf.srelgot,
4035 &outrel);
4036 htab->tls_ld_or_ldm_got.offset |= 1;
4038 relocation = htab->elf.sgot->output_section->vma
4039 + htab->elf.sgot->output_offset + off;
4040 unresolved_reloc = false;
4041 break;
4043 case R_X86_64_DTPOFF32:
4044 if (!bfd_link_executable (info)
4045 || (input_section->flags & SEC_CODE) == 0)
4046 relocation -= _bfd_x86_elf_dtpoff_base (info);
4047 else
4048 relocation = elf_x86_64_tpoff (info, relocation);
4049 break;
4051 case R_X86_64_TPOFF32:
4052 case R_X86_64_TPOFF64:
4053 BFD_ASSERT (bfd_link_executable (info));
4054 relocation = elf_x86_64_tpoff (info, relocation);
4055 break;
4057 case R_X86_64_DTPOFF64:
4058 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
4059 relocation -= _bfd_x86_elf_dtpoff_base (info);
4060 break;
4062 default:
4063 break;
4066 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
4067 because such sections are not SEC_ALLOC and thus ld.so will
4068 not process them. */
4069 if (unresolved_reloc
4070 && !((input_section->flags & SEC_DEBUGGING) != 0
4071 && h->def_dynamic)
4072 && _bfd_elf_section_offset (output_bfd, info, input_section,
4073 rel->r_offset) != (bfd_vma) -1)
4075 switch (r_type)
4077 case R_X86_64_32S:
4078 sec = h->root.u.def.section;
4079 if ((info->nocopyreloc
4080 || (eh->def_protected
4081 && elf_has_no_copy_on_protected (h->root.u.def.section->owner)))
4082 && !(h->root.u.def.section->flags & SEC_CODE))
4083 return elf_x86_64_need_pic (info, input_bfd, input_section,
4084 h, NULL, NULL, howto);
4085 /* Fall through. */
4087 default:
4088 _bfd_error_handler
4089 /* xgettext:c-format */
4090 (_("%pB(%pA+%#" PRIx64 "): "
4091 "unresolvable %s relocation against symbol `%s'"),
4092 input_bfd,
4093 input_section,
4094 (uint64_t) rel->r_offset,
4095 howto->name,
4096 h->root.root.string);
4097 return false;
4101 do_relocation:
4102 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4103 contents, rel->r_offset,
4104 relocation, rel->r_addend);
4106 check_relocation_error:
4107 if (r != bfd_reloc_ok)
4109 const char *name;
4111 if (h != NULL)
4112 name = h->root.root.string;
4113 else
4115 name = bfd_elf_string_from_elf_section (input_bfd,
4116 symtab_hdr->sh_link,
4117 sym->st_name);
4118 if (name == NULL)
4119 return false;
4120 if (*name == '\0')
4121 name = bfd_section_name (sec);
4124 if (r == bfd_reloc_overflow)
4126 if (converted_reloc)
4128 info->callbacks->einfo
4129 ("%X%H:", input_bfd, input_section, rel->r_offset);
4130 info->callbacks->einfo
4131 (_(" failed to convert GOTPCREL relocation against "
4132 "'%s'; relink with --no-relax\n"),
4133 name);
4134 status = false;
4135 continue;
4137 (*info->callbacks->reloc_overflow)
4138 (info, (h ? &h->root : NULL), name, howto->name,
4139 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4141 else
4143 _bfd_error_handler
4144 /* xgettext:c-format */
4145 (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
4146 input_bfd, input_section,
4147 (uint64_t) rel->r_offset, name, (int) r);
4148 return false;
4152 if (wrel != rel)
4153 *wrel = *rel;
4156 if (wrel != rel)
4158 Elf_Internal_Shdr *rel_hdr;
4159 size_t deleted = rel - wrel;
4161 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
4162 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4163 if (rel_hdr->sh_size == 0)
4165 /* It is too late to remove an empty reloc section. Leave
4166 one NONE reloc.
4167 ??? What is wrong with an empty section??? */
4168 rel_hdr->sh_size = rel_hdr->sh_entsize;
4169 deleted -= 1;
4171 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
4172 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
4173 input_section->reloc_count -= deleted;
4176 return status;
4179 /* Finish up dynamic symbol handling. We set the contents of various
4180 dynamic sections here. */
4182 static bool
4183 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
4184 struct bfd_link_info *info,
4185 struct elf_link_hash_entry *h,
4186 Elf_Internal_Sym *sym)
4188 struct elf_x86_link_hash_table *htab;
4189 bool use_plt_second;
4190 struct elf_x86_link_hash_entry *eh;
4191 bool local_undefweak;
4193 htab = elf_x86_hash_table (info, X86_64_ELF_DATA);
4194 if (htab == NULL)
4195 return false;
4197 /* Use the second PLT section only if there is .plt section. */
4198 use_plt_second = htab->elf.splt != NULL && htab->plt_second != NULL;
4200 eh = (struct elf_x86_link_hash_entry *) h;
4201 if (eh->no_finish_dynamic_symbol)
4202 abort ();
4204 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
4205 resolved undefined weak symbols in executable so that their
4206 references have value 0 at run-time. */
4207 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info, eh);
4209 if (h->plt.offset != (bfd_vma) -1)
4211 bfd_vma plt_index;
4212 bfd_vma got_offset, plt_offset;
4213 Elf_Internal_Rela rela;
4214 bfd_byte *loc;
4215 asection *plt, *gotplt, *relplt, *resolved_plt;
4216 const struct elf_backend_data *bed;
4217 bfd_vma plt_got_pcrel_offset;
4219 /* When building a static executable, use .iplt, .igot.plt and
4220 .rela.iplt sections for STT_GNU_IFUNC symbols. */
4221 if (htab->elf.splt != NULL)
4223 plt = htab->elf.splt;
4224 gotplt = htab->elf.sgotplt;
4225 relplt = htab->elf.srelplt;
4227 else
4229 plt = htab->elf.iplt;
4230 gotplt = htab->elf.igotplt;
4231 relplt = htab->elf.irelplt;
4234 VERIFY_PLT_ENTRY (info, h, plt, gotplt, relplt, local_undefweak)
4236 /* Get the index in the procedure linkage table which
4237 corresponds to this symbol. This is the index of this symbol
4238 in all the symbols for which we are making plt entries. The
4239 first entry in the procedure linkage table is reserved.
4241 Get the offset into the .got table of the entry that
4242 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
4243 bytes. The first three are reserved for the dynamic linker.
4245 For static executables, we don't reserve anything. */
4247 if (plt == htab->elf.splt)
4249 got_offset = (h->plt.offset / htab->plt.plt_entry_size
4250 - htab->plt.has_plt0);
4251 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
4253 else
4255 got_offset = h->plt.offset / htab->plt.plt_entry_size;
4256 got_offset = got_offset * GOT_ENTRY_SIZE;
4259 /* Fill in the entry in the procedure linkage table. */
4260 memcpy (plt->contents + h->plt.offset, htab->plt.plt_entry,
4261 htab->plt.plt_entry_size);
4262 if (use_plt_second)
4264 memcpy (htab->plt_second->contents + eh->plt_second.offset,
4265 htab->non_lazy_plt->plt_entry,
4266 htab->non_lazy_plt->plt_entry_size);
4268 resolved_plt = htab->plt_second;
4269 plt_offset = eh->plt_second.offset;
4271 else
4273 resolved_plt = plt;
4274 plt_offset = h->plt.offset;
4277 /* Insert the relocation positions of the plt section. */
4279 /* Put offset the PC-relative instruction referring to the GOT entry,
4280 subtracting the size of that instruction. */
4281 plt_got_pcrel_offset = (gotplt->output_section->vma
4282 + gotplt->output_offset
4283 + got_offset
4284 - resolved_plt->output_section->vma
4285 - resolved_plt->output_offset
4286 - plt_offset
4287 - htab->plt.plt_got_insn_size);
4289 /* Check PC-relative offset overflow in PLT entry. */
4290 if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
4291 /* xgettext:c-format */
4292 info->callbacks->einfo (_("%F%pB: PC-relative offset overflow in PLT entry for `%s'\n"),
4293 output_bfd, h->root.root.string);
4295 bfd_put_32 (output_bfd, plt_got_pcrel_offset,
4296 (resolved_plt->contents + plt_offset
4297 + htab->plt.plt_got_offset));
4299 /* Fill in the entry in the global offset table, initially this
4300 points to the second part of the PLT entry. Leave the entry
4301 as zero for undefined weak symbol in PIE. No PLT relocation
4302 against undefined weak symbol in PIE. */
4303 if (!local_undefweak)
4305 if (htab->plt.has_plt0)
4306 bfd_put_64 (output_bfd, (plt->output_section->vma
4307 + plt->output_offset
4308 + h->plt.offset
4309 + htab->lazy_plt->plt_lazy_offset),
4310 gotplt->contents + got_offset);
4312 /* Fill in the entry in the .rela.plt section. */
4313 rela.r_offset = (gotplt->output_section->vma
4314 + gotplt->output_offset
4315 + got_offset);
4316 if (PLT_LOCAL_IFUNC_P (info, h))
4318 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
4319 h->root.root.string,
4320 h->root.u.def.section->owner);
4322 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4323 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
4324 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4325 rela.r_addend = (h->root.u.def.value
4326 + h->root.u.def.section->output_section->vma
4327 + h->root.u.def.section->output_offset);
4329 if (htab->params->report_relative_reloc)
4330 _bfd_x86_elf_link_report_relative_reloc
4331 (info, relplt, h, sym, "R_X86_64_IRELATIVE", &rela);
4333 /* R_X86_64_IRELATIVE comes last. */
4334 plt_index = htab->next_irelative_index--;
4336 else
4338 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
4339 rela.r_addend = 0;
4340 plt_index = htab->next_jump_slot_index++;
4343 /* Don't fill the second and third slots in PLT entry for
4344 static executables nor without PLT0. */
4345 if (plt == htab->elf.splt && htab->plt.has_plt0)
4347 bfd_vma plt0_offset
4348 = h->plt.offset + htab->lazy_plt->plt_plt_insn_end;
4350 /* Put relocation index. */
4351 bfd_put_32 (output_bfd, plt_index,
4352 (plt->contents + h->plt.offset
4353 + htab->lazy_plt->plt_reloc_offset));
4355 /* Put offset for jmp .PLT0 and check for overflow. We don't
4356 check relocation index for overflow since branch displacement
4357 will overflow first. */
4358 if (plt0_offset > 0x80000000)
4359 /* xgettext:c-format */
4360 info->callbacks->einfo (_("%F%pB: branch displacement overflow in PLT entry for `%s'\n"),
4361 output_bfd, h->root.root.string);
4362 bfd_put_32 (output_bfd, - plt0_offset,
4363 (plt->contents + h->plt.offset
4364 + htab->lazy_plt->plt_plt_offset));
4367 bed = get_elf_backend_data (output_bfd);
4368 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
4369 bed->s->swap_reloca_out (output_bfd, &rela, loc);
4372 else if (eh->plt_got.offset != (bfd_vma) -1)
4374 bfd_vma got_offset, plt_offset;
4375 asection *plt, *got;
4376 bool got_after_plt;
4377 int32_t got_pcrel_offset;
4379 /* Set the entry in the GOT procedure linkage table. */
4380 plt = htab->plt_got;
4381 got = htab->elf.sgot;
4382 got_offset = h->got.offset;
4384 if (got_offset == (bfd_vma) -1
4385 || (h->type == STT_GNU_IFUNC && h->def_regular)
4386 || plt == NULL
4387 || got == NULL)
4388 abort ();
4390 /* Use the non-lazy PLT entry template for the GOT PLT since they
4391 are the identical. */
4392 /* Fill in the entry in the GOT procedure linkage table. */
4393 plt_offset = eh->plt_got.offset;
4394 memcpy (plt->contents + plt_offset,
4395 htab->non_lazy_plt->plt_entry,
4396 htab->non_lazy_plt->plt_entry_size);
4398 /* Put offset the PC-relative instruction referring to the GOT
4399 entry, subtracting the size of that instruction. */
4400 got_pcrel_offset = (got->output_section->vma
4401 + got->output_offset
4402 + got_offset
4403 - plt->output_section->vma
4404 - plt->output_offset
4405 - plt_offset
4406 - htab->non_lazy_plt->plt_got_insn_size);
4408 /* Check PC-relative offset overflow in GOT PLT entry. */
4409 got_after_plt = got->output_section->vma > plt->output_section->vma;
4410 if ((got_after_plt && got_pcrel_offset < 0)
4411 || (!got_after_plt && got_pcrel_offset > 0))
4412 /* xgettext:c-format */
4413 info->callbacks->einfo (_("%F%pB: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
4414 output_bfd, h->root.root.string);
4416 bfd_put_32 (output_bfd, got_pcrel_offset,
4417 (plt->contents + plt_offset
4418 + htab->non_lazy_plt->plt_got_offset));
4421 if (!local_undefweak
4422 && !h->def_regular
4423 && (h->plt.offset != (bfd_vma) -1
4424 || eh->plt_got.offset != (bfd_vma) -1))
4426 /* Mark the symbol as undefined, rather than as defined in
4427 the .plt section. Leave the value if there were any
4428 relocations where pointer equality matters (this is a clue
4429 for the dynamic linker, to make function pointer
4430 comparisons work between an application and shared
4431 library), otherwise set it to zero. If a function is only
4432 called from a binary, there is no need to slow down
4433 shared libraries because of that. */
4434 sym->st_shndx = SHN_UNDEF;
4435 if (!h->pointer_equality_needed)
4436 sym->st_value = 0;
4439 _bfd_x86_elf_link_fixup_ifunc_symbol (info, htab, h, sym);
4441 /* Don't generate dynamic GOT relocation against undefined weak
4442 symbol in executable. */
4443 if (h->got.offset != (bfd_vma) -1
4444 && ! GOT_TLS_GD_ANY_P (elf_x86_hash_entry (h)->tls_type)
4445 && elf_x86_hash_entry (h)->tls_type != GOT_TLS_IE
4446 && !local_undefweak)
4448 Elf_Internal_Rela rela;
4449 asection *relgot = htab->elf.srelgot;
4450 const char *relative_reloc_name = NULL;
4452 /* This symbol has an entry in the global offset table. Set it
4453 up. */
4454 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4455 abort ();
4457 rela.r_offset = (htab->elf.sgot->output_section->vma
4458 + htab->elf.sgot->output_offset
4459 + (h->got.offset &~ (bfd_vma) 1));
4461 /* If this is a static link, or it is a -Bsymbolic link and the
4462 symbol is defined locally or was forced to be local because
4463 of a version file, we just want to emit a RELATIVE reloc.
4464 The entry in the global offset table will already have been
4465 initialized in the relocate_section function. */
4466 if (h->def_regular
4467 && h->type == STT_GNU_IFUNC)
4469 if (h->plt.offset == (bfd_vma) -1)
4471 /* STT_GNU_IFUNC is referenced without PLT. */
4472 if (htab->elf.splt == NULL)
4474 /* use .rel[a].iplt section to store .got relocations
4475 in static executable. */
4476 relgot = htab->elf.irelplt;
4478 if (SYMBOL_REFERENCES_LOCAL_P (info, h))
4480 info->callbacks->minfo (_("Local IFUNC function `%s' in %pB\n"),
4481 h->root.root.string,
4482 h->root.u.def.section->owner);
4484 rela.r_info = htab->r_info (0,
4485 R_X86_64_IRELATIVE);
4486 rela.r_addend = (h->root.u.def.value
4487 + h->root.u.def.section->output_section->vma
4488 + h->root.u.def.section->output_offset);
4489 relative_reloc_name = "R_X86_64_IRELATIVE";
4491 else
4492 goto do_glob_dat;
4494 else if (bfd_link_pic (info))
4496 /* Generate R_X86_64_GLOB_DAT. */
4497 goto do_glob_dat;
4499 else
4501 asection *plt;
4502 bfd_vma plt_offset;
4504 if (!h->pointer_equality_needed)
4505 abort ();
4507 /* For non-shared object, we can't use .got.plt, which
4508 contains the real function addres if we need pointer
4509 equality. We load the GOT entry with the PLT entry. */
4510 if (htab->plt_second != NULL)
4512 plt = htab->plt_second;
4513 plt_offset = eh->plt_second.offset;
4515 else
4517 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4518 plt_offset = h->plt.offset;
4520 bfd_put_64 (output_bfd, (plt->output_section->vma
4521 + plt->output_offset
4522 + plt_offset),
4523 htab->elf.sgot->contents + h->got.offset);
4524 return true;
4527 else if (bfd_link_pic (info)
4528 && SYMBOL_REFERENCES_LOCAL_P (info, h))
4530 if (!SYMBOL_DEFINED_NON_SHARED_P (h))
4531 return false;
4532 BFD_ASSERT((h->got.offset & 1) != 0);
4533 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4534 rela.r_addend = (h->root.u.def.value
4535 + h->root.u.def.section->output_section->vma
4536 + h->root.u.def.section->output_offset);
4537 relative_reloc_name = "R_X86_64_RELATIVE";
4539 else
4541 BFD_ASSERT((h->got.offset & 1) == 0);
4542 do_glob_dat:
4543 bfd_put_64 (output_bfd, (bfd_vma) 0,
4544 htab->elf.sgot->contents + h->got.offset);
4545 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
4546 rela.r_addend = 0;
4549 if (relative_reloc_name != NULL
4550 && htab->params->report_relative_reloc)
4551 _bfd_x86_elf_link_report_relative_reloc
4552 (info, relgot, h, sym, relative_reloc_name, &rela);
4554 elf_append_rela (output_bfd, relgot, &rela);
4557 if (h->needs_copy)
4559 Elf_Internal_Rela rela;
4560 asection *s;
4562 /* This symbol needs a copy reloc. Set it up. */
4563 VERIFY_COPY_RELOC (h, htab)
4565 rela.r_offset = (h->root.u.def.value
4566 + h->root.u.def.section->output_section->vma
4567 + h->root.u.def.section->output_offset);
4568 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
4569 rela.r_addend = 0;
4570 if (h->root.u.def.section == htab->elf.sdynrelro)
4571 s = htab->elf.sreldynrelro;
4572 else
4573 s = htab->elf.srelbss;
4574 elf_append_rela (output_bfd, s, &rela);
4577 return true;
4580 /* Finish up local dynamic symbol handling. We set the contents of
4581 various dynamic sections here. */
4583 static int
4584 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
4586 struct elf_link_hash_entry *h
4587 = (struct elf_link_hash_entry *) *slot;
4588 struct bfd_link_info *info
4589 = (struct bfd_link_info *) inf;
4591 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
4592 info, h, NULL);
4595 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
4596 here since undefined weak symbol may not be dynamic and may not be
4597 called for elf_x86_64_finish_dynamic_symbol. */
4599 static bool
4600 elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
4601 void *inf)
4603 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
4604 struct bfd_link_info *info = (struct bfd_link_info *) inf;
4606 if (h->root.type != bfd_link_hash_undefweak
4607 || h->dynindx != -1)
4608 return true;
4610 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
4611 info, h, NULL);
4614 /* Used to decide how to sort relocs in an optimal manner for the
4615 dynamic linker, before writing them out. */
4617 static enum elf_reloc_type_class
4618 elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
4619 const asection *rel_sec ATTRIBUTE_UNUSED,
4620 const Elf_Internal_Rela *rela)
4622 bfd *abfd = info->output_bfd;
4623 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4624 struct elf_x86_link_hash_table *htab
4625 = elf_x86_hash_table (info, X86_64_ELF_DATA);
4627 if (htab->elf.dynsym != NULL
4628 && htab->elf.dynsym->contents != NULL)
4630 /* Check relocation against STT_GNU_IFUNC symbol if there are
4631 dynamic symbols. */
4632 unsigned long r_symndx = htab->r_sym (rela->r_info);
4633 if (r_symndx != STN_UNDEF)
4635 Elf_Internal_Sym sym;
4636 if (!bed->s->swap_symbol_in (abfd,
4637 (htab->elf.dynsym->contents
4638 + r_symndx * bed->s->sizeof_sym),
4639 0, &sym))
4640 abort ();
4642 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
4643 return reloc_class_ifunc;
4647 switch ((int) ELF32_R_TYPE (rela->r_info))
4649 case R_X86_64_IRELATIVE:
4650 return reloc_class_ifunc;
4651 case R_X86_64_RELATIVE:
4652 case R_X86_64_RELATIVE64:
4653 return reloc_class_relative;
4654 case R_X86_64_JUMP_SLOT:
4655 return reloc_class_plt;
4656 case R_X86_64_COPY:
4657 return reloc_class_copy;
4658 default:
4659 return reloc_class_normal;
4663 /* Finish up the dynamic sections. */
4665 static bool
4666 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
4667 struct bfd_link_info *info)
4669 struct elf_x86_link_hash_table *htab;
4671 htab = _bfd_x86_elf_finish_dynamic_sections (output_bfd, info);
4672 if (htab == NULL)
4673 return false;
4675 if (! htab->elf.dynamic_sections_created)
4676 return true;
4678 if (htab->elf.splt && htab->elf.splt->size > 0)
4680 elf_section_data (htab->elf.splt->output_section)
4681 ->this_hdr.sh_entsize = htab->plt.plt_entry_size;
4683 if (htab->plt.has_plt0)
4685 /* Fill in the special first entry in the procedure linkage
4686 table. */
4687 memcpy (htab->elf.splt->contents,
4688 htab->lazy_plt->plt0_entry,
4689 htab->lazy_plt->plt0_entry_size);
4690 /* Add offset for pushq GOT+8(%rip), since the instruction
4691 uses 6 bytes subtract this value. */
4692 bfd_put_32 (output_bfd,
4693 (htab->elf.sgotplt->output_section->vma
4694 + htab->elf.sgotplt->output_offset
4696 - htab->elf.splt->output_section->vma
4697 - htab->elf.splt->output_offset
4698 - 6),
4699 (htab->elf.splt->contents
4700 + htab->lazy_plt->plt0_got1_offset));
4701 /* Add offset for the PC-relative instruction accessing
4702 GOT+16, subtracting the offset to the end of that
4703 instruction. */
4704 bfd_put_32 (output_bfd,
4705 (htab->elf.sgotplt->output_section->vma
4706 + htab->elf.sgotplt->output_offset
4707 + 16
4708 - htab->elf.splt->output_section->vma
4709 - htab->elf.splt->output_offset
4710 - htab->lazy_plt->plt0_got2_insn_end),
4711 (htab->elf.splt->contents
4712 + htab->lazy_plt->plt0_got2_offset));
4715 if (htab->elf.tlsdesc_plt)
4717 bfd_put_64 (output_bfd, (bfd_vma) 0,
4718 htab->elf.sgot->contents + htab->elf.tlsdesc_got);
4720 memcpy (htab->elf.splt->contents + htab->elf.tlsdesc_plt,
4721 htab->lazy_plt->plt_tlsdesc_entry,
4722 htab->lazy_plt->plt_tlsdesc_entry_size);
4724 /* Add offset for pushq GOT+8(%rip), since ENDBR64 uses 4
4725 bytes and the instruction uses 6 bytes, subtract these
4726 values. */
4727 bfd_put_32 (output_bfd,
4728 (htab->elf.sgotplt->output_section->vma
4729 + htab->elf.sgotplt->output_offset
4731 - htab->elf.splt->output_section->vma
4732 - htab->elf.splt->output_offset
4733 - htab->elf.tlsdesc_plt
4734 - htab->lazy_plt->plt_tlsdesc_got1_insn_end),
4735 (htab->elf.splt->contents
4736 + htab->elf.tlsdesc_plt
4737 + htab->lazy_plt->plt_tlsdesc_got1_offset));
4738 /* Add offset for indirect branch via GOT+TDG, where TDG
4739 stands for htab->tlsdesc_got, subtracting the offset
4740 to the end of that instruction. */
4741 bfd_put_32 (output_bfd,
4742 (htab->elf.sgot->output_section->vma
4743 + htab->elf.sgot->output_offset
4744 + htab->elf.tlsdesc_got
4745 - htab->elf.splt->output_section->vma
4746 - htab->elf.splt->output_offset
4747 - htab->elf.tlsdesc_plt
4748 - htab->lazy_plt->plt_tlsdesc_got2_insn_end),
4749 (htab->elf.splt->contents
4750 + htab->elf.tlsdesc_plt
4751 + htab->lazy_plt->plt_tlsdesc_got2_offset));
4755 /* Fill PLT entries for undefined weak symbols in PIE. */
4756 if (bfd_link_pie (info))
4757 bfd_hash_traverse (&info->hash->table,
4758 elf_x86_64_pie_finish_undefweak_symbol,
4759 info);
4761 return true;
4764 /* Fill PLT/GOT entries and allocate dynamic relocations for local
4765 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
4766 It has to be done before elf_link_sort_relocs is called so that
4767 dynamic relocations are properly sorted. */
4769 static bool
4770 elf_x86_64_output_arch_local_syms
4771 (bfd *output_bfd ATTRIBUTE_UNUSED,
4772 struct bfd_link_info *info,
4773 void *flaginfo ATTRIBUTE_UNUSED,
4774 int (*func) (void *, const char *,
4775 Elf_Internal_Sym *,
4776 asection *,
4777 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
4779 struct elf_x86_link_hash_table *htab
4780 = elf_x86_hash_table (info, X86_64_ELF_DATA);
4781 if (htab == NULL)
4782 return false;
4784 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4785 htab_traverse (htab->loc_hash_table,
4786 elf_x86_64_finish_local_dynamic_symbol,
4787 info);
4789 return true;
4792 /* Similar to _bfd_elf_get_synthetic_symtab. Support PLTs with all
4793 dynamic relocations. */
4795 static long
4796 elf_x86_64_get_synthetic_symtab (bfd *abfd,
4797 long symcount ATTRIBUTE_UNUSED,
4798 asymbol **syms ATTRIBUTE_UNUSED,
4799 long dynsymcount,
4800 asymbol **dynsyms,
4801 asymbol **ret)
4803 long count, i, n;
4804 int j;
4805 bfd_byte *plt_contents;
4806 long relsize;
4807 const struct elf_x86_lazy_plt_layout *lazy_plt;
4808 const struct elf_x86_non_lazy_plt_layout *non_lazy_plt;
4809 const struct elf_x86_lazy_plt_layout *lazy_bnd_plt;
4810 const struct elf_x86_non_lazy_plt_layout *non_lazy_bnd_plt;
4811 const struct elf_x86_lazy_plt_layout *lazy_ibt_plt;
4812 const struct elf_x86_non_lazy_plt_layout *non_lazy_ibt_plt;
4813 asection *plt;
4814 enum elf_x86_plt_type plt_type;
4815 struct elf_x86_plt plts[] =
4817 { ".plt", NULL, NULL, plt_unknown, 0, 0, 0, 0 },
4818 { ".plt.got", NULL, NULL, plt_non_lazy, 0, 0, 0, 0 },
4819 { ".plt.sec", NULL, NULL, plt_second, 0, 0, 0, 0 },
4820 { ".plt.bnd", NULL, NULL, plt_second, 0, 0, 0, 0 },
4821 { NULL, NULL, NULL, plt_non_lazy, 0, 0, 0, 0 }
4824 *ret = NULL;
4826 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0)
4827 return 0;
4829 if (dynsymcount <= 0)
4830 return 0;
4832 relsize = bfd_get_dynamic_reloc_upper_bound (abfd);
4833 if (relsize <= 0)
4834 return -1;
4836 lazy_plt = &elf_x86_64_lazy_plt;
4837 non_lazy_plt = &elf_x86_64_non_lazy_plt;
4838 lazy_bnd_plt = &elf_x86_64_lazy_bnd_plt;
4839 non_lazy_bnd_plt = &elf_x86_64_non_lazy_bnd_plt;
4840 if (ABI_64_P (abfd))
4842 lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
4843 non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
4845 else
4847 lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
4848 non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
4851 count = 0;
4852 for (j = 0; plts[j].name != NULL; j++)
4854 plt = bfd_get_section_by_name (abfd, plts[j].name);
4855 if (plt == NULL || plt->size == 0)
4856 continue;
4858 /* Get the PLT section contents. */
4859 if (!bfd_malloc_and_get_section (abfd, plt, &plt_contents))
4860 break;
4862 /* Check what kind of PLT it is. */
4863 plt_type = plt_unknown;
4864 if (plts[j].type == plt_unknown
4865 && (plt->size >= (lazy_plt->plt_entry_size
4866 + lazy_plt->plt_entry_size)))
4868 /* Match lazy PLT first. Need to check the first two
4869 instructions. */
4870 if ((memcmp (plt_contents, lazy_plt->plt0_entry,
4871 lazy_plt->plt0_got1_offset) == 0)
4872 && (memcmp (plt_contents + 6, lazy_plt->plt0_entry + 6,
4873 2) == 0))
4874 plt_type = plt_lazy;
4875 else if (lazy_bnd_plt != NULL
4876 && (memcmp (plt_contents, lazy_bnd_plt->plt0_entry,
4877 lazy_bnd_plt->plt0_got1_offset) == 0)
4878 && (memcmp (plt_contents + 6,
4879 lazy_bnd_plt->plt0_entry + 6, 3) == 0))
4881 plt_type = plt_lazy | plt_second;
4882 /* The fist entry in the lazy IBT PLT is the same as the
4883 lazy BND PLT. */
4884 if ((memcmp (plt_contents + lazy_ibt_plt->plt_entry_size,
4885 lazy_ibt_plt->plt_entry,
4886 lazy_ibt_plt->plt_got_offset) == 0))
4887 lazy_plt = lazy_ibt_plt;
4888 else
4889 lazy_plt = lazy_bnd_plt;
4893 if (non_lazy_plt != NULL
4894 && (plt_type == plt_unknown || plt_type == plt_non_lazy)
4895 && plt->size >= non_lazy_plt->plt_entry_size)
4897 /* Match non-lazy PLT. */
4898 if (memcmp (plt_contents, non_lazy_plt->plt_entry,
4899 non_lazy_plt->plt_got_offset) == 0)
4900 plt_type = plt_non_lazy;
4903 if (plt_type == plt_unknown || plt_type == plt_second)
4905 if (non_lazy_bnd_plt != NULL
4906 && plt->size >= non_lazy_bnd_plt->plt_entry_size
4907 && (memcmp (plt_contents, non_lazy_bnd_plt->plt_entry,
4908 non_lazy_bnd_plt->plt_got_offset) == 0))
4910 /* Match BND PLT. */
4911 plt_type = plt_second;
4912 non_lazy_plt = non_lazy_bnd_plt;
4914 else if (non_lazy_ibt_plt != NULL
4915 && plt->size >= non_lazy_ibt_plt->plt_entry_size
4916 && (memcmp (plt_contents,
4917 non_lazy_ibt_plt->plt_entry,
4918 non_lazy_ibt_plt->plt_got_offset) == 0))
4920 /* Match IBT PLT. */
4921 plt_type = plt_second;
4922 non_lazy_plt = non_lazy_ibt_plt;
4926 if (plt_type == plt_unknown)
4928 free (plt_contents);
4929 continue;
4932 plts[j].sec = plt;
4933 plts[j].type = plt_type;
4935 if ((plt_type & plt_lazy))
4937 plts[j].plt_got_offset = lazy_plt->plt_got_offset;
4938 plts[j].plt_got_insn_size = lazy_plt->plt_got_insn_size;
4939 plts[j].plt_entry_size = lazy_plt->plt_entry_size;
4940 /* Skip PLT0 in lazy PLT. */
4941 i = 1;
4943 else
4945 plts[j].plt_got_offset = non_lazy_plt->plt_got_offset;
4946 plts[j].plt_got_insn_size = non_lazy_plt->plt_got_insn_size;
4947 plts[j].plt_entry_size = non_lazy_plt->plt_entry_size;
4948 i = 0;
4951 /* Skip lazy PLT when the second PLT is used. */
4952 if (plt_type == (plt_lazy | plt_second))
4953 plts[j].count = 0;
4954 else
4956 n = plt->size / plts[j].plt_entry_size;
4957 plts[j].count = n;
4958 count += n - i;
4961 plts[j].contents = plt_contents;
4964 return _bfd_x86_elf_get_synthetic_symtab (abfd, count, relsize,
4965 (bfd_vma) 0, plts, dynsyms,
4966 ret);
4969 /* Handle an x86-64 specific section when reading an object file. This
4970 is called when elfcode.h finds a section with an unknown type. */
4972 static bool
4973 elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
4974 const char *name, int shindex)
4976 if (hdr->sh_type != SHT_X86_64_UNWIND)
4977 return false;
4979 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4980 return false;
4982 return true;
4985 /* Hook called by the linker routine which adds symbols from an object
4986 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4987 of .bss. */
4989 static bool
4990 elf_x86_64_add_symbol_hook (bfd *abfd,
4991 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4992 Elf_Internal_Sym *sym,
4993 const char **namep ATTRIBUTE_UNUSED,
4994 flagword *flagsp ATTRIBUTE_UNUSED,
4995 asection **secp,
4996 bfd_vma *valp)
4998 asection *lcomm;
5000 switch (sym->st_shndx)
5002 case SHN_X86_64_LCOMMON:
5003 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
5004 if (lcomm == NULL)
5006 lcomm = bfd_make_section_with_flags (abfd,
5007 "LARGE_COMMON",
5008 (SEC_ALLOC
5009 | SEC_IS_COMMON
5010 | SEC_LINKER_CREATED));
5011 if (lcomm == NULL)
5012 return false;
5013 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
5015 *secp = lcomm;
5016 *valp = sym->st_size;
5017 return true;
5020 return true;
5024 /* Given a BFD section, try to locate the corresponding ELF section
5025 index. */
5027 static bool
5028 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
5029 asection *sec, int *index_return)
5031 if (sec == &_bfd_elf_large_com_section)
5033 *index_return = SHN_X86_64_LCOMMON;
5034 return true;
5036 return false;
5039 /* Process a symbol. */
5041 static void
5042 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
5043 asymbol *asym)
5045 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
5047 switch (elfsym->internal_elf_sym.st_shndx)
5049 case SHN_X86_64_LCOMMON:
5050 asym->section = &_bfd_elf_large_com_section;
5051 asym->value = elfsym->internal_elf_sym.st_size;
5052 /* Common symbol doesn't set BSF_GLOBAL. */
5053 asym->flags &= ~BSF_GLOBAL;
5054 break;
5058 static bool
5059 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
5061 return (sym->st_shndx == SHN_COMMON
5062 || sym->st_shndx == SHN_X86_64_LCOMMON);
5065 static unsigned int
5066 elf_x86_64_common_section_index (asection *sec)
5068 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5069 return SHN_COMMON;
5070 else
5071 return SHN_X86_64_LCOMMON;
5074 static asection *
5075 elf_x86_64_common_section (asection *sec)
5077 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
5078 return bfd_com_section_ptr;
5079 else
5080 return &_bfd_elf_large_com_section;
5083 static bool
5084 elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
5085 const Elf_Internal_Sym *sym,
5086 asection **psec,
5087 bool newdef,
5088 bool olddef,
5089 bfd *oldbfd,
5090 const asection *oldsec)
5092 /* A normal common symbol and a large common symbol result in a
5093 normal common symbol. We turn the large common symbol into a
5094 normal one. */
5095 if (!olddef
5096 && h->root.type == bfd_link_hash_common
5097 && !newdef
5098 && bfd_is_com_section (*psec)
5099 && oldsec != *psec)
5101 if (sym->st_shndx == SHN_COMMON
5102 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
5104 h->root.u.c.p->section
5105 = bfd_make_section_old_way (oldbfd, "COMMON");
5106 h->root.u.c.p->section->flags = SEC_ALLOC;
5108 else if (sym->st_shndx == SHN_X86_64_LCOMMON
5109 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
5110 *psec = bfd_com_section_ptr;
5113 return true;
5116 static int
5117 elf_x86_64_additional_program_headers (bfd *abfd,
5118 struct bfd_link_info *info ATTRIBUTE_UNUSED)
5120 asection *s;
5121 int count = 0;
5123 /* Check to see if we need a large readonly segment. */
5124 s = bfd_get_section_by_name (abfd, ".lrodata");
5125 if (s && (s->flags & SEC_LOAD))
5126 count++;
5128 /* Check to see if we need a large data segment. Since .lbss sections
5129 is placed right after the .bss section, there should be no need for
5130 a large data segment just because of .lbss. */
5131 s = bfd_get_section_by_name (abfd, ".ldata");
5132 if (s && (s->flags & SEC_LOAD))
5133 count++;
5135 return count;
5138 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
5140 static bool
5141 elf_x86_64_relocs_compatible (const bfd_target *input,
5142 const bfd_target *output)
5144 return ((xvec_get_elf_backend_data (input)->s->elfclass
5145 == xvec_get_elf_backend_data (output)->s->elfclass)
5146 && _bfd_elf_relocs_compatible (input, output));
5149 /* Set up x86-64 GNU properties. Return the first relocatable ELF input
5150 with GNU properties if found. Otherwise, return NULL. */
5152 static bfd *
5153 elf_x86_64_link_setup_gnu_properties (struct bfd_link_info *info)
5155 struct elf_x86_init_table init_table;
5156 const struct elf_backend_data *bed;
5157 struct elf_x86_link_hash_table *htab;
5159 if ((int) R_X86_64_standard >= (int) R_X86_64_converted_reloc_bit
5160 || (int) R_X86_64_max <= (int) R_X86_64_converted_reloc_bit
5161 || ((int) (R_X86_64_GNU_VTINHERIT | R_X86_64_converted_reloc_bit)
5162 != (int) R_X86_64_GNU_VTINHERIT)
5163 || ((int) (R_X86_64_GNU_VTENTRY | R_X86_64_converted_reloc_bit)
5164 != (int) R_X86_64_GNU_VTENTRY))
5165 abort ();
5167 /* This is unused for x86-64. */
5168 init_table.plt0_pad_byte = 0x90;
5170 bed = get_elf_backend_data (info->output_bfd);
5171 htab = elf_x86_hash_table (info, bed->target_id);
5172 if (!htab)
5173 abort ();
5174 if (htab->params->bndplt)
5176 init_table.lazy_plt = &elf_x86_64_lazy_bnd_plt;
5177 init_table.non_lazy_plt = &elf_x86_64_non_lazy_bnd_plt;
5179 else
5181 init_table.lazy_plt = &elf_x86_64_lazy_plt;
5182 init_table.non_lazy_plt = &elf_x86_64_non_lazy_plt;
5185 if (ABI_64_P (info->output_bfd))
5187 init_table.lazy_ibt_plt = &elf_x86_64_lazy_ibt_plt;
5188 init_table.non_lazy_ibt_plt = &elf_x86_64_non_lazy_ibt_plt;
5190 else
5192 init_table.lazy_ibt_plt = &elf_x32_lazy_ibt_plt;
5193 init_table.non_lazy_ibt_plt = &elf_x32_non_lazy_ibt_plt;
5196 if (ABI_64_P (info->output_bfd))
5198 init_table.r_info = elf64_r_info;
5199 init_table.r_sym = elf64_r_sym;
5201 else
5203 init_table.r_info = elf32_r_info;
5204 init_table.r_sym = elf32_r_sym;
5207 return _bfd_x86_elf_link_setup_gnu_properties (info, &init_table);
5210 static const struct bfd_elf_special_section
5211 elf_x86_64_special_sections[]=
5213 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5214 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5215 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
5216 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5217 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
5218 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
5219 { NULL, 0, 0, 0, 0 }
5222 #define TARGET_LITTLE_SYM x86_64_elf64_vec
5223 #define TARGET_LITTLE_NAME "elf64-x86-64"
5224 #define ELF_ARCH bfd_arch_i386
5225 #define ELF_TARGET_ID X86_64_ELF_DATA
5226 #define ELF_MACHINE_CODE EM_X86_64
5227 #if DEFAULT_LD_Z_SEPARATE_CODE
5228 # define ELF_MAXPAGESIZE 0x1000
5229 #else
5230 # define ELF_MAXPAGESIZE 0x200000
5231 #endif
5232 #define ELF_MINPAGESIZE 0x1000
5233 #define ELF_COMMONPAGESIZE 0x1000
5235 #define elf_backend_can_gc_sections 1
5236 #define elf_backend_can_refcount 1
5237 #define elf_backend_want_got_plt 1
5238 #define elf_backend_plt_readonly 1
5239 #define elf_backend_want_plt_sym 0
5240 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
5241 #define elf_backend_rela_normal 1
5242 #define elf_backend_plt_alignment 4
5243 #define elf_backend_extern_protected_data 1
5244 #define elf_backend_caches_rawsize 1
5245 #define elf_backend_dtrel_excludes_plt 1
5246 #define elf_backend_want_dynrelro 1
5248 #define elf_info_to_howto elf_x86_64_info_to_howto
5250 #define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
5251 #define bfd_elf64_bfd_reloc_name_lookup \
5252 elf_x86_64_reloc_name_lookup
5254 #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
5255 #define elf_backend_check_relocs elf_x86_64_check_relocs
5256 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
5257 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
5258 #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
5259 #define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
5260 #define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
5261 #define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
5262 #ifdef CORE_HEADER
5263 #define elf_backend_write_core_note elf_x86_64_write_core_note
5264 #endif
5265 #define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
5266 #define elf_backend_relocate_section elf_x86_64_relocate_section
5267 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
5268 #define elf_backend_object_p elf64_x86_64_elf_object_p
5269 #define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
5271 #define elf_backend_section_from_shdr \
5272 elf_x86_64_section_from_shdr
5274 #define elf_backend_section_from_bfd_section \
5275 elf_x86_64_elf_section_from_bfd_section
5276 #define elf_backend_add_symbol_hook \
5277 elf_x86_64_add_symbol_hook
5278 #define elf_backend_symbol_processing \
5279 elf_x86_64_symbol_processing
5280 #define elf_backend_common_section_index \
5281 elf_x86_64_common_section_index
5282 #define elf_backend_common_section \
5283 elf_x86_64_common_section
5284 #define elf_backend_common_definition \
5285 elf_x86_64_common_definition
5286 #define elf_backend_merge_symbol \
5287 elf_x86_64_merge_symbol
5288 #define elf_backend_special_sections \
5289 elf_x86_64_special_sections
5290 #define elf_backend_additional_program_headers \
5291 elf_x86_64_additional_program_headers
5292 #define elf_backend_setup_gnu_properties \
5293 elf_x86_64_link_setup_gnu_properties
5294 #define elf_backend_hide_symbol \
5295 _bfd_x86_elf_hide_symbol
5297 #undef elf64_bed
5298 #define elf64_bed elf64_x86_64_bed
5300 #include "elf64-target.h"
5302 /* CloudABI support. */
5304 #undef TARGET_LITTLE_SYM
5305 #define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
5306 #undef TARGET_LITTLE_NAME
5307 #define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
5309 #undef ELF_OSABI
5310 #define ELF_OSABI ELFOSABI_CLOUDABI
5312 #undef elf64_bed
5313 #define elf64_bed elf64_x86_64_cloudabi_bed
5315 #include "elf64-target.h"
5317 /* FreeBSD support. */
5319 #undef TARGET_LITTLE_SYM
5320 #define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
5321 #undef TARGET_LITTLE_NAME
5322 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
5324 #undef ELF_OSABI
5325 #define ELF_OSABI ELFOSABI_FREEBSD
5327 #undef elf64_bed
5328 #define elf64_bed elf64_x86_64_fbsd_bed
5330 #include "elf64-target.h"
5332 /* Solaris 2 support. */
5334 #undef TARGET_LITTLE_SYM
5335 #define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
5336 #undef TARGET_LITTLE_NAME
5337 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
5339 #undef ELF_TARGET_OS
5340 #define ELF_TARGET_OS is_solaris
5342 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
5343 objects won't be recognized. */
5344 #undef ELF_OSABI
5346 #undef elf64_bed
5347 #define elf64_bed elf64_x86_64_sol2_bed
5349 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
5350 boundary. */
5351 #undef elf_backend_static_tls_alignment
5352 #define elf_backend_static_tls_alignment 16
5354 /* The Solaris 2 ABI requires a plt symbol on all platforms.
5356 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
5357 File, p.63. */
5358 #undef elf_backend_want_plt_sym
5359 #define elf_backend_want_plt_sym 1
5361 #undef elf_backend_strtab_flags
5362 #define elf_backend_strtab_flags SHF_STRINGS
5364 static bool
5365 elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
5366 bfd *obfd ATTRIBUTE_UNUSED,
5367 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
5368 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
5370 /* PR 19938: FIXME: Need to add code for setting the sh_info
5371 and sh_link fields of Solaris specific section types. */
5372 return false;
5375 #undef elf_backend_copy_special_section_fields
5376 #define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
5378 #include "elf64-target.h"
5380 /* Restore defaults. */
5381 #undef ELF_OSABI
5382 #undef elf_backend_static_tls_alignment
5383 #undef elf_backend_want_plt_sym
5384 #define elf_backend_want_plt_sym 0
5385 #undef elf_backend_strtab_flags
5386 #undef elf_backend_copy_special_section_fields
5388 /* Intel L1OM support. */
5390 static bool
5391 elf64_l1om_elf_object_p (bfd *abfd)
5393 /* Set the right machine number for an L1OM elf64 file. */
5394 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
5395 return true;
5398 #undef TARGET_LITTLE_SYM
5399 #define TARGET_LITTLE_SYM l1om_elf64_vec
5400 #undef TARGET_LITTLE_NAME
5401 #define TARGET_LITTLE_NAME "elf64-l1om"
5402 #undef ELF_ARCH
5403 #define ELF_ARCH bfd_arch_l1om
5405 #undef ELF_MACHINE_CODE
5406 #define ELF_MACHINE_CODE EM_L1OM
5408 #undef ELF_OSABI
5410 #undef elf64_bed
5411 #define elf64_bed elf64_l1om_bed
5413 #undef elf_backend_object_p
5414 #define elf_backend_object_p elf64_l1om_elf_object_p
5416 /* Restore defaults. */
5417 #undef ELF_MAXPAGESIZE
5418 #undef ELF_MINPAGESIZE
5419 #undef ELF_COMMONPAGESIZE
5420 #if DEFAULT_LD_Z_SEPARATE_CODE
5421 # define ELF_MAXPAGESIZE 0x1000
5422 #else
5423 # define ELF_MAXPAGESIZE 0x200000
5424 #endif
5425 #define ELF_MINPAGESIZE 0x1000
5426 #define ELF_COMMONPAGESIZE 0x1000
5427 #undef elf_backend_plt_alignment
5428 #define elf_backend_plt_alignment 4
5429 #undef ELF_TARGET_OS
5431 #include "elf64-target.h"
5433 /* FreeBSD L1OM support. */
5435 #undef TARGET_LITTLE_SYM
5436 #define TARGET_LITTLE_SYM l1om_elf64_fbsd_vec
5437 #undef TARGET_LITTLE_NAME
5438 #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
5440 #undef ELF_OSABI
5441 #define ELF_OSABI ELFOSABI_FREEBSD
5443 #undef elf64_bed
5444 #define elf64_bed elf64_l1om_fbsd_bed
5446 #include "elf64-target.h"
5448 /* Intel K1OM support. */
5450 static bool
5451 elf64_k1om_elf_object_p (bfd *abfd)
5453 /* Set the right machine number for an K1OM elf64 file. */
5454 bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
5455 return true;
5458 #undef TARGET_LITTLE_SYM
5459 #define TARGET_LITTLE_SYM k1om_elf64_vec
5460 #undef TARGET_LITTLE_NAME
5461 #define TARGET_LITTLE_NAME "elf64-k1om"
5462 #undef ELF_ARCH
5463 #define ELF_ARCH bfd_arch_k1om
5465 #undef ELF_MACHINE_CODE
5466 #define ELF_MACHINE_CODE EM_K1OM
5468 #undef ELF_OSABI
5470 #undef elf64_bed
5471 #define elf64_bed elf64_k1om_bed
5473 #undef elf_backend_object_p
5474 #define elf_backend_object_p elf64_k1om_elf_object_p
5476 #include "elf64-target.h"
5478 /* FreeBSD K1OM support. */
5480 #undef TARGET_LITTLE_SYM
5481 #define TARGET_LITTLE_SYM k1om_elf64_fbsd_vec
5482 #undef TARGET_LITTLE_NAME
5483 #define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
5485 #undef ELF_OSABI
5486 #define ELF_OSABI ELFOSABI_FREEBSD
5488 #undef elf64_bed
5489 #define elf64_bed elf64_k1om_fbsd_bed
5491 #include "elf64-target.h"
5493 /* 32bit x86-64 support. */
5495 #undef TARGET_LITTLE_SYM
5496 #define TARGET_LITTLE_SYM x86_64_elf32_vec
5497 #undef TARGET_LITTLE_NAME
5498 #define TARGET_LITTLE_NAME "elf32-x86-64"
5499 #undef elf32_bed
5500 #define elf32_bed elf32_x86_64_bed
5502 #undef ELF_ARCH
5503 #define ELF_ARCH bfd_arch_i386
5505 #undef ELF_MACHINE_CODE
5506 #define ELF_MACHINE_CODE EM_X86_64
5508 #undef ELF_OSABI
5510 #define bfd_elf32_bfd_reloc_type_lookup \
5511 elf_x86_64_reloc_type_lookup
5512 #define bfd_elf32_bfd_reloc_name_lookup \
5513 elf_x86_64_reloc_name_lookup
5514 #define bfd_elf32_get_synthetic_symtab \
5515 elf_x86_64_get_synthetic_symtab
5517 #undef elf_backend_object_p
5518 #define elf_backend_object_p \
5519 elf32_x86_64_elf_object_p
5521 #undef elf_backend_bfd_from_remote_memory
5522 #define elf_backend_bfd_from_remote_memory \
5523 _bfd_elf32_bfd_from_remote_memory
5525 #undef elf_backend_size_info
5526 #define elf_backend_size_info \
5527 _bfd_elf32_size_info
5529 #include "elf32-target.h"