Automatic date update in version.in
[deliverable/binutils-gdb.git] / bfd / elf64-x86-64.c
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. */
56 static reloc_howto_type x86_64_elf_howto_table[] =
57 {
58 HOWTO(R_X86_64_NONE, 0, 3, 0, FALSE, 0, complain_overflow_dont,
59 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
60 FALSE),
61 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
63 FALSE),
64 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
65 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
66 TRUE),
67 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
68 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
69 FALSE),
70 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
71 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
72 TRUE),
73 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
74 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
75 FALSE),
76 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
78 MINUS_ONE, FALSE),
79 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
80 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
81 MINUS_ONE, FALSE),
82 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
84 MINUS_ONE, FALSE),
85 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
86 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
87 0xffffffff, TRUE),
88 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
89 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
90 FALSE),
91 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
92 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
93 FALSE),
94 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
95 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
96 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
97 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
98 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
99 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
100 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
101 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
102 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
103 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
104 MINUS_ONE, FALSE),
105 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
106 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
107 MINUS_ONE, FALSE),
108 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
109 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
110 MINUS_ONE, FALSE),
111 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
112 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
113 0xffffffff, TRUE),
114 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
115 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
116 0xffffffff, TRUE),
117 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
118 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
119 0xffffffff, FALSE),
120 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
121 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
122 0xffffffff, TRUE),
123 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
124 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
125 0xffffffff, FALSE),
126 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
127 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
128 TRUE),
129 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
130 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
131 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
132 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
133 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
134 FALSE, 0xffffffff, 0xffffffff, TRUE),
135 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
136 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
137 FALSE),
138 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
139 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
140 MINUS_ONE, TRUE),
141 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
142 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
143 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
144 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
145 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
146 MINUS_ONE, FALSE),
147 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
148 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
149 MINUS_ONE, FALSE),
150 HOWTO(R_X86_64_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
151 bfd_elf_generic_reloc, "R_X86_64_SIZE32", FALSE, 0xffffffff, 0xffffffff,
152 FALSE),
153 HOWTO(R_X86_64_SIZE64, 0, 4, 64, FALSE, 0, complain_overflow_unsigned,
154 bfd_elf_generic_reloc, "R_X86_64_SIZE64", FALSE, MINUS_ONE, MINUS_ONE,
155 FALSE),
156 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
157 complain_overflow_bitfield, bfd_elf_generic_reloc,
158 "R_X86_64_GOTPC32_TLSDESC",
159 FALSE, 0xffffffff, 0xffffffff, TRUE),
160 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
161 complain_overflow_dont, bfd_elf_generic_reloc,
162 "R_X86_64_TLSDESC_CALL",
163 FALSE, 0, 0, FALSE),
164 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
165 complain_overflow_bitfield, bfd_elf_generic_reloc,
166 "R_X86_64_TLSDESC",
167 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
168 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
169 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
170 MINUS_ONE, FALSE),
171 HOWTO(R_X86_64_RELATIVE64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
172 bfd_elf_generic_reloc, "R_X86_64_RELATIVE64", FALSE, MINUS_ONE,
173 MINUS_ONE, FALSE),
174 HOWTO(R_X86_64_PC32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
175 bfd_elf_generic_reloc, "R_X86_64_PC32_BND", FALSE, 0xffffffff, 0xffffffff,
176 TRUE),
177 HOWTO(R_X86_64_PLT32_BND, 0, 2, 32, TRUE, 0, complain_overflow_signed,
178 bfd_elf_generic_reloc, "R_X86_64_PLT32_BND", FALSE, 0xffffffff, 0xffffffff,
179 TRUE),
180 HOWTO(R_X86_64_GOTPCRELX, 0, 2, 32, TRUE, 0, complain_overflow_signed,
181 bfd_elf_generic_reloc, "R_X86_64_GOTPCRELX", FALSE, 0xffffffff,
182 0xffffffff, TRUE),
183 HOWTO(R_X86_64_REX_GOTPCRELX, 0, 2, 32, TRUE, 0, complain_overflow_signed,
184 bfd_elf_generic_reloc, "R_X86_64_REX_GOTPCRELX", FALSE, 0xffffffff,
185 0xffffffff, TRUE),
186
187 /* We have a gap in the reloc numbers here.
188 R_X86_64_standard counts the number up to this point, and
189 R_X86_64_vt_offset is the value to subtract from a reloc type of
190 R_X86_64_GNU_VT* to form an index into this table. */
191 #define R_X86_64_standard (R_X86_64_REX_GOTPCRELX + 1)
192 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
193
194 /* GNU extension to record C++ vtable hierarchy. */
195 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
196 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
197
198 /* GNU extension to record C++ vtable member usage. */
199 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
200 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
201 FALSE),
202
203 /* Use complain_overflow_bitfield on R_X86_64_32 for x32. */
204 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
205 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
206 FALSE)
207 };
208
209 #define IS_X86_64_PCREL_TYPE(TYPE) \
210 ( ((TYPE) == R_X86_64_PC8) \
211 || ((TYPE) == R_X86_64_PC16) \
212 || ((TYPE) == R_X86_64_PC32) \
213 || ((TYPE) == R_X86_64_PC32_BND) \
214 || ((TYPE) == R_X86_64_PC64))
215
216 /* Map BFD relocs to the x86_64 elf relocs. */
217 struct elf_reloc_map
218 {
219 bfd_reloc_code_real_type bfd_reloc_val;
220 unsigned char elf_reloc_val;
221 };
222
223 static const struct elf_reloc_map x86_64_reloc_map[] =
224 {
225 { BFD_RELOC_NONE, R_X86_64_NONE, },
226 { BFD_RELOC_64, R_X86_64_64, },
227 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
228 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
229 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
230 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
231 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
232 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
233 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
234 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
235 { BFD_RELOC_32, R_X86_64_32, },
236 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
237 { BFD_RELOC_16, R_X86_64_16, },
238 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
239 { BFD_RELOC_8, R_X86_64_8, },
240 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
241 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
242 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
243 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
244 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
245 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
246 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
247 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
248 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
249 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
250 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
251 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
252 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
253 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
254 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
255 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
256 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
257 { BFD_RELOC_SIZE32, R_X86_64_SIZE32, },
258 { BFD_RELOC_SIZE64, R_X86_64_SIZE64, },
259 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
260 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
261 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
262 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
263 { BFD_RELOC_X86_64_PC32_BND, R_X86_64_PC32_BND, },
264 { BFD_RELOC_X86_64_PLT32_BND, R_X86_64_PLT32_BND, },
265 { BFD_RELOC_X86_64_GOTPCRELX, R_X86_64_GOTPCRELX, },
266 { BFD_RELOC_X86_64_REX_GOTPCRELX, R_X86_64_REX_GOTPCRELX, },
267 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
268 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
269 };
270
271 static reloc_howto_type *
272 elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
273 {
274 unsigned i;
275
276 if (r_type == (unsigned int) R_X86_64_32)
277 {
278 if (ABI_64_P (abfd))
279 i = r_type;
280 else
281 i = ARRAY_SIZE (x86_64_elf_howto_table) - 1;
282 }
283 else if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
284 || r_type >= (unsigned int) R_X86_64_max)
285 {
286 if (r_type >= (unsigned int) R_X86_64_standard)
287 {
288 /* xgettext:c-format */
289 _bfd_error_handler (_("%B: invalid relocation type %d"),
290 abfd, (int) r_type);
291 r_type = R_X86_64_NONE;
292 }
293 i = r_type;
294 }
295 else
296 i = r_type - (unsigned int) R_X86_64_vt_offset;
297 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
298 return &x86_64_elf_howto_table[i];
299 }
300
301 /* Given a BFD reloc type, return a HOWTO structure. */
302 static reloc_howto_type *
303 elf_x86_64_reloc_type_lookup (bfd *abfd,
304 bfd_reloc_code_real_type code)
305 {
306 unsigned int i;
307
308 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
309 i++)
310 {
311 if (x86_64_reloc_map[i].bfd_reloc_val == code)
312 return elf_x86_64_rtype_to_howto (abfd,
313 x86_64_reloc_map[i].elf_reloc_val);
314 }
315 return NULL;
316 }
317
318 static reloc_howto_type *
319 elf_x86_64_reloc_name_lookup (bfd *abfd,
320 const char *r_name)
321 {
322 unsigned int i;
323
324 if (!ABI_64_P (abfd) && strcasecmp (r_name, "R_X86_64_32") == 0)
325 {
326 /* Get x32 R_X86_64_32. */
327 reloc_howto_type *reloc
328 = &x86_64_elf_howto_table[ARRAY_SIZE (x86_64_elf_howto_table) - 1];
329 BFD_ASSERT (reloc->type == (unsigned int) R_X86_64_32);
330 return reloc;
331 }
332
333 for (i = 0; i < ARRAY_SIZE (x86_64_elf_howto_table); i++)
334 if (x86_64_elf_howto_table[i].name != NULL
335 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
336 return &x86_64_elf_howto_table[i];
337
338 return NULL;
339 }
340
341 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
342
343 static void
344 elf_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
345 Elf_Internal_Rela *dst)
346 {
347 unsigned r_type;
348
349 r_type = ELF32_R_TYPE (dst->r_info);
350 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
351 BFD_ASSERT (r_type == cache_ptr->howto->type);
352 }
353 \f
354 /* Support for core dump NOTE sections. */
355 static bfd_boolean
356 elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
357 {
358 int offset;
359 size_t size;
360
361 switch (note->descsz)
362 {
363 default:
364 return FALSE;
365
366 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
367 /* pr_cursig */
368 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
369
370 /* pr_pid */
371 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
372
373 /* pr_reg */
374 offset = 72;
375 size = 216;
376
377 break;
378
379 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
380 /* pr_cursig */
381 elf_tdata (abfd)->core->signal
382 = bfd_get_16 (abfd, note->descdata + 12);
383
384 /* pr_pid */
385 elf_tdata (abfd)->core->lwpid
386 = bfd_get_32 (abfd, note->descdata + 32);
387
388 /* pr_reg */
389 offset = 112;
390 size = 216;
391
392 break;
393 }
394
395 /* Make a ".reg/999" section. */
396 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
397 size, note->descpos + offset);
398 }
399
400 static bfd_boolean
401 elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
402 {
403 switch (note->descsz)
404 {
405 default:
406 return FALSE;
407
408 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
409 elf_tdata (abfd)->core->pid
410 = bfd_get_32 (abfd, note->descdata + 12);
411 elf_tdata (abfd)->core->program
412 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
413 elf_tdata (abfd)->core->command
414 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
415 break;
416
417 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
418 elf_tdata (abfd)->core->pid
419 = bfd_get_32 (abfd, note->descdata + 24);
420 elf_tdata (abfd)->core->program
421 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
422 elf_tdata (abfd)->core->command
423 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
424 }
425
426 /* Note that for some reason, a spurious space is tacked
427 onto the end of the args in some (at least one anyway)
428 implementations, so strip it off if it exists. */
429
430 {
431 char *command = elf_tdata (abfd)->core->command;
432 int n = strlen (command);
433
434 if (0 < n && command[n - 1] == ' ')
435 command[n - 1] = '\0';
436 }
437
438 return TRUE;
439 }
440
441 #ifdef CORE_HEADER
442 static char *
443 elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
444 int note_type, ...)
445 {
446 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
447 va_list ap;
448 const char *fname, *psargs;
449 long pid;
450 int cursig;
451 const void *gregs;
452
453 switch (note_type)
454 {
455 default:
456 return NULL;
457
458 case NT_PRPSINFO:
459 va_start (ap, note_type);
460 fname = va_arg (ap, const char *);
461 psargs = va_arg (ap, const char *);
462 va_end (ap);
463
464 if (bed->s->elfclass == ELFCLASS32)
465 {
466 prpsinfo32_t data;
467 memset (&data, 0, sizeof (data));
468 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
469 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
470 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
471 &data, sizeof (data));
472 }
473 else
474 {
475 prpsinfo64_t data;
476 memset (&data, 0, sizeof (data));
477 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
478 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
479 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
480 &data, sizeof (data));
481 }
482 /* NOTREACHED */
483
484 case NT_PRSTATUS:
485 va_start (ap, note_type);
486 pid = va_arg (ap, long);
487 cursig = va_arg (ap, int);
488 gregs = va_arg (ap, const void *);
489 va_end (ap);
490
491 if (bed->s->elfclass == ELFCLASS32)
492 {
493 if (bed->elf_machine_code == EM_X86_64)
494 {
495 prstatusx32_t prstat;
496 memset (&prstat, 0, sizeof (prstat));
497 prstat.pr_pid = pid;
498 prstat.pr_cursig = cursig;
499 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
500 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
501 &prstat, sizeof (prstat));
502 }
503 else
504 {
505 prstatus32_t prstat;
506 memset (&prstat, 0, sizeof (prstat));
507 prstat.pr_pid = pid;
508 prstat.pr_cursig = cursig;
509 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
510 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
511 &prstat, sizeof (prstat));
512 }
513 }
514 else
515 {
516 prstatus64_t prstat;
517 memset (&prstat, 0, sizeof (prstat));
518 prstat.pr_pid = pid;
519 prstat.pr_cursig = cursig;
520 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
521 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type,
522 &prstat, sizeof (prstat));
523 }
524 }
525 /* NOTREACHED */
526 }
527 #endif
528 \f
529 /* Functions for the x86-64 ELF linker. */
530
531 /* The name of the dynamic interpreter. This is put in the .interp
532 section. */
533
534 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
535 #define ELF32_DYNAMIC_INTERPRETER "/lib/ldx32.so.1"
536
537 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
538 copying dynamic variables from a shared lib into an app's dynbss
539 section, and instead use a dynamic relocation to point into the
540 shared lib. */
541 #define ELIMINATE_COPY_RELOCS 1
542
543 /* The size in bytes of an entry in the global offset table. */
544
545 #define GOT_ENTRY_SIZE 8
546
547 /* The size in bytes of an entry in the procedure linkage table. */
548
549 #define PLT_ENTRY_SIZE 16
550
551 /* The first entry in a procedure linkage table looks like this. See the
552 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
553
554 static const bfd_byte elf_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
555 {
556 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
557 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
558 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
559 };
560
561 /* Subsequent entries in a procedure linkage table look like this. */
562
563 static const bfd_byte elf_x86_64_plt_entry[PLT_ENTRY_SIZE] =
564 {
565 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
566 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
567 0x68, /* pushq immediate */
568 0, 0, 0, 0, /* replaced with index into relocation table. */
569 0xe9, /* jmp relative */
570 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
571 };
572
573 /* The first entry in a procedure linkage table with BND relocations
574 like this. */
575
576 static const bfd_byte elf_x86_64_bnd_plt0_entry[PLT_ENTRY_SIZE] =
577 {
578 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
579 0xf2, 0xff, 0x25, 16, 0, 0, 0, /* bnd jmpq *GOT+16(%rip) */
580 0x0f, 0x1f, 0 /* nopl (%rax) */
581 };
582
583 /* Subsequent entries for legacy branches in a procedure linkage table
584 with BND relocations look like this. */
585
586 static const bfd_byte elf_x86_64_legacy_plt_entry[PLT_ENTRY_SIZE] =
587 {
588 0x68, 0, 0, 0, 0, /* pushq immediate */
589 0xe9, 0, 0, 0, 0, /* jmpq relative */
590 0x66, 0x0f, 0x1f, 0x44, 0, 0 /* nopw (%rax,%rax,1) */
591 };
592
593 /* Subsequent entries for branches with BND prefx in a procedure linkage
594 table with BND relocations look like this. */
595
596 static const bfd_byte elf_x86_64_bnd_plt_entry[PLT_ENTRY_SIZE] =
597 {
598 0x68, 0, 0, 0, 0, /* pushq immediate */
599 0xf2, 0xe9, 0, 0, 0, 0, /* bnd jmpq relative */
600 0x0f, 0x1f, 0x44, 0, 0 /* nopl 0(%rax,%rax,1) */
601 };
602
603 /* Entries for legacy branches in the second procedure linkage table
604 look like this. */
605
606 static const bfd_byte elf_x86_64_legacy_plt2_entry[8] =
607 {
608 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
609 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
610 0x66, 0x90 /* xchg %ax,%ax */
611 };
612
613 /* Entries for branches with BND prefix in the second procedure linkage
614 table look like this. */
615
616 static const bfd_byte elf_x86_64_bnd_plt2_entry[8] =
617 {
618 0xf2, 0xff, 0x25, /* bnd jmpq *name@GOTPC(%rip) */
619 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
620 0x90 /* nop */
621 };
622
623 /* .eh_frame covering the .plt section. */
624
625 static const bfd_byte elf_x86_64_eh_frame_plt[] =
626 {
627 #define PLT_CIE_LENGTH 20
628 #define PLT_FDE_LENGTH 36
629 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8
630 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12
631 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
632 0, 0, 0, 0, /* CIE ID */
633 1, /* CIE version */
634 'z', 'R', 0, /* Augmentation string */
635 1, /* Code alignment factor */
636 0x78, /* Data alignment factor */
637 16, /* Return address column */
638 1, /* Augmentation size */
639 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
640 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
641 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
642 DW_CFA_nop, DW_CFA_nop,
643
644 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
645 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */
646 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
647 0, 0, 0, 0, /* .plt size goes here */
648 0, /* Augmentation size */
649 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
650 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
651 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
652 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */
653 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
654 11, /* Block length */
655 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
656 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
657 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge,
658 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
659 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop
660 };
661
662 /* Architecture-specific backend data for x86-64. */
663
664 struct 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. */
708 static 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
726 static 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
762 struct 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
825 struct 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
850 static bfd_boolean
851 elf_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
859 struct 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
926 static struct bfd_hash_entry *
927 elf_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
970 static hashval_t
971 elf_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
980 static int
981 elf_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
993 static struct elf_link_hash_entry *
994 elf_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
1036 static void
1037 elf_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
1051 static struct bfd_link_hash_table *
1052 elf_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
1106 static bfd_boolean
1107 elf_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
1182 static void
1183 elf_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
1264 static bfd_boolean
1265 elf64_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
1272 static bfd_boolean
1273 elf32_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
1283 static bfd_boolean
1284 elf_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
1548 static bfd_boolean
1549 elf_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
1676 static bfd_boolean
1677 elf_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
1745 static bfd_boolean
1746 elf_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
1993 convert:
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
2118 rewrite_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
2146 static bfd_boolean
2147 elf_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:
2563 pointer:
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;
2613 do_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
2798 error_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
2808 static asection *
2809 elf_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
2829 static bfd_boolean
2830 elf_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
2851 static bfd_boolean
2852 elf_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
3030 static bfd_boolean
3031 elf_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
3425 static bfd_boolean
3426 elf_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
3443 static bfd_boolean
3444 elf_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
3481 static bfd_boolean
3482 elf_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
3611 static bfd_boolean
3612 elf_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
3972 static bfd_boolean
3973 elf_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
4021 static void
4022 elf_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
4045 static bfd_vma
4046 elf_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
4057 static bfd_vma
4058 elf_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
4076 static bfd_boolean
4077 is_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
4093 static bfd_boolean
4094 elf_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:
4382 bad_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:
4406 do_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 && htab->elf.splt != NULL)
4698 {
4699 if (htab->plt_bnd != NULL)
4700 {
4701 resolved_plt = htab->plt_bnd;
4702 plt_offset = eh->plt_bnd.offset;
4703 }
4704 else
4705 {
4706 resolved_plt = htab->elf.splt;
4707 plt_offset = h->plt.offset;
4708 }
4709
4710 relocation = (resolved_plt->output_section->vma
4711 + resolved_plt->output_offset
4712 + plt_offset);
4713 unresolved_reloc = FALSE;
4714 }
4715
4716 relocation -= htab->elf.sgotplt->output_section->vma
4717 + htab->elf.sgotplt->output_offset;
4718 break;
4719
4720 case R_X86_64_PLT32:
4721 case R_X86_64_PLT32_BND:
4722 /* Relocation is to the entry for this symbol in the
4723 procedure linkage table. */
4724
4725 /* Resolve a PLT32 reloc against a local symbol directly,
4726 without using the procedure linkage table. */
4727 if (h == NULL)
4728 break;
4729
4730 if ((h->plt.offset == (bfd_vma) -1
4731 && eh->plt_got.offset == (bfd_vma) -1)
4732 || htab->elf.splt == NULL)
4733 {
4734 /* We didn't make a PLT entry for this symbol. This
4735 happens when statically linking PIC code, or when
4736 using -Bsymbolic. */
4737 break;
4738 }
4739
4740 if (h->plt.offset != (bfd_vma) -1)
4741 {
4742 if (htab->plt_bnd != NULL)
4743 {
4744 resolved_plt = htab->plt_bnd;
4745 plt_offset = eh->plt_bnd.offset;
4746 }
4747 else
4748 {
4749 resolved_plt = htab->elf.splt;
4750 plt_offset = h->plt.offset;
4751 }
4752 }
4753 else
4754 {
4755 /* Use the GOT PLT. */
4756 resolved_plt = htab->plt_got;
4757 plt_offset = eh->plt_got.offset;
4758 }
4759
4760 relocation = (resolved_plt->output_section->vma
4761 + resolved_plt->output_offset
4762 + plt_offset);
4763 unresolved_reloc = FALSE;
4764 break;
4765
4766 case R_X86_64_SIZE32:
4767 case R_X86_64_SIZE64:
4768 /* Set to symbol size. */
4769 relocation = st_size;
4770 goto direct;
4771
4772 case R_X86_64_PC8:
4773 case R_X86_64_PC16:
4774 case R_X86_64_PC32:
4775 case R_X86_64_PC32_BND:
4776 /* Don't complain about -fPIC if the symbol is undefined when
4777 building executable unless it is unresolved weak symbol. */
4778 if ((input_section->flags & SEC_ALLOC) != 0
4779 && (input_section->flags & SEC_READONLY) != 0
4780 && h != NULL
4781 && ((bfd_link_executable (info)
4782 && h->root.type == bfd_link_hash_undefweak
4783 && !resolved_to_zero)
4784 || (bfd_link_pic (info)
4785 && !(bfd_link_pie (info)
4786 && h->root.type == bfd_link_hash_undefined))))
4787 {
4788 bfd_boolean fail = FALSE;
4789 bfd_boolean branch
4790 = ((r_type == R_X86_64_PC32
4791 || r_type == R_X86_64_PC32_BND)
4792 && is_32bit_relative_branch (contents, rel->r_offset));
4793
4794 if (SYMBOL_REFERENCES_LOCAL (info, h))
4795 {
4796 /* Symbol is referenced locally. Make sure it is
4797 defined locally or for a branch. */
4798 fail = !h->def_regular && !branch;
4799 }
4800 else if (!(bfd_link_pie (info)
4801 && (h->needs_copy || eh->needs_copy)))
4802 {
4803 /* Symbol doesn't need copy reloc and isn't referenced
4804 locally. We only allow branch to symbol with
4805 non-default visibility. */
4806 fail = (!branch
4807 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
4808 }
4809
4810 if (fail)
4811 return elf_x86_64_need_pic (input_bfd, input_section,
4812 h, NULL, NULL, howto);
4813 }
4814 /* Fall through. */
4815
4816 case R_X86_64_8:
4817 case R_X86_64_16:
4818 case R_X86_64_32:
4819 case R_X86_64_PC64:
4820 case R_X86_64_64:
4821 /* FIXME: The ABI says the linker should make sure the value is
4822 the same when it's zeroextended to 64 bit. */
4823
4824 direct:
4825 if ((input_section->flags & SEC_ALLOC) == 0)
4826 break;
4827
4828 /* Don't copy a pc-relative relocation into the output file
4829 if the symbol needs copy reloc or the symbol is undefined
4830 when building executable. Copy dynamic function pointer
4831 relocations. Don't generate dynamic relocations against
4832 resolved undefined weak symbols in PIE. */
4833 if ((bfd_link_pic (info)
4834 && !(bfd_link_pie (info)
4835 && h != NULL
4836 && (h->needs_copy
4837 || eh->needs_copy
4838 || h->root.type == bfd_link_hash_undefined)
4839 && (IS_X86_64_PCREL_TYPE (r_type)
4840 || r_type == R_X86_64_SIZE32
4841 || r_type == R_X86_64_SIZE64))
4842 && (h == NULL
4843 || ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4844 && !resolved_to_zero)
4845 || h->root.type != bfd_link_hash_undefweak))
4846 && ((! IS_X86_64_PCREL_TYPE (r_type)
4847 && r_type != R_X86_64_SIZE32
4848 && r_type != R_X86_64_SIZE64)
4849 || ! SYMBOL_CALLS_LOCAL (info, h)))
4850 || (ELIMINATE_COPY_RELOCS
4851 && !bfd_link_pic (info)
4852 && h != NULL
4853 && h->dynindx != -1
4854 && (!h->non_got_ref
4855 || eh->func_pointer_refcount > 0
4856 || (h->root.type == bfd_link_hash_undefweak
4857 && !resolved_to_zero))
4858 && ((h->def_dynamic && !h->def_regular)
4859 /* Undefined weak symbol is bound locally when
4860 PIC is false. */
4861 || h->root.type == bfd_link_hash_undefined)))
4862 {
4863 Elf_Internal_Rela outrel;
4864 bfd_boolean skip, relocate;
4865 asection *sreloc;
4866
4867 /* When generating a shared object, these relocations
4868 are copied into the output file to be resolved at run
4869 time. */
4870 skip = FALSE;
4871 relocate = FALSE;
4872
4873 outrel.r_offset =
4874 _bfd_elf_section_offset (output_bfd, info, input_section,
4875 rel->r_offset);
4876 if (outrel.r_offset == (bfd_vma) -1)
4877 skip = TRUE;
4878 else if (outrel.r_offset == (bfd_vma) -2)
4879 skip = TRUE, relocate = TRUE;
4880
4881 outrel.r_offset += (input_section->output_section->vma
4882 + input_section->output_offset);
4883
4884 if (skip)
4885 memset (&outrel, 0, sizeof outrel);
4886
4887 /* h->dynindx may be -1 if this symbol was marked to
4888 become local. */
4889 else if (h != NULL
4890 && h->dynindx != -1
4891 && (IS_X86_64_PCREL_TYPE (r_type)
4892 || !(bfd_link_executable (info)
4893 || SYMBOLIC_BIND (info, h))
4894 || ! h->def_regular))
4895 {
4896 outrel.r_info = htab->r_info (h->dynindx, r_type);
4897 outrel.r_addend = rel->r_addend;
4898 }
4899 else
4900 {
4901 /* This symbol is local, or marked to become local.
4902 When relocation overflow check is disabled, we
4903 convert R_X86_64_32 to dynamic R_X86_64_RELATIVE. */
4904 if (r_type == htab->pointer_r_type
4905 || (r_type == R_X86_64_32
4906 && info->no_reloc_overflow_check))
4907 {
4908 relocate = TRUE;
4909 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4910 outrel.r_addend = relocation + rel->r_addend;
4911 }
4912 else if (r_type == R_X86_64_64
4913 && !ABI_64_P (output_bfd))
4914 {
4915 relocate = TRUE;
4916 outrel.r_info = htab->r_info (0,
4917 R_X86_64_RELATIVE64);
4918 outrel.r_addend = relocation + rel->r_addend;
4919 /* Check addend overflow. */
4920 if ((outrel.r_addend & 0x80000000)
4921 != (rel->r_addend & 0x80000000))
4922 {
4923 const char *name;
4924 int addend = rel->r_addend;
4925 if (h && h->root.root.string)
4926 name = h->root.root.string;
4927 else
4928 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
4929 sym, NULL);
4930 if (addend < 0)
4931 _bfd_error_handler
4932 /* xgettext:c-format */
4933 (_("%B: addend -0x%x in relocation %s against "
4934 "symbol `%s' at 0x%lx in section `%A' is "
4935 "out of range"),
4936 input_bfd, input_section, addend,
4937 howto->name, name,
4938 (unsigned long) rel->r_offset);
4939 else
4940 _bfd_error_handler
4941 /* xgettext:c-format */
4942 (_("%B: addend 0x%x in relocation %s against "
4943 "symbol `%s' at 0x%lx in section `%A' is "
4944 "out of range"),
4945 input_bfd, input_section, addend,
4946 howto->name, name,
4947 (unsigned long) rel->r_offset);
4948 bfd_set_error (bfd_error_bad_value);
4949 return FALSE;
4950 }
4951 }
4952 else
4953 {
4954 long sindx;
4955
4956 if (bfd_is_abs_section (sec))
4957 sindx = 0;
4958 else if (sec == NULL || sec->owner == NULL)
4959 {
4960 bfd_set_error (bfd_error_bad_value);
4961 return FALSE;
4962 }
4963 else
4964 {
4965 asection *osec;
4966
4967 /* We are turning this relocation into one
4968 against a section symbol. It would be
4969 proper to subtract the symbol's value,
4970 osec->vma, from the emitted reloc addend,
4971 but ld.so expects buggy relocs. */
4972 osec = sec->output_section;
4973 sindx = elf_section_data (osec)->dynindx;
4974 if (sindx == 0)
4975 {
4976 asection *oi = htab->elf.text_index_section;
4977 sindx = elf_section_data (oi)->dynindx;
4978 }
4979 BFD_ASSERT (sindx != 0);
4980 }
4981
4982 outrel.r_info = htab->r_info (sindx, r_type);
4983 outrel.r_addend = relocation + rel->r_addend;
4984 }
4985 }
4986
4987 sreloc = elf_section_data (input_section)->sreloc;
4988
4989 if (sreloc == NULL || sreloc->contents == NULL)
4990 {
4991 r = bfd_reloc_notsupported;
4992 goto check_relocation_error;
4993 }
4994
4995 elf_append_rela (output_bfd, sreloc, &outrel);
4996
4997 /* If this reloc is against an external symbol, we do
4998 not want to fiddle with the addend. Otherwise, we
4999 need to include the symbol value so that it becomes
5000 an addend for the dynamic reloc. */
5001 if (! relocate)
5002 continue;
5003 }
5004
5005 break;
5006
5007 case R_X86_64_TLSGD:
5008 case R_X86_64_GOTPC32_TLSDESC:
5009 case R_X86_64_TLSDESC_CALL:
5010 case R_X86_64_GOTTPOFF:
5011 tls_type = GOT_UNKNOWN;
5012 if (h == NULL && local_got_offsets)
5013 tls_type = elf_x86_64_local_got_tls_type (input_bfd) [r_symndx];
5014 else if (h != NULL)
5015 tls_type = elf_x86_64_hash_entry (h)->tls_type;
5016
5017 if (! elf_x86_64_tls_transition (info, input_bfd,
5018 input_section, contents,
5019 symtab_hdr, sym_hashes,
5020 &r_type, tls_type, rel,
5021 relend, h, r_symndx, TRUE))
5022 return FALSE;
5023
5024 if (r_type == R_X86_64_TPOFF32)
5025 {
5026 bfd_vma roff = rel->r_offset;
5027
5028 BFD_ASSERT (! unresolved_reloc);
5029
5030 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
5031 {
5032 /* GD->LE transition. For 64bit, change
5033 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5034 .word 0x6666; rex64; call __tls_get_addr@PLT
5035 or
5036 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5037 .byte 0x66; rex64
5038 call *__tls_get_addr@GOTPCREL(%rip)
5039 which may be converted to
5040 addr32 call __tls_get_addr
5041 into:
5042 movq %fs:0, %rax
5043 leaq foo@tpoff(%rax), %rax
5044 For 32bit, change
5045 leaq foo@tlsgd(%rip), %rdi
5046 .word 0x6666; rex64; call __tls_get_addr@PLT
5047 or
5048 leaq foo@tlsgd(%rip), %rdi
5049 .byte 0x66; rex64
5050 call *__tls_get_addr@GOTPCREL(%rip)
5051 which may be converted to
5052 addr32 call __tls_get_addr
5053 into:
5054 movl %fs:0, %eax
5055 leaq foo@tpoff(%rax), %rax
5056 For largepic, change:
5057 leaq foo@tlsgd(%rip), %rdi
5058 movabsq $__tls_get_addr@pltoff, %rax
5059 addq %r15, %rax
5060 call *%rax
5061 into:
5062 movq %fs:0, %rax
5063 leaq foo@tpoff(%rax), %rax
5064 nopw 0x0(%rax,%rax,1) */
5065 int largepic = 0;
5066 if (ABI_64_P (output_bfd))
5067 {
5068 if (contents[roff + 5] == 0xb8)
5069 {
5070 memcpy (contents + roff - 3,
5071 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80"
5072 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
5073 largepic = 1;
5074 }
5075 else
5076 memcpy (contents + roff - 4,
5077 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
5078 16);
5079 }
5080 else
5081 memcpy (contents + roff - 3,
5082 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
5083 15);
5084 bfd_put_32 (output_bfd,
5085 elf_x86_64_tpoff (info, relocation),
5086 contents + roff + 8 + largepic);
5087 /* Skip R_X86_64_PC32, R_X86_64_PLT32,
5088 R_X86_64_GOTPCRELX and R_X86_64_PLTOFF64. */
5089 rel++;
5090 wrel++;
5091 continue;
5092 }
5093 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
5094 {
5095 /* GDesc -> LE transition.
5096 It's originally something like:
5097 leaq x@tlsdesc(%rip), %rax
5098
5099 Change it to:
5100 movl $x@tpoff, %rax. */
5101
5102 unsigned int val, type;
5103
5104 type = bfd_get_8 (input_bfd, contents + roff - 3);
5105 val = bfd_get_8 (input_bfd, contents + roff - 1);
5106 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
5107 contents + roff - 3);
5108 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
5109 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
5110 contents + roff - 1);
5111 bfd_put_32 (output_bfd,
5112 elf_x86_64_tpoff (info, relocation),
5113 contents + roff);
5114 continue;
5115 }
5116 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
5117 {
5118 /* GDesc -> LE transition.
5119 It's originally:
5120 call *(%rax)
5121 Turn it into:
5122 xchg %ax,%ax. */
5123 bfd_put_8 (output_bfd, 0x66, contents + roff);
5124 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5125 continue;
5126 }
5127 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
5128 {
5129 /* IE->LE transition:
5130 For 64bit, originally it can be one of:
5131 movq foo@gottpoff(%rip), %reg
5132 addq foo@gottpoff(%rip), %reg
5133 We change it into:
5134 movq $foo, %reg
5135 leaq foo(%reg), %reg
5136 addq $foo, %reg.
5137 For 32bit, originally it can be one of:
5138 movq foo@gottpoff(%rip), %reg
5139 addl foo@gottpoff(%rip), %reg
5140 We change it into:
5141 movq $foo, %reg
5142 leal foo(%reg), %reg
5143 addl $foo, %reg. */
5144
5145 unsigned int val, type, reg;
5146
5147 if (roff >= 3)
5148 val = bfd_get_8 (input_bfd, contents + roff - 3);
5149 else
5150 val = 0;
5151 type = bfd_get_8 (input_bfd, contents + roff - 2);
5152 reg = bfd_get_8 (input_bfd, contents + roff - 1);
5153 reg >>= 3;
5154 if (type == 0x8b)
5155 {
5156 /* movq */
5157 if (val == 0x4c)
5158 bfd_put_8 (output_bfd, 0x49,
5159 contents + roff - 3);
5160 else if (!ABI_64_P (output_bfd) && val == 0x44)
5161 bfd_put_8 (output_bfd, 0x41,
5162 contents + roff - 3);
5163 bfd_put_8 (output_bfd, 0xc7,
5164 contents + roff - 2);
5165 bfd_put_8 (output_bfd, 0xc0 | reg,
5166 contents + roff - 1);
5167 }
5168 else if (reg == 4)
5169 {
5170 /* addq/addl -> addq/addl - addressing with %rsp/%r12
5171 is special */
5172 if (val == 0x4c)
5173 bfd_put_8 (output_bfd, 0x49,
5174 contents + roff - 3);
5175 else if (!ABI_64_P (output_bfd) && val == 0x44)
5176 bfd_put_8 (output_bfd, 0x41,
5177 contents + roff - 3);
5178 bfd_put_8 (output_bfd, 0x81,
5179 contents + roff - 2);
5180 bfd_put_8 (output_bfd, 0xc0 | reg,
5181 contents + roff - 1);
5182 }
5183 else
5184 {
5185 /* addq/addl -> leaq/leal */
5186 if (val == 0x4c)
5187 bfd_put_8 (output_bfd, 0x4d,
5188 contents + roff - 3);
5189 else if (!ABI_64_P (output_bfd) && val == 0x44)
5190 bfd_put_8 (output_bfd, 0x45,
5191 contents + roff - 3);
5192 bfd_put_8 (output_bfd, 0x8d,
5193 contents + roff - 2);
5194 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
5195 contents + roff - 1);
5196 }
5197 bfd_put_32 (output_bfd,
5198 elf_x86_64_tpoff (info, relocation),
5199 contents + roff);
5200 continue;
5201 }
5202 else
5203 BFD_ASSERT (FALSE);
5204 }
5205
5206 if (htab->elf.sgot == NULL)
5207 abort ();
5208
5209 if (h != NULL)
5210 {
5211 off = h->got.offset;
5212 offplt = elf_x86_64_hash_entry (h)->tlsdesc_got;
5213 }
5214 else
5215 {
5216 if (local_got_offsets == NULL)
5217 abort ();
5218
5219 off = local_got_offsets[r_symndx];
5220 offplt = local_tlsdesc_gotents[r_symndx];
5221 }
5222
5223 if ((off & 1) != 0)
5224 off &= ~1;
5225 else
5226 {
5227 Elf_Internal_Rela outrel;
5228 int dr_type, indx;
5229 asection *sreloc;
5230
5231 if (htab->elf.srelgot == NULL)
5232 abort ();
5233
5234 indx = h && h->dynindx != -1 ? h->dynindx : 0;
5235
5236 if (GOT_TLS_GDESC_P (tls_type))
5237 {
5238 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
5239 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
5240 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
5241 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
5242 + htab->elf.sgotplt->output_offset
5243 + offplt
5244 + htab->sgotplt_jump_table_size);
5245 sreloc = htab->elf.srelplt;
5246 if (indx == 0)
5247 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
5248 else
5249 outrel.r_addend = 0;
5250 elf_append_rela (output_bfd, sreloc, &outrel);
5251 }
5252
5253 sreloc = htab->elf.srelgot;
5254
5255 outrel.r_offset = (htab->elf.sgot->output_section->vma
5256 + htab->elf.sgot->output_offset + off);
5257
5258 if (GOT_TLS_GD_P (tls_type))
5259 dr_type = R_X86_64_DTPMOD64;
5260 else if (GOT_TLS_GDESC_P (tls_type))
5261 goto dr_done;
5262 else
5263 dr_type = R_X86_64_TPOFF64;
5264
5265 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
5266 outrel.r_addend = 0;
5267 if ((dr_type == R_X86_64_TPOFF64
5268 || dr_type == R_X86_64_TLSDESC) && indx == 0)
5269 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
5270 outrel.r_info = htab->r_info (indx, dr_type);
5271
5272 elf_append_rela (output_bfd, sreloc, &outrel);
5273
5274 if (GOT_TLS_GD_P (tls_type))
5275 {
5276 if (indx == 0)
5277 {
5278 BFD_ASSERT (! unresolved_reloc);
5279 bfd_put_64 (output_bfd,
5280 relocation - elf_x86_64_dtpoff_base (info),
5281 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
5282 }
5283 else
5284 {
5285 bfd_put_64 (output_bfd, 0,
5286 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
5287 outrel.r_info = htab->r_info (indx,
5288 R_X86_64_DTPOFF64);
5289 outrel.r_offset += GOT_ENTRY_SIZE;
5290 elf_append_rela (output_bfd, sreloc,
5291 &outrel);
5292 }
5293 }
5294
5295 dr_done:
5296 if (h != NULL)
5297 h->got.offset |= 1;
5298 else
5299 local_got_offsets[r_symndx] |= 1;
5300 }
5301
5302 if (off >= (bfd_vma) -2
5303 && ! GOT_TLS_GDESC_P (tls_type))
5304 abort ();
5305 if (r_type == ELF32_R_TYPE (rel->r_info))
5306 {
5307 if (r_type == R_X86_64_GOTPC32_TLSDESC
5308 || r_type == R_X86_64_TLSDESC_CALL)
5309 relocation = htab->elf.sgotplt->output_section->vma
5310 + htab->elf.sgotplt->output_offset
5311 + offplt + htab->sgotplt_jump_table_size;
5312 else
5313 relocation = htab->elf.sgot->output_section->vma
5314 + htab->elf.sgot->output_offset + off;
5315 unresolved_reloc = FALSE;
5316 }
5317 else
5318 {
5319 bfd_vma roff = rel->r_offset;
5320
5321 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
5322 {
5323 /* GD->IE transition. For 64bit, change
5324 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5325 .word 0x6666; rex64; call __tls_get_addr@PLT
5326 or
5327 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
5328 .byte 0x66; rex64
5329 call *__tls_get_addr@GOTPCREL(%rip
5330 which may be converted to
5331 addr32 call __tls_get_addr
5332 into:
5333 movq %fs:0, %rax
5334 addq foo@gottpoff(%rip), %rax
5335 For 32bit, change
5336 leaq foo@tlsgd(%rip), %rdi
5337 .word 0x6666; rex64; call __tls_get_addr@PLT
5338 or
5339 leaq foo@tlsgd(%rip), %rdi
5340 .byte 0x66; rex64;
5341 call *__tls_get_addr@GOTPCREL(%rip)
5342 which may be converted to
5343 addr32 call __tls_get_addr
5344 into:
5345 movl %fs:0, %eax
5346 addq foo@gottpoff(%rip), %rax
5347 For largepic, change:
5348 leaq foo@tlsgd(%rip), %rdi
5349 movabsq $__tls_get_addr@pltoff, %rax
5350 addq %r15, %rax
5351 call *%rax
5352 into:
5353 movq %fs:0, %rax
5354 addq foo@gottpoff(%rax), %rax
5355 nopw 0x0(%rax,%rax,1) */
5356 int largepic = 0;
5357 if (ABI_64_P (output_bfd))
5358 {
5359 if (contents[roff + 5] == 0xb8)
5360 {
5361 memcpy (contents + roff - 3,
5362 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05"
5363 "\0\0\0\0\x66\x0f\x1f\x44\0", 22);
5364 largepic = 1;
5365 }
5366 else
5367 memcpy (contents + roff - 4,
5368 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5369 16);
5370 }
5371 else
5372 memcpy (contents + roff - 3,
5373 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
5374 15);
5375
5376 relocation = (htab->elf.sgot->output_section->vma
5377 + htab->elf.sgot->output_offset + off
5378 - roff
5379 - largepic
5380 - input_section->output_section->vma
5381 - input_section->output_offset
5382 - 12);
5383 bfd_put_32 (output_bfd, relocation,
5384 contents + roff + 8 + largepic);
5385 /* Skip R_X86_64_PLT32/R_X86_64_PLTOFF64. */
5386 rel++;
5387 wrel++;
5388 continue;
5389 }
5390 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
5391 {
5392 /* GDesc -> IE transition.
5393 It's originally something like:
5394 leaq x@tlsdesc(%rip), %rax
5395
5396 Change it to:
5397 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
5398
5399 /* Now modify the instruction as appropriate. To
5400 turn a leaq into a movq in the form we use it, it
5401 suffices to change the second byte from 0x8d to
5402 0x8b. */
5403 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
5404
5405 bfd_put_32 (output_bfd,
5406 htab->elf.sgot->output_section->vma
5407 + htab->elf.sgot->output_offset + off
5408 - rel->r_offset
5409 - input_section->output_section->vma
5410 - input_section->output_offset
5411 - 4,
5412 contents + roff);
5413 continue;
5414 }
5415 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
5416 {
5417 /* GDesc -> IE transition.
5418 It's originally:
5419 call *(%rax)
5420
5421 Change it to:
5422 xchg %ax, %ax. */
5423
5424 bfd_put_8 (output_bfd, 0x66, contents + roff);
5425 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
5426 continue;
5427 }
5428 else
5429 BFD_ASSERT (FALSE);
5430 }
5431 break;
5432
5433 case R_X86_64_TLSLD:
5434 if (! elf_x86_64_tls_transition (info, input_bfd,
5435 input_section, contents,
5436 symtab_hdr, sym_hashes,
5437 &r_type, GOT_UNKNOWN, rel,
5438 relend, h, r_symndx, TRUE))
5439 return FALSE;
5440
5441 if (r_type != R_X86_64_TLSLD)
5442 {
5443 /* LD->LE transition:
5444 leaq foo@tlsld(%rip), %rdi
5445 call __tls_get_addr@PLT
5446 For 64bit, we change it into:
5447 .word 0x6666; .byte 0x66; movq %fs:0, %rax
5448 For 32bit, we change it into:
5449 nopl 0x0(%rax); movl %fs:0, %eax
5450 Or
5451 leaq foo@tlsld(%rip), %rdi;
5452 call *__tls_get_addr@GOTPCREL(%rip)
5453 which may be converted to
5454 addr32 call __tls_get_addr
5455 For 64bit, we change it into:
5456 .word 0x6666; .word 0x6666; movq %fs:0, %rax
5457 For 32bit, we change it into:
5458 nopw 0x0(%rax); movl %fs:0, %eax
5459 For largepic, change:
5460 leaq foo@tlsgd(%rip), %rdi
5461 movabsq $__tls_get_addr@pltoff, %rax
5462 addq %rbx, %rax
5463 call *%rax
5464 into
5465 data16 data16 data16 nopw %cs:0x0(%rax,%rax,1)
5466 movq %fs:0, %eax */
5467
5468 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
5469 if (ABI_64_P (output_bfd))
5470 {
5471 if (contents[rel->r_offset + 5] == 0xb8)
5472 memcpy (contents + rel->r_offset - 3,
5473 "\x66\x66\x66\x66\x2e\x0f\x1f\x84\0\0\0\0\0"
5474 "\x64\x48\x8b\x04\x25\0\0\0", 22);
5475 else if (contents[rel->r_offset + 4] == 0xff
5476 || contents[rel->r_offset + 4] == 0x67)
5477 memcpy (contents + rel->r_offset - 3,
5478 "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0",
5479 13);
5480 else
5481 memcpy (contents + rel->r_offset - 3,
5482 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
5483 }
5484 else
5485 {
5486 if (contents[rel->r_offset + 4] == 0xff)
5487 memcpy (contents + rel->r_offset - 3,
5488 "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0",
5489 13);
5490 else
5491 memcpy (contents + rel->r_offset - 3,
5492 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
5493 }
5494 /* Skip R_X86_64_PC32, R_X86_64_PLT32, R_X86_64_GOTPCRELX
5495 and R_X86_64_PLTOFF64. */
5496 rel++;
5497 wrel++;
5498 continue;
5499 }
5500
5501 if (htab->elf.sgot == NULL)
5502 abort ();
5503
5504 off = htab->tls_ld_got.offset;
5505 if (off & 1)
5506 off &= ~1;
5507 else
5508 {
5509 Elf_Internal_Rela outrel;
5510
5511 if (htab->elf.srelgot == NULL)
5512 abort ();
5513
5514 outrel.r_offset = (htab->elf.sgot->output_section->vma
5515 + htab->elf.sgot->output_offset + off);
5516
5517 bfd_put_64 (output_bfd, 0,
5518 htab->elf.sgot->contents + off);
5519 bfd_put_64 (output_bfd, 0,
5520 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
5521 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
5522 outrel.r_addend = 0;
5523 elf_append_rela (output_bfd, htab->elf.srelgot,
5524 &outrel);
5525 htab->tls_ld_got.offset |= 1;
5526 }
5527 relocation = htab->elf.sgot->output_section->vma
5528 + htab->elf.sgot->output_offset + off;
5529 unresolved_reloc = FALSE;
5530 break;
5531
5532 case R_X86_64_DTPOFF32:
5533 if (!bfd_link_executable (info)
5534 || (input_section->flags & SEC_CODE) == 0)
5535 relocation -= elf_x86_64_dtpoff_base (info);
5536 else
5537 relocation = elf_x86_64_tpoff (info, relocation);
5538 break;
5539
5540 case R_X86_64_TPOFF32:
5541 case R_X86_64_TPOFF64:
5542 BFD_ASSERT (bfd_link_executable (info));
5543 relocation = elf_x86_64_tpoff (info, relocation);
5544 break;
5545
5546 case R_X86_64_DTPOFF64:
5547 BFD_ASSERT ((input_section->flags & SEC_CODE) == 0);
5548 relocation -= elf_x86_64_dtpoff_base (info);
5549 break;
5550
5551 default:
5552 break;
5553 }
5554
5555 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
5556 because such sections are not SEC_ALLOC and thus ld.so will
5557 not process them. */
5558 if (unresolved_reloc
5559 && !((input_section->flags & SEC_DEBUGGING) != 0
5560 && h->def_dynamic)
5561 && _bfd_elf_section_offset (output_bfd, info, input_section,
5562 rel->r_offset) != (bfd_vma) -1)
5563 {
5564 _bfd_error_handler
5565 /* xgettext:c-format */
5566 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
5567 input_bfd,
5568 input_section,
5569 (long) rel->r_offset,
5570 howto->name,
5571 h->root.root.string);
5572 return FALSE;
5573 }
5574
5575 do_relocation:
5576 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5577 contents, rel->r_offset,
5578 relocation, rel->r_addend);
5579
5580 check_relocation_error:
5581 if (r != bfd_reloc_ok)
5582 {
5583 const char *name;
5584
5585 if (h != NULL)
5586 name = h->root.root.string;
5587 else
5588 {
5589 name = bfd_elf_string_from_elf_section (input_bfd,
5590 symtab_hdr->sh_link,
5591 sym->st_name);
5592 if (name == NULL)
5593 return FALSE;
5594 if (*name == '\0')
5595 name = bfd_section_name (input_bfd, sec);
5596 }
5597
5598 if (r == bfd_reloc_overflow)
5599 (*info->callbacks->reloc_overflow)
5600 (info, (h ? &h->root : NULL), name, howto->name,
5601 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5602 else
5603 {
5604 _bfd_error_handler
5605 /* xgettext:c-format */
5606 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
5607 input_bfd, input_section,
5608 (long) rel->r_offset, name, (int) r);
5609 return FALSE;
5610 }
5611 }
5612
5613 if (wrel != rel)
5614 *wrel = *rel;
5615 }
5616
5617 if (wrel != rel)
5618 {
5619 Elf_Internal_Shdr *rel_hdr;
5620 size_t deleted = rel - wrel;
5621
5622 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
5623 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5624 if (rel_hdr->sh_size == 0)
5625 {
5626 /* It is too late to remove an empty reloc section. Leave
5627 one NONE reloc.
5628 ??? What is wrong with an empty section??? */
5629 rel_hdr->sh_size = rel_hdr->sh_entsize;
5630 deleted -= 1;
5631 }
5632 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
5633 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
5634 input_section->reloc_count -= deleted;
5635 }
5636
5637 return TRUE;
5638 }
5639
5640 /* Finish up dynamic symbol handling. We set the contents of various
5641 dynamic sections here. */
5642
5643 static bfd_boolean
5644 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
5645 struct bfd_link_info *info,
5646 struct elf_link_hash_entry *h,
5647 Elf_Internal_Sym *sym)
5648 {
5649 struct elf_x86_64_link_hash_table *htab;
5650 const struct elf_x86_64_backend_data *abed;
5651 bfd_boolean use_plt_bnd;
5652 struct elf_x86_64_link_hash_entry *eh;
5653 bfd_boolean local_undefweak;
5654
5655 htab = elf_x86_64_hash_table (info);
5656 if (htab == NULL)
5657 return FALSE;
5658
5659 /* Use MPX backend data in case of BND relocation. Use .plt_bnd
5660 section only if there is .plt section. */
5661 use_plt_bnd = htab->elf.splt != NULL && htab->plt_bnd != NULL;
5662 abed = (use_plt_bnd
5663 ? &elf_x86_64_bnd_arch_bed
5664 : get_elf_x86_64_backend_data (output_bfd));
5665
5666 eh = (struct elf_x86_64_link_hash_entry *) h;
5667
5668 /* We keep PLT/GOT entries without dynamic PLT/GOT relocations for
5669 resolved undefined weak symbols in executable so that their
5670 references have value 0 at run-time. */
5671 local_undefweak = UNDEFINED_WEAK_RESOLVED_TO_ZERO (info,
5672 eh->has_got_reloc,
5673 eh);
5674
5675 if (h->plt.offset != (bfd_vma) -1)
5676 {
5677 bfd_vma plt_index;
5678 bfd_vma got_offset, plt_offset, plt_plt_offset, plt_got_offset;
5679 bfd_vma plt_plt_insn_end, plt_got_insn_size;
5680 Elf_Internal_Rela rela;
5681 bfd_byte *loc;
5682 asection *plt, *gotplt, *relplt, *resolved_plt;
5683 const struct elf_backend_data *bed;
5684 bfd_vma plt_got_pcrel_offset;
5685
5686 /* When building a static executable, use .iplt, .igot.plt and
5687 .rela.iplt sections for STT_GNU_IFUNC symbols. */
5688 if (htab->elf.splt != NULL)
5689 {
5690 plt = htab->elf.splt;
5691 gotplt = htab->elf.sgotplt;
5692 relplt = htab->elf.srelplt;
5693 }
5694 else
5695 {
5696 plt = htab->elf.iplt;
5697 gotplt = htab->elf.igotplt;
5698 relplt = htab->elf.irelplt;
5699 }
5700
5701 /* This symbol has an entry in the procedure linkage table. Set
5702 it up. */
5703 if ((h->dynindx == -1
5704 && !local_undefweak
5705 && !((h->forced_local || bfd_link_executable (info))
5706 && h->def_regular
5707 && h->type == STT_GNU_IFUNC))
5708 || plt == NULL
5709 || gotplt == NULL
5710 || relplt == NULL)
5711 abort ();
5712
5713 /* Get the index in the procedure linkage table which
5714 corresponds to this symbol. This is the index of this symbol
5715 in all the symbols for which we are making plt entries. The
5716 first entry in the procedure linkage table is reserved.
5717
5718 Get the offset into the .got table of the entry that
5719 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
5720 bytes. The first three are reserved for the dynamic linker.
5721
5722 For static executables, we don't reserve anything. */
5723
5724 if (plt == htab->elf.splt)
5725 {
5726 got_offset = h->plt.offset / abed->plt_entry_size - 1;
5727 got_offset = (got_offset + 3) * GOT_ENTRY_SIZE;
5728 }
5729 else
5730 {
5731 got_offset = h->plt.offset / abed->plt_entry_size;
5732 got_offset = got_offset * GOT_ENTRY_SIZE;
5733 }
5734
5735 plt_plt_insn_end = abed->plt_plt_insn_end;
5736 plt_plt_offset = abed->plt_plt_offset;
5737 plt_got_insn_size = abed->plt_got_insn_size;
5738 plt_got_offset = abed->plt_got_offset;
5739 if (use_plt_bnd)
5740 {
5741 /* Use the second PLT with BND relocations. */
5742 const bfd_byte *plt_entry, *plt2_entry;
5743
5744 if (eh->has_bnd_reloc)
5745 {
5746 plt_entry = elf_x86_64_bnd_plt_entry;
5747 plt2_entry = elf_x86_64_bnd_plt2_entry;
5748 }
5749 else
5750 {
5751 plt_entry = elf_x86_64_legacy_plt_entry;
5752 plt2_entry = elf_x86_64_legacy_plt2_entry;
5753
5754 /* Subtract 1 since there is no BND prefix. */
5755 plt_plt_insn_end -= 1;
5756 plt_plt_offset -= 1;
5757 plt_got_insn_size -= 1;
5758 plt_got_offset -= 1;
5759 }
5760
5761 BFD_ASSERT (sizeof (elf_x86_64_bnd_plt_entry)
5762 == sizeof (elf_x86_64_legacy_plt_entry));
5763
5764 /* Fill in the entry in the procedure linkage table. */
5765 memcpy (plt->contents + h->plt.offset,
5766 plt_entry, sizeof (elf_x86_64_legacy_plt_entry));
5767 /* Fill in the entry in the second PLT. */
5768 memcpy (htab->plt_bnd->contents + eh->plt_bnd.offset,
5769 plt2_entry, sizeof (elf_x86_64_legacy_plt2_entry));
5770
5771 resolved_plt = htab->plt_bnd;
5772 plt_offset = eh->plt_bnd.offset;
5773 }
5774 else
5775 {
5776 /* Fill in the entry in the procedure linkage table. */
5777 memcpy (plt->contents + h->plt.offset, abed->plt_entry,
5778 abed->plt_entry_size);
5779
5780 resolved_plt = plt;
5781 plt_offset = h->plt.offset;
5782 }
5783
5784 /* Insert the relocation positions of the plt section. */
5785
5786 /* Put offset the PC-relative instruction referring to the GOT entry,
5787 subtracting the size of that instruction. */
5788 plt_got_pcrel_offset = (gotplt->output_section->vma
5789 + gotplt->output_offset
5790 + got_offset
5791 - resolved_plt->output_section->vma
5792 - resolved_plt->output_offset
5793 - plt_offset
5794 - plt_got_insn_size);
5795
5796 /* Check PC-relative offset overflow in PLT entry. */
5797 if ((plt_got_pcrel_offset + 0x80000000) > 0xffffffff)
5798 /* xgettext:c-format */
5799 info->callbacks->einfo (_("%F%B: PC-relative offset overflow in PLT entry for `%s'\n"),
5800 output_bfd, h->root.root.string);
5801
5802 bfd_put_32 (output_bfd, plt_got_pcrel_offset,
5803 resolved_plt->contents + plt_offset + plt_got_offset);
5804
5805 /* Fill in the entry in the global offset table, initially this
5806 points to the second part of the PLT entry. Leave the entry
5807 as zero for undefined weak symbol in PIE. No PLT relocation
5808 against undefined weak symbol in PIE. */
5809 if (!local_undefweak)
5810 {
5811 bfd_put_64 (output_bfd, (plt->output_section->vma
5812 + plt->output_offset
5813 + h->plt.offset
5814 + abed->plt_lazy_offset),
5815 gotplt->contents + got_offset);
5816
5817 /* Fill in the entry in the .rela.plt section. */
5818 rela.r_offset = (gotplt->output_section->vma
5819 + gotplt->output_offset
5820 + got_offset);
5821 if (h->dynindx == -1
5822 || ((bfd_link_executable (info)
5823 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
5824 && h->def_regular
5825 && h->type == STT_GNU_IFUNC))
5826 {
5827 /* If an STT_GNU_IFUNC symbol is locally defined, generate
5828 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
5829 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
5830 rela.r_addend = (h->root.u.def.value
5831 + h->root.u.def.section->output_section->vma
5832 + h->root.u.def.section->output_offset);
5833 /* R_X86_64_IRELATIVE comes last. */
5834 plt_index = htab->next_irelative_index--;
5835 }
5836 else
5837 {
5838 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
5839 rela.r_addend = 0;
5840 plt_index = htab->next_jump_slot_index++;
5841 }
5842
5843 /* Don't fill PLT entry for static executables. */
5844 if (plt == htab->elf.splt)
5845 {
5846 bfd_vma plt0_offset = h->plt.offset + plt_plt_insn_end;
5847
5848 /* Put relocation index. */
5849 bfd_put_32 (output_bfd, plt_index,
5850 (plt->contents + h->plt.offset
5851 + abed->plt_reloc_offset));
5852
5853 /* Put offset for jmp .PLT0 and check for overflow. We don't
5854 check relocation index for overflow since branch displacement
5855 will overflow first. */
5856 if (plt0_offset > 0x80000000)
5857 /* xgettext:c-format */
5858 info->callbacks->einfo (_("%F%B: branch displacement overflow in PLT entry for `%s'\n"),
5859 output_bfd, h->root.root.string);
5860 bfd_put_32 (output_bfd, - plt0_offset,
5861 plt->contents + h->plt.offset + plt_plt_offset);
5862 }
5863
5864 bed = get_elf_backend_data (output_bfd);
5865 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
5866 bed->s->swap_reloca_out (output_bfd, &rela, loc);
5867 }
5868 }
5869 else if (eh->plt_got.offset != (bfd_vma) -1)
5870 {
5871 bfd_vma got_offset, plt_offset, plt_got_offset, plt_got_insn_size;
5872 asection *plt, *got;
5873 bfd_boolean got_after_plt;
5874 int32_t got_pcrel_offset;
5875 const bfd_byte *got_plt_entry;
5876
5877 /* Set the entry in the GOT procedure linkage table. */
5878 plt = htab->plt_got;
5879 got = htab->elf.sgot;
5880 got_offset = h->got.offset;
5881
5882 if (got_offset == (bfd_vma) -1
5883 || h->type == STT_GNU_IFUNC
5884 || plt == NULL
5885 || got == NULL)
5886 abort ();
5887
5888 /* Use the second PLT entry template for the GOT PLT since they
5889 are the identical. */
5890 plt_got_insn_size = elf_x86_64_bnd_arch_bed.plt_got_insn_size;
5891 plt_got_offset = elf_x86_64_bnd_arch_bed.plt_got_offset;
5892 if (eh->has_bnd_reloc)
5893 got_plt_entry = elf_x86_64_bnd_plt2_entry;
5894 else
5895 {
5896 got_plt_entry = elf_x86_64_legacy_plt2_entry;
5897
5898 /* Subtract 1 since there is no BND prefix. */
5899 plt_got_insn_size -= 1;
5900 plt_got_offset -= 1;
5901 }
5902
5903 /* Fill in the entry in the GOT procedure linkage table. */
5904 plt_offset = eh->plt_got.offset;
5905 memcpy (plt->contents + plt_offset,
5906 got_plt_entry, sizeof (elf_x86_64_legacy_plt2_entry));
5907
5908 /* Put offset the PC-relative instruction referring to the GOT
5909 entry, subtracting the size of that instruction. */
5910 got_pcrel_offset = (got->output_section->vma
5911 + got->output_offset
5912 + got_offset
5913 - plt->output_section->vma
5914 - plt->output_offset
5915 - plt_offset
5916 - plt_got_insn_size);
5917
5918 /* Check PC-relative offset overflow in GOT PLT entry. */
5919 got_after_plt = got->output_section->vma > plt->output_section->vma;
5920 if ((got_after_plt && got_pcrel_offset < 0)
5921 || (!got_after_plt && got_pcrel_offset > 0))
5922 /* xgettext:c-format */
5923 info->callbacks->einfo (_("%F%B: PC-relative offset overflow in GOT PLT entry for `%s'\n"),
5924 output_bfd, h->root.root.string);
5925
5926 bfd_put_32 (output_bfd, got_pcrel_offset,
5927 plt->contents + plt_offset + plt_got_offset);
5928 }
5929
5930 if (!local_undefweak
5931 && !h->def_regular
5932 && (h->plt.offset != (bfd_vma) -1
5933 || eh->plt_got.offset != (bfd_vma) -1))
5934 {
5935 /* Mark the symbol as undefined, rather than as defined in
5936 the .plt section. Leave the value if there were any
5937 relocations where pointer equality matters (this is a clue
5938 for the dynamic linker, to make function pointer
5939 comparisons work between an application and shared
5940 library), otherwise set it to zero. If a function is only
5941 called from a binary, there is no need to slow down
5942 shared libraries because of that. */
5943 sym->st_shndx = SHN_UNDEF;
5944 if (!h->pointer_equality_needed)
5945 sym->st_value = 0;
5946 }
5947
5948 /* Don't generate dynamic GOT relocation against undefined weak
5949 symbol in executable. */
5950 if (h->got.offset != (bfd_vma) -1
5951 && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h)->tls_type)
5952 && elf_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE
5953 && !local_undefweak)
5954 {
5955 Elf_Internal_Rela rela;
5956 asection *relgot = htab->elf.srelgot;
5957
5958 /* This symbol has an entry in the global offset table. Set it
5959 up. */
5960 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
5961 abort ();
5962
5963 rela.r_offset = (htab->elf.sgot->output_section->vma
5964 + htab->elf.sgot->output_offset
5965 + (h->got.offset &~ (bfd_vma) 1));
5966
5967 /* If this is a static link, or it is a -Bsymbolic link and the
5968 symbol is defined locally or was forced to be local because
5969 of a version file, we just want to emit a RELATIVE reloc.
5970 The entry in the global offset table will already have been
5971 initialized in the relocate_section function. */
5972 if (h->def_regular
5973 && h->type == STT_GNU_IFUNC)
5974 {
5975 if (h->plt.offset == (bfd_vma) -1)
5976 {
5977 /* STT_GNU_IFUNC is referenced without PLT. */
5978 if (htab->elf.splt == NULL)
5979 {
5980 /* use .rel[a].iplt section to store .got relocations
5981 in static executable. */
5982 relgot = htab->elf.irelplt;
5983 }
5984 if (SYMBOL_REFERENCES_LOCAL (info, h))
5985 {
5986 rela.r_info = htab->r_info (0,
5987 R_X86_64_IRELATIVE);
5988 rela.r_addend = (h->root.u.def.value
5989 + h->root.u.def.section->output_section->vma
5990 + h->root.u.def.section->output_offset);
5991 }
5992 else
5993 goto do_glob_dat;
5994 }
5995 else if (bfd_link_pic (info))
5996 {
5997 /* Generate R_X86_64_GLOB_DAT. */
5998 goto do_glob_dat;
5999 }
6000 else
6001 {
6002 asection *plt;
6003
6004 if (!h->pointer_equality_needed)
6005 abort ();
6006
6007 /* For non-shared object, we can't use .got.plt, which
6008 contains the real function addres if we need pointer
6009 equality. We load the GOT entry with the PLT entry. */
6010 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
6011 bfd_put_64 (output_bfd, (plt->output_section->vma
6012 + plt->output_offset
6013 + h->plt.offset),
6014 htab->elf.sgot->contents + h->got.offset);
6015 return TRUE;
6016 }
6017 }
6018 else if (bfd_link_pic (info)
6019 && SYMBOL_REFERENCES_LOCAL (info, h))
6020 {
6021 if (!h->def_regular)
6022 return FALSE;
6023 BFD_ASSERT((h->got.offset & 1) != 0);
6024 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
6025 rela.r_addend = (h->root.u.def.value
6026 + h->root.u.def.section->output_section->vma
6027 + h->root.u.def.section->output_offset);
6028 }
6029 else
6030 {
6031 BFD_ASSERT((h->got.offset & 1) == 0);
6032 do_glob_dat:
6033 bfd_put_64 (output_bfd, (bfd_vma) 0,
6034 htab->elf.sgot->contents + h->got.offset);
6035 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
6036 rela.r_addend = 0;
6037 }
6038
6039 elf_append_rela (output_bfd, relgot, &rela);
6040 }
6041
6042 if (h->needs_copy)
6043 {
6044 Elf_Internal_Rela rela;
6045
6046 /* This symbol needs a copy reloc. Set it up. */
6047
6048 if (h->dynindx == -1
6049 || (h->root.type != bfd_link_hash_defined
6050 && h->root.type != bfd_link_hash_defweak)
6051 || htab->srelbss == NULL)
6052 abort ();
6053
6054 rela.r_offset = (h->root.u.def.value
6055 + h->root.u.def.section->output_section->vma
6056 + h->root.u.def.section->output_offset);
6057 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
6058 rela.r_addend = 0;
6059 elf_append_rela (output_bfd, htab->srelbss, &rela);
6060 }
6061
6062 return TRUE;
6063 }
6064
6065 /* Finish up local dynamic symbol handling. We set the contents of
6066 various dynamic sections here. */
6067
6068 static bfd_boolean
6069 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
6070 {
6071 struct elf_link_hash_entry *h
6072 = (struct elf_link_hash_entry *) *slot;
6073 struct bfd_link_info *info
6074 = (struct bfd_link_info *) inf;
6075
6076 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
6077 info, h, NULL);
6078 }
6079
6080 /* Finish up undefined weak symbol handling in PIE. Fill its PLT entry
6081 here since undefined weak symbol may not be dynamic and may not be
6082 called for elf_x86_64_finish_dynamic_symbol. */
6083
6084 static bfd_boolean
6085 elf_x86_64_pie_finish_undefweak_symbol (struct bfd_hash_entry *bh,
6086 void *inf)
6087 {
6088 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh;
6089 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6090
6091 if (h->root.type != bfd_link_hash_undefweak
6092 || h->dynindx != -1)
6093 return TRUE;
6094
6095 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
6096 info, h, NULL);
6097 }
6098
6099 /* Used to decide how to sort relocs in an optimal manner for the
6100 dynamic linker, before writing them out. */
6101
6102 static enum elf_reloc_type_class
6103 elf_x86_64_reloc_type_class (const struct bfd_link_info *info,
6104 const asection *rel_sec ATTRIBUTE_UNUSED,
6105 const Elf_Internal_Rela *rela)
6106 {
6107 bfd *abfd = info->output_bfd;
6108 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
6109 struct elf_x86_64_link_hash_table *htab = elf_x86_64_hash_table (info);
6110
6111 if (htab->elf.dynsym != NULL
6112 && htab->elf.dynsym->contents != NULL)
6113 {
6114 /* Check relocation against STT_GNU_IFUNC symbol if there are
6115 dynamic symbols. */
6116 unsigned long r_symndx = htab->r_sym (rela->r_info);
6117 if (r_symndx != STN_UNDEF)
6118 {
6119 Elf_Internal_Sym sym;
6120 if (!bed->s->swap_symbol_in (abfd,
6121 (htab->elf.dynsym->contents
6122 + r_symndx * bed->s->sizeof_sym),
6123 0, &sym))
6124 abort ();
6125
6126 if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
6127 return reloc_class_ifunc;
6128 }
6129 }
6130
6131 switch ((int) ELF32_R_TYPE (rela->r_info))
6132 {
6133 case R_X86_64_IRELATIVE:
6134 return reloc_class_ifunc;
6135 case R_X86_64_RELATIVE:
6136 case R_X86_64_RELATIVE64:
6137 return reloc_class_relative;
6138 case R_X86_64_JUMP_SLOT:
6139 return reloc_class_plt;
6140 case R_X86_64_COPY:
6141 return reloc_class_copy;
6142 default:
6143 return reloc_class_normal;
6144 }
6145 }
6146
6147 /* Finish up the dynamic sections. */
6148
6149 static bfd_boolean
6150 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
6151 struct bfd_link_info *info)
6152 {
6153 struct elf_x86_64_link_hash_table *htab;
6154 bfd *dynobj;
6155 asection *sdyn;
6156 const struct elf_x86_64_backend_data *abed;
6157
6158 htab = elf_x86_64_hash_table (info);
6159 if (htab == NULL)
6160 return FALSE;
6161
6162 /* Use MPX backend data in case of BND relocation. Use .plt_bnd
6163 section only if there is .plt section. */
6164 abed = (htab->elf.splt != NULL && htab->plt_bnd != NULL
6165 ? &elf_x86_64_bnd_arch_bed
6166 : get_elf_x86_64_backend_data (output_bfd));
6167
6168 dynobj = htab->elf.dynobj;
6169 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
6170
6171 if (htab->elf.dynamic_sections_created)
6172 {
6173 bfd_byte *dyncon, *dynconend;
6174 const struct elf_backend_data *bed;
6175 bfd_size_type sizeof_dyn;
6176
6177 if (sdyn == NULL || htab->elf.sgot == NULL)
6178 abort ();
6179
6180 bed = get_elf_backend_data (dynobj);
6181 sizeof_dyn = bed->s->sizeof_dyn;
6182 dyncon = sdyn->contents;
6183 dynconend = sdyn->contents + sdyn->size;
6184 for (; dyncon < dynconend; dyncon += sizeof_dyn)
6185 {
6186 Elf_Internal_Dyn dyn;
6187 asection *s;
6188
6189 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
6190
6191 switch (dyn.d_tag)
6192 {
6193 default:
6194 continue;
6195
6196 case DT_PLTGOT:
6197 s = htab->elf.sgotplt;
6198 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6199 break;
6200
6201 case DT_JMPREL:
6202 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
6203 break;
6204
6205 case DT_PLTRELSZ:
6206 s = htab->elf.srelplt->output_section;
6207 dyn.d_un.d_val = s->size;
6208 break;
6209
6210 case DT_RELASZ:
6211 /* The procedure linkage table relocs (DT_JMPREL) should
6212 not be included in the overall relocs (DT_RELA).
6213 Therefore, we override the DT_RELASZ entry here to
6214 make it not include the JMPREL relocs. Since the
6215 linker script arranges for .rela.plt to follow all
6216 other relocation sections, we don't have to worry
6217 about changing the DT_RELA entry. */
6218 if (htab->elf.srelplt != NULL)
6219 {
6220 s = htab->elf.srelplt->output_section;
6221 dyn.d_un.d_val -= s->size;
6222 }
6223 break;
6224
6225 case DT_TLSDESC_PLT:
6226 s = htab->elf.splt;
6227 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
6228 + htab->tlsdesc_plt;
6229 break;
6230
6231 case DT_TLSDESC_GOT:
6232 s = htab->elf.sgot;
6233 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
6234 + htab->tlsdesc_got;
6235 break;
6236 }
6237
6238 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
6239 }
6240
6241 /* Fill in the special first entry in the procedure linkage table. */
6242 if (htab->elf.splt && htab->elf.splt->size > 0)
6243 {
6244 /* Fill in the first entry in the procedure linkage table. */
6245 memcpy (htab->elf.splt->contents,
6246 abed->plt0_entry, abed->plt_entry_size);
6247 /* Add offset for pushq GOT+8(%rip), since the instruction
6248 uses 6 bytes subtract this value. */
6249 bfd_put_32 (output_bfd,
6250 (htab->elf.sgotplt->output_section->vma
6251 + htab->elf.sgotplt->output_offset
6252 + 8
6253 - htab->elf.splt->output_section->vma
6254 - htab->elf.splt->output_offset
6255 - 6),
6256 htab->elf.splt->contents + abed->plt0_got1_offset);
6257 /* Add offset for the PC-relative instruction accessing GOT+16,
6258 subtracting the offset to the end of that instruction. */
6259 bfd_put_32 (output_bfd,
6260 (htab->elf.sgotplt->output_section->vma
6261 + htab->elf.sgotplt->output_offset
6262 + 16
6263 - htab->elf.splt->output_section->vma
6264 - htab->elf.splt->output_offset
6265 - abed->plt0_got2_insn_end),
6266 htab->elf.splt->contents + abed->plt0_got2_offset);
6267
6268 elf_section_data (htab->elf.splt->output_section)
6269 ->this_hdr.sh_entsize = abed->plt_entry_size;
6270
6271 if (htab->tlsdesc_plt)
6272 {
6273 bfd_put_64 (output_bfd, (bfd_vma) 0,
6274 htab->elf.sgot->contents + htab->tlsdesc_got);
6275
6276 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
6277 abed->plt0_entry, abed->plt_entry_size);
6278
6279 /* Add offset for pushq GOT+8(%rip), since the
6280 instruction uses 6 bytes subtract this value. */
6281 bfd_put_32 (output_bfd,
6282 (htab->elf.sgotplt->output_section->vma
6283 + htab->elf.sgotplt->output_offset
6284 + 8
6285 - htab->elf.splt->output_section->vma
6286 - htab->elf.splt->output_offset
6287 - htab->tlsdesc_plt
6288 - 6),
6289 htab->elf.splt->contents
6290 + htab->tlsdesc_plt + abed->plt0_got1_offset);
6291 /* Add offset for the PC-relative instruction accessing GOT+TDG,
6292 where TGD stands for htab->tlsdesc_got, subtracting the offset
6293 to the end of that instruction. */
6294 bfd_put_32 (output_bfd,
6295 (htab->elf.sgot->output_section->vma
6296 + htab->elf.sgot->output_offset
6297 + htab->tlsdesc_got
6298 - htab->elf.splt->output_section->vma
6299 - htab->elf.splt->output_offset
6300 - htab->tlsdesc_plt
6301 - abed->plt0_got2_insn_end),
6302 htab->elf.splt->contents
6303 + htab->tlsdesc_plt + abed->plt0_got2_offset);
6304 }
6305 }
6306 }
6307
6308 if (htab->plt_bnd != NULL)
6309 elf_section_data (htab->plt_bnd->output_section)
6310 ->this_hdr.sh_entsize = sizeof (elf_x86_64_bnd_plt2_entry);
6311
6312 if (htab->elf.sgotplt)
6313 {
6314 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
6315 {
6316 _bfd_error_handler
6317 (_("discarded output section: `%A'"), htab->elf.sgotplt);
6318 return FALSE;
6319 }
6320
6321 /* Fill in the first three entries in the global offset table. */
6322 if (htab->elf.sgotplt->size > 0)
6323 {
6324 /* Set the first entry in the global offset table to the address of
6325 the dynamic section. */
6326 if (sdyn == NULL)
6327 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
6328 else
6329 bfd_put_64 (output_bfd,
6330 sdyn->output_section->vma + sdyn->output_offset,
6331 htab->elf.sgotplt->contents);
6332 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
6333 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
6334 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
6335 }
6336
6337 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
6338 GOT_ENTRY_SIZE;
6339 }
6340
6341 /* Adjust .eh_frame for .plt section. */
6342 if (htab->plt_eh_frame != NULL
6343 && htab->plt_eh_frame->contents != NULL)
6344 {
6345 if (htab->elf.splt != NULL
6346 && htab->elf.splt->size != 0
6347 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0
6348 && htab->elf.splt->output_section != NULL
6349 && htab->plt_eh_frame->output_section != NULL)
6350 {
6351 bfd_vma plt_start = htab->elf.splt->output_section->vma;
6352 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma
6353 + htab->plt_eh_frame->output_offset
6354 + PLT_FDE_START_OFFSET;
6355 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start,
6356 htab->plt_eh_frame->contents
6357 + PLT_FDE_START_OFFSET);
6358 }
6359 if (htab->plt_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
6360 {
6361 if (! _bfd_elf_write_section_eh_frame (output_bfd, info,
6362 htab->plt_eh_frame,
6363 htab->plt_eh_frame->contents))
6364 return FALSE;
6365 }
6366 }
6367
6368 if (htab->elf.sgot && htab->elf.sgot->size > 0)
6369 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
6370 = GOT_ENTRY_SIZE;
6371
6372 /* Fill PLT entries for undefined weak symbols in PIE. */
6373 if (bfd_link_pie (info))
6374 bfd_hash_traverse (&info->hash->table,
6375 elf_x86_64_pie_finish_undefweak_symbol,
6376 info);
6377
6378 return TRUE;
6379 }
6380
6381 /* Fill PLT/GOT entries and allocate dynamic relocations for local
6382 STT_GNU_IFUNC symbols, which aren't in the ELF linker hash table.
6383 It has to be done before elf_link_sort_relocs is called so that
6384 dynamic relocations are properly sorted. */
6385
6386 static bfd_boolean
6387 elf_x86_64_output_arch_local_syms
6388 (bfd *output_bfd ATTRIBUTE_UNUSED,
6389 struct bfd_link_info *info,
6390 void *flaginfo ATTRIBUTE_UNUSED,
6391 int (*func) (void *, const char *,
6392 Elf_Internal_Sym *,
6393 asection *,
6394 struct elf_link_hash_entry *) ATTRIBUTE_UNUSED)
6395 {
6396 struct elf_x86_64_link_hash_table *htab = elf_x86_64_hash_table (info);
6397 if (htab == NULL)
6398 return FALSE;
6399
6400 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
6401 htab_traverse (htab->loc_hash_table,
6402 elf_x86_64_finish_local_dynamic_symbol,
6403 info);
6404
6405 return TRUE;
6406 }
6407
6408 /* Return an array of PLT entry symbol values. */
6409
6410 static bfd_vma *
6411 elf_x86_64_get_plt_sym_val (bfd *abfd, asymbol **dynsyms, asection *plt,
6412 asection *relplt)
6413 {
6414 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
6415 arelent *p;
6416 long count, i;
6417 bfd_vma *plt_sym_val;
6418 bfd_vma plt_offset;
6419 bfd_byte *plt_contents;
6420 const struct elf_x86_64_backend_data *bed;
6421 Elf_Internal_Shdr *hdr;
6422 asection *plt_bnd;
6423
6424 /* Get the .plt section contents. PLT passed down may point to the
6425 .plt.bnd section. Make sure that PLT always points to the .plt
6426 section. */
6427 plt_bnd = bfd_get_section_by_name (abfd, ".plt.bnd");
6428 if (plt_bnd)
6429 {
6430 if (plt != plt_bnd)
6431 abort ();
6432 plt = bfd_get_section_by_name (abfd, ".plt");
6433 if (plt == NULL)
6434 abort ();
6435 bed = &elf_x86_64_bnd_arch_bed;
6436 }
6437 else
6438 bed = get_elf_x86_64_backend_data (abfd);
6439
6440 plt_contents = (bfd_byte *) bfd_malloc (plt->size);
6441 if (plt_contents == NULL)
6442 return NULL;
6443 if (!bfd_get_section_contents (abfd, (asection *) plt,
6444 plt_contents, 0, plt->size))
6445 {
6446 bad_return:
6447 free (plt_contents);
6448 return NULL;
6449 }
6450
6451 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
6452 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE))
6453 goto bad_return;
6454
6455 hdr = &elf_section_data (relplt)->this_hdr;
6456 count = relplt->size / hdr->sh_entsize;
6457
6458 plt_sym_val = (bfd_vma *) bfd_malloc (sizeof (bfd_vma) * count);
6459 if (plt_sym_val == NULL)
6460 goto bad_return;
6461
6462 for (i = 0; i < count; i++)
6463 plt_sym_val[i] = -1;
6464
6465 plt_offset = bed->plt_entry_size;
6466 p = relplt->relocation;
6467 for (i = 0; i < count; i++, p++)
6468 {
6469 long reloc_index;
6470
6471 /* Skip unknown relocation. */
6472 if (p->howto == NULL)
6473 continue;
6474
6475 if (p->howto->type != R_X86_64_JUMP_SLOT
6476 && p->howto->type != R_X86_64_IRELATIVE)
6477 continue;
6478
6479 reloc_index = H_GET_32 (abfd, (plt_contents + plt_offset
6480 + bed->plt_reloc_offset));
6481 if (reloc_index < count)
6482 {
6483 if (plt_bnd)
6484 {
6485 /* This is the index in .plt section. */
6486 long plt_index = plt_offset / bed->plt_entry_size;
6487 /* Store VMA + the offset in .plt.bnd section. */
6488 plt_sym_val[reloc_index] =
6489 (plt_bnd->vma
6490 + (plt_index - 1) * sizeof (elf_x86_64_legacy_plt2_entry));
6491 }
6492 else
6493 plt_sym_val[reloc_index] = plt->vma + plt_offset;
6494 }
6495 plt_offset += bed->plt_entry_size;
6496
6497 /* PR binutils/18437: Skip extra relocations in the .rela.plt
6498 section. */
6499 if (plt_offset >= plt->size)
6500 break;
6501 }
6502
6503 free (plt_contents);
6504
6505 return plt_sym_val;
6506 }
6507
6508 /* Similar to _bfd_elf_get_synthetic_symtab, with .plt.bnd section
6509 support. */
6510
6511 static long
6512 elf_x86_64_get_synthetic_symtab (bfd *abfd,
6513 long symcount,
6514 asymbol **syms,
6515 long dynsymcount,
6516 asymbol **dynsyms,
6517 asymbol **ret)
6518 {
6519 /* Pass the .plt.bnd section to _bfd_elf_ifunc_get_synthetic_symtab
6520 as PLT if it exists. */
6521 asection *plt = bfd_get_section_by_name (abfd, ".plt.bnd");
6522 if (plt == NULL)
6523 plt = bfd_get_section_by_name (abfd, ".plt");
6524 return _bfd_elf_ifunc_get_synthetic_symtab (abfd, symcount, syms,
6525 dynsymcount, dynsyms, ret,
6526 plt,
6527 elf_x86_64_get_plt_sym_val);
6528 }
6529
6530 /* Handle an x86-64 specific section when reading an object file. This
6531 is called when elfcode.h finds a section with an unknown type. */
6532
6533 static bfd_boolean
6534 elf_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
6535 const char *name, int shindex)
6536 {
6537 if (hdr->sh_type != SHT_X86_64_UNWIND)
6538 return FALSE;
6539
6540 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
6541 return FALSE;
6542
6543 return TRUE;
6544 }
6545
6546 /* Hook called by the linker routine which adds symbols from an object
6547 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
6548 of .bss. */
6549
6550 static bfd_boolean
6551 elf_x86_64_add_symbol_hook (bfd *abfd,
6552 struct bfd_link_info *info ATTRIBUTE_UNUSED,
6553 Elf_Internal_Sym *sym,
6554 const char **namep ATTRIBUTE_UNUSED,
6555 flagword *flagsp ATTRIBUTE_UNUSED,
6556 asection **secp,
6557 bfd_vma *valp)
6558 {
6559 asection *lcomm;
6560
6561 switch (sym->st_shndx)
6562 {
6563 case SHN_X86_64_LCOMMON:
6564 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
6565 if (lcomm == NULL)
6566 {
6567 lcomm = bfd_make_section_with_flags (abfd,
6568 "LARGE_COMMON",
6569 (SEC_ALLOC
6570 | SEC_IS_COMMON
6571 | SEC_LINKER_CREATED));
6572 if (lcomm == NULL)
6573 return FALSE;
6574 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
6575 }
6576 *secp = lcomm;
6577 *valp = sym->st_size;
6578 return TRUE;
6579 }
6580
6581 return TRUE;
6582 }
6583
6584
6585 /* Given a BFD section, try to locate the corresponding ELF section
6586 index. */
6587
6588 static bfd_boolean
6589 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
6590 asection *sec, int *index_return)
6591 {
6592 if (sec == &_bfd_elf_large_com_section)
6593 {
6594 *index_return = SHN_X86_64_LCOMMON;
6595 return TRUE;
6596 }
6597 return FALSE;
6598 }
6599
6600 /* Process a symbol. */
6601
6602 static void
6603 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
6604 asymbol *asym)
6605 {
6606 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
6607
6608 switch (elfsym->internal_elf_sym.st_shndx)
6609 {
6610 case SHN_X86_64_LCOMMON:
6611 asym->section = &_bfd_elf_large_com_section;
6612 asym->value = elfsym->internal_elf_sym.st_size;
6613 /* Common symbol doesn't set BSF_GLOBAL. */
6614 asym->flags &= ~BSF_GLOBAL;
6615 break;
6616 }
6617 }
6618
6619 static bfd_boolean
6620 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
6621 {
6622 return (sym->st_shndx == SHN_COMMON
6623 || sym->st_shndx == SHN_X86_64_LCOMMON);
6624 }
6625
6626 static unsigned int
6627 elf_x86_64_common_section_index (asection *sec)
6628 {
6629 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6630 return SHN_COMMON;
6631 else
6632 return SHN_X86_64_LCOMMON;
6633 }
6634
6635 static asection *
6636 elf_x86_64_common_section (asection *sec)
6637 {
6638 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
6639 return bfd_com_section_ptr;
6640 else
6641 return &_bfd_elf_large_com_section;
6642 }
6643
6644 static bfd_boolean
6645 elf_x86_64_merge_symbol (struct elf_link_hash_entry *h,
6646 const Elf_Internal_Sym *sym,
6647 asection **psec,
6648 bfd_boolean newdef,
6649 bfd_boolean olddef,
6650 bfd *oldbfd,
6651 const asection *oldsec)
6652 {
6653 /* A normal common symbol and a large common symbol result in a
6654 normal common symbol. We turn the large common symbol into a
6655 normal one. */
6656 if (!olddef
6657 && h->root.type == bfd_link_hash_common
6658 && !newdef
6659 && bfd_is_com_section (*psec)
6660 && oldsec != *psec)
6661 {
6662 if (sym->st_shndx == SHN_COMMON
6663 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) != 0)
6664 {
6665 h->root.u.c.p->section
6666 = bfd_make_section_old_way (oldbfd, "COMMON");
6667 h->root.u.c.p->section->flags = SEC_ALLOC;
6668 }
6669 else if (sym->st_shndx == SHN_X86_64_LCOMMON
6670 && (elf_section_flags (oldsec) & SHF_X86_64_LARGE) == 0)
6671 *psec = bfd_com_section_ptr;
6672 }
6673
6674 return TRUE;
6675 }
6676
6677 static int
6678 elf_x86_64_additional_program_headers (bfd *abfd,
6679 struct bfd_link_info *info ATTRIBUTE_UNUSED)
6680 {
6681 asection *s;
6682 int count = 0;
6683
6684 /* Check to see if we need a large readonly segment. */
6685 s = bfd_get_section_by_name (abfd, ".lrodata");
6686 if (s && (s->flags & SEC_LOAD))
6687 count++;
6688
6689 /* Check to see if we need a large data segment. Since .lbss sections
6690 is placed right after the .bss section, there should be no need for
6691 a large data segment just because of .lbss. */
6692 s = bfd_get_section_by_name (abfd, ".ldata");
6693 if (s && (s->flags & SEC_LOAD))
6694 count++;
6695
6696 return count;
6697 }
6698
6699 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
6700
6701 static bfd_boolean
6702 elf_x86_64_hash_symbol (struct elf_link_hash_entry *h)
6703 {
6704 if (h->plt.offset != (bfd_vma) -1
6705 && !h->def_regular
6706 && !h->pointer_equality_needed)
6707 return FALSE;
6708
6709 return _bfd_elf_hash_symbol (h);
6710 }
6711
6712 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
6713
6714 static bfd_boolean
6715 elf_x86_64_relocs_compatible (const bfd_target *input,
6716 const bfd_target *output)
6717 {
6718 return ((xvec_get_elf_backend_data (input)->s->elfclass
6719 == xvec_get_elf_backend_data (output)->s->elfclass)
6720 && _bfd_elf_relocs_compatible (input, output));
6721 }
6722
6723 static const struct bfd_elf_special_section
6724 elf_x86_64_special_sections[]=
6725 {
6726 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6727 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
6728 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
6729 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6730 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
6731 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
6732 { NULL, 0, 0, 0, 0 }
6733 };
6734
6735 #define TARGET_LITTLE_SYM x86_64_elf64_vec
6736 #define TARGET_LITTLE_NAME "elf64-x86-64"
6737 #define ELF_ARCH bfd_arch_i386
6738 #define ELF_TARGET_ID X86_64_ELF_DATA
6739 #define ELF_MACHINE_CODE EM_X86_64
6740 #define ELF_MAXPAGESIZE 0x200000
6741 #define ELF_MINPAGESIZE 0x1000
6742 #define ELF_COMMONPAGESIZE 0x1000
6743
6744 #define elf_backend_can_gc_sections 1
6745 #define elf_backend_can_refcount 1
6746 #define elf_backend_want_got_plt 1
6747 #define elf_backend_plt_readonly 1
6748 #define elf_backend_want_plt_sym 0
6749 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
6750 #define elf_backend_rela_normal 1
6751 #define elf_backend_plt_alignment 4
6752 #define elf_backend_extern_protected_data 1
6753 #define elf_backend_caches_rawsize 1
6754
6755 #define elf_info_to_howto elf_x86_64_info_to_howto
6756
6757 #define bfd_elf64_bfd_link_hash_table_create \
6758 elf_x86_64_link_hash_table_create
6759 #define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
6760 #define bfd_elf64_bfd_reloc_name_lookup \
6761 elf_x86_64_reloc_name_lookup
6762
6763 #define elf_backend_adjust_dynamic_symbol elf_x86_64_adjust_dynamic_symbol
6764 #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
6765 #define elf_backend_check_relocs elf_x86_64_check_relocs
6766 #define elf_backend_copy_indirect_symbol elf_x86_64_copy_indirect_symbol
6767 #define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
6768 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
6769 #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
6770 #define elf_backend_output_arch_local_syms elf_x86_64_output_arch_local_syms
6771 #define elf_backend_gc_mark_hook elf_x86_64_gc_mark_hook
6772 #define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
6773 #define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
6774 #ifdef CORE_HEADER
6775 #define elf_backend_write_core_note elf_x86_64_write_core_note
6776 #endif
6777 #define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
6778 #define elf_backend_relocate_section elf_x86_64_relocate_section
6779 #define elf_backend_size_dynamic_sections elf_x86_64_size_dynamic_sections
6780 #define elf_backend_always_size_sections elf_x86_64_always_size_sections
6781 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
6782 #define elf_backend_object_p elf64_x86_64_elf_object_p
6783 #define bfd_elf64_mkobject elf_x86_64_mkobject
6784 #define bfd_elf64_get_synthetic_symtab elf_x86_64_get_synthetic_symtab
6785
6786 #define elf_backend_section_from_shdr \
6787 elf_x86_64_section_from_shdr
6788
6789 #define elf_backend_section_from_bfd_section \
6790 elf_x86_64_elf_section_from_bfd_section
6791 #define elf_backend_add_symbol_hook \
6792 elf_x86_64_add_symbol_hook
6793 #define elf_backend_symbol_processing \
6794 elf_x86_64_symbol_processing
6795 #define elf_backend_common_section_index \
6796 elf_x86_64_common_section_index
6797 #define elf_backend_common_section \
6798 elf_x86_64_common_section
6799 #define elf_backend_common_definition \
6800 elf_x86_64_common_definition
6801 #define elf_backend_merge_symbol \
6802 elf_x86_64_merge_symbol
6803 #define elf_backend_special_sections \
6804 elf_x86_64_special_sections
6805 #define elf_backend_additional_program_headers \
6806 elf_x86_64_additional_program_headers
6807 #define elf_backend_hash_symbol \
6808 elf_x86_64_hash_symbol
6809 #define elf_backend_omit_section_dynsym \
6810 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
6811 #define elf_backend_fixup_symbol \
6812 elf_x86_64_fixup_symbol
6813
6814 #include "elf64-target.h"
6815
6816 /* CloudABI support. */
6817
6818 #undef TARGET_LITTLE_SYM
6819 #define TARGET_LITTLE_SYM x86_64_elf64_cloudabi_vec
6820 #undef TARGET_LITTLE_NAME
6821 #define TARGET_LITTLE_NAME "elf64-x86-64-cloudabi"
6822
6823 #undef ELF_OSABI
6824 #define ELF_OSABI ELFOSABI_CLOUDABI
6825
6826 #undef elf64_bed
6827 #define elf64_bed elf64_x86_64_cloudabi_bed
6828
6829 #include "elf64-target.h"
6830
6831 /* FreeBSD support. */
6832
6833 #undef TARGET_LITTLE_SYM
6834 #define TARGET_LITTLE_SYM x86_64_elf64_fbsd_vec
6835 #undef TARGET_LITTLE_NAME
6836 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
6837
6838 #undef ELF_OSABI
6839 #define ELF_OSABI ELFOSABI_FREEBSD
6840
6841 #undef elf64_bed
6842 #define elf64_bed elf64_x86_64_fbsd_bed
6843
6844 #include "elf64-target.h"
6845
6846 /* Solaris 2 support. */
6847
6848 #undef TARGET_LITTLE_SYM
6849 #define TARGET_LITTLE_SYM x86_64_elf64_sol2_vec
6850 #undef TARGET_LITTLE_NAME
6851 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
6852
6853 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
6854 objects won't be recognized. */
6855 #undef ELF_OSABI
6856
6857 #undef elf64_bed
6858 #define elf64_bed elf64_x86_64_sol2_bed
6859
6860 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
6861 boundary. */
6862 #undef elf_backend_static_tls_alignment
6863 #define elf_backend_static_tls_alignment 16
6864
6865 /* The Solaris 2 ABI requires a plt symbol on all platforms.
6866
6867 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
6868 File, p.63. */
6869 #undef elf_backend_want_plt_sym
6870 #define elf_backend_want_plt_sym 1
6871
6872 #undef elf_backend_strtab_flags
6873 #define elf_backend_strtab_flags SHF_STRINGS
6874
6875 static bfd_boolean
6876 elf64_x86_64_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
6877 bfd *obfd ATTRIBUTE_UNUSED,
6878 const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
6879 Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
6880 {
6881 /* PR 19938: FIXME: Need to add code for setting the sh_info
6882 and sh_link fields of Solaris specific section types. */
6883 return FALSE;
6884 }
6885
6886 #undef elf_backend_copy_special_section_fields
6887 #define elf_backend_copy_special_section_fields elf64_x86_64_copy_solaris_special_section_fields
6888
6889 #include "elf64-target.h"
6890
6891 /* Native Client support. */
6892
6893 static bfd_boolean
6894 elf64_x86_64_nacl_elf_object_p (bfd *abfd)
6895 {
6896 /* Set the right machine number for a NaCl x86-64 ELF64 file. */
6897 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64_nacl);
6898 return TRUE;
6899 }
6900
6901 #undef TARGET_LITTLE_SYM
6902 #define TARGET_LITTLE_SYM x86_64_elf64_nacl_vec
6903 #undef TARGET_LITTLE_NAME
6904 #define TARGET_LITTLE_NAME "elf64-x86-64-nacl"
6905 #undef elf64_bed
6906 #define elf64_bed elf64_x86_64_nacl_bed
6907
6908 #undef ELF_MAXPAGESIZE
6909 #undef ELF_MINPAGESIZE
6910 #undef ELF_COMMONPAGESIZE
6911 #define ELF_MAXPAGESIZE 0x10000
6912 #define ELF_MINPAGESIZE 0x10000
6913 #define ELF_COMMONPAGESIZE 0x10000
6914
6915 /* Restore defaults. */
6916 #undef ELF_OSABI
6917 #undef elf_backend_static_tls_alignment
6918 #undef elf_backend_want_plt_sym
6919 #define elf_backend_want_plt_sym 0
6920 #undef elf_backend_strtab_flags
6921 #undef elf_backend_copy_special_section_fields
6922
6923 /* NaCl uses substantially different PLT entries for the same effects. */
6924
6925 #undef elf_backend_plt_alignment
6926 #define elf_backend_plt_alignment 5
6927 #define NACL_PLT_ENTRY_SIZE 64
6928 #define NACLMASK 0xe0 /* 32-byte alignment mask. */
6929
6930 static const bfd_byte elf_x86_64_nacl_plt0_entry[NACL_PLT_ENTRY_SIZE] =
6931 {
6932 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
6933 0x4c, 0x8b, 0x1d, 16, 0, 0, 0, /* mov GOT+16(%rip), %r11 */
6934 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
6935 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
6936 0x41, 0xff, 0xe3, /* jmpq *%r11 */
6937
6938 /* 9-byte nop sequence to pad out to the next 32-byte boundary. */
6939 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw 0x0(%rax,%rax,1) */
6940
6941 /* 32 bytes of nop to pad out to the standard size. */
6942 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
6943 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
6944 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
6945 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
6946 0x66, /* excess data16 prefix */
6947 0x90 /* nop */
6948 };
6949
6950 static const bfd_byte elf_x86_64_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] =
6951 {
6952 0x4c, 0x8b, 0x1d, 0, 0, 0, 0, /* mov name@GOTPCREL(%rip),%r11 */
6953 0x41, 0x83, 0xe3, NACLMASK, /* and $-32, %r11d */
6954 0x4d, 0x01, 0xfb, /* add %r15, %r11 */
6955 0x41, 0xff, 0xe3, /* jmpq *%r11 */
6956
6957 /* 15-byte nop sequence to pad out to the next 32-byte boundary. */
6958 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
6959 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
6960
6961 /* Lazy GOT entries point here (32-byte aligned). */
6962 0x68, /* pushq immediate */
6963 0, 0, 0, 0, /* replaced with index into relocation table. */
6964 0xe9, /* jmp relative */
6965 0, 0, 0, 0, /* replaced with offset to start of .plt0. */
6966
6967 /* 22 bytes of nop to pad out to the standard size. */
6968 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, /* excess data16 prefixes */
6969 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, /* nopw %cs:0x0(%rax,%rax,1) */
6970 0x0f, 0x1f, 0x80, 0, 0, 0, 0, /* nopl 0x0(%rax) */
6971 };
6972
6973 /* .eh_frame covering the .plt section. */
6974
6975 static const bfd_byte elf_x86_64_nacl_eh_frame_plt[] =
6976 {
6977 #if (PLT_CIE_LENGTH != 20 \
6978 || PLT_FDE_LENGTH != 36 \
6979 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \
6980 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12)
6981 # error "Need elf_x86_64_backend_data parameters for eh_frame_plt offsets!"
6982 #endif
6983 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */
6984 0, 0, 0, 0, /* CIE ID */
6985 1, /* CIE version */
6986 'z', 'R', 0, /* Augmentation string */
6987 1, /* Code alignment factor */
6988 0x78, /* Data alignment factor */
6989 16, /* Return address column */
6990 1, /* Augmentation size */
6991 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */
6992 DW_CFA_def_cfa, 7, 8, /* DW_CFA_def_cfa: r7 (rsp) ofs 8 */
6993 DW_CFA_offset + 16, 1, /* DW_CFA_offset: r16 (rip) at cfa-8 */
6994 DW_CFA_nop, DW_CFA_nop,
6995
6996 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */
6997 PLT_CIE_LENGTH + 8, 0, 0, 0,/* CIE pointer */
6998 0, 0, 0, 0, /* R_X86_64_PC32 .plt goes here */
6999 0, 0, 0, 0, /* .plt size goes here */
7000 0, /* Augmentation size */
7001 DW_CFA_def_cfa_offset, 16, /* DW_CFA_def_cfa_offset: 16 */
7002 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */
7003 DW_CFA_def_cfa_offset, 24, /* DW_CFA_def_cfa_offset: 24 */
7004 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */
7005 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */
7006 13, /* Block length */
7007 DW_OP_breg7, 8, /* DW_OP_breg7 (rsp): 8 */
7008 DW_OP_breg16, 0, /* DW_OP_breg16 (rip): 0 */
7009 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge,
7010 DW_OP_lit3, DW_OP_shl, DW_OP_plus,
7011 DW_CFA_nop, DW_CFA_nop
7012 };
7013
7014 static const struct elf_x86_64_backend_data elf_x86_64_nacl_arch_bed =
7015 {
7016 elf_x86_64_nacl_plt0_entry, /* plt0_entry */
7017 elf_x86_64_nacl_plt_entry, /* plt_entry */
7018 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */
7019 2, /* plt0_got1_offset */
7020 9, /* plt0_got2_offset */
7021 13, /* plt0_got2_insn_end */
7022 3, /* plt_got_offset */
7023 33, /* plt_reloc_offset */
7024 38, /* plt_plt_offset */
7025 7, /* plt_got_insn_size */
7026 42, /* plt_plt_insn_end */
7027 32, /* plt_lazy_offset */
7028 elf_x86_64_nacl_eh_frame_plt, /* eh_frame_plt */
7029 sizeof (elf_x86_64_nacl_eh_frame_plt), /* eh_frame_plt_size */
7030 };
7031
7032 #undef elf_backend_arch_data
7033 #define elf_backend_arch_data &elf_x86_64_nacl_arch_bed
7034
7035 #undef elf_backend_object_p
7036 #define elf_backend_object_p elf64_x86_64_nacl_elf_object_p
7037 #undef elf_backend_modify_segment_map
7038 #define elf_backend_modify_segment_map nacl_modify_segment_map
7039 #undef elf_backend_modify_program_headers
7040 #define elf_backend_modify_program_headers nacl_modify_program_headers
7041 #undef elf_backend_final_write_processing
7042 #define elf_backend_final_write_processing nacl_final_write_processing
7043
7044 #include "elf64-target.h"
7045
7046 /* Native Client x32 support. */
7047
7048 static bfd_boolean
7049 elf32_x86_64_nacl_elf_object_p (bfd *abfd)
7050 {
7051 /* Set the right machine number for a NaCl x86-64 ELF32 file. */
7052 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32_nacl);
7053 return TRUE;
7054 }
7055
7056 #undef TARGET_LITTLE_SYM
7057 #define TARGET_LITTLE_SYM x86_64_elf32_nacl_vec
7058 #undef TARGET_LITTLE_NAME
7059 #define TARGET_LITTLE_NAME "elf32-x86-64-nacl"
7060 #undef elf32_bed
7061 #define elf32_bed elf32_x86_64_nacl_bed
7062
7063 #define bfd_elf32_bfd_link_hash_table_create \
7064 elf_x86_64_link_hash_table_create
7065 #define bfd_elf32_bfd_reloc_type_lookup \
7066 elf_x86_64_reloc_type_lookup
7067 #define bfd_elf32_bfd_reloc_name_lookup \
7068 elf_x86_64_reloc_name_lookup
7069 #define bfd_elf32_mkobject \
7070 elf_x86_64_mkobject
7071 #define bfd_elf32_get_synthetic_symtab \
7072 elf_x86_64_get_synthetic_symtab
7073
7074 #undef elf_backend_object_p
7075 #define elf_backend_object_p \
7076 elf32_x86_64_nacl_elf_object_p
7077
7078 #undef elf_backend_bfd_from_remote_memory
7079 #define elf_backend_bfd_from_remote_memory \
7080 _bfd_elf32_bfd_from_remote_memory
7081
7082 #undef elf_backend_size_info
7083 #define elf_backend_size_info \
7084 _bfd_elf32_size_info
7085
7086 #include "elf32-target.h"
7087
7088 /* Restore defaults. */
7089 #undef elf_backend_object_p
7090 #define elf_backend_object_p elf64_x86_64_elf_object_p
7091 #undef elf_backend_bfd_from_remote_memory
7092 #undef elf_backend_size_info
7093 #undef elf_backend_modify_segment_map
7094 #undef elf_backend_modify_program_headers
7095 #undef elf_backend_final_write_processing
7096
7097 /* Intel L1OM support. */
7098
7099 static bfd_boolean
7100 elf64_l1om_elf_object_p (bfd *abfd)
7101 {
7102 /* Set the right machine number for an L1OM elf64 file. */
7103 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
7104 return TRUE;
7105 }
7106
7107 #undef TARGET_LITTLE_SYM
7108 #define TARGET_LITTLE_SYM l1om_elf64_vec
7109 #undef TARGET_LITTLE_NAME
7110 #define TARGET_LITTLE_NAME "elf64-l1om"
7111 #undef ELF_ARCH
7112 #define ELF_ARCH bfd_arch_l1om
7113
7114 #undef ELF_MACHINE_CODE
7115 #define ELF_MACHINE_CODE EM_L1OM
7116
7117 #undef ELF_OSABI
7118
7119 #undef elf64_bed
7120 #define elf64_bed elf64_l1om_bed
7121
7122 #undef elf_backend_object_p
7123 #define elf_backend_object_p elf64_l1om_elf_object_p
7124
7125 /* Restore defaults. */
7126 #undef ELF_MAXPAGESIZE
7127 #undef ELF_MINPAGESIZE
7128 #undef ELF_COMMONPAGESIZE
7129 #define ELF_MAXPAGESIZE 0x200000
7130 #define ELF_MINPAGESIZE 0x1000
7131 #define ELF_COMMONPAGESIZE 0x1000
7132 #undef elf_backend_plt_alignment
7133 #define elf_backend_plt_alignment 4
7134 #undef elf_backend_arch_data
7135 #define elf_backend_arch_data &elf_x86_64_arch_bed
7136
7137 #include "elf64-target.h"
7138
7139 /* FreeBSD L1OM support. */
7140
7141 #undef TARGET_LITTLE_SYM
7142 #define TARGET_LITTLE_SYM l1om_elf64_fbsd_vec
7143 #undef TARGET_LITTLE_NAME
7144 #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
7145
7146 #undef ELF_OSABI
7147 #define ELF_OSABI ELFOSABI_FREEBSD
7148
7149 #undef elf64_bed
7150 #define elf64_bed elf64_l1om_fbsd_bed
7151
7152 #include "elf64-target.h"
7153
7154 /* Intel K1OM support. */
7155
7156 static bfd_boolean
7157 elf64_k1om_elf_object_p (bfd *abfd)
7158 {
7159 /* Set the right machine number for an K1OM elf64 file. */
7160 bfd_default_set_arch_mach (abfd, bfd_arch_k1om, bfd_mach_k1om);
7161 return TRUE;
7162 }
7163
7164 #undef TARGET_LITTLE_SYM
7165 #define TARGET_LITTLE_SYM k1om_elf64_vec
7166 #undef TARGET_LITTLE_NAME
7167 #define TARGET_LITTLE_NAME "elf64-k1om"
7168 #undef ELF_ARCH
7169 #define ELF_ARCH bfd_arch_k1om
7170
7171 #undef ELF_MACHINE_CODE
7172 #define ELF_MACHINE_CODE EM_K1OM
7173
7174 #undef ELF_OSABI
7175
7176 #undef elf64_bed
7177 #define elf64_bed elf64_k1om_bed
7178
7179 #undef elf_backend_object_p
7180 #define elf_backend_object_p elf64_k1om_elf_object_p
7181
7182 #undef elf_backend_static_tls_alignment
7183
7184 #undef elf_backend_want_plt_sym
7185 #define elf_backend_want_plt_sym 0
7186
7187 #include "elf64-target.h"
7188
7189 /* FreeBSD K1OM support. */
7190
7191 #undef TARGET_LITTLE_SYM
7192 #define TARGET_LITTLE_SYM k1om_elf64_fbsd_vec
7193 #undef TARGET_LITTLE_NAME
7194 #define TARGET_LITTLE_NAME "elf64-k1om-freebsd"
7195
7196 #undef ELF_OSABI
7197 #define ELF_OSABI ELFOSABI_FREEBSD
7198
7199 #undef elf64_bed
7200 #define elf64_bed elf64_k1om_fbsd_bed
7201
7202 #include "elf64-target.h"
7203
7204 /* 32bit x86-64 support. */
7205
7206 #undef TARGET_LITTLE_SYM
7207 #define TARGET_LITTLE_SYM x86_64_elf32_vec
7208 #undef TARGET_LITTLE_NAME
7209 #define TARGET_LITTLE_NAME "elf32-x86-64"
7210 #undef elf32_bed
7211
7212 #undef ELF_ARCH
7213 #define ELF_ARCH bfd_arch_i386
7214
7215 #undef ELF_MACHINE_CODE
7216 #define ELF_MACHINE_CODE EM_X86_64
7217
7218 #undef ELF_OSABI
7219
7220 #undef elf_backend_object_p
7221 #define elf_backend_object_p \
7222 elf32_x86_64_elf_object_p
7223
7224 #undef elf_backend_bfd_from_remote_memory
7225 #define elf_backend_bfd_from_remote_memory \
7226 _bfd_elf32_bfd_from_remote_memory
7227
7228 #undef elf_backend_size_info
7229 #define elf_backend_size_info \
7230 _bfd_elf32_size_info
7231
7232 #include "elf32-target.h"
This page took 0.263656 seconds and 4 git commands to generate.