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