1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 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"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
47 class Output_data_plt_x86_64
;
49 // The x86_64 target class.
51 // http://www.x86-64.org/documentation/abi.pdf
52 // TLS info comes from
53 // http://people.redhat.com/drepper/tls.pdf
54 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
56 class Target_x86_64
: public Target_freebsd
<64, false>
59 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
60 // uses only Elf64_Rela relocation entries with explicit addends."
61 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
64 : Target_freebsd
<64, false>(&x86_64_info
),
65 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rela_dyn_(NULL
),
66 copy_relocs_(elfcpp::R_X86_64_COPY
), dynbss_(NULL
),
67 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
70 // Scan the relocations to look for symbol adjustments.
72 gc_process_relocs(const General_options
& options
,
75 Sized_relobj
<64, false>* object
,
76 unsigned int data_shndx
,
78 const unsigned char* prelocs
,
80 Output_section
* output_section
,
81 bool needs_special_offset_handling
,
82 size_t local_symbol_count
,
83 const unsigned char* plocal_symbols
);
85 // Scan the relocations to look for symbol adjustments.
87 scan_relocs(const General_options
& options
,
90 Sized_relobj
<64, false>* object
,
91 unsigned int data_shndx
,
93 const unsigned char* prelocs
,
95 Output_section
* output_section
,
96 bool needs_special_offset_handling
,
97 size_t local_symbol_count
,
98 const unsigned char* plocal_symbols
);
100 // Finalize the sections.
102 do_finalize_sections(Layout
*);
104 // Return the value to use for a dynamic which requires special
107 do_dynsym_value(const Symbol
*) const;
109 // Relocate a section.
111 relocate_section(const Relocate_info
<64, false>*,
112 unsigned int sh_type
,
113 const unsigned char* prelocs
,
115 Output_section
* output_section
,
116 bool needs_special_offset_handling
,
118 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
119 section_size_type view_size
);
121 // Scan the relocs during a relocatable link.
123 scan_relocatable_relocs(const General_options
& options
,
124 Symbol_table
* symtab
,
126 Sized_relobj
<64, false>* object
,
127 unsigned int data_shndx
,
128 unsigned int sh_type
,
129 const unsigned char* prelocs
,
131 Output_section
* output_section
,
132 bool needs_special_offset_handling
,
133 size_t local_symbol_count
,
134 const unsigned char* plocal_symbols
,
135 Relocatable_relocs
*);
137 // Relocate a section during a relocatable link.
139 relocate_for_relocatable(const Relocate_info
<64, false>*,
140 unsigned int sh_type
,
141 const unsigned char* prelocs
,
143 Output_section
* output_section
,
144 off_t offset_in_output_section
,
145 const Relocatable_relocs
*,
147 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
148 section_size_type view_size
,
149 unsigned char* reloc_view
,
150 section_size_type reloc_view_size
);
152 // Return a string used to fill a code section with nops.
154 do_code_fill(section_size_type length
) const;
156 // Return whether SYM is defined by the ABI.
158 do_is_defined_by_abi(const Symbol
* sym
) const
159 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
161 // Return the size of the GOT section.
165 gold_assert(this->got_
!= NULL
);
166 return this->got_
->data_size();
170 // The class which scans relocations.
175 : issued_non_pic_error_(false)
179 local(const General_options
& options
, Symbol_table
* symtab
,
180 Layout
* layout
, Target_x86_64
* target
,
181 Sized_relobj
<64, false>* object
,
182 unsigned int data_shndx
,
183 Output_section
* output_section
,
184 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
185 const elfcpp::Sym
<64, false>& lsym
);
188 global(const General_options
& options
, Symbol_table
* symtab
,
189 Layout
* layout
, Target_x86_64
* target
,
190 Sized_relobj
<64, false>* object
,
191 unsigned int data_shndx
,
192 Output_section
* output_section
,
193 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
198 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
201 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
205 check_non_pic(Relobj
*, unsigned int r_type
);
207 // Whether we have issued an error about a non-PIC compilation.
208 bool issued_non_pic_error_
;
211 // The class which implements relocation.
216 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
221 if (this->skip_call_tls_get_addr_
)
223 // FIXME: This needs to specify the location somehow.
224 gold_error(_("missing expected TLS relocation"));
228 // Do a relocation. Return false if the caller should not issue
229 // any warnings about this relocation.
231 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, Output_section
*,
232 size_t relnum
, const elfcpp::Rela
<64, false>&,
233 unsigned int r_type
, const Sized_symbol
<64>*,
234 const Symbol_value
<64>*,
235 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
239 // Do a TLS relocation.
241 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
242 size_t relnum
, const elfcpp::Rela
<64, false>&,
243 unsigned int r_type
, const Sized_symbol
<64>*,
244 const Symbol_value
<64>*,
245 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
248 // Do a TLS General-Dynamic to Initial-Exec transition.
250 tls_gd_to_ie(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 elfcpp::Elf_types
<64>::Elf_Addr
,
256 section_size_type view_size
);
258 // Do a TLS General-Dynamic to Local-Exec transition.
260 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
261 Output_segment
* tls_segment
,
262 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
263 elfcpp::Elf_types
<64>::Elf_Addr value
,
265 section_size_type view_size
);
267 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
269 tls_desc_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
270 Output_segment
* tls_segment
,
271 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
272 elfcpp::Elf_types
<64>::Elf_Addr value
,
274 elfcpp::Elf_types
<64>::Elf_Addr
,
275 section_size_type view_size
);
277 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
279 tls_desc_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
280 Output_segment
* tls_segment
,
281 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
282 elfcpp::Elf_types
<64>::Elf_Addr value
,
284 section_size_type view_size
);
286 // Do a TLS Local-Dynamic to Local-Exec transition.
288 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
289 Output_segment
* tls_segment
,
290 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
291 elfcpp::Elf_types
<64>::Elf_Addr value
,
293 section_size_type view_size
);
295 // Do a TLS Initial-Exec to Local-Exec transition.
297 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
298 Output_segment
* tls_segment
,
299 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
300 elfcpp::Elf_types
<64>::Elf_Addr value
,
302 section_size_type view_size
);
304 // This is set if we should skip the next reloc, which should be a
305 // PLT32 reloc against ___tls_get_addr.
306 bool skip_call_tls_get_addr_
;
308 // This is set if we see a relocation which could load the address
309 // of the TLS block. Whether we see such a relocation determines
310 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
311 // in debugging sections.
312 bool saw_tls_block_reloc_
;
315 // A class which returns the size required for a relocation type,
316 // used while scanning relocs during a relocatable link.
317 class Relocatable_size_for_reloc
321 get_size_for_reloc(unsigned int, Relobj
*);
324 // Adjust TLS relocation type based on the options and whether this
325 // is a local symbol.
326 static tls::Tls_optimization
327 optimize_tls_reloc(bool is_final
, int r_type
);
329 // Get the GOT section, creating it if necessary.
330 Output_data_got
<64, false>*
331 got_section(Symbol_table
*, Layout
*);
333 // Get the GOT PLT section.
335 got_plt_section() const
337 gold_assert(this->got_plt_
!= NULL
);
338 return this->got_plt_
;
341 // Create the PLT section.
343 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
345 // Create a PLT entry for a global symbol.
347 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
349 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
351 define_tls_base_symbol(Symbol_table
*, Layout
*);
353 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
355 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
357 // Create a GOT entry for the TLS module index.
359 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
360 Sized_relobj
<64, false>* object
);
362 // Get the PLT section.
363 Output_data_plt_x86_64
*
366 gold_assert(this->plt_
!= NULL
);
370 // Get the dynamic reloc section, creating it if necessary.
372 rela_dyn_section(Layout
*);
374 // Return true if the symbol may need a COPY relocation.
375 // References from an executable object to non-function symbols
376 // defined in a dynamic object may need a COPY relocation.
378 may_need_copy_reloc(Symbol
* gsym
)
380 return (!parameters
->options().shared()
381 && gsym
->is_from_dynobj()
382 && gsym
->type() != elfcpp::STT_FUNC
);
385 // Add a potential copy relocation.
387 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
388 Sized_relobj
<64, false>* object
,
389 unsigned int shndx
, Output_section
* output_section
,
390 Symbol
* sym
, const elfcpp::Rela
<64, false>& reloc
)
392 this->copy_relocs_
.copy_reloc(symtab
, layout
,
393 symtab
->get_sized_symbol
<64>(sym
),
394 object
, shndx
, output_section
,
395 reloc
, this->rela_dyn_section(layout
));
398 // Information about this specific target which we pass to the
399 // general Target structure.
400 static const Target::Target_info x86_64_info
;
404 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
405 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
406 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
407 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
411 Output_data_got
<64, false>* got_
;
413 Output_data_plt_x86_64
* plt_
;
414 // The GOT PLT section.
415 Output_data_space
* got_plt_
;
416 // The dynamic reloc section.
417 Reloc_section
* rela_dyn_
;
418 // Relocs saved to avoid a COPY reloc.
419 Copy_relocs
<elfcpp::SHT_RELA
, 64, false> copy_relocs_
;
420 // Space for variables copied with a COPY reloc.
421 Output_data_space
* dynbss_
;
422 // Offset of the GOT entry for the TLS module index.
423 unsigned int got_mod_index_offset_
;
424 // True if the _TLS_MODULE_BASE_ symbol has been defined.
425 bool tls_base_symbol_defined_
;
428 const Target::Target_info
Target_x86_64::x86_64_info
=
431 false, // is_big_endian
432 elfcpp::EM_X86_64
, // machine_code
433 false, // has_make_symbol
434 false, // has_resolve
435 true, // has_code_fill
436 true, // is_default_stack_executable
438 "/lib/ld64.so.1", // program interpreter
439 0x400000, // default_text_segment_address
440 0x1000, // abi_pagesize (overridable by -z max-page-size)
441 0x1000 // common_pagesize (overridable by -z common-page-size)
444 // Get the GOT section, creating it if necessary.
446 Output_data_got
<64, false>*
447 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
449 if (this->got_
== NULL
)
451 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
453 this->got_
= new Output_data_got
<64, false>();
456 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
458 | elfcpp::SHF_WRITE
),
462 // The old GNU linker creates a .got.plt section. We just
463 // create another set of data in the .got section. Note that we
464 // always create a PLT if we create a GOT, although the PLT
466 this->got_plt_
= new Output_data_space(8, "** GOT PLT");
467 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
469 | elfcpp::SHF_WRITE
),
473 // The first three entries are reserved.
474 this->got_plt_
->set_current_data_size(3 * 8);
476 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
477 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
479 0, 0, elfcpp::STT_OBJECT
,
481 elfcpp::STV_HIDDEN
, 0,
488 // Get the dynamic reloc section, creating it if necessary.
490 Target_x86_64::Reloc_section
*
491 Target_x86_64::rela_dyn_section(Layout
* layout
)
493 if (this->rela_dyn_
== NULL
)
495 gold_assert(layout
!= NULL
);
496 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
497 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
498 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
500 return this->rela_dyn_
;
503 // A class to handle the PLT data.
505 class Output_data_plt_x86_64
: public Output_section_data
508 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
510 Output_data_plt_x86_64(Layout
*, Output_data_got
<64, false>*,
513 // Add an entry to the PLT.
515 add_entry(Symbol
* gsym
);
517 // Add the reserved TLSDESC_PLT entry to the PLT.
519 reserve_tlsdesc_entry(unsigned int got_offset
)
520 { this->tlsdesc_got_offset_
= got_offset
; }
522 // Return true if a TLSDESC_PLT entry has been reserved.
524 has_tlsdesc_entry() const
525 { return this->tlsdesc_got_offset_
!= -1U; }
527 // Return the GOT offset for the reserved TLSDESC_PLT entry.
529 get_tlsdesc_got_offset() const
530 { return this->tlsdesc_got_offset_
; }
532 // Return the offset of the reserved TLSDESC_PLT entry.
534 get_tlsdesc_plt_offset() const
535 { return (this->count_
+ 1) * plt_entry_size
; }
537 // Return the .rel.plt section data.
540 { return this->rel_
; }
544 do_adjust_output_section(Output_section
* os
);
546 // Write to a map file.
548 do_print_to_mapfile(Mapfile
* mapfile
) const
549 { mapfile
->print_output_data(this, _("** PLT")); }
552 // The size of an entry in the PLT.
553 static const int plt_entry_size
= 16;
555 // The first entry in the PLT.
556 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
557 // procedure linkage table for both programs and shared objects."
558 static unsigned char first_plt_entry
[plt_entry_size
];
560 // Other entries in the PLT for an executable.
561 static unsigned char plt_entry
[plt_entry_size
];
563 // The reserved TLSDESC entry in the PLT for an executable.
564 static unsigned char tlsdesc_plt_entry
[plt_entry_size
];
566 // Set the final size.
568 set_final_data_size();
570 // Write out the PLT data.
572 do_write(Output_file
*);
574 // The reloc section.
577 Output_data_got
<64, false>* got_
;
578 // The .got.plt section.
579 Output_data_space
* got_plt_
;
580 // The number of PLT entries.
582 // Offset of the reserved TLSDESC_GOT entry when needed.
583 unsigned int tlsdesc_got_offset_
;
586 // Create the PLT section. The ordinary .got section is an argument,
587 // since we need to refer to the start. We also create our own .got
588 // section just for PLT entries.
590 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
591 Output_data_got
<64, false>* got
,
592 Output_data_space
* got_plt
)
593 : Output_section_data(8), got_(got
), got_plt_(got_plt
), count_(0),
594 tlsdesc_got_offset_(-1U)
596 this->rel_
= new Reloc_section(false);
597 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
598 elfcpp::SHF_ALLOC
, this->rel_
);
602 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
604 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
605 // linker, and so do we.
609 // Add an entry to the PLT.
612 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
614 gold_assert(!gsym
->has_plt_offset());
616 // Note that when setting the PLT offset we skip the initial
617 // reserved PLT entry.
618 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
622 section_offset_type got_offset
= this->got_plt_
->current_data_size();
624 // Every PLT entry needs a GOT entry which points back to the PLT
625 // entry (this will be changed by the dynamic linker, normally
626 // lazily when the function is called).
627 this->got_plt_
->set_current_data_size(got_offset
+ 8);
629 // Every PLT entry needs a reloc.
630 gsym
->set_needs_dynsym_entry();
631 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
634 // Note that we don't need to save the symbol. The contents of the
635 // PLT are independent of which symbols are used. The symbols only
636 // appear in the relocations.
639 // Set the final size.
641 Output_data_plt_x86_64::set_final_data_size()
643 unsigned int count
= this->count_
;
644 if (this->has_tlsdesc_entry())
646 this->set_data_size((count
+ 1) * plt_entry_size
);
649 // The first entry in the PLT for an executable.
651 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
653 // From AMD64 ABI Draft 0.98, page 76
654 0xff, 0x35, // pushq contents of memory address
655 0, 0, 0, 0, // replaced with address of .got + 8
656 0xff, 0x25, // jmp indirect
657 0, 0, 0, 0, // replaced with address of .got + 16
658 0x90, 0x90, 0x90, 0x90 // noop (x4)
661 // Subsequent entries in the PLT for an executable.
663 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
665 // From AMD64 ABI Draft 0.98, page 76
666 0xff, 0x25, // jmpq indirect
667 0, 0, 0, 0, // replaced with address of symbol in .got
668 0x68, // pushq immediate
669 0, 0, 0, 0, // replaced with offset into relocation table
670 0xe9, // jmpq relative
671 0, 0, 0, 0 // replaced with offset to start of .plt
674 // The reserved TLSDESC entry in the PLT for an executable.
676 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry
[plt_entry_size
] =
678 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
679 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
680 0xff, 0x35, // pushq x(%rip)
681 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
682 0xff, 0x25, // jmpq *y(%rip)
683 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
688 // Write out the PLT. This uses the hand-coded instructions above,
689 // and adjusts them as needed. This is specified by the AMD64 ABI.
692 Output_data_plt_x86_64::do_write(Output_file
* of
)
694 const off_t offset
= this->offset();
695 const section_size_type oview_size
=
696 convert_to_section_size_type(this->data_size());
697 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
699 const off_t got_file_offset
= this->got_plt_
->offset();
700 const section_size_type got_size
=
701 convert_to_section_size_type(this->got_plt_
->data_size());
702 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
705 unsigned char* pov
= oview
;
707 // The base address of the .plt section.
708 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
709 // The base address of the .got section.
710 elfcpp::Elf_types
<64>::Elf_Addr got_base
= this->got_
->address();
711 // The base address of the PLT portion of the .got section,
712 // which is where the GOT pointer will point, and where the
713 // three reserved GOT entries are located.
714 elfcpp::Elf_types
<64>::Elf_Addr got_address
= this->got_plt_
->address();
716 memcpy(pov
, first_plt_entry
, plt_entry_size
);
717 // We do a jmp relative to the PC at the end of this instruction.
718 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
720 - (plt_address
+ 6)));
721 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
723 - (plt_address
+ 12)));
724 pov
+= plt_entry_size
;
726 unsigned char* got_pov
= got_view
;
728 memset(got_pov
, 0, 24);
731 unsigned int plt_offset
= plt_entry_size
;
732 unsigned int got_offset
= 24;
733 const unsigned int count
= this->count_
;
734 for (unsigned int plt_index
= 0;
737 pov
+= plt_entry_size
,
739 plt_offset
+= plt_entry_size
,
742 // Set and adjust the PLT entry itself.
743 memcpy(pov
, plt_entry
, plt_entry_size
);
744 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
745 (got_address
+ got_offset
746 - (plt_address
+ plt_offset
749 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
750 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
751 - (plt_offset
+ plt_entry_size
));
753 // Set the entry in the GOT.
754 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
757 if (this->has_tlsdesc_entry())
759 // Set and adjust the reserved TLSDESC PLT entry.
760 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
761 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
762 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
764 - (plt_address
+ plt_offset
766 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
769 - (plt_address
+ plt_offset
771 pov
+= plt_entry_size
;
774 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
775 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
777 of
->write_output_view(offset
, oview_size
, oview
);
778 of
->write_output_view(got_file_offset
, got_size
, got_view
);
781 // Create the PLT section.
784 Target_x86_64::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
786 if (this->plt_
== NULL
)
788 // Create the GOT sections first.
789 this->got_section(symtab
, layout
);
791 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_
,
793 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
795 | elfcpp::SHF_EXECINSTR
),
800 // Create a PLT entry for a global symbol.
803 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
806 if (gsym
->has_plt_offset())
809 if (this->plt_
== NULL
)
810 this->make_plt_section(symtab
, layout
);
812 this->plt_
->add_entry(gsym
);
815 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
818 Target_x86_64::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
820 if (this->tls_base_symbol_defined_
)
823 Output_segment
* tls_segment
= layout
->tls_segment();
824 if (tls_segment
!= NULL
)
826 bool is_exec
= parameters
->options().output_is_executable();
827 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
831 elfcpp::STV_HIDDEN
, 0,
833 ? Symbol::SEGMENT_END
834 : Symbol::SEGMENT_START
),
837 this->tls_base_symbol_defined_
= true;
840 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
843 Target_x86_64::reserve_tlsdesc_entries(Symbol_table
* symtab
,
846 if (this->plt_
== NULL
)
847 this->make_plt_section(symtab
, layout
);
849 if (!this->plt_
->has_tlsdesc_entry())
851 // Allocate the TLSDESC_GOT entry.
852 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
853 unsigned int got_offset
= got
->add_constant(0);
855 // Allocate the TLSDESC_PLT entry.
856 this->plt_
->reserve_tlsdesc_entry(got_offset
);
860 // Create a GOT entry for the TLS module index.
863 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
864 Sized_relobj
<64, false>* object
)
866 if (this->got_mod_index_offset_
== -1U)
868 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
869 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
870 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
871 unsigned int got_offset
= got
->add_constant(0);
872 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
874 got
->add_constant(0);
875 this->got_mod_index_offset_
= got_offset
;
877 return this->got_mod_index_offset_
;
880 // Optimize the TLS relocation type based on what we know about the
881 // symbol. IS_FINAL is true if the final address of this symbol is
882 // known at link time.
884 tls::Tls_optimization
885 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
887 // If we are generating a shared library, then we can't do anything
889 if (parameters
->options().shared())
890 return tls::TLSOPT_NONE
;
894 case elfcpp::R_X86_64_TLSGD
:
895 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
896 case elfcpp::R_X86_64_TLSDESC_CALL
:
897 // These are General-Dynamic which permits fully general TLS
898 // access. Since we know that we are generating an executable,
899 // we can convert this to Initial-Exec. If we also know that
900 // this is a local symbol, we can further switch to Local-Exec.
902 return tls::TLSOPT_TO_LE
;
903 return tls::TLSOPT_TO_IE
;
905 case elfcpp::R_X86_64_TLSLD
:
906 // This is Local-Dynamic, which refers to a local symbol in the
907 // dynamic TLS block. Since we know that we generating an
908 // executable, we can switch to Local-Exec.
909 return tls::TLSOPT_TO_LE
;
911 case elfcpp::R_X86_64_DTPOFF32
:
912 case elfcpp::R_X86_64_DTPOFF64
:
913 // Another Local-Dynamic reloc.
914 return tls::TLSOPT_TO_LE
;
916 case elfcpp::R_X86_64_GOTTPOFF
:
917 // These are Initial-Exec relocs which get the thread offset
918 // from the GOT. If we know that we are linking against the
919 // local symbol, we can switch to Local-Exec, which links the
920 // thread offset into the instruction.
922 return tls::TLSOPT_TO_LE
;
923 return tls::TLSOPT_NONE
;
925 case elfcpp::R_X86_64_TPOFF32
:
926 // When we already have Local-Exec, there is nothing further we
928 return tls::TLSOPT_NONE
;
935 // Report an unsupported relocation against a local symbol.
938 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
941 gold_error(_("%s: unsupported reloc %u against local symbol"),
942 object
->name().c_str(), r_type
);
945 // We are about to emit a dynamic relocation of type R_TYPE. If the
946 // dynamic linker does not support it, issue an error. The GNU linker
947 // only issues a non-PIC error for an allocated read-only section.
948 // Here we know the section is allocated, but we don't know that it is
949 // read-only. But we check for all the relocation types which the
950 // glibc dynamic linker supports, so it seems appropriate to issue an
951 // error even if the section is not read-only.
954 Target_x86_64::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
)
958 // These are the relocation types supported by glibc for x86_64.
959 case elfcpp::R_X86_64_RELATIVE
:
960 case elfcpp::R_X86_64_GLOB_DAT
:
961 case elfcpp::R_X86_64_JUMP_SLOT
:
962 case elfcpp::R_X86_64_DTPMOD64
:
963 case elfcpp::R_X86_64_DTPOFF64
:
964 case elfcpp::R_X86_64_TPOFF64
:
965 case elfcpp::R_X86_64_64
:
966 case elfcpp::R_X86_64_32
:
967 case elfcpp::R_X86_64_PC32
:
968 case elfcpp::R_X86_64_COPY
:
972 // This prevents us from issuing more than one error per reloc
973 // section. But we can still wind up issuing more than one
974 // error per object file.
975 if (this->issued_non_pic_error_
)
977 object
->error(_("requires unsupported dynamic reloc; "
978 "recompile with -fPIC"));
979 this->issued_non_pic_error_
= true;
982 case elfcpp::R_X86_64_NONE
:
987 // Scan a relocation for a local symbol.
990 Target_x86_64::Scan::local(const General_options
&,
991 Symbol_table
* symtab
,
993 Target_x86_64
* target
,
994 Sized_relobj
<64, false>* object
,
995 unsigned int data_shndx
,
996 Output_section
* output_section
,
997 const elfcpp::Rela
<64, false>& reloc
,
999 const elfcpp::Sym
<64, false>& lsym
)
1003 case elfcpp::R_X86_64_NONE
:
1004 case elfcpp::R_386_GNU_VTINHERIT
:
1005 case elfcpp::R_386_GNU_VTENTRY
:
1008 case elfcpp::R_X86_64_64
:
1009 // If building a shared library (or a position-independent
1010 // executable), we need to create a dynamic relocation for this
1011 // location. The relocation applied at link time will apply the
1012 // link-time value, so we flag the location with an
1013 // R_X86_64_RELATIVE relocation so the dynamic loader can
1014 // relocate it easily.
1015 if (parameters
->options().output_is_position_independent())
1017 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1018 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1019 rela_dyn
->add_local_relative(object
, r_sym
,
1020 elfcpp::R_X86_64_RELATIVE
,
1021 output_section
, data_shndx
,
1022 reloc
.get_r_offset(),
1023 reloc
.get_r_addend());
1027 case elfcpp::R_X86_64_32
:
1028 case elfcpp::R_X86_64_32S
:
1029 case elfcpp::R_X86_64_16
:
1030 case elfcpp::R_X86_64_8
:
1031 // If building a shared library (or a position-independent
1032 // executable), we need to create a dynamic relocation for this
1033 // location. We can't use an R_X86_64_RELATIVE relocation
1034 // because that is always a 64-bit relocation.
1035 if (parameters
->options().output_is_position_independent())
1037 this->check_non_pic(object
, r_type
);
1039 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1040 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1041 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1042 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1043 data_shndx
, reloc
.get_r_offset(),
1044 reloc
.get_r_addend());
1047 gold_assert(lsym
.get_st_value() == 0);
1048 unsigned int shndx
= lsym
.get_st_shndx();
1050 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1053 object
->error(_("section symbol %u has bad shndx %u"),
1056 rela_dyn
->add_local_section(object
, shndx
,
1057 r_type
, output_section
,
1058 data_shndx
, reloc
.get_r_offset(),
1059 reloc
.get_r_addend());
1064 case elfcpp::R_X86_64_PC64
:
1065 case elfcpp::R_X86_64_PC32
:
1066 case elfcpp::R_X86_64_PC16
:
1067 case elfcpp::R_X86_64_PC8
:
1070 case elfcpp::R_X86_64_PLT32
:
1071 // Since we know this is a local symbol, we can handle this as a
1075 case elfcpp::R_X86_64_GOTPC32
:
1076 case elfcpp::R_X86_64_GOTOFF64
:
1077 case elfcpp::R_X86_64_GOTPC64
:
1078 case elfcpp::R_X86_64_PLTOFF64
:
1079 // We need a GOT section.
1080 target
->got_section(symtab
, layout
);
1081 // For PLTOFF64, we'd normally want a PLT section, but since we
1082 // know this is a local symbol, no PLT is needed.
1085 case elfcpp::R_X86_64_GOT64
:
1086 case elfcpp::R_X86_64_GOT32
:
1087 case elfcpp::R_X86_64_GOTPCREL64
:
1088 case elfcpp::R_X86_64_GOTPCREL
:
1089 case elfcpp::R_X86_64_GOTPLT64
:
1091 // The symbol requires a GOT entry.
1092 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1093 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1094 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
1096 // If we are generating a shared object, we need to add a
1097 // dynamic relocation for this symbol's GOT entry.
1098 if (parameters
->options().output_is_position_independent())
1100 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1101 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1102 if (r_type
!= elfcpp::R_X86_64_GOT32
)
1103 rela_dyn
->add_local_relative(
1104 object
, r_sym
, elfcpp::R_X86_64_RELATIVE
, got
,
1105 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1108 this->check_non_pic(object
, r_type
);
1110 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1111 rela_dyn
->add_local(
1112 object
, r_sym
, r_type
, got
,
1113 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1117 // For GOTPLT64, we'd normally want a PLT section, but since
1118 // we know this is a local symbol, no PLT is needed.
1122 case elfcpp::R_X86_64_COPY
:
1123 case elfcpp::R_X86_64_GLOB_DAT
:
1124 case elfcpp::R_X86_64_JUMP_SLOT
:
1125 case elfcpp::R_X86_64_RELATIVE
:
1126 // These are outstanding tls relocs, which are unexpected when linking
1127 case elfcpp::R_X86_64_TPOFF64
:
1128 case elfcpp::R_X86_64_DTPMOD64
:
1129 case elfcpp::R_X86_64_TLSDESC
:
1130 gold_error(_("%s: unexpected reloc %u in object file"),
1131 object
->name().c_str(), r_type
);
1134 // These are initial tls relocs, which are expected when linking
1135 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1136 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1137 case elfcpp::R_X86_64_TLSDESC_CALL
:
1138 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1139 case elfcpp::R_X86_64_DTPOFF32
:
1140 case elfcpp::R_X86_64_DTPOFF64
:
1141 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1142 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1144 bool output_is_shared
= parameters
->options().shared();
1145 const tls::Tls_optimization optimized_type
1146 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
1149 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1150 if (optimized_type
== tls::TLSOPT_NONE
)
1152 // Create a pair of GOT entries for the module index and
1153 // dtv-relative offset.
1154 Output_data_got
<64, false>* got
1155 = target
->got_section(symtab
, layout
);
1156 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1157 unsigned int shndx
= lsym
.get_st_shndx();
1159 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1161 object
->error(_("local symbol %u has bad shndx %u"),
1164 got
->add_local_pair_with_rela(object
, r_sym
,
1167 target
->rela_dyn_section(layout
),
1168 elfcpp::R_X86_64_DTPMOD64
, 0);
1170 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1171 unsupported_reloc_local(object
, r_type
);
1174 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1175 target
->define_tls_base_symbol(symtab
, layout
);
1176 if (optimized_type
== tls::TLSOPT_NONE
)
1178 // Create reserved PLT and GOT entries for the resolver.
1179 target
->reserve_tlsdesc_entries(symtab
, layout
);
1181 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1182 Output_data_got
<64, false>* got
1183 = target
->got_section(symtab
, layout
);
1184 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1185 unsigned int shndx
= lsym
.get_st_shndx();
1187 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1189 object
->error(_("local symbol %u has bad shndx %u"),
1192 got
->add_local_pair_with_rela(object
, r_sym
,
1195 target
->rela_dyn_section(layout
),
1196 elfcpp::R_X86_64_TLSDESC
, 0);
1198 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1199 unsupported_reloc_local(object
, r_type
);
1202 case elfcpp::R_X86_64_TLSDESC_CALL
:
1205 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1206 if (optimized_type
== tls::TLSOPT_NONE
)
1208 // Create a GOT entry for the module index.
1209 target
->got_mod_index_entry(symtab
, layout
, object
);
1211 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1212 unsupported_reloc_local(object
, r_type
);
1215 case elfcpp::R_X86_64_DTPOFF32
:
1216 case elfcpp::R_X86_64_DTPOFF64
:
1219 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1220 layout
->set_has_static_tls();
1221 if (optimized_type
== tls::TLSOPT_NONE
)
1223 // Create a GOT entry for the tp-relative offset.
1224 Output_data_got
<64, false>* got
1225 = target
->got_section(symtab
, layout
);
1226 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1227 got
->add_local_with_rela(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1228 target
->rela_dyn_section(layout
),
1229 elfcpp::R_X86_64_TPOFF64
);
1231 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1232 unsupported_reloc_local(object
, r_type
);
1235 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1236 layout
->set_has_static_tls();
1237 if (output_is_shared
)
1238 unsupported_reloc_local(object
, r_type
);
1247 case elfcpp::R_X86_64_SIZE32
:
1248 case elfcpp::R_X86_64_SIZE64
:
1250 gold_error(_("%s: unsupported reloc %u against local symbol"),
1251 object
->name().c_str(), r_type
);
1257 // Report an unsupported relocation against a global symbol.
1260 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
1261 unsigned int r_type
,
1264 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1265 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1268 // Scan a relocation for a global symbol.
1271 Target_x86_64::Scan::global(const General_options
&,
1272 Symbol_table
* symtab
,
1274 Target_x86_64
* target
,
1275 Sized_relobj
<64, false>* object
,
1276 unsigned int data_shndx
,
1277 Output_section
* output_section
,
1278 const elfcpp::Rela
<64, false>& reloc
,
1279 unsigned int r_type
,
1284 case elfcpp::R_X86_64_NONE
:
1285 case elfcpp::R_386_GNU_VTINHERIT
:
1286 case elfcpp::R_386_GNU_VTENTRY
:
1289 case elfcpp::R_X86_64_64
:
1290 case elfcpp::R_X86_64_32
:
1291 case elfcpp::R_X86_64_32S
:
1292 case elfcpp::R_X86_64_16
:
1293 case elfcpp::R_X86_64_8
:
1295 // Make a PLT entry if necessary.
1296 if (gsym
->needs_plt_entry())
1298 target
->make_plt_entry(symtab
, layout
, gsym
);
1299 // Since this is not a PC-relative relocation, we may be
1300 // taking the address of a function. In that case we need to
1301 // set the entry in the dynamic symbol table to the address of
1303 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1304 gsym
->set_needs_dynsym_value();
1306 // Make a dynamic relocation if necessary.
1307 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1309 if (target
->may_need_copy_reloc(gsym
))
1311 target
->copy_reloc(symtab
, layout
, object
,
1312 data_shndx
, output_section
, gsym
, reloc
);
1314 else if (r_type
== elfcpp::R_X86_64_64
1315 && gsym
->can_use_relative_reloc(false))
1317 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1318 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1319 output_section
, object
,
1320 data_shndx
, reloc
.get_r_offset(),
1321 reloc
.get_r_addend());
1325 this->check_non_pic(object
, r_type
);
1326 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1327 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1328 data_shndx
, reloc
.get_r_offset(),
1329 reloc
.get_r_addend());
1335 case elfcpp::R_X86_64_PC64
:
1336 case elfcpp::R_X86_64_PC32
:
1337 case elfcpp::R_X86_64_PC16
:
1338 case elfcpp::R_X86_64_PC8
:
1340 // Make a PLT entry if necessary.
1341 if (gsym
->needs_plt_entry())
1342 target
->make_plt_entry(symtab
, layout
, gsym
);
1343 // Make a dynamic relocation if necessary.
1344 int flags
= Symbol::NON_PIC_REF
;
1345 if (gsym
->type() == elfcpp::STT_FUNC
)
1346 flags
|= Symbol::FUNCTION_CALL
;
1347 if (gsym
->needs_dynamic_reloc(flags
))
1349 if (target
->may_need_copy_reloc(gsym
))
1351 target
->copy_reloc(symtab
, layout
, object
,
1352 data_shndx
, output_section
, gsym
, reloc
);
1356 this->check_non_pic(object
, r_type
);
1357 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1358 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1359 data_shndx
, reloc
.get_r_offset(),
1360 reloc
.get_r_addend());
1366 case elfcpp::R_X86_64_GOT64
:
1367 case elfcpp::R_X86_64_GOT32
:
1368 case elfcpp::R_X86_64_GOTPCREL64
:
1369 case elfcpp::R_X86_64_GOTPCREL
:
1370 case elfcpp::R_X86_64_GOTPLT64
:
1372 // The symbol requires a GOT entry.
1373 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1374 if (gsym
->final_value_is_known())
1375 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1378 // If this symbol is not fully resolved, we need to add a
1379 // dynamic relocation for it.
1380 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1381 if (gsym
->is_from_dynobj()
1382 || gsym
->is_undefined()
1383 || gsym
->is_preemptible())
1384 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1385 elfcpp::R_X86_64_GLOB_DAT
);
1388 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1389 rela_dyn
->add_global_relative(
1390 gsym
, elfcpp::R_X86_64_RELATIVE
, got
,
1391 gsym
->got_offset(GOT_TYPE_STANDARD
), 0);
1394 // For GOTPLT64, we also need a PLT entry (but only if the
1395 // symbol is not fully resolved).
1396 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1397 && !gsym
->final_value_is_known())
1398 target
->make_plt_entry(symtab
, layout
, gsym
);
1402 case elfcpp::R_X86_64_PLT32
:
1403 // If the symbol is fully resolved, this is just a PC32 reloc.
1404 // Otherwise we need a PLT entry.
1405 if (gsym
->final_value_is_known())
1407 // If building a shared library, we can also skip the PLT entry
1408 // if the symbol is defined in the output file and is protected
1410 if (gsym
->is_defined()
1411 && !gsym
->is_from_dynobj()
1412 && !gsym
->is_preemptible())
1414 target
->make_plt_entry(symtab
, layout
, gsym
);
1417 case elfcpp::R_X86_64_GOTPC32
:
1418 case elfcpp::R_X86_64_GOTOFF64
:
1419 case elfcpp::R_X86_64_GOTPC64
:
1420 case elfcpp::R_X86_64_PLTOFF64
:
1421 // We need a GOT section.
1422 target
->got_section(symtab
, layout
);
1423 // For PLTOFF64, we also need a PLT entry (but only if the
1424 // symbol is not fully resolved).
1425 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1426 && !gsym
->final_value_is_known())
1427 target
->make_plt_entry(symtab
, layout
, gsym
);
1430 case elfcpp::R_X86_64_COPY
:
1431 case elfcpp::R_X86_64_GLOB_DAT
:
1432 case elfcpp::R_X86_64_JUMP_SLOT
:
1433 case elfcpp::R_X86_64_RELATIVE
:
1434 // These are outstanding tls relocs, which are unexpected when linking
1435 case elfcpp::R_X86_64_TPOFF64
:
1436 case elfcpp::R_X86_64_DTPMOD64
:
1437 case elfcpp::R_X86_64_TLSDESC
:
1438 gold_error(_("%s: unexpected reloc %u in object file"),
1439 object
->name().c_str(), r_type
);
1442 // These are initial tls relocs, which are expected for global()
1443 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1444 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1445 case elfcpp::R_X86_64_TLSDESC_CALL
:
1446 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1447 case elfcpp::R_X86_64_DTPOFF32
:
1448 case elfcpp::R_X86_64_DTPOFF64
:
1449 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1450 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1452 const bool is_final
= gsym
->final_value_is_known();
1453 const tls::Tls_optimization optimized_type
1454 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1457 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1458 if (optimized_type
== tls::TLSOPT_NONE
)
1460 // Create a pair of GOT entries for the module index and
1461 // dtv-relative offset.
1462 Output_data_got
<64, false>* got
1463 = target
->got_section(symtab
, layout
);
1464 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_PAIR
,
1465 target
->rela_dyn_section(layout
),
1466 elfcpp::R_X86_64_DTPMOD64
,
1467 elfcpp::R_X86_64_DTPOFF64
);
1469 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1471 // Create a GOT entry for the tp-relative offset.
1472 Output_data_got
<64, false>* got
1473 = target
->got_section(symtab
, layout
);
1474 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1475 target
->rela_dyn_section(layout
),
1476 elfcpp::R_X86_64_TPOFF64
);
1478 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1479 unsupported_reloc_global(object
, r_type
, gsym
);
1482 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1483 target
->define_tls_base_symbol(symtab
, layout
);
1484 if (optimized_type
== tls::TLSOPT_NONE
)
1486 // Create reserved PLT and GOT entries for the resolver.
1487 target
->reserve_tlsdesc_entries(symtab
, layout
);
1489 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1490 Output_data_got
<64, false>* got
1491 = target
->got_section(symtab
, layout
);
1492 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_DESC
,
1493 target
->rela_dyn_section(layout
),
1494 elfcpp::R_X86_64_TLSDESC
, 0);
1496 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1498 // Create a GOT entry for the tp-relative offset.
1499 Output_data_got
<64, false>* got
1500 = target
->got_section(symtab
, layout
);
1501 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1502 target
->rela_dyn_section(layout
),
1503 elfcpp::R_X86_64_TPOFF64
);
1505 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1506 unsupported_reloc_global(object
, r_type
, gsym
);
1509 case elfcpp::R_X86_64_TLSDESC_CALL
:
1512 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1513 if (optimized_type
== tls::TLSOPT_NONE
)
1515 // Create a GOT entry for the module index.
1516 target
->got_mod_index_entry(symtab
, layout
, object
);
1518 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1519 unsupported_reloc_global(object
, r_type
, gsym
);
1522 case elfcpp::R_X86_64_DTPOFF32
:
1523 case elfcpp::R_X86_64_DTPOFF64
:
1526 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1527 layout
->set_has_static_tls();
1528 if (optimized_type
== tls::TLSOPT_NONE
)
1530 // Create a GOT entry for the tp-relative offset.
1531 Output_data_got
<64, false>* got
1532 = target
->got_section(symtab
, layout
);
1533 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1534 target
->rela_dyn_section(layout
),
1535 elfcpp::R_X86_64_TPOFF64
);
1537 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1538 unsupported_reloc_global(object
, r_type
, gsym
);
1541 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1542 layout
->set_has_static_tls();
1543 if (parameters
->options().shared())
1544 unsupported_reloc_local(object
, r_type
);
1553 case elfcpp::R_X86_64_SIZE32
:
1554 case elfcpp::R_X86_64_SIZE64
:
1556 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1557 object
->name().c_str(), r_type
,
1558 gsym
->demangled_name().c_str());
1564 Target_x86_64::gc_process_relocs(const General_options
& options
,
1565 Symbol_table
* symtab
,
1567 Sized_relobj
<64, false>* object
,
1568 unsigned int data_shndx
,
1569 unsigned int sh_type
,
1570 const unsigned char* prelocs
,
1572 Output_section
* output_section
,
1573 bool needs_special_offset_handling
,
1574 size_t local_symbol_count
,
1575 const unsigned char* plocal_symbols
)
1578 if (sh_type
== elfcpp::SHT_REL
)
1583 gold::gc_process_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1584 Target_x86_64::Scan
>(
1594 needs_special_offset_handling
,
1599 // Scan relocations for a section.
1602 Target_x86_64::scan_relocs(const General_options
& options
,
1603 Symbol_table
* symtab
,
1605 Sized_relobj
<64, false>* object
,
1606 unsigned int data_shndx
,
1607 unsigned int sh_type
,
1608 const unsigned char* prelocs
,
1610 Output_section
* output_section
,
1611 bool needs_special_offset_handling
,
1612 size_t local_symbol_count
,
1613 const unsigned char* plocal_symbols
)
1615 if (sh_type
== elfcpp::SHT_REL
)
1617 gold_error(_("%s: unsupported REL reloc section"),
1618 object
->name().c_str());
1622 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1623 Target_x86_64::Scan
>(
1633 needs_special_offset_handling
,
1638 // Finalize the sections.
1641 Target_x86_64::do_finalize_sections(Layout
* layout
)
1643 // Fill in some more dynamic tags.
1644 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1647 if (this->got_plt_
!= NULL
)
1648 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1650 if (this->plt_
!= NULL
)
1652 const Output_data
* od
= this->plt_
->rel_plt();
1653 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1654 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1655 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1656 if (this->plt_
->has_tlsdesc_entry())
1658 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
1659 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
1660 this->got_
->finalize_data_size();
1661 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
1662 this->plt_
, plt_offset
);
1663 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
1664 this->got_
, got_offset
);
1668 if (this->rela_dyn_
!= NULL
)
1670 const Output_data
* od
= this->rela_dyn_
;
1671 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1672 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1673 odyn
->add_constant(elfcpp::DT_RELAENT
,
1674 elfcpp::Elf_sizes
<64>::rela_size
);
1677 if (!parameters
->options().shared())
1679 // The value of the DT_DEBUG tag is filled in by the dynamic
1680 // linker at run time, and used by the debugger.
1681 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1685 // Emit any relocs we saved in an attempt to avoid generating COPY
1687 if (this->copy_relocs_
.any_saved_relocs())
1688 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1691 // Perform a relocation.
1694 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1695 Target_x86_64
* target
,
1698 const elfcpp::Rela
<64, false>& rela
,
1699 unsigned int r_type
,
1700 const Sized_symbol
<64>* gsym
,
1701 const Symbol_value
<64>* psymval
,
1702 unsigned char* view
,
1703 elfcpp::Elf_types
<64>::Elf_Addr address
,
1704 section_size_type view_size
)
1706 if (this->skip_call_tls_get_addr_
)
1708 if ((r_type
!= elfcpp::R_X86_64_PLT32
1709 && r_type
!= elfcpp::R_X86_64_PC32
)
1711 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1713 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1714 _("missing expected TLS relocation"));
1718 this->skip_call_tls_get_addr_
= false;
1723 // Pick the value to use for symbols defined in shared objects.
1724 Symbol_value
<64> symval
;
1726 && gsym
->use_plt_offset(r_type
== elfcpp::R_X86_64_PC64
1727 || r_type
== elfcpp::R_X86_64_PC32
1728 || r_type
== elfcpp::R_X86_64_PC16
1729 || r_type
== elfcpp::R_X86_64_PC8
))
1731 symval
.set_output_value(target
->plt_section()->address()
1732 + gsym
->plt_offset());
1736 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1737 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1739 // Get the GOT offset if needed.
1740 // The GOT pointer points to the end of the GOT section.
1741 // We need to subtract the size of the GOT section to get
1742 // the actual offset to use in the relocation.
1743 bool have_got_offset
= false;
1744 unsigned int got_offset
= 0;
1747 case elfcpp::R_X86_64_GOT32
:
1748 case elfcpp::R_X86_64_GOT64
:
1749 case elfcpp::R_X86_64_GOTPLT64
:
1750 case elfcpp::R_X86_64_GOTPCREL
:
1751 case elfcpp::R_X86_64_GOTPCREL64
:
1754 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1755 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
1759 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1760 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1761 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1762 - target
->got_size());
1764 have_got_offset
= true;
1773 case elfcpp::R_X86_64_NONE
:
1774 case elfcpp::R_386_GNU_VTINHERIT
:
1775 case elfcpp::R_386_GNU_VTENTRY
:
1778 case elfcpp::R_X86_64_64
:
1779 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1782 case elfcpp::R_X86_64_PC64
:
1783 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1787 case elfcpp::R_X86_64_32
:
1788 // FIXME: we need to verify that value + addend fits into 32 bits:
1789 // uint64_t x = value + addend;
1790 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1791 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1792 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1795 case elfcpp::R_X86_64_32S
:
1796 // FIXME: we need to verify that value + addend fits into 32 bits:
1797 // int64_t x = value + addend; // note this quantity is signed!
1798 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1799 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1802 case elfcpp::R_X86_64_PC32
:
1803 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1807 case elfcpp::R_X86_64_16
:
1808 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1811 case elfcpp::R_X86_64_PC16
:
1812 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1816 case elfcpp::R_X86_64_8
:
1817 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1820 case elfcpp::R_X86_64_PC8
:
1821 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1825 case elfcpp::R_X86_64_PLT32
:
1826 gold_assert(gsym
== NULL
1827 || gsym
->has_plt_offset()
1828 || gsym
->final_value_is_known()
1829 || (gsym
->is_defined()
1830 && !gsym
->is_from_dynobj()
1831 && !gsym
->is_preemptible()));
1832 // Note: while this code looks the same as for R_X86_64_PC32, it
1833 // behaves differently because psymval was set to point to
1834 // the PLT entry, rather than the symbol, in Scan::global().
1835 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1839 case elfcpp::R_X86_64_PLTOFF64
:
1842 gold_assert(gsym
->has_plt_offset()
1843 || gsym
->final_value_is_known());
1844 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
1845 got_address
= target
->got_section(NULL
, NULL
)->address();
1846 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
1847 addend
- got_address
);
1850 case elfcpp::R_X86_64_GOT32
:
1851 gold_assert(have_got_offset
);
1852 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1855 case elfcpp::R_X86_64_GOTPC32
:
1858 elfcpp::Elf_types
<64>::Elf_Addr value
;
1859 value
= target
->got_plt_section()->address();
1860 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1864 case elfcpp::R_X86_64_GOT64
:
1865 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1866 // Since we always add a PLT entry, this is equivalent.
1867 case elfcpp::R_X86_64_GOTPLT64
:
1868 gold_assert(have_got_offset
);
1869 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1872 case elfcpp::R_X86_64_GOTPC64
:
1875 elfcpp::Elf_types
<64>::Elf_Addr value
;
1876 value
= target
->got_plt_section()->address();
1877 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1881 case elfcpp::R_X86_64_GOTOFF64
:
1883 elfcpp::Elf_types
<64>::Elf_Addr value
;
1884 value
= (psymval
->value(object
, 0)
1885 - target
->got_plt_section()->address());
1886 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1890 case elfcpp::R_X86_64_GOTPCREL
:
1892 gold_assert(have_got_offset
);
1893 elfcpp::Elf_types
<64>::Elf_Addr value
;
1894 value
= target
->got_plt_section()->address() + got_offset
;
1895 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1899 case elfcpp::R_X86_64_GOTPCREL64
:
1901 gold_assert(have_got_offset
);
1902 elfcpp::Elf_types
<64>::Elf_Addr value
;
1903 value
= target
->got_plt_section()->address() + got_offset
;
1904 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1908 case elfcpp::R_X86_64_COPY
:
1909 case elfcpp::R_X86_64_GLOB_DAT
:
1910 case elfcpp::R_X86_64_JUMP_SLOT
:
1911 case elfcpp::R_X86_64_RELATIVE
:
1912 // These are outstanding tls relocs, which are unexpected when linking
1913 case elfcpp::R_X86_64_TPOFF64
:
1914 case elfcpp::R_X86_64_DTPMOD64
:
1915 case elfcpp::R_X86_64_TLSDESC
:
1916 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1917 _("unexpected reloc %u in object file"),
1921 // These are initial tls relocs, which are expected when linking
1922 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1923 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1924 case elfcpp::R_X86_64_TLSDESC_CALL
:
1925 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1926 case elfcpp::R_X86_64_DTPOFF32
:
1927 case elfcpp::R_X86_64_DTPOFF64
:
1928 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1929 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1930 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
1931 view
, address
, view_size
);
1934 case elfcpp::R_X86_64_SIZE32
:
1935 case elfcpp::R_X86_64_SIZE64
:
1937 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1938 _("unsupported reloc %u"),
1946 // Perform a TLS relocation.
1949 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1950 Target_x86_64
* target
,
1952 const elfcpp::Rela
<64, false>& rela
,
1953 unsigned int r_type
,
1954 const Sized_symbol
<64>* gsym
,
1955 const Symbol_value
<64>* psymval
,
1956 unsigned char* view
,
1957 elfcpp::Elf_types
<64>::Elf_Addr address
,
1958 section_size_type view_size
)
1960 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1962 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1963 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1965 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1967 const bool is_final
= (gsym
== NULL
1968 ? !parameters
->options().output_is_position_independent()
1969 : gsym
->final_value_is_known());
1970 const tls::Tls_optimization optimized_type
1971 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1974 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1975 this->saw_tls_block_reloc_
= true;
1976 if (optimized_type
== tls::TLSOPT_TO_LE
)
1978 gold_assert(tls_segment
!= NULL
);
1979 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1980 rela
, r_type
, value
, view
,
1986 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1987 ? GOT_TYPE_TLS_OFFSET
1988 : GOT_TYPE_TLS_PAIR
);
1989 unsigned int got_offset
;
1992 gold_assert(gsym
->has_got_offset(got_type
));
1993 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1997 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1998 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1999 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2000 - target
->got_size());
2002 if (optimized_type
== tls::TLSOPT_TO_IE
)
2004 gold_assert(tls_segment
!= NULL
);
2005 value
= target
->got_plt_section()->address() + got_offset
;
2006 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2007 value
, view
, address
, view_size
);
2010 else if (optimized_type
== tls::TLSOPT_NONE
)
2012 // Relocate the field with the offset of the pair of GOT
2014 value
= target
->got_plt_section()->address() + got_offset
;
2015 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2020 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2021 _("unsupported reloc %u"), r_type
);
2024 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2025 case elfcpp::R_X86_64_TLSDESC_CALL
:
2026 this->saw_tls_block_reloc_
= true;
2027 if (optimized_type
== tls::TLSOPT_TO_LE
)
2029 gold_assert(tls_segment
!= NULL
);
2030 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2031 rela
, r_type
, value
, view
,
2037 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2038 ? GOT_TYPE_TLS_OFFSET
2039 : GOT_TYPE_TLS_DESC
);
2040 unsigned int got_offset
;
2043 gold_assert(gsym
->has_got_offset(got_type
));
2044 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2048 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2049 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2050 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2051 - target
->got_size());
2053 if (optimized_type
== tls::TLSOPT_TO_IE
)
2055 gold_assert(tls_segment
!= NULL
);
2056 value
= target
->got_plt_section()->address() + got_offset
;
2057 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
2058 rela
, r_type
, value
, view
, address
,
2062 else if (optimized_type
== tls::TLSOPT_NONE
)
2064 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2066 // Relocate the field with the offset of the pair of GOT
2068 value
= target
->got_plt_section()->address() + got_offset
;
2069 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2075 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2076 _("unsupported reloc %u"), r_type
);
2079 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2080 this->saw_tls_block_reloc_
= true;
2081 if (optimized_type
== tls::TLSOPT_TO_LE
)
2083 gold_assert(tls_segment
!= NULL
);
2084 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2085 value
, view
, view_size
);
2088 else if (optimized_type
== tls::TLSOPT_NONE
)
2090 // Relocate the field with the offset of the GOT entry for
2091 // the module index.
2092 unsigned int got_offset
;
2093 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2094 - target
->got_size());
2095 value
= target
->got_plt_section()->address() + got_offset
;
2096 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2100 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2101 _("unsupported reloc %u"), r_type
);
2104 case elfcpp::R_X86_64_DTPOFF32
:
2105 gold_assert(tls_segment
!= NULL
);
2106 if (optimized_type
== tls::TLSOPT_TO_LE
)
2108 // This relocation type is used in debugging information.
2109 // In that case we need to not optimize the value. If we
2110 // haven't seen a TLSLD reloc, then we assume we should not
2111 // optimize this reloc.
2112 if (this->saw_tls_block_reloc_
)
2113 value
-= tls_segment
->memsz();
2115 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2118 case elfcpp::R_X86_64_DTPOFF64
:
2119 gold_assert(tls_segment
!= NULL
);
2120 if (optimized_type
== tls::TLSOPT_TO_LE
)
2122 // See R_X86_64_DTPOFF32, just above, for why we test this.
2123 if (this->saw_tls_block_reloc_
)
2124 value
-= tls_segment
->memsz();
2126 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
2129 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2130 if (optimized_type
== tls::TLSOPT_TO_LE
)
2132 gold_assert(tls_segment
!= NULL
);
2133 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2134 rela
, r_type
, value
, view
,
2138 else if (optimized_type
== tls::TLSOPT_NONE
)
2140 // Relocate the field with the offset of the GOT entry for
2141 // the tp-relative offset of the symbol.
2142 unsigned int got_offset
;
2145 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
2146 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
2147 - target
->got_size());
2151 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2152 gold_assert(object
->local_has_got_offset(r_sym
,
2153 GOT_TYPE_TLS_OFFSET
));
2154 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
2155 - target
->got_size());
2157 value
= target
->got_plt_section()->address() + got_offset
;
2158 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2161 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2162 _("unsupported reloc type %u"),
2166 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2167 value
-= tls_segment
->memsz();
2168 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2173 // Do a relocation in which we convert a TLS General-Dynamic to an
2177 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
2180 const elfcpp::Rela
<64, false>& rela
,
2182 elfcpp::Elf_types
<64>::Elf_Addr value
,
2183 unsigned char* view
,
2184 elfcpp::Elf_types
<64>::Elf_Addr address
,
2185 section_size_type view_size
)
2187 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2188 // .word 0x6666; rex64; call __tls_get_addr
2189 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2191 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2192 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2194 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2195 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2196 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2197 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2199 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2201 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2202 Relocate_functions
<64, false>::pcrela32(view
+ 8, value
, addend
- 8, address
);
2204 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2206 this->skip_call_tls_get_addr_
= true;
2209 // Do a relocation in which we convert a TLS General-Dynamic to a
2213 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
2215 Output_segment
* tls_segment
,
2216 const elfcpp::Rela
<64, false>& rela
,
2218 elfcpp::Elf_types
<64>::Elf_Addr value
,
2219 unsigned char* view
,
2220 section_size_type view_size
)
2222 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2223 // .word 0x6666; rex64; call __tls_get_addr
2224 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2226 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2227 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2229 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2230 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2231 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2232 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2234 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2236 value
-= tls_segment
->memsz();
2237 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
2239 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2241 this->skip_call_tls_get_addr_
= true;
2244 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2247 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2248 const Relocate_info
<64, false>* relinfo
,
2251 const elfcpp::Rela
<64, false>& rela
,
2252 unsigned int r_type
,
2253 elfcpp::Elf_types
<64>::Elf_Addr value
,
2254 unsigned char* view
,
2255 elfcpp::Elf_types
<64>::Elf_Addr address
,
2256 section_size_type view_size
)
2258 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2260 // leaq foo@tlsdesc(%rip), %rax
2261 // ==> movq foo@gottpoff(%rip), %rax
2262 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2263 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2264 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2265 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2267 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2268 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2272 // call *foo@tlscall(%rax)
2274 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2275 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2276 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2277 view
[0] == 0xff && view
[1] == 0x10);
2283 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2286 Target_x86_64::Relocate::tls_desc_gd_to_le(
2287 const Relocate_info
<64, false>* relinfo
,
2289 Output_segment
* tls_segment
,
2290 const elfcpp::Rela
<64, false>& rela
,
2291 unsigned int r_type
,
2292 elfcpp::Elf_types
<64>::Elf_Addr value
,
2293 unsigned char* view
,
2294 section_size_type view_size
)
2296 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2298 // leaq foo@tlsdesc(%rip), %rax
2299 // ==> movq foo@tpoff, %rax
2300 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2301 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2302 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2303 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2306 value
-= tls_segment
->memsz();
2307 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2311 // call *foo@tlscall(%rax)
2313 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2314 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2315 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2316 view
[0] == 0xff && view
[1] == 0x10);
2323 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
2326 const elfcpp::Rela
<64, false>& rela
,
2328 elfcpp::Elf_types
<64>::Elf_Addr
,
2329 unsigned char* view
,
2330 section_size_type view_size
)
2332 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2333 // ... leq foo@dtpoff(%rax),%reg
2334 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2336 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2337 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
2339 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2340 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
2342 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
2344 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2346 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2348 this->skip_call_tls_get_addr_
= true;
2351 // Do a relocation in which we convert a TLS Initial-Exec to a
2355 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
2357 Output_segment
* tls_segment
,
2358 const elfcpp::Rela
<64, false>& rela
,
2360 elfcpp::Elf_types
<64>::Elf_Addr value
,
2361 unsigned char* view
,
2362 section_size_type view_size
)
2364 // We need to examine the opcodes to figure out which instruction we
2367 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2368 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2370 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2371 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2373 unsigned char op1
= view
[-3];
2374 unsigned char op2
= view
[-2];
2375 unsigned char op3
= view
[-1];
2376 unsigned char reg
= op3
>> 3;
2384 view
[-1] = 0xc0 | reg
;
2388 // Special handling for %rsp.
2392 view
[-1] = 0xc0 | reg
;
2400 view
[-1] = 0x80 | reg
| (reg
<< 3);
2403 value
-= tls_segment
->memsz();
2404 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2407 // Relocate section data.
2410 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
2411 unsigned int sh_type
,
2412 const unsigned char* prelocs
,
2414 Output_section
* output_section
,
2415 bool needs_special_offset_handling
,
2416 unsigned char* view
,
2417 elfcpp::Elf_types
<64>::Elf_Addr address
,
2418 section_size_type view_size
)
2420 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2422 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2423 Target_x86_64::Relocate
>(
2429 needs_special_offset_handling
,
2435 // Return the size of a relocation while scanning during a relocatable
2439 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2440 unsigned int r_type
,
2445 case elfcpp::R_X86_64_NONE
:
2446 case elfcpp::R_386_GNU_VTINHERIT
:
2447 case elfcpp::R_386_GNU_VTENTRY
:
2448 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2449 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2450 case elfcpp::R_X86_64_TLSDESC_CALL
:
2451 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2452 case elfcpp::R_X86_64_DTPOFF32
:
2453 case elfcpp::R_X86_64_DTPOFF64
:
2454 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2455 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2458 case elfcpp::R_X86_64_64
:
2459 case elfcpp::R_X86_64_PC64
:
2460 case elfcpp::R_X86_64_GOTOFF64
:
2461 case elfcpp::R_X86_64_GOTPC64
:
2462 case elfcpp::R_X86_64_PLTOFF64
:
2463 case elfcpp::R_X86_64_GOT64
:
2464 case elfcpp::R_X86_64_GOTPCREL64
:
2465 case elfcpp::R_X86_64_GOTPCREL
:
2466 case elfcpp::R_X86_64_GOTPLT64
:
2469 case elfcpp::R_X86_64_32
:
2470 case elfcpp::R_X86_64_32S
:
2471 case elfcpp::R_X86_64_PC32
:
2472 case elfcpp::R_X86_64_PLT32
:
2473 case elfcpp::R_X86_64_GOTPC32
:
2474 case elfcpp::R_X86_64_GOT32
:
2477 case elfcpp::R_X86_64_16
:
2478 case elfcpp::R_X86_64_PC16
:
2481 case elfcpp::R_X86_64_8
:
2482 case elfcpp::R_X86_64_PC8
:
2485 case elfcpp::R_X86_64_COPY
:
2486 case elfcpp::R_X86_64_GLOB_DAT
:
2487 case elfcpp::R_X86_64_JUMP_SLOT
:
2488 case elfcpp::R_X86_64_RELATIVE
:
2489 // These are outstanding tls relocs, which are unexpected when linking
2490 case elfcpp::R_X86_64_TPOFF64
:
2491 case elfcpp::R_X86_64_DTPMOD64
:
2492 case elfcpp::R_X86_64_TLSDESC
:
2493 object
->error(_("unexpected reloc %u in object file"), r_type
);
2496 case elfcpp::R_X86_64_SIZE32
:
2497 case elfcpp::R_X86_64_SIZE64
:
2499 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
2504 // Scan the relocs during a relocatable link.
2507 Target_x86_64::scan_relocatable_relocs(const General_options
& options
,
2508 Symbol_table
* symtab
,
2510 Sized_relobj
<64, false>* object
,
2511 unsigned int data_shndx
,
2512 unsigned int sh_type
,
2513 const unsigned char* prelocs
,
2515 Output_section
* output_section
,
2516 bool needs_special_offset_handling
,
2517 size_t local_symbol_count
,
2518 const unsigned char* plocal_symbols
,
2519 Relocatable_relocs
* rr
)
2521 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2523 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
2524 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2526 gold::scan_relocatable_relocs
<64, false, elfcpp::SHT_RELA
,
2527 Scan_relocatable_relocs
>(
2536 needs_special_offset_handling
,
2542 // Relocate a section during a relocatable link.
2545 Target_x86_64::relocate_for_relocatable(
2546 const Relocate_info
<64, false>* relinfo
,
2547 unsigned int sh_type
,
2548 const unsigned char* prelocs
,
2550 Output_section
* output_section
,
2551 off_t offset_in_output_section
,
2552 const Relocatable_relocs
* rr
,
2553 unsigned char* view
,
2554 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
2555 section_size_type view_size
,
2556 unsigned char* reloc_view
,
2557 section_size_type reloc_view_size
)
2559 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2561 gold::relocate_for_relocatable
<64, false, elfcpp::SHT_RELA
>(
2566 offset_in_output_section
,
2575 // Return the value to use for a dynamic which requires special
2576 // treatment. This is how we support equality comparisons of function
2577 // pointers across shared library boundaries, as described in the
2578 // processor specific ABI supplement.
2581 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
2583 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2584 return this->plt_section()->address() + gsym
->plt_offset();
2587 // Return a string used to fill a code section with nops to take up
2588 // the specified length.
2591 Target_x86_64::do_code_fill(section_size_type length
) const
2595 // Build a jmpq instruction to skip over the bytes.
2596 unsigned char jmp
[5];
2598 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2599 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2600 + std::string(length
- 5, '\0'));
2603 // Nop sequences of various lengths.
2604 const char nop1
[1] = { 0x90 }; // nop
2605 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2606 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2607 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2608 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2609 0x00 }; // leal 0(%esi,1),%esi
2610 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2612 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2614 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2615 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2616 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2617 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2619 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2620 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2622 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2623 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2625 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2626 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2627 0x00, 0x00, 0x00, 0x00 };
2628 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2629 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2630 0x27, 0x00, 0x00, 0x00,
2632 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2633 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2634 0xbc, 0x27, 0x00, 0x00,
2636 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2637 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2638 0x90, 0x90, 0x90, 0x90,
2641 const char* nops
[16] = {
2643 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2644 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2647 return std::string(nops
[length
], length
);
2650 // The selector for x86_64 object files.
2652 class Target_selector_x86_64
: public Target_selector_freebsd
2655 Target_selector_x86_64()
2656 : Target_selector_freebsd(elfcpp::EM_X86_64
, 64, false, "elf64-x86-64",
2657 "elf64-x86-64-freebsd")
2661 do_instantiate_target()
2662 { return new Target_x86_64(); }
2666 Target_selector_x86_64 target_selector_x86_64
;
2668 } // End anonymous namespace.