1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
51 // A class to handle the PLT data.
54 class Output_data_plt_x86_64
: public Output_section_data
57 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
59 Output_data_plt_x86_64(Layout
* layout
, Output_data_got
<64, false>* got
,
60 Output_data_space
* got_plt
,
61 Output_data_space
* got_irelative
)
62 : Output_section_data(16), layout_(layout
), tlsdesc_rel_(NULL
),
63 irelative_rel_(NULL
), got_(got
), got_plt_(got_plt
),
64 got_irelative_(got_irelative
), count_(0), irelative_count_(0),
65 tlsdesc_got_offset_(-1U), free_list_()
66 { this->init(layout
); }
68 Output_data_plt_x86_64(Layout
* layout
, Output_data_got
<64, false>* got
,
69 Output_data_space
* got_plt
,
70 Output_data_space
* got_irelative
,
71 unsigned int plt_count
)
72 : Output_section_data((plt_count
+ 1) * plt_entry_size
, 16, false),
73 layout_(layout
), tlsdesc_rel_(NULL
), irelative_rel_(NULL
), got_(got
),
74 got_plt_(got_plt
), got_irelative_(got_irelative
), count_(plt_count
),
75 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
79 // Initialize the free list and reserve the first entry.
80 this->free_list_
.init((plt_count
+ 1) * plt_entry_size
, false);
81 this->free_list_
.remove(0, plt_entry_size
);
84 // Initialize the PLT section.
88 // Add an entry to the PLT.
90 add_entry(Symbol_table
*, Layout
*, Symbol
* gsym
);
92 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
94 add_local_ifunc_entry(Symbol_table
* symtab
, Layout
*,
95 Sized_relobj_file
<size
, false>* relobj
,
96 unsigned int local_sym_index
);
98 // Add the relocation for a PLT entry.
100 add_relocation(Symbol_table
*, Layout
*, Symbol
* gsym
,
101 unsigned int got_offset
);
103 // Add the reserved TLSDESC_PLT entry to the PLT.
105 reserve_tlsdesc_entry(unsigned int got_offset
)
106 { this->tlsdesc_got_offset_
= got_offset
; }
108 // Return true if a TLSDESC_PLT entry has been reserved.
110 has_tlsdesc_entry() const
111 { return this->tlsdesc_got_offset_
!= -1U; }
113 // Return the GOT offset for the reserved TLSDESC_PLT entry.
115 get_tlsdesc_got_offset() const
116 { return this->tlsdesc_got_offset_
; }
118 // Return the offset of the reserved TLSDESC_PLT entry.
120 get_tlsdesc_plt_offset() const
121 { return (this->count_
+ this->irelative_count_
+ 1) * plt_entry_size
; }
123 // Return the .rela.plt section data.
126 { return this->rel_
; }
128 // Return where the TLSDESC relocations should go.
130 rela_tlsdesc(Layout
*);
132 // Return where the IRELATIVE relocations should go in the PLT
135 rela_irelative(Symbol_table
*, Layout
*);
137 // Return whether we created a section for IRELATIVE relocations.
139 has_irelative_section() const
140 { return this->irelative_rel_
!= NULL
; }
142 // Return the number of PLT entries.
145 { return this->count_
+ this->irelative_count_
; }
147 // Return the offset of the first non-reserved PLT entry.
149 first_plt_entry_offset()
150 { return plt_entry_size
; }
152 // Return the size of a PLT entry.
155 { return plt_entry_size
; }
157 // Reserve a slot in the PLT for an existing symbol in an incremental update.
159 reserve_slot(unsigned int plt_index
)
161 this->free_list_
.remove((plt_index
+ 1) * plt_entry_size
,
162 (plt_index
+ 2) * plt_entry_size
);
165 // Return the PLT address to use for a global symbol.
167 address_for_global(const Symbol
*);
169 // Return the PLT address to use for a local symbol.
171 address_for_local(const Relobj
*, unsigned int symndx
);
175 do_adjust_output_section(Output_section
* os
);
177 // Write to a map file.
179 do_print_to_mapfile(Mapfile
* mapfile
) const
180 { mapfile
->print_output_data(this, _("** PLT")); }
183 // The size of an entry in the PLT.
184 static const int plt_entry_size
= 16;
186 // The first entry in the PLT.
187 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
188 // procedure linkage table for both programs and shared objects."
189 static const unsigned char first_plt_entry
[plt_entry_size
];
191 // Other entries in the PLT for an executable.
192 static const unsigned char plt_entry
[plt_entry_size
];
194 // The reserved TLSDESC entry in the PLT for an executable.
195 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
197 // The .eh_frame unwind information for the PLT.
198 static const int plt_eh_frame_cie_size
= 16;
199 static const int plt_eh_frame_fde_size
= 32;
200 static const unsigned char plt_eh_frame_cie
[plt_eh_frame_cie_size
];
201 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
203 // Set the final size.
205 set_final_data_size();
207 // Write out the PLT data.
209 do_write(Output_file
*);
211 // A pointer to the Layout class, so that we can find the .dynamic
212 // section when we write out the GOT PLT section.
214 // The reloc section.
216 // The TLSDESC relocs, if necessary. These must follow the regular
218 Reloc_section
* tlsdesc_rel_
;
219 // The IRELATIVE relocs, if necessary. These must follow the
220 // regular PLT relocations and the TLSDESC relocations.
221 Reloc_section
* irelative_rel_
;
223 Output_data_got
<64, false>* got_
;
224 // The .got.plt section.
225 Output_data_space
* got_plt_
;
226 // The part of the .got.plt section used for IRELATIVE relocs.
227 Output_data_space
* got_irelative_
;
228 // The number of PLT entries.
230 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
231 // follow the regular PLT entries.
232 unsigned int irelative_count_
;
233 // Offset of the reserved TLSDESC_GOT entry when needed.
234 unsigned int tlsdesc_got_offset_
;
235 // List of available regions within the section, for incremental
237 Free_list free_list_
;
240 // The x86_64 target class.
242 // http://www.x86-64.org/documentation/abi.pdf
243 // TLS info comes from
244 // http://people.redhat.com/drepper/tls.pdf
245 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
248 class Target_x86_64
: public Sized_target
<size
, false>
251 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
252 // uses only Elf64_Rela relocation entries with explicit addends."
253 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
256 : Sized_target
<size
, false>(&x86_64_info
),
257 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_irelative_(NULL
),
258 got_tlsdesc_(NULL
), global_offset_table_(NULL
), rela_dyn_(NULL
),
259 rela_irelative_(NULL
), copy_relocs_(elfcpp::R_X86_64_COPY
),
260 dynbss_(NULL
), got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
261 tls_base_symbol_defined_(false)
264 // Hook for a new output section.
266 do_new_output_section(Output_section
*) const;
268 // Scan the relocations to look for symbol adjustments.
270 gc_process_relocs(Symbol_table
* symtab
,
272 Sized_relobj_file
<size
, false>* object
,
273 unsigned int data_shndx
,
274 unsigned int sh_type
,
275 const unsigned char* prelocs
,
277 Output_section
* output_section
,
278 bool needs_special_offset_handling
,
279 size_t local_symbol_count
,
280 const unsigned char* plocal_symbols
);
282 // Scan the relocations to look for symbol adjustments.
284 scan_relocs(Symbol_table
* symtab
,
286 Sized_relobj_file
<size
, false>* object
,
287 unsigned int data_shndx
,
288 unsigned int sh_type
,
289 const unsigned char* prelocs
,
291 Output_section
* output_section
,
292 bool needs_special_offset_handling
,
293 size_t local_symbol_count
,
294 const unsigned char* plocal_symbols
);
296 // Finalize the sections.
298 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
300 // Return the value to use for a dynamic which requires special
303 do_dynsym_value(const Symbol
*) const;
305 // Relocate a section.
307 relocate_section(const Relocate_info
<size
, false>*,
308 unsigned int sh_type
,
309 const unsigned char* prelocs
,
311 Output_section
* output_section
,
312 bool needs_special_offset_handling
,
314 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
315 section_size_type view_size
,
316 const Reloc_symbol_changes
*);
318 // Scan the relocs during a relocatable link.
320 scan_relocatable_relocs(Symbol_table
* symtab
,
322 Sized_relobj_file
<size
, false>* object
,
323 unsigned int data_shndx
,
324 unsigned int sh_type
,
325 const unsigned char* prelocs
,
327 Output_section
* output_section
,
328 bool needs_special_offset_handling
,
329 size_t local_symbol_count
,
330 const unsigned char* plocal_symbols
,
331 Relocatable_relocs
*);
333 // Relocate a section during a relocatable link.
335 relocate_for_relocatable(
336 const Relocate_info
<size
, false>*,
337 unsigned int sh_type
,
338 const unsigned char* prelocs
,
340 Output_section
* output_section
,
341 off_t offset_in_output_section
,
342 const Relocatable_relocs
*,
344 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
345 section_size_type view_size
,
346 unsigned char* reloc_view
,
347 section_size_type reloc_view_size
);
349 // Return a string used to fill a code section with nops.
351 do_code_fill(section_size_type length
) const;
353 // Return whether SYM is defined by the ABI.
355 do_is_defined_by_abi(const Symbol
* sym
) const
356 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
358 // Return the symbol index to use for a target specific relocation.
359 // The only target specific relocation is R_X86_64_TLSDESC for a
360 // local symbol, which is an absolute reloc.
362 do_reloc_symbol_index(void*, unsigned int r_type
) const
364 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
368 // Return the addend to use for a target specific relocation.
370 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
372 // Return the PLT section.
374 do_plt_address_for_global(const Symbol
* gsym
) const
375 { return this->plt_section()->address_for_global(gsym
); }
378 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
379 { return this->plt_section()->address_for_local(relobj
, symndx
); }
381 // This function should be defined in targets that can use relocation
382 // types to determine (implemented in local_reloc_may_be_function_pointer
383 // and global_reloc_may_be_function_pointer)
384 // if a function's pointer is taken. ICF uses this in safe mode to only
385 // fold those functions whose pointer is defintely not taken. For x86_64
386 // pie binaries, safe ICF cannot be done by looking at relocation types.
388 do_can_check_for_function_pointers() const
389 { return !parameters
->options().pie(); }
391 // Return the base for a DW_EH_PE_datarel encoding.
393 do_ehframe_datarel_base() const;
395 // Adjust -fsplit-stack code which calls non-split-stack code.
397 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
398 section_offset_type fnoffset
, section_size_type fnsize
,
399 unsigned char* view
, section_size_type view_size
,
400 std::string
* from
, std::string
* to
) const;
402 // Return the size of the GOT section.
406 gold_assert(this->got_
!= NULL
);
407 return this->got_
->data_size();
410 // Return the number of entries in the GOT.
412 got_entry_count() const
414 if (this->got_
== NULL
)
416 return this->got_size() / 8;
419 // Return the number of entries in the PLT.
421 plt_entry_count() const;
423 // Return the offset of the first non-reserved PLT entry.
425 first_plt_entry_offset() const;
427 // Return the size of each PLT entry.
429 plt_entry_size() const;
431 // Create the GOT section for an incremental update.
432 Output_data_got_base
*
433 init_got_plt_for_update(Symbol_table
* symtab
,
435 unsigned int got_count
,
436 unsigned int plt_count
);
438 // Reserve a GOT entry for a local symbol, and regenerate any
439 // necessary dynamic relocations.
441 reserve_local_got_entry(unsigned int got_index
,
442 Sized_relobj
<size
, false>* obj
,
444 unsigned int got_type
);
446 // Reserve a GOT entry for a global symbol, and regenerate any
447 // necessary dynamic relocations.
449 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
450 unsigned int got_type
);
452 // Register an existing PLT entry for a global symbol.
454 register_global_plt_entry(Symbol_table
*, Layout
*, unsigned int plt_index
,
457 // Force a COPY relocation for a given symbol.
459 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
461 // Apply an incremental relocation.
463 apply_relocation(const Relocate_info
<size
, false>* relinfo
,
464 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
466 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
469 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
470 section_size_type view_size
);
472 // Add a new reloc argument, returning the index in the vector.
474 add_tlsdesc_info(Sized_relobj_file
<size
, false>* object
, unsigned int r_sym
)
476 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
477 return this->tlsdesc_reloc_info_
.size() - 1;
481 // The class which scans relocations.
486 : issued_non_pic_error_(false)
490 get_reference_flags(unsigned int r_type
);
493 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
494 Sized_relobj_file
<size
, false>* object
,
495 unsigned int data_shndx
,
496 Output_section
* output_section
,
497 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
498 const elfcpp::Sym
<size
, false>& lsym
);
501 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
502 Sized_relobj_file
<size
, false>* object
,
503 unsigned int data_shndx
,
504 Output_section
* output_section
,
505 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
509 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
510 Target_x86_64
* target
,
511 Sized_relobj_file
<size
, false>* object
,
512 unsigned int data_shndx
,
513 Output_section
* output_section
,
514 const elfcpp::Rela
<size
, false>& reloc
,
516 const elfcpp::Sym
<size
, false>& lsym
);
519 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
520 Target_x86_64
* target
,
521 Sized_relobj_file
<size
, false>* object
,
522 unsigned int data_shndx
,
523 Output_section
* output_section
,
524 const elfcpp::Rela
<size
, false>& reloc
,
530 unsupported_reloc_local(Sized_relobj_file
<size
, false>*,
531 unsigned int r_type
);
534 unsupported_reloc_global(Sized_relobj_file
<size
, false>*,
535 unsigned int r_type
, Symbol
*);
538 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
541 possible_function_pointer_reloc(unsigned int r_type
);
544 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, false>*,
545 unsigned int r_type
);
547 // Whether we have issued an error about a non-PIC compilation.
548 bool issued_non_pic_error_
;
551 // The class which implements relocation.
556 : skip_call_tls_get_addr_(false)
561 if (this->skip_call_tls_get_addr_
)
563 // FIXME: This needs to specify the location somehow.
564 gold_error(_("missing expected TLS relocation"));
568 // Do a relocation. Return false if the caller should not issue
569 // any warnings about this relocation.
571 relocate(const Relocate_info
<size
, false>*, Target_x86_64
*,
573 size_t relnum
, const elfcpp::Rela
<size
, false>&,
574 unsigned int r_type
, const Sized_symbol
<size
>*,
575 const Symbol_value
<size
>*,
576 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
580 // Do a TLS relocation.
582 relocate_tls(const Relocate_info
<size
, false>*, Target_x86_64
*,
583 size_t relnum
, const elfcpp::Rela
<size
, false>&,
584 unsigned int r_type
, const Sized_symbol
<size
>*,
585 const Symbol_value
<size
>*,
586 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
589 // Do a TLS General-Dynamic to Initial-Exec transition.
591 tls_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
592 Output_segment
* tls_segment
,
593 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
594 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
596 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
597 section_size_type view_size
);
599 // Do a TLS General-Dynamic to Local-Exec transition.
601 tls_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
602 Output_segment
* tls_segment
,
603 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
604 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
606 section_size_type view_size
);
608 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
610 tls_desc_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
611 Output_segment
* tls_segment
,
612 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
613 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
615 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
616 section_size_type view_size
);
618 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
620 tls_desc_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
621 Output_segment
* tls_segment
,
622 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
623 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
625 section_size_type view_size
);
627 // Do a TLS Local-Dynamic to Local-Exec transition.
629 tls_ld_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
630 Output_segment
* tls_segment
,
631 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
632 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
634 section_size_type view_size
);
636 // Do a TLS Initial-Exec to Local-Exec transition.
638 tls_ie_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
639 Output_segment
* tls_segment
,
640 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
641 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
643 section_size_type view_size
);
645 // This is set if we should skip the next reloc, which should be a
646 // PLT32 reloc against ___tls_get_addr.
647 bool skip_call_tls_get_addr_
;
650 // A class which returns the size required for a relocation type,
651 // used while scanning relocs during a relocatable link.
652 class Relocatable_size_for_reloc
656 get_size_for_reloc(unsigned int, Relobj
*);
659 // Adjust TLS relocation type based on the options and whether this
660 // is a local symbol.
661 static tls::Tls_optimization
662 optimize_tls_reloc(bool is_final
, int r_type
);
664 // Get the GOT section, creating it if necessary.
665 Output_data_got
<64, false>*
666 got_section(Symbol_table
*, Layout
*);
668 // Get the GOT PLT section.
670 got_plt_section() const
672 gold_assert(this->got_plt_
!= NULL
);
673 return this->got_plt_
;
676 // Get the GOT section for TLSDESC entries.
677 Output_data_got
<64, false>*
678 got_tlsdesc_section() const
680 gold_assert(this->got_tlsdesc_
!= NULL
);
681 return this->got_tlsdesc_
;
684 // Create the PLT section.
686 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
688 // Create a PLT entry for a global symbol.
690 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
692 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
694 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
695 Sized_relobj_file
<size
, false>* relobj
,
696 unsigned int local_sym_index
);
698 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
700 define_tls_base_symbol(Symbol_table
*, Layout
*);
702 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
704 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
706 // Create a GOT entry for the TLS module index.
708 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
709 Sized_relobj_file
<size
, false>* object
);
711 // Get the PLT section.
712 Output_data_plt_x86_64
<size
>*
715 gold_assert(this->plt_
!= NULL
);
719 // Get the dynamic reloc section, creating it if necessary.
721 rela_dyn_section(Layout
*);
723 // Get the section to use for TLSDESC relocations.
725 rela_tlsdesc_section(Layout
*) const;
727 // Get the section to use for IRELATIVE relocations.
729 rela_irelative_section(Layout
*);
731 // Add a potential copy relocation.
733 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
734 Sized_relobj_file
<size
, false>* object
,
735 unsigned int shndx
, Output_section
* output_section
,
736 Symbol
* sym
, const elfcpp::Rela
<size
, false>& reloc
)
738 this->copy_relocs_
.copy_reloc(symtab
, layout
,
739 symtab
->get_sized_symbol
<size
>(sym
),
740 object
, shndx
, output_section
,
741 reloc
, this->rela_dyn_section(layout
));
744 // Information about this specific target which we pass to the
745 // general Target structure.
746 static const Target::Target_info x86_64_info
;
748 // The types of GOT entries needed for this platform.
749 // These values are exposed to the ABI in an incremental link.
750 // Do not renumber existing values without changing the version
751 // number of the .gnu_incremental_inputs section.
754 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
755 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
756 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
757 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
760 // This type is used as the argument to the target specific
761 // relocation routines. The only target specific reloc is
762 // R_X86_64_TLSDESC against a local symbol.
765 Tlsdesc_info(Sized_relobj_file
<size
, false>* a_object
, unsigned int a_r_sym
)
766 : object(a_object
), r_sym(a_r_sym
)
769 // The object in which the local symbol is defined.
770 Sized_relobj_file
<size
, false>* object
;
771 // The local symbol index in the object.
776 Output_data_got
<64, false>* got_
;
778 Output_data_plt_x86_64
<size
>* plt_
;
779 // The GOT PLT section.
780 Output_data_space
* got_plt_
;
781 // The GOT section for IRELATIVE relocations.
782 Output_data_space
* got_irelative_
;
783 // The GOT section for TLSDESC relocations.
784 Output_data_got
<64, false>* got_tlsdesc_
;
785 // The _GLOBAL_OFFSET_TABLE_ symbol.
786 Symbol
* global_offset_table_
;
787 // The dynamic reloc section.
788 Reloc_section
* rela_dyn_
;
789 // The section to use for IRELATIVE relocs.
790 Reloc_section
* rela_irelative_
;
791 // Relocs saved to avoid a COPY reloc.
792 Copy_relocs
<elfcpp::SHT_RELA
, size
, false> copy_relocs_
;
793 // Space for variables copied with a COPY reloc.
794 Output_data_space
* dynbss_
;
795 // Offset of the GOT entry for the TLS module index.
796 unsigned int got_mod_index_offset_
;
797 // We handle R_X86_64_TLSDESC against a local symbol as a target
798 // specific relocation. Here we store the object and local symbol
799 // index for the relocation.
800 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
801 // True if the _TLS_MODULE_BASE_ symbol has been defined.
802 bool tls_base_symbol_defined_
;
806 const Target::Target_info Target_x86_64
<64>::x86_64_info
=
809 false, // is_big_endian
810 elfcpp::EM_X86_64
, // machine_code
811 false, // has_make_symbol
812 false, // has_resolve
813 true, // has_code_fill
814 true, // is_default_stack_executable
815 true, // can_icf_inline_merge_sections
817 "/lib/ld64.so.1", // program interpreter
818 0x400000, // default_text_segment_address
819 0x1000, // abi_pagesize (overridable by -z max-page-size)
820 0x1000, // common_pagesize (overridable by -z common-page-size)
821 elfcpp::SHN_UNDEF
, // small_common_shndx
822 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
823 0, // small_common_section_flags
824 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
825 NULL
, // attributes_section
826 NULL
// attributes_vendor
830 const Target::Target_info Target_x86_64
<32>::x86_64_info
=
833 false, // is_big_endian
834 elfcpp::EM_X86_64
, // machine_code
835 false, // has_make_symbol
836 false, // has_resolve
837 true, // has_code_fill
838 true, // is_default_stack_executable
839 true, // can_icf_inline_merge_sections
841 "/libx32/ldx32.so.1", // program interpreter
842 0x400000, // default_text_segment_address
843 0x1000, // abi_pagesize (overridable by -z max-page-size)
844 0x1000, // common_pagesize (overridable by -z common-page-size)
845 elfcpp::SHN_UNDEF
, // small_common_shndx
846 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
847 0, // small_common_section_flags
848 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
849 NULL
, // attributes_section
850 NULL
// attributes_vendor
853 // This is called when a new output section is created. This is where
854 // we handle the SHF_X86_64_LARGE.
858 Target_x86_64
<size
>::do_new_output_section(Output_section
* os
) const
860 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
861 os
->set_is_large_section();
864 // Get the GOT section, creating it if necessary.
867 Output_data_got
<64, false>*
868 Target_x86_64
<size
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
870 if (this->got_
== NULL
)
872 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
874 // When using -z now, we can treat .got.plt as a relro section.
875 // Without -z now, it is modified after program startup by lazy
877 bool is_got_plt_relro
= parameters
->options().now();
878 Output_section_order got_order
= (is_got_plt_relro
881 Output_section_order got_plt_order
= (is_got_plt_relro
883 : ORDER_NON_RELRO_FIRST
);
885 this->got_
= new Output_data_got
<64, false>();
887 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
889 | elfcpp::SHF_WRITE
),
890 this->got_
, got_order
, true);
892 this->got_plt_
= new Output_data_space(8, "** GOT PLT");
893 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
895 | elfcpp::SHF_WRITE
),
896 this->got_plt_
, got_plt_order
,
899 // The first three entries are reserved.
900 this->got_plt_
->set_current_data_size(3 * 8);
902 if (!is_got_plt_relro
)
904 // Those bytes can go into the relro segment.
905 layout
->increase_relro(3 * 8);
908 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
909 this->global_offset_table_
=
910 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
911 Symbol_table::PREDEFINED
,
913 0, 0, elfcpp::STT_OBJECT
,
915 elfcpp::STV_HIDDEN
, 0,
918 // If there are any IRELATIVE relocations, they get GOT entries
919 // in .got.plt after the jump slot entries.
920 this->got_irelative_
= new Output_data_space(8, "** GOT IRELATIVE PLT");
921 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
923 | elfcpp::SHF_WRITE
),
924 this->got_irelative_
,
925 got_plt_order
, is_got_plt_relro
);
927 // If there are any TLSDESC relocations, they get GOT entries in
928 // .got.plt after the jump slot and IRELATIVE entries.
929 this->got_tlsdesc_
= new Output_data_got
<64, false>();
930 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
932 | elfcpp::SHF_WRITE
),
934 got_plt_order
, is_got_plt_relro
);
940 // Get the dynamic reloc section, creating it if necessary.
943 typename Target_x86_64
<size
>::Reloc_section
*
944 Target_x86_64
<size
>::rela_dyn_section(Layout
* layout
)
946 if (this->rela_dyn_
== NULL
)
948 gold_assert(layout
!= NULL
);
949 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
950 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
951 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
952 ORDER_DYNAMIC_RELOCS
, false);
954 return this->rela_dyn_
;
957 // Get the section to use for IRELATIVE relocs, creating it if
958 // necessary. These go in .rela.dyn, but only after all other dynamic
959 // relocations. They need to follow the other dynamic relocations so
960 // that they can refer to global variables initialized by those
964 typename Target_x86_64
<size
>::Reloc_section
*
965 Target_x86_64
<size
>::rela_irelative_section(Layout
* layout
)
967 if (this->rela_irelative_
== NULL
)
969 // Make sure we have already created the dynamic reloc section.
970 this->rela_dyn_section(layout
);
971 this->rela_irelative_
= new Reloc_section(false);
972 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
973 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
974 ORDER_DYNAMIC_RELOCS
, false);
975 gold_assert(this->rela_dyn_
->output_section()
976 == this->rela_irelative_
->output_section());
978 return this->rela_irelative_
;
981 // Initialize the PLT section.
985 Output_data_plt_x86_64
<size
>::init(Layout
* layout
)
987 this->rel_
= new Reloc_section(false);
988 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
989 elfcpp::SHF_ALLOC
, this->rel_
,
990 ORDER_DYNAMIC_PLT_RELOCS
, false);
992 // Add unwind information if requested.
993 if (parameters
->options().ld_generated_unwind_info())
994 layout
->add_eh_frame_for_plt(this, plt_eh_frame_cie
, plt_eh_frame_cie_size
,
995 plt_eh_frame_fde
, plt_eh_frame_fde_size
);
1000 Output_data_plt_x86_64
<size
>::do_adjust_output_section(Output_section
* os
)
1002 os
->set_entsize(plt_entry_size
);
1005 // Add an entry to the PLT.
1009 Output_data_plt_x86_64
<size
>::add_entry(Symbol_table
* symtab
, Layout
* layout
,
1012 gold_assert(!gsym
->has_plt_offset());
1014 unsigned int plt_index
;
1016 section_offset_type got_offset
;
1018 unsigned int* pcount
;
1019 unsigned int offset
;
1020 unsigned int reserved
;
1021 Output_data_space
* got
;
1022 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1023 && gsym
->can_use_relative_reloc(false))
1025 pcount
= &this->irelative_count_
;
1028 got
= this->got_irelative_
;
1032 pcount
= &this->count_
;
1035 got
= this->got_plt_
;
1038 if (!this->is_data_size_valid())
1040 // Note that when setting the PLT offset for a non-IRELATIVE
1041 // entry we skip the initial reserved PLT entry.
1042 plt_index
= *pcount
+ offset
;
1043 plt_offset
= plt_index
* plt_entry_size
;
1047 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1048 gold_assert(got_offset
== got
->current_data_size());
1050 // Every PLT entry needs a GOT entry which points back to the PLT
1051 // entry (this will be changed by the dynamic linker, normally
1052 // lazily when the function is called).
1053 got
->set_current_data_size(got_offset
+ 8);
1057 // FIXME: This is probably not correct for IRELATIVE relocs.
1059 // For incremental updates, find an available slot.
1060 plt_offset
= this->free_list_
.allocate(plt_entry_size
, plt_entry_size
, 0);
1061 if (plt_offset
== -1)
1062 gold_fallback(_("out of patch space (PLT);"
1063 " relink with --incremental-full"));
1065 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1066 // can be calculated from the PLT index, adjusting for the three
1067 // reserved entries at the beginning of the GOT.
1068 plt_index
= plt_offset
/ plt_entry_size
- 1;
1069 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1072 gsym
->set_plt_offset(plt_offset
);
1074 // Every PLT entry needs a reloc.
1075 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
1077 // Note that we don't need to save the symbol. The contents of the
1078 // PLT are independent of which symbols are used. The symbols only
1079 // appear in the relocations.
1082 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1087 Output_data_plt_x86_64
<size
>::add_local_ifunc_entry(
1088 Symbol_table
* symtab
,
1090 Sized_relobj_file
<size
, false>* relobj
,
1091 unsigned int local_sym_index
)
1093 unsigned int plt_offset
= this->irelative_count_
* plt_entry_size
;
1094 ++this->irelative_count_
;
1096 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
1098 // Every PLT entry needs a GOT entry which points back to the PLT
1100 this->got_irelative_
->set_current_data_size(got_offset
+ 8);
1102 // Every PLT entry needs a reloc.
1103 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1104 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
1105 elfcpp::R_X86_64_IRELATIVE
,
1106 this->got_irelative_
, got_offset
, 0);
1111 // Add the relocation for a PLT entry.
1115 Output_data_plt_x86_64
<size
>::add_relocation(Symbol_table
* symtab
,
1118 unsigned int got_offset
)
1120 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1121 && gsym
->can_use_relative_reloc(false))
1123 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1124 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
1125 this->got_irelative_
, got_offset
, 0);
1129 gsym
->set_needs_dynsym_entry();
1130 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
1135 // Return where the TLSDESC relocations should go, creating it if
1136 // necessary. These follow the JUMP_SLOT relocations.
1139 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1140 Output_data_plt_x86_64
<size
>::rela_tlsdesc(Layout
* layout
)
1142 if (this->tlsdesc_rel_
== NULL
)
1144 this->tlsdesc_rel_
= new Reloc_section(false);
1145 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1146 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
1147 ORDER_DYNAMIC_PLT_RELOCS
, false);
1148 gold_assert(this->tlsdesc_rel_
->output_section()
1149 == this->rel_
->output_section());
1151 return this->tlsdesc_rel_
;
1154 // Return where the IRELATIVE relocations should go in the PLT. These
1155 // follow the JUMP_SLOT and the TLSDESC relocations.
1158 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1159 Output_data_plt_x86_64
<size
>::rela_irelative(Symbol_table
* symtab
,
1162 if (this->irelative_rel_
== NULL
)
1164 // Make sure we have a place for the TLSDESC relocations, in
1165 // case we see any later on.
1166 this->rela_tlsdesc(layout
);
1167 this->irelative_rel_
= new Reloc_section(false);
1168 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1169 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
1170 ORDER_DYNAMIC_PLT_RELOCS
, false);
1171 gold_assert(this->irelative_rel_
->output_section()
1172 == this->rel_
->output_section());
1174 if (parameters
->doing_static_link())
1176 // A statically linked executable will only have a .rela.plt
1177 // section to hold R_X86_64_IRELATIVE relocs for
1178 // STT_GNU_IFUNC symbols. The library will use these
1179 // symbols to locate the IRELATIVE relocs at program startup
1181 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
1182 Symbol_table::PREDEFINED
,
1183 this->irelative_rel_
, 0, 0,
1184 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1185 elfcpp::STV_HIDDEN
, 0, false, true);
1186 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
1187 Symbol_table::PREDEFINED
,
1188 this->irelative_rel_
, 0, 0,
1189 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1190 elfcpp::STV_HIDDEN
, 0, true, true);
1193 return this->irelative_rel_
;
1196 // Return the PLT address to use for a global symbol.
1200 Output_data_plt_x86_64
<size
>::address_for_global(const Symbol
* gsym
)
1202 uint64_t offset
= 0;
1203 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1204 && gsym
->can_use_relative_reloc(false))
1205 offset
= (this->count_
+ 1) * plt_entry_size
;
1206 return this->address() + offset
;
1209 // Return the PLT address to use for a local symbol. These are always
1210 // IRELATIVE relocs.
1214 Output_data_plt_x86_64
<size
>::address_for_local(const Relobj
*, unsigned int)
1216 return this->address() + (this->count_
+ 1) * plt_entry_size
;
1219 // Set the final size.
1222 Output_data_plt_x86_64
<size
>::set_final_data_size()
1224 unsigned int count
= this->count_
+ this->irelative_count_
;
1225 if (this->has_tlsdesc_entry())
1227 this->set_data_size((count
+ 1) * plt_entry_size
);
1230 // The first entry in the PLT for an executable.
1234 Output_data_plt_x86_64
<size
>::first_plt_entry
[plt_entry_size
] =
1236 // From AMD64 ABI Draft 0.98, page 76
1237 0xff, 0x35, // pushq contents of memory address
1238 0, 0, 0, 0, // replaced with address of .got + 8
1239 0xff, 0x25, // jmp indirect
1240 0, 0, 0, 0, // replaced with address of .got + 16
1241 0x90, 0x90, 0x90, 0x90 // noop (x4)
1244 // Subsequent entries in the PLT for an executable.
1248 Output_data_plt_x86_64
<size
>::plt_entry
[plt_entry_size
] =
1250 // From AMD64 ABI Draft 0.98, page 76
1251 0xff, 0x25, // jmpq indirect
1252 0, 0, 0, 0, // replaced with address of symbol in .got
1253 0x68, // pushq immediate
1254 0, 0, 0, 0, // replaced with offset into relocation table
1255 0xe9, // jmpq relative
1256 0, 0, 0, 0 // replaced with offset to start of .plt
1259 // The reserved TLSDESC entry in the PLT for an executable.
1263 Output_data_plt_x86_64
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
1265 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1266 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1267 0xff, 0x35, // pushq x(%rip)
1268 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1269 0xff, 0x25, // jmpq *y(%rip)
1270 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1275 // The .eh_frame unwind information for the PLT.
1279 Output_data_plt_x86_64
<size
>::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
1282 'z', // Augmentation: augmentation size included.
1283 'R', // Augmentation: FDE encoding included.
1284 '\0', // End of augmentation string.
1285 1, // Code alignment factor.
1286 0x78, // Data alignment factor.
1287 16, // Return address column.
1288 1, // Augmentation size.
1289 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
1290 | elfcpp::DW_EH_PE_sdata4
),
1291 elfcpp::DW_CFA_def_cfa
, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1292 elfcpp::DW_CFA_offset
+ 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1293 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
1299 Output_data_plt_x86_64
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
1301 0, 0, 0, 0, // Replaced with offset to .plt.
1302 0, 0, 0, 0, // Replaced with size of .plt.
1303 0, // Augmentation size.
1304 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
1305 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
1306 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
1307 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
1308 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
1309 11, // Block length.
1310 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
1311 elfcpp::DW_OP_breg16
, 0, // Push %rip.
1312 elfcpp::DW_OP_lit15
, // Push 0xf.
1313 elfcpp::DW_OP_and
, // & (%rip & 0xf).
1314 elfcpp::DW_OP_lit11
, // Push 0xb.
1315 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 0xb)
1316 elfcpp::DW_OP_lit3
, // Push 3.
1317 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 0xb) << 3)
1318 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1319 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
1325 // Write out the PLT. This uses the hand-coded instructions above,
1326 // and adjusts them as needed. This is specified by the AMD64 ABI.
1330 Output_data_plt_x86_64
<size
>::do_write(Output_file
* of
)
1332 const off_t offset
= this->offset();
1333 const section_size_type oview_size
=
1334 convert_to_section_size_type(this->data_size());
1335 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1337 const off_t got_file_offset
= this->got_plt_
->offset();
1338 gold_assert(parameters
->incremental_update()
1339 || (got_file_offset
+ this->got_plt_
->data_size()
1340 == this->got_irelative_
->offset()));
1341 const section_size_type got_size
=
1342 convert_to_section_size_type(this->got_plt_
->data_size()
1343 + this->got_irelative_
->data_size());
1344 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1347 unsigned char* pov
= oview
;
1349 // The base address of the .plt section.
1350 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
1351 // The base address of the .got section.
1352 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
= this->got_
->address();
1353 // The base address of the PLT portion of the .got section,
1354 // which is where the GOT pointer will point, and where the
1355 // three reserved GOT entries are located.
1356 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
1357 = this->got_plt_
->address();
1359 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1360 // We do a jmp relative to the PC at the end of this instruction.
1361 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1363 - (plt_address
+ 6)));
1364 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1366 - (plt_address
+ 12)));
1367 pov
+= plt_entry_size
;
1369 unsigned char* got_pov
= got_view
;
1371 // The first entry in the GOT is the address of the .dynamic section
1372 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1373 // We saved space for them when we created the section in
1374 // Target_x86_64::got_section.
1375 Output_section
* dynamic
= this->layout_
->dynamic_section();
1376 uint32_t dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
1377 elfcpp::Swap
<64, false>::writeval(got_pov
, dynamic_addr
);
1379 memset(got_pov
, 0, 16);
1382 unsigned int plt_offset
= plt_entry_size
;
1383 unsigned int got_offset
= 24;
1384 const unsigned int count
= this->count_
+ this->irelative_count_
;
1385 for (unsigned int plt_index
= 0;
1388 pov
+= plt_entry_size
,
1390 plt_offset
+= plt_entry_size
,
1393 // Set and adjust the PLT entry itself.
1394 memcpy(pov
, plt_entry
, plt_entry_size
);
1395 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1396 (got_address
+ got_offset
1397 - (plt_address
+ plt_offset
1400 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1401 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1402 - (plt_offset
+ plt_entry_size
));
1404 // Set the entry in the GOT.
1405 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
1408 if (this->has_tlsdesc_entry())
1410 // Set and adjust the reserved TLSDESC PLT entry.
1411 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
1412 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1413 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1415 - (plt_address
+ plt_offset
1417 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1419 + tlsdesc_got_offset
1420 - (plt_address
+ plt_offset
1422 pov
+= plt_entry_size
;
1425 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1426 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1428 of
->write_output_view(offset
, oview_size
, oview
);
1429 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1432 // Create the PLT section.
1436 Target_x86_64
<size
>::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1438 if (this->plt_
== NULL
)
1440 // Create the GOT sections first.
1441 this->got_section(symtab
, layout
);
1443 this->plt_
= new Output_data_plt_x86_64
<size
>(layout
, this->got_
,
1445 this->got_irelative_
);
1446 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1448 | elfcpp::SHF_EXECINSTR
),
1449 this->plt_
, ORDER_PLT
, false);
1451 // Make the sh_info field of .rela.plt point to .plt.
1452 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1453 rela_plt_os
->set_info_section(this->plt_
->output_section());
1457 // Return the section for TLSDESC relocations.
1460 typename Target_x86_64
<size
>::Reloc_section
*
1461 Target_x86_64
<size
>::rela_tlsdesc_section(Layout
* layout
) const
1463 return this->plt_section()->rela_tlsdesc(layout
);
1466 // Create a PLT entry for a global symbol.
1470 Target_x86_64
<size
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1473 if (gsym
->has_plt_offset())
1476 if (this->plt_
== NULL
)
1477 this->make_plt_section(symtab
, layout
);
1479 this->plt_
->add_entry(symtab
, layout
, gsym
);
1482 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1486 Target_x86_64
<size
>::make_local_ifunc_plt_entry(
1487 Symbol_table
* symtab
, Layout
* layout
,
1488 Sized_relobj_file
<size
, false>* relobj
,
1489 unsigned int local_sym_index
)
1491 if (relobj
->local_has_plt_offset(local_sym_index
))
1493 if (this->plt_
== NULL
)
1494 this->make_plt_section(symtab
, layout
);
1495 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
1498 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1501 // Return the number of entries in the PLT.
1505 Target_x86_64
<size
>::plt_entry_count() const
1507 if (this->plt_
== NULL
)
1509 return this->plt_
->entry_count();
1512 // Return the offset of the first non-reserved PLT entry.
1516 Target_x86_64
<size
>::first_plt_entry_offset() const
1518 return Output_data_plt_x86_64
<size
>::first_plt_entry_offset();
1521 // Return the size of each PLT entry.
1525 Target_x86_64
<size
>::plt_entry_size() const
1527 return Output_data_plt_x86_64
<size
>::get_plt_entry_size();
1530 // Create the GOT and PLT sections for an incremental update.
1533 Output_data_got_base
*
1534 Target_x86_64
<size
>::init_got_plt_for_update(Symbol_table
* symtab
,
1536 unsigned int got_count
,
1537 unsigned int plt_count
)
1539 gold_assert(this->got_
== NULL
);
1541 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
1542 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1544 | elfcpp::SHF_WRITE
),
1545 this->got_
, ORDER_RELRO_LAST
,
1548 // Add the three reserved entries.
1549 this->got_plt_
= new Output_data_space((plt_count
+ 3) * 8, 8, "** GOT PLT");
1550 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1552 | elfcpp::SHF_WRITE
),
1553 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
1556 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1557 this->global_offset_table_
=
1558 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1559 Symbol_table::PREDEFINED
,
1561 0, 0, elfcpp::STT_OBJECT
,
1563 elfcpp::STV_HIDDEN
, 0,
1566 // If there are any TLSDESC relocations, they get GOT entries in
1567 // .got.plt after the jump slot entries.
1568 // FIXME: Get the count for TLSDESC entries.
1569 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
1570 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1571 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1573 ORDER_NON_RELRO_FIRST
, false);
1575 // If there are any IRELATIVE relocations, they get GOT entries in
1576 // .got.plt after the jump slot and TLSDESC entries.
1577 this->got_irelative_
= new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1578 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1579 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1580 this->got_irelative_
,
1581 ORDER_NON_RELRO_FIRST
, false);
1583 // Create the PLT section.
1584 this->plt_
= new Output_data_plt_x86_64
<size
>(layout
, this->got_
,
1586 this->got_irelative_
,
1588 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1589 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1590 this->plt_
, ORDER_PLT
, false);
1592 // Make the sh_info field of .rela.plt point to .plt.
1593 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1594 rela_plt_os
->set_info_section(this->plt_
->output_section());
1596 // Create the rela_dyn section.
1597 this->rela_dyn_section(layout
);
1602 // Reserve a GOT entry for a local symbol, and regenerate any
1603 // necessary dynamic relocations.
1607 Target_x86_64
<size
>::reserve_local_got_entry(
1608 unsigned int got_index
,
1609 Sized_relobj
<size
, false>* obj
,
1611 unsigned int got_type
)
1613 unsigned int got_offset
= got_index
* 8;
1614 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1616 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
1619 case GOT_TYPE_STANDARD
:
1620 if (parameters
->options().output_is_position_independent())
1621 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
1622 this->got_
, got_offset
, 0, false);
1624 case GOT_TYPE_TLS_OFFSET
:
1625 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
1626 this->got_
, got_offset
, 0);
1628 case GOT_TYPE_TLS_PAIR
:
1629 this->got_
->reserve_slot(got_index
+ 1);
1630 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
1631 this->got_
, got_offset
, 0);
1633 case GOT_TYPE_TLS_DESC
:
1634 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1635 // this->got_->reserve_slot(got_index + 1);
1636 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1637 // this->got_, got_offset, 0);
1644 // Reserve a GOT entry for a global symbol, and regenerate any
1645 // necessary dynamic relocations.
1649 Target_x86_64
<size
>::reserve_global_got_entry(unsigned int got_index
,
1651 unsigned int got_type
)
1653 unsigned int got_offset
= got_index
* 8;
1654 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1656 this->got_
->reserve_global(got_index
, gsym
, got_type
);
1659 case GOT_TYPE_STANDARD
:
1660 if (!gsym
->final_value_is_known())
1662 if (gsym
->is_from_dynobj()
1663 || gsym
->is_undefined()
1664 || gsym
->is_preemptible()
1665 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1666 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
1667 this->got_
, got_offset
, 0);
1669 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1670 this->got_
, got_offset
, 0);
1673 case GOT_TYPE_TLS_OFFSET
:
1674 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
1675 this->got_
, got_offset
, 0);
1677 case GOT_TYPE_TLS_PAIR
:
1678 this->got_
->reserve_slot(got_index
+ 1);
1679 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
1680 this->got_
, got_offset
, 0);
1681 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
1682 this->got_
, got_offset
+ 8, 0);
1684 case GOT_TYPE_TLS_DESC
:
1685 this->got_
->reserve_slot(got_index
+ 1);
1686 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
1687 this->got_
, got_offset
, 0);
1694 // Register an existing PLT entry for a global symbol.
1698 Target_x86_64
<size
>::register_global_plt_entry(Symbol_table
* symtab
,
1700 unsigned int plt_index
,
1703 gold_assert(this->plt_
!= NULL
);
1704 gold_assert(!gsym
->has_plt_offset());
1706 this->plt_
->reserve_slot(plt_index
);
1708 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
1710 unsigned int got_offset
= (plt_index
+ 3) * 8;
1711 this->plt_
->add_relocation(symtab
, layout
, gsym
, got_offset
);
1714 // Force a COPY relocation for a given symbol.
1718 Target_x86_64
<size
>::emit_copy_reloc(
1719 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
1721 this->copy_relocs_
.emit_copy_reloc(symtab
,
1722 symtab
->get_sized_symbol
<size
>(sym
),
1725 this->rela_dyn_section(NULL
));
1728 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1732 Target_x86_64
<size
>::define_tls_base_symbol(Symbol_table
* symtab
,
1735 if (this->tls_base_symbol_defined_
)
1738 Output_segment
* tls_segment
= layout
->tls_segment();
1739 if (tls_segment
!= NULL
)
1741 bool is_exec
= parameters
->options().output_is_executable();
1742 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1743 Symbol_table::PREDEFINED
,
1747 elfcpp::STV_HIDDEN
, 0,
1749 ? Symbol::SEGMENT_END
1750 : Symbol::SEGMENT_START
),
1753 this->tls_base_symbol_defined_
= true;
1756 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1760 Target_x86_64
<size
>::reserve_tlsdesc_entries(Symbol_table
* symtab
,
1763 if (this->plt_
== NULL
)
1764 this->make_plt_section(symtab
, layout
);
1766 if (!this->plt_
->has_tlsdesc_entry())
1768 // Allocate the TLSDESC_GOT entry.
1769 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1770 unsigned int got_offset
= got
->add_constant(0);
1772 // Allocate the TLSDESC_PLT entry.
1773 this->plt_
->reserve_tlsdesc_entry(got_offset
);
1777 // Create a GOT entry for the TLS module index.
1781 Target_x86_64
<size
>::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1782 Sized_relobj_file
<size
, false>* object
)
1784 if (this->got_mod_index_offset_
== -1U)
1786 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1787 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1788 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
1789 unsigned int got_offset
= got
->add_constant(0);
1790 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
1792 got
->add_constant(0);
1793 this->got_mod_index_offset_
= got_offset
;
1795 return this->got_mod_index_offset_
;
1798 // Optimize the TLS relocation type based on what we know about the
1799 // symbol. IS_FINAL is true if the final address of this symbol is
1800 // known at link time.
1803 tls::Tls_optimization
1804 Target_x86_64
<size
>::optimize_tls_reloc(bool is_final
, int r_type
)
1806 // If we are generating a shared library, then we can't do anything
1808 if (parameters
->options().shared())
1809 return tls::TLSOPT_NONE
;
1813 case elfcpp::R_X86_64_TLSGD
:
1814 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1815 case elfcpp::R_X86_64_TLSDESC_CALL
:
1816 // These are General-Dynamic which permits fully general TLS
1817 // access. Since we know that we are generating an executable,
1818 // we can convert this to Initial-Exec. If we also know that
1819 // this is a local symbol, we can further switch to Local-Exec.
1821 return tls::TLSOPT_TO_LE
;
1822 return tls::TLSOPT_TO_IE
;
1824 case elfcpp::R_X86_64_TLSLD
:
1825 // This is Local-Dynamic, which refers to a local symbol in the
1826 // dynamic TLS block. Since we know that we generating an
1827 // executable, we can switch to Local-Exec.
1828 return tls::TLSOPT_TO_LE
;
1830 case elfcpp::R_X86_64_DTPOFF32
:
1831 case elfcpp::R_X86_64_DTPOFF64
:
1832 // Another Local-Dynamic reloc.
1833 return tls::TLSOPT_TO_LE
;
1835 case elfcpp::R_X86_64_GOTTPOFF
:
1836 // These are Initial-Exec relocs which get the thread offset
1837 // from the GOT. If we know that we are linking against the
1838 // local symbol, we can switch to Local-Exec, which links the
1839 // thread offset into the instruction.
1841 return tls::TLSOPT_TO_LE
;
1842 return tls::TLSOPT_NONE
;
1844 case elfcpp::R_X86_64_TPOFF32
:
1845 // When we already have Local-Exec, there is nothing further we
1847 return tls::TLSOPT_NONE
;
1854 // Get the Reference_flags for a particular relocation.
1858 Target_x86_64
<size
>::Scan::get_reference_flags(unsigned int r_type
)
1862 case elfcpp::R_X86_64_NONE
:
1863 case elfcpp::R_X86_64_GNU_VTINHERIT
:
1864 case elfcpp::R_X86_64_GNU_VTENTRY
:
1865 case elfcpp::R_X86_64_GOTPC32
:
1866 case elfcpp::R_X86_64_GOTPC64
:
1867 // No symbol reference.
1870 case elfcpp::R_X86_64_64
:
1871 case elfcpp::R_X86_64_32
:
1872 case elfcpp::R_X86_64_32S
:
1873 case elfcpp::R_X86_64_16
:
1874 case elfcpp::R_X86_64_8
:
1875 return Symbol::ABSOLUTE_REF
;
1877 case elfcpp::R_X86_64_PC64
:
1878 case elfcpp::R_X86_64_PC32
:
1879 case elfcpp::R_X86_64_PC16
:
1880 case elfcpp::R_X86_64_PC8
:
1881 case elfcpp::R_X86_64_GOTOFF64
:
1882 return Symbol::RELATIVE_REF
;
1884 case elfcpp::R_X86_64_PLT32
:
1885 case elfcpp::R_X86_64_PLTOFF64
:
1886 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
1888 case elfcpp::R_X86_64_GOT64
:
1889 case elfcpp::R_X86_64_GOT32
:
1890 case elfcpp::R_X86_64_GOTPCREL64
:
1891 case elfcpp::R_X86_64_GOTPCREL
:
1892 case elfcpp::R_X86_64_GOTPLT64
:
1894 return Symbol::ABSOLUTE_REF
;
1896 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1897 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1898 case elfcpp::R_X86_64_TLSDESC_CALL
:
1899 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1900 case elfcpp::R_X86_64_DTPOFF32
:
1901 case elfcpp::R_X86_64_DTPOFF64
:
1902 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1903 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1904 return Symbol::TLS_REF
;
1906 case elfcpp::R_X86_64_COPY
:
1907 case elfcpp::R_X86_64_GLOB_DAT
:
1908 case elfcpp::R_X86_64_JUMP_SLOT
:
1909 case elfcpp::R_X86_64_RELATIVE
:
1910 case elfcpp::R_X86_64_IRELATIVE
:
1911 case elfcpp::R_X86_64_TPOFF64
:
1912 case elfcpp::R_X86_64_DTPMOD64
:
1913 case elfcpp::R_X86_64_TLSDESC
:
1914 case elfcpp::R_X86_64_SIZE32
:
1915 case elfcpp::R_X86_64_SIZE64
:
1917 // Not expected. We will give an error later.
1922 // Report an unsupported relocation against a local symbol.
1926 Target_x86_64
<size
>::Scan::unsupported_reloc_local(
1927 Sized_relobj_file
<size
, false>* object
,
1928 unsigned int r_type
)
1930 gold_error(_("%s: unsupported reloc %u against local symbol"),
1931 object
->name().c_str(), r_type
);
1934 // We are about to emit a dynamic relocation of type R_TYPE. If the
1935 // dynamic linker does not support it, issue an error. The GNU linker
1936 // only issues a non-PIC error for an allocated read-only section.
1937 // Here we know the section is allocated, but we don't know that it is
1938 // read-only. But we check for all the relocation types which the
1939 // glibc dynamic linker supports, so it seems appropriate to issue an
1940 // error even if the section is not read-only. If GSYM is not NULL,
1941 // it is the symbol the relocation is against; if it is NULL, the
1942 // relocation is against a local symbol.
1946 Target_x86_64
<size
>::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
1951 // These are the relocation types supported by glibc for x86_64
1952 // which should always work.
1953 case elfcpp::R_X86_64_RELATIVE
:
1954 case elfcpp::R_X86_64_IRELATIVE
:
1955 case elfcpp::R_X86_64_GLOB_DAT
:
1956 case elfcpp::R_X86_64_JUMP_SLOT
:
1957 case elfcpp::R_X86_64_DTPMOD64
:
1958 case elfcpp::R_X86_64_DTPOFF64
:
1959 case elfcpp::R_X86_64_TPOFF64
:
1960 case elfcpp::R_X86_64_64
:
1961 case elfcpp::R_X86_64_COPY
:
1964 // glibc supports these reloc types, but they can overflow.
1965 case elfcpp::R_X86_64_PC32
:
1966 // A PC relative reference is OK against a local symbol or if
1967 // the symbol is defined locally.
1969 || (!gsym
->is_from_dynobj()
1970 && !gsym
->is_undefined()
1971 && !gsym
->is_preemptible()))
1974 case elfcpp::R_X86_64_32
:
1975 // R_X86_64_32 is OK for x32.
1976 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
1978 if (this->issued_non_pic_error_
)
1980 gold_assert(parameters
->options().output_is_position_independent());
1982 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
1983 "overflow at runtime; recompile with -fPIC"));
1985 object
->error(_("requires dynamic %s reloc against '%s' which may "
1986 "overflow at runtime; recompile with -fPIC"),
1987 (r_type
== elfcpp::R_X86_64_32
1991 this->issued_non_pic_error_
= true;
1995 // This prevents us from issuing more than one error per reloc
1996 // section. But we can still wind up issuing more than one
1997 // error per object file.
1998 if (this->issued_non_pic_error_
)
2000 gold_assert(parameters
->options().output_is_position_independent());
2001 object
->error(_("requires unsupported dynamic reloc %u; "
2002 "recompile with -fPIC"),
2004 this->issued_non_pic_error_
= true;
2007 case elfcpp::R_X86_64_NONE
:
2012 // Return whether we need to make a PLT entry for a relocation of the
2013 // given type against a STT_GNU_IFUNC symbol.
2017 Target_x86_64
<size
>::Scan::reloc_needs_plt_for_ifunc(
2018 Sized_relobj_file
<size
, false>* object
,
2019 unsigned int r_type
)
2021 int flags
= Scan::get_reference_flags(r_type
);
2022 if (flags
& Symbol::TLS_REF
)
2023 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2024 object
->name().c_str(), r_type
);
2028 // Scan a relocation for a local symbol.
2032 Target_x86_64
<size
>::Scan::local(Symbol_table
* symtab
,
2034 Target_x86_64
<size
>* target
,
2035 Sized_relobj_file
<size
, false>* object
,
2036 unsigned int data_shndx
,
2037 Output_section
* output_section
,
2038 const elfcpp::Rela
<size
, false>& reloc
,
2039 unsigned int r_type
,
2040 const elfcpp::Sym
<size
, false>& lsym
)
2042 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2043 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
2044 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
2046 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2047 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
2052 case elfcpp::R_X86_64_NONE
:
2053 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2054 case elfcpp::R_X86_64_GNU_VTENTRY
:
2057 case elfcpp::R_X86_64_64
:
2058 // If building a shared library (or a position-independent
2059 // executable), we need to create a dynamic relocation for this
2060 // location. The relocation applied at link time will apply the
2061 // link-time value, so we flag the location with an
2062 // R_X86_64_RELATIVE relocation so the dynamic loader can
2063 // relocate it easily.
2064 if (parameters
->options().output_is_position_independent())
2066 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2067 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2068 rela_dyn
->add_local_relative(object
, r_sym
,
2069 elfcpp::R_X86_64_RELATIVE
,
2070 output_section
, data_shndx
,
2071 reloc
.get_r_offset(),
2072 reloc
.get_r_addend(), is_ifunc
);
2076 case elfcpp::R_X86_64_32
:
2077 case elfcpp::R_X86_64_32S
:
2078 case elfcpp::R_X86_64_16
:
2079 case elfcpp::R_X86_64_8
:
2080 // If building a shared library (or a position-independent
2081 // executable), we need to create a dynamic relocation for this
2082 // location. We can't use an R_X86_64_RELATIVE relocation
2083 // because that is always a 64-bit relocation.
2084 if (parameters
->options().output_is_position_independent())
2086 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2087 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2089 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2090 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2091 rela_dyn
->add_local_relative(object
, r_sym
,
2092 elfcpp::R_X86_64_RELATIVE
,
2093 output_section
, data_shndx
,
2094 reloc
.get_r_offset(),
2095 reloc
.get_r_addend(), is_ifunc
);
2099 this->check_non_pic(object
, r_type
, NULL
);
2101 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2102 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2103 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
2104 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
2105 data_shndx
, reloc
.get_r_offset(),
2106 reloc
.get_r_addend());
2109 gold_assert(lsym
.get_st_value() == 0);
2110 unsigned int shndx
= lsym
.get_st_shndx();
2112 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
2115 object
->error(_("section symbol %u has bad shndx %u"),
2118 rela_dyn
->add_local_section(object
, shndx
,
2119 r_type
, output_section
,
2120 data_shndx
, reloc
.get_r_offset(),
2121 reloc
.get_r_addend());
2126 case elfcpp::R_X86_64_PC64
:
2127 case elfcpp::R_X86_64_PC32
:
2128 case elfcpp::R_X86_64_PC16
:
2129 case elfcpp::R_X86_64_PC8
:
2132 case elfcpp::R_X86_64_PLT32
:
2133 // Since we know this is a local symbol, we can handle this as a
2137 case elfcpp::R_X86_64_GOTPC32
:
2138 case elfcpp::R_X86_64_GOTOFF64
:
2139 case elfcpp::R_X86_64_GOTPC64
:
2140 case elfcpp::R_X86_64_PLTOFF64
:
2141 // We need a GOT section.
2142 target
->got_section(symtab
, layout
);
2143 // For PLTOFF64, we'd normally want a PLT section, but since we
2144 // know this is a local symbol, no PLT is needed.
2147 case elfcpp::R_X86_64_GOT64
:
2148 case elfcpp::R_X86_64_GOT32
:
2149 case elfcpp::R_X86_64_GOTPCREL64
:
2150 case elfcpp::R_X86_64_GOTPCREL
:
2151 case elfcpp::R_X86_64_GOTPLT64
:
2153 // The symbol requires a GOT entry.
2154 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2155 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2157 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2158 // lets function pointers compare correctly with shared
2159 // libraries. Otherwise we would need an IRELATIVE reloc.
2162 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
2164 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
2167 // If we are generating a shared object, we need to add a
2168 // dynamic relocation for this symbol's GOT entry.
2169 if (parameters
->options().output_is_position_independent())
2171 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2172 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2173 if (r_type
!= elfcpp::R_X86_64_GOT32
)
2175 unsigned int got_offset
=
2176 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
2177 rela_dyn
->add_local_relative(object
, r_sym
,
2178 elfcpp::R_X86_64_RELATIVE
,
2179 got
, got_offset
, 0, is_ifunc
);
2183 this->check_non_pic(object
, r_type
, NULL
);
2185 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
2186 rela_dyn
->add_local(
2187 object
, r_sym
, r_type
, got
,
2188 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
2192 // For GOTPLT64, we'd normally want a PLT section, but since
2193 // we know this is a local symbol, no PLT is needed.
2197 case elfcpp::R_X86_64_COPY
:
2198 case elfcpp::R_X86_64_GLOB_DAT
:
2199 case elfcpp::R_X86_64_JUMP_SLOT
:
2200 case elfcpp::R_X86_64_RELATIVE
:
2201 case elfcpp::R_X86_64_IRELATIVE
:
2202 // These are outstanding tls relocs, which are unexpected when linking
2203 case elfcpp::R_X86_64_TPOFF64
:
2204 case elfcpp::R_X86_64_DTPMOD64
:
2205 case elfcpp::R_X86_64_TLSDESC
:
2206 gold_error(_("%s: unexpected reloc %u in object file"),
2207 object
->name().c_str(), r_type
);
2210 // These are initial tls relocs, which are expected when linking
2211 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2212 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2213 case elfcpp::R_X86_64_TLSDESC_CALL
:
2214 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2215 case elfcpp::R_X86_64_DTPOFF32
:
2216 case elfcpp::R_X86_64_DTPOFF64
:
2217 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2218 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2220 bool output_is_shared
= parameters
->options().shared();
2221 const tls::Tls_optimization optimized_type
2222 = Target_x86_64
<size
>::optimize_tls_reloc(!output_is_shared
,
2226 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2227 if (optimized_type
== tls::TLSOPT_NONE
)
2229 // Create a pair of GOT entries for the module index and
2230 // dtv-relative offset.
2231 Output_data_got
<64, false>* got
2232 = target
->got_section(symtab
, layout
);
2233 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2234 unsigned int shndx
= lsym
.get_st_shndx();
2236 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2238 object
->error(_("local symbol %u has bad shndx %u"),
2241 got
->add_local_pair_with_rel(object
, r_sym
,
2244 target
->rela_dyn_section(layout
),
2245 elfcpp::R_X86_64_DTPMOD64
, 0);
2247 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2248 unsupported_reloc_local(object
, r_type
);
2251 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2252 target
->define_tls_base_symbol(symtab
, layout
);
2253 if (optimized_type
== tls::TLSOPT_NONE
)
2255 // Create reserved PLT and GOT entries for the resolver.
2256 target
->reserve_tlsdesc_entries(symtab
, layout
);
2258 // Generate a double GOT entry with an
2259 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2260 // is resolved lazily, so the GOT entry needs to be in
2261 // an area in .got.plt, not .got. Call got_section to
2262 // make sure the section has been created.
2263 target
->got_section(symtab
, layout
);
2264 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2265 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2266 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
2268 unsigned int got_offset
= got
->add_constant(0);
2269 got
->add_constant(0);
2270 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
2272 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2273 // We store the arguments we need in a vector, and
2274 // use the index into the vector as the parameter
2275 // to pass to the target specific routines.
2276 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
2277 void* arg
= reinterpret_cast<void*>(intarg
);
2278 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
2279 got
, got_offset
, 0);
2282 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2283 unsupported_reloc_local(object
, r_type
);
2286 case elfcpp::R_X86_64_TLSDESC_CALL
:
2289 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2290 if (optimized_type
== tls::TLSOPT_NONE
)
2292 // Create a GOT entry for the module index.
2293 target
->got_mod_index_entry(symtab
, layout
, object
);
2295 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2296 unsupported_reloc_local(object
, r_type
);
2299 case elfcpp::R_X86_64_DTPOFF32
:
2300 case elfcpp::R_X86_64_DTPOFF64
:
2303 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2304 layout
->set_has_static_tls();
2305 if (optimized_type
== tls::TLSOPT_NONE
)
2307 // Create a GOT entry for the tp-relative offset.
2308 Output_data_got
<64, false>* got
2309 = target
->got_section(symtab
, layout
);
2310 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2311 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
2312 target
->rela_dyn_section(layout
),
2313 elfcpp::R_X86_64_TPOFF64
);
2315 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2316 unsupported_reloc_local(object
, r_type
);
2319 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2320 layout
->set_has_static_tls();
2321 if (output_is_shared
)
2322 unsupported_reloc_local(object
, r_type
);
2331 case elfcpp::R_X86_64_SIZE32
:
2332 case elfcpp::R_X86_64_SIZE64
:
2334 gold_error(_("%s: unsupported reloc %u against local symbol"),
2335 object
->name().c_str(), r_type
);
2341 // Report an unsupported relocation against a global symbol.
2345 Target_x86_64
<size
>::Scan::unsupported_reloc_global(
2346 Sized_relobj_file
<size
, false>* object
,
2347 unsigned int r_type
,
2350 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2351 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
2354 // Returns true if this relocation type could be that of a function pointer.
2357 Target_x86_64
<size
>::Scan::possible_function_pointer_reloc(unsigned int r_type
)
2361 case elfcpp::R_X86_64_64
:
2362 case elfcpp::R_X86_64_32
:
2363 case elfcpp::R_X86_64_32S
:
2364 case elfcpp::R_X86_64_16
:
2365 case elfcpp::R_X86_64_8
:
2366 case elfcpp::R_X86_64_GOT64
:
2367 case elfcpp::R_X86_64_GOT32
:
2368 case elfcpp::R_X86_64_GOTPCREL64
:
2369 case elfcpp::R_X86_64_GOTPCREL
:
2370 case elfcpp::R_X86_64_GOTPLT64
:
2378 // For safe ICF, scan a relocation for a local symbol to check if it
2379 // corresponds to a function pointer being taken. In that case mark
2380 // the function whose pointer was taken as not foldable.
2384 Target_x86_64
<size
>::Scan::local_reloc_may_be_function_pointer(
2387 Target_x86_64
<size
>* ,
2388 Sized_relobj_file
<size
, false>* ,
2391 const elfcpp::Rela
<size
, false>& ,
2392 unsigned int r_type
,
2393 const elfcpp::Sym
<size
, false>&)
2395 // When building a shared library, do not fold any local symbols as it is
2396 // not possible to distinguish pointer taken versus a call by looking at
2397 // the relocation types.
2398 return (parameters
->options().shared()
2399 || possible_function_pointer_reloc(r_type
));
2402 // For safe ICF, scan a relocation for a global symbol to check if it
2403 // corresponds to a function pointer being taken. In that case mark
2404 // the function whose pointer was taken as not foldable.
2408 Target_x86_64
<size
>::Scan::global_reloc_may_be_function_pointer(
2411 Target_x86_64
<size
>* ,
2412 Sized_relobj_file
<size
, false>* ,
2415 const elfcpp::Rela
<size
, false>& ,
2416 unsigned int r_type
,
2419 // When building a shared library, do not fold symbols whose visibility
2420 // is hidden, internal or protected.
2421 return ((parameters
->options().shared()
2422 && (gsym
->visibility() == elfcpp::STV_INTERNAL
2423 || gsym
->visibility() == elfcpp::STV_PROTECTED
2424 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
2425 || possible_function_pointer_reloc(r_type
));
2428 // Scan a relocation for a global symbol.
2432 Target_x86_64
<size
>::Scan::global(Symbol_table
* symtab
,
2434 Target_x86_64
<size
>* target
,
2435 Sized_relobj_file
<size
, false>* object
,
2436 unsigned int data_shndx
,
2437 Output_section
* output_section
,
2438 const elfcpp::Rela
<size
, false>& reloc
,
2439 unsigned int r_type
,
2442 // A STT_GNU_IFUNC symbol may require a PLT entry.
2443 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2444 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
2445 target
->make_plt_entry(symtab
, layout
, gsym
);
2449 case elfcpp::R_X86_64_NONE
:
2450 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2451 case elfcpp::R_X86_64_GNU_VTENTRY
:
2454 case elfcpp::R_X86_64_64
:
2455 case elfcpp::R_X86_64_32
:
2456 case elfcpp::R_X86_64_32S
:
2457 case elfcpp::R_X86_64_16
:
2458 case elfcpp::R_X86_64_8
:
2460 // Make a PLT entry if necessary.
2461 if (gsym
->needs_plt_entry())
2463 target
->make_plt_entry(symtab
, layout
, gsym
);
2464 // Since this is not a PC-relative relocation, we may be
2465 // taking the address of a function. In that case we need to
2466 // set the entry in the dynamic symbol table to the address of
2468 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
2469 gsym
->set_needs_dynsym_value();
2471 // Make a dynamic relocation if necessary.
2472 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2474 if (gsym
->may_need_copy_reloc())
2476 target
->copy_reloc(symtab
, layout
, object
,
2477 data_shndx
, output_section
, gsym
, reloc
);
2479 else if (r_type
== elfcpp::R_X86_64_64
2480 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2481 && gsym
->can_use_relative_reloc(false)
2482 && !gsym
->is_from_dynobj()
2483 && !gsym
->is_undefined()
2484 && !gsym
->is_preemptible())
2486 // Use an IRELATIVE reloc for a locally defined
2487 // STT_GNU_IFUNC symbol. This makes a function
2488 // address in a PIE executable match the address in a
2489 // shared library that it links against.
2490 Reloc_section
* rela_dyn
=
2491 target
->rela_irelative_section(layout
);
2492 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
2493 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
2494 output_section
, object
,
2496 reloc
.get_r_offset(),
2497 reloc
.get_r_addend());
2499 else if (r_type
== elfcpp::R_X86_64_64
2500 && gsym
->can_use_relative_reloc(false))
2502 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2503 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2504 output_section
, object
,
2506 reloc
.get_r_offset(),
2507 reloc
.get_r_addend());
2511 this->check_non_pic(object
, r_type
, gsym
);
2512 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2513 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2514 data_shndx
, reloc
.get_r_offset(),
2515 reloc
.get_r_addend());
2521 case elfcpp::R_X86_64_PC64
:
2522 case elfcpp::R_X86_64_PC32
:
2523 case elfcpp::R_X86_64_PC16
:
2524 case elfcpp::R_X86_64_PC8
:
2526 // Make a PLT entry if necessary.
2527 if (gsym
->needs_plt_entry())
2528 target
->make_plt_entry(symtab
, layout
, gsym
);
2529 // Make a dynamic relocation if necessary.
2530 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2532 if (gsym
->may_need_copy_reloc())
2534 target
->copy_reloc(symtab
, layout
, object
,
2535 data_shndx
, output_section
, gsym
, reloc
);
2539 this->check_non_pic(object
, r_type
, gsym
);
2540 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2541 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2542 data_shndx
, reloc
.get_r_offset(),
2543 reloc
.get_r_addend());
2549 case elfcpp::R_X86_64_GOT64
:
2550 case elfcpp::R_X86_64_GOT32
:
2551 case elfcpp::R_X86_64_GOTPCREL64
:
2552 case elfcpp::R_X86_64_GOTPCREL
:
2553 case elfcpp::R_X86_64_GOTPLT64
:
2555 // The symbol requires a GOT entry.
2556 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2557 if (gsym
->final_value_is_known())
2559 // For a STT_GNU_IFUNC symbol we want the PLT address.
2560 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
2561 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2563 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2567 // If this symbol is not fully resolved, we need to add a
2568 // dynamic relocation for it.
2569 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2571 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2573 // 1) The symbol may be defined in some other module.
2575 // 2) We are building a shared library and this is a
2576 // protected symbol; using GLOB_DAT means that the dynamic
2577 // linker can use the address of the PLT in the main
2578 // executable when appropriate so that function address
2579 // comparisons work.
2581 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2582 // code, again so that function address comparisons work.
2583 if (gsym
->is_from_dynobj()
2584 || gsym
->is_undefined()
2585 || gsym
->is_preemptible()
2586 || (gsym
->visibility() == elfcpp::STV_PROTECTED
2587 && parameters
->options().shared())
2588 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
2589 && parameters
->options().output_is_position_independent()))
2590 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2591 elfcpp::R_X86_64_GLOB_DAT
);
2594 // For a STT_GNU_IFUNC symbol we want to write the PLT
2595 // offset into the GOT, so that function pointer
2596 // comparisons work correctly.
2598 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
2599 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2602 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2603 // Tell the dynamic linker to use the PLT address
2604 // when resolving relocations.
2605 if (gsym
->is_from_dynobj()
2606 && !parameters
->options().shared())
2607 gsym
->set_needs_dynsym_value();
2611 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2612 rela_dyn
->add_global_relative(gsym
,
2613 elfcpp::R_X86_64_RELATIVE
,
2618 // For GOTPLT64, we also need a PLT entry (but only if the
2619 // symbol is not fully resolved).
2620 if (r_type
== elfcpp::R_X86_64_GOTPLT64
2621 && !gsym
->final_value_is_known())
2622 target
->make_plt_entry(symtab
, layout
, gsym
);
2626 case elfcpp::R_X86_64_PLT32
:
2627 // If the symbol is fully resolved, this is just a PC32 reloc.
2628 // Otherwise we need a PLT entry.
2629 if (gsym
->final_value_is_known())
2631 // If building a shared library, we can also skip the PLT entry
2632 // if the symbol is defined in the output file and is protected
2634 if (gsym
->is_defined()
2635 && !gsym
->is_from_dynobj()
2636 && !gsym
->is_preemptible())
2638 target
->make_plt_entry(symtab
, layout
, gsym
);
2641 case elfcpp::R_X86_64_GOTPC32
:
2642 case elfcpp::R_X86_64_GOTOFF64
:
2643 case elfcpp::R_X86_64_GOTPC64
:
2644 case elfcpp::R_X86_64_PLTOFF64
:
2645 // We need a GOT section.
2646 target
->got_section(symtab
, layout
);
2647 // For PLTOFF64, we also need a PLT entry (but only if the
2648 // symbol is not fully resolved).
2649 if (r_type
== elfcpp::R_X86_64_PLTOFF64
2650 && !gsym
->final_value_is_known())
2651 target
->make_plt_entry(symtab
, layout
, gsym
);
2654 case elfcpp::R_X86_64_COPY
:
2655 case elfcpp::R_X86_64_GLOB_DAT
:
2656 case elfcpp::R_X86_64_JUMP_SLOT
:
2657 case elfcpp::R_X86_64_RELATIVE
:
2658 case elfcpp::R_X86_64_IRELATIVE
:
2659 // These are outstanding tls relocs, which are unexpected when linking
2660 case elfcpp::R_X86_64_TPOFF64
:
2661 case elfcpp::R_X86_64_DTPMOD64
:
2662 case elfcpp::R_X86_64_TLSDESC
:
2663 gold_error(_("%s: unexpected reloc %u in object file"),
2664 object
->name().c_str(), r_type
);
2667 // These are initial tls relocs, which are expected for global()
2668 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2669 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2670 case elfcpp::R_X86_64_TLSDESC_CALL
:
2671 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2672 case elfcpp::R_X86_64_DTPOFF32
:
2673 case elfcpp::R_X86_64_DTPOFF64
:
2674 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2675 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2677 const bool is_final
= gsym
->final_value_is_known();
2678 const tls::Tls_optimization optimized_type
2679 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
2682 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2683 if (optimized_type
== tls::TLSOPT_NONE
)
2685 // Create a pair of GOT entries for the module index and
2686 // dtv-relative offset.
2687 Output_data_got
<64, false>* got
2688 = target
->got_section(symtab
, layout
);
2689 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
2690 target
->rela_dyn_section(layout
),
2691 elfcpp::R_X86_64_DTPMOD64
,
2692 elfcpp::R_X86_64_DTPOFF64
);
2694 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2696 // Create a GOT entry for the tp-relative offset.
2697 Output_data_got
<64, false>* got
2698 = target
->got_section(symtab
, layout
);
2699 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
2700 target
->rela_dyn_section(layout
),
2701 elfcpp::R_X86_64_TPOFF64
);
2703 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2704 unsupported_reloc_global(object
, r_type
, gsym
);
2707 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2708 target
->define_tls_base_symbol(symtab
, layout
);
2709 if (optimized_type
== tls::TLSOPT_NONE
)
2711 // Create reserved PLT and GOT entries for the resolver.
2712 target
->reserve_tlsdesc_entries(symtab
, layout
);
2714 // Create a double GOT entry with an R_X86_64_TLSDESC
2715 // reloc. The R_X86_64_TLSDESC reloc is resolved
2716 // lazily, so the GOT entry needs to be in an area in
2717 // .got.plt, not .got. Call got_section to make sure
2718 // the section has been created.
2719 target
->got_section(symtab
, layout
);
2720 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2721 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2722 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
2723 elfcpp::R_X86_64_TLSDESC
, 0);
2725 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2727 // Create a GOT entry for the tp-relative offset.
2728 Output_data_got
<64, false>* got
2729 = target
->got_section(symtab
, layout
);
2730 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
2731 target
->rela_dyn_section(layout
),
2732 elfcpp::R_X86_64_TPOFF64
);
2734 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2735 unsupported_reloc_global(object
, r_type
, gsym
);
2738 case elfcpp::R_X86_64_TLSDESC_CALL
:
2741 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2742 if (optimized_type
== tls::TLSOPT_NONE
)
2744 // Create a GOT entry for the module index.
2745 target
->got_mod_index_entry(symtab
, layout
, object
);
2747 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2748 unsupported_reloc_global(object
, r_type
, gsym
);
2751 case elfcpp::R_X86_64_DTPOFF32
:
2752 case elfcpp::R_X86_64_DTPOFF64
:
2755 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2756 layout
->set_has_static_tls();
2757 if (optimized_type
== tls::TLSOPT_NONE
)
2759 // Create a GOT entry for the tp-relative offset.
2760 Output_data_got
<64, false>* got
2761 = target
->got_section(symtab
, layout
);
2762 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
2763 target
->rela_dyn_section(layout
),
2764 elfcpp::R_X86_64_TPOFF64
);
2766 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2767 unsupported_reloc_global(object
, r_type
, gsym
);
2770 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2771 layout
->set_has_static_tls();
2772 if (parameters
->options().shared())
2773 unsupported_reloc_local(object
, r_type
);
2782 case elfcpp::R_X86_64_SIZE32
:
2783 case elfcpp::R_X86_64_SIZE64
:
2785 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2786 object
->name().c_str(), r_type
,
2787 gsym
->demangled_name().c_str());
2794 Target_x86_64
<size
>::gc_process_relocs(Symbol_table
* symtab
,
2796 Sized_relobj_file
<size
, false>* object
,
2797 unsigned int data_shndx
,
2798 unsigned int sh_type
,
2799 const unsigned char* prelocs
,
2801 Output_section
* output_section
,
2802 bool needs_special_offset_handling
,
2803 size_t local_symbol_count
,
2804 const unsigned char* plocal_symbols
)
2807 if (sh_type
== elfcpp::SHT_REL
)
2812 gold::gc_process_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
2813 typename Target_x86_64
<size
>::Scan
,
2814 typename Target_x86_64
<size
>::Relocatable_size_for_reloc
>(
2823 needs_special_offset_handling
,
2828 // Scan relocations for a section.
2832 Target_x86_64
<size
>::scan_relocs(Symbol_table
* symtab
,
2834 Sized_relobj_file
<size
, false>* object
,
2835 unsigned int data_shndx
,
2836 unsigned int sh_type
,
2837 const unsigned char* prelocs
,
2839 Output_section
* output_section
,
2840 bool needs_special_offset_handling
,
2841 size_t local_symbol_count
,
2842 const unsigned char* plocal_symbols
)
2844 if (sh_type
== elfcpp::SHT_REL
)
2846 gold_error(_("%s: unsupported REL reloc section"),
2847 object
->name().c_str());
2851 gold::scan_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
2852 typename Target_x86_64
<size
>::Scan
>(
2861 needs_special_offset_handling
,
2866 // Finalize the sections.
2870 Target_x86_64
<size
>::do_finalize_sections(
2872 const Input_objects
*,
2873 Symbol_table
* symtab
)
2875 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2877 : this->plt_
->rela_plt());
2878 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
2879 this->rela_dyn_
, true, false);
2881 // Fill in some more dynamic tags.
2882 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
2885 if (this->plt_
!= NULL
2886 && this->plt_
->output_section() != NULL
2887 && this->plt_
->has_tlsdesc_entry())
2889 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
2890 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
2891 this->got_
->finalize_data_size();
2892 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
2893 this->plt_
, plt_offset
);
2894 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
2895 this->got_
, got_offset
);
2899 // Emit any relocs we saved in an attempt to avoid generating COPY
2901 if (this->copy_relocs_
.any_saved_relocs())
2902 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
2904 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2905 // the .got.plt section.
2906 Symbol
* sym
= this->global_offset_table_
;
2909 uint64_t data_size
= this->got_plt_
->current_data_size();
2910 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
2913 if (parameters
->doing_static_link()
2914 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
2916 // If linking statically, make sure that the __rela_iplt symbols
2917 // were defined if necessary, even if we didn't create a PLT.
2918 static const Define_symbol_in_segment syms
[] =
2921 "__rela_iplt_start", // name
2922 elfcpp::PT_LOAD
, // segment_type
2923 elfcpp::PF_W
, // segment_flags_set
2924 elfcpp::PF(0), // segment_flags_clear
2927 elfcpp::STT_NOTYPE
, // type
2928 elfcpp::STB_GLOBAL
, // binding
2929 elfcpp::STV_HIDDEN
, // visibility
2931 Symbol::SEGMENT_START
, // offset_from_base
2935 "__rela_iplt_end", // name
2936 elfcpp::PT_LOAD
, // segment_type
2937 elfcpp::PF_W
, // segment_flags_set
2938 elfcpp::PF(0), // segment_flags_clear
2941 elfcpp::STT_NOTYPE
, // type
2942 elfcpp::STB_GLOBAL
, // binding
2943 elfcpp::STV_HIDDEN
, // visibility
2945 Symbol::SEGMENT_START
, // offset_from_base
2950 symtab
->define_symbols(layout
, 2, syms
,
2951 layout
->script_options()->saw_sections_clause());
2955 // Perform a relocation.
2959 Target_x86_64
<size
>::Relocate::relocate(
2960 const Relocate_info
<size
, false>* relinfo
,
2961 Target_x86_64
<size
>* target
,
2964 const elfcpp::Rela
<size
, false>& rela
,
2965 unsigned int r_type
,
2966 const Sized_symbol
<size
>* gsym
,
2967 const Symbol_value
<size
>* psymval
,
2968 unsigned char* view
,
2969 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
2970 section_size_type view_size
)
2972 if (this->skip_call_tls_get_addr_
)
2974 if ((r_type
!= elfcpp::R_X86_64_PLT32
2975 && r_type
!= elfcpp::R_X86_64_PC32
)
2977 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
2979 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2980 _("missing expected TLS relocation"));
2984 this->skip_call_tls_get_addr_
= false;
2989 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
2991 // Pick the value to use for symbols defined in the PLT.
2992 Symbol_value
<size
> symval
;
2994 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
2996 symval
.set_output_value(target
->plt_address_for_global(gsym
)
2997 + gsym
->plt_offset());
3000 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
3002 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3003 if (object
->local_has_plt_offset(r_sym
))
3005 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
)
3006 + object
->local_plt_offset(r_sym
));
3011 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3013 // Get the GOT offset if needed.
3014 // The GOT pointer points to the end of the GOT section.
3015 // We need to subtract the size of the GOT section to get
3016 // the actual offset to use in the relocation.
3017 bool have_got_offset
= false;
3018 unsigned int got_offset
= 0;
3021 case elfcpp::R_X86_64_GOT32
:
3022 case elfcpp::R_X86_64_GOT64
:
3023 case elfcpp::R_X86_64_GOTPLT64
:
3024 case elfcpp::R_X86_64_GOTPCREL
:
3025 case elfcpp::R_X86_64_GOTPCREL64
:
3028 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3029 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
3033 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3034 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3035 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
3036 - target
->got_size());
3038 have_got_offset
= true;
3047 case elfcpp::R_X86_64_NONE
:
3048 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3049 case elfcpp::R_X86_64_GNU_VTENTRY
:
3052 case elfcpp::R_X86_64_64
:
3053 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
, addend
);
3056 case elfcpp::R_X86_64_PC64
:
3057 Relocate_functions
<size
, false>::pcrela64(view
, object
, psymval
, addend
,
3061 case elfcpp::R_X86_64_32
:
3062 // FIXME: we need to verify that value + addend fits into 32 bits:
3063 // uint64_t x = value + addend;
3064 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3065 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
3066 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3069 case elfcpp::R_X86_64_32S
:
3070 // FIXME: we need to verify that value + addend fits into 32 bits:
3071 // int64_t x = value + addend; // note this quantity is signed!
3072 // x == static_cast<int64_t>(static_cast<int32_t>(x))
3073 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3076 case elfcpp::R_X86_64_PC32
:
3077 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3081 case elfcpp::R_X86_64_16
:
3082 Relocate_functions
<size
, false>::rela16(view
, object
, psymval
, addend
);
3085 case elfcpp::R_X86_64_PC16
:
3086 Relocate_functions
<size
, false>::pcrela16(view
, object
, psymval
, addend
,
3090 case elfcpp::R_X86_64_8
:
3091 Relocate_functions
<size
, false>::rela8(view
, object
, psymval
, addend
);
3094 case elfcpp::R_X86_64_PC8
:
3095 Relocate_functions
<size
, false>::pcrela8(view
, object
, psymval
, addend
,
3099 case elfcpp::R_X86_64_PLT32
:
3100 gold_assert(gsym
== NULL
3101 || gsym
->has_plt_offset()
3102 || gsym
->final_value_is_known()
3103 || (gsym
->is_defined()
3104 && !gsym
->is_from_dynobj()
3105 && !gsym
->is_preemptible()));
3106 // Note: while this code looks the same as for R_X86_64_PC32, it
3107 // behaves differently because psymval was set to point to
3108 // the PLT entry, rather than the symbol, in Scan::global().
3109 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3113 case elfcpp::R_X86_64_PLTOFF64
:
3116 gold_assert(gsym
->has_plt_offset()
3117 || gsym
->final_value_is_known());
3118 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
;
3119 got_address
= target
->got_section(NULL
, NULL
)->address();
3120 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
,
3121 addend
- got_address
);
3124 case elfcpp::R_X86_64_GOT32
:
3125 gold_assert(have_got_offset
);
3126 Relocate_functions
<size
, false>::rela32(view
, got_offset
, addend
);
3129 case elfcpp::R_X86_64_GOTPC32
:
3132 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3133 value
= target
->got_plt_section()->address();
3134 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3138 case elfcpp::R_X86_64_GOT64
:
3139 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
3140 // Since we always add a PLT entry, this is equivalent.
3141 case elfcpp::R_X86_64_GOTPLT64
:
3142 gold_assert(have_got_offset
);
3143 Relocate_functions
<size
, false>::rela64(view
, got_offset
, addend
);
3146 case elfcpp::R_X86_64_GOTPC64
:
3149 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3150 value
= target
->got_plt_section()->address();
3151 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3155 case elfcpp::R_X86_64_GOTOFF64
:
3157 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3158 value
= (psymval
->value(object
, 0)
3159 - target
->got_plt_section()->address());
3160 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3164 case elfcpp::R_X86_64_GOTPCREL
:
3166 gold_assert(have_got_offset
);
3167 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3168 value
= target
->got_plt_section()->address() + got_offset
;
3169 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3173 case elfcpp::R_X86_64_GOTPCREL64
:
3175 gold_assert(have_got_offset
);
3176 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3177 value
= target
->got_plt_section()->address() + got_offset
;
3178 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3182 case elfcpp::R_X86_64_COPY
:
3183 case elfcpp::R_X86_64_GLOB_DAT
:
3184 case elfcpp::R_X86_64_JUMP_SLOT
:
3185 case elfcpp::R_X86_64_RELATIVE
:
3186 case elfcpp::R_X86_64_IRELATIVE
:
3187 // These are outstanding tls relocs, which are unexpected when linking
3188 case elfcpp::R_X86_64_TPOFF64
:
3189 case elfcpp::R_X86_64_DTPMOD64
:
3190 case elfcpp::R_X86_64_TLSDESC
:
3191 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3192 _("unexpected reloc %u in object file"),
3196 // These are initial tls relocs, which are expected when linking
3197 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3198 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3199 case elfcpp::R_X86_64_TLSDESC_CALL
:
3200 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3201 case elfcpp::R_X86_64_DTPOFF32
:
3202 case elfcpp::R_X86_64_DTPOFF64
:
3203 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3204 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3205 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
3206 view
, address
, view_size
);
3209 case elfcpp::R_X86_64_SIZE32
:
3210 case elfcpp::R_X86_64_SIZE64
:
3212 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3213 _("unsupported reloc %u"),
3221 // Perform a TLS relocation.
3225 Target_x86_64
<size
>::Relocate::relocate_tls(
3226 const Relocate_info
<size
, false>* relinfo
,
3227 Target_x86_64
<size
>* target
,
3229 const elfcpp::Rela
<size
, false>& rela
,
3230 unsigned int r_type
,
3231 const Sized_symbol
<size
>* gsym
,
3232 const Symbol_value
<size
>* psymval
,
3233 unsigned char* view
,
3234 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3235 section_size_type view_size
)
3237 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
3239 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3240 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3241 elfcpp::Shdr
<size
, false> data_shdr(relinfo
->data_shdr
);
3242 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
3244 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
3246 const bool is_final
= (gsym
== NULL
3247 ? !parameters
->options().shared()
3248 : gsym
->final_value_is_known());
3249 tls::Tls_optimization optimized_type
3250 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3253 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3254 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3256 // If this code sequence is used in a non-executable section,
3257 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3258 // on the assumption that it's being used by itself in a debug
3259 // section. Therefore, in the unlikely event that the code
3260 // sequence appears in a non-executable section, we simply
3261 // leave it unoptimized.
3262 optimized_type
= tls::TLSOPT_NONE
;
3264 if (optimized_type
== tls::TLSOPT_TO_LE
)
3266 if (tls_segment
== NULL
)
3268 gold_assert(parameters
->errors()->error_count() > 0
3269 || issue_undefined_symbol_error(gsym
));
3272 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
3273 rela
, r_type
, value
, view
,
3279 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3280 ? GOT_TYPE_TLS_OFFSET
3281 : GOT_TYPE_TLS_PAIR
);
3282 unsigned int got_offset
;
3285 gold_assert(gsym
->has_got_offset(got_type
));
3286 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
3290 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3291 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3292 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
3293 - target
->got_size());
3295 if (optimized_type
== tls::TLSOPT_TO_IE
)
3297 value
= target
->got_plt_section()->address() + got_offset
;
3298 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3299 value
, view
, address
, view_size
);
3302 else if (optimized_type
== tls::TLSOPT_NONE
)
3304 // Relocate the field with the offset of the pair of GOT
3306 value
= target
->got_plt_section()->address() + got_offset
;
3307 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3312 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3313 _("unsupported reloc %u"), r_type
);
3316 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3317 case elfcpp::R_X86_64_TLSDESC_CALL
:
3318 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3320 // See above comment for R_X86_64_TLSGD.
3321 optimized_type
= tls::TLSOPT_NONE
;
3323 if (optimized_type
== tls::TLSOPT_TO_LE
)
3325 if (tls_segment
== NULL
)
3327 gold_assert(parameters
->errors()->error_count() > 0
3328 || issue_undefined_symbol_error(gsym
));
3331 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
3332 rela
, r_type
, value
, view
,
3338 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3339 ? GOT_TYPE_TLS_OFFSET
3340 : GOT_TYPE_TLS_DESC
);
3341 unsigned int got_offset
= 0;
3342 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
3343 && optimized_type
== tls::TLSOPT_NONE
)
3345 // We created GOT entries in the .got.tlsdesc portion of
3346 // the .got.plt section, but the offset stored in the
3347 // symbol is the offset within .got.tlsdesc.
3348 got_offset
= (target
->got_size()
3349 + target
->got_plt_section()->data_size());
3353 gold_assert(gsym
->has_got_offset(got_type
));
3354 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
3358 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3359 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3360 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
3361 - target
->got_size());
3363 if (optimized_type
== tls::TLSOPT_TO_IE
)
3365 if (tls_segment
== NULL
)
3367 gold_assert(parameters
->errors()->error_count() > 0
3368 || issue_undefined_symbol_error(gsym
));
3371 value
= target
->got_plt_section()->address() + got_offset
;
3372 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
3373 rela
, r_type
, value
, view
, address
,
3377 else if (optimized_type
== tls::TLSOPT_NONE
)
3379 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3381 // Relocate the field with the offset of the pair of GOT
3383 value
= target
->got_plt_section()->address() + got_offset
;
3384 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3390 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3391 _("unsupported reloc %u"), r_type
);
3394 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3395 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3397 // See above comment for R_X86_64_TLSGD.
3398 optimized_type
= tls::TLSOPT_NONE
;
3400 if (optimized_type
== tls::TLSOPT_TO_LE
)
3402 if (tls_segment
== NULL
)
3404 gold_assert(parameters
->errors()->error_count() > 0
3405 || issue_undefined_symbol_error(gsym
));
3408 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3409 value
, view
, view_size
);
3412 else if (optimized_type
== tls::TLSOPT_NONE
)
3414 // Relocate the field with the offset of the GOT entry for
3415 // the module index.
3416 unsigned int got_offset
;
3417 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
3418 - target
->got_size());
3419 value
= target
->got_plt_section()->address() + got_offset
;
3420 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3424 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3425 _("unsupported reloc %u"), r_type
);
3428 case elfcpp::R_X86_64_DTPOFF32
:
3429 // This relocation type is used in debugging information.
3430 // In that case we need to not optimize the value. If the
3431 // section is not executable, then we assume we should not
3432 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3433 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3435 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3437 if (tls_segment
== NULL
)
3439 gold_assert(parameters
->errors()->error_count() > 0
3440 || issue_undefined_symbol_error(gsym
));
3443 value
-= tls_segment
->memsz();
3445 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3448 case elfcpp::R_X86_64_DTPOFF64
:
3449 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3450 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3452 if (tls_segment
== NULL
)
3454 gold_assert(parameters
->errors()->error_count() > 0
3455 || issue_undefined_symbol_error(gsym
));
3458 value
-= tls_segment
->memsz();
3460 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3463 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3464 if (optimized_type
== tls::TLSOPT_TO_LE
)
3466 if (tls_segment
== NULL
)
3468 gold_assert(parameters
->errors()->error_count() > 0
3469 || issue_undefined_symbol_error(gsym
));
3472 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
3474 r_type
, value
, view
,
3478 else if (optimized_type
== tls::TLSOPT_NONE
)
3480 // Relocate the field with the offset of the GOT entry for
3481 // the tp-relative offset of the symbol.
3482 unsigned int got_offset
;
3485 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
3486 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
3487 - target
->got_size());
3491 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3492 gold_assert(object
->local_has_got_offset(r_sym
,
3493 GOT_TYPE_TLS_OFFSET
));
3494 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
3495 - target
->got_size());
3497 value
= target
->got_plt_section()->address() + got_offset
;
3498 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3502 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3503 _("unsupported reloc type %u"),
3507 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3508 if (tls_segment
== NULL
)
3510 gold_assert(parameters
->errors()->error_count() > 0
3511 || issue_undefined_symbol_error(gsym
));
3514 value
-= tls_segment
->memsz();
3515 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3520 // Do a relocation in which we convert a TLS General-Dynamic to an
3525 Target_x86_64
<size
>::Relocate::tls_gd_to_ie(
3526 const Relocate_info
<size
, false>* relinfo
,
3529 const elfcpp::Rela
<size
, false>& rela
,
3531 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3532 unsigned char* view
,
3533 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3534 section_size_type view_size
)
3536 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3537 // .word 0x6666; rex64; call __tls_get_addr
3538 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3540 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
3541 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3543 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3544 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3545 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3546 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3548 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3550 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3551 Relocate_functions
<size
, false>::pcrela32(view
+ 8, value
, addend
- 8,
3554 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3556 this->skip_call_tls_get_addr_
= true;
3559 // Do a relocation in which we convert a TLS General-Dynamic to a
3564 Target_x86_64
<size
>::Relocate::tls_gd_to_le(
3565 const Relocate_info
<size
, false>* relinfo
,
3567 Output_segment
* tls_segment
,
3568 const elfcpp::Rela
<size
, false>& rela
,
3570 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3571 unsigned char* view
,
3572 section_size_type view_size
)
3574 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3575 // .word 0x6666; rex64; call __tls_get_addr
3576 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3578 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
3579 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3581 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3582 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3583 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3584 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3586 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
3588 value
-= tls_segment
->memsz();
3589 Relocate_functions
<size
, false>::rela32(view
+ 8, value
, 0);
3591 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3593 this->skip_call_tls_get_addr_
= true;
3596 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3600 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_ie(
3601 const Relocate_info
<size
, false>* relinfo
,
3604 const elfcpp::Rela
<size
, false>& rela
,
3605 unsigned int r_type
,
3606 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3607 unsigned char* view
,
3608 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3609 section_size_type view_size
)
3611 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3613 // leaq foo@tlsdesc(%rip), %rax
3614 // ==> movq foo@gottpoff(%rip), %rax
3615 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3616 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3617 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3618 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
3620 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3621 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3625 // call *foo@tlscall(%rax)
3627 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
3628 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
3629 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3630 view
[0] == 0xff && view
[1] == 0x10);
3636 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3640 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_le(
3641 const Relocate_info
<size
, false>* relinfo
,
3643 Output_segment
* tls_segment
,
3644 const elfcpp::Rela
<size
, false>& rela
,
3645 unsigned int r_type
,
3646 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3647 unsigned char* view
,
3648 section_size_type view_size
)
3650 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3652 // leaq foo@tlsdesc(%rip), %rax
3653 // ==> movq foo@tpoff, %rax
3654 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3655 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3656 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3657 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
3660 value
-= tls_segment
->memsz();
3661 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
3665 // call *foo@tlscall(%rax)
3667 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
3668 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
3669 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3670 view
[0] == 0xff && view
[1] == 0x10);
3678 Target_x86_64
<size
>::Relocate::tls_ld_to_le(
3679 const Relocate_info
<size
, false>* relinfo
,
3682 const elfcpp::Rela
<size
, false>& rela
,
3684 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
3685 unsigned char* view
,
3686 section_size_type view_size
)
3688 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3689 // ... leq foo@dtpoff(%rax),%reg
3690 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3692 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3693 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
3695 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3696 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
3698 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
3700 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3702 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3704 this->skip_call_tls_get_addr_
= true;
3707 // Do a relocation in which we convert a TLS Initial-Exec to a
3712 Target_x86_64
<size
>::Relocate::tls_ie_to_le(
3713 const Relocate_info
<size
, false>* relinfo
,
3715 Output_segment
* tls_segment
,
3716 const elfcpp::Rela
<size
, false>& rela
,
3718 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3719 unsigned char* view
,
3720 section_size_type view_size
)
3722 // We need to examine the opcodes to figure out which instruction we
3725 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
3726 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
3728 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3729 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3731 unsigned char op1
= view
[-3];
3732 unsigned char op2
= view
[-2];
3733 unsigned char op3
= view
[-1];
3734 unsigned char reg
= op3
>> 3;
3742 view
[-1] = 0xc0 | reg
;
3746 // Special handling for %rsp.
3750 view
[-1] = 0xc0 | reg
;
3758 view
[-1] = 0x80 | reg
| (reg
<< 3);
3761 value
-= tls_segment
->memsz();
3762 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
3765 // Relocate section data.
3769 Target_x86_64
<size
>::relocate_section(
3770 const Relocate_info
<size
, false>* relinfo
,
3771 unsigned int sh_type
,
3772 const unsigned char* prelocs
,
3774 Output_section
* output_section
,
3775 bool needs_special_offset_handling
,
3776 unsigned char* view
,
3777 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3778 section_size_type view_size
,
3779 const Reloc_symbol_changes
* reloc_symbol_changes
)
3781 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3783 gold::relocate_section
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
3784 typename Target_x86_64
<size
>::Relocate
>(
3790 needs_special_offset_handling
,
3794 reloc_symbol_changes
);
3797 // Apply an incremental relocation. Incremental relocations always refer
3798 // to global symbols.
3802 Target_x86_64
<size
>::apply_relocation(
3803 const Relocate_info
<size
, false>* relinfo
,
3804 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
3805 unsigned int r_type
,
3806 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
3808 unsigned char* view
,
3809 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3810 section_size_type view_size
)
3812 gold::apply_relocation
<size
, false, Target_x86_64
<size
>,
3813 typename Target_x86_64
<size
>::Relocate
>(
3825 // Return the size of a relocation while scanning during a relocatable
3830 Target_x86_64
<size
>::Relocatable_size_for_reloc::get_size_for_reloc(
3831 unsigned int r_type
,
3836 case elfcpp::R_X86_64_NONE
:
3837 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3838 case elfcpp::R_X86_64_GNU_VTENTRY
:
3839 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3840 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3841 case elfcpp::R_X86_64_TLSDESC_CALL
:
3842 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3843 case elfcpp::R_X86_64_DTPOFF32
:
3844 case elfcpp::R_X86_64_DTPOFF64
:
3845 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3846 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3849 case elfcpp::R_X86_64_64
:
3850 case elfcpp::R_X86_64_PC64
:
3851 case elfcpp::R_X86_64_GOTOFF64
:
3852 case elfcpp::R_X86_64_GOTPC64
:
3853 case elfcpp::R_X86_64_PLTOFF64
:
3854 case elfcpp::R_X86_64_GOT64
:
3855 case elfcpp::R_X86_64_GOTPCREL64
:
3856 case elfcpp::R_X86_64_GOTPCREL
:
3857 case elfcpp::R_X86_64_GOTPLT64
:
3860 case elfcpp::R_X86_64_32
:
3861 case elfcpp::R_X86_64_32S
:
3862 case elfcpp::R_X86_64_PC32
:
3863 case elfcpp::R_X86_64_PLT32
:
3864 case elfcpp::R_X86_64_GOTPC32
:
3865 case elfcpp::R_X86_64_GOT32
:
3868 case elfcpp::R_X86_64_16
:
3869 case elfcpp::R_X86_64_PC16
:
3872 case elfcpp::R_X86_64_8
:
3873 case elfcpp::R_X86_64_PC8
:
3876 case elfcpp::R_X86_64_COPY
:
3877 case elfcpp::R_X86_64_GLOB_DAT
:
3878 case elfcpp::R_X86_64_JUMP_SLOT
:
3879 case elfcpp::R_X86_64_RELATIVE
:
3880 case elfcpp::R_X86_64_IRELATIVE
:
3881 // These are outstanding tls relocs, which are unexpected when linking
3882 case elfcpp::R_X86_64_TPOFF64
:
3883 case elfcpp::R_X86_64_DTPMOD64
:
3884 case elfcpp::R_X86_64_TLSDESC
:
3885 object
->error(_("unexpected reloc %u in object file"), r_type
);
3888 case elfcpp::R_X86_64_SIZE32
:
3889 case elfcpp::R_X86_64_SIZE64
:
3891 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
3896 // Scan the relocs during a relocatable link.
3900 Target_x86_64
<size
>::scan_relocatable_relocs(
3901 Symbol_table
* symtab
,
3903 Sized_relobj_file
<size
, false>* object
,
3904 unsigned int data_shndx
,
3905 unsigned int sh_type
,
3906 const unsigned char* prelocs
,
3908 Output_section
* output_section
,
3909 bool needs_special_offset_handling
,
3910 size_t local_symbol_count
,
3911 const unsigned char* plocal_symbols
,
3912 Relocatable_relocs
* rr
)
3914 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3916 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
3917 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
3919 gold::scan_relocatable_relocs
<size
, false, elfcpp::SHT_RELA
,
3920 Scan_relocatable_relocs
>(
3928 needs_special_offset_handling
,
3934 // Relocate a section during a relocatable link.
3938 Target_x86_64
<size
>::relocate_for_relocatable(
3939 const Relocate_info
<size
, false>* relinfo
,
3940 unsigned int sh_type
,
3941 const unsigned char* prelocs
,
3943 Output_section
* output_section
,
3944 off_t offset_in_output_section
,
3945 const Relocatable_relocs
* rr
,
3946 unsigned char* view
,
3947 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
3948 section_size_type view_size
,
3949 unsigned char* reloc_view
,
3950 section_size_type reloc_view_size
)
3952 gold_assert(sh_type
== elfcpp::SHT_RELA
);
3954 gold::relocate_for_relocatable
<size
, false, elfcpp::SHT_RELA
>(
3959 offset_in_output_section
,
3968 // Return the value to use for a dynamic which requires special
3969 // treatment. This is how we support equality comparisons of function
3970 // pointers across shared library boundaries, as described in the
3971 // processor specific ABI supplement.
3975 Target_x86_64
<size
>::do_dynsym_value(const Symbol
* gsym
) const
3977 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
3978 return this->plt_address_for_global(gsym
) + gsym
->plt_offset();
3981 // Return a string used to fill a code section with nops to take up
3982 // the specified length.
3986 Target_x86_64
<size
>::do_code_fill(section_size_type length
) const
3990 // Build a jmpq instruction to skip over the bytes.
3991 unsigned char jmp
[5];
3993 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
3994 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
3995 + std::string(length
- 5, static_cast<char>(0x90)));
3998 // Nop sequences of various lengths.
3999 const char nop1
[1] = { '\x90' }; // nop
4000 const char nop2
[2] = { '\x66', '\x90' }; // xchg %ax %ax
4001 const char nop3
[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4002 const char nop4
[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4004 const char nop5
[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4006 const char nop6
[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4007 '\x44', '\x00', '\x00' };
4008 const char nop7
[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4009 '\x00', '\x00', '\x00',
4011 const char nop8
[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4012 '\x00', '\x00', '\x00',
4014 const char nop9
[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4015 '\x84', '\x00', '\x00',
4016 '\x00', '\x00', '\x00' };
4017 const char nop10
[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4018 '\x1f', '\x84', '\x00',
4019 '\x00', '\x00', '\x00',
4021 const char nop11
[11] = { '\x66', '\x66', '\x2e', // data16
4022 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4023 '\x00', '\x00', '\x00',
4025 const char nop12
[12] = { '\x66', '\x66', '\x66', // data16; data16
4026 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4027 '\x84', '\x00', '\x00',
4028 '\x00', '\x00', '\x00' };
4029 const char nop13
[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4030 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4031 '\x1f', '\x84', '\x00',
4032 '\x00', '\x00', '\x00',
4034 const char nop14
[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4035 '\x66', '\x66', '\x2e', // data16
4036 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4037 '\x00', '\x00', '\x00',
4039 const char nop15
[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4040 '\x66', '\x66', '\x66', // data16; data16
4041 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4042 '\x84', '\x00', '\x00',
4043 '\x00', '\x00', '\x00' };
4045 const char* nops
[16] = {
4047 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
4048 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
4051 return std::string(nops
[length
], length
);
4054 // Return the addend to use for a target specific relocation. The
4055 // only target specific relocation is R_X86_64_TLSDESC for a local
4056 // symbol. We want to set the addend is the offset of the local
4057 // symbol in the TLS segment.
4061 Target_x86_64
<size
>::do_reloc_addend(void* arg
, unsigned int r_type
,
4064 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
4065 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
4066 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
4067 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
4068 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
4069 gold_assert(psymval
->is_tls_symbol());
4070 // The value of a TLS symbol is the offset in the TLS segment.
4071 return psymval
->value(ti
.object
, 0);
4074 // Return the value to use for the base of a DW_EH_PE_datarel offset
4075 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4076 // assembler can not write out the difference between two labels in
4077 // different sections, so instead of using a pc-relative value they
4078 // use an offset from the GOT.
4082 Target_x86_64
<size
>::do_ehframe_datarel_base() const
4084 gold_assert(this->global_offset_table_
!= NULL
);
4085 Symbol
* sym
= this->global_offset_table_
;
4086 Sized_symbol
<size
>* ssym
= static_cast<Sized_symbol
<size
>*>(sym
);
4087 return ssym
->value();
4090 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4091 // compiled with -fsplit-stack. The function calls non-split-stack
4092 // code. We have to change the function so that it always ensures
4093 // that it has enough stack space to run some random function.
4097 Target_x86_64
<size
>::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
4098 section_offset_type fnoffset
,
4099 section_size_type fnsize
,
4100 unsigned char* view
,
4101 section_size_type view_size
,
4103 std::string
* to
) const
4105 // The function starts with a comparison of the stack pointer and a
4106 // field in the TCB. This is followed by a jump.
4109 if (this->match_view(view
, view_size
, fnoffset
, "\x64\x48\x3b\x24\x25", 5)
4112 // We will call __morestack if the carry flag is set after this
4113 // comparison. We turn the comparison into an stc instruction
4115 view
[fnoffset
] = '\xf9';
4116 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 8);
4118 // lea NN(%rsp),%r10
4119 // lea NN(%rsp),%r11
4120 else if ((this->match_view(view
, view_size
, fnoffset
,
4121 "\x4c\x8d\x94\x24", 4)
4122 || this->match_view(view
, view_size
, fnoffset
,
4123 "\x4c\x8d\x9c\x24", 4))
4126 // This is loading an offset from the stack pointer for a
4127 // comparison. The offset is negative, so we decrease the
4128 // offset by the amount of space we need for the stack. This
4129 // means we will avoid calling __morestack if there happens to
4130 // be plenty of space on the stack already.
4131 unsigned char* pval
= view
+ fnoffset
+ 4;
4132 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
4133 val
-= parameters
->options().split_stack_adjust_size();
4134 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
4138 if (!object
->has_no_split_stack())
4139 object
->error(_("failed to match split-stack sequence at "
4140 "section %u offset %0zx"),
4141 shndx
, static_cast<size_t>(fnoffset
));
4145 // We have to change the function so that it calls
4146 // __morestack_non_split instead of __morestack. The former will
4147 // allocate additional stack space.
4148 *from
= "__morestack";
4149 *to
= "__morestack_non_split";
4152 // The selector for x86_64 object files.
4155 class Target_selector_x86_64
: public Target_selector_freebsd
4158 Target_selector_x86_64()
4159 : Target_selector_freebsd(elfcpp::EM_X86_64
, size
, false,
4161 ? "elf64-x86-64" : "elf32-x86-64"),
4163 ? "elf64-x86-64-freebsd"
4164 : "elf32-x86-64-freebsd"),
4165 (size
== 64 ? "elf_x86_64" : "elf32_x86_64"))
4169 do_instantiate_target()
4170 { return new Target_x86_64
<size
>(); }
4174 Target_selector_x86_64
<64> target_selector_x86_64
;
4175 Target_selector_x86_64
<32> target_selector_x32
;
4177 } // End anonymous namespace.