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