1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 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
9 // modify it under the terms of the GNU Library General Public License
10 // as published by the Free Software Foundation; either version 2, or
11 // (at your option) any later version.
13 // In addition to the permissions in the GNU Library General Public
14 // License, the Free Software Foundation gives you unlimited
15 // permission to link the compiled version of this file into
16 // combinations with other programs, and to distribute those
17 // combinations without any restriction coming from the use of this
18 // file. (The Library Public License restrictions do apply in other
19 // respects; for example, they cover modification of the file, and
20 /// distribution when not linked into a combined executable.)
22 // This program is distributed in the hope that it will be useful, but
23 // WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 // Library General Public License for more details.
27 // You should have received a copy of the GNU Library General Public
28 // License along with this program; if not, write to the Free Software
29 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
37 #include "parameters.h"
45 #include "target-reloc.h"
46 #include "target-select.h"
54 class Output_data_plt_x86_64
;
56 // The x86_64 target class.
58 // http://www.x86-64.org/documentation/abi.pdf
59 // TLS info comes from
60 // http://people.redhat.com/drepper/tls.pdf
61 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
63 class Target_x86_64
: public Sized_target
<64, false>
66 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
67 // uses only Elf64_Rela relocation entries with explicit addends."
68 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
71 : Sized_target
<64, false>(&x86_64_info
),
72 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rela_dyn_(NULL
),
73 copy_relocs_(NULL
), dynbss_(NULL
)
76 // Scan the relocations to look for symbol adjustments.
78 scan_relocs(const General_options
& options
,
81 Sized_relobj
<64, false>* object
,
82 unsigned int data_shndx
,
84 const unsigned char* prelocs
,
86 Output_section
* output_section
,
87 bool needs_special_offset_handling
,
88 size_t local_symbol_count
,
89 const unsigned char* plocal_symbols
);
91 // Finalize the sections.
93 do_finalize_sections(Layout
*);
95 // Return the value to use for a dynamic which requires special
98 do_dynsym_value(const Symbol
*) const;
100 // Relocate a section.
102 relocate_section(const Relocate_info
<64, false>*,
103 unsigned int sh_type
,
104 const unsigned char* prelocs
,
106 Output_section
* output_section
,
107 bool needs_special_offset_handling
,
109 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
112 // Return a string used to fill a code section with nops.
114 do_code_fill(off_t length
);
116 // Return whether SYM is defined by the ABI.
118 do_is_defined_by_abi(Symbol
* sym
) const
119 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
121 // Return the size of the GOT section.
125 gold_assert(this->got_
!= NULL
);
126 return this->got_
->data_size();
130 // The class which scans relocations.
134 local(const General_options
& options
, Symbol_table
* symtab
,
135 Layout
* layout
, Target_x86_64
* target
,
136 Sized_relobj
<64, false>* object
,
137 unsigned int data_shndx
,
138 Output_section
* output_section
,
139 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
140 const elfcpp::Sym
<64, false>& lsym
);
143 global(const General_options
& options
, Symbol_table
* symtab
,
144 Layout
* layout
, Target_x86_64
* target
,
145 Sized_relobj
<64, false>* object
,
146 unsigned int data_shndx
,
147 Output_section
* output_section
,
148 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
152 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
155 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
159 // The class which implements relocation.
164 : skip_call_tls_get_addr_(false)
169 if (this->skip_call_tls_get_addr_
)
171 // FIXME: This needs to specify the location somehow.
172 gold_error(_("missing expected TLS relocation"));
176 // Do a relocation. Return false if the caller should not issue
177 // any warnings about this relocation.
179 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, size_t relnum
,
180 const elfcpp::Rela
<64, false>&,
181 unsigned int r_type
, const Sized_symbol
<64>*,
182 const Symbol_value
<64>*,
183 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
187 // Do a TLS relocation.
189 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
190 size_t relnum
, const elfcpp::Rela
<64, false>&,
191 unsigned int r_type
, const Sized_symbol
<64>*,
192 const Symbol_value
<64>*,
193 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
, off_t
);
195 // Do a TLS General-Dynamic to Local-Exec transition.
197 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
198 Output_segment
* tls_segment
,
199 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
200 elfcpp::Elf_types
<64>::Elf_Addr value
,
204 // Do a TLS General-Dynamic to Local-Exec transition.
206 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
207 Output_segment
* tls_segment
,
208 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
209 elfcpp::Elf_types
<64>::Elf_Addr value
,
213 // Do a TLS Local-Dynamic to Local-Exec transition.
215 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
216 Output_segment
* tls_segment
,
217 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
218 elfcpp::Elf_types
<64>::Elf_Addr value
,
222 // Do a TLS Initial-Exec to Local-Exec transition.
224 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
225 Output_segment
* tls_segment
,
226 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
227 elfcpp::Elf_types
<64>::Elf_Addr value
,
231 // This is set if we should skip the next reloc, which should be a
232 // PLT32 reloc against ___tls_get_addr.
233 bool skip_call_tls_get_addr_
;
236 // Adjust TLS relocation type based on the options and whether this
237 // is a local symbol.
238 static tls::Tls_optimization
239 optimize_tls_reloc(bool is_final
, int r_type
);
241 // Get the GOT section, creating it if necessary.
242 Output_data_got
<64, false>*
243 got_section(Symbol_table
*, Layout
*);
245 // Get the GOT PLT section.
247 got_plt_section() const
249 gold_assert(this->got_plt_
!= NULL
);
250 return this->got_plt_
;
253 // Create a PLT entry for a global symbol.
255 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
257 // Get the PLT section.
258 Output_data_plt_x86_64
*
261 gold_assert(this->plt_
!= NULL
);
265 // Get the dynamic reloc section, creating it if necessary.
267 rela_dyn_section(Layout
*);
269 // Return true if the symbol may need a COPY relocation.
270 // References from an executable object to non-function symbols
271 // defined in a dynamic object may need a COPY relocation.
273 may_need_copy_reloc(Symbol
* gsym
)
275 return (!parameters
->output_is_shared()
276 && gsym
->is_from_dynobj()
277 && gsym
->type() != elfcpp::STT_FUNC
);
280 // Copy a relocation against a global symbol.
282 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
283 Sized_relobj
<64, false>*, unsigned int,
284 Output_section
*, Symbol
*, const elfcpp::Rela
<64, false>&);
286 // Information about this specific target which we pass to the
287 // general Target structure.
288 static const Target::Target_info x86_64_info
;
291 Output_data_got
<64, false>* got_
;
293 Output_data_plt_x86_64
* plt_
;
294 // The GOT PLT section.
295 Output_data_space
* got_plt_
;
296 // The dynamic reloc section.
297 Reloc_section
* rela_dyn_
;
298 // Relocs saved to avoid a COPY reloc.
299 Copy_relocs
<64, false>* copy_relocs_
;
300 // Space for variables copied with a COPY reloc.
301 Output_data_space
* dynbss_
;
304 const Target::Target_info
Target_x86_64::x86_64_info
=
307 false, // is_big_endian
308 elfcpp::EM_X86_64
, // machine_code
309 false, // has_make_symbol
310 false, // has_resolve
311 true, // has_code_fill
312 true, // is_default_stack_executable
313 "/lib/ld64.so.1", // program interpreter
314 0x400000, // default_text_segment_address
315 0x1000, // abi_pagesize
316 0x1000 // common_pagesize
319 // Get the GOT section, creating it if necessary.
321 Output_data_got
<64, false>*
322 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
324 if (this->got_
== NULL
)
326 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
328 this->got_
= new Output_data_got
<64, false>();
330 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
331 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
334 // The old GNU linker creates a .got.plt section. We just
335 // create another set of data in the .got section. Note that we
336 // always create a PLT if we create a GOT, although the PLT
338 this->got_plt_
= new Output_data_space(8);
339 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
340 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
343 // The first three entries are reserved.
344 this->got_plt_
->set_current_data_size(3 * 8);
346 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
347 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
349 0, 0, elfcpp::STT_OBJECT
,
351 elfcpp::STV_HIDDEN
, 0,
358 // Get the dynamic reloc section, creating it if necessary.
360 Target_x86_64::Reloc_section
*
361 Target_x86_64::rela_dyn_section(Layout
* layout
)
363 if (this->rela_dyn_
== NULL
)
365 gold_assert(layout
!= NULL
);
366 this->rela_dyn_
= new Reloc_section();
367 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
368 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
370 return this->rela_dyn_
;
373 // A class to handle the PLT data.
375 class Output_data_plt_x86_64
: public Output_section_data
378 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
380 Output_data_plt_x86_64(Layout
*, Output_data_space
*);
382 // Add an entry to the PLT.
384 add_entry(Symbol
* gsym
);
386 // Return the .rel.plt section data.
389 { return this->rel_
; }
393 do_adjust_output_section(Output_section
* os
);
396 // The size of an entry in the PLT.
397 static const int plt_entry_size
= 16;
399 // The first entry in the PLT.
400 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
401 // procedure linkage table for both programs and shared objects."
402 static unsigned char first_plt_entry
[plt_entry_size
];
404 // Other entries in the PLT for an executable.
405 static unsigned char plt_entry
[plt_entry_size
];
407 // Set the final size.
409 set_final_data_size()
410 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
412 // Write out the PLT data.
414 do_write(Output_file
*);
416 // The reloc section.
418 // The .got.plt section.
419 Output_data_space
* got_plt_
;
420 // The number of PLT entries.
424 // Create the PLT section. The ordinary .got section is an argument,
425 // since we need to refer to the start. We also create our own .got
426 // section just for PLT entries.
428 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
429 Output_data_space
* got_plt
)
430 : Output_section_data(8), got_plt_(got_plt
), count_(0)
432 this->rel_
= new Reloc_section();
433 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
434 elfcpp::SHF_ALLOC
, this->rel_
);
438 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
440 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
441 // linker, and so do we.
445 // Add an entry to the PLT.
448 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
450 gold_assert(!gsym
->has_plt_offset());
452 // Note that when setting the PLT offset we skip the initial
453 // reserved PLT entry.
454 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
458 off_t got_offset
= this->got_plt_
->current_data_size();
460 // Every PLT entry needs a GOT entry which points back to the PLT
461 // entry (this will be changed by the dynamic linker, normally
462 // lazily when the function is called).
463 this->got_plt_
->set_current_data_size(got_offset
+ 8);
465 // Every PLT entry needs a reloc.
466 gsym
->set_needs_dynsym_entry();
467 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
470 // Note that we don't need to save the symbol. The contents of the
471 // PLT are independent of which symbols are used. The symbols only
472 // appear in the relocations.
475 // The first entry in the PLT for an executable.
477 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
479 // From AMD64 ABI Draft 0.98, page 76
480 0xff, 0x35, // pushq contents of memory address
481 0, 0, 0, 0, // replaced with address of .got + 8
482 0xff, 0x25, // jmp indirect
483 0, 0, 0, 0, // replaced with address of .got + 16
484 0x90, 0x90, 0x90, 0x90 // noop (x4)
487 // Subsequent entries in the PLT for an executable.
489 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
491 // From AMD64 ABI Draft 0.98, page 76
492 0xff, 0x25, // jmpq indirect
493 0, 0, 0, 0, // replaced with address of symbol in .got
494 0x68, // pushq immediate
495 0, 0, 0, 0, // replaced with offset into relocation table
496 0xe9, // jmpq relative
497 0, 0, 0, 0 // replaced with offset to start of .plt
500 // Write out the PLT. This uses the hand-coded instructions above,
501 // and adjusts them as needed. This is specified by the AMD64 ABI.
504 Output_data_plt_x86_64::do_write(Output_file
* of
)
506 const off_t offset
= this->offset();
507 const off_t oview_size
= this->data_size();
508 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
510 const off_t got_file_offset
= this->got_plt_
->offset();
511 const off_t got_size
= this->got_plt_
->data_size();
512 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
515 unsigned char* pov
= oview
;
517 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
518 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
520 memcpy(pov
, first_plt_entry
, plt_entry_size
);
521 // We do a jmp relative to the PC at the end of this instruction.
522 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 8
523 - (plt_address
+ 6));
524 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 16
525 - (plt_address
+ 12));
526 pov
+= plt_entry_size
;
528 unsigned char* got_pov
= got_view
;
530 memset(got_pov
, 0, 24);
533 unsigned int plt_offset
= plt_entry_size
;
534 unsigned int got_offset
= 24;
535 const unsigned int count
= this->count_
;
536 for (unsigned int plt_index
= 0;
539 pov
+= plt_entry_size
,
541 plt_offset
+= plt_entry_size
,
544 // Set and adjust the PLT entry itself.
545 memcpy(pov
, plt_entry
, plt_entry_size
);
546 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
547 (got_address
+ got_offset
548 - (plt_address
+ plt_offset
551 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
552 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
553 - (plt_offset
+ plt_entry_size
));
555 // Set the entry in the GOT.
556 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
559 gold_assert(pov
- oview
== oview_size
);
560 gold_assert(got_pov
- got_view
== got_size
);
562 of
->write_output_view(offset
, oview_size
, oview
);
563 of
->write_output_view(got_file_offset
, got_size
, got_view
);
566 // Create a PLT entry for a global symbol.
569 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
572 if (gsym
->has_plt_offset())
575 if (this->plt_
== NULL
)
577 // Create the GOT sections first.
578 this->got_section(symtab
, layout
);
580 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_plt_
);
581 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
583 | elfcpp::SHF_EXECINSTR
),
587 this->plt_
->add_entry(gsym
);
590 // Handle a relocation against a non-function symbol defined in a
591 // dynamic object. The traditional way to handle this is to generate
592 // a COPY relocation to copy the variable at runtime from the shared
593 // object into the executable's data segment. However, this is
594 // undesirable in general, as if the size of the object changes in the
595 // dynamic object, the executable will no longer work correctly. If
596 // this relocation is in a writable section, then we can create a
597 // dynamic reloc and the dynamic linker will resolve it to the correct
598 // address at runtime. However, we do not want do that if the
599 // relocation is in a read-only section, as it would prevent the
600 // readonly segment from being shared. And if we have to eventually
601 // generate a COPY reloc, then any dynamic relocations will be
602 // useless. So this means that if this is a writable section, we need
603 // to save the relocation until we see whether we have to create a
604 // COPY relocation for this symbol for any other relocation.
607 Target_x86_64::copy_reloc(const General_options
* options
,
608 Symbol_table
* symtab
,
610 Sized_relobj
<64, false>* object
,
611 unsigned int data_shndx
,
612 Output_section
* output_section
,
614 const elfcpp::Rela
<64, false>& rela
)
616 Sized_symbol
<64>* ssym
;
617 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(64) (gsym
620 if (!Copy_relocs
<64, false>::need_copy_reloc(options
, object
,
623 // So far we do not need a COPY reloc. Save this relocation.
624 // If it turns out that we never need a COPY reloc for this
625 // symbol, then we will emit the relocation.
626 if (this->copy_relocs_
== NULL
)
627 this->copy_relocs_
= new Copy_relocs
<64, false>();
628 this->copy_relocs_
->save(ssym
, object
, data_shndx
, output_section
, rela
);
632 // Allocate space for this symbol in the .bss section.
634 elfcpp::Elf_types
<64>::Elf_WXword symsize
= ssym
->symsize();
636 // There is no defined way to determine the required alignment
637 // of the symbol. We pick the alignment based on the size. We
638 // set an arbitrary maximum of 256.
640 for (align
= 1; align
< 512; align
<<= 1)
641 if ((symsize
& align
) != 0)
644 if (this->dynbss_
== NULL
)
646 this->dynbss_
= new Output_data_space(align
);
647 layout
->add_output_section_data(".bss",
650 | elfcpp::SHF_WRITE
),
654 Output_data_space
* dynbss
= this->dynbss_
;
656 if (align
> dynbss
->addralign())
657 dynbss
->set_space_alignment(align
);
659 off_t dynbss_size
= dynbss
->current_data_size();
660 dynbss_size
= align_address(dynbss_size
, align
);
661 off_t offset
= dynbss_size
;
662 dynbss
->set_current_data_size(dynbss_size
+ symsize
);
664 symtab
->define_with_copy_reloc(this, ssym
, dynbss
, offset
);
666 // Add the COPY reloc.
667 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
668 rela_dyn
->add_global(ssym
, elfcpp::R_X86_64_COPY
, dynbss
, offset
, 0);
673 // Optimize the TLS relocation type based on what we know about the
674 // symbol. IS_FINAL is true if the final address of this symbol is
675 // known at link time.
677 tls::Tls_optimization
678 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
680 // If we are generating a shared library, then we can't do anything
682 if (parameters
->output_is_shared())
683 return tls::TLSOPT_NONE
;
687 case elfcpp::R_X86_64_TLSGD
:
688 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
689 case elfcpp::R_X86_64_TLSDESC_CALL
:
690 // These are General-Dynamic which permits fully general TLS
691 // access. Since we know that we are generating an executable,
692 // we can convert this to Initial-Exec. If we also know that
693 // this is a local symbol, we can further switch to Local-Exec.
695 return tls::TLSOPT_TO_LE
;
696 return tls::TLSOPT_TO_IE
;
698 case elfcpp::R_X86_64_TLSLD
:
699 // This is Local-Dynamic, which refers to a local symbol in the
700 // dynamic TLS block. Since we know that we generating an
701 // executable, we can switch to Local-Exec.
702 return tls::TLSOPT_TO_LE
;
704 case elfcpp::R_X86_64_DTPOFF32
:
705 case elfcpp::R_X86_64_DTPOFF64
:
706 // Another Local-Dynamic reloc.
707 return tls::TLSOPT_TO_LE
;
709 case elfcpp::R_X86_64_GOTTPOFF
:
710 // These are Initial-Exec relocs which get the thread offset
711 // from the GOT. If we know that we are linking against the
712 // local symbol, we can switch to Local-Exec, which links the
713 // thread offset into the instruction.
715 return tls::TLSOPT_TO_LE
;
716 return tls::TLSOPT_NONE
;
718 case elfcpp::R_X86_64_TPOFF32
:
719 // When we already have Local-Exec, there is nothing further we
721 return tls::TLSOPT_NONE
;
728 // Report an unsupported relocation against a local symbol.
731 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
734 gold_error(_("%s: unsupported reloc %u against local symbol"),
735 object
->name().c_str(), r_type
);
738 // Scan a relocation for a local symbol.
741 Target_x86_64::Scan::local(const General_options
&,
742 Symbol_table
* symtab
,
744 Target_x86_64
* target
,
745 Sized_relobj
<64, false>* object
,
746 unsigned int data_shndx
,
747 Output_section
* output_section
,
748 const elfcpp::Rela
<64, false>& reloc
,
750 const elfcpp::Sym
<64, false>& lsym
)
754 case elfcpp::R_X86_64_NONE
:
755 case elfcpp::R_386_GNU_VTINHERIT
:
756 case elfcpp::R_386_GNU_VTENTRY
:
759 case elfcpp::R_X86_64_64
:
760 // If building a shared library (or a position-independent
761 // executable), we need to create a dynamic relocation for
762 // this location. The relocation applied at link time will
763 // apply the link-time value, so we flag the location with
764 // an R_386_RELATIVE relocation so the dynamic loader can
765 // relocate it easily.
766 if (parameters
->output_is_position_independent())
768 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
769 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
770 output_section
, data_shndx
,
771 reloc
.get_r_offset(), 0);
775 case elfcpp::R_X86_64_32
:
776 case elfcpp::R_X86_64_32S
:
777 case elfcpp::R_X86_64_16
:
778 case elfcpp::R_X86_64_8
:
779 // If building a shared library (or a position-independent
780 // executable), we need to create a dynamic relocation for
781 // this location. The relocation applied at link time will
782 // apply the link-time value, so we flag the location with
783 // an R_386_RELATIVE relocation so the dynamic loader can
784 // relocate it easily.
785 if (parameters
->output_is_position_independent())
787 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
788 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
789 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
790 data_shndx
, reloc
.get_r_offset(),
791 reloc
.get_r_addend());
795 case elfcpp::R_X86_64_PC64
:
796 case elfcpp::R_X86_64_PC32
:
797 case elfcpp::R_X86_64_PC16
:
798 case elfcpp::R_X86_64_PC8
:
801 case elfcpp::R_X86_64_PLT32
:
802 // Since we know this is a local symbol, we can handle this as a
806 case elfcpp::R_X86_64_GOTPC32
:
807 case elfcpp::R_X86_64_GOTOFF64
:
808 case elfcpp::R_X86_64_GOTPC64
:
809 case elfcpp::R_X86_64_PLTOFF64
:
810 // We need a GOT section.
811 target
->got_section(symtab
, layout
);
812 // For PLTOFF64, we'd normally want a PLT section, but since we
813 // know this is a local symbol, no PLT is needed.
816 case elfcpp::R_X86_64_GOT64
:
817 case elfcpp::R_X86_64_GOT32
:
818 case elfcpp::R_X86_64_GOTPCREL64
:
819 case elfcpp::R_X86_64_GOTPCREL
:
820 case elfcpp::R_X86_64_GOTPLT64
:
822 // The symbol requires a GOT entry.
823 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
824 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
825 if (got
->add_local(object
, r_sym
))
827 // If we are generating a shared object, we need to add a
828 // dynamic relocation for this symbol's GOT entry.
829 if (parameters
->output_is_position_independent())
831 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
832 // R_X86_64_RELATIVE assumes a 64-bit relocation.
833 if (r_type
!= elfcpp::R_X86_64_GOT32
)
834 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
835 got
, object
->local_got_offset(r_sym
), 0);
837 rela_dyn
->add_local(object
, r_sym
, r_type
,
838 got
, object
->local_got_offset(r_sym
), 0);
841 // For GOTPLT64, we'd normally want a PLT section, but since
842 // we know this is a local symbol, no PLT is needed.
846 case elfcpp::R_X86_64_COPY
:
847 case elfcpp::R_X86_64_GLOB_DAT
:
848 case elfcpp::R_X86_64_JUMP_SLOT
:
849 case elfcpp::R_X86_64_RELATIVE
:
850 // These are outstanding tls relocs, which are unexpected when linking
851 case elfcpp::R_X86_64_TPOFF64
:
852 case elfcpp::R_X86_64_DTPMOD64
:
853 case elfcpp::R_X86_64_TLSDESC
:
854 gold_error(_("%s: unexpected reloc %u in object file"),
855 object
->name().c_str(), r_type
);
858 // These are initial tls relocs, which are expected when linking
859 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
860 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
861 case elfcpp::R_X86_64_TLSDESC_CALL
:
862 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
863 case elfcpp::R_X86_64_DTPOFF32
:
864 case elfcpp::R_X86_64_DTPOFF64
:
865 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
866 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
868 bool output_is_shared
= parameters
->output_is_shared();
869 const tls::Tls_optimization optimized_type
870 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
873 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
874 if (optimized_type
== tls::TLSOPT_NONE
)
876 // Create a pair of GOT entries for the module index and
877 // dtv-relative offset.
878 Output_data_got
<64, false>* got
879 = target
->got_section(symtab
, layout
);
880 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
881 got
->add_local_tls_with_rela(object
, r_sym
,
882 lsym
.get_st_shndx(), true,
883 target
->rela_dyn_section(layout
),
884 elfcpp::R_X86_64_DTPMOD64
);
886 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
887 unsupported_reloc_local(object
, r_type
);
890 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
891 case elfcpp::R_X86_64_TLSDESC_CALL
:
892 // FIXME: If not relaxing to LE, we need to generate
893 // a GOT entry with a R_x86_64_TLSDESC reloc.
894 if (optimized_type
!= tls::TLSOPT_TO_LE
)
895 unsupported_reloc_local(object
, r_type
);
898 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
899 if (optimized_type
== tls::TLSOPT_NONE
)
901 // Create a GOT entry for the module index.
902 Output_data_got
<64, false>* got
903 = target
->got_section(symtab
, layout
);
904 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
905 got
->add_local_tls_with_rela(object
, r_sym
,
906 lsym
.get_st_shndx(), false,
907 target
->rela_dyn_section(layout
),
908 elfcpp::R_X86_64_DTPMOD64
);
910 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
911 unsupported_reloc_local(object
, r_type
);
914 case elfcpp::R_X86_64_DTPOFF32
:
915 case elfcpp::R_X86_64_DTPOFF64
:
918 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
919 if (optimized_type
== tls::TLSOPT_NONE
)
921 // Create a GOT entry for the tp-relative offset.
922 Output_data_got
<64, false>* got
923 = target
->got_section(symtab
, layout
);
924 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
925 got
->add_local_with_rela(object
, r_sym
,
926 target
->rela_dyn_section(layout
),
927 elfcpp::R_X86_64_TPOFF64
);
929 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
930 unsupported_reloc_local(object
, r_type
);
933 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
934 if (output_is_shared
)
935 unsupported_reloc_local(object
, r_type
);
944 case elfcpp::R_X86_64_SIZE32
:
945 case elfcpp::R_X86_64_SIZE64
:
947 gold_error(_("%s: unsupported reloc %u against local symbol"),
948 object
->name().c_str(), r_type
);
954 // Report an unsupported relocation against a global symbol.
957 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
961 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
962 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
965 // Scan a relocation for a global symbol.
968 Target_x86_64::Scan::global(const General_options
& options
,
969 Symbol_table
* symtab
,
971 Target_x86_64
* target
,
972 Sized_relobj
<64, false>* object
,
973 unsigned int data_shndx
,
974 Output_section
* output_section
,
975 const elfcpp::Rela
<64, false>& reloc
,
981 case elfcpp::R_X86_64_NONE
:
982 case elfcpp::R_386_GNU_VTINHERIT
:
983 case elfcpp::R_386_GNU_VTENTRY
:
986 case elfcpp::R_X86_64_64
:
987 case elfcpp::R_X86_64_32
:
988 case elfcpp::R_X86_64_32S
:
989 case elfcpp::R_X86_64_16
:
990 case elfcpp::R_X86_64_8
:
992 // Make a PLT entry if necessary.
993 if (gsym
->needs_plt_entry())
995 target
->make_plt_entry(symtab
, layout
, gsym
);
996 // Since this is not a PC-relative relocation, we may be
997 // taking the address of a function. In that case we need to
998 // set the entry in the dynamic symbol table to the address of
1000 if (gsym
->is_from_dynobj())
1001 gsym
->set_needs_dynsym_value();
1003 // Make a dynamic relocation if necessary.
1004 if (gsym
->needs_dynamic_reloc(true, false))
1006 if (target
->may_need_copy_reloc(gsym
))
1008 target
->copy_reloc(&options
, symtab
, layout
, object
,
1009 data_shndx
, output_section
, gsym
, reloc
);
1011 else if (r_type
== elfcpp::R_X86_64_64
1012 && gsym
->can_use_relative_reloc(false))
1014 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1015 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
1016 output_section
, data_shndx
,
1017 reloc
.get_r_offset(), 0);
1021 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1022 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1023 data_shndx
, reloc
.get_r_offset(),
1024 reloc
.get_r_addend());
1030 case elfcpp::R_X86_64_PC64
:
1031 case elfcpp::R_X86_64_PC32
:
1032 case elfcpp::R_X86_64_PC16
:
1033 case elfcpp::R_X86_64_PC8
:
1035 // Make a PLT entry if necessary.
1036 if (gsym
->needs_plt_entry())
1037 target
->make_plt_entry(symtab
, layout
, gsym
);
1038 // Make a dynamic relocation if necessary.
1039 bool is_function_call
= (gsym
->type() == elfcpp::STT_FUNC
);
1040 if (gsym
->needs_dynamic_reloc(false, is_function_call
))
1042 if (target
->may_need_copy_reloc(gsym
))
1044 target
->copy_reloc(&options
, symtab
, layout
, object
,
1045 data_shndx
, output_section
, gsym
, reloc
);
1049 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1050 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1051 data_shndx
, reloc
.get_r_offset(),
1052 reloc
.get_r_addend());
1058 case elfcpp::R_X86_64_GOT64
:
1059 case elfcpp::R_X86_64_GOT32
:
1060 case elfcpp::R_X86_64_GOTPCREL64
:
1061 case elfcpp::R_X86_64_GOTPCREL
:
1062 case elfcpp::R_X86_64_GOTPLT64
:
1064 // The symbol requires a GOT entry.
1065 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1066 if (gsym
->final_value_is_known())
1067 got
->add_global(gsym
);
1070 // If this symbol is not fully resolved, we need to add a
1071 // dynamic relocation for it.
1072 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1073 if (gsym
->is_from_dynobj() || gsym
->is_preemptible())
1074 got
->add_global_with_rela(gsym
, rela_dyn
,
1075 elfcpp::R_X86_64_GLOB_DAT
);
1078 if (got
->add_global(gsym
))
1080 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
1081 got
, gsym
->got_offset(), 0);
1082 // Make sure we write the link-time value to the GOT.
1083 gsym
->set_needs_value_in_got();
1087 // For GOTPLT64, we also need a PLT entry (but only if the
1088 // symbol is not fully resolved).
1089 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1090 && !gsym
->final_value_is_known())
1091 target
->make_plt_entry(symtab
, layout
, gsym
);
1095 case elfcpp::R_X86_64_PLT32
:
1096 // If the symbol is fully resolved, this is just a PC32 reloc.
1097 // Otherwise we need a PLT entry.
1098 if (gsym
->final_value_is_known())
1100 // If building a shared library, we can also skip the PLT entry
1101 // if the symbol is defined in the output file and is protected
1103 if (gsym
->is_defined()
1104 && !gsym
->is_from_dynobj()
1105 && !gsym
->is_preemptible())
1107 target
->make_plt_entry(symtab
, layout
, gsym
);
1110 case elfcpp::R_X86_64_GOTPC32
:
1111 case elfcpp::R_X86_64_GOTOFF64
:
1112 case elfcpp::R_X86_64_GOTPC64
:
1113 case elfcpp::R_X86_64_PLTOFF64
:
1114 // We need a GOT section.
1115 target
->got_section(symtab
, layout
);
1116 // For PLTOFF64, we also need a PLT entry (but only if the
1117 // symbol is not fully resolved).
1118 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1119 && !gsym
->final_value_is_known())
1120 target
->make_plt_entry(symtab
, layout
, gsym
);
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 for global()
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 const bool is_final
= gsym
->final_value_is_known();
1146 const tls::Tls_optimization optimized_type
1147 = Target_x86_64::optimize_tls_reloc(is_final
, 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 got
->add_global_tls_with_rela(gsym
,
1158 target
->rela_dyn_section(layout
),
1159 elfcpp::R_X86_64_DTPMOD64
,
1160 elfcpp::R_X86_64_DTPOFF64
);
1162 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1164 // Create a GOT entry for the tp-relative offset.
1165 Output_data_got
<64, false>* got
1166 = target
->got_section(symtab
, layout
);
1167 got
->add_global_with_rela(gsym
,
1168 target
->rela_dyn_section(layout
),
1169 elfcpp::R_X86_64_TPOFF64
);
1171 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1172 unsupported_reloc_global(object
, r_type
, gsym
);
1175 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1176 case elfcpp::R_X86_64_TLSDESC_CALL
:
1177 // FIXME: If not relaxing to LE, we need to generate
1178 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1179 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1180 unsupported_reloc_global(object
, r_type
, gsym
);
1183 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1184 if (optimized_type
== tls::TLSOPT_NONE
)
1186 // Create a GOT entry for the module index.
1187 Output_data_got
<64, false>* got
1188 = target
->got_section(symtab
, layout
);
1189 got
->add_global_tls_with_rela(gsym
,
1190 target
->rela_dyn_section(layout
),
1191 elfcpp::R_X86_64_DTPMOD64
);
1193 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1194 unsupported_reloc_global(object
, r_type
, gsym
);
1197 case elfcpp::R_X86_64_DTPOFF32
:
1198 case elfcpp::R_X86_64_DTPOFF64
:
1201 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1202 if (optimized_type
== tls::TLSOPT_NONE
)
1204 // Create a GOT entry for the tp-relative offset.
1205 Output_data_got
<64, false>* got
1206 = target
->got_section(symtab
, layout
);
1207 got
->add_global_with_rela(gsym
,
1208 target
->rela_dyn_section(layout
),
1209 elfcpp::R_X86_64_TPOFF64
);
1211 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1212 unsupported_reloc_global(object
, r_type
, gsym
);
1215 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1216 if (parameters
->output_is_shared())
1217 unsupported_reloc_local(object
, r_type
);
1226 case elfcpp::R_X86_64_SIZE32
:
1227 case elfcpp::R_X86_64_SIZE64
:
1229 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1230 object
->name().c_str(), r_type
,
1231 gsym
->demangled_name().c_str());
1236 // Scan relocations for a section.
1239 Target_x86_64::scan_relocs(const General_options
& options
,
1240 Symbol_table
* symtab
,
1242 Sized_relobj
<64, false>* object
,
1243 unsigned int data_shndx
,
1244 unsigned int sh_type
,
1245 const unsigned char* prelocs
,
1247 Output_section
* output_section
,
1248 bool needs_special_offset_handling
,
1249 size_t local_symbol_count
,
1250 const unsigned char* plocal_symbols
)
1252 if (sh_type
== elfcpp::SHT_REL
)
1254 gold_error(_("%s: unsupported REL reloc section"),
1255 object
->name().c_str());
1259 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1260 Target_x86_64::Scan
>(
1270 needs_special_offset_handling
,
1275 // Finalize the sections.
1278 Target_x86_64::do_finalize_sections(Layout
* layout
)
1280 // Fill in some more dynamic tags.
1281 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1284 if (this->got_plt_
!= NULL
)
1285 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1287 if (this->plt_
!= NULL
)
1289 const Output_data
* od
= this->plt_
->rel_plt();
1290 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1291 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1292 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1295 if (this->rela_dyn_
!= NULL
)
1297 const Output_data
* od
= this->rela_dyn_
;
1298 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1299 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1300 odyn
->add_constant(elfcpp::DT_RELAENT
,
1301 elfcpp::Elf_sizes
<64>::rela_size
);
1304 if (!parameters
->output_is_shared())
1306 // The value of the DT_DEBUG tag is filled in by the dynamic
1307 // linker at run time, and used by the debugger.
1308 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1312 // Emit any relocs we saved in an attempt to avoid generating COPY
1314 if (this->copy_relocs_
== NULL
)
1316 if (this->copy_relocs_
->any_to_emit())
1318 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1319 this->copy_relocs_
->emit(rela_dyn
);
1321 delete this->copy_relocs_
;
1322 this->copy_relocs_
= NULL
;
1325 // Perform a relocation.
1328 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1329 Target_x86_64
* target
,
1331 const elfcpp::Rela
<64, false>& rela
,
1332 unsigned int r_type
,
1333 const Sized_symbol
<64>* gsym
,
1334 const Symbol_value
<64>* psymval
,
1335 unsigned char* view
,
1336 elfcpp::Elf_types
<64>::Elf_Addr address
,
1339 if (this->skip_call_tls_get_addr_
)
1341 if (r_type
!= elfcpp::R_X86_64_PLT32
1343 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1345 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1346 _("missing expected TLS relocation"));
1350 this->skip_call_tls_get_addr_
= false;
1355 // Pick the value to use for symbols defined in shared objects.
1356 Symbol_value
<64> symval
;
1358 && (gsym
->is_from_dynobj()
1359 || (parameters
->output_is_shared()
1360 && gsym
->is_preemptible()))
1361 && gsym
->has_plt_offset())
1363 symval
.set_output_value(target
->plt_section()->address()
1364 + gsym
->plt_offset());
1368 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1369 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1371 // Get the GOT offset if needed.
1372 // The GOT pointer points to the end of the GOT section.
1373 // We need to subtract the size of the GOT section to get
1374 // the actual offset to use in the relocation.
1375 bool have_got_offset
= false;
1376 unsigned int got_offset
= 0;
1379 case elfcpp::R_X86_64_GOT32
:
1380 case elfcpp::R_X86_64_GOT64
:
1381 case elfcpp::R_X86_64_GOTPLT64
:
1382 case elfcpp::R_X86_64_GOTPCREL
:
1383 case elfcpp::R_X86_64_GOTPCREL64
:
1386 gold_assert(gsym
->has_got_offset());
1387 got_offset
= gsym
->got_offset() - target
->got_size();
1391 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1392 gold_assert(object
->local_has_got_offset(r_sym
));
1393 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1395 have_got_offset
= true;
1404 case elfcpp::R_X86_64_NONE
:
1405 case elfcpp::R_386_GNU_VTINHERIT
:
1406 case elfcpp::R_386_GNU_VTENTRY
:
1409 case elfcpp::R_X86_64_64
:
1410 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1413 case elfcpp::R_X86_64_PC64
:
1414 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1418 case elfcpp::R_X86_64_32
:
1419 // FIXME: we need to verify that value + addend fits into 32 bits:
1420 // uint64_t x = value + addend;
1421 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1422 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1423 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1426 case elfcpp::R_X86_64_32S
:
1427 // FIXME: we need to verify that value + addend fits into 32 bits:
1428 // int64_t x = value + addend; // note this quantity is signed!
1429 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1430 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1433 case elfcpp::R_X86_64_PC32
:
1434 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1438 case elfcpp::R_X86_64_16
:
1439 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1442 case elfcpp::R_X86_64_PC16
:
1443 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1447 case elfcpp::R_X86_64_8
:
1448 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1451 case elfcpp::R_X86_64_PC8
:
1452 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1456 case elfcpp::R_X86_64_PLT32
:
1457 gold_assert(gsym
== NULL
1458 || gsym
->has_plt_offset()
1459 || gsym
->final_value_is_known());
1460 // Note: while this code looks the same as for R_X86_64_PC32, it
1461 // behaves differently because psymval was set to point to
1462 // the PLT entry, rather than the symbol, in Scan::global().
1463 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1467 case elfcpp::R_X86_64_PLTOFF64
:
1470 gold_assert(gsym
->has_plt_offset()
1471 || gsym
->final_value_is_known());
1472 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
1473 got_address
= target
->got_section(NULL
, NULL
)->address();
1474 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
1475 addend
- got_address
);
1478 case elfcpp::R_X86_64_GOT32
:
1479 gold_assert(have_got_offset
);
1480 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1483 case elfcpp::R_X86_64_GOTPC32
:
1486 elfcpp::Elf_types
<64>::Elf_Addr value
;
1487 value
= target
->got_plt_section()->address();
1488 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1492 case elfcpp::R_X86_64_GOT64
:
1493 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1494 // Since we always add a PLT entry, this is equivalent.
1495 case elfcpp::R_X86_64_GOTPLT64
:
1496 gold_assert(have_got_offset
);
1497 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1500 case elfcpp::R_X86_64_GOTPC64
:
1503 elfcpp::Elf_types
<64>::Elf_Addr value
;
1504 value
= target
->got_plt_section()->address();
1505 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1509 case elfcpp::R_X86_64_GOTOFF64
:
1511 elfcpp::Elf_types
<64>::Elf_Addr value
;
1512 value
= (psymval
->value(object
, 0)
1513 - target
->got_plt_section()->address());
1514 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1518 case elfcpp::R_X86_64_GOTPCREL
:
1520 gold_assert(have_got_offset
);
1521 elfcpp::Elf_types
<64>::Elf_Addr value
;
1522 value
= target
->got_plt_section()->address() + got_offset
;
1523 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1527 case elfcpp::R_X86_64_GOTPCREL64
:
1529 gold_assert(have_got_offset
);
1530 elfcpp::Elf_types
<64>::Elf_Addr value
;
1531 value
= target
->got_plt_section()->address() + got_offset
;
1532 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1536 case elfcpp::R_X86_64_COPY
:
1537 case elfcpp::R_X86_64_GLOB_DAT
:
1538 case elfcpp::R_X86_64_JUMP_SLOT
:
1539 case elfcpp::R_X86_64_RELATIVE
:
1540 // These are outstanding tls relocs, which are unexpected when linking
1541 case elfcpp::R_X86_64_TPOFF64
:
1542 case elfcpp::R_X86_64_DTPMOD64
:
1543 case elfcpp::R_X86_64_TLSDESC
:
1544 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1545 _("unexpected reloc %u in object file"),
1549 // These are initial tls relocs, which are expected when linking
1550 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1551 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1552 case elfcpp::R_X86_64_TLSDESC_CALL
:
1553 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1554 case elfcpp::R_X86_64_DTPOFF32
:
1555 case elfcpp::R_X86_64_DTPOFF64
:
1556 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1557 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1558 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
1559 view
, address
, view_size
);
1562 case elfcpp::R_X86_64_SIZE32
:
1563 case elfcpp::R_X86_64_SIZE64
:
1565 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1566 _("unsupported reloc %u"),
1574 // Perform a TLS relocation.
1577 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1578 Target_x86_64
* target
,
1580 const elfcpp::Rela
<64, false>& rela
,
1581 unsigned int r_type
,
1582 const Sized_symbol
<64>* gsym
,
1583 const Symbol_value
<64>* psymval
,
1584 unsigned char* view
,
1585 elfcpp::Elf_types
<64>::Elf_Addr
,
1588 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1590 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1592 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1594 const bool is_final
= (gsym
== NULL
1595 ? !parameters
->output_is_position_independent()
1596 : gsym
->final_value_is_known());
1597 const tls::Tls_optimization optimized_type
1598 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1601 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1602 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1603 case elfcpp::R_X86_64_TLSDESC_CALL
:
1604 if (optimized_type
== tls::TLSOPT_TO_LE
)
1606 gold_assert(tls_segment
!= NULL
);
1607 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1608 rela
, r_type
, value
, view
,
1614 unsigned int got_offset
;
1617 gold_assert(gsym
->has_tls_got_offset(true));
1618 got_offset
= gsym
->tls_got_offset(true) - target
->got_size();
1622 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1623 gold_assert(object
->local_has_tls_got_offset(r_sym
, true));
1624 got_offset
= (object
->local_tls_got_offset(r_sym
, true)
1625 - target
->got_size());
1627 if (optimized_type
== tls::TLSOPT_TO_IE
)
1629 gold_assert(tls_segment
!= NULL
);
1630 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1631 got_offset
, view
, view_size
);
1634 else if (optimized_type
== tls::TLSOPT_NONE
)
1636 // Relocate the field with the offset of the pair of GOT
1638 Relocate_functions
<64, false>::rel64(view
, got_offset
);
1642 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1643 _("unsupported reloc %u"), r_type
);
1646 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1647 if (optimized_type
== tls::TLSOPT_TO_LE
)
1649 gold_assert(tls_segment
!= NULL
);
1650 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1651 value
, view
, view_size
);
1654 else if (optimized_type
== tls::TLSOPT_NONE
)
1656 // Relocate the field with the offset of the GOT entry for
1657 // the module index.
1658 unsigned int got_offset
;
1661 gold_assert(gsym
->has_tls_got_offset(false));
1662 got_offset
= gsym
->tls_got_offset(false) - target
->got_size();
1666 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1667 gold_assert(object
->local_has_tls_got_offset(r_sym
, false));
1668 got_offset
= (object
->local_tls_got_offset(r_sym
, false)
1669 - target
->got_size());
1671 Relocate_functions
<64, false>::rel64(view
, got_offset
);
1674 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1675 _("unsupported reloc %u"), r_type
);
1678 case elfcpp::R_X86_64_DTPOFF32
:
1679 gold_assert(tls_segment
!= NULL
);
1680 if (optimized_type
== tls::TLSOPT_TO_LE
)
1681 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1683 value
= value
- tls_segment
->vaddr();
1684 Relocate_functions
<64, false>::rel32(view
, value
);
1687 case elfcpp::R_X86_64_DTPOFF64
:
1688 gold_assert(tls_segment
!= NULL
);
1689 if (optimized_type
== tls::TLSOPT_TO_LE
)
1690 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1692 value
= value
- tls_segment
->vaddr();
1693 Relocate_functions
<64, false>::rel64(view
, value
);
1696 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1697 if (optimized_type
== tls::TLSOPT_TO_LE
)
1699 gold_assert(tls_segment
!= NULL
);
1700 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1701 rela
, r_type
, value
, view
,
1705 else if (optimized_type
== tls::TLSOPT_NONE
)
1707 // Relocate the field with the offset of the GOT entry for
1708 // the tp-relative offset of the symbol.
1709 unsigned int got_offset
;
1712 gold_assert(gsym
->has_got_offset());
1713 got_offset
= gsym
->got_offset() - target
->got_size();
1717 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1718 gold_assert(object
->local_has_got_offset(r_sym
));
1719 got_offset
= (object
->local_got_offset(r_sym
)
1720 - target
->got_size());
1722 Relocate_functions
<64, false>::rel64(view
, got_offset
);
1725 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1726 _("unsupported reloc type %u"),
1730 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1731 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1732 Relocate_functions
<64, false>::rel32(view
, value
);
1737 // Do a relocation in which we convert a TLS General-Dynamic to an
1741 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
1743 Output_segment
* tls_segment
,
1744 const elfcpp::Rela
<64, false>& rela
,
1746 elfcpp::Elf_types
<64>::Elf_Addr value
,
1747 unsigned char* view
,
1750 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1751 // .word 0x6666; rex64; call __tls_get_addr
1752 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
1754 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
1755 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
1757 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1758 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
1759 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1760 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
1762 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
1764 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1765 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
1767 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1769 this->skip_call_tls_get_addr_
= true;
1772 // Do a relocation in which we convert a TLS General-Dynamic to a
1776 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
1778 Output_segment
* tls_segment
,
1779 const elfcpp::Rela
<64, false>& rela
,
1781 elfcpp::Elf_types
<64>::Elf_Addr value
,
1782 unsigned char* view
,
1785 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1786 // .word 0x6666; rex64; call __tls_get_addr
1787 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1789 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
1790 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
1792 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1793 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
1794 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1795 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
1797 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1799 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1800 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
1802 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1804 this->skip_call_tls_get_addr_
= true;
1808 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
1811 const elfcpp::Rela
<64, false>& rela
,
1813 elfcpp::Elf_types
<64>::Elf_Addr
,
1814 unsigned char* view
,
1817 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1818 // ... leq foo@dtpoff(%rax),%reg
1819 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1821 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1822 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
1824 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1825 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
1827 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
1829 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1831 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1833 this->skip_call_tls_get_addr_
= true;
1836 // Do a relocation in which we convert a TLS Initial-Exec to a
1840 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
1842 Output_segment
* tls_segment
,
1843 const elfcpp::Rela
<64, false>& rela
,
1845 elfcpp::Elf_types
<64>::Elf_Addr value
,
1846 unsigned char* view
,
1849 // We need to examine the opcodes to figure out which instruction we
1852 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1853 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1855 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1856 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
1858 unsigned char op1
= view
[-3];
1859 unsigned char op2
= view
[-2];
1860 unsigned char op3
= view
[-1];
1861 unsigned char reg
= op3
>> 3;
1869 view
[-1] = 0xc0 | reg
;
1873 // Special handling for %rsp.
1877 view
[-1] = 0xc0 | reg
;
1885 view
[-1] = 0x80 | reg
| (reg
<< 3);
1888 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1889 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1892 // Relocate section data.
1895 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
1896 unsigned int sh_type
,
1897 const unsigned char* prelocs
,
1899 Output_section
* output_section
,
1900 bool needs_special_offset_handling
,
1901 unsigned char* view
,
1902 elfcpp::Elf_types
<64>::Elf_Addr address
,
1905 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1907 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1908 Target_x86_64::Relocate
>(
1914 needs_special_offset_handling
,
1920 // Return the value to use for a dynamic which requires special
1921 // treatment. This is how we support equality comparisons of function
1922 // pointers across shared library boundaries, as described in the
1923 // processor specific ABI supplement.
1926 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
1928 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1929 return this->plt_section()->address() + gsym
->plt_offset();
1932 // Return a string used to fill a code section with nops to take up
1933 // the specified length.
1936 Target_x86_64::do_code_fill(off_t length
)
1940 // Build a jmpq instruction to skip over the bytes.
1941 unsigned char jmp
[5];
1943 elfcpp::Swap_unaligned
<64, false>::writeval(jmp
+ 1, length
- 5);
1944 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1945 + std::string(length
- 5, '\0'));
1948 // Nop sequences of various lengths.
1949 const char nop1
[1] = { 0x90 }; // nop
1950 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1951 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1952 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1953 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1954 0x00 }; // leal 0(%esi,1),%esi
1955 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1957 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1959 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1960 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1961 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1962 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1964 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1965 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1967 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1968 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1970 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1971 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1972 0x00, 0x00, 0x00, 0x00 };
1973 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1974 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1975 0x27, 0x00, 0x00, 0x00,
1977 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1978 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1979 0xbc, 0x27, 0x00, 0x00,
1981 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1982 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1983 0x90, 0x90, 0x90, 0x90,
1986 const char* nops
[16] = {
1988 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
1989 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
1992 return std::string(nops
[length
], length
);
1995 // The selector for x86_64 object files.
1997 class Target_selector_x86_64
: public Target_selector
2000 Target_selector_x86_64()
2001 : Target_selector(elfcpp::EM_X86_64
, 64, false)
2005 recognize(int machine
, int osabi
, int abiversion
);
2008 Target_x86_64
* target_
;
2011 // Recognize an x86_64 object file when we already know that the machine
2012 // number is EM_X86_64.
2015 Target_selector_x86_64::recognize(int, int, int)
2017 if (this->target_
== NULL
)
2018 this->target_
= new Target_x86_64();
2019 return this->target_
;
2022 Target_selector_x86_64 target_selector_x86_64
;
2024 } // End anonymous namespace.