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