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