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