* aout-adobe.c (aout_32_bfd_reloc_name_lookup): Define.
[binutils.git] / bfd / elf64-x86-64.c
blobf171b197acf9420bace6203e6295b864df4bf10b
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
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, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
35 special_function, name, partial_inplace, src_mask, dst_mask, 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 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
116 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
117 FALSE),
118 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
119 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
120 MINUS_ONE, TRUE),
121 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
122 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
123 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
124 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
125 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
126 MINUS_ONE, FALSE),
127 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
128 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
129 MINUS_ONE, FALSE),
130 EMPTY_HOWTO (32),
131 EMPTY_HOWTO (33),
132 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
133 complain_overflow_bitfield, bfd_elf_generic_reloc,
134 "R_X86_64_GOTPC32_TLSDESC",
135 FALSE, 0xffffffff, 0xffffffff, TRUE),
136 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
137 complain_overflow_dont, bfd_elf_generic_reloc,
138 "R_X86_64_TLSDESC_CALL",
139 FALSE, 0, 0, FALSE),
140 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
141 complain_overflow_bitfield, bfd_elf_generic_reloc,
142 "R_X86_64_TLSDESC",
143 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
145 /* We have a gap in the reloc numbers here.
146 R_X86_64_standard counts the number up to this point, and
147 R_X86_64_vt_offset is the value to subtract from a reloc type of
148 R_X86_64_GNU_VT* to form an index into this table. */
149 #define R_X86_64_standard (R_X86_64_TLSDESC + 1)
150 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
152 /* GNU extension to record C++ vtable hierarchy. */
153 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
154 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
156 /* GNU extension to record C++ vtable member usage. */
157 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
158 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
159 FALSE)
162 /* Map BFD relocs to the x86_64 elf relocs. */
163 struct elf_reloc_map
165 bfd_reloc_code_real_type bfd_reloc_val;
166 unsigned char elf_reloc_val;
169 static const struct elf_reloc_map x86_64_reloc_map[] =
171 { BFD_RELOC_NONE, R_X86_64_NONE, },
172 { BFD_RELOC_64, R_X86_64_64, },
173 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
174 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
175 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
176 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
177 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
178 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
179 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
180 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
181 { BFD_RELOC_32, R_X86_64_32, },
182 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
183 { BFD_RELOC_16, R_X86_64_16, },
184 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
185 { BFD_RELOC_8, R_X86_64_8, },
186 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
187 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
188 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
189 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
190 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
191 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
192 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
193 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
194 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
195 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
196 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
197 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
198 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
199 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
200 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
201 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
202 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
203 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
204 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
205 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
206 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
207 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
210 static reloc_howto_type *
211 elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
213 unsigned i;
215 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
216 || r_type >= (unsigned int) R_X86_64_max)
218 if (r_type >= (unsigned int) R_X86_64_standard)
220 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
221 abfd, (int) r_type);
222 r_type = R_X86_64_NONE;
224 i = r_type;
226 else
227 i = r_type - (unsigned int) R_X86_64_vt_offset;
228 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
229 return &x86_64_elf_howto_table[i];
232 /* Given a BFD reloc type, return a HOWTO structure. */
233 static reloc_howto_type *
234 elf64_x86_64_reloc_type_lookup (bfd *abfd,
235 bfd_reloc_code_real_type code)
237 unsigned int i;
239 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
240 i++)
242 if (x86_64_reloc_map[i].bfd_reloc_val == code)
243 return elf64_x86_64_rtype_to_howto (abfd,
244 x86_64_reloc_map[i].elf_reloc_val);
246 return 0;
249 static reloc_howto_type *
250 elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
251 const char *r_name)
253 unsigned int i;
255 for (i = 0;
256 i < (sizeof (x86_64_elf_howto_table)
257 / sizeof (x86_64_elf_howto_table[0]));
258 i++)
259 if (x86_64_elf_howto_table[i].name != NULL
260 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
261 return &x86_64_elf_howto_table[i];
263 return NULL;
266 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
268 static void
269 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
270 Elf_Internal_Rela *dst)
272 unsigned r_type;
274 r_type = ELF64_R_TYPE (dst->r_info);
275 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
276 BFD_ASSERT (r_type == cache_ptr->howto->type);
279 /* Support for core dump NOTE sections. */
280 static bfd_boolean
281 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
283 int offset;
284 size_t size;
286 switch (note->descsz)
288 default:
289 return FALSE;
291 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
292 /* pr_cursig */
293 elf_tdata (abfd)->core_signal
294 = bfd_get_16 (abfd, note->descdata + 12);
296 /* pr_pid */
297 elf_tdata (abfd)->core_pid
298 = bfd_get_32 (abfd, note->descdata + 32);
300 /* pr_reg */
301 offset = 112;
302 size = 216;
304 break;
307 /* Make a ".reg/999" section. */
308 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
309 size, note->descpos + offset);
312 static bfd_boolean
313 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
315 switch (note->descsz)
317 default:
318 return FALSE;
320 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
321 elf_tdata (abfd)->core_program
322 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
323 elf_tdata (abfd)->core_command
324 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
327 /* Note that for some reason, a spurious space is tacked
328 onto the end of the args in some (at least one anyway)
329 implementations, so strip it off if it exists. */
332 char *command = elf_tdata (abfd)->core_command;
333 int n = strlen (command);
335 if (0 < n && command[n - 1] == ' ')
336 command[n - 1] = '\0';
339 return TRUE;
342 /* Functions for the x86-64 ELF linker. */
344 /* The name of the dynamic interpreter. This is put in the .interp
345 section. */
347 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
349 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
350 copying dynamic variables from a shared lib into an app's dynbss
351 section, and instead use a dynamic relocation to point into the
352 shared lib. */
353 #define ELIMINATE_COPY_RELOCS 1
355 /* The size in bytes of an entry in the global offset table. */
357 #define GOT_ENTRY_SIZE 8
359 /* The size in bytes of an entry in the procedure linkage table. */
361 #define PLT_ENTRY_SIZE 16
363 /* The first entry in a procedure linkage table looks like this. See the
364 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
366 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
368 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
369 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
370 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
373 /* Subsequent entries in a procedure linkage table look like this. */
375 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
377 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
378 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
379 0x68, /* pushq immediate */
380 0, 0, 0, 0, /* replaced with index into relocation table. */
381 0xe9, /* jmp relative */
382 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
385 /* The x86-64 linker needs to keep track of the number of relocs that
386 it decides to copy as dynamic relocs in check_relocs for each symbol.
387 This is so that it can later discard them if they are found to be
388 unnecessary. We store the information in a field extending the
389 regular ELF linker hash table. */
391 struct elf64_x86_64_dyn_relocs
393 /* Next section. */
394 struct elf64_x86_64_dyn_relocs *next;
396 /* The input section of the reloc. */
397 asection *sec;
399 /* Total number of relocs copied for the input section. */
400 bfd_size_type count;
402 /* Number of pc-relative relocs copied for the input section. */
403 bfd_size_type pc_count;
406 /* x86-64 ELF linker hash entry. */
408 struct elf64_x86_64_link_hash_entry
410 struct elf_link_hash_entry elf;
412 /* Track dynamic relocs copied for this symbol. */
413 struct elf64_x86_64_dyn_relocs *dyn_relocs;
415 #define GOT_UNKNOWN 0
416 #define GOT_NORMAL 1
417 #define GOT_TLS_GD 2
418 #define GOT_TLS_IE 3
419 #define GOT_TLS_GDESC 4
420 #define GOT_TLS_GD_BOTH_P(type) \
421 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
422 #define GOT_TLS_GD_P(type) \
423 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
424 #define GOT_TLS_GDESC_P(type) \
425 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
426 #define GOT_TLS_GD_ANY_P(type) \
427 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
428 unsigned char tls_type;
430 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
431 starting at the end of the jump table. */
432 bfd_vma tlsdesc_got;
435 #define elf64_x86_64_hash_entry(ent) \
436 ((struct elf64_x86_64_link_hash_entry *)(ent))
438 struct elf64_x86_64_obj_tdata
440 struct elf_obj_tdata root;
442 /* tls_type for each local got entry. */
443 char *local_got_tls_type;
445 /* GOTPLT entries for TLS descriptors. */
446 bfd_vma *local_tlsdesc_gotent;
449 #define elf64_x86_64_tdata(abfd) \
450 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
452 #define elf64_x86_64_local_got_tls_type(abfd) \
453 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
455 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
456 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
458 /* x86-64 ELF linker hash table. */
460 struct elf64_x86_64_link_hash_table
462 struct elf_link_hash_table elf;
464 /* Short-cuts to get to dynamic linker sections. */
465 asection *sgot;
466 asection *sgotplt;
467 asection *srelgot;
468 asection *splt;
469 asection *srelplt;
470 asection *sdynbss;
471 asection *srelbss;
473 /* The offset into splt of the PLT entry for the TLS descriptor
474 resolver. Special values are 0, if not necessary (or not found
475 to be necessary yet), and -1 if needed but not determined
476 yet. */
477 bfd_vma tlsdesc_plt;
478 /* The offset into sgot of the GOT entry used by the PLT entry
479 above. */
480 bfd_vma tlsdesc_got;
482 union {
483 bfd_signed_vma refcount;
484 bfd_vma offset;
485 } tls_ld_got;
487 /* The amount of space used by the jump slots in the GOT. */
488 bfd_vma sgotplt_jump_table_size;
490 /* Small local sym to section mapping cache. */
491 struct sym_sec_cache sym_sec;
494 /* Get the x86-64 ELF linker hash table from a link_info structure. */
496 #define elf64_x86_64_hash_table(p) \
497 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
499 #define elf64_x86_64_compute_jump_table_size(htab) \
500 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
502 /* Create an entry in an x86-64 ELF linker hash table. */
504 static struct bfd_hash_entry *
505 link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
506 const char *string)
508 /* Allocate the structure if it has not already been allocated by a
509 subclass. */
510 if (entry == NULL)
512 entry = bfd_hash_allocate (table,
513 sizeof (struct elf64_x86_64_link_hash_entry));
514 if (entry == NULL)
515 return entry;
518 /* Call the allocation method of the superclass. */
519 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
520 if (entry != NULL)
522 struct elf64_x86_64_link_hash_entry *eh;
524 eh = (struct elf64_x86_64_link_hash_entry *) entry;
525 eh->dyn_relocs = NULL;
526 eh->tls_type = GOT_UNKNOWN;
527 eh->tlsdesc_got = (bfd_vma) -1;
530 return entry;
533 /* Create an X86-64 ELF linker hash table. */
535 static struct bfd_link_hash_table *
536 elf64_x86_64_link_hash_table_create (bfd *abfd)
538 struct elf64_x86_64_link_hash_table *ret;
539 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
541 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
542 if (ret == NULL)
543 return NULL;
545 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc,
546 sizeof (struct elf64_x86_64_link_hash_entry)))
548 free (ret);
549 return NULL;
552 ret->sgot = NULL;
553 ret->sgotplt = NULL;
554 ret->srelgot = NULL;
555 ret->splt = NULL;
556 ret->srelplt = NULL;
557 ret->sdynbss = NULL;
558 ret->srelbss = NULL;
559 ret->sym_sec.abfd = NULL;
560 ret->tlsdesc_plt = 0;
561 ret->tlsdesc_got = 0;
562 ret->tls_ld_got.refcount = 0;
563 ret->sgotplt_jump_table_size = 0;
565 return &ret->elf.root;
568 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
569 shortcuts to them in our hash table. */
571 static bfd_boolean
572 create_got_section (bfd *dynobj, struct bfd_link_info *info)
574 struct elf64_x86_64_link_hash_table *htab;
576 if (! _bfd_elf_create_got_section (dynobj, info))
577 return FALSE;
579 htab = elf64_x86_64_hash_table (info);
580 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
581 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
582 if (!htab->sgot || !htab->sgotplt)
583 abort ();
585 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
586 (SEC_ALLOC | SEC_LOAD
587 | SEC_HAS_CONTENTS
588 | SEC_IN_MEMORY
589 | SEC_LINKER_CREATED
590 | SEC_READONLY));
591 if (htab->srelgot == NULL
592 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
593 return FALSE;
594 return TRUE;
597 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
598 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
599 hash table. */
601 static bfd_boolean
602 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
604 struct elf64_x86_64_link_hash_table *htab;
606 htab = elf64_x86_64_hash_table (info);
607 if (!htab->sgot && !create_got_section (dynobj, info))
608 return FALSE;
610 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
611 return FALSE;
613 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
614 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
615 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
616 if (!info->shared)
617 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
619 if (!htab->splt || !htab->srelplt || !htab->sdynbss
620 || (!info->shared && !htab->srelbss))
621 abort ();
623 return TRUE;
626 /* Copy the extra info we tack onto an elf_link_hash_entry. */
628 static void
629 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
630 struct elf_link_hash_entry *dir,
631 struct elf_link_hash_entry *ind)
633 struct elf64_x86_64_link_hash_entry *edir, *eind;
635 edir = (struct elf64_x86_64_link_hash_entry *) dir;
636 eind = (struct elf64_x86_64_link_hash_entry *) ind;
638 if (eind->dyn_relocs != NULL)
640 if (edir->dyn_relocs != NULL)
642 struct elf64_x86_64_dyn_relocs **pp;
643 struct elf64_x86_64_dyn_relocs *p;
645 /* Add reloc counts against the indirect sym to the direct sym
646 list. Merge any entries against the same section. */
647 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
649 struct elf64_x86_64_dyn_relocs *q;
651 for (q = edir->dyn_relocs; q != NULL; q = q->next)
652 if (q->sec == p->sec)
654 q->pc_count += p->pc_count;
655 q->count += p->count;
656 *pp = p->next;
657 break;
659 if (q == NULL)
660 pp = &p->next;
662 *pp = edir->dyn_relocs;
665 edir->dyn_relocs = eind->dyn_relocs;
666 eind->dyn_relocs = NULL;
669 if (ind->root.type == bfd_link_hash_indirect
670 && dir->got.refcount <= 0)
672 edir->tls_type = eind->tls_type;
673 eind->tls_type = GOT_UNKNOWN;
676 if (ELIMINATE_COPY_RELOCS
677 && ind->root.type != bfd_link_hash_indirect
678 && dir->dynamic_adjusted)
680 /* If called to transfer flags for a weakdef during processing
681 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
682 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
683 dir->ref_dynamic |= ind->ref_dynamic;
684 dir->ref_regular |= ind->ref_regular;
685 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
686 dir->needs_plt |= ind->needs_plt;
687 dir->pointer_equality_needed |= ind->pointer_equality_needed;
689 else
690 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
693 static bfd_boolean
694 elf64_x86_64_mkobject (bfd *abfd)
696 if (abfd->tdata.any == NULL)
698 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
699 abfd->tdata.any = bfd_zalloc (abfd, amt);
700 if (abfd->tdata.any == NULL)
701 return FALSE;
703 return bfd_elf_mkobject (abfd);
706 static bfd_boolean
707 elf64_x86_64_elf_object_p (bfd *abfd)
709 /* Set the right machine number for an x86-64 elf64 file. */
710 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
711 return TRUE;
714 static int
715 elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
717 if (info->shared)
718 return r_type;
720 switch (r_type)
722 case R_X86_64_TLSGD:
723 case R_X86_64_GOTPC32_TLSDESC:
724 case R_X86_64_TLSDESC_CALL:
725 case R_X86_64_GOTTPOFF:
726 if (is_local)
727 return R_X86_64_TPOFF32;
728 return R_X86_64_GOTTPOFF;
729 case R_X86_64_TLSLD:
730 return R_X86_64_TPOFF32;
733 return r_type;
736 /* Look through the relocs for a section during the first phase, and
737 calculate needed space in the global offset table, procedure
738 linkage table, and dynamic reloc sections. */
740 static bfd_boolean
741 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
742 const Elf_Internal_Rela *relocs)
744 struct elf64_x86_64_link_hash_table *htab;
745 Elf_Internal_Shdr *symtab_hdr;
746 struct elf_link_hash_entry **sym_hashes;
747 const Elf_Internal_Rela *rel;
748 const Elf_Internal_Rela *rel_end;
749 asection *sreloc;
751 if (info->relocatable)
752 return TRUE;
754 htab = elf64_x86_64_hash_table (info);
755 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
756 sym_hashes = elf_sym_hashes (abfd);
758 sreloc = NULL;
760 rel_end = relocs + sec->reloc_count;
761 for (rel = relocs; rel < rel_end; rel++)
763 unsigned int r_type;
764 unsigned long r_symndx;
765 struct elf_link_hash_entry *h;
767 r_symndx = ELF64_R_SYM (rel->r_info);
768 r_type = ELF64_R_TYPE (rel->r_info);
770 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
772 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
773 abfd, r_symndx);
774 return FALSE;
777 if (r_symndx < symtab_hdr->sh_info)
778 h = NULL;
779 else
781 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
782 while (h->root.type == bfd_link_hash_indirect
783 || h->root.type == bfd_link_hash_warning)
784 h = (struct elf_link_hash_entry *) h->root.u.i.link;
787 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
788 switch (r_type)
790 case R_X86_64_TLSLD:
791 htab->tls_ld_got.refcount += 1;
792 goto create_got;
794 case R_X86_64_TPOFF32:
795 if (info->shared)
797 (*_bfd_error_handler)
798 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
799 abfd,
800 x86_64_elf_howto_table[r_type].name,
801 (h) ? h->root.root.string : "a local symbol");
802 bfd_set_error (bfd_error_bad_value);
803 return FALSE;
805 break;
807 case R_X86_64_GOTTPOFF:
808 if (info->shared)
809 info->flags |= DF_STATIC_TLS;
810 /* Fall through */
812 case R_X86_64_GOT32:
813 case R_X86_64_GOTPCREL:
814 case R_X86_64_TLSGD:
815 case R_X86_64_GOT64:
816 case R_X86_64_GOTPCREL64:
817 case R_X86_64_GOTPLT64:
818 case R_X86_64_GOTPC32_TLSDESC:
819 case R_X86_64_TLSDESC_CALL:
820 /* This symbol requires a global offset table entry. */
822 int tls_type, old_tls_type;
824 switch (r_type)
826 default: tls_type = GOT_NORMAL; break;
827 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
828 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
829 case R_X86_64_GOTPC32_TLSDESC:
830 case R_X86_64_TLSDESC_CALL:
831 tls_type = GOT_TLS_GDESC; break;
834 if (h != NULL)
836 if (r_type == R_X86_64_GOTPLT64)
838 /* This relocation indicates that we also need
839 a PLT entry, as this is a function. We don't need
840 a PLT entry for local symbols. */
841 h->needs_plt = 1;
842 h->plt.refcount += 1;
844 h->got.refcount += 1;
845 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
847 else
849 bfd_signed_vma *local_got_refcounts;
851 /* This is a global offset table entry for a local symbol. */
852 local_got_refcounts = elf_local_got_refcounts (abfd);
853 if (local_got_refcounts == NULL)
855 bfd_size_type size;
857 size = symtab_hdr->sh_info;
858 size *= sizeof (bfd_signed_vma)
859 + sizeof (bfd_vma) + sizeof (char);
860 local_got_refcounts = ((bfd_signed_vma *)
861 bfd_zalloc (abfd, size));
862 if (local_got_refcounts == NULL)
863 return FALSE;
864 elf_local_got_refcounts (abfd) = local_got_refcounts;
865 elf64_x86_64_local_tlsdesc_gotent (abfd)
866 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
867 elf64_x86_64_local_got_tls_type (abfd)
868 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
870 local_got_refcounts[r_symndx] += 1;
871 old_tls_type
872 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
875 /* If a TLS symbol is accessed using IE at least once,
876 there is no point to use dynamic model for it. */
877 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
878 && (! GOT_TLS_GD_ANY_P (old_tls_type)
879 || tls_type != GOT_TLS_IE))
881 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
882 tls_type = old_tls_type;
883 else if (GOT_TLS_GD_ANY_P (old_tls_type)
884 && GOT_TLS_GD_ANY_P (tls_type))
885 tls_type |= old_tls_type;
886 else
888 (*_bfd_error_handler)
889 (_("%B: %s' accessed both as normal and thread local symbol"),
890 abfd, h ? h->root.root.string : "<local>");
891 return FALSE;
895 if (old_tls_type != tls_type)
897 if (h != NULL)
898 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
899 else
900 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
903 /* Fall through */
905 case R_X86_64_GOTOFF64:
906 case R_X86_64_GOTPC32:
907 case R_X86_64_GOTPC64:
908 create_got:
909 if (htab->sgot == NULL)
911 if (htab->elf.dynobj == NULL)
912 htab->elf.dynobj = abfd;
913 if (!create_got_section (htab->elf.dynobj, info))
914 return FALSE;
916 break;
918 case R_X86_64_PLT32:
919 /* This symbol requires a procedure linkage table entry. We
920 actually build the entry in adjust_dynamic_symbol,
921 because this might be a case of linking PIC code which is
922 never referenced by a dynamic object, in which case we
923 don't need to generate a procedure linkage table entry
924 after all. */
926 /* If this is a local symbol, we resolve it directly without
927 creating a procedure linkage table entry. */
928 if (h == NULL)
929 continue;
931 h->needs_plt = 1;
932 h->plt.refcount += 1;
933 break;
935 case R_X86_64_PLTOFF64:
936 /* This tries to form the 'address' of a function relative
937 to GOT. For global symbols we need a PLT entry. */
938 if (h != NULL)
940 h->needs_plt = 1;
941 h->plt.refcount += 1;
943 goto create_got;
945 case R_X86_64_8:
946 case R_X86_64_16:
947 case R_X86_64_32:
948 case R_X86_64_32S:
949 /* Let's help debug shared library creation. These relocs
950 cannot be used in shared libs. Don't error out for
951 sections we don't care about, such as debug sections or
952 non-constant sections. */
953 if (info->shared
954 && (sec->flags & SEC_ALLOC) != 0
955 && (sec->flags & SEC_READONLY) != 0)
957 (*_bfd_error_handler)
958 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
959 abfd,
960 x86_64_elf_howto_table[r_type].name,
961 (h) ? h->root.root.string : "a local symbol");
962 bfd_set_error (bfd_error_bad_value);
963 return FALSE;
965 /* Fall through. */
967 case R_X86_64_PC8:
968 case R_X86_64_PC16:
969 case R_X86_64_PC32:
970 case R_X86_64_PC64:
971 case R_X86_64_64:
972 if (h != NULL && !info->shared)
974 /* If this reloc is in a read-only section, we might
975 need a copy reloc. We can't check reliably at this
976 stage whether the section is read-only, as input
977 sections have not yet been mapped to output sections.
978 Tentatively set the flag for now, and correct in
979 adjust_dynamic_symbol. */
980 h->non_got_ref = 1;
982 /* We may need a .plt entry if the function this reloc
983 refers to is in a shared lib. */
984 h->plt.refcount += 1;
985 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
986 h->pointer_equality_needed = 1;
989 /* If we are creating a shared library, and this is a reloc
990 against a global symbol, or a non PC relative reloc
991 against a local symbol, then we need to copy the reloc
992 into the shared library. However, if we are linking with
993 -Bsymbolic, we do not need to copy a reloc against a
994 global symbol which is defined in an object we are
995 including in the link (i.e., DEF_REGULAR is set). At
996 this point we have not seen all the input files, so it is
997 possible that DEF_REGULAR is not set now but will be set
998 later (it is never cleared). In case of a weak definition,
999 DEF_REGULAR may be cleared later by a strong definition in
1000 a shared library. We account for that possibility below by
1001 storing information in the relocs_copied field of the hash
1002 table entry. A similar situation occurs when creating
1003 shared libraries and symbol visibility changes render the
1004 symbol local.
1006 If on the other hand, we are creating an executable, we
1007 may need to keep relocations for symbols satisfied by a
1008 dynamic library if we manage to avoid copy relocs for the
1009 symbol. */
1010 if ((info->shared
1011 && (sec->flags & SEC_ALLOC) != 0
1012 && (((r_type != R_X86_64_PC8)
1013 && (r_type != R_X86_64_PC16)
1014 && (r_type != R_X86_64_PC32)
1015 && (r_type != R_X86_64_PC64))
1016 || (h != NULL
1017 && (! SYMBOLIC_BIND (info, h)
1018 || h->root.type == bfd_link_hash_defweak
1019 || !h->def_regular))))
1020 || (ELIMINATE_COPY_RELOCS
1021 && !info->shared
1022 && (sec->flags & SEC_ALLOC) != 0
1023 && h != NULL
1024 && (h->root.type == bfd_link_hash_defweak
1025 || !h->def_regular)))
1027 struct elf64_x86_64_dyn_relocs *p;
1028 struct elf64_x86_64_dyn_relocs **head;
1030 /* We must copy these reloc types into the output file.
1031 Create a reloc section in dynobj and make room for
1032 this reloc. */
1033 if (sreloc == NULL)
1035 const char *name;
1036 bfd *dynobj;
1038 name = (bfd_elf_string_from_elf_section
1039 (abfd,
1040 elf_elfheader (abfd)->e_shstrndx,
1041 elf_section_data (sec)->rel_hdr.sh_name));
1042 if (name == NULL)
1043 return FALSE;
1045 if (! CONST_STRNEQ (name, ".rela")
1046 || strcmp (bfd_get_section_name (abfd, sec),
1047 name + 5) != 0)
1049 (*_bfd_error_handler)
1050 (_("%B: bad relocation section name `%s\'"),
1051 abfd, name);
1054 if (htab->elf.dynobj == NULL)
1055 htab->elf.dynobj = abfd;
1057 dynobj = htab->elf.dynobj;
1059 sreloc = bfd_get_section_by_name (dynobj, name);
1060 if (sreloc == NULL)
1062 flagword flags;
1064 flags = (SEC_HAS_CONTENTS | SEC_READONLY
1065 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
1066 if ((sec->flags & SEC_ALLOC) != 0)
1067 flags |= SEC_ALLOC | SEC_LOAD;
1068 sreloc = bfd_make_section_with_flags (dynobj,
1069 name,
1070 flags);
1071 if (sreloc == NULL
1072 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
1073 return FALSE;
1075 elf_section_data (sec)->sreloc = sreloc;
1078 /* If this is a global symbol, we count the number of
1079 relocations we need for this symbol. */
1080 if (h != NULL)
1082 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1084 else
1086 void **vpp;
1087 /* Track dynamic relocs needed for local syms too.
1088 We really need local syms available to do this
1089 easily. Oh well. */
1091 asection *s;
1092 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1093 sec, r_symndx);
1094 if (s == NULL)
1095 return FALSE;
1097 /* Beware of type punned pointers vs strict aliasing
1098 rules. */
1099 vpp = &(elf_section_data (s)->local_dynrel);
1100 head = (struct elf64_x86_64_dyn_relocs **)vpp;
1103 p = *head;
1104 if (p == NULL || p->sec != sec)
1106 bfd_size_type amt = sizeof *p;
1107 p = ((struct elf64_x86_64_dyn_relocs *)
1108 bfd_alloc (htab->elf.dynobj, amt));
1109 if (p == NULL)
1110 return FALSE;
1111 p->next = *head;
1112 *head = p;
1113 p->sec = sec;
1114 p->count = 0;
1115 p->pc_count = 0;
1118 p->count += 1;
1119 if (r_type == R_X86_64_PC8
1120 || r_type == R_X86_64_PC16
1121 || r_type == R_X86_64_PC32
1122 || r_type == R_X86_64_PC64)
1123 p->pc_count += 1;
1125 break;
1127 /* This relocation describes the C++ object vtable hierarchy.
1128 Reconstruct it for later use during GC. */
1129 case R_X86_64_GNU_VTINHERIT:
1130 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1131 return FALSE;
1132 break;
1134 /* This relocation describes which C++ vtable entries are actually
1135 used. Record for later use during GC. */
1136 case R_X86_64_GNU_VTENTRY:
1137 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1138 return FALSE;
1139 break;
1141 default:
1142 break;
1146 return TRUE;
1149 /* Return the section that should be marked against GC for a given
1150 relocation. */
1152 static asection *
1153 elf64_x86_64_gc_mark_hook (asection *sec,
1154 struct bfd_link_info *info,
1155 Elf_Internal_Rela *rel,
1156 struct elf_link_hash_entry *h,
1157 Elf_Internal_Sym *sym)
1159 if (h != NULL)
1160 switch (ELF64_R_TYPE (rel->r_info))
1162 case R_X86_64_GNU_VTINHERIT:
1163 case R_X86_64_GNU_VTENTRY:
1164 return NULL;
1167 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1170 /* Update the got entry reference counts for the section being removed. */
1172 static bfd_boolean
1173 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1174 asection *sec, const Elf_Internal_Rela *relocs)
1176 Elf_Internal_Shdr *symtab_hdr;
1177 struct elf_link_hash_entry **sym_hashes;
1178 bfd_signed_vma *local_got_refcounts;
1179 const Elf_Internal_Rela *rel, *relend;
1181 elf_section_data (sec)->local_dynrel = NULL;
1183 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1184 sym_hashes = elf_sym_hashes (abfd);
1185 local_got_refcounts = elf_local_got_refcounts (abfd);
1187 relend = relocs + sec->reloc_count;
1188 for (rel = relocs; rel < relend; rel++)
1190 unsigned long r_symndx;
1191 unsigned int r_type;
1192 struct elf_link_hash_entry *h = NULL;
1194 r_symndx = ELF64_R_SYM (rel->r_info);
1195 if (r_symndx >= symtab_hdr->sh_info)
1197 struct elf64_x86_64_link_hash_entry *eh;
1198 struct elf64_x86_64_dyn_relocs **pp;
1199 struct elf64_x86_64_dyn_relocs *p;
1201 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1202 while (h->root.type == bfd_link_hash_indirect
1203 || h->root.type == bfd_link_hash_warning)
1204 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1205 eh = (struct elf64_x86_64_link_hash_entry *) h;
1207 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1208 if (p->sec == sec)
1210 /* Everything must go for SEC. */
1211 *pp = p->next;
1212 break;
1216 r_type = ELF64_R_TYPE (rel->r_info);
1217 r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
1218 switch (r_type)
1220 case R_X86_64_TLSLD:
1221 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1222 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1223 break;
1225 case R_X86_64_TLSGD:
1226 case R_X86_64_GOTPC32_TLSDESC:
1227 case R_X86_64_TLSDESC_CALL:
1228 case R_X86_64_GOTTPOFF:
1229 case R_X86_64_GOT32:
1230 case R_X86_64_GOTPCREL:
1231 case R_X86_64_GOT64:
1232 case R_X86_64_GOTPCREL64:
1233 case R_X86_64_GOTPLT64:
1234 if (h != NULL)
1236 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1237 h->plt.refcount -= 1;
1238 if (h->got.refcount > 0)
1239 h->got.refcount -= 1;
1241 else if (local_got_refcounts != NULL)
1243 if (local_got_refcounts[r_symndx] > 0)
1244 local_got_refcounts[r_symndx] -= 1;
1246 break;
1248 case R_X86_64_8:
1249 case R_X86_64_16:
1250 case R_X86_64_32:
1251 case R_X86_64_64:
1252 case R_X86_64_32S:
1253 case R_X86_64_PC8:
1254 case R_X86_64_PC16:
1255 case R_X86_64_PC32:
1256 case R_X86_64_PC64:
1257 if (info->shared)
1258 break;
1259 /* Fall thru */
1261 case R_X86_64_PLT32:
1262 case R_X86_64_PLTOFF64:
1263 if (h != NULL)
1265 if (h->plt.refcount > 0)
1266 h->plt.refcount -= 1;
1268 break;
1270 default:
1271 break;
1275 return TRUE;
1278 /* Adjust a symbol defined by a dynamic object and referenced by a
1279 regular object. The current definition is in some section of the
1280 dynamic object, but we're not including those sections. We have to
1281 change the definition to something the rest of the link can
1282 understand. */
1284 static bfd_boolean
1285 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1286 struct elf_link_hash_entry *h)
1288 struct elf64_x86_64_link_hash_table *htab;
1289 asection *s;
1290 unsigned int power_of_two;
1292 /* If this is a function, put it in the procedure linkage table. We
1293 will fill in the contents of the procedure linkage table later,
1294 when we know the address of the .got section. */
1295 if (h->type == STT_FUNC
1296 || h->needs_plt)
1298 if (h->plt.refcount <= 0
1299 || SYMBOL_CALLS_LOCAL (info, h)
1300 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1301 && h->root.type == bfd_link_hash_undefweak))
1303 /* This case can occur if we saw a PLT32 reloc in an input
1304 file, but the symbol was never referred to by a dynamic
1305 object, or if all references were garbage collected. In
1306 such a case, we don't actually need to build a procedure
1307 linkage table, and we can just do a PC32 reloc instead. */
1308 h->plt.offset = (bfd_vma) -1;
1309 h->needs_plt = 0;
1312 return TRUE;
1314 else
1315 /* It's possible that we incorrectly decided a .plt reloc was
1316 needed for an R_X86_64_PC32 reloc to a non-function sym in
1317 check_relocs. We can't decide accurately between function and
1318 non-function syms in check-relocs; Objects loaded later in
1319 the link may change h->type. So fix it now. */
1320 h->plt.offset = (bfd_vma) -1;
1322 /* If this is a weak symbol, and there is a real definition, the
1323 processor independent code will have arranged for us to see the
1324 real definition first, and we can just use the same value. */
1325 if (h->u.weakdef != NULL)
1327 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1328 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1329 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1330 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1331 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1332 h->non_got_ref = h->u.weakdef->non_got_ref;
1333 return TRUE;
1336 /* This is a reference to a symbol defined by a dynamic object which
1337 is not a function. */
1339 /* If we are creating a shared library, we must presume that the
1340 only references to the symbol are via the global offset table.
1341 For such cases we need not do anything here; the relocations will
1342 be handled correctly by relocate_section. */
1343 if (info->shared)
1344 return TRUE;
1346 /* If there are no references to this symbol that do not use the
1347 GOT, we don't need to generate a copy reloc. */
1348 if (!h->non_got_ref)
1349 return TRUE;
1351 /* If -z nocopyreloc was given, we won't generate them either. */
1352 if (info->nocopyreloc)
1354 h->non_got_ref = 0;
1355 return TRUE;
1358 if (ELIMINATE_COPY_RELOCS)
1360 struct elf64_x86_64_link_hash_entry * eh;
1361 struct elf64_x86_64_dyn_relocs *p;
1363 eh = (struct elf64_x86_64_link_hash_entry *) h;
1364 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1366 s = p->sec->output_section;
1367 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1368 break;
1371 /* If we didn't find any dynamic relocs in read-only sections, then
1372 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1373 if (p == NULL)
1375 h->non_got_ref = 0;
1376 return TRUE;
1380 if (h->size == 0)
1382 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1383 h->root.root.string);
1384 return TRUE;
1387 /* We must allocate the symbol in our .dynbss section, which will
1388 become part of the .bss section of the executable. There will be
1389 an entry for this symbol in the .dynsym section. The dynamic
1390 object will contain position independent code, so all references
1391 from the dynamic object to this symbol will go through the global
1392 offset table. The dynamic linker will use the .dynsym entry to
1393 determine the address it must put in the global offset table, so
1394 both the dynamic object and the regular object will refer to the
1395 same memory location for the variable. */
1397 htab = elf64_x86_64_hash_table (info);
1399 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1400 to copy the initial value out of the dynamic object and into the
1401 runtime process image. */
1402 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1404 htab->srelbss->size += sizeof (Elf64_External_Rela);
1405 h->needs_copy = 1;
1408 /* We need to figure out the alignment required for this symbol. I
1409 have no idea how ELF linkers handle this. 16-bytes is the size
1410 of the largest type that requires hard alignment -- long double. */
1411 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1412 this construct. */
1413 power_of_two = bfd_log2 (h->size);
1414 if (power_of_two > 4)
1415 power_of_two = 4;
1417 /* Apply the required alignment. */
1418 s = htab->sdynbss;
1419 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1420 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1422 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1423 return FALSE;
1426 /* Define the symbol as being at this point in the section. */
1427 h->root.u.def.section = s;
1428 h->root.u.def.value = s->size;
1430 /* Increment the section size to make room for the symbol. */
1431 s->size += h->size;
1433 return TRUE;
1436 /* Allocate space in .plt, .got and associated reloc sections for
1437 dynamic relocs. */
1439 static bfd_boolean
1440 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1442 struct bfd_link_info *info;
1443 struct elf64_x86_64_link_hash_table *htab;
1444 struct elf64_x86_64_link_hash_entry *eh;
1445 struct elf64_x86_64_dyn_relocs *p;
1447 if (h->root.type == bfd_link_hash_indirect)
1448 return TRUE;
1450 if (h->root.type == bfd_link_hash_warning)
1451 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1453 info = (struct bfd_link_info *) inf;
1454 htab = elf64_x86_64_hash_table (info);
1456 if (htab->elf.dynamic_sections_created
1457 && h->plt.refcount > 0)
1459 /* Make sure this symbol is output as a dynamic symbol.
1460 Undefined weak syms won't yet be marked as dynamic. */
1461 if (h->dynindx == -1
1462 && !h->forced_local)
1464 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1465 return FALSE;
1468 if (info->shared
1469 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1471 asection *s = htab->splt;
1473 /* If this is the first .plt entry, make room for the special
1474 first entry. */
1475 if (s->size == 0)
1476 s->size += PLT_ENTRY_SIZE;
1478 h->plt.offset = s->size;
1480 /* If this symbol is not defined in a regular file, and we are
1481 not generating a shared library, then set the symbol to this
1482 location in the .plt. This is required to make function
1483 pointers compare as equal between the normal executable and
1484 the shared library. */
1485 if (! info->shared
1486 && !h->def_regular)
1488 h->root.u.def.section = s;
1489 h->root.u.def.value = h->plt.offset;
1492 /* Make room for this entry. */
1493 s->size += PLT_ENTRY_SIZE;
1495 /* We also need to make an entry in the .got.plt section, which
1496 will be placed in the .got section by the linker script. */
1497 htab->sgotplt->size += GOT_ENTRY_SIZE;
1499 /* We also need to make an entry in the .rela.plt section. */
1500 htab->srelplt->size += sizeof (Elf64_External_Rela);
1501 htab->srelplt->reloc_count++;
1503 else
1505 h->plt.offset = (bfd_vma) -1;
1506 h->needs_plt = 0;
1509 else
1511 h->plt.offset = (bfd_vma) -1;
1512 h->needs_plt = 0;
1515 eh = (struct elf64_x86_64_link_hash_entry *) h;
1516 eh->tlsdesc_got = (bfd_vma) -1;
1518 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1519 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1520 if (h->got.refcount > 0
1521 && !info->shared
1522 && h->dynindx == -1
1523 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1524 h->got.offset = (bfd_vma) -1;
1525 else if (h->got.refcount > 0)
1527 asection *s;
1528 bfd_boolean dyn;
1529 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1531 /* Make sure this symbol is output as a dynamic symbol.
1532 Undefined weak syms won't yet be marked as dynamic. */
1533 if (h->dynindx == -1
1534 && !h->forced_local)
1536 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1537 return FALSE;
1540 if (GOT_TLS_GDESC_P (tls_type))
1542 eh->tlsdesc_got = htab->sgotplt->size
1543 - elf64_x86_64_compute_jump_table_size (htab);
1544 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1545 h->got.offset = (bfd_vma) -2;
1547 if (! GOT_TLS_GDESC_P (tls_type)
1548 || GOT_TLS_GD_P (tls_type))
1550 s = htab->sgot;
1551 h->got.offset = s->size;
1552 s->size += GOT_ENTRY_SIZE;
1553 if (GOT_TLS_GD_P (tls_type))
1554 s->size += GOT_ENTRY_SIZE;
1556 dyn = htab->elf.dynamic_sections_created;
1557 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1558 and two if global.
1559 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1560 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
1561 || tls_type == GOT_TLS_IE)
1562 htab->srelgot->size += sizeof (Elf64_External_Rela);
1563 else if (GOT_TLS_GD_P (tls_type))
1564 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
1565 else if (! GOT_TLS_GDESC_P (tls_type)
1566 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1567 || h->root.type != bfd_link_hash_undefweak)
1568 && (info->shared
1569 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1570 htab->srelgot->size += sizeof (Elf64_External_Rela);
1571 if (GOT_TLS_GDESC_P (tls_type))
1573 htab->srelplt->size += sizeof (Elf64_External_Rela);
1574 htab->tlsdesc_plt = (bfd_vma) -1;
1577 else
1578 h->got.offset = (bfd_vma) -1;
1580 if (eh->dyn_relocs == NULL)
1581 return TRUE;
1583 /* In the shared -Bsymbolic case, discard space allocated for
1584 dynamic pc-relative relocs against symbols which turn out to be
1585 defined in regular objects. For the normal shared case, discard
1586 space for pc-relative relocs that have become local due to symbol
1587 visibility changes. */
1589 if (info->shared)
1591 /* Relocs that use pc_count are those that appear on a call
1592 insn, or certain REL relocs that can generated via assembly.
1593 We want calls to protected symbols to resolve directly to the
1594 function rather than going via the plt. If people want
1595 function pointer comparisons to work as expected then they
1596 should avoid writing weird assembly. */
1597 if (SYMBOL_CALLS_LOCAL (info, h))
1599 struct elf64_x86_64_dyn_relocs **pp;
1601 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1603 p->count -= p->pc_count;
1604 p->pc_count = 0;
1605 if (p->count == 0)
1606 *pp = p->next;
1607 else
1608 pp = &p->next;
1612 /* Also discard relocs on undefined weak syms with non-default
1613 visibility. */
1614 if (eh->dyn_relocs != NULL
1615 && h->root.type == bfd_link_hash_undefweak)
1617 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1618 eh->dyn_relocs = NULL;
1620 /* Make sure undefined weak symbols are output as a dynamic
1621 symbol in PIEs. */
1622 else if (h->dynindx == -1
1623 && !h->forced_local)
1625 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1626 return FALSE;
1630 else if (ELIMINATE_COPY_RELOCS)
1632 /* For the non-shared case, discard space for relocs against
1633 symbols which turn out to need copy relocs or are not
1634 dynamic. */
1636 if (!h->non_got_ref
1637 && ((h->def_dynamic
1638 && !h->def_regular)
1639 || (htab->elf.dynamic_sections_created
1640 && (h->root.type == bfd_link_hash_undefweak
1641 || h->root.type == bfd_link_hash_undefined))))
1643 /* Make sure this symbol is output as a dynamic symbol.
1644 Undefined weak syms won't yet be marked as dynamic. */
1645 if (h->dynindx == -1
1646 && !h->forced_local)
1648 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1649 return FALSE;
1652 /* If that succeeded, we know we'll be keeping all the
1653 relocs. */
1654 if (h->dynindx != -1)
1655 goto keep;
1658 eh->dyn_relocs = NULL;
1660 keep: ;
1663 /* Finally, allocate space. */
1664 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1666 asection *sreloc = elf_section_data (p->sec)->sreloc;
1667 sreloc->size += p->count * sizeof (Elf64_External_Rela);
1670 return TRUE;
1673 /* Find any dynamic relocs that apply to read-only sections. */
1675 static bfd_boolean
1676 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1678 struct elf64_x86_64_link_hash_entry *eh;
1679 struct elf64_x86_64_dyn_relocs *p;
1681 if (h->root.type == bfd_link_hash_warning)
1682 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1684 eh = (struct elf64_x86_64_link_hash_entry *) h;
1685 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1687 asection *s = p->sec->output_section;
1689 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1691 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1693 info->flags |= DF_TEXTREL;
1695 /* Not an error, just cut short the traversal. */
1696 return FALSE;
1699 return TRUE;
1702 /* Set the sizes of the dynamic sections. */
1704 static bfd_boolean
1705 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1706 struct bfd_link_info *info)
1708 struct elf64_x86_64_link_hash_table *htab;
1709 bfd *dynobj;
1710 asection *s;
1711 bfd_boolean relocs;
1712 bfd *ibfd;
1714 htab = elf64_x86_64_hash_table (info);
1715 dynobj = htab->elf.dynobj;
1716 if (dynobj == NULL)
1717 abort ();
1719 if (htab->elf.dynamic_sections_created)
1721 /* Set the contents of the .interp section to the interpreter. */
1722 if (info->executable)
1724 s = bfd_get_section_by_name (dynobj, ".interp");
1725 if (s == NULL)
1726 abort ();
1727 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1728 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1732 /* Set up .got offsets for local syms, and space for local dynamic
1733 relocs. */
1734 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1736 bfd_signed_vma *local_got;
1737 bfd_signed_vma *end_local_got;
1738 char *local_tls_type;
1739 bfd_vma *local_tlsdesc_gotent;
1740 bfd_size_type locsymcount;
1741 Elf_Internal_Shdr *symtab_hdr;
1742 asection *srel;
1744 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1745 continue;
1747 for (s = ibfd->sections; s != NULL; s = s->next)
1749 struct elf64_x86_64_dyn_relocs *p;
1751 for (p = (struct elf64_x86_64_dyn_relocs *)
1752 (elf_section_data (s)->local_dynrel);
1753 p != NULL;
1754 p = p->next)
1756 if (!bfd_is_abs_section (p->sec)
1757 && bfd_is_abs_section (p->sec->output_section))
1759 /* Input section has been discarded, either because
1760 it is a copy of a linkonce section or due to
1761 linker script /DISCARD/, so we'll be discarding
1762 the relocs too. */
1764 else if (p->count != 0)
1766 srel = elf_section_data (p->sec)->sreloc;
1767 srel->size += p->count * sizeof (Elf64_External_Rela);
1768 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1769 info->flags |= DF_TEXTREL;
1775 local_got = elf_local_got_refcounts (ibfd);
1776 if (!local_got)
1777 continue;
1779 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1780 locsymcount = symtab_hdr->sh_info;
1781 end_local_got = local_got + locsymcount;
1782 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
1783 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
1784 s = htab->sgot;
1785 srel = htab->srelgot;
1786 for (; local_got < end_local_got;
1787 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
1789 *local_tlsdesc_gotent = (bfd_vma) -1;
1790 if (*local_got > 0)
1792 if (GOT_TLS_GDESC_P (*local_tls_type))
1794 *local_tlsdesc_gotent = htab->sgotplt->size
1795 - elf64_x86_64_compute_jump_table_size (htab);
1796 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
1797 *local_got = (bfd_vma) -2;
1799 if (! GOT_TLS_GDESC_P (*local_tls_type)
1800 || GOT_TLS_GD_P (*local_tls_type))
1802 *local_got = s->size;
1803 s->size += GOT_ENTRY_SIZE;
1804 if (GOT_TLS_GD_P (*local_tls_type))
1805 s->size += GOT_ENTRY_SIZE;
1807 if (info->shared
1808 || GOT_TLS_GD_ANY_P (*local_tls_type)
1809 || *local_tls_type == GOT_TLS_IE)
1811 if (GOT_TLS_GDESC_P (*local_tls_type))
1813 htab->srelplt->size += sizeof (Elf64_External_Rela);
1814 htab->tlsdesc_plt = (bfd_vma) -1;
1816 if (! GOT_TLS_GDESC_P (*local_tls_type)
1817 || GOT_TLS_GD_P (*local_tls_type))
1818 srel->size += sizeof (Elf64_External_Rela);
1821 else
1822 *local_got = (bfd_vma) -1;
1826 if (htab->tls_ld_got.refcount > 0)
1828 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1829 relocs. */
1830 htab->tls_ld_got.offset = htab->sgot->size;
1831 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
1832 htab->srelgot->size += sizeof (Elf64_External_Rela);
1834 else
1835 htab->tls_ld_got.offset = -1;
1837 /* Allocate global sym .plt and .got entries, and space for global
1838 sym dynamic relocs. */
1839 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1841 /* For every jump slot reserved in the sgotplt, reloc_count is
1842 incremented. However, when we reserve space for TLS descriptors,
1843 it's not incremented, so in order to compute the space reserved
1844 for them, it suffices to multiply the reloc count by the jump
1845 slot size. */
1846 if (htab->srelplt)
1847 htab->sgotplt_jump_table_size
1848 = elf64_x86_64_compute_jump_table_size (htab);
1850 if (htab->tlsdesc_plt)
1852 /* If we're not using lazy TLS relocations, don't generate the
1853 PLT and GOT entries they require. */
1854 if ((info->flags & DF_BIND_NOW))
1855 htab->tlsdesc_plt = 0;
1856 else
1858 htab->tlsdesc_got = htab->sgot->size;
1859 htab->sgot->size += GOT_ENTRY_SIZE;
1860 /* Reserve room for the initial entry.
1861 FIXME: we could probably do away with it in this case. */
1862 if (htab->splt->size == 0)
1863 htab->splt->size += PLT_ENTRY_SIZE;
1864 htab->tlsdesc_plt = htab->splt->size;
1865 htab->splt->size += PLT_ENTRY_SIZE;
1869 /* We now have determined the sizes of the various dynamic sections.
1870 Allocate memory for them. */
1871 relocs = FALSE;
1872 for (s = dynobj->sections; s != NULL; s = s->next)
1874 if ((s->flags & SEC_LINKER_CREATED) == 0)
1875 continue;
1877 if (s == htab->splt
1878 || s == htab->sgot
1879 || s == htab->sgotplt
1880 || s == htab->sdynbss)
1882 /* Strip this section if we don't need it; see the
1883 comment below. */
1885 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
1887 if (s->size != 0 && s != htab->srelplt)
1888 relocs = TRUE;
1890 /* We use the reloc_count field as a counter if we need
1891 to copy relocs into the output file. */
1892 if (s != htab->srelplt)
1893 s->reloc_count = 0;
1895 else
1897 /* It's not one of our sections, so don't allocate space. */
1898 continue;
1901 if (s->size == 0)
1903 /* If we don't need this section, strip it from the
1904 output file. This is mostly to handle .rela.bss and
1905 .rela.plt. We must create both sections in
1906 create_dynamic_sections, because they must be created
1907 before the linker maps input sections to output
1908 sections. The linker does that before
1909 adjust_dynamic_symbol is called, and it is that
1910 function which decides whether anything needs to go
1911 into these sections. */
1913 s->flags |= SEC_EXCLUDE;
1914 continue;
1917 if ((s->flags & SEC_HAS_CONTENTS) == 0)
1918 continue;
1920 /* Allocate memory for the section contents. We use bfd_zalloc
1921 here in case unused entries are not reclaimed before the
1922 section's contents are written out. This should not happen,
1923 but this way if it does, we get a R_X86_64_NONE reloc instead
1924 of garbage. */
1925 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1926 if (s->contents == NULL)
1927 return FALSE;
1930 if (htab->elf.dynamic_sections_created)
1932 /* Add some entries to the .dynamic section. We fill in the
1933 values later, in elf64_x86_64_finish_dynamic_sections, but we
1934 must add the entries now so that we get the correct size for
1935 the .dynamic section. The DT_DEBUG entry is filled in by the
1936 dynamic linker and used by the debugger. */
1937 #define add_dynamic_entry(TAG, VAL) \
1938 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1940 if (info->executable)
1942 if (!add_dynamic_entry (DT_DEBUG, 0))
1943 return FALSE;
1946 if (htab->splt->size != 0)
1948 if (!add_dynamic_entry (DT_PLTGOT, 0)
1949 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1950 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1951 || !add_dynamic_entry (DT_JMPREL, 0))
1952 return FALSE;
1954 if (htab->tlsdesc_plt
1955 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
1956 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
1957 return FALSE;
1960 if (relocs)
1962 if (!add_dynamic_entry (DT_RELA, 0)
1963 || !add_dynamic_entry (DT_RELASZ, 0)
1964 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1965 return FALSE;
1967 /* If any dynamic relocs apply to a read-only section,
1968 then we need a DT_TEXTREL entry. */
1969 if ((info->flags & DF_TEXTREL) == 0)
1970 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1971 (PTR) info);
1973 if ((info->flags & DF_TEXTREL) != 0)
1975 if (!add_dynamic_entry (DT_TEXTREL, 0))
1976 return FALSE;
1980 #undef add_dynamic_entry
1982 return TRUE;
1985 static bfd_boolean
1986 elf64_x86_64_always_size_sections (bfd *output_bfd,
1987 struct bfd_link_info *info)
1989 asection *tls_sec = elf_hash_table (info)->tls_sec;
1991 if (tls_sec)
1993 struct elf_link_hash_entry *tlsbase;
1995 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
1996 "_TLS_MODULE_BASE_",
1997 FALSE, FALSE, FALSE);
1999 if (tlsbase && tlsbase->type == STT_TLS)
2001 struct bfd_link_hash_entry *bh = NULL;
2002 const struct elf_backend_data *bed
2003 = get_elf_backend_data (output_bfd);
2005 if (!(_bfd_generic_link_add_one_symbol
2006 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2007 tls_sec, 0, NULL, FALSE,
2008 bed->collect, &bh)))
2009 return FALSE;
2010 tlsbase = (struct elf_link_hash_entry *)bh;
2011 tlsbase->def_regular = 1;
2012 tlsbase->other = STV_HIDDEN;
2013 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2017 return TRUE;
2020 /* Return the base VMA address which should be subtracted from real addresses
2021 when resolving @dtpoff relocation.
2022 This is PT_TLS segment p_vaddr. */
2024 static bfd_vma
2025 dtpoff_base (struct bfd_link_info *info)
2027 /* If tls_sec is NULL, we should have signalled an error already. */
2028 if (elf_hash_table (info)->tls_sec == NULL)
2029 return 0;
2030 return elf_hash_table (info)->tls_sec->vma;
2033 /* Return the relocation value for @tpoff relocation
2034 if STT_TLS virtual address is ADDRESS. */
2036 static bfd_vma
2037 tpoff (struct bfd_link_info *info, bfd_vma address)
2039 struct elf_link_hash_table *htab = elf_hash_table (info);
2041 /* If tls_segment is NULL, we should have signalled an error already. */
2042 if (htab->tls_sec == NULL)
2043 return 0;
2044 return address - htab->tls_size - htab->tls_sec->vma;
2047 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2048 branch? */
2050 static bfd_boolean
2051 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2053 /* Opcode Instruction
2054 0xe8 call
2055 0xe9 jump
2056 0x0f 0x8x conditional jump */
2057 return ((offset > 0
2058 && (contents [offset - 1] == 0xe8
2059 || contents [offset - 1] == 0xe9))
2060 || (offset > 1
2061 && contents [offset - 2] == 0x0f
2062 && (contents [offset - 1] & 0xf0) == 0x80));
2065 /* Relocate an x86_64 ELF section. */
2067 static bfd_boolean
2068 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2069 bfd *input_bfd, asection *input_section,
2070 bfd_byte *contents, Elf_Internal_Rela *relocs,
2071 Elf_Internal_Sym *local_syms,
2072 asection **local_sections)
2074 struct elf64_x86_64_link_hash_table *htab;
2075 Elf_Internal_Shdr *symtab_hdr;
2076 struct elf_link_hash_entry **sym_hashes;
2077 bfd_vma *local_got_offsets;
2078 bfd_vma *local_tlsdesc_gotents;
2079 Elf_Internal_Rela *rel;
2080 Elf_Internal_Rela *relend;
2082 htab = elf64_x86_64_hash_table (info);
2083 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2084 sym_hashes = elf_sym_hashes (input_bfd);
2085 local_got_offsets = elf_local_got_offsets (input_bfd);
2086 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2088 rel = relocs;
2089 relend = relocs + input_section->reloc_count;
2090 for (; rel < relend; rel++)
2092 unsigned int r_type;
2093 reloc_howto_type *howto;
2094 unsigned long r_symndx;
2095 struct elf_link_hash_entry *h;
2096 Elf_Internal_Sym *sym;
2097 asection *sec;
2098 bfd_vma off, offplt;
2099 bfd_vma relocation;
2100 bfd_boolean unresolved_reloc;
2101 bfd_reloc_status_type r;
2102 int tls_type;
2104 r_type = ELF64_R_TYPE (rel->r_info);
2105 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2106 || r_type == (int) R_X86_64_GNU_VTENTRY)
2107 continue;
2109 if (r_type >= R_X86_64_max)
2111 bfd_set_error (bfd_error_bad_value);
2112 return FALSE;
2115 howto = x86_64_elf_howto_table + r_type;
2116 r_symndx = ELF64_R_SYM (rel->r_info);
2117 h = NULL;
2118 sym = NULL;
2119 sec = NULL;
2120 unresolved_reloc = FALSE;
2121 if (r_symndx < symtab_hdr->sh_info)
2123 sym = local_syms + r_symndx;
2124 sec = local_sections[r_symndx];
2126 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2128 else
2130 bfd_boolean warned;
2132 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2133 r_symndx, symtab_hdr, sym_hashes,
2134 h, sec, relocation,
2135 unresolved_reloc, warned);
2138 if (sec != NULL && elf_discarded_section (sec))
2140 /* For relocs against symbols from removed linkonce sections,
2141 or sections discarded by a linker script, we just want the
2142 section contents zeroed. Avoid any special processing. */
2143 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2144 rel->r_info = 0;
2145 rel->r_addend = 0;
2146 continue;
2149 if (info->relocatable)
2150 continue;
2152 /* When generating a shared object, the relocations handled here are
2153 copied into the output file to be resolved at run time. */
2154 switch (r_type)
2156 asection *base_got;
2157 case R_X86_64_GOT32:
2158 case R_X86_64_GOT64:
2159 /* Relocation is to the entry for this symbol in the global
2160 offset table. */
2161 case R_X86_64_GOTPCREL:
2162 case R_X86_64_GOTPCREL64:
2163 /* Use global offset table entry as symbol value. */
2164 case R_X86_64_GOTPLT64:
2165 /* This is the same as GOT64 for relocation purposes, but
2166 indicates the existence of a PLT entry. The difficulty is,
2167 that we must calculate the GOT slot offset from the PLT
2168 offset, if this symbol got a PLT entry (it was global).
2169 Additionally if it's computed from the PLT entry, then that
2170 GOT offset is relative to .got.plt, not to .got. */
2171 base_got = htab->sgot;
2173 if (htab->sgot == NULL)
2174 abort ();
2176 if (h != NULL)
2178 bfd_boolean dyn;
2180 off = h->got.offset;
2181 if (h->needs_plt
2182 && h->plt.offset != (bfd_vma)-1
2183 && off == (bfd_vma)-1)
2185 /* We can't use h->got.offset here to save
2186 state, or even just remember the offset, as
2187 finish_dynamic_symbol would use that as offset into
2188 .got. */
2189 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2190 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2191 base_got = htab->sgotplt;
2194 dyn = htab->elf.dynamic_sections_created;
2196 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2197 || (info->shared
2198 && SYMBOL_REFERENCES_LOCAL (info, h))
2199 || (ELF_ST_VISIBILITY (h->other)
2200 && h->root.type == bfd_link_hash_undefweak))
2202 /* This is actually a static link, or it is a -Bsymbolic
2203 link and the symbol is defined locally, or the symbol
2204 was forced to be local because of a version file. We
2205 must initialize this entry in the global offset table.
2206 Since the offset must always be a multiple of 8, we
2207 use the least significant bit to record whether we
2208 have initialized it already.
2210 When doing a dynamic link, we create a .rela.got
2211 relocation entry to initialize the value. This is
2212 done in the finish_dynamic_symbol routine. */
2213 if ((off & 1) != 0)
2214 off &= ~1;
2215 else
2217 bfd_put_64 (output_bfd, relocation,
2218 base_got->contents + off);
2219 /* Note that this is harmless for the GOTPLT64 case,
2220 as -1 | 1 still is -1. */
2221 h->got.offset |= 1;
2224 else
2225 unresolved_reloc = FALSE;
2227 else
2229 if (local_got_offsets == NULL)
2230 abort ();
2232 off = local_got_offsets[r_symndx];
2234 /* The offset must always be a multiple of 8. We use
2235 the least significant bit to record whether we have
2236 already generated the necessary reloc. */
2237 if ((off & 1) != 0)
2238 off &= ~1;
2239 else
2241 bfd_put_64 (output_bfd, relocation,
2242 base_got->contents + off);
2244 if (info->shared)
2246 asection *s;
2247 Elf_Internal_Rela outrel;
2248 bfd_byte *loc;
2250 /* We need to generate a R_X86_64_RELATIVE reloc
2251 for the dynamic linker. */
2252 s = htab->srelgot;
2253 if (s == NULL)
2254 abort ();
2256 outrel.r_offset = (base_got->output_section->vma
2257 + base_got->output_offset
2258 + off);
2259 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2260 outrel.r_addend = relocation;
2261 loc = s->contents;
2262 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2263 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2266 local_got_offsets[r_symndx] |= 1;
2270 if (off >= (bfd_vma) -2)
2271 abort ();
2273 relocation = base_got->output_section->vma
2274 + base_got->output_offset + off;
2275 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
2276 relocation -= htab->sgotplt->output_section->vma
2277 - htab->sgotplt->output_offset;
2279 break;
2281 case R_X86_64_GOTOFF64:
2282 /* Relocation is relative to the start of the global offset
2283 table. */
2285 /* Check to make sure it isn't a protected function symbol
2286 for shared library since it may not be local when used
2287 as function address. */
2288 if (info->shared
2289 && h
2290 && h->def_regular
2291 && h->type == STT_FUNC
2292 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2294 (*_bfd_error_handler)
2295 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2296 input_bfd, h->root.root.string);
2297 bfd_set_error (bfd_error_bad_value);
2298 return FALSE;
2301 /* Note that sgot is not involved in this
2302 calculation. We always want the start of .got.plt. If we
2303 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2304 permitted by the ABI, we might have to change this
2305 calculation. */
2306 relocation -= htab->sgotplt->output_section->vma
2307 + htab->sgotplt->output_offset;
2308 break;
2310 case R_X86_64_GOTPC32:
2311 case R_X86_64_GOTPC64:
2312 /* Use global offset table as symbol value. */
2313 relocation = htab->sgotplt->output_section->vma
2314 + htab->sgotplt->output_offset;
2315 unresolved_reloc = FALSE;
2316 break;
2318 case R_X86_64_PLTOFF64:
2319 /* Relocation is PLT entry relative to GOT. For local
2320 symbols it's the symbol itself relative to GOT. */
2321 if (h != NULL
2322 /* See PLT32 handling. */
2323 && h->plt.offset != (bfd_vma) -1
2324 && htab->splt != NULL)
2326 relocation = (htab->splt->output_section->vma
2327 + htab->splt->output_offset
2328 + h->plt.offset);
2329 unresolved_reloc = FALSE;
2332 relocation -= htab->sgotplt->output_section->vma
2333 + htab->sgotplt->output_offset;
2334 break;
2336 case R_X86_64_PLT32:
2337 /* Relocation is to the entry for this symbol in the
2338 procedure linkage table. */
2340 /* Resolve a PLT32 reloc against a local symbol directly,
2341 without using the procedure linkage table. */
2342 if (h == NULL)
2343 break;
2345 if (h->plt.offset == (bfd_vma) -1
2346 || htab->splt == NULL)
2348 /* We didn't make a PLT entry for this symbol. This
2349 happens when statically linking PIC code, or when
2350 using -Bsymbolic. */
2351 break;
2354 relocation = (htab->splt->output_section->vma
2355 + htab->splt->output_offset
2356 + h->plt.offset);
2357 unresolved_reloc = FALSE;
2358 break;
2360 case R_X86_64_PC8:
2361 case R_X86_64_PC16:
2362 case R_X86_64_PC32:
2363 if (info->shared
2364 && !SYMBOL_REFERENCES_LOCAL (info, h)
2365 && (input_section->flags & SEC_ALLOC) != 0
2366 && (input_section->flags & SEC_READONLY) != 0
2367 && (!h->def_regular
2368 || r_type != R_X86_64_PC32
2369 || h->type != STT_FUNC
2370 || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
2371 || !is_32bit_relative_branch (contents,
2372 rel->r_offset)))
2374 if (h->def_regular
2375 && r_type == R_X86_64_PC32
2376 && h->type == STT_FUNC
2377 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2378 (*_bfd_error_handler)
2379 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
2380 input_bfd, h->root.root.string);
2381 else
2382 (*_bfd_error_handler)
2383 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
2384 input_bfd, x86_64_elf_howto_table[r_type].name,
2385 h->root.root.string);
2386 bfd_set_error (bfd_error_bad_value);
2387 return FALSE;
2389 /* Fall through. */
2391 case R_X86_64_8:
2392 case R_X86_64_16:
2393 case R_X86_64_32:
2394 case R_X86_64_PC64:
2395 case R_X86_64_64:
2396 /* FIXME: The ABI says the linker should make sure the value is
2397 the same when it's zeroextended to 64 bit. */
2399 if ((input_section->flags & SEC_ALLOC) == 0)
2400 break;
2402 if ((info->shared
2403 && (h == NULL
2404 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2405 || h->root.type != bfd_link_hash_undefweak)
2406 && ((r_type != R_X86_64_PC8
2407 && r_type != R_X86_64_PC16
2408 && r_type != R_X86_64_PC32
2409 && r_type != R_X86_64_PC64)
2410 || !SYMBOL_CALLS_LOCAL (info, h)))
2411 || (ELIMINATE_COPY_RELOCS
2412 && !info->shared
2413 && h != NULL
2414 && h->dynindx != -1
2415 && !h->non_got_ref
2416 && ((h->def_dynamic
2417 && !h->def_regular)
2418 || h->root.type == bfd_link_hash_undefweak
2419 || h->root.type == bfd_link_hash_undefined)))
2421 Elf_Internal_Rela outrel;
2422 bfd_byte *loc;
2423 bfd_boolean skip, relocate;
2424 asection *sreloc;
2426 /* When generating a shared object, these relocations
2427 are copied into the output file to be resolved at run
2428 time. */
2429 skip = FALSE;
2430 relocate = FALSE;
2432 outrel.r_offset =
2433 _bfd_elf_section_offset (output_bfd, info, input_section,
2434 rel->r_offset);
2435 if (outrel.r_offset == (bfd_vma) -1)
2436 skip = TRUE;
2437 else if (outrel.r_offset == (bfd_vma) -2)
2438 skip = TRUE, relocate = TRUE;
2440 outrel.r_offset += (input_section->output_section->vma
2441 + input_section->output_offset);
2443 if (skip)
2444 memset (&outrel, 0, sizeof outrel);
2446 /* h->dynindx may be -1 if this symbol was marked to
2447 become local. */
2448 else if (h != NULL
2449 && h->dynindx != -1
2450 && (r_type == R_X86_64_PC8
2451 || r_type == R_X86_64_PC16
2452 || r_type == R_X86_64_PC32
2453 || r_type == R_X86_64_PC64
2454 || !info->shared
2455 || !SYMBOLIC_BIND (info, h)
2456 || !h->def_regular))
2458 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2459 outrel.r_addend = rel->r_addend;
2461 else
2463 /* This symbol is local, or marked to become local. */
2464 if (r_type == R_X86_64_64)
2466 relocate = TRUE;
2467 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2468 outrel.r_addend = relocation + rel->r_addend;
2470 else
2472 long sindx;
2474 if (bfd_is_abs_section (sec))
2475 sindx = 0;
2476 else if (sec == NULL || sec->owner == NULL)
2478 bfd_set_error (bfd_error_bad_value);
2479 return FALSE;
2481 else
2483 asection *osec;
2485 /* We are turning this relocation into one
2486 against a section symbol. It would be
2487 proper to subtract the symbol's value,
2488 osec->vma, from the emitted reloc addend,
2489 but ld.so expects buggy relocs. */
2490 osec = sec->output_section;
2491 sindx = elf_section_data (osec)->dynindx;
2492 if (sindx == 0)
2494 asection *oi = htab->elf.text_index_section;
2495 sindx = elf_section_data (oi)->dynindx;
2497 BFD_ASSERT (sindx != 0);
2500 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2501 outrel.r_addend = relocation + rel->r_addend;
2505 sreloc = elf_section_data (input_section)->sreloc;
2506 if (sreloc == NULL)
2507 abort ();
2509 loc = sreloc->contents;
2510 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2511 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2513 /* If this reloc is against an external symbol, we do
2514 not want to fiddle with the addend. Otherwise, we
2515 need to include the symbol value so that it becomes
2516 an addend for the dynamic reloc. */
2517 if (! relocate)
2518 continue;
2521 break;
2523 case R_X86_64_TLSGD:
2524 case R_X86_64_GOTPC32_TLSDESC:
2525 case R_X86_64_TLSDESC_CALL:
2526 case R_X86_64_GOTTPOFF:
2527 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
2528 tls_type = GOT_UNKNOWN;
2529 if (h == NULL && local_got_offsets)
2530 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2531 else if (h != NULL)
2533 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2534 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2535 r_type = R_X86_64_TPOFF32;
2537 if (r_type == R_X86_64_TLSGD
2538 || r_type == R_X86_64_GOTPC32_TLSDESC
2539 || r_type == R_X86_64_TLSDESC_CALL)
2541 if (tls_type == GOT_TLS_IE)
2542 r_type = R_X86_64_GOTTPOFF;
2545 if (r_type == R_X86_64_TPOFF32)
2547 BFD_ASSERT (! unresolved_reloc);
2548 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2550 unsigned int i;
2551 static unsigned char tlsgd[8]
2552 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2554 /* GD->LE transition.
2555 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2556 .word 0x6666; rex64; call __tls_get_addr@plt
2557 Change it into:
2558 movq %fs:0, %rax
2559 leaq foo@tpoff(%rax), %rax */
2560 BFD_ASSERT (rel->r_offset >= 4);
2561 for (i = 0; i < 4; i++)
2562 BFD_ASSERT (bfd_get_8 (input_bfd,
2563 contents + rel->r_offset - 4 + i)
2564 == tlsgd[i]);
2565 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2566 for (i = 0; i < 4; i++)
2567 BFD_ASSERT (bfd_get_8 (input_bfd,
2568 contents + rel->r_offset + 4 + i)
2569 == tlsgd[i+4]);
2570 BFD_ASSERT (rel + 1 < relend);
2571 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2572 memcpy (contents + rel->r_offset - 4,
2573 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2574 16);
2575 bfd_put_32 (output_bfd, tpoff (info, relocation),
2576 contents + rel->r_offset + 8);
2577 /* Skip R_X86_64_PLT32. */
2578 rel++;
2579 continue;
2581 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2583 /* GDesc -> LE transition.
2584 It's originally something like:
2585 leaq x@tlsdesc(%rip), %rax
2587 Change it to:
2588 movl $x@tpoff, %rax
2590 Registers other than %rax may be set up here. */
2592 unsigned int val, type, type2;
2593 bfd_vma roff;
2595 /* First, make sure it's a leaq adding rip to a
2596 32-bit offset into any register, although it's
2597 probably almost always going to be rax. */
2598 roff = rel->r_offset;
2599 BFD_ASSERT (roff >= 3);
2600 type = bfd_get_8 (input_bfd, contents + roff - 3);
2601 BFD_ASSERT ((type & 0xfb) == 0x48);
2602 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2603 BFD_ASSERT (type2 == 0x8d);
2604 val = bfd_get_8 (input_bfd, contents + roff - 1);
2605 BFD_ASSERT ((val & 0xc7) == 0x05);
2606 BFD_ASSERT (roff + 4 <= input_section->size);
2608 /* Now modify the instruction as appropriate. */
2609 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
2610 contents + roff - 3);
2611 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
2612 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
2613 contents + roff - 1);
2614 bfd_put_32 (output_bfd, tpoff (info, relocation),
2615 contents + roff);
2616 continue;
2618 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2620 /* GDesc -> LE transition.
2621 It's originally:
2622 call *(%rax)
2623 Turn it into:
2624 nop; nop. */
2626 unsigned int val, type;
2627 bfd_vma roff;
2629 /* First, make sure it's a call *(%rax). */
2630 roff = rel->r_offset;
2631 BFD_ASSERT (roff + 2 <= input_section->size);
2632 type = bfd_get_8 (input_bfd, contents + roff);
2633 BFD_ASSERT (type == 0xff);
2634 val = bfd_get_8 (input_bfd, contents + roff + 1);
2635 BFD_ASSERT (val == 0x10);
2637 /* Now modify the instruction as appropriate. Use
2638 xchg %ax,%ax instead of 2 nops. */
2639 bfd_put_8 (output_bfd, 0x66, contents + roff);
2640 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2641 continue;
2643 else
2645 unsigned int val, type, reg;
2647 /* IE->LE transition:
2648 Originally it can be one of:
2649 movq foo@gottpoff(%rip), %reg
2650 addq foo@gottpoff(%rip), %reg
2651 We change it into:
2652 movq $foo, %reg
2653 leaq foo(%reg), %reg
2654 addq $foo, %reg. */
2655 BFD_ASSERT (rel->r_offset >= 3);
2656 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2657 BFD_ASSERT (val == 0x48 || val == 0x4c);
2658 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2659 BFD_ASSERT (type == 0x8b || type == 0x03);
2660 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2661 BFD_ASSERT ((reg & 0xc7) == 5);
2662 reg >>= 3;
2663 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2664 if (type == 0x8b)
2666 /* movq */
2667 if (val == 0x4c)
2668 bfd_put_8 (output_bfd, 0x49,
2669 contents + rel->r_offset - 3);
2670 bfd_put_8 (output_bfd, 0xc7,
2671 contents + rel->r_offset - 2);
2672 bfd_put_8 (output_bfd, 0xc0 | reg,
2673 contents + rel->r_offset - 1);
2675 else if (reg == 4)
2677 /* addq -> addq - addressing with %rsp/%r12 is
2678 special */
2679 if (val == 0x4c)
2680 bfd_put_8 (output_bfd, 0x49,
2681 contents + rel->r_offset - 3);
2682 bfd_put_8 (output_bfd, 0x81,
2683 contents + rel->r_offset - 2);
2684 bfd_put_8 (output_bfd, 0xc0 | reg,
2685 contents + rel->r_offset - 1);
2687 else
2689 /* addq -> leaq */
2690 if (val == 0x4c)
2691 bfd_put_8 (output_bfd, 0x4d,
2692 contents + rel->r_offset - 3);
2693 bfd_put_8 (output_bfd, 0x8d,
2694 contents + rel->r_offset - 2);
2695 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2696 contents + rel->r_offset - 1);
2698 bfd_put_32 (output_bfd, tpoff (info, relocation),
2699 contents + rel->r_offset);
2700 continue;
2704 if (htab->sgot == NULL)
2705 abort ();
2707 if (h != NULL)
2709 off = h->got.offset;
2710 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
2712 else
2714 if (local_got_offsets == NULL)
2715 abort ();
2717 off = local_got_offsets[r_symndx];
2718 offplt = local_tlsdesc_gotents[r_symndx];
2721 if ((off & 1) != 0)
2722 off &= ~1;
2723 else
2725 Elf_Internal_Rela outrel;
2726 bfd_byte *loc;
2727 int dr_type, indx;
2728 asection *sreloc;
2730 if (htab->srelgot == NULL)
2731 abort ();
2733 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2735 if (GOT_TLS_GDESC_P (tls_type))
2737 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
2738 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
2739 + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
2740 outrel.r_offset = (htab->sgotplt->output_section->vma
2741 + htab->sgotplt->output_offset
2742 + offplt
2743 + htab->sgotplt_jump_table_size);
2744 sreloc = htab->srelplt;
2745 loc = sreloc->contents;
2746 loc += sreloc->reloc_count++
2747 * sizeof (Elf64_External_Rela);
2748 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2749 <= sreloc->contents + sreloc->size);
2750 if (indx == 0)
2751 outrel.r_addend = relocation - dtpoff_base (info);
2752 else
2753 outrel.r_addend = 0;
2754 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2757 sreloc = htab->srelgot;
2759 outrel.r_offset = (htab->sgot->output_section->vma
2760 + htab->sgot->output_offset + off);
2762 if (GOT_TLS_GD_P (tls_type))
2763 dr_type = R_X86_64_DTPMOD64;
2764 else if (GOT_TLS_GDESC_P (tls_type))
2765 goto dr_done;
2766 else
2767 dr_type = R_X86_64_TPOFF64;
2769 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2770 outrel.r_addend = 0;
2771 if ((dr_type == R_X86_64_TPOFF64
2772 || dr_type == R_X86_64_TLSDESC) && indx == 0)
2773 outrel.r_addend = relocation - dtpoff_base (info);
2774 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2776 loc = sreloc->contents;
2777 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2778 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2779 <= sreloc->contents + sreloc->size);
2780 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2782 if (GOT_TLS_GD_P (tls_type))
2784 if (indx == 0)
2786 BFD_ASSERT (! unresolved_reloc);
2787 bfd_put_64 (output_bfd,
2788 relocation - dtpoff_base (info),
2789 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2791 else
2793 bfd_put_64 (output_bfd, 0,
2794 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2795 outrel.r_info = ELF64_R_INFO (indx,
2796 R_X86_64_DTPOFF64);
2797 outrel.r_offset += GOT_ENTRY_SIZE;
2798 sreloc->reloc_count++;
2799 loc += sizeof (Elf64_External_Rela);
2800 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
2801 <= sreloc->contents + sreloc->size);
2802 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2806 dr_done:
2807 if (h != NULL)
2808 h->got.offset |= 1;
2809 else
2810 local_got_offsets[r_symndx] |= 1;
2813 if (off >= (bfd_vma) -2
2814 && ! GOT_TLS_GDESC_P (tls_type))
2815 abort ();
2816 if (r_type == ELF64_R_TYPE (rel->r_info))
2818 if (r_type == R_X86_64_GOTPC32_TLSDESC
2819 || r_type == R_X86_64_TLSDESC_CALL)
2820 relocation = htab->sgotplt->output_section->vma
2821 + htab->sgotplt->output_offset
2822 + offplt + htab->sgotplt_jump_table_size;
2823 else
2824 relocation = htab->sgot->output_section->vma
2825 + htab->sgot->output_offset + off;
2826 unresolved_reloc = FALSE;
2828 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2830 unsigned int i;
2831 static unsigned char tlsgd[8]
2832 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2834 /* GD->IE transition.
2835 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2836 .word 0x6666; rex64; call __tls_get_addr@plt
2837 Change it into:
2838 movq %fs:0, %rax
2839 addq foo@gottpoff(%rip), %rax */
2840 BFD_ASSERT (rel->r_offset >= 4);
2841 for (i = 0; i < 4; i++)
2842 BFD_ASSERT (bfd_get_8 (input_bfd,
2843 contents + rel->r_offset - 4 + i)
2844 == tlsgd[i]);
2845 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2846 for (i = 0; i < 4; i++)
2847 BFD_ASSERT (bfd_get_8 (input_bfd,
2848 contents + rel->r_offset + 4 + i)
2849 == tlsgd[i+4]);
2850 BFD_ASSERT (rel + 1 < relend);
2851 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2852 memcpy (contents + rel->r_offset - 4,
2853 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2854 16);
2856 relocation = (htab->sgot->output_section->vma
2857 + htab->sgot->output_offset + off
2858 - rel->r_offset
2859 - input_section->output_section->vma
2860 - input_section->output_offset
2861 - 12);
2862 bfd_put_32 (output_bfd, relocation,
2863 contents + rel->r_offset + 8);
2864 /* Skip R_X86_64_PLT32. */
2865 rel++;
2866 continue;
2868 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
2870 /* GDesc -> IE transition.
2871 It's originally something like:
2872 leaq x@tlsdesc(%rip), %rax
2874 Change it to:
2875 movq x@gottpoff(%rip), %rax # before nop; nop
2877 Registers other than %rax may be set up here. */
2879 unsigned int val, type, type2;
2880 bfd_vma roff;
2882 /* First, make sure it's a leaq adding rip to a 32-bit
2883 offset into any register, although it's probably
2884 almost always going to be rax. */
2885 roff = rel->r_offset;
2886 BFD_ASSERT (roff >= 3);
2887 type = bfd_get_8 (input_bfd, contents + roff - 3);
2888 BFD_ASSERT ((type & 0xfb) == 0x48);
2889 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
2890 BFD_ASSERT (type2 == 0x8d);
2891 val = bfd_get_8 (input_bfd, contents + roff - 1);
2892 BFD_ASSERT ((val & 0xc7) == 0x05);
2893 BFD_ASSERT (roff + 4 <= input_section->size);
2895 /* Now modify the instruction as appropriate. */
2896 /* To turn a leaq into a movq in the form we use it, it
2897 suffices to change the second byte from 0x8d to
2898 0x8b. */
2899 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
2901 bfd_put_32 (output_bfd,
2902 htab->sgot->output_section->vma
2903 + htab->sgot->output_offset + off
2904 - rel->r_offset
2905 - input_section->output_section->vma
2906 - input_section->output_offset
2907 - 4,
2908 contents + roff);
2909 continue;
2911 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
2913 /* GDesc -> IE transition.
2914 It's originally:
2915 call *(%rax)
2917 Change it to:
2918 nop; nop. */
2920 unsigned int val, type;
2921 bfd_vma roff;
2923 /* First, make sure it's a call *(%eax). */
2924 roff = rel->r_offset;
2925 BFD_ASSERT (roff + 2 <= input_section->size);
2926 type = bfd_get_8 (input_bfd, contents + roff);
2927 BFD_ASSERT (type == 0xff);
2928 val = bfd_get_8 (input_bfd, contents + roff + 1);
2929 BFD_ASSERT (val == 0x10);
2931 /* Now modify the instruction as appropriate. Use
2932 xchg %ax,%ax instead of 2 nops. */
2933 bfd_put_8 (output_bfd, 0x66, contents + roff);
2934 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
2936 continue;
2938 else
2939 BFD_ASSERT (FALSE);
2940 break;
2942 case R_X86_64_TLSLD:
2943 if (! info->shared)
2945 /* LD->LE transition:
2946 Ensure it is:
2947 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2948 We change it into:
2949 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2950 BFD_ASSERT (rel->r_offset >= 3);
2951 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2952 == 0x48);
2953 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2954 == 0x8d);
2955 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2956 == 0x3d);
2957 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2958 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2959 == 0xe8);
2960 BFD_ASSERT (rel + 1 < relend);
2961 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2962 memcpy (contents + rel->r_offset - 3,
2963 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2964 /* Skip R_X86_64_PLT32. */
2965 rel++;
2966 continue;
2969 if (htab->sgot == NULL)
2970 abort ();
2972 off = htab->tls_ld_got.offset;
2973 if (off & 1)
2974 off &= ~1;
2975 else
2977 Elf_Internal_Rela outrel;
2978 bfd_byte *loc;
2980 if (htab->srelgot == NULL)
2981 abort ();
2983 outrel.r_offset = (htab->sgot->output_section->vma
2984 + htab->sgot->output_offset + off);
2986 bfd_put_64 (output_bfd, 0,
2987 htab->sgot->contents + off);
2988 bfd_put_64 (output_bfd, 0,
2989 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2990 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2991 outrel.r_addend = 0;
2992 loc = htab->srelgot->contents;
2993 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2994 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2995 htab->tls_ld_got.offset |= 1;
2997 relocation = htab->sgot->output_section->vma
2998 + htab->sgot->output_offset + off;
2999 unresolved_reloc = FALSE;
3000 break;
3002 case R_X86_64_DTPOFF32:
3003 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3004 relocation -= dtpoff_base (info);
3005 else
3006 relocation = tpoff (info, relocation);
3007 break;
3009 case R_X86_64_TPOFF32:
3010 BFD_ASSERT (! info->shared);
3011 relocation = tpoff (info, relocation);
3012 break;
3014 default:
3015 break;
3018 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3019 because such sections are not SEC_ALLOC and thus ld.so will
3020 not process them. */
3021 if (unresolved_reloc
3022 && !((input_section->flags & SEC_DEBUGGING) != 0
3023 && h->def_dynamic))
3024 (*_bfd_error_handler)
3025 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3026 input_bfd,
3027 input_section,
3028 (long) rel->r_offset,
3029 howto->name,
3030 h->root.root.string);
3032 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3033 contents, rel->r_offset,
3034 relocation, rel->r_addend);
3036 if (r != bfd_reloc_ok)
3038 const char *name;
3040 if (h != NULL)
3041 name = h->root.root.string;
3042 else
3044 name = bfd_elf_string_from_elf_section (input_bfd,
3045 symtab_hdr->sh_link,
3046 sym->st_name);
3047 if (name == NULL)
3048 return FALSE;
3049 if (*name == '\0')
3050 name = bfd_section_name (input_bfd, sec);
3053 if (r == bfd_reloc_overflow)
3055 if (! ((*info->callbacks->reloc_overflow)
3056 (info, (h ? &h->root : NULL), name, howto->name,
3057 (bfd_vma) 0, input_bfd, input_section,
3058 rel->r_offset)))
3059 return FALSE;
3061 else
3063 (*_bfd_error_handler)
3064 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3065 input_bfd, input_section,
3066 (long) rel->r_offset, name, (int) r);
3067 return FALSE;
3072 return TRUE;
3075 /* Finish up dynamic symbol handling. We set the contents of various
3076 dynamic sections here. */
3078 static bfd_boolean
3079 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3080 struct bfd_link_info *info,
3081 struct elf_link_hash_entry *h,
3082 Elf_Internal_Sym *sym)
3084 struct elf64_x86_64_link_hash_table *htab;
3086 htab = elf64_x86_64_hash_table (info);
3088 if (h->plt.offset != (bfd_vma) -1)
3090 bfd_vma plt_index;
3091 bfd_vma got_offset;
3092 Elf_Internal_Rela rela;
3093 bfd_byte *loc;
3095 /* This symbol has an entry in the procedure linkage table. Set
3096 it up. */
3097 if (h->dynindx == -1
3098 || htab->splt == NULL
3099 || htab->sgotplt == NULL
3100 || htab->srelplt == NULL)
3101 abort ();
3103 /* Get the index in the procedure linkage table which
3104 corresponds to this symbol. This is the index of this symbol
3105 in all the symbols for which we are making plt entries. The
3106 first entry in the procedure linkage table is reserved. */
3107 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3109 /* Get the offset into the .got table of the entry that
3110 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3111 bytes. The first three are reserved for the dynamic linker. */
3112 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3114 /* Fill in the entry in the procedure linkage table. */
3115 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3116 PLT_ENTRY_SIZE);
3118 /* Insert the relocation positions of the plt section. The magic
3119 numbers at the end of the statements are the positions of the
3120 relocations in the plt section. */
3121 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3122 instruction uses 6 bytes, subtract this value. */
3123 bfd_put_32 (output_bfd,
3124 (htab->sgotplt->output_section->vma
3125 + htab->sgotplt->output_offset
3126 + got_offset
3127 - htab->splt->output_section->vma
3128 - htab->splt->output_offset
3129 - h->plt.offset
3130 - 6),
3131 htab->splt->contents + h->plt.offset + 2);
3132 /* Put relocation index. */
3133 bfd_put_32 (output_bfd, plt_index,
3134 htab->splt->contents + h->plt.offset + 7);
3135 /* Put offset for jmp .PLT0. */
3136 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3137 htab->splt->contents + h->plt.offset + 12);
3139 /* Fill in the entry in the global offset table, initially this
3140 points to the pushq instruction in the PLT which is at offset 6. */
3141 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
3142 + htab->splt->output_offset
3143 + h->plt.offset + 6),
3144 htab->sgotplt->contents + got_offset);
3146 /* Fill in the entry in the .rela.plt section. */
3147 rela.r_offset = (htab->sgotplt->output_section->vma
3148 + htab->sgotplt->output_offset
3149 + got_offset);
3150 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3151 rela.r_addend = 0;
3152 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
3153 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3155 if (!h->def_regular)
3157 /* Mark the symbol as undefined, rather than as defined in
3158 the .plt section. Leave the value if there were any
3159 relocations where pointer equality matters (this is a clue
3160 for the dynamic linker, to make function pointer
3161 comparisons work between an application and shared
3162 library), otherwise set it to zero. If a function is only
3163 called from a binary, there is no need to slow down
3164 shared libraries because of that. */
3165 sym->st_shndx = SHN_UNDEF;
3166 if (!h->pointer_equality_needed)
3167 sym->st_value = 0;
3171 if (h->got.offset != (bfd_vma) -1
3172 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3173 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3175 Elf_Internal_Rela rela;
3176 bfd_byte *loc;
3178 /* This symbol has an entry in the global offset table. Set it
3179 up. */
3180 if (htab->sgot == NULL || htab->srelgot == NULL)
3181 abort ();
3183 rela.r_offset = (htab->sgot->output_section->vma
3184 + htab->sgot->output_offset
3185 + (h->got.offset &~ (bfd_vma) 1));
3187 /* If this is a static link, or it is a -Bsymbolic link and the
3188 symbol is defined locally or was forced to be local because
3189 of a version file, we just want to emit a RELATIVE reloc.
3190 The entry in the global offset table will already have been
3191 initialized in the relocate_section function. */
3192 if (info->shared
3193 && SYMBOL_REFERENCES_LOCAL (info, h))
3195 BFD_ASSERT((h->got.offset & 1) != 0);
3196 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3197 rela.r_addend = (h->root.u.def.value
3198 + h->root.u.def.section->output_section->vma
3199 + h->root.u.def.section->output_offset);
3201 else
3203 BFD_ASSERT((h->got.offset & 1) == 0);
3204 bfd_put_64 (output_bfd, (bfd_vma) 0,
3205 htab->sgot->contents + h->got.offset);
3206 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3207 rela.r_addend = 0;
3210 loc = htab->srelgot->contents;
3211 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3212 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3215 if (h->needs_copy)
3217 Elf_Internal_Rela rela;
3218 bfd_byte *loc;
3220 /* This symbol needs a copy reloc. Set it up. */
3222 if (h->dynindx == -1
3223 || (h->root.type != bfd_link_hash_defined
3224 && h->root.type != bfd_link_hash_defweak)
3225 || htab->srelbss == NULL)
3226 abort ();
3228 rela.r_offset = (h->root.u.def.value
3229 + h->root.u.def.section->output_section->vma
3230 + h->root.u.def.section->output_offset);
3231 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3232 rela.r_addend = 0;
3233 loc = htab->srelbss->contents;
3234 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
3235 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3238 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3239 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3240 || h == htab->elf.hgot)
3241 sym->st_shndx = SHN_ABS;
3243 return TRUE;
3246 /* Used to decide how to sort relocs in an optimal manner for the
3247 dynamic linker, before writing them out. */
3249 static enum elf_reloc_type_class
3250 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
3252 switch ((int) ELF64_R_TYPE (rela->r_info))
3254 case R_X86_64_RELATIVE:
3255 return reloc_class_relative;
3256 case R_X86_64_JUMP_SLOT:
3257 return reloc_class_plt;
3258 case R_X86_64_COPY:
3259 return reloc_class_copy;
3260 default:
3261 return reloc_class_normal;
3265 /* Finish up the dynamic sections. */
3267 static bfd_boolean
3268 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3270 struct elf64_x86_64_link_hash_table *htab;
3271 bfd *dynobj;
3272 asection *sdyn;
3274 htab = elf64_x86_64_hash_table (info);
3275 dynobj = htab->elf.dynobj;
3276 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3278 if (htab->elf.dynamic_sections_created)
3280 Elf64_External_Dyn *dyncon, *dynconend;
3282 if (sdyn == NULL || htab->sgot == NULL)
3283 abort ();
3285 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3286 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3287 for (; dyncon < dynconend; dyncon++)
3289 Elf_Internal_Dyn dyn;
3290 asection *s;
3292 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
3294 switch (dyn.d_tag)
3296 default:
3297 continue;
3299 case DT_PLTGOT:
3300 s = htab->sgotplt;
3301 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
3302 break;
3304 case DT_JMPREL:
3305 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
3306 break;
3308 case DT_PLTRELSZ:
3309 s = htab->srelplt->output_section;
3310 dyn.d_un.d_val = s->size;
3311 break;
3313 case DT_RELASZ:
3314 /* The procedure linkage table relocs (DT_JMPREL) should
3315 not be included in the overall relocs (DT_RELA).
3316 Therefore, we override the DT_RELASZ entry here to
3317 make it not include the JMPREL relocs. Since the
3318 linker script arranges for .rela.plt to follow all
3319 other relocation sections, we don't have to worry
3320 about changing the DT_RELA entry. */
3321 if (htab->srelplt != NULL)
3323 s = htab->srelplt->output_section;
3324 dyn.d_un.d_val -= s->size;
3326 break;
3328 case DT_TLSDESC_PLT:
3329 s = htab->splt;
3330 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3331 + htab->tlsdesc_plt;
3332 break;
3334 case DT_TLSDESC_GOT:
3335 s = htab->sgot;
3336 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
3337 + htab->tlsdesc_got;
3338 break;
3341 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
3344 /* Fill in the special first entry in the procedure linkage table. */
3345 if (htab->splt && htab->splt->size > 0)
3347 /* Fill in the first entry in the procedure linkage table. */
3348 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
3349 PLT_ENTRY_SIZE);
3350 /* Add offset for pushq GOT+8(%rip), since the instruction
3351 uses 6 bytes subtract this value. */
3352 bfd_put_32 (output_bfd,
3353 (htab->sgotplt->output_section->vma
3354 + htab->sgotplt->output_offset
3356 - htab->splt->output_section->vma
3357 - htab->splt->output_offset
3358 - 6),
3359 htab->splt->contents + 2);
3360 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
3361 the end of the instruction. */
3362 bfd_put_32 (output_bfd,
3363 (htab->sgotplt->output_section->vma
3364 + htab->sgotplt->output_offset
3365 + 16
3366 - htab->splt->output_section->vma
3367 - htab->splt->output_offset
3368 - 12),
3369 htab->splt->contents + 8);
3371 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
3372 PLT_ENTRY_SIZE;
3374 if (htab->tlsdesc_plt)
3376 bfd_put_64 (output_bfd, (bfd_vma) 0,
3377 htab->sgot->contents + htab->tlsdesc_got);
3379 memcpy (htab->splt->contents + htab->tlsdesc_plt,
3380 elf64_x86_64_plt0_entry,
3381 PLT_ENTRY_SIZE);
3383 /* Add offset for pushq GOT+8(%rip), since the
3384 instruction uses 6 bytes subtract this value. */
3385 bfd_put_32 (output_bfd,
3386 (htab->sgotplt->output_section->vma
3387 + htab->sgotplt->output_offset
3389 - htab->splt->output_section->vma
3390 - htab->splt->output_offset
3391 - htab->tlsdesc_plt
3392 - 6),
3393 htab->splt->contents + htab->tlsdesc_plt + 2);
3394 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
3395 htab->tlsdesc_got. The 12 is the offset to the end of
3396 the instruction. */
3397 bfd_put_32 (output_bfd,
3398 (htab->sgot->output_section->vma
3399 + htab->sgot->output_offset
3400 + htab->tlsdesc_got
3401 - htab->splt->output_section->vma
3402 - htab->splt->output_offset
3403 - htab->tlsdesc_plt
3404 - 12),
3405 htab->splt->contents + htab->tlsdesc_plt + 8);
3410 if (htab->sgotplt)
3412 /* Fill in the first three entries in the global offset table. */
3413 if (htab->sgotplt->size > 0)
3415 /* Set the first entry in the global offset table to the address of
3416 the dynamic section. */
3417 if (sdyn == NULL)
3418 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
3419 else
3420 bfd_put_64 (output_bfd,
3421 sdyn->output_section->vma + sdyn->output_offset,
3422 htab->sgotplt->contents);
3423 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
3424 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
3425 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
3428 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
3429 GOT_ENTRY_SIZE;
3432 if (htab->sgot && htab->sgot->size > 0)
3433 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
3434 = GOT_ENTRY_SIZE;
3436 return TRUE;
3439 /* Return address for Ith PLT stub in section PLT, for relocation REL
3440 or (bfd_vma) -1 if it should not be included. */
3442 static bfd_vma
3443 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
3444 const arelent *rel ATTRIBUTE_UNUSED)
3446 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
3449 /* Handle an x86-64 specific section when reading an object file. This
3450 is called when elfcode.h finds a section with an unknown type. */
3452 static bfd_boolean
3453 elf64_x86_64_section_from_shdr (bfd *abfd,
3454 Elf_Internal_Shdr *hdr,
3455 const char *name,
3456 int shindex)
3458 if (hdr->sh_type != SHT_X86_64_UNWIND)
3459 return FALSE;
3461 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
3462 return FALSE;
3464 return TRUE;
3467 /* Hook called by the linker routine which adds symbols from an object
3468 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
3469 of .bss. */
3471 static bfd_boolean
3472 elf64_x86_64_add_symbol_hook (bfd *abfd,
3473 struct bfd_link_info *info ATTRIBUTE_UNUSED,
3474 Elf_Internal_Sym *sym,
3475 const char **namep ATTRIBUTE_UNUSED,
3476 flagword *flagsp ATTRIBUTE_UNUSED,
3477 asection **secp, bfd_vma *valp)
3479 asection *lcomm;
3481 switch (sym->st_shndx)
3483 case SHN_X86_64_LCOMMON:
3484 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
3485 if (lcomm == NULL)
3487 lcomm = bfd_make_section_with_flags (abfd,
3488 "LARGE_COMMON",
3489 (SEC_ALLOC
3490 | SEC_IS_COMMON
3491 | SEC_LINKER_CREATED));
3492 if (lcomm == NULL)
3493 return FALSE;
3494 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
3496 *secp = lcomm;
3497 *valp = sym->st_size;
3498 break;
3500 return TRUE;
3504 /* Given a BFD section, try to locate the corresponding ELF section
3505 index. */
3507 static bfd_boolean
3508 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
3509 asection *sec, int *index)
3511 if (sec == &_bfd_elf_large_com_section)
3513 *index = SHN_X86_64_LCOMMON;
3514 return TRUE;
3516 return FALSE;
3519 /* Process a symbol. */
3521 static void
3522 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
3523 asymbol *asym)
3525 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
3527 switch (elfsym->internal_elf_sym.st_shndx)
3529 case SHN_X86_64_LCOMMON:
3530 asym->section = &_bfd_elf_large_com_section;
3531 asym->value = elfsym->internal_elf_sym.st_size;
3532 /* Common symbol doesn't set BSF_GLOBAL. */
3533 asym->flags &= ~BSF_GLOBAL;
3534 break;
3538 static bfd_boolean
3539 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
3541 return (sym->st_shndx == SHN_COMMON
3542 || sym->st_shndx == SHN_X86_64_LCOMMON);
3545 static unsigned int
3546 elf64_x86_64_common_section_index (asection *sec)
3548 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3549 return SHN_COMMON;
3550 else
3551 return SHN_X86_64_LCOMMON;
3554 static asection *
3555 elf64_x86_64_common_section (asection *sec)
3557 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
3558 return bfd_com_section_ptr;
3559 else
3560 return &_bfd_elf_large_com_section;
3563 static bfd_boolean
3564 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
3565 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
3566 struct elf_link_hash_entry *h,
3567 Elf_Internal_Sym *sym,
3568 asection **psec,
3569 bfd_vma *pvalue ATTRIBUTE_UNUSED,
3570 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
3571 bfd_boolean *skip ATTRIBUTE_UNUSED,
3572 bfd_boolean *override ATTRIBUTE_UNUSED,
3573 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
3574 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
3575 bfd_boolean *newdef ATTRIBUTE_UNUSED,
3576 bfd_boolean *newdyn,
3577 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
3578 bfd_boolean *newweak ATTRIBUTE_UNUSED,
3579 bfd *abfd ATTRIBUTE_UNUSED,
3580 asection **sec,
3581 bfd_boolean *olddef ATTRIBUTE_UNUSED,
3582 bfd_boolean *olddyn,
3583 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
3584 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
3585 bfd *oldbfd,
3586 asection **oldsec)
3588 /* A normal common symbol and a large common symbol result in a
3589 normal common symbol. We turn the large common symbol into a
3590 normal one. */
3591 if (!*olddyn
3592 && h->root.type == bfd_link_hash_common
3593 && !*newdyn
3594 && bfd_is_com_section (*sec)
3595 && *oldsec != *sec)
3597 if (sym->st_shndx == SHN_COMMON
3598 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
3600 h->root.u.c.p->section
3601 = bfd_make_section_old_way (oldbfd, "COMMON");
3602 h->root.u.c.p->section->flags = SEC_ALLOC;
3604 else if (sym->st_shndx == SHN_X86_64_LCOMMON
3605 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
3606 *psec = *sec = bfd_com_section_ptr;
3609 return TRUE;
3612 static int
3613 elf64_x86_64_additional_program_headers (bfd *abfd,
3614 struct bfd_link_info *info ATTRIBUTE_UNUSED)
3616 asection *s;
3617 int count = 0;
3619 /* Check to see if we need a large readonly segment. */
3620 s = bfd_get_section_by_name (abfd, ".lrodata");
3621 if (s && (s->flags & SEC_LOAD))
3622 count++;
3624 /* Check to see if we need a large data segment. Since .lbss sections
3625 is placed right after the .bss section, there should be no need for
3626 a large data segment just because of .lbss. */
3627 s = bfd_get_section_by_name (abfd, ".ldata");
3628 if (s && (s->flags & SEC_LOAD))
3629 count++;
3631 return count;
3634 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
3636 static bfd_boolean
3637 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
3639 if (h->plt.offset != (bfd_vma) -1
3640 && !h->def_regular
3641 && !h->pointer_equality_needed)
3642 return FALSE;
3644 return _bfd_elf_hash_symbol (h);
3647 static const struct bfd_elf_special_section
3648 elf64_x86_64_special_sections[]=
3650 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3651 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3652 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
3653 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3654 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
3655 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
3656 { NULL, 0, 0, 0, 0 }
3659 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
3660 #define TARGET_LITTLE_NAME "elf64-x86-64"
3661 #define ELF_ARCH bfd_arch_i386
3662 #define ELF_MACHINE_CODE EM_X86_64
3663 #define ELF_MAXPAGESIZE 0x200000
3664 #define ELF_MINPAGESIZE 0x1000
3665 #define ELF_COMMONPAGESIZE 0x1000
3667 #define elf_backend_can_gc_sections 1
3668 #define elf_backend_can_refcount 1
3669 #define elf_backend_want_got_plt 1
3670 #define elf_backend_plt_readonly 1
3671 #define elf_backend_want_plt_sym 0
3672 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
3673 #define elf_backend_rela_normal 1
3675 #define elf_info_to_howto elf64_x86_64_info_to_howto
3677 #define bfd_elf64_bfd_link_hash_table_create \
3678 elf64_x86_64_link_hash_table_create
3679 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
3680 #define bfd_elf64_bfd_reloc_name_lookup \
3681 elf64_x86_64_reloc_name_lookup
3683 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
3684 #define elf_backend_check_relocs elf64_x86_64_check_relocs
3685 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
3686 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
3687 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
3688 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
3689 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
3690 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
3691 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
3692 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
3693 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
3694 #define elf_backend_relocate_section elf64_x86_64_relocate_section
3695 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
3696 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
3697 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
3698 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
3699 #define elf_backend_object_p elf64_x86_64_elf_object_p
3700 #define bfd_elf64_mkobject elf64_x86_64_mkobject
3702 #define elf_backend_section_from_shdr \
3703 elf64_x86_64_section_from_shdr
3705 #define elf_backend_section_from_bfd_section \
3706 elf64_x86_64_elf_section_from_bfd_section
3707 #define elf_backend_add_symbol_hook \
3708 elf64_x86_64_add_symbol_hook
3709 #define elf_backend_symbol_processing \
3710 elf64_x86_64_symbol_processing
3711 #define elf_backend_common_section_index \
3712 elf64_x86_64_common_section_index
3713 #define elf_backend_common_section \
3714 elf64_x86_64_common_section
3715 #define elf_backend_common_definition \
3716 elf64_x86_64_common_definition
3717 #define elf_backend_merge_symbol \
3718 elf64_x86_64_merge_symbol
3719 #define elf_backend_special_sections \
3720 elf64_x86_64_special_sections
3721 #define elf_backend_additional_program_headers \
3722 elf64_x86_64_additional_program_headers
3723 #define elf_backend_hash_symbol \
3724 elf64_x86_64_hash_symbol
3726 #include "elf64-target.h"
3728 /* FreeBSD support. */
3730 #undef TARGET_LITTLE_SYM
3731 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
3732 #undef TARGET_LITTLE_NAME
3733 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
3735 #undef ELF_OSABI
3736 #define ELF_OSABI ELFOSABI_FREEBSD
3738 #undef elf_backend_post_process_headers
3739 #define elf_backend_post_process_headers _bfd_elf_set_osabi
3741 #undef elf64_bed
3742 #define elf64_bed elf64_x86_64_fbsd_bed
3744 #include "elf64-target.h"