* elf.c (_bfd_elf_link_hash_copy_indirect): Copy
[binutils.git] / bfd / elf64-x86-64.c
blob5778f165fc058a04d69eb2248ce27aed4641732a
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "bfdlink.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
27 #include "elf/x86-64.h"
29 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
30 #define MINUS_ONE (~ (bfd_vma) 0)
32 /* The relocation "howto" table. Order of fields:
33 type, size, bitsize, pc_relative, complain_on_overflow,
34 special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */
35 static reloc_howto_type x86_64_elf_howto_table[] =
37 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
38 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
39 FALSE),
40 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
41 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
42 FALSE),
43 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
44 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
45 TRUE),
46 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
47 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
48 FALSE),
49 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
50 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
51 TRUE),
52 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
53 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
54 FALSE),
55 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
56 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
57 MINUS_ONE, FALSE),
58 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
59 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
60 MINUS_ONE, FALSE),
61 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
62 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
63 MINUS_ONE, FALSE),
64 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
65 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
66 0xffffffff, TRUE),
67 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
68 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
69 FALSE),
70 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
71 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
72 FALSE),
73 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
74 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
75 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
76 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
77 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_signed,
78 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
79 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
80 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
81 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
82 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
83 MINUS_ONE, FALSE),
84 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
85 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
86 MINUS_ONE, FALSE),
87 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
88 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
89 MINUS_ONE, FALSE),
90 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
91 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
92 0xffffffff, TRUE),
93 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
94 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
95 0xffffffff, TRUE),
96 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
97 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
98 0xffffffff, FALSE),
99 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
100 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
101 0xffffffff, TRUE),
102 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
103 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
104 0xffffffff, FALSE),
106 /* GNU extension to record C++ vtable hierarchy. */
107 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
108 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
110 /* GNU extension to record C++ vtable member usage. */
111 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
112 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
113 FALSE)
116 /* Map BFD relocs to the x86_64 elf relocs. */
117 struct elf_reloc_map
119 bfd_reloc_code_real_type bfd_reloc_val;
120 unsigned char elf_reloc_val;
123 static const struct elf_reloc_map x86_64_reloc_map[] =
125 { BFD_RELOC_NONE, R_X86_64_NONE, },
126 { BFD_RELOC_64, R_X86_64_64, },
127 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
128 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
129 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
130 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
131 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
132 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
133 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
134 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
135 { BFD_RELOC_32, R_X86_64_32, },
136 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
137 { BFD_RELOC_16, R_X86_64_16, },
138 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
139 { BFD_RELOC_8, R_X86_64_8, },
140 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
141 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
142 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
143 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
144 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
145 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
146 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
147 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
148 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
149 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
150 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
154 /* Given a BFD reloc type, return a HOWTO structure. */
155 static reloc_howto_type *
156 elf64_x86_64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
157 bfd_reloc_code_real_type code)
159 unsigned int i;
161 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
162 i++)
164 if (x86_64_reloc_map[i].bfd_reloc_val == code)
165 return &x86_64_elf_howto_table[i];
167 return 0;
170 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
172 static void
173 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
174 Elf_Internal_Rela *dst)
176 unsigned r_type, i;
178 r_type = ELF64_R_TYPE (dst->r_info);
179 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
181 BFD_ASSERT (r_type <= (unsigned int) R_X86_64_TPOFF32);
182 i = r_type;
184 else
186 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
187 i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_TPOFF32 - 1);
189 cache_ptr->howto = &x86_64_elf_howto_table[i];
190 BFD_ASSERT (r_type == cache_ptr->howto->type);
193 /* Support for core dump NOTE sections. */
194 static bfd_boolean
195 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
197 int offset;
198 size_t raw_size;
200 switch (note->descsz)
202 default:
203 return FALSE;
205 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
206 /* pr_cursig */
207 elf_tdata (abfd)->core_signal
208 = bfd_get_16 (abfd, note->descdata + 12);
210 /* pr_pid */
211 elf_tdata (abfd)->core_pid
212 = bfd_get_32 (abfd, note->descdata + 32);
214 /* pr_reg */
215 offset = 112;
216 raw_size = 216;
218 break;
221 /* Make a ".reg/999" section. */
222 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
223 raw_size, note->descpos + offset);
226 static bfd_boolean
227 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
229 switch (note->descsz)
231 default:
232 return FALSE;
234 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
235 elf_tdata (abfd)->core_program
236 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
237 elf_tdata (abfd)->core_command
238 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
241 /* Note that for some reason, a spurious space is tacked
242 onto the end of the args in some (at least one anyway)
243 implementations, so strip it off if it exists. */
246 char *command = elf_tdata (abfd)->core_command;
247 int n = strlen (command);
249 if (0 < n && command[n - 1] == ' ')
250 command[n - 1] = '\0';
253 return TRUE;
256 /* Functions for the x86-64 ELF linker. */
258 /* The name of the dynamic interpreter. This is put in the .interp
259 section. */
261 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
263 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
264 copying dynamic variables from a shared lib into an app's dynbss
265 section, and instead use a dynamic relocation to point into the
266 shared lib. */
267 #define ELIMINATE_COPY_RELOCS 1
269 /* The size in bytes of an entry in the global offset table. */
271 #define GOT_ENTRY_SIZE 8
273 /* The size in bytes of an entry in the procedure linkage table. */
275 #define PLT_ENTRY_SIZE 16
277 /* The first entry in a procedure linkage table looks like this. See the
278 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
280 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
282 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
283 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
284 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
287 /* Subsequent entries in a procedure linkage table look like this. */
289 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
291 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
292 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
293 0x68, /* pushq immediate */
294 0, 0, 0, 0, /* replaced with index into relocation table. */
295 0xe9, /* jmp relative */
296 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
299 /* The x86-64 linker needs to keep track of the number of relocs that
300 it decides to copy as dynamic relocs in check_relocs for each symbol.
301 This is so that it can later discard them if they are found to be
302 unnecessary. We store the information in a field extending the
303 regular ELF linker hash table. */
305 struct elf64_x86_64_dyn_relocs
307 /* Next section. */
308 struct elf64_x86_64_dyn_relocs *next;
310 /* The input section of the reloc. */
311 asection *sec;
313 /* Total number of relocs copied for the input section. */
314 bfd_size_type count;
316 /* Number of pc-relative relocs copied for the input section. */
317 bfd_size_type pc_count;
320 /* x86-64 ELF linker hash entry. */
322 struct elf64_x86_64_link_hash_entry
324 struct elf_link_hash_entry elf;
326 /* Track dynamic relocs copied for this symbol. */
327 struct elf64_x86_64_dyn_relocs *dyn_relocs;
329 #define GOT_UNKNOWN 0
330 #define GOT_NORMAL 1
331 #define GOT_TLS_GD 2
332 #define GOT_TLS_IE 3
333 unsigned char tls_type;
336 #define elf64_x86_64_hash_entry(ent) \
337 ((struct elf64_x86_64_link_hash_entry *)(ent))
339 struct elf64_x86_64_obj_tdata
341 struct elf_obj_tdata root;
343 /* tls_type for each local got entry. */
344 char *local_got_tls_type;
347 #define elf64_x86_64_tdata(abfd) \
348 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
350 #define elf64_x86_64_local_got_tls_type(abfd) \
351 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
354 /* x86-64 ELF linker hash table. */
356 struct elf64_x86_64_link_hash_table
358 struct elf_link_hash_table elf;
360 /* Short-cuts to get to dynamic linker sections. */
361 asection *sgot;
362 asection *sgotplt;
363 asection *srelgot;
364 asection *splt;
365 asection *srelplt;
366 asection *sdynbss;
367 asection *srelbss;
369 union {
370 bfd_signed_vma refcount;
371 bfd_vma offset;
372 } tls_ld_got;
374 /* Small local sym to section mapping cache. */
375 struct sym_sec_cache sym_sec;
378 /* Get the x86-64 ELF linker hash table from a link_info structure. */
380 #define elf64_x86_64_hash_table(p) \
381 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
383 /* Create an entry in an x86-64 ELF linker hash table. */
385 static struct bfd_hash_entry *
386 link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
387 const char *string)
389 /* Allocate the structure if it has not already been allocated by a
390 subclass. */
391 if (entry == NULL)
393 entry = bfd_hash_allocate (table,
394 sizeof (struct elf64_x86_64_link_hash_entry));
395 if (entry == NULL)
396 return entry;
399 /* Call the allocation method of the superclass. */
400 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
401 if (entry != NULL)
403 struct elf64_x86_64_link_hash_entry *eh;
405 eh = (struct elf64_x86_64_link_hash_entry *) entry;
406 eh->dyn_relocs = NULL;
407 eh->tls_type = GOT_UNKNOWN;
410 return entry;
413 /* Create an X86-64 ELF linker hash table. */
415 static struct bfd_link_hash_table *
416 elf64_x86_64_link_hash_table_create (bfd *abfd)
418 struct elf64_x86_64_link_hash_table *ret;
419 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
421 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
422 if (ret == NULL)
423 return NULL;
425 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
427 free (ret);
428 return NULL;
431 ret->sgot = NULL;
432 ret->sgotplt = NULL;
433 ret->srelgot = NULL;
434 ret->splt = NULL;
435 ret->srelplt = NULL;
436 ret->sdynbss = NULL;
437 ret->srelbss = NULL;
438 ret->sym_sec.abfd = NULL;
439 ret->tls_ld_got.refcount = 0;
441 return &ret->elf.root;
444 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
445 shortcuts to them in our hash table. */
447 static bfd_boolean
448 create_got_section (bfd *dynobj, struct bfd_link_info *info)
450 struct elf64_x86_64_link_hash_table *htab;
452 if (! _bfd_elf_create_got_section (dynobj, info))
453 return FALSE;
455 htab = elf64_x86_64_hash_table (info);
456 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
457 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
458 if (!htab->sgot || !htab->sgotplt)
459 abort ();
461 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
462 if (htab->srelgot == NULL
463 || ! bfd_set_section_flags (dynobj, htab->srelgot,
464 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
465 | SEC_IN_MEMORY | SEC_LINKER_CREATED
466 | SEC_READONLY))
467 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
468 return FALSE;
469 return TRUE;
472 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
473 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
474 hash table. */
476 static bfd_boolean
477 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
479 struct elf64_x86_64_link_hash_table *htab;
481 htab = elf64_x86_64_hash_table (info);
482 if (!htab->sgot && !create_got_section (dynobj, info))
483 return FALSE;
485 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
486 return FALSE;
488 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
489 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
490 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
491 if (!info->shared)
492 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
494 if (!htab->splt || !htab->srelplt || !htab->sdynbss
495 || (!info->shared && !htab->srelbss))
496 abort ();
498 return TRUE;
501 /* Copy the extra info we tack onto an elf_link_hash_entry. */
503 static void
504 elf64_x86_64_copy_indirect_symbol (const struct elf_backend_data *bed,
505 struct elf_link_hash_entry *dir,
506 struct elf_link_hash_entry *ind)
508 struct elf64_x86_64_link_hash_entry *edir, *eind;
510 edir = (struct elf64_x86_64_link_hash_entry *) dir;
511 eind = (struct elf64_x86_64_link_hash_entry *) ind;
513 if (eind->dyn_relocs != NULL)
515 if (edir->dyn_relocs != NULL)
517 struct elf64_x86_64_dyn_relocs **pp;
518 struct elf64_x86_64_dyn_relocs *p;
520 if (ind->root.type == bfd_link_hash_indirect)
521 abort ();
523 /* Add reloc counts against the weak sym to the strong sym
524 list. Merge any entries against the same section. */
525 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
527 struct elf64_x86_64_dyn_relocs *q;
529 for (q = edir->dyn_relocs; q != NULL; q = q->next)
530 if (q->sec == p->sec)
532 q->pc_count += p->pc_count;
533 q->count += p->count;
534 *pp = p->next;
535 break;
537 if (q == NULL)
538 pp = &p->next;
540 *pp = edir->dyn_relocs;
543 edir->dyn_relocs = eind->dyn_relocs;
544 eind->dyn_relocs = NULL;
547 if (ind->root.type == bfd_link_hash_indirect
548 && dir->got.refcount <= 0)
550 edir->tls_type = eind->tls_type;
551 eind->tls_type = GOT_UNKNOWN;
554 if (ELIMINATE_COPY_RELOCS
555 && ind->root.type != bfd_link_hash_indirect
556 && (dir->elf_link_hash_flags & ELF_LINK_HASH_DYNAMIC_ADJUSTED) != 0)
557 /* If called to transfer flags for a weakdef during processing
558 of elf_adjust_dynamic_symbol, don't copy ELF_LINK_NON_GOT_REF.
559 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
560 dir->elf_link_hash_flags |=
561 (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
562 | ELF_LINK_HASH_REF_REGULAR
563 | ELF_LINK_HASH_REF_REGULAR_NONWEAK
564 | ELF_LINK_HASH_NEEDS_PLT));
565 else
566 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
569 static bfd_boolean
570 elf64_x86_64_mkobject (bfd *abfd)
572 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
573 abfd->tdata.any = bfd_zalloc (abfd, amt);
574 if (abfd->tdata.any == NULL)
575 return FALSE;
576 return TRUE;
579 static bfd_boolean
580 elf64_x86_64_elf_object_p (bfd *abfd)
582 /* Allocate our special target data. */
583 struct elf64_x86_64_obj_tdata *new_tdata;
584 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
585 new_tdata = bfd_zalloc (abfd, amt);
586 if (new_tdata == NULL)
587 return FALSE;
588 new_tdata->root = *abfd->tdata.elf_obj_data;
589 abfd->tdata.any = new_tdata;
590 /* Set the right machine number for an x86-64 elf64 file. */
591 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
592 return TRUE;
595 static int
596 elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
598 if (info->shared)
599 return r_type;
601 switch (r_type)
603 case R_X86_64_TLSGD:
604 case R_X86_64_GOTTPOFF:
605 if (is_local)
606 return R_X86_64_TPOFF32;
607 return R_X86_64_GOTTPOFF;
608 case R_X86_64_TLSLD:
609 return R_X86_64_TPOFF32;
612 return r_type;
615 /* Look through the relocs for a section during the first phase, and
616 calculate needed space in the global offset table, procedure
617 linkage table, and dynamic reloc sections. */
619 static bfd_boolean
620 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
621 const Elf_Internal_Rela *relocs)
623 struct elf64_x86_64_link_hash_table *htab;
624 Elf_Internal_Shdr *symtab_hdr;
625 struct elf_link_hash_entry **sym_hashes;
626 const Elf_Internal_Rela *rel;
627 const Elf_Internal_Rela *rel_end;
628 asection *sreloc;
630 if (info->relocatable)
631 return TRUE;
633 htab = elf64_x86_64_hash_table (info);
634 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
635 sym_hashes = elf_sym_hashes (abfd);
637 sreloc = NULL;
639 rel_end = relocs + sec->reloc_count;
640 for (rel = relocs; rel < rel_end; rel++)
642 unsigned int r_type;
643 unsigned long r_symndx;
644 struct elf_link_hash_entry *h;
646 r_symndx = ELF64_R_SYM (rel->r_info);
647 r_type = ELF64_R_TYPE (rel->r_info);
649 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
651 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
652 bfd_archive_filename (abfd),
653 r_symndx);
654 return FALSE;
657 if (r_symndx < symtab_hdr->sh_info)
658 h = NULL;
659 else
660 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
662 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
663 switch (r_type)
665 case R_X86_64_TLSLD:
666 htab->tls_ld_got.refcount += 1;
667 goto create_got;
669 case R_X86_64_TPOFF32:
670 if (info->shared)
672 (*_bfd_error_handler)
673 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
674 bfd_archive_filename (abfd),
675 x86_64_elf_howto_table[r_type].name);
676 bfd_set_error (bfd_error_bad_value);
677 return FALSE;
679 break;
681 case R_X86_64_GOTTPOFF:
682 if (info->shared)
683 info->flags |= DF_STATIC_TLS;
684 /* Fall through */
686 case R_X86_64_GOT32:
687 case R_X86_64_GOTPCREL:
688 case R_X86_64_TLSGD:
689 /* This symbol requires a global offset table entry. */
691 int tls_type, old_tls_type;
693 switch (r_type)
695 default: tls_type = GOT_NORMAL; break;
696 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
697 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
700 if (h != NULL)
702 h->got.refcount += 1;
703 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
705 else
707 bfd_signed_vma *local_got_refcounts;
709 /* This is a global offset table entry for a local symbol. */
710 local_got_refcounts = elf_local_got_refcounts (abfd);
711 if (local_got_refcounts == NULL)
713 bfd_size_type size;
715 size = symtab_hdr->sh_info;
716 size *= sizeof (bfd_signed_vma) + sizeof (char);
717 local_got_refcounts = ((bfd_signed_vma *)
718 bfd_zalloc (abfd, size));
719 if (local_got_refcounts == NULL)
720 return FALSE;
721 elf_local_got_refcounts (abfd) = local_got_refcounts;
722 elf64_x86_64_local_got_tls_type (abfd)
723 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
725 local_got_refcounts[r_symndx] += 1;
726 old_tls_type
727 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
730 /* If a TLS symbol is accessed using IE at least once,
731 there is no point to use dynamic model for it. */
732 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
733 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
735 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
736 tls_type = old_tls_type;
737 else
739 (*_bfd_error_handler)
740 (_("%s: %s' accessed both as normal and thread local symbol"),
741 bfd_archive_filename (abfd),
742 h ? h->root.root.string : "<local>");
743 return FALSE;
747 if (old_tls_type != tls_type)
749 if (h != NULL)
750 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
751 else
752 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
755 /* Fall through */
757 //case R_X86_64_GOTPCREL:
758 create_got:
759 if (htab->sgot == NULL)
761 if (htab->elf.dynobj == NULL)
762 htab->elf.dynobj = abfd;
763 if (!create_got_section (htab->elf.dynobj, info))
764 return FALSE;
766 break;
768 case R_X86_64_PLT32:
769 /* This symbol requires a procedure linkage table entry. We
770 actually build the entry in adjust_dynamic_symbol,
771 because this might be a case of linking PIC code which is
772 never referenced by a dynamic object, in which case we
773 don't need to generate a procedure linkage table entry
774 after all. */
776 /* If this is a local symbol, we resolve it directly without
777 creating a procedure linkage table entry. */
778 if (h == NULL)
779 continue;
781 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
782 h->plt.refcount += 1;
783 break;
785 case R_X86_64_8:
786 case R_X86_64_16:
787 case R_X86_64_32:
788 case R_X86_64_32S:
789 /* Let's help debug shared library creation. These relocs
790 cannot be used in shared libs. Don't error out for
791 sections we don't care about, such as debug sections or
792 non-constant sections. */
793 if (info->shared
794 && (sec->flags & SEC_ALLOC) != 0
795 && (sec->flags & SEC_READONLY) != 0)
797 (*_bfd_error_handler)
798 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
799 bfd_archive_filename (abfd),
800 x86_64_elf_howto_table[r_type].name);
801 bfd_set_error (bfd_error_bad_value);
802 return FALSE;
804 /* Fall through. */
806 case R_X86_64_PC8:
807 case R_X86_64_PC16:
808 case R_X86_64_PC32:
809 case R_X86_64_64:
810 if (h != NULL && !info->shared)
812 /* If this reloc is in a read-only section, we might
813 need a copy reloc. We can't check reliably at this
814 stage whether the section is read-only, as input
815 sections have not yet been mapped to output sections.
816 Tentatively set the flag for now, and correct in
817 adjust_dynamic_symbol. */
818 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
820 /* We may need a .plt entry if the function this reloc
821 refers to is in a shared lib. */
822 h->plt.refcount += 1;
825 /* If we are creating a shared library, and this is a reloc
826 against a global symbol, or a non PC relative reloc
827 against a local symbol, then we need to copy the reloc
828 into the shared library. However, if we are linking with
829 -Bsymbolic, we do not need to copy a reloc against a
830 global symbol which is defined in an object we are
831 including in the link (i.e., DEF_REGULAR is set). At
832 this point we have not seen all the input files, so it is
833 possible that DEF_REGULAR is not set now but will be set
834 later (it is never cleared). In case of a weak definition,
835 DEF_REGULAR may be cleared later by a strong definition in
836 a shared library. We account for that possibility below by
837 storing information in the relocs_copied field of the hash
838 table entry. A similar situation occurs when creating
839 shared libraries and symbol visibility changes render the
840 symbol local.
842 If on the other hand, we are creating an executable, we
843 may need to keep relocations for symbols satisfied by a
844 dynamic library if we manage to avoid copy relocs for the
845 symbol. */
846 if ((info->shared
847 && (sec->flags & SEC_ALLOC) != 0
848 && (((r_type != R_X86_64_PC8)
849 && (r_type != R_X86_64_PC16)
850 && (r_type != R_X86_64_PC32))
851 || (h != NULL
852 && (! info->symbolic
853 || h->root.type == bfd_link_hash_defweak
854 || (h->elf_link_hash_flags
855 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
856 || (ELIMINATE_COPY_RELOCS
857 && !info->shared
858 && (sec->flags & SEC_ALLOC) != 0
859 && h != NULL
860 && (h->root.type == bfd_link_hash_defweak
861 || (h->elf_link_hash_flags
862 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
864 struct elf64_x86_64_dyn_relocs *p;
865 struct elf64_x86_64_dyn_relocs **head;
867 /* We must copy these reloc types into the output file.
868 Create a reloc section in dynobj and make room for
869 this reloc. */
870 if (sreloc == NULL)
872 const char *name;
873 bfd *dynobj;
875 name = (bfd_elf_string_from_elf_section
876 (abfd,
877 elf_elfheader (abfd)->e_shstrndx,
878 elf_section_data (sec)->rel_hdr.sh_name));
879 if (name == NULL)
880 return FALSE;
882 if (strncmp (name, ".rela", 5) != 0
883 || strcmp (bfd_get_section_name (abfd, sec),
884 name + 5) != 0)
886 (*_bfd_error_handler)
887 (_("%s: bad relocation section name `%s\'"),
888 bfd_archive_filename (abfd), name);
891 if (htab->elf.dynobj == NULL)
892 htab->elf.dynobj = abfd;
894 dynobj = htab->elf.dynobj;
896 sreloc = bfd_get_section_by_name (dynobj, name);
897 if (sreloc == NULL)
899 flagword flags;
901 sreloc = bfd_make_section (dynobj, name);
902 flags = (SEC_HAS_CONTENTS | SEC_READONLY
903 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
904 if ((sec->flags & SEC_ALLOC) != 0)
905 flags |= SEC_ALLOC | SEC_LOAD;
906 if (sreloc == NULL
907 || ! bfd_set_section_flags (dynobj, sreloc, flags)
908 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
909 return FALSE;
911 elf_section_data (sec)->sreloc = sreloc;
914 /* If this is a global symbol, we count the number of
915 relocations we need for this symbol. */
916 if (h != NULL)
918 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
920 else
922 /* Track dynamic relocs needed for local syms too.
923 We really need local syms available to do this
924 easily. Oh well. */
926 asection *s;
927 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
928 sec, r_symndx);
929 if (s == NULL)
930 return FALSE;
932 head = ((struct elf64_x86_64_dyn_relocs **)
933 &elf_section_data (s)->local_dynrel);
936 p = *head;
937 if (p == NULL || p->sec != sec)
939 bfd_size_type amt = sizeof *p;
940 p = ((struct elf64_x86_64_dyn_relocs *)
941 bfd_alloc (htab->elf.dynobj, amt));
942 if (p == NULL)
943 return FALSE;
944 p->next = *head;
945 *head = p;
946 p->sec = sec;
947 p->count = 0;
948 p->pc_count = 0;
951 p->count += 1;
952 if (r_type == R_X86_64_PC8
953 || r_type == R_X86_64_PC16
954 || r_type == R_X86_64_PC32)
955 p->pc_count += 1;
957 break;
959 /* This relocation describes the C++ object vtable hierarchy.
960 Reconstruct it for later use during GC. */
961 case R_X86_64_GNU_VTINHERIT:
962 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
963 return FALSE;
964 break;
966 /* This relocation describes which C++ vtable entries are actually
967 used. Record for later use during GC. */
968 case R_X86_64_GNU_VTENTRY:
969 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
970 return FALSE;
971 break;
973 default:
974 break;
978 return TRUE;
981 /* Return the section that should be marked against GC for a given
982 relocation. */
984 static asection *
985 elf64_x86_64_gc_mark_hook (asection *sec,
986 struct bfd_link_info *info ATTRIBUTE_UNUSED,
987 Elf_Internal_Rela *rel,
988 struct elf_link_hash_entry *h,
989 Elf_Internal_Sym *sym)
991 if (h != NULL)
993 switch (ELF64_R_TYPE (rel->r_info))
995 case R_X86_64_GNU_VTINHERIT:
996 case R_X86_64_GNU_VTENTRY:
997 break;
999 default:
1000 switch (h->root.type)
1002 case bfd_link_hash_defined:
1003 case bfd_link_hash_defweak:
1004 return h->root.u.def.section;
1006 case bfd_link_hash_common:
1007 return h->root.u.c.p->section;
1009 default:
1010 break;
1014 else
1015 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
1017 return NULL;
1020 /* Update the got entry reference counts for the section being removed. */
1022 static bfd_boolean
1023 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
1024 asection *sec, const Elf_Internal_Rela *relocs)
1026 Elf_Internal_Shdr *symtab_hdr;
1027 struct elf_link_hash_entry **sym_hashes;
1028 bfd_signed_vma *local_got_refcounts;
1029 const Elf_Internal_Rela *rel, *relend;
1031 elf_section_data (sec)->local_dynrel = NULL;
1033 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1034 sym_hashes = elf_sym_hashes (abfd);
1035 local_got_refcounts = elf_local_got_refcounts (abfd);
1037 relend = relocs + sec->reloc_count;
1038 for (rel = relocs; rel < relend; rel++)
1040 unsigned long r_symndx;
1041 unsigned int r_type;
1042 struct elf_link_hash_entry *h = NULL;
1044 r_symndx = ELF64_R_SYM (rel->r_info);
1045 if (r_symndx >= symtab_hdr->sh_info)
1047 struct elf64_x86_64_link_hash_entry *eh;
1048 struct elf64_x86_64_dyn_relocs **pp;
1049 struct elf64_x86_64_dyn_relocs *p;
1051 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
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->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
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->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
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->weakdef != NULL)
1165 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1166 || h->weakdef->root.type == bfd_link_hash_defweak);
1167 h->root.u.def.section = h->weakdef->root.u.def.section;
1168 h->root.u.def.value = h->weakdef->root.u.def.value;
1169 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
1170 h->elf_link_hash_flags
1171 = ((h->elf_link_hash_flags & ~ELF_LINK_NON_GOT_REF)
1172 | (h->weakdef->elf_link_hash_flags & ELF_LINK_NON_GOT_REF));
1173 return TRUE;
1176 /* This is a reference to a symbol defined by a dynamic object which
1177 is not a function. */
1179 /* If we are creating a shared library, we must presume that the
1180 only references to the symbol are via the global offset table.
1181 For such cases we need not do anything here; the relocations will
1182 be handled correctly by relocate_section. */
1183 if (info->shared)
1184 return TRUE;
1186 /* If there are no references to this symbol that do not use the
1187 GOT, we don't need to generate a copy reloc. */
1188 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1189 return TRUE;
1191 /* If -z nocopyreloc was given, we won't generate them either. */
1192 if (info->nocopyreloc)
1194 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1195 return TRUE;
1198 if (ELIMINATE_COPY_RELOCS)
1200 struct elf64_x86_64_link_hash_entry * eh;
1201 struct elf64_x86_64_dyn_relocs *p;
1203 eh = (struct elf64_x86_64_link_hash_entry *) h;
1204 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1206 s = p->sec->output_section;
1207 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1208 break;
1211 /* If we didn't find any dynamic relocs in read-only sections, then
1212 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1213 if (p == NULL)
1215 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1216 return TRUE;
1220 /* We must allocate the symbol in our .dynbss section, which will
1221 become part of the .bss section of the executable. There will be
1222 an entry for this symbol in the .dynsym section. The dynamic
1223 object will contain position independent code, so all references
1224 from the dynamic object to this symbol will go through the global
1225 offset table. The dynamic linker will use the .dynsym entry to
1226 determine the address it must put in the global offset table, so
1227 both the dynamic object and the regular object will refer to the
1228 same memory location for the variable. */
1230 htab = elf64_x86_64_hash_table (info);
1232 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1233 to copy the initial value out of the dynamic object and into the
1234 runtime process image. */
1235 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1237 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
1238 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1241 /* We need to figure out the alignment required for this symbol. I
1242 have no idea how ELF linkers handle this. 16-bytes is the size
1243 of the largest type that requires hard alignment -- long double. */
1244 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1245 this construct. */
1246 power_of_two = bfd_log2 (h->size);
1247 if (power_of_two > 4)
1248 power_of_two = 4;
1250 /* Apply the required alignment. */
1251 s = htab->sdynbss;
1252 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
1253 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1255 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1256 return FALSE;
1259 /* Define the symbol as being at this point in the section. */
1260 h->root.u.def.section = s;
1261 h->root.u.def.value = s->_raw_size;
1263 /* Increment the section size to make room for the symbol. */
1264 s->_raw_size += h->size;
1266 return TRUE;
1269 /* This is the condition under which elf64_x86_64_finish_dynamic_symbol
1270 will be called from elflink.h. If elflink.h doesn't call our
1271 finish_dynamic_symbol routine, we'll need to do something about
1272 initializing any .plt and .got entries in elf64_x86_64_relocate_section. */
1273 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
1274 ((DYN) \
1275 && ((SHARED) \
1276 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1277 && ((H)->dynindx != -1 \
1278 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1280 /* Allocate space in .plt, .got and associated reloc sections for
1281 dynamic relocs. */
1283 static bfd_boolean
1284 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1286 struct bfd_link_info *info;
1287 struct elf64_x86_64_link_hash_table *htab;
1288 struct elf64_x86_64_link_hash_entry *eh;
1289 struct elf64_x86_64_dyn_relocs *p;
1291 if (h->root.type == bfd_link_hash_indirect)
1292 return TRUE;
1294 if (h->root.type == bfd_link_hash_warning)
1295 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1297 info = (struct bfd_link_info *) inf;
1298 htab = elf64_x86_64_hash_table (info);
1300 if (htab->elf.dynamic_sections_created
1301 && h->plt.refcount > 0)
1303 /* Make sure this symbol is output as a dynamic symbol.
1304 Undefined weak syms won't yet be marked as dynamic. */
1305 if (h->dynindx == -1
1306 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1308 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1309 return FALSE;
1312 if (info->shared
1313 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
1315 asection *s = htab->splt;
1317 /* If this is the first .plt entry, make room for the special
1318 first entry. */
1319 if (s->_raw_size == 0)
1320 s->_raw_size += PLT_ENTRY_SIZE;
1322 h->plt.offset = s->_raw_size;
1324 /* If this symbol is not defined in a regular file, and we are
1325 not generating a shared library, then set the symbol to this
1326 location in the .plt. This is required to make function
1327 pointers compare as equal between the normal executable and
1328 the shared library. */
1329 if (! info->shared
1330 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1332 h->root.u.def.section = s;
1333 h->root.u.def.value = h->plt.offset;
1336 /* Make room for this entry. */
1337 s->_raw_size += PLT_ENTRY_SIZE;
1339 /* We also need to make an entry in the .got.plt section, which
1340 will be placed in the .got section by the linker script. */
1341 htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
1343 /* We also need to make an entry in the .rela.plt section. */
1344 htab->srelplt->_raw_size += sizeof (Elf64_External_Rela);
1346 else
1348 h->plt.offset = (bfd_vma) -1;
1349 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1352 else
1354 h->plt.offset = (bfd_vma) -1;
1355 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1358 /* If R_X86_64_GOTTPOFF symbol is now local to the binary,
1359 make it a R_X86_64_TPOFF32 requiring no GOT entry. */
1360 if (h->got.refcount > 0
1361 && !info->shared
1362 && h->dynindx == -1
1363 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
1364 h->got.offset = (bfd_vma) -1;
1365 else if (h->got.refcount > 0)
1367 asection *s;
1368 bfd_boolean dyn;
1369 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
1371 /* Make sure this symbol is output as a dynamic symbol.
1372 Undefined weak syms won't yet be marked as dynamic. */
1373 if (h->dynindx == -1
1374 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1376 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1377 return FALSE;
1380 s = htab->sgot;
1381 h->got.offset = s->_raw_size;
1382 s->_raw_size += GOT_ENTRY_SIZE;
1383 /* R_X86_64_TLSGD needs 2 consecutive GOT slots. */
1384 if (tls_type == GOT_TLS_GD)
1385 s->_raw_size += GOT_ENTRY_SIZE;
1386 dyn = htab->elf.dynamic_sections_created;
1387 /* R_X86_64_TLSGD needs one dynamic relocation if local symbol
1388 and two if global.
1389 R_X86_64_GOTTPOFF needs one dynamic relocation. */
1390 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
1391 || tls_type == GOT_TLS_IE)
1392 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
1393 else if (tls_type == GOT_TLS_GD)
1394 htab->srelgot->_raw_size += 2 * sizeof (Elf64_External_Rela);
1395 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1396 || h->root.type != bfd_link_hash_undefweak)
1397 && (info->shared
1398 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
1399 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
1401 else
1402 h->got.offset = (bfd_vma) -1;
1404 eh = (struct elf64_x86_64_link_hash_entry *) h;
1405 if (eh->dyn_relocs == NULL)
1406 return TRUE;
1408 /* In the shared -Bsymbolic case, discard space allocated for
1409 dynamic pc-relative relocs against symbols which turn out to be
1410 defined in regular objects. For the normal shared case, discard
1411 space for pc-relative relocs that have become local due to symbol
1412 visibility changes. */
1414 if (info->shared)
1416 /* Relocs that use pc_count are those that appear on a call
1417 insn, or certain REL relocs that can generated via assembly.
1418 We want calls to protected symbols to resolve directly to the
1419 function rather than going via the plt. If people want
1420 function pointer comparisons to work as expected then they
1421 should avoid writing weird assembly. */
1422 if (SYMBOL_CALLS_LOCAL (info, h))
1424 struct elf64_x86_64_dyn_relocs **pp;
1426 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1428 p->count -= p->pc_count;
1429 p->pc_count = 0;
1430 if (p->count == 0)
1431 *pp = p->next;
1432 else
1433 pp = &p->next;
1437 /* Also discard relocs on undefined weak syms with non-default
1438 visibility. */
1439 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1440 && h->root.type == bfd_link_hash_undefweak)
1441 eh->dyn_relocs = NULL;
1443 else if (ELIMINATE_COPY_RELOCS)
1445 /* For the non-shared case, discard space for relocs against
1446 symbols which turn out to need copy relocs or are not
1447 dynamic. */
1449 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1450 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1451 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1452 || (htab->elf.dynamic_sections_created
1453 && (h->root.type == bfd_link_hash_undefweak
1454 || h->root.type == bfd_link_hash_undefined))))
1456 /* Make sure this symbol is output as a dynamic symbol.
1457 Undefined weak syms won't yet be marked as dynamic. */
1458 if (h->dynindx == -1
1459 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1461 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1462 return FALSE;
1465 /* If that succeeded, we know we'll be keeping all the
1466 relocs. */
1467 if (h->dynindx != -1)
1468 goto keep;
1471 eh->dyn_relocs = NULL;
1473 keep: ;
1476 /* Finally, allocate space. */
1477 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1479 asection *sreloc = elf_section_data (p->sec)->sreloc;
1480 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
1483 return TRUE;
1486 /* Find any dynamic relocs that apply to read-only sections. */
1488 static bfd_boolean
1489 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
1491 struct elf64_x86_64_link_hash_entry *eh;
1492 struct elf64_x86_64_dyn_relocs *p;
1494 if (h->root.type == bfd_link_hash_warning)
1495 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1497 eh = (struct elf64_x86_64_link_hash_entry *) h;
1498 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1500 asection *s = p->sec->output_section;
1502 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1504 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1506 info->flags |= DF_TEXTREL;
1508 /* Not an error, just cut short the traversal. */
1509 return FALSE;
1512 return TRUE;
1515 /* Set the sizes of the dynamic sections. */
1517 static bfd_boolean
1518 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
1519 struct bfd_link_info *info)
1521 struct elf64_x86_64_link_hash_table *htab;
1522 bfd *dynobj;
1523 asection *s;
1524 bfd_boolean relocs;
1525 bfd *ibfd;
1527 htab = elf64_x86_64_hash_table (info);
1528 dynobj = htab->elf.dynobj;
1529 if (dynobj == NULL)
1530 abort ();
1532 if (htab->elf.dynamic_sections_created)
1534 /* Set the contents of the .interp section to the interpreter. */
1535 if (info->executable)
1537 s = bfd_get_section_by_name (dynobj, ".interp");
1538 if (s == NULL)
1539 abort ();
1540 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1541 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1545 /* Set up .got offsets for local syms, and space for local dynamic
1546 relocs. */
1547 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1549 bfd_signed_vma *local_got;
1550 bfd_signed_vma *end_local_got;
1551 char *local_tls_type;
1552 bfd_size_type locsymcount;
1553 Elf_Internal_Shdr *symtab_hdr;
1554 asection *srel;
1556 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1557 continue;
1559 for (s = ibfd->sections; s != NULL; s = s->next)
1561 struct elf64_x86_64_dyn_relocs *p;
1563 for (p = *((struct elf64_x86_64_dyn_relocs **)
1564 &elf_section_data (s)->local_dynrel);
1565 p != NULL;
1566 p = p->next)
1568 if (!bfd_is_abs_section (p->sec)
1569 && bfd_is_abs_section (p->sec->output_section))
1571 /* Input section has been discarded, either because
1572 it is a copy of a linkonce section or due to
1573 linker script /DISCARD/, so we'll be discarding
1574 the relocs too. */
1576 else if (p->count != 0)
1578 srel = elf_section_data (p->sec)->sreloc;
1579 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
1580 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1581 info->flags |= DF_TEXTREL;
1587 local_got = elf_local_got_refcounts (ibfd);
1588 if (!local_got)
1589 continue;
1591 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1592 locsymcount = symtab_hdr->sh_info;
1593 end_local_got = local_got + locsymcount;
1594 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
1595 s = htab->sgot;
1596 srel = htab->srelgot;
1597 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
1599 if (*local_got > 0)
1601 *local_got = s->_raw_size;
1602 s->_raw_size += GOT_ENTRY_SIZE;
1603 if (*local_tls_type == GOT_TLS_GD)
1604 s->_raw_size += GOT_ENTRY_SIZE;
1605 if (info->shared
1606 || *local_tls_type == GOT_TLS_GD
1607 || *local_tls_type == GOT_TLS_IE)
1608 srel->_raw_size += sizeof (Elf64_External_Rela);
1610 else
1611 *local_got = (bfd_vma) -1;
1615 if (htab->tls_ld_got.refcount > 0)
1617 /* Allocate 2 got entries and 1 dynamic reloc for R_X86_64_TLSLD
1618 relocs. */
1619 htab->tls_ld_got.offset = htab->sgot->_raw_size;
1620 htab->sgot->_raw_size += 2 * GOT_ENTRY_SIZE;
1621 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
1623 else
1624 htab->tls_ld_got.offset = -1;
1626 /* Allocate global sym .plt and .got entries, and space for global
1627 sym dynamic relocs. */
1628 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1630 /* We now have determined the sizes of the various dynamic sections.
1631 Allocate memory for them. */
1632 relocs = FALSE;
1633 for (s = dynobj->sections; s != NULL; s = s->next)
1635 if ((s->flags & SEC_LINKER_CREATED) == 0)
1636 continue;
1638 if (s == htab->splt
1639 || s == htab->sgot
1640 || s == htab->sgotplt)
1642 /* Strip this section if we don't need it; see the
1643 comment below. */
1645 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1647 if (s->_raw_size != 0 && s != htab->srelplt)
1648 relocs = TRUE;
1650 /* We use the reloc_count field as a counter if we need
1651 to copy relocs into the output file. */
1652 s->reloc_count = 0;
1654 else
1656 /* It's not one of our sections, so don't allocate space. */
1657 continue;
1660 if (s->_raw_size == 0)
1662 /* If we don't need this section, strip it from the
1663 output file. This is mostly to handle .rela.bss and
1664 .rela.plt. We must create both sections in
1665 create_dynamic_sections, because they must be created
1666 before the linker maps input sections to output
1667 sections. The linker does that before
1668 adjust_dynamic_symbol is called, and it is that
1669 function which decides whether anything needs to go
1670 into these sections. */
1672 _bfd_strip_section_from_output (info, s);
1673 continue;
1676 /* Allocate memory for the section contents. We use bfd_zalloc
1677 here in case unused entries are not reclaimed before the
1678 section's contents are written out. This should not happen,
1679 but this way if it does, we get a R_X86_64_NONE reloc instead
1680 of garbage. */
1681 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1682 if (s->contents == NULL)
1683 return FALSE;
1686 if (htab->elf.dynamic_sections_created)
1688 /* Add some entries to the .dynamic section. We fill in the
1689 values later, in elf64_x86_64_finish_dynamic_sections, but we
1690 must add the entries now so that we get the correct size for
1691 the .dynamic section. The DT_DEBUG entry is filled in by the
1692 dynamic linker and used by the debugger. */
1693 #define add_dynamic_entry(TAG, VAL) \
1694 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1696 if (info->executable)
1698 if (!add_dynamic_entry (DT_DEBUG, 0))
1699 return FALSE;
1702 if (htab->splt->_raw_size != 0)
1704 if (!add_dynamic_entry (DT_PLTGOT, 0)
1705 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1706 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1707 || !add_dynamic_entry (DT_JMPREL, 0))
1708 return FALSE;
1711 if (relocs)
1713 if (!add_dynamic_entry (DT_RELA, 0)
1714 || !add_dynamic_entry (DT_RELASZ, 0)
1715 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1716 return FALSE;
1718 /* If any dynamic relocs apply to a read-only section,
1719 then we need a DT_TEXTREL entry. */
1720 if ((info->flags & DF_TEXTREL) == 0)
1721 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1722 (PTR) info);
1724 if ((info->flags & DF_TEXTREL) != 0)
1726 if (!add_dynamic_entry (DT_TEXTREL, 0))
1727 return FALSE;
1731 #undef add_dynamic_entry
1733 return TRUE;
1736 /* Return the base VMA address which should be subtracted from real addresses
1737 when resolving @dtpoff relocation.
1738 This is PT_TLS segment p_vaddr. */
1740 static bfd_vma
1741 dtpoff_base (struct bfd_link_info *info)
1743 /* If tls_sec is NULL, we should have signalled an error already. */
1744 if (elf_hash_table (info)->tls_sec == NULL)
1745 return 0;
1746 return elf_hash_table (info)->tls_sec->vma;
1749 /* Return the relocation value for @tpoff relocation
1750 if STT_TLS virtual address is ADDRESS. */
1752 static bfd_vma
1753 tpoff (struct bfd_link_info *info, bfd_vma address)
1755 struct elf_link_hash_table *htab = elf_hash_table (info);
1757 /* If tls_segment is NULL, we should have signalled an error already. */
1758 if (htab->tls_sec == NULL)
1759 return 0;
1760 return address - htab->tls_size - htab->tls_sec->vma;
1763 /* Relocate an x86_64 ELF section. */
1765 static bfd_boolean
1766 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
1767 bfd *input_bfd, asection *input_section,
1768 bfd_byte *contents, Elf_Internal_Rela *relocs,
1769 Elf_Internal_Sym *local_syms,
1770 asection **local_sections)
1772 struct elf64_x86_64_link_hash_table *htab;
1773 Elf_Internal_Shdr *symtab_hdr;
1774 struct elf_link_hash_entry **sym_hashes;
1775 bfd_vma *local_got_offsets;
1776 Elf_Internal_Rela *rel;
1777 Elf_Internal_Rela *relend;
1779 if (info->relocatable)
1780 return TRUE;
1782 htab = elf64_x86_64_hash_table (info);
1783 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1784 sym_hashes = elf_sym_hashes (input_bfd);
1785 local_got_offsets = elf_local_got_offsets (input_bfd);
1787 rel = relocs;
1788 relend = relocs + input_section->reloc_count;
1789 for (; rel < relend; rel++)
1791 unsigned int r_type;
1792 reloc_howto_type *howto;
1793 unsigned long r_symndx;
1794 struct elf_link_hash_entry *h;
1795 Elf_Internal_Sym *sym;
1796 asection *sec;
1797 bfd_vma off;
1798 bfd_vma relocation;
1799 bfd_boolean unresolved_reloc;
1800 bfd_reloc_status_type r;
1801 int tls_type;
1803 r_type = ELF64_R_TYPE (rel->r_info);
1804 if (r_type == (int) R_X86_64_GNU_VTINHERIT
1805 || r_type == (int) R_X86_64_GNU_VTENTRY)
1806 continue;
1808 if (r_type >= R_X86_64_max)
1810 bfd_set_error (bfd_error_bad_value);
1811 return FALSE;
1814 howto = x86_64_elf_howto_table + r_type;
1815 r_symndx = ELF64_R_SYM (rel->r_info);
1816 h = NULL;
1817 sym = NULL;
1818 sec = NULL;
1819 unresolved_reloc = FALSE;
1820 if (r_symndx < symtab_hdr->sh_info)
1822 sym = local_syms + r_symndx;
1823 sec = local_sections[r_symndx];
1825 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1827 else
1829 bfd_boolean warned;
1831 RELOC_FOR_GLOBAL_SYMBOL (h, sym_hashes, r_symndx,
1832 symtab_hdr, relocation, sec,
1833 unresolved_reloc, info,
1834 warned);
1836 /* When generating a shared object, the relocations handled here are
1837 copied into the output file to be resolved at run time. */
1838 switch (r_type)
1840 case R_X86_64_GOT32:
1841 /* Relocation is to the entry for this symbol in the global
1842 offset table. */
1843 case R_X86_64_GOTPCREL:
1844 /* Use global offset table as symbol value. */
1845 if (htab->sgot == NULL)
1846 abort ();
1848 if (h != NULL)
1850 bfd_boolean dyn;
1852 off = h->got.offset;
1853 dyn = htab->elf.dynamic_sections_created;
1855 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
1856 || (info->shared
1857 && SYMBOL_REFERENCES_LOCAL (info, h))
1858 || (ELF_ST_VISIBILITY (h->other)
1859 && h->root.type == bfd_link_hash_undefweak))
1861 /* This is actually a static link, or it is a -Bsymbolic
1862 link and the symbol is defined locally, or the symbol
1863 was forced to be local because of a version file. We
1864 must initialize this entry in the global offset table.
1865 Since the offset must always be a multiple of 8, we
1866 use the least significant bit to record whether we
1867 have initialized it already.
1869 When doing a dynamic link, we create a .rela.got
1870 relocation entry to initialize the value. This is
1871 done in the finish_dynamic_symbol routine. */
1872 if ((off & 1) != 0)
1873 off &= ~1;
1874 else
1876 bfd_put_64 (output_bfd, relocation,
1877 htab->sgot->contents + off);
1878 h->got.offset |= 1;
1881 else
1882 unresolved_reloc = FALSE;
1884 else
1886 if (local_got_offsets == NULL)
1887 abort ();
1889 off = local_got_offsets[r_symndx];
1891 /* The offset must always be a multiple of 8. We use
1892 the least significant bit to record whether we have
1893 already generated the necessary reloc. */
1894 if ((off & 1) != 0)
1895 off &= ~1;
1896 else
1898 bfd_put_64 (output_bfd, relocation,
1899 htab->sgot->contents + off);
1901 if (info->shared)
1903 asection *s;
1904 Elf_Internal_Rela outrel;
1905 bfd_byte *loc;
1907 /* We need to generate a R_X86_64_RELATIVE reloc
1908 for the dynamic linker. */
1909 s = htab->srelgot;
1910 if (s == NULL)
1911 abort ();
1913 outrel.r_offset = (htab->sgot->output_section->vma
1914 + htab->sgot->output_offset
1915 + off);
1916 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1917 outrel.r_addend = relocation;
1918 loc = s->contents;
1919 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
1920 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1923 local_got_offsets[r_symndx] |= 1;
1927 if (off >= (bfd_vma) -2)
1928 abort ();
1930 relocation = htab->sgot->output_offset + off;
1931 if (r_type == R_X86_64_GOTPCREL)
1932 relocation += htab->sgot->output_section->vma;
1934 break;
1936 case R_X86_64_PLT32:
1937 /* Relocation is to the entry for this symbol in the
1938 procedure linkage table. */
1940 /* Resolve a PLT32 reloc against a local symbol directly,
1941 without using the procedure linkage table. */
1942 if (h == NULL)
1943 break;
1945 if (h->plt.offset == (bfd_vma) -1
1946 || htab->splt == NULL)
1948 /* We didn't make a PLT entry for this symbol. This
1949 happens when statically linking PIC code, or when
1950 using -Bsymbolic. */
1951 break;
1954 relocation = (htab->splt->output_section->vma
1955 + htab->splt->output_offset
1956 + h->plt.offset);
1957 unresolved_reloc = FALSE;
1958 break;
1960 case R_X86_64_PC8:
1961 case R_X86_64_PC16:
1962 case R_X86_64_PC32:
1963 case R_X86_64_8:
1964 case R_X86_64_16:
1965 case R_X86_64_32:
1966 case R_X86_64_64:
1967 /* FIXME: The ABI says the linker should make sure the value is
1968 the same when it's zeroextended to 64 bit. */
1970 /* r_symndx will be zero only for relocs against symbols
1971 from removed linkonce sections, or sections discarded by
1972 a linker script. */
1973 if (r_symndx == 0
1974 || (input_section->flags & SEC_ALLOC) == 0)
1975 break;
1977 if ((info->shared
1978 && (h == NULL
1979 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1980 || h->root.type != bfd_link_hash_undefweak)
1981 && ((r_type != R_X86_64_PC8
1982 && r_type != R_X86_64_PC16
1983 && r_type != R_X86_64_PC32)
1984 || !SYMBOL_CALLS_LOCAL (info, h)))
1985 || (ELIMINATE_COPY_RELOCS
1986 && !info->shared
1987 && h != NULL
1988 && h->dynindx != -1
1989 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1990 && (((h->elf_link_hash_flags
1991 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1992 && (h->elf_link_hash_flags
1993 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1994 || h->root.type == bfd_link_hash_undefweak
1995 || h->root.type == bfd_link_hash_undefined)))
1997 Elf_Internal_Rela outrel;
1998 bfd_byte *loc;
1999 bfd_boolean skip, relocate;
2000 asection *sreloc;
2002 /* When generating a shared object, these relocations
2003 are copied into the output file to be resolved at run
2004 time. */
2005 skip = FALSE;
2006 relocate = FALSE;
2008 outrel.r_offset =
2009 _bfd_elf_section_offset (output_bfd, info, input_section,
2010 rel->r_offset);
2011 if (outrel.r_offset == (bfd_vma) -1)
2012 skip = TRUE;
2013 else if (outrel.r_offset == (bfd_vma) -2)
2014 skip = TRUE, relocate = TRUE;
2016 outrel.r_offset += (input_section->output_section->vma
2017 + input_section->output_offset);
2019 if (skip)
2020 memset (&outrel, 0, sizeof outrel);
2022 /* h->dynindx may be -1 if this symbol was marked to
2023 become local. */
2024 else if (h != NULL
2025 && h->dynindx != -1
2026 && (r_type == R_X86_64_PC8
2027 || r_type == R_X86_64_PC16
2028 || r_type == R_X86_64_PC32
2029 || !info->shared
2030 || !info->symbolic
2031 || (h->elf_link_hash_flags
2032 & ELF_LINK_HASH_DEF_REGULAR) == 0))
2034 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
2035 outrel.r_addend = rel->r_addend;
2037 else
2039 /* This symbol is local, or marked to become local. */
2040 if (r_type == R_X86_64_64)
2042 relocate = TRUE;
2043 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2044 outrel.r_addend = relocation + rel->r_addend;
2046 else
2048 long sindx;
2050 if (bfd_is_abs_section (sec))
2051 sindx = 0;
2052 else if (sec == NULL || sec->owner == NULL)
2054 bfd_set_error (bfd_error_bad_value);
2055 return FALSE;
2057 else
2059 asection *osec;
2061 osec = sec->output_section;
2062 sindx = elf_section_data (osec)->dynindx;
2063 BFD_ASSERT (sindx > 0);
2066 outrel.r_info = ELF64_R_INFO (sindx, r_type);
2067 outrel.r_addend = relocation + rel->r_addend;
2071 sreloc = elf_section_data (input_section)->sreloc;
2072 if (sreloc == NULL)
2073 abort ();
2075 loc = sreloc->contents;
2076 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
2077 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2079 /* If this reloc is against an external symbol, we do
2080 not want to fiddle with the addend. Otherwise, we
2081 need to include the symbol value so that it becomes
2082 an addend for the dynamic reloc. */
2083 if (! relocate)
2084 continue;
2087 break;
2089 case R_X86_64_TLSGD:
2090 case R_X86_64_GOTTPOFF:
2091 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
2092 tls_type = GOT_UNKNOWN;
2093 if (h == NULL && local_got_offsets)
2094 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
2095 else if (h != NULL)
2097 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
2098 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
2099 r_type = R_X86_64_TPOFF32;
2101 if (r_type == R_X86_64_TLSGD)
2103 if (tls_type == GOT_TLS_IE)
2104 r_type = R_X86_64_GOTTPOFF;
2107 if (r_type == R_X86_64_TPOFF32)
2109 BFD_ASSERT (! unresolved_reloc);
2110 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
2112 unsigned int i;
2113 static unsigned char tlsgd[8]
2114 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2116 /* GD->LE transition.
2117 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2118 .word 0x6666; rex64; call __tls_get_addr@plt
2119 Change it into:
2120 movq %fs:0, %rax
2121 leaq foo@tpoff(%rax), %rax */
2122 BFD_ASSERT (rel->r_offset >= 4);
2123 for (i = 0; i < 4; i++)
2124 BFD_ASSERT (bfd_get_8 (input_bfd,
2125 contents + rel->r_offset - 4 + i)
2126 == tlsgd[i]);
2127 BFD_ASSERT (rel->r_offset + 12 <= input_section->_raw_size);
2128 for (i = 0; i < 4; i++)
2129 BFD_ASSERT (bfd_get_8 (input_bfd,
2130 contents + rel->r_offset + 4 + i)
2131 == tlsgd[i+4]);
2132 BFD_ASSERT (rel + 1 < relend);
2133 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2134 memcpy (contents + rel->r_offset - 4,
2135 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
2136 16);
2137 bfd_put_32 (output_bfd, tpoff (info, relocation),
2138 contents + rel->r_offset + 8);
2139 /* Skip R_X86_64_PLT32. */
2140 rel++;
2141 continue;
2143 else
2145 unsigned int val, type, reg;
2147 /* IE->LE transition:
2148 Originally it can be one of:
2149 movq foo@gottpoff(%rip), %reg
2150 addq foo@gottpoff(%rip), %reg
2151 We change it into:
2152 movq $foo, %reg
2153 leaq foo(%reg), %reg
2154 addq $foo, %reg. */
2155 BFD_ASSERT (rel->r_offset >= 3);
2156 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
2157 BFD_ASSERT (val == 0x48 || val == 0x4c);
2158 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
2159 BFD_ASSERT (type == 0x8b || type == 0x03);
2160 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
2161 BFD_ASSERT ((reg & 0xc7) == 5);
2162 reg >>= 3;
2163 BFD_ASSERT (rel->r_offset + 4 <= input_section->_raw_size);
2164 if (type == 0x8b)
2166 /* movq */
2167 if (val == 0x4c)
2168 bfd_put_8 (output_bfd, 0x49,
2169 contents + rel->r_offset - 3);
2170 bfd_put_8 (output_bfd, 0xc7,
2171 contents + rel->r_offset - 2);
2172 bfd_put_8 (output_bfd, 0xc0 | reg,
2173 contents + rel->r_offset - 1);
2175 else if (reg == 4)
2177 /* addq -> addq - addressing with %rsp/%r12 is
2178 special */
2179 if (val == 0x4c)
2180 bfd_put_8 (output_bfd, 0x49,
2181 contents + rel->r_offset - 3);
2182 bfd_put_8 (output_bfd, 0x81,
2183 contents + rel->r_offset - 2);
2184 bfd_put_8 (output_bfd, 0xc0 | reg,
2185 contents + rel->r_offset - 1);
2187 else
2189 /* addq -> leaq */
2190 if (val == 0x4c)
2191 bfd_put_8 (output_bfd, 0x4d,
2192 contents + rel->r_offset - 3);
2193 bfd_put_8 (output_bfd, 0x8d,
2194 contents + rel->r_offset - 2);
2195 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
2196 contents + rel->r_offset - 1);
2198 bfd_put_32 (output_bfd, tpoff (info, relocation),
2199 contents + rel->r_offset);
2200 continue;
2204 if (htab->sgot == NULL)
2205 abort ();
2207 if (h != NULL)
2208 off = h->got.offset;
2209 else
2211 if (local_got_offsets == NULL)
2212 abort ();
2214 off = local_got_offsets[r_symndx];
2217 if ((off & 1) != 0)
2218 off &= ~1;
2219 else
2221 Elf_Internal_Rela outrel;
2222 bfd_byte *loc;
2223 int dr_type, indx;
2225 if (htab->srelgot == NULL)
2226 abort ();
2228 outrel.r_offset = (htab->sgot->output_section->vma
2229 + htab->sgot->output_offset + off);
2231 indx = h && h->dynindx != -1 ? h->dynindx : 0;
2232 if (r_type == R_X86_64_TLSGD)
2233 dr_type = R_X86_64_DTPMOD64;
2234 else
2235 dr_type = R_X86_64_TPOFF64;
2237 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
2238 outrel.r_addend = 0;
2239 if (dr_type == R_X86_64_TPOFF64 && indx == 0)
2240 outrel.r_addend = relocation - dtpoff_base (info);
2241 outrel.r_info = ELF64_R_INFO (indx, dr_type);
2243 loc = htab->srelgot->contents;
2244 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2245 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2247 if (r_type == R_X86_64_TLSGD)
2249 if (indx == 0)
2251 BFD_ASSERT (! unresolved_reloc);
2252 bfd_put_64 (output_bfd,
2253 relocation - dtpoff_base (info),
2254 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2256 else
2258 bfd_put_64 (output_bfd, 0,
2259 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2260 outrel.r_info = ELF64_R_INFO (indx,
2261 R_X86_64_DTPOFF64);
2262 outrel.r_offset += GOT_ENTRY_SIZE;
2263 htab->srelgot->reloc_count++;
2264 loc += sizeof (Elf64_External_Rela);
2265 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2269 if (h != NULL)
2270 h->got.offset |= 1;
2271 else
2272 local_got_offsets[r_symndx] |= 1;
2275 if (off >= (bfd_vma) -2)
2276 abort ();
2277 if (r_type == ELF64_R_TYPE (rel->r_info))
2279 relocation = htab->sgot->output_section->vma
2280 + htab->sgot->output_offset + off;
2281 unresolved_reloc = FALSE;
2283 else
2285 unsigned int i;
2286 static unsigned char tlsgd[8]
2287 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
2289 /* GD->IE transition.
2290 .byte 0x66; leaq foo@tlsgd(%rip), %rdi
2291 .word 0x6666; rex64; call __tls_get_addr@plt
2292 Change it into:
2293 movq %fs:0, %rax
2294 addq foo@gottpoff(%rip), %rax */
2295 BFD_ASSERT (rel->r_offset >= 4);
2296 for (i = 0; i < 4; i++)
2297 BFD_ASSERT (bfd_get_8 (input_bfd,
2298 contents + rel->r_offset - 4 + i)
2299 == tlsgd[i]);
2300 BFD_ASSERT (rel->r_offset + 12 <= input_section->_raw_size);
2301 for (i = 0; i < 4; i++)
2302 BFD_ASSERT (bfd_get_8 (input_bfd,
2303 contents + rel->r_offset + 4 + i)
2304 == tlsgd[i+4]);
2305 BFD_ASSERT (rel + 1 < relend);
2306 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2307 memcpy (contents + rel->r_offset - 4,
2308 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
2309 16);
2311 relocation = (htab->sgot->output_section->vma
2312 + htab->sgot->output_offset + off
2313 - rel->r_offset
2314 - input_section->output_section->vma
2315 - input_section->output_offset
2316 - 12);
2317 bfd_put_32 (output_bfd, relocation,
2318 contents + rel->r_offset + 8);
2319 /* Skip R_X86_64_PLT32. */
2320 rel++;
2321 continue;
2323 break;
2325 case R_X86_64_TLSLD:
2326 if (! info->shared)
2328 /* LD->LE transition:
2329 Ensure it is:
2330 leaq foo@tlsld(%rip), %rdi; call __tls_get_addr@plt.
2331 We change it into:
2332 .word 0x6666; .byte 0x66; movl %fs:0, %rax. */
2333 BFD_ASSERT (rel->r_offset >= 3);
2334 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
2335 == 0x48);
2336 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
2337 == 0x8d);
2338 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
2339 == 0x3d);
2340 BFD_ASSERT (rel->r_offset + 9 <= input_section->_raw_size);
2341 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
2342 == 0xe8);
2343 BFD_ASSERT (rel + 1 < relend);
2344 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
2345 memcpy (contents + rel->r_offset - 3,
2346 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
2347 /* Skip R_X86_64_PLT32. */
2348 rel++;
2349 continue;
2352 if (htab->sgot == NULL)
2353 abort ();
2355 off = htab->tls_ld_got.offset;
2356 if (off & 1)
2357 off &= ~1;
2358 else
2360 Elf_Internal_Rela outrel;
2361 bfd_byte *loc;
2363 if (htab->srelgot == NULL)
2364 abort ();
2366 outrel.r_offset = (htab->sgot->output_section->vma
2367 + htab->sgot->output_offset + off);
2369 bfd_put_64 (output_bfd, 0,
2370 htab->sgot->contents + off);
2371 bfd_put_64 (output_bfd, 0,
2372 htab->sgot->contents + off + GOT_ENTRY_SIZE);
2373 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
2374 outrel.r_addend = 0;
2375 loc = htab->srelgot->contents;
2376 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2377 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
2378 htab->tls_ld_got.offset |= 1;
2380 relocation = htab->sgot->output_section->vma
2381 + htab->sgot->output_offset + off;
2382 unresolved_reloc = FALSE;
2383 break;
2385 case R_X86_64_DTPOFF32:
2386 if (info->shared || (input_section->flags & SEC_CODE) == 0)
2387 relocation -= dtpoff_base (info);
2388 else
2389 relocation = tpoff (info, relocation);
2390 break;
2392 case R_X86_64_TPOFF32:
2393 BFD_ASSERT (! info->shared);
2394 relocation = tpoff (info, relocation);
2395 break;
2397 default:
2398 break;
2401 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2402 because such sections are not SEC_ALLOC and thus ld.so will
2403 not process them. */
2404 if (unresolved_reloc
2405 && !((input_section->flags & SEC_DEBUGGING) != 0
2406 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
2407 (*_bfd_error_handler)
2408 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
2409 bfd_archive_filename (input_bfd),
2410 bfd_get_section_name (input_bfd, input_section),
2411 (long) rel->r_offset,
2412 h->root.root.string);
2414 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
2415 contents, rel->r_offset,
2416 relocation, rel->r_addend);
2418 if (r != bfd_reloc_ok)
2420 const char *name;
2422 if (h != NULL)
2423 name = h->root.root.string;
2424 else
2426 name = bfd_elf_string_from_elf_section (input_bfd,
2427 symtab_hdr->sh_link,
2428 sym->st_name);
2429 if (name == NULL)
2430 return FALSE;
2431 if (*name == '\0')
2432 name = bfd_section_name (input_bfd, sec);
2435 if (r == bfd_reloc_overflow)
2438 if (! ((*info->callbacks->reloc_overflow)
2439 (info, name, howto->name, (bfd_vma) 0,
2440 input_bfd, input_section, rel->r_offset)))
2441 return FALSE;
2443 else
2445 (*_bfd_error_handler)
2446 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
2447 bfd_archive_filename (input_bfd),
2448 bfd_get_section_name (input_bfd, input_section),
2449 (long) rel->r_offset, name, (int) r);
2450 return FALSE;
2455 return TRUE;
2458 /* Finish up dynamic symbol handling. We set the contents of various
2459 dynamic sections here. */
2461 static bfd_boolean
2462 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
2463 struct bfd_link_info *info,
2464 struct elf_link_hash_entry *h,
2465 Elf_Internal_Sym *sym)
2467 struct elf64_x86_64_link_hash_table *htab;
2469 htab = elf64_x86_64_hash_table (info);
2471 if (h->plt.offset != (bfd_vma) -1)
2473 bfd_vma plt_index;
2474 bfd_vma got_offset;
2475 Elf_Internal_Rela rela;
2476 bfd_byte *loc;
2478 /* This symbol has an entry in the procedure linkage table. Set
2479 it up. */
2480 if (h->dynindx == -1
2481 || htab->splt == NULL
2482 || htab->sgotplt == NULL
2483 || htab->srelplt == NULL)
2484 abort ();
2486 /* Get the index in the procedure linkage table which
2487 corresponds to this symbol. This is the index of this symbol
2488 in all the symbols for which we are making plt entries. The
2489 first entry in the procedure linkage table is reserved. */
2490 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2492 /* Get the offset into the .got table of the entry that
2493 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
2494 bytes. The first three are reserved for the dynamic linker. */
2495 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2497 /* Fill in the entry in the procedure linkage table. */
2498 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
2499 PLT_ENTRY_SIZE);
2501 /* Insert the relocation positions of the plt section. The magic
2502 numbers at the end of the statements are the positions of the
2503 relocations in the plt section. */
2504 /* Put offset for jmp *name@GOTPCREL(%rip), since the
2505 instruction uses 6 bytes, subtract this value. */
2506 bfd_put_32 (output_bfd,
2507 (htab->sgotplt->output_section->vma
2508 + htab->sgotplt->output_offset
2509 + got_offset
2510 - htab->splt->output_section->vma
2511 - htab->splt->output_offset
2512 - h->plt.offset
2513 - 6),
2514 htab->splt->contents + h->plt.offset + 2);
2515 /* Put relocation index. */
2516 bfd_put_32 (output_bfd, plt_index,
2517 htab->splt->contents + h->plt.offset + 7);
2518 /* Put offset for jmp .PLT0. */
2519 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
2520 htab->splt->contents + h->plt.offset + 12);
2522 /* Fill in the entry in the global offset table, initially this
2523 points to the pushq instruction in the PLT which is at offset 6. */
2524 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
2525 + htab->splt->output_offset
2526 + h->plt.offset + 6),
2527 htab->sgotplt->contents + got_offset);
2529 /* Fill in the entry in the .rela.plt section. */
2530 rela.r_offset = (htab->sgotplt->output_section->vma
2531 + htab->sgotplt->output_offset
2532 + got_offset);
2533 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
2534 rela.r_addend = 0;
2535 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
2536 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2538 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2540 /* Mark the symbol as undefined, rather than as defined in
2541 the .plt section. Leave the value alone. This is a clue
2542 for the dynamic linker, to make function pointer
2543 comparisons work between an application and shared
2544 library. */
2545 sym->st_shndx = SHN_UNDEF;
2549 if (h->got.offset != (bfd_vma) -1
2550 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_GD
2551 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
2553 Elf_Internal_Rela rela;
2554 bfd_byte *loc;
2556 /* This symbol has an entry in the global offset table. Set it
2557 up. */
2558 if (htab->sgot == NULL || htab->srelgot == NULL)
2559 abort ();
2561 rela.r_offset = (htab->sgot->output_section->vma
2562 + htab->sgot->output_offset
2563 + (h->got.offset &~ (bfd_vma) 1));
2565 /* If this is a static link, or it is a -Bsymbolic link and the
2566 symbol is defined locally or was forced to be local because
2567 of a version file, we just want to emit a RELATIVE reloc.
2568 The entry in the global offset table will already have been
2569 initialized in the relocate_section function. */
2570 if (info->shared
2571 && SYMBOL_REFERENCES_LOCAL (info, h))
2573 BFD_ASSERT((h->got.offset & 1) != 0);
2574 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2575 rela.r_addend = (h->root.u.def.value
2576 + h->root.u.def.section->output_section->vma
2577 + h->root.u.def.section->output_offset);
2579 else
2581 BFD_ASSERT((h->got.offset & 1) == 0);
2582 bfd_put_64 (output_bfd, (bfd_vma) 0,
2583 htab->sgot->contents + h->got.offset);
2584 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
2585 rela.r_addend = 0;
2588 loc = htab->srelgot->contents;
2589 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
2590 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2593 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2595 Elf_Internal_Rela rela;
2596 bfd_byte *loc;
2598 /* This symbol needs a copy reloc. Set it up. */
2600 if (h->dynindx == -1
2601 || (h->root.type != bfd_link_hash_defined
2602 && h->root.type != bfd_link_hash_defweak)
2603 || htab->srelbss == NULL)
2604 abort ();
2606 rela.r_offset = (h->root.u.def.value
2607 + h->root.u.def.section->output_section->vma
2608 + h->root.u.def.section->output_offset);
2609 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
2610 rela.r_addend = 0;
2611 loc = htab->srelbss->contents;
2612 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
2613 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2616 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2617 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2618 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2619 sym->st_shndx = SHN_ABS;
2621 return TRUE;
2624 /* Used to decide how to sort relocs in an optimal manner for the
2625 dynamic linker, before writing them out. */
2627 static enum elf_reloc_type_class
2628 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
2630 switch ((int) ELF64_R_TYPE (rela->r_info))
2632 case R_X86_64_RELATIVE:
2633 return reloc_class_relative;
2634 case R_X86_64_JUMP_SLOT:
2635 return reloc_class_plt;
2636 case R_X86_64_COPY:
2637 return reloc_class_copy;
2638 default:
2639 return reloc_class_normal;
2643 /* Finish up the dynamic sections. */
2645 static bfd_boolean
2646 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
2648 struct elf64_x86_64_link_hash_table *htab;
2649 bfd *dynobj;
2650 asection *sdyn;
2652 htab = elf64_x86_64_hash_table (info);
2653 dynobj = htab->elf.dynobj;
2654 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2656 if (htab->elf.dynamic_sections_created)
2658 Elf64_External_Dyn *dyncon, *dynconend;
2660 if (sdyn == NULL || htab->sgot == NULL)
2661 abort ();
2663 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2664 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2665 for (; dyncon < dynconend; dyncon++)
2667 Elf_Internal_Dyn dyn;
2668 asection *s;
2670 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2672 switch (dyn.d_tag)
2674 default:
2675 continue;
2677 case DT_PLTGOT:
2678 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
2679 break;
2681 case DT_JMPREL:
2682 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2683 break;
2685 case DT_PLTRELSZ:
2686 s = htab->srelplt->output_section;
2687 if (s->_cooked_size != 0)
2688 dyn.d_un.d_val = s->_cooked_size;
2689 else
2690 dyn.d_un.d_val = s->_raw_size;
2691 break;
2693 case DT_RELASZ:
2694 /* The procedure linkage table relocs (DT_JMPREL) should
2695 not be included in the overall relocs (DT_RELA).
2696 Therefore, we override the DT_RELASZ entry here to
2697 make it not include the JMPREL relocs. Since the
2698 linker script arranges for .rela.plt to follow all
2699 other relocation sections, we don't have to worry
2700 about changing the DT_RELA entry. */
2701 if (htab->srelplt != NULL)
2703 s = htab->srelplt->output_section;
2704 if (s->_cooked_size != 0)
2705 dyn.d_un.d_val -= s->_cooked_size;
2706 else
2707 dyn.d_un.d_val -= s->_raw_size;
2709 break;
2712 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2715 /* Fill in the special first entry in the procedure linkage table. */
2716 if (htab->splt && htab->splt->_raw_size > 0)
2718 /* Fill in the first entry in the procedure linkage table. */
2719 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
2720 PLT_ENTRY_SIZE);
2721 /* Add offset for pushq GOT+8(%rip), since the instruction
2722 uses 6 bytes subtract this value. */
2723 bfd_put_32 (output_bfd,
2724 (htab->sgotplt->output_section->vma
2725 + htab->sgotplt->output_offset
2727 - htab->splt->output_section->vma
2728 - htab->splt->output_offset
2729 - 6),
2730 htab->splt->contents + 2);
2731 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
2732 the end of the instruction. */
2733 bfd_put_32 (output_bfd,
2734 (htab->sgotplt->output_section->vma
2735 + htab->sgotplt->output_offset
2736 + 16
2737 - htab->splt->output_section->vma
2738 - htab->splt->output_offset
2739 - 12),
2740 htab->splt->contents + 8);
2742 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
2743 PLT_ENTRY_SIZE;
2747 if (htab->sgotplt)
2749 /* Fill in the first three entries in the global offset table. */
2750 if (htab->sgotplt->_raw_size > 0)
2752 /* Set the first entry in the global offset table to the address of
2753 the dynamic section. */
2754 if (sdyn == NULL)
2755 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
2756 else
2757 bfd_put_64 (output_bfd,
2758 sdyn->output_section->vma + sdyn->output_offset,
2759 htab->sgotplt->contents);
2760 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
2761 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
2762 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
2765 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
2766 GOT_ENTRY_SIZE;
2769 return TRUE;
2773 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
2774 #define TARGET_LITTLE_NAME "elf64-x86-64"
2775 #define ELF_ARCH bfd_arch_i386
2776 #define ELF_MACHINE_CODE EM_X86_64
2777 #define ELF_MAXPAGESIZE 0x100000
2779 #define elf_backend_can_gc_sections 1
2780 #define elf_backend_can_refcount 1
2781 #define elf_backend_want_got_plt 1
2782 #define elf_backend_plt_readonly 1
2783 #define elf_backend_want_plt_sym 0
2784 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
2785 #define elf_backend_rela_normal 1
2787 #define elf_info_to_howto elf64_x86_64_info_to_howto
2789 #define bfd_elf64_bfd_link_hash_table_create \
2790 elf64_x86_64_link_hash_table_create
2791 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
2793 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
2794 #define elf_backend_check_relocs elf64_x86_64_check_relocs
2795 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
2796 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
2797 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
2798 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
2799 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
2800 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
2801 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
2802 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
2803 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
2804 #define elf_backend_relocate_section elf64_x86_64_relocate_section
2805 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
2806 #define elf_backend_object_p elf64_x86_64_elf_object_p
2807 #define bfd_elf64_mkobject elf64_x86_64_mkobject
2809 #include "elf64-target.h"