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. */
26 #include "elf/x86-64.h"
28 /* We use only the RELA entries. */
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_GOTPCREL",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. */
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
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
;
114 for (i
= 0; i
< sizeof (x86_64_reloc_map
) / sizeof (struct elf_reloc_map
);
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
];
124 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
127 elf64_x86_64_info_to_howto (abfd
, cache_ptr
, dst
)
128 bfd
*abfd ATTRIBUTE_UNUSED
;
130 Elf64_Internal_Rela
*dst
;
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
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
189 struct elf64_x86_64_pcrel_relocs_copied
*next
;
190 /* A section in dynobj. */
192 /* Number of relocs copied in this section. */
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 \
222 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
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
;
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
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
,
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
)
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
)
275 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
276 elf64_x86_64_link_hash_newfunc
))
278 bfd_release (abfd
, ret
);
282 return &ret
->root
.root
;
286 elf64_x86_64_elf_object_p (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
);
294 /* Look through the relocs for a section during the first phase, and
295 allocate space in the global offset table or procedure linkage
299 elf64_x86_64_check_relocs (abfd
, info
, sec
, relocs
)
301 struct bfd_link_info
*info
;
303 const Elf_Internal_Rela
*relocs
;
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
;
315 if (info
->relocateable
)
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
)
334 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
336 /* Some relocs require a global offset table. */
339 switch (ELF64_R_TYPE (rel
->r_info
))
342 case R_X86_64_GOTPCREL
:
343 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
344 if (! _bfd_elf_create_got_section (dynobj
, info
))
350 switch (ELF64_R_TYPE (rel
->r_info
))
352 case R_X86_64_GOTPCREL
:
354 /* This symbol requires a global offset table entry. */
358 sgot
= bfd_get_section_by_name (dynobj
, ".got");
359 BFD_ASSERT (sgot
!= NULL
);
362 if (srelgot
== NULL
&& (h
!= NULL
|| info
->shared
))
364 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
367 srelgot
= bfd_make_section (dynobj
, ".rela.got");
369 || ! bfd_set_section_flags (dynobj
, srelgot
,
376 || ! bfd_set_section_alignment (dynobj
, srelgot
, 3))
383 if (h
->got
.refcount
== -1)
387 /* Make sure this symbol is output as a dynamic symbol. */
388 if (h
->dynindx
== -1)
390 if (! bfd_elf64_link_record_dynamic_symbol (info
, h
))
394 sgot
->_raw_size
+= GOT_ENTRY_SIZE
;
395 srelgot
->_raw_size
+= sizeof (Elf64_External_Rela
);
398 h
->got
.refcount
+= 1;
402 /* This is a global offset table entry for a local symbol. */
403 if (local_got_refcounts
== NULL
)
407 size
= symtab_hdr
->sh_info
* sizeof (bfd_signed_vma
);
408 local_got_refcounts
= ((bfd_signed_vma
*)
409 bfd_alloc (abfd
, size
));
410 if (local_got_refcounts
== NULL
)
412 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
413 memset (local_got_refcounts
, -1, size
);
415 if (local_got_refcounts
[r_symndx
] == -1)
417 local_got_refcounts
[r_symndx
] = 1;
419 sgot
->_raw_size
+= GOT_ENTRY_SIZE
;
422 /* If we are generating a shared object, we need to
423 output a R_X86_64_RELATIVE reloc so that the dynamic
424 linker can adjust this GOT entry. */
425 srelgot
->_raw_size
+= sizeof (Elf64_External_Rela
);
429 local_got_refcounts
[r_symndx
] += 1;
434 /* This symbol requires a procedure linkage table entry. We
435 actually build the entry in adjust_dynamic_symbol,
436 because this might be a case of linking PIC code which is
437 never referenced by a dynamic object, in which case we
438 don't need to generate a procedure linkage table entry
441 /* If this is a local symbol, we resolve it directly without
442 creating a procedure linkage table entry. */
446 if (h
->plt
.refcount
== -1)
449 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
452 h
->plt
.refcount
+= 1;
459 h
->elf_link_hash_flags
|= ELF_LINK_NON_GOT_REF
;
461 /* If we are creating a shared library, and this is a reloc
462 against a global symbol, or a non PC relative reloc
463 against a local symbol, then we need to copy the reloc
464 into the shared library. However, if we are linking with
465 -Bsymbolic, we do not need to copy a reloc against a
466 global symbol which is defined in an object we are
467 including in the link (i.e., DEF_REGULAR is set). At
468 this point we have not seen all the input files, so it is
469 possible that DEF_REGULAR is not set now but will be set
470 later (it is never cleared). We account for that
471 possibility below by storing information in the
472 pcrel_relocs_copied field of the hash table entry.
473 A similar situation occurs when creating shared libraries
474 and symbol visibility changes render the symbol local. */
476 && (sec
->flags
& SEC_ALLOC
) != 0
477 && (ELF64_R_TYPE (rel
->r_info
) != R_X86_64_PC32
480 || (h
->elf_link_hash_flags
481 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
483 /* When creating a shared object, we must copy these
484 reloc types into the output file. We create a reloc
485 section in dynobj and make room for this reloc. */
490 name
= (bfd_elf_string_from_elf_section
492 elf_elfheader (abfd
)->e_shstrndx
,
493 elf_section_data (sec
)->rel_hdr
.sh_name
));
497 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
498 && strcmp (bfd_get_section_name (abfd
, sec
),
501 sreloc
= bfd_get_section_by_name (dynobj
, name
);
506 sreloc
= bfd_make_section (dynobj
, name
);
507 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
508 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
509 if ((sec
->flags
& SEC_ALLOC
) != 0)
510 flags
|= SEC_ALLOC
| SEC_LOAD
;
512 || ! bfd_set_section_flags (dynobj
, sreloc
, flags
)
513 || ! bfd_set_section_alignment (dynobj
, sreloc
, 2))
518 sreloc
->_raw_size
+= sizeof (Elf64_External_Rela
);
520 /* If this is a global symbol, we count the number of PC
521 relative relocations we have entered for this symbol,
522 so that we can discard them later as necessary. Note
523 that this function is only called if we are using an
524 elf64_x86_64 linker hash table, which means that h is
525 really a pointer to an elf64_x86_64_link_hash_entry. */
527 && ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC32
)
529 struct elf64_x86_64_link_hash_entry
*eh
;
530 struct elf64_x86_64_pcrel_relocs_copied
*p
;
532 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
534 for (p
= eh
->pcrel_relocs_copied
; p
!= NULL
; p
= p
->next
)
535 if (p
->section
== sreloc
)
540 p
= ((struct elf64_x86_64_pcrel_relocs_copied
*)
541 bfd_alloc (dynobj
, sizeof *p
));
544 p
->next
= eh
->pcrel_relocs_copied
;
545 eh
->pcrel_relocs_copied
= p
;
560 /* Return the section that should be marked against GC for a given
564 elf64_x86_64_gc_mark_hook (abfd
, info
, rel
, h
, sym
)
566 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
567 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
;
568 struct elf_link_hash_entry
*h
;
569 Elf_Internal_Sym
*sym
;
573 switch (h
->root
.type
)
575 case bfd_link_hash_defined
:
576 case bfd_link_hash_defweak
:
577 return h
->root
.u
.def
.section
;
579 case bfd_link_hash_common
:
580 return h
->root
.u
.c
.p
->section
;
588 if (!(elf_bad_symtab (abfd
)
589 && ELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
590 && ! ((sym
->st_shndx
<= 0 || sym
->st_shndx
>= SHN_LORESERVE
)
591 && sym
->st_shndx
!= SHN_COMMON
))
593 return bfd_section_from_elf_index (abfd
, sym
->st_shndx
);
600 /* Update the got entry reference counts for the section being removed. */
603 elf64_x86_64_gc_sweep_hook (abfd
, info
, sec
, relocs
)
605 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
607 const Elf_Internal_Rela
*relocs
;
609 Elf_Internal_Shdr
*symtab_hdr
;
610 struct elf_link_hash_entry
**sym_hashes
;
611 bfd_signed_vma
*local_got_refcounts
;
612 const Elf_Internal_Rela
*rel
, *relend
;
613 unsigned long r_symndx
;
614 struct elf_link_hash_entry
*h
;
619 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
620 sym_hashes
= elf_sym_hashes (abfd
);
621 local_got_refcounts
= elf_local_got_refcounts (abfd
);
623 dynobj
= elf_hash_table (info
)->dynobj
;
627 sgot
= bfd_get_section_by_name (dynobj
, ".got");
628 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
630 relend
= relocs
+ sec
->reloc_count
;
631 for (rel
= relocs
; rel
< relend
; rel
++)
632 switch (ELF64_R_TYPE (rel
->r_info
))
635 case R_X86_64_GOTPCREL
:
636 r_symndx
= ELF64_R_SYM (rel
->r_info
);
637 if (r_symndx
>= symtab_hdr
->sh_info
)
639 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
640 if (h
->got
.refcount
> 0)
642 h
->got
.refcount
-= 1;
643 if (h
->got
.refcount
== 0)
645 sgot
->_raw_size
-= GOT_ENTRY_SIZE
;
646 srelgot
->_raw_size
-= sizeof (Elf64_External_Rela
);
650 else if (local_got_refcounts
!= NULL
)
652 if (local_got_refcounts
[r_symndx
] > 0)
654 local_got_refcounts
[r_symndx
] -= 1;
655 if (local_got_refcounts
[r_symndx
] == 0)
657 sgot
->_raw_size
-= GOT_ENTRY_SIZE
;
659 srelgot
->_raw_size
-= sizeof (Elf64_External_Rela
);
666 r_symndx
= ELF64_R_SYM (rel
->r_info
);
667 if (r_symndx
>= symtab_hdr
->sh_info
)
669 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
670 if (h
->plt
.refcount
> 0)
671 h
->plt
.refcount
-= 1;
682 /* Adjust a symbol defined by a dynamic object and referenced by a
683 regular object. The current definition is in some section of the
684 dynamic object, but we're not including those sections. We have to
685 change the definition to something the rest of the link can
689 elf64_x86_64_adjust_dynamic_symbol (info
, h
)
690 struct bfd_link_info
*info
;
691 struct elf_link_hash_entry
*h
;
695 unsigned int power_of_two
;
697 dynobj
= elf_hash_table (info
)->dynobj
;
699 /* Make sure we know what is going on here. */
700 BFD_ASSERT (dynobj
!= NULL
701 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
)
702 || h
->weakdef
!= NULL
703 || ((h
->elf_link_hash_flags
704 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
705 && (h
->elf_link_hash_flags
706 & ELF_LINK_HASH_REF_REGULAR
) != 0
707 && (h
->elf_link_hash_flags
708 & ELF_LINK_HASH_DEF_REGULAR
) == 0)));
710 /* If this is a function, put it in the procedure linkage table. We
711 will fill in the contents of the procedure linkage table later,
712 when we know the address of the .got section. */
713 if (h
->type
== STT_FUNC
714 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
716 if (! elf_hash_table (info
)->dynamic_sections_created
)
718 /* FIXME: These are the sparc64 comment and then the i386 comment.
719 How we need to deal with this and why remains to be seen. */
720 /* This case can occur if we saw a WPLT30 reloc in an input
721 file, but none of the input files were dynamic objects.
722 In such a case, we don't actually need to build a
723 procedure linkage table, and we can just do a WDISP30
725 /* This case can occur if we saw a PLT32 reloc in an input
726 file, but the symbol was never referred to by a dynamic
727 object, or if all references were garbage collected. In
728 such a case, we don't actually need to build a procedure
729 linkage table, and we can just do a PC32 reloc instead. */
731 h
->plt
.offset
= (bfd_vma
) -1;
732 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
736 /* Make sure this symbol is output as a dynamic symbol. */
737 if (h
->dynindx
== -1)
739 if (! bfd_elf64_link_record_dynamic_symbol (info
, h
))
743 s
= bfd_get_section_by_name (dynobj
, ".plt");
744 BFD_ASSERT (s
!= NULL
);
746 /* If this is the first .plt entry, make room for the special
748 if (s
->_raw_size
== 0)
749 s
->_raw_size
= PLT_ENTRY_SIZE
;
751 /* If this symbol is not defined in a regular file, and we are
752 not generating a shared library, then set the symbol to this
753 location in the .plt. This is required to make function
754 pointers compare as equal between the normal executable and
755 the shared library. */
757 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
759 h
->root
.u
.def
.section
= s
;
760 h
->root
.u
.def
.value
= s
->_raw_size
;
763 h
->plt
.offset
= s
->_raw_size
;
765 /* Make room for this entry. */
766 s
->_raw_size
+= PLT_ENTRY_SIZE
;
768 /* We also need to make an entry in the .rela.plt section. */
769 s
= bfd_get_section_by_name (dynobj
, ".rela.plt");
770 BFD_ASSERT (s
!= NULL
);
771 s
->_raw_size
+= sizeof (Elf64_External_Rela
);
776 /* If this is a weak symbol, and there is a real definition, the
777 processor independent code will have arranged for us to see the
778 real definition first, and we can just use the same value. */
779 if (h
->weakdef
!= NULL
)
781 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
782 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
783 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
784 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
788 /* This is a reference to a symbol defined by a dynamic object which
789 is not a function. */
791 /* If we are creating a shared library, we must presume that the
792 only references to the symbol are via the global offset table.
793 For such cases we need not do anything here; the relocations will
794 be handled correctly by relocate_section. */
798 /* If there are no references to this symbol that do not use the
799 GOT, we don't need to generate a copy reloc. */
800 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0)
803 /* We must allocate the symbol in our .dynbss section, which will
804 become part of the .bss section of the executable. There will be
805 an entry for this symbol in the .dynsym section. The dynamic
806 object will contain position independent code, so all references
807 from the dynamic object to this symbol will go through the global
808 offset table. The dynamic linker will use the .dynsym entry to
809 determine the address it must put in the global offset table, so
810 both the dynamic object and the regular object will refer to the
811 same memory location for the variable. */
813 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
814 BFD_ASSERT (s
!= NULL
);
816 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
817 to copy the initial value out of the dynamic object and into the
818 runtime process image. We need to remember the offset into the
819 .rela.bss section we are going to use. */
820 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
824 srel
= bfd_get_section_by_name (dynobj
, ".rela.bss");
825 BFD_ASSERT (srel
!= NULL
);
826 srel
->_raw_size
+= sizeof (Elf64_External_Rela
);
827 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
830 /* We need to figure out the alignment required for this symbol. I
831 have no idea how ELF linkers handle this. 16-bytes is the size
832 of the largest type that requires hard alignment -- long double. */
833 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
835 power_of_two
= bfd_log2 (h
->size
);
836 if (power_of_two
> 4)
839 /* Apply the required alignment. */
840 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
, (bfd_size_type
) (1 << power_of_two
));
841 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
843 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
847 /* Define the symbol as being at this point in the section. */
848 h
->root
.u
.def
.section
= s
;
849 h
->root
.u
.def
.value
= s
->_raw_size
;
851 /* Increment the section size to make room for the symbol. */
852 s
->_raw_size
+= h
->size
;
857 /* Set the sizes of the dynamic sections. */
860 elf64_x86_64_size_dynamic_sections (output_bfd
, info
)
862 struct bfd_link_info
*info
;
870 dynobj
= elf_hash_table (info
)->dynobj
;
871 BFD_ASSERT (dynobj
!= NULL
);
873 if (elf_hash_table (info
)->dynamic_sections_created
)
875 /* Set the contents of the .interp section to the interpreter. */
878 s
= bfd_get_section_by_name (dynobj
, ".interp");
879 BFD_ASSERT (s
!= NULL
);
880 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
881 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
886 /* We may have created entries in the .rela.got section.
887 However, if we are not creating the dynamic sections, we will
888 not actually use these entries. Reset the size of .rela.got,
889 which will cause it to get stripped from the output file
891 s
= bfd_get_section_by_name (dynobj
, ".rela.got");
896 /* If this is a -Bsymbolic shared link, then we need to discard all
897 PC relative relocs against symbols defined in a regular object.
898 We allocated space for them in the check_relocs routine, but we
899 will not fill them in in the relocate_section routine. */
901 elf64_x86_64_link_hash_traverse (elf64_x86_64_hash_table (info
),
902 elf64_x86_64_discard_copies
,
905 /* The check_relocs and adjust_dynamic_symbol entry points have
906 determined the sizes of the various dynamic sections. Allocate
908 plt
= relocs
= reltext
= false;
909 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
914 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
917 /* It's OK to base decisions on the section name, because none
918 of the dynobj section names depend upon the input files. */
919 name
= bfd_get_section_name (dynobj
, s
);
922 if (strcmp (name
, ".plt") == 0)
924 if (s
->_raw_size
== 0)
926 /* Strip this section if we don't need it; see the
932 /* Remember whether there is a PLT. */
936 else if (strncmp (name
, ".rela", 5) == 0)
938 if (s
->_raw_size
== 0)
940 /* If we don't need this section, strip it from the
941 output file. This is mostly to handle .rela.bss and
942 .rela.plt. We must create both sections in
943 create_dynamic_sections, because they must be created
944 before the linker maps input sections to output
945 sections. The linker does that before
946 adjust_dynamic_symbol is called, and it is that
947 function which decides whether anything needs to go
948 into these sections. */
955 /* Remember whether there are any reloc sections other
957 if (strcmp (name
, ".rela.plt") != 0)
963 /* If this relocation section applies to a read only
964 section, then we probably need a DT_TEXTREL
965 entry. The entries in the .rela.plt section
966 really apply to the .got section, which we
967 created ourselves and so know is not readonly. */
968 outname
= bfd_get_section_name (output_bfd
,
970 target
= bfd_get_section_by_name (output_bfd
, outname
+ 5);
972 && (target
->flags
& SEC_READONLY
) != 0
973 && (target
->flags
& SEC_ALLOC
) != 0)
977 /* We use the reloc_count field as a counter if we need
978 to copy relocs into the output file. */
982 else if (strncmp (name
, ".got", 4) != 0)
984 /* It's not one of our sections, so don't allocate space. */
990 _bfd_strip_section_from_output (info
, s
);
994 /* Allocate memory for the section contents. We use bfd_zalloc
995 here in case unused entries are not reclaimed before the
996 section's contents are written out. This should not happen,
997 but this way if it does, we get a R_X86_64_NONE reloc instead
999 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->_raw_size
);
1000 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
1004 if (elf_hash_table (info
)->dynamic_sections_created
)
1006 /* Add some entries to the .dynamic section. We fill in the
1007 values later, in elf64_x86_64_finish_dynamic_sections, but we
1008 must add the entries now so that we get the correct size for
1009 the .dynamic section. The DT_DEBUG entry is filled in by the
1010 dynamic linker and used by the debugger. */
1013 if (! bfd_elf64_add_dynamic_entry (info
, DT_DEBUG
, 0))
1019 /* FIXME: Are all these needed? */
1020 if (! bfd_elf64_add_dynamic_entry (info
, DT_PLTGOT
, 0)
1021 || ! bfd_elf64_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
1022 || ! bfd_elf64_add_dynamic_entry (info
, DT_PLTREL
, DT_REL
)
1023 || ! bfd_elf64_add_dynamic_entry (info
, DT_JMPREL
, 0))
1029 if (! bfd_elf64_add_dynamic_entry (info
, DT_RELA
, 0)
1030 || ! bfd_elf64_add_dynamic_entry (info
, DT_RELASZ
, 0)
1031 || ! bfd_elf64_add_dynamic_entry (info
, DT_RELAENT
,
1032 sizeof (Elf64_External_Rela
)))
1038 if (! bfd_elf64_add_dynamic_entry (info
, DT_TEXTREL
, 0))
1040 info
->flags
|= DF_TEXTREL
;
1047 /* This function is called via elf64_x86_64_link_hash_traverse if we are
1048 creating a shared object. In the -Bsymbolic case, it discards the
1049 space allocated to copy PC relative relocs against symbols which
1050 are defined in regular objects. For the normal non-symbolic case,
1051 we also discard space for relocs that have become local due to
1052 symbol visibility changes. We allocated space for them in the
1053 check_relocs routine, but we won't fill them in in the
1054 relocate_section routine. */
1057 elf64_x86_64_discard_copies (h
, inf
)
1058 struct elf64_x86_64_link_hash_entry
*h
;
1061 struct elf64_x86_64_pcrel_relocs_copied
*s
;
1062 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
1064 /* If a symbol has been forced local or we have found a regular
1065 definition for the symbolic link case, then we won't be needing
1067 if ((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
1068 && ((h
->root
.elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0
1071 for (s
= h
->pcrel_relocs_copied
; s
!= NULL
; s
= s
->next
)
1072 s
->section
->_raw_size
-= s
->count
* sizeof (Elf64_External_Rela
);
1078 /* Relocate an x86_64 ELF section. */
1081 elf64_x86_64_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1082 contents
, relocs
, local_syms
, local_sections
)
1084 struct bfd_link_info
*info
;
1086 asection
*input_section
;
1088 Elf_Internal_Rela
*relocs
;
1089 Elf_Internal_Sym
*local_syms
;
1090 asection
**local_sections
;
1093 Elf_Internal_Shdr
*symtab_hdr
;
1094 struct elf_link_hash_entry
**sym_hashes
;
1095 bfd_vma
*local_got_offsets
;
1099 Elf_Internal_Rela
*rela
;
1100 Elf_Internal_Rela
*relend
;
1102 dynobj
= elf_hash_table (info
)->dynobj
;
1103 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1104 sym_hashes
= elf_sym_hashes (input_bfd
);
1105 local_got_offsets
= elf_local_got_offsets (input_bfd
);
1107 sreloc
= splt
= sgot
= NULL
;
1110 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1111 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1115 relend
= relocs
+ input_section
->reloc_count
;
1116 for (; rela
< relend
; rela
++)
1119 reloc_howto_type
*howto
;
1120 unsigned long r_symndx
;
1121 struct elf_link_hash_entry
*h
;
1122 Elf_Internal_Sym
*sym
;
1125 bfd_reloc_status_type r
;
1128 r_type
= ELF64_R_TYPE (rela
->r_info
);
1130 if ((indx
= (unsigned) r_type
) >= R_X86_64_max
)
1132 bfd_set_error (bfd_error_bad_value
);
1135 howto
= x86_64_elf_howto_table
+ indx
;
1137 r_symndx
= ELF64_R_SYM (rela
->r_info
);
1139 if (info
->relocateable
)
1141 /* This is a relocateable link. We don't have to change
1142 anything, unless the reloc is against a section symbol,
1143 in which case we have to adjust according to where the
1144 section symbol winds up in the output section. */
1145 if (r_symndx
< symtab_hdr
->sh_info
)
1147 sym
= local_syms
+ r_symndx
;
1148 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1150 sec
= local_sections
[r_symndx
];
1151 rela
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
1158 /* This is a final link. */
1162 if (r_symndx
< symtab_hdr
->sh_info
)
1164 sym
= local_syms
+ r_symndx
;
1165 sec
= local_sections
[r_symndx
];
1166 relocation
= (sec
->output_section
->vma
1167 + sec
->output_offset
1172 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1173 while (h
->root
.type
== bfd_link_hash_indirect
1174 || h
->root
.type
== bfd_link_hash_warning
)
1175 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1176 if (h
->root
.type
== bfd_link_hash_defined
1177 || h
->root
.type
== bfd_link_hash_defweak
)
1179 sec
= h
->root
.u
.def
.section
;
1180 if (sec
->output_section
== NULL
)
1182 (*_bfd_error_handler
)
1183 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1184 bfd_get_filename (input_bfd
), h
->root
.root
.string
,
1185 bfd_get_section_name (input_bfd
, input_section
));
1189 relocation
= (h
->root
.u
.def
.value
1190 + sec
->output_section
->vma
1191 + sec
->output_offset
);
1193 else if (h
->root
.type
== bfd_link_hash_undefweak
)
1195 else if (info
->shared
&& !info
->symbolic
&& !info
->no_undefined
1196 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
1200 if (! ((*info
->callbacks
->undefined_symbol
)
1201 (info
, h
->root
.root
.string
, input_bfd
,
1202 input_section
, rela
->r_offset
,
1203 (!info
->shared
|| info
->no_undefined
1204 || ELF_ST_VISIBILITY (h
->other
)))))
1210 /* When generating a shared object, the relocations handled here are
1211 copied into the output file to be resolved at run time. */
1214 case R_X86_64_GOT32
:
1215 /* Relocation is to the entry for this symbol in the global
1217 BFD_ASSERT (sgot
!= NULL
);
1221 bfd_vma off
= h
->got
.offset
;
1222 BFD_ASSERT (off
!= (bfd_vma
) -1);
1224 if (! elf_hash_table (info
)->dynamic_sections_created
1226 && (info
->symbolic
|| h
->dynindx
== -1)
1227 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
1229 /* This is actually a static link, or it is a -Bsymbolic
1230 link and the symbol is defined locally, or the symbol
1231 was forced to be local because of a version file. We
1232 must initialize this entry in the global offset table.
1233 Since the offset must always be a multiple of 8, we
1234 use the least significant bit to record whether we
1235 have initialized it already.
1237 When doing a dynamic link, we create a .rela.got
1238 relocation entry to initialize the value. This is
1239 done in the finish_dynamic_symbol routine. */
1244 bfd_put_64 (output_bfd
, relocation
,
1245 sgot
->contents
+ off
);
1249 relocation
= sgot
->output_offset
+ off
;
1255 BFD_ASSERT (local_got_offsets
!= NULL
1256 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1258 off
= local_got_offsets
[r_symndx
];
1260 /* The offset must always be a multiple of 8. We use
1261 the least significant bit to record whether we have
1262 already generated the necessary reloc. */
1267 bfd_put_64 (output_bfd
, relocation
, sgot
->contents
+ off
);
1272 Elf_Internal_Rela outrel
;
1274 /* We need to generate a R_X86_64_RELATIVE reloc
1275 for the dynamic linker. */
1276 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
1277 BFD_ASSERT (srelgot
!= NULL
);
1279 outrel
.r_offset
= (sgot
->output_section
->vma
1280 + sgot
->output_offset
1282 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
1283 outrel
.r_addend
= relocation
;
1284 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
,
1285 (((Elf64_External_Rela
*)
1287 + srelgot
->reloc_count
));
1288 ++srelgot
->reloc_count
;
1291 local_got_offsets
[r_symndx
] |= 1;
1294 relocation
= sgot
->output_offset
+ off
;
1299 case R_X86_64_GOTPCREL
:
1300 /* Use global offset table as symbol value. */
1302 BFD_ASSERT (sgot
!= NULL
);
1305 bfd_vma off
= h
->got
.offset
;
1306 BFD_ASSERT (off
!= (bfd_vma
) -1);
1308 if (! elf_hash_table (info
)->dynamic_sections_created
1310 && (info
->symbolic
|| h
->dynindx
== -1)
1311 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
1313 /* This is actually a static link, or it is a -Bsymbolic
1314 link and the symbol is defined locally, or the symbol
1315 was forced to be local because of a version file. We
1316 must initialize this entry in the global offset table.
1317 Since the offset must always be a multiple of 8, we
1318 use the least significant bit to record whether we
1319 have initialized it already.
1321 When doing a dynamic link, we create a .rela.got
1322 relocation entry to initialize the value. This is
1323 done in the finish_dynamic_symbol routine. */
1328 bfd_put_64 (output_bfd
, relocation
,
1329 sgot
->contents
+ off
);
1333 relocation
= sgot
->output_offset
+ off
;
1339 BFD_ASSERT (local_got_offsets
!= NULL
1340 && local_got_offsets
[r_symndx
] != (bfd_vma
) -1);
1342 off
= local_got_offsets
[r_symndx
];
1344 /* The offset must always be a multiple of 8. We use
1345 the least significant bit to record whether we have
1346 already generated the necessary reloc. */
1351 bfd_put_64 (output_bfd
, relocation
, sgot
->contents
+ off
);
1356 Elf_Internal_Rela outrel
;
1358 /* We need to generate a R_X86_64_RELATIVE reloc
1359 for the dynamic linker. */
1360 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
1361 BFD_ASSERT (srelgot
!= NULL
);
1363 outrel
.r_offset
= (sgot
->output_section
->vma
1364 + sgot
->output_offset
1366 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
1367 outrel
.r_addend
= relocation
;
1368 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
,
1369 (((Elf64_External_Rela
*)
1371 + srelgot
->reloc_count
));
1372 ++srelgot
->reloc_count
;
1375 local_got_offsets
[r_symndx
] |= 1;
1378 relocation
= sgot
->output_section
->vma
+ off
;
1382 case R_X86_64_PLT32
:
1383 /* Relocation is to the entry for this symbol in the
1384 procedure linkage table. */
1386 /* Resolve a PLT32 reloc against a local symbol directly,
1387 without using the procedure linkage table. */
1391 if (h
->plt
.offset
== (bfd_vma
) -1 || splt
== NULL
)
1393 /* We didn't make a PLT entry for this symbol. This
1394 happens when statically linking PIC code, or when
1395 using -Bsymbolic. */
1399 relocation
= (splt
->output_section
->vma
1400 + splt
->output_offset
1410 /* FIXME: The abi says the linker should make sure the value is
1411 the same when it's zeroextended to 64 bit. */
1413 && (input_section
->flags
& SEC_ALLOC
) != 0
1414 && ((r_type
!= R_X86_64_PC8
&& r_type
!= R_X86_64_PC16
1415 && r_type
!= R_X86_64_PC32
)
1418 && (! info
->symbolic
1419 || (h
->elf_link_hash_flags
1420 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
1422 Elf_Internal_Rela outrel
;
1423 boolean skip
, relocate
;
1425 /* When generating a shared object, these relocations
1426 are copied into the output file to be resolved at run
1433 name
= (bfd_elf_string_from_elf_section
1435 elf_elfheader (input_bfd
)->e_shstrndx
,
1436 elf_section_data (input_section
)->rel_hdr
.sh_name
));
1440 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
1441 && strcmp (bfd_get_section_name (input_bfd
,
1445 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1446 BFD_ASSERT (sreloc
!= NULL
);
1451 if (elf_section_data (input_section
)->stab_info
== NULL
)
1452 outrel
.r_offset
= rela
->r_offset
;
1457 off
= (_bfd_stab_section_offset
1458 (output_bfd
, &elf_hash_table (info
)->stab_info
,
1460 &elf_section_data (input_section
)->stab_info
,
1462 if (off
== (bfd_vma
) -1)
1464 outrel
.r_offset
= off
;
1467 outrel
.r_offset
+= (input_section
->output_section
->vma
1468 + input_section
->output_offset
);
1472 memset (&outrel
, 0, sizeof outrel
);
1475 else if ((r_type
== R_X86_64_PC8
) || (r_type
== R_X86_64_PC16
)
1476 || (r_type
== R_X86_64_PC32
))
1478 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
1480 outrel
.r_info
= ELF64_R_INFO (h
->dynindx
, r_type
);
1481 outrel
.r_addend
= rela
->r_addend
;
1485 /* h->dynindx may be -1 if this symbol was marked to
1488 || ((info
->symbolic
|| h
->dynindx
== -1)
1489 && (h
->elf_link_hash_flags
1490 & ELF_LINK_HASH_DEF_REGULAR
) != 0))
1493 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
1494 outrel
.r_addend
= rela
->r_addend
;
1498 BFD_ASSERT (h
->dynindx
!= -1);
1500 outrel
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_32
);
1501 outrel
.r_addend
= rela
->r_addend
;
1505 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
,
1506 (((Elf64_External_Rela
*)
1508 + sreloc
->reloc_count
));
1509 ++sreloc
->reloc_count
;
1511 /* If this reloc is against an external symbol, we do
1512 not want to fiddle with the addend. Otherwise, we
1513 need to include the symbol value so that it becomes
1514 an addend for the dynamic reloc. */
1525 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1526 contents
, rela
->r_offset
,
1527 relocation
, rela
->r_addend
);
1529 if (r
!= bfd_reloc_ok
)
1534 case bfd_reloc_outofrange
:
1536 case bfd_reloc_overflow
:
1541 name
= h
->root
.root
.string
;
1544 name
= bfd_elf_string_from_elf_section (input_bfd
,
1545 symtab_hdr
->sh_link
,
1550 name
= bfd_section_name (input_bfd
, sec
);
1552 if (! ((*info
->callbacks
->reloc_overflow
)
1553 (info
, name
, howto
->name
, (bfd_vma
) 0,
1554 input_bfd
, input_section
, rela
->r_offset
)))
1565 /* Finish up dynamic symbol handling. We set the contents of various
1566 dynamic sections here. */
1569 elf64_x86_64_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
1571 struct bfd_link_info
*info
;
1572 struct elf_link_hash_entry
*h
;
1573 Elf_Internal_Sym
*sym
;
1577 dynobj
= elf_hash_table (info
)->dynobj
;
1579 if (h
->plt
.offset
!= (bfd_vma
) -1)
1586 Elf_Internal_Rela rela
;
1588 /* This symbol has an entry in the procedure linkage table. Set
1591 BFD_ASSERT (h
->dynindx
!= -1);
1593 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1594 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
1595 srela
= bfd_get_section_by_name (dynobj
, ".rela.plt");
1596 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srela
!= NULL
);
1598 /* Get the index in the procedure linkage table which
1599 corresponds to this symbol. This is the index of this symbol
1600 in all the symbols for which we are making plt entries. The
1601 first entry in the procedure linkage table is reserved. */
1602 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
1604 /* Get the offset into the .got table of the entry that
1605 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
1606 bytes. The first three are reserved. */
1607 got_offset
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
1609 /* Fill in the entry in the procedure linkage table. */
1610 memcpy (splt
->contents
+ h
->plt
.offset
, elf64_x86_64_plt_entry
,
1613 /* Insert the relocation positions of the plt section. The magic
1614 numbers at the end of the statements are the positions of the
1615 relocations in the plt section. */
1616 bfd_put_64 (output_bfd
, got_offset
, splt
->contents
+ h
->plt
.offset
+ 2);
1617 bfd_put_64 (output_bfd
, plt_index
* sizeof (Elf64_External_Rela
),
1618 splt
->contents
+ h
->plt
.offset
+ 7);
1619 bfd_put_64 (output_bfd
, - (h
->plt
.offset
+ PLT_ENTRY_SIZE
),
1620 splt
->contents
+ h
->plt
.offset
+ 12);
1622 /* Fill in the entry in the global offset table. */
1623 bfd_put_64 (output_bfd
, (splt
->output_section
->vma
+ splt
->output_offset
1624 + h
->plt
.offset
+ 6),
1625 sgot
->contents
+ got_offset
);
1627 /* Fill in the entry in the .rela.plt section. */
1628 rela
.r_offset
= (sgot
->output_section
->vma
1629 + sgot
->output_offset
1631 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_JUMP_SLOT
);
1633 bfd_elf64_swap_reloca_out (output_bfd
, &rela
,
1634 ((Elf64_External_Rela
*) srela
->contents
1637 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1639 /* Mark the symbol as undefined, rather than as defined in
1640 the .plt section. Leave the value alone. */
1641 sym
->st_shndx
= SHN_UNDEF
;
1645 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
1648 Elf_Internal_Rela rela
;
1650 /* This symbol needs a copy reloc. Set it up. */
1652 BFD_ASSERT (h
->dynindx
!= -1
1653 && (h
->root
.type
== bfd_link_hash_defined
1654 || h
->root
.type
== bfd_link_hash_defweak
));
1656 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
1658 BFD_ASSERT (s
!= NULL
);
1660 rela
.r_offset
= (h
->root
.u
.def
.value
1661 + h
->root
.u
.def
.section
->output_section
->vma
1662 + h
->root
.u
.def
.section
->output_offset
);
1663 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_COPY
);
1665 bfd_elf64_swap_reloca_out (output_bfd
, &rela
,
1666 ((Elf64_External_Rela
*) s
->contents
1671 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
1672 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
1673 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
1674 sym
->st_shndx
= SHN_ABS
;
1679 /* Finish up the dynamic sections. */
1682 elf64_x86_64_finish_dynamic_sections (output_bfd
, info
)
1684 struct bfd_link_info
*info
;
1690 dynobj
= elf_hash_table (info
)->dynobj
;
1692 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
1694 if (elf_hash_table (info
)->dynamic_sections_created
)
1697 Elf64_External_Dyn
*dyncon
, *dynconend
;
1699 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1700 BFD_ASSERT (splt
!= NULL
&& sdyn
!= NULL
);
1702 dyncon
= (Elf64_External_Dyn
*) sdyn
->contents
;
1703 dynconend
= (Elf64_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
1704 for (; dyncon
< dynconend
; dyncon
++)
1706 Elf_Internal_Dyn dyn
;
1710 bfd_elf64_swap_dyn_in (dynobj
, dyncon
, &dyn
);
1725 s
= bfd_get_section_by_name (output_bfd
, name
);
1726 BFD_ASSERT (s
!= NULL
);
1727 dyn
.d_un
.d_ptr
= s
->vma
;
1731 /* FIXME: This comment and code is from elf64-alpha.c: */
1732 /* My interpretation of the TIS v1.1 ELF document indicates
1733 that RELASZ should not include JMPREL. This is not what
1734 the rest of the BFD does. It is, however, what the
1735 glibc ld.so wants. Do this fixup here until we found
1736 out who is right. */
1737 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
1740 /* Subtract JMPREL size from RELASZ. */
1742 (s
->_cooked_size
? s
->_cooked_size
: s
->_raw_size
);
1747 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
1748 BFD_ASSERT (s
!= NULL
);
1750 (s
->_cooked_size
!= 0 ? s
->_cooked_size
: s
->_raw_size
);
1754 bfd_elf64_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
1757 /* Initialize the contents of the .plt section. */
1758 if (splt
->_raw_size
> 0)
1760 memcpy (splt
->contents
, elf64_x86_64_plt0_entry
, PLT_ENTRY_SIZE
);
1763 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
=
1767 /* Set the first entry in the global offset table to the address of
1768 the dynamic section. */
1769 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1770 BFD_ASSERT (sgot
!= NULL
);
1771 if (sgot
->_raw_size
> 0)
1774 bfd_put_64 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
1776 bfd_put_64 (output_bfd
,
1777 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
1779 /* Write GOT[1] and GOT[2], needed for the linker. */
1780 bfd_put_64 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ GOT_ENTRY_SIZE
);
1781 bfd_put_64 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ GOT_ENTRY_SIZE
*2);
1784 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
=
1790 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
1791 #define TARGET_LITTLE_NAME "elf64-x86-64"
1792 #define ELF_ARCH bfd_arch_i386
1793 #define ELF_MACHINE_CODE EM_X86_64
1794 #define ELF_MAXPAGESIZE 0x100000
1796 #define elf_backend_can_gc_sections 1
1797 #define elf_backend_want_got_plt 1
1798 #define elf_backend_plt_readonly 1
1799 #define elf_backend_want_plt_sym 0
1800 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
1801 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
1803 #define elf_info_to_howto elf64_x86_64_info_to_howto
1804 #define elf_backend_object_p elf64_x86_64_elf_object_p
1805 #define elf_backend_relocate_section elf64_x86_64_relocate_section
1807 #define bfd_elf64_bfd_final_link _bfd_elf64_gc_common_final_link
1808 #define bfd_elf64_bfd_link_hash_table_create \
1809 elf64_x86_64_link_hash_table_create
1810 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
1812 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
1813 #define elf_backend_check_relocs elf64_x86_64_check_relocs
1814 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
1815 #define elf_backend_finish_dynamic_sections \
1816 elf64_x86_64_finish_dynamic_sections
1817 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
1818 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
1819 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
1820 #define elf_backend_relocate_section elf64_x86_64_relocate_section
1821 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
1823 #include "elf64-target.h"