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