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>*, size_t relnum
,
190 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_le(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 Local-Dynamic to Local-Exec transition.
206 tls_ld_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 Initial-Exec to Local-Exec transition.
215 tls_ie_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 // This is set if we should skip the next reloc, which should be a
223 // PLT32 reloc against ___tls_get_addr.
224 bool skip_call_tls_get_addr_
;
227 // Adjust TLS relocation type based on the options and whether this
228 // is a local symbol.
229 static tls::Tls_optimization
230 optimize_tls_reloc(bool is_final
, int r_type
);
232 // Get the GOT section, creating it if necessary.
233 Output_data_got
<64, false>*
234 got_section(Symbol_table
*, Layout
*);
236 // Get the GOT PLT section.
238 got_plt_section() const
240 gold_assert(this->got_plt_
!= NULL
);
241 return this->got_plt_
;
244 // Create a PLT entry for a global symbol.
246 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
248 // Get the PLT section.
249 Output_data_plt_x86_64
*
252 gold_assert(this->plt_
!= NULL
);
256 // Get the dynamic reloc section, creating it if necessary.
258 rela_dyn_section(Layout
*);
260 // Return true if the symbol may need a COPY relocation.
261 // References from an executable object to non-function symbols
262 // defined in a dynamic object may need a COPY relocation.
264 may_need_copy_reloc(Symbol
* gsym
)
266 return (!parameters
->output_is_shared()
267 && gsym
->is_from_dynobj()
268 && gsym
->type() != elfcpp::STT_FUNC
);
271 // Copy a relocation against a global symbol.
273 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
274 Sized_relobj
<64, false>*, unsigned int,
275 Output_section
*, Symbol
*, const elfcpp::Rela
<64, false>&);
277 // Information about this specific target which we pass to the
278 // general Target structure.
279 static const Target::Target_info x86_64_info
;
282 Output_data_got
<64, false>* got_
;
284 Output_data_plt_x86_64
* plt_
;
285 // The GOT PLT section.
286 Output_data_space
* got_plt_
;
287 // The dynamic reloc section.
288 Reloc_section
* rela_dyn_
;
289 // Relocs saved to avoid a COPY reloc.
290 Copy_relocs
<64, false>* copy_relocs_
;
291 // Space for variables copied with a COPY reloc.
292 Output_data_space
* dynbss_
;
295 const Target::Target_info
Target_x86_64::x86_64_info
=
298 false, // is_big_endian
299 elfcpp::EM_X86_64
, // machine_code
300 false, // has_make_symbol
301 false, // has_resolve
302 true, // has_code_fill
303 true, // is_default_stack_executable
304 "/lib/ld64.so.1", // program interpreter
305 0x400000, // default_text_segment_address
306 0x1000, // abi_pagesize
307 0x1000 // common_pagesize
310 // Get the GOT section, creating it if necessary.
312 Output_data_got
<64, false>*
313 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
315 if (this->got_
== NULL
)
317 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
319 this->got_
= new Output_data_got
<64, false>();
321 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
322 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
325 // The old GNU linker creates a .got.plt section. We just
326 // create another set of data in the .got section. Note that we
327 // always create a PLT if we create a GOT, although the PLT
329 this->got_plt_
= new Output_data_space(8);
330 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
331 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
334 // The first three entries are reserved.
335 this->got_plt_
->set_current_data_size(3 * 8);
337 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
338 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
340 0, 0, elfcpp::STT_OBJECT
,
342 elfcpp::STV_HIDDEN
, 0,
349 // Get the dynamic reloc section, creating it if necessary.
351 Target_x86_64::Reloc_section
*
352 Target_x86_64::rela_dyn_section(Layout
* layout
)
354 if (this->rela_dyn_
== NULL
)
356 gold_assert(layout
!= NULL
);
357 this->rela_dyn_
= new Reloc_section();
358 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
359 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
361 return this->rela_dyn_
;
364 // A class to handle the PLT data.
366 class Output_data_plt_x86_64
: public Output_section_data
369 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
371 Output_data_plt_x86_64(Layout
*, Output_data_space
*);
373 // Add an entry to the PLT.
375 add_entry(Symbol
* gsym
);
377 // Return the .rel.plt section data.
380 { return this->rel_
; }
384 do_adjust_output_section(Output_section
* os
);
387 // The size of an entry in the PLT.
388 static const int plt_entry_size
= 16;
390 // The first entry in the PLT.
391 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
392 // procedure linkage table for both programs and shared objects."
393 static unsigned char first_plt_entry
[plt_entry_size
];
395 // Other entries in the PLT for an executable.
396 static unsigned char plt_entry
[plt_entry_size
];
398 // Set the final size.
400 set_final_data_size()
401 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
403 // Write out the PLT data.
405 do_write(Output_file
*);
407 // The reloc section.
409 // The .got.plt section.
410 Output_data_space
* got_plt_
;
411 // The number of PLT entries.
415 // Create the PLT section. The ordinary .got section is an argument,
416 // since we need to refer to the start. We also create our own .got
417 // section just for PLT entries.
419 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
420 Output_data_space
* got_plt
)
421 : Output_section_data(8), got_plt_(got_plt
), count_(0)
423 this->rel_
= new Reloc_section();
424 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
425 elfcpp::SHF_ALLOC
, this->rel_
);
429 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
431 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
432 // linker, and so do we.
436 // Add an entry to the PLT.
439 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
441 gold_assert(!gsym
->has_plt_offset());
443 // Note that when setting the PLT offset we skip the initial
444 // reserved PLT entry.
445 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
449 off_t got_offset
= this->got_plt_
->current_data_size();
451 // Every PLT entry needs a GOT entry which points back to the PLT
452 // entry (this will be changed by the dynamic linker, normally
453 // lazily when the function is called).
454 this->got_plt_
->set_current_data_size(got_offset
+ 8);
456 // Every PLT entry needs a reloc.
457 gsym
->set_needs_dynsym_entry();
458 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
461 // Note that we don't need to save the symbol. The contents of the
462 // PLT are independent of which symbols are used. The symbols only
463 // appear in the relocations.
466 // The first entry in the PLT for an executable.
468 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
470 // From AMD64 ABI Draft 0.98, page 76
471 0xff, 0x35, // pushq contents of memory address
472 0, 0, 0, 0, // replaced with address of .got + 4
473 0xff, 0x25, // jmp indirect
474 0, 0, 0, 0, // replaced with address of .got + 8
475 0x90, 0x90, 0x90, 0x90 // noop (x4)
478 // Subsequent entries in the PLT for an executable.
480 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
482 // From AMD64 ABI Draft 0.98, page 76
483 0xff, 0x25, // jmpq indirect
484 0, 0, 0, 0, // replaced with address of symbol in .got
485 0x68, // pushq immediate
486 0, 0, 0, 0, // replaced with offset into relocation table
487 0xe9, // jmpq relative
488 0, 0, 0, 0 // replaced with offset to start of .plt
491 // Write out the PLT. This uses the hand-coded instructions above,
492 // and adjusts them as needed. This is specified by the AMD64 ABI.
495 Output_data_plt_x86_64::do_write(Output_file
* of
)
497 const off_t offset
= this->offset();
498 const off_t oview_size
= this->data_size();
499 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
501 const off_t got_file_offset
= this->got_plt_
->offset();
502 const off_t got_size
= this->got_plt_
->data_size();
503 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
506 unsigned char* pov
= oview
;
508 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
509 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
511 memcpy(pov
, first_plt_entry
, plt_entry_size
);
512 if (!parameters
->output_is_shared())
514 // We do a jmp relative to the PC at the end of this instruction.
515 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 8
516 - (plt_address
+ 6));
517 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 16
518 - (plt_address
+ 12));
520 pov
+= plt_entry_size
;
522 unsigned char* got_pov
= got_view
;
524 memset(got_pov
, 0, 24);
527 unsigned int plt_offset
= plt_entry_size
;
528 unsigned int got_offset
= 24;
529 const unsigned int count
= this->count_
;
530 for (unsigned int plt_index
= 0;
533 pov
+= plt_entry_size
,
535 plt_offset
+= plt_entry_size
,
538 // Set and adjust the PLT entry itself.
539 memcpy(pov
, plt_entry
, plt_entry_size
);
540 if (parameters
->output_is_shared())
541 // FIXME(csilvers): what's the right thing to write here?
542 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
544 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
545 (got_address
+ got_offset
546 - (plt_address
+ plt_offset
549 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
550 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
551 - (plt_offset
+ plt_entry_size
));
553 // Set the entry in the GOT.
554 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
557 gold_assert(pov
- oview
== oview_size
);
558 gold_assert(got_pov
- got_view
== got_size
);
560 of
->write_output_view(offset
, oview_size
, oview
);
561 of
->write_output_view(got_file_offset
, got_size
, got_view
);
564 // Create a PLT entry for a global symbol.
567 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
570 if (gsym
->has_plt_offset())
573 if (this->plt_
== NULL
)
575 // Create the GOT sections first.
576 this->got_section(symtab
, layout
);
578 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_plt_
);
579 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
581 | elfcpp::SHF_EXECINSTR
),
585 this->plt_
->add_entry(gsym
);
588 // Handle a relocation against a non-function symbol defined in a
589 // dynamic object. The traditional way to handle this is to generate
590 // a COPY relocation to copy the variable at runtime from the shared
591 // object into the executable's data segment. However, this is
592 // undesirable in general, as if the size of the object changes in the
593 // dynamic object, the executable will no longer work correctly. If
594 // this relocation is in a writable section, then we can create a
595 // dynamic reloc and the dynamic linker will resolve it to the correct
596 // address at runtime. However, we do not want do that if the
597 // relocation is in a read-only section, as it would prevent the
598 // readonly segment from being shared. And if we have to eventually
599 // generate a COPY reloc, then any dynamic relocations will be
600 // useless. So this means that if this is a writable section, we need
601 // to save the relocation until we see whether we have to create a
602 // COPY relocation for this symbol for any other relocation.
605 Target_x86_64::copy_reloc(const General_options
* options
,
606 Symbol_table
* symtab
,
608 Sized_relobj
<64, false>* object
,
609 unsigned int data_shndx
,
610 Output_section
* output_section
,
612 const elfcpp::Rela
<64, false>& rela
)
614 Sized_symbol
<64>* ssym
;
615 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(64) (gsym
618 if (!Copy_relocs
<64, false>::need_copy_reloc(options
, object
,
621 // So far we do not need a COPY reloc. Save this relocation.
622 // If it turns out that we never need a COPY reloc for this
623 // symbol, then we will emit the relocation.
624 if (this->copy_relocs_
== NULL
)
625 this->copy_relocs_
= new Copy_relocs
<64, false>();
626 this->copy_relocs_
->save(ssym
, object
, data_shndx
, output_section
, rela
);
630 // Allocate space for this symbol in the .bss section.
632 elfcpp::Elf_types
<64>::Elf_WXword symsize
= ssym
->symsize();
634 // There is no defined way to determine the required alignment
635 // of the symbol. We pick the alignment based on the size. We
636 // set an arbitrary maximum of 256.
638 for (align
= 1; align
< 512; align
<<= 1)
639 if ((symsize
& align
) != 0)
642 if (this->dynbss_
== NULL
)
644 this->dynbss_
= new Output_data_space(align
);
645 layout
->add_output_section_data(".bss",
648 | elfcpp::SHF_WRITE
),
652 Output_data_space
* dynbss
= this->dynbss_
;
654 if (align
> dynbss
->addralign())
655 dynbss
->set_space_alignment(align
);
657 off_t dynbss_size
= dynbss
->current_data_size();
658 dynbss_size
= align_address(dynbss_size
, align
);
659 off_t offset
= dynbss_size
;
660 dynbss
->set_current_data_size(dynbss_size
+ symsize
);
662 symtab
->define_with_copy_reloc(this, ssym
, dynbss
, offset
);
664 // Add the COPY reloc.
665 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
666 rela_dyn
->add_global(ssym
, elfcpp::R_X86_64_COPY
, dynbss
, offset
, 0);
671 // Optimize the TLS relocation type based on what we know about the
672 // symbol. IS_FINAL is true if the final address of this symbol is
673 // known at link time.
675 tls::Tls_optimization
676 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
678 // If we are generating a shared library, then we can't do anything
680 if (parameters
->output_is_shared())
681 return tls::TLSOPT_NONE
;
685 case elfcpp::R_X86_64_TLSGD
:
686 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
687 case elfcpp::R_X86_64_TLSDESC_CALL
:
688 // These are General-Dynamic which permits fully general TLS
689 // access. Since we know that we are generating an executable,
690 // we can convert this to Initial-Exec. If we also know that
691 // this is a local symbol, we can further switch to Local-Exec.
693 return tls::TLSOPT_TO_LE
;
694 return tls::TLSOPT_TO_IE
;
696 case elfcpp::R_X86_64_TLSLD
:
697 // This is Local-Dynamic, which refers to a local symbol in the
698 // dynamic TLS block. Since we know that we generating an
699 // executable, we can switch to Local-Exec.
700 return tls::TLSOPT_TO_LE
;
702 case elfcpp::R_X86_64_DTPOFF32
:
703 case elfcpp::R_X86_64_DTPOFF64
:
704 // Another Local-Dynamic reloc.
705 return tls::TLSOPT_TO_LE
;
707 case elfcpp::R_X86_64_GOTTPOFF
:
708 // These are Initial-Exec relocs which get the thread offset
709 // from the GOT. If we know that we are linking against the
710 // local symbol, we can switch to Local-Exec, which links the
711 // thread offset into the instruction.
713 return tls::TLSOPT_TO_LE
;
714 return tls::TLSOPT_NONE
;
716 case elfcpp::R_X86_64_TPOFF32
:
717 // When we already have Local-Exec, there is nothing further we
719 return tls::TLSOPT_NONE
;
726 // Report an unsupported relocation against a local symbol.
729 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
732 gold_error(_("%s: unsupported reloc %u against local symbol"),
733 object
->name().c_str(), r_type
);
736 // Scan a relocation for a local symbol.
739 Target_x86_64::Scan::local(const General_options
&,
740 Symbol_table
* symtab
,
742 Target_x86_64
* target
,
743 Sized_relobj
<64, false>* object
,
744 unsigned int data_shndx
,
745 Output_section
* output_section
,
746 const elfcpp::Rela
<64, false>& reloc
,
748 const elfcpp::Sym
<64, false>&)
752 case elfcpp::R_X86_64_NONE
:
753 case elfcpp::R_386_GNU_VTINHERIT
:
754 case elfcpp::R_386_GNU_VTENTRY
:
757 case elfcpp::R_X86_64_64
:
758 // If building a shared library (or a position-independent
759 // executable), we need to create a dynamic relocation for
760 // this location. The relocation applied at link time will
761 // apply the link-time value, so we flag the location with
762 // an R_386_RELATIVE relocation so the dynamic loader can
763 // relocate it easily.
764 if (parameters
->output_is_position_independent())
766 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
767 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
768 output_section
, data_shndx
,
769 reloc
.get_r_offset(), 0);
773 case elfcpp::R_X86_64_32
:
774 case elfcpp::R_X86_64_32S
:
775 case elfcpp::R_X86_64_16
:
776 case elfcpp::R_X86_64_8
:
777 // If building a shared library (or a position-independent
778 // executable), we need to create a dynamic relocation for
779 // this location. The relocation applied at link time will
780 // apply the link-time value, so we flag the location with
781 // an R_386_RELATIVE relocation so the dynamic loader can
782 // relocate it easily.
783 if (parameters
->output_is_position_independent())
785 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
786 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
787 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
788 data_shndx
, reloc
.get_r_offset(),
789 reloc
.get_r_addend());
793 case elfcpp::R_X86_64_PC64
:
794 case elfcpp::R_X86_64_PC32
:
795 case elfcpp::R_X86_64_PC16
:
796 case elfcpp::R_X86_64_PC8
:
799 case elfcpp::R_X86_64_PLT32
:
800 // Since we know this is a local symbol, we can handle this as a
804 case elfcpp::R_X86_64_GOTPC32
:
805 case elfcpp::R_X86_64_GOTOFF64
:
806 case elfcpp::R_X86_64_GOTPC64
:
807 case elfcpp::R_X86_64_PLTOFF64
:
808 // We need a GOT section.
809 target
->got_section(symtab
, layout
);
810 // For PLTOFF64, we'd normally want a PLT section, but since we
811 // know this is a local symbol, no PLT is needed.
814 case elfcpp::R_X86_64_GOT64
:
815 case elfcpp::R_X86_64_GOT32
:
816 case elfcpp::R_X86_64_GOTPCREL64
:
817 case elfcpp::R_X86_64_GOTPCREL
:
818 case elfcpp::R_X86_64_GOTPLT64
:
820 // The symbol requires a GOT entry.
821 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
822 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
823 if (got
->add_local(object
, r_sym
))
825 // If we are generating a shared object, we need to add a
826 // dynamic RELATIVE relocation for this symbol.
827 if (parameters
->output_is_position_independent())
829 // FIXME: R_X86_64_RELATIVE assumes a 64-bit relocation.
830 gold_assert(r_type
!= elfcpp::R_X86_64_GOT32
);
832 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
833 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
834 output_section
, data_shndx
,
835 reloc
.get_r_offset(), 0);
838 // For GOTPLT64, we'd normally want a PLT section, but since
839 // we know this is a local symbol, no PLT is needed.
843 case elfcpp::R_X86_64_COPY
:
844 case elfcpp::R_X86_64_GLOB_DAT
:
845 case elfcpp::R_X86_64_JUMP_SLOT
:
846 case elfcpp::R_X86_64_RELATIVE
:
847 // These are outstanding tls relocs, which are unexpected when linking
848 case elfcpp::R_X86_64_TPOFF64
:
849 case elfcpp::R_X86_64_DTPMOD64
:
850 case elfcpp::R_X86_64_TLSDESC
:
851 gold_error(_("%s: unexpected reloc %u in object file"),
852 object
->name().c_str(), r_type
);
855 // These are initial tls relocs, which are expected when linking
856 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
857 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
858 case elfcpp::R_X86_64_TLSDESC_CALL
:
859 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
860 case elfcpp::R_X86_64_DTPOFF32
:
861 case elfcpp::R_X86_64_DTPOFF64
:
862 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
863 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
865 bool output_is_shared
= parameters
->output_is_shared();
866 const tls::Tls_optimization optimized_type
867 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
870 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
871 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
872 case elfcpp::R_X86_64_TLSDESC_CALL
:
873 // FIXME: If not relaxing to LE, we need to generate
874 // DTPMOD64 and DTPOFF64 relocs.
875 if (optimized_type
!= tls::TLSOPT_TO_LE
)
876 unsupported_reloc_local(object
, r_type
);
879 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
880 case elfcpp::R_X86_64_DTPOFF32
:
881 case elfcpp::R_X86_64_DTPOFF64
:
882 // FIXME: If not relaxing to LE, we need to generate a
884 if (optimized_type
!= tls::TLSOPT_TO_LE
)
885 unsupported_reloc_local(object
, r_type
);
888 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
889 // FIXME: If not relaxing to LE, we need to generate a
891 if (optimized_type
!= tls::TLSOPT_TO_LE
)
892 unsupported_reloc_local(object
, r_type
);
895 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
896 // FIXME: If generating a shared object, we need to copy
897 // this relocation into the object.
898 gold_assert(!output_is_shared
);
907 case elfcpp::R_X86_64_SIZE32
:
908 case elfcpp::R_X86_64_SIZE64
:
910 gold_error(_("%s: unsupported reloc %u against local symbol"),
911 object
->name().c_str(), r_type
);
917 // Report an unsupported relocation against a global symbol.
920 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
924 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
925 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
928 // Scan a relocation for a global symbol.
931 Target_x86_64::Scan::global(const General_options
& options
,
932 Symbol_table
* symtab
,
934 Target_x86_64
* target
,
935 Sized_relobj
<64, false>* object
,
936 unsigned int data_shndx
,
937 Output_section
* output_section
,
938 const elfcpp::Rela
<64, false>& reloc
,
944 case elfcpp::R_X86_64_NONE
:
945 case elfcpp::R_386_GNU_VTINHERIT
:
946 case elfcpp::R_386_GNU_VTENTRY
:
949 case elfcpp::R_X86_64_64
:
950 case elfcpp::R_X86_64_32
:
951 case elfcpp::R_X86_64_32S
:
952 case elfcpp::R_X86_64_16
:
953 case elfcpp::R_X86_64_8
:
955 // Make a PLT entry if necessary.
956 if (gsym
->needs_plt_entry())
958 target
->make_plt_entry(symtab
, layout
, gsym
);
959 // Since this is not a PC-relative relocation, we may be
960 // taking the address of a function. In that case we need to
961 // set the entry in the dynamic symbol table to the address of
963 if (gsym
->is_from_dynobj())
964 gsym
->set_needs_dynsym_value();
966 // Make a dynamic relocation if necessary.
967 if (gsym
->needs_dynamic_reloc(true, false))
969 if (target
->may_need_copy_reloc(gsym
))
971 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
972 output_section
, gsym
, reloc
);
974 else if (r_type
== elfcpp::R_X86_64_64
975 && gsym
->can_use_relative_reloc(false))
977 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
978 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
979 output_section
, data_shndx
,
980 reloc
.get_r_offset(), 0);
984 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
985 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
986 data_shndx
, reloc
.get_r_offset(),
987 reloc
.get_r_addend());
993 case elfcpp::R_X86_64_PC64
:
994 case elfcpp::R_X86_64_PC32
:
995 case elfcpp::R_X86_64_PC16
:
996 case elfcpp::R_X86_64_PC8
:
998 // Make a PLT entry if necessary.
999 if (gsym
->needs_plt_entry())
1000 target
->make_plt_entry(symtab
, layout
, gsym
);
1001 // Make a dynamic relocation if necessary.
1002 bool is_function_call
= (gsym
->type() == elfcpp::STT_FUNC
);
1003 if (gsym
->needs_dynamic_reloc(true, is_function_call
))
1005 if (target
->may_need_copy_reloc(gsym
))
1007 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
1008 output_section
, gsym
, reloc
);
1012 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1013 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1014 data_shndx
, reloc
.get_r_offset(),
1015 reloc
.get_r_addend());
1021 case elfcpp::R_X86_64_GOT64
:
1022 case elfcpp::R_X86_64_GOT32
:
1023 case elfcpp::R_X86_64_GOTPCREL64
:
1024 case elfcpp::R_X86_64_GOTPCREL
:
1025 case elfcpp::R_X86_64_GOTPLT64
:
1027 // The symbol requires a GOT entry.
1028 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1029 if (got
->add_global(gsym
))
1031 // If this symbol is not fully resolved, we need to add a
1032 // dynamic relocation for it.
1033 if (!gsym
->final_value_is_known())
1035 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1036 if (gsym
->is_from_dynobj()
1037 || gsym
->is_preemptible())
1038 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
, got
,
1039 gsym
->got_offset(), 0);
1042 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_RELATIVE
,
1043 got
, gsym
->got_offset(), 0);
1044 // Make sure we write the link-time value to the GOT.
1045 gsym
->set_needs_value_in_got();
1049 // For GOTPLT64, we also need a PLT entry (but only if the
1050 // symbol is not fully resolved).
1051 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1052 && !gsym
->final_value_is_known())
1053 target
->make_plt_entry(symtab
, layout
, gsym
);
1057 case elfcpp::R_X86_64_PLT32
:
1058 // If the symbol is fully resolved, this is just a PC32 reloc.
1059 // Otherwise we need a PLT entry.
1060 if (gsym
->final_value_is_known())
1062 // If building a shared library, we can also skip the PLT entry
1063 // if the symbol is defined in the output file and is protected
1065 if (gsym
->is_defined()
1066 && !gsym
->is_from_dynobj()
1067 && !gsym
->is_preemptible())
1069 target
->make_plt_entry(symtab
, layout
, gsym
);
1072 case elfcpp::R_X86_64_GOTPC32
:
1073 case elfcpp::R_X86_64_GOTOFF64
:
1074 case elfcpp::R_X86_64_GOTPC64
:
1075 case elfcpp::R_X86_64_PLTOFF64
:
1076 // We need a GOT section.
1077 target
->got_section(symtab
, layout
);
1078 // For PLTOFF64, we also need a PLT entry (but only if the
1079 // symbol is not fully resolved).
1080 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1081 && !gsym
->final_value_is_known())
1082 target
->make_plt_entry(symtab
, layout
, gsym
);
1085 case elfcpp::R_X86_64_COPY
:
1086 case elfcpp::R_X86_64_GLOB_DAT
:
1087 case elfcpp::R_X86_64_JUMP_SLOT
:
1088 case elfcpp::R_X86_64_RELATIVE
:
1089 // These are outstanding tls relocs, which are unexpected when linking
1090 case elfcpp::R_X86_64_TPOFF64
:
1091 case elfcpp::R_X86_64_DTPMOD64
:
1092 case elfcpp::R_X86_64_TLSDESC
:
1093 gold_error(_("%s: unexpected reloc %u in object file"),
1094 object
->name().c_str(), r_type
);
1097 // These are initial tls relocs, which are expected for global()
1098 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1099 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1100 case elfcpp::R_X86_64_TLSDESC_CALL
:
1101 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1102 case elfcpp::R_X86_64_DTPOFF32
:
1103 case elfcpp::R_X86_64_DTPOFF64
:
1104 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1105 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1107 const bool is_final
= gsym
->final_value_is_known();
1108 const tls::Tls_optimization optimized_type
1109 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1112 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1113 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1114 case elfcpp::R_X86_64_TLSDESC_CALL
:
1115 // FIXME: If not relaxing to LE, we need to generate
1116 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1117 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1118 unsupported_reloc_global(object
, r_type
, gsym
);
1121 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1122 case elfcpp::R_X86_64_DTPOFF32
:
1123 case elfcpp::R_X86_64_DTPOFF64
:
1124 // FIXME: If not relaxing to LE, we need to generate a
1126 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1127 unsupported_reloc_global(object
, r_type
, gsym
);
1130 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1131 // FIXME: If not relaxing to LE, we need to generate a
1133 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1134 unsupported_reloc_global(object
, r_type
, gsym
);
1137 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1138 // FIXME: If generating a shared object, we need to copy
1139 // this relocation into the object.
1140 gold_assert(is_final
);
1149 case elfcpp::R_X86_64_SIZE32
:
1150 case elfcpp::R_X86_64_SIZE64
:
1152 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1153 object
->name().c_str(), r_type
,
1154 gsym
->demangled_name().c_str());
1159 // Scan relocations for a section.
1162 Target_x86_64::scan_relocs(const General_options
& options
,
1163 Symbol_table
* symtab
,
1165 Sized_relobj
<64, false>* object
,
1166 unsigned int data_shndx
,
1167 unsigned int sh_type
,
1168 const unsigned char* prelocs
,
1170 Output_section
* output_section
,
1171 bool needs_special_offset_handling
,
1172 size_t local_symbol_count
,
1173 const unsigned char* plocal_symbols
)
1175 if (sh_type
== elfcpp::SHT_REL
)
1177 gold_error(_("%s: unsupported REL reloc section"),
1178 object
->name().c_str());
1182 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1183 Target_x86_64::Scan
>(
1193 needs_special_offset_handling
,
1198 // Finalize the sections.
1201 Target_x86_64::do_finalize_sections(Layout
* layout
)
1203 // Fill in some more dynamic tags.
1204 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1207 if (this->got_plt_
!= NULL
)
1208 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1210 if (this->plt_
!= NULL
)
1212 const Output_data
* od
= this->plt_
->rel_plt();
1213 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1214 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1215 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1218 if (this->rela_dyn_
!= NULL
)
1220 const Output_data
* od
= this->rela_dyn_
;
1221 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1222 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1223 odyn
->add_constant(elfcpp::DT_RELAENT
,
1224 elfcpp::Elf_sizes
<64>::rela_size
);
1227 if (!parameters
->output_is_shared())
1229 // The value of the DT_DEBUG tag is filled in by the dynamic
1230 // linker at run time, and used by the debugger.
1231 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1235 // Emit any relocs we saved in an attempt to avoid generating COPY
1237 if (this->copy_relocs_
== NULL
)
1239 if (this->copy_relocs_
->any_to_emit())
1241 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1242 this->copy_relocs_
->emit(rela_dyn
);
1244 delete this->copy_relocs_
;
1245 this->copy_relocs_
= NULL
;
1248 // Perform a relocation.
1251 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1252 Target_x86_64
* target
,
1254 const elfcpp::Rela
<64, false>& rela
,
1255 unsigned int r_type
,
1256 const Sized_symbol
<64>* gsym
,
1257 const Symbol_value
<64>* psymval
,
1258 unsigned char* view
,
1259 elfcpp::Elf_types
<64>::Elf_Addr address
,
1262 if (this->skip_call_tls_get_addr_
)
1264 if (r_type
!= elfcpp::R_X86_64_PLT32
1266 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1268 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1269 _("missing expected TLS relocation"));
1273 this->skip_call_tls_get_addr_
= false;
1278 // Pick the value to use for symbols defined in shared objects.
1279 Symbol_value
<64> symval
;
1281 && (gsym
->is_from_dynobj()
1282 || (parameters
->output_is_shared()
1283 && gsym
->is_preemptible()))
1284 && gsym
->has_plt_offset())
1286 symval
.set_output_value(target
->plt_section()->address()
1287 + gsym
->plt_offset());
1291 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1292 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1294 // Get the GOT offset if needed.
1295 // The GOT pointer points to the end of the GOT section.
1296 // We need to subtract the size of the GOT section to get
1297 // the actual offset to use in the relocation.
1298 bool have_got_offset
= false;
1299 unsigned int got_offset
= 0;
1302 case elfcpp::R_X86_64_GOT32
:
1303 case elfcpp::R_X86_64_GOT64
:
1304 case elfcpp::R_X86_64_GOTPLT64
:
1305 case elfcpp::R_X86_64_GOTPCREL
:
1306 case elfcpp::R_X86_64_GOTPCREL64
:
1309 gold_assert(gsym
->has_got_offset());
1310 got_offset
= gsym
->got_offset() - target
->got_size();
1314 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1315 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1317 have_got_offset
= true;
1326 case elfcpp::R_X86_64_NONE
:
1327 case elfcpp::R_386_GNU_VTINHERIT
:
1328 case elfcpp::R_386_GNU_VTENTRY
:
1331 case elfcpp::R_X86_64_64
:
1332 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1335 case elfcpp::R_X86_64_PC64
:
1336 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1340 case elfcpp::R_X86_64_32
:
1341 // FIXME: we need to verify that value + addend fits into 32 bits:
1342 // uint64_t x = value + addend;
1343 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1344 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1345 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1348 case elfcpp::R_X86_64_32S
:
1349 // FIXME: we need to verify that value + addend fits into 32 bits:
1350 // int64_t x = value + addend; // note this quantity is signed!
1351 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1352 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1355 case elfcpp::R_X86_64_PC32
:
1356 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1360 case elfcpp::R_X86_64_16
:
1361 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1364 case elfcpp::R_X86_64_PC16
:
1365 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1369 case elfcpp::R_X86_64_8
:
1370 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1373 case elfcpp::R_X86_64_PC8
:
1374 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1378 case elfcpp::R_X86_64_PLT32
:
1379 gold_assert(gsym
== NULL
1380 || gsym
->has_plt_offset()
1381 || gsym
->final_value_is_known());
1382 // Note: while this code looks the same as for R_X86_64_PC32, it
1383 // behaves differently because psymval was set to point to
1384 // the PLT entry, rather than the symbol, in Scan::global().
1385 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1389 case elfcpp::R_X86_64_PLTOFF64
:
1392 gold_assert(gsym
->has_plt_offset()
1393 || gsym
->final_value_is_known());
1394 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
1395 got_address
= target
->got_section(NULL
, NULL
)->address();
1396 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
1397 addend
- got_address
);
1400 case elfcpp::R_X86_64_GOT32
:
1401 gold_assert(have_got_offset
);
1402 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1405 case elfcpp::R_X86_64_GOTPC32
:
1408 elfcpp::Elf_types
<64>::Elf_Addr value
;
1409 value
= target
->got_plt_section()->address();
1410 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1414 case elfcpp::R_X86_64_GOT64
:
1415 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1416 // Since we always add a PLT entry, this is equivalent.
1417 case elfcpp::R_X86_64_GOTPLT64
:
1418 gold_assert(have_got_offset
);
1419 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1422 case elfcpp::R_X86_64_GOTPC64
:
1425 elfcpp::Elf_types
<64>::Elf_Addr value
;
1426 value
= target
->got_plt_section()->address();
1427 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1431 case elfcpp::R_X86_64_GOTOFF64
:
1433 elfcpp::Elf_types
<64>::Elf_Addr value
;
1434 value
= (psymval
->value(object
, 0)
1435 - target
->got_plt_section()->address());
1436 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1440 case elfcpp::R_X86_64_GOTPCREL
:
1442 gold_assert(have_got_offset
);
1443 elfcpp::Elf_types
<64>::Elf_Addr value
;
1444 value
= target
->got_plt_section()->address() + got_offset
;
1445 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1449 case elfcpp::R_X86_64_GOTPCREL64
:
1451 gold_assert(have_got_offset
);
1452 elfcpp::Elf_types
<64>::Elf_Addr value
;
1453 value
= target
->got_plt_section()->address() + got_offset
;
1454 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1458 case elfcpp::R_X86_64_COPY
:
1459 case elfcpp::R_X86_64_GLOB_DAT
:
1460 case elfcpp::R_X86_64_JUMP_SLOT
:
1461 case elfcpp::R_X86_64_RELATIVE
:
1462 // These are outstanding tls relocs, which are unexpected when linking
1463 case elfcpp::R_X86_64_TPOFF64
:
1464 case elfcpp::R_X86_64_DTPMOD64
:
1465 case elfcpp::R_X86_64_TLSDESC
:
1466 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1467 _("unexpected reloc %u in object file"),
1471 // These are initial tls relocs, which are expected when linking
1472 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1473 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1474 case elfcpp::R_X86_64_TLSDESC_CALL
:
1475 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1476 case elfcpp::R_X86_64_DTPOFF32
:
1477 case elfcpp::R_X86_64_DTPOFF64
:
1478 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1479 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1480 this->relocate_tls(relinfo
, relnum
, rela
, r_type
, gsym
, psymval
, view
,
1481 address
, view_size
);
1484 case elfcpp::R_X86_64_SIZE32
:
1485 case elfcpp::R_X86_64_SIZE64
:
1487 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1488 _("unsupported reloc %u"),
1496 // Perform a TLS relocation.
1499 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1501 const elfcpp::Rela
<64, false>& rela
,
1502 unsigned int r_type
,
1503 const Sized_symbol
<64>* gsym
,
1504 const Symbol_value
<64>* psymval
,
1505 unsigned char* view
,
1506 elfcpp::Elf_types
<64>::Elf_Addr
,
1509 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1510 if (tls_segment
== NULL
)
1512 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1513 _("TLS reloc but no TLS segment"));
1517 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1519 const bool is_final
= (gsym
== NULL
1520 ? !parameters
->output_is_position_independent()
1521 : gsym
->final_value_is_known());
1522 const tls::Tls_optimization optimized_type
1523 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1526 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1527 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1528 case elfcpp::R_X86_64_TLSDESC_CALL
:
1529 if (optimized_type
== tls::TLSOPT_TO_LE
)
1531 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1532 rela
, r_type
, value
, view
,
1536 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1537 _("unsupported reloc %u"), r_type
);
1540 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1541 if (optimized_type
== tls::TLSOPT_TO_LE
)
1543 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1544 value
, view
, view_size
);
1547 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1548 _("unsupported reloc %u"), r_type
);
1551 case elfcpp::R_X86_64_DTPOFF32
:
1552 if (optimized_type
== tls::TLSOPT_TO_LE
)
1553 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1555 value
= value
- tls_segment
->vaddr();
1556 Relocate_functions
<64, false>::rel32(view
, value
);
1559 case elfcpp::R_X86_64_DTPOFF64
:
1560 if (optimized_type
== tls::TLSOPT_TO_LE
)
1561 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1563 value
= value
- tls_segment
->vaddr();
1564 Relocate_functions
<64, false>::rel64(view
, value
);
1567 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1568 if (optimized_type
== tls::TLSOPT_TO_LE
)
1570 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1571 rela
, r_type
, value
, view
,
1575 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1576 _("unsupported reloc type %u"),
1580 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1581 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1582 Relocate_functions
<64, false>::rel32(view
, value
);
1587 // Do a relocation in which we convert a TLS General-Dynamic to a
1591 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
1593 Output_segment
* tls_segment
,
1594 const elfcpp::Rela
<64, false>& rela
,
1596 elfcpp::Elf_types
<64>::Elf_Addr value
,
1597 unsigned char* view
,
1600 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1601 // .word 0x6666; rex64; call __tls_get_addr
1602 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1604 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
1605 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
1607 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1608 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
1609 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1610 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
1612 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1614 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1615 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
1617 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1619 this->skip_call_tls_get_addr_
= true;
1623 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
1626 const elfcpp::Rela
<64, false>& rela
,
1628 elfcpp::Elf_types
<64>::Elf_Addr
,
1629 unsigned char* view
,
1632 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1633 // ... leq foo@dtpoff(%rax),%reg
1634 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1636 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1637 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
1639 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1640 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
1642 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
1644 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1646 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1648 this->skip_call_tls_get_addr_
= true;
1651 // Do a relocation in which we convert a TLS Initial-Exec to a
1655 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
1657 Output_segment
* tls_segment
,
1658 const elfcpp::Rela
<64, false>& rela
,
1660 elfcpp::Elf_types
<64>::Elf_Addr value
,
1661 unsigned char* view
,
1664 // We need to examine the opcodes to figure out which instruction we
1667 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1668 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1670 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1671 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
1673 unsigned char op1
= view
[-3];
1674 unsigned char op2
= view
[-2];
1675 unsigned char op3
= view
[-1];
1676 unsigned char reg
= op3
>> 3;
1684 view
[-1] = 0xc0 | reg
;
1688 // Special handling for %rsp.
1692 view
[-1] = 0xc0 | reg
;
1700 view
[-1] = 0x80 | reg
| (reg
<< 3);
1703 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1704 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1707 // Relocate section data.
1710 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
1711 unsigned int sh_type
,
1712 const unsigned char* prelocs
,
1714 Output_section
* output_section
,
1715 bool needs_special_offset_handling
,
1716 unsigned char* view
,
1717 elfcpp::Elf_types
<64>::Elf_Addr address
,
1720 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1722 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1723 Target_x86_64::Relocate
>(
1729 needs_special_offset_handling
,
1735 // Return the value to use for a dynamic which requires special
1736 // treatment. This is how we support equality comparisons of function
1737 // pointers across shared library boundaries, as described in the
1738 // processor specific ABI supplement.
1741 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
1743 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1744 return this->plt_section()->address() + gsym
->plt_offset();
1747 // Return a string used to fill a code section with nops to take up
1748 // the specified length.
1751 Target_x86_64::do_code_fill(off_t length
)
1755 // Build a jmpq instruction to skip over the bytes.
1756 unsigned char jmp
[5];
1758 elfcpp::Swap_unaligned
<64, false>::writeval(jmp
+ 1, length
- 5);
1759 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1760 + std::string(length
- 5, '\0'));
1763 // Nop sequences of various lengths.
1764 const char nop1
[1] = { 0x90 }; // nop
1765 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1766 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1767 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1768 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1769 0x00 }; // leal 0(%esi,1),%esi
1770 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1772 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1774 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1775 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1776 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1777 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1779 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1780 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1782 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1783 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1785 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1786 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1787 0x00, 0x00, 0x00, 0x00 };
1788 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1789 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1790 0x27, 0x00, 0x00, 0x00,
1792 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1793 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1794 0xbc, 0x27, 0x00, 0x00,
1796 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1797 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1798 0x90, 0x90, 0x90, 0x90,
1801 const char* nops
[16] = {
1803 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
1804 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
1807 return std::string(nops
[length
], length
);
1810 // The selector for x86_64 object files.
1812 class Target_selector_x86_64
: public Target_selector
1815 Target_selector_x86_64()
1816 : Target_selector(elfcpp::EM_X86_64
, 64, false)
1820 recognize(int machine
, int osabi
, int abiversion
);
1823 Target_x86_64
* target_
;
1826 // Recognize an x86_64 object file when we already know that the machine
1827 // number is EM_X86_64.
1830 Target_selector_x86_64::recognize(int, int, int)
1832 if (this->target_
== NULL
)
1833 this->target_
= new Target_x86_64();
1834 return this->target_
;
1837 Target_selector_x86_64 target_selector_x86_64
;
1839 } // End anonymous namespace.