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