1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2018 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.
29 #include "parameters.h"
36 #include "copy-relocs.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
51 // A class to handle the .got.plt section.
53 class Output_data_got_plt_x86_64
: public Output_section_data_build
56 Output_data_got_plt_x86_64(Layout
* layout
)
57 : Output_section_data_build(8),
61 Output_data_got_plt_x86_64(Layout
* layout
, off_t data_size
)
62 : Output_section_data_build(data_size
, 8),
67 // Write out the PLT data.
69 do_write(Output_file
*);
71 // Write to a map file.
73 do_print_to_mapfile(Mapfile
* mapfile
) const
74 { mapfile
->print_output_data(this, "** GOT PLT"); }
77 // A pointer to the Layout class, so that we can find the .dynamic
78 // section when we write out the GOT PLT section.
82 // A class to handle the PLT data.
83 // This is an abstract base class that handles most of the linker details
84 // but does not know the actual contents of PLT entries. The derived
85 // classes below fill in those details.
88 class Output_data_plt_x86_64
: public Output_section_data
91 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
93 Output_data_plt_x86_64(Layout
* layout
, uint64_t addralign
,
94 Output_data_got
<64, false>* got
,
95 Output_data_got_plt_x86_64
* got_plt
,
96 Output_data_space
* got_irelative
)
97 : Output_section_data(addralign
), tlsdesc_rel_(NULL
),
98 irelative_rel_(NULL
), got_(got
), got_plt_(got_plt
),
99 got_irelative_(got_irelative
), count_(0), irelative_count_(0),
100 tlsdesc_got_offset_(-1U), free_list_()
101 { this->init(layout
); }
103 Output_data_plt_x86_64(Layout
* layout
, uint64_t plt_entry_size
,
104 Output_data_got
<64, false>* got
,
105 Output_data_got_plt_x86_64
* got_plt
,
106 Output_data_space
* got_irelative
,
107 unsigned int plt_count
)
108 : Output_section_data((plt_count
+ 1) * plt_entry_size
,
109 plt_entry_size
, false),
110 tlsdesc_rel_(NULL
), irelative_rel_(NULL
), got_(got
),
111 got_plt_(got_plt
), got_irelative_(got_irelative
), count_(plt_count
),
112 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
116 // Initialize the free list and reserve the first entry.
117 this->free_list_
.init((plt_count
+ 1) * plt_entry_size
, false);
118 this->free_list_
.remove(0, plt_entry_size
);
121 // Initialize the PLT section.
123 init(Layout
* layout
);
125 // Add an entry to the PLT.
127 add_entry(Symbol_table
*, Layout
*, Symbol
* gsym
);
129 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
131 add_local_ifunc_entry(Symbol_table
* symtab
, Layout
*,
132 Sized_relobj_file
<size
, false>* relobj
,
133 unsigned int local_sym_index
);
135 // Add the relocation for a PLT entry.
137 add_relocation(Symbol_table
*, Layout
*, Symbol
* gsym
,
138 unsigned int got_offset
);
140 // Add the reserved TLSDESC_PLT entry to the PLT.
142 reserve_tlsdesc_entry(unsigned int got_offset
)
143 { this->tlsdesc_got_offset_
= got_offset
; }
145 // Return true if a TLSDESC_PLT entry has been reserved.
147 has_tlsdesc_entry() const
148 { return this->tlsdesc_got_offset_
!= -1U; }
150 // Return the GOT offset for the reserved TLSDESC_PLT entry.
152 get_tlsdesc_got_offset() const
153 { return this->tlsdesc_got_offset_
; }
155 // Return the offset of the reserved TLSDESC_PLT entry.
157 get_tlsdesc_plt_offset() const
159 return ((this->count_
+ this->irelative_count_
+ 1)
160 * this->get_plt_entry_size());
163 // Return the .rela.plt section data.
166 { return this->rel_
; }
168 // Return where the TLSDESC relocations should go.
170 rela_tlsdesc(Layout
*);
172 // Return where the IRELATIVE relocations should go in the PLT
175 rela_irelative(Symbol_table
*, Layout
*);
177 // Return whether we created a section for IRELATIVE relocations.
179 has_irelative_section() const
180 { return this->irelative_rel_
!= NULL
; }
182 // Get count of regular PLT entries.
184 regular_count() const
185 { return this->count_
; }
187 // Return the total number of PLT entries.
190 { return this->count_
+ this->irelative_count_
; }
192 // Return the offset of the first non-reserved PLT entry.
194 first_plt_entry_offset()
195 { return this->get_plt_entry_size(); }
197 // Return the size of a PLT entry.
199 get_plt_entry_size() const
200 { return this->do_get_plt_entry_size(); }
202 // Reserve a slot in the PLT for an existing symbol in an incremental update.
204 reserve_slot(unsigned int plt_index
)
206 this->free_list_
.remove((plt_index
+ 1) * this->get_plt_entry_size(),
207 (plt_index
+ 2) * this->get_plt_entry_size());
210 // Return the PLT address to use for a global symbol.
212 address_for_global(const Symbol
* sym
)
213 { return do_address_for_global(sym
); }
215 // Return the PLT address to use for a local symbol.
217 address_for_local(const Relobj
* obj
, unsigned int symndx
)
218 { return do_address_for_local(obj
, symndx
); }
220 // Add .eh_frame information for the PLT.
222 add_eh_frame(Layout
* layout
)
223 { this->do_add_eh_frame(layout
); }
226 Output_data_got
<64, false>*
228 { return this->got_
; }
230 Output_data_got_plt_x86_64
*
232 { return this->got_plt_
; }
235 got_irelative() const
236 { return this->got_irelative_
; }
238 // Fill in the first PLT entry.
240 fill_first_plt_entry(unsigned char* pov
,
241 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
242 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
243 { this->do_fill_first_plt_entry(pov
, got_address
, plt_address
); }
245 // Fill in a normal PLT entry. Returns the offset into the entry that
246 // should be the initial GOT slot value.
248 fill_plt_entry(unsigned char* pov
,
249 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
250 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
251 unsigned int got_offset
,
252 unsigned int plt_offset
,
253 unsigned int plt_index
)
255 return this->do_fill_plt_entry(pov
, got_address
, plt_address
,
256 got_offset
, plt_offset
, plt_index
);
259 // Fill in the reserved TLSDESC PLT entry.
261 fill_tlsdesc_entry(unsigned char* pov
,
262 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
263 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
264 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
265 unsigned int tlsdesc_got_offset
,
266 unsigned int plt_offset
)
268 this->do_fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
269 tlsdesc_got_offset
, plt_offset
);
273 do_get_plt_entry_size() const = 0;
276 do_fill_first_plt_entry(unsigned char* pov
,
277 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
278 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
)
282 do_fill_plt_entry(unsigned char* pov
,
283 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
284 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
285 unsigned int got_offset
,
286 unsigned int plt_offset
,
287 unsigned int plt_index
) = 0;
290 do_fill_tlsdesc_entry(unsigned char* pov
,
291 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
292 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
293 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
294 unsigned int tlsdesc_got_offset
,
295 unsigned int plt_offset
) = 0;
297 // Return the PLT address to use for a global symbol.
299 do_address_for_global(const Symbol
* sym
);
301 // Return the PLT address to use for a local symbol.
303 do_address_for_local(const Relobj
* obj
, unsigned int symndx
);
306 do_add_eh_frame(Layout
* layout
) = 0;
309 do_adjust_output_section(Output_section
* os
);
311 // Write to a map file.
313 do_print_to_mapfile(Mapfile
* mapfile
) const
314 { mapfile
->print_output_data(this, _("** PLT")); }
316 // The CIE of the .eh_frame unwind information for the PLT.
317 static const int plt_eh_frame_cie_size
= 16;
318 static const unsigned char plt_eh_frame_cie
[plt_eh_frame_cie_size
];
321 // Set the final size.
323 set_final_data_size();
325 // Write out the PLT data.
327 do_write(Output_file
*);
329 // The reloc section.
331 // The TLSDESC relocs, if necessary. These must follow the regular
333 Reloc_section
* tlsdesc_rel_
;
334 // The IRELATIVE relocs, if necessary. These must follow the
335 // regular PLT relocations and the TLSDESC relocations.
336 Reloc_section
* irelative_rel_
;
338 Output_data_got
<64, false>* got_
;
339 // The .got.plt section.
340 Output_data_got_plt_x86_64
* got_plt_
;
341 // The part of the .got.plt section used for IRELATIVE relocs.
342 Output_data_space
* got_irelative_
;
343 // The number of PLT entries.
345 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
346 // follow the regular PLT entries.
347 unsigned int irelative_count_
;
348 // Offset of the reserved TLSDESC_GOT entry when needed.
349 unsigned int tlsdesc_got_offset_
;
350 // List of available regions within the section, for incremental
352 Free_list free_list_
;
356 class Output_data_plt_x86_64_standard
: public Output_data_plt_x86_64
<size
>
359 Output_data_plt_x86_64_standard(Layout
* layout
,
360 Output_data_got
<64, false>* got
,
361 Output_data_got_plt_x86_64
* got_plt
,
362 Output_data_space
* got_irelative
)
363 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
364 got
, got_plt
, got_irelative
)
367 Output_data_plt_x86_64_standard(Layout
* layout
,
368 Output_data_got
<64, false>* got
,
369 Output_data_got_plt_x86_64
* got_plt
,
370 Output_data_space
* got_irelative
,
371 unsigned int plt_count
)
372 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
373 got
, got_plt
, got_irelative
,
379 do_get_plt_entry_size() const
380 { return plt_entry_size
; }
383 do_add_eh_frame(Layout
* layout
)
385 layout
->add_eh_frame_for_plt(this,
386 this->plt_eh_frame_cie
,
387 this->plt_eh_frame_cie_size
,
389 plt_eh_frame_fde_size
);
393 do_fill_first_plt_entry(unsigned char* pov
,
394 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
395 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
398 do_fill_plt_entry(unsigned char* pov
,
399 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
400 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
401 unsigned int got_offset
,
402 unsigned int plt_offset
,
403 unsigned int plt_index
);
406 do_fill_tlsdesc_entry(unsigned char* pov
,
407 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
408 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
409 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
410 unsigned int tlsdesc_got_offset
,
411 unsigned int plt_offset
);
414 // The size of an entry in the PLT.
415 static const int plt_entry_size
= 16;
417 // The first entry in the PLT.
418 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
419 // procedure linkage table for both programs and shared objects."
420 static const unsigned char first_plt_entry
[plt_entry_size
];
422 // Other entries in the PLT for an executable.
423 static const unsigned char plt_entry
[plt_entry_size
];
425 // The reserved TLSDESC entry in the PLT for an executable.
426 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
428 // The .eh_frame unwind information for the PLT.
429 static const int plt_eh_frame_fde_size
= 32;
430 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
433 class Output_data_plt_x86_64_bnd
: public Output_data_plt_x86_64
<64>
436 Output_data_plt_x86_64_bnd(Layout
* layout
,
437 Output_data_got
<64, false>* got
,
438 Output_data_got_plt_x86_64
* got_plt
,
439 Output_data_space
* got_irelative
)
440 : Output_data_plt_x86_64
<64>(layout
, plt_entry_size
,
441 got
, got_plt
, got_irelative
),
445 Output_data_plt_x86_64_bnd(Layout
* layout
,
446 Output_data_got
<64, false>* got
,
447 Output_data_got_plt_x86_64
* got_plt
,
448 Output_data_space
* got_irelative
,
449 unsigned int plt_count
)
450 : Output_data_plt_x86_64
<64>(layout
, plt_entry_size
,
451 got
, got_plt
, got_irelative
,
458 do_get_plt_entry_size() const
459 { return plt_entry_size
; }
461 // Return the PLT address to use for a global symbol.
463 do_address_for_global(const Symbol
*);
465 // Return the PLT address to use for a local symbol.
467 do_address_for_local(const Relobj
*, unsigned int symndx
);
470 do_add_eh_frame(Layout
* layout
)
472 layout
->add_eh_frame_for_plt(this,
473 this->plt_eh_frame_cie
,
474 this->plt_eh_frame_cie_size
,
476 plt_eh_frame_fde_size
);
480 do_fill_first_plt_entry(unsigned char* pov
,
481 elfcpp::Elf_types
<64>::Elf_Addr got_addr
,
482 elfcpp::Elf_types
<64>::Elf_Addr plt_addr
);
485 do_fill_plt_entry(unsigned char* pov
,
486 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
487 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
488 unsigned int got_offset
,
489 unsigned int plt_offset
,
490 unsigned int plt_index
);
493 do_fill_tlsdesc_entry(unsigned char* pov
,
494 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
495 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
496 elfcpp::Elf_types
<64>::Elf_Addr got_base
,
497 unsigned int tlsdesc_got_offset
,
498 unsigned int plt_offset
);
501 fill_aplt_entry(unsigned char* pov
,
502 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
503 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
504 unsigned int got_offset
,
505 unsigned int plt_offset
,
506 unsigned int plt_index
);
509 // Set the final size.
511 set_final_data_size();
513 // Write out the BND PLT data.
515 do_write(Output_file
*);
517 // Offset of the Additional PLT (if using -z bndplt).
518 unsigned int aplt_offset_
;
520 // The size of an entry in the PLT.
521 static const int plt_entry_size
= 16;
523 // The size of an entry in the additional PLT.
524 static const int aplt_entry_size
= 8;
526 // The first entry in the PLT.
527 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
528 // procedure linkage table for both programs and shared objects."
529 static const unsigned char first_plt_entry
[plt_entry_size
];
531 // Other entries in the PLT for an executable.
532 static const unsigned char plt_entry
[plt_entry_size
];
534 // Entries in the additional PLT.
535 static const unsigned char aplt_entry
[aplt_entry_size
];
537 // The reserved TLSDESC entry in the PLT for an executable.
538 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
540 // The .eh_frame unwind information for the PLT.
541 static const int plt_eh_frame_fde_size
= 32;
542 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
549 Lazy_view(Sized_relobj_file
<size
, false>* object
, unsigned int data_shndx
)
550 : object_(object
), data_shndx_(data_shndx
), view_(NULL
), view_size_(0)
554 operator[](size_t offset
)
556 if (this->view_
== NULL
)
557 this->view_
= this->object_
->section_contents(this->data_shndx_
,
560 if (offset
>= this->view_size_
)
562 return this->view_
[offset
];
566 Sized_relobj_file
<size
, false>* object_
;
567 unsigned int data_shndx_
;
568 const unsigned char* view_
;
569 section_size_type view_size_
;
572 // The x86_64 target class.
574 // http://www.x86-64.org/documentation/abi.pdf
575 // TLS info comes from
576 // http://people.redhat.com/drepper/tls.pdf
577 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
580 class Target_x86_64
: public Sized_target
<size
, false>
583 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
584 // uses only Elf64_Rela relocation entries with explicit addends."
585 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
587 Target_x86_64(const Target::Target_info
* info
= &x86_64_info
)
588 : Sized_target
<size
, false>(info
),
589 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_irelative_(NULL
),
590 got_tlsdesc_(NULL
), global_offset_table_(NULL
), rela_dyn_(NULL
),
591 rela_irelative_(NULL
), copy_relocs_(elfcpp::R_X86_64_COPY
),
592 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
593 tls_base_symbol_defined_(false), isa_1_used_(0), isa_1_needed_(0),
594 feature_1_(0), object_feature_1_(0), seen_first_object_(false)
597 // Hook for a new output section.
599 do_new_output_section(Output_section
*) const;
601 // Scan the relocations to look for symbol adjustments.
603 gc_process_relocs(Symbol_table
* symtab
,
605 Sized_relobj_file
<size
, false>* object
,
606 unsigned int data_shndx
,
607 unsigned int sh_type
,
608 const unsigned char* prelocs
,
610 Output_section
* output_section
,
611 bool needs_special_offset_handling
,
612 size_t local_symbol_count
,
613 const unsigned char* plocal_symbols
);
615 // Scan the relocations to look for symbol adjustments.
617 scan_relocs(Symbol_table
* symtab
,
619 Sized_relobj_file
<size
, false>* object
,
620 unsigned int data_shndx
,
621 unsigned int sh_type
,
622 const unsigned char* prelocs
,
624 Output_section
* output_section
,
625 bool needs_special_offset_handling
,
626 size_t local_symbol_count
,
627 const unsigned char* plocal_symbols
);
629 // Finalize the sections.
631 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
633 // Return the value to use for a dynamic which requires special
636 do_dynsym_value(const Symbol
*) const;
638 // Relocate a section.
640 relocate_section(const Relocate_info
<size
, false>*,
641 unsigned int sh_type
,
642 const unsigned char* prelocs
,
644 Output_section
* output_section
,
645 bool needs_special_offset_handling
,
647 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
648 section_size_type view_size
,
649 const Reloc_symbol_changes
*);
651 // Scan the relocs during a relocatable link.
653 scan_relocatable_relocs(Symbol_table
* symtab
,
655 Sized_relobj_file
<size
, false>* object
,
656 unsigned int data_shndx
,
657 unsigned int sh_type
,
658 const unsigned char* prelocs
,
660 Output_section
* output_section
,
661 bool needs_special_offset_handling
,
662 size_t local_symbol_count
,
663 const unsigned char* plocal_symbols
,
664 Relocatable_relocs
*);
666 // Scan the relocs for --emit-relocs.
668 emit_relocs_scan(Symbol_table
* symtab
,
670 Sized_relobj_file
<size
, false>* object
,
671 unsigned int data_shndx
,
672 unsigned int sh_type
,
673 const unsigned char* prelocs
,
675 Output_section
* output_section
,
676 bool needs_special_offset_handling
,
677 size_t local_symbol_count
,
678 const unsigned char* plocal_syms
,
679 Relocatable_relocs
* rr
);
681 // Emit relocations for a section.
684 const Relocate_info
<size
, false>*,
685 unsigned int sh_type
,
686 const unsigned char* prelocs
,
688 Output_section
* output_section
,
689 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
691 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
692 section_size_type view_size
,
693 unsigned char* reloc_view
,
694 section_size_type reloc_view_size
);
696 // Return a string used to fill a code section with nops.
698 do_code_fill(section_size_type length
) const;
700 // Return whether SYM is defined by the ABI.
702 do_is_defined_by_abi(const Symbol
* sym
) const
703 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
705 // Return the symbol index to use for a target specific relocation.
706 // The only target specific relocation is R_X86_64_TLSDESC for a
707 // local symbol, which is an absolute reloc.
709 do_reloc_symbol_index(void*, unsigned int r_type
) const
711 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
715 // Return the addend to use for a target specific relocation.
717 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
719 // Return the PLT section.
721 do_plt_address_for_global(const Symbol
* gsym
) const
722 { return this->plt_section()->address_for_global(gsym
); }
725 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
726 { return this->plt_section()->address_for_local(relobj
, symndx
); }
728 // This function should be defined in targets that can use relocation
729 // types to determine (implemented in local_reloc_may_be_function_pointer
730 // and global_reloc_may_be_function_pointer)
731 // if a function's pointer is taken. ICF uses this in safe mode to only
732 // fold those functions whose pointer is defintely not taken. For x86_64
733 // pie binaries, safe ICF cannot be done by looking at only relocation
734 // types, and for certain cases (e.g. R_X86_64_PC32), the instruction
735 // opcode is checked as well to distinguish a function call from taking
736 // a function's pointer.
738 do_can_check_for_function_pointers() const
741 // Return the base for a DW_EH_PE_datarel encoding.
743 do_ehframe_datarel_base() const;
745 // Adjust -fsplit-stack code which calls non-split-stack code.
747 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
748 section_offset_type fnoffset
, section_size_type fnsize
,
749 const unsigned char* prelocs
, size_t reloc_count
,
750 unsigned char* view
, section_size_type view_size
,
751 std::string
* from
, std::string
* to
) const;
753 // Return the size of the GOT section.
757 gold_assert(this->got_
!= NULL
);
758 return this->got_
->data_size();
761 // Return the number of entries in the GOT.
763 got_entry_count() const
765 if (this->got_
== NULL
)
767 return this->got_size() / 8;
770 // Return the number of entries in the PLT.
772 plt_entry_count() const;
774 // Return the offset of the first non-reserved PLT entry.
776 first_plt_entry_offset() const;
778 // Return the size of each PLT entry.
780 plt_entry_size() const;
782 // Return the size of each GOT entry.
784 got_entry_size() const
787 // Create the GOT section for an incremental update.
788 Output_data_got_base
*
789 init_got_plt_for_update(Symbol_table
* symtab
,
791 unsigned int got_count
,
792 unsigned int plt_count
);
794 // Reserve a GOT entry for a local symbol, and regenerate any
795 // necessary dynamic relocations.
797 reserve_local_got_entry(unsigned int got_index
,
798 Sized_relobj
<size
, false>* obj
,
800 unsigned int got_type
);
802 // Reserve a GOT entry for a global symbol, and regenerate any
803 // necessary dynamic relocations.
805 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
806 unsigned int got_type
);
808 // Register an existing PLT entry for a global symbol.
810 register_global_plt_entry(Symbol_table
*, Layout
*, unsigned int plt_index
,
813 // Force a COPY relocation for a given symbol.
815 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
817 // Apply an incremental relocation.
819 apply_relocation(const Relocate_info
<size
, false>* relinfo
,
820 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
822 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
825 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
826 section_size_type view_size
);
828 // Add a new reloc argument, returning the index in the vector.
830 add_tlsdesc_info(Sized_relobj_file
<size
, false>* object
, unsigned int r_sym
)
832 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
833 return this->tlsdesc_reloc_info_
.size() - 1;
836 Output_data_plt_x86_64
<size
>*
837 make_data_plt(Layout
* layout
,
838 Output_data_got
<64, false>* got
,
839 Output_data_got_plt_x86_64
* got_plt
,
840 Output_data_space
* got_irelative
)
842 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
845 Output_data_plt_x86_64
<size
>*
846 make_data_plt(Layout
* layout
,
847 Output_data_got
<64, false>* got
,
848 Output_data_got_plt_x86_64
* got_plt
,
849 Output_data_space
* got_irelative
,
850 unsigned int plt_count
)
852 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
,
856 virtual Output_data_plt_x86_64
<size
>*
857 do_make_data_plt(Layout
* layout
,
858 Output_data_got
<64, false>* got
,
859 Output_data_got_plt_x86_64
* got_plt
,
860 Output_data_space
* got_irelative
);
862 virtual Output_data_plt_x86_64
<size
>*
863 do_make_data_plt(Layout
* layout
,
864 Output_data_got
<64, false>* got
,
865 Output_data_got_plt_x86_64
* got_plt
,
866 Output_data_space
* got_irelative
,
867 unsigned int plt_count
);
870 // The class which scans relocations.
875 : issued_non_pic_error_(false)
879 get_reference_flags(unsigned int r_type
);
882 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
883 Sized_relobj_file
<size
, false>* object
,
884 unsigned int data_shndx
,
885 Output_section
* output_section
,
886 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
887 const elfcpp::Sym
<size
, false>& lsym
,
891 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
892 Sized_relobj_file
<size
, false>* object
,
893 unsigned int data_shndx
,
894 Output_section
* output_section
,
895 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
899 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
900 Target_x86_64
* target
,
901 Sized_relobj_file
<size
, false>* object
,
902 unsigned int data_shndx
,
903 Output_section
* output_section
,
904 const elfcpp::Rela
<size
, false>& reloc
,
906 const elfcpp::Sym
<size
, false>& lsym
);
909 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
910 Target_x86_64
* target
,
911 Sized_relobj_file
<size
, false>* object
,
912 unsigned int data_shndx
,
913 Output_section
* output_section
,
914 const elfcpp::Rela
<size
, false>& reloc
,
920 unsupported_reloc_local(Sized_relobj_file
<size
, false>*,
921 unsigned int r_type
);
924 unsupported_reloc_global(Sized_relobj_file
<size
, false>*,
925 unsigned int r_type
, Symbol
*);
928 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
931 possible_function_pointer_reloc(Sized_relobj_file
<size
, false>* src_obj
,
932 unsigned int src_indx
,
933 unsigned int r_offset
,
934 unsigned int r_type
);
937 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, false>*,
938 unsigned int r_type
);
940 // Whether we have issued an error about a non-PIC compilation.
941 bool issued_non_pic_error_
;
944 // The class which implements relocation.
949 : skip_call_tls_get_addr_(false)
954 if (this->skip_call_tls_get_addr_
)
956 // FIXME: This needs to specify the location somehow.
957 gold_error(_("missing expected TLS relocation"));
961 // Do a relocation. Return false if the caller should not issue
962 // any warnings about this relocation.
964 relocate(const Relocate_info
<size
, false>*, unsigned int,
965 Target_x86_64
*, Output_section
*, size_t, const unsigned char*,
966 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
967 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
971 // Do a TLS relocation.
973 relocate_tls(const Relocate_info
<size
, false>*, Target_x86_64
*,
974 size_t relnum
, const elfcpp::Rela
<size
, false>&,
975 unsigned int r_type
, const Sized_symbol
<size
>*,
976 const Symbol_value
<size
>*,
977 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
980 // Do a TLS General-Dynamic to Initial-Exec transition.
982 tls_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
983 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
984 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
986 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
987 section_size_type view_size
);
989 // Do a TLS General-Dynamic to Local-Exec transition.
991 tls_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
992 Output_segment
* tls_segment
,
993 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
994 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
996 section_size_type view_size
);
998 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
1000 tls_desc_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
1001 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1002 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1003 unsigned char* view
,
1004 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1005 section_size_type view_size
);
1007 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
1009 tls_desc_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1010 Output_segment
* tls_segment
,
1011 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1012 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1013 unsigned char* view
,
1014 section_size_type view_size
);
1016 // Do a TLS Local-Dynamic to Local-Exec transition.
1018 tls_ld_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1019 Output_segment
* tls_segment
,
1020 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1021 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1022 unsigned char* view
,
1023 section_size_type view_size
);
1025 // Do a TLS Initial-Exec to Local-Exec transition.
1027 tls_ie_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1028 Output_segment
* tls_segment
,
1029 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1030 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1031 unsigned char* view
,
1032 section_size_type view_size
);
1034 // This is set if we should skip the next reloc, which should be a
1035 // PLT32 reloc against ___tls_get_addr.
1036 bool skip_call_tls_get_addr_
;
1039 // Check if relocation against this symbol is a candidate for
1041 // mov foo@GOTPCREL(%rip), %reg
1042 // to lea foo(%rip), %reg.
1043 template<class View_type
>
1045 can_convert_mov_to_lea(const Symbol
* gsym
, unsigned int r_type
,
1046 size_t r_offset
, View_type
* view
)
1048 gold_assert(gsym
!= NULL
);
1049 // We cannot do the conversion unless it's one of these relocations.
1050 if (r_type
!= elfcpp::R_X86_64_GOTPCREL
1051 && r_type
!= elfcpp::R_X86_64_GOTPCRELX
1052 && r_type
!= elfcpp::R_X86_64_REX_GOTPCRELX
)
1054 // We cannot convert references to IFUNC symbols, or to symbols that
1055 // are not local to the current module.
1056 // We can't do predefined symbols because they may become undefined
1057 // (e.g., __ehdr_start when the headers aren't mapped to a segment).
1058 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1059 || gsym
->is_undefined()
1060 || gsym
->is_predefined()
1061 || gsym
->is_from_dynobj()
1062 || gsym
->is_preemptible())
1064 // If we are building a shared object and the symbol is protected, we may
1065 // need to go through the GOT.
1066 if (parameters
->options().shared()
1067 && gsym
->visibility() == elfcpp::STV_PROTECTED
)
1069 // We cannot convert references to the _DYNAMIC symbol.
1070 if (strcmp(gsym
->name(), "_DYNAMIC") == 0)
1072 // Check for a MOV opcode.
1073 return (*view
)[r_offset
- 2] == 0x8b;
1077 // callq *foo@GOTPCRELX(%rip) to
1079 // and jmpq *foo@GOTPCRELX(%rip) to
1082 template<class View_type
>
1084 can_convert_callq_to_direct(const Symbol
* gsym
, unsigned int r_type
,
1085 size_t r_offset
, View_type
* view
)
1087 gold_assert(gsym
!= NULL
);
1088 // We cannot do the conversion unless it's a GOTPCRELX relocation.
1089 if (r_type
!= elfcpp::R_X86_64_GOTPCRELX
)
1091 // We cannot convert references to IFUNC symbols, or to symbols that
1092 // are not local to the current module.
1093 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1094 || gsym
->is_undefined ()
1095 || gsym
->is_from_dynobj()
1096 || gsym
->is_preemptible())
1098 // Check for a CALLQ or JMPQ opcode.
1099 return ((*view
)[r_offset
- 2] == 0xff
1100 && ((*view
)[r_offset
- 1] == 0x15
1101 || (*view
)[r_offset
- 1] == 0x25));
1104 // Adjust TLS relocation type based on the options and whether this
1105 // is a local symbol.
1106 static tls::Tls_optimization
1107 optimize_tls_reloc(bool is_final
, int r_type
);
1109 // Get the GOT section, creating it if necessary.
1110 Output_data_got
<64, false>*
1111 got_section(Symbol_table
*, Layout
*);
1113 // Get the GOT PLT section.
1114 Output_data_got_plt_x86_64
*
1115 got_plt_section() const
1117 gold_assert(this->got_plt_
!= NULL
);
1118 return this->got_plt_
;
1121 // Get the GOT section for TLSDESC entries.
1122 Output_data_got
<64, false>*
1123 got_tlsdesc_section() const
1125 gold_assert(this->got_tlsdesc_
!= NULL
);
1126 return this->got_tlsdesc_
;
1129 // Create the PLT section.
1131 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
1133 // Create a PLT entry for a global symbol.
1135 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1137 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
1139 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1140 Sized_relobj_file
<size
, false>* relobj
,
1141 unsigned int local_sym_index
);
1143 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1145 define_tls_base_symbol(Symbol_table
*, Layout
*);
1147 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1149 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
1151 // Create a GOT entry for the TLS module index.
1153 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1154 Sized_relobj_file
<size
, false>* object
);
1156 // Get the PLT section.
1157 Output_data_plt_x86_64
<size
>*
1160 gold_assert(this->plt_
!= NULL
);
1164 // Get the dynamic reloc section, creating it if necessary.
1166 rela_dyn_section(Layout
*);
1168 // Get the section to use for TLSDESC relocations.
1170 rela_tlsdesc_section(Layout
*) const;
1172 // Get the section to use for IRELATIVE relocations.
1174 rela_irelative_section(Layout
*);
1176 // Add a potential copy relocation.
1178 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1179 Sized_relobj_file
<size
, false>* object
,
1180 unsigned int shndx
, Output_section
* output_section
,
1181 Symbol
* sym
, const elfcpp::Rela
<size
, false>& reloc
)
1183 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1184 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1185 symtab
->get_sized_symbol
<size
>(sym
),
1186 object
, shndx
, output_section
,
1187 r_type
, reloc
.get_r_offset(),
1188 reloc
.get_r_addend(),
1189 this->rela_dyn_section(layout
));
1192 // Record a target-specific program property in the .note.gnu.property
1195 record_gnu_property(int, int, size_t, const unsigned char*, const Object
*);
1197 // Merge the target-specific program properties from the current object.
1199 merge_gnu_properties(const Object
*);
1201 // Finalize the target-specific program properties and add them back to
1204 do_finalize_gnu_properties(Layout
*) const;
1206 // Information about this specific target which we pass to the
1207 // general Target structure.
1208 static const Target::Target_info x86_64_info
;
1210 // The types of GOT entries needed for this platform.
1211 // These values are exposed to the ABI in an incremental link.
1212 // Do not renumber existing values without changing the version
1213 // number of the .gnu_incremental_inputs section.
1216 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
1217 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
1218 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
1219 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
1222 // This type is used as the argument to the target specific
1223 // relocation routines. The only target specific reloc is
1224 // R_X86_64_TLSDESC against a local symbol.
1227 Tlsdesc_info(Sized_relobj_file
<size
, false>* a_object
, unsigned int a_r_sym
)
1228 : object(a_object
), r_sym(a_r_sym
)
1231 // The object in which the local symbol is defined.
1232 Sized_relobj_file
<size
, false>* object
;
1233 // The local symbol index in the object.
1238 Output_data_got
<64, false>* got_
;
1240 Output_data_plt_x86_64
<size
>* plt_
;
1241 // The GOT PLT section.
1242 Output_data_got_plt_x86_64
* got_plt_
;
1243 // The GOT section for IRELATIVE relocations.
1244 Output_data_space
* got_irelative_
;
1245 // The GOT section for TLSDESC relocations.
1246 Output_data_got
<64, false>* got_tlsdesc_
;
1247 // The _GLOBAL_OFFSET_TABLE_ symbol.
1248 Symbol
* global_offset_table_
;
1249 // The dynamic reloc section.
1250 Reloc_section
* rela_dyn_
;
1251 // The section to use for IRELATIVE relocs.
1252 Reloc_section
* rela_irelative_
;
1253 // Relocs saved to avoid a COPY reloc.
1254 Copy_relocs
<elfcpp::SHT_RELA
, size
, false> copy_relocs_
;
1255 // Offset of the GOT entry for the TLS module index.
1256 unsigned int got_mod_index_offset_
;
1257 // We handle R_X86_64_TLSDESC against a local symbol as a target
1258 // specific relocation. Here we store the object and local symbol
1259 // index for the relocation.
1260 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
1261 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1262 bool tls_base_symbol_defined_
;
1263 // Target-specific program properties, from .note.gnu.property section.
1264 // Each bit represents a specific feature.
1265 uint32_t isa_1_used_
;
1266 uint32_t isa_1_needed_
;
1267 uint32_t feature_1_
;
1268 // Target-specific properties from the current object.
1269 // These bits get ANDed into FEATURE_1_ after all properties for the object
1270 // have been processed.
1271 uint32_t object_feature_1_
;
1272 // Whether we have seen our first object, for use in initializing FEATURE_1_.
1273 bool seen_first_object_
;
1277 const Target::Target_info Target_x86_64
<64>::x86_64_info
=
1280 false, // is_big_endian
1281 elfcpp::EM_X86_64
, // machine_code
1282 false, // has_make_symbol
1283 false, // has_resolve
1284 true, // has_code_fill
1285 true, // is_default_stack_executable
1286 true, // can_icf_inline_merge_sections
1288 "/lib/ld64.so.1", // program interpreter
1289 0x400000, // default_text_segment_address
1290 0x1000, // abi_pagesize (overridable by -z max-page-size)
1291 0x1000, // common_pagesize (overridable by -z common-page-size)
1292 false, // isolate_execinstr
1294 elfcpp::SHN_UNDEF
, // small_common_shndx
1295 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1296 0, // small_common_section_flags
1297 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1298 NULL
, // attributes_section
1299 NULL
, // attributes_vendor
1300 "_start", // entry_symbol_name
1301 32, // hash_entry_size
1302 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
1306 const Target::Target_info Target_x86_64
<32>::x86_64_info
=
1309 false, // is_big_endian
1310 elfcpp::EM_X86_64
, // machine_code
1311 false, // has_make_symbol
1312 false, // has_resolve
1313 true, // has_code_fill
1314 true, // is_default_stack_executable
1315 true, // can_icf_inline_merge_sections
1317 "/libx32/ldx32.so.1", // program interpreter
1318 0x400000, // default_text_segment_address
1319 0x1000, // abi_pagesize (overridable by -z max-page-size)
1320 0x1000, // common_pagesize (overridable by -z common-page-size)
1321 false, // isolate_execinstr
1323 elfcpp::SHN_UNDEF
, // small_common_shndx
1324 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1325 0, // small_common_section_flags
1326 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1327 NULL
, // attributes_section
1328 NULL
, // attributes_vendor
1329 "_start", // entry_symbol_name
1330 32, // hash_entry_size
1331 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
1334 // This is called when a new output section is created. This is where
1335 // we handle the SHF_X86_64_LARGE.
1339 Target_x86_64
<size
>::do_new_output_section(Output_section
* os
) const
1341 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
1342 os
->set_is_large_section();
1345 // Get the GOT section, creating it if necessary.
1348 Output_data_got
<64, false>*
1349 Target_x86_64
<size
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
1351 if (this->got_
== NULL
)
1353 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1355 // When using -z now, we can treat .got.plt as a relro section.
1356 // Without -z now, it is modified after program startup by lazy
1358 bool is_got_plt_relro
= parameters
->options().now();
1359 Output_section_order got_order
= (is_got_plt_relro
1361 : ORDER_RELRO_LAST
);
1362 Output_section_order got_plt_order
= (is_got_plt_relro
1364 : ORDER_NON_RELRO_FIRST
);
1366 this->got_
= new Output_data_got
<64, false>();
1368 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1370 | elfcpp::SHF_WRITE
),
1371 this->got_
, got_order
, true);
1373 this->got_plt_
= new Output_data_got_plt_x86_64(layout
);
1374 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1376 | elfcpp::SHF_WRITE
),
1377 this->got_plt_
, got_plt_order
,
1380 // The first three entries are reserved.
1381 this->got_plt_
->set_current_data_size(3 * 8);
1383 if (!is_got_plt_relro
)
1385 // Those bytes can go into the relro segment.
1386 layout
->increase_relro(3 * 8);
1389 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1390 this->global_offset_table_
=
1391 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1392 Symbol_table::PREDEFINED
,
1394 0, 0, elfcpp::STT_OBJECT
,
1396 elfcpp::STV_HIDDEN
, 0,
1399 // If there are any IRELATIVE relocations, they get GOT entries
1400 // in .got.plt after the jump slot entries.
1401 this->got_irelative_
= new Output_data_space(8, "** GOT IRELATIVE PLT");
1402 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1404 | elfcpp::SHF_WRITE
),
1405 this->got_irelative_
,
1406 got_plt_order
, is_got_plt_relro
);
1408 // If there are any TLSDESC relocations, they get GOT entries in
1409 // .got.plt after the jump slot and IRELATIVE entries.
1410 this->got_tlsdesc_
= new Output_data_got
<64, false>();
1411 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1413 | elfcpp::SHF_WRITE
),
1415 got_plt_order
, is_got_plt_relro
);
1421 // Get the dynamic reloc section, creating it if necessary.
1424 typename Target_x86_64
<size
>::Reloc_section
*
1425 Target_x86_64
<size
>::rela_dyn_section(Layout
* layout
)
1427 if (this->rela_dyn_
== NULL
)
1429 gold_assert(layout
!= NULL
);
1430 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1431 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1432 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1433 ORDER_DYNAMIC_RELOCS
, false);
1435 return this->rela_dyn_
;
1438 // Get the section to use for IRELATIVE relocs, creating it if
1439 // necessary. These go in .rela.dyn, but only after all other dynamic
1440 // relocations. They need to follow the other dynamic relocations so
1441 // that they can refer to global variables initialized by those
1445 typename Target_x86_64
<size
>::Reloc_section
*
1446 Target_x86_64
<size
>::rela_irelative_section(Layout
* layout
)
1448 if (this->rela_irelative_
== NULL
)
1450 // Make sure we have already created the dynamic reloc section.
1451 this->rela_dyn_section(layout
);
1452 this->rela_irelative_
= new Reloc_section(false);
1453 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1454 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
1455 ORDER_DYNAMIC_RELOCS
, false);
1456 gold_assert(this->rela_dyn_
->output_section()
1457 == this->rela_irelative_
->output_section());
1459 return this->rela_irelative_
;
1462 // Record a target-specific program property from the .note.gnu.property
1466 Target_x86_64
<size
>::record_gnu_property(
1468 size_t pr_datasz
, const unsigned char* pr_data
,
1469 const Object
* object
)
1475 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED
:
1476 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED
:
1477 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND
:
1480 gold_warning(_("%s: corrupt .note.gnu.property section "
1481 "(pr_datasz for property %d is not 4)"),
1482 object
->name().c_str(), pr_type
);
1485 val
= elfcpp::Swap
<32, false>::readval(pr_data
);
1488 gold_warning(_("%s: unknown program property type 0x%x "
1489 "in .note.gnu.property section"),
1490 object
->name().c_str(), pr_type
);
1496 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED
:
1497 this->isa_1_used_
|= val
;
1499 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED
:
1500 this->isa_1_needed_
|= val
;
1502 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND
:
1503 // If we see multiple feature props in one object, OR them together.
1504 this->object_feature_1_
|= val
;
1509 // Merge the target-specific program properties from the current object.
1512 Target_x86_64
<size
>::merge_gnu_properties(const Object
*)
1514 if (this->seen_first_object_
)
1515 this->feature_1_
&= this->object_feature_1_
;
1518 this->feature_1_
= this->object_feature_1_
;
1519 this->seen_first_object_
= true;
1521 this->object_feature_1_
= 0;
1525 add_property(Layout
* layout
, unsigned int pr_type
, uint32_t val
)
1527 unsigned char buf
[4];
1528 elfcpp::Swap
<32, false>::writeval(buf
, val
);
1529 layout
->add_gnu_property(elfcpp::NT_GNU_PROPERTY_TYPE_0
, pr_type
, 4, buf
);
1532 // Finalize the target-specific program properties and add them back to
1536 Target_x86_64
<size
>::do_finalize_gnu_properties(Layout
* layout
) const
1538 if (this->isa_1_used_
!= 0)
1539 add_property(layout
, elfcpp::GNU_PROPERTY_X86_ISA_1_USED
,
1541 if (this->isa_1_needed_
!= 0)
1542 add_property(layout
, elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED
,
1543 this->isa_1_needed_
);
1544 if (this->feature_1_
!= 0)
1545 add_property(layout
, elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND
,
1549 // Write the first three reserved words of the .got.plt section.
1550 // The remainder of the section is written while writing the PLT
1551 // in Output_data_plt_i386::do_write.
1554 Output_data_got_plt_x86_64::do_write(Output_file
* of
)
1556 // The first entry in the GOT is the address of the .dynamic section
1557 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1558 // We saved space for them when we created the section in
1559 // Target_x86_64::got_section.
1560 const off_t got_file_offset
= this->offset();
1561 gold_assert(this->data_size() >= 24);
1562 unsigned char* const got_view
= of
->get_output_view(got_file_offset
, 24);
1563 Output_section
* dynamic
= this->layout_
->dynamic_section();
1564 uint64_t dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
1565 elfcpp::Swap
<64, false>::writeval(got_view
, dynamic_addr
);
1566 memset(got_view
+ 8, 0, 16);
1567 of
->write_output_view(got_file_offset
, 24, got_view
);
1570 // Initialize the PLT section.
1574 Output_data_plt_x86_64
<size
>::init(Layout
* layout
)
1576 this->rel_
= new Reloc_section(false);
1577 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1578 elfcpp::SHF_ALLOC
, this->rel_
,
1579 ORDER_DYNAMIC_PLT_RELOCS
, false);
1584 Output_data_plt_x86_64
<size
>::do_adjust_output_section(Output_section
* os
)
1586 os
->set_entsize(this->get_plt_entry_size());
1589 // Add an entry to the PLT.
1593 Output_data_plt_x86_64
<size
>::add_entry(Symbol_table
* symtab
, Layout
* layout
,
1596 gold_assert(!gsym
->has_plt_offset());
1598 unsigned int plt_index
;
1600 section_offset_type got_offset
;
1602 unsigned int* pcount
;
1603 unsigned int offset
;
1604 unsigned int reserved
;
1605 Output_section_data_build
* got
;
1606 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1607 && gsym
->can_use_relative_reloc(false))
1609 pcount
= &this->irelative_count_
;
1612 got
= this->got_irelative_
;
1616 pcount
= &this->count_
;
1619 got
= this->got_plt_
;
1622 if (!this->is_data_size_valid())
1624 // Note that when setting the PLT offset for a non-IRELATIVE
1625 // entry we skip the initial reserved PLT entry.
1626 plt_index
= *pcount
+ offset
;
1627 plt_offset
= plt_index
* this->get_plt_entry_size();
1631 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1632 gold_assert(got_offset
== got
->current_data_size());
1634 // Every PLT entry needs a GOT entry which points back to the PLT
1635 // entry (this will be changed by the dynamic linker, normally
1636 // lazily when the function is called).
1637 got
->set_current_data_size(got_offset
+ 8);
1641 // FIXME: This is probably not correct for IRELATIVE relocs.
1643 // For incremental updates, find an available slot.
1644 plt_offset
= this->free_list_
.allocate(this->get_plt_entry_size(),
1645 this->get_plt_entry_size(), 0);
1646 if (plt_offset
== -1)
1647 gold_fallback(_("out of patch space (PLT);"
1648 " relink with --incremental-full"));
1650 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1651 // can be calculated from the PLT index, adjusting for the three
1652 // reserved entries at the beginning of the GOT.
1653 plt_index
= plt_offset
/ this->get_plt_entry_size() - 1;
1654 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1657 gsym
->set_plt_offset(plt_offset
);
1659 // Every PLT entry needs a reloc.
1660 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
1662 // Note that we don't need to save the symbol. The contents of the
1663 // PLT are independent of which symbols are used. The symbols only
1664 // appear in the relocations.
1667 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1672 Output_data_plt_x86_64
<size
>::add_local_ifunc_entry(
1673 Symbol_table
* symtab
,
1675 Sized_relobj_file
<size
, false>* relobj
,
1676 unsigned int local_sym_index
)
1678 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
1679 ++this->irelative_count_
;
1681 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
1683 // Every PLT entry needs a GOT entry which points back to the PLT
1685 this->got_irelative_
->set_current_data_size(got_offset
+ 8);
1687 // Every PLT entry needs a reloc.
1688 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1689 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
1690 elfcpp::R_X86_64_IRELATIVE
,
1691 this->got_irelative_
, got_offset
, 0);
1696 // Add the relocation for a PLT entry.
1700 Output_data_plt_x86_64
<size
>::add_relocation(Symbol_table
* symtab
,
1703 unsigned int got_offset
)
1705 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1706 && gsym
->can_use_relative_reloc(false))
1708 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1709 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
1710 this->got_irelative_
, got_offset
, 0);
1714 gsym
->set_needs_dynsym_entry();
1715 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
1720 // Return where the TLSDESC relocations should go, creating it if
1721 // necessary. These follow the JUMP_SLOT relocations.
1724 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1725 Output_data_plt_x86_64
<size
>::rela_tlsdesc(Layout
* layout
)
1727 if (this->tlsdesc_rel_
== NULL
)
1729 this->tlsdesc_rel_
= new Reloc_section(false);
1730 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1731 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
1732 ORDER_DYNAMIC_PLT_RELOCS
, false);
1733 gold_assert(this->tlsdesc_rel_
->output_section()
1734 == this->rel_
->output_section());
1736 return this->tlsdesc_rel_
;
1739 // Return where the IRELATIVE relocations should go in the PLT. These
1740 // follow the JUMP_SLOT and the TLSDESC relocations.
1743 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1744 Output_data_plt_x86_64
<size
>::rela_irelative(Symbol_table
* symtab
,
1747 if (this->irelative_rel_
== NULL
)
1749 // Make sure we have a place for the TLSDESC relocations, in
1750 // case we see any later on.
1751 this->rela_tlsdesc(layout
);
1752 this->irelative_rel_
= new Reloc_section(false);
1753 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1754 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
1755 ORDER_DYNAMIC_PLT_RELOCS
, false);
1756 gold_assert(this->irelative_rel_
->output_section()
1757 == this->rel_
->output_section());
1759 if (parameters
->doing_static_link())
1761 // A statically linked executable will only have a .rela.plt
1762 // section to hold R_X86_64_IRELATIVE relocs for
1763 // STT_GNU_IFUNC symbols. The library will use these
1764 // symbols to locate the IRELATIVE relocs at program startup
1766 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
1767 Symbol_table::PREDEFINED
,
1768 this->irelative_rel_
, 0, 0,
1769 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1770 elfcpp::STV_HIDDEN
, 0, false, true);
1771 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
1772 Symbol_table::PREDEFINED
,
1773 this->irelative_rel_
, 0, 0,
1774 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1775 elfcpp::STV_HIDDEN
, 0, true, true);
1778 return this->irelative_rel_
;
1781 // Return the PLT address to use for a global symbol.
1785 Output_data_plt_x86_64
<size
>::do_address_for_global(const Symbol
* gsym
)
1787 uint64_t offset
= 0;
1788 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1789 && gsym
->can_use_relative_reloc(false))
1790 offset
= (this->count_
+ 1) * this->get_plt_entry_size();
1791 return this->address() + offset
+ gsym
->plt_offset();
1794 // Return the PLT address to use for a local symbol. These are always
1795 // IRELATIVE relocs.
1799 Output_data_plt_x86_64
<size
>::do_address_for_local(const Relobj
* object
,
1802 return (this->address()
1803 + (this->count_
+ 1) * this->get_plt_entry_size()
1804 + object
->local_plt_offset(r_sym
));
1807 // Set the final size.
1810 Output_data_plt_x86_64
<size
>::set_final_data_size()
1812 // Number of regular and IFUNC PLT entries, plus the first entry.
1813 unsigned int count
= this->count_
+ this->irelative_count_
+ 1;
1814 // Count the TLSDESC entry, if present.
1815 if (this->has_tlsdesc_entry())
1817 this->set_data_size(count
* this->get_plt_entry_size());
1820 // The first entry in the PLT for an executable.
1824 Output_data_plt_x86_64_standard
<size
>::first_plt_entry
[plt_entry_size
] =
1826 // From AMD64 ABI Draft 0.98, page 76
1827 0xff, 0x35, // pushq contents of memory address
1828 0, 0, 0, 0, // replaced with address of .got + 8
1829 0xff, 0x25, // jmp indirect
1830 0, 0, 0, 0, // replaced with address of .got + 16
1831 0x90, 0x90, 0x90, 0x90 // noop (x4)
1836 Output_data_plt_x86_64_standard
<size
>::do_fill_first_plt_entry(
1838 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1839 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
1841 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1842 // We do a jmp relative to the PC at the end of this instruction.
1843 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1845 - (plt_address
+ 6)));
1846 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1848 - (plt_address
+ 12)));
1851 // Subsequent entries in the PLT for an executable.
1855 Output_data_plt_x86_64_standard
<size
>::plt_entry
[plt_entry_size
] =
1857 // From AMD64 ABI Draft 0.98, page 76
1858 0xff, 0x25, // jmpq indirect
1859 0, 0, 0, 0, // replaced with address of symbol in .got
1860 0x68, // pushq immediate
1861 0, 0, 0, 0, // replaced with offset into relocation table
1862 0xe9, // jmpq relative
1863 0, 0, 0, 0 // replaced with offset to start of .plt
1868 Output_data_plt_x86_64_standard
<size
>::do_fill_plt_entry(
1870 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1871 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1872 unsigned int got_offset
,
1873 unsigned int plt_offset
,
1874 unsigned int plt_index
)
1876 // Check PC-relative offset overflow in PLT entry.
1877 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
1878 - (plt_address
+ plt_offset
+ 6));
1879 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
1880 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1883 memcpy(pov
, plt_entry
, plt_entry_size
);
1884 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1885 plt_got_pcrel_offset
);
1887 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1888 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1889 - (plt_offset
+ plt_entry_size
));
1894 // The reserved TLSDESC entry in the PLT for an executable.
1898 Output_data_plt_x86_64_standard
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
1900 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1901 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1902 0xff, 0x35, // pushq x(%rip)
1903 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1904 0xff, 0x25, // jmpq *y(%rip)
1905 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1912 Output_data_plt_x86_64_standard
<size
>::do_fill_tlsdesc_entry(
1914 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1915 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1916 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
1917 unsigned int tlsdesc_got_offset
,
1918 unsigned int plt_offset
)
1920 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1921 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1923 - (plt_address
+ plt_offset
1925 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1927 + tlsdesc_got_offset
1928 - (plt_address
+ plt_offset
1932 // Return the APLT address to use for a global symbol (for -z bndplt).
1935 Output_data_plt_x86_64_bnd::do_address_for_global(const Symbol
* gsym
)
1937 uint64_t offset
= this->aplt_offset_
;
1938 // Convert the PLT offset into an APLT offset.
1939 unsigned int plt_offset
= gsym
->plt_offset();
1940 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1941 && gsym
->can_use_relative_reloc(false))
1942 offset
+= this->regular_count() * aplt_entry_size
;
1944 plt_offset
-= plt_entry_size
;
1945 plt_offset
= plt_offset
/ (plt_entry_size
/ aplt_entry_size
);
1946 return this->address() + offset
+ plt_offset
;
1949 // Return the PLT address to use for a local symbol. These are always
1950 // IRELATIVE relocs.
1953 Output_data_plt_x86_64_bnd::do_address_for_local(const Relobj
* object
,
1956 // Convert the PLT offset into an APLT offset.
1957 unsigned int plt_offset
= ((object
->local_plt_offset(r_sym
) - plt_entry_size
)
1958 / (plt_entry_size
/ aplt_entry_size
));
1959 return (this->address()
1960 + this->aplt_offset_
1961 + this->regular_count() * aplt_entry_size
1965 // Set the final size.
1967 Output_data_plt_x86_64_bnd::set_final_data_size()
1969 // Number of regular and IFUNC PLT entries.
1970 unsigned int count
= this->entry_count();
1971 // Count the first entry and the TLSDESC entry, if present.
1972 unsigned int extra
= this->has_tlsdesc_entry() ? 2 : 1;
1973 unsigned int plt_size
= (count
+ extra
) * plt_entry_size
;
1974 // Offset of the APLT.
1975 this->aplt_offset_
= plt_size
;
1976 // Size of the APLT.
1977 plt_size
+= count
* aplt_entry_size
;
1978 this->set_data_size(plt_size
);
1981 // The first entry in the BND PLT.
1984 Output_data_plt_x86_64_bnd::first_plt_entry
[plt_entry_size
] =
1986 // From AMD64 ABI Draft 0.98, page 76
1987 0xff, 0x35, // pushq contents of memory address
1988 0, 0, 0, 0, // replaced with address of .got + 8
1989 0xf2, 0xff, 0x25, // bnd jmp indirect
1990 0, 0, 0, 0, // replaced with address of .got + 16
1991 0x0f, 0x1f, 0x00 // nop
1995 Output_data_plt_x86_64_bnd::do_fill_first_plt_entry(
1997 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
1998 elfcpp::Elf_types
<64>::Elf_Addr plt_address
)
2000 memcpy(pov
, first_plt_entry
, plt_entry_size
);
2001 // We do a jmp relative to the PC at the end of this instruction.
2002 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2004 - (plt_address
+ 6)));
2005 elfcpp::Swap
<32, false>::writeval(pov
+ 9,
2007 - (plt_address
+ 13)));
2010 // Subsequent entries in the BND PLT.
2013 Output_data_plt_x86_64_bnd::plt_entry
[plt_entry_size
] =
2015 // From AMD64 ABI Draft 0.99.8, page 139
2016 0x68, // pushq immediate
2017 0, 0, 0, 0, // replaced with offset into relocation table
2018 0xf2, 0xe9, // bnd jmpq relative
2019 0, 0, 0, 0, // replaced with offset to start of .plt
2020 0x0f, 0x1f, 0x44, 0, 0 // nop
2023 // Entries in the BND Additional PLT.
2026 Output_data_plt_x86_64_bnd::aplt_entry
[aplt_entry_size
] =
2028 // From AMD64 ABI Draft 0.99.8, page 139
2029 0xf2, 0xff, 0x25, // bnd jmpq indirect
2030 0, 0, 0, 0, // replaced with address of symbol in .got
2035 Output_data_plt_x86_64_bnd::do_fill_plt_entry(
2037 elfcpp::Elf_types
<64>::Elf_Addr
,
2038 elfcpp::Elf_types
<64>::Elf_Addr
,
2040 unsigned int plt_offset
,
2041 unsigned int plt_index
)
2043 memcpy(pov
, plt_entry
, plt_entry_size
);
2044 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 1, plt_index
);
2045 elfcpp::Swap
<32, false>::writeval(pov
+ 7, -(plt_offset
+ 11));
2050 Output_data_plt_x86_64_bnd::fill_aplt_entry(
2052 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
2053 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
2054 unsigned int got_offset
,
2055 unsigned int plt_offset
,
2056 unsigned int plt_index
)
2058 // Check PC-relative offset overflow in PLT entry.
2059 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
2060 - (plt_address
+ plt_offset
+ 7));
2061 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
2062 gold_error(_("PC-relative offset overflow in APLT entry %d"),
2065 memcpy(pov
, aplt_entry
, aplt_entry_size
);
2066 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3, plt_got_pcrel_offset
);
2069 // The reserved TLSDESC entry in the PLT for an executable.
2072 Output_data_plt_x86_64_bnd::tlsdesc_plt_entry
[plt_entry_size
] =
2074 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2075 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2076 0xff, 0x35, // pushq x(%rip)
2077 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2078 0xf2, 0xff, 0x25, // jmpq *y(%rip)
2079 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2080 0x0f, 0x1f, 0 // nop
2084 Output_data_plt_x86_64_bnd::do_fill_tlsdesc_entry(
2086 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
2087 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
2088 elfcpp::Elf_types
<64>::Elf_Addr got_base
,
2089 unsigned int tlsdesc_got_offset
,
2090 unsigned int plt_offset
)
2092 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
2093 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2095 - (plt_address
+ plt_offset
2097 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
2099 + tlsdesc_got_offset
2100 - (plt_address
+ plt_offset
2104 // The .eh_frame unwind information for the PLT.
2108 Output_data_plt_x86_64
<size
>::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
2111 'z', // Augmentation: augmentation size included.
2112 'R', // Augmentation: FDE encoding included.
2113 '\0', // End of augmentation string.
2114 1, // Code alignment factor.
2115 0x78, // Data alignment factor.
2116 16, // Return address column.
2117 1, // Augmentation size.
2118 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
2119 | elfcpp::DW_EH_PE_sdata4
),
2120 elfcpp::DW_CFA_def_cfa
, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
2121 elfcpp::DW_CFA_offset
+ 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
2122 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
2128 Output_data_plt_x86_64_standard
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
2130 0, 0, 0, 0, // Replaced with offset to .plt.
2131 0, 0, 0, 0, // Replaced with size of .plt.
2132 0, // Augmentation size.
2133 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
2134 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
2135 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
2136 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
2137 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
2138 11, // Block length.
2139 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
2140 elfcpp::DW_OP_breg16
, 0, // Push %rip.
2141 elfcpp::DW_OP_lit15
, // Push 0xf.
2142 elfcpp::DW_OP_and
, // & (%rip & 0xf).
2143 elfcpp::DW_OP_lit11
, // Push 0xb.
2144 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 0xb)
2145 elfcpp::DW_OP_lit3
, // Push 3.
2146 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 0xb) << 3)
2147 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
2148 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
2154 // The .eh_frame unwind information for the BND PLT.
2156 Output_data_plt_x86_64_bnd::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
2158 0, 0, 0, 0, // Replaced with offset to .plt.
2159 0, 0, 0, 0, // Replaced with size of .plt.
2160 0, // Augmentation size.
2161 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
2162 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
2163 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
2164 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
2165 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
2166 11, // Block length.
2167 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
2168 elfcpp::DW_OP_breg16
, 0, // Push %rip.
2169 elfcpp::DW_OP_lit15
, // Push 0xf.
2170 elfcpp::DW_OP_and
, // & (%rip & 0xf).
2171 elfcpp::DW_OP_lit5
, // Push 5.
2172 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 5)
2173 elfcpp::DW_OP_lit3
, // Push 3.
2174 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 5) << 3)
2175 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=5)<<3)+%rsp+8
2176 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
2182 // Write out the PLT. This uses the hand-coded instructions above,
2183 // and adjusts them as needed. This is specified by the AMD64 ABI.
2187 Output_data_plt_x86_64
<size
>::do_write(Output_file
* of
)
2189 const off_t offset
= this->offset();
2190 const section_size_type oview_size
=
2191 convert_to_section_size_type(this->data_size());
2192 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2194 const off_t got_file_offset
= this->got_plt_
->offset();
2195 gold_assert(parameters
->incremental_update()
2196 || (got_file_offset
+ this->got_plt_
->data_size()
2197 == this->got_irelative_
->offset()));
2198 const section_size_type got_size
=
2199 convert_to_section_size_type(this->got_plt_
->data_size()
2200 + this->got_irelative_
->data_size());
2201 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
2204 unsigned char* pov
= oview
;
2206 // The base address of the .plt section.
2207 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
2208 // The base address of the .got section.
2209 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
= this->got_
->address();
2210 // The base address of the PLT portion of the .got section,
2211 // which is where the GOT pointer will point, and where the
2212 // three reserved GOT entries are located.
2213 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
2214 = this->got_plt_
->address();
2216 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
2217 pov
+= this->get_plt_entry_size();
2219 // The first three entries in the GOT are reserved, and are written
2220 // by Output_data_got_plt_x86_64::do_write.
2221 unsigned char* got_pov
= got_view
+ 24;
2223 unsigned int plt_offset
= this->get_plt_entry_size();
2224 unsigned int got_offset
= 24;
2225 const unsigned int count
= this->count_
+ this->irelative_count_
;
2226 for (unsigned int plt_index
= 0;
2229 pov
+= this->get_plt_entry_size(),
2231 plt_offset
+= this->get_plt_entry_size(),
2234 // Set and adjust the PLT entry itself.
2235 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
2236 got_address
, plt_address
,
2237 got_offset
, plt_offset
,
2240 // Set the entry in the GOT.
2241 elfcpp::Swap
<64, false>::writeval(got_pov
,
2242 plt_address
+ plt_offset
+ lazy_offset
);
2245 if (this->has_tlsdesc_entry())
2247 // Set and adjust the reserved TLSDESC PLT entry.
2248 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
2249 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
2250 tlsdesc_got_offset
, plt_offset
);
2251 pov
+= this->get_plt_entry_size();
2254 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
2255 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
2257 of
->write_output_view(offset
, oview_size
, oview
);
2258 of
->write_output_view(got_file_offset
, got_size
, got_view
);
2261 // Write out the BND PLT.
2264 Output_data_plt_x86_64_bnd::do_write(Output_file
* of
)
2266 const off_t offset
= this->offset();
2267 const section_size_type oview_size
=
2268 convert_to_section_size_type(this->data_size());
2269 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2271 Output_data_got
<64, false>* got
= this->got();
2272 Output_data_got_plt_x86_64
* got_plt
= this->got_plt();
2273 Output_data_space
* got_irelative
= this->got_irelative();
2275 const off_t got_file_offset
= got_plt
->offset();
2276 gold_assert(parameters
->incremental_update()
2277 || (got_file_offset
+ got_plt
->data_size()
2278 == got_irelative
->offset()));
2279 const section_size_type got_size
=
2280 convert_to_section_size_type(got_plt
->data_size()
2281 + got_irelative
->data_size());
2282 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
2285 unsigned char* pov
= oview
;
2287 // The base address of the .plt section.
2288 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
2289 // The base address of the .got section.
2290 elfcpp::Elf_types
<64>::Elf_Addr got_base
= got
->address();
2291 // The base address of the PLT portion of the .got section,
2292 // which is where the GOT pointer will point, and where the
2293 // three reserved GOT entries are located.
2294 elfcpp::Elf_types
<64>::Elf_Addr got_address
= got_plt
->address();
2296 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
2297 pov
+= plt_entry_size
;
2299 // The first three entries in the GOT are reserved, and are written
2300 // by Output_data_got_plt_x86_64::do_write.
2301 unsigned char* got_pov
= got_view
+ 24;
2303 unsigned int plt_offset
= plt_entry_size
;
2304 unsigned int got_offset
= 24;
2305 const unsigned int count
= this->entry_count();
2306 for (unsigned int plt_index
= 0;
2309 pov
+= plt_entry_size
,
2311 plt_offset
+= plt_entry_size
,
2314 // Set and adjust the PLT entry itself.
2315 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
2316 got_address
, plt_address
,
2317 got_offset
, plt_offset
,
2320 // Set the entry in the GOT.
2321 elfcpp::Swap
<64, false>::writeval(got_pov
,
2322 plt_address
+ plt_offset
+ lazy_offset
);
2325 if (this->has_tlsdesc_entry())
2327 // Set and adjust the reserved TLSDESC PLT entry.
2328 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
2329 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
2330 tlsdesc_got_offset
, plt_offset
);
2331 pov
+= this->get_plt_entry_size();
2334 // Write the additional PLT.
2336 for (unsigned int plt_index
= 0;
2339 pov
+= aplt_entry_size
,
2340 plt_offset
+= aplt_entry_size
,
2343 // Set and adjust the PLT entry itself.
2344 this->fill_aplt_entry(pov
, got_address
, plt_address
, got_offset
,
2345 plt_offset
, plt_index
);
2348 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
2349 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
2351 of
->write_output_view(offset
, oview_size
, oview
);
2352 of
->write_output_view(got_file_offset
, got_size
, got_view
);
2355 // Create the PLT section.
2359 Target_x86_64
<size
>::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
2361 if (this->plt_
== NULL
)
2363 // Create the GOT sections first.
2364 this->got_section(symtab
, layout
);
2366 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
2367 this->got_irelative_
);
2369 // Add unwind information if requested.
2370 if (parameters
->options().ld_generated_unwind_info())
2371 this->plt_
->add_eh_frame(layout
);
2373 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
2375 | elfcpp::SHF_EXECINSTR
),
2376 this->plt_
, ORDER_PLT
, false);
2378 // Make the sh_info field of .rela.plt point to .plt.
2379 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
2380 rela_plt_os
->set_info_section(this->plt_
->output_section());
2385 Output_data_plt_x86_64
<32>*
2386 Target_x86_64
<32>::do_make_data_plt(Layout
* layout
,
2387 Output_data_got
<64, false>* got
,
2388 Output_data_got_plt_x86_64
* got_plt
,
2389 Output_data_space
* got_irelative
)
2391 return new Output_data_plt_x86_64_standard
<32>(layout
, got
, got_plt
,
2396 Output_data_plt_x86_64
<64>*
2397 Target_x86_64
<64>::do_make_data_plt(Layout
* layout
,
2398 Output_data_got
<64, false>* got
,
2399 Output_data_got_plt_x86_64
* got_plt
,
2400 Output_data_space
* got_irelative
)
2402 if (parameters
->options().bndplt())
2403 return new Output_data_plt_x86_64_bnd(layout
, got
, got_plt
,
2406 return new Output_data_plt_x86_64_standard
<64>(layout
, got
, got_plt
,
2411 Output_data_plt_x86_64
<32>*
2412 Target_x86_64
<32>::do_make_data_plt(Layout
* layout
,
2413 Output_data_got
<64, false>* got
,
2414 Output_data_got_plt_x86_64
* got_plt
,
2415 Output_data_space
* got_irelative
,
2416 unsigned int plt_count
)
2418 return new Output_data_plt_x86_64_standard
<32>(layout
, got
, got_plt
,
2424 Output_data_plt_x86_64
<64>*
2425 Target_x86_64
<64>::do_make_data_plt(Layout
* layout
,
2426 Output_data_got
<64, false>* got
,
2427 Output_data_got_plt_x86_64
* got_plt
,
2428 Output_data_space
* got_irelative
,
2429 unsigned int plt_count
)
2431 if (parameters
->options().bndplt())
2432 return new Output_data_plt_x86_64_bnd(layout
, got
, got_plt
,
2433 got_irelative
, plt_count
);
2435 return new Output_data_plt_x86_64_standard
<64>(layout
, got
, got_plt
,
2440 // Return the section for TLSDESC relocations.
2443 typename Target_x86_64
<size
>::Reloc_section
*
2444 Target_x86_64
<size
>::rela_tlsdesc_section(Layout
* layout
) const
2446 return this->plt_section()->rela_tlsdesc(layout
);
2449 // Create a PLT entry for a global symbol.
2453 Target_x86_64
<size
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
2456 if (gsym
->has_plt_offset())
2459 if (this->plt_
== NULL
)
2460 this->make_plt_section(symtab
, layout
);
2462 this->plt_
->add_entry(symtab
, layout
, gsym
);
2465 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2469 Target_x86_64
<size
>::make_local_ifunc_plt_entry(
2470 Symbol_table
* symtab
, Layout
* layout
,
2471 Sized_relobj_file
<size
, false>* relobj
,
2472 unsigned int local_sym_index
)
2474 if (relobj
->local_has_plt_offset(local_sym_index
))
2476 if (this->plt_
== NULL
)
2477 this->make_plt_section(symtab
, layout
);
2478 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
2481 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
2484 // Return the number of entries in the PLT.
2488 Target_x86_64
<size
>::plt_entry_count() const
2490 if (this->plt_
== NULL
)
2492 return this->plt_
->entry_count();
2495 // Return the offset of the first non-reserved PLT entry.
2499 Target_x86_64
<size
>::first_plt_entry_offset() const
2501 if (this->plt_
== NULL
)
2503 return this->plt_
->first_plt_entry_offset();
2506 // Return the size of each PLT entry.
2510 Target_x86_64
<size
>::plt_entry_size() const
2512 if (this->plt_
== NULL
)
2514 return this->plt_
->get_plt_entry_size();
2517 // Create the GOT and PLT sections for an incremental update.
2520 Output_data_got_base
*
2521 Target_x86_64
<size
>::init_got_plt_for_update(Symbol_table
* symtab
,
2523 unsigned int got_count
,
2524 unsigned int plt_count
)
2526 gold_assert(this->got_
== NULL
);
2528 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
2529 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2531 | elfcpp::SHF_WRITE
),
2532 this->got_
, ORDER_RELRO_LAST
,
2535 // Add the three reserved entries.
2536 this->got_plt_
= new Output_data_got_plt_x86_64(layout
, (plt_count
+ 3) * 8);
2537 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
2539 | elfcpp::SHF_WRITE
),
2540 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
2543 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2544 this->global_offset_table_
=
2545 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2546 Symbol_table::PREDEFINED
,
2548 0, 0, elfcpp::STT_OBJECT
,
2550 elfcpp::STV_HIDDEN
, 0,
2553 // If there are any TLSDESC relocations, they get GOT entries in
2554 // .got.plt after the jump slot entries.
2555 // FIXME: Get the count for TLSDESC entries.
2556 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
2557 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
2558 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2560 ORDER_NON_RELRO_FIRST
, false);
2562 // If there are any IRELATIVE relocations, they get GOT entries in
2563 // .got.plt after the jump slot and TLSDESC entries.
2564 this->got_irelative_
= new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
2565 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
2566 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2567 this->got_irelative_
,
2568 ORDER_NON_RELRO_FIRST
, false);
2570 // Create the PLT section.
2571 this->plt_
= this->make_data_plt(layout
, this->got_
,
2573 this->got_irelative_
,
2576 // Add unwind information if requested.
2577 if (parameters
->options().ld_generated_unwind_info())
2578 this->plt_
->add_eh_frame(layout
);
2580 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
2581 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
2582 this->plt_
, ORDER_PLT
, false);
2584 // Make the sh_info field of .rela.plt point to .plt.
2585 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
2586 rela_plt_os
->set_info_section(this->plt_
->output_section());
2588 // Create the rela_dyn section.
2589 this->rela_dyn_section(layout
);
2594 // Reserve a GOT entry for a local symbol, and regenerate any
2595 // necessary dynamic relocations.
2599 Target_x86_64
<size
>::reserve_local_got_entry(
2600 unsigned int got_index
,
2601 Sized_relobj
<size
, false>* obj
,
2603 unsigned int got_type
)
2605 unsigned int got_offset
= got_index
* 8;
2606 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
2608 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
2611 case GOT_TYPE_STANDARD
:
2612 if (parameters
->options().output_is_position_independent())
2613 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
2614 this->got_
, got_offset
, 0, false);
2616 case GOT_TYPE_TLS_OFFSET
:
2617 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
2618 this->got_
, got_offset
, 0);
2620 case GOT_TYPE_TLS_PAIR
:
2621 this->got_
->reserve_slot(got_index
+ 1);
2622 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
2623 this->got_
, got_offset
, 0);
2625 case GOT_TYPE_TLS_DESC
:
2626 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
2627 // this->got_->reserve_slot(got_index + 1);
2628 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2629 // this->got_, got_offset, 0);
2636 // Reserve a GOT entry for a global symbol, and regenerate any
2637 // necessary dynamic relocations.
2641 Target_x86_64
<size
>::reserve_global_got_entry(unsigned int got_index
,
2643 unsigned int got_type
)
2645 unsigned int got_offset
= got_index
* 8;
2646 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
2648 this->got_
->reserve_global(got_index
, gsym
, got_type
);
2651 case GOT_TYPE_STANDARD
:
2652 if (!gsym
->final_value_is_known())
2654 if (gsym
->is_from_dynobj()
2655 || gsym
->is_undefined()
2656 || gsym
->is_preemptible()
2657 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
2658 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
2659 this->got_
, got_offset
, 0);
2661 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2662 this->got_
, got_offset
, 0, false);
2665 case GOT_TYPE_TLS_OFFSET
:
2666 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
2667 this->got_
, got_offset
, 0, false);
2669 case GOT_TYPE_TLS_PAIR
:
2670 this->got_
->reserve_slot(got_index
+ 1);
2671 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
2672 this->got_
, got_offset
, 0, false);
2673 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
2674 this->got_
, got_offset
+ 8, 0, false);
2676 case GOT_TYPE_TLS_DESC
:
2677 this->got_
->reserve_slot(got_index
+ 1);
2678 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
2679 this->got_
, got_offset
, 0, false);
2686 // Register an existing PLT entry for a global symbol.
2690 Target_x86_64
<size
>::register_global_plt_entry(Symbol_table
* symtab
,
2692 unsigned int plt_index
,
2695 gold_assert(this->plt_
!= NULL
);
2696 gold_assert(!gsym
->has_plt_offset());
2698 this->plt_
->reserve_slot(plt_index
);
2700 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
2702 unsigned int got_offset
= (plt_index
+ 3) * 8;
2703 this->plt_
->add_relocation(symtab
, layout
, gsym
, got_offset
);
2706 // Force a COPY relocation for a given symbol.
2710 Target_x86_64
<size
>::emit_copy_reloc(
2711 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
2713 this->copy_relocs_
.emit_copy_reloc(symtab
,
2714 symtab
->get_sized_symbol
<size
>(sym
),
2717 this->rela_dyn_section(NULL
));
2720 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2724 Target_x86_64
<size
>::define_tls_base_symbol(Symbol_table
* symtab
,
2727 if (this->tls_base_symbol_defined_
)
2730 Output_segment
* tls_segment
= layout
->tls_segment();
2731 if (tls_segment
!= NULL
)
2733 bool is_exec
= parameters
->options().output_is_executable();
2734 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
2735 Symbol_table::PREDEFINED
,
2739 elfcpp::STV_HIDDEN
, 0,
2741 ? Symbol::SEGMENT_END
2742 : Symbol::SEGMENT_START
),
2745 this->tls_base_symbol_defined_
= true;
2748 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2752 Target_x86_64
<size
>::reserve_tlsdesc_entries(Symbol_table
* symtab
,
2755 if (this->plt_
== NULL
)
2756 this->make_plt_section(symtab
, layout
);
2758 if (!this->plt_
->has_tlsdesc_entry())
2760 // Allocate the TLSDESC_GOT entry.
2761 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2762 unsigned int got_offset
= got
->add_constant(0);
2764 // Allocate the TLSDESC_PLT entry.
2765 this->plt_
->reserve_tlsdesc_entry(got_offset
);
2769 // Create a GOT entry for the TLS module index.
2773 Target_x86_64
<size
>::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
2774 Sized_relobj_file
<size
, false>* object
)
2776 if (this->got_mod_index_offset_
== -1U)
2778 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
2779 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
2780 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2781 unsigned int got_offset
= got
->add_constant(0);
2782 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
2784 got
->add_constant(0);
2785 this->got_mod_index_offset_
= got_offset
;
2787 return this->got_mod_index_offset_
;
2790 // Optimize the TLS relocation type based on what we know about the
2791 // symbol. IS_FINAL is true if the final address of this symbol is
2792 // known at link time.
2795 tls::Tls_optimization
2796 Target_x86_64
<size
>::optimize_tls_reloc(bool is_final
, int r_type
)
2798 // If we are generating a shared library, then we can't do anything
2800 if (parameters
->options().shared())
2801 return tls::TLSOPT_NONE
;
2805 case elfcpp::R_X86_64_TLSGD
:
2806 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2807 case elfcpp::R_X86_64_TLSDESC_CALL
:
2808 // These are General-Dynamic which permits fully general TLS
2809 // access. Since we know that we are generating an executable,
2810 // we can convert this to Initial-Exec. If we also know that
2811 // this is a local symbol, we can further switch to Local-Exec.
2813 return tls::TLSOPT_TO_LE
;
2814 return tls::TLSOPT_TO_IE
;
2816 case elfcpp::R_X86_64_TLSLD
:
2817 // This is Local-Dynamic, which refers to a local symbol in the
2818 // dynamic TLS block. Since we know that we generating an
2819 // executable, we can switch to Local-Exec.
2820 return tls::TLSOPT_TO_LE
;
2822 case elfcpp::R_X86_64_DTPOFF32
:
2823 case elfcpp::R_X86_64_DTPOFF64
:
2824 // Another Local-Dynamic reloc.
2825 return tls::TLSOPT_TO_LE
;
2827 case elfcpp::R_X86_64_GOTTPOFF
:
2828 // These are Initial-Exec relocs which get the thread offset
2829 // from the GOT. If we know that we are linking against the
2830 // local symbol, we can switch to Local-Exec, which links the
2831 // thread offset into the instruction.
2833 return tls::TLSOPT_TO_LE
;
2834 return tls::TLSOPT_NONE
;
2836 case elfcpp::R_X86_64_TPOFF32
:
2837 // When we already have Local-Exec, there is nothing further we
2839 return tls::TLSOPT_NONE
;
2846 // Get the Reference_flags for a particular relocation.
2850 Target_x86_64
<size
>::Scan::get_reference_flags(unsigned int r_type
)
2854 case elfcpp::R_X86_64_NONE
:
2855 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2856 case elfcpp::R_X86_64_GNU_VTENTRY
:
2857 case elfcpp::R_X86_64_GOTPC32
:
2858 case elfcpp::R_X86_64_GOTPC64
:
2859 // No symbol reference.
2862 case elfcpp::R_X86_64_64
:
2863 case elfcpp::R_X86_64_32
:
2864 case elfcpp::R_X86_64_32S
:
2865 case elfcpp::R_X86_64_16
:
2866 case elfcpp::R_X86_64_8
:
2867 return Symbol::ABSOLUTE_REF
;
2869 case elfcpp::R_X86_64_PC64
:
2870 case elfcpp::R_X86_64_PC32
:
2871 case elfcpp::R_X86_64_PC32_BND
:
2872 case elfcpp::R_X86_64_PC16
:
2873 case elfcpp::R_X86_64_PC8
:
2874 case elfcpp::R_X86_64_GOTOFF64
:
2875 return Symbol::RELATIVE_REF
;
2877 case elfcpp::R_X86_64_PLT32
:
2878 case elfcpp::R_X86_64_PLT32_BND
:
2879 case elfcpp::R_X86_64_PLTOFF64
:
2880 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
2882 case elfcpp::R_X86_64_GOT64
:
2883 case elfcpp::R_X86_64_GOT32
:
2884 case elfcpp::R_X86_64_GOTPCREL64
:
2885 case elfcpp::R_X86_64_GOTPCREL
:
2886 case elfcpp::R_X86_64_GOTPCRELX
:
2887 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2888 case elfcpp::R_X86_64_GOTPLT64
:
2890 return Symbol::ABSOLUTE_REF
;
2892 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2893 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2894 case elfcpp::R_X86_64_TLSDESC_CALL
:
2895 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2896 case elfcpp::R_X86_64_DTPOFF32
:
2897 case elfcpp::R_X86_64_DTPOFF64
:
2898 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2899 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2900 return Symbol::TLS_REF
;
2902 case elfcpp::R_X86_64_COPY
:
2903 case elfcpp::R_X86_64_GLOB_DAT
:
2904 case elfcpp::R_X86_64_JUMP_SLOT
:
2905 case elfcpp::R_X86_64_RELATIVE
:
2906 case elfcpp::R_X86_64_IRELATIVE
:
2907 case elfcpp::R_X86_64_TPOFF64
:
2908 case elfcpp::R_X86_64_DTPMOD64
:
2909 case elfcpp::R_X86_64_TLSDESC
:
2910 case elfcpp::R_X86_64_SIZE32
:
2911 case elfcpp::R_X86_64_SIZE64
:
2913 // Not expected. We will give an error later.
2918 // Report an unsupported relocation against a local symbol.
2922 Target_x86_64
<size
>::Scan::unsupported_reloc_local(
2923 Sized_relobj_file
<size
, false>* object
,
2924 unsigned int r_type
)
2926 gold_error(_("%s: unsupported reloc %u against local symbol"),
2927 object
->name().c_str(), r_type
);
2930 // We are about to emit a dynamic relocation of type R_TYPE. If the
2931 // dynamic linker does not support it, issue an error. The GNU linker
2932 // only issues a non-PIC error for an allocated read-only section.
2933 // Here we know the section is allocated, but we don't know that it is
2934 // read-only. But we check for all the relocation types which the
2935 // glibc dynamic linker supports, so it seems appropriate to issue an
2936 // error even if the section is not read-only. If GSYM is not NULL,
2937 // it is the symbol the relocation is against; if it is NULL, the
2938 // relocation is against a local symbol.
2942 Target_x86_64
<size
>::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
2947 // These are the relocation types supported by glibc for x86_64
2948 // which should always work.
2949 case elfcpp::R_X86_64_RELATIVE
:
2950 case elfcpp::R_X86_64_IRELATIVE
:
2951 case elfcpp::R_X86_64_GLOB_DAT
:
2952 case elfcpp::R_X86_64_JUMP_SLOT
:
2953 case elfcpp::R_X86_64_DTPMOD64
:
2954 case elfcpp::R_X86_64_DTPOFF64
:
2955 case elfcpp::R_X86_64_TPOFF64
:
2956 case elfcpp::R_X86_64_64
:
2957 case elfcpp::R_X86_64_COPY
:
2960 // glibc supports these reloc types, but they can overflow.
2961 case elfcpp::R_X86_64_PC32
:
2962 case elfcpp::R_X86_64_PC32_BND
:
2963 // A PC relative reference is OK against a local symbol or if
2964 // the symbol is defined locally.
2966 || (!gsym
->is_from_dynobj()
2967 && !gsym
->is_undefined()
2968 && !gsym
->is_preemptible()))
2971 case elfcpp::R_X86_64_32
:
2972 // R_X86_64_32 is OK for x32.
2973 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2975 if (this->issued_non_pic_error_
)
2977 gold_assert(parameters
->options().output_is_position_independent());
2979 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
2980 "overflow at runtime; recompile with -fPIC"));
2986 case elfcpp::R_X86_64_32
:
2987 r_name
= "R_X86_64_32";
2989 case elfcpp::R_X86_64_PC32
:
2990 r_name
= "R_X86_64_PC32";
2992 case elfcpp::R_X86_64_PC32_BND
:
2993 r_name
= "R_X86_64_PC32_BND";
2999 object
->error(_("requires dynamic %s reloc against '%s' "
3000 "which may overflow at runtime; recompile "
3002 r_name
, gsym
->name());
3004 this->issued_non_pic_error_
= true;
3008 // This prevents us from issuing more than one error per reloc
3009 // section. But we can still wind up issuing more than one
3010 // error per object file.
3011 if (this->issued_non_pic_error_
)
3013 gold_assert(parameters
->options().output_is_position_independent());
3014 object
->error(_("requires unsupported dynamic reloc %u; "
3015 "recompile with -fPIC"),
3017 this->issued_non_pic_error_
= true;
3020 case elfcpp::R_X86_64_NONE
:
3025 // Return whether we need to make a PLT entry for a relocation of the
3026 // given type against a STT_GNU_IFUNC symbol.
3030 Target_x86_64
<size
>::Scan::reloc_needs_plt_for_ifunc(
3031 Sized_relobj_file
<size
, false>* object
,
3032 unsigned int r_type
)
3034 int flags
= Scan::get_reference_flags(r_type
);
3035 if (flags
& Symbol::TLS_REF
)
3036 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
3037 object
->name().c_str(), r_type
);
3041 // Scan a relocation for a local symbol.
3045 Target_x86_64
<size
>::Scan::local(Symbol_table
* symtab
,
3047 Target_x86_64
<size
>* target
,
3048 Sized_relobj_file
<size
, false>* object
,
3049 unsigned int data_shndx
,
3050 Output_section
* output_section
,
3051 const elfcpp::Rela
<size
, false>& reloc
,
3052 unsigned int r_type
,
3053 const elfcpp::Sym
<size
, false>& lsym
,
3059 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3060 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
3061 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
3063 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3064 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
3069 case elfcpp::R_X86_64_NONE
:
3070 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3071 case elfcpp::R_X86_64_GNU_VTENTRY
:
3074 case elfcpp::R_X86_64_64
:
3075 // If building a shared library (or a position-independent
3076 // executable), we need to create a dynamic relocation for this
3077 // location. The relocation applied at link time will apply the
3078 // link-time value, so we flag the location with an
3079 // R_X86_64_RELATIVE relocation so the dynamic loader can
3080 // relocate it easily.
3081 if (parameters
->options().output_is_position_independent())
3083 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3084 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3085 rela_dyn
->add_local_relative(object
, r_sym
,
3087 ? elfcpp::R_X86_64_RELATIVE64
3088 : elfcpp::R_X86_64_RELATIVE
),
3089 output_section
, data_shndx
,
3090 reloc
.get_r_offset(),
3091 reloc
.get_r_addend(), is_ifunc
);
3095 case elfcpp::R_X86_64_32
:
3096 case elfcpp::R_X86_64_32S
:
3097 case elfcpp::R_X86_64_16
:
3098 case elfcpp::R_X86_64_8
:
3099 // If building a shared library (or a position-independent
3100 // executable), we need to create a dynamic relocation for this
3101 // location. We can't use an R_X86_64_RELATIVE relocation
3102 // because that is always a 64-bit relocation.
3103 if (parameters
->options().output_is_position_independent())
3105 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
3106 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
3108 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3109 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3110 rela_dyn
->add_local_relative(object
, r_sym
,
3111 elfcpp::R_X86_64_RELATIVE
,
3112 output_section
, data_shndx
,
3113 reloc
.get_r_offset(),
3114 reloc
.get_r_addend(), is_ifunc
);
3118 this->check_non_pic(object
, r_type
, NULL
);
3120 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3121 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3122 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
3123 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
3124 data_shndx
, reloc
.get_r_offset(),
3125 reloc
.get_r_addend());
3128 gold_assert(lsym
.get_st_value() == 0);
3129 unsigned int shndx
= lsym
.get_st_shndx();
3131 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
3134 object
->error(_("section symbol %u has bad shndx %u"),
3137 rela_dyn
->add_local_section(object
, shndx
,
3138 r_type
, output_section
,
3139 data_shndx
, reloc
.get_r_offset(),
3140 reloc
.get_r_addend());
3145 case elfcpp::R_X86_64_PC64
:
3146 case elfcpp::R_X86_64_PC32
:
3147 case elfcpp::R_X86_64_PC32_BND
:
3148 case elfcpp::R_X86_64_PC16
:
3149 case elfcpp::R_X86_64_PC8
:
3152 case elfcpp::R_X86_64_PLT32
:
3153 case elfcpp::R_X86_64_PLT32_BND
:
3154 // Since we know this is a local symbol, we can handle this as a
3158 case elfcpp::R_X86_64_GOTPC32
:
3159 case elfcpp::R_X86_64_GOTOFF64
:
3160 case elfcpp::R_X86_64_GOTPC64
:
3161 case elfcpp::R_X86_64_PLTOFF64
:
3162 // We need a GOT section.
3163 target
->got_section(symtab
, layout
);
3164 // For PLTOFF64, we'd normally want a PLT section, but since we
3165 // know this is a local symbol, no PLT is needed.
3168 case elfcpp::R_X86_64_GOT64
:
3169 case elfcpp::R_X86_64_GOT32
:
3170 case elfcpp::R_X86_64_GOTPCREL64
:
3171 case elfcpp::R_X86_64_GOTPCREL
:
3172 case elfcpp::R_X86_64_GOTPCRELX
:
3173 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3174 case elfcpp::R_X86_64_GOTPLT64
:
3176 // The symbol requires a GOT section.
3177 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
3179 // If the relocation symbol isn't IFUNC,
3180 // and is local, then we will convert
3181 // mov foo@GOTPCREL(%rip), %reg
3182 // to lea foo(%rip), %reg.
3183 // in Relocate::relocate.
3184 if (!parameters
->incremental()
3185 && (r_type
== elfcpp::R_X86_64_GOTPCREL
3186 || r_type
== elfcpp::R_X86_64_GOTPCRELX
3187 || r_type
== elfcpp::R_X86_64_REX_GOTPCRELX
)
3188 && reloc
.get_r_offset() >= 2
3191 section_size_type stype
;
3192 const unsigned char* view
= object
->section_contents(data_shndx
,
3194 if (view
[reloc
.get_r_offset() - 2] == 0x8b)
3198 // The symbol requires a GOT entry.
3199 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3201 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3202 // lets function pointers compare correctly with shared
3203 // libraries. Otherwise we would need an IRELATIVE reloc.
3206 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
3208 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
3211 // If we are generating a shared object, we need to add a
3212 // dynamic relocation for this symbol's GOT entry.
3213 if (parameters
->options().output_is_position_independent())
3215 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3216 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3217 if (r_type
!= elfcpp::R_X86_64_GOT32
)
3219 unsigned int got_offset
=
3220 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
3221 rela_dyn
->add_local_relative(object
, r_sym
,
3222 elfcpp::R_X86_64_RELATIVE
,
3223 got
, got_offset
, 0, is_ifunc
);
3227 this->check_non_pic(object
, r_type
, NULL
);
3229 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
3230 rela_dyn
->add_local(
3231 object
, r_sym
, r_type
, got
,
3232 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
3236 // For GOTPLT64, we'd normally want a PLT section, but since
3237 // we know this is a local symbol, no PLT is needed.
3241 case elfcpp::R_X86_64_COPY
:
3242 case elfcpp::R_X86_64_GLOB_DAT
:
3243 case elfcpp::R_X86_64_JUMP_SLOT
:
3244 case elfcpp::R_X86_64_RELATIVE
:
3245 case elfcpp::R_X86_64_IRELATIVE
:
3246 // These are outstanding tls relocs, which are unexpected when linking
3247 case elfcpp::R_X86_64_TPOFF64
:
3248 case elfcpp::R_X86_64_DTPMOD64
:
3249 case elfcpp::R_X86_64_TLSDESC
:
3250 gold_error(_("%s: unexpected reloc %u in object file"),
3251 object
->name().c_str(), r_type
);
3254 // These are initial tls relocs, which are expected when linking
3255 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3256 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3257 case elfcpp::R_X86_64_TLSDESC_CALL
:
3258 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3259 case elfcpp::R_X86_64_DTPOFF32
:
3260 case elfcpp::R_X86_64_DTPOFF64
:
3261 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3262 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3264 bool output_is_shared
= parameters
->options().shared();
3265 const tls::Tls_optimization optimized_type
3266 = Target_x86_64
<size
>::optimize_tls_reloc(!output_is_shared
,
3270 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
3271 if (optimized_type
== tls::TLSOPT_NONE
)
3273 // Create a pair of GOT entries for the module index and
3274 // dtv-relative offset.
3275 Output_data_got
<64, false>* got
3276 = target
->got_section(symtab
, layout
);
3277 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3278 unsigned int shndx
= lsym
.get_st_shndx();
3280 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
3282 object
->error(_("local symbol %u has bad shndx %u"),
3285 got
->add_local_pair_with_rel(object
, r_sym
,
3288 target
->rela_dyn_section(layout
),
3289 elfcpp::R_X86_64_DTPMOD64
);
3291 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3292 unsupported_reloc_local(object
, r_type
);
3295 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3296 target
->define_tls_base_symbol(symtab
, layout
);
3297 if (optimized_type
== tls::TLSOPT_NONE
)
3299 // Create reserved PLT and GOT entries for the resolver.
3300 target
->reserve_tlsdesc_entries(symtab
, layout
);
3302 // Generate a double GOT entry with an
3303 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3304 // is resolved lazily, so the GOT entry needs to be in
3305 // an area in .got.plt, not .got. Call got_section to
3306 // make sure the section has been created.
3307 target
->got_section(symtab
, layout
);
3308 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
3309 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3310 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
3312 unsigned int got_offset
= got
->add_constant(0);
3313 got
->add_constant(0);
3314 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
3316 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
3317 // We store the arguments we need in a vector, and
3318 // use the index into the vector as the parameter
3319 // to pass to the target specific routines.
3320 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
3321 void* arg
= reinterpret_cast<void*>(intarg
);
3322 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
3323 got
, got_offset
, 0);
3326 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3327 unsupported_reloc_local(object
, r_type
);
3330 case elfcpp::R_X86_64_TLSDESC_CALL
:
3333 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3334 if (optimized_type
== tls::TLSOPT_NONE
)
3336 // Create a GOT entry for the module index.
3337 target
->got_mod_index_entry(symtab
, layout
, object
);
3339 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3340 unsupported_reloc_local(object
, r_type
);
3343 case elfcpp::R_X86_64_DTPOFF32
:
3344 case elfcpp::R_X86_64_DTPOFF64
:
3347 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3348 layout
->set_has_static_tls();
3349 if (optimized_type
== tls::TLSOPT_NONE
)
3351 // Create a GOT entry for the tp-relative offset.
3352 Output_data_got
<64, false>* got
3353 = target
->got_section(symtab
, layout
);
3354 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3355 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
3356 target
->rela_dyn_section(layout
),
3357 elfcpp::R_X86_64_TPOFF64
);
3359 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3360 unsupported_reloc_local(object
, r_type
);
3363 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3364 layout
->set_has_static_tls();
3365 if (output_is_shared
)
3366 unsupported_reloc_local(object
, r_type
);
3375 case elfcpp::R_X86_64_SIZE32
:
3376 case elfcpp::R_X86_64_SIZE64
:
3378 gold_error(_("%s: unsupported reloc %u against local symbol"),
3379 object
->name().c_str(), r_type
);
3385 // Report an unsupported relocation against a global symbol.
3389 Target_x86_64
<size
>::Scan::unsupported_reloc_global(
3390 Sized_relobj_file
<size
, false>* object
,
3391 unsigned int r_type
,
3394 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3395 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
3398 // Returns true if this relocation type could be that of a function pointer.
3401 Target_x86_64
<size
>::Scan::possible_function_pointer_reloc(
3402 Sized_relobj_file
<size
, false>* src_obj
,
3403 unsigned int src_indx
,
3404 unsigned int r_offset
,
3405 unsigned int r_type
)
3409 case elfcpp::R_X86_64_64
:
3410 case elfcpp::R_X86_64_32
:
3411 case elfcpp::R_X86_64_32S
:
3412 case elfcpp::R_X86_64_16
:
3413 case elfcpp::R_X86_64_8
:
3414 case elfcpp::R_X86_64_GOT64
:
3415 case elfcpp::R_X86_64_GOT32
:
3416 case elfcpp::R_X86_64_GOTPCREL64
:
3417 case elfcpp::R_X86_64_GOTPCREL
:
3418 case elfcpp::R_X86_64_GOTPCRELX
:
3419 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3420 case elfcpp::R_X86_64_GOTPLT64
:
3424 case elfcpp::R_X86_64_PC32
:
3426 // This relocation may be used both for function calls and
3427 // for taking address of a function. We distinguish between
3428 // them by checking the opcodes.
3429 uint64_t sh_flags
= src_obj
->section_flags(src_indx
);
3430 bool is_executable
= (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0;
3433 section_size_type stype
;
3434 const unsigned char* view
= src_obj
->section_contents(src_indx
,
3440 && view
[r_offset
- 1] == 0xe8)
3445 && view
[r_offset
- 1] == 0xe9)
3448 // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
3450 && view
[r_offset
- 2] == 0x0f
3451 && view
[r_offset
- 1] >= 0x80
3452 && view
[r_offset
- 1] <= 0x8f)
3456 // Be conservative and treat all others as function pointers.
3463 // For safe ICF, scan a relocation for a local symbol to check if it
3464 // corresponds to a function pointer being taken. In that case mark
3465 // the function whose pointer was taken as not foldable.
3469 Target_x86_64
<size
>::Scan::local_reloc_may_be_function_pointer(
3472 Target_x86_64
<size
>* ,
3473 Sized_relobj_file
<size
, false>* src_obj
,
3474 unsigned int src_indx
,
3476 const elfcpp::Rela
<size
, false>& reloc
,
3477 unsigned int r_type
,
3478 const elfcpp::Sym
<size
, false>&)
3480 // When building a shared library, do not fold any local symbols as it is
3481 // not possible to distinguish pointer taken versus a call by looking at
3482 // the relocation types.
3483 if (parameters
->options().shared())
3486 return possible_function_pointer_reloc(src_obj
, src_indx
,
3487 reloc
.get_r_offset(), r_type
);
3490 // For safe ICF, scan a relocation for a global symbol to check if it
3491 // corresponds to a function pointer being taken. In that case mark
3492 // the function whose pointer was taken as not foldable.
3496 Target_x86_64
<size
>::Scan::global_reloc_may_be_function_pointer(
3499 Target_x86_64
<size
>* ,
3500 Sized_relobj_file
<size
, false>* src_obj
,
3501 unsigned int src_indx
,
3503 const elfcpp::Rela
<size
, false>& reloc
,
3504 unsigned int r_type
,
3507 // When building a shared library, do not fold symbols whose visibility
3508 // is hidden, internal or protected.
3509 if (parameters
->options().shared()
3510 && (gsym
->visibility() == elfcpp::STV_INTERNAL
3511 || gsym
->visibility() == elfcpp::STV_PROTECTED
3512 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
3515 return possible_function_pointer_reloc(src_obj
, src_indx
,
3516 reloc
.get_r_offset(), r_type
);
3519 // Scan a relocation for a global symbol.
3523 Target_x86_64
<size
>::Scan::global(Symbol_table
* symtab
,
3525 Target_x86_64
<size
>* target
,
3526 Sized_relobj_file
<size
, false>* object
,
3527 unsigned int data_shndx
,
3528 Output_section
* output_section
,
3529 const elfcpp::Rela
<size
, false>& reloc
,
3530 unsigned int r_type
,
3533 // A STT_GNU_IFUNC symbol may require a PLT entry.
3534 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
3535 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
3536 target
->make_plt_entry(symtab
, layout
, gsym
);
3540 case elfcpp::R_X86_64_NONE
:
3541 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3542 case elfcpp::R_X86_64_GNU_VTENTRY
:
3545 case elfcpp::R_X86_64_64
:
3546 case elfcpp::R_X86_64_32
:
3547 case elfcpp::R_X86_64_32S
:
3548 case elfcpp::R_X86_64_16
:
3549 case elfcpp::R_X86_64_8
:
3551 // Make a PLT entry if necessary.
3552 if (gsym
->needs_plt_entry())
3554 target
->make_plt_entry(symtab
, layout
, gsym
);
3555 // Since this is not a PC-relative relocation, we may be
3556 // taking the address of a function. In that case we need to
3557 // set the entry in the dynamic symbol table to the address of
3559 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
3560 gsym
->set_needs_dynsym_value();
3562 // Make a dynamic relocation if necessary.
3563 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
3565 if (!parameters
->options().output_is_position_independent()
3566 && gsym
->may_need_copy_reloc())
3568 target
->copy_reloc(symtab
, layout
, object
,
3569 data_shndx
, output_section
, gsym
, reloc
);
3571 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
3572 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
3573 && gsym
->type() == elfcpp::STT_GNU_IFUNC
3574 && gsym
->can_use_relative_reloc(false)
3575 && !gsym
->is_from_dynobj()
3576 && !gsym
->is_undefined()
3577 && !gsym
->is_preemptible())
3579 // Use an IRELATIVE reloc for a locally defined
3580 // STT_GNU_IFUNC symbol. This makes a function
3581 // address in a PIE executable match the address in a
3582 // shared library that it links against.
3583 Reloc_section
* rela_dyn
=
3584 target
->rela_irelative_section(layout
);
3585 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
3586 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
3587 output_section
, object
,
3589 reloc
.get_r_offset(),
3590 reloc
.get_r_addend());
3592 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
3593 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
3594 && gsym
->can_use_relative_reloc(false))
3596 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3597 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
3598 output_section
, object
,
3600 reloc
.get_r_offset(),
3601 reloc
.get_r_addend(), false);
3605 this->check_non_pic(object
, r_type
, gsym
);
3606 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3607 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
3608 data_shndx
, reloc
.get_r_offset(),
3609 reloc
.get_r_addend());
3615 case elfcpp::R_X86_64_PC64
:
3616 case elfcpp::R_X86_64_PC32
:
3617 case elfcpp::R_X86_64_PC32_BND
:
3618 case elfcpp::R_X86_64_PC16
:
3619 case elfcpp::R_X86_64_PC8
:
3621 // Make a PLT entry if necessary.
3622 if (gsym
->needs_plt_entry())
3623 target
->make_plt_entry(symtab
, layout
, gsym
);
3624 // Make a dynamic relocation if necessary.
3625 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
3627 if (parameters
->options().output_is_executable()
3628 && gsym
->may_need_copy_reloc())
3630 target
->copy_reloc(symtab
, layout
, object
,
3631 data_shndx
, output_section
, gsym
, reloc
);
3635 this->check_non_pic(object
, r_type
, gsym
);
3636 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3637 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
3638 data_shndx
, reloc
.get_r_offset(),
3639 reloc
.get_r_addend());
3645 case elfcpp::R_X86_64_GOT64
:
3646 case elfcpp::R_X86_64_GOT32
:
3647 case elfcpp::R_X86_64_GOTPCREL64
:
3648 case elfcpp::R_X86_64_GOTPCREL
:
3649 case elfcpp::R_X86_64_GOTPCRELX
:
3650 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3651 case elfcpp::R_X86_64_GOTPLT64
:
3653 // The symbol requires a GOT entry.
3654 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
3656 // If we convert this from
3657 // mov foo@GOTPCREL(%rip), %reg
3658 // to lea foo(%rip), %reg.
3661 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
3663 // in Relocate::relocate, then there is nothing to do here.
3664 // We cannot make these optimizations in incremental linking mode,
3665 // because we look at the opcode to decide whether or not to make
3666 // change, and during an incremental update, the change may have
3667 // already been applied.
3669 Lazy_view
<size
> view(object
, data_shndx
);
3670 size_t r_offset
= reloc
.get_r_offset();
3671 if (!parameters
->incremental()
3673 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
, r_type
,
3677 if (!parameters
->incremental()
3679 && Target_x86_64
<size
>::can_convert_callq_to_direct(gsym
, r_type
,
3684 if (gsym
->final_value_is_known())
3686 // For a STT_GNU_IFUNC symbol we want the PLT address.
3687 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
3688 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
3690 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
3694 // If this symbol is not fully resolved, we need to add a
3695 // dynamic relocation for it.
3696 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3698 // Use a GLOB_DAT rather than a RELATIVE reloc if:
3700 // 1) The symbol may be defined in some other module.
3702 // 2) We are building a shared library and this is a
3703 // protected symbol; using GLOB_DAT means that the dynamic
3704 // linker can use the address of the PLT in the main
3705 // executable when appropriate so that function address
3706 // comparisons work.
3708 // 3) This is a STT_GNU_IFUNC symbol in position dependent
3709 // code, again so that function address comparisons work.
3710 if (gsym
->is_from_dynobj()
3711 || gsym
->is_undefined()
3712 || gsym
->is_preemptible()
3713 || (gsym
->visibility() == elfcpp::STV_PROTECTED
3714 && parameters
->options().shared())
3715 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
3716 && parameters
->options().output_is_position_independent()))
3717 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
3718 elfcpp::R_X86_64_GLOB_DAT
);
3721 // For a STT_GNU_IFUNC symbol we want to write the PLT
3722 // offset into the GOT, so that function pointer
3723 // comparisons work correctly.
3725 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
3726 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
3729 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
3730 // Tell the dynamic linker to use the PLT address
3731 // when resolving relocations.
3732 if (gsym
->is_from_dynobj()
3733 && !parameters
->options().shared())
3734 gsym
->set_needs_dynsym_value();
3738 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
3739 rela_dyn
->add_global_relative(gsym
,
3740 elfcpp::R_X86_64_RELATIVE
,
3741 got
, got_off
, 0, false);
3748 case elfcpp::R_X86_64_PLT32
:
3749 case elfcpp::R_X86_64_PLT32_BND
:
3750 // If the symbol is fully resolved, this is just a PC32 reloc.
3751 // Otherwise we need a PLT entry.
3752 if (gsym
->final_value_is_known())
3754 // If building a shared library, we can also skip the PLT entry
3755 // if the symbol is defined in the output file and is protected
3757 if (gsym
->is_defined()
3758 && !gsym
->is_from_dynobj()
3759 && !gsym
->is_preemptible())
3761 target
->make_plt_entry(symtab
, layout
, gsym
);
3764 case elfcpp::R_X86_64_GOTPC32
:
3765 case elfcpp::R_X86_64_GOTOFF64
:
3766 case elfcpp::R_X86_64_GOTPC64
:
3767 case elfcpp::R_X86_64_PLTOFF64
:
3768 // We need a GOT section.
3769 target
->got_section(symtab
, layout
);
3770 // For PLTOFF64, we also need a PLT entry (but only if the
3771 // symbol is not fully resolved).
3772 if (r_type
== elfcpp::R_X86_64_PLTOFF64
3773 && !gsym
->final_value_is_known())
3774 target
->make_plt_entry(symtab
, layout
, gsym
);
3777 case elfcpp::R_X86_64_COPY
:
3778 case elfcpp::R_X86_64_GLOB_DAT
:
3779 case elfcpp::R_X86_64_JUMP_SLOT
:
3780 case elfcpp::R_X86_64_RELATIVE
:
3781 case elfcpp::R_X86_64_IRELATIVE
:
3782 // These are outstanding tls relocs, which are unexpected when linking
3783 case elfcpp::R_X86_64_TPOFF64
:
3784 case elfcpp::R_X86_64_DTPMOD64
:
3785 case elfcpp::R_X86_64_TLSDESC
:
3786 gold_error(_("%s: unexpected reloc %u in object file"),
3787 object
->name().c_str(), r_type
);
3790 // These are initial tls relocs, which are expected for global()
3791 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3792 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3793 case elfcpp::R_X86_64_TLSDESC_CALL
:
3794 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3795 case elfcpp::R_X86_64_DTPOFF32
:
3796 case elfcpp::R_X86_64_DTPOFF64
:
3797 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3798 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3800 // For the Initial-Exec model, we can treat undef symbols as final
3801 // when building an executable.
3802 const bool is_final
= (gsym
->final_value_is_known() ||
3803 (r_type
== elfcpp::R_X86_64_GOTTPOFF
&&
3804 gsym
->is_undefined() &&
3805 parameters
->options().output_is_executable()));
3806 const tls::Tls_optimization optimized_type
3807 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3810 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
3811 if (optimized_type
== tls::TLSOPT_NONE
)
3813 // Create a pair of GOT entries for the module index and
3814 // dtv-relative offset.
3815 Output_data_got
<64, false>* got
3816 = target
->got_section(symtab
, layout
);
3817 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
3818 target
->rela_dyn_section(layout
),
3819 elfcpp::R_X86_64_DTPMOD64
,
3820 elfcpp::R_X86_64_DTPOFF64
);
3822 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3824 // Create a GOT entry for the tp-relative offset.
3825 Output_data_got
<64, false>* got
3826 = target
->got_section(symtab
, layout
);
3827 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3828 target
->rela_dyn_section(layout
),
3829 elfcpp::R_X86_64_TPOFF64
);
3831 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3832 unsupported_reloc_global(object
, r_type
, gsym
);
3835 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3836 target
->define_tls_base_symbol(symtab
, layout
);
3837 if (optimized_type
== tls::TLSOPT_NONE
)
3839 // Create reserved PLT and GOT entries for the resolver.
3840 target
->reserve_tlsdesc_entries(symtab
, layout
);
3842 // Create a double GOT entry with an R_X86_64_TLSDESC
3843 // reloc. The R_X86_64_TLSDESC reloc is resolved
3844 // lazily, so the GOT entry needs to be in an area in
3845 // .got.plt, not .got. Call got_section to make sure
3846 // the section has been created.
3847 target
->got_section(symtab
, layout
);
3848 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
3849 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
3850 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
3851 elfcpp::R_X86_64_TLSDESC
, 0);
3853 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3855 // Create a GOT entry for the tp-relative offset.
3856 Output_data_got
<64, false>* got
3857 = target
->got_section(symtab
, layout
);
3858 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3859 target
->rela_dyn_section(layout
),
3860 elfcpp::R_X86_64_TPOFF64
);
3862 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3863 unsupported_reloc_global(object
, r_type
, gsym
);
3866 case elfcpp::R_X86_64_TLSDESC_CALL
:
3869 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3870 if (optimized_type
== tls::TLSOPT_NONE
)
3872 // Create a GOT entry for the module index.
3873 target
->got_mod_index_entry(symtab
, layout
, object
);
3875 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3876 unsupported_reloc_global(object
, r_type
, gsym
);
3879 case elfcpp::R_X86_64_DTPOFF32
:
3880 case elfcpp::R_X86_64_DTPOFF64
:
3883 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3884 layout
->set_has_static_tls();
3885 if (optimized_type
== tls::TLSOPT_NONE
)
3887 // Create a GOT entry for the tp-relative offset.
3888 Output_data_got
<64, false>* got
3889 = target
->got_section(symtab
, layout
);
3890 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3891 target
->rela_dyn_section(layout
),
3892 elfcpp::R_X86_64_TPOFF64
);
3894 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3895 unsupported_reloc_global(object
, r_type
, gsym
);
3898 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3899 layout
->set_has_static_tls();
3900 if (parameters
->options().shared())
3901 unsupported_reloc_global(object
, r_type
, gsym
);
3910 case elfcpp::R_X86_64_SIZE32
:
3911 case elfcpp::R_X86_64_SIZE64
:
3913 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3914 object
->name().c_str(), r_type
,
3915 gsym
->demangled_name().c_str());
3922 Target_x86_64
<size
>::gc_process_relocs(Symbol_table
* symtab
,
3924 Sized_relobj_file
<size
, false>* object
,
3925 unsigned int data_shndx
,
3926 unsigned int sh_type
,
3927 const unsigned char* prelocs
,
3929 Output_section
* output_section
,
3930 bool needs_special_offset_handling
,
3931 size_t local_symbol_count
,
3932 const unsigned char* plocal_symbols
)
3934 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
3937 if (sh_type
== elfcpp::SHT_REL
)
3942 gold::gc_process_relocs
<size
, false, Target_x86_64
<size
>, Scan
,
3952 needs_special_offset_handling
,
3957 // Scan relocations for a section.
3961 Target_x86_64
<size
>::scan_relocs(Symbol_table
* symtab
,
3963 Sized_relobj_file
<size
, false>* object
,
3964 unsigned int data_shndx
,
3965 unsigned int sh_type
,
3966 const unsigned char* prelocs
,
3968 Output_section
* output_section
,
3969 bool needs_special_offset_handling
,
3970 size_t local_symbol_count
,
3971 const unsigned char* plocal_symbols
)
3973 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
3976 if (sh_type
== elfcpp::SHT_REL
)
3978 gold_error(_("%s: unsupported REL reloc section"),
3979 object
->name().c_str());
3983 gold::scan_relocs
<size
, false, Target_x86_64
<size
>, Scan
, Classify_reloc
>(
3992 needs_special_offset_handling
,
3997 // Finalize the sections.
4001 Target_x86_64
<size
>::do_finalize_sections(
4003 const Input_objects
*,
4004 Symbol_table
* symtab
)
4006 const Reloc_section
* rel_plt
= (this->plt_
== NULL
4008 : this->plt_
->rela_plt());
4009 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
4010 this->rela_dyn_
, true, false);
4012 // Fill in some more dynamic tags.
4013 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
4016 if (this->plt_
!= NULL
4017 && this->plt_
->output_section() != NULL
4018 && this->plt_
->has_tlsdesc_entry())
4020 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
4021 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
4022 this->got_
->finalize_data_size();
4023 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
4024 this->plt_
, plt_offset
);
4025 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
4026 this->got_
, got_offset
);
4030 // Emit any relocs we saved in an attempt to avoid generating COPY
4032 if (this->copy_relocs_
.any_saved_relocs())
4033 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
4035 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
4036 // the .got.plt section.
4037 Symbol
* sym
= this->global_offset_table_
;
4040 uint64_t data_size
= this->got_plt_
->current_data_size();
4041 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
4044 if (parameters
->doing_static_link()
4045 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
4047 // If linking statically, make sure that the __rela_iplt symbols
4048 // were defined if necessary, even if we didn't create a PLT.
4049 static const Define_symbol_in_segment syms
[] =
4052 "__rela_iplt_start", // name
4053 elfcpp::PT_LOAD
, // segment_type
4054 elfcpp::PF_W
, // segment_flags_set
4055 elfcpp::PF(0), // segment_flags_clear
4058 elfcpp::STT_NOTYPE
, // type
4059 elfcpp::STB_GLOBAL
, // binding
4060 elfcpp::STV_HIDDEN
, // visibility
4062 Symbol::SEGMENT_START
, // offset_from_base
4066 "__rela_iplt_end", // name
4067 elfcpp::PT_LOAD
, // segment_type
4068 elfcpp::PF_W
, // segment_flags_set
4069 elfcpp::PF(0), // segment_flags_clear
4072 elfcpp::STT_NOTYPE
, // type
4073 elfcpp::STB_GLOBAL
, // binding
4074 elfcpp::STV_HIDDEN
, // visibility
4076 Symbol::SEGMENT_START
, // offset_from_base
4081 symtab
->define_symbols(layout
, 2, syms
,
4082 layout
->script_options()->saw_sections_clause());
4086 // For x32, we need to handle PC-relative relocations using full 64-bit
4087 // arithmetic, so that we can detect relocation overflows properly.
4088 // This class overrides the pcrela32_check methods from the defaults in
4089 // Relocate_functions in reloc.h.
4092 class X86_64_relocate_functions
: public Relocate_functions
<size
, false>
4095 typedef Relocate_functions
<size
, false> Base
;
4097 // Do a simple PC relative relocation with the addend in the
4099 static inline typename
Base::Reloc_status
4100 pcrela32_check(unsigned char* view
,
4101 typename
elfcpp::Elf_types
<64>::Elf_Addr value
,
4102 typename
elfcpp::Elf_types
<64>::Elf_Swxword addend
,
4103 typename
elfcpp::Elf_types
<64>::Elf_Addr address
)
4105 typedef typename
elfcpp::Swap
<32, false>::Valtype Valtype
;
4106 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
4107 value
= value
+ addend
- address
;
4108 elfcpp::Swap
<32, false>::writeval(wv
, value
);
4109 return (Bits
<32>::has_overflow(value
)
4110 ? Base::RELOC_OVERFLOW
: Base::RELOC_OK
);
4113 // Do a simple PC relative relocation with a Symbol_value with the
4114 // addend in the relocation.
4115 static inline typename
Base::Reloc_status
4116 pcrela32_check(unsigned char* view
,
4117 const Sized_relobj_file
<size
, false>* object
,
4118 const Symbol_value
<size
>* psymval
,
4119 typename
elfcpp::Elf_types
<64>::Elf_Swxword addend
,
4120 typename
elfcpp::Elf_types
<64>::Elf_Addr address
)
4122 typedef typename
elfcpp::Swap
<32, false>::Valtype Valtype
;
4123 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
4124 typename
elfcpp::Elf_types
<64>::Elf_Addr value
;
4126 value
= psymval
->value(object
, addend
);
4129 // For negative addends, get the symbol value without
4130 // the addend, then add the addend using 64-bit arithmetic.
4131 value
= psymval
->value(object
, 0);
4135 elfcpp::Swap
<32, false>::writeval(wv
, value
);
4136 return (Bits
<32>::has_overflow(value
)
4137 ? Base::RELOC_OVERFLOW
: Base::RELOC_OK
);
4141 // Perform a relocation.
4145 Target_x86_64
<size
>::Relocate::relocate(
4146 const Relocate_info
<size
, false>* relinfo
,
4148 Target_x86_64
<size
>* target
,
4151 const unsigned char* preloc
,
4152 const Sized_symbol
<size
>* gsym
,
4153 const Symbol_value
<size
>* psymval
,
4154 unsigned char* view
,
4155 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4156 section_size_type view_size
)
4158 typedef X86_64_relocate_functions
<size
> Reloc_funcs
;
4159 const elfcpp::Rela
<size
, false> rela(preloc
);
4160 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
4162 if (this->skip_call_tls_get_addr_
)
4164 if ((r_type
!= elfcpp::R_X86_64_PLT32
4165 && r_type
!= elfcpp::R_X86_64_GOTPCREL
4166 && r_type
!= elfcpp::R_X86_64_GOTPCRELX
4167 && r_type
!= elfcpp::R_X86_64_PLT32_BND
4168 && r_type
!= elfcpp::R_X86_64_PC32_BND
4169 && r_type
!= elfcpp::R_X86_64_PC32
)
4171 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
4173 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4174 _("missing expected TLS relocation"));
4175 this->skip_call_tls_get_addr_
= false;
4179 this->skip_call_tls_get_addr_
= false;
4187 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
4189 // Pick the value to use for symbols defined in the PLT.
4190 Symbol_value
<size
> symval
;
4192 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
4194 symval
.set_output_value(target
->plt_address_for_global(gsym
));
4197 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
4199 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4200 if (object
->local_has_plt_offset(r_sym
))
4202 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
4207 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4209 // Get the GOT offset if needed.
4210 // The GOT pointer points to the end of the GOT section.
4211 // We need to subtract the size of the GOT section to get
4212 // the actual offset to use in the relocation.
4213 bool have_got_offset
= false;
4214 // Since the actual offset is always negative, we use signed int to
4215 // support 64-bit GOT relocations.
4219 case elfcpp::R_X86_64_GOT32
:
4220 case elfcpp::R_X86_64_GOT64
:
4221 case elfcpp::R_X86_64_GOTPLT64
:
4222 case elfcpp::R_X86_64_GOTPCREL64
:
4225 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4226 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
4230 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4231 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4232 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4233 - target
->got_size());
4235 have_got_offset
= true;
4242 typename
Reloc_funcs::Reloc_status rstatus
= Reloc_funcs::RELOC_OK
;
4246 case elfcpp::R_X86_64_NONE
:
4247 case elfcpp::R_X86_64_GNU_VTINHERIT
:
4248 case elfcpp::R_X86_64_GNU_VTENTRY
:
4251 case elfcpp::R_X86_64_64
:
4252 Reloc_funcs::rela64(view
, object
, psymval
, addend
);
4255 case elfcpp::R_X86_64_PC64
:
4256 Reloc_funcs::pcrela64(view
, object
, psymval
, addend
,
4260 case elfcpp::R_X86_64_32
:
4261 rstatus
= Reloc_funcs::rela32_check(view
, object
, psymval
, addend
,
4262 Reloc_funcs::CHECK_UNSIGNED
);
4265 case elfcpp::R_X86_64_32S
:
4266 rstatus
= Reloc_funcs::rela32_check(view
, object
, psymval
, addend
,
4267 Reloc_funcs::CHECK_SIGNED
);
4270 case elfcpp::R_X86_64_PC32
:
4271 case elfcpp::R_X86_64_PC32_BND
:
4272 rstatus
= Reloc_funcs::pcrela32_check(view
, object
, psymval
, addend
,
4276 case elfcpp::R_X86_64_16
:
4277 Reloc_funcs::rela16(view
, object
, psymval
, addend
);
4280 case elfcpp::R_X86_64_PC16
:
4281 Reloc_funcs::pcrela16(view
, object
, psymval
, addend
, address
);
4284 case elfcpp::R_X86_64_8
:
4285 Reloc_funcs::rela8(view
, object
, psymval
, addend
);
4288 case elfcpp::R_X86_64_PC8
:
4289 Reloc_funcs::pcrela8(view
, object
, psymval
, addend
, address
);
4292 case elfcpp::R_X86_64_PLT32
:
4293 case elfcpp::R_X86_64_PLT32_BND
:
4294 gold_assert(gsym
== NULL
4295 || gsym
->has_plt_offset()
4296 || gsym
->final_value_is_known()
4297 || (gsym
->is_defined()
4298 && !gsym
->is_from_dynobj()
4299 && !gsym
->is_preemptible()));
4300 // Note: while this code looks the same as for R_X86_64_PC32, it
4301 // behaves differently because psymval was set to point to
4302 // the PLT entry, rather than the symbol, in Scan::global().
4303 rstatus
= Reloc_funcs::pcrela32_check(view
, object
, psymval
, addend
,
4307 case elfcpp::R_X86_64_PLTOFF64
:
4310 gold_assert(gsym
->has_plt_offset()
4311 || gsym
->final_value_is_known());
4312 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
;
4313 // This is the address of GLOBAL_OFFSET_TABLE.
4314 got_address
= target
->got_plt_section()->address();
4315 Reloc_funcs::rela64(view
, object
, psymval
, addend
- got_address
);
4319 case elfcpp::R_X86_64_GOT32
:
4320 gold_assert(have_got_offset
);
4321 Reloc_funcs::rela32(view
, got_offset
, addend
);
4324 case elfcpp::R_X86_64_GOTPC32
:
4327 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4328 value
= target
->got_plt_section()->address();
4329 Reloc_funcs::pcrela32_check(view
, value
, addend
, address
);
4333 case elfcpp::R_X86_64_GOT64
:
4334 case elfcpp::R_X86_64_GOTPLT64
:
4335 // R_X86_64_GOTPLT64 is obsolete and treated the same as
4337 gold_assert(have_got_offset
);
4338 Reloc_funcs::rela64(view
, got_offset
, addend
);
4341 case elfcpp::R_X86_64_GOTPC64
:
4344 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4345 value
= target
->got_plt_section()->address();
4346 Reloc_funcs::pcrela64(view
, value
, addend
, address
);
4350 case elfcpp::R_X86_64_GOTOFF64
:
4352 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4353 value
= (psymval
->value(object
, 0)
4354 - target
->got_plt_section()->address());
4355 Reloc_funcs::rela64(view
, value
, addend
);
4359 case elfcpp::R_X86_64_GOTPCREL
:
4360 case elfcpp::R_X86_64_GOTPCRELX
:
4361 case elfcpp::R_X86_64_REX_GOTPCRELX
:
4364 // mov foo@GOTPCREL(%rip), %reg
4365 // to lea foo(%rip), %reg.
4367 if (!parameters
->incremental()
4369 && rela
.get_r_offset() >= 2
4371 && !psymval
->is_ifunc_symbol())
4373 && rela
.get_r_offset() >= 2
4374 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
, r_type
,
4378 Reloc_funcs::pcrela32(view
, object
, psymval
, addend
, address
);
4381 // callq *foo@GOTPCRELX(%rip) to
4383 // and jmpq *foo@GOTPCRELX(%rip) to
4386 else if (!parameters
->incremental()
4388 && rela
.get_r_offset() >= 2
4389 && Target_x86_64
<size
>::can_convert_callq_to_direct(gsym
,
4393 if (view
[-1] == 0x15)
4395 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4396 // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
4399 // Convert GOTPCRELX to 32-bit pc relative reloc.
4400 Reloc_funcs::pcrela32(view
, object
, psymval
, addend
, address
);
4404 // Convert jmpq *foo@GOTPCRELX(%rip) to
4407 // The opcode of direct jmpq is 0xe9.
4409 // The opcode of nop is 0x90.
4411 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip
4412 // relative and since the instruction following the jmpq is now
4413 // the nop, offset the address by 1 byte. The start of the
4414 // relocation also moves ahead by 1 byte.
4415 Reloc_funcs::pcrela32(&view
[-1], object
, psymval
, addend
,
4423 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4424 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
4425 - target
->got_size());
4429 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4430 gold_assert(object
->local_has_got_offset(r_sym
,
4431 GOT_TYPE_STANDARD
));
4432 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4433 - target
->got_size());
4435 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4436 value
= target
->got_plt_section()->address() + got_offset
;
4437 Reloc_funcs::pcrela32_check(view
, value
, addend
, address
);
4442 case elfcpp::R_X86_64_GOTPCREL64
:
4444 gold_assert(have_got_offset
);
4445 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4446 value
= target
->got_plt_section()->address() + got_offset
;
4447 Reloc_funcs::pcrela64(view
, value
, addend
, address
);
4451 case elfcpp::R_X86_64_COPY
:
4452 case elfcpp::R_X86_64_GLOB_DAT
:
4453 case elfcpp::R_X86_64_JUMP_SLOT
:
4454 case elfcpp::R_X86_64_RELATIVE
:
4455 case elfcpp::R_X86_64_IRELATIVE
:
4456 // These are outstanding tls relocs, which are unexpected when linking
4457 case elfcpp::R_X86_64_TPOFF64
:
4458 case elfcpp::R_X86_64_DTPMOD64
:
4459 case elfcpp::R_X86_64_TLSDESC
:
4460 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4461 _("unexpected reloc %u in object file"),
4465 // These are initial tls relocs, which are expected when linking
4466 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4467 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4468 case elfcpp::R_X86_64_TLSDESC_CALL
:
4469 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4470 case elfcpp::R_X86_64_DTPOFF32
:
4471 case elfcpp::R_X86_64_DTPOFF64
:
4472 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4473 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4474 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
4475 view
, address
, view_size
);
4478 case elfcpp::R_X86_64_SIZE32
:
4479 case elfcpp::R_X86_64_SIZE64
:
4481 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4482 _("unsupported reloc %u"),
4487 if (rstatus
== Reloc_funcs::RELOC_OVERFLOW
)
4491 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4492 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4493 _("relocation overflow: "
4494 "reference to local symbol %u in %s"),
4495 r_sym
, object
->name().c_str());
4497 else if (gsym
->is_defined() && gsym
->source() == Symbol::FROM_OBJECT
)
4499 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4500 _("relocation overflow: "
4501 "reference to '%s' defined in %s"),
4503 gsym
->object()->name().c_str());
4507 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4508 _("relocation overflow: reference to '%s'"),
4516 // Perform a TLS relocation.
4520 Target_x86_64
<size
>::Relocate::relocate_tls(
4521 const Relocate_info
<size
, false>* relinfo
,
4522 Target_x86_64
<size
>* target
,
4524 const elfcpp::Rela
<size
, false>& rela
,
4525 unsigned int r_type
,
4526 const Sized_symbol
<size
>* gsym
,
4527 const Symbol_value
<size
>* psymval
,
4528 unsigned char* view
,
4529 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4530 section_size_type view_size
)
4532 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
4534 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
4535 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4536 elfcpp::Shdr
<size
, false> data_shdr(relinfo
->data_shdr
);
4537 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
4539 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
4541 const bool is_final
= (gsym
== NULL
4542 ? !parameters
->options().shared()
4543 : gsym
->final_value_is_known());
4544 tls::Tls_optimization optimized_type
4545 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
4548 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4549 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
4551 // If this code sequence is used in a non-executable section,
4552 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
4553 // on the assumption that it's being used by itself in a debug
4554 // section. Therefore, in the unlikely event that the code
4555 // sequence appears in a non-executable section, we simply
4556 // leave it unoptimized.
4557 optimized_type
= tls::TLSOPT_NONE
;
4559 if (optimized_type
== tls::TLSOPT_TO_LE
)
4561 if (tls_segment
== NULL
)
4563 gold_assert(parameters
->errors()->error_count() > 0
4564 || issue_undefined_symbol_error(gsym
));
4567 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
4568 rela
, r_type
, value
, view
,
4574 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
4575 ? GOT_TYPE_TLS_OFFSET
4576 : GOT_TYPE_TLS_PAIR
);
4577 unsigned int got_offset
;
4580 gold_assert(gsym
->has_got_offset(got_type
));
4581 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
4585 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4586 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
4587 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
4588 - target
->got_size());
4590 if (optimized_type
== tls::TLSOPT_TO_IE
)
4592 value
= target
->got_plt_section()->address() + got_offset
;
4593 this->tls_gd_to_ie(relinfo
, relnum
, rela
, r_type
,
4594 value
, view
, address
, view_size
);
4597 else if (optimized_type
== tls::TLSOPT_NONE
)
4599 // Relocate the field with the offset of the pair of GOT
4601 value
= target
->got_plt_section()->address() + got_offset
;
4602 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
4607 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4608 _("unsupported reloc %u"), r_type
);
4611 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4612 case elfcpp::R_X86_64_TLSDESC_CALL
:
4613 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
4615 // See above comment for R_X86_64_TLSGD.
4616 optimized_type
= tls::TLSOPT_NONE
;
4618 if (optimized_type
== tls::TLSOPT_TO_LE
)
4620 if (tls_segment
== NULL
)
4622 gold_assert(parameters
->errors()->error_count() > 0
4623 || issue_undefined_symbol_error(gsym
));
4626 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
4627 rela
, r_type
, value
, view
,
4633 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
4634 ? GOT_TYPE_TLS_OFFSET
4635 : GOT_TYPE_TLS_DESC
);
4636 unsigned int got_offset
= 0;
4637 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
4638 && optimized_type
== tls::TLSOPT_NONE
)
4640 // We created GOT entries in the .got.tlsdesc portion of
4641 // the .got.plt section, but the offset stored in the
4642 // symbol is the offset within .got.tlsdesc.
4643 got_offset
= (target
->got_size()
4644 + target
->got_plt_section()->data_size());
4648 gold_assert(gsym
->has_got_offset(got_type
));
4649 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
4653 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4654 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
4655 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
4656 - target
->got_size());
4658 if (optimized_type
== tls::TLSOPT_TO_IE
)
4660 value
= target
->got_plt_section()->address() + got_offset
;
4661 this->tls_desc_gd_to_ie(relinfo
, relnum
,
4662 rela
, r_type
, value
, view
, address
,
4666 else if (optimized_type
== tls::TLSOPT_NONE
)
4668 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4670 // Relocate the field with the offset of the pair of GOT
4672 value
= target
->got_plt_section()->address() + got_offset
;
4673 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
4679 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4680 _("unsupported reloc %u"), r_type
);
4683 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4684 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
4686 // See above comment for R_X86_64_TLSGD.
4687 optimized_type
= tls::TLSOPT_NONE
;
4689 if (optimized_type
== tls::TLSOPT_TO_LE
)
4691 if (tls_segment
== NULL
)
4693 gold_assert(parameters
->errors()->error_count() > 0
4694 || issue_undefined_symbol_error(gsym
));
4697 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
4698 value
, view
, view_size
);
4701 else if (optimized_type
== tls::TLSOPT_NONE
)
4703 // Relocate the field with the offset of the GOT entry for
4704 // the module index.
4705 unsigned int got_offset
;
4706 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
4707 - target
->got_size());
4708 value
= target
->got_plt_section()->address() + got_offset
;
4709 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
4713 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4714 _("unsupported reloc %u"), r_type
);
4717 case elfcpp::R_X86_64_DTPOFF32
:
4718 // This relocation type is used in debugging information.
4719 // In that case we need to not optimize the value. If the
4720 // section is not executable, then we assume we should not
4721 // optimize this reloc. See comments above for R_X86_64_TLSGD,
4722 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
4724 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
4726 if (tls_segment
== NULL
)
4728 gold_assert(parameters
->errors()->error_count() > 0
4729 || issue_undefined_symbol_error(gsym
));
4732 value
-= tls_segment
->memsz();
4734 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
4737 case elfcpp::R_X86_64_DTPOFF64
:
4738 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
4739 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
4741 if (tls_segment
== NULL
)
4743 gold_assert(parameters
->errors()->error_count() > 0
4744 || issue_undefined_symbol_error(gsym
));
4747 value
-= tls_segment
->memsz();
4749 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
4752 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4754 && gsym
->is_undefined()
4755 && parameters
->options().output_is_executable())
4757 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
4759 r_type
, value
, view
,
4763 else if (optimized_type
== tls::TLSOPT_TO_LE
)
4765 if (tls_segment
== NULL
)
4767 gold_assert(parameters
->errors()->error_count() > 0
4768 || issue_undefined_symbol_error(gsym
));
4771 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
4773 r_type
, value
, view
,
4777 else if (optimized_type
== tls::TLSOPT_NONE
)
4779 // Relocate the field with the offset of the GOT entry for
4780 // the tp-relative offset of the symbol.
4781 unsigned int got_offset
;
4784 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
4785 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
4786 - target
->got_size());
4790 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4791 gold_assert(object
->local_has_got_offset(r_sym
,
4792 GOT_TYPE_TLS_OFFSET
));
4793 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
4794 - target
->got_size());
4796 value
= target
->got_plt_section()->address() + got_offset
;
4797 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
4801 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4802 _("unsupported reloc type %u"),
4806 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4807 if (tls_segment
== NULL
)
4809 gold_assert(parameters
->errors()->error_count() > 0
4810 || issue_undefined_symbol_error(gsym
));
4813 value
-= tls_segment
->memsz();
4814 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
4819 // Do a relocation in which we convert a TLS General-Dynamic to an
4824 Target_x86_64
<size
>::Relocate::tls_gd_to_ie(
4825 const Relocate_info
<size
, false>* relinfo
,
4827 const elfcpp::Rela
<size
, false>& rela
,
4829 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4830 unsigned char* view
,
4831 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4832 section_size_type view_size
)
4835 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4836 // .word 0x6666; rex64; call __tls_get_addr@PLT
4837 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4838 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4839 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4840 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4842 // leaq foo@tlsgd(%rip),%rdi;
4843 // .word 0x6666; rex64; call __tls_get_addr@PLT
4844 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4845 // leaq foo@tlsgd(%rip),%rdi;
4846 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4847 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4849 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
4850 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4851 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0
4852 || memcmp(view
+ 4, "\x66\x48\xff", 3) == 0));
4856 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4858 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4859 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
4860 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4865 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4867 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4868 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
4869 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4873 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4874 Relocate_functions
<size
, false>::pcrela32(view
+ 8, value
, addend
- 8,
4877 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4879 this->skip_call_tls_get_addr_
= true;
4882 // Do a relocation in which we convert a TLS General-Dynamic to a
4887 Target_x86_64
<size
>::Relocate::tls_gd_to_le(
4888 const Relocate_info
<size
, false>* relinfo
,
4890 Output_segment
* tls_segment
,
4891 const elfcpp::Rela
<size
, false>& rela
,
4893 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4894 unsigned char* view
,
4895 section_size_type view_size
)
4898 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4899 // .word 0x6666; rex64; call __tls_get_addr@PLT
4900 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4901 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4902 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4903 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4905 // leaq foo@tlsgd(%rip),%rdi;
4906 // .word 0x6666; rex64; call __tls_get_addr@PLT
4907 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4908 // leaq foo@tlsgd(%rip),%rdi;
4909 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4910 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4912 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
4913 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4914 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0
4915 || memcmp(view
+ 4, "\x66\x48\xff", 3) == 0));
4919 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4921 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4922 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
4923 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4928 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4930 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4931 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
4933 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4937 value
-= tls_segment
->memsz();
4938 Relocate_functions
<size
, false>::rela32(view
+ 8, value
, 0);
4940 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4942 this->skip_call_tls_get_addr_
= true;
4945 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4949 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_ie(
4950 const Relocate_info
<size
, false>* relinfo
,
4952 const elfcpp::Rela
<size
, false>& rela
,
4953 unsigned int r_type
,
4954 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4955 unsigned char* view
,
4956 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4957 section_size_type view_size
)
4959 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4961 // leaq foo@tlsdesc(%rip), %rax
4962 // ==> movq foo@gottpoff(%rip), %rax
4963 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4964 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4965 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4966 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
4968 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4969 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
4973 // call *foo@tlscall(%rax)
4975 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
4976 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
4977 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4978 view
[0] == 0xff && view
[1] == 0x10);
4984 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4988 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_le(
4989 const Relocate_info
<size
, false>* relinfo
,
4991 Output_segment
* tls_segment
,
4992 const elfcpp::Rela
<size
, false>& rela
,
4993 unsigned int r_type
,
4994 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4995 unsigned char* view
,
4996 section_size_type view_size
)
4998 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
5000 // leaq foo@tlsdesc(%rip), %rax
5001 // ==> movq foo@tpoff, %rax
5002 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
5003 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
5004 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5005 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
5008 value
-= tls_segment
->memsz();
5009 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
5013 // call *foo@tlscall(%rax)
5015 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
5016 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
5017 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5018 view
[0] == 0xff && view
[1] == 0x10);
5026 Target_x86_64
<size
>::Relocate::tls_ld_to_le(
5027 const Relocate_info
<size
, false>* relinfo
,
5030 const elfcpp::Rela
<size
, false>& rela
,
5032 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
5033 unsigned char* view
,
5034 section_size_type view_size
)
5036 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
5038 // ... leq foo@dtpoff(%rax),%reg
5039 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5041 // ... leq foo@dtpoff(%rax),%reg
5042 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5043 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
5045 // ... leq foo@dtpoff(%rax),%reg
5046 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5048 // ... leq foo@dtpoff(%rax),%reg
5049 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5051 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
5052 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
5054 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5055 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
5057 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5058 view
[4] == 0xe8 || view
[4] == 0xff);
5060 if (view
[4] == 0xe8)
5063 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
5065 memcpy(view
- 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
5070 memcpy(view
- 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
5073 memcpy(view
- 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
5077 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5079 this->skip_call_tls_get_addr_
= true;
5082 // Do a relocation in which we convert a TLS Initial-Exec to a
5087 Target_x86_64
<size
>::Relocate::tls_ie_to_le(
5088 const Relocate_info
<size
, false>* relinfo
,
5090 Output_segment
* tls_segment
,
5091 const elfcpp::Rela
<size
, false>& rela
,
5093 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
5094 unsigned char* view
,
5095 section_size_type view_size
)
5097 // We need to examine the opcodes to figure out which instruction we
5100 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
5101 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
5103 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
5104 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
5106 unsigned char op1
= view
[-3];
5107 unsigned char op2
= view
[-2];
5108 unsigned char op3
= view
[-1];
5109 unsigned char reg
= op3
>> 3;
5116 else if (size
== 32 && op1
== 0x44)
5119 view
[-1] = 0xc0 | reg
;
5123 // Special handling for %rsp.
5126 else if (size
== 32 && op1
== 0x44)
5129 view
[-1] = 0xc0 | reg
;
5136 else if (size
== 32 && op1
== 0x44)
5139 view
[-1] = 0x80 | reg
| (reg
<< 3);
5142 if (tls_segment
!= NULL
)
5143 value
-= tls_segment
->memsz();
5144 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
5147 // Relocate section data.
5151 Target_x86_64
<size
>::relocate_section(
5152 const Relocate_info
<size
, false>* relinfo
,
5153 unsigned int sh_type
,
5154 const unsigned char* prelocs
,
5156 Output_section
* output_section
,
5157 bool needs_special_offset_handling
,
5158 unsigned char* view
,
5159 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5160 section_size_type view_size
,
5161 const Reloc_symbol_changes
* reloc_symbol_changes
)
5163 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5166 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5168 gold::relocate_section
<size
, false, Target_x86_64
<size
>, Relocate
,
5169 gold::Default_comdat_behavior
, Classify_reloc
>(
5175 needs_special_offset_handling
,
5179 reloc_symbol_changes
);
5182 // Apply an incremental relocation. Incremental relocations always refer
5183 // to global symbols.
5187 Target_x86_64
<size
>::apply_relocation(
5188 const Relocate_info
<size
, false>* relinfo
,
5189 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
5190 unsigned int r_type
,
5191 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
5193 unsigned char* view
,
5194 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5195 section_size_type view_size
)
5197 gold::apply_relocation
<size
, false, Target_x86_64
<size
>,
5198 typename Target_x86_64
<size
>::Relocate
>(
5210 // Scan the relocs during a relocatable link.
5214 Target_x86_64
<size
>::scan_relocatable_relocs(
5215 Symbol_table
* symtab
,
5217 Sized_relobj_file
<size
, false>* object
,
5218 unsigned int data_shndx
,
5219 unsigned int sh_type
,
5220 const unsigned char* prelocs
,
5222 Output_section
* output_section
,
5223 bool needs_special_offset_handling
,
5224 size_t local_symbol_count
,
5225 const unsigned char* plocal_symbols
,
5226 Relocatable_relocs
* rr
)
5228 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5230 typedef gold::Default_scan_relocatable_relocs
<Classify_reloc
>
5231 Scan_relocatable_relocs
;
5233 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5235 gold::scan_relocatable_relocs
<size
, false, Scan_relocatable_relocs
>(
5243 needs_special_offset_handling
,
5249 // Scan the relocs for --emit-relocs.
5253 Target_x86_64
<size
>::emit_relocs_scan(
5254 Symbol_table
* symtab
,
5256 Sized_relobj_file
<size
, false>* object
,
5257 unsigned int data_shndx
,
5258 unsigned int sh_type
,
5259 const unsigned char* prelocs
,
5261 Output_section
* output_section
,
5262 bool needs_special_offset_handling
,
5263 size_t local_symbol_count
,
5264 const unsigned char* plocal_syms
,
5265 Relocatable_relocs
* rr
)
5267 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5269 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
5270 Emit_relocs_strategy
;
5272 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5274 gold::scan_relocatable_relocs
<size
, false, Emit_relocs_strategy
>(
5282 needs_special_offset_handling
,
5288 // Relocate a section during a relocatable link.
5292 Target_x86_64
<size
>::relocate_relocs(
5293 const Relocate_info
<size
, false>* relinfo
,
5294 unsigned int sh_type
,
5295 const unsigned char* prelocs
,
5297 Output_section
* output_section
,
5298 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
5299 unsigned char* view
,
5300 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
5301 section_size_type view_size
,
5302 unsigned char* reloc_view
,
5303 section_size_type reloc_view_size
)
5305 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5308 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5310 gold::relocate_relocs
<size
, false, Classify_reloc
>(
5315 offset_in_output_section
,
5323 // Return the value to use for a dynamic which requires special
5324 // treatment. This is how we support equality comparisons of function
5325 // pointers across shared library boundaries, as described in the
5326 // processor specific ABI supplement.
5330 Target_x86_64
<size
>::do_dynsym_value(const Symbol
* gsym
) const
5332 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
5333 return this->plt_address_for_global(gsym
);
5336 // Return a string used to fill a code section with nops to take up
5337 // the specified length.
5341 Target_x86_64
<size
>::do_code_fill(section_size_type length
) const
5345 // Build a jmpq instruction to skip over the bytes.
5346 unsigned char jmp
[5];
5348 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
5349 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
5350 + std::string(length
- 5, static_cast<char>(0x90)));
5353 // Nop sequences of various lengths.
5354 const char nop1
[1] = { '\x90' }; // nop
5355 const char nop2
[2] = { '\x66', '\x90' }; // xchg %ax %ax
5356 const char nop3
[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5357 const char nop4
[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5359 const char nop5
[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5361 const char nop6
[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5362 '\x44', '\x00', '\x00' };
5363 const char nop7
[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5364 '\x00', '\x00', '\x00',
5366 const char nop8
[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5367 '\x00', '\x00', '\x00',
5369 const char nop9
[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5370 '\x84', '\x00', '\x00',
5371 '\x00', '\x00', '\x00' };
5372 const char nop10
[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5373 '\x1f', '\x84', '\x00',
5374 '\x00', '\x00', '\x00',
5376 const char nop11
[11] = { '\x66', '\x66', '\x2e', // data16
5377 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5378 '\x00', '\x00', '\x00',
5380 const char nop12
[12] = { '\x66', '\x66', '\x66', // data16; data16
5381 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5382 '\x84', '\x00', '\x00',
5383 '\x00', '\x00', '\x00' };
5384 const char nop13
[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5385 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5386 '\x1f', '\x84', '\x00',
5387 '\x00', '\x00', '\x00',
5389 const char nop14
[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5390 '\x66', '\x66', '\x2e', // data16
5391 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5392 '\x00', '\x00', '\x00',
5394 const char nop15
[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5395 '\x66', '\x66', '\x66', // data16; data16
5396 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5397 '\x84', '\x00', '\x00',
5398 '\x00', '\x00', '\x00' };
5400 const char* nops
[16] = {
5402 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
5403 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
5406 return std::string(nops
[length
], length
);
5409 // Return the addend to use for a target specific relocation. The
5410 // only target specific relocation is R_X86_64_TLSDESC for a local
5411 // symbol. We want to set the addend is the offset of the local
5412 // symbol in the TLS segment.
5416 Target_x86_64
<size
>::do_reloc_addend(void* arg
, unsigned int r_type
,
5419 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
5420 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
5421 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
5422 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
5423 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
5424 gold_assert(psymval
->is_tls_symbol());
5425 // The value of a TLS symbol is the offset in the TLS segment.
5426 return psymval
->value(ti
.object
, 0);
5429 // Return the value to use for the base of a DW_EH_PE_datarel offset
5430 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
5431 // assembler can not write out the difference between two labels in
5432 // different sections, so instead of using a pc-relative value they
5433 // use an offset from the GOT.
5437 Target_x86_64
<size
>::do_ehframe_datarel_base() const
5439 gold_assert(this->global_offset_table_
!= NULL
);
5440 Symbol
* sym
= this->global_offset_table_
;
5441 Sized_symbol
<size
>* ssym
= static_cast<Sized_symbol
<size
>*>(sym
);
5442 return ssym
->value();
5445 // FNOFFSET in section SHNDX in OBJECT is the start of a function
5446 // compiled with -fsplit-stack. The function calls non-split-stack
5447 // code. We have to change the function so that it always ensures
5448 // that it has enough stack space to run some random function.
5450 static const unsigned char cmp_insn_32
[] = { 0x64, 0x3b, 0x24, 0x25 };
5451 static const unsigned char lea_r10_insn_32
[] = { 0x44, 0x8d, 0x94, 0x24 };
5452 static const unsigned char lea_r11_insn_32
[] = { 0x44, 0x8d, 0x9c, 0x24 };
5454 static const unsigned char cmp_insn_64
[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
5455 static const unsigned char lea_r10_insn_64
[] = { 0x4c, 0x8d, 0x94, 0x24 };
5456 static const unsigned char lea_r11_insn_64
[] = { 0x4c, 0x8d, 0x9c, 0x24 };
5460 Target_x86_64
<size
>::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
5461 section_offset_type fnoffset
,
5462 section_size_type fnsize
,
5463 const unsigned char*,
5465 unsigned char* view
,
5466 section_size_type view_size
,
5468 std::string
* to
) const
5470 const char* const cmp_insn
= reinterpret_cast<const char*>
5471 (size
== 32 ? cmp_insn_32
: cmp_insn_64
);
5472 const char* const lea_r10_insn
= reinterpret_cast<const char*>
5473 (size
== 32 ? lea_r10_insn_32
: lea_r10_insn_64
);
5474 const char* const lea_r11_insn
= reinterpret_cast<const char*>
5475 (size
== 32 ? lea_r11_insn_32
: lea_r11_insn_64
);
5477 const size_t cmp_insn_len
=
5478 (size
== 32 ? sizeof(cmp_insn_32
) : sizeof(cmp_insn_64
));
5479 const size_t lea_r10_insn_len
=
5480 (size
== 32 ? sizeof(lea_r10_insn_32
) : sizeof(lea_r10_insn_64
));
5481 const size_t lea_r11_insn_len
=
5482 (size
== 32 ? sizeof(lea_r11_insn_32
) : sizeof(lea_r11_insn_64
));
5483 const size_t nop_len
= (size
== 32 ? 7 : 8);
5485 // The function starts with a comparison of the stack pointer and a
5486 // field in the TCB. This is followed by a jump.
5489 if (this->match_view(view
, view_size
, fnoffset
, cmp_insn
, cmp_insn_len
)
5490 && fnsize
> nop_len
+ 1)
5492 // We will call __morestack if the carry flag is set after this
5493 // comparison. We turn the comparison into an stc instruction
5495 view
[fnoffset
] = '\xf9';
5496 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, nop_len
);
5498 // lea NN(%rsp),%r10
5499 // lea NN(%rsp),%r11
5500 else if ((this->match_view(view
, view_size
, fnoffset
,
5501 lea_r10_insn
, lea_r10_insn_len
)
5502 || this->match_view(view
, view_size
, fnoffset
,
5503 lea_r11_insn
, lea_r11_insn_len
))
5506 // This is loading an offset from the stack pointer for a
5507 // comparison. The offset is negative, so we decrease the
5508 // offset by the amount of space we need for the stack. This
5509 // means we will avoid calling __morestack if there happens to
5510 // be plenty of space on the stack already.
5511 unsigned char* pval
= view
+ fnoffset
+ 4;
5512 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
5513 val
-= parameters
->options().split_stack_adjust_size();
5514 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
5518 if (!object
->has_no_split_stack())
5519 object
->error(_("failed to match split-stack sequence at "
5520 "section %u offset %0zx"),
5521 shndx
, static_cast<size_t>(fnoffset
));
5525 // We have to change the function so that it calls
5526 // __morestack_non_split instead of __morestack. The former will
5527 // allocate additional stack space.
5528 *from
= "__morestack";
5529 *to
= "__morestack_non_split";
5532 // The selector for x86_64 object files. Note this is never instantiated
5533 // directly. It's only used in Target_selector_x86_64_nacl, below.
5536 class Target_selector_x86_64
: public Target_selector_freebsd
5539 Target_selector_x86_64()
5540 : Target_selector_freebsd(elfcpp::EM_X86_64
, size
, false,
5542 ? "elf64-x86-64" : "elf32-x86-64"),
5544 ? "elf64-x86-64-freebsd"
5545 : "elf32-x86-64-freebsd"),
5546 (size
== 64 ? "elf_x86_64" : "elf32_x86_64"))
5550 do_instantiate_target()
5551 { return new Target_x86_64
<size
>(); }
5555 // NaCl variant. It uses different PLT contents.
5558 class Output_data_plt_x86_64_nacl
: public Output_data_plt_x86_64
<size
>
5561 Output_data_plt_x86_64_nacl(Layout
* layout
,
5562 Output_data_got
<64, false>* got
,
5563 Output_data_got_plt_x86_64
* got_plt
,
5564 Output_data_space
* got_irelative
)
5565 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
5566 got
, got_plt
, got_irelative
)
5569 Output_data_plt_x86_64_nacl(Layout
* layout
,
5570 Output_data_got
<64, false>* got
,
5571 Output_data_got_plt_x86_64
* got_plt
,
5572 Output_data_space
* got_irelative
,
5573 unsigned int plt_count
)
5574 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
5575 got
, got_plt
, got_irelative
,
5580 virtual unsigned int
5581 do_get_plt_entry_size() const
5582 { return plt_entry_size
; }
5585 do_add_eh_frame(Layout
* layout
)
5587 layout
->add_eh_frame_for_plt(this,
5588 this->plt_eh_frame_cie
,
5589 this->plt_eh_frame_cie_size
,
5591 plt_eh_frame_fde_size
);
5595 do_fill_first_plt_entry(unsigned char* pov
,
5596 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
5597 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
5599 virtual unsigned int
5600 do_fill_plt_entry(unsigned char* pov
,
5601 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5602 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
5603 unsigned int got_offset
,
5604 unsigned int plt_offset
,
5605 unsigned int plt_index
);
5608 do_fill_tlsdesc_entry(unsigned char* pov
,
5609 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5610 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
5611 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
5612 unsigned int tlsdesc_got_offset
,
5613 unsigned int plt_offset
);
5616 // The size of an entry in the PLT.
5617 static const int plt_entry_size
= 64;
5619 // The first entry in the PLT.
5620 static const unsigned char first_plt_entry
[plt_entry_size
];
5622 // Other entries in the PLT for an executable.
5623 static const unsigned char plt_entry
[plt_entry_size
];
5625 // The reserved TLSDESC entry in the PLT for an executable.
5626 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
5628 // The .eh_frame unwind information for the PLT.
5629 static const int plt_eh_frame_fde_size
= 32;
5630 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
5634 class Target_x86_64_nacl
: public Target_x86_64
<size
>
5637 Target_x86_64_nacl()
5638 : Target_x86_64
<size
>(&x86_64_nacl_info
)
5641 virtual Output_data_plt_x86_64
<size
>*
5642 do_make_data_plt(Layout
* layout
,
5643 Output_data_got
<64, false>* got
,
5644 Output_data_got_plt_x86_64
* got_plt
,
5645 Output_data_space
* got_irelative
)
5647 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
5651 virtual Output_data_plt_x86_64
<size
>*
5652 do_make_data_plt(Layout
* layout
,
5653 Output_data_got
<64, false>* got
,
5654 Output_data_got_plt_x86_64
* got_plt
,
5655 Output_data_space
* got_irelative
,
5656 unsigned int plt_count
)
5658 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
5664 do_code_fill(section_size_type length
) const;
5667 static const Target::Target_info x86_64_nacl_info
;
5671 const Target::Target_info Target_x86_64_nacl
<64>::x86_64_nacl_info
=
5674 false, // is_big_endian
5675 elfcpp::EM_X86_64
, // machine_code
5676 false, // has_make_symbol
5677 false, // has_resolve
5678 true, // has_code_fill
5679 true, // is_default_stack_executable
5680 true, // can_icf_inline_merge_sections
5682 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
5683 0x20000, // default_text_segment_address
5684 0x10000, // abi_pagesize (overridable by -z max-page-size)
5685 0x10000, // common_pagesize (overridable by -z common-page-size)
5686 true, // isolate_execinstr
5687 0x10000000, // rosegment_gap
5688 elfcpp::SHN_UNDEF
, // small_common_shndx
5689 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
5690 0, // small_common_section_flags
5691 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
5692 NULL
, // attributes_section
5693 NULL
, // attributes_vendor
5694 "_start", // entry_symbol_name
5695 32, // hash_entry_size
5696 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
5700 const Target::Target_info Target_x86_64_nacl
<32>::x86_64_nacl_info
=
5703 false, // is_big_endian
5704 elfcpp::EM_X86_64
, // machine_code
5705 false, // has_make_symbol
5706 false, // has_resolve
5707 true, // has_code_fill
5708 true, // is_default_stack_executable
5709 true, // can_icf_inline_merge_sections
5711 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
5712 0x20000, // default_text_segment_address
5713 0x10000, // abi_pagesize (overridable by -z max-page-size)
5714 0x10000, // common_pagesize (overridable by -z common-page-size)
5715 true, // isolate_execinstr
5716 0x10000000, // rosegment_gap
5717 elfcpp::SHN_UNDEF
, // small_common_shndx
5718 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
5719 0, // small_common_section_flags
5720 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
5721 NULL
, // attributes_section
5722 NULL
, // attributes_vendor
5723 "_start", // entry_symbol_name
5724 32, // hash_entry_size
5725 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
5728 #define NACLMASK 0xe0 // 32-byte alignment mask.
5730 // The first entry in the PLT.
5734 Output_data_plt_x86_64_nacl
<size
>::first_plt_entry
[plt_entry_size
] =
5736 0xff, 0x35, // pushq contents of memory address
5737 0, 0, 0, 0, // replaced with address of .got + 8
5738 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
5739 0, 0, 0, 0, // replaced with address of .got + 16
5740 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
5741 0x4d, 0x01, 0xfb, // add %r15, %r11
5742 0x41, 0xff, 0xe3, // jmpq *%r11
5744 // 9-byte nop sequence to pad out to the next 32-byte boundary.
5745 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
5747 // 32 bytes of nop to pad out to the standard size
5748 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5749 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5750 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5751 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5752 0x66, // excess data32 prefix
5758 Output_data_plt_x86_64_nacl
<size
>::do_fill_first_plt_entry(
5760 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5761 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
5763 memcpy(pov
, first_plt_entry
, plt_entry_size
);
5764 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
5766 - (plt_address
+ 2 + 4)));
5767 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
5769 - (plt_address
+ 9 + 4)));
5772 // Subsequent entries in the PLT.
5776 Output_data_plt_x86_64_nacl
<size
>::plt_entry
[plt_entry_size
] =
5778 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
5779 0, 0, 0, 0, // replaced with address of symbol in .got
5780 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
5781 0x4d, 0x01, 0xfb, // add %r15, %r11
5782 0x41, 0xff, 0xe3, // jmpq *%r11
5784 // 15-byte nop sequence to pad out to the next 32-byte boundary.
5785 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5786 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5788 // Lazy GOT entries point here (32-byte aligned).
5789 0x68, // pushq immediate
5790 0, 0, 0, 0, // replaced with index into relocation table
5791 0xe9, // jmp relative
5792 0, 0, 0, 0, // replaced with offset to start of .plt0
5794 // 22 bytes of nop to pad out to the standard size.
5795 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5796 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5797 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
5802 Output_data_plt_x86_64_nacl
<size
>::do_fill_plt_entry(
5804 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5805 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
5806 unsigned int got_offset
,
5807 unsigned int plt_offset
,
5808 unsigned int plt_index
)
5810 memcpy(pov
, plt_entry
, plt_entry_size
);
5811 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3,
5812 (got_address
+ got_offset
5813 - (plt_address
+ plt_offset
5816 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 33, plt_index
);
5817 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 38,
5818 - (plt_offset
+ 38 + 4));
5823 // The reserved TLSDESC entry in the PLT.
5827 Output_data_plt_x86_64_nacl
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
5829 0xff, 0x35, // pushq x(%rip)
5830 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
5831 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
5832 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
5833 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
5834 0x4d, 0x01, 0xfb, // add %r15, %r11
5835 0x41, 0xff, 0xe3, // jmpq *%r11
5837 // 41 bytes of nop to pad out to the standard size.
5838 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5839 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5840 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5841 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5842 0x66, 0x66, // excess data32 prefixes
5843 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5848 Output_data_plt_x86_64_nacl
<size
>::do_fill_tlsdesc_entry(
5850 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5851 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
5852 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
5853 unsigned int tlsdesc_got_offset
,
5854 unsigned int plt_offset
)
5856 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
5857 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
5859 - (plt_address
+ plt_offset
5861 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
5863 + tlsdesc_got_offset
5864 - (plt_address
+ plt_offset
5868 // The .eh_frame unwind information for the PLT.
5872 Output_data_plt_x86_64_nacl
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
5874 0, 0, 0, 0, // Replaced with offset to .plt.
5875 0, 0, 0, 0, // Replaced with size of .plt.
5876 0, // Augmentation size.
5877 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
5878 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
5879 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
5880 elfcpp::DW_CFA_advance_loc
+ 58, // Advance 58 to __PLT__ + 64.
5881 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
5882 13, // Block length.
5883 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
5884 elfcpp::DW_OP_breg16
, 0, // Push %rip.
5885 elfcpp::DW_OP_const1u
, 63, // Push 0x3f.
5886 elfcpp::DW_OP_and
, // & (%rip & 0x3f).
5887 elfcpp::DW_OP_const1u
, 37, // Push 0x25.
5888 elfcpp::DW_OP_ge
, // >= ((%rip & 0x3f) >= 0x25)
5889 elfcpp::DW_OP_lit3
, // Push 3.
5890 elfcpp::DW_OP_shl
, // << (((%rip & 0x3f) >= 0x25) << 3)
5891 elfcpp::DW_OP_plus
, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5892 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
5896 // Return a string used to fill a code section with nops.
5897 // For NaCl, long NOPs are only valid if they do not cross
5898 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5901 Target_x86_64_nacl
<size
>::do_code_fill(section_size_type length
) const
5903 return std::string(length
, static_cast<char>(0x90));
5906 // The selector for x86_64-nacl object files.
5909 class Target_selector_x86_64_nacl
5910 : public Target_selector_nacl
<Target_selector_x86_64
<size
>,
5911 Target_x86_64_nacl
<size
> >
5914 Target_selector_x86_64_nacl()
5915 : Target_selector_nacl
<Target_selector_x86_64
<size
>,
5916 Target_x86_64_nacl
<size
> >("x86-64",
5918 ? "elf64-x86-64-nacl"
5919 : "elf32-x86-64-nacl",
5922 : "elf32_x86_64_nacl")
5926 Target_selector_x86_64_nacl
<64> target_selector_x86_64
;
5927 Target_selector_x86_64_nacl
<32> target_selector_x32
;
5929 } // End anonymous namespace.