* elf.c (sym_is_global): Return a bfd_boolean.
[binutils.git] / bfd / elf64-x86-64.c
blob504a19b7b9833f1ee059760c2eba068b4fde1c0c
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka <jh@suse.cz>.
6 This file is part of BFD, the Binary File Descriptor library.
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 2 of the License, or
11 (at your option) any later version.
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.
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, MA 02110-1301, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
28 #include "elf/x86-64.h"
30 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
31 #define MINUS_ONE (~ (bfd_vma) 0)
33 /* The relocation "howto" table. Order of fields:
34 type, size, bitsize, pc_relative, complain_on_overflow,
35 special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */
36 static reloc_howto_type x86_64_elf_howto_table[] =
38 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
39 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
40 FALSE),
41 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
42 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
43 FALSE),
44 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
45 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
46 TRUE),
47 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
48 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
49 FALSE),
50 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
51 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
52 TRUE),
53 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
54 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
55 FALSE),
56 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
57 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
58 MINUS_ONE, FALSE),
59 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
60 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
61 MINUS_ONE, FALSE),
62 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
63 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
64 MINUS_ONE, FALSE),
65 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
66 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
67 0xffffffff, TRUE),
68 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
69 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
70 FALSE),
71 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
72 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
73 FALSE),
74 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
75 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
76 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
78 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
79 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
80 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
81 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
82 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
83 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
84 MINUS_ONE, FALSE),
85 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
86 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
87 MINUS_ONE, FALSE),
88 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
89 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
90 MINUS_ONE, FALSE),
91 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
92 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
93 0xffffffff, TRUE),
94 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
95 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
96 0xffffffff, TRUE),
97 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
98 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
99 0xffffffff, FALSE),
100 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
101 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
102 0xffffffff, TRUE),
103 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
104 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
105 0xffffffff, FALSE),
106 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
107 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
108 TRUE),
109 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
110 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
111 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
112 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
113 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
114 FALSE, 0xffffffff, 0xffffffff, TRUE),
115 EMPTY_HOWTO (27),
116 EMPTY_HOWTO (28),
117 EMPTY_HOWTO (29),
118 EMPTY_HOWTO (30),
119 EMPTY_HOWTO (31),
120 EMPTY_HOWTO (32),
121 EMPTY_HOWTO (33),
122 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
123 complain_overflow_bitfield, bfd_elf_generic_reloc,
124 "R_X86_64_GOTPC32_TLSDESC",
125 FALSE, 0xffffffff, 0xffffffff, TRUE),
126 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
127 complain_overflow_dont, bfd_elf_generic_reloc,
128 "R_X86_64_TLSDESC_CALL",
129 FALSE, 0, 0, FALSE),
130 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
131 complain_overflow_bitfield, bfd_elf_generic_reloc,
132 "R_X86_64_TLSDESC",
133 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
135 /* We have a gap in the reloc numbers here.
136 R_X86_64_standard counts the number up to this point, and
137 R_X86_64_vt_offset is the value to subtract from a reloc type of
138 R_X86_64_GNU_VT* to form an index into this table. */
139 #define R_X86_64_standard (R_X86_64_TLSDESC + 1)
140 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
142 /* GNU extension to record C++ vtable hierarchy. */
143 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
144 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
146 /* GNU extension to record C++ vtable member usage. */
147 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
148 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
149 FALSE)
152 /* Map BFD relocs to the x86_64 elf relocs. */
153 struct elf_reloc_map
155 bfd_reloc_code_real_type bfd_reloc_val;
156 unsigned char elf_reloc_val;
159 static const struct elf_reloc_map x86_64_reloc_map[] =
161 { BFD_RELOC_NONE, R_X86_64_NONE, },
162 { BFD_RELOC_64, R_X86_64_64, },
163 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
164 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
165 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
166 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
167 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
168 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
169 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
170 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
171 { BFD_RELOC_32, R_X86_64_32, },
172 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
173 { BFD_RELOC_16, R_X86_64_16, },
174 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
175 { BFD_RELOC_8, R_X86_64_8, },
176 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
177 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
178 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
179 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
180 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
181 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
182 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
183 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
184 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
185 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
186 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
187 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
188 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
189 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
190 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
191 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
192 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
195 static reloc_howto_type *
196 elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
198 unsigned i;
200 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
201 || r_type >= (unsigned int) R_X86_64_max)
203 if (r_type >= (unsigned int) R_X86_64_standard)
205 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
206 abfd, (int) r_type);
207 r_type = R_X86_64_NONE;
209 i = r_type;
211 else
212 i = r_type - (unsigned int) R_X86_64_vt_offset;
213 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
214 return &x86_64_elf_howto_table[i];
217 /* Given a BFD reloc type, return a HOWTO structure. */
218 static reloc_howto_type *
219 elf64_x86_64_reloc_type_lookup (bfd *abfd,
220 bfd_reloc_code_real_type code)
222 unsigned int i;
224 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
225 i++)
227 if (x86_64_reloc_map[i].bfd_reloc_val == code)
228 return elf64_x86_64_rtype_to_howto (abfd,
229 x86_64_reloc_map[i].elf_reloc_val);
231 return 0;
234 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
236 static void
237 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
238 Elf_Internal_Rela *dst)
240 unsigned r_type;
242 r_type = ELF64_R_TYPE (dst->r_info);
243 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
244 BFD_ASSERT (r_type == cache_ptr->howto->type);
247 /* Support for core dump NOTE sections. */
248 static bfd_boolean
249 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
251 int offset;
252 size_t size;
254 switch (note->descsz)
256 default:
257 return FALSE;
259 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
260 /* pr_cursig */
261 elf_tdata (abfd)->core_signal
262 = bfd_get_16 (abfd, note->descdata + 12);
264 /* pr_pid */
265 elf_tdata (abfd)->core_pid
266 = bfd_get_32 (abfd, note->descdata + 32);
268 /* pr_reg */
269 offset = 112;
270 size = 216;
272 break;
275 /* Make a ".reg/999" section. */
276 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
277 size, note->descpos + offset);
280 static bfd_boolean
281 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
283 switch (note->descsz)
285 default:
286 return FALSE;
288 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
289 elf_tdata (abfd)->core_program
290 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
291 elf_tdata (abfd)->core_command
292 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
295 /* Note that for some reason, a spurious space is tacked
296 onto the end of the args in some (at least one anyway)
297 implementations, so strip it off if it exists. */
300 char *command = elf_tdata (abfd)->core_command;
301 int n = strlen (command);
303 if (0 < n && command[n - 1] == ' ')
304 command[n - 1] = '\0';
307 return TRUE;
310 /* Functions for the x86-64 ELF linker. */
312 /* The name of the dynamic interpreter. This is put in the .interp
313 section. */
315 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
317 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
318 copying dynamic variables from a shared lib into an app's dynbss
319 section, and instead use a dynamic relocation to point into the
320 shared lib. */
321 #define ELIMINATE_COPY_RELOCS 1
323 /* The size in bytes of an entry in the global offset table. */
325 #define GOT_ENTRY_SIZE 8
327 /* The size in bytes of an entry in the procedure linkage table. */
329 #define PLT_ENTRY_SIZE 16
331 /* The first entry in a procedure linkage table looks like this. See the
332 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
334 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
336 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
337 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
338 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
341 /* Subsequent entries in a procedure linkage table look like this. */
343 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
345 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
346 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
347 0x68, /* pushq immediate */
348 0, 0, 0, 0, /* replaced with index into relocation table. */
349 0xe9, /* jmp relative */
350 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
353 /* The x86-64 linker needs to keep track of the number of relocs that
354 it decides to copy as dynamic relocs in check_relocs for each symbol.
355 This is so that it can later discard them if they are found to be
356 unnecessary. We store the information in a field extending the
357 regular ELF linker hash table. */
359 struct elf64_x86_64_dyn_relocs
361 /* Next section. */
362 struct elf64_x86_64_dyn_relocs *next;
364 /* The input section of the reloc. */
365 asection *sec;
367 /* Total number of relocs copied for the input section. */
368 bfd_size_type count;
370 /* Number of pc-relative relocs copied for the input section. */
371 bfd_size_type pc_count;
374 /* x86-64 ELF linker hash entry. */
376 struct elf64_x86_64_link_hash_entry
378 struct elf_link_hash_entry elf;
380 /* Track dynamic relocs copied for this symbol. */
381 struct elf64_x86_64_dyn_relocs *dyn_relocs;
383 #define GOT_UNKNOWN 0
384 #define GOT_NORMAL 1
385 #define GOT_TLS_GD 2
386 #define GOT_TLS_IE 3
387 #define GOT_TLS_GDESC 4
388 #define GOT_TLS_GD_BOTH_P(type) \
389 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
390 #define GOT_TLS_GD_P(type) \
391 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
392 #define GOT_TLS_GDESC_P(type) \
393 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
394 #define GOT_TLS_GD_ANY_P(type) \
395 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
396 unsigned char tls_type;
398 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
399 starting at the end of the jump table. */
400 bfd_vma tlsdesc_got;
403 #define elf64_x86_64_hash_entry(ent) \
404 ((struct elf64_x86_64_link_hash_entry *)(ent))
406 struct elf64_x86_64_obj_tdata
408 struct elf_obj_tdata root;
410 /* tls_type for each local got entry. */
411 char *local_got_tls_type;
413 /* GOTPLT entries for TLS descriptors. */
414 bfd_vma *local_tlsdesc_gotent;
417 #define elf64_x86_64_tdata(abfd) \
418 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
420 #define elf64_x86_64_local_got_tls_type(abfd) \
421 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
423 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
424 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
426 /* x86-64 ELF linker hash table. */
428 struct elf64_x86_64_link_hash_table
430 struct elf_link_hash_table elf;
432 /* Short-cuts to get to dynamic linker sections. */
433 asection *sgot;
434 asection *sgotplt;
435 asection *srelgot;
436 asection *splt;
437 asection *srelplt;
438 asection *sdynbss;
439 asection *srelbss;
441 /* The offset into splt of the PLT entry for the TLS descriptor
442 resolver. Special values are 0, if not necessary (or not found
443 to be necessary yet), and -1 if needed but not determined
444 yet. */
445 bfd_vma tlsdesc_plt;
446 /* The offset into sgot of the GOT entry used by the PLT entry
447 above. */
448 bfd_vma tlsdesc_got;
450 union {
451 bfd_signed_vma refcount;
452 bfd_vma offset;
453 } tls_ld_got;
455 /* The amount of space used by the jump slots in the GOT. */
456 bfd_vma sgotplt_jump_table_size;
458 /* Small local sym to section mapping cache. */
459 struct sym_sec_cache sym_sec;
462 /* Get the x86-64 ELF linker hash table from a link_info structure. */
464 #define elf64_x86_64_hash_table(p) \
465 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
467 #define elf64_x86_64_compute_jump_table_size(htab) \
468 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
470 /* Create an entry in an x86-64 ELF linker hash table. */
472 static struct bfd_hash_entry *
473 link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
474 const char *string)
476 /* Allocate the structure if it has not already been allocated by a
477 subclass. */
478 if (entry == NULL)
480 entry = bfd_hash_allocate (table,
481 sizeof (struct elf64_x86_64_link_hash_entry));
482 if (entry == NULL)
483 return entry;
486 /* Call the allocation method of the superclass. */
487 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
488 if (entry != NULL)
490 struct elf64_x86_64_link_hash_entry *eh;
492 eh = (struct elf64_x86_64_link_hash_entry *) entry;
493 eh->dyn_relocs = NULL;
494 eh->tls_type = GOT_UNKNOWN;
495 eh->tlsdesc_got = (bfd_vma) -1;
498 return entry;
501 /* Create an X86-64 ELF linker hash table. */
503 static struct bfd_link_hash_table *
504 elf64_x86_64_link_hash_table_create (bfd *abfd)
506 struct elf64_x86_64_link_hash_table *ret;
507 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
509 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
510 if (ret == NULL)
511 return NULL;
513 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
514 sizeof (struct elf64_x86_64_link_hash_entry)))
516 free (ret);
517 return NULL;
520 ret->sgot = NULL;
521 ret->sgotplt = NULL;
522 ret->srelgot = NULL;
523 ret->splt = NULL;
524 ret->srelplt = NULL;
525 ret->sdynbss = NULL;
526 ret->srelbss = NULL;
527 ret->sym_sec.abfd = NULL;
528 ret->tlsdesc_plt = 0;
529 ret->tlsdesc_got = 0;
530 ret->tls_ld_got.refcount = 0;
531 ret->sgotplt_jump_table_size = 0;
533 return &ret->elf.root;
536 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
537 shortcuts to them in our hash table. */
539 static bfd_boolean
540 create_got_section (bfd *dynobj, struct bfd_link_info *info)
542 struct elf64_x86_64_link_hash_table *htab;
544 if (! _bfd_elf_create_got_section (dynobj, info))
545 return FALSE;
547 htab = elf64_x86_64_hash_table (info);
548 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
549 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
550 if (!htab->sgot || !htab->sgotplt)
551 abort ();
553 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
554 (SEC_ALLOC | SEC_LOAD
555 | SEC_HAS_CONTENTS
556 | SEC_IN_MEMORY
557 | SEC_LINKER_CREATED
558 | SEC_READONLY));
559 if (htab->srelgot == NULL
560 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
561 return FALSE;
562 return TRUE;
565 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
566 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
567 hash table. */
569 static bfd_boolean
570 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
572 struct elf64_x86_64_link_hash_table *htab;
574 htab = elf64_x86_64_hash_table (info);
575 if (!htab->sgot && !create_got_section (dynobj, info))
576 return FALSE;
578 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
579 return FALSE;
581 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
582 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
583 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
584 if (!info->shared)
585 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
587 if (!htab->splt || !htab->srelplt || !htab->sdynbss
588 || (!info->shared && !htab->srelbss))
589 abort ();
591 return TRUE;
594 /* Copy the extra info we tack onto an elf_link_hash_entry. */
596 static void
597 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
598 struct elf_link_hash_entry *dir,
599 struct elf_link_hash_entry *ind)
601 struct elf64_x86_64_link_hash_entry *edir, *eind;
603 edir = (struct elf64_x86_64_link_hash_entry *) dir;
604 eind = (struct elf64_x86_64_link_hash_entry *) ind;
606 if (eind->dyn_relocs != NULL)
608 if (edir->dyn_relocs != NULL)
610 struct elf64_x86_64_dyn_relocs **pp;
611 struct elf64_x86_64_dyn_relocs *p;
613 /* Add reloc counts against the indirect sym to the direct sym
614 list. Merge any entries against the same section. */
615 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
617 struct elf64_x86_64_dyn_relocs *q;
619 for (q = edir->dyn_relocs; q != NULL; q = q->next)
620 if (q->sec == p->sec)
622 q->pc_count += p->pc_count;
623 q->count += p->count;
624 *pp = p->next;
625 break;
627 if (q == NULL)
628 pp = &p->next;
630 *pp = edir->dyn_relocs;
633 edir->dyn_relocs = eind->dyn_relocs;
634 eind->dyn_relocs = NULL;
637 if (ind->root.type == bfd_link_hash_indirect
638 && dir->got.refcount <= 0)
640 edir->tls_type = eind->tls_type;
641 eind->tls_type = GOT_UNKNOWN;
644 if (ELIMINATE_COPY_RELOCS
645 && ind->root.type != bfd_link_hash_indirect
646 && dir->dynamic_adjusted)
648 /* If called to transfer flags for a weakdef during processing
649 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
650 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
651 dir->ref_dynamic |= ind->ref_dynamic;
652 dir->ref_regular |= ind->ref_regular;
653 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
654 dir->needs_plt |= ind->needs_plt;
655 dir->pointer_equality_needed |= ind->pointer_equality_needed;
657 else
658 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
661 static bfd_boolean
662 elf64_x86_64_mkobject (bfd *abfd)
664 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
665 abfd->tdata.any = bfd_zalloc (abfd, amt);
666 if (abfd->tdata.any == NULL)
667 return FALSE;
668 return TRUE;
671 static bfd_boolean
672 elf64_x86_64_elf_object_p (bfd *abfd)
674 /* Set the right machine number for an x86-64 elf64 file. */
675 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
676 return TRUE;
679 static int
680 elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
682 if (info->shared)
683 return r_type;
685 switch (r_type)
687 case R_X86_64_TLSGD:
688 case R_X86_64_GOTPC32_TLSDESC:
689 case R_X86_64_TLSDESC_CALL:
690 case R_X86_64_GOTTPOFF:
691 if (is_local)
692 return R_X86_64_TPOFF32;
693 return R_X86_64_GOTTPOFF;
694 case R_X86_64_TLSLD:
695 return R_X86_64_TPOFF32;
698 return r_type;
701 /* Look through the relocs for a section during the first phase, and
702 calculate needed space in the global offset table, procedure
703 linkage table, and dynamic reloc sections. */
705 static bfd_boolean
706 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
707 const Elf_Internal_Rela *relocs)
709 struct elf64_x86_64_link_hash_table *htab;
710 Elf_Internal_Shdr *symtab_hdr;
711 struct elf_link_hash_entry **sym_hashes;
712 const Elf_Internal_Rela *rel;
713 const Elf_Internal_Rela *rel_end;
714 asection *sreloc;
716 if (info->relocatable)
717 return TRUE;
719 htab = elf64_x86_64_hash_table (info);
720 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
721 sym_hashes = elf_sym_hashes (abfd);
723 sreloc = NULL;
725 rel_end = relocs + sec->reloc_count;
726 for (rel = relocs; rel < rel_end; rel++)
728 unsigned int r_type;
729 unsigned long r_symndx;
730 struct elf_link_hash_entry *h;
732 r_symndx = ELF64_R_SYM (rel->r_info);
733 r_type = ELF64_R_TYPE (rel->r_info);
735 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
737 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
738 abfd, r_symndx);
739 return FALSE;
742 if (r_symndx < symtab_hdr->sh_info)
743 h = NULL;
744 else
746 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
747 while (h->root.type == bfd_link_hash_indirect
748 || h->root.type == bfd_link_hash_warning)
749 h = (struct elf_link_hash_entry *) h->root.u.i.link;
752 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
753 switch (r_type)
755 case R_X86_64_TLSLD:
756 htab->tls_ld_got.refcount += 1;
757 goto create_got;
759 case R_X86_64_TPOFF32:
760 if (info->shared)
762 (*_bfd_error_handler)
763 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
764 abfd,
765 x86_64_elf_howto_table[r_type].name,
766 (h) ? h->root.root.string : "a local symbol");
767 bfd_set_error (bfd_error_bad_value);
768 return FALSE;
770 break;
772 case R_X86_64_GOTTPOFF:
773 if (info->shared)
774 info->flags |= DF_STATIC_TLS;
775 /* Fall through */
777 case R_X86_64_GOT32:
778 case R_X86_64_GOTPCREL:
779 case R_X86_64_TLSGD:
780 case R_X86_64_GOTPC32_TLSDESC:
781 case R_X86_64_TLSDESC_CALL:
782 /* This symbol requires a global offset table entry. */
784 int tls_type, old_tls_type;
786 switch (r_type)
788 default: tls_type = GOT_NORMAL; break;
789 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
790 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
791 case R_X86_64_GOTPC32_TLSDESC:
792 case R_X86_64_TLSDESC_CALL:
793 tls_type = GOT_TLS_GDESC; break;
796 if (h != NULL)
798 h->got.refcount += 1;
799 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
801 else
803 bfd_signed_vma *local_got_refcounts;
805 /* This is a global offset table entry for a local symbol. */
806 local_got_refcounts = elf_local_got_refcounts (abfd);
807 if (local_got_refcounts == NULL)
809 bfd_size_type size;
811 size = symtab_hdr->sh_info;
812 size *= sizeof (bfd_signed_vma)
813 + sizeof (bfd_vma) + sizeof (char);
814 local_got_refcounts = ((bfd_signed_vma *)
815 bfd_zalloc (abfd, size));
816 if (local_got_refcounts == NULL)
817 return FALSE;
818 elf_local_got_refcounts (abfd) = local_got_refcounts;
819 elf64_x86_64_local_tlsdesc_gotent (abfd)
820 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
821 elf64_x86_64_local_got_tls_type (abfd)
822 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
824 local_got_refcounts[r_symndx] += 1;
825 old_tls_type
826 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
829 /* If a TLS symbol is accessed using IE at least once,
830 there is no point to use dynamic model for it. */
831 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
832 && (! GOT_TLS_GD_ANY_P (old_tls_type)
833 || tls_type != GOT_TLS_IE))
835 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
836 tls_type = old_tls_type;
837 else if (GOT_TLS_GD_ANY_P (old_tls_type)
838 && GOT_TLS_GD_ANY_P (tls_type))
839 tls_type |= old_tls_type;
840 else
842 (*_bfd_error_handler)
843 (_("%B: %s' accessed both as normal and thread local symbol"),
844 abfd, h ? h->root.root.string : "<local>");
845 return FALSE;
849 if (old_tls_type != tls_type)
851 if (h != NULL)
852 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
853 else
854 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
857 /* Fall through */
859 case R_X86_64_GOTOFF64:
860 case R_X86_64_GOTPC32:
861 create_got:
862 if (htab->sgot == NULL)
864 if (htab->elf.dynobj == NULL)
865 htab->elf.dynobj = abfd;
866 if (!create_got_section (htab->elf.dynobj, info))
867 return FALSE;
869 break;
871 case R_X86_64_PLT32:
872 /* This symbol requires a procedure linkage table entry. We
873 actually build the entry in adjust_dynamic_symbol,
874 because this might be a case of linking PIC code which is
875 never referenced by a dynamic object, in which case we
876 don't need to generate a procedure linkage table entry
877 after all. */
879 /* If this is a local symbol, we resolve it directly without
880 creating a procedure linkage table entry. */
881 if (h == NULL)
882 continue;
884 h->needs_plt = 1;
885 h->plt.refcount += 1;
886 break;
888 case R_X86_64_8:
889 case R_X86_64_16:
890 case R_X86_64_32:
891 case R_X86_64_32S:
892 /* Let's help debug shared library creation. These relocs
893 cannot be used in shared libs. Don't error out for
894 sections we don't care about, such as debug sections or
895 non-constant sections. */
896 if (info->shared
897 && (sec->flags & SEC_ALLOC) != 0
898 && (sec->flags & SEC_READONLY) != 0)
900 (*_bfd_error_handler)
901 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
902 abfd,
903 x86_64_elf_howto_table[r_type].name,
904 (h) ? h->root.root.string : "a local symbol");
905 bfd_set_error (bfd_error_bad_value);
906 return FALSE;
908 /* Fall through. */
910 case R_X86_64_PC8:
911 case R_X86_64_PC16:
912 case R_X86_64_PC32:
913 case R_X86_64_PC64:
914 case R_X86_64_64:
915 if (h != NULL && !info->shared)
917 /* If this reloc is in a read-only section, we might
918 need a copy reloc. We can't check reliably at this
919 stage whether the section is read-only, as input
920 sections have not yet been mapped to output sections.
921 Tentatively set the flag for now, and correct in
922 adjust_dynamic_symbol. */
923 h->non_got_ref = 1;
925 /* We may need a .plt entry if the function this reloc
926 refers to is in a shared lib. */
927 h->plt.refcount += 1;
928 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
929 h->pointer_equality_needed = 1;
932 /* If we are creating a shared library, and this is a reloc
933 against a global symbol, or a non PC relative reloc
934 against a local symbol, then we need to copy the reloc
935 into the shared library. However, if we are linking with
936 -Bsymbolic, we do not need to copy a reloc against a
937 global symbol which is defined in an object we are
938 including in the link (i.e., DEF_REGULAR is set). At
939 this point we have not seen all the input files, so it is
940 possible that DEF_REGULAR is not set now but will be set
941 later (it is never cleared). In case of a weak definition,
942 DEF_REGULAR may be cleared later by a strong definition in
943 a shared library. We account for that possibility below by
944 storing information in the relocs_copied field of the hash
945 table entry. A similar situation occurs when creating
946 shared libraries and symbol visibility changes render the
947 symbol local.
949 If on the other hand, we are creating an executable, we
950 may need to keep relocations for symbols satisfied by a
951 dynamic library if we manage to avoid copy relocs for the
952 symbol. */
953 if ((info->shared
954 && (sec->flags & SEC_ALLOC) != 0
955 && (((r_type != R_X86_64_PC8)
956 && (r_type != R_X86_64_PC16)
957 && (r_type != R_X86_64_PC32)
958 && (r_type != R_X86_64_PC64))
959 || (h != NULL
960 && (! info->symbolic
961 || h->root.type == bfd_link_hash_defweak
962 || !h->def_regular))))
963 || (ELIMINATE_COPY_RELOCS
964 && !info->shared
965 && (sec->flags & SEC_ALLOC) != 0
966 && h != NULL
967 && (h->root.type == bfd_link_hash_defweak
968 || !h->def_regular)))
970 struct elf64_x86_64_dyn_relocs *p;
971 struct elf64_x86_64_dyn_relocs **head;
973 /* We must copy these reloc types into the output file.
974 Create a reloc section in dynobj and make room for
975 this reloc. */
976 if (sreloc == NULL)
978 const char *name;
979 bfd *dynobj;
981 name = (bfd_elf_string_from_elf_section
982 (abfd,
983 elf_elfheader (abfd)->e_shstrndx,
984 elf_section_data (sec)->rel_hdr.sh_name));
985 if (name == NULL)
986 return FALSE;
988 if (strncmp (name, ".rela", 5) != 0
989 || strcmp (bfd_get_section_name (abfd, sec),
990 name + 5) != 0)
992 (*_bfd_error_handler)
993 (_("%B: bad relocation section name `%s\'"),
994 abfd, name);
997 if (htab->elf.dynobj == NULL)
998 htab->elf.dynobj = abfd;
1000 dynobj = htab->elf.dynobj;
1002 sreloc = bfd_get_section_by_name (dynobj, name);
1003 if (sreloc == NULL)
1005 flagword flags;
1007 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1008 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1009 if ((sec->flags & SEC_ALLOC) != 0)
1010 flags |= SEC_ALLOC | SEC_LOAD;
1011 sreloc = bfd_make_section_with_flags (dynobj,
1012 name,
1013 flags);
1014 if (sreloc == NULL
1015 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
1016 return FALSE;
1018 elf_section_data (sec)->sreloc = sreloc;
1021 /* If this is a global symbol, we count the number of
1022 relocations we need for this symbol. */
1023 if (h != NULL)
1025 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1027 else
1029 void **vpp;
1030 /* Track dynamic relocs needed for local syms too.
1031 We really need local syms available to do this
1032 easily. Oh well. */
1034 asection *s;
1035 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1036 sec, r_symndx);
1037 if (s == NULL)
1038 return FALSE;
1040 /* Beware of type punned pointers vs strict aliasing
1041 rules. */
1042 vpp = &(elf_section_data (s)->local_dynrel);
1043 head = (struct elf64_x86_64_dyn_relocs **)vpp;
1046 p = *head;
1047 if (p == NULL || p->sec != sec)
1049 bfd_size_type amt = sizeof *p;
1050 p = ((struct elf64_x86_64_dyn_relocs *)
1051 bfd_alloc (htab->elf.dynobj, amt));
1052 if (p == NULL)
1053 return FALSE;
1054 p->next = *head;
1055 *head = p;
1056 p->sec = sec;
1057 p->count = 0;
1058 p->pc_count = 0;
1061 p->count += 1;
1062 if (r_type == R_X86_64_PC8
1063 || r_type == R_X86_64_PC16
1064 || r_type == R_X86_64_PC32
1065 || r_type == R_X86_64_PC64)
1066 p->pc_count += 1;
1068 break;
1070 /* This relocation describes the C++ object vtable hierarchy.
1071 Reconstruct it for later use during GC. */
1072 case R_X86_64_GNU_VTINHERIT:
1073 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1074 return FALSE;
1075 break;
1077 /* This relocation describes which C++ vtable entries are actually
1078 used. Record for later use during GC. */
1079 case R_X86_64_GNU_VTENTRY:
1080 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1081 return FALSE;
1082 break;
1084 default:
1085 break;
1089 return TRUE;
1092 /* Return the section that should be marked against GC for a given
1093 relocation. */
1095 static asection *
1096 elf64_x86_64_gc_mark_hook (asection *sec,
1097 struct bfd_link_info *info ATTRIBUTE_UNUSED,
1098 Elf_Internal_Rela *rel,
1099 struct elf_link_hash_entry *h,
1100 Elf_Internal_Sym *sym)
1102 if (h != NULL)
1104 switch (ELF64_R_TYPE (rel->r_info))
1106 case R_X86_64_GNU_VTINHERIT:
1107 case R_X86_64_GNU_VTENTRY:
1108 break;
1110 default:
1111 switch (h->root.type)
1113 case bfd_link_hash_defined:
1114 case bfd_link_hash_defweak:
1115 return h->root.u.def.section;
1117 case bfd_link_hash_common:
1118 return h->root.u.c.p->section;
1120 default:
1121 break;
1125 else
1126 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1128 return NULL;
1131 /* Update the got entry reference counts for the section being removed. */
1133 static bfd_boolean
1134 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1135 asection *sec, const Elf_Internal_Rela *relocs)
1137 Elf_Internal_Shdr *symtab_hdr;
1138 struct elf_link_hash_entry **sym_hashes;
1139 bfd_signed_vma *local_got_refcounts;
1140 const Elf_Internal_Rela *rel, *relend;
1142 elf_section_data (sec)->local_dynrel = NULL;
1144 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1145 sym_hashes = elf_sym_hashes (abfd);
1146 local_got_refcounts = elf_local_got_refcounts (abfd);
1148 relend = relocs + sec->reloc_count;
1149 for (rel = relocs; rel < relend; rel++)
1151 unsigned long r_symndx;
1152 unsigned int r_type;
1153 struct elf_link_hash_entry *h = NULL;
1155 r_symndx = ELF64_R_SYM (rel->r_info);
1156 if (r_symndx >= symtab_hdr->sh_info)
1158 struct elf64_x86_64_link_hash_entry *eh;
1159 struct elf64_x86_64_dyn_relocs **pp;
1160 struct elf64_x86_64_dyn_relocs *p;
1162 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1163 while (h->root.type == bfd_link_hash_indirect
1164 || h->root.type == bfd_link_hash_warning)
1165 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1166 eh = (struct elf64_x86_64_link_hash_entry *) h;
1168 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1169 if (p->sec == sec)
1171 /* Everything must go for SEC. */
1172 *pp = p->next;
1173 break;
1177 r_type = ELF64_R_TYPE (rel->r_info);
1178 r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
1179 switch (r_type)
1181 case R_X86_64_TLSLD:
1182 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1183 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1184 break;
1186 case R_X86_64_TLSGD:
1187 case R_X86_64_GOTPC32_TLSDESC:
1188 case R_X86_64_TLSDESC_CALL:
1189 case R_X86_64_GOTTPOFF:
1190 case R_X86_64_GOT32:
1191 case R_X86_64_GOTPCREL:
1192 if (h != NULL)
1194 if (h->got.refcount > 0)
1195 h->got.refcount -= 1;
1197 else if (local_got_refcounts != NULL)
1199 if (local_got_refcounts[r_symndx] > 0)
1200 local_got_refcounts[r_symndx] -= 1;
1202 break;
1204 case R_X86_64_8:
1205 case R_X86_64_16:
1206 case R_X86_64_32:
1207 case R_X86_64_64:
1208 case R_X86_64_32S:
1209 case R_X86_64_PC8:
1210 case R_X86_64_PC16:
1211 case R_X86_64_PC32:
1212 case R_X86_64_PC64:
1213 if (info->shared)
1214 break;
1215 /* Fall thru */
1217 case R_X86_64_PLT32:
1218 if (h != NULL)
1220 if (h->plt.refcount > 0)
1221 h->plt.refcount -= 1;
1223 break;
1225 default:
1226 break;
1230 return TRUE;
1233 /* Adjust a symbol defined by a dynamic object and referenced by a
1234 regular object. The current definition is in some section of the
1235 dynamic object, but we're not including those sections. We have to
1236 change the definition to something the rest of the link can
1237 understand. */
1239 static bfd_boolean
1240 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1241 struct elf_link_hash_entry *h)
1243 struct elf64_x86_64_link_hash_table *htab;
1244 asection *s;
1245 unsigned int power_of_two;
1247 /* If this is a function, put it in the procedure linkage table. We
1248 will fill in the contents of the procedure linkage table later,
1249 when we know the address of the .got section. */
1250 if (h->type == STT_FUNC
1251 || h->needs_plt)
1253 if (h->plt.refcount <= 0
1254 || SYMBOL_CALLS_LOCAL (info, h)
1255 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1256 && h->root.type == bfd_link_hash_undefweak))
1258 /* This case can occur if we saw a PLT32 reloc in an input
1259 file, but the symbol was never referred to by a dynamic
1260 object, or if all references were garbage collected. In
1261 such a case, we don't actually need to build a procedure
1262 linkage table, and we can just do a PC32 reloc instead. */
1263 h->plt.offset = (bfd_vma) -1;
1264 h->needs_plt = 0;
1267 return TRUE;
1269 else
1270 /* It's possible that we incorrectly decided a .plt reloc was
1271 needed for an R_X86_64_PC32 reloc to a non-function sym in
1272 check_relocs. We can't decide accurately between function and
1273 non-function syms in check-relocs; Objects loaded later in
1274 the link may change h->type. So fix it now. */
1275 h->plt.offset = (bfd_vma) -1;
1277 /* If this is a weak symbol, and there is a real definition, the
1278 processor independent code will have arranged for us to see the
1279 real definition first, and we can just use the same value. */
1280 if (h->u.weakdef != NULL)
1282 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1283 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1284 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1285 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1286 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1287 h->non_got_ref = h->u.weakdef->non_got_ref;
1288 return TRUE;
1291 /* This is a reference to a symbol defined by a dynamic object which
1292 is not a function. */
1294 /* If we are creating a shared library, we must presume that the
1295 only references to the symbol are via the global offset table.
1296 For such cases we need not do anything here; the relocations will
1297 be handled correctly by relocate_section. */
1298 if (info->shared)
1299 return TRUE;
1301 /* If there are no references to this symbol that do not use the
1302 GOT, we don't need to generate a copy reloc. */
1303 if (!h->non_got_ref)
1304 return TRUE;
1306 /* If -z nocopyreloc was given, we won't generate them either. */
1307 if (info->nocopyreloc)
1309 h->non_got_ref = 0;
1310 return TRUE;
1313 if (ELIMINATE_COPY_RELOCS)
1315 struct elf64_x86_64_link_hash_entry * eh;
1316 struct elf64_x86_64_dyn_relocs *p;
1318 eh = (struct elf64_x86_64_link_hash_entry *) h;
1319 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1321 s = p->sec->output_section;
1322 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1323 break;
1326 /* If we didn't find any dynamic relocs in read-only sections, then
1327 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1328 if (p == NULL)
1330 h->non_got_ref = 0;
1331 return TRUE;
1335 if (h->size == 0)
1337 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1338 h->root.root.string);
1339 return TRUE;
1342 /* We must allocate the symbol in our .dynbss section, which will
1343 become part of the .bss section of the executable. There will be
1344 an entry for this symbol in the .dynsym section. The dynamic
1345 object will contain position independent code, so all references
1346 from the dynamic object to this symbol will go through the global
1347 offset table. The dynamic linker will use the .dynsym entry to
1348 determine the address it must put in the global offset table, so
1349 both the dynamic object and the regular object will refer to the
1350 same memory location for the variable. */
1352 htab = elf64_x86_64_hash_table (info);
1354 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1355 to copy the initial value out of the dynamic object and into the
1356 runtime process image. */
1357 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1359 htab->srelbss->size += sizeof (Elf64_External_Rela);
1360 h->needs_copy = 1;
1363 /* We need to figure out the alignment required for this symbol. I
1364 have no idea how ELF linkers handle this. 16-bytes is the size
1365 of the largest type that requires hard alignment -- long double. */
1366 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1367 this construct. */
1368 power_of_two = bfd_log2 (h->size);
1369 if (power_of_two > 4)
1370 power_of_two = 4;
1372 /* Apply the required alignment. */
1373 s = htab->sdynbss;
1374 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1375 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1377 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1378 return FALSE;
1381 /* Define the symbol as being at this point in the section. */
1382 h->root.u.def.section = s;
1383 h->root.u.def.value = s->size;
1385 /* Increment the section size to make room for the symbol. */
1386 s->size += h->size;
1388 return TRUE;
1391 /* Allocate space in .plt, .got and associated reloc sections for
1392 dynamic relocs. */
1394 static bfd_boolean
1395 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1397 struct bfd_link_info *info;
1398 struct elf64_x86_64_link_hash_table *htab;
1399 struct elf64_x86_64_link_hash_entry *eh;
1400 struct elf64_x86_64_dyn_relocs *p;
1402 if (h->root.type == bfd_link_hash_indirect)
1403 return TRUE;
1405 if (h->root.type == bfd_link_hash_warning)
1406 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1408 info = (struct bfd_link_info *) inf;
1409 htab = elf64_x86_64_hash_table (info);
1411 if (htab->elf.dynamic_sections_created
1412 && h->plt.refcount > 0)
1414 /* Make sure this symbol is output as a dynamic symbol.
1415 Undefined weak syms won't yet be marked as dynamic. */
1416 if (h->dynindx == -1
1417 && !h->forced_local)
1419 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1420 return FALSE;
1423 if (info->shared
1424 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1426 asection *s = htab->splt;
1428 /* If this is the first .plt entry, make room for the special
1429 first entry. */
1430 if (s->size == 0)
1431 s->size += PLT_ENTRY_SIZE;
1433 h->plt.offset = s->size;
1435 /* If this symbol is not defined in a regular file, and we are
1436 not generating a shared library, then set the symbol to this
1437 location in the .plt. This is required to make function
1438 pointers compare as equal between the normal executable and
1439 the shared library. */
1440 if (! info->shared
1441 && !h->def_regular)
1443 h->root.u.def.section = s;
1444 h->root.u.def.value = h->plt.offset;
1447 /* Make room for this entry. */
1448 s->size += PLT_ENTRY_SIZE;
1450 /* We also need to make an entry in the .got.plt section, which
1451 will be placed in the .got section by the linker script. */
1452 htab->sgotplt->size += GOT_ENTRY_SIZE;
1454 /* We also need to make an entry in the .rela.plt section. */
1455 htab->srelplt->size += sizeof (Elf64_External_Rela);
1456 htab->srelplt->reloc_count++;
1458 else
1460 h->plt.offset = (bfd_vma) -1;
1461 h->needs_plt = 0;
1464 else
1466 h->plt.offset = (bfd_vma) -1;
1467 h->needs_plt = 0;
1470 eh = (struct elf64_x86_64_link_hash_entry *) h;
1471 eh->tlsdesc_got = (bfd_vma) -1;
1473 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1474 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1475 if (h->got.refcount > 0
1476 && !info->shared
1477 && h->dynindx == -1
1478 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1479 h->got.offset = (bfd_vma) -1;
1480 else if (h->got.refcount > 0)
1482 asection *s;
1483 bfd_boolean dyn;
1484 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1486 /* Make sure this symbol is output as a dynamic symbol.
1487 Undefined weak syms won't yet be marked as dynamic. */
1488 if (h->dynindx == -1
1489 && !h->forced_local)
1491 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1492 return FALSE;
1495 if (GOT_TLS_GDESC_P (tls_type))
1497 eh->tlsdesc_got = htab->sgotplt->size
1498 - elf64_x86_64_compute_jump_table_size (htab);
1499 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1500 h->got.offset = (bfd_vma) -2;
1502 if (! GOT_TLS_GDESC_P (tls_type)
1503 || GOT_TLS_GD_P (tls_type))
1505 s = htab->sgot;
1506 h->got.offset = s->size;
1507 s->size += GOT_ENTRY_SIZE;
1508 if (GOT_TLS_GD_P (tls_type))
1509 s->size += GOT_ENTRY_SIZE;
1511 dyn = htab->elf.dynamic_sections_created;
1512 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1513 and two if global.
1514 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1515 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1516 || tls_type == GOT_TLS_IE)
1517 htab->srelgot->size += sizeof (Elf64_External_Rela);
1518 else if (GOT_TLS_GD_P (tls_type))
1519 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
1520 else if (! GOT_TLS_GDESC_P (tls_type)
1521 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1522 || h->root.type != bfd_link_hash_undefweak)
1523 && (info->shared
1524 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1525 htab->srelgot->size += sizeof (Elf64_External_Rela);
1526 if (GOT_TLS_GDESC_P (tls_type))
1528 htab->srelplt->size += sizeof (Elf64_External_Rela);
1529 htab->tlsdesc_plt = (bfd_vma) -1;
1532 else
1533 h->got.offset = (bfd_vma) -1;
1535 if (eh->dyn_relocs == NULL)
1536 return TRUE;
1538 /* In the shared -Bsymbolic case, discard space allocated for
1539 dynamic pc-relative relocs against symbols which turn out to be
1540 defined in regular objects. For the normal shared case, discard
1541 space for pc-relative relocs that have become local due to symbol
1542 visibility changes. */
1544 if (info->shared)
1546 /* Relocs that use pc_count are those that appear on a call
1547 insn, or certain REL relocs that can generated via assembly.
1548 We want calls to protected symbols to resolve directly to the
1549 function rather than going via the plt. If people want
1550 function pointer comparisons to work as expected then they
1551 should avoid writing weird assembly. */
1552 if (SYMBOL_CALLS_LOCAL (info, h))
1554 struct elf64_x86_64_dyn_relocs **pp;
1556 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1558 p->count -= p->pc_count;
1559 p->pc_count = 0;
1560 if (p->count == 0)
1561 *pp = p->next;
1562 else
1563 pp = &p->next;
1567 /* Also discard relocs on undefined weak syms with non-default
1568 visibility. */
1569 if (eh->dyn_relocs != NULL
1570 && h->root.type == bfd_link_hash_undefweak)
1572 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1573 eh->dyn_relocs = NULL;
1575 /* Make sure undefined weak symbols are output as a dynamic
1576 symbol in PIEs. */
1577 else if (h->dynindx == -1
1578 && !h->forced_local)
1580 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1581 return FALSE;
1585 else if (ELIMINATE_COPY_RELOCS)
1587 /* For the non-shared case, discard space for relocs against
1588 symbols which turn out to need copy relocs or are not
1589 dynamic. */
1591 if (!h->non_got_ref
1592 && ((h->def_dynamic
1593 && !h->def_regular)
1594 || (htab->elf.dynamic_sections_created
1595 && (h->root.type == bfd_link_hash_undefweak
1596 || h->root.type == bfd_link_hash_undefined))))
1598 /* Make sure this symbol is output as a dynamic symbol.
1599 Undefined weak syms won't yet be marked as dynamic. */
1600 if (h->dynindx == -1
1601 && !h->forced_local)
1603 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1604 return FALSE;
1607 /* If that succeeded, we know we'll be keeping all the
1608 relocs. */
1609 if (h->dynindx != -1)
1610 goto keep;
1613 eh->dyn_relocs = NULL;
1615 keep: ;
1618 /* Finally, allocate space. */
1619 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1621 asection *sreloc = elf_section_data (p->sec)->sreloc;
1622 sreloc->size += p->count * sizeof (Elf64_External_Rela);
1625 return TRUE;
1628 /* Find any dynamic relocs that apply to read-only sections. */
1630 static bfd_boolean
1631 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1633 struct elf64_x86_64_link_hash_entry *eh;
1634 struct elf64_x86_64_dyn_relocs *p;
1636 if (h->root.type == bfd_link_hash_warning)
1637 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1639 eh = (struct elf64_x86_64_link_hash_entry *) h;
1640 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1642 asection *s = p->sec->output_section;
1644 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1646 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1648 info->flags |= DF_TEXTREL;
1650 /* Not an error, just cut short the traversal. */
1651 return FALSE;
1654 return TRUE;
1657 /* Set the sizes of the dynamic sections. */
1659 static bfd_boolean
1660 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1661 struct bfd_link_info *info)
1663 struct elf64_x86_64_link_hash_table *htab;
1664 bfd *dynobj;
1665 asection *s;
1666 bfd_boolean relocs;
1667 bfd *ibfd;
1669 htab = elf64_x86_64_hash_table (info);
1670 dynobj = htab->elf.dynobj;
1671 if (dynobj == NULL)
1672 abort ();
1674 if (htab->elf.dynamic_sections_created)
1676 /* Set the contents of the .interp section to the interpreter. */
1677 if (info->executable)
1679 s = bfd_get_section_by_name (dynobj, ".interp");
1680 if (s == NULL)
1681 abort ();
1682 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1683 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1687 /* Set up .got offsets for local syms, and space for local dynamic
1688 relocs. */
1689 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1691 bfd_signed_vma *local_got;
1692 bfd_signed_vma *end_local_got;
1693 char *local_tls_type;
1694 bfd_vma *local_tlsdesc_gotent;
1695 bfd_size_type locsymcount;
1696 Elf_Internal_Shdr *symtab_hdr;
1697 asection *srel;
1699 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1700 continue;
1702 for (s = ibfd->sections; s != NULL; s = s->next)
1704 struct elf64_x86_64_dyn_relocs *p;
1706 for (p = (struct elf64_x86_64_dyn_relocs *)
1707 (elf_section_data (s)->local_dynrel);
1708 p != NULL;
1709 p = p->next)
1711 if (!bfd_is_abs_section (p->sec)
1712 && bfd_is_abs_section (p->sec->output_section))
1714 /* Input section has been discarded, either because
1715 it is a copy of a linkonce section or due to
1716 linker script /DISCARD/, so we'll be discarding
1717 the relocs too. */
1719 else if (p->count != 0)
1721 srel = elf_section_data (p->sec)->sreloc;
1722 srel->size += p->count * sizeof (Elf64_External_Rela);
1723 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1724 info->flags |= DF_TEXTREL;
1730 local_got = elf_local_got_refcounts (ibfd);
1731 if (!local_got)
1732 continue;
1734 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1735 locsymcount = symtab_hdr->sh_info;
1736 end_local_got = local_got + locsymcount;
1737 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
1738 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
1739 s = htab->sgot;
1740 srel = htab->srelgot;
1741 for (; local_got < end_local_got;
1742 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
1744 *local_tlsdesc_gotent = (bfd_vma) -1;
1745 if (*local_got > 0)
1747 if (GOT_TLS_GDESC_P (*local_tls_type))
1749 *local_tlsdesc_gotent = htab->sgotplt->size
1750 - elf64_x86_64_compute_jump_table_size (htab);
1751 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1752 *local_got = (bfd_vma) -2;
1754 if (! GOT_TLS_GDESC_P (*local_tls_type)
1755 || GOT_TLS_GD_P (*local_tls_type))
1757 *local_got = s->size;
1758 s->size += GOT_ENTRY_SIZE;
1759 if (GOT_TLS_GD_P (*local_tls_type))
1760 s->size += GOT_ENTRY_SIZE;
1762 if (info->shared
1763 || GOT_TLS_GD_ANY_P (*local_tls_type)
1764 || *local_tls_type == GOT_TLS_IE)
1766 if (GOT_TLS_GDESC_P (*local_tls_type))
1768 htab->srelplt->size += sizeof (Elf64_External_Rela);
1769 htab->tlsdesc_plt = (bfd_vma) -1;
1771 if (! GOT_TLS_GDESC_P (*local_tls_type)
1772 || GOT_TLS_GD_P (*local_tls_type))
1773 srel->size += sizeof (Elf64_External_Rela);
1776 else
1777 *local_got = (bfd_vma) -1;
1781 if (htab->tls_ld_got.refcount > 0)
1783 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1784 relocs. */
1785 htab->tls_ld_got.offset = htab->sgot->size;
1786 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
1787 htab->srelgot->size += sizeof (Elf64_External_Rela);
1789 else
1790 htab->tls_ld_got.offset = -1;
1792 /* Allocate global sym .plt and .got entries, and space for global
1793 sym dynamic relocs. */
1794 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1796 /* For every jump slot reserved in the sgotplt, reloc_count is
1797 incremented. However, when we reserve space for TLS descriptors,
1798 it's not incremented, so in order to compute the space reserved
1799 for them, it suffices to multiply the reloc count by the jump
1800 slot size. */
1801 if (htab->srelplt)
1802 htab->sgotplt_jump_table_size
1803 = elf64_x86_64_compute_jump_table_size (htab);
1805 if (htab->tlsdesc_plt)
1807 /* If we're not using lazy TLS relocations, don't generate the
1808 PLT and GOT entries they require. */
1809 if ((info->flags & DF_BIND_NOW))
1810 htab->tlsdesc_plt = 0;
1811 else
1813 htab->tlsdesc_got = htab->sgot->size;
1814 htab->sgot->size += GOT_ENTRY_SIZE;
1815 /* Reserve room for the initial entry.
1816 FIXME: we could probably do away with it in this case. */
1817 if (htab->splt->size == 0)
1818 htab->splt->size += PLT_ENTRY_SIZE;
1819 htab->tlsdesc_plt = htab->splt->size;
1820 htab->splt->size += PLT_ENTRY_SIZE;
1824 /* We now have determined the sizes of the various dynamic sections.
1825 Allocate memory for them. */
1826 relocs = FALSE;
1827 for (s = dynobj->sections; s != NULL; s = s->next)
1829 if ((s->flags & SEC_LINKER_CREATED) == 0)
1830 continue;
1832 if (s == htab->splt
1833 || s == htab->sgot
1834 || s == htab->sgotplt
1835 || s == htab->sdynbss)
1837 /* Strip this section if we don't need it; see the
1838 comment below. */
1840 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1842 if (s->size != 0 && s != htab->srelplt)
1843 relocs = TRUE;
1845 /* We use the reloc_count field as a counter if we need
1846 to copy relocs into the output file. */
1847 if (s != htab->srelplt)
1848 s->reloc_count = 0;
1850 else
1852 /* It's not one of our sections, so don't allocate space. */
1853 continue;
1856 if (s->size == 0)
1858 /* If we don't need this section, strip it from the
1859 output file. This is mostly to handle .rela.bss and
1860 .rela.plt. We must create both sections in
1861 create_dynamic_sections, because they must be created
1862 before the linker maps input sections to output
1863 sections. The linker does that before
1864 adjust_dynamic_symbol is called, and it is that
1865 function which decides whether anything needs to go
1866 into these sections. */
1868 s->flags |= SEC_EXCLUDE;
1869 continue;
1872 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1873 continue;
1875 /* Allocate memory for the section contents. We use bfd_zalloc
1876 here in case unused entries are not reclaimed before the
1877 section's contents are written out. This should not happen,
1878 but this way if it does, we get a R_X86_64_NONE reloc instead
1879 of garbage. */
1880 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1881 if (s->contents == NULL)
1882 return FALSE;
1885 if (htab->elf.dynamic_sections_created)
1887 /* Add some entries to the .dynamic section. We fill in the
1888 values later, in elf64_x86_64_finish_dynamic_sections, but we
1889 must add the entries now so that we get the correct size for
1890 the .dynamic section. The DT_DEBUG entry is filled in by the
1891 dynamic linker and used by the debugger. */
1892 #define add_dynamic_entry(TAG, VAL) \
1893 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1895 if (info->executable)
1897 if (!add_dynamic_entry (DT_DEBUG, 0))
1898 return FALSE;
1901 if (htab->splt->size != 0)
1903 if (!add_dynamic_entry (DT_PLTGOT, 0)
1904 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1905 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1906 || !add_dynamic_entry (DT_JMPREL, 0))
1907 return FALSE;
1909 if (htab->tlsdesc_plt
1910 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
1911 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
1912 return FALSE;
1915 if (relocs)
1917 if (!add_dynamic_entry (DT_RELA, 0)
1918 || !add_dynamic_entry (DT_RELASZ, 0)
1919 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1920 return FALSE;
1922 /* If any dynamic relocs apply to a read-only section,
1923 then we need a DT_TEXTREL entry. */
1924 if ((info->flags & DF_TEXTREL) == 0)
1925 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1926 (PTR) info);
1928 if ((info->flags & DF_TEXTREL) != 0)
1930 if (!add_dynamic_entry (DT_TEXTREL, 0))
1931 return FALSE;
1935 #undef add_dynamic_entry
1937 return TRUE;
1940 static bfd_boolean
1941 elf64_x86_64_always_size_sections (bfd *output_bfd,
1942 struct bfd_link_info *info)
1944 asection *tls_sec = elf_hash_table (info)->tls_sec;
1946 if (tls_sec)
1948 struct elf_link_hash_entry *tlsbase;
1950 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
1951 "_TLS_MODULE_BASE_",
1952 FALSE, FALSE, FALSE);
1954 if (tlsbase && tlsbase->type == STT_TLS)
1956 struct bfd_link_hash_entry *bh = NULL;
1957 const struct elf_backend_data *bed
1958 = get_elf_backend_data (output_bfd);
1960 if (!(_bfd_generic_link_add_one_symbol
1961 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
1962 tls_sec, 0, NULL, FALSE,
1963 bed->collect, &bh)))
1964 return FALSE;
1965 tlsbase = (struct elf_link_hash_entry *)bh;
1966 tlsbase->def_regular = 1;
1967 tlsbase->other = STV_HIDDEN;
1968 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
1972 return TRUE;
1975 /* Return the base VMA address which should be subtracted from real addresses
1976 when resolving @dtpoff relocation.
1977 This is PT_TLS segment p_vaddr. */
1979 static bfd_vma
1980 dtpoff_base (struct bfd_link_info *info)
1982 /* If tls_sec is NULL, we should have signalled an error already. */
1983 if (elf_hash_table (info)->tls_sec == NULL)
1984 return 0;
1985 return elf_hash_table (info)->tls_sec->vma;
1988 /* Return the relocation value for @tpoff relocation
1989 if STT_TLS virtual address is ADDRESS. */
1991 static bfd_vma
1992 tpoff (struct bfd_link_info *info, bfd_vma address)
1994 struct elf_link_hash_table *htab = elf_hash_table (info);
1996 /* If tls_segment is NULL, we should have signalled an error already. */
1997 if (htab->tls_sec == NULL)
1998 return 0;
1999 return address - htab->tls_size - htab->tls_sec->vma;
2002 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2003 branch? */
2005 static bfd_boolean
2006 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2008 /* Opcode Instruction
2009 0xe8 call
2010 0xe9 jump
2011 0x0f 0x8x conditional jump */
2012 return ((offset > 0
2013 && (contents [offset - 1] == 0xe8
2014 || contents [offset - 1] == 0xe9))
2015 || (offset > 1
2016 && contents [offset - 2] == 0x0f
2017 && (contents [offset - 1] & 0xf0) == 0x80));
2020 /* Relocate an x86_64 ELF section. */
2022 static bfd_boolean
2023 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2024 bfd *input_bfd, asection *input_section,
2025 bfd_byte *contents, Elf_Internal_Rela *relocs,
2026 Elf_Internal_Sym *local_syms,
2027 asection **local_sections)
2029 struct elf64_x86_64_link_hash_table *htab;
2030 Elf_Internal_Shdr *symtab_hdr;
2031 struct elf_link_hash_entry **sym_hashes;
2032 bfd_vma *local_got_offsets;
2033 bfd_vma *local_tlsdesc_gotents;
2034 Elf_Internal_Rela *rel;
2035 Elf_Internal_Rela *relend;
2037 if (info->relocatable)
2038 return TRUE;
2040 htab = elf64_x86_64_hash_table (info);
2041 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2042 sym_hashes = elf_sym_hashes (input_bfd);
2043 local_got_offsets = elf_local_got_offsets (input_bfd);
2044 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2046 rel = relocs;
2047 relend = relocs + input_section->reloc_count;
2048 for (; rel < relend; rel++)
2050 unsigned int r_type;
2051 reloc_howto_type *howto;
2052 unsigned long r_symndx;
2053 struct elf_link_hash_entry *h;
2054 Elf_Internal_Sym *sym;
2055 asection *sec;
2056 bfd_vma off, offplt;
2057 bfd_vma relocation;
2058 bfd_boolean unresolved_reloc;
2059 bfd_reloc_status_type r;
2060 int tls_type;
2062 r_type = ELF64_R_TYPE (rel->r_info);
2063 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2064 || r_type == (int) R_X86_64_GNU_VTENTRY)
2065 continue;
2067 if (r_type >= R_X86_64_max)
2069 bfd_set_error (bfd_error_bad_value);
2070 return FALSE;
2073 howto = x86_64_elf_howto_table + r_type;
2074 r_symndx = ELF64_R_SYM (rel->r_info);
2075 h = NULL;
2076 sym = NULL;
2077 sec = NULL;
2078 unresolved_reloc = FALSE;
2079 if (r_symndx < symtab_hdr->sh_info)
2081 sym = local_syms + r_symndx;
2082 sec = local_sections[r_symndx];
2084 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2086 else
2088 bfd_boolean warned;
2090 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2091 r_symndx, symtab_hdr, sym_hashes,
2092 h, sec, relocation,
2093 unresolved_reloc, warned);
2095 /* When generating a shared object, the relocations handled here are
2096 copied into the output file to be resolved at run time. */
2097 switch (r_type)
2099 case R_X86_64_GOT32:
2100 /* Relocation is to the entry for this symbol in the global
2101 offset table. */
2102 case R_X86_64_GOTPCREL:
2103 /* Use global offset table as symbol value. */
2104 if (htab->sgot == NULL)
2105 abort ();
2107 if (h != NULL)
2109 bfd_boolean dyn;
2111 off = h->got.offset;
2112 dyn = htab->elf.dynamic_sections_created;
2114 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2115 || (info->shared
2116 && SYMBOL_REFERENCES_LOCAL (info, h))
2117 || (ELF_ST_VISIBILITY (h->other)
2118 && h->root.type == bfd_link_hash_undefweak))
2120 /* This is actually a static link, or it is a -Bsymbolic
2121 link and the symbol is defined locally, or the symbol
2122 was forced to be local because of a version file. We
2123 must initialize this entry in the global offset table.
2124 Since the offset must always be a multiple of 8, we
2125 use the least significant bit to record whether we
2126 have initialized it already.
2128 When doing a dynamic link, we create a .rela.got
2129 relocation entry to initialize the value. This is
2130 done in the finish_dynamic_symbol routine. */
2131 if ((off & 1) != 0)
2132 off &= ~1;
2133 else
2135 bfd_put_64 (output_bfd, relocation,
2136 htab->sgot->contents + off);
2137 h->got.offset |= 1;
2140 else
2141 unresolved_reloc = FALSE;
2143 else
2145 if (local_got_offsets == NULL)
2146 abort ();
2148 off = local_got_offsets[r_symndx];
2150 /* The offset must always be a multiple of 8. We use
2151 the least significant bit to record whether we have
2152 already generated the necessary reloc. */
2153 if ((off & 1) != 0)
2154 off &= ~1;
2155 else
2157 bfd_put_64 (output_bfd, relocation,
2158 htab->sgot->contents + off);
2160 if (info->shared)
2162 asection *s;
2163 Elf_Internal_Rela outrel;
2164 bfd_byte *loc;
2166 /* We need to generate a R_X86_64_RELATIVE reloc
2167 for the dynamic linker. */
2168 s = htab->srelgot;
2169 if (s == NULL)
2170 abort ();
2172 outrel.r_offset = (htab->sgot->output_section->vma
2173 + htab->sgot->output_offset
2174 + off);
2175 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2176 outrel.r_addend = relocation;
2177 loc = s->contents;
2178 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2179 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2182 local_got_offsets[r_symndx] |= 1;
2186 if (off >= (bfd_vma) -2)
2187 abort ();
2189 relocation = htab->sgot->output_section->vma
2190 + htab->sgot->output_offset + off;
2191 if (r_type != R_X86_64_GOTPCREL)
2192 relocation -= htab->sgotplt->output_section->vma
2193 - htab->sgotplt->output_offset;
2195 break;
2197 case R_X86_64_GOTOFF64:
2198 /* Relocation is relative to the start of the global offset
2199 table. */
2201 /* Check to make sure it isn't a protected function symbol
2202 for shared library since it may not be local when used
2203 as function address. */
2204 if (info->shared
2205 && h
2206 && h->def_regular
2207 && h->type == STT_FUNC
2208 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2210 (*_bfd_error_handler)
2211 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2212 input_bfd, h->root.root.string);
2213 bfd_set_error (bfd_error_bad_value);
2214 return FALSE;
2217 /* Note that sgot is not involved in this
2218 calculation. We always want the start of .got.plt. If we
2219 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2220 permitted by the ABI, we might have to change this
2221 calculation. */
2222 relocation -= htab->sgotplt->output_section->vma
2223 + htab->sgotplt->output_offset;
2224 break;
2226 case R_X86_64_GOTPC32:
2227 /* Use global offset table as symbol value. */
2228 relocation = htab->sgotplt->output_section->vma
2229 + htab->sgotplt->output_offset;
2230 unresolved_reloc = FALSE;
2231 break;
2233 case R_X86_64_PLT32:
2234 /* Relocation is to the entry for this symbol in the
2235 procedure linkage table. */
2237 /* Resolve a PLT32 reloc against a local symbol directly,
2238 without using the procedure linkage table. */
2239 if (h == NULL)
2240 break;
2242 if (h->plt.offset == (bfd_vma) -1
2243 || htab->splt == NULL)
2245 /* We didn't make a PLT entry for this symbol. This
2246 happens when statically linking PIC code, or when
2247 using -Bsymbolic. */
2248 break;
2251 relocation = (htab->splt->output_section->vma
2252 + htab->splt->output_offset
2253 + h->plt.offset);
2254 unresolved_reloc = FALSE;
2255 break;
2257 case R_X86_64_PC8:
2258 case R_X86_64_PC16:
2259 case R_X86_64_PC32:
2260 if (info->shared
2261 && !SYMBOL_REFERENCES_LOCAL (info, h)
2262 && (input_section->flags & SEC_ALLOC) != 0
2263 && (input_section->flags & SEC_READONLY) != 0
2264 && (!h->def_regular
2265 || r_type != R_X86_64_PC32
2266 || h->type != STT_FUNC
2267 || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
2268 || !is_32bit_relative_branch (contents,
2269 rel->r_offset)))
2271 if (h->def_regular
2272 && r_type == R_X86_64_PC32
2273 && h->type == STT_FUNC
2274 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2275 (*_bfd_error_handler)
2276 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
2277 input_bfd, h->root.root.string);
2278 else
2279 (*_bfd_error_handler)
2280 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2281 input_bfd, x86_64_elf_howto_table[r_type].name,
2282 h->root.root.string);
2283 bfd_set_error (bfd_error_bad_value);
2284 return FALSE;
2286 /* Fall through. */
2288 case R_X86_64_8:
2289 case R_X86_64_16:
2290 case R_X86_64_32:
2291 case R_X86_64_PC64:
2292 case R_X86_64_64:
2293 /* FIXME: The ABI says the linker should make sure the value is
2294 the same when it's zeroextended to 64 bit. */
2296 /* r_symndx will be zero only for relocs against symbols
2297 from removed linkonce sections, or sections discarded by
2298 a linker script. */
2299 if (r_symndx == 0
2300 || (input_section->flags & SEC_ALLOC) == 0)
2301 break;
2303 if ((info->shared
2304 && (h == NULL
2305 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2306 || h->root.type != bfd_link_hash_undefweak)
2307 && ((r_type != R_X86_64_PC8
2308 && r_type != R_X86_64_PC16
2309 && r_type != R_X86_64_PC32
2310 && r_type != R_X86_64_PC64)
2311 || !SYMBOL_CALLS_LOCAL (info, h)))
2312 || (ELIMINATE_COPY_RELOCS
2313 && !info->shared
2314 && h != NULL
2315 && h->dynindx != -1
2316 && !h->non_got_ref
2317 && ((h->def_dynamic
2318 && !h->def_regular)
2319 || h->root.type == bfd_link_hash_undefweak
2320 || h->root.type == bfd_link_hash_undefined)))
2322 Elf_Internal_Rela outrel;
2323 bfd_byte *loc;
2324 bfd_boolean skip, relocate;
2325 asection *sreloc;
2327 /* When generating a shared object, these relocations
2328 are copied into the output file to be resolved at run
2329 time. */
2330 skip = FALSE;
2331 relocate = FALSE;
2333 outrel.r_offset =
2334 _bfd_elf_section_offset (output_bfd, info, input_section,
2335 rel->r_offset);
2336 if (outrel.r_offset == (bfd_vma) -1)
2337 skip = TRUE;
2338 else if (outrel.r_offset == (bfd_vma) -2)
2339 skip = TRUE, relocate = TRUE;
2341 outrel.r_offset += (input_section->output_section->vma
2342 + input_section->output_offset);
2344 if (skip)
2345 memset (&outrel, 0, sizeof outrel);
2347 /* h->dynindx may be -1 if this symbol was marked to
2348 become local. */
2349 else if (h != NULL
2350 && h->dynindx != -1
2351 && (r_type == R_X86_64_PC8
2352 || r_type == R_X86_64_PC16
2353 || r_type == R_X86_64_PC32
2354 || r_type == R_X86_64_PC64
2355 || !info->shared
2356 || !info->symbolic
2357 || !h->def_regular))
2359 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2360 outrel.r_addend = rel->r_addend;
2362 else
2364 /* This symbol is local, or marked to become local. */
2365 if (r_type == R_X86_64_64)
2367 relocate = TRUE;
2368 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2369 outrel.r_addend = relocation + rel->r_addend;
2371 else
2373 long sindx;
2375 if (bfd_is_abs_section (sec))
2376 sindx = 0;
2377 else if (sec == NULL || sec->owner == NULL)
2379 bfd_set_error (bfd_error_bad_value);
2380 return FALSE;
2382 else
2384 asection *osec;
2386 osec = sec->output_section;
2387 sindx = elf_section_data (osec)->dynindx;
2388 BFD_ASSERT (sindx > 0);
2391 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2392 outrel.r_addend = relocation + rel->r_addend;
2396 sreloc = elf_section_data (input_section)->sreloc;
2397 if (sreloc == NULL)
2398 abort ();
2400 loc = sreloc->contents;
2401 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2402 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2404 /* If this reloc is against an external symbol, we do
2405 not want to fiddle with the addend. Otherwise, we
2406 need to include the symbol value so that it becomes
2407 an addend for the dynamic reloc. */
2408 if (! relocate)
2409 continue;
2412 break;
2414 case R_X86_64_TLSGD:
2415 case R_X86_64_GOTPC32_TLSDESC:
2416 case R_X86_64_TLSDESC_CALL:
2417 case R_X86_64_GOTTPOFF:
2418 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
2419 tls_type = GOT_UNKNOWN;
2420 if (h == NULL && local_got_offsets)
2421 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2422 else if (h != NULL)
2424 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2425 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2426 r_type = R_X86_64_TPOFF32;
2428 if (r_type == R_X86_64_TLSGD
2429 || r_type == R_X86_64_GOTPC32_TLSDESC
2430 || r_type == R_X86_64_TLSDESC_CALL)
2432 if (tls_type == GOT_TLS_IE)
2433 r_type = R_X86_64_GOTTPOFF;
2436 if (r_type == R_X86_64_TPOFF32)
2438 BFD_ASSERT (! unresolved_reloc);
2439 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2441 unsigned int i;
2442 static unsigned char tlsgd[8]
2443 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2445 /* GD->LE transition.
2446 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2447 .word 0x6666; rex64; call __tls_get_addr@plt
2448 Change it into:
2449 movq %fs:0, %rax
2450 leaq foo@tpoff(%rax), %rax */
2451 BFD_ASSERT (rel->r_offset >= 4);
2452 for (i = 0; i < 4; i++)
2453 BFD_ASSERT (bfd_get_8 (input_bfd,
2454 contents + rel->r_offset - 4 + i)
2455 == tlsgd[i]);
2456 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2457 for (i = 0; i < 4; i++)
2458 BFD_ASSERT (bfd_get_8 (input_bfd,
2459 contents + rel->r_offset + 4 + i)
2460 == tlsgd[i+4]);
2461 BFD_ASSERT (rel + 1 < relend);
2462 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2463 memcpy (contents + rel->r_offset - 4,
2464 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2465 16);
2466 bfd_put_32 (output_bfd, tpoff (info, relocation),
2467 contents + rel->r_offset + 8);
2468 /* Skip R_X86_64_PLT32. */
2469 rel++;
2470 continue;
2472 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2474 /* GDesc -> LE transition.
2475 It's originally something like:
2476 leaq x@tlsdesc(%rip), %rax
2478 Change it to:
2479 movl $x@tpoff, %rax
2481 Registers other than %rax may be set up here. */
2483 unsigned int val, type, type2;
2484 bfd_vma roff;
2486 /* First, make sure it's a leaq adding rip to a
2487 32-bit offset into any register, although it's
2488 probably almost always going to be rax. */
2489 roff = rel->r_offset;
2490 BFD_ASSERT (roff >= 3);
2491 type = bfd_get_8 (input_bfd, contents + roff - 3);
2492 BFD_ASSERT ((type & 0xfb) == 0x48);
2493 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2494 BFD_ASSERT (type2 == 0x8d);
2495 val = bfd_get_8 (input_bfd, contents + roff - 1);
2496 BFD_ASSERT ((val & 0xc7) == 0x05);
2497 BFD_ASSERT (roff + 4 <= input_section->size);
2499 /* Now modify the instruction as appropriate. */
2500 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
2501 contents + roff - 3);
2502 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
2503 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2504 contents + roff - 1);
2505 bfd_put_32 (output_bfd, tpoff (info, relocation),
2506 contents + roff);
2507 continue;
2509 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2511 /* GDesc -> LE transition.
2512 It's originally:
2513 call *(%rax)
2514 Turn it into:
2515 nop; nop. */
2517 unsigned int val, type;
2518 bfd_vma roff;
2520 /* First, make sure it's a call *(%rax). */
2521 roff = rel->r_offset;
2522 BFD_ASSERT (roff + 2 <= input_section->size);
2523 type = bfd_get_8 (input_bfd, contents + roff);
2524 BFD_ASSERT (type == 0xff);
2525 val = bfd_get_8 (input_bfd, contents + roff + 1);
2526 BFD_ASSERT (val == 0x10);
2528 /* Now modify the instruction as appropriate. */
2529 bfd_put_8 (output_bfd, 0x90, contents + roff);
2530 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2531 continue;
2533 else
2535 unsigned int val, type, reg;
2537 /* IE->LE transition:
2538 Originally it can be one of:
2539 movq foo@gottpoff(%rip), %reg
2540 addq foo@gottpoff(%rip), %reg
2541 We change it into:
2542 movq $foo, %reg
2543 leaq foo(%reg), %reg
2544 addq $foo, %reg. */
2545 BFD_ASSERT (rel->r_offset >= 3);
2546 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2547 BFD_ASSERT (val == 0x48 || val == 0x4c);
2548 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2549 BFD_ASSERT (type == 0x8b || type == 0x03);
2550 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2551 BFD_ASSERT ((reg & 0xc7) == 5);
2552 reg >>= 3;
2553 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2554 if (type == 0x8b)
2556 /* movq */
2557 if (val == 0x4c)
2558 bfd_put_8 (output_bfd, 0x49,
2559 contents + rel->r_offset - 3);
2560 bfd_put_8 (output_bfd, 0xc7,
2561 contents + rel->r_offset - 2);
2562 bfd_put_8 (output_bfd, 0xc0 | reg,
2563 contents + rel->r_offset - 1);
2565 else if (reg == 4)
2567 /* addq -> addq - addressing with %rsp/%r12 is
2568 special */
2569 if (val == 0x4c)
2570 bfd_put_8 (output_bfd, 0x49,
2571 contents + rel->r_offset - 3);
2572 bfd_put_8 (output_bfd, 0x81,
2573 contents + rel->r_offset - 2);
2574 bfd_put_8 (output_bfd, 0xc0 | reg,
2575 contents + rel->r_offset - 1);
2577 else
2579 /* addq -> leaq */
2580 if (val == 0x4c)
2581 bfd_put_8 (output_bfd, 0x4d,
2582 contents + rel->r_offset - 3);
2583 bfd_put_8 (output_bfd, 0x8d,
2584 contents + rel->r_offset - 2);
2585 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2586 contents + rel->r_offset - 1);
2588 bfd_put_32 (output_bfd, tpoff (info, relocation),
2589 contents + rel->r_offset);
2590 continue;
2594 if (htab->sgot == NULL)
2595 abort ();
2597 if (h != NULL)
2599 off = h->got.offset;
2600 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
2602 else
2604 if (local_got_offsets == NULL)
2605 abort ();
2607 off = local_got_offsets[r_symndx];
2608 offplt = local_tlsdesc_gotents[r_symndx];
2611 if ((off & 1) != 0)
2612 off &= ~1;
2613 else
2615 Elf_Internal_Rela outrel;
2616 bfd_byte *loc;
2617 int dr_type, indx;
2618 asection *sreloc;
2620 if (htab->srelgot == NULL)
2621 abort ();
2623 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2625 if (GOT_TLS_GDESC_P (tls_type))
2627 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
2628 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
2629 + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
2630 outrel.r_offset = (htab->sgotplt->output_section->vma
2631 + htab->sgotplt->output_offset
2632 + offplt
2633 + htab->sgotplt_jump_table_size);
2634 sreloc = htab->srelplt;
2635 loc = sreloc->contents;
2636 loc += sreloc->reloc_count++
2637 * sizeof (Elf64_External_Rela);
2638 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2639 <= sreloc->contents + sreloc->size);
2640 if (indx == 0)
2641 outrel.r_addend = relocation - dtpoff_base (info);
2642 else
2643 outrel.r_addend = 0;
2644 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2647 sreloc = htab->srelgot;
2649 outrel.r_offset = (htab->sgot->output_section->vma
2650 + htab->sgot->output_offset + off);
2652 if (GOT_TLS_GD_P (tls_type))
2653 dr_type = R_X86_64_DTPMOD64;
2654 else if (GOT_TLS_GDESC_P (tls_type))
2655 goto dr_done;
2656 else
2657 dr_type = R_X86_64_TPOFF64;
2659 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2660 outrel.r_addend = 0;
2661 if ((dr_type == R_X86_64_TPOFF64
2662 || dr_type == R_X86_64_TLSDESC) && indx == 0)
2663 outrel.r_addend = relocation - dtpoff_base (info);
2664 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2666 loc = sreloc->contents;
2667 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2668 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2669 <= sreloc->contents + sreloc->size);
2670 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2672 if (GOT_TLS_GD_P (tls_type))
2674 if (indx == 0)
2676 BFD_ASSERT (! unresolved_reloc);
2677 bfd_put_64 (output_bfd,
2678 relocation - dtpoff_base (info),
2679 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2681 else
2683 bfd_put_64 (output_bfd, 0,
2684 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2685 outrel.r_info = ELF64_R_INFO (indx,
2686 R_X86_64_DTPOFF64);
2687 outrel.r_offset += GOT_ENTRY_SIZE;
2688 sreloc->reloc_count++;
2689 loc += sizeof (Elf64_External_Rela);
2690 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2691 <= sreloc->contents + sreloc->size);
2692 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2696 dr_done:
2697 if (h != NULL)
2698 h->got.offset |= 1;
2699 else
2700 local_got_offsets[r_symndx] |= 1;
2703 if (off >= (bfd_vma) -2
2704 && ! GOT_TLS_GDESC_P (tls_type))
2705 abort ();
2706 if (r_type == ELF64_R_TYPE (rel->r_info))
2708 if (r_type == R_X86_64_GOTPC32_TLSDESC
2709 || r_type == R_X86_64_TLSDESC_CALL)
2710 relocation = htab->sgotplt->output_section->vma
2711 + htab->sgotplt->output_offset
2712 + offplt + htab->sgotplt_jump_table_size;
2713 else
2714 relocation = htab->sgot->output_section->vma
2715 + htab->sgot->output_offset + off;
2716 unresolved_reloc = FALSE;
2718 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2720 unsigned int i;
2721 static unsigned char tlsgd[8]
2722 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2724 /* GD->IE transition.
2725 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2726 .word 0x6666; rex64; call __tls_get_addr@plt
2727 Change it into:
2728 movq %fs:0, %rax
2729 addq foo@gottpoff(%rip), %rax */
2730 BFD_ASSERT (rel->r_offset >= 4);
2731 for (i = 0; i < 4; i++)
2732 BFD_ASSERT (bfd_get_8 (input_bfd,
2733 contents + rel->r_offset - 4 + i)
2734 == tlsgd[i]);
2735 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2736 for (i = 0; i < 4; i++)
2737 BFD_ASSERT (bfd_get_8 (input_bfd,
2738 contents + rel->r_offset + 4 + i)
2739 == tlsgd[i+4]);
2740 BFD_ASSERT (rel + 1 < relend);
2741 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2742 memcpy (contents + rel->r_offset - 4,
2743 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2744 16);
2746 relocation = (htab->sgot->output_section->vma
2747 + htab->sgot->output_offset + off
2748 - rel->r_offset
2749 - input_section->output_section->vma
2750 - input_section->output_offset
2751 - 12);
2752 bfd_put_32 (output_bfd, relocation,
2753 contents + rel->r_offset + 8);
2754 /* Skip R_X86_64_PLT32. */
2755 rel++;
2756 continue;
2758 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2760 /* GDesc -> IE transition.
2761 It's originally something like:
2762 leaq x@tlsdesc(%rip), %rax
2764 Change it to:
2765 movq x@gottpoff(%rip), %rax # before nop; nop
2767 Registers other than %rax may be set up here. */
2769 unsigned int val, type, type2;
2770 bfd_vma roff;
2772 /* First, make sure it's a leaq adding rip to a 32-bit
2773 offset into any register, although it's probably
2774 almost always going to be rax. */
2775 roff = rel->r_offset;
2776 BFD_ASSERT (roff >= 3);
2777 type = bfd_get_8 (input_bfd, contents + roff - 3);
2778 BFD_ASSERT ((type & 0xfb) == 0x48);
2779 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2780 BFD_ASSERT (type2 == 0x8d);
2781 val = bfd_get_8 (input_bfd, contents + roff - 1);
2782 BFD_ASSERT ((val & 0xc7) == 0x05);
2783 BFD_ASSERT (roff + 4 <= input_section->size);
2785 /* Now modify the instruction as appropriate. */
2786 /* To turn a leaq into a movq in the form we use it, it
2787 suffices to change the second byte from 0x8d to
2788 0x8b. */
2789 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
2791 bfd_put_32 (output_bfd,
2792 htab->sgot->output_section->vma
2793 + htab->sgot->output_offset + off
2794 - rel->r_offset
2795 - input_section->output_section->vma
2796 - input_section->output_offset
2797 - 4,
2798 contents + roff);
2799 continue;
2801 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2803 /* GDesc -> IE transition.
2804 It's originally:
2805 call *(%rax)
2807 Change it to:
2808 nop; nop. */
2810 unsigned int val, type;
2811 bfd_vma roff;
2813 /* First, make sure it's a call *(%eax). */
2814 roff = rel->r_offset;
2815 BFD_ASSERT (roff + 2 <= input_section->size);
2816 type = bfd_get_8 (input_bfd, contents + roff);
2817 BFD_ASSERT (type == 0xff);
2818 val = bfd_get_8 (input_bfd, contents + roff + 1);
2819 BFD_ASSERT (val == 0x10);
2821 /* Now modify the instruction as appropriate. */
2822 bfd_put_8 (output_bfd, 0x90, contents + roff);
2823 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2825 continue;
2827 else
2828 BFD_ASSERT (FALSE);
2829 break;
2831 case R_X86_64_TLSLD:
2832 if (! info->shared)
2834 /* LD->LE transition:
2835 Ensure it is:
2836 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2837 We change it into:
2838 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2839 BFD_ASSERT (rel->r_offset >= 3);
2840 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2841 == 0x48);
2842 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2843 == 0x8d);
2844 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2845 == 0x3d);
2846 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2847 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2848 == 0xe8);
2849 BFD_ASSERT (rel + 1 < relend);
2850 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2851 memcpy (contents + rel->r_offset - 3,
2852 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2853 /* Skip R_X86_64_PLT32. */
2854 rel++;
2855 continue;
2858 if (htab->sgot == NULL)
2859 abort ();
2861 off = htab->tls_ld_got.offset;
2862 if (off & 1)
2863 off &= ~1;
2864 else
2866 Elf_Internal_Rela outrel;
2867 bfd_byte *loc;
2869 if (htab->srelgot == NULL)
2870 abort ();
2872 outrel.r_offset = (htab->sgot->output_section->vma
2873 + htab->sgot->output_offset + off);
2875 bfd_put_64 (output_bfd, 0,
2876 htab->sgot->contents + off);
2877 bfd_put_64 (output_bfd, 0,
2878 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2879 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2880 outrel.r_addend = 0;
2881 loc = htab->srelgot->contents;
2882 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2883 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2884 htab->tls_ld_got.offset |= 1;
2886 relocation = htab->sgot->output_section->vma
2887 + htab->sgot->output_offset + off;
2888 unresolved_reloc = FALSE;
2889 break;
2891 case R_X86_64_DTPOFF32:
2892 if (info->shared || (input_section->flags & SEC_CODE) == 0)
2893 relocation -= dtpoff_base (info);
2894 else
2895 relocation = tpoff (info, relocation);
2896 break;
2898 case R_X86_64_TPOFF32:
2899 BFD_ASSERT (! info->shared);
2900 relocation = tpoff (info, relocation);
2901 break;
2903 default:
2904 break;
2907 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2908 because such sections are not SEC_ALLOC and thus ld.so will
2909 not process them. */
2910 if (unresolved_reloc
2911 && !((input_section->flags & SEC_DEBUGGING) != 0
2912 && h->def_dynamic))
2913 (*_bfd_error_handler)
2914 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
2915 input_bfd,
2916 input_section,
2917 (long) rel->r_offset,
2918 howto->name,
2919 h->root.root.string);
2921 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2922 contents, rel->r_offset,
2923 relocation, rel->r_addend);
2925 if (r != bfd_reloc_ok)
2927 const char *name;
2929 if (h != NULL)
2930 name = h->root.root.string;
2931 else
2933 name = bfd_elf_string_from_elf_section (input_bfd,
2934 symtab_hdr->sh_link,
2935 sym->st_name);
2936 if (name == NULL)
2937 return FALSE;
2938 if (*name == '\0')
2939 name = bfd_section_name (input_bfd, sec);
2942 if (r == bfd_reloc_overflow)
2944 if (h != NULL
2945 && h->root.type == bfd_link_hash_undefweak
2946 && howto->pc_relative)
2947 /* Ignore reloc overflow on branches to undefweak syms. */
2948 continue;
2950 if (! ((*info->callbacks->reloc_overflow)
2951 (info, (h ? &h->root : NULL), name, howto->name,
2952 (bfd_vma) 0, input_bfd, input_section,
2953 rel->r_offset)))
2954 return FALSE;
2956 else
2958 (*_bfd_error_handler)
2959 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2960 input_bfd, input_section,
2961 (long) rel->r_offset, name, (int) r);
2962 return FALSE;
2967 return TRUE;
2970 /* Finish up dynamic symbol handling. We set the contents of various
2971 dynamic sections here. */
2973 static bfd_boolean
2974 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
2975 struct bfd_link_info *info,
2976 struct elf_link_hash_entry *h,
2977 Elf_Internal_Sym *sym)
2979 struct elf64_x86_64_link_hash_table *htab;
2981 htab = elf64_x86_64_hash_table (info);
2983 if (h->plt.offset != (bfd_vma) -1)
2985 bfd_vma plt_index;
2986 bfd_vma got_offset;
2987 Elf_Internal_Rela rela;
2988 bfd_byte *loc;
2990 /* This symbol has an entry in the procedure linkage table. Set
2991 it up. */
2992 if (h->dynindx == -1
2993 || htab->splt == NULL
2994 || htab->sgotplt == NULL
2995 || htab->srelplt == NULL)
2996 abort ();
2998 /* Get the index in the procedure linkage table which
2999 corresponds to this symbol. This is the index of this symbol
3000 in all the symbols for which we are making plt entries. The
3001 first entry in the procedure linkage table is reserved. */
3002 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3004 /* Get the offset into the .got table of the entry that
3005 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3006 bytes. The first three are reserved for the dynamic linker. */
3007 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3009 /* Fill in the entry in the procedure linkage table. */
3010 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3011 PLT_ENTRY_SIZE);
3013 /* Insert the relocation positions of the plt section. The magic
3014 numbers at the end of the statements are the positions of the
3015 relocations in the plt section. */
3016 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3017 instruction uses 6 bytes, subtract this value. */
3018 bfd_put_32 (output_bfd,
3019 (htab->sgotplt->output_section->vma
3020 + htab->sgotplt->output_offset
3021 + got_offset
3022 - htab->splt->output_section->vma
3023 - htab->splt->output_offset
3024 - h->plt.offset
3025 - 6),
3026 htab->splt->contents + h->plt.offset + 2);
3027 /* Put relocation index. */
3028 bfd_put_32 (output_bfd, plt_index,
3029 htab->splt->contents + h->plt.offset + 7);
3030 /* Put offset for jmp .PLT0. */
3031 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3032 htab->splt->contents + h->plt.offset + 12);
3034 /* Fill in the entry in the global offset table, initially this
3035 points to the pushq instruction in the PLT which is at offset 6. */
3036 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
3037 + htab->splt->output_offset
3038 + h->plt.offset + 6),
3039 htab->sgotplt->contents + got_offset);
3041 /* Fill in the entry in the .rela.plt section. */
3042 rela.r_offset = (htab->sgotplt->output_section->vma
3043 + htab->sgotplt->output_offset
3044 + got_offset);
3045 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3046 rela.r_addend = 0;
3047 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
3048 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3050 if (!h->def_regular)
3052 /* Mark the symbol as undefined, rather than as defined in
3053 the .plt section. Leave the value if there were any
3054 relocations where pointer equality matters (this is a clue
3055 for the dynamic linker, to make function pointer
3056 comparisons work between an application and shared
3057 library), otherwise set it to zero. If a function is only
3058 called from a binary, there is no need to slow down
3059 shared libraries because of that. */
3060 sym->st_shndx = SHN_UNDEF;
3061 if (!h->pointer_equality_needed)
3062 sym->st_value = 0;
3066 if (h->got.offset != (bfd_vma) -1
3067 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3068 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3070 Elf_Internal_Rela rela;
3071 bfd_byte *loc;
3073 /* This symbol has an entry in the global offset table. Set it
3074 up. */
3075 if (htab->sgot == NULL || htab->srelgot == NULL)
3076 abort ();
3078 rela.r_offset = (htab->sgot->output_section->vma
3079 + htab->sgot->output_offset
3080 + (h->got.offset &~ (bfd_vma) 1));
3082 /* If this is a static link, or it is a -Bsymbolic link and the
3083 symbol is defined locally or was forced to be local because
3084 of a version file, we just want to emit a RELATIVE reloc.
3085 The entry in the global offset table will already have been
3086 initialized in the relocate_section function. */
3087 if (info->shared
3088 && SYMBOL_REFERENCES_LOCAL (info, h))
3090 BFD_ASSERT((h->got.offset & 1) != 0);
3091 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3092 rela.r_addend = (h->root.u.def.value
3093 + h->root.u.def.section->output_section->vma
3094 + h->root.u.def.section->output_offset);
3096 else
3098 BFD_ASSERT((h->got.offset & 1) == 0);
3099 bfd_put_64 (output_bfd, (bfd_vma) 0,
3100 htab->sgot->contents + h->got.offset);
3101 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3102 rela.r_addend = 0;
3105 loc = htab->srelgot->contents;
3106 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3107 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3110 if (h->needs_copy)
3112 Elf_Internal_Rela rela;
3113 bfd_byte *loc;
3115 /* This symbol needs a copy reloc. Set it up. */
3117 if (h->dynindx == -1
3118 || (h->root.type != bfd_link_hash_defined
3119 && h->root.type != bfd_link_hash_defweak)
3120 || htab->srelbss == NULL)
3121 abort ();
3123 rela.r_offset = (h->root.u.def.value
3124 + h->root.u.def.section->output_section->vma
3125 + h->root.u.def.section->output_offset);
3126 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3127 rela.r_addend = 0;
3128 loc = htab->srelbss->contents;
3129 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
3130 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3133 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3134 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3135 || h == htab->elf.hgot)
3136 sym->st_shndx = SHN_ABS;
3138 return TRUE;
3141 /* Used to decide how to sort relocs in an optimal manner for the
3142 dynamic linker, before writing them out. */
3144 static enum elf_reloc_type_class
3145 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
3147 switch ((int) ELF64_R_TYPE (rela->r_info))
3149 case R_X86_64_RELATIVE:
3150 return reloc_class_relative;
3151 case R_X86_64_JUMP_SLOT:
3152 return reloc_class_plt;
3153 case R_X86_64_COPY:
3154 return reloc_class_copy;
3155 default:
3156 return reloc_class_normal;
3160 /* Finish up the dynamic sections. */
3162 static bfd_boolean
3163 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3165 struct elf64_x86_64_link_hash_table *htab;
3166 bfd *dynobj;
3167 asection *sdyn;
3169 htab = elf64_x86_64_hash_table (info);
3170 dynobj = htab->elf.dynobj;
3171 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3173 if (htab->elf.dynamic_sections_created)
3175 Elf64_External_Dyn *dyncon, *dynconend;
3177 if (sdyn == NULL || htab->sgot == NULL)
3178 abort ();
3180 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3181 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3182 for (; dyncon < dynconend; dyncon++)
3184 Elf_Internal_Dyn dyn;
3185 asection *s;
3187 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3189 switch (dyn.d_tag)
3191 default:
3192 continue;
3194 case DT_PLTGOT:
3195 s = htab->sgotplt;
3196 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3197 break;
3199 case DT_JMPREL:
3200 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3201 break;
3203 case DT_PLTRELSZ:
3204 s = htab->srelplt->output_section;
3205 dyn.d_un.d_val = s->size;
3206 break;
3208 case DT_RELASZ:
3209 /* The procedure linkage table relocs (DT_JMPREL) should
3210 not be included in the overall relocs (DT_RELA).
3211 Therefore, we override the DT_RELASZ entry here to
3212 make it not include the JMPREL relocs. Since the
3213 linker script arranges for .rela.plt to follow all
3214 other relocation sections, we don't have to worry
3215 about changing the DT_RELA entry. */
3216 if (htab->srelplt != NULL)
3218 s = htab->srelplt->output_section;
3219 dyn.d_un.d_val -= s->size;
3221 break;
3223 case DT_TLSDESC_PLT:
3224 s = htab->splt;
3225 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3226 + htab->tlsdesc_plt;
3227 break;
3229 case DT_TLSDESC_GOT:
3230 s = htab->sgot;
3231 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3232 + htab->tlsdesc_got;
3233 break;
3236 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3239 /* Fill in the special first entry in the procedure linkage table. */
3240 if (htab->splt && htab->splt->size > 0)
3242 /* Fill in the first entry in the procedure linkage table. */
3243 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
3244 PLT_ENTRY_SIZE);
3245 /* Add offset for pushq GOT+8(%rip), since the instruction
3246 uses 6 bytes subtract this value. */
3247 bfd_put_32 (output_bfd,
3248 (htab->sgotplt->output_section->vma
3249 + htab->sgotplt->output_offset
3251 - htab->splt->output_section->vma
3252 - htab->splt->output_offset
3253 - 6),
3254 htab->splt->contents + 2);
3255 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3256 the end of the instruction. */
3257 bfd_put_32 (output_bfd,
3258 (htab->sgotplt->output_section->vma
3259 + htab->sgotplt->output_offset
3260 + 16
3261 - htab->splt->output_section->vma
3262 - htab->splt->output_offset
3263 - 12),
3264 htab->splt->contents + 8);
3266 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
3267 PLT_ENTRY_SIZE;
3269 if (htab->tlsdesc_plt)
3271 bfd_put_64 (output_bfd, (bfd_vma) 0,
3272 htab->sgot->contents + htab->tlsdesc_got);
3274 memcpy (htab->splt->contents + htab->tlsdesc_plt,
3275 elf64_x86_64_plt0_entry,
3276 PLT_ENTRY_SIZE);
3278 /* Add offset for pushq GOT+8(%rip), since the
3279 instruction uses 6 bytes subtract this value. */
3280 bfd_put_32 (output_bfd,
3281 (htab->sgotplt->output_section->vma
3282 + htab->sgotplt->output_offset
3284 - htab->splt->output_section->vma
3285 - htab->splt->output_offset
3286 - htab->tlsdesc_plt
3287 - 6),
3288 htab->splt->contents + htab->tlsdesc_plt + 2);
3289 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3290 htab->tlsdesc_got. The 12 is the offset to the end of
3291 the instruction. */
3292 bfd_put_32 (output_bfd,
3293 (htab->sgot->output_section->vma
3294 + htab->sgot->output_offset
3295 + htab->tlsdesc_got
3296 - htab->splt->output_section->vma
3297 - htab->splt->output_offset
3298 - htab->tlsdesc_plt
3299 - 12),
3300 htab->splt->contents + htab->tlsdesc_plt + 8);
3305 if (htab->sgotplt)
3307 /* Fill in the first three entries in the global offset table. */
3308 if (htab->sgotplt->size > 0)
3310 /* Set the first entry in the global offset table to the address of
3311 the dynamic section. */
3312 if (sdyn == NULL)
3313 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
3314 else
3315 bfd_put_64 (output_bfd,
3316 sdyn->output_section->vma + sdyn->output_offset,
3317 htab->sgotplt->contents);
3318 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3319 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
3320 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
3323 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
3324 GOT_ENTRY_SIZE;
3327 if (htab->sgot && htab->sgot->size > 0)
3328 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
3329 = GOT_ENTRY_SIZE;
3331 return TRUE;
3334 /* Return address for Ith PLT stub in section PLT, for relocation REL
3335 or (bfd_vma) -1 if it should not be included. */
3337 static bfd_vma
3338 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
3339 const arelent *rel ATTRIBUTE_UNUSED)
3341 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3344 /* Handle an x86-64 specific section when reading an object file. This
3345 is called when elfcode.h finds a section with an unknown type. */
3347 static bfd_boolean
3348 elf64_x86_64_section_from_shdr (bfd *abfd,
3349 Elf_Internal_Shdr *hdr,
3350 const char *name,
3351 int shindex)
3353 if (hdr->sh_type != SHT_X86_64_UNWIND)
3354 return FALSE;
3356 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3357 return FALSE;
3359 return TRUE;
3362 /* Hook called by the linker routine which adds symbols from an object
3363 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3364 of .bss. */
3366 static bfd_boolean
3367 elf64_x86_64_add_symbol_hook (bfd *abfd,
3368 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3369 Elf_Internal_Sym *sym,
3370 const char **namep ATTRIBUTE_UNUSED,
3371 flagword *flagsp ATTRIBUTE_UNUSED,
3372 asection **secp, bfd_vma *valp)
3374 asection *lcomm;
3376 switch (sym->st_shndx)
3378 case SHN_X86_64_LCOMMON:
3379 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
3380 if (lcomm == NULL)
3382 lcomm = bfd_make_section_with_flags (abfd,
3383 "LARGE_COMMON",
3384 (SEC_ALLOC
3385 | SEC_IS_COMMON
3386 | SEC_LINKER_CREATED));
3387 if (lcomm == NULL)
3388 return FALSE;
3389 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
3391 *secp = lcomm;
3392 *valp = sym->st_size;
3393 break;
3395 return TRUE;
3399 /* Given a BFD section, try to locate the corresponding ELF section
3400 index. */
3402 static bfd_boolean
3403 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
3404 asection *sec, int *index)
3406 if (sec == &_bfd_elf_large_com_section)
3408 *index = SHN_X86_64_LCOMMON;
3409 return TRUE;
3411 return FALSE;
3414 /* Process a symbol. */
3416 static void
3417 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
3418 asymbol *asym)
3420 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
3422 switch (elfsym->internal_elf_sym.st_shndx)
3424 case SHN_X86_64_LCOMMON:
3425 asym->section = &_bfd_elf_large_com_section;
3426 asym->value = elfsym->internal_elf_sym.st_size;
3427 /* Common symbol doesn't set BSF_GLOBAL. */
3428 asym->flags &= ~BSF_GLOBAL;
3429 break;
3433 static bfd_boolean
3434 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
3436 return (sym->st_shndx == SHN_COMMON
3437 || sym->st_shndx == SHN_X86_64_LCOMMON);
3440 static unsigned int
3441 elf64_x86_64_common_section_index (asection *sec)
3443 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3444 return SHN_COMMON;
3445 else
3446 return SHN_X86_64_LCOMMON;
3449 static asection *
3450 elf64_x86_64_common_section (asection *sec)
3452 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3453 return bfd_com_section_ptr;
3454 else
3455 return &_bfd_elf_large_com_section;
3458 static bfd_boolean
3459 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3460 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
3461 struct elf_link_hash_entry *h,
3462 Elf_Internal_Sym *sym,
3463 asection **psec,
3464 bfd_vma *pvalue ATTRIBUTE_UNUSED,
3465 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
3466 bfd_boolean *skip ATTRIBUTE_UNUSED,
3467 bfd_boolean *override ATTRIBUTE_UNUSED,
3468 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
3469 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
3470 bfd_boolean *newdef ATTRIBUTE_UNUSED,
3471 bfd_boolean *newdyn,
3472 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
3473 bfd_boolean *newweak ATTRIBUTE_UNUSED,
3474 bfd *abfd ATTRIBUTE_UNUSED,
3475 asection **sec,
3476 bfd_boolean *olddef ATTRIBUTE_UNUSED,
3477 bfd_boolean *olddyn,
3478 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
3479 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
3480 bfd *oldbfd,
3481 asection **oldsec)
3483 /* A normal common symbol and a large common symbol result in a
3484 normal common symbol. We turn the large common symbol into a
3485 normal one. */
3486 if (!*olddyn
3487 && h->root.type == bfd_link_hash_common
3488 && !*newdyn
3489 && bfd_is_com_section (*sec)
3490 && *oldsec != *sec)
3492 if (sym->st_shndx == SHN_COMMON
3493 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
3495 h->root.u.c.p->section
3496 = bfd_make_section_old_way (oldbfd, "COMMON");
3497 h->root.u.c.p->section->flags = SEC_ALLOC;
3499 else if (sym->st_shndx == SHN_X86_64_LCOMMON
3500 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
3501 *psec = *sec = bfd_com_section_ptr;
3504 return TRUE;
3507 static int
3508 elf64_x86_64_additional_program_headers (bfd *abfd)
3510 asection *s;
3511 int count = 0;
3513 /* Check to see if we need a large readonly segment. */
3514 s = bfd_get_section_by_name (abfd, ".lrodata");
3515 if (s && (s->flags & SEC_LOAD))
3516 count++;
3518 /* Check to see if we need a large data segment. Since .lbss sections
3519 is placed right after the .bss section, there should be no need for
3520 a large data segment just because of .lbss. */
3521 s = bfd_get_section_by_name (abfd, ".ldata");
3522 if (s && (s->flags & SEC_LOAD))
3523 count++;
3525 return count;
3528 static const struct bfd_elf_special_section
3529 elf64_x86_64_special_sections[]=
3531 { ".gnu.linkonce.lb", 16, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3532 { ".gnu.linkonce.lr", 16, -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3533 { ".gnu.linkonce.lt", 16, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
3534 { ".lbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3535 { ".ldata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3536 { ".lrodata", 8, -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3537 { NULL, 0, 0, 0, 0 }
3540 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3541 #define TARGET_LITTLE_NAME "elf64-x86-64"
3542 #define ELF_ARCH bfd_arch_i386
3543 #define ELF_MACHINE_CODE EM_X86_64
3544 #define ELF_MAXPAGESIZE 0x100000
3546 #define elf_backend_can_gc_sections 1
3547 #define elf_backend_can_refcount 1
3548 #define elf_backend_want_got_plt 1
3549 #define elf_backend_plt_readonly 1
3550 #define elf_backend_want_plt_sym 0
3551 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
3552 #define elf_backend_rela_normal 1
3554 #define elf_info_to_howto elf64_x86_64_info_to_howto
3556 #define bfd_elf64_bfd_link_hash_table_create \
3557 elf64_x86_64_link_hash_table_create
3558 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
3560 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
3561 #define elf_backend_check_relocs elf64_x86_64_check_relocs
3562 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3563 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3564 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
3565 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3566 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3567 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3568 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3569 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
3570 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
3571 #define elf_backend_relocate_section elf64_x86_64_relocate_section
3572 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
3573 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
3574 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
3575 #define elf_backend_object_p elf64_x86_64_elf_object_p
3576 #define bfd_elf64_mkobject elf64_x86_64_mkobject
3578 #define elf_backend_section_from_shdr \
3579 elf64_x86_64_section_from_shdr
3581 #define elf_backend_section_from_bfd_section \
3582 elf64_x86_64_elf_section_from_bfd_section
3583 #define elf_backend_add_symbol_hook \
3584 elf64_x86_64_add_symbol_hook
3585 #define elf_backend_symbol_processing \
3586 elf64_x86_64_symbol_processing
3587 #define elf_backend_common_section_index \
3588 elf64_x86_64_common_section_index
3589 #define elf_backend_common_section \
3590 elf64_x86_64_common_section
3591 #define elf_backend_common_definition \
3592 elf64_x86_64_common_definition
3593 #define elf_backend_merge_symbol \
3594 elf64_x86_64_merge_symbol
3595 #define elf_backend_special_sections \
3596 elf64_x86_64_special_sections
3597 #define elf_backend_additional_program_headers \
3598 elf64_x86_64_additional_program_headers
3600 #include "elf64-target.h"