PR 10165
[binutils.git] / bfd / elf64-x86-64.c
blob6819b34c5af1a141335b8caa915e10e4d023397e
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
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 3 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,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "elf-bfd.h"
28 #include "bfd_stdint.h"
30 #include "elf/x86-64.h"
32 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
33 #define MINUS_ONE (~ (bfd_vma) 0)
35 /* The relocation "howto" table. Order of fields:
36 type, rightshift, size, bitsize, pc_relative, bitpos, complain_on_overflow,
37 special_function, name, partial_inplace, src_mask, dst_mask, pcrel_offset. */
38 static reloc_howto_type x86_64_elf_howto_table[] =
40 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
41 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
42 FALSE),
43 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
44 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
45 FALSE),
46 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
47 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
48 TRUE),
49 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
50 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
51 FALSE),
52 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
53 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
54 TRUE),
55 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
56 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
57 FALSE),
58 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
59 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
60 MINUS_ONE, FALSE),
61 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
63 MINUS_ONE, FALSE),
64 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
65 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
66 MINUS_ONE, FALSE),
67 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
68 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
69 0xffffffff, TRUE),
70 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
71 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
72 FALSE),
73 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
74 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
75 FALSE),
76 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
77 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
78 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
79 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
80 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield,
81 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
82 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
83 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
84 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
85 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
86 MINUS_ONE, FALSE),
87 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
89 MINUS_ONE, FALSE),
90 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
91 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
92 MINUS_ONE, FALSE),
93 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
94 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
95 0xffffffff, TRUE),
96 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
97 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
98 0xffffffff, TRUE),
99 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
100 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
101 0xffffffff, FALSE),
102 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
103 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
104 0xffffffff, TRUE),
105 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
106 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
107 0xffffffff, FALSE),
108 HOWTO(R_X86_64_PC64, 0, 4, 64, TRUE, 0, complain_overflow_bitfield,
109 bfd_elf_generic_reloc, "R_X86_64_PC64", FALSE, MINUS_ONE, MINUS_ONE,
110 TRUE),
111 HOWTO(R_X86_64_GOTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
112 bfd_elf_generic_reloc, "R_X86_64_GOTOFF64",
113 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
114 HOWTO(R_X86_64_GOTPC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
115 bfd_elf_generic_reloc, "R_X86_64_GOTPC32",
116 FALSE, 0xffffffff, 0xffffffff, TRUE),
117 HOWTO(R_X86_64_GOT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
118 bfd_elf_generic_reloc, "R_X86_64_GOT64", FALSE, MINUS_ONE, MINUS_ONE,
119 FALSE),
120 HOWTO(R_X86_64_GOTPCREL64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
121 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL64", FALSE, MINUS_ONE,
122 MINUS_ONE, TRUE),
123 HOWTO(R_X86_64_GOTPC64, 0, 4, 64, TRUE, 0, complain_overflow_signed,
124 bfd_elf_generic_reloc, "R_X86_64_GOTPC64",
125 FALSE, MINUS_ONE, MINUS_ONE, TRUE),
126 HOWTO(R_X86_64_GOTPLT64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
127 bfd_elf_generic_reloc, "R_X86_64_GOTPLT64", FALSE, MINUS_ONE,
128 MINUS_ONE, FALSE),
129 HOWTO(R_X86_64_PLTOFF64, 0, 4, 64, FALSE, 0, complain_overflow_signed,
130 bfd_elf_generic_reloc, "R_X86_64_PLTOFF64", FALSE, MINUS_ONE,
131 MINUS_ONE, FALSE),
132 EMPTY_HOWTO (32),
133 EMPTY_HOWTO (33),
134 HOWTO(R_X86_64_GOTPC32_TLSDESC, 0, 2, 32, TRUE, 0,
135 complain_overflow_bitfield, bfd_elf_generic_reloc,
136 "R_X86_64_GOTPC32_TLSDESC",
137 FALSE, 0xffffffff, 0xffffffff, TRUE),
138 HOWTO(R_X86_64_TLSDESC_CALL, 0, 0, 0, FALSE, 0,
139 complain_overflow_dont, bfd_elf_generic_reloc,
140 "R_X86_64_TLSDESC_CALL",
141 FALSE, 0, 0, FALSE),
142 HOWTO(R_X86_64_TLSDESC, 0, 4, 64, FALSE, 0,
143 complain_overflow_bitfield, bfd_elf_generic_reloc,
144 "R_X86_64_TLSDESC",
145 FALSE, MINUS_ONE, MINUS_ONE, FALSE),
146 HOWTO(R_X86_64_IRELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
147 bfd_elf_generic_reloc, "R_X86_64_IRELATIVE", FALSE, MINUS_ONE,
148 MINUS_ONE, FALSE),
150 /* We have a gap in the reloc numbers here.
151 R_X86_64_standard counts the number up to this point, and
152 R_X86_64_vt_offset is the value to subtract from a reloc type of
153 R_X86_64_GNU_VT* to form an index into this table. */
154 #define R_X86_64_standard (R_X86_64_IRELATIVE + 1)
155 #define R_X86_64_vt_offset (R_X86_64_GNU_VTINHERIT - R_X86_64_standard)
157 /* GNU extension to record C++ vtable hierarchy. */
158 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
159 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
161 /* GNU extension to record C++ vtable member usage. */
162 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
163 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
164 FALSE)
167 #define IS_X86_64_PCREL_TYPE(TYPE) \
168 ( ((TYPE) == R_X86_64_PC8) \
169 || ((TYPE) == R_X86_64_PC16) \
170 || ((TYPE) == R_X86_64_PC32) \
171 || ((TYPE) == R_X86_64_PC64))
173 /* Map BFD relocs to the x86_64 elf relocs. */
174 struct elf_reloc_map
176 bfd_reloc_code_real_type bfd_reloc_val;
177 unsigned char elf_reloc_val;
180 static const struct elf_reloc_map x86_64_reloc_map[] =
182 { BFD_RELOC_NONE, R_X86_64_NONE, },
183 { BFD_RELOC_64, R_X86_64_64, },
184 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
185 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
186 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
187 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
188 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
189 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
190 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
191 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
192 { BFD_RELOC_32, R_X86_64_32, },
193 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
194 { BFD_RELOC_16, R_X86_64_16, },
195 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
196 { BFD_RELOC_8, R_X86_64_8, },
197 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
198 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
199 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
200 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
201 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
202 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
203 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
204 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
205 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
206 { BFD_RELOC_64_PCREL, R_X86_64_PC64, },
207 { BFD_RELOC_X86_64_GOTOFF64, R_X86_64_GOTOFF64, },
208 { BFD_RELOC_X86_64_GOTPC32, R_X86_64_GOTPC32, },
209 { BFD_RELOC_X86_64_GOT64, R_X86_64_GOT64, },
210 { BFD_RELOC_X86_64_GOTPCREL64,R_X86_64_GOTPCREL64, },
211 { BFD_RELOC_X86_64_GOTPC64, R_X86_64_GOTPC64, },
212 { BFD_RELOC_X86_64_GOTPLT64, R_X86_64_GOTPLT64, },
213 { BFD_RELOC_X86_64_PLTOFF64, R_X86_64_PLTOFF64, },
214 { BFD_RELOC_X86_64_GOTPC32_TLSDESC, R_X86_64_GOTPC32_TLSDESC, },
215 { BFD_RELOC_X86_64_TLSDESC_CALL, R_X86_64_TLSDESC_CALL, },
216 { BFD_RELOC_X86_64_TLSDESC, R_X86_64_TLSDESC, },
217 { BFD_RELOC_X86_64_IRELATIVE, R_X86_64_IRELATIVE, },
218 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
219 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
222 static reloc_howto_type *
223 elf64_x86_64_rtype_to_howto (bfd *abfd, unsigned r_type)
225 unsigned i;
227 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT
228 || r_type >= (unsigned int) R_X86_64_max)
230 if (r_type >= (unsigned int) R_X86_64_standard)
232 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
233 abfd, (int) r_type);
234 r_type = R_X86_64_NONE;
236 i = r_type;
238 else
239 i = r_type - (unsigned int) R_X86_64_vt_offset;
240 BFD_ASSERT (x86_64_elf_howto_table[i].type == r_type);
241 return &x86_64_elf_howto_table[i];
244 /* Given a BFD reloc type, return a HOWTO structure. */
245 static reloc_howto_type *
246 elf64_x86_64_reloc_type_lookup (bfd *abfd,
247 bfd_reloc_code_real_type code)
249 unsigned int i;
251 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
252 i++)
254 if (x86_64_reloc_map[i].bfd_reloc_val == code)
255 return elf64_x86_64_rtype_to_howto (abfd,
256 x86_64_reloc_map[i].elf_reloc_val);
258 return 0;
261 static reloc_howto_type *
262 elf64_x86_64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
263 const char *r_name)
265 unsigned int i;
267 for (i = 0;
268 i < (sizeof (x86_64_elf_howto_table)
269 / sizeof (x86_64_elf_howto_table[0]));
270 i++)
271 if (x86_64_elf_howto_table[i].name != NULL
272 && strcasecmp (x86_64_elf_howto_table[i].name, r_name) == 0)
273 return &x86_64_elf_howto_table[i];
275 return NULL;
278 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
280 static void
281 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
282 Elf_Internal_Rela *dst)
284 unsigned r_type;
286 r_type = ELF64_R_TYPE (dst->r_info);
287 cache_ptr->howto = elf64_x86_64_rtype_to_howto (abfd, r_type);
288 BFD_ASSERT (r_type == cache_ptr->howto->type);
291 /* Support for core dump NOTE sections. */
292 static bfd_boolean
293 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
295 int offset;
296 size_t size;
298 switch (note->descsz)
300 default:
301 return FALSE;
303 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
304 /* pr_cursig */
305 elf_tdata (abfd)->core_signal
306 = bfd_get_16 (abfd, note->descdata + 12);
308 /* pr_pid */
309 elf_tdata (abfd)->core_pid
310 = bfd_get_32 (abfd, note->descdata + 32);
312 /* pr_reg */
313 offset = 112;
314 size = 216;
316 break;
319 /* Make a ".reg/999" section. */
320 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
321 size, note->descpos + offset);
324 static bfd_boolean
325 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
327 switch (note->descsz)
329 default:
330 return FALSE;
332 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
333 elf_tdata (abfd)->core_program
334 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
335 elf_tdata (abfd)->core_command
336 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
339 /* Note that for some reason, a spurious space is tacked
340 onto the end of the args in some (at least one anyway)
341 implementations, so strip it off if it exists. */
344 char *command = elf_tdata (abfd)->core_command;
345 int n = strlen (command);
347 if (0 < n && command[n - 1] == ' ')
348 command[n - 1] = '\0';
351 return TRUE;
354 /* Functions for the x86-64 ELF linker. */
356 /* The name of the dynamic interpreter. This is put in the .interp
357 section. */
359 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
361 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
362 copying dynamic variables from a shared lib into an app's dynbss
363 section, and instead use a dynamic relocation to point into the
364 shared lib. */
365 #define ELIMINATE_COPY_RELOCS 1
367 /* The size in bytes of an entry in the global offset table. */
369 #define GOT_ENTRY_SIZE 8
371 /* The size in bytes of an entry in the procedure linkage table. */
373 #define PLT_ENTRY_SIZE 16
375 /* The first entry in a procedure linkage table looks like this. See the
376 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
378 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
380 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
381 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
382 0x0f, 0x1f, 0x40, 0x00 /* nopl 0(%rax) */
385 /* Subsequent entries in a procedure linkage table look like this. */
387 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
389 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
390 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
391 0x68, /* pushq immediate */
392 0, 0, 0, 0, /* replaced with index into relocation table. */
393 0xe9, /* jmp relative */
394 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
397 /* The x86-64 linker needs to keep track of the number of relocs that
398 it decides to copy as dynamic relocs in check_relocs for each symbol.
399 This is so that it can later discard them if they are found to be
400 unnecessary. We store the information in a field extending the
401 regular ELF linker hash table. */
403 struct elf64_x86_64_dyn_relocs
405 /* Next section. */
406 struct elf64_x86_64_dyn_relocs *next;
408 /* The input section of the reloc. */
409 asection *sec;
411 /* Total number of relocs copied for the input section. */
412 bfd_size_type count;
414 /* Number of pc-relative relocs copied for the input section. */
415 bfd_size_type pc_count;
418 /* x86-64 ELF linker hash entry. */
420 struct elf64_x86_64_link_hash_entry
422 struct elf_link_hash_entry elf;
424 /* Track dynamic relocs copied for this symbol. */
425 struct elf64_x86_64_dyn_relocs *dyn_relocs;
427 #define GOT_UNKNOWN 0
428 #define GOT_NORMAL 1
429 #define GOT_TLS_GD 2
430 #define GOT_TLS_IE 3
431 #define GOT_TLS_GDESC 4
432 #define GOT_TLS_GD_BOTH_P(type) \
433 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC))
434 #define GOT_TLS_GD_P(type) \
435 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type))
436 #define GOT_TLS_GDESC_P(type) \
437 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type))
438 #define GOT_TLS_GD_ANY_P(type) \
439 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type))
440 unsigned char tls_type;
442 /* Offset of the GOTPLT entry reserved for the TLS descriptor,
443 starting at the end of the jump table. */
444 bfd_vma tlsdesc_got;
447 #define elf64_x86_64_hash_entry(ent) \
448 ((struct elf64_x86_64_link_hash_entry *)(ent))
450 struct elf64_x86_64_obj_tdata
452 struct elf_obj_tdata root;
454 /* tls_type for each local got entry. */
455 char *local_got_tls_type;
457 /* GOTPLT entries for TLS descriptors. */
458 bfd_vma *local_tlsdesc_gotent;
461 #define elf64_x86_64_tdata(abfd) \
462 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
464 #define elf64_x86_64_local_got_tls_type(abfd) \
465 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
467 #define elf64_x86_64_local_tlsdesc_gotent(abfd) \
468 (elf64_x86_64_tdata (abfd)->local_tlsdesc_gotent)
470 #define is_x86_64_elf(bfd) \
471 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
472 && elf_tdata (bfd) != NULL \
473 && elf_object_id (bfd) == X86_64_ELF_TDATA)
475 static bfd_boolean
476 elf64_x86_64_mkobject (bfd *abfd)
478 return bfd_elf_allocate_object (abfd, sizeof (struct elf64_x86_64_obj_tdata),
479 X86_64_ELF_TDATA);
482 /* x86-64 ELF linker hash table. */
484 struct elf64_x86_64_link_hash_table
486 struct elf_link_hash_table elf;
488 /* Short-cuts to get to dynamic linker sections. */
489 asection *sgot;
490 asection *sgotplt;
491 asection *srelgot;
492 asection *splt;
493 asection *srelplt;
494 asection *sdynbss;
495 asection *srelbss;
496 asection *igotplt;
497 asection *iplt;
498 asection *irelplt;
499 asection *irelifunc;
501 /* The offset into splt of the PLT entry for the TLS descriptor
502 resolver. Special values are 0, if not necessary (or not found
503 to be necessary yet), and -1 if needed but not determined
504 yet. */
505 bfd_vma tlsdesc_plt;
506 /* The offset into sgot of the GOT entry used by the PLT entry
507 above. */
508 bfd_vma tlsdesc_got;
510 union {
511 bfd_signed_vma refcount;
512 bfd_vma offset;
513 } tls_ld_got;
515 /* The amount of space used by the jump slots in the GOT. */
516 bfd_vma sgotplt_jump_table_size;
518 /* Small local sym to section mapping cache. */
519 struct sym_sec_cache sym_sec;
521 /* _TLS_MODULE_BASE_ symbol. */
522 struct bfd_link_hash_entry *tls_module_base;
525 /* Get the x86-64 ELF linker hash table from a link_info structure. */
527 #define elf64_x86_64_hash_table(p) \
528 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
530 #define elf64_x86_64_compute_jump_table_size(htab) \
531 ((htab)->srelplt->reloc_count * GOT_ENTRY_SIZE)
533 /* Create an entry in an x86-64 ELF linker hash table. */
535 static struct bfd_hash_entry *
536 elf64_x86_64_link_hash_newfunc (struct bfd_hash_entry *entry,
537 struct bfd_hash_table *table,
538 const char *string)
540 /* Allocate the structure if it has not already been allocated by a
541 subclass. */
542 if (entry == NULL)
544 entry = bfd_hash_allocate (table,
545 sizeof (struct elf64_x86_64_link_hash_entry));
546 if (entry == NULL)
547 return entry;
550 /* Call the allocation method of the superclass. */
551 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
552 if (entry != NULL)
554 struct elf64_x86_64_link_hash_entry *eh;
556 eh = (struct elf64_x86_64_link_hash_entry *) entry;
557 eh->dyn_relocs = NULL;
558 eh->tls_type = GOT_UNKNOWN;
559 eh->tlsdesc_got = (bfd_vma) -1;
562 return entry;
565 /* Create an X86-64 ELF linker hash table. */
567 static struct bfd_link_hash_table *
568 elf64_x86_64_link_hash_table_create (bfd *abfd)
570 struct elf64_x86_64_link_hash_table *ret;
571 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
573 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
574 if (ret == NULL)
575 return NULL;
577 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
578 elf64_x86_64_link_hash_newfunc,
579 sizeof (struct elf64_x86_64_link_hash_entry)))
581 free (ret);
582 return NULL;
585 ret->sgot = NULL;
586 ret->sgotplt = NULL;
587 ret->srelgot = NULL;
588 ret->splt = NULL;
589 ret->srelplt = NULL;
590 ret->sdynbss = NULL;
591 ret->srelbss = NULL;
592 ret->igotplt= NULL;
593 ret->iplt = NULL;
594 ret->irelplt= NULL;
595 ret->irelifunc = NULL;
596 ret->sym_sec.abfd = NULL;
597 ret->tlsdesc_plt = 0;
598 ret->tlsdesc_got = 0;
599 ret->tls_ld_got.refcount = 0;
600 ret->sgotplt_jump_table_size = 0;
601 ret->tls_module_base = NULL;
603 return &ret->elf.root;
606 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
607 shortcuts to them in our hash table. */
609 static bfd_boolean
610 elf64_x86_64_create_got_section (bfd *dynobj, struct bfd_link_info *info)
612 struct elf64_x86_64_link_hash_table *htab;
614 if (! _bfd_elf_create_got_section (dynobj, info))
615 return FALSE;
617 htab = elf64_x86_64_hash_table (info);
618 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
619 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
620 if (!htab->sgot || !htab->sgotplt)
621 abort ();
623 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
624 (SEC_ALLOC | SEC_LOAD
625 | SEC_HAS_CONTENTS
626 | SEC_IN_MEMORY
627 | SEC_LINKER_CREATED
628 | SEC_READONLY));
629 if (htab->srelgot == NULL
630 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
631 return FALSE;
632 return TRUE;
635 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
636 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
637 hash table. */
639 static bfd_boolean
640 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
642 struct elf64_x86_64_link_hash_table *htab;
644 htab = elf64_x86_64_hash_table (info);
645 if (!htab->sgot && !elf64_x86_64_create_got_section (dynobj, info))
646 return FALSE;
648 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
649 return FALSE;
651 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
652 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
653 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
654 if (!info->shared)
655 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
657 if (!htab->splt || !htab->srelplt || !htab->sdynbss
658 || (!info->shared && !htab->srelbss))
659 abort ();
661 return TRUE;
664 /* Copy the extra info we tack onto an elf_link_hash_entry. */
666 static void
667 elf64_x86_64_copy_indirect_symbol (struct bfd_link_info *info,
668 struct elf_link_hash_entry *dir,
669 struct elf_link_hash_entry *ind)
671 struct elf64_x86_64_link_hash_entry *edir, *eind;
673 edir = (struct elf64_x86_64_link_hash_entry *) dir;
674 eind = (struct elf64_x86_64_link_hash_entry *) ind;
676 if (eind->dyn_relocs != NULL)
678 if (edir->dyn_relocs != NULL)
680 struct elf64_x86_64_dyn_relocs **pp;
681 struct elf64_x86_64_dyn_relocs *p;
683 /* Add reloc counts against the indirect sym to the direct sym
684 list. Merge any entries against the same section. */
685 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
687 struct elf64_x86_64_dyn_relocs *q;
689 for (q = edir->dyn_relocs; q != NULL; q = q->next)
690 if (q->sec == p->sec)
692 q->pc_count += p->pc_count;
693 q->count += p->count;
694 *pp = p->next;
695 break;
697 if (q == NULL)
698 pp = &p->next;
700 *pp = edir->dyn_relocs;
703 edir->dyn_relocs = eind->dyn_relocs;
704 eind->dyn_relocs = NULL;
707 if (ind->root.type == bfd_link_hash_indirect
708 && dir->got.refcount <= 0)
710 edir->tls_type = eind->tls_type;
711 eind->tls_type = GOT_UNKNOWN;
714 if (ELIMINATE_COPY_RELOCS
715 && ind->root.type != bfd_link_hash_indirect
716 && dir->dynamic_adjusted)
718 /* If called to transfer flags for a weakdef during processing
719 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
720 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
721 dir->ref_dynamic |= ind->ref_dynamic;
722 dir->ref_regular |= ind->ref_regular;
723 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
724 dir->needs_plt |= ind->needs_plt;
725 dir->pointer_equality_needed |= ind->pointer_equality_needed;
727 else
728 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
731 static bfd_boolean
732 elf64_x86_64_elf_object_p (bfd *abfd)
734 /* Set the right machine number for an x86-64 elf64 file. */
735 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
736 return TRUE;
739 typedef union
741 unsigned char c[2];
742 uint16_t i;
744 x86_64_opcode16;
746 typedef union
748 unsigned char c[4];
749 uint32_t i;
751 x86_64_opcode32;
753 /* Return TRUE if the TLS access code sequence support transition
754 from R_TYPE. */
756 static bfd_boolean
757 elf64_x86_64_check_tls_transition (bfd *abfd, asection *sec,
758 bfd_byte *contents,
759 Elf_Internal_Shdr *symtab_hdr,
760 struct elf_link_hash_entry **sym_hashes,
761 unsigned int r_type,
762 const Elf_Internal_Rela *rel,
763 const Elf_Internal_Rela *relend)
765 unsigned int val;
766 unsigned long r_symndx;
767 struct elf_link_hash_entry *h;
768 bfd_vma offset;
770 /* Get the section contents. */
771 if (contents == NULL)
773 if (elf_section_data (sec)->this_hdr.contents != NULL)
774 contents = elf_section_data (sec)->this_hdr.contents;
775 else
777 /* FIXME: How to better handle error condition? */
778 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
779 return FALSE;
781 /* Cache the section contents for elf_link_input_bfd. */
782 elf_section_data (sec)->this_hdr.contents = contents;
786 offset = rel->r_offset;
787 switch (r_type)
789 case R_X86_64_TLSGD:
790 case R_X86_64_TLSLD:
791 if ((rel + 1) >= relend)
792 return FALSE;
794 if (r_type == R_X86_64_TLSGD)
796 /* Check transition from GD access model. Only
797 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
798 .word 0x6666; rex64; call __tls_get_addr
799 can transit to different access model. */
801 static x86_64_opcode32 leaq = { { 0x66, 0x48, 0x8d, 0x3d } },
802 call = { { 0x66, 0x66, 0x48, 0xe8 } };
803 if (offset < 4
804 || (offset + 12) > sec->size
805 || bfd_get_32 (abfd, contents + offset - 4) != leaq.i
806 || bfd_get_32 (abfd, contents + offset + 4) != call.i)
807 return FALSE;
809 else
811 /* Check transition from LD access model. Only
812 leaq foo@tlsld(%rip), %rdi;
813 call __tls_get_addr
814 can transit to different access model. */
816 static x86_64_opcode32 ld = { { 0x48, 0x8d, 0x3d, 0xe8 } };
817 x86_64_opcode32 op;
819 if (offset < 3 || (offset + 9) > sec->size)
820 return FALSE;
822 op.i = bfd_get_32 (abfd, contents + offset - 3);
823 op.c[3] = bfd_get_8 (abfd, contents + offset + 4);
824 if (op.i != ld.i)
825 return FALSE;
828 r_symndx = ELF64_R_SYM (rel[1].r_info);
829 if (r_symndx < symtab_hdr->sh_info)
830 return FALSE;
832 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
833 /* Use strncmp to check __tls_get_addr since __tls_get_addr
834 may be versioned. */
835 return (h != NULL
836 && h->root.root.string != NULL
837 && (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PC32
838 || ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32)
839 && (strncmp (h->root.root.string,
840 "__tls_get_addr", 14) == 0));
842 case R_X86_64_GOTTPOFF:
843 /* Check transition from IE access model:
844 movq foo@gottpoff(%rip), %reg
845 addq foo@gottpoff(%rip), %reg
848 if (offset < 3 || (offset + 4) > sec->size)
849 return FALSE;
851 val = bfd_get_8 (abfd, contents + offset - 3);
852 if (val != 0x48 && val != 0x4c)
853 return FALSE;
855 val = bfd_get_8 (abfd, contents + offset - 2);
856 if (val != 0x8b && val != 0x03)
857 return FALSE;
859 val = bfd_get_8 (abfd, contents + offset - 1);
860 return (val & 0xc7) == 5;
862 case R_X86_64_GOTPC32_TLSDESC:
863 /* Check transition from GDesc access model:
864 leaq x@tlsdesc(%rip), %rax
866 Make sure it's a leaq adding rip to a 32-bit offset
867 into any register, although it's probably almost always
868 going to be rax. */
870 if (offset < 3 || (offset + 4) > sec->size)
871 return FALSE;
873 val = bfd_get_8 (abfd, contents + offset - 3);
874 if ((val & 0xfb) != 0x48)
875 return FALSE;
877 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d)
878 return FALSE;
880 val = bfd_get_8 (abfd, contents + offset - 1);
881 return (val & 0xc7) == 0x05;
883 case R_X86_64_TLSDESC_CALL:
884 /* Check transition from GDesc access model:
885 call *x@tlsdesc(%rax)
887 if (offset + 2 <= sec->size)
889 /* Make sure that it's a call *x@tlsdesc(%rax). */
890 static x86_64_opcode16 call = { { 0xff, 0x10 } };
891 return bfd_get_16 (abfd, contents + offset) == call.i;
894 return FALSE;
896 default:
897 abort ();
901 /* Return TRUE if the TLS access transition is OK or no transition
902 will be performed. Update R_TYPE if there is a transition. */
904 static bfd_boolean
905 elf64_x86_64_tls_transition (struct bfd_link_info *info, bfd *abfd,
906 asection *sec, bfd_byte *contents,
907 Elf_Internal_Shdr *symtab_hdr,
908 struct elf_link_hash_entry **sym_hashes,
909 unsigned int *r_type, int tls_type,
910 const Elf_Internal_Rela *rel,
911 const Elf_Internal_Rela *relend,
912 struct elf_link_hash_entry *h)
914 unsigned int from_type = *r_type;
915 unsigned int to_type = from_type;
916 bfd_boolean check = TRUE;
918 switch (from_type)
920 case R_X86_64_TLSGD:
921 case R_X86_64_GOTPC32_TLSDESC:
922 case R_X86_64_TLSDESC_CALL:
923 case R_X86_64_GOTTPOFF:
924 if (!info->shared)
926 if (h == NULL)
927 to_type = R_X86_64_TPOFF32;
928 else
929 to_type = R_X86_64_GOTTPOFF;
932 /* When we are called from elf64_x86_64_relocate_section,
933 CONTENTS isn't NULL and there may be additional transitions
934 based on TLS_TYPE. */
935 if (contents != NULL)
937 unsigned int new_to_type = to_type;
939 if (!info->shared
940 && h != NULL
941 && h->dynindx == -1
942 && tls_type == GOT_TLS_IE)
943 new_to_type = R_X86_64_TPOFF32;
945 if (to_type == R_X86_64_TLSGD
946 || to_type == R_X86_64_GOTPC32_TLSDESC
947 || to_type == R_X86_64_TLSDESC_CALL)
949 if (tls_type == GOT_TLS_IE)
950 new_to_type = R_X86_64_GOTTPOFF;
953 /* We checked the transition before when we were called from
954 elf64_x86_64_check_relocs. We only want to check the new
955 transition which hasn't been checked before. */
956 check = new_to_type != to_type && from_type == to_type;
957 to_type = new_to_type;
960 break;
962 case R_X86_64_TLSLD:
963 if (!info->shared)
964 to_type = R_X86_64_TPOFF32;
965 break;
967 default:
968 return TRUE;
971 /* Return TRUE if there is no transition. */
972 if (from_type == to_type)
973 return TRUE;
975 /* Check if the transition can be performed. */
976 if (check
977 && ! elf64_x86_64_check_tls_transition (abfd, sec, contents,
978 symtab_hdr, sym_hashes,
979 from_type, rel, relend))
981 reloc_howto_type *from, *to;
983 from = elf64_x86_64_rtype_to_howto (abfd, from_type);
984 to = elf64_x86_64_rtype_to_howto (abfd, to_type);
986 (*_bfd_error_handler)
987 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx "
988 "in section `%A' failed"),
989 abfd, sec, from->name, to->name,
990 h ? h->root.root.string : "a local symbol",
991 (unsigned long) rel->r_offset);
992 bfd_set_error (bfd_error_bad_value);
993 return FALSE;
996 *r_type = to_type;
997 return TRUE;
1000 /* Look through the relocs for a section during the first phase, and
1001 calculate needed space in the global offset table, procedure
1002 linkage table, and dynamic reloc sections. */
1004 static bfd_boolean
1005 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info,
1006 asection *sec,
1007 const Elf_Internal_Rela *relocs)
1009 struct elf64_x86_64_link_hash_table *htab;
1010 Elf_Internal_Shdr *symtab_hdr;
1011 struct elf_link_hash_entry **sym_hashes;
1012 const Elf_Internal_Rela *rel;
1013 const Elf_Internal_Rela *rel_end;
1014 asection *sreloc;
1016 if (info->relocatable)
1017 return TRUE;
1019 BFD_ASSERT (is_x86_64_elf (abfd));
1021 htab = elf64_x86_64_hash_table (info);
1022 symtab_hdr = &elf_symtab_hdr (abfd);
1023 sym_hashes = elf_sym_hashes (abfd);
1025 sreloc = NULL;
1027 rel_end = relocs + sec->reloc_count;
1028 for (rel = relocs; rel < rel_end; rel++)
1030 unsigned int r_type;
1031 unsigned long r_symndx;
1032 struct elf_link_hash_entry *h;
1034 r_symndx = ELF64_R_SYM (rel->r_info);
1035 r_type = ELF64_R_TYPE (rel->r_info);
1037 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
1039 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
1040 abfd, r_symndx);
1041 return FALSE;
1044 if (r_symndx < symtab_hdr->sh_info)
1045 h = NULL;
1046 else
1048 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1049 while (h->root.type == bfd_link_hash_indirect
1050 || h->root.type == bfd_link_hash_warning)
1051 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1053 /* Create the ifunc sections for static executables. If we
1054 never see an indirect function symbol nor we are building
1055 a static executable, those sections will be empty and
1056 won't appear in output. */
1057 switch (r_type)
1059 default:
1060 break;
1062 case R_X86_64_32S:
1063 case R_X86_64_32:
1064 case R_X86_64_64:
1065 case R_X86_64_PC32:
1066 case R_X86_64_PC64:
1067 case R_X86_64_PLT32:
1068 case R_X86_64_GOTPCREL:
1069 case R_X86_64_GOTPCREL64:
1070 if (htab->irelifunc == NULL && htab->iplt == NULL)
1072 if (!_bfd_elf_create_ifunc_sections (abfd, info))
1073 return FALSE;
1075 if (info->shared)
1077 htab->irelifunc = bfd_get_section_by_name (abfd,
1078 ".rela.ifunc");
1079 if (!htab->irelifunc)
1080 abort ();
1082 else
1084 htab->iplt = bfd_get_section_by_name (abfd, ".iplt");
1085 htab->irelplt = bfd_get_section_by_name (abfd,
1086 ".rela.iplt");
1087 htab->igotplt = bfd_get_section_by_name (abfd,
1088 ".igot.plt");
1089 if (!htab->iplt
1090 || !htab->irelplt
1091 || !htab->igotplt)
1092 abort ();
1095 break;
1098 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
1099 it here if it is defined in a non-shared object. */
1100 if (h->type == STT_GNU_IFUNC
1101 && h->def_regular)
1103 /* It is referenced by a non-shared object. */
1104 h->ref_regular = 1;
1106 /* STT_GNU_IFUNC symbol must go through PLT. */
1107 h->plt.refcount += 1;
1109 /* STT_GNU_IFUNC needs dynamic sections. */
1110 if (htab->elf.dynobj == NULL)
1111 htab->elf.dynobj = abfd;
1113 switch (r_type)
1115 default:
1116 (*_bfd_error_handler)
1117 (_("%B: relocation %s against STT_GNU_IFUNC "
1118 "symbol `%s' isn't handled by %s"), abfd,
1119 x86_64_elf_howto_table[r_type].name,
1120 h->root.root.string, __FUNCTION__);
1121 bfd_set_error (bfd_error_bad_value);
1122 return FALSE;
1124 case R_X86_64_64:
1125 h->non_got_ref = 1;
1126 h->pointer_equality_needed = 1;
1127 if (info->shared)
1129 struct elf64_x86_64_dyn_relocs *p;
1130 struct elf64_x86_64_dyn_relocs **head;
1132 /* We must copy these reloc types into the output
1133 file. Create a reloc section in dynobj and
1134 make room for this reloc. */
1135 if (sreloc == NULL)
1137 if (htab->elf.dynobj == NULL)
1138 htab->elf.dynobj = abfd;
1140 sreloc = _bfd_elf_make_dynamic_reloc_section
1141 (sec, htab->elf.dynobj, 3, abfd, TRUE);
1143 if (sreloc == NULL)
1144 return FALSE;
1147 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1148 p = *head;
1149 if (p == NULL || p->sec != sec)
1151 bfd_size_type amt = sizeof *p;
1153 p = ((struct elf64_x86_64_dyn_relocs *)
1154 bfd_alloc (htab->elf.dynobj, amt));
1155 if (p == NULL)
1156 return FALSE;
1157 p->next = *head;
1158 *head = p;
1159 p->sec = sec;
1160 p->count = 0;
1161 p->pc_count = 0;
1163 p->count += 1;
1165 break;
1167 case R_X86_64_32S:
1168 case R_X86_64_32:
1169 case R_X86_64_PC32:
1170 case R_X86_64_PC64:
1171 h->non_got_ref = 1;
1172 if (r_type != R_X86_64_PC32
1173 && r_type != R_X86_64_PC64)
1174 h->pointer_equality_needed = 1;
1175 break;
1177 case R_X86_64_PLT32:
1178 break;
1180 case R_X86_64_GOTPCREL:
1181 case R_X86_64_GOTPCREL64:
1182 h->got.refcount += 1;
1183 if (htab->sgot == NULL
1184 && !elf64_x86_64_create_got_section (htab->elf.dynobj,
1185 info))
1186 return FALSE;
1187 break;
1190 continue;
1194 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1195 symtab_hdr, sym_hashes,
1196 &r_type, GOT_UNKNOWN,
1197 rel, rel_end, h))
1198 return FALSE;
1200 switch (r_type)
1202 case R_X86_64_TLSLD:
1203 htab->tls_ld_got.refcount += 1;
1204 goto create_got;
1206 case R_X86_64_TPOFF32:
1207 if (info->shared)
1209 (*_bfd_error_handler)
1210 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1211 abfd,
1212 x86_64_elf_howto_table[r_type].name,
1213 (h) ? h->root.root.string : "a local symbol");
1214 bfd_set_error (bfd_error_bad_value);
1215 return FALSE;
1217 break;
1219 case R_X86_64_GOTTPOFF:
1220 if (info->shared)
1221 info->flags |= DF_STATIC_TLS;
1222 /* Fall through */
1224 case R_X86_64_GOT32:
1225 case R_X86_64_GOTPCREL:
1226 case R_X86_64_TLSGD:
1227 case R_X86_64_GOT64:
1228 case R_X86_64_GOTPCREL64:
1229 case R_X86_64_GOTPLT64:
1230 case R_X86_64_GOTPC32_TLSDESC:
1231 case R_X86_64_TLSDESC_CALL:
1232 /* This symbol requires a global offset table entry. */
1234 int tls_type, old_tls_type;
1236 switch (r_type)
1238 default: tls_type = GOT_NORMAL; break;
1239 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
1240 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
1241 case R_X86_64_GOTPC32_TLSDESC:
1242 case R_X86_64_TLSDESC_CALL:
1243 tls_type = GOT_TLS_GDESC; break;
1246 if (h != NULL)
1248 if (r_type == R_X86_64_GOTPLT64)
1250 /* This relocation indicates that we also need
1251 a PLT entry, as this is a function. We don't need
1252 a PLT entry for local symbols. */
1253 h->needs_plt = 1;
1254 h->plt.refcount += 1;
1256 h->got.refcount += 1;
1257 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1259 else
1261 bfd_signed_vma *local_got_refcounts;
1263 /* This is a global offset table entry for a local symbol. */
1264 local_got_refcounts = elf_local_got_refcounts (abfd);
1265 if (local_got_refcounts == NULL)
1267 bfd_size_type size;
1269 size = symtab_hdr->sh_info;
1270 size *= sizeof (bfd_signed_vma)
1271 + sizeof (bfd_vma) + sizeof (char);
1272 local_got_refcounts = ((bfd_signed_vma *)
1273 bfd_zalloc (abfd, size));
1274 if (local_got_refcounts == NULL)
1275 return FALSE;
1276 elf_local_got_refcounts (abfd) = local_got_refcounts;
1277 elf64_x86_64_local_tlsdesc_gotent (abfd)
1278 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info);
1279 elf64_x86_64_local_got_tls_type (abfd)
1280 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info);
1282 local_got_refcounts[r_symndx] += 1;
1283 old_tls_type
1284 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
1287 /* If a TLS symbol is accessed using IE at least once,
1288 there is no point to use dynamic model for it. */
1289 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
1290 && (! GOT_TLS_GD_ANY_P (old_tls_type)
1291 || tls_type != GOT_TLS_IE))
1293 if (old_tls_type == GOT_TLS_IE && GOT_TLS_GD_ANY_P (tls_type))
1294 tls_type = old_tls_type;
1295 else if (GOT_TLS_GD_ANY_P (old_tls_type)
1296 && GOT_TLS_GD_ANY_P (tls_type))
1297 tls_type |= old_tls_type;
1298 else
1300 (*_bfd_error_handler)
1301 (_("%B: '%s' accessed both as normal and thread local symbol"),
1302 abfd, h ? h->root.root.string : "<local>");
1303 return FALSE;
1307 if (old_tls_type != tls_type)
1309 if (h != NULL)
1310 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
1311 else
1312 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
1315 /* Fall through */
1317 case R_X86_64_GOTOFF64:
1318 case R_X86_64_GOTPC32:
1319 case R_X86_64_GOTPC64:
1320 create_got:
1321 if (htab->sgot == NULL)
1323 if (htab->elf.dynobj == NULL)
1324 htab->elf.dynobj = abfd;
1325 if (!elf64_x86_64_create_got_section (htab->elf.dynobj,
1326 info))
1327 return FALSE;
1329 break;
1331 case R_X86_64_PLT32:
1332 /* This symbol requires a procedure linkage table entry. We
1333 actually build the entry in adjust_dynamic_symbol,
1334 because this might be a case of linking PIC code which is
1335 never referenced by a dynamic object, in which case we
1336 don't need to generate a procedure linkage table entry
1337 after all. */
1339 /* If this is a local symbol, we resolve it directly without
1340 creating a procedure linkage table entry. */
1341 if (h == NULL)
1342 continue;
1344 h->needs_plt = 1;
1345 h->plt.refcount += 1;
1346 break;
1348 case R_X86_64_PLTOFF64:
1349 /* This tries to form the 'address' of a function relative
1350 to GOT. For global symbols we need a PLT entry. */
1351 if (h != NULL)
1353 h->needs_plt = 1;
1354 h->plt.refcount += 1;
1356 goto create_got;
1358 case R_X86_64_8:
1359 case R_X86_64_16:
1360 case R_X86_64_32:
1361 case R_X86_64_32S:
1362 /* Let's help debug shared library creation. These relocs
1363 cannot be used in shared libs. Don't error out for
1364 sections we don't care about, such as debug sections or
1365 non-constant sections. */
1366 if (info->shared
1367 && (sec->flags & SEC_ALLOC) != 0
1368 && (sec->flags & SEC_READONLY) != 0)
1370 (*_bfd_error_handler)
1371 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1372 abfd,
1373 x86_64_elf_howto_table[r_type].name,
1374 (h) ? h->root.root.string : "a local symbol");
1375 bfd_set_error (bfd_error_bad_value);
1376 return FALSE;
1378 /* Fall through. */
1380 case R_X86_64_PC8:
1381 case R_X86_64_PC16:
1382 case R_X86_64_PC32:
1383 case R_X86_64_PC64:
1384 case R_X86_64_64:
1385 if (h != NULL && info->executable)
1387 /* If this reloc is in a read-only section, we might
1388 need a copy reloc. We can't check reliably at this
1389 stage whether the section is read-only, as input
1390 sections have not yet been mapped to output sections.
1391 Tentatively set the flag for now, and correct in
1392 adjust_dynamic_symbol. */
1393 h->non_got_ref = 1;
1395 /* We may need a .plt entry if the function this reloc
1396 refers to is in a shared lib. */
1397 h->plt.refcount += 1;
1398 if (r_type != R_X86_64_PC32 && r_type != R_X86_64_PC64)
1399 h->pointer_equality_needed = 1;
1402 /* If we are creating a shared library, and this is a reloc
1403 against a global symbol, or a non PC relative reloc
1404 against a local symbol, then we need to copy the reloc
1405 into the shared library. However, if we are linking with
1406 -Bsymbolic, we do not need to copy a reloc against a
1407 global symbol which is defined in an object we are
1408 including in the link (i.e., DEF_REGULAR is set). At
1409 this point we have not seen all the input files, so it is
1410 possible that DEF_REGULAR is not set now but will be set
1411 later (it is never cleared). In case of a weak definition,
1412 DEF_REGULAR may be cleared later by a strong definition in
1413 a shared library. We account for that possibility below by
1414 storing information in the relocs_copied field of the hash
1415 table entry. A similar situation occurs when creating
1416 shared libraries and symbol visibility changes render the
1417 symbol local.
1419 If on the other hand, we are creating an executable, we
1420 may need to keep relocations for symbols satisfied by a
1421 dynamic library if we manage to avoid copy relocs for the
1422 symbol. */
1423 if ((info->shared
1424 && (sec->flags & SEC_ALLOC) != 0
1425 && (! IS_X86_64_PCREL_TYPE (r_type)
1426 || (h != NULL
1427 && (! SYMBOLIC_BIND (info, h)
1428 || h->root.type == bfd_link_hash_defweak
1429 || !h->def_regular))))
1430 || (ELIMINATE_COPY_RELOCS
1431 && !info->shared
1432 && (sec->flags & SEC_ALLOC) != 0
1433 && h != NULL
1434 && (h->root.type == bfd_link_hash_defweak
1435 || !h->def_regular)))
1437 struct elf64_x86_64_dyn_relocs *p;
1438 struct elf64_x86_64_dyn_relocs **head;
1440 /* We must copy these reloc types into the output file.
1441 Create a reloc section in dynobj and make room for
1442 this reloc. */
1443 if (sreloc == NULL)
1445 if (htab->elf.dynobj == NULL)
1446 htab->elf.dynobj = abfd;
1448 sreloc = _bfd_elf_make_dynamic_reloc_section
1449 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
1451 if (sreloc == NULL)
1452 return FALSE;
1455 /* If this is a global symbol, we count the number of
1456 relocations we need for this symbol. */
1457 if (h != NULL)
1459 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
1461 else
1463 void **vpp;
1464 /* Track dynamic relocs needed for local syms too.
1465 We really need local syms available to do this
1466 easily. Oh well. */
1468 asection *s;
1469 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
1470 sec, r_symndx);
1471 if (s == NULL)
1472 return FALSE;
1474 /* Beware of type punned pointers vs strict aliasing
1475 rules. */
1476 vpp = &(elf_section_data (s)->local_dynrel);
1477 head = (struct elf64_x86_64_dyn_relocs **)vpp;
1480 p = *head;
1481 if (p == NULL || p->sec != sec)
1483 bfd_size_type amt = sizeof *p;
1485 p = ((struct elf64_x86_64_dyn_relocs *)
1486 bfd_alloc (htab->elf.dynobj, amt));
1487 if (p == NULL)
1488 return FALSE;
1489 p->next = *head;
1490 *head = p;
1491 p->sec = sec;
1492 p->count = 0;
1493 p->pc_count = 0;
1496 p->count += 1;
1497 if (IS_X86_64_PCREL_TYPE (r_type))
1498 p->pc_count += 1;
1500 break;
1502 /* This relocation describes the C++ object vtable hierarchy.
1503 Reconstruct it for later use during GC. */
1504 case R_X86_64_GNU_VTINHERIT:
1505 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1506 return FALSE;
1507 break;
1509 /* This relocation describes which C++ vtable entries are actually
1510 used. Record for later use during GC. */
1511 case R_X86_64_GNU_VTENTRY:
1512 BFD_ASSERT (h != NULL);
1513 if (h != NULL
1514 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1515 return FALSE;
1516 break;
1518 default:
1519 break;
1523 return TRUE;
1526 /* Return the section that should be marked against GC for a given
1527 relocation. */
1529 static asection *
1530 elf64_x86_64_gc_mark_hook (asection *sec,
1531 struct bfd_link_info *info,
1532 Elf_Internal_Rela *rel,
1533 struct elf_link_hash_entry *h,
1534 Elf_Internal_Sym *sym)
1536 if (h != NULL)
1537 switch (ELF64_R_TYPE (rel->r_info))
1539 case R_X86_64_GNU_VTINHERIT:
1540 case R_X86_64_GNU_VTENTRY:
1541 return NULL;
1544 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
1547 /* Update the got entry reference counts for the section being removed. */
1549 static bfd_boolean
1550 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1551 asection *sec,
1552 const Elf_Internal_Rela *relocs)
1554 Elf_Internal_Shdr *symtab_hdr;
1555 struct elf_link_hash_entry **sym_hashes;
1556 bfd_signed_vma *local_got_refcounts;
1557 const Elf_Internal_Rela *rel, *relend;
1559 if (info->relocatable)
1560 return TRUE;
1562 elf_section_data (sec)->local_dynrel = NULL;
1564 symtab_hdr = &elf_symtab_hdr (abfd);
1565 sym_hashes = elf_sym_hashes (abfd);
1566 local_got_refcounts = elf_local_got_refcounts (abfd);
1568 relend = relocs + sec->reloc_count;
1569 for (rel = relocs; rel < relend; rel++)
1571 unsigned long r_symndx;
1572 unsigned int r_type;
1573 struct elf_link_hash_entry *h = NULL;
1575 r_symndx = ELF64_R_SYM (rel->r_info);
1576 if (r_symndx >= symtab_hdr->sh_info)
1578 struct elf64_x86_64_link_hash_entry *eh;
1579 struct elf64_x86_64_dyn_relocs **pp;
1580 struct elf64_x86_64_dyn_relocs *p;
1582 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1583 while (h->root.type == bfd_link_hash_indirect
1584 || h->root.type == bfd_link_hash_warning)
1585 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1586 eh = (struct elf64_x86_64_link_hash_entry *) h;
1588 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1589 if (p->sec == sec)
1591 /* Everything must go for SEC. */
1592 *pp = p->next;
1593 break;
1597 r_type = ELF64_R_TYPE (rel->r_info);
1598 if (! elf64_x86_64_tls_transition (info, abfd, sec, NULL,
1599 symtab_hdr, sym_hashes,
1600 &r_type, GOT_UNKNOWN,
1601 rel, relend, h))
1602 return FALSE;
1604 switch (r_type)
1606 case R_X86_64_TLSLD:
1607 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1608 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1609 break;
1611 case R_X86_64_TLSGD:
1612 case R_X86_64_GOTPC32_TLSDESC:
1613 case R_X86_64_TLSDESC_CALL:
1614 case R_X86_64_GOTTPOFF:
1615 case R_X86_64_GOT32:
1616 case R_X86_64_GOTPCREL:
1617 case R_X86_64_GOT64:
1618 case R_X86_64_GOTPCREL64:
1619 case R_X86_64_GOTPLT64:
1620 if (h != NULL)
1622 if (r_type == R_X86_64_GOTPLT64 && h->plt.refcount > 0)
1623 h->plt.refcount -= 1;
1624 if (h->got.refcount > 0)
1625 h->got.refcount -= 1;
1627 else if (local_got_refcounts != NULL)
1629 if (local_got_refcounts[r_symndx] > 0)
1630 local_got_refcounts[r_symndx] -= 1;
1632 break;
1634 case R_X86_64_8:
1635 case R_X86_64_16:
1636 case R_X86_64_32:
1637 case R_X86_64_64:
1638 case R_X86_64_32S:
1639 case R_X86_64_PC8:
1640 case R_X86_64_PC16:
1641 case R_X86_64_PC32:
1642 case R_X86_64_PC64:
1643 if (info->shared)
1644 break;
1645 /* Fall thru */
1647 case R_X86_64_PLT32:
1648 case R_X86_64_PLTOFF64:
1649 if (h != NULL)
1651 if (h->plt.refcount > 0)
1652 h->plt.refcount -= 1;
1654 break;
1656 default:
1657 break;
1661 return TRUE;
1664 /* Adjust a symbol defined by a dynamic object and referenced by a
1665 regular object. The current definition is in some section of the
1666 dynamic object, but we're not including those sections. We have to
1667 change the definition to something the rest of the link can
1668 understand. */
1670 static bfd_boolean
1671 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1672 struct elf_link_hash_entry *h)
1674 struct elf64_x86_64_link_hash_table *htab;
1675 asection *s;
1677 /* STT_GNU_IFUNC symbol must go through PLT. */
1678 if (h->type == STT_GNU_IFUNC)
1680 if (h->plt.refcount <= 0)
1682 h->plt.offset = (bfd_vma) -1;
1683 h->needs_plt = 0;
1685 return TRUE;
1688 /* If this is a function, put it in the procedure linkage table. We
1689 will fill in the contents of the procedure linkage table later,
1690 when we know the address of the .got section. */
1691 if (h->type == STT_FUNC
1692 || h->needs_plt)
1694 if (h->plt.refcount <= 0
1695 || SYMBOL_CALLS_LOCAL (info, h)
1696 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1697 && h->root.type == bfd_link_hash_undefweak))
1699 /* This case can occur if we saw a PLT32 reloc in an input
1700 file, but the symbol was never referred to by a dynamic
1701 object, or if all references were garbage collected. In
1702 such a case, we don't actually need to build a procedure
1703 linkage table, and we can just do a PC32 reloc instead. */
1704 h->plt.offset = (bfd_vma) -1;
1705 h->needs_plt = 0;
1708 return TRUE;
1710 else
1711 /* It's possible that we incorrectly decided a .plt reloc was
1712 needed for an R_X86_64_PC32 reloc to a non-function sym in
1713 check_relocs. We can't decide accurately between function and
1714 non-function syms in check-relocs; Objects loaded later in
1715 the link may change h->type. So fix it now. */
1716 h->plt.offset = (bfd_vma) -1;
1718 /* If this is a weak symbol, and there is a real definition, the
1719 processor independent code will have arranged for us to see the
1720 real definition first, and we can just use the same value. */
1721 if (h->u.weakdef != NULL)
1723 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1724 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1725 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1726 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1727 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1728 h->non_got_ref = h->u.weakdef->non_got_ref;
1729 return TRUE;
1732 /* This is a reference to a symbol defined by a dynamic object which
1733 is not a function. */
1735 /* If we are creating a shared library, we must presume that the
1736 only references to the symbol are via the global offset table.
1737 For such cases we need not do anything here; the relocations will
1738 be handled correctly by relocate_section. */
1739 if (info->shared)
1740 return TRUE;
1742 /* If there are no references to this symbol that do not use the
1743 GOT, we don't need to generate a copy reloc. */
1744 if (!h->non_got_ref)
1745 return TRUE;
1747 /* If -z nocopyreloc was given, we won't generate them either. */
1748 if (info->nocopyreloc)
1750 h->non_got_ref = 0;
1751 return TRUE;
1754 if (ELIMINATE_COPY_RELOCS)
1756 struct elf64_x86_64_link_hash_entry * eh;
1757 struct elf64_x86_64_dyn_relocs *p;
1759 eh = (struct elf64_x86_64_link_hash_entry *) h;
1760 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1762 s = p->sec->output_section;
1763 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1764 break;
1767 /* If we didn't find any dynamic relocs in read-only sections, then
1768 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1769 if (p == NULL)
1771 h->non_got_ref = 0;
1772 return TRUE;
1776 if (h->size == 0)
1778 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
1779 h->root.root.string);
1780 return TRUE;
1783 /* We must allocate the symbol in our .dynbss section, which will
1784 become part of the .bss section of the executable. There will be
1785 an entry for this symbol in the .dynsym section. The dynamic
1786 object will contain position independent code, so all references
1787 from the dynamic object to this symbol will go through the global
1788 offset table. The dynamic linker will use the .dynsym entry to
1789 determine the address it must put in the global offset table, so
1790 both the dynamic object and the regular object will refer to the
1791 same memory location for the variable. */
1793 htab = elf64_x86_64_hash_table (info);
1795 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1796 to copy the initial value out of the dynamic object and into the
1797 runtime process image. */
1798 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1800 htab->srelbss->size += sizeof (Elf64_External_Rela);
1801 h->needs_copy = 1;
1804 s = htab->sdynbss;
1806 return _bfd_elf_adjust_dynamic_copy (h, s);
1809 /* Allocate space in .plt, .got and associated reloc sections for
1810 dynamic relocs. */
1812 static bfd_boolean
1813 elf64_x86_64_allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1815 struct bfd_link_info *info;
1816 struct elf64_x86_64_link_hash_table *htab;
1817 struct elf64_x86_64_link_hash_entry *eh;
1818 struct elf64_x86_64_dyn_relocs *p;
1820 if (h->root.type == bfd_link_hash_indirect)
1821 return TRUE;
1823 if (h->root.type == bfd_link_hash_warning)
1824 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1825 eh = (struct elf64_x86_64_link_hash_entry *) h;
1827 info = (struct bfd_link_info *) inf;
1828 htab = elf64_x86_64_hash_table (info);
1830 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it
1831 here if it is defined and referenced in a non-shared object. */
1832 if (h->type == STT_GNU_IFUNC
1833 && h->def_regular)
1835 asection *plt, *gotplt, *relplt;
1837 /* Return and discard space for dynamic relocations against it if
1838 it is never referenced in a non-shared object. */
1839 if (!h->ref_regular)
1841 if (h->plt.refcount > 0
1842 || h->got.refcount > 0)
1843 abort ();
1844 h->got.offset = (bfd_vma) -1;
1845 eh->dyn_relocs = NULL;
1846 return TRUE;
1849 /* When building a static executable, use .iplt, .igot.plt and
1850 .rela.iplt sections for STT_GNU_IFUNC symbols. */
1851 if (htab->splt != NULL)
1853 plt = htab->splt;
1854 gotplt = htab->sgotplt;
1855 relplt = htab->srelplt;
1857 /* If this is the first .plt entry, make room for the special
1858 first entry. */
1859 if (plt->size == 0)
1860 plt->size += PLT_ENTRY_SIZE;
1862 else
1864 plt = htab->iplt;
1865 gotplt = htab->igotplt;
1866 relplt = htab->irelplt;
1869 /* Don't update value of STT_GNU_IFUNC symbol to PLT. We need
1870 the original value for R_X86_64_IRELATIVE. */
1871 h->plt.offset = plt->size;
1873 /* Make room for this entry in the .plt/.iplt section. */
1874 plt->size += PLT_ENTRY_SIZE;
1876 /* We also need to make an entry in the .got.plt/.got.iplt
1877 section, which will be placed in the .got section by the
1878 linker script. */
1879 gotplt->size += GOT_ENTRY_SIZE;
1881 /* We also need to make an entry in the .rela.plt/.rela.iplt
1882 section. */
1883 relplt->size += sizeof (Elf64_External_Rela);
1884 relplt->reloc_count++;
1886 /* We need dynamic relocation for STT_GNU_IFUNC symbol only
1887 when there is a non-GOT reference in a shared object. */
1888 if (!info->shared
1889 || !h->non_got_ref)
1890 eh->dyn_relocs = NULL;
1892 /* Finally, allocate space. */
1893 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1894 htab->irelifunc->size += p->count * sizeof (Elf64_External_Rela);
1896 /* For STT_GNU_IFUNC symbol, .got.plt has the real function
1897 addres and .got has the PLT entry adddress. We will load
1898 the GOT entry with the PLT entry in finish_dynamic_symbol if
1899 it is used. For branch, it uses .got.plt. For symbol value,
1900 1. Use .got.plt in a shared object if it is forced local or
1901 not dynamic.
1902 2. Use .got.plt in a non-shared object if pointer equality
1903 isn't needed.
1904 3. Use .got.plt if .got isn't used.
1905 4. Otherwise use .got so that it can be shared among different
1906 objects at run-time.
1907 We only need to relocate .got entry in shared object. */
1908 if ((info->shared
1909 && (h->dynindx == -1
1910 || h->forced_local))
1911 || (!info->shared
1912 && !h->pointer_equality_needed)
1913 || htab->sgot == NULL)
1915 /* Use .got.plt. */
1916 h->got.offset = (bfd_vma) -1;
1918 else
1920 h->got.offset = htab->sgot->size;
1921 htab->sgot->size += GOT_ENTRY_SIZE;
1922 if (info->shared)
1923 htab->srelgot->size += sizeof (Elf64_External_Rela);
1926 return TRUE;
1928 else if (htab->elf.dynamic_sections_created
1929 && h->plt.refcount > 0)
1931 /* Make sure this symbol is output as a dynamic symbol.
1932 Undefined weak syms won't yet be marked as dynamic. */
1933 if (h->dynindx == -1
1934 && !h->forced_local)
1936 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1937 return FALSE;
1940 if (info->shared
1941 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1943 asection *s = htab->splt;
1945 /* If this is the first .plt entry, make room for the special
1946 first entry. */
1947 if (s->size == 0)
1948 s->size += PLT_ENTRY_SIZE;
1950 h->plt.offset = s->size;
1952 /* If this symbol is not defined in a regular file, and we are
1953 not generating a shared library, then set the symbol to this
1954 location in the .plt. This is required to make function
1955 pointers compare as equal between the normal executable and
1956 the shared library. */
1957 if (! info->shared
1958 && !h->def_regular)
1960 h->root.u.def.section = s;
1961 h->root.u.def.value = h->plt.offset;
1964 /* Make room for this entry. */
1965 s->size += PLT_ENTRY_SIZE;
1967 /* We also need to make an entry in the .got.plt section, which
1968 will be placed in the .got section by the linker script. */
1969 htab->sgotplt->size += GOT_ENTRY_SIZE;
1971 /* We also need to make an entry in the .rela.plt section. */
1972 htab->srelplt->size += sizeof (Elf64_External_Rela);
1973 htab->srelplt->reloc_count++;
1975 else
1977 h->plt.offset = (bfd_vma) -1;
1978 h->needs_plt = 0;
1981 else
1983 h->plt.offset = (bfd_vma) -1;
1984 h->needs_plt = 0;
1987 eh->tlsdesc_got = (bfd_vma) -1;
1989 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1990 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1991 if (h->got.refcount > 0
1992 && !info->shared
1993 && h->dynindx == -1
1994 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1996 h->got.offset = (bfd_vma) -1;
1998 else if (h->got.refcount > 0)
2000 asection *s;
2001 bfd_boolean dyn;
2002 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2004 /* Make sure this symbol is output as a dynamic symbol.
2005 Undefined weak syms won't yet be marked as dynamic. */
2006 if (h->dynindx == -1
2007 && !h->forced_local)
2009 if (! bfd_elf_link_record_dynamic_symbol (info, h))
2010 return FALSE;
2013 if (GOT_TLS_GDESC_P (tls_type))
2015 eh->tlsdesc_got = htab->sgotplt->size
2016 - elf64_x86_64_compute_jump_table_size (htab);
2017 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
2018 h->got.offset = (bfd_vma) -2;
2020 if (! GOT_TLS_GDESC_P (tls_type)
2021 || GOT_TLS_GD_P (tls_type))
2023 s = htab->sgot;
2024 h->got.offset = s->size;
2025 s->size += GOT_ENTRY_SIZE;
2026 if (GOT_TLS_GD_P (tls_type))
2027 s->size += GOT_ENTRY_SIZE;
2029 dyn = htab->elf.dynamic_sections_created;
2030 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
2031 and two if global.
2032 R_X86_64_GOTTPOFF needs one dynamic relocation. */
2033 if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1)
2034 || tls_type == GOT_TLS_IE)
2035 htab->srelgot->size += sizeof (Elf64_External_Rela);
2036 else if (GOT_TLS_GD_P (tls_type))
2037 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
2038 else if (! GOT_TLS_GDESC_P (tls_type)
2039 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2040 || h->root.type != bfd_link_hash_undefweak)
2041 && (info->shared
2042 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2043 htab->srelgot->size += sizeof (Elf64_External_Rela);
2044 if (GOT_TLS_GDESC_P (tls_type))
2046 htab->srelplt->size += sizeof (Elf64_External_Rela);
2047 htab->tlsdesc_plt = (bfd_vma) -1;
2050 else
2051 h->got.offset = (bfd_vma) -1;
2053 if (eh->dyn_relocs == NULL)
2054 return TRUE;
2056 /* In the shared -Bsymbolic case, discard space allocated for
2057 dynamic pc-relative relocs against symbols which turn out to be
2058 defined in regular objects. For the normal shared case, discard
2059 space for pc-relative relocs that have become local due to symbol
2060 visibility changes. */
2062 if (info->shared)
2064 /* Relocs that use pc_count are those that appear on a call
2065 insn, or certain REL relocs that can generated via assembly.
2066 We want calls to protected symbols to resolve directly to the
2067 function rather than going via the plt. If people want
2068 function pointer comparisons to work as expected then they
2069 should avoid writing weird assembly. */
2070 if (SYMBOL_CALLS_LOCAL (info, h))
2072 struct elf64_x86_64_dyn_relocs **pp;
2074 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
2076 p->count -= p->pc_count;
2077 p->pc_count = 0;
2078 if (p->count == 0)
2079 *pp = p->next;
2080 else
2081 pp = &p->next;
2085 /* Also discard relocs on undefined weak syms with non-default
2086 visibility. */
2087 if (eh->dyn_relocs != NULL
2088 && h->root.type == bfd_link_hash_undefweak)
2090 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
2091 eh->dyn_relocs = NULL;
2093 /* Make sure undefined weak symbols are output as a dynamic
2094 symbol in PIEs. */
2095 else if (h->dynindx == -1
2096 && ! h->forced_local
2097 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2098 return FALSE;
2102 else if (ELIMINATE_COPY_RELOCS)
2104 /* For the non-shared case, discard space for relocs against
2105 symbols which turn out to need copy relocs or are not
2106 dynamic. */
2108 if (!h->non_got_ref
2109 && ((h->def_dynamic
2110 && !h->def_regular)
2111 || (htab->elf.dynamic_sections_created
2112 && (h->root.type == bfd_link_hash_undefweak
2113 || h->root.type == bfd_link_hash_undefined))))
2115 /* Make sure this symbol is output as a dynamic symbol.
2116 Undefined weak syms won't yet be marked as dynamic. */
2117 if (h->dynindx == -1
2118 && ! h->forced_local
2119 && ! bfd_elf_link_record_dynamic_symbol (info, h))
2120 return FALSE;
2122 /* If that succeeded, we know we'll be keeping all the
2123 relocs. */
2124 if (h->dynindx != -1)
2125 goto keep;
2128 eh->dyn_relocs = NULL;
2130 keep: ;
2133 /* Finally, allocate space. */
2134 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2136 asection * sreloc;
2138 sreloc = elf_section_data (p->sec)->sreloc;
2140 BFD_ASSERT (sreloc != NULL);
2142 sreloc->size += p->count * sizeof (Elf64_External_Rela);
2145 return TRUE;
2148 /* Find any dynamic relocs that apply to read-only sections. */
2150 static bfd_boolean
2151 elf64_x86_64_readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
2153 struct elf64_x86_64_link_hash_entry *eh;
2154 struct elf64_x86_64_dyn_relocs *p;
2156 if (h->root.type == bfd_link_hash_warning)
2157 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2159 eh = (struct elf64_x86_64_link_hash_entry *) h;
2160 for (p = eh->dyn_relocs; p != NULL; p = p->next)
2162 asection *s = p->sec->output_section;
2164 if (s != NULL && (s->flags & SEC_READONLY) != 0)
2166 struct bfd_link_info *info = (struct bfd_link_info *) inf;
2168 info->flags |= DF_TEXTREL;
2170 /* Not an error, just cut short the traversal. */
2171 return FALSE;
2174 return TRUE;
2177 /* Set the sizes of the dynamic sections. */
2179 static bfd_boolean
2180 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2181 struct bfd_link_info *info)
2183 struct elf64_x86_64_link_hash_table *htab;
2184 bfd *dynobj;
2185 asection *s;
2186 bfd_boolean relocs;
2187 bfd *ibfd;
2189 htab = elf64_x86_64_hash_table (info);
2190 dynobj = htab->elf.dynobj;
2191 if (dynobj == NULL)
2192 abort ();
2194 if (htab->elf.dynamic_sections_created)
2196 /* Set the contents of the .interp section to the interpreter. */
2197 if (info->executable)
2199 s = bfd_get_section_by_name (dynobj, ".interp");
2200 if (s == NULL)
2201 abort ();
2202 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2203 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2207 /* Set up .got offsets for local syms, and space for local dynamic
2208 relocs. */
2209 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
2211 bfd_signed_vma *local_got;
2212 bfd_signed_vma *end_local_got;
2213 char *local_tls_type;
2214 bfd_vma *local_tlsdesc_gotent;
2215 bfd_size_type locsymcount;
2216 Elf_Internal_Shdr *symtab_hdr;
2217 asection *srel;
2219 if (! is_x86_64_elf (ibfd))
2220 continue;
2222 for (s = ibfd->sections; s != NULL; s = s->next)
2224 struct elf64_x86_64_dyn_relocs *p;
2226 for (p = (struct elf64_x86_64_dyn_relocs *)
2227 (elf_section_data (s)->local_dynrel);
2228 p != NULL;
2229 p = p->next)
2231 if (!bfd_is_abs_section (p->sec)
2232 && bfd_is_abs_section (p->sec->output_section))
2234 /* Input section has been discarded, either because
2235 it is a copy of a linkonce section or due to
2236 linker script /DISCARD/, so we'll be discarding
2237 the relocs too. */
2239 else if (p->count != 0)
2241 srel = elf_section_data (p->sec)->sreloc;
2242 srel->size += p->count * sizeof (Elf64_External_Rela);
2243 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
2244 info->flags |= DF_TEXTREL;
2249 local_got = elf_local_got_refcounts (ibfd);
2250 if (!local_got)
2251 continue;
2253 symtab_hdr = &elf_symtab_hdr (ibfd);
2254 locsymcount = symtab_hdr->sh_info;
2255 end_local_got = local_got + locsymcount;
2256 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
2257 local_tlsdesc_gotent = elf64_x86_64_local_tlsdesc_gotent (ibfd);
2258 s = htab->sgot;
2259 srel = htab->srelgot;
2260 for (; local_got < end_local_got;
2261 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
2263 *local_tlsdesc_gotent = (bfd_vma) -1;
2264 if (*local_got > 0)
2266 if (GOT_TLS_GDESC_P (*local_tls_type))
2268 *local_tlsdesc_gotent = htab->sgotplt->size
2269 - elf64_x86_64_compute_jump_table_size (htab);
2270 htab->sgotplt->size += 2 * GOT_ENTRY_SIZE;
2271 *local_got = (bfd_vma) -2;
2273 if (! GOT_TLS_GDESC_P (*local_tls_type)
2274 || GOT_TLS_GD_P (*local_tls_type))
2276 *local_got = s->size;
2277 s->size += GOT_ENTRY_SIZE;
2278 if (GOT_TLS_GD_P (*local_tls_type))
2279 s->size += GOT_ENTRY_SIZE;
2281 if (info->shared
2282 || GOT_TLS_GD_ANY_P (*local_tls_type)
2283 || *local_tls_type == GOT_TLS_IE)
2285 if (GOT_TLS_GDESC_P (*local_tls_type))
2287 htab->srelplt->size += sizeof (Elf64_External_Rela);
2288 htab->tlsdesc_plt = (bfd_vma) -1;
2290 if (! GOT_TLS_GDESC_P (*local_tls_type)
2291 || GOT_TLS_GD_P (*local_tls_type))
2292 srel->size += sizeof (Elf64_External_Rela);
2295 else
2296 *local_got = (bfd_vma) -1;
2300 if (htab->tls_ld_got.refcount > 0)
2302 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
2303 relocs. */
2304 htab->tls_ld_got.offset = htab->sgot->size;
2305 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
2306 htab->srelgot->size += sizeof (Elf64_External_Rela);
2308 else
2309 htab->tls_ld_got.offset = -1;
2311 /* Allocate global sym .plt and .got entries, and space for global
2312 sym dynamic relocs. */
2313 elf_link_hash_traverse (&htab->elf, elf64_x86_64_allocate_dynrelocs,
2314 info);
2316 /* For every jump slot reserved in the sgotplt, reloc_count is
2317 incremented. However, when we reserve space for TLS descriptors,
2318 it's not incremented, so in order to compute the space reserved
2319 for them, it suffices to multiply the reloc count by the jump
2320 slot size. */
2321 if (htab->srelplt)
2322 htab->sgotplt_jump_table_size
2323 = elf64_x86_64_compute_jump_table_size (htab);
2325 if (htab->tlsdesc_plt)
2327 /* If we're not using lazy TLS relocations, don't generate the
2328 PLT and GOT entries they require. */
2329 if ((info->flags & DF_BIND_NOW))
2330 htab->tlsdesc_plt = 0;
2331 else
2333 htab->tlsdesc_got = htab->sgot->size;
2334 htab->sgot->size += GOT_ENTRY_SIZE;
2335 /* Reserve room for the initial entry.
2336 FIXME: we could probably do away with it in this case. */
2337 if (htab->splt->size == 0)
2338 htab->splt->size += PLT_ENTRY_SIZE;
2339 htab->tlsdesc_plt = htab->splt->size;
2340 htab->splt->size += PLT_ENTRY_SIZE;
2344 /* We now have determined the sizes of the various dynamic sections.
2345 Allocate memory for them. */
2346 relocs = FALSE;
2347 for (s = dynobj->sections; s != NULL; s = s->next)
2349 if ((s->flags & SEC_LINKER_CREATED) == 0)
2350 continue;
2352 if (s == htab->splt
2353 || s == htab->sgot
2354 || s == htab->sgotplt
2355 || s == htab->iplt
2356 || s == htab->igotplt
2357 || s == htab->sdynbss)
2359 /* Strip this section if we don't need it; see the
2360 comment below. */
2362 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
2364 if (s->size != 0 && s != htab->srelplt)
2365 relocs = TRUE;
2367 /* We use the reloc_count field as a counter if we need
2368 to copy relocs into the output file. */
2369 if (s != htab->srelplt)
2370 s->reloc_count = 0;
2372 else
2374 /* It's not one of our sections, so don't allocate space. */
2375 continue;
2378 if (s->size == 0)
2380 /* If we don't need this section, strip it from the
2381 output file. This is mostly to handle .rela.bss and
2382 .rela.plt. We must create both sections in
2383 create_dynamic_sections, because they must be created
2384 before the linker maps input sections to output
2385 sections. The linker does that before
2386 adjust_dynamic_symbol is called, and it is that
2387 function which decides whether anything needs to go
2388 into these sections. */
2390 s->flags |= SEC_EXCLUDE;
2391 continue;
2394 if ((s->flags & SEC_HAS_CONTENTS) == 0)
2395 continue;
2397 /* Allocate memory for the section contents. We use bfd_zalloc
2398 here in case unused entries are not reclaimed before the
2399 section's contents are written out. This should not happen,
2400 but this way if it does, we get a R_X86_64_NONE reloc instead
2401 of garbage. */
2402 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
2403 if (s->contents == NULL)
2404 return FALSE;
2407 if (htab->elf.dynamic_sections_created)
2409 /* Add some entries to the .dynamic section. We fill in the
2410 values later, in elf64_x86_64_finish_dynamic_sections, but we
2411 must add the entries now so that we get the correct size for
2412 the .dynamic section. The DT_DEBUG entry is filled in by the
2413 dynamic linker and used by the debugger. */
2414 #define add_dynamic_entry(TAG, VAL) \
2415 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
2417 if (info->executable)
2419 if (!add_dynamic_entry (DT_DEBUG, 0))
2420 return FALSE;
2423 if (htab->splt->size != 0)
2425 if (!add_dynamic_entry (DT_PLTGOT, 0)
2426 || !add_dynamic_entry (DT_PLTRELSZ, 0)
2427 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
2428 || !add_dynamic_entry (DT_JMPREL, 0))
2429 return FALSE;
2431 if (htab->tlsdesc_plt
2432 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0)
2433 || !add_dynamic_entry (DT_TLSDESC_GOT, 0)))
2434 return FALSE;
2437 if (relocs)
2439 if (!add_dynamic_entry (DT_RELA, 0)
2440 || !add_dynamic_entry (DT_RELASZ, 0)
2441 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
2442 return FALSE;
2444 /* If any dynamic relocs apply to a read-only section,
2445 then we need a DT_TEXTREL entry. */
2446 if ((info->flags & DF_TEXTREL) == 0)
2447 elf_link_hash_traverse (&htab->elf,
2448 elf64_x86_64_readonly_dynrelocs,
2449 info);
2451 if ((info->flags & DF_TEXTREL) != 0)
2453 if (!add_dynamic_entry (DT_TEXTREL, 0))
2454 return FALSE;
2458 #undef add_dynamic_entry
2460 return TRUE;
2463 static bfd_boolean
2464 elf64_x86_64_always_size_sections (bfd *output_bfd,
2465 struct bfd_link_info *info)
2467 asection *tls_sec = elf_hash_table (info)->tls_sec;
2469 if (tls_sec)
2471 struct elf_link_hash_entry *tlsbase;
2473 tlsbase = elf_link_hash_lookup (elf_hash_table (info),
2474 "_TLS_MODULE_BASE_",
2475 FALSE, FALSE, FALSE);
2477 if (tlsbase && tlsbase->type == STT_TLS)
2479 struct bfd_link_hash_entry *bh = NULL;
2480 const struct elf_backend_data *bed
2481 = get_elf_backend_data (output_bfd);
2483 if (!(_bfd_generic_link_add_one_symbol
2484 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
2485 tls_sec, 0, NULL, FALSE,
2486 bed->collect, &bh)))
2487 return FALSE;
2489 elf64_x86_64_hash_table (info)->tls_module_base = bh;
2491 tlsbase = (struct elf_link_hash_entry *)bh;
2492 tlsbase->def_regular = 1;
2493 tlsbase->other = STV_HIDDEN;
2494 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE);
2498 return TRUE;
2501 /* _TLS_MODULE_BASE_ needs to be treated especially when linking
2502 executables. Rather than setting it to the beginning of the TLS
2503 section, we have to set it to the end. This function may be called
2504 multiple times, it is idempotent. */
2506 static void
2507 elf64_x86_64_set_tls_module_base (struct bfd_link_info *info)
2509 struct bfd_link_hash_entry *base;
2511 if (!info->executable)
2512 return;
2514 base = elf64_x86_64_hash_table (info)->tls_module_base;
2516 if (!base)
2517 return;
2519 base->u.def.value = elf_hash_table (info)->tls_size;
2522 /* Return the base VMA address which should be subtracted from real addresses
2523 when resolving @dtpoff relocation.
2524 This is PT_TLS segment p_vaddr. */
2526 static bfd_vma
2527 elf64_x86_64_dtpoff_base (struct bfd_link_info *info)
2529 /* If tls_sec is NULL, we should have signalled an error already. */
2530 if (elf_hash_table (info)->tls_sec == NULL)
2531 return 0;
2532 return elf_hash_table (info)->tls_sec->vma;
2535 /* Return the relocation value for @tpoff relocation
2536 if STT_TLS virtual address is ADDRESS. */
2538 static bfd_vma
2539 elf64_x86_64_tpoff (struct bfd_link_info *info, bfd_vma address)
2541 struct elf_link_hash_table *htab = elf_hash_table (info);
2543 /* If tls_segment is NULL, we should have signalled an error already. */
2544 if (htab->tls_sec == NULL)
2545 return 0;
2546 return address - htab->tls_size - htab->tls_sec->vma;
2549 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
2550 branch? */
2552 static bfd_boolean
2553 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
2555 /* Opcode Instruction
2556 0xe8 call
2557 0xe9 jump
2558 0x0f 0x8x conditional jump */
2559 return ((offset > 0
2560 && (contents [offset - 1] == 0xe8
2561 || contents [offset - 1] == 0xe9))
2562 || (offset > 1
2563 && contents [offset - 2] == 0x0f
2564 && (contents [offset - 1] & 0xf0) == 0x80));
2567 /* Relocate an x86_64 ELF section. */
2569 static bfd_boolean
2570 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
2571 bfd *input_bfd, asection *input_section,
2572 bfd_byte *contents, Elf_Internal_Rela *relocs,
2573 Elf_Internal_Sym *local_syms,
2574 asection **local_sections)
2576 struct elf64_x86_64_link_hash_table *htab;
2577 Elf_Internal_Shdr *symtab_hdr;
2578 struct elf_link_hash_entry **sym_hashes;
2579 bfd_vma *local_got_offsets;
2580 bfd_vma *local_tlsdesc_gotents;
2581 Elf_Internal_Rela *rel;
2582 Elf_Internal_Rela *relend;
2584 BFD_ASSERT (is_x86_64_elf (input_bfd));
2586 htab = elf64_x86_64_hash_table (info);
2587 symtab_hdr = &elf_symtab_hdr (input_bfd);
2588 sym_hashes = elf_sym_hashes (input_bfd);
2589 local_got_offsets = elf_local_got_offsets (input_bfd);
2590 local_tlsdesc_gotents = elf64_x86_64_local_tlsdesc_gotent (input_bfd);
2592 elf64_x86_64_set_tls_module_base (info);
2594 rel = relocs;
2595 relend = relocs + input_section->reloc_count;
2596 for (; rel < relend; rel++)
2598 unsigned int r_type;
2599 reloc_howto_type *howto;
2600 unsigned long r_symndx;
2601 struct elf_link_hash_entry *h;
2602 Elf_Internal_Sym *sym;
2603 asection *sec;
2604 bfd_vma off, offplt;
2605 bfd_vma relocation;
2606 bfd_boolean unresolved_reloc;
2607 bfd_reloc_status_type r;
2608 int tls_type;
2609 asection *base_got;
2611 r_type = ELF64_R_TYPE (rel->r_info);
2612 if (r_type == (int) R_X86_64_GNU_VTINHERIT
2613 || r_type == (int) R_X86_64_GNU_VTENTRY)
2614 continue;
2616 if (r_type >= R_X86_64_max)
2618 bfd_set_error (bfd_error_bad_value);
2619 return FALSE;
2622 howto = x86_64_elf_howto_table + r_type;
2623 r_symndx = ELF64_R_SYM (rel->r_info);
2624 h = NULL;
2625 sym = NULL;
2626 sec = NULL;
2627 unresolved_reloc = FALSE;
2628 if (r_symndx < symtab_hdr->sh_info)
2630 sym = local_syms + r_symndx;
2631 sec = local_sections[r_symndx];
2633 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2635 else
2637 bfd_boolean warned;
2639 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2640 r_symndx, symtab_hdr, sym_hashes,
2641 h, sec, relocation,
2642 unresolved_reloc, warned);
2645 if (sec != NULL && elf_discarded_section (sec))
2647 /* For relocs against symbols from removed linkonce sections,
2648 or sections discarded by a linker script, we just want the
2649 section contents zeroed. Avoid any special processing. */
2650 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
2651 rel->r_info = 0;
2652 rel->r_addend = 0;
2653 continue;
2656 if (info->relocatable)
2657 continue;
2659 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle
2660 it here if it is defined in a non-shared object. */
2661 if (h != NULL
2662 && h->type == STT_GNU_IFUNC
2663 && h->def_regular)
2665 asection *plt;
2666 bfd_vma plt_index;
2668 if ((input_section->flags & SEC_ALLOC) == 0
2669 || h->plt.offset == (bfd_vma) -1)
2670 abort ();
2672 /* STT_GNU_IFUNC symbol must go through PLT. */
2673 plt = htab->splt ? htab->splt : htab->iplt;
2674 relocation = (plt->output_section->vma
2675 + plt->output_offset + h->plt.offset);
2677 switch (r_type)
2679 default:
2680 (*_bfd_error_handler)
2681 (_("%B: relocation %s against STT_GNU_IFUNC "
2682 "symbol `%s' isn't handled by %s"), input_bfd,
2683 x86_64_elf_howto_table[r_type].name,
2684 h->root.root.string, __FUNCTION__);
2685 bfd_set_error (bfd_error_bad_value);
2686 return FALSE;
2688 case R_X86_64_32S:
2689 if (info->shared)
2690 abort ();
2691 goto do_relocation;
2693 case R_X86_64_64:
2694 if (rel->r_addend != 0)
2696 (*_bfd_error_handler)
2697 (_("%B: relocation %s against STT_GNU_IFUNC "
2698 "symbol `%s' has non-zero addend: %d"),
2699 input_bfd, x86_64_elf_howto_table[r_type].name,
2700 h->root.root.string, rel->r_addend);
2701 bfd_set_error (bfd_error_bad_value);
2702 return FALSE;
2705 /* Generate dynamic relcoation only when there is a
2706 non-GOF reference in a shared object. */
2707 if (info->shared && h->non_got_ref)
2709 Elf_Internal_Rela outrel;
2710 bfd_byte *loc;
2711 asection *sreloc;
2713 /* Need a dynamic relocation get the the real
2714 function address. */
2715 outrel.r_offset = _bfd_elf_section_offset (output_bfd,
2716 info,
2717 input_section,
2718 rel->r_offset);
2719 if (outrel.r_offset == (bfd_vma) -1
2720 || outrel.r_offset == (bfd_vma) -2)
2721 abort ();
2723 outrel.r_offset += (input_section->output_section->vma
2724 + input_section->output_offset);
2726 if (h->dynindx == -1
2727 || h->forced_local)
2729 /* This symbol is resolved locally. */
2730 outrel.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
2731 outrel.r_addend = (h->root.u.def.value
2732 + h->root.u.def.section->output_section->vma
2733 + h->root.u.def.section->output_offset);
2735 else
2737 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2738 outrel.r_addend = 0;
2741 sreloc = htab->irelifunc;
2742 loc = sreloc->contents;
2743 loc += (sreloc->reloc_count++
2744 * sizeof (Elf64_External_Rela));
2745 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2747 /* If this reloc is against an external symbol, we
2748 do not want to fiddle with the addend. Otherwise,
2749 we need to include the symbol value so that it
2750 becomes an addend for the dynamic reloc. For an
2751 internal symbol, we have updated addend. */
2752 continue;
2755 case R_X86_64_32:
2756 case R_X86_64_PC32:
2757 case R_X86_64_PC64:
2758 case R_X86_64_PLT32:
2759 goto do_relocation;
2761 case R_X86_64_GOTPCREL:
2762 case R_X86_64_GOTPCREL64:
2763 base_got = htab->sgot;
2764 off = h->got.offset;
2766 if (base_got == NULL)
2767 abort ();
2769 if (off == (bfd_vma) -1)
2771 /* We can't use h->got.offset here to save state, or
2772 even just remember the offset, as finish_dynamic_symbol
2773 would use that as offset into .got. */
2775 if (htab->splt != NULL)
2777 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2778 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2779 base_got = htab->sgotplt;
2781 else
2783 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
2784 off = plt_index * GOT_ENTRY_SIZE;
2785 base_got = htab->igotplt;
2788 if (h->dynindx == -1
2789 || h->forced_local
2790 || info->symbolic)
2792 /* This references the local defitionion. We must
2793 initialize this entry in the global offset table.
2794 Since the offset must always be a multiple of 8,
2795 we use the least significant bit to record
2796 whether we have initialized it already.
2798 When doing a dynamic link, we create a .rela.got
2799 relocation entry to initialize the value. This
2800 is done in the finish_dynamic_symbol routine. */
2801 if ((off & 1) != 0)
2802 off &= ~1;
2803 else
2805 bfd_put_64 (output_bfd, relocation,
2806 base_got->contents + off);
2807 /* Note that this is harmless for the GOTPLT64
2808 case, as -1 | 1 still is -1. */
2809 h->got.offset |= 1;
2814 relocation = (base_got->output_section->vma
2815 + base_got->output_offset + off);
2817 if (r_type != R_X86_64_GOTPCREL
2818 && r_type != R_X86_64_GOTPCREL64)
2820 asection *gotplt;
2821 if (htab->splt != NULL)
2822 gotplt = htab->sgotplt;
2823 else
2824 gotplt = htab->igotplt;
2825 relocation -= (gotplt->output_section->vma
2826 - gotplt->output_offset);
2829 goto do_relocation;
2833 /* When generating a shared object, the relocations handled here are
2834 copied into the output file to be resolved at run time. */
2835 switch (r_type)
2837 case R_X86_64_GOT32:
2838 case R_X86_64_GOT64:
2839 /* Relocation is to the entry for this symbol in the global
2840 offset table. */
2841 case R_X86_64_GOTPCREL:
2842 case R_X86_64_GOTPCREL64:
2843 /* Use global offset table entry as symbol value. */
2844 case R_X86_64_GOTPLT64:
2845 /* This is the same as GOT64 for relocation purposes, but
2846 indicates the existence of a PLT entry. The difficulty is,
2847 that we must calculate the GOT slot offset from the PLT
2848 offset, if this symbol got a PLT entry (it was global).
2849 Additionally if it's computed from the PLT entry, then that
2850 GOT offset is relative to .got.plt, not to .got. */
2851 base_got = htab->sgot;
2853 if (htab->sgot == NULL)
2854 abort ();
2856 if (h != NULL)
2858 bfd_boolean dyn;
2860 off = h->got.offset;
2861 if (h->needs_plt
2862 && h->plt.offset != (bfd_vma)-1
2863 && off == (bfd_vma)-1)
2865 /* We can't use h->got.offset here to save
2866 state, or even just remember the offset, as
2867 finish_dynamic_symbol would use that as offset into
2868 .got. */
2869 bfd_vma plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2870 off = (plt_index + 3) * GOT_ENTRY_SIZE;
2871 base_got = htab->sgotplt;
2874 dyn = htab->elf.dynamic_sections_created;
2876 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
2877 || (info->shared
2878 && SYMBOL_REFERENCES_LOCAL (info, h))
2879 || (ELF_ST_VISIBILITY (h->other)
2880 && h->root.type == bfd_link_hash_undefweak))
2882 /* This is actually a static link, or it is a -Bsymbolic
2883 link and the symbol is defined locally, or the symbol
2884 was forced to be local because of a version file. We
2885 must initialize this entry in the global offset table.
2886 Since the offset must always be a multiple of 8, we
2887 use the least significant bit to record whether we
2888 have initialized it already.
2890 When doing a dynamic link, we create a .rela.got
2891 relocation entry to initialize the value. This is
2892 done in the finish_dynamic_symbol routine. */
2893 if ((off & 1) != 0)
2894 off &= ~1;
2895 else
2897 bfd_put_64 (output_bfd, relocation,
2898 base_got->contents + off);
2899 /* Note that this is harmless for the GOTPLT64 case,
2900 as -1 | 1 still is -1. */
2901 h->got.offset |= 1;
2904 else
2905 unresolved_reloc = FALSE;
2907 else
2909 if (local_got_offsets == NULL)
2910 abort ();
2912 off = local_got_offsets[r_symndx];
2914 /* The offset must always be a multiple of 8. We use
2915 the least significant bit to record whether we have
2916 already generated the necessary reloc. */
2917 if ((off & 1) != 0)
2918 off &= ~1;
2919 else
2921 bfd_put_64 (output_bfd, relocation,
2922 base_got->contents + off);
2924 if (info->shared)
2926 asection *s;
2927 Elf_Internal_Rela outrel;
2928 bfd_byte *loc;
2930 /* We need to generate a R_X86_64_RELATIVE reloc
2931 for the dynamic linker. */
2932 s = htab->srelgot;
2933 if (s == NULL)
2934 abort ();
2936 outrel.r_offset = (base_got->output_section->vma
2937 + base_got->output_offset
2938 + off);
2939 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2940 outrel.r_addend = relocation;
2941 loc = s->contents;
2942 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
2943 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2946 local_got_offsets[r_symndx] |= 1;
2950 if (off >= (bfd_vma) -2)
2951 abort ();
2953 relocation = base_got->output_section->vma
2954 + base_got->output_offset + off;
2955 if (r_type != R_X86_64_GOTPCREL && r_type != R_X86_64_GOTPCREL64)
2956 relocation -= htab->sgotplt->output_section->vma
2957 - htab->sgotplt->output_offset;
2959 break;
2961 case R_X86_64_GOTOFF64:
2962 /* Relocation is relative to the start of the global offset
2963 table. */
2965 /* Check to make sure it isn't a protected function symbol
2966 for shared library since it may not be local when used
2967 as function address. */
2968 if (info->shared
2969 && h
2970 && h->def_regular
2971 && h->type == STT_FUNC
2972 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
2974 (*_bfd_error_handler)
2975 (_("%B: relocation R_X86_64_GOTOFF64 against protected function `%s' can not be used when making a shared object"),
2976 input_bfd, h->root.root.string);
2977 bfd_set_error (bfd_error_bad_value);
2978 return FALSE;
2981 /* Note that sgot is not involved in this
2982 calculation. We always want the start of .got.plt. If we
2983 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is
2984 permitted by the ABI, we might have to change this
2985 calculation. */
2986 relocation -= htab->sgotplt->output_section->vma
2987 + htab->sgotplt->output_offset;
2988 break;
2990 case R_X86_64_GOTPC32:
2991 case R_X86_64_GOTPC64:
2992 /* Use global offset table as symbol value. */
2993 relocation = htab->sgotplt->output_section->vma
2994 + htab->sgotplt->output_offset;
2995 unresolved_reloc = FALSE;
2996 break;
2998 case R_X86_64_PLTOFF64:
2999 /* Relocation is PLT entry relative to GOT. For local
3000 symbols it's the symbol itself relative to GOT. */
3001 if (h != NULL
3002 /* See PLT32 handling. */
3003 && h->plt.offset != (bfd_vma) -1
3004 && htab->splt != NULL)
3006 relocation = (htab->splt->output_section->vma
3007 + htab->splt->output_offset
3008 + h->plt.offset);
3009 unresolved_reloc = FALSE;
3012 relocation -= htab->sgotplt->output_section->vma
3013 + htab->sgotplt->output_offset;
3014 break;
3016 case R_X86_64_PLT32:
3017 /* Relocation is to the entry for this symbol in the
3018 procedure linkage table. */
3020 /* Resolve a PLT32 reloc against a local symbol directly,
3021 without using the procedure linkage table. */
3022 if (h == NULL)
3023 break;
3025 if (h->plt.offset == (bfd_vma) -1
3026 || htab->splt == NULL)
3028 /* We didn't make a PLT entry for this symbol. This
3029 happens when statically linking PIC code, or when
3030 using -Bsymbolic. */
3031 break;
3034 relocation = (htab->splt->output_section->vma
3035 + htab->splt->output_offset
3036 + h->plt.offset);
3037 unresolved_reloc = FALSE;
3038 break;
3040 case R_X86_64_PC8:
3041 case R_X86_64_PC16:
3042 case R_X86_64_PC32:
3043 if (info->shared
3044 && (input_section->flags & SEC_ALLOC) != 0
3045 && (input_section->flags & SEC_READONLY) != 0
3046 && h != NULL)
3048 bfd_boolean fail = FALSE;
3049 bfd_boolean branch
3050 = (r_type == R_X86_64_PC32
3051 && is_32bit_relative_branch (contents, rel->r_offset));
3053 if (SYMBOL_REFERENCES_LOCAL (info, h))
3055 /* Symbol is referenced locally. Make sure it is
3056 defined locally or for a branch. */
3057 fail = !h->def_regular && !branch;
3059 else
3061 /* Symbol isn't referenced locally. We only allow
3062 branch to symbol with non-default visibility. */
3063 fail = (!branch
3064 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT);
3067 if (fail)
3069 const char *fmt;
3070 const char *v;
3071 const char *pic = "";
3073 switch (ELF_ST_VISIBILITY (h->other))
3075 case STV_HIDDEN:
3076 v = _("hidden symbol");
3077 break;
3078 case STV_INTERNAL:
3079 v = _("internal symbol");
3080 break;
3081 case STV_PROTECTED:
3082 v = _("protected symbol");
3083 break;
3084 default:
3085 v = _("symbol");
3086 pic = _("; recompile with -fPIC");
3087 break;
3090 if (h->def_regular)
3091 fmt = _("%B: relocation %s against %s `%s' can not be used when making a shared object%s");
3092 else
3093 fmt = _("%B: relocation %s against undefined %s `%s' can not be used when making a shared object%s");
3095 (*_bfd_error_handler) (fmt, input_bfd,
3096 x86_64_elf_howto_table[r_type].name,
3097 v, h->root.root.string, pic);
3098 bfd_set_error (bfd_error_bad_value);
3099 return FALSE;
3102 /* Fall through. */
3104 case R_X86_64_8:
3105 case R_X86_64_16:
3106 case R_X86_64_32:
3107 case R_X86_64_PC64:
3108 case R_X86_64_64:
3109 /* FIXME: The ABI says the linker should make sure the value is
3110 the same when it's zeroextended to 64 bit. */
3112 if ((input_section->flags & SEC_ALLOC) == 0)
3113 break;
3115 if ((info->shared
3116 && (h == NULL
3117 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3118 || h->root.type != bfd_link_hash_undefweak)
3119 && (! IS_X86_64_PCREL_TYPE (r_type)
3120 || ! SYMBOL_CALLS_LOCAL (info, h)))
3121 || (ELIMINATE_COPY_RELOCS
3122 && !info->shared
3123 && h != NULL
3124 && h->dynindx != -1
3125 && !h->non_got_ref
3126 && ((h->def_dynamic
3127 && !h->def_regular)
3128 || h->root.type == bfd_link_hash_undefweak
3129 || h->root.type == bfd_link_hash_undefined)))
3131 Elf_Internal_Rela outrel;
3132 bfd_byte *loc;
3133 bfd_boolean skip, relocate;
3134 asection *sreloc;
3136 /* When generating a shared object, these relocations
3137 are copied into the output file to be resolved at run
3138 time. */
3139 skip = FALSE;
3140 relocate = FALSE;
3142 outrel.r_offset =
3143 _bfd_elf_section_offset (output_bfd, info, input_section,
3144 rel->r_offset);
3145 if (outrel.r_offset == (bfd_vma) -1)
3146 skip = TRUE;
3147 else if (outrel.r_offset == (bfd_vma) -2)
3148 skip = TRUE, relocate = TRUE;
3150 outrel.r_offset += (input_section->output_section->vma
3151 + input_section->output_offset);
3153 if (skip)
3154 memset (&outrel, 0, sizeof outrel);
3156 /* h->dynindx may be -1 if this symbol was marked to
3157 become local. */
3158 else if (h != NULL
3159 && h->dynindx != -1
3160 && (IS_X86_64_PCREL_TYPE (r_type)
3161 || ! info->shared
3162 || ! SYMBOLIC_BIND (info, h)
3163 || ! h->def_regular))
3165 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
3166 outrel.r_addend = rel->r_addend;
3168 else
3170 /* This symbol is local, or marked to become local. */
3171 if (r_type == R_X86_64_64)
3173 relocate = TRUE;
3174 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3175 outrel.r_addend = relocation + rel->r_addend;
3177 else
3179 long sindx;
3181 if (bfd_is_abs_section (sec))
3182 sindx = 0;
3183 else if (sec == NULL || sec->owner == NULL)
3185 bfd_set_error (bfd_error_bad_value);
3186 return FALSE;
3188 else
3190 asection *osec;
3192 /* We are turning this relocation into one
3193 against a section symbol. It would be
3194 proper to subtract the symbol's value,
3195 osec->vma, from the emitted reloc addend,
3196 but ld.so expects buggy relocs. */
3197 osec = sec->output_section;
3198 sindx = elf_section_data (osec)->dynindx;
3199 if (sindx == 0)
3201 asection *oi = htab->elf.text_index_section;
3202 sindx = elf_section_data (oi)->dynindx;
3204 BFD_ASSERT (sindx != 0);
3207 outrel.r_info = ELF64_R_INFO (sindx, r_type);
3208 outrel.r_addend = relocation + rel->r_addend;
3212 sreloc = elf_section_data (input_section)->sreloc;
3214 BFD_ASSERT (sreloc != NULL && sreloc->contents != NULL);
3216 loc = sreloc->contents;
3217 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3218 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3220 /* If this reloc is against an external symbol, we do
3221 not want to fiddle with the addend. Otherwise, we
3222 need to include the symbol value so that it becomes
3223 an addend for the dynamic reloc. */
3224 if (! relocate)
3225 continue;
3228 break;
3230 case R_X86_64_TLSGD:
3231 case R_X86_64_GOTPC32_TLSDESC:
3232 case R_X86_64_TLSDESC_CALL:
3233 case R_X86_64_GOTTPOFF:
3234 tls_type = GOT_UNKNOWN;
3235 if (h == NULL && local_got_offsets)
3236 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
3237 else if (h != NULL)
3238 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
3240 if (! elf64_x86_64_tls_transition (info, input_bfd,
3241 input_section, contents,
3242 symtab_hdr, sym_hashes,
3243 &r_type, tls_type, rel,
3244 relend, h))
3245 return FALSE;
3247 if (r_type == R_X86_64_TPOFF32)
3249 bfd_vma roff = rel->r_offset;
3251 BFD_ASSERT (! unresolved_reloc);
3253 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3255 /* GD->LE transition.
3256 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3257 .word 0x6666; rex64; call __tls_get_addr
3258 Change it into:
3259 movq %fs:0, %rax
3260 leaq foo@tpoff(%rax), %rax */
3261 memcpy (contents + roff - 4,
3262 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
3263 16);
3264 bfd_put_32 (output_bfd,
3265 elf64_x86_64_tpoff (info, relocation),
3266 contents + roff + 8);
3267 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3268 rel++;
3269 continue;
3271 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3273 /* GDesc -> LE transition.
3274 It's originally something like:
3275 leaq x@tlsdesc(%rip), %rax
3277 Change it to:
3278 movl $x@tpoff, %rax
3281 unsigned int val, type, type2;
3283 type = bfd_get_8 (input_bfd, contents + roff - 3);
3284 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3285 val = bfd_get_8 (input_bfd, contents + roff - 1);
3286 bfd_put_8 (output_bfd, 0x48 | ((type >> 2) & 1),
3287 contents + roff - 3);
3288 bfd_put_8 (output_bfd, 0xc7, contents + roff - 2);
3289 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7),
3290 contents + roff - 1);
3291 bfd_put_32 (output_bfd,
3292 elf64_x86_64_tpoff (info, relocation),
3293 contents + roff);
3294 continue;
3296 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3298 /* GDesc -> LE transition.
3299 It's originally:
3300 call *(%rax)
3301 Turn it into:
3302 xchg %ax,%ax. */
3303 bfd_put_8 (output_bfd, 0x66, contents + roff);
3304 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3305 continue;
3307 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTTPOFF)
3309 /* IE->LE transition:
3310 Originally it can be one of:
3311 movq foo@gottpoff(%rip), %reg
3312 addq foo@gottpoff(%rip), %reg
3313 We change it into:
3314 movq $foo, %reg
3315 leaq foo(%reg), %reg
3316 addq $foo, %reg. */
3318 unsigned int val, type, reg;
3320 val = bfd_get_8 (input_bfd, contents + roff - 3);
3321 type = bfd_get_8 (input_bfd, contents + roff - 2);
3322 reg = bfd_get_8 (input_bfd, contents + roff - 1);
3323 reg >>= 3;
3324 if (type == 0x8b)
3326 /* movq */
3327 if (val == 0x4c)
3328 bfd_put_8 (output_bfd, 0x49,
3329 contents + roff - 3);
3330 bfd_put_8 (output_bfd, 0xc7,
3331 contents + roff - 2);
3332 bfd_put_8 (output_bfd, 0xc0 | reg,
3333 contents + roff - 1);
3335 else if (reg == 4)
3337 /* addq -> addq - addressing with %rsp/%r12 is
3338 special */
3339 if (val == 0x4c)
3340 bfd_put_8 (output_bfd, 0x49,
3341 contents + roff - 3);
3342 bfd_put_8 (output_bfd, 0x81,
3343 contents + roff - 2);
3344 bfd_put_8 (output_bfd, 0xc0 | reg,
3345 contents + roff - 1);
3347 else
3349 /* addq -> leaq */
3350 if (val == 0x4c)
3351 bfd_put_8 (output_bfd, 0x4d,
3352 contents + roff - 3);
3353 bfd_put_8 (output_bfd, 0x8d,
3354 contents + roff - 2);
3355 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
3356 contents + roff - 1);
3358 bfd_put_32 (output_bfd,
3359 elf64_x86_64_tpoff (info, relocation),
3360 contents + roff);
3361 continue;
3363 else
3364 BFD_ASSERT (FALSE);
3367 if (htab->sgot == NULL)
3368 abort ();
3370 if (h != NULL)
3372 off = h->got.offset;
3373 offplt = elf64_x86_64_hash_entry (h)->tlsdesc_got;
3375 else
3377 if (local_got_offsets == NULL)
3378 abort ();
3380 off = local_got_offsets[r_symndx];
3381 offplt = local_tlsdesc_gotents[r_symndx];
3384 if ((off & 1) != 0)
3385 off &= ~1;
3386 else
3388 Elf_Internal_Rela outrel;
3389 bfd_byte *loc;
3390 int dr_type, indx;
3391 asection *sreloc;
3393 if (htab->srelgot == NULL)
3394 abort ();
3396 indx = h && h->dynindx != -1 ? h->dynindx : 0;
3398 if (GOT_TLS_GDESC_P (tls_type))
3400 outrel.r_info = ELF64_R_INFO (indx, R_X86_64_TLSDESC);
3401 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt
3402 + 2 * GOT_ENTRY_SIZE <= htab->sgotplt->size);
3403 outrel.r_offset = (htab->sgotplt->output_section->vma
3404 + htab->sgotplt->output_offset
3405 + offplt
3406 + htab->sgotplt_jump_table_size);
3407 sreloc = htab->srelplt;
3408 loc = sreloc->contents;
3409 loc += sreloc->reloc_count++
3410 * sizeof (Elf64_External_Rela);
3411 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3412 <= sreloc->contents + sreloc->size);
3413 if (indx == 0)
3414 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3415 else
3416 outrel.r_addend = 0;
3417 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3420 sreloc = htab->srelgot;
3422 outrel.r_offset = (htab->sgot->output_section->vma
3423 + htab->sgot->output_offset + off);
3425 if (GOT_TLS_GD_P (tls_type))
3426 dr_type = R_X86_64_DTPMOD64;
3427 else if (GOT_TLS_GDESC_P (tls_type))
3428 goto dr_done;
3429 else
3430 dr_type = R_X86_64_TPOFF64;
3432 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
3433 outrel.r_addend = 0;
3434 if ((dr_type == R_X86_64_TPOFF64
3435 || dr_type == R_X86_64_TLSDESC) && indx == 0)
3436 outrel.r_addend = relocation - elf64_x86_64_dtpoff_base (info);
3437 outrel.r_info = ELF64_R_INFO (indx, dr_type);
3439 loc = sreloc->contents;
3440 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
3441 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3442 <= sreloc->contents + sreloc->size);
3443 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3445 if (GOT_TLS_GD_P (tls_type))
3447 if (indx == 0)
3449 BFD_ASSERT (! unresolved_reloc);
3450 bfd_put_64 (output_bfd,
3451 relocation - elf64_x86_64_dtpoff_base (info),
3452 htab->sgot->contents + off + GOT_ENTRY_SIZE);
3454 else
3456 bfd_put_64 (output_bfd, 0,
3457 htab->sgot->contents + off + GOT_ENTRY_SIZE);
3458 outrel.r_info = ELF64_R_INFO (indx,
3459 R_X86_64_DTPOFF64);
3460 outrel.r_offset += GOT_ENTRY_SIZE;
3461 sreloc->reloc_count++;
3462 loc += sizeof (Elf64_External_Rela);
3463 BFD_ASSERT (loc + sizeof (Elf64_External_Rela)
3464 <= sreloc->contents + sreloc->size);
3465 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3469 dr_done:
3470 if (h != NULL)
3471 h->got.offset |= 1;
3472 else
3473 local_got_offsets[r_symndx] |= 1;
3476 if (off >= (bfd_vma) -2
3477 && ! GOT_TLS_GDESC_P (tls_type))
3478 abort ();
3479 if (r_type == ELF64_R_TYPE (rel->r_info))
3481 if (r_type == R_X86_64_GOTPC32_TLSDESC
3482 || r_type == R_X86_64_TLSDESC_CALL)
3483 relocation = htab->sgotplt->output_section->vma
3484 + htab->sgotplt->output_offset
3485 + offplt + htab->sgotplt_jump_table_size;
3486 else
3487 relocation = htab->sgot->output_section->vma
3488 + htab->sgot->output_offset + off;
3489 unresolved_reloc = FALSE;
3491 else
3493 bfd_vma roff = rel->r_offset;
3495 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
3497 /* GD->IE transition.
3498 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
3499 .word 0x6666; rex64; call __tls_get_addr@plt
3500 Change it into:
3501 movq %fs:0, %rax
3502 addq foo@gottpoff(%rip), %rax */
3503 memcpy (contents + roff - 4,
3504 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
3505 16);
3507 relocation = (htab->sgot->output_section->vma
3508 + htab->sgot->output_offset + off
3509 - roff
3510 - input_section->output_section->vma
3511 - input_section->output_offset
3512 - 12);
3513 bfd_put_32 (output_bfd, relocation,
3514 contents + roff + 8);
3515 /* Skip R_X86_64_PLT32. */
3516 rel++;
3517 continue;
3519 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_GOTPC32_TLSDESC)
3521 /* GDesc -> IE transition.
3522 It's originally something like:
3523 leaq x@tlsdesc(%rip), %rax
3525 Change it to:
3526 movq x@gottpoff(%rip), %rax # before xchg %ax,%ax
3529 unsigned int val, type, type2;
3531 type = bfd_get_8 (input_bfd, contents + roff - 3);
3532 type2 = bfd_get_8 (input_bfd, contents + roff - 2);
3533 val = bfd_get_8 (input_bfd, contents + roff - 1);
3535 /* Now modify the instruction as appropriate. To
3536 turn a leaq into a movq in the form we use it, it
3537 suffices to change the second byte from 0x8d to
3538 0x8b. */
3539 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2);
3541 bfd_put_32 (output_bfd,
3542 htab->sgot->output_section->vma
3543 + htab->sgot->output_offset + off
3544 - rel->r_offset
3545 - input_section->output_section->vma
3546 - input_section->output_offset
3547 - 4,
3548 contents + roff);
3549 continue;
3551 else if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSDESC_CALL)
3553 /* GDesc -> IE transition.
3554 It's originally:
3555 call *(%rax)
3557 Change it to:
3558 xchg %ax,%ax. */
3560 unsigned int val, type;
3562 type = bfd_get_8 (input_bfd, contents + roff);
3563 val = bfd_get_8 (input_bfd, contents + roff + 1);
3564 bfd_put_8 (output_bfd, 0x66, contents + roff);
3565 bfd_put_8 (output_bfd, 0x90, contents + roff + 1);
3566 continue;
3568 else
3569 BFD_ASSERT (FALSE);
3571 break;
3573 case R_X86_64_TLSLD:
3574 if (! elf64_x86_64_tls_transition (info, input_bfd,
3575 input_section, contents,
3576 symtab_hdr, sym_hashes,
3577 &r_type, GOT_UNKNOWN,
3578 rel, relend, h))
3579 return FALSE;
3581 if (r_type != R_X86_64_TLSLD)
3583 /* LD->LE transition:
3584 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr.
3585 We change it into:
3586 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
3588 BFD_ASSERT (r_type == R_X86_64_TPOFF32);
3589 memcpy (contents + rel->r_offset - 3,
3590 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
3591 /* Skip R_X86_64_PC32/R_X86_64_PLT32. */
3592 rel++;
3593 continue;
3596 if (htab->sgot == NULL)
3597 abort ();
3599 off = htab->tls_ld_got.offset;
3600 if (off & 1)
3601 off &= ~1;
3602 else
3604 Elf_Internal_Rela outrel;
3605 bfd_byte *loc;
3607 if (htab->srelgot == NULL)
3608 abort ();
3610 outrel.r_offset = (htab->sgot->output_section->vma
3611 + htab->sgot->output_offset + off);
3613 bfd_put_64 (output_bfd, 0,
3614 htab->sgot->contents + off);
3615 bfd_put_64 (output_bfd, 0,
3616 htab->sgot->contents + off + GOT_ENTRY_SIZE);
3617 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
3618 outrel.r_addend = 0;
3619 loc = htab->srelgot->contents;
3620 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3621 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
3622 htab->tls_ld_got.offset |= 1;
3624 relocation = htab->sgot->output_section->vma
3625 + htab->sgot->output_offset + off;
3626 unresolved_reloc = FALSE;
3627 break;
3629 case R_X86_64_DTPOFF32:
3630 if (info->shared || (input_section->flags & SEC_CODE) == 0)
3631 relocation -= elf64_x86_64_dtpoff_base (info);
3632 else
3633 relocation = elf64_x86_64_tpoff (info, relocation);
3634 break;
3636 case R_X86_64_TPOFF32:
3637 BFD_ASSERT (! info->shared);
3638 relocation = elf64_x86_64_tpoff (info, relocation);
3639 break;
3641 default:
3642 break;
3645 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
3646 because such sections are not SEC_ALLOC and thus ld.so will
3647 not process them. */
3648 if (unresolved_reloc
3649 && !((input_section->flags & SEC_DEBUGGING) != 0
3650 && h->def_dynamic))
3651 (*_bfd_error_handler)
3652 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
3653 input_bfd,
3654 input_section,
3655 (long) rel->r_offset,
3656 howto->name,
3657 h->root.root.string);
3659 do_relocation:
3660 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3661 contents, rel->r_offset,
3662 relocation, rel->r_addend);
3664 if (r != bfd_reloc_ok)
3666 const char *name;
3668 if (h != NULL)
3669 name = h->root.root.string;
3670 else
3672 name = bfd_elf_string_from_elf_section (input_bfd,
3673 symtab_hdr->sh_link,
3674 sym->st_name);
3675 if (name == NULL)
3676 return FALSE;
3677 if (*name == '\0')
3678 name = bfd_section_name (input_bfd, sec);
3681 if (r == bfd_reloc_overflow)
3683 if (! ((*info->callbacks->reloc_overflow)
3684 (info, (h ? &h->root : NULL), name, howto->name,
3685 (bfd_vma) 0, input_bfd, input_section,
3686 rel->r_offset)))
3687 return FALSE;
3689 else
3691 (*_bfd_error_handler)
3692 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
3693 input_bfd, input_section,
3694 (long) rel->r_offset, name, (int) r);
3695 return FALSE;
3700 return TRUE;
3703 /* Finish up dynamic symbol handling. We set the contents of various
3704 dynamic sections here. */
3706 static bfd_boolean
3707 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
3708 struct bfd_link_info *info,
3709 struct elf_link_hash_entry *h,
3710 Elf_Internal_Sym *sym)
3712 struct elf64_x86_64_link_hash_table *htab;
3714 htab = elf64_x86_64_hash_table (info);
3716 if (h->plt.offset != (bfd_vma) -1)
3718 bfd_vma plt_index;
3719 bfd_vma got_offset;
3720 Elf_Internal_Rela rela;
3721 bfd_byte *loc;
3722 asection *plt, *gotplt, *relplt;
3724 /* When building a static executable, use .iplt, .igot.plt and
3725 .rela.iplt sections for STT_GNU_IFUNC symbols. */
3726 if (htab->splt != NULL)
3728 plt = htab->splt;
3729 gotplt = htab->sgotplt;
3730 relplt = htab->srelplt;
3732 else
3734 plt = htab->iplt;
3735 gotplt = htab->igotplt;
3736 relplt = htab->irelplt;
3739 /* This symbol has an entry in the procedure linkage table. Set
3740 it up. */
3741 if ((h->dynindx == -1
3742 && !((h->forced_local || info->executable)
3743 && h->def_regular
3744 && h->type == STT_GNU_IFUNC))
3745 || plt == NULL
3746 || gotplt == NULL
3747 || relplt == NULL)
3748 abort ();
3750 /* Get the index in the procedure linkage table which
3751 corresponds to this symbol. This is the index of this symbol
3752 in all the symbols for which we are making plt entries. The
3753 first entry in the procedure linkage table is reserved.
3755 Get the offset into the .got table of the entry that
3756 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
3757 bytes. The first three are reserved for the dynamic linker.
3759 For static executables, we don't reserve anything. */
3761 if (plt == htab->splt)
3763 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
3764 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
3766 else
3768 plt_index = h->plt.offset / PLT_ENTRY_SIZE;
3769 got_offset = plt_index * GOT_ENTRY_SIZE;
3772 /* Fill in the entry in the procedure linkage table. */
3773 memcpy (plt->contents + h->plt.offset, elf64_x86_64_plt_entry,
3774 PLT_ENTRY_SIZE);
3776 /* Insert the relocation positions of the plt section. The magic
3777 numbers at the end of the statements are the positions of the
3778 relocations in the plt section. */
3779 /* Put offset for jmp *name@GOTPCREL(%rip), since the
3780 instruction uses 6 bytes, subtract this value. */
3781 bfd_put_32 (output_bfd,
3782 (gotplt->output_section->vma
3783 + gotplt->output_offset
3784 + got_offset
3785 - plt->output_section->vma
3786 - plt->output_offset
3787 - h->plt.offset
3788 - 6),
3789 plt->contents + h->plt.offset + 2);
3791 /* Don't fill PLT entry for static executables. */
3792 if (plt == htab->splt)
3794 /* Put relocation index. */
3795 bfd_put_32 (output_bfd, plt_index,
3796 plt->contents + h->plt.offset + 7);
3797 /* Put offset for jmp .PLT0. */
3798 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
3799 plt->contents + h->plt.offset + 12);
3802 /* Fill in the entry in the global offset table, initially this
3803 points to the pushq instruction in the PLT which is at offset 6. */
3804 bfd_put_64 (output_bfd, (plt->output_section->vma
3805 + plt->output_offset
3806 + h->plt.offset + 6),
3807 gotplt->contents + got_offset);
3809 /* Fill in the entry in the .rela.plt section. */
3810 rela.r_offset = (gotplt->output_section->vma
3811 + gotplt->output_offset
3812 + got_offset);
3813 if (h->dynindx == -1
3814 || ((info->executable
3815 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3816 && h->def_regular
3817 && h->type == STT_GNU_IFUNC))
3819 /* If an STT_GNU_IFUNC symbol is locally defined, generate
3820 R_X86_64_IRELATIVE instead of R_X86_64_JUMP_SLOT. */
3821 rela.r_info = ELF64_R_INFO (0, R_X86_64_IRELATIVE);
3822 rela.r_addend = (h->root.u.def.value
3823 + h->root.u.def.section->output_section->vma
3824 + h->root.u.def.section->output_offset);
3826 else
3828 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
3829 rela.r_addend = 0;
3831 loc = relplt->contents + plt_index * sizeof (Elf64_External_Rela);
3832 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3834 if (!h->def_regular)
3836 /* Mark the symbol as undefined, rather than as defined in
3837 the .plt section. Leave the value if there were any
3838 relocations where pointer equality matters (this is a clue
3839 for the dynamic linker, to make function pointer
3840 comparisons work between an application and shared
3841 library), otherwise set it to zero. If a function is only
3842 called from a binary, there is no need to slow down
3843 shared libraries because of that. */
3844 sym->st_shndx = SHN_UNDEF;
3845 if (!h->pointer_equality_needed)
3846 sym->st_value = 0;
3850 if (h->got.offset != (bfd_vma) -1
3851 && ! GOT_TLS_GD_ANY_P (elf64_x86_64_hash_entry (h)->tls_type)
3852 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
3854 Elf_Internal_Rela rela;
3855 bfd_byte *loc;
3857 /* This symbol has an entry in the global offset table. Set it
3858 up. */
3859 if (htab->sgot == NULL || htab->srelgot == NULL)
3860 abort ();
3862 rela.r_offset = (htab->sgot->output_section->vma
3863 + htab->sgot->output_offset
3864 + (h->got.offset &~ (bfd_vma) 1));
3866 /* If this is a static link, or it is a -Bsymbolic link and the
3867 symbol is defined locally or was forced to be local because
3868 of a version file, we just want to emit a RELATIVE reloc.
3869 The entry in the global offset table will already have been
3870 initialized in the relocate_section function. */
3871 if (h->def_regular
3872 && h->type == STT_GNU_IFUNC)
3874 if (info->shared)
3876 /* Generate R_X86_64_GLOB_DAT. */
3877 goto do_glob_dat;
3879 else
3881 if (!h->pointer_equality_needed)
3882 abort ();
3884 /* For non-shared object, we can't use .got.plt, which
3885 contains the real function addres if we need pointer
3886 equality. We load the GOT entry with the PLT entry. */
3887 asection *plt = htab->splt ? htab->splt : htab->iplt;
3888 bfd_put_64 (output_bfd, (plt->output_section->vma
3889 + plt->output_offset
3890 + h->plt.offset),
3891 htab->sgot->contents + h->got.offset);
3892 return TRUE;
3895 else if (info->shared
3896 && SYMBOL_REFERENCES_LOCAL (info, h))
3898 if (!h->def_regular)
3899 return FALSE;
3900 BFD_ASSERT((h->got.offset & 1) != 0);
3901 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
3902 rela.r_addend = (h->root.u.def.value
3903 + h->root.u.def.section->output_section->vma
3904 + h->root.u.def.section->output_offset);
3906 else
3908 BFD_ASSERT((h->got.offset & 1) == 0);
3909 do_glob_dat:
3910 bfd_put_64 (output_bfd, (bfd_vma) 0,
3911 htab->sgot->contents + h->got.offset);
3912 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
3913 rela.r_addend = 0;
3916 loc = htab->srelgot->contents;
3917 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
3918 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3921 if (h->needs_copy)
3923 Elf_Internal_Rela rela;
3924 bfd_byte *loc;
3926 /* This symbol needs a copy reloc. Set it up. */
3928 if (h->dynindx == -1
3929 || (h->root.type != bfd_link_hash_defined
3930 && h->root.type != bfd_link_hash_defweak)
3931 || htab->srelbss == NULL)
3932 abort ();
3934 rela.r_offset = (h->root.u.def.value
3935 + h->root.u.def.section->output_section->vma
3936 + h->root.u.def.section->output_offset);
3937 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
3938 rela.r_addend = 0;
3939 loc = htab->srelbss->contents;
3940 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
3941 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
3944 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
3945 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
3946 || h == htab->elf.hgot)
3947 sym->st_shndx = SHN_ABS;
3949 return TRUE;
3952 /* Used to decide how to sort relocs in an optimal manner for the
3953 dynamic linker, before writing them out. */
3955 static enum elf_reloc_type_class
3956 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
3958 switch ((int) ELF64_R_TYPE (rela->r_info))
3960 case R_X86_64_RELATIVE:
3961 return reloc_class_relative;
3962 case R_X86_64_JUMP_SLOT:
3963 return reloc_class_plt;
3964 case R_X86_64_COPY:
3965 return reloc_class_copy;
3966 default:
3967 return reloc_class_normal;
3971 /* Finish up the dynamic sections. */
3973 static bfd_boolean
3974 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
3976 struct elf64_x86_64_link_hash_table *htab;
3977 bfd *dynobj;
3978 asection *sdyn;
3980 htab = elf64_x86_64_hash_table (info);
3981 dynobj = htab->elf.dynobj;
3982 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
3984 if (htab->elf.dynamic_sections_created)
3986 Elf64_External_Dyn *dyncon, *dynconend;
3988 if (sdyn == NULL || htab->sgot == NULL)
3989 abort ();
3991 dyncon = (Elf64_External_Dyn *) sdyn->contents;
3992 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
3993 for (; dyncon < dynconend; dyncon++)
3995 Elf_Internal_Dyn dyn;
3996 asection *s;
3998 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
4000 switch (dyn.d_tag)
4002 default:
4003 continue;
4005 case DT_PLTGOT:
4006 s = htab->sgotplt;
4007 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4008 break;
4010 case DT_JMPREL:
4011 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
4012 break;
4014 case DT_PLTRELSZ:
4015 s = htab->srelplt->output_section;
4016 dyn.d_un.d_val = s->size;
4017 break;
4019 case DT_RELASZ:
4020 /* The procedure linkage table relocs (DT_JMPREL) should
4021 not be included in the overall relocs (DT_RELA).
4022 Therefore, we override the DT_RELASZ entry here to
4023 make it not include the JMPREL relocs. Since the
4024 linker script arranges for .rela.plt to follow all
4025 other relocation sections, we don't have to worry
4026 about changing the DT_RELA entry. */
4027 if (htab->srelplt != NULL)
4029 s = htab->srelplt->output_section;
4030 dyn.d_un.d_val -= s->size;
4032 break;
4034 case DT_TLSDESC_PLT:
4035 s = htab->splt;
4036 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4037 + htab->tlsdesc_plt;
4038 break;
4040 case DT_TLSDESC_GOT:
4041 s = htab->sgot;
4042 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset
4043 + htab->tlsdesc_got;
4044 break;
4047 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
4050 /* Fill in the special first entry in the procedure linkage table. */
4051 if (htab->splt && htab->splt->size > 0)
4053 /* Fill in the first entry in the procedure linkage table. */
4054 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
4055 PLT_ENTRY_SIZE);
4056 /* Add offset for pushq GOT+8(%rip), since the instruction
4057 uses 6 bytes subtract this value. */
4058 bfd_put_32 (output_bfd,
4059 (htab->sgotplt->output_section->vma
4060 + htab->sgotplt->output_offset
4062 - htab->splt->output_section->vma
4063 - htab->splt->output_offset
4064 - 6),
4065 htab->splt->contents + 2);
4066 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
4067 the end of the instruction. */
4068 bfd_put_32 (output_bfd,
4069 (htab->sgotplt->output_section->vma
4070 + htab->sgotplt->output_offset
4071 + 16
4072 - htab->splt->output_section->vma
4073 - htab->splt->output_offset
4074 - 12),
4075 htab->splt->contents + 8);
4077 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
4078 PLT_ENTRY_SIZE;
4080 if (htab->tlsdesc_plt)
4082 bfd_put_64 (output_bfd, (bfd_vma) 0,
4083 htab->sgot->contents + htab->tlsdesc_got);
4085 memcpy (htab->splt->contents + htab->tlsdesc_plt,
4086 elf64_x86_64_plt0_entry,
4087 PLT_ENTRY_SIZE);
4089 /* Add offset for pushq GOT+8(%rip), since the
4090 instruction uses 6 bytes subtract this value. */
4091 bfd_put_32 (output_bfd,
4092 (htab->sgotplt->output_section->vma
4093 + htab->sgotplt->output_offset
4095 - htab->splt->output_section->vma
4096 - htab->splt->output_offset
4097 - htab->tlsdesc_plt
4098 - 6),
4099 htab->splt->contents + htab->tlsdesc_plt + 2);
4100 /* Add offset for jmp *GOT+TDG(%rip), where TGD stands for
4101 htab->tlsdesc_got. The 12 is the offset to the end of
4102 the instruction. */
4103 bfd_put_32 (output_bfd,
4104 (htab->sgot->output_section->vma
4105 + htab->sgot->output_offset
4106 + htab->tlsdesc_got
4107 - htab->splt->output_section->vma
4108 - htab->splt->output_offset
4109 - htab->tlsdesc_plt
4110 - 12),
4111 htab->splt->contents + htab->tlsdesc_plt + 8);
4116 if (htab->sgotplt)
4118 /* Fill in the first three entries in the global offset table. */
4119 if (htab->sgotplt->size > 0)
4121 /* Set the first entry in the global offset table to the address of
4122 the dynamic section. */
4123 if (sdyn == NULL)
4124 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
4125 else
4126 bfd_put_64 (output_bfd,
4127 sdyn->output_section->vma + sdyn->output_offset,
4128 htab->sgotplt->contents);
4129 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
4130 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
4131 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
4134 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
4135 GOT_ENTRY_SIZE;
4138 if (htab->sgot && htab->sgot->size > 0)
4139 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
4140 = GOT_ENTRY_SIZE;
4142 return TRUE;
4145 /* Return address for Ith PLT stub in section PLT, for relocation REL
4146 or (bfd_vma) -1 if it should not be included. */
4148 static bfd_vma
4149 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
4150 const arelent *rel ATTRIBUTE_UNUSED)
4152 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
4155 /* Handle an x86-64 specific section when reading an object file. This
4156 is called when elfcode.h finds a section with an unknown type. */
4158 static bfd_boolean
4159 elf64_x86_64_section_from_shdr (bfd *abfd,
4160 Elf_Internal_Shdr *hdr,
4161 const char *name,
4162 int shindex)
4164 if (hdr->sh_type != SHT_X86_64_UNWIND)
4165 return FALSE;
4167 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
4168 return FALSE;
4170 return TRUE;
4173 /* Hook called by the linker routine which adds symbols from an object
4174 file. We use it to put SHN_X86_64_LCOMMON items in .lbss, instead
4175 of .bss. */
4177 static bfd_boolean
4178 elf64_x86_64_add_symbol_hook (bfd *abfd,
4179 struct bfd_link_info *info,
4180 Elf_Internal_Sym *sym,
4181 const char **namep ATTRIBUTE_UNUSED,
4182 flagword *flagsp ATTRIBUTE_UNUSED,
4183 asection **secp,
4184 bfd_vma *valp)
4186 asection *lcomm;
4188 switch (sym->st_shndx)
4190 case SHN_X86_64_LCOMMON:
4191 lcomm = bfd_get_section_by_name (abfd, "LARGE_COMMON");
4192 if (lcomm == NULL)
4194 lcomm = bfd_make_section_with_flags (abfd,
4195 "LARGE_COMMON",
4196 (SEC_ALLOC
4197 | SEC_IS_COMMON
4198 | SEC_LINKER_CREATED));
4199 if (lcomm == NULL)
4200 return FALSE;
4201 elf_section_flags (lcomm) |= SHF_X86_64_LARGE;
4203 *secp = lcomm;
4204 *valp = sym->st_size;
4205 break;
4208 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
4209 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4211 return TRUE;
4215 /* Given a BFD section, try to locate the corresponding ELF section
4216 index. */
4218 static bfd_boolean
4219 elf64_x86_64_elf_section_from_bfd_section (bfd *abfd ATTRIBUTE_UNUSED,
4220 asection *sec, int *index)
4222 if (sec == &_bfd_elf_large_com_section)
4224 *index = SHN_X86_64_LCOMMON;
4225 return TRUE;
4227 return FALSE;
4230 /* Process a symbol. */
4232 static void
4233 elf64_x86_64_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED,
4234 asymbol *asym)
4236 elf_symbol_type *elfsym = (elf_symbol_type *) asym;
4238 switch (elfsym->internal_elf_sym.st_shndx)
4240 case SHN_X86_64_LCOMMON:
4241 asym->section = &_bfd_elf_large_com_section;
4242 asym->value = elfsym->internal_elf_sym.st_size;
4243 /* Common symbol doesn't set BSF_GLOBAL. */
4244 asym->flags &= ~BSF_GLOBAL;
4245 break;
4249 static bfd_boolean
4250 elf64_x86_64_common_definition (Elf_Internal_Sym *sym)
4252 return (sym->st_shndx == SHN_COMMON
4253 || sym->st_shndx == SHN_X86_64_LCOMMON);
4256 static unsigned int
4257 elf64_x86_64_common_section_index (asection *sec)
4259 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4260 return SHN_COMMON;
4261 else
4262 return SHN_X86_64_LCOMMON;
4265 static asection *
4266 elf64_x86_64_common_section (asection *sec)
4268 if ((elf_section_flags (sec) & SHF_X86_64_LARGE) == 0)
4269 return bfd_com_section_ptr;
4270 else
4271 return &_bfd_elf_large_com_section;
4274 static bfd_boolean
4275 elf64_x86_64_merge_symbol (struct bfd_link_info *info ATTRIBUTE_UNUSED,
4276 struct elf_link_hash_entry **sym_hash ATTRIBUTE_UNUSED,
4277 struct elf_link_hash_entry *h,
4278 Elf_Internal_Sym *sym,
4279 asection **psec,
4280 bfd_vma *pvalue ATTRIBUTE_UNUSED,
4281 unsigned int *pold_alignment ATTRIBUTE_UNUSED,
4282 bfd_boolean *skip ATTRIBUTE_UNUSED,
4283 bfd_boolean *override ATTRIBUTE_UNUSED,
4284 bfd_boolean *type_change_ok ATTRIBUTE_UNUSED,
4285 bfd_boolean *size_change_ok ATTRIBUTE_UNUSED,
4286 bfd_boolean *newdef ATTRIBUTE_UNUSED,
4287 bfd_boolean *newdyn,
4288 bfd_boolean *newdyncommon ATTRIBUTE_UNUSED,
4289 bfd_boolean *newweak ATTRIBUTE_UNUSED,
4290 bfd *abfd ATTRIBUTE_UNUSED,
4291 asection **sec,
4292 bfd_boolean *olddef ATTRIBUTE_UNUSED,
4293 bfd_boolean *olddyn,
4294 bfd_boolean *olddyncommon ATTRIBUTE_UNUSED,
4295 bfd_boolean *oldweak ATTRIBUTE_UNUSED,
4296 bfd *oldbfd,
4297 asection **oldsec)
4299 /* A normal common symbol and a large common symbol result in a
4300 normal common symbol. We turn the large common symbol into a
4301 normal one. */
4302 if (!*olddyn
4303 && h->root.type == bfd_link_hash_common
4304 && !*newdyn
4305 && bfd_is_com_section (*sec)
4306 && *oldsec != *sec)
4308 if (sym->st_shndx == SHN_COMMON
4309 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) != 0)
4311 h->root.u.c.p->section
4312 = bfd_make_section_old_way (oldbfd, "COMMON");
4313 h->root.u.c.p->section->flags = SEC_ALLOC;
4315 else if (sym->st_shndx == SHN_X86_64_LCOMMON
4316 && (elf_section_flags (*oldsec) & SHF_X86_64_LARGE) == 0)
4317 *psec = *sec = bfd_com_section_ptr;
4320 return TRUE;
4323 static int
4324 elf64_x86_64_additional_program_headers (bfd *abfd,
4325 struct bfd_link_info *info ATTRIBUTE_UNUSED)
4327 asection *s;
4328 int count = 0;
4330 /* Check to see if we need a large readonly segment. */
4331 s = bfd_get_section_by_name (abfd, ".lrodata");
4332 if (s && (s->flags & SEC_LOAD))
4333 count++;
4335 /* Check to see if we need a large data segment. Since .lbss sections
4336 is placed right after the .bss section, there should be no need for
4337 a large data segment just because of .lbss. */
4338 s = bfd_get_section_by_name (abfd, ".ldata");
4339 if (s && (s->flags & SEC_LOAD))
4340 count++;
4342 return count;
4345 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
4347 static bfd_boolean
4348 elf64_x86_64_hash_symbol (struct elf_link_hash_entry *h)
4350 if (h->plt.offset != (bfd_vma) -1
4351 && !h->def_regular
4352 && !h->pointer_equality_needed)
4353 return FALSE;
4355 return _bfd_elf_hash_symbol (h);
4358 static const struct bfd_elf_special_section
4359 elf64_x86_64_special_sections[]=
4361 { STRING_COMMA_LEN (".gnu.linkonce.lb"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4362 { STRING_COMMA_LEN (".gnu.linkonce.lr"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4363 { STRING_COMMA_LEN (".gnu.linkonce.lt"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR + SHF_X86_64_LARGE},
4364 { STRING_COMMA_LEN (".lbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4365 { STRING_COMMA_LEN (".ldata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_X86_64_LARGE},
4366 { STRING_COMMA_LEN (".lrodata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_X86_64_LARGE},
4367 { NULL, 0, 0, 0, 0 }
4370 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
4371 #define TARGET_LITTLE_NAME "elf64-x86-64"
4372 #define ELF_ARCH bfd_arch_i386
4373 #define ELF_MACHINE_CODE EM_X86_64
4374 #define ELF_MAXPAGESIZE 0x200000
4375 #define ELF_MINPAGESIZE 0x1000
4376 #define ELF_COMMONPAGESIZE 0x1000
4378 #define elf_backend_can_gc_sections 1
4379 #define elf_backend_can_refcount 1
4380 #define elf_backend_want_got_plt 1
4381 #define elf_backend_plt_readonly 1
4382 #define elf_backend_want_plt_sym 0
4383 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
4384 #define elf_backend_rela_normal 1
4386 #define elf_info_to_howto elf64_x86_64_info_to_howto
4388 #define bfd_elf64_bfd_link_hash_table_create \
4389 elf64_x86_64_link_hash_table_create
4390 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
4391 #define bfd_elf64_bfd_reloc_name_lookup \
4392 elf64_x86_64_reloc_name_lookup
4394 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
4395 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
4396 #define elf_backend_check_relocs elf64_x86_64_check_relocs
4397 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
4398 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
4399 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
4400 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
4401 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
4402 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
4403 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
4404 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
4405 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
4406 #define elf_backend_relocate_section elf64_x86_64_relocate_section
4407 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
4408 #define elf_backend_always_size_sections elf64_x86_64_always_size_sections
4409 #define elf_backend_init_index_section _bfd_elf_init_1_index_section
4410 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
4411 #define elf_backend_object_p elf64_x86_64_elf_object_p
4412 #define bfd_elf64_mkobject elf64_x86_64_mkobject
4414 #define elf_backend_section_from_shdr \
4415 elf64_x86_64_section_from_shdr
4417 #define elf_backend_section_from_bfd_section \
4418 elf64_x86_64_elf_section_from_bfd_section
4419 #define elf_backend_add_symbol_hook \
4420 elf64_x86_64_add_symbol_hook
4421 #define elf_backend_symbol_processing \
4422 elf64_x86_64_symbol_processing
4423 #define elf_backend_common_section_index \
4424 elf64_x86_64_common_section_index
4425 #define elf_backend_common_section \
4426 elf64_x86_64_common_section
4427 #define elf_backend_common_definition \
4428 elf64_x86_64_common_definition
4429 #define elf_backend_merge_symbol \
4430 elf64_x86_64_merge_symbol
4431 #define elf_backend_special_sections \
4432 elf64_x86_64_special_sections
4433 #define elf_backend_additional_program_headers \
4434 elf64_x86_64_additional_program_headers
4435 #define elf_backend_hash_symbol \
4436 elf64_x86_64_hash_symbol
4438 #undef elf_backend_post_process_headers
4439 #define elf_backend_post_process_headers _bfd_elf_set_osabi
4441 #include "elf64-target.h"
4443 /* FreeBSD support. */
4445 #undef TARGET_LITTLE_SYM
4446 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_freebsd_vec
4447 #undef TARGET_LITTLE_NAME
4448 #define TARGET_LITTLE_NAME "elf64-x86-64-freebsd"
4450 #undef ELF_OSABI
4451 #define ELF_OSABI ELFOSABI_FREEBSD
4453 #undef elf64_bed
4454 #define elf64_bed elf64_x86_64_fbsd_bed
4456 #include "elf64-target.h"