2001-05-25 H.J. Lu <hjl@gnu.org>
[binutils.git] / bfd / elf64-x86-64.c
bloba92ba78c8005b495dec0b33c5d7685a507c2614d
1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001 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 "libbfd.h"
24 #include "elf-bfd.h"
26 #include "elf/x86-64.h"
28 /* We use only the RELA entries. */
29 #define USE_RELA
31 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
32 #define MINUS_ONE (~ (bfd_vma) 0)
34 /* The relocation "howto" table. Order of fields:
35 type, size, bitsize, pc_relative, complain_on_overflow,
36 special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */
37 static reloc_howto_type x86_64_elf_howto_table[] =
39 HOWTO(R_X86_64_NONE, 0, 0, 0, false, 0, complain_overflow_dont,
40 bfd_elf_generic_reloc, "R_X86_64_NONE", false, 0x00000000, 0x00000000,
41 false),
42 HOWTO(R_X86_64_64, 0, 4, 64, false, 0, complain_overflow_bitfield,
43 bfd_elf_generic_reloc, "R_X86_64_64", false, MINUS_ONE, MINUS_ONE,
44 false),
45 HOWTO(R_X86_64_PC32, 0, 4, 32, true, 0, complain_overflow_signed,
46 bfd_elf_generic_reloc, "R_X86_64_PC32", false, 0xffffffff, 0xffffffff,
47 true),
48 HOWTO(R_X86_64_GOT32, 0, 4, 32, false, 0, complain_overflow_signed,
49 bfd_elf_generic_reloc, "R_X86_64_GOT32", false, 0xffffffff, 0xffffffff,
50 false),
51 HOWTO(R_X86_64_PLT32, 0, 4, 32, true, 0, complain_overflow_signed,
52 bfd_elf_generic_reloc, "R_X86_64_PLT32", false, 0xffffffff, 0xffffffff,
53 true),
54 HOWTO(R_X86_64_COPY, 0, 4, 32, false, 0, complain_overflow_bitfield,
55 bfd_elf_generic_reloc, "R_X86_64_COPY", false, 0xffffffff, 0xffffffff,
56 false),
57 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, false, 0, complain_overflow_bitfield,
58 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", false, MINUS_ONE,
59 MINUS_ONE, false),
60 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, false, 0, complain_overflow_bitfield,
61 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", false, MINUS_ONE,
62 MINUS_ONE, false),
63 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, false, 0, complain_overflow_bitfield,
64 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", false, MINUS_ONE,
65 MINUS_ONE, false),
66 HOWTO(R_X86_64_GOTPCREL, 0, 4, 32, true,0 , complain_overflow_signed,
67 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", false, 0xffffffff,
68 0xffffffff, true),
69 HOWTO(R_X86_64_32, 0, 4, 32, false, 0, complain_overflow_unsigned,
70 bfd_elf_generic_reloc, "R_X86_64_32", false, 0xffffffff, 0xffffffff,
71 false),
72 HOWTO(R_X86_64_32S, 0, 4, 32, false, 0, complain_overflow_signed,
73 bfd_elf_generic_reloc, "R_X86_64_32S", false, 0xffffffff, 0xffffffff,
74 false),
75 HOWTO(R_X86_64_16, 0, 1, 16, false, 0, complain_overflow_bitfield,
76 bfd_elf_generic_reloc, "R_X86_64_16", false, 0xffff, 0xffff, false),
77 HOWTO(R_X86_64_PC16,0, 1, 16, true, 0, complain_overflow_bitfield,
78 bfd_elf_generic_reloc, "R_X86_64_PC16", false, 0xffff, 0xffff, true),
79 HOWTO(R_X86_64_8, 0, 0, 8, false, 0, complain_overflow_signed,
80 bfd_elf_generic_reloc, "R_X86_64_8", false, 0xff, 0xff, false),
81 HOWTO(R_X86_64_PC8, 0, 0, 8, true, 0, complain_overflow_signed,
82 bfd_elf_generic_reloc, "R_X86_64_PC8", false, 0xff, 0xff, true),
84 /* GNU extension to record C++ vtable hierarchy. */
85 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, false, 0, complain_overflow_dont,
86 NULL, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
88 /* GNU extension to record C++ vtable member usage. */
89 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, false, 0, complain_overflow_dont,
90 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", false, 0, 0,
91 false)
94 /* Map BFD relocs to the x86_64 elf relocs. */
95 struct elf_reloc_map
97 bfd_reloc_code_real_type bfd_reloc_val;
98 unsigned char elf_reloc_val;
101 static CONST struct elf_reloc_map x86_64_reloc_map[] =
103 { BFD_RELOC_NONE, R_X86_64_NONE, },
104 { BFD_RELOC_64, R_X86_64_64, },
105 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
106 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
107 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
108 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
109 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
110 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
111 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
112 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
113 { BFD_RELOC_32, R_X86_64_32, },
114 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
115 { BFD_RELOC_16, R_X86_64_16, },
116 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
117 { BFD_RELOC_8, R_X86_64_8, },
118 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
119 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
120 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
123 static reloc_howto_type *elf64_x86_64_reloc_type_lookup
124 PARAMS ((bfd *, bfd_reloc_code_real_type));
125 static void elf64_x86_64_info_to_howto
126 PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
127 static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create
128 PARAMS ((bfd *));
130 static struct bfd_hash_entry *elf64_x86_64_link_hash_newfunc
131 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
132 static boolean elf64_x86_64_adjust_dynamic_symbol
133 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
135 static boolean elf64_x86_64_size_dynamic_sections
136 PARAMS ((bfd *, struct bfd_link_info *));
137 static boolean elf64_x86_64_relocate_section
138 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
139 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
140 static boolean elf64_x86_64_finish_dynamic_symbol
141 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
142 Elf_Internal_Sym *sym));
143 static boolean elf64_x86_64_finish_dynamic_sections
144 PARAMS ((bfd *, struct bfd_link_info *));
146 /* Given a BFD reloc type, return a HOWTO structure. */
147 static reloc_howto_type *
148 elf64_x86_64_reloc_type_lookup (abfd, code)
149 bfd *abfd ATTRIBUTE_UNUSED;
150 bfd_reloc_code_real_type code;
152 unsigned int i;
153 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
154 i++)
156 if (x86_64_reloc_map[i].bfd_reloc_val == code)
157 return &x86_64_elf_howto_table[(int)
158 x86_64_reloc_map[i].elf_reloc_val];
160 return 0;
163 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
165 static void
166 elf64_x86_64_info_to_howto (abfd, cache_ptr, dst)
167 bfd *abfd ATTRIBUTE_UNUSED;
168 arelent *cache_ptr;
169 Elf64_Internal_Rela *dst;
171 unsigned r_type, i;
173 r_type = ELF64_R_TYPE (dst->r_info);
174 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
176 BFD_ASSERT (r_type <= (unsigned int) R_X86_64_PC8);
177 i = r_type;
179 else
181 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
182 i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_PC8 - 1);
184 cache_ptr->howto = &x86_64_elf_howto_table[i];
185 BFD_ASSERT (r_type == cache_ptr->howto->type);
188 /* Functions for the x86-64 ELF linker. */
190 /* The name of the dynamic interpreter. This is put in the .interp
191 section. */
193 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
195 /* The size in bytes of an entry in the global offset table. */
197 #define GOT_ENTRY_SIZE 8
199 /* The size in bytes of an entry in the procedure linkage table. */
201 #define PLT_ENTRY_SIZE 16
203 /* The first entry in a procedure linkage table looks like this. See the
204 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
206 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
208 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
209 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
210 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
213 /* Subsequent entries in a procedure linkage table look like this. */
215 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
217 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
218 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
219 0x68, /* pushq immediate */
220 0, 0, 0, 0, /* replaced with index into relocation table. */
221 0xe9, /* jmp relative */
222 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
225 /* The x86-64 linker needs to keep track of the number of relocs that
226 it decides to copy in check_relocs for each symbol. This is so
227 that it can discard PC relative relocs if it doesn't need them when
228 linking with -Bsymbolic. We store the information in a field
229 extending the regular ELF linker hash table. */
231 /* This structure keeps track of the number of PC relative relocs we
232 have copied for a given symbol. */
234 struct elf64_x86_64_pcrel_relocs_copied
236 /* Next section. */
237 struct elf64_x86_64_pcrel_relocs_copied *next;
238 /* A section in dynobj. */
239 asection *section;
240 /* Number of relocs copied in this section. */
241 bfd_size_type count;
244 /* x86-64 ELF linker hash entry. */
246 struct elf64_x86_64_link_hash_entry
248 struct elf_link_hash_entry root;
250 /* Number of PC relative relocs copied for this symbol. */
251 struct elf64_x86_64_pcrel_relocs_copied *pcrel_relocs_copied;
254 /* x86-64 ELF linker hash table. */
256 struct elf64_x86_64_link_hash_table
258 struct elf_link_hash_table root;
261 /* Declare this now that the above structures are defined. */
263 static boolean elf64_x86_64_discard_copies
264 PARAMS ((struct elf64_x86_64_link_hash_entry *, PTR));
266 /* Traverse an x86-64 ELF linker hash table. */
268 #define elf64_x86_64_link_hash_traverse(table, func, info) \
269 (elf_link_hash_traverse \
270 (&(table)->root, \
271 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
272 (info)))
274 /* Get the x86-64 ELF linker hash table from a link_info structure. */
276 #define elf64_x86_64_hash_table(p) \
277 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
279 /* Create an entry in an x86-64 ELF linker hash table. */
281 static struct bfd_hash_entry *
282 elf64_x86_64_link_hash_newfunc (entry, table, string)
283 struct bfd_hash_entry *entry;
284 struct bfd_hash_table *table;
285 const char *string;
287 struct elf64_x86_64_link_hash_entry *ret =
288 (struct elf64_x86_64_link_hash_entry *) entry;
290 /* Allocate the structure if it has not already been allocated by a
291 subclass. */
292 if (ret == (struct elf64_x86_64_link_hash_entry *) NULL)
293 ret = ((struct elf64_x86_64_link_hash_entry *)
294 bfd_hash_allocate (table,
295 sizeof (struct elf64_x86_64_link_hash_entry)));
296 if (ret == (struct elf64_x86_64_link_hash_entry *) NULL)
297 return (struct bfd_hash_entry *) ret;
299 /* Call the allocation method of the superclass. */
300 ret = ((struct elf64_x86_64_link_hash_entry *)
301 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
302 table, string));
303 if (ret != (struct elf64_x86_64_link_hash_entry *) NULL)
305 ret->pcrel_relocs_copied = NULL;
308 return (struct bfd_hash_entry *) ret;
311 /* Create an X86-64 ELF linker hash table. */
313 static struct bfd_link_hash_table *
314 elf64_x86_64_link_hash_table_create (abfd)
315 bfd *abfd;
317 struct elf64_x86_64_link_hash_table *ret;
319 ret = ((struct elf64_x86_64_link_hash_table *)
320 bfd_alloc (abfd, sizeof (struct elf64_x86_64_link_hash_table)));
321 if (ret == (struct elf64_x86_64_link_hash_table *) NULL)
322 return NULL;
324 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
325 elf64_x86_64_link_hash_newfunc))
327 bfd_release (abfd, ret);
328 return NULL;
331 return &ret->root.root;
334 boolean
335 elf64_x86_64_elf_object_p (abfd)
336 bfd *abfd;
338 /* Set the right machine number for an x86-64 elf64 file. */
339 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
340 return true;
343 /* Look through the relocs for a section during the first phase, and
344 allocate space in the global offset table or procedure linkage
345 table. */
347 static boolean
348 elf64_x86_64_check_relocs (abfd, info, sec, relocs)
349 bfd *abfd;
350 struct bfd_link_info *info;
351 asection *sec;
352 const Elf_Internal_Rela *relocs;
354 bfd *dynobj;
355 Elf_Internal_Shdr *symtab_hdr;
356 struct elf_link_hash_entry **sym_hashes;
357 bfd_signed_vma *local_got_refcounts;
358 const Elf_Internal_Rela *rel;
359 const Elf_Internal_Rela *rel_end;
360 asection *sgot;
361 asection *srelgot;
362 asection *sreloc;
364 if (info->relocateable)
365 return true;
367 dynobj = elf_hash_table (info)->dynobj;
368 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
369 sym_hashes = elf_sym_hashes (abfd);
370 local_got_refcounts = elf_local_got_refcounts (abfd);
372 sgot = srelgot = sreloc = NULL;
373 rel_end = relocs + sec->reloc_count;
374 for (rel = relocs; rel < rel_end; rel++)
376 unsigned long r_symndx;
377 struct elf_link_hash_entry *h;
379 r_symndx = ELF64_R_SYM (rel->r_info);
380 if (r_symndx < symtab_hdr->sh_info)
381 h = NULL;
382 else
383 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
385 /* Some relocs require a global offset table. */
386 if (dynobj == NULL)
388 switch (ELF64_R_TYPE (rel->r_info))
390 case R_X86_64_GOT32:
391 case R_X86_64_GOTPCREL:
392 elf_hash_table (info)->dynobj = dynobj = abfd;
393 if (! _bfd_elf_create_got_section (dynobj, info))
394 return false;
395 break;
399 switch (ELF64_R_TYPE (rel->r_info))
401 case R_X86_64_GOTPCREL:
402 case R_X86_64_GOT32:
403 /* This symbol requires a global offset table entry. */
405 if (sgot == NULL)
407 sgot = bfd_get_section_by_name (dynobj, ".got");
408 BFD_ASSERT (sgot != NULL);
411 if (srelgot == NULL && (h != NULL || info->shared))
413 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
414 if (srelgot == NULL)
416 srelgot = bfd_make_section (dynobj, ".rela.got");
417 if (srelgot == NULL
418 || ! bfd_set_section_flags (dynobj, srelgot,
419 (SEC_ALLOC
420 | SEC_LOAD
421 | SEC_HAS_CONTENTS
422 | SEC_IN_MEMORY
423 | SEC_LINKER_CREATED
424 | SEC_READONLY))
425 || ! bfd_set_section_alignment (dynobj, srelgot, 3))
426 return false;
430 if (h != NULL)
432 if (h->got.refcount == -1)
434 h->got.refcount = 1;
436 /* Make sure this symbol is output as a dynamic symbol. */
437 if (h->dynindx == -1)
439 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
440 return false;
443 sgot->_raw_size += GOT_ENTRY_SIZE;
444 srelgot->_raw_size += sizeof (Elf64_External_Rela);
446 else
447 h->got.refcount += 1;
449 else
451 /* This is a global offset table entry for a local symbol. */
452 if (local_got_refcounts == NULL)
454 size_t size;
456 size = symtab_hdr->sh_info * sizeof (bfd_signed_vma);
457 local_got_refcounts = ((bfd_signed_vma *)
458 bfd_alloc (abfd, size));
459 if (local_got_refcounts == NULL)
460 return false;
461 elf_local_got_refcounts (abfd) = local_got_refcounts;
462 memset (local_got_refcounts, -1, size);
464 if (local_got_refcounts[r_symndx] == -1)
466 local_got_refcounts[r_symndx] = 1;
468 sgot->_raw_size += GOT_ENTRY_SIZE;
469 if (info->shared)
471 /* If we are generating a shared object, we need to
472 output a R_X86_64_RELATIVE reloc so that the dynamic
473 linker can adjust this GOT entry. */
474 srelgot->_raw_size += sizeof (Elf64_External_Rela);
477 else
478 local_got_refcounts[r_symndx] += 1;
480 break;
482 case R_X86_64_PLT32:
483 /* This symbol requires a procedure linkage table entry. We
484 actually build the entry in adjust_dynamic_symbol,
485 because this might be a case of linking PIC code which is
486 never referenced by a dynamic object, in which case we
487 don't need to generate a procedure linkage table entry
488 after all. */
490 /* If this is a local symbol, we resolve it directly without
491 creating a procedure linkage table entry. */
492 if (h == NULL)
493 continue;
495 if (h->plt.refcount == -1)
497 h->plt.refcount = 1;
498 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
500 else
501 h->plt.refcount += 1;
502 break;
504 case R_X86_64_32:
505 case R_X86_64_32S:
506 case R_X86_64_PC32:
507 if (h != NULL)
508 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
510 /* If we are creating a shared library, and this is a reloc
511 against a global symbol, or a non PC relative reloc
512 against a local symbol, then we need to copy the reloc
513 into the shared library. However, if we are linking with
514 -Bsymbolic, we do not need to copy a reloc against a
515 global symbol which is defined in an object we are
516 including in the link (i.e., DEF_REGULAR is set). At
517 this point we have not seen all the input files, so it is
518 possible that DEF_REGULAR is not set now but will be set
519 later (it is never cleared). We account for that
520 possibility below by storing information in the
521 pcrel_relocs_copied field of the hash table entry.
522 A similar situation occurs when creating shared libraries
523 and symbol visibility changes render the symbol local. */
524 if (info->shared
525 && (sec->flags & SEC_ALLOC) != 0
526 && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32
527 || (h != NULL
528 && (! info->symbolic
529 || (h->elf_link_hash_flags
530 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
532 /* When creating a shared object, we must copy these
533 reloc types into the output file. We create a reloc
534 section in dynobj and make room for this reloc. */
535 if (sreloc == NULL)
537 const char *name;
539 name = (bfd_elf_string_from_elf_section
540 (abfd,
541 elf_elfheader (abfd)->e_shstrndx,
542 elf_section_data (sec)->rel_hdr.sh_name));
543 if (name == NULL)
544 return false;
546 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
547 && strcmp (bfd_get_section_name (abfd, sec),
548 name + 5) == 0);
550 sreloc = bfd_get_section_by_name (dynobj, name);
551 if (sreloc == NULL)
553 flagword flags;
555 sreloc = bfd_make_section (dynobj, name);
556 flags = (SEC_HAS_CONTENTS | SEC_READONLY
557 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
558 if ((sec->flags & SEC_ALLOC) != 0)
559 flags |= SEC_ALLOC | SEC_LOAD;
560 if (sreloc == NULL
561 || ! bfd_set_section_flags (dynobj, sreloc, flags)
562 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
563 return false;
567 sreloc->_raw_size += sizeof (Elf64_External_Rela);
569 /* If this is a global symbol, we count the number of PC
570 relative relocations we have entered for this symbol,
571 so that we can discard them later as necessary. Note
572 that this function is only called if we are using an
573 elf64_x86_64 linker hash table, which means that h is
574 really a pointer to an elf64_x86_64_link_hash_entry. */
575 if (h != NULL && ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)
577 struct elf64_x86_64_link_hash_entry *eh;
578 struct elf64_x86_64_pcrel_relocs_copied *p;
580 eh = (struct elf64_x86_64_link_hash_entry *) h;
582 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
583 if (p->section == sreloc)
584 break;
586 if (p == NULL)
588 p = ((struct elf64_x86_64_pcrel_relocs_copied *)
589 bfd_alloc (dynobj, sizeof *p));
590 if (p == NULL)
591 return false;
592 p->next = eh->pcrel_relocs_copied;
593 eh->pcrel_relocs_copied = p;
594 p->section = sreloc;
595 p->count = 0;
598 ++p->count;
601 break;
603 /* This relocation describes the C++ object vtable hierarchy.
604 Reconstruct it for later use during GC. */
605 case R_X86_64_GNU_VTINHERIT:
606 if (!_bfd_elf64_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
607 return false;
608 break;
610 /* This relocation describes which C++ vtable entries are actually
611 used. Record for later use during GC. */
612 case R_X86_64_GNU_VTENTRY:
613 if (!_bfd_elf64_gc_record_vtentry (abfd, sec, h, rel->r_offset))
614 return false;
615 break;
619 return true;
622 /* Return the section that should be marked against GC for a given
623 relocation. */
625 static asection *
626 elf64_x86_64_gc_mark_hook (abfd, info, rel, h, sym)
627 bfd *abfd;
628 struct bfd_link_info *info ATTRIBUTE_UNUSED;
629 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED;
630 struct elf_link_hash_entry *h;
631 Elf_Internal_Sym *sym;
633 if (h != NULL)
635 switch (ELF64_R_TYPE (rel->r_info))
637 case R_X86_64_GNU_VTINHERIT:
638 case R_X86_64_GNU_VTENTRY:
639 break;
641 default:
642 switch (h->root.type)
644 case bfd_link_hash_defined:
645 case bfd_link_hash_defweak:
646 return h->root.u.def.section;
648 case bfd_link_hash_common:
649 return h->root.u.c.p->section;
651 default:
652 break;
656 else
658 if (!(elf_bad_symtab (abfd)
659 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
660 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
661 && sym->st_shndx != SHN_COMMON))
663 return bfd_section_from_elf_index (abfd, sym->st_shndx);
667 return NULL;
670 /* Update the got entry reference counts for the section being removed. */
672 static boolean
673 elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs)
674 bfd *abfd;
675 struct bfd_link_info *info ATTRIBUTE_UNUSED;
676 asection *sec;
677 const Elf_Internal_Rela *relocs;
679 Elf_Internal_Shdr *symtab_hdr;
680 struct elf_link_hash_entry **sym_hashes;
681 bfd_signed_vma *local_got_refcounts;
682 const Elf_Internal_Rela *rel, *relend;
683 unsigned long r_symndx;
684 struct elf_link_hash_entry *h;
685 bfd *dynobj;
686 asection *sgot;
687 asection *srelgot;
689 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
690 sym_hashes = elf_sym_hashes (abfd);
691 local_got_refcounts = elf_local_got_refcounts (abfd);
693 dynobj = elf_hash_table (info)->dynobj;
694 if (dynobj == NULL)
695 return true;
697 sgot = bfd_get_section_by_name (dynobj, ".got");
698 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
700 relend = relocs + sec->reloc_count;
701 for (rel = relocs; rel < relend; rel++)
702 switch (ELF64_R_TYPE (rel->r_info))
704 case R_X86_64_GOT32:
705 case R_X86_64_GOTPCREL:
706 r_symndx = ELF64_R_SYM (rel->r_info);
707 if (r_symndx >= symtab_hdr->sh_info)
709 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
710 if (h->got.refcount > 0)
712 h->got.refcount -= 1;
713 if (h->got.refcount == 0)
715 sgot->_raw_size -= GOT_ENTRY_SIZE;
716 srelgot->_raw_size -= sizeof (Elf64_External_Rela);
720 else if (local_got_refcounts != NULL)
722 if (local_got_refcounts[r_symndx] > 0)
724 local_got_refcounts[r_symndx] -= 1;
725 if (local_got_refcounts[r_symndx] == 0)
727 sgot->_raw_size -= GOT_ENTRY_SIZE;
728 if (info->shared)
729 srelgot->_raw_size -= sizeof (Elf64_External_Rela);
733 break;
735 case R_X86_64_PLT32:
736 r_symndx = ELF64_R_SYM (rel->r_info);
737 if (r_symndx >= symtab_hdr->sh_info)
739 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
740 if (h->plt.refcount > 0)
741 h->plt.refcount -= 1;
743 break;
745 default:
746 break;
749 return true;
752 /* Adjust a symbol defined by a dynamic object and referenced by a
753 regular object. The current definition is in some section of the
754 dynamic object, but we're not including those sections. We have to
755 change the definition to something the rest of the link can
756 understand. */
758 static boolean
759 elf64_x86_64_adjust_dynamic_symbol (info, h)
760 struct bfd_link_info *info;
761 struct elf_link_hash_entry *h;
763 bfd *dynobj;
764 asection *s;
765 unsigned int power_of_two;
767 dynobj = elf_hash_table (info)->dynobj;
769 /* Make sure we know what is going on here. */
770 BFD_ASSERT (dynobj != NULL
771 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
772 || h->weakdef != NULL
773 || ((h->elf_link_hash_flags
774 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
775 && (h->elf_link_hash_flags
776 & ELF_LINK_HASH_REF_REGULAR) != 0
777 && (h->elf_link_hash_flags
778 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
780 /* If this is a function, put it in the procedure linkage table. We
781 will fill in the contents of the procedure linkage table later,
782 when we know the address of the .got section. */
783 if (h->type == STT_FUNC
784 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
786 if ((! info->shared
787 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0
788 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0)
789 || (info->shared && h->plt.refcount <= 0))
791 /* This case can occur if we saw a PLT32 reloc in an input
792 file, but the symbol was never referred to by a dynamic
793 object, or if all references were garbage collected. In
794 such a case, we don't actually need to build a procedure
795 linkage table, and we can just do a PC32 reloc instead. */
796 h->plt.offset = (bfd_vma) -1;
797 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
798 return true;
801 /* Make sure this symbol is output as a dynamic symbol. */
802 if (h->dynindx == -1)
804 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
805 return false;
808 s = bfd_get_section_by_name (dynobj, ".plt");
809 BFD_ASSERT (s != NULL);
811 /* If this is the first .plt entry, make room for the special
812 first entry. */
813 if (s->_raw_size == 0)
814 s->_raw_size = PLT_ENTRY_SIZE;
816 /* If this symbol is not defined in a regular file, and we are
817 not generating a shared library, then set the symbol to this
818 location in the .plt. This is required to make function
819 pointers compare as equal between the normal executable and
820 the shared library. */
821 if (! info->shared
822 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
824 h->root.u.def.section = s;
825 h->root.u.def.value = s->_raw_size;
828 h->plt.offset = s->_raw_size;
830 /* Make room for this entry. */
831 s->_raw_size += PLT_ENTRY_SIZE;
833 /* We also need to make an entry in the .got.plt section, which
834 will be placed in the .got section by the linker script. */
835 s = bfd_get_section_by_name (dynobj, ".got.plt");
836 BFD_ASSERT (s != NULL);
837 s->_raw_size += GOT_ENTRY_SIZE;
839 /* We also need to make an entry in the .rela.plt section. */
840 s = bfd_get_section_by_name (dynobj, ".rela.plt");
841 BFD_ASSERT (s != NULL);
842 s->_raw_size += sizeof (Elf64_External_Rela);
844 return true;
847 /* If this is a weak symbol, and there is a real definition, the
848 processor independent code will have arranged for us to see the
849 real definition first, and we can just use the same value. */
850 if (h->weakdef != NULL)
852 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
853 || h->weakdef->root.type == bfd_link_hash_defweak);
854 h->root.u.def.section = h->weakdef->root.u.def.section;
855 h->root.u.def.value = h->weakdef->root.u.def.value;
856 return true;
859 /* This is a reference to a symbol defined by a dynamic object which
860 is not a function. */
862 /* If we are creating a shared library, we must presume that the
863 only references to the symbol are via the global offset table.
864 For such cases we need not do anything here; the relocations will
865 be handled correctly by relocate_section. */
866 if (info->shared)
867 return true;
869 /* If there are no references to this symbol that do not use the
870 GOT, we don't need to generate a copy reloc. */
871 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
872 return true;
874 /* We must allocate the symbol in our .dynbss section, which will
875 become part of the .bss section of the executable. There will be
876 an entry for this symbol in the .dynsym section. The dynamic
877 object will contain position independent code, so all references
878 from the dynamic object to this symbol will go through the global
879 offset table. The dynamic linker will use the .dynsym entry to
880 determine the address it must put in the global offset table, so
881 both the dynamic object and the regular object will refer to the
882 same memory location for the variable. */
884 s = bfd_get_section_by_name (dynobj, ".dynbss");
885 BFD_ASSERT (s != NULL);
887 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
888 to copy the initial value out of the dynamic object and into the
889 runtime process image. We need to remember the offset into the
890 .rela.bss section we are going to use. */
891 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
893 asection *srel;
895 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
896 BFD_ASSERT (srel != NULL);
897 srel->_raw_size += sizeof (Elf64_External_Rela);
898 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
901 /* We need to figure out the alignment required for this symbol. I
902 have no idea how ELF linkers handle this. 16-bytes is the size
903 of the largest type that requires hard alignment -- long double. */
904 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
905 this construct. */
906 power_of_two = bfd_log2 (h->size);
907 if (power_of_two > 4)
908 power_of_two = 4;
910 /* Apply the required alignment. */
911 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
912 if (power_of_two > bfd_get_section_alignment (dynobj, s))
914 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
915 return false;
918 /* Define the symbol as being at this point in the section. */
919 h->root.u.def.section = s;
920 h->root.u.def.value = s->_raw_size;
922 /* Increment the section size to make room for the symbol. */
923 s->_raw_size += h->size;
925 return true;
928 /* Set the sizes of the dynamic sections. */
930 static boolean
931 elf64_x86_64_size_dynamic_sections (output_bfd, info)
932 bfd *output_bfd;
933 struct bfd_link_info *info;
935 bfd *dynobj;
936 asection *s;
937 boolean plt;
938 boolean relocs;
939 boolean reltext;
941 dynobj = elf_hash_table (info)->dynobj;
942 BFD_ASSERT (dynobj != NULL);
944 if (elf_hash_table (info)->dynamic_sections_created)
946 /* Set the contents of the .interp section to the interpreter. */
947 if (! info->shared)
949 s = bfd_get_section_by_name (dynobj, ".interp");
950 BFD_ASSERT (s != NULL);
951 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
952 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
955 else
957 /* We may have created entries in the .rela.got section.
958 However, if we are not creating the dynamic sections, we will
959 not actually use these entries. Reset the size of .rela.got,
960 which will cause it to get stripped from the output file
961 below. */
962 s = bfd_get_section_by_name (dynobj, ".rela.got");
963 if (s != NULL)
964 s->_raw_size = 0;
967 /* If this is a -Bsymbolic shared link, then we need to discard all
968 PC relative relocs against symbols defined in a regular object.
969 We allocated space for them in the check_relocs routine, but we
970 will not fill them in in the relocate_section routine. */
971 if (info->shared)
972 elf64_x86_64_link_hash_traverse (elf64_x86_64_hash_table (info),
973 elf64_x86_64_discard_copies,
974 (PTR) info);
976 /* The check_relocs and adjust_dynamic_symbol entry points have
977 determined the sizes of the various dynamic sections. Allocate
978 memory for them. */
979 plt = relocs = reltext = false;
980 for (s = dynobj->sections; s != NULL; s = s->next)
982 const char *name;
983 boolean strip;
985 if ((s->flags & SEC_LINKER_CREATED) == 0)
986 continue;
988 /* It's OK to base decisions on the section name, because none
989 of the dynobj section names depend upon the input files. */
990 name = bfd_get_section_name (dynobj, s);
992 strip = false;
993 if (strcmp (name, ".plt") == 0)
995 if (s->_raw_size == 0)
997 /* Strip this section if we don't need it; see the
998 comment below. */
999 strip = true;
1001 else
1003 /* Remember whether there is a PLT. */
1004 plt = true;
1007 else if (strncmp (name, ".rela", 5) == 0)
1009 if (s->_raw_size == 0)
1011 /* If we don't need this section, strip it from the
1012 output file. This is mostly to handle .rela.bss and
1013 .rela.plt. We must create both sections in
1014 create_dynamic_sections, because they must be created
1015 before the linker maps input sections to output
1016 sections. The linker does that before
1017 adjust_dynamic_symbol is called, and it is that
1018 function which decides whether anything needs to go
1019 into these sections. */
1020 strip = true;
1022 else
1024 asection *target;
1026 /* Remember whether there are any reloc sections other
1027 than .rela.plt. */
1028 if (strcmp (name, ".rela.plt") != 0)
1030 const char *outname;
1032 relocs = true;
1034 /* If this relocation section applies to a read only
1035 section, then we probably need a DT_TEXTREL
1036 entry. The entries in the .rela.plt section
1037 really apply to the .got section, which we
1038 created ourselves and so know is not readonly. */
1039 outname = bfd_get_section_name (output_bfd,
1040 s->output_section);
1041 target = bfd_get_section_by_name (output_bfd, outname + 5);
1042 if (target != NULL
1043 && (target->flags & SEC_READONLY) != 0
1044 && (target->flags & SEC_ALLOC) != 0)
1045 reltext = true;
1048 /* We use the reloc_count field as a counter if we need
1049 to copy relocs into the output file. */
1050 s->reloc_count = 0;
1053 else if (strncmp (name, ".got", 4) != 0)
1055 /* It's not one of our sections, so don't allocate space. */
1056 continue;
1059 if (strip)
1061 _bfd_strip_section_from_output (info, s);
1062 continue;
1065 /* Allocate memory for the section contents. We use bfd_zalloc
1066 here in case unused entries are not reclaimed before the
1067 section's contents are written out. This should not happen,
1068 but this way if it does, we get a R_X86_64_NONE reloc instead
1069 of garbage. */
1070 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
1071 if (s->contents == NULL && s->_raw_size != 0)
1072 return false;
1075 if (elf_hash_table (info)->dynamic_sections_created)
1077 /* Add some entries to the .dynamic section. We fill in the
1078 values later, in elf64_x86_64_finish_dynamic_sections, but we
1079 must add the entries now so that we get the correct size for
1080 the .dynamic section. The DT_DEBUG entry is filled in by the
1081 dynamic linker and used by the debugger. */
1082 if (! info->shared)
1084 if (! bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
1085 return false;
1088 if (plt)
1090 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0)
1091 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1092 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
1093 || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
1094 return false;
1097 if (relocs)
1099 if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
1100 || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
1101 || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
1102 sizeof (Elf64_External_Rela)))
1103 return false;
1106 if (reltext)
1108 if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
1109 return false;
1110 info->flags |= DF_TEXTREL;
1114 return true;
1117 /* This function is called via elf64_x86_64_link_hash_traverse if we are
1118 creating a shared object. In the -Bsymbolic case, it discards the
1119 space allocated to copy PC relative relocs against symbols which
1120 are defined in regular objects. For the normal non-symbolic case,
1121 we also discard space for relocs that have become local due to
1122 symbol visibility changes. We allocated space for them in the
1123 check_relocs routine, but we won't fill them in in the
1124 relocate_section routine. */
1126 static boolean
1127 elf64_x86_64_discard_copies (h, inf)
1128 struct elf64_x86_64_link_hash_entry *h;
1129 PTR inf;
1131 struct elf64_x86_64_pcrel_relocs_copied *s;
1132 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1134 /* If a symbol has been forced local or we have found a regular
1135 definition for the symbolic link case, then we won't be needing
1136 any relocs. */
1137 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1138 && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1139 || info->symbolic))
1141 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1142 s->section->_raw_size -= s->count * sizeof (Elf64_External_Rela);
1145 return true;
1148 /* Relocate an x86_64 ELF section. */
1150 static boolean
1151 elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
1152 contents, relocs, local_syms, local_sections)
1153 bfd *output_bfd;
1154 struct bfd_link_info *info;
1155 bfd *input_bfd;
1156 asection *input_section;
1157 bfd_byte *contents;
1158 Elf_Internal_Rela *relocs;
1159 Elf_Internal_Sym *local_syms;
1160 asection **local_sections;
1162 bfd *dynobj;
1163 Elf_Internal_Shdr *symtab_hdr;
1164 struct elf_link_hash_entry **sym_hashes;
1165 bfd_vma *local_got_offsets;
1166 asection *sgot;
1167 asection *splt;
1168 asection *sreloc;
1169 Elf_Internal_Rela *rela;
1170 Elf_Internal_Rela *relend;
1172 dynobj = elf_hash_table (info)->dynobj;
1173 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1174 sym_hashes = elf_sym_hashes (input_bfd);
1175 local_got_offsets = elf_local_got_offsets (input_bfd);
1177 sreloc = splt = sgot = NULL;
1178 if (dynobj != NULL)
1180 splt = bfd_get_section_by_name (dynobj, ".plt");
1181 sgot = bfd_get_section_by_name (dynobj, ".got");
1184 rela = relocs;
1185 relend = relocs + input_section->reloc_count;
1186 for (; rela < relend; rela++)
1188 int r_type;
1189 reloc_howto_type *howto;
1190 unsigned long r_symndx;
1191 struct elf_link_hash_entry *h;
1192 Elf_Internal_Sym *sym;
1193 asection *sec;
1194 bfd_vma relocation;
1195 bfd_reloc_status_type r;
1196 unsigned int indx;
1198 r_type = ELF64_R_TYPE (rela->r_info);
1199 if (r_type == (int) R_X86_64_GNU_VTINHERIT
1200 || r_type == (int) R_X86_64_GNU_VTENTRY)
1201 continue;
1203 if ((indx = (unsigned) r_type) >= R_X86_64_max)
1205 bfd_set_error (bfd_error_bad_value);
1206 return false;
1208 howto = x86_64_elf_howto_table + indx;
1210 r_symndx = ELF64_R_SYM (rela->r_info);
1212 if (info->relocateable)
1214 /* This is a relocateable link. We don't have to change
1215 anything, unless the reloc is against a section symbol,
1216 in which case we have to adjust according to where the
1217 section symbol winds up in the output section. */
1218 if (r_symndx < symtab_hdr->sh_info)
1220 sym = local_syms + r_symndx;
1221 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1223 sec = local_sections[r_symndx];
1224 rela->r_addend += sec->output_offset + sym->st_value;
1228 continue;
1231 /* This is a final link. */
1232 h = NULL;
1233 sym = NULL;
1234 sec = NULL;
1235 if (r_symndx < symtab_hdr->sh_info)
1237 sym = local_syms + r_symndx;
1238 sec = local_sections[r_symndx];
1239 relocation = (sec->output_section->vma
1240 + sec->output_offset
1241 + sym->st_value);
1243 else
1245 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1246 while (h->root.type == bfd_link_hash_indirect
1247 || h->root.type == bfd_link_hash_warning)
1248 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1249 if (h->root.type == bfd_link_hash_defined
1250 || h->root.type == bfd_link_hash_defweak)
1252 sec = h->root.u.def.section;
1253 if ((r_type == R_X86_64_PLT32
1254 && splt != NULL
1255 && h->plt.offset != (bfd_vma) -1)
1256 || ((r_type == R_X86_64_GOT32 || r_type == R_X86_64_GOTPCREL)
1257 && elf_hash_table (info)->dynamic_sections_created
1258 && (!info->shared
1259 || (! info->symbolic && h->dynindx != -1)
1260 || (h->elf_link_hash_flags
1261 & ELF_LINK_HASH_DEF_REGULAR) == 0))
1262 || (info->shared
1263 && ((! info->symbolic && h->dynindx != -1)
1264 || (h->elf_link_hash_flags
1265 & ELF_LINK_HASH_DEF_REGULAR) == 0)
1266 && ( r_type == R_X86_64_8 ||
1267 r_type == R_X86_64_16 ||
1268 r_type == R_X86_64_32 ||
1269 r_type == R_X86_64_64 ||
1270 r_type == R_X86_64_PC16 ||
1271 r_type == R_X86_64_PC32)
1272 && ((input_section->flags & SEC_ALLOC) != 0
1273 /* DWARF will emit R_X86_64_32 relocations in its
1274 sections against symbols defined externally
1275 in shared libraries. We can't do anything
1276 with them here. */
1277 || ((input_section->flags & SEC_DEBUGGING) != 0
1278 && (h->elf_link_hash_flags
1279 & ELF_LINK_HASH_DEF_DYNAMIC) != 0))))
1281 /* In these cases, we don't need the relocation
1282 value. We check specially because in some
1283 obscure cases sec->output_section will be NULL. */
1284 relocation = 0;
1286 else if (sec->output_section == NULL)
1288 (*_bfd_error_handler)
1289 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1290 bfd_get_filename (input_bfd), h->root.root.string,
1291 bfd_get_section_name (input_bfd, input_section));
1292 relocation = 0;
1294 else
1295 relocation = (h->root.u.def.value
1296 + sec->output_section->vma
1297 + sec->output_offset);
1299 else if (h->root.type == bfd_link_hash_undefweak)
1300 relocation = 0;
1301 else if (info->shared && !info->symbolic && !info->no_undefined
1302 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1303 relocation = 0;
1304 else
1306 if (! ((*info->callbacks->undefined_symbol)
1307 (info, h->root.root.string, input_bfd,
1308 input_section, rela->r_offset,
1309 (!info->shared || info->no_undefined
1310 || ELF_ST_VISIBILITY (h->other)))))
1311 return false;
1312 relocation = 0;
1316 /* When generating a shared object, the relocations handled here are
1317 copied into the output file to be resolved at run time. */
1318 switch (r_type)
1320 case R_X86_64_GOT32:
1321 /* Relocation is to the entry for this symbol in the global
1322 offset table. */
1323 case R_X86_64_GOTPCREL:
1324 /* Use global offset table as symbol value. */
1325 BFD_ASSERT (sgot != NULL);
1327 if (h != NULL)
1329 bfd_vma off = h->got.offset;
1330 BFD_ASSERT (off != (bfd_vma) -1);
1332 if (! elf_hash_table (info)->dynamic_sections_created
1333 || (info->shared
1334 && (info->symbolic || h->dynindx == -1)
1335 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1337 /* This is actually a static link, or it is a -Bsymbolic
1338 link and the symbol is defined locally, or the symbol
1339 was forced to be local because of a version file. We
1340 must initialize this entry in the global offset table.
1341 Since the offset must always be a multiple of 8, we
1342 use the least significant bit to record whether we
1343 have initialized it already.
1345 When doing a dynamic link, we create a .rela.got
1346 relocation entry to initialize the value. This is
1347 done in the finish_dynamic_symbol routine. */
1348 if ((off & 1) != 0)
1349 off &= ~1;
1350 else
1352 bfd_put_64 (output_bfd, relocation,
1353 sgot->contents + off);
1354 h->got.offset |= 1;
1357 if (r_type == R_X86_64_GOTPCREL)
1358 relocation = sgot->output_section->vma + sgot->output_offset + off;
1359 else
1360 relocation = sgot->output_offset + off;
1362 else
1364 bfd_vma off;
1366 BFD_ASSERT (local_got_offsets != NULL
1367 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1369 off = local_got_offsets[r_symndx];
1371 /* The offset must always be a multiple of 8. We use
1372 the least significant bit to record whether we have
1373 already generated the necessary reloc. */
1374 if ((off & 1) != 0)
1375 off &= ~1;
1376 else
1378 bfd_put_64 (output_bfd, relocation, sgot->contents + off);
1380 if (info->shared)
1382 asection *srelgot;
1383 Elf_Internal_Rela outrel;
1385 /* We need to generate a R_X86_64_RELATIVE reloc
1386 for the dynamic linker. */
1387 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1388 BFD_ASSERT (srelgot != NULL);
1390 outrel.r_offset = (sgot->output_section->vma
1391 + sgot->output_offset
1392 + off);
1393 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1394 outrel.r_addend = relocation;
1395 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1396 (((Elf64_External_Rela *)
1397 srelgot->contents)
1398 + srelgot->reloc_count));
1399 ++srelgot->reloc_count;
1402 local_got_offsets[r_symndx] |= 1;
1405 if (r_type == R_X86_64_GOTPCREL)
1406 relocation = sgot->output_section->vma + sgot->output_offset + off;
1407 else
1408 relocation = sgot->output_offset + off;
1411 break;
1413 case R_X86_64_PLT32:
1414 /* Relocation is to the entry for this symbol in the
1415 procedure linkage table. */
1417 /* Resolve a PLT32 reloc against a local symbol directly,
1418 without using the procedure linkage table. */
1419 if (h == NULL)
1420 break;
1422 if (h->plt.offset == (bfd_vma) -1 || splt == NULL)
1424 /* We didn't make a PLT entry for this symbol. This
1425 happens when statically linking PIC code, or when
1426 using -Bsymbolic. */
1427 break;
1430 relocation = (splt->output_section->vma
1431 + splt->output_offset
1432 + h->plt.offset);
1433 break;
1435 case R_X86_64_8:
1436 case R_X86_64_16:
1437 case R_X86_64_32:
1438 case R_X86_64_PC8:
1439 case R_X86_64_PC16:
1440 case R_X86_64_PC32:
1441 /* FIXME: The abi says the linker should make sure the value is
1442 the same when it's zeroextended to 64 bit. */
1443 if (info->shared
1444 && (input_section->flags & SEC_ALLOC) != 0
1445 && ((r_type != R_X86_64_PC8 && r_type != R_X86_64_PC16
1446 && r_type != R_X86_64_PC32)
1447 || (h != NULL
1448 && h->dynindx != -1
1449 && (! info->symbolic
1450 || (h->elf_link_hash_flags
1451 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1453 Elf_Internal_Rela outrel;
1454 boolean skip, relocate;
1456 /* When generating a shared object, these relocations
1457 are copied into the output file to be resolved at run
1458 time. */
1460 if (sreloc == NULL)
1462 const char *name;
1464 name = (bfd_elf_string_from_elf_section
1465 (input_bfd,
1466 elf_elfheader (input_bfd)->e_shstrndx,
1467 elf_section_data (input_section)->rel_hdr.sh_name));
1468 if (name == NULL)
1469 return false;
1471 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1472 && strcmp (bfd_get_section_name (input_bfd,
1473 input_section),
1474 name + 5) == 0);
1476 sreloc = bfd_get_section_by_name (dynobj, name);
1477 BFD_ASSERT (sreloc != NULL);
1480 skip = false;
1482 if (elf_section_data (input_section)->stab_info == NULL)
1483 outrel.r_offset = rela->r_offset;
1484 else
1486 bfd_vma off;
1488 off = (_bfd_stab_section_offset
1489 (output_bfd, &elf_hash_table (info)->stab_info,
1490 input_section,
1491 &elf_section_data (input_section)->stab_info,
1492 rela->r_offset));
1493 if (off == (bfd_vma) -1)
1494 skip = true;
1495 outrel.r_offset = off;
1498 outrel.r_offset += (input_section->output_section->vma
1499 + input_section->output_offset);
1501 if (skip)
1503 memset (&outrel, 0, sizeof outrel);
1504 relocate = false;
1506 else if ((r_type == R_X86_64_PC8) || (r_type == R_X86_64_PC16)
1507 || (r_type == R_X86_64_PC32))
1509 BFD_ASSERT (h != NULL && h->dynindx != -1);
1510 relocate = false;
1511 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
1512 outrel.r_addend = relocation + rela->r_addend;
1514 else
1516 /* h->dynindx may be -1 if this symbol was marked to
1517 become local. */
1518 if (h == NULL
1519 || ((info->symbolic || h->dynindx == -1)
1520 && (h->elf_link_hash_flags
1521 & ELF_LINK_HASH_DEF_REGULAR) != 0))
1523 relocate = true;
1524 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1525 outrel.r_addend = relocation + rela->r_addend;
1527 else
1529 BFD_ASSERT (h->dynindx != -1);
1530 relocate = false;
1531 outrel.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_32);
1532 outrel.r_addend = relocation + rela->r_addend;
1536 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1537 (((Elf64_External_Rela *)
1538 sreloc->contents)
1539 + sreloc->reloc_count));
1540 ++sreloc->reloc_count;
1542 /* If this reloc is against an external symbol, we do
1543 not want to fiddle with the addend. Otherwise, we
1544 need to include the symbol value so that it becomes
1545 an addend for the dynamic reloc. */
1546 if (! relocate)
1547 continue;
1550 break;
1552 default:
1553 break;
1556 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1557 contents, rela->r_offset,
1558 relocation, rela->r_addend);
1560 if (r != bfd_reloc_ok)
1562 switch (r)
1564 default:
1565 case bfd_reloc_outofrange:
1566 abort ();
1567 case bfd_reloc_overflow:
1569 const char *name;
1571 if (h != NULL)
1572 name = h->root.root.string;
1573 else
1575 name = bfd_elf_string_from_elf_section (input_bfd,
1576 symtab_hdr->sh_link,
1577 sym->st_name);
1578 if (name == NULL)
1579 return false;
1580 if (*name == '\0')
1581 name = bfd_section_name (input_bfd, sec);
1583 if (! ((*info->callbacks->reloc_overflow)
1584 (info, name, howto->name, (bfd_vma) 0,
1585 input_bfd, input_section, rela->r_offset)))
1586 return false;
1588 break;
1593 return true;
1596 /* Finish up dynamic symbol handling. We set the contents of various
1597 dynamic sections here. */
1599 static boolean
1600 elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym)
1601 bfd *output_bfd;
1602 struct bfd_link_info *info;
1603 struct elf_link_hash_entry *h;
1604 Elf_Internal_Sym *sym;
1606 bfd *dynobj;
1608 dynobj = elf_hash_table (info)->dynobj;
1610 if (h->plt.offset != (bfd_vma) -1)
1612 asection *splt;
1613 asection *sgot;
1614 asection *srela;
1615 bfd_vma plt_index;
1616 bfd_vma got_offset;
1617 Elf_Internal_Rela rela;
1619 /* This symbol has an entry in the procedure linkage table. Set
1620 it up. */
1622 BFD_ASSERT (h->dynindx != -1);
1624 splt = bfd_get_section_by_name (dynobj, ".plt");
1625 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1626 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1627 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1629 /* Get the index in the procedure linkage table which
1630 corresponds to this symbol. This is the index of this symbol
1631 in all the symbols for which we are making plt entries. The
1632 first entry in the procedure linkage table is reserved. */
1633 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1635 /* Get the offset into the .got table of the entry that
1636 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
1637 bytes. The first three are reserved for the dynamic linker. */
1638 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
1640 /* Fill in the entry in the procedure linkage table. */
1641 memcpy (splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
1642 PLT_ENTRY_SIZE);
1644 /* Insert the relocation positions of the plt section. The magic
1645 numbers at the end of the statements are the positions of the
1646 relocations in the plt section. */
1647 /* Put offset for jmp *name@GOTPCREL(%rip), since the
1648 instruction uses 6 bytes, subtract this value. */
1649 bfd_put_32 (output_bfd,
1650 (sgot->output_section->vma
1651 + sgot->output_offset
1652 + got_offset
1653 - splt->output_section->vma
1654 - splt->output_offset
1655 - h->plt.offset
1656 - 6),
1657 splt->contents + h->plt.offset + 2);
1658 /* Put relocation index. */
1659 bfd_put_32 (output_bfd, plt_index,
1660 splt->contents + h->plt.offset + 7);
1661 /* Put offset for jmp .PLT0. */
1662 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
1663 splt->contents + h->plt.offset + 12);
1665 /* Fill in the entry in the global offset table, initially this
1666 points to the pushq instruction in the PLT which is at offset 6. */
1667 bfd_put_64 (output_bfd, (splt->output_section->vma + splt->output_offset
1668 + h->plt.offset + 6),
1669 sgot->contents + got_offset);
1671 /* Fill in the entry in the .rela.plt section. */
1672 rela.r_offset = (sgot->output_section->vma
1673 + sgot->output_offset
1674 + got_offset);
1675 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
1676 rela.r_addend = 0;
1677 bfd_elf64_swap_reloca_out (output_bfd, &rela,
1678 ((Elf64_External_Rela *) srela->contents
1679 + plt_index));
1681 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1683 /* Mark the symbol as undefined, rather than as defined in
1684 the .plt section. Leave the value alone. */
1685 sym->st_shndx = SHN_UNDEF;
1686 /* If the symbol is weak, we do need to clear the value.
1687 Otherwise, the PLT entry would provide a definition for
1688 the symbol even if the symbol wasn't defined anywhere,
1689 and so the symbol would never be NULL. */
1690 if ((h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR_NONWEAK)
1691 == 0)
1692 sym->st_value = 0;
1696 if (h->got.offset != (bfd_vma) -1)
1698 asection *sgot;
1699 asection *srela;
1700 Elf_Internal_Rela rela;
1702 /* This symbol has an entry in the global offset table. Set it
1703 up. */
1705 sgot = bfd_get_section_by_name (dynobj, ".got");
1706 srela = bfd_get_section_by_name (dynobj, ".rela.got");
1707 BFD_ASSERT (sgot != NULL && srela != NULL);
1709 rela.r_offset = (sgot->output_section->vma
1710 + sgot->output_offset
1711 + (h->got.offset &~ 1));
1713 /* If this is a static link, or it is a -Bsymbolic link and the
1714 symbol is defined locally or was forced to be local because
1715 of a version file, we just want to emit a RELATIVE reloc.
1716 The entry in the global offset table will already have been
1717 initialized in the relocate_section function. */
1718 if (! elf_hash_table (info)->dynamic_sections_created
1719 || (info->shared
1720 && (info->symbolic || h->dynindx == -1)
1721 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1723 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1724 rela.r_addend = (h->root.u.def.value
1725 + h->root.u.def.section->output_section->vma
1726 + h->root.u.def.section->output_offset);
1728 else
1730 BFD_ASSERT((h->got.offset & 1) == 0);
1731 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
1732 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
1733 rela.r_addend = 0;
1736 bfd_elf64_swap_reloca_out (output_bfd, &rela,
1737 ((Elf64_External_Rela *) srela->contents
1738 + srela->reloc_count));
1739 ++srela->reloc_count;
1742 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1744 asection *s;
1745 Elf_Internal_Rela rela;
1747 /* This symbol needs a copy reloc. Set it up. */
1749 BFD_ASSERT (h->dynindx != -1
1750 && (h->root.type == bfd_link_hash_defined
1751 || h->root.type == bfd_link_hash_defweak));
1753 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1754 ".rela.bss");
1755 BFD_ASSERT (s != NULL);
1757 rela.r_offset = (h->root.u.def.value
1758 + h->root.u.def.section->output_section->vma
1759 + h->root.u.def.section->output_offset);
1760 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
1761 rela.r_addend = 0;
1762 bfd_elf64_swap_reloca_out (output_bfd, &rela,
1763 ((Elf64_External_Rela *) s->contents
1764 + s->reloc_count));
1765 ++s->reloc_count;
1768 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1769 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1770 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1771 sym->st_shndx = SHN_ABS;
1773 return true;
1776 /* Finish up the dynamic sections. */
1778 static boolean
1779 elf64_x86_64_finish_dynamic_sections (output_bfd, info)
1780 bfd *output_bfd;
1781 struct bfd_link_info *info;
1783 bfd *dynobj;
1784 asection *sdyn;
1785 asection *sgot;
1787 dynobj = elf_hash_table (info)->dynobj;
1789 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1790 BFD_ASSERT (sgot != NULL);
1791 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1793 if (elf_hash_table (info)->dynamic_sections_created)
1795 asection *splt;
1796 Elf64_External_Dyn *dyncon, *dynconend;
1798 BFD_ASSERT (sdyn != NULL);
1800 dyncon = (Elf64_External_Dyn *) sdyn->contents;
1801 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1802 for (; dyncon < dynconend; dyncon++)
1804 Elf_Internal_Dyn dyn;
1805 const char *name;
1806 asection *s;
1808 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
1810 switch (dyn.d_tag)
1812 default:
1813 continue;
1815 case DT_PLTGOT:
1816 name = ".got";
1817 goto get_vma;
1819 case DT_JMPREL:
1820 name = ".rela.plt";
1822 get_vma:
1823 s = bfd_get_section_by_name (output_bfd, name);
1824 BFD_ASSERT (s != NULL);
1825 dyn.d_un.d_ptr = s->vma;
1826 break;
1828 case DT_RELASZ:
1829 /* FIXME: This comment and code is from elf64-alpha.c: */
1830 /* My interpretation of the TIS v1.1 ELF document indicates
1831 that RELASZ should not include JMPREL. This is not what
1832 the rest of the BFD does. It is, however, what the
1833 glibc ld.so wants. Do this fixup here until we found
1834 out who is right. */
1835 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1836 if (s)
1838 /* Subtract JMPREL size from RELASZ. */
1839 dyn.d_un.d_val -=
1840 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
1842 break;
1844 case DT_PLTRELSZ:
1845 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1846 BFD_ASSERT (s != NULL);
1847 dyn.d_un.d_val =
1848 (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size);
1849 break;
1851 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
1854 /* Initialize the contents of the .plt section. */
1855 splt = bfd_get_section_by_name (dynobj, ".plt");
1856 BFD_ASSERT (splt != NULL);
1857 if (splt->_raw_size > 0)
1859 /* Fill in the first entry in the procedure linkage table. */
1860 memcpy (splt->contents, elf64_x86_64_plt0_entry, PLT_ENTRY_SIZE);
1861 /* Add offset for pushq GOT+8(%rip), since the instruction
1862 uses 6 bytes subtract this value. */
1863 bfd_put_32 (output_bfd,
1864 (sgot->output_section->vma
1865 + sgot->output_offset
1867 - splt->output_section->vma
1868 - splt->output_offset
1869 - 6),
1870 splt->contents + 2);
1871 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
1872 the end of the instruction. */
1873 bfd_put_32 (output_bfd,
1874 (sgot->output_section->vma
1875 + sgot->output_offset
1876 + 16
1877 - splt->output_section->vma
1878 - splt->output_offset
1879 - 12),
1880 splt->contents + 8);
1884 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1885 PLT_ENTRY_SIZE;
1888 /* Set the first entry in the global offset table to the address of
1889 the dynamic section. */
1890 if (sgot->_raw_size > 0)
1892 if (sdyn == NULL)
1893 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
1894 else
1895 bfd_put_64 (output_bfd,
1896 sdyn->output_section->vma + sdyn->output_offset,
1897 sgot->contents);
1898 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
1899 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE);
1900 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE*2);
1903 elf_section_data (sgot->output_section)->this_hdr.sh_entsize =
1904 GOT_ENTRY_SIZE;
1906 return true;
1910 * Why was the hash table entry size definition changed from
1911 * ARCH_SIZE/8 to 4? This breaks the 64 bit dynamic linker and
1912 * this is the only reason for the elf64_x86_64_size_info structure.
1915 const struct elf_size_info elf64_86_64_size_info =
1917 sizeof (Elf64_External_Ehdr),
1918 sizeof (Elf64_External_Phdr),
1919 sizeof (Elf64_External_Shdr),
1920 sizeof (Elf64_External_Rel),
1921 sizeof (Elf64_External_Rela),
1922 sizeof (Elf64_External_Sym),
1923 sizeof (Elf64_External_Dyn),
1924 sizeof (Elf_External_Note),
1925 8, /* hash-table entry size */
1926 1, /* internal relocations per external relocations */
1927 64, /* arch_size */
1928 8, /* file_align */
1929 ELFCLASS64, EV_CURRENT,
1930 bfd_elf64_write_out_phdrs,
1931 bfd_elf64_write_shdrs_and_ehdr,
1932 bfd_elf64_write_relocs,
1933 bfd_elf64_swap_symbol_out,
1934 bfd_elf64_slurp_reloc_table,
1935 bfd_elf64_slurp_symbol_table,
1936 bfd_elf64_swap_dyn_in,
1937 bfd_elf64_swap_dyn_out,
1938 NULL,
1939 NULL,
1940 NULL,
1941 NULL
1944 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
1945 #define TARGET_LITTLE_NAME "elf64-x86-64"
1946 #define ELF_ARCH bfd_arch_i386
1947 #define ELF_MACHINE_CODE EM_X86_64
1948 #define ELF_MAXPAGESIZE 0x100000
1950 #define elf_backend_size_info elf64_86_64_size_info
1952 #define elf_backend_can_gc_sections 1
1953 #define elf_backend_want_got_plt 1
1954 #define elf_backend_plt_readonly 1
1955 #define elf_backend_want_plt_sym 0
1956 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
1957 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
1959 #define elf_info_to_howto elf64_x86_64_info_to_howto
1961 #define bfd_elf64_bfd_final_link _bfd_elf64_gc_common_final_link
1962 #define bfd_elf64_bfd_link_hash_table_create \
1963 elf64_x86_64_link_hash_table_create
1964 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
1966 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
1967 #define elf_backend_check_relocs elf64_x86_64_check_relocs
1968 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
1969 #define elf_backend_finish_dynamic_sections \
1970 elf64_x86_64_finish_dynamic_sections
1971 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
1972 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
1973 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
1974 #define elf_backend_relocate_section elf64_x86_64_relocate_section
1975 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
1976 #define elf_backend_object_p elf64_x86_64_elf_object_p
1978 #include "elf64-target.h"