1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
45 class Output_data_plt_x86_64
;
47 // The x86_64 target class.
49 // http://www.x86-64.org/documentation/abi.pdf
50 // TLS info comes from
51 // http://people.redhat.com/drepper/tls.pdf
52 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
54 class Target_x86_64
: public Sized_target
<64, false>
57 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
58 // uses only Elf64_Rela relocation entries with explicit addends."
59 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
62 : Sized_target
<64, false>(&x86_64_info
),
63 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rela_dyn_(NULL
),
64 copy_relocs_(NULL
), dynbss_(NULL
), got_mod_index_offset_(-1U)
67 // Scan the relocations to look for symbol adjustments.
69 scan_relocs(const General_options
& options
,
72 Sized_relobj
<64, false>* object
,
73 unsigned int data_shndx
,
75 const unsigned char* prelocs
,
77 Output_section
* output_section
,
78 bool needs_special_offset_handling
,
79 size_t local_symbol_count
,
80 const unsigned char* plocal_symbols
);
82 // Finalize the sections.
84 do_finalize_sections(Layout
*);
86 // Return the value to use for a dynamic which requires special
89 do_dynsym_value(const Symbol
*) const;
91 // Relocate a section.
93 relocate_section(const Relocate_info
<64, false>*,
95 const unsigned char* prelocs
,
97 Output_section
* output_section
,
98 bool needs_special_offset_handling
,
100 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
101 section_size_type view_size
);
103 // Scan the relocs during a relocatable link.
105 scan_relocatable_relocs(const General_options
& options
,
106 Symbol_table
* symtab
,
108 Sized_relobj
<64, false>* object
,
109 unsigned int data_shndx
,
110 unsigned int sh_type
,
111 const unsigned char* prelocs
,
113 Output_section
* output_section
,
114 bool needs_special_offset_handling
,
115 size_t local_symbol_count
,
116 const unsigned char* plocal_symbols
,
117 Relocatable_relocs
*);
119 // Relocate a section during a relocatable link.
121 relocate_for_relocatable(const Relocate_info
<64, false>*,
122 unsigned int sh_type
,
123 const unsigned char* prelocs
,
125 Output_section
* output_section
,
126 off_t offset_in_output_section
,
127 const Relocatable_relocs
*,
129 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
130 section_size_type view_size
,
131 unsigned char* reloc_view
,
132 section_size_type reloc_view_size
);
134 // Return a string used to fill a code section with nops.
136 do_code_fill(section_size_type length
) const;
138 // Return whether SYM is defined by the ABI.
140 do_is_defined_by_abi(Symbol
* sym
) const
141 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
143 // Return the size of the GOT section.
147 gold_assert(this->got_
!= NULL
);
148 return this->got_
->data_size();
152 // The class which scans relocations.
157 : issued_non_pic_error_(false)
161 local(const General_options
& options
, Symbol_table
* symtab
,
162 Layout
* layout
, Target_x86_64
* target
,
163 Sized_relobj
<64, false>* object
,
164 unsigned int data_shndx
,
165 Output_section
* output_section
,
166 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
167 const elfcpp::Sym
<64, false>& lsym
);
170 global(const General_options
& options
, Symbol_table
* symtab
,
171 Layout
* layout
, Target_x86_64
* target
,
172 Sized_relobj
<64, false>* object
,
173 unsigned int data_shndx
,
174 Output_section
* output_section
,
175 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
180 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
183 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
187 check_non_pic(Relobj
*, unsigned int r_type
);
189 // Whether we have issued an error about a non-PIC compilation.
190 bool issued_non_pic_error_
;
193 // The class which implements relocation.
198 : skip_call_tls_get_addr_(false)
203 if (this->skip_call_tls_get_addr_
)
205 // FIXME: This needs to specify the location somehow.
206 gold_error(_("missing expected TLS relocation"));
210 // Do a relocation. Return false if the caller should not issue
211 // any warnings about this relocation.
213 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, size_t relnum
,
214 const elfcpp::Rela
<64, false>&,
215 unsigned int r_type
, const Sized_symbol
<64>*,
216 const Symbol_value
<64>*,
217 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
221 // Do a TLS relocation.
223 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
224 size_t relnum
, const elfcpp::Rela
<64, false>&,
225 unsigned int r_type
, const Sized_symbol
<64>*,
226 const Symbol_value
<64>*,
227 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
230 // Do a TLS General-Dynamic to Local-Exec transition.
232 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
233 Output_segment
* tls_segment
,
234 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
235 elfcpp::Elf_types
<64>::Elf_Addr value
,
237 section_size_type view_size
);
239 // Do a TLS General-Dynamic to Local-Exec transition.
241 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
242 Output_segment
* tls_segment
,
243 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
244 elfcpp::Elf_types
<64>::Elf_Addr value
,
246 section_size_type view_size
);
248 // Do a TLS Local-Dynamic to Local-Exec transition.
250 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
251 Output_segment
* tls_segment
,
252 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
253 elfcpp::Elf_types
<64>::Elf_Addr value
,
255 section_size_type view_size
);
257 // Do a TLS Initial-Exec to Local-Exec transition.
259 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
260 Output_segment
* tls_segment
,
261 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
262 elfcpp::Elf_types
<64>::Elf_Addr value
,
264 section_size_type view_size
);
266 // This is set if we should skip the next reloc, which should be a
267 // PLT32 reloc against ___tls_get_addr.
268 bool skip_call_tls_get_addr_
;
271 // A class which returns the size required for a relocation type,
272 // used while scanning relocs during a relocatable link.
273 class Relocatable_size_for_reloc
277 get_size_for_reloc(unsigned int, Relobj
*);
280 // Adjust TLS relocation type based on the options and whether this
281 // is a local symbol.
282 static tls::Tls_optimization
283 optimize_tls_reloc(bool is_final
, int r_type
);
285 // Get the GOT section, creating it if necessary.
286 Output_data_got
<64, false>*
287 got_section(Symbol_table
*, Layout
*);
289 // Get the GOT PLT section.
291 got_plt_section() const
293 gold_assert(this->got_plt_
!= NULL
);
294 return this->got_plt_
;
297 // Create a PLT entry for a global symbol.
299 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
301 // Create a GOT entry for the TLS module index.
303 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
304 Sized_relobj
<64, false>* object
);
306 // Get the PLT section.
307 Output_data_plt_x86_64
*
310 gold_assert(this->plt_
!= NULL
);
314 // Get the dynamic reloc section, creating it if necessary.
316 rela_dyn_section(Layout
*);
318 // Return true if the symbol may need a COPY relocation.
319 // References from an executable object to non-function symbols
320 // defined in a dynamic object may need a COPY relocation.
322 may_need_copy_reloc(Symbol
* gsym
)
324 return (!parameters
->options().shared()
325 && gsym
->is_from_dynobj()
326 && gsym
->type() != elfcpp::STT_FUNC
);
329 // Copy a relocation against a global symbol.
331 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
332 Sized_relobj
<64, false>*, unsigned int,
333 Output_section
*, Symbol
*, const elfcpp::Rela
<64, false>&);
335 // Information about this specific target which we pass to the
336 // general Target structure.
337 static const Target::Target_info x86_64_info
;
341 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
342 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
343 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
344 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
348 Output_data_got
<64, false>* got_
;
350 Output_data_plt_x86_64
* plt_
;
351 // The GOT PLT section.
352 Output_data_space
* got_plt_
;
353 // The dynamic reloc section.
354 Reloc_section
* rela_dyn_
;
355 // Relocs saved to avoid a COPY reloc.
356 Copy_relocs
<64, false>* copy_relocs_
;
357 // Space for variables copied with a COPY reloc.
358 Output_data_space
* dynbss_
;
359 // Offset of the GOT entry for the TLS module index;
360 unsigned int got_mod_index_offset_
;
363 const Target::Target_info
Target_x86_64::x86_64_info
=
366 false, // is_big_endian
367 elfcpp::EM_X86_64
, // machine_code
368 false, // has_make_symbol
369 false, // has_resolve
370 true, // has_code_fill
371 true, // is_default_stack_executable
372 "/lib/ld64.so.1", // program interpreter
373 0x400000, // default_text_segment_address
374 0x1000, // abi_pagesize (overridable by -z max-page-size)
375 0x1000 // common_pagesize (overridable by -z common-page-size)
378 // Get the GOT section, creating it if necessary.
380 Output_data_got
<64, false>*
381 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
383 if (this->got_
== NULL
)
385 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
387 this->got_
= new Output_data_got
<64, false>();
389 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
390 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
393 // The old GNU linker creates a .got.plt section. We just
394 // create another set of data in the .got section. Note that we
395 // always create a PLT if we create a GOT, although the PLT
397 this->got_plt_
= new Output_data_space(8);
398 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
399 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
402 // The first three entries are reserved.
403 this->got_plt_
->set_current_data_size(3 * 8);
405 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
406 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
408 0, 0, elfcpp::STT_OBJECT
,
410 elfcpp::STV_HIDDEN
, 0,
417 // Get the dynamic reloc section, creating it if necessary.
419 Target_x86_64::Reloc_section
*
420 Target_x86_64::rela_dyn_section(Layout
* layout
)
422 if (this->rela_dyn_
== NULL
)
424 gold_assert(layout
!= NULL
);
425 this->rela_dyn_
= new Reloc_section();
426 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
427 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
429 return this->rela_dyn_
;
432 // A class to handle the PLT data.
434 class Output_data_plt_x86_64
: public Output_section_data
437 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
439 Output_data_plt_x86_64(Layout
*, Output_data_space
*);
441 // Add an entry to the PLT.
443 add_entry(Symbol
* gsym
);
445 // Return the .rel.plt section data.
448 { return this->rel_
; }
452 do_adjust_output_section(Output_section
* os
);
455 // The size of an entry in the PLT.
456 static const int plt_entry_size
= 16;
458 // The first entry in the PLT.
459 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
460 // procedure linkage table for both programs and shared objects."
461 static unsigned char first_plt_entry
[plt_entry_size
];
463 // Other entries in the PLT for an executable.
464 static unsigned char plt_entry
[plt_entry_size
];
466 // Set the final size.
468 set_final_data_size()
469 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
471 // Write out the PLT data.
473 do_write(Output_file
*);
475 // The reloc section.
477 // The .got.plt section.
478 Output_data_space
* got_plt_
;
479 // The number of PLT entries.
483 // Create the PLT section. The ordinary .got section is an argument,
484 // since we need to refer to the start. We also create our own .got
485 // section just for PLT entries.
487 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
488 Output_data_space
* got_plt
)
489 : Output_section_data(8), got_plt_(got_plt
), count_(0)
491 this->rel_
= new Reloc_section();
492 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
493 elfcpp::SHF_ALLOC
, this->rel_
);
497 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
499 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
500 // linker, and so do we.
504 // Add an entry to the PLT.
507 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
509 gold_assert(!gsym
->has_plt_offset());
511 // Note that when setting the PLT offset we skip the initial
512 // reserved PLT entry.
513 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
517 section_offset_type got_offset
= this->got_plt_
->current_data_size();
519 // Every PLT entry needs a GOT entry which points back to the PLT
520 // entry (this will be changed by the dynamic linker, normally
521 // lazily when the function is called).
522 this->got_plt_
->set_current_data_size(got_offset
+ 8);
524 // Every PLT entry needs a reloc.
525 gsym
->set_needs_dynsym_entry();
526 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
529 // Note that we don't need to save the symbol. The contents of the
530 // PLT are independent of which symbols are used. The symbols only
531 // appear in the relocations.
534 // The first entry in the PLT for an executable.
536 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
538 // From AMD64 ABI Draft 0.98, page 76
539 0xff, 0x35, // pushq contents of memory address
540 0, 0, 0, 0, // replaced with address of .got + 8
541 0xff, 0x25, // jmp indirect
542 0, 0, 0, 0, // replaced with address of .got + 16
543 0x90, 0x90, 0x90, 0x90 // noop (x4)
546 // Subsequent entries in the PLT for an executable.
548 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
550 // From AMD64 ABI Draft 0.98, page 76
551 0xff, 0x25, // jmpq indirect
552 0, 0, 0, 0, // replaced with address of symbol in .got
553 0x68, // pushq immediate
554 0, 0, 0, 0, // replaced with offset into relocation table
555 0xe9, // jmpq relative
556 0, 0, 0, 0 // replaced with offset to start of .plt
559 // Write out the PLT. This uses the hand-coded instructions above,
560 // and adjusts them as needed. This is specified by the AMD64 ABI.
563 Output_data_plt_x86_64::do_write(Output_file
* of
)
565 const off_t offset
= this->offset();
566 const section_size_type oview_size
=
567 convert_to_section_size_type(this->data_size());
568 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
570 const off_t got_file_offset
= this->got_plt_
->offset();
571 const section_size_type got_size
=
572 convert_to_section_size_type(this->got_plt_
->data_size());
573 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
576 unsigned char* pov
= oview
;
578 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
579 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
581 memcpy(pov
, first_plt_entry
, plt_entry_size
);
582 // We do a jmp relative to the PC at the end of this instruction.
583 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 8
584 - (plt_address
+ 6));
585 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 16
586 - (plt_address
+ 12));
587 pov
+= plt_entry_size
;
589 unsigned char* got_pov
= got_view
;
591 memset(got_pov
, 0, 24);
594 unsigned int plt_offset
= plt_entry_size
;
595 unsigned int got_offset
= 24;
596 const unsigned int count
= this->count_
;
597 for (unsigned int plt_index
= 0;
600 pov
+= plt_entry_size
,
602 plt_offset
+= plt_entry_size
,
605 // Set and adjust the PLT entry itself.
606 memcpy(pov
, plt_entry
, plt_entry_size
);
607 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
608 (got_address
+ got_offset
609 - (plt_address
+ plt_offset
612 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
613 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
614 - (plt_offset
+ plt_entry_size
));
616 // Set the entry in the GOT.
617 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
620 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
621 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
623 of
->write_output_view(offset
, oview_size
, oview
);
624 of
->write_output_view(got_file_offset
, got_size
, got_view
);
627 // Create a PLT entry for a global symbol.
630 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
633 if (gsym
->has_plt_offset())
636 if (this->plt_
== NULL
)
638 // Create the GOT sections first.
639 this->got_section(symtab
, layout
);
641 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_plt_
);
642 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
644 | elfcpp::SHF_EXECINSTR
),
648 this->plt_
->add_entry(gsym
);
651 // Create a GOT entry for the TLS module index.
654 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
655 Sized_relobj
<64, false>* object
)
657 if (this->got_mod_index_offset_
== -1U)
659 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
660 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
661 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
662 unsigned int got_offset
= got
->add_constant(0);
663 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
665 got
->add_constant(0);
666 this->got_mod_index_offset_
= got_offset
;
668 return this->got_mod_index_offset_
;
671 // Handle a relocation against a non-function symbol defined in a
672 // dynamic object. The traditional way to handle this is to generate
673 // a COPY relocation to copy the variable at runtime from the shared
674 // object into the executable's data segment. However, this is
675 // undesirable in general, as if the size of the object changes in the
676 // dynamic object, the executable will no longer work correctly. If
677 // this relocation is in a writable section, then we can create a
678 // dynamic reloc and the dynamic linker will resolve it to the correct
679 // address at runtime. However, we do not want do that if the
680 // relocation is in a read-only section, as it would prevent the
681 // readonly segment from being shared. And if we have to eventually
682 // generate a COPY reloc, then any dynamic relocations will be
683 // useless. So this means that if this is a writable section, we need
684 // to save the relocation until we see whether we have to create a
685 // COPY relocation for this symbol for any other relocation.
688 Target_x86_64::copy_reloc(const General_options
* options
,
689 Symbol_table
* symtab
,
691 Sized_relobj
<64, false>* object
,
692 unsigned int data_shndx
,
693 Output_section
* output_section
,
695 const elfcpp::Rela
<64, false>& rela
)
697 Sized_symbol
<64>* ssym
= symtab
->get_sized_symbol
<64>(gsym
);
699 if (!Copy_relocs
<64, false>::need_copy_reloc(options
, object
,
702 // So far we do not need a COPY reloc. Save this relocation.
703 // If it turns out that we never need a COPY reloc for this
704 // symbol, then we will emit the relocation.
705 if (this->copy_relocs_
== NULL
)
706 this->copy_relocs_
= new Copy_relocs
<64, false>();
707 this->copy_relocs_
->save(ssym
, object
, data_shndx
, output_section
, rela
);
711 // Allocate space for this symbol in the .bss section.
713 elfcpp::Elf_types
<64>::Elf_WXword symsize
= ssym
->symsize();
715 // There is no defined way to determine the required alignment
716 // of the symbol. We pick the alignment based on the size. We
717 // set an arbitrary maximum of 256.
719 for (align
= 1; align
< 512; align
<<= 1)
720 if ((symsize
& align
) != 0)
723 if (this->dynbss_
== NULL
)
725 this->dynbss_
= new Output_data_space(align
);
726 layout
->add_output_section_data(".bss",
729 | elfcpp::SHF_WRITE
),
733 Output_data_space
* dynbss
= this->dynbss_
;
735 if (align
> dynbss
->addralign())
736 dynbss
->set_space_alignment(align
);
738 section_size_type dynbss_size
= dynbss
->current_data_size();
739 dynbss_size
= align_address(dynbss_size
, align
);
740 section_size_type offset
= dynbss_size
;
741 dynbss
->set_current_data_size(dynbss_size
+ symsize
);
743 symtab
->define_with_copy_reloc(ssym
, dynbss
, offset
);
745 // Add the COPY reloc.
746 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
747 rela_dyn
->add_global(ssym
, elfcpp::R_X86_64_COPY
, dynbss
, offset
, 0);
752 // Optimize the TLS relocation type based on what we know about the
753 // symbol. IS_FINAL is true if the final address of this symbol is
754 // known at link time.
756 tls::Tls_optimization
757 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
759 // If we are generating a shared library, then we can't do anything
761 if (parameters
->options().shared())
762 return tls::TLSOPT_NONE
;
766 case elfcpp::R_X86_64_TLSGD
:
767 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
768 case elfcpp::R_X86_64_TLSDESC_CALL
:
769 // These are General-Dynamic which permits fully general TLS
770 // access. Since we know that we are generating an executable,
771 // we can convert this to Initial-Exec. If we also know that
772 // this is a local symbol, we can further switch to Local-Exec.
774 return tls::TLSOPT_TO_LE
;
775 return tls::TLSOPT_TO_IE
;
777 case elfcpp::R_X86_64_TLSLD
:
778 // This is Local-Dynamic, which refers to a local symbol in the
779 // dynamic TLS block. Since we know that we generating an
780 // executable, we can switch to Local-Exec.
781 return tls::TLSOPT_TO_LE
;
783 case elfcpp::R_X86_64_DTPOFF32
:
784 case elfcpp::R_X86_64_DTPOFF64
:
785 // Another Local-Dynamic reloc.
786 return tls::TLSOPT_TO_LE
;
788 case elfcpp::R_X86_64_GOTTPOFF
:
789 // These are Initial-Exec relocs which get the thread offset
790 // from the GOT. If we know that we are linking against the
791 // local symbol, we can switch to Local-Exec, which links the
792 // thread offset into the instruction.
794 return tls::TLSOPT_TO_LE
;
795 return tls::TLSOPT_NONE
;
797 case elfcpp::R_X86_64_TPOFF32
:
798 // When we already have Local-Exec, there is nothing further we
800 return tls::TLSOPT_NONE
;
807 // Report an unsupported relocation against a local symbol.
810 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
813 gold_error(_("%s: unsupported reloc %u against local symbol"),
814 object
->name().c_str(), r_type
);
817 // We are about to emit a dynamic relocation of type R_TYPE. If the
818 // dynamic linker does not support it, issue an error. The GNU linker
819 // only issues a non-PIC error for an allocated read-only section.
820 // Here we know the section is allocated, but we don't know that it is
821 // read-only. But we check for all the relocation types which the
822 // glibc dynamic linker supports, so it seems appropriate to issue an
823 // error even if the section is not read-only.
826 Target_x86_64::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
)
830 // These are the relocation types supported by glibc for x86_64.
831 case elfcpp::R_X86_64_RELATIVE
:
832 case elfcpp::R_X86_64_GLOB_DAT
:
833 case elfcpp::R_X86_64_JUMP_SLOT
:
834 case elfcpp::R_X86_64_DTPMOD64
:
835 case elfcpp::R_X86_64_DTPOFF64
:
836 case elfcpp::R_X86_64_TPOFF64
:
837 case elfcpp::R_X86_64_64
:
838 case elfcpp::R_X86_64_32
:
839 case elfcpp::R_X86_64_PC32
:
840 case elfcpp::R_X86_64_COPY
:
844 // This prevents us from issuing more than one error per reloc
845 // section. But we can still wind up issuing more than one
846 // error per object file.
847 if (this->issued_non_pic_error_
)
849 object
->error(_("requires unsupported dynamic reloc; "
850 "recompile with -fPIC"));
851 this->issued_non_pic_error_
= true;
854 case elfcpp::R_X86_64_NONE
:
859 // Scan a relocation for a local symbol.
862 Target_x86_64::Scan::local(const General_options
&,
863 Symbol_table
* symtab
,
865 Target_x86_64
* target
,
866 Sized_relobj
<64, false>* object
,
867 unsigned int data_shndx
,
868 Output_section
* output_section
,
869 const elfcpp::Rela
<64, false>& reloc
,
871 const elfcpp::Sym
<64, false>& lsym
)
875 case elfcpp::R_X86_64_NONE
:
876 case elfcpp::R_386_GNU_VTINHERIT
:
877 case elfcpp::R_386_GNU_VTENTRY
:
880 case elfcpp::R_X86_64_64
:
881 // If building a shared library (or a position-independent
882 // executable), we need to create a dynamic relocation for this
883 // location. The relocation applied at link time will apply the
884 // link-time value, so we flag the location with an
885 // R_X86_64_RELATIVE relocation so the dynamic loader can
886 // relocate it easily.
887 if (parameters
->options().output_is_position_independent())
889 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
890 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
891 rela_dyn
->add_local_relative(object
, r_sym
,
892 elfcpp::R_X86_64_RELATIVE
,
893 output_section
, data_shndx
,
894 reloc
.get_r_offset(),
895 reloc
.get_r_addend());
899 case elfcpp::R_X86_64_32
:
900 case elfcpp::R_X86_64_32S
:
901 case elfcpp::R_X86_64_16
:
902 case elfcpp::R_X86_64_8
:
903 // If building a shared library (or a position-independent
904 // executable), we need to create a dynamic relocation for this
905 // location. We can't use an R_X86_64_RELATIVE relocation
906 // because that is always a 64-bit relocation.
907 if (parameters
->options().output_is_position_independent())
909 this->check_non_pic(object
, r_type
);
911 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
912 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
914 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
915 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
916 data_shndx
, reloc
.get_r_offset(),
917 reloc
.get_r_addend());
921 gold_assert(lsym
.get_st_value() == 0);
922 rela_dyn
->add_local_section(object
, lsym
.get_st_shndx(),
923 r_type
, output_section
,
924 data_shndx
, reloc
.get_r_offset(),
925 reloc
.get_r_addend());
930 case elfcpp::R_X86_64_PC64
:
931 case elfcpp::R_X86_64_PC32
:
932 case elfcpp::R_X86_64_PC16
:
933 case elfcpp::R_X86_64_PC8
:
936 case elfcpp::R_X86_64_PLT32
:
937 // Since we know this is a local symbol, we can handle this as a
941 case elfcpp::R_X86_64_GOTPC32
:
942 case elfcpp::R_X86_64_GOTOFF64
:
943 case elfcpp::R_X86_64_GOTPC64
:
944 case elfcpp::R_X86_64_PLTOFF64
:
945 // We need a GOT section.
946 target
->got_section(symtab
, layout
);
947 // For PLTOFF64, we'd normally want a PLT section, but since we
948 // know this is a local symbol, no PLT is needed.
951 case elfcpp::R_X86_64_GOT64
:
952 case elfcpp::R_X86_64_GOT32
:
953 case elfcpp::R_X86_64_GOTPCREL64
:
954 case elfcpp::R_X86_64_GOTPCREL
:
955 case elfcpp::R_X86_64_GOTPLT64
:
957 // The symbol requires a GOT entry.
958 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
959 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
960 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
962 // If we are generating a shared object, we need to add a
963 // dynamic relocation for this symbol's GOT entry.
964 if (parameters
->options().output_is_position_independent())
966 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
967 // R_X86_64_RELATIVE assumes a 64-bit relocation.
968 if (r_type
!= elfcpp::R_X86_64_GOT32
)
969 rela_dyn
->add_local_relative(
970 object
, r_sym
, elfcpp::R_X86_64_RELATIVE
, got
,
971 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
974 this->check_non_pic(object
, r_type
);
976 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
978 object
, r_sym
, r_type
, got
,
979 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
983 // For GOTPLT64, we'd normally want a PLT section, but since
984 // we know this is a local symbol, no PLT is needed.
988 case elfcpp::R_X86_64_COPY
:
989 case elfcpp::R_X86_64_GLOB_DAT
:
990 case elfcpp::R_X86_64_JUMP_SLOT
:
991 case elfcpp::R_X86_64_RELATIVE
:
992 // These are outstanding tls relocs, which are unexpected when linking
993 case elfcpp::R_X86_64_TPOFF64
:
994 case elfcpp::R_X86_64_DTPMOD64
:
995 case elfcpp::R_X86_64_TLSDESC
:
996 gold_error(_("%s: unexpected reloc %u in object file"),
997 object
->name().c_str(), r_type
);
1000 // These are initial tls relocs, which are expected when linking
1001 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1002 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1003 case elfcpp::R_X86_64_TLSDESC_CALL
:
1004 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1005 case elfcpp::R_X86_64_DTPOFF32
:
1006 case elfcpp::R_X86_64_DTPOFF64
:
1007 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1008 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1010 bool output_is_shared
= parameters
->options().shared();
1011 const tls::Tls_optimization optimized_type
1012 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
1015 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1016 if (optimized_type
== tls::TLSOPT_NONE
)
1018 // Create a pair of GOT entries for the module index and
1019 // dtv-relative offset.
1020 Output_data_got
<64, false>* got
1021 = target
->got_section(symtab
, layout
);
1022 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1023 got
->add_local_pair_with_rela(object
, r_sym
,
1024 lsym
.get_st_shndx(),
1026 target
->rela_dyn_section(layout
),
1027 elfcpp::R_X86_64_DTPMOD64
, 0);
1029 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1030 unsupported_reloc_local(object
, r_type
);
1033 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1034 case elfcpp::R_X86_64_TLSDESC_CALL
:
1035 // FIXME: If not relaxing to LE, we need to generate
1036 // a GOT entry with a R_x86_64_TLSDESC reloc.
1037 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1038 unsupported_reloc_local(object
, r_type
);
1041 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1042 if (optimized_type
== tls::TLSOPT_NONE
)
1044 // Create a GOT entry for the module index.
1045 target
->got_mod_index_entry(symtab
, layout
, object
);
1047 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1048 unsupported_reloc_local(object
, r_type
);
1051 case elfcpp::R_X86_64_DTPOFF32
:
1052 case elfcpp::R_X86_64_DTPOFF64
:
1055 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1056 layout
->set_has_static_tls();
1057 if (optimized_type
== tls::TLSOPT_NONE
)
1059 // Create a GOT entry for the tp-relative offset.
1060 Output_data_got
<64, false>* got
1061 = target
->got_section(symtab
, layout
);
1062 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1063 got
->add_local_with_rela(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1064 target
->rela_dyn_section(layout
),
1065 elfcpp::R_X86_64_TPOFF64
);
1067 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1068 unsupported_reloc_local(object
, r_type
);
1071 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1072 layout
->set_has_static_tls();
1073 if (output_is_shared
)
1074 unsupported_reloc_local(object
, r_type
);
1083 case elfcpp::R_X86_64_SIZE32
:
1084 case elfcpp::R_X86_64_SIZE64
:
1086 gold_error(_("%s: unsupported reloc %u against local symbol"),
1087 object
->name().c_str(), r_type
);
1093 // Report an unsupported relocation against a global symbol.
1096 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
1097 unsigned int r_type
,
1100 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1101 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1104 // Scan a relocation for a global symbol.
1107 Target_x86_64::Scan::global(const General_options
& options
,
1108 Symbol_table
* symtab
,
1110 Target_x86_64
* target
,
1111 Sized_relobj
<64, false>* object
,
1112 unsigned int data_shndx
,
1113 Output_section
* output_section
,
1114 const elfcpp::Rela
<64, false>& reloc
,
1115 unsigned int r_type
,
1120 case elfcpp::R_X86_64_NONE
:
1121 case elfcpp::R_386_GNU_VTINHERIT
:
1122 case elfcpp::R_386_GNU_VTENTRY
:
1125 case elfcpp::R_X86_64_64
:
1126 case elfcpp::R_X86_64_32
:
1127 case elfcpp::R_X86_64_32S
:
1128 case elfcpp::R_X86_64_16
:
1129 case elfcpp::R_X86_64_8
:
1131 // Make a PLT entry if necessary.
1132 if (gsym
->needs_plt_entry())
1134 target
->make_plt_entry(symtab
, layout
, gsym
);
1135 // Since this is not a PC-relative relocation, we may be
1136 // taking the address of a function. In that case we need to
1137 // set the entry in the dynamic symbol table to the address of
1139 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1140 gsym
->set_needs_dynsym_value();
1142 // Make a dynamic relocation if necessary.
1143 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1145 if (target
->may_need_copy_reloc(gsym
))
1147 target
->copy_reloc(&options
, symtab
, layout
, object
,
1148 data_shndx
, output_section
, gsym
, reloc
);
1150 else if (r_type
== elfcpp::R_X86_64_64
1151 && gsym
->can_use_relative_reloc(false))
1153 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1154 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1155 output_section
, object
,
1156 data_shndx
, reloc
.get_r_offset(),
1157 reloc
.get_r_addend());
1161 this->check_non_pic(object
, r_type
);
1162 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1163 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1164 data_shndx
, reloc
.get_r_offset(),
1165 reloc
.get_r_addend());
1171 case elfcpp::R_X86_64_PC64
:
1172 case elfcpp::R_X86_64_PC32
:
1173 case elfcpp::R_X86_64_PC16
:
1174 case elfcpp::R_X86_64_PC8
:
1176 // Make a PLT entry if necessary.
1177 if (gsym
->needs_plt_entry())
1178 target
->make_plt_entry(symtab
, layout
, gsym
);
1179 // Make a dynamic relocation if necessary.
1180 int flags
= Symbol::NON_PIC_REF
;
1181 if (gsym
->type() == elfcpp::STT_FUNC
)
1182 flags
|= Symbol::FUNCTION_CALL
;
1183 if (gsym
->needs_dynamic_reloc(flags
))
1185 if (target
->may_need_copy_reloc(gsym
))
1187 target
->copy_reloc(&options
, symtab
, layout
, object
,
1188 data_shndx
, output_section
, gsym
, reloc
);
1192 this->check_non_pic(object
, r_type
);
1193 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1194 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1195 data_shndx
, reloc
.get_r_offset(),
1196 reloc
.get_r_addend());
1202 case elfcpp::R_X86_64_GOT64
:
1203 case elfcpp::R_X86_64_GOT32
:
1204 case elfcpp::R_X86_64_GOTPCREL64
:
1205 case elfcpp::R_X86_64_GOTPCREL
:
1206 case elfcpp::R_X86_64_GOTPLT64
:
1208 // The symbol requires a GOT entry.
1209 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1210 if (gsym
->final_value_is_known())
1211 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1214 // If this symbol is not fully resolved, we need to add a
1215 // dynamic relocation for it.
1216 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1217 if (gsym
->is_from_dynobj()
1218 || gsym
->is_undefined()
1219 || gsym
->is_preemptible())
1220 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1221 elfcpp::R_X86_64_GLOB_DAT
);
1224 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1225 rela_dyn
->add_global_relative(
1226 gsym
, elfcpp::R_X86_64_RELATIVE
, got
,
1227 gsym
->got_offset(GOT_TYPE_STANDARD
), 0);
1230 // For GOTPLT64, we also need a PLT entry (but only if the
1231 // symbol is not fully resolved).
1232 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1233 && !gsym
->final_value_is_known())
1234 target
->make_plt_entry(symtab
, layout
, gsym
);
1238 case elfcpp::R_X86_64_PLT32
:
1239 // If the symbol is fully resolved, this is just a PC32 reloc.
1240 // Otherwise we need a PLT entry.
1241 if (gsym
->final_value_is_known())
1243 // If building a shared library, we can also skip the PLT entry
1244 // if the symbol is defined in the output file and is protected
1246 if (gsym
->is_defined()
1247 && !gsym
->is_from_dynobj()
1248 && !gsym
->is_preemptible())
1250 target
->make_plt_entry(symtab
, layout
, gsym
);
1253 case elfcpp::R_X86_64_GOTPC32
:
1254 case elfcpp::R_X86_64_GOTOFF64
:
1255 case elfcpp::R_X86_64_GOTPC64
:
1256 case elfcpp::R_X86_64_PLTOFF64
:
1257 // We need a GOT section.
1258 target
->got_section(symtab
, layout
);
1259 // For PLTOFF64, we also need a PLT entry (but only if the
1260 // symbol is not fully resolved).
1261 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1262 && !gsym
->final_value_is_known())
1263 target
->make_plt_entry(symtab
, layout
, gsym
);
1266 case elfcpp::R_X86_64_COPY
:
1267 case elfcpp::R_X86_64_GLOB_DAT
:
1268 case elfcpp::R_X86_64_JUMP_SLOT
:
1269 case elfcpp::R_X86_64_RELATIVE
:
1270 // These are outstanding tls relocs, which are unexpected when linking
1271 case elfcpp::R_X86_64_TPOFF64
:
1272 case elfcpp::R_X86_64_DTPMOD64
:
1273 case elfcpp::R_X86_64_TLSDESC
:
1274 gold_error(_("%s: unexpected reloc %u in object file"),
1275 object
->name().c_str(), r_type
);
1278 // These are initial tls relocs, which are expected for global()
1279 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1280 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1281 case elfcpp::R_X86_64_TLSDESC_CALL
:
1282 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1283 case elfcpp::R_X86_64_DTPOFF32
:
1284 case elfcpp::R_X86_64_DTPOFF64
:
1285 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1286 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1288 const bool is_final
= gsym
->final_value_is_known();
1289 const tls::Tls_optimization optimized_type
1290 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1293 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1294 if (optimized_type
== tls::TLSOPT_NONE
)
1296 // Create a pair of GOT entries for the module index and
1297 // dtv-relative offset.
1298 Output_data_got
<64, false>* got
1299 = target
->got_section(symtab
, layout
);
1300 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_PAIR
,
1301 target
->rela_dyn_section(layout
),
1302 elfcpp::R_X86_64_DTPMOD64
,
1303 elfcpp::R_X86_64_DTPOFF64
);
1305 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1307 // Create a GOT entry for the tp-relative offset.
1308 Output_data_got
<64, false>* got
1309 = target
->got_section(symtab
, layout
);
1310 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1311 target
->rela_dyn_section(layout
),
1312 elfcpp::R_X86_64_TPOFF64
);
1314 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1315 unsupported_reloc_global(object
, r_type
, gsym
);
1318 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1319 case elfcpp::R_X86_64_TLSDESC_CALL
:
1320 // FIXME: If not relaxing to LE, we need to generate
1321 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1322 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1323 unsupported_reloc_global(object
, r_type
, gsym
);
1326 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1327 if (optimized_type
== tls::TLSOPT_NONE
)
1329 // Create a GOT entry for the module index.
1330 target
->got_mod_index_entry(symtab
, layout
, object
);
1332 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1333 unsupported_reloc_global(object
, r_type
, gsym
);
1336 case elfcpp::R_X86_64_DTPOFF32
:
1337 case elfcpp::R_X86_64_DTPOFF64
:
1340 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1341 layout
->set_has_static_tls();
1342 if (optimized_type
== tls::TLSOPT_NONE
)
1344 // Create a GOT entry for the tp-relative offset.
1345 Output_data_got
<64, false>* got
1346 = target
->got_section(symtab
, layout
);
1347 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1348 target
->rela_dyn_section(layout
),
1349 elfcpp::R_X86_64_TPOFF64
);
1351 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1352 unsupported_reloc_global(object
, r_type
, gsym
);
1355 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1356 layout
->set_has_static_tls();
1357 if (parameters
->options().shared())
1358 unsupported_reloc_local(object
, r_type
);
1367 case elfcpp::R_X86_64_SIZE32
:
1368 case elfcpp::R_X86_64_SIZE64
:
1370 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1371 object
->name().c_str(), r_type
,
1372 gsym
->demangled_name().c_str());
1377 // Scan relocations for a section.
1380 Target_x86_64::scan_relocs(const General_options
& options
,
1381 Symbol_table
* symtab
,
1383 Sized_relobj
<64, false>* object
,
1384 unsigned int data_shndx
,
1385 unsigned int sh_type
,
1386 const unsigned char* prelocs
,
1388 Output_section
* output_section
,
1389 bool needs_special_offset_handling
,
1390 size_t local_symbol_count
,
1391 const unsigned char* plocal_symbols
)
1393 if (sh_type
== elfcpp::SHT_REL
)
1395 gold_error(_("%s: unsupported REL reloc section"),
1396 object
->name().c_str());
1400 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1401 Target_x86_64::Scan
>(
1411 needs_special_offset_handling
,
1416 // Finalize the sections.
1419 Target_x86_64::do_finalize_sections(Layout
* layout
)
1421 // Fill in some more dynamic tags.
1422 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1425 if (this->got_plt_
!= NULL
)
1426 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1428 if (this->plt_
!= NULL
)
1430 const Output_data
* od
= this->plt_
->rel_plt();
1431 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1432 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1433 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1436 if (this->rela_dyn_
!= NULL
)
1438 const Output_data
* od
= this->rela_dyn_
;
1439 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1440 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1441 odyn
->add_constant(elfcpp::DT_RELAENT
,
1442 elfcpp::Elf_sizes
<64>::rela_size
);
1445 if (!parameters
->options().shared())
1447 // The value of the DT_DEBUG tag is filled in by the dynamic
1448 // linker at run time, and used by the debugger.
1449 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1453 // Emit any relocs we saved in an attempt to avoid generating COPY
1455 if (this->copy_relocs_
== NULL
)
1457 if (this->copy_relocs_
->any_to_emit())
1459 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1460 this->copy_relocs_
->emit(rela_dyn
);
1462 delete this->copy_relocs_
;
1463 this->copy_relocs_
= NULL
;
1466 // Perform a relocation.
1469 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1470 Target_x86_64
* target
,
1472 const elfcpp::Rela
<64, false>& rela
,
1473 unsigned int r_type
,
1474 const Sized_symbol
<64>* gsym
,
1475 const Symbol_value
<64>* psymval
,
1476 unsigned char* view
,
1477 elfcpp::Elf_types
<64>::Elf_Addr address
,
1478 section_size_type view_size
)
1480 if (this->skip_call_tls_get_addr_
)
1482 if (r_type
!= elfcpp::R_X86_64_PLT32
1484 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1486 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1487 _("missing expected TLS relocation"));
1491 this->skip_call_tls_get_addr_
= false;
1496 // Pick the value to use for symbols defined in shared objects.
1497 Symbol_value
<64> symval
;
1499 && (gsym
->is_from_dynobj()
1500 || (parameters
->options().shared()
1501 && (gsym
->is_undefined() || gsym
->is_preemptible())))
1502 && gsym
->has_plt_offset())
1504 symval
.set_output_value(target
->plt_section()->address()
1505 + gsym
->plt_offset());
1509 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1510 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1512 // Get the GOT offset if needed.
1513 // The GOT pointer points to the end of the GOT section.
1514 // We need to subtract the size of the GOT section to get
1515 // the actual offset to use in the relocation.
1516 bool have_got_offset
= false;
1517 unsigned int got_offset
= 0;
1520 case elfcpp::R_X86_64_GOT32
:
1521 case elfcpp::R_X86_64_GOT64
:
1522 case elfcpp::R_X86_64_GOTPLT64
:
1523 case elfcpp::R_X86_64_GOTPCREL
:
1524 case elfcpp::R_X86_64_GOTPCREL64
:
1527 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1528 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
1532 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1533 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1534 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1535 - target
->got_size());
1537 have_got_offset
= true;
1546 case elfcpp::R_X86_64_NONE
:
1547 case elfcpp::R_386_GNU_VTINHERIT
:
1548 case elfcpp::R_386_GNU_VTENTRY
:
1551 case elfcpp::R_X86_64_64
:
1552 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1555 case elfcpp::R_X86_64_PC64
:
1556 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1560 case elfcpp::R_X86_64_32
:
1561 // FIXME: we need to verify that value + addend fits into 32 bits:
1562 // uint64_t x = value + addend;
1563 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1564 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1565 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1568 case elfcpp::R_X86_64_32S
:
1569 // FIXME: we need to verify that value + addend fits into 32 bits:
1570 // int64_t x = value + addend; // note this quantity is signed!
1571 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1572 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1575 case elfcpp::R_X86_64_PC32
:
1576 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1580 case elfcpp::R_X86_64_16
:
1581 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1584 case elfcpp::R_X86_64_PC16
:
1585 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1589 case elfcpp::R_X86_64_8
:
1590 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1593 case elfcpp::R_X86_64_PC8
:
1594 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1598 case elfcpp::R_X86_64_PLT32
:
1599 gold_assert(gsym
== NULL
1600 || gsym
->has_plt_offset()
1601 || gsym
->final_value_is_known()
1602 || (gsym
->is_defined()
1603 && !gsym
->is_from_dynobj()
1604 && !gsym
->is_preemptible()));
1605 // Note: while this code looks the same as for R_X86_64_PC32, it
1606 // behaves differently because psymval was set to point to
1607 // the PLT entry, rather than the symbol, in Scan::global().
1608 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1612 case elfcpp::R_X86_64_PLTOFF64
:
1615 gold_assert(gsym
->has_plt_offset()
1616 || gsym
->final_value_is_known());
1617 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
1618 got_address
= target
->got_section(NULL
, NULL
)->address();
1619 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
1620 addend
- got_address
);
1623 case elfcpp::R_X86_64_GOT32
:
1624 gold_assert(have_got_offset
);
1625 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1628 case elfcpp::R_X86_64_GOTPC32
:
1631 elfcpp::Elf_types
<64>::Elf_Addr value
;
1632 value
= target
->got_plt_section()->address();
1633 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1637 case elfcpp::R_X86_64_GOT64
:
1638 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1639 // Since we always add a PLT entry, this is equivalent.
1640 case elfcpp::R_X86_64_GOTPLT64
:
1641 gold_assert(have_got_offset
);
1642 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1645 case elfcpp::R_X86_64_GOTPC64
:
1648 elfcpp::Elf_types
<64>::Elf_Addr value
;
1649 value
= target
->got_plt_section()->address();
1650 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1654 case elfcpp::R_X86_64_GOTOFF64
:
1656 elfcpp::Elf_types
<64>::Elf_Addr value
;
1657 value
= (psymval
->value(object
, 0)
1658 - target
->got_plt_section()->address());
1659 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1663 case elfcpp::R_X86_64_GOTPCREL
:
1665 gold_assert(have_got_offset
);
1666 elfcpp::Elf_types
<64>::Elf_Addr value
;
1667 value
= target
->got_plt_section()->address() + got_offset
;
1668 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1672 case elfcpp::R_X86_64_GOTPCREL64
:
1674 gold_assert(have_got_offset
);
1675 elfcpp::Elf_types
<64>::Elf_Addr value
;
1676 value
= target
->got_plt_section()->address() + got_offset
;
1677 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1681 case elfcpp::R_X86_64_COPY
:
1682 case elfcpp::R_X86_64_GLOB_DAT
:
1683 case elfcpp::R_X86_64_JUMP_SLOT
:
1684 case elfcpp::R_X86_64_RELATIVE
:
1685 // These are outstanding tls relocs, which are unexpected when linking
1686 case elfcpp::R_X86_64_TPOFF64
:
1687 case elfcpp::R_X86_64_DTPMOD64
:
1688 case elfcpp::R_X86_64_TLSDESC
:
1689 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1690 _("unexpected reloc %u in object file"),
1694 // These are initial tls relocs, which are expected when linking
1695 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1696 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1697 case elfcpp::R_X86_64_TLSDESC_CALL
:
1698 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1699 case elfcpp::R_X86_64_DTPOFF32
:
1700 case elfcpp::R_X86_64_DTPOFF64
:
1701 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1702 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1703 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
1704 view
, address
, view_size
);
1707 case elfcpp::R_X86_64_SIZE32
:
1708 case elfcpp::R_X86_64_SIZE64
:
1710 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1711 _("unsupported reloc %u"),
1719 // Perform a TLS relocation.
1722 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1723 Target_x86_64
* target
,
1725 const elfcpp::Rela
<64, false>& rela
,
1726 unsigned int r_type
,
1727 const Sized_symbol
<64>* gsym
,
1728 const Symbol_value
<64>* psymval
,
1729 unsigned char* view
,
1730 elfcpp::Elf_types
<64>::Elf_Addr address
,
1731 section_size_type view_size
)
1733 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1735 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1736 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1738 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1740 const bool is_final
= (gsym
== NULL
1741 ? !parameters
->options().output_is_position_independent()
1742 : gsym
->final_value_is_known());
1743 const tls::Tls_optimization optimized_type
1744 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1747 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1748 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1749 case elfcpp::R_X86_64_TLSDESC_CALL
:
1750 if (optimized_type
== tls::TLSOPT_TO_LE
)
1752 gold_assert(tls_segment
!= NULL
);
1753 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1754 rela
, r_type
, value
, view
,
1760 unsigned int got_offset
;
1763 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_PAIR
));
1764 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_PAIR
)
1765 - target
->got_size());
1769 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1770 gold_assert(object
->local_has_got_offset(r_sym
,
1771 GOT_TYPE_TLS_PAIR
));
1772 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_PAIR
)
1773 - target
->got_size());
1775 if (optimized_type
== tls::TLSOPT_TO_IE
)
1777 gold_assert(tls_segment
!= NULL
);
1778 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1779 got_offset
, view
, view_size
);
1782 else if (optimized_type
== tls::TLSOPT_NONE
)
1784 // Relocate the field with the offset of the pair of GOT
1786 value
= target
->got_plt_section()->address() + got_offset
;
1787 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
1792 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1793 _("unsupported reloc %u"), r_type
);
1796 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1797 if (optimized_type
== tls::TLSOPT_TO_LE
)
1799 gold_assert(tls_segment
!= NULL
);
1800 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1801 value
, view
, view_size
);
1804 else if (optimized_type
== tls::TLSOPT_NONE
)
1806 // Relocate the field with the offset of the GOT entry for
1807 // the module index.
1808 unsigned int got_offset
;
1809 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1810 - target
->got_size());
1811 value
= target
->got_plt_section()->address() + got_offset
;
1812 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
1816 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1817 _("unsupported reloc %u"), r_type
);
1820 case elfcpp::R_X86_64_DTPOFF32
:
1821 gold_assert(tls_segment
!= NULL
);
1822 if (optimized_type
== tls::TLSOPT_TO_LE
)
1823 value
-= tls_segment
->memsz();
1824 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1827 case elfcpp::R_X86_64_DTPOFF64
:
1828 gold_assert(tls_segment
!= NULL
);
1829 if (optimized_type
== tls::TLSOPT_TO_LE
)
1830 value
-= tls_segment
->memsz();
1831 Relocate_functions
<64, false>::rela64(view
, value
, 0);
1834 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1835 if (optimized_type
== tls::TLSOPT_TO_LE
)
1837 gold_assert(tls_segment
!= NULL
);
1838 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1839 rela
, r_type
, value
, view
,
1843 else if (optimized_type
== tls::TLSOPT_NONE
)
1845 // Relocate the field with the offset of the GOT entry for
1846 // the tp-relative offset of the symbol.
1847 unsigned int got_offset
;
1850 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
1851 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
1852 - target
->got_size());
1856 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1857 gold_assert(object
->local_has_got_offset(r_sym
,
1858 GOT_TYPE_TLS_OFFSET
));
1859 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
1860 - target
->got_size());
1862 value
= target
->got_plt_section()->address() + got_offset
;
1863 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1866 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1867 _("unsupported reloc type %u"),
1871 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1872 value
-= tls_segment
->memsz();
1873 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1878 // Do a relocation in which we convert a TLS General-Dynamic to an
1882 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
1884 Output_segment
* tls_segment
,
1885 const elfcpp::Rela
<64, false>& rela
,
1887 elfcpp::Elf_types
<64>::Elf_Addr value
,
1888 unsigned char* view
,
1889 section_size_type view_size
)
1891 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1892 // .word 0x6666; rex64; call __tls_get_addr
1893 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
1895 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
1896 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
1898 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1899 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
1900 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1901 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
1903 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
1905 value
-= tls_segment
->memsz();
1906 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
1908 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1910 this->skip_call_tls_get_addr_
= true;
1913 // Do a relocation in which we convert a TLS General-Dynamic to a
1917 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
1919 Output_segment
* tls_segment
,
1920 const elfcpp::Rela
<64, false>& rela
,
1922 elfcpp::Elf_types
<64>::Elf_Addr value
,
1923 unsigned char* view
,
1924 section_size_type view_size
)
1926 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1927 // .word 0x6666; rex64; call __tls_get_addr
1928 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1930 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
1931 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
1933 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1934 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
1935 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1936 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
1938 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1940 value
-= tls_segment
->memsz();
1941 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
1943 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1945 this->skip_call_tls_get_addr_
= true;
1949 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
1952 const elfcpp::Rela
<64, false>& rela
,
1954 elfcpp::Elf_types
<64>::Elf_Addr
,
1955 unsigned char* view
,
1956 section_size_type view_size
)
1958 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1959 // ... leq foo@dtpoff(%rax),%reg
1960 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1962 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1963 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
1965 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1966 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
1968 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
1970 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1972 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1974 this->skip_call_tls_get_addr_
= true;
1977 // Do a relocation in which we convert a TLS Initial-Exec to a
1981 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
1983 Output_segment
* tls_segment
,
1984 const elfcpp::Rela
<64, false>& rela
,
1986 elfcpp::Elf_types
<64>::Elf_Addr value
,
1987 unsigned char* view
,
1988 section_size_type view_size
)
1990 // We need to examine the opcodes to figure out which instruction we
1993 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1994 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1996 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1997 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
1999 unsigned char op1
= view
[-3];
2000 unsigned char op2
= view
[-2];
2001 unsigned char op3
= view
[-1];
2002 unsigned char reg
= op3
>> 3;
2010 view
[-1] = 0xc0 | reg
;
2014 // Special handling for %rsp.
2018 view
[-1] = 0xc0 | reg
;
2026 view
[-1] = 0x80 | reg
| (reg
<< 3);
2029 value
-= tls_segment
->memsz();
2030 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2033 // Relocate section data.
2036 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
2037 unsigned int sh_type
,
2038 const unsigned char* prelocs
,
2040 Output_section
* output_section
,
2041 bool needs_special_offset_handling
,
2042 unsigned char* view
,
2043 elfcpp::Elf_types
<64>::Elf_Addr address
,
2044 section_size_type view_size
)
2046 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2048 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2049 Target_x86_64::Relocate
>(
2055 needs_special_offset_handling
,
2061 // Return the size of a relocation while scanning during a relocatable
2065 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2066 unsigned int r_type
,
2071 case elfcpp::R_X86_64_NONE
:
2072 case elfcpp::R_386_GNU_VTINHERIT
:
2073 case elfcpp::R_386_GNU_VTENTRY
:
2074 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2075 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2076 case elfcpp::R_X86_64_TLSDESC_CALL
:
2077 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2078 case elfcpp::R_X86_64_DTPOFF32
:
2079 case elfcpp::R_X86_64_DTPOFF64
:
2080 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2081 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2084 case elfcpp::R_X86_64_64
:
2085 case elfcpp::R_X86_64_PC64
:
2086 case elfcpp::R_X86_64_GOTOFF64
:
2087 case elfcpp::R_X86_64_GOTPC64
:
2088 case elfcpp::R_X86_64_PLTOFF64
:
2089 case elfcpp::R_X86_64_GOT64
:
2090 case elfcpp::R_X86_64_GOTPCREL64
:
2091 case elfcpp::R_X86_64_GOTPCREL
:
2092 case elfcpp::R_X86_64_GOTPLT64
:
2095 case elfcpp::R_X86_64_32
:
2096 case elfcpp::R_X86_64_32S
:
2097 case elfcpp::R_X86_64_PC32
:
2098 case elfcpp::R_X86_64_PLT32
:
2099 case elfcpp::R_X86_64_GOTPC32
:
2100 case elfcpp::R_X86_64_GOT32
:
2103 case elfcpp::R_X86_64_16
:
2104 case elfcpp::R_X86_64_PC16
:
2107 case elfcpp::R_X86_64_8
:
2108 case elfcpp::R_X86_64_PC8
:
2111 case elfcpp::R_X86_64_COPY
:
2112 case elfcpp::R_X86_64_GLOB_DAT
:
2113 case elfcpp::R_X86_64_JUMP_SLOT
:
2114 case elfcpp::R_X86_64_RELATIVE
:
2115 // These are outstanding tls relocs, which are unexpected when linking
2116 case elfcpp::R_X86_64_TPOFF64
:
2117 case elfcpp::R_X86_64_DTPMOD64
:
2118 case elfcpp::R_X86_64_TLSDESC
:
2119 object
->error(_("unexpected reloc %u in object file"), r_type
);
2122 case elfcpp::R_X86_64_SIZE32
:
2123 case elfcpp::R_X86_64_SIZE64
:
2125 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
2130 // Scan the relocs during a relocatable link.
2133 Target_x86_64::scan_relocatable_relocs(const General_options
& options
,
2134 Symbol_table
* symtab
,
2136 Sized_relobj
<64, false>* object
,
2137 unsigned int data_shndx
,
2138 unsigned int sh_type
,
2139 const unsigned char* prelocs
,
2141 Output_section
* output_section
,
2142 bool needs_special_offset_handling
,
2143 size_t local_symbol_count
,
2144 const unsigned char* plocal_symbols
,
2145 Relocatable_relocs
* rr
)
2147 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2149 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
2150 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2152 gold::scan_relocatable_relocs
<64, false, elfcpp::SHT_RELA
,
2153 Scan_relocatable_relocs
>(
2162 needs_special_offset_handling
,
2168 // Relocate a section during a relocatable link.
2171 Target_x86_64::relocate_for_relocatable(
2172 const Relocate_info
<64, false>* relinfo
,
2173 unsigned int sh_type
,
2174 const unsigned char* prelocs
,
2176 Output_section
* output_section
,
2177 off_t offset_in_output_section
,
2178 const Relocatable_relocs
* rr
,
2179 unsigned char* view
,
2180 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
2181 section_size_type view_size
,
2182 unsigned char* reloc_view
,
2183 section_size_type reloc_view_size
)
2185 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2187 gold::relocate_for_relocatable
<64, false, elfcpp::SHT_RELA
>(
2192 offset_in_output_section
,
2201 // Return the value to use for a dynamic which requires special
2202 // treatment. This is how we support equality comparisons of function
2203 // pointers across shared library boundaries, as described in the
2204 // processor specific ABI supplement.
2207 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
2209 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2210 return this->plt_section()->address() + gsym
->plt_offset();
2213 // Return a string used to fill a code section with nops to take up
2214 // the specified length.
2217 Target_x86_64::do_code_fill(section_size_type length
) const
2221 // Build a jmpq instruction to skip over the bytes.
2222 unsigned char jmp
[5];
2224 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2225 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2226 + std::string(length
- 5, '\0'));
2229 // Nop sequences of various lengths.
2230 const char nop1
[1] = { 0x90 }; // nop
2231 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2232 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2233 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2234 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2235 0x00 }; // leal 0(%esi,1),%esi
2236 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2238 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2240 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2241 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2242 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2243 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2245 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2246 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2248 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2249 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2251 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2252 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2253 0x00, 0x00, 0x00, 0x00 };
2254 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2255 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2256 0x27, 0x00, 0x00, 0x00,
2258 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2259 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2260 0xbc, 0x27, 0x00, 0x00,
2262 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2263 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2264 0x90, 0x90, 0x90, 0x90,
2267 const char* nops
[16] = {
2269 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2270 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2273 return std::string(nops
[length
], length
);
2276 // The selector for x86_64 object files.
2278 class Target_selector_x86_64
: public Target_selector
2281 Target_selector_x86_64()
2282 : Target_selector(elfcpp::EM_X86_64
, 64, false, "elf64-x86-64")
2286 do_instantiate_target()
2287 { return new Target_x86_64(); }
2290 Target_selector_x86_64 target_selector_x86_64
;
2292 } // End anonymous namespace.