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 class Output_data_plt_x86_64
;
51 // The x86_64 target class.
53 // http://www.x86-64.org/documentation/abi.pdf
54 // TLS info comes from
55 // http://people.redhat.com/drepper/tls.pdf
56 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
58 class Target_x86_64
: public Target_freebsd
<64, false>
61 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
62 // uses only Elf64_Rela relocation entries with explicit addends."
63 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
66 : Target_freebsd
<64, false>(&x86_64_info
),
67 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_tlsdesc_(NULL
),
68 global_offset_table_(NULL
), rela_dyn_(NULL
),
69 copy_relocs_(elfcpp::R_X86_64_COPY
), dynbss_(NULL
),
70 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
71 tls_base_symbol_defined_(false)
74 // This function should be defined in targets that can use relocation
75 // types to determine (implemented in local_reloc_may_be_function_pointer
76 // and global_reloc_may_be_function_pointer)
77 // if a function's pointer is taken. ICF uses this in safe mode to only
78 // fold those functions whose pointer is defintely not taken. For x86_64
79 // pie binaries, safe ICF cannot be done by looking at relocation types.
81 can_check_for_function_pointers() const
82 { return !parameters
->options().pie(); }
84 // Hook for a new output section.
86 do_new_output_section(Output_section
*) const;
88 // Scan the relocations to look for symbol adjustments.
90 gc_process_relocs(Symbol_table
* symtab
,
92 Sized_relobj
<64, false>* object
,
93 unsigned int data_shndx
,
95 const unsigned char* prelocs
,
97 Output_section
* output_section
,
98 bool needs_special_offset_handling
,
99 size_t local_symbol_count
,
100 const unsigned char* plocal_symbols
);
102 // Scan the relocations to look for symbol adjustments.
104 scan_relocs(Symbol_table
* symtab
,
106 Sized_relobj
<64, false>* object
,
107 unsigned int data_shndx
,
108 unsigned int sh_type
,
109 const unsigned char* prelocs
,
111 Output_section
* output_section
,
112 bool needs_special_offset_handling
,
113 size_t local_symbol_count
,
114 const unsigned char* plocal_symbols
);
116 // Finalize the sections.
118 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
120 // Return the value to use for a dynamic which requires special
123 do_dynsym_value(const Symbol
*) const;
125 // Relocate a section.
127 relocate_section(const Relocate_info
<64, false>*,
128 unsigned int sh_type
,
129 const unsigned char* prelocs
,
131 Output_section
* output_section
,
132 bool needs_special_offset_handling
,
134 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
135 section_size_type view_size
,
136 const Reloc_symbol_changes
*);
138 // Scan the relocs during a relocatable link.
140 scan_relocatable_relocs(Symbol_table
* symtab
,
142 Sized_relobj
<64, false>* object
,
143 unsigned int data_shndx
,
144 unsigned int sh_type
,
145 const unsigned char* prelocs
,
147 Output_section
* output_section
,
148 bool needs_special_offset_handling
,
149 size_t local_symbol_count
,
150 const unsigned char* plocal_symbols
,
151 Relocatable_relocs
*);
153 // Relocate a section during a relocatable link.
155 relocate_for_relocatable(const Relocate_info
<64, false>*,
156 unsigned int sh_type
,
157 const unsigned char* prelocs
,
159 Output_section
* output_section
,
160 off_t offset_in_output_section
,
161 const Relocatable_relocs
*,
163 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
164 section_size_type view_size
,
165 unsigned char* reloc_view
,
166 section_size_type reloc_view_size
);
168 // Return a string used to fill a code section with nops.
170 do_code_fill(section_size_type length
) const;
172 // Return whether SYM is defined by the ABI.
174 do_is_defined_by_abi(const Symbol
* sym
) const
175 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
177 // Return the symbol index to use for a target specific relocation.
178 // The only target specific relocation is R_X86_64_TLSDESC for a
179 // local symbol, which is an absolute reloc.
181 do_reloc_symbol_index(void*, unsigned int r_type
) const
183 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
187 // Return the addend to use for a target specific relocation.
189 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
191 // Adjust -fstack-split code which calls non-stack-split code.
193 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
194 section_offset_type fnoffset
, section_size_type fnsize
,
195 unsigned char* view
, section_size_type view_size
,
196 std::string
* from
, std::string
* to
) const;
198 // Return the size of the GOT section.
202 gold_assert(this->got_
!= NULL
);
203 return this->got_
->data_size();
206 // Return the number of entries in the GOT.
208 got_entry_count() const
210 if (this->got_
== NULL
)
212 return this->got_size() / 8;
215 // Return the number of entries in the PLT.
217 plt_entry_count() const;
219 // Return the offset of the first non-reserved PLT entry.
221 first_plt_entry_offset() const;
223 // Return the size of each PLT entry.
225 plt_entry_size() const;
227 // Add a new reloc argument, returning the index in the vector.
229 add_tlsdesc_info(Sized_relobj
<64, false>* object
, unsigned int r_sym
)
231 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
232 return this->tlsdesc_reloc_info_
.size() - 1;
236 // The class which scans relocations.
241 : issued_non_pic_error_(false)
245 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
246 Sized_relobj
<64, false>* object
,
247 unsigned int data_shndx
,
248 Output_section
* output_section
,
249 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
250 const elfcpp::Sym
<64, false>& lsym
);
253 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
254 Sized_relobj
<64, false>* object
,
255 unsigned int data_shndx
,
256 Output_section
* output_section
,
257 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
261 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
262 Target_x86_64
* target
,
263 Sized_relobj
<64, false>* object
,
264 unsigned int data_shndx
,
265 Output_section
* output_section
,
266 const elfcpp::Rela
<64, false>& reloc
,
268 const elfcpp::Sym
<64, false>& lsym
);
271 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
272 Target_x86_64
* target
,
273 Sized_relobj
<64, false>* object
,
274 unsigned int data_shndx
,
275 Output_section
* output_section
,
276 const elfcpp::Rela
<64, false>& reloc
,
282 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
285 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
289 check_non_pic(Relobj
*, unsigned int r_type
);
292 possible_function_pointer_reloc(unsigned int r_type
);
294 // Whether we have issued an error about a non-PIC compilation.
295 bool issued_non_pic_error_
;
298 // The class which implements relocation.
303 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
308 if (this->skip_call_tls_get_addr_
)
310 // FIXME: This needs to specify the location somehow.
311 gold_error(_("missing expected TLS relocation"));
315 // Do a relocation. Return false if the caller should not issue
316 // any warnings about this relocation.
318 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, Output_section
*,
319 size_t relnum
, const elfcpp::Rela
<64, false>&,
320 unsigned int r_type
, const Sized_symbol
<64>*,
321 const Symbol_value
<64>*,
322 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
326 // Do a TLS relocation.
328 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
329 size_t relnum
, const elfcpp::Rela
<64, false>&,
330 unsigned int r_type
, const Sized_symbol
<64>*,
331 const Symbol_value
<64>*,
332 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
335 // Do a TLS General-Dynamic to Initial-Exec transition.
337 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
338 Output_segment
* tls_segment
,
339 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
340 elfcpp::Elf_types
<64>::Elf_Addr value
,
342 elfcpp::Elf_types
<64>::Elf_Addr
,
343 section_size_type view_size
);
345 // Do a TLS General-Dynamic to Local-Exec transition.
347 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
348 Output_segment
* tls_segment
,
349 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
350 elfcpp::Elf_types
<64>::Elf_Addr value
,
352 section_size_type view_size
);
354 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
356 tls_desc_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
357 Output_segment
* tls_segment
,
358 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
359 elfcpp::Elf_types
<64>::Elf_Addr value
,
361 elfcpp::Elf_types
<64>::Elf_Addr
,
362 section_size_type view_size
);
364 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
366 tls_desc_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
367 Output_segment
* tls_segment
,
368 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
369 elfcpp::Elf_types
<64>::Elf_Addr value
,
371 section_size_type view_size
);
373 // Do a TLS Local-Dynamic to Local-Exec transition.
375 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
376 Output_segment
* tls_segment
,
377 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
378 elfcpp::Elf_types
<64>::Elf_Addr value
,
380 section_size_type view_size
);
382 // Do a TLS Initial-Exec to Local-Exec transition.
384 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
385 Output_segment
* tls_segment
,
386 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
387 elfcpp::Elf_types
<64>::Elf_Addr value
,
389 section_size_type view_size
);
391 // This is set if we should skip the next reloc, which should be a
392 // PLT32 reloc against ___tls_get_addr.
393 bool skip_call_tls_get_addr_
;
395 // This is set if we see a relocation which could load the address
396 // of the TLS block. Whether we see such a relocation determines
397 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
398 // in debugging sections.
399 bool saw_tls_block_reloc_
;
402 // A class which returns the size required for a relocation type,
403 // used while scanning relocs during a relocatable link.
404 class Relocatable_size_for_reloc
408 get_size_for_reloc(unsigned int, Relobj
*);
411 // Adjust TLS relocation type based on the options and whether this
412 // is a local symbol.
413 static tls::Tls_optimization
414 optimize_tls_reloc(bool is_final
, int r_type
);
416 // Get the GOT section, creating it if necessary.
417 Output_data_got
<64, false>*
418 got_section(Symbol_table
*, Layout
*);
420 // Get the GOT PLT section.
422 got_plt_section() const
424 gold_assert(this->got_plt_
!= NULL
);
425 return this->got_plt_
;
428 // Get the GOT section for TLSDESC entries.
429 Output_data_got
<64, false>*
430 got_tlsdesc_section() const
432 gold_assert(this->got_tlsdesc_
!= NULL
);
433 return this->got_tlsdesc_
;
436 // Create the PLT section.
438 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
440 // Create a PLT entry for a global symbol.
442 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
444 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
446 define_tls_base_symbol(Symbol_table
*, Layout
*);
448 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
450 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
452 // Create a GOT entry for the TLS module index.
454 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
455 Sized_relobj
<64, false>* object
);
457 // Get the PLT section.
458 Output_data_plt_x86_64
*
461 gold_assert(this->plt_
!= NULL
);
465 // Get the dynamic reloc section, creating it if necessary.
467 rela_dyn_section(Layout
*);
469 // Get the section to use for TLSDESC relocations.
471 rela_tlsdesc_section(Layout
*) const;
473 // Add a potential copy relocation.
475 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
476 Sized_relobj
<64, false>* object
,
477 unsigned int shndx
, Output_section
* output_section
,
478 Symbol
* sym
, const elfcpp::Rela
<64, false>& reloc
)
480 this->copy_relocs_
.copy_reloc(symtab
, layout
,
481 symtab
->get_sized_symbol
<64>(sym
),
482 object
, shndx
, output_section
,
483 reloc
, this->rela_dyn_section(layout
));
486 // Information about this specific target which we pass to the
487 // general Target structure.
488 static const Target::Target_info x86_64_info
;
490 // The types of GOT entries needed for this platform.
491 // These values are exposed to the ABI in an incremental link.
492 // Do not renumber existing values without changing the version
493 // number of the .gnu_incremental_inputs section.
496 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
497 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
498 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
499 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
502 // This type is used as the argument to the target specific
503 // relocation routines. The only target specific reloc is
504 // R_X86_64_TLSDESC against a local symbol.
507 Tlsdesc_info(Sized_relobj
<64, false>* a_object
, unsigned int a_r_sym
)
508 : object(a_object
), r_sym(a_r_sym
)
511 // The object in which the local symbol is defined.
512 Sized_relobj
<64, false>* object
;
513 // The local symbol index in the object.
518 Output_data_got
<64, false>* got_
;
520 Output_data_plt_x86_64
* plt_
;
521 // The GOT PLT section.
522 Output_data_space
* got_plt_
;
523 // The GOT section for TLSDESC relocations.
524 Output_data_got
<64, false>* got_tlsdesc_
;
525 // The _GLOBAL_OFFSET_TABLE_ symbol.
526 Symbol
* global_offset_table_
;
527 // The dynamic reloc section.
528 Reloc_section
* rela_dyn_
;
529 // Relocs saved to avoid a COPY reloc.
530 Copy_relocs
<elfcpp::SHT_RELA
, 64, false> copy_relocs_
;
531 // Space for variables copied with a COPY reloc.
532 Output_data_space
* dynbss_
;
533 // Offset of the GOT entry for the TLS module index.
534 unsigned int got_mod_index_offset_
;
535 // We handle R_X86_64_TLSDESC against a local symbol as a target
536 // specific relocation. Here we store the object and local symbol
537 // index for the relocation.
538 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
539 // True if the _TLS_MODULE_BASE_ symbol has been defined.
540 bool tls_base_symbol_defined_
;
543 const Target::Target_info
Target_x86_64::x86_64_info
=
546 false, // is_big_endian
547 elfcpp::EM_X86_64
, // machine_code
548 false, // has_make_symbol
549 false, // has_resolve
550 true, // has_code_fill
551 true, // is_default_stack_executable
553 "/lib/ld64.so.1", // program interpreter
554 0x400000, // default_text_segment_address
555 0x1000, // abi_pagesize (overridable by -z max-page-size)
556 0x1000, // common_pagesize (overridable by -z common-page-size)
557 elfcpp::SHN_UNDEF
, // small_common_shndx
558 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
559 0, // small_common_section_flags
560 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
561 NULL
, // attributes_section
562 NULL
// attributes_vendor
565 // This is called when a new output section is created. This is where
566 // we handle the SHF_X86_64_LARGE.
569 Target_x86_64::do_new_output_section(Output_section
*os
) const
571 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
572 os
->set_is_large_section();
575 // Get the GOT section, creating it if necessary.
577 Output_data_got
<64, false>*
578 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
580 if (this->got_
== NULL
)
582 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
584 this->got_
= new Output_data_got
<64, false>();
586 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
588 | elfcpp::SHF_WRITE
),
589 this->got_
, ORDER_RELRO_LAST
,
592 this->got_plt_
= new Output_data_space(8, "** GOT PLT");
593 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
595 | elfcpp::SHF_WRITE
),
596 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
599 // The first three entries are reserved.
600 this->got_plt_
->set_current_data_size(3 * 8);
602 // Those bytes can go into the relro segment.
603 layout
->increase_relro(3 * 8);
605 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
606 this->global_offset_table_
=
607 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
608 Symbol_table::PREDEFINED
,
610 0, 0, elfcpp::STT_OBJECT
,
612 elfcpp::STV_HIDDEN
, 0,
615 // If there are any TLSDESC relocations, they get GOT entries in
616 // .got.plt after the jump slot entries.
617 this->got_tlsdesc_
= new Output_data_got
<64, false>();
618 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
620 | elfcpp::SHF_WRITE
),
622 ORDER_NON_RELRO_FIRST
, false);
628 // Get the dynamic reloc section, creating it if necessary.
630 Target_x86_64::Reloc_section
*
631 Target_x86_64::rela_dyn_section(Layout
* layout
)
633 if (this->rela_dyn_
== NULL
)
635 gold_assert(layout
!= NULL
);
636 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
637 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
638 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
639 ORDER_DYNAMIC_RELOCS
, false);
641 return this->rela_dyn_
;
644 // A class to handle the PLT data.
646 class Output_data_plt_x86_64
: public Output_section_data
649 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
651 Output_data_plt_x86_64(Layout
*, Output_data_got
<64, false>*,
654 // Add an entry to the PLT.
656 add_entry(Symbol
* gsym
);
658 // Add the reserved TLSDESC_PLT entry to the PLT.
660 reserve_tlsdesc_entry(unsigned int got_offset
)
661 { this->tlsdesc_got_offset_
= got_offset
; }
663 // Return true if a TLSDESC_PLT entry has been reserved.
665 has_tlsdesc_entry() const
666 { return this->tlsdesc_got_offset_
!= -1U; }
668 // Return the GOT offset for the reserved TLSDESC_PLT entry.
670 get_tlsdesc_got_offset() const
671 { return this->tlsdesc_got_offset_
; }
673 // Return the offset of the reserved TLSDESC_PLT entry.
675 get_tlsdesc_plt_offset() const
676 { return (this->count_
+ 1) * plt_entry_size
; }
678 // Return the .rela.plt section data.
681 { return this->rel_
; }
683 // Return where the TLSDESC relocations should go.
685 rela_tlsdesc(Layout
*);
687 // Return the number of PLT entries.
690 { return this->count_
; }
692 // Return the offset of the first non-reserved PLT entry.
694 first_plt_entry_offset()
695 { return plt_entry_size
; }
697 // Return the size of a PLT entry.
700 { return plt_entry_size
; }
704 do_adjust_output_section(Output_section
* os
);
706 // Write to a map file.
708 do_print_to_mapfile(Mapfile
* mapfile
) const
709 { mapfile
->print_output_data(this, _("** PLT")); }
712 // The size of an entry in the PLT.
713 static const int plt_entry_size
= 16;
715 // The first entry in the PLT.
716 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
717 // procedure linkage table for both programs and shared objects."
718 static unsigned char first_plt_entry
[plt_entry_size
];
720 // Other entries in the PLT for an executable.
721 static unsigned char plt_entry
[plt_entry_size
];
723 // The reserved TLSDESC entry in the PLT for an executable.
724 static unsigned char tlsdesc_plt_entry
[plt_entry_size
];
726 // Set the final size.
728 set_final_data_size();
730 // Write out the PLT data.
732 do_write(Output_file
*);
734 // The reloc section.
736 // The TLSDESC relocs, if necessary. These must follow the regular
738 Reloc_section
* tlsdesc_rel_
;
740 Output_data_got
<64, false>* got_
;
741 // The .got.plt section.
742 Output_data_space
* got_plt_
;
743 // The number of PLT entries.
745 // Offset of the reserved TLSDESC_GOT entry when needed.
746 unsigned int tlsdesc_got_offset_
;
749 // Create the PLT section. The ordinary .got section is an argument,
750 // since we need to refer to the start. We also create our own .got
751 // section just for PLT entries.
753 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
754 Output_data_got
<64, false>* got
,
755 Output_data_space
* got_plt
)
756 : Output_section_data(8), tlsdesc_rel_(NULL
), got_(got
), got_plt_(got_plt
),
757 count_(0), tlsdesc_got_offset_(-1U)
759 this->rel_
= new Reloc_section(false);
760 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
761 elfcpp::SHF_ALLOC
, this->rel_
,
762 ORDER_DYNAMIC_PLT_RELOCS
, false);
766 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
768 os
->set_entsize(plt_entry_size
);
771 // Add an entry to the PLT.
774 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
776 gold_assert(!gsym
->has_plt_offset());
778 // Note that when setting the PLT offset we skip the initial
779 // reserved PLT entry.
780 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
784 section_offset_type got_offset
= this->got_plt_
->current_data_size();
786 // Every PLT entry needs a GOT entry which points back to the PLT
787 // entry (this will be changed by the dynamic linker, normally
788 // lazily when the function is called).
789 this->got_plt_
->set_current_data_size(got_offset
+ 8);
791 // Every PLT entry needs a reloc.
792 gsym
->set_needs_dynsym_entry();
793 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
796 // Note that we don't need to save the symbol. The contents of the
797 // PLT are independent of which symbols are used. The symbols only
798 // appear in the relocations.
801 // Return where the TLSDESC relocations should go, creating it if
802 // necessary. These follow the JUMP_SLOT relocations.
804 Output_data_plt_x86_64::Reloc_section
*
805 Output_data_plt_x86_64::rela_tlsdesc(Layout
* layout
)
807 if (this->tlsdesc_rel_
== NULL
)
809 this->tlsdesc_rel_
= new Reloc_section(false);
810 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
811 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
812 ORDER_DYNAMIC_PLT_RELOCS
, false);
813 gold_assert(this->tlsdesc_rel_
->output_section() ==
814 this->rel_
->output_section());
816 return this->tlsdesc_rel_
;
819 // Set the final size.
821 Output_data_plt_x86_64::set_final_data_size()
823 unsigned int count
= this->count_
;
824 if (this->has_tlsdesc_entry())
826 this->set_data_size((count
+ 1) * plt_entry_size
);
829 // The first entry in the PLT for an executable.
831 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
833 // From AMD64 ABI Draft 0.98, page 76
834 0xff, 0x35, // pushq contents of memory address
835 0, 0, 0, 0, // replaced with address of .got + 8
836 0xff, 0x25, // jmp indirect
837 0, 0, 0, 0, // replaced with address of .got + 16
838 0x90, 0x90, 0x90, 0x90 // noop (x4)
841 // Subsequent entries in the PLT for an executable.
843 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
845 // From AMD64 ABI Draft 0.98, page 76
846 0xff, 0x25, // jmpq indirect
847 0, 0, 0, 0, // replaced with address of symbol in .got
848 0x68, // pushq immediate
849 0, 0, 0, 0, // replaced with offset into relocation table
850 0xe9, // jmpq relative
851 0, 0, 0, 0 // replaced with offset to start of .plt
854 // The reserved TLSDESC entry in the PLT for an executable.
856 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry
[plt_entry_size
] =
858 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
859 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
860 0xff, 0x35, // pushq x(%rip)
861 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
862 0xff, 0x25, // jmpq *y(%rip)
863 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
868 // Write out the PLT. This uses the hand-coded instructions above,
869 // and adjusts them as needed. This is specified by the AMD64 ABI.
872 Output_data_plt_x86_64::do_write(Output_file
* of
)
874 const off_t offset
= this->offset();
875 const section_size_type oview_size
=
876 convert_to_section_size_type(this->data_size());
877 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
879 const off_t got_file_offset
= this->got_plt_
->offset();
880 const section_size_type got_size
=
881 convert_to_section_size_type(this->got_plt_
->data_size());
882 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
885 unsigned char* pov
= oview
;
887 // The base address of the .plt section.
888 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
889 // The base address of the .got section.
890 elfcpp::Elf_types
<64>::Elf_Addr got_base
= this->got_
->address();
891 // The base address of the PLT portion of the .got section,
892 // which is where the GOT pointer will point, and where the
893 // three reserved GOT entries are located.
894 elfcpp::Elf_types
<64>::Elf_Addr got_address
= this->got_plt_
->address();
896 memcpy(pov
, first_plt_entry
, plt_entry_size
);
897 // We do a jmp relative to the PC at the end of this instruction.
898 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
900 - (plt_address
+ 6)));
901 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
903 - (plt_address
+ 12)));
904 pov
+= plt_entry_size
;
906 unsigned char* got_pov
= got_view
;
908 memset(got_pov
, 0, 24);
911 unsigned int plt_offset
= plt_entry_size
;
912 unsigned int got_offset
= 24;
913 const unsigned int count
= this->count_
;
914 for (unsigned int plt_index
= 0;
917 pov
+= plt_entry_size
,
919 plt_offset
+= plt_entry_size
,
922 // Set and adjust the PLT entry itself.
923 memcpy(pov
, plt_entry
, plt_entry_size
);
924 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
925 (got_address
+ got_offset
926 - (plt_address
+ plt_offset
929 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
930 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
931 - (plt_offset
+ plt_entry_size
));
933 // Set the entry in the GOT.
934 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
937 if (this->has_tlsdesc_entry())
939 // Set and adjust the reserved TLSDESC PLT entry.
940 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
941 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
942 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
944 - (plt_address
+ plt_offset
946 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
949 - (plt_address
+ plt_offset
951 pov
+= plt_entry_size
;
954 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
955 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
957 of
->write_output_view(offset
, oview_size
, oview
);
958 of
->write_output_view(got_file_offset
, got_size
, got_view
);
961 // Create the PLT section.
964 Target_x86_64::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
966 if (this->plt_
== NULL
)
968 // Create the GOT sections first.
969 this->got_section(symtab
, layout
);
971 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_
,
973 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
975 | elfcpp::SHF_EXECINSTR
),
976 this->plt_
, ORDER_PLT
, false);
980 // Return the section for TLSDESC relocations.
982 Target_x86_64::Reloc_section
*
983 Target_x86_64::rela_tlsdesc_section(Layout
* layout
) const
985 return this->plt_section()->rela_tlsdesc(layout
);
988 // Create a PLT entry for a global symbol.
991 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
994 if (gsym
->has_plt_offset())
997 if (this->plt_
== NULL
)
998 this->make_plt_section(symtab
, layout
);
1000 this->plt_
->add_entry(gsym
);
1003 // Return the number of entries in the PLT.
1006 Target_x86_64::plt_entry_count() const
1008 if (this->plt_
== NULL
)
1010 return this->plt_
->entry_count();
1013 // Return the offset of the first non-reserved PLT entry.
1016 Target_x86_64::first_plt_entry_offset() const
1018 return Output_data_plt_x86_64::first_plt_entry_offset();
1021 // Return the size of each PLT entry.
1024 Target_x86_64::plt_entry_size() const
1026 return Output_data_plt_x86_64::get_plt_entry_size();
1029 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1032 Target_x86_64::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
1034 if (this->tls_base_symbol_defined_
)
1037 Output_segment
* tls_segment
= layout
->tls_segment();
1038 if (tls_segment
!= NULL
)
1040 bool is_exec
= parameters
->options().output_is_executable();
1041 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1042 Symbol_table::PREDEFINED
,
1046 elfcpp::STV_HIDDEN
, 0,
1048 ? Symbol::SEGMENT_END
1049 : Symbol::SEGMENT_START
),
1052 this->tls_base_symbol_defined_
= true;
1055 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1058 Target_x86_64::reserve_tlsdesc_entries(Symbol_table
* symtab
,
1061 if (this->plt_
== NULL
)
1062 this->make_plt_section(symtab
, layout
);
1064 if (!this->plt_
->has_tlsdesc_entry())
1066 // Allocate the TLSDESC_GOT entry.
1067 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1068 unsigned int got_offset
= got
->add_constant(0);
1070 // Allocate the TLSDESC_PLT entry.
1071 this->plt_
->reserve_tlsdesc_entry(got_offset
);
1075 // Create a GOT entry for the TLS module index.
1078 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1079 Sized_relobj
<64, false>* object
)
1081 if (this->got_mod_index_offset_
== -1U)
1083 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1084 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1085 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1086 unsigned int got_offset
= got
->add_constant(0);
1087 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
1089 got
->add_constant(0);
1090 this->got_mod_index_offset_
= got_offset
;
1092 return this->got_mod_index_offset_
;
1095 // Optimize the TLS relocation type based on what we know about the
1096 // symbol. IS_FINAL is true if the final address of this symbol is
1097 // known at link time.
1099 tls::Tls_optimization
1100 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
1102 // If we are generating a shared library, then we can't do anything
1104 if (parameters
->options().shared())
1105 return tls::TLSOPT_NONE
;
1109 case elfcpp::R_X86_64_TLSGD
:
1110 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1111 case elfcpp::R_X86_64_TLSDESC_CALL
:
1112 // These are General-Dynamic which permits fully general TLS
1113 // access. Since we know that we are generating an executable,
1114 // we can convert this to Initial-Exec. If we also know that
1115 // this is a local symbol, we can further switch to Local-Exec.
1117 return tls::TLSOPT_TO_LE
;
1118 return tls::TLSOPT_TO_IE
;
1120 case elfcpp::R_X86_64_TLSLD
:
1121 // This is Local-Dynamic, which refers to a local symbol in the
1122 // dynamic TLS block. Since we know that we generating an
1123 // executable, we can switch to Local-Exec.
1124 return tls::TLSOPT_TO_LE
;
1126 case elfcpp::R_X86_64_DTPOFF32
:
1127 case elfcpp::R_X86_64_DTPOFF64
:
1128 // Another Local-Dynamic reloc.
1129 return tls::TLSOPT_TO_LE
;
1131 case elfcpp::R_X86_64_GOTTPOFF
:
1132 // These are Initial-Exec relocs which get the thread offset
1133 // from the GOT. If we know that we are linking against the
1134 // local symbol, we can switch to Local-Exec, which links the
1135 // thread offset into the instruction.
1137 return tls::TLSOPT_TO_LE
;
1138 return tls::TLSOPT_NONE
;
1140 case elfcpp::R_X86_64_TPOFF32
:
1141 // When we already have Local-Exec, there is nothing further we
1143 return tls::TLSOPT_NONE
;
1150 // Report an unsupported relocation against a local symbol.
1153 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
1154 unsigned int r_type
)
1156 gold_error(_("%s: unsupported reloc %u against local symbol"),
1157 object
->name().c_str(), r_type
);
1160 // We are about to emit a dynamic relocation of type R_TYPE. If the
1161 // dynamic linker does not support it, issue an error. The GNU linker
1162 // only issues a non-PIC error for an allocated read-only section.
1163 // Here we know the section is allocated, but we don't know that it is
1164 // read-only. But we check for all the relocation types which the
1165 // glibc dynamic linker supports, so it seems appropriate to issue an
1166 // error even if the section is not read-only.
1169 Target_x86_64::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
)
1173 // These are the relocation types supported by glibc for x86_64.
1174 case elfcpp::R_X86_64_RELATIVE
:
1175 case elfcpp::R_X86_64_GLOB_DAT
:
1176 case elfcpp::R_X86_64_JUMP_SLOT
:
1177 case elfcpp::R_X86_64_DTPMOD64
:
1178 case elfcpp::R_X86_64_DTPOFF64
:
1179 case elfcpp::R_X86_64_TPOFF64
:
1180 case elfcpp::R_X86_64_64
:
1181 case elfcpp::R_X86_64_32
:
1182 case elfcpp::R_X86_64_PC32
:
1183 case elfcpp::R_X86_64_COPY
:
1187 // This prevents us from issuing more than one error per reloc
1188 // section. But we can still wind up issuing more than one
1189 // error per object file.
1190 if (this->issued_non_pic_error_
)
1192 gold_assert(parameters
->options().output_is_position_independent());
1193 object
->error(_("requires unsupported dynamic reloc; "
1194 "recompile with -fPIC"));
1195 this->issued_non_pic_error_
= true;
1198 case elfcpp::R_X86_64_NONE
:
1203 // Scan a relocation for a local symbol.
1206 Target_x86_64::Scan::local(Symbol_table
* symtab
,
1208 Target_x86_64
* target
,
1209 Sized_relobj
<64, false>* object
,
1210 unsigned int data_shndx
,
1211 Output_section
* output_section
,
1212 const elfcpp::Rela
<64, false>& reloc
,
1213 unsigned int r_type
,
1214 const elfcpp::Sym
<64, false>& lsym
)
1218 case elfcpp::R_X86_64_NONE
:
1219 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1220 case elfcpp::R_X86_64_GNU_VTENTRY
:
1223 case elfcpp::R_X86_64_64
:
1224 // If building a shared library (or a position-independent
1225 // executable), we need to create a dynamic relocation for this
1226 // location. The relocation applied at link time will apply the
1227 // link-time value, so we flag the location with an
1228 // R_X86_64_RELATIVE relocation so the dynamic loader can
1229 // relocate it easily.
1230 if (parameters
->options().output_is_position_independent())
1232 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1233 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1234 rela_dyn
->add_local_relative(object
, r_sym
,
1235 elfcpp::R_X86_64_RELATIVE
,
1236 output_section
, data_shndx
,
1237 reloc
.get_r_offset(),
1238 reloc
.get_r_addend());
1242 case elfcpp::R_X86_64_32
:
1243 case elfcpp::R_X86_64_32S
:
1244 case elfcpp::R_X86_64_16
:
1245 case elfcpp::R_X86_64_8
:
1246 // If building a shared library (or a position-independent
1247 // executable), we need to create a dynamic relocation for this
1248 // location. We can't use an R_X86_64_RELATIVE relocation
1249 // because that is always a 64-bit relocation.
1250 if (parameters
->options().output_is_position_independent())
1252 this->check_non_pic(object
, r_type
);
1254 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1255 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1256 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1257 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1258 data_shndx
, reloc
.get_r_offset(),
1259 reloc
.get_r_addend());
1262 gold_assert(lsym
.get_st_value() == 0);
1263 unsigned int shndx
= lsym
.get_st_shndx();
1265 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1268 object
->error(_("section symbol %u has bad shndx %u"),
1271 rela_dyn
->add_local_section(object
, shndx
,
1272 r_type
, output_section
,
1273 data_shndx
, reloc
.get_r_offset(),
1274 reloc
.get_r_addend());
1279 case elfcpp::R_X86_64_PC64
:
1280 case elfcpp::R_X86_64_PC32
:
1281 case elfcpp::R_X86_64_PC16
:
1282 case elfcpp::R_X86_64_PC8
:
1285 case elfcpp::R_X86_64_PLT32
:
1286 // Since we know this is a local symbol, we can handle this as a
1290 case elfcpp::R_X86_64_GOTPC32
:
1291 case elfcpp::R_X86_64_GOTOFF64
:
1292 case elfcpp::R_X86_64_GOTPC64
:
1293 case elfcpp::R_X86_64_PLTOFF64
:
1294 // We need a GOT section.
1295 target
->got_section(symtab
, layout
);
1296 // For PLTOFF64, we'd normally want a PLT section, but since we
1297 // know this is a local symbol, no PLT is needed.
1300 case elfcpp::R_X86_64_GOT64
:
1301 case elfcpp::R_X86_64_GOT32
:
1302 case elfcpp::R_X86_64_GOTPCREL64
:
1303 case elfcpp::R_X86_64_GOTPCREL
:
1304 case elfcpp::R_X86_64_GOTPLT64
:
1306 // The symbol requires a GOT entry.
1307 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1308 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1309 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
1311 // If we are generating a shared object, we need to add a
1312 // dynamic relocation for this symbol's GOT entry.
1313 if (parameters
->options().output_is_position_independent())
1315 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1316 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1317 if (r_type
!= elfcpp::R_X86_64_GOT32
)
1318 rela_dyn
->add_local_relative(
1319 object
, r_sym
, elfcpp::R_X86_64_RELATIVE
, got
,
1320 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1323 this->check_non_pic(object
, r_type
);
1325 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1326 rela_dyn
->add_local(
1327 object
, r_sym
, r_type
, got
,
1328 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1332 // For GOTPLT64, we'd normally want a PLT section, but since
1333 // we know this is a local symbol, no PLT is needed.
1337 case elfcpp::R_X86_64_COPY
:
1338 case elfcpp::R_X86_64_GLOB_DAT
:
1339 case elfcpp::R_X86_64_JUMP_SLOT
:
1340 case elfcpp::R_X86_64_RELATIVE
:
1341 // These are outstanding tls relocs, which are unexpected when linking
1342 case elfcpp::R_X86_64_TPOFF64
:
1343 case elfcpp::R_X86_64_DTPMOD64
:
1344 case elfcpp::R_X86_64_TLSDESC
:
1345 gold_error(_("%s: unexpected reloc %u in object file"),
1346 object
->name().c_str(), r_type
);
1349 // These are initial tls relocs, which are expected when linking
1350 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1351 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1352 case elfcpp::R_X86_64_TLSDESC_CALL
:
1353 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1354 case elfcpp::R_X86_64_DTPOFF32
:
1355 case elfcpp::R_X86_64_DTPOFF64
:
1356 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1357 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1359 bool output_is_shared
= parameters
->options().shared();
1360 const tls::Tls_optimization optimized_type
1361 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
1364 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1365 if (optimized_type
== tls::TLSOPT_NONE
)
1367 // Create a pair of GOT entries for the module index and
1368 // dtv-relative offset.
1369 Output_data_got
<64, false>* got
1370 = target
->got_section(symtab
, layout
);
1371 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1372 unsigned int shndx
= lsym
.get_st_shndx();
1374 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1376 object
->error(_("local symbol %u has bad shndx %u"),
1379 got
->add_local_pair_with_rela(object
, r_sym
,
1382 target
->rela_dyn_section(layout
),
1383 elfcpp::R_X86_64_DTPMOD64
, 0);
1385 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1386 unsupported_reloc_local(object
, r_type
);
1389 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1390 target
->define_tls_base_symbol(symtab
, layout
);
1391 if (optimized_type
== tls::TLSOPT_NONE
)
1393 // Create reserved PLT and GOT entries for the resolver.
1394 target
->reserve_tlsdesc_entries(symtab
, layout
);
1396 // Generate a double GOT entry with an
1397 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
1398 // is resolved lazily, so the GOT entry needs to be in
1399 // an area in .got.plt, not .got. Call got_section to
1400 // make sure the section has been created.
1401 target
->got_section(symtab
, layout
);
1402 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
1403 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1404 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
1406 unsigned int got_offset
= got
->add_constant(0);
1407 got
->add_constant(0);
1408 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
1410 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
1411 // We store the arguments we need in a vector, and
1412 // use the index into the vector as the parameter
1413 // to pass to the target specific routines.
1414 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
1415 void* arg
= reinterpret_cast<void*>(intarg
);
1416 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
1417 got
, got_offset
, 0);
1420 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1421 unsupported_reloc_local(object
, r_type
);
1424 case elfcpp::R_X86_64_TLSDESC_CALL
:
1427 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1428 if (optimized_type
== tls::TLSOPT_NONE
)
1430 // Create a GOT entry for the module index.
1431 target
->got_mod_index_entry(symtab
, layout
, object
);
1433 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1434 unsupported_reloc_local(object
, r_type
);
1437 case elfcpp::R_X86_64_DTPOFF32
:
1438 case elfcpp::R_X86_64_DTPOFF64
:
1441 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1442 layout
->set_has_static_tls();
1443 if (optimized_type
== tls::TLSOPT_NONE
)
1445 // Create a GOT entry for the tp-relative offset.
1446 Output_data_got
<64, false>* got
1447 = target
->got_section(symtab
, layout
);
1448 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1449 got
->add_local_with_rela(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1450 target
->rela_dyn_section(layout
),
1451 elfcpp::R_X86_64_TPOFF64
);
1453 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1454 unsupported_reloc_local(object
, r_type
);
1457 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1458 layout
->set_has_static_tls();
1459 if (output_is_shared
)
1460 unsupported_reloc_local(object
, r_type
);
1469 case elfcpp::R_X86_64_SIZE32
:
1470 case elfcpp::R_X86_64_SIZE64
:
1472 gold_error(_("%s: unsupported reloc %u against local symbol"),
1473 object
->name().c_str(), r_type
);
1479 // Report an unsupported relocation against a global symbol.
1482 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
1483 unsigned int r_type
,
1486 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1487 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1490 // Returns true if this relocation type could be that of a function pointer.
1492 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type
)
1496 case elfcpp::R_X86_64_64
:
1497 case elfcpp::R_X86_64_32
:
1498 case elfcpp::R_X86_64_32S
:
1499 case elfcpp::R_X86_64_16
:
1500 case elfcpp::R_X86_64_8
:
1501 case elfcpp::R_X86_64_GOT64
:
1502 case elfcpp::R_X86_64_GOT32
:
1503 case elfcpp::R_X86_64_GOTPCREL64
:
1504 case elfcpp::R_X86_64_GOTPCREL
:
1505 case elfcpp::R_X86_64_GOTPLT64
:
1513 // For safe ICF, scan a relocation for a local symbol to check if it
1514 // corresponds to a function pointer being taken. In that case mark
1515 // the function whose pointer was taken as not foldable.
1518 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
1522 Sized_relobj
<64, false>* ,
1525 const elfcpp::Rela
<64, false>& ,
1526 unsigned int r_type
,
1527 const elfcpp::Sym
<64, false>&)
1529 // When building a shared library, do not fold any local symbols as it is
1530 // not possible to distinguish pointer taken versus a call by looking at
1531 // the relocation types.
1532 return (parameters
->options().shared()
1533 || possible_function_pointer_reloc(r_type
));
1536 // For safe ICF, scan a relocation for a global symbol to check if it
1537 // corresponds to a function pointer being taken. In that case mark
1538 // the function whose pointer was taken as not foldable.
1541 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
1545 Sized_relobj
<64, false>* ,
1548 const elfcpp::Rela
<64, false>& ,
1549 unsigned int r_type
,
1552 // When building a shared library, do not fold symbols whose visibility
1553 // is hidden, internal or protected.
1554 return ((parameters
->options().shared()
1555 && (gsym
->visibility() == elfcpp::STV_INTERNAL
1556 || gsym
->visibility() == elfcpp::STV_PROTECTED
1557 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
1558 || possible_function_pointer_reloc(r_type
));
1561 // Scan a relocation for a global symbol.
1564 Target_x86_64::Scan::global(Symbol_table
* symtab
,
1566 Target_x86_64
* target
,
1567 Sized_relobj
<64, false>* object
,
1568 unsigned int data_shndx
,
1569 Output_section
* output_section
,
1570 const elfcpp::Rela
<64, false>& reloc
,
1571 unsigned int r_type
,
1576 case elfcpp::R_X86_64_NONE
:
1577 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1578 case elfcpp::R_X86_64_GNU_VTENTRY
:
1581 case elfcpp::R_X86_64_64
:
1582 case elfcpp::R_X86_64_32
:
1583 case elfcpp::R_X86_64_32S
:
1584 case elfcpp::R_X86_64_16
:
1585 case elfcpp::R_X86_64_8
:
1587 // Make a PLT entry if necessary.
1588 if (gsym
->needs_plt_entry())
1590 target
->make_plt_entry(symtab
, layout
, gsym
);
1591 // Since this is not a PC-relative relocation, we may be
1592 // taking the address of a function. In that case we need to
1593 // set the entry in the dynamic symbol table to the address of
1595 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1596 gsym
->set_needs_dynsym_value();
1598 // Make a dynamic relocation if necessary.
1599 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1601 if (gsym
->may_need_copy_reloc())
1603 target
->copy_reloc(symtab
, layout
, object
,
1604 data_shndx
, output_section
, gsym
, reloc
);
1606 else if (r_type
== elfcpp::R_X86_64_64
1607 && gsym
->can_use_relative_reloc(false))
1609 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1610 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1611 output_section
, object
,
1612 data_shndx
, reloc
.get_r_offset(),
1613 reloc
.get_r_addend());
1617 this->check_non_pic(object
, r_type
);
1618 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1619 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1620 data_shndx
, reloc
.get_r_offset(),
1621 reloc
.get_r_addend());
1627 case elfcpp::R_X86_64_PC64
:
1628 case elfcpp::R_X86_64_PC32
:
1629 case elfcpp::R_X86_64_PC16
:
1630 case elfcpp::R_X86_64_PC8
:
1632 // Make a PLT entry if necessary.
1633 if (gsym
->needs_plt_entry())
1634 target
->make_plt_entry(symtab
, layout
, gsym
);
1635 // Make a dynamic relocation if necessary.
1636 int flags
= Symbol::NON_PIC_REF
;
1637 if (gsym
->is_func())
1638 flags
|= Symbol::FUNCTION_CALL
;
1639 if (gsym
->needs_dynamic_reloc(flags
))
1641 if (gsym
->may_need_copy_reloc())
1643 target
->copy_reloc(symtab
, layout
, object
,
1644 data_shndx
, output_section
, gsym
, reloc
);
1648 this->check_non_pic(object
, r_type
);
1649 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1650 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1651 data_shndx
, reloc
.get_r_offset(),
1652 reloc
.get_r_addend());
1658 case elfcpp::R_X86_64_GOT64
:
1659 case elfcpp::R_X86_64_GOT32
:
1660 case elfcpp::R_X86_64_GOTPCREL64
:
1661 case elfcpp::R_X86_64_GOTPCREL
:
1662 case elfcpp::R_X86_64_GOTPLT64
:
1664 // The symbol requires a GOT entry.
1665 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1666 if (gsym
->final_value_is_known())
1667 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1670 // If this symbol is not fully resolved, we need to add a
1671 // dynamic relocation for it.
1672 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1673 if (gsym
->is_from_dynobj()
1674 || gsym
->is_undefined()
1675 || gsym
->is_preemptible())
1676 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1677 elfcpp::R_X86_64_GLOB_DAT
);
1680 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1681 rela_dyn
->add_global_relative(
1682 gsym
, elfcpp::R_X86_64_RELATIVE
, got
,
1683 gsym
->got_offset(GOT_TYPE_STANDARD
), 0);
1686 // For GOTPLT64, we also need a PLT entry (but only if the
1687 // symbol is not fully resolved).
1688 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1689 && !gsym
->final_value_is_known())
1690 target
->make_plt_entry(symtab
, layout
, gsym
);
1694 case elfcpp::R_X86_64_PLT32
:
1695 // If the symbol is fully resolved, this is just a PC32 reloc.
1696 // Otherwise we need a PLT entry.
1697 if (gsym
->final_value_is_known())
1699 // If building a shared library, we can also skip the PLT entry
1700 // if the symbol is defined in the output file and is protected
1702 if (gsym
->is_defined()
1703 && !gsym
->is_from_dynobj()
1704 && !gsym
->is_preemptible())
1706 target
->make_plt_entry(symtab
, layout
, gsym
);
1709 case elfcpp::R_X86_64_GOTPC32
:
1710 case elfcpp::R_X86_64_GOTOFF64
:
1711 case elfcpp::R_X86_64_GOTPC64
:
1712 case elfcpp::R_X86_64_PLTOFF64
:
1713 // We need a GOT section.
1714 target
->got_section(symtab
, layout
);
1715 // For PLTOFF64, we also need a PLT entry (but only if the
1716 // symbol is not fully resolved).
1717 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1718 && !gsym
->final_value_is_known())
1719 target
->make_plt_entry(symtab
, layout
, gsym
);
1722 case elfcpp::R_X86_64_COPY
:
1723 case elfcpp::R_X86_64_GLOB_DAT
:
1724 case elfcpp::R_X86_64_JUMP_SLOT
:
1725 case elfcpp::R_X86_64_RELATIVE
:
1726 // These are outstanding tls relocs, which are unexpected when linking
1727 case elfcpp::R_X86_64_TPOFF64
:
1728 case elfcpp::R_X86_64_DTPMOD64
:
1729 case elfcpp::R_X86_64_TLSDESC
:
1730 gold_error(_("%s: unexpected reloc %u in object file"),
1731 object
->name().c_str(), r_type
);
1734 // These are initial tls relocs, which are expected for global()
1735 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1736 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1737 case elfcpp::R_X86_64_TLSDESC_CALL
:
1738 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1739 case elfcpp::R_X86_64_DTPOFF32
:
1740 case elfcpp::R_X86_64_DTPOFF64
:
1741 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1742 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1744 const bool is_final
= gsym
->final_value_is_known();
1745 const tls::Tls_optimization optimized_type
1746 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1749 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1750 if (optimized_type
== tls::TLSOPT_NONE
)
1752 // Create a pair of GOT entries for the module index and
1753 // dtv-relative offset.
1754 Output_data_got
<64, false>* got
1755 = target
->got_section(symtab
, layout
);
1756 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_PAIR
,
1757 target
->rela_dyn_section(layout
),
1758 elfcpp::R_X86_64_DTPMOD64
,
1759 elfcpp::R_X86_64_DTPOFF64
);
1761 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1763 // Create a GOT entry for the tp-relative offset.
1764 Output_data_got
<64, false>* got
1765 = target
->got_section(symtab
, layout
);
1766 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1767 target
->rela_dyn_section(layout
),
1768 elfcpp::R_X86_64_TPOFF64
);
1770 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1771 unsupported_reloc_global(object
, r_type
, gsym
);
1774 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1775 target
->define_tls_base_symbol(symtab
, layout
);
1776 if (optimized_type
== tls::TLSOPT_NONE
)
1778 // Create reserved PLT and GOT entries for the resolver.
1779 target
->reserve_tlsdesc_entries(symtab
, layout
);
1781 // Create a double GOT entry with an R_X86_64_TLSDESC
1782 // reloc. The R_X86_64_TLSDESC reloc is resolved
1783 // lazily, so the GOT entry needs to be in an area in
1784 // .got.plt, not .got. Call got_section to make sure
1785 // the section has been created.
1786 target
->got_section(symtab
, layout
);
1787 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
1788 Reloc_section
*rt
= target
->rela_tlsdesc_section(layout
);
1789 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_DESC
, rt
,
1790 elfcpp::R_X86_64_TLSDESC
, 0);
1792 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1794 // Create a GOT entry for the tp-relative offset.
1795 Output_data_got
<64, false>* got
1796 = target
->got_section(symtab
, layout
);
1797 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1798 target
->rela_dyn_section(layout
),
1799 elfcpp::R_X86_64_TPOFF64
);
1801 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1802 unsupported_reloc_global(object
, r_type
, gsym
);
1805 case elfcpp::R_X86_64_TLSDESC_CALL
:
1808 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1809 if (optimized_type
== tls::TLSOPT_NONE
)
1811 // Create a GOT entry for the module index.
1812 target
->got_mod_index_entry(symtab
, layout
, object
);
1814 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1815 unsupported_reloc_global(object
, r_type
, gsym
);
1818 case elfcpp::R_X86_64_DTPOFF32
:
1819 case elfcpp::R_X86_64_DTPOFF64
:
1822 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1823 layout
->set_has_static_tls();
1824 if (optimized_type
== tls::TLSOPT_NONE
)
1826 // Create a GOT entry for the tp-relative offset.
1827 Output_data_got
<64, false>* got
1828 = target
->got_section(symtab
, layout
);
1829 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1830 target
->rela_dyn_section(layout
),
1831 elfcpp::R_X86_64_TPOFF64
);
1833 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1834 unsupported_reloc_global(object
, r_type
, gsym
);
1837 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1838 layout
->set_has_static_tls();
1839 if (parameters
->options().shared())
1840 unsupported_reloc_local(object
, r_type
);
1849 case elfcpp::R_X86_64_SIZE32
:
1850 case elfcpp::R_X86_64_SIZE64
:
1852 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1853 object
->name().c_str(), r_type
,
1854 gsym
->demangled_name().c_str());
1860 Target_x86_64::gc_process_relocs(Symbol_table
* symtab
,
1862 Sized_relobj
<64, false>* object
,
1863 unsigned int data_shndx
,
1864 unsigned int sh_type
,
1865 const unsigned char* prelocs
,
1867 Output_section
* output_section
,
1868 bool needs_special_offset_handling
,
1869 size_t local_symbol_count
,
1870 const unsigned char* plocal_symbols
)
1873 if (sh_type
== elfcpp::SHT_REL
)
1878 gold::gc_process_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1879 Target_x86_64::Scan
,
1880 Target_x86_64::Relocatable_size_for_reloc
>(
1889 needs_special_offset_handling
,
1894 // Scan relocations for a section.
1897 Target_x86_64::scan_relocs(Symbol_table
* symtab
,
1899 Sized_relobj
<64, false>* object
,
1900 unsigned int data_shndx
,
1901 unsigned int sh_type
,
1902 const unsigned char* prelocs
,
1904 Output_section
* output_section
,
1905 bool needs_special_offset_handling
,
1906 size_t local_symbol_count
,
1907 const unsigned char* plocal_symbols
)
1909 if (sh_type
== elfcpp::SHT_REL
)
1911 gold_error(_("%s: unsupported REL reloc section"),
1912 object
->name().c_str());
1916 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1917 Target_x86_64::Scan
>(
1926 needs_special_offset_handling
,
1931 // Finalize the sections.
1934 Target_x86_64::do_finalize_sections(
1936 const Input_objects
*,
1937 Symbol_table
* symtab
)
1939 const Reloc_section
* rel_plt
= (this->plt_
== NULL
1941 : this->plt_
->rela_plt());
1942 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
1943 this->rela_dyn_
, true, false);
1945 // Fill in some more dynamic tags.
1946 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1949 if (this->plt_
!= NULL
1950 && this->plt_
->output_section() != NULL
1951 && this->plt_
->has_tlsdesc_entry())
1953 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
1954 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
1955 this->got_
->finalize_data_size();
1956 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
1957 this->plt_
, plt_offset
);
1958 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
1959 this->got_
, got_offset
);
1963 // Emit any relocs we saved in an attempt to avoid generating COPY
1965 if (this->copy_relocs_
.any_saved_relocs())
1966 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1968 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1969 // the .got.plt section.
1970 Symbol
* sym
= this->global_offset_table_
;
1973 uint64_t data_size
= this->got_plt_
->current_data_size();
1974 symtab
->get_sized_symbol
<64>(sym
)->set_symsize(data_size
);
1978 // Perform a relocation.
1981 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1982 Target_x86_64
* target
,
1985 const elfcpp::Rela
<64, false>& rela
,
1986 unsigned int r_type
,
1987 const Sized_symbol
<64>* gsym
,
1988 const Symbol_value
<64>* psymval
,
1989 unsigned char* view
,
1990 elfcpp::Elf_types
<64>::Elf_Addr address
,
1991 section_size_type view_size
)
1993 if (this->skip_call_tls_get_addr_
)
1995 if ((r_type
!= elfcpp::R_X86_64_PLT32
1996 && r_type
!= elfcpp::R_X86_64_PC32
)
1998 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
2000 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2001 _("missing expected TLS relocation"));
2005 this->skip_call_tls_get_addr_
= false;
2010 // Pick the value to use for symbols defined in shared objects.
2011 Symbol_value
<64> symval
;
2013 && gsym
->use_plt_offset(r_type
== elfcpp::R_X86_64_PC64
2014 || r_type
== elfcpp::R_X86_64_PC32
2015 || r_type
== elfcpp::R_X86_64_PC16
2016 || r_type
== elfcpp::R_X86_64_PC8
))
2018 symval
.set_output_value(target
->plt_section()->address()
2019 + gsym
->plt_offset());
2023 const Sized_relobj
<64, false>* object
= relinfo
->object
;
2024 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2026 // Get the GOT offset if needed.
2027 // The GOT pointer points to the end of the GOT section.
2028 // We need to subtract the size of the GOT section to get
2029 // the actual offset to use in the relocation.
2030 bool have_got_offset
= false;
2031 unsigned int got_offset
= 0;
2034 case elfcpp::R_X86_64_GOT32
:
2035 case elfcpp::R_X86_64_GOT64
:
2036 case elfcpp::R_X86_64_GOTPLT64
:
2037 case elfcpp::R_X86_64_GOTPCREL
:
2038 case elfcpp::R_X86_64_GOTPCREL64
:
2041 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2042 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
2046 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2047 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2048 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
2049 - target
->got_size());
2051 have_got_offset
= true;
2060 case elfcpp::R_X86_64_NONE
:
2061 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2062 case elfcpp::R_X86_64_GNU_VTENTRY
:
2065 case elfcpp::R_X86_64_64
:
2066 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
2069 case elfcpp::R_X86_64_PC64
:
2070 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
2074 case elfcpp::R_X86_64_32
:
2075 // FIXME: we need to verify that value + addend fits into 32 bits:
2076 // uint64_t x = value + addend;
2077 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2078 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2079 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
2082 case elfcpp::R_X86_64_32S
:
2083 // FIXME: we need to verify that value + addend fits into 32 bits:
2084 // int64_t x = value + addend; // note this quantity is signed!
2085 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2086 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
2089 case elfcpp::R_X86_64_PC32
:
2090 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
2094 case elfcpp::R_X86_64_16
:
2095 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
2098 case elfcpp::R_X86_64_PC16
:
2099 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
2103 case elfcpp::R_X86_64_8
:
2104 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
2107 case elfcpp::R_X86_64_PC8
:
2108 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
2112 case elfcpp::R_X86_64_PLT32
:
2113 gold_assert(gsym
== NULL
2114 || gsym
->has_plt_offset()
2115 || gsym
->final_value_is_known()
2116 || (gsym
->is_defined()
2117 && !gsym
->is_from_dynobj()
2118 && !gsym
->is_preemptible()));
2119 // Note: while this code looks the same as for R_X86_64_PC32, it
2120 // behaves differently because psymval was set to point to
2121 // the PLT entry, rather than the symbol, in Scan::global().
2122 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
2126 case elfcpp::R_X86_64_PLTOFF64
:
2129 gold_assert(gsym
->has_plt_offset()
2130 || gsym
->final_value_is_known());
2131 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
2132 got_address
= target
->got_section(NULL
, NULL
)->address();
2133 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
2134 addend
- got_address
);
2137 case elfcpp::R_X86_64_GOT32
:
2138 gold_assert(have_got_offset
);
2139 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
2142 case elfcpp::R_X86_64_GOTPC32
:
2145 elfcpp::Elf_types
<64>::Elf_Addr value
;
2146 value
= target
->got_plt_section()->address();
2147 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2151 case elfcpp::R_X86_64_GOT64
:
2152 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2153 // Since we always add a PLT entry, this is equivalent.
2154 case elfcpp::R_X86_64_GOTPLT64
:
2155 gold_assert(have_got_offset
);
2156 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
2159 case elfcpp::R_X86_64_GOTPC64
:
2162 elfcpp::Elf_types
<64>::Elf_Addr value
;
2163 value
= target
->got_plt_section()->address();
2164 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
2168 case elfcpp::R_X86_64_GOTOFF64
:
2170 elfcpp::Elf_types
<64>::Elf_Addr value
;
2171 value
= (psymval
->value(object
, 0)
2172 - target
->got_plt_section()->address());
2173 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
2177 case elfcpp::R_X86_64_GOTPCREL
:
2179 gold_assert(have_got_offset
);
2180 elfcpp::Elf_types
<64>::Elf_Addr value
;
2181 value
= target
->got_plt_section()->address() + got_offset
;
2182 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2186 case elfcpp::R_X86_64_GOTPCREL64
:
2188 gold_assert(have_got_offset
);
2189 elfcpp::Elf_types
<64>::Elf_Addr value
;
2190 value
= target
->got_plt_section()->address() + got_offset
;
2191 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
2195 case elfcpp::R_X86_64_COPY
:
2196 case elfcpp::R_X86_64_GLOB_DAT
:
2197 case elfcpp::R_X86_64_JUMP_SLOT
:
2198 case elfcpp::R_X86_64_RELATIVE
:
2199 // These are outstanding tls relocs, which are unexpected when linking
2200 case elfcpp::R_X86_64_TPOFF64
:
2201 case elfcpp::R_X86_64_DTPMOD64
:
2202 case elfcpp::R_X86_64_TLSDESC
:
2203 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2204 _("unexpected reloc %u in object file"),
2208 // These are initial tls relocs, which are expected when linking
2209 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2210 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2211 case elfcpp::R_X86_64_TLSDESC_CALL
:
2212 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2213 case elfcpp::R_X86_64_DTPOFF32
:
2214 case elfcpp::R_X86_64_DTPOFF64
:
2215 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2216 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2217 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
2218 view
, address
, view_size
);
2221 case elfcpp::R_X86_64_SIZE32
:
2222 case elfcpp::R_X86_64_SIZE64
:
2224 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2225 _("unsupported reloc %u"),
2233 // Perform a TLS relocation.
2236 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
2237 Target_x86_64
* target
,
2239 const elfcpp::Rela
<64, false>& rela
,
2240 unsigned int r_type
,
2241 const Sized_symbol
<64>* gsym
,
2242 const Symbol_value
<64>* psymval
,
2243 unsigned char* view
,
2244 elfcpp::Elf_types
<64>::Elf_Addr address
,
2245 section_size_type view_size
)
2247 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2249 const Sized_relobj
<64, false>* object
= relinfo
->object
;
2250 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2252 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
2254 const bool is_final
= (gsym
== NULL
2255 ? !parameters
->options().shared()
2256 : gsym
->final_value_is_known());
2257 const tls::Tls_optimization optimized_type
2258 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
2261 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2262 this->saw_tls_block_reloc_
= true;
2263 if (optimized_type
== tls::TLSOPT_TO_LE
)
2265 gold_assert(tls_segment
!= NULL
);
2266 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
2267 rela
, r_type
, value
, view
,
2273 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2274 ? GOT_TYPE_TLS_OFFSET
2275 : GOT_TYPE_TLS_PAIR
);
2276 unsigned int got_offset
;
2279 gold_assert(gsym
->has_got_offset(got_type
));
2280 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2284 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2285 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2286 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2287 - target
->got_size());
2289 if (optimized_type
== tls::TLSOPT_TO_IE
)
2291 gold_assert(tls_segment
!= NULL
);
2292 value
= target
->got_plt_section()->address() + got_offset
;
2293 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2294 value
, view
, address
, view_size
);
2297 else if (optimized_type
== tls::TLSOPT_NONE
)
2299 // Relocate the field with the offset of the pair of GOT
2301 value
= target
->got_plt_section()->address() + got_offset
;
2302 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2307 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2308 _("unsupported reloc %u"), r_type
);
2311 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2312 case elfcpp::R_X86_64_TLSDESC_CALL
:
2313 this->saw_tls_block_reloc_
= true;
2314 if (optimized_type
== tls::TLSOPT_TO_LE
)
2316 gold_assert(tls_segment
!= NULL
);
2317 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2318 rela
, r_type
, value
, view
,
2324 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2325 ? GOT_TYPE_TLS_OFFSET
2326 : GOT_TYPE_TLS_DESC
);
2327 unsigned int got_offset
= 0;
2328 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
2329 && optimized_type
== tls::TLSOPT_NONE
)
2331 // We created GOT entries in the .got.tlsdesc portion of
2332 // the .got.plt section, but the offset stored in the
2333 // symbol is the offset within .got.tlsdesc.
2334 got_offset
= (target
->got_size()
2335 + target
->got_plt_section()->data_size());
2339 gold_assert(gsym
->has_got_offset(got_type
));
2340 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
2344 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2345 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2346 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
2347 - target
->got_size());
2349 if (optimized_type
== tls::TLSOPT_TO_IE
)
2351 gold_assert(tls_segment
!= NULL
);
2352 value
= target
->got_plt_section()->address() + got_offset
;
2353 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
2354 rela
, r_type
, value
, view
, address
,
2358 else if (optimized_type
== tls::TLSOPT_NONE
)
2360 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2362 // Relocate the field with the offset of the pair of GOT
2364 value
= target
->got_plt_section()->address() + got_offset
;
2365 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2371 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2372 _("unsupported reloc %u"), r_type
);
2375 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2376 this->saw_tls_block_reloc_
= true;
2377 if (optimized_type
== tls::TLSOPT_TO_LE
)
2379 gold_assert(tls_segment
!= NULL
);
2380 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2381 value
, view
, view_size
);
2384 else if (optimized_type
== tls::TLSOPT_NONE
)
2386 // Relocate the field with the offset of the GOT entry for
2387 // the module index.
2388 unsigned int got_offset
;
2389 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2390 - target
->got_size());
2391 value
= target
->got_plt_section()->address() + got_offset
;
2392 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2396 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2397 _("unsupported reloc %u"), r_type
);
2400 case elfcpp::R_X86_64_DTPOFF32
:
2401 if (optimized_type
== tls::TLSOPT_TO_LE
)
2403 // This relocation type is used in debugging information.
2404 // In that case we need to not optimize the value. If we
2405 // haven't seen a TLSLD reloc, then we assume we should not
2406 // optimize this reloc.
2407 if (this->saw_tls_block_reloc_
)
2409 gold_assert(tls_segment
!= NULL
);
2410 value
-= tls_segment
->memsz();
2413 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2416 case elfcpp::R_X86_64_DTPOFF64
:
2417 if (optimized_type
== tls::TLSOPT_TO_LE
)
2419 // See R_X86_64_DTPOFF32, just above, for why we test this.
2420 if (this->saw_tls_block_reloc_
)
2422 gold_assert(tls_segment
!= NULL
);
2423 value
-= tls_segment
->memsz();
2426 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
2429 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2430 if (optimized_type
== tls::TLSOPT_TO_LE
)
2432 gold_assert(tls_segment
!= NULL
);
2433 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2434 rela
, r_type
, value
, view
,
2438 else if (optimized_type
== tls::TLSOPT_NONE
)
2440 // Relocate the field with the offset of the GOT entry for
2441 // the tp-relative offset of the symbol.
2442 unsigned int got_offset
;
2445 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
2446 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
2447 - target
->got_size());
2451 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2452 gold_assert(object
->local_has_got_offset(r_sym
,
2453 GOT_TYPE_TLS_OFFSET
));
2454 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
2455 - target
->got_size());
2457 value
= target
->got_plt_section()->address() + got_offset
;
2458 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2461 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2462 _("unsupported reloc type %u"),
2466 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2467 value
-= tls_segment
->memsz();
2468 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2473 // Do a relocation in which we convert a TLS General-Dynamic to an
2477 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
2480 const elfcpp::Rela
<64, false>& rela
,
2482 elfcpp::Elf_types
<64>::Elf_Addr value
,
2483 unsigned char* view
,
2484 elfcpp::Elf_types
<64>::Elf_Addr address
,
2485 section_size_type view_size
)
2487 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2488 // .word 0x6666; rex64; call __tls_get_addr
2489 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2491 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2492 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2494 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2495 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2496 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2497 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2499 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2501 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2502 Relocate_functions
<64, false>::pcrela32(view
+ 8, value
, addend
- 8, address
);
2504 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2506 this->skip_call_tls_get_addr_
= true;
2509 // Do a relocation in which we convert a TLS General-Dynamic to a
2513 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
2515 Output_segment
* tls_segment
,
2516 const elfcpp::Rela
<64, false>& rela
,
2518 elfcpp::Elf_types
<64>::Elf_Addr value
,
2519 unsigned char* view
,
2520 section_size_type view_size
)
2522 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2523 // .word 0x6666; rex64; call __tls_get_addr
2524 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2526 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2527 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2529 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2530 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2531 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2532 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2534 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2536 value
-= tls_segment
->memsz();
2537 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
2539 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2541 this->skip_call_tls_get_addr_
= true;
2544 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2547 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2548 const Relocate_info
<64, false>* relinfo
,
2551 const elfcpp::Rela
<64, false>& rela
,
2552 unsigned int r_type
,
2553 elfcpp::Elf_types
<64>::Elf_Addr value
,
2554 unsigned char* view
,
2555 elfcpp::Elf_types
<64>::Elf_Addr address
,
2556 section_size_type view_size
)
2558 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2560 // leaq foo@tlsdesc(%rip), %rax
2561 // ==> movq foo@gottpoff(%rip), %rax
2562 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2563 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2564 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2565 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2567 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2568 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2572 // call *foo@tlscall(%rax)
2574 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2575 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2576 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2577 view
[0] == 0xff && view
[1] == 0x10);
2583 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2586 Target_x86_64::Relocate::tls_desc_gd_to_le(
2587 const Relocate_info
<64, false>* relinfo
,
2589 Output_segment
* tls_segment
,
2590 const elfcpp::Rela
<64, false>& rela
,
2591 unsigned int r_type
,
2592 elfcpp::Elf_types
<64>::Elf_Addr value
,
2593 unsigned char* view
,
2594 section_size_type view_size
)
2596 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2598 // leaq foo@tlsdesc(%rip), %rax
2599 // ==> movq foo@tpoff, %rax
2600 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2601 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2602 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2603 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2606 value
-= tls_segment
->memsz();
2607 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2611 // call *foo@tlscall(%rax)
2613 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2614 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2615 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2616 view
[0] == 0xff && view
[1] == 0x10);
2623 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
2626 const elfcpp::Rela
<64, false>& rela
,
2628 elfcpp::Elf_types
<64>::Elf_Addr
,
2629 unsigned char* view
,
2630 section_size_type view_size
)
2632 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2633 // ... leq foo@dtpoff(%rax),%reg
2634 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2636 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2637 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
2639 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2640 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
2642 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
2644 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2646 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2648 this->skip_call_tls_get_addr_
= true;
2651 // Do a relocation in which we convert a TLS Initial-Exec to a
2655 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
2657 Output_segment
* tls_segment
,
2658 const elfcpp::Rela
<64, false>& rela
,
2660 elfcpp::Elf_types
<64>::Elf_Addr value
,
2661 unsigned char* view
,
2662 section_size_type view_size
)
2664 // We need to examine the opcodes to figure out which instruction we
2667 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2668 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2670 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2671 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2673 unsigned char op1
= view
[-3];
2674 unsigned char op2
= view
[-2];
2675 unsigned char op3
= view
[-1];
2676 unsigned char reg
= op3
>> 3;
2684 view
[-1] = 0xc0 | reg
;
2688 // Special handling for %rsp.
2692 view
[-1] = 0xc0 | reg
;
2700 view
[-1] = 0x80 | reg
| (reg
<< 3);
2703 value
-= tls_segment
->memsz();
2704 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2707 // Relocate section data.
2710 Target_x86_64::relocate_section(
2711 const Relocate_info
<64, false>* relinfo
,
2712 unsigned int sh_type
,
2713 const unsigned char* prelocs
,
2715 Output_section
* output_section
,
2716 bool needs_special_offset_handling
,
2717 unsigned char* view
,
2718 elfcpp::Elf_types
<64>::Elf_Addr address
,
2719 section_size_type view_size
,
2720 const Reloc_symbol_changes
* reloc_symbol_changes
)
2722 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2724 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2725 Target_x86_64::Relocate
>(
2731 needs_special_offset_handling
,
2735 reloc_symbol_changes
);
2738 // Return the size of a relocation while scanning during a relocatable
2742 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2743 unsigned int r_type
,
2748 case elfcpp::R_X86_64_NONE
:
2749 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2750 case elfcpp::R_X86_64_GNU_VTENTRY
:
2751 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2752 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2753 case elfcpp::R_X86_64_TLSDESC_CALL
:
2754 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2755 case elfcpp::R_X86_64_DTPOFF32
:
2756 case elfcpp::R_X86_64_DTPOFF64
:
2757 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2758 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2761 case elfcpp::R_X86_64_64
:
2762 case elfcpp::R_X86_64_PC64
:
2763 case elfcpp::R_X86_64_GOTOFF64
:
2764 case elfcpp::R_X86_64_GOTPC64
:
2765 case elfcpp::R_X86_64_PLTOFF64
:
2766 case elfcpp::R_X86_64_GOT64
:
2767 case elfcpp::R_X86_64_GOTPCREL64
:
2768 case elfcpp::R_X86_64_GOTPCREL
:
2769 case elfcpp::R_X86_64_GOTPLT64
:
2772 case elfcpp::R_X86_64_32
:
2773 case elfcpp::R_X86_64_32S
:
2774 case elfcpp::R_X86_64_PC32
:
2775 case elfcpp::R_X86_64_PLT32
:
2776 case elfcpp::R_X86_64_GOTPC32
:
2777 case elfcpp::R_X86_64_GOT32
:
2780 case elfcpp::R_X86_64_16
:
2781 case elfcpp::R_X86_64_PC16
:
2784 case elfcpp::R_X86_64_8
:
2785 case elfcpp::R_X86_64_PC8
:
2788 case elfcpp::R_X86_64_COPY
:
2789 case elfcpp::R_X86_64_GLOB_DAT
:
2790 case elfcpp::R_X86_64_JUMP_SLOT
:
2791 case elfcpp::R_X86_64_RELATIVE
:
2792 // These are outstanding tls relocs, which are unexpected when linking
2793 case elfcpp::R_X86_64_TPOFF64
:
2794 case elfcpp::R_X86_64_DTPMOD64
:
2795 case elfcpp::R_X86_64_TLSDESC
:
2796 object
->error(_("unexpected reloc %u in object file"), r_type
);
2799 case elfcpp::R_X86_64_SIZE32
:
2800 case elfcpp::R_X86_64_SIZE64
:
2802 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
2807 // Scan the relocs during a relocatable link.
2810 Target_x86_64::scan_relocatable_relocs(Symbol_table
* symtab
,
2812 Sized_relobj
<64, false>* object
,
2813 unsigned int data_shndx
,
2814 unsigned int sh_type
,
2815 const unsigned char* prelocs
,
2817 Output_section
* output_section
,
2818 bool needs_special_offset_handling
,
2819 size_t local_symbol_count
,
2820 const unsigned char* plocal_symbols
,
2821 Relocatable_relocs
* rr
)
2823 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2825 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
2826 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2828 gold::scan_relocatable_relocs
<64, false, elfcpp::SHT_RELA
,
2829 Scan_relocatable_relocs
>(
2837 needs_special_offset_handling
,
2843 // Relocate a section during a relocatable link.
2846 Target_x86_64::relocate_for_relocatable(
2847 const Relocate_info
<64, false>* relinfo
,
2848 unsigned int sh_type
,
2849 const unsigned char* prelocs
,
2851 Output_section
* output_section
,
2852 off_t offset_in_output_section
,
2853 const Relocatable_relocs
* rr
,
2854 unsigned char* view
,
2855 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
2856 section_size_type view_size
,
2857 unsigned char* reloc_view
,
2858 section_size_type reloc_view_size
)
2860 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2862 gold::relocate_for_relocatable
<64, false, elfcpp::SHT_RELA
>(
2867 offset_in_output_section
,
2876 // Return the value to use for a dynamic which requires special
2877 // treatment. This is how we support equality comparisons of function
2878 // pointers across shared library boundaries, as described in the
2879 // processor specific ABI supplement.
2882 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
2884 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2885 return this->plt_section()->address() + gsym
->plt_offset();
2888 // Return a string used to fill a code section with nops to take up
2889 // the specified length.
2892 Target_x86_64::do_code_fill(section_size_type length
) const
2896 // Build a jmpq instruction to skip over the bytes.
2897 unsigned char jmp
[5];
2899 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2900 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2901 + std::string(length
- 5, '\0'));
2904 // Nop sequences of various lengths.
2905 const char nop1
[1] = { 0x90 }; // nop
2906 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2907 const char nop3
[3] = { 0x0f, 0x1f, 0x00 }; // nop (%rax)
2908 const char nop4
[4] = { 0x0f, 0x1f, 0x40, 0x00}; // nop 0(%rax)
2909 const char nop5
[5] = { 0x0f, 0x1f, 0x44, 0x00, // nop 0(%rax,%rax,1)
2911 const char nop6
[6] = { 0x66, 0x0f, 0x1f, 0x44, // nopw 0(%rax,%rax,1)
2913 const char nop7
[7] = { 0x0f, 0x1f, 0x80, 0x00, // nopl 0L(%rax)
2915 const char nop8
[8] = { 0x0f, 0x1f, 0x84, 0x00, // nopl 0L(%rax,%rax,1)
2916 0x00, 0x00, 0x00, 0x00 };
2917 const char nop9
[9] = { 0x66, 0x0f, 0x1f, 0x84, // nopw 0L(%rax,%rax,1)
2918 0x00, 0x00, 0x00, 0x00,
2920 const char nop10
[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
2921 0x84, 0x00, 0x00, 0x00,
2923 const char nop11
[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
2924 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2926 const char nop12
[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
2927 0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2928 0x00, 0x00, 0x00, 0x00 };
2929 const char nop13
[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2930 0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
2931 0x00, 0x00, 0x00, 0x00,
2933 const char nop14
[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2934 0x66, 0x2e, 0x0f, 0x1f, // data16
2935 0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2937 const char nop15
[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2938 0x66, 0x66, 0x2e, 0x0f, // data16; data16
2939 0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2942 const char* nops
[16] = {
2944 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2945 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2948 return std::string(nops
[length
], length
);
2951 // Return the addend to use for a target specific relocation. The
2952 // only target specific relocation is R_X86_64_TLSDESC for a local
2953 // symbol. We want to set the addend is the offset of the local
2954 // symbol in the TLS segment.
2957 Target_x86_64::do_reloc_addend(void* arg
, unsigned int r_type
,
2960 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
2961 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
2962 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
2963 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
2964 const Symbol_value
<64>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
2965 gold_assert(psymval
->is_tls_symbol());
2966 // The value of a TLS symbol is the offset in the TLS segment.
2967 return psymval
->value(ti
.object
, 0);
2970 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2971 // compiled with -fstack-split. The function calls non-stack-split
2972 // code. We have to change the function so that it always ensures
2973 // that it has enough stack space to run some random function.
2976 Target_x86_64::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
2977 section_offset_type fnoffset
,
2978 section_size_type fnsize
,
2979 unsigned char* view
,
2980 section_size_type view_size
,
2982 std::string
* to
) const
2984 // The function starts with a comparison of the stack pointer and a
2985 // field in the TCB. This is followed by a jump.
2988 if (this->match_view(view
, view_size
, fnoffset
, "\x64\x48\x3b\x24\x25", 5)
2991 // We will call __morestack if the carry flag is set after this
2992 // comparison. We turn the comparison into an stc instruction
2994 view
[fnoffset
] = '\xf9';
2995 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 8);
2997 // lea NN(%rsp),%r10
2998 // lea NN(%rsp),%r11
2999 else if ((this->match_view(view
, view_size
, fnoffset
,
3000 "\x4c\x8d\x94\x24", 4)
3001 || this->match_view(view
, view_size
, fnoffset
,
3002 "\x4c\x8d\x9c\x24", 4))
3005 // This is loading an offset from the stack pointer for a
3006 // comparison. The offset is negative, so we decrease the
3007 // offset by the amount of space we need for the stack. This
3008 // means we will avoid calling __morestack if there happens to
3009 // be plenty of space on the stack already.
3010 unsigned char* pval
= view
+ fnoffset
+ 4;
3011 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
3012 val
-= parameters
->options().split_stack_adjust_size();
3013 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
3017 if (!object
->has_no_split_stack())
3018 object
->error(_("failed to match split-stack sequence at "
3019 "section %u offset %0zx"),
3020 shndx
, static_cast<size_t>(fnoffset
));
3024 // We have to change the function so that it calls
3025 // __morestack_non_split instead of __morestack. The former will
3026 // allocate additional stack space.
3027 *from
= "__morestack";
3028 *to
= "__morestack_non_split";
3031 // The selector for x86_64 object files.
3033 class Target_selector_x86_64
: public Target_selector_freebsd
3036 Target_selector_x86_64()
3037 : Target_selector_freebsd(elfcpp::EM_X86_64
, 64, false, "elf64-x86-64",
3038 "elf64-x86-64-freebsd")
3042 do_instantiate_target()
3043 { return new Target_x86_64(); }
3047 Target_selector_x86_64 target_selector_x86_64
;
3049 } // End anonymous namespace.