2005-05-20 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / bfd / elf64-x86-64.c
blob179530b381c553880eadb491126fdb9ad71a5741
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>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
28 #include "elf/x86-64.h"
30 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
31 #define MINUS_ONE (~ (bfd_vma) 0)
33 /* The relocation "howto" table. Order of fields:
34 type, 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[] =
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),
107 /* GNU extension to record C++ vtable hierarchy. */
108 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
109 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
111 /* GNU extension to record C++ vtable member usage. */
112 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
113 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
114 FALSE)
117 /* Map BFD relocs to the x86_64 elf relocs. */
118 struct elf_reloc_map
120 bfd_reloc_code_real_type bfd_reloc_val;
121 unsigned char elf_reloc_val;
124 static const struct elf_reloc_map x86_64_reloc_map[] =
126 { BFD_RELOC_NONE, R_X86_64_NONE, },
127 { BFD_RELOC_64, R_X86_64_64, },
128 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
129 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
130 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
131 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
132 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
133 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
134 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
135 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
136 { BFD_RELOC_32, R_X86_64_32, },
137 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
138 { BFD_RELOC_16, R_X86_64_16, },
139 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
140 { BFD_RELOC_8, R_X86_64_8, },
141 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
142 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
143 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
144 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
145 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
146 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
147 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
148 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
149 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
150 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
151 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
155 /* Given a BFD reloc type, return a HOWTO structure. */
156 static reloc_howto_type *
157 elf64_x86_64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
158 bfd_reloc_code_real_type code)
160 unsigned int i;
162 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
163 i++)
165 if (x86_64_reloc_map[i].bfd_reloc_val == code)
166 return &x86_64_elf_howto_table[i];
168 return 0;
171 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
173 static void
174 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
175 Elf_Internal_Rela *dst)
177 unsigned r_type, i;
179 r_type = ELF64_R_TYPE (dst->r_info);
180 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
182 BFD_ASSERT (r_type <= (unsigned int) R_X86_64_TPOFF32);
183 i = r_type;
185 else
187 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
188 i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_TPOFF32 - 1);
190 cache_ptr->howto = &x86_64_elf_howto_table[i];
191 BFD_ASSERT (r_type == cache_ptr->howto->type);
194 /* Support for core dump NOTE sections. */
195 static bfd_boolean
196 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
198 int offset;
199 size_t size;
201 switch (note->descsz)
203 default:
204 return FALSE;
206 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
207 /* pr_cursig */
208 elf_tdata (abfd)->core_signal
209 = bfd_get_16 (abfd, note->descdata + 12);
211 /* pr_pid */
212 elf_tdata (abfd)->core_pid
213 = bfd_get_32 (abfd, note->descdata + 32);
215 /* pr_reg */
216 offset = 112;
217 size = 216;
219 break;
222 /* Make a ".reg/999" section. */
223 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
224 size, note->descpos + offset);
227 static bfd_boolean
228 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
230 switch (note->descsz)
232 default:
233 return FALSE;
235 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
236 elf_tdata (abfd)->core_program
237 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
238 elf_tdata (abfd)->core_command
239 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
242 /* Note that for some reason, a spurious space is tacked
243 onto the end of the args in some (at least one anyway)
244 implementations, so strip it off if it exists. */
247 char *command = elf_tdata (abfd)->core_command;
248 int n = strlen (command);
250 if (0 < n && command[n - 1] == ' ')
251 command[n - 1] = '\0';
254 return TRUE;
257 /* Functions for the x86-64 ELF linker. */
259 /* The name of the dynamic interpreter. This is put in the .interp
260 section. */
262 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
264 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
265 copying dynamic variables from a shared lib into an app's dynbss
266 section, and instead use a dynamic relocation to point into the
267 shared lib. */
268 #define ELIMINATE_COPY_RELOCS 1
270 /* The size in bytes of an entry in the global offset table. */
272 #define GOT_ENTRY_SIZE 8
274 /* The size in bytes of an entry in the procedure linkage table. */
276 #define PLT_ENTRY_SIZE 16
278 /* The first entry in a procedure linkage table looks like this. See the
279 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
281 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
283 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
284 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
285 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
288 /* Subsequent entries in a procedure linkage table look like this. */
290 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
292 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
293 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
294 0x68, /* pushq immediate */
295 0, 0, 0, 0, /* replaced with index into relocation table. */
296 0xe9, /* jmp relative */
297 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
300 /* The x86-64 linker needs to keep track of the number of relocs that
301 it decides to copy as dynamic relocs in check_relocs for each symbol.
302 This is so that it can later discard them if they are found to be
303 unnecessary. We store the information in a field extending the
304 regular ELF linker hash table. */
306 struct elf64_x86_64_dyn_relocs
308 /* Next section. */
309 struct elf64_x86_64_dyn_relocs *next;
311 /* The input section of the reloc. */
312 asection *sec;
314 /* Total number of relocs copied for the input section. */
315 bfd_size_type count;
317 /* Number of pc-relative relocs copied for the input section. */
318 bfd_size_type pc_count;
321 /* x86-64 ELF linker hash entry. */
323 struct elf64_x86_64_link_hash_entry
325 struct elf_link_hash_entry elf;
327 /* Track dynamic relocs copied for this symbol. */
328 struct elf64_x86_64_dyn_relocs *dyn_relocs;
330 #define GOT_UNKNOWN 0
331 #define GOT_NORMAL 1
332 #define GOT_TLS_GD 2
333 #define GOT_TLS_IE 3
334 unsigned char tls_type;
337 #define elf64_x86_64_hash_entry(ent) \
338 ((struct elf64_x86_64_link_hash_entry *)(ent))
340 struct elf64_x86_64_obj_tdata
342 struct elf_obj_tdata root;
344 /* tls_type for each local got entry. */
345 char *local_got_tls_type;
348 #define elf64_x86_64_tdata(abfd) \
349 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
351 #define elf64_x86_64_local_got_tls_type(abfd) \
352 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
355 /* x86-64 ELF linker hash table. */
357 struct elf64_x86_64_link_hash_table
359 struct elf_link_hash_table elf;
361 /* Short-cuts to get to dynamic linker sections. */
362 asection *sgot;
363 asection *sgotplt;
364 asection *srelgot;
365 asection *splt;
366 asection *srelplt;
367 asection *sdynbss;
368 asection *srelbss;
370 union {
371 bfd_signed_vma refcount;
372 bfd_vma offset;
373 } tls_ld_got;
375 /* Small local sym to section mapping cache. */
376 struct sym_sec_cache sym_sec;
379 /* Get the x86-64 ELF linker hash table from a link_info structure. */
381 #define elf64_x86_64_hash_table(p) \
382 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
384 /* Create an entry in an x86-64 ELF linker hash table. */
386 static struct bfd_hash_entry *
387 link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
388 const char *string)
390 /* Allocate the structure if it has not already been allocated by a
391 subclass. */
392 if (entry == NULL)
394 entry = bfd_hash_allocate (table,
395 sizeof (struct elf64_x86_64_link_hash_entry));
396 if (entry == NULL)
397 return entry;
400 /* Call the allocation method of the superclass. */
401 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
402 if (entry != NULL)
404 struct elf64_x86_64_link_hash_entry *eh;
406 eh = (struct elf64_x86_64_link_hash_entry *) entry;
407 eh->dyn_relocs = NULL;
408 eh->tls_type = GOT_UNKNOWN;
411 return entry;
414 /* Create an X86-64 ELF linker hash table. */
416 static struct bfd_link_hash_table *
417 elf64_x86_64_link_hash_table_create (bfd *abfd)
419 struct elf64_x86_64_link_hash_table *ret;
420 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
422 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
423 if (ret == NULL)
424 return NULL;
426 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
428 free (ret);
429 return NULL;
432 ret->sgot = NULL;
433 ret->sgotplt = NULL;
434 ret->srelgot = NULL;
435 ret->splt = NULL;
436 ret->srelplt = NULL;
437 ret->sdynbss = NULL;
438 ret->srelbss = NULL;
439 ret->sym_sec.abfd = NULL;
440 ret->tls_ld_got.refcount = 0;
442 return &ret->elf.root;
445 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
446 shortcuts to them in our hash table. */
448 static bfd_boolean
449 create_got_section (bfd *dynobj, struct bfd_link_info *info)
451 struct elf64_x86_64_link_hash_table *htab;
453 if (! _bfd_elf_create_got_section (dynobj, info))
454 return FALSE;
456 htab = elf64_x86_64_hash_table (info);
457 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
458 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
459 if (!htab->sgot || !htab->sgotplt)
460 abort ();
462 htab->srelgot = bfd_make_section_with_flags (dynobj, ".rela.got",
463 (SEC_ALLOC | SEC_LOAD
464 | SEC_HAS_CONTENTS
465 | SEC_IN_MEMORY
466 | SEC_LINKER_CREATED
467 | SEC_READONLY));
468 if (htab->srelgot == NULL
469 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
470 return FALSE;
471 return TRUE;
474 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
475 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
476 hash table. */
478 static bfd_boolean
479 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
481 struct elf64_x86_64_link_hash_table *htab;
483 htab = elf64_x86_64_hash_table (info);
484 if (!htab->sgot && !create_got_section (dynobj, info))
485 return FALSE;
487 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
488 return FALSE;
490 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
491 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
492 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
493 if (!info->shared)
494 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
496 if (!htab->splt || !htab->srelplt || !htab->sdynbss
497 || (!info->shared && !htab->srelbss))
498 abort ();
500 return TRUE;
503 /* Copy the extra info we tack onto an elf_link_hash_entry. */
505 static void
506 elf64_x86_64_copy_indirect_symbol (const struct elf_backend_data *bed,
507 struct elf_link_hash_entry *dir,
508 struct elf_link_hash_entry *ind)
510 struct elf64_x86_64_link_hash_entry *edir, *eind;
512 edir = (struct elf64_x86_64_link_hash_entry *) dir;
513 eind = (struct elf64_x86_64_link_hash_entry *) ind;
515 if (eind->dyn_relocs != NULL)
517 if (edir->dyn_relocs != NULL)
519 struct elf64_x86_64_dyn_relocs **pp;
520 struct elf64_x86_64_dyn_relocs *p;
522 if (ind->root.type == bfd_link_hash_indirect)
523 abort ();
525 /* Add reloc counts against the weak sym to the strong sym
526 list. Merge any entries against the same section. */
527 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
529 struct elf64_x86_64_dyn_relocs *q;
531 for (q = edir->dyn_relocs; q != NULL; q = q->next)
532 if (q->sec == p->sec)
534 q->pc_count += p->pc_count;
535 q->count += p->count;
536 *pp = p->next;
537 break;
539 if (q == NULL)
540 pp = &p->next;
542 *pp = edir->dyn_relocs;
545 edir->dyn_relocs = eind->dyn_relocs;
546 eind->dyn_relocs = NULL;
549 if (ind->root.type == bfd_link_hash_indirect
550 && dir->got.refcount <= 0)
552 edir->tls_type = eind->tls_type;
553 eind->tls_type = GOT_UNKNOWN;
556 if (ELIMINATE_COPY_RELOCS
557 && ind->root.type != bfd_link_hash_indirect
558 && dir->dynamic_adjusted)
560 /* If called to transfer flags for a weakdef during processing
561 of elf_adjust_dynamic_symbol, don't copy non_got_ref.
562 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
563 dir->ref_dynamic |= ind->ref_dynamic;
564 dir->ref_regular |= ind->ref_regular;
565 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
566 dir->needs_plt |= ind->needs_plt;
567 dir->pointer_equality_needed |= ind->pointer_equality_needed;
569 else
570 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
573 static bfd_boolean
574 elf64_x86_64_mkobject (bfd *abfd)
576 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
577 abfd->tdata.any = bfd_zalloc (abfd, amt);
578 if (abfd->tdata.any == NULL)
579 return FALSE;
580 return TRUE;
583 static bfd_boolean
584 elf64_x86_64_elf_object_p (bfd *abfd)
586 /* Set the right machine number for an x86-64 elf64 file. */
587 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
588 return TRUE;
591 static int
592 elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
594 if (info->shared)
595 return r_type;
597 switch (r_type)
599 case R_X86_64_TLSGD:
600 case R_X86_64_GOTTPOFF:
601 if (is_local)
602 return R_X86_64_TPOFF32;
603 return R_X86_64_GOTTPOFF;
604 case R_X86_64_TLSLD:
605 return R_X86_64_TPOFF32;
608 return r_type;
611 /* Look through the relocs for a section during the first phase, and
612 calculate needed space in the global offset table, procedure
613 linkage table, and dynamic reloc sections. */
615 static bfd_boolean
616 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
617 const Elf_Internal_Rela *relocs)
619 struct elf64_x86_64_link_hash_table *htab;
620 Elf_Internal_Shdr *symtab_hdr;
621 struct elf_link_hash_entry **sym_hashes;
622 const Elf_Internal_Rela *rel;
623 const Elf_Internal_Rela *rel_end;
624 asection *sreloc;
626 if (info->relocatable)
627 return TRUE;
629 htab = elf64_x86_64_hash_table (info);
630 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
631 sym_hashes = elf_sym_hashes (abfd);
633 sreloc = NULL;
635 rel_end = relocs + sec->reloc_count;
636 for (rel = relocs; rel < rel_end; rel++)
638 unsigned int r_type;
639 unsigned long r_symndx;
640 struct elf_link_hash_entry *h;
642 r_symndx = ELF64_R_SYM (rel->r_info);
643 r_type = ELF64_R_TYPE (rel->r_info);
645 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
647 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
648 abfd, r_symndx);
649 return FALSE;
652 if (r_symndx < symtab_hdr->sh_info)
653 h = NULL;
654 else
655 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
657 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
658 switch (r_type)
660 case R_X86_64_TLSLD:
661 htab->tls_ld_got.refcount += 1;
662 goto create_got;
664 case R_X86_64_TPOFF32:
665 if (info->shared)
667 (*_bfd_error_handler)
668 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
669 abfd,
670 x86_64_elf_howto_table[r_type].name,
671 (h) ? h->root.root.string : "a local symbol");
672 bfd_set_error (bfd_error_bad_value);
673 return FALSE;
675 break;
677 case R_X86_64_GOTTPOFF:
678 if (info->shared)
679 info->flags |= DF_STATIC_TLS;
680 /* Fall through */
682 case R_X86_64_GOT32:
683 case R_X86_64_GOTPCREL:
684 case R_X86_64_TLSGD:
685 /* This symbol requires a global offset table entry. */
687 int tls_type, old_tls_type;
689 switch (r_type)
691 default: tls_type = GOT_NORMAL; break;
692 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
693 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
696 if (h != NULL)
698 h->got.refcount += 1;
699 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
701 else
703 bfd_signed_vma *local_got_refcounts;
705 /* This is a global offset table entry for a local symbol. */
706 local_got_refcounts = elf_local_got_refcounts (abfd);
707 if (local_got_refcounts == NULL)
709 bfd_size_type size;
711 size = symtab_hdr->sh_info;
712 size *= sizeof (bfd_signed_vma) + sizeof (char);
713 local_got_refcounts = ((bfd_signed_vma *)
714 bfd_zalloc (abfd, size));
715 if (local_got_refcounts == NULL)
716 return FALSE;
717 elf_local_got_refcounts (abfd) = local_got_refcounts;
718 elf64_x86_64_local_got_tls_type (abfd)
719 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
721 local_got_refcounts[r_symndx] += 1;
722 old_tls_type
723 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
726 /* If a TLS symbol is accessed using IE at least once,
727 there is no point to use dynamic model for it. */
728 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
729 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
731 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
732 tls_type = old_tls_type;
733 else
735 (*_bfd_error_handler)
736 (_("%B: %s' accessed both as normal and thread local symbol"),
737 abfd, h ? h->root.root.string : "<local>");
738 return FALSE;
742 if (old_tls_type != tls_type)
744 if (h != NULL)
745 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
746 else
747 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
750 /* Fall through */
752 //case R_X86_64_GOTPCREL:
753 create_got:
754 if (htab->sgot == NULL)
756 if (htab->elf.dynobj == NULL)
757 htab->elf.dynobj = abfd;
758 if (!create_got_section (htab->elf.dynobj, info))
759 return FALSE;
761 break;
763 case R_X86_64_PLT32:
764 /* This symbol requires a procedure linkage table entry. We
765 actually build the entry in adjust_dynamic_symbol,
766 because this might be a case of linking PIC code which is
767 never referenced by a dynamic object, in which case we
768 don't need to generate a procedure linkage table entry
769 after all. */
771 /* If this is a local symbol, we resolve it directly without
772 creating a procedure linkage table entry. */
773 if (h == NULL)
774 continue;
776 h->needs_plt = 1;
777 h->plt.refcount += 1;
778 break;
780 case R_X86_64_8:
781 case R_X86_64_16:
782 case R_X86_64_32:
783 case R_X86_64_32S:
784 /* Let's help debug shared library creation. These relocs
785 cannot be used in shared libs. Don't error out for
786 sections we don't care about, such as debug sections or
787 non-constant sections. */
788 if (info->shared
789 && (sec->flags & SEC_ALLOC) != 0
790 && (sec->flags & SEC_READONLY) != 0)
792 (*_bfd_error_handler)
793 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
794 abfd,
795 x86_64_elf_howto_table[r_type].name,
796 (h) ? h->root.root.string : "a local symbol");
797 bfd_set_error (bfd_error_bad_value);
798 return FALSE;
800 /* Fall through. */
802 case R_X86_64_PC8:
803 case R_X86_64_PC16:
804 case R_X86_64_PC32:
805 case R_X86_64_64:
806 if (h != NULL && !info->shared)
808 /* If this reloc is in a read-only section, we might
809 need a copy reloc. We can't check reliably at this
810 stage whether the section is read-only, as input
811 sections have not yet been mapped to output sections.
812 Tentatively set the flag for now, and correct in
813 adjust_dynamic_symbol. */
814 h->non_got_ref = 1;
816 /* We may need a .plt entry if the function this reloc
817 refers to is in a shared lib. */
818 h->plt.refcount += 1;
819 if (r_type != R_X86_64_PC32)
820 h->pointer_equality_needed = 1;
823 /* If we are creating a shared library, and this is a reloc
824 against a global symbol, or a non PC relative reloc
825 against a local symbol, then we need to copy the reloc
826 into the shared library. However, if we are linking with
827 -Bsymbolic, we do not need to copy a reloc against a
828 global symbol which is defined in an object we are
829 including in the link (i.e., DEF_REGULAR is set). At
830 this point we have not seen all the input files, so it is
831 possible that DEF_REGULAR is not set now but will be set
832 later (it is never cleared). In case of a weak definition,
833 DEF_REGULAR may be cleared later by a strong definition in
834 a shared library. We account for that possibility below by
835 storing information in the relocs_copied field of the hash
836 table entry. A similar situation occurs when creating
837 shared libraries and symbol visibility changes render the
838 symbol local.
840 If on the other hand, we are creating an executable, we
841 may need to keep relocations for symbols satisfied by a
842 dynamic library if we manage to avoid copy relocs for the
843 symbol. */
844 if ((info->shared
845 && (sec->flags & SEC_ALLOC) != 0
846 && (((r_type != R_X86_64_PC8)
847 && (r_type != R_X86_64_PC16)
848 && (r_type != R_X86_64_PC32))
849 || (h != NULL
850 && (! info->symbolic
851 || h->root.type == bfd_link_hash_defweak
852 || !h->def_regular))))
853 || (ELIMINATE_COPY_RELOCS
854 && !info->shared
855 && (sec->flags & SEC_ALLOC) != 0
856 && h != NULL
857 && (h->root.type == bfd_link_hash_defweak
858 || !h->def_regular)))
860 struct elf64_x86_64_dyn_relocs *p;
861 struct elf64_x86_64_dyn_relocs **head;
863 /* We must copy these reloc types into the output file.
864 Create a reloc section in dynobj and make room for
865 this reloc. */
866 if (sreloc == NULL)
868 const char *name;
869 bfd *dynobj;
871 name = (bfd_elf_string_from_elf_section
872 (abfd,
873 elf_elfheader (abfd)->e_shstrndx,
874 elf_section_data (sec)->rel_hdr.sh_name));
875 if (name == NULL)
876 return FALSE;
878 if (strncmp (name, ".rela", 5) != 0
879 || strcmp (bfd_get_section_name (abfd, sec),
880 name + 5) != 0)
882 (*_bfd_error_handler)
883 (_("%B: bad relocation section name `%s\'"),
884 abfd, name);
887 if (htab->elf.dynobj == NULL)
888 htab->elf.dynobj = abfd;
890 dynobj = htab->elf.dynobj;
892 sreloc = bfd_get_section_by_name (dynobj, name);
893 if (sreloc == NULL)
895 flagword flags;
897 flags = (SEC_HAS_CONTENTS | SEC_READONLY
898 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
899 if ((sec->flags & SEC_ALLOC) != 0)
900 flags |= SEC_ALLOC | SEC_LOAD;
901 sreloc = bfd_make_section_with_flags (dynobj,
902 name,
903 flags);
904 if (sreloc == NULL
905 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
906 return FALSE;
908 elf_section_data (sec)->sreloc = sreloc;
911 /* If this is a global symbol, we count the number of
912 relocations we need for this symbol. */
913 if (h != NULL)
915 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
917 else
919 /* Track dynamic relocs needed for local syms too.
920 We really need local syms available to do this
921 easily. Oh well. */
923 asection *s;
924 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
925 sec, r_symndx);
926 if (s == NULL)
927 return FALSE;
929 head = ((struct elf64_x86_64_dyn_relocs **)
930 &elf_section_data (s)->local_dynrel);
933 p = *head;
934 if (p == NULL || p->sec != sec)
936 bfd_size_type amt = sizeof *p;
937 p = ((struct elf64_x86_64_dyn_relocs *)
938 bfd_alloc (htab->elf.dynobj, amt));
939 if (p == NULL)
940 return FALSE;
941 p->next = *head;
942 *head = p;
943 p->sec = sec;
944 p->count = 0;
945 p->pc_count = 0;
948 p->count += 1;
949 if (r_type == R_X86_64_PC8
950 || r_type == R_X86_64_PC16
951 || r_type == R_X86_64_PC32)
952 p->pc_count += 1;
954 break;
956 /* This relocation describes the C++ object vtable hierarchy.
957 Reconstruct it for later use during GC. */
958 case R_X86_64_GNU_VTINHERIT:
959 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
960 return FALSE;
961 break;
963 /* This relocation describes which C++ vtable entries are actually
964 used. Record for later use during GC. */
965 case R_X86_64_GNU_VTENTRY:
966 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
967 return FALSE;
968 break;
970 default:
971 break;
975 return TRUE;
978 /* Return the section that should be marked against GC for a given
979 relocation. */
981 static asection *
982 elf64_x86_64_gc_mark_hook (asection *sec,
983 struct bfd_link_info *info ATTRIBUTE_UNUSED,
984 Elf_Internal_Rela *rel,
985 struct elf_link_hash_entry *h,
986 Elf_Internal_Sym *sym)
988 if (h != NULL)
990 switch (ELF64_R_TYPE (rel->r_info))
992 case R_X86_64_GNU_VTINHERIT:
993 case R_X86_64_GNU_VTENTRY:
994 break;
996 default:
997 switch (h->root.type)
999 case bfd_link_hash_defined:
1000 case bfd_link_hash_defweak:
1001 return h->root.u.def.section;
1003 case bfd_link_hash_common:
1004 return h->root.u.c.p->section;
1006 default:
1007 break;
1011 else
1012 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1014 return NULL;
1017 /* Update the got entry reference counts for the section being removed. */
1019 static bfd_boolean
1020 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1021 asection *sec, const Elf_Internal_Rela *relocs)
1023 Elf_Internal_Shdr *symtab_hdr;
1024 struct elf_link_hash_entry **sym_hashes;
1025 bfd_signed_vma *local_got_refcounts;
1026 const Elf_Internal_Rela *rel, *relend;
1028 elf_section_data (sec)->local_dynrel = NULL;
1030 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1031 sym_hashes = elf_sym_hashes (abfd);
1032 local_got_refcounts = elf_local_got_refcounts (abfd);
1034 relend = relocs + sec->reloc_count;
1035 for (rel = relocs; rel < relend; rel++)
1037 unsigned long r_symndx;
1038 unsigned int r_type;
1039 struct elf_link_hash_entry *h = NULL;
1041 r_symndx = ELF64_R_SYM (rel->r_info);
1042 if (r_symndx >= symtab_hdr->sh_info)
1044 struct elf64_x86_64_link_hash_entry *eh;
1045 struct elf64_x86_64_dyn_relocs **pp;
1046 struct elf64_x86_64_dyn_relocs *p;
1048 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1049 while (h->root.type == bfd_link_hash_indirect
1050 || h->root.type == bfd_link_hash_warning)
1051 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1052 eh = (struct elf64_x86_64_link_hash_entry *) h;
1054 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
1055 if (p->sec == sec)
1057 /* Everything must go for SEC. */
1058 *pp = p->next;
1059 break;
1063 r_type = ELF64_R_TYPE (rel->r_info);
1064 r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
1065 switch (r_type)
1067 case R_X86_64_TLSLD:
1068 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
1069 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
1070 break;
1072 case R_X86_64_TLSGD:
1073 case R_X86_64_GOTTPOFF:
1074 case R_X86_64_GOT32:
1075 case R_X86_64_GOTPCREL:
1076 if (h != NULL)
1078 if (h->got.refcount > 0)
1079 h->got.refcount -= 1;
1081 else if (local_got_refcounts != NULL)
1083 if (local_got_refcounts[r_symndx] > 0)
1084 local_got_refcounts[r_symndx] -= 1;
1086 break;
1088 case R_X86_64_8:
1089 case R_X86_64_16:
1090 case R_X86_64_32:
1091 case R_X86_64_64:
1092 case R_X86_64_32S:
1093 case R_X86_64_PC8:
1094 case R_X86_64_PC16:
1095 case R_X86_64_PC32:
1096 if (info->shared)
1097 break;
1098 /* Fall thru */
1100 case R_X86_64_PLT32:
1101 if (h != NULL)
1103 if (h->plt.refcount > 0)
1104 h->plt.refcount -= 1;
1106 break;
1108 default:
1109 break;
1113 return TRUE;
1116 /* Adjust a symbol defined by a dynamic object and referenced by a
1117 regular object. The current definition is in some section of the
1118 dynamic object, but we're not including those sections. We have to
1119 change the definition to something the rest of the link can
1120 understand. */
1122 static bfd_boolean
1123 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
1124 struct elf_link_hash_entry *h)
1126 struct elf64_x86_64_link_hash_table *htab;
1127 asection *s;
1128 unsigned int power_of_two;
1130 /* If this is a function, put it in the procedure linkage table. We
1131 will fill in the contents of the procedure linkage table later,
1132 when we know the address of the .got section. */
1133 if (h->type == STT_FUNC
1134 || h->needs_plt)
1136 if (h->plt.refcount <= 0
1137 || SYMBOL_CALLS_LOCAL (info, h)
1138 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1139 && h->root.type == bfd_link_hash_undefweak))
1141 /* This case can occur if we saw a PLT32 reloc in an input
1142 file, but the symbol was never referred to by a dynamic
1143 object, or if all references were garbage collected. In
1144 such a case, we don't actually need to build a procedure
1145 linkage table, and we can just do a PC32 reloc instead. */
1146 h->plt.offset = (bfd_vma) -1;
1147 h->needs_plt = 0;
1150 return TRUE;
1152 else
1153 /* It's possible that we incorrectly decided a .plt reloc was
1154 needed for an R_X86_64_PC32 reloc to a non-function sym in
1155 check_relocs. We can't decide accurately between function and
1156 non-function syms in check-relocs; Objects loaded later in
1157 the link may change h->type. So fix it now. */
1158 h->plt.offset = (bfd_vma) -1;
1160 /* If this is a weak symbol, and there is a real definition, the
1161 processor independent code will have arranged for us to see the
1162 real definition first, and we can just use the same value. */
1163 if (h->u.weakdef != NULL)
1165 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
1166 || h->u.weakdef->root.type == bfd_link_hash_defweak);
1167 h->root.u.def.section = h->u.weakdef->root.u.def.section;
1168 h->root.u.def.value = h->u.weakdef->root.u.def.value;
1169 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1170 h->non_got_ref = h->u.weakdef->non_got_ref;
1171 return TRUE;
1174 /* This is a reference to a symbol defined by a dynamic object which
1175 is not a function. */
1177 /* If we are creating a shared library, we must presume that the
1178 only references to the symbol are via the global offset table.
1179 For such cases we need not do anything here; the relocations will
1180 be handled correctly by relocate_section. */
1181 if (info->shared)
1182 return TRUE;
1184 /* If there are no references to this symbol that do not use the
1185 GOT, we don't need to generate a copy reloc. */
1186 if (!h->non_got_ref)
1187 return TRUE;
1189 /* If -z nocopyreloc was given, we won't generate them either. */
1190 if (info->nocopyreloc)
1192 h->non_got_ref = 0;
1193 return TRUE;
1196 if (ELIMINATE_COPY_RELOCS)
1198 struct elf64_x86_64_link_hash_entry * eh;
1199 struct elf64_x86_64_dyn_relocs *p;
1201 eh = (struct elf64_x86_64_link_hash_entry *) h;
1202 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1204 s = p->sec->output_section;
1205 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1206 break;
1209 /* If we didn't find any dynamic relocs in read-only sections, then
1210 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1211 if (p == NULL)
1213 h->non_got_ref = 0;
1214 return TRUE;
1218 /* We must allocate the symbol in our .dynbss section, which will
1219 become part of the .bss section of the executable. There will be
1220 an entry for this symbol in the .dynsym section. The dynamic
1221 object will contain position independent code, so all references
1222 from the dynamic object to this symbol will go through the global
1223 offset table. The dynamic linker will use the .dynsym entry to
1224 determine the address it must put in the global offset table, so
1225 both the dynamic object and the regular object will refer to the
1226 same memory location for the variable. */
1228 htab = elf64_x86_64_hash_table (info);
1230 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1231 to copy the initial value out of the dynamic object and into the
1232 runtime process image. */
1233 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1235 htab->srelbss->size += sizeof (Elf64_External_Rela);
1236 h->needs_copy = 1;
1239 /* We need to figure out the alignment required for this symbol. I
1240 have no idea how ELF linkers handle this. 16-bytes is the size
1241 of the largest type that requires hard alignment -- long double. */
1242 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1243 this construct. */
1244 power_of_two = bfd_log2 (h->size);
1245 if (power_of_two > 4)
1246 power_of_two = 4;
1248 /* Apply the required alignment. */
1249 s = htab->sdynbss;
1250 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
1251 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1253 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1254 return FALSE;
1257 /* Define the symbol as being at this point in the section. */
1258 h->root.u.def.section = s;
1259 h->root.u.def.value = s->size;
1261 /* Increment the section size to make room for the symbol. */
1262 s->size += h->size;
1264 return TRUE;
1267 /* Allocate space in .plt, .got and associated reloc sections for
1268 dynamic relocs. */
1270 static bfd_boolean
1271 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1273 struct bfd_link_info *info;
1274 struct elf64_x86_64_link_hash_table *htab;
1275 struct elf64_x86_64_link_hash_entry *eh;
1276 struct elf64_x86_64_dyn_relocs *p;
1278 if (h->root.type == bfd_link_hash_indirect)
1279 return TRUE;
1281 if (h->root.type == bfd_link_hash_warning)
1282 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1284 info = (struct bfd_link_info *) inf;
1285 htab = elf64_x86_64_hash_table (info);
1287 if (htab->elf.dynamic_sections_created
1288 && h->plt.refcount > 0)
1290 /* Make sure this symbol is output as a dynamic symbol.
1291 Undefined weak syms won't yet be marked as dynamic. */
1292 if (h->dynindx == -1
1293 && !h->forced_local)
1295 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1296 return FALSE;
1299 if (info->shared
1300 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1302 asection *s = htab->splt;
1304 /* If this is the first .plt entry, make room for the special
1305 first entry. */
1306 if (s->size == 0)
1307 s->size += PLT_ENTRY_SIZE;
1309 h->plt.offset = s->size;
1311 /* If this symbol is not defined in a regular file, and we are
1312 not generating a shared library, then set the symbol to this
1313 location in the .plt. This is required to make function
1314 pointers compare as equal between the normal executable and
1315 the shared library. */
1316 if (! info->shared
1317 && !h->def_regular)
1319 h->root.u.def.section = s;
1320 h->root.u.def.value = h->plt.offset;
1323 /* Make room for this entry. */
1324 s->size += PLT_ENTRY_SIZE;
1326 /* We also need to make an entry in the .got.plt section, which
1327 will be placed in the .got section by the linker script. */
1328 htab->sgotplt->size += GOT_ENTRY_SIZE;
1330 /* We also need to make an entry in the .rela.plt section. */
1331 htab->srelplt->size += sizeof (Elf64_External_Rela);
1333 else
1335 h->plt.offset = (bfd_vma) -1;
1336 h->needs_plt = 0;
1339 else
1341 h->plt.offset = (bfd_vma) -1;
1342 h->needs_plt = 0;
1345 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1346 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1347 if (h->got.refcount > 0
1348 && !info->shared
1349 && h->dynindx == -1
1350 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1351 h->got.offset = (bfd_vma) -1;
1352 else if (h->got.refcount > 0)
1354 asection *s;
1355 bfd_boolean dyn;
1356 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1358 /* Make sure this symbol is output as a dynamic symbol.
1359 Undefined weak syms won't yet be marked as dynamic. */
1360 if (h->dynindx == -1
1361 && !h->forced_local)
1363 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1364 return FALSE;
1367 s = htab->sgot;
1368 h->got.offset = s->size;
1369 s->size += GOT_ENTRY_SIZE;
1370 /* R_X86_64_TLSGD needs 2 consecutive GOT slots. */
1371 if (tls_type == GOT_TLS_GD)
1372 s->size += GOT_ENTRY_SIZE;
1373 dyn = htab->elf.dynamic_sections_created;
1374 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1375 and two if global.
1376 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1377 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1378 || tls_type == GOT_TLS_IE)
1379 htab->srelgot->size += sizeof (Elf64_External_Rela);
1380 else if (tls_type == GOT_TLS_GD)
1381 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
1382 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1383 || h->root.type != bfd_link_hash_undefweak)
1384 && (info->shared
1385 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1386 htab->srelgot->size += sizeof (Elf64_External_Rela);
1388 else
1389 h->got.offset = (bfd_vma) -1;
1391 eh = (struct elf64_x86_64_link_hash_entry *) h;
1392 if (eh->dyn_relocs == NULL)
1393 return TRUE;
1395 /* In the shared -Bsymbolic case, discard space allocated for
1396 dynamic pc-relative relocs against symbols which turn out to be
1397 defined in regular objects. For the normal shared case, discard
1398 space for pc-relative relocs that have become local due to symbol
1399 visibility changes. */
1401 if (info->shared)
1403 /* Relocs that use pc_count are those that appear on a call
1404 insn, or certain REL relocs that can generated via assembly.
1405 We want calls to protected symbols to resolve directly to the
1406 function rather than going via the plt. If people want
1407 function pointer comparisons to work as expected then they
1408 should avoid writing weird assembly. */
1409 if (SYMBOL_CALLS_LOCAL (info, h))
1411 struct elf64_x86_64_dyn_relocs **pp;
1413 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1415 p->count -= p->pc_count;
1416 p->pc_count = 0;
1417 if (p->count == 0)
1418 *pp = p->next;
1419 else
1420 pp = &p->next;
1424 /* Also discard relocs on undefined weak syms with non-default
1425 visibility. */
1426 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1427 && h->root.type == bfd_link_hash_undefweak)
1428 eh->dyn_relocs = NULL;
1430 else if (ELIMINATE_COPY_RELOCS)
1432 /* For the non-shared case, discard space for relocs against
1433 symbols which turn out to need copy relocs or are not
1434 dynamic. */
1436 if (!h->non_got_ref
1437 && ((h->def_dynamic
1438 && !h->def_regular)
1439 || (htab->elf.dynamic_sections_created
1440 && (h->root.type == bfd_link_hash_undefweak
1441 || h->root.type == bfd_link_hash_undefined))))
1443 /* Make sure this symbol is output as a dynamic symbol.
1444 Undefined weak syms won't yet be marked as dynamic. */
1445 if (h->dynindx == -1
1446 && !h->forced_local)
1448 if (! bfd_elf_link_record_dynamic_symbol (info, h))
1449 return FALSE;
1452 /* If that succeeded, we know we'll be keeping all the
1453 relocs. */
1454 if (h->dynindx != -1)
1455 goto keep;
1458 eh->dyn_relocs = NULL;
1460 keep: ;
1463 /* Finally, allocate space. */
1464 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1466 asection *sreloc = elf_section_data (p->sec)->sreloc;
1467 sreloc->size += p->count * sizeof (Elf64_External_Rela);
1470 return TRUE;
1473 /* Find any dynamic relocs that apply to read-only sections. */
1475 static bfd_boolean
1476 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1478 struct elf64_x86_64_link_hash_entry *eh;
1479 struct elf64_x86_64_dyn_relocs *p;
1481 if (h->root.type == bfd_link_hash_warning)
1482 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1484 eh = (struct elf64_x86_64_link_hash_entry *) h;
1485 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1487 asection *s = p->sec->output_section;
1489 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1491 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1493 info->flags |= DF_TEXTREL;
1495 /* Not an error, just cut short the traversal. */
1496 return FALSE;
1499 return TRUE;
1502 /* Set the sizes of the dynamic sections. */
1504 static bfd_boolean
1505 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1506 struct bfd_link_info *info)
1508 struct elf64_x86_64_link_hash_table *htab;
1509 bfd *dynobj;
1510 asection *s;
1511 bfd_boolean relocs;
1512 bfd *ibfd;
1514 htab = elf64_x86_64_hash_table (info);
1515 dynobj = htab->elf.dynobj;
1516 if (dynobj == NULL)
1517 abort ();
1519 if (htab->elf.dynamic_sections_created)
1521 /* Set the contents of the .interp section to the interpreter. */
1522 if (info->executable)
1524 s = bfd_get_section_by_name (dynobj, ".interp");
1525 if (s == NULL)
1526 abort ();
1527 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
1528 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1532 /* Set up .got offsets for local syms, and space for local dynamic
1533 relocs. */
1534 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1536 bfd_signed_vma *local_got;
1537 bfd_signed_vma *end_local_got;
1538 char *local_tls_type;
1539 bfd_size_type locsymcount;
1540 Elf_Internal_Shdr *symtab_hdr;
1541 asection *srel;
1543 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1544 continue;
1546 for (s = ibfd->sections; s != NULL; s = s->next)
1548 struct elf64_x86_64_dyn_relocs *p;
1550 for (p = *((struct elf64_x86_64_dyn_relocs **)
1551 &elf_section_data (s)->local_dynrel);
1552 p != NULL;
1553 p = p->next)
1555 if (!bfd_is_abs_section (p->sec)
1556 && bfd_is_abs_section (p->sec->output_section))
1558 /* Input section has been discarded, either because
1559 it is a copy of a linkonce section or due to
1560 linker script /DISCARD/, so we'll be discarding
1561 the relocs too. */
1563 else if (p->count != 0)
1565 srel = elf_section_data (p->sec)->sreloc;
1566 srel->size += p->count * sizeof (Elf64_External_Rela);
1567 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1568 info->flags |= DF_TEXTREL;
1574 local_got = elf_local_got_refcounts (ibfd);
1575 if (!local_got)
1576 continue;
1578 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1579 locsymcount = symtab_hdr->sh_info;
1580 end_local_got = local_got + locsymcount;
1581 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
1582 s = htab->sgot;
1583 srel = htab->srelgot;
1584 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1586 if (*local_got > 0)
1588 *local_got = s->size;
1589 s->size += GOT_ENTRY_SIZE;
1590 if (*local_tls_type == GOT_TLS_GD)
1591 s->size += GOT_ENTRY_SIZE;
1592 if (info->shared
1593 || *local_tls_type == GOT_TLS_GD
1594 || *local_tls_type == GOT_TLS_IE)
1595 srel->size += sizeof (Elf64_External_Rela);
1597 else
1598 *local_got = (bfd_vma) -1;
1602 if (htab->tls_ld_got.refcount > 0)
1604 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1605 relocs. */
1606 htab->tls_ld_got.offset = htab->sgot->size;
1607 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
1608 htab->srelgot->size += sizeof (Elf64_External_Rela);
1610 else
1611 htab->tls_ld_got.offset = -1;
1613 /* Allocate global sym .plt and .got entries, and space for global
1614 sym dynamic relocs. */
1615 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1617 /* We now have determined the sizes of the various dynamic sections.
1618 Allocate memory for them. */
1619 relocs = FALSE;
1620 for (s = dynobj->sections; s != NULL; s = s->next)
1622 if ((s->flags & SEC_LINKER_CREATED) == 0)
1623 continue;
1625 if (s == htab->splt
1626 || s == htab->sgot
1627 || s == htab->sgotplt
1628 || s == htab->sdynbss)
1630 /* Strip this section if we don't need it; see the
1631 comment below. */
1633 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1635 if (s->size != 0 && s != htab->srelplt)
1636 relocs = TRUE;
1638 /* We use the reloc_count field as a counter if we need
1639 to copy relocs into the output file. */
1640 s->reloc_count = 0;
1642 else
1644 /* It's not one of our sections, so don't allocate space. */
1645 continue;
1648 if (s->size == 0)
1650 /* If we don't need this section, strip it from the
1651 output file. This is mostly to handle .rela.bss and
1652 .rela.plt. We must create both sections in
1653 create_dynamic_sections, because they must be created
1654 before the linker maps input sections to output
1655 sections. The linker does that before
1656 adjust_dynamic_symbol is called, and it is that
1657 function which decides whether anything needs to go
1658 into these sections. */
1660 s->flags |= SEC_EXCLUDE;
1661 continue;
1664 /* Allocate memory for the section contents. We use bfd_zalloc
1665 here in case unused entries are not reclaimed before the
1666 section's contents are written out. This should not happen,
1667 but this way if it does, we get a R_X86_64_NONE reloc instead
1668 of garbage. */
1669 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
1670 if (s->contents == NULL)
1671 return FALSE;
1674 if (htab->elf.dynamic_sections_created)
1676 /* Add some entries to the .dynamic section. We fill in the
1677 values later, in elf64_x86_64_finish_dynamic_sections, but we
1678 must add the entries now so that we get the correct size for
1679 the .dynamic section. The DT_DEBUG entry is filled in by the
1680 dynamic linker and used by the debugger. */
1681 #define add_dynamic_entry(TAG, VAL) \
1682 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
1684 if (info->executable)
1686 if (!add_dynamic_entry (DT_DEBUG, 0))
1687 return FALSE;
1690 if (htab->splt->size != 0)
1692 if (!add_dynamic_entry (DT_PLTGOT, 0)
1693 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1694 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1695 || !add_dynamic_entry (DT_JMPREL, 0))
1696 return FALSE;
1699 if (relocs)
1701 if (!add_dynamic_entry (DT_RELA, 0)
1702 || !add_dynamic_entry (DT_RELASZ, 0)
1703 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1704 return FALSE;
1706 /* If any dynamic relocs apply to a read-only section,
1707 then we need a DT_TEXTREL entry. */
1708 if ((info->flags & DF_TEXTREL) == 0)
1709 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1710 (PTR) info);
1712 if ((info->flags & DF_TEXTREL) != 0)
1714 if (!add_dynamic_entry (DT_TEXTREL, 0))
1715 return FALSE;
1719 #undef add_dynamic_entry
1721 return TRUE;
1724 /* Return the base VMA address which should be subtracted from real addresses
1725 when resolving @dtpoff relocation.
1726 This is PT_TLS segment p_vaddr. */
1728 static bfd_vma
1729 dtpoff_base (struct bfd_link_info *info)
1731 /* If tls_sec is NULL, we should have signalled an error already. */
1732 if (elf_hash_table (info)->tls_sec == NULL)
1733 return 0;
1734 return elf_hash_table (info)->tls_sec->vma;
1737 /* Return the relocation value for @tpoff relocation
1738 if STT_TLS virtual address is ADDRESS. */
1740 static bfd_vma
1741 tpoff (struct bfd_link_info *info, bfd_vma address)
1743 struct elf_link_hash_table *htab = elf_hash_table (info);
1745 /* If tls_segment is NULL, we should have signalled an error already. */
1746 if (htab->tls_sec == NULL)
1747 return 0;
1748 return address - htab->tls_size - htab->tls_sec->vma;
1751 /* Is the instruction before OFFSET in CONTENTS a 32bit relative
1752 branch? */
1754 static bfd_boolean
1755 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
1757 /* Opcode Instruction
1758 0xe8 call
1759 0xe9 jump
1760 0x0f 0x8x conditional jump */
1761 return ((offset > 0
1762 && (contents [offset - 1] == 0xe8
1763 || contents [offset - 1] == 0xe9))
1764 || (offset > 1
1765 && contents [offset - 2] == 0x0f
1766 && (contents [offset - 1] & 0xf0) == 0x80));
1769 /* Relocate an x86_64 ELF section. */
1771 static bfd_boolean
1772 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
1773 bfd *input_bfd, asection *input_section,
1774 bfd_byte *contents, Elf_Internal_Rela *relocs,
1775 Elf_Internal_Sym *local_syms,
1776 asection **local_sections)
1778 struct elf64_x86_64_link_hash_table *htab;
1779 Elf_Internal_Shdr *symtab_hdr;
1780 struct elf_link_hash_entry **sym_hashes;
1781 bfd_vma *local_got_offsets;
1782 Elf_Internal_Rela *rel;
1783 Elf_Internal_Rela *relend;
1785 if (info->relocatable)
1786 return TRUE;
1788 htab = elf64_x86_64_hash_table (info);
1789 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1790 sym_hashes = elf_sym_hashes (input_bfd);
1791 local_got_offsets = elf_local_got_offsets (input_bfd);
1793 rel = relocs;
1794 relend = relocs + input_section->reloc_count;
1795 for (; rel < relend; rel++)
1797 unsigned int r_type;
1798 reloc_howto_type *howto;
1799 unsigned long r_symndx;
1800 struct elf_link_hash_entry *h;
1801 Elf_Internal_Sym *sym;
1802 asection *sec;
1803 bfd_vma off;
1804 bfd_vma relocation;
1805 bfd_boolean unresolved_reloc;
1806 bfd_reloc_status_type r;
1807 int tls_type;
1809 r_type = ELF64_R_TYPE (rel->r_info);
1810 if (r_type == (int) R_X86_64_GNU_VTINHERIT
1811 || r_type == (int) R_X86_64_GNU_VTENTRY)
1812 continue;
1814 if (r_type >= R_X86_64_max)
1816 bfd_set_error (bfd_error_bad_value);
1817 return FALSE;
1820 howto = x86_64_elf_howto_table + r_type;
1821 r_symndx = ELF64_R_SYM (rel->r_info);
1822 h = NULL;
1823 sym = NULL;
1824 sec = NULL;
1825 unresolved_reloc = FALSE;
1826 if (r_symndx < symtab_hdr->sh_info)
1828 sym = local_syms + r_symndx;
1829 sec = local_sections[r_symndx];
1831 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1833 else
1835 bfd_boolean warned;
1837 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1838 r_symndx, symtab_hdr, sym_hashes,
1839 h, sec, relocation,
1840 unresolved_reloc, warned);
1842 /* When generating a shared object, the relocations handled here are
1843 copied into the output file to be resolved at run time. */
1844 switch (r_type)
1846 case R_X86_64_GOT32:
1847 /* Relocation is to the entry for this symbol in the global
1848 offset table. */
1849 case R_X86_64_GOTPCREL:
1850 /* Use global offset table as symbol value. */
1851 if (htab->sgot == NULL)
1852 abort ();
1854 if (h != NULL)
1856 bfd_boolean dyn;
1858 off = h->got.offset;
1859 dyn = htab->elf.dynamic_sections_created;
1861 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1862 || (info->shared
1863 && SYMBOL_REFERENCES_LOCAL (info, h))
1864 || (ELF_ST_VISIBILITY (h->other)
1865 && h->root.type == bfd_link_hash_undefweak))
1867 /* This is actually a static link, or it is a -Bsymbolic
1868 link and the symbol is defined locally, or the symbol
1869 was forced to be local because of a version file. We
1870 must initialize this entry in the global offset table.
1871 Since the offset must always be a multiple of 8, we
1872 use the least significant bit to record whether we
1873 have initialized it already.
1875 When doing a dynamic link, we create a .rela.got
1876 relocation entry to initialize the value. This is
1877 done in the finish_dynamic_symbol routine. */
1878 if ((off & 1) != 0)
1879 off &= ~1;
1880 else
1882 bfd_put_64 (output_bfd, relocation,
1883 htab->sgot->contents + off);
1884 h->got.offset |= 1;
1887 else
1888 unresolved_reloc = FALSE;
1890 else
1892 if (local_got_offsets == NULL)
1893 abort ();
1895 off = local_got_offsets[r_symndx];
1897 /* The offset must always be a multiple of 8. We use
1898 the least significant bit to record whether we have
1899 already generated the necessary reloc. */
1900 if ((off & 1) != 0)
1901 off &= ~1;
1902 else
1904 bfd_put_64 (output_bfd, relocation,
1905 htab->sgot->contents + off);
1907 if (info->shared)
1909 asection *s;
1910 Elf_Internal_Rela outrel;
1911 bfd_byte *loc;
1913 /* We need to generate a R_X86_64_RELATIVE reloc
1914 for the dynamic linker. */
1915 s = htab->srelgot;
1916 if (s == NULL)
1917 abort ();
1919 outrel.r_offset = (htab->sgot->output_section->vma
1920 + htab->sgot->output_offset
1921 + off);
1922 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1923 outrel.r_addend = relocation;
1924 loc = s->contents;
1925 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
1926 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1929 local_got_offsets[r_symndx] |= 1;
1933 if (off >= (bfd_vma) -2)
1934 abort ();
1936 relocation = htab->sgot->output_section->vma
1937 + htab->sgot->output_offset + off;
1938 if (r_type != R_X86_64_GOTPCREL)
1939 relocation -= htab->sgotplt->output_section->vma
1940 - htab->sgotplt->output_offset;
1942 break;
1944 case R_X86_64_PLT32:
1945 /* Relocation is to the entry for this symbol in the
1946 procedure linkage table. */
1948 /* Resolve a PLT32 reloc against a local symbol directly,
1949 without using the procedure linkage table. */
1950 if (h == NULL)
1951 break;
1953 if (h->plt.offset == (bfd_vma) -1
1954 || htab->splt == NULL)
1956 /* We didn't make a PLT entry for this symbol. This
1957 happens when statically linking PIC code, or when
1958 using -Bsymbolic. */
1959 break;
1962 relocation = (htab->splt->output_section->vma
1963 + htab->splt->output_offset
1964 + h->plt.offset);
1965 unresolved_reloc = FALSE;
1966 break;
1968 case R_X86_64_PC8:
1969 case R_X86_64_PC16:
1970 case R_X86_64_PC32:
1971 if (info->shared
1972 && !SYMBOL_REFERENCES_LOCAL (info, h)
1973 && (input_section->flags & SEC_ALLOC) != 0
1974 && (input_section->flags & SEC_READONLY) != 0
1975 && (!h->def_regular
1976 || r_type != R_X86_64_PC32
1977 || h->type != STT_FUNC
1978 || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
1979 || !is_32bit_relative_branch (contents,
1980 rel->r_offset)))
1982 if (h->def_regular
1983 && r_type == R_X86_64_PC32
1984 && h->type == STT_FUNC
1985 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
1986 (*_bfd_error_handler)
1987 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
1988 input_bfd, h->root.root.string);
1989 else
1990 (*_bfd_error_handler)
1991 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
1992 input_bfd, x86_64_elf_howto_table[r_type].name,
1993 h->root.root.string);
1994 bfd_set_error (bfd_error_bad_value);
1995 return FALSE;
1997 /* Fall through. */
1999 case R_X86_64_8:
2000 case R_X86_64_16:
2001 case R_X86_64_32:
2002 case R_X86_64_64:
2003 /* FIXME: The ABI says the linker should make sure the value is
2004 the same when it's zeroextended to 64 bit. */
2006 /* r_symndx will be zero only for relocs against symbols
2007 from removed linkonce sections, or sections discarded by
2008 a linker script. */
2009 if (r_symndx == 0
2010 || (input_section->flags & SEC_ALLOC) == 0)
2011 break;
2013 if ((info->shared
2014 && (h == NULL
2015 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2016 || h->root.type != bfd_link_hash_undefweak)
2017 && ((r_type != R_X86_64_PC8
2018 && r_type != R_X86_64_PC16
2019 && r_type != R_X86_64_PC32)
2020 || !SYMBOL_CALLS_LOCAL (info, h)))
2021 || (ELIMINATE_COPY_RELOCS
2022 && !info->shared
2023 && h != NULL
2024 && h->dynindx != -1
2025 && !h->non_got_ref
2026 && ((h->def_dynamic
2027 && !h->def_regular)
2028 || h->root.type == bfd_link_hash_undefweak
2029 || h->root.type == bfd_link_hash_undefined)))
2031 Elf_Internal_Rela outrel;
2032 bfd_byte *loc;
2033 bfd_boolean skip, relocate;
2034 asection *sreloc;
2036 /* When generating a shared object, these relocations
2037 are copied into the output file to be resolved at run
2038 time. */
2039 skip = FALSE;
2040 relocate = FALSE;
2042 outrel.r_offset =
2043 _bfd_elf_section_offset (output_bfd, info, input_section,
2044 rel->r_offset);
2045 if (outrel.r_offset == (bfd_vma) -1)
2046 skip = TRUE;
2047 else if (outrel.r_offset == (bfd_vma) -2)
2048 skip = TRUE, relocate = TRUE;
2050 outrel.r_offset += (input_section->output_section->vma
2051 + input_section->output_offset);
2053 if (skip)
2054 memset (&outrel, 0, sizeof outrel);
2056 /* h->dynindx may be -1 if this symbol was marked to
2057 become local. */
2058 else if (h != NULL
2059 && h->dynindx != -1
2060 && (r_type == R_X86_64_PC8
2061 || r_type == R_X86_64_PC16
2062 || r_type == R_X86_64_PC32
2063 || !info->shared
2064 || !info->symbolic
2065 || !h->def_regular))
2067 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2068 outrel.r_addend = rel->r_addend;
2070 else
2072 /* This symbol is local, or marked to become local. */
2073 if (r_type == R_X86_64_64)
2075 relocate = TRUE;
2076 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2077 outrel.r_addend = relocation + rel->r_addend;
2079 else
2081 long sindx;
2083 if (bfd_is_abs_section (sec))
2084 sindx = 0;
2085 else if (sec == NULL || sec->owner == NULL)
2087 bfd_set_error (bfd_error_bad_value);
2088 return FALSE;
2090 else
2092 asection *osec;
2094 osec = sec->output_section;
2095 sindx = elf_section_data (osec)->dynindx;
2096 BFD_ASSERT (sindx > 0);
2099 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2100 outrel.r_addend = relocation + rel->r_addend;
2104 sreloc = elf_section_data (input_section)->sreloc;
2105 if (sreloc == NULL)
2106 abort ();
2108 loc = sreloc->contents;
2109 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2110 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2112 /* If this reloc is against an external symbol, we do
2113 not want to fiddle with the addend. Otherwise, we
2114 need to include the symbol value so that it becomes
2115 an addend for the dynamic reloc. */
2116 if (! relocate)
2117 continue;
2120 break;
2122 case R_X86_64_TLSGD:
2123 case R_X86_64_GOTTPOFF:
2124 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
2125 tls_type = GOT_UNKNOWN;
2126 if (h == NULL && local_got_offsets)
2127 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2128 else if (h != NULL)
2130 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2131 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2132 r_type = R_X86_64_TPOFF32;
2134 if (r_type == R_X86_64_TLSGD)
2136 if (tls_type == GOT_TLS_IE)
2137 r_type = R_X86_64_GOTTPOFF;
2140 if (r_type == R_X86_64_TPOFF32)
2142 BFD_ASSERT (! unresolved_reloc);
2143 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2145 unsigned int i;
2146 static unsigned char tlsgd[8]
2147 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2149 /* GD->LE transition.
2150 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2151 .word 0x6666; rex64; call __tls_get_addr@plt
2152 Change it into:
2153 movq %fs:0, %rax
2154 leaq foo@tpoff(%rax), %rax */
2155 BFD_ASSERT (rel->r_offset >= 4);
2156 for (i = 0; i < 4; i++)
2157 BFD_ASSERT (bfd_get_8 (input_bfd,
2158 contents + rel->r_offset - 4 + i)
2159 == tlsgd[i]);
2160 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2161 for (i = 0; i < 4; i++)
2162 BFD_ASSERT (bfd_get_8 (input_bfd,
2163 contents + rel->r_offset + 4 + i)
2164 == tlsgd[i+4]);
2165 BFD_ASSERT (rel + 1 < relend);
2166 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2167 memcpy (contents + rel->r_offset - 4,
2168 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2169 16);
2170 bfd_put_32 (output_bfd, tpoff (info, relocation),
2171 contents + rel->r_offset + 8);
2172 /* Skip R_X86_64_PLT32. */
2173 rel++;
2174 continue;
2176 else
2178 unsigned int val, type, reg;
2180 /* IE->LE transition:
2181 Originally it can be one of:
2182 movq foo@gottpoff(%rip), %reg
2183 addq foo@gottpoff(%rip), %reg
2184 We change it into:
2185 movq $foo, %reg
2186 leaq foo(%reg), %reg
2187 addq $foo, %reg. */
2188 BFD_ASSERT (rel->r_offset >= 3);
2189 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2190 BFD_ASSERT (val == 0x48 || val == 0x4c);
2191 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2192 BFD_ASSERT (type == 0x8b || type == 0x03);
2193 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2194 BFD_ASSERT ((reg & 0xc7) == 5);
2195 reg >>= 3;
2196 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
2197 if (type == 0x8b)
2199 /* movq */
2200 if (val == 0x4c)
2201 bfd_put_8 (output_bfd, 0x49,
2202 contents + rel->r_offset - 3);
2203 bfd_put_8 (output_bfd, 0xc7,
2204 contents + rel->r_offset - 2);
2205 bfd_put_8 (output_bfd, 0xc0 | reg,
2206 contents + rel->r_offset - 1);
2208 else if (reg == 4)
2210 /* addq -> addq - addressing with %rsp/%r12 is
2211 special */
2212 if (val == 0x4c)
2213 bfd_put_8 (output_bfd, 0x49,
2214 contents + rel->r_offset - 3);
2215 bfd_put_8 (output_bfd, 0x81,
2216 contents + rel->r_offset - 2);
2217 bfd_put_8 (output_bfd, 0xc0 | reg,
2218 contents + rel->r_offset - 1);
2220 else
2222 /* addq -> leaq */
2223 if (val == 0x4c)
2224 bfd_put_8 (output_bfd, 0x4d,
2225 contents + rel->r_offset - 3);
2226 bfd_put_8 (output_bfd, 0x8d,
2227 contents + rel->r_offset - 2);
2228 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2229 contents + rel->r_offset - 1);
2231 bfd_put_32 (output_bfd, tpoff (info, relocation),
2232 contents + rel->r_offset);
2233 continue;
2237 if (htab->sgot == NULL)
2238 abort ();
2240 if (h != NULL)
2241 off = h->got.offset;
2242 else
2244 if (local_got_offsets == NULL)
2245 abort ();
2247 off = local_got_offsets[r_symndx];
2250 if ((off & 1) != 0)
2251 off &= ~1;
2252 else
2254 Elf_Internal_Rela outrel;
2255 bfd_byte *loc;
2256 int dr_type, indx;
2258 if (htab->srelgot == NULL)
2259 abort ();
2261 outrel.r_offset = (htab->sgot->output_section->vma
2262 + htab->sgot->output_offset + off);
2264 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2265 if (r_type == R_X86_64_TLSGD)
2266 dr_type = R_X86_64_DTPMOD64;
2267 else
2268 dr_type = R_X86_64_TPOFF64;
2270 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2271 outrel.r_addend = 0;
2272 if (dr_type == R_X86_64_TPOFF64 && indx == 0)
2273 outrel.r_addend = relocation - dtpoff_base (info);
2274 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2276 loc = htab->srelgot->contents;
2277 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2278 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2280 if (r_type == R_X86_64_TLSGD)
2282 if (indx == 0)
2284 BFD_ASSERT (! unresolved_reloc);
2285 bfd_put_64 (output_bfd,
2286 relocation - dtpoff_base (info),
2287 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2289 else
2291 bfd_put_64 (output_bfd, 0,
2292 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2293 outrel.r_info = ELF64_R_INFO (indx,
2294 R_X86_64_DTPOFF64);
2295 outrel.r_offset += GOT_ENTRY_SIZE;
2296 htab->srelgot->reloc_count++;
2297 loc += sizeof (Elf64_External_Rela);
2298 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2302 if (h != NULL)
2303 h->got.offset |= 1;
2304 else
2305 local_got_offsets[r_symndx] |= 1;
2308 if (off >= (bfd_vma) -2)
2309 abort ();
2310 if (r_type == ELF64_R_TYPE (rel->r_info))
2312 relocation = htab->sgot->output_section->vma
2313 + htab->sgot->output_offset + off;
2314 unresolved_reloc = FALSE;
2316 else
2318 unsigned int i;
2319 static unsigned char tlsgd[8]
2320 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2322 /* GD->IE transition.
2323 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2324 .word 0x6666; rex64; call __tls_get_addr@plt
2325 Change it into:
2326 movq %fs:0, %rax
2327 addq foo@gottpoff(%rip), %rax */
2328 BFD_ASSERT (rel->r_offset >= 4);
2329 for (i = 0; i < 4; i++)
2330 BFD_ASSERT (bfd_get_8 (input_bfd,
2331 contents + rel->r_offset - 4 + i)
2332 == tlsgd[i]);
2333 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
2334 for (i = 0; i < 4; i++)
2335 BFD_ASSERT (bfd_get_8 (input_bfd,
2336 contents + rel->r_offset + 4 + i)
2337 == tlsgd[i+4]);
2338 BFD_ASSERT (rel + 1 < relend);
2339 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2340 memcpy (contents + rel->r_offset - 4,
2341 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2342 16);
2344 relocation = (htab->sgot->output_section->vma
2345 + htab->sgot->output_offset + off
2346 - rel->r_offset
2347 - input_section->output_section->vma
2348 - input_section->output_offset
2349 - 12);
2350 bfd_put_32 (output_bfd, relocation,
2351 contents + rel->r_offset + 8);
2352 /* Skip R_X86_64_PLT32. */
2353 rel++;
2354 continue;
2356 break;
2358 case R_X86_64_TLSLD:
2359 if (! info->shared)
2361 /* LD->LE transition:
2362 Ensure it is:
2363 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2364 We change it into:
2365 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2366 BFD_ASSERT (rel->r_offset >= 3);
2367 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2368 == 0x48);
2369 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2370 == 0x8d);
2371 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2372 == 0x3d);
2373 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
2374 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2375 == 0xe8);
2376 BFD_ASSERT (rel + 1 < relend);
2377 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2378 memcpy (contents + rel->r_offset - 3,
2379 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2380 /* Skip R_X86_64_PLT32. */
2381 rel++;
2382 continue;
2385 if (htab->sgot == NULL)
2386 abort ();
2388 off = htab->tls_ld_got.offset;
2389 if (off & 1)
2390 off &= ~1;
2391 else
2393 Elf_Internal_Rela outrel;
2394 bfd_byte *loc;
2396 if (htab->srelgot == NULL)
2397 abort ();
2399 outrel.r_offset = (htab->sgot->output_section->vma
2400 + htab->sgot->output_offset + off);
2402 bfd_put_64 (output_bfd, 0,
2403 htab->sgot->contents + off);
2404 bfd_put_64 (output_bfd, 0,
2405 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2406 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2407 outrel.r_addend = 0;
2408 loc = htab->srelgot->contents;
2409 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2410 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2411 htab->tls_ld_got.offset |= 1;
2413 relocation = htab->sgot->output_section->vma
2414 + htab->sgot->output_offset + off;
2415 unresolved_reloc = FALSE;
2416 break;
2418 case R_X86_64_DTPOFF32:
2419 if (info->shared || (input_section->flags & SEC_CODE) == 0)
2420 relocation -= dtpoff_base (info);
2421 else
2422 relocation = tpoff (info, relocation);
2423 break;
2425 case R_X86_64_TPOFF32:
2426 BFD_ASSERT (! info->shared);
2427 relocation = tpoff (info, relocation);
2428 break;
2430 default:
2431 break;
2434 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2435 because such sections are not SEC_ALLOC and thus ld.so will
2436 not process them. */
2437 if (unresolved_reloc
2438 && !((input_section->flags & SEC_DEBUGGING) != 0
2439 && h->def_dynamic))
2440 (*_bfd_error_handler)
2441 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
2442 input_bfd,
2443 input_section,
2444 (long) rel->r_offset,
2445 h->root.root.string);
2447 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2448 contents, rel->r_offset,
2449 relocation, rel->r_addend);
2451 if (r != bfd_reloc_ok)
2453 const char *name;
2455 if (h != NULL)
2456 name = h->root.root.string;
2457 else
2459 name = bfd_elf_string_from_elf_section (input_bfd,
2460 symtab_hdr->sh_link,
2461 sym->st_name);
2462 if (name == NULL)
2463 return FALSE;
2464 if (*name == '\0')
2465 name = bfd_section_name (input_bfd, sec);
2468 if (r == bfd_reloc_overflow)
2470 if (h != NULL
2471 && h->root.type == bfd_link_hash_undefweak
2472 && howto->pc_relative)
2473 /* Ignore reloc overflow on branches to undefweak syms. */
2474 continue;
2476 if (! ((*info->callbacks->reloc_overflow)
2477 (info, (h ? &h->root : NULL), name, howto->name,
2478 (bfd_vma) 0, input_bfd, input_section,
2479 rel->r_offset)))
2480 return FALSE;
2482 else
2484 (*_bfd_error_handler)
2485 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
2486 input_bfd, input_section,
2487 (long) rel->r_offset, name, (int) r);
2488 return FALSE;
2493 return TRUE;
2496 /* Finish up dynamic symbol handling. We set the contents of various
2497 dynamic sections here. */
2499 static bfd_boolean
2500 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
2501 struct bfd_link_info *info,
2502 struct elf_link_hash_entry *h,
2503 Elf_Internal_Sym *sym)
2505 struct elf64_x86_64_link_hash_table *htab;
2507 htab = elf64_x86_64_hash_table (info);
2509 if (h->plt.offset != (bfd_vma) -1)
2511 bfd_vma plt_index;
2512 bfd_vma got_offset;
2513 Elf_Internal_Rela rela;
2514 bfd_byte *loc;
2516 /* This symbol has an entry in the procedure linkage table. Set
2517 it up. */
2518 if (h->dynindx == -1
2519 || htab->splt == NULL
2520 || htab->sgotplt == NULL
2521 || htab->srelplt == NULL)
2522 abort ();
2524 /* Get the index in the procedure linkage table which
2525 corresponds to this symbol. This is the index of this symbol
2526 in all the symbols for which we are making plt entries. The
2527 first entry in the procedure linkage table is reserved. */
2528 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2530 /* Get the offset into the .got table of the entry that
2531 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
2532 bytes. The first three are reserved for the dynamic linker. */
2533 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2535 /* Fill in the entry in the procedure linkage table. */
2536 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
2537 PLT_ENTRY_SIZE);
2539 /* Insert the relocation positions of the plt section. The magic
2540 numbers at the end of the statements are the positions of the
2541 relocations in the plt section. */
2542 /* Put offset for jmp *name@GOTPCREL(%rip), since the
2543 instruction uses 6 bytes, subtract this value. */
2544 bfd_put_32 (output_bfd,
2545 (htab->sgotplt->output_section->vma
2546 + htab->sgotplt->output_offset
2547 + got_offset
2548 - htab->splt->output_section->vma
2549 - htab->splt->output_offset
2550 - h->plt.offset
2551 - 6),
2552 htab->splt->contents + h->plt.offset + 2);
2553 /* Put relocation index. */
2554 bfd_put_32 (output_bfd, plt_index,
2555 htab->splt->contents + h->plt.offset + 7);
2556 /* Put offset for jmp .PLT0. */
2557 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
2558 htab->splt->contents + h->plt.offset + 12);
2560 /* Fill in the entry in the global offset table, initially this
2561 points to the pushq instruction in the PLT which is at offset 6. */
2562 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
2563 + htab->splt->output_offset
2564 + h->plt.offset + 6),
2565 htab->sgotplt->contents + got_offset);
2567 /* Fill in the entry in the .rela.plt section. */
2568 rela.r_offset = (htab->sgotplt->output_section->vma
2569 + htab->sgotplt->output_offset
2570 + got_offset);
2571 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
2572 rela.r_addend = 0;
2573 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
2574 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2576 if (!h->def_regular)
2578 /* Mark the symbol as undefined, rather than as defined in
2579 the .plt section. Leave the value if there were any
2580 relocations where pointer equality matters (this is a clue
2581 for the dynamic linker, to make function pointer
2582 comparisons work between an application and shared
2583 library), otherwise set it to zero. If a function is only
2584 called from a binary, there is no need to slow down
2585 shared libraries because of that. */
2586 sym->st_shndx = SHN_UNDEF;
2587 if (!h->pointer_equality_needed)
2588 sym->st_value = 0;
2592 if (h->got.offset != (bfd_vma) -1
2593 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_GD
2594 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
2596 Elf_Internal_Rela rela;
2597 bfd_byte *loc;
2599 /* This symbol has an entry in the global offset table. Set it
2600 up. */
2601 if (htab->sgot == NULL || htab->srelgot == NULL)
2602 abort ();
2604 rela.r_offset = (htab->sgot->output_section->vma
2605 + htab->sgot->output_offset
2606 + (h->got.offset &~ (bfd_vma) 1));
2608 /* If this is a static link, or it is a -Bsymbolic link and the
2609 symbol is defined locally or was forced to be local because
2610 of a version file, we just want to emit a RELATIVE reloc.
2611 The entry in the global offset table will already have been
2612 initialized in the relocate_section function. */
2613 if (info->shared
2614 && SYMBOL_REFERENCES_LOCAL (info, h))
2616 BFD_ASSERT((h->got.offset & 1) != 0);
2617 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2618 rela.r_addend = (h->root.u.def.value
2619 + h->root.u.def.section->output_section->vma
2620 + h->root.u.def.section->output_offset);
2622 else
2624 BFD_ASSERT((h->got.offset & 1) == 0);
2625 bfd_put_64 (output_bfd, (bfd_vma) 0,
2626 htab->sgot->contents + h->got.offset);
2627 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
2628 rela.r_addend = 0;
2631 loc = htab->srelgot->contents;
2632 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2633 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2636 if (h->needs_copy)
2638 Elf_Internal_Rela rela;
2639 bfd_byte *loc;
2641 /* This symbol needs a copy reloc. Set it up. */
2643 if (h->dynindx == -1
2644 || (h->root.type != bfd_link_hash_defined
2645 && h->root.type != bfd_link_hash_defweak)
2646 || htab->srelbss == NULL)
2647 abort ();
2649 rela.r_offset = (h->root.u.def.value
2650 + h->root.u.def.section->output_section->vma
2651 + h->root.u.def.section->output_offset);
2652 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
2653 rela.r_addend = 0;
2654 loc = htab->srelbss->contents;
2655 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
2656 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2659 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2660 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2661 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2662 sym->st_shndx = SHN_ABS;
2664 return TRUE;
2667 /* Used to decide how to sort relocs in an optimal manner for the
2668 dynamic linker, before writing them out. */
2670 static enum elf_reloc_type_class
2671 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
2673 switch ((int) ELF64_R_TYPE (rela->r_info))
2675 case R_X86_64_RELATIVE:
2676 return reloc_class_relative;
2677 case R_X86_64_JUMP_SLOT:
2678 return reloc_class_plt;
2679 case R_X86_64_COPY:
2680 return reloc_class_copy;
2681 default:
2682 return reloc_class_normal;
2686 /* Finish up the dynamic sections. */
2688 static bfd_boolean
2689 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
2691 struct elf64_x86_64_link_hash_table *htab;
2692 bfd *dynobj;
2693 asection *sdyn;
2695 htab = elf64_x86_64_hash_table (info);
2696 dynobj = htab->elf.dynobj;
2697 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2699 if (htab->elf.dynamic_sections_created)
2701 Elf64_External_Dyn *dyncon, *dynconend;
2703 if (sdyn == NULL || htab->sgot == NULL)
2704 abort ();
2706 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2707 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
2708 for (; dyncon < dynconend; dyncon++)
2710 Elf_Internal_Dyn dyn;
2711 asection *s;
2713 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2715 switch (dyn.d_tag)
2717 default:
2718 continue;
2720 case DT_PLTGOT:
2721 s = htab->sgotplt;
2722 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
2723 break;
2725 case DT_JMPREL:
2726 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2727 break;
2729 case DT_PLTRELSZ:
2730 s = htab->srelplt->output_section;
2731 dyn.d_un.d_val = s->size;
2732 break;
2734 case DT_RELASZ:
2735 /* The procedure linkage table relocs (DT_JMPREL) should
2736 not be included in the overall relocs (DT_RELA).
2737 Therefore, we override the DT_RELASZ entry here to
2738 make it not include the JMPREL relocs. Since the
2739 linker script arranges for .rela.plt to follow all
2740 other relocation sections, we don't have to worry
2741 about changing the DT_RELA entry. */
2742 if (htab->srelplt != NULL)
2744 s = htab->srelplt->output_section;
2745 dyn.d_un.d_val -= s->size;
2747 break;
2750 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2753 /* Fill in the special first entry in the procedure linkage table. */
2754 if (htab->splt && htab->splt->size > 0)
2756 /* Fill in the first entry in the procedure linkage table. */
2757 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
2758 PLT_ENTRY_SIZE);
2759 /* Add offset for pushq GOT+8(%rip), since the instruction
2760 uses 6 bytes subtract this value. */
2761 bfd_put_32 (output_bfd,
2762 (htab->sgotplt->output_section->vma
2763 + htab->sgotplt->output_offset
2765 - htab->splt->output_section->vma
2766 - htab->splt->output_offset
2767 - 6),
2768 htab->splt->contents + 2);
2769 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
2770 the end of the instruction. */
2771 bfd_put_32 (output_bfd,
2772 (htab->sgotplt->output_section->vma
2773 + htab->sgotplt->output_offset
2774 + 16
2775 - htab->splt->output_section->vma
2776 - htab->splt->output_offset
2777 - 12),
2778 htab->splt->contents + 8);
2780 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
2781 PLT_ENTRY_SIZE;
2785 if (htab->sgotplt)
2787 /* Fill in the first three entries in the global offset table. */
2788 if (htab->sgotplt->size > 0)
2790 /* Set the first entry in the global offset table to the address of
2791 the dynamic section. */
2792 if (sdyn == NULL)
2793 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
2794 else
2795 bfd_put_64 (output_bfd,
2796 sdyn->output_section->vma + sdyn->output_offset,
2797 htab->sgotplt->contents);
2798 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
2799 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
2800 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
2803 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
2804 GOT_ENTRY_SIZE;
2807 if (htab->sgot && htab->sgot->size > 0)
2808 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
2809 = GOT_ENTRY_SIZE;
2811 return TRUE;
2814 /* Return address for Ith PLT stub in section PLT, for relocation REL
2815 or (bfd_vma) -1 if it should not be included. */
2817 static bfd_vma
2818 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
2819 const arelent *rel ATTRIBUTE_UNUSED)
2821 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
2824 /* Handle an x86-64 specific section when reading an object file. This
2825 is called when elfcode.h finds a section with an unknown type. */
2827 static bfd_boolean
2828 elf64_x86_64_section_from_shdr (bfd *abfd,
2829 Elf_Internal_Shdr *hdr,
2830 const char *name,
2831 int shindex)
2833 if (hdr->sh_type != SHT_X86_64_UNWIND)
2834 return FALSE;
2836 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2837 return FALSE;
2839 return TRUE;
2842 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
2843 #define TARGET_LITTLE_NAME "elf64-x86-64"
2844 #define ELF_ARCH bfd_arch_i386
2845 #define ELF_MACHINE_CODE EM_X86_64
2846 #define ELF_MAXPAGESIZE 0x100000
2848 #define elf_backend_can_gc_sections 1
2849 #define elf_backend_can_refcount 1
2850 #define elf_backend_want_got_plt 1
2851 #define elf_backend_plt_readonly 1
2852 #define elf_backend_want_plt_sym 0
2853 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
2854 #define elf_backend_rela_normal 1
2856 #define elf_info_to_howto elf64_x86_64_info_to_howto
2858 #define bfd_elf64_bfd_link_hash_table_create \
2859 elf64_x86_64_link_hash_table_create
2860 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
2862 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
2863 #define elf_backend_check_relocs elf64_x86_64_check_relocs
2864 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
2865 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
2866 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
2867 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
2868 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
2869 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
2870 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
2871 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
2872 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
2873 #define elf_backend_relocate_section elf64_x86_64_relocate_section
2874 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
2875 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
2876 #define elf_backend_object_p elf64_x86_64_elf_object_p
2877 #define bfd_elf64_mkobject elf64_x86_64_mkobject
2879 #define elf_backend_section_from_shdr \
2880 elf64_x86_64_section_from_shdr
2882 #include "elf64-target.h"