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