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