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