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