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
), got_mod_index_offset_(-1U)
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
,
110 section_size_type view_size
);
112 // Scan the relocs during a relocatable link.
114 scan_relocatable_relocs(const General_options
& options
,
115 Symbol_table
* symtab
,
117 Sized_relobj
<64, false>* object
,
118 unsigned int data_shndx
,
119 unsigned int sh_type
,
120 const unsigned char* prelocs
,
122 Output_section
* output_section
,
123 bool needs_special_offset_handling
,
124 size_t local_symbol_count
,
125 const unsigned char* plocal_symbols
,
126 Relocatable_relocs
*);
128 // Relocate a section during a relocatable link.
130 relocate_for_relocatable(const Relocate_info
<64, false>*,
131 unsigned int sh_type
,
132 const unsigned char* prelocs
,
134 Output_section
* output_section
,
135 off_t offset_in_output_section
,
136 const Relocatable_relocs
*,
138 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
139 section_size_type view_size
,
140 unsigned char* reloc_view
,
141 section_size_type reloc_view_size
);
143 // Return a string used to fill a code section with nops.
145 do_code_fill(section_size_type length
);
147 // Return whether SYM is defined by the ABI.
149 do_is_defined_by_abi(Symbol
* sym
) const
150 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
152 // Return the size of the GOT section.
156 gold_assert(this->got_
!= NULL
);
157 return this->got_
->data_size();
161 // The class which scans relocations.
165 local(const General_options
& options
, Symbol_table
* symtab
,
166 Layout
* layout
, Target_x86_64
* target
,
167 Sized_relobj
<64, false>* object
,
168 unsigned int data_shndx
,
169 Output_section
* output_section
,
170 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
171 const elfcpp::Sym
<64, false>& lsym
);
174 global(const General_options
& options
, Symbol_table
* symtab
,
175 Layout
* layout
, Target_x86_64
* target
,
176 Sized_relobj
<64, false>* object
,
177 unsigned int data_shndx
,
178 Output_section
* output_section
,
179 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
183 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
186 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
190 // The class which implements relocation.
195 : skip_call_tls_get_addr_(false)
200 if (this->skip_call_tls_get_addr_
)
202 // FIXME: This needs to specify the location somehow.
203 gold_error(_("missing expected TLS relocation"));
207 // Do a relocation. Return false if the caller should not issue
208 // any warnings about this relocation.
210 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, size_t relnum
,
211 const elfcpp::Rela
<64, false>&,
212 unsigned int r_type
, const Sized_symbol
<64>*,
213 const Symbol_value
<64>*,
214 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
218 // Do a TLS relocation.
220 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
221 size_t relnum
, const elfcpp::Rela
<64, false>&,
222 unsigned int r_type
, const Sized_symbol
<64>*,
223 const Symbol_value
<64>*,
224 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
227 // Do a TLS General-Dynamic to Local-Exec transition.
229 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
230 Output_segment
* tls_segment
,
231 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
232 elfcpp::Elf_types
<64>::Elf_Addr value
,
234 section_size_type view_size
);
236 // Do a TLS General-Dynamic to Local-Exec transition.
238 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
239 Output_segment
* tls_segment
,
240 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
241 elfcpp::Elf_types
<64>::Elf_Addr value
,
243 section_size_type view_size
);
245 // Do a TLS Local-Dynamic to Local-Exec transition.
247 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
248 Output_segment
* tls_segment
,
249 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
250 elfcpp::Elf_types
<64>::Elf_Addr value
,
252 section_size_type view_size
);
254 // Do a TLS Initial-Exec to Local-Exec transition.
256 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
257 Output_segment
* tls_segment
,
258 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
259 elfcpp::Elf_types
<64>::Elf_Addr value
,
261 section_size_type view_size
);
263 // This is set if we should skip the next reloc, which should be a
264 // PLT32 reloc against ___tls_get_addr.
265 bool skip_call_tls_get_addr_
;
268 // A class which returns the size required for a relocation type,
269 // used while scanning relocs during a relocatable link.
270 class Relocatable_size_for_reloc
274 get_size_for_reloc(unsigned int, Relobj
*);
277 // Adjust TLS relocation type based on the options and whether this
278 // is a local symbol.
279 static tls::Tls_optimization
280 optimize_tls_reloc(bool is_final
, int r_type
);
282 // Get the GOT section, creating it if necessary.
283 Output_data_got
<64, false>*
284 got_section(Symbol_table
*, Layout
*);
286 // Get the GOT PLT section.
288 got_plt_section() const
290 gold_assert(this->got_plt_
!= NULL
);
291 return this->got_plt_
;
294 // Create a PLT entry for a global symbol.
296 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
298 // Create a GOT entry for the TLS module index.
300 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
301 Sized_relobj
<64, false>* object
);
303 // Get the PLT section.
304 Output_data_plt_x86_64
*
307 gold_assert(this->plt_
!= NULL
);
311 // Get the dynamic reloc section, creating it if necessary.
313 rela_dyn_section(Layout
*);
315 // Return true if the symbol may need a COPY relocation.
316 // References from an executable object to non-function symbols
317 // defined in a dynamic object may need a COPY relocation.
319 may_need_copy_reloc(Symbol
* gsym
)
321 return (!parameters
->output_is_shared()
322 && gsym
->is_from_dynobj()
323 && gsym
->type() != elfcpp::STT_FUNC
);
326 // Copy a relocation against a global symbol.
328 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
329 Sized_relobj
<64, false>*, unsigned int,
330 Output_section
*, Symbol
*, const elfcpp::Rela
<64, false>&);
332 // Information about this specific target which we pass to the
333 // general Target structure.
334 static const Target::Target_info x86_64_info
;
337 Output_data_got
<64, false>* got_
;
339 Output_data_plt_x86_64
* plt_
;
340 // The GOT PLT section.
341 Output_data_space
* got_plt_
;
342 // The dynamic reloc section.
343 Reloc_section
* rela_dyn_
;
344 // Relocs saved to avoid a COPY reloc.
345 Copy_relocs
<64, false>* copy_relocs_
;
346 // Space for variables copied with a COPY reloc.
347 Output_data_space
* dynbss_
;
348 // Offset of the GOT entry for the TLS module index;
349 unsigned int got_mod_index_offset_
;
352 const Target::Target_info
Target_x86_64::x86_64_info
=
355 false, // is_big_endian
356 elfcpp::EM_X86_64
, // machine_code
357 false, // has_make_symbol
358 false, // has_resolve
359 true, // has_code_fill
360 true, // is_default_stack_executable
361 "/lib/ld64.so.1", // program interpreter
362 0x400000, // default_text_segment_address
363 0x1000, // abi_pagesize
364 0x1000 // common_pagesize
367 // Get the GOT section, creating it if necessary.
369 Output_data_got
<64, false>*
370 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
372 if (this->got_
== NULL
)
374 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
376 this->got_
= new Output_data_got
<64, false>();
378 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
379 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
382 // The old GNU linker creates a .got.plt section. We just
383 // create another set of data in the .got section. Note that we
384 // always create a PLT if we create a GOT, although the PLT
386 this->got_plt_
= new Output_data_space(8);
387 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
388 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
391 // The first three entries are reserved.
392 this->got_plt_
->set_current_data_size(3 * 8);
394 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
395 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
397 0, 0, elfcpp::STT_OBJECT
,
399 elfcpp::STV_HIDDEN
, 0,
406 // Get the dynamic reloc section, creating it if necessary.
408 Target_x86_64::Reloc_section
*
409 Target_x86_64::rela_dyn_section(Layout
* layout
)
411 if (this->rela_dyn_
== NULL
)
413 gold_assert(layout
!= NULL
);
414 this->rela_dyn_
= new Reloc_section();
415 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
416 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
418 return this->rela_dyn_
;
421 // A class to handle the PLT data.
423 class Output_data_plt_x86_64
: public Output_section_data
426 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
428 Output_data_plt_x86_64(Layout
*, Output_data_space
*);
430 // Add an entry to the PLT.
432 add_entry(Symbol
* gsym
);
434 // Return the .rel.plt section data.
437 { return this->rel_
; }
441 do_adjust_output_section(Output_section
* os
);
444 // The size of an entry in the PLT.
445 static const int plt_entry_size
= 16;
447 // The first entry in the PLT.
448 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
449 // procedure linkage table for both programs and shared objects."
450 static unsigned char first_plt_entry
[plt_entry_size
];
452 // Other entries in the PLT for an executable.
453 static unsigned char plt_entry
[plt_entry_size
];
455 // Set the final size.
457 set_final_data_size()
458 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
460 // Write out the PLT data.
462 do_write(Output_file
*);
464 // The reloc section.
466 // The .got.plt section.
467 Output_data_space
* got_plt_
;
468 // The number of PLT entries.
472 // Create the PLT section. The ordinary .got section is an argument,
473 // since we need to refer to the start. We also create our own .got
474 // section just for PLT entries.
476 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
477 Output_data_space
* got_plt
)
478 : Output_section_data(8), got_plt_(got_plt
), count_(0)
480 this->rel_
= new Reloc_section();
481 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
482 elfcpp::SHF_ALLOC
, this->rel_
);
486 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
488 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
489 // linker, and so do we.
493 // Add an entry to the PLT.
496 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
498 gold_assert(!gsym
->has_plt_offset());
500 // Note that when setting the PLT offset we skip the initial
501 // reserved PLT entry.
502 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
506 section_offset_type got_offset
= this->got_plt_
->current_data_size();
508 // Every PLT entry needs a GOT entry which points back to the PLT
509 // entry (this will be changed by the dynamic linker, normally
510 // lazily when the function is called).
511 this->got_plt_
->set_current_data_size(got_offset
+ 8);
513 // Every PLT entry needs a reloc.
514 gsym
->set_needs_dynsym_entry();
515 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
518 // Note that we don't need to save the symbol. The contents of the
519 // PLT are independent of which symbols are used. The symbols only
520 // appear in the relocations.
523 // The first entry in the PLT for an executable.
525 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
527 // From AMD64 ABI Draft 0.98, page 76
528 0xff, 0x35, // pushq contents of memory address
529 0, 0, 0, 0, // replaced with address of .got + 8
530 0xff, 0x25, // jmp indirect
531 0, 0, 0, 0, // replaced with address of .got + 16
532 0x90, 0x90, 0x90, 0x90 // noop (x4)
535 // Subsequent entries in the PLT for an executable.
537 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
539 // From AMD64 ABI Draft 0.98, page 76
540 0xff, 0x25, // jmpq indirect
541 0, 0, 0, 0, // replaced with address of symbol in .got
542 0x68, // pushq immediate
543 0, 0, 0, 0, // replaced with offset into relocation table
544 0xe9, // jmpq relative
545 0, 0, 0, 0 // replaced with offset to start of .plt
548 // Write out the PLT. This uses the hand-coded instructions above,
549 // and adjusts them as needed. This is specified by the AMD64 ABI.
552 Output_data_plt_x86_64::do_write(Output_file
* of
)
554 const off_t offset
= this->offset();
555 const section_size_type oview_size
=
556 convert_to_section_size_type(this->data_size());
557 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
559 const off_t got_file_offset
= this->got_plt_
->offset();
560 const section_size_type got_size
=
561 convert_to_section_size_type(this->got_plt_
->data_size());
562 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
565 unsigned char* pov
= oview
;
567 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
568 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
570 memcpy(pov
, first_plt_entry
, plt_entry_size
);
571 // We do a jmp relative to the PC at the end of this instruction.
572 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 8
573 - (plt_address
+ 6));
574 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 16
575 - (plt_address
+ 12));
576 pov
+= plt_entry_size
;
578 unsigned char* got_pov
= got_view
;
580 memset(got_pov
, 0, 24);
583 unsigned int plt_offset
= plt_entry_size
;
584 unsigned int got_offset
= 24;
585 const unsigned int count
= this->count_
;
586 for (unsigned int plt_index
= 0;
589 pov
+= plt_entry_size
,
591 plt_offset
+= plt_entry_size
,
594 // Set and adjust the PLT entry itself.
595 memcpy(pov
, plt_entry
, plt_entry_size
);
596 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
597 (got_address
+ got_offset
598 - (plt_address
+ plt_offset
601 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
602 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
603 - (plt_offset
+ plt_entry_size
));
605 // Set the entry in the GOT.
606 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
609 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
610 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
612 of
->write_output_view(offset
, oview_size
, oview
);
613 of
->write_output_view(got_file_offset
, got_size
, got_view
);
616 // Create a PLT entry for a global symbol.
619 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
622 if (gsym
->has_plt_offset())
625 if (this->plt_
== NULL
)
627 // Create the GOT sections first.
628 this->got_section(symtab
, layout
);
630 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_plt_
);
631 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
633 | elfcpp::SHF_EXECINSTR
),
637 this->plt_
->add_entry(gsym
);
640 // Create a GOT entry for the TLS module index.
643 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
644 Sized_relobj
<64, false>* object
)
646 if (this->got_mod_index_offset_
== -1U)
648 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
649 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
650 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
651 unsigned int got_offset
= got
->add_constant(0);
652 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
654 got
->add_constant(0);
655 this->got_mod_index_offset_
= got_offset
;
657 return this->got_mod_index_offset_
;
660 // Handle a relocation against a non-function symbol defined in a
661 // dynamic object. The traditional way to handle this is to generate
662 // a COPY relocation to copy the variable at runtime from the shared
663 // object into the executable's data segment. However, this is
664 // undesirable in general, as if the size of the object changes in the
665 // dynamic object, the executable will no longer work correctly. If
666 // this relocation is in a writable section, then we can create a
667 // dynamic reloc and the dynamic linker will resolve it to the correct
668 // address at runtime. However, we do not want do that if the
669 // relocation is in a read-only section, as it would prevent the
670 // readonly segment from being shared. And if we have to eventually
671 // generate a COPY reloc, then any dynamic relocations will be
672 // useless. So this means that if this is a writable section, we need
673 // to save the relocation until we see whether we have to create a
674 // COPY relocation for this symbol for any other relocation.
677 Target_x86_64::copy_reloc(const General_options
* options
,
678 Symbol_table
* symtab
,
680 Sized_relobj
<64, false>* object
,
681 unsigned int data_shndx
,
682 Output_section
* output_section
,
684 const elfcpp::Rela
<64, false>& rela
)
686 Sized_symbol
<64>* ssym
;
687 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(64) (gsym
690 if (!Copy_relocs
<64, false>::need_copy_reloc(options
, object
,
693 // So far we do not need a COPY reloc. Save this relocation.
694 // If it turns out that we never need a COPY reloc for this
695 // symbol, then we will emit the relocation.
696 if (this->copy_relocs_
== NULL
)
697 this->copy_relocs_
= new Copy_relocs
<64, false>();
698 this->copy_relocs_
->save(ssym
, object
, data_shndx
, output_section
, rela
);
702 // Allocate space for this symbol in the .bss section.
704 elfcpp::Elf_types
<64>::Elf_WXword symsize
= ssym
->symsize();
706 // There is no defined way to determine the required alignment
707 // of the symbol. We pick the alignment based on the size. We
708 // set an arbitrary maximum of 256.
710 for (align
= 1; align
< 512; align
<<= 1)
711 if ((symsize
& align
) != 0)
714 if (this->dynbss_
== NULL
)
716 this->dynbss_
= new Output_data_space(align
);
717 layout
->add_output_section_data(".bss",
720 | elfcpp::SHF_WRITE
),
724 Output_data_space
* dynbss
= this->dynbss_
;
726 if (align
> dynbss
->addralign())
727 dynbss
->set_space_alignment(align
);
729 section_size_type dynbss_size
= dynbss
->current_data_size();
730 dynbss_size
= align_address(dynbss_size
, align
);
731 section_size_type offset
= dynbss_size
;
732 dynbss
->set_current_data_size(dynbss_size
+ symsize
);
734 symtab
->define_with_copy_reloc(ssym
, dynbss
, offset
);
736 // Add the COPY reloc.
737 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
738 rela_dyn
->add_global(ssym
, elfcpp::R_X86_64_COPY
, dynbss
, offset
, 0);
743 // Optimize the TLS relocation type based on what we know about the
744 // symbol. IS_FINAL is true if the final address of this symbol is
745 // known at link time.
747 tls::Tls_optimization
748 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
750 // If we are generating a shared library, then we can't do anything
752 if (parameters
->output_is_shared())
753 return tls::TLSOPT_NONE
;
757 case elfcpp::R_X86_64_TLSGD
:
758 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
759 case elfcpp::R_X86_64_TLSDESC_CALL
:
760 // These are General-Dynamic which permits fully general TLS
761 // access. Since we know that we are generating an executable,
762 // we can convert this to Initial-Exec. If we also know that
763 // this is a local symbol, we can further switch to Local-Exec.
765 return tls::TLSOPT_TO_LE
;
766 return tls::TLSOPT_TO_IE
;
768 case elfcpp::R_X86_64_TLSLD
:
769 // This is Local-Dynamic, which refers to a local symbol in the
770 // dynamic TLS block. Since we know that we generating an
771 // executable, we can switch to Local-Exec.
772 return tls::TLSOPT_TO_LE
;
774 case elfcpp::R_X86_64_DTPOFF32
:
775 case elfcpp::R_X86_64_DTPOFF64
:
776 // Another Local-Dynamic reloc.
777 return tls::TLSOPT_TO_LE
;
779 case elfcpp::R_X86_64_GOTTPOFF
:
780 // These are Initial-Exec relocs which get the thread offset
781 // from the GOT. If we know that we are linking against the
782 // local symbol, we can switch to Local-Exec, which links the
783 // thread offset into the instruction.
785 return tls::TLSOPT_TO_LE
;
786 return tls::TLSOPT_NONE
;
788 case elfcpp::R_X86_64_TPOFF32
:
789 // When we already have Local-Exec, there is nothing further we
791 return tls::TLSOPT_NONE
;
798 // Report an unsupported relocation against a local symbol.
801 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
804 gold_error(_("%s: unsupported reloc %u against local symbol"),
805 object
->name().c_str(), r_type
);
808 // Scan a relocation for a local symbol.
811 Target_x86_64::Scan::local(const General_options
&,
812 Symbol_table
* symtab
,
814 Target_x86_64
* target
,
815 Sized_relobj
<64, false>* object
,
816 unsigned int data_shndx
,
817 Output_section
* output_section
,
818 const elfcpp::Rela
<64, false>& reloc
,
820 const elfcpp::Sym
<64, false>& lsym
)
824 case elfcpp::R_X86_64_NONE
:
825 case elfcpp::R_386_GNU_VTINHERIT
:
826 case elfcpp::R_386_GNU_VTENTRY
:
829 case elfcpp::R_X86_64_64
:
830 // If building a shared library (or a position-independent
831 // executable), we need to create a dynamic relocation for
832 // this location. The relocation applied at link time will
833 // apply the link-time value, so we flag the location with
834 // an R_386_RELATIVE relocation so the dynamic loader can
835 // relocate it easily.
836 if (parameters
->output_is_position_independent())
838 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
839 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
840 rela_dyn
->add_local_relative(object
, r_sym
,
841 elfcpp::R_X86_64_RELATIVE
,
842 output_section
, data_shndx
,
843 reloc
.get_r_offset(),
844 reloc
.get_r_addend());
848 case elfcpp::R_X86_64_32
:
849 case elfcpp::R_X86_64_32S
:
850 case elfcpp::R_X86_64_16
:
851 case elfcpp::R_X86_64_8
:
852 // If building a shared library (or a position-independent
853 // executable), we need to create a dynamic relocation for
854 // this location. The relocation applied at link time will
855 // apply the link-time value, so we flag the location with
856 // an R_386_RELATIVE relocation so the dynamic loader can
857 // relocate it easily.
858 if (parameters
->output_is_position_independent())
860 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
861 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
862 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
863 data_shndx
, reloc
.get_r_offset(),
864 reloc
.get_r_addend());
868 case elfcpp::R_X86_64_PC64
:
869 case elfcpp::R_X86_64_PC32
:
870 case elfcpp::R_X86_64_PC16
:
871 case elfcpp::R_X86_64_PC8
:
874 case elfcpp::R_X86_64_PLT32
:
875 // Since we know this is a local symbol, we can handle this as a
879 case elfcpp::R_X86_64_GOTPC32
:
880 case elfcpp::R_X86_64_GOTOFF64
:
881 case elfcpp::R_X86_64_GOTPC64
:
882 case elfcpp::R_X86_64_PLTOFF64
:
883 // We need a GOT section.
884 target
->got_section(symtab
, layout
);
885 // For PLTOFF64, we'd normally want a PLT section, but since we
886 // know this is a local symbol, no PLT is needed.
889 case elfcpp::R_X86_64_GOT64
:
890 case elfcpp::R_X86_64_GOT32
:
891 case elfcpp::R_X86_64_GOTPCREL64
:
892 case elfcpp::R_X86_64_GOTPCREL
:
893 case elfcpp::R_X86_64_GOTPLT64
:
895 // The symbol requires a GOT entry.
896 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
897 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
898 if (got
->add_local(object
, r_sym
))
900 // If we are generating a shared object, we need to add a
901 // dynamic relocation for this symbol's GOT entry.
902 if (parameters
->output_is_position_independent())
904 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
905 // R_X86_64_RELATIVE assumes a 64-bit relocation.
906 if (r_type
!= elfcpp::R_X86_64_GOT32
)
907 rela_dyn
->add_local_relative(object
, r_sym
,
908 elfcpp::R_X86_64_RELATIVE
, got
,
909 object
->local_got_offset(r_sym
),
912 rela_dyn
->add_local(object
, r_sym
, r_type
,
913 got
, object
->local_got_offset(r_sym
), 0);
916 // For GOTPLT64, we'd normally want a PLT section, but since
917 // we know this is a local symbol, no PLT is needed.
921 case elfcpp::R_X86_64_COPY
:
922 case elfcpp::R_X86_64_GLOB_DAT
:
923 case elfcpp::R_X86_64_JUMP_SLOT
:
924 case elfcpp::R_X86_64_RELATIVE
:
925 // These are outstanding tls relocs, which are unexpected when linking
926 case elfcpp::R_X86_64_TPOFF64
:
927 case elfcpp::R_X86_64_DTPMOD64
:
928 case elfcpp::R_X86_64_TLSDESC
:
929 gold_error(_("%s: unexpected reloc %u in object file"),
930 object
->name().c_str(), r_type
);
933 // These are initial tls relocs, which are expected when linking
934 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
935 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
936 case elfcpp::R_X86_64_TLSDESC_CALL
:
937 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
938 case elfcpp::R_X86_64_DTPOFF32
:
939 case elfcpp::R_X86_64_DTPOFF64
:
940 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
941 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
943 bool output_is_shared
= parameters
->output_is_shared();
944 const tls::Tls_optimization optimized_type
945 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
948 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
949 if (optimized_type
== tls::TLSOPT_NONE
)
951 // Create a pair of GOT entries for the module index and
952 // dtv-relative offset.
953 Output_data_got
<64, false>* got
954 = target
->got_section(symtab
, layout
);
955 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
956 got
->add_local_tls_with_rela(object
, r_sym
,
957 lsym
.get_st_shndx(), true,
958 target
->rela_dyn_section(layout
),
959 elfcpp::R_X86_64_DTPMOD64
);
961 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
962 unsupported_reloc_local(object
, r_type
);
965 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
966 case elfcpp::R_X86_64_TLSDESC_CALL
:
967 // FIXME: If not relaxing to LE, we need to generate
968 // a GOT entry with a R_x86_64_TLSDESC reloc.
969 if (optimized_type
!= tls::TLSOPT_TO_LE
)
970 unsupported_reloc_local(object
, r_type
);
973 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
974 if (optimized_type
== tls::TLSOPT_NONE
)
976 // Create a GOT entry for the module index.
977 target
->got_mod_index_entry(symtab
, layout
, object
);
979 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
980 unsupported_reloc_local(object
, r_type
);
983 case elfcpp::R_X86_64_DTPOFF32
:
984 case elfcpp::R_X86_64_DTPOFF64
:
987 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
988 layout
->set_has_static_tls();
989 if (optimized_type
== tls::TLSOPT_NONE
)
991 // Create a GOT entry for the tp-relative offset.
992 Output_data_got
<64, false>* got
993 = target
->got_section(symtab
, layout
);
994 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
995 got
->add_local_with_rela(object
, r_sym
,
996 target
->rela_dyn_section(layout
),
997 elfcpp::R_X86_64_TPOFF64
);
999 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1000 unsupported_reloc_local(object
, r_type
);
1003 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1004 layout
->set_has_static_tls();
1005 if (output_is_shared
)
1006 unsupported_reloc_local(object
, r_type
);
1015 case elfcpp::R_X86_64_SIZE32
:
1016 case elfcpp::R_X86_64_SIZE64
:
1018 gold_error(_("%s: unsupported reloc %u against local symbol"),
1019 object
->name().c_str(), r_type
);
1025 // Report an unsupported relocation against a global symbol.
1028 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
1029 unsigned int r_type
,
1032 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1033 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1036 // Scan a relocation for a global symbol.
1039 Target_x86_64::Scan::global(const General_options
& options
,
1040 Symbol_table
* symtab
,
1042 Target_x86_64
* target
,
1043 Sized_relobj
<64, false>* object
,
1044 unsigned int data_shndx
,
1045 Output_section
* output_section
,
1046 const elfcpp::Rela
<64, false>& reloc
,
1047 unsigned int r_type
,
1052 case elfcpp::R_X86_64_NONE
:
1053 case elfcpp::R_386_GNU_VTINHERIT
:
1054 case elfcpp::R_386_GNU_VTENTRY
:
1057 case elfcpp::R_X86_64_64
:
1058 case elfcpp::R_X86_64_32
:
1059 case elfcpp::R_X86_64_32S
:
1060 case elfcpp::R_X86_64_16
:
1061 case elfcpp::R_X86_64_8
:
1063 // Make a PLT entry if necessary.
1064 if (gsym
->needs_plt_entry())
1066 target
->make_plt_entry(symtab
, layout
, gsym
);
1067 // Since this is not a PC-relative relocation, we may be
1068 // taking the address of a function. In that case we need to
1069 // set the entry in the dynamic symbol table to the address of
1071 if (gsym
->is_from_dynobj() && !parameters
->output_is_shared())
1072 gsym
->set_needs_dynsym_value();
1074 // Make a dynamic relocation if necessary.
1075 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1077 if (target
->may_need_copy_reloc(gsym
))
1079 target
->copy_reloc(&options
, symtab
, layout
, object
,
1080 data_shndx
, output_section
, gsym
, reloc
);
1082 else if (r_type
== elfcpp::R_X86_64_64
1083 && gsym
->can_use_relative_reloc(false))
1085 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1086 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1087 output_section
, object
,
1088 data_shndx
, reloc
.get_r_offset(),
1089 reloc
.get_r_addend());
1093 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1094 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1095 data_shndx
, reloc
.get_r_offset(),
1096 reloc
.get_r_addend());
1102 case elfcpp::R_X86_64_PC64
:
1103 case elfcpp::R_X86_64_PC32
:
1104 case elfcpp::R_X86_64_PC16
:
1105 case elfcpp::R_X86_64_PC8
:
1107 // Make a PLT entry if necessary.
1108 if (gsym
->needs_plt_entry())
1109 target
->make_plt_entry(symtab
, layout
, gsym
);
1110 // Make a dynamic relocation if necessary.
1111 int flags
= Symbol::NON_PIC_REF
;
1112 if (gsym
->type() == elfcpp::STT_FUNC
)
1113 flags
|= Symbol::FUNCTION_CALL
;
1114 if (gsym
->needs_dynamic_reloc(flags
))
1116 if (target
->may_need_copy_reloc(gsym
))
1118 target
->copy_reloc(&options
, symtab
, layout
, object
,
1119 data_shndx
, output_section
, gsym
, reloc
);
1123 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1124 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1125 data_shndx
, reloc
.get_r_offset(),
1126 reloc
.get_r_addend());
1132 case elfcpp::R_X86_64_GOT64
:
1133 case elfcpp::R_X86_64_GOT32
:
1134 case elfcpp::R_X86_64_GOTPCREL64
:
1135 case elfcpp::R_X86_64_GOTPCREL
:
1136 case elfcpp::R_X86_64_GOTPLT64
:
1138 // The symbol requires a GOT entry.
1139 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1140 if (gsym
->final_value_is_known())
1141 got
->add_global(gsym
);
1144 // If this symbol is not fully resolved, we need to add a
1145 // dynamic relocation for it.
1146 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1147 if (gsym
->is_from_dynobj() || gsym
->is_preemptible())
1148 got
->add_global_with_rela(gsym
, rela_dyn
,
1149 elfcpp::R_X86_64_GLOB_DAT
);
1152 if (got
->add_global(gsym
))
1153 rela_dyn
->add_global_relative(gsym
,
1154 elfcpp::R_X86_64_RELATIVE
,
1155 got
, gsym
->got_offset(), 0);
1158 // For GOTPLT64, we also need a PLT entry (but only if the
1159 // symbol is not fully resolved).
1160 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1161 && !gsym
->final_value_is_known())
1162 target
->make_plt_entry(symtab
, layout
, gsym
);
1166 case elfcpp::R_X86_64_PLT32
:
1167 // If the symbol is fully resolved, this is just a PC32 reloc.
1168 // Otherwise we need a PLT entry.
1169 if (gsym
->final_value_is_known())
1171 // If building a shared library, we can also skip the PLT entry
1172 // if the symbol is defined in the output file and is protected
1174 if (gsym
->is_defined()
1175 && !gsym
->is_from_dynobj()
1176 && !gsym
->is_preemptible())
1178 target
->make_plt_entry(symtab
, layout
, gsym
);
1181 case elfcpp::R_X86_64_GOTPC32
:
1182 case elfcpp::R_X86_64_GOTOFF64
:
1183 case elfcpp::R_X86_64_GOTPC64
:
1184 case elfcpp::R_X86_64_PLTOFF64
:
1185 // We need a GOT section.
1186 target
->got_section(symtab
, layout
);
1187 // For PLTOFF64, we also need a PLT entry (but only if the
1188 // symbol is not fully resolved).
1189 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1190 && !gsym
->final_value_is_known())
1191 target
->make_plt_entry(symtab
, layout
, gsym
);
1194 case elfcpp::R_X86_64_COPY
:
1195 case elfcpp::R_X86_64_GLOB_DAT
:
1196 case elfcpp::R_X86_64_JUMP_SLOT
:
1197 case elfcpp::R_X86_64_RELATIVE
:
1198 // These are outstanding tls relocs, which are unexpected when linking
1199 case elfcpp::R_X86_64_TPOFF64
:
1200 case elfcpp::R_X86_64_DTPMOD64
:
1201 case elfcpp::R_X86_64_TLSDESC
:
1202 gold_error(_("%s: unexpected reloc %u in object file"),
1203 object
->name().c_str(), r_type
);
1206 // These are initial tls relocs, which are expected for global()
1207 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1208 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1209 case elfcpp::R_X86_64_TLSDESC_CALL
:
1210 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1211 case elfcpp::R_X86_64_DTPOFF32
:
1212 case elfcpp::R_X86_64_DTPOFF64
:
1213 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1214 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1216 const bool is_final
= gsym
->final_value_is_known();
1217 const tls::Tls_optimization optimized_type
1218 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1221 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1222 if (optimized_type
== tls::TLSOPT_NONE
)
1224 // Create a pair of GOT entries for the module index and
1225 // dtv-relative offset.
1226 Output_data_got
<64, false>* got
1227 = target
->got_section(symtab
, layout
);
1228 got
->add_global_tls_with_rela(gsym
,
1229 target
->rela_dyn_section(layout
),
1230 elfcpp::R_X86_64_DTPMOD64
,
1231 elfcpp::R_X86_64_DTPOFF64
);
1233 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1235 // Create a GOT entry for the tp-relative offset.
1236 Output_data_got
<64, false>* got
1237 = target
->got_section(symtab
, layout
);
1238 got
->add_global_with_rela(gsym
,
1239 target
->rela_dyn_section(layout
),
1240 elfcpp::R_X86_64_TPOFF64
);
1242 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1243 unsupported_reloc_global(object
, r_type
, gsym
);
1246 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1247 case elfcpp::R_X86_64_TLSDESC_CALL
:
1248 // FIXME: If not relaxing to LE, we need to generate
1249 // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1250 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1251 unsupported_reloc_global(object
, r_type
, gsym
);
1254 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1255 if (optimized_type
== tls::TLSOPT_NONE
)
1257 // Create a GOT entry for the module index.
1258 target
->got_mod_index_entry(symtab
, layout
, object
);
1260 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1261 unsupported_reloc_global(object
, r_type
, gsym
);
1264 case elfcpp::R_X86_64_DTPOFF32
:
1265 case elfcpp::R_X86_64_DTPOFF64
:
1268 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1269 layout
->set_has_static_tls();
1270 if (optimized_type
== tls::TLSOPT_NONE
)
1272 // Create a GOT entry for the tp-relative offset.
1273 Output_data_got
<64, false>* got
1274 = target
->got_section(symtab
, layout
);
1275 got
->add_global_with_rela(gsym
,
1276 target
->rela_dyn_section(layout
),
1277 elfcpp::R_X86_64_TPOFF64
);
1279 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1280 unsupported_reloc_global(object
, r_type
, gsym
);
1283 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1284 layout
->set_has_static_tls();
1285 if (parameters
->output_is_shared())
1286 unsupported_reloc_local(object
, r_type
);
1295 case elfcpp::R_X86_64_SIZE32
:
1296 case elfcpp::R_X86_64_SIZE64
:
1298 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1299 object
->name().c_str(), r_type
,
1300 gsym
->demangled_name().c_str());
1305 // Scan relocations for a section.
1308 Target_x86_64::scan_relocs(const General_options
& options
,
1309 Symbol_table
* symtab
,
1311 Sized_relobj
<64, false>* object
,
1312 unsigned int data_shndx
,
1313 unsigned int sh_type
,
1314 const unsigned char* prelocs
,
1316 Output_section
* output_section
,
1317 bool needs_special_offset_handling
,
1318 size_t local_symbol_count
,
1319 const unsigned char* plocal_symbols
)
1321 if (sh_type
== elfcpp::SHT_REL
)
1323 gold_error(_("%s: unsupported REL reloc section"),
1324 object
->name().c_str());
1328 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1329 Target_x86_64::Scan
>(
1339 needs_special_offset_handling
,
1344 // Finalize the sections.
1347 Target_x86_64::do_finalize_sections(Layout
* layout
)
1349 // Fill in some more dynamic tags.
1350 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1353 if (this->got_plt_
!= NULL
)
1354 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1356 if (this->plt_
!= NULL
)
1358 const Output_data
* od
= this->plt_
->rel_plt();
1359 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1360 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1361 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1364 if (this->rela_dyn_
!= NULL
)
1366 const Output_data
* od
= this->rela_dyn_
;
1367 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1368 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1369 odyn
->add_constant(elfcpp::DT_RELAENT
,
1370 elfcpp::Elf_sizes
<64>::rela_size
);
1373 if (!parameters
->output_is_shared())
1375 // The value of the DT_DEBUG tag is filled in by the dynamic
1376 // linker at run time, and used by the debugger.
1377 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1381 // Emit any relocs we saved in an attempt to avoid generating COPY
1383 if (this->copy_relocs_
== NULL
)
1385 if (this->copy_relocs_
->any_to_emit())
1387 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
1388 this->copy_relocs_
->emit(rela_dyn
);
1390 delete this->copy_relocs_
;
1391 this->copy_relocs_
= NULL
;
1394 // Perform a relocation.
1397 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1398 Target_x86_64
* target
,
1400 const elfcpp::Rela
<64, false>& rela
,
1401 unsigned int r_type
,
1402 const Sized_symbol
<64>* gsym
,
1403 const Symbol_value
<64>* psymval
,
1404 unsigned char* view
,
1405 elfcpp::Elf_types
<64>::Elf_Addr address
,
1406 section_size_type view_size
)
1408 if (this->skip_call_tls_get_addr_
)
1410 if (r_type
!= elfcpp::R_X86_64_PLT32
1412 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1414 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1415 _("missing expected TLS relocation"));
1419 this->skip_call_tls_get_addr_
= false;
1424 // Pick the value to use for symbols defined in shared objects.
1425 Symbol_value
<64> symval
;
1427 && (gsym
->is_from_dynobj()
1428 || (parameters
->output_is_shared()
1429 && gsym
->is_preemptible()))
1430 && gsym
->has_plt_offset())
1432 symval
.set_output_value(target
->plt_section()->address()
1433 + gsym
->plt_offset());
1437 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1438 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1440 // Get the GOT offset if needed.
1441 // The GOT pointer points to the end of the GOT section.
1442 // We need to subtract the size of the GOT section to get
1443 // the actual offset to use in the relocation.
1444 bool have_got_offset
= false;
1445 unsigned int got_offset
= 0;
1448 case elfcpp::R_X86_64_GOT32
:
1449 case elfcpp::R_X86_64_GOT64
:
1450 case elfcpp::R_X86_64_GOTPLT64
:
1451 case elfcpp::R_X86_64_GOTPCREL
:
1452 case elfcpp::R_X86_64_GOTPCREL64
:
1455 gold_assert(gsym
->has_got_offset());
1456 got_offset
= gsym
->got_offset() - target
->got_size();
1460 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1461 gold_assert(object
->local_has_got_offset(r_sym
));
1462 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1464 have_got_offset
= true;
1473 case elfcpp::R_X86_64_NONE
:
1474 case elfcpp::R_386_GNU_VTINHERIT
:
1475 case elfcpp::R_386_GNU_VTENTRY
:
1478 case elfcpp::R_X86_64_64
:
1479 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1482 case elfcpp::R_X86_64_PC64
:
1483 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1487 case elfcpp::R_X86_64_32
:
1488 // FIXME: we need to verify that value + addend fits into 32 bits:
1489 // uint64_t x = value + addend;
1490 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1491 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1492 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1495 case elfcpp::R_X86_64_32S
:
1496 // FIXME: we need to verify that value + addend fits into 32 bits:
1497 // int64_t x = value + addend; // note this quantity is signed!
1498 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1499 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1502 case elfcpp::R_X86_64_PC32
:
1503 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1507 case elfcpp::R_X86_64_16
:
1508 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1511 case elfcpp::R_X86_64_PC16
:
1512 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1516 case elfcpp::R_X86_64_8
:
1517 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1520 case elfcpp::R_X86_64_PC8
:
1521 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1525 case elfcpp::R_X86_64_PLT32
:
1526 gold_assert(gsym
== NULL
1527 || gsym
->has_plt_offset()
1528 || gsym
->final_value_is_known()
1529 || (gsym
->is_defined()
1530 && !gsym
->is_from_dynobj()
1531 && !gsym
->is_preemptible()));
1532 // Note: while this code looks the same as for R_X86_64_PC32, it
1533 // behaves differently because psymval was set to point to
1534 // the PLT entry, rather than the symbol, in Scan::global().
1535 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1539 case elfcpp::R_X86_64_PLTOFF64
:
1542 gold_assert(gsym
->has_plt_offset()
1543 || gsym
->final_value_is_known());
1544 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
1545 got_address
= target
->got_section(NULL
, NULL
)->address();
1546 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
1547 addend
- got_address
);
1550 case elfcpp::R_X86_64_GOT32
:
1551 gold_assert(have_got_offset
);
1552 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1555 case elfcpp::R_X86_64_GOTPC32
:
1558 elfcpp::Elf_types
<64>::Elf_Addr value
;
1559 value
= target
->got_plt_section()->address();
1560 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1564 case elfcpp::R_X86_64_GOT64
:
1565 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1566 // Since we always add a PLT entry, this is equivalent.
1567 case elfcpp::R_X86_64_GOTPLT64
:
1568 gold_assert(have_got_offset
);
1569 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1572 case elfcpp::R_X86_64_GOTPC64
:
1575 elfcpp::Elf_types
<64>::Elf_Addr value
;
1576 value
= target
->got_plt_section()->address();
1577 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1581 case elfcpp::R_X86_64_GOTOFF64
:
1583 elfcpp::Elf_types
<64>::Elf_Addr value
;
1584 value
= (psymval
->value(object
, 0)
1585 - target
->got_plt_section()->address());
1586 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1590 case elfcpp::R_X86_64_GOTPCREL
:
1592 gold_assert(have_got_offset
);
1593 elfcpp::Elf_types
<64>::Elf_Addr value
;
1594 value
= target
->got_plt_section()->address() + got_offset
;
1595 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1599 case elfcpp::R_X86_64_GOTPCREL64
:
1601 gold_assert(have_got_offset
);
1602 elfcpp::Elf_types
<64>::Elf_Addr value
;
1603 value
= target
->got_plt_section()->address() + got_offset
;
1604 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1608 case elfcpp::R_X86_64_COPY
:
1609 case elfcpp::R_X86_64_GLOB_DAT
:
1610 case elfcpp::R_X86_64_JUMP_SLOT
:
1611 case elfcpp::R_X86_64_RELATIVE
:
1612 // These are outstanding tls relocs, which are unexpected when linking
1613 case elfcpp::R_X86_64_TPOFF64
:
1614 case elfcpp::R_X86_64_DTPMOD64
:
1615 case elfcpp::R_X86_64_TLSDESC
:
1616 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1617 _("unexpected reloc %u in object file"),
1621 // These are initial tls relocs, which are expected when linking
1622 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1623 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1624 case elfcpp::R_X86_64_TLSDESC_CALL
:
1625 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1626 case elfcpp::R_X86_64_DTPOFF32
:
1627 case elfcpp::R_X86_64_DTPOFF64
:
1628 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1629 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1630 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
1631 view
, address
, view_size
);
1634 case elfcpp::R_X86_64_SIZE32
:
1635 case elfcpp::R_X86_64_SIZE64
:
1637 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1638 _("unsupported reloc %u"),
1646 // Perform a TLS relocation.
1649 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1650 Target_x86_64
* target
,
1652 const elfcpp::Rela
<64, false>& rela
,
1653 unsigned int r_type
,
1654 const Sized_symbol
<64>* gsym
,
1655 const Symbol_value
<64>* psymval
,
1656 unsigned char* view
,
1657 elfcpp::Elf_types
<64>::Elf_Addr address
,
1658 section_size_type view_size
)
1660 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1662 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1663 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1665 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1667 const bool is_final
= (gsym
== NULL
1668 ? !parameters
->output_is_position_independent()
1669 : gsym
->final_value_is_known());
1670 const tls::Tls_optimization optimized_type
1671 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1674 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1675 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1676 case elfcpp::R_X86_64_TLSDESC_CALL
:
1677 if (optimized_type
== tls::TLSOPT_TO_LE
)
1679 gold_assert(tls_segment
!= NULL
);
1680 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1681 rela
, r_type
, value
, view
,
1687 unsigned int got_offset
;
1690 gold_assert(gsym
->has_tls_got_offset(true));
1691 got_offset
= gsym
->tls_got_offset(true) - target
->got_size();
1695 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1696 gold_assert(object
->local_has_tls_got_offset(r_sym
, true));
1697 got_offset
= (object
->local_tls_got_offset(r_sym
, true)
1698 - target
->got_size());
1700 if (optimized_type
== tls::TLSOPT_TO_IE
)
1702 gold_assert(tls_segment
!= NULL
);
1703 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1704 got_offset
, view
, view_size
);
1707 else if (optimized_type
== tls::TLSOPT_NONE
)
1709 // Relocate the field with the offset of the pair of GOT
1711 value
= target
->got_plt_section()->address() + got_offset
;
1712 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
1717 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1718 _("unsupported reloc %u"), r_type
);
1721 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1722 if (optimized_type
== tls::TLSOPT_TO_LE
)
1724 gold_assert(tls_segment
!= NULL
);
1725 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1726 value
, view
, view_size
);
1729 else if (optimized_type
== tls::TLSOPT_NONE
)
1731 // Relocate the field with the offset of the GOT entry for
1732 // the module index.
1733 unsigned int got_offset
;
1734 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1735 - target
->got_size());
1736 value
= target
->got_plt_section()->address() + got_offset
;
1737 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
1741 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1742 _("unsupported reloc %u"), r_type
);
1745 case elfcpp::R_X86_64_DTPOFF32
:
1746 gold_assert(tls_segment
!= NULL
);
1747 if (optimized_type
== tls::TLSOPT_TO_LE
)
1748 value
-= tls_segment
->memsz();
1749 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1752 case elfcpp::R_X86_64_DTPOFF64
:
1753 gold_assert(tls_segment
!= NULL
);
1754 if (optimized_type
== tls::TLSOPT_TO_LE
)
1755 value
-= tls_segment
->memsz();
1756 Relocate_functions
<64, false>::rela64(view
, value
, 0);
1759 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1760 if (optimized_type
== tls::TLSOPT_TO_LE
)
1762 gold_assert(tls_segment
!= NULL
);
1763 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1764 rela
, r_type
, value
, view
,
1768 else if (optimized_type
== tls::TLSOPT_NONE
)
1770 // Relocate the field with the offset of the GOT entry for
1771 // the tp-relative offset of the symbol.
1772 unsigned int got_offset
;
1775 gold_assert(gsym
->has_got_offset());
1776 got_offset
= gsym
->got_offset() - target
->got_size();
1780 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1781 gold_assert(object
->local_has_got_offset(r_sym
));
1782 got_offset
= (object
->local_got_offset(r_sym
)
1783 - target
->got_size());
1785 value
= target
->got_plt_section()->address() + got_offset
;
1786 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1789 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1790 _("unsupported reloc type %u"),
1794 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1795 value
-= tls_segment
->memsz();
1796 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1801 // Do a relocation in which we convert a TLS General-Dynamic to an
1805 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
1807 Output_segment
* tls_segment
,
1808 const elfcpp::Rela
<64, false>& rela
,
1810 elfcpp::Elf_types
<64>::Elf_Addr value
,
1811 unsigned char* view
,
1812 section_size_type view_size
)
1814 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1815 // .word 0x6666; rex64; call __tls_get_addr
1816 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
1818 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
1819 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
1821 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1822 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
1823 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1824 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
1826 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
1828 value
-= tls_segment
->memsz();
1829 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
1831 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1833 this->skip_call_tls_get_addr_
= true;
1836 // Do a relocation in which we convert a TLS General-Dynamic to a
1840 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
1842 Output_segment
* tls_segment
,
1843 const elfcpp::Rela
<64, false>& rela
,
1845 elfcpp::Elf_types
<64>::Elf_Addr value
,
1846 unsigned char* view
,
1847 section_size_type view_size
)
1849 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1850 // .word 0x6666; rex64; call __tls_get_addr
1851 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1853 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
1854 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
1856 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1857 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
1858 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1859 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
1861 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1863 value
-= tls_segment
->memsz();
1864 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
1866 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1868 this->skip_call_tls_get_addr_
= true;
1872 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
1875 const elfcpp::Rela
<64, false>& rela
,
1877 elfcpp::Elf_types
<64>::Elf_Addr
,
1878 unsigned char* view
,
1879 section_size_type view_size
)
1881 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1882 // ... leq foo@dtpoff(%rax),%reg
1883 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1885 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1886 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
1888 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
1889 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
1891 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
1893 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1895 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1897 this->skip_call_tls_get_addr_
= true;
1900 // Do a relocation in which we convert a TLS Initial-Exec to a
1904 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
1906 Output_segment
* tls_segment
,
1907 const elfcpp::Rela
<64, false>& rela
,
1909 elfcpp::Elf_types
<64>::Elf_Addr value
,
1910 unsigned char* view
,
1911 section_size_type view_size
)
1913 // We need to examine the opcodes to figure out which instruction we
1916 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
1917 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
1919 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
1920 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
1922 unsigned char op1
= view
[-3];
1923 unsigned char op2
= view
[-2];
1924 unsigned char op3
= view
[-1];
1925 unsigned char reg
= op3
>> 3;
1933 view
[-1] = 0xc0 | reg
;
1937 // Special handling for %rsp.
1941 view
[-1] = 0xc0 | reg
;
1949 view
[-1] = 0x80 | reg
| (reg
<< 3);
1952 value
-= tls_segment
->memsz();
1953 Relocate_functions
<64, false>::rela32(view
, value
, 0);
1956 // Relocate section data.
1959 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
1960 unsigned int sh_type
,
1961 const unsigned char* prelocs
,
1963 Output_section
* output_section
,
1964 bool needs_special_offset_handling
,
1965 unsigned char* view
,
1966 elfcpp::Elf_types
<64>::Elf_Addr address
,
1967 section_size_type view_size
)
1969 gold_assert(sh_type
== elfcpp::SHT_RELA
);
1971 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1972 Target_x86_64::Relocate
>(
1978 needs_special_offset_handling
,
1984 // Return the size of a relocation while scanning during a relocatable
1988 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
1989 unsigned int r_type
,
1994 case elfcpp::R_X86_64_NONE
:
1995 case elfcpp::R_386_GNU_VTINHERIT
:
1996 case elfcpp::R_386_GNU_VTENTRY
:
1997 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1998 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1999 case elfcpp::R_X86_64_TLSDESC_CALL
:
2000 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2001 case elfcpp::R_X86_64_DTPOFF32
:
2002 case elfcpp::R_X86_64_DTPOFF64
:
2003 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2004 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2007 case elfcpp::R_X86_64_64
:
2008 case elfcpp::R_X86_64_PC64
:
2009 case elfcpp::R_X86_64_GOTOFF64
:
2010 case elfcpp::R_X86_64_GOTPC64
:
2011 case elfcpp::R_X86_64_PLTOFF64
:
2012 case elfcpp::R_X86_64_GOT64
:
2013 case elfcpp::R_X86_64_GOTPCREL64
:
2014 case elfcpp::R_X86_64_GOTPCREL
:
2015 case elfcpp::R_X86_64_GOTPLT64
:
2018 case elfcpp::R_X86_64_32
:
2019 case elfcpp::R_X86_64_32S
:
2020 case elfcpp::R_X86_64_PC32
:
2021 case elfcpp::R_X86_64_PLT32
:
2022 case elfcpp::R_X86_64_GOTPC32
:
2023 case elfcpp::R_X86_64_GOT32
:
2026 case elfcpp::R_X86_64_16
:
2027 case elfcpp::R_X86_64_PC16
:
2030 case elfcpp::R_X86_64_8
:
2031 case elfcpp::R_X86_64_PC8
:
2034 case elfcpp::R_X86_64_COPY
:
2035 case elfcpp::R_X86_64_GLOB_DAT
:
2036 case elfcpp::R_X86_64_JUMP_SLOT
:
2037 case elfcpp::R_X86_64_RELATIVE
:
2038 // These are outstanding tls relocs, which are unexpected when linking
2039 case elfcpp::R_X86_64_TPOFF64
:
2040 case elfcpp::R_X86_64_DTPMOD64
:
2041 case elfcpp::R_X86_64_TLSDESC
:
2042 object
->error(_("unexpected reloc %u in object file"), r_type
);
2045 case elfcpp::R_X86_64_SIZE32
:
2046 case elfcpp::R_X86_64_SIZE64
:
2048 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
2053 // Scan the relocs during a relocatable link.
2056 Target_x86_64::scan_relocatable_relocs(const General_options
& options
,
2057 Symbol_table
* symtab
,
2059 Sized_relobj
<64, false>* object
,
2060 unsigned int data_shndx
,
2061 unsigned int sh_type
,
2062 const unsigned char* prelocs
,
2064 Output_section
* output_section
,
2065 bool needs_special_offset_handling
,
2066 size_t local_symbol_count
,
2067 const unsigned char* plocal_symbols
,
2068 Relocatable_relocs
* rr
)
2070 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2072 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
2073 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2075 gold::scan_relocatable_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2076 Scan_relocatable_relocs
>(
2085 needs_special_offset_handling
,
2091 // Relocate a section during a relocatable link.
2094 Target_x86_64::relocate_for_relocatable(
2095 const Relocate_info
<64, false>* relinfo
,
2096 unsigned int sh_type
,
2097 const unsigned char* prelocs
,
2099 Output_section
* output_section
,
2100 off_t offset_in_output_section
,
2101 const Relocatable_relocs
* rr
,
2102 unsigned char* view
,
2103 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
2104 section_size_type view_size
,
2105 unsigned char* reloc_view
,
2106 section_size_type reloc_view_size
)
2108 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2110 gold::relocate_for_relocatable
<64, false, Target_x86_64
, elfcpp::SHT_RELA
>(
2115 offset_in_output_section
,
2124 // Return the value to use for a dynamic which requires special
2125 // treatment. This is how we support equality comparisons of function
2126 // pointers across shared library boundaries, as described in the
2127 // processor specific ABI supplement.
2130 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
2132 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2133 return this->plt_section()->address() + gsym
->plt_offset();
2136 // Return a string used to fill a code section with nops to take up
2137 // the specified length.
2140 Target_x86_64::do_code_fill(section_size_type length
)
2144 // Build a jmpq instruction to skip over the bytes.
2145 unsigned char jmp
[5];
2147 elfcpp::Swap_unaligned
<64, false>::writeval(jmp
+ 1, length
- 5);
2148 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2149 + std::string(length
- 5, '\0'));
2152 // Nop sequences of various lengths.
2153 const char nop1
[1] = { 0x90 }; // nop
2154 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2155 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2156 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2157 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2158 0x00 }; // leal 0(%esi,1),%esi
2159 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2161 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2163 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2164 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2165 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2166 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2168 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2169 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2171 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2172 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2174 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2175 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2176 0x00, 0x00, 0x00, 0x00 };
2177 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2178 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2179 0x27, 0x00, 0x00, 0x00,
2181 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2182 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2183 0xbc, 0x27, 0x00, 0x00,
2185 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2186 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2187 0x90, 0x90, 0x90, 0x90,
2190 const char* nops
[16] = {
2192 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2193 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2196 return std::string(nops
[length
], length
);
2199 // The selector for x86_64 object files.
2201 class Target_selector_x86_64
: public Target_selector
2204 Target_selector_x86_64()
2205 : Target_selector(elfcpp::EM_X86_64
, 64, false)
2209 recognize(int machine
, int osabi
, int abiversion
);
2212 Target_x86_64
* target_
;
2215 // Recognize an x86_64 object file when we already know that the machine
2216 // number is EM_X86_64.
2219 Target_selector_x86_64::recognize(int, int, int)
2221 if (this->target_
== NULL
)
2222 this->target_
= new Target_x86_64();
2223 return this->target_
;
2226 Target_selector_x86_64 target_selector_x86_64
;
2228 } // End anonymous namespace.