1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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 Sized_target
<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 : Sized_target
<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 // Hook for a new output section.
222 do_new_output_section(Output_section
*) const;
224 // Scan the relocations to look for symbol adjustments.
226 gc_process_relocs(Symbol_table
* symtab
,
228 Sized_relobj_file
<64, false>* object
,
229 unsigned int data_shndx
,
230 unsigned int sh_type
,
231 const unsigned char* prelocs
,
233 Output_section
* output_section
,
234 bool needs_special_offset_handling
,
235 size_t local_symbol_count
,
236 const unsigned char* plocal_symbols
);
238 // Scan the relocations to look for symbol adjustments.
240 scan_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 // Finalize the sections.
254 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
256 // Return the value to use for a dynamic which requires special
259 do_dynsym_value(const Symbol
*) const;
261 // Relocate a section.
263 relocate_section(const Relocate_info
<64, false>*,
264 unsigned int sh_type
,
265 const unsigned char* prelocs
,
267 Output_section
* output_section
,
268 bool needs_special_offset_handling
,
270 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
271 section_size_type view_size
,
272 const Reloc_symbol_changes
*);
274 // Scan the relocs during a relocatable link.
276 scan_relocatable_relocs(Symbol_table
* symtab
,
278 Sized_relobj_file
<64, false>* object
,
279 unsigned int data_shndx
,
280 unsigned int sh_type
,
281 const unsigned char* prelocs
,
283 Output_section
* output_section
,
284 bool needs_special_offset_handling
,
285 size_t local_symbol_count
,
286 const unsigned char* plocal_symbols
,
287 Relocatable_relocs
*);
289 // Relocate a section during a relocatable link.
291 relocate_for_relocatable(const Relocate_info
<64, false>*,
292 unsigned int sh_type
,
293 const unsigned char* prelocs
,
295 Output_section
* output_section
,
296 off_t offset_in_output_section
,
297 const Relocatable_relocs
*,
299 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
300 section_size_type view_size
,
301 unsigned char* reloc_view
,
302 section_size_type reloc_view_size
);
304 // Return a string used to fill a code section with nops.
306 do_code_fill(section_size_type length
) const;
308 // Return whether SYM is defined by the ABI.
310 do_is_defined_by_abi(const Symbol
* sym
) const
311 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
313 // Return the symbol index to use for a target specific relocation.
314 // The only target specific relocation is R_X86_64_TLSDESC for a
315 // local symbol, which is an absolute reloc.
317 do_reloc_symbol_index(void*, unsigned int r_type
) const
319 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
323 // Return the addend to use for a target specific relocation.
325 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
327 // Return the PLT section.
329 do_plt_section_for_global(const Symbol
*) const
330 { return this->plt_section(); }
333 do_plt_section_for_local(const Relobj
*, unsigned int) const
334 { return this->plt_section(); }
336 // This function should be defined in targets that can use relocation
337 // types to determine (implemented in local_reloc_may_be_function_pointer
338 // and global_reloc_may_be_function_pointer)
339 // if a function's pointer is taken. ICF uses this in safe mode to only
340 // fold those functions whose pointer is defintely not taken. For x86_64
341 // pie binaries, safe ICF cannot be done by looking at relocation types.
343 do_can_check_for_function_pointers() const
344 { return !parameters
->options().pie(); }
346 // Adjust -fsplit-stack code which calls non-split-stack code.
348 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
349 section_offset_type fnoffset
, section_size_type fnsize
,
350 unsigned char* view
, section_size_type view_size
,
351 std::string
* from
, std::string
* to
) const;
353 // Return the size of the GOT section.
357 gold_assert(this->got_
!= NULL
);
358 return this->got_
->data_size();
361 // Return the number of entries in the GOT.
363 got_entry_count() const
365 if (this->got_
== NULL
)
367 return this->got_size() / 8;
370 // Return the number of entries in the PLT.
372 plt_entry_count() const;
374 // Return the offset of the first non-reserved PLT entry.
376 first_plt_entry_offset() const;
378 // Return the size of each PLT entry.
380 plt_entry_size() const;
382 // Create the GOT section for an incremental update.
383 Output_data_got
<64, false>*
384 init_got_plt_for_update(Symbol_table
* symtab
,
386 unsigned int got_count
,
387 unsigned int plt_count
);
389 // Reserve a GOT entry for a local symbol, and regenerate any
390 // necessary dynamic relocations.
392 reserve_local_got_entry(unsigned int got_index
,
393 Sized_relobj
<64, false>* obj
,
395 unsigned int got_type
);
397 // Reserve a GOT entry for a global symbol, and regenerate any
398 // necessary dynamic relocations.
400 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
401 unsigned int got_type
);
403 // Register an existing PLT entry for a global symbol.
405 register_global_plt_entry(unsigned int plt_index
, Symbol
* gsym
);
407 // Force a COPY relocation for a given symbol.
409 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
411 // Apply an incremental relocation.
413 apply_relocation(const Relocate_info
<64, false>* relinfo
,
414 elfcpp::Elf_types
<64>::Elf_Addr r_offset
,
416 elfcpp::Elf_types
<64>::Elf_Swxword r_addend
,
419 elfcpp::Elf_types
<64>::Elf_Addr address
,
420 section_size_type view_size
);
422 // Add a new reloc argument, returning the index in the vector.
424 add_tlsdesc_info(Sized_relobj_file
<64, false>* object
, unsigned int r_sym
)
426 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
427 return this->tlsdesc_reloc_info_
.size() - 1;
431 // The class which scans relocations.
436 : issued_non_pic_error_(false)
440 get_reference_flags(unsigned int r_type
);
443 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
444 Sized_relobj_file
<64, false>* object
,
445 unsigned int data_shndx
,
446 Output_section
* output_section
,
447 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
448 const elfcpp::Sym
<64, false>& lsym
);
451 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
452 Sized_relobj_file
<64, false>* object
,
453 unsigned int data_shndx
,
454 Output_section
* output_section
,
455 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
459 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
460 Target_x86_64
* target
,
461 Sized_relobj_file
<64, false>* object
,
462 unsigned int data_shndx
,
463 Output_section
* output_section
,
464 const elfcpp::Rela
<64, false>& reloc
,
466 const elfcpp::Sym
<64, false>& lsym
);
469 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
470 Target_x86_64
* target
,
471 Sized_relobj_file
<64, false>* object
,
472 unsigned int data_shndx
,
473 Output_section
* output_section
,
474 const elfcpp::Rela
<64, false>& reloc
,
480 unsupported_reloc_local(Sized_relobj_file
<64, false>*, unsigned int r_type
);
483 unsupported_reloc_global(Sized_relobj_file
<64, false>*, unsigned int r_type
,
487 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
490 possible_function_pointer_reloc(unsigned int r_type
);
493 reloc_needs_plt_for_ifunc(Sized_relobj_file
<64, false>*,
494 unsigned int r_type
);
496 // Whether we have issued an error about a non-PIC compilation.
497 bool issued_non_pic_error_
;
500 // The class which implements relocation.
505 : skip_call_tls_get_addr_(false)
510 if (this->skip_call_tls_get_addr_
)
512 // FIXME: This needs to specify the location somehow.
513 gold_error(_("missing expected TLS relocation"));
517 // Do a relocation. Return false if the caller should not issue
518 // any warnings about this relocation.
520 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, Output_section
*,
521 size_t relnum
, const elfcpp::Rela
<64, false>&,
522 unsigned int r_type
, const Sized_symbol
<64>*,
523 const Symbol_value
<64>*,
524 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
528 // Do a TLS relocation.
530 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
531 size_t relnum
, const elfcpp::Rela
<64, false>&,
532 unsigned int r_type
, const Sized_symbol
<64>*,
533 const Symbol_value
<64>*,
534 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
537 // Do a TLS General-Dynamic to Initial-Exec transition.
539 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
540 Output_segment
* tls_segment
,
541 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
542 elfcpp::Elf_types
<64>::Elf_Addr value
,
544 elfcpp::Elf_types
<64>::Elf_Addr
,
545 section_size_type view_size
);
547 // Do a TLS General-Dynamic to Local-Exec transition.
549 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
550 Output_segment
* tls_segment
,
551 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
552 elfcpp::Elf_types
<64>::Elf_Addr value
,
554 section_size_type view_size
);
556 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
558 tls_desc_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
559 Output_segment
* tls_segment
,
560 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
561 elfcpp::Elf_types
<64>::Elf_Addr value
,
563 elfcpp::Elf_types
<64>::Elf_Addr
,
564 section_size_type view_size
);
566 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
568 tls_desc_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
569 Output_segment
* tls_segment
,
570 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
571 elfcpp::Elf_types
<64>::Elf_Addr value
,
573 section_size_type view_size
);
575 // Do a TLS Local-Dynamic to Local-Exec transition.
577 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
578 Output_segment
* tls_segment
,
579 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
580 elfcpp::Elf_types
<64>::Elf_Addr value
,
582 section_size_type view_size
);
584 // Do a TLS Initial-Exec to Local-Exec transition.
586 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
587 Output_segment
* tls_segment
,
588 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
589 elfcpp::Elf_types
<64>::Elf_Addr value
,
591 section_size_type view_size
);
593 // This is set if we should skip the next reloc, which should be a
594 // PLT32 reloc against ___tls_get_addr.
595 bool skip_call_tls_get_addr_
;
598 // A class which returns the size required for a relocation type,
599 // used while scanning relocs during a relocatable link.
600 class Relocatable_size_for_reloc
604 get_size_for_reloc(unsigned int, Relobj
*);
607 // Adjust TLS relocation type based on the options and whether this
608 // is a local symbol.
609 static tls::Tls_optimization
610 optimize_tls_reloc(bool is_final
, int r_type
);
612 // Get the GOT section, creating it if necessary.
613 Output_data_got
<64, false>*
614 got_section(Symbol_table
*, Layout
*);
616 // Get the GOT PLT section.
618 got_plt_section() const
620 gold_assert(this->got_plt_
!= NULL
);
621 return this->got_plt_
;
624 // Get the GOT section for TLSDESC entries.
625 Output_data_got
<64, false>*
626 got_tlsdesc_section() const
628 gold_assert(this->got_tlsdesc_
!= NULL
);
629 return this->got_tlsdesc_
;
632 // Create the PLT section.
634 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
636 // Create a PLT entry for a global symbol.
638 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
640 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
642 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
643 Sized_relobj_file
<64, false>* relobj
,
644 unsigned int local_sym_index
);
646 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
648 define_tls_base_symbol(Symbol_table
*, Layout
*);
650 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
652 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
654 // Create a GOT entry for the TLS module index.
656 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
657 Sized_relobj_file
<64, false>* object
);
659 // Get the PLT section.
660 Output_data_plt_x86_64
*
663 gold_assert(this->plt_
!= NULL
);
667 // Get the dynamic reloc section, creating it if necessary.
669 rela_dyn_section(Layout
*);
671 // Get the section to use for TLSDESC relocations.
673 rela_tlsdesc_section(Layout
*) const;
675 // Add a potential copy relocation.
677 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
678 Sized_relobj_file
<64, false>* object
,
679 unsigned int shndx
, Output_section
* output_section
,
680 Symbol
* sym
, const elfcpp::Rela
<64, false>& reloc
)
682 this->copy_relocs_
.copy_reloc(symtab
, layout
,
683 symtab
->get_sized_symbol
<64>(sym
),
684 object
, shndx
, output_section
,
685 reloc
, this->rela_dyn_section(layout
));
688 // Information about this specific target which we pass to the
689 // general Target structure.
690 static const Target::Target_info x86_64_info
;
692 // The types of GOT entries needed for this platform.
693 // These values are exposed to the ABI in an incremental link.
694 // Do not renumber existing values without changing the version
695 // number of the .gnu_incremental_inputs section.
698 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
699 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
700 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
701 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
704 // This type is used as the argument to the target specific
705 // relocation routines. The only target specific reloc is
706 // R_X86_64_TLSDESC against a local symbol.
709 Tlsdesc_info(Sized_relobj_file
<64, false>* a_object
, unsigned int a_r_sym
)
710 : object(a_object
), r_sym(a_r_sym
)
713 // The object in which the local symbol is defined.
714 Sized_relobj_file
<64, false>* object
;
715 // The local symbol index in the object.
720 Output_data_got
<64, false>* got_
;
722 Output_data_plt_x86_64
* plt_
;
723 // The GOT PLT section.
724 Output_data_space
* got_plt_
;
725 // The GOT section for TLSDESC relocations.
726 Output_data_got
<64, false>* got_tlsdesc_
;
727 // The _GLOBAL_OFFSET_TABLE_ symbol.
728 Symbol
* global_offset_table_
;
729 // The dynamic reloc section.
730 Reloc_section
* rela_dyn_
;
731 // Relocs saved to avoid a COPY reloc.
732 Copy_relocs
<elfcpp::SHT_RELA
, 64, false> copy_relocs_
;
733 // Space for variables copied with a COPY reloc.
734 Output_data_space
* dynbss_
;
735 // Offset of the GOT entry for the TLS module index.
736 unsigned int got_mod_index_offset_
;
737 // We handle R_X86_64_TLSDESC against a local symbol as a target
738 // specific relocation. Here we store the object and local symbol
739 // index for the relocation.
740 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
741 // True if the _TLS_MODULE_BASE_ symbol has been defined.
742 bool tls_base_symbol_defined_
;
745 const Target::Target_info
Target_x86_64::x86_64_info
=
748 false, // is_big_endian
749 elfcpp::EM_X86_64
, // machine_code
750 false, // has_make_symbol
751 false, // has_resolve
752 true, // has_code_fill
753 true, // is_default_stack_executable
754 true, // can_icf_inline_merge_sections
756 "/lib/ld64.so.1", // program interpreter
757 0x400000, // default_text_segment_address
758 0x1000, // abi_pagesize (overridable by -z max-page-size)
759 0x1000, // common_pagesize (overridable by -z common-page-size)
760 elfcpp::SHN_UNDEF
, // small_common_shndx
761 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
762 0, // small_common_section_flags
763 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
764 NULL
, // attributes_section
765 NULL
// attributes_vendor
768 // This is called when a new output section is created. This is where
769 // we handle the SHF_X86_64_LARGE.
772 Target_x86_64::do_new_output_section(Output_section
* os
) const
774 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
775 os
->set_is_large_section();
778 // Get the GOT section, creating it if necessary.
780 Output_data_got
<64, false>*
781 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
783 if (this->got_
== NULL
)
785 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
787 this->got_
= new Output_data_got
<64, false>();
789 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
791 | elfcpp::SHF_WRITE
),
792 this->got_
, ORDER_RELRO_LAST
,
795 this->got_plt_
= new Output_data_space(8, "** GOT PLT");
796 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
798 | elfcpp::SHF_WRITE
),
799 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
802 // The first three entries are reserved.
803 this->got_plt_
->set_current_data_size(3 * 8);
805 // Those bytes can go into the relro segment.
806 layout
->increase_relro(3 * 8);
808 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
809 this->global_offset_table_
=
810 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
811 Symbol_table::PREDEFINED
,
813 0, 0, elfcpp::STT_OBJECT
,
815 elfcpp::STV_HIDDEN
, 0,
818 // If there are any TLSDESC relocations, they get GOT entries in
819 // .got.plt after the jump slot entries.
820 this->got_tlsdesc_
= new Output_data_got
<64, false>();
821 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
823 | elfcpp::SHF_WRITE
),
825 ORDER_NON_RELRO_FIRST
, false);
831 // Get the dynamic reloc section, creating it if necessary.
833 Target_x86_64::Reloc_section
*
834 Target_x86_64::rela_dyn_section(Layout
* layout
)
836 if (this->rela_dyn_
== NULL
)
838 gold_assert(layout
!= NULL
);
839 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
840 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
841 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
842 ORDER_DYNAMIC_RELOCS
, false);
844 return this->rela_dyn_
;
847 // Initialize the PLT section.
850 Output_data_plt_x86_64::init(Symbol_table
* symtab
, Layout
* layout
)
852 this->rel_
= new Reloc_section(false);
853 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
854 elfcpp::SHF_ALLOC
, this->rel_
,
855 ORDER_DYNAMIC_PLT_RELOCS
, false);
857 if (parameters
->doing_static_link())
859 // A statically linked executable will only have a .rela.plt
860 // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
861 // symbols. The library will use these symbols to locate the
862 // IRELATIVE relocs at program startup time.
863 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
864 Symbol_table::PREDEFINED
,
865 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
866 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
868 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
869 Symbol_table::PREDEFINED
,
870 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
871 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
877 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
879 os
->set_entsize(plt_entry_size
);
882 // Add an entry to the PLT.
885 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
887 gold_assert(!gsym
->has_plt_offset());
889 unsigned int plt_index
;
891 section_offset_type got_offset
;
893 if (!this->is_data_size_valid())
895 // Note that when setting the PLT offset we skip the initial
896 // reserved PLT entry.
897 plt_index
= this->count_
+ 1;
898 plt_offset
= plt_index
* plt_entry_size
;
902 got_offset
= (plt_index
- 1 + 3) * 8;
903 gold_assert(got_offset
== this->got_plt_
->current_data_size());
905 // Every PLT entry needs a GOT entry which points back to the PLT
906 // entry (this will be changed by the dynamic linker, normally
907 // lazily when the function is called).
908 this->got_plt_
->set_current_data_size(got_offset
+ 8);
912 // For incremental updates, find an available slot.
913 plt_offset
= this->free_list_
.allocate(plt_entry_size
, plt_entry_size
, 0);
914 if (plt_offset
== -1)
915 gold_fallback(_("out of patch space (PLT);"
916 " relink with --incremental-full"));
918 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
919 // can be calculated from the PLT index, adjusting for the three
920 // reserved entries at the beginning of the GOT.
921 plt_index
= plt_offset
/ plt_entry_size
- 1;
922 got_offset
= (plt_index
- 1 + 3) * 8;
925 gsym
->set_plt_offset(plt_offset
);
927 // Every PLT entry needs a reloc.
928 this->add_relocation(gsym
, got_offset
);
930 // Note that we don't need to save the symbol. The contents of the
931 // PLT are independent of which symbols are used. The symbols only
932 // appear in the relocations.
935 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
939 Output_data_plt_x86_64::add_local_ifunc_entry(
940 Sized_relobj_file
<64, false>* relobj
,
941 unsigned int local_sym_index
)
943 unsigned int plt_offset
= (this->count_
+ 1) * plt_entry_size
;
946 section_offset_type got_offset
= this->got_plt_
->current_data_size();
948 // Every PLT entry needs a GOT entry which points back to the PLT
950 this->got_plt_
->set_current_data_size(got_offset
+ 8);
952 // Every PLT entry needs a reloc.
953 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
,
954 elfcpp::R_X86_64_IRELATIVE
,
955 this->got_plt_
, got_offset
, 0);
960 // Add the relocation for a PLT entry.
963 Output_data_plt_x86_64::add_relocation(Symbol
* gsym
, unsigned int got_offset
)
965 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
966 && gsym
->can_use_relative_reloc(false))
967 this->rel_
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
968 this->got_plt_
, got_offset
, 0);
971 gsym
->set_needs_dynsym_entry();
972 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
977 // Return where the TLSDESC relocations should go, creating it if
978 // necessary. These follow the JUMP_SLOT relocations.
980 Output_data_plt_x86_64::Reloc_section
*
981 Output_data_plt_x86_64::rela_tlsdesc(Layout
* layout
)
983 if (this->tlsdesc_rel_
== NULL
)
985 this->tlsdesc_rel_
= new Reloc_section(false);
986 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
987 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
988 ORDER_DYNAMIC_PLT_RELOCS
, false);
989 gold_assert(this->tlsdesc_rel_
->output_section() ==
990 this->rel_
->output_section());
992 return this->tlsdesc_rel_
;
995 // Set the final size.
997 Output_data_plt_x86_64::set_final_data_size()
999 unsigned int count
= this->count_
;
1000 if (this->has_tlsdesc_entry())
1002 this->set_data_size((count
+ 1) * plt_entry_size
);
1005 // The first entry in the PLT for an executable.
1007 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
1009 // From AMD64 ABI Draft 0.98, page 76
1010 0xff, 0x35, // pushq contents of memory address
1011 0, 0, 0, 0, // replaced with address of .got + 8
1012 0xff, 0x25, // jmp indirect
1013 0, 0, 0, 0, // replaced with address of .got + 16
1014 0x90, 0x90, 0x90, 0x90 // noop (x4)
1017 // Subsequent entries in the PLT for an executable.
1019 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
1021 // From AMD64 ABI Draft 0.98, page 76
1022 0xff, 0x25, // jmpq indirect
1023 0, 0, 0, 0, // replaced with address of symbol in .got
1024 0x68, // pushq immediate
1025 0, 0, 0, 0, // replaced with offset into relocation table
1026 0xe9, // jmpq relative
1027 0, 0, 0, 0 // replaced with offset to start of .plt
1030 // The reserved TLSDESC entry in the PLT for an executable.
1032 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry
[plt_entry_size
] =
1034 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1035 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1036 0xff, 0x35, // pushq x(%rip)
1037 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1038 0xff, 0x25, // jmpq *y(%rip)
1039 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1044 // Write out the PLT. This uses the hand-coded instructions above,
1045 // and adjusts them as needed. This is specified by the AMD64 ABI.
1048 Output_data_plt_x86_64::do_write(Output_file
* of
)
1050 const off_t offset
= this->offset();
1051 const section_size_type oview_size
=
1052 convert_to_section_size_type(this->data_size());
1053 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1055 const off_t got_file_offset
= this->got_plt_
->offset();
1056 const section_size_type got_size
=
1057 convert_to_section_size_type(this->got_plt_
->data_size());
1058 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1061 unsigned char* pov
= oview
;
1063 // The base address of the .plt section.
1064 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
1065 // The base address of the .got section.
1066 elfcpp::Elf_types
<64>::Elf_Addr got_base
= this->got_
->address();
1067 // The base address of the PLT portion of the .got section,
1068 // which is where the GOT pointer will point, and where the
1069 // three reserved GOT entries are located.
1070 elfcpp::Elf_types
<64>::Elf_Addr got_address
= this->got_plt_
->address();
1072 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1073 // We do a jmp relative to the PC at the end of this instruction.
1074 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1076 - (plt_address
+ 6)));
1077 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1079 - (plt_address
+ 12)));
1080 pov
+= plt_entry_size
;
1082 unsigned char* got_pov
= got_view
;
1084 memset(got_pov
, 0, 24);
1087 unsigned int plt_offset
= plt_entry_size
;
1088 unsigned int got_offset
= 24;
1089 const unsigned int count
= this->count_
;
1090 for (unsigned int plt_index
= 0;
1093 pov
+= plt_entry_size
,
1095 plt_offset
+= plt_entry_size
,
1098 // Set and adjust the PLT entry itself.
1099 memcpy(pov
, plt_entry
, plt_entry_size
);
1100 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1101 (got_address
+ got_offset
1102 - (plt_address
+ plt_offset
1105 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1106 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1107 - (plt_offset
+ plt_entry_size
));
1109 // Set the entry in the GOT.
1110 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
1113 if (this->has_tlsdesc_entry())
1115 // Set and adjust the reserved TLSDESC PLT entry.
1116 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
1117 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1118 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1120 - (plt_address
+ plt_offset
1122 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1124 + tlsdesc_got_offset
1125 - (plt_address
+ plt_offset
1127 pov
+= plt_entry_size
;
1130 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1131 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1133 of
->write_output_view(offset
, oview_size
, oview
);
1134 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1137 // Create the PLT section.
1140 Target_x86_64::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1142 if (this->plt_
== NULL
)
1144 // Create the GOT sections first.
1145 this->got_section(symtab
, layout
);
1147 this->plt_
= new Output_data_plt_x86_64(symtab
, layout
, this->got_
,
1149 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1151 | elfcpp::SHF_EXECINSTR
),
1152 this->plt_
, ORDER_PLT
, false);
1154 // Make the sh_info field of .rela.plt point to .plt.
1155 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1156 rela_plt_os
->set_info_section(this->plt_
->output_section());
1160 // Return the section for TLSDESC relocations.
1162 Target_x86_64::Reloc_section
*
1163 Target_x86_64::rela_tlsdesc_section(Layout
* layout
) const
1165 return this->plt_section()->rela_tlsdesc(layout
);
1168 // Create a PLT entry for a global symbol.
1171 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1174 if (gsym
->has_plt_offset())
1177 if (this->plt_
== NULL
)
1178 this->make_plt_section(symtab
, layout
);
1180 this->plt_
->add_entry(gsym
);
1183 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1186 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1187 Sized_relobj_file
<64, false>* relobj
,
1188 unsigned int local_sym_index
)
1190 if (relobj
->local_has_plt_offset(local_sym_index
))
1192 if (this->plt_
== NULL
)
1193 this->make_plt_section(symtab
, layout
);
1194 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(relobj
,
1196 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1199 // Return the number of entries in the PLT.
1202 Target_x86_64::plt_entry_count() const
1204 if (this->plt_
== NULL
)
1206 return this->plt_
->entry_count();
1209 // Return the offset of the first non-reserved PLT entry.
1212 Target_x86_64::first_plt_entry_offset() const
1214 return Output_data_plt_x86_64::first_plt_entry_offset();
1217 // Return the size of each PLT entry.
1220 Target_x86_64::plt_entry_size() const
1222 return Output_data_plt_x86_64::get_plt_entry_size();
1225 // Create the GOT and PLT sections for an incremental update.
1227 Output_data_got
<64, false>*
1228 Target_x86_64::init_got_plt_for_update(Symbol_table
* symtab
,
1230 unsigned int got_count
,
1231 unsigned int plt_count
)
1233 gold_assert(this->got_
== NULL
);
1235 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
1236 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1238 | elfcpp::SHF_WRITE
),
1239 this->got_
, ORDER_RELRO_LAST
,
1242 // Add the three reserved entries.
1243 this->got_plt_
= new Output_data_space((plt_count
+ 3) * 8, 8, "** GOT PLT");
1244 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1246 | elfcpp::SHF_WRITE
),
1247 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
1250 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1251 this->global_offset_table_
=
1252 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1253 Symbol_table::PREDEFINED
,
1255 0, 0, elfcpp::STT_OBJECT
,
1257 elfcpp::STV_HIDDEN
, 0,
1260 // If there are any TLSDESC relocations, they get GOT entries in
1261 // .got.plt after the jump slot entries.
1262 // FIXME: Get the count for TLSDESC entries.
1263 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
1264 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1265 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1267 ORDER_NON_RELRO_FIRST
, false);
1269 // Create the PLT section.
1270 this->plt_
= new Output_data_plt_x86_64(symtab
, layout
, this->got_
,
1271 this->got_plt_
, plt_count
);
1272 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1273 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1274 this->plt_
, ORDER_PLT
, false);
1276 // Make the sh_info field of .rela.plt point to .plt.
1277 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1278 rela_plt_os
->set_info_section(this->plt_
->output_section());
1280 // Create the rela_dyn section.
1281 this->rela_dyn_section(layout
);
1286 // Reserve a GOT entry for a local symbol, and regenerate any
1287 // necessary dynamic relocations.
1290 Target_x86_64::reserve_local_got_entry(
1291 unsigned int got_index
,
1292 Sized_relobj
<64, false>* obj
,
1294 unsigned int got_type
)
1296 unsigned int got_offset
= got_index
* 8;
1297 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1299 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
1302 case GOT_TYPE_STANDARD
:
1303 if (parameters
->options().output_is_position_independent())
1304 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
1305 this->got_
, got_offset
, 0);
1307 case GOT_TYPE_TLS_OFFSET
:
1308 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
1309 this->got_
, got_offset
, 0);
1311 case GOT_TYPE_TLS_PAIR
:
1312 this->got_
->reserve_slot(got_index
+ 1);
1313 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
1314 this->got_
, got_offset
, 0);
1316 case GOT_TYPE_TLS_DESC
:
1317 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1318 // this->got_->reserve_slot(got_index + 1);
1319 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1320 // this->got_, got_offset, 0);
1327 // Reserve a GOT entry for a global symbol, and regenerate any
1328 // necessary dynamic relocations.
1331 Target_x86_64::reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
1332 unsigned int got_type
)
1334 unsigned int got_offset
= got_index
* 8;
1335 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1337 this->got_
->reserve_global(got_index
, gsym
, got_type
);
1340 case GOT_TYPE_STANDARD
:
1341 if (!gsym
->final_value_is_known())
1343 if (gsym
->is_from_dynobj()
1344 || gsym
->is_undefined()
1345 || gsym
->is_preemptible()
1346 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1347 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
1348 this->got_
, got_offset
, 0);
1350 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1351 this->got_
, got_offset
, 0);
1354 case GOT_TYPE_TLS_OFFSET
:
1355 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
1356 this->got_
, got_offset
, 0);
1358 case GOT_TYPE_TLS_PAIR
:
1359 this->got_
->reserve_slot(got_index
+ 1);
1360 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
1361 this->got_
, got_offset
, 0);
1362 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
1363 this->got_
, got_offset
+ 8, 0);
1365 case GOT_TYPE_TLS_DESC
:
1366 this->got_
->reserve_slot(got_index
+ 1);
1367 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
1368 this->got_
, got_offset
, 0);
1375 // Register an existing PLT entry for a global symbol.
1378 Target_x86_64::register_global_plt_entry(unsigned int plt_index
,
1381 gold_assert(this->plt_
!= NULL
);
1382 gold_assert(!gsym
->has_plt_offset());
1384 this->plt_
->reserve_slot(plt_index
);
1386 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
1388 unsigned int got_offset
= (plt_index
+ 3) * 8;
1389 this->plt_
->add_relocation(gsym
, got_offset
);
1392 // Force a COPY relocation for a given symbol.
1395 Target_x86_64::emit_copy_reloc(
1396 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
1398 this->copy_relocs_
.emit_copy_reloc(symtab
,
1399 symtab
->get_sized_symbol
<64>(sym
),
1402 this->rela_dyn_section(NULL
));
1405 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1408 Target_x86_64::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
1410 if (this->tls_base_symbol_defined_
)
1413 Output_segment
* tls_segment
= layout
->tls_segment();
1414 if (tls_segment
!= NULL
)
1416 bool is_exec
= parameters
->options().output_is_executable();
1417 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1418 Symbol_table::PREDEFINED
,
1422 elfcpp::STV_HIDDEN
, 0,
1424 ? Symbol::SEGMENT_END
1425 : Symbol::SEGMENT_START
),
1428 this->tls_base_symbol_defined_
= true;
1431 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1434 Target_x86_64::reserve_tlsdesc_entries(Symbol_table
* symtab
,
1437 if (this->plt_
== NULL
)
1438 this->make_plt_section(symtab
, layout
);
1440 if (!this->plt_
->has_tlsdesc_entry())
1442 // Allocate the TLSDESC_GOT entry.
1443 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1444 unsigned int got_offset
= got
->add_constant(0);
1446 // Allocate the TLSDESC_PLT entry.
1447 this->plt_
->reserve_tlsdesc_entry(got_offset
);
1451 // Create a GOT entry for the TLS module index.
1454 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1455 Sized_relobj_file
<64, false>* object
)
1457 if (this->got_mod_index_offset_
== -1U)
1459 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1460 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1461 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1462 unsigned int got_offset
= got
->add_constant(0);
1463 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
1465 got
->add_constant(0);
1466 this->got_mod_index_offset_
= got_offset
;
1468 return this->got_mod_index_offset_
;
1471 // Optimize the TLS relocation type based on what we know about the
1472 // symbol. IS_FINAL is true if the final address of this symbol is
1473 // known at link time.
1475 tls::Tls_optimization
1476 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
1478 // If we are generating a shared library, then we can't do anything
1480 if (parameters
->options().shared())
1481 return tls::TLSOPT_NONE
;
1485 case elfcpp::R_X86_64_TLSGD
:
1486 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1487 case elfcpp::R_X86_64_TLSDESC_CALL
:
1488 // These are General-Dynamic which permits fully general TLS
1489 // access. Since we know that we are generating an executable,
1490 // we can convert this to Initial-Exec. If we also know that
1491 // this is a local symbol, we can further switch to Local-Exec.
1493 return tls::TLSOPT_TO_LE
;
1494 return tls::TLSOPT_TO_IE
;
1496 case elfcpp::R_X86_64_TLSLD
:
1497 // This is Local-Dynamic, which refers to a local symbol in the
1498 // dynamic TLS block. Since we know that we generating an
1499 // executable, we can switch to Local-Exec.
1500 return tls::TLSOPT_TO_LE
;
1502 case elfcpp::R_X86_64_DTPOFF32
:
1503 case elfcpp::R_X86_64_DTPOFF64
:
1504 // Another Local-Dynamic reloc.
1505 return tls::TLSOPT_TO_LE
;
1507 case elfcpp::R_X86_64_GOTTPOFF
:
1508 // These are Initial-Exec relocs which get the thread offset
1509 // from the GOT. If we know that we are linking against the
1510 // local symbol, we can switch to Local-Exec, which links the
1511 // thread offset into the instruction.
1513 return tls::TLSOPT_TO_LE
;
1514 return tls::TLSOPT_NONE
;
1516 case elfcpp::R_X86_64_TPOFF32
:
1517 // When we already have Local-Exec, there is nothing further we
1519 return tls::TLSOPT_NONE
;
1526 // Get the Reference_flags for a particular relocation.
1529 Target_x86_64::Scan::get_reference_flags(unsigned int r_type
)
1533 case elfcpp::R_X86_64_NONE
:
1534 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1535 case elfcpp::R_X86_64_GNU_VTENTRY
:
1536 case elfcpp::R_X86_64_GOTPC32
:
1537 case elfcpp::R_X86_64_GOTPC64
:
1538 // No symbol reference.
1541 case elfcpp::R_X86_64_64
:
1542 case elfcpp::R_X86_64_32
:
1543 case elfcpp::R_X86_64_32S
:
1544 case elfcpp::R_X86_64_16
:
1545 case elfcpp::R_X86_64_8
:
1546 return Symbol::ABSOLUTE_REF
;
1548 case elfcpp::R_X86_64_PC64
:
1549 case elfcpp::R_X86_64_PC32
:
1550 case elfcpp::R_X86_64_PC16
:
1551 case elfcpp::R_X86_64_PC8
:
1552 case elfcpp::R_X86_64_GOTOFF64
:
1553 return Symbol::RELATIVE_REF
;
1555 case elfcpp::R_X86_64_PLT32
:
1556 case elfcpp::R_X86_64_PLTOFF64
:
1557 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
1559 case elfcpp::R_X86_64_GOT64
:
1560 case elfcpp::R_X86_64_GOT32
:
1561 case elfcpp::R_X86_64_GOTPCREL64
:
1562 case elfcpp::R_X86_64_GOTPCREL
:
1563 case elfcpp::R_X86_64_GOTPLT64
:
1565 return Symbol::ABSOLUTE_REF
;
1567 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1568 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1569 case elfcpp::R_X86_64_TLSDESC_CALL
:
1570 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1571 case elfcpp::R_X86_64_DTPOFF32
:
1572 case elfcpp::R_X86_64_DTPOFF64
:
1573 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1574 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1575 return Symbol::TLS_REF
;
1577 case elfcpp::R_X86_64_COPY
:
1578 case elfcpp::R_X86_64_GLOB_DAT
:
1579 case elfcpp::R_X86_64_JUMP_SLOT
:
1580 case elfcpp::R_X86_64_RELATIVE
:
1581 case elfcpp::R_X86_64_IRELATIVE
:
1582 case elfcpp::R_X86_64_TPOFF64
:
1583 case elfcpp::R_X86_64_DTPMOD64
:
1584 case elfcpp::R_X86_64_TLSDESC
:
1585 case elfcpp::R_X86_64_SIZE32
:
1586 case elfcpp::R_X86_64_SIZE64
:
1588 // Not expected. We will give an error later.
1593 // Report an unsupported relocation against a local symbol.
1596 Target_x86_64::Scan::unsupported_reloc_local(
1597 Sized_relobj_file
<64, false>* object
,
1598 unsigned int r_type
)
1600 gold_error(_("%s: unsupported reloc %u against local symbol"),
1601 object
->name().c_str(), r_type
);
1604 // We are about to emit a dynamic relocation of type R_TYPE. If the
1605 // dynamic linker does not support it, issue an error. The GNU linker
1606 // only issues a non-PIC error for an allocated read-only section.
1607 // Here we know the section is allocated, but we don't know that it is
1608 // read-only. But we check for all the relocation types which the
1609 // glibc dynamic linker supports, so it seems appropriate to issue an
1610 // error even if the section is not read-only. If GSYM is not NULL,
1611 // it is the symbol the relocation is against; if it is NULL, the
1612 // relocation is against a local symbol.
1615 Target_x86_64::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
1620 // These are the relocation types supported by glibc for x86_64
1621 // which should always work.
1622 case elfcpp::R_X86_64_RELATIVE
:
1623 case elfcpp::R_X86_64_IRELATIVE
:
1624 case elfcpp::R_X86_64_GLOB_DAT
:
1625 case elfcpp::R_X86_64_JUMP_SLOT
:
1626 case elfcpp::R_X86_64_DTPMOD64
:
1627 case elfcpp::R_X86_64_DTPOFF64
:
1628 case elfcpp::R_X86_64_TPOFF64
:
1629 case elfcpp::R_X86_64_64
:
1630 case elfcpp::R_X86_64_COPY
:
1633 // glibc supports these reloc types, but they can overflow.
1634 case elfcpp::R_X86_64_PC32
:
1635 // A PC relative reference is OK against a local symbol or if
1636 // the symbol is defined locally.
1638 || (!gsym
->is_from_dynobj()
1639 && !gsym
->is_undefined()
1640 && !gsym
->is_preemptible()))
1643 case elfcpp::R_X86_64_32
:
1644 if (this->issued_non_pic_error_
)
1646 gold_assert(parameters
->options().output_is_position_independent());
1648 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
1649 "overflow at runtime; recompile with -fPIC"));
1651 object
->error(_("requires dynamic %s reloc against '%s' which may "
1652 "overflow at runtime; recompile with -fPIC"),
1653 (r_type
== elfcpp::R_X86_64_32
1657 this->issued_non_pic_error_
= true;
1661 // This prevents us from issuing more than one error per reloc
1662 // section. But we can still wind up issuing more than one
1663 // error per object file.
1664 if (this->issued_non_pic_error_
)
1666 gold_assert(parameters
->options().output_is_position_independent());
1667 object
->error(_("requires unsupported dynamic reloc %u; "
1668 "recompile with -fPIC"),
1670 this->issued_non_pic_error_
= true;
1673 case elfcpp::R_X86_64_NONE
:
1678 // Return whether we need to make a PLT entry for a relocation of the
1679 // given type against a STT_GNU_IFUNC symbol.
1682 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1683 Sized_relobj_file
<64, false>* object
,
1684 unsigned int r_type
)
1686 int flags
= Scan::get_reference_flags(r_type
);
1687 if (flags
& Symbol::TLS_REF
)
1688 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1689 object
->name().c_str(), r_type
);
1693 // Scan a relocation for a local symbol.
1696 Target_x86_64::Scan::local(Symbol_table
* symtab
,
1698 Target_x86_64
* target
,
1699 Sized_relobj_file
<64, false>* object
,
1700 unsigned int data_shndx
,
1701 Output_section
* output_section
,
1702 const elfcpp::Rela
<64, false>& reloc
,
1703 unsigned int r_type
,
1704 const elfcpp::Sym
<64, false>& lsym
)
1706 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1707 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
1708 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1710 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1711 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
1716 case elfcpp::R_X86_64_NONE
:
1717 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1718 case elfcpp::R_X86_64_GNU_VTENTRY
:
1721 case elfcpp::R_X86_64_64
:
1722 // If building a shared library (or a position-independent
1723 // executable), we need to create a dynamic relocation for this
1724 // location. The relocation applied at link time will apply the
1725 // link-time value, so we flag the location with an
1726 // R_X86_64_RELATIVE relocation so the dynamic loader can
1727 // relocate it easily.
1728 if (parameters
->options().output_is_position_independent())
1730 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1731 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1732 rela_dyn
->add_local_relative(object
, r_sym
,
1733 elfcpp::R_X86_64_RELATIVE
,
1734 output_section
, data_shndx
,
1735 reloc
.get_r_offset(),
1736 reloc
.get_r_addend());
1740 case elfcpp::R_X86_64_32
:
1741 case elfcpp::R_X86_64_32S
:
1742 case elfcpp::R_X86_64_16
:
1743 case elfcpp::R_X86_64_8
:
1744 // If building a shared library (or a position-independent
1745 // executable), we need to create a dynamic relocation for this
1746 // location. We can't use an R_X86_64_RELATIVE relocation
1747 // because that is always a 64-bit relocation.
1748 if (parameters
->options().output_is_position_independent())
1750 this->check_non_pic(object
, r_type
, NULL
);
1752 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1753 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1754 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1755 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1756 data_shndx
, reloc
.get_r_offset(),
1757 reloc
.get_r_addend());
1760 gold_assert(lsym
.get_st_value() == 0);
1761 unsigned int shndx
= lsym
.get_st_shndx();
1763 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1766 object
->error(_("section symbol %u has bad shndx %u"),
1769 rela_dyn
->add_local_section(object
, shndx
,
1770 r_type
, output_section
,
1771 data_shndx
, reloc
.get_r_offset(),
1772 reloc
.get_r_addend());
1777 case elfcpp::R_X86_64_PC64
:
1778 case elfcpp::R_X86_64_PC32
:
1779 case elfcpp::R_X86_64_PC16
:
1780 case elfcpp::R_X86_64_PC8
:
1783 case elfcpp::R_X86_64_PLT32
:
1784 // Since we know this is a local symbol, we can handle this as a
1788 case elfcpp::R_X86_64_GOTPC32
:
1789 case elfcpp::R_X86_64_GOTOFF64
:
1790 case elfcpp::R_X86_64_GOTPC64
:
1791 case elfcpp::R_X86_64_PLTOFF64
:
1792 // We need a GOT section.
1793 target
->got_section(symtab
, layout
);
1794 // For PLTOFF64, we'd normally want a PLT section, but since we
1795 // know this is a local symbol, no PLT is needed.
1798 case elfcpp::R_X86_64_GOT64
:
1799 case elfcpp::R_X86_64_GOT32
:
1800 case elfcpp::R_X86_64_GOTPCREL64
:
1801 case elfcpp::R_X86_64_GOTPCREL
:
1802 case elfcpp::R_X86_64_GOTPLT64
:
1804 // The symbol requires a GOT entry.
1805 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1806 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1808 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1809 // lets function pointers compare correctly with shared
1810 // libraries. Otherwise we would need an IRELATIVE reloc.
1812 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
)
1813 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
1815 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1818 // If we are generating a shared object, we need to add a
1819 // dynamic relocation for this symbol's GOT entry.
1820 if (parameters
->options().output_is_position_independent())
1822 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1823 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1824 if (r_type
!= elfcpp::R_X86_64_GOT32
)
1826 unsigned int got_offset
=
1827 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1828 rela_dyn
->add_local_relative(object
, r_sym
,
1829 elfcpp::R_X86_64_RELATIVE
,
1830 got
, got_offset
, 0);
1834 this->check_non_pic(object
, r_type
, NULL
);
1836 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1837 rela_dyn
->add_local(
1838 object
, r_sym
, r_type
, got
,
1839 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1843 // For GOTPLT64, we'd normally want a PLT section, but since
1844 // we know this is a local symbol, no PLT is needed.
1848 case elfcpp::R_X86_64_COPY
:
1849 case elfcpp::R_X86_64_GLOB_DAT
:
1850 case elfcpp::R_X86_64_JUMP_SLOT
:
1851 case elfcpp::R_X86_64_RELATIVE
:
1852 case elfcpp::R_X86_64_IRELATIVE
:
1853 // These are outstanding tls relocs, which are unexpected when linking
1854 case elfcpp::R_X86_64_TPOFF64
:
1855 case elfcpp::R_X86_64_DTPMOD64
:
1856 case elfcpp::R_X86_64_TLSDESC
:
1857 gold_error(_("%s: unexpected reloc %u in object file"),
1858 object
->name().c_str(), r_type
);
1861 // These are initial tls relocs, which are expected when linking
1862 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1863 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1864 case elfcpp::R_X86_64_TLSDESC_CALL
:
1865 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1866 case elfcpp::R_X86_64_DTPOFF32
:
1867 case elfcpp::R_X86_64_DTPOFF64
:
1868 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1869 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1871 bool output_is_shared
= parameters
->options().shared();
1872 const tls::Tls_optimization optimized_type
1873 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
1876 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1877 if (optimized_type
== tls::TLSOPT_NONE
)
1879 // Create a pair of GOT entries for the module index and
1880 // dtv-relative offset.
1881 Output_data_got
<64, false>* got
1882 = target
->got_section(symtab
, layout
);
1883 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1884 unsigned int shndx
= lsym
.get_st_shndx();
1886 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1888 object
->error(_("local symbol %u has bad shndx %u"),
1891 got
->add_local_pair_with_rela(object
, r_sym
,
1894 target
->rela_dyn_section(layout
),
1895 elfcpp::R_X86_64_DTPMOD64
, 0);
1897 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1898 unsupported_reloc_local(object
, r_type
);
1901 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1902 target
->define_tls_base_symbol(symtab
, layout
);
1903 if (optimized_type
== tls::TLSOPT_NONE
)
1905 // Create reserved PLT and GOT entries for the resolver.
1906 target
->reserve_tlsdesc_entries(symtab
, layout
);
1908 // Generate a double GOT entry with an
1909 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1910 // is resolved lazily, so the GOT entry needs to be in
1911 // an area in .got.plt, not .got. Call got_section to
1912 // make sure the section has been created.
1913 target
->got_section(symtab
, layout
);
1914 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
1915 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1916 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
1918 unsigned int got_offset
= got
->add_constant(0);
1919 got
->add_constant(0);
1920 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
1922 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
1923 // We store the arguments we need in a vector, and
1924 // use the index into the vector as the parameter
1925 // to pass to the target specific routines.
1926 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
1927 void* arg
= reinterpret_cast<void*>(intarg
);
1928 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
1929 got
, got_offset
, 0);
1932 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1933 unsupported_reloc_local(object
, r_type
);
1936 case elfcpp::R_X86_64_TLSDESC_CALL
:
1939 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1940 if (optimized_type
== tls::TLSOPT_NONE
)
1942 // Create a GOT entry for the module index.
1943 target
->got_mod_index_entry(symtab
, layout
, object
);
1945 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1946 unsupported_reloc_local(object
, r_type
);
1949 case elfcpp::R_X86_64_DTPOFF32
:
1950 case elfcpp::R_X86_64_DTPOFF64
:
1953 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1954 layout
->set_has_static_tls();
1955 if (optimized_type
== tls::TLSOPT_NONE
)
1957 // Create a GOT entry for the tp-relative offset.
1958 Output_data_got
<64, false>* got
1959 = target
->got_section(symtab
, layout
);
1960 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1961 got
->add_local_with_rela(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1962 target
->rela_dyn_section(layout
),
1963 elfcpp::R_X86_64_TPOFF64
);
1965 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1966 unsupported_reloc_local(object
, r_type
);
1969 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1970 layout
->set_has_static_tls();
1971 if (output_is_shared
)
1972 unsupported_reloc_local(object
, r_type
);
1981 case elfcpp::R_X86_64_SIZE32
:
1982 case elfcpp::R_X86_64_SIZE64
:
1984 gold_error(_("%s: unsupported reloc %u against local symbol"),
1985 object
->name().c_str(), r_type
);
1991 // Report an unsupported relocation against a global symbol.
1994 Target_x86_64::Scan::unsupported_reloc_global(
1995 Sized_relobj_file
<64, false>* object
,
1996 unsigned int r_type
,
1999 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2000 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
2003 // Returns true if this relocation type could be that of a function pointer.
2005 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type
)
2009 case elfcpp::R_X86_64_64
:
2010 case elfcpp::R_X86_64_32
:
2011 case elfcpp::R_X86_64_32S
:
2012 case elfcpp::R_X86_64_16
:
2013 case elfcpp::R_X86_64_8
:
2014 case elfcpp::R_X86_64_GOT64
:
2015 case elfcpp::R_X86_64_GOT32
:
2016 case elfcpp::R_X86_64_GOTPCREL64
:
2017 case elfcpp::R_X86_64_GOTPCREL
:
2018 case elfcpp::R_X86_64_GOTPLT64
:
2026 // For safe ICF, scan a relocation for a local symbol to check if it
2027 // corresponds to a function pointer being taken. In that case mark
2028 // the function whose pointer was taken as not foldable.
2031 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
2035 Sized_relobj_file
<64, false>* ,
2038 const elfcpp::Rela
<64, false>& ,
2039 unsigned int r_type
,
2040 const elfcpp::Sym
<64, false>&)
2042 // When building a shared library, do not fold any local symbols as it is
2043 // not possible to distinguish pointer taken versus a call by looking at
2044 // the relocation types.
2045 return (parameters
->options().shared()
2046 || possible_function_pointer_reloc(r_type
));
2049 // For safe ICF, scan a relocation for a global symbol to check if it
2050 // corresponds to a function pointer being taken. In that case mark
2051 // the function whose pointer was taken as not foldable.
2054 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2058 Sized_relobj_file
<64, false>* ,
2061 const elfcpp::Rela
<64, false>& ,
2062 unsigned int r_type
,
2065 // When building a shared library, do not fold symbols whose visibility
2066 // is hidden, internal or protected.
2067 return ((parameters
->options().shared()
2068 && (gsym
->visibility() == elfcpp::STV_INTERNAL
2069 || gsym
->visibility() == elfcpp::STV_PROTECTED
2070 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
2071 || possible_function_pointer_reloc(r_type
));
2074 // Scan a relocation for a global symbol.
2077 Target_x86_64::Scan::global(Symbol_table
* symtab
,
2079 Target_x86_64
* target
,
2080 Sized_relobj_file
<64, false>* object
,
2081 unsigned int data_shndx
,
2082 Output_section
* output_section
,
2083 const elfcpp::Rela
<64, false>& reloc
,
2084 unsigned int r_type
,
2087 // A STT_GNU_IFUNC symbol may require a PLT entry.
2088 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2089 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
2090 target
->make_plt_entry(symtab
, layout
, gsym
);
2094 case elfcpp::R_X86_64_NONE
:
2095 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2096 case elfcpp::R_X86_64_GNU_VTENTRY
:
2099 case elfcpp::R_X86_64_64
:
2100 case elfcpp::R_X86_64_32
:
2101 case elfcpp::R_X86_64_32S
:
2102 case elfcpp::R_X86_64_16
:
2103 case elfcpp::R_X86_64_8
:
2105 // Make a PLT entry if necessary.
2106 if (gsym
->needs_plt_entry())
2108 target
->make_plt_entry(symtab
, layout
, gsym
);
2109 // Since this is not a PC-relative relocation, we may be
2110 // taking the address of a function. In that case we need to
2111 // set the entry in the dynamic symbol table to the address of
2113 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
2114 gsym
->set_needs_dynsym_value();
2116 // Make a dynamic relocation if necessary.
2117 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2119 if (gsym
->may_need_copy_reloc())
2121 target
->copy_reloc(symtab
, layout
, object
,
2122 data_shndx
, output_section
, gsym
, reloc
);
2124 else if (r_type
== elfcpp::R_X86_64_64
2125 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2126 && gsym
->can_use_relative_reloc(false)
2127 && !gsym
->is_from_dynobj()
2128 && !gsym
->is_undefined()
2129 && !gsym
->is_preemptible())
2131 // Use an IRELATIVE reloc for a locally defined
2132 // STT_GNU_IFUNC symbol. This makes a function
2133 // address in a PIE executable match the address in a
2134 // shared library that it links against.
2135 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2136 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
2137 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
2138 output_section
, object
,
2140 reloc
.get_r_offset(),
2141 reloc
.get_r_addend());
2143 else if (r_type
== elfcpp::R_X86_64_64
2144 && gsym
->can_use_relative_reloc(false))
2146 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2147 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2148 output_section
, object
,
2150 reloc
.get_r_offset(),
2151 reloc
.get_r_addend());
2155 this->check_non_pic(object
, r_type
, gsym
);
2156 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2157 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2158 data_shndx
, reloc
.get_r_offset(),
2159 reloc
.get_r_addend());
2165 case elfcpp::R_X86_64_PC64
:
2166 case elfcpp::R_X86_64_PC32
:
2167 case elfcpp::R_X86_64_PC16
:
2168 case elfcpp::R_X86_64_PC8
:
2170 // Make a PLT entry if necessary.
2171 if (gsym
->needs_plt_entry())
2172 target
->make_plt_entry(symtab
, layout
, gsym
);
2173 // Make a dynamic relocation if necessary.
2174 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2176 if (gsym
->may_need_copy_reloc())
2178 target
->copy_reloc(symtab
, layout
, object
,
2179 data_shndx
, output_section
, gsym
, reloc
);
2183 this->check_non_pic(object
, r_type
, gsym
);
2184 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2185 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2186 data_shndx
, reloc
.get_r_offset(),
2187 reloc
.get_r_addend());
2193 case elfcpp::R_X86_64_GOT64
:
2194 case elfcpp::R_X86_64_GOT32
:
2195 case elfcpp::R_X86_64_GOTPCREL64
:
2196 case elfcpp::R_X86_64_GOTPCREL
:
2197 case elfcpp::R_X86_64_GOTPLT64
:
2199 // The symbol requires a GOT entry.
2200 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2201 if (gsym
->final_value_is_known())
2203 // For a STT_GNU_IFUNC symbol we want the PLT address.
2204 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
2205 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2207 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2211 // If this symbol is not fully resolved, we need to add a
2212 // dynamic relocation for it.
2213 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2214 if (gsym
->is_from_dynobj()
2215 || gsym
->is_undefined()
2216 || gsym
->is_preemptible()
2217 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
2218 && parameters
->options().output_is_position_independent()))
2219 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2220 elfcpp::R_X86_64_GLOB_DAT
);
2223 // For a STT_GNU_IFUNC symbol we want to write the PLT
2224 // offset into the GOT, so that function pointer
2225 // comparisons work correctly.
2227 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
2228 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2231 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2232 // Tell the dynamic linker to use the PLT address
2233 // when resolving relocations.
2234 if (gsym
->is_from_dynobj()
2235 && !parameters
->options().shared())
2236 gsym
->set_needs_dynsym_value();
2240 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2241 rela_dyn
->add_global_relative(gsym
,
2242 elfcpp::R_X86_64_RELATIVE
,
2247 // For GOTPLT64, we also need a PLT entry (but only if the
2248 // symbol is not fully resolved).
2249 if (r_type
== elfcpp::R_X86_64_GOTPLT64
2250 && !gsym
->final_value_is_known())
2251 target
->make_plt_entry(symtab
, layout
, gsym
);
2255 case elfcpp::R_X86_64_PLT32
:
2256 // If the symbol is fully resolved, this is just a PC32 reloc.
2257 // Otherwise we need a PLT entry.
2258 if (gsym
->final_value_is_known())
2260 // If building a shared library, we can also skip the PLT entry
2261 // if the symbol is defined in the output file and is protected
2263 if (gsym
->is_defined()
2264 && !gsym
->is_from_dynobj()
2265 && !gsym
->is_preemptible())
2267 target
->make_plt_entry(symtab
, layout
, gsym
);
2270 case elfcpp::R_X86_64_GOTPC32
:
2271 case elfcpp::R_X86_64_GOTOFF64
:
2272 case elfcpp::R_X86_64_GOTPC64
:
2273 case elfcpp::R_X86_64_PLTOFF64
:
2274 // We need a GOT section.
2275 target
->got_section(symtab
, layout
);
2276 // For PLTOFF64, we also need a PLT entry (but only if the
2277 // symbol is not fully resolved).
2278 if (r_type
== elfcpp::R_X86_64_PLTOFF64
2279 && !gsym
->final_value_is_known())
2280 target
->make_plt_entry(symtab
, layout
, gsym
);
2283 case elfcpp::R_X86_64_COPY
:
2284 case elfcpp::R_X86_64_GLOB_DAT
:
2285 case elfcpp::R_X86_64_JUMP_SLOT
:
2286 case elfcpp::R_X86_64_RELATIVE
:
2287 case elfcpp::R_X86_64_IRELATIVE
:
2288 // These are outstanding tls relocs, which are unexpected when linking
2289 case elfcpp::R_X86_64_TPOFF64
:
2290 case elfcpp::R_X86_64_DTPMOD64
:
2291 case elfcpp::R_X86_64_TLSDESC
:
2292 gold_error(_("%s: unexpected reloc %u in object file"),
2293 object
->name().c_str(), r_type
);
2296 // These are initial tls relocs, which are expected for global()
2297 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2298 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2299 case elfcpp::R_X86_64_TLSDESC_CALL
:
2300 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2301 case elfcpp::R_X86_64_DTPOFF32
:
2302 case elfcpp::R_X86_64_DTPOFF64
:
2303 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2304 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2306 const bool is_final
= gsym
->final_value_is_known();
2307 const tls::Tls_optimization optimized_type
2308 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
2311 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2312 if (optimized_type
== tls::TLSOPT_NONE
)
2314 // Create a pair of GOT entries for the module index and
2315 // dtv-relative offset.
2316 Output_data_got
<64, false>* got
2317 = target
->got_section(symtab
, layout
);
2318 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_PAIR
,
2319 target
->rela_dyn_section(layout
),
2320 elfcpp::R_X86_64_DTPMOD64
,
2321 elfcpp::R_X86_64_DTPOFF64
);
2323 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2325 // Create a GOT entry for the tp-relative offset.
2326 Output_data_got
<64, false>* got
2327 = target
->got_section(symtab
, layout
);
2328 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
2329 target
->rela_dyn_section(layout
),
2330 elfcpp::R_X86_64_TPOFF64
);
2332 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2333 unsupported_reloc_global(object
, r_type
, gsym
);
2336 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2337 target
->define_tls_base_symbol(symtab
, layout
);
2338 if (optimized_type
== tls::TLSOPT_NONE
)
2340 // Create reserved PLT and GOT entries for the resolver.
2341 target
->reserve_tlsdesc_entries(symtab
, layout
);
2343 // Create a double GOT entry with an R_X86_64_TLSDESC
2344 // reloc. The R_X86_64_TLSDESC reloc is resolved
2345 // lazily, so the GOT entry needs to be in an area in
2346 // .got.plt, not .got. Call got_section to make sure
2347 // the section has been created.
2348 target
->got_section(symtab
, layout
);
2349 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2350 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2351 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_DESC
, rt
,
2352 elfcpp::R_X86_64_TLSDESC
, 0);
2354 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2356 // Create a GOT entry for the tp-relative offset.
2357 Output_data_got
<64, false>* got
2358 = target
->got_section(symtab
, layout
);
2359 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
2360 target
->rela_dyn_section(layout
),
2361 elfcpp::R_X86_64_TPOFF64
);
2363 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2364 unsupported_reloc_global(object
, r_type
, gsym
);
2367 case elfcpp::R_X86_64_TLSDESC_CALL
:
2370 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2371 if (optimized_type
== tls::TLSOPT_NONE
)
2373 // Create a GOT entry for the module index.
2374 target
->got_mod_index_entry(symtab
, layout
, object
);
2376 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2377 unsupported_reloc_global(object
, r_type
, gsym
);
2380 case elfcpp::R_X86_64_DTPOFF32
:
2381 case elfcpp::R_X86_64_DTPOFF64
:
2384 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2385 layout
->set_has_static_tls();
2386 if (optimized_type
== tls::TLSOPT_NONE
)
2388 // Create a GOT entry for the tp-relative offset.
2389 Output_data_got
<64, false>* got
2390 = target
->got_section(symtab
, layout
);
2391 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
2392 target
->rela_dyn_section(layout
),
2393 elfcpp::R_X86_64_TPOFF64
);
2395 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2396 unsupported_reloc_global(object
, r_type
, gsym
);
2399 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2400 layout
->set_has_static_tls();
2401 if (parameters
->options().shared())
2402 unsupported_reloc_local(object
, r_type
);
2411 case elfcpp::R_X86_64_SIZE32
:
2412 case elfcpp::R_X86_64_SIZE64
:
2414 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2415 object
->name().c_str(), r_type
,
2416 gsym
->demangled_name().c_str());
2422 Target_x86_64::gc_process_relocs(Symbol_table
* symtab
,
2424 Sized_relobj_file
<64, false>* object
,
2425 unsigned int data_shndx
,
2426 unsigned int sh_type
,
2427 const unsigned char* prelocs
,
2429 Output_section
* output_section
,
2430 bool needs_special_offset_handling
,
2431 size_t local_symbol_count
,
2432 const unsigned char* plocal_symbols
)
2435 if (sh_type
== elfcpp::SHT_REL
)
2440 gold::gc_process_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2441 Target_x86_64::Scan
,
2442 Target_x86_64::Relocatable_size_for_reloc
>(
2451 needs_special_offset_handling
,
2456 // Scan relocations for a section.
2459 Target_x86_64::scan_relocs(Symbol_table
* symtab
,
2461 Sized_relobj_file
<64, false>* object
,
2462 unsigned int data_shndx
,
2463 unsigned int sh_type
,
2464 const unsigned char* prelocs
,
2466 Output_section
* output_section
,
2467 bool needs_special_offset_handling
,
2468 size_t local_symbol_count
,
2469 const unsigned char* plocal_symbols
)
2471 if (sh_type
== elfcpp::SHT_REL
)
2473 gold_error(_("%s: unsupported REL reloc section"),
2474 object
->name().c_str());
2478 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2479 Target_x86_64::Scan
>(
2488 needs_special_offset_handling
,
2493 // Finalize the sections.
2496 Target_x86_64::do_finalize_sections(
2498 const Input_objects
*,
2499 Symbol_table
* symtab
)
2501 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2503 : this->plt_
->rela_plt());
2504 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
2505 this->rela_dyn_
, true, false);
2507 // Fill in some more dynamic tags.
2508 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
2511 if (this->plt_
!= NULL
2512 && this->plt_
->output_section() != NULL
2513 && this->plt_
->has_tlsdesc_entry())
2515 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
2516 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
2517 this->got_
->finalize_data_size();
2518 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
2519 this->plt_
, plt_offset
);
2520 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
2521 this->got_
, got_offset
);
2525 // Emit any relocs we saved in an attempt to avoid generating COPY
2527 if (this->copy_relocs_
.any_saved_relocs())
2528 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
2530 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2531 // the .got.plt section.
2532 Symbol
* sym
= this->global_offset_table_
;
2535 uint64_t data_size
= this->got_plt_
->current_data_size();
2536 symtab
->get_sized_symbol
<64>(sym
)->set_symsize(data_size
);
2540 // Perform a relocation.
2543 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
2544 Target_x86_64
* target
,
2547 const elfcpp::Rela
<64, false>& rela
,
2548 unsigned int r_type
,
2549 const Sized_symbol
<64>* gsym
,
2550 const Symbol_value
<64>* psymval
,
2551 unsigned char* view
,
2552 elfcpp::Elf_types
<64>::Elf_Addr address
,
2553 section_size_type view_size
)
2555 if (this->skip_call_tls_get_addr_
)
2557 if ((r_type
!= elfcpp::R_X86_64_PLT32
2558 && r_type
!= elfcpp::R_X86_64_PC32
)
2560 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
2562 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2563 _("missing expected TLS relocation"));
2567 this->skip_call_tls_get_addr_
= false;
2572 const Sized_relobj_file
<64, false>* object
= relinfo
->object
;
2574 // Pick the value to use for symbols defined in the PLT.
2575 Symbol_value
<64> symval
;
2577 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
2579 symval
.set_output_value(target
->plt_section()->address()
2580 + gsym
->plt_offset());
2583 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
2585 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2586 if (object
->local_has_plt_offset(r_sym
))
2588 symval
.set_output_value(target
->plt_section()->address()
2589 + object
->local_plt_offset(r_sym
));
2594 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2596 // Get the GOT offset if needed.
2597 // The GOT pointer points to the end of the GOT section.
2598 // We need to subtract the size of the GOT section to get
2599 // the actual offset to use in the relocation.
2600 bool have_got_offset
= false;
2601 unsigned int got_offset
= 0;
2604 case elfcpp::R_X86_64_GOT32
:
2605 case elfcpp::R_X86_64_GOT64
:
2606 case elfcpp::R_X86_64_GOTPLT64
:
2607 case elfcpp::R_X86_64_GOTPCREL
:
2608 case elfcpp::R_X86_64_GOTPCREL64
:
2611 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2612 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
2616 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2617 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2618 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
2619 - target
->got_size());
2621 have_got_offset
= true;
2630 case elfcpp::R_X86_64_NONE
:
2631 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2632 case elfcpp::R_X86_64_GNU_VTENTRY
:
2635 case elfcpp::R_X86_64_64
:
2636 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
2639 case elfcpp::R_X86_64_PC64
:
2640 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
2644 case elfcpp::R_X86_64_32
:
2645 // FIXME: we need to verify that value + addend fits into 32 bits:
2646 // uint64_t x = value + addend;
2647 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2648 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2649 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
2652 case elfcpp::R_X86_64_32S
:
2653 // FIXME: we need to verify that value + addend fits into 32 bits:
2654 // int64_t x = value + addend; // note this quantity is signed!
2655 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2656 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
2659 case elfcpp::R_X86_64_PC32
:
2660 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
2664 case elfcpp::R_X86_64_16
:
2665 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
2668 case elfcpp::R_X86_64_PC16
:
2669 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
2673 case elfcpp::R_X86_64_8
:
2674 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
2677 case elfcpp::R_X86_64_PC8
:
2678 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
2682 case elfcpp::R_X86_64_PLT32
:
2683 gold_assert(gsym
== NULL
2684 || gsym
->has_plt_offset()
2685 || gsym
->final_value_is_known()
2686 || (gsym
->is_defined()
2687 && !gsym
->is_from_dynobj()
2688 && !gsym
->is_preemptible()));
2689 // Note: while this code looks the same as for R_X86_64_PC32, it
2690 // behaves differently because psymval was set to point to
2691 // the PLT entry, rather than the symbol, in Scan::global().
2692 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
2696 case elfcpp::R_X86_64_PLTOFF64
:
2699 gold_assert(gsym
->has_plt_offset()
2700 || gsym
->final_value_is_known());
2701 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
2702 got_address
= target
->got_section(NULL
, NULL
)->address();
2703 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
2704 addend
- got_address
);
2707 case elfcpp::R_X86_64_GOT32
:
2708 gold_assert(have_got_offset
);
2709 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
2712 case elfcpp::R_X86_64_GOTPC32
:
2715 elfcpp::Elf_types
<64>::Elf_Addr value
;
2716 value
= target
->got_plt_section()->address();
2717 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2721 case elfcpp::R_X86_64_GOT64
:
2722 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2723 // Since we always add a PLT entry, this is equivalent.
2724 case elfcpp::R_X86_64_GOTPLT64
:
2725 gold_assert(have_got_offset
);
2726 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
2729 case elfcpp::R_X86_64_GOTPC64
:
2732 elfcpp::Elf_types
<64>::Elf_Addr value
;
2733 value
= target
->got_plt_section()->address();
2734 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
2738 case elfcpp::R_X86_64_GOTOFF64
:
2740 elfcpp::Elf_types
<64>::Elf_Addr value
;
2741 value
= (psymval
->value(object
, 0)
2742 - target
->got_plt_section()->address());
2743 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
2747 case elfcpp::R_X86_64_GOTPCREL
:
2749 gold_assert(have_got_offset
);
2750 elfcpp::Elf_types
<64>::Elf_Addr value
;
2751 value
= target
->got_plt_section()->address() + got_offset
;
2752 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2756 case elfcpp::R_X86_64_GOTPCREL64
:
2758 gold_assert(have_got_offset
);
2759 elfcpp::Elf_types
<64>::Elf_Addr value
;
2760 value
= target
->got_plt_section()->address() + got_offset
;
2761 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
2765 case elfcpp::R_X86_64_COPY
:
2766 case elfcpp::R_X86_64_GLOB_DAT
:
2767 case elfcpp::R_X86_64_JUMP_SLOT
:
2768 case elfcpp::R_X86_64_RELATIVE
:
2769 case elfcpp::R_X86_64_IRELATIVE
:
2770 // These are outstanding tls relocs, which are unexpected when linking
2771 case elfcpp::R_X86_64_TPOFF64
:
2772 case elfcpp::R_X86_64_DTPMOD64
:
2773 case elfcpp::R_X86_64_TLSDESC
:
2774 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2775 _("unexpected reloc %u in object file"),
2779 // These are initial tls relocs, which are expected when linking
2780 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2781 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2782 case elfcpp::R_X86_64_TLSDESC_CALL
:
2783 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2784 case elfcpp::R_X86_64_DTPOFF32
:
2785 case elfcpp::R_X86_64_DTPOFF64
:
2786 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2787 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2788 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
2789 view
, address
, view_size
);
2792 case elfcpp::R_X86_64_SIZE32
:
2793 case elfcpp::R_X86_64_SIZE64
:
2795 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2796 _("unsupported reloc %u"),
2804 // Perform a TLS relocation.
2807 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
2808 Target_x86_64
* target
,
2810 const elfcpp::Rela
<64, false>& rela
,
2811 unsigned int r_type
,
2812 const Sized_symbol
<64>* gsym
,
2813 const Symbol_value
<64>* psymval
,
2814 unsigned char* view
,
2815 elfcpp::Elf_types
<64>::Elf_Addr address
,
2816 section_size_type view_size
)
2818 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2820 const Sized_relobj_file
<64, false>* object
= relinfo
->object
;
2821 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2822 elfcpp::Shdr
<64, false> data_shdr(relinfo
->data_shdr
);
2823 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
2825 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
2827 const bool is_final
= (gsym
== NULL
2828 ? !parameters
->options().shared()
2829 : gsym
->final_value_is_known());
2830 tls::Tls_optimization optimized_type
2831 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
2834 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2835 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2837 // If this code sequence is used in a non-executable section,
2838 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2839 // on the assumption that it's being used by itself in a debug
2840 // section. Therefore, in the unlikely event that the code
2841 // sequence appears in a non-executable section, we simply
2842 // leave it unoptimized.
2843 optimized_type
= tls::TLSOPT_NONE
;
2845 if (optimized_type
== tls::TLSOPT_TO_LE
)
2847 gold_assert(tls_segment
!= NULL
);
2848 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
2849 rela
, r_type
, value
, view
,
2855 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2856 ? GOT_TYPE_TLS_OFFSET
2857 : GOT_TYPE_TLS_PAIR
);
2858 unsigned int got_offset
;
2861 gold_assert(gsym
->has_got_offset(got_type
));
2862 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2866 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2867 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2868 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2869 - target
->got_size());
2871 if (optimized_type
== tls::TLSOPT_TO_IE
)
2873 gold_assert(tls_segment
!= NULL
);
2874 value
= target
->got_plt_section()->address() + got_offset
;
2875 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2876 value
, view
, address
, view_size
);
2879 else if (optimized_type
== tls::TLSOPT_NONE
)
2881 // Relocate the field with the offset of the pair of GOT
2883 value
= target
->got_plt_section()->address() + got_offset
;
2884 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2889 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2890 _("unsupported reloc %u"), r_type
);
2893 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2894 case elfcpp::R_X86_64_TLSDESC_CALL
:
2895 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2897 // See above comment for R_X86_64_TLSGD.
2898 optimized_type
= tls::TLSOPT_NONE
;
2900 if (optimized_type
== tls::TLSOPT_TO_LE
)
2902 gold_assert(tls_segment
!= NULL
);
2903 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2904 rela
, r_type
, value
, view
,
2910 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2911 ? GOT_TYPE_TLS_OFFSET
2912 : GOT_TYPE_TLS_DESC
);
2913 unsigned int got_offset
= 0;
2914 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
2915 && optimized_type
== tls::TLSOPT_NONE
)
2917 // We created GOT entries in the .got.tlsdesc portion of
2918 // the .got.plt section, but the offset stored in the
2919 // symbol is the offset within .got.tlsdesc.
2920 got_offset
= (target
->got_size()
2921 + target
->got_plt_section()->data_size());
2925 gold_assert(gsym
->has_got_offset(got_type
));
2926 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
2930 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2931 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2932 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
2933 - target
->got_size());
2935 if (optimized_type
== tls::TLSOPT_TO_IE
)
2937 gold_assert(tls_segment
!= NULL
);
2938 value
= target
->got_plt_section()->address() + got_offset
;
2939 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
2940 rela
, r_type
, value
, view
, address
,
2944 else if (optimized_type
== tls::TLSOPT_NONE
)
2946 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2948 // Relocate the field with the offset of the pair of GOT
2950 value
= target
->got_plt_section()->address() + got_offset
;
2951 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2957 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2958 _("unsupported reloc %u"), r_type
);
2961 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2962 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2964 // See above comment for R_X86_64_TLSGD.
2965 optimized_type
= tls::TLSOPT_NONE
;
2967 if (optimized_type
== tls::TLSOPT_TO_LE
)
2969 gold_assert(tls_segment
!= NULL
);
2970 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2971 value
, view
, view_size
);
2974 else if (optimized_type
== tls::TLSOPT_NONE
)
2976 // Relocate the field with the offset of the GOT entry for
2977 // the module index.
2978 unsigned int got_offset
;
2979 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2980 - target
->got_size());
2981 value
= target
->got_plt_section()->address() + got_offset
;
2982 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2986 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2987 _("unsupported reloc %u"), r_type
);
2990 case elfcpp::R_X86_64_DTPOFF32
:
2991 // This relocation type is used in debugging information.
2992 // In that case we need to not optimize the value. If the
2993 // section is not executable, then we assume we should not
2994 // optimize this reloc. See comments above for R_X86_64_TLSGD,
2995 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
2997 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
2999 gold_assert(tls_segment
!= NULL
);
3000 value
-= tls_segment
->memsz();
3002 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
3005 case elfcpp::R_X86_64_DTPOFF64
:
3006 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3007 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3009 gold_assert(tls_segment
!= NULL
);
3010 value
-= tls_segment
->memsz();
3012 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
3015 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3016 if (optimized_type
== tls::TLSOPT_TO_LE
)
3018 gold_assert(tls_segment
!= NULL
);
3019 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
3020 rela
, r_type
, value
, view
,
3024 else if (optimized_type
== tls::TLSOPT_NONE
)
3026 // Relocate the field with the offset of the GOT entry for
3027 // the tp-relative offset of the symbol.
3028 unsigned int got_offset
;
3031 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
3032 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
3033 - target
->got_size());
3037 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
3038 gold_assert(object
->local_has_got_offset(r_sym
,
3039 GOT_TYPE_TLS_OFFSET
));
3040 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
3041 - target
->got_size());
3043 value
= target
->got_plt_section()->address() + got_offset
;
3044 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
3047 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3048 _("unsupported reloc type %u"),
3052 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3053 value
-= tls_segment
->memsz();
3054 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
3059 // Do a relocation in which we convert a TLS General-Dynamic to an
3063 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
3066 const elfcpp::Rela
<64, false>& rela
,
3068 elfcpp::Elf_types
<64>::Elf_Addr value
,
3069 unsigned char* view
,
3070 elfcpp::Elf_types
<64>::Elf_Addr address
,
3071 section_size_type view_size
)
3073 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3074 // .word 0x6666; rex64; call __tls_get_addr
3075 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3077 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
3078 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3080 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3081 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3082 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3083 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3085 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3087 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3088 Relocate_functions
<64, false>::pcrela32(view
+ 8, value
, addend
- 8, address
);
3090 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3092 this->skip_call_tls_get_addr_
= true;
3095 // Do a relocation in which we convert a TLS General-Dynamic to a
3099 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
3101 Output_segment
* tls_segment
,
3102 const elfcpp::Rela
<64, false>& rela
,
3104 elfcpp::Elf_types
<64>::Elf_Addr value
,
3105 unsigned char* view
,
3106 section_size_type view_size
)
3108 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3109 // .word 0x6666; rex64; call __tls_get_addr
3110 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3112 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
3113 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3115 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3116 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3117 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3118 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3120 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
3122 value
-= tls_segment
->memsz();
3123 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
3125 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3127 this->skip_call_tls_get_addr_
= true;
3130 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3133 Target_x86_64::Relocate::tls_desc_gd_to_ie(
3134 const Relocate_info
<64, false>* relinfo
,
3137 const elfcpp::Rela
<64, false>& rela
,
3138 unsigned int r_type
,
3139 elfcpp::Elf_types
<64>::Elf_Addr value
,
3140 unsigned char* view
,
3141 elfcpp::Elf_types
<64>::Elf_Addr address
,
3142 section_size_type view_size
)
3144 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3146 // leaq foo@tlsdesc(%rip), %rax
3147 // ==> movq foo@gottpoff(%rip), %rax
3148 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3149 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3150 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3151 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
3153 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3154 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
3158 // call *foo@tlscall(%rax)
3160 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
3161 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
3162 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3163 view
[0] == 0xff && view
[1] == 0x10);
3169 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3172 Target_x86_64::Relocate::tls_desc_gd_to_le(
3173 const Relocate_info
<64, false>* relinfo
,
3175 Output_segment
* tls_segment
,
3176 const elfcpp::Rela
<64, false>& rela
,
3177 unsigned int r_type
,
3178 elfcpp::Elf_types
<64>::Elf_Addr value
,
3179 unsigned char* view
,
3180 section_size_type view_size
)
3182 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3184 // leaq foo@tlsdesc(%rip), %rax
3185 // ==> movq foo@tpoff, %rax
3186 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3187 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3188 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3189 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
3192 value
-= tls_segment
->memsz();
3193 Relocate_functions
<64, false>::rela32(view
, value
, 0);
3197 // call *foo@tlscall(%rax)
3199 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
3200 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
3201 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3202 view
[0] == 0xff && view
[1] == 0x10);
3209 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
3212 const elfcpp::Rela
<64, false>& rela
,
3214 elfcpp::Elf_types
<64>::Elf_Addr
,
3215 unsigned char* view
,
3216 section_size_type view_size
)
3218 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3219 // ... leq foo@dtpoff(%rax),%reg
3220 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3222 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3223 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
3225 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3226 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
3228 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
3230 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3232 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3234 this->skip_call_tls_get_addr_
= true;
3237 // Do a relocation in which we convert a TLS Initial-Exec to a
3241 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
3243 Output_segment
* tls_segment
,
3244 const elfcpp::Rela
<64, false>& rela
,
3246 elfcpp::Elf_types
<64>::Elf_Addr value
,
3247 unsigned char* view
,
3248 section_size_type view_size
)
3250 // We need to examine the opcodes to figure out which instruction we
3253 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3254 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3256 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3257 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3259 unsigned char op1
= view
[-3];
3260 unsigned char op2
= view
[-2];
3261 unsigned char op3
= view
[-1];
3262 unsigned char reg
= op3
>> 3;
3270 view
[-1] = 0xc0 | reg
;
3274 // Special handling for %rsp.
3278 view
[-1] = 0xc0 | reg
;
3286 view
[-1] = 0x80 | reg
| (reg
<< 3);
3289 value
-= tls_segment
->memsz();
3290 Relocate_functions
<64, false>::rela32(view
, value
, 0);
3293 // Relocate section data.
3296 Target_x86_64::relocate_section(
3297 const Relocate_info
<64, false>* relinfo
,
3298 unsigned int sh_type
,
3299 const unsigned char* prelocs
,
3301 Output_section
* output_section
,
3302 bool needs_special_offset_handling
,
3303 unsigned char* view
,
3304 elfcpp::Elf_types
<64>::Elf_Addr address
,
3305 section_size_type view_size
,
3306 const Reloc_symbol_changes
* reloc_symbol_changes
)
3308 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3310 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
3311 Target_x86_64::Relocate
>(
3317 needs_special_offset_handling
,
3321 reloc_symbol_changes
);
3324 // Apply an incremental relocation. Incremental relocations always refer
3325 // to global symbols.
3328 Target_x86_64::apply_relocation(
3329 const Relocate_info
<64, false>* relinfo
,
3330 elfcpp::Elf_types
<64>::Elf_Addr r_offset
,
3331 unsigned int r_type
,
3332 elfcpp::Elf_types
<64>::Elf_Swxword r_addend
,
3334 unsigned char* view
,
3335 elfcpp::Elf_types
<64>::Elf_Addr address
,
3336 section_size_type view_size
)
3338 gold::apply_relocation
<64, false, Target_x86_64
, Target_x86_64::Relocate
>(
3350 // Return the size of a relocation while scanning during a relocatable
3354 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3355 unsigned int r_type
,
3360 case elfcpp::R_X86_64_NONE
:
3361 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3362 case elfcpp::R_X86_64_GNU_VTENTRY
:
3363 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3364 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3365 case elfcpp::R_X86_64_TLSDESC_CALL
:
3366 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3367 case elfcpp::R_X86_64_DTPOFF32
:
3368 case elfcpp::R_X86_64_DTPOFF64
:
3369 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3370 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3373 case elfcpp::R_X86_64_64
:
3374 case elfcpp::R_X86_64_PC64
:
3375 case elfcpp::R_X86_64_GOTOFF64
:
3376 case elfcpp::R_X86_64_GOTPC64
:
3377 case elfcpp::R_X86_64_PLTOFF64
:
3378 case elfcpp::R_X86_64_GOT64
:
3379 case elfcpp::R_X86_64_GOTPCREL64
:
3380 case elfcpp::R_X86_64_GOTPCREL
:
3381 case elfcpp::R_X86_64_GOTPLT64
:
3384 case elfcpp::R_X86_64_32
:
3385 case elfcpp::R_X86_64_32S
:
3386 case elfcpp::R_X86_64_PC32
:
3387 case elfcpp::R_X86_64_PLT32
:
3388 case elfcpp::R_X86_64_GOTPC32
:
3389 case elfcpp::R_X86_64_GOT32
:
3392 case elfcpp::R_X86_64_16
:
3393 case elfcpp::R_X86_64_PC16
:
3396 case elfcpp::R_X86_64_8
:
3397 case elfcpp::R_X86_64_PC8
:
3400 case elfcpp::R_X86_64_COPY
:
3401 case elfcpp::R_X86_64_GLOB_DAT
:
3402 case elfcpp::R_X86_64_JUMP_SLOT
:
3403 case elfcpp::R_X86_64_RELATIVE
:
3404 case elfcpp::R_X86_64_IRELATIVE
:
3405 // These are outstanding tls relocs, which are unexpected when linking
3406 case elfcpp::R_X86_64_TPOFF64
:
3407 case elfcpp::R_X86_64_DTPMOD64
:
3408 case elfcpp::R_X86_64_TLSDESC
:
3409 object
->error(_("unexpected reloc %u in object file"), r_type
);
3412 case elfcpp::R_X86_64_SIZE32
:
3413 case elfcpp::R_X86_64_SIZE64
:
3415 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
3420 // Scan the relocs during a relocatable link.
3423 Target_x86_64::scan_relocatable_relocs(Symbol_table
* symtab
,
3425 Sized_relobj_file
<64, false>* object
,
3426 unsigned int data_shndx
,
3427 unsigned int sh_type
,
3428 const unsigned char* prelocs
,
3430 Output_section
* output_section
,
3431 bool needs_special_offset_handling
,
3432 size_t local_symbol_count
,
3433 const unsigned char* plocal_symbols
,
3434 Relocatable_relocs
* rr
)
3436 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3438 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
3439 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
3441 gold::scan_relocatable_relocs
<64, false, elfcpp::SHT_RELA
,
3442 Scan_relocatable_relocs
>(
3450 needs_special_offset_handling
,
3456 // Relocate a section during a relocatable link.
3459 Target_x86_64::relocate_for_relocatable(
3460 const Relocate_info
<64, false>* relinfo
,
3461 unsigned int sh_type
,
3462 const unsigned char* prelocs
,
3464 Output_section
* output_section
,
3465 off_t offset_in_output_section
,
3466 const Relocatable_relocs
* rr
,
3467 unsigned char* view
,
3468 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
3469 section_size_type view_size
,
3470 unsigned char* reloc_view
,
3471 section_size_type reloc_view_size
)
3473 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3475 gold::relocate_for_relocatable
<64, false, elfcpp::SHT_RELA
>(
3480 offset_in_output_section
,
3489 // Return the value to use for a dynamic which requires special
3490 // treatment. This is how we support equality comparisons of function
3491 // pointers across shared library boundaries, as described in the
3492 // processor specific ABI supplement.
3495 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
3497 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
3498 return this->plt_section()->address() + gsym
->plt_offset();
3501 // Return a string used to fill a code section with nops to take up
3502 // the specified length.
3505 Target_x86_64::do_code_fill(section_size_type length
) const
3509 // Build a jmpq instruction to skip over the bytes.
3510 unsigned char jmp
[5];
3512 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
3513 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
3514 + std::string(length
- 5, '\0'));
3517 // Nop sequences of various lengths.
3518 const char nop1
[1] = { 0x90 }; // nop
3519 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
3520 const char nop3
[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3521 const char nop4
[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3522 const char nop5
[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3524 const char nop6
[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3526 const char nop7
[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3528 const char nop8
[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3529 0x00, 0x00, 0x00, 0x00 };
3530 const char nop9
[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3531 0x00, 0x00, 0x00, 0x00,
3533 const char nop10
[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3534 0x84, 0x00, 0x00, 0x00,
3536 const char nop11
[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3537 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3539 const char nop12
[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3540 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3541 0x00, 0x00, 0x00, 0x00 };
3542 const char nop13
[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3543 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3544 0x00, 0x00, 0x00, 0x00,
3546 const char nop14
[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3547 0x66, 0x2e, 0x0f, 0x1f, // data16
3548 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3550 const char nop15
[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3551 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3552 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3555 const char* nops
[16] = {
3557 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
3558 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
3561 return std::string(nops
[length
], length
);
3564 // Return the addend to use for a target specific relocation. The
3565 // only target specific relocation is R_X86_64_TLSDESC for a local
3566 // symbol. We want to set the addend is the offset of the local
3567 // symbol in the TLS segment.
3570 Target_x86_64::do_reloc_addend(void* arg
, unsigned int r_type
,
3573 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
3574 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
3575 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
3576 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
3577 const Symbol_value
<64>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
3578 gold_assert(psymval
->is_tls_symbol());
3579 // The value of a TLS symbol is the offset in the TLS segment.
3580 return psymval
->value(ti
.object
, 0);
3583 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3584 // compiled with -fsplit-stack. The function calls non-split-stack
3585 // code. We have to change the function so that it always ensures
3586 // that it has enough stack space to run some random function.
3589 Target_x86_64::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
3590 section_offset_type fnoffset
,
3591 section_size_type fnsize
,
3592 unsigned char* view
,
3593 section_size_type view_size
,
3595 std::string
* to
) const
3597 // The function starts with a comparison of the stack pointer and a
3598 // field in the TCB. This is followed by a jump.
3601 if (this->match_view(view
, view_size
, fnoffset
, "\x64\x48\x3b\x24\x25", 5)
3604 // We will call __morestack if the carry flag is set after this
3605 // comparison. We turn the comparison into an stc instruction
3607 view
[fnoffset
] = '\xf9';
3608 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 8);
3610 // lea NN(%rsp),%r10
3611 // lea NN(%rsp),%r11
3612 else if ((this->match_view(view
, view_size
, fnoffset
,
3613 "\x4c\x8d\x94\x24", 4)
3614 || this->match_view(view
, view_size
, fnoffset
,
3615 "\x4c\x8d\x9c\x24", 4))
3618 // This is loading an offset from the stack pointer for a
3619 // comparison. The offset is negative, so we decrease the
3620 // offset by the amount of space we need for the stack. This
3621 // means we will avoid calling __morestack if there happens to
3622 // be plenty of space on the stack already.
3623 unsigned char* pval
= view
+ fnoffset
+ 4;
3624 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
3625 val
-= parameters
->options().split_stack_adjust_size();
3626 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
3630 if (!object
->has_no_split_stack())
3631 object
->error(_("failed to match split-stack sequence at "
3632 "section %u offset %0zx"),
3633 shndx
, static_cast<size_t>(fnoffset
));
3637 // We have to change the function so that it calls
3638 // __morestack_non_split instead of __morestack. The former will
3639 // allocate additional stack space.
3640 *from
= "__morestack";
3641 *to
= "__morestack_non_split";
3644 // The selector for x86_64 object files.
3646 class Target_selector_x86_64
: public Target_selector_freebsd
3649 Target_selector_x86_64()
3650 : Target_selector_freebsd(elfcpp::EM_X86_64
, 64, false, "elf64-x86-64",
3651 "elf64-x86-64-freebsd", "elf_x86_64")
3655 do_instantiate_target()
3656 { return new Target_x86_64(); }
3660 Target_selector_x86_64 target_selector_x86_64
;
3662 } // End anonymous namespace.