1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2016 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 // Return the number of PLT entries.
185 { return this->count_
+ this->irelative_count_
; }
187 // Return the offset of the first non-reserved PLT entry.
189 first_plt_entry_offset()
190 { return this->get_plt_entry_size(); }
192 // Return the size of a PLT entry.
194 get_plt_entry_size() const
195 { return this->do_get_plt_entry_size(); }
197 // Reserve a slot in the PLT for an existing symbol in an incremental update.
199 reserve_slot(unsigned int plt_index
)
201 this->free_list_
.remove((plt_index
+ 1) * this->get_plt_entry_size(),
202 (plt_index
+ 2) * this->get_plt_entry_size());
205 // Return the PLT address to use for a global symbol.
207 address_for_global(const Symbol
*);
209 // Return the PLT address to use for a local symbol.
211 address_for_local(const Relobj
*, unsigned int symndx
);
213 // Add .eh_frame information for the PLT.
215 add_eh_frame(Layout
* layout
)
216 { this->do_add_eh_frame(layout
); }
219 // Fill in the first PLT entry.
221 fill_first_plt_entry(unsigned char* pov
,
222 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
223 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
224 { this->do_fill_first_plt_entry(pov
, got_address
, plt_address
); }
226 // Fill in a normal PLT entry. Returns the offset into the entry that
227 // should be the initial GOT slot value.
229 fill_plt_entry(unsigned char* pov
,
230 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
231 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
232 unsigned int got_offset
,
233 unsigned int plt_offset
,
234 unsigned int plt_index
)
236 return this->do_fill_plt_entry(pov
, got_address
, plt_address
,
237 got_offset
, plt_offset
, plt_index
);
240 // Fill in the reserved TLSDESC PLT entry.
242 fill_tlsdesc_entry(unsigned char* pov
,
243 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
244 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
245 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
246 unsigned int tlsdesc_got_offset
,
247 unsigned int plt_offset
)
249 this->do_fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
250 tlsdesc_got_offset
, plt_offset
);
254 do_get_plt_entry_size() const = 0;
257 do_fill_first_plt_entry(unsigned char* pov
,
258 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
259 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
)
263 do_fill_plt_entry(unsigned char* pov
,
264 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
265 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
266 unsigned int got_offset
,
267 unsigned int plt_offset
,
268 unsigned int plt_index
) = 0;
271 do_fill_tlsdesc_entry(unsigned char* pov
,
272 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
273 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
274 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
275 unsigned int tlsdesc_got_offset
,
276 unsigned int plt_offset
) = 0;
279 do_add_eh_frame(Layout
* layout
) = 0;
282 do_adjust_output_section(Output_section
* os
);
284 // Write to a map file.
286 do_print_to_mapfile(Mapfile
* mapfile
) const
287 { mapfile
->print_output_data(this, _("** PLT")); }
289 // The CIE of the .eh_frame unwind information for the PLT.
290 static const int plt_eh_frame_cie_size
= 16;
291 static const unsigned char plt_eh_frame_cie
[plt_eh_frame_cie_size
];
294 // Set the final size.
296 set_final_data_size();
298 // Write out the PLT data.
300 do_write(Output_file
*);
302 // The reloc section.
304 // The TLSDESC relocs, if necessary. These must follow the regular
306 Reloc_section
* tlsdesc_rel_
;
307 // The IRELATIVE relocs, if necessary. These must follow the
308 // regular PLT relocations and the TLSDESC relocations.
309 Reloc_section
* irelative_rel_
;
311 Output_data_got
<64, false>* got_
;
312 // The .got.plt section.
313 Output_data_got_plt_x86_64
* got_plt_
;
314 // The part of the .got.plt section used for IRELATIVE relocs.
315 Output_data_space
* got_irelative_
;
316 // The number of PLT entries.
318 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
319 // follow the regular PLT entries.
320 unsigned int irelative_count_
;
321 // Offset of the reserved TLSDESC_GOT entry when needed.
322 unsigned int tlsdesc_got_offset_
;
323 // List of available regions within the section, for incremental
325 Free_list free_list_
;
329 class Output_data_plt_x86_64_standard
: public Output_data_plt_x86_64
<size
>
332 Output_data_plt_x86_64_standard(Layout
* layout
,
333 Output_data_got
<64, false>* got
,
334 Output_data_got_plt_x86_64
* got_plt
,
335 Output_data_space
* got_irelative
)
336 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
337 got
, got_plt
, got_irelative
)
340 Output_data_plt_x86_64_standard(Layout
* layout
,
341 Output_data_got
<64, false>* got
,
342 Output_data_got_plt_x86_64
* got_plt
,
343 Output_data_space
* got_irelative
,
344 unsigned int plt_count
)
345 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
346 got
, got_plt
, got_irelative
,
352 do_get_plt_entry_size() const
353 { return plt_entry_size
; }
356 do_add_eh_frame(Layout
* layout
)
358 layout
->add_eh_frame_for_plt(this,
359 this->plt_eh_frame_cie
,
360 this->plt_eh_frame_cie_size
,
362 plt_eh_frame_fde_size
);
366 do_fill_first_plt_entry(unsigned char* pov
,
367 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
368 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
371 do_fill_plt_entry(unsigned char* pov
,
372 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
373 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
374 unsigned int got_offset
,
375 unsigned int plt_offset
,
376 unsigned int plt_index
);
379 do_fill_tlsdesc_entry(unsigned char* pov
,
380 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
381 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
382 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
383 unsigned int tlsdesc_got_offset
,
384 unsigned int plt_offset
);
387 // The size of an entry in the PLT.
388 static const int plt_entry_size
= 16;
390 // The first entry in the PLT.
391 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
392 // procedure linkage table for both programs and shared objects."
393 static const unsigned char first_plt_entry
[plt_entry_size
];
395 // Other entries in the PLT for an executable.
396 static const unsigned char plt_entry
[plt_entry_size
];
398 // The reserved TLSDESC entry in the PLT for an executable.
399 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
401 // The .eh_frame unwind information for the PLT.
402 static const int plt_eh_frame_fde_size
= 32;
403 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
410 Lazy_view(Sized_relobj_file
<size
, false>* object
, unsigned int data_shndx
)
411 : object_(object
), data_shndx_(data_shndx
), view_(NULL
), view_size_(0)
415 operator[](size_t offset
)
417 if (this->view_
== NULL
)
418 this->view_
= this->object_
->section_contents(this->data_shndx_
,
421 if (offset
>= this->view_size_
)
423 return this->view_
[offset
];
427 Sized_relobj_file
<size
, false>* object_
;
428 unsigned int data_shndx_
;
429 const unsigned char* view_
;
430 section_size_type view_size_
;
433 // The x86_64 target class.
435 // http://www.x86-64.org/documentation/abi.pdf
436 // TLS info comes from
437 // http://people.redhat.com/drepper/tls.pdf
438 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
441 class Target_x86_64
: public Sized_target
<size
, false>
444 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
445 // uses only Elf64_Rela relocation entries with explicit addends."
446 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
448 Target_x86_64(const Target::Target_info
* info
= &x86_64_info
)
449 : Sized_target
<size
, false>(info
),
450 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_irelative_(NULL
),
451 got_tlsdesc_(NULL
), global_offset_table_(NULL
), rela_dyn_(NULL
),
452 rela_irelative_(NULL
), copy_relocs_(elfcpp::R_X86_64_COPY
),
453 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
454 tls_base_symbol_defined_(false)
457 // Hook for a new output section.
459 do_new_output_section(Output_section
*) const;
461 // Scan the relocations to look for symbol adjustments.
463 gc_process_relocs(Symbol_table
* symtab
,
465 Sized_relobj_file
<size
, false>* object
,
466 unsigned int data_shndx
,
467 unsigned int sh_type
,
468 const unsigned char* prelocs
,
470 Output_section
* output_section
,
471 bool needs_special_offset_handling
,
472 size_t local_symbol_count
,
473 const unsigned char* plocal_symbols
);
475 // Scan the relocations to look for symbol adjustments.
477 scan_relocs(Symbol_table
* symtab
,
479 Sized_relobj_file
<size
, false>* object
,
480 unsigned int data_shndx
,
481 unsigned int sh_type
,
482 const unsigned char* prelocs
,
484 Output_section
* output_section
,
485 bool needs_special_offset_handling
,
486 size_t local_symbol_count
,
487 const unsigned char* plocal_symbols
);
489 // Finalize the sections.
491 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
493 // Return the value to use for a dynamic which requires special
496 do_dynsym_value(const Symbol
*) const;
498 // Relocate a section.
500 relocate_section(const Relocate_info
<size
, false>*,
501 unsigned int sh_type
,
502 const unsigned char* prelocs
,
504 Output_section
* output_section
,
505 bool needs_special_offset_handling
,
507 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
508 section_size_type view_size
,
509 const Reloc_symbol_changes
*);
511 // Scan the relocs during a relocatable link.
513 scan_relocatable_relocs(Symbol_table
* symtab
,
515 Sized_relobj_file
<size
, false>* object
,
516 unsigned int data_shndx
,
517 unsigned int sh_type
,
518 const unsigned char* prelocs
,
520 Output_section
* output_section
,
521 bool needs_special_offset_handling
,
522 size_t local_symbol_count
,
523 const unsigned char* plocal_symbols
,
524 Relocatable_relocs
*);
526 // Scan the relocs for --emit-relocs.
528 emit_relocs_scan(Symbol_table
* symtab
,
530 Sized_relobj_file
<size
, false>* object
,
531 unsigned int data_shndx
,
532 unsigned int sh_type
,
533 const unsigned char* prelocs
,
535 Output_section
* output_section
,
536 bool needs_special_offset_handling
,
537 size_t local_symbol_count
,
538 const unsigned char* plocal_syms
,
539 Relocatable_relocs
* rr
);
541 // Emit relocations for a section.
544 const Relocate_info
<size
, false>*,
545 unsigned int sh_type
,
546 const unsigned char* prelocs
,
548 Output_section
* output_section
,
549 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
551 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
552 section_size_type view_size
,
553 unsigned char* reloc_view
,
554 section_size_type reloc_view_size
);
556 // Return a string used to fill a code section with nops.
558 do_code_fill(section_size_type length
) const;
560 // Return whether SYM is defined by the ABI.
562 do_is_defined_by_abi(const Symbol
* sym
) const
563 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
565 // Return the symbol index to use for a target specific relocation.
566 // The only target specific relocation is R_X86_64_TLSDESC for a
567 // local symbol, which is an absolute reloc.
569 do_reloc_symbol_index(void*, unsigned int r_type
) const
571 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
575 // Return the addend to use for a target specific relocation.
577 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
579 // Return the PLT section.
581 do_plt_address_for_global(const Symbol
* gsym
) const
582 { return this->plt_section()->address_for_global(gsym
); }
585 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
586 { return this->plt_section()->address_for_local(relobj
, symndx
); }
588 // This function should be defined in targets that can use relocation
589 // types to determine (implemented in local_reloc_may_be_function_pointer
590 // and global_reloc_may_be_function_pointer)
591 // if a function's pointer is taken. ICF uses this in safe mode to only
592 // fold those functions whose pointer is defintely not taken. For x86_64
593 // pie binaries, safe ICF cannot be done by looking at relocation types.
595 do_can_check_for_function_pointers() const
596 { return !parameters
->options().pie(); }
598 // Return the base for a DW_EH_PE_datarel encoding.
600 do_ehframe_datarel_base() const;
602 // Adjust -fsplit-stack code which calls non-split-stack code.
604 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
605 section_offset_type fnoffset
, section_size_type fnsize
,
606 const unsigned char* prelocs
, size_t reloc_count
,
607 unsigned char* view
, section_size_type view_size
,
608 std::string
* from
, std::string
* to
) const;
610 // Return the size of the GOT section.
614 gold_assert(this->got_
!= NULL
);
615 return this->got_
->data_size();
618 // Return the number of entries in the GOT.
620 got_entry_count() const
622 if (this->got_
== NULL
)
624 return this->got_size() / 8;
627 // Return the number of entries in the PLT.
629 plt_entry_count() const;
631 // Return the offset of the first non-reserved PLT entry.
633 first_plt_entry_offset() const;
635 // Return the size of each PLT entry.
637 plt_entry_size() const;
639 // Return the size of each GOT entry.
641 got_entry_size() const
644 // Create the GOT section for an incremental update.
645 Output_data_got_base
*
646 init_got_plt_for_update(Symbol_table
* symtab
,
648 unsigned int got_count
,
649 unsigned int plt_count
);
651 // Reserve a GOT entry for a local symbol, and regenerate any
652 // necessary dynamic relocations.
654 reserve_local_got_entry(unsigned int got_index
,
655 Sized_relobj
<size
, false>* obj
,
657 unsigned int got_type
);
659 // Reserve a GOT entry for a global symbol, and regenerate any
660 // necessary dynamic relocations.
662 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
663 unsigned int got_type
);
665 // Register an existing PLT entry for a global symbol.
667 register_global_plt_entry(Symbol_table
*, Layout
*, unsigned int plt_index
,
670 // Force a COPY relocation for a given symbol.
672 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
674 // Apply an incremental relocation.
676 apply_relocation(const Relocate_info
<size
, false>* relinfo
,
677 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
679 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
682 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
683 section_size_type view_size
);
685 // Add a new reloc argument, returning the index in the vector.
687 add_tlsdesc_info(Sized_relobj_file
<size
, false>* object
, unsigned int r_sym
)
689 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
690 return this->tlsdesc_reloc_info_
.size() - 1;
693 Output_data_plt_x86_64
<size
>*
694 make_data_plt(Layout
* layout
,
695 Output_data_got
<64, false>* got
,
696 Output_data_got_plt_x86_64
* got_plt
,
697 Output_data_space
* got_irelative
)
699 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
702 Output_data_plt_x86_64
<size
>*
703 make_data_plt(Layout
* layout
,
704 Output_data_got
<64, false>* got
,
705 Output_data_got_plt_x86_64
* got_plt
,
706 Output_data_space
* got_irelative
,
707 unsigned int plt_count
)
709 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
,
713 virtual Output_data_plt_x86_64
<size
>*
714 do_make_data_plt(Layout
* layout
,
715 Output_data_got
<64, false>* got
,
716 Output_data_got_plt_x86_64
* got_plt
,
717 Output_data_space
* got_irelative
)
719 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
723 virtual Output_data_plt_x86_64
<size
>*
724 do_make_data_plt(Layout
* layout
,
725 Output_data_got
<64, false>* got
,
726 Output_data_got_plt_x86_64
* got_plt
,
727 Output_data_space
* got_irelative
,
728 unsigned int plt_count
)
730 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
736 // The class which scans relocations.
741 : issued_non_pic_error_(false)
745 get_reference_flags(unsigned int r_type
);
748 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
749 Sized_relobj_file
<size
, false>* object
,
750 unsigned int data_shndx
,
751 Output_section
* output_section
,
752 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
753 const elfcpp::Sym
<size
, false>& lsym
,
757 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
758 Sized_relobj_file
<size
, false>* object
,
759 unsigned int data_shndx
,
760 Output_section
* output_section
,
761 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
765 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
766 Target_x86_64
* target
,
767 Sized_relobj_file
<size
, false>* object
,
768 unsigned int data_shndx
,
769 Output_section
* output_section
,
770 const elfcpp::Rela
<size
, false>& reloc
,
772 const elfcpp::Sym
<size
, false>& lsym
);
775 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
776 Target_x86_64
* target
,
777 Sized_relobj_file
<size
, false>* object
,
778 unsigned int data_shndx
,
779 Output_section
* output_section
,
780 const elfcpp::Rela
<size
, false>& reloc
,
786 unsupported_reloc_local(Sized_relobj_file
<size
, false>*,
787 unsigned int r_type
);
790 unsupported_reloc_global(Sized_relobj_file
<size
, false>*,
791 unsigned int r_type
, Symbol
*);
794 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
797 possible_function_pointer_reloc(unsigned int r_type
);
800 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, false>*,
801 unsigned int r_type
);
803 // Whether we have issued an error about a non-PIC compilation.
804 bool issued_non_pic_error_
;
807 // The class which implements relocation.
812 : skip_call_tls_get_addr_(false)
817 if (this->skip_call_tls_get_addr_
)
819 // FIXME: This needs to specify the location somehow.
820 gold_error(_("missing expected TLS relocation"));
824 // Do a relocation. Return false if the caller should not issue
825 // any warnings about this relocation.
827 relocate(const Relocate_info
<size
, false>*, unsigned int,
828 Target_x86_64
*, Output_section
*, size_t, const unsigned char*,
829 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
830 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
834 // Do a TLS relocation.
836 relocate_tls(const Relocate_info
<size
, false>*, Target_x86_64
*,
837 size_t relnum
, const elfcpp::Rela
<size
, false>&,
838 unsigned int r_type
, const Sized_symbol
<size
>*,
839 const Symbol_value
<size
>*,
840 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
843 // Do a TLS General-Dynamic to Initial-Exec transition.
845 tls_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
846 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
847 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
849 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
850 section_size_type view_size
);
852 // Do a TLS General-Dynamic to Local-Exec transition.
854 tls_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
855 Output_segment
* tls_segment
,
856 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
857 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
859 section_size_type view_size
);
861 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
863 tls_desc_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
864 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
865 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
867 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
868 section_size_type view_size
);
870 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
872 tls_desc_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
873 Output_segment
* tls_segment
,
874 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
875 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
877 section_size_type view_size
);
879 // Do a TLS Local-Dynamic to Local-Exec transition.
881 tls_ld_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
882 Output_segment
* tls_segment
,
883 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
884 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
886 section_size_type view_size
);
888 // Do a TLS Initial-Exec to Local-Exec transition.
890 tls_ie_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
891 Output_segment
* tls_segment
,
892 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
893 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
895 section_size_type view_size
);
897 // This is set if we should skip the next reloc, which should be a
898 // PLT32 reloc against ___tls_get_addr.
899 bool skip_call_tls_get_addr_
;
902 // Check if relocation against this symbol is a candidate for
904 // mov foo@GOTPCREL(%rip), %reg
905 // to lea foo(%rip), %reg.
906 template<class View_type
>
908 can_convert_mov_to_lea(const Symbol
* gsym
, unsigned int r_type
,
909 size_t r_offset
, View_type
* view
)
911 gold_assert(gsym
!= NULL
);
912 // We cannot do the conversion unless it's one of these relocations.
913 if (r_type
!= elfcpp::R_X86_64_GOTPCREL
914 && r_type
!= elfcpp::R_X86_64_GOTPCRELX
915 && r_type
!= elfcpp::R_X86_64_REX_GOTPCRELX
)
917 // We cannot convert references to IFUNC symbols, or to symbols that
918 // are not local to the current module.
919 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
920 || gsym
->is_undefined ()
921 || gsym
->is_from_dynobj()
922 || gsym
->is_preemptible())
924 // If we are building a shared object and the symbol is protected, we may
925 // need to go through the GOT.
926 if (parameters
->options().shared()
927 && gsym
->visibility() == elfcpp::STV_PROTECTED
)
929 // We cannot convert references to the _DYNAMIC symbol.
930 if (strcmp(gsym
->name(), "_DYNAMIC") == 0)
932 // Check for a MOV opcode.
933 return (*view
)[r_offset
- 2] == 0x8b;
937 // callq *foo@GOTPCRELX(%rip) to
939 // and jmpq *foo@GOTPCRELX(%rip) to
942 template<class View_type
>
944 can_convert_callq_to_direct(const Symbol
* gsym
, unsigned int r_type
,
945 size_t r_offset
, View_type
* view
)
947 gold_assert(gsym
!= NULL
);
948 // We cannot do the conversion unless it's a GOTPCRELX relocation.
949 if (r_type
!= elfcpp::R_X86_64_GOTPCRELX
)
951 // We cannot convert references to IFUNC symbols, or to symbols that
952 // are not local to the current module.
953 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
954 || gsym
->is_undefined ()
955 || gsym
->is_from_dynobj()
956 || gsym
->is_preemptible())
958 // Check for a CALLQ or JMPQ opcode.
959 return ((*view
)[r_offset
- 2] == 0xff
960 && ((*view
)[r_offset
- 1] == 0x15
961 || (*view
)[r_offset
- 1] == 0x25));
964 // Adjust TLS relocation type based on the options and whether this
965 // is a local symbol.
966 static tls::Tls_optimization
967 optimize_tls_reloc(bool is_final
, int r_type
);
969 // Get the GOT section, creating it if necessary.
970 Output_data_got
<64, false>*
971 got_section(Symbol_table
*, Layout
*);
973 // Get the GOT PLT section.
974 Output_data_got_plt_x86_64
*
975 got_plt_section() const
977 gold_assert(this->got_plt_
!= NULL
);
978 return this->got_plt_
;
981 // Get the GOT section for TLSDESC entries.
982 Output_data_got
<64, false>*
983 got_tlsdesc_section() const
985 gold_assert(this->got_tlsdesc_
!= NULL
);
986 return this->got_tlsdesc_
;
989 // Create the PLT section.
991 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
993 // Create a PLT entry for a global symbol.
995 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
997 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
999 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1000 Sized_relobj_file
<size
, false>* relobj
,
1001 unsigned int local_sym_index
);
1003 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1005 define_tls_base_symbol(Symbol_table
*, Layout
*);
1007 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1009 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
1011 // Create a GOT entry for the TLS module index.
1013 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1014 Sized_relobj_file
<size
, false>* object
);
1016 // Get the PLT section.
1017 Output_data_plt_x86_64
<size
>*
1020 gold_assert(this->plt_
!= NULL
);
1024 // Get the dynamic reloc section, creating it if necessary.
1026 rela_dyn_section(Layout
*);
1028 // Get the section to use for TLSDESC relocations.
1030 rela_tlsdesc_section(Layout
*) const;
1032 // Get the section to use for IRELATIVE relocations.
1034 rela_irelative_section(Layout
*);
1036 // Add a potential copy relocation.
1038 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1039 Sized_relobj_file
<size
, false>* object
,
1040 unsigned int shndx
, Output_section
* output_section
,
1041 Symbol
* sym
, const elfcpp::Rela
<size
, false>& reloc
)
1043 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1044 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1045 symtab
->get_sized_symbol
<size
>(sym
),
1046 object
, shndx
, output_section
,
1047 r_type
, reloc
.get_r_offset(),
1048 reloc
.get_r_addend(),
1049 this->rela_dyn_section(layout
));
1052 // Information about this specific target which we pass to the
1053 // general Target structure.
1054 static const Target::Target_info x86_64_info
;
1056 // The types of GOT entries needed for this platform.
1057 // These values are exposed to the ABI in an incremental link.
1058 // Do not renumber existing values without changing the version
1059 // number of the .gnu_incremental_inputs section.
1062 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
1063 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
1064 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
1065 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
1068 // This type is used as the argument to the target specific
1069 // relocation routines. The only target specific reloc is
1070 // R_X86_64_TLSDESC against a local symbol.
1073 Tlsdesc_info(Sized_relobj_file
<size
, false>* a_object
, unsigned int a_r_sym
)
1074 : object(a_object
), r_sym(a_r_sym
)
1077 // The object in which the local symbol is defined.
1078 Sized_relobj_file
<size
, false>* object
;
1079 // The local symbol index in the object.
1084 Output_data_got
<64, false>* got_
;
1086 Output_data_plt_x86_64
<size
>* plt_
;
1087 // The GOT PLT section.
1088 Output_data_got_plt_x86_64
* got_plt_
;
1089 // The GOT section for IRELATIVE relocations.
1090 Output_data_space
* got_irelative_
;
1091 // The GOT section for TLSDESC relocations.
1092 Output_data_got
<64, false>* got_tlsdesc_
;
1093 // The _GLOBAL_OFFSET_TABLE_ symbol.
1094 Symbol
* global_offset_table_
;
1095 // The dynamic reloc section.
1096 Reloc_section
* rela_dyn_
;
1097 // The section to use for IRELATIVE relocs.
1098 Reloc_section
* rela_irelative_
;
1099 // Relocs saved to avoid a COPY reloc.
1100 Copy_relocs
<elfcpp::SHT_RELA
, size
, false> copy_relocs_
;
1101 // Offset of the GOT entry for the TLS module index.
1102 unsigned int got_mod_index_offset_
;
1103 // We handle R_X86_64_TLSDESC against a local symbol as a target
1104 // specific relocation. Here we store the object and local symbol
1105 // index for the relocation.
1106 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
1107 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1108 bool tls_base_symbol_defined_
;
1112 const Target::Target_info Target_x86_64
<64>::x86_64_info
=
1115 false, // is_big_endian
1116 elfcpp::EM_X86_64
, // machine_code
1117 false, // has_make_symbol
1118 false, // has_resolve
1119 true, // has_code_fill
1120 true, // is_default_stack_executable
1121 true, // can_icf_inline_merge_sections
1123 "/lib/ld64.so.1", // program interpreter
1124 0x400000, // default_text_segment_address
1125 0x1000, // abi_pagesize (overridable by -z max-page-size)
1126 0x1000, // common_pagesize (overridable by -z common-page-size)
1127 false, // isolate_execinstr
1129 elfcpp::SHN_UNDEF
, // small_common_shndx
1130 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1131 0, // small_common_section_flags
1132 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1133 NULL
, // attributes_section
1134 NULL
, // attributes_vendor
1135 "_start", // entry_symbol_name
1136 32, // hash_entry_size
1140 const Target::Target_info Target_x86_64
<32>::x86_64_info
=
1143 false, // is_big_endian
1144 elfcpp::EM_X86_64
, // machine_code
1145 false, // has_make_symbol
1146 false, // has_resolve
1147 true, // has_code_fill
1148 true, // is_default_stack_executable
1149 true, // can_icf_inline_merge_sections
1151 "/libx32/ldx32.so.1", // program interpreter
1152 0x400000, // default_text_segment_address
1153 0x1000, // abi_pagesize (overridable by -z max-page-size)
1154 0x1000, // common_pagesize (overridable by -z common-page-size)
1155 false, // isolate_execinstr
1157 elfcpp::SHN_UNDEF
, // small_common_shndx
1158 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1159 0, // small_common_section_flags
1160 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1161 NULL
, // attributes_section
1162 NULL
, // attributes_vendor
1163 "_start", // entry_symbol_name
1164 32, // hash_entry_size
1167 // This is called when a new output section is created. This is where
1168 // we handle the SHF_X86_64_LARGE.
1172 Target_x86_64
<size
>::do_new_output_section(Output_section
* os
) const
1174 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
1175 os
->set_is_large_section();
1178 // Get the GOT section, creating it if necessary.
1181 Output_data_got
<64, false>*
1182 Target_x86_64
<size
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
1184 if (this->got_
== NULL
)
1186 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1188 // When using -z now, we can treat .got.plt as a relro section.
1189 // Without -z now, it is modified after program startup by lazy
1191 bool is_got_plt_relro
= parameters
->options().now();
1192 Output_section_order got_order
= (is_got_plt_relro
1194 : ORDER_RELRO_LAST
);
1195 Output_section_order got_plt_order
= (is_got_plt_relro
1197 : ORDER_NON_RELRO_FIRST
);
1199 this->got_
= new Output_data_got
<64, false>();
1201 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1203 | elfcpp::SHF_WRITE
),
1204 this->got_
, got_order
, true);
1206 this->got_plt_
= new Output_data_got_plt_x86_64(layout
);
1207 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1209 | elfcpp::SHF_WRITE
),
1210 this->got_plt_
, got_plt_order
,
1213 // The first three entries are reserved.
1214 this->got_plt_
->set_current_data_size(3 * 8);
1216 if (!is_got_plt_relro
)
1218 // Those bytes can go into the relro segment.
1219 layout
->increase_relro(3 * 8);
1222 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1223 this->global_offset_table_
=
1224 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1225 Symbol_table::PREDEFINED
,
1227 0, 0, elfcpp::STT_OBJECT
,
1229 elfcpp::STV_HIDDEN
, 0,
1232 // If there are any IRELATIVE relocations, they get GOT entries
1233 // in .got.plt after the jump slot entries.
1234 this->got_irelative_
= new Output_data_space(8, "** GOT IRELATIVE PLT");
1235 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1237 | elfcpp::SHF_WRITE
),
1238 this->got_irelative_
,
1239 got_plt_order
, is_got_plt_relro
);
1241 // If there are any TLSDESC relocations, they get GOT entries in
1242 // .got.plt after the jump slot and IRELATIVE entries.
1243 this->got_tlsdesc_
= new Output_data_got
<64, false>();
1244 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1246 | elfcpp::SHF_WRITE
),
1248 got_plt_order
, is_got_plt_relro
);
1254 // Get the dynamic reloc section, creating it if necessary.
1257 typename Target_x86_64
<size
>::Reloc_section
*
1258 Target_x86_64
<size
>::rela_dyn_section(Layout
* layout
)
1260 if (this->rela_dyn_
== NULL
)
1262 gold_assert(layout
!= NULL
);
1263 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1264 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1265 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1266 ORDER_DYNAMIC_RELOCS
, false);
1268 return this->rela_dyn_
;
1271 // Get the section to use for IRELATIVE relocs, creating it if
1272 // necessary. These go in .rela.dyn, but only after all other dynamic
1273 // relocations. They need to follow the other dynamic relocations so
1274 // that they can refer to global variables initialized by those
1278 typename Target_x86_64
<size
>::Reloc_section
*
1279 Target_x86_64
<size
>::rela_irelative_section(Layout
* layout
)
1281 if (this->rela_irelative_
== NULL
)
1283 // Make sure we have already created the dynamic reloc section.
1284 this->rela_dyn_section(layout
);
1285 this->rela_irelative_
= new Reloc_section(false);
1286 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1287 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
1288 ORDER_DYNAMIC_RELOCS
, false);
1289 gold_assert(this->rela_dyn_
->output_section()
1290 == this->rela_irelative_
->output_section());
1292 return this->rela_irelative_
;
1295 // Write the first three reserved words of the .got.plt section.
1296 // The remainder of the section is written while writing the PLT
1297 // in Output_data_plt_i386::do_write.
1300 Output_data_got_plt_x86_64::do_write(Output_file
* of
)
1302 // The first entry in the GOT is the address of the .dynamic section
1303 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1304 // We saved space for them when we created the section in
1305 // Target_x86_64::got_section.
1306 const off_t got_file_offset
= this->offset();
1307 gold_assert(this->data_size() >= 24);
1308 unsigned char* const got_view
= of
->get_output_view(got_file_offset
, 24);
1309 Output_section
* dynamic
= this->layout_
->dynamic_section();
1310 uint64_t dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
1311 elfcpp::Swap
<64, false>::writeval(got_view
, dynamic_addr
);
1312 memset(got_view
+ 8, 0, 16);
1313 of
->write_output_view(got_file_offset
, 24, got_view
);
1316 // Initialize the PLT section.
1320 Output_data_plt_x86_64
<size
>::init(Layout
* layout
)
1322 this->rel_
= new Reloc_section(false);
1323 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1324 elfcpp::SHF_ALLOC
, this->rel_
,
1325 ORDER_DYNAMIC_PLT_RELOCS
, false);
1330 Output_data_plt_x86_64
<size
>::do_adjust_output_section(Output_section
* os
)
1332 os
->set_entsize(this->get_plt_entry_size());
1335 // Add an entry to the PLT.
1339 Output_data_plt_x86_64
<size
>::add_entry(Symbol_table
* symtab
, Layout
* layout
,
1342 gold_assert(!gsym
->has_plt_offset());
1344 unsigned int plt_index
;
1346 section_offset_type got_offset
;
1348 unsigned int* pcount
;
1349 unsigned int offset
;
1350 unsigned int reserved
;
1351 Output_section_data_build
* got
;
1352 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1353 && gsym
->can_use_relative_reloc(false))
1355 pcount
= &this->irelative_count_
;
1358 got
= this->got_irelative_
;
1362 pcount
= &this->count_
;
1365 got
= this->got_plt_
;
1368 if (!this->is_data_size_valid())
1370 // Note that when setting the PLT offset for a non-IRELATIVE
1371 // entry we skip the initial reserved PLT entry.
1372 plt_index
= *pcount
+ offset
;
1373 plt_offset
= plt_index
* this->get_plt_entry_size();
1377 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1378 gold_assert(got_offset
== got
->current_data_size());
1380 // Every PLT entry needs a GOT entry which points back to the PLT
1381 // entry (this will be changed by the dynamic linker, normally
1382 // lazily when the function is called).
1383 got
->set_current_data_size(got_offset
+ 8);
1387 // FIXME: This is probably not correct for IRELATIVE relocs.
1389 // For incremental updates, find an available slot.
1390 plt_offset
= this->free_list_
.allocate(this->get_plt_entry_size(),
1391 this->get_plt_entry_size(), 0);
1392 if (plt_offset
== -1)
1393 gold_fallback(_("out of patch space (PLT);"
1394 " relink with --incremental-full"));
1396 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1397 // can be calculated from the PLT index, adjusting for the three
1398 // reserved entries at the beginning of the GOT.
1399 plt_index
= plt_offset
/ this->get_plt_entry_size() - 1;
1400 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1403 gsym
->set_plt_offset(plt_offset
);
1405 // Every PLT entry needs a reloc.
1406 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
1408 // Note that we don't need to save the symbol. The contents of the
1409 // PLT are independent of which symbols are used. The symbols only
1410 // appear in the relocations.
1413 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1418 Output_data_plt_x86_64
<size
>::add_local_ifunc_entry(
1419 Symbol_table
* symtab
,
1421 Sized_relobj_file
<size
, false>* relobj
,
1422 unsigned int local_sym_index
)
1424 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
1425 ++this->irelative_count_
;
1427 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
1429 // Every PLT entry needs a GOT entry which points back to the PLT
1431 this->got_irelative_
->set_current_data_size(got_offset
+ 8);
1433 // Every PLT entry needs a reloc.
1434 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1435 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
1436 elfcpp::R_X86_64_IRELATIVE
,
1437 this->got_irelative_
, got_offset
, 0);
1442 // Add the relocation for a PLT entry.
1446 Output_data_plt_x86_64
<size
>::add_relocation(Symbol_table
* symtab
,
1449 unsigned int got_offset
)
1451 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1452 && gsym
->can_use_relative_reloc(false))
1454 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1455 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
1456 this->got_irelative_
, got_offset
, 0);
1460 gsym
->set_needs_dynsym_entry();
1461 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
1466 // Return where the TLSDESC relocations should go, creating it if
1467 // necessary. These follow the JUMP_SLOT relocations.
1470 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1471 Output_data_plt_x86_64
<size
>::rela_tlsdesc(Layout
* layout
)
1473 if (this->tlsdesc_rel_
== NULL
)
1475 this->tlsdesc_rel_
= new Reloc_section(false);
1476 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1477 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
1478 ORDER_DYNAMIC_PLT_RELOCS
, false);
1479 gold_assert(this->tlsdesc_rel_
->output_section()
1480 == this->rel_
->output_section());
1482 return this->tlsdesc_rel_
;
1485 // Return where the IRELATIVE relocations should go in the PLT. These
1486 // follow the JUMP_SLOT and the TLSDESC relocations.
1489 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1490 Output_data_plt_x86_64
<size
>::rela_irelative(Symbol_table
* symtab
,
1493 if (this->irelative_rel_
== NULL
)
1495 // Make sure we have a place for the TLSDESC relocations, in
1496 // case we see any later on.
1497 this->rela_tlsdesc(layout
);
1498 this->irelative_rel_
= new Reloc_section(false);
1499 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1500 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
1501 ORDER_DYNAMIC_PLT_RELOCS
, false);
1502 gold_assert(this->irelative_rel_
->output_section()
1503 == this->rel_
->output_section());
1505 if (parameters
->doing_static_link())
1507 // A statically linked executable will only have a .rela.plt
1508 // section to hold R_X86_64_IRELATIVE relocs for
1509 // STT_GNU_IFUNC symbols. The library will use these
1510 // symbols to locate the IRELATIVE relocs at program startup
1512 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
1513 Symbol_table::PREDEFINED
,
1514 this->irelative_rel_
, 0, 0,
1515 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1516 elfcpp::STV_HIDDEN
, 0, false, true);
1517 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
1518 Symbol_table::PREDEFINED
,
1519 this->irelative_rel_
, 0, 0,
1520 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1521 elfcpp::STV_HIDDEN
, 0, true, true);
1524 return this->irelative_rel_
;
1527 // Return the PLT address to use for a global symbol.
1531 Output_data_plt_x86_64
<size
>::address_for_global(const Symbol
* gsym
)
1533 uint64_t offset
= 0;
1534 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1535 && gsym
->can_use_relative_reloc(false))
1536 offset
= (this->count_
+ 1) * this->get_plt_entry_size();
1537 return this->address() + offset
+ gsym
->plt_offset();
1540 // Return the PLT address to use for a local symbol. These are always
1541 // IRELATIVE relocs.
1545 Output_data_plt_x86_64
<size
>::address_for_local(const Relobj
* object
,
1548 return (this->address()
1549 + (this->count_
+ 1) * this->get_plt_entry_size()
1550 + object
->local_plt_offset(r_sym
));
1553 // Set the final size.
1556 Output_data_plt_x86_64
<size
>::set_final_data_size()
1558 unsigned int count
= this->count_
+ this->irelative_count_
;
1559 if (this->has_tlsdesc_entry())
1561 this->set_data_size((count
+ 1) * this->get_plt_entry_size());
1564 // The first entry in the PLT for an executable.
1568 Output_data_plt_x86_64_standard
<size
>::first_plt_entry
[plt_entry_size
] =
1570 // From AMD64 ABI Draft 0.98, page 76
1571 0xff, 0x35, // pushq contents of memory address
1572 0, 0, 0, 0, // replaced with address of .got + 8
1573 0xff, 0x25, // jmp indirect
1574 0, 0, 0, 0, // replaced with address of .got + 16
1575 0x90, 0x90, 0x90, 0x90 // noop (x4)
1580 Output_data_plt_x86_64_standard
<size
>::do_fill_first_plt_entry(
1582 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1583 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
1585 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1586 // We do a jmp relative to the PC at the end of this instruction.
1587 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1589 - (plt_address
+ 6)));
1590 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1592 - (plt_address
+ 12)));
1595 // Subsequent entries in the PLT for an executable.
1599 Output_data_plt_x86_64_standard
<size
>::plt_entry
[plt_entry_size
] =
1601 // From AMD64 ABI Draft 0.98, page 76
1602 0xff, 0x25, // jmpq indirect
1603 0, 0, 0, 0, // replaced with address of symbol in .got
1604 0x68, // pushq immediate
1605 0, 0, 0, 0, // replaced with offset into relocation table
1606 0xe9, // jmpq relative
1607 0, 0, 0, 0 // replaced with offset to start of .plt
1612 Output_data_plt_x86_64_standard
<size
>::do_fill_plt_entry(
1614 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1615 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1616 unsigned int got_offset
,
1617 unsigned int plt_offset
,
1618 unsigned int plt_index
)
1620 // Check PC-relative offset overflow in PLT entry.
1621 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
1622 - (plt_address
+ plt_offset
+ 6));
1623 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
1624 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1627 memcpy(pov
, plt_entry
, plt_entry_size
);
1628 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1629 plt_got_pcrel_offset
);
1631 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1632 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1633 - (plt_offset
+ plt_entry_size
));
1638 // The reserved TLSDESC entry in the PLT for an executable.
1642 Output_data_plt_x86_64_standard
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
1644 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1645 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1646 0xff, 0x35, // pushq x(%rip)
1647 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1648 0xff, 0x25, // jmpq *y(%rip)
1649 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1656 Output_data_plt_x86_64_standard
<size
>::do_fill_tlsdesc_entry(
1658 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1659 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1660 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
1661 unsigned int tlsdesc_got_offset
,
1662 unsigned int plt_offset
)
1664 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1665 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1667 - (plt_address
+ plt_offset
1669 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1671 + tlsdesc_got_offset
1672 - (plt_address
+ plt_offset
1676 // The .eh_frame unwind information for the PLT.
1680 Output_data_plt_x86_64
<size
>::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
1683 'z', // Augmentation: augmentation size included.
1684 'R', // Augmentation: FDE encoding included.
1685 '\0', // End of augmentation string.
1686 1, // Code alignment factor.
1687 0x78, // Data alignment factor.
1688 16, // Return address column.
1689 1, // Augmentation size.
1690 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
1691 | elfcpp::DW_EH_PE_sdata4
),
1692 elfcpp::DW_CFA_def_cfa
, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1693 elfcpp::DW_CFA_offset
+ 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1694 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
1700 Output_data_plt_x86_64_standard
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
1702 0, 0, 0, 0, // Replaced with offset to .plt.
1703 0, 0, 0, 0, // Replaced with size of .plt.
1704 0, // Augmentation size.
1705 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
1706 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
1707 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
1708 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
1709 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
1710 11, // Block length.
1711 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
1712 elfcpp::DW_OP_breg16
, 0, // Push %rip.
1713 elfcpp::DW_OP_lit15
, // Push 0xf.
1714 elfcpp::DW_OP_and
, // & (%rip & 0xf).
1715 elfcpp::DW_OP_lit11
, // Push 0xb.
1716 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 0xb)
1717 elfcpp::DW_OP_lit3
, // Push 3.
1718 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 0xb) << 3)
1719 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1720 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
1726 // Write out the PLT. This uses the hand-coded instructions above,
1727 // and adjusts them as needed. This is specified by the AMD64 ABI.
1731 Output_data_plt_x86_64
<size
>::do_write(Output_file
* of
)
1733 const off_t offset
= this->offset();
1734 const section_size_type oview_size
=
1735 convert_to_section_size_type(this->data_size());
1736 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1738 const off_t got_file_offset
= this->got_plt_
->offset();
1739 gold_assert(parameters
->incremental_update()
1740 || (got_file_offset
+ this->got_plt_
->data_size()
1741 == this->got_irelative_
->offset()));
1742 const section_size_type got_size
=
1743 convert_to_section_size_type(this->got_plt_
->data_size()
1744 + this->got_irelative_
->data_size());
1745 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1748 unsigned char* pov
= oview
;
1750 // The base address of the .plt section.
1751 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
1752 // The base address of the .got section.
1753 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
= this->got_
->address();
1754 // The base address of the PLT portion of the .got section,
1755 // which is where the GOT pointer will point, and where the
1756 // three reserved GOT entries are located.
1757 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
1758 = this->got_plt_
->address();
1760 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
1761 pov
+= this->get_plt_entry_size();
1763 // The first three entries in the GOT are reserved, and are written
1764 // by Output_data_got_plt_x86_64::do_write.
1765 unsigned char* got_pov
= got_view
+ 24;
1767 unsigned int plt_offset
= this->get_plt_entry_size();
1768 unsigned int got_offset
= 24;
1769 const unsigned int count
= this->count_
+ this->irelative_count_
;
1770 for (unsigned int plt_index
= 0;
1773 pov
+= this->get_plt_entry_size(),
1775 plt_offset
+= this->get_plt_entry_size(),
1778 // Set and adjust the PLT entry itself.
1779 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
1780 got_address
, plt_address
,
1781 got_offset
, plt_offset
,
1784 // Set the entry in the GOT.
1785 elfcpp::Swap
<64, false>::writeval(got_pov
,
1786 plt_address
+ plt_offset
+ lazy_offset
);
1789 if (this->has_tlsdesc_entry())
1791 // Set and adjust the reserved TLSDESC PLT entry.
1792 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
1793 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
1794 tlsdesc_got_offset
, plt_offset
);
1795 pov
+= this->get_plt_entry_size();
1798 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1799 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1801 of
->write_output_view(offset
, oview_size
, oview
);
1802 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1805 // Create the PLT section.
1809 Target_x86_64
<size
>::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1811 if (this->plt_
== NULL
)
1813 // Create the GOT sections first.
1814 this->got_section(symtab
, layout
);
1816 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
1817 this->got_irelative_
);
1819 // Add unwind information if requested.
1820 if (parameters
->options().ld_generated_unwind_info())
1821 this->plt_
->add_eh_frame(layout
);
1823 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1825 | elfcpp::SHF_EXECINSTR
),
1826 this->plt_
, ORDER_PLT
, false);
1828 // Make the sh_info field of .rela.plt point to .plt.
1829 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1830 rela_plt_os
->set_info_section(this->plt_
->output_section());
1834 // Return the section for TLSDESC relocations.
1837 typename Target_x86_64
<size
>::Reloc_section
*
1838 Target_x86_64
<size
>::rela_tlsdesc_section(Layout
* layout
) const
1840 return this->plt_section()->rela_tlsdesc(layout
);
1843 // Create a PLT entry for a global symbol.
1847 Target_x86_64
<size
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1850 if (gsym
->has_plt_offset())
1853 if (this->plt_
== NULL
)
1854 this->make_plt_section(symtab
, layout
);
1856 this->plt_
->add_entry(symtab
, layout
, gsym
);
1859 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1863 Target_x86_64
<size
>::make_local_ifunc_plt_entry(
1864 Symbol_table
* symtab
, Layout
* layout
,
1865 Sized_relobj_file
<size
, false>* relobj
,
1866 unsigned int local_sym_index
)
1868 if (relobj
->local_has_plt_offset(local_sym_index
))
1870 if (this->plt_
== NULL
)
1871 this->make_plt_section(symtab
, layout
);
1872 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
1875 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1878 // Return the number of entries in the PLT.
1882 Target_x86_64
<size
>::plt_entry_count() const
1884 if (this->plt_
== NULL
)
1886 return this->plt_
->entry_count();
1889 // Return the offset of the first non-reserved PLT entry.
1893 Target_x86_64
<size
>::first_plt_entry_offset() const
1895 if (this->plt_
== NULL
)
1897 return this->plt_
->first_plt_entry_offset();
1900 // Return the size of each PLT entry.
1904 Target_x86_64
<size
>::plt_entry_size() const
1906 if (this->plt_
== NULL
)
1908 return this->plt_
->get_plt_entry_size();
1911 // Create the GOT and PLT sections for an incremental update.
1914 Output_data_got_base
*
1915 Target_x86_64
<size
>::init_got_plt_for_update(Symbol_table
* symtab
,
1917 unsigned int got_count
,
1918 unsigned int plt_count
)
1920 gold_assert(this->got_
== NULL
);
1922 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
1923 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1925 | elfcpp::SHF_WRITE
),
1926 this->got_
, ORDER_RELRO_LAST
,
1929 // Add the three reserved entries.
1930 this->got_plt_
= new Output_data_got_plt_x86_64(layout
, (plt_count
+ 3) * 8);
1931 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1933 | elfcpp::SHF_WRITE
),
1934 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
1937 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1938 this->global_offset_table_
=
1939 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1940 Symbol_table::PREDEFINED
,
1942 0, 0, elfcpp::STT_OBJECT
,
1944 elfcpp::STV_HIDDEN
, 0,
1947 // If there are any TLSDESC relocations, they get GOT entries in
1948 // .got.plt after the jump slot entries.
1949 // FIXME: Get the count for TLSDESC entries.
1950 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
1951 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1952 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1954 ORDER_NON_RELRO_FIRST
, false);
1956 // If there are any IRELATIVE relocations, they get GOT entries in
1957 // .got.plt after the jump slot and TLSDESC entries.
1958 this->got_irelative_
= new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1959 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1960 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1961 this->got_irelative_
,
1962 ORDER_NON_RELRO_FIRST
, false);
1964 // Create the PLT section.
1965 this->plt_
= this->make_data_plt(layout
, this->got_
,
1967 this->got_irelative_
,
1970 // Add unwind information if requested.
1971 if (parameters
->options().ld_generated_unwind_info())
1972 this->plt_
->add_eh_frame(layout
);
1974 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1975 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1976 this->plt_
, ORDER_PLT
, false);
1978 // Make the sh_info field of .rela.plt point to .plt.
1979 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1980 rela_plt_os
->set_info_section(this->plt_
->output_section());
1982 // Create the rela_dyn section.
1983 this->rela_dyn_section(layout
);
1988 // Reserve a GOT entry for a local symbol, and regenerate any
1989 // necessary dynamic relocations.
1993 Target_x86_64
<size
>::reserve_local_got_entry(
1994 unsigned int got_index
,
1995 Sized_relobj
<size
, false>* obj
,
1997 unsigned int got_type
)
1999 unsigned int got_offset
= got_index
* 8;
2000 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
2002 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
2005 case GOT_TYPE_STANDARD
:
2006 if (parameters
->options().output_is_position_independent())
2007 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
2008 this->got_
, got_offset
, 0, false);
2010 case GOT_TYPE_TLS_OFFSET
:
2011 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
2012 this->got_
, got_offset
, 0);
2014 case GOT_TYPE_TLS_PAIR
:
2015 this->got_
->reserve_slot(got_index
+ 1);
2016 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
2017 this->got_
, got_offset
, 0);
2019 case GOT_TYPE_TLS_DESC
:
2020 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
2021 // this->got_->reserve_slot(got_index + 1);
2022 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
2023 // this->got_, got_offset, 0);
2030 // Reserve a GOT entry for a global symbol, and regenerate any
2031 // necessary dynamic relocations.
2035 Target_x86_64
<size
>::reserve_global_got_entry(unsigned int got_index
,
2037 unsigned int got_type
)
2039 unsigned int got_offset
= got_index
* 8;
2040 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
2042 this->got_
->reserve_global(got_index
, gsym
, got_type
);
2045 case GOT_TYPE_STANDARD
:
2046 if (!gsym
->final_value_is_known())
2048 if (gsym
->is_from_dynobj()
2049 || gsym
->is_undefined()
2050 || gsym
->is_preemptible()
2051 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
2052 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
2053 this->got_
, got_offset
, 0);
2055 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2056 this->got_
, got_offset
, 0, false);
2059 case GOT_TYPE_TLS_OFFSET
:
2060 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
2061 this->got_
, got_offset
, 0, false);
2063 case GOT_TYPE_TLS_PAIR
:
2064 this->got_
->reserve_slot(got_index
+ 1);
2065 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
2066 this->got_
, got_offset
, 0, false);
2067 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
2068 this->got_
, got_offset
+ 8, 0, false);
2070 case GOT_TYPE_TLS_DESC
:
2071 this->got_
->reserve_slot(got_index
+ 1);
2072 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
2073 this->got_
, got_offset
, 0, false);
2080 // Register an existing PLT entry for a global symbol.
2084 Target_x86_64
<size
>::register_global_plt_entry(Symbol_table
* symtab
,
2086 unsigned int plt_index
,
2089 gold_assert(this->plt_
!= NULL
);
2090 gold_assert(!gsym
->has_plt_offset());
2092 this->plt_
->reserve_slot(plt_index
);
2094 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
2096 unsigned int got_offset
= (plt_index
+ 3) * 8;
2097 this->plt_
->add_relocation(symtab
, layout
, gsym
, got_offset
);
2100 // Force a COPY relocation for a given symbol.
2104 Target_x86_64
<size
>::emit_copy_reloc(
2105 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
2107 this->copy_relocs_
.emit_copy_reloc(symtab
,
2108 symtab
->get_sized_symbol
<size
>(sym
),
2111 this->rela_dyn_section(NULL
));
2114 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2118 Target_x86_64
<size
>::define_tls_base_symbol(Symbol_table
* symtab
,
2121 if (this->tls_base_symbol_defined_
)
2124 Output_segment
* tls_segment
= layout
->tls_segment();
2125 if (tls_segment
!= NULL
)
2127 bool is_exec
= parameters
->options().output_is_executable();
2128 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
2129 Symbol_table::PREDEFINED
,
2133 elfcpp::STV_HIDDEN
, 0,
2135 ? Symbol::SEGMENT_END
2136 : Symbol::SEGMENT_START
),
2139 this->tls_base_symbol_defined_
= true;
2142 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2146 Target_x86_64
<size
>::reserve_tlsdesc_entries(Symbol_table
* symtab
,
2149 if (this->plt_
== NULL
)
2150 this->make_plt_section(symtab
, layout
);
2152 if (!this->plt_
->has_tlsdesc_entry())
2154 // Allocate the TLSDESC_GOT entry.
2155 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2156 unsigned int got_offset
= got
->add_constant(0);
2158 // Allocate the TLSDESC_PLT entry.
2159 this->plt_
->reserve_tlsdesc_entry(got_offset
);
2163 // Create a GOT entry for the TLS module index.
2167 Target_x86_64
<size
>::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
2168 Sized_relobj_file
<size
, false>* object
)
2170 if (this->got_mod_index_offset_
== -1U)
2172 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
2173 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
2174 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2175 unsigned int got_offset
= got
->add_constant(0);
2176 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
2178 got
->add_constant(0);
2179 this->got_mod_index_offset_
= got_offset
;
2181 return this->got_mod_index_offset_
;
2184 // Optimize the TLS relocation type based on what we know about the
2185 // symbol. IS_FINAL is true if the final address of this symbol is
2186 // known at link time.
2189 tls::Tls_optimization
2190 Target_x86_64
<size
>::optimize_tls_reloc(bool is_final
, int r_type
)
2192 // If we are generating a shared library, then we can't do anything
2194 if (parameters
->options().shared())
2195 return tls::TLSOPT_NONE
;
2199 case elfcpp::R_X86_64_TLSGD
:
2200 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2201 case elfcpp::R_X86_64_TLSDESC_CALL
:
2202 // These are General-Dynamic which permits fully general TLS
2203 // access. Since we know that we are generating an executable,
2204 // we can convert this to Initial-Exec. If we also know that
2205 // this is a local symbol, we can further switch to Local-Exec.
2207 return tls::TLSOPT_TO_LE
;
2208 return tls::TLSOPT_TO_IE
;
2210 case elfcpp::R_X86_64_TLSLD
:
2211 // This is Local-Dynamic, which refers to a local symbol in the
2212 // dynamic TLS block. Since we know that we generating an
2213 // executable, we can switch to Local-Exec.
2214 return tls::TLSOPT_TO_LE
;
2216 case elfcpp::R_X86_64_DTPOFF32
:
2217 case elfcpp::R_X86_64_DTPOFF64
:
2218 // Another Local-Dynamic reloc.
2219 return tls::TLSOPT_TO_LE
;
2221 case elfcpp::R_X86_64_GOTTPOFF
:
2222 // These are Initial-Exec relocs which get the thread offset
2223 // from the GOT. If we know that we are linking against the
2224 // local symbol, we can switch to Local-Exec, which links the
2225 // thread offset into the instruction.
2227 return tls::TLSOPT_TO_LE
;
2228 return tls::TLSOPT_NONE
;
2230 case elfcpp::R_X86_64_TPOFF32
:
2231 // When we already have Local-Exec, there is nothing further we
2233 return tls::TLSOPT_NONE
;
2240 // Get the Reference_flags for a particular relocation.
2244 Target_x86_64
<size
>::Scan::get_reference_flags(unsigned int r_type
)
2248 case elfcpp::R_X86_64_NONE
:
2249 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2250 case elfcpp::R_X86_64_GNU_VTENTRY
:
2251 case elfcpp::R_X86_64_GOTPC32
:
2252 case elfcpp::R_X86_64_GOTPC64
:
2253 // No symbol reference.
2256 case elfcpp::R_X86_64_64
:
2257 case elfcpp::R_X86_64_32
:
2258 case elfcpp::R_X86_64_32S
:
2259 case elfcpp::R_X86_64_16
:
2260 case elfcpp::R_X86_64_8
:
2261 return Symbol::ABSOLUTE_REF
;
2263 case elfcpp::R_X86_64_PC64
:
2264 case elfcpp::R_X86_64_PC32
:
2265 case elfcpp::R_X86_64_PC32_BND
:
2266 case elfcpp::R_X86_64_PC16
:
2267 case elfcpp::R_X86_64_PC8
:
2268 case elfcpp::R_X86_64_GOTOFF64
:
2269 return Symbol::RELATIVE_REF
;
2271 case elfcpp::R_X86_64_PLT32
:
2272 case elfcpp::R_X86_64_PLT32_BND
:
2273 case elfcpp::R_X86_64_PLTOFF64
:
2274 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
2276 case elfcpp::R_X86_64_GOT64
:
2277 case elfcpp::R_X86_64_GOT32
:
2278 case elfcpp::R_X86_64_GOTPCREL64
:
2279 case elfcpp::R_X86_64_GOTPCREL
:
2280 case elfcpp::R_X86_64_GOTPCRELX
:
2281 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2282 case elfcpp::R_X86_64_GOTPLT64
:
2284 return Symbol::ABSOLUTE_REF
;
2286 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2287 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2288 case elfcpp::R_X86_64_TLSDESC_CALL
:
2289 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2290 case elfcpp::R_X86_64_DTPOFF32
:
2291 case elfcpp::R_X86_64_DTPOFF64
:
2292 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2293 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2294 return Symbol::TLS_REF
;
2296 case elfcpp::R_X86_64_COPY
:
2297 case elfcpp::R_X86_64_GLOB_DAT
:
2298 case elfcpp::R_X86_64_JUMP_SLOT
:
2299 case elfcpp::R_X86_64_RELATIVE
:
2300 case elfcpp::R_X86_64_IRELATIVE
:
2301 case elfcpp::R_X86_64_TPOFF64
:
2302 case elfcpp::R_X86_64_DTPMOD64
:
2303 case elfcpp::R_X86_64_TLSDESC
:
2304 case elfcpp::R_X86_64_SIZE32
:
2305 case elfcpp::R_X86_64_SIZE64
:
2307 // Not expected. We will give an error later.
2312 // Report an unsupported relocation against a local symbol.
2316 Target_x86_64
<size
>::Scan::unsupported_reloc_local(
2317 Sized_relobj_file
<size
, false>* object
,
2318 unsigned int r_type
)
2320 gold_error(_("%s: unsupported reloc %u against local symbol"),
2321 object
->name().c_str(), r_type
);
2324 // We are about to emit a dynamic relocation of type R_TYPE. If the
2325 // dynamic linker does not support it, issue an error. The GNU linker
2326 // only issues a non-PIC error for an allocated read-only section.
2327 // Here we know the section is allocated, but we don't know that it is
2328 // read-only. But we check for all the relocation types which the
2329 // glibc dynamic linker supports, so it seems appropriate to issue an
2330 // error even if the section is not read-only. If GSYM is not NULL,
2331 // it is the symbol the relocation is against; if it is NULL, the
2332 // relocation is against a local symbol.
2336 Target_x86_64
<size
>::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
2341 // These are the relocation types supported by glibc for x86_64
2342 // which should always work.
2343 case elfcpp::R_X86_64_RELATIVE
:
2344 case elfcpp::R_X86_64_IRELATIVE
:
2345 case elfcpp::R_X86_64_GLOB_DAT
:
2346 case elfcpp::R_X86_64_JUMP_SLOT
:
2347 case elfcpp::R_X86_64_DTPMOD64
:
2348 case elfcpp::R_X86_64_DTPOFF64
:
2349 case elfcpp::R_X86_64_TPOFF64
:
2350 case elfcpp::R_X86_64_64
:
2351 case elfcpp::R_X86_64_COPY
:
2354 // glibc supports these reloc types, but they can overflow.
2355 case elfcpp::R_X86_64_PC32
:
2356 case elfcpp::R_X86_64_PC32_BND
:
2357 // A PC relative reference is OK against a local symbol or if
2358 // the symbol is defined locally.
2360 || (!gsym
->is_from_dynobj()
2361 && !gsym
->is_undefined()
2362 && !gsym
->is_preemptible()))
2365 case elfcpp::R_X86_64_32
:
2366 // R_X86_64_32 is OK for x32.
2367 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2369 if (this->issued_non_pic_error_
)
2371 gold_assert(parameters
->options().output_is_position_independent());
2373 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
2374 "overflow at runtime; recompile with -fPIC"));
2380 case elfcpp::R_X86_64_32
:
2381 r_name
= "R_X86_64_32";
2383 case elfcpp::R_X86_64_PC32
:
2384 r_name
= "R_X86_64_PC32";
2386 case elfcpp::R_X86_64_PC32_BND
:
2387 r_name
= "R_X86_64_PC32_BND";
2393 object
->error(_("requires dynamic %s reloc against '%s' "
2394 "which may overflow at runtime; recompile "
2396 r_name
, gsym
->name());
2398 this->issued_non_pic_error_
= true;
2402 // This prevents us from issuing more than one error per reloc
2403 // section. But we can still wind up issuing more than one
2404 // error per object file.
2405 if (this->issued_non_pic_error_
)
2407 gold_assert(parameters
->options().output_is_position_independent());
2408 object
->error(_("requires unsupported dynamic reloc %u; "
2409 "recompile with -fPIC"),
2411 this->issued_non_pic_error_
= true;
2414 case elfcpp::R_X86_64_NONE
:
2419 // Return whether we need to make a PLT entry for a relocation of the
2420 // given type against a STT_GNU_IFUNC symbol.
2424 Target_x86_64
<size
>::Scan::reloc_needs_plt_for_ifunc(
2425 Sized_relobj_file
<size
, false>* object
,
2426 unsigned int r_type
)
2428 int flags
= Scan::get_reference_flags(r_type
);
2429 if (flags
& Symbol::TLS_REF
)
2430 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2431 object
->name().c_str(), r_type
);
2435 // Scan a relocation for a local symbol.
2439 Target_x86_64
<size
>::Scan::local(Symbol_table
* symtab
,
2441 Target_x86_64
<size
>* target
,
2442 Sized_relobj_file
<size
, false>* object
,
2443 unsigned int data_shndx
,
2444 Output_section
* output_section
,
2445 const elfcpp::Rela
<size
, false>& reloc
,
2446 unsigned int r_type
,
2447 const elfcpp::Sym
<size
, false>& lsym
,
2453 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2454 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
2455 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
2457 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2458 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
2463 case elfcpp::R_X86_64_NONE
:
2464 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2465 case elfcpp::R_X86_64_GNU_VTENTRY
:
2468 case elfcpp::R_X86_64_64
:
2469 // If building a shared library (or a position-independent
2470 // executable), we need to create a dynamic relocation for this
2471 // location. The relocation applied at link time will apply the
2472 // link-time value, so we flag the location with an
2473 // R_X86_64_RELATIVE relocation so the dynamic loader can
2474 // relocate it easily.
2475 if (parameters
->options().output_is_position_independent())
2477 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2478 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2479 rela_dyn
->add_local_relative(object
, r_sym
,
2481 ? elfcpp::R_X86_64_RELATIVE64
2482 : elfcpp::R_X86_64_RELATIVE
),
2483 output_section
, data_shndx
,
2484 reloc
.get_r_offset(),
2485 reloc
.get_r_addend(), is_ifunc
);
2489 case elfcpp::R_X86_64_32
:
2490 case elfcpp::R_X86_64_32S
:
2491 case elfcpp::R_X86_64_16
:
2492 case elfcpp::R_X86_64_8
:
2493 // If building a shared library (or a position-independent
2494 // executable), we need to create a dynamic relocation for this
2495 // location. We can't use an R_X86_64_RELATIVE relocation
2496 // because that is always a 64-bit relocation.
2497 if (parameters
->options().output_is_position_independent())
2499 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2500 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2502 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2503 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2504 rela_dyn
->add_local_relative(object
, r_sym
,
2505 elfcpp::R_X86_64_RELATIVE
,
2506 output_section
, data_shndx
,
2507 reloc
.get_r_offset(),
2508 reloc
.get_r_addend(), is_ifunc
);
2512 this->check_non_pic(object
, r_type
, NULL
);
2514 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2515 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2516 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
2517 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
2518 data_shndx
, reloc
.get_r_offset(),
2519 reloc
.get_r_addend());
2522 gold_assert(lsym
.get_st_value() == 0);
2523 unsigned int shndx
= lsym
.get_st_shndx();
2525 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
2528 object
->error(_("section symbol %u has bad shndx %u"),
2531 rela_dyn
->add_local_section(object
, shndx
,
2532 r_type
, output_section
,
2533 data_shndx
, reloc
.get_r_offset(),
2534 reloc
.get_r_addend());
2539 case elfcpp::R_X86_64_PC64
:
2540 case elfcpp::R_X86_64_PC32
:
2541 case elfcpp::R_X86_64_PC32_BND
:
2542 case elfcpp::R_X86_64_PC16
:
2543 case elfcpp::R_X86_64_PC8
:
2546 case elfcpp::R_X86_64_PLT32
:
2547 case elfcpp::R_X86_64_PLT32_BND
:
2548 // Since we know this is a local symbol, we can handle this as a
2552 case elfcpp::R_X86_64_GOTPC32
:
2553 case elfcpp::R_X86_64_GOTOFF64
:
2554 case elfcpp::R_X86_64_GOTPC64
:
2555 case elfcpp::R_X86_64_PLTOFF64
:
2556 // We need a GOT section.
2557 target
->got_section(symtab
, layout
);
2558 // For PLTOFF64, we'd normally want a PLT section, but since we
2559 // know this is a local symbol, no PLT is needed.
2562 case elfcpp::R_X86_64_GOT64
:
2563 case elfcpp::R_X86_64_GOT32
:
2564 case elfcpp::R_X86_64_GOTPCREL64
:
2565 case elfcpp::R_X86_64_GOTPCREL
:
2566 case elfcpp::R_X86_64_GOTPCRELX
:
2567 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2568 case elfcpp::R_X86_64_GOTPLT64
:
2570 // The symbol requires a GOT section.
2571 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2573 // If the relocation symbol isn't IFUNC,
2574 // and is local, then we will convert
2575 // mov foo@GOTPCREL(%rip), %reg
2576 // to lea foo(%rip), %reg.
2577 // in Relocate::relocate.
2578 if ((r_type
== elfcpp::R_X86_64_GOTPCREL
2579 || r_type
== elfcpp::R_X86_64_GOTPCRELX
2580 || r_type
== elfcpp::R_X86_64_REX_GOTPCRELX
)
2581 && reloc
.get_r_offset() >= 2
2584 section_size_type stype
;
2585 const unsigned char* view
= object
->section_contents(data_shndx
,
2587 if (view
[reloc
.get_r_offset() - 2] == 0x8b)
2592 // The symbol requires a GOT entry.
2593 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2595 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2596 // lets function pointers compare correctly with shared
2597 // libraries. Otherwise we would need an IRELATIVE reloc.
2600 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
2602 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
2605 // If we are generating a shared object, we need to add a
2606 // dynamic relocation for this symbol's GOT entry.
2607 if (parameters
->options().output_is_position_independent())
2609 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2610 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2611 if (r_type
!= elfcpp::R_X86_64_GOT32
)
2613 unsigned int got_offset
=
2614 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
2615 rela_dyn
->add_local_relative(object
, r_sym
,
2616 elfcpp::R_X86_64_RELATIVE
,
2617 got
, got_offset
, 0, is_ifunc
);
2621 this->check_non_pic(object
, r_type
, NULL
);
2623 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
2624 rela_dyn
->add_local(
2625 object
, r_sym
, r_type
, got
,
2626 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
2630 // For GOTPLT64, we'd normally want a PLT section, but since
2631 // we know this is a local symbol, no PLT is needed.
2635 case elfcpp::R_X86_64_COPY
:
2636 case elfcpp::R_X86_64_GLOB_DAT
:
2637 case elfcpp::R_X86_64_JUMP_SLOT
:
2638 case elfcpp::R_X86_64_RELATIVE
:
2639 case elfcpp::R_X86_64_IRELATIVE
:
2640 // These are outstanding tls relocs, which are unexpected when linking
2641 case elfcpp::R_X86_64_TPOFF64
:
2642 case elfcpp::R_X86_64_DTPMOD64
:
2643 case elfcpp::R_X86_64_TLSDESC
:
2644 gold_error(_("%s: unexpected reloc %u in object file"),
2645 object
->name().c_str(), r_type
);
2648 // These are initial tls relocs, which are expected when linking
2649 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2650 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2651 case elfcpp::R_X86_64_TLSDESC_CALL
:
2652 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2653 case elfcpp::R_X86_64_DTPOFF32
:
2654 case elfcpp::R_X86_64_DTPOFF64
:
2655 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2656 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2658 bool output_is_shared
= parameters
->options().shared();
2659 const tls::Tls_optimization optimized_type
2660 = Target_x86_64
<size
>::optimize_tls_reloc(!output_is_shared
,
2664 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2665 if (optimized_type
== tls::TLSOPT_NONE
)
2667 // Create a pair of GOT entries for the module index and
2668 // dtv-relative offset.
2669 Output_data_got
<64, false>* got
2670 = target
->got_section(symtab
, layout
);
2671 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2672 unsigned int shndx
= lsym
.get_st_shndx();
2674 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2676 object
->error(_("local symbol %u has bad shndx %u"),
2679 got
->add_local_pair_with_rel(object
, r_sym
,
2682 target
->rela_dyn_section(layout
),
2683 elfcpp::R_X86_64_DTPMOD64
);
2685 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2686 unsupported_reloc_local(object
, r_type
);
2689 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2690 target
->define_tls_base_symbol(symtab
, layout
);
2691 if (optimized_type
== tls::TLSOPT_NONE
)
2693 // Create reserved PLT and GOT entries for the resolver.
2694 target
->reserve_tlsdesc_entries(symtab
, layout
);
2696 // Generate a double GOT entry with an
2697 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2698 // is resolved lazily, so the GOT entry needs to be in
2699 // an area in .got.plt, not .got. Call got_section to
2700 // make sure the section has been created.
2701 target
->got_section(symtab
, layout
);
2702 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2703 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2704 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
2706 unsigned int got_offset
= got
->add_constant(0);
2707 got
->add_constant(0);
2708 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
2710 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2711 // We store the arguments we need in a vector, and
2712 // use the index into the vector as the parameter
2713 // to pass to the target specific routines.
2714 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
2715 void* arg
= reinterpret_cast<void*>(intarg
);
2716 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
2717 got
, got_offset
, 0);
2720 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2721 unsupported_reloc_local(object
, r_type
);
2724 case elfcpp::R_X86_64_TLSDESC_CALL
:
2727 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2728 if (optimized_type
== tls::TLSOPT_NONE
)
2730 // Create a GOT entry for the module index.
2731 target
->got_mod_index_entry(symtab
, layout
, object
);
2733 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2734 unsupported_reloc_local(object
, r_type
);
2737 case elfcpp::R_X86_64_DTPOFF32
:
2738 case elfcpp::R_X86_64_DTPOFF64
:
2741 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2742 layout
->set_has_static_tls();
2743 if (optimized_type
== tls::TLSOPT_NONE
)
2745 // Create a GOT entry for the tp-relative offset.
2746 Output_data_got
<64, false>* got
2747 = target
->got_section(symtab
, layout
);
2748 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2749 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
2750 target
->rela_dyn_section(layout
),
2751 elfcpp::R_X86_64_TPOFF64
);
2753 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2754 unsupported_reloc_local(object
, r_type
);
2757 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2758 layout
->set_has_static_tls();
2759 if (output_is_shared
)
2760 unsupported_reloc_local(object
, r_type
);
2769 case elfcpp::R_X86_64_SIZE32
:
2770 case elfcpp::R_X86_64_SIZE64
:
2772 gold_error(_("%s: unsupported reloc %u against local symbol"),
2773 object
->name().c_str(), r_type
);
2779 // Report an unsupported relocation against a global symbol.
2783 Target_x86_64
<size
>::Scan::unsupported_reloc_global(
2784 Sized_relobj_file
<size
, false>* object
,
2785 unsigned int r_type
,
2788 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2789 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
2792 // Returns true if this relocation type could be that of a function pointer.
2795 Target_x86_64
<size
>::Scan::possible_function_pointer_reloc(unsigned int r_type
)
2799 case elfcpp::R_X86_64_64
:
2800 case elfcpp::R_X86_64_32
:
2801 case elfcpp::R_X86_64_32S
:
2802 case elfcpp::R_X86_64_16
:
2803 case elfcpp::R_X86_64_8
:
2804 case elfcpp::R_X86_64_GOT64
:
2805 case elfcpp::R_X86_64_GOT32
:
2806 case elfcpp::R_X86_64_GOTPCREL64
:
2807 case elfcpp::R_X86_64_GOTPCREL
:
2808 case elfcpp::R_X86_64_GOTPCRELX
:
2809 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2810 case elfcpp::R_X86_64_GOTPLT64
:
2818 // For safe ICF, scan a relocation for a local symbol to check if it
2819 // corresponds to a function pointer being taken. In that case mark
2820 // the function whose pointer was taken as not foldable.
2824 Target_x86_64
<size
>::Scan::local_reloc_may_be_function_pointer(
2827 Target_x86_64
<size
>* ,
2828 Sized_relobj_file
<size
, false>* ,
2831 const elfcpp::Rela
<size
, false>& ,
2832 unsigned int r_type
,
2833 const elfcpp::Sym
<size
, false>&)
2835 // When building a shared library, do not fold any local symbols as it is
2836 // not possible to distinguish pointer taken versus a call by looking at
2837 // the relocation types.
2838 return (parameters
->options().shared()
2839 || possible_function_pointer_reloc(r_type
));
2842 // For safe ICF, scan a relocation for a global symbol to check if it
2843 // corresponds to a function pointer being taken. In that case mark
2844 // the function whose pointer was taken as not foldable.
2848 Target_x86_64
<size
>::Scan::global_reloc_may_be_function_pointer(
2851 Target_x86_64
<size
>* ,
2852 Sized_relobj_file
<size
, false>* ,
2855 const elfcpp::Rela
<size
, false>& ,
2856 unsigned int r_type
,
2859 // When building a shared library, do not fold symbols whose visibility
2860 // is hidden, internal or protected.
2861 return ((parameters
->options().shared()
2862 && (gsym
->visibility() == elfcpp::STV_INTERNAL
2863 || gsym
->visibility() == elfcpp::STV_PROTECTED
2864 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
2865 || possible_function_pointer_reloc(r_type
));
2868 // Scan a relocation for a global symbol.
2872 Target_x86_64
<size
>::Scan::global(Symbol_table
* symtab
,
2874 Target_x86_64
<size
>* target
,
2875 Sized_relobj_file
<size
, false>* object
,
2876 unsigned int data_shndx
,
2877 Output_section
* output_section
,
2878 const elfcpp::Rela
<size
, false>& reloc
,
2879 unsigned int r_type
,
2882 // A STT_GNU_IFUNC symbol may require a PLT entry.
2883 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2884 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
2885 target
->make_plt_entry(symtab
, layout
, gsym
);
2889 case elfcpp::R_X86_64_NONE
:
2890 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2891 case elfcpp::R_X86_64_GNU_VTENTRY
:
2894 case elfcpp::R_X86_64_64
:
2895 case elfcpp::R_X86_64_32
:
2896 case elfcpp::R_X86_64_32S
:
2897 case elfcpp::R_X86_64_16
:
2898 case elfcpp::R_X86_64_8
:
2900 // Make a PLT entry if necessary.
2901 if (gsym
->needs_plt_entry())
2903 target
->make_plt_entry(symtab
, layout
, gsym
);
2904 // Since this is not a PC-relative relocation, we may be
2905 // taking the address of a function. In that case we need to
2906 // set the entry in the dynamic symbol table to the address of
2908 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
2909 gsym
->set_needs_dynsym_value();
2911 // Make a dynamic relocation if necessary.
2912 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2914 if (!parameters
->options().output_is_position_independent()
2915 && gsym
->may_need_copy_reloc())
2917 target
->copy_reloc(symtab
, layout
, object
,
2918 data_shndx
, output_section
, gsym
, reloc
);
2920 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
2921 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
2922 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2923 && gsym
->can_use_relative_reloc(false)
2924 && !gsym
->is_from_dynobj()
2925 && !gsym
->is_undefined()
2926 && !gsym
->is_preemptible())
2928 // Use an IRELATIVE reloc for a locally defined
2929 // STT_GNU_IFUNC symbol. This makes a function
2930 // address in a PIE executable match the address in a
2931 // shared library that it links against.
2932 Reloc_section
* rela_dyn
=
2933 target
->rela_irelative_section(layout
);
2934 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
2935 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
2936 output_section
, object
,
2938 reloc
.get_r_offset(),
2939 reloc
.get_r_addend());
2941 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
2942 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
2943 && gsym
->can_use_relative_reloc(false))
2945 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2946 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2947 output_section
, object
,
2949 reloc
.get_r_offset(),
2950 reloc
.get_r_addend(), false);
2954 this->check_non_pic(object
, r_type
, gsym
);
2955 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2956 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2957 data_shndx
, reloc
.get_r_offset(),
2958 reloc
.get_r_addend());
2964 case elfcpp::R_X86_64_PC64
:
2965 case elfcpp::R_X86_64_PC32
:
2966 case elfcpp::R_X86_64_PC32_BND
:
2967 case elfcpp::R_X86_64_PC16
:
2968 case elfcpp::R_X86_64_PC8
:
2970 // Make a PLT entry if necessary.
2971 if (gsym
->needs_plt_entry())
2972 target
->make_plt_entry(symtab
, layout
, gsym
);
2973 // Make a dynamic relocation if necessary.
2974 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2976 if (parameters
->options().output_is_executable()
2977 && gsym
->may_need_copy_reloc())
2979 target
->copy_reloc(symtab
, layout
, object
,
2980 data_shndx
, output_section
, gsym
, reloc
);
2984 this->check_non_pic(object
, r_type
, gsym
);
2985 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2986 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2987 data_shndx
, reloc
.get_r_offset(),
2988 reloc
.get_r_addend());
2994 case elfcpp::R_X86_64_GOT64
:
2995 case elfcpp::R_X86_64_GOT32
:
2996 case elfcpp::R_X86_64_GOTPCREL64
:
2997 case elfcpp::R_X86_64_GOTPCREL
:
2998 case elfcpp::R_X86_64_GOTPCRELX
:
2999 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3000 case elfcpp::R_X86_64_GOTPLT64
:
3002 // The symbol requires a GOT entry.
3003 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
3005 // If we convert this from
3006 // mov foo@GOTPCREL(%rip), %reg
3007 // to lea foo(%rip), %reg.
3010 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
3012 // in Relocate::relocate, then there is nothing to do here.
3014 Lazy_view
<size
> view(object
, data_shndx
);
3015 size_t r_offset
= reloc
.get_r_offset();
3017 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
, r_type
,
3022 && Target_x86_64
<size
>::can_convert_callq_to_direct(gsym
, r_type
,
3027 if (gsym
->final_value_is_known())
3029 // For a STT_GNU_IFUNC symbol we want the PLT address.
3030 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
3031 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
3033 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
3037 // If this symbol is not fully resolved, we need to add a
3038 // dynamic relocation for it.
3039 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3041 // Use a GLOB_DAT rather than a RELATIVE reloc if:
3043 // 1) The symbol may be defined in some other module.
3045 // 2) We are building a shared library and this is a
3046 // protected symbol; using GLOB_DAT means that the dynamic
3047 // linker can use the address of the PLT in the main
3048 // executable when appropriate so that function address
3049 // comparisons work.
3051 // 3) This is a STT_GNU_IFUNC symbol in position dependent
3052 // code, again so that function address comparisons work.
3053 if (gsym
->is_from_dynobj()
3054 || gsym
->is_undefined()
3055 || gsym
->is_preemptible()
3056 || (gsym
->visibility() == elfcpp::STV_PROTECTED
3057 && parameters
->options().shared())
3058 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
3059 && parameters
->options().output_is_position_independent()))
3060 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
3061 elfcpp::R_X86_64_GLOB_DAT
);
3064 // For a STT_GNU_IFUNC symbol we want to write the PLT
3065 // offset into the GOT, so that function pointer
3066 // comparisons work correctly.
3068 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
3069 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
3072 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
3073 // Tell the dynamic linker to use the PLT address
3074 // when resolving relocations.
3075 if (gsym
->is_from_dynobj()
3076 && !parameters
->options().shared())
3077 gsym
->set_needs_dynsym_value();
3081 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
3082 rela_dyn
->add_global_relative(gsym
,
3083 elfcpp::R_X86_64_RELATIVE
,
3084 got
, got_off
, 0, false);
3091 case elfcpp::R_X86_64_PLT32
:
3092 case elfcpp::R_X86_64_PLT32_BND
:
3093 // If the symbol is fully resolved, this is just a PC32 reloc.
3094 // Otherwise we need a PLT entry.
3095 if (gsym
->final_value_is_known())
3097 // If building a shared library, we can also skip the PLT entry
3098 // if the symbol is defined in the output file and is protected
3100 if (gsym
->is_defined()
3101 && !gsym
->is_from_dynobj()
3102 && !gsym
->is_preemptible())
3104 target
->make_plt_entry(symtab
, layout
, gsym
);
3107 case elfcpp::R_X86_64_GOTPC32
:
3108 case elfcpp::R_X86_64_GOTOFF64
:
3109 case elfcpp::R_X86_64_GOTPC64
:
3110 case elfcpp::R_X86_64_PLTOFF64
:
3111 // We need a GOT section.
3112 target
->got_section(symtab
, layout
);
3113 // For PLTOFF64, we also need a PLT entry (but only if the
3114 // symbol is not fully resolved).
3115 if (r_type
== elfcpp::R_X86_64_PLTOFF64
3116 && !gsym
->final_value_is_known())
3117 target
->make_plt_entry(symtab
, layout
, gsym
);
3120 case elfcpp::R_X86_64_COPY
:
3121 case elfcpp::R_X86_64_GLOB_DAT
:
3122 case elfcpp::R_X86_64_JUMP_SLOT
:
3123 case elfcpp::R_X86_64_RELATIVE
:
3124 case elfcpp::R_X86_64_IRELATIVE
:
3125 // These are outstanding tls relocs, which are unexpected when linking
3126 case elfcpp::R_X86_64_TPOFF64
:
3127 case elfcpp::R_X86_64_DTPMOD64
:
3128 case elfcpp::R_X86_64_TLSDESC
:
3129 gold_error(_("%s: unexpected reloc %u in object file"),
3130 object
->name().c_str(), r_type
);
3133 // These are initial tls relocs, which are expected for global()
3134 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3135 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3136 case elfcpp::R_X86_64_TLSDESC_CALL
:
3137 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3138 case elfcpp::R_X86_64_DTPOFF32
:
3139 case elfcpp::R_X86_64_DTPOFF64
:
3140 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3141 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3143 // For the Initial-Exec model, we can treat undef symbols as final
3144 // when building an executable.
3145 const bool is_final
= (gsym
->final_value_is_known() ||
3146 (r_type
== elfcpp::R_X86_64_GOTTPOFF
&&
3147 gsym
->is_undefined() &&
3148 parameters
->options().output_is_executable()));
3149 const tls::Tls_optimization optimized_type
3150 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3153 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
3154 if (optimized_type
== tls::TLSOPT_NONE
)
3156 // Create a pair of GOT entries for the module index and
3157 // dtv-relative offset.
3158 Output_data_got
<64, false>* got
3159 = target
->got_section(symtab
, layout
);
3160 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
3161 target
->rela_dyn_section(layout
),
3162 elfcpp::R_X86_64_DTPMOD64
,
3163 elfcpp::R_X86_64_DTPOFF64
);
3165 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3167 // Create a GOT entry for the tp-relative offset.
3168 Output_data_got
<64, false>* got
3169 = target
->got_section(symtab
, layout
);
3170 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3171 target
->rela_dyn_section(layout
),
3172 elfcpp::R_X86_64_TPOFF64
);
3174 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3175 unsupported_reloc_global(object
, r_type
, gsym
);
3178 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3179 target
->define_tls_base_symbol(symtab
, layout
);
3180 if (optimized_type
== tls::TLSOPT_NONE
)
3182 // Create reserved PLT and GOT entries for the resolver.
3183 target
->reserve_tlsdesc_entries(symtab
, layout
);
3185 // Create a double GOT entry with an R_X86_64_TLSDESC
3186 // reloc. The R_X86_64_TLSDESC reloc is resolved
3187 // lazily, so the GOT entry needs to be in an area in
3188 // .got.plt, not .got. Call got_section to make sure
3189 // the section has been created.
3190 target
->got_section(symtab
, layout
);
3191 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
3192 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
3193 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
3194 elfcpp::R_X86_64_TLSDESC
, 0);
3196 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3198 // Create a GOT entry for the tp-relative offset.
3199 Output_data_got
<64, false>* got
3200 = target
->got_section(symtab
, layout
);
3201 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3202 target
->rela_dyn_section(layout
),
3203 elfcpp::R_X86_64_TPOFF64
);
3205 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3206 unsupported_reloc_global(object
, r_type
, gsym
);
3209 case elfcpp::R_X86_64_TLSDESC_CALL
:
3212 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3213 if (optimized_type
== tls::TLSOPT_NONE
)
3215 // Create a GOT entry for the module index.
3216 target
->got_mod_index_entry(symtab
, layout
, object
);
3218 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3219 unsupported_reloc_global(object
, r_type
, gsym
);
3222 case elfcpp::R_X86_64_DTPOFF32
:
3223 case elfcpp::R_X86_64_DTPOFF64
:
3226 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3227 layout
->set_has_static_tls();
3228 if (optimized_type
== tls::TLSOPT_NONE
)
3230 // Create a GOT entry for the tp-relative offset.
3231 Output_data_got
<64, false>* got
3232 = target
->got_section(symtab
, layout
);
3233 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3234 target
->rela_dyn_section(layout
),
3235 elfcpp::R_X86_64_TPOFF64
);
3237 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3238 unsupported_reloc_global(object
, r_type
, gsym
);
3241 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3242 layout
->set_has_static_tls();
3243 if (parameters
->options().shared())
3244 unsupported_reloc_global(object
, r_type
, gsym
);
3253 case elfcpp::R_X86_64_SIZE32
:
3254 case elfcpp::R_X86_64_SIZE64
:
3256 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3257 object
->name().c_str(), r_type
,
3258 gsym
->demangled_name().c_str());
3265 Target_x86_64
<size
>::gc_process_relocs(Symbol_table
* symtab
,
3267 Sized_relobj_file
<size
, false>* object
,
3268 unsigned int data_shndx
,
3269 unsigned int sh_type
,
3270 const unsigned char* prelocs
,
3272 Output_section
* output_section
,
3273 bool needs_special_offset_handling
,
3274 size_t local_symbol_count
,
3275 const unsigned char* plocal_symbols
)
3277 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
3280 if (sh_type
== elfcpp::SHT_REL
)
3285 gold::gc_process_relocs
<size
, false, Target_x86_64
<size
>, Scan
,
3295 needs_special_offset_handling
,
3300 // Scan relocations for a section.
3304 Target_x86_64
<size
>::scan_relocs(Symbol_table
* symtab
,
3306 Sized_relobj_file
<size
, false>* object
,
3307 unsigned int data_shndx
,
3308 unsigned int sh_type
,
3309 const unsigned char* prelocs
,
3311 Output_section
* output_section
,
3312 bool needs_special_offset_handling
,
3313 size_t local_symbol_count
,
3314 const unsigned char* plocal_symbols
)
3316 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
3319 if (sh_type
== elfcpp::SHT_REL
)
3321 gold_error(_("%s: unsupported REL reloc section"),
3322 object
->name().c_str());
3326 gold::scan_relocs
<size
, false, Target_x86_64
<size
>, Scan
, Classify_reloc
>(
3335 needs_special_offset_handling
,
3340 // Finalize the sections.
3344 Target_x86_64
<size
>::do_finalize_sections(
3346 const Input_objects
*,
3347 Symbol_table
* symtab
)
3349 const Reloc_section
* rel_plt
= (this->plt_
== NULL
3351 : this->plt_
->rela_plt());
3352 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
3353 this->rela_dyn_
, true, false);
3355 // Fill in some more dynamic tags.
3356 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
3359 if (this->plt_
!= NULL
3360 && this->plt_
->output_section() != NULL
3361 && this->plt_
->has_tlsdesc_entry())
3363 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
3364 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
3365 this->got_
->finalize_data_size();
3366 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
3367 this->plt_
, plt_offset
);
3368 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
3369 this->got_
, got_offset
);
3373 // Emit any relocs we saved in an attempt to avoid generating COPY
3375 if (this->copy_relocs_
.any_saved_relocs())
3376 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
3378 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3379 // the .got.plt section.
3380 Symbol
* sym
= this->global_offset_table_
;
3383 uint64_t data_size
= this->got_plt_
->current_data_size();
3384 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
3387 if (parameters
->doing_static_link()
3388 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
3390 // If linking statically, make sure that the __rela_iplt symbols
3391 // were defined if necessary, even if we didn't create a PLT.
3392 static const Define_symbol_in_segment syms
[] =
3395 "__rela_iplt_start", // name
3396 elfcpp::PT_LOAD
, // segment_type
3397 elfcpp::PF_W
, // segment_flags_set
3398 elfcpp::PF(0), // segment_flags_clear
3401 elfcpp::STT_NOTYPE
, // type
3402 elfcpp::STB_GLOBAL
, // binding
3403 elfcpp::STV_HIDDEN
, // visibility
3405 Symbol::SEGMENT_START
, // offset_from_base
3409 "__rela_iplt_end", // name
3410 elfcpp::PT_LOAD
, // segment_type
3411 elfcpp::PF_W
, // segment_flags_set
3412 elfcpp::PF(0), // segment_flags_clear
3415 elfcpp::STT_NOTYPE
, // type
3416 elfcpp::STB_GLOBAL
, // binding
3417 elfcpp::STV_HIDDEN
, // visibility
3419 Symbol::SEGMENT_START
, // offset_from_base
3424 symtab
->define_symbols(layout
, 2, syms
,
3425 layout
->script_options()->saw_sections_clause());
3429 // For x32, we need to handle PC-relative relocations using full 64-bit
3430 // arithmetic, so that we can detect relocation overflows properly.
3431 // This class overrides the pcrela32_check methods from the defaults in
3432 // Relocate_functions in reloc.h.
3435 class X86_64_relocate_functions
: public Relocate_functions
<size
, false>
3438 typedef Relocate_functions
<size
, false> Base
;
3440 // Do a simple PC relative relocation with the addend in the
3442 static inline typename
Base::Reloc_status
3443 pcrela32_check(unsigned char* view
,
3444 typename
elfcpp::Elf_types
<64>::Elf_Addr value
,
3445 typename
elfcpp::Elf_types
<64>::Elf_Swxword addend
,
3446 typename
elfcpp::Elf_types
<64>::Elf_Addr address
)
3448 typedef typename
elfcpp::Swap
<32, false>::Valtype Valtype
;
3449 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3450 value
= value
+ addend
- address
;
3451 elfcpp::Swap
<32, false>::writeval(wv
, value
);
3452 return (Bits
<32>::has_overflow(value
)
3453 ? Base::RELOC_OVERFLOW
: Base::RELOC_OK
);
3456 // Do a simple PC relative relocation with a Symbol_value with the
3457 // addend in the relocation.
3458 static inline typename
Base::Reloc_status
3459 pcrela32_check(unsigned char* view
,
3460 const Sized_relobj_file
<size
, false>* object
,
3461 const Symbol_value
<size
>* psymval
,
3462 typename
elfcpp::Elf_types
<64>::Elf_Swxword addend
,
3463 typename
elfcpp::Elf_types
<64>::Elf_Addr address
)
3465 typedef typename
elfcpp::Swap
<32, false>::Valtype Valtype
;
3466 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
3467 typename
elfcpp::Elf_types
<64>::Elf_Addr value
;
3469 value
= psymval
->value(object
, addend
);
3472 // For negative addends, get the symbol value without
3473 // the addend, then add the addend using 64-bit arithmetic.
3474 value
= psymval
->value(object
, 0);
3478 elfcpp::Swap
<32, false>::writeval(wv
, value
);
3479 return (Bits
<32>::has_overflow(value
)
3480 ? Base::RELOC_OVERFLOW
: Base::RELOC_OK
);
3484 // Perform a relocation.
3488 Target_x86_64
<size
>::Relocate::relocate(
3489 const Relocate_info
<size
, false>* relinfo
,
3491 Target_x86_64
<size
>* target
,
3494 const unsigned char* preloc
,
3495 const Sized_symbol
<size
>* gsym
,
3496 const Symbol_value
<size
>* psymval
,
3497 unsigned char* view
,
3498 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3499 section_size_type view_size
)
3501 typedef X86_64_relocate_functions
<size
> Reloc_funcs
;
3502 const elfcpp::Rela
<size
, false> rela(preloc
);
3503 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
3505 if (this->skip_call_tls_get_addr_
)
3507 if ((r_type
!= elfcpp::R_X86_64_PLT32
3508 && r_type
!= elfcpp::R_X86_64_PLT32_BND
3509 && r_type
!= elfcpp::R_X86_64_PC32_BND
3510 && r_type
!= elfcpp::R_X86_64_PC32
)
3512 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
3514 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3515 _("missing expected TLS relocation"));
3519 this->skip_call_tls_get_addr_
= false;
3527 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3529 // Pick the value to use for symbols defined in the PLT.
3530 Symbol_value
<size
> symval
;
3532 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
3534 symval
.set_output_value(target
->plt_address_for_global(gsym
));
3537 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
3539 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3540 if (object
->local_has_plt_offset(r_sym
))
3542 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
3547 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3549 // Get the GOT offset if needed.
3550 // The GOT pointer points to the end of the GOT section.
3551 // We need to subtract the size of the GOT section to get
3552 // the actual offset to use in the relocation.
3553 bool have_got_offset
= false;
3554 // Since the actual offset is always negative, we use signed int to
3555 // support 64-bit GOT relocations.
3559 case elfcpp::R_X86_64_GOT32
:
3560 case elfcpp::R_X86_64_GOT64
:
3561 case elfcpp::R_X86_64_GOTPLT64
:
3562 case elfcpp::R_X86_64_GOTPCREL64
:
3565 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3566 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
3570 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3571 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3572 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
3573 - target
->got_size());
3575 have_got_offset
= true;
3582 typename
Reloc_funcs::Reloc_status rstatus
= Reloc_funcs::RELOC_OK
;
3586 case elfcpp::R_X86_64_NONE
:
3587 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3588 case elfcpp::R_X86_64_GNU_VTENTRY
:
3591 case elfcpp::R_X86_64_64
:
3592 Reloc_funcs::rela64(view
, object
, psymval
, addend
);
3595 case elfcpp::R_X86_64_PC64
:
3596 Reloc_funcs::pcrela64(view
, object
, psymval
, addend
,
3600 case elfcpp::R_X86_64_32
:
3601 rstatus
= Reloc_funcs::rela32_check(view
, object
, psymval
, addend
,
3602 Reloc_funcs::CHECK_UNSIGNED
);
3605 case elfcpp::R_X86_64_32S
:
3606 rstatus
= Reloc_funcs::rela32_check(view
, object
, psymval
, addend
,
3607 Reloc_funcs::CHECK_SIGNED
);
3610 case elfcpp::R_X86_64_PC32
:
3611 case elfcpp::R_X86_64_PC32_BND
:
3612 rstatus
= Reloc_funcs::pcrela32_check(view
, object
, psymval
, addend
,
3616 case elfcpp::R_X86_64_16
:
3617 Reloc_funcs::rela16(view
, object
, psymval
, addend
);
3620 case elfcpp::R_X86_64_PC16
:
3621 Reloc_funcs::pcrela16(view
, object
, psymval
, addend
, address
);
3624 case elfcpp::R_X86_64_8
:
3625 Reloc_funcs::rela8(view
, object
, psymval
, addend
);
3628 case elfcpp::R_X86_64_PC8
:
3629 Reloc_funcs::pcrela8(view
, object
, psymval
, addend
, address
);
3632 case elfcpp::R_X86_64_PLT32
:
3633 case elfcpp::R_X86_64_PLT32_BND
:
3634 gold_assert(gsym
== NULL
3635 || gsym
->has_plt_offset()
3636 || gsym
->final_value_is_known()
3637 || (gsym
->is_defined()
3638 && !gsym
->is_from_dynobj()
3639 && !gsym
->is_preemptible()));
3640 // Note: while this code looks the same as for R_X86_64_PC32, it
3641 // behaves differently because psymval was set to point to
3642 // the PLT entry, rather than the symbol, in Scan::global().
3643 rstatus
= Reloc_funcs::pcrela32_check(view
, object
, psymval
, addend
,
3647 case elfcpp::R_X86_64_PLTOFF64
:
3650 gold_assert(gsym
->has_plt_offset()
3651 || gsym
->final_value_is_known());
3652 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
;
3653 // This is the address of GLOBAL_OFFSET_TABLE.
3654 got_address
= target
->got_plt_section()->address();
3655 Reloc_funcs::rela64(view
, object
, psymval
, addend
- got_address
);
3659 case elfcpp::R_X86_64_GOT32
:
3660 gold_assert(have_got_offset
);
3661 Reloc_funcs::rela32(view
, got_offset
, addend
);
3664 case elfcpp::R_X86_64_GOTPC32
:
3667 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3668 value
= target
->got_plt_section()->address();
3669 Reloc_funcs::pcrela32_check(view
, value
, addend
, address
);
3673 case elfcpp::R_X86_64_GOT64
:
3674 case elfcpp::R_X86_64_GOTPLT64
:
3675 // R_X86_64_GOTPLT64 is obsolete and treated the the same as
3677 gold_assert(have_got_offset
);
3678 Reloc_funcs::rela64(view
, got_offset
, addend
);
3681 case elfcpp::R_X86_64_GOTPC64
:
3684 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3685 value
= target
->got_plt_section()->address();
3686 Reloc_funcs::pcrela64(view
, value
, addend
, address
);
3690 case elfcpp::R_X86_64_GOTOFF64
:
3692 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3693 value
= (psymval
->value(object
, 0)
3694 - target
->got_plt_section()->address());
3695 Reloc_funcs::rela64(view
, value
, addend
);
3699 case elfcpp::R_X86_64_GOTPCREL
:
3700 case elfcpp::R_X86_64_GOTPCRELX
:
3701 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3704 // mov foo@GOTPCREL(%rip), %reg
3705 // to lea foo(%rip), %reg.
3708 && rela
.get_r_offset() >= 2
3710 && !psymval
->is_ifunc_symbol())
3712 && rela
.get_r_offset() >= 2
3713 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
, r_type
,
3717 Reloc_funcs::pcrela32(view
, object
, psymval
, addend
, address
);
3720 // callq *foo@GOTPCRELX(%rip) to
3722 // and jmpq *foo@GOTPCRELX(%rip) to
3725 else if (gsym
!= NULL
3726 && rela
.get_r_offset() >= 2
3727 && Target_x86_64
<size
>::can_convert_callq_to_direct(gsym
,
3731 if (view
[-1] == 0x15)
3733 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
3734 // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
3737 // Convert GOTPCRELX to 32-bit pc relative reloc.
3738 Reloc_funcs::pcrela32(view
, object
, psymval
, addend
, address
);
3742 // Convert jmpq *foo@GOTPCRELX(%rip) to
3745 // The opcode of direct jmpq is 0xe9.
3747 // The opcode of nop is 0x90.
3749 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip
3750 // relative and since the instruction following the jmpq is now
3751 // the nop, offset the address by 1 byte. The start of the
3752 // relocation also moves ahead by 1 byte.
3753 Reloc_funcs::pcrela32(&view
[-1], object
, psymval
, addend
,
3761 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3762 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
3766 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3767 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3768 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
3769 - target
->got_size());
3771 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3772 value
= target
->got_plt_section()->address() + got_offset
;
3773 Reloc_funcs::pcrela32_check(view
, value
, addend
, address
);
3778 case elfcpp::R_X86_64_GOTPCREL64
:
3780 gold_assert(have_got_offset
);
3781 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3782 value
= target
->got_plt_section()->address() + got_offset
;
3783 Reloc_funcs::pcrela64(view
, value
, addend
, address
);
3787 case elfcpp::R_X86_64_COPY
:
3788 case elfcpp::R_X86_64_GLOB_DAT
:
3789 case elfcpp::R_X86_64_JUMP_SLOT
:
3790 case elfcpp::R_X86_64_RELATIVE
:
3791 case elfcpp::R_X86_64_IRELATIVE
:
3792 // These are outstanding tls relocs, which are unexpected when linking
3793 case elfcpp::R_X86_64_TPOFF64
:
3794 case elfcpp::R_X86_64_DTPMOD64
:
3795 case elfcpp::R_X86_64_TLSDESC
:
3796 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3797 _("unexpected reloc %u in object file"),
3801 // These are initial tls relocs, which are expected when linking
3802 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3803 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3804 case elfcpp::R_X86_64_TLSDESC_CALL
:
3805 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3806 case elfcpp::R_X86_64_DTPOFF32
:
3807 case elfcpp::R_X86_64_DTPOFF64
:
3808 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3809 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3810 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
3811 view
, address
, view_size
);
3814 case elfcpp::R_X86_64_SIZE32
:
3815 case elfcpp::R_X86_64_SIZE64
:
3817 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3818 _("unsupported reloc %u"),
3823 if (rstatus
== Reloc_funcs::RELOC_OVERFLOW
)
3827 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3828 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3829 _("relocation overflow: "
3830 "reference to local symbol %u in %s"),
3831 r_sym
, object
->name().c_str());
3833 else if (gsym
->is_defined() && gsym
->source() == Symbol::FROM_OBJECT
)
3835 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3836 _("relocation overflow: "
3837 "reference to '%s' defined in %s"),
3839 gsym
->object()->name().c_str());
3843 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3844 _("relocation overflow: reference to '%s'"),
3852 // Perform a TLS relocation.
3856 Target_x86_64
<size
>::Relocate::relocate_tls(
3857 const Relocate_info
<size
, false>* relinfo
,
3858 Target_x86_64
<size
>* target
,
3860 const elfcpp::Rela
<size
, false>& rela
,
3861 unsigned int r_type
,
3862 const Sized_symbol
<size
>* gsym
,
3863 const Symbol_value
<size
>* psymval
,
3864 unsigned char* view
,
3865 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3866 section_size_type view_size
)
3868 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
3870 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3871 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3872 elfcpp::Shdr
<size
, false> data_shdr(relinfo
->data_shdr
);
3873 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
3875 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
3877 const bool is_final
= (gsym
== NULL
3878 ? !parameters
->options().shared()
3879 : gsym
->final_value_is_known());
3880 tls::Tls_optimization optimized_type
3881 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3884 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3885 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3887 // If this code sequence is used in a non-executable section,
3888 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3889 // on the assumption that it's being used by itself in a debug
3890 // section. Therefore, in the unlikely event that the code
3891 // sequence appears in a non-executable section, we simply
3892 // leave it unoptimized.
3893 optimized_type
= tls::TLSOPT_NONE
;
3895 if (optimized_type
== tls::TLSOPT_TO_LE
)
3897 if (tls_segment
== NULL
)
3899 gold_assert(parameters
->errors()->error_count() > 0
3900 || issue_undefined_symbol_error(gsym
));
3903 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
3904 rela
, r_type
, value
, view
,
3910 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3911 ? GOT_TYPE_TLS_OFFSET
3912 : GOT_TYPE_TLS_PAIR
);
3913 unsigned int got_offset
;
3916 gold_assert(gsym
->has_got_offset(got_type
));
3917 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
3921 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3922 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3923 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
3924 - target
->got_size());
3926 if (optimized_type
== tls::TLSOPT_TO_IE
)
3928 value
= target
->got_plt_section()->address() + got_offset
;
3929 this->tls_gd_to_ie(relinfo
, relnum
, rela
, r_type
,
3930 value
, view
, address
, view_size
);
3933 else if (optimized_type
== tls::TLSOPT_NONE
)
3935 // Relocate the field with the offset of the pair of GOT
3937 value
= target
->got_plt_section()->address() + got_offset
;
3938 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3943 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3944 _("unsupported reloc %u"), r_type
);
3947 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3948 case elfcpp::R_X86_64_TLSDESC_CALL
:
3949 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3951 // See above comment for R_X86_64_TLSGD.
3952 optimized_type
= tls::TLSOPT_NONE
;
3954 if (optimized_type
== tls::TLSOPT_TO_LE
)
3956 if (tls_segment
== NULL
)
3958 gold_assert(parameters
->errors()->error_count() > 0
3959 || issue_undefined_symbol_error(gsym
));
3962 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
3963 rela
, r_type
, value
, view
,
3969 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3970 ? GOT_TYPE_TLS_OFFSET
3971 : GOT_TYPE_TLS_DESC
);
3972 unsigned int got_offset
= 0;
3973 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
3974 && optimized_type
== tls::TLSOPT_NONE
)
3976 // We created GOT entries in the .got.tlsdesc portion of
3977 // the .got.plt section, but the offset stored in the
3978 // symbol is the offset within .got.tlsdesc.
3979 got_offset
= (target
->got_size()
3980 + target
->got_plt_section()->data_size());
3984 gold_assert(gsym
->has_got_offset(got_type
));
3985 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
3989 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3990 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3991 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
3992 - target
->got_size());
3994 if (optimized_type
== tls::TLSOPT_TO_IE
)
3996 value
= target
->got_plt_section()->address() + got_offset
;
3997 this->tls_desc_gd_to_ie(relinfo
, relnum
,
3998 rela
, r_type
, value
, view
, address
,
4002 else if (optimized_type
== tls::TLSOPT_NONE
)
4004 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4006 // Relocate the field with the offset of the pair of GOT
4008 value
= target
->got_plt_section()->address() + got_offset
;
4009 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
4015 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4016 _("unsupported reloc %u"), r_type
);
4019 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4020 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
4022 // See above comment for R_X86_64_TLSGD.
4023 optimized_type
= tls::TLSOPT_NONE
;
4025 if (optimized_type
== tls::TLSOPT_TO_LE
)
4027 if (tls_segment
== NULL
)
4029 gold_assert(parameters
->errors()->error_count() > 0
4030 || issue_undefined_symbol_error(gsym
));
4033 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
4034 value
, view
, view_size
);
4037 else if (optimized_type
== tls::TLSOPT_NONE
)
4039 // Relocate the field with the offset of the GOT entry for
4040 // the module index.
4041 unsigned int got_offset
;
4042 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
4043 - target
->got_size());
4044 value
= target
->got_plt_section()->address() + got_offset
;
4045 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
4049 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4050 _("unsupported reloc %u"), r_type
);
4053 case elfcpp::R_X86_64_DTPOFF32
:
4054 // This relocation type is used in debugging information.
4055 // In that case we need to not optimize the value. If the
4056 // section is not executable, then we assume we should not
4057 // optimize this reloc. See comments above for R_X86_64_TLSGD,
4058 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
4060 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
4062 if (tls_segment
== NULL
)
4064 gold_assert(parameters
->errors()->error_count() > 0
4065 || issue_undefined_symbol_error(gsym
));
4068 value
-= tls_segment
->memsz();
4070 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
4073 case elfcpp::R_X86_64_DTPOFF64
:
4074 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
4075 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
4077 if (tls_segment
== NULL
)
4079 gold_assert(parameters
->errors()->error_count() > 0
4080 || issue_undefined_symbol_error(gsym
));
4083 value
-= tls_segment
->memsz();
4085 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
4088 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4090 && gsym
->is_undefined()
4091 && parameters
->options().output_is_executable())
4093 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
4095 r_type
, value
, view
,
4099 else if (optimized_type
== tls::TLSOPT_TO_LE
)
4101 if (tls_segment
== NULL
)
4103 gold_assert(parameters
->errors()->error_count() > 0
4104 || issue_undefined_symbol_error(gsym
));
4107 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
4109 r_type
, value
, view
,
4113 else if (optimized_type
== tls::TLSOPT_NONE
)
4115 // Relocate the field with the offset of the GOT entry for
4116 // the tp-relative offset of the symbol.
4117 unsigned int got_offset
;
4120 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
4121 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
4122 - target
->got_size());
4126 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4127 gold_assert(object
->local_has_got_offset(r_sym
,
4128 GOT_TYPE_TLS_OFFSET
));
4129 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
4130 - target
->got_size());
4132 value
= target
->got_plt_section()->address() + got_offset
;
4133 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
4137 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4138 _("unsupported reloc type %u"),
4142 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4143 if (tls_segment
== NULL
)
4145 gold_assert(parameters
->errors()->error_count() > 0
4146 || issue_undefined_symbol_error(gsym
));
4149 value
-= tls_segment
->memsz();
4150 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
4155 // Do a relocation in which we convert a TLS General-Dynamic to an
4160 Target_x86_64
<size
>::Relocate::tls_gd_to_ie(
4161 const Relocate_info
<size
, false>* relinfo
,
4163 const elfcpp::Rela
<size
, false>& rela
,
4165 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4166 unsigned char* view
,
4167 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4168 section_size_type view_size
)
4171 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4172 // .word 0x6666; rex64; call __tls_get_addr
4173 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4175 // leaq foo@tlsgd(%rip),%rdi;
4176 // .word 0x6666; rex64; call __tls_get_addr
4177 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4179 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
4180 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4181 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
4185 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4187 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4188 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
4189 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4194 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4196 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4197 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
4198 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4202 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4203 Relocate_functions
<size
, false>::pcrela32(view
+ 8, value
, addend
- 8,
4206 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4208 this->skip_call_tls_get_addr_
= true;
4211 // Do a relocation in which we convert a TLS General-Dynamic to a
4216 Target_x86_64
<size
>::Relocate::tls_gd_to_le(
4217 const Relocate_info
<size
, false>* relinfo
,
4219 Output_segment
* tls_segment
,
4220 const elfcpp::Rela
<size
, false>& rela
,
4222 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4223 unsigned char* view
,
4224 section_size_type view_size
)
4227 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4228 // .word 0x6666; rex64; call __tls_get_addr
4229 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4231 // leaq foo@tlsgd(%rip),%rdi;
4232 // .word 0x6666; rex64; call __tls_get_addr
4233 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4235 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
4236 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4237 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
4241 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4243 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4244 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
4245 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4250 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4252 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4253 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
4255 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4259 value
-= tls_segment
->memsz();
4260 Relocate_functions
<size
, false>::rela32(view
+ 8, value
, 0);
4262 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4264 this->skip_call_tls_get_addr_
= true;
4267 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4271 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_ie(
4272 const Relocate_info
<size
, false>* relinfo
,
4274 const elfcpp::Rela
<size
, false>& rela
,
4275 unsigned int r_type
,
4276 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4277 unsigned char* view
,
4278 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4279 section_size_type view_size
)
4281 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4283 // leaq foo@tlsdesc(%rip), %rax
4284 // ==> movq foo@gottpoff(%rip), %rax
4285 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4286 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4287 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4288 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
4290 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4291 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
4295 // call *foo@tlscall(%rax)
4297 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
4298 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
4299 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4300 view
[0] == 0xff && view
[1] == 0x10);
4306 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4310 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_le(
4311 const Relocate_info
<size
, false>* relinfo
,
4313 Output_segment
* tls_segment
,
4314 const elfcpp::Rela
<size
, false>& rela
,
4315 unsigned int r_type
,
4316 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4317 unsigned char* view
,
4318 section_size_type view_size
)
4320 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4322 // leaq foo@tlsdesc(%rip), %rax
4323 // ==> movq foo@tpoff, %rax
4324 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4325 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4326 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4327 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
4330 value
-= tls_segment
->memsz();
4331 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
4335 // call *foo@tlscall(%rax)
4337 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
4338 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
4339 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4340 view
[0] == 0xff && view
[1] == 0x10);
4348 Target_x86_64
<size
>::Relocate::tls_ld_to_le(
4349 const Relocate_info
<size
, false>* relinfo
,
4352 const elfcpp::Rela
<size
, false>& rela
,
4354 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
4355 unsigned char* view
,
4356 section_size_type view_size
)
4358 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4360 // ... leq foo@dtpoff(%rax),%reg
4361 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4363 // ... leq foo@dtpoff(%rax),%reg
4364 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4366 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4367 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
4369 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4370 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
4372 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
4375 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4377 memcpy(view
- 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4379 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4381 this->skip_call_tls_get_addr_
= true;
4384 // Do a relocation in which we convert a TLS Initial-Exec to a
4389 Target_x86_64
<size
>::Relocate::tls_ie_to_le(
4390 const Relocate_info
<size
, false>* relinfo
,
4392 Output_segment
* tls_segment
,
4393 const elfcpp::Rela
<size
, false>& rela
,
4395 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4396 unsigned char* view
,
4397 section_size_type view_size
)
4399 // We need to examine the opcodes to figure out which instruction we
4402 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4403 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4405 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4406 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4408 unsigned char op1
= view
[-3];
4409 unsigned char op2
= view
[-2];
4410 unsigned char op3
= view
[-1];
4411 unsigned char reg
= op3
>> 3;
4418 else if (size
== 32 && op1
== 0x44)
4421 view
[-1] = 0xc0 | reg
;
4425 // Special handling for %rsp.
4428 else if (size
== 32 && op1
== 0x44)
4431 view
[-1] = 0xc0 | reg
;
4438 else if (size
== 32 && op1
== 0x44)
4441 view
[-1] = 0x80 | reg
| (reg
<< 3);
4444 if (tls_segment
!= NULL
)
4445 value
-= tls_segment
->memsz();
4446 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
4449 // Relocate section data.
4453 Target_x86_64
<size
>::relocate_section(
4454 const Relocate_info
<size
, false>* relinfo
,
4455 unsigned int sh_type
,
4456 const unsigned char* prelocs
,
4458 Output_section
* output_section
,
4459 bool needs_special_offset_handling
,
4460 unsigned char* view
,
4461 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4462 section_size_type view_size
,
4463 const Reloc_symbol_changes
* reloc_symbol_changes
)
4465 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
4468 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4470 gold::relocate_section
<size
, false, Target_x86_64
<size
>, Relocate
,
4471 gold::Default_comdat_behavior
, Classify_reloc
>(
4477 needs_special_offset_handling
,
4481 reloc_symbol_changes
);
4484 // Apply an incremental relocation. Incremental relocations always refer
4485 // to global symbols.
4489 Target_x86_64
<size
>::apply_relocation(
4490 const Relocate_info
<size
, false>* relinfo
,
4491 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
4492 unsigned int r_type
,
4493 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
4495 unsigned char* view
,
4496 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4497 section_size_type view_size
)
4499 gold::apply_relocation
<size
, false, Target_x86_64
<size
>,
4500 typename Target_x86_64
<size
>::Relocate
>(
4512 // Scan the relocs during a relocatable link.
4516 Target_x86_64
<size
>::scan_relocatable_relocs(
4517 Symbol_table
* symtab
,
4519 Sized_relobj_file
<size
, false>* object
,
4520 unsigned int data_shndx
,
4521 unsigned int sh_type
,
4522 const unsigned char* prelocs
,
4524 Output_section
* output_section
,
4525 bool needs_special_offset_handling
,
4526 size_t local_symbol_count
,
4527 const unsigned char* plocal_symbols
,
4528 Relocatable_relocs
* rr
)
4530 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
4532 typedef gold::Default_scan_relocatable_relocs
<Classify_reloc
>
4533 Scan_relocatable_relocs
;
4535 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4537 gold::scan_relocatable_relocs
<size
, false, Scan_relocatable_relocs
>(
4545 needs_special_offset_handling
,
4551 // Scan the relocs for --emit-relocs.
4555 Target_x86_64
<size
>::emit_relocs_scan(
4556 Symbol_table
* symtab
,
4558 Sized_relobj_file
<size
, false>* object
,
4559 unsigned int data_shndx
,
4560 unsigned int sh_type
,
4561 const unsigned char* prelocs
,
4563 Output_section
* output_section
,
4564 bool needs_special_offset_handling
,
4565 size_t local_symbol_count
,
4566 const unsigned char* plocal_syms
,
4567 Relocatable_relocs
* rr
)
4569 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
4571 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
4572 Emit_relocs_strategy
;
4574 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4576 gold::scan_relocatable_relocs
<size
, false, Emit_relocs_strategy
>(
4584 needs_special_offset_handling
,
4590 // Relocate a section during a relocatable link.
4594 Target_x86_64
<size
>::relocate_relocs(
4595 const Relocate_info
<size
, false>* relinfo
,
4596 unsigned int sh_type
,
4597 const unsigned char* prelocs
,
4599 Output_section
* output_section
,
4600 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
4601 unsigned char* view
,
4602 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
4603 section_size_type view_size
,
4604 unsigned char* reloc_view
,
4605 section_size_type reloc_view_size
)
4607 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
4610 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4612 gold::relocate_relocs
<size
, false, Classify_reloc
>(
4617 offset_in_output_section
,
4625 // Return the value to use for a dynamic which requires special
4626 // treatment. This is how we support equality comparisons of function
4627 // pointers across shared library boundaries, as described in the
4628 // processor specific ABI supplement.
4632 Target_x86_64
<size
>::do_dynsym_value(const Symbol
* gsym
) const
4634 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
4635 return this->plt_address_for_global(gsym
);
4638 // Return a string used to fill a code section with nops to take up
4639 // the specified length.
4643 Target_x86_64
<size
>::do_code_fill(section_size_type length
) const
4647 // Build a jmpq instruction to skip over the bytes.
4648 unsigned char jmp
[5];
4650 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
4651 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
4652 + std::string(length
- 5, static_cast<char>(0x90)));
4655 // Nop sequences of various lengths.
4656 const char nop1
[1] = { '\x90' }; // nop
4657 const char nop2
[2] = { '\x66', '\x90' }; // xchg %ax %ax
4658 const char nop3
[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4659 const char nop4
[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4661 const char nop5
[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4663 const char nop6
[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4664 '\x44', '\x00', '\x00' };
4665 const char nop7
[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4666 '\x00', '\x00', '\x00',
4668 const char nop8
[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4669 '\x00', '\x00', '\x00',
4671 const char nop9
[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4672 '\x84', '\x00', '\x00',
4673 '\x00', '\x00', '\x00' };
4674 const char nop10
[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4675 '\x1f', '\x84', '\x00',
4676 '\x00', '\x00', '\x00',
4678 const char nop11
[11] = { '\x66', '\x66', '\x2e', // data16
4679 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4680 '\x00', '\x00', '\x00',
4682 const char nop12
[12] = { '\x66', '\x66', '\x66', // data16; data16
4683 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4684 '\x84', '\x00', '\x00',
4685 '\x00', '\x00', '\x00' };
4686 const char nop13
[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4687 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4688 '\x1f', '\x84', '\x00',
4689 '\x00', '\x00', '\x00',
4691 const char nop14
[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4692 '\x66', '\x66', '\x2e', // data16
4693 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4694 '\x00', '\x00', '\x00',
4696 const char nop15
[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4697 '\x66', '\x66', '\x66', // data16; data16
4698 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4699 '\x84', '\x00', '\x00',
4700 '\x00', '\x00', '\x00' };
4702 const char* nops
[16] = {
4704 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
4705 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
4708 return std::string(nops
[length
], length
);
4711 // Return the addend to use for a target specific relocation. The
4712 // only target specific relocation is R_X86_64_TLSDESC for a local
4713 // symbol. We want to set the addend is the offset of the local
4714 // symbol in the TLS segment.
4718 Target_x86_64
<size
>::do_reloc_addend(void* arg
, unsigned int r_type
,
4721 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
4722 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
4723 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
4724 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
4725 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
4726 gold_assert(psymval
->is_tls_symbol());
4727 // The value of a TLS symbol is the offset in the TLS segment.
4728 return psymval
->value(ti
.object
, 0);
4731 // Return the value to use for the base of a DW_EH_PE_datarel offset
4732 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4733 // assembler can not write out the difference between two labels in
4734 // different sections, so instead of using a pc-relative value they
4735 // use an offset from the GOT.
4739 Target_x86_64
<size
>::do_ehframe_datarel_base() const
4741 gold_assert(this->global_offset_table_
!= NULL
);
4742 Symbol
* sym
= this->global_offset_table_
;
4743 Sized_symbol
<size
>* ssym
= static_cast<Sized_symbol
<size
>*>(sym
);
4744 return ssym
->value();
4747 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4748 // compiled with -fsplit-stack. The function calls non-split-stack
4749 // code. We have to change the function so that it always ensures
4750 // that it has enough stack space to run some random function.
4752 static const unsigned char cmp_insn_32
[] = { 0x64, 0x3b, 0x24, 0x25 };
4753 static const unsigned char lea_r10_insn_32
[] = { 0x44, 0x8d, 0x94, 0x24 };
4754 static const unsigned char lea_r11_insn_32
[] = { 0x44, 0x8d, 0x9c, 0x24 };
4756 static const unsigned char cmp_insn_64
[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
4757 static const unsigned char lea_r10_insn_64
[] = { 0x4c, 0x8d, 0x94, 0x24 };
4758 static const unsigned char lea_r11_insn_64
[] = { 0x4c, 0x8d, 0x9c, 0x24 };
4762 Target_x86_64
<size
>::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
4763 section_offset_type fnoffset
,
4764 section_size_type fnsize
,
4765 const unsigned char*,
4767 unsigned char* view
,
4768 section_size_type view_size
,
4770 std::string
* to
) const
4772 const char* const cmp_insn
= reinterpret_cast<const char*>
4773 (size
== 32 ? cmp_insn_32
: cmp_insn_64
);
4774 const char* const lea_r10_insn
= reinterpret_cast<const char*>
4775 (size
== 32 ? lea_r10_insn_32
: lea_r10_insn_64
);
4776 const char* const lea_r11_insn
= reinterpret_cast<const char*>
4777 (size
== 32 ? lea_r11_insn_32
: lea_r11_insn_64
);
4779 const size_t cmp_insn_len
=
4780 (size
== 32 ? sizeof(cmp_insn_32
) : sizeof(cmp_insn_64
));
4781 const size_t lea_r10_insn_len
=
4782 (size
== 32 ? sizeof(lea_r10_insn_32
) : sizeof(lea_r10_insn_64
));
4783 const size_t lea_r11_insn_len
=
4784 (size
== 32 ? sizeof(lea_r11_insn_32
) : sizeof(lea_r11_insn_64
));
4785 const size_t nop_len
= (size
== 32 ? 7 : 8);
4787 // The function starts with a comparison of the stack pointer and a
4788 // field in the TCB. This is followed by a jump.
4791 if (this->match_view(view
, view_size
, fnoffset
, cmp_insn
, cmp_insn_len
)
4792 && fnsize
> nop_len
+ 1)
4794 // We will call __morestack if the carry flag is set after this
4795 // comparison. We turn the comparison into an stc instruction
4797 view
[fnoffset
] = '\xf9';
4798 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, nop_len
);
4800 // lea NN(%rsp),%r10
4801 // lea NN(%rsp),%r11
4802 else if ((this->match_view(view
, view_size
, fnoffset
,
4803 lea_r10_insn
, lea_r10_insn_len
)
4804 || this->match_view(view
, view_size
, fnoffset
,
4805 lea_r11_insn
, lea_r11_insn_len
))
4808 // This is loading an offset from the stack pointer for a
4809 // comparison. The offset is negative, so we decrease the
4810 // offset by the amount of space we need for the stack. This
4811 // means we will avoid calling __morestack if there happens to
4812 // be plenty of space on the stack already.
4813 unsigned char* pval
= view
+ fnoffset
+ 4;
4814 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
4815 val
-= parameters
->options().split_stack_adjust_size();
4816 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
4820 if (!object
->has_no_split_stack())
4821 object
->error(_("failed to match split-stack sequence at "
4822 "section %u offset %0zx"),
4823 shndx
, static_cast<size_t>(fnoffset
));
4827 // We have to change the function so that it calls
4828 // __morestack_non_split instead of __morestack. The former will
4829 // allocate additional stack space.
4830 *from
= "__morestack";
4831 *to
= "__morestack_non_split";
4834 // The selector for x86_64 object files. Note this is never instantiated
4835 // directly. It's only used in Target_selector_x86_64_nacl, below.
4838 class Target_selector_x86_64
: public Target_selector_freebsd
4841 Target_selector_x86_64()
4842 : Target_selector_freebsd(elfcpp::EM_X86_64
, size
, false,
4844 ? "elf64-x86-64" : "elf32-x86-64"),
4846 ? "elf64-x86-64-freebsd"
4847 : "elf32-x86-64-freebsd"),
4848 (size
== 64 ? "elf_x86_64" : "elf32_x86_64"))
4852 do_instantiate_target()
4853 { return new Target_x86_64
<size
>(); }
4857 // NaCl variant. It uses different PLT contents.
4860 class Output_data_plt_x86_64_nacl
: public Output_data_plt_x86_64
<size
>
4863 Output_data_plt_x86_64_nacl(Layout
* layout
,
4864 Output_data_got
<64, false>* got
,
4865 Output_data_got_plt_x86_64
* got_plt
,
4866 Output_data_space
* got_irelative
)
4867 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4868 got
, got_plt
, got_irelative
)
4871 Output_data_plt_x86_64_nacl(Layout
* layout
,
4872 Output_data_got
<64, false>* got
,
4873 Output_data_got_plt_x86_64
* got_plt
,
4874 Output_data_space
* got_irelative
,
4875 unsigned int plt_count
)
4876 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4877 got
, got_plt
, got_irelative
,
4882 virtual unsigned int
4883 do_get_plt_entry_size() const
4884 { return plt_entry_size
; }
4887 do_add_eh_frame(Layout
* layout
)
4889 layout
->add_eh_frame_for_plt(this,
4890 this->plt_eh_frame_cie
,
4891 this->plt_eh_frame_cie_size
,
4893 plt_eh_frame_fde_size
);
4897 do_fill_first_plt_entry(unsigned char* pov
,
4898 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
4899 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
4901 virtual unsigned int
4902 do_fill_plt_entry(unsigned char* pov
,
4903 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4904 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4905 unsigned int got_offset
,
4906 unsigned int plt_offset
,
4907 unsigned int plt_index
);
4910 do_fill_tlsdesc_entry(unsigned char* pov
,
4911 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4912 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4913 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
4914 unsigned int tlsdesc_got_offset
,
4915 unsigned int plt_offset
);
4918 // The size of an entry in the PLT.
4919 static const int plt_entry_size
= 64;
4921 // The first entry in the PLT.
4922 static const unsigned char first_plt_entry
[plt_entry_size
];
4924 // Other entries in the PLT for an executable.
4925 static const unsigned char plt_entry
[plt_entry_size
];
4927 // The reserved TLSDESC entry in the PLT for an executable.
4928 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
4930 // The .eh_frame unwind information for the PLT.
4931 static const int plt_eh_frame_fde_size
= 32;
4932 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
4936 class Target_x86_64_nacl
: public Target_x86_64
<size
>
4939 Target_x86_64_nacl()
4940 : Target_x86_64
<size
>(&x86_64_nacl_info
)
4943 virtual Output_data_plt_x86_64
<size
>*
4944 do_make_data_plt(Layout
* layout
,
4945 Output_data_got
<64, false>* got
,
4946 Output_data_got_plt_x86_64
* got_plt
,
4947 Output_data_space
* got_irelative
)
4949 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4953 virtual Output_data_plt_x86_64
<size
>*
4954 do_make_data_plt(Layout
* layout
,
4955 Output_data_got
<64, false>* got
,
4956 Output_data_got_plt_x86_64
* got_plt
,
4957 Output_data_space
* got_irelative
,
4958 unsigned int plt_count
)
4960 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4966 do_code_fill(section_size_type length
) const;
4969 static const Target::Target_info x86_64_nacl_info
;
4973 const Target::Target_info Target_x86_64_nacl
<64>::x86_64_nacl_info
=
4976 false, // is_big_endian
4977 elfcpp::EM_X86_64
, // machine_code
4978 false, // has_make_symbol
4979 false, // has_resolve
4980 true, // has_code_fill
4981 true, // is_default_stack_executable
4982 true, // can_icf_inline_merge_sections
4984 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4985 0x20000, // default_text_segment_address
4986 0x10000, // abi_pagesize (overridable by -z max-page-size)
4987 0x10000, // common_pagesize (overridable by -z common-page-size)
4988 true, // isolate_execinstr
4989 0x10000000, // rosegment_gap
4990 elfcpp::SHN_UNDEF
, // small_common_shndx
4991 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
4992 0, // small_common_section_flags
4993 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
4994 NULL
, // attributes_section
4995 NULL
, // attributes_vendor
4996 "_start", // entry_symbol_name
4997 32, // hash_entry_size
5001 const Target::Target_info Target_x86_64_nacl
<32>::x86_64_nacl_info
=
5004 false, // is_big_endian
5005 elfcpp::EM_X86_64
, // machine_code
5006 false, // has_make_symbol
5007 false, // has_resolve
5008 true, // has_code_fill
5009 true, // is_default_stack_executable
5010 true, // can_icf_inline_merge_sections
5012 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
5013 0x20000, // default_text_segment_address
5014 0x10000, // abi_pagesize (overridable by -z max-page-size)
5015 0x10000, // common_pagesize (overridable by -z common-page-size)
5016 true, // isolate_execinstr
5017 0x10000000, // rosegment_gap
5018 elfcpp::SHN_UNDEF
, // small_common_shndx
5019 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
5020 0, // small_common_section_flags
5021 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
5022 NULL
, // attributes_section
5023 NULL
, // attributes_vendor
5024 "_start", // entry_symbol_name
5025 32, // hash_entry_size
5028 #define NACLMASK 0xe0 // 32-byte alignment mask.
5030 // The first entry in the PLT.
5034 Output_data_plt_x86_64_nacl
<size
>::first_plt_entry
[plt_entry_size
] =
5036 0xff, 0x35, // pushq contents of memory address
5037 0, 0, 0, 0, // replaced with address of .got + 8
5038 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
5039 0, 0, 0, 0, // replaced with address of .got + 16
5040 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
5041 0x4d, 0x01, 0xfb, // add %r15, %r11
5042 0x41, 0xff, 0xe3, // jmpq *%r11
5044 // 9-byte nop sequence to pad out to the next 32-byte boundary.
5045 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
5047 // 32 bytes of nop to pad out to the standard size
5048 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5049 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5050 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5051 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5052 0x66, // excess data32 prefix
5058 Output_data_plt_x86_64_nacl
<size
>::do_fill_first_plt_entry(
5060 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5061 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
5063 memcpy(pov
, first_plt_entry
, plt_entry_size
);
5064 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
5066 - (plt_address
+ 2 + 4)));
5067 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
5069 - (plt_address
+ 9 + 4)));
5072 // Subsequent entries in the PLT.
5076 Output_data_plt_x86_64_nacl
<size
>::plt_entry
[plt_entry_size
] =
5078 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
5079 0, 0, 0, 0, // replaced with address of symbol in .got
5080 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
5081 0x4d, 0x01, 0xfb, // add %r15, %r11
5082 0x41, 0xff, 0xe3, // jmpq *%r11
5084 // 15-byte nop sequence to pad out to the next 32-byte boundary.
5085 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5086 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5088 // Lazy GOT entries point here (32-byte aligned).
5089 0x68, // pushq immediate
5090 0, 0, 0, 0, // replaced with index into relocation table
5091 0xe9, // jmp relative
5092 0, 0, 0, 0, // replaced with offset to start of .plt0
5094 // 22 bytes of nop to pad out to the standard size.
5095 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5096 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5097 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
5102 Output_data_plt_x86_64_nacl
<size
>::do_fill_plt_entry(
5104 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5105 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
5106 unsigned int got_offset
,
5107 unsigned int plt_offset
,
5108 unsigned int plt_index
)
5110 memcpy(pov
, plt_entry
, plt_entry_size
);
5111 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3,
5112 (got_address
+ got_offset
5113 - (plt_address
+ plt_offset
5116 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 33, plt_index
);
5117 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 38,
5118 - (plt_offset
+ 38 + 4));
5123 // The reserved TLSDESC entry in the PLT.
5127 Output_data_plt_x86_64_nacl
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
5129 0xff, 0x35, // pushq x(%rip)
5130 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
5131 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
5132 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
5133 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
5134 0x4d, 0x01, 0xfb, // add %r15, %r11
5135 0x41, 0xff, 0xe3, // jmpq *%r11
5137 // 41 bytes of nop to pad out to the standard size.
5138 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5139 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5140 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
5141 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5142 0x66, 0x66, // excess data32 prefixes
5143 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5148 Output_data_plt_x86_64_nacl
<size
>::do_fill_tlsdesc_entry(
5150 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
5151 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
5152 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
5153 unsigned int tlsdesc_got_offset
,
5154 unsigned int plt_offset
)
5156 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
5157 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
5159 - (plt_address
+ plt_offset
5161 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
5163 + tlsdesc_got_offset
5164 - (plt_address
+ plt_offset
5168 // The .eh_frame unwind information for the PLT.
5172 Output_data_plt_x86_64_nacl
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
5174 0, 0, 0, 0, // Replaced with offset to .plt.
5175 0, 0, 0, 0, // Replaced with size of .plt.
5176 0, // Augmentation size.
5177 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
5178 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
5179 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
5180 elfcpp::DW_CFA_advance_loc
+ 58, // Advance 58 to __PLT__ + 64.
5181 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
5182 13, // Block length.
5183 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
5184 elfcpp::DW_OP_breg16
, 0, // Push %rip.
5185 elfcpp::DW_OP_const1u
, 63, // Push 0x3f.
5186 elfcpp::DW_OP_and
, // & (%rip & 0x3f).
5187 elfcpp::DW_OP_const1u
, 37, // Push 0x25.
5188 elfcpp::DW_OP_ge
, // >= ((%rip & 0x3f) >= 0x25)
5189 elfcpp::DW_OP_lit3
, // Push 3.
5190 elfcpp::DW_OP_shl
, // << (((%rip & 0x3f) >= 0x25) << 3)
5191 elfcpp::DW_OP_plus
, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5192 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
5196 // Return a string used to fill a code section with nops.
5197 // For NaCl, long NOPs are only valid if they do not cross
5198 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5201 Target_x86_64_nacl
<size
>::do_code_fill(section_size_type length
) const
5203 return std::string(length
, static_cast<char>(0x90));
5206 // The selector for x86_64-nacl object files.
5209 class Target_selector_x86_64_nacl
5210 : public Target_selector_nacl
<Target_selector_x86_64
<size
>,
5211 Target_x86_64_nacl
<size
> >
5214 Target_selector_x86_64_nacl()
5215 : Target_selector_nacl
<Target_selector_x86_64
<size
>,
5216 Target_x86_64_nacl
<size
> >("x86-64",
5218 ? "elf64-x86-64-nacl"
5219 : "elf32-x86-64-nacl",
5222 : "elf32_x86_64_nacl")
5226 Target_selector_x86_64_nacl
<64> target_selector_x86_64
;
5227 Target_selector_x86_64_nacl
<32> target_selector_x32
;
5229 } // End anonymous namespace.