1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
45 class Output_data_plt_x86_64
;
47 // The x86_64 target class.
49 // http://www.x86-64.org/documentation/abi.pdf
50 // TLS info comes from
51 // http://people.redhat.com/drepper/tls.pdf
52 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
54 class Target_x86_64
: public Sized_target
<64, false>
57 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
58 // uses only Elf64_Rela relocation entries with explicit addends."
59 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
62 : Sized_target
<64, false>(&x86_64_info
),
63 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rela_dyn_(NULL
),
64 copy_relocs_(NULL
), dynbss_(NULL
), got_mod_index_offset_(-1U)
67 // Scan the relocations to look for symbol adjustments.
69 scan_relocs(const General_options
& options
,
72 Sized_relobj
<64, false>* object
,
73 unsigned int data_shndx
,
75 const unsigned char* prelocs
,
77 Output_section
* output_section
,
78 bool needs_special_offset_handling
,
79 size_t local_symbol_count
,
80 const unsigned char* plocal_symbols
);
82 // Finalize the sections.
84 do_finalize_sections(Layout
*);
86 // Return the value to use for a dynamic which requires special
89 do_dynsym_value(const Symbol
*) const;
91 // Relocate a section.
93 relocate_section(const Relocate_info
<64, false>*,
95 const unsigned char* prelocs
,
97 Output_section
* output_section
,
98 bool needs_special_offset_handling
,
100 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
101 section_size_type view_size
);
103 // Scan the relocs during a relocatable link.
105 scan_relocatable_relocs(const General_options
& options
,
106 Symbol_table
* symtab
,
108 Sized_relobj
<64, false>* object
,
109 unsigned int data_shndx
,
110 unsigned int sh_type
,
111 const unsigned char* prelocs
,
113 Output_section
* output_section
,
114 bool needs_special_offset_handling
,
115 size_t local_symbol_count
,
116 const unsigned char* plocal_symbols
,
117 Relocatable_relocs
*);
119 // Relocate a section during a relocatable link.
121 relocate_for_relocatable(const Relocate_info
<64, false>*,
122 unsigned int sh_type
,
123 const unsigned char* prelocs
,
125 Output_section
* output_section
,
126 off_t offset_in_output_section
,
127 const Relocatable_relocs
*,
129 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
130 section_size_type view_size
,
131 unsigned char* reloc_view
,
132 section_size_type reloc_view_size
);
134 // Return a string used to fill a code section with nops.
136 do_code_fill(section_size_type length
) const;
138 // Return whether SYM is defined by the ABI.
140 do_is_defined_by_abi(Symbol
* sym
) const
141 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
143 // Return the size of the GOT section.
147 gold_assert(this->got_
!= NULL
);
148 return this->got_
->data_size();
152 // The class which scans relocations.
157 : issued_non_pic_error_(false)
161 local(const General_options
& options
, Symbol_table
* symtab
,
162 Layout
* layout
, Target_x86_64
* target
,
163 Sized_relobj
<64, false>* object
,
164 unsigned int data_shndx
,
165 Output_section
* output_section
,
166 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
167 const elfcpp::Sym
<64, false>& lsym
);
170 global(const General_options
& options
, Symbol_table
* symtab
,
171 Layout
* layout
, Target_x86_64
* target
,
172 Sized_relobj
<64, false>* object
,
173 unsigned int data_shndx
,
174 Output_section
* output_section
,
175 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
180 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
183 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
187 check_non_pic(Relobj
*, unsigned int r_type
);
189 // Whether we have issued an error about a non-PIC compilation.
190 bool issued_non_pic_error_
;
193 // The class which implements relocation.
198 : skip_call_tls_get_addr_(false)
203 if (this->skip_call_tls_get_addr_
)
205 // FIXME: This needs to specify the location somehow.
206 gold_error(_("missing expected TLS relocation"));
210 // Do a relocation. Return false if the caller should not issue
211 // any warnings about this relocation.
213 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, size_t relnum
,
214 const elfcpp::Rela
<64, false>&,
215 unsigned int r_type
, const Sized_symbol
<64>*,
216 const Symbol_value
<64>*,
217 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
221 // Do a TLS relocation.
223 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
224 size_t relnum
, const elfcpp::Rela
<64, false>&,
225 unsigned int r_type
, const Sized_symbol
<64>*,
226 const Symbol_value
<64>*,
227 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
230 // Do a TLS General-Dynamic to Initial-Exec transition.
232 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
233 Output_segment
* tls_segment
,
234 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
235 elfcpp::Elf_types
<64>::Elf_Addr value
,
237 elfcpp::Elf_types
<64>::Elf_Addr
,
238 section_size_type view_size
);
240 // Do a TLS General-Dynamic to Local-Exec transition.
242 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
243 Output_segment
* tls_segment
,
244 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
245 elfcpp::Elf_types
<64>::Elf_Addr value
,
247 section_size_type view_size
);
249 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
251 tls_desc_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
252 Output_segment
* tls_segment
,
253 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
254 elfcpp::Elf_types
<64>::Elf_Addr value
,
256 elfcpp::Elf_types
<64>::Elf_Addr
,
257 section_size_type view_size
);
259 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
261 tls_desc_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
262 Output_segment
* tls_segment
,
263 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
264 elfcpp::Elf_types
<64>::Elf_Addr value
,
266 section_size_type view_size
);
268 // Do a TLS Local-Dynamic to Local-Exec transition.
270 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
271 Output_segment
* tls_segment
,
272 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
273 elfcpp::Elf_types
<64>::Elf_Addr value
,
275 section_size_type view_size
);
277 // Do a TLS Initial-Exec to Local-Exec transition.
279 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
280 Output_segment
* tls_segment
,
281 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
282 elfcpp::Elf_types
<64>::Elf_Addr value
,
284 section_size_type view_size
);
286 // This is set if we should skip the next reloc, which should be a
287 // PLT32 reloc against ___tls_get_addr.
288 bool skip_call_tls_get_addr_
;
291 // A class which returns the size required for a relocation type,
292 // used while scanning relocs during a relocatable link.
293 class Relocatable_size_for_reloc
297 get_size_for_reloc(unsigned int, Relobj
*);
300 // Adjust TLS relocation type based on the options and whether this
301 // is a local symbol.
302 static tls::Tls_optimization
303 optimize_tls_reloc(bool is_final
, int r_type
);
305 // Get the GOT section, creating it if necessary.
306 Output_data_got
<64, false>*
307 got_section(Symbol_table
*, Layout
*);
309 // Get the GOT PLT section.
311 got_plt_section() const
313 gold_assert(this->got_plt_
!= NULL
);
314 return this->got_plt_
;
317 // Create the PLT section.
319 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
321 // Create a PLT entry for a global symbol.
323 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
325 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
327 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
329 // Create a GOT entry for the TLS module index.
331 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
332 Sized_relobj
<64, false>* object
);
334 // Get the PLT section.
335 Output_data_plt_x86_64
*
338 gold_assert(this->plt_
!= NULL
);
342 // Get the dynamic reloc section, creating it if necessary.
344 rela_dyn_section(Layout
*);
346 // Return true if the symbol may need a COPY relocation.
347 // References from an executable object to non-function symbols
348 // defined in a dynamic object may need a COPY relocation.
350 may_need_copy_reloc(Symbol
* gsym
)
352 return (!parameters
->options().shared()
353 && gsym
->is_from_dynobj()
354 && gsym
->type() != elfcpp::STT_FUNC
);
357 // Copy a relocation against a global symbol.
359 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
360 Sized_relobj
<64, false>*, unsigned int,
361 Output_section
*, Symbol
*, const elfcpp::Rela
<64, false>&);
363 // Information about this specific target which we pass to the
364 // general Target structure.
365 static const Target::Target_info x86_64_info
;
369 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
370 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
371 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
372 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
376 Output_data_got
<64, false>* got_
;
378 Output_data_plt_x86_64
* plt_
;
379 // The GOT PLT section.
380 Output_data_space
* got_plt_
;
381 // The dynamic reloc section.
382 Reloc_section
* rela_dyn_
;
383 // Relocs saved to avoid a COPY reloc.
384 Copy_relocs
<64, false>* copy_relocs_
;
385 // Space for variables copied with a COPY reloc.
386 Output_data_space
* dynbss_
;
387 // Offset of the GOT entry for the TLS module index.
388 unsigned int got_mod_index_offset_
;
391 const Target::Target_info
Target_x86_64::x86_64_info
=
394 false, // is_big_endian
395 elfcpp::EM_X86_64
, // machine_code
396 false, // has_make_symbol
397 false, // has_resolve
398 true, // has_code_fill
399 true, // is_default_stack_executable
401 "/lib/ld64.so.1", // program interpreter
402 0x400000, // default_text_segment_address
403 0x1000, // abi_pagesize (overridable by -z max-page-size)
404 0x1000 // common_pagesize (overridable by -z common-page-size)
407 // Get the GOT section, creating it if necessary.
409 Output_data_got
<64, false>*
410 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
412 if (this->got_
== NULL
)
414 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
416 this->got_
= new Output_data_got
<64, false>();
418 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
419 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
422 // The old GNU linker creates a .got.plt section. We just
423 // create another set of data in the .got section. Note that we
424 // always create a PLT if we create a GOT, although the PLT
426 this->got_plt_
= new Output_data_space(8);
427 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
428 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
431 // The first three entries are reserved.
432 this->got_plt_
->set_current_data_size(3 * 8);
434 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
435 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
437 0, 0, elfcpp::STT_OBJECT
,
439 elfcpp::STV_HIDDEN
, 0,
446 // Get the dynamic reloc section, creating it if necessary.
448 Target_x86_64::Reloc_section
*
449 Target_x86_64::rela_dyn_section(Layout
* layout
)
451 if (this->rela_dyn_
== NULL
)
453 gold_assert(layout
!= NULL
);
454 this->rela_dyn_
= new Reloc_section();
455 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
456 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
458 return this->rela_dyn_
;
461 // A class to handle the PLT data.
463 class Output_data_plt_x86_64
: public Output_section_data
466 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
468 Output_data_plt_x86_64(Layout
*, Output_data_got
<64, false>*,
471 // Add an entry to the PLT.
473 add_entry(Symbol
* gsym
);
475 // Add the reserved TLSDESC_PLT entry to the PLT.
477 reserve_tlsdesc_entry(unsigned int got_offset
)
478 { this->tlsdesc_got_offset_
= got_offset
; }
480 // Return true if a TLSDESC_PLT entry has been reserved.
482 has_tlsdesc_entry() const
483 { return this->tlsdesc_got_offset_
!= -1U; }
485 // Return the GOT offset for the reserved TLSDESC_PLT entry.
487 get_tlsdesc_got_offset() const
488 { return this->tlsdesc_got_offset_
; }
490 // Return the offset of the reserved TLSDESC_PLT entry.
492 get_tlsdesc_plt_offset() const
493 { return (this->count_
+ 1) * plt_entry_size
; }
495 // Return the .rel.plt section data.
498 { return this->rel_
; }
502 do_adjust_output_section(Output_section
* os
);
505 // The size of an entry in the PLT.
506 static const int plt_entry_size
= 16;
508 // The first entry in the PLT.
509 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
510 // procedure linkage table for both programs and shared objects."
511 static unsigned char first_plt_entry
[plt_entry_size
];
513 // Other entries in the PLT for an executable.
514 static unsigned char plt_entry
[plt_entry_size
];
516 // The reserved TLSDESC entry in the PLT for an executable.
517 static unsigned char tlsdesc_plt_entry
[plt_entry_size
];
519 // Set the final size.
521 set_final_data_size();
523 // Write out the PLT data.
525 do_write(Output_file
*);
527 // The reloc section.
530 Output_data_got
<64, false>* got_
;
531 // The .got.plt section.
532 Output_data_space
* got_plt_
;
533 // The number of PLT entries.
535 // Offset of the reserved TLSDESC_GOT entry when needed.
536 unsigned int tlsdesc_got_offset_
;
539 // Create the PLT section. The ordinary .got section is an argument,
540 // since we need to refer to the start. We also create our own .got
541 // section just for PLT entries.
543 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
544 Output_data_got
<64, false>* got
,
545 Output_data_space
* got_plt
)
546 : Output_section_data(8), got_(got
), got_plt_(got_plt
), count_(0),
547 tlsdesc_got_offset_(-1U)
549 this->rel_
= new Reloc_section();
550 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
551 elfcpp::SHF_ALLOC
, this->rel_
);
555 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
557 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
558 // linker, and so do we.
562 // Add an entry to the PLT.
565 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
567 gold_assert(!gsym
->has_plt_offset());
569 // Note that when setting the PLT offset we skip the initial
570 // reserved PLT entry.
571 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
575 section_offset_type got_offset
= this->got_plt_
->current_data_size();
577 // Every PLT entry needs a GOT entry which points back to the PLT
578 // entry (this will be changed by the dynamic linker, normally
579 // lazily when the function is called).
580 this->got_plt_
->set_current_data_size(got_offset
+ 8);
582 // Every PLT entry needs a reloc.
583 gsym
->set_needs_dynsym_entry();
584 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
587 // Note that we don't need to save the symbol. The contents of the
588 // PLT are independent of which symbols are used. The symbols only
589 // appear in the relocations.
592 // Set the final size.
594 Output_data_plt_x86_64::set_final_data_size()
596 unsigned int count
= this->count_
;
597 if (this->has_tlsdesc_entry())
599 this->set_data_size((count
+ 1) * plt_entry_size
);
602 // The first entry in the PLT for an executable.
604 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
606 // From AMD64 ABI Draft 0.98, page 76
607 0xff, 0x35, // pushq contents of memory address
608 0, 0, 0, 0, // replaced with address of .got + 8
609 0xff, 0x25, // jmp indirect
610 0, 0, 0, 0, // replaced with address of .got + 16
611 0x90, 0x90, 0x90, 0x90 // noop (x4)
614 // Subsequent entries in the PLT for an executable.
616 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
618 // From AMD64 ABI Draft 0.98, page 76
619 0xff, 0x25, // jmpq indirect
620 0, 0, 0, 0, // replaced with address of symbol in .got
621 0x68, // pushq immediate
622 0, 0, 0, 0, // replaced with offset into relocation table
623 0xe9, // jmpq relative
624 0, 0, 0, 0 // replaced with offset to start of .plt
627 // The reserved TLSDESC entry in the PLT for an executable.
629 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry
[plt_entry_size
] =
631 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
632 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
633 0xff, 0x35, // pushq x(%rip)
634 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
635 0xff, 0x25, // jmpq *y(%rip)
636 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
641 // Write out the PLT. This uses the hand-coded instructions above,
642 // and adjusts them as needed. This is specified by the AMD64 ABI.
645 Output_data_plt_x86_64::do_write(Output_file
* of
)
647 const off_t offset
= this->offset();
648 const section_size_type oview_size
=
649 convert_to_section_size_type(this->data_size());
650 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
652 const off_t got_file_offset
= this->got_plt_
->offset();
653 const section_size_type got_size
=
654 convert_to_section_size_type(this->got_plt_
->data_size());
655 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
658 unsigned char* pov
= oview
;
660 // The base address of the .plt section.
661 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
662 // The base address of the .got section.
663 elfcpp::Elf_types
<32>::Elf_Addr got_base
= this->got_
->address();
664 // The base address of the PLT portion of the .got section,
665 // which is where the GOT pointer will point, and where the
666 // three reserved GOT entries are located.
667 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
669 memcpy(pov
, first_plt_entry
, plt_entry_size
);
670 // We do a jmp relative to the PC at the end of this instruction.
671 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 8
672 - (plt_address
+ 6));
673 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 16
674 - (plt_address
+ 12));
675 pov
+= plt_entry_size
;
677 unsigned char* got_pov
= got_view
;
679 memset(got_pov
, 0, 24);
682 unsigned int plt_offset
= plt_entry_size
;
683 unsigned int got_offset
= 24;
684 const unsigned int count
= this->count_
;
685 for (unsigned int plt_index
= 0;
688 pov
+= plt_entry_size
,
690 plt_offset
+= plt_entry_size
,
693 // Set and adjust the PLT entry itself.
694 memcpy(pov
, plt_entry
, plt_entry_size
);
695 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
696 (got_address
+ got_offset
697 - (plt_address
+ plt_offset
700 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
701 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
702 - (plt_offset
+ plt_entry_size
));
704 // Set the entry in the GOT.
705 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
708 if (this->has_tlsdesc_entry())
710 // Set and adjust the reserved TLSDESC PLT entry.
711 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
712 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
713 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
715 - (plt_address
+ plt_offset
717 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
720 - (plt_address
+ plt_offset
722 pov
+= plt_entry_size
;
725 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
726 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
728 of
->write_output_view(offset
, oview_size
, oview
);
729 of
->write_output_view(got_file_offset
, got_size
, got_view
);
732 // Create the PLT section.
735 Target_x86_64::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
737 if (this->plt_
== NULL
)
739 // Create the GOT sections first.
740 this->got_section(symtab
, layout
);
742 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_
,
744 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
746 | elfcpp::SHF_EXECINSTR
),
751 // Create a PLT entry for a global symbol.
754 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
757 if (gsym
->has_plt_offset())
760 if (this->plt_
== NULL
)
761 this->make_plt_section(symtab
, layout
);
763 this->plt_
->add_entry(gsym
);
766 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
769 Target_x86_64::reserve_tlsdesc_entries(Symbol_table
* symtab
,
772 if (this->plt_
== NULL
)
773 this->make_plt_section(symtab
, layout
);
775 if (!this->plt_
->has_tlsdesc_entry())
777 // Allocate the TLSDESC_GOT entry.
778 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
779 unsigned int got_offset
= got
->add_constant(0);
781 // Allocate the TLSDESC_PLT entry.
782 this->plt_
->reserve_tlsdesc_entry(got_offset
);
786 // Create a GOT entry for the TLS module index.
789 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
790 Sized_relobj
<64, false>* object
)
792 if (this->got_mod_index_offset_
== -1U)
794 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
795 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
796 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
797 unsigned int got_offset
= got
->add_constant(0);
798 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
800 got
->add_constant(0);
801 this->got_mod_index_offset_
= got_offset
;
803 return this->got_mod_index_offset_
;
806 // Handle a relocation against a non-function symbol defined in a
807 // dynamic object. The traditional way to handle this is to generate
808 // a COPY relocation to copy the variable at runtime from the shared
809 // object into the executable's data segment. However, this is
810 // undesirable in general, as if the size of the object changes in the
811 // dynamic object, the executable will no longer work correctly. If
812 // this relocation is in a writable section, then we can create a
813 // dynamic reloc and the dynamic linker will resolve it to the correct
814 // address at runtime. However, we do not want do that if the
815 // relocation is in a read-only section, as it would prevent the
816 // readonly segment from being shared. And if we have to eventually
817 // generate a COPY reloc, then any dynamic relocations will be
818 // useless. So this means that if this is a writable section, we need
819 // to save the relocation until we see whether we have to create a
820 // COPY relocation for this symbol for any other relocation.
823 Target_x86_64::copy_reloc(const General_options
* options
,
824 Symbol_table
* symtab
,
826 Sized_relobj
<64, false>* object
,
827 unsigned int data_shndx
,
828 Output_section
* output_section
,
830 const elfcpp::Rela
<64, false>& rela
)
832 Sized_symbol
<64>* ssym
= symtab
->get_sized_symbol
<64>(gsym
);
834 if (!Copy_relocs
<64, false>::need_copy_reloc(options
, object
,
837 // So far we do not need a COPY reloc. Save this relocation.
838 // If it turns out that we never need a COPY reloc for this
839 // symbol, then we will emit the relocation.
840 if (this->copy_relocs_
== NULL
)
841 this->copy_relocs_
= new Copy_relocs
<64, false>();
842 this->copy_relocs_
->save(ssym
, object
, data_shndx
, output_section
, rela
);
846 // Allocate space for this symbol in the .bss section.
848 elfcpp::Elf_types
<64>::Elf_WXword symsize
= ssym
->symsize();
850 // There is no defined way to determine the required alignment
851 // of the symbol. We pick the alignment based on the size. We
852 // set an arbitrary maximum of 256.
854 for (align
= 1; align
< 512; align
<<= 1)
855 if ((symsize
& align
) != 0)
858 if (this->dynbss_
== NULL
)
860 this->dynbss_
= new Output_data_space(align
);
861 layout
->add_output_section_data(".bss",
864 | elfcpp::SHF_WRITE
),
868 Output_data_space
* dynbss
= this->dynbss_
;
870 if (align
> dynbss
->addralign())
871 dynbss
->set_space_alignment(align
);
873 section_size_type dynbss_size
= dynbss
->current_data_size();
874 dynbss_size
= align_address(dynbss_size
, align
);
875 section_size_type offset
= dynbss_size
;
876 dynbss
->set_current_data_size(dynbss_size
+ symsize
);
878 symtab
->define_with_copy_reloc(ssym
, dynbss
, offset
);
880 // Add the COPY reloc.
881 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
882 rela_dyn
->add_global(ssym
, elfcpp::R_X86_64_COPY
, dynbss
, offset
, 0);
887 // Optimize the TLS relocation type based on what we know about the
888 // symbol. IS_FINAL is true if the final address of this symbol is
889 // known at link time.
891 tls::Tls_optimization
892 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
894 // If we are generating a shared library, then we can't do anything
896 if (parameters
->options().shared())
897 return tls::TLSOPT_NONE
;
901 case elfcpp::R_X86_64_TLSGD
:
902 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
903 case elfcpp::R_X86_64_TLSDESC_CALL
:
904 // These are General-Dynamic which permits fully general TLS
905 // access. Since we know that we are generating an executable,
906 // we can convert this to Initial-Exec. If we also know that
907 // this is a local symbol, we can further switch to Local-Exec.
909 return tls::TLSOPT_TO_LE
;
910 return tls::TLSOPT_TO_IE
;
912 case elfcpp::R_X86_64_TLSLD
:
913 // This is Local-Dynamic, which refers to a local symbol in the
914 // dynamic TLS block. Since we know that we generating an
915 // executable, we can switch to Local-Exec.
916 return tls::TLSOPT_TO_LE
;
918 case elfcpp::R_X86_64_DTPOFF32
:
919 case elfcpp::R_X86_64_DTPOFF64
:
920 // Another Local-Dynamic reloc.
921 return tls::TLSOPT_TO_LE
;
923 case elfcpp::R_X86_64_GOTTPOFF
:
924 // These are Initial-Exec relocs which get the thread offset
925 // from the GOT. If we know that we are linking against the
926 // local symbol, we can switch to Local-Exec, which links the
927 // thread offset into the instruction.
929 return tls::TLSOPT_TO_LE
;
930 return tls::TLSOPT_NONE
;
932 case elfcpp::R_X86_64_TPOFF32
:
933 // When we already have Local-Exec, there is nothing further we
935 return tls::TLSOPT_NONE
;
942 // Report an unsupported relocation against a local symbol.
945 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
948 gold_error(_("%s: unsupported reloc %u against local symbol"),
949 object
->name().c_str(), r_type
);
952 // We are about to emit a dynamic relocation of type R_TYPE. If the
953 // dynamic linker does not support it, issue an error. The GNU linker
954 // only issues a non-PIC error for an allocated read-only section.
955 // Here we know the section is allocated, but we don't know that it is
956 // read-only. But we check for all the relocation types which the
957 // glibc dynamic linker supports, so it seems appropriate to issue an
958 // error even if the section is not read-only.
961 Target_x86_64::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
)
965 // These are the relocation types supported by glibc for x86_64.
966 case elfcpp::R_X86_64_RELATIVE
:
967 case elfcpp::R_X86_64_GLOB_DAT
:
968 case elfcpp::R_X86_64_JUMP_SLOT
:
969 case elfcpp::R_X86_64_DTPMOD64
:
970 case elfcpp::R_X86_64_DTPOFF64
:
971 case elfcpp::R_X86_64_TPOFF64
:
972 case elfcpp::R_X86_64_64
:
973 case elfcpp::R_X86_64_32
:
974 case elfcpp::R_X86_64_PC32
:
975 case elfcpp::R_X86_64_COPY
:
979 // This prevents us from issuing more than one error per reloc
980 // section. But we can still wind up issuing more than one
981 // error per object file.
982 if (this->issued_non_pic_error_
)
984 object
->error(_("requires unsupported dynamic reloc; "
985 "recompile with -fPIC"));
986 this->issued_non_pic_error_
= true;
989 case elfcpp::R_X86_64_NONE
:
994 // Scan a relocation for a local symbol.
997 Target_x86_64::Scan::local(const General_options
&,
998 Symbol_table
* symtab
,
1000 Target_x86_64
* target
,
1001 Sized_relobj
<64, false>* object
,
1002 unsigned int data_shndx
,
1003 Output_section
* output_section
,
1004 const elfcpp::Rela
<64, false>& reloc
,
1005 unsigned int r_type
,
1006 const elfcpp::Sym
<64, false>& lsym
)
1010 case elfcpp::R_X86_64_NONE
:
1011 case elfcpp::R_386_GNU_VTINHERIT
:
1012 case elfcpp::R_386_GNU_VTENTRY
:
1015 case elfcpp::R_X86_64_64
:
1016 // If building a shared library (or a position-independent
1017 // executable), we need to create a dynamic relocation for this
1018 // location. The relocation applied at link time will apply the
1019 // link-time value, so we flag the location with an
1020 // R_X86_64_RELATIVE relocation so the dynamic loader can
1021 // relocate it easily.
1022 if (parameters
->options().output_is_position_independent())
1024 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1025 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1026 rela_dyn
->add_local_relative(object
, r_sym
,
1027 elfcpp::R_X86_64_RELATIVE
,
1028 output_section
, data_shndx
,
1029 reloc
.get_r_offset(),
1030 reloc
.get_r_addend());
1034 case elfcpp::R_X86_64_32
:
1035 case elfcpp::R_X86_64_32S
:
1036 case elfcpp::R_X86_64_16
:
1037 case elfcpp::R_X86_64_8
:
1038 // If building a shared library (or a position-independent
1039 // executable), we need to create a dynamic relocation for this
1040 // location. We can't use an R_X86_64_RELATIVE relocation
1041 // because that is always a 64-bit relocation.
1042 if (parameters
->options().output_is_position_independent())
1044 this->check_non_pic(object
, r_type
);
1046 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1047 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1049 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1050 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1051 data_shndx
, reloc
.get_r_offset(),
1052 reloc
.get_r_addend());
1056 gold_assert(lsym
.get_st_value() == 0);
1057 rela_dyn
->add_local_section(object
, lsym
.get_st_shndx(),
1058 r_type
, output_section
,
1059 data_shndx
, reloc
.get_r_offset(),
1060 reloc
.get_r_addend());
1065 case elfcpp::R_X86_64_PC64
:
1066 case elfcpp::R_X86_64_PC32
:
1067 case elfcpp::R_X86_64_PC16
:
1068 case elfcpp::R_X86_64_PC8
:
1071 case elfcpp::R_X86_64_PLT32
:
1072 // Since we know this is a local symbol, we can handle this as a
1076 case elfcpp::R_X86_64_GOTPC32
:
1077 case elfcpp::R_X86_64_GOTOFF64
:
1078 case elfcpp::R_X86_64_GOTPC64
:
1079 case elfcpp::R_X86_64_PLTOFF64
:
1080 // We need a GOT section.
1081 target
->got_section(symtab
, layout
);
1082 // For PLTOFF64, we'd normally want a PLT section, but since we
1083 // know this is a local symbol, no PLT is needed.
1086 case elfcpp::R_X86_64_GOT64
:
1087 case elfcpp::R_X86_64_GOT32
:
1088 case elfcpp::R_X86_64_GOTPCREL64
:
1089 case elfcpp::R_X86_64_GOTPCREL
:
1090 case elfcpp::R_X86_64_GOTPLT64
:
1092 // The symbol requires a GOT entry.
1093 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1094 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1095 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
1097 // If we are generating a shared object, we need to add a
1098 // dynamic relocation for this symbol's GOT entry.
1099 if (parameters
->options().output_is_position_independent())
1101 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1102 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1103 if (r_type
!= elfcpp::R_X86_64_GOT32
)
1104 rela_dyn
->add_local_relative(
1105 object
, r_sym
, elfcpp::R_X86_64_RELATIVE
, got
,
1106 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1109 this->check_non_pic(object
, r_type
);
1111 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1112 rela_dyn
->add_local(
1113 object
, r_sym
, r_type
, got
,
1114 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1118 // For GOTPLT64, we'd normally want a PLT section, but since
1119 // we know this is a local symbol, no PLT is needed.
1123 case elfcpp::R_X86_64_COPY
:
1124 case elfcpp::R_X86_64_GLOB_DAT
:
1125 case elfcpp::R_X86_64_JUMP_SLOT
:
1126 case elfcpp::R_X86_64_RELATIVE
:
1127 // These are outstanding tls relocs, which are unexpected when linking
1128 case elfcpp::R_X86_64_TPOFF64
:
1129 case elfcpp::R_X86_64_DTPMOD64
:
1130 case elfcpp::R_X86_64_TLSDESC
:
1131 gold_error(_("%s: unexpected reloc %u in object file"),
1132 object
->name().c_str(), r_type
);
1135 // These are initial tls relocs, which are expected when linking
1136 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1137 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1138 case elfcpp::R_X86_64_TLSDESC_CALL
:
1139 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1140 case elfcpp::R_X86_64_DTPOFF32
:
1141 case elfcpp::R_X86_64_DTPOFF64
:
1142 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1143 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1145 bool output_is_shared
= parameters
->options().shared();
1146 const tls::Tls_optimization optimized_type
1147 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
1150 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1151 if (optimized_type
== tls::TLSOPT_NONE
)
1153 // Create a pair of GOT entries for the module index and
1154 // dtv-relative offset.
1155 Output_data_got
<64, false>* got
1156 = target
->got_section(symtab
, layout
);
1157 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1158 got
->add_local_pair_with_rela(object
, r_sym
,
1159 lsym
.get_st_shndx(),
1161 target
->rela_dyn_section(layout
),
1162 elfcpp::R_X86_64_DTPMOD64
, 0);
1164 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1165 unsupported_reloc_local(object
, r_type
);
1168 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1169 if (optimized_type
== tls::TLSOPT_NONE
)
1171 // Create reserved PLT and GOT entries for the resolver.
1172 target
->reserve_tlsdesc_entries(symtab
, layout
);
1174 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1175 Output_data_got
<64, false>* got
1176 = target
->got_section(symtab
, layout
);
1177 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1178 got
->add_local_pair_with_rela(object
, r_sym
,
1179 lsym
.get_st_shndx(),
1181 target
->rela_dyn_section(layout
),
1182 elfcpp::R_X86_64_TLSDESC
, 0);
1184 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1185 unsupported_reloc_local(object
, r_type
);
1188 case elfcpp::R_X86_64_TLSDESC_CALL
:
1191 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1192 if (optimized_type
== tls::TLSOPT_NONE
)
1194 // Create a GOT entry for the module index.
1195 target
->got_mod_index_entry(symtab
, layout
, object
);
1197 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1198 unsupported_reloc_local(object
, r_type
);
1201 case elfcpp::R_X86_64_DTPOFF32
:
1202 case elfcpp::R_X86_64_DTPOFF64
:
1205 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1206 layout
->set_has_static_tls();
1207 if (optimized_type
== tls::TLSOPT_NONE
)
1209 // Create a GOT entry for the tp-relative offset.
1210 Output_data_got
<64, false>* got
1211 = target
->got_section(symtab
, layout
);
1212 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1213 got
->add_local_with_rela(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1214 target
->rela_dyn_section(layout
),
1215 elfcpp::R_X86_64_TPOFF64
);
1217 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1218 unsupported_reloc_local(object
, r_type
);
1221 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1222 layout
->set_has_static_tls();
1223 if (output_is_shared
)
1224 unsupported_reloc_local(object
, r_type
);
1233 case elfcpp::R_X86_64_SIZE32
:
1234 case elfcpp::R_X86_64_SIZE64
:
1236 gold_error(_("%s: unsupported reloc %u against local symbol"),
1237 object
->name().c_str(), r_type
);
1243 // Report an unsupported relocation against a global symbol.
1246 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
1247 unsigned int r_type
,
1250 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1251 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1254 // Scan a relocation for a global symbol.
1257 Target_x86_64::Scan::global(const General_options
& options
,
1258 Symbol_table
* symtab
,
1260 Target_x86_64
* target
,
1261 Sized_relobj
<64, false>* object
,
1262 unsigned int data_shndx
,
1263 Output_section
* output_section
,
1264 const elfcpp::Rela
<64, false>& reloc
,
1265 unsigned int r_type
,
1270 case elfcpp::R_X86_64_NONE
:
1271 case elfcpp::R_386_GNU_VTINHERIT
:
1272 case elfcpp::R_386_GNU_VTENTRY
:
1275 case elfcpp::R_X86_64_64
:
1276 case elfcpp::R_X86_64_32
:
1277 case elfcpp::R_X86_64_32S
:
1278 case elfcpp::R_X86_64_16
:
1279 case elfcpp::R_X86_64_8
:
1281 // Make a PLT entry if necessary.
1282 if (gsym
->needs_plt_entry())
1284 target
->make_plt_entry(symtab
, layout
, gsym
);
1285 // Since this is not a PC-relative relocation, we may be
1286 // taking the address of a function. In that case we need to
1287 // set the entry in the dynamic symbol table to the address of
1289 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1290 gsym
->set_needs_dynsym_value();
1292 // Make a dynamic relocation if necessary.
1293 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1295 if (target
->may_need_copy_reloc(gsym
))
1297 target
->copy_reloc(&options
, symtab
, layout
, object
,
1298 data_shndx
, output_section
, gsym
, reloc
);
1300 else if (r_type
== elfcpp::R_X86_64_64
1301 && gsym
->can_use_relative_reloc(false))
1303 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1304 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1305 output_section
, object
,
1306 data_shndx
, reloc
.get_r_offset(),
1307 reloc
.get_r_addend());
1311 this->check_non_pic(object
, r_type
);
1312 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1313 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1314 data_shndx
, reloc
.get_r_offset(),
1315 reloc
.get_r_addend());
1321 case elfcpp::R_X86_64_PC64
:
1322 case elfcpp::R_X86_64_PC32
:
1323 case elfcpp::R_X86_64_PC16
:
1324 case elfcpp::R_X86_64_PC8
:
1326 // Make a PLT entry if necessary.
1327 if (gsym
->needs_plt_entry())
1328 target
->make_plt_entry(symtab
, layout
, gsym
);
1329 // Make a dynamic relocation if necessary.
1330 int flags
= Symbol::NON_PIC_REF
;
1331 if (gsym
->type() == elfcpp::STT_FUNC
)
1332 flags
|= Symbol::FUNCTION_CALL
;
1333 if (gsym
->needs_dynamic_reloc(flags
))
1335 if (target
->may_need_copy_reloc(gsym
))
1337 target
->copy_reloc(&options
, symtab
, layout
, object
,
1338 data_shndx
, output_section
, gsym
, reloc
);
1342 this->check_non_pic(object
, r_type
);
1343 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1344 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1345 data_shndx
, reloc
.get_r_offset(),
1346 reloc
.get_r_addend());
1352 case elfcpp::R_X86_64_GOT64
:
1353 case elfcpp::R_X86_64_GOT32
:
1354 case elfcpp::R_X86_64_GOTPCREL64
:
1355 case elfcpp::R_X86_64_GOTPCREL
:
1356 case elfcpp::R_X86_64_GOTPLT64
:
1358 // The symbol requires a GOT entry.
1359 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1360 if (gsym
->final_value_is_known())
1361 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1364 // If this symbol is not fully resolved, we need to add a
1365 // dynamic relocation for it.
1366 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1367 if (gsym
->is_from_dynobj()
1368 || gsym
->is_undefined()
1369 || gsym
->is_preemptible())
1370 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1371 elfcpp::R_X86_64_GLOB_DAT
);
1374 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1375 rela_dyn
->add_global_relative(
1376 gsym
, elfcpp::R_X86_64_RELATIVE
, got
,
1377 gsym
->got_offset(GOT_TYPE_STANDARD
), 0);
1380 // For GOTPLT64, we also need a PLT entry (but only if the
1381 // symbol is not fully resolved).
1382 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1383 && !gsym
->final_value_is_known())
1384 target
->make_plt_entry(symtab
, layout
, gsym
);
1388 case elfcpp::R_X86_64_PLT32
:
1389 // If the symbol is fully resolved, this is just a PC32 reloc.
1390 // Otherwise we need a PLT entry.
1391 if (gsym
->final_value_is_known())
1393 // If building a shared library, we can also skip the PLT entry
1394 // if the symbol is defined in the output file and is protected
1396 if (gsym
->is_defined()
1397 && !gsym
->is_from_dynobj()
1398 && !gsym
->is_preemptible())
1400 target
->make_plt_entry(symtab
, layout
, gsym
);
1403 case elfcpp::R_X86_64_GOTPC32
:
1404 case elfcpp::R_X86_64_GOTOFF64
:
1405 case elfcpp::R_X86_64_GOTPC64
:
1406 case elfcpp::R_X86_64_PLTOFF64
:
1407 // We need a GOT section.
1408 target
->got_section(symtab
, layout
);
1409 // For PLTOFF64, we also need a PLT entry (but only if the
1410 // symbol is not fully resolved).
1411 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1412 && !gsym
->final_value_is_known())
1413 target
->make_plt_entry(symtab
, layout
, gsym
);
1416 case elfcpp::R_X86_64_COPY
:
1417 case elfcpp::R_X86_64_GLOB_DAT
:
1418 case elfcpp::R_X86_64_JUMP_SLOT
:
1419 case elfcpp::R_X86_64_RELATIVE
:
1420 // These are outstanding tls relocs, which are unexpected when linking
1421 case elfcpp::R_X86_64_TPOFF64
:
1422 case elfcpp::R_X86_64_DTPMOD64
:
1423 case elfcpp::R_X86_64_TLSDESC
:
1424 gold_error(_("%s: unexpected reloc %u in object file"),
1425 object
->name().c_str(), r_type
);
1428 // These are initial tls relocs, which are expected for global()
1429 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1430 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1431 case elfcpp::R_X86_64_TLSDESC_CALL
:
1432 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1433 case elfcpp::R_X86_64_DTPOFF32
:
1434 case elfcpp::R_X86_64_DTPOFF64
:
1435 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1436 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1438 const bool is_final
= gsym
->final_value_is_known();
1439 const tls::Tls_optimization optimized_type
1440 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1443 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1444 if (optimized_type
== tls::TLSOPT_NONE
)
1446 // Create a pair of GOT entries for the module index and
1447 // dtv-relative offset.
1448 Output_data_got
<64, false>* got
1449 = target
->got_section(symtab
, layout
);
1450 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_PAIR
,
1451 target
->rela_dyn_section(layout
),
1452 elfcpp::R_X86_64_DTPMOD64
,
1453 elfcpp::R_X86_64_DTPOFF64
);
1455 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1457 // Create a GOT entry for the tp-relative offset.
1458 Output_data_got
<64, false>* got
1459 = target
->got_section(symtab
, layout
);
1460 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1461 target
->rela_dyn_section(layout
),
1462 elfcpp::R_X86_64_TPOFF64
);
1464 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1465 unsupported_reloc_global(object
, r_type
, gsym
);
1468 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1469 if (optimized_type
== tls::TLSOPT_NONE
)
1471 // Create reserved PLT and GOT entries for the resolver.
1472 target
->reserve_tlsdesc_entries(symtab
, layout
);
1474 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1475 Output_data_got
<64, false>* got
1476 = target
->got_section(symtab
, layout
);
1477 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_DESC
,
1478 target
->rela_dyn_section(layout
),
1479 elfcpp::R_X86_64_TLSDESC
, 0);
1481 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1483 // Create a GOT entry for the tp-relative offset.
1484 Output_data_got
<64, false>* got
1485 = target
->got_section(symtab
, layout
);
1486 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1487 target
->rela_dyn_section(layout
),
1488 elfcpp::R_X86_64_TPOFF64
);
1490 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1491 unsupported_reloc_global(object
, r_type
, gsym
);
1494 case elfcpp::R_X86_64_TLSDESC_CALL
:
1497 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1498 if (optimized_type
== tls::TLSOPT_NONE
)
1500 // Create a GOT entry for the module index.
1501 target
->got_mod_index_entry(symtab
, layout
, object
);
1503 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1504 unsupported_reloc_global(object
, r_type
, gsym
);
1507 case elfcpp::R_X86_64_DTPOFF32
:
1508 case elfcpp::R_X86_64_DTPOFF64
:
1511 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1512 layout
->set_has_static_tls();
1513 if (optimized_type
== tls::TLSOPT_NONE
)
1515 // Create a GOT entry for the tp-relative offset.
1516 Output_data_got
<64, false>* got
1517 = target
->got_section(symtab
, layout
);
1518 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1519 target
->rela_dyn_section(layout
),
1520 elfcpp::R_X86_64_TPOFF64
);
1522 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1523 unsupported_reloc_global(object
, r_type
, gsym
);
1526 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1527 layout
->set_has_static_tls();
1528 if (parameters
->options().shared())
1529 unsupported_reloc_local(object
, r_type
);
1538 case elfcpp::R_X86_64_SIZE32
:
1539 case elfcpp::R_X86_64_SIZE64
:
1541 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1542 object
->name().c_str(), r_type
,
1543 gsym
->demangled_name().c_str());
1548 // Scan relocations for a section.
1551 Target_x86_64::scan_relocs(const General_options
& options
,
1552 Symbol_table
* symtab
,
1554 Sized_relobj
<64, false>* object
,
1555 unsigned int data_shndx
,
1556 unsigned int sh_type
,
1557 const unsigned char* prelocs
,
1559 Output_section
* output_section
,
1560 bool needs_special_offset_handling
,
1561 size_t local_symbol_count
,
1562 const unsigned char* plocal_symbols
)
1564 if (sh_type
== elfcpp::SHT_REL
)
1566 gold_error(_("%s: unsupported REL reloc section"),
1567 object
->name().c_str());
1571 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1572 Target_x86_64::Scan
>(
1582 needs_special_offset_handling
,
1587 // Finalize the sections.
1590 Target_x86_64::do_finalize_sections(Layout
* layout
)
1592 // Fill in some more dynamic tags.
1593 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1596 if (this->got_plt_
!= NULL
)
1597 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1599 if (this->plt_
!= NULL
)
1601 const Output_data
* od
= this->plt_
->rel_plt();
1602 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1603 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1604 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1605 if (this->plt_
->has_tlsdesc_entry())
1607 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
1608 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
1609 this->got_
->finalize_data_size();
1610 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
1611 this->plt_
, plt_offset
);
1612 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
1613 this->got_
, got_offset
);
1617 if (this->rela_dyn_
!= NULL
)
1619 const Output_data
* od
= this->rela_dyn_
;
1620 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1621 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1622 odyn
->add_constant(elfcpp::DT_RELAENT
,
1623 elfcpp::Elf_sizes
<64>::rela_size
);
1626 if (!parameters
->options().shared())
1628 // The value of the DT_DEBUG tag is filled in by the dynamic
1629 // linker at run time, and used by the debugger.
1630 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1634 // Emit any relocs we saved in an attempt to avoid generating COPY
1636 if (this->copy_relocs_
== NULL
)
1638 if (this->copy_relocs_
->any_to_emit())
1640 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1641 this->copy_relocs_
->emit(rela_dyn
);
1643 delete this->copy_relocs_
;
1644 this->copy_relocs_
= NULL
;
1647 // Perform a relocation.
1650 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1651 Target_x86_64
* target
,
1653 const elfcpp::Rela
<64, false>& rela
,
1654 unsigned int r_type
,
1655 const Sized_symbol
<64>* gsym
,
1656 const Symbol_value
<64>* psymval
,
1657 unsigned char* view
,
1658 elfcpp::Elf_types
<64>::Elf_Addr address
,
1659 section_size_type view_size
)
1661 if (this->skip_call_tls_get_addr_
)
1663 if (r_type
!= elfcpp::R_X86_64_PLT32
1665 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1667 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1668 _("missing expected TLS relocation"));
1672 this->skip_call_tls_get_addr_
= false;
1677 // Pick the value to use for symbols defined in shared objects.
1678 Symbol_value
<64> symval
;
1680 && (gsym
->is_from_dynobj()
1681 || (parameters
->options().shared()
1682 && (gsym
->is_undefined() || gsym
->is_preemptible())))
1683 && gsym
->has_plt_offset())
1685 symval
.set_output_value(target
->plt_section()->address()
1686 + gsym
->plt_offset());
1690 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1691 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1693 // Get the GOT offset if needed.
1694 // The GOT pointer points to the end of the GOT section.
1695 // We need to subtract the size of the GOT section to get
1696 // the actual offset to use in the relocation.
1697 bool have_got_offset
= false;
1698 unsigned int got_offset
= 0;
1701 case elfcpp::R_X86_64_GOT32
:
1702 case elfcpp::R_X86_64_GOT64
:
1703 case elfcpp::R_X86_64_GOTPLT64
:
1704 case elfcpp::R_X86_64_GOTPCREL
:
1705 case elfcpp::R_X86_64_GOTPCREL64
:
1708 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1709 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
1713 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1714 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1715 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1716 - target
->got_size());
1718 have_got_offset
= true;
1727 case elfcpp::R_X86_64_NONE
:
1728 case elfcpp::R_386_GNU_VTINHERIT
:
1729 case elfcpp::R_386_GNU_VTENTRY
:
1732 case elfcpp::R_X86_64_64
:
1733 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1736 case elfcpp::R_X86_64_PC64
:
1737 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1741 case elfcpp::R_X86_64_32
:
1742 // FIXME: we need to verify that value + addend fits into 32 bits:
1743 // uint64_t x = value + addend;
1744 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1745 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1746 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1749 case elfcpp::R_X86_64_32S
:
1750 // FIXME: we need to verify that value + addend fits into 32 bits:
1751 // int64_t x = value + addend; // note this quantity is signed!
1752 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1753 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1756 case elfcpp::R_X86_64_PC32
:
1757 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1761 case elfcpp::R_X86_64_16
:
1762 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1765 case elfcpp::R_X86_64_PC16
:
1766 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1770 case elfcpp::R_X86_64_8
:
1771 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1774 case elfcpp::R_X86_64_PC8
:
1775 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1779 case elfcpp::R_X86_64_PLT32
:
1780 gold_assert(gsym
== NULL
1781 || gsym
->has_plt_offset()
1782 || gsym
->final_value_is_known()
1783 || (gsym
->is_defined()
1784 && !gsym
->is_from_dynobj()
1785 && !gsym
->is_preemptible()));
1786 // Note: while this code looks the same as for R_X86_64_PC32, it
1787 // behaves differently because psymval was set to point to
1788 // the PLT entry, rather than the symbol, in Scan::global().
1789 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1793 case elfcpp::R_X86_64_PLTOFF64
:
1796 gold_assert(gsym
->has_plt_offset()
1797 || gsym
->final_value_is_known());
1798 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
1799 got_address
= target
->got_section(NULL
, NULL
)->address();
1800 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
1801 addend
- got_address
);
1804 case elfcpp::R_X86_64_GOT32
:
1805 gold_assert(have_got_offset
);
1806 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1809 case elfcpp::R_X86_64_GOTPC32
:
1812 elfcpp::Elf_types
<64>::Elf_Addr value
;
1813 value
= target
->got_plt_section()->address();
1814 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1818 case elfcpp::R_X86_64_GOT64
:
1819 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1820 // Since we always add a PLT entry, this is equivalent.
1821 case elfcpp::R_X86_64_GOTPLT64
:
1822 gold_assert(have_got_offset
);
1823 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1826 case elfcpp::R_X86_64_GOTPC64
:
1829 elfcpp::Elf_types
<64>::Elf_Addr value
;
1830 value
= target
->got_plt_section()->address();
1831 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1835 case elfcpp::R_X86_64_GOTOFF64
:
1837 elfcpp::Elf_types
<64>::Elf_Addr value
;
1838 value
= (psymval
->value(object
, 0)
1839 - target
->got_plt_section()->address());
1840 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1844 case elfcpp::R_X86_64_GOTPCREL
:
1846 gold_assert(have_got_offset
);
1847 elfcpp::Elf_types
<64>::Elf_Addr value
;
1848 value
= target
->got_plt_section()->address() + got_offset
;
1849 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1853 case elfcpp::R_X86_64_GOTPCREL64
:
1855 gold_assert(have_got_offset
);
1856 elfcpp::Elf_types
<64>::Elf_Addr value
;
1857 value
= target
->got_plt_section()->address() + got_offset
;
1858 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1862 case elfcpp::R_X86_64_COPY
:
1863 case elfcpp::R_X86_64_GLOB_DAT
:
1864 case elfcpp::R_X86_64_JUMP_SLOT
:
1865 case elfcpp::R_X86_64_RELATIVE
:
1866 // These are outstanding tls relocs, which are unexpected when linking
1867 case elfcpp::R_X86_64_TPOFF64
:
1868 case elfcpp::R_X86_64_DTPMOD64
:
1869 case elfcpp::R_X86_64_TLSDESC
:
1870 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1871 _("unexpected reloc %u in object file"),
1875 // These are initial tls relocs, which are expected when linking
1876 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1877 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1878 case elfcpp::R_X86_64_TLSDESC_CALL
:
1879 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1880 case elfcpp::R_X86_64_DTPOFF32
:
1881 case elfcpp::R_X86_64_DTPOFF64
:
1882 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1883 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1884 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
1885 view
, address
, view_size
);
1888 case elfcpp::R_X86_64_SIZE32
:
1889 case elfcpp::R_X86_64_SIZE64
:
1891 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1892 _("unsupported reloc %u"),
1900 // Perform a TLS relocation.
1903 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1904 Target_x86_64
* target
,
1906 const elfcpp::Rela
<64, false>& rela
,
1907 unsigned int r_type
,
1908 const Sized_symbol
<64>* gsym
,
1909 const Symbol_value
<64>* psymval
,
1910 unsigned char* view
,
1911 elfcpp::Elf_types
<64>::Elf_Addr address
,
1912 section_size_type view_size
)
1914 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1916 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1917 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1919 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1921 const bool is_final
= (gsym
== NULL
1922 ? !parameters
->options().output_is_position_independent()
1923 : gsym
->final_value_is_known());
1924 const tls::Tls_optimization optimized_type
1925 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1928 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1929 if (optimized_type
== tls::TLSOPT_TO_LE
)
1931 gold_assert(tls_segment
!= NULL
);
1932 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1933 rela
, r_type
, value
, view
,
1939 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1940 ? GOT_TYPE_TLS_OFFSET
1941 : GOT_TYPE_TLS_PAIR
);
1942 unsigned int got_offset
;
1945 gold_assert(gsym
->has_got_offset(got_type
));
1946 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1950 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1951 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1952 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1953 - target
->got_size());
1955 if (optimized_type
== tls::TLSOPT_TO_IE
)
1957 gold_assert(tls_segment
!= NULL
);
1958 value
= target
->got_plt_section()->address() + got_offset
;
1959 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1960 value
, view
, address
, view_size
);
1963 else if (optimized_type
== tls::TLSOPT_NONE
)
1965 // Relocate the field with the offset of the pair of GOT
1967 value
= target
->got_plt_section()->address() + got_offset
;
1968 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
1973 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1974 _("unsupported reloc %u"), r_type
);
1977 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1978 case elfcpp::R_X86_64_TLSDESC_CALL
:
1979 if (optimized_type
== tls::TLSOPT_TO_LE
)
1981 gold_assert(tls_segment
!= NULL
);
1982 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
1983 rela
, r_type
, value
, view
,
1989 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1990 ? GOT_TYPE_TLS_OFFSET
1991 : GOT_TYPE_TLS_DESC
);
1992 unsigned int got_offset
;
1995 gold_assert(gsym
->has_got_offset(got_type
));
1996 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2000 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2001 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2002 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2003 - target
->got_size());
2005 if (optimized_type
== tls::TLSOPT_TO_IE
)
2007 gold_assert(tls_segment
!= NULL
);
2008 value
= target
->got_plt_section()->address() + got_offset
;
2009 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
2010 rela
, r_type
, value
, view
, address
,
2014 else if (optimized_type
== tls::TLSOPT_NONE
)
2016 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2018 // Relocate the field with the offset of the pair of GOT
2020 value
= target
->got_plt_section()->address() + got_offset
;
2021 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2027 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2028 _("unsupported reloc %u"), r_type
);
2031 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2032 if (optimized_type
== tls::TLSOPT_TO_LE
)
2034 gold_assert(tls_segment
!= NULL
);
2035 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2036 value
, view
, view_size
);
2039 else if (optimized_type
== tls::TLSOPT_NONE
)
2041 // Relocate the field with the offset of the GOT entry for
2042 // the module index.
2043 unsigned int got_offset
;
2044 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2045 - target
->got_size());
2046 value
= target
->got_plt_section()->address() + got_offset
;
2047 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2051 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2052 _("unsupported reloc %u"), r_type
);
2055 case elfcpp::R_X86_64_DTPOFF32
:
2056 gold_assert(tls_segment
!= NULL
);
2057 if (optimized_type
== tls::TLSOPT_TO_LE
)
2058 value
-= tls_segment
->memsz();
2059 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2062 case elfcpp::R_X86_64_DTPOFF64
:
2063 gold_assert(tls_segment
!= NULL
);
2064 if (optimized_type
== tls::TLSOPT_TO_LE
)
2065 value
-= tls_segment
->memsz();
2066 Relocate_functions
<64, false>::rela64(view
, value
, 0);
2069 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2070 if (optimized_type
== tls::TLSOPT_TO_LE
)
2072 gold_assert(tls_segment
!= NULL
);
2073 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2074 rela
, r_type
, value
, view
,
2078 else if (optimized_type
== tls::TLSOPT_NONE
)
2080 // Relocate the field with the offset of the GOT entry for
2081 // the tp-relative offset of the symbol.
2082 unsigned int got_offset
;
2085 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
2086 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
2087 - target
->got_size());
2091 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2092 gold_assert(object
->local_has_got_offset(r_sym
,
2093 GOT_TYPE_TLS_OFFSET
));
2094 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
2095 - target
->got_size());
2097 value
= target
->got_plt_section()->address() + got_offset
;
2098 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2101 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2102 _("unsupported reloc type %u"),
2106 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2107 value
-= tls_segment
->memsz();
2108 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2113 // Do a relocation in which we convert a TLS General-Dynamic to an
2117 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
2120 const elfcpp::Rela
<64, false>& rela
,
2122 elfcpp::Elf_types
<64>::Elf_Addr value
,
2123 unsigned char* view
,
2124 elfcpp::Elf_types
<64>::Elf_Addr address
,
2125 section_size_type view_size
)
2127 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2128 // .word 0x6666; rex64; call __tls_get_addr
2129 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2131 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2132 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2134 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2135 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2136 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2137 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2139 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2141 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2142 Relocate_functions
<64, false>::pcrela32(view
+ 8, value
, addend
- 8, address
);
2144 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2146 this->skip_call_tls_get_addr_
= true;
2149 // Do a relocation in which we convert a TLS General-Dynamic to a
2153 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
2155 Output_segment
* tls_segment
,
2156 const elfcpp::Rela
<64, false>& rela
,
2158 elfcpp::Elf_types
<64>::Elf_Addr value
,
2159 unsigned char* view
,
2160 section_size_type view_size
)
2162 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2163 // .word 0x6666; rex64; call __tls_get_addr
2164 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2166 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2167 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2169 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2170 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2171 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2172 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2174 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2176 value
-= tls_segment
->memsz();
2177 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
2179 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2181 this->skip_call_tls_get_addr_
= true;
2184 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2187 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2188 const Relocate_info
<64, false>* relinfo
,
2191 const elfcpp::Rela
<64, false>& rela
,
2192 unsigned int r_type
,
2193 elfcpp::Elf_types
<64>::Elf_Addr value
,
2194 unsigned char* view
,
2195 elfcpp::Elf_types
<64>::Elf_Addr address
,
2196 section_size_type view_size
)
2198 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2200 // leaq foo@tlsdesc(%rip), %rax
2201 // ==> movq foo@gottpoff(%rip), %rax
2202 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2203 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2204 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2205 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2207 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2208 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2212 // call *foo@tlscall(%rax)
2214 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2215 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2216 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2217 view
[0] == 0xff && view
[1] == 0x10);
2223 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2226 Target_x86_64::Relocate::tls_desc_gd_to_le(
2227 const Relocate_info
<64, false>* relinfo
,
2229 Output_segment
* tls_segment
,
2230 const elfcpp::Rela
<64, false>& rela
,
2231 unsigned int r_type
,
2232 elfcpp::Elf_types
<64>::Elf_Addr value
,
2233 unsigned char* view
,
2234 section_size_type view_size
)
2236 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2238 // leaq foo@tlsdesc(%rip), %rax
2239 // ==> movq foo@tpoff, %rax
2240 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2241 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2242 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2243 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2246 value
-= tls_segment
->memsz();
2247 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2251 // call *foo@tlscall(%rax)
2253 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2254 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2255 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2256 view
[0] == 0xff && view
[1] == 0x10);
2263 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
2266 const elfcpp::Rela
<64, false>& rela
,
2268 elfcpp::Elf_types
<64>::Elf_Addr
,
2269 unsigned char* view
,
2270 section_size_type view_size
)
2272 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2273 // ... leq foo@dtpoff(%rax),%reg
2274 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2276 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2277 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
2279 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2280 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
2282 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
2284 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2286 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2288 this->skip_call_tls_get_addr_
= true;
2291 // Do a relocation in which we convert a TLS Initial-Exec to a
2295 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
2297 Output_segment
* tls_segment
,
2298 const elfcpp::Rela
<64, false>& rela
,
2300 elfcpp::Elf_types
<64>::Elf_Addr value
,
2301 unsigned char* view
,
2302 section_size_type view_size
)
2304 // We need to examine the opcodes to figure out which instruction we
2307 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2308 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2310 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2311 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2313 unsigned char op1
= view
[-3];
2314 unsigned char op2
= view
[-2];
2315 unsigned char op3
= view
[-1];
2316 unsigned char reg
= op3
>> 3;
2324 view
[-1] = 0xc0 | reg
;
2328 // Special handling for %rsp.
2332 view
[-1] = 0xc0 | reg
;
2340 view
[-1] = 0x80 | reg
| (reg
<< 3);
2343 value
-= tls_segment
->memsz();
2344 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2347 // Relocate section data.
2350 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
2351 unsigned int sh_type
,
2352 const unsigned char* prelocs
,
2354 Output_section
* output_section
,
2355 bool needs_special_offset_handling
,
2356 unsigned char* view
,
2357 elfcpp::Elf_types
<64>::Elf_Addr address
,
2358 section_size_type view_size
)
2360 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2362 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2363 Target_x86_64::Relocate
>(
2369 needs_special_offset_handling
,
2375 // Return the size of a relocation while scanning during a relocatable
2379 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2380 unsigned int r_type
,
2385 case elfcpp::R_X86_64_NONE
:
2386 case elfcpp::R_386_GNU_VTINHERIT
:
2387 case elfcpp::R_386_GNU_VTENTRY
:
2388 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2389 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2390 case elfcpp::R_X86_64_TLSDESC_CALL
:
2391 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2392 case elfcpp::R_X86_64_DTPOFF32
:
2393 case elfcpp::R_X86_64_DTPOFF64
:
2394 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2395 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2398 case elfcpp::R_X86_64_64
:
2399 case elfcpp::R_X86_64_PC64
:
2400 case elfcpp::R_X86_64_GOTOFF64
:
2401 case elfcpp::R_X86_64_GOTPC64
:
2402 case elfcpp::R_X86_64_PLTOFF64
:
2403 case elfcpp::R_X86_64_GOT64
:
2404 case elfcpp::R_X86_64_GOTPCREL64
:
2405 case elfcpp::R_X86_64_GOTPCREL
:
2406 case elfcpp::R_X86_64_GOTPLT64
:
2409 case elfcpp::R_X86_64_32
:
2410 case elfcpp::R_X86_64_32S
:
2411 case elfcpp::R_X86_64_PC32
:
2412 case elfcpp::R_X86_64_PLT32
:
2413 case elfcpp::R_X86_64_GOTPC32
:
2414 case elfcpp::R_X86_64_GOT32
:
2417 case elfcpp::R_X86_64_16
:
2418 case elfcpp::R_X86_64_PC16
:
2421 case elfcpp::R_X86_64_8
:
2422 case elfcpp::R_X86_64_PC8
:
2425 case elfcpp::R_X86_64_COPY
:
2426 case elfcpp::R_X86_64_GLOB_DAT
:
2427 case elfcpp::R_X86_64_JUMP_SLOT
:
2428 case elfcpp::R_X86_64_RELATIVE
:
2429 // These are outstanding tls relocs, which are unexpected when linking
2430 case elfcpp::R_X86_64_TPOFF64
:
2431 case elfcpp::R_X86_64_DTPMOD64
:
2432 case elfcpp::R_X86_64_TLSDESC
:
2433 object
->error(_("unexpected reloc %u in object file"), r_type
);
2436 case elfcpp::R_X86_64_SIZE32
:
2437 case elfcpp::R_X86_64_SIZE64
:
2439 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
2444 // Scan the relocs during a relocatable link.
2447 Target_x86_64::scan_relocatable_relocs(const General_options
& options
,
2448 Symbol_table
* symtab
,
2450 Sized_relobj
<64, false>* object
,
2451 unsigned int data_shndx
,
2452 unsigned int sh_type
,
2453 const unsigned char* prelocs
,
2455 Output_section
* output_section
,
2456 bool needs_special_offset_handling
,
2457 size_t local_symbol_count
,
2458 const unsigned char* plocal_symbols
,
2459 Relocatable_relocs
* rr
)
2461 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2463 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
2464 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2466 gold::scan_relocatable_relocs
<64, false, elfcpp::SHT_RELA
,
2467 Scan_relocatable_relocs
>(
2476 needs_special_offset_handling
,
2482 // Relocate a section during a relocatable link.
2485 Target_x86_64::relocate_for_relocatable(
2486 const Relocate_info
<64, false>* relinfo
,
2487 unsigned int sh_type
,
2488 const unsigned char* prelocs
,
2490 Output_section
* output_section
,
2491 off_t offset_in_output_section
,
2492 const Relocatable_relocs
* rr
,
2493 unsigned char* view
,
2494 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
2495 section_size_type view_size
,
2496 unsigned char* reloc_view
,
2497 section_size_type reloc_view_size
)
2499 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2501 gold::relocate_for_relocatable
<64, false, elfcpp::SHT_RELA
>(
2506 offset_in_output_section
,
2515 // Return the value to use for a dynamic which requires special
2516 // treatment. This is how we support equality comparisons of function
2517 // pointers across shared library boundaries, as described in the
2518 // processor specific ABI supplement.
2521 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
2523 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2524 return this->plt_section()->address() + gsym
->plt_offset();
2527 // Return a string used to fill a code section with nops to take up
2528 // the specified length.
2531 Target_x86_64::do_code_fill(section_size_type length
) const
2535 // Build a jmpq instruction to skip over the bytes.
2536 unsigned char jmp
[5];
2538 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2539 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2540 + std::string(length
- 5, '\0'));
2543 // Nop sequences of various lengths.
2544 const char nop1
[1] = { 0x90 }; // nop
2545 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2546 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2547 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2548 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2549 0x00 }; // leal 0(%esi,1),%esi
2550 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2552 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2554 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2555 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2556 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2557 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2559 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2560 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2562 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2563 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2565 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2566 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2567 0x00, 0x00, 0x00, 0x00 };
2568 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2569 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2570 0x27, 0x00, 0x00, 0x00,
2572 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2573 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2574 0xbc, 0x27, 0x00, 0x00,
2576 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2577 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2578 0x90, 0x90, 0x90, 0x90,
2581 const char* nops
[16] = {
2583 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2584 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2587 return std::string(nops
[length
], length
);
2590 // The selector for x86_64 object files.
2592 class Target_selector_x86_64
: public Target_selector
2595 Target_selector_x86_64()
2596 : Target_selector(elfcpp::EM_X86_64
, 64, false, "elf64-x86-64")
2600 do_instantiate_target()
2601 { return new Target_x86_64(); }
2604 Target_selector_x86_64 target_selector_x86_64
;
2606 } // End anonymous namespace.