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