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 -fsplit-stack code which calls non-split-stack 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 // Apply an incremental relocation.
350 apply_relocation(const Relocate_info
<64, false>* relinfo
,
351 elfcpp::Elf_types
<64>::Elf_Addr r_offset
,
353 elfcpp::Elf_types
<64>::Elf_Swxword r_addend
,
356 elfcpp::Elf_types
<64>::Elf_Addr address
,
357 section_size_type view_size
);
359 // Add a new reloc argument, returning the index in the vector.
361 add_tlsdesc_info(Sized_relobj
<64, false>* object
, unsigned int r_sym
)
363 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
364 return this->tlsdesc_reloc_info_
.size() - 1;
368 // The class which scans relocations.
373 : issued_non_pic_error_(false)
377 get_reference_flags(unsigned int r_type
);
380 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
381 Sized_relobj
<64, false>* object
,
382 unsigned int data_shndx
,
383 Output_section
* output_section
,
384 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
385 const elfcpp::Sym
<64, false>& lsym
);
388 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
389 Sized_relobj
<64, false>* object
,
390 unsigned int data_shndx
,
391 Output_section
* output_section
,
392 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
396 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
397 Target_x86_64
* target
,
398 Sized_relobj
<64, false>* object
,
399 unsigned int data_shndx
,
400 Output_section
* output_section
,
401 const elfcpp::Rela
<64, false>& reloc
,
403 const elfcpp::Sym
<64, false>& lsym
);
406 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
407 Target_x86_64
* target
,
408 Sized_relobj
<64, false>* object
,
409 unsigned int data_shndx
,
410 Output_section
* output_section
,
411 const elfcpp::Rela
<64, false>& reloc
,
417 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
420 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
424 check_non_pic(Relobj
*, unsigned int r_type
);
427 possible_function_pointer_reloc(unsigned int r_type
);
430 reloc_needs_plt_for_ifunc(Sized_relobj
<64, false>*, unsigned int r_type
);
432 // Whether we have issued an error about a non-PIC compilation.
433 bool issued_non_pic_error_
;
436 // The class which implements relocation.
441 : skip_call_tls_get_addr_(false)
446 if (this->skip_call_tls_get_addr_
)
448 // FIXME: This needs to specify the location somehow.
449 gold_error(_("missing expected TLS relocation"));
453 // Do a relocation. Return false if the caller should not issue
454 // any warnings about this relocation.
456 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, Output_section
*,
457 size_t relnum
, const elfcpp::Rela
<64, false>&,
458 unsigned int r_type
, const Sized_symbol
<64>*,
459 const Symbol_value
<64>*,
460 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
464 // Do a TLS relocation.
466 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
467 size_t relnum
, const elfcpp::Rela
<64, false>&,
468 unsigned int r_type
, const Sized_symbol
<64>*,
469 const Symbol_value
<64>*,
470 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
473 // Do a TLS General-Dynamic to Initial-Exec transition.
475 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
476 Output_segment
* tls_segment
,
477 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
478 elfcpp::Elf_types
<64>::Elf_Addr value
,
480 elfcpp::Elf_types
<64>::Elf_Addr
,
481 section_size_type view_size
);
483 // Do a TLS General-Dynamic to Local-Exec transition.
485 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
486 Output_segment
* tls_segment
,
487 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
488 elfcpp::Elf_types
<64>::Elf_Addr value
,
490 section_size_type view_size
);
492 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
494 tls_desc_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
495 Output_segment
* tls_segment
,
496 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
497 elfcpp::Elf_types
<64>::Elf_Addr value
,
499 elfcpp::Elf_types
<64>::Elf_Addr
,
500 section_size_type view_size
);
502 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
504 tls_desc_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
505 Output_segment
* tls_segment
,
506 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
507 elfcpp::Elf_types
<64>::Elf_Addr value
,
509 section_size_type view_size
);
511 // Do a TLS Local-Dynamic to Local-Exec transition.
513 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
514 Output_segment
* tls_segment
,
515 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
516 elfcpp::Elf_types
<64>::Elf_Addr value
,
518 section_size_type view_size
);
520 // Do a TLS Initial-Exec to Local-Exec transition.
522 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
523 Output_segment
* tls_segment
,
524 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
525 elfcpp::Elf_types
<64>::Elf_Addr value
,
527 section_size_type view_size
);
529 // This is set if we should skip the next reloc, which should be a
530 // PLT32 reloc against ___tls_get_addr.
531 bool skip_call_tls_get_addr_
;
534 // A class which returns the size required for a relocation type,
535 // used while scanning relocs during a relocatable link.
536 class Relocatable_size_for_reloc
540 get_size_for_reloc(unsigned int, Relobj
*);
543 // Adjust TLS relocation type based on the options and whether this
544 // is a local symbol.
545 static tls::Tls_optimization
546 optimize_tls_reloc(bool is_final
, int r_type
);
548 // Get the GOT section, creating it if necessary.
549 Output_data_got
<64, false>*
550 got_section(Symbol_table
*, Layout
*);
552 // Get the GOT PLT section.
554 got_plt_section() const
556 gold_assert(this->got_plt_
!= NULL
);
557 return this->got_plt_
;
560 // Get the GOT section for TLSDESC entries.
561 Output_data_got
<64, false>*
562 got_tlsdesc_section() const
564 gold_assert(this->got_tlsdesc_
!= NULL
);
565 return this->got_tlsdesc_
;
568 // Create the PLT section.
570 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
572 // Create a PLT entry for a global symbol.
574 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
576 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
578 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
579 Sized_relobj
<64, false>* relobj
,
580 unsigned int local_sym_index
);
582 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
584 define_tls_base_symbol(Symbol_table
*, Layout
*);
586 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
588 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
590 // Create a GOT entry for the TLS module index.
592 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
593 Sized_relobj
<64, false>* object
);
595 // Get the PLT section.
596 Output_data_plt_x86_64
*
599 gold_assert(this->plt_
!= NULL
);
603 // Get the dynamic reloc section, creating it if necessary.
605 rela_dyn_section(Layout
*);
607 // Get the section to use for TLSDESC relocations.
609 rela_tlsdesc_section(Layout
*) const;
611 // Add a potential copy relocation.
613 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
614 Sized_relobj
<64, false>* object
,
615 unsigned int shndx
, Output_section
* output_section
,
616 Symbol
* sym
, const elfcpp::Rela
<64, false>& reloc
)
618 this->copy_relocs_
.copy_reloc(symtab
, layout
,
619 symtab
->get_sized_symbol
<64>(sym
),
620 object
, shndx
, output_section
,
621 reloc
, this->rela_dyn_section(layout
));
624 // Information about this specific target which we pass to the
625 // general Target structure.
626 static const Target::Target_info x86_64_info
;
628 // The types of GOT entries needed for this platform.
629 // These values are exposed to the ABI in an incremental link.
630 // Do not renumber existing values without changing the version
631 // number of the .gnu_incremental_inputs section.
634 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
635 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
636 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
637 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
640 // This type is used as the argument to the target specific
641 // relocation routines. The only target specific reloc is
642 // R_X86_64_TLSDESC against a local symbol.
645 Tlsdesc_info(Sized_relobj
<64, false>* a_object
, unsigned int a_r_sym
)
646 : object(a_object
), r_sym(a_r_sym
)
649 // The object in which the local symbol is defined.
650 Sized_relobj
<64, false>* object
;
651 // The local symbol index in the object.
656 Output_data_got
<64, false>* got_
;
658 Output_data_plt_x86_64
* plt_
;
659 // The GOT PLT section.
660 Output_data_space
* got_plt_
;
661 // The GOT section for TLSDESC relocations.
662 Output_data_got
<64, false>* got_tlsdesc_
;
663 // The _GLOBAL_OFFSET_TABLE_ symbol.
664 Symbol
* global_offset_table_
;
665 // The dynamic reloc section.
666 Reloc_section
* rela_dyn_
;
667 // Relocs saved to avoid a COPY reloc.
668 Copy_relocs
<elfcpp::SHT_RELA
, 64, false> copy_relocs_
;
669 // Space for variables copied with a COPY reloc.
670 Output_data_space
* dynbss_
;
671 // Offset of the GOT entry for the TLS module index.
672 unsigned int got_mod_index_offset_
;
673 // We handle R_X86_64_TLSDESC against a local symbol as a target
674 // specific relocation. Here we store the object and local symbol
675 // index for the relocation.
676 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
677 // True if the _TLS_MODULE_BASE_ symbol has been defined.
678 bool tls_base_symbol_defined_
;
681 const Target::Target_info
Target_x86_64::x86_64_info
=
684 false, // is_big_endian
685 elfcpp::EM_X86_64
, // machine_code
686 false, // has_make_symbol
687 false, // has_resolve
688 true, // has_code_fill
689 true, // is_default_stack_executable
691 "/lib/ld64.so.1", // program interpreter
692 0x400000, // default_text_segment_address
693 0x1000, // abi_pagesize (overridable by -z max-page-size)
694 0x1000, // common_pagesize (overridable by -z common-page-size)
695 elfcpp::SHN_UNDEF
, // small_common_shndx
696 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
697 0, // small_common_section_flags
698 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
699 NULL
, // attributes_section
700 NULL
// attributes_vendor
703 // This is called when a new output section is created. This is where
704 // we handle the SHF_X86_64_LARGE.
707 Target_x86_64::do_new_output_section(Output_section
* os
) const
709 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
710 os
->set_is_large_section();
713 // Get the GOT section, creating it if necessary.
715 Output_data_got
<64, false>*
716 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
718 if (this->got_
== NULL
)
720 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
722 this->got_
= new Output_data_got
<64, false>();
724 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
726 | elfcpp::SHF_WRITE
),
727 this->got_
, ORDER_RELRO_LAST
,
730 this->got_plt_
= new Output_data_space(8, "** GOT PLT");
731 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
733 | elfcpp::SHF_WRITE
),
734 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
737 // The first three entries are reserved.
738 this->got_plt_
->set_current_data_size(3 * 8);
740 // Those bytes can go into the relro segment.
741 layout
->increase_relro(3 * 8);
743 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
744 this->global_offset_table_
=
745 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
746 Symbol_table::PREDEFINED
,
748 0, 0, elfcpp::STT_OBJECT
,
750 elfcpp::STV_HIDDEN
, 0,
753 // If there are any TLSDESC relocations, they get GOT entries in
754 // .got.plt after the jump slot entries.
755 this->got_tlsdesc_
= new Output_data_got
<64, false>();
756 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
758 | elfcpp::SHF_WRITE
),
760 ORDER_NON_RELRO_FIRST
, false);
766 // Get the dynamic reloc section, creating it if necessary.
768 Target_x86_64::Reloc_section
*
769 Target_x86_64::rela_dyn_section(Layout
* layout
)
771 if (this->rela_dyn_
== NULL
)
773 gold_assert(layout
!= NULL
);
774 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
775 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
776 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
777 ORDER_DYNAMIC_RELOCS
, false);
779 return this->rela_dyn_
;
782 // Create the PLT section. The ordinary .got section is an argument,
783 // since we need to refer to the start. We also create our own .got
784 // section just for PLT entries.
786 Output_data_plt_x86_64::Output_data_plt_x86_64(Symbol_table
* symtab
,
788 Output_data_got
<64, false>* got
,
789 Output_data_space
* got_plt
)
790 : Output_section_data(8), tlsdesc_rel_(NULL
), got_(got
), got_plt_(got_plt
),
791 count_(0), tlsdesc_got_offset_(-1U)
793 this->rel_
= new Reloc_section(false);
794 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
795 elfcpp::SHF_ALLOC
, this->rel_
,
796 ORDER_DYNAMIC_PLT_RELOCS
, false);
798 if (parameters
->doing_static_link())
800 // A statically linked executable will only have a .rela.plt
801 // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
802 // symbols. The library will use these symbols to locate the
803 // IRELATIVE relocs at program startup time.
804 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
805 Symbol_table::PREDEFINED
,
806 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
807 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
809 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
810 Symbol_table::PREDEFINED
,
811 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
812 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
818 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
820 os
->set_entsize(plt_entry_size
);
823 // Add an entry to the PLT.
826 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
828 gold_assert(!gsym
->has_plt_offset());
830 // Note that when setting the PLT offset we skip the initial
831 // reserved PLT entry.
832 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
836 section_offset_type got_offset
= this->got_plt_
->current_data_size();
838 // Every PLT entry needs a GOT entry which points back to the PLT
839 // entry (this will be changed by the dynamic linker, normally
840 // lazily when the function is called).
841 this->got_plt_
->set_current_data_size(got_offset
+ 8);
843 // Every PLT entry needs a reloc.
844 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
845 && gsym
->can_use_relative_reloc(false))
846 this->rel_
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
847 this->got_plt_
, got_offset
, 0);
850 gsym
->set_needs_dynsym_entry();
851 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
855 // Note that we don't need to save the symbol. The contents of the
856 // PLT are independent of which symbols are used. The symbols only
857 // appear in the relocations.
860 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
864 Output_data_plt_x86_64::add_local_ifunc_entry(Sized_relobj
<64, false>* relobj
,
865 unsigned int local_sym_index
)
867 unsigned int plt_offset
= (this->count_
+ 1) * plt_entry_size
;
870 section_offset_type got_offset
= this->got_plt_
->current_data_size();
872 // Every PLT entry needs a GOT entry which points back to the PLT
874 this->got_plt_
->set_current_data_size(got_offset
+ 8);
876 // Every PLT entry needs a reloc.
877 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
,
878 elfcpp::R_X86_64_IRELATIVE
,
879 this->got_plt_
, got_offset
, 0);
884 // Return where the TLSDESC relocations should go, creating it if
885 // necessary. These follow the JUMP_SLOT relocations.
887 Output_data_plt_x86_64::Reloc_section
*
888 Output_data_plt_x86_64::rela_tlsdesc(Layout
* layout
)
890 if (this->tlsdesc_rel_
== NULL
)
892 this->tlsdesc_rel_
= new Reloc_section(false);
893 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
894 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
895 ORDER_DYNAMIC_PLT_RELOCS
, false);
896 gold_assert(this->tlsdesc_rel_
->output_section() ==
897 this->rel_
->output_section());
899 return this->tlsdesc_rel_
;
902 // Set the final size.
904 Output_data_plt_x86_64::set_final_data_size()
906 unsigned int count
= this->count_
;
907 if (this->has_tlsdesc_entry())
909 this->set_data_size((count
+ 1) * plt_entry_size
);
912 // The first entry in the PLT for an executable.
914 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
916 // From AMD64 ABI Draft 0.98, page 76
917 0xff, 0x35, // pushq contents of memory address
918 0, 0, 0, 0, // replaced with address of .got + 8
919 0xff, 0x25, // jmp indirect
920 0, 0, 0, 0, // replaced with address of .got + 16
921 0x90, 0x90, 0x90, 0x90 // noop (x4)
924 // Subsequent entries in the PLT for an executable.
926 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
928 // From AMD64 ABI Draft 0.98, page 76
929 0xff, 0x25, // jmpq indirect
930 0, 0, 0, 0, // replaced with address of symbol in .got
931 0x68, // pushq immediate
932 0, 0, 0, 0, // replaced with offset into relocation table
933 0xe9, // jmpq relative
934 0, 0, 0, 0 // replaced with offset to start of .plt
937 // The reserved TLSDESC entry in the PLT for an executable.
939 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry
[plt_entry_size
] =
941 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
942 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
943 0xff, 0x35, // pushq x(%rip)
944 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
945 0xff, 0x25, // jmpq *y(%rip)
946 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
951 // Write out the PLT. This uses the hand-coded instructions above,
952 // and adjusts them as needed. This is specified by the AMD64 ABI.
955 Output_data_plt_x86_64::do_write(Output_file
* of
)
957 const off_t offset
= this->offset();
958 const section_size_type oview_size
=
959 convert_to_section_size_type(this->data_size());
960 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
962 const off_t got_file_offset
= this->got_plt_
->offset();
963 const section_size_type got_size
=
964 convert_to_section_size_type(this->got_plt_
->data_size());
965 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
968 unsigned char* pov
= oview
;
970 // The base address of the .plt section.
971 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
972 // The base address of the .got section.
973 elfcpp::Elf_types
<64>::Elf_Addr got_base
= this->got_
->address();
974 // The base address of the PLT portion of the .got section,
975 // which is where the GOT pointer will point, and where the
976 // three reserved GOT entries are located.
977 elfcpp::Elf_types
<64>::Elf_Addr got_address
= this->got_plt_
->address();
979 memcpy(pov
, first_plt_entry
, plt_entry_size
);
980 // We do a jmp relative to the PC at the end of this instruction.
981 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
983 - (plt_address
+ 6)));
984 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
986 - (plt_address
+ 12)));
987 pov
+= plt_entry_size
;
989 unsigned char* got_pov
= got_view
;
991 memset(got_pov
, 0, 24);
994 unsigned int plt_offset
= plt_entry_size
;
995 unsigned int got_offset
= 24;
996 const unsigned int count
= this->count_
;
997 for (unsigned int plt_index
= 0;
1000 pov
+= plt_entry_size
,
1002 plt_offset
+= plt_entry_size
,
1005 // Set and adjust the PLT entry itself.
1006 memcpy(pov
, plt_entry
, plt_entry_size
);
1007 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1008 (got_address
+ got_offset
1009 - (plt_address
+ plt_offset
1012 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1013 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1014 - (plt_offset
+ plt_entry_size
));
1016 // Set the entry in the GOT.
1017 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
1020 if (this->has_tlsdesc_entry())
1022 // Set and adjust the reserved TLSDESC PLT entry.
1023 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
1024 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1025 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1027 - (plt_address
+ plt_offset
1029 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1031 + tlsdesc_got_offset
1032 - (plt_address
+ plt_offset
1034 pov
+= plt_entry_size
;
1037 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1038 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1040 of
->write_output_view(offset
, oview_size
, oview
);
1041 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1044 // Create the PLT section.
1047 Target_x86_64::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1049 if (this->plt_
== NULL
)
1051 // Create the GOT sections first.
1052 this->got_section(symtab
, layout
);
1054 this->plt_
= new Output_data_plt_x86_64(symtab
, layout
, this->got_
,
1056 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1058 | elfcpp::SHF_EXECINSTR
),
1059 this->plt_
, ORDER_PLT
, false);
1061 // Make the sh_info field of .rela.plt point to .plt.
1062 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1063 rela_plt_os
->set_info_section(this->plt_
->output_section());
1067 // Return the section for TLSDESC relocations.
1069 Target_x86_64::Reloc_section
*
1070 Target_x86_64::rela_tlsdesc_section(Layout
* layout
) const
1072 return this->plt_section()->rela_tlsdesc(layout
);
1075 // Create a PLT entry for a global symbol.
1078 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1081 if (gsym
->has_plt_offset())
1084 if (this->plt_
== NULL
)
1085 this->make_plt_section(symtab
, layout
);
1087 this->plt_
->add_entry(gsym
);
1090 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1093 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1094 Sized_relobj
<64, false>* relobj
,
1095 unsigned int local_sym_index
)
1097 if (relobj
->local_has_plt_offset(local_sym_index
))
1099 if (this->plt_
== NULL
)
1100 this->make_plt_section(symtab
, layout
);
1101 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(relobj
,
1103 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1106 // Return the number of entries in the PLT.
1109 Target_x86_64::plt_entry_count() const
1111 if (this->plt_
== NULL
)
1113 return this->plt_
->entry_count();
1116 // Return the offset of the first non-reserved PLT entry.
1119 Target_x86_64::first_plt_entry_offset() const
1121 return Output_data_plt_x86_64::first_plt_entry_offset();
1124 // Return the size of each PLT entry.
1127 Target_x86_64::plt_entry_size() const
1129 return Output_data_plt_x86_64::get_plt_entry_size();
1132 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1135 Target_x86_64::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
1137 if (this->tls_base_symbol_defined_
)
1140 Output_segment
* tls_segment
= layout
->tls_segment();
1141 if (tls_segment
!= NULL
)
1143 bool is_exec
= parameters
->options().output_is_executable();
1144 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1145 Symbol_table::PREDEFINED
,
1149 elfcpp::STV_HIDDEN
, 0,
1151 ? Symbol::SEGMENT_END
1152 : Symbol::SEGMENT_START
),
1155 this->tls_base_symbol_defined_
= true;
1158 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1161 Target_x86_64::reserve_tlsdesc_entries(Symbol_table
* symtab
,
1164 if (this->plt_
== NULL
)
1165 this->make_plt_section(symtab
, layout
);
1167 if (!this->plt_
->has_tlsdesc_entry())
1169 // Allocate the TLSDESC_GOT entry.
1170 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1171 unsigned int got_offset
= got
->add_constant(0);
1173 // Allocate the TLSDESC_PLT entry.
1174 this->plt_
->reserve_tlsdesc_entry(got_offset
);
1178 // Create a GOT entry for the TLS module index.
1181 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1182 Sized_relobj
<64, false>* object
)
1184 if (this->got_mod_index_offset_
== -1U)
1186 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1187 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1188 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1189 unsigned int got_offset
= got
->add_constant(0);
1190 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
1192 got
->add_constant(0);
1193 this->got_mod_index_offset_
= got_offset
;
1195 return this->got_mod_index_offset_
;
1198 // Optimize the TLS relocation type based on what we know about the
1199 // symbol. IS_FINAL is true if the final address of this symbol is
1200 // known at link time.
1202 tls::Tls_optimization
1203 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
1205 // If we are generating a shared library, then we can't do anything
1207 if (parameters
->options().shared())
1208 return tls::TLSOPT_NONE
;
1212 case elfcpp::R_X86_64_TLSGD
:
1213 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1214 case elfcpp::R_X86_64_TLSDESC_CALL
:
1215 // These are General-Dynamic which permits fully general TLS
1216 // access. Since we know that we are generating an executable,
1217 // we can convert this to Initial-Exec. If we also know that
1218 // this is a local symbol, we can further switch to Local-Exec.
1220 return tls::TLSOPT_TO_LE
;
1221 return tls::TLSOPT_TO_IE
;
1223 case elfcpp::R_X86_64_TLSLD
:
1224 // This is Local-Dynamic, which refers to a local symbol in the
1225 // dynamic TLS block. Since we know that we generating an
1226 // executable, we can switch to Local-Exec.
1227 return tls::TLSOPT_TO_LE
;
1229 case elfcpp::R_X86_64_DTPOFF32
:
1230 case elfcpp::R_X86_64_DTPOFF64
:
1231 // Another Local-Dynamic reloc.
1232 return tls::TLSOPT_TO_LE
;
1234 case elfcpp::R_X86_64_GOTTPOFF
:
1235 // These are Initial-Exec relocs which get the thread offset
1236 // from the GOT. If we know that we are linking against the
1237 // local symbol, we can switch to Local-Exec, which links the
1238 // thread offset into the instruction.
1240 return tls::TLSOPT_TO_LE
;
1241 return tls::TLSOPT_NONE
;
1243 case elfcpp::R_X86_64_TPOFF32
:
1244 // When we already have Local-Exec, there is nothing further we
1246 return tls::TLSOPT_NONE
;
1253 // Get the Reference_flags for a particular relocation.
1256 Target_x86_64::Scan::get_reference_flags(unsigned int r_type
)
1260 case elfcpp::R_X86_64_NONE
:
1261 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1262 case elfcpp::R_X86_64_GNU_VTENTRY
:
1263 case elfcpp::R_X86_64_GOTPC32
:
1264 case elfcpp::R_X86_64_GOTPC64
:
1265 // No symbol reference.
1268 case elfcpp::R_X86_64_64
:
1269 case elfcpp::R_X86_64_32
:
1270 case elfcpp::R_X86_64_32S
:
1271 case elfcpp::R_X86_64_16
:
1272 case elfcpp::R_X86_64_8
:
1273 return Symbol::ABSOLUTE_REF
;
1275 case elfcpp::R_X86_64_PC64
:
1276 case elfcpp::R_X86_64_PC32
:
1277 case elfcpp::R_X86_64_PC16
:
1278 case elfcpp::R_X86_64_PC8
:
1279 case elfcpp::R_X86_64_GOTOFF64
:
1280 return Symbol::RELATIVE_REF
;
1282 case elfcpp::R_X86_64_PLT32
:
1283 case elfcpp::R_X86_64_PLTOFF64
:
1284 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
1286 case elfcpp::R_X86_64_GOT64
:
1287 case elfcpp::R_X86_64_GOT32
:
1288 case elfcpp::R_X86_64_GOTPCREL64
:
1289 case elfcpp::R_X86_64_GOTPCREL
:
1290 case elfcpp::R_X86_64_GOTPLT64
:
1292 return Symbol::ABSOLUTE_REF
;
1294 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1295 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1296 case elfcpp::R_X86_64_TLSDESC_CALL
:
1297 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1298 case elfcpp::R_X86_64_DTPOFF32
:
1299 case elfcpp::R_X86_64_DTPOFF64
:
1300 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1301 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1302 return Symbol::TLS_REF
;
1304 case elfcpp::R_X86_64_COPY
:
1305 case elfcpp::R_X86_64_GLOB_DAT
:
1306 case elfcpp::R_X86_64_JUMP_SLOT
:
1307 case elfcpp::R_X86_64_RELATIVE
:
1308 case elfcpp::R_X86_64_IRELATIVE
:
1309 case elfcpp::R_X86_64_TPOFF64
:
1310 case elfcpp::R_X86_64_DTPMOD64
:
1311 case elfcpp::R_X86_64_TLSDESC
:
1312 case elfcpp::R_X86_64_SIZE32
:
1313 case elfcpp::R_X86_64_SIZE64
:
1315 // Not expected. We will give an error later.
1320 // Report an unsupported relocation against a local symbol.
1323 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
1324 unsigned int r_type
)
1326 gold_error(_("%s: unsupported reloc %u against local symbol"),
1327 object
->name().c_str(), r_type
);
1330 // We are about to emit a dynamic relocation of type R_TYPE. If the
1331 // dynamic linker does not support it, issue an error. The GNU linker
1332 // only issues a non-PIC error for an allocated read-only section.
1333 // Here we know the section is allocated, but we don't know that it is
1334 // read-only. But we check for all the relocation types which the
1335 // glibc dynamic linker supports, so it seems appropriate to issue an
1336 // error even if the section is not read-only.
1339 Target_x86_64::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
)
1343 // These are the relocation types supported by glibc for x86_64
1344 // which should always work.
1345 case elfcpp::R_X86_64_RELATIVE
:
1346 case elfcpp::R_X86_64_IRELATIVE
:
1347 case elfcpp::R_X86_64_GLOB_DAT
:
1348 case elfcpp::R_X86_64_JUMP_SLOT
:
1349 case elfcpp::R_X86_64_DTPMOD64
:
1350 case elfcpp::R_X86_64_DTPOFF64
:
1351 case elfcpp::R_X86_64_TPOFF64
:
1352 case elfcpp::R_X86_64_64
:
1353 case elfcpp::R_X86_64_COPY
:
1356 // glibc supports these reloc types, but they can overflow.
1357 case elfcpp::R_X86_64_32
:
1358 case elfcpp::R_X86_64_PC32
:
1359 if (this->issued_non_pic_error_
)
1361 gold_assert(parameters
->options().output_is_position_independent());
1362 object
->error(_("requires dynamic reloc which may overflow at runtime; "
1363 "recompile with -fPIC"));
1364 this->issued_non_pic_error_
= true;
1368 // This prevents us from issuing more than one error per reloc
1369 // section. But we can still wind up issuing more than one
1370 // error per object file.
1371 if (this->issued_non_pic_error_
)
1373 gold_assert(parameters
->options().output_is_position_independent());
1374 object
->error(_("requires unsupported dynamic reloc; "
1375 "recompile with -fPIC"));
1376 this->issued_non_pic_error_
= true;
1379 case elfcpp::R_X86_64_NONE
:
1384 // Return whether we need to make a PLT entry for a relocation of the
1385 // given type against a STT_GNU_IFUNC symbol.
1388 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(Sized_relobj
<64, false>* object
,
1389 unsigned int r_type
)
1391 int flags
= Scan::get_reference_flags(r_type
);
1392 if (flags
& Symbol::TLS_REF
)
1393 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1394 object
->name().c_str(), r_type
);
1398 // Scan a relocation for a local symbol.
1401 Target_x86_64::Scan::local(Symbol_table
* symtab
,
1403 Target_x86_64
* target
,
1404 Sized_relobj
<64, false>* object
,
1405 unsigned int data_shndx
,
1406 Output_section
* output_section
,
1407 const elfcpp::Rela
<64, false>& reloc
,
1408 unsigned int r_type
,
1409 const elfcpp::Sym
<64, false>& lsym
)
1411 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1412 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
1413 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1415 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1416 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
1421 case elfcpp::R_X86_64_NONE
:
1422 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1423 case elfcpp::R_X86_64_GNU_VTENTRY
:
1426 case elfcpp::R_X86_64_64
:
1427 // If building a shared library (or a position-independent
1428 // executable), we need to create a dynamic relocation for this
1429 // location. The relocation applied at link time will apply the
1430 // link-time value, so we flag the location with an
1431 // R_X86_64_RELATIVE relocation so the dynamic loader can
1432 // relocate it easily.
1433 if (parameters
->options().output_is_position_independent())
1435 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1436 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1437 rela_dyn
->add_local_relative(object
, r_sym
,
1438 elfcpp::R_X86_64_RELATIVE
,
1439 output_section
, data_shndx
,
1440 reloc
.get_r_offset(),
1441 reloc
.get_r_addend());
1445 case elfcpp::R_X86_64_32
:
1446 case elfcpp::R_X86_64_32S
:
1447 case elfcpp::R_X86_64_16
:
1448 case elfcpp::R_X86_64_8
:
1449 // If building a shared library (or a position-independent
1450 // executable), we need to create a dynamic relocation for this
1451 // location. We can't use an R_X86_64_RELATIVE relocation
1452 // because that is always a 64-bit relocation.
1453 if (parameters
->options().output_is_position_independent())
1455 this->check_non_pic(object
, r_type
);
1457 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1458 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1459 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1460 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1461 data_shndx
, reloc
.get_r_offset(),
1462 reloc
.get_r_addend());
1465 gold_assert(lsym
.get_st_value() == 0);
1466 unsigned int shndx
= lsym
.get_st_shndx();
1468 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1471 object
->error(_("section symbol %u has bad shndx %u"),
1474 rela_dyn
->add_local_section(object
, shndx
,
1475 r_type
, output_section
,
1476 data_shndx
, reloc
.get_r_offset(),
1477 reloc
.get_r_addend());
1482 case elfcpp::R_X86_64_PC64
:
1483 case elfcpp::R_X86_64_PC32
:
1484 case elfcpp::R_X86_64_PC16
:
1485 case elfcpp::R_X86_64_PC8
:
1488 case elfcpp::R_X86_64_PLT32
:
1489 // Since we know this is a local symbol, we can handle this as a
1493 case elfcpp::R_X86_64_GOTPC32
:
1494 case elfcpp::R_X86_64_GOTOFF64
:
1495 case elfcpp::R_X86_64_GOTPC64
:
1496 case elfcpp::R_X86_64_PLTOFF64
:
1497 // We need a GOT section.
1498 target
->got_section(symtab
, layout
);
1499 // For PLTOFF64, we'd normally want a PLT section, but since we
1500 // know this is a local symbol, no PLT is needed.
1503 case elfcpp::R_X86_64_GOT64
:
1504 case elfcpp::R_X86_64_GOT32
:
1505 case elfcpp::R_X86_64_GOTPCREL64
:
1506 case elfcpp::R_X86_64_GOTPCREL
:
1507 case elfcpp::R_X86_64_GOTPLT64
:
1509 // The symbol requires a GOT entry.
1510 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1511 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1513 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1514 // lets function pointers compare correctly with shared
1515 // libraries. Otherwise we would need an IRELATIVE reloc.
1517 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
)
1518 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
1520 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1523 // If we are generating a shared object, we need to add a
1524 // dynamic relocation for this symbol's GOT entry.
1525 if (parameters
->options().output_is_position_independent())
1527 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1528 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1529 if (r_type
!= elfcpp::R_X86_64_GOT32
)
1531 unsigned int got_offset
=
1532 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1533 rela_dyn
->add_local_relative(object
, r_sym
,
1534 elfcpp::R_X86_64_RELATIVE
,
1535 got
, got_offset
, 0);
1539 this->check_non_pic(object
, r_type
);
1541 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1542 rela_dyn
->add_local(
1543 object
, r_sym
, r_type
, got
,
1544 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1548 // For GOTPLT64, we'd normally want a PLT section, but since
1549 // we know this is a local symbol, no PLT is needed.
1553 case elfcpp::R_X86_64_COPY
:
1554 case elfcpp::R_X86_64_GLOB_DAT
:
1555 case elfcpp::R_X86_64_JUMP_SLOT
:
1556 case elfcpp::R_X86_64_RELATIVE
:
1557 case elfcpp::R_X86_64_IRELATIVE
:
1558 // These are outstanding tls relocs, which are unexpected when linking
1559 case elfcpp::R_X86_64_TPOFF64
:
1560 case elfcpp::R_X86_64_DTPMOD64
:
1561 case elfcpp::R_X86_64_TLSDESC
:
1562 gold_error(_("%s: unexpected reloc %u in object file"),
1563 object
->name().c_str(), r_type
);
1566 // These are initial tls relocs, which are expected when linking
1567 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1568 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1569 case elfcpp::R_X86_64_TLSDESC_CALL
:
1570 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1571 case elfcpp::R_X86_64_DTPOFF32
:
1572 case elfcpp::R_X86_64_DTPOFF64
:
1573 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1574 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1576 bool output_is_shared
= parameters
->options().shared();
1577 const tls::Tls_optimization optimized_type
1578 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
1581 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1582 if (optimized_type
== tls::TLSOPT_NONE
)
1584 // Create a pair of GOT entries for the module index and
1585 // dtv-relative offset.
1586 Output_data_got
<64, false>* got
1587 = target
->got_section(symtab
, layout
);
1588 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1589 unsigned int shndx
= lsym
.get_st_shndx();
1591 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1593 object
->error(_("local symbol %u has bad shndx %u"),
1596 got
->add_local_pair_with_rela(object
, r_sym
,
1599 target
->rela_dyn_section(layout
),
1600 elfcpp::R_X86_64_DTPMOD64
, 0);
1602 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1603 unsupported_reloc_local(object
, r_type
);
1606 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1607 target
->define_tls_base_symbol(symtab
, layout
);
1608 if (optimized_type
== tls::TLSOPT_NONE
)
1610 // Create reserved PLT and GOT entries for the resolver.
1611 target
->reserve_tlsdesc_entries(symtab
, layout
);
1613 // Generate a double GOT entry with an
1614 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1615 // is resolved lazily, so the GOT entry needs to be in
1616 // an area in .got.plt, not .got. Call got_section to
1617 // make sure the section has been created.
1618 target
->got_section(symtab
, layout
);
1619 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
1620 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1621 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
1623 unsigned int got_offset
= got
->add_constant(0);
1624 got
->add_constant(0);
1625 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
1627 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
1628 // We store the arguments we need in a vector, and
1629 // use the index into the vector as the parameter
1630 // to pass to the target specific routines.
1631 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
1632 void* arg
= reinterpret_cast<void*>(intarg
);
1633 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
1634 got
, got_offset
, 0);
1637 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1638 unsupported_reloc_local(object
, r_type
);
1641 case elfcpp::R_X86_64_TLSDESC_CALL
:
1644 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1645 if (optimized_type
== tls::TLSOPT_NONE
)
1647 // Create a GOT entry for the module index.
1648 target
->got_mod_index_entry(symtab
, layout
, object
);
1650 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1651 unsupported_reloc_local(object
, r_type
);
1654 case elfcpp::R_X86_64_DTPOFF32
:
1655 case elfcpp::R_X86_64_DTPOFF64
:
1658 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1659 layout
->set_has_static_tls();
1660 if (optimized_type
== tls::TLSOPT_NONE
)
1662 // Create a GOT entry for the tp-relative offset.
1663 Output_data_got
<64, false>* got
1664 = target
->got_section(symtab
, layout
);
1665 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1666 got
->add_local_with_rela(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1667 target
->rela_dyn_section(layout
),
1668 elfcpp::R_X86_64_TPOFF64
);
1670 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1671 unsupported_reloc_local(object
, r_type
);
1674 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1675 layout
->set_has_static_tls();
1676 if (output_is_shared
)
1677 unsupported_reloc_local(object
, r_type
);
1686 case elfcpp::R_X86_64_SIZE32
:
1687 case elfcpp::R_X86_64_SIZE64
:
1689 gold_error(_("%s: unsupported reloc %u against local symbol"),
1690 object
->name().c_str(), r_type
);
1696 // Report an unsupported relocation against a global symbol.
1699 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
1700 unsigned int r_type
,
1703 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1704 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1707 // Returns true if this relocation type could be that of a function pointer.
1709 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type
)
1713 case elfcpp::R_X86_64_64
:
1714 case elfcpp::R_X86_64_32
:
1715 case elfcpp::R_X86_64_32S
:
1716 case elfcpp::R_X86_64_16
:
1717 case elfcpp::R_X86_64_8
:
1718 case elfcpp::R_X86_64_GOT64
:
1719 case elfcpp::R_X86_64_GOT32
:
1720 case elfcpp::R_X86_64_GOTPCREL64
:
1721 case elfcpp::R_X86_64_GOTPCREL
:
1722 case elfcpp::R_X86_64_GOTPLT64
:
1730 // For safe ICF, scan a relocation for a local symbol to check if it
1731 // corresponds to a function pointer being taken. In that case mark
1732 // the function whose pointer was taken as not foldable.
1735 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
1739 Sized_relobj
<64, false>* ,
1742 const elfcpp::Rela
<64, false>& ,
1743 unsigned int r_type
,
1744 const elfcpp::Sym
<64, false>&)
1746 // When building a shared library, do not fold any local symbols as it is
1747 // not possible to distinguish pointer taken versus a call by looking at
1748 // the relocation types.
1749 return (parameters
->options().shared()
1750 || possible_function_pointer_reloc(r_type
));
1753 // For safe ICF, scan a relocation for a global symbol to check if it
1754 // corresponds to a function pointer being taken. In that case mark
1755 // the function whose pointer was taken as not foldable.
1758 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
1762 Sized_relobj
<64, false>* ,
1765 const elfcpp::Rela
<64, false>& ,
1766 unsigned int r_type
,
1769 // When building a shared library, do not fold symbols whose visibility
1770 // is hidden, internal or protected.
1771 return ((parameters
->options().shared()
1772 && (gsym
->visibility() == elfcpp::STV_INTERNAL
1773 || gsym
->visibility() == elfcpp::STV_PROTECTED
1774 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
1775 || possible_function_pointer_reloc(r_type
));
1778 // Scan a relocation for a global symbol.
1781 Target_x86_64::Scan::global(Symbol_table
* symtab
,
1783 Target_x86_64
* target
,
1784 Sized_relobj
<64, false>* object
,
1785 unsigned int data_shndx
,
1786 Output_section
* output_section
,
1787 const elfcpp::Rela
<64, false>& reloc
,
1788 unsigned int r_type
,
1791 // A STT_GNU_IFUNC symbol may require a PLT entry.
1792 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1793 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1794 target
->make_plt_entry(symtab
, layout
, gsym
);
1798 case elfcpp::R_X86_64_NONE
:
1799 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1800 case elfcpp::R_X86_64_GNU_VTENTRY
:
1803 case elfcpp::R_X86_64_64
:
1804 case elfcpp::R_X86_64_32
:
1805 case elfcpp::R_X86_64_32S
:
1806 case elfcpp::R_X86_64_16
:
1807 case elfcpp::R_X86_64_8
:
1809 // Make a PLT entry if necessary.
1810 if (gsym
->needs_plt_entry())
1812 target
->make_plt_entry(symtab
, layout
, gsym
);
1813 // Since this is not a PC-relative relocation, we may be
1814 // taking the address of a function. In that case we need to
1815 // set the entry in the dynamic symbol table to the address of
1817 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1818 gsym
->set_needs_dynsym_value();
1820 // Make a dynamic relocation if necessary.
1821 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
1823 if (gsym
->may_need_copy_reloc())
1825 target
->copy_reloc(symtab
, layout
, object
,
1826 data_shndx
, output_section
, gsym
, reloc
);
1828 else if (r_type
== elfcpp::R_X86_64_64
1829 && gsym
->type() == elfcpp::STT_GNU_IFUNC
1830 && gsym
->can_use_relative_reloc(false)
1831 && !gsym
->is_from_dynobj()
1832 && !gsym
->is_undefined()
1833 && !gsym
->is_preemptible())
1835 // Use an IRELATIVE reloc for a locally defined
1836 // STT_GNU_IFUNC symbol. This makes a function
1837 // address in a PIE executable match the address in a
1838 // shared library that it links against.
1839 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1840 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
1841 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
1842 output_section
, object
,
1844 reloc
.get_r_offset(),
1845 reloc
.get_r_addend());
1847 else if (r_type
== elfcpp::R_X86_64_64
1848 && gsym
->can_use_relative_reloc(false))
1850 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1851 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1852 output_section
, object
,
1854 reloc
.get_r_offset(),
1855 reloc
.get_r_addend());
1859 this->check_non_pic(object
, r_type
);
1860 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1861 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1862 data_shndx
, reloc
.get_r_offset(),
1863 reloc
.get_r_addend());
1869 case elfcpp::R_X86_64_PC64
:
1870 case elfcpp::R_X86_64_PC32
:
1871 case elfcpp::R_X86_64_PC16
:
1872 case elfcpp::R_X86_64_PC8
:
1874 // Make a PLT entry if necessary.
1875 if (gsym
->needs_plt_entry())
1876 target
->make_plt_entry(symtab
, layout
, gsym
);
1877 // Make a dynamic relocation if necessary.
1878 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
1880 if (gsym
->may_need_copy_reloc())
1882 target
->copy_reloc(symtab
, layout
, object
,
1883 data_shndx
, output_section
, gsym
, reloc
);
1887 this->check_non_pic(object
, r_type
);
1888 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1889 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1890 data_shndx
, reloc
.get_r_offset(),
1891 reloc
.get_r_addend());
1897 case elfcpp::R_X86_64_GOT64
:
1898 case elfcpp::R_X86_64_GOT32
:
1899 case elfcpp::R_X86_64_GOTPCREL64
:
1900 case elfcpp::R_X86_64_GOTPCREL
:
1901 case elfcpp::R_X86_64_GOTPLT64
:
1903 // The symbol requires a GOT entry.
1904 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1905 if (gsym
->final_value_is_known())
1907 // For a STT_GNU_IFUNC symbol we want the PLT address.
1908 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1909 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
1911 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1915 // If this symbol is not fully resolved, we need to add a
1916 // dynamic relocation for it.
1917 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1918 if (gsym
->is_from_dynobj()
1919 || gsym
->is_undefined()
1920 || gsym
->is_preemptible()
1921 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
1922 && parameters
->options().output_is_position_independent()))
1923 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1924 elfcpp::R_X86_64_GLOB_DAT
);
1927 // For a STT_GNU_IFUNC symbol we want to write the PLT
1928 // offset into the GOT, so that function pointer
1929 // comparisons work correctly.
1931 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
1932 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1935 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
1936 // Tell the dynamic linker to use the PLT address
1937 // when resolving relocations.
1938 if (gsym
->is_from_dynobj()
1939 && !parameters
->options().shared())
1940 gsym
->set_needs_dynsym_value();
1944 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1945 rela_dyn
->add_global_relative(gsym
,
1946 elfcpp::R_X86_64_RELATIVE
,
1951 // For GOTPLT64, we also need a PLT entry (but only if the
1952 // symbol is not fully resolved).
1953 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1954 && !gsym
->final_value_is_known())
1955 target
->make_plt_entry(symtab
, layout
, gsym
);
1959 case elfcpp::R_X86_64_PLT32
:
1960 // If the symbol is fully resolved, this is just a PC32 reloc.
1961 // Otherwise we need a PLT entry.
1962 if (gsym
->final_value_is_known())
1964 // If building a shared library, we can also skip the PLT entry
1965 // if the symbol is defined in the output file and is protected
1967 if (gsym
->is_defined()
1968 && !gsym
->is_from_dynobj()
1969 && !gsym
->is_preemptible())
1971 target
->make_plt_entry(symtab
, layout
, gsym
);
1974 case elfcpp::R_X86_64_GOTPC32
:
1975 case elfcpp::R_X86_64_GOTOFF64
:
1976 case elfcpp::R_X86_64_GOTPC64
:
1977 case elfcpp::R_X86_64_PLTOFF64
:
1978 // We need a GOT section.
1979 target
->got_section(symtab
, layout
);
1980 // For PLTOFF64, we also need a PLT entry (but only if the
1981 // symbol is not fully resolved).
1982 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1983 && !gsym
->final_value_is_known())
1984 target
->make_plt_entry(symtab
, layout
, gsym
);
1987 case elfcpp::R_X86_64_COPY
:
1988 case elfcpp::R_X86_64_GLOB_DAT
:
1989 case elfcpp::R_X86_64_JUMP_SLOT
:
1990 case elfcpp::R_X86_64_RELATIVE
:
1991 case elfcpp::R_X86_64_IRELATIVE
:
1992 // These are outstanding tls relocs, which are unexpected when linking
1993 case elfcpp::R_X86_64_TPOFF64
:
1994 case elfcpp::R_X86_64_DTPMOD64
:
1995 case elfcpp::R_X86_64_TLSDESC
:
1996 gold_error(_("%s: unexpected reloc %u in object file"),
1997 object
->name().c_str(), r_type
);
2000 // These are initial tls relocs, which are expected for global()
2001 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2002 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2003 case elfcpp::R_X86_64_TLSDESC_CALL
:
2004 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2005 case elfcpp::R_X86_64_DTPOFF32
:
2006 case elfcpp::R_X86_64_DTPOFF64
:
2007 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2008 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2010 const bool is_final
= gsym
->final_value_is_known();
2011 const tls::Tls_optimization optimized_type
2012 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
2015 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2016 if (optimized_type
== tls::TLSOPT_NONE
)
2018 // Create a pair of GOT entries for the module index and
2019 // dtv-relative offset.
2020 Output_data_got
<64, false>* got
2021 = target
->got_section(symtab
, layout
);
2022 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_PAIR
,
2023 target
->rela_dyn_section(layout
),
2024 elfcpp::R_X86_64_DTPMOD64
,
2025 elfcpp::R_X86_64_DTPOFF64
);
2027 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2029 // Create a GOT entry for the tp-relative offset.
2030 Output_data_got
<64, false>* got
2031 = target
->got_section(symtab
, layout
);
2032 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
2033 target
->rela_dyn_section(layout
),
2034 elfcpp::R_X86_64_TPOFF64
);
2036 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2037 unsupported_reloc_global(object
, r_type
, gsym
);
2040 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2041 target
->define_tls_base_symbol(symtab
, layout
);
2042 if (optimized_type
== tls::TLSOPT_NONE
)
2044 // Create reserved PLT and GOT entries for the resolver.
2045 target
->reserve_tlsdesc_entries(symtab
, layout
);
2047 // Create a double GOT entry with an R_X86_64_TLSDESC
2048 // reloc. The R_X86_64_TLSDESC reloc is resolved
2049 // lazily, so the GOT entry needs to be in an area in
2050 // .got.plt, not .got. Call got_section to make sure
2051 // the section has been created.
2052 target
->got_section(symtab
, layout
);
2053 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2054 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2055 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_DESC
, rt
,
2056 elfcpp::R_X86_64_TLSDESC
, 0);
2058 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2060 // Create a GOT entry for the tp-relative offset.
2061 Output_data_got
<64, false>* got
2062 = target
->got_section(symtab
, layout
);
2063 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
2064 target
->rela_dyn_section(layout
),
2065 elfcpp::R_X86_64_TPOFF64
);
2067 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2068 unsupported_reloc_global(object
, r_type
, gsym
);
2071 case elfcpp::R_X86_64_TLSDESC_CALL
:
2074 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2075 if (optimized_type
== tls::TLSOPT_NONE
)
2077 // Create a GOT entry for the module index.
2078 target
->got_mod_index_entry(symtab
, layout
, object
);
2080 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2081 unsupported_reloc_global(object
, r_type
, gsym
);
2084 case elfcpp::R_X86_64_DTPOFF32
:
2085 case elfcpp::R_X86_64_DTPOFF64
:
2088 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2089 layout
->set_has_static_tls();
2090 if (optimized_type
== tls::TLSOPT_NONE
)
2092 // Create a GOT entry for the tp-relative offset.
2093 Output_data_got
<64, false>* got
2094 = target
->got_section(symtab
, layout
);
2095 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
2096 target
->rela_dyn_section(layout
),
2097 elfcpp::R_X86_64_TPOFF64
);
2099 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2100 unsupported_reloc_global(object
, r_type
, gsym
);
2103 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2104 layout
->set_has_static_tls();
2105 if (parameters
->options().shared())
2106 unsupported_reloc_local(object
, r_type
);
2115 case elfcpp::R_X86_64_SIZE32
:
2116 case elfcpp::R_X86_64_SIZE64
:
2118 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2119 object
->name().c_str(), r_type
,
2120 gsym
->demangled_name().c_str());
2126 Target_x86_64::gc_process_relocs(Symbol_table
* symtab
,
2128 Sized_relobj
<64, false>* object
,
2129 unsigned int data_shndx
,
2130 unsigned int sh_type
,
2131 const unsigned char* prelocs
,
2133 Output_section
* output_section
,
2134 bool needs_special_offset_handling
,
2135 size_t local_symbol_count
,
2136 const unsigned char* plocal_symbols
)
2139 if (sh_type
== elfcpp::SHT_REL
)
2144 gold::gc_process_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2145 Target_x86_64::Scan
,
2146 Target_x86_64::Relocatable_size_for_reloc
>(
2155 needs_special_offset_handling
,
2160 // Scan relocations for a section.
2163 Target_x86_64::scan_relocs(Symbol_table
* symtab
,
2165 Sized_relobj
<64, false>* object
,
2166 unsigned int data_shndx
,
2167 unsigned int sh_type
,
2168 const unsigned char* prelocs
,
2170 Output_section
* output_section
,
2171 bool needs_special_offset_handling
,
2172 size_t local_symbol_count
,
2173 const unsigned char* plocal_symbols
)
2175 if (sh_type
== elfcpp::SHT_REL
)
2177 gold_error(_("%s: unsupported REL reloc section"),
2178 object
->name().c_str());
2182 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2183 Target_x86_64::Scan
>(
2192 needs_special_offset_handling
,
2197 // Finalize the sections.
2200 Target_x86_64::do_finalize_sections(
2202 const Input_objects
*,
2203 Symbol_table
* symtab
)
2205 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2207 : this->plt_
->rela_plt());
2208 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
2209 this->rela_dyn_
, true, false);
2211 // Fill in some more dynamic tags.
2212 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
2215 if (this->plt_
!= NULL
2216 && this->plt_
->output_section() != NULL
2217 && this->plt_
->has_tlsdesc_entry())
2219 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
2220 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
2221 this->got_
->finalize_data_size();
2222 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
2223 this->plt_
, plt_offset
);
2224 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
2225 this->got_
, got_offset
);
2229 // Emit any relocs we saved in an attempt to avoid generating COPY
2231 if (this->copy_relocs_
.any_saved_relocs())
2232 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
2234 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2235 // the .got.plt section.
2236 Symbol
* sym
= this->global_offset_table_
;
2239 uint64_t data_size
= this->got_plt_
->current_data_size();
2240 symtab
->get_sized_symbol
<64>(sym
)->set_symsize(data_size
);
2244 // Perform a relocation.
2247 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
2248 Target_x86_64
* target
,
2251 const elfcpp::Rela
<64, false>& rela
,
2252 unsigned int r_type
,
2253 const Sized_symbol
<64>* gsym
,
2254 const Symbol_value
<64>* psymval
,
2255 unsigned char* view
,
2256 elfcpp::Elf_types
<64>::Elf_Addr address
,
2257 section_size_type view_size
)
2259 if (this->skip_call_tls_get_addr_
)
2261 if ((r_type
!= elfcpp::R_X86_64_PLT32
2262 && r_type
!= elfcpp::R_X86_64_PC32
)
2264 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
2266 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2267 _("missing expected TLS relocation"));
2271 this->skip_call_tls_get_addr_
= false;
2276 const Sized_relobj
<64, false>* object
= relinfo
->object
;
2278 // Pick the value to use for symbols defined in the PLT.
2279 Symbol_value
<64> symval
;
2281 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
2283 symval
.set_output_value(target
->plt_section()->address()
2284 + gsym
->plt_offset());
2287 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
2289 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2290 if (object
->local_has_plt_offset(r_sym
))
2292 symval
.set_output_value(target
->plt_section()->address()
2293 + object
->local_plt_offset(r_sym
));
2298 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2300 // Get the GOT offset if needed.
2301 // The GOT pointer points to the end of the GOT section.
2302 // We need to subtract the size of the GOT section to get
2303 // the actual offset to use in the relocation.
2304 bool have_got_offset
= false;
2305 unsigned int got_offset
= 0;
2308 case elfcpp::R_X86_64_GOT32
:
2309 case elfcpp::R_X86_64_GOT64
:
2310 case elfcpp::R_X86_64_GOTPLT64
:
2311 case elfcpp::R_X86_64_GOTPCREL
:
2312 case elfcpp::R_X86_64_GOTPCREL64
:
2315 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2316 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
2320 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2321 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2322 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
2323 - target
->got_size());
2325 have_got_offset
= true;
2334 case elfcpp::R_X86_64_NONE
:
2335 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2336 case elfcpp::R_X86_64_GNU_VTENTRY
:
2339 case elfcpp::R_X86_64_64
:
2340 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
2343 case elfcpp::R_X86_64_PC64
:
2344 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
2348 case elfcpp::R_X86_64_32
:
2349 // FIXME: we need to verify that value + addend fits into 32 bits:
2350 // uint64_t x = value + addend;
2351 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2352 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2353 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
2356 case elfcpp::R_X86_64_32S
:
2357 // FIXME: we need to verify that value + addend fits into 32 bits:
2358 // int64_t x = value + addend; // note this quantity is signed!
2359 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2360 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
2363 case elfcpp::R_X86_64_PC32
:
2364 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
2368 case elfcpp::R_X86_64_16
:
2369 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
2372 case elfcpp::R_X86_64_PC16
:
2373 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
2377 case elfcpp::R_X86_64_8
:
2378 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
2381 case elfcpp::R_X86_64_PC8
:
2382 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
2386 case elfcpp::R_X86_64_PLT32
:
2387 gold_assert(gsym
== NULL
2388 || gsym
->has_plt_offset()
2389 || gsym
->final_value_is_known()
2390 || (gsym
->is_defined()
2391 && !gsym
->is_from_dynobj()
2392 && !gsym
->is_preemptible()));
2393 // Note: while this code looks the same as for R_X86_64_PC32, it
2394 // behaves differently because psymval was set to point to
2395 // the PLT entry, rather than the symbol, in Scan::global().
2396 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
2400 case elfcpp::R_X86_64_PLTOFF64
:
2403 gold_assert(gsym
->has_plt_offset()
2404 || gsym
->final_value_is_known());
2405 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
2406 got_address
= target
->got_section(NULL
, NULL
)->address();
2407 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
2408 addend
- got_address
);
2411 case elfcpp::R_X86_64_GOT32
:
2412 gold_assert(have_got_offset
);
2413 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
2416 case elfcpp::R_X86_64_GOTPC32
:
2419 elfcpp::Elf_types
<64>::Elf_Addr value
;
2420 value
= target
->got_plt_section()->address();
2421 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2425 case elfcpp::R_X86_64_GOT64
:
2426 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2427 // Since we always add a PLT entry, this is equivalent.
2428 case elfcpp::R_X86_64_GOTPLT64
:
2429 gold_assert(have_got_offset
);
2430 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
2433 case elfcpp::R_X86_64_GOTPC64
:
2436 elfcpp::Elf_types
<64>::Elf_Addr value
;
2437 value
= target
->got_plt_section()->address();
2438 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
2442 case elfcpp::R_X86_64_GOTOFF64
:
2444 elfcpp::Elf_types
<64>::Elf_Addr value
;
2445 value
= (psymval
->value(object
, 0)
2446 - target
->got_plt_section()->address());
2447 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
2451 case elfcpp::R_X86_64_GOTPCREL
:
2453 gold_assert(have_got_offset
);
2454 elfcpp::Elf_types
<64>::Elf_Addr value
;
2455 value
= target
->got_plt_section()->address() + got_offset
;
2456 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2460 case elfcpp::R_X86_64_GOTPCREL64
:
2462 gold_assert(have_got_offset
);
2463 elfcpp::Elf_types
<64>::Elf_Addr value
;
2464 value
= target
->got_plt_section()->address() + got_offset
;
2465 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
2469 case elfcpp::R_X86_64_COPY
:
2470 case elfcpp::R_X86_64_GLOB_DAT
:
2471 case elfcpp::R_X86_64_JUMP_SLOT
:
2472 case elfcpp::R_X86_64_RELATIVE
:
2473 case elfcpp::R_X86_64_IRELATIVE
:
2474 // These are outstanding tls relocs, which are unexpected when linking
2475 case elfcpp::R_X86_64_TPOFF64
:
2476 case elfcpp::R_X86_64_DTPMOD64
:
2477 case elfcpp::R_X86_64_TLSDESC
:
2478 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2479 _("unexpected reloc %u in object file"),
2483 // These are initial tls relocs, which are expected when linking
2484 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2485 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2486 case elfcpp::R_X86_64_TLSDESC_CALL
:
2487 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2488 case elfcpp::R_X86_64_DTPOFF32
:
2489 case elfcpp::R_X86_64_DTPOFF64
:
2490 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2491 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2492 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
2493 view
, address
, view_size
);
2496 case elfcpp::R_X86_64_SIZE32
:
2497 case elfcpp::R_X86_64_SIZE64
:
2499 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2500 _("unsupported reloc %u"),
2508 // Perform a TLS relocation.
2511 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
2512 Target_x86_64
* target
,
2514 const elfcpp::Rela
<64, false>& rela
,
2515 unsigned int r_type
,
2516 const Sized_symbol
<64>* gsym
,
2517 const Symbol_value
<64>* psymval
,
2518 unsigned char* view
,
2519 elfcpp::Elf_types
<64>::Elf_Addr address
,
2520 section_size_type view_size
)
2522 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2524 const Sized_relobj
<64, false>* object
= relinfo
->object
;
2525 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2526 elfcpp::Shdr
<64, false> data_shdr(relinfo
->data_shdr
);
2527 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
2529 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
2531 const bool is_final
= (gsym
== NULL
2532 ? !parameters
->options().shared()
2533 : gsym
->final_value_is_known());
2534 tls::Tls_optimization optimized_type
2535 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
2538 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2539 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2541 // If this code sequence is used in a non-executable section,
2542 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2543 // on the assumption that it's being used by itself in a debug
2544 // section. Therefore, in the unlikely event that the code
2545 // sequence appears in a non-executable section, we simply
2546 // leave it unoptimized.
2547 optimized_type
= tls::TLSOPT_NONE
;
2549 if (optimized_type
== tls::TLSOPT_TO_LE
)
2551 gold_assert(tls_segment
!= NULL
);
2552 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
2553 rela
, r_type
, value
, view
,
2559 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2560 ? GOT_TYPE_TLS_OFFSET
2561 : GOT_TYPE_TLS_PAIR
);
2562 unsigned int got_offset
;
2565 gold_assert(gsym
->has_got_offset(got_type
));
2566 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2570 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2571 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2572 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2573 - target
->got_size());
2575 if (optimized_type
== tls::TLSOPT_TO_IE
)
2577 gold_assert(tls_segment
!= NULL
);
2578 value
= target
->got_plt_section()->address() + got_offset
;
2579 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2580 value
, view
, address
, view_size
);
2583 else if (optimized_type
== tls::TLSOPT_NONE
)
2585 // Relocate the field with the offset of the pair of GOT
2587 value
= target
->got_plt_section()->address() + got_offset
;
2588 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2593 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2594 _("unsupported reloc %u"), r_type
);
2597 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2598 case elfcpp::R_X86_64_TLSDESC_CALL
:
2599 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2601 // See above comment for R_X86_64_TLSGD.
2602 optimized_type
= tls::TLSOPT_NONE
;
2604 if (optimized_type
== tls::TLSOPT_TO_LE
)
2606 gold_assert(tls_segment
!= NULL
);
2607 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2608 rela
, r_type
, value
, view
,
2614 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2615 ? GOT_TYPE_TLS_OFFSET
2616 : GOT_TYPE_TLS_DESC
);
2617 unsigned int got_offset
= 0;
2618 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
2619 && optimized_type
== tls::TLSOPT_NONE
)
2621 // We created GOT entries in the .got.tlsdesc portion of
2622 // the .got.plt section, but the offset stored in the
2623 // symbol is the offset within .got.tlsdesc.
2624 got_offset
= (target
->got_size()
2625 + target
->got_plt_section()->data_size());
2629 gold_assert(gsym
->has_got_offset(got_type
));
2630 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
2634 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2635 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2636 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
2637 - target
->got_size());
2639 if (optimized_type
== tls::TLSOPT_TO_IE
)
2641 gold_assert(tls_segment
!= NULL
);
2642 value
= target
->got_plt_section()->address() + got_offset
;
2643 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
2644 rela
, r_type
, value
, view
, address
,
2648 else if (optimized_type
== tls::TLSOPT_NONE
)
2650 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2652 // Relocate the field with the offset of the pair of GOT
2654 value
= target
->got_plt_section()->address() + got_offset
;
2655 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2661 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2662 _("unsupported reloc %u"), r_type
);
2665 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2666 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
2668 // See above comment for R_X86_64_TLSGD.
2669 optimized_type
= tls::TLSOPT_NONE
;
2671 if (optimized_type
== tls::TLSOPT_TO_LE
)
2673 gold_assert(tls_segment
!= NULL
);
2674 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2675 value
, view
, view_size
);
2678 else if (optimized_type
== tls::TLSOPT_NONE
)
2680 // Relocate the field with the offset of the GOT entry for
2681 // the module index.
2682 unsigned int got_offset
;
2683 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2684 - target
->got_size());
2685 value
= target
->got_plt_section()->address() + got_offset
;
2686 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2690 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2691 _("unsupported reloc %u"), r_type
);
2694 case elfcpp::R_X86_64_DTPOFF32
:
2695 // This relocation type is used in debugging information.
2696 // In that case we need to not optimize the value. If the
2697 // section is not executable, then we assume we should not
2698 // optimize this reloc. See comments above for R_X86_64_TLSGD,
2699 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
2701 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
2703 gold_assert(tls_segment
!= NULL
);
2704 value
-= tls_segment
->memsz();
2706 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2709 case elfcpp::R_X86_64_DTPOFF64
:
2710 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
2711 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
2713 gold_assert(tls_segment
!= NULL
);
2714 value
-= tls_segment
->memsz();
2716 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
2719 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2720 if (optimized_type
== tls::TLSOPT_TO_LE
)
2722 gold_assert(tls_segment
!= NULL
);
2723 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2724 rela
, r_type
, value
, view
,
2728 else if (optimized_type
== tls::TLSOPT_NONE
)
2730 // Relocate the field with the offset of the GOT entry for
2731 // the tp-relative offset of the symbol.
2732 unsigned int got_offset
;
2735 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
2736 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
2737 - target
->got_size());
2741 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2742 gold_assert(object
->local_has_got_offset(r_sym
,
2743 GOT_TYPE_TLS_OFFSET
));
2744 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
2745 - target
->got_size());
2747 value
= target
->got_plt_section()->address() + got_offset
;
2748 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2751 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2752 _("unsupported reloc type %u"),
2756 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2757 value
-= tls_segment
->memsz();
2758 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2763 // Do a relocation in which we convert a TLS General-Dynamic to an
2767 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
2770 const elfcpp::Rela
<64, false>& rela
,
2772 elfcpp::Elf_types
<64>::Elf_Addr value
,
2773 unsigned char* view
,
2774 elfcpp::Elf_types
<64>::Elf_Addr address
,
2775 section_size_type view_size
)
2777 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2778 // .word 0x6666; rex64; call __tls_get_addr
2779 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2781 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2782 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2784 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2785 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2786 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2787 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2789 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2791 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2792 Relocate_functions
<64, false>::pcrela32(view
+ 8, value
, addend
- 8, address
);
2794 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2796 this->skip_call_tls_get_addr_
= true;
2799 // Do a relocation in which we convert a TLS General-Dynamic to a
2803 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
2805 Output_segment
* tls_segment
,
2806 const elfcpp::Rela
<64, false>& rela
,
2808 elfcpp::Elf_types
<64>::Elf_Addr value
,
2809 unsigned char* view
,
2810 section_size_type view_size
)
2812 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2813 // .word 0x6666; rex64; call __tls_get_addr
2814 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2816 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2817 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2819 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2820 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2821 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2822 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2824 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2826 value
-= tls_segment
->memsz();
2827 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
2829 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2831 this->skip_call_tls_get_addr_
= true;
2834 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2837 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2838 const Relocate_info
<64, false>* relinfo
,
2841 const elfcpp::Rela
<64, false>& rela
,
2842 unsigned int r_type
,
2843 elfcpp::Elf_types
<64>::Elf_Addr value
,
2844 unsigned char* view
,
2845 elfcpp::Elf_types
<64>::Elf_Addr address
,
2846 section_size_type view_size
)
2848 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2850 // leaq foo@tlsdesc(%rip), %rax
2851 // ==> movq foo@gottpoff(%rip), %rax
2852 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2853 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2854 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2855 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2857 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2858 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2862 // call *foo@tlscall(%rax)
2864 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2865 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2866 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2867 view
[0] == 0xff && view
[1] == 0x10);
2873 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2876 Target_x86_64::Relocate::tls_desc_gd_to_le(
2877 const Relocate_info
<64, false>* relinfo
,
2879 Output_segment
* tls_segment
,
2880 const elfcpp::Rela
<64, false>& rela
,
2881 unsigned int r_type
,
2882 elfcpp::Elf_types
<64>::Elf_Addr value
,
2883 unsigned char* view
,
2884 section_size_type view_size
)
2886 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2888 // leaq foo@tlsdesc(%rip), %rax
2889 // ==> movq foo@tpoff, %rax
2890 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2891 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2892 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2893 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2896 value
-= tls_segment
->memsz();
2897 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2901 // call *foo@tlscall(%rax)
2903 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2904 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2905 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2906 view
[0] == 0xff && view
[1] == 0x10);
2913 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
2916 const elfcpp::Rela
<64, false>& rela
,
2918 elfcpp::Elf_types
<64>::Elf_Addr
,
2919 unsigned char* view
,
2920 section_size_type view_size
)
2922 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2923 // ... leq foo@dtpoff(%rax),%reg
2924 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2926 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2927 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
2929 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2930 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
2932 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
2934 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2936 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2938 this->skip_call_tls_get_addr_
= true;
2941 // Do a relocation in which we convert a TLS Initial-Exec to a
2945 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
2947 Output_segment
* tls_segment
,
2948 const elfcpp::Rela
<64, false>& rela
,
2950 elfcpp::Elf_types
<64>::Elf_Addr value
,
2951 unsigned char* view
,
2952 section_size_type view_size
)
2954 // We need to examine the opcodes to figure out which instruction we
2957 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2958 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2960 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2961 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2963 unsigned char op1
= view
[-3];
2964 unsigned char op2
= view
[-2];
2965 unsigned char op3
= view
[-1];
2966 unsigned char reg
= op3
>> 3;
2974 view
[-1] = 0xc0 | reg
;
2978 // Special handling for %rsp.
2982 view
[-1] = 0xc0 | reg
;
2990 view
[-1] = 0x80 | reg
| (reg
<< 3);
2993 value
-= tls_segment
->memsz();
2994 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2997 // Relocate section data.
3000 Target_x86_64::relocate_section(
3001 const Relocate_info
<64, false>* relinfo
,
3002 unsigned int sh_type
,
3003 const unsigned char* prelocs
,
3005 Output_section
* output_section
,
3006 bool needs_special_offset_handling
,
3007 unsigned char* view
,
3008 elfcpp::Elf_types
<64>::Elf_Addr address
,
3009 section_size_type view_size
,
3010 const Reloc_symbol_changes
* reloc_symbol_changes
)
3012 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3014 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
3015 Target_x86_64::Relocate
>(
3021 needs_special_offset_handling
,
3025 reloc_symbol_changes
);
3028 // Apply an incremental relocation. Incremental relocations always refer
3029 // to global symbols.
3032 Target_x86_64::apply_relocation(
3033 const Relocate_info
<64, false>* relinfo
,
3034 elfcpp::Elf_types
<64>::Elf_Addr r_offset
,
3035 unsigned int r_type
,
3036 elfcpp::Elf_types
<64>::Elf_Swxword r_addend
,
3038 unsigned char* view
,
3039 elfcpp::Elf_types
<64>::Elf_Addr address
,
3040 section_size_type view_size
)
3042 gold::apply_relocation
<64, false, Target_x86_64
, Target_x86_64::Relocate
>(
3054 // Return the size of a relocation while scanning during a relocatable
3058 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3059 unsigned int r_type
,
3064 case elfcpp::R_X86_64_NONE
:
3065 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3066 case elfcpp::R_X86_64_GNU_VTENTRY
:
3067 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3068 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3069 case elfcpp::R_X86_64_TLSDESC_CALL
:
3070 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3071 case elfcpp::R_X86_64_DTPOFF32
:
3072 case elfcpp::R_X86_64_DTPOFF64
:
3073 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3074 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3077 case elfcpp::R_X86_64_64
:
3078 case elfcpp::R_X86_64_PC64
:
3079 case elfcpp::R_X86_64_GOTOFF64
:
3080 case elfcpp::R_X86_64_GOTPC64
:
3081 case elfcpp::R_X86_64_PLTOFF64
:
3082 case elfcpp::R_X86_64_GOT64
:
3083 case elfcpp::R_X86_64_GOTPCREL64
:
3084 case elfcpp::R_X86_64_GOTPCREL
:
3085 case elfcpp::R_X86_64_GOTPLT64
:
3088 case elfcpp::R_X86_64_32
:
3089 case elfcpp::R_X86_64_32S
:
3090 case elfcpp::R_X86_64_PC32
:
3091 case elfcpp::R_X86_64_PLT32
:
3092 case elfcpp::R_X86_64_GOTPC32
:
3093 case elfcpp::R_X86_64_GOT32
:
3096 case elfcpp::R_X86_64_16
:
3097 case elfcpp::R_X86_64_PC16
:
3100 case elfcpp::R_X86_64_8
:
3101 case elfcpp::R_X86_64_PC8
:
3104 case elfcpp::R_X86_64_COPY
:
3105 case elfcpp::R_X86_64_GLOB_DAT
:
3106 case elfcpp::R_X86_64_JUMP_SLOT
:
3107 case elfcpp::R_X86_64_RELATIVE
:
3108 case elfcpp::R_X86_64_IRELATIVE
:
3109 // These are outstanding tls relocs, which are unexpected when linking
3110 case elfcpp::R_X86_64_TPOFF64
:
3111 case elfcpp::R_X86_64_DTPMOD64
:
3112 case elfcpp::R_X86_64_TLSDESC
:
3113 object
->error(_("unexpected reloc %u in object file"), r_type
);
3116 case elfcpp::R_X86_64_SIZE32
:
3117 case elfcpp::R_X86_64_SIZE64
:
3119 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
3124 // Scan the relocs during a relocatable link.
3127 Target_x86_64::scan_relocatable_relocs(Symbol_table
* symtab
,
3129 Sized_relobj
<64, false>* object
,
3130 unsigned int data_shndx
,
3131 unsigned int sh_type
,
3132 const unsigned char* prelocs
,
3134 Output_section
* output_section
,
3135 bool needs_special_offset_handling
,
3136 size_t local_symbol_count
,
3137 const unsigned char* plocal_symbols
,
3138 Relocatable_relocs
* rr
)
3140 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3142 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
3143 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
3145 gold::scan_relocatable_relocs
<64, false, elfcpp::SHT_RELA
,
3146 Scan_relocatable_relocs
>(
3154 needs_special_offset_handling
,
3160 // Relocate a section during a relocatable link.
3163 Target_x86_64::relocate_for_relocatable(
3164 const Relocate_info
<64, false>* relinfo
,
3165 unsigned int sh_type
,
3166 const unsigned char* prelocs
,
3168 Output_section
* output_section
,
3169 off_t offset_in_output_section
,
3170 const Relocatable_relocs
* rr
,
3171 unsigned char* view
,
3172 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
3173 section_size_type view_size
,
3174 unsigned char* reloc_view
,
3175 section_size_type reloc_view_size
)
3177 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3179 gold::relocate_for_relocatable
<64, false, elfcpp::SHT_RELA
>(
3184 offset_in_output_section
,
3193 // Return the value to use for a dynamic which requires special
3194 // treatment. This is how we support equality comparisons of function
3195 // pointers across shared library boundaries, as described in the
3196 // processor specific ABI supplement.
3199 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
3201 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
3202 return this->plt_section()->address() + gsym
->plt_offset();
3205 // Return a string used to fill a code section with nops to take up
3206 // the specified length.
3209 Target_x86_64::do_code_fill(section_size_type length
) const
3213 // Build a jmpq instruction to skip over the bytes.
3214 unsigned char jmp
[5];
3216 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
3217 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
3218 + std::string(length
- 5, '\0'));
3221 // Nop sequences of various lengths.
3222 const char nop1
[1] = { 0x90 }; // nop
3223 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
3224 const char nop3
[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
3225 const char nop4
[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
3226 const char nop5
[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
3228 const char nop6
[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
3230 const char nop7
[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
3232 const char nop8
[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
3233 0x00, 0x00, 0x00, 0x00 };
3234 const char nop9
[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
3235 0x00, 0x00, 0x00, 0x00,
3237 const char nop10
[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3238 0x84, 0x00, 0x00, 0x00,
3240 const char nop11
[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3241 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3243 const char nop12
[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3244 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3245 0x00, 0x00, 0x00, 0x00 };
3246 const char nop13
[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3247 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3248 0x00, 0x00, 0x00, 0x00,
3250 const char nop14
[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3251 0x66, 0x2e, 0x0f, 0x1f, // data16
3252 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3254 const char nop15
[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3255 0x66, 0x66, 0x2e, 0x0f, // data16; data16
3256 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3259 const char* nops
[16] = {
3261 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
3262 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
3265 return std::string(nops
[length
], length
);
3268 // Return the addend to use for a target specific relocation. The
3269 // only target specific relocation is R_X86_64_TLSDESC for a local
3270 // symbol. We want to set the addend is the offset of the local
3271 // symbol in the TLS segment.
3274 Target_x86_64::do_reloc_addend(void* arg
, unsigned int r_type
,
3277 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
3278 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
3279 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
3280 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
3281 const Symbol_value
<64>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
3282 gold_assert(psymval
->is_tls_symbol());
3283 // The value of a TLS symbol is the offset in the TLS segment.
3284 return psymval
->value(ti
.object
, 0);
3287 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3288 // compiled with -fsplit-stack. The function calls non-split-stack
3289 // code. We have to change the function so that it always ensures
3290 // that it has enough stack space to run some random function.
3293 Target_x86_64::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
3294 section_offset_type fnoffset
,
3295 section_size_type fnsize
,
3296 unsigned char* view
,
3297 section_size_type view_size
,
3299 std::string
* to
) const
3301 // The function starts with a comparison of the stack pointer and a
3302 // field in the TCB. This is followed by a jump.
3305 if (this->match_view(view
, view_size
, fnoffset
, "\x64\x48\x3b\x24\x25", 5)
3308 // We will call __morestack if the carry flag is set after this
3309 // comparison. We turn the comparison into an stc instruction
3311 view
[fnoffset
] = '\xf9';
3312 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 8);
3314 // lea NN(%rsp),%r10
3315 // lea NN(%rsp),%r11
3316 else if ((this->match_view(view
, view_size
, fnoffset
,
3317 "\x4c\x8d\x94\x24", 4)
3318 || this->match_view(view
, view_size
, fnoffset
,
3319 "\x4c\x8d\x9c\x24", 4))
3322 // This is loading an offset from the stack pointer for a
3323 // comparison. The offset is negative, so we decrease the
3324 // offset by the amount of space we need for the stack. This
3325 // means we will avoid calling __morestack if there happens to
3326 // be plenty of space on the stack already.
3327 unsigned char* pval
= view
+ fnoffset
+ 4;
3328 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
3329 val
-= parameters
->options().split_stack_adjust_size();
3330 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
3334 if (!object
->has_no_split_stack())
3335 object
->error(_("failed to match split-stack sequence at "
3336 "section %u offset %0zx"),
3337 shndx
, static_cast<size_t>(fnoffset
));
3341 // We have to change the function so that it calls
3342 // __morestack_non_split instead of __morestack. The former will
3343 // allocate additional stack space.
3344 *from
= "__morestack";
3345 *to
= "__morestack_non_split";
3348 // The selector for x86_64 object files.
3350 class Target_selector_x86_64
: public Target_selector_freebsd
3353 Target_selector_x86_64()
3354 : Target_selector_freebsd(elfcpp::EM_X86_64
, 64, false, "elf64-x86-64",
3355 "elf64-x86-64-freebsd")
3359 do_instantiate_target()
3360 { return new Target_x86_64(); }
3364 Target_selector_x86_64 target_selector_x86_64
;
3366 } // End anonymous namespace.