* doc/Makefile.am: (CPU_DOCS): Add c-xstormy16.texi.
[binutils-gdb.git] / bfd / elf64-x86-64.c
1 /* X86-64 specific support for ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010, 2011
4 Free Software Foundation, Inc.
5 Contributed by Jan Hubicka <jh@suse.cz>.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
23
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "bfdlink.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "bfd_stdint.h"
30 #include "objalloc.h"
31 #include "hashtab.h"
32
33 #include "elf/x86-64.h"
34
35 #ifdef CORE_HEADER
36 #include <stdarg.h>
37 #include CORE_HEADER
38 #endif
39
40 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
41 #define MINUS_ONE (~ (bfd_vma) 0)
42
43 /* Since both 32-bit and 64-bit x86-64 encode relocation type in the
44 identical manner, we use ELF32_R_TYPE instead of ELF64_R_TYPE to get
45 relocation type. We also use ELF_ST_TYPE instead of ELF64_ST_TYPE
46 since they are the same. */
47
48 #define ABI_64_P(abfd) \
49 (get_elf_backend_data (abfd)->s->elfclass == ELFCLASS64)
50
51 /* The relocation "howto" table. Order of fields:
52 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
53 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
54 static reloc_howto_type x86_64_elf_howto_table[] =
55 {
56 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
57 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
58 FALSE),
59 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
60 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
61 FALSE),
62 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
63 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
64 TRUE),
65 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
66 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
67 FALSE),
68 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
69 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
70 TRUE),
71 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
72 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
73 FALSE),
74 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
75 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
76 MINUS_ONE, FALSE),
77 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
78 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
79 MINUS_ONE, FALSE),
80 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
81 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
82 MINUS_ONE, FALSE),
83 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
84 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
85 0xffffffff, TRUE),
86 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
87 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
88 FALSE),
89 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
90 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
91 FALSE),
92 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
93 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
94 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
95 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
96 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
97 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
98 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
99 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
100 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
101 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
102 MINUS_ONE, FALSE),
103 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
104 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
105 MINUS_ONE, FALSE),
106 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
107 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
108 MINUS_ONE, FALSE),
109 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
110 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
111 0xffffffff, TRUE),
112 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
113 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
114 0xffffffff, TRUE),
115 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
116 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
117 0xffffffff, FALSE),
118 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
119 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
120 0xffffffff, TRUE),
121 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
122 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
123 0xffffffff, FALSE),
124 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
125 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
126 TRUE),
127 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
128 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
129 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
130 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
131 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
132 FALSE, 0xffffffff, 0xffffffff, TRUE),
133 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
134 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
135 FALSE),
136 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
137 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
138 MINUS_ONE, TRUE),
139 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
140 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
141 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
142 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
143 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
144 MINUS_ONE, FALSE),
145 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
146 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
147 MINUS_ONE, FALSE),
148 EMPTY_HOWTO (32),
149 EMPTY_HOWTO (33),
150 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
151 complain_overflow_bitfield, bfd_elf_generic_reloc,
152 "R_X86_64_GOTPC32_TLSDESC",
153 FALSE, 0xffffffff, 0xffffffff, TRUE),
154 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
155 complain_overflow_dont, bfd_elf_generic_reloc,
156 "R_X86_64_TLSDESC_CALL",
157 FALSE, 0, 0, FALSE),
158 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
159 complain_overflow_bitfield, bfd_elf_generic_reloc,
160 "R_X86_64_TLSDESC",
161 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
162 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
163 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
164 MINUS_ONE, FALSE),
165
166 /* We have a gap in the reloc numbers here.
167 R_X86_64_standard counts the number up to this point, and
168 R_X86_64_vt_offset is the value to subtract from a reloc type of
169 R_X86_64_GNU_VT* to form an index into this table. */
170 #define R_X86_64_standard (R_X86_64_IRELATIVE + 1)
171 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
172
173 /* GNU extension to record C++ vtable hierarchy. */
174 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
175 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
176
177 /* GNU extension to record C++ vtable member usage. */
178 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
179 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
180 FALSE)
181 };
182
183 #define IS_X86_64_PCREL_TYPE(TYPE) \
184 ( ((TYPE) == R_X86_64_PC8) \
185 || ((TYPE) == R_X86_64_PC16) \
186 || ((TYPE) == R_X86_64_PC32) \
187 || ((TYPE) == R_X86_64_PC64))
188
189 /* Map BFD relocs to the x86_64 elf relocs. */
190 struct elf_reloc_map
191 {
192 bfd_reloc_code_real_type bfd_reloc_val;
193 unsigned char elf_reloc_val;
194 };
195
196 static const struct elf_reloc_map x86_64_reloc_map[] =
197 {
198 { BFD_RELOC_NONE, R_X86_64_NONE, },
199 { BFD_RELOC_64, R_X86_64_64, },
200 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
201 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
202 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
203 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
204 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
205 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
206 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
207 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
208 { BFD_RELOC_32, R_X86_64_32, },
209 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
210 { BFD_RELOC_16, R_X86_64_16, },
211 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
212 { BFD_RELOC_8, R_X86_64_8, },
213 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
214 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
215 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
216 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
217 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
218 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
219 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
220 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
221 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
222 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
223 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
224 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
225 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
226 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
227 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
228 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
229 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
230 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
231 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
232 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
233 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
234 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
235 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
236 };
237
238 static reloc_howto_type *
239 elf_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
240 {
241 unsigned i;
242
243 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
244 || r_type >= (unsigned int) R_X86_64_max)
245 {
246 if (r_type >= (unsigned int) R_X86_64_standard)
247 {
248 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
249 abfd, (int) r_type);
250 r_type = R_X86_64_NONE;
251 }
252 i = r_type;
253 }
254 else
255 i = r_type - (unsigned int) R_X86_64_vt_offset;
256 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
257 return &x86_64_elf_howto_table[i];
258 }
259
260 /* Given a BFD reloc type, return a HOWTO structure. */
261 static reloc_howto_type *
262 elf_x86_64_reloc_type_lookup (bfd *abfd,
263 bfd_reloc_code_real_type code)
264 {
265 unsigned int i;
266
267 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
268 i++)
269 {
270 if (x86_64_reloc_map[i].bfd_reloc_val == code)
271 return elf_x86_64_rtype_to_howto (abfd,
272 x86_64_reloc_map[i].elf_reloc_val);
273 }
274 return 0;
275 }
276
277 static reloc_howto_type *
278 elf_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
279 const char *r_name)
280 {
281 unsigned int i;
282
283 for (i = 0;
284 i < (sizeof (x86_64_elf_howto_table)
285 / sizeof (x86_64_elf_howto_table[0]));
286 i++)
287 if (x86_64_elf_howto_table[i].name != NULL
288 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
289 return &x86_64_elf_howto_table[i];
290
291 return NULL;
292 }
293
294 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
295
296 static void
297 elf_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
298 Elf_Internal_Rela *dst)
299 {
300 unsigned r_type;
301
302 r_type = ELF32_R_TYPE (dst->r_info);
303 cache_ptr->howto = elf_x86_64_rtype_to_howto (abfd, r_type);
304 BFD_ASSERT (r_type == cache_ptr->howto->type);
305 }
306 \f
307 /* Support for core dump NOTE sections. */
308 static bfd_boolean
309 elf_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
310 {
311 int offset;
312 size_t size;
313
314 switch (note->descsz)
315 {
316 default:
317 return FALSE;
318
319 case 296: /* sizeof(istruct elf_prstatus) on Linux/x32 */
320 /* pr_cursig */
321 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
322
323 /* pr_pid */
324 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 24);
325
326 /* pr_reg */
327 offset = 72;
328 size = 216;
329
330 break;
331
332 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
333 /* pr_cursig */
334 elf_tdata (abfd)->core_signal
335 = bfd_get_16 (abfd, note->descdata + 12);
336
337 /* pr_pid */
338 elf_tdata (abfd)->core_lwpid
339 = bfd_get_32 (abfd, note->descdata + 32);
340
341 /* pr_reg */
342 offset = 112;
343 size = 216;
344
345 break;
346 }
347
348 /* Make a ".reg/999" section. */
349 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
350 size, note->descpos + offset);
351 }
352
353 static bfd_boolean
354 elf_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
355 {
356 switch (note->descsz)
357 {
358 default:
359 return FALSE;
360
361 case 124: /* sizeof(struct elf_prpsinfo) on Linux/x32 */
362 elf_tdata (abfd)->core_pid
363 = bfd_get_32 (abfd, note->descdata + 12);
364 elf_tdata (abfd)->core_program
365 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
366 elf_tdata (abfd)->core_command
367 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
368 break;
369
370 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
371 elf_tdata (abfd)->core_pid
372 = bfd_get_32 (abfd, note->descdata + 24);
373 elf_tdata (abfd)->core_program
374 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
375 elf_tdata (abfd)->core_command
376 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
377 }
378
379 /* Note that for some reason, a spurious space is tacked
380 onto the end of the args in some (at least one anyway)
381 implementations, so strip it off if it exists. */
382
383 {
384 char *command = elf_tdata (abfd)->core_command;
385 int n = strlen (command);
386
387 if (0 < n && command[n - 1] == ' ')
388 command[n - 1] = '\0';
389 }
390
391 return TRUE;
392 }
393
394 #ifdef CORE_HEADER
395 static char *
396 elf_x86_64_write_core_note (bfd *abfd, char *buf, int *bufsiz,
397 int note_type, ...)
398 {
399 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
400 const void *p;
401 int size;
402 va_list ap;
403 const char *fname, *psargs;
404 long pid;
405 int cursig;
406 const void *gregs;
407
408 switch (note_type)
409 {
410 default:
411 return NULL;
412
413 case NT_PRPSINFO:
414 va_start (ap, note_type);
415 fname = va_arg (ap, const char *);
416 psargs = va_arg (ap, const char *);
417 va_end (ap);
418
419 if (bed->s->elfclass == ELFCLASS32)
420 {
421 prpsinfo32_t data;
422 memset (&data, 0, sizeof (data));
423 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
424 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
425 p = (const void *) &data;
426 size = sizeof (data);
427 }
428 else
429 {
430 prpsinfo_t data;
431 memset (&data, 0, sizeof (data));
432 strncpy (data.pr_fname, fname, sizeof (data.pr_fname));
433 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs));
434 p = (const void *) &data;
435 size = sizeof (data);
436 }
437 break;
438
439 case NT_PRSTATUS:
440 va_start (ap, note_type);
441 pid = va_arg (ap, long);
442 cursig = va_arg (ap, int);
443 gregs = va_arg (ap, const void *);
444 va_end (ap);
445
446 if (bed->s->elfclass == ELFCLASS32)
447 {
448 if (bed->elf_machine_code == EM_X86_64)
449 {
450 prstatusx32_t prstat;
451 memset (&prstat, 0, sizeof (prstat));
452 prstat.pr_pid = pid;
453 prstat.pr_cursig = cursig;
454 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
455 p = (const void *) &prstat;
456 size = sizeof (prstat);
457 }
458 else
459 {
460 prstatus32_t prstat;
461 memset (&prstat, 0, sizeof (prstat));
462 prstat.pr_pid = pid;
463 prstat.pr_cursig = cursig;
464 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
465 p = (const void *) &prstat;
466 size = sizeof (prstat);
467 }
468 }
469 else
470 {
471 prstatus_t prstat;
472 memset (&prstat, 0, sizeof (prstat));
473 prstat.pr_pid = pid;
474 prstat.pr_cursig = cursig;
475 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg));
476 p = (const void *) &prstat;
477 size = sizeof (prstat);
478 }
479 break;
480 }
481
482 return elfcore_write_note (abfd, buf, bufsiz, "CORE", note_type, p,
483 size);
484 }
485 #endif
486 \f
487 /* Functions for the x86-64 ELF linker. */
488
489 /* The name of the dynamic interpreter. This is put in the .interp
490 section. */
491
492 #define ELF64_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
493 #define ELF32_DYNAMIC_INTERPRETER "/lib/ld32.so.1"
494
495 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
496 copying dynamic variables from a shared lib into an app's dynbss
497 section, and instead use a dynamic relocation to point into the
498 shared lib. */
499 #define ELIMINATE_COPY_RELOCS 1
500
501 /* The size in bytes of an entry in the global offset table. */
502
503 #define GOT_ENTRY_SIZE 8
504
505 /* The size in bytes of an entry in the procedure linkage table. */
506
507 #define PLT_ENTRY_SIZE 16
508
509 /* The first entry in a procedure linkage table looks like this. See the
510 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
511
512 static const bfd_byte elf_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
513 {
514 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
515 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
516 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
517 };
518
519 /* Subsequent entries in a procedure linkage table look like this. */
520
521 static const bfd_byte elf_x86_64_plt_entry[PLT_ENTRY_SIZE] =
522 {
523 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
524 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
525 0x68, /* pushq immediate */
526 0, 0, 0, 0, /* replaced with index into relocation table. */
527 0xe9, /* jmp relative */
528 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
529 };
530
531 /* x86-64 ELF linker hash entry. */
532
533 struct elf_x86_64_link_hash_entry
534 {
535 struct elf_link_hash_entry elf;
536
537 /* Track dynamic relocs copied for this symbol. */
538 struct elf_dyn_relocs *dyn_relocs;
539
540 #define GOT_UNKNOWN 0
541 #define GOT_NORMAL 1
542 #define GOT_TLS_GD 2
543 #define GOT_TLS_IE 3
544 #define GOT_TLS_GDESC 4
545 #define GOT_TLS_GD_BOTH_P(type) \
546 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
547 #define GOT_TLS_GD_P(type) \
548 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
549 #define GOT_TLS_GDESC_P(type) \
550 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
551 #define GOT_TLS_GD_ANY_P(type) \
552 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
553 unsigned char tls_type;
554
555 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
556 starting at the end of the jump table. */
557 bfd_vma tlsdesc_got;
558 };
559
560 #define elf_x86_64_hash_entry(ent) \
561 ((struct elf_x86_64_link_hash_entry *)(ent))
562
563 struct elf_x86_64_obj_tdata
564 {
565 struct elf_obj_tdata root;
566
567 /* tls_type for each local got entry. */
568 char *local_got_tls_type;
569
570 /* GOTPLT entries for TLS descriptors. */
571 bfd_vma *local_tlsdesc_gotent;
572 };
573
574 #define elf_x86_64_tdata(abfd) \
575 ((struct elf_x86_64_obj_tdata *) (abfd)->tdata.any)
576
577 #define elf_x86_64_local_got_tls_type(abfd) \
578 (elf_x86_64_tdata (abfd)->local_got_tls_type)
579
580 #define elf_x86_64_local_tlsdesc_gotent(abfd) \
581 (elf_x86_64_tdata (abfd)->local_tlsdesc_gotent)
582
583 #define is_x86_64_elf(bfd) \
584 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
585 && elf_tdata (bfd) != NULL \
586 && elf_object_id (bfd) == X86_64_ELF_DATA)
587
588 static bfd_boolean
589 elf_x86_64_mkobject (bfd *abfd)
590 {
591 return bfd_elf_allocate_object (abfd, sizeof (struct elf_x86_64_obj_tdata),
592 X86_64_ELF_DATA);
593 }
594
595 /* x86-64 ELF linker hash table. */
596
597 struct elf_x86_64_link_hash_table
598 {
599 struct elf_link_hash_table elf;
600
601 /* Short-cuts to get to dynamic linker sections. */
602 asection *sdynbss;
603 asection *srelbss;
604
605 union
606 {
607 bfd_signed_vma refcount;
608 bfd_vma offset;
609 } tls_ld_got;
610
611 /* The amount of space used by the jump slots in the GOT. */
612 bfd_vma sgotplt_jump_table_size;
613
614 /* Small local sym cache. */
615 struct sym_cache sym_cache;
616
617 bfd_vma (*r_info) (bfd_vma, bfd_vma);
618 bfd_vma (*r_sym) (bfd_vma);
619 unsigned int pointer_r_type;
620 const char *dynamic_interpreter;
621 int dynamic_interpreter_size;
622
623 /* _TLS_MODULE_BASE_ symbol. */
624 struct bfd_link_hash_entry *tls_module_base;
625
626 /* Used by local STT_GNU_IFUNC symbols. */
627 htab_t loc_hash_table;
628 void * loc_hash_memory;
629
630 /* The offset into splt of the PLT entry for the TLS descriptor
631 resolver. Special values are 0, if not necessary (or not found
632 to be necessary yet), and -1 if needed but not determined
633 yet. */
634 bfd_vma tlsdesc_plt;
635 /* The offset into sgot of the GOT entry used by the PLT entry
636 above. */
637 bfd_vma tlsdesc_got;
638 };
639
640 /* Get the x86-64 ELF linker hash table from a link_info structure. */
641
642 #define elf_x86_64_hash_table(p) \
643 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
644 == X86_64_ELF_DATA ? ((struct elf_x86_64_link_hash_table *) ((p)->hash)) : NULL)
645
646 #define elf_x86_64_compute_jump_table_size(htab) \
647 ((htab)->elf.srelplt->reloc_count * GOT_ENTRY_SIZE)
648
649 /* Create an entry in an x86-64 ELF linker hash table. */
650
651 static struct bfd_hash_entry *
652 elf_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
653 struct bfd_hash_table *table,
654 const char *string)
655 {
656 /* Allocate the structure if it has not already been allocated by a
657 subclass. */
658 if (entry == NULL)
659 {
660 entry = (struct bfd_hash_entry *)
661 bfd_hash_allocate (table,
662 sizeof (struct elf_x86_64_link_hash_entry));
663 if (entry == NULL)
664 return entry;
665 }
666
667 /* Call the allocation method of the superclass. */
668 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
669 if (entry != NULL)
670 {
671 struct elf_x86_64_link_hash_entry *eh;
672
673 eh = (struct elf_x86_64_link_hash_entry *) entry;
674 eh->dyn_relocs = NULL;
675 eh->tls_type = GOT_UNKNOWN;
676 eh->tlsdesc_got = (bfd_vma) -1;
677 }
678
679 return entry;
680 }
681
682 /* Compute a hash of a local hash entry. We use elf_link_hash_entry
683 for local symbol so that we can handle local STT_GNU_IFUNC symbols
684 as global symbol. We reuse indx and dynstr_index for local symbol
685 hash since they aren't used by global symbols in this backend. */
686
687 static hashval_t
688 elf_x86_64_local_htab_hash (const void *ptr)
689 {
690 struct elf_link_hash_entry *h
691 = (struct elf_link_hash_entry *) ptr;
692 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index);
693 }
694
695 /* Compare local hash entries. */
696
697 static int
698 elf_x86_64_local_htab_eq (const void *ptr1, const void *ptr2)
699 {
700 struct elf_link_hash_entry *h1
701 = (struct elf_link_hash_entry *) ptr1;
702 struct elf_link_hash_entry *h2
703 = (struct elf_link_hash_entry *) ptr2;
704
705 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index;
706 }
707
708 /* Find and/or create a hash entry for local symbol. */
709
710 static struct elf_link_hash_entry *
711 elf_x86_64_get_local_sym_hash (struct elf_x86_64_link_hash_table *htab,
712 bfd *abfd, const Elf_Internal_Rela *rel,
713 bfd_boolean create)
714 {
715 struct elf_x86_64_link_hash_entry e, *ret;
716 asection *sec = abfd->sections;
717 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id,
718 htab->r_sym (rel->r_info));
719 void **slot;
720
721 e.elf.indx = sec->id;
722 e.elf.dynstr_index = htab->r_sym (rel->r_info);
723 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h,
724 create ? INSERT : NO_INSERT);
725
726 if (!slot)
727 return NULL;
728
729 if (*slot)
730 {
731 ret = (struct elf_x86_64_link_hash_entry *) *slot;
732 return &ret->elf;
733 }
734
735 ret = (struct elf_x86_64_link_hash_entry *)
736 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory,
737 sizeof (struct elf_x86_64_link_hash_entry));
738 if (ret)
739 {
740 memset (ret, 0, sizeof (*ret));
741 ret->elf.indx = sec->id;
742 ret->elf.dynstr_index = htab->r_sym (rel->r_info);
743 ret->elf.dynindx = -1;
744 *slot = ret;
745 }
746 return &ret->elf;
747 }
748
749 /* Create an X86-64 ELF linker hash table. */
750
751 static struct bfd_link_hash_table *
752 elf_x86_64_link_hash_table_create (bfd *abfd)
753 {
754 struct elf_x86_64_link_hash_table *ret;
755 bfd_size_type amt = sizeof (struct elf_x86_64_link_hash_table);
756
757 ret = (struct elf_x86_64_link_hash_table *) bfd_malloc (amt);
758 if (ret == NULL)
759 return NULL;
760
761 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
762 elf_x86_64_link_hash_newfunc,
763 sizeof (struct elf_x86_64_link_hash_entry),
764 X86_64_ELF_DATA))
765 {
766 free (ret);
767 return NULL;
768 }
769
770 ret->sdynbss = NULL;
771 ret->srelbss = NULL;
772 ret->sym_cache.abfd = NULL;
773 ret->tlsdesc_plt = 0;
774 ret->tlsdesc_got = 0;
775 ret->tls_ld_got.refcount = 0;
776 ret->sgotplt_jump_table_size = 0;
777 ret->tls_module_base = NULL;
778
779 if (ABI_64_P (abfd))
780 {
781 ret->r_info = elf64_r_info;
782 ret->r_sym = elf64_r_sym;
783 ret->pointer_r_type = R_X86_64_64;
784 ret->dynamic_interpreter = ELF64_DYNAMIC_INTERPRETER;
785 ret->dynamic_interpreter_size = sizeof ELF64_DYNAMIC_INTERPRETER;
786 }
787 else
788 {
789 ret->r_info = elf32_r_info;
790 ret->r_sym = elf32_r_sym;
791 ret->pointer_r_type = R_X86_64_32;
792 ret->dynamic_interpreter = ELF32_DYNAMIC_INTERPRETER;
793 ret->dynamic_interpreter_size = sizeof ELF32_DYNAMIC_INTERPRETER;
794 }
795
796 ret->loc_hash_table = htab_try_create (1024,
797 elf_x86_64_local_htab_hash,
798 elf_x86_64_local_htab_eq,
799 NULL);
800 ret->loc_hash_memory = objalloc_create ();
801 if (!ret->loc_hash_table || !ret->loc_hash_memory)
802 {
803 free (ret);
804 return NULL;
805 }
806
807 return &ret->elf.root;
808 }
809
810 /* Destroy an X86-64 ELF linker hash table. */
811
812 static void
813 elf_x86_64_link_hash_table_free (struct bfd_link_hash_table *hash)
814 {
815 struct elf_x86_64_link_hash_table *htab
816 = (struct elf_x86_64_link_hash_table *) hash;
817
818 if (htab->loc_hash_table)
819 htab_delete (htab->loc_hash_table);
820 if (htab->loc_hash_memory)
821 objalloc_free ((struct objalloc *) htab->loc_hash_memory);
822 _bfd_generic_link_hash_table_free (hash);
823 }
824
825 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
826 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
827 hash table. */
828
829 static bfd_boolean
830 elf_x86_64_create_dynamic_sections (bfd *dynobj,
831 struct bfd_link_info *info)
832 {
833 struct elf_x86_64_link_hash_table *htab;
834
835 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
836 return FALSE;
837
838 htab = elf_x86_64_hash_table (info);
839 if (htab == NULL)
840 return FALSE;
841
842 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
843 if (!info->shared)
844 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
845
846 if (!htab->sdynbss
847 || (!info->shared && !htab->srelbss))
848 abort ();
849
850 return TRUE;
851 }
852
853 /* Copy the extra info we tack onto an elf_link_hash_entry. */
854
855 static void
856 elf_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
857 struct elf_link_hash_entry *dir,
858 struct elf_link_hash_entry *ind)
859 {
860 struct elf_x86_64_link_hash_entry *edir, *eind;
861
862 edir = (struct elf_x86_64_link_hash_entry *) dir;
863 eind = (struct elf_x86_64_link_hash_entry *) ind;
864
865 if (eind->dyn_relocs != NULL)
866 {
867 if (edir->dyn_relocs != NULL)
868 {
869 struct elf_dyn_relocs **pp;
870 struct elf_dyn_relocs *p;
871
872 /* Add reloc counts against the indirect sym to the direct sym
873 list. Merge any entries against the same section. */
874 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
875 {
876 struct elf_dyn_relocs *q;
877
878 for (q = edir->dyn_relocs; q != NULL; q = q->next)
879 if (q->sec == p->sec)
880 {
881 q->pc_count += p->pc_count;
882 q->count += p->count;
883 *pp = p->next;
884 break;
885 }
886 if (q == NULL)
887 pp = &p->next;
888 }
889 *pp = edir->dyn_relocs;
890 }
891
892 edir->dyn_relocs = eind->dyn_relocs;
893 eind->dyn_relocs = NULL;
894 }
895
896 if (ind->root.type == bfd_link_hash_indirect
897 && dir->got.refcount <= 0)
898 {
899 edir->tls_type = eind->tls_type;
900 eind->tls_type = GOT_UNKNOWN;
901 }
902
903 if (ELIMINATE_COPY_RELOCS
904 && ind->root.type != bfd_link_hash_indirect
905 && dir->dynamic_adjusted)
906 {
907 /* If called to transfer flags for a weakdef during processing
908 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
909 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
910 dir->ref_dynamic |= ind->ref_dynamic;
911 dir->ref_regular |= ind->ref_regular;
912 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
913 dir->needs_plt |= ind->needs_plt;
914 dir->pointer_equality_needed |= ind->pointer_equality_needed;
915 }
916 else
917 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
918 }
919
920 static bfd_boolean
921 elf64_x86_64_elf_object_p (bfd *abfd)
922 {
923 /* Set the right machine number for an x86-64 elf64 file. */
924 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
925 return TRUE;
926 }
927
928 typedef union
929 {
930 unsigned char c[2];
931 uint16_t i;
932 }
933 x86_64_opcode16;
934
935 typedef union
936 {
937 unsigned char c[4];
938 uint32_t i;
939 }
940 x86_64_opcode32;
941
942 /* Return TRUE if the TLS access code sequence support transition
943 from R_TYPE. */
944
945 static bfd_boolean
946 elf_x86_64_check_tls_transition (bfd *abfd,
947 struct bfd_link_info *info,
948 asection *sec,
949 bfd_byte *contents,
950 Elf_Internal_Shdr *symtab_hdr,
951 struct elf_link_hash_entry **sym_hashes,
952 unsigned int r_type,
953 const Elf_Internal_Rela *rel,
954 const Elf_Internal_Rela *relend)
955 {
956 unsigned int val;
957 unsigned long r_symndx;
958 struct elf_link_hash_entry *h;
959 bfd_vma offset;
960 struct elf_x86_64_link_hash_table *htab;
961
962 /* Get the section contents. */
963 if (contents == NULL)
964 {
965 if (elf_section_data (sec)->this_hdr.contents != NULL)
966 contents = elf_section_data (sec)->this_hdr.contents;
967 else
968 {
969 /* FIXME: How to better handle error condition? */
970 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
971 return FALSE;
972
973 /* Cache the section contents for elf_link_input_bfd. */
974 elf_section_data (sec)->this_hdr.contents = contents;
975 }
976 }
977
978 htab = elf_x86_64_hash_table (info);
979 offset = rel->r_offset;
980 switch (r_type)
981 {
982 case R_X86_64_TLSGD:
983 case R_X86_64_TLSLD:
984 if ((rel + 1) >= relend)
985 return FALSE;
986
987 if (r_type == R_X86_64_TLSGD)
988 {
989 /* Check transition from GD access model. For 64bit, only
990 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
991 .word 0x6666; rex64; call __tls_get_addr
992 can transit to different access model. For 32bit, only
993 leaq foo@tlsgd(%rip), %rdi
994 .word 0x6666; rex64; call __tls_get_addr
995 can transit to different access model. */
996
997 static x86_64_opcode32 call = { { 0x66, 0x66, 0x48, 0xe8 } };
998 if ((offset + 12) > sec->size
999 || bfd_get_32 (abfd, contents + offset + 4) != call.i)
1000 return FALSE;
1001
1002 if (ABI_64_P (abfd))
1003 {
1004 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } };
1005 if (offset < 4
1006 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i)
1007 return FALSE;
1008 }
1009 else
1010 {
1011 static x86_64_opcode16 lea = { { 0x8d, 0x3d } };
1012 if (offset < 3
1013 || bfd_get_8 (abfd, contents + offset - 3) != 0x48
1014 || bfd_get_16 (abfd, contents + offset - 2) != lea.i)
1015 return FALSE;
1016 }
1017 }
1018 else
1019 {
1020 /* Check transition from LD access model. Only
1021 leaq foo@tlsld(%rip), %rdi;
1022 call __tls_get_addr
1023 can transit to different access model. */
1024
1025 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
1026 x86_64_opcode32 op;
1027
1028 if (offset < 3 || (offset + 9) > sec->size)
1029 return FALSE;
1030
1031 op.i = bfd_get_32 (abfd, contents + offset - 3);
1032 op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
1033 if (op.i != ld.i)
1034 return FALSE;
1035 }
1036
1037 r_symndx = htab->r_sym (rel[1].r_info);
1038 if (r_symndx < symtab_hdr->sh_info)
1039 return FALSE;
1040
1041 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1042 /* Use strncmp to check __tls_get_addr since __tls_get_addr
1043 may be versioned. */
1044 return (h != NULL
1045 && h->root.root.string != NULL
1046 && (ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PC32
1047 || ELF32_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
1048 && (strncmp (h->root.root.string,
1049 "__tls_get_addr", 14) == 0));
1050
1051 case R_X86_64_GOTTPOFF:
1052 /* Check transition from IE access model:
1053 mov foo@gottpoff(%rip), %reg
1054 add foo@gottpoff(%rip), %reg
1055 */
1056
1057 /* Check REX prefix first. */
1058 if (offset >= 3 && (offset + 4) <= sec->size)
1059 {
1060 val = bfd_get_8 (abfd, contents + offset - 3);
1061 if (val != 0x48 && val != 0x4c)
1062 {
1063 /* X32 may have 0x44 REX prefix or no REX prefix. */
1064 if (ABI_64_P (abfd))
1065 return FALSE;
1066 }
1067 }
1068 else
1069 {
1070 /* X32 may not have any REX prefix. */
1071 if (ABI_64_P (abfd))
1072 return FALSE;
1073 if (offset < 2 || (offset + 3) > sec->size)
1074 return FALSE;
1075 }
1076
1077 val = bfd_get_8 (abfd, contents + offset - 2);
1078 if (val != 0x8b && val != 0x03)
1079 return FALSE;
1080
1081 val = bfd_get_8 (abfd, contents + offset - 1);
1082 return (val & 0xc7) == 5;
1083
1084 case R_X86_64_GOTPC32_TLSDESC:
1085 /* Check transition from GDesc access model:
1086 leaq x@tlsdesc(%rip), %rax
1087
1088 Make sure it's a leaq adding rip to a 32-bit offset
1089 into any register, although it's probably almost always
1090 going to be rax. */
1091
1092 if (offset < 3 || (offset + 4) > sec->size)
1093 return FALSE;
1094
1095 val = bfd_get_8 (abfd, contents + offset - 3);
1096 if ((val & 0xfb) != 0x48)
1097 return FALSE;
1098
1099 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
1100 return FALSE;
1101
1102 val = bfd_get_8 (abfd, contents + offset - 1);
1103 return (val & 0xc7) == 0x05;
1104
1105 case R_X86_64_TLSDESC_CALL:
1106 /* Check transition from GDesc access model:
1107 call *x@tlsdesc(%rax)
1108 */
1109 if (offset + 2 <= sec->size)
1110 {
1111 /* Make sure that it's a call *x@tlsdesc(%rax). */
1112 static x86_64_opcode16 call = { { 0xff, 0x10 } };
1113 return bfd_get_16 (abfd, contents + offset) == call.i;
1114 }
1115
1116 return FALSE;
1117
1118 default:
1119 abort ();
1120 }
1121 }
1122
1123 /* Return TRUE if the TLS access transition is OK or no transition
1124 will be performed. Update R_TYPE if there is a transition. */
1125
1126 static bfd_boolean
1127 elf_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
1128 asection *sec, bfd_byte *contents,
1129 Elf_Internal_Shdr *symtab_hdr,
1130 struct elf_link_hash_entry **sym_hashes,
1131 unsigned int *r_type, int tls_type,
1132 const Elf_Internal_Rela *rel,
1133 const Elf_Internal_Rela *relend,
1134 struct elf_link_hash_entry *h,
1135 unsigned long r_symndx)
1136 {
1137 unsigned int from_type = *r_type;
1138 unsigned int to_type = from_type;
1139 bfd_boolean check = TRUE;
1140
1141 /* Skip TLS transition for functions. */
1142 if (h != NULL
1143 && (h->type == STT_FUNC
1144 || h->type == STT_GNU_IFUNC))
1145 return TRUE;
1146
1147 switch (from_type)
1148 {
1149 case R_X86_64_TLSGD:
1150 case R_X86_64_GOTPC32_TLSDESC:
1151 case R_X86_64_TLSDESC_CALL:
1152 case R_X86_64_GOTTPOFF:
1153 if (info->executable)
1154 {
1155 if (h == NULL)
1156 to_type = R_X86_64_TPOFF32;
1157 else
1158 to_type = R_X86_64_GOTTPOFF;
1159 }
1160
1161 /* When we are called from elf_x86_64_relocate_section,
1162 CONTENTS isn't NULL and there may be additional transitions
1163 based on TLS_TYPE. */
1164 if (contents != NULL)
1165 {
1166 unsigned int new_to_type = to_type;
1167
1168 if (info->executable
1169 && h != NULL
1170 && h->dynindx == -1
1171 && tls_type == GOT_TLS_IE)
1172 new_to_type = R_X86_64_TPOFF32;
1173
1174 if (to_type == R_X86_64_TLSGD
1175 || to_type == R_X86_64_GOTPC32_TLSDESC
1176 || to_type == R_X86_64_TLSDESC_CALL)
1177 {
1178 if (tls_type == GOT_TLS_IE)
1179 new_to_type = R_X86_64_GOTTPOFF;
1180 }
1181
1182 /* We checked the transition before when we were called from
1183 elf_x86_64_check_relocs. We only want to check the new
1184 transition which hasn't been checked before. */
1185 check = new_to_type != to_type && from_type == to_type;
1186 to_type = new_to_type;
1187 }
1188
1189 break;
1190
1191 case R_X86_64_TLSLD:
1192 if (info->executable)
1193 to_type = R_X86_64_TPOFF32;
1194 break;
1195
1196 default:
1197 return TRUE;
1198 }
1199
1200 /* Return TRUE if there is no transition. */
1201 if (from_type == to_type)
1202 return TRUE;
1203
1204 /* Check if the transition can be performed. */
1205 if (check
1206 && ! elf_x86_64_check_tls_transition (abfd, info, sec, contents,
1207 symtab_hdr, sym_hashes,
1208 from_type, rel, relend))
1209 {
1210 reloc_howto_type *from, *to;
1211 const char *name;
1212
1213 from = elf_x86_64_rtype_to_howto (abfd, from_type);
1214 to = elf_x86_64_rtype_to_howto (abfd, to_type);
1215
1216 if (h)
1217 name = h->root.root.string;
1218 else
1219 {
1220 struct elf_x86_64_link_hash_table *htab;
1221
1222 htab = elf_x86_64_hash_table (info);
1223 if (htab == NULL)
1224 name = "*unknown*";
1225 else
1226 {
1227 Elf_Internal_Sym *isym;
1228
1229 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1230 abfd, r_symndx);
1231 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1232 }
1233 }
1234
1235 (*_bfd_error_handler)
1236 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
1237 "in section `%A' failed"),
1238 abfd, sec, from->name, to->name, name,
1239 (unsigned long) rel->r_offset);
1240 bfd_set_error (bfd_error_bad_value);
1241 return FALSE;
1242 }
1243
1244 *r_type = to_type;
1245 return TRUE;
1246 }
1247
1248 /* Look through the relocs for a section during the first phase, and
1249 calculate needed space in the global offset table, procedure
1250 linkage table, and dynamic reloc sections. */
1251
1252 static bfd_boolean
1253 elf_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1254 asection *sec,
1255 const Elf_Internal_Rela *relocs)
1256 {
1257 struct elf_x86_64_link_hash_table *htab;
1258 Elf_Internal_Shdr *symtab_hdr;
1259 struct elf_link_hash_entry **sym_hashes;
1260 const Elf_Internal_Rela *rel;
1261 const Elf_Internal_Rela *rel_end;
1262 asection *sreloc;
1263
1264 if (info->relocatable)
1265 return TRUE;
1266
1267 BFD_ASSERT (is_x86_64_elf (abfd));
1268
1269 htab = elf_x86_64_hash_table (info);
1270 if (htab == NULL)
1271 return FALSE;
1272
1273 symtab_hdr = &elf_symtab_hdr (abfd);
1274 sym_hashes = elf_sym_hashes (abfd);
1275
1276 sreloc = NULL;
1277
1278 rel_end = relocs + sec->reloc_count;
1279 for (rel = relocs; rel < rel_end; rel++)
1280 {
1281 unsigned int r_type;
1282 unsigned long r_symndx;
1283 struct elf_link_hash_entry *h;
1284 Elf_Internal_Sym *isym;
1285 const char *name;
1286
1287 r_symndx = htab->r_sym (rel->r_info);
1288 r_type = ELF32_R_TYPE (rel->r_info);
1289
1290 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1291 {
1292 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1293 abfd, r_symndx);
1294 return FALSE;
1295 }
1296
1297 if (r_symndx < symtab_hdr->sh_info)
1298 {
1299 /* A local symbol. */
1300 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1301 abfd, r_symndx);
1302 if (isym == NULL)
1303 return FALSE;
1304
1305 /* Check relocation against local STT_GNU_IFUNC symbol. */
1306 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1307 {
1308 h = elf_x86_64_get_local_sym_hash (htab, abfd, rel,
1309 TRUE);
1310 if (h == NULL)
1311 return FALSE;
1312
1313 /* Fake a STT_GNU_IFUNC symbol. */
1314 h->type = STT_GNU_IFUNC;
1315 h->def_regular = 1;
1316 h->ref_regular = 1;
1317 h->forced_local = 1;
1318 h->root.type = bfd_link_hash_defined;
1319 }
1320 else
1321 h = NULL;
1322 }
1323 else
1324 {
1325 isym = NULL;
1326 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1327 while (h->root.type == bfd_link_hash_indirect
1328 || h->root.type == bfd_link_hash_warning)
1329 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1330 }
1331
1332 /* Check invalid x32 relocations. */
1333 if (!ABI_64_P (abfd))
1334 switch (r_type)
1335 {
1336 default:
1337 break;
1338
1339 case R_X86_64_64:
1340 /* Allow R_X86_64_64 relocations in SEC_DEBUGGING sections
1341 when building shared libraries. */
1342 if (info->shared
1343 && !info->executable
1344 && (sec->flags & SEC_DEBUGGING) != 0)
1345 break;
1346
1347 case R_X86_64_DTPOFF64:
1348 case R_X86_64_TPOFF64:
1349 case R_X86_64_PC64:
1350 case R_X86_64_GOTOFF64:
1351 case R_X86_64_GOT64:
1352 case R_X86_64_GOTPCREL64:
1353 case R_X86_64_GOTPC64:
1354 case R_X86_64_GOTPLT64:
1355 case R_X86_64_PLTOFF64:
1356 {
1357 if (h)
1358 name = h->root.root.string;
1359 else
1360 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1361 NULL);
1362 (*_bfd_error_handler)
1363 (_("%B: relocation %s against symbol `%s' isn't "
1364 "supported in x32 mode"), abfd,
1365 x86_64_elf_howto_table[r_type].name, name);
1366 bfd_set_error (bfd_error_bad_value);
1367 return FALSE;
1368 }
1369 break;
1370 }
1371
1372 if (h != NULL)
1373 {
1374 /* Create the ifunc sections for static executables. If we
1375 never see an indirect function symbol nor we are building
1376 a static executable, those sections will be empty and
1377 won't appear in output. */
1378 switch (r_type)
1379 {
1380 default:
1381 break;
1382
1383 case R_X86_64_32S:
1384 case R_X86_64_32:
1385 case R_X86_64_64:
1386 case R_X86_64_PC32:
1387 case R_X86_64_PC64:
1388 case R_X86_64_PLT32:
1389 case R_X86_64_GOTPCREL:
1390 case R_X86_64_GOTPCREL64:
1391 if (htab->elf.dynobj == NULL)
1392 htab->elf.dynobj = abfd;
1393 if (!_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info))
1394 return FALSE;
1395 break;
1396 }
1397
1398 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1399 it here if it is defined in a non-shared object. */
1400 if (h->type == STT_GNU_IFUNC
1401 && h->def_regular)
1402 {
1403 /* It is referenced by a non-shared object. */
1404 h->ref_regular = 1;
1405 h->needs_plt = 1;
1406
1407 /* STT_GNU_IFUNC symbol must go through PLT. */
1408 h->plt.refcount += 1;
1409
1410 /* STT_GNU_IFUNC needs dynamic sections. */
1411 if (htab->elf.dynobj == NULL)
1412 htab->elf.dynobj = abfd;
1413
1414 switch (r_type)
1415 {
1416 default:
1417 if (h->root.root.string)
1418 name = h->root.root.string;
1419 else
1420 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1421 NULL);
1422 (*_bfd_error_handler)
1423 (_("%B: relocation %s against STT_GNU_IFUNC "
1424 "symbol `%s' isn't handled by %s"), abfd,
1425 x86_64_elf_howto_table[r_type].name,
1426 name, __FUNCTION__);
1427 bfd_set_error (bfd_error_bad_value);
1428 return FALSE;
1429
1430 case R_X86_64_32:
1431 if (ABI_64_P (abfd))
1432 goto not_pointer;
1433 case R_X86_64_64:
1434 h->non_got_ref = 1;
1435 h->pointer_equality_needed = 1;
1436 if (info->shared)
1437 {
1438 /* We must copy these reloc types into the output
1439 file. Create a reloc section in dynobj and
1440 make room for this reloc. */
1441 sreloc = _bfd_elf_create_ifunc_dyn_reloc
1442 (abfd, info, sec, sreloc,
1443 &((struct elf_x86_64_link_hash_entry *) h)->dyn_relocs);
1444 if (sreloc == NULL)
1445 return FALSE;
1446 }
1447 break;
1448
1449 case R_X86_64_32S:
1450 case R_X86_64_PC32:
1451 case R_X86_64_PC64:
1452 not_pointer:
1453 h->non_got_ref = 1;
1454 if (r_type != R_X86_64_PC32
1455 && r_type != R_X86_64_PC64)
1456 h->pointer_equality_needed = 1;
1457 break;
1458
1459 case R_X86_64_PLT32:
1460 break;
1461
1462 case R_X86_64_GOTPCREL:
1463 case R_X86_64_GOTPCREL64:
1464 h->got.refcount += 1;
1465 if (htab->elf.sgot == NULL
1466 && !_bfd_elf_create_got_section (htab->elf.dynobj,
1467 info))
1468 return FALSE;
1469 break;
1470 }
1471
1472 continue;
1473 }
1474 }
1475
1476 if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
1477 symtab_hdr, sym_hashes,
1478 &r_type, GOT_UNKNOWN,
1479 rel, rel_end, h, r_symndx))
1480 return FALSE;
1481
1482 switch (r_type)
1483 {
1484 case R_X86_64_TLSLD:
1485 htab->tls_ld_got.refcount += 1;
1486 goto create_got;
1487
1488 case R_X86_64_TPOFF32:
1489 if (!info->executable && ABI_64_P (abfd))
1490 {
1491 if (h)
1492 name = h->root.root.string;
1493 else
1494 name = bfd_elf_sym_name (abfd, symtab_hdr, isym,
1495 NULL);
1496 (*_bfd_error_handler)
1497 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1498 abfd,
1499 x86_64_elf_howto_table[r_type].name, name);
1500 bfd_set_error (bfd_error_bad_value);
1501 return FALSE;
1502 }
1503 break;
1504
1505 case R_X86_64_GOTTPOFF:
1506 if (!info->executable)
1507 info->flags |= DF_STATIC_TLS;
1508 /* Fall through */
1509
1510 case R_X86_64_GOT32:
1511 case R_X86_64_GOTPCREL:
1512 case R_X86_64_TLSGD:
1513 case R_X86_64_GOT64:
1514 case R_X86_64_GOTPCREL64:
1515 case R_X86_64_GOTPLT64:
1516 case R_X86_64_GOTPC32_TLSDESC:
1517 case R_X86_64_TLSDESC_CALL:
1518 /* This symbol requires a global offset table entry. */
1519 {
1520 int tls_type, old_tls_type;
1521
1522 switch (r_type)
1523 {
1524 default: tls_type = GOT_NORMAL; break;
1525 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1526 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1527 case R_X86_64_GOTPC32_TLSDESC:
1528 case R_X86_64_TLSDESC_CALL:
1529 tls_type = GOT_TLS_GDESC; break;
1530 }
1531
1532 if (h != NULL)
1533 {
1534 if (r_type == R_X86_64_GOTPLT64)
1535 {
1536 /* This relocation indicates that we also need
1537 a PLT entry, as this is a function. We don't need
1538 a PLT entry for local symbols. */
1539 h->needs_plt = 1;
1540 h->plt.refcount += 1;
1541 }
1542 h->got.refcount += 1;
1543 old_tls_type = elf_x86_64_hash_entry (h)->tls_type;
1544 }
1545 else
1546 {
1547 bfd_signed_vma *local_got_refcounts;
1548
1549 /* This is a global offset table entry for a local symbol. */
1550 local_got_refcounts = elf_local_got_refcounts (abfd);
1551 if (local_got_refcounts == NULL)
1552 {
1553 bfd_size_type size;
1554
1555 size = symtab_hdr->sh_info;
1556 size *= sizeof (bfd_signed_vma)
1557 + sizeof (bfd_vma) + sizeof (char);
1558 local_got_refcounts = ((bfd_signed_vma *)
1559 bfd_zalloc (abfd, size));
1560 if (local_got_refcounts == NULL)
1561 return FALSE;
1562 elf_local_got_refcounts (abfd) = local_got_refcounts;
1563 elf_x86_64_local_tlsdesc_gotent (abfd)
1564 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1565 elf_x86_64_local_got_tls_type (abfd)
1566 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1567 }
1568 local_got_refcounts[r_symndx] += 1;
1569 old_tls_type
1570 = elf_x86_64_local_got_tls_type (abfd) [r_symndx];
1571 }
1572
1573 /* If a TLS symbol is accessed using IE at least once,
1574 there is no point to use dynamic model for it. */
1575 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1576 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1577 || tls_type != GOT_TLS_IE))
1578 {
1579 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1580 tls_type = old_tls_type;
1581 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1582 && GOT_TLS_GD_ANY_P (tls_type))
1583 tls_type |= old_tls_type;
1584 else
1585 {
1586 if (h)
1587 name = h->root.root.string;
1588 else
1589 name = bfd_elf_sym_name (abfd, symtab_hdr,
1590 isym, NULL);
1591 (*_bfd_error_handler)
1592 (_("%B: '%s' accessed both as normal and thread local symbol"),
1593 abfd, name);
1594 return FALSE;
1595 }
1596 }
1597
1598 if (old_tls_type != tls_type)
1599 {
1600 if (h != NULL)
1601 elf_x86_64_hash_entry (h)->tls_type = tls_type;
1602 else
1603 elf_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1604 }
1605 }
1606 /* Fall through */
1607
1608 case R_X86_64_GOTOFF64:
1609 case R_X86_64_GOTPC32:
1610 case R_X86_64_GOTPC64:
1611 create_got:
1612 if (htab->elf.sgot == NULL)
1613 {
1614 if (htab->elf.dynobj == NULL)
1615 htab->elf.dynobj = abfd;
1616 if (!_bfd_elf_create_got_section (htab->elf.dynobj,
1617 info))
1618 return FALSE;
1619 }
1620 break;
1621
1622 case R_X86_64_PLT32:
1623 /* This symbol requires a procedure linkage table entry. We
1624 actually build the entry in adjust_dynamic_symbol,
1625 because this might be a case of linking PIC code which is
1626 never referenced by a dynamic object, in which case we
1627 don't need to generate a procedure linkage table entry
1628 after all. */
1629
1630 /* If this is a local symbol, we resolve it directly without
1631 creating a procedure linkage table entry. */
1632 if (h == NULL)
1633 continue;
1634
1635 h->needs_plt = 1;
1636 h->plt.refcount += 1;
1637 break;
1638
1639 case R_X86_64_PLTOFF64:
1640 /* This tries to form the 'address' of a function relative
1641 to GOT. For global symbols we need a PLT entry. */
1642 if (h != NULL)
1643 {
1644 h->needs_plt = 1;
1645 h->plt.refcount += 1;
1646 }
1647 goto create_got;
1648
1649 case R_X86_64_32:
1650 if (!ABI_64_P (abfd))
1651 goto pointer;
1652 case R_X86_64_8:
1653 case R_X86_64_16:
1654 case R_X86_64_32S:
1655 /* Let's help debug shared library creation. These relocs
1656 cannot be used in shared libs. Don't error out for
1657 sections we don't care about, such as debug sections or
1658 non-constant sections. */
1659 if (info->shared
1660 && (sec->flags & SEC_ALLOC) != 0
1661 && (sec->flags & SEC_READONLY) != 0)
1662 {
1663 if (h)
1664 name = h->root.root.string;
1665 else
1666 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);
1667 (*_bfd_error_handler)
1668 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1669 abfd, x86_64_elf_howto_table[r_type].name, name);
1670 bfd_set_error (bfd_error_bad_value);
1671 return FALSE;
1672 }
1673 /* Fall through. */
1674
1675 case R_X86_64_PC8:
1676 case R_X86_64_PC16:
1677 case R_X86_64_PC32:
1678 case R_X86_64_PC64:
1679 case R_X86_64_64:
1680 pointer:
1681 if (h != NULL && info->executable)
1682 {
1683 /* If this reloc is in a read-only section, we might
1684 need a copy reloc. We can't check reliably at this
1685 stage whether the section is read-only, as input
1686 sections have not yet been mapped to output sections.
1687 Tentatively set the flag for now, and correct in
1688 adjust_dynamic_symbol. */
1689 h->non_got_ref = 1;
1690
1691 /* We may need a .plt entry if the function this reloc
1692 refers to is in a shared lib. */
1693 h->plt.refcount += 1;
1694 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1695 h->pointer_equality_needed = 1;
1696 }
1697
1698 /* If we are creating a shared library, and this is a reloc
1699 against a global symbol, or a non PC relative reloc
1700 against a local symbol, then we need to copy the reloc
1701 into the shared library. However, if we are linking with
1702 -Bsymbolic, we do not need to copy a reloc against a
1703 global symbol which is defined in an object we are
1704 including in the link (i.e., DEF_REGULAR is set). At
1705 this point we have not seen all the input files, so it is
1706 possible that DEF_REGULAR is not set now but will be set
1707 later (it is never cleared). In case of a weak definition,
1708 DEF_REGULAR may be cleared later by a strong definition in
1709 a shared library. We account for that possibility below by
1710 storing information in the relocs_copied field of the hash
1711 table entry. A similar situation occurs when creating
1712 shared libraries and symbol visibility changes render the
1713 symbol local.
1714
1715 If on the other hand, we are creating an executable, we
1716 may need to keep relocations for symbols satisfied by a
1717 dynamic library if we manage to avoid copy relocs for the
1718 symbol. */
1719 if ((info->shared
1720 && (sec->flags & SEC_ALLOC) != 0
1721 && (! IS_X86_64_PCREL_TYPE (r_type)
1722 || (h != NULL
1723 && (! SYMBOLIC_BIND (info, h)
1724 || h->root.type == bfd_link_hash_defweak
1725 || !h->def_regular))))
1726 || (ELIMINATE_COPY_RELOCS
1727 && !info->shared
1728 && (sec->flags & SEC_ALLOC) != 0
1729 && h != NULL
1730 && (h->root.type == bfd_link_hash_defweak
1731 || !h->def_regular)))
1732 {
1733 struct elf_dyn_relocs *p;
1734 struct elf_dyn_relocs **head;
1735
1736 /* We must copy these reloc types into the output file.
1737 Create a reloc section in dynobj and make room for
1738 this reloc. */
1739 if (sreloc == NULL)
1740 {
1741 if (htab->elf.dynobj == NULL)
1742 htab->elf.dynobj = abfd;
1743
1744 sreloc = _bfd_elf_make_dynamic_reloc_section
1745 (sec, htab->elf.dynobj, ABI_64_P (abfd) ? 3 : 2,
1746 abfd, /*rela?*/ TRUE);
1747
1748 if (sreloc == NULL)
1749 return FALSE;
1750 }
1751
1752 /* If this is a global symbol, we count the number of
1753 relocations we need for this symbol. */
1754 if (h != NULL)
1755 {
1756 head = &((struct elf_x86_64_link_hash_entry *) h)->dyn_relocs;
1757 }
1758 else
1759 {
1760 /* Track dynamic relocs needed for local syms too.
1761 We really need local syms available to do this
1762 easily. Oh well. */
1763 asection *s;
1764 void **vpp;
1765
1766 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1767 abfd, r_symndx);
1768 if (isym == NULL)
1769 return FALSE;
1770
1771 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
1772 if (s == NULL)
1773 s = sec;
1774
1775 /* Beware of type punned pointers vs strict aliasing
1776 rules. */
1777 vpp = &(elf_section_data (s)->local_dynrel);
1778 head = (struct elf_dyn_relocs **)vpp;
1779 }
1780
1781 p = *head;
1782 if (p == NULL || p->sec != sec)
1783 {
1784 bfd_size_type amt = sizeof *p;
1785
1786 p = ((struct elf_dyn_relocs *)
1787 bfd_alloc (htab->elf.dynobj, amt));
1788 if (p == NULL)
1789 return FALSE;
1790 p->next = *head;
1791 *head = p;
1792 p->sec = sec;
1793 p->count = 0;
1794 p->pc_count = 0;
1795 }
1796
1797 p->count += 1;
1798 if (IS_X86_64_PCREL_TYPE (r_type))
1799 p->pc_count += 1;
1800 }
1801 break;
1802
1803 /* This relocation describes the C++ object vtable hierarchy.
1804 Reconstruct it for later use during GC. */
1805 case R_X86_64_GNU_VTINHERIT:
1806 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1807 return FALSE;
1808 break;
1809
1810 /* This relocation describes which C++ vtable entries are actually
1811 used. Record for later use during GC. */
1812 case R_X86_64_GNU_VTENTRY:
1813 BFD_ASSERT (h != NULL);
1814 if (h != NULL
1815 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1816 return FALSE;
1817 break;
1818
1819 default:
1820 break;
1821 }
1822 }
1823
1824 return TRUE;
1825 }
1826
1827 /* Return the section that should be marked against GC for a given
1828 relocation. */
1829
1830 static asection *
1831 elf_x86_64_gc_mark_hook (asection *sec,
1832 struct bfd_link_info *info,
1833 Elf_Internal_Rela *rel,
1834 struct elf_link_hash_entry *h,
1835 Elf_Internal_Sym *sym)
1836 {
1837 if (h != NULL)
1838 switch (ELF32_R_TYPE (rel->r_info))
1839 {
1840 case R_X86_64_GNU_VTINHERIT:
1841 case R_X86_64_GNU_VTENTRY:
1842 return NULL;
1843 }
1844
1845 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1846 }
1847
1848 /* Update the got entry reference counts for the section being removed. */
1849
1850 static bfd_boolean
1851 elf_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1852 asection *sec,
1853 const Elf_Internal_Rela *relocs)
1854 {
1855 struct elf_x86_64_link_hash_table *htab;
1856 Elf_Internal_Shdr *symtab_hdr;
1857 struct elf_link_hash_entry **sym_hashes;
1858 bfd_signed_vma *local_got_refcounts;
1859 const Elf_Internal_Rela *rel, *relend;
1860
1861 if (info->relocatable)
1862 return TRUE;
1863
1864 htab = elf_x86_64_hash_table (info);
1865 if (htab == NULL)
1866 return FALSE;
1867
1868 elf_section_data (sec)->local_dynrel = NULL;
1869
1870 symtab_hdr = &elf_symtab_hdr (abfd);
1871 sym_hashes = elf_sym_hashes (abfd);
1872 local_got_refcounts = elf_local_got_refcounts (abfd);
1873
1874 htab = elf_x86_64_hash_table (info);
1875 relend = relocs + sec->reloc_count;
1876 for (rel = relocs; rel < relend; rel++)
1877 {
1878 unsigned long r_symndx;
1879 unsigned int r_type;
1880 struct elf_link_hash_entry *h = NULL;
1881
1882 r_symndx = htab->r_sym (rel->r_info);
1883 if (r_symndx >= symtab_hdr->sh_info)
1884 {
1885 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1886 while (h->root.type == bfd_link_hash_indirect
1887 || h->root.type == bfd_link_hash_warning)
1888 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1889 }
1890 else
1891 {
1892 /* A local symbol. */
1893 Elf_Internal_Sym *isym;
1894
1895 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
1896 abfd, r_symndx);
1897
1898 /* Check relocation against local STT_GNU_IFUNC symbol. */
1899 if (isym != NULL
1900 && ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
1901 {
1902 h = elf_x86_64_get_local_sym_hash (htab, abfd, rel, FALSE);
1903 if (h == NULL)
1904 abort ();
1905 }
1906 }
1907
1908 if (h)
1909 {
1910 struct elf_x86_64_link_hash_entry *eh;
1911 struct elf_dyn_relocs **pp;
1912 struct elf_dyn_relocs *p;
1913
1914 eh = (struct elf_x86_64_link_hash_entry *) h;
1915
1916 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1917 if (p->sec == sec)
1918 {
1919 /* Everything must go for SEC. */
1920 *pp = p->next;
1921 break;
1922 }
1923 }
1924
1925 r_type = ELF32_R_TYPE (rel->r_info);
1926 if (! elf_x86_64_tls_transition (info, abfd, sec, NULL,
1927 symtab_hdr, sym_hashes,
1928 &r_type, GOT_UNKNOWN,
1929 rel, relend, h, r_symndx))
1930 return FALSE;
1931
1932 switch (r_type)
1933 {
1934 case R_X86_64_TLSLD:
1935 if (htab->tls_ld_got.refcount > 0)
1936 htab->tls_ld_got.refcount -= 1;
1937 break;
1938
1939 case R_X86_64_TLSGD:
1940 case R_X86_64_GOTPC32_TLSDESC:
1941 case R_X86_64_TLSDESC_CALL:
1942 case R_X86_64_GOTTPOFF:
1943 case R_X86_64_GOT32:
1944 case R_X86_64_GOTPCREL:
1945 case R_X86_64_GOT64:
1946 case R_X86_64_GOTPCREL64:
1947 case R_X86_64_GOTPLT64:
1948 if (h != NULL)
1949 {
1950 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1951 h->plt.refcount -= 1;
1952 if (h->got.refcount > 0)
1953 h->got.refcount -= 1;
1954 if (h->type == STT_GNU_IFUNC)
1955 {
1956 if (h->plt.refcount > 0)
1957 h->plt.refcount -= 1;
1958 }
1959 }
1960 else if (local_got_refcounts != NULL)
1961 {
1962 if (local_got_refcounts[r_symndx] > 0)
1963 local_got_refcounts[r_symndx] -= 1;
1964 }
1965 break;
1966
1967 case R_X86_64_8:
1968 case R_X86_64_16:
1969 case R_X86_64_32:
1970 case R_X86_64_64:
1971 case R_X86_64_32S:
1972 case R_X86_64_PC8:
1973 case R_X86_64_PC16:
1974 case R_X86_64_PC32:
1975 case R_X86_64_PC64:
1976 if (info->shared
1977 && (h == NULL || h->type != STT_GNU_IFUNC))
1978 break;
1979 /* Fall thru */
1980
1981 case R_X86_64_PLT32:
1982 case R_X86_64_PLTOFF64:
1983 if (h != NULL)
1984 {
1985 if (h->plt.refcount > 0)
1986 h->plt.refcount -= 1;
1987 }
1988 break;
1989
1990 default:
1991 break;
1992 }
1993 }
1994
1995 return TRUE;
1996 }
1997
1998 /* Adjust a symbol defined by a dynamic object and referenced by a
1999 regular object. The current definition is in some section of the
2000 dynamic object, but we're not including those sections. We have to
2001 change the definition to something the rest of the link can
2002 understand. */
2003
2004 static bfd_boolean
2005 elf_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
2006 struct elf_link_hash_entry *h)
2007 {
2008 struct elf_x86_64_link_hash_table *htab;
2009 asection *s;
2010
2011 /* STT_GNU_IFUNC symbol must go through PLT. */
2012 if (h->type == STT_GNU_IFUNC)
2013 {
2014 if (h->plt.refcount <= 0)
2015 {
2016 h->plt.offset = (bfd_vma) -1;
2017 h->needs_plt = 0;
2018 }
2019 return TRUE;
2020 }
2021
2022 /* If this is a function, put it in the procedure linkage table. We
2023 will fill in the contents of the procedure linkage table later,
2024 when we know the address of the .got section. */
2025 if (h->type == STT_FUNC
2026 || h->needs_plt)
2027 {
2028 if (h->plt.refcount <= 0
2029 || SYMBOL_CALLS_LOCAL (info, h)
2030 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2031 && h->root.type == bfd_link_hash_undefweak))
2032 {
2033 /* This case can occur if we saw a PLT32 reloc in an input
2034 file, but the symbol was never referred to by a dynamic
2035 object, or if all references were garbage collected. In
2036 such a case, we don't actually need to build a procedure
2037 linkage table, and we can just do a PC32 reloc instead. */
2038 h->plt.offset = (bfd_vma) -1;
2039 h->needs_plt = 0;
2040 }
2041
2042 return TRUE;
2043 }
2044 else
2045 /* It's possible that we incorrectly decided a .plt reloc was
2046 needed for an R_X86_64_PC32 reloc to a non-function sym in
2047 check_relocs. We can't decide accurately between function and
2048 non-function syms in check-relocs; Objects loaded later in
2049 the link may change h->type. So fix it now. */
2050 h->plt.offset = (bfd_vma) -1;
2051
2052 /* If this is a weak symbol, and there is a real definition, the
2053 processor independent code will have arranged for us to see the
2054 real definition first, and we can just use the same value. */
2055 if (h->u.weakdef != NULL)
2056 {
2057 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
2058 || h->u.weakdef->root.type == bfd_link_hash_defweak);
2059 h->root.u.def.section = h->u.weakdef->root.u.def.section;
2060 h->root.u.def.value = h->u.weakdef->root.u.def.value;
2061 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
2062 h->non_got_ref = h->u.weakdef->non_got_ref;
2063 return TRUE;
2064 }
2065
2066 /* This is a reference to a symbol defined by a dynamic object which
2067 is not a function. */
2068
2069 /* If we are creating a shared library, we must presume that the
2070 only references to the symbol are via the global offset table.
2071 For such cases we need not do anything here; the relocations will
2072 be handled correctly by relocate_section. */
2073 if (info->shared)
2074 return TRUE;
2075
2076 /* If there are no references to this symbol that do not use the
2077 GOT, we don't need to generate a copy reloc. */
2078 if (!h->non_got_ref)
2079 return TRUE;
2080
2081 /* If -z nocopyreloc was given, we won't generate them either. */
2082 if (info->nocopyreloc)
2083 {
2084 h->non_got_ref = 0;
2085 return TRUE;
2086 }
2087
2088 if (ELIMINATE_COPY_RELOCS)
2089 {
2090 struct elf_x86_64_link_hash_entry * eh;
2091 struct elf_dyn_relocs *p;
2092
2093 eh = (struct elf_x86_64_link_hash_entry *) h;
2094 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2095 {
2096 s = p->sec->output_section;
2097 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2098 break;
2099 }
2100
2101 /* If we didn't find any dynamic relocs in read-only sections, then
2102 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
2103 if (p == NULL)
2104 {
2105 h->non_got_ref = 0;
2106 return TRUE;
2107 }
2108 }
2109
2110 if (h->size == 0)
2111 {
2112 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
2113 h->root.root.string);
2114 return TRUE;
2115 }
2116
2117 /* We must allocate the symbol in our .dynbss section, which will
2118 become part of the .bss section of the executable. There will be
2119 an entry for this symbol in the .dynsym section. The dynamic
2120 object will contain position independent code, so all references
2121 from the dynamic object to this symbol will go through the global
2122 offset table. The dynamic linker will use the .dynsym entry to
2123 determine the address it must put in the global offset table, so
2124 both the dynamic object and the regular object will refer to the
2125 same memory location for the variable. */
2126
2127 htab = elf_x86_64_hash_table (info);
2128 if (htab == NULL)
2129 return FALSE;
2130
2131 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
2132 to copy the initial value out of the dynamic object and into the
2133 runtime process image. */
2134 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2135 {
2136 const struct elf_backend_data *bed;
2137 bed = get_elf_backend_data (info->output_bfd);
2138 htab->srelbss->size += bed->s->sizeof_rela;
2139 h->needs_copy = 1;
2140 }
2141
2142 s = htab->sdynbss;
2143
2144 return _bfd_elf_adjust_dynamic_copy (h, s);
2145 }
2146
2147 /* Allocate space in .plt, .got and associated reloc sections for
2148 dynamic relocs. */
2149
2150 static bfd_boolean
2151 elf_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2152 {
2153 struct bfd_link_info *info;
2154 struct elf_x86_64_link_hash_table *htab;
2155 struct elf_x86_64_link_hash_entry *eh;
2156 struct elf_dyn_relocs *p;
2157 const struct elf_backend_data *bed;
2158
2159 if (h->root.type == bfd_link_hash_indirect)
2160 return TRUE;
2161
2162 eh = (struct elf_x86_64_link_hash_entry *) h;
2163
2164 info = (struct bfd_link_info *) inf;
2165 htab = elf_x86_64_hash_table (info);
2166 if (htab == NULL)
2167 return FALSE;
2168 bed = get_elf_backend_data (info->output_bfd);
2169
2170 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
2171 here if it is defined and referenced in a non-shared object. */
2172 if (h->type == STT_GNU_IFUNC
2173 && h->def_regular)
2174 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h,
2175 &eh->dyn_relocs,
2176 PLT_ENTRY_SIZE,
2177 GOT_ENTRY_SIZE);
2178 else if (htab->elf.dynamic_sections_created
2179 && h->plt.refcount > 0)
2180 {
2181 /* Make sure this symbol is output as a dynamic symbol.
2182 Undefined weak syms won't yet be marked as dynamic. */
2183 if (h->dynindx == -1
2184 && !h->forced_local)
2185 {
2186 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2187 return FALSE;
2188 }
2189
2190 if (info->shared
2191 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2192 {
2193 asection *s = htab->elf.splt;
2194
2195 /* If this is the first .plt entry, make room for the special
2196 first entry. */
2197 if (s->size == 0)
2198 s->size += PLT_ENTRY_SIZE;
2199
2200 h->plt.offset = s->size;
2201
2202 /* If this symbol is not defined in a regular file, and we are
2203 not generating a shared library, then set the symbol to this
2204 location in the .plt. This is required to make function
2205 pointers compare as equal between the normal executable and
2206 the shared library. */
2207 if (! info->shared
2208 && !h->def_regular)
2209 {
2210 h->root.u.def.section = s;
2211 h->root.u.def.value = h->plt.offset;
2212 }
2213
2214 /* Make room for this entry. */
2215 s->size += PLT_ENTRY_SIZE;
2216
2217 /* We also need to make an entry in the .got.plt section, which
2218 will be placed in the .got section by the linker script. */
2219 htab->elf.sgotplt->size += GOT_ENTRY_SIZE;
2220
2221 /* We also need to make an entry in the .rela.plt section. */
2222 htab->elf.srelplt->size += bed->s->sizeof_rela;
2223 htab->elf.srelplt->reloc_count++;
2224 }
2225 else
2226 {
2227 h->plt.offset = (bfd_vma) -1;
2228 h->needs_plt = 0;
2229 }
2230 }
2231 else
2232 {
2233 h->plt.offset = (bfd_vma) -1;
2234 h->needs_plt = 0;
2235 }
2236
2237 eh->tlsdesc_got = (bfd_vma) -1;
2238
2239 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
2240 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
2241 if (h->got.refcount > 0
2242 && info->executable
2243 && h->dynindx == -1
2244 && elf_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
2245 {
2246 h->got.offset = (bfd_vma) -1;
2247 }
2248 else if (h->got.refcount > 0)
2249 {
2250 asection *s;
2251 bfd_boolean dyn;
2252 int tls_type = elf_x86_64_hash_entry (h)->tls_type;
2253
2254 /* Make sure this symbol is output as a dynamic symbol.
2255 Undefined weak syms won't yet be marked as dynamic. */
2256 if (h->dynindx == -1
2257 && !h->forced_local)
2258 {
2259 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2260 return FALSE;
2261 }
2262
2263 if (GOT_TLS_GDESC_P (tls_type))
2264 {
2265 eh->tlsdesc_got = htab->elf.sgotplt->size
2266 - elf_x86_64_compute_jump_table_size (htab);
2267 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2268 h->got.offset = (bfd_vma) -2;
2269 }
2270 if (! GOT_TLS_GDESC_P (tls_type)
2271 || GOT_TLS_GD_P (tls_type))
2272 {
2273 s = htab->elf.sgot;
2274 h->got.offset = s->size;
2275 s->size += GOT_ENTRY_SIZE;
2276 if (GOT_TLS_GD_P (tls_type))
2277 s->size += GOT_ENTRY_SIZE;
2278 }
2279 dyn = htab->elf.dynamic_sections_created;
2280 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2281 and two if global.
2282 R_X86_64_GOTTPOFF needs one dynamic relocation. */
2283 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2284 || tls_type == GOT_TLS_IE)
2285 htab->elf.srelgot->size += bed->s->sizeof_rela;
2286 else if (GOT_TLS_GD_P (tls_type))
2287 htab->elf.srelgot->size += 2 * bed->s->sizeof_rela;
2288 else if (! GOT_TLS_GDESC_P (tls_type)
2289 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2290 || h->root.type != bfd_link_hash_undefweak)
2291 && (info->shared
2292 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2293 htab->elf.srelgot->size += bed->s->sizeof_rela;
2294 if (GOT_TLS_GDESC_P (tls_type))
2295 {
2296 htab->elf.srelplt->size += bed->s->sizeof_rela;
2297 htab->tlsdesc_plt = (bfd_vma) -1;
2298 }
2299 }
2300 else
2301 h->got.offset = (bfd_vma) -1;
2302
2303 if (eh->dyn_relocs == NULL)
2304 return TRUE;
2305
2306 /* In the shared -Bsymbolic case, discard space allocated for
2307 dynamic pc-relative relocs against symbols which turn out to be
2308 defined in regular objects. For the normal shared case, discard
2309 space for pc-relative relocs that have become local due to symbol
2310 visibility changes. */
2311
2312 if (info->shared)
2313 {
2314 /* Relocs that use pc_count are those that appear on a call
2315 insn, or certain REL relocs that can generated via assembly.
2316 We want calls to protected symbols to resolve directly to the
2317 function rather than going via the plt. If people want
2318 function pointer comparisons to work as expected then they
2319 should avoid writing weird assembly. */
2320 if (SYMBOL_CALLS_LOCAL (info, h))
2321 {
2322 struct elf_dyn_relocs **pp;
2323
2324 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2325 {
2326 p->count -= p->pc_count;
2327 p->pc_count = 0;
2328 if (p->count == 0)
2329 *pp = p->next;
2330 else
2331 pp = &p->next;
2332 }
2333 }
2334
2335 /* Also discard relocs on undefined weak syms with non-default
2336 visibility. */
2337 if (eh->dyn_relocs != NULL
2338 && h->root.type == bfd_link_hash_undefweak)
2339 {
2340 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2341 eh->dyn_relocs = NULL;
2342
2343 /* Make sure undefined weak symbols are output as a dynamic
2344 symbol in PIEs. */
2345 else if (h->dynindx == -1
2346 && ! h->forced_local
2347 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2348 return FALSE;
2349 }
2350
2351 }
2352 else if (ELIMINATE_COPY_RELOCS)
2353 {
2354 /* For the non-shared case, discard space for relocs against
2355 symbols which turn out to need copy relocs or are not
2356 dynamic. */
2357
2358 if (!h->non_got_ref
2359 && ((h->def_dynamic
2360 && !h->def_regular)
2361 || (htab->elf.dynamic_sections_created
2362 && (h->root.type == bfd_link_hash_undefweak
2363 || h->root.type == bfd_link_hash_undefined))))
2364 {
2365 /* Make sure this symbol is output as a dynamic symbol.
2366 Undefined weak syms won't yet be marked as dynamic. */
2367 if (h->dynindx == -1
2368 && ! h->forced_local
2369 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2370 return FALSE;
2371
2372 /* If that succeeded, we know we'll be keeping all the
2373 relocs. */
2374 if (h->dynindx != -1)
2375 goto keep;
2376 }
2377
2378 eh->dyn_relocs = NULL;
2379
2380 keep: ;
2381 }
2382
2383 /* Finally, allocate space. */
2384 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2385 {
2386 asection * sreloc;
2387
2388 sreloc = elf_section_data (p->sec)->sreloc;
2389
2390 BFD_ASSERT (sreloc != NULL);
2391
2392 sreloc->size += p->count * bed->s->sizeof_rela;
2393 }
2394
2395 return TRUE;
2396 }
2397
2398 /* Allocate space in .plt, .got and associated reloc sections for
2399 local dynamic relocs. */
2400
2401 static bfd_boolean
2402 elf_x86_64_allocate_local_dynrelocs (void **slot, void *inf)
2403 {
2404 struct elf_link_hash_entry *h
2405 = (struct elf_link_hash_entry *) *slot;
2406
2407 if (h->type != STT_GNU_IFUNC
2408 || !h->def_regular
2409 || !h->ref_regular
2410 || !h->forced_local
2411 || h->root.type != bfd_link_hash_defined)
2412 abort ();
2413
2414 return elf_x86_64_allocate_dynrelocs (h, inf);
2415 }
2416
2417 /* Find any dynamic relocs that apply to read-only sections. */
2418
2419 static bfd_boolean
2420 elf_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h,
2421 void * inf)
2422 {
2423 struct elf_x86_64_link_hash_entry *eh;
2424 struct elf_dyn_relocs *p;
2425
2426 /* Skip local IFUNC symbols. */
2427 if (h->forced_local && h->type == STT_GNU_IFUNC)
2428 return TRUE;
2429
2430 eh = (struct elf_x86_64_link_hash_entry *) h;
2431 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2432 {
2433 asection *s = p->sec->output_section;
2434
2435 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2436 {
2437 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2438
2439 info->flags |= DF_TEXTREL;
2440
2441 if (info->warn_shared_textrel && info->shared)
2442 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'.\n"),
2443 p->sec->owner, h->root.root.string,
2444 p->sec);
2445
2446 /* Not an error, just cut short the traversal. */
2447 return FALSE;
2448 }
2449 }
2450 return TRUE;
2451 }
2452
2453 /* Set the sizes of the dynamic sections. */
2454
2455 static bfd_boolean
2456 elf_x86_64_size_dynamic_sections (bfd *output_bfd,
2457 struct bfd_link_info *info)
2458 {
2459 struct elf_x86_64_link_hash_table *htab;
2460 bfd *dynobj;
2461 asection *s;
2462 bfd_boolean relocs;
2463 bfd *ibfd;
2464 const struct elf_backend_data *bed;
2465
2466 htab = elf_x86_64_hash_table (info);
2467 if (htab == NULL)
2468 return FALSE;
2469 bed = get_elf_backend_data (output_bfd);
2470
2471 dynobj = htab->elf.dynobj;
2472 if (dynobj == NULL)
2473 abort ();
2474
2475 if (htab->elf.dynamic_sections_created)
2476 {
2477 /* Set the contents of the .interp section to the interpreter. */
2478 if (info->executable)
2479 {
2480 s = bfd_get_section_by_name (dynobj, ".interp");
2481 if (s == NULL)
2482 abort ();
2483 s->size = htab->dynamic_interpreter_size;
2484 s->contents = (unsigned char *) htab->dynamic_interpreter;
2485 }
2486 }
2487
2488 /* Set up .got offsets for local syms, and space for local dynamic
2489 relocs. */
2490 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2491 {
2492 bfd_signed_vma *local_got;
2493 bfd_signed_vma *end_local_got;
2494 char *local_tls_type;
2495 bfd_vma *local_tlsdesc_gotent;
2496 bfd_size_type locsymcount;
2497 Elf_Internal_Shdr *symtab_hdr;
2498 asection *srel;
2499
2500 if (! is_x86_64_elf (ibfd))
2501 continue;
2502
2503 for (s = ibfd->sections; s != NULL; s = s->next)
2504 {
2505 struct elf_dyn_relocs *p;
2506
2507 for (p = (struct elf_dyn_relocs *)
2508 (elf_section_data (s)->local_dynrel);
2509 p != NULL;
2510 p = p->next)
2511 {
2512 if (!bfd_is_abs_section (p->sec)
2513 && bfd_is_abs_section (p->sec->output_section))
2514 {
2515 /* Input section has been discarded, either because
2516 it is a copy of a linkonce section or due to
2517 linker script /DISCARD/, so we'll be discarding
2518 the relocs too. */
2519 }
2520 else if (p->count != 0)
2521 {
2522 srel = elf_section_data (p->sec)->sreloc;
2523 srel->size += p->count * bed->s->sizeof_rela;
2524 if ((p->sec->output_section->flags & SEC_READONLY) != 0
2525 && (info->flags & DF_TEXTREL) == 0)
2526 {
2527 info->flags |= DF_TEXTREL;
2528 if (info->warn_shared_textrel && info->shared)
2529 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'.\n"),
2530 p->sec->owner, p->sec);
2531 }
2532 }
2533 }
2534 }
2535
2536 local_got = elf_local_got_refcounts (ibfd);
2537 if (!local_got)
2538 continue;
2539
2540 symtab_hdr = &elf_symtab_hdr (ibfd);
2541 locsymcount = symtab_hdr->sh_info;
2542 end_local_got = local_got + locsymcount;
2543 local_tls_type = elf_x86_64_local_got_tls_type (ibfd);
2544 local_tlsdesc_gotent = elf_x86_64_local_tlsdesc_gotent (ibfd);
2545 s = htab->elf.sgot;
2546 srel = htab->elf.srelgot;
2547 for (; local_got < end_local_got;
2548 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2549 {
2550 *local_tlsdesc_gotent = (bfd_vma) -1;
2551 if (*local_got > 0)
2552 {
2553 if (GOT_TLS_GDESC_P (*local_tls_type))
2554 {
2555 *local_tlsdesc_gotent = htab->elf.sgotplt->size
2556 - elf_x86_64_compute_jump_table_size (htab);
2557 htab->elf.sgotplt->size += 2 * GOT_ENTRY_SIZE;
2558 *local_got = (bfd_vma) -2;
2559 }
2560 if (! GOT_TLS_GDESC_P (*local_tls_type)
2561 || GOT_TLS_GD_P (*local_tls_type))
2562 {
2563 *local_got = s->size;
2564 s->size += GOT_ENTRY_SIZE;
2565 if (GOT_TLS_GD_P (*local_tls_type))
2566 s->size += GOT_ENTRY_SIZE;
2567 }
2568 if (info->shared
2569 || GOT_TLS_GD_ANY_P (*local_tls_type)
2570 || *local_tls_type == GOT_TLS_IE)
2571 {
2572 if (GOT_TLS_GDESC_P (*local_tls_type))
2573 {
2574 htab->elf.srelplt->size
2575 += bed->s->sizeof_rela;
2576 htab->tlsdesc_plt = (bfd_vma) -1;
2577 }
2578 if (! GOT_TLS_GDESC_P (*local_tls_type)
2579 || GOT_TLS_GD_P (*local_tls_type))
2580 srel->size += bed->s->sizeof_rela;
2581 }
2582 }
2583 else
2584 *local_got = (bfd_vma) -1;
2585 }
2586 }
2587
2588 if (htab->tls_ld_got.refcount > 0)
2589 {
2590 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2591 relocs. */
2592 htab->tls_ld_got.offset = htab->elf.sgot->size;
2593 htab->elf.sgot->size += 2 * GOT_ENTRY_SIZE;
2594 htab->elf.srelgot->size += bed->s->sizeof_rela;
2595 }
2596 else
2597 htab->tls_ld_got.offset = -1;
2598
2599 /* Allocate global sym .plt and .got entries, and space for global
2600 sym dynamic relocs. */
2601 elf_link_hash_traverse (&htab->elf, elf_x86_64_allocate_dynrelocs,
2602 info);
2603
2604 /* Allocate .plt and .got entries, and space for local symbols. */
2605 htab_traverse (htab->loc_hash_table,
2606 elf_x86_64_allocate_local_dynrelocs,
2607 info);
2608
2609 /* For every jump slot reserved in the sgotplt, reloc_count is
2610 incremented. However, when we reserve space for TLS descriptors,
2611 it's not incremented, so in order to compute the space reserved
2612 for them, it suffices to multiply the reloc count by the jump
2613 slot size. */
2614 if (htab->elf.srelplt)
2615 htab->sgotplt_jump_table_size
2616 = elf_x86_64_compute_jump_table_size (htab);
2617
2618 if (htab->tlsdesc_plt)
2619 {
2620 /* If we're not using lazy TLS relocations, don't generate the
2621 PLT and GOT entries they require. */
2622 if ((info->flags & DF_BIND_NOW))
2623 htab->tlsdesc_plt = 0;
2624 else
2625 {
2626 htab->tlsdesc_got = htab->elf.sgot->size;
2627 htab->elf.sgot->size += GOT_ENTRY_SIZE;
2628 /* Reserve room for the initial entry.
2629 FIXME: we could probably do away with it in this case. */
2630 if (htab->elf.splt->size == 0)
2631 htab->elf.splt->size += PLT_ENTRY_SIZE;
2632 htab->tlsdesc_plt = htab->elf.splt->size;
2633 htab->elf.splt->size += PLT_ENTRY_SIZE;
2634 }
2635 }
2636
2637 if (htab->elf.sgotplt)
2638 {
2639 struct elf_link_hash_entry *got;
2640 got = elf_link_hash_lookup (elf_hash_table (info),
2641 "_GLOBAL_OFFSET_TABLE_",
2642 FALSE, FALSE, FALSE);
2643
2644 /* Don't allocate .got.plt section if there are no GOT nor PLT
2645 entries and there is no refeence to _GLOBAL_OFFSET_TABLE_. */
2646 if ((got == NULL
2647 || !got->ref_regular_nonweak)
2648 && (htab->elf.sgotplt->size
2649 == get_elf_backend_data (output_bfd)->got_header_size)
2650 && (htab->elf.splt == NULL
2651 || htab->elf.splt->size == 0)
2652 && (htab->elf.sgot == NULL
2653 || htab->elf.sgot->size == 0)
2654 && (htab->elf.iplt == NULL
2655 || htab->elf.iplt->size == 0)
2656 && (htab->elf.igotplt == NULL
2657 || htab->elf.igotplt->size == 0))
2658 htab->elf.sgotplt->size = 0;
2659 }
2660
2661 /* We now have determined the sizes of the various dynamic sections.
2662 Allocate memory for them. */
2663 relocs = FALSE;
2664 for (s = dynobj->sections; s != NULL; s = s->next)
2665 {
2666 if ((s->flags & SEC_LINKER_CREATED) == 0)
2667 continue;
2668
2669 if (s == htab->elf.splt
2670 || s == htab->elf.sgot
2671 || s == htab->elf.sgotplt
2672 || s == htab->elf.iplt
2673 || s == htab->elf.igotplt
2674 || s == htab->sdynbss)
2675 {
2676 /* Strip this section if we don't need it; see the
2677 comment below. */
2678 }
2679 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2680 {
2681 if (s->size != 0 && s != htab->elf.srelplt)
2682 relocs = TRUE;
2683
2684 /* We use the reloc_count field as a counter if we need
2685 to copy relocs into the output file. */
2686 if (s != htab->elf.srelplt)
2687 s->reloc_count = 0;
2688 }
2689 else
2690 {
2691 /* It's not one of our sections, so don't allocate space. */
2692 continue;
2693 }
2694
2695 if (s->size == 0)
2696 {
2697 /* If we don't need this section, strip it from the
2698 output file. This is mostly to handle .rela.bss and
2699 .rela.plt. We must create both sections in
2700 create_dynamic_sections, because they must be created
2701 before the linker maps input sections to output
2702 sections. The linker does that before
2703 adjust_dynamic_symbol is called, and it is that
2704 function which decides whether anything needs to go
2705 into these sections. */
2706
2707 s->flags |= SEC_EXCLUDE;
2708 continue;
2709 }
2710
2711 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2712 continue;
2713
2714 /* Allocate memory for the section contents. We use bfd_zalloc
2715 here in case unused entries are not reclaimed before the
2716 section's contents are written out. This should not happen,
2717 but this way if it does, we get a R_X86_64_NONE reloc instead
2718 of garbage. */
2719 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2720 if (s->contents == NULL)
2721 return FALSE;
2722 }
2723
2724 if (htab->elf.dynamic_sections_created)
2725 {
2726 /* Add some entries to the .dynamic section. We fill in the
2727 values later, in elf_x86_64_finish_dynamic_sections, but we
2728 must add the entries now so that we get the correct size for
2729 the .dynamic section. The DT_DEBUG entry is filled in by the
2730 dynamic linker and used by the debugger. */
2731 #define add_dynamic_entry(TAG, VAL) \
2732 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2733
2734 if (info->executable)
2735 {
2736 if (!add_dynamic_entry (DT_DEBUG, 0))
2737 return FALSE;
2738 }
2739
2740 if (htab->elf.splt->size != 0)
2741 {
2742 if (!add_dynamic_entry (DT_PLTGOT, 0)
2743 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2744 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2745 || !add_dynamic_entry (DT_JMPREL, 0))
2746 return FALSE;
2747
2748 if (htab->tlsdesc_plt
2749 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2750 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2751 return FALSE;
2752 }
2753
2754 if (relocs)
2755 {
2756 if (!add_dynamic_entry (DT_RELA, 0)
2757 || !add_dynamic_entry (DT_RELASZ, 0)
2758 || !add_dynamic_entry (DT_RELAENT, bed->s->sizeof_rela))
2759 return FALSE;
2760
2761 /* If any dynamic relocs apply to a read-only section,
2762 then we need a DT_TEXTREL entry. */
2763 if ((info->flags & DF_TEXTREL) == 0)
2764 elf_link_hash_traverse (&htab->elf,
2765 elf_x86_64_readonly_dynrelocs,
2766 info);
2767
2768 if ((info->flags & DF_TEXTREL) != 0)
2769 {
2770 if (!add_dynamic_entry (DT_TEXTREL, 0))
2771 return FALSE;
2772 }
2773 }
2774 }
2775 #undef add_dynamic_entry
2776
2777 return TRUE;
2778 }
2779
2780 static bfd_boolean
2781 elf_x86_64_always_size_sections (bfd *output_bfd,
2782 struct bfd_link_info *info)
2783 {
2784 asection *tls_sec = elf_hash_table (info)->tls_sec;
2785
2786 if (tls_sec)
2787 {
2788 struct elf_link_hash_entry *tlsbase;
2789
2790 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2791 "_TLS_MODULE_BASE_",
2792 FALSE, FALSE, FALSE);
2793
2794 if (tlsbase && tlsbase->type == STT_TLS)
2795 {
2796 struct elf_x86_64_link_hash_table *htab;
2797 struct bfd_link_hash_entry *bh = NULL;
2798 const struct elf_backend_data *bed
2799 = get_elf_backend_data (output_bfd);
2800
2801 htab = elf_x86_64_hash_table (info);
2802 if (htab == NULL)
2803 return FALSE;
2804
2805 if (!(_bfd_generic_link_add_one_symbol
2806 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2807 tls_sec, 0, NULL, FALSE,
2808 bed->collect, &bh)))
2809 return FALSE;
2810
2811 htab->tls_module_base = bh;
2812
2813 tlsbase = (struct elf_link_hash_entry *)bh;
2814 tlsbase->def_regular = 1;
2815 tlsbase->other = STV_HIDDEN;
2816 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2817 }
2818 }
2819
2820 return TRUE;
2821 }
2822
2823 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2824 executables. Rather than setting it to the beginning of the TLS
2825 section, we have to set it to the end. This function may be called
2826 multiple times, it is idempotent. */
2827
2828 static void
2829 elf_x86_64_set_tls_module_base (struct bfd_link_info *info)
2830 {
2831 struct elf_x86_64_link_hash_table *htab;
2832 struct bfd_link_hash_entry *base;
2833
2834 if (!info->executable)
2835 return;
2836
2837 htab = elf_x86_64_hash_table (info);
2838 if (htab == NULL)
2839 return;
2840
2841 base = htab->tls_module_base;
2842 if (base == NULL)
2843 return;
2844
2845 base->u.def.value = htab->elf.tls_size;
2846 }
2847
2848 /* Return the base VMA address which should be subtracted from real addresses
2849 when resolving @dtpoff relocation.
2850 This is PT_TLS segment p_vaddr. */
2851
2852 static bfd_vma
2853 elf_x86_64_dtpoff_base (struct bfd_link_info *info)
2854 {
2855 /* If tls_sec is NULL, we should have signalled an error already. */
2856 if (elf_hash_table (info)->tls_sec == NULL)
2857 return 0;
2858 return elf_hash_table (info)->tls_sec->vma;
2859 }
2860
2861 /* Return the relocation value for @tpoff relocation
2862 if STT_TLS virtual address is ADDRESS. */
2863
2864 static bfd_vma
2865 elf_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2866 {
2867 struct elf_link_hash_table *htab = elf_hash_table (info);
2868 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
2869 bfd_vma static_tls_size;
2870
2871 /* If tls_segment is NULL, we should have signalled an error already. */
2872 if (htab->tls_sec == NULL)
2873 return 0;
2874
2875 /* Consider special static TLS alignment requirements. */
2876 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment);
2877 return address - static_tls_size - htab->tls_sec->vma;
2878 }
2879
2880 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2881 branch? */
2882
2883 static bfd_boolean
2884 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2885 {
2886 /* Opcode Instruction
2887 0xe8 call
2888 0xe9 jump
2889 0x0f 0x8x conditional jump */
2890 return ((offset > 0
2891 && (contents [offset - 1] == 0xe8
2892 || contents [offset - 1] == 0xe9))
2893 || (offset > 1
2894 && contents [offset - 2] == 0x0f
2895 && (contents [offset - 1] & 0xf0) == 0x80));
2896 }
2897
2898 /* Relocate an x86_64 ELF section. */
2899
2900 static bfd_boolean
2901 elf_x86_64_relocate_section (bfd *output_bfd,
2902 struct bfd_link_info *info,
2903 bfd *input_bfd,
2904 asection *input_section,
2905 bfd_byte *contents,
2906 Elf_Internal_Rela *relocs,
2907 Elf_Internal_Sym *local_syms,
2908 asection **local_sections)
2909 {
2910 struct elf_x86_64_link_hash_table *htab;
2911 Elf_Internal_Shdr *symtab_hdr;
2912 struct elf_link_hash_entry **sym_hashes;
2913 bfd_vma *local_got_offsets;
2914 bfd_vma *local_tlsdesc_gotents;
2915 Elf_Internal_Rela *rel;
2916 Elf_Internal_Rela *relend;
2917
2918 BFD_ASSERT (is_x86_64_elf (input_bfd));
2919
2920 htab = elf_x86_64_hash_table (info);
2921 if (htab == NULL)
2922 return FALSE;
2923 symtab_hdr = &elf_symtab_hdr (input_bfd);
2924 sym_hashes = elf_sym_hashes (input_bfd);
2925 local_got_offsets = elf_local_got_offsets (input_bfd);
2926 local_tlsdesc_gotents = elf_x86_64_local_tlsdesc_gotent (input_bfd);
2927
2928 elf_x86_64_set_tls_module_base (info);
2929
2930 rel = relocs;
2931 relend = relocs + input_section->reloc_count;
2932 for (; rel < relend; rel++)
2933 {
2934 unsigned int r_type;
2935 reloc_howto_type *howto;
2936 unsigned long r_symndx;
2937 struct elf_link_hash_entry *h;
2938 Elf_Internal_Sym *sym;
2939 asection *sec;
2940 bfd_vma off, offplt;
2941 bfd_vma relocation;
2942 bfd_boolean unresolved_reloc;
2943 bfd_reloc_status_type r;
2944 int tls_type;
2945 asection *base_got;
2946
2947 r_type = ELF32_R_TYPE (rel->r_info);
2948 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2949 || r_type == (int) R_X86_64_GNU_VTENTRY)
2950 continue;
2951
2952 if (r_type >= R_X86_64_max)
2953 {
2954 bfd_set_error (bfd_error_bad_value);
2955 return FALSE;
2956 }
2957
2958 howto = x86_64_elf_howto_table + r_type;
2959 r_symndx = htab->r_sym (rel->r_info);
2960 h = NULL;
2961 sym = NULL;
2962 sec = NULL;
2963 unresolved_reloc = FALSE;
2964 if (r_symndx < symtab_hdr->sh_info)
2965 {
2966 sym = local_syms + r_symndx;
2967 sec = local_sections[r_symndx];
2968
2969 relocation = _bfd_elf_rela_local_sym (output_bfd, sym,
2970 &sec, rel);
2971
2972 /* Relocate against local STT_GNU_IFUNC symbol. */
2973 if (!info->relocatable
2974 && ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
2975 {
2976 h = elf_x86_64_get_local_sym_hash (htab, input_bfd,
2977 rel, FALSE);
2978 if (h == NULL)
2979 abort ();
2980
2981 /* Set STT_GNU_IFUNC symbol value. */
2982 h->root.u.def.value = sym->st_value;
2983 h->root.u.def.section = sec;
2984 }
2985 }
2986 else
2987 {
2988 bfd_boolean warned ATTRIBUTE_UNUSED;
2989
2990 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2991 r_symndx, symtab_hdr, sym_hashes,
2992 h, sec, relocation,
2993 unresolved_reloc, warned);
2994 }
2995
2996 if (sec != NULL && elf_discarded_section (sec))
2997 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2998 rel, relend, howto, contents);
2999
3000 if (info->relocatable)
3001 continue;
3002
3003 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
3004 it here if it is defined in a non-shared object. */
3005 if (h != NULL
3006 && h->type == STT_GNU_IFUNC
3007 && h->def_regular)
3008 {
3009 asection *plt;
3010 bfd_vma plt_index;
3011 const char *name;
3012
3013 if ((input_section->flags & SEC_ALLOC) == 0
3014 || h->plt.offset == (bfd_vma) -1)
3015 abort ();
3016
3017 /* STT_GNU_IFUNC symbol must go through PLT. */
3018 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
3019 relocation = (plt->output_section->vma
3020 + plt->output_offset + h->plt.offset);
3021
3022 switch (r_type)
3023 {
3024 default:
3025 if (h->root.root.string)
3026 name = h->root.root.string;
3027 else
3028 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
3029 NULL);
3030 (*_bfd_error_handler)
3031 (_("%B: relocation %s against STT_GNU_IFUNC "
3032 "symbol `%s' isn't handled by %s"), input_bfd,
3033 x86_64_elf_howto_table[r_type].name,
3034 name, __FUNCTION__);
3035 bfd_set_error (bfd_error_bad_value);
3036 return FALSE;
3037
3038 case R_X86_64_32S:
3039 if (info->shared)
3040 abort ();
3041 goto do_relocation;
3042
3043 case R_X86_64_32:
3044 if (ABI_64_P (output_bfd))
3045 goto do_relocation;
3046 /* FALLTHROUGH */
3047 case R_X86_64_64:
3048 if (rel->r_addend != 0)
3049 {
3050 if (h->root.root.string)
3051 name = h->root.root.string;
3052 else
3053 name = bfd_elf_sym_name (input_bfd, symtab_hdr,
3054 sym, NULL);
3055 (*_bfd_error_handler)
3056 (_("%B: relocation %s against STT_GNU_IFUNC "
3057 "symbol `%s' has non-zero addend: %d"),
3058 input_bfd, x86_64_elf_howto_table[r_type].name,
3059 name, rel->r_addend);
3060 bfd_set_error (bfd_error_bad_value);
3061 return FALSE;
3062 }
3063
3064 /* Generate dynamic relcoation only when there is a
3065 non-GOF reference in a shared object. */
3066 if (info->shared && h->non_got_ref)
3067 {
3068 Elf_Internal_Rela outrel;
3069 asection *sreloc;
3070
3071 /* Need a dynamic relocation to get the real function
3072 address. */
3073 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
3074 info,
3075 input_section,
3076 rel->r_offset);
3077 if (outrel.r_offset == (bfd_vma) -1
3078 || outrel.r_offset == (bfd_vma) -2)
3079 abort ();
3080
3081 outrel.r_offset += (input_section->output_section->vma
3082 + input_section->output_offset);
3083
3084 if (h->dynindx == -1
3085 || h->forced_local
3086 || info->executable)
3087 {
3088 /* This symbol is resolved locally. */
3089 outrel.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
3090 outrel.r_addend = (h->root.u.def.value
3091 + h->root.u.def.section->output_section->vma
3092 + h->root.u.def.section->output_offset);
3093 }
3094 else
3095 {
3096 outrel.r_info = htab->r_info (h->dynindx, r_type);
3097 outrel.r_addend = 0;
3098 }
3099
3100 sreloc = htab->elf.irelifunc;
3101 elf_append_rela (output_bfd, sreloc, &outrel);
3102
3103 /* If this reloc is against an external symbol, we
3104 do not want to fiddle with the addend. Otherwise,
3105 we need to include the symbol value so that it
3106 becomes an addend for the dynamic reloc. For an
3107 internal symbol, we have updated addend. */
3108 continue;
3109 }
3110 /* FALLTHROUGH */
3111 case R_X86_64_PC32:
3112 case R_X86_64_PC64:
3113 case R_X86_64_PLT32:
3114 goto do_relocation;
3115
3116 case R_X86_64_GOTPCREL:
3117 case R_X86_64_GOTPCREL64:
3118 base_got = htab->elf.sgot;
3119 off = h->got.offset;
3120
3121 if (base_got == NULL)
3122 abort ();
3123
3124 if (off == (bfd_vma) -1)
3125 {
3126 /* We can't use h->got.offset here to save state, or
3127 even just remember the offset, as finish_dynamic_symbol
3128 would use that as offset into .got. */
3129
3130 if (htab->elf.splt != NULL)
3131 {
3132 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3133 off = (plt_index + 3) * GOT_ENTRY_SIZE;
3134 base_got = htab->elf.sgotplt;
3135 }
3136 else
3137 {
3138 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3139 off = plt_index * GOT_ENTRY_SIZE;
3140 base_got = htab->elf.igotplt;
3141 }
3142
3143 if (h->dynindx == -1
3144 || h->forced_local
3145 || info->symbolic)
3146 {
3147 /* This references the local defitionion. We must
3148 initialize this entry in the global offset table.
3149 Since the offset must always be a multiple of 8,
3150 we use the least significant bit to record
3151 whether we have initialized it already.
3152
3153 When doing a dynamic link, we create a .rela.got
3154 relocation entry to initialize the value. This
3155 is done in the finish_dynamic_symbol routine. */
3156 if ((off & 1) != 0)
3157 off &= ~1;
3158 else
3159 {
3160 bfd_put_64 (output_bfd, relocation,
3161 base_got->contents + off);
3162 /* Note that this is harmless for the GOTPLT64
3163 case, as -1 | 1 still is -1. */
3164 h->got.offset |= 1;
3165 }
3166 }
3167 }
3168
3169 relocation = (base_got->output_section->vma
3170 + base_got->output_offset + off);
3171
3172 goto do_relocation;
3173 }
3174 }
3175
3176 /* When generating a shared object, the relocations handled here are
3177 copied into the output file to be resolved at run time. */
3178 switch (r_type)
3179 {
3180 case R_X86_64_GOT32:
3181 case R_X86_64_GOT64:
3182 /* Relocation is to the entry for this symbol in the global
3183 offset table. */
3184 case R_X86_64_GOTPCREL:
3185 case R_X86_64_GOTPCREL64:
3186 /* Use global offset table entry as symbol value. */
3187 case R_X86_64_GOTPLT64:
3188 /* This is the same as GOT64 for relocation purposes, but
3189 indicates the existence of a PLT entry. The difficulty is,
3190 that we must calculate the GOT slot offset from the PLT
3191 offset, if this symbol got a PLT entry (it was global).
3192 Additionally if it's computed from the PLT entry, then that
3193 GOT offset is relative to .got.plt, not to .got. */
3194 base_got = htab->elf.sgot;
3195
3196 if (htab->elf.sgot == NULL)
3197 abort ();
3198
3199 if (h != NULL)
3200 {
3201 bfd_boolean dyn;
3202
3203 off = h->got.offset;
3204 if (h->needs_plt
3205 && h->plt.offset != (bfd_vma)-1
3206 && off == (bfd_vma)-1)
3207 {
3208 /* We can't use h->got.offset here to save
3209 state, or even just remember the offset, as
3210 finish_dynamic_symbol would use that as offset into
3211 .got. */
3212 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3213 off = (plt_index + 3) * GOT_ENTRY_SIZE;
3214 base_got = htab->elf.sgotplt;
3215 }
3216
3217 dyn = htab->elf.dynamic_sections_created;
3218
3219 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
3220 || (info->shared
3221 && SYMBOL_REFERENCES_LOCAL (info, h))
3222 || (ELF_ST_VISIBILITY (h->other)
3223 && h->root.type == bfd_link_hash_undefweak))
3224 {
3225 /* This is actually a static link, or it is a -Bsymbolic
3226 link and the symbol is defined locally, or the symbol
3227 was forced to be local because of a version file. We
3228 must initialize this entry in the global offset table.
3229 Since the offset must always be a multiple of 8, we
3230 use the least significant bit to record whether we
3231 have initialized it already.
3232
3233 When doing a dynamic link, we create a .rela.got
3234 relocation entry to initialize the value. This is
3235 done in the finish_dynamic_symbol routine. */
3236 if ((off & 1) != 0)
3237 off &= ~1;
3238 else
3239 {
3240 bfd_put_64 (output_bfd, relocation,
3241 base_got->contents + off);
3242 /* Note that this is harmless for the GOTPLT64 case,
3243 as -1 | 1 still is -1. */
3244 h->got.offset |= 1;
3245 }
3246 }
3247 else
3248 unresolved_reloc = FALSE;
3249 }
3250 else
3251 {
3252 if (local_got_offsets == NULL)
3253 abort ();
3254
3255 off = local_got_offsets[r_symndx];
3256
3257 /* The offset must always be a multiple of 8. We use
3258 the least significant bit to record whether we have
3259 already generated the necessary reloc. */
3260 if ((off & 1) != 0)
3261 off &= ~1;
3262 else
3263 {
3264 bfd_put_64 (output_bfd, relocation,
3265 base_got->contents + off);
3266
3267 if (info->shared)
3268 {
3269 asection *s;
3270 Elf_Internal_Rela outrel;
3271
3272 /* We need to generate a R_X86_64_RELATIVE reloc
3273 for the dynamic linker. */
3274 s = htab->elf.srelgot;
3275 if (s == NULL)
3276 abort ();
3277
3278 outrel.r_offset = (base_got->output_section->vma
3279 + base_got->output_offset
3280 + off);
3281 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3282 outrel.r_addend = relocation;
3283 elf_append_rela (output_bfd, s, &outrel);
3284 }
3285
3286 local_got_offsets[r_symndx] |= 1;
3287 }
3288 }
3289
3290 if (off >= (bfd_vma) -2)
3291 abort ();
3292
3293 relocation = base_got->output_section->vma
3294 + base_got->output_offset + off;
3295 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
3296 relocation -= htab->elf.sgotplt->output_section->vma
3297 - htab->elf.sgotplt->output_offset;
3298
3299 break;
3300
3301 case R_X86_64_GOTOFF64:
3302 /* Relocation is relative to the start of the global offset
3303 table. */
3304
3305 /* Check to make sure it isn't a protected function symbol
3306 for shared library since it may not be local when used
3307 as function address. */
3308 if (info->shared
3309 && h
3310 && h->def_regular
3311 && h->type == STT_FUNC
3312 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
3313 {
3314 (*_bfd_error_handler)
3315 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
3316 input_bfd, h->root.root.string);
3317 bfd_set_error (bfd_error_bad_value);
3318 return FALSE;
3319 }
3320
3321 /* Note that sgot is not involved in this
3322 calculation. We always want the start of .got.plt. If we
3323 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
3324 permitted by the ABI, we might have to change this
3325 calculation. */
3326 relocation -= htab->elf.sgotplt->output_section->vma
3327 + htab->elf.sgotplt->output_offset;
3328 break;
3329
3330 case R_X86_64_GOTPC32:
3331 case R_X86_64_GOTPC64:
3332 /* Use global offset table as symbol value. */
3333 relocation = htab->elf.sgotplt->output_section->vma
3334 + htab->elf.sgotplt->output_offset;
3335 unresolved_reloc = FALSE;
3336 break;
3337
3338 case R_X86_64_PLTOFF64:
3339 /* Relocation is PLT entry relative to GOT. For local
3340 symbols it's the symbol itself relative to GOT. */
3341 if (h != NULL
3342 /* See PLT32 handling. */
3343 && h->plt.offset != (bfd_vma) -1
3344 && htab->elf.splt != NULL)
3345 {
3346 relocation = (htab->elf.splt->output_section->vma
3347 + htab->elf.splt->output_offset
3348 + h->plt.offset);
3349 unresolved_reloc = FALSE;
3350 }
3351
3352 relocation -= htab->elf.sgotplt->output_section->vma
3353 + htab->elf.sgotplt->output_offset;
3354 break;
3355
3356 case R_X86_64_PLT32:
3357 /* Relocation is to the entry for this symbol in the
3358 procedure linkage table. */
3359
3360 /* Resolve a PLT32 reloc against a local symbol directly,
3361 without using the procedure linkage table. */
3362 if (h == NULL)
3363 break;
3364
3365 if (h->plt.offset == (bfd_vma) -1
3366 || htab->elf.splt == NULL)
3367 {
3368 /* We didn't make a PLT entry for this symbol. This
3369 happens when statically linking PIC code, or when
3370 using -Bsymbolic. */
3371 break;
3372 }
3373
3374 relocation = (htab->elf.splt->output_section->vma
3375 + htab->elf.splt->output_offset
3376 + h->plt.offset);
3377 unresolved_reloc = FALSE;
3378 break;
3379
3380 case R_X86_64_PC8:
3381 case R_X86_64_PC16:
3382 case R_X86_64_PC32:
3383 if (info->shared
3384 && ABI_64_P (output_bfd)
3385 && (input_section->flags & SEC_ALLOC) != 0
3386 && (input_section->flags & SEC_READONLY) != 0
3387 && h != NULL)
3388 {
3389 bfd_boolean fail = FALSE;
3390 bfd_boolean branch
3391 = (r_type == R_X86_64_PC32
3392 && is_32bit_relative_branch (contents, rel->r_offset));
3393
3394 if (SYMBOL_REFERENCES_LOCAL (info, h))
3395 {
3396 /* Symbol is referenced locally. Make sure it is
3397 defined locally or for a branch. */
3398 fail = !h->def_regular && !branch;
3399 }
3400 else
3401 {
3402 /* Symbol isn't referenced locally. We only allow
3403 branch to symbol with non-default visibility. */
3404 fail = (!branch
3405 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3406 }
3407
3408 if (fail)
3409 {
3410 const char *fmt;
3411 const char *v;
3412 const char *pic = "";
3413
3414 switch (ELF_ST_VISIBILITY (h->other))
3415 {
3416 case STV_HIDDEN:
3417 v = _("hidden symbol");
3418 break;
3419 case STV_INTERNAL:
3420 v = _("internal symbol");
3421 break;
3422 case STV_PROTECTED:
3423 v = _("protected symbol");
3424 break;
3425 default:
3426 v = _("symbol");
3427 pic = _("; recompile with -fPIC");
3428 break;
3429 }
3430
3431 if (h->def_regular)
3432 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3433 else
3434 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3435
3436 (*_bfd_error_handler) (fmt, input_bfd,
3437 x86_64_elf_howto_table[r_type].name,
3438 v, h->root.root.string, pic);
3439 bfd_set_error (bfd_error_bad_value);
3440 return FALSE;
3441 }
3442 }
3443 /* Fall through. */
3444
3445 case R_X86_64_8:
3446 case R_X86_64_16:
3447 case R_X86_64_32:
3448 case R_X86_64_PC64:
3449 case R_X86_64_64:
3450 /* FIXME: The ABI says the linker should make sure the value is
3451 the same when it's zeroextended to 64 bit. */
3452
3453 if ((input_section->flags & SEC_ALLOC) == 0)
3454 break;
3455
3456 if ((info->shared
3457 && (h == NULL
3458 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3459 || h->root.type != bfd_link_hash_undefweak)
3460 && (! IS_X86_64_PCREL_TYPE (r_type)
3461 || ! SYMBOL_CALLS_LOCAL (info, h)))
3462 || (ELIMINATE_COPY_RELOCS
3463 && !info->shared
3464 && h != NULL
3465 && h->dynindx != -1
3466 && !h->non_got_ref
3467 && ((h->def_dynamic
3468 && !h->def_regular)
3469 || h->root.type == bfd_link_hash_undefweak
3470 || h->root.type == bfd_link_hash_undefined)))
3471 {
3472 Elf_Internal_Rela outrel;
3473 bfd_boolean skip, relocate;
3474 asection *sreloc;
3475
3476 /* When generating a shared object, these relocations
3477 are copied into the output file to be resolved at run
3478 time. */
3479 skip = FALSE;
3480 relocate = FALSE;
3481
3482 outrel.r_offset =
3483 _bfd_elf_section_offset (output_bfd, info, input_section,
3484 rel->r_offset);
3485 if (outrel.r_offset == (bfd_vma) -1)
3486 skip = TRUE;
3487 else if (outrel.r_offset == (bfd_vma) -2)
3488 skip = TRUE, relocate = TRUE;
3489
3490 outrel.r_offset += (input_section->output_section->vma
3491 + input_section->output_offset);
3492
3493 if (skip)
3494 memset (&outrel, 0, sizeof outrel);
3495
3496 /* h->dynindx may be -1 if this symbol was marked to
3497 become local. */
3498 else if (h != NULL
3499 && h->dynindx != -1
3500 && (IS_X86_64_PCREL_TYPE (r_type)
3501 || ! info->shared
3502 || ! SYMBOLIC_BIND (info, h)
3503 || ! h->def_regular))
3504 {
3505 outrel.r_info = htab->r_info (h->dynindx, r_type);
3506 outrel.r_addend = rel->r_addend;
3507 }
3508 else
3509 {
3510 /* This symbol is local, or marked to become local. */
3511 if (r_type == htab->pointer_r_type)
3512 {
3513 relocate = TRUE;
3514 outrel.r_info = htab->r_info (0, R_X86_64_RELATIVE);
3515 outrel.r_addend = relocation + rel->r_addend;
3516 }
3517 else
3518 {
3519 long sindx;
3520
3521 if (bfd_is_abs_section (sec))
3522 sindx = 0;
3523 else if (sec == NULL || sec->owner == NULL)
3524 {
3525 bfd_set_error (bfd_error_bad_value);
3526 return FALSE;
3527 }
3528 else
3529 {
3530 asection *osec;
3531
3532 /* We are turning this relocation into one
3533 against a section symbol. It would be
3534 proper to subtract the symbol's value,
3535 osec->vma, from the emitted reloc addend,
3536 but ld.so expects buggy relocs. */
3537 osec = sec->output_section;
3538 sindx = elf_section_data (osec)->dynindx;
3539 if (sindx == 0)
3540 {
3541 asection *oi = htab->elf.text_index_section;
3542 sindx = elf_section_data (oi)->dynindx;
3543 }
3544 BFD_ASSERT (sindx != 0);
3545 }
3546
3547 outrel.r_info = htab->r_info (sindx, r_type);
3548 outrel.r_addend = relocation + rel->r_addend;
3549 }
3550 }
3551
3552 sreloc = elf_section_data (input_section)->sreloc;
3553
3554 if (sreloc == NULL || sreloc->contents == NULL)
3555 {
3556 r = bfd_reloc_notsupported;
3557 goto check_relocation_error;
3558 }
3559
3560 elf_append_rela (output_bfd, sreloc, &outrel);
3561
3562 /* If this reloc is against an external symbol, we do
3563 not want to fiddle with the addend. Otherwise, we
3564 need to include the symbol value so that it becomes
3565 an addend for the dynamic reloc. */
3566 if (! relocate)
3567 continue;
3568 }
3569
3570 break;
3571
3572 case R_X86_64_TLSGD:
3573 case R_X86_64_GOTPC32_TLSDESC:
3574 case R_X86_64_TLSDESC_CALL:
3575 case R_X86_64_GOTTPOFF:
3576 tls_type = GOT_UNKNOWN;
3577 if (h == NULL && local_got_offsets)
3578 tls_type = elf_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3579 else if (h != NULL)
3580 tls_type = elf_x86_64_hash_entry (h)->tls_type;
3581
3582 if (! elf_x86_64_tls_transition (info, input_bfd,
3583 input_section, contents,
3584 symtab_hdr, sym_hashes,
3585 &r_type, tls_type, rel,
3586 relend, h, r_symndx))
3587 return FALSE;
3588
3589 if (r_type == R_X86_64_TPOFF32)
3590 {
3591 bfd_vma roff = rel->r_offset;
3592
3593 BFD_ASSERT (! unresolved_reloc);
3594
3595 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3596 {
3597 /* GD->LE transition. For 64bit, change
3598 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3599 .word 0x6666; rex64; call __tls_get_addr
3600 into:
3601 movq %fs:0, %rax
3602 leaq foo@tpoff(%rax), %rax
3603 For 32bit, change
3604 leaq foo@tlsgd(%rip), %rdi
3605 .word 0x6666; rex64; call __tls_get_addr
3606 into:
3607 movl %fs:0, %eax
3608 leaq foo@tpoff(%rax), %rax */
3609 if (ABI_64_P (output_bfd))
3610 memcpy (contents + roff - 4,
3611 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3612 16);
3613 else
3614 memcpy (contents + roff - 3,
3615 "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3616 15);
3617 bfd_put_32 (output_bfd,
3618 elf_x86_64_tpoff (info, relocation),
3619 contents + roff + 8);
3620 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3621 rel++;
3622 continue;
3623 }
3624 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3625 {
3626 /* GDesc -> LE transition.
3627 It's originally something like:
3628 leaq x@tlsdesc(%rip), %rax
3629
3630 Change it to:
3631 movl $x@tpoff, %rax. */
3632
3633 unsigned int val, type;
3634
3635 type = bfd_get_8 (input_bfd, contents + roff - 3);
3636 val = bfd_get_8 (input_bfd, contents + roff - 1);
3637 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3638 contents + roff - 3);
3639 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3640 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3641 contents + roff - 1);
3642 bfd_put_32 (output_bfd,
3643 elf_x86_64_tpoff (info, relocation),
3644 contents + roff);
3645 continue;
3646 }
3647 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3648 {
3649 /* GDesc -> LE transition.
3650 It's originally:
3651 call *(%rax)
3652 Turn it into:
3653 xchg %ax,%ax. */
3654 bfd_put_8 (output_bfd, 0x66, contents + roff);
3655 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3656 continue;
3657 }
3658 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3659 {
3660 /* IE->LE transition:
3661 Originally it can be one of:
3662 movq foo@gottpoff(%rip), %reg
3663 addq foo@gottpoff(%rip), %reg
3664 We change it into:
3665 movq $foo, %reg
3666 leaq foo(%reg), %reg
3667 addq $foo, %reg. */
3668
3669 unsigned int val, type, reg;
3670
3671 val = bfd_get_8 (input_bfd, contents + roff - 3);
3672 type = bfd_get_8 (input_bfd, contents + roff - 2);
3673 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3674 reg >>= 3;
3675 if (type == 0x8b)
3676 {
3677 /* movq */
3678 if (val == 0x4c)
3679 bfd_put_8 (output_bfd, 0x49,
3680 contents + roff - 3);
3681 else if (!ABI_64_P (output_bfd) && val == 0x44)
3682 bfd_put_8 (output_bfd, 0x41,
3683 contents + roff - 3);
3684 bfd_put_8 (output_bfd, 0xc7,
3685 contents + roff - 2);
3686 bfd_put_8 (output_bfd, 0xc0 | reg,
3687 contents + roff - 1);
3688 }
3689 else if (reg == 4)
3690 {
3691 /* addq -> addq - addressing with %rsp/%r12 is
3692 special */
3693 if (val == 0x4c)
3694 bfd_put_8 (output_bfd, 0x49,
3695 contents + roff - 3);
3696 else if (!ABI_64_P (output_bfd) && val == 0x44)
3697 bfd_put_8 (output_bfd, 0x41,
3698 contents + roff - 3);
3699 bfd_put_8 (output_bfd, 0x81,
3700 contents + roff - 2);
3701 bfd_put_8 (output_bfd, 0xc0 | reg,
3702 contents + roff - 1);
3703 }
3704 else
3705 {
3706 /* addq -> leaq */
3707 if (val == 0x4c)
3708 bfd_put_8 (output_bfd, 0x4d,
3709 contents + roff - 3);
3710 else if (!ABI_64_P (output_bfd) && val == 0x44)
3711 bfd_put_8 (output_bfd, 0x45,
3712 contents + roff - 3);
3713 bfd_put_8 (output_bfd, 0x8d,
3714 contents + roff - 2);
3715 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3716 contents + roff - 1);
3717 }
3718 bfd_put_32 (output_bfd,
3719 elf_x86_64_tpoff (info, relocation),
3720 contents + roff);
3721 continue;
3722 }
3723 else
3724 BFD_ASSERT (FALSE);
3725 }
3726
3727 if (htab->elf.sgot == NULL)
3728 abort ();
3729
3730 if (h != NULL)
3731 {
3732 off = h->got.offset;
3733 offplt = elf_x86_64_hash_entry (h)->tlsdesc_got;
3734 }
3735 else
3736 {
3737 if (local_got_offsets == NULL)
3738 abort ();
3739
3740 off = local_got_offsets[r_symndx];
3741 offplt = local_tlsdesc_gotents[r_symndx];
3742 }
3743
3744 if ((off & 1) != 0)
3745 off &= ~1;
3746 else
3747 {
3748 Elf_Internal_Rela outrel;
3749 int dr_type, indx;
3750 asection *sreloc;
3751
3752 if (htab->elf.srelgot == NULL)
3753 abort ();
3754
3755 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3756
3757 if (GOT_TLS_GDESC_P (tls_type))
3758 {
3759 outrel.r_info = htab->r_info (indx, R_X86_64_TLSDESC);
3760 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3761 + 2 * GOT_ENTRY_SIZE <= htab->elf.sgotplt->size);
3762 outrel.r_offset = (htab->elf.sgotplt->output_section->vma
3763 + htab->elf.sgotplt->output_offset
3764 + offplt
3765 + htab->sgotplt_jump_table_size);
3766 sreloc = htab->elf.srelplt;
3767 if (indx == 0)
3768 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
3769 else
3770 outrel.r_addend = 0;
3771 elf_append_rela (output_bfd, sreloc, &outrel);
3772 }
3773
3774 sreloc = htab->elf.srelgot;
3775
3776 outrel.r_offset = (htab->elf.sgot->output_section->vma
3777 + htab->elf.sgot->output_offset + off);
3778
3779 if (GOT_TLS_GD_P (tls_type))
3780 dr_type = R_X86_64_DTPMOD64;
3781 else if (GOT_TLS_GDESC_P (tls_type))
3782 goto dr_done;
3783 else
3784 dr_type = R_X86_64_TPOFF64;
3785
3786 bfd_put_64 (output_bfd, 0, htab->elf.sgot->contents + off);
3787 outrel.r_addend = 0;
3788 if ((dr_type == R_X86_64_TPOFF64
3789 || dr_type == R_X86_64_TLSDESC) && indx == 0)
3790 outrel.r_addend = relocation - elf_x86_64_dtpoff_base (info);
3791 outrel.r_info = htab->r_info (indx, dr_type);
3792
3793 elf_append_rela (output_bfd, sreloc, &outrel);
3794
3795 if (GOT_TLS_GD_P (tls_type))
3796 {
3797 if (indx == 0)
3798 {
3799 BFD_ASSERT (! unresolved_reloc);
3800 bfd_put_64 (output_bfd,
3801 relocation - elf_x86_64_dtpoff_base (info),
3802 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3803 }
3804 else
3805 {
3806 bfd_put_64 (output_bfd, 0,
3807 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3808 outrel.r_info = htab->r_info (indx,
3809 R_X86_64_DTPOFF64);
3810 outrel.r_offset += GOT_ENTRY_SIZE;
3811 elf_append_rela (output_bfd, sreloc,
3812 &outrel);
3813 }
3814 }
3815
3816 dr_done:
3817 if (h != NULL)
3818 h->got.offset |= 1;
3819 else
3820 local_got_offsets[r_symndx] |= 1;
3821 }
3822
3823 if (off >= (bfd_vma) -2
3824 && ! GOT_TLS_GDESC_P (tls_type))
3825 abort ();
3826 if (r_type == ELF32_R_TYPE (rel->r_info))
3827 {
3828 if (r_type == R_X86_64_GOTPC32_TLSDESC
3829 || r_type == R_X86_64_TLSDESC_CALL)
3830 relocation = htab->elf.sgotplt->output_section->vma
3831 + htab->elf.sgotplt->output_offset
3832 + offplt + htab->sgotplt_jump_table_size;
3833 else
3834 relocation = htab->elf.sgot->output_section->vma
3835 + htab->elf.sgot->output_offset + off;
3836 unresolved_reloc = FALSE;
3837 }
3838 else
3839 {
3840 bfd_vma roff = rel->r_offset;
3841
3842 if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3843 {
3844 /* GD->IE transition. For 64bit, change
3845 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3846 .word 0x6666; rex64; call __tls_get_addr@plt
3847 into:
3848 movq %fs:0, %rax
3849 addq foo@gottpoff(%rip), %rax
3850 For 32bit, change
3851 leaq foo@tlsgd(%rip), %rdi
3852 .word 0x6666; rex64; call __tls_get_addr@plt
3853 into:
3854 movl %fs:0, %eax
3855 addq foo@gottpoff(%rip), %rax */
3856 if (ABI_64_P (output_bfd))
3857 memcpy (contents + roff - 4,
3858 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3859 16);
3860 else
3861 memcpy (contents + roff - 3,
3862 "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3863 15);
3864
3865 relocation = (htab->elf.sgot->output_section->vma
3866 + htab->elf.sgot->output_offset + off
3867 - roff
3868 - input_section->output_section->vma
3869 - input_section->output_offset
3870 - 12);
3871 bfd_put_32 (output_bfd, relocation,
3872 contents + roff + 8);
3873 /* Skip R_X86_64_PLT32. */
3874 rel++;
3875 continue;
3876 }
3877 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3878 {
3879 /* GDesc -> IE transition.
3880 It's originally something like:
3881 leaq x@tlsdesc(%rip), %rax
3882
3883 Change it to:
3884 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax. */
3885
3886 /* Now modify the instruction as appropriate. To
3887 turn a leaq into a movq in the form we use it, it
3888 suffices to change the second byte from 0x8d to
3889 0x8b. */
3890 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3891
3892 bfd_put_32 (output_bfd,
3893 htab->elf.sgot->output_section->vma
3894 + htab->elf.sgot->output_offset + off
3895 - rel->r_offset
3896 - input_section->output_section->vma
3897 - input_section->output_offset
3898 - 4,
3899 contents + roff);
3900 continue;
3901 }
3902 else if (ELF32_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3903 {
3904 /* GDesc -> IE transition.
3905 It's originally:
3906 call *(%rax)
3907
3908 Change it to:
3909 xchg %ax, %ax. */
3910
3911 bfd_put_8 (output_bfd, 0x66, contents + roff);
3912 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3913 continue;
3914 }
3915 else
3916 BFD_ASSERT (FALSE);
3917 }
3918 break;
3919
3920 case R_X86_64_TLSLD:
3921 if (! elf_x86_64_tls_transition (info, input_bfd,
3922 input_section, contents,
3923 symtab_hdr, sym_hashes,
3924 &r_type, GOT_UNKNOWN,
3925 rel, relend, h, r_symndx))
3926 return FALSE;
3927
3928 if (r_type != R_X86_64_TLSLD)
3929 {
3930 /* LD->LE transition:
3931 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3932 For 64bit, we change it into:
3933 .word 0x6666; .byte 0x66; movq %fs:0, %rax.
3934 For 32bit, we change it into:
3935 nopl 0x0(%rax); movl %fs:0, %eax. */
3936
3937 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3938 if (ABI_64_P (output_bfd))
3939 memcpy (contents + rel->r_offset - 3,
3940 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3941 else
3942 memcpy (contents + rel->r_offset - 3,
3943 "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0", 12);
3944 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3945 rel++;
3946 continue;
3947 }
3948
3949 if (htab->elf.sgot == NULL)
3950 abort ();
3951
3952 off = htab->tls_ld_got.offset;
3953 if (off & 1)
3954 off &= ~1;
3955 else
3956 {
3957 Elf_Internal_Rela outrel;
3958
3959 if (htab->elf.srelgot == NULL)
3960 abort ();
3961
3962 outrel.r_offset = (htab->elf.sgot->output_section->vma
3963 + htab->elf.sgot->output_offset + off);
3964
3965 bfd_put_64 (output_bfd, 0,
3966 htab->elf.sgot->contents + off);
3967 bfd_put_64 (output_bfd, 0,
3968 htab->elf.sgot->contents + off + GOT_ENTRY_SIZE);
3969 outrel.r_info = htab->r_info (0, R_X86_64_DTPMOD64);
3970 outrel.r_addend = 0;
3971 elf_append_rela (output_bfd, htab->elf.srelgot,
3972 &outrel);
3973 htab->tls_ld_got.offset |= 1;
3974 }
3975 relocation = htab->elf.sgot->output_section->vma
3976 + htab->elf.sgot->output_offset + off;
3977 unresolved_reloc = FALSE;
3978 break;
3979
3980 case R_X86_64_DTPOFF32:
3981 if (!info->executable|| (input_section->flags & SEC_CODE) == 0)
3982 relocation -= elf_x86_64_dtpoff_base (info);
3983 else
3984 relocation = elf_x86_64_tpoff (info, relocation);
3985 break;
3986
3987 case R_X86_64_TPOFF32:
3988 case R_X86_64_TPOFF64:
3989 BFD_ASSERT (info->executable);
3990 relocation = elf_x86_64_tpoff (info, relocation);
3991 break;
3992
3993 default:
3994 break;
3995 }
3996
3997 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3998 because such sections are not SEC_ALLOC and thus ld.so will
3999 not process them. */
4000 if (unresolved_reloc
4001 && !((input_section->flags & SEC_DEBUGGING) != 0
4002 && h->def_dynamic))
4003 (*_bfd_error_handler)
4004 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
4005 input_bfd,
4006 input_section,
4007 (long) rel->r_offset,
4008 howto->name,
4009 h->root.root.string);
4010
4011 do_relocation:
4012 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
4013 contents, rel->r_offset,
4014 relocation, rel->r_addend);
4015
4016 check_relocation_error:
4017 if (r != bfd_reloc_ok)
4018 {
4019 const char *name;
4020
4021 if (h != NULL)
4022 name = h->root.root.string;
4023 else
4024 {
4025 name = bfd_elf_string_from_elf_section (input_bfd,
4026 symtab_hdr->sh_link,
4027 sym->st_name);
4028 if (name == NULL)
4029 return FALSE;
4030 if (*name == '\0')
4031 name = bfd_section_name (input_bfd, sec);
4032 }
4033
4034 if (r == bfd_reloc_overflow)
4035 {
4036 if (! ((*info->callbacks->reloc_overflow)
4037 (info, (h ? &h->root : NULL), name, howto->name,
4038 (bfd_vma) 0, input_bfd, input_section,
4039 rel->r_offset)))
4040 return FALSE;
4041 }
4042 else
4043 {
4044 (*_bfd_error_handler)
4045 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
4046 input_bfd, input_section,
4047 (long) rel->r_offset, name, (int) r);
4048 return FALSE;
4049 }
4050 }
4051 }
4052
4053 return TRUE;
4054 }
4055
4056 /* Finish up dynamic symbol handling. We set the contents of various
4057 dynamic sections here. */
4058
4059 static bfd_boolean
4060 elf_x86_64_finish_dynamic_symbol (bfd *output_bfd,
4061 struct bfd_link_info *info,
4062 struct elf_link_hash_entry *h,
4063 Elf_Internal_Sym *sym)
4064 {
4065 struct elf_x86_64_link_hash_table *htab;
4066
4067 htab = elf_x86_64_hash_table (info);
4068 if (htab == NULL)
4069 return FALSE;
4070
4071 if (h->plt.offset != (bfd_vma) -1)
4072 {
4073 bfd_vma plt_index;
4074 bfd_vma got_offset;
4075 Elf_Internal_Rela rela;
4076 bfd_byte *loc;
4077 asection *plt, *gotplt, *relplt;
4078 const struct elf_backend_data *bed;
4079
4080 /* When building a static executable, use .iplt, .igot.plt and
4081 .rela.iplt sections for STT_GNU_IFUNC symbols. */
4082 if (htab->elf.splt != NULL)
4083 {
4084 plt = htab->elf.splt;
4085 gotplt = htab->elf.sgotplt;
4086 relplt = htab->elf.srelplt;
4087 }
4088 else
4089 {
4090 plt = htab->elf.iplt;
4091 gotplt = htab->elf.igotplt;
4092 relplt = htab->elf.irelplt;
4093 }
4094
4095 /* This symbol has an entry in the procedure linkage table. Set
4096 it up. */
4097 if ((h->dynindx == -1
4098 && !((h->forced_local || info->executable)
4099 && h->def_regular
4100 && h->type == STT_GNU_IFUNC))
4101 || plt == NULL
4102 || gotplt == NULL
4103 || relplt == NULL)
4104 return FALSE;
4105
4106 /* Get the index in the procedure linkage table which
4107 corresponds to this symbol. This is the index of this symbol
4108 in all the symbols for which we are making plt entries. The
4109 first entry in the procedure linkage table is reserved.
4110
4111 Get the offset into the .got table of the entry that
4112 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
4113 bytes. The first three are reserved for the dynamic linker.
4114
4115 For static executables, we don't reserve anything. */
4116
4117 if (plt == htab->elf.splt)
4118 {
4119 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
4120 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
4121 }
4122 else
4123 {
4124 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
4125 got_offset = plt_index * GOT_ENTRY_SIZE;
4126 }
4127
4128 /* Fill in the entry in the procedure linkage table. */
4129 memcpy (plt->contents + h->plt.offset, elf_x86_64_plt_entry,
4130 PLT_ENTRY_SIZE);
4131
4132 /* Insert the relocation positions of the plt section. The magic
4133 numbers at the end of the statements are the positions of the
4134 relocations in the plt section. */
4135 /* Put offset for jmp *name@GOTPCREL(%rip), since the
4136 instruction uses 6 bytes, subtract this value. */
4137 bfd_put_32 (output_bfd,
4138 (gotplt->output_section->vma
4139 + gotplt->output_offset
4140 + got_offset
4141 - plt->output_section->vma
4142 - plt->output_offset
4143 - h->plt.offset
4144 - 6),
4145 plt->contents + h->plt.offset + 2);
4146
4147 /* Don't fill PLT entry for static executables. */
4148 if (plt == htab->elf.splt)
4149 {
4150 /* Put relocation index. */
4151 bfd_put_32 (output_bfd, plt_index,
4152 plt->contents + h->plt.offset + 7);
4153 /* Put offset for jmp .PLT0. */
4154 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
4155 plt->contents + h->plt.offset + 12);
4156 }
4157
4158 /* Fill in the entry in the global offset table, initially this
4159 points to the pushq instruction in the PLT which is at offset 6. */
4160 bfd_put_64 (output_bfd, (plt->output_section->vma
4161 + plt->output_offset
4162 + h->plt.offset + 6),
4163 gotplt->contents + got_offset);
4164
4165 /* Fill in the entry in the .rela.plt section. */
4166 rela.r_offset = (gotplt->output_section->vma
4167 + gotplt->output_offset
4168 + got_offset);
4169 if (h->dynindx == -1
4170 || ((info->executable
4171 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
4172 && h->def_regular
4173 && h->type == STT_GNU_IFUNC))
4174 {
4175 /* If an STT_GNU_IFUNC symbol is locally defined, generate
4176 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
4177 rela.r_info = htab->r_info (0, R_X86_64_IRELATIVE);
4178 rela.r_addend = (h->root.u.def.value
4179 + h->root.u.def.section->output_section->vma
4180 + h->root.u.def.section->output_offset);
4181 }
4182 else
4183 {
4184 rela.r_info = htab->r_info (h->dynindx, R_X86_64_JUMP_SLOT);
4185 rela.r_addend = 0;
4186 }
4187
4188 bed = get_elf_backend_data (output_bfd);
4189 loc = relplt->contents + plt_index * bed->s->sizeof_rela;
4190 bed->s->swap_reloca_out (output_bfd, &rela, loc);
4191
4192 if (!h->def_regular)
4193 {
4194 /* Mark the symbol as undefined, rather than as defined in
4195 the .plt section. Leave the value if there were any
4196 relocations where pointer equality matters (this is a clue
4197 for the dynamic linker, to make function pointer
4198 comparisons work between an application and shared
4199 library), otherwise set it to zero. If a function is only
4200 called from a binary, there is no need to slow down
4201 shared libraries because of that. */
4202 sym->st_shndx = SHN_UNDEF;
4203 if (!h->pointer_equality_needed)
4204 sym->st_value = 0;
4205 }
4206 }
4207
4208 if (h->got.offset != (bfd_vma) -1
4209 && ! GOT_TLS_GD_ANY_P (elf_x86_64_hash_entry (h)->tls_type)
4210 && elf_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
4211 {
4212 Elf_Internal_Rela rela;
4213
4214 /* This symbol has an entry in the global offset table. Set it
4215 up. */
4216 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL)
4217 abort ();
4218
4219 rela.r_offset = (htab->elf.sgot->output_section->vma
4220 + htab->elf.sgot->output_offset
4221 + (h->got.offset &~ (bfd_vma) 1));
4222
4223 /* If this is a static link, or it is a -Bsymbolic link and the
4224 symbol is defined locally or was forced to be local because
4225 of a version file, we just want to emit a RELATIVE reloc.
4226 The entry in the global offset table will already have been
4227 initialized in the relocate_section function. */
4228 if (h->def_regular
4229 && h->type == STT_GNU_IFUNC)
4230 {
4231 if (info->shared)
4232 {
4233 /* Generate R_X86_64_GLOB_DAT. */
4234 goto do_glob_dat;
4235 }
4236 else
4237 {
4238 asection *plt;
4239
4240 if (!h->pointer_equality_needed)
4241 abort ();
4242
4243 /* For non-shared object, we can't use .got.plt, which
4244 contains the real function addres if we need pointer
4245 equality. We load the GOT entry with the PLT entry. */
4246 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt;
4247 bfd_put_64 (output_bfd, (plt->output_section->vma
4248 + plt->output_offset
4249 + h->plt.offset),
4250 htab->elf.sgot->contents + h->got.offset);
4251 return TRUE;
4252 }
4253 }
4254 else if (info->shared
4255 && SYMBOL_REFERENCES_LOCAL (info, h))
4256 {
4257 if (!h->def_regular)
4258 return FALSE;
4259 BFD_ASSERT((h->got.offset & 1) != 0);
4260 rela.r_info = htab->r_info (0, R_X86_64_RELATIVE);
4261 rela.r_addend = (h->root.u.def.value
4262 + h->root.u.def.section->output_section->vma
4263 + h->root.u.def.section->output_offset);
4264 }
4265 else
4266 {
4267 BFD_ASSERT((h->got.offset & 1) == 0);
4268 do_glob_dat:
4269 bfd_put_64 (output_bfd, (bfd_vma) 0,
4270 htab->elf.sgot->contents + h->got.offset);
4271 rela.r_info = htab->r_info (h->dynindx, R_X86_64_GLOB_DAT);
4272 rela.r_addend = 0;
4273 }
4274
4275 elf_append_rela (output_bfd, htab->elf.srelgot, &rela);
4276 }
4277
4278 if (h->needs_copy)
4279 {
4280 Elf_Internal_Rela rela;
4281
4282 /* This symbol needs a copy reloc. Set it up. */
4283
4284 if (h->dynindx == -1
4285 || (h->root.type != bfd_link_hash_defined
4286 && h->root.type != bfd_link_hash_defweak)
4287 || htab->srelbss == NULL)
4288 abort ();
4289
4290 rela.r_offset = (h->root.u.def.value
4291 + h->root.u.def.section->output_section->vma
4292 + h->root.u.def.section->output_offset);
4293 rela.r_info = htab->r_info (h->dynindx, R_X86_64_COPY);
4294 rela.r_addend = 0;
4295 elf_append_rela (output_bfd, htab->srelbss, &rela);
4296 }
4297
4298 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. SYM may
4299 be NULL for local symbols. */
4300 if (sym != NULL
4301 && (strcmp (h->root.root.string, "_DYNAMIC") == 0
4302 || h == htab->elf.hgot))
4303 sym->st_shndx = SHN_ABS;
4304
4305 return TRUE;
4306 }
4307
4308 /* Finish up local dynamic symbol handling. We set the contents of
4309 various dynamic sections here. */
4310
4311 static bfd_boolean
4312 elf_x86_64_finish_local_dynamic_symbol (void **slot, void *inf)
4313 {
4314 struct elf_link_hash_entry *h
4315 = (struct elf_link_hash_entry *) *slot;
4316 struct bfd_link_info *info
4317 = (struct bfd_link_info *) inf;
4318
4319 return elf_x86_64_finish_dynamic_symbol (info->output_bfd,
4320 info, h, NULL);
4321 }
4322
4323 /* Used to decide how to sort relocs in an optimal manner for the
4324 dynamic linker, before writing them out. */
4325
4326 static enum elf_reloc_type_class
4327 elf_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
4328 {
4329 switch ((int) ELF32_R_TYPE (rela->r_info))
4330 {
4331 case R_X86_64_RELATIVE:
4332 return reloc_class_relative;
4333 case R_X86_64_JUMP_SLOT:
4334 return reloc_class_plt;
4335 case R_X86_64_COPY:
4336 return reloc_class_copy;
4337 default:
4338 return reloc_class_normal;
4339 }
4340 }
4341
4342 /* Finish up the dynamic sections. */
4343
4344 static bfd_boolean
4345 elf_x86_64_finish_dynamic_sections (bfd *output_bfd,
4346 struct bfd_link_info *info)
4347 {
4348 struct elf_x86_64_link_hash_table *htab;
4349 bfd *dynobj;
4350 asection *sdyn;
4351
4352 htab = elf_x86_64_hash_table (info);
4353 if (htab == NULL)
4354 return FALSE;
4355
4356 dynobj = htab->elf.dynobj;
4357 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4358
4359 if (htab->elf.dynamic_sections_created)
4360 {
4361 bfd_byte *dyncon, *dynconend;
4362 const struct elf_backend_data *bed;
4363 bfd_size_type sizeof_dyn;
4364
4365 if (sdyn == NULL || htab->elf.sgot == NULL)
4366 abort ();
4367
4368 bed = get_elf_backend_data (dynobj);
4369 sizeof_dyn = bed->s->sizeof_dyn;
4370 dyncon = sdyn->contents;
4371 dynconend = sdyn->contents + sdyn->size;
4372 for (; dyncon < dynconend; dyncon += sizeof_dyn)
4373 {
4374 Elf_Internal_Dyn dyn;
4375 asection *s;
4376
4377 (*bed->s->swap_dyn_in) (dynobj, dyncon, &dyn);
4378
4379 switch (dyn.d_tag)
4380 {
4381 default:
4382 continue;
4383
4384 case DT_PLTGOT:
4385 s = htab->elf.sgotplt;
4386 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4387 break;
4388
4389 case DT_JMPREL:
4390 dyn.d_un.d_ptr = htab->elf.srelplt->output_section->vma;
4391 break;
4392
4393 case DT_PLTRELSZ:
4394 s = htab->elf.srelplt->output_section;
4395 dyn.d_un.d_val = s->size;
4396 break;
4397
4398 case DT_RELASZ:
4399 /* The procedure linkage table relocs (DT_JMPREL) should
4400 not be included in the overall relocs (DT_RELA).
4401 Therefore, we override the DT_RELASZ entry here to
4402 make it not include the JMPREL relocs. Since the
4403 linker script arranges for .rela.plt to follow all
4404 other relocation sections, we don't have to worry
4405 about changing the DT_RELA entry. */
4406 if (htab->elf.srelplt != NULL)
4407 {
4408 s = htab->elf.srelplt->output_section;
4409 dyn.d_un.d_val -= s->size;
4410 }
4411 break;
4412
4413 case DT_TLSDESC_PLT:
4414 s = htab->elf.splt;
4415 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4416 + htab->tlsdesc_plt;
4417 break;
4418
4419 case DT_TLSDESC_GOT:
4420 s = htab->elf.sgot;
4421 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4422 + htab->tlsdesc_got;
4423 break;
4424 }
4425
4426 (*bed->s->swap_dyn_out) (output_bfd, &dyn, dyncon);
4427 }
4428
4429 /* Fill in the special first entry in the procedure linkage table. */
4430 if (htab->elf.splt && htab->elf.splt->size > 0)
4431 {
4432 /* Fill in the first entry in the procedure linkage table. */
4433 memcpy (htab->elf.splt->contents, elf_x86_64_plt0_entry,
4434 PLT_ENTRY_SIZE);
4435 /* Add offset for pushq GOT+8(%rip), since the instruction
4436 uses 6 bytes subtract this value. */
4437 bfd_put_32 (output_bfd,
4438 (htab->elf.sgotplt->output_section->vma
4439 + htab->elf.sgotplt->output_offset
4440 + 8
4441 - htab->elf.splt->output_section->vma
4442 - htab->elf.splt->output_offset
4443 - 6),
4444 htab->elf.splt->contents + 2);
4445 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4446 the end of the instruction. */
4447 bfd_put_32 (output_bfd,
4448 (htab->elf.sgotplt->output_section->vma
4449 + htab->elf.sgotplt->output_offset
4450 + 16
4451 - htab->elf.splt->output_section->vma
4452 - htab->elf.splt->output_offset
4453 - 12),
4454 htab->elf.splt->contents + 8);
4455
4456 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize =
4457 PLT_ENTRY_SIZE;
4458
4459 if (htab->tlsdesc_plt)
4460 {
4461 bfd_put_64 (output_bfd, (bfd_vma) 0,
4462 htab->elf.sgot->contents + htab->tlsdesc_got);
4463
4464 memcpy (htab->elf.splt->contents + htab->tlsdesc_plt,
4465 elf_x86_64_plt0_entry,
4466 PLT_ENTRY_SIZE);
4467
4468 /* Add offset for pushq GOT+8(%rip), since the
4469 instruction uses 6 bytes subtract this value. */
4470 bfd_put_32 (output_bfd,
4471 (htab->elf.sgotplt->output_section->vma
4472 + htab->elf.sgotplt->output_offset
4473 + 8
4474 - htab->elf.splt->output_section->vma
4475 - htab->elf.splt->output_offset
4476 - htab->tlsdesc_plt
4477 - 6),
4478 htab->elf.splt->contents + htab->tlsdesc_plt + 2);
4479 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4480 htab->tlsdesc_got. The 12 is the offset to the end of
4481 the instruction. */
4482 bfd_put_32 (output_bfd,
4483 (htab->elf.sgot->output_section->vma
4484 + htab->elf.sgot->output_offset
4485 + htab->tlsdesc_got
4486 - htab->elf.splt->output_section->vma
4487 - htab->elf.splt->output_offset
4488 - htab->tlsdesc_plt
4489 - 12),
4490 htab->elf.splt->contents + htab->tlsdesc_plt + 8);
4491 }
4492 }
4493 }
4494
4495 if (htab->elf.sgotplt)
4496 {
4497 if (bfd_is_abs_section (htab->elf.sgotplt->output_section))
4498 {
4499 (*_bfd_error_handler)
4500 (_("discarded output section: `%A'"), htab->elf.sgotplt);
4501 return FALSE;
4502 }
4503
4504 /* Fill in the first three entries in the global offset table. */
4505 if (htab->elf.sgotplt->size > 0)
4506 {
4507 /* Set the first entry in the global offset table to the address of
4508 the dynamic section. */
4509 if (sdyn == NULL)
4510 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents);
4511 else
4512 bfd_put_64 (output_bfd,
4513 sdyn->output_section->vma + sdyn->output_offset,
4514 htab->elf.sgotplt->contents);
4515 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4516 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE);
4517 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->elf.sgotplt->contents + GOT_ENTRY_SIZE*2);
4518 }
4519
4520 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize =
4521 GOT_ENTRY_SIZE;
4522 }
4523
4524 if (htab->elf.sgot && htab->elf.sgot->size > 0)
4525 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
4526 = GOT_ENTRY_SIZE;
4527
4528 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */
4529 htab_traverse (htab->loc_hash_table,
4530 elf_x86_64_finish_local_dynamic_symbol,
4531 info);
4532
4533 return TRUE;
4534 }
4535
4536 /* Return address for Ith PLT stub in section PLT, for relocation REL
4537 or (bfd_vma) -1 if it should not be included. */
4538
4539 static bfd_vma
4540 elf_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4541 const arelent *rel ATTRIBUTE_UNUSED)
4542 {
4543 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4544 }
4545
4546 /* Handle an x86-64 specific section when reading an object file. This
4547 is called when elfcode.h finds a section with an unknown type. */
4548
4549 static bfd_boolean
4550 elf_x86_64_section_from_shdr (bfd *abfd,
4551 Elf_Internal_Shdr *hdr,
4552 const char *name,
4553 int shindex)
4554 {
4555 if (hdr->sh_type != SHT_X86_64_UNWIND)
4556 return FALSE;
4557
4558 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4559 return FALSE;
4560
4561 return TRUE;
4562 }
4563
4564 /* Hook called by the linker routine which adds symbols from an object
4565 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4566 of .bss. */
4567
4568 static bfd_boolean
4569 elf_x86_64_add_symbol_hook (bfd *abfd,
4570 struct bfd_link_info *info,
4571 Elf_Internal_Sym *sym,
4572 const char **namep ATTRIBUTE_UNUSED,
4573 flagword *flagsp ATTRIBUTE_UNUSED,
4574 asection **secp,
4575 bfd_vma *valp)
4576 {
4577 asection *lcomm;
4578
4579 switch (sym->st_shndx)
4580 {
4581 case SHN_X86_64_LCOMMON:
4582 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4583 if (lcomm == NULL)
4584 {
4585 lcomm = bfd_make_section_with_flags (abfd,
4586 "LARGE_COMMON",
4587 (SEC_ALLOC
4588 | SEC_IS_COMMON
4589 | SEC_LINKER_CREATED));
4590 if (lcomm == NULL)
4591 return FALSE;
4592 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4593 }
4594 *secp = lcomm;
4595 *valp = sym->st_size;
4596 return TRUE;
4597 }
4598
4599 if ((abfd->flags & DYNAMIC) == 0
4600 && (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
4601 || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE))
4602 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4603
4604 return TRUE;
4605 }
4606
4607
4608 /* Given a BFD section, try to locate the corresponding ELF section
4609 index. */
4610
4611 static bfd_boolean
4612 elf_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4613 asection *sec, int *index_return)
4614 {
4615 if (sec == &_bfd_elf_large_com_section)
4616 {
4617 *index_return = SHN_X86_64_LCOMMON;
4618 return TRUE;
4619 }
4620 return FALSE;
4621 }
4622
4623 /* Process a symbol. */
4624
4625 static void
4626 elf_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4627 asymbol *asym)
4628 {
4629 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4630
4631 switch (elfsym->internal_elf_sym.st_shndx)
4632 {
4633 case SHN_X86_64_LCOMMON:
4634 asym->section = &_bfd_elf_large_com_section;
4635 asym->value = elfsym->internal_elf_sym.st_size;
4636 /* Common symbol doesn't set BSF_GLOBAL. */
4637 asym->flags &= ~BSF_GLOBAL;
4638 break;
4639 }
4640 }
4641
4642 static bfd_boolean
4643 elf_x86_64_common_definition (Elf_Internal_Sym *sym)
4644 {
4645 return (sym->st_shndx == SHN_COMMON
4646 || sym->st_shndx == SHN_X86_64_LCOMMON);
4647 }
4648
4649 static unsigned int
4650 elf_x86_64_common_section_index (asection *sec)
4651 {
4652 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4653 return SHN_COMMON;
4654 else
4655 return SHN_X86_64_LCOMMON;
4656 }
4657
4658 static asection *
4659 elf_x86_64_common_section (asection *sec)
4660 {
4661 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4662 return bfd_com_section_ptr;
4663 else
4664 return &_bfd_elf_large_com_section;
4665 }
4666
4667 static bfd_boolean
4668 elf_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4669 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4670 struct elf_link_hash_entry *h,
4671 Elf_Internal_Sym *sym,
4672 asection **psec,
4673 bfd_vma *pvalue ATTRIBUTE_UNUSED,
4674 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4675 bfd_boolean *skip ATTRIBUTE_UNUSED,
4676 bfd_boolean *override ATTRIBUTE_UNUSED,
4677 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4678 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4679 bfd_boolean *newdyn ATTRIBUTE_UNUSED,
4680 bfd_boolean *newdef,
4681 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4682 bfd_boolean *newweak ATTRIBUTE_UNUSED,
4683 bfd *abfd ATTRIBUTE_UNUSED,
4684 asection **sec,
4685 bfd_boolean *olddyn ATTRIBUTE_UNUSED,
4686 bfd_boolean *olddef,
4687 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4688 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4689 bfd *oldbfd,
4690 asection **oldsec)
4691 {
4692 /* A normal common symbol and a large common symbol result in a
4693 normal common symbol. We turn the large common symbol into a
4694 normal one. */
4695 if (!*olddef
4696 && h->root.type == bfd_link_hash_common
4697 && !*newdef
4698 && bfd_is_com_section (*sec)
4699 && *oldsec != *sec)
4700 {
4701 if (sym->st_shndx == SHN_COMMON
4702 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4703 {
4704 h->root.u.c.p->section
4705 = bfd_make_section_old_way (oldbfd, "COMMON");
4706 h->root.u.c.p->section->flags = SEC_ALLOC;
4707 }
4708 else if (sym->st_shndx == SHN_X86_64_LCOMMON
4709 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4710 *psec = *sec = bfd_com_section_ptr;
4711 }
4712
4713 return TRUE;
4714 }
4715
4716 static int
4717 elf_x86_64_additional_program_headers (bfd *abfd,
4718 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4719 {
4720 asection *s;
4721 int count = 0;
4722
4723 /* Check to see if we need a large readonly segment. */
4724 s = bfd_get_section_by_name (abfd, ".lrodata");
4725 if (s && (s->flags & SEC_LOAD))
4726 count++;
4727
4728 /* Check to see if we need a large data segment. Since .lbss sections
4729 is placed right after the .bss section, there should be no need for
4730 a large data segment just because of .lbss. */
4731 s = bfd_get_section_by_name (abfd, ".ldata");
4732 if (s && (s->flags & SEC_LOAD))
4733 count++;
4734
4735 return count;
4736 }
4737
4738 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4739
4740 static bfd_boolean
4741 elf_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4742 {
4743 if (h->plt.offset != (bfd_vma) -1
4744 && !h->def_regular
4745 && !h->pointer_equality_needed)
4746 return FALSE;
4747
4748 return _bfd_elf_hash_symbol (h);
4749 }
4750
4751 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. */
4752
4753 static bfd_boolean
4754 elf_x86_64_relocs_compatible (const bfd_target *input,
4755 const bfd_target *output)
4756 {
4757 return ((xvec_get_elf_backend_data (input)->s->elfclass
4758 == xvec_get_elf_backend_data (output)->s->elfclass)
4759 && _bfd_elf_relocs_compatible (input, output));
4760 }
4761
4762 static const struct bfd_elf_special_section
4763 elf_x86_64_special_sections[]=
4764 {
4765 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4766 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4767 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4768 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4769 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4770 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4771 { NULL, 0, 0, 0, 0 }
4772 };
4773
4774 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
4775 #define TARGET_LITTLE_NAME "elf64-x86-64"
4776 #define ELF_ARCH bfd_arch_i386
4777 #define ELF_TARGET_ID X86_64_ELF_DATA
4778 #define ELF_MACHINE_CODE EM_X86_64
4779 #define ELF_MAXPAGESIZE 0x200000
4780 #define ELF_MINPAGESIZE 0x1000
4781 #define ELF_COMMONPAGESIZE 0x1000
4782
4783 #define elf_backend_can_gc_sections 1
4784 #define elf_backend_can_refcount 1
4785 #define elf_backend_want_got_plt 1
4786 #define elf_backend_plt_readonly 1
4787 #define elf_backend_want_plt_sym 0
4788 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
4789 #define elf_backend_rela_normal 1
4790
4791 #define elf_info_to_howto elf_x86_64_info_to_howto
4792
4793 #define bfd_elf64_bfd_link_hash_table_create \
4794 elf_x86_64_link_hash_table_create
4795 #define bfd_elf64_bfd_link_hash_table_free \
4796 elf_x86_64_link_hash_table_free
4797 #define bfd_elf64_bfd_reloc_type_lookup elf_x86_64_reloc_type_lookup
4798 #define bfd_elf64_bfd_reloc_name_lookup \
4799 elf_x86_64_reloc_name_lookup
4800
4801 #define elf_backend_adjust_dynamic_symbol elf_x86_64_adjust_dynamic_symbol
4802 #define elf_backend_relocs_compatible elf_x86_64_relocs_compatible
4803 #define elf_backend_check_relocs elf_x86_64_check_relocs
4804 #define elf_backend_copy_indirect_symbol elf_x86_64_copy_indirect_symbol
4805 #define elf_backend_create_dynamic_sections elf_x86_64_create_dynamic_sections
4806 #define elf_backend_finish_dynamic_sections elf_x86_64_finish_dynamic_sections
4807 #define elf_backend_finish_dynamic_symbol elf_x86_64_finish_dynamic_symbol
4808 #define elf_backend_gc_mark_hook elf_x86_64_gc_mark_hook
4809 #define elf_backend_gc_sweep_hook elf_x86_64_gc_sweep_hook
4810 #define elf_backend_grok_prstatus elf_x86_64_grok_prstatus
4811 #define elf_backend_grok_psinfo elf_x86_64_grok_psinfo
4812 #ifdef CORE_HEADER
4813 #define elf_backend_write_core_note elf_x86_64_write_core_note
4814 #endif
4815 #define elf_backend_reloc_type_class elf_x86_64_reloc_type_class
4816 #define elf_backend_relocate_section elf_x86_64_relocate_section
4817 #define elf_backend_size_dynamic_sections elf_x86_64_size_dynamic_sections
4818 #define elf_backend_always_size_sections elf_x86_64_always_size_sections
4819 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4820 #define elf_backend_plt_sym_val elf_x86_64_plt_sym_val
4821 #define elf_backend_object_p elf64_x86_64_elf_object_p
4822 #define bfd_elf64_mkobject elf_x86_64_mkobject
4823
4824 #define elf_backend_section_from_shdr \
4825 elf_x86_64_section_from_shdr
4826
4827 #define elf_backend_section_from_bfd_section \
4828 elf_x86_64_elf_section_from_bfd_section
4829 #define elf_backend_add_symbol_hook \
4830 elf_x86_64_add_symbol_hook
4831 #define elf_backend_symbol_processing \
4832 elf_x86_64_symbol_processing
4833 #define elf_backend_common_section_index \
4834 elf_x86_64_common_section_index
4835 #define elf_backend_common_section \
4836 elf_x86_64_common_section
4837 #define elf_backend_common_definition \
4838 elf_x86_64_common_definition
4839 #define elf_backend_merge_symbol \
4840 elf_x86_64_merge_symbol
4841 #define elf_backend_special_sections \
4842 elf_x86_64_special_sections
4843 #define elf_backend_additional_program_headers \
4844 elf_x86_64_additional_program_headers
4845 #define elf_backend_hash_symbol \
4846 elf_x86_64_hash_symbol
4847
4848 #undef elf_backend_post_process_headers
4849 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4850
4851 #include "elf64-target.h"
4852
4853 /* FreeBSD support. */
4854
4855 #undef TARGET_LITTLE_SYM
4856 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
4857 #undef TARGET_LITTLE_NAME
4858 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
4859
4860 #undef ELF_OSABI
4861 #define ELF_OSABI ELFOSABI_FREEBSD
4862
4863 #undef elf64_bed
4864 #define elf64_bed elf64_x86_64_fbsd_bed
4865
4866 #include "elf64-target.h"
4867
4868 /* Solaris 2 support. */
4869
4870 #undef TARGET_LITTLE_SYM
4871 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_sol2_vec
4872 #undef TARGET_LITTLE_NAME
4873 #define TARGET_LITTLE_NAME "elf64-x86-64-sol2"
4874
4875 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
4876 objects won't be recognized. */
4877 #undef ELF_OSABI
4878
4879 #undef elf64_bed
4880 #define elf64_bed elf64_x86_64_sol2_bed
4881
4882 /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
4883 boundary. */
4884 #undef elf_backend_static_tls_alignment
4885 #define elf_backend_static_tls_alignment 16
4886
4887 /* The Solaris 2 ABI requires a plt symbol on all platforms.
4888
4889 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output
4890 File, p.63. */
4891 #undef elf_backend_want_plt_sym
4892 #define elf_backend_want_plt_sym 1
4893
4894 #include "elf64-target.h"
4895
4896 /* Intel L1OM support. */
4897
4898 static bfd_boolean
4899 elf64_l1om_elf_object_p (bfd *abfd)
4900 {
4901 /* Set the right machine number for an L1OM elf64 file. */
4902 bfd_default_set_arch_mach (abfd, bfd_arch_l1om, bfd_mach_l1om);
4903 return TRUE;
4904 }
4905
4906 #undef TARGET_LITTLE_SYM
4907 #define TARGET_LITTLE_SYM bfd_elf64_l1om_vec
4908 #undef TARGET_LITTLE_NAME
4909 #define TARGET_LITTLE_NAME "elf64-l1om"
4910 #undef ELF_ARCH
4911 #define ELF_ARCH bfd_arch_l1om
4912
4913 #undef ELF_MACHINE_CODE
4914 #define ELF_MACHINE_CODE EM_L1OM
4915
4916 #undef ELF_OSABI
4917
4918 #undef elf64_bed
4919 #define elf64_bed elf64_l1om_bed
4920
4921 #undef elf_backend_object_p
4922 #define elf_backend_object_p elf64_l1om_elf_object_p
4923
4924 #undef elf_backend_post_process_headers
4925 #undef elf_backend_static_tls_alignment
4926
4927 #undef elf_backend_want_plt_sym
4928 #define elf_backend_want_plt_sym 0
4929
4930 #include "elf64-target.h"
4931
4932 /* FreeBSD L1OM support. */
4933
4934 #undef TARGET_LITTLE_SYM
4935 #define TARGET_LITTLE_SYM bfd_elf64_l1om_freebsd_vec
4936 #undef TARGET_LITTLE_NAME
4937 #define TARGET_LITTLE_NAME "elf64-l1om-freebsd"
4938
4939 #undef ELF_OSABI
4940 #define ELF_OSABI ELFOSABI_FREEBSD
4941
4942 #undef elf64_bed
4943 #define elf64_bed elf64_l1om_fbsd_bed
4944
4945 #undef elf_backend_post_process_headers
4946 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4947
4948 #include "elf64-target.h"
4949
4950 /* 32bit x86-64 support. */
4951
4952 static bfd_boolean
4953 elf32_x86_64_elf_object_p (bfd *abfd)
4954 {
4955 /* Set the right machine number for an x86-64 elf32 file. */
4956 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x64_32);
4957 return TRUE;
4958 }
4959
4960 #undef TARGET_LITTLE_SYM
4961 #define TARGET_LITTLE_SYM bfd_elf32_x86_64_vec
4962 #undef TARGET_LITTLE_NAME
4963 #define TARGET_LITTLE_NAME "elf32-x86-64"
4964
4965 #undef ELF_ARCH
4966 #define ELF_ARCH bfd_arch_i386
4967
4968 #undef ELF_MACHINE_CODE
4969 #define ELF_MACHINE_CODE EM_X86_64
4970
4971 #define bfd_elf32_bfd_link_hash_table_create \
4972 elf_x86_64_link_hash_table_create
4973 #define bfd_elf32_bfd_link_hash_table_free \
4974 elf_x86_64_link_hash_table_free
4975 #define bfd_elf32_bfd_reloc_type_lookup \
4976 elf_x86_64_reloc_type_lookup
4977 #define bfd_elf32_bfd_reloc_name_lookup \
4978 elf_x86_64_reloc_name_lookup
4979 #define bfd_elf32_mkobject \
4980 elf_x86_64_mkobject
4981
4982 #undef ELF_OSABI
4983
4984 #undef elf_backend_post_process_headers
4985
4986 #undef elf_backend_object_p
4987 #define elf_backend_object_p \
4988 elf32_x86_64_elf_object_p
4989
4990 #undef elf_backend_bfd_from_remote_memory
4991 #define elf_backend_bfd_from_remote_memory \
4992 _bfd_elf32_bfd_from_remote_memory
4993
4994 #undef elf_backend_size_info
4995 #define elf_backend_size_info \
4996 _bfd_elf32_size_info
4997
4998 #undef elf_backend_post_process_headers
4999 #define elf_backend_post_process_headers _bfd_elf_set_osabi
5000
5001 #include "elf32-target.h"