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_link_hash_entry
*, struct elf_link_hash_entry
*));
138 static boolean elf64_x86_64_check_relocs
139 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*sec
,
140 const Elf_Internal_Rela
*));
141 static asection
*elf64_x86_64_gc_mark_hook
142 PARAMS ((asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
143 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
145 static boolean elf64_x86_64_gc_sweep_hook
146 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
147 const Elf_Internal_Rela
*));
149 static struct bfd_hash_entry
*link_hash_newfunc
150 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
151 static boolean elf64_x86_64_adjust_dynamic_symbol
152 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
154 static boolean allocate_dynrelocs
155 PARAMS ((struct elf_link_hash_entry
*, PTR
));
156 static boolean readonly_dynrelocs
157 PARAMS ((struct elf_link_hash_entry
*, PTR
));
158 static boolean elf64_x86_64_size_dynamic_sections
159 PARAMS ((bfd
*, struct bfd_link_info
*));
160 static boolean elf64_x86_64_relocate_section
161 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
162 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
163 static boolean elf64_x86_64_finish_dynamic_symbol
164 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
165 Elf_Internal_Sym
*sym
));
166 static boolean elf64_x86_64_finish_dynamic_sections
167 PARAMS ((bfd
*, struct bfd_link_info
*));
168 static enum elf_reloc_type_class elf64_x86_64_reloc_type_class
169 PARAMS ((const Elf_Internal_Rela
*));
171 /* Given a BFD reloc type, return a HOWTO structure. */
172 static reloc_howto_type
*
173 elf64_x86_64_reloc_type_lookup (abfd
, code
)
174 bfd
*abfd ATTRIBUTE_UNUSED
;
175 bfd_reloc_code_real_type code
;
178 for (i
= 0; i
< sizeof (x86_64_reloc_map
) / sizeof (struct elf_reloc_map
);
181 if (x86_64_reloc_map
[i
].bfd_reloc_val
== code
)
182 return &x86_64_elf_howto_table
[i
];
187 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
190 elf64_x86_64_info_to_howto (abfd
, cache_ptr
, dst
)
191 bfd
*abfd ATTRIBUTE_UNUSED
;
193 Elf64_Internal_Rela
*dst
;
197 r_type
= ELF64_R_TYPE (dst
->r_info
);
198 if (r_type
< (unsigned int) R_X86_64_GNU_VTINHERIT
)
200 BFD_ASSERT (r_type
<= (unsigned int) R_X86_64_PC8
);
205 BFD_ASSERT (r_type
< (unsigned int) R_X86_64_max
);
206 i
= r_type
- ((unsigned int) R_X86_64_GNU_VTINHERIT
- R_X86_64_PC8
- 1);
208 cache_ptr
->howto
= &x86_64_elf_howto_table
[i
];
209 BFD_ASSERT (r_type
== cache_ptr
->howto
->type
);
212 /* Support for core dump NOTE sections. */
214 elf64_x86_64_grok_prstatus (abfd
, note
)
216 Elf_Internal_Note
*note
;
221 switch (note
->descsz
)
226 case 336: /* sizeof(istruct elf_prstatus) on Linux/x86_64 */
228 elf_tdata (abfd
)->core_signal
229 = bfd_get_16 (abfd
, note
->descdata
+ 12);
232 elf_tdata (abfd
)->core_pid
233 = bfd_get_32 (abfd
, note
->descdata
+ 32);
242 /* Make a ".reg/999" section. */
243 return _bfd_elfcore_make_pseudosection (abfd
, ".reg",
244 raw_size
, note
->descpos
+ offset
);
248 elf64_x86_64_grok_psinfo (abfd
, note
)
250 Elf_Internal_Note
*note
;
252 switch (note
->descsz
)
257 case 136: /* sizeof(struct elf_prpsinfo) on Linux/x86_64 */
258 elf_tdata (abfd
)->core_program
259 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 40, 16);
260 elf_tdata (abfd
)->core_command
261 = _bfd_elfcore_strndup (abfd
, note
->descdata
+ 56, 80);
264 /* Note that for some reason, a spurious space is tacked
265 onto the end of the args in some (at least one anyway)
266 implementations, so strip it off if it exists. */
269 char *command
= elf_tdata (abfd
)->core_command
;
270 int n
= strlen (command
);
272 if (0 < n
&& command
[n
- 1] == ' ')
273 command
[n
- 1] = '\0';
279 /* Functions for the x86-64 ELF linker. */
281 /* The name of the dynamic interpreter. This is put in the .interp
284 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
286 /* The size in bytes of an entry in the global offset table. */
288 #define GOT_ENTRY_SIZE 8
290 /* The size in bytes of an entry in the procedure linkage table. */
292 #define PLT_ENTRY_SIZE 16
294 /* The first entry in a procedure linkage table looks like this. See the
295 SVR4 ABI i386 supplement and the x86-64 ABI to see how this works. */
297 static const bfd_byte elf64_x86_64_plt0_entry
[PLT_ENTRY_SIZE
] =
299 0xff, 0x35, 8, 0, 0, 0, /* pushq GOT+8(%rip) */
300 0xff, 0x25, 16, 0, 0, 0, /* jmpq *GOT+16(%rip) */
301 0x90, 0x90, 0x90, 0x90 /* pad out to 16 bytes with nops. */
304 /* Subsequent entries in a procedure linkage table look like this. */
306 static const bfd_byte elf64_x86_64_plt_entry
[PLT_ENTRY_SIZE
] =
308 0xff, 0x25, /* jmpq *name@GOTPC(%rip) */
309 0, 0, 0, 0, /* replaced with offset to this symbol in .got. */
310 0x68, /* pushq immediate */
311 0, 0, 0, 0, /* replaced with index into relocation table. */
312 0xe9, /* jmp relative */
313 0, 0, 0, 0 /* replaced with offset to start of .plt0. */
316 /* The x86-64 linker needs to keep track of the number of relocs that
317 it decides to copy as dynamic relocs in check_relocs for each symbol.
318 This is so that it can later discard them if they are found to be
319 unnecessary. We store the information in a field extending the
320 regular ELF linker hash table. */
322 struct elf64_x86_64_dyn_relocs
325 struct elf64_x86_64_dyn_relocs
*next
;
327 /* The input section of the reloc. */
330 /* Total number of relocs copied for the input section. */
333 /* Number of pc-relative relocs copied for the input section. */
334 bfd_size_type pc_count
;
337 /* x86-64 ELF linker hash entry. */
339 struct elf64_x86_64_link_hash_entry
341 struct elf_link_hash_entry elf
;
343 /* Track dynamic relocs copied for this symbol. */
344 struct elf64_x86_64_dyn_relocs
*dyn_relocs
;
347 /* x86-64 ELF linker hash table. */
349 struct elf64_x86_64_link_hash_table
351 struct elf_link_hash_table elf
;
353 /* Short-cuts to get to dynamic linker sections. */
362 /* Small local sym to section mapping cache. */
363 struct sym_sec_cache sym_sec
;
366 /* Get the x86-64 ELF linker hash table from a link_info structure. */
368 #define elf64_x86_64_hash_table(p) \
369 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
371 /* Create an entry in an x86-64 ELF linker hash table. */
373 static struct bfd_hash_entry
*
374 link_hash_newfunc (entry
, table
, string
)
375 struct bfd_hash_entry
*entry
;
376 struct bfd_hash_table
*table
;
379 /* Allocate the structure if it has not already been allocated by a
383 entry
= bfd_hash_allocate (table
,
384 sizeof (struct elf64_x86_64_link_hash_entry
));
389 /* Call the allocation method of the superclass. */
390 entry
= _bfd_elf_link_hash_newfunc (entry
, table
, string
);
393 struct elf64_x86_64_link_hash_entry
*eh
;
395 eh
= (struct elf64_x86_64_link_hash_entry
*) entry
;
396 eh
->dyn_relocs
= NULL
;
402 /* Create an X86-64 ELF linker hash table. */
404 static struct bfd_link_hash_table
*
405 elf64_x86_64_link_hash_table_create (abfd
)
408 struct elf64_x86_64_link_hash_table
*ret
;
409 bfd_size_type amt
= sizeof (struct elf64_x86_64_link_hash_table
);
411 ret
= (struct elf64_x86_64_link_hash_table
*) bfd_malloc (amt
);
415 if (! _bfd_elf_link_hash_table_init (&ret
->elf
, abfd
, link_hash_newfunc
))
428 ret
->sym_sec
.abfd
= NULL
;
430 return &ret
->elf
.root
;
433 /* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
434 shortcuts to them in our hash table. */
437 create_got_section (dynobj
, info
)
439 struct bfd_link_info
*info
;
441 struct elf64_x86_64_link_hash_table
*htab
;
443 if (! _bfd_elf_create_got_section (dynobj
, info
))
446 htab
= elf64_x86_64_hash_table (info
);
447 htab
->sgot
= bfd_get_section_by_name (dynobj
, ".got");
448 htab
->sgotplt
= bfd_get_section_by_name (dynobj
, ".got.plt");
449 if (!htab
->sgot
|| !htab
->sgotplt
)
452 htab
->srelgot
= bfd_make_section (dynobj
, ".rela.got");
453 if (htab
->srelgot
== NULL
454 || ! bfd_set_section_flags (dynobj
, htab
->srelgot
,
455 (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
456 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
458 || ! bfd_set_section_alignment (dynobj
, htab
->srelgot
, 3))
463 /* Create .plt, .rela.plt, .got, .got.plt, .rela.got, .dynbss, and
464 .rela.bss sections in DYNOBJ, and set up shortcuts to them in our
468 elf64_x86_64_create_dynamic_sections (dynobj
, info
)
470 struct bfd_link_info
*info
;
472 struct elf64_x86_64_link_hash_table
*htab
;
474 htab
= elf64_x86_64_hash_table (info
);
475 if (!htab
->sgot
&& !create_got_section (dynobj
, info
))
478 if (!_bfd_elf_create_dynamic_sections (dynobj
, info
))
481 htab
->splt
= bfd_get_section_by_name (dynobj
, ".plt");
482 htab
->srelplt
= bfd_get_section_by_name (dynobj
, ".rela.plt");
483 htab
->sdynbss
= bfd_get_section_by_name (dynobj
, ".dynbss");
485 htab
->srelbss
= bfd_get_section_by_name (dynobj
, ".rela.bss");
487 if (!htab
->splt
|| !htab
->srelplt
|| !htab
->sdynbss
488 || (!info
->shared
&& !htab
->srelbss
))
494 /* Copy the extra info we tack onto an elf_link_hash_entry. */
497 elf64_x86_64_copy_indirect_symbol (dir
, ind
)
498 struct elf_link_hash_entry
*dir
, *ind
;
500 struct elf64_x86_64_link_hash_entry
*edir
, *eind
;
502 edir
= (struct elf64_x86_64_link_hash_entry
*) dir
;
503 eind
= (struct elf64_x86_64_link_hash_entry
*) ind
;
505 if (eind
->dyn_relocs
!= NULL
)
507 if (edir
->dyn_relocs
!= NULL
)
509 struct elf64_x86_64_dyn_relocs
**pp
;
510 struct elf64_x86_64_dyn_relocs
*p
;
512 if (ind
->root
.type
== bfd_link_hash_indirect
)
515 /* Add reloc counts against the weak sym to the strong sym
516 list. Merge any entries against the same section. */
517 for (pp
= &eind
->dyn_relocs
; (p
= *pp
) != NULL
; )
519 struct elf64_x86_64_dyn_relocs
*q
;
521 for (q
= edir
->dyn_relocs
; q
!= NULL
; q
= q
->next
)
522 if (q
->sec
== p
->sec
)
524 q
->pc_count
+= p
->pc_count
;
525 q
->count
+= p
->count
;
532 *pp
= edir
->dyn_relocs
;
535 edir
->dyn_relocs
= eind
->dyn_relocs
;
536 eind
->dyn_relocs
= NULL
;
539 _bfd_elf_link_hash_copy_indirect (dir
, ind
);
543 elf64_x86_64_elf_object_p (abfd
)
546 /* Set the right machine number for an x86-64 elf64 file. */
547 bfd_default_set_arch_mach (abfd
, bfd_arch_i386
, bfd_mach_x86_64
);
551 /* Look through the relocs for a section during the first phase, and
552 calculate needed space in the global offset table, procedure
553 linkage table, and dynamic reloc sections. */
556 elf64_x86_64_check_relocs (abfd
, info
, sec
, relocs
)
558 struct bfd_link_info
*info
;
560 const Elf_Internal_Rela
*relocs
;
562 struct elf64_x86_64_link_hash_table
*htab
;
563 Elf_Internal_Shdr
*symtab_hdr
;
564 struct elf_link_hash_entry
**sym_hashes
;
565 const Elf_Internal_Rela
*rel
;
566 const Elf_Internal_Rela
*rel_end
;
569 if (info
->relocateable
)
572 htab
= elf64_x86_64_hash_table (info
);
573 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
574 sym_hashes
= elf_sym_hashes (abfd
);
578 rel_end
= relocs
+ sec
->reloc_count
;
579 for (rel
= relocs
; rel
< rel_end
; rel
++)
581 unsigned long r_symndx
;
582 struct elf_link_hash_entry
*h
;
584 r_symndx
= ELF64_R_SYM (rel
->r_info
);
586 if (r_symndx
>= NUM_SHDR_ENTRIES (symtab_hdr
))
588 (*_bfd_error_handler
) (_("%s: bad symbol index: %d"),
589 bfd_archive_filename (abfd
),
594 if (r_symndx
< symtab_hdr
->sh_info
)
597 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
599 switch (ELF64_R_TYPE (rel
->r_info
))
602 case R_X86_64_GOTPCREL
:
603 /* This symbol requires a global offset table entry. */
606 h
->got
.refcount
+= 1;
610 bfd_signed_vma
*local_got_refcounts
;
612 /* This is a global offset table entry for a local symbol. */
613 local_got_refcounts
= elf_local_got_refcounts (abfd
);
614 if (local_got_refcounts
== NULL
)
618 size
= symtab_hdr
->sh_info
;
619 size
*= sizeof (bfd_signed_vma
);
620 local_got_refcounts
= ((bfd_signed_vma
*)
621 bfd_zalloc (abfd
, size
));
622 if (local_got_refcounts
== NULL
)
624 elf_local_got_refcounts (abfd
) = local_got_refcounts
;
626 local_got_refcounts
[r_symndx
] += 1;
630 //case R_X86_64_GOTPCREL:
631 if (htab
->sgot
== NULL
)
633 if (htab
->elf
.dynobj
== NULL
)
634 htab
->elf
.dynobj
= abfd
;
635 if (!create_got_section (htab
->elf
.dynobj
, info
))
641 /* This symbol requires a procedure linkage table entry. We
642 actually build the entry in adjust_dynamic_symbol,
643 because this might be a case of linking PIC code which is
644 never referenced by a dynamic object, in which case we
645 don't need to generate a procedure linkage table entry
648 /* If this is a local symbol, we resolve it directly without
649 creating a procedure linkage table entry. */
653 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
654 h
->plt
.refcount
+= 1;
661 /* Let's help debug shared library creation. These relocs
662 cannot be used in shared libs. Don't error out for
663 sections we don't care about, such as debug sections or
664 non-constant sections. */
666 && (sec
->flags
& SEC_ALLOC
) != 0
667 && (sec
->flags
& SEC_READONLY
) != 0)
669 (*_bfd_error_handler
)
670 (_("%s: relocation %s can not be used when making a shared object; recompile with -fPIC"),
671 bfd_archive_filename (abfd
),
672 x86_64_elf_howto_table
[ELF64_R_TYPE (rel
->r_info
)].name
);
673 bfd_set_error (bfd_error_bad_value
);
682 if (h
!= NULL
&& !info
->shared
)
684 /* If this reloc is in a read-only section, we might
685 need a copy reloc. We can't check reliably at this
686 stage whether the section is read-only, as input
687 sections have not yet been mapped to output sections.
688 Tentatively set the flag for now, and correct in
689 adjust_dynamic_symbol. */
690 h
->elf_link_hash_flags
|= ELF_LINK_NON_GOT_REF
;
692 /* We may need a .plt entry if the function this reloc
693 refers to is in a shared lib. */
694 h
->plt
.refcount
+= 1;
697 /* If we are creating a shared library, and this is a reloc
698 against a global symbol, or a non PC relative reloc
699 against a local symbol, then we need to copy the reloc
700 into the shared library. However, if we are linking with
701 -Bsymbolic, we do not need to copy a reloc against a
702 global symbol which is defined in an object we are
703 including in the link (i.e., DEF_REGULAR is set). At
704 this point we have not seen all the input files, so it is
705 possible that DEF_REGULAR is not set now but will be set
706 later (it is never cleared). In case of a weak definition,
707 DEF_REGULAR may be cleared later by a strong definition in
708 a shared library. We account for that possibility below by
709 storing information in the relocs_copied field of the hash
710 table entry. A similar situation occurs when creating
711 shared libraries and symbol visibility changes render the
714 If on the other hand, we are creating an executable, we
715 may need to keep relocations for symbols satisfied by a
716 dynamic library if we manage to avoid copy relocs for the
719 && (sec
->flags
& SEC_ALLOC
) != 0
720 && (((ELF64_R_TYPE (rel
->r_info
) != R_X86_64_PC8
)
721 && (ELF64_R_TYPE (rel
->r_info
) != R_X86_64_PC16
)
722 && (ELF64_R_TYPE (rel
->r_info
) != R_X86_64_PC32
))
725 || h
->root
.type
== bfd_link_hash_defweak
726 || (h
->elf_link_hash_flags
727 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
729 && (sec
->flags
& SEC_ALLOC
) != 0
731 && (h
->root
.type
== bfd_link_hash_defweak
732 || (h
->elf_link_hash_flags
733 & ELF_LINK_HASH_DEF_REGULAR
) == 0)))
735 struct elf64_x86_64_dyn_relocs
*p
;
736 struct elf64_x86_64_dyn_relocs
**head
;
738 /* We must copy these reloc types into the output file.
739 Create a reloc section in dynobj and make room for
746 name
= (bfd_elf_string_from_elf_section
748 elf_elfheader (abfd
)->e_shstrndx
,
749 elf_section_data (sec
)->rel_hdr
.sh_name
));
753 if (strncmp (name
, ".rela", 5) != 0
754 || strcmp (bfd_get_section_name (abfd
, sec
),
757 (*_bfd_error_handler
)
758 (_("%s: bad relocation section name `%s\'"),
759 bfd_archive_filename (abfd
), name
);
762 if (htab
->elf
.dynobj
== NULL
)
763 htab
->elf
.dynobj
= abfd
;
765 dynobj
= htab
->elf
.dynobj
;
767 sreloc
= bfd_get_section_by_name (dynobj
, name
);
772 sreloc
= bfd_make_section (dynobj
, name
);
773 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
774 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
775 if ((sec
->flags
& SEC_ALLOC
) != 0)
776 flags
|= SEC_ALLOC
| SEC_LOAD
;
778 || ! bfd_set_section_flags (dynobj
, sreloc
, flags
)
779 || ! bfd_set_section_alignment (dynobj
, sreloc
, 3))
782 elf_section_data (sec
)->sreloc
= sreloc
;
785 /* If this is a global symbol, we count the number of
786 relocations we need for this symbol. */
789 head
= &((struct elf64_x86_64_link_hash_entry
*) h
)->dyn_relocs
;
793 /* Track dynamic relocs needed for local syms too.
794 We really need local syms available to do this
798 s
= bfd_section_from_r_symndx (abfd
, &htab
->sym_sec
,
803 head
= ((struct elf64_x86_64_dyn_relocs
**)
804 &elf_section_data (s
)->local_dynrel
);
808 if (p
== NULL
|| p
->sec
!= sec
)
810 bfd_size_type amt
= sizeof *p
;
811 p
= ((struct elf64_x86_64_dyn_relocs
*)
812 bfd_alloc (htab
->elf
.dynobj
, amt
));
823 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC8
824 || ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC16
825 || ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC32
)
830 /* This relocation describes the C++ object vtable hierarchy.
831 Reconstruct it for later use during GC. */
832 case R_X86_64_GNU_VTINHERIT
:
833 if (!_bfd_elf64_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
837 /* This relocation describes which C++ vtable entries are actually
838 used. Record for later use during GC. */
839 case R_X86_64_GNU_VTENTRY
:
840 if (!_bfd_elf64_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
852 /* Return the section that should be marked against GC for a given
856 elf64_x86_64_gc_mark_hook (sec
, info
, rel
, h
, sym
)
858 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
859 Elf_Internal_Rela
*rel
;
860 struct elf_link_hash_entry
*h
;
861 Elf_Internal_Sym
*sym
;
865 switch (ELF64_R_TYPE (rel
->r_info
))
867 case R_X86_64_GNU_VTINHERIT
:
868 case R_X86_64_GNU_VTENTRY
:
872 switch (h
->root
.type
)
874 case bfd_link_hash_defined
:
875 case bfd_link_hash_defweak
:
876 return h
->root
.u
.def
.section
;
878 case bfd_link_hash_common
:
879 return h
->root
.u
.c
.p
->section
;
887 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
892 /* Update the got entry reference counts for the section being removed. */
895 elf64_x86_64_gc_sweep_hook (abfd
, info
, sec
, relocs
)
897 struct bfd_link_info
*info
;
899 const Elf_Internal_Rela
*relocs
;
901 Elf_Internal_Shdr
*symtab_hdr
;
902 struct elf_link_hash_entry
**sym_hashes
;
903 bfd_signed_vma
*local_got_refcounts
;
904 const Elf_Internal_Rela
*rel
, *relend
;
905 unsigned long r_symndx
;
906 struct elf_link_hash_entry
*h
;
908 elf_section_data (sec
)->local_dynrel
= NULL
;
910 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
911 sym_hashes
= elf_sym_hashes (abfd
);
912 local_got_refcounts
= elf_local_got_refcounts (abfd
);
914 relend
= relocs
+ sec
->reloc_count
;
915 for (rel
= relocs
; rel
< relend
; rel
++)
916 switch (ELF64_R_TYPE (rel
->r_info
))
919 case R_X86_64_GOTPCREL
:
920 r_symndx
= ELF64_R_SYM (rel
->r_info
);
921 if (r_symndx
>= symtab_hdr
->sh_info
)
923 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
924 if (h
->got
.refcount
> 0)
925 h
->got
.refcount
-= 1;
927 else if (local_got_refcounts
!= NULL
)
929 if (local_got_refcounts
[r_symndx
] > 0)
930 local_got_refcounts
[r_symndx
] -= 1;
942 r_symndx
= ELF64_R_SYM (rel
->r_info
);
943 if (r_symndx
>= symtab_hdr
->sh_info
)
945 struct elf64_x86_64_link_hash_entry
*eh
;
946 struct elf64_x86_64_dyn_relocs
**pp
;
947 struct elf64_x86_64_dyn_relocs
*p
;
949 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
951 if (!info
->shared
&& h
->plt
.refcount
> 0)
952 h
->plt
.refcount
-= 1;
954 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
956 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; pp
= &p
->next
)
959 if (ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC8
960 || ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC16
961 || ELF64_R_TYPE (rel
->r_info
) == R_X86_64_PC32
)
973 r_symndx
= ELF64_R_SYM (rel
->r_info
);
974 if (r_symndx
>= symtab_hdr
->sh_info
)
976 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
977 if (h
->plt
.refcount
> 0)
978 h
->plt
.refcount
-= 1;
989 /* Adjust a symbol defined by a dynamic object and referenced by a
990 regular object. The current definition is in some section of the
991 dynamic object, but we're not including those sections. We have to
992 change the definition to something the rest of the link can
996 elf64_x86_64_adjust_dynamic_symbol (info
, h
)
997 struct bfd_link_info
*info
;
998 struct elf_link_hash_entry
*h
;
1000 struct elf64_x86_64_link_hash_table
*htab
;
1001 struct elf64_x86_64_link_hash_entry
* eh
;
1002 struct elf64_x86_64_dyn_relocs
*p
;
1004 unsigned int power_of_two
;
1006 /* If this is a function, put it in the procedure linkage table. We
1007 will fill in the contents of the procedure linkage table later,
1008 when we know the address of the .got section. */
1009 if (h
->type
== STT_FUNC
1010 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
1012 if (h
->plt
.refcount
<= 0
1014 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
1015 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0
1016 && h
->root
.type
!= bfd_link_hash_undefweak
1017 && h
->root
.type
!= bfd_link_hash_undefined
))
1019 /* This case can occur if we saw a PLT32 reloc in an input
1020 file, but the symbol was never referred to by a dynamic
1021 object, or if all references were garbage collected. In
1022 such a case, we don't actually need to build a procedure
1023 linkage table, and we can just do a PC32 reloc instead. */
1024 h
->plt
.offset
= (bfd_vma
) -1;
1025 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1031 /* It's possible that we incorrectly decided a .plt reloc was
1032 needed for an R_X86_64_PC32 reloc to a non-function sym in
1033 check_relocs. We can't decide accurately between function and
1034 non-function syms in check-relocs; Objects loaded later in
1035 the link may change h->type. So fix it now. */
1036 h
->plt
.offset
= (bfd_vma
) -1;
1038 /* If this is a weak symbol, and there is a real definition, the
1039 processor independent code will have arranged for us to see the
1040 real definition first, and we can just use the same value. */
1041 if (h
->weakdef
!= NULL
)
1043 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
1044 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
1045 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
1046 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
1050 /* This is a reference to a symbol defined by a dynamic object which
1051 is not a function. */
1053 /* If we are creating a shared library, we must presume that the
1054 only references to the symbol are via the global offset table.
1055 For such cases we need not do anything here; the relocations will
1056 be handled correctly by relocate_section. */
1060 /* If there are no references to this symbol that do not use the
1061 GOT, we don't need to generate a copy reloc. */
1062 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0)
1065 /* If -z nocopyreloc was given, we won't generate them either. */
1066 if (info
->nocopyreloc
)
1068 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_GOT_REF
;
1072 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1073 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1075 s
= p
->sec
->output_section
;
1076 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1080 /* If we didn't find any dynamic relocs in read-only sections, then
1081 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
1084 h
->elf_link_hash_flags
&= ~ELF_LINK_NON_GOT_REF
;
1088 /* We must allocate the symbol in our .dynbss section, which will
1089 become part of the .bss section of the executable. There will be
1090 an entry for this symbol in the .dynsym section. The dynamic
1091 object will contain position independent code, so all references
1092 from the dynamic object to this symbol will go through the global
1093 offset table. The dynamic linker will use the .dynsym entry to
1094 determine the address it must put in the global offset table, so
1095 both the dynamic object and the regular object will refer to the
1096 same memory location for the variable. */
1098 htab
= elf64_x86_64_hash_table (info
);
1100 /* We must generate a R_X86_64_COPY reloc to tell the dynamic linker
1101 to copy the initial value out of the dynamic object and into the
1102 runtime process image. */
1103 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
1105 htab
->srelbss
->_raw_size
+= sizeof (Elf64_External_Rela
);
1106 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
1109 /* We need to figure out the alignment required for this symbol. I
1110 have no idea how ELF linkers handle this. 16-bytes is the size
1111 of the largest type that requires hard alignment -- long double. */
1112 /* FIXME: This is VERY ugly. Should be fixed for all architectures using
1114 power_of_two
= bfd_log2 (h
->size
);
1115 if (power_of_two
> 4)
1118 /* Apply the required alignment. */
1120 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
, (bfd_size_type
) (1 << power_of_two
));
1121 if (power_of_two
> bfd_get_section_alignment (htab
->elf
.dynobj
, s
))
1123 if (! bfd_set_section_alignment (htab
->elf
.dynobj
, s
, power_of_two
))
1127 /* Define the symbol as being at this point in the section. */
1128 h
->root
.u
.def
.section
= s
;
1129 h
->root
.u
.def
.value
= s
->_raw_size
;
1131 /* Increment the section size to make room for the symbol. */
1132 s
->_raw_size
+= h
->size
;
1137 /* This is the condition under which elf64_x86_64_finish_dynamic_symbol
1138 will be called from elflink.h. If elflink.h doesn't call our
1139 finish_dynamic_symbol routine, we'll need to do something about
1140 initializing any .plt and .got entries in elf64_x86_64_relocate_section. */
1141 #define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, INFO, H) \
1143 && ((INFO)->shared \
1144 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0) \
1145 && ((H)->dynindx != -1 \
1146 || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
1148 /* Allocate space in .plt, .got and associated reloc sections for
1152 allocate_dynrelocs (h
, inf
)
1153 struct elf_link_hash_entry
*h
;
1156 struct bfd_link_info
*info
;
1157 struct elf64_x86_64_link_hash_table
*htab
;
1158 struct elf64_x86_64_link_hash_entry
*eh
;
1159 struct elf64_x86_64_dyn_relocs
*p
;
1161 if (h
->root
.type
== bfd_link_hash_indirect
)
1164 if (h
->root
.type
== bfd_link_hash_warning
)
1165 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1167 info
= (struct bfd_link_info
*) inf
;
1168 htab
= elf64_x86_64_hash_table (info
);
1170 if (htab
->elf
.dynamic_sections_created
1171 && h
->plt
.refcount
> 0)
1173 /* Make sure this symbol is output as a dynamic symbol.
1174 Undefined weak syms won't yet be marked as dynamic. */
1175 if (h
->dynindx
== -1
1176 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
1178 if (! bfd_elf64_link_record_dynamic_symbol (info
, h
))
1182 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info
, h
))
1184 asection
*s
= htab
->splt
;
1186 /* If this is the first .plt entry, make room for the special
1188 if (s
->_raw_size
== 0)
1189 s
->_raw_size
+= PLT_ENTRY_SIZE
;
1191 h
->plt
.offset
= s
->_raw_size
;
1193 /* If this symbol is not defined in a regular file, and we are
1194 not generating a shared library, then set the symbol to this
1195 location in the .plt. This is required to make function
1196 pointers compare as equal between the normal executable and
1197 the shared library. */
1199 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1201 h
->root
.u
.def
.section
= s
;
1202 h
->root
.u
.def
.value
= h
->plt
.offset
;
1205 /* Make room for this entry. */
1206 s
->_raw_size
+= PLT_ENTRY_SIZE
;
1208 /* We also need to make an entry in the .got.plt section, which
1209 will be placed in the .got section by the linker script. */
1210 htab
->sgotplt
->_raw_size
+= GOT_ENTRY_SIZE
;
1212 /* We also need to make an entry in the .rela.plt section. */
1213 htab
->srelplt
->_raw_size
+= sizeof (Elf64_External_Rela
);
1217 h
->plt
.offset
= (bfd_vma
) -1;
1218 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1223 h
->plt
.offset
= (bfd_vma
) -1;
1224 h
->elf_link_hash_flags
&= ~ELF_LINK_HASH_NEEDS_PLT
;
1227 if (h
->got
.refcount
> 0)
1232 /* Make sure this symbol is output as a dynamic symbol.
1233 Undefined weak syms won't yet be marked as dynamic. */
1234 if (h
->dynindx
== -1
1235 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
1237 if (! bfd_elf64_link_record_dynamic_symbol (info
, h
))
1242 h
->got
.offset
= s
->_raw_size
;
1243 s
->_raw_size
+= GOT_ENTRY_SIZE
;
1244 dyn
= htab
->elf
.dynamic_sections_created
;
1245 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
, h
))
1246 htab
->srelgot
->_raw_size
+= sizeof (Elf64_External_Rela
);
1249 h
->got
.offset
= (bfd_vma
) -1;
1251 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1252 if (eh
->dyn_relocs
== NULL
)
1255 /* In the shared -Bsymbolic case, discard space allocated for
1256 dynamic pc-relative relocs against symbols which turn out to be
1257 defined in regular objects. For the normal shared case, discard
1258 space for pc-relative relocs that have become local due to symbol
1259 visibility changes. */
1263 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
1264 && ((h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0
1267 struct elf64_x86_64_dyn_relocs
**pp
;
1269 for (pp
= &eh
->dyn_relocs
; (p
= *pp
) != NULL
; )
1271 p
->count
-= p
->pc_count
;
1282 /* For the non-shared case, discard space for relocs against
1283 symbols which turn out to need copy relocs or are not
1286 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0
1287 && (((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1288 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
1289 || (htab
->elf
.dynamic_sections_created
1290 && (h
->root
.type
== bfd_link_hash_undefweak
1291 || h
->root
.type
== bfd_link_hash_undefined
))))
1293 /* Make sure this symbol is output as a dynamic symbol.
1294 Undefined weak syms won't yet be marked as dynamic. */
1295 if (h
->dynindx
== -1
1296 && (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) == 0)
1298 if (! bfd_elf64_link_record_dynamic_symbol (info
, h
))
1302 /* If that succeeded, we know we'll be keeping all the
1304 if (h
->dynindx
!= -1)
1308 eh
->dyn_relocs
= NULL
;
1313 /* Finally, allocate space. */
1314 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1316 asection
*sreloc
= elf_section_data (p
->sec
)->sreloc
;
1317 sreloc
->_raw_size
+= p
->count
* sizeof (Elf64_External_Rela
);
1323 /* Find any dynamic relocs that apply to read-only sections. */
1326 readonly_dynrelocs (h
, inf
)
1327 struct elf_link_hash_entry
*h
;
1330 struct elf64_x86_64_link_hash_entry
*eh
;
1331 struct elf64_x86_64_dyn_relocs
*p
;
1333 if (h
->root
.type
== bfd_link_hash_warning
)
1334 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1336 eh
= (struct elf64_x86_64_link_hash_entry
*) h
;
1337 for (p
= eh
->dyn_relocs
; p
!= NULL
; p
= p
->next
)
1339 asection
*s
= p
->sec
->output_section
;
1341 if (s
!= NULL
&& (s
->flags
& SEC_READONLY
) != 0)
1343 struct bfd_link_info
*info
= (struct bfd_link_info
*) inf
;
1345 info
->flags
|= DF_TEXTREL
;
1347 /* Not an error, just cut short the traversal. */
1354 /* Set the sizes of the dynamic sections. */
1357 elf64_x86_64_size_dynamic_sections (output_bfd
, info
)
1358 bfd
*output_bfd ATTRIBUTE_UNUSED
;
1359 struct bfd_link_info
*info
;
1361 struct elf64_x86_64_link_hash_table
*htab
;
1367 htab
= elf64_x86_64_hash_table (info
);
1368 dynobj
= htab
->elf
.dynobj
;
1372 if (htab
->elf
.dynamic_sections_created
)
1374 /* Set the contents of the .interp section to the interpreter. */
1377 s
= bfd_get_section_by_name (dynobj
, ".interp");
1380 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
1381 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
1385 /* Set up .got offsets for local syms, and space for local dynamic
1387 for (ibfd
= info
->input_bfds
; ibfd
!= NULL
; ibfd
= ibfd
->link_next
)
1389 bfd_signed_vma
*local_got
;
1390 bfd_signed_vma
*end_local_got
;
1391 bfd_size_type locsymcount
;
1392 Elf_Internal_Shdr
*symtab_hdr
;
1395 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
)
1398 for (s
= ibfd
->sections
; s
!= NULL
; s
= s
->next
)
1400 struct elf64_x86_64_dyn_relocs
*p
;
1402 for (p
= *((struct elf64_x86_64_dyn_relocs
**)
1403 &elf_section_data (s
)->local_dynrel
);
1407 if (!bfd_is_abs_section (p
->sec
)
1408 && bfd_is_abs_section (p
->sec
->output_section
))
1410 /* Input section has been discarded, either because
1411 it is a copy of a linkonce section or due to
1412 linker script /DISCARD/, so we'll be discarding
1415 else if (p
->count
!= 0)
1417 srel
= elf_section_data (p
->sec
)->sreloc
;
1418 srel
->_raw_size
+= p
->count
* sizeof (Elf64_External_Rela
);
1419 if ((p
->sec
->output_section
->flags
& SEC_READONLY
) != 0)
1420 info
->flags
|= DF_TEXTREL
;
1426 local_got
= elf_local_got_refcounts (ibfd
);
1430 symtab_hdr
= &elf_tdata (ibfd
)->symtab_hdr
;
1431 locsymcount
= symtab_hdr
->sh_info
;
1432 end_local_got
= local_got
+ locsymcount
;
1434 srel
= htab
->srelgot
;
1435 for (; local_got
< end_local_got
; ++local_got
)
1439 *local_got
= s
->_raw_size
;
1440 s
->_raw_size
+= GOT_ENTRY_SIZE
;
1442 srel
->_raw_size
+= sizeof (Elf64_External_Rela
);
1445 *local_got
= (bfd_vma
) -1;
1449 /* Allocate global sym .plt and .got entries, and space for global
1450 sym dynamic relocs. */
1451 elf_link_hash_traverse (&htab
->elf
, allocate_dynrelocs
, (PTR
) info
);
1453 /* We now have determined the sizes of the various dynamic sections.
1454 Allocate memory for them. */
1456 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
1458 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
1463 || s
== htab
->sgotplt
)
1465 /* Strip this section if we don't need it; see the
1468 else if (strncmp (bfd_get_section_name (dynobj
, s
), ".rela", 5) == 0)
1470 if (s
->_raw_size
!= 0 && s
!= htab
->srelplt
)
1473 /* We use the reloc_count field as a counter if we need
1474 to copy relocs into the output file. */
1479 /* It's not one of our sections, so don't allocate space. */
1483 if (s
->_raw_size
== 0)
1485 /* If we don't need this section, strip it from the
1486 output file. This is mostly to handle .rela.bss and
1487 .rela.plt. We must create both sections in
1488 create_dynamic_sections, because they must be created
1489 before the linker maps input sections to output
1490 sections. The linker does that before
1491 adjust_dynamic_symbol is called, and it is that
1492 function which decides whether anything needs to go
1493 into these sections. */
1495 _bfd_strip_section_from_output (info
, s
);
1499 /* Allocate memory for the section contents. We use bfd_zalloc
1500 here in case unused entries are not reclaimed before the
1501 section's contents are written out. This should not happen,
1502 but this way if it does, we get a R_X86_64_NONE reloc instead
1504 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->_raw_size
);
1505 if (s
->contents
== NULL
)
1509 if (htab
->elf
.dynamic_sections_created
)
1511 /* Add some entries to the .dynamic section. We fill in the
1512 values later, in elf64_x86_64_finish_dynamic_sections, but we
1513 must add the entries now so that we get the correct size for
1514 the .dynamic section. The DT_DEBUG entry is filled in by the
1515 dynamic linker and used by the debugger. */
1516 #define add_dynamic_entry(TAG, VAL) \
1517 bfd_elf64_add_dynamic_entry (info, (bfd_vma) (TAG), (bfd_vma) (VAL))
1521 if (!add_dynamic_entry (DT_DEBUG
, 0))
1525 if (htab
->splt
->_raw_size
!= 0)
1527 if (!add_dynamic_entry (DT_PLTGOT
, 0)
1528 || !add_dynamic_entry (DT_PLTRELSZ
, 0)
1529 || !add_dynamic_entry (DT_PLTREL
, DT_RELA
)
1530 || !add_dynamic_entry (DT_JMPREL
, 0))
1536 if (!add_dynamic_entry (DT_RELA
, 0)
1537 || !add_dynamic_entry (DT_RELASZ
, 0)
1538 || !add_dynamic_entry (DT_RELAENT
, sizeof (Elf64_External_Rela
)))
1541 /* If any dynamic relocs apply to a read-only section,
1542 then we need a DT_TEXTREL entry. */
1543 if ((info
->flags
& DF_TEXTREL
) == 0)
1544 elf_link_hash_traverse (&htab
->elf
, readonly_dynrelocs
,
1547 if ((info
->flags
& DF_TEXTREL
) != 0)
1549 if (!add_dynamic_entry (DT_TEXTREL
, 0))
1554 #undef add_dynamic_entry
1559 /* Relocate an x86_64 ELF section. */
1562 elf64_x86_64_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1563 contents
, relocs
, local_syms
, local_sections
)
1565 struct bfd_link_info
*info
;
1567 asection
*input_section
;
1569 Elf_Internal_Rela
*relocs
;
1570 Elf_Internal_Sym
*local_syms
;
1571 asection
**local_sections
;
1573 struct elf64_x86_64_link_hash_table
*htab
;
1574 Elf_Internal_Shdr
*symtab_hdr
;
1575 struct elf_link_hash_entry
**sym_hashes
;
1576 bfd_vma
*local_got_offsets
;
1577 Elf_Internal_Rela
*rel
;
1578 Elf_Internal_Rela
*relend
;
1580 if (info
->relocateable
)
1583 htab
= elf64_x86_64_hash_table (info
);
1584 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1585 sym_hashes
= elf_sym_hashes (input_bfd
);
1586 local_got_offsets
= elf_local_got_offsets (input_bfd
);
1589 relend
= relocs
+ input_section
->reloc_count
;
1590 for (; rel
< relend
; rel
++)
1593 reloc_howto_type
*howto
;
1594 unsigned long r_symndx
;
1595 struct elf_link_hash_entry
*h
;
1596 Elf_Internal_Sym
*sym
;
1600 boolean unresolved_reloc
;
1601 bfd_reloc_status_type r
;
1603 r_type
= ELF64_R_TYPE (rel
->r_info
);
1604 if (r_type
== (int) R_X86_64_GNU_VTINHERIT
1605 || r_type
== (int) R_X86_64_GNU_VTENTRY
)
1608 if (r_type
< 0 || r_type
>= R_X86_64_max
)
1610 bfd_set_error (bfd_error_bad_value
);
1614 howto
= x86_64_elf_howto_table
+ r_type
;
1615 r_symndx
= ELF64_R_SYM (rel
->r_info
);
1619 unresolved_reloc
= false;
1620 if (r_symndx
< symtab_hdr
->sh_info
)
1622 sym
= local_syms
+ r_symndx
;
1623 sec
= local_sections
[r_symndx
];
1625 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, sec
, rel
);
1629 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1630 while (h
->root
.type
== bfd_link_hash_indirect
1631 || h
->root
.type
== bfd_link_hash_warning
)
1632 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
1634 if (h
->root
.type
== bfd_link_hash_defined
1635 || h
->root
.type
== bfd_link_hash_defweak
)
1637 sec
= h
->root
.u
.def
.section
;
1638 if (sec
->output_section
== NULL
)
1640 /* Set a flag that will be cleared later if we find a
1641 relocation value for this symbol. output_section
1642 is typically NULL for symbols satisfied by a shared
1644 unresolved_reloc
= true;
1648 relocation
= (h
->root
.u
.def
.value
1649 + sec
->output_section
->vma
1650 + sec
->output_offset
);
1652 else if (h
->root
.type
== bfd_link_hash_undefweak
)
1654 else if (info
->shared
1655 && (!info
->symbolic
|| info
->allow_shlib_undefined
)
1656 && !info
->no_undefined
1657 && ELF_ST_VISIBILITY (h
->other
) == STV_DEFAULT
)
1661 if (! ((*info
->callbacks
->undefined_symbol
)
1662 (info
, h
->root
.root
.string
, input_bfd
,
1663 input_section
, rel
->r_offset
,
1664 (!info
->shared
|| info
->no_undefined
1665 || ELF_ST_VISIBILITY (h
->other
)))))
1670 /* When generating a shared object, the relocations handled here are
1671 copied into the output file to be resolved at run time. */
1674 case R_X86_64_GOT32
:
1675 /* Relocation is to the entry for this symbol in the global
1677 case R_X86_64_GOTPCREL
:
1678 /* Use global offset table as symbol value. */
1679 if (htab
->sgot
== NULL
)
1686 off
= h
->got
.offset
;
1687 dyn
= htab
->elf
.dynamic_sections_created
;
1689 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn
, info
, h
)
1693 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
))
1694 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
1696 /* This is actually a static link, or it is a -Bsymbolic
1697 link and the symbol is defined locally, or the symbol
1698 was forced to be local because of a version file. We
1699 must initialize this entry in the global offset table.
1700 Since the offset must always be a multiple of 8, we
1701 use the least significant bit to record whether we
1702 have initialized it already.
1704 When doing a dynamic link, we create a .rela.got
1705 relocation entry to initialize the value. This is
1706 done in the finish_dynamic_symbol routine. */
1711 bfd_put_64 (output_bfd
, relocation
,
1712 htab
->sgot
->contents
+ off
);
1717 unresolved_reloc
= false;
1721 if (local_got_offsets
== NULL
)
1724 off
= local_got_offsets
[r_symndx
];
1726 /* The offset must always be a multiple of 8. We use
1727 the least significant bit to record whether we have
1728 already generated the necessary reloc. */
1733 bfd_put_64 (output_bfd
, relocation
,
1734 htab
->sgot
->contents
+ off
);
1739 Elf_Internal_Rela outrel
;
1740 Elf64_External_Rela
*loc
;
1742 /* We need to generate a R_X86_64_RELATIVE reloc
1743 for the dynamic linker. */
1744 srelgot
= htab
->srelgot
;
1745 if (srelgot
== NULL
)
1748 outrel
.r_offset
= (htab
->sgot
->output_section
->vma
1749 + htab
->sgot
->output_offset
1751 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
1752 outrel
.r_addend
= relocation
;
1753 loc
= (Elf64_External_Rela
*) srelgot
->contents
;
1754 loc
+= srelgot
->reloc_count
++;
1755 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
1758 local_got_offsets
[r_symndx
] |= 1;
1762 if (off
>= (bfd_vma
) -2)
1765 relocation
= htab
->sgot
->output_offset
+ off
;
1766 if (r_type
== R_X86_64_GOTPCREL
)
1767 relocation
+= htab
->sgot
->output_section
->vma
;
1771 case R_X86_64_PLT32
:
1772 /* Relocation is to the entry for this symbol in the
1773 procedure linkage table. */
1775 /* Resolve a PLT32 reloc against a local symbol directly,
1776 without using the procedure linkage table. */
1780 if (h
->plt
.offset
== (bfd_vma
) -1
1781 || htab
->splt
== NULL
)
1783 /* We didn't make a PLT entry for this symbol. This
1784 happens when statically linking PIC code, or when
1785 using -Bsymbolic. */
1789 relocation
= (htab
->splt
->output_section
->vma
1790 + htab
->splt
->output_offset
1792 unresolved_reloc
= false;
1802 /* FIXME: The ABI says the linker should make sure the value is
1803 the same when it's zeroextended to 64 bit. */
1805 /* r_symndx will be zero only for relocs against symbols
1806 from removed linkonce sections, or sections discarded by
1809 || (input_section
->flags
& SEC_ALLOC
) == 0)
1813 && ((r_type
!= R_X86_64_PC8
1814 && r_type
!= R_X86_64_PC16
1815 && r_type
!= R_X86_64_PC32
)
1818 && (! info
->symbolic
1819 || (h
->elf_link_hash_flags
1820 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
1824 && (h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0
1825 && (((h
->elf_link_hash_flags
1826 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
1827 && (h
->elf_link_hash_flags
1828 & ELF_LINK_HASH_DEF_REGULAR
) == 0)
1829 || h
->root
.type
== bfd_link_hash_undefweak
1830 || h
->root
.type
== bfd_link_hash_undefined
)))
1832 Elf_Internal_Rela outrel
;
1833 boolean skip
, relocate
;
1835 Elf64_External_Rela
*loc
;
1837 /* When generating a shared object, these relocations
1838 are copied into the output file to be resolved at run
1845 _bfd_elf_section_offset (output_bfd
, info
, input_section
,
1847 if (outrel
.r_offset
== (bfd_vma
) -1)
1849 else if (outrel
.r_offset
== (bfd_vma
) -2)
1850 skip
= true, relocate
= true;
1852 outrel
.r_offset
+= (input_section
->output_section
->vma
1853 + input_section
->output_offset
);
1856 memset (&outrel
, 0, sizeof outrel
);
1858 /* h->dynindx may be -1 if this symbol was marked to
1862 && (r_type
== R_X86_64_PC8
1863 || r_type
== R_X86_64_PC16
1864 || r_type
== R_X86_64_PC32
1867 || (h
->elf_link_hash_flags
1868 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
1870 outrel
.r_info
= ELF64_R_INFO (h
->dynindx
, r_type
);
1871 outrel
.r_addend
= rel
->r_addend
;
1875 /* This symbol is local, or marked to become local. */
1876 if (r_type
== R_X86_64_64
)
1879 outrel
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
1880 outrel
.r_addend
= relocation
+ rel
->r_addend
;
1887 sec
= local_sections
[r_symndx
];
1890 BFD_ASSERT (h
->root
.type
== bfd_link_hash_defined
1892 == bfd_link_hash_defweak
));
1893 sec
= h
->root
.u
.def
.section
;
1895 if (sec
!= NULL
&& bfd_is_abs_section (sec
))
1897 else if (sec
== NULL
|| sec
->owner
== NULL
)
1899 bfd_set_error (bfd_error_bad_value
);
1906 osec
= sec
->output_section
;
1907 sindx
= elf_section_data (osec
)->dynindx
;
1908 BFD_ASSERT (sindx
> 0);
1911 outrel
.r_info
= ELF64_R_INFO (sindx
, r_type
);
1912 outrel
.r_addend
= relocation
+ rel
->r_addend
;
1916 sreloc
= elf_section_data (input_section
)->sreloc
;
1920 loc
= (Elf64_External_Rela
*) sreloc
->contents
;
1921 loc
+= sreloc
->reloc_count
++;
1922 bfd_elf64_swap_reloca_out (output_bfd
, &outrel
, loc
);
1924 /* If this reloc is against an external symbol, we do
1925 not want to fiddle with the addend. Otherwise, we
1926 need to include the symbol value so that it becomes
1927 an addend for the dynamic reloc. */
1938 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1939 because such sections are not SEC_ALLOC and thus ld.so will
1940 not process them. */
1941 if (unresolved_reloc
1942 && !((input_section
->flags
& SEC_DEBUGGING
) != 0
1943 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) != 0))
1944 (*_bfd_error_handler
)
1945 (_("%s(%s+0x%lx): unresolvable relocation against symbol `%s'"),
1946 bfd_archive_filename (input_bfd
),
1947 bfd_get_section_name (input_bfd
, input_section
),
1948 (long) rel
->r_offset
,
1949 h
->root
.root
.string
);
1951 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1952 contents
, rel
->r_offset
,
1953 relocation
, rel
->r_addend
);
1955 if (r
!= bfd_reloc_ok
)
1960 name
= h
->root
.root
.string
;
1963 name
= bfd_elf_string_from_elf_section (input_bfd
,
1964 symtab_hdr
->sh_link
,
1969 name
= bfd_section_name (input_bfd
, sec
);
1972 if (r
== bfd_reloc_overflow
)
1975 if (! ((*info
->callbacks
->reloc_overflow
)
1976 (info
, name
, howto
->name
, (bfd_vma
) 0,
1977 input_bfd
, input_section
, rel
->r_offset
)))
1982 (*_bfd_error_handler
)
1983 (_("%s(%s+0x%lx): reloc against `%s': error %d"),
1984 bfd_archive_filename (input_bfd
),
1985 bfd_get_section_name (input_bfd
, input_section
),
1986 (long) rel
->r_offset
, name
, (int) r
);
1995 /* Finish up dynamic symbol handling. We set the contents of various
1996 dynamic sections here. */
1999 elf64_x86_64_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
2001 struct bfd_link_info
*info
;
2002 struct elf_link_hash_entry
*h
;
2003 Elf_Internal_Sym
*sym
;
2005 struct elf64_x86_64_link_hash_table
*htab
;
2007 htab
= elf64_x86_64_hash_table (info
);
2009 if (h
->plt
.offset
!= (bfd_vma
) -1)
2013 Elf_Internal_Rela rela
;
2014 Elf64_External_Rela
*loc
;
2016 /* This symbol has an entry in the procedure linkage table. Set
2019 if (h
->dynindx
== -1
2020 || htab
->splt
== NULL
2021 || htab
->sgotplt
== NULL
2022 || htab
->srelplt
== NULL
)
2025 /* Get the index in the procedure linkage table which
2026 corresponds to this symbol. This is the index of this symbol
2027 in all the symbols for which we are making plt entries. The
2028 first entry in the procedure linkage table is reserved. */
2029 plt_index
= h
->plt
.offset
/ PLT_ENTRY_SIZE
- 1;
2031 /* Get the offset into the .got table of the entry that
2032 corresponds to this function. Each .got entry is GOT_ENTRY_SIZE
2033 bytes. The first three are reserved for the dynamic linker. */
2034 got_offset
= (plt_index
+ 3) * GOT_ENTRY_SIZE
;
2036 /* Fill in the entry in the procedure linkage table. */
2037 memcpy (htab
->splt
->contents
+ h
->plt
.offset
, elf64_x86_64_plt_entry
,
2040 /* Insert the relocation positions of the plt section. The magic
2041 numbers at the end of the statements are the positions of the
2042 relocations in the plt section. */
2043 /* Put offset for jmp *name@GOTPCREL(%rip), since the
2044 instruction uses 6 bytes, subtract this value. */
2045 bfd_put_32 (output_bfd
,
2046 (htab
->sgotplt
->output_section
->vma
2047 + htab
->sgotplt
->output_offset
2049 - htab
->splt
->output_section
->vma
2050 - htab
->splt
->output_offset
2053 htab
->splt
->contents
+ h
->plt
.offset
+ 2);
2054 /* Put relocation index. */
2055 bfd_put_32 (output_bfd
, plt_index
,
2056 htab
->splt
->contents
+ h
->plt
.offset
+ 7);
2057 /* Put offset for jmp .PLT0. */
2058 bfd_put_32 (output_bfd
, - (h
->plt
.offset
+ PLT_ENTRY_SIZE
),
2059 htab
->splt
->contents
+ h
->plt
.offset
+ 12);
2061 /* Fill in the entry in the global offset table, initially this
2062 points to the pushq instruction in the PLT which is at offset 6. */
2063 bfd_put_64 (output_bfd
, (htab
->splt
->output_section
->vma
2064 + htab
->splt
->output_offset
2065 + h
->plt
.offset
+ 6),
2066 htab
->sgotplt
->contents
+ got_offset
);
2068 /* Fill in the entry in the .rela.plt section. */
2069 rela
.r_offset
= (htab
->sgotplt
->output_section
->vma
2070 + htab
->sgotplt
->output_offset
2072 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_JUMP_SLOT
);
2074 loc
= (Elf64_External_Rela
*) htab
->srelplt
->contents
+ plt_index
;
2075 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
2077 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
2079 /* Mark the symbol as undefined, rather than as defined in
2080 the .plt section. Leave the value alone. This is a clue
2081 for the dynamic linker, to make function pointer
2082 comparisons work between an application and shared
2084 sym
->st_shndx
= SHN_UNDEF
;
2088 if (h
->got
.offset
!= (bfd_vma
) -1)
2090 Elf_Internal_Rela rela
;
2091 Elf64_External_Rela
*loc
;
2093 /* This symbol has an entry in the global offset table. Set it
2096 if (htab
->sgot
== NULL
|| htab
->srelgot
== NULL
)
2099 rela
.r_offset
= (htab
->sgot
->output_section
->vma
2100 + htab
->sgot
->output_offset
2101 + (h
->got
.offset
&~ (bfd_vma
) 1));
2103 /* If this is a static link, or it is a -Bsymbolic link and the
2104 symbol is defined locally or was forced to be local because
2105 of a version file, we just want to emit a RELATIVE reloc.
2106 The entry in the global offset table will already have been
2107 initialized in the relocate_section function. */
2111 || (h
->elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
))
2112 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
))
2114 BFD_ASSERT((h
->got
.offset
& 1) != 0);
2115 rela
.r_info
= ELF64_R_INFO (0, R_X86_64_RELATIVE
);
2116 rela
.r_addend
= (h
->root
.u
.def
.value
2117 + h
->root
.u
.def
.section
->output_section
->vma
2118 + h
->root
.u
.def
.section
->output_offset
);
2122 BFD_ASSERT((h
->got
.offset
& 1) == 0);
2123 bfd_put_64 (output_bfd
, (bfd_vma
) 0,
2124 htab
->sgot
->contents
+ h
->got
.offset
);
2125 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_GLOB_DAT
);
2129 loc
= (Elf64_External_Rela
*) htab
->srelgot
->contents
;
2130 loc
+= htab
->srelgot
->reloc_count
++;
2131 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
2134 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
2136 Elf_Internal_Rela rela
;
2137 Elf64_External_Rela
*loc
;
2139 /* This symbol needs a copy reloc. Set it up. */
2141 if (h
->dynindx
== -1
2142 || (h
->root
.type
!= bfd_link_hash_defined
2143 && h
->root
.type
!= bfd_link_hash_defweak
)
2144 || htab
->srelbss
== NULL
)
2147 rela
.r_offset
= (h
->root
.u
.def
.value
2148 + h
->root
.u
.def
.section
->output_section
->vma
2149 + h
->root
.u
.def
.section
->output_offset
);
2150 rela
.r_info
= ELF64_R_INFO (h
->dynindx
, R_X86_64_COPY
);
2152 loc
= (Elf64_External_Rela
*) htab
->srelbss
->contents
;
2153 loc
+= htab
->srelbss
->reloc_count
++;
2154 bfd_elf64_swap_reloca_out (output_bfd
, &rela
, loc
);
2157 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
2158 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
2159 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
2160 sym
->st_shndx
= SHN_ABS
;
2165 /* Used to decide how to sort relocs in an optimal manner for the
2166 dynamic linker, before writing them out. */
2168 static enum elf_reloc_type_class
2169 elf64_x86_64_reloc_type_class (rela
)
2170 const Elf_Internal_Rela
*rela
;
2172 switch ((int) ELF64_R_TYPE (rela
->r_info
))
2174 case R_X86_64_RELATIVE
:
2175 return reloc_class_relative
;
2176 case R_X86_64_JUMP_SLOT
:
2177 return reloc_class_plt
;
2179 return reloc_class_copy
;
2181 return reloc_class_normal
;
2185 /* Finish up the dynamic sections. */
2188 elf64_x86_64_finish_dynamic_sections (output_bfd
, info
)
2190 struct bfd_link_info
*info
;
2192 struct elf64_x86_64_link_hash_table
*htab
;
2196 htab
= elf64_x86_64_hash_table (info
);
2197 dynobj
= htab
->elf
.dynobj
;
2198 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
2200 if (htab
->elf
.dynamic_sections_created
)
2202 Elf64_External_Dyn
*dyncon
, *dynconend
;
2204 if (sdyn
== NULL
|| htab
->sgot
== NULL
)
2207 dyncon
= (Elf64_External_Dyn
*) sdyn
->contents
;
2208 dynconend
= (Elf64_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
2209 for (; dyncon
< dynconend
; dyncon
++)
2211 Elf_Internal_Dyn dyn
;
2214 bfd_elf64_swap_dyn_in (dynobj
, dyncon
, &dyn
);
2222 dyn
.d_un
.d_ptr
= htab
->sgot
->output_section
->vma
;
2226 dyn
.d_un
.d_ptr
= htab
->srelplt
->output_section
->vma
;
2230 s
= htab
->srelplt
->output_section
;
2231 if (s
->_cooked_size
!= 0)
2232 dyn
.d_un
.d_val
= s
->_cooked_size
;
2234 dyn
.d_un
.d_val
= s
->_raw_size
;
2238 /* The procedure linkage table relocs (DT_JMPREL) should
2239 not be included in the overall relocs (DT_RELA).
2240 Therefore, we override the DT_RELASZ entry here to
2241 make it not include the JMPREL relocs. Since the
2242 linker script arranges for .rela.plt to follow all
2243 other relocation sections, we don't have to worry
2244 about changing the DT_RELA entry. */
2245 if (htab
->srelplt
!= NULL
)
2247 s
= htab
->srelplt
->output_section
;
2248 if (s
->_cooked_size
!= 0)
2249 dyn
.d_un
.d_val
-= s
->_cooked_size
;
2251 dyn
.d_un
.d_val
-= s
->_raw_size
;
2256 bfd_elf64_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
2259 /* Fill in the special first entry in the procedure linkage table. */
2260 if (htab
->splt
&& htab
->splt
->_raw_size
> 0)
2262 /* Fill in the first entry in the procedure linkage table. */
2263 memcpy (htab
->splt
->contents
, elf64_x86_64_plt0_entry
,
2265 /* Add offset for pushq GOT+8(%rip), since the instruction
2266 uses 6 bytes subtract this value. */
2267 bfd_put_32 (output_bfd
,
2268 (htab
->sgotplt
->output_section
->vma
2269 + htab
->sgotplt
->output_offset
2271 - htab
->splt
->output_section
->vma
2272 - htab
->splt
->output_offset
2274 htab
->splt
->contents
+ 2);
2275 /* Add offset for jmp *GOT+16(%rip). The 12 is the offset to
2276 the end of the instruction. */
2277 bfd_put_32 (output_bfd
,
2278 (htab
->sgotplt
->output_section
->vma
2279 + htab
->sgotplt
->output_offset
2281 - htab
->splt
->output_section
->vma
2282 - htab
->splt
->output_offset
2284 htab
->splt
->contents
+ 8);
2286 elf_section_data (htab
->splt
->output_section
)->this_hdr
.sh_entsize
=
2293 /* Fill in the first three entries in the global offset table. */
2294 if (htab
->sgotplt
->_raw_size
> 0)
2296 /* Set the first entry in the global offset table to the address of
2297 the dynamic section. */
2299 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
);
2301 bfd_put_64 (output_bfd
,
2302 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
2303 htab
->sgotplt
->contents
);
2304 /* Write GOT[1] and GOT[2], needed for the dynamic linker. */
2305 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
);
2306 bfd_put_64 (output_bfd
, (bfd_vma
) 0, htab
->sgotplt
->contents
+ GOT_ENTRY_SIZE
*2);
2309 elf_section_data (htab
->sgotplt
->output_section
)->this_hdr
.sh_entsize
=
2317 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
2318 #define TARGET_LITTLE_NAME "elf64-x86-64"
2319 #define ELF_ARCH bfd_arch_i386
2320 #define ELF_MACHINE_CODE EM_X86_64
2321 #define ELF_MAXPAGESIZE 0x100000
2323 #define elf_backend_can_gc_sections 1
2324 #define elf_backend_can_refcount 1
2325 #define elf_backend_want_got_plt 1
2326 #define elf_backend_plt_readonly 1
2327 #define elf_backend_want_plt_sym 0
2328 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
2329 #define elf_backend_plt_header_size PLT_ENTRY_SIZE
2330 #define elf_backend_rela_normal 1
2332 #define elf_info_to_howto elf64_x86_64_info_to_howto
2334 #define bfd_elf64_bfd_link_hash_table_create \
2335 elf64_x86_64_link_hash_table_create
2336 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
2338 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
2339 #define elf_backend_check_relocs elf64_x86_64_check_relocs
2340 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
2341 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
2342 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
2343 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
2344 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
2345 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
2346 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
2347 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
2348 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
2349 #define elf_backend_relocate_section elf64_x86_64_relocate_section
2350 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
2351 #define elf_backend_object_p elf64_x86_64_elf_object_p
2353 #include "elf64-target.h"