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