1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
49 // A class to handle the PLT data.
51 class Output_data_plt_x86_64
: public Output_section_data
54 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
56 Output_data_plt_x86_64(Symbol_table
*, Layout
*, Output_data_got
<64, false>*,
59 // Add an entry to the PLT.
61 add_entry(Symbol
* gsym
);
63 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
65 add_local_ifunc_entry(Sized_relobj
<64, false>* relobj
,
66 unsigned int local_sym_index
);
68 // Add the reserved TLSDESC_PLT entry to the PLT.
70 reserve_tlsdesc_entry(unsigned int got_offset
)
71 { this->tlsdesc_got_offset_
= got_offset
; }
73 // Return true if a TLSDESC_PLT entry has been reserved.
75 has_tlsdesc_entry() const
76 { return this->tlsdesc_got_offset_
!= -1U; }
78 // Return the GOT offset for the reserved TLSDESC_PLT entry.
80 get_tlsdesc_got_offset() const
81 { return this->tlsdesc_got_offset_
; }
83 // Return the offset of the reserved TLSDESC_PLT entry.
85 get_tlsdesc_plt_offset() const
86 { return (this->count_
+ 1) * plt_entry_size
; }
88 // Return the .rela.plt section data.
91 { return this->rel_
; }
93 // Return where the TLSDESC relocations should go.
95 rela_tlsdesc(Layout
*);
97 // Return the number of PLT entries.
100 { return this->count_
; }
102 // Return the offset of the first non-reserved PLT entry.
104 first_plt_entry_offset()
105 { return plt_entry_size
; }
107 // Return the size of a PLT entry.
110 { return plt_entry_size
; }
114 do_adjust_output_section(Output_section
* os
);
116 // Write to a map file.
118 do_print_to_mapfile(Mapfile
* mapfile
) const
119 { mapfile
->print_output_data(this, _("** PLT")); }
122 // The size of an entry in the PLT.
123 static const int plt_entry_size
= 16;
125 // The first entry in the PLT.
126 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
127 // procedure linkage table for both programs and shared objects."
128 static unsigned char first_plt_entry
[plt_entry_size
];
130 // Other entries in the PLT for an executable.
131 static unsigned char plt_entry
[plt_entry_size
];
133 // The reserved TLSDESC entry in the PLT for an executable.
134 static unsigned char tlsdesc_plt_entry
[plt_entry_size
];
136 // Set the final size.
138 set_final_data_size();
140 // Write out the PLT data.
142 do_write(Output_file
*);
144 // The reloc section.
146 // The TLSDESC relocs, if necessary. These must follow the regular
148 Reloc_section
* tlsdesc_rel_
;
150 Output_data_got
<64, false>* got_
;
151 // The .got.plt section.
152 Output_data_space
* got_plt_
;
153 // The number of PLT entries.
155 // Offset of the reserved TLSDESC_GOT entry when needed.
156 unsigned int tlsdesc_got_offset_
;
159 // The x86_64 target class.
161 // http://www.x86-64.org/documentation/abi.pdf
162 // TLS info comes from
163 // http://people.redhat.com/drepper/tls.pdf
164 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
166 class Target_x86_64
: public Target_freebsd
<64, false>
169 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
170 // uses only Elf64_Rela relocation entries with explicit addends."
171 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
174 : Target_freebsd
<64, false>(&x86_64_info
),
175 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_tlsdesc_(NULL
),
176 global_offset_table_(NULL
), rela_dyn_(NULL
),
177 copy_relocs_(elfcpp::R_X86_64_COPY
), dynbss_(NULL
),
178 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
179 tls_base_symbol_defined_(false)
182 // This function should be defined in targets that can use relocation
183 // types to determine (implemented in local_reloc_may_be_function_pointer
184 // and global_reloc_may_be_function_pointer)
185 // if a function's pointer is taken. ICF uses this in safe mode to only
186 // fold those functions whose pointer is defintely not taken. For x86_64
187 // pie binaries, safe ICF cannot be done by looking at relocation types.
189 can_check_for_function_pointers() const
190 { return !parameters
->options().pie(); }
193 can_icf_inline_merge_sections () const
196 // Hook for a new output section.
198 do_new_output_section(Output_section
*) const;
200 // Scan the relocations to look for symbol adjustments.
202 gc_process_relocs(Symbol_table
* symtab
,
204 Sized_relobj
<64, false>* object
,
205 unsigned int data_shndx
,
206 unsigned int sh_type
,
207 const unsigned char* prelocs
,
209 Output_section
* output_section
,
210 bool needs_special_offset_handling
,
211 size_t local_symbol_count
,
212 const unsigned char* plocal_symbols
);
214 // Scan the relocations to look for symbol adjustments.
216 scan_relocs(Symbol_table
* symtab
,
218 Sized_relobj
<64, false>* object
,
219 unsigned int data_shndx
,
220 unsigned int sh_type
,
221 const unsigned char* prelocs
,
223 Output_section
* output_section
,
224 bool needs_special_offset_handling
,
225 size_t local_symbol_count
,
226 const unsigned char* plocal_symbols
);
228 // Finalize the sections.
230 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
232 // Return the value to use for a dynamic which requires special
235 do_dynsym_value(const Symbol
*) const;
237 // Relocate a section.
239 relocate_section(const Relocate_info
<64, false>*,
240 unsigned int sh_type
,
241 const unsigned char* prelocs
,
243 Output_section
* output_section
,
244 bool needs_special_offset_handling
,
246 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
247 section_size_type view_size
,
248 const Reloc_symbol_changes
*);
250 // Scan the relocs during a relocatable link.
252 scan_relocatable_relocs(Symbol_table
* symtab
,
254 Sized_relobj
<64, false>* object
,
255 unsigned int data_shndx
,
256 unsigned int sh_type
,
257 const unsigned char* prelocs
,
259 Output_section
* output_section
,
260 bool needs_special_offset_handling
,
261 size_t local_symbol_count
,
262 const unsigned char* plocal_symbols
,
263 Relocatable_relocs
*);
265 // Relocate a section during a relocatable link.
267 relocate_for_relocatable(const Relocate_info
<64, false>*,
268 unsigned int sh_type
,
269 const unsigned char* prelocs
,
271 Output_section
* output_section
,
272 off_t offset_in_output_section
,
273 const Relocatable_relocs
*,
275 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
276 section_size_type view_size
,
277 unsigned char* reloc_view
,
278 section_size_type reloc_view_size
);
280 // Return a string used to fill a code section with nops.
282 do_code_fill(section_size_type length
) const;
284 // Return whether SYM is defined by the ABI.
286 do_is_defined_by_abi(const Symbol
* sym
) const
287 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
289 // Return the symbol index to use for a target specific relocation.
290 // The only target specific relocation is R_X86_64_TLSDESC for a
291 // local symbol, which is an absolute reloc.
293 do_reloc_symbol_index(void*, unsigned int r_type
) const
295 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
299 // Return the addend to use for a target specific relocation.
301 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
303 // Return the PLT section.
305 do_plt_section_for_global(const Symbol
*) const
306 { return this->plt_section(); }
309 do_plt_section_for_local(const Relobj
*, unsigned int) const
310 { return this->plt_section(); }
312 // Adjust -fstack-split code which calls non-stack-split code.
314 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
315 section_offset_type fnoffset
, section_size_type fnsize
,
316 unsigned char* view
, section_size_type view_size
,
317 std::string
* from
, std::string
* to
) const;
319 // Return the size of the GOT section.
323 gold_assert(this->got_
!= NULL
);
324 return this->got_
->data_size();
327 // Return the number of entries in the GOT.
329 got_entry_count() const
331 if (this->got_
== NULL
)
333 return this->got_size() / 8;
336 // Return the number of entries in the PLT.
338 plt_entry_count() const;
340 // Return the offset of the first non-reserved PLT entry.
342 first_plt_entry_offset() const;
344 // Return the size of each PLT entry.
346 plt_entry_size() const;
348 // Add a new reloc argument, returning the index in the vector.
350 add_tlsdesc_info(Sized_relobj
<64, false>* object
, unsigned int r_sym
)
352 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
353 return this->tlsdesc_reloc_info_
.size() - 1;
357 // The class which scans relocations.
362 : issued_non_pic_error_(false)
366 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
367 Sized_relobj
<64, false>* object
,
368 unsigned int data_shndx
,
369 Output_section
* output_section
,
370 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
371 const elfcpp::Sym
<64, false>& lsym
);
374 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
375 Sized_relobj
<64, false>* object
,
376 unsigned int data_shndx
,
377 Output_section
* output_section
,
378 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
382 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
383 Target_x86_64
* target
,
384 Sized_relobj
<64, false>* object
,
385 unsigned int data_shndx
,
386 Output_section
* output_section
,
387 const elfcpp::Rela
<64, false>& reloc
,
389 const elfcpp::Sym
<64, false>& lsym
);
392 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
393 Target_x86_64
* target
,
394 Sized_relobj
<64, false>* object
,
395 unsigned int data_shndx
,
396 Output_section
* output_section
,
397 const elfcpp::Rela
<64, false>& reloc
,
403 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
406 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
410 check_non_pic(Relobj
*, unsigned int r_type
);
413 possible_function_pointer_reloc(unsigned int r_type
);
416 reloc_needs_plt_for_ifunc(Sized_relobj
<64, false>*, unsigned int r_type
);
418 // Whether we have issued an error about a non-PIC compilation.
419 bool issued_non_pic_error_
;
422 // The class which implements relocation.
427 : skip_call_tls_get_addr_(false)
432 if (this->skip_call_tls_get_addr_
)
434 // FIXME: This needs to specify the location somehow.
435 gold_error(_("missing expected TLS relocation"));
439 // Do a relocation. Return false if the caller should not issue
440 // any warnings about this relocation.
442 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, Output_section
*,
443 size_t relnum
, const elfcpp::Rela
<64, false>&,
444 unsigned int r_type
, const Sized_symbol
<64>*,
445 const Symbol_value
<64>*,
446 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
450 // Do a TLS relocation.
452 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
453 size_t relnum
, const elfcpp::Rela
<64, false>&,
454 unsigned int r_type
, const Sized_symbol
<64>*,
455 const Symbol_value
<64>*,
456 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
459 // Do a TLS General-Dynamic to Initial-Exec transition.
461 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
462 Output_segment
* tls_segment
,
463 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
464 elfcpp::Elf_types
<64>::Elf_Addr value
,
466 elfcpp::Elf_types
<64>::Elf_Addr
,
467 section_size_type view_size
);
469 // Do a TLS General-Dynamic to Local-Exec transition.
471 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
472 Output_segment
* tls_segment
,
473 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
474 elfcpp::Elf_types
<64>::Elf_Addr value
,
476 section_size_type view_size
);
478 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
480 tls_desc_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
481 Output_segment
* tls_segment
,
482 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
483 elfcpp::Elf_types
<64>::Elf_Addr value
,
485 elfcpp::Elf_types
<64>::Elf_Addr
,
486 section_size_type view_size
);
488 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
490 tls_desc_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
491 Output_segment
* tls_segment
,
492 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
493 elfcpp::Elf_types
<64>::Elf_Addr value
,
495 section_size_type view_size
);
497 // Do a TLS Local-Dynamic to Local-Exec transition.
499 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
500 Output_segment
* tls_segment
,
501 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
502 elfcpp::Elf_types
<64>::Elf_Addr value
,
504 section_size_type view_size
);
506 // Do a TLS Initial-Exec to Local-Exec transition.
508 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
509 Output_segment
* tls_segment
,
510 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
511 elfcpp::Elf_types
<64>::Elf_Addr value
,
513 section_size_type view_size
);
515 // This is set if we should skip the next reloc, which should be a
516 // PLT32 reloc against ___tls_get_addr.
517 bool skip_call_tls_get_addr_
;
520 // A class which returns the size required for a relocation type,
521 // used while scanning relocs during a relocatable link.
522 class Relocatable_size_for_reloc
526 get_size_for_reloc(unsigned int, Relobj
*);
529 // Adjust TLS relocation type based on the options and whether this
530 // is a local symbol.
531 static tls::Tls_optimization
532 optimize_tls_reloc(bool is_final
, int r_type
);
534 // Get the GOT section, creating it if necessary.
535 Output_data_got
<64, false>*
536 got_section(Symbol_table
*, Layout
*);
538 // Get the GOT PLT section.
540 got_plt_section() const
542 gold_assert(this->got_plt_
!= NULL
);
543 return this->got_plt_
;
546 // Get the GOT section for TLSDESC entries.
547 Output_data_got
<64, false>*
548 got_tlsdesc_section() const
550 gold_assert(this->got_tlsdesc_
!= NULL
);
551 return this->got_tlsdesc_
;
554 // Create the PLT section.
556 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
558 // Create a PLT entry for a global symbol.
560 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
562 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
564 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
565 Sized_relobj
<64, false>* relobj
,
566 unsigned int local_sym_index
);
568 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
570 define_tls_base_symbol(Symbol_table
*, Layout
*);
572 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
574 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
576 // Create a GOT entry for the TLS module index.
578 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
579 Sized_relobj
<64, false>* object
);
581 // Get the PLT section.
582 Output_data_plt_x86_64
*
585 gold_assert(this->plt_
!= NULL
);
589 // Get the dynamic reloc section, creating it if necessary.
591 rela_dyn_section(Layout
*);
593 // Get the section to use for TLSDESC relocations.
595 rela_tlsdesc_section(Layout
*) const;
597 // Add a potential copy relocation.
599 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
600 Sized_relobj
<64, false>* object
,
601 unsigned int shndx
, Output_section
* output_section
,
602 Symbol
* sym
, const elfcpp::Rela
<64, false>& reloc
)
604 this->copy_relocs_
.copy_reloc(symtab
, layout
,
605 symtab
->get_sized_symbol
<64>(sym
),
606 object
, shndx
, output_section
,
607 reloc
, this->rela_dyn_section(layout
));
610 // Information about this specific target which we pass to the
611 // general Target structure.
612 static const Target::Target_info x86_64_info
;
614 // The types of GOT entries needed for this platform.
615 // These values are exposed to the ABI in an incremental link.
616 // Do not renumber existing values without changing the version
617 // number of the .gnu_incremental_inputs section.
620 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
621 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
622 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
623 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
626 // This type is used as the argument to the target specific
627 // relocation routines. The only target specific reloc is
628 // R_X86_64_TLSDESC against a local symbol.
631 Tlsdesc_info(Sized_relobj
<64, false>* a_object
, unsigned int a_r_sym
)
632 : object(a_object
), r_sym(a_r_sym
)
635 // The object in which the local symbol is defined.
636 Sized_relobj
<64, false>* object
;
637 // The local symbol index in the object.
642 Output_data_got
<64, false>* got_
;
644 Output_data_plt_x86_64
* plt_
;
645 // The GOT PLT section.
646 Output_data_space
* got_plt_
;
647 // The GOT section for TLSDESC relocations.
648 Output_data_got
<64, false>* got_tlsdesc_
;
649 // The _GLOBAL_OFFSET_TABLE_ symbol.
650 Symbol
* global_offset_table_
;
651 // The dynamic reloc section.
652 Reloc_section
* rela_dyn_
;
653 // Relocs saved to avoid a COPY reloc.
654 Copy_relocs
<elfcpp::SHT_RELA
, 64, false> copy_relocs_
;
655 // Space for variables copied with a COPY reloc.
656 Output_data_space
* dynbss_
;
657 // Offset of the GOT entry for the TLS module index.
658 unsigned int got_mod_index_offset_
;
659 // We handle R_X86_64_TLSDESC against a local symbol as a target
660 // specific relocation. Here we store the object and local symbol
661 // index for the relocation.
662 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
663 // True if the _TLS_MODULE_BASE_ symbol has been defined.
664 bool tls_base_symbol_defined_
;
667 const Target::Target_info
Target_x86_64::x86_64_info
=
670 false, // is_big_endian
671 elfcpp::EM_X86_64
, // machine_code
672 false, // has_make_symbol
673 false, // has_resolve
674 true, // has_code_fill
675 true, // is_default_stack_executable
677 "/lib/ld64.so.1", // program interpreter
678 0x400000, // default_text_segment_address
679 0x1000, // abi_pagesize (overridable by -z max-page-size)
680 0x1000, // common_pagesize (overridable by -z common-page-size)
681 elfcpp::SHN_UNDEF
, // small_common_shndx
682 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
683 0, // small_common_section_flags
684 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
685 NULL
, // attributes_section
686 NULL
// attributes_vendor
689 // This is called when a new output section is created. This is where
690 // we handle the SHF_X86_64_LARGE.
693 Target_x86_64::do_new_output_section(Output_section
* os
) const
695 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
696 os
->set_is_large_section();
699 // Get the GOT section, creating it if necessary.
701 Output_data_got
<64, false>*
702 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
704 if (this->got_
== NULL
)
706 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
708 this->got_
= new Output_data_got
<64, false>();
710 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
712 | elfcpp::SHF_WRITE
),
713 this->got_
, ORDER_RELRO_LAST
,
716 this->got_plt_
= new Output_data_space(8, "** GOT PLT");
717 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
719 | elfcpp::SHF_WRITE
),
720 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
723 // The first three entries are reserved.
724 this->got_plt_
->set_current_data_size(3 * 8);
726 // Those bytes can go into the relro segment.
727 layout
->increase_relro(3 * 8);
729 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
730 this->global_offset_table_
=
731 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
732 Symbol_table::PREDEFINED
,
734 0, 0, elfcpp::STT_OBJECT
,
736 elfcpp::STV_HIDDEN
, 0,
739 // If there are any TLSDESC relocations, they get GOT entries in
740 // .got.plt after the jump slot entries.
741 this->got_tlsdesc_
= new Output_data_got
<64, false>();
742 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
744 | elfcpp::SHF_WRITE
),
746 ORDER_NON_RELRO_FIRST
, false);
752 // Get the dynamic reloc section, creating it if necessary.
754 Target_x86_64::Reloc_section
*
755 Target_x86_64::rela_dyn_section(Layout
* layout
)
757 if (this->rela_dyn_
== NULL
)
759 gold_assert(layout
!= NULL
);
760 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
761 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
762 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
763 ORDER_DYNAMIC_RELOCS
, false);
765 return this->rela_dyn_
;
768 // Create the PLT section. The ordinary .got section is an argument,
769 // since we need to refer to the start. We also create our own .got
770 // section just for PLT entries.
772 Output_data_plt_x86_64::Output_data_plt_x86_64(Symbol_table
* symtab
,
774 Output_data_got
<64, false>* got
,
775 Output_data_space
* got_plt
)
776 : Output_section_data(8), tlsdesc_rel_(NULL
), got_(got
), got_plt_(got_plt
),
777 count_(0), tlsdesc_got_offset_(-1U)
779 this->rel_
= new Reloc_section(false);
780 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
781 elfcpp::SHF_ALLOC
, this->rel_
,
782 ORDER_DYNAMIC_PLT_RELOCS
, false);
784 if (parameters
->doing_static_link())
786 // A statically linked executable will only have a .rela.plt
787 // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
788 // symbols. The library will use these symbols to locate the
789 // IRELATIVE relocs at program startup time.
790 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
791 Symbol_table::PREDEFINED
,
792 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
793 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
795 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
796 Symbol_table::PREDEFINED
,
797 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
798 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
804 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
806 os
->set_entsize(plt_entry_size
);
809 // Add an entry to the PLT.
812 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
814 gold_assert(!gsym
->has_plt_offset());
816 // Note that when setting the PLT offset we skip the initial
817 // reserved PLT entry.
818 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
822 section_offset_type got_offset
= this->got_plt_
->current_data_size();
824 // Every PLT entry needs a GOT entry which points back to the PLT
825 // entry (this will be changed by the dynamic linker, normally
826 // lazily when the function is called).
827 this->got_plt_
->set_current_data_size(got_offset
+ 8);
829 // Every PLT entry needs a reloc.
830 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
831 && gsym
->can_use_relative_reloc(false))
832 this->rel_
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
833 this->got_plt_
, got_offset
, 0);
836 gsym
->set_needs_dynsym_entry();
837 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
841 // Note that we don't need to save the symbol. The contents of the
842 // PLT are independent of which symbols are used. The symbols only
843 // appear in the relocations.
846 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
850 Output_data_plt_x86_64::add_local_ifunc_entry(Sized_relobj
<64, false>* relobj
,
851 unsigned int local_sym_index
)
853 unsigned int plt_offset
= (this->count_
+ 1) * plt_entry_size
;
856 section_offset_type got_offset
= this->got_plt_
->current_data_size();
858 // Every PLT entry needs a GOT entry which points back to the PLT
860 this->got_plt_
->set_current_data_size(got_offset
+ 8);
862 // Every PLT entry needs a reloc.
863 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
,
864 elfcpp::R_X86_64_IRELATIVE
,
865 this->got_plt_
, got_offset
, 0);
870 // Return where the TLSDESC relocations should go, creating it if
871 // necessary. These follow the JUMP_SLOT relocations.
873 Output_data_plt_x86_64::Reloc_section
*
874 Output_data_plt_x86_64::rela_tlsdesc(Layout
* layout
)
876 if (this->tlsdesc_rel_
== NULL
)
878 this->tlsdesc_rel_
= new Reloc_section(false);
879 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
880 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
881 ORDER_DYNAMIC_PLT_RELOCS
, false);
882 gold_assert(this->tlsdesc_rel_
->output_section() ==
883 this->rel_
->output_section());
885 return this->tlsdesc_rel_
;
888 // Set the final size.
890 Output_data_plt_x86_64::set_final_data_size()
892 unsigned int count
= this->count_
;
893 if (this->has_tlsdesc_entry())
895 this->set_data_size((count
+ 1) * plt_entry_size
);
898 // The first entry in the PLT for an executable.
900 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
902 // From AMD64 ABI Draft 0.98, page 76
903 0xff, 0x35, // pushq contents of memory address
904 0, 0, 0, 0, // replaced with address of .got + 8
905 0xff, 0x25, // jmp indirect
906 0, 0, 0, 0, // replaced with address of .got + 16
907 0x90, 0x90, 0x90, 0x90 // noop (x4)
910 // Subsequent entries in the PLT for an executable.
912 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
914 // From AMD64 ABI Draft 0.98, page 76
915 0xff, 0x25, // jmpq indirect
916 0, 0, 0, 0, // replaced with address of symbol in .got
917 0x68, // pushq immediate
918 0, 0, 0, 0, // replaced with offset into relocation table
919 0xe9, // jmpq relative
920 0, 0, 0, 0 // replaced with offset to start of .plt
923 // The reserved TLSDESC entry in the PLT for an executable.
925 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry
[plt_entry_size
] =
927 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
928 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
929 0xff, 0x35, // pushq x(%rip)
930 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
931 0xff, 0x25, // jmpq *y(%rip)
932 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
937 // Write out the PLT. This uses the hand-coded instructions above,
938 // and adjusts them as needed. This is specified by the AMD64 ABI.
941 Output_data_plt_x86_64::do_write(Output_file
* of
)
943 const off_t offset
= this->offset();
944 const section_size_type oview_size
=
945 convert_to_section_size_type(this->data_size());
946 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
948 const off_t got_file_offset
= this->got_plt_
->offset();
949 const section_size_type got_size
=
950 convert_to_section_size_type(this->got_plt_
->data_size());
951 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
954 unsigned char* pov
= oview
;
956 // The base address of the .plt section.
957 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
958 // The base address of the .got section.
959 elfcpp::Elf_types
<64>::Elf_Addr got_base
= this->got_
->address();
960 // The base address of the PLT portion of the .got section,
961 // which is where the GOT pointer will point, and where the
962 // three reserved GOT entries are located.
963 elfcpp::Elf_types
<64>::Elf_Addr got_address
= this->got_plt_
->address();
965 memcpy(pov
, first_plt_entry
, plt_entry_size
);
966 // We do a jmp relative to the PC at the end of this instruction.
967 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
969 - (plt_address
+ 6)));
970 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
972 - (plt_address
+ 12)));
973 pov
+= plt_entry_size
;
975 unsigned char* got_pov
= got_view
;
977 memset(got_pov
, 0, 24);
980 unsigned int plt_offset
= plt_entry_size
;
981 unsigned int got_offset
= 24;
982 const unsigned int count
= this->count_
;
983 for (unsigned int plt_index
= 0;
986 pov
+= plt_entry_size
,
988 plt_offset
+= plt_entry_size
,
991 // Set and adjust the PLT entry itself.
992 memcpy(pov
, plt_entry
, plt_entry_size
);
993 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
994 (got_address
+ got_offset
995 - (plt_address
+ plt_offset
998 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
999 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1000 - (plt_offset
+ plt_entry_size
));
1002 // Set the entry in the GOT.
1003 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
1006 if (this->has_tlsdesc_entry())
1008 // Set and adjust the reserved TLSDESC PLT entry.
1009 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
1010 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1011 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1013 - (plt_address
+ plt_offset
1015 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1017 + tlsdesc_got_offset
1018 - (plt_address
+ plt_offset
1020 pov
+= plt_entry_size
;
1023 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1024 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1026 of
->write_output_view(offset
, oview_size
, oview
);
1027 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1030 // Create the PLT section.
1033 Target_x86_64::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1035 if (this->plt_
== NULL
)
1037 // Create the GOT sections first.
1038 this->got_section(symtab
, layout
);
1040 this->plt_
= new Output_data_plt_x86_64(symtab
, layout
, this->got_
,
1042 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1044 | elfcpp::SHF_EXECINSTR
),
1045 this->plt_
, ORDER_PLT
, false);
1047 // Make the sh_info field of .rela.plt point to .plt.
1048 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1049 rela_plt_os
->set_info_section(this->plt_
->output_section());
1053 // Return the section for TLSDESC relocations.
1055 Target_x86_64::Reloc_section
*
1056 Target_x86_64::rela_tlsdesc_section(Layout
* layout
) const
1058 return this->plt_section()->rela_tlsdesc(layout
);
1061 // Create a PLT entry for a global symbol.
1064 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1067 if (gsym
->has_plt_offset())
1070 if (this->plt_
== NULL
)
1071 this->make_plt_section(symtab
, layout
);
1073 this->plt_
->add_entry(gsym
);
1076 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1079 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1080 Sized_relobj
<64, false>* relobj
,
1081 unsigned int local_sym_index
)
1083 if (relobj
->local_has_plt_offset(local_sym_index
))
1085 if (this->plt_
== NULL
)
1086 this->make_plt_section(symtab
, layout
);
1087 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(relobj
,
1089 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1092 // Return the number of entries in the PLT.
1095 Target_x86_64::plt_entry_count() const
1097 if (this->plt_
== NULL
)
1099 return this->plt_
->entry_count();
1102 // Return the offset of the first non-reserved PLT entry.
1105 Target_x86_64::first_plt_entry_offset() const
1107 return Output_data_plt_x86_64::first_plt_entry_offset();
1110 // Return the size of each PLT entry.
1113 Target_x86_64::plt_entry_size() const
1115 return Output_data_plt_x86_64::get_plt_entry_size();
1118 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1121 Target_x86_64::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
1123 if (this->tls_base_symbol_defined_
)
1126 Output_segment
* tls_segment
= layout
->tls_segment();
1127 if (tls_segment
!= NULL
)
1129 bool is_exec
= parameters
->options().output_is_executable();
1130 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1131 Symbol_table::PREDEFINED
,
1135 elfcpp::STV_HIDDEN
, 0,
1137 ? Symbol::SEGMENT_END
1138 : Symbol::SEGMENT_START
),
1141 this->tls_base_symbol_defined_
= true;
1144 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1147 Target_x86_64::reserve_tlsdesc_entries(Symbol_table
* symtab
,
1150 if (this->plt_
== NULL
)
1151 this->make_plt_section(symtab
, layout
);
1153 if (!this->plt_
->has_tlsdesc_entry())
1155 // Allocate the TLSDESC_GOT entry.
1156 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1157 unsigned int got_offset
= got
->add_constant(0);
1159 // Allocate the TLSDESC_PLT entry.
1160 this->plt_
->reserve_tlsdesc_entry(got_offset
);
1164 // Create a GOT entry for the TLS module index.
1167 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1168 Sized_relobj
<64, false>* object
)
1170 if (this->got_mod_index_offset_
== -1U)
1172 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1173 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1174 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1175 unsigned int got_offset
= got
->add_constant(0);
1176 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
1178 got
->add_constant(0);
1179 this->got_mod_index_offset_
= got_offset
;
1181 return this->got_mod_index_offset_
;
1184 // Optimize the TLS relocation type based on what we know about the
1185 // symbol. IS_FINAL is true if the final address of this symbol is
1186 // known at link time.
1188 tls::Tls_optimization
1189 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
1191 // If we are generating a shared library, then we can't do anything
1193 if (parameters
->options().shared())
1194 return tls::TLSOPT_NONE
;
1198 case elfcpp::R_X86_64_TLSGD
:
1199 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1200 case elfcpp::R_X86_64_TLSDESC_CALL
:
1201 // These are General-Dynamic which permits fully general TLS
1202 // access. Since we know that we are generating an executable,
1203 // we can convert this to Initial-Exec. If we also know that
1204 // this is a local symbol, we can further switch to Local-Exec.
1206 return tls::TLSOPT_TO_LE
;
1207 return tls::TLSOPT_TO_IE
;
1209 case elfcpp::R_X86_64_TLSLD
:
1210 // This is Local-Dynamic, which refers to a local symbol in the
1211 // dynamic TLS block. Since we know that we generating an
1212 // executable, we can switch to Local-Exec.
1213 return tls::TLSOPT_TO_LE
;
1215 case elfcpp::R_X86_64_DTPOFF32
:
1216 case elfcpp::R_X86_64_DTPOFF64
:
1217 // Another Local-Dynamic reloc.
1218 return tls::TLSOPT_TO_LE
;
1220 case elfcpp::R_X86_64_GOTTPOFF
:
1221 // These are Initial-Exec relocs which get the thread offset
1222 // from the GOT. If we know that we are linking against the
1223 // local symbol, we can switch to Local-Exec, which links the
1224 // thread offset into the instruction.
1226 return tls::TLSOPT_TO_LE
;
1227 return tls::TLSOPT_NONE
;
1229 case elfcpp::R_X86_64_TPOFF32
:
1230 // When we already have Local-Exec, there is nothing further we
1232 return tls::TLSOPT_NONE
;
1239 // Report an unsupported relocation against a local symbol.
1242 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
1243 unsigned int r_type
)
1245 gold_error(_("%s: unsupported reloc %u against local symbol"),
1246 object
->name().c_str(), r_type
);
1249 // We are about to emit a dynamic relocation of type R_TYPE. If the
1250 // dynamic linker does not support it, issue an error. The GNU linker
1251 // only issues a non-PIC error for an allocated read-only section.
1252 // Here we know the section is allocated, but we don't know that it is
1253 // read-only. But we check for all the relocation types which the
1254 // glibc dynamic linker supports, so it seems appropriate to issue an
1255 // error even if the section is not read-only.
1258 Target_x86_64::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
)
1262 // These are the relocation types supported by glibc for x86_64.
1263 case elfcpp::R_X86_64_RELATIVE
:
1264 case elfcpp::R_X86_64_IRELATIVE
:
1265 case elfcpp::R_X86_64_GLOB_DAT
:
1266 case elfcpp::R_X86_64_JUMP_SLOT
:
1267 case elfcpp::R_X86_64_DTPMOD64
:
1268 case elfcpp::R_X86_64_DTPOFF64
:
1269 case elfcpp::R_X86_64_TPOFF64
:
1270 case elfcpp::R_X86_64_64
:
1271 case elfcpp::R_X86_64_32
:
1272 case elfcpp::R_X86_64_PC32
:
1273 case elfcpp::R_X86_64_COPY
:
1277 // This prevents us from issuing more than one error per reloc
1278 // section. But we can still wind up issuing more than one
1279 // error per object file.
1280 if (this->issued_non_pic_error_
)
1282 gold_assert(parameters
->options().output_is_position_independent());
1283 object
->error(_("requires unsupported dynamic reloc; "
1284 "recompile with -fPIC"));
1285 this->issued_non_pic_error_
= true;
1288 case elfcpp::R_X86_64_NONE
:
1293 // Return whether we need to make a PLT entry for a relocation of the
1294 // given type against a STT_GNU_IFUNC symbol.
1297 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(Sized_relobj
<64, false>* object
,
1298 unsigned int r_type
)
1302 case elfcpp::R_X86_64_NONE
:
1303 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1304 case elfcpp::R_X86_64_GNU_VTENTRY
:
1307 case elfcpp::R_X86_64_64
:
1308 case elfcpp::R_X86_64_32
:
1309 case elfcpp::R_X86_64_32S
:
1310 case elfcpp::R_X86_64_16
:
1311 case elfcpp::R_X86_64_8
:
1312 case elfcpp::R_X86_64_PC64
:
1313 case elfcpp::R_X86_64_PC32
:
1314 case elfcpp::R_X86_64_PC16
:
1315 case elfcpp::R_X86_64_PC8
:
1316 case elfcpp::R_X86_64_PLT32
:
1317 case elfcpp::R_X86_64_GOTPC32
:
1318 case elfcpp::R_X86_64_GOTOFF64
:
1319 case elfcpp::R_X86_64_GOTPC64
:
1320 case elfcpp::R_X86_64_PLTOFF64
:
1321 case elfcpp::R_X86_64_GOT64
:
1322 case elfcpp::R_X86_64_GOT32
:
1323 case elfcpp::R_X86_64_GOTPCREL64
:
1324 case elfcpp::R_X86_64_GOTPCREL
:
1325 case elfcpp::R_X86_64_GOTPLT64
:
1328 case elfcpp::R_X86_64_COPY
:
1329 case elfcpp::R_X86_64_GLOB_DAT
:
1330 case elfcpp::R_X86_64_JUMP_SLOT
:
1331 case elfcpp::R_X86_64_RELATIVE
:
1332 case elfcpp::R_X86_64_IRELATIVE
:
1333 case elfcpp::R_X86_64_TPOFF64
:
1334 case elfcpp::R_X86_64_DTPMOD64
:
1335 case elfcpp::R_X86_64_TLSDESC
:
1336 // We will give an error later.
1339 case elfcpp::R_X86_64_TLSGD
:
1340 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1341 case elfcpp::R_X86_64_TLSDESC_CALL
:
1342 case elfcpp::R_X86_64_TLSLD
:
1343 case elfcpp::R_X86_64_DTPOFF32
:
1344 case elfcpp::R_X86_64_DTPOFF64
:
1345 case elfcpp::R_X86_64_GOTTPOFF
:
1346 case elfcpp::R_X86_64_TPOFF32
:
1347 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1348 object
->name().c_str(), r_type
);
1351 case elfcpp::R_X86_64_SIZE32
:
1352 case elfcpp::R_X86_64_SIZE64
:
1354 // We will give an error later.
1359 // Scan a relocation for a local symbol.
1362 Target_x86_64::Scan::local(Symbol_table
* symtab
,
1364 Target_x86_64
* target
,
1365 Sized_relobj
<64, false>* object
,
1366 unsigned int data_shndx
,
1367 Output_section
* output_section
,
1368 const elfcpp::Rela
<64, false>& reloc
,
1369 unsigned int r_type
,
1370 const elfcpp::Sym
<64, false>& lsym
)
1372 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1373 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
1374 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1376 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1377 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
1382 case elfcpp::R_X86_64_NONE
:
1383 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1384 case elfcpp::R_X86_64_GNU_VTENTRY
:
1387 case elfcpp::R_X86_64_64
:
1388 // If building a shared library (or a position-independent
1389 // executable), we need to create a dynamic relocation for this
1390 // location. The relocation applied at link time will apply the
1391 // link-time value, so we flag the location with an
1392 // R_X86_64_RELATIVE relocation so the dynamic loader can
1393 // relocate it easily.
1394 if (parameters
->options().output_is_position_independent())
1396 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1397 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1398 rela_dyn
->add_local_relative(object
, r_sym
,
1399 elfcpp::R_X86_64_RELATIVE
,
1400 output_section
, data_shndx
,
1401 reloc
.get_r_offset(),
1402 reloc
.get_r_addend());
1406 case elfcpp::R_X86_64_32
:
1407 case elfcpp::R_X86_64_32S
:
1408 case elfcpp::R_X86_64_16
:
1409 case elfcpp::R_X86_64_8
:
1410 // If building a shared library (or a position-independent
1411 // executable), we need to create a dynamic relocation for this
1412 // location. We can't use an R_X86_64_RELATIVE relocation
1413 // because that is always a 64-bit relocation.
1414 if (parameters
->options().output_is_position_independent())
1416 this->check_non_pic(object
, r_type
);
1418 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1419 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1420 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1421 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1422 data_shndx
, reloc
.get_r_offset(),
1423 reloc
.get_r_addend());
1426 gold_assert(lsym
.get_st_value() == 0);
1427 unsigned int shndx
= lsym
.get_st_shndx();
1429 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1432 object
->error(_("section symbol %u has bad shndx %u"),
1435 rela_dyn
->add_local_section(object
, shndx
,
1436 r_type
, output_section
,
1437 data_shndx
, reloc
.get_r_offset(),
1438 reloc
.get_r_addend());
1443 case elfcpp::R_X86_64_PC64
:
1444 case elfcpp::R_X86_64_PC32
:
1445 case elfcpp::R_X86_64_PC16
:
1446 case elfcpp::R_X86_64_PC8
:
1449 case elfcpp::R_X86_64_PLT32
:
1450 // Since we know this is a local symbol, we can handle this as a
1454 case elfcpp::R_X86_64_GOTPC32
:
1455 case elfcpp::R_X86_64_GOTOFF64
:
1456 case elfcpp::R_X86_64_GOTPC64
:
1457 case elfcpp::R_X86_64_PLTOFF64
:
1458 // We need a GOT section.
1459 target
->got_section(symtab
, layout
);
1460 // For PLTOFF64, we'd normally want a PLT section, but since we
1461 // know this is a local symbol, no PLT is needed.
1464 case elfcpp::R_X86_64_GOT64
:
1465 case elfcpp::R_X86_64_GOT32
:
1466 case elfcpp::R_X86_64_GOTPCREL64
:
1467 case elfcpp::R_X86_64_GOTPCREL
:
1468 case elfcpp::R_X86_64_GOTPLT64
:
1470 // The symbol requires a GOT entry.
1471 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1472 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1474 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1475 // lets function pointers compare correctly with shared
1476 // libraries. Otherwise we would need an IRELATIVE reloc.
1478 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
)
1479 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
1481 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1484 // If we are generating a shared object, we need to add a
1485 // dynamic relocation for this symbol's GOT entry.
1486 if (parameters
->options().output_is_position_independent())
1488 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1489 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1490 if (r_type
!= elfcpp::R_X86_64_GOT32
)
1492 unsigned int got_offset
=
1493 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1494 rela_dyn
->add_local_relative(object
, r_sym
,
1495 elfcpp::R_X86_64_RELATIVE
,
1496 got
, got_offset
, 0);
1500 this->check_non_pic(object
, r_type
);
1502 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1503 rela_dyn
->add_local(
1504 object
, r_sym
, r_type
, got
,
1505 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1509 // For GOTPLT64, we'd normally want a PLT section, but since
1510 // we know this is a local symbol, no PLT is needed.
1514 case elfcpp::R_X86_64_COPY
:
1515 case elfcpp::R_X86_64_GLOB_DAT
:
1516 case elfcpp::R_X86_64_JUMP_SLOT
:
1517 case elfcpp::R_X86_64_RELATIVE
:
1518 case elfcpp::R_X86_64_IRELATIVE
:
1519 // These are outstanding tls relocs, which are unexpected when linking
1520 case elfcpp::R_X86_64_TPOFF64
:
1521 case elfcpp::R_X86_64_DTPMOD64
:
1522 case elfcpp::R_X86_64_TLSDESC
:
1523 gold_error(_("%s: unexpected reloc %u in object file"),
1524 object
->name().c_str(), r_type
);
1527 // These are initial tls relocs, which are expected when linking
1528 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1529 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1530 case elfcpp::R_X86_64_TLSDESC_CALL
:
1531 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1532 case elfcpp::R_X86_64_DTPOFF32
:
1533 case elfcpp::R_X86_64_DTPOFF64
:
1534 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1535 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1537 bool output_is_shared
= parameters
->options().shared();
1538 const tls::Tls_optimization optimized_type
1539 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
1542 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1543 if (optimized_type
== tls::TLSOPT_NONE
)
1545 // Create a pair of GOT entries for the module index and
1546 // dtv-relative offset.
1547 Output_data_got
<64, false>* got
1548 = target
->got_section(symtab
, layout
);
1549 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1550 unsigned int shndx
= lsym
.get_st_shndx();
1552 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1554 object
->error(_("local symbol %u has bad shndx %u"),
1557 got
->add_local_pair_with_rela(object
, r_sym
,
1560 target
->rela_dyn_section(layout
),
1561 elfcpp::R_X86_64_DTPMOD64
, 0);
1563 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1564 unsupported_reloc_local(object
, r_type
);
1567 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1568 target
->define_tls_base_symbol(symtab
, layout
);
1569 if (optimized_type
== tls::TLSOPT_NONE
)
1571 // Create reserved PLT and GOT entries for the resolver.
1572 target
->reserve_tlsdesc_entries(symtab
, layout
);
1574 // Generate a double GOT entry with an
1575 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1576 // is resolved lazily, so the GOT entry needs to be in
1577 // an area in .got.plt, not .got. Call got_section to
1578 // make sure the section has been created.
1579 target
->got_section(symtab
, layout
);
1580 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
1581 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1582 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
1584 unsigned int got_offset
= got
->add_constant(0);
1585 got
->add_constant(0);
1586 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
1588 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
1589 // We store the arguments we need in a vector, and
1590 // use the index into the vector as the parameter
1591 // to pass to the target specific routines.
1592 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
1593 void* arg
= reinterpret_cast<void*>(intarg
);
1594 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
1595 got
, got_offset
, 0);
1598 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1599 unsupported_reloc_local(object
, r_type
);
1602 case elfcpp::R_X86_64_TLSDESC_CALL
:
1605 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1606 if (optimized_type
== tls::TLSOPT_NONE
)
1608 // Create a GOT entry for the module index.
1609 target
->got_mod_index_entry(symtab
, layout
, object
);
1611 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1612 unsupported_reloc_local(object
, r_type
);
1615 case elfcpp::R_X86_64_DTPOFF32
:
1616 case elfcpp::R_X86_64_DTPOFF64
:
1619 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1620 layout
->set_has_static_tls();
1621 if (optimized_type
== tls::TLSOPT_NONE
)
1623 // Create a GOT entry for the tp-relative offset.
1624 Output_data_got
<64, false>* got
1625 = target
->got_section(symtab
, layout
);
1626 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1627 got
->add_local_with_rela(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1628 target
->rela_dyn_section(layout
),
1629 elfcpp::R_X86_64_TPOFF64
);
1631 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1632 unsupported_reloc_local(object
, r_type
);
1635 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1636 layout
->set_has_static_tls();
1637 if (output_is_shared
)
1638 unsupported_reloc_local(object
, r_type
);
1647 case elfcpp::R_X86_64_SIZE32
:
1648 case elfcpp::R_X86_64_SIZE64
:
1650 gold_error(_("%s: unsupported reloc %u against local symbol"),
1651 object
->name().c_str(), r_type
);
1657 // Report an unsupported relocation against a global symbol.
1660 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
1661 unsigned int r_type
,
1664 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1665 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1668 // Returns true if this relocation type could be that of a function pointer.
1670 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type
)
1674 case elfcpp::R_X86_64_64
:
1675 case elfcpp::R_X86_64_32
:
1676 case elfcpp::R_X86_64_32S
:
1677 case elfcpp::R_X86_64_16
:
1678 case elfcpp::R_X86_64_8
:
1679 case elfcpp::R_X86_64_GOT64
:
1680 case elfcpp::R_X86_64_GOT32
:
1681 case elfcpp::R_X86_64_GOTPCREL64
:
1682 case elfcpp::R_X86_64_GOTPCREL
:
1683 case elfcpp::R_X86_64_GOTPLT64
:
1691 // For safe ICF, scan a relocation for a local symbol to check if it
1692 // corresponds to a function pointer being taken. In that case mark
1693 // the function whose pointer was taken as not foldable.
1696 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
1700 Sized_relobj
<64, false>* ,
1703 const elfcpp::Rela
<64, false>& ,
1704 unsigned int r_type
,
1705 const elfcpp::Sym
<64, false>&)
1707 // When building a shared library, do not fold any local symbols as it is
1708 // not possible to distinguish pointer taken versus a call by looking at
1709 // the relocation types.
1710 return (parameters
->options().shared()
1711 || possible_function_pointer_reloc(r_type
));
1714 // For safe ICF, scan a relocation for a global symbol to check if it
1715 // corresponds to a function pointer being taken. In that case mark
1716 // the function whose pointer was taken as not foldable.
1719 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
1723 Sized_relobj
<64, false>* ,
1726 const elfcpp::Rela
<64, false>& ,
1727 unsigned int r_type
,
1730 // When building a shared library, do not fold symbols whose visibility
1731 // is hidden, internal or protected.
1732 return ((parameters
->options().shared()
1733 && (gsym
->visibility() == elfcpp::STV_INTERNAL
1734 || gsym
->visibility() == elfcpp::STV_PROTECTED
1735 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
1736 || possible_function_pointer_reloc(r_type
));
1739 // Scan a relocation for a global symbol.
1742 Target_x86_64::Scan::global(Symbol_table
* symtab
,
1744 Target_x86_64
* target
,
1745 Sized_relobj
<64, false>* object
,
1746 unsigned int data_shndx
,
1747 Output_section
* output_section
,
1748 const elfcpp::Rela
<64, false>& reloc
,
1749 unsigned int r_type
,
1752 // A STT_GNU_IFUNC symbol may require a PLT entry.
1753 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1754 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1755 target
->make_plt_entry(symtab
, layout
, gsym
);
1759 case elfcpp::R_X86_64_NONE
:
1760 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1761 case elfcpp::R_X86_64_GNU_VTENTRY
:
1764 case elfcpp::R_X86_64_64
:
1765 case elfcpp::R_X86_64_32
:
1766 case elfcpp::R_X86_64_32S
:
1767 case elfcpp::R_X86_64_16
:
1768 case elfcpp::R_X86_64_8
:
1770 // Make a PLT entry if necessary.
1771 if (gsym
->needs_plt_entry())
1773 target
->make_plt_entry(symtab
, layout
, gsym
);
1774 // Since this is not a PC-relative relocation, we may be
1775 // taking the address of a function. In that case we need to
1776 // set the entry in the dynamic symbol table to the address of
1778 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1779 gsym
->set_needs_dynsym_value();
1781 // Make a dynamic relocation if necessary.
1782 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1784 if (gsym
->may_need_copy_reloc())
1786 target
->copy_reloc(symtab
, layout
, object
,
1787 data_shndx
, output_section
, gsym
, reloc
);
1789 else if (r_type
== elfcpp::R_X86_64_64
1790 && gsym
->type() == elfcpp::STT_GNU_IFUNC
1791 && gsym
->can_use_relative_reloc(false)
1792 && !gsym
->is_from_dynobj()
1793 && !gsym
->is_undefined()
1794 && !gsym
->is_preemptible())
1796 // Use an IRELATIVE reloc for a locally defined
1797 // STT_GNU_IFUNC symbol. This makes a function
1798 // address in a PIE executable match the address in a
1799 // shared library that it links against.
1800 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1801 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
1802 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
1803 output_section
, object
,
1805 reloc
.get_r_offset(),
1806 reloc
.get_r_addend());
1808 else if (r_type
== elfcpp::R_X86_64_64
1809 && gsym
->can_use_relative_reloc(false))
1811 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1812 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1813 output_section
, object
,
1815 reloc
.get_r_offset(),
1816 reloc
.get_r_addend());
1820 this->check_non_pic(object
, r_type
);
1821 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1822 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1823 data_shndx
, reloc
.get_r_offset(),
1824 reloc
.get_r_addend());
1830 case elfcpp::R_X86_64_PC64
:
1831 case elfcpp::R_X86_64_PC32
:
1832 case elfcpp::R_X86_64_PC16
:
1833 case elfcpp::R_X86_64_PC8
:
1835 // Make a PLT entry if necessary.
1836 if (gsym
->needs_plt_entry())
1837 target
->make_plt_entry(symtab
, layout
, gsym
);
1838 // Make a dynamic relocation if necessary.
1839 int flags
= Symbol::NON_PIC_REF
;
1840 if (gsym
->is_func())
1841 flags
|= Symbol::FUNCTION_CALL
;
1842 if (gsym
->needs_dynamic_reloc(flags
))
1844 if (gsym
->may_need_copy_reloc())
1846 target
->copy_reloc(symtab
, layout
, object
,
1847 data_shndx
, output_section
, gsym
, reloc
);
1851 this->check_non_pic(object
, r_type
);
1852 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1853 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1854 data_shndx
, reloc
.get_r_offset(),
1855 reloc
.get_r_addend());
1861 case elfcpp::R_X86_64_GOT64
:
1862 case elfcpp::R_X86_64_GOT32
:
1863 case elfcpp::R_X86_64_GOTPCREL64
:
1864 case elfcpp::R_X86_64_GOTPCREL
:
1865 case elfcpp::R_X86_64_GOTPLT64
:
1867 // The symbol requires a GOT entry.
1868 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1869 if (gsym
->final_value_is_known())
1871 // For a STT_GNU_IFUNC symbol we want the PLT address.
1872 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1873 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
1875 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1879 // If this symbol is not fully resolved, we need to add a
1880 // dynamic relocation for it.
1881 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1882 if (gsym
->is_from_dynobj()
1883 || gsym
->is_undefined()
1884 || gsym
->is_preemptible()
1885 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
1886 && parameters
->options().output_is_position_independent()))
1887 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1888 elfcpp::R_X86_64_GLOB_DAT
);
1891 // For a STT_GNU_IFUNC symbol we want to write the PLT
1892 // offset into the GOT, so that function pointer
1893 // comparisons work correctly.
1895 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
1896 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1899 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
1900 // Tell the dynamic linker to use the PLT address
1901 // when resolving relocations.
1902 if (gsym
->is_from_dynobj()
1903 && !parameters
->options().shared())
1904 gsym
->set_needs_dynsym_value();
1908 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1909 rela_dyn
->add_global_relative(gsym
,
1910 elfcpp::R_X86_64_RELATIVE
,
1915 // For GOTPLT64, we also need a PLT entry (but only if the
1916 // symbol is not fully resolved).
1917 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1918 && !gsym
->final_value_is_known())
1919 target
->make_plt_entry(symtab
, layout
, gsym
);
1923 case elfcpp::R_X86_64_PLT32
:
1924 // If the symbol is fully resolved, this is just a PC32 reloc.
1925 // Otherwise we need a PLT entry.
1926 if (gsym
->final_value_is_known())
1928 // If building a shared library, we can also skip the PLT entry
1929 // if the symbol is defined in the output file and is protected
1931 if (gsym
->is_defined()
1932 && !gsym
->is_from_dynobj()
1933 && !gsym
->is_preemptible())
1935 target
->make_plt_entry(symtab
, layout
, gsym
);
1938 case elfcpp::R_X86_64_GOTPC32
:
1939 case elfcpp::R_X86_64_GOTOFF64
:
1940 case elfcpp::R_X86_64_GOTPC64
:
1941 case elfcpp::R_X86_64_PLTOFF64
:
1942 // We need a GOT section.
1943 target
->got_section(symtab
, layout
);
1944 // For PLTOFF64, we also need a PLT entry (but only if the
1945 // symbol is not fully resolved).
1946 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1947 && !gsym
->final_value_is_known())
1948 target
->make_plt_entry(symtab
, layout
, gsym
);
1951 case elfcpp::R_X86_64_COPY
:
1952 case elfcpp::R_X86_64_GLOB_DAT
:
1953 case elfcpp::R_X86_64_JUMP_SLOT
:
1954 case elfcpp::R_X86_64_RELATIVE
:
1955 case elfcpp::R_X86_64_IRELATIVE
:
1956 // These are outstanding tls relocs, which are unexpected when linking
1957 case elfcpp::R_X86_64_TPOFF64
:
1958 case elfcpp::R_X86_64_DTPMOD64
:
1959 case elfcpp::R_X86_64_TLSDESC
:
1960 gold_error(_("%s: unexpected reloc %u in object file"),
1961 object
->name().c_str(), r_type
);
1964 // These are initial tls relocs, which are expected for global()
1965 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1966 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1967 case elfcpp::R_X86_64_TLSDESC_CALL
:
1968 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1969 case elfcpp::R_X86_64_DTPOFF32
:
1970 case elfcpp::R_X86_64_DTPOFF64
:
1971 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1972 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1974 const bool is_final
= gsym
->final_value_is_known();
1975 const tls::Tls_optimization optimized_type
1976 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1979 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1980 if (optimized_type
== tls::TLSOPT_NONE
)
1982 // Create a pair of GOT entries for the module index and
1983 // dtv-relative offset.
1984 Output_data_got
<64, false>* got
1985 = target
->got_section(symtab
, layout
);
1986 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_PAIR
,
1987 target
->rela_dyn_section(layout
),
1988 elfcpp::R_X86_64_DTPMOD64
,
1989 elfcpp::R_X86_64_DTPOFF64
);
1991 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1993 // Create a GOT entry for the tp-relative offset.
1994 Output_data_got
<64, false>* got
1995 = target
->got_section(symtab
, layout
);
1996 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1997 target
->rela_dyn_section(layout
),
1998 elfcpp::R_X86_64_TPOFF64
);
2000 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2001 unsupported_reloc_global(object
, r_type
, gsym
);
2004 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2005 target
->define_tls_base_symbol(symtab
, layout
);
2006 if (optimized_type
== tls::TLSOPT_NONE
)
2008 // Create reserved PLT and GOT entries for the resolver.
2009 target
->reserve_tlsdesc_entries(symtab
, layout
);
2011 // Create a double GOT entry with an R_X86_64_TLSDESC
2012 // reloc. The R_X86_64_TLSDESC reloc is resolved
2013 // lazily, so the GOT entry needs to be in an area in
2014 // .got.plt, not .got. Call got_section to make sure
2015 // the section has been created.
2016 target
->got_section(symtab
, layout
);
2017 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2018 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2019 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_DESC
, rt
,
2020 elfcpp::R_X86_64_TLSDESC
, 0);
2022 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2024 // Create a GOT entry for the tp-relative offset.
2025 Output_data_got
<64, false>* got
2026 = target
->got_section(symtab
, layout
);
2027 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
2028 target
->rela_dyn_section(layout
),
2029 elfcpp::R_X86_64_TPOFF64
);
2031 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2032 unsupported_reloc_global(object
, r_type
, gsym
);
2035 case elfcpp::R_X86_64_TLSDESC_CALL
:
2038 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2039 if (optimized_type
== tls::TLSOPT_NONE
)
2041 // Create a GOT entry for the module index.
2042 target
->got_mod_index_entry(symtab
, layout
, object
);
2044 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2045 unsupported_reloc_global(object
, r_type
, gsym
);
2048 case elfcpp::R_X86_64_DTPOFF32
:
2049 case elfcpp::R_X86_64_DTPOFF64
:
2052 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2053 layout
->set_has_static_tls();
2054 if (optimized_type
== tls::TLSOPT_NONE
)
2056 // Create a GOT entry for the tp-relative offset.
2057 Output_data_got
<64, false>* got
2058 = target
->got_section(symtab
, layout
);
2059 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
2060 target
->rela_dyn_section(layout
),
2061 elfcpp::R_X86_64_TPOFF64
);
2063 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2064 unsupported_reloc_global(object
, r_type
, gsym
);
2067 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2068 layout
->set_has_static_tls();
2069 if (parameters
->options().shared())
2070 unsupported_reloc_local(object
, r_type
);
2079 case elfcpp::R_X86_64_SIZE32
:
2080 case elfcpp::R_X86_64_SIZE64
:
2082 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2083 object
->name().c_str(), r_type
,
2084 gsym
->demangled_name().c_str());
2090 Target_x86_64::gc_process_relocs(Symbol_table
* symtab
,
2092 Sized_relobj
<64, false>* object
,
2093 unsigned int data_shndx
,
2094 unsigned int sh_type
,
2095 const unsigned char* prelocs
,
2097 Output_section
* output_section
,
2098 bool needs_special_offset_handling
,
2099 size_t local_symbol_count
,
2100 const unsigned char* plocal_symbols
)
2103 if (sh_type
== elfcpp::SHT_REL
)
2108 gold::gc_process_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2109 Target_x86_64::Scan
,
2110 Target_x86_64::Relocatable_size_for_reloc
>(
2119 needs_special_offset_handling
,
2124 // Scan relocations for a section.
2127 Target_x86_64::scan_relocs(Symbol_table
* symtab
,
2129 Sized_relobj
<64, false>* object
,
2130 unsigned int data_shndx
,
2131 unsigned int sh_type
,
2132 const unsigned char* prelocs
,
2134 Output_section
* output_section
,
2135 bool needs_special_offset_handling
,
2136 size_t local_symbol_count
,
2137 const unsigned char* plocal_symbols
)
2139 if (sh_type
== elfcpp::SHT_REL
)
2141 gold_error(_("%s: unsupported REL reloc section"),
2142 object
->name().c_str());
2146 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2147 Target_x86_64::Scan
>(
2156 needs_special_offset_handling
,
2161 // Finalize the sections.
2164 Target_x86_64::do_finalize_sections(
2166 const Input_objects
*,
2167 Symbol_table
* symtab
)
2169 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2171 : this->plt_
->rela_plt());
2172 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
2173 this->rela_dyn_
, true, false);
2175 // Fill in some more dynamic tags.
2176 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
2179 if (this->plt_
!= NULL
2180 && this->plt_
->output_section() != NULL
2181 && this->plt_
->has_tlsdesc_entry())
2183 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
2184 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
2185 this->got_
->finalize_data_size();
2186 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
2187 this->plt_
, plt_offset
);
2188 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
2189 this->got_
, got_offset
);
2193 // Emit any relocs we saved in an attempt to avoid generating COPY
2195 if (this->copy_relocs_
.any_saved_relocs())
2196 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
2198 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2199 // the .got.plt section.
2200 Symbol
* sym
= this->global_offset_table_
;
2203 uint64_t data_size
= this->got_plt_
->current_data_size();
2204 symtab
->get_sized_symbol
<64>(sym
)->set_symsize(data_size
);
2208 // Perform a relocation.
2211 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
2212 Target_x86_64
* target
,
2215 const elfcpp::Rela
<64, false>& rela
,
2216 unsigned int r_type
,
2217 const Sized_symbol
<64>* gsym
,
2218 const Symbol_value
<64>* psymval
,
2219 unsigned char* view
,
2220 elfcpp::Elf_types
<64>::Elf_Addr address
,
2221 section_size_type view_size
)
2223 if (this->skip_call_tls_get_addr_
)
2225 if ((r_type
!= elfcpp::R_X86_64_PLT32
2226 && r_type
!= elfcpp::R_X86_64_PC32
)
2228 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
2230 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2231 _("missing expected TLS relocation"));
2235 this->skip_call_tls_get_addr_
= false;
2240 const Sized_relobj
<64, false>* object
= relinfo
->object
;
2242 // Pick the value to use for symbols defined in the PLT.
2243 Symbol_value
<64> symval
;
2245 && gsym
->use_plt_offset(r_type
== elfcpp::R_X86_64_PC64
2246 || r_type
== elfcpp::R_X86_64_PC32
2247 || r_type
== elfcpp::R_X86_64_PC16
2248 || r_type
== elfcpp::R_X86_64_PC8
))
2250 symval
.set_output_value(target
->plt_section()->address()
2251 + gsym
->plt_offset());
2254 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
2256 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2257 if (object
->local_has_plt_offset(r_sym
))
2259 symval
.set_output_value(target
->plt_section()->address()
2260 + object
->local_plt_offset(r_sym
));
2265 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2267 // Get the GOT offset if needed.
2268 // The GOT pointer points to the end of the GOT section.
2269 // We need to subtract the size of the GOT section to get
2270 // the actual offset to use in the relocation.
2271 bool have_got_offset
= false;
2272 unsigned int got_offset
= 0;
2275 case elfcpp::R_X86_64_GOT32
:
2276 case elfcpp::R_X86_64_GOT64
:
2277 case elfcpp::R_X86_64_GOTPLT64
:
2278 case elfcpp::R_X86_64_GOTPCREL
:
2279 case elfcpp::R_X86_64_GOTPCREL64
:
2282 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2283 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
2287 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2288 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2289 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
2290 - target
->got_size());
2292 have_got_offset
= true;
2301 case elfcpp::R_X86_64_NONE
:
2302 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2303 case elfcpp::R_X86_64_GNU_VTENTRY
:
2306 case elfcpp::R_X86_64_64
:
2307 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
2310 case elfcpp::R_X86_64_PC64
:
2311 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
2315 case elfcpp::R_X86_64_32
:
2316 // FIXME: we need to verify that value + addend fits into 32 bits:
2317 // uint64_t x = value + addend;
2318 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2319 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2320 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
2323 case elfcpp::R_X86_64_32S
:
2324 // FIXME: we need to verify that value + addend fits into 32 bits:
2325 // int64_t x = value + addend; // note this quantity is signed!
2326 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2327 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
2330 case elfcpp::R_X86_64_PC32
:
2331 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
2335 case elfcpp::R_X86_64_16
:
2336 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
2339 case elfcpp::R_X86_64_PC16
:
2340 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
2344 case elfcpp::R_X86_64_8
:
2345 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
2348 case elfcpp::R_X86_64_PC8
:
2349 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
2353 case elfcpp::R_X86_64_PLT32
:
2354 gold_assert(gsym
== NULL
2355 || gsym
->has_plt_offset()
2356 || gsym
->final_value_is_known()
2357 || (gsym
->is_defined()
2358 && !gsym
->is_from_dynobj()
2359 && !gsym
->is_preemptible()));
2360 // Note: while this code looks the same as for R_X86_64_PC32, it
2361 // behaves differently because psymval was set to point to
2362 // the PLT entry, rather than the symbol, in Scan::global().
2363 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
2367 case elfcpp::R_X86_64_PLTOFF64
:
2370 gold_assert(gsym
->has_plt_offset()
2371 || gsym
->final_value_is_known());
2372 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
2373 got_address
= target
->got_section(NULL
, NULL
)->address();
2374 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
2375 addend
- got_address
);
2378 case elfcpp::R_X86_64_GOT32
:
2379 gold_assert(have_got_offset
);
2380 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
2383 case elfcpp::R_X86_64_GOTPC32
:
2386 elfcpp::Elf_types
<64>::Elf_Addr value
;
2387 value
= target
->got_plt_section()->address();
2388 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2392 case elfcpp::R_X86_64_GOT64
:
2393 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2394 // Since we always add a PLT entry, this is equivalent.
2395 case elfcpp::R_X86_64_GOTPLT64
:
2396 gold_assert(have_got_offset
);
2397 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
2400 case elfcpp::R_X86_64_GOTPC64
:
2403 elfcpp::Elf_types
<64>::Elf_Addr value
;
2404 value
= target
->got_plt_section()->address();
2405 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
2409 case elfcpp::R_X86_64_GOTOFF64
:
2411 elfcpp::Elf_types
<64>::Elf_Addr value
;
2412 value
= (psymval
->value(object
, 0)
2413 - target
->got_plt_section()->address());
2414 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
2418 case elfcpp::R_X86_64_GOTPCREL
:
2420 gold_assert(have_got_offset
);
2421 elfcpp::Elf_types
<64>::Elf_Addr value
;
2422 value
= target
->got_plt_section()->address() + got_offset
;
2423 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2427 case elfcpp::R_X86_64_GOTPCREL64
:
2429 gold_assert(have_got_offset
);
2430 elfcpp::Elf_types
<64>::Elf_Addr value
;
2431 value
= target
->got_plt_section()->address() + got_offset
;
2432 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
2436 case elfcpp::R_X86_64_COPY
:
2437 case elfcpp::R_X86_64_GLOB_DAT
:
2438 case elfcpp::R_X86_64_JUMP_SLOT
:
2439 case elfcpp::R_X86_64_RELATIVE
:
2440 case elfcpp::R_X86_64_IRELATIVE
:
2441 // These are outstanding tls relocs, which are unexpected when linking
2442 case elfcpp::R_X86_64_TPOFF64
:
2443 case elfcpp::R_X86_64_DTPMOD64
:
2444 case elfcpp::R_X86_64_TLSDESC
:
2445 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2446 _("unexpected reloc %u in object file"),
2450 // These are initial tls relocs, which are expected when linking
2451 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2452 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2453 case elfcpp::R_X86_64_TLSDESC_CALL
:
2454 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2455 case elfcpp::R_X86_64_DTPOFF32
:
2456 case elfcpp::R_X86_64_DTPOFF64
:
2457 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2458 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2459 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
2460 view
, address
, view_size
);
2463 case elfcpp::R_X86_64_SIZE32
:
2464 case elfcpp::R_X86_64_SIZE64
:
2466 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2467 _("unsupported reloc %u"),
2475 // Perform a TLS relocation.
2478 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
2479 Target_x86_64
* target
,
2481 const elfcpp::Rela
<64, false>& rela
,
2482 unsigned int r_type
,
2483 const Sized_symbol
<64>* gsym
,
2484 const Symbol_value
<64>* psymval
,
2485 unsigned char* view
,
2486 elfcpp::Elf_types
<64>::Elf_Addr address
,
2487 section_size_type view_size
)
2489 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2491 const Sized_relobj
<64, false>* object
= relinfo
->object
;
2492 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2493 elfcpp::Shdr
<64, false> data_shdr(relinfo
->data_shdr
);
2494 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
2496 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
2498 const bool is_final
= (gsym
== NULL
2499 ? !parameters
->options().shared()
2500 : gsym
->final_value_is_known());
2501 tls::Tls_optimization optimized_type
2502 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
2505 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2506 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2508 // If this code sequence is used in a non-executable section,
2509 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2510 // on the assumption that it's being used by itself in a debug
2511 // section. Therefore, in the unlikely event that the code
2512 // sequence appears in a non-executable section, we simply
2513 // leave it unoptimized.
2514 optimized_type
= tls::TLSOPT_NONE
;
2516 if (optimized_type
== tls::TLSOPT_TO_LE
)
2518 gold_assert(tls_segment
!= NULL
);
2519 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
2520 rela
, r_type
, value
, view
,
2526 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2527 ? GOT_TYPE_TLS_OFFSET
2528 : GOT_TYPE_TLS_PAIR
);
2529 unsigned int got_offset
;
2532 gold_assert(gsym
->has_got_offset(got_type
));
2533 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2537 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2538 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2539 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2540 - target
->got_size());
2542 if (optimized_type
== tls::TLSOPT_TO_IE
)
2544 gold_assert(tls_segment
!= NULL
);
2545 value
= target
->got_plt_section()->address() + got_offset
;
2546 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2547 value
, view
, address
, view_size
);
2550 else if (optimized_type
== tls::TLSOPT_NONE
)
2552 // Relocate the field with the offset of the pair of GOT
2554 value
= target
->got_plt_section()->address() + got_offset
;
2555 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2560 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2561 _("unsupported reloc %u"), r_type
);
2564 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2565 case elfcpp::R_X86_64_TLSDESC_CALL
:
2566 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2568 // See above comment for R_X86_64_TLSGD.
2569 optimized_type
= tls::TLSOPT_NONE
;
2571 if (optimized_type
== tls::TLSOPT_TO_LE
)
2573 gold_assert(tls_segment
!= NULL
);
2574 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2575 rela
, r_type
, value
, view
,
2581 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2582 ? GOT_TYPE_TLS_OFFSET
2583 : GOT_TYPE_TLS_DESC
);
2584 unsigned int got_offset
= 0;
2585 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
2586 && optimized_type
== tls::TLSOPT_NONE
)
2588 // We created GOT entries in the .got.tlsdesc portion of
2589 // the .got.plt section, but the offset stored in the
2590 // symbol is the offset within .got.tlsdesc.
2591 got_offset
= (target
->got_size()
2592 + target
->got_plt_section()->data_size());
2596 gold_assert(gsym
->has_got_offset(got_type
));
2597 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
2601 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2602 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2603 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
2604 - target
->got_size());
2606 if (optimized_type
== tls::TLSOPT_TO_IE
)
2608 gold_assert(tls_segment
!= NULL
);
2609 value
= target
->got_plt_section()->address() + got_offset
;
2610 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
2611 rela
, r_type
, value
, view
, address
,
2615 else if (optimized_type
== tls::TLSOPT_NONE
)
2617 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2619 // Relocate the field with the offset of the pair of GOT
2621 value
= target
->got_plt_section()->address() + got_offset
;
2622 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2628 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2629 _("unsupported reloc %u"), r_type
);
2632 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2633 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2635 // See above comment for R_X86_64_TLSGD.
2636 optimized_type
= tls::TLSOPT_NONE
;
2638 if (optimized_type
== tls::TLSOPT_TO_LE
)
2640 gold_assert(tls_segment
!= NULL
);
2641 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2642 value
, view
, view_size
);
2645 else if (optimized_type
== tls::TLSOPT_NONE
)
2647 // Relocate the field with the offset of the GOT entry for
2648 // the module index.
2649 unsigned int got_offset
;
2650 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2651 - target
->got_size());
2652 value
= target
->got_plt_section()->address() + got_offset
;
2653 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2657 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2658 _("unsupported reloc %u"), r_type
);
2661 case elfcpp::R_X86_64_DTPOFF32
:
2662 // This relocation type is used in debugging information.
2663 // In that case we need to not optimize the value. If the
2664 // section is not executable, then we assume we should not
2665 // optimize this reloc. See comments above for R_X86_64_TLSGD,
2666 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
2668 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
2670 gold_assert(tls_segment
!= NULL
);
2671 value
-= tls_segment
->memsz();
2673 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2676 case elfcpp::R_X86_64_DTPOFF64
:
2677 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
2678 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
2680 gold_assert(tls_segment
!= NULL
);
2681 value
-= tls_segment
->memsz();
2683 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
2686 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2687 if (optimized_type
== tls::TLSOPT_TO_LE
)
2689 gold_assert(tls_segment
!= NULL
);
2690 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2691 rela
, r_type
, value
, view
,
2695 else if (optimized_type
== tls::TLSOPT_NONE
)
2697 // Relocate the field with the offset of the GOT entry for
2698 // the tp-relative offset of the symbol.
2699 unsigned int got_offset
;
2702 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
2703 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
2704 - target
->got_size());
2708 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2709 gold_assert(object
->local_has_got_offset(r_sym
,
2710 GOT_TYPE_TLS_OFFSET
));
2711 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
2712 - target
->got_size());
2714 value
= target
->got_plt_section()->address() + got_offset
;
2715 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2718 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2719 _("unsupported reloc type %u"),
2723 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2724 value
-= tls_segment
->memsz();
2725 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2730 // Do a relocation in which we convert a TLS General-Dynamic to an
2734 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
2737 const elfcpp::Rela
<64, false>& rela
,
2739 elfcpp::Elf_types
<64>::Elf_Addr value
,
2740 unsigned char* view
,
2741 elfcpp::Elf_types
<64>::Elf_Addr address
,
2742 section_size_type view_size
)
2744 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2745 // .word 0x6666; rex64; call __tls_get_addr
2746 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2748 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2749 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2751 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2752 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2753 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2754 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2756 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2758 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2759 Relocate_functions
<64, false>::pcrela32(view
+ 8, value
, addend
- 8, address
);
2761 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2763 this->skip_call_tls_get_addr_
= true;
2766 // Do a relocation in which we convert a TLS General-Dynamic to a
2770 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
2772 Output_segment
* tls_segment
,
2773 const elfcpp::Rela
<64, false>& rela
,
2775 elfcpp::Elf_types
<64>::Elf_Addr value
,
2776 unsigned char* view
,
2777 section_size_type view_size
)
2779 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2780 // .word 0x6666; rex64; call __tls_get_addr
2781 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2783 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2784 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2786 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2787 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2788 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2789 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2791 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2793 value
-= tls_segment
->memsz();
2794 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
2796 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2798 this->skip_call_tls_get_addr_
= true;
2801 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2804 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2805 const Relocate_info
<64, false>* relinfo
,
2808 const elfcpp::Rela
<64, false>& rela
,
2809 unsigned int r_type
,
2810 elfcpp::Elf_types
<64>::Elf_Addr value
,
2811 unsigned char* view
,
2812 elfcpp::Elf_types
<64>::Elf_Addr address
,
2813 section_size_type view_size
)
2815 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2817 // leaq foo@tlsdesc(%rip), %rax
2818 // ==> movq foo@gottpoff(%rip), %rax
2819 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2820 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2821 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2822 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2824 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2825 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2829 // call *foo@tlscall(%rax)
2831 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2832 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2833 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2834 view
[0] == 0xff && view
[1] == 0x10);
2840 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2843 Target_x86_64::Relocate::tls_desc_gd_to_le(
2844 const Relocate_info
<64, false>* relinfo
,
2846 Output_segment
* tls_segment
,
2847 const elfcpp::Rela
<64, false>& rela
,
2848 unsigned int r_type
,
2849 elfcpp::Elf_types
<64>::Elf_Addr value
,
2850 unsigned char* view
,
2851 section_size_type view_size
)
2853 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2855 // leaq foo@tlsdesc(%rip), %rax
2856 // ==> movq foo@tpoff, %rax
2857 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2858 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2859 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2860 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2863 value
-= tls_segment
->memsz();
2864 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2868 // call *foo@tlscall(%rax)
2870 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2871 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2872 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2873 view
[0] == 0xff && view
[1] == 0x10);
2880 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
2883 const elfcpp::Rela
<64, false>& rela
,
2885 elfcpp::Elf_types
<64>::Elf_Addr
,
2886 unsigned char* view
,
2887 section_size_type view_size
)
2889 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2890 // ... leq foo@dtpoff(%rax),%reg
2891 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2893 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2894 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
2896 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2897 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
2899 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
2901 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2903 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2905 this->skip_call_tls_get_addr_
= true;
2908 // Do a relocation in which we convert a TLS Initial-Exec to a
2912 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
2914 Output_segment
* tls_segment
,
2915 const elfcpp::Rela
<64, false>& rela
,
2917 elfcpp::Elf_types
<64>::Elf_Addr value
,
2918 unsigned char* view
,
2919 section_size_type view_size
)
2921 // We need to examine the opcodes to figure out which instruction we
2924 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2925 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2927 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2928 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2930 unsigned char op1
= view
[-3];
2931 unsigned char op2
= view
[-2];
2932 unsigned char op3
= view
[-1];
2933 unsigned char reg
= op3
>> 3;
2941 view
[-1] = 0xc0 | reg
;
2945 // Special handling for %rsp.
2949 view
[-1] = 0xc0 | reg
;
2957 view
[-1] = 0x80 | reg
| (reg
<< 3);
2960 value
-= tls_segment
->memsz();
2961 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2964 // Relocate section data.
2967 Target_x86_64::relocate_section(
2968 const Relocate_info
<64, false>* relinfo
,
2969 unsigned int sh_type
,
2970 const unsigned char* prelocs
,
2972 Output_section
* output_section
,
2973 bool needs_special_offset_handling
,
2974 unsigned char* view
,
2975 elfcpp::Elf_types
<64>::Elf_Addr address
,
2976 section_size_type view_size
,
2977 const Reloc_symbol_changes
* reloc_symbol_changes
)
2979 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2981 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2982 Target_x86_64::Relocate
>(
2988 needs_special_offset_handling
,
2992 reloc_symbol_changes
);
2995 // Return the size of a relocation while scanning during a relocatable
2999 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3000 unsigned int r_type
,
3005 case elfcpp::R_X86_64_NONE
:
3006 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3007 case elfcpp::R_X86_64_GNU_VTENTRY
:
3008 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3009 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3010 case elfcpp::R_X86_64_TLSDESC_CALL
:
3011 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3012 case elfcpp::R_X86_64_DTPOFF32
:
3013 case elfcpp::R_X86_64_DTPOFF64
:
3014 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3015 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3018 case elfcpp::R_X86_64_64
:
3019 case elfcpp::R_X86_64_PC64
:
3020 case elfcpp::R_X86_64_GOTOFF64
:
3021 case elfcpp::R_X86_64_GOTPC64
:
3022 case elfcpp::R_X86_64_PLTOFF64
:
3023 case elfcpp::R_X86_64_GOT64
:
3024 case elfcpp::R_X86_64_GOTPCREL64
:
3025 case elfcpp::R_X86_64_GOTPCREL
:
3026 case elfcpp::R_X86_64_GOTPLT64
:
3029 case elfcpp::R_X86_64_32
:
3030 case elfcpp::R_X86_64_32S
:
3031 case elfcpp::R_X86_64_PC32
:
3032 case elfcpp::R_X86_64_PLT32
:
3033 case elfcpp::R_X86_64_GOTPC32
:
3034 case elfcpp::R_X86_64_GOT32
:
3037 case elfcpp::R_X86_64_16
:
3038 case elfcpp::R_X86_64_PC16
:
3041 case elfcpp::R_X86_64_8
:
3042 case elfcpp::R_X86_64_PC8
:
3045 case elfcpp::R_X86_64_COPY
:
3046 case elfcpp::R_X86_64_GLOB_DAT
:
3047 case elfcpp::R_X86_64_JUMP_SLOT
:
3048 case elfcpp::R_X86_64_RELATIVE
:
3049 case elfcpp::R_X86_64_IRELATIVE
:
3050 // These are outstanding tls relocs, which are unexpected when linking
3051 case elfcpp::R_X86_64_TPOFF64
:
3052 case elfcpp::R_X86_64_DTPMOD64
:
3053 case elfcpp::R_X86_64_TLSDESC
:
3054 object
->error(_("unexpected reloc %u in object file"), r_type
);
3057 case elfcpp::R_X86_64_SIZE32
:
3058 case elfcpp::R_X86_64_SIZE64
:
3060 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
3065 // Scan the relocs during a relocatable link.
3068 Target_x86_64::scan_relocatable_relocs(Symbol_table
* symtab
,
3070 Sized_relobj
<64, false>* object
,
3071 unsigned int data_shndx
,
3072 unsigned int sh_type
,
3073 const unsigned char* prelocs
,
3075 Output_section
* output_section
,
3076 bool needs_special_offset_handling
,
3077 size_t local_symbol_count
,
3078 const unsigned char* plocal_symbols
,
3079 Relocatable_relocs
* rr
)
3081 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3083 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
3084 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
3086 gold::scan_relocatable_relocs
<64, false, elfcpp::SHT_RELA
,
3087 Scan_relocatable_relocs
>(
3095 needs_special_offset_handling
,
3101 // Relocate a section during a relocatable link.
3104 Target_x86_64::relocate_for_relocatable(
3105 const Relocate_info
<64, false>* relinfo
,
3106 unsigned int sh_type
,
3107 const unsigned char* prelocs
,
3109 Output_section
* output_section
,
3110 off_t offset_in_output_section
,
3111 const Relocatable_relocs
* rr
,
3112 unsigned char* view
,
3113 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
3114 section_size_type view_size
,
3115 unsigned char* reloc_view
,
3116 section_size_type reloc_view_size
)
3118 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3120 gold::relocate_for_relocatable
<64, false, elfcpp::SHT_RELA
>(
3125 offset_in_output_section
,
3134 // Return the value to use for a dynamic which requires special
3135 // treatment. This is how we support equality comparisons of function
3136 // pointers across shared library boundaries, as described in the
3137 // processor specific ABI supplement.
3140 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
3142 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
3143 return this->plt_section()->address() + gsym
->plt_offset();
3146 // Return a string used to fill a code section with nops to take up
3147 // the specified length.
3150 Target_x86_64::do_code_fill(section_size_type length
) const
3154 // Build a jmpq instruction to skip over the bytes.
3155 unsigned char jmp
[5];
3157 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
3158 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
3159 + std::string(length
- 5, '\0'));
3162 // Nop sequences of various lengths.
3163 const char nop1
[1] = { 0x90 }; // nop
3164 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
3165 const char nop3
[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3166 const char nop4
[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3167 const char nop5
[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3169 const char nop6
[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3171 const char nop7
[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3173 const char nop8
[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3174 0x00, 0x00, 0x00, 0x00 };
3175 const char nop9
[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3176 0x00, 0x00, 0x00, 0x00,
3178 const char nop10
[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3179 0x84, 0x00, 0x00, 0x00,
3181 const char nop11
[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3182 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3184 const char nop12
[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3185 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3186 0x00, 0x00, 0x00, 0x00 };
3187 const char nop13
[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3188 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3189 0x00, 0x00, 0x00, 0x00,
3191 const char nop14
[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3192 0x66, 0x2e, 0x0f, 0x1f, // data16
3193 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3195 const char nop15
[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3196 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3197 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3200 const char* nops
[16] = {
3202 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
3203 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
3206 return std::string(nops
[length
], length
);
3209 // Return the addend to use for a target specific relocation. The
3210 // only target specific relocation is R_X86_64_TLSDESC for a local
3211 // symbol. We want to set the addend is the offset of the local
3212 // symbol in the TLS segment.
3215 Target_x86_64::do_reloc_addend(void* arg
, unsigned int r_type
,
3218 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
3219 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
3220 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
3221 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
3222 const Symbol_value
<64>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
3223 gold_assert(psymval
->is_tls_symbol());
3224 // The value of a TLS symbol is the offset in the TLS segment.
3225 return psymval
->value(ti
.object
, 0);
3228 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3229 // compiled with -fstack-split. The function calls non-stack-split
3230 // code. We have to change the function so that it always ensures
3231 // that it has enough stack space to run some random function.
3234 Target_x86_64::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
3235 section_offset_type fnoffset
,
3236 section_size_type fnsize
,
3237 unsigned char* view
,
3238 section_size_type view_size
,
3240 std::string
* to
) const
3242 // The function starts with a comparison of the stack pointer and a
3243 // field in the TCB. This is followed by a jump.
3246 if (this->match_view(view
, view_size
, fnoffset
, "\x64\x48\x3b\x24\x25", 5)
3249 // We will call __morestack if the carry flag is set after this
3250 // comparison. We turn the comparison into an stc instruction
3252 view
[fnoffset
] = '\xf9';
3253 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 8);
3255 // lea NN(%rsp),%r10
3256 // lea NN(%rsp),%r11
3257 else if ((this->match_view(view
, view_size
, fnoffset
,
3258 "\x4c\x8d\x94\x24", 4)
3259 || this->match_view(view
, view_size
, fnoffset
,
3260 "\x4c\x8d\x9c\x24", 4))
3263 // This is loading an offset from the stack pointer for a
3264 // comparison. The offset is negative, so we decrease the
3265 // offset by the amount of space we need for the stack. This
3266 // means we will avoid calling __morestack if there happens to
3267 // be plenty of space on the stack already.
3268 unsigned char* pval
= view
+ fnoffset
+ 4;
3269 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
3270 val
-= parameters
->options().split_stack_adjust_size();
3271 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
3275 if (!object
->has_no_split_stack())
3276 object
->error(_("failed to match split-stack sequence at "
3277 "section %u offset %0zx"),
3278 shndx
, static_cast<size_t>(fnoffset
));
3282 // We have to change the function so that it calls
3283 // __morestack_non_split instead of __morestack. The former will
3284 // allocate additional stack space.
3285 *from
= "__morestack";
3286 *to
= "__morestack_non_split";
3289 // The selector for x86_64 object files.
3291 class Target_selector_x86_64
: public Target_selector_freebsd
3294 Target_selector_x86_64()
3295 : Target_selector_freebsd(elfcpp::EM_X86_64
, 64, false, "elf64-x86-64",
3296 "elf64-x86-64-freebsd")
3300 do_instantiate_target()
3301 { return new Target_x86_64(); }
3305 Target_selector_x86_64 target_selector_x86_64
;
3307 } // End anonymous namespace.