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