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