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)
596 // Hook for a new output section.
598 do_new_output_section(Output_section
*) const;
600 // Scan the relocations to look for symbol adjustments.
602 gc_process_relocs(Symbol_table
* symtab
,
604 Sized_relobj_file
<size
, false>* object
,
605 unsigned int data_shndx
,
606 unsigned int sh_type
,
607 const unsigned char* prelocs
,
609 Output_section
* output_section
,
610 bool needs_special_offset_handling
,
611 size_t local_symbol_count
,
612 const unsigned char* plocal_symbols
);
614 // Scan the relocations to look for symbol adjustments.
616 scan_relocs(Symbol_table
* symtab
,
618 Sized_relobj_file
<size
, false>* object
,
619 unsigned int data_shndx
,
620 unsigned int sh_type
,
621 const unsigned char* prelocs
,
623 Output_section
* output_section
,
624 bool needs_special_offset_handling
,
625 size_t local_symbol_count
,
626 const unsigned char* plocal_symbols
);
628 // Finalize the sections.
630 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
632 // Return the value to use for a dynamic which requires special
635 do_dynsym_value(const Symbol
*) const;
637 // Relocate a section.
639 relocate_section(const Relocate_info
<size
, false>*,
640 unsigned int sh_type
,
641 const unsigned char* prelocs
,
643 Output_section
* output_section
,
644 bool needs_special_offset_handling
,
646 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
647 section_size_type view_size
,
648 const Reloc_symbol_changes
*);
650 // Scan the relocs during a relocatable link.
652 scan_relocatable_relocs(Symbol_table
* symtab
,
654 Sized_relobj_file
<size
, false>* object
,
655 unsigned int data_shndx
,
656 unsigned int sh_type
,
657 const unsigned char* prelocs
,
659 Output_section
* output_section
,
660 bool needs_special_offset_handling
,
661 size_t local_symbol_count
,
662 const unsigned char* plocal_symbols
,
663 Relocatable_relocs
*);
665 // Scan the relocs for --emit-relocs.
667 emit_relocs_scan(Symbol_table
* symtab
,
669 Sized_relobj_file
<size
, false>* object
,
670 unsigned int data_shndx
,
671 unsigned int sh_type
,
672 const unsigned char* prelocs
,
674 Output_section
* output_section
,
675 bool needs_special_offset_handling
,
676 size_t local_symbol_count
,
677 const unsigned char* plocal_syms
,
678 Relocatable_relocs
* rr
);
680 // Emit relocations for a section.
683 const Relocate_info
<size
, false>*,
684 unsigned int sh_type
,
685 const unsigned char* prelocs
,
687 Output_section
* output_section
,
688 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
690 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
691 section_size_type view_size
,
692 unsigned char* reloc_view
,
693 section_size_type reloc_view_size
);
695 // Return a string used to fill a code section with nops.
697 do_code_fill(section_size_type length
) const;
699 // Return whether SYM is defined by the ABI.
701 do_is_defined_by_abi(const Symbol
* sym
) const
702 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
704 // Return the symbol index to use for a target specific relocation.
705 // The only target specific relocation is R_X86_64_TLSDESC for a
706 // local symbol, which is an absolute reloc.
708 do_reloc_symbol_index(void*, unsigned int r_type
) const
710 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
714 // Return the addend to use for a target specific relocation.
716 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
718 // Return the PLT section.
720 do_plt_address_for_global(const Symbol
* gsym
) const
721 { return this->plt_section()->address_for_global(gsym
); }
724 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
725 { return this->plt_section()->address_for_local(relobj
, symndx
); }
727 // This function should be defined in targets that can use relocation
728 // types to determine (implemented in local_reloc_may_be_function_pointer
729 // and global_reloc_may_be_function_pointer)
730 // if a function's pointer is taken. ICF uses this in safe mode to only
731 // fold those functions whose pointer is defintely not taken. For x86_64
732 // pie binaries, safe ICF cannot be done by looking at only relocation
733 // types, and for certain cases (e.g. R_X86_64_PC32), the instruction
734 // opcode is checked as well to distinguish a function call from taking
735 // a function's pointer.
737 do_can_check_for_function_pointers() const
740 // Return the base for a DW_EH_PE_datarel encoding.
742 do_ehframe_datarel_base() const;
744 // Adjust -fsplit-stack code which calls non-split-stack code.
746 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
747 section_offset_type fnoffset
, section_size_type fnsize
,
748 const unsigned char* prelocs
, size_t reloc_count
,
749 unsigned char* view
, section_size_type view_size
,
750 std::string
* from
, std::string
* to
) const;
752 // Return the size of the GOT section.
756 gold_assert(this->got_
!= NULL
);
757 return this->got_
->data_size();
760 // Return the number of entries in the GOT.
762 got_entry_count() const
764 if (this->got_
== NULL
)
766 return this->got_size() / 8;
769 // Return the number of entries in the PLT.
771 plt_entry_count() const;
773 // Return the offset of the first non-reserved PLT entry.
775 first_plt_entry_offset() const;
777 // Return the size of each PLT entry.
779 plt_entry_size() const;
781 // Return the size of each GOT entry.
783 got_entry_size() const
786 // Create the GOT section for an incremental update.
787 Output_data_got_base
*
788 init_got_plt_for_update(Symbol_table
* symtab
,
790 unsigned int got_count
,
791 unsigned int plt_count
);
793 // Reserve a GOT entry for a local symbol, and regenerate any
794 // necessary dynamic relocations.
796 reserve_local_got_entry(unsigned int got_index
,
797 Sized_relobj
<size
, false>* obj
,
799 unsigned int got_type
);
801 // Reserve a GOT entry for a global symbol, and regenerate any
802 // necessary dynamic relocations.
804 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
805 unsigned int got_type
);
807 // Register an existing PLT entry for a global symbol.
809 register_global_plt_entry(Symbol_table
*, Layout
*, unsigned int plt_index
,
812 // Force a COPY relocation for a given symbol.
814 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
816 // Apply an incremental relocation.
818 apply_relocation(const Relocate_info
<size
, false>* relinfo
,
819 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
821 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
824 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
825 section_size_type view_size
);
827 // Add a new reloc argument, returning the index in the vector.
829 add_tlsdesc_info(Sized_relobj_file
<size
, false>* object
, unsigned int r_sym
)
831 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
832 return this->tlsdesc_reloc_info_
.size() - 1;
835 Output_data_plt_x86_64
<size
>*
836 make_data_plt(Layout
* layout
,
837 Output_data_got
<64, false>* got
,
838 Output_data_got_plt_x86_64
* got_plt
,
839 Output_data_space
* got_irelative
)
841 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
844 Output_data_plt_x86_64
<size
>*
845 make_data_plt(Layout
* layout
,
846 Output_data_got
<64, false>* got
,
847 Output_data_got_plt_x86_64
* got_plt
,
848 Output_data_space
* got_irelative
,
849 unsigned int plt_count
)
851 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
,
855 virtual Output_data_plt_x86_64
<size
>*
856 do_make_data_plt(Layout
* layout
,
857 Output_data_got
<64, false>* got
,
858 Output_data_got_plt_x86_64
* got_plt
,
859 Output_data_space
* got_irelative
);
861 virtual Output_data_plt_x86_64
<size
>*
862 do_make_data_plt(Layout
* layout
,
863 Output_data_got
<64, false>* got
,
864 Output_data_got_plt_x86_64
* got_plt
,
865 Output_data_space
* got_irelative
,
866 unsigned int plt_count
);
869 // The class which scans relocations.
874 : issued_non_pic_error_(false)
878 get_reference_flags(unsigned int r_type
);
881 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
882 Sized_relobj_file
<size
, false>* object
,
883 unsigned int data_shndx
,
884 Output_section
* output_section
,
885 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
886 const elfcpp::Sym
<size
, false>& lsym
,
890 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
891 Sized_relobj_file
<size
, false>* object
,
892 unsigned int data_shndx
,
893 Output_section
* output_section
,
894 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
898 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
899 Target_x86_64
* target
,
900 Sized_relobj_file
<size
, false>* object
,
901 unsigned int data_shndx
,
902 Output_section
* output_section
,
903 const elfcpp::Rela
<size
, false>& reloc
,
905 const elfcpp::Sym
<size
, false>& lsym
);
908 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
909 Target_x86_64
* target
,
910 Sized_relobj_file
<size
, false>* object
,
911 unsigned int data_shndx
,
912 Output_section
* output_section
,
913 const elfcpp::Rela
<size
, false>& reloc
,
919 unsupported_reloc_local(Sized_relobj_file
<size
, false>*,
920 unsigned int r_type
);
923 unsupported_reloc_global(Sized_relobj_file
<size
, false>*,
924 unsigned int r_type
, Symbol
*);
927 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
930 possible_function_pointer_reloc(Sized_relobj_file
<size
, false>* src_obj
,
931 unsigned int src_indx
,
932 unsigned int r_offset
,
933 unsigned int r_type
);
936 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, false>*,
937 unsigned int r_type
);
939 // Whether we have issued an error about a non-PIC compilation.
940 bool issued_non_pic_error_
;
943 // The class which implements relocation.
948 : skip_call_tls_get_addr_(false)
953 if (this->skip_call_tls_get_addr_
)
955 // FIXME: This needs to specify the location somehow.
956 gold_error(_("missing expected TLS relocation"));
960 // Do a relocation. Return false if the caller should not issue
961 // any warnings about this relocation.
963 relocate(const Relocate_info
<size
, false>*, unsigned int,
964 Target_x86_64
*, Output_section
*, size_t, const unsigned char*,
965 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
966 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
970 // Do a TLS relocation.
972 relocate_tls(const Relocate_info
<size
, false>*, Target_x86_64
*,
973 size_t relnum
, const elfcpp::Rela
<size
, false>&,
974 unsigned int r_type
, const Sized_symbol
<size
>*,
975 const Symbol_value
<size
>*,
976 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
979 // Do a TLS General-Dynamic to Initial-Exec transition.
981 tls_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
982 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
983 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
985 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
986 section_size_type view_size
);
988 // Do a TLS General-Dynamic to Local-Exec transition.
990 tls_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
991 Output_segment
* tls_segment
,
992 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
993 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
995 section_size_type view_size
);
997 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
999 tls_desc_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
1000 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1001 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1002 unsigned char* view
,
1003 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1004 section_size_type view_size
);
1006 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
1008 tls_desc_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1009 Output_segment
* tls_segment
,
1010 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1011 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1012 unsigned char* view
,
1013 section_size_type view_size
);
1015 // Do a TLS Local-Dynamic to Local-Exec transition.
1017 tls_ld_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1018 Output_segment
* tls_segment
,
1019 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1020 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1021 unsigned char* view
,
1022 section_size_type view_size
);
1024 // Do a TLS Initial-Exec to Local-Exec transition.
1026 tls_ie_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1027 Output_segment
* tls_segment
,
1028 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1029 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1030 unsigned char* view
,
1031 section_size_type view_size
);
1033 // This is set if we should skip the next reloc, which should be a
1034 // PLT32 reloc against ___tls_get_addr.
1035 bool skip_call_tls_get_addr_
;
1038 // Check if relocation against this symbol is a candidate for
1040 // mov foo@GOTPCREL(%rip), %reg
1041 // to lea foo(%rip), %reg.
1042 template<class View_type
>
1044 can_convert_mov_to_lea(const Symbol
* gsym
, unsigned int r_type
,
1045 size_t r_offset
, View_type
* view
)
1047 gold_assert(gsym
!= NULL
);
1048 // We cannot do the conversion unless it's one of these relocations.
1049 if (r_type
!= elfcpp::R_X86_64_GOTPCREL
1050 && r_type
!= elfcpp::R_X86_64_GOTPCRELX
1051 && r_type
!= elfcpp::R_X86_64_REX_GOTPCRELX
)
1053 // We cannot convert references to IFUNC symbols, or to symbols that
1054 // are not local to the current module.
1055 // We can't do predefined symbols because they may become undefined
1056 // (e.g., __ehdr_start when the headers aren't mapped to a segment).
1057 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1058 || gsym
->is_undefined()
1059 || gsym
->is_predefined()
1060 || gsym
->is_from_dynobj()
1061 || gsym
->is_preemptible())
1063 // If we are building a shared object and the symbol is protected, we may
1064 // need to go through the GOT.
1065 if (parameters
->options().shared()
1066 && gsym
->visibility() == elfcpp::STV_PROTECTED
)
1068 // We cannot convert references to the _DYNAMIC symbol.
1069 if (strcmp(gsym
->name(), "_DYNAMIC") == 0)
1071 // Check for a MOV opcode.
1072 return (*view
)[r_offset
- 2] == 0x8b;
1076 // callq *foo@GOTPCRELX(%rip) to
1078 // and jmpq *foo@GOTPCRELX(%rip) to
1081 template<class View_type
>
1083 can_convert_callq_to_direct(const Symbol
* gsym
, unsigned int r_type
,
1084 size_t r_offset
, View_type
* view
)
1086 gold_assert(gsym
!= NULL
);
1087 // We cannot do the conversion unless it's a GOTPCRELX relocation.
1088 if (r_type
!= elfcpp::R_X86_64_GOTPCRELX
)
1090 // We cannot convert references to IFUNC symbols, or to symbols that
1091 // are not local to the current module.
1092 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1093 || gsym
->is_undefined ()
1094 || gsym
->is_from_dynobj()
1095 || gsym
->is_preemptible())
1097 // Check for a CALLQ or JMPQ opcode.
1098 return ((*view
)[r_offset
- 2] == 0xff
1099 && ((*view
)[r_offset
- 1] == 0x15
1100 || (*view
)[r_offset
- 1] == 0x25));
1103 // Adjust TLS relocation type based on the options and whether this
1104 // is a local symbol.
1105 static tls::Tls_optimization
1106 optimize_tls_reloc(bool is_final
, int r_type
);
1108 // Get the GOT section, creating it if necessary.
1109 Output_data_got
<64, false>*
1110 got_section(Symbol_table
*, Layout
*);
1112 // Get the GOT PLT section.
1113 Output_data_got_plt_x86_64
*
1114 got_plt_section() const
1116 gold_assert(this->got_plt_
!= NULL
);
1117 return this->got_plt_
;
1120 // Get the GOT section for TLSDESC entries.
1121 Output_data_got
<64, false>*
1122 got_tlsdesc_section() const
1124 gold_assert(this->got_tlsdesc_
!= NULL
);
1125 return this->got_tlsdesc_
;
1128 // Create the PLT section.
1130 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
1132 // Create a PLT entry for a global symbol.
1134 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1136 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
1138 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1139 Sized_relobj_file
<size
, false>* relobj
,
1140 unsigned int local_sym_index
);
1142 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1144 define_tls_base_symbol(Symbol_table
*, Layout
*);
1146 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1148 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
1150 // Create a GOT entry for the TLS module index.
1152 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1153 Sized_relobj_file
<size
, false>* object
);
1155 // Get the PLT section.
1156 Output_data_plt_x86_64
<size
>*
1159 gold_assert(this->plt_
!= NULL
);
1163 // Get the dynamic reloc section, creating it if necessary.
1165 rela_dyn_section(Layout
*);
1167 // Get the section to use for TLSDESC relocations.
1169 rela_tlsdesc_section(Layout
*) const;
1171 // Get the section to use for IRELATIVE relocations.
1173 rela_irelative_section(Layout
*);
1175 // Add a potential copy relocation.
1177 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1178 Sized_relobj_file
<size
, false>* object
,
1179 unsigned int shndx
, Output_section
* output_section
,
1180 Symbol
* sym
, const elfcpp::Rela
<size
, false>& reloc
)
1182 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1183 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1184 symtab
->get_sized_symbol
<size
>(sym
),
1185 object
, shndx
, output_section
,
1186 r_type
, reloc
.get_r_offset(),
1187 reloc
.get_r_addend(),
1188 this->rela_dyn_section(layout
));
1191 // Information about this specific target which we pass to the
1192 // general Target structure.
1193 static const Target::Target_info x86_64_info
;
1195 // The types of GOT entries needed for this platform.
1196 // These values are exposed to the ABI in an incremental link.
1197 // Do not renumber existing values without changing the version
1198 // number of the .gnu_incremental_inputs section.
1201 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
1202 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
1203 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
1204 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
1207 // This type is used as the argument to the target specific
1208 // relocation routines. The only target specific reloc is
1209 // R_X86_64_TLSDESC against a local symbol.
1212 Tlsdesc_info(Sized_relobj_file
<size
, false>* a_object
, unsigned int a_r_sym
)
1213 : object(a_object
), r_sym(a_r_sym
)
1216 // The object in which the local symbol is defined.
1217 Sized_relobj_file
<size
, false>* object
;
1218 // The local symbol index in the object.
1223 Output_data_got
<64, false>* got_
;
1225 Output_data_plt_x86_64
<size
>* plt_
;
1226 // The GOT PLT section.
1227 Output_data_got_plt_x86_64
* got_plt_
;
1228 // The GOT section for IRELATIVE relocations.
1229 Output_data_space
* got_irelative_
;
1230 // The GOT section for TLSDESC relocations.
1231 Output_data_got
<64, false>* got_tlsdesc_
;
1232 // The _GLOBAL_OFFSET_TABLE_ symbol.
1233 Symbol
* global_offset_table_
;
1234 // The dynamic reloc section.
1235 Reloc_section
* rela_dyn_
;
1236 // The section to use for IRELATIVE relocs.
1237 Reloc_section
* rela_irelative_
;
1238 // Relocs saved to avoid a COPY reloc.
1239 Copy_relocs
<elfcpp::SHT_RELA
, size
, false> copy_relocs_
;
1240 // Offset of the GOT entry for the TLS module index.
1241 unsigned int got_mod_index_offset_
;
1242 // We handle R_X86_64_TLSDESC against a local symbol as a target
1243 // specific relocation. Here we store the object and local symbol
1244 // index for the relocation.
1245 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
1246 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1247 bool tls_base_symbol_defined_
;
1251 const Target::Target_info Target_x86_64
<64>::x86_64_info
=
1254 false, // is_big_endian
1255 elfcpp::EM_X86_64
, // machine_code
1256 false, // has_make_symbol
1257 false, // has_resolve
1258 true, // has_code_fill
1259 true, // is_default_stack_executable
1260 true, // can_icf_inline_merge_sections
1262 "/lib/ld64.so.1", // program interpreter
1263 0x400000, // default_text_segment_address
1264 0x1000, // abi_pagesize (overridable by -z max-page-size)
1265 0x1000, // common_pagesize (overridable by -z common-page-size)
1266 false, // isolate_execinstr
1268 elfcpp::SHN_UNDEF
, // small_common_shndx
1269 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1270 0, // small_common_section_flags
1271 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1272 NULL
, // attributes_section
1273 NULL
, // attributes_vendor
1274 "_start", // entry_symbol_name
1275 32, // hash_entry_size
1276 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
1280 const Target::Target_info Target_x86_64
<32>::x86_64_info
=
1283 false, // is_big_endian
1284 elfcpp::EM_X86_64
, // machine_code
1285 false, // has_make_symbol
1286 false, // has_resolve
1287 true, // has_code_fill
1288 true, // is_default_stack_executable
1289 true, // can_icf_inline_merge_sections
1291 "/libx32/ldx32.so.1", // program interpreter
1292 0x400000, // default_text_segment_address
1293 0x1000, // abi_pagesize (overridable by -z max-page-size)
1294 0x1000, // common_pagesize (overridable by -z common-page-size)
1295 false, // isolate_execinstr
1297 elfcpp::SHN_UNDEF
, // small_common_shndx
1298 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1299 0, // small_common_section_flags
1300 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1301 NULL
, // attributes_section
1302 NULL
, // attributes_vendor
1303 "_start", // entry_symbol_name
1304 32, // hash_entry_size
1305 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
1308 // This is called when a new output section is created. This is where
1309 // we handle the SHF_X86_64_LARGE.
1313 Target_x86_64
<size
>::do_new_output_section(Output_section
* os
) const
1315 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
1316 os
->set_is_large_section();
1319 // Get the GOT section, creating it if necessary.
1322 Output_data_got
<64, false>*
1323 Target_x86_64
<size
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
1325 if (this->got_
== NULL
)
1327 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1329 // When using -z now, we can treat .got.plt as a relro section.
1330 // Without -z now, it is modified after program startup by lazy
1332 bool is_got_plt_relro
= parameters
->options().now();
1333 Output_section_order got_order
= (is_got_plt_relro
1335 : ORDER_RELRO_LAST
);
1336 Output_section_order got_plt_order
= (is_got_plt_relro
1338 : ORDER_NON_RELRO_FIRST
);
1340 this->got_
= new Output_data_got
<64, false>();
1342 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1344 | elfcpp::SHF_WRITE
),
1345 this->got_
, got_order
, true);
1347 this->got_plt_
= new Output_data_got_plt_x86_64(layout
);
1348 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1350 | elfcpp::SHF_WRITE
),
1351 this->got_plt_
, got_plt_order
,
1354 // The first three entries are reserved.
1355 this->got_plt_
->set_current_data_size(3 * 8);
1357 if (!is_got_plt_relro
)
1359 // Those bytes can go into the relro segment.
1360 layout
->increase_relro(3 * 8);
1363 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1364 this->global_offset_table_
=
1365 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1366 Symbol_table::PREDEFINED
,
1368 0, 0, elfcpp::STT_OBJECT
,
1370 elfcpp::STV_HIDDEN
, 0,
1373 // If there are any IRELATIVE relocations, they get GOT entries
1374 // in .got.plt after the jump slot entries.
1375 this->got_irelative_
= new Output_data_space(8, "** GOT IRELATIVE PLT");
1376 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1378 | elfcpp::SHF_WRITE
),
1379 this->got_irelative_
,
1380 got_plt_order
, is_got_plt_relro
);
1382 // If there are any TLSDESC relocations, they get GOT entries in
1383 // .got.plt after the jump slot and IRELATIVE entries.
1384 this->got_tlsdesc_
= new Output_data_got
<64, false>();
1385 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1387 | elfcpp::SHF_WRITE
),
1389 got_plt_order
, is_got_plt_relro
);
1395 // Get the dynamic reloc section, creating it if necessary.
1398 typename Target_x86_64
<size
>::Reloc_section
*
1399 Target_x86_64
<size
>::rela_dyn_section(Layout
* layout
)
1401 if (this->rela_dyn_
== NULL
)
1403 gold_assert(layout
!= NULL
);
1404 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1405 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1406 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1407 ORDER_DYNAMIC_RELOCS
, false);
1409 return this->rela_dyn_
;
1412 // Get the section to use for IRELATIVE relocs, creating it if
1413 // necessary. These go in .rela.dyn, but only after all other dynamic
1414 // relocations. They need to follow the other dynamic relocations so
1415 // that they can refer to global variables initialized by those
1419 typename Target_x86_64
<size
>::Reloc_section
*
1420 Target_x86_64
<size
>::rela_irelative_section(Layout
* layout
)
1422 if (this->rela_irelative_
== NULL
)
1424 // Make sure we have already created the dynamic reloc section.
1425 this->rela_dyn_section(layout
);
1426 this->rela_irelative_
= new Reloc_section(false);
1427 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1428 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
1429 ORDER_DYNAMIC_RELOCS
, false);
1430 gold_assert(this->rela_dyn_
->output_section()
1431 == this->rela_irelative_
->output_section());
1433 return this->rela_irelative_
;
1436 // Write the first three reserved words of the .got.plt section.
1437 // The remainder of the section is written while writing the PLT
1438 // in Output_data_plt_i386::do_write.
1441 Output_data_got_plt_x86_64::do_write(Output_file
* of
)
1443 // The first entry in the GOT is the address of the .dynamic section
1444 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1445 // We saved space for them when we created the section in
1446 // Target_x86_64::got_section.
1447 const off_t got_file_offset
= this->offset();
1448 gold_assert(this->data_size() >= 24);
1449 unsigned char* const got_view
= of
->get_output_view(got_file_offset
, 24);
1450 Output_section
* dynamic
= this->layout_
->dynamic_section();
1451 uint64_t dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
1452 elfcpp::Swap
<64, false>::writeval(got_view
, dynamic_addr
);
1453 memset(got_view
+ 8, 0, 16);
1454 of
->write_output_view(got_file_offset
, 24, got_view
);
1457 // Initialize the PLT section.
1461 Output_data_plt_x86_64
<size
>::init(Layout
* layout
)
1463 this->rel_
= new Reloc_section(false);
1464 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1465 elfcpp::SHF_ALLOC
, this->rel_
,
1466 ORDER_DYNAMIC_PLT_RELOCS
, false);
1471 Output_data_plt_x86_64
<size
>::do_adjust_output_section(Output_section
* os
)
1473 os
->set_entsize(this->get_plt_entry_size());
1476 // Add an entry to the PLT.
1480 Output_data_plt_x86_64
<size
>::add_entry(Symbol_table
* symtab
, Layout
* layout
,
1483 gold_assert(!gsym
->has_plt_offset());
1485 unsigned int plt_index
;
1487 section_offset_type got_offset
;
1489 unsigned int* pcount
;
1490 unsigned int offset
;
1491 unsigned int reserved
;
1492 Output_section_data_build
* got
;
1493 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1494 && gsym
->can_use_relative_reloc(false))
1496 pcount
= &this->irelative_count_
;
1499 got
= this->got_irelative_
;
1503 pcount
= &this->count_
;
1506 got
= this->got_plt_
;
1509 if (!this->is_data_size_valid())
1511 // Note that when setting the PLT offset for a non-IRELATIVE
1512 // entry we skip the initial reserved PLT entry.
1513 plt_index
= *pcount
+ offset
;
1514 plt_offset
= plt_index
* this->get_plt_entry_size();
1518 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1519 gold_assert(got_offset
== got
->current_data_size());
1521 // Every PLT entry needs a GOT entry which points back to the PLT
1522 // entry (this will be changed by the dynamic linker, normally
1523 // lazily when the function is called).
1524 got
->set_current_data_size(got_offset
+ 8);
1528 // FIXME: This is probably not correct for IRELATIVE relocs.
1530 // For incremental updates, find an available slot.
1531 plt_offset
= this->free_list_
.allocate(this->get_plt_entry_size(),
1532 this->get_plt_entry_size(), 0);
1533 if (plt_offset
== -1)
1534 gold_fallback(_("out of patch space (PLT);"
1535 " relink with --incremental-full"));
1537 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1538 // can be calculated from the PLT index, adjusting for the three
1539 // reserved entries at the beginning of the GOT.
1540 plt_index
= plt_offset
/ this->get_plt_entry_size() - 1;
1541 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1544 gsym
->set_plt_offset(plt_offset
);
1546 // Every PLT entry needs a reloc.
1547 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
1549 // Note that we don't need to save the symbol. The contents of the
1550 // PLT are independent of which symbols are used. The symbols only
1551 // appear in the relocations.
1554 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1559 Output_data_plt_x86_64
<size
>::add_local_ifunc_entry(
1560 Symbol_table
* symtab
,
1562 Sized_relobj_file
<size
, false>* relobj
,
1563 unsigned int local_sym_index
)
1565 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
1566 ++this->irelative_count_
;
1568 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
1570 // Every PLT entry needs a GOT entry which points back to the PLT
1572 this->got_irelative_
->set_current_data_size(got_offset
+ 8);
1574 // Every PLT entry needs a reloc.
1575 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1576 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
1577 elfcpp::R_X86_64_IRELATIVE
,
1578 this->got_irelative_
, got_offset
, 0);
1583 // Add the relocation for a PLT entry.
1587 Output_data_plt_x86_64
<size
>::add_relocation(Symbol_table
* symtab
,
1590 unsigned int got_offset
)
1592 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1593 && gsym
->can_use_relative_reloc(false))
1595 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1596 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
1597 this->got_irelative_
, got_offset
, 0);
1601 gsym
->set_needs_dynsym_entry();
1602 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
1607 // Return where the TLSDESC relocations should go, creating it if
1608 // necessary. These follow the JUMP_SLOT relocations.
1611 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1612 Output_data_plt_x86_64
<size
>::rela_tlsdesc(Layout
* layout
)
1614 if (this->tlsdesc_rel_
== NULL
)
1616 this->tlsdesc_rel_
= new Reloc_section(false);
1617 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1618 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
1619 ORDER_DYNAMIC_PLT_RELOCS
, false);
1620 gold_assert(this->tlsdesc_rel_
->output_section()
1621 == this->rel_
->output_section());
1623 return this->tlsdesc_rel_
;
1626 // Return where the IRELATIVE relocations should go in the PLT. These
1627 // follow the JUMP_SLOT and the TLSDESC relocations.
1630 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1631 Output_data_plt_x86_64
<size
>::rela_irelative(Symbol_table
* symtab
,
1634 if (this->irelative_rel_
== NULL
)
1636 // Make sure we have a place for the TLSDESC relocations, in
1637 // case we see any later on.
1638 this->rela_tlsdesc(layout
);
1639 this->irelative_rel_
= new Reloc_section(false);
1640 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1641 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
1642 ORDER_DYNAMIC_PLT_RELOCS
, false);
1643 gold_assert(this->irelative_rel_
->output_section()
1644 == this->rel_
->output_section());
1646 if (parameters
->doing_static_link())
1648 // A statically linked executable will only have a .rela.plt
1649 // section to hold R_X86_64_IRELATIVE relocs for
1650 // STT_GNU_IFUNC symbols. The library will use these
1651 // symbols to locate the IRELATIVE relocs at program startup
1653 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
1654 Symbol_table::PREDEFINED
,
1655 this->irelative_rel_
, 0, 0,
1656 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1657 elfcpp::STV_HIDDEN
, 0, false, true);
1658 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
1659 Symbol_table::PREDEFINED
,
1660 this->irelative_rel_
, 0, 0,
1661 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1662 elfcpp::STV_HIDDEN
, 0, true, true);
1665 return this->irelative_rel_
;
1668 // Return the PLT address to use for a global symbol.
1672 Output_data_plt_x86_64
<size
>::do_address_for_global(const Symbol
* gsym
)
1674 uint64_t offset
= 0;
1675 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1676 && gsym
->can_use_relative_reloc(false))
1677 offset
= (this->count_
+ 1) * this->get_plt_entry_size();
1678 return this->address() + offset
+ gsym
->plt_offset();
1681 // Return the PLT address to use for a local symbol. These are always
1682 // IRELATIVE relocs.
1686 Output_data_plt_x86_64
<size
>::do_address_for_local(const Relobj
* object
,
1689 return (this->address()
1690 + (this->count_
+ 1) * this->get_plt_entry_size()
1691 + object
->local_plt_offset(r_sym
));
1694 // Set the final size.
1697 Output_data_plt_x86_64
<size
>::set_final_data_size()
1699 // Number of regular and IFUNC PLT entries, plus the first entry.
1700 unsigned int count
= this->count_
+ this->irelative_count_
+ 1;
1701 // Count the TLSDESC entry, if present.
1702 if (this->has_tlsdesc_entry())
1704 this->set_data_size(count
* this->get_plt_entry_size());
1707 // The first entry in the PLT for an executable.
1711 Output_data_plt_x86_64_standard
<size
>::first_plt_entry
[plt_entry_size
] =
1713 // From AMD64 ABI Draft 0.98, page 76
1714 0xff, 0x35, // pushq contents of memory address
1715 0, 0, 0, 0, // replaced with address of .got + 8
1716 0xff, 0x25, // jmp indirect
1717 0, 0, 0, 0, // replaced with address of .got + 16
1718 0x90, 0x90, 0x90, 0x90 // noop (x4)
1723 Output_data_plt_x86_64_standard
<size
>::do_fill_first_plt_entry(
1725 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1726 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
1728 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1729 // We do a jmp relative to the PC at the end of this instruction.
1730 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1732 - (plt_address
+ 6)));
1733 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1735 - (plt_address
+ 12)));
1738 // Subsequent entries in the PLT for an executable.
1742 Output_data_plt_x86_64_standard
<size
>::plt_entry
[plt_entry_size
] =
1744 // From AMD64 ABI Draft 0.98, page 76
1745 0xff, 0x25, // jmpq indirect
1746 0, 0, 0, 0, // replaced with address of symbol in .got
1747 0x68, // pushq immediate
1748 0, 0, 0, 0, // replaced with offset into relocation table
1749 0xe9, // jmpq relative
1750 0, 0, 0, 0 // replaced with offset to start of .plt
1755 Output_data_plt_x86_64_standard
<size
>::do_fill_plt_entry(
1757 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1758 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1759 unsigned int got_offset
,
1760 unsigned int plt_offset
,
1761 unsigned int plt_index
)
1763 // Check PC-relative offset overflow in PLT entry.
1764 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
1765 - (plt_address
+ plt_offset
+ 6));
1766 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
1767 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1770 memcpy(pov
, plt_entry
, plt_entry_size
);
1771 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1772 plt_got_pcrel_offset
);
1774 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1775 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1776 - (plt_offset
+ plt_entry_size
));
1781 // The reserved TLSDESC entry in the PLT for an executable.
1785 Output_data_plt_x86_64_standard
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
1787 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1788 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1789 0xff, 0x35, // pushq x(%rip)
1790 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1791 0xff, 0x25, // jmpq *y(%rip)
1792 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1799 Output_data_plt_x86_64_standard
<size
>::do_fill_tlsdesc_entry(
1801 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1802 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1803 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
1804 unsigned int tlsdesc_got_offset
,
1805 unsigned int plt_offset
)
1807 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1808 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1810 - (plt_address
+ plt_offset
1812 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1814 + tlsdesc_got_offset
1815 - (plt_address
+ plt_offset
1819 // Return the APLT address to use for a global symbol (for -z bndplt).
1822 Output_data_plt_x86_64_bnd::do_address_for_global(const Symbol
* gsym
)
1824 uint64_t offset
= this->aplt_offset_
;
1825 // Convert the PLT offset into an APLT offset.
1826 unsigned int plt_offset
= gsym
->plt_offset();
1827 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1828 && gsym
->can_use_relative_reloc(false))
1829 offset
+= this->regular_count() * aplt_entry_size
;
1831 plt_offset
-= plt_entry_size
;
1832 plt_offset
= plt_offset
/ (plt_entry_size
/ aplt_entry_size
);
1833 return this->address() + offset
+ plt_offset
;
1836 // Return the PLT address to use for a local symbol. These are always
1837 // IRELATIVE relocs.
1840 Output_data_plt_x86_64_bnd::do_address_for_local(const Relobj
* object
,
1843 // Convert the PLT offset into an APLT offset.
1844 unsigned int plt_offset
= ((object
->local_plt_offset(r_sym
) - plt_entry_size
)
1845 / (plt_entry_size
/ aplt_entry_size
));
1846 return (this->address()
1847 + this->aplt_offset_
1848 + this->regular_count() * aplt_entry_size
1852 // Set the final size.
1854 Output_data_plt_x86_64_bnd::set_final_data_size()
1856 // Number of regular and IFUNC PLT entries.
1857 unsigned int count
= this->entry_count();
1858 // Count the first entry and the TLSDESC entry, if present.
1859 unsigned int extra
= this->has_tlsdesc_entry() ? 2 : 1;
1860 unsigned int plt_size
= (count
+ extra
) * plt_entry_size
;
1861 // Offset of the APLT.
1862 this->aplt_offset_
= plt_size
;
1863 // Size of the APLT.
1864 plt_size
+= count
* aplt_entry_size
;
1865 this->set_data_size(plt_size
);
1868 // The first entry in the BND PLT.
1871 Output_data_plt_x86_64_bnd::first_plt_entry
[plt_entry_size
] =
1873 // From AMD64 ABI Draft 0.98, page 76
1874 0xff, 0x35, // pushq contents of memory address
1875 0, 0, 0, 0, // replaced with address of .got + 8
1876 0xf2, 0xff, 0x25, // bnd jmp indirect
1877 0, 0, 0, 0, // replaced with address of .got + 16
1878 0x0f, 0x1f, 0x00 // nop
1882 Output_data_plt_x86_64_bnd::do_fill_first_plt_entry(
1884 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
1885 elfcpp::Elf_types
<64>::Elf_Addr plt_address
)
1887 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1888 // We do a jmp relative to the PC at the end of this instruction.
1889 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1891 - (plt_address
+ 6)));
1892 elfcpp::Swap
<32, false>::writeval(pov
+ 9,
1894 - (plt_address
+ 13)));
1897 // Subsequent entries in the BND PLT.
1900 Output_data_plt_x86_64_bnd::plt_entry
[plt_entry_size
] =
1902 // From AMD64 ABI Draft 0.99.8, page 139
1903 0x68, // pushq immediate
1904 0, 0, 0, 0, // replaced with offset into relocation table
1905 0xf2, 0xe9, // bnd jmpq relative
1906 0, 0, 0, 0, // replaced with offset to start of .plt
1907 0x0f, 0x1f, 0x44, 0, 0 // nop
1910 // Entries in the BND Additional PLT.
1913 Output_data_plt_x86_64_bnd::aplt_entry
[aplt_entry_size
] =
1915 // From AMD64 ABI Draft 0.99.8, page 139
1916 0xf2, 0xff, 0x25, // bnd jmpq indirect
1917 0, 0, 0, 0, // replaced with address of symbol in .got
1922 Output_data_plt_x86_64_bnd::do_fill_plt_entry(
1924 elfcpp::Elf_types
<64>::Elf_Addr
,
1925 elfcpp::Elf_types
<64>::Elf_Addr
,
1927 unsigned int plt_offset
,
1928 unsigned int plt_index
)
1930 memcpy(pov
, plt_entry
, plt_entry_size
);
1931 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 1, plt_index
);
1932 elfcpp::Swap
<32, false>::writeval(pov
+ 7, -(plt_offset
+ 11));
1937 Output_data_plt_x86_64_bnd::fill_aplt_entry(
1939 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
1940 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
1941 unsigned int got_offset
,
1942 unsigned int plt_offset
,
1943 unsigned int plt_index
)
1945 // Check PC-relative offset overflow in PLT entry.
1946 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
1947 - (plt_address
+ plt_offset
+ 7));
1948 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
1949 gold_error(_("PC-relative offset overflow in APLT entry %d"),
1952 memcpy(pov
, aplt_entry
, aplt_entry_size
);
1953 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3, plt_got_pcrel_offset
);
1956 // The reserved TLSDESC entry in the PLT for an executable.
1959 Output_data_plt_x86_64_bnd::tlsdesc_plt_entry
[plt_entry_size
] =
1961 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1962 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1963 0xff, 0x35, // pushq x(%rip)
1964 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1965 0xf2, 0xff, 0x25, // jmpq *y(%rip)
1966 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1967 0x0f, 0x1f, 0 // nop
1971 Output_data_plt_x86_64_bnd::do_fill_tlsdesc_entry(
1973 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
1974 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
1975 elfcpp::Elf_types
<64>::Elf_Addr got_base
,
1976 unsigned int tlsdesc_got_offset
,
1977 unsigned int plt_offset
)
1979 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1980 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1982 - (plt_address
+ plt_offset
1984 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
1986 + tlsdesc_got_offset
1987 - (plt_address
+ plt_offset
1991 // The .eh_frame unwind information for the PLT.
1995 Output_data_plt_x86_64
<size
>::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
1998 'z', // Augmentation: augmentation size included.
1999 'R', // Augmentation: FDE encoding included.
2000 '\0', // End of augmentation string.
2001 1, // Code alignment factor.
2002 0x78, // Data alignment factor.
2003 16, // Return address column.
2004 1, // Augmentation size.
2005 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
2006 | elfcpp::DW_EH_PE_sdata4
),
2007 elfcpp::DW_CFA_def_cfa
, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
2008 elfcpp::DW_CFA_offset
+ 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
2009 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
2015 Output_data_plt_x86_64_standard
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
2017 0, 0, 0, 0, // Replaced with offset to .plt.
2018 0, 0, 0, 0, // Replaced with size of .plt.
2019 0, // Augmentation size.
2020 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
2021 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
2022 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
2023 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
2024 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
2025 11, // Block length.
2026 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
2027 elfcpp::DW_OP_breg16
, 0, // Push %rip.
2028 elfcpp::DW_OP_lit15
, // Push 0xf.
2029 elfcpp::DW_OP_and
, // & (%rip & 0xf).
2030 elfcpp::DW_OP_lit11
, // Push 0xb.
2031 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 0xb)
2032 elfcpp::DW_OP_lit3
, // Push 3.
2033 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 0xb) << 3)
2034 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
2035 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
2041 // The .eh_frame unwind information for the BND PLT.
2043 Output_data_plt_x86_64_bnd::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
2045 0, 0, 0, 0, // Replaced with offset to .plt.
2046 0, 0, 0, 0, // Replaced with size of .plt.
2047 0, // Augmentation size.
2048 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
2049 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
2050 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
2051 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
2052 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
2053 11, // Block length.
2054 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
2055 elfcpp::DW_OP_breg16
, 0, // Push %rip.
2056 elfcpp::DW_OP_lit15
, // Push 0xf.
2057 elfcpp::DW_OP_and
, // & (%rip & 0xf).
2058 elfcpp::DW_OP_lit5
, // Push 5.
2059 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 5)
2060 elfcpp::DW_OP_lit3
, // Push 3.
2061 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 5) << 3)
2062 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=5)<<3)+%rsp+8
2063 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
2069 // Write out the PLT. This uses the hand-coded instructions above,
2070 // and adjusts them as needed. This is specified by the AMD64 ABI.
2074 Output_data_plt_x86_64
<size
>::do_write(Output_file
* of
)
2076 const off_t offset
= this->offset();
2077 const section_size_type oview_size
=
2078 convert_to_section_size_type(this->data_size());
2079 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2081 const off_t got_file_offset
= this->got_plt_
->offset();
2082 gold_assert(parameters
->incremental_update()
2083 || (got_file_offset
+ this->got_plt_
->data_size()
2084 == this->got_irelative_
->offset()));
2085 const section_size_type got_size
=
2086 convert_to_section_size_type(this->got_plt_
->data_size()
2087 + this->got_irelative_
->data_size());
2088 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
2091 unsigned char* pov
= oview
;
2093 // The base address of the .plt section.
2094 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
2095 // The base address of the .got section.
2096 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
= this->got_
->address();
2097 // The base address of the PLT portion of the .got section,
2098 // which is where the GOT pointer will point, and where the
2099 // three reserved GOT entries are located.
2100 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
2101 = this->got_plt_
->address();
2103 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
2104 pov
+= this->get_plt_entry_size();
2106 // The first three entries in the GOT are reserved, and are written
2107 // by Output_data_got_plt_x86_64::do_write.
2108 unsigned char* got_pov
= got_view
+ 24;
2110 unsigned int plt_offset
= this->get_plt_entry_size();
2111 unsigned int got_offset
= 24;
2112 const unsigned int count
= this->count_
+ this->irelative_count_
;
2113 for (unsigned int plt_index
= 0;
2116 pov
+= this->get_plt_entry_size(),
2118 plt_offset
+= this->get_plt_entry_size(),
2121 // Set and adjust the PLT entry itself.
2122 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
2123 got_address
, plt_address
,
2124 got_offset
, plt_offset
,
2127 // Set the entry in the GOT.
2128 elfcpp::Swap
<64, false>::writeval(got_pov
,
2129 plt_address
+ plt_offset
+ lazy_offset
);
2132 if (this->has_tlsdesc_entry())
2134 // Set and adjust the reserved TLSDESC PLT entry.
2135 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
2136 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
2137 tlsdesc_got_offset
, plt_offset
);
2138 pov
+= this->get_plt_entry_size();
2141 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
2142 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
2144 of
->write_output_view(offset
, oview_size
, oview
);
2145 of
->write_output_view(got_file_offset
, got_size
, got_view
);
2148 // Write out the BND PLT.
2151 Output_data_plt_x86_64_bnd::do_write(Output_file
* of
)
2153 const off_t offset
= this->offset();
2154 const section_size_type oview_size
=
2155 convert_to_section_size_type(this->data_size());
2156 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2158 Output_data_got
<64, false>* got
= this->got();
2159 Output_data_got_plt_x86_64
* got_plt
= this->got_plt();
2160 Output_data_space
* got_irelative
= this->got_irelative();
2162 const off_t got_file_offset
= got_plt
->offset();
2163 gold_assert(parameters
->incremental_update()
2164 || (got_file_offset
+ got_plt
->data_size()
2165 == got_irelative
->offset()));
2166 const section_size_type got_size
=
2167 convert_to_section_size_type(got_plt
->data_size()
2168 + got_irelative
->data_size());
2169 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
2172 unsigned char* pov
= oview
;
2174 // The base address of the .plt section.
2175 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
2176 // The base address of the .got section.
2177 elfcpp::Elf_types
<64>::Elf_Addr got_base
= got
->address();
2178 // The base address of the PLT portion of the .got section,
2179 // which is where the GOT pointer will point, and where the
2180 // three reserved GOT entries are located.
2181 elfcpp::Elf_types
<64>::Elf_Addr got_address
= got_plt
->address();
2183 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
2184 pov
+= plt_entry_size
;
2186 // The first three entries in the GOT are reserved, and are written
2187 // by Output_data_got_plt_x86_64::do_write.
2188 unsigned char* got_pov
= got_view
+ 24;
2190 unsigned int plt_offset
= plt_entry_size
;
2191 unsigned int got_offset
= 24;
2192 const unsigned int count
= this->entry_count();
2193 for (unsigned int plt_index
= 0;
2196 pov
+= plt_entry_size
,
2198 plt_offset
+= plt_entry_size
,
2201 // Set and adjust the PLT entry itself.
2202 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
2203 got_address
, plt_address
,
2204 got_offset
, plt_offset
,
2207 // Set the entry in the GOT.
2208 elfcpp::Swap
<64, false>::writeval(got_pov
,
2209 plt_address
+ plt_offset
+ lazy_offset
);
2212 if (this->has_tlsdesc_entry())
2214 // Set and adjust the reserved TLSDESC PLT entry.
2215 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
2216 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
2217 tlsdesc_got_offset
, plt_offset
);
2218 pov
+= this->get_plt_entry_size();
2221 // Write the additional PLT.
2223 for (unsigned int plt_index
= 0;
2226 pov
+= aplt_entry_size
,
2227 plt_offset
+= aplt_entry_size
,
2230 // Set and adjust the PLT entry itself.
2231 this->fill_aplt_entry(pov
, got_address
, plt_address
, got_offset
,
2232 plt_offset
, plt_index
);
2235 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
2236 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
2238 of
->write_output_view(offset
, oview_size
, oview
);
2239 of
->write_output_view(got_file_offset
, got_size
, got_view
);
2242 // Create the PLT section.
2246 Target_x86_64
<size
>::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
2248 if (this->plt_
== NULL
)
2250 // Create the GOT sections first.
2251 this->got_section(symtab
, layout
);
2253 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
2254 this->got_irelative_
);
2256 // Add unwind information if requested.
2257 if (parameters
->options().ld_generated_unwind_info())
2258 this->plt_
->add_eh_frame(layout
);
2260 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
2262 | elfcpp::SHF_EXECINSTR
),
2263 this->plt_
, ORDER_PLT
, false);
2265 // Make the sh_info field of .rela.plt point to .plt.
2266 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
2267 rela_plt_os
->set_info_section(this->plt_
->output_section());
2272 Output_data_plt_x86_64
<32>*
2273 Target_x86_64
<32>::do_make_data_plt(Layout
* layout
,
2274 Output_data_got
<64, false>* got
,
2275 Output_data_got_plt_x86_64
* got_plt
,
2276 Output_data_space
* got_irelative
)
2278 return new Output_data_plt_x86_64_standard
<32>(layout
, got
, got_plt
,
2283 Output_data_plt_x86_64
<64>*
2284 Target_x86_64
<64>::do_make_data_plt(Layout
* layout
,
2285 Output_data_got
<64, false>* got
,
2286 Output_data_got_plt_x86_64
* got_plt
,
2287 Output_data_space
* got_irelative
)
2289 if (parameters
->options().bndplt())
2290 return new Output_data_plt_x86_64_bnd(layout
, got
, got_plt
,
2293 return new Output_data_plt_x86_64_standard
<64>(layout
, got
, got_plt
,
2298 Output_data_plt_x86_64
<32>*
2299 Target_x86_64
<32>::do_make_data_plt(Layout
* layout
,
2300 Output_data_got
<64, false>* got
,
2301 Output_data_got_plt_x86_64
* got_plt
,
2302 Output_data_space
* got_irelative
,
2303 unsigned int plt_count
)
2305 return new Output_data_plt_x86_64_standard
<32>(layout
, got
, got_plt
,
2311 Output_data_plt_x86_64
<64>*
2312 Target_x86_64
<64>::do_make_data_plt(Layout
* layout
,
2313 Output_data_got
<64, false>* got
,
2314 Output_data_got_plt_x86_64
* got_plt
,
2315 Output_data_space
* got_irelative
,
2316 unsigned int plt_count
)
2318 if (parameters
->options().bndplt())
2319 return new Output_data_plt_x86_64_bnd(layout
, got
, got_plt
,
2320 got_irelative
, plt_count
);
2322 return new Output_data_plt_x86_64_standard
<64>(layout
, got
, got_plt
,
2327 // Return the section for TLSDESC relocations.
2330 typename Target_x86_64
<size
>::Reloc_section
*
2331 Target_x86_64
<size
>::rela_tlsdesc_section(Layout
* layout
) const
2333 return this->plt_section()->rela_tlsdesc(layout
);
2336 // Create a PLT entry for a global symbol.
2340 Target_x86_64
<size
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
2343 if (gsym
->has_plt_offset())
2346 if (this->plt_
== NULL
)
2347 this->make_plt_section(symtab
, layout
);
2349 this->plt_
->add_entry(symtab
, layout
, gsym
);
2352 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2356 Target_x86_64
<size
>::make_local_ifunc_plt_entry(
2357 Symbol_table
* symtab
, Layout
* layout
,
2358 Sized_relobj_file
<size
, false>* relobj
,
2359 unsigned int local_sym_index
)
2361 if (relobj
->local_has_plt_offset(local_sym_index
))
2363 if (this->plt_
== NULL
)
2364 this->make_plt_section(symtab
, layout
);
2365 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
2368 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
2371 // Return the number of entries in the PLT.
2375 Target_x86_64
<size
>::plt_entry_count() const
2377 if (this->plt_
== NULL
)
2379 return this->plt_
->entry_count();
2382 // Return the offset of the first non-reserved PLT entry.
2386 Target_x86_64
<size
>::first_plt_entry_offset() const
2388 if (this->plt_
== NULL
)
2390 return this->plt_
->first_plt_entry_offset();
2393 // Return the size of each PLT entry.
2397 Target_x86_64
<size
>::plt_entry_size() const
2399 if (this->plt_
== NULL
)
2401 return this->plt_
->get_plt_entry_size();
2404 // Create the GOT and PLT sections for an incremental update.
2407 Output_data_got_base
*
2408 Target_x86_64
<size
>::init_got_plt_for_update(Symbol_table
* symtab
,
2410 unsigned int got_count
,
2411 unsigned int plt_count
)
2413 gold_assert(this->got_
== NULL
);
2415 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
2416 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
2418 | elfcpp::SHF_WRITE
),
2419 this->got_
, ORDER_RELRO_LAST
,
2422 // Add the three reserved entries.
2423 this->got_plt_
= new Output_data_got_plt_x86_64(layout
, (plt_count
+ 3) * 8);
2424 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
2426 | elfcpp::SHF_WRITE
),
2427 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
2430 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
2431 this->global_offset_table_
=
2432 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
2433 Symbol_table::PREDEFINED
,
2435 0, 0, elfcpp::STT_OBJECT
,
2437 elfcpp::STV_HIDDEN
, 0,
2440 // If there are any TLSDESC relocations, they get GOT entries in
2441 // .got.plt after the jump slot entries.
2442 // FIXME: Get the count for TLSDESC entries.
2443 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
2444 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
2445 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2447 ORDER_NON_RELRO_FIRST
, false);
2449 // If there are any IRELATIVE relocations, they get GOT entries in
2450 // .got.plt after the jump slot and TLSDESC entries.
2451 this->got_irelative_
= new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
2452 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
2453 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
2454 this->got_irelative_
,
2455 ORDER_NON_RELRO_FIRST
, false);
2457 // Create the PLT section.
2458 this->plt_
= this->make_data_plt(layout
, this->got_
,
2460 this->got_irelative_
,
2463 // Add unwind information if requested.
2464 if (parameters
->options().ld_generated_unwind_info())
2465 this->plt_
->add_eh_frame(layout
);
2467 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
2468 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
2469 this->plt_
, ORDER_PLT
, false);
2471 // Make the sh_info field of .rela.plt point to .plt.
2472 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
2473 rela_plt_os
->set_info_section(this->plt_
->output_section());
2475 // Create the rela_dyn section.
2476 this->rela_dyn_section(layout
);
2481 // Reserve a GOT entry for a local symbol, and regenerate any
2482 // necessary dynamic relocations.
2486 Target_x86_64
<size
>::reserve_local_got_entry(
2487 unsigned int got_index
,
2488 Sized_relobj
<size
, false>* obj
,
2490 unsigned int got_type
)
2492 unsigned int got_offset
= got_index
* 8;
2493 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
2495 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
2498 case GOT_TYPE_STANDARD
:
2499 if (parameters
->options().output_is_position_independent())
2500 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
2501 this->got_
, got_offset
, 0, false);
2503 case GOT_TYPE_TLS_OFFSET
:
2504 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
2505 this->got_
, got_offset
, 0);
2507 case GOT_TYPE_TLS_PAIR
:
2508 this->got_
->reserve_slot(got_index
+ 1);
2509 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
2510 this->got_
, got_offset
, 0);
2512 case GOT_TYPE_TLS_DESC
:
2513 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
2514 // this->got_->reserve_slot(got_index + 1);
2515 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2516 // this->got_, got_offset, 0);
2523 // Reserve a GOT entry for a global symbol, and regenerate any
2524 // necessary dynamic relocations.
2528 Target_x86_64
<size
>::reserve_global_got_entry(unsigned int got_index
,
2530 unsigned int got_type
)
2532 unsigned int got_offset
= got_index
* 8;
2533 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
2535 this->got_
->reserve_global(got_index
, gsym
, got_type
);
2538 case GOT_TYPE_STANDARD
:
2539 if (!gsym
->final_value_is_known())
2541 if (gsym
->is_from_dynobj()
2542 || gsym
->is_undefined()
2543 || gsym
->is_preemptible()
2544 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
2545 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
2546 this->got_
, got_offset
, 0);
2548 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2549 this->got_
, got_offset
, 0, false);
2552 case GOT_TYPE_TLS_OFFSET
:
2553 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
2554 this->got_
, got_offset
, 0, false);
2556 case GOT_TYPE_TLS_PAIR
:
2557 this->got_
->reserve_slot(got_index
+ 1);
2558 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
2559 this->got_
, got_offset
, 0, false);
2560 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
2561 this->got_
, got_offset
+ 8, 0, false);
2563 case GOT_TYPE_TLS_DESC
:
2564 this->got_
->reserve_slot(got_index
+ 1);
2565 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
2566 this->got_
, got_offset
, 0, false);
2573 // Register an existing PLT entry for a global symbol.
2577 Target_x86_64
<size
>::register_global_plt_entry(Symbol_table
* symtab
,
2579 unsigned int plt_index
,
2582 gold_assert(this->plt_
!= NULL
);
2583 gold_assert(!gsym
->has_plt_offset());
2585 this->plt_
->reserve_slot(plt_index
);
2587 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
2589 unsigned int got_offset
= (plt_index
+ 3) * 8;
2590 this->plt_
->add_relocation(symtab
, layout
, gsym
, got_offset
);
2593 // Force a COPY relocation for a given symbol.
2597 Target_x86_64
<size
>::emit_copy_reloc(
2598 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
2600 this->copy_relocs_
.emit_copy_reloc(symtab
,
2601 symtab
->get_sized_symbol
<size
>(sym
),
2604 this->rela_dyn_section(NULL
));
2607 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2611 Target_x86_64
<size
>::define_tls_base_symbol(Symbol_table
* symtab
,
2614 if (this->tls_base_symbol_defined_
)
2617 Output_segment
* tls_segment
= layout
->tls_segment();
2618 if (tls_segment
!= NULL
)
2620 bool is_exec
= parameters
->options().output_is_executable();
2621 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
2622 Symbol_table::PREDEFINED
,
2626 elfcpp::STV_HIDDEN
, 0,
2628 ? Symbol::SEGMENT_END
2629 : Symbol::SEGMENT_START
),
2632 this->tls_base_symbol_defined_
= true;
2635 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2639 Target_x86_64
<size
>::reserve_tlsdesc_entries(Symbol_table
* symtab
,
2642 if (this->plt_
== NULL
)
2643 this->make_plt_section(symtab
, layout
);
2645 if (!this->plt_
->has_tlsdesc_entry())
2647 // Allocate the TLSDESC_GOT entry.
2648 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2649 unsigned int got_offset
= got
->add_constant(0);
2651 // Allocate the TLSDESC_PLT entry.
2652 this->plt_
->reserve_tlsdesc_entry(got_offset
);
2656 // Create a GOT entry for the TLS module index.
2660 Target_x86_64
<size
>::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
2661 Sized_relobj_file
<size
, false>* object
)
2663 if (this->got_mod_index_offset_
== -1U)
2665 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
2666 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
2667 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2668 unsigned int got_offset
= got
->add_constant(0);
2669 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
2671 got
->add_constant(0);
2672 this->got_mod_index_offset_
= got_offset
;
2674 return this->got_mod_index_offset_
;
2677 // Optimize the TLS relocation type based on what we know about the
2678 // symbol. IS_FINAL is true if the final address of this symbol is
2679 // known at link time.
2682 tls::Tls_optimization
2683 Target_x86_64
<size
>::optimize_tls_reloc(bool is_final
, int r_type
)
2685 // If we are generating a shared library, then we can't do anything
2687 if (parameters
->options().shared())
2688 return tls::TLSOPT_NONE
;
2692 case elfcpp::R_X86_64_TLSGD
:
2693 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2694 case elfcpp::R_X86_64_TLSDESC_CALL
:
2695 // These are General-Dynamic which permits fully general TLS
2696 // access. Since we know that we are generating an executable,
2697 // we can convert this to Initial-Exec. If we also know that
2698 // this is a local symbol, we can further switch to Local-Exec.
2700 return tls::TLSOPT_TO_LE
;
2701 return tls::TLSOPT_TO_IE
;
2703 case elfcpp::R_X86_64_TLSLD
:
2704 // This is Local-Dynamic, which refers to a local symbol in the
2705 // dynamic TLS block. Since we know that we generating an
2706 // executable, we can switch to Local-Exec.
2707 return tls::TLSOPT_TO_LE
;
2709 case elfcpp::R_X86_64_DTPOFF32
:
2710 case elfcpp::R_X86_64_DTPOFF64
:
2711 // Another Local-Dynamic reloc.
2712 return tls::TLSOPT_TO_LE
;
2714 case elfcpp::R_X86_64_GOTTPOFF
:
2715 // These are Initial-Exec relocs which get the thread offset
2716 // from the GOT. If we know that we are linking against the
2717 // local symbol, we can switch to Local-Exec, which links the
2718 // thread offset into the instruction.
2720 return tls::TLSOPT_TO_LE
;
2721 return tls::TLSOPT_NONE
;
2723 case elfcpp::R_X86_64_TPOFF32
:
2724 // When we already have Local-Exec, there is nothing further we
2726 return tls::TLSOPT_NONE
;
2733 // Get the Reference_flags for a particular relocation.
2737 Target_x86_64
<size
>::Scan::get_reference_flags(unsigned int r_type
)
2741 case elfcpp::R_X86_64_NONE
:
2742 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2743 case elfcpp::R_X86_64_GNU_VTENTRY
:
2744 case elfcpp::R_X86_64_GOTPC32
:
2745 case elfcpp::R_X86_64_GOTPC64
:
2746 // No symbol reference.
2749 case elfcpp::R_X86_64_64
:
2750 case elfcpp::R_X86_64_32
:
2751 case elfcpp::R_X86_64_32S
:
2752 case elfcpp::R_X86_64_16
:
2753 case elfcpp::R_X86_64_8
:
2754 return Symbol::ABSOLUTE_REF
;
2756 case elfcpp::R_X86_64_PC64
:
2757 case elfcpp::R_X86_64_PC32
:
2758 case elfcpp::R_X86_64_PC32_BND
:
2759 case elfcpp::R_X86_64_PC16
:
2760 case elfcpp::R_X86_64_PC8
:
2761 case elfcpp::R_X86_64_GOTOFF64
:
2762 return Symbol::RELATIVE_REF
;
2764 case elfcpp::R_X86_64_PLT32
:
2765 case elfcpp::R_X86_64_PLT32_BND
:
2766 case elfcpp::R_X86_64_PLTOFF64
:
2767 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
2769 case elfcpp::R_X86_64_GOT64
:
2770 case elfcpp::R_X86_64_GOT32
:
2771 case elfcpp::R_X86_64_GOTPCREL64
:
2772 case elfcpp::R_X86_64_GOTPCREL
:
2773 case elfcpp::R_X86_64_GOTPCRELX
:
2774 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2775 case elfcpp::R_X86_64_GOTPLT64
:
2777 return Symbol::ABSOLUTE_REF
;
2779 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2780 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2781 case elfcpp::R_X86_64_TLSDESC_CALL
:
2782 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2783 case elfcpp::R_X86_64_DTPOFF32
:
2784 case elfcpp::R_X86_64_DTPOFF64
:
2785 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2786 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2787 return Symbol::TLS_REF
;
2789 case elfcpp::R_X86_64_COPY
:
2790 case elfcpp::R_X86_64_GLOB_DAT
:
2791 case elfcpp::R_X86_64_JUMP_SLOT
:
2792 case elfcpp::R_X86_64_RELATIVE
:
2793 case elfcpp::R_X86_64_IRELATIVE
:
2794 case elfcpp::R_X86_64_TPOFF64
:
2795 case elfcpp::R_X86_64_DTPMOD64
:
2796 case elfcpp::R_X86_64_TLSDESC
:
2797 case elfcpp::R_X86_64_SIZE32
:
2798 case elfcpp::R_X86_64_SIZE64
:
2800 // Not expected. We will give an error later.
2805 // Report an unsupported relocation against a local symbol.
2809 Target_x86_64
<size
>::Scan::unsupported_reloc_local(
2810 Sized_relobj_file
<size
, false>* object
,
2811 unsigned int r_type
)
2813 gold_error(_("%s: unsupported reloc %u against local symbol"),
2814 object
->name().c_str(), r_type
);
2817 // We are about to emit a dynamic relocation of type R_TYPE. If the
2818 // dynamic linker does not support it, issue an error. The GNU linker
2819 // only issues a non-PIC error for an allocated read-only section.
2820 // Here we know the section is allocated, but we don't know that it is
2821 // read-only. But we check for all the relocation types which the
2822 // glibc dynamic linker supports, so it seems appropriate to issue an
2823 // error even if the section is not read-only. If GSYM is not NULL,
2824 // it is the symbol the relocation is against; if it is NULL, the
2825 // relocation is against a local symbol.
2829 Target_x86_64
<size
>::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
2834 // These are the relocation types supported by glibc for x86_64
2835 // which should always work.
2836 case elfcpp::R_X86_64_RELATIVE
:
2837 case elfcpp::R_X86_64_IRELATIVE
:
2838 case elfcpp::R_X86_64_GLOB_DAT
:
2839 case elfcpp::R_X86_64_JUMP_SLOT
:
2840 case elfcpp::R_X86_64_DTPMOD64
:
2841 case elfcpp::R_X86_64_DTPOFF64
:
2842 case elfcpp::R_X86_64_TPOFF64
:
2843 case elfcpp::R_X86_64_64
:
2844 case elfcpp::R_X86_64_COPY
:
2847 // glibc supports these reloc types, but they can overflow.
2848 case elfcpp::R_X86_64_PC32
:
2849 case elfcpp::R_X86_64_PC32_BND
:
2850 // A PC relative reference is OK against a local symbol or if
2851 // the symbol is defined locally.
2853 || (!gsym
->is_from_dynobj()
2854 && !gsym
->is_undefined()
2855 && !gsym
->is_preemptible()))
2858 case elfcpp::R_X86_64_32
:
2859 // R_X86_64_32 is OK for x32.
2860 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2862 if (this->issued_non_pic_error_
)
2864 gold_assert(parameters
->options().output_is_position_independent());
2866 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
2867 "overflow at runtime; recompile with -fPIC"));
2873 case elfcpp::R_X86_64_32
:
2874 r_name
= "R_X86_64_32";
2876 case elfcpp::R_X86_64_PC32
:
2877 r_name
= "R_X86_64_PC32";
2879 case elfcpp::R_X86_64_PC32_BND
:
2880 r_name
= "R_X86_64_PC32_BND";
2886 object
->error(_("requires dynamic %s reloc against '%s' "
2887 "which may overflow at runtime; recompile "
2889 r_name
, gsym
->name());
2891 this->issued_non_pic_error_
= true;
2895 // This prevents us from issuing more than one error per reloc
2896 // section. But we can still wind up issuing more than one
2897 // error per object file.
2898 if (this->issued_non_pic_error_
)
2900 gold_assert(parameters
->options().output_is_position_independent());
2901 object
->error(_("requires unsupported dynamic reloc %u; "
2902 "recompile with -fPIC"),
2904 this->issued_non_pic_error_
= true;
2907 case elfcpp::R_X86_64_NONE
:
2912 // Return whether we need to make a PLT entry for a relocation of the
2913 // given type against a STT_GNU_IFUNC symbol.
2917 Target_x86_64
<size
>::Scan::reloc_needs_plt_for_ifunc(
2918 Sized_relobj_file
<size
, false>* object
,
2919 unsigned int r_type
)
2921 int flags
= Scan::get_reference_flags(r_type
);
2922 if (flags
& Symbol::TLS_REF
)
2923 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2924 object
->name().c_str(), r_type
);
2928 // Scan a relocation for a local symbol.
2932 Target_x86_64
<size
>::Scan::local(Symbol_table
* symtab
,
2934 Target_x86_64
<size
>* target
,
2935 Sized_relobj_file
<size
, false>* object
,
2936 unsigned int data_shndx
,
2937 Output_section
* output_section
,
2938 const elfcpp::Rela
<size
, false>& reloc
,
2939 unsigned int r_type
,
2940 const elfcpp::Sym
<size
, false>& lsym
,
2946 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2947 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
2948 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
2950 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2951 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
2956 case elfcpp::R_X86_64_NONE
:
2957 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2958 case elfcpp::R_X86_64_GNU_VTENTRY
:
2961 case elfcpp::R_X86_64_64
:
2962 // If building a shared library (or a position-independent
2963 // executable), we need to create a dynamic relocation for this
2964 // location. The relocation applied at link time will apply the
2965 // link-time value, so we flag the location with an
2966 // R_X86_64_RELATIVE relocation so the dynamic loader can
2967 // relocate it easily.
2968 if (parameters
->options().output_is_position_independent())
2970 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2971 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2972 rela_dyn
->add_local_relative(object
, r_sym
,
2974 ? elfcpp::R_X86_64_RELATIVE64
2975 : elfcpp::R_X86_64_RELATIVE
),
2976 output_section
, data_shndx
,
2977 reloc
.get_r_offset(),
2978 reloc
.get_r_addend(), is_ifunc
);
2982 case elfcpp::R_X86_64_32
:
2983 case elfcpp::R_X86_64_32S
:
2984 case elfcpp::R_X86_64_16
:
2985 case elfcpp::R_X86_64_8
:
2986 // If building a shared library (or a position-independent
2987 // executable), we need to create a dynamic relocation for this
2988 // location. We can't use an R_X86_64_RELATIVE relocation
2989 // because that is always a 64-bit relocation.
2990 if (parameters
->options().output_is_position_independent())
2992 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2993 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2995 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2996 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2997 rela_dyn
->add_local_relative(object
, r_sym
,
2998 elfcpp::R_X86_64_RELATIVE
,
2999 output_section
, data_shndx
,
3000 reloc
.get_r_offset(),
3001 reloc
.get_r_addend(), is_ifunc
);
3005 this->check_non_pic(object
, r_type
, NULL
);
3007 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3008 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3009 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
3010 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
3011 data_shndx
, reloc
.get_r_offset(),
3012 reloc
.get_r_addend());
3015 gold_assert(lsym
.get_st_value() == 0);
3016 unsigned int shndx
= lsym
.get_st_shndx();
3018 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
3021 object
->error(_("section symbol %u has bad shndx %u"),
3024 rela_dyn
->add_local_section(object
, shndx
,
3025 r_type
, output_section
,
3026 data_shndx
, reloc
.get_r_offset(),
3027 reloc
.get_r_addend());
3032 case elfcpp::R_X86_64_PC64
:
3033 case elfcpp::R_X86_64_PC32
:
3034 case elfcpp::R_X86_64_PC32_BND
:
3035 case elfcpp::R_X86_64_PC16
:
3036 case elfcpp::R_X86_64_PC8
:
3039 case elfcpp::R_X86_64_PLT32
:
3040 case elfcpp::R_X86_64_PLT32_BND
:
3041 // Since we know this is a local symbol, we can handle this as a
3045 case elfcpp::R_X86_64_GOTPC32
:
3046 case elfcpp::R_X86_64_GOTOFF64
:
3047 case elfcpp::R_X86_64_GOTPC64
:
3048 case elfcpp::R_X86_64_PLTOFF64
:
3049 // We need a GOT section.
3050 target
->got_section(symtab
, layout
);
3051 // For PLTOFF64, we'd normally want a PLT section, but since we
3052 // know this is a local symbol, no PLT is needed.
3055 case elfcpp::R_X86_64_GOT64
:
3056 case elfcpp::R_X86_64_GOT32
:
3057 case elfcpp::R_X86_64_GOTPCREL64
:
3058 case elfcpp::R_X86_64_GOTPCREL
:
3059 case elfcpp::R_X86_64_GOTPCRELX
:
3060 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3061 case elfcpp::R_X86_64_GOTPLT64
:
3063 // The symbol requires a GOT section.
3064 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
3066 // If the relocation symbol isn't IFUNC,
3067 // and is local, then we will convert
3068 // mov foo@GOTPCREL(%rip), %reg
3069 // to lea foo(%rip), %reg.
3070 // in Relocate::relocate.
3071 if (!parameters
->incremental()
3072 && (r_type
== elfcpp::R_X86_64_GOTPCREL
3073 || r_type
== elfcpp::R_X86_64_GOTPCRELX
3074 || r_type
== elfcpp::R_X86_64_REX_GOTPCRELX
)
3075 && reloc
.get_r_offset() >= 2
3078 section_size_type stype
;
3079 const unsigned char* view
= object
->section_contents(data_shndx
,
3081 if (view
[reloc
.get_r_offset() - 2] == 0x8b)
3085 // The symbol requires a GOT entry.
3086 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3088 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3089 // lets function pointers compare correctly with shared
3090 // libraries. Otherwise we would need an IRELATIVE reloc.
3093 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
3095 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
3098 // If we are generating a shared object, we need to add a
3099 // dynamic relocation for this symbol's GOT entry.
3100 if (parameters
->options().output_is_position_independent())
3102 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3103 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3104 if (r_type
!= elfcpp::R_X86_64_GOT32
)
3106 unsigned int got_offset
=
3107 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
3108 rela_dyn
->add_local_relative(object
, r_sym
,
3109 elfcpp::R_X86_64_RELATIVE
,
3110 got
, got_offset
, 0, is_ifunc
);
3114 this->check_non_pic(object
, r_type
, NULL
);
3116 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
3117 rela_dyn
->add_local(
3118 object
, r_sym
, r_type
, got
,
3119 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
3123 // For GOTPLT64, we'd normally want a PLT section, but since
3124 // we know this is a local symbol, no PLT is needed.
3128 case elfcpp::R_X86_64_COPY
:
3129 case elfcpp::R_X86_64_GLOB_DAT
:
3130 case elfcpp::R_X86_64_JUMP_SLOT
:
3131 case elfcpp::R_X86_64_RELATIVE
:
3132 case elfcpp::R_X86_64_IRELATIVE
:
3133 // These are outstanding tls relocs, which are unexpected when linking
3134 case elfcpp::R_X86_64_TPOFF64
:
3135 case elfcpp::R_X86_64_DTPMOD64
:
3136 case elfcpp::R_X86_64_TLSDESC
:
3137 gold_error(_("%s: unexpected reloc %u in object file"),
3138 object
->name().c_str(), r_type
);
3141 // These are initial tls relocs, which are expected when linking
3142 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3143 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3144 case elfcpp::R_X86_64_TLSDESC_CALL
:
3145 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3146 case elfcpp::R_X86_64_DTPOFF32
:
3147 case elfcpp::R_X86_64_DTPOFF64
:
3148 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3149 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3151 bool output_is_shared
= parameters
->options().shared();
3152 const tls::Tls_optimization optimized_type
3153 = Target_x86_64
<size
>::optimize_tls_reloc(!output_is_shared
,
3157 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
3158 if (optimized_type
== tls::TLSOPT_NONE
)
3160 // Create a pair of GOT entries for the module index and
3161 // dtv-relative offset.
3162 Output_data_got
<64, false>* got
3163 = target
->got_section(symtab
, layout
);
3164 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3165 unsigned int shndx
= lsym
.get_st_shndx();
3167 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
3169 object
->error(_("local symbol %u has bad shndx %u"),
3172 got
->add_local_pair_with_rel(object
, r_sym
,
3175 target
->rela_dyn_section(layout
),
3176 elfcpp::R_X86_64_DTPMOD64
);
3178 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3179 unsupported_reloc_local(object
, r_type
);
3182 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3183 target
->define_tls_base_symbol(symtab
, layout
);
3184 if (optimized_type
== tls::TLSOPT_NONE
)
3186 // Create reserved PLT and GOT entries for the resolver.
3187 target
->reserve_tlsdesc_entries(symtab
, layout
);
3189 // Generate a double GOT entry with an
3190 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3191 // is resolved lazily, so the GOT entry needs to be in
3192 // an area in .got.plt, not .got. Call got_section to
3193 // make sure the section has been created.
3194 target
->got_section(symtab
, layout
);
3195 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
3196 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3197 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
3199 unsigned int got_offset
= got
->add_constant(0);
3200 got
->add_constant(0);
3201 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
3203 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
3204 // We store the arguments we need in a vector, and
3205 // use the index into the vector as the parameter
3206 // to pass to the target specific routines.
3207 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
3208 void* arg
= reinterpret_cast<void*>(intarg
);
3209 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
3210 got
, got_offset
, 0);
3213 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3214 unsupported_reloc_local(object
, r_type
);
3217 case elfcpp::R_X86_64_TLSDESC_CALL
:
3220 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3221 if (optimized_type
== tls::TLSOPT_NONE
)
3223 // Create a GOT entry for the module index.
3224 target
->got_mod_index_entry(symtab
, layout
, object
);
3226 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3227 unsupported_reloc_local(object
, r_type
);
3230 case elfcpp::R_X86_64_DTPOFF32
:
3231 case elfcpp::R_X86_64_DTPOFF64
:
3234 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3235 layout
->set_has_static_tls();
3236 if (optimized_type
== tls::TLSOPT_NONE
)
3238 // Create a GOT entry for the tp-relative offset.
3239 Output_data_got
<64, false>* got
3240 = target
->got_section(symtab
, layout
);
3241 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3242 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
3243 target
->rela_dyn_section(layout
),
3244 elfcpp::R_X86_64_TPOFF64
);
3246 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3247 unsupported_reloc_local(object
, r_type
);
3250 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3251 layout
->set_has_static_tls();
3252 if (output_is_shared
)
3253 unsupported_reloc_local(object
, r_type
);
3262 case elfcpp::R_X86_64_SIZE32
:
3263 case elfcpp::R_X86_64_SIZE64
:
3265 gold_error(_("%s: unsupported reloc %u against local symbol"),
3266 object
->name().c_str(), r_type
);
3272 // Report an unsupported relocation against a global symbol.
3276 Target_x86_64
<size
>::Scan::unsupported_reloc_global(
3277 Sized_relobj_file
<size
, false>* object
,
3278 unsigned int r_type
,
3281 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3282 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
3285 // Returns true if this relocation type could be that of a function pointer.
3288 Target_x86_64
<size
>::Scan::possible_function_pointer_reloc(
3289 Sized_relobj_file
<size
, false>* src_obj
,
3290 unsigned int src_indx
,
3291 unsigned int r_offset
,
3292 unsigned int r_type
)
3296 case elfcpp::R_X86_64_64
:
3297 case elfcpp::R_X86_64_32
:
3298 case elfcpp::R_X86_64_32S
:
3299 case elfcpp::R_X86_64_16
:
3300 case elfcpp::R_X86_64_8
:
3301 case elfcpp::R_X86_64_GOT64
:
3302 case elfcpp::R_X86_64_GOT32
:
3303 case elfcpp::R_X86_64_GOTPCREL64
:
3304 case elfcpp::R_X86_64_GOTPCREL
:
3305 case elfcpp::R_X86_64_GOTPCRELX
:
3306 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3307 case elfcpp::R_X86_64_GOTPLT64
:
3311 case elfcpp::R_X86_64_PC32
:
3313 // This relocation may be used both for function calls and
3314 // for taking address of a function. We distinguish between
3315 // them by checking the opcodes.
3316 uint64_t sh_flags
= src_obj
->section_flags(src_indx
);
3317 bool is_executable
= (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0;
3320 section_size_type stype
;
3321 const unsigned char* view
= src_obj
->section_contents(src_indx
,
3327 && view
[r_offset
- 1] == 0xe8)
3332 && view
[r_offset
- 1] == 0xe9)
3335 // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
3337 && view
[r_offset
- 2] == 0x0f
3338 && view
[r_offset
- 1] >= 0x80
3339 && view
[r_offset
- 1] <= 0x8f)
3343 // Be conservative and treat all others as function pointers.
3350 // For safe ICF, scan a relocation for a local symbol to check if it
3351 // corresponds to a function pointer being taken. In that case mark
3352 // the function whose pointer was taken as not foldable.
3356 Target_x86_64
<size
>::Scan::local_reloc_may_be_function_pointer(
3359 Target_x86_64
<size
>* ,
3360 Sized_relobj_file
<size
, false>* src_obj
,
3361 unsigned int src_indx
,
3363 const elfcpp::Rela
<size
, false>& reloc
,
3364 unsigned int r_type
,
3365 const elfcpp::Sym
<size
, false>&)
3367 // When building a shared library, do not fold any local symbols as it is
3368 // not possible to distinguish pointer taken versus a call by looking at
3369 // the relocation types.
3370 if (parameters
->options().shared())
3373 return possible_function_pointer_reloc(src_obj
, src_indx
,
3374 reloc
.get_r_offset(), r_type
);
3377 // For safe ICF, scan a relocation for a global symbol to check if it
3378 // corresponds to a function pointer being taken. In that case mark
3379 // the function whose pointer was taken as not foldable.
3383 Target_x86_64
<size
>::Scan::global_reloc_may_be_function_pointer(
3386 Target_x86_64
<size
>* ,
3387 Sized_relobj_file
<size
, false>* src_obj
,
3388 unsigned int src_indx
,
3390 const elfcpp::Rela
<size
, false>& reloc
,
3391 unsigned int r_type
,
3394 // When building a shared library, do not fold symbols whose visibility
3395 // is hidden, internal or protected.
3396 if (parameters
->options().shared()
3397 && (gsym
->visibility() == elfcpp::STV_INTERNAL
3398 || gsym
->visibility() == elfcpp::STV_PROTECTED
3399 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
3402 return possible_function_pointer_reloc(src_obj
, src_indx
,
3403 reloc
.get_r_offset(), r_type
);
3406 // Scan a relocation for a global symbol.
3410 Target_x86_64
<size
>::Scan::global(Symbol_table
* symtab
,
3412 Target_x86_64
<size
>* target
,
3413 Sized_relobj_file
<size
, false>* object
,
3414 unsigned int data_shndx
,
3415 Output_section
* output_section
,
3416 const elfcpp::Rela
<size
, false>& reloc
,
3417 unsigned int r_type
,
3420 // A STT_GNU_IFUNC symbol may require a PLT entry.
3421 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
3422 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
3423 target
->make_plt_entry(symtab
, layout
, gsym
);
3427 case elfcpp::R_X86_64_NONE
:
3428 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3429 case elfcpp::R_X86_64_GNU_VTENTRY
:
3432 case elfcpp::R_X86_64_64
:
3433 case elfcpp::R_X86_64_32
:
3434 case elfcpp::R_X86_64_32S
:
3435 case elfcpp::R_X86_64_16
:
3436 case elfcpp::R_X86_64_8
:
3438 // Make a PLT entry if necessary.
3439 if (gsym
->needs_plt_entry())
3441 target
->make_plt_entry(symtab
, layout
, gsym
);
3442 // Since this is not a PC-relative relocation, we may be
3443 // taking the address of a function. In that case we need to
3444 // set the entry in the dynamic symbol table to the address of
3446 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
3447 gsym
->set_needs_dynsym_value();
3449 // Make a dynamic relocation if necessary.
3450 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
3452 if (!parameters
->options().output_is_position_independent()
3453 && gsym
->may_need_copy_reloc())
3455 target
->copy_reloc(symtab
, layout
, object
,
3456 data_shndx
, output_section
, gsym
, reloc
);
3458 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
3459 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
3460 && gsym
->type() == elfcpp::STT_GNU_IFUNC
3461 && gsym
->can_use_relative_reloc(false)
3462 && !gsym
->is_from_dynobj()
3463 && !gsym
->is_undefined()
3464 && !gsym
->is_preemptible())
3466 // Use an IRELATIVE reloc for a locally defined
3467 // STT_GNU_IFUNC symbol. This makes a function
3468 // address in a PIE executable match the address in a
3469 // shared library that it links against.
3470 Reloc_section
* rela_dyn
=
3471 target
->rela_irelative_section(layout
);
3472 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
3473 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
3474 output_section
, object
,
3476 reloc
.get_r_offset(),
3477 reloc
.get_r_addend());
3479 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
3480 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
3481 && gsym
->can_use_relative_reloc(false))
3483 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3484 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
3485 output_section
, object
,
3487 reloc
.get_r_offset(),
3488 reloc
.get_r_addend(), false);
3492 this->check_non_pic(object
, r_type
, gsym
);
3493 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3494 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
3495 data_shndx
, reloc
.get_r_offset(),
3496 reloc
.get_r_addend());
3502 case elfcpp::R_X86_64_PC64
:
3503 case elfcpp::R_X86_64_PC32
:
3504 case elfcpp::R_X86_64_PC32_BND
:
3505 case elfcpp::R_X86_64_PC16
:
3506 case elfcpp::R_X86_64_PC8
:
3508 // Make a PLT entry if necessary.
3509 if (gsym
->needs_plt_entry())
3510 target
->make_plt_entry(symtab
, layout
, gsym
);
3511 // Make a dynamic relocation if necessary.
3512 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
3514 if (parameters
->options().output_is_executable()
3515 && gsym
->may_need_copy_reloc())
3517 target
->copy_reloc(symtab
, layout
, object
,
3518 data_shndx
, output_section
, gsym
, reloc
);
3522 this->check_non_pic(object
, r_type
, gsym
);
3523 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3524 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
3525 data_shndx
, reloc
.get_r_offset(),
3526 reloc
.get_r_addend());
3532 case elfcpp::R_X86_64_GOT64
:
3533 case elfcpp::R_X86_64_GOT32
:
3534 case elfcpp::R_X86_64_GOTPCREL64
:
3535 case elfcpp::R_X86_64_GOTPCREL
:
3536 case elfcpp::R_X86_64_GOTPCRELX
:
3537 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3538 case elfcpp::R_X86_64_GOTPLT64
:
3540 // The symbol requires a GOT entry.
3541 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
3543 // If we convert this from
3544 // mov foo@GOTPCREL(%rip), %reg
3545 // to lea foo(%rip), %reg.
3548 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
3550 // in Relocate::relocate, then there is nothing to do here.
3551 // We cannot make these optimizations in incremental linking mode,
3552 // because we look at the opcode to decide whether or not to make
3553 // change, and during an incremental update, the change may have
3554 // already been applied.
3556 Lazy_view
<size
> view(object
, data_shndx
);
3557 size_t r_offset
= reloc
.get_r_offset();
3558 if (!parameters
->incremental()
3560 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
, r_type
,
3564 if (!parameters
->incremental()
3566 && Target_x86_64
<size
>::can_convert_callq_to_direct(gsym
, r_type
,
3571 if (gsym
->final_value_is_known())
3573 // For a STT_GNU_IFUNC symbol we want the PLT address.
3574 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
3575 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
3577 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
3581 // If this symbol is not fully resolved, we need to add a
3582 // dynamic relocation for it.
3583 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3585 // Use a GLOB_DAT rather than a RELATIVE reloc if:
3587 // 1) The symbol may be defined in some other module.
3589 // 2) We are building a shared library and this is a
3590 // protected symbol; using GLOB_DAT means that the dynamic
3591 // linker can use the address of the PLT in the main
3592 // executable when appropriate so that function address
3593 // comparisons work.
3595 // 3) This is a STT_GNU_IFUNC symbol in position dependent
3596 // code, again so that function address comparisons work.
3597 if (gsym
->is_from_dynobj()
3598 || gsym
->is_undefined()
3599 || gsym
->is_preemptible()
3600 || (gsym
->visibility() == elfcpp::STV_PROTECTED
3601 && parameters
->options().shared())
3602 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
3603 && parameters
->options().output_is_position_independent()))
3604 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
3605 elfcpp::R_X86_64_GLOB_DAT
);
3608 // For a STT_GNU_IFUNC symbol we want to write the PLT
3609 // offset into the GOT, so that function pointer
3610 // comparisons work correctly.
3612 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
3613 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
3616 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
3617 // Tell the dynamic linker to use the PLT address
3618 // when resolving relocations.
3619 if (gsym
->is_from_dynobj()
3620 && !parameters
->options().shared())
3621 gsym
->set_needs_dynsym_value();
3625 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
3626 rela_dyn
->add_global_relative(gsym
,
3627 elfcpp::R_X86_64_RELATIVE
,
3628 got
, got_off
, 0, false);
3635 case elfcpp::R_X86_64_PLT32
:
3636 case elfcpp::R_X86_64_PLT32_BND
:
3637 // If the symbol is fully resolved, this is just a PC32 reloc.
3638 // Otherwise we need a PLT entry.
3639 if (gsym
->final_value_is_known())
3641 // If building a shared library, we can also skip the PLT entry
3642 // if the symbol is defined in the output file and is protected
3644 if (gsym
->is_defined()
3645 && !gsym
->is_from_dynobj()
3646 && !gsym
->is_preemptible())
3648 target
->make_plt_entry(symtab
, layout
, gsym
);
3651 case elfcpp::R_X86_64_GOTPC32
:
3652 case elfcpp::R_X86_64_GOTOFF64
:
3653 case elfcpp::R_X86_64_GOTPC64
:
3654 case elfcpp::R_X86_64_PLTOFF64
:
3655 // We need a GOT section.
3656 target
->got_section(symtab
, layout
);
3657 // For PLTOFF64, we also need a PLT entry (but only if the
3658 // symbol is not fully resolved).
3659 if (r_type
== elfcpp::R_X86_64_PLTOFF64
3660 && !gsym
->final_value_is_known())
3661 target
->make_plt_entry(symtab
, layout
, gsym
);
3664 case elfcpp::R_X86_64_COPY
:
3665 case elfcpp::R_X86_64_GLOB_DAT
:
3666 case elfcpp::R_X86_64_JUMP_SLOT
:
3667 case elfcpp::R_X86_64_RELATIVE
:
3668 case elfcpp::R_X86_64_IRELATIVE
:
3669 // These are outstanding tls relocs, which are unexpected when linking
3670 case elfcpp::R_X86_64_TPOFF64
:
3671 case elfcpp::R_X86_64_DTPMOD64
:
3672 case elfcpp::R_X86_64_TLSDESC
:
3673 gold_error(_("%s: unexpected reloc %u in object file"),
3674 object
->name().c_str(), r_type
);
3677 // These are initial tls relocs, which are expected for global()
3678 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3679 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3680 case elfcpp::R_X86_64_TLSDESC_CALL
:
3681 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3682 case elfcpp::R_X86_64_DTPOFF32
:
3683 case elfcpp::R_X86_64_DTPOFF64
:
3684 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3685 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3687 // For the Initial-Exec model, we can treat undef symbols as final
3688 // when building an executable.
3689 const bool is_final
= (gsym
->final_value_is_known() ||
3690 (r_type
== elfcpp::R_X86_64_GOTTPOFF
&&
3691 gsym
->is_undefined() &&
3692 parameters
->options().output_is_executable()));
3693 const tls::Tls_optimization optimized_type
3694 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3697 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
3698 if (optimized_type
== tls::TLSOPT_NONE
)
3700 // Create a pair of GOT entries for the module index and
3701 // dtv-relative offset.
3702 Output_data_got
<64, false>* got
3703 = target
->got_section(symtab
, layout
);
3704 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
3705 target
->rela_dyn_section(layout
),
3706 elfcpp::R_X86_64_DTPMOD64
,
3707 elfcpp::R_X86_64_DTPOFF64
);
3709 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3711 // Create a GOT entry for the tp-relative offset.
3712 Output_data_got
<64, false>* got
3713 = target
->got_section(symtab
, layout
);
3714 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3715 target
->rela_dyn_section(layout
),
3716 elfcpp::R_X86_64_TPOFF64
);
3718 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3719 unsupported_reloc_global(object
, r_type
, gsym
);
3722 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3723 target
->define_tls_base_symbol(symtab
, layout
);
3724 if (optimized_type
== tls::TLSOPT_NONE
)
3726 // Create reserved PLT and GOT entries for the resolver.
3727 target
->reserve_tlsdesc_entries(symtab
, layout
);
3729 // Create a double GOT entry with an R_X86_64_TLSDESC
3730 // reloc. The R_X86_64_TLSDESC reloc is resolved
3731 // lazily, so the GOT entry needs to be in an area in
3732 // .got.plt, not .got. Call got_section to make sure
3733 // the section has been created.
3734 target
->got_section(symtab
, layout
);
3735 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
3736 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
3737 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
3738 elfcpp::R_X86_64_TLSDESC
, 0);
3740 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3742 // Create a GOT entry for the tp-relative offset.
3743 Output_data_got
<64, false>* got
3744 = target
->got_section(symtab
, layout
);
3745 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3746 target
->rela_dyn_section(layout
),
3747 elfcpp::R_X86_64_TPOFF64
);
3749 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3750 unsupported_reloc_global(object
, r_type
, gsym
);
3753 case elfcpp::R_X86_64_TLSDESC_CALL
:
3756 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3757 if (optimized_type
== tls::TLSOPT_NONE
)
3759 // Create a GOT entry for the module index.
3760 target
->got_mod_index_entry(symtab
, layout
, object
);
3762 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3763 unsupported_reloc_global(object
, r_type
, gsym
);
3766 case elfcpp::R_X86_64_DTPOFF32
:
3767 case elfcpp::R_X86_64_DTPOFF64
:
3770 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3771 layout
->set_has_static_tls();
3772 if (optimized_type
== tls::TLSOPT_NONE
)
3774 // Create a GOT entry for the tp-relative offset.
3775 Output_data_got
<64, false>* got
3776 = target
->got_section(symtab
, layout
);
3777 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3778 target
->rela_dyn_section(layout
),
3779 elfcpp::R_X86_64_TPOFF64
);
3781 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3782 unsupported_reloc_global(object
, r_type
, gsym
);
3785 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3786 layout
->set_has_static_tls();
3787 if (parameters
->options().shared())
3788 unsupported_reloc_global(object
, r_type
, gsym
);
3797 case elfcpp::R_X86_64_SIZE32
:
3798 case elfcpp::R_X86_64_SIZE64
:
3800 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3801 object
->name().c_str(), r_type
,
3802 gsym
->demangled_name().c_str());
3809 Target_x86_64
<size
>::gc_process_relocs(Symbol_table
* symtab
,
3811 Sized_relobj_file
<size
, false>* object
,
3812 unsigned int data_shndx
,
3813 unsigned int sh_type
,
3814 const unsigned char* prelocs
,
3816 Output_section
* output_section
,
3817 bool needs_special_offset_handling
,
3818 size_t local_symbol_count
,
3819 const unsigned char* plocal_symbols
)
3821 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
3824 if (sh_type
== elfcpp::SHT_REL
)
3829 gold::gc_process_relocs
<size
, false, Target_x86_64
<size
>, Scan
,
3839 needs_special_offset_handling
,
3844 // Scan relocations for a section.
3848 Target_x86_64
<size
>::scan_relocs(Symbol_table
* symtab
,
3850 Sized_relobj_file
<size
, false>* object
,
3851 unsigned int data_shndx
,
3852 unsigned int sh_type
,
3853 const unsigned char* prelocs
,
3855 Output_section
* output_section
,
3856 bool needs_special_offset_handling
,
3857 size_t local_symbol_count
,
3858 const unsigned char* plocal_symbols
)
3860 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
3863 if (sh_type
== elfcpp::SHT_REL
)
3865 gold_error(_("%s: unsupported REL reloc section"),
3866 object
->name().c_str());
3870 gold::scan_relocs
<size
, false, Target_x86_64
<size
>, Scan
, Classify_reloc
>(
3879 needs_special_offset_handling
,
3884 // Finalize the sections.
3888 Target_x86_64
<size
>::do_finalize_sections(
3890 const Input_objects
*,
3891 Symbol_table
* symtab
)
3893 const Reloc_section
* rel_plt
= (this->plt_
== NULL
3895 : this->plt_
->rela_plt());
3896 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
3897 this->rela_dyn_
, true, false);
3899 // Fill in some more dynamic tags.
3900 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
3903 if (this->plt_
!= NULL
3904 && this->plt_
->output_section() != NULL
3905 && this->plt_
->has_tlsdesc_entry())
3907 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
3908 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
3909 this->got_
->finalize_data_size();
3910 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
3911 this->plt_
, plt_offset
);
3912 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
3913 this->got_
, got_offset
);
3917 // Emit any relocs we saved in an attempt to avoid generating COPY
3919 if (this->copy_relocs_
.any_saved_relocs())
3920 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
3922 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3923 // the .got.plt section.
3924 Symbol
* sym
= this->global_offset_table_
;
3927 uint64_t data_size
= this->got_plt_
->current_data_size();
3928 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
3931 if (parameters
->doing_static_link()
3932 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
3934 // If linking statically, make sure that the __rela_iplt symbols
3935 // were defined if necessary, even if we didn't create a PLT.
3936 static const Define_symbol_in_segment syms
[] =
3939 "__rela_iplt_start", // name
3940 elfcpp::PT_LOAD
, // segment_type
3941 elfcpp::PF_W
, // segment_flags_set
3942 elfcpp::PF(0), // segment_flags_clear
3945 elfcpp::STT_NOTYPE
, // type
3946 elfcpp::STB_GLOBAL
, // binding
3947 elfcpp::STV_HIDDEN
, // visibility
3949 Symbol::SEGMENT_START
, // offset_from_base
3953 "__rela_iplt_end", // name
3954 elfcpp::PT_LOAD
, // segment_type
3955 elfcpp::PF_W
, // segment_flags_set
3956 elfcpp::PF(0), // segment_flags_clear
3959 elfcpp::STT_NOTYPE
, // type
3960 elfcpp::STB_GLOBAL
, // binding
3961 elfcpp::STV_HIDDEN
, // visibility
3963 Symbol::SEGMENT_START
, // offset_from_base
3968 symtab
->define_symbols(layout
, 2, syms
,
3969 layout
->script_options()->saw_sections_clause());
3973 // For x32, we need to handle PC-relative relocations using full 64-bit
3974 // arithmetic, so that we can detect relocation overflows properly.
3975 // This class overrides the pcrela32_check methods from the defaults in
3976 // Relocate_functions in reloc.h.
3979 class X86_64_relocate_functions
: public Relocate_functions
<size
, false>
3982 typedef Relocate_functions
<size
, false> Base
;
3984 // Do a simple PC relative relocation with the addend in the
3986 static inline typename
Base::Reloc_status
3987 pcrela32_check(unsigned char* view
,
3988 typename
elfcpp::Elf_types
<64>::Elf_Addr value
,
3989 typename
elfcpp::Elf_types
<64>::Elf_Swxword addend
,
3990 typename
elfcpp::Elf_types
<64>::Elf_Addr address
)
3992 typedef typename
elfcpp::Swap
<32, false>::Valtype Valtype
;
3993 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3994 value
= value
+ addend
- address
;
3995 elfcpp::Swap
<32, false>::writeval(wv
, value
);
3996 return (Bits
<32>::has_overflow(value
)
3997 ? Base::RELOC_OVERFLOW
: Base::RELOC_OK
);
4000 // Do a simple PC relative relocation with a Symbol_value with the
4001 // addend in the relocation.
4002 static inline typename
Base::Reloc_status
4003 pcrela32_check(unsigned char* view
,
4004 const Sized_relobj_file
<size
, false>* object
,
4005 const Symbol_value
<size
>* psymval
,
4006 typename
elfcpp::Elf_types
<64>::Elf_Swxword addend
,
4007 typename
elfcpp::Elf_types
<64>::Elf_Addr address
)
4009 typedef typename
elfcpp::Swap
<32, false>::Valtype Valtype
;
4010 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
4011 typename
elfcpp::Elf_types
<64>::Elf_Addr value
;
4013 value
= psymval
->value(object
, addend
);
4016 // For negative addends, get the symbol value without
4017 // the addend, then add the addend using 64-bit arithmetic.
4018 value
= psymval
->value(object
, 0);
4022 elfcpp::Swap
<32, false>::writeval(wv
, value
);
4023 return (Bits
<32>::has_overflow(value
)
4024 ? Base::RELOC_OVERFLOW
: Base::RELOC_OK
);
4028 // Perform a relocation.
4032 Target_x86_64
<size
>::Relocate::relocate(
4033 const Relocate_info
<size
, false>* relinfo
,
4035 Target_x86_64
<size
>* target
,
4038 const unsigned char* preloc
,
4039 const Sized_symbol
<size
>* gsym
,
4040 const Symbol_value
<size
>* psymval
,
4041 unsigned char* view
,
4042 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4043 section_size_type view_size
)
4045 typedef X86_64_relocate_functions
<size
> Reloc_funcs
;
4046 const elfcpp::Rela
<size
, false> rela(preloc
);
4047 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
4049 if (this->skip_call_tls_get_addr_
)
4051 if ((r_type
!= elfcpp::R_X86_64_PLT32
4052 && r_type
!= elfcpp::R_X86_64_GOTPCREL
4053 && r_type
!= elfcpp::R_X86_64_GOTPCRELX
4054 && r_type
!= elfcpp::R_X86_64_PLT32_BND
4055 && r_type
!= elfcpp::R_X86_64_PC32_BND
4056 && r_type
!= elfcpp::R_X86_64_PC32
)
4058 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
4060 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4061 _("missing expected TLS relocation"));
4062 this->skip_call_tls_get_addr_
= false;
4066 this->skip_call_tls_get_addr_
= false;
4074 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
4076 // Pick the value to use for symbols defined in the PLT.
4077 Symbol_value
<size
> symval
;
4079 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
4081 symval
.set_output_value(target
->plt_address_for_global(gsym
));
4084 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
4086 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4087 if (object
->local_has_plt_offset(r_sym
))
4089 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
4094 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4096 // Get the GOT offset if needed.
4097 // The GOT pointer points to the end of the GOT section.
4098 // We need to subtract the size of the GOT section to get
4099 // the actual offset to use in the relocation.
4100 bool have_got_offset
= false;
4101 // Since the actual offset is always negative, we use signed int to
4102 // support 64-bit GOT relocations.
4106 case elfcpp::R_X86_64_GOT32
:
4107 case elfcpp::R_X86_64_GOT64
:
4108 case elfcpp::R_X86_64_GOTPLT64
:
4109 case elfcpp::R_X86_64_GOTPCREL64
:
4112 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4113 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
4117 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4118 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4119 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4120 - target
->got_size());
4122 have_got_offset
= true;
4129 typename
Reloc_funcs::Reloc_status rstatus
= Reloc_funcs::RELOC_OK
;
4133 case elfcpp::R_X86_64_NONE
:
4134 case elfcpp::R_X86_64_GNU_VTINHERIT
:
4135 case elfcpp::R_X86_64_GNU_VTENTRY
:
4138 case elfcpp::R_X86_64_64
:
4139 Reloc_funcs::rela64(view
, object
, psymval
, addend
);
4142 case elfcpp::R_X86_64_PC64
:
4143 Reloc_funcs::pcrela64(view
, object
, psymval
, addend
,
4147 case elfcpp::R_X86_64_32
:
4148 rstatus
= Reloc_funcs::rela32_check(view
, object
, psymval
, addend
,
4149 Reloc_funcs::CHECK_UNSIGNED
);
4152 case elfcpp::R_X86_64_32S
:
4153 rstatus
= Reloc_funcs::rela32_check(view
, object
, psymval
, addend
,
4154 Reloc_funcs::CHECK_SIGNED
);
4157 case elfcpp::R_X86_64_PC32
:
4158 case elfcpp::R_X86_64_PC32_BND
:
4159 rstatus
= Reloc_funcs::pcrela32_check(view
, object
, psymval
, addend
,
4163 case elfcpp::R_X86_64_16
:
4164 Reloc_funcs::rela16(view
, object
, psymval
, addend
);
4167 case elfcpp::R_X86_64_PC16
:
4168 Reloc_funcs::pcrela16(view
, object
, psymval
, addend
, address
);
4171 case elfcpp::R_X86_64_8
:
4172 Reloc_funcs::rela8(view
, object
, psymval
, addend
);
4175 case elfcpp::R_X86_64_PC8
:
4176 Reloc_funcs::pcrela8(view
, object
, psymval
, addend
, address
);
4179 case elfcpp::R_X86_64_PLT32
:
4180 case elfcpp::R_X86_64_PLT32_BND
:
4181 gold_assert(gsym
== NULL
4182 || gsym
->has_plt_offset()
4183 || gsym
->final_value_is_known()
4184 || (gsym
->is_defined()
4185 && !gsym
->is_from_dynobj()
4186 && !gsym
->is_preemptible()));
4187 // Note: while this code looks the same as for R_X86_64_PC32, it
4188 // behaves differently because psymval was set to point to
4189 // the PLT entry, rather than the symbol, in Scan::global().
4190 rstatus
= Reloc_funcs::pcrela32_check(view
, object
, psymval
, addend
,
4194 case elfcpp::R_X86_64_PLTOFF64
:
4197 gold_assert(gsym
->has_plt_offset()
4198 || gsym
->final_value_is_known());
4199 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
;
4200 // This is the address of GLOBAL_OFFSET_TABLE.
4201 got_address
= target
->got_plt_section()->address();
4202 Reloc_funcs::rela64(view
, object
, psymval
, addend
- got_address
);
4206 case elfcpp::R_X86_64_GOT32
:
4207 gold_assert(have_got_offset
);
4208 Reloc_funcs::rela32(view
, got_offset
, addend
);
4211 case elfcpp::R_X86_64_GOTPC32
:
4214 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4215 value
= target
->got_plt_section()->address();
4216 Reloc_funcs::pcrela32_check(view
, value
, addend
, address
);
4220 case elfcpp::R_X86_64_GOT64
:
4221 case elfcpp::R_X86_64_GOTPLT64
:
4222 // R_X86_64_GOTPLT64 is obsolete and treated the same as
4224 gold_assert(have_got_offset
);
4225 Reloc_funcs::rela64(view
, got_offset
, addend
);
4228 case elfcpp::R_X86_64_GOTPC64
:
4231 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4232 value
= target
->got_plt_section()->address();
4233 Reloc_funcs::pcrela64(view
, value
, addend
, address
);
4237 case elfcpp::R_X86_64_GOTOFF64
:
4239 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4240 value
= (psymval
->value(object
, 0)
4241 - target
->got_plt_section()->address());
4242 Reloc_funcs::rela64(view
, value
, addend
);
4246 case elfcpp::R_X86_64_GOTPCREL
:
4247 case elfcpp::R_X86_64_GOTPCRELX
:
4248 case elfcpp::R_X86_64_REX_GOTPCRELX
:
4251 // mov foo@GOTPCREL(%rip), %reg
4252 // to lea foo(%rip), %reg.
4254 if (!parameters
->incremental()
4256 && rela
.get_r_offset() >= 2
4258 && !psymval
->is_ifunc_symbol())
4260 && rela
.get_r_offset() >= 2
4261 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
, r_type
,
4265 Reloc_funcs::pcrela32(view
, object
, psymval
, addend
, address
);
4268 // callq *foo@GOTPCRELX(%rip) to
4270 // and jmpq *foo@GOTPCRELX(%rip) to
4273 else if (!parameters
->incremental()
4275 && rela
.get_r_offset() >= 2
4276 && Target_x86_64
<size
>::can_convert_callq_to_direct(gsym
,
4280 if (view
[-1] == 0x15)
4282 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4283 // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
4286 // Convert GOTPCRELX to 32-bit pc relative reloc.
4287 Reloc_funcs::pcrela32(view
, object
, psymval
, addend
, address
);
4291 // Convert jmpq *foo@GOTPCRELX(%rip) to
4294 // The opcode of direct jmpq is 0xe9.
4296 // The opcode of nop is 0x90.
4298 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip
4299 // relative and since the instruction following the jmpq is now
4300 // the nop, offset the address by 1 byte. The start of the
4301 // relocation also moves ahead by 1 byte.
4302 Reloc_funcs::pcrela32(&view
[-1], object
, psymval
, addend
,
4310 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4311 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
4312 - target
->got_size());
4316 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4317 gold_assert(object
->local_has_got_offset(r_sym
,
4318 GOT_TYPE_STANDARD
));
4319 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4320 - target
->got_size());
4322 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4323 value
= target
->got_plt_section()->address() + got_offset
;
4324 Reloc_funcs::pcrela32_check(view
, value
, addend
, address
);
4329 case elfcpp::R_X86_64_GOTPCREL64
:
4331 gold_assert(have_got_offset
);
4332 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4333 value
= target
->got_plt_section()->address() + got_offset
;
4334 Reloc_funcs::pcrela64(view
, value
, addend
, address
);
4338 case elfcpp::R_X86_64_COPY
:
4339 case elfcpp::R_X86_64_GLOB_DAT
:
4340 case elfcpp::R_X86_64_JUMP_SLOT
:
4341 case elfcpp::R_X86_64_RELATIVE
:
4342 case elfcpp::R_X86_64_IRELATIVE
:
4343 // These are outstanding tls relocs, which are unexpected when linking
4344 case elfcpp::R_X86_64_TPOFF64
:
4345 case elfcpp::R_X86_64_DTPMOD64
:
4346 case elfcpp::R_X86_64_TLSDESC
:
4347 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4348 _("unexpected reloc %u in object file"),
4352 // These are initial tls relocs, which are expected when linking
4353 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4354 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4355 case elfcpp::R_X86_64_TLSDESC_CALL
:
4356 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4357 case elfcpp::R_X86_64_DTPOFF32
:
4358 case elfcpp::R_X86_64_DTPOFF64
:
4359 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4360 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4361 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
4362 view
, address
, view_size
);
4365 case elfcpp::R_X86_64_SIZE32
:
4366 case elfcpp::R_X86_64_SIZE64
:
4368 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4369 _("unsupported reloc %u"),
4374 if (rstatus
== Reloc_funcs::RELOC_OVERFLOW
)
4378 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4379 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4380 _("relocation overflow: "
4381 "reference to local symbol %u in %s"),
4382 r_sym
, object
->name().c_str());
4384 else if (gsym
->is_defined() && gsym
->source() == Symbol::FROM_OBJECT
)
4386 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4387 _("relocation overflow: "
4388 "reference to '%s' defined in %s"),
4390 gsym
->object()->name().c_str());
4394 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4395 _("relocation overflow: reference to '%s'"),
4403 // Perform a TLS relocation.
4407 Target_x86_64
<size
>::Relocate::relocate_tls(
4408 const Relocate_info
<size
, false>* relinfo
,
4409 Target_x86_64
<size
>* target
,
4411 const elfcpp::Rela
<size
, false>& rela
,
4412 unsigned int r_type
,
4413 const Sized_symbol
<size
>* gsym
,
4414 const Symbol_value
<size
>* psymval
,
4415 unsigned char* view
,
4416 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4417 section_size_type view_size
)
4419 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
4421 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
4422 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4423 elfcpp::Shdr
<size
, false> data_shdr(relinfo
->data_shdr
);
4424 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
4426 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
4428 const bool is_final
= (gsym
== NULL
4429 ? !parameters
->options().shared()
4430 : gsym
->final_value_is_known());
4431 tls::Tls_optimization optimized_type
4432 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
4435 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4436 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
4438 // If this code sequence is used in a non-executable section,
4439 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
4440 // on the assumption that it's being used by itself in a debug
4441 // section. Therefore, in the unlikely event that the code
4442 // sequence appears in a non-executable section, we simply
4443 // leave it unoptimized.
4444 optimized_type
= tls::TLSOPT_NONE
;
4446 if (optimized_type
== tls::TLSOPT_TO_LE
)
4448 if (tls_segment
== NULL
)
4450 gold_assert(parameters
->errors()->error_count() > 0
4451 || issue_undefined_symbol_error(gsym
));
4454 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
4455 rela
, r_type
, value
, view
,
4461 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
4462 ? GOT_TYPE_TLS_OFFSET
4463 : GOT_TYPE_TLS_PAIR
);
4464 unsigned int got_offset
;
4467 gold_assert(gsym
->has_got_offset(got_type
));
4468 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
4472 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4473 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
4474 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
4475 - target
->got_size());
4477 if (optimized_type
== tls::TLSOPT_TO_IE
)
4479 value
= target
->got_plt_section()->address() + got_offset
;
4480 this->tls_gd_to_ie(relinfo
, relnum
, rela
, r_type
,
4481 value
, view
, address
, view_size
);
4484 else if (optimized_type
== tls::TLSOPT_NONE
)
4486 // Relocate the field with the offset of the pair of GOT
4488 value
= target
->got_plt_section()->address() + got_offset
;
4489 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
4494 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4495 _("unsupported reloc %u"), r_type
);
4498 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4499 case elfcpp::R_X86_64_TLSDESC_CALL
:
4500 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
4502 // See above comment for R_X86_64_TLSGD.
4503 optimized_type
= tls::TLSOPT_NONE
;
4505 if (optimized_type
== tls::TLSOPT_TO_LE
)
4507 if (tls_segment
== NULL
)
4509 gold_assert(parameters
->errors()->error_count() > 0
4510 || issue_undefined_symbol_error(gsym
));
4513 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
4514 rela
, r_type
, value
, view
,
4520 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
4521 ? GOT_TYPE_TLS_OFFSET
4522 : GOT_TYPE_TLS_DESC
);
4523 unsigned int got_offset
= 0;
4524 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
4525 && optimized_type
== tls::TLSOPT_NONE
)
4527 // We created GOT entries in the .got.tlsdesc portion of
4528 // the .got.plt section, but the offset stored in the
4529 // symbol is the offset within .got.tlsdesc.
4530 got_offset
= (target
->got_size()
4531 + target
->got_plt_section()->data_size());
4535 gold_assert(gsym
->has_got_offset(got_type
));
4536 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
4540 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4541 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
4542 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
4543 - target
->got_size());
4545 if (optimized_type
== tls::TLSOPT_TO_IE
)
4547 value
= target
->got_plt_section()->address() + got_offset
;
4548 this->tls_desc_gd_to_ie(relinfo
, relnum
,
4549 rela
, r_type
, value
, view
, address
,
4553 else if (optimized_type
== tls::TLSOPT_NONE
)
4555 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4557 // Relocate the field with the offset of the pair of GOT
4559 value
= target
->got_plt_section()->address() + got_offset
;
4560 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
4566 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4567 _("unsupported reloc %u"), r_type
);
4570 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4571 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
4573 // See above comment for R_X86_64_TLSGD.
4574 optimized_type
= tls::TLSOPT_NONE
;
4576 if (optimized_type
== tls::TLSOPT_TO_LE
)
4578 if (tls_segment
== NULL
)
4580 gold_assert(parameters
->errors()->error_count() > 0
4581 || issue_undefined_symbol_error(gsym
));
4584 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
4585 value
, view
, view_size
);
4588 else if (optimized_type
== tls::TLSOPT_NONE
)
4590 // Relocate the field with the offset of the GOT entry for
4591 // the module index.
4592 unsigned int got_offset
;
4593 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
4594 - target
->got_size());
4595 value
= target
->got_plt_section()->address() + got_offset
;
4596 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
4600 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4601 _("unsupported reloc %u"), r_type
);
4604 case elfcpp::R_X86_64_DTPOFF32
:
4605 // This relocation type is used in debugging information.
4606 // In that case we need to not optimize the value. If the
4607 // section is not executable, then we assume we should not
4608 // optimize this reloc. See comments above for R_X86_64_TLSGD,
4609 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
4611 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
4613 if (tls_segment
== NULL
)
4615 gold_assert(parameters
->errors()->error_count() > 0
4616 || issue_undefined_symbol_error(gsym
));
4619 value
-= tls_segment
->memsz();
4621 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
4624 case elfcpp::R_X86_64_DTPOFF64
:
4625 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
4626 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
4628 if (tls_segment
== NULL
)
4630 gold_assert(parameters
->errors()->error_count() > 0
4631 || issue_undefined_symbol_error(gsym
));
4634 value
-= tls_segment
->memsz();
4636 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
4639 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4641 && gsym
->is_undefined()
4642 && parameters
->options().output_is_executable())
4644 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
4646 r_type
, value
, view
,
4650 else if (optimized_type
== tls::TLSOPT_TO_LE
)
4652 if (tls_segment
== NULL
)
4654 gold_assert(parameters
->errors()->error_count() > 0
4655 || issue_undefined_symbol_error(gsym
));
4658 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
4660 r_type
, value
, view
,
4664 else if (optimized_type
== tls::TLSOPT_NONE
)
4666 // Relocate the field with the offset of the GOT entry for
4667 // the tp-relative offset of the symbol.
4668 unsigned int got_offset
;
4671 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
4672 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
4673 - target
->got_size());
4677 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4678 gold_assert(object
->local_has_got_offset(r_sym
,
4679 GOT_TYPE_TLS_OFFSET
));
4680 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
4681 - target
->got_size());
4683 value
= target
->got_plt_section()->address() + got_offset
;
4684 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
4688 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4689 _("unsupported reloc type %u"),
4693 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4694 if (tls_segment
== NULL
)
4696 gold_assert(parameters
->errors()->error_count() > 0
4697 || issue_undefined_symbol_error(gsym
));
4700 value
-= tls_segment
->memsz();
4701 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
4706 // Do a relocation in which we convert a TLS General-Dynamic to an
4711 Target_x86_64
<size
>::Relocate::tls_gd_to_ie(
4712 const Relocate_info
<size
, false>* relinfo
,
4714 const elfcpp::Rela
<size
, false>& rela
,
4716 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4717 unsigned char* view
,
4718 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4719 section_size_type view_size
)
4722 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4723 // .word 0x6666; rex64; call __tls_get_addr@PLT
4724 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4725 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4726 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4727 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4729 // leaq foo@tlsgd(%rip),%rdi;
4730 // .word 0x6666; rex64; call __tls_get_addr@PLT
4731 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4732 // leaq foo@tlsgd(%rip),%rdi;
4733 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4734 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4736 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
4737 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4738 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0
4739 || memcmp(view
+ 4, "\x66\x48\xff", 3) == 0));
4743 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4745 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4746 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
4747 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4752 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4754 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4755 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
4756 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4760 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4761 Relocate_functions
<size
, false>::pcrela32(view
+ 8, value
, addend
- 8,
4764 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4766 this->skip_call_tls_get_addr_
= true;
4769 // Do a relocation in which we convert a TLS General-Dynamic to a
4774 Target_x86_64
<size
>::Relocate::tls_gd_to_le(
4775 const Relocate_info
<size
, false>* relinfo
,
4777 Output_segment
* tls_segment
,
4778 const elfcpp::Rela
<size
, false>& rela
,
4780 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4781 unsigned char* view
,
4782 section_size_type view_size
)
4785 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4786 // .word 0x6666; rex64; call __tls_get_addr@PLT
4787 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4788 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4789 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4790 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4792 // leaq foo@tlsgd(%rip),%rdi;
4793 // .word 0x6666; rex64; call __tls_get_addr@PLT
4794 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4795 // leaq foo@tlsgd(%rip),%rdi;
4796 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4797 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4799 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
4800 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4801 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0
4802 || memcmp(view
+ 4, "\x66\x48\xff", 3) == 0));
4806 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4808 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4809 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
4810 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4815 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4817 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4818 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
4820 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4824 value
-= tls_segment
->memsz();
4825 Relocate_functions
<size
, false>::rela32(view
+ 8, value
, 0);
4827 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4829 this->skip_call_tls_get_addr_
= true;
4832 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4836 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_ie(
4837 const Relocate_info
<size
, false>* relinfo
,
4839 const elfcpp::Rela
<size
, false>& rela
,
4840 unsigned int r_type
,
4841 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4842 unsigned char* view
,
4843 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4844 section_size_type view_size
)
4846 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4848 // leaq foo@tlsdesc(%rip), %rax
4849 // ==> movq foo@gottpoff(%rip), %rax
4850 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4851 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4852 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4853 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
4855 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4856 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
4860 // call *foo@tlscall(%rax)
4862 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
4863 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
4864 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4865 view
[0] == 0xff && view
[1] == 0x10);
4871 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4875 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_le(
4876 const Relocate_info
<size
, false>* relinfo
,
4878 Output_segment
* tls_segment
,
4879 const elfcpp::Rela
<size
, false>& rela
,
4880 unsigned int r_type
,
4881 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4882 unsigned char* view
,
4883 section_size_type view_size
)
4885 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4887 // leaq foo@tlsdesc(%rip), %rax
4888 // ==> movq foo@tpoff, %rax
4889 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4890 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4891 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4892 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
4895 value
-= tls_segment
->memsz();
4896 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
4900 // call *foo@tlscall(%rax)
4902 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
4903 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
4904 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4905 view
[0] == 0xff && view
[1] == 0x10);
4913 Target_x86_64
<size
>::Relocate::tls_ld_to_le(
4914 const Relocate_info
<size
, false>* relinfo
,
4917 const elfcpp::Rela
<size
, false>& rela
,
4919 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
4920 unsigned char* view
,
4921 section_size_type view_size
)
4923 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4925 // ... leq foo@dtpoff(%rax),%reg
4926 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4928 // ... leq foo@dtpoff(%rax),%reg
4929 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4930 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
4932 // ... leq foo@dtpoff(%rax),%reg
4933 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4935 // ... leq foo@dtpoff(%rax),%reg
4936 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4938 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4939 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
4941 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4942 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
4944 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4945 view
[4] == 0xe8 || view
[4] == 0xff);
4947 if (view
[4] == 0xe8)
4950 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4952 memcpy(view
- 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4957 memcpy(view
- 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
4960 memcpy(view
- 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
4964 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4966 this->skip_call_tls_get_addr_
= true;
4969 // Do a relocation in which we convert a TLS Initial-Exec to a
4974 Target_x86_64
<size
>::Relocate::tls_ie_to_le(
4975 const Relocate_info
<size
, false>* relinfo
,
4977 Output_segment
* tls_segment
,
4978 const elfcpp::Rela
<size
, false>& rela
,
4980 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4981 unsigned char* view
,
4982 section_size_type view_size
)
4984 // We need to examine the opcodes to figure out which instruction we
4987 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4988 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4990 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4991 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4993 unsigned char op1
= view
[-3];
4994 unsigned char op2
= view
[-2];
4995 unsigned char op3
= view
[-1];
4996 unsigned char reg
= op3
>> 3;
5003 else if (size
== 32 && op1
== 0x44)
5006 view
[-1] = 0xc0 | reg
;
5010 // Special handling for %rsp.
5013 else if (size
== 32 && op1
== 0x44)
5016 view
[-1] = 0xc0 | reg
;
5023 else if (size
== 32 && op1
== 0x44)
5026 view
[-1] = 0x80 | reg
| (reg
<< 3);
5029 if (tls_segment
!= NULL
)
5030 value
-= tls_segment
->memsz();
5031 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
5034 // Relocate section data.
5038 Target_x86_64
<size
>::relocate_section(
5039 const Relocate_info
<size
, false>* relinfo
,
5040 unsigned int sh_type
,
5041 const unsigned char* prelocs
,
5043 Output_section
* output_section
,
5044 bool needs_special_offset_handling
,
5045 unsigned char* view
,
5046 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5047 section_size_type view_size
,
5048 const Reloc_symbol_changes
* reloc_symbol_changes
)
5050 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5053 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5055 gold::relocate_section
<size
, false, Target_x86_64
<size
>, Relocate
,
5056 gold::Default_comdat_behavior
, Classify_reloc
>(
5062 needs_special_offset_handling
,
5066 reloc_symbol_changes
);
5069 // Apply an incremental relocation. Incremental relocations always refer
5070 // to global symbols.
5074 Target_x86_64
<size
>::apply_relocation(
5075 const Relocate_info
<size
, false>* relinfo
,
5076 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
5077 unsigned int r_type
,
5078 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
5080 unsigned char* view
,
5081 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5082 section_size_type view_size
)
5084 gold::apply_relocation
<size
, false, Target_x86_64
<size
>,
5085 typename Target_x86_64
<size
>::Relocate
>(
5097 // Scan the relocs during a relocatable link.
5101 Target_x86_64
<size
>::scan_relocatable_relocs(
5102 Symbol_table
* symtab
,
5104 Sized_relobj_file
<size
, false>* object
,
5105 unsigned int data_shndx
,
5106 unsigned int sh_type
,
5107 const unsigned char* prelocs
,
5109 Output_section
* output_section
,
5110 bool needs_special_offset_handling
,
5111 size_t local_symbol_count
,
5112 const unsigned char* plocal_symbols
,
5113 Relocatable_relocs
* rr
)
5115 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5117 typedef gold::Default_scan_relocatable_relocs
<Classify_reloc
>
5118 Scan_relocatable_relocs
;
5120 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5122 gold::scan_relocatable_relocs
<size
, false, Scan_relocatable_relocs
>(
5130 needs_special_offset_handling
,
5136 // Scan the relocs for --emit-relocs.
5140 Target_x86_64
<size
>::emit_relocs_scan(
5141 Symbol_table
* symtab
,
5143 Sized_relobj_file
<size
, false>* object
,
5144 unsigned int data_shndx
,
5145 unsigned int sh_type
,
5146 const unsigned char* prelocs
,
5148 Output_section
* output_section
,
5149 bool needs_special_offset_handling
,
5150 size_t local_symbol_count
,
5151 const unsigned char* plocal_syms
,
5152 Relocatable_relocs
* rr
)
5154 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5156 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
5157 Emit_relocs_strategy
;
5159 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5161 gold::scan_relocatable_relocs
<size
, false, Emit_relocs_strategy
>(
5169 needs_special_offset_handling
,
5175 // Relocate a section during a relocatable link.
5179 Target_x86_64
<size
>::relocate_relocs(
5180 const Relocate_info
<size
, false>* relinfo
,
5181 unsigned int sh_type
,
5182 const unsigned char* prelocs
,
5184 Output_section
* output_section
,
5185 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
5186 unsigned char* view
,
5187 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
5188 section_size_type view_size
,
5189 unsigned char* reloc_view
,
5190 section_size_type reloc_view_size
)
5192 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5195 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5197 gold::relocate_relocs
<size
, false, Classify_reloc
>(
5202 offset_in_output_section
,
5210 // Return the value to use for a dynamic which requires special
5211 // treatment. This is how we support equality comparisons of function
5212 // pointers across shared library boundaries, as described in the
5213 // processor specific ABI supplement.
5217 Target_x86_64
<size
>::do_dynsym_value(const Symbol
* gsym
) const
5219 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
5220 return this->plt_address_for_global(gsym
);
5223 // Return a string used to fill a code section with nops to take up
5224 // the specified length.
5228 Target_x86_64
<size
>::do_code_fill(section_size_type length
) const
5232 // Build a jmpq instruction to skip over the bytes.
5233 unsigned char jmp
[5];
5235 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
5236 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
5237 + std::string(length
- 5, static_cast<char>(0x90)));
5240 // Nop sequences of various lengths.
5241 const char nop1
[1] = { '\x90' }; // nop
5242 const char nop2
[2] = { '\x66', '\x90' }; // xchg %ax %ax
5243 const char nop3
[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5244 const char nop4
[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5246 const char nop5
[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5248 const char nop6
[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5249 '\x44', '\x00', '\x00' };
5250 const char nop7
[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5251 '\x00', '\x00', '\x00',
5253 const char nop8
[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5254 '\x00', '\x00', '\x00',
5256 const char nop9
[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5257 '\x84', '\x00', '\x00',
5258 '\x00', '\x00', '\x00' };
5259 const char nop10
[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5260 '\x1f', '\x84', '\x00',
5261 '\x00', '\x00', '\x00',
5263 const char nop11
[11] = { '\x66', '\x66', '\x2e', // data16
5264 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5265 '\x00', '\x00', '\x00',
5267 const char nop12
[12] = { '\x66', '\x66', '\x66', // data16; data16
5268 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5269 '\x84', '\x00', '\x00',
5270 '\x00', '\x00', '\x00' };
5271 const char nop13
[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5272 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5273 '\x1f', '\x84', '\x00',
5274 '\x00', '\x00', '\x00',
5276 const char nop14
[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5277 '\x66', '\x66', '\x2e', // data16
5278 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5279 '\x00', '\x00', '\x00',
5281 const char nop15
[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5282 '\x66', '\x66', '\x66', // data16; data16
5283 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5284 '\x84', '\x00', '\x00',
5285 '\x00', '\x00', '\x00' };
5287 const char* nops
[16] = {
5289 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
5290 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
5293 return std::string(nops
[length
], length
);
5296 // Return the addend to use for a target specific relocation. The
5297 // only target specific relocation is R_X86_64_TLSDESC for a local
5298 // symbol. We want to set the addend is the offset of the local
5299 // symbol in the TLS segment.
5303 Target_x86_64
<size
>::do_reloc_addend(void* arg
, unsigned int r_type
,
5306 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
5307 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
5308 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
5309 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
5310 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
5311 gold_assert(psymval
->is_tls_symbol());
5312 // The value of a TLS symbol is the offset in the TLS segment.
5313 return psymval
->value(ti
.object
, 0);
5316 // Return the value to use for the base of a DW_EH_PE_datarel offset
5317 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
5318 // assembler can not write out the difference between two labels in
5319 // different sections, so instead of using a pc-relative value they
5320 // use an offset from the GOT.
5324 Target_x86_64
<size
>::do_ehframe_datarel_base() const
5326 gold_assert(this->global_offset_table_
!= NULL
);
5327 Symbol
* sym
= this->global_offset_table_
;
5328 Sized_symbol
<size
>* ssym
= static_cast<Sized_symbol
<size
>*>(sym
);
5329 return ssym
->value();
5332 // FNOFFSET in section SHNDX in OBJECT is the start of a function
5333 // compiled with -fsplit-stack. The function calls non-split-stack
5334 // code. We have to change the function so that it always ensures
5335 // that it has enough stack space to run some random function.
5337 static const unsigned char cmp_insn_32
[] = { 0x64, 0x3b, 0x24, 0x25 };
5338 static const unsigned char lea_r10_insn_32
[] = { 0x44, 0x8d, 0x94, 0x24 };
5339 static const unsigned char lea_r11_insn_32
[] = { 0x44, 0x8d, 0x9c, 0x24 };
5341 static const unsigned char cmp_insn_64
[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
5342 static const unsigned char lea_r10_insn_64
[] = { 0x4c, 0x8d, 0x94, 0x24 };
5343 static const unsigned char lea_r11_insn_64
[] = { 0x4c, 0x8d, 0x9c, 0x24 };
5347 Target_x86_64
<size
>::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
5348 section_offset_type fnoffset
,
5349 section_size_type fnsize
,
5350 const unsigned char*,
5352 unsigned char* view
,
5353 section_size_type view_size
,
5355 std::string
* to
) const
5357 const char* const cmp_insn
= reinterpret_cast<const char*>
5358 (size
== 32 ? cmp_insn_32
: cmp_insn_64
);
5359 const char* const lea_r10_insn
= reinterpret_cast<const char*>
5360 (size
== 32 ? lea_r10_insn_32
: lea_r10_insn_64
);
5361 const char* const lea_r11_insn
= reinterpret_cast<const char*>
5362 (size
== 32 ? lea_r11_insn_32
: lea_r11_insn_64
);
5364 const size_t cmp_insn_len
=
5365 (size
== 32 ? sizeof(cmp_insn_32
) : sizeof(cmp_insn_64
));
5366 const size_t lea_r10_insn_len
=
5367 (size
== 32 ? sizeof(lea_r10_insn_32
) : sizeof(lea_r10_insn_64
));
5368 const size_t lea_r11_insn_len
=
5369 (size
== 32 ? sizeof(lea_r11_insn_32
) : sizeof(lea_r11_insn_64
));
5370 const size_t nop_len
= (size
== 32 ? 7 : 8);
5372 // The function starts with a comparison of the stack pointer and a
5373 // field in the TCB. This is followed by a jump.
5376 if (this->match_view(view
, view_size
, fnoffset
, cmp_insn
, cmp_insn_len
)
5377 && fnsize
> nop_len
+ 1)
5379 // We will call __morestack if the carry flag is set after this
5380 // comparison. We turn the comparison into an stc instruction
5382 view
[fnoffset
] = '\xf9';
5383 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, nop_len
);
5385 // lea NN(%rsp),%r10
5386 // lea NN(%rsp),%r11
5387 else if ((this->match_view(view
, view_size
, fnoffset
,
5388 lea_r10_insn
, lea_r10_insn_len
)
5389 || this->match_view(view
, view_size
, fnoffset
,
5390 lea_r11_insn
, lea_r11_insn_len
))
5393 // This is loading an offset from the stack pointer for a
5394 // comparison. The offset is negative, so we decrease the
5395 // offset by the amount of space we need for the stack. This
5396 // means we will avoid calling __morestack if there happens to
5397 // be plenty of space on the stack already.
5398 unsigned char* pval
= view
+ fnoffset
+ 4;
5399 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
5400 val
-= parameters
->options().split_stack_adjust_size();
5401 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
5405 if (!object
->has_no_split_stack())
5406 object
->error(_("failed to match split-stack sequence at "
5407 "section %u offset %0zx"),
5408 shndx
, static_cast<size_t>(fnoffset
));
5412 // We have to change the function so that it calls
5413 // __morestack_non_split instead of __morestack. The former will
5414 // allocate additional stack space.
5415 *from
= "__morestack";
5416 *to
= "__morestack_non_split";
5419 // The selector for x86_64 object files. Note this is never instantiated
5420 // directly. It's only used in Target_selector_x86_64_nacl, below.
5423 class Target_selector_x86_64
: public Target_selector_freebsd
5426 Target_selector_x86_64()
5427 : Target_selector_freebsd(elfcpp::EM_X86_64
, size
, false,
5429 ? "elf64-x86-64" : "elf32-x86-64"),
5431 ? "elf64-x86-64-freebsd"
5432 : "elf32-x86-64-freebsd"),
5433 (size
== 64 ? "elf_x86_64" : "elf32_x86_64"))
5437 do_instantiate_target()
5438 { return new Target_x86_64
<size
>(); }
5442 // NaCl variant. It uses different PLT contents.
5445 class Output_data_plt_x86_64_nacl
: public Output_data_plt_x86_64
<size
>
5448 Output_data_plt_x86_64_nacl(Layout
* layout
,
5449 Output_data_got
<64, false>* got
,
5450 Output_data_got_plt_x86_64
* got_plt
,
5451 Output_data_space
* got_irelative
)
5452 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
5453 got
, got_plt
, got_irelative
)
5456 Output_data_plt_x86_64_nacl(Layout
* layout
,
5457 Output_data_got
<64, false>* got
,
5458 Output_data_got_plt_x86_64
* got_plt
,
5459 Output_data_space
* got_irelative
,
5460 unsigned int plt_count
)
5461 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
5462 got
, got_plt
, got_irelative
,
5467 virtual unsigned int
5468 do_get_plt_entry_size() const
5469 { return plt_entry_size
; }
5472 do_add_eh_frame(Layout
* layout
)
5474 layout
->add_eh_frame_for_plt(this,
5475 this->plt_eh_frame_cie
,
5476 this->plt_eh_frame_cie_size
,
5478 plt_eh_frame_fde_size
);
5482 do_fill_first_plt_entry(unsigned char* pov
,
5483 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
5484 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
5486 virtual unsigned int
5487 do_fill_plt_entry(unsigned char* pov
,
5488 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5489 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
5490 unsigned int got_offset
,
5491 unsigned int plt_offset
,
5492 unsigned int plt_index
);
5495 do_fill_tlsdesc_entry(unsigned char* pov
,
5496 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5497 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
5498 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
5499 unsigned int tlsdesc_got_offset
,
5500 unsigned int plt_offset
);
5503 // The size of an entry in the PLT.
5504 static const int plt_entry_size
= 64;
5506 // The first entry in the PLT.
5507 static const unsigned char first_plt_entry
[plt_entry_size
];
5509 // Other entries in the PLT for an executable.
5510 static const unsigned char plt_entry
[plt_entry_size
];
5512 // The reserved TLSDESC entry in the PLT for an executable.
5513 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
5515 // The .eh_frame unwind information for the PLT.
5516 static const int plt_eh_frame_fde_size
= 32;
5517 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
5521 class Target_x86_64_nacl
: public Target_x86_64
<size
>
5524 Target_x86_64_nacl()
5525 : Target_x86_64
<size
>(&x86_64_nacl_info
)
5528 virtual Output_data_plt_x86_64
<size
>*
5529 do_make_data_plt(Layout
* layout
,
5530 Output_data_got
<64, false>* got
,
5531 Output_data_got_plt_x86_64
* got_plt
,
5532 Output_data_space
* got_irelative
)
5534 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
5538 virtual Output_data_plt_x86_64
<size
>*
5539 do_make_data_plt(Layout
* layout
,
5540 Output_data_got
<64, false>* got
,
5541 Output_data_got_plt_x86_64
* got_plt
,
5542 Output_data_space
* got_irelative
,
5543 unsigned int plt_count
)
5545 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
5551 do_code_fill(section_size_type length
) const;
5554 static const Target::Target_info x86_64_nacl_info
;
5558 const Target::Target_info Target_x86_64_nacl
<64>::x86_64_nacl_info
=
5561 false, // is_big_endian
5562 elfcpp::EM_X86_64
, // machine_code
5563 false, // has_make_symbol
5564 false, // has_resolve
5565 true, // has_code_fill
5566 true, // is_default_stack_executable
5567 true, // can_icf_inline_merge_sections
5569 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
5570 0x20000, // default_text_segment_address
5571 0x10000, // abi_pagesize (overridable by -z max-page-size)
5572 0x10000, // common_pagesize (overridable by -z common-page-size)
5573 true, // isolate_execinstr
5574 0x10000000, // rosegment_gap
5575 elfcpp::SHN_UNDEF
, // small_common_shndx
5576 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
5577 0, // small_common_section_flags
5578 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
5579 NULL
, // attributes_section
5580 NULL
, // attributes_vendor
5581 "_start", // entry_symbol_name
5582 32, // hash_entry_size
5583 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
5587 const Target::Target_info Target_x86_64_nacl
<32>::x86_64_nacl_info
=
5590 false, // is_big_endian
5591 elfcpp::EM_X86_64
, // machine_code
5592 false, // has_make_symbol
5593 false, // has_resolve
5594 true, // has_code_fill
5595 true, // is_default_stack_executable
5596 true, // can_icf_inline_merge_sections
5598 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
5599 0x20000, // default_text_segment_address
5600 0x10000, // abi_pagesize (overridable by -z max-page-size)
5601 0x10000, // common_pagesize (overridable by -z common-page-size)
5602 true, // isolate_execinstr
5603 0x10000000, // rosegment_gap
5604 elfcpp::SHN_UNDEF
, // small_common_shndx
5605 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
5606 0, // small_common_section_flags
5607 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
5608 NULL
, // attributes_section
5609 NULL
, // attributes_vendor
5610 "_start", // entry_symbol_name
5611 32, // hash_entry_size
5612 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
5615 #define NACLMASK 0xe0 // 32-byte alignment mask.
5617 // The first entry in the PLT.
5621 Output_data_plt_x86_64_nacl
<size
>::first_plt_entry
[plt_entry_size
] =
5623 0xff, 0x35, // pushq contents of memory address
5624 0, 0, 0, 0, // replaced with address of .got + 8
5625 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
5626 0, 0, 0, 0, // replaced with address of .got + 16
5627 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
5628 0x4d, 0x01, 0xfb, // add %r15, %r11
5629 0x41, 0xff, 0xe3, // jmpq *%r11
5631 // 9-byte nop sequence to pad out to the next 32-byte boundary.
5632 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
5634 // 32 bytes of nop to pad out to the standard size
5635 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5636 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5637 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5638 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5639 0x66, // excess data32 prefix
5645 Output_data_plt_x86_64_nacl
<size
>::do_fill_first_plt_entry(
5647 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5648 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
5650 memcpy(pov
, first_plt_entry
, plt_entry_size
);
5651 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
5653 - (plt_address
+ 2 + 4)));
5654 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
5656 - (plt_address
+ 9 + 4)));
5659 // Subsequent entries in the PLT.
5663 Output_data_plt_x86_64_nacl
<size
>::plt_entry
[plt_entry_size
] =
5665 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
5666 0, 0, 0, 0, // replaced with address of symbol in .got
5667 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
5668 0x4d, 0x01, 0xfb, // add %r15, %r11
5669 0x41, 0xff, 0xe3, // jmpq *%r11
5671 // 15-byte nop sequence to pad out to the next 32-byte boundary.
5672 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5673 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5675 // Lazy GOT entries point here (32-byte aligned).
5676 0x68, // pushq immediate
5677 0, 0, 0, 0, // replaced with index into relocation table
5678 0xe9, // jmp relative
5679 0, 0, 0, 0, // replaced with offset to start of .plt0
5681 // 22 bytes of nop to pad out to the standard size.
5682 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5683 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5684 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
5689 Output_data_plt_x86_64_nacl
<size
>::do_fill_plt_entry(
5691 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5692 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
5693 unsigned int got_offset
,
5694 unsigned int plt_offset
,
5695 unsigned int plt_index
)
5697 memcpy(pov
, plt_entry
, plt_entry_size
);
5698 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3,
5699 (got_address
+ got_offset
5700 - (plt_address
+ plt_offset
5703 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 33, plt_index
);
5704 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 38,
5705 - (plt_offset
+ 38 + 4));
5710 // The reserved TLSDESC entry in the PLT.
5714 Output_data_plt_x86_64_nacl
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
5716 0xff, 0x35, // pushq x(%rip)
5717 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
5718 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
5719 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
5720 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
5721 0x4d, 0x01, 0xfb, // add %r15, %r11
5722 0x41, 0xff, 0xe3, // jmpq *%r11
5724 // 41 bytes of nop to pad out to the standard size.
5725 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5726 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5727 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5728 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5729 0x66, 0x66, // excess data32 prefixes
5730 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5735 Output_data_plt_x86_64_nacl
<size
>::do_fill_tlsdesc_entry(
5737 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5738 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
5739 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
5740 unsigned int tlsdesc_got_offset
,
5741 unsigned int plt_offset
)
5743 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
5744 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
5746 - (plt_address
+ plt_offset
5748 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
5750 + tlsdesc_got_offset
5751 - (plt_address
+ plt_offset
5755 // The .eh_frame unwind information for the PLT.
5759 Output_data_plt_x86_64_nacl
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
5761 0, 0, 0, 0, // Replaced with offset to .plt.
5762 0, 0, 0, 0, // Replaced with size of .plt.
5763 0, // Augmentation size.
5764 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
5765 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
5766 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
5767 elfcpp::DW_CFA_advance_loc
+ 58, // Advance 58 to __PLT__ + 64.
5768 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
5769 13, // Block length.
5770 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
5771 elfcpp::DW_OP_breg16
, 0, // Push %rip.
5772 elfcpp::DW_OP_const1u
, 63, // Push 0x3f.
5773 elfcpp::DW_OP_and
, // & (%rip & 0x3f).
5774 elfcpp::DW_OP_const1u
, 37, // Push 0x25.
5775 elfcpp::DW_OP_ge
, // >= ((%rip & 0x3f) >= 0x25)
5776 elfcpp::DW_OP_lit3
, // Push 3.
5777 elfcpp::DW_OP_shl
, // << (((%rip & 0x3f) >= 0x25) << 3)
5778 elfcpp::DW_OP_plus
, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5779 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
5783 // Return a string used to fill a code section with nops.
5784 // For NaCl, long NOPs are only valid if they do not cross
5785 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5788 Target_x86_64_nacl
<size
>::do_code_fill(section_size_type length
) const
5790 return std::string(length
, static_cast<char>(0x90));
5793 // The selector for x86_64-nacl object files.
5796 class Target_selector_x86_64_nacl
5797 : public Target_selector_nacl
<Target_selector_x86_64
<size
>,
5798 Target_x86_64_nacl
<size
> >
5801 Target_selector_x86_64_nacl()
5802 : Target_selector_nacl
<Target_selector_x86_64
<size
>,
5803 Target_x86_64_nacl
<size
> >("x86-64",
5805 ? "elf64-x86-64-nacl"
5806 : "elf32-x86-64-nacl",
5809 : "elf32_x86_64_nacl")
5813 Target_selector_x86_64_nacl
<64> target_selector_x86_64
;
5814 Target_selector_x86_64_nacl
<32> target_selector_x32
;
5816 } // End anonymous namespace.