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