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