Update Spanish and Swedish translations
[binutils.git] / bfd / elf64-x86-64.c
blobb6cce8eef991a0120f5372e1ac626efe2f84f814
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002 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, 4, 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, 4, 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, 4, 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, 4, 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, 4, 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, 4, 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, 4, 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),
82 /* GNU extension to record C++ vtable hierarchy. */
83 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, false, 0, complain_overflow_dont,
84 NULL, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
86 /* GNU extension to record C++ vtable member usage. */
87 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, false, 0, complain_overflow_dont,
88 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0,
89 false)
92 /* Map BFD relocs to the x86_64 elf relocs. */
93 struct elf_reloc_map
95 bfd_reloc_code_real_type bfd_reloc_val;
96 unsigned char elf_reloc_val;
99 static const struct elf_reloc_map x86_64_reloc_map[] =
101 { BFD_RELOC_NONE, R_X86_64_NONE, },
102 { BFD_RELOC_64, R_X86_64_64, },
103 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
104 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
105 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
106 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
107 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
108 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
109 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
110 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
111 { BFD_RELOC_32, R_X86_64_32, },
112 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
113 { BFD_RELOC_16, R_X86_64_16, },
114 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
115 { BFD_RELOC_8, R_X86_64_8, },
116 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
117 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
118 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
121 static reloc_howto_type *elf64_x86_64_reloc_type_lookup
122 PARAMS ((bfd *, bfd_reloc_code_real_type));
123 static void elf64_x86_64_info_to_howto
124 PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
125 static boolean elf64_x86_64_grok_prstatus
126 PARAMS ((bfd *, Elf_Internal_Note *));
127 static boolean elf64_x86_64_grok_psinfo
128 PARAMS ((bfd *, Elf_Internal_Note *));
129 static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create
130 PARAMS ((bfd *));
131 static boolean elf64_x86_64_elf_object_p PARAMS ((bfd *abfd));
132 static boolean create_got_section
133 PARAMS((bfd *, struct bfd_link_info *));
134 static boolean elf64_x86_64_create_dynamic_sections
135 PARAMS((bfd *, struct bfd_link_info *));
136 static void elf64_x86_64_copy_indirect_symbol
137 PARAMS ((struct elf_link_hash_entry *, struct elf_link_hash_entry *));
138 static boolean elf64_x86_64_check_relocs
139 PARAMS ((bfd *, struct bfd_link_info *, asection *sec,
140 const Elf_Internal_Rela *));
141 static asection *elf64_x86_64_gc_mark_hook
142 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
143 struct elf_link_hash_entry *, Elf_Internal_Sym *));
145 static boolean elf64_x86_64_gc_sweep_hook
146 PARAMS ((bfd *, struct bfd_link_info *, asection *,
147 const Elf_Internal_Rela *));
149 static struct bfd_hash_entry *link_hash_newfunc
150 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
151 static boolean elf64_x86_64_adjust_dynamic_symbol
152 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
154 static boolean allocate_dynrelocs
155 PARAMS ((struct elf_link_hash_entry *, PTR));
156 static boolean readonly_dynrelocs
157 PARAMS ((struct elf_link_hash_entry *, PTR));
158 static boolean elf64_x86_64_size_dynamic_sections
159 PARAMS ((bfd *, struct bfd_link_info *));
160 static boolean elf64_x86_64_relocate_section
161 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
162 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
163 static boolean elf64_x86_64_finish_dynamic_symbol
164 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
165 Elf_Internal_Sym *sym));
166 static boolean elf64_x86_64_finish_dynamic_sections
167 PARAMS ((bfd *, struct bfd_link_info *));
168 static enum elf_reloc_type_class elf64_x86_64_reloc_type_class
169 PARAMS ((const Elf_Internal_Rela *));
171 /* Given a BFD reloc type, return a HOWTO structure. */
172 static reloc_howto_type *
173 elf64_x86_64_reloc_type_lookup (abfd, code)
174 bfd *abfd ATTRIBUTE_UNUSED;
175 bfd_reloc_code_real_type code;
177 unsigned int i;
178 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
179 i++)
181 if (x86_64_reloc_map[i].bfd_reloc_val == code)
182 return &x86_64_elf_howto_table[i];
184 return 0;
187 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
189 static void
190 elf64_x86_64_info_to_howto (abfd, cache_ptr, dst)
191 bfd *abfd ATTRIBUTE_UNUSED;
192 arelent *cache_ptr;
193 Elf64_Internal_Rela *dst;
195 unsigned r_type, i;
197 r_type = ELF64_R_TYPE (dst->r_info);
198 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
200 BFD_ASSERT (r_type <= (unsigned int) R_X86_64_PC8);
201 i = r_type;
203 else
205 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
206 i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_PC8 - 1);
208 cache_ptr->howto = &x86_64_elf_howto_table[i];
209 BFD_ASSERT (r_type == cache_ptr->howto->type);
212 /* Support for core dump NOTE sections. */
213 static boolean
214 elf64_x86_64_grok_prstatus (abfd, note)
215 bfd *abfd;
216 Elf_Internal_Note *note;
218 int offset;
219 size_t raw_size;
221 switch (note->descsz)
223 default:
224 return false;
226 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
227 /* pr_cursig */
228 elf_tdata (abfd)->core_signal
229 = bfd_get_16 (abfd, note->descdata + 12);
231 /* pr_pid */
232 elf_tdata (abfd)->core_pid
233 = bfd_get_32 (abfd, note->descdata + 32);
235 /* pr_reg */
236 offset = 112;
237 raw_size = 216;
239 break;
242 /* Make a ".reg/999" section. */
243 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
244 raw_size, note->descpos + offset);
247 static boolean
248 elf64_x86_64_grok_psinfo (abfd, note)
249 bfd *abfd;
250 Elf_Internal_Note *note;
252 switch (note->descsz)
254 default:
255 return false;
257 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
258 elf_tdata (abfd)->core_program
259 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
260 elf_tdata (abfd)->core_command
261 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
264 /* Note that for some reason, a spurious space is tacked
265 onto the end of the args in some (at least one anyway)
266 implementations, so strip it off if it exists. */
269 char *command = elf_tdata (abfd)->core_command;
270 int n = strlen (command);
272 if (0 < n && command[n - 1] == ' ')
273 command[n - 1] = '\0';
276 return true;
279 /* Functions for the x86-64 ELF linker. */
281 /* The name of the dynamic interpreter. This is put in the .interp
282 section. */
284 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
286 /* The size in bytes of an entry in the global offset table. */
288 #define GOT_ENTRY_SIZE 8
290 /* The size in bytes of an entry in the procedure linkage table. */
292 #define PLT_ENTRY_SIZE 16
294 /* The first entry in a procedure linkage table looks like this. See the
295 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
297 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
299 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
300 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
301 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
304 /* Subsequent entries in a procedure linkage table look like this. */
306 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
308 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
309 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
310 0x68, /* pushq immediate */
311 0, 0, 0, 0, /* replaced with index into relocation table. */
312 0xe9, /* jmp relative */
313 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
316 /* The x86-64 linker needs to keep track of the number of relocs that
317 it decides to copy as dynamic relocs in check_relocs for each symbol.
318 This is so that it can later discard them if they are found to be
319 unnecessary. We store the information in a field extending the
320 regular ELF linker hash table. */
322 struct elf64_x86_64_dyn_relocs
324 /* Next section. */
325 struct elf64_x86_64_dyn_relocs *next;
327 /* The input section of the reloc. */
328 asection *sec;
330 /* Total number of relocs copied for the input section. */
331 bfd_size_type count;
333 /* Number of pc-relative relocs copied for the input section. */
334 bfd_size_type pc_count;
337 /* x86-64 ELF linker hash entry. */
339 struct elf64_x86_64_link_hash_entry
341 struct elf_link_hash_entry elf;
343 /* Track dynamic relocs copied for this symbol. */
344 struct elf64_x86_64_dyn_relocs *dyn_relocs;
347 /* x86-64 ELF linker hash table. */
349 struct elf64_x86_64_link_hash_table
351 struct elf_link_hash_table elf;
353 /* Short-cuts to get to dynamic linker sections. */
354 asection *sgot;
355 asection *sgotplt;
356 asection *srelgot;
357 asection *splt;
358 asection *srelplt;
359 asection *sdynbss;
360 asection *srelbss;
362 /* Small local sym to section mapping cache. */
363 struct sym_sec_cache sym_sec;
366 /* Get the x86-64 ELF linker hash table from a link_info structure. */
368 #define elf64_x86_64_hash_table(p) \
369 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
371 /* Create an entry in an x86-64 ELF linker hash table. */
373 static struct bfd_hash_entry *
374 link_hash_newfunc (entry, table, string)
375 struct bfd_hash_entry *entry;
376 struct bfd_hash_table *table;
377 const char *string;
379 /* Allocate the structure if it has not already been allocated by a
380 subclass. */
381 if (entry == NULL)
383 entry = bfd_hash_allocate (table,
384 sizeof (struct elf64_x86_64_link_hash_entry));
385 if (entry == NULL)
386 return entry;
389 /* Call the allocation method of the superclass. */
390 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
391 if (entry != NULL)
393 struct elf64_x86_64_link_hash_entry *eh;
395 eh = (struct elf64_x86_64_link_hash_entry *) entry;
396 eh->dyn_relocs = NULL;
399 return entry;
402 /* Create an X86-64 ELF linker hash table. */
404 static struct bfd_link_hash_table *
405 elf64_x86_64_link_hash_table_create (abfd)
406 bfd *abfd;
408 struct elf64_x86_64_link_hash_table *ret;
409 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
411 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
412 if (ret == NULL)
413 return NULL;
415 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
417 free (ret);
418 return NULL;
421 ret->sgot = NULL;
422 ret->sgotplt = NULL;
423 ret->srelgot = NULL;
424 ret->splt = NULL;
425 ret->srelplt = NULL;
426 ret->sdynbss = NULL;
427 ret->srelbss = NULL;
428 ret->sym_sec.abfd = NULL;
430 return &ret->elf.root;
433 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
434 shortcuts to them in our hash table. */
436 static boolean
437 create_got_section (dynobj, info)
438 bfd *dynobj;
439 struct bfd_link_info *info;
441 struct elf64_x86_64_link_hash_table *htab;
443 if (! _bfd_elf_create_got_section (dynobj, info))
444 return false;
446 htab = elf64_x86_64_hash_table (info);
447 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
448 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
449 if (!htab->sgot || !htab->sgotplt)
450 abort ();
452 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
453 if (htab->srelgot == NULL
454 || ! bfd_set_section_flags (dynobj, htab->srelgot,
455 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
456 | SEC_IN_MEMORY | SEC_LINKER_CREATED
457 | SEC_READONLY))
458 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
459 return false;
460 return true;
463 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
464 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
465 hash table. */
467 static boolean
468 elf64_x86_64_create_dynamic_sections (dynobj, info)
469 bfd *dynobj;
470 struct bfd_link_info *info;
472 struct elf64_x86_64_link_hash_table *htab;
474 htab = elf64_x86_64_hash_table (info);
475 if (!htab->sgot && !create_got_section (dynobj, info))
476 return false;
478 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
479 return false;
481 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
482 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
483 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
484 if (!info->shared)
485 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
487 if (!htab->splt || !htab->srelplt || !htab->sdynbss
488 || (!info->shared && !htab->srelbss))
489 abort ();
491 return true;
494 /* Copy the extra info we tack onto an elf_link_hash_entry. */
496 static void
497 elf64_x86_64_copy_indirect_symbol (dir, ind)
498 struct elf_link_hash_entry *dir, *ind;
500 struct elf64_x86_64_link_hash_entry *edir, *eind;
502 edir = (struct elf64_x86_64_link_hash_entry *) dir;
503 eind = (struct elf64_x86_64_link_hash_entry *) ind;
505 if (eind->dyn_relocs != NULL)
507 if (edir->dyn_relocs != NULL)
509 struct elf64_x86_64_dyn_relocs **pp;
510 struct elf64_x86_64_dyn_relocs *p;
512 if (ind->root.type == bfd_link_hash_indirect)
513 abort ();
515 /* Add reloc counts against the weak sym to the strong sym
516 list. Merge any entries against the same section. */
517 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
519 struct elf64_x86_64_dyn_relocs *q;
521 for (q = edir->dyn_relocs; q != NULL; q = q->next)
522 if (q->sec == p->sec)
524 q->pc_count += p->pc_count;
525 q->count += p->count;
526 *pp = p->next;
527 break;
529 if (q == NULL)
530 pp = &p->next;
532 *pp = edir->dyn_relocs;
535 edir->dyn_relocs = eind->dyn_relocs;
536 eind->dyn_relocs = NULL;
539 _bfd_elf_link_hash_copy_indirect (dir, ind);
542 static boolean
543 elf64_x86_64_elf_object_p (abfd)
544 bfd *abfd;
546 /* Set the right machine number for an x86-64 elf64 file. */
547 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
548 return true;
551 /* Look through the relocs for a section during the first phase, and
552 calculate needed space in the global offset table, procedure
553 linkage table, and dynamic reloc sections. */
555 static boolean
556 elf64_x86_64_check_relocs (abfd, info, sec, relocs)
557 bfd *abfd;
558 struct bfd_link_info *info;
559 asection *sec;
560 const Elf_Internal_Rela *relocs;
562 struct elf64_x86_64_link_hash_table *htab;
563 Elf_Internal_Shdr *symtab_hdr;
564 struct elf_link_hash_entry **sym_hashes;
565 const Elf_Internal_Rela *rel;
566 const Elf_Internal_Rela *rel_end;
567 asection *sreloc;
569 if (info->relocateable)
570 return true;
572 htab = elf64_x86_64_hash_table (info);
573 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
574 sym_hashes = elf_sym_hashes (abfd);
576 sreloc = NULL;
578 rel_end = relocs + sec->reloc_count;
579 for (rel = relocs; rel < rel_end; rel++)
581 unsigned long r_symndx;
582 struct elf_link_hash_entry *h;
584 r_symndx = ELF64_R_SYM (rel->r_info);
586 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
588 (*_bfd_error_handler) (_("%s: bad symbol index: %d"),
589 bfd_archive_filename (abfd),
590 r_symndx);
591 return false;
594 if (r_symndx < symtab_hdr->sh_info)
595 h = NULL;
596 else
597 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
599 switch (ELF64_R_TYPE (rel->r_info))
601 case R_X86_64_GOT32:
602 case R_X86_64_GOTPCREL:
603 /* This symbol requires a global offset table entry. */
604 if (h != NULL)
606 h->got.refcount += 1;
608 else
610 bfd_signed_vma *local_got_refcounts;
612 /* This is a global offset table entry for a local symbol. */
613 local_got_refcounts = elf_local_got_refcounts (abfd);
614 if (local_got_refcounts == NULL)
616 bfd_size_type size;
618 size = symtab_hdr->sh_info;
619 size *= sizeof (bfd_signed_vma);
620 local_got_refcounts = ((bfd_signed_vma *)
621 bfd_zalloc (abfd, size));
622 if (local_got_refcounts == NULL)
623 return false;
624 elf_local_got_refcounts (abfd) = local_got_refcounts;
626 local_got_refcounts[r_symndx] += 1;
628 /* Fall through */
630 //case R_X86_64_GOTPCREL:
631 if (htab->sgot == NULL)
633 if (htab->elf.dynobj == NULL)
634 htab->elf.dynobj = abfd;
635 if (!create_got_section (htab->elf.dynobj, info))
636 return false;
638 break;
640 case R_X86_64_PLT32:
641 /* This symbol requires a procedure linkage table entry. We
642 actually build the entry in adjust_dynamic_symbol,
643 because this might be a case of linking PIC code which is
644 never referenced by a dynamic object, in which case we
645 don't need to generate a procedure linkage table entry
646 after all. */
648 /* If this is a local symbol, we resolve it directly without
649 creating a procedure linkage table entry. */
650 if (h == NULL)
651 continue;
653 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
654 h->plt.refcount += 1;
655 break;
657 case R_X86_64_8:
658 case R_X86_64_16:
659 case R_X86_64_32:
660 case R_X86_64_32S:
661 /* Let's help debug shared library creation. These relocs
662 cannot be used in shared libs. Don't error out for
663 sections we don't care about, such as debug sections or
664 non-constant sections. */
665 if (info->shared
666 && (sec->flags & SEC_ALLOC) != 0
667 && (sec->flags & SEC_READONLY) != 0)
669 (*_bfd_error_handler)
670 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
671 bfd_archive_filename (abfd),
672 x86_64_elf_howto_table[ELF64_R_TYPE (rel->r_info)].name);
673 bfd_set_error (bfd_error_bad_value);
674 return false;
676 /* Fall through. */
678 case R_X86_64_PC8:
679 case R_X86_64_PC16:
680 case R_X86_64_PC32:
681 case R_X86_64_64:
682 if (h != NULL && !info->shared)
684 /* If this reloc is in a read-only section, we might
685 need a copy reloc. We can't check reliably at this
686 stage whether the section is read-only, as input
687 sections have not yet been mapped to output sections.
688 Tentatively set the flag for now, and correct in
689 adjust_dynamic_symbol. */
690 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
692 /* We may need a .plt entry if the function this reloc
693 refers to is in a shared lib. */
694 h->plt.refcount += 1;
697 /* If we are creating a shared library, and this is a reloc
698 against a global symbol, or a non PC relative reloc
699 against a local symbol, then we need to copy the reloc
700 into the shared library. However, if we are linking with
701 -Bsymbolic, we do not need to copy a reloc against a
702 global symbol which is defined in an object we are
703 including in the link (i.e., DEF_REGULAR is set). At
704 this point we have not seen all the input files, so it is
705 possible that DEF_REGULAR is not set now but will be set
706 later (it is never cleared). In case of a weak definition,
707 DEF_REGULAR may be cleared later by a strong definition in
708 a shared library. We account for that possibility below by
709 storing information in the relocs_copied field of the hash
710 table entry. A similar situation occurs when creating
711 shared libraries and symbol visibility changes render the
712 symbol local.
714 If on the other hand, we are creating an executable, we
715 may need to keep relocations for symbols satisfied by a
716 dynamic library if we manage to avoid copy relocs for the
717 symbol. */
718 if ((info->shared
719 && (sec->flags & SEC_ALLOC) != 0
720 && (((ELF64_R_TYPE (rel->r_info) != R_X86_64_PC8)
721 && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC16)
722 && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32))
723 || (h != NULL
724 && (! info->symbolic
725 || h->root.type == bfd_link_hash_defweak
726 || (h->elf_link_hash_flags
727 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
728 || (!info->shared
729 && (sec->flags & SEC_ALLOC) != 0
730 && h != NULL
731 && (h->root.type == bfd_link_hash_defweak
732 || (h->elf_link_hash_flags
733 & ELF_LINK_HASH_DEF_REGULAR) == 0)))
735 struct elf64_x86_64_dyn_relocs *p;
736 struct elf64_x86_64_dyn_relocs **head;
738 /* We must copy these reloc types into the output file.
739 Create a reloc section in dynobj and make room for
740 this reloc. */
741 if (sreloc == NULL)
743 const char *name;
744 bfd *dynobj;
746 name = (bfd_elf_string_from_elf_section
747 (abfd,
748 elf_elfheader (abfd)->e_shstrndx,
749 elf_section_data (sec)->rel_hdr.sh_name));
750 if (name == NULL)
751 return false;
753 if (strncmp (name, ".rela", 5) != 0
754 || strcmp (bfd_get_section_name (abfd, sec),
755 name + 5) != 0)
757 (*_bfd_error_handler)
758 (_("%s: bad relocation section name `%s\'"),
759 bfd_archive_filename (abfd), name);
762 if (htab->elf.dynobj == NULL)
763 htab->elf.dynobj = abfd;
765 dynobj = htab->elf.dynobj;
767 sreloc = bfd_get_section_by_name (dynobj, name);
768 if (sreloc == NULL)
770 flagword flags;
772 sreloc = bfd_make_section (dynobj, name);
773 flags = (SEC_HAS_CONTENTS | SEC_READONLY
774 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
775 if ((sec->flags & SEC_ALLOC) != 0)
776 flags |= SEC_ALLOC | SEC_LOAD;
777 if (sreloc == NULL
778 || ! bfd_set_section_flags (dynobj, sreloc, flags)
779 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
780 return false;
782 elf_section_data (sec)->sreloc = sreloc;
785 /* If this is a global symbol, we count the number of
786 relocations we need for this symbol. */
787 if (h != NULL)
789 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
791 else
793 /* Track dynamic relocs needed for local syms too.
794 We really need local syms available to do this
795 easily. Oh well. */
797 asection *s;
798 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
799 sec, r_symndx);
800 if (s == NULL)
801 return false;
803 head = ((struct elf64_x86_64_dyn_relocs **)
804 &elf_section_data (s)->local_dynrel);
807 p = *head;
808 if (p == NULL || p->sec != sec)
810 bfd_size_type amt = sizeof *p;
811 p = ((struct elf64_x86_64_dyn_relocs *)
812 bfd_alloc (htab->elf.dynobj, amt));
813 if (p == NULL)
814 return false;
815 p->next = *head;
816 *head = p;
817 p->sec = sec;
818 p->count = 0;
819 p->pc_count = 0;
822 p->count += 1;
823 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8
824 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16
825 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)
826 p->pc_count += 1;
828 break;
830 /* This relocation describes the C++ object vtable hierarchy.
831 Reconstruct it for later use during GC. */
832 case R_X86_64_GNU_VTINHERIT:
833 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
834 return false;
835 break;
837 /* This relocation describes which C++ vtable entries are actually
838 used. Record for later use during GC. */
839 case R_X86_64_GNU_VTENTRY:
840 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_addend))
841 return false;
842 break;
844 default:
845 break;
849 return true;
852 /* Return the section that should be marked against GC for a given
853 relocation. */
855 static asection *
856 elf64_x86_64_gc_mark_hook (sec, info, rel, h, sym)
857 asection *sec;
858 struct bfd_link_info *info ATTRIBUTE_UNUSED;
859 Elf_Internal_Rela *rel;
860 struct elf_link_hash_entry *h;
861 Elf_Internal_Sym *sym;
863 if (h != NULL)
865 switch (ELF64_R_TYPE (rel->r_info))
867 case R_X86_64_GNU_VTINHERIT:
868 case R_X86_64_GNU_VTENTRY:
869 break;
871 default:
872 switch (h->root.type)
874 case bfd_link_hash_defined:
875 case bfd_link_hash_defweak:
876 return h->root.u.def.section;
878 case bfd_link_hash_common:
879 return h->root.u.c.p->section;
881 default:
882 break;
886 else
887 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
889 return NULL;
892 /* Update the got entry reference counts for the section being removed. */
894 static boolean
895 elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs)
896 bfd *abfd;
897 struct bfd_link_info *info;
898 asection *sec;
899 const Elf_Internal_Rela *relocs;
901 Elf_Internal_Shdr *symtab_hdr;
902 struct elf_link_hash_entry **sym_hashes;
903 bfd_signed_vma *local_got_refcounts;
904 const Elf_Internal_Rela *rel, *relend;
905 unsigned long r_symndx;
906 struct elf_link_hash_entry *h;
908 elf_section_data (sec)->local_dynrel = NULL;
910 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
911 sym_hashes = elf_sym_hashes (abfd);
912 local_got_refcounts = elf_local_got_refcounts (abfd);
914 relend = relocs + sec->reloc_count;
915 for (rel = relocs; rel < relend; rel++)
916 switch (ELF64_R_TYPE (rel->r_info))
918 case R_X86_64_GOT32:
919 case R_X86_64_GOTPCREL:
920 r_symndx = ELF64_R_SYM (rel->r_info);
921 if (r_symndx >= symtab_hdr->sh_info)
923 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
924 if (h->got.refcount > 0)
925 h->got.refcount -= 1;
927 else if (local_got_refcounts != NULL)
929 if (local_got_refcounts[r_symndx] > 0)
930 local_got_refcounts[r_symndx] -= 1;
932 break;
934 case R_X86_64_8:
935 case R_X86_64_16:
936 case R_X86_64_32:
937 case R_X86_64_64:
938 case R_X86_64_32S:
939 case R_X86_64_PC8:
940 case R_X86_64_PC16:
941 case R_X86_64_PC32:
942 r_symndx = ELF64_R_SYM (rel->r_info);
943 if (r_symndx >= symtab_hdr->sh_info)
945 struct elf64_x86_64_link_hash_entry *eh;
946 struct elf64_x86_64_dyn_relocs **pp;
947 struct elf64_x86_64_dyn_relocs *p;
949 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
951 if (!info->shared && h->plt.refcount > 0)
952 h->plt.refcount -= 1;
954 eh = (struct elf64_x86_64_link_hash_entry *) h;
956 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
957 if (p->sec == sec)
959 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_PC8
960 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC16
961 || ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)
962 p->pc_count -= 1;
963 p->count -= 1;
964 if (p->count == 0)
965 *pp = p->next;
966 break;
969 break;
972 case R_X86_64_PLT32:
973 r_symndx = ELF64_R_SYM (rel->r_info);
974 if (r_symndx >= symtab_hdr->sh_info)
976 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
977 if (h->plt.refcount > 0)
978 h->plt.refcount -= 1;
980 break;
982 default:
983 break;
986 return true;
989 /* Adjust a symbol defined by a dynamic object and referenced by a
990 regular object. The current definition is in some section of the
991 dynamic object, but we're not including those sections. We have to
992 change the definition to something the rest of the link can
993 understand. */
995 static boolean
996 elf64_x86_64_adjust_dynamic_symbol (info, h)
997 struct bfd_link_info *info;
998 struct elf_link_hash_entry *h;
1000 struct elf64_x86_64_link_hash_table *htab;
1001 struct elf64_x86_64_link_hash_entry * eh;
1002 struct elf64_x86_64_dyn_relocs *p;
1003 asection *s;
1004 unsigned int power_of_two;
1006 /* If this is a function, put it in the procedure linkage table. We
1007 will fill in the contents of the procedure linkage table later,
1008 when we know the address of the .got section. */
1009 if (h->type == STT_FUNC
1010 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
1012 if (h->plt.refcount <= 0
1013 || (! info->shared
1014 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
1015 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0
1016 && h->root.type != bfd_link_hash_undefweak
1017 && h->root.type != bfd_link_hash_undefined))
1019 /* This case can occur if we saw a PLT32 reloc in an input
1020 file, but the symbol was never referred to by a dynamic
1021 object, or if all references were garbage collected. In
1022 such a case, we don't actually need to build a procedure
1023 linkage table, and we can just do a PC32 reloc instead. */
1024 h->plt.offset = (bfd_vma) -1;
1025 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1028 return true;
1030 else
1031 /* It's possible that we incorrectly decided a .plt reloc was
1032 needed for an R_X86_64_PC32 reloc to a non-function sym in
1033 check_relocs. We can't decide accurately between function and
1034 non-function syms in check-relocs; Objects loaded later in
1035 the link may change h->type. So fix it now. */
1036 h->plt.offset = (bfd_vma) -1;
1038 /* If this is a weak symbol, and there is a real definition, the
1039 processor independent code will have arranged for us to see the
1040 real definition first, and we can just use the same value. */
1041 if (h->weakdef != NULL)
1043 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
1044 || h->weakdef->root.type == bfd_link_hash_defweak);
1045 h->root.u.def.section = h->weakdef->root.u.def.section;
1046 h->root.u.def.value = h->weakdef->root.u.def.value;
1047 return true;
1050 /* This is a reference to a symbol defined by a dynamic object which
1051 is not a function. */
1053 /* If we are creating a shared library, we must presume that the
1054 only references to the symbol are via the global offset table.
1055 For such cases we need not do anything here; the relocations will
1056 be handled correctly by relocate_section. */
1057 if (info->shared)
1058 return true;
1060 /* If there are no references to this symbol that do not use the
1061 GOT, we don't need to generate a copy reloc. */
1062 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
1063 return true;
1065 /* If -z nocopyreloc was given, we won't generate them either. */
1066 if (info->nocopyreloc)
1068 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1069 return true;
1072 eh = (struct elf64_x86_64_link_hash_entry *) h;
1073 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1075 s = p->sec->output_section;
1076 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1077 break;
1080 /* If we didn't find any dynamic relocs in read-only sections, then
1081 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1082 if (p == NULL)
1084 h->elf_link_hash_flags &= ~ELF_LINK_NON_GOT_REF;
1085 return true;
1088 /* We must allocate the symbol in our .dynbss section, which will
1089 become part of the .bss section of the executable. There will be
1090 an entry for this symbol in the .dynsym section. The dynamic
1091 object will contain position independent code, so all references
1092 from the dynamic object to this symbol will go through the global
1093 offset table. The dynamic linker will use the .dynsym entry to
1094 determine the address it must put in the global offset table, so
1095 both the dynamic object and the regular object will refer to the
1096 same memory location for the variable. */
1098 htab = elf64_x86_64_hash_table (info);
1100 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1101 to copy the initial value out of the dynamic object and into the
1102 runtime process image. */
1103 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
1105 htab->srelbss->_raw_size += sizeof (Elf64_External_Rela);
1106 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
1109 /* We need to figure out the alignment required for this symbol. I
1110 have no idea how ELF linkers handle this. 16-bytes is the size
1111 of the largest type that requires hard alignment -- long double. */
1112 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1113 this construct. */
1114 power_of_two = bfd_log2 (h->size);
1115 if (power_of_two > 4)
1116 power_of_two = 4;
1118 /* Apply the required alignment. */
1119 s = htab->sdynbss;
1120 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
1121 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
1123 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
1124 return false;
1127 /* Define the symbol as being at this point in the section. */
1128 h->root.u.def.section = s;
1129 h->root.u.def.value = s->_raw_size;
1131 /* Increment the section size to make room for the symbol. */
1132 s->_raw_size += h->size;
1134 return true;
1137 /* This is the condition under which elf64_x86_64_finish_dynamic_symbol
1138 will be called from elflink.h. If elflink.h doesn't call our
1139 finish_dynamic_symbol routine, we'll need to do something about
1140 initializing any .plt and .got entries in elf64_x86_64_relocate_section. */
1141 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1142 ((DYN) \
1143 && ((INFO)->shared \
1144 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1145 && ((H)->dynindx != -1 \
1146 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1148 /* Allocate space in .plt, .got and associated reloc sections for
1149 dynamic relocs. */
1151 static boolean
1152 allocate_dynrelocs (h, inf)
1153 struct elf_link_hash_entry *h;
1154 PTR inf;
1156 struct bfd_link_info *info;
1157 struct elf64_x86_64_link_hash_table *htab;
1158 struct elf64_x86_64_link_hash_entry *eh;
1159 struct elf64_x86_64_dyn_relocs *p;
1161 if (h->root.type == bfd_link_hash_indirect)
1162 return true;
1164 if (h->root.type == bfd_link_hash_warning)
1165 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1167 info = (struct bfd_link_info *) inf;
1168 htab = elf64_x86_64_hash_table (info);
1170 if (htab->elf.dynamic_sections_created
1171 && h->plt.refcount > 0)
1173 /* Make sure this symbol is output as a dynamic symbol.
1174 Undefined weak syms won't yet be marked as dynamic. */
1175 if (h->dynindx == -1
1176 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1178 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1179 return false;
1182 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info, h))
1184 asection *s = htab->splt;
1186 /* If this is the first .plt entry, make room for the special
1187 first entry. */
1188 if (s->_raw_size == 0)
1189 s->_raw_size += PLT_ENTRY_SIZE;
1191 h->plt.offset = s->_raw_size;
1193 /* If this symbol is not defined in a regular file, and we are
1194 not generating a shared library, then set the symbol to this
1195 location in the .plt. This is required to make function
1196 pointers compare as equal between the normal executable and
1197 the shared library. */
1198 if (! info->shared
1199 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1201 h->root.u.def.section = s;
1202 h->root.u.def.value = h->plt.offset;
1205 /* Make room for this entry. */
1206 s->_raw_size += PLT_ENTRY_SIZE;
1208 /* We also need to make an entry in the .got.plt section, which
1209 will be placed in the .got section by the linker script. */
1210 htab->sgotplt->_raw_size += GOT_ENTRY_SIZE;
1212 /* We also need to make an entry in the .rela.plt section. */
1213 htab->srelplt->_raw_size += sizeof (Elf64_External_Rela);
1215 else
1217 h->plt.offset = (bfd_vma) -1;
1218 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1221 else
1223 h->plt.offset = (bfd_vma) -1;
1224 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
1227 if (h->got.refcount > 0)
1229 asection *s;
1230 boolean dyn;
1232 /* Make sure this symbol is output as a dynamic symbol.
1233 Undefined weak syms won't yet be marked as dynamic. */
1234 if (h->dynindx == -1
1235 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1237 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1238 return false;
1241 s = htab->sgot;
1242 h->got.offset = s->_raw_size;
1243 s->_raw_size += GOT_ENTRY_SIZE;
1244 dyn = htab->elf.dynamic_sections_created;
1245 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h))
1246 htab->srelgot->_raw_size += sizeof (Elf64_External_Rela);
1248 else
1249 h->got.offset = (bfd_vma) -1;
1251 eh = (struct elf64_x86_64_link_hash_entry *) h;
1252 if (eh->dyn_relocs == NULL)
1253 return true;
1255 /* In the shared -Bsymbolic case, discard space allocated for
1256 dynamic pc-relative relocs against symbols which turn out to be
1257 defined in regular objects. For the normal shared case, discard
1258 space for pc-relative relocs that have become local due to symbol
1259 visibility changes. */
1261 if (info->shared)
1263 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1264 && ((h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1265 || info->symbolic))
1267 struct elf64_x86_64_dyn_relocs **pp;
1269 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
1271 p->count -= p->pc_count;
1272 p->pc_count = 0;
1273 if (p->count == 0)
1274 *pp = p->next;
1275 else
1276 pp = &p->next;
1280 else
1282 /* For the non-shared case, discard space for relocs against
1283 symbols which turn out to need copy relocs or are not
1284 dynamic. */
1286 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1287 && (((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1288 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1289 || (htab->elf.dynamic_sections_created
1290 && (h->root.type == bfd_link_hash_undefweak
1291 || h->root.type == bfd_link_hash_undefined))))
1293 /* Make sure this symbol is output as a dynamic symbol.
1294 Undefined weak syms won't yet be marked as dynamic. */
1295 if (h->dynindx == -1
1296 && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
1298 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
1299 return false;
1302 /* If that succeeded, we know we'll be keeping all the
1303 relocs. */
1304 if (h->dynindx != -1)
1305 goto keep;
1308 eh->dyn_relocs = NULL;
1310 keep: ;
1313 /* Finally, allocate space. */
1314 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1316 asection *sreloc = elf_section_data (p->sec)->sreloc;
1317 sreloc->_raw_size += p->count * sizeof (Elf64_External_Rela);
1320 return true;
1323 /* Find any dynamic relocs that apply to read-only sections. */
1325 static boolean
1326 readonly_dynrelocs (h, inf)
1327 struct elf_link_hash_entry *h;
1328 PTR inf;
1330 struct elf64_x86_64_link_hash_entry *eh;
1331 struct elf64_x86_64_dyn_relocs *p;
1333 if (h->root.type == bfd_link_hash_warning)
1334 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1336 eh = (struct elf64_x86_64_link_hash_entry *) h;
1337 for (p = eh->dyn_relocs; p != NULL; p = p->next)
1339 asection *s = p->sec->output_section;
1341 if (s != NULL && (s->flags & SEC_READONLY) != 0)
1343 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1345 info->flags |= DF_TEXTREL;
1347 /* Not an error, just cut short the traversal. */
1348 return false;
1351 return true;
1354 /* Set the sizes of the dynamic sections. */
1356 static boolean
1357 elf64_x86_64_size_dynamic_sections (output_bfd, info)
1358 bfd *output_bfd ATTRIBUTE_UNUSED;
1359 struct bfd_link_info *info;
1361 struct elf64_x86_64_link_hash_table *htab;
1362 bfd *dynobj;
1363 asection *s;
1364 boolean relocs;
1365 bfd *ibfd;
1367 htab = elf64_x86_64_hash_table (info);
1368 dynobj = htab->elf.dynobj;
1369 if (dynobj == NULL)
1370 abort ();
1372 if (htab->elf.dynamic_sections_created)
1374 /* Set the contents of the .interp section to the interpreter. */
1375 if (! info->shared)
1377 s = bfd_get_section_by_name (dynobj, ".interp");
1378 if (s == NULL)
1379 abort ();
1380 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
1381 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
1385 /* Set up .got offsets for local syms, and space for local dynamic
1386 relocs. */
1387 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1389 bfd_signed_vma *local_got;
1390 bfd_signed_vma *end_local_got;
1391 bfd_size_type locsymcount;
1392 Elf_Internal_Shdr *symtab_hdr;
1393 asection *srel;
1395 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1396 continue;
1398 for (s = ibfd->sections; s != NULL; s = s->next)
1400 struct elf64_x86_64_dyn_relocs *p;
1402 for (p = *((struct elf64_x86_64_dyn_relocs **)
1403 &elf_section_data (s)->local_dynrel);
1404 p != NULL;
1405 p = p->next)
1407 if (!bfd_is_abs_section (p->sec)
1408 && bfd_is_abs_section (p->sec->output_section))
1410 /* Input section has been discarded, either because
1411 it is a copy of a linkonce section or due to
1412 linker script /DISCARD/, so we'll be discarding
1413 the relocs too. */
1415 else if (p->count != 0)
1417 srel = elf_section_data (p->sec)->sreloc;
1418 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
1419 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
1420 info->flags |= DF_TEXTREL;
1426 local_got = elf_local_got_refcounts (ibfd);
1427 if (!local_got)
1428 continue;
1430 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
1431 locsymcount = symtab_hdr->sh_info;
1432 end_local_got = local_got + locsymcount;
1433 s = htab->sgot;
1434 srel = htab->srelgot;
1435 for (; local_got < end_local_got; ++local_got)
1437 if (*local_got > 0)
1439 *local_got = s->_raw_size;
1440 s->_raw_size += GOT_ENTRY_SIZE;
1441 if (info->shared)
1442 srel->_raw_size += sizeof (Elf64_External_Rela);
1444 else
1445 *local_got = (bfd_vma) -1;
1449 /* Allocate global sym .plt and .got entries, and space for global
1450 sym dynamic relocs. */
1451 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
1453 /* We now have determined the sizes of the various dynamic sections.
1454 Allocate memory for them. */
1455 relocs = false;
1456 for (s = dynobj->sections; s != NULL; s = s->next)
1458 if ((s->flags & SEC_LINKER_CREATED) == 0)
1459 continue;
1461 if (s == htab->splt
1462 || s == htab->sgot
1463 || s == htab->sgotplt)
1465 /* Strip this section if we don't need it; see the
1466 comment below. */
1468 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
1470 if (s->_raw_size != 0 && s != htab->srelplt)
1471 relocs = true;
1473 /* We use the reloc_count field as a counter if we need
1474 to copy relocs into the output file. */
1475 s->reloc_count = 0;
1477 else
1479 /* It's not one of our sections, so don't allocate space. */
1480 continue;
1483 if (s->_raw_size == 0)
1485 /* If we don't need this section, strip it from the
1486 output file. This is mostly to handle .rela.bss and
1487 .rela.plt. We must create both sections in
1488 create_dynamic_sections, because they must be created
1489 before the linker maps input sections to output
1490 sections. The linker does that before
1491 adjust_dynamic_symbol is called, and it is that
1492 function which decides whether anything needs to go
1493 into these sections. */
1495 _bfd_strip_section_from_output (info, s);
1496 continue;
1499 /* Allocate memory for the section contents. We use bfd_zalloc
1500 here in case unused entries are not reclaimed before the
1501 section's contents are written out. This should not happen,
1502 but this way if it does, we get a R_X86_64_NONE reloc instead
1503 of garbage. */
1504 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1505 if (s->contents == NULL)
1506 return false;
1509 if (htab->elf.dynamic_sections_created)
1511 /* Add some entries to the .dynamic section. We fill in the
1512 values later, in elf64_x86_64_finish_dynamic_sections, but we
1513 must add the entries now so that we get the correct size for
1514 the .dynamic section. The DT_DEBUG entry is filled in by the
1515 dynamic linker and used by the debugger. */
1516 #define add_dynamic_entry(TAG, VAL) \
1517 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1519 if (! info->shared)
1521 if (!add_dynamic_entry (DT_DEBUG, 0))
1522 return false;
1525 if (htab->splt->_raw_size != 0)
1527 if (!add_dynamic_entry (DT_PLTGOT, 0)
1528 || !add_dynamic_entry (DT_PLTRELSZ, 0)
1529 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
1530 || !add_dynamic_entry (DT_JMPREL, 0))
1531 return false;
1534 if (relocs)
1536 if (!add_dynamic_entry (DT_RELA, 0)
1537 || !add_dynamic_entry (DT_RELASZ, 0)
1538 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
1539 return false;
1541 /* If any dynamic relocs apply to a read-only section,
1542 then we need a DT_TEXTREL entry. */
1543 if ((info->flags & DF_TEXTREL) == 0)
1544 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
1545 (PTR) info);
1547 if ((info->flags & DF_TEXTREL) != 0)
1549 if (!add_dynamic_entry (DT_TEXTREL, 0))
1550 return false;
1554 #undef add_dynamic_entry
1556 return true;
1559 /* Relocate an x86_64 ELF section. */
1561 static boolean
1562 elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
1563 contents, relocs, local_syms, local_sections)
1564 bfd *output_bfd;
1565 struct bfd_link_info *info;
1566 bfd *input_bfd;
1567 asection *input_section;
1568 bfd_byte *contents;
1569 Elf_Internal_Rela *relocs;
1570 Elf_Internal_Sym *local_syms;
1571 asection **local_sections;
1573 struct elf64_x86_64_link_hash_table *htab;
1574 Elf_Internal_Shdr *symtab_hdr;
1575 struct elf_link_hash_entry **sym_hashes;
1576 bfd_vma *local_got_offsets;
1577 Elf_Internal_Rela *rel;
1578 Elf_Internal_Rela *relend;
1580 if (info->relocateable)
1581 return true;
1583 htab = elf64_x86_64_hash_table (info);
1584 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1585 sym_hashes = elf_sym_hashes (input_bfd);
1586 local_got_offsets = elf_local_got_offsets (input_bfd);
1588 rel = relocs;
1589 relend = relocs + input_section->reloc_count;
1590 for (; rel < relend; rel++)
1592 int r_type;
1593 reloc_howto_type *howto;
1594 unsigned long r_symndx;
1595 struct elf_link_hash_entry *h;
1596 Elf_Internal_Sym *sym;
1597 asection *sec;
1598 bfd_vma off;
1599 bfd_vma relocation;
1600 boolean unresolved_reloc;
1601 bfd_reloc_status_type r;
1603 r_type = ELF64_R_TYPE (rel->r_info);
1604 if (r_type == (int) R_X86_64_GNU_VTINHERIT
1605 || r_type == (int) R_X86_64_GNU_VTENTRY)
1606 continue;
1608 if (r_type < 0 || r_type >= R_X86_64_max)
1610 bfd_set_error (bfd_error_bad_value);
1611 return false;
1614 howto = x86_64_elf_howto_table + r_type;
1615 r_symndx = ELF64_R_SYM (rel->r_info);
1616 h = NULL;
1617 sym = NULL;
1618 sec = NULL;
1619 unresolved_reloc = false;
1620 if (r_symndx < symtab_hdr->sh_info)
1622 sym = local_syms + r_symndx;
1623 sec = local_sections[r_symndx];
1625 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
1627 else
1629 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1630 while (h->root.type == bfd_link_hash_indirect
1631 || h->root.type == bfd_link_hash_warning)
1632 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1634 if (h->root.type == bfd_link_hash_defined
1635 || h->root.type == bfd_link_hash_defweak)
1637 sec = h->root.u.def.section;
1638 if (sec->output_section == NULL)
1640 /* Set a flag that will be cleared later if we find a
1641 relocation value for this symbol. output_section
1642 is typically NULL for symbols satisfied by a shared
1643 library. */
1644 unresolved_reloc = true;
1645 relocation = 0;
1647 else
1648 relocation = (h->root.u.def.value
1649 + sec->output_section->vma
1650 + sec->output_offset);
1652 else if (h->root.type == bfd_link_hash_undefweak)
1653 relocation = 0;
1654 else if (info->shared
1655 && (!info->symbolic || info->allow_shlib_undefined)
1656 && !info->no_undefined
1657 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1658 relocation = 0;
1659 else
1661 if (! ((*info->callbacks->undefined_symbol)
1662 (info, h->root.root.string, input_bfd,
1663 input_section, rel->r_offset,
1664 (!info->shared || info->no_undefined
1665 || ELF_ST_VISIBILITY (h->other)))))
1666 return false;
1667 relocation = 0;
1670 /* When generating a shared object, the relocations handled here are
1671 copied into the output file to be resolved at run time. */
1672 switch (r_type)
1674 case R_X86_64_GOT32:
1675 /* Relocation is to the entry for this symbol in the global
1676 offset table. */
1677 case R_X86_64_GOTPCREL:
1678 /* Use global offset table as symbol value. */
1679 if (htab->sgot == NULL)
1680 abort ();
1682 if (h != NULL)
1684 boolean dyn;
1686 off = h->got.offset;
1687 dyn = htab->elf.dynamic_sections_created;
1689 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info, h)
1690 || (info->shared
1691 && (info->symbolic
1692 || h->dynindx == -1
1693 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
1694 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1696 /* This is actually a static link, or it is a -Bsymbolic
1697 link and the symbol is defined locally, or the symbol
1698 was forced to be local because of a version file. We
1699 must initialize this entry in the global offset table.
1700 Since the offset must always be a multiple of 8, we
1701 use the least significant bit to record whether we
1702 have initialized it already.
1704 When doing a dynamic link, we create a .rela.got
1705 relocation entry to initialize the value. This is
1706 done in the finish_dynamic_symbol routine. */
1707 if ((off & 1) != 0)
1708 off &= ~1;
1709 else
1711 bfd_put_64 (output_bfd, relocation,
1712 htab->sgot->contents + off);
1713 h->got.offset |= 1;
1716 else
1717 unresolved_reloc = false;
1719 else
1721 if (local_got_offsets == NULL)
1722 abort ();
1724 off = local_got_offsets[r_symndx];
1726 /* The offset must always be a multiple of 8. We use
1727 the least significant bit to record whether we have
1728 already generated the necessary reloc. */
1729 if ((off & 1) != 0)
1730 off &= ~1;
1731 else
1733 bfd_put_64 (output_bfd, relocation,
1734 htab->sgot->contents + off);
1736 if (info->shared)
1738 asection *srelgot;
1739 Elf_Internal_Rela outrel;
1740 Elf64_External_Rela *loc;
1742 /* We need to generate a R_X86_64_RELATIVE reloc
1743 for the dynamic linker. */
1744 srelgot = htab->srelgot;
1745 if (srelgot == NULL)
1746 abort ();
1748 outrel.r_offset = (htab->sgot->output_section->vma
1749 + htab->sgot->output_offset
1750 + off);
1751 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1752 outrel.r_addend = relocation;
1753 loc = (Elf64_External_Rela *) srelgot->contents;
1754 loc += srelgot->reloc_count++;
1755 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1758 local_got_offsets[r_symndx] |= 1;
1762 if (off >= (bfd_vma) -2)
1763 abort ();
1765 relocation = htab->sgot->output_offset + off;
1766 if (r_type == R_X86_64_GOTPCREL)
1767 relocation += htab->sgot->output_section->vma;
1769 break;
1771 case R_X86_64_PLT32:
1772 /* Relocation is to the entry for this symbol in the
1773 procedure linkage table. */
1775 /* Resolve a PLT32 reloc against a local symbol directly,
1776 without using the procedure linkage table. */
1777 if (h == NULL)
1778 break;
1780 if (h->plt.offset == (bfd_vma) -1
1781 || htab->splt == NULL)
1783 /* We didn't make a PLT entry for this symbol. This
1784 happens when statically linking PIC code, or when
1785 using -Bsymbolic. */
1786 break;
1789 relocation = (htab->splt->output_section->vma
1790 + htab->splt->output_offset
1791 + h->plt.offset);
1792 unresolved_reloc = false;
1793 break;
1795 case R_X86_64_PC8:
1796 case R_X86_64_PC16:
1797 case R_X86_64_PC32:
1798 case R_X86_64_8:
1799 case R_X86_64_16:
1800 case R_X86_64_32:
1801 case R_X86_64_64:
1802 /* FIXME: The ABI says the linker should make sure the value is
1803 the same when it's zeroextended to 64 bit. */
1805 /* r_symndx will be zero only for relocs against symbols
1806 from removed linkonce sections, or sections discarded by
1807 a linker script. */
1808 if (r_symndx == 0
1809 || (input_section->flags & SEC_ALLOC) == 0)
1810 break;
1812 if ((info->shared
1813 && ((r_type != R_X86_64_PC8
1814 && r_type != R_X86_64_PC16
1815 && r_type != R_X86_64_PC32)
1816 || (h != NULL
1817 && h->dynindx != -1
1818 && (! info->symbolic
1819 || (h->elf_link_hash_flags
1820 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1821 || (!info->shared
1822 && h != NULL
1823 && h->dynindx != -1
1824 && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
1825 && (((h->elf_link_hash_flags
1826 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
1827 && (h->elf_link_hash_flags
1828 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1829 || h->root.type == bfd_link_hash_undefweak
1830 || h->root.type == bfd_link_hash_undefined)))
1832 Elf_Internal_Rela outrel;
1833 boolean skip, relocate;
1834 asection *sreloc;
1835 Elf64_External_Rela *loc;
1837 /* When generating a shared object, these relocations
1838 are copied into the output file to be resolved at run
1839 time. */
1841 skip = false;
1842 relocate = false;
1844 outrel.r_offset =
1845 _bfd_elf_section_offset (output_bfd, info, input_section,
1846 rel->r_offset);
1847 if (outrel.r_offset == (bfd_vma) -1)
1848 skip = true;
1849 else if (outrel.r_offset == (bfd_vma) -2)
1850 skip = true, relocate = true;
1852 outrel.r_offset += (input_section->output_section->vma
1853 + input_section->output_offset);
1855 if (skip)
1856 memset (&outrel, 0, sizeof outrel);
1858 /* h->dynindx may be -1 if this symbol was marked to
1859 become local. */
1860 else if (h != NULL
1861 && h->dynindx != -1
1862 && (r_type == R_X86_64_PC8
1863 || r_type == R_X86_64_PC16
1864 || r_type == R_X86_64_PC32
1865 || !info->shared
1866 || !info->symbolic
1867 || (h->elf_link_hash_flags
1868 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1870 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
1871 outrel.r_addend = rel->r_addend;
1873 else
1875 /* This symbol is local, or marked to become local. */
1876 if (r_type == R_X86_64_64)
1878 relocate = true;
1879 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1880 outrel.r_addend = relocation + rel->r_addend;
1882 else
1884 long sindx;
1886 if (h == NULL)
1887 sec = local_sections[r_symndx];
1888 else
1890 BFD_ASSERT (h->root.type == bfd_link_hash_defined
1891 || (h->root.type
1892 == bfd_link_hash_defweak));
1893 sec = h->root.u.def.section;
1895 if (sec != NULL && bfd_is_abs_section (sec))
1896 sindx = 0;
1897 else if (sec == NULL || sec->owner == NULL)
1899 bfd_set_error (bfd_error_bad_value);
1900 return false;
1902 else
1904 asection *osec;
1906 osec = sec->output_section;
1907 sindx = elf_section_data (osec)->dynindx;
1908 BFD_ASSERT (sindx > 0);
1911 outrel.r_info = ELF64_R_INFO (sindx, r_type);
1912 outrel.r_addend = relocation + rel->r_addend;
1916 sreloc = elf_section_data (input_section)->sreloc;
1917 if (sreloc == NULL)
1918 abort ();
1920 loc = (Elf64_External_Rela *) sreloc->contents;
1921 loc += sreloc->reloc_count++;
1922 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
1924 /* If this reloc is against an external symbol, we do
1925 not want to fiddle with the addend. Otherwise, we
1926 need to include the symbol value so that it becomes
1927 an addend for the dynamic reloc. */
1928 if (! relocate)
1929 continue;
1932 break;
1934 default:
1935 break;
1938 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1939 because such sections are not SEC_ALLOC and thus ld.so will
1940 not process them. */
1941 if (unresolved_reloc
1942 && !((input_section->flags & SEC_DEBUGGING) != 0
1943 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0))
1944 (*_bfd_error_handler)
1945 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
1946 bfd_archive_filename (input_bfd),
1947 bfd_get_section_name (input_bfd, input_section),
1948 (long) rel->r_offset,
1949 h->root.root.string);
1951 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1952 contents, rel->r_offset,
1953 relocation, rel->r_addend);
1955 if (r != bfd_reloc_ok)
1957 const char *name;
1959 if (h != NULL)
1960 name = h->root.root.string;
1961 else
1963 name = bfd_elf_string_from_elf_section (input_bfd,
1964 symtab_hdr->sh_link,
1965 sym->st_name);
1966 if (name == NULL)
1967 return false;
1968 if (*name == '\0')
1969 name = bfd_section_name (input_bfd, sec);
1972 if (r == bfd_reloc_overflow)
1975 if (! ((*info->callbacks->reloc_overflow)
1976 (info, name, howto->name, (bfd_vma) 0,
1977 input_bfd, input_section, rel->r_offset)))
1978 return false;
1980 else
1982 (*_bfd_error_handler)
1983 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
1984 bfd_archive_filename (input_bfd),
1985 bfd_get_section_name (input_bfd, input_section),
1986 (long) rel->r_offset, name, (int) r);
1987 return false;
1992 return true;
1995 /* Finish up dynamic symbol handling. We set the contents of various
1996 dynamic sections here. */
1998 static boolean
1999 elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym)
2000 bfd *output_bfd;
2001 struct bfd_link_info *info;
2002 struct elf_link_hash_entry *h;
2003 Elf_Internal_Sym *sym;
2005 struct elf64_x86_64_link_hash_table *htab;
2007 htab = elf64_x86_64_hash_table (info);
2009 if (h->plt.offset != (bfd_vma) -1)
2011 bfd_vma plt_index;
2012 bfd_vma got_offset;
2013 Elf_Internal_Rela rela;
2014 Elf64_External_Rela *loc;
2016 /* This symbol has an entry in the procedure linkage table. Set
2017 it up. */
2019 if (h->dynindx == -1
2020 || htab->splt == NULL
2021 || htab->sgotplt == NULL
2022 || htab->srelplt == NULL)
2023 abort ();
2025 /* Get the index in the procedure linkage table which
2026 corresponds to this symbol. This is the index of this symbol
2027 in all the symbols for which we are making plt entries. The
2028 first entry in the procedure linkage table is reserved. */
2029 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
2031 /* Get the offset into the .got table of the entry that
2032 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
2033 bytes. The first three are reserved for the dynamic linker. */
2034 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
2036 /* Fill in the entry in the procedure linkage table. */
2037 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
2038 PLT_ENTRY_SIZE);
2040 /* Insert the relocation positions of the plt section. The magic
2041 numbers at the end of the statements are the positions of the
2042 relocations in the plt section. */
2043 /* Put offset for jmp *name@GOTPCREL(%rip), since the
2044 instruction uses 6 bytes, subtract this value. */
2045 bfd_put_32 (output_bfd,
2046 (htab->sgotplt->output_section->vma
2047 + htab->sgotplt->output_offset
2048 + got_offset
2049 - htab->splt->output_section->vma
2050 - htab->splt->output_offset
2051 - h->plt.offset
2052 - 6),
2053 htab->splt->contents + h->plt.offset + 2);
2054 /* Put relocation index. */
2055 bfd_put_32 (output_bfd, plt_index,
2056 htab->splt->contents + h->plt.offset + 7);
2057 /* Put offset for jmp .PLT0. */
2058 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
2059 htab->splt->contents + h->plt.offset + 12);
2061 /* Fill in the entry in the global offset table, initially this
2062 points to the pushq instruction in the PLT which is at offset 6. */
2063 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
2064 + htab->splt->output_offset
2065 + h->plt.offset + 6),
2066 htab->sgotplt->contents + got_offset);
2068 /* Fill in the entry in the .rela.plt section. */
2069 rela.r_offset = (htab->sgotplt->output_section->vma
2070 + htab->sgotplt->output_offset
2071 + got_offset);
2072 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
2073 rela.r_addend = 0;
2074 loc = (Elf64_External_Rela *) htab->srelplt->contents + plt_index;
2075 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2077 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
2079 /* Mark the symbol as undefined, rather than as defined in
2080 the .plt section. Leave the value alone. This is a clue
2081 for the dynamic linker, to make function pointer
2082 comparisons work between an application and shared
2083 library. */
2084 sym->st_shndx = SHN_UNDEF;
2088 if (h->got.offset != (bfd_vma) -1)
2090 Elf_Internal_Rela rela;
2091 Elf64_External_Rela *loc;
2093 /* This symbol has an entry in the global offset table. Set it
2094 up. */
2096 if (htab->sgot == NULL || htab->srelgot == NULL)
2097 abort ();
2099 rela.r_offset = (htab->sgot->output_section->vma
2100 + htab->sgot->output_offset
2101 + (h->got.offset &~ (bfd_vma) 1));
2103 /* If this is a static link, or it is a -Bsymbolic link and the
2104 symbol is defined locally or was forced to be local because
2105 of a version file, we just want to emit a RELATIVE reloc.
2106 The entry in the global offset table will already have been
2107 initialized in the relocate_section function. */
2108 if (info->shared
2109 && (info->symbolic
2110 || h->dynindx == -1
2111 || (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL))
2112 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR))
2114 BFD_ASSERT((h->got.offset & 1) != 0);
2115 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
2116 rela.r_addend = (h->root.u.def.value
2117 + h->root.u.def.section->output_section->vma
2118 + h->root.u.def.section->output_offset);
2120 else
2122 BFD_ASSERT((h->got.offset & 1) == 0);
2123 bfd_put_64 (output_bfd, (bfd_vma) 0,
2124 htab->sgot->contents + h->got.offset);
2125 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
2126 rela.r_addend = 0;
2129 loc = (Elf64_External_Rela *) htab->srelgot->contents;
2130 loc += htab->srelgot->reloc_count++;
2131 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2134 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
2136 Elf_Internal_Rela rela;
2137 Elf64_External_Rela *loc;
2139 /* This symbol needs a copy reloc. Set it up. */
2141 if (h->dynindx == -1
2142 || (h->root.type != bfd_link_hash_defined
2143 && h->root.type != bfd_link_hash_defweak)
2144 || htab->srelbss == NULL)
2145 abort ();
2147 rela.r_offset = (h->root.u.def.value
2148 + h->root.u.def.section->output_section->vma
2149 + h->root.u.def.section->output_offset);
2150 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
2151 rela.r_addend = 0;
2152 loc = (Elf64_External_Rela *) htab->srelbss->contents;
2153 loc += htab->srelbss->reloc_count++;
2154 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
2157 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2158 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2159 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2160 sym->st_shndx = SHN_ABS;
2162 return true;
2165 /* Used to decide how to sort relocs in an optimal manner for the
2166 dynamic linker, before writing them out. */
2168 static enum elf_reloc_type_class
2169 elf64_x86_64_reloc_type_class (rela)
2170 const Elf_Internal_Rela *rela;
2172 switch ((int) ELF64_R_TYPE (rela->r_info))
2174 case R_X86_64_RELATIVE:
2175 return reloc_class_relative;
2176 case R_X86_64_JUMP_SLOT:
2177 return reloc_class_plt;
2178 case R_X86_64_COPY:
2179 return reloc_class_copy;
2180 default:
2181 return reloc_class_normal;
2185 /* Finish up the dynamic sections. */
2187 static boolean
2188 elf64_x86_64_finish_dynamic_sections (output_bfd, info)
2189 bfd *output_bfd;
2190 struct bfd_link_info *info;
2192 struct elf64_x86_64_link_hash_table *htab;
2193 bfd *dynobj;
2194 asection *sdyn;
2196 htab = elf64_x86_64_hash_table (info);
2197 dynobj = htab->elf.dynobj;
2198 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
2200 if (htab->elf.dynamic_sections_created)
2202 Elf64_External_Dyn *dyncon, *dynconend;
2204 if (sdyn == NULL || htab->sgot == NULL)
2205 abort ();
2207 dyncon = (Elf64_External_Dyn *) sdyn->contents;
2208 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
2209 for (; dyncon < dynconend; dyncon++)
2211 Elf_Internal_Dyn dyn;
2212 asection *s;
2214 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
2216 switch (dyn.d_tag)
2218 default:
2219 continue;
2221 case DT_PLTGOT:
2222 dyn.d_un.d_ptr = htab->sgot->output_section->vma;
2223 break;
2225 case DT_JMPREL:
2226 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
2227 break;
2229 case DT_PLTRELSZ:
2230 s = htab->srelplt->output_section;
2231 if (s->_cooked_size != 0)
2232 dyn.d_un.d_val = s->_cooked_size;
2233 else
2234 dyn.d_un.d_val = s->_raw_size;
2235 break;
2237 case DT_RELASZ:
2238 /* The procedure linkage table relocs (DT_JMPREL) should
2239 not be included in the overall relocs (DT_RELA).
2240 Therefore, we override the DT_RELASZ entry here to
2241 make it not include the JMPREL relocs. Since the
2242 linker script arranges for .rela.plt to follow all
2243 other relocation sections, we don't have to worry
2244 about changing the DT_RELA entry. */
2245 if (htab->srelplt != NULL)
2247 s = htab->srelplt->output_section;
2248 if (s->_cooked_size != 0)
2249 dyn.d_un.d_val -= s->_cooked_size;
2250 else
2251 dyn.d_un.d_val -= s->_raw_size;
2253 break;
2256 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
2259 /* Fill in the special first entry in the procedure linkage table. */
2260 if (htab->splt && htab->splt->_raw_size > 0)
2262 /* Fill in the first entry in the procedure linkage table. */
2263 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
2264 PLT_ENTRY_SIZE);
2265 /* Add offset for pushq GOT+8(%rip), since the instruction
2266 uses 6 bytes subtract this value. */
2267 bfd_put_32 (output_bfd,
2268 (htab->sgotplt->output_section->vma
2269 + htab->sgotplt->output_offset
2271 - htab->splt->output_section->vma
2272 - htab->splt->output_offset
2273 - 6),
2274 htab->splt->contents + 2);
2275 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
2276 the end of the instruction. */
2277 bfd_put_32 (output_bfd,
2278 (htab->sgotplt->output_section->vma
2279 + htab->sgotplt->output_offset
2280 + 16
2281 - htab->splt->output_section->vma
2282 - htab->splt->output_offset
2283 - 12),
2284 htab->splt->contents + 8);
2286 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
2287 PLT_ENTRY_SIZE;
2291 if (htab->sgotplt)
2293 /* Fill in the first three entries in the global offset table. */
2294 if (htab->sgotplt->_raw_size > 0)
2296 /* Set the first entry in the global offset table to the address of
2297 the dynamic section. */
2298 if (sdyn == NULL)
2299 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
2300 else
2301 bfd_put_64 (output_bfd,
2302 sdyn->output_section->vma + sdyn->output_offset,
2303 htab->sgotplt->contents);
2304 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
2305 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
2306 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
2309 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
2310 GOT_ENTRY_SIZE;
2313 return true;
2317 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
2318 #define TARGET_LITTLE_NAME "elf64-x86-64"
2319 #define ELF_ARCH bfd_arch_i386
2320 #define ELF_MACHINE_CODE EM_X86_64
2321 #define ELF_MAXPAGESIZE 0x100000
2323 #define elf_backend_can_gc_sections 1
2324 #define elf_backend_can_refcount 1
2325 #define elf_backend_want_got_plt 1
2326 #define elf_backend_plt_readonly 1
2327 #define elf_backend_want_plt_sym 0
2328 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
2329 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
2330 #define elf_backend_rela_normal 1
2332 #define elf_info_to_howto elf64_x86_64_info_to_howto
2334 #define bfd_elf64_bfd_link_hash_table_create \
2335 elf64_x86_64_link_hash_table_create
2336 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
2338 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
2339 #define elf_backend_check_relocs elf64_x86_64_check_relocs
2340 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
2341 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
2342 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
2343 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
2344 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
2345 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
2346 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
2347 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
2348 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
2349 #define elf_backend_relocate_section elf64_x86_64_relocate_section
2350 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
2351 #define elf_backend_object_p elf64_x86_64_elf_object_p
2353 #include "elf64-target.h"