1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
49 // A class to handle the PLT data.
51 class Output_data_plt_x86_64
: public Output_section_data
54 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
56 Output_data_plt_x86_64(Symbol_table
* symtab
, Layout
* layout
,
57 Output_data_got
<64, false>* got
,
58 Output_data_space
* got_plt
)
59 : Output_section_data(8), tlsdesc_rel_(NULL
), got_(got
), got_plt_(got_plt
),
60 count_(0), tlsdesc_got_offset_(-1U), free_list_()
61 { this->init(symtab
, layout
); }
63 Output_data_plt_x86_64(Symbol_table
* symtab
, Layout
* layout
,
64 Output_data_got
<64, false>* got
,
65 Output_data_space
* got_plt
,
66 unsigned int plt_count
)
67 : Output_section_data((plt_count
+ 1) * plt_entry_size
, 8, false),
68 tlsdesc_rel_(NULL
), got_(got
), got_plt_(got_plt
),
69 count_(plt_count
), tlsdesc_got_offset_(-1U), free_list_()
71 this->init(symtab
, layout
);
73 // Initialize the free list and reserve the first entry.
74 this->free_list_
.init((plt_count
+ 1) * plt_entry_size
, false);
75 this->free_list_
.remove(0, plt_entry_size
);
78 // Initialize the PLT section.
80 init(Symbol_table
* symtab
, Layout
* layout
);
82 // Add an entry to the PLT.
84 add_entry(Symbol
* gsym
);
86 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
88 add_local_ifunc_entry(Sized_relobj_file
<64, false>* relobj
,
89 unsigned int local_sym_index
);
91 // Add the relocation for a PLT entry.
93 add_relocation(Symbol
* gsym
, unsigned int got_offset
);
95 // Add the reserved TLSDESC_PLT entry to the PLT.
97 reserve_tlsdesc_entry(unsigned int got_offset
)
98 { this->tlsdesc_got_offset_
= got_offset
; }
100 // Return true if a TLSDESC_PLT entry has been reserved.
102 has_tlsdesc_entry() const
103 { return this->tlsdesc_got_offset_
!= -1U; }
105 // Return the GOT offset for the reserved TLSDESC_PLT entry.
107 get_tlsdesc_got_offset() const
108 { return this->tlsdesc_got_offset_
; }
110 // Return the offset of the reserved TLSDESC_PLT entry.
112 get_tlsdesc_plt_offset() const
113 { return (this->count_
+ 1) * plt_entry_size
; }
115 // Return the .rela.plt section data.
118 { return this->rel_
; }
120 // Return where the TLSDESC relocations should go.
122 rela_tlsdesc(Layout
*);
124 // Return the number of PLT entries.
127 { return this->count_
; }
129 // Return the offset of the first non-reserved PLT entry.
131 first_plt_entry_offset()
132 { return plt_entry_size
; }
134 // Return the size of a PLT entry.
137 { return plt_entry_size
; }
139 // Reserve a slot in the PLT for an existing symbol in an incremental update.
141 reserve_slot(unsigned int plt_index
)
143 this->free_list_
.remove((plt_index
+ 1) * plt_entry_size
,
144 (plt_index
+ 2) * plt_entry_size
);
149 do_adjust_output_section(Output_section
* os
);
151 // Write to a map file.
153 do_print_to_mapfile(Mapfile
* mapfile
) const
154 { mapfile
->print_output_data(this, _("** PLT")); }
157 // The size of an entry in the PLT.
158 static const int plt_entry_size
= 16;
160 // The first entry in the PLT.
161 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
162 // procedure linkage table for both programs and shared objects."
163 static unsigned char first_plt_entry
[plt_entry_size
];
165 // Other entries in the PLT for an executable.
166 static unsigned char plt_entry
[plt_entry_size
];
168 // The reserved TLSDESC entry in the PLT for an executable.
169 static unsigned char tlsdesc_plt_entry
[plt_entry_size
];
171 // Set the final size.
173 set_final_data_size();
175 // Write out the PLT data.
177 do_write(Output_file
*);
179 // The reloc section.
181 // The TLSDESC relocs, if necessary. These must follow the regular
183 Reloc_section
* tlsdesc_rel_
;
185 Output_data_got
<64, false>* got_
;
186 // The .got.plt section.
187 Output_data_space
* got_plt_
;
188 // The number of PLT entries.
190 // Offset of the reserved TLSDESC_GOT entry when needed.
191 unsigned int tlsdesc_got_offset_
;
192 // List of available regions within the section, for incremental
194 Free_list free_list_
;
197 // The x86_64 target class.
199 // http://www.x86-64.org/documentation/abi.pdf
200 // TLS info comes from
201 // http://people.redhat.com/drepper/tls.pdf
202 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
204 class Target_x86_64
: public Target_freebsd
<64, false>
207 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
208 // uses only Elf64_Rela relocation entries with explicit addends."
209 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
212 : Target_freebsd
<64, false>(&x86_64_info
),
213 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_tlsdesc_(NULL
),
214 global_offset_table_(NULL
), rela_dyn_(NULL
),
215 copy_relocs_(elfcpp::R_X86_64_COPY
), dynbss_(NULL
),
216 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
217 tls_base_symbol_defined_(false)
220 // This function should be defined in targets that can use relocation
221 // types to determine (implemented in local_reloc_may_be_function_pointer
222 // and global_reloc_may_be_function_pointer)
223 // if a function's pointer is taken. ICF uses this in safe mode to only
224 // fold those functions whose pointer is defintely not taken. For x86_64
225 // pie binaries, safe ICF cannot be done by looking at relocation types.
227 can_check_for_function_pointers() const
228 { return !parameters
->options().pie(); }
231 can_icf_inline_merge_sections () const
234 // Hook for a new output section.
236 do_new_output_section(Output_section
*) const;
238 // Scan the relocations to look for symbol adjustments.
240 gc_process_relocs(Symbol_table
* symtab
,
242 Sized_relobj_file
<64, false>* object
,
243 unsigned int data_shndx
,
244 unsigned int sh_type
,
245 const unsigned char* prelocs
,
247 Output_section
* output_section
,
248 bool needs_special_offset_handling
,
249 size_t local_symbol_count
,
250 const unsigned char* plocal_symbols
);
252 // Scan the relocations to look for symbol adjustments.
254 scan_relocs(Symbol_table
* symtab
,
256 Sized_relobj_file
<64, false>* object
,
257 unsigned int data_shndx
,
258 unsigned int sh_type
,
259 const unsigned char* prelocs
,
261 Output_section
* output_section
,
262 bool needs_special_offset_handling
,
263 size_t local_symbol_count
,
264 const unsigned char* plocal_symbols
);
266 // Finalize the sections.
268 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
270 // Return the value to use for a dynamic which requires special
273 do_dynsym_value(const Symbol
*) const;
275 // Relocate a section.
277 relocate_section(const Relocate_info
<64, false>*,
278 unsigned int sh_type
,
279 const unsigned char* prelocs
,
281 Output_section
* output_section
,
282 bool needs_special_offset_handling
,
284 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
285 section_size_type view_size
,
286 const Reloc_symbol_changes
*);
288 // Scan the relocs during a relocatable link.
290 scan_relocatable_relocs(Symbol_table
* symtab
,
292 Sized_relobj_file
<64, false>* object
,
293 unsigned int data_shndx
,
294 unsigned int sh_type
,
295 const unsigned char* prelocs
,
297 Output_section
* output_section
,
298 bool needs_special_offset_handling
,
299 size_t local_symbol_count
,
300 const unsigned char* plocal_symbols
,
301 Relocatable_relocs
*);
303 // Relocate a section during a relocatable link.
305 relocate_for_relocatable(const Relocate_info
<64, false>*,
306 unsigned int sh_type
,
307 const unsigned char* prelocs
,
309 Output_section
* output_section
,
310 off_t offset_in_output_section
,
311 const Relocatable_relocs
*,
313 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
314 section_size_type view_size
,
315 unsigned char* reloc_view
,
316 section_size_type reloc_view_size
);
318 // Return a string used to fill a code section with nops.
320 do_code_fill(section_size_type length
) const;
322 // Return whether SYM is defined by the ABI.
324 do_is_defined_by_abi(const Symbol
* sym
) const
325 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
327 // Return the symbol index to use for a target specific relocation.
328 // The only target specific relocation is R_X86_64_TLSDESC for a
329 // local symbol, which is an absolute reloc.
331 do_reloc_symbol_index(void*, unsigned int r_type
) const
333 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
337 // Return the addend to use for a target specific relocation.
339 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
341 // Return the PLT section.
343 do_plt_section_for_global(const Symbol
*) const
344 { return this->plt_section(); }
347 do_plt_section_for_local(const Relobj
*, unsigned int) const
348 { return this->plt_section(); }
350 // Adjust -fsplit-stack code which calls non-split-stack code.
352 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
353 section_offset_type fnoffset
, section_size_type fnsize
,
354 unsigned char* view
, section_size_type view_size
,
355 std::string
* from
, std::string
* to
) const;
357 // Return the size of the GOT section.
361 gold_assert(this->got_
!= NULL
);
362 return this->got_
->data_size();
365 // Return the number of entries in the GOT.
367 got_entry_count() const
369 if (this->got_
== NULL
)
371 return this->got_size() / 8;
374 // Return the number of entries in the PLT.
376 plt_entry_count() const;
378 // Return the offset of the first non-reserved PLT entry.
380 first_plt_entry_offset() const;
382 // Return the size of each PLT entry.
384 plt_entry_size() const;
386 // Create the GOT section for an incremental update.
387 Output_data_got
<64, false>*
388 init_got_plt_for_update(Symbol_table
* symtab
,
390 unsigned int got_count
,
391 unsigned int plt_count
);
393 // Reserve a GOT entry for a local symbol, and regenerate any
394 // necessary dynamic relocations.
396 reserve_local_got_entry(unsigned int got_index
,
397 Sized_relobj
<64, false>* obj
,
399 unsigned int got_type
);
401 // Reserve a GOT entry for a global symbol, and regenerate any
402 // necessary dynamic relocations.
404 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
405 unsigned int got_type
);
407 // Register an existing PLT entry for a global symbol.
409 register_global_plt_entry(unsigned int plt_index
, Symbol
* gsym
);
411 // Force a COPY relocation for a given symbol.
413 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
415 // Apply an incremental relocation.
417 apply_relocation(const Relocate_info
<64, false>* relinfo
,
418 elfcpp::Elf_types
<64>::Elf_Addr r_offset
,
420 elfcpp::Elf_types
<64>::Elf_Swxword r_addend
,
423 elfcpp::Elf_types
<64>::Elf_Addr address
,
424 section_size_type view_size
);
426 // Add a new reloc argument, returning the index in the vector.
428 add_tlsdesc_info(Sized_relobj_file
<64, false>* object
, unsigned int r_sym
)
430 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
431 return this->tlsdesc_reloc_info_
.size() - 1;
435 // The class which scans relocations.
440 : issued_non_pic_error_(false)
444 get_reference_flags(unsigned int r_type
);
447 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
448 Sized_relobj_file
<64, false>* object
,
449 unsigned int data_shndx
,
450 Output_section
* output_section
,
451 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
452 const elfcpp::Sym
<64, false>& lsym
);
455 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
456 Sized_relobj_file
<64, false>* object
,
457 unsigned int data_shndx
,
458 Output_section
* output_section
,
459 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
463 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
464 Target_x86_64
* target
,
465 Sized_relobj_file
<64, false>* object
,
466 unsigned int data_shndx
,
467 Output_section
* output_section
,
468 const elfcpp::Rela
<64, false>& reloc
,
470 const elfcpp::Sym
<64, false>& lsym
);
473 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
474 Target_x86_64
* target
,
475 Sized_relobj_file
<64, false>* object
,
476 unsigned int data_shndx
,
477 Output_section
* output_section
,
478 const elfcpp::Rela
<64, false>& reloc
,
484 unsupported_reloc_local(Sized_relobj_file
<64, false>*, unsigned int r_type
);
487 unsupported_reloc_global(Sized_relobj_file
<64, false>*, unsigned int r_type
,
491 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
494 possible_function_pointer_reloc(unsigned int r_type
);
497 reloc_needs_plt_for_ifunc(Sized_relobj_file
<64, false>*,
498 unsigned int r_type
);
500 // Whether we have issued an error about a non-PIC compilation.
501 bool issued_non_pic_error_
;
504 // The class which implements relocation.
509 : skip_call_tls_get_addr_(false)
514 if (this->skip_call_tls_get_addr_
)
516 // FIXME: This needs to specify the location somehow.
517 gold_error(_("missing expected TLS relocation"));
521 // Do a relocation. Return false if the caller should not issue
522 // any warnings about this relocation.
524 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, Output_section
*,
525 size_t relnum
, const elfcpp::Rela
<64, false>&,
526 unsigned int r_type
, const Sized_symbol
<64>*,
527 const Symbol_value
<64>*,
528 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
532 // Do a TLS relocation.
534 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
535 size_t relnum
, const elfcpp::Rela
<64, false>&,
536 unsigned int r_type
, const Sized_symbol
<64>*,
537 const Symbol_value
<64>*,
538 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
541 // Do a TLS General-Dynamic to Initial-Exec transition.
543 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
544 Output_segment
* tls_segment
,
545 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
546 elfcpp::Elf_types
<64>::Elf_Addr value
,
548 elfcpp::Elf_types
<64>::Elf_Addr
,
549 section_size_type view_size
);
551 // Do a TLS General-Dynamic to Local-Exec transition.
553 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
554 Output_segment
* tls_segment
,
555 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
556 elfcpp::Elf_types
<64>::Elf_Addr value
,
558 section_size_type view_size
);
560 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
562 tls_desc_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
563 Output_segment
* tls_segment
,
564 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
565 elfcpp::Elf_types
<64>::Elf_Addr value
,
567 elfcpp::Elf_types
<64>::Elf_Addr
,
568 section_size_type view_size
);
570 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
572 tls_desc_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
573 Output_segment
* tls_segment
,
574 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
575 elfcpp::Elf_types
<64>::Elf_Addr value
,
577 section_size_type view_size
);
579 // Do a TLS Local-Dynamic to Local-Exec transition.
581 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
582 Output_segment
* tls_segment
,
583 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
584 elfcpp::Elf_types
<64>::Elf_Addr value
,
586 section_size_type view_size
);
588 // Do a TLS Initial-Exec to Local-Exec transition.
590 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
591 Output_segment
* tls_segment
,
592 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
593 elfcpp::Elf_types
<64>::Elf_Addr value
,
595 section_size_type view_size
);
597 // This is set if we should skip the next reloc, which should be a
598 // PLT32 reloc against ___tls_get_addr.
599 bool skip_call_tls_get_addr_
;
602 // A class which returns the size required for a relocation type,
603 // used while scanning relocs during a relocatable link.
604 class Relocatable_size_for_reloc
608 get_size_for_reloc(unsigned int, Relobj
*);
611 // Adjust TLS relocation type based on the options and whether this
612 // is a local symbol.
613 static tls::Tls_optimization
614 optimize_tls_reloc(bool is_final
, int r_type
);
616 // Get the GOT section, creating it if necessary.
617 Output_data_got
<64, false>*
618 got_section(Symbol_table
*, Layout
*);
620 // Get the GOT PLT section.
622 got_plt_section() const
624 gold_assert(this->got_plt_
!= NULL
);
625 return this->got_plt_
;
628 // Get the GOT section for TLSDESC entries.
629 Output_data_got
<64, false>*
630 got_tlsdesc_section() const
632 gold_assert(this->got_tlsdesc_
!= NULL
);
633 return this->got_tlsdesc_
;
636 // Create the PLT section.
638 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
640 // Create a PLT entry for a global symbol.
642 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
644 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
646 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
647 Sized_relobj_file
<64, false>* relobj
,
648 unsigned int local_sym_index
);
650 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
652 define_tls_base_symbol(Symbol_table
*, Layout
*);
654 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
656 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
658 // Create a GOT entry for the TLS module index.
660 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
661 Sized_relobj_file
<64, false>* object
);
663 // Get the PLT section.
664 Output_data_plt_x86_64
*
667 gold_assert(this->plt_
!= NULL
);
671 // Get the dynamic reloc section, creating it if necessary.
673 rela_dyn_section(Layout
*);
675 // Get the section to use for TLSDESC relocations.
677 rela_tlsdesc_section(Layout
*) const;
679 // Add a potential copy relocation.
681 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
682 Sized_relobj_file
<64, false>* object
,
683 unsigned int shndx
, Output_section
* output_section
,
684 Symbol
* sym
, const elfcpp::Rela
<64, false>& reloc
)
686 this->copy_relocs_
.copy_reloc(symtab
, layout
,
687 symtab
->get_sized_symbol
<64>(sym
),
688 object
, shndx
, output_section
,
689 reloc
, this->rela_dyn_section(layout
));
692 // Information about this specific target which we pass to the
693 // general Target structure.
694 static const Target::Target_info x86_64_info
;
696 // The types of GOT entries needed for this platform.
697 // These values are exposed to the ABI in an incremental link.
698 // Do not renumber existing values without changing the version
699 // number of the .gnu_incremental_inputs section.
702 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
703 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
704 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
705 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
708 // This type is used as the argument to the target specific
709 // relocation routines. The only target specific reloc is
710 // R_X86_64_TLSDESC against a local symbol.
713 Tlsdesc_info(Sized_relobj_file
<64, false>* a_object
, unsigned int a_r_sym
)
714 : object(a_object
), r_sym(a_r_sym
)
717 // The object in which the local symbol is defined.
718 Sized_relobj_file
<64, false>* object
;
719 // The local symbol index in the object.
724 Output_data_got
<64, false>* got_
;
726 Output_data_plt_x86_64
* plt_
;
727 // The GOT PLT section.
728 Output_data_space
* got_plt_
;
729 // The GOT section for TLSDESC relocations.
730 Output_data_got
<64, false>* got_tlsdesc_
;
731 // The _GLOBAL_OFFSET_TABLE_ symbol.
732 Symbol
* global_offset_table_
;
733 // The dynamic reloc section.
734 Reloc_section
* rela_dyn_
;
735 // Relocs saved to avoid a COPY reloc.
736 Copy_relocs
<elfcpp::SHT_RELA
, 64, false> copy_relocs_
;
737 // Space for variables copied with a COPY reloc.
738 Output_data_space
* dynbss_
;
739 // Offset of the GOT entry for the TLS module index.
740 unsigned int got_mod_index_offset_
;
741 // We handle R_X86_64_TLSDESC against a local symbol as a target
742 // specific relocation. Here we store the object and local symbol
743 // index for the relocation.
744 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
745 // True if the _TLS_MODULE_BASE_ symbol has been defined.
746 bool tls_base_symbol_defined_
;
749 const Target::Target_info
Target_x86_64::x86_64_info
=
752 false, // is_big_endian
753 elfcpp::EM_X86_64
, // machine_code
754 false, // has_make_symbol
755 false, // has_resolve
756 true, // has_code_fill
757 true, // is_default_stack_executable
759 "/lib/ld64.so.1", // program interpreter
760 0x400000, // default_text_segment_address
761 0x1000, // abi_pagesize (overridable by -z max-page-size)
762 0x1000, // common_pagesize (overridable by -z common-page-size)
763 elfcpp::SHN_UNDEF
, // small_common_shndx
764 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
765 0, // small_common_section_flags
766 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
767 NULL
, // attributes_section
768 NULL
// attributes_vendor
771 // This is called when a new output section is created. This is where
772 // we handle the SHF_X86_64_LARGE.
775 Target_x86_64::do_new_output_section(Output_section
* os
) const
777 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
778 os
->set_is_large_section();
781 // Get the GOT section, creating it if necessary.
783 Output_data_got
<64, false>*
784 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
786 if (this->got_
== NULL
)
788 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
790 this->got_
= new Output_data_got
<64, false>();
792 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
794 | elfcpp::SHF_WRITE
),
795 this->got_
, ORDER_RELRO_LAST
,
798 this->got_plt_
= new Output_data_space(8, "** GOT PLT");
799 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
801 | elfcpp::SHF_WRITE
),
802 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
805 // The first three entries are reserved.
806 this->got_plt_
->set_current_data_size(3 * 8);
808 // Those bytes can go into the relro segment.
809 layout
->increase_relro(3 * 8);
811 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
812 this->global_offset_table_
=
813 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
814 Symbol_table::PREDEFINED
,
816 0, 0, elfcpp::STT_OBJECT
,
818 elfcpp::STV_HIDDEN
, 0,
821 // If there are any TLSDESC relocations, they get GOT entries in
822 // .got.plt after the jump slot entries.
823 this->got_tlsdesc_
= new Output_data_got
<64, false>();
824 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
826 | elfcpp::SHF_WRITE
),
828 ORDER_NON_RELRO_FIRST
, false);
834 // Get the dynamic reloc section, creating it if necessary.
836 Target_x86_64::Reloc_section
*
837 Target_x86_64::rela_dyn_section(Layout
* layout
)
839 if (this->rela_dyn_
== NULL
)
841 gold_assert(layout
!= NULL
);
842 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
843 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
844 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
845 ORDER_DYNAMIC_RELOCS
, false);
847 return this->rela_dyn_
;
850 // Initialize the PLT section.
853 Output_data_plt_x86_64::init(Symbol_table
* symtab
, Layout
* layout
)
855 this->rel_
= new Reloc_section(false);
856 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
857 elfcpp::SHF_ALLOC
, this->rel_
,
858 ORDER_DYNAMIC_PLT_RELOCS
, false);
860 if (parameters
->doing_static_link())
862 // A statically linked executable will only have a .rela.plt
863 // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
864 // symbols. The library will use these symbols to locate the
865 // IRELATIVE relocs at program startup time.
866 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
867 Symbol_table::PREDEFINED
,
868 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
869 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
871 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
872 Symbol_table::PREDEFINED
,
873 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
874 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
880 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
882 os
->set_entsize(plt_entry_size
);
885 // Add an entry to the PLT.
888 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
890 gold_assert(!gsym
->has_plt_offset());
892 unsigned int plt_index
;
894 section_offset_type got_offset
;
896 if (!this->is_data_size_valid())
898 // Note that when setting the PLT offset we skip the initial
899 // reserved PLT entry.
900 plt_index
= this->count_
+ 1;
901 plt_offset
= plt_index
* plt_entry_size
;
905 got_offset
= (plt_index
- 1 + 3) * 8;
906 gold_assert(got_offset
== this->got_plt_
->current_data_size());
908 // Every PLT entry needs a GOT entry which points back to the PLT
909 // entry (this will be changed by the dynamic linker, normally
910 // lazily when the function is called).
911 this->got_plt_
->set_current_data_size(got_offset
+ 8);
915 // For incremental updates, find an available slot.
916 plt_offset
= this->free_list_
.allocate(plt_entry_size
, plt_entry_size
, 0);
917 if (plt_offset
== -1)
918 gold_fallback(_("out of patch space (PLT);"
919 " relink with --incremental-full"));
921 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
922 // can be calculated from the PLT index, adjusting for the three
923 // reserved entries at the beginning of the GOT.
924 plt_index
= plt_offset
/ plt_entry_size
- 1;
925 got_offset
= (plt_index
- 1 + 3) * 8;
928 gsym
->set_plt_offset(plt_offset
);
930 // Every PLT entry needs a reloc.
931 this->add_relocation(gsym
, got_offset
);
933 // Note that we don't need to save the symbol. The contents of the
934 // PLT are independent of which symbols are used. The symbols only
935 // appear in the relocations.
938 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
942 Output_data_plt_x86_64::add_local_ifunc_entry(
943 Sized_relobj_file
<64, false>* relobj
,
944 unsigned int local_sym_index
)
946 unsigned int plt_offset
= (this->count_
+ 1) * plt_entry_size
;
949 section_offset_type got_offset
= this->got_plt_
->current_data_size();
951 // Every PLT entry needs a GOT entry which points back to the PLT
953 this->got_plt_
->set_current_data_size(got_offset
+ 8);
955 // Every PLT entry needs a reloc.
956 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
,
957 elfcpp::R_X86_64_IRELATIVE
,
958 this->got_plt_
, got_offset
, 0);
963 // Add the relocation for a PLT entry.
966 Output_data_plt_x86_64::add_relocation(Symbol
* gsym
, unsigned int got_offset
)
968 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
969 && gsym
->can_use_relative_reloc(false))
970 this->rel_
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
971 this->got_plt_
, got_offset
, 0);
974 gsym
->set_needs_dynsym_entry();
975 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
980 // Return where the TLSDESC relocations should go, creating it if
981 // necessary. These follow the JUMP_SLOT relocations.
983 Output_data_plt_x86_64::Reloc_section
*
984 Output_data_plt_x86_64::rela_tlsdesc(Layout
* layout
)
986 if (this->tlsdesc_rel_
== NULL
)
988 this->tlsdesc_rel_
= new Reloc_section(false);
989 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
990 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
991 ORDER_DYNAMIC_PLT_RELOCS
, false);
992 gold_assert(this->tlsdesc_rel_
->output_section() ==
993 this->rel_
->output_section());
995 return this->tlsdesc_rel_
;
998 // Set the final size.
1000 Output_data_plt_x86_64::set_final_data_size()
1002 unsigned int count
= this->count_
;
1003 if (this->has_tlsdesc_entry())
1005 this->set_data_size((count
+ 1) * plt_entry_size
);
1008 // The first entry in the PLT for an executable.
1010 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
1012 // From AMD64 ABI Draft 0.98, page 76
1013 0xff, 0x35, // pushq contents of memory address
1014 0, 0, 0, 0, // replaced with address of .got + 8
1015 0xff, 0x25, // jmp indirect
1016 0, 0, 0, 0, // replaced with address of .got + 16
1017 0x90, 0x90, 0x90, 0x90 // noop (x4)
1020 // Subsequent entries in the PLT for an executable.
1022 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
1024 // From AMD64 ABI Draft 0.98, page 76
1025 0xff, 0x25, // jmpq indirect
1026 0, 0, 0, 0, // replaced with address of symbol in .got
1027 0x68, // pushq immediate
1028 0, 0, 0, 0, // replaced with offset into relocation table
1029 0xe9, // jmpq relative
1030 0, 0, 0, 0 // replaced with offset to start of .plt
1033 // The reserved TLSDESC entry in the PLT for an executable.
1035 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry
[plt_entry_size
] =
1037 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1038 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1039 0xff, 0x35, // pushq x(%rip)
1040 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1041 0xff, 0x25, // jmpq *y(%rip)
1042 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1047 // Write out the PLT. This uses the hand-coded instructions above,
1048 // and adjusts them as needed. This is specified by the AMD64 ABI.
1051 Output_data_plt_x86_64::do_write(Output_file
* of
)
1053 const off_t offset
= this->offset();
1054 const section_size_type oview_size
=
1055 convert_to_section_size_type(this->data_size());
1056 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1058 const off_t got_file_offset
= this->got_plt_
->offset();
1059 const section_size_type got_size
=
1060 convert_to_section_size_type(this->got_plt_
->data_size());
1061 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1064 unsigned char* pov
= oview
;
1066 // The base address of the .plt section.
1067 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
1068 // The base address of the .got section.
1069 elfcpp::Elf_types
<64>::Elf_Addr got_base
= this->got_
->address();
1070 // The base address of the PLT portion of the .got section,
1071 // which is where the GOT pointer will point, and where the
1072 // three reserved GOT entries are located.
1073 elfcpp::Elf_types
<64>::Elf_Addr got_address
= this->got_plt_
->address();
1075 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1076 // We do a jmp relative to the PC at the end of this instruction.
1077 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1079 - (plt_address
+ 6)));
1080 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1082 - (plt_address
+ 12)));
1083 pov
+= plt_entry_size
;
1085 unsigned char* got_pov
= got_view
;
1087 memset(got_pov
, 0, 24);
1090 unsigned int plt_offset
= plt_entry_size
;
1091 unsigned int got_offset
= 24;
1092 const unsigned int count
= this->count_
;
1093 for (unsigned int plt_index
= 0;
1096 pov
+= plt_entry_size
,
1098 plt_offset
+= plt_entry_size
,
1101 // Set and adjust the PLT entry itself.
1102 memcpy(pov
, plt_entry
, plt_entry_size
);
1103 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1104 (got_address
+ got_offset
1105 - (plt_address
+ plt_offset
1108 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1109 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1110 - (plt_offset
+ plt_entry_size
));
1112 // Set the entry in the GOT.
1113 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
1116 if (this->has_tlsdesc_entry())
1118 // Set and adjust the reserved TLSDESC PLT entry.
1119 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
1120 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1121 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1123 - (plt_address
+ plt_offset
1125 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1127 + tlsdesc_got_offset
1128 - (plt_address
+ plt_offset
1130 pov
+= plt_entry_size
;
1133 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1134 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1136 of
->write_output_view(offset
, oview_size
, oview
);
1137 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1140 // Create the PLT section.
1143 Target_x86_64::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1145 if (this->plt_
== NULL
)
1147 // Create the GOT sections first.
1148 this->got_section(symtab
, layout
);
1150 this->plt_
= new Output_data_plt_x86_64(symtab
, layout
, this->got_
,
1152 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1154 | elfcpp::SHF_EXECINSTR
),
1155 this->plt_
, ORDER_PLT
, false);
1157 // Make the sh_info field of .rela.plt point to .plt.
1158 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1159 rela_plt_os
->set_info_section(this->plt_
->output_section());
1163 // Return the section for TLSDESC relocations.
1165 Target_x86_64::Reloc_section
*
1166 Target_x86_64::rela_tlsdesc_section(Layout
* layout
) const
1168 return this->plt_section()->rela_tlsdesc(layout
);
1171 // Create a PLT entry for a global symbol.
1174 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1177 if (gsym
->has_plt_offset())
1180 if (this->plt_
== NULL
)
1181 this->make_plt_section(symtab
, layout
);
1183 this->plt_
->add_entry(gsym
);
1186 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1189 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1190 Sized_relobj_file
<64, false>* relobj
,
1191 unsigned int local_sym_index
)
1193 if (relobj
->local_has_plt_offset(local_sym_index
))
1195 if (this->plt_
== NULL
)
1196 this->make_plt_section(symtab
, layout
);
1197 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(relobj
,
1199 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1202 // Return the number of entries in the PLT.
1205 Target_x86_64::plt_entry_count() const
1207 if (this->plt_
== NULL
)
1209 return this->plt_
->entry_count();
1212 // Return the offset of the first non-reserved PLT entry.
1215 Target_x86_64::first_plt_entry_offset() const
1217 return Output_data_plt_x86_64::first_plt_entry_offset();
1220 // Return the size of each PLT entry.
1223 Target_x86_64::plt_entry_size() const
1225 return Output_data_plt_x86_64::get_plt_entry_size();
1228 // Create the GOT and PLT sections for an incremental update.
1230 Output_data_got
<64, false>*
1231 Target_x86_64::init_got_plt_for_update(Symbol_table
* symtab
,
1233 unsigned int got_count
,
1234 unsigned int plt_count
)
1236 gold_assert(this->got_
== NULL
);
1238 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
1239 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1241 | elfcpp::SHF_WRITE
),
1242 this->got_
, ORDER_RELRO_LAST
,
1245 // Add the three reserved entries.
1246 this->got_plt_
= new Output_data_space((plt_count
+ 3) * 8, 8, "** GOT PLT");
1247 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1249 | elfcpp::SHF_WRITE
),
1250 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
1253 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1254 this->global_offset_table_
=
1255 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1256 Symbol_table::PREDEFINED
,
1258 0, 0, elfcpp::STT_OBJECT
,
1260 elfcpp::STV_HIDDEN
, 0,
1263 // If there are any TLSDESC relocations, they get GOT entries in
1264 // .got.plt after the jump slot entries.
1265 // FIXME: Get the count for TLSDESC entries.
1266 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
1267 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1268 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1270 ORDER_NON_RELRO_FIRST
, false);
1272 // Create the PLT section.
1273 this->plt_
= new Output_data_plt_x86_64(symtab
, layout
, this->got_
,
1274 this->got_plt_
, plt_count
);
1275 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1276 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1277 this->plt_
, ORDER_PLT
, false);
1279 // Make the sh_info field of .rela.plt point to .plt.
1280 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1281 rela_plt_os
->set_info_section(this->plt_
->output_section());
1283 // Create the rela_dyn section.
1284 this->rela_dyn_section(layout
);
1289 // Reserve a GOT entry for a local symbol, and regenerate any
1290 // necessary dynamic relocations.
1293 Target_x86_64::reserve_local_got_entry(
1294 unsigned int got_index
,
1295 Sized_relobj
<64, false>* obj
,
1297 unsigned int got_type
)
1299 unsigned int got_offset
= got_index
* 8;
1300 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1302 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
1305 case GOT_TYPE_STANDARD
:
1306 if (parameters
->options().output_is_position_independent())
1307 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
1308 this->got_
, got_offset
, 0);
1310 case GOT_TYPE_TLS_OFFSET
:
1311 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
1312 this->got_
, got_offset
, 0);
1314 case GOT_TYPE_TLS_PAIR
:
1315 this->got_
->reserve_slot(got_index
+ 1);
1316 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
1317 this->got_
, got_offset
, 0);
1319 case GOT_TYPE_TLS_DESC
:
1320 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1321 // this->got_->reserve_slot(got_index + 1);
1322 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1323 // this->got_, got_offset, 0);
1330 // Reserve a GOT entry for a global symbol, and regenerate any
1331 // necessary dynamic relocations.
1334 Target_x86_64::reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
1335 unsigned int got_type
)
1337 unsigned int got_offset
= got_index
* 8;
1338 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1340 this->got_
->reserve_global(got_index
, gsym
, got_type
);
1343 case GOT_TYPE_STANDARD
:
1344 if (!gsym
->final_value_is_known())
1346 if (gsym
->is_from_dynobj()
1347 || gsym
->is_undefined()
1348 || gsym
->is_preemptible()
1349 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1350 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
1351 this->got_
, got_offset
, 0);
1353 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1354 this->got_
, got_offset
, 0);
1357 case GOT_TYPE_TLS_OFFSET
:
1358 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
1359 this->got_
, got_offset
, 0);
1361 case GOT_TYPE_TLS_PAIR
:
1362 this->got_
->reserve_slot(got_index
+ 1);
1363 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
1364 this->got_
, got_offset
, 0);
1365 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
1366 this->got_
, got_offset
+ 8, 0);
1368 case GOT_TYPE_TLS_DESC
:
1369 this->got_
->reserve_slot(got_index
+ 1);
1370 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
1371 this->got_
, got_offset
, 0);
1378 // Register an existing PLT entry for a global symbol.
1381 Target_x86_64::register_global_plt_entry(unsigned int plt_index
,
1384 gold_assert(this->plt_
!= NULL
);
1385 gold_assert(!gsym
->has_plt_offset());
1387 this->plt_
->reserve_slot(plt_index
);
1389 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
1391 unsigned int got_offset
= (plt_index
+ 3) * 8;
1392 this->plt_
->add_relocation(gsym
, got_offset
);
1395 // Force a COPY relocation for a given symbol.
1398 Target_x86_64::emit_copy_reloc(
1399 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
1401 this->copy_relocs_
.emit_copy_reloc(symtab
,
1402 symtab
->get_sized_symbol
<64>(sym
),
1405 this->rela_dyn_section(NULL
));
1408 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1411 Target_x86_64::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
1413 if (this->tls_base_symbol_defined_
)
1416 Output_segment
* tls_segment
= layout
->tls_segment();
1417 if (tls_segment
!= NULL
)
1419 bool is_exec
= parameters
->options().output_is_executable();
1420 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1421 Symbol_table::PREDEFINED
,
1425 elfcpp::STV_HIDDEN
, 0,
1427 ? Symbol::SEGMENT_END
1428 : Symbol::SEGMENT_START
),
1431 this->tls_base_symbol_defined_
= true;
1434 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1437 Target_x86_64::reserve_tlsdesc_entries(Symbol_table
* symtab
,
1440 if (this->plt_
== NULL
)
1441 this->make_plt_section(symtab
, layout
);
1443 if (!this->plt_
->has_tlsdesc_entry())
1445 // Allocate the TLSDESC_GOT entry.
1446 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1447 unsigned int got_offset
= got
->add_constant(0);
1449 // Allocate the TLSDESC_PLT entry.
1450 this->plt_
->reserve_tlsdesc_entry(got_offset
);
1454 // Create a GOT entry for the TLS module index.
1457 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1458 Sized_relobj_file
<64, false>* object
)
1460 if (this->got_mod_index_offset_
== -1U)
1462 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1463 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1464 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1465 unsigned int got_offset
= got
->add_constant(0);
1466 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
1468 got
->add_constant(0);
1469 this->got_mod_index_offset_
= got_offset
;
1471 return this->got_mod_index_offset_
;
1474 // Optimize the TLS relocation type based on what we know about the
1475 // symbol. IS_FINAL is true if the final address of this symbol is
1476 // known at link time.
1478 tls::Tls_optimization
1479 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
1481 // If we are generating a shared library, then we can't do anything
1483 if (parameters
->options().shared())
1484 return tls::TLSOPT_NONE
;
1488 case elfcpp::R_X86_64_TLSGD
:
1489 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1490 case elfcpp::R_X86_64_TLSDESC_CALL
:
1491 // These are General-Dynamic which permits fully general TLS
1492 // access. Since we know that we are generating an executable,
1493 // we can convert this to Initial-Exec. If we also know that
1494 // this is a local symbol, we can further switch to Local-Exec.
1496 return tls::TLSOPT_TO_LE
;
1497 return tls::TLSOPT_TO_IE
;
1499 case elfcpp::R_X86_64_TLSLD
:
1500 // This is Local-Dynamic, which refers to a local symbol in the
1501 // dynamic TLS block. Since we know that we generating an
1502 // executable, we can switch to Local-Exec.
1503 return tls::TLSOPT_TO_LE
;
1505 case elfcpp::R_X86_64_DTPOFF32
:
1506 case elfcpp::R_X86_64_DTPOFF64
:
1507 // Another Local-Dynamic reloc.
1508 return tls::TLSOPT_TO_LE
;
1510 case elfcpp::R_X86_64_GOTTPOFF
:
1511 // These are Initial-Exec relocs which get the thread offset
1512 // from the GOT. If we know that we are linking against the
1513 // local symbol, we can switch to Local-Exec, which links the
1514 // thread offset into the instruction.
1516 return tls::TLSOPT_TO_LE
;
1517 return tls::TLSOPT_NONE
;
1519 case elfcpp::R_X86_64_TPOFF32
:
1520 // When we already have Local-Exec, there is nothing further we
1522 return tls::TLSOPT_NONE
;
1529 // Get the Reference_flags for a particular relocation.
1532 Target_x86_64::Scan::get_reference_flags(unsigned int r_type
)
1536 case elfcpp::R_X86_64_NONE
:
1537 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1538 case elfcpp::R_X86_64_GNU_VTENTRY
:
1539 case elfcpp::R_X86_64_GOTPC32
:
1540 case elfcpp::R_X86_64_GOTPC64
:
1541 // No symbol reference.
1544 case elfcpp::R_X86_64_64
:
1545 case elfcpp::R_X86_64_32
:
1546 case elfcpp::R_X86_64_32S
:
1547 case elfcpp::R_X86_64_16
:
1548 case elfcpp::R_X86_64_8
:
1549 return Symbol::ABSOLUTE_REF
;
1551 case elfcpp::R_X86_64_PC64
:
1552 case elfcpp::R_X86_64_PC32
:
1553 case elfcpp::R_X86_64_PC16
:
1554 case elfcpp::R_X86_64_PC8
:
1555 case elfcpp::R_X86_64_GOTOFF64
:
1556 return Symbol::RELATIVE_REF
;
1558 case elfcpp::R_X86_64_PLT32
:
1559 case elfcpp::R_X86_64_PLTOFF64
:
1560 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
1562 case elfcpp::R_X86_64_GOT64
:
1563 case elfcpp::R_X86_64_GOT32
:
1564 case elfcpp::R_X86_64_GOTPCREL64
:
1565 case elfcpp::R_X86_64_GOTPCREL
:
1566 case elfcpp::R_X86_64_GOTPLT64
:
1568 return Symbol::ABSOLUTE_REF
;
1570 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1571 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1572 case elfcpp::R_X86_64_TLSDESC_CALL
:
1573 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1574 case elfcpp::R_X86_64_DTPOFF32
:
1575 case elfcpp::R_X86_64_DTPOFF64
:
1576 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1577 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1578 return Symbol::TLS_REF
;
1580 case elfcpp::R_X86_64_COPY
:
1581 case elfcpp::R_X86_64_GLOB_DAT
:
1582 case elfcpp::R_X86_64_JUMP_SLOT
:
1583 case elfcpp::R_X86_64_RELATIVE
:
1584 case elfcpp::R_X86_64_IRELATIVE
:
1585 case elfcpp::R_X86_64_TPOFF64
:
1586 case elfcpp::R_X86_64_DTPMOD64
:
1587 case elfcpp::R_X86_64_TLSDESC
:
1588 case elfcpp::R_X86_64_SIZE32
:
1589 case elfcpp::R_X86_64_SIZE64
:
1591 // Not expected. We will give an error later.
1596 // Report an unsupported relocation against a local symbol.
1599 Target_x86_64::Scan::unsupported_reloc_local(
1600 Sized_relobj_file
<64, false>* object
,
1601 unsigned int r_type
)
1603 gold_error(_("%s: unsupported reloc %u against local symbol"),
1604 object
->name().c_str(), r_type
);
1607 // We are about to emit a dynamic relocation of type R_TYPE. If the
1608 // dynamic linker does not support it, issue an error. The GNU linker
1609 // only issues a non-PIC error for an allocated read-only section.
1610 // Here we know the section is allocated, but we don't know that it is
1611 // read-only. But we check for all the relocation types which the
1612 // glibc dynamic linker supports, so it seems appropriate to issue an
1613 // error even if the section is not read-only. If GSYM is not NULL,
1614 // it is the symbol the relocation is against; if it is NULL, the
1615 // relocation is against a local symbol.
1618 Target_x86_64::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
1623 // These are the relocation types supported by glibc for x86_64
1624 // which should always work.
1625 case elfcpp::R_X86_64_RELATIVE
:
1626 case elfcpp::R_X86_64_IRELATIVE
:
1627 case elfcpp::R_X86_64_GLOB_DAT
:
1628 case elfcpp::R_X86_64_JUMP_SLOT
:
1629 case elfcpp::R_X86_64_DTPMOD64
:
1630 case elfcpp::R_X86_64_DTPOFF64
:
1631 case elfcpp::R_X86_64_TPOFF64
:
1632 case elfcpp::R_X86_64_64
:
1633 case elfcpp::R_X86_64_COPY
:
1636 // glibc supports these reloc types, but they can overflow.
1637 case elfcpp::R_X86_64_PC32
:
1638 // A PC relative reference is OK against a local symbol or if
1639 // the symbol is defined locally.
1641 || (!gsym
->is_from_dynobj()
1642 && !gsym
->is_undefined()
1643 && !gsym
->is_preemptible()))
1646 case elfcpp::R_X86_64_32
:
1647 if (this->issued_non_pic_error_
)
1649 gold_assert(parameters
->options().output_is_position_independent());
1651 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
1652 "overflow at runtime; recompile with -fPIC"));
1654 object
->error(_("requires dynamic %s reloc against '%s' which may "
1655 "overflow at runtime; recompile with -fPIC"),
1656 (r_type
== elfcpp::R_X86_64_32
1660 this->issued_non_pic_error_
= true;
1664 // This prevents us from issuing more than one error per reloc
1665 // section. But we can still wind up issuing more than one
1666 // error per object file.
1667 if (this->issued_non_pic_error_
)
1669 gold_assert(parameters
->options().output_is_position_independent());
1670 object
->error(_("requires unsupported dynamic reloc %u; "
1671 "recompile with -fPIC"),
1673 this->issued_non_pic_error_
= true;
1676 case elfcpp::R_X86_64_NONE
:
1681 // Return whether we need to make a PLT entry for a relocation of the
1682 // given type against a STT_GNU_IFUNC symbol.
1685 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1686 Sized_relobj_file
<64, false>* object
,
1687 unsigned int r_type
)
1689 int flags
= Scan::get_reference_flags(r_type
);
1690 if (flags
& Symbol::TLS_REF
)
1691 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1692 object
->name().c_str(), r_type
);
1696 // Scan a relocation for a local symbol.
1699 Target_x86_64::Scan::local(Symbol_table
* symtab
,
1701 Target_x86_64
* target
,
1702 Sized_relobj_file
<64, false>* object
,
1703 unsigned int data_shndx
,
1704 Output_section
* output_section
,
1705 const elfcpp::Rela
<64, false>& reloc
,
1706 unsigned int r_type
,
1707 const elfcpp::Sym
<64, false>& lsym
)
1709 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1710 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
1711 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1713 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1714 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
1719 case elfcpp::R_X86_64_NONE
:
1720 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1721 case elfcpp::R_X86_64_GNU_VTENTRY
:
1724 case elfcpp::R_X86_64_64
:
1725 // If building a shared library (or a position-independent
1726 // executable), we need to create a dynamic relocation for this
1727 // location. The relocation applied at link time will apply the
1728 // link-time value, so we flag the location with an
1729 // R_X86_64_RELATIVE relocation so the dynamic loader can
1730 // relocate it easily.
1731 if (parameters
->options().output_is_position_independent())
1733 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1734 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1735 rela_dyn
->add_local_relative(object
, r_sym
,
1736 elfcpp::R_X86_64_RELATIVE
,
1737 output_section
, data_shndx
,
1738 reloc
.get_r_offset(),
1739 reloc
.get_r_addend());
1743 case elfcpp::R_X86_64_32
:
1744 case elfcpp::R_X86_64_32S
:
1745 case elfcpp::R_X86_64_16
:
1746 case elfcpp::R_X86_64_8
:
1747 // If building a shared library (or a position-independent
1748 // executable), we need to create a dynamic relocation for this
1749 // location. We can't use an R_X86_64_RELATIVE relocation
1750 // because that is always a 64-bit relocation.
1751 if (parameters
->options().output_is_position_independent())
1753 this->check_non_pic(object
, r_type
, NULL
);
1755 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1756 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1757 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1758 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1759 data_shndx
, reloc
.get_r_offset(),
1760 reloc
.get_r_addend());
1763 gold_assert(lsym
.get_st_value() == 0);
1764 unsigned int shndx
= lsym
.get_st_shndx();
1766 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1769 object
->error(_("section symbol %u has bad shndx %u"),
1772 rela_dyn
->add_local_section(object
, shndx
,
1773 r_type
, output_section
,
1774 data_shndx
, reloc
.get_r_offset(),
1775 reloc
.get_r_addend());
1780 case elfcpp::R_X86_64_PC64
:
1781 case elfcpp::R_X86_64_PC32
:
1782 case elfcpp::R_X86_64_PC16
:
1783 case elfcpp::R_X86_64_PC8
:
1786 case elfcpp::R_X86_64_PLT32
:
1787 // Since we know this is a local symbol, we can handle this as a
1791 case elfcpp::R_X86_64_GOTPC32
:
1792 case elfcpp::R_X86_64_GOTOFF64
:
1793 case elfcpp::R_X86_64_GOTPC64
:
1794 case elfcpp::R_X86_64_PLTOFF64
:
1795 // We need a GOT section.
1796 target
->got_section(symtab
, layout
);
1797 // For PLTOFF64, we'd normally want a PLT section, but since we
1798 // know this is a local symbol, no PLT is needed.
1801 case elfcpp::R_X86_64_GOT64
:
1802 case elfcpp::R_X86_64_GOT32
:
1803 case elfcpp::R_X86_64_GOTPCREL64
:
1804 case elfcpp::R_X86_64_GOTPCREL
:
1805 case elfcpp::R_X86_64_GOTPLT64
:
1807 // The symbol requires a GOT entry.
1808 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1809 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1811 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1812 // lets function pointers compare correctly with shared
1813 // libraries. Otherwise we would need an IRELATIVE reloc.
1815 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
)
1816 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
1818 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1821 // If we are generating a shared object, we need to add a
1822 // dynamic relocation for this symbol's GOT entry.
1823 if (parameters
->options().output_is_position_independent())
1825 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1826 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1827 if (r_type
!= elfcpp::R_X86_64_GOT32
)
1829 unsigned int got_offset
=
1830 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1831 rela_dyn
->add_local_relative(object
, r_sym
,
1832 elfcpp::R_X86_64_RELATIVE
,
1833 got
, got_offset
, 0);
1837 this->check_non_pic(object
, r_type
, NULL
);
1839 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1840 rela_dyn
->add_local(
1841 object
, r_sym
, r_type
, got
,
1842 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1846 // For GOTPLT64, we'd normally want a PLT section, but since
1847 // we know this is a local symbol, no PLT is needed.
1851 case elfcpp::R_X86_64_COPY
:
1852 case elfcpp::R_X86_64_GLOB_DAT
:
1853 case elfcpp::R_X86_64_JUMP_SLOT
:
1854 case elfcpp::R_X86_64_RELATIVE
:
1855 case elfcpp::R_X86_64_IRELATIVE
:
1856 // These are outstanding tls relocs, which are unexpected when linking
1857 case elfcpp::R_X86_64_TPOFF64
:
1858 case elfcpp::R_X86_64_DTPMOD64
:
1859 case elfcpp::R_X86_64_TLSDESC
:
1860 gold_error(_("%s: unexpected reloc %u in object file"),
1861 object
->name().c_str(), r_type
);
1864 // These are initial tls relocs, which are expected when linking
1865 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1866 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1867 case elfcpp::R_X86_64_TLSDESC_CALL
:
1868 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1869 case elfcpp::R_X86_64_DTPOFF32
:
1870 case elfcpp::R_X86_64_DTPOFF64
:
1871 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1872 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1874 bool output_is_shared
= parameters
->options().shared();
1875 const tls::Tls_optimization optimized_type
1876 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
1879 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1880 if (optimized_type
== tls::TLSOPT_NONE
)
1882 // Create a pair of GOT entries for the module index and
1883 // dtv-relative offset.
1884 Output_data_got
<64, false>* got
1885 = target
->got_section(symtab
, layout
);
1886 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1887 unsigned int shndx
= lsym
.get_st_shndx();
1889 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1891 object
->error(_("local symbol %u has bad shndx %u"),
1894 got
->add_local_pair_with_rela(object
, r_sym
,
1897 target
->rela_dyn_section(layout
),
1898 elfcpp::R_X86_64_DTPMOD64
, 0);
1900 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1901 unsupported_reloc_local(object
, r_type
);
1904 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1905 target
->define_tls_base_symbol(symtab
, layout
);
1906 if (optimized_type
== tls::TLSOPT_NONE
)
1908 // Create reserved PLT and GOT entries for the resolver.
1909 target
->reserve_tlsdesc_entries(symtab
, layout
);
1911 // Generate a double GOT entry with an
1912 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1913 // is resolved lazily, so the GOT entry needs to be in
1914 // an area in .got.plt, not .got. Call got_section to
1915 // make sure the section has been created.
1916 target
->got_section(symtab
, layout
);
1917 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
1918 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1919 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
1921 unsigned int got_offset
= got
->add_constant(0);
1922 got
->add_constant(0);
1923 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
1925 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
1926 // We store the arguments we need in a vector, and
1927 // use the index into the vector as the parameter
1928 // to pass to the target specific routines.
1929 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
1930 void* arg
= reinterpret_cast<void*>(intarg
);
1931 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
1932 got
, got_offset
, 0);
1935 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1936 unsupported_reloc_local(object
, r_type
);
1939 case elfcpp::R_X86_64_TLSDESC_CALL
:
1942 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1943 if (optimized_type
== tls::TLSOPT_NONE
)
1945 // Create a GOT entry for the module index.
1946 target
->got_mod_index_entry(symtab
, layout
, object
);
1948 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1949 unsupported_reloc_local(object
, r_type
);
1952 case elfcpp::R_X86_64_DTPOFF32
:
1953 case elfcpp::R_X86_64_DTPOFF64
:
1956 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1957 layout
->set_has_static_tls();
1958 if (optimized_type
== tls::TLSOPT_NONE
)
1960 // Create a GOT entry for the tp-relative offset.
1961 Output_data_got
<64, false>* got
1962 = target
->got_section(symtab
, layout
);
1963 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1964 got
->add_local_with_rela(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1965 target
->rela_dyn_section(layout
),
1966 elfcpp::R_X86_64_TPOFF64
);
1968 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1969 unsupported_reloc_local(object
, r_type
);
1972 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1973 layout
->set_has_static_tls();
1974 if (output_is_shared
)
1975 unsupported_reloc_local(object
, r_type
);
1984 case elfcpp::R_X86_64_SIZE32
:
1985 case elfcpp::R_X86_64_SIZE64
:
1987 gold_error(_("%s: unsupported reloc %u against local symbol"),
1988 object
->name().c_str(), r_type
);
1994 // Report an unsupported relocation against a global symbol.
1997 Target_x86_64::Scan::unsupported_reloc_global(
1998 Sized_relobj_file
<64, false>* object
,
1999 unsigned int r_type
,
2002 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2003 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
2006 // Returns true if this relocation type could be that of a function pointer.
2008 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type
)
2012 case elfcpp::R_X86_64_64
:
2013 case elfcpp::R_X86_64_32
:
2014 case elfcpp::R_X86_64_32S
:
2015 case elfcpp::R_X86_64_16
:
2016 case elfcpp::R_X86_64_8
:
2017 case elfcpp::R_X86_64_GOT64
:
2018 case elfcpp::R_X86_64_GOT32
:
2019 case elfcpp::R_X86_64_GOTPCREL64
:
2020 case elfcpp::R_X86_64_GOTPCREL
:
2021 case elfcpp::R_X86_64_GOTPLT64
:
2029 // For safe ICF, scan a relocation for a local symbol to check if it
2030 // corresponds to a function pointer being taken. In that case mark
2031 // the function whose pointer was taken as not foldable.
2034 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
2038 Sized_relobj_file
<64, false>* ,
2041 const elfcpp::Rela
<64, false>& ,
2042 unsigned int r_type
,
2043 const elfcpp::Sym
<64, false>&)
2045 // When building a shared library, do not fold any local symbols as it is
2046 // not possible to distinguish pointer taken versus a call by looking at
2047 // the relocation types.
2048 return (parameters
->options().shared()
2049 || possible_function_pointer_reloc(r_type
));
2052 // For safe ICF, scan a relocation for a global symbol to check if it
2053 // corresponds to a function pointer being taken. In that case mark
2054 // the function whose pointer was taken as not foldable.
2057 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2061 Sized_relobj_file
<64, false>* ,
2064 const elfcpp::Rela
<64, false>& ,
2065 unsigned int r_type
,
2068 // When building a shared library, do not fold symbols whose visibility
2069 // is hidden, internal or protected.
2070 return ((parameters
->options().shared()
2071 && (gsym
->visibility() == elfcpp::STV_INTERNAL
2072 || gsym
->visibility() == elfcpp::STV_PROTECTED
2073 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
2074 || possible_function_pointer_reloc(r_type
));
2077 // Scan a relocation for a global symbol.
2080 Target_x86_64::Scan::global(Symbol_table
* symtab
,
2082 Target_x86_64
* target
,
2083 Sized_relobj_file
<64, false>* object
,
2084 unsigned int data_shndx
,
2085 Output_section
* output_section
,
2086 const elfcpp::Rela
<64, false>& reloc
,
2087 unsigned int r_type
,
2090 // A STT_GNU_IFUNC symbol may require a PLT entry.
2091 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2092 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
2093 target
->make_plt_entry(symtab
, layout
, gsym
);
2097 case elfcpp::R_X86_64_NONE
:
2098 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2099 case elfcpp::R_X86_64_GNU_VTENTRY
:
2102 case elfcpp::R_X86_64_64
:
2103 case elfcpp::R_X86_64_32
:
2104 case elfcpp::R_X86_64_32S
:
2105 case elfcpp::R_X86_64_16
:
2106 case elfcpp::R_X86_64_8
:
2108 // Make a PLT entry if necessary.
2109 if (gsym
->needs_plt_entry())
2111 target
->make_plt_entry(symtab
, layout
, gsym
);
2112 // Since this is not a PC-relative relocation, we may be
2113 // taking the address of a function. In that case we need to
2114 // set the entry in the dynamic symbol table to the address of
2116 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
2117 gsym
->set_needs_dynsym_value();
2119 // Make a dynamic relocation if necessary.
2120 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2122 if (gsym
->may_need_copy_reloc())
2124 target
->copy_reloc(symtab
, layout
, object
,
2125 data_shndx
, output_section
, gsym
, reloc
);
2127 else if (r_type
== elfcpp::R_X86_64_64
2128 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2129 && gsym
->can_use_relative_reloc(false)
2130 && !gsym
->is_from_dynobj()
2131 && !gsym
->is_undefined()
2132 && !gsym
->is_preemptible())
2134 // Use an IRELATIVE reloc for a locally defined
2135 // STT_GNU_IFUNC symbol. This makes a function
2136 // address in a PIE executable match the address in a
2137 // shared library that it links against.
2138 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2139 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
2140 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
2141 output_section
, object
,
2143 reloc
.get_r_offset(),
2144 reloc
.get_r_addend());
2146 else if (r_type
== elfcpp::R_X86_64_64
2147 && gsym
->can_use_relative_reloc(false))
2149 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2150 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2151 output_section
, object
,
2153 reloc
.get_r_offset(),
2154 reloc
.get_r_addend());
2158 this->check_non_pic(object
, r_type
, gsym
);
2159 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2160 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2161 data_shndx
, reloc
.get_r_offset(),
2162 reloc
.get_r_addend());
2168 case elfcpp::R_X86_64_PC64
:
2169 case elfcpp::R_X86_64_PC32
:
2170 case elfcpp::R_X86_64_PC16
:
2171 case elfcpp::R_X86_64_PC8
:
2173 // Make a PLT entry if necessary.
2174 if (gsym
->needs_plt_entry())
2175 target
->make_plt_entry(symtab
, layout
, gsym
);
2176 // Make a dynamic relocation if necessary.
2177 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2179 if (gsym
->may_need_copy_reloc())
2181 target
->copy_reloc(symtab
, layout
, object
,
2182 data_shndx
, output_section
, gsym
, reloc
);
2186 this->check_non_pic(object
, r_type
, gsym
);
2187 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2188 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2189 data_shndx
, reloc
.get_r_offset(),
2190 reloc
.get_r_addend());
2196 case elfcpp::R_X86_64_GOT64
:
2197 case elfcpp::R_X86_64_GOT32
:
2198 case elfcpp::R_X86_64_GOTPCREL64
:
2199 case elfcpp::R_X86_64_GOTPCREL
:
2200 case elfcpp::R_X86_64_GOTPLT64
:
2202 // The symbol requires a GOT entry.
2203 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2204 if (gsym
->final_value_is_known())
2206 // For a STT_GNU_IFUNC symbol we want the PLT address.
2207 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
2208 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2210 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2214 // If this symbol is not fully resolved, we need to add a
2215 // dynamic relocation for it.
2216 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2217 if (gsym
->is_from_dynobj()
2218 || gsym
->is_undefined()
2219 || gsym
->is_preemptible()
2220 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
2221 && parameters
->options().output_is_position_independent()))
2222 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2223 elfcpp::R_X86_64_GLOB_DAT
);
2226 // For a STT_GNU_IFUNC symbol we want to write the PLT
2227 // offset into the GOT, so that function pointer
2228 // comparisons work correctly.
2230 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
2231 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2234 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2235 // Tell the dynamic linker to use the PLT address
2236 // when resolving relocations.
2237 if (gsym
->is_from_dynobj()
2238 && !parameters
->options().shared())
2239 gsym
->set_needs_dynsym_value();
2243 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2244 rela_dyn
->add_global_relative(gsym
,
2245 elfcpp::R_X86_64_RELATIVE
,
2250 // For GOTPLT64, we also need a PLT entry (but only if the
2251 // symbol is not fully resolved).
2252 if (r_type
== elfcpp::R_X86_64_GOTPLT64
2253 && !gsym
->final_value_is_known())
2254 target
->make_plt_entry(symtab
, layout
, gsym
);
2258 case elfcpp::R_X86_64_PLT32
:
2259 // If the symbol is fully resolved, this is just a PC32 reloc.
2260 // Otherwise we need a PLT entry.
2261 if (gsym
->final_value_is_known())
2263 // If building a shared library, we can also skip the PLT entry
2264 // if the symbol is defined in the output file and is protected
2266 if (gsym
->is_defined()
2267 && !gsym
->is_from_dynobj()
2268 && !gsym
->is_preemptible())
2270 target
->make_plt_entry(symtab
, layout
, gsym
);
2273 case elfcpp::R_X86_64_GOTPC32
:
2274 case elfcpp::R_X86_64_GOTOFF64
:
2275 case elfcpp::R_X86_64_GOTPC64
:
2276 case elfcpp::R_X86_64_PLTOFF64
:
2277 // We need a GOT section.
2278 target
->got_section(symtab
, layout
);
2279 // For PLTOFF64, we also need a PLT entry (but only if the
2280 // symbol is not fully resolved).
2281 if (r_type
== elfcpp::R_X86_64_PLTOFF64
2282 && !gsym
->final_value_is_known())
2283 target
->make_plt_entry(symtab
, layout
, gsym
);
2286 case elfcpp::R_X86_64_COPY
:
2287 case elfcpp::R_X86_64_GLOB_DAT
:
2288 case elfcpp::R_X86_64_JUMP_SLOT
:
2289 case elfcpp::R_X86_64_RELATIVE
:
2290 case elfcpp::R_X86_64_IRELATIVE
:
2291 // These are outstanding tls relocs, which are unexpected when linking
2292 case elfcpp::R_X86_64_TPOFF64
:
2293 case elfcpp::R_X86_64_DTPMOD64
:
2294 case elfcpp::R_X86_64_TLSDESC
:
2295 gold_error(_("%s: unexpected reloc %u in object file"),
2296 object
->name().c_str(), r_type
);
2299 // These are initial tls relocs, which are expected for global()
2300 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2301 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2302 case elfcpp::R_X86_64_TLSDESC_CALL
:
2303 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2304 case elfcpp::R_X86_64_DTPOFF32
:
2305 case elfcpp::R_X86_64_DTPOFF64
:
2306 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2307 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2309 const bool is_final
= gsym
->final_value_is_known();
2310 const tls::Tls_optimization optimized_type
2311 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
2314 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2315 if (optimized_type
== tls::TLSOPT_NONE
)
2317 // Create a pair of GOT entries for the module index and
2318 // dtv-relative offset.
2319 Output_data_got
<64, false>* got
2320 = target
->got_section(symtab
, layout
);
2321 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_PAIR
,
2322 target
->rela_dyn_section(layout
),
2323 elfcpp::R_X86_64_DTPMOD64
,
2324 elfcpp::R_X86_64_DTPOFF64
);
2326 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2328 // Create a GOT entry for the tp-relative offset.
2329 Output_data_got
<64, false>* got
2330 = target
->got_section(symtab
, layout
);
2331 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
2332 target
->rela_dyn_section(layout
),
2333 elfcpp::R_X86_64_TPOFF64
);
2335 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2336 unsupported_reloc_global(object
, r_type
, gsym
);
2339 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2340 target
->define_tls_base_symbol(symtab
, layout
);
2341 if (optimized_type
== tls::TLSOPT_NONE
)
2343 // Create reserved PLT and GOT entries for the resolver.
2344 target
->reserve_tlsdesc_entries(symtab
, layout
);
2346 // Create a double GOT entry with an R_X86_64_TLSDESC
2347 // reloc. The R_X86_64_TLSDESC reloc is resolved
2348 // lazily, so the GOT entry needs to be in an area in
2349 // .got.plt, not .got. Call got_section to make sure
2350 // the section has been created.
2351 target
->got_section(symtab
, layout
);
2352 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2353 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2354 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_DESC
, rt
,
2355 elfcpp::R_X86_64_TLSDESC
, 0);
2357 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2359 // Create a GOT entry for the tp-relative offset.
2360 Output_data_got
<64, false>* got
2361 = target
->got_section(symtab
, layout
);
2362 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
2363 target
->rela_dyn_section(layout
),
2364 elfcpp::R_X86_64_TPOFF64
);
2366 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2367 unsupported_reloc_global(object
, r_type
, gsym
);
2370 case elfcpp::R_X86_64_TLSDESC_CALL
:
2373 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2374 if (optimized_type
== tls::TLSOPT_NONE
)
2376 // Create a GOT entry for the module index.
2377 target
->got_mod_index_entry(symtab
, layout
, object
);
2379 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2380 unsupported_reloc_global(object
, r_type
, gsym
);
2383 case elfcpp::R_X86_64_DTPOFF32
:
2384 case elfcpp::R_X86_64_DTPOFF64
:
2387 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2388 layout
->set_has_static_tls();
2389 if (optimized_type
== tls::TLSOPT_NONE
)
2391 // Create a GOT entry for the tp-relative offset.
2392 Output_data_got
<64, false>* got
2393 = target
->got_section(symtab
, layout
);
2394 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
2395 target
->rela_dyn_section(layout
),
2396 elfcpp::R_X86_64_TPOFF64
);
2398 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2399 unsupported_reloc_global(object
, r_type
, gsym
);
2402 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2403 layout
->set_has_static_tls();
2404 if (parameters
->options().shared())
2405 unsupported_reloc_local(object
, r_type
);
2414 case elfcpp::R_X86_64_SIZE32
:
2415 case elfcpp::R_X86_64_SIZE64
:
2417 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2418 object
->name().c_str(), r_type
,
2419 gsym
->demangled_name().c_str());
2425 Target_x86_64::gc_process_relocs(Symbol_table
* symtab
,
2427 Sized_relobj_file
<64, false>* object
,
2428 unsigned int data_shndx
,
2429 unsigned int sh_type
,
2430 const unsigned char* prelocs
,
2432 Output_section
* output_section
,
2433 bool needs_special_offset_handling
,
2434 size_t local_symbol_count
,
2435 const unsigned char* plocal_symbols
)
2438 if (sh_type
== elfcpp::SHT_REL
)
2443 gold::gc_process_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2444 Target_x86_64::Scan
,
2445 Target_x86_64::Relocatable_size_for_reloc
>(
2454 needs_special_offset_handling
,
2459 // Scan relocations for a section.
2462 Target_x86_64::scan_relocs(Symbol_table
* symtab
,
2464 Sized_relobj_file
<64, false>* object
,
2465 unsigned int data_shndx
,
2466 unsigned int sh_type
,
2467 const unsigned char* prelocs
,
2469 Output_section
* output_section
,
2470 bool needs_special_offset_handling
,
2471 size_t local_symbol_count
,
2472 const unsigned char* plocal_symbols
)
2474 if (sh_type
== elfcpp::SHT_REL
)
2476 gold_error(_("%s: unsupported REL reloc section"),
2477 object
->name().c_str());
2481 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2482 Target_x86_64::Scan
>(
2491 needs_special_offset_handling
,
2496 // Finalize the sections.
2499 Target_x86_64::do_finalize_sections(
2501 const Input_objects
*,
2502 Symbol_table
* symtab
)
2504 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2506 : this->plt_
->rela_plt());
2507 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
2508 this->rela_dyn_
, true, false);
2510 // Fill in some more dynamic tags.
2511 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
2514 if (this->plt_
!= NULL
2515 && this->plt_
->output_section() != NULL
2516 && this->plt_
->has_tlsdesc_entry())
2518 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
2519 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
2520 this->got_
->finalize_data_size();
2521 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
2522 this->plt_
, plt_offset
);
2523 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
2524 this->got_
, got_offset
);
2528 // Emit any relocs we saved in an attempt to avoid generating COPY
2530 if (this->copy_relocs_
.any_saved_relocs())
2531 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
2533 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2534 // the .got.plt section.
2535 Symbol
* sym
= this->global_offset_table_
;
2538 uint64_t data_size
= this->got_plt_
->current_data_size();
2539 symtab
->get_sized_symbol
<64>(sym
)->set_symsize(data_size
);
2543 // Perform a relocation.
2546 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
2547 Target_x86_64
* target
,
2550 const elfcpp::Rela
<64, false>& rela
,
2551 unsigned int r_type
,
2552 const Sized_symbol
<64>* gsym
,
2553 const Symbol_value
<64>* psymval
,
2554 unsigned char* view
,
2555 elfcpp::Elf_types
<64>::Elf_Addr address
,
2556 section_size_type view_size
)
2558 if (this->skip_call_tls_get_addr_
)
2560 if ((r_type
!= elfcpp::R_X86_64_PLT32
2561 && r_type
!= elfcpp::R_X86_64_PC32
)
2563 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
2565 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2566 _("missing expected TLS relocation"));
2570 this->skip_call_tls_get_addr_
= false;
2575 const Sized_relobj_file
<64, false>* object
= relinfo
->object
;
2577 // Pick the value to use for symbols defined in the PLT.
2578 Symbol_value
<64> symval
;
2580 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
2582 symval
.set_output_value(target
->plt_section()->address()
2583 + gsym
->plt_offset());
2586 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
2588 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2589 if (object
->local_has_plt_offset(r_sym
))
2591 symval
.set_output_value(target
->plt_section()->address()
2592 + object
->local_plt_offset(r_sym
));
2597 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2599 // Get the GOT offset if needed.
2600 // The GOT pointer points to the end of the GOT section.
2601 // We need to subtract the size of the GOT section to get
2602 // the actual offset to use in the relocation.
2603 bool have_got_offset
= false;
2604 unsigned int got_offset
= 0;
2607 case elfcpp::R_X86_64_GOT32
:
2608 case elfcpp::R_X86_64_GOT64
:
2609 case elfcpp::R_X86_64_GOTPLT64
:
2610 case elfcpp::R_X86_64_GOTPCREL
:
2611 case elfcpp::R_X86_64_GOTPCREL64
:
2614 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2615 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
2619 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2620 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2621 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
2622 - target
->got_size());
2624 have_got_offset
= true;
2633 case elfcpp::R_X86_64_NONE
:
2634 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2635 case elfcpp::R_X86_64_GNU_VTENTRY
:
2638 case elfcpp::R_X86_64_64
:
2639 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
2642 case elfcpp::R_X86_64_PC64
:
2643 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
2647 case elfcpp::R_X86_64_32
:
2648 // FIXME: we need to verify that value + addend fits into 32 bits:
2649 // uint64_t x = value + addend;
2650 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2651 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2652 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
2655 case elfcpp::R_X86_64_32S
:
2656 // FIXME: we need to verify that value + addend fits into 32 bits:
2657 // int64_t x = value + addend; // note this quantity is signed!
2658 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2659 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
2662 case elfcpp::R_X86_64_PC32
:
2663 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
2667 case elfcpp::R_X86_64_16
:
2668 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
2671 case elfcpp::R_X86_64_PC16
:
2672 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
2676 case elfcpp::R_X86_64_8
:
2677 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
2680 case elfcpp::R_X86_64_PC8
:
2681 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
2685 case elfcpp::R_X86_64_PLT32
:
2686 gold_assert(gsym
== NULL
2687 || gsym
->has_plt_offset()
2688 || gsym
->final_value_is_known()
2689 || (gsym
->is_defined()
2690 && !gsym
->is_from_dynobj()
2691 && !gsym
->is_preemptible()));
2692 // Note: while this code looks the same as for R_X86_64_PC32, it
2693 // behaves differently because psymval was set to point to
2694 // the PLT entry, rather than the symbol, in Scan::global().
2695 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
2699 case elfcpp::R_X86_64_PLTOFF64
:
2702 gold_assert(gsym
->has_plt_offset()
2703 || gsym
->final_value_is_known());
2704 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
2705 got_address
= target
->got_section(NULL
, NULL
)->address();
2706 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
2707 addend
- got_address
);
2710 case elfcpp::R_X86_64_GOT32
:
2711 gold_assert(have_got_offset
);
2712 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
2715 case elfcpp::R_X86_64_GOTPC32
:
2718 elfcpp::Elf_types
<64>::Elf_Addr value
;
2719 value
= target
->got_plt_section()->address();
2720 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2724 case elfcpp::R_X86_64_GOT64
:
2725 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2726 // Since we always add a PLT entry, this is equivalent.
2727 case elfcpp::R_X86_64_GOTPLT64
:
2728 gold_assert(have_got_offset
);
2729 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
2732 case elfcpp::R_X86_64_GOTPC64
:
2735 elfcpp::Elf_types
<64>::Elf_Addr value
;
2736 value
= target
->got_plt_section()->address();
2737 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
2741 case elfcpp::R_X86_64_GOTOFF64
:
2743 elfcpp::Elf_types
<64>::Elf_Addr value
;
2744 value
= (psymval
->value(object
, 0)
2745 - target
->got_plt_section()->address());
2746 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
2750 case elfcpp::R_X86_64_GOTPCREL
:
2752 gold_assert(have_got_offset
);
2753 elfcpp::Elf_types
<64>::Elf_Addr value
;
2754 value
= target
->got_plt_section()->address() + got_offset
;
2755 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2759 case elfcpp::R_X86_64_GOTPCREL64
:
2761 gold_assert(have_got_offset
);
2762 elfcpp::Elf_types
<64>::Elf_Addr value
;
2763 value
= target
->got_plt_section()->address() + got_offset
;
2764 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
2768 case elfcpp::R_X86_64_COPY
:
2769 case elfcpp::R_X86_64_GLOB_DAT
:
2770 case elfcpp::R_X86_64_JUMP_SLOT
:
2771 case elfcpp::R_X86_64_RELATIVE
:
2772 case elfcpp::R_X86_64_IRELATIVE
:
2773 // These are outstanding tls relocs, which are unexpected when linking
2774 case elfcpp::R_X86_64_TPOFF64
:
2775 case elfcpp::R_X86_64_DTPMOD64
:
2776 case elfcpp::R_X86_64_TLSDESC
:
2777 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2778 _("unexpected reloc %u in object file"),
2782 // These are initial tls relocs, which are expected when linking
2783 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2784 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2785 case elfcpp::R_X86_64_TLSDESC_CALL
:
2786 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2787 case elfcpp::R_X86_64_DTPOFF32
:
2788 case elfcpp::R_X86_64_DTPOFF64
:
2789 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2790 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2791 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
2792 view
, address
, view_size
);
2795 case elfcpp::R_X86_64_SIZE32
:
2796 case elfcpp::R_X86_64_SIZE64
:
2798 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2799 _("unsupported reloc %u"),
2807 // Perform a TLS relocation.
2810 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
2811 Target_x86_64
* target
,
2813 const elfcpp::Rela
<64, false>& rela
,
2814 unsigned int r_type
,
2815 const Sized_symbol
<64>* gsym
,
2816 const Symbol_value
<64>* psymval
,
2817 unsigned char* view
,
2818 elfcpp::Elf_types
<64>::Elf_Addr address
,
2819 section_size_type view_size
)
2821 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2823 const Sized_relobj_file
<64, false>* object
= relinfo
->object
;
2824 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2825 elfcpp::Shdr
<64, false> data_shdr(relinfo
->data_shdr
);
2826 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
2828 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
2830 const bool is_final
= (gsym
== NULL
2831 ? !parameters
->options().shared()
2832 : gsym
->final_value_is_known());
2833 tls::Tls_optimization optimized_type
2834 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
2837 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2838 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2840 // If this code sequence is used in a non-executable section,
2841 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2842 // on the assumption that it's being used by itself in a debug
2843 // section. Therefore, in the unlikely event that the code
2844 // sequence appears in a non-executable section, we simply
2845 // leave it unoptimized.
2846 optimized_type
= tls::TLSOPT_NONE
;
2848 if (optimized_type
== tls::TLSOPT_TO_LE
)
2850 gold_assert(tls_segment
!= NULL
);
2851 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
2852 rela
, r_type
, value
, view
,
2858 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2859 ? GOT_TYPE_TLS_OFFSET
2860 : GOT_TYPE_TLS_PAIR
);
2861 unsigned int got_offset
;
2864 gold_assert(gsym
->has_got_offset(got_type
));
2865 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2869 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2870 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2871 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2872 - target
->got_size());
2874 if (optimized_type
== tls::TLSOPT_TO_IE
)
2876 gold_assert(tls_segment
!= NULL
);
2877 value
= target
->got_plt_section()->address() + got_offset
;
2878 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2879 value
, view
, address
, view_size
);
2882 else if (optimized_type
== tls::TLSOPT_NONE
)
2884 // Relocate the field with the offset of the pair of GOT
2886 value
= target
->got_plt_section()->address() + got_offset
;
2887 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2892 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2893 _("unsupported reloc %u"), r_type
);
2896 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2897 case elfcpp::R_X86_64_TLSDESC_CALL
:
2898 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2900 // See above comment for R_X86_64_TLSGD.
2901 optimized_type
= tls::TLSOPT_NONE
;
2903 if (optimized_type
== tls::TLSOPT_TO_LE
)
2905 gold_assert(tls_segment
!= NULL
);
2906 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2907 rela
, r_type
, value
, view
,
2913 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2914 ? GOT_TYPE_TLS_OFFSET
2915 : GOT_TYPE_TLS_DESC
);
2916 unsigned int got_offset
= 0;
2917 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
2918 && optimized_type
== tls::TLSOPT_NONE
)
2920 // We created GOT entries in the .got.tlsdesc portion of
2921 // the .got.plt section, but the offset stored in the
2922 // symbol is the offset within .got.tlsdesc.
2923 got_offset
= (target
->got_size()
2924 + target
->got_plt_section()->data_size());
2928 gold_assert(gsym
->has_got_offset(got_type
));
2929 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
2933 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2934 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2935 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
2936 - target
->got_size());
2938 if (optimized_type
== tls::TLSOPT_TO_IE
)
2940 gold_assert(tls_segment
!= NULL
);
2941 value
= target
->got_plt_section()->address() + got_offset
;
2942 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
2943 rela
, r_type
, value
, view
, address
,
2947 else if (optimized_type
== tls::TLSOPT_NONE
)
2949 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2951 // Relocate the field with the offset of the pair of GOT
2953 value
= target
->got_plt_section()->address() + got_offset
;
2954 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2960 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2961 _("unsupported reloc %u"), r_type
);
2964 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2965 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2967 // See above comment for R_X86_64_TLSGD.
2968 optimized_type
= tls::TLSOPT_NONE
;
2970 if (optimized_type
== tls::TLSOPT_TO_LE
)
2972 gold_assert(tls_segment
!= NULL
);
2973 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2974 value
, view
, view_size
);
2977 else if (optimized_type
== tls::TLSOPT_NONE
)
2979 // Relocate the field with the offset of the GOT entry for
2980 // the module index.
2981 unsigned int got_offset
;
2982 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2983 - target
->got_size());
2984 value
= target
->got_plt_section()->address() + got_offset
;
2985 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2989 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2990 _("unsupported reloc %u"), r_type
);
2993 case elfcpp::R_X86_64_DTPOFF32
:
2994 // This relocation type is used in debugging information.
2995 // In that case we need to not optimize the value. If the
2996 // section is not executable, then we assume we should not
2997 // optimize this reloc. See comments above for R_X86_64_TLSGD,
2998 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3000 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3002 gold_assert(tls_segment
!= NULL
);
3003 value
-= tls_segment
->memsz();
3005 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
3008 case elfcpp::R_X86_64_DTPOFF64
:
3009 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3010 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3012 gold_assert(tls_segment
!= NULL
);
3013 value
-= tls_segment
->memsz();
3015 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
3018 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3019 if (optimized_type
== tls::TLSOPT_TO_LE
)
3021 gold_assert(tls_segment
!= NULL
);
3022 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
3023 rela
, r_type
, value
, view
,
3027 else if (optimized_type
== tls::TLSOPT_NONE
)
3029 // Relocate the field with the offset of the GOT entry for
3030 // the tp-relative offset of the symbol.
3031 unsigned int got_offset
;
3034 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
3035 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
3036 - target
->got_size());
3040 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
3041 gold_assert(object
->local_has_got_offset(r_sym
,
3042 GOT_TYPE_TLS_OFFSET
));
3043 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
3044 - target
->got_size());
3046 value
= target
->got_plt_section()->address() + got_offset
;
3047 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
3050 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3051 _("unsupported reloc type %u"),
3055 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3056 value
-= tls_segment
->memsz();
3057 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
3062 // Do a relocation in which we convert a TLS General-Dynamic to an
3066 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
3069 const elfcpp::Rela
<64, false>& rela
,
3071 elfcpp::Elf_types
<64>::Elf_Addr value
,
3072 unsigned char* view
,
3073 elfcpp::Elf_types
<64>::Elf_Addr address
,
3074 section_size_type view_size
)
3076 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3077 // .word 0x6666; rex64; call __tls_get_addr
3078 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3080 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
3081 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3083 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3084 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3085 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3086 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3088 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3090 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3091 Relocate_functions
<64, false>::pcrela32(view
+ 8, value
, addend
- 8, address
);
3093 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3095 this->skip_call_tls_get_addr_
= true;
3098 // Do a relocation in which we convert a TLS General-Dynamic to a
3102 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
3104 Output_segment
* tls_segment
,
3105 const elfcpp::Rela
<64, false>& rela
,
3107 elfcpp::Elf_types
<64>::Elf_Addr value
,
3108 unsigned char* view
,
3109 section_size_type view_size
)
3111 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3112 // .word 0x6666; rex64; call __tls_get_addr
3113 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3115 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
3116 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3118 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3119 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3120 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3121 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3123 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
3125 value
-= tls_segment
->memsz();
3126 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
3128 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3130 this->skip_call_tls_get_addr_
= true;
3133 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3136 Target_x86_64::Relocate::tls_desc_gd_to_ie(
3137 const Relocate_info
<64, false>* relinfo
,
3140 const elfcpp::Rela
<64, false>& rela
,
3141 unsigned int r_type
,
3142 elfcpp::Elf_types
<64>::Elf_Addr value
,
3143 unsigned char* view
,
3144 elfcpp::Elf_types
<64>::Elf_Addr address
,
3145 section_size_type view_size
)
3147 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3149 // leaq foo@tlsdesc(%rip), %rax
3150 // ==> movq foo@gottpoff(%rip), %rax
3151 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3152 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3153 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3154 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
3156 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3157 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
3161 // call *foo@tlscall(%rax)
3163 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
3164 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
3165 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3166 view
[0] == 0xff && view
[1] == 0x10);
3172 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3175 Target_x86_64::Relocate::tls_desc_gd_to_le(
3176 const Relocate_info
<64, false>* relinfo
,
3178 Output_segment
* tls_segment
,
3179 const elfcpp::Rela
<64, false>& rela
,
3180 unsigned int r_type
,
3181 elfcpp::Elf_types
<64>::Elf_Addr value
,
3182 unsigned char* view
,
3183 section_size_type view_size
)
3185 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3187 // leaq foo@tlsdesc(%rip), %rax
3188 // ==> movq foo@tpoff, %rax
3189 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3190 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3191 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3192 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
3195 value
-= tls_segment
->memsz();
3196 Relocate_functions
<64, false>::rela32(view
, value
, 0);
3200 // call *foo@tlscall(%rax)
3202 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
3203 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
3204 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3205 view
[0] == 0xff && view
[1] == 0x10);
3212 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
3215 const elfcpp::Rela
<64, false>& rela
,
3217 elfcpp::Elf_types
<64>::Elf_Addr
,
3218 unsigned char* view
,
3219 section_size_type view_size
)
3221 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3222 // ... leq foo@dtpoff(%rax),%reg
3223 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3225 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3226 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
3228 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3229 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
3231 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
3233 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3235 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3237 this->skip_call_tls_get_addr_
= true;
3240 // Do a relocation in which we convert a TLS Initial-Exec to a
3244 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
3246 Output_segment
* tls_segment
,
3247 const elfcpp::Rela
<64, false>& rela
,
3249 elfcpp::Elf_types
<64>::Elf_Addr value
,
3250 unsigned char* view
,
3251 section_size_type view_size
)
3253 // We need to examine the opcodes to figure out which instruction we
3256 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3257 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3259 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3260 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3262 unsigned char op1
= view
[-3];
3263 unsigned char op2
= view
[-2];
3264 unsigned char op3
= view
[-1];
3265 unsigned char reg
= op3
>> 3;
3273 view
[-1] = 0xc0 | reg
;
3277 // Special handling for %rsp.
3281 view
[-1] = 0xc0 | reg
;
3289 view
[-1] = 0x80 | reg
| (reg
<< 3);
3292 value
-= tls_segment
->memsz();
3293 Relocate_functions
<64, false>::rela32(view
, value
, 0);
3296 // Relocate section data.
3299 Target_x86_64::relocate_section(
3300 const Relocate_info
<64, false>* relinfo
,
3301 unsigned int sh_type
,
3302 const unsigned char* prelocs
,
3304 Output_section
* output_section
,
3305 bool needs_special_offset_handling
,
3306 unsigned char* view
,
3307 elfcpp::Elf_types
<64>::Elf_Addr address
,
3308 section_size_type view_size
,
3309 const Reloc_symbol_changes
* reloc_symbol_changes
)
3311 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3313 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
3314 Target_x86_64::Relocate
>(
3320 needs_special_offset_handling
,
3324 reloc_symbol_changes
);
3327 // Apply an incremental relocation. Incremental relocations always refer
3328 // to global symbols.
3331 Target_x86_64::apply_relocation(
3332 const Relocate_info
<64, false>* relinfo
,
3333 elfcpp::Elf_types
<64>::Elf_Addr r_offset
,
3334 unsigned int r_type
,
3335 elfcpp::Elf_types
<64>::Elf_Swxword r_addend
,
3337 unsigned char* view
,
3338 elfcpp::Elf_types
<64>::Elf_Addr address
,
3339 section_size_type view_size
)
3341 gold::apply_relocation
<64, false, Target_x86_64
, Target_x86_64::Relocate
>(
3353 // Return the size of a relocation while scanning during a relocatable
3357 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3358 unsigned int r_type
,
3363 case elfcpp::R_X86_64_NONE
:
3364 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3365 case elfcpp::R_X86_64_GNU_VTENTRY
:
3366 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3367 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3368 case elfcpp::R_X86_64_TLSDESC_CALL
:
3369 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3370 case elfcpp::R_X86_64_DTPOFF32
:
3371 case elfcpp::R_X86_64_DTPOFF64
:
3372 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3373 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3376 case elfcpp::R_X86_64_64
:
3377 case elfcpp::R_X86_64_PC64
:
3378 case elfcpp::R_X86_64_GOTOFF64
:
3379 case elfcpp::R_X86_64_GOTPC64
:
3380 case elfcpp::R_X86_64_PLTOFF64
:
3381 case elfcpp::R_X86_64_GOT64
:
3382 case elfcpp::R_X86_64_GOTPCREL64
:
3383 case elfcpp::R_X86_64_GOTPCREL
:
3384 case elfcpp::R_X86_64_GOTPLT64
:
3387 case elfcpp::R_X86_64_32
:
3388 case elfcpp::R_X86_64_32S
:
3389 case elfcpp::R_X86_64_PC32
:
3390 case elfcpp::R_X86_64_PLT32
:
3391 case elfcpp::R_X86_64_GOTPC32
:
3392 case elfcpp::R_X86_64_GOT32
:
3395 case elfcpp::R_X86_64_16
:
3396 case elfcpp::R_X86_64_PC16
:
3399 case elfcpp::R_X86_64_8
:
3400 case elfcpp::R_X86_64_PC8
:
3403 case elfcpp::R_X86_64_COPY
:
3404 case elfcpp::R_X86_64_GLOB_DAT
:
3405 case elfcpp::R_X86_64_JUMP_SLOT
:
3406 case elfcpp::R_X86_64_RELATIVE
:
3407 case elfcpp::R_X86_64_IRELATIVE
:
3408 // These are outstanding tls relocs, which are unexpected when linking
3409 case elfcpp::R_X86_64_TPOFF64
:
3410 case elfcpp::R_X86_64_DTPMOD64
:
3411 case elfcpp::R_X86_64_TLSDESC
:
3412 object
->error(_("unexpected reloc %u in object file"), r_type
);
3415 case elfcpp::R_X86_64_SIZE32
:
3416 case elfcpp::R_X86_64_SIZE64
:
3418 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
3423 // Scan the relocs during a relocatable link.
3426 Target_x86_64::scan_relocatable_relocs(Symbol_table
* symtab
,
3428 Sized_relobj_file
<64, false>* object
,
3429 unsigned int data_shndx
,
3430 unsigned int sh_type
,
3431 const unsigned char* prelocs
,
3433 Output_section
* output_section
,
3434 bool needs_special_offset_handling
,
3435 size_t local_symbol_count
,
3436 const unsigned char* plocal_symbols
,
3437 Relocatable_relocs
* rr
)
3439 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3441 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
3442 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
3444 gold::scan_relocatable_relocs
<64, false, elfcpp::SHT_RELA
,
3445 Scan_relocatable_relocs
>(
3453 needs_special_offset_handling
,
3459 // Relocate a section during a relocatable link.
3462 Target_x86_64::relocate_for_relocatable(
3463 const Relocate_info
<64, false>* relinfo
,
3464 unsigned int sh_type
,
3465 const unsigned char* prelocs
,
3467 Output_section
* output_section
,
3468 off_t offset_in_output_section
,
3469 const Relocatable_relocs
* rr
,
3470 unsigned char* view
,
3471 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
3472 section_size_type view_size
,
3473 unsigned char* reloc_view
,
3474 section_size_type reloc_view_size
)
3476 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3478 gold::relocate_for_relocatable
<64, false, elfcpp::SHT_RELA
>(
3483 offset_in_output_section
,
3492 // Return the value to use for a dynamic which requires special
3493 // treatment. This is how we support equality comparisons of function
3494 // pointers across shared library boundaries, as described in the
3495 // processor specific ABI supplement.
3498 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
3500 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
3501 return this->plt_section()->address() + gsym
->plt_offset();
3504 // Return a string used to fill a code section with nops to take up
3505 // the specified length.
3508 Target_x86_64::do_code_fill(section_size_type length
) const
3512 // Build a jmpq instruction to skip over the bytes.
3513 unsigned char jmp
[5];
3515 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
3516 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
3517 + std::string(length
- 5, '\0'));
3520 // Nop sequences of various lengths.
3521 const char nop1
[1] = { 0x90 }; // nop
3522 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
3523 const char nop3
[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3524 const char nop4
[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3525 const char nop5
[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3527 const char nop6
[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3529 const char nop7
[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3531 const char nop8
[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3532 0x00, 0x00, 0x00, 0x00 };
3533 const char nop9
[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3534 0x00, 0x00, 0x00, 0x00,
3536 const char nop10
[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3537 0x84, 0x00, 0x00, 0x00,
3539 const char nop11
[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3540 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3542 const char nop12
[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3543 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3544 0x00, 0x00, 0x00, 0x00 };
3545 const char nop13
[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3546 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3547 0x00, 0x00, 0x00, 0x00,
3549 const char nop14
[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3550 0x66, 0x2e, 0x0f, 0x1f, // data16
3551 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3553 const char nop15
[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3554 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3555 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3558 const char* nops
[16] = {
3560 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
3561 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
3564 return std::string(nops
[length
], length
);
3567 // Return the addend to use for a target specific relocation. The
3568 // only target specific relocation is R_X86_64_TLSDESC for a local
3569 // symbol. We want to set the addend is the offset of the local
3570 // symbol in the TLS segment.
3573 Target_x86_64::do_reloc_addend(void* arg
, unsigned int r_type
,
3576 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
3577 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
3578 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
3579 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
3580 const Symbol_value
<64>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
3581 gold_assert(psymval
->is_tls_symbol());
3582 // The value of a TLS symbol is the offset in the TLS segment.
3583 return psymval
->value(ti
.object
, 0);
3586 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3587 // compiled with -fsplit-stack. The function calls non-split-stack
3588 // code. We have to change the function so that it always ensures
3589 // that it has enough stack space to run some random function.
3592 Target_x86_64::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
3593 section_offset_type fnoffset
,
3594 section_size_type fnsize
,
3595 unsigned char* view
,
3596 section_size_type view_size
,
3598 std::string
* to
) const
3600 // The function starts with a comparison of the stack pointer and a
3601 // field in the TCB. This is followed by a jump.
3604 if (this->match_view(view
, view_size
, fnoffset
, "\x64\x48\x3b\x24\x25", 5)
3607 // We will call __morestack if the carry flag is set after this
3608 // comparison. We turn the comparison into an stc instruction
3610 view
[fnoffset
] = '\xf9';
3611 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 8);
3613 // lea NN(%rsp),%r10
3614 // lea NN(%rsp),%r11
3615 else if ((this->match_view(view
, view_size
, fnoffset
,
3616 "\x4c\x8d\x94\x24", 4)
3617 || this->match_view(view
, view_size
, fnoffset
,
3618 "\x4c\x8d\x9c\x24", 4))
3621 // This is loading an offset from the stack pointer for a
3622 // comparison. The offset is negative, so we decrease the
3623 // offset by the amount of space we need for the stack. This
3624 // means we will avoid calling __morestack if there happens to
3625 // be plenty of space on the stack already.
3626 unsigned char* pval
= view
+ fnoffset
+ 4;
3627 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
3628 val
-= parameters
->options().split_stack_adjust_size();
3629 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
3633 if (!object
->has_no_split_stack())
3634 object
->error(_("failed to match split-stack sequence at "
3635 "section %u offset %0zx"),
3636 shndx
, static_cast<size_t>(fnoffset
));
3640 // We have to change the function so that it calls
3641 // __morestack_non_split instead of __morestack. The former will
3642 // allocate additional stack space.
3643 *from
= "__morestack";
3644 *to
= "__morestack_non_split";
3647 // The selector for x86_64 object files.
3649 class Target_selector_x86_64
: public Target_selector_freebsd
3652 Target_selector_x86_64()
3653 : Target_selector_freebsd(elfcpp::EM_X86_64
, 64, false, "elf64-x86-64",
3654 "elf64-x86-64-freebsd")
3658 do_instantiate_target()
3659 { return new Target_x86_64(); }
3663 Target_selector_x86_64 target_selector_x86_64
;
3665 } // End anonymous namespace.