2001-02-08 H.J. Lu <hjl@gnu.org>
[binutils.git] / bfd / elf64-x86-64.c
blob27a97a29ef0739554008bb8f3092a9f3e133e1b4
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, special_function,
36 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, 0, "R_X86_64_NONE", false,0x00000000,0x00000000,false),
40 HOWTO(R_X86_64_64, 0,4,64,false,0,complain_overflow_bitfield,0, "R_X86_64_64", false,MINUS_ONE ,MINUS_ONE ,false),
41 HOWTO(R_X86_64_PC32, 0,4,32,true ,0,complain_overflow_signed ,0, "R_X86_64_PC32", false,0xffffffff,0xffffffff,true),
42 HOWTO(R_X86_64_GOT32, 0,4,32,false,0,complain_overflow_signed ,0, "R_X86_64_GOT32", false,0xffffffff,0xffffffff,false),
43 HOWTO(R_X86_64_PLT32, 0,4,32,true ,0,complain_overflow_signed ,0, "R_X86_64_PLT32", false,0xffffffff,0xffffffff,true),
44 HOWTO(R_X86_64_COPY, 0,4,32,false,0,complain_overflow_bitfield,0, "R_X86_64_COPY", false,0xffffffff,0xffffffff,false),
45 HOWTO(R_X86_64_GLOB_DAT, 0,4,64,false,0,complain_overflow_bitfield,0,"R_X86_64_GLOB_DAT",false,MINUS_ONE ,MINUS_ONE ,false),
46 HOWTO(R_X86_64_RELATIVE ,0,4,64,false,0,complain_overflow_bitfield,0,"R_X86_64_RELATIVE",false,MINUS_ONE ,MINUS_ONE ,false),
47 HOWTO(R_X86_64_JUMP_SLOT,0,4,64,false,0,complain_overflow_bitfield,0,"R_X86_64_JUMP_SLOT",false,MINUS_ONE,MINUS_ONE ,false),
48 HOWTO(R_X86_64_GOTPCREL, 0,4,32,true, 0,complain_overflow_signed ,0, "R_X86_64_PCREL", false,0xffffffff,0xffffffff,true),
49 HOWTO(R_X86_64_32, 0,4,32,false,0,complain_overflow_unsigned,0, "R_X86_64_32", false,0xffffffff,0xffffffff,false),
50 HOWTO(R_X86_64_32S, 0,4,32,false,0,complain_overflow_signed, 0, "R_X86_64_32S", false,0xffffffff,0xffffffff,false),
51 HOWTO(R_X86_64_16, 0,1,16,false,0,complain_overflow_bitfield,0, "R_X86_64_16", false,0xffff ,0xffff, false),
52 HOWTO(R_X86_64_PC16, 0,1,16,true ,0,complain_overflow_bitfield,0, "R_X86_64_PC16", false,0xffff ,0xffff, true),
53 HOWTO(R_X86_64_8, 0,0, 8,false,0,complain_overflow_signed ,0, "R_X86_64_8", false,0xff ,0xff, false),
54 HOWTO(R_X86_64_PC8, 0,0, 8,true ,0,complain_overflow_signed ,0, "R_X86_64_PC8", false,0xff ,0xff, true),
57 /* Map BFD relocs to the x86_64 elf relocs. */
58 struct elf_reloc_map
60 bfd_reloc_code_real_type bfd_reloc_val;
61 unsigned char elf_reloc_val;
64 static CONST struct elf_reloc_map x86_64_reloc_map[] =
66 { BFD_RELOC_NONE, R_X86_64_NONE, },
67 { BFD_RELOC_64, R_X86_64_64, },
68 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
69 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
70 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
71 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
72 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
73 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
74 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
75 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
76 { BFD_RELOC_32, R_X86_64_32, },
77 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
78 { BFD_RELOC_16, R_X86_64_16, },
79 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
80 { BFD_RELOC_8, R_X86_64_8, },
81 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
84 static reloc_howto_type *elf64_x86_64_reloc_type_lookup
85 PARAMS ((bfd *, bfd_reloc_code_real_type));
86 static void elf64_x86_64_info_to_howto
87 PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
88 static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create
89 PARAMS ((bfd *));
91 static struct bfd_hash_entry *elf64_x86_64_link_hash_newfunc
92 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
93 static boolean elf64_x86_64_adjust_dynamic_symbol
94 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
96 static boolean elf64_x86_64_size_dynamic_sections
97 PARAMS ((bfd *, struct bfd_link_info *));
98 static boolean elf64_x86_64_relocate_section
99 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
100 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
101 static boolean elf64_x86_64_finish_dynamic_symbol
102 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
103 Elf_Internal_Sym *sym));
104 static boolean elf64_x86_64_finish_dynamic_sections
105 PARAMS ((bfd *, struct bfd_link_info *));
107 /* Given a BFD reloc type, return a HOWTO structure. */
108 static reloc_howto_type *
109 elf64_x86_64_reloc_type_lookup (abfd, code)
110 bfd *abfd ATTRIBUTE_UNUSED;
111 bfd_reloc_code_real_type code;
113 unsigned int i;
114 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
115 i++)
117 if (x86_64_reloc_map[i].bfd_reloc_val == code)
118 return &x86_64_elf_howto_table[(int)
119 x86_64_reloc_map[i].elf_reloc_val];
121 return 0;
124 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
126 static void
127 elf64_x86_64_info_to_howto (abfd, cache_ptr, dst)
128 bfd *abfd ATTRIBUTE_UNUSED;
129 arelent *cache_ptr;
130 Elf64_Internal_Rela *dst;
132 unsigned r_type;
134 r_type = ELF64_R_TYPE (dst->r_info);
135 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
136 cache_ptr->howto = &x86_64_elf_howto_table[r_type];
137 BFD_ASSERT (r_type == cache_ptr->howto->type);
140 /* Functions for the x86-64 ELF linker. */
142 /* The name of the dynamic interpreter. This is put in the .interp
143 section. */
145 #define ELF_DYNAMIC_INTERPRETER "/lib/libd64.so.1"
147 /* The size in bytes of an entry in the global offset table. */
149 #define GOT_ENTRY_SIZE 8
151 /* The size in bytes of an entry in the procedure linkage table. */
153 #define PLT_ENTRY_SIZE 16
155 /* The first entry in a procedure linkage table looks like this. See the
156 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
158 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
160 0xff, 0xb3, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
161 0xff, 0xa3, 16, 0, 0, 0, /* jmp GOT+16(%rip) */
162 0, 0, 0, 0 /* pad out to 16 bytes. */
165 /* Subsequent entries in a procedure linkage table look like this. */
167 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
169 0xff, 0xa3, /* jmp *name@GOTPC(%rip) */
170 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
171 0x68, /* pushq immediate */
172 0, 0, 0, 0, /* replaced with index into relocation table. */
173 0xe9, /* jmp relative */
174 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
177 /* The x86-64 linker needs to keep track of the number of relocs that
178 it decides to copy in check_relocs for each symbol. This is so
179 that it can discard PC relative relocs if it doesn't need them when
180 linking with -Bsymbolic. We store the information in a field
181 extending the regular ELF linker hash table. */
183 /* This structure keeps track of the number of PC relative relocs we
184 have copied for a given symbol. */
186 struct elf64_x86_64_pcrel_relocs_copied
188 /* Next section. */
189 struct elf64_x86_64_pcrel_relocs_copied *next;
190 /* A section in dynobj. */
191 asection *section;
192 /* Number of relocs copied in this section. */
193 bfd_size_type count;
196 /* x86-64 ELF linker hash entry. */
198 struct elf64_x86_64_link_hash_entry
200 struct elf_link_hash_entry root;
202 /* Number of PC relative relocs copied for this symbol. */
203 struct elf64_x86_64_pcrel_relocs_copied *pcrel_relocs_copied;
206 /* x86-64 ELF linker hash table. */
208 struct elf64_x86_64_link_hash_table {
209 struct elf_link_hash_table root;
212 /* Declare this now that the above structures are defined. */
214 static boolean elf64_x86_64_discard_copies
215 PARAMS ((struct elf64_x86_64_link_hash_entry *, PTR));
217 /* Traverse an x86-64 ELF linker hash table. */
219 #define elf64_x86_64_link_hash_traverse(table, func, info) \
220 (elf_link_hash_traverse \
221 (&(table)->root, \
222 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
223 (info)))
225 /* Get the x86-64 ELF linker hash table from a link_info structure. */
227 #define elf64_x86_64_hash_table(p) \
228 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
230 /* Create an entry in an x86-64 ELF linker hash table. */
232 static struct bfd_hash_entry *
233 elf64_x86_64_link_hash_newfunc (entry, table, string)
234 struct bfd_hash_entry *entry;
235 struct bfd_hash_table *table;
236 const char *string;
238 struct elf64_x86_64_link_hash_entry *ret =
239 (struct elf64_x86_64_link_hash_entry *) entry;
241 /* Allocate the structure if it has not already been allocated by a
242 subclass. */
243 if (ret == (struct elf64_x86_64_link_hash_entry *) NULL)
244 ret = ((struct elf64_x86_64_link_hash_entry *)
245 bfd_hash_allocate (table,
246 sizeof (struct elf64_x86_64_link_hash_entry)));
247 if (ret == (struct elf64_x86_64_link_hash_entry *) NULL)
248 return (struct bfd_hash_entry *) ret;
250 /* Call the allocation method of the superclass. */
251 ret = ((struct elf64_x86_64_link_hash_entry *)
252 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
253 table, string));
254 if (ret != (struct elf64_x86_64_link_hash_entry *) NULL)
256 ret->pcrel_relocs_copied = NULL;
259 return (struct bfd_hash_entry *) ret;
262 /* Create an X86-64 ELF linker hash table. */
264 static struct bfd_link_hash_table *
265 elf64_x86_64_link_hash_table_create (abfd)
266 bfd *abfd;
268 struct elf64_x86_64_link_hash_table *ret;
270 ret = ((struct elf64_x86_64_link_hash_table *)
271 bfd_alloc (abfd, sizeof (struct elf64_x86_64_link_hash_table)));
272 if (ret == (struct elf64_x86_64_link_hash_table *) NULL)
273 return NULL;
275 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
276 elf64_x86_64_link_hash_newfunc))
278 bfd_release (abfd, ret);
279 return NULL;
282 return &ret->root.root;
285 boolean
286 elf64_x86_64_elf_object_p (abfd)
287 bfd *abfd;
289 /* Set the right machine number for an x86-64 elf64 file. */
290 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
291 return true;
294 /* Look through the relocs for a section during the first phase, and
295 allocate space in the global offset table or procedure linkage
296 table. */
298 static boolean
299 elf64_x86_64_check_relocs (abfd, info, sec, relocs)
300 bfd *abfd;
301 struct bfd_link_info *info;
302 asection *sec;
303 const Elf_Internal_Rela *relocs;
305 bfd *dynobj;
306 Elf_Internal_Shdr *symtab_hdr;
307 struct elf_link_hash_entry **sym_hashes;
308 bfd_signed_vma *local_got_refcounts;
309 const Elf_Internal_Rela *rel;
310 const Elf_Internal_Rela *rel_end;
311 asection *sgot;
312 asection *srelgot;
313 asection *sreloc;
315 if (info->relocateable)
316 return true;
318 dynobj = elf_hash_table (info)->dynobj;
319 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
320 sym_hashes = elf_sym_hashes (abfd);
321 local_got_refcounts = elf_local_got_refcounts (abfd);
323 sgot = srelgot = sreloc = NULL;
324 rel_end = relocs + sec->reloc_count;
325 for (rel = relocs; rel < rel_end; rel++)
327 unsigned long r_symndx;
328 struct elf_link_hash_entry *h;
330 r_symndx = ELF64_R_SYM (rel->r_info);
331 if (r_symndx < symtab_hdr->sh_info)
332 h = NULL;
333 else
334 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
336 /* Some relocs require a global offset table. */
337 if (dynobj == NULL)
339 switch (ELF64_R_TYPE (rel->r_info))
341 case R_X86_64_GOT32:
342 case R_X86_64_GOTPCREL:
343 elf_hash_table (info)->dynobj = dynobj = abfd;
344 if (! _bfd_elf_create_got_section (dynobj, info))
345 return false;
346 break;
350 switch (ELF64_R_TYPE (rel->r_info))
352 case R_X86_64_GOT32:
353 /* This symbol requires a global offset table entry. */
355 if (sgot == NULL)
357 sgot = bfd_get_section_by_name (dynobj, ".got");
358 BFD_ASSERT (sgot != NULL);
361 if (srelgot == NULL && (h != NULL || info->shared))
363 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
364 if (srelgot == NULL)
366 srelgot = bfd_make_section (dynobj, ".rela.got");
367 if (srelgot == NULL
368 || ! bfd_set_section_flags (dynobj, srelgot,
369 (SEC_ALLOC
370 | SEC_LOAD
371 | SEC_HAS_CONTENTS
372 | SEC_IN_MEMORY
373 | SEC_LINKER_CREATED
374 | SEC_READONLY))
375 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
376 return false;
380 if (h != NULL)
382 if (h->got.refcount == -1)
384 h->got.refcount = 1;
386 /* Make sure this symbol is output as a dynamic symbol. */
387 if (h->dynindx == -1)
389 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
390 return false;
393 sgot->_raw_size += GOT_ENTRY_SIZE;
394 srelgot->_raw_size += sizeof (Elf64_External_Rela);
396 else
397 h->got.refcount += 1;
399 else
401 /* This is a global offset table entry for a local symbol. */
402 if (local_got_refcounts == NULL)
404 size_t size;
406 size = symtab_hdr->sh_info * sizeof (bfd_signed_vma);
407 local_got_refcounts = ((bfd_signed_vma *)
408 bfd_alloc (abfd, size));
409 if (local_got_refcounts == NULL)
410 return false;
411 elf_local_got_refcounts (abfd) = local_got_refcounts;
412 memset (local_got_refcounts, -1, size);
414 if (local_got_refcounts[r_symndx] == -1)
416 local_got_refcounts[r_symndx] = 1;
418 sgot->_raw_size += GOT_ENTRY_SIZE;
419 if (info->shared)
421 /* If we are generating a shared object, we need to
422 output a R_X86_64_RELATIVE reloc so that the dynamic
423 linker can adjust this GOT entry. */
424 srelgot->_raw_size += sizeof (Elf64_External_Rela);
427 else
428 local_got_refcounts[r_symndx] += 1;
430 break;
432 case R_X86_64_PLT32:
433 /* This symbol requires a procedure linkage table entry. We
434 actually build the entry in adjust_dynamic_symbol,
435 because this might be a case of linking PIC code which is
436 never referenced by a dynamic object, in which case we
437 don't need to generate a procedure linkage table entry
438 after all. */
440 /* If this is a local symbol, we resolve it directly without
441 creating a procedure linkage table entry. */
442 if (h == NULL)
443 continue;
445 if (h->plt.refcount == -1)
447 h->plt.refcount = 1;
448 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_PLT;
450 else
451 h->plt.refcount += 1;
452 break;
454 case R_X86_64_32:
455 case R_X86_64_32S:
456 case R_X86_64_PC32:
457 if (h != NULL)
458 h->elf_link_hash_flags |= ELF_LINK_NON_GOT_REF;
460 /* If we are creating a shared library, and this is a reloc
461 against a global symbol, or a non PC relative reloc
462 against a local symbol, then we need to copy the reloc
463 into the shared library. However, if we are linking with
464 -Bsymbolic, we do not need to copy a reloc against a
465 global symbol which is defined in an object we are
466 including in the link (i.e., DEF_REGULAR is set). At
467 this point we have not seen all the input files, so it is
468 possible that DEF_REGULAR is not set now but will be set
469 later (it is never cleared). We account for that
470 possibility below by storing information in the
471 pcrel_relocs_copied field of the hash table entry.
472 A similar situation occurs when creating shared libraries
473 and symbol visibility changes render the symbol local. */
474 if (info->shared
475 && (sec->flags & SEC_ALLOC) != 0
476 && (ELF64_R_TYPE (rel->r_info) != R_X86_64_PC32
477 || (h != NULL
478 && (! info->symbolic
479 || (h->elf_link_hash_flags
480 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
482 /* When creating a shared object, we must copy these
483 reloc types into the output file. We create a reloc
484 section in dynobj and make room for this reloc. */
485 if (sreloc == NULL)
487 const char *name;
489 name = (bfd_elf_string_from_elf_section
490 (abfd,
491 elf_elfheader (abfd)->e_shstrndx,
492 elf_section_data (sec)->rel_hdr.sh_name));
493 if (name == NULL)
494 return false;
496 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
497 && strcmp (bfd_get_section_name (abfd, sec),
498 name + 5) == 0);
500 sreloc = bfd_get_section_by_name (dynobj, name);
501 if (sreloc == NULL)
503 flagword flags;
505 sreloc = bfd_make_section (dynobj, name);
506 flags = (SEC_HAS_CONTENTS | SEC_READONLY
507 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
508 if ((sec->flags & SEC_ALLOC) != 0)
509 flags |= SEC_ALLOC | SEC_LOAD;
510 if (sreloc == NULL
511 || ! bfd_set_section_flags (dynobj, sreloc, flags)
512 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
513 return false;
517 sreloc->_raw_size += sizeof (Elf64_External_Rela);
519 /* If this is a global symbol, we count the number of PC
520 relative relocations we have entered for this symbol,
521 so that we can discard them later as necessary. Note
522 that this function is only called if we are using an
523 elf64_x86_64 linker hash table, which means that h is
524 really a pointer to an elf64_x86_64_link_hash_entry. */
525 if (h != NULL
526 && ELF64_R_TYPE (rel->r_info) == R_X86_64_PC32)
528 struct elf64_x86_64_link_hash_entry *eh;
529 struct elf64_x86_64_pcrel_relocs_copied *p;
531 eh = (struct elf64_x86_64_link_hash_entry *) h;
533 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
534 if (p->section == sreloc)
535 break;
537 if (p == NULL)
539 p = ((struct elf64_x86_64_pcrel_relocs_copied *)
540 bfd_alloc (dynobj, sizeof *p));
541 if (p == NULL)
542 return false;
543 p->next = eh->pcrel_relocs_copied;
544 eh->pcrel_relocs_copied = p;
545 p->section = sreloc;
546 p->count = 0;
549 ++p->count;
552 break;
556 return true;
559 /* Return the section that should be marked against GC for a given
560 relocation. */
562 static asection *
563 elf64_x86_64_gc_mark_hook (abfd, info, rel, h, sym)
564 bfd *abfd;
565 struct bfd_link_info *info ATTRIBUTE_UNUSED;
566 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED;
567 struct elf_link_hash_entry *h;
568 Elf_Internal_Sym *sym;
570 if (h != NULL)
572 switch (h->root.type)
574 case bfd_link_hash_defined:
575 case bfd_link_hash_defweak:
576 return h->root.u.def.section;
578 case bfd_link_hash_common:
579 return h->root.u.c.p->section;
581 default:
582 break;
585 else
587 if (!(elf_bad_symtab (abfd)
588 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
589 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
590 && sym->st_shndx != SHN_COMMON))
592 return bfd_section_from_elf_index (abfd, sym->st_shndx);
596 return NULL;
599 /* Update the got entry reference counts for the section being removed. */
601 static boolean
602 elf64_x86_64_gc_sweep_hook (abfd, info, sec, relocs)
603 bfd *abfd;
604 struct bfd_link_info *info ATTRIBUTE_UNUSED;
605 asection *sec;
606 const Elf_Internal_Rela *relocs;
608 Elf_Internal_Shdr *symtab_hdr;
609 struct elf_link_hash_entry **sym_hashes;
610 bfd_signed_vma *local_got_refcounts;
611 const Elf_Internal_Rela *rel, *relend;
612 unsigned long r_symndx;
613 struct elf_link_hash_entry *h;
614 bfd *dynobj;
615 asection *sgot;
616 asection *srelgot;
618 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
619 sym_hashes = elf_sym_hashes (abfd);
620 local_got_refcounts = elf_local_got_refcounts (abfd);
622 dynobj = elf_hash_table (info)->dynobj;
623 if (dynobj == NULL)
624 return true;
626 sgot = bfd_get_section_by_name (dynobj, ".got");
627 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
629 relend = relocs + sec->reloc_count;
630 for (rel = relocs; rel < relend; rel++)
631 switch (ELF64_R_TYPE (rel->r_info))
633 case R_X86_64_GOT32:
634 case R_X86_64_GOTPCREL:
635 r_symndx = ELF64_R_SYM (rel->r_info);
636 if (r_symndx >= symtab_hdr->sh_info)
638 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
639 if (h->got.refcount > 0)
641 h->got.refcount -= 1;
642 if (h->got.refcount == 0)
644 sgot->_raw_size -= GOT_ENTRY_SIZE;
645 srelgot->_raw_size -= sizeof (Elf64_External_Rela);
649 else if (local_got_refcounts != NULL)
651 if (local_got_refcounts[r_symndx] > 0)
653 local_got_refcounts[r_symndx] -= 1;
654 if (local_got_refcounts[r_symndx] == 0)
656 sgot->_raw_size -= GOT_ENTRY_SIZE;
657 if (info->shared)
658 srelgot->_raw_size -= sizeof (Elf64_External_Rela);
662 break;
664 case R_X86_64_PLT32:
665 r_symndx = ELF64_R_SYM (rel->r_info);
666 if (r_symndx >= symtab_hdr->sh_info)
668 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
669 if (h->plt.refcount > 0)
670 h->plt.refcount -= 1;
672 break;
674 default:
675 break;
678 return true;
681 /* Adjust a symbol defined by a dynamic object and referenced by a
682 regular object. The current definition is in some section of the
683 dynamic object, but we're not including those sections. We have to
684 change the definition to something the rest of the link can
685 understand. */
687 static boolean
688 elf64_x86_64_adjust_dynamic_symbol (info, h)
689 struct bfd_link_info *info;
690 struct elf_link_hash_entry *h;
692 bfd *dynobj;
693 asection *s;
694 unsigned int power_of_two;
696 dynobj = elf_hash_table (info)->dynobj;
698 /* Make sure we know what is going on here. */
699 BFD_ASSERT (dynobj != NULL
700 && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT)
701 || h->weakdef != NULL
702 || ((h->elf_link_hash_flags
703 & ELF_LINK_HASH_DEF_DYNAMIC) != 0
704 && (h->elf_link_hash_flags
705 & ELF_LINK_HASH_REF_REGULAR) != 0
706 && (h->elf_link_hash_flags
707 & ELF_LINK_HASH_DEF_REGULAR) == 0)));
709 /* If this is a function, put it in the procedure linkage table. We
710 will fill in the contents of the procedure linkage table later,
711 when we know the address of the .got section. */
712 if (h->type == STT_FUNC
713 || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0)
715 if (! elf_hash_table (info)->dynamic_sections_created)
717 /* FIXME: These are the sparc64 comment and then the i386 comment.
718 How we need to deal with this and why remains to be seen. */
719 /* This case can occur if we saw a WPLT30 reloc in an input
720 file, but none of the input files were dynamic objects.
721 In such a case, we don't actually need to build a
722 procedure linkage table, and we can just do a WDISP30
723 reloc instead. */
724 /* This case can occur if we saw a PLT32 reloc in an input
725 file, but the symbol was never referred to by a dynamic
726 object, or if all references were garbage collected. In
727 such a case, we don't actually need to build a procedure
728 linkage table, and we can just do a PC32 reloc instead. */
729 /* i386 code: */
730 h->plt.offset = (bfd_vma) -1;
731 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT;
732 return true;
735 /* Make sure this symbol is output as a dynamic symbol. */
736 if (h->dynindx == -1)
738 if (! bfd_elf64_link_record_dynamic_symbol (info, h))
739 return false;
742 s = bfd_get_section_by_name (dynobj, ".plt");
743 BFD_ASSERT (s != NULL);
745 /* If this is the first .plt entry, make room for the special
746 first entry. */
747 if (s->_raw_size == 0)
748 s->_raw_size = PLT_ENTRY_SIZE;
750 /* If this symbol is not defined in a regular file, and we are
751 not generating a shared library, then set the symbol to this
752 location in the .plt. This is required to make function
753 pointers compare as equal between the normal executable and
754 the shared library. */
755 if (! info->shared
756 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
758 h->root.u.def.section = s;
759 h->root.u.def.value = s->_raw_size;
762 h->plt.offset = s->_raw_size;
764 /* Make room for this entry. */
765 s->_raw_size += PLT_ENTRY_SIZE;
767 /* We also need to make an entry in the .rela.plt section. */
768 s = bfd_get_section_by_name (dynobj, ".rela.plt");
769 BFD_ASSERT (s != NULL);
770 s->_raw_size += sizeof (Elf64_External_Rela);
772 return true;
775 /* If this is a weak symbol, and there is a real definition, the
776 processor independent code will have arranged for us to see the
777 real definition first, and we can just use the same value. */
778 if (h->weakdef != NULL)
780 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
781 || h->weakdef->root.type == bfd_link_hash_defweak);
782 h->root.u.def.section = h->weakdef->root.u.def.section;
783 h->root.u.def.value = h->weakdef->root.u.def.value;
784 return true;
787 /* This is a reference to a symbol defined by a dynamic object which
788 is not a function. */
790 /* If we are creating a shared library, we must presume that the
791 only references to the symbol are via the global offset table.
792 For such cases we need not do anything here; the relocations will
793 be handled correctly by relocate_section. */
794 if (info->shared)
795 return true;
797 /* If there are no references to this symbol that do not use the
798 GOT, we don't need to generate a copy reloc. */
799 if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0)
800 return true;
802 /* We must allocate the symbol in our .dynbss section, which will
803 become part of the .bss section of the executable. There will be
804 an entry for this symbol in the .dynsym section. The dynamic
805 object will contain position independent code, so all references
806 from the dynamic object to this symbol will go through the global
807 offset table. The dynamic linker will use the .dynsym entry to
808 determine the address it must put in the global offset table, so
809 both the dynamic object and the regular object will refer to the
810 same memory location for the variable. */
812 s = bfd_get_section_by_name (dynobj, ".dynbss");
813 BFD_ASSERT (s != NULL);
815 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
816 to copy the initial value out of the dynamic object and into the
817 runtime process image. We need to remember the offset into the
818 .rela.bss section we are going to use. */
819 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
821 asection *srel;
823 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
824 BFD_ASSERT (srel != NULL);
825 srel->_raw_size += sizeof (Elf64_External_Rela);
826 h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
829 /* We need to figure out the alignment required for this symbol. I
830 have no idea how ELF linkers handle this. 16-bytes is the size
831 of the largest type that requires hard alignment -- long double. */
832 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
833 this construct. */
834 power_of_two = bfd_log2 (h->size);
835 if (power_of_two > 4)
836 power_of_two = 4;
838 /* Apply the required alignment. */
839 s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two));
840 if (power_of_two > bfd_get_section_alignment (dynobj, s))
842 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
843 return false;
846 /* Define the symbol as being at this point in the section. */
847 h->root.u.def.section = s;
848 h->root.u.def.value = s->_raw_size;
850 /* Increment the section size to make room for the symbol. */
851 s->_raw_size += h->size;
853 return true;
856 /* Set the sizes of the dynamic sections. */
858 static boolean
859 elf64_x86_64_size_dynamic_sections (output_bfd, info)
860 bfd *output_bfd;
861 struct bfd_link_info *info;
863 bfd *dynobj;
864 asection *s;
865 boolean plt;
866 boolean relocs;
867 boolean reltext;
869 dynobj = elf_hash_table (info)->dynobj;
870 BFD_ASSERT (dynobj != NULL);
872 if (elf_hash_table (info)->dynamic_sections_created)
874 /* Set the contents of the .interp section to the interpreter. */
875 if (! info->shared)
877 s = bfd_get_section_by_name (dynobj, ".interp");
878 BFD_ASSERT (s != NULL);
879 s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
880 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
883 else
885 /* We may have created entries in the .rela.got section.
886 However, if we are not creating the dynamic sections, we will
887 not actually use these entries. Reset the size of .rela.got,
888 which will cause it to get stripped from the output file
889 below. */
890 s = bfd_get_section_by_name (dynobj, ".rela.got");
891 if (s != NULL)
892 s->_raw_size = 0;
895 /* If this is a -Bsymbolic shared link, then we need to discard all
896 PC relative relocs against symbols defined in a regular object.
897 We allocated space for them in the check_relocs routine, but we
898 will not fill them in in the relocate_section routine. */
899 if (info->shared)
900 elf64_x86_64_link_hash_traverse (elf64_x86_64_hash_table (info),
901 elf64_x86_64_discard_copies,
902 (PTR) info);
904 /* The check_relocs and adjust_dynamic_symbol entry points have
905 determined the sizes of the various dynamic sections. Allocate
906 memory for them. */
907 plt = relocs = reltext = false;
908 for (s = dynobj->sections; s != NULL; s = s->next)
910 const char *name;
911 boolean strip;
913 if ((s->flags & SEC_LINKER_CREATED) == 0)
914 continue;
916 /* It's OK to base decisions on the section name, because none
917 of the dynobj section names depend upon the input files. */
918 name = bfd_get_section_name (dynobj, s);
920 strip = false;
921 if (strcmp (name, ".plt") == 0)
923 if (s->_raw_size == 0)
925 /* Strip this section if we don't need it; see the
926 comment below. */
927 strip = true;
929 else
931 /* Remember whether there is a PLT. */
932 plt = true;
935 else if (strncmp (name, ".rela", 5) == 0)
937 if (s->_raw_size == 0)
939 /* If we don't need this section, strip it from the
940 output file. This is mostly to handle .rela.bss and
941 .rela.plt. We must create both sections in
942 create_dynamic_sections, because they must be created
943 before the linker maps input sections to output
944 sections. The linker does that before
945 adjust_dynamic_symbol is called, and it is that
946 function which decides whether anything needs to go
947 into these sections. */
948 strip = true;
950 else
952 asection *target;
954 /* Remember whether there are any reloc sections other
955 than .rela.plt. */
956 if (strcmp (name, ".rela.plt") != 0)
958 const char *outname;
960 relocs = true;
962 /* If this relocation section applies to a read only
963 section, then we probably need a DT_TEXTREL
964 entry. The entries in the .rela.plt section
965 really apply to the .got section, which we
966 created ourselves and so know is not readonly. */
967 outname = bfd_get_section_name (output_bfd,
968 s->output_section);
969 target = bfd_get_section_by_name (output_bfd, outname + 5);
970 if (target != NULL
971 && (target->flags & SEC_READONLY) != 0
972 && (target->flags & SEC_ALLOC) != 0)
973 reltext = true;
976 /* We use the reloc_count field as a counter if we need
977 to copy relocs into the output file. */
978 s->reloc_count = 0;
981 else if (strncmp (name, ".got", 4) != 0)
983 /* It's not one of our sections, so don't allocate space. */
984 continue;
987 if (strip)
989 _bfd_strip_section_from_output (info, s);
990 continue;
993 /* Allocate memory for the section contents. We use bfd_zalloc
994 here in case unused entries are not reclaimed before the
995 section's contents are written out. This should not happen,
996 but this way if it does, we get a R_X86_64_NONE reloc instead
997 of garbage. */
998 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size);
999 if (s->contents == NULL && s->_raw_size != 0)
1000 return false;
1003 if (elf_hash_table (info)->dynamic_sections_created)
1005 /* Add some entries to the .dynamic section. We fill in the
1006 values later, in elf64_x86_64_finish_dynamic_sections, but we
1007 must add the entries now so that we get the correct size for
1008 the .dynamic section. The DT_DEBUG entry is filled in by the
1009 dynamic linker and used by the debugger. */
1010 if (! info->shared)
1012 if (! bfd_elf64_add_dynamic_entry (info, DT_DEBUG, 0))
1013 return false;
1016 if (plt)
1018 /* FIXME: Are all these needed? */
1019 if (! bfd_elf64_add_dynamic_entry (info, DT_PLTGOT, 0)
1020 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTRELSZ, 0)
1021 || ! bfd_elf64_add_dynamic_entry (info, DT_PLTREL, DT_REL)
1022 || ! bfd_elf64_add_dynamic_entry (info, DT_JMPREL, 0))
1023 return false;
1026 if (relocs)
1028 if (! bfd_elf64_add_dynamic_entry (info, DT_RELA, 0)
1029 || ! bfd_elf64_add_dynamic_entry (info, DT_RELASZ, 0)
1030 || ! bfd_elf64_add_dynamic_entry (info, DT_RELAENT,
1031 sizeof (Elf64_External_Rela)))
1032 return false;
1035 if (reltext)
1037 if (! bfd_elf64_add_dynamic_entry (info, DT_TEXTREL, 0))
1038 return false;
1039 info->flags |= DF_TEXTREL;
1043 return true;
1046 /* This function is called via elf64_x86_64_link_hash_traverse if we are
1047 creating a shared object. In the -Bsymbolic case, it discards the
1048 space allocated to copy PC relative relocs against symbols which
1049 are defined in regular objects. For the normal non-symbolic case,
1050 we also discard space for relocs that have become local due to
1051 symbol visibility changes. We allocated space for them in the
1052 check_relocs routine, but we won't fill them in in the
1053 relocate_section routine. */
1055 static boolean
1056 elf64_x86_64_discard_copies (h, inf)
1057 struct elf64_x86_64_link_hash_entry *h;
1058 PTR inf;
1060 struct elf64_x86_64_pcrel_relocs_copied *s;
1061 struct bfd_link_info *info = (struct bfd_link_info *) inf;
1063 /* If a symbol has been forced local or we have found a regular
1064 definition for the symbolic link case, then we won't be needing
1065 any relocs. */
1066 if ((h->root.elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0
1067 && ((h->root.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0
1068 || info->symbolic))
1070 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
1071 s->section->_raw_size -= s->count * sizeof (Elf64_External_Rela);
1074 return true;
1077 /* Relocate an x86_64 ELF section. */
1079 static boolean
1080 elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
1081 contents, relocs, local_syms, local_sections)
1082 bfd *output_bfd;
1083 struct bfd_link_info *info;
1084 bfd *input_bfd;
1085 asection *input_section;
1086 bfd_byte *contents;
1087 Elf_Internal_Rela *relocs;
1088 Elf_Internal_Sym *local_syms;
1089 asection **local_sections;
1091 bfd *dynobj;
1092 Elf_Internal_Shdr *symtab_hdr;
1093 struct elf_link_hash_entry **sym_hashes;
1094 bfd_vma *local_got_offsets;
1095 asection *sgot;
1096 asection *splt;
1097 asection *sreloc;
1098 Elf_Internal_Rela *rela;
1099 Elf_Internal_Rela *relend;
1101 dynobj = elf_hash_table (info)->dynobj;
1102 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1103 sym_hashes = elf_sym_hashes (input_bfd);
1104 local_got_offsets = elf_local_got_offsets (input_bfd);
1106 sreloc = splt = sgot = NULL;
1107 if (dynobj != NULL)
1109 splt = bfd_get_section_by_name (dynobj, ".plt");
1110 sgot = bfd_get_section_by_name (dynobj, ".got");
1113 rela = relocs;
1114 relend = relocs + input_section->reloc_count;
1115 for (; rela < relend; rela++)
1117 int r_type;
1118 reloc_howto_type *howto;
1119 unsigned long r_symndx;
1120 struct elf_link_hash_entry *h;
1121 Elf_Internal_Sym *sym;
1122 asection *sec;
1123 bfd_vma relocation;
1124 bfd_reloc_status_type r;
1125 unsigned int indx;
1127 r_type = ELF64_R_TYPE (rela->r_info);
1129 if ((indx = (unsigned) r_type) >= R_X86_64_max)
1131 bfd_set_error (bfd_error_bad_value);
1132 return false;
1134 howto = x86_64_elf_howto_table + indx;
1136 r_symndx = ELF64_R_SYM (rela->r_info);
1138 if (info->relocateable)
1140 /* This is a relocateable link. We don't have to change
1141 anything, unless the reloc is against a section symbol,
1142 in which case we have to adjust according to where the
1143 section symbol winds up in the output section. */
1144 if (r_symndx < symtab_hdr->sh_info)
1146 sym = local_syms + r_symndx;
1147 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1149 sec = local_sections[r_symndx];
1150 rela->r_addend += sec->output_offset + sym->st_value;
1154 continue;
1157 /* This is a final link. */
1158 h = NULL;
1159 sym = NULL;
1160 sec = NULL;
1161 if (r_symndx < symtab_hdr->sh_info)
1163 sym = local_syms + r_symndx;
1164 sec = local_sections[r_symndx];
1165 relocation = (sec->output_section->vma
1166 + sec->output_offset
1167 + sym->st_value);
1169 else
1171 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1172 while (h->root.type == bfd_link_hash_indirect
1173 || h->root.type == bfd_link_hash_warning)
1174 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1175 if (h->root.type == bfd_link_hash_defined
1176 || h->root.type == bfd_link_hash_defweak)
1178 sec = h->root.u.def.section;
1179 if (sec->output_section == NULL)
1181 (*_bfd_error_handler)
1182 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1183 bfd_get_filename (input_bfd), h->root.root.string,
1184 bfd_get_section_name (input_bfd, input_section));
1185 relocation = 0;
1187 else
1188 relocation = (h->root.u.def.value
1189 + sec->output_section->vma
1190 + sec->output_offset);
1192 else if (h->root.type == bfd_link_hash_undefweak)
1193 relocation = 0;
1194 else if (info->shared && !info->symbolic && !info->no_undefined
1195 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
1196 relocation = 0;
1197 else
1199 if (! ((*info->callbacks->undefined_symbol)
1200 (info, h->root.root.string, input_bfd,
1201 input_section, rela->r_offset,
1202 (!info->shared || info->no_undefined
1203 || ELF_ST_VISIBILITY (h->other)))))
1204 return false;
1205 relocation = 0;
1209 /* When generating a shared object, the relocations handled here are
1210 copied into the output file to be resolved at run time. */
1211 switch (r_type)
1213 case R_X86_64_GOT32:
1214 /* Relocation is to the entry for this symbol in the global
1215 offset table. */
1216 BFD_ASSERT (sgot != NULL);
1218 if (h != NULL)
1220 bfd_vma off = h->got.offset;
1221 BFD_ASSERT (off != (bfd_vma) -1);
1223 if (! elf_hash_table (info)->dynamic_sections_created
1224 || (info->shared
1225 && (info->symbolic || h->dynindx == -1)
1226 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR)))
1228 /* This is actually a static link, or it is a -Bsymbolic
1229 link and the symbol is defined locally, or the symbol
1230 was forced to be local because of a version file. We
1231 must initialize this entry in the global offset table.
1232 Since the offset must always be a multiple of 8, we
1233 use the least significant bit to record whether we
1234 have initialized it already.
1236 When doing a dynamic link, we create a .rela.got
1237 relocation entry to initialize the value. This is
1238 done in the finish_dynamic_symbol routine. */
1239 if ((off & 1) != 0)
1240 off &= ~1;
1241 else
1243 bfd_put_64 (output_bfd, relocation,
1244 sgot->contents + off);
1245 h->got.offset |= 1;
1248 relocation = sgot->output_offset + off;
1250 else
1252 bfd_vma off;
1254 BFD_ASSERT (local_got_offsets != NULL
1255 && local_got_offsets[r_symndx] != (bfd_vma) -1);
1257 off = local_got_offsets[r_symndx];
1259 /* The offset must always be a multiple of 8. We use
1260 the least significant bit to record whether we have
1261 already generated the necessary reloc. */
1262 if ((off & 1) != 0)
1263 off &= ~1;
1264 else
1266 bfd_put_64 (output_bfd, relocation, sgot->contents + off);
1268 if (info->shared)
1270 asection *srelgot;
1271 Elf_Internal_Rela outrel;
1273 /* We need to generate a R_X86_64_RELATIVE reloc
1274 for the dynamic linker. */
1275 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1276 BFD_ASSERT (srelgot != NULL);
1278 outrel.r_offset = (sgot->output_section->vma
1279 + sgot->output_offset
1280 + off);
1281 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1282 outrel.r_addend = relocation;
1283 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1284 (((Elf64_External_Rela *)
1285 srelgot->contents)
1286 + srelgot->reloc_count));
1287 ++srelgot->reloc_count;
1290 local_got_offsets[r_symndx] |= 1;
1293 relocation = sgot->output_offset + off;
1296 break;
1298 case R_X86_64_GOTPCREL:
1299 /* Use global offset table as symbol value. */
1301 if (sgot == NULL)
1303 sgot = bfd_get_section_by_name (dynobj, ".got");
1304 BFD_ASSERT (sgot != NULL);
1307 relocation = sgot->output_section->vma + (h->got.offset & ~1);
1309 break;
1311 case R_X86_64_PLT32:
1312 /* Relocation is to the entry for this symbol in the
1313 procedure linkage table. */
1315 /* Resolve a PLT32 reloc against a local symbol directly,
1316 without using the procedure linkage table. */
1317 if (h == NULL)
1318 break;
1320 if (h->plt.offset == (bfd_vma) -1 || splt == NULL)
1322 /* We didn't make a PLT entry for this symbol. This
1323 happens when statically linking PIC code, or when
1324 using -Bsymbolic. */
1325 break;
1328 relocation = (splt->output_section->vma
1329 + splt->output_offset
1330 + h->plt.offset);
1331 break;
1333 case R_X86_64_8:
1334 case R_X86_64_16:
1335 case R_X86_64_32:
1336 case R_X86_64_PC8:
1337 case R_X86_64_PC16:
1338 case R_X86_64_PC32:
1339 /* FIXME: The abi says the linker should make sure the value is
1340 the same when it's zeroextended to 64 bit. */
1341 if (info->shared
1342 && (input_section->flags & SEC_ALLOC) != 0
1343 && ((r_type != R_X86_64_PC8 && r_type != R_X86_64_PC16
1344 && r_type != R_X86_64_PC32)
1345 || (h != NULL
1346 && h->dynindx != -1
1347 && (! info->symbolic
1348 || (h->elf_link_hash_flags
1349 & ELF_LINK_HASH_DEF_REGULAR) == 0))))
1351 Elf_Internal_Rela outrel;
1352 boolean skip, relocate;
1354 /* When generating a shared object, these relocations
1355 are copied into the output file to be resolved at run
1356 time. */
1358 if (sreloc == NULL)
1360 const char *name;
1362 name = (bfd_elf_string_from_elf_section
1363 (input_bfd,
1364 elf_elfheader (input_bfd)->e_shstrndx,
1365 elf_section_data (input_section)->rel_hdr.sh_name));
1366 if (name == NULL)
1367 return false;
1369 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1370 && strcmp (bfd_get_section_name (input_bfd,
1371 input_section),
1372 name + 5) == 0);
1374 sreloc = bfd_get_section_by_name (dynobj, name);
1375 BFD_ASSERT (sreloc != NULL);
1378 skip = false;
1380 if (elf_section_data (input_section)->stab_info == NULL)
1381 outrel.r_offset = rela->r_offset;
1382 else
1384 bfd_vma off;
1386 off = (_bfd_stab_section_offset
1387 (output_bfd, &elf_hash_table (info)->stab_info,
1388 input_section,
1389 &elf_section_data (input_section)->stab_info,
1390 rela->r_offset));
1391 if (off == (bfd_vma) -1)
1392 skip = true;
1393 outrel.r_offset = off;
1396 outrel.r_offset += (input_section->output_section->vma
1397 + input_section->output_offset);
1399 if (skip)
1401 memset (&outrel, 0, sizeof outrel);
1402 relocate = false;
1404 else if ((r_type == R_X86_64_PC8) || (r_type == R_X86_64_PC16)
1405 || (r_type == R_X86_64_PC32))
1407 BFD_ASSERT (h != NULL && h->dynindx != -1);
1408 relocate = false;
1409 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
1410 outrel.r_addend = relocation + rela->r_addend;
1412 else
1414 /* h->dynindx may be -1 if this symbol was marked to
1415 become local. */
1416 if (h == NULL
1417 || ((info->symbolic || h->dynindx == -1)
1418 && (h->elf_link_hash_flags
1419 & ELF_LINK_HASH_DEF_REGULAR) != 0))
1421 relocate = true;
1422 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
1423 outrel.r_addend = relocation + rela->r_addend;
1425 else
1427 BFD_ASSERT (h->dynindx != -1);
1428 relocate = false;
1429 outrel.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_32);
1430 outrel.r_addend = relocation + rela->r_addend;
1434 bfd_elf64_swap_reloca_out (output_bfd, &outrel,
1435 (((Elf64_External_Rela *)
1436 sreloc->contents)
1437 + sreloc->reloc_count));
1438 ++sreloc->reloc_count;
1440 /* If this reloc is against an external symbol, we do
1441 not want to fiddle with the addend. Otherwise, we
1442 need to include the symbol value so that it becomes
1443 an addend for the dynamic reloc. */
1444 if (! relocate)
1445 continue;
1448 break;
1450 default:
1451 break;
1454 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1455 contents, rela->r_offset,
1456 relocation, rela->r_addend);
1458 if (r != bfd_reloc_ok)
1460 switch (r)
1462 default:
1463 case bfd_reloc_outofrange:
1464 abort ();
1465 case bfd_reloc_overflow:
1467 const char *name;
1469 if (h != NULL)
1470 name = h->root.root.string;
1471 else
1473 name = bfd_elf_string_from_elf_section (input_bfd,
1474 symtab_hdr->sh_link,
1475 sym->st_name);
1476 if (name == NULL)
1477 return false;
1478 if (*name == '\0')
1479 name = bfd_section_name (input_bfd, sec);
1481 if (! ((*info->callbacks->reloc_overflow)
1482 (info, name, howto->name, (bfd_vma) 0,
1483 input_bfd, input_section, rela->r_offset)))
1484 return false;
1486 break;
1491 return true;
1494 /* Finish up dynamic symbol handling. We set the contents of various
1495 dynamic sections here. */
1497 static boolean
1498 elf64_x86_64_finish_dynamic_symbol (output_bfd, info, h, sym)
1499 bfd *output_bfd;
1500 struct bfd_link_info *info;
1501 struct elf_link_hash_entry *h;
1502 Elf_Internal_Sym *sym;
1504 bfd *dynobj;
1506 dynobj = elf_hash_table (info)->dynobj;
1508 if (h->plt.offset != (bfd_vma) -1)
1510 asection *splt;
1511 asection *sgot;
1512 asection *srela;
1513 bfd_vma plt_index;
1514 bfd_vma got_offset;
1515 Elf_Internal_Rela rela;
1517 /* This symbol has an entry in the procedure linkage table. Set
1518 it up. */
1520 BFD_ASSERT (h->dynindx != -1);
1522 splt = bfd_get_section_by_name (dynobj, ".plt");
1523 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
1524 srela = bfd_get_section_by_name (dynobj, ".rela.plt");
1525 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
1527 /* Get the index in the procedure linkage table which
1528 corresponds to this symbol. This is the index of this symbol
1529 in all the symbols for which we are making plt entries. The
1530 first entry in the procedure linkage table is reserved. */
1531 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
1533 /* Get the offset into the .got table of the entry that
1534 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
1535 bytes. The first three are reserved. */
1536 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
1538 /* Fill in the entry in the procedure linkage table. */
1539 memcpy (splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
1540 PLT_ENTRY_SIZE);
1542 /* Insert the relocation positions of the plt section. The magic
1543 numbers at the end of the statements are the positions of the
1544 relocations in the plt section. */
1545 bfd_put_64 (output_bfd, got_offset, splt->contents + h->plt.offset + 2);
1546 bfd_put_64 (output_bfd, plt_index * sizeof (Elf64_External_Rela),
1547 splt->contents + h->plt.offset + 7);
1548 bfd_put_64 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
1549 splt->contents + h->plt.offset + 12);
1551 /* Fill in the entry in the global offset table. */
1552 bfd_put_64 (output_bfd, (splt->output_section->vma + splt->output_offset
1553 + h->plt.offset + 6),
1554 sgot->contents + got_offset);
1556 /* Fill in the entry in the .rela.plt section. */
1557 rela.r_offset = (sgot->output_section->vma
1558 + sgot->output_offset
1559 + got_offset);
1560 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
1561 rela.r_addend = 0;
1562 bfd_elf64_swap_reloca_out (output_bfd, &rela,
1563 ((Elf64_External_Rela *) srela->contents
1564 + plt_index));
1566 if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
1568 /* Mark the symbol as undefined, rather than as defined in
1569 the .plt section. Leave the value alone. */
1570 sym->st_shndx = SHN_UNDEF;
1574 if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0)
1576 asection *s;
1577 Elf_Internal_Rela rela;
1579 /* This symbol needs a copy reloc. Set it up. */
1581 BFD_ASSERT (h->dynindx != -1
1582 && (h->root.type == bfd_link_hash_defined
1583 || h->root.type == bfd_link_hash_defweak));
1585 s = bfd_get_section_by_name (h->root.u.def.section->owner,
1586 ".rela.bss");
1587 BFD_ASSERT (s != NULL);
1589 rela.r_offset = (h->root.u.def.value
1590 + h->root.u.def.section->output_section->vma
1591 + h->root.u.def.section->output_offset);
1592 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
1593 rela.r_addend = 0;
1594 bfd_elf64_swap_reloca_out (output_bfd, &rela,
1595 ((Elf64_External_Rela *) s->contents
1596 + s->reloc_count));
1597 ++s->reloc_count;
1600 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1601 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
1602 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
1603 sym->st_shndx = SHN_ABS;
1605 return true;
1608 /* Finish up the dynamic sections. */
1610 static boolean
1611 elf64_x86_64_finish_dynamic_sections (output_bfd, info)
1612 bfd *output_bfd;
1613 struct bfd_link_info *info;
1615 bfd *dynobj;
1616 asection *sdyn;
1617 asection *sgot;
1619 dynobj = elf_hash_table (info)->dynobj;
1621 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
1623 if (elf_hash_table (info)->dynamic_sections_created)
1625 asection *splt;
1626 Elf64_External_Dyn *dyncon, *dynconend;
1628 splt = bfd_get_section_by_name (dynobj, ".plt");
1629 BFD_ASSERT (splt != NULL && sdyn != NULL);
1631 dyncon = (Elf64_External_Dyn *) sdyn->contents;
1632 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
1633 for (; dyncon < dynconend; dyncon++)
1635 Elf_Internal_Dyn dyn;
1636 const char *name;
1637 asection *s;
1639 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
1641 switch (dyn.d_tag)
1643 default:
1644 break;
1646 case DT_PLTGOT:
1647 name = ".got";
1648 goto get_vma;
1650 case DT_JMPREL:
1651 name = ".rela.plt";
1653 get_vma:
1654 s = bfd_get_section_by_name (output_bfd, name);
1655 BFD_ASSERT (s != NULL);
1656 dyn.d_un.d_ptr = s->vma;
1657 break;
1659 case DT_RELASZ:
1660 /* FIXME: This comment and code is from elf64-alpha.c: */
1661 /* My interpretation of the TIS v1.1 ELF document indicates
1662 that RELASZ should not include JMPREL. This is not what
1663 the rest of the BFD does. It is, however, what the
1664 glibc ld.so wants. Do this fixup here until we found
1665 out who is right. */
1666 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1667 if (s)
1669 /* Subtract JMPREL size from RELASZ. */
1670 dyn.d_un.d_val -=
1671 (s->_cooked_size ? s->_cooked_size : s->_raw_size);
1673 break;
1675 case DT_PLTRELSZ:
1676 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
1677 BFD_ASSERT (s != NULL);
1678 dyn.d_un.d_val =
1679 (s->_cooked_size != 0 ? s->_cooked_size : s->_raw_size);
1680 break;
1683 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
1686 /* Initialize the contents of the .plt section. */
1687 if (splt->_raw_size > 0)
1689 memcpy (splt->contents, elf64_x86_64_plt0_entry, PLT_ENTRY_SIZE);
1692 elf_section_data (splt->output_section)->this_hdr.sh_entsize =
1693 PLT_ENTRY_SIZE;
1696 /* Set the first entry in the global offset table to the address of
1697 the dynamic section. */
1698 sgot = bfd_get_section_by_name (dynobj, ".got");
1699 BFD_ASSERT (sgot != NULL);
1700 if (sgot->_raw_size > 0)
1702 if (sdyn == NULL)
1703 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents);
1704 else
1705 bfd_put_64 (output_bfd,
1706 sdyn->output_section->vma + sdyn->output_offset,
1707 sgot->contents);
1708 /* Write GOT[1] and GOT[2], needed for the linker. */
1709 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE);
1710 bfd_put_64 (output_bfd, (bfd_vma) 0, sgot->contents + GOT_ENTRY_SIZE*2);
1713 elf_section_data (sgot->output_section)->this_hdr.sh_entsize =
1714 GOT_ENTRY_SIZE;
1716 return true;
1719 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
1720 #define TARGET_LITTLE_NAME "elf64-x86-64"
1721 #define ELF_ARCH bfd_arch_i386
1722 #define ELF_MACHINE_CODE EM_X86_64
1723 #define ELF_MAXPAGESIZE 0x100000
1725 #define elf_backend_can_gc_sections 1
1726 #define elf_backend_want_got_plt 1
1727 #define elf_backend_plt_readonly 1
1728 #define elf_backend_want_plt_sym 0
1729 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
1730 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
1732 #define elf_info_to_howto elf64_x86_64_info_to_howto
1733 #define elf_backend_object_p elf64_x86_64_elf_object_p
1734 #define elf_backend_relocate_section elf64_x86_64_relocate_section
1736 #define bfd_elf64_bfd_final_link _bfd_elf64_gc_common_final_link
1737 #define bfd_elf64_bfd_link_hash_table_create \
1738 elf64_x86_64_link_hash_table_create
1739 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
1741 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
1742 #define elf_backend_check_relocs elf64_x86_64_check_relocs
1743 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
1744 #define elf_backend_finish_dynamic_sections \
1745 elf64_x86_64_finish_dynamic_sections
1746 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
1747 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
1748 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
1749 #define elf_backend_relocate_section elf64_x86_64_relocate_section
1750 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
1752 #include "elf64-target.h"