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