1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
46 class Output_data_plt_x86_64
;
48 // The x86_64 target class.
50 // http://www.x86-64.org/documentation/abi.pdf
51 // TLS info comes from
52 // http://people.redhat.com/drepper/tls.pdf
53 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
55 class Target_x86_64
: public Sized_target
<64, false>
58 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
59 // uses only Elf64_Rela relocation entries with explicit addends."
60 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
63 : Sized_target
<64, false>(&x86_64_info
),
64 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rela_dyn_(NULL
),
65 copy_relocs_(elfcpp::R_X86_64_COPY
), dynbss_(NULL
),
66 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
69 // Scan the relocations to look for symbol adjustments.
71 scan_relocs(const General_options
& options
,
74 Sized_relobj
<64, false>* object
,
75 unsigned int data_shndx
,
77 const unsigned char* prelocs
,
79 Output_section
* output_section
,
80 bool needs_special_offset_handling
,
81 size_t local_symbol_count
,
82 const unsigned char* plocal_symbols
);
84 // Finalize the sections.
86 do_finalize_sections(Layout
*);
88 // Return the value to use for a dynamic which requires special
91 do_dynsym_value(const Symbol
*) const;
93 // Relocate a section.
95 relocate_section(const Relocate_info
<64, false>*,
97 const unsigned char* prelocs
,
99 Output_section
* output_section
,
100 bool needs_special_offset_handling
,
102 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
103 section_size_type view_size
);
105 // Scan the relocs during a relocatable link.
107 scan_relocatable_relocs(const General_options
& options
,
108 Symbol_table
* symtab
,
110 Sized_relobj
<64, false>* object
,
111 unsigned int data_shndx
,
112 unsigned int sh_type
,
113 const unsigned char* prelocs
,
115 Output_section
* output_section
,
116 bool needs_special_offset_handling
,
117 size_t local_symbol_count
,
118 const unsigned char* plocal_symbols
,
119 Relocatable_relocs
*);
121 // Relocate a section during a relocatable link.
123 relocate_for_relocatable(const Relocate_info
<64, false>*,
124 unsigned int sh_type
,
125 const unsigned char* prelocs
,
127 Output_section
* output_section
,
128 off_t offset_in_output_section
,
129 const Relocatable_relocs
*,
131 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
132 section_size_type view_size
,
133 unsigned char* reloc_view
,
134 section_size_type reloc_view_size
);
136 // Return a string used to fill a code section with nops.
138 do_code_fill(section_size_type length
) const;
140 // Return whether SYM is defined by the ABI.
142 do_is_defined_by_abi(const Symbol
* sym
) const
143 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
145 // Return the size of the GOT section.
149 gold_assert(this->got_
!= NULL
);
150 return this->got_
->data_size();
154 // The class which scans relocations.
159 : issued_non_pic_error_(false)
163 local(const General_options
& options
, Symbol_table
* symtab
,
164 Layout
* layout
, Target_x86_64
* target
,
165 Sized_relobj
<64, false>* object
,
166 unsigned int data_shndx
,
167 Output_section
* output_section
,
168 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
169 const elfcpp::Sym
<64, false>& lsym
);
172 global(const General_options
& options
, Symbol_table
* symtab
,
173 Layout
* layout
, Target_x86_64
* target
,
174 Sized_relobj
<64, false>* object
,
175 unsigned int data_shndx
,
176 Output_section
* output_section
,
177 const elfcpp::Rela
<64, false>& reloc
, unsigned int r_type
,
182 unsupported_reloc_local(Sized_relobj
<64, false>*, unsigned int r_type
);
185 unsupported_reloc_global(Sized_relobj
<64, false>*, unsigned int r_type
,
189 check_non_pic(Relobj
*, unsigned int r_type
);
191 // Whether we have issued an error about a non-PIC compilation.
192 bool issued_non_pic_error_
;
195 // The class which implements relocation.
200 : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
205 if (this->skip_call_tls_get_addr_
)
207 // FIXME: This needs to specify the location somehow.
208 gold_error(_("missing expected TLS relocation"));
212 // Do a relocation. Return false if the caller should not issue
213 // any warnings about this relocation.
215 relocate(const Relocate_info
<64, false>*, Target_x86_64
*, size_t relnum
,
216 const elfcpp::Rela
<64, false>&,
217 unsigned int r_type
, const Sized_symbol
<64>*,
218 const Symbol_value
<64>*,
219 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
223 // Do a TLS relocation.
225 relocate_tls(const Relocate_info
<64, false>*, Target_x86_64
*,
226 size_t relnum
, const elfcpp::Rela
<64, false>&,
227 unsigned int r_type
, const Sized_symbol
<64>*,
228 const Symbol_value
<64>*,
229 unsigned char*, elfcpp::Elf_types
<64>::Elf_Addr
,
232 // Do a TLS General-Dynamic to Initial-Exec transition.
234 tls_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
235 Output_segment
* tls_segment
,
236 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
237 elfcpp::Elf_types
<64>::Elf_Addr value
,
239 elfcpp::Elf_types
<64>::Elf_Addr
,
240 section_size_type view_size
);
242 // Do a TLS General-Dynamic to Local-Exec transition.
244 tls_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
245 Output_segment
* tls_segment
,
246 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
247 elfcpp::Elf_types
<64>::Elf_Addr value
,
249 section_size_type view_size
);
251 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
253 tls_desc_gd_to_ie(const Relocate_info
<64, false>*, size_t relnum
,
254 Output_segment
* tls_segment
,
255 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
256 elfcpp::Elf_types
<64>::Elf_Addr value
,
258 elfcpp::Elf_types
<64>::Elf_Addr
,
259 section_size_type view_size
);
261 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
263 tls_desc_gd_to_le(const Relocate_info
<64, false>*, size_t relnum
,
264 Output_segment
* tls_segment
,
265 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
266 elfcpp::Elf_types
<64>::Elf_Addr value
,
268 section_size_type view_size
);
270 // Do a TLS Local-Dynamic to Local-Exec transition.
272 tls_ld_to_le(const Relocate_info
<64, false>*, size_t relnum
,
273 Output_segment
* tls_segment
,
274 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
275 elfcpp::Elf_types
<64>::Elf_Addr value
,
277 section_size_type view_size
);
279 // Do a TLS Initial-Exec to Local-Exec transition.
281 tls_ie_to_le(const Relocate_info
<64, false>*, size_t relnum
,
282 Output_segment
* tls_segment
,
283 const elfcpp::Rela
<64, false>&, unsigned int r_type
,
284 elfcpp::Elf_types
<64>::Elf_Addr value
,
286 section_size_type view_size
);
288 // This is set if we should skip the next reloc, which should be a
289 // PLT32 reloc against ___tls_get_addr.
290 bool skip_call_tls_get_addr_
;
292 // This is set if we see a relocation which could load the address
293 // of the TLS block. Whether we see such a relocation determines
294 // how we handle the R_X86_64_DTPOFF32 relocation, which is used
295 // in debugging sections.
296 bool saw_tls_block_reloc_
;
299 // A class which returns the size required for a relocation type,
300 // used while scanning relocs during a relocatable link.
301 class Relocatable_size_for_reloc
305 get_size_for_reloc(unsigned int, Relobj
*);
308 // Adjust TLS relocation type based on the options and whether this
309 // is a local symbol.
310 static tls::Tls_optimization
311 optimize_tls_reloc(bool is_final
, int r_type
);
313 // Get the GOT section, creating it if necessary.
314 Output_data_got
<64, false>*
315 got_section(Symbol_table
*, Layout
*);
317 // Get the GOT PLT section.
319 got_plt_section() const
321 gold_assert(this->got_plt_
!= NULL
);
322 return this->got_plt_
;
325 // Create the PLT section.
327 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
329 // Create a PLT entry for a global symbol.
331 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
333 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
335 define_tls_base_symbol(Symbol_table
*, Layout
*);
337 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
339 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
341 // Create a GOT entry for the TLS module index.
343 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
344 Sized_relobj
<64, false>* object
);
346 // Get the PLT section.
347 Output_data_plt_x86_64
*
350 gold_assert(this->plt_
!= NULL
);
354 // Get the dynamic reloc section, creating it if necessary.
356 rela_dyn_section(Layout
*);
358 // Return true if the symbol may need a COPY relocation.
359 // References from an executable object to non-function symbols
360 // defined in a dynamic object may need a COPY relocation.
362 may_need_copy_reloc(Symbol
* gsym
)
364 return (!parameters
->options().shared()
365 && gsym
->is_from_dynobj()
366 && gsym
->type() != elfcpp::STT_FUNC
);
369 // Add a potential copy relocation.
371 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
372 Sized_relobj
<64, false>* object
,
373 unsigned int shndx
, Output_section
* output_section
,
374 Symbol
* sym
, const elfcpp::Rela
<64, false>& reloc
)
376 this->copy_relocs_
.copy_reloc(symtab
, layout
,
377 symtab
->get_sized_symbol
<64>(sym
),
378 object
, shndx
, output_section
,
379 reloc
, this->rela_dyn_section(layout
));
382 // Information about this specific target which we pass to the
383 // general Target structure.
384 static const Target::Target_info x86_64_info
;
388 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
389 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
390 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
391 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
395 Output_data_got
<64, false>* got_
;
397 Output_data_plt_x86_64
* plt_
;
398 // The GOT PLT section.
399 Output_data_space
* got_plt_
;
400 // The dynamic reloc section.
401 Reloc_section
* rela_dyn_
;
402 // Relocs saved to avoid a COPY reloc.
403 Copy_relocs
<elfcpp::SHT_RELA
, 64, false> copy_relocs_
;
404 // Space for variables copied with a COPY reloc.
405 Output_data_space
* dynbss_
;
406 // Offset of the GOT entry for the TLS module index.
407 unsigned int got_mod_index_offset_
;
408 // True if the _TLS_MODULE_BASE_ symbol has been defined.
409 bool tls_base_symbol_defined_
;
412 const Target::Target_info
Target_x86_64::x86_64_info
=
415 false, // is_big_endian
416 elfcpp::EM_X86_64
, // machine_code
417 false, // has_make_symbol
418 false, // has_resolve
419 true, // has_code_fill
420 true, // is_default_stack_executable
422 "/lib/ld64.so.1", // program interpreter
423 0x400000, // default_text_segment_address
424 0x1000, // abi_pagesize (overridable by -z max-page-size)
425 0x1000 // common_pagesize (overridable by -z common-page-size)
428 // Get the GOT section, creating it if necessary.
430 Output_data_got
<64, false>*
431 Target_x86_64::got_section(Symbol_table
* symtab
, Layout
* layout
)
433 if (this->got_
== NULL
)
435 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
437 this->got_
= new Output_data_got
<64, false>();
440 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
442 | elfcpp::SHF_WRITE
),
446 // The old GNU linker creates a .got.plt section. We just
447 // create another set of data in the .got section. Note that we
448 // always create a PLT if we create a GOT, although the PLT
450 this->got_plt_
= new Output_data_space(8, "** GOT PLT");
451 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
453 | elfcpp::SHF_WRITE
),
457 // The first three entries are reserved.
458 this->got_plt_
->set_current_data_size(3 * 8);
460 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
461 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
463 0, 0, elfcpp::STT_OBJECT
,
465 elfcpp::STV_HIDDEN
, 0,
472 // Get the dynamic reloc section, creating it if necessary.
474 Target_x86_64::Reloc_section
*
475 Target_x86_64::rela_dyn_section(Layout
* layout
)
477 if (this->rela_dyn_
== NULL
)
479 gold_assert(layout
!= NULL
);
480 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
481 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
482 elfcpp::SHF_ALLOC
, this->rela_dyn_
);
484 return this->rela_dyn_
;
487 // A class to handle the PLT data.
489 class Output_data_plt_x86_64
: public Output_section_data
492 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, 64, false> Reloc_section
;
494 Output_data_plt_x86_64(Layout
*, Output_data_got
<64, false>*,
497 // Add an entry to the PLT.
499 add_entry(Symbol
* gsym
);
501 // Add the reserved TLSDESC_PLT entry to the PLT.
503 reserve_tlsdesc_entry(unsigned int got_offset
)
504 { this->tlsdesc_got_offset_
= got_offset
; }
506 // Return true if a TLSDESC_PLT entry has been reserved.
508 has_tlsdesc_entry() const
509 { return this->tlsdesc_got_offset_
!= -1U; }
511 // Return the GOT offset for the reserved TLSDESC_PLT entry.
513 get_tlsdesc_got_offset() const
514 { return this->tlsdesc_got_offset_
; }
516 // Return the offset of the reserved TLSDESC_PLT entry.
518 get_tlsdesc_plt_offset() const
519 { return (this->count_
+ 1) * plt_entry_size
; }
521 // Return the .rel.plt section data.
524 { return this->rel_
; }
528 do_adjust_output_section(Output_section
* os
);
530 // Write to a map file.
532 do_print_to_mapfile(Mapfile
* mapfile
) const
533 { mapfile
->print_output_data(this, _("** PLT")); }
536 // The size of an entry in the PLT.
537 static const int plt_entry_size
= 16;
539 // The first entry in the PLT.
540 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
541 // procedure linkage table for both programs and shared objects."
542 static unsigned char first_plt_entry
[plt_entry_size
];
544 // Other entries in the PLT for an executable.
545 static unsigned char plt_entry
[plt_entry_size
];
547 // The reserved TLSDESC entry in the PLT for an executable.
548 static unsigned char tlsdesc_plt_entry
[plt_entry_size
];
550 // Set the final size.
552 set_final_data_size();
554 // Write out the PLT data.
556 do_write(Output_file
*);
558 // The reloc section.
561 Output_data_got
<64, false>* got_
;
562 // The .got.plt section.
563 Output_data_space
* got_plt_
;
564 // The number of PLT entries.
566 // Offset of the reserved TLSDESC_GOT entry when needed.
567 unsigned int tlsdesc_got_offset_
;
570 // Create the PLT section. The ordinary .got section is an argument,
571 // since we need to refer to the start. We also create our own .got
572 // section just for PLT entries.
574 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout
* layout
,
575 Output_data_got
<64, false>* got
,
576 Output_data_space
* got_plt
)
577 : Output_section_data(8), got_(got
), got_plt_(got_plt
), count_(0),
578 tlsdesc_got_offset_(-1U)
580 this->rel_
= new Reloc_section(false);
581 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
582 elfcpp::SHF_ALLOC
, this->rel_
);
586 Output_data_plt_x86_64::do_adjust_output_section(Output_section
* os
)
588 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
589 // linker, and so do we.
593 // Add an entry to the PLT.
596 Output_data_plt_x86_64::add_entry(Symbol
* gsym
)
598 gold_assert(!gsym
->has_plt_offset());
600 // Note that when setting the PLT offset we skip the initial
601 // reserved PLT entry.
602 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
606 section_offset_type got_offset
= this->got_plt_
->current_data_size();
608 // Every PLT entry needs a GOT entry which points back to the PLT
609 // entry (this will be changed by the dynamic linker, normally
610 // lazily when the function is called).
611 this->got_plt_
->set_current_data_size(got_offset
+ 8);
613 // Every PLT entry needs a reloc.
614 gsym
->set_needs_dynsym_entry();
615 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
618 // Note that we don't need to save the symbol. The contents of the
619 // PLT are independent of which symbols are used. The symbols only
620 // appear in the relocations.
623 // Set the final size.
625 Output_data_plt_x86_64::set_final_data_size()
627 unsigned int count
= this->count_
;
628 if (this->has_tlsdesc_entry())
630 this->set_data_size((count
+ 1) * plt_entry_size
);
633 // The first entry in the PLT for an executable.
635 unsigned char Output_data_plt_x86_64::first_plt_entry
[plt_entry_size
] =
637 // From AMD64 ABI Draft 0.98, page 76
638 0xff, 0x35, // pushq contents of memory address
639 0, 0, 0, 0, // replaced with address of .got + 8
640 0xff, 0x25, // jmp indirect
641 0, 0, 0, 0, // replaced with address of .got + 16
642 0x90, 0x90, 0x90, 0x90 // noop (x4)
645 // Subsequent entries in the PLT for an executable.
647 unsigned char Output_data_plt_x86_64::plt_entry
[plt_entry_size
] =
649 // From AMD64 ABI Draft 0.98, page 76
650 0xff, 0x25, // jmpq indirect
651 0, 0, 0, 0, // replaced with address of symbol in .got
652 0x68, // pushq immediate
653 0, 0, 0, 0, // replaced with offset into relocation table
654 0xe9, // jmpq relative
655 0, 0, 0, 0 // replaced with offset to start of .plt
658 // The reserved TLSDESC entry in the PLT for an executable.
660 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry
[plt_entry_size
] =
662 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
663 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
664 0xff, 0x35, // pushq x(%rip)
665 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
666 0xff, 0x25, // jmpq *y(%rip)
667 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
672 // Write out the PLT. This uses the hand-coded instructions above,
673 // and adjusts them as needed. This is specified by the AMD64 ABI.
676 Output_data_plt_x86_64::do_write(Output_file
* of
)
678 const off_t offset
= this->offset();
679 const section_size_type oview_size
=
680 convert_to_section_size_type(this->data_size());
681 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
683 const off_t got_file_offset
= this->got_plt_
->offset();
684 const section_size_type got_size
=
685 convert_to_section_size_type(this->got_plt_
->data_size());
686 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
689 unsigned char* pov
= oview
;
691 // The base address of the .plt section.
692 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
693 // The base address of the .got section.
694 elfcpp::Elf_types
<64>::Elf_Addr got_base
= this->got_
->address();
695 // The base address of the PLT portion of the .got section,
696 // which is where the GOT pointer will point, and where the
697 // three reserved GOT entries are located.
698 elfcpp::Elf_types
<64>::Elf_Addr got_address
= this->got_plt_
->address();
700 memcpy(pov
, first_plt_entry
, plt_entry_size
);
701 // We do a jmp relative to the PC at the end of this instruction.
702 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
704 - (plt_address
+ 6)));
705 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
707 - (plt_address
+ 12)));
708 pov
+= plt_entry_size
;
710 unsigned char* got_pov
= got_view
;
712 memset(got_pov
, 0, 24);
715 unsigned int plt_offset
= plt_entry_size
;
716 unsigned int got_offset
= 24;
717 const unsigned int count
= this->count_
;
718 for (unsigned int plt_index
= 0;
721 pov
+= plt_entry_size
,
723 plt_offset
+= plt_entry_size
,
726 // Set and adjust the PLT entry itself.
727 memcpy(pov
, plt_entry
, plt_entry_size
);
728 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
729 (got_address
+ got_offset
730 - (plt_address
+ plt_offset
733 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
734 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
735 - (plt_offset
+ plt_entry_size
));
737 // Set the entry in the GOT.
738 elfcpp::Swap
<64, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
741 if (this->has_tlsdesc_entry())
743 // Set and adjust the reserved TLSDESC PLT entry.
744 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
745 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
746 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
748 - (plt_address
+ plt_offset
750 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
753 - (plt_address
+ plt_offset
755 pov
+= plt_entry_size
;
758 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
759 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
761 of
->write_output_view(offset
, oview_size
, oview
);
762 of
->write_output_view(got_file_offset
, got_size
, got_view
);
765 // Create the PLT section.
768 Target_x86_64::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
770 if (this->plt_
== NULL
)
772 // Create the GOT sections first.
773 this->got_section(symtab
, layout
);
775 this->plt_
= new Output_data_plt_x86_64(layout
, this->got_
,
777 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
779 | elfcpp::SHF_EXECINSTR
),
784 // Create a PLT entry for a global symbol.
787 Target_x86_64::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
790 if (gsym
->has_plt_offset())
793 if (this->plt_
== NULL
)
794 this->make_plt_section(symtab
, layout
);
796 this->plt_
->add_entry(gsym
);
799 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
802 Target_x86_64::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
804 if (this->tls_base_symbol_defined_
)
807 Output_segment
* tls_segment
= layout
->tls_segment();
808 if (tls_segment
!= NULL
)
810 bool is_exec
= parameters
->options().output_is_executable();
811 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
815 elfcpp::STV_HIDDEN
, 0,
817 ? Symbol::SEGMENT_END
818 : Symbol::SEGMENT_START
),
821 this->tls_base_symbol_defined_
= true;
824 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
827 Target_x86_64::reserve_tlsdesc_entries(Symbol_table
* symtab
,
830 if (this->plt_
== NULL
)
831 this->make_plt_section(symtab
, layout
);
833 if (!this->plt_
->has_tlsdesc_entry())
835 // Allocate the TLSDESC_GOT entry.
836 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
837 unsigned int got_offset
= got
->add_constant(0);
839 // Allocate the TLSDESC_PLT entry.
840 this->plt_
->reserve_tlsdesc_entry(got_offset
);
844 // Create a GOT entry for the TLS module index.
847 Target_x86_64::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
848 Sized_relobj
<64, false>* object
)
850 if (this->got_mod_index_offset_
== -1U)
852 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
853 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
854 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
855 unsigned int got_offset
= got
->add_constant(0);
856 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
858 got
->add_constant(0);
859 this->got_mod_index_offset_
= got_offset
;
861 return this->got_mod_index_offset_
;
864 // Optimize the TLS relocation type based on what we know about the
865 // symbol. IS_FINAL is true if the final address of this symbol is
866 // known at link time.
868 tls::Tls_optimization
869 Target_x86_64::optimize_tls_reloc(bool is_final
, int r_type
)
871 // If we are generating a shared library, then we can't do anything
873 if (parameters
->options().shared())
874 return tls::TLSOPT_NONE
;
878 case elfcpp::R_X86_64_TLSGD
:
879 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
880 case elfcpp::R_X86_64_TLSDESC_CALL
:
881 // These are General-Dynamic which permits fully general TLS
882 // access. Since we know that we are generating an executable,
883 // we can convert this to Initial-Exec. If we also know that
884 // this is a local symbol, we can further switch to Local-Exec.
886 return tls::TLSOPT_TO_LE
;
887 return tls::TLSOPT_TO_IE
;
889 case elfcpp::R_X86_64_TLSLD
:
890 // This is Local-Dynamic, which refers to a local symbol in the
891 // dynamic TLS block. Since we know that we generating an
892 // executable, we can switch to Local-Exec.
893 return tls::TLSOPT_TO_LE
;
895 case elfcpp::R_X86_64_DTPOFF32
:
896 case elfcpp::R_X86_64_DTPOFF64
:
897 // Another Local-Dynamic reloc.
898 return tls::TLSOPT_TO_LE
;
900 case elfcpp::R_X86_64_GOTTPOFF
:
901 // These are Initial-Exec relocs which get the thread offset
902 // from the GOT. If we know that we are linking against the
903 // local symbol, we can switch to Local-Exec, which links the
904 // thread offset into the instruction.
906 return tls::TLSOPT_TO_LE
;
907 return tls::TLSOPT_NONE
;
909 case elfcpp::R_X86_64_TPOFF32
:
910 // When we already have Local-Exec, there is nothing further we
912 return tls::TLSOPT_NONE
;
919 // Report an unsupported relocation against a local symbol.
922 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj
<64, false>* object
,
925 gold_error(_("%s: unsupported reloc %u against local symbol"),
926 object
->name().c_str(), r_type
);
929 // We are about to emit a dynamic relocation of type R_TYPE. If the
930 // dynamic linker does not support it, issue an error. The GNU linker
931 // only issues a non-PIC error for an allocated read-only section.
932 // Here we know the section is allocated, but we don't know that it is
933 // read-only. But we check for all the relocation types which the
934 // glibc dynamic linker supports, so it seems appropriate to issue an
935 // error even if the section is not read-only.
938 Target_x86_64::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
)
942 // These are the relocation types supported by glibc for x86_64.
943 case elfcpp::R_X86_64_RELATIVE
:
944 case elfcpp::R_X86_64_GLOB_DAT
:
945 case elfcpp::R_X86_64_JUMP_SLOT
:
946 case elfcpp::R_X86_64_DTPMOD64
:
947 case elfcpp::R_X86_64_DTPOFF64
:
948 case elfcpp::R_X86_64_TPOFF64
:
949 case elfcpp::R_X86_64_64
:
950 case elfcpp::R_X86_64_32
:
951 case elfcpp::R_X86_64_PC32
:
952 case elfcpp::R_X86_64_COPY
:
956 // This prevents us from issuing more than one error per reloc
957 // section. But we can still wind up issuing more than one
958 // error per object file.
959 if (this->issued_non_pic_error_
)
961 object
->error(_("requires unsupported dynamic reloc; "
962 "recompile with -fPIC"));
963 this->issued_non_pic_error_
= true;
966 case elfcpp::R_X86_64_NONE
:
971 // Scan a relocation for a local symbol.
974 Target_x86_64::Scan::local(const General_options
&,
975 Symbol_table
* symtab
,
977 Target_x86_64
* target
,
978 Sized_relobj
<64, false>* object
,
979 unsigned int data_shndx
,
980 Output_section
* output_section
,
981 const elfcpp::Rela
<64, false>& reloc
,
983 const elfcpp::Sym
<64, false>& lsym
)
987 case elfcpp::R_X86_64_NONE
:
988 case elfcpp::R_386_GNU_VTINHERIT
:
989 case elfcpp::R_386_GNU_VTENTRY
:
992 case elfcpp::R_X86_64_64
:
993 // If building a shared library (or a position-independent
994 // executable), we need to create a dynamic relocation for this
995 // location. The relocation applied at link time will apply the
996 // link-time value, so we flag the location with an
997 // R_X86_64_RELATIVE relocation so the dynamic loader can
998 // relocate it easily.
999 if (parameters
->options().output_is_position_independent())
1001 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1002 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1003 rela_dyn
->add_local_relative(object
, r_sym
,
1004 elfcpp::R_X86_64_RELATIVE
,
1005 output_section
, data_shndx
,
1006 reloc
.get_r_offset(),
1007 reloc
.get_r_addend());
1011 case elfcpp::R_X86_64_32
:
1012 case elfcpp::R_X86_64_32S
:
1013 case elfcpp::R_X86_64_16
:
1014 case elfcpp::R_X86_64_8
:
1015 // If building a shared library (or a position-independent
1016 // executable), we need to create a dynamic relocation for this
1017 // location. We can't use an R_X86_64_RELATIVE relocation
1018 // because that is always a 64-bit relocation.
1019 if (parameters
->options().output_is_position_independent())
1021 this->check_non_pic(object
, r_type
);
1023 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1024 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1025 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1026 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1027 data_shndx
, reloc
.get_r_offset(),
1028 reloc
.get_r_addend());
1031 gold_assert(lsym
.get_st_value() == 0);
1032 unsigned int shndx
= lsym
.get_st_shndx();
1034 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1037 object
->error(_("section symbol %u has bad shndx %u"),
1040 rela_dyn
->add_local_section(object
, shndx
,
1041 r_type
, output_section
,
1042 data_shndx
, reloc
.get_r_offset(),
1043 reloc
.get_r_addend());
1048 case elfcpp::R_X86_64_PC64
:
1049 case elfcpp::R_X86_64_PC32
:
1050 case elfcpp::R_X86_64_PC16
:
1051 case elfcpp::R_X86_64_PC8
:
1054 case elfcpp::R_X86_64_PLT32
:
1055 // Since we know this is a local symbol, we can handle this as a
1059 case elfcpp::R_X86_64_GOTPC32
:
1060 case elfcpp::R_X86_64_GOTOFF64
:
1061 case elfcpp::R_X86_64_GOTPC64
:
1062 case elfcpp::R_X86_64_PLTOFF64
:
1063 // We need a GOT section.
1064 target
->got_section(symtab
, layout
);
1065 // For PLTOFF64, we'd normally want a PLT section, but since we
1066 // know this is a local symbol, no PLT is needed.
1069 case elfcpp::R_X86_64_GOT64
:
1070 case elfcpp::R_X86_64_GOT32
:
1071 case elfcpp::R_X86_64_GOTPCREL64
:
1072 case elfcpp::R_X86_64_GOTPCREL
:
1073 case elfcpp::R_X86_64_GOTPLT64
:
1075 // The symbol requires a GOT entry.
1076 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1077 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1078 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
1080 // If we are generating a shared object, we need to add a
1081 // dynamic relocation for this symbol's GOT entry.
1082 if (parameters
->options().output_is_position_independent())
1084 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1085 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1086 if (r_type
!= elfcpp::R_X86_64_GOT32
)
1087 rela_dyn
->add_local_relative(
1088 object
, r_sym
, elfcpp::R_X86_64_RELATIVE
, got
,
1089 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1092 this->check_non_pic(object
, r_type
);
1094 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1095 rela_dyn
->add_local(
1096 object
, r_sym
, r_type
, got
,
1097 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
1101 // For GOTPLT64, we'd normally want a PLT section, but since
1102 // we know this is a local symbol, no PLT is needed.
1106 case elfcpp::R_X86_64_COPY
:
1107 case elfcpp::R_X86_64_GLOB_DAT
:
1108 case elfcpp::R_X86_64_JUMP_SLOT
:
1109 case elfcpp::R_X86_64_RELATIVE
:
1110 // These are outstanding tls relocs, which are unexpected when linking
1111 case elfcpp::R_X86_64_TPOFF64
:
1112 case elfcpp::R_X86_64_DTPMOD64
:
1113 case elfcpp::R_X86_64_TLSDESC
:
1114 gold_error(_("%s: unexpected reloc %u in object file"),
1115 object
->name().c_str(), r_type
);
1118 // These are initial tls relocs, which are expected when linking
1119 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1120 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1121 case elfcpp::R_X86_64_TLSDESC_CALL
:
1122 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1123 case elfcpp::R_X86_64_DTPOFF32
:
1124 case elfcpp::R_X86_64_DTPOFF64
:
1125 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1126 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1128 bool output_is_shared
= parameters
->options().shared();
1129 const tls::Tls_optimization optimized_type
1130 = Target_x86_64::optimize_tls_reloc(!output_is_shared
, r_type
);
1133 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1134 if (optimized_type
== tls::TLSOPT_NONE
)
1136 // Create a pair of GOT entries for the module index and
1137 // dtv-relative offset.
1138 Output_data_got
<64, false>* got
1139 = target
->got_section(symtab
, layout
);
1140 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1141 unsigned int shndx
= lsym
.get_st_shndx();
1143 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1145 object
->error(_("local symbol %u has bad shndx %u"),
1148 got
->add_local_pair_with_rela(object
, r_sym
,
1151 target
->rela_dyn_section(layout
),
1152 elfcpp::R_X86_64_DTPMOD64
, 0);
1154 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1155 unsupported_reloc_local(object
, r_type
);
1158 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1159 target
->define_tls_base_symbol(symtab
, layout
);
1160 if (optimized_type
== tls::TLSOPT_NONE
)
1162 // Create reserved PLT and GOT entries for the resolver.
1163 target
->reserve_tlsdesc_entries(symtab
, layout
);
1165 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1166 Output_data_got
<64, false>* got
1167 = target
->got_section(symtab
, layout
);
1168 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1169 unsigned int shndx
= lsym
.get_st_shndx();
1171 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1173 object
->error(_("local symbol %u has bad shndx %u"),
1176 got
->add_local_pair_with_rela(object
, r_sym
,
1179 target
->rela_dyn_section(layout
),
1180 elfcpp::R_X86_64_TLSDESC
, 0);
1182 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1183 unsupported_reloc_local(object
, r_type
);
1186 case elfcpp::R_X86_64_TLSDESC_CALL
:
1189 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1190 if (optimized_type
== tls::TLSOPT_NONE
)
1192 // Create a GOT entry for the module index.
1193 target
->got_mod_index_entry(symtab
, layout
, object
);
1195 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1196 unsupported_reloc_local(object
, r_type
);
1199 case elfcpp::R_X86_64_DTPOFF32
:
1200 case elfcpp::R_X86_64_DTPOFF64
:
1203 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1204 layout
->set_has_static_tls();
1205 if (optimized_type
== tls::TLSOPT_NONE
)
1207 // Create a GOT entry for the tp-relative offset.
1208 Output_data_got
<64, false>* got
1209 = target
->got_section(symtab
, layout
);
1210 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(reloc
.get_r_info());
1211 got
->add_local_with_rela(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1212 target
->rela_dyn_section(layout
),
1213 elfcpp::R_X86_64_TPOFF64
);
1215 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1216 unsupported_reloc_local(object
, r_type
);
1219 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1220 layout
->set_has_static_tls();
1221 if (output_is_shared
)
1222 unsupported_reloc_local(object
, r_type
);
1231 case elfcpp::R_X86_64_SIZE32
:
1232 case elfcpp::R_X86_64_SIZE64
:
1234 gold_error(_("%s: unsupported reloc %u against local symbol"),
1235 object
->name().c_str(), r_type
);
1241 // Report an unsupported relocation against a global symbol.
1244 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj
<64, false>* object
,
1245 unsigned int r_type
,
1248 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1249 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1252 // Scan a relocation for a global symbol.
1255 Target_x86_64::Scan::global(const General_options
&,
1256 Symbol_table
* symtab
,
1258 Target_x86_64
* target
,
1259 Sized_relobj
<64, false>* object
,
1260 unsigned int data_shndx
,
1261 Output_section
* output_section
,
1262 const elfcpp::Rela
<64, false>& reloc
,
1263 unsigned int r_type
,
1268 case elfcpp::R_X86_64_NONE
:
1269 case elfcpp::R_386_GNU_VTINHERIT
:
1270 case elfcpp::R_386_GNU_VTENTRY
:
1273 case elfcpp::R_X86_64_64
:
1274 case elfcpp::R_X86_64_32
:
1275 case elfcpp::R_X86_64_32S
:
1276 case elfcpp::R_X86_64_16
:
1277 case elfcpp::R_X86_64_8
:
1279 // Make a PLT entry if necessary.
1280 if (gsym
->needs_plt_entry())
1282 target
->make_plt_entry(symtab
, layout
, gsym
);
1283 // Since this is not a PC-relative relocation, we may be
1284 // taking the address of a function. In that case we need to
1285 // set the entry in the dynamic symbol table to the address of
1287 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1288 gsym
->set_needs_dynsym_value();
1290 // Make a dynamic relocation if necessary.
1291 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1293 if (target
->may_need_copy_reloc(gsym
))
1295 target
->copy_reloc(symtab
, layout
, object
,
1296 data_shndx
, output_section
, gsym
, reloc
);
1298 else if (r_type
== elfcpp::R_X86_64_64
1299 && gsym
->can_use_relative_reloc(false))
1301 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1302 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1303 output_section
, object
,
1304 data_shndx
, reloc
.get_r_offset(),
1305 reloc
.get_r_addend());
1309 this->check_non_pic(object
, r_type
);
1310 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1311 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1312 data_shndx
, reloc
.get_r_offset(),
1313 reloc
.get_r_addend());
1319 case elfcpp::R_X86_64_PC64
:
1320 case elfcpp::R_X86_64_PC32
:
1321 case elfcpp::R_X86_64_PC16
:
1322 case elfcpp::R_X86_64_PC8
:
1324 // Make a PLT entry if necessary.
1325 if (gsym
->needs_plt_entry())
1326 target
->make_plt_entry(symtab
, layout
, gsym
);
1327 // Make a dynamic relocation if necessary.
1328 int flags
= Symbol::NON_PIC_REF
;
1329 if (gsym
->type() == elfcpp::STT_FUNC
)
1330 flags
|= Symbol::FUNCTION_CALL
;
1331 if (gsym
->needs_dynamic_reloc(flags
))
1333 if (target
->may_need_copy_reloc(gsym
))
1335 target
->copy_reloc(symtab
, layout
, object
,
1336 data_shndx
, output_section
, gsym
, reloc
);
1340 this->check_non_pic(object
, r_type
);
1341 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1342 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1343 data_shndx
, reloc
.get_r_offset(),
1344 reloc
.get_r_addend());
1350 case elfcpp::R_X86_64_GOT64
:
1351 case elfcpp::R_X86_64_GOT32
:
1352 case elfcpp::R_X86_64_GOTPCREL64
:
1353 case elfcpp::R_X86_64_GOTPCREL
:
1354 case elfcpp::R_X86_64_GOTPLT64
:
1356 // The symbol requires a GOT entry.
1357 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
1358 if (gsym
->final_value_is_known())
1359 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1362 // If this symbol is not fully resolved, we need to add a
1363 // dynamic relocation for it.
1364 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
1365 if (gsym
->is_from_dynobj()
1366 || gsym
->is_undefined()
1367 || gsym
->is_preemptible())
1368 got
->add_global_with_rela(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
1369 elfcpp::R_X86_64_GLOB_DAT
);
1372 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1373 rela_dyn
->add_global_relative(
1374 gsym
, elfcpp::R_X86_64_RELATIVE
, got
,
1375 gsym
->got_offset(GOT_TYPE_STANDARD
), 0);
1378 // For GOTPLT64, we also need a PLT entry (but only if the
1379 // symbol is not fully resolved).
1380 if (r_type
== elfcpp::R_X86_64_GOTPLT64
1381 && !gsym
->final_value_is_known())
1382 target
->make_plt_entry(symtab
, layout
, gsym
);
1386 case elfcpp::R_X86_64_PLT32
:
1387 // If the symbol is fully resolved, this is just a PC32 reloc.
1388 // Otherwise we need a PLT entry.
1389 if (gsym
->final_value_is_known())
1391 // If building a shared library, we can also skip the PLT entry
1392 // if the symbol is defined in the output file and is protected
1394 if (gsym
->is_defined()
1395 && !gsym
->is_from_dynobj()
1396 && !gsym
->is_preemptible())
1398 target
->make_plt_entry(symtab
, layout
, gsym
);
1401 case elfcpp::R_X86_64_GOTPC32
:
1402 case elfcpp::R_X86_64_GOTOFF64
:
1403 case elfcpp::R_X86_64_GOTPC64
:
1404 case elfcpp::R_X86_64_PLTOFF64
:
1405 // We need a GOT section.
1406 target
->got_section(symtab
, layout
);
1407 // For PLTOFF64, we also need a PLT entry (but only if the
1408 // symbol is not fully resolved).
1409 if (r_type
== elfcpp::R_X86_64_PLTOFF64
1410 && !gsym
->final_value_is_known())
1411 target
->make_plt_entry(symtab
, layout
, gsym
);
1414 case elfcpp::R_X86_64_COPY
:
1415 case elfcpp::R_X86_64_GLOB_DAT
:
1416 case elfcpp::R_X86_64_JUMP_SLOT
:
1417 case elfcpp::R_X86_64_RELATIVE
:
1418 // These are outstanding tls relocs, which are unexpected when linking
1419 case elfcpp::R_X86_64_TPOFF64
:
1420 case elfcpp::R_X86_64_DTPMOD64
:
1421 case elfcpp::R_X86_64_TLSDESC
:
1422 gold_error(_("%s: unexpected reloc %u in object file"),
1423 object
->name().c_str(), r_type
);
1426 // These are initial tls relocs, which are expected for global()
1427 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1428 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1429 case elfcpp::R_X86_64_TLSDESC_CALL
:
1430 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1431 case elfcpp::R_X86_64_DTPOFF32
:
1432 case elfcpp::R_X86_64_DTPOFF64
:
1433 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1434 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1436 const bool is_final
= gsym
->final_value_is_known();
1437 const tls::Tls_optimization optimized_type
1438 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1441 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
1442 if (optimized_type
== tls::TLSOPT_NONE
)
1444 // Create a pair of GOT entries for the module index and
1445 // dtv-relative offset.
1446 Output_data_got
<64, false>* got
1447 = target
->got_section(symtab
, layout
);
1448 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_PAIR
,
1449 target
->rela_dyn_section(layout
),
1450 elfcpp::R_X86_64_DTPMOD64
,
1451 elfcpp::R_X86_64_DTPOFF64
);
1453 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1455 // Create a GOT entry for the tp-relative offset.
1456 Output_data_got
<64, false>* got
1457 = target
->got_section(symtab
, layout
);
1458 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1459 target
->rela_dyn_section(layout
),
1460 elfcpp::R_X86_64_TPOFF64
);
1462 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1463 unsupported_reloc_global(object
, r_type
, gsym
);
1466 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
1467 target
->define_tls_base_symbol(symtab
, layout
);
1468 if (optimized_type
== tls::TLSOPT_NONE
)
1470 // Create reserved PLT and GOT entries for the resolver.
1471 target
->reserve_tlsdesc_entries(symtab
, layout
);
1473 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1474 Output_data_got
<64, false>* got
1475 = target
->got_section(symtab
, layout
);
1476 got
->add_global_pair_with_rela(gsym
, GOT_TYPE_TLS_DESC
,
1477 target
->rela_dyn_section(layout
),
1478 elfcpp::R_X86_64_TLSDESC
, 0);
1480 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1482 // Create a GOT entry for the tp-relative offset.
1483 Output_data_got
<64, false>* got
1484 = target
->got_section(symtab
, layout
);
1485 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1486 target
->rela_dyn_section(layout
),
1487 elfcpp::R_X86_64_TPOFF64
);
1489 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1490 unsupported_reloc_global(object
, r_type
, gsym
);
1493 case elfcpp::R_X86_64_TLSDESC_CALL
:
1496 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1497 if (optimized_type
== tls::TLSOPT_NONE
)
1499 // Create a GOT entry for the module index.
1500 target
->got_mod_index_entry(symtab
, layout
, object
);
1502 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1503 unsupported_reloc_global(object
, r_type
, gsym
);
1506 case elfcpp::R_X86_64_DTPOFF32
:
1507 case elfcpp::R_X86_64_DTPOFF64
:
1510 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1511 layout
->set_has_static_tls();
1512 if (optimized_type
== tls::TLSOPT_NONE
)
1514 // Create a GOT entry for the tp-relative offset.
1515 Output_data_got
<64, false>* got
1516 = target
->got_section(symtab
, layout
);
1517 got
->add_global_with_rela(gsym
, GOT_TYPE_TLS_OFFSET
,
1518 target
->rela_dyn_section(layout
),
1519 elfcpp::R_X86_64_TPOFF64
);
1521 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1522 unsupported_reloc_global(object
, r_type
, gsym
);
1525 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1526 layout
->set_has_static_tls();
1527 if (parameters
->options().shared())
1528 unsupported_reloc_local(object
, r_type
);
1537 case elfcpp::R_X86_64_SIZE32
:
1538 case elfcpp::R_X86_64_SIZE64
:
1540 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1541 object
->name().c_str(), r_type
,
1542 gsym
->demangled_name().c_str());
1547 // Scan relocations for a section.
1550 Target_x86_64::scan_relocs(const General_options
& options
,
1551 Symbol_table
* symtab
,
1553 Sized_relobj
<64, false>* object
,
1554 unsigned int data_shndx
,
1555 unsigned int sh_type
,
1556 const unsigned char* prelocs
,
1558 Output_section
* output_section
,
1559 bool needs_special_offset_handling
,
1560 size_t local_symbol_count
,
1561 const unsigned char* plocal_symbols
)
1563 if (sh_type
== elfcpp::SHT_REL
)
1565 gold_error(_("%s: unsupported REL reloc section"),
1566 object
->name().c_str());
1570 gold::scan_relocs
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
1571 Target_x86_64::Scan
>(
1581 needs_special_offset_handling
,
1586 // Finalize the sections.
1589 Target_x86_64::do_finalize_sections(Layout
* layout
)
1591 // Fill in some more dynamic tags.
1592 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1595 if (this->got_plt_
!= NULL
)
1596 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1598 if (this->plt_
!= NULL
)
1600 const Output_data
* od
= this->plt_
->rel_plt();
1601 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1602 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1603 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_RELA
);
1604 if (this->plt_
->has_tlsdesc_entry())
1606 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
1607 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
1608 this->got_
->finalize_data_size();
1609 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
1610 this->plt_
, plt_offset
);
1611 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
1612 this->got_
, got_offset
);
1616 if (this->rela_dyn_
!= NULL
)
1618 const Output_data
* od
= this->rela_dyn_
;
1619 odyn
->add_section_address(elfcpp::DT_RELA
, od
);
1620 odyn
->add_section_size(elfcpp::DT_RELASZ
, od
);
1621 odyn
->add_constant(elfcpp::DT_RELAENT
,
1622 elfcpp::Elf_sizes
<64>::rela_size
);
1625 if (!parameters
->options().shared())
1627 // The value of the DT_DEBUG tag is filled in by the dynamic
1628 // linker at run time, and used by the debugger.
1629 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1633 // Emit any relocs we saved in an attempt to avoid generating COPY
1635 if (this->copy_relocs_
.any_saved_relocs())
1636 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
1639 // Perform a relocation.
1642 Target_x86_64::Relocate::relocate(const Relocate_info
<64, false>* relinfo
,
1643 Target_x86_64
* target
,
1645 const elfcpp::Rela
<64, false>& rela
,
1646 unsigned int r_type
,
1647 const Sized_symbol
<64>* gsym
,
1648 const Symbol_value
<64>* psymval
,
1649 unsigned char* view
,
1650 elfcpp::Elf_types
<64>::Elf_Addr address
,
1651 section_size_type view_size
)
1653 if (this->skip_call_tls_get_addr_
)
1655 if (r_type
!= elfcpp::R_X86_64_PLT32
1657 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
1659 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1660 _("missing expected TLS relocation"));
1664 this->skip_call_tls_get_addr_
= false;
1669 // Pick the value to use for symbols defined in shared objects.
1670 Symbol_value
<64> symval
;
1672 && gsym
->use_plt_offset(r_type
== elfcpp::R_X86_64_PC64
1673 || r_type
== elfcpp::R_X86_64_PC32
1674 || r_type
== elfcpp::R_X86_64_PC16
1675 || r_type
== elfcpp::R_X86_64_PC8
))
1677 symval
.set_output_value(target
->plt_section()->address()
1678 + gsym
->plt_offset());
1682 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1683 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1685 // Get the GOT offset if needed.
1686 // The GOT pointer points to the end of the GOT section.
1687 // We need to subtract the size of the GOT section to get
1688 // the actual offset to use in the relocation.
1689 bool have_got_offset
= false;
1690 unsigned int got_offset
= 0;
1693 case elfcpp::R_X86_64_GOT32
:
1694 case elfcpp::R_X86_64_GOT64
:
1695 case elfcpp::R_X86_64_GOTPLT64
:
1696 case elfcpp::R_X86_64_GOTPCREL
:
1697 case elfcpp::R_X86_64_GOTPCREL64
:
1700 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1701 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
1705 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1706 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1707 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1708 - target
->got_size());
1710 have_got_offset
= true;
1719 case elfcpp::R_X86_64_NONE
:
1720 case elfcpp::R_386_GNU_VTINHERIT
:
1721 case elfcpp::R_386_GNU_VTENTRY
:
1724 case elfcpp::R_X86_64_64
:
1725 Relocate_functions
<64, false>::rela64(view
, object
, psymval
, addend
);
1728 case elfcpp::R_X86_64_PC64
:
1729 Relocate_functions
<64, false>::pcrela64(view
, object
, psymval
, addend
,
1733 case elfcpp::R_X86_64_32
:
1734 // FIXME: we need to verify that value + addend fits into 32 bits:
1735 // uint64_t x = value + addend;
1736 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1737 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1738 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1741 case elfcpp::R_X86_64_32S
:
1742 // FIXME: we need to verify that value + addend fits into 32 bits:
1743 // int64_t x = value + addend; // note this quantity is signed!
1744 // x == static_cast<int64_t>(static_cast<int32_t>(x))
1745 Relocate_functions
<64, false>::rela32(view
, object
, psymval
, addend
);
1748 case elfcpp::R_X86_64_PC32
:
1749 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1753 case elfcpp::R_X86_64_16
:
1754 Relocate_functions
<64, false>::rela16(view
, object
, psymval
, addend
);
1757 case elfcpp::R_X86_64_PC16
:
1758 Relocate_functions
<64, false>::pcrela16(view
, object
, psymval
, addend
,
1762 case elfcpp::R_X86_64_8
:
1763 Relocate_functions
<64, false>::rela8(view
, object
, psymval
, addend
);
1766 case elfcpp::R_X86_64_PC8
:
1767 Relocate_functions
<64, false>::pcrela8(view
, object
, psymval
, addend
,
1771 case elfcpp::R_X86_64_PLT32
:
1772 gold_assert(gsym
== NULL
1773 || gsym
->has_plt_offset()
1774 || gsym
->final_value_is_known()
1775 || (gsym
->is_defined()
1776 && !gsym
->is_from_dynobj()
1777 && !gsym
->is_preemptible()));
1778 // Note: while this code looks the same as for R_X86_64_PC32, it
1779 // behaves differently because psymval was set to point to
1780 // the PLT entry, rather than the symbol, in Scan::global().
1781 Relocate_functions
<64, false>::pcrela32(view
, object
, psymval
, addend
,
1785 case elfcpp::R_X86_64_PLTOFF64
:
1788 gold_assert(gsym
->has_plt_offset()
1789 || gsym
->final_value_is_known());
1790 elfcpp::Elf_types
<64>::Elf_Addr got_address
;
1791 got_address
= target
->got_section(NULL
, NULL
)->address();
1792 Relocate_functions
<64, false>::rela64(view
, object
, psymval
,
1793 addend
- got_address
);
1796 case elfcpp::R_X86_64_GOT32
:
1797 gold_assert(have_got_offset
);
1798 Relocate_functions
<64, false>::rela32(view
, got_offset
, addend
);
1801 case elfcpp::R_X86_64_GOTPC32
:
1804 elfcpp::Elf_types
<64>::Elf_Addr value
;
1805 value
= target
->got_plt_section()->address();
1806 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1810 case elfcpp::R_X86_64_GOT64
:
1811 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1812 // Since we always add a PLT entry, this is equivalent.
1813 case elfcpp::R_X86_64_GOTPLT64
:
1814 gold_assert(have_got_offset
);
1815 Relocate_functions
<64, false>::rela64(view
, got_offset
, addend
);
1818 case elfcpp::R_X86_64_GOTPC64
:
1821 elfcpp::Elf_types
<64>::Elf_Addr value
;
1822 value
= target
->got_plt_section()->address();
1823 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1827 case elfcpp::R_X86_64_GOTOFF64
:
1829 elfcpp::Elf_types
<64>::Elf_Addr value
;
1830 value
= (psymval
->value(object
, 0)
1831 - target
->got_plt_section()->address());
1832 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
1836 case elfcpp::R_X86_64_GOTPCREL
:
1838 gold_assert(have_got_offset
);
1839 elfcpp::Elf_types
<64>::Elf_Addr value
;
1840 value
= target
->got_plt_section()->address() + got_offset
;
1841 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
1845 case elfcpp::R_X86_64_GOTPCREL64
:
1847 gold_assert(have_got_offset
);
1848 elfcpp::Elf_types
<64>::Elf_Addr value
;
1849 value
= target
->got_plt_section()->address() + got_offset
;
1850 Relocate_functions
<64, false>::pcrela64(view
, value
, addend
, address
);
1854 case elfcpp::R_X86_64_COPY
:
1855 case elfcpp::R_X86_64_GLOB_DAT
:
1856 case elfcpp::R_X86_64_JUMP_SLOT
:
1857 case elfcpp::R_X86_64_RELATIVE
:
1858 // These are outstanding tls relocs, which are unexpected when linking
1859 case elfcpp::R_X86_64_TPOFF64
:
1860 case elfcpp::R_X86_64_DTPMOD64
:
1861 case elfcpp::R_X86_64_TLSDESC
:
1862 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1863 _("unexpected reloc %u in object file"),
1867 // These are initial tls relocs, which are expected when linking
1868 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1869 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1870 case elfcpp::R_X86_64_TLSDESC_CALL
:
1871 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
1872 case elfcpp::R_X86_64_DTPOFF32
:
1873 case elfcpp::R_X86_64_DTPOFF64
:
1874 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
1875 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
1876 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
1877 view
, address
, view_size
);
1880 case elfcpp::R_X86_64_SIZE32
:
1881 case elfcpp::R_X86_64_SIZE64
:
1883 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1884 _("unsupported reloc %u"),
1892 // Perform a TLS relocation.
1895 Target_x86_64::Relocate::relocate_tls(const Relocate_info
<64, false>* relinfo
,
1896 Target_x86_64
* target
,
1898 const elfcpp::Rela
<64, false>& rela
,
1899 unsigned int r_type
,
1900 const Sized_symbol
<64>* gsym
,
1901 const Symbol_value
<64>* psymval
,
1902 unsigned char* view
,
1903 elfcpp::Elf_types
<64>::Elf_Addr address
,
1904 section_size_type view_size
)
1906 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1908 const Sized_relobj
<64, false>* object
= relinfo
->object
;
1909 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
1911 elfcpp::Elf_types
<64>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1913 const bool is_final
= (gsym
== NULL
1914 ? !parameters
->options().output_is_position_independent()
1915 : gsym
->final_value_is_known());
1916 const tls::Tls_optimization optimized_type
1917 = Target_x86_64::optimize_tls_reloc(is_final
, r_type
);
1920 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
1921 this->saw_tls_block_reloc_
= true;
1922 if (optimized_type
== tls::TLSOPT_TO_LE
)
1924 gold_assert(tls_segment
!= NULL
);
1925 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1926 rela
, r_type
, value
, view
,
1932 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1933 ? GOT_TYPE_TLS_OFFSET
1934 : GOT_TYPE_TLS_PAIR
);
1935 unsigned int got_offset
;
1938 gold_assert(gsym
->has_got_offset(got_type
));
1939 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1943 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1944 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1945 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1946 - target
->got_size());
1948 if (optimized_type
== tls::TLSOPT_TO_IE
)
1950 gold_assert(tls_segment
!= NULL
);
1951 value
= target
->got_plt_section()->address() + got_offset
;
1952 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
1953 value
, view
, address
, view_size
);
1956 else if (optimized_type
== tls::TLSOPT_NONE
)
1958 // Relocate the field with the offset of the pair of GOT
1960 value
= target
->got_plt_section()->address() + got_offset
;
1961 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
1966 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
1967 _("unsupported reloc %u"), r_type
);
1970 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
1971 case elfcpp::R_X86_64_TLSDESC_CALL
:
1972 this->saw_tls_block_reloc_
= true;
1973 if (optimized_type
== tls::TLSOPT_TO_LE
)
1975 gold_assert(tls_segment
!= NULL
);
1976 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
1977 rela
, r_type
, value
, view
,
1983 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1984 ? GOT_TYPE_TLS_OFFSET
1985 : GOT_TYPE_TLS_DESC
);
1986 unsigned int got_offset
;
1989 gold_assert(gsym
->has_got_offset(got_type
));
1990 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1994 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
1995 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1996 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1997 - target
->got_size());
1999 if (optimized_type
== tls::TLSOPT_TO_IE
)
2001 gold_assert(tls_segment
!= NULL
);
2002 value
= target
->got_plt_section()->address() + got_offset
;
2003 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
2004 rela
, r_type
, value
, view
, address
,
2008 else if (optimized_type
== tls::TLSOPT_NONE
)
2010 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
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
,
2021 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2022 _("unsupported reloc %u"), r_type
);
2025 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2026 this->saw_tls_block_reloc_
= true;
2027 if (optimized_type
== tls::TLSOPT_TO_LE
)
2029 gold_assert(tls_segment
!= NULL
);
2030 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
2031 value
, view
, view_size
);
2034 else if (optimized_type
== tls::TLSOPT_NONE
)
2036 // Relocate the field with the offset of the GOT entry for
2037 // the module index.
2038 unsigned int got_offset
;
2039 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2040 - target
->got_size());
2041 value
= target
->got_plt_section()->address() + got_offset
;
2042 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
,
2046 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2047 _("unsupported reloc %u"), r_type
);
2050 case elfcpp::R_X86_64_DTPOFF32
:
2051 gold_assert(tls_segment
!= NULL
);
2052 if (optimized_type
== tls::TLSOPT_TO_LE
)
2054 // This relocation type is used in debugging information.
2055 // In that case we need to not optimize the value. If we
2056 // haven't seen a TLSLD reloc, then we assume we should not
2057 // optimize this reloc.
2058 if (this->saw_tls_block_reloc_
)
2059 value
-= tls_segment
->memsz();
2061 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2064 case elfcpp::R_X86_64_DTPOFF64
:
2065 gold_assert(tls_segment
!= NULL
);
2066 if (optimized_type
== tls::TLSOPT_TO_LE
)
2068 // See R_X86_64_DTPOFF32, just above, for why we test this.
2069 if (this->saw_tls_block_reloc_
)
2070 value
-= tls_segment
->memsz();
2072 Relocate_functions
<64, false>::rela64(view
, value
, addend
);
2075 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2076 if (optimized_type
== tls::TLSOPT_TO_LE
)
2078 gold_assert(tls_segment
!= NULL
);
2079 Target_x86_64::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2080 rela
, r_type
, value
, view
,
2084 else if (optimized_type
== tls::TLSOPT_NONE
)
2086 // Relocate the field with the offset of the GOT entry for
2087 // the tp-relative offset of the symbol.
2088 unsigned int got_offset
;
2091 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
2092 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
2093 - target
->got_size());
2097 unsigned int r_sym
= elfcpp::elf_r_sym
<64>(rela
.get_r_info());
2098 gold_assert(object
->local_has_got_offset(r_sym
,
2099 GOT_TYPE_TLS_OFFSET
));
2100 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
2101 - target
->got_size());
2103 value
= target
->got_plt_section()->address() + got_offset
;
2104 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2107 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
2108 _("unsupported reloc type %u"),
2112 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2113 value
-= tls_segment
->memsz();
2114 Relocate_functions
<64, false>::rela32(view
, value
, addend
);
2119 // Do a relocation in which we convert a TLS General-Dynamic to an
2123 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info
<64, false>* relinfo
,
2126 const elfcpp::Rela
<64, false>& rela
,
2128 elfcpp::Elf_types
<64>::Elf_Addr value
,
2129 unsigned char* view
,
2130 elfcpp::Elf_types
<64>::Elf_Addr address
,
2131 section_size_type view_size
)
2133 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2134 // .word 0x6666; rex64; call __tls_get_addr
2135 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2137 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2138 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2140 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2141 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2142 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2143 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2145 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2147 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2148 Relocate_functions
<64, false>::pcrela32(view
+ 8, value
, addend
- 8, address
);
2150 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2152 this->skip_call_tls_get_addr_
= true;
2155 // Do a relocation in which we convert a TLS General-Dynamic to a
2159 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info
<64, false>* relinfo
,
2161 Output_segment
* tls_segment
,
2162 const elfcpp::Rela
<64, false>& rela
,
2164 elfcpp::Elf_types
<64>::Elf_Addr value
,
2165 unsigned char* view
,
2166 section_size_type view_size
)
2168 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2169 // .word 0x6666; rex64; call __tls_get_addr
2170 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2172 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -4);
2173 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
2175 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2176 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
2177 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2178 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
2180 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2182 value
-= tls_segment
->memsz();
2183 Relocate_functions
<64, false>::rela32(view
+ 8, value
, 0);
2185 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2187 this->skip_call_tls_get_addr_
= true;
2190 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2193 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2194 const Relocate_info
<64, false>* relinfo
,
2197 const elfcpp::Rela
<64, false>& rela
,
2198 unsigned int r_type
,
2199 elfcpp::Elf_types
<64>::Elf_Addr value
,
2200 unsigned char* view
,
2201 elfcpp::Elf_types
<64>::Elf_Addr address
,
2202 section_size_type view_size
)
2204 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2206 // leaq foo@tlsdesc(%rip), %rax
2207 // ==> movq foo@gottpoff(%rip), %rax
2208 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2209 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2210 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2211 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2213 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
2214 Relocate_functions
<64, false>::pcrela32(view
, value
, addend
, address
);
2218 // call *foo@tlscall(%rax)
2220 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2221 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2222 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2223 view
[0] == 0xff && view
[1] == 0x10);
2229 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2232 Target_x86_64::Relocate::tls_desc_gd_to_le(
2233 const Relocate_info
<64, false>* relinfo
,
2235 Output_segment
* tls_segment
,
2236 const elfcpp::Rela
<64, false>& rela
,
2237 unsigned int r_type
,
2238 elfcpp::Elf_types
<64>::Elf_Addr value
,
2239 unsigned char* view
,
2240 section_size_type view_size
)
2242 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
2244 // leaq foo@tlsdesc(%rip), %rax
2245 // ==> movq foo@tpoff, %rax
2246 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2247 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2248 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2249 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
2252 value
-= tls_segment
->memsz();
2253 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2257 // call *foo@tlscall(%rax)
2259 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
2260 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
2261 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2262 view
[0] == 0xff && view
[1] == 0x10);
2269 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info
<64, false>* relinfo
,
2272 const elfcpp::Rela
<64, false>& rela
,
2274 elfcpp::Elf_types
<64>::Elf_Addr
,
2275 unsigned char* view
,
2276 section_size_type view_size
)
2278 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2279 // ... leq foo@dtpoff(%rax),%reg
2280 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2282 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2283 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
2285 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
2286 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
2288 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
2290 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2292 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2294 this->skip_call_tls_get_addr_
= true;
2297 // Do a relocation in which we convert a TLS Initial-Exec to a
2301 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info
<64, false>* relinfo
,
2303 Output_segment
* tls_segment
,
2304 const elfcpp::Rela
<64, false>& rela
,
2306 elfcpp::Elf_types
<64>::Elf_Addr value
,
2307 unsigned char* view
,
2308 section_size_type view_size
)
2310 // We need to examine the opcodes to figure out which instruction we
2313 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2314 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2316 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
2317 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
2319 unsigned char op1
= view
[-3];
2320 unsigned char op2
= view
[-2];
2321 unsigned char op3
= view
[-1];
2322 unsigned char reg
= op3
>> 3;
2330 view
[-1] = 0xc0 | reg
;
2334 // Special handling for %rsp.
2338 view
[-1] = 0xc0 | reg
;
2346 view
[-1] = 0x80 | reg
| (reg
<< 3);
2349 value
-= tls_segment
->memsz();
2350 Relocate_functions
<64, false>::rela32(view
, value
, 0);
2353 // Relocate section data.
2356 Target_x86_64::relocate_section(const Relocate_info
<64, false>* relinfo
,
2357 unsigned int sh_type
,
2358 const unsigned char* prelocs
,
2360 Output_section
* output_section
,
2361 bool needs_special_offset_handling
,
2362 unsigned char* view
,
2363 elfcpp::Elf_types
<64>::Elf_Addr address
,
2364 section_size_type view_size
)
2366 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2368 gold::relocate_section
<64, false, Target_x86_64
, elfcpp::SHT_RELA
,
2369 Target_x86_64::Relocate
>(
2375 needs_special_offset_handling
,
2381 // Return the size of a relocation while scanning during a relocatable
2385 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2386 unsigned int r_type
,
2391 case elfcpp::R_X86_64_NONE
:
2392 case elfcpp::R_386_GNU_VTINHERIT
:
2393 case elfcpp::R_386_GNU_VTENTRY
:
2394 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2395 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2396 case elfcpp::R_X86_64_TLSDESC_CALL
:
2397 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2398 case elfcpp::R_X86_64_DTPOFF32
:
2399 case elfcpp::R_X86_64_DTPOFF64
:
2400 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2401 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2404 case elfcpp::R_X86_64_64
:
2405 case elfcpp::R_X86_64_PC64
:
2406 case elfcpp::R_X86_64_GOTOFF64
:
2407 case elfcpp::R_X86_64_GOTPC64
:
2408 case elfcpp::R_X86_64_PLTOFF64
:
2409 case elfcpp::R_X86_64_GOT64
:
2410 case elfcpp::R_X86_64_GOTPCREL64
:
2411 case elfcpp::R_X86_64_GOTPCREL
:
2412 case elfcpp::R_X86_64_GOTPLT64
:
2415 case elfcpp::R_X86_64_32
:
2416 case elfcpp::R_X86_64_32S
:
2417 case elfcpp::R_X86_64_PC32
:
2418 case elfcpp::R_X86_64_PLT32
:
2419 case elfcpp::R_X86_64_GOTPC32
:
2420 case elfcpp::R_X86_64_GOT32
:
2423 case elfcpp::R_X86_64_16
:
2424 case elfcpp::R_X86_64_PC16
:
2427 case elfcpp::R_X86_64_8
:
2428 case elfcpp::R_X86_64_PC8
:
2431 case elfcpp::R_X86_64_COPY
:
2432 case elfcpp::R_X86_64_GLOB_DAT
:
2433 case elfcpp::R_X86_64_JUMP_SLOT
:
2434 case elfcpp::R_X86_64_RELATIVE
:
2435 // These are outstanding tls relocs, which are unexpected when linking
2436 case elfcpp::R_X86_64_TPOFF64
:
2437 case elfcpp::R_X86_64_DTPMOD64
:
2438 case elfcpp::R_X86_64_TLSDESC
:
2439 object
->error(_("unexpected reloc %u in object file"), r_type
);
2442 case elfcpp::R_X86_64_SIZE32
:
2443 case elfcpp::R_X86_64_SIZE64
:
2445 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
2450 // Scan the relocs during a relocatable link.
2453 Target_x86_64::scan_relocatable_relocs(const General_options
& options
,
2454 Symbol_table
* symtab
,
2456 Sized_relobj
<64, false>* object
,
2457 unsigned int data_shndx
,
2458 unsigned int sh_type
,
2459 const unsigned char* prelocs
,
2461 Output_section
* output_section
,
2462 bool needs_special_offset_handling
,
2463 size_t local_symbol_count
,
2464 const unsigned char* plocal_symbols
,
2465 Relocatable_relocs
* rr
)
2467 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2469 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
2470 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2472 gold::scan_relocatable_relocs
<64, false, elfcpp::SHT_RELA
,
2473 Scan_relocatable_relocs
>(
2482 needs_special_offset_handling
,
2488 // Relocate a section during a relocatable link.
2491 Target_x86_64::relocate_for_relocatable(
2492 const Relocate_info
<64, false>* relinfo
,
2493 unsigned int sh_type
,
2494 const unsigned char* prelocs
,
2496 Output_section
* output_section
,
2497 off_t offset_in_output_section
,
2498 const Relocatable_relocs
* rr
,
2499 unsigned char* view
,
2500 elfcpp::Elf_types
<64>::Elf_Addr view_address
,
2501 section_size_type view_size
,
2502 unsigned char* reloc_view
,
2503 section_size_type reloc_view_size
)
2505 gold_assert(sh_type
== elfcpp::SHT_RELA
);
2507 gold::relocate_for_relocatable
<64, false, elfcpp::SHT_RELA
>(
2512 offset_in_output_section
,
2521 // Return the value to use for a dynamic which requires special
2522 // treatment. This is how we support equality comparisons of function
2523 // pointers across shared library boundaries, as described in the
2524 // processor specific ABI supplement.
2527 Target_x86_64::do_dynsym_value(const Symbol
* gsym
) const
2529 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2530 return this->plt_section()->address() + gsym
->plt_offset();
2533 // Return a string used to fill a code section with nops to take up
2534 // the specified length.
2537 Target_x86_64::do_code_fill(section_size_type length
) const
2541 // Build a jmpq instruction to skip over the bytes.
2542 unsigned char jmp
[5];
2544 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2545 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2546 + std::string(length
- 5, '\0'));
2549 // Nop sequences of various lengths.
2550 const char nop1
[1] = { 0x90 }; // nop
2551 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2552 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2553 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2554 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2555 0x00 }; // leal 0(%esi,1),%esi
2556 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2558 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2560 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2561 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2562 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2563 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2565 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2566 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2568 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2569 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2571 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2572 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2573 0x00, 0x00, 0x00, 0x00 };
2574 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2575 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2576 0x27, 0x00, 0x00, 0x00,
2578 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2579 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2580 0xbc, 0x27, 0x00, 0x00,
2582 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2583 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2584 0x90, 0x90, 0x90, 0x90,
2587 const char* nops
[16] = {
2589 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2590 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2593 return std::string(nops
[length
], length
);
2596 // The selector for x86_64 object files.
2598 class Target_selector_x86_64
: public Target_selector
2601 Target_selector_x86_64()
2602 : Target_selector(elfcpp::EM_X86_64
, 64, false, "elf64-x86-64")
2606 do_instantiate_target()
2607 { return new Target_x86_64(); }
2610 Target_selector_x86_64 target_selector_x86_64
;
2612 } // End anonymous namespace.