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