1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
27 #include "elf/x86-64.h"
29 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
30 #define MINUS_ONE (~ (bfd_vma) 0)
32 /* The relocation "howto" table. Order of fields:
33 type, size, bitsize, pc_relative, complain_on_overflow,
34 special_function, name, partial_inplace, src_mask, dst_pack, pcrel_offset. */
35 static reloc_howto_type x86_64_elf_howto_table
[] =
37 HOWTO(R_X86_64_NONE
, 0, 0, 0, false, 0, complain_overflow_dont
,
38 bfd_elf_generic_reloc
, "R_X86_64_NONE", false, 0x00000000, 0x00000000,
40 HOWTO(R_X86_64_64
, 0, 4, 64, false, 0, complain_overflow_bitfield
,
41 bfd_elf_generic_reloc
, "R_X86_64_64", false, MINUS_ONE
, MINUS_ONE
,
43 HOWTO(R_X86_64_PC32
, 0, 4, 32, true, 0, complain_overflow_signed
,
44 bfd_elf_generic_reloc
, "R_X86_64_PC32", false, 0xffffffff, 0xffffffff,
46 HOWTO(R_X86_64_GOT32
, 0, 4, 32, false, 0, complain_overflow_signed
,
47 bfd_elf_generic_reloc
, "R_X86_64_GOT32", false, 0xffffffff, 0xffffffff,
49 HOWTO(R_X86_64_PLT32
, 0, 4, 32, true, 0, complain_overflow_signed
,
50 bfd_elf_generic_reloc
, "R_X86_64_PLT32", false, 0xffffffff, 0xffffffff,
52 HOWTO(R_X86_64_COPY
, 0, 4, 32, false, 0, complain_overflow_bitfield
,
53 bfd_elf_generic_reloc
, "R_X86_64_COPY", false, 0xffffffff, 0xffffffff,
55 HOWTO(R_X86_64_GLOB_DAT
, 0, 4, 64, false, 0, complain_overflow_bitfield
,
56 bfd_elf_generic_reloc
, "R_X86_64_GLOB_DAT", false, MINUS_ONE
,
58 HOWTO(R_X86_64_JUMP_SLOT
, 0, 4, 64, false, 0, complain_overflow_bitfield
,
59 bfd_elf_generic_reloc
, "R_X86_64_JUMP_SLOT", false, MINUS_ONE
,
61 HOWTO(R_X86_64_RELATIVE
, 0, 4, 64, false, 0, complain_overflow_bitfield
,
62 bfd_elf_generic_reloc
, "R_X86_64_RELATIVE", false, MINUS_ONE
,
64 HOWTO(R_X86_64_GOTPCREL
, 0, 4, 32, true,0 , complain_overflow_signed
,
65 bfd_elf_generic_reloc
, "R_X86_64_GOTPCREL", false, 0xffffffff,
67 HOWTO(R_X86_64_32
, 0, 4, 32, false, 0, complain_overflow_unsigned
,
68 bfd_elf_generic_reloc
, "R_X86_64_32", false, 0xffffffff, 0xffffffff,
70 HOWTO(R_X86_64_32S
, 0, 4, 32, false, 0, complain_overflow_signed
,
71 bfd_elf_generic_reloc
, "R_X86_64_32S", false, 0xffffffff, 0xffffffff,
73 HOWTO(R_X86_64_16
, 0, 1, 16, false, 0, complain_overflow_bitfield
,
74 bfd_elf_generic_reloc
, "R_X86_64_16", false, 0xffff, 0xffff, false),
75 HOWTO(R_X86_64_PC16
,0, 1, 16, true, 0, complain_overflow_bitfield
,
76 bfd_elf_generic_reloc
, "R_X86_64_PC16", false, 0xffff, 0xffff, true),
77 HOWTO(R_X86_64_8
, 0, 0, 8, false, 0, complain_overflow_signed
,
78 bfd_elf_generic_reloc
, "R_X86_64_8", false, 0xff, 0xff, false),
79 HOWTO(R_X86_64_PC8
, 0, 0, 8, true, 0, complain_overflow_signed
,
80 bfd_elf_generic_reloc
, "R_X86_64_PC8", false, 0xff, 0xff, true),
82 /* GNU extension to record C++ vtable hierarchy. */
83 HOWTO (R_X86_64_GNU_VTINHERIT
, 0, 4, 0, false, 0, complain_overflow_dont
,
84 NULL
, "R_X86_64_GNU_VTINHERIT", false, 0, 0, false),
86 /* GNU extension to record C++ vtable member usage. */
87 HOWTO (R_X86_64_GNU_VTENTRY
, 0, 4, 0, false, 0, complain_overflow_dont
,
88 _bfd_elf_rel_vtable_reloc_fn
, "R_X86_64_GNU_VTENTRY", false, 0, 0,
92 /* Map BFD relocs to the x86_64 elf relocs. */
95 bfd_reloc_code_real_type bfd_reloc_val
;
96 unsigned char elf_reloc_val
;
99 static const struct elf_reloc_map x86_64_reloc_map
[] =
101 { BFD_RELOC_NONE
, R_X86_64_NONE
, },
102 { BFD_RELOC_64
, R_X86_64_64
, },
103 { BFD_RELOC_32_PCREL
, R_X86_64_PC32
, },
104 { BFD_RELOC_X86_64_GOT32
, R_X86_64_GOT32
,},
105 { BFD_RELOC_X86_64_PLT32
, R_X86_64_PLT32
,},
106 { BFD_RELOC_X86_64_COPY
, R_X86_64_COPY
, },
107 { BFD_RELOC_X86_64_GLOB_DAT
, R_X86_64_GLOB_DAT
, },
108 { BFD_RELOC_X86_64_JUMP_SLOT
, R_X86_64_JUMP_SLOT
, },
109 { BFD_RELOC_X86_64_RELATIVE
, R_X86_64_RELATIVE
, },
110 { BFD_RELOC_X86_64_GOTPCREL
, R_X86_64_GOTPCREL
, },
111 { BFD_RELOC_32
, R_X86_64_32
, },
112 { BFD_RELOC_X86_64_32S
, R_X86_64_32S
, },
113 { BFD_RELOC_16
, R_X86_64_16
, },
114 { BFD_RELOC_16_PCREL
, R_X86_64_PC16
, },
115 { BFD_RELOC_8
, R_X86_64_8
, },
116 { BFD_RELOC_8_PCREL
, R_X86_64_PC8
, },
117 { BFD_RELOC_VTABLE_INHERIT
, R_X86_64_GNU_VTINHERIT
, },
118 { BFD_RELOC_VTABLE_ENTRY
, R_X86_64_GNU_VTENTRY
, },
121 static reloc_howto_type
*elf64_x86_64_reloc_type_lookup
122 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
123 static void elf64_x86_64_info_to_howto
124 PARAMS ((bfd
*, arelent
*, Elf64_Internal_Rela
*));
125 static boolean elf64_x86_64_grok_prstatus
126 PARAMS ((bfd
*, Elf_Internal_Note
*));
127 static boolean elf64_x86_64_grok_psinfo
128 PARAMS ((bfd
*, Elf_Internal_Note
*));
129 static struct bfd_link_hash_table
*elf64_x86_64_link_hash_table_create
131 static boolean elf64_x86_64_elf_object_p
PARAMS ((bfd
*abfd
));
132 static boolean create_got_section
133 PARAMS((bfd
*, struct bfd_link_info
*));
134 static boolean elf64_x86_64_create_dynamic_sections
135 PARAMS((bfd
*, struct bfd_link_info
*));
136 static void elf64_x86_64_copy_indirect_symbol
137 PARAMS ((struct elf_backend_data
*, struct elf_link_hash_entry
*,
138 struct elf_link_hash_entry
*));
139 static boolean elf64_x86_64_check_relocs
140 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*sec
,
141 const Elf_Internal_Rela
*));
142 static asection
*elf64_x86_64_gc_mark_hook
143 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
144 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
146 static boolean elf64_x86_64_gc_sweep_hook
147 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
148 const Elf_Internal_Rela
*));
150 static struct bfd_hash_entry
*link_hash_newfunc
151 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
152 static boolean elf64_x86_64_adjust_dynamic_symbol
153 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
155 static boolean allocate_dynrelocs
156 PARAMS ((struct elf_link_hash_entry
*, PTR
));
157 static boolean readonly_dynrelocs
158 PARAMS ((struct elf_link_hash_entry
*, PTR
));
159 static boolean elf64_x86_64_size_dynamic_sections
160 PARAMS ((bfd
*, struct bfd_link_info
*));
161 static boolean elf64_x86_64_relocate_section
162 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
163 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
164 static boolean elf64_x86_64_finish_dynamic_symbol
165 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
166 Elf_Internal_Sym
*sym
));
167 static boolean elf64_x86_64_finish_dynamic_sections
168 PARAMS ((bfd
*, struct bfd_link_info
*));
169 static enum elf_reloc_type_class elf64_x86_64_reloc_type_class
170 PARAMS ((const Elf_Internal_Rela
*));
172 /* Given a BFD reloc type, return a HOWTO structure. */
173 static reloc_howto_type
*
174 elf64_x86_64_reloc_type_lookup (abfd
, code
)
175 bfd
*abfd ATTRIBUTE_UNUSED
;
176 bfd_reloc_code_real_type code
;
179 for (i
= 0; i
< sizeof (x86_64_reloc_map
) / sizeof (struct elf_reloc_map
);
182 if (x86_64_reloc_map
[i
].bfd_reloc_val
== code
)
183 return &x86_64_elf_howto_table
[i
];
188 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
191 elf64_x86_64_info_to_howto (abfd
, cache_ptr
, dst
)
192 bfd
*abfd ATTRIBUTE_UNUSED
;
194 Elf64_Internal_Rela
*dst
;
198 r_type
= ELF64_R_TYPE (dst
->r_info
);
199 if (r_type
< (unsigned int) R_X86_64_GNU_VTINHERIT
)
201 BFD_ASSERT (r_type
<= (unsigned int) R_X86_64_PC8
);
206 BFD_ASSERT (r_type
< (unsigned int) R_X86_64_max
);
207 i
= r_type
- ((unsigned int) R_X86_64_GNU_VTINHERIT
- R_X86_64_PC8
- 1);
209 cache_ptr
->howto
= &x86_64_elf_howto_table
[i
];
210 BFD_ASSERT (r_type
== cache_ptr
->howto
->type
);
213 /* Support for core dump NOTE sections. */
215 elf64_x86_64_grok_prstatus (abfd
, note
)
217 Elf_Internal_Note
*note
;
222 switch (note
->descsz
)
227 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
229 elf_tdata (abfd
)->core_signal
230 = bfd_get_16 (abfd
, note
->descdata
+ 12);
233 elf_tdata (abfd
)->core_pid
234 = bfd_get_32 (abfd
, note
->descdata
+ 32);
243 /* Make a ".reg/999" section. */
244 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
245 raw_size
, note
->descpos
+ offset
);
249 elf64_x86_64_grok_psinfo (abfd
, note
)
251 Elf_Internal_Note
*note
;
253 switch (note
->descsz
)
258 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
259 elf_tdata (abfd
)->core_program
260 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 40, 16);
261 elf_tdata (abfd
)->core_command
262 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 56, 80);
265 /* Note that for some reason, a spurious space is tacked
266 onto the end of the args in some (at least one anyway)
267 implementations, so strip it off if it exists. */
270 char *command
= elf_tdata (abfd
)->core_command
;
271 int n
= strlen (command
);
273 if (0 < n
&& command
[n
- 1] == ' ')
274 command
[n
- 1] = '\0';
280 /* Functions for the x86-64 ELF linker. */
282 /* The name of the dynamic interpreter. This is put in the .interp
285 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
287 /* The size in bytes of an entry in the global offset table. */
289 #define GOT_ENTRY_SIZE 8
291 /* The size in bytes of an entry in the procedure linkage table. */
293 #define PLT_ENTRY_SIZE 16
295 /* The first entry in a procedure linkage table looks like this. See the
296 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
298 static const bfd_byte elf64_x86_64_plt0_entry
[PLT_ENTRY_SIZE
] =
300 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
301 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
302 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
305 /* Subsequent entries in a procedure linkage table look like this. */
307 static const bfd_byte elf64_x86_64_plt_entry
[PLT_ENTRY_SIZE
] =
309 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
310 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
311 0x68, /* pushq immediate */
312 0, 0, 0, 0, /* replaced with index into relocation table. */
313 0xe9, /* jmp relative */
314 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
317 /* The x86-64 linker needs to keep track of the number of relocs that
318 it decides to copy as dynamic relocs in check_relocs for each symbol.
319 This is so that it can later discard them if they are found to be
320 unnecessary. We store the information in a field extending the
321 regular ELF linker hash table. */
323 struct elf64_x86_64_dyn_relocs
326 struct elf64_x86_64_dyn_relocs
*next
;
328 /* The input section of the reloc. */
331 /* Total number of relocs copied for the input section. */
334 /* Number of pc-relative relocs copied for the input section. */
335 bfd_size_type pc_count
;
338 /* x86-64 ELF linker hash entry. */
340 struct elf64_x86_64_link_hash_entry
342 struct elf_link_hash_entry elf
;
344 /* Track dynamic relocs copied for this symbol. */
345 struct elf64_x86_64_dyn_relocs
*dyn_relocs
;
348 /* x86-64 ELF linker hash table. */
350 struct elf64_x86_64_link_hash_table
352 struct elf_link_hash_table elf
;
354 /* Short-cuts to get to dynamic linker sections. */
363 /* Small local sym to section mapping cache. */
364 struct sym_sec_cache sym_sec
;
367 /* Get the x86-64 ELF linker hash table from a link_info structure. */
369 #define elf64_x86_64_hash_table(p) \
370 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
372 /* Create an entry in an x86-64 ELF linker hash table. */
374 static struct bfd_hash_entry
*
375 link_hash_newfunc (entry
, table
, string
)
376 struct bfd_hash_entry
*entry
;
377 struct bfd_hash_table
*table
;
380 /* Allocate the structure if it has not already been allocated by a
384 entry
= bfd_hash_allocate (table
,
385 sizeof (struct elf64_x86_64_link_hash_entry
));
390 /* Call the allocation method of the superclass. */
391 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
394 struct elf64_x86_64_link_hash_entry
*eh
;
396 eh
= (struct elf64_x86_64_link_hash_entry
*) entry
;
397 eh
->dyn_relocs
= NULL
;
403 /* Create an X86-64 ELF linker hash table. */
405 static struct bfd_link_hash_table
*
406 elf64_x86_64_link_hash_table_create (abfd
)
409 struct elf64_x86_64_link_hash_table
*ret
;
410 bfd_size_type amt
= sizeof (struct elf64_x86_64_link_hash_table
);
412 ret
= (struct elf64_x86_64_link_hash_table
*) bfd_malloc (amt
);
416 if (! _bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
))
429 ret
->sym_sec
.abfd
= NULL
;
431 return &ret
->elf
.root
;
434 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
435 shortcuts to them in our hash table. */
438 create_got_section (dynobj
, info
)
440 struct bfd_link_info
*info
;
442 struct elf64_x86_64_link_hash_table
*htab
;
444 if (! _bfd_elf_create_got_section (dynobj
, info
))
447 htab
= elf64_x86_64_hash_table (info
);
448 htab
->sgot
= bfd_get_section_by_name (dynobj
, ".got");
449 htab
->sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
450 if (!htab
->sgot
|| !htab
->sgotplt
)
453 htab
->srelgot
= bfd_make_section (dynobj
, ".rela.got");
454 if (htab
->srelgot
== NULL
455 || ! bfd_set_section_flags (dynobj
, htab
->srelgot
,
456 (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
457 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
459 || ! bfd_set_section_alignment (dynobj
, htab
->srelgot
, 3))
464 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
465 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
469 elf64_x86_64_create_dynamic_sections (dynobj
, info
)
471 struct bfd_link_info
*info
;
473 struct elf64_x86_64_link_hash_table
*htab
;
475 htab
= elf64_x86_64_hash_table (info
);
476 if (!htab
->sgot
&& !create_got_section (dynobj
, info
))
479 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
482 htab
->splt
= bfd_get_section_by_name (dynobj
, ".plt");
483 htab
->srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
484 htab
->sdynbss
= bfd_get_section_by_name (dynobj
, ".dynbss");
486 htab
->srelbss
= bfd_get_section_by_name (dynobj
, ".rela.bss");
488 if (!htab
->splt
|| !htab
->srelplt
|| !htab
->sdynbss
489 || (!info
->shared
&& !htab
->srelbss
))
495 /* Copy the extra info we tack onto an elf_link_hash_entry. */
498 elf64_x86_64_copy_indirect_symbol (bed
, dir
, ind
)
499 struct elf_backend_data
*bed
;
500 struct elf_link_hash_entry
*dir
, *ind
;
502 struct elf64_x86_64_link_hash_entry
*edir
, *eind
;
504 edir
= (struct elf64_x86_64_link_hash_entry
*) dir
;
505 eind
= (struct elf64_x86_64_link_hash_entry
*) ind
;
507 if (eind
->dyn_relocs
!= NULL
)
509 if (edir
->dyn_relocs
!= NULL
)
511 struct elf64_x86_64_dyn_relocs
**pp
;
512 struct elf64_x86_64_dyn_relocs
*p
;
514 if (ind
->root
.type
== bfd_link_hash_indirect
)
517 /* Add reloc counts against the weak sym to the strong sym
518 list. Merge any entries against the same section. */
519 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
521 struct elf64_x86_64_dyn_relocs
*q
;
523 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
524 if (q
->sec
== p
->sec
)
526 q
->pc_count
+= p
->pc_count
;
527 q
->count
+= p
->count
;
534 *pp
= edir
->dyn_relocs
;
537 edir
->dyn_relocs
= eind
->dyn_relocs
;
538 eind
->dyn_relocs
= NULL
;
541 _bfd_elf_link_hash_copy_indirect (bed
, dir
, ind
);
545 elf64_x86_64_elf_object_p (abfd
)
548 /* Set the right machine number for an x86-64 elf64 file. */
549 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x86_64
);
553 /* Look through the relocs for a section during the first phase, and
554 calculate needed space in the global offset table, procedure
555 linkage table, and dynamic reloc sections. */
558 elf64_x86_64_check_relocs (abfd
, info
, sec
, relocs
)
560 struct bfd_link_info
*info
;
562 const Elf_Internal_Rela
*relocs
;
564 struct elf64_x86_64_link_hash_table
*htab
;
565 Elf_Internal_Shdr
*symtab_hdr
;
566 struct elf_link_hash_entry
**sym_hashes
;
567 const Elf_Internal_Rela
*rel
;
568 const Elf_Internal_Rela
*rel_end
;
571 if (info
->relocateable
)
574 htab
= elf64_x86_64_hash_table (info
);
575 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
576 sym_hashes
= elf_sym_hashes (abfd
);
580 rel_end
= relocs
+ sec
->reloc_count
;
581 for (rel
= relocs
; rel
< rel_end
; rel
++)
583 unsigned long r_symndx
;
584 struct elf_link_hash_entry
*h
;
586 r_symndx
= ELF64_R_SYM (rel
->r_info
);
588 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
590 (*_bfd_error_handler
) (_("%s: bad symbol index: %d"),
591 bfd_archive_filename (abfd
),
596 if (r_symndx
< symtab_hdr
->sh_info
)
599 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
601 switch (ELF64_R_TYPE (rel
->r_info
))
604 case R_X86_64_GOTPCREL
:
605 /* This symbol requires a global offset table entry. */
608 h
->got
.refcount
+= 1;
612 bfd_signed_vma
*local_got_refcounts
;
614 /* This is a global offset table entry for a local symbol. */
615 local_got_refcounts
= elf_local_got_refcounts (abfd
);
616 if (local_got_refcounts
== NULL
)
620 size
= symtab_hdr
->sh_info
;
621 size
*= sizeof (bfd_signed_vma
);
622 local_got_refcounts
= ((bfd_signed_vma
*)
623 bfd_zalloc (abfd
, size
));
624 if (local_got_refcounts
== NULL
)
626 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
628 local_got_refcounts
[r_symndx
] += 1;
632 //case R_X86_64_GOTPCREL:
633 if (htab
->sgot
== NULL
)
635 if (htab
->elf
.dynobj
== NULL
)
636 htab
->elf
.dynobj
= abfd
;
637 if (!create_got_section (htab
->elf
.dynobj
, info
))
643 /* This symbol requires a procedure linkage table entry. We
644 actually build the entry in adjust_dynamic_symbol,
645 because this might be a case of linking PIC code which is
646 never referenced by a dynamic object, in which case we
647 don't need to generate a procedure linkage table entry
650 /* If this is a local symbol, we resolve it directly without
651 creating a procedure linkage table entry. */
655 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
656 h
->plt
.refcount
+= 1;
663 /* Let's help debug shared library creation. These relocs
664 cannot be used in shared libs. Don't error out for
665 sections we don't care about, such as debug sections or
666 non-constant sections. */
668 && (sec
->flags
& SEC_ALLOC
) != 0
669 && (sec
->flags
& SEC_READONLY
) != 0)
671 (*_bfd_error_handler
)
672 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
673 bfd_archive_filename (abfd
),
674 x86_64_elf_howto_table
[ELF64_R_TYPE (rel
->r_info
)].name
);
675 bfd_set_error (bfd_error_bad_value
);
684 if (h
!= NULL
&& !info
->shared
)
686 /* If this reloc is in a read-only section, we might
687 need a copy reloc. We can't check reliably at this
688 stage whether the section is read-only, as input
689 sections have not yet been mapped to output sections.
690 Tentatively set the flag for now, and correct in
691 adjust_dynamic_symbol. */
692 h
->elf_link_hash_flags
|= ELF_LINK_NON_GOT_REF
;
694 /* We may need a .plt entry if the function this reloc
695 refers to is in a shared lib. */
696 h
->plt
.refcount
+= 1;
699 /* If we are creating a shared library, and this is a reloc
700 against a global symbol, or a non PC relative reloc
701 against a local symbol, then we need to copy the reloc
702 into the shared library. However, if we are linking with
703 -Bsymbolic, we do not need to copy a reloc against a
704 global symbol which is defined in an object we are
705 including in the link (i.e., DEF_REGULAR is set). At
706 this point we have not seen all the input files, so it is
707 possible that DEF_REGULAR is not set now but will be set
708 later (it is never cleared). In case of a weak definition,
709 DEF_REGULAR may be cleared later by a strong definition in
710 a shared library. We account for that possibility below by
711 storing information in the relocs_copied field of the hash
712 table entry. A similar situation occurs when creating
713 shared libraries and symbol visibility changes render the
716 If on the other hand, we are creating an executable, we
717 may need to keep relocations for symbols satisfied by a
718 dynamic library if we manage to avoid copy relocs for the
721 && (sec
->flags
& SEC_ALLOC
) != 0
722 && (((ELF64_R_TYPE (rel
->r_info
) != R_X86_64_PC8
)
723 && (ELF64_R_TYPE (rel
->r_info
) != R_X86_64_PC16
)
724 && (ELF64_R_TYPE (rel
->r_info
) != R_X86_64_PC32
))
727 || h
->root
.type
== bfd_link_hash_defweak
728 || (h
->elf_link_hash_flags
729 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
731 && (sec
->flags
& SEC_ALLOC
) != 0
733 && (h
->root
.type
== bfd_link_hash_defweak
734 || (h
->elf_link_hash_flags
735 & ELF_LINK_HASH_DEF_REGULAR
) == 0)))
737 struct elf64_x86_64_dyn_relocs
*p
;
738 struct elf64_x86_64_dyn_relocs
**head
;
740 /* We must copy these reloc types into the output file.
741 Create a reloc section in dynobj and make room for
748 name
= (bfd_elf_string_from_elf_section
750 elf_elfheader (abfd
)->e_shstrndx
,
751 elf_section_data (sec
)->rel_hdr
.sh_name
));
755 if (strncmp (name
, ".rela", 5) != 0
756 || strcmp (bfd_get_section_name (abfd
, sec
),
759 (*_bfd_error_handler
)
760 (_("%s: bad relocation section name `%s\'"),
761 bfd_archive_filename (abfd
), name
);
764 if (htab
->elf
.dynobj
== NULL
)
765 htab
->elf
.dynobj
= abfd
;
767 dynobj
= htab
->elf
.dynobj
;
769 sreloc
= bfd_get_section_by_name (dynobj
, name
);
774 sreloc
= bfd_make_section (dynobj
, name
);
775 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
776 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
777 if ((sec
->flags
& SEC_ALLOC
) != 0)
778 flags
|= SEC_ALLOC
| SEC_LOAD
;
780 || ! bfd_set_section_flags (dynobj
, sreloc
, flags
)
781 || ! bfd_set_section_alignment (dynobj
, sreloc
, 3))
784 elf_section_data (sec
)->sreloc
= sreloc
;
787 /* If this is a global symbol, we count the number of
788 relocations we need for this symbol. */
791 head
= &((struct elf64_x86_64_link_hash_entry
*) h
)->dyn_relocs
;
795 /* Track dynamic relocs needed for local syms too.
796 We really need local syms available to do this
800 s
= bfd_section_from_r_symndx (abfd
, &htab
->sym_sec
,
805 head
= ((struct elf64_x86_64_dyn_relocs
**)
806 &elf_section_data (s
)->local_dynrel
);
810 if (p
== NULL
|| p
->sec
!= sec
)
812 bfd_size_type amt
= sizeof *p
;
813 p
= ((struct elf64_x86_64_dyn_relocs
*)
814 bfd_alloc (htab
->elf
.dynobj
, amt
));
825 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC8
826 || ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC16
827 || ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC32
)
832 /* This relocation describes the C++ object vtable hierarchy.
833 Reconstruct it for later use during GC. */
834 case R_X86_64_GNU_VTINHERIT
:
835 if (!_bfd_elf64_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
839 /* This relocation describes which C++ vtable entries are actually
840 used. Record for later use during GC. */
841 case R_X86_64_GNU_VTENTRY
:
842 if (!_bfd_elf64_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
854 /* Return the section that should be marked against GC for a given
858 elf64_x86_64_gc_mark_hook (sec
, info
, rel
, h
, sym
)
860 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
861 Elf_Internal_Rela
*rel
;
862 struct elf_link_hash_entry
*h
;
863 Elf_Internal_Sym
*sym
;
867 switch (ELF64_R_TYPE (rel
->r_info
))
869 case R_X86_64_GNU_VTINHERIT
:
870 case R_X86_64_GNU_VTENTRY
:
874 switch (h
->root
.type
)
876 case bfd_link_hash_defined
:
877 case bfd_link_hash_defweak
:
878 return h
->root
.u
.def
.section
;
880 case bfd_link_hash_common
:
881 return h
->root
.u
.c
.p
->section
;
889 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
894 /* Update the got entry reference counts for the section being removed. */
897 elf64_x86_64_gc_sweep_hook (abfd
, info
, sec
, relocs
)
899 struct bfd_link_info
*info
;
901 const Elf_Internal_Rela
*relocs
;
903 Elf_Internal_Shdr
*symtab_hdr
;
904 struct elf_link_hash_entry
**sym_hashes
;
905 bfd_signed_vma
*local_got_refcounts
;
906 const Elf_Internal_Rela
*rel
, *relend
;
907 unsigned long r_symndx
;
908 struct elf_link_hash_entry
*h
;
910 elf_section_data (sec
)->local_dynrel
= NULL
;
912 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
913 sym_hashes
= elf_sym_hashes (abfd
);
914 local_got_refcounts
= elf_local_got_refcounts (abfd
);
916 relend
= relocs
+ sec
->reloc_count
;
917 for (rel
= relocs
; rel
< relend
; rel
++)
918 switch (ELF64_R_TYPE (rel
->r_info
))
921 case R_X86_64_GOTPCREL
:
922 r_symndx
= ELF64_R_SYM (rel
->r_info
);
923 if (r_symndx
>= symtab_hdr
->sh_info
)
925 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
926 if (h
->got
.refcount
> 0)
927 h
->got
.refcount
-= 1;
929 else if (local_got_refcounts
!= NULL
)
931 if (local_got_refcounts
[r_symndx
] > 0)
932 local_got_refcounts
[r_symndx
] -= 1;
944 r_symndx
= ELF64_R_SYM (rel
->r_info
);
945 if (r_symndx
>= symtab_hdr
->sh_info
)
947 struct elf64_x86_64_link_hash_entry
*eh
;
948 struct elf64_x86_64_dyn_relocs
**pp
;
949 struct elf64_x86_64_dyn_relocs
*p
;
951 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
953 if (!info
->shared
&& h
->plt
.refcount
> 0)
954 h
->plt
.refcount
-= 1;
956 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
958 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; pp
= &p
->next
)
961 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC8
962 || ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC16
963 || ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC32
)
975 r_symndx
= ELF64_R_SYM (rel
->r_info
);
976 if (r_symndx
>= symtab_hdr
->sh_info
)
978 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
979 if (h
->plt
.refcount
> 0)
980 h
->plt
.refcount
-= 1;
991 /* Adjust a symbol defined by a dynamic object and referenced by a
992 regular object. The current definition is in some section of the
993 dynamic object, but we're not including those sections. We have to
994 change the definition to something the rest of the link can
998 elf64_x86_64_adjust_dynamic_symbol (info
, h
)
999 struct bfd_link_info
*info
;
1000 struct elf_link_hash_entry
*h
;
1002 struct elf64_x86_64_link_hash_table
*htab
;
1003 struct elf64_x86_64_link_hash_entry
* eh
;
1004 struct elf64_x86_64_dyn_relocs
*p
;
1006 unsigned int power_of_two
;
1008 /* If this is a function, put it in the procedure linkage table. We
1009 will fill in the contents of the procedure linkage table later,
1010 when we know the address of the .got section. */
1011 if (h
->type
== STT_FUNC
1012 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
1014 if (h
->plt
.refcount
<= 0
1016 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
1017 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0
1018 && h
->root
.type
!= bfd_link_hash_undefweak
1019 && h
->root
.type
!= bfd_link_hash_undefined
))
1021 /* This case can occur if we saw a PLT32 reloc in an input
1022 file, but the symbol was never referred to by a dynamic
1023 object, or if all references were garbage collected. In
1024 such a case, we don't actually need to build a procedure
1025 linkage table, and we can just do a PC32 reloc instead. */
1026 h
->plt
.offset
= (bfd_vma
) -1;
1027 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1033 /* It's possible that we incorrectly decided a .plt reloc was
1034 needed for an R_X86_64_PC32 reloc to a non-function sym in
1035 check_relocs. We can't decide accurately between function and
1036 non-function syms in check-relocs; Objects loaded later in
1037 the link may change h->type. So fix it now. */
1038 h
->plt
.offset
= (bfd_vma
) -1;
1040 /* If this is a weak symbol, and there is a real definition, the
1041 processor independent code will have arranged for us to see the
1042 real definition first, and we can just use the same value. */
1043 if (h
->weakdef
!= NULL
)
1045 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
1046 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
1047 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
1048 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
1052 /* This is a reference to a symbol defined by a dynamic object which
1053 is not a function. */
1055 /* If we are creating a shared library, we must presume that the
1056 only references to the symbol are via the global offset table.
1057 For such cases we need not do anything here; the relocations will
1058 be handled correctly by relocate_section. */
1062 /* If there are no references to this symbol that do not use the
1063 GOT, we don't need to generate a copy reloc. */
1064 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0)
1067 /* If -z nocopyreloc was given, we won't generate them either. */
1068 if (info
->nocopyreloc
)
1070 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_GOT_REF
;
1074 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1075 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1077 s
= p
->sec
->output_section
;
1078 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1082 /* If we didn't find any dynamic relocs in read-only sections, then
1083 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1086 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_GOT_REF
;
1090 /* We must allocate the symbol in our .dynbss section, which will
1091 become part of the .bss section of the executable. There will be
1092 an entry for this symbol in the .dynsym section. The dynamic
1093 object will contain position independent code, so all references
1094 from the dynamic object to this symbol will go through the global
1095 offset table. The dynamic linker will use the .dynsym entry to
1096 determine the address it must put in the global offset table, so
1097 both the dynamic object and the regular object will refer to the
1098 same memory location for the variable. */
1100 htab
= elf64_x86_64_hash_table (info
);
1102 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1103 to copy the initial value out of the dynamic object and into the
1104 runtime process image. */
1105 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
1107 htab
->srelbss
->_raw_size
+= sizeof (Elf64_External_Rela
);
1108 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
1111 /* We need to figure out the alignment required for this symbol. I
1112 have no idea how ELF linkers handle this. 16-bytes is the size
1113 of the largest type that requires hard alignment -- long double. */
1114 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1116 power_of_two
= bfd_log2 (h
->size
);
1117 if (power_of_two
> 4)
1120 /* Apply the required alignment. */
1122 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
, (bfd_size_type
) (1 << power_of_two
));
1123 if (power_of_two
> bfd_get_section_alignment (htab
->elf
.dynobj
, s
))
1125 if (! bfd_set_section_alignment (htab
->elf
.dynobj
, s
, power_of_two
))
1129 /* Define the symbol as being at this point in the section. */
1130 h
->root
.u
.def
.section
= s
;
1131 h
->root
.u
.def
.value
= s
->_raw_size
;
1133 /* Increment the section size to make room for the symbol. */
1134 s
->_raw_size
+= h
->size
;
1139 /* This is the condition under which elf64_x86_64_finish_dynamic_symbol
1140 will be called from elflink.h. If elflink.h doesn't call our
1141 finish_dynamic_symbol routine, we'll need to do something about
1142 initializing any .plt and .got entries in elf64_x86_64_relocate_section. */
1143 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1145 && ((INFO)->shared \
1146 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1147 && ((H)->dynindx != -1 \
1148 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1150 /* Allocate space in .plt, .got and associated reloc sections for
1154 allocate_dynrelocs (h
, inf
)
1155 struct elf_link_hash_entry
*h
;
1158 struct bfd_link_info
*info
;
1159 struct elf64_x86_64_link_hash_table
*htab
;
1160 struct elf64_x86_64_link_hash_entry
*eh
;
1161 struct elf64_x86_64_dyn_relocs
*p
;
1163 if (h
->root
.type
== bfd_link_hash_indirect
)
1166 if (h
->root
.type
== bfd_link_hash_warning
)
1167 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1169 info
= (struct bfd_link_info
*) inf
;
1170 htab
= elf64_x86_64_hash_table (info
);
1172 if (htab
->elf
.dynamic_sections_created
1173 && h
->plt
.refcount
> 0)
1175 /* Make sure this symbol is output as a dynamic symbol.
1176 Undefined weak syms won't yet be marked as dynamic. */
1177 if (h
->dynindx
== -1
1178 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
1180 if (! bfd_elf64_link_record_dynamic_symbol (info
, h
))
1184 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info
, h
))
1186 asection
*s
= htab
->splt
;
1188 /* If this is the first .plt entry, make room for the special
1190 if (s
->_raw_size
== 0)
1191 s
->_raw_size
+= PLT_ENTRY_SIZE
;
1193 h
->plt
.offset
= s
->_raw_size
;
1195 /* If this symbol is not defined in a regular file, and we are
1196 not generating a shared library, then set the symbol to this
1197 location in the .plt. This is required to make function
1198 pointers compare as equal between the normal executable and
1199 the shared library. */
1201 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1203 h
->root
.u
.def
.section
= s
;
1204 h
->root
.u
.def
.value
= h
->plt
.offset
;
1207 /* Make room for this entry. */
1208 s
->_raw_size
+= PLT_ENTRY_SIZE
;
1210 /* We also need to make an entry in the .got.plt section, which
1211 will be placed in the .got section by the linker script. */
1212 htab
->sgotplt
->_raw_size
+= GOT_ENTRY_SIZE
;
1214 /* We also need to make an entry in the .rela.plt section. */
1215 htab
->srelplt
->_raw_size
+= sizeof (Elf64_External_Rela
);
1219 h
->plt
.offset
= (bfd_vma
) -1;
1220 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1225 h
->plt
.offset
= (bfd_vma
) -1;
1226 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1229 if (h
->got
.refcount
> 0)
1234 /* Make sure this symbol is output as a dynamic symbol.
1235 Undefined weak syms won't yet be marked as dynamic. */
1236 if (h
->dynindx
== -1
1237 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
1239 if (! bfd_elf64_link_record_dynamic_symbol (info
, h
))
1244 h
->got
.offset
= s
->_raw_size
;
1245 s
->_raw_size
+= GOT_ENTRY_SIZE
;
1246 dyn
= htab
->elf
.dynamic_sections_created
;
1247 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
, h
))
1248 htab
->srelgot
->_raw_size
+= sizeof (Elf64_External_Rela
);
1251 h
->got
.offset
= (bfd_vma
) -1;
1253 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1254 if (eh
->dyn_relocs
== NULL
)
1257 /* In the shared -Bsymbolic case, discard space allocated for
1258 dynamic pc-relative relocs against symbols which turn out to be
1259 defined in regular objects. For the normal shared case, discard
1260 space for pc-relative relocs that have become local due to symbol
1261 visibility changes. */
1265 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
1266 && ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0
1269 struct elf64_x86_64_dyn_relocs
**pp
;
1271 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
1273 p
->count
-= p
->pc_count
;
1284 /* For the non-shared case, discard space for relocs against
1285 symbols which turn out to need copy relocs or are not
1288 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0
1289 && (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1290 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1291 || (htab
->elf
.dynamic_sections_created
1292 && (h
->root
.type
== bfd_link_hash_undefweak
1293 || h
->root
.type
== bfd_link_hash_undefined
))))
1295 /* Make sure this symbol is output as a dynamic symbol.
1296 Undefined weak syms won't yet be marked as dynamic. */
1297 if (h
->dynindx
== -1
1298 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
1300 if (! bfd_elf64_link_record_dynamic_symbol (info
, h
))
1304 /* If that succeeded, we know we'll be keeping all the
1306 if (h
->dynindx
!= -1)
1310 eh
->dyn_relocs
= NULL
;
1315 /* Finally, allocate space. */
1316 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1318 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1319 sreloc
->_raw_size
+= p
->count
* sizeof (Elf64_External_Rela
);
1325 /* Find any dynamic relocs that apply to read-only sections. */
1328 readonly_dynrelocs (h
, inf
)
1329 struct elf_link_hash_entry
*h
;
1332 struct elf64_x86_64_link_hash_entry
*eh
;
1333 struct elf64_x86_64_dyn_relocs
*p
;
1335 if (h
->root
.type
== bfd_link_hash_warning
)
1336 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1338 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1339 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1341 asection
*s
= p
->sec
->output_section
;
1343 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1345 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
1347 info
->flags
|= DF_TEXTREL
;
1349 /* Not an error, just cut short the traversal. */
1356 /* Set the sizes of the dynamic sections. */
1359 elf64_x86_64_size_dynamic_sections (output_bfd
, info
)
1360 bfd
*output_bfd ATTRIBUTE_UNUSED
;
1361 struct bfd_link_info
*info
;
1363 struct elf64_x86_64_link_hash_table
*htab
;
1369 htab
= elf64_x86_64_hash_table (info
);
1370 dynobj
= htab
->elf
.dynobj
;
1374 if (htab
->elf
.dynamic_sections_created
)
1376 /* Set the contents of the .interp section to the interpreter. */
1379 s
= bfd_get_section_by_name (dynobj
, ".interp");
1382 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
1383 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
1387 /* Set up .got offsets for local syms, and space for local dynamic
1389 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
1391 bfd_signed_vma
*local_got
;
1392 bfd_signed_vma
*end_local_got
;
1393 bfd_size_type locsymcount
;
1394 Elf_Internal_Shdr
*symtab_hdr
;
1397 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
1400 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1402 struct elf64_x86_64_dyn_relocs
*p
;
1404 for (p
= *((struct elf64_x86_64_dyn_relocs
**)
1405 &elf_section_data (s
)->local_dynrel
);
1409 if (!bfd_is_abs_section (p
->sec
)
1410 && bfd_is_abs_section (p
->sec
->output_section
))
1412 /* Input section has been discarded, either because
1413 it is a copy of a linkonce section or due to
1414 linker script /DISCARD/, so we'll be discarding
1417 else if (p
->count
!= 0)
1419 srel
= elf_section_data (p
->sec
)->sreloc
;
1420 srel
->_raw_size
+= p
->count
* sizeof (Elf64_External_Rela
);
1421 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1422 info
->flags
|= DF_TEXTREL
;
1428 local_got
= elf_local_got_refcounts (ibfd
);
1432 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
1433 locsymcount
= symtab_hdr
->sh_info
;
1434 end_local_got
= local_got
+ locsymcount
;
1436 srel
= htab
->srelgot
;
1437 for (; local_got
< end_local_got
; ++local_got
)
1441 *local_got
= s
->_raw_size
;
1442 s
->_raw_size
+= GOT_ENTRY_SIZE
;
1444 srel
->_raw_size
+= sizeof (Elf64_External_Rela
);
1447 *local_got
= (bfd_vma
) -1;
1451 /* Allocate global sym .plt and .got entries, and space for global
1452 sym dynamic relocs. */
1453 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, (PTR
) info
);
1455 /* We now have determined the sizes of the various dynamic sections.
1456 Allocate memory for them. */
1458 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1460 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1465 || s
== htab
->sgotplt
)
1467 /* Strip this section if we don't need it; see the
1470 else if (strncmp (bfd_get_section_name (dynobj
, s
), ".rela", 5) == 0)
1472 if (s
->_raw_size
!= 0 && s
!= htab
->srelplt
)
1475 /* We use the reloc_count field as a counter if we need
1476 to copy relocs into the output file. */
1481 /* It's not one of our sections, so don't allocate space. */
1485 if (s
->_raw_size
== 0)
1487 /* If we don't need this section, strip it from the
1488 output file. This is mostly to handle .rela.bss and
1489 .rela.plt. We must create both sections in
1490 create_dynamic_sections, because they must be created
1491 before the linker maps input sections to output
1492 sections. The linker does that before
1493 adjust_dynamic_symbol is called, and it is that
1494 function which decides whether anything needs to go
1495 into these sections. */
1497 _bfd_strip_section_from_output (info
, s
);
1501 /* Allocate memory for the section contents. We use bfd_zalloc
1502 here in case unused entries are not reclaimed before the
1503 section's contents are written out. This should not happen,
1504 but this way if it does, we get a R_X86_64_NONE reloc instead
1506 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->_raw_size
);
1507 if (s
->contents
== NULL
)
1511 if (htab
->elf
.dynamic_sections_created
)
1513 /* Add some entries to the .dynamic section. We fill in the
1514 values later, in elf64_x86_64_finish_dynamic_sections, but we
1515 must add the entries now so that we get the correct size for
1516 the .dynamic section. The DT_DEBUG entry is filled in by the
1517 dynamic linker and used by the debugger. */
1518 #define add_dynamic_entry(TAG, VAL) \
1519 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1523 if (!add_dynamic_entry (DT_DEBUG
, 0))
1527 if (htab
->splt
->_raw_size
!= 0)
1529 if (!add_dynamic_entry (DT_PLTGOT
, 0)
1530 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
1531 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
1532 || !add_dynamic_entry (DT_JMPREL
, 0))
1538 if (!add_dynamic_entry (DT_RELA
, 0)
1539 || !add_dynamic_entry (DT_RELASZ
, 0)
1540 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf64_External_Rela
)))
1543 /* If any dynamic relocs apply to a read-only section,
1544 then we need a DT_TEXTREL entry. */
1545 if ((info
->flags
& DF_TEXTREL
) == 0)
1546 elf_link_hash_traverse (&htab
->elf
, readonly_dynrelocs
,
1549 if ((info
->flags
& DF_TEXTREL
) != 0)
1551 if (!add_dynamic_entry (DT_TEXTREL
, 0))
1556 #undef add_dynamic_entry
1561 /* Relocate an x86_64 ELF section. */
1564 elf64_x86_64_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1565 contents
, relocs
, local_syms
, local_sections
)
1567 struct bfd_link_info
*info
;
1569 asection
*input_section
;
1571 Elf_Internal_Rela
*relocs
;
1572 Elf_Internal_Sym
*local_syms
;
1573 asection
**local_sections
;
1575 struct elf64_x86_64_link_hash_table
*htab
;
1576 Elf_Internal_Shdr
*symtab_hdr
;
1577 struct elf_link_hash_entry
**sym_hashes
;
1578 bfd_vma
*local_got_offsets
;
1579 Elf_Internal_Rela
*rel
;
1580 Elf_Internal_Rela
*relend
;
1582 if (info
->relocateable
)
1585 htab
= elf64_x86_64_hash_table (info
);
1586 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1587 sym_hashes
= elf_sym_hashes (input_bfd
);
1588 local_got_offsets
= elf_local_got_offsets (input_bfd
);
1591 relend
= relocs
+ input_section
->reloc_count
;
1592 for (; rel
< relend
; rel
++)
1595 reloc_howto_type
*howto
;
1596 unsigned long r_symndx
;
1597 struct elf_link_hash_entry
*h
;
1598 Elf_Internal_Sym
*sym
;
1602 boolean unresolved_reloc
;
1603 bfd_reloc_status_type r
;
1605 r_type
= ELF64_R_TYPE (rel
->r_info
);
1606 if (r_type
== (int) R_X86_64_GNU_VTINHERIT
1607 || r_type
== (int) R_X86_64_GNU_VTENTRY
)
1610 if (r_type
< 0 || r_type
>= R_X86_64_max
)
1612 bfd_set_error (bfd_error_bad_value
);
1616 howto
= x86_64_elf_howto_table
+ r_type
;
1617 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1621 unresolved_reloc
= false;
1622 if (r_symndx
< symtab_hdr
->sh_info
)
1624 sym
= local_syms
+ r_symndx
;
1625 sec
= local_sections
[r_symndx
];
1627 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, sec
, rel
);
1631 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1632 while (h
->root
.type
== bfd_link_hash_indirect
1633 || h
->root
.type
== bfd_link_hash_warning
)
1634 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1636 if (h
->root
.type
== bfd_link_hash_defined
1637 || h
->root
.type
== bfd_link_hash_defweak
)
1639 sec
= h
->root
.u
.def
.section
;
1640 if (sec
->output_section
== NULL
)
1642 /* Set a flag that will be cleared later if we find a
1643 relocation value for this symbol. output_section
1644 is typically NULL for symbols satisfied by a shared
1646 unresolved_reloc
= true;
1650 relocation
= (h
->root
.u
.def
.value
1651 + sec
->output_section
->vma
1652 + sec
->output_offset
);
1654 else if (h
->root
.type
== bfd_link_hash_undefweak
)
1656 else if (info
->shared
1657 && (!info
->symbolic
|| info
->allow_shlib_undefined
)
1658 && !info
->no_undefined
1659 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
1663 if (! ((*info
->callbacks
->undefined_symbol
)
1664 (info
, h
->root
.root
.string
, input_bfd
,
1665 input_section
, rel
->r_offset
,
1666 (!info
->shared
|| info
->no_undefined
1667 || ELF_ST_VISIBILITY (h
->other
)))))
1672 /* When generating a shared object, the relocations handled here are
1673 copied into the output file to be resolved at run time. */
1676 case R_X86_64_GOT32
:
1677 /* Relocation is to the entry for this symbol in the global
1679 case R_X86_64_GOTPCREL
:
1680 /* Use global offset table as symbol value. */
1681 if (htab
->sgot
== NULL
)
1688 off
= h
->got
.offset
;
1689 dyn
= htab
->elf
.dynamic_sections_created
;
1691 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
, h
)
1695 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
))
1696 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
1698 /* This is actually a static link, or it is a -Bsymbolic
1699 link and the symbol is defined locally, or the symbol
1700 was forced to be local because of a version file. We
1701 must initialize this entry in the global offset table.
1702 Since the offset must always be a multiple of 8, we
1703 use the least significant bit to record whether we
1704 have initialized it already.
1706 When doing a dynamic link, we create a .rela.got
1707 relocation entry to initialize the value. This is
1708 done in the finish_dynamic_symbol routine. */
1713 bfd_put_64 (output_bfd
, relocation
,
1714 htab
->sgot
->contents
+ off
);
1719 unresolved_reloc
= false;
1723 if (local_got_offsets
== NULL
)
1726 off
= local_got_offsets
[r_symndx
];
1728 /* The offset must always be a multiple of 8. We use
1729 the least significant bit to record whether we have
1730 already generated the necessary reloc. */
1735 bfd_put_64 (output_bfd
, relocation
,
1736 htab
->sgot
->contents
+ off
);
1741 Elf_Internal_Rela outrel
;
1742 Elf64_External_Rela
*loc
;
1744 /* We need to generate a R_X86_64_RELATIVE reloc
1745 for the dynamic linker. */
1746 srelgot
= htab
->srelgot
;
1747 if (srelgot
== NULL
)
1750 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
1751 + htab
->sgot
->output_offset
1753 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
1754 outrel
.r_addend
= relocation
;
1755 loc
= (Elf64_External_Rela
*) srelgot
->contents
;
1756 loc
+= srelgot
->reloc_count
++;
1757 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
1760 local_got_offsets
[r_symndx
] |= 1;
1764 if (off
>= (bfd_vma
) -2)
1767 relocation
= htab
->sgot
->output_offset
+ off
;
1768 if (r_type
== R_X86_64_GOTPCREL
)
1769 relocation
+= htab
->sgot
->output_section
->vma
;
1773 case R_X86_64_PLT32
:
1774 /* Relocation is to the entry for this symbol in the
1775 procedure linkage table. */
1777 /* Resolve a PLT32 reloc against a local symbol directly,
1778 without using the procedure linkage table. */
1782 if (h
->plt
.offset
== (bfd_vma
) -1
1783 || htab
->splt
== NULL
)
1785 /* We didn't make a PLT entry for this symbol. This
1786 happens when statically linking PIC code, or when
1787 using -Bsymbolic. */
1791 relocation
= (htab
->splt
->output_section
->vma
1792 + htab
->splt
->output_offset
1794 unresolved_reloc
= false;
1804 /* FIXME: The ABI says the linker should make sure the value is
1805 the same when it's zeroextended to 64 bit. */
1807 /* r_symndx will be zero only for relocs against symbols
1808 from removed linkonce sections, or sections discarded by
1811 || (input_section
->flags
& SEC_ALLOC
) == 0)
1815 && ((r_type
!= R_X86_64_PC8
1816 && r_type
!= R_X86_64_PC16
1817 && r_type
!= R_X86_64_PC32
)
1820 && (! info
->symbolic
1821 || (h
->elf_link_hash_flags
1822 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
1826 && (h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0
1827 && (((h
->elf_link_hash_flags
1828 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1829 && (h
->elf_link_hash_flags
1830 & ELF_LINK_HASH_DEF_REGULAR
) == 0)
1831 || h
->root
.type
== bfd_link_hash_undefweak
1832 || h
->root
.type
== bfd_link_hash_undefined
)))
1834 Elf_Internal_Rela outrel
;
1835 boolean skip
, relocate
;
1837 Elf64_External_Rela
*loc
;
1839 /* When generating a shared object, these relocations
1840 are copied into the output file to be resolved at run
1847 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
1849 if (outrel
.r_offset
== (bfd_vma
) -1)
1851 else if (outrel
.r_offset
== (bfd_vma
) -2)
1852 skip
= true, relocate
= true;
1854 outrel
.r_offset
+= (input_section
->output_section
->vma
1855 + input_section
->output_offset
);
1858 memset (&outrel
, 0, sizeof outrel
);
1860 /* h->dynindx may be -1 if this symbol was marked to
1864 && (r_type
== R_X86_64_PC8
1865 || r_type
== R_X86_64_PC16
1866 || r_type
== R_X86_64_PC32
1869 || (h
->elf_link_hash_flags
1870 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
1872 outrel
.r_info
= ELF64_R_INFO (h
->dynindx
, r_type
);
1873 outrel
.r_addend
= rel
->r_addend
;
1877 /* This symbol is local, or marked to become local. */
1878 if (r_type
== R_X86_64_64
)
1881 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
1882 outrel
.r_addend
= relocation
+ rel
->r_addend
;
1889 sec
= local_sections
[r_symndx
];
1892 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
1894 == bfd_link_hash_defweak
));
1895 sec
= h
->root
.u
.def
.section
;
1897 if (sec
!= NULL
&& bfd_is_abs_section (sec
))
1899 else if (sec
== NULL
|| sec
->owner
== NULL
)
1901 bfd_set_error (bfd_error_bad_value
);
1908 osec
= sec
->output_section
;
1909 sindx
= elf_section_data (osec
)->dynindx
;
1910 BFD_ASSERT (sindx
> 0);
1913 outrel
.r_info
= ELF64_R_INFO (sindx
, r_type
);
1914 outrel
.r_addend
= relocation
+ rel
->r_addend
;
1918 sreloc
= elf_section_data (input_section
)->sreloc
;
1922 loc
= (Elf64_External_Rela
*) sreloc
->contents
;
1923 loc
+= sreloc
->reloc_count
++;
1924 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
1926 /* If this reloc is against an external symbol, we do
1927 not want to fiddle with the addend. Otherwise, we
1928 need to include the symbol value so that it becomes
1929 an addend for the dynamic reloc. */
1940 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1941 because such sections are not SEC_ALLOC and thus ld.so will
1942 not process them. */
1943 if (unresolved_reloc
1944 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
1945 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0))
1946 (*_bfd_error_handler
)
1947 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
1948 bfd_archive_filename (input_bfd
),
1949 bfd_get_section_name (input_bfd
, input_section
),
1950 (long) rel
->r_offset
,
1951 h
->root
.root
.string
);
1953 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1954 contents
, rel
->r_offset
,
1955 relocation
, rel
->r_addend
);
1957 if (r
!= bfd_reloc_ok
)
1962 name
= h
->root
.root
.string
;
1965 name
= bfd_elf_string_from_elf_section (input_bfd
,
1966 symtab_hdr
->sh_link
,
1971 name
= bfd_section_name (input_bfd
, sec
);
1974 if (r
== bfd_reloc_overflow
)
1977 if (! ((*info
->callbacks
->reloc_overflow
)
1978 (info
, name
, howto
->name
, (bfd_vma
) 0,
1979 input_bfd
, input_section
, rel
->r_offset
)))
1984 (*_bfd_error_handler
)
1985 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
1986 bfd_archive_filename (input_bfd
),
1987 bfd_get_section_name (input_bfd
, input_section
),
1988 (long) rel
->r_offset
, name
, (int) r
);
1997 /* Finish up dynamic symbol handling. We set the contents of various
1998 dynamic sections here. */
2001 elf64_x86_64_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
2003 struct bfd_link_info
*info
;
2004 struct elf_link_hash_entry
*h
;
2005 Elf_Internal_Sym
*sym
;
2007 struct elf64_x86_64_link_hash_table
*htab
;
2009 htab
= elf64_x86_64_hash_table (info
);
2011 if (h
->plt
.offset
!= (bfd_vma
) -1)
2015 Elf_Internal_Rela rela
;
2016 Elf64_External_Rela
*loc
;
2018 /* This symbol has an entry in the procedure linkage table. Set
2021 if (h
->dynindx
== -1
2022 || htab
->splt
== NULL
2023 || htab
->sgotplt
== NULL
2024 || htab
->srelplt
== NULL
)
2027 /* Get the index in the procedure linkage table which
2028 corresponds to this symbol. This is the index of this symbol
2029 in all the symbols for which we are making plt entries. The
2030 first entry in the procedure linkage table is reserved. */
2031 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
2033 /* Get the offset into the .got table of the entry that
2034 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
2035 bytes. The first three are reserved for the dynamic linker. */
2036 got_offset
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
2038 /* Fill in the entry in the procedure linkage table. */
2039 memcpy (htab
->splt
->contents
+ h
->plt
.offset
, elf64_x86_64_plt_entry
,
2042 /* Insert the relocation positions of the plt section. The magic
2043 numbers at the end of the statements are the positions of the
2044 relocations in the plt section. */
2045 /* Put offset for jmp *name@GOTPCREL(%rip), since the
2046 instruction uses 6 bytes, subtract this value. */
2047 bfd_put_32 (output_bfd
,
2048 (htab
->sgotplt
->output_section
->vma
2049 + htab
->sgotplt
->output_offset
2051 - htab
->splt
->output_section
->vma
2052 - htab
->splt
->output_offset
2055 htab
->splt
->contents
+ h
->plt
.offset
+ 2);
2056 /* Put relocation index. */
2057 bfd_put_32 (output_bfd
, plt_index
,
2058 htab
->splt
->contents
+ h
->plt
.offset
+ 7);
2059 /* Put offset for jmp .PLT0. */
2060 bfd_put_32 (output_bfd
, - (h
->plt
.offset
+ PLT_ENTRY_SIZE
),
2061 htab
->splt
->contents
+ h
->plt
.offset
+ 12);
2063 /* Fill in the entry in the global offset table, initially this
2064 points to the pushq instruction in the PLT which is at offset 6. */
2065 bfd_put_64 (output_bfd
, (htab
->splt
->output_section
->vma
2066 + htab
->splt
->output_offset
2067 + h
->plt
.offset
+ 6),
2068 htab
->sgotplt
->contents
+ got_offset
);
2070 /* Fill in the entry in the .rela.plt section. */
2071 rela
.r_offset
= (htab
->sgotplt
->output_section
->vma
2072 + htab
->sgotplt
->output_offset
2074 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_JUMP_SLOT
);
2076 loc
= (Elf64_External_Rela
*) htab
->srelplt
->contents
+ plt_index
;
2077 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
2079 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2081 /* Mark the symbol as undefined, rather than as defined in
2082 the .plt section. Leave the value alone. This is a clue
2083 for the dynamic linker, to make function pointer
2084 comparisons work between an application and shared
2086 sym
->st_shndx
= SHN_UNDEF
;
2090 if (h
->got
.offset
!= (bfd_vma
) -1)
2092 Elf_Internal_Rela rela
;
2093 Elf64_External_Rela
*loc
;
2095 /* This symbol has an entry in the global offset table. Set it
2098 if (htab
->sgot
== NULL
|| htab
->srelgot
== NULL
)
2101 rela
.r_offset
= (htab
->sgot
->output_section
->vma
2102 + htab
->sgot
->output_offset
2103 + (h
->got
.offset
&~ (bfd_vma
) 1));
2105 /* If this is a static link, or it is a -Bsymbolic link and the
2106 symbol is defined locally or was forced to be local because
2107 of a version file, we just want to emit a RELATIVE reloc.
2108 The entry in the global offset table will already have been
2109 initialized in the relocate_section function. */
2113 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
))
2114 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
))
2116 BFD_ASSERT((h
->got
.offset
& 1) != 0);
2117 rela
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
2118 rela
.r_addend
= (h
->root
.u
.def
.value
2119 + h
->root
.u
.def
.section
->output_section
->vma
2120 + h
->root
.u
.def
.section
->output_offset
);
2124 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2125 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
2126 htab
->sgot
->contents
+ h
->got
.offset
);
2127 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_GLOB_DAT
);
2131 loc
= (Elf64_External_Rela
*) htab
->srelgot
->contents
;
2132 loc
+= htab
->srelgot
->reloc_count
++;
2133 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
2136 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
2138 Elf_Internal_Rela rela
;
2139 Elf64_External_Rela
*loc
;
2141 /* This symbol needs a copy reloc. Set it up. */
2143 if (h
->dynindx
== -1
2144 || (h
->root
.type
!= bfd_link_hash_defined
2145 && h
->root
.type
!= bfd_link_hash_defweak
)
2146 || htab
->srelbss
== NULL
)
2149 rela
.r_offset
= (h
->root
.u
.def
.value
2150 + h
->root
.u
.def
.section
->output_section
->vma
2151 + h
->root
.u
.def
.section
->output_offset
);
2152 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_COPY
);
2154 loc
= (Elf64_External_Rela
*) htab
->srelbss
->contents
;
2155 loc
+= htab
->srelbss
->reloc_count
++;
2156 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
2159 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2160 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
2161 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
2162 sym
->st_shndx
= SHN_ABS
;
2167 /* Used to decide how to sort relocs in an optimal manner for the
2168 dynamic linker, before writing them out. */
2170 static enum elf_reloc_type_class
2171 elf64_x86_64_reloc_type_class (rela
)
2172 const Elf_Internal_Rela
*rela
;
2174 switch ((int) ELF64_R_TYPE (rela
->r_info
))
2176 case R_X86_64_RELATIVE
:
2177 return reloc_class_relative
;
2178 case R_X86_64_JUMP_SLOT
:
2179 return reloc_class_plt
;
2181 return reloc_class_copy
;
2183 return reloc_class_normal
;
2187 /* Finish up the dynamic sections. */
2190 elf64_x86_64_finish_dynamic_sections (output_bfd
, info
)
2192 struct bfd_link_info
*info
;
2194 struct elf64_x86_64_link_hash_table
*htab
;
2198 htab
= elf64_x86_64_hash_table (info
);
2199 dynobj
= htab
->elf
.dynobj
;
2200 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2202 if (htab
->elf
.dynamic_sections_created
)
2204 Elf64_External_Dyn
*dyncon
, *dynconend
;
2206 if (sdyn
== NULL
|| htab
->sgot
== NULL
)
2209 dyncon
= (Elf64_External_Dyn
*) sdyn
->contents
;
2210 dynconend
= (Elf64_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
2211 for (; dyncon
< dynconend
; dyncon
++)
2213 Elf_Internal_Dyn dyn
;
2216 bfd_elf64_swap_dyn_in (dynobj
, dyncon
, &dyn
);
2224 dyn
.d_un
.d_ptr
= htab
->sgot
->output_section
->vma
;
2228 dyn
.d_un
.d_ptr
= htab
->srelplt
->output_section
->vma
;
2232 s
= htab
->srelplt
->output_section
;
2233 if (s
->_cooked_size
!= 0)
2234 dyn
.d_un
.d_val
= s
->_cooked_size
;
2236 dyn
.d_un
.d_val
= s
->_raw_size
;
2240 /* The procedure linkage table relocs (DT_JMPREL) should
2241 not be included in the overall relocs (DT_RELA).
2242 Therefore, we override the DT_RELASZ entry here to
2243 make it not include the JMPREL relocs. Since the
2244 linker script arranges for .rela.plt to follow all
2245 other relocation sections, we don't have to worry
2246 about changing the DT_RELA entry. */
2247 if (htab
->srelplt
!= NULL
)
2249 s
= htab
->srelplt
->output_section
;
2250 if (s
->_cooked_size
!= 0)
2251 dyn
.d_un
.d_val
-= s
->_cooked_size
;
2253 dyn
.d_un
.d_val
-= s
->_raw_size
;
2258 bfd_elf64_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
2261 /* Fill in the special first entry in the procedure linkage table. */
2262 if (htab
->splt
&& htab
->splt
->_raw_size
> 0)
2264 /* Fill in the first entry in the procedure linkage table. */
2265 memcpy (htab
->splt
->contents
, elf64_x86_64_plt0_entry
,
2267 /* Add offset for pushq GOT+8(%rip), since the instruction
2268 uses 6 bytes subtract this value. */
2269 bfd_put_32 (output_bfd
,
2270 (htab
->sgotplt
->output_section
->vma
2271 + htab
->sgotplt
->output_offset
2273 - htab
->splt
->output_section
->vma
2274 - htab
->splt
->output_offset
2276 htab
->splt
->contents
+ 2);
2277 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
2278 the end of the instruction. */
2279 bfd_put_32 (output_bfd
,
2280 (htab
->sgotplt
->output_section
->vma
2281 + htab
->sgotplt
->output_offset
2283 - htab
->splt
->output_section
->vma
2284 - htab
->splt
->output_offset
2286 htab
->splt
->contents
+ 8);
2288 elf_section_data (htab
->splt
->output_section
)->this_hdr
.sh_entsize
=
2295 /* Fill in the first three entries in the global offset table. */
2296 if (htab
->sgotplt
->_raw_size
> 0)
2298 /* Set the first entry in the global offset table to the address of
2299 the dynamic section. */
2301 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
);
2303 bfd_put_64 (output_bfd
,
2304 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
2305 htab
->sgotplt
->contents
);
2306 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
2307 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
);
2308 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
*2);
2311 elf_section_data (htab
->sgotplt
->output_section
)->this_hdr
.sh_entsize
=
2319 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
2320 #define TARGET_LITTLE_NAME "elf64-x86-64"
2321 #define ELF_ARCH bfd_arch_i386
2322 #define ELF_MACHINE_CODE EM_X86_64
2323 #define ELF_MAXPAGESIZE 0x100000
2325 #define elf_backend_can_gc_sections 1
2326 #define elf_backend_can_refcount 1
2327 #define elf_backend_want_got_plt 1
2328 #define elf_backend_plt_readonly 1
2329 #define elf_backend_want_plt_sym 0
2330 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
2331 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
2332 #define elf_backend_rela_normal 1
2334 #define elf_info_to_howto elf64_x86_64_info_to_howto
2336 #define bfd_elf64_bfd_link_hash_table_create \
2337 elf64_x86_64_link_hash_table_create
2338 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
2340 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
2341 #define elf_backend_check_relocs elf64_x86_64_check_relocs
2342 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
2343 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
2344 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
2345 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
2346 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
2347 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
2348 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
2349 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
2350 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
2351 #define elf_backend_relocate_section elf64_x86_64_relocate_section
2352 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
2353 #define elf_backend_object_p elf64_x86_64_elf_object_p
2355 #include "elf64-target.h"