1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2015 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
];
406 // The x86_64 target class.
408 // http://www.x86-64.org/documentation/abi.pdf
409 // TLS info comes from
410 // http://people.redhat.com/drepper/tls.pdf
411 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
414 class Target_x86_64
: public Sized_target
<size
, false>
417 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
418 // uses only Elf64_Rela relocation entries with explicit addends."
419 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
421 Target_x86_64(const Target::Target_info
* info
= &x86_64_info
)
422 : Sized_target
<size
, false>(info
),
423 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_irelative_(NULL
),
424 got_tlsdesc_(NULL
), global_offset_table_(NULL
), rela_dyn_(NULL
),
425 rela_irelative_(NULL
), copy_relocs_(elfcpp::R_X86_64_COPY
),
426 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
427 tls_base_symbol_defined_(false)
430 // Hook for a new output section.
432 do_new_output_section(Output_section
*) const;
434 // Scan the relocations to look for symbol adjustments.
436 gc_process_relocs(Symbol_table
* symtab
,
438 Sized_relobj_file
<size
, false>* object
,
439 unsigned int data_shndx
,
440 unsigned int sh_type
,
441 const unsigned char* prelocs
,
443 Output_section
* output_section
,
444 bool needs_special_offset_handling
,
445 size_t local_symbol_count
,
446 const unsigned char* plocal_symbols
);
448 // Scan the relocations to look for symbol adjustments.
450 scan_relocs(Symbol_table
* symtab
,
452 Sized_relobj_file
<size
, false>* object
,
453 unsigned int data_shndx
,
454 unsigned int sh_type
,
455 const unsigned char* prelocs
,
457 Output_section
* output_section
,
458 bool needs_special_offset_handling
,
459 size_t local_symbol_count
,
460 const unsigned char* plocal_symbols
);
462 // Finalize the sections.
464 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
466 // Return the value to use for a dynamic which requires special
469 do_dynsym_value(const Symbol
*) const;
471 // Relocate a section.
473 relocate_section(const Relocate_info
<size
, false>*,
474 unsigned int sh_type
,
475 const unsigned char* prelocs
,
477 Output_section
* output_section
,
478 bool needs_special_offset_handling
,
480 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
481 section_size_type view_size
,
482 const Reloc_symbol_changes
*);
484 // Scan the relocs during a relocatable link.
486 scan_relocatable_relocs(Symbol_table
* symtab
,
488 Sized_relobj_file
<size
, false>* object
,
489 unsigned int data_shndx
,
490 unsigned int sh_type
,
491 const unsigned char* prelocs
,
493 Output_section
* output_section
,
494 bool needs_special_offset_handling
,
495 size_t local_symbol_count
,
496 const unsigned char* plocal_symbols
,
497 Relocatable_relocs
*);
499 // Emit relocations for a section.
502 const Relocate_info
<size
, false>*,
503 unsigned int sh_type
,
504 const unsigned char* prelocs
,
506 Output_section
* output_section
,
507 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
509 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
510 section_size_type view_size
,
511 unsigned char* reloc_view
,
512 section_size_type reloc_view_size
);
514 // Return a string used to fill a code section with nops.
516 do_code_fill(section_size_type length
) const;
518 // Return whether SYM is defined by the ABI.
520 do_is_defined_by_abi(const Symbol
* sym
) const
521 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
523 // Return the symbol index to use for a target specific relocation.
524 // The only target specific relocation is R_X86_64_TLSDESC for a
525 // local symbol, which is an absolute reloc.
527 do_reloc_symbol_index(void*, unsigned int r_type
) const
529 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
533 // Return the addend to use for a target specific relocation.
535 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
537 // Return the PLT section.
539 do_plt_address_for_global(const Symbol
* gsym
) const
540 { return this->plt_section()->address_for_global(gsym
); }
543 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
544 { return this->plt_section()->address_for_local(relobj
, symndx
); }
546 // This function should be defined in targets that can use relocation
547 // types to determine (implemented in local_reloc_may_be_function_pointer
548 // and global_reloc_may_be_function_pointer)
549 // if a function's pointer is taken. ICF uses this in safe mode to only
550 // fold those functions whose pointer is defintely not taken. For x86_64
551 // pie binaries, safe ICF cannot be done by looking at relocation types.
553 do_can_check_for_function_pointers() const
554 { return !parameters
->options().pie(); }
556 // Return the base for a DW_EH_PE_datarel encoding.
558 do_ehframe_datarel_base() const;
560 // Adjust -fsplit-stack code which calls non-split-stack code.
562 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
563 section_offset_type fnoffset
, section_size_type fnsize
,
564 unsigned char* view
, section_size_type view_size
,
565 std::string
* from
, std::string
* to
) const;
567 // Return the size of the GOT section.
571 gold_assert(this->got_
!= NULL
);
572 return this->got_
->data_size();
575 // Return the number of entries in the GOT.
577 got_entry_count() const
579 if (this->got_
== NULL
)
581 return this->got_size() / 8;
584 // Return the number of entries in the PLT.
586 plt_entry_count() const;
588 // Return the offset of the first non-reserved PLT entry.
590 first_plt_entry_offset() const;
592 // Return the size of each PLT entry.
594 plt_entry_size() const;
596 // Return the size of each GOT entry.
598 got_entry_size() const
601 // Create the GOT section for an incremental update.
602 Output_data_got_base
*
603 init_got_plt_for_update(Symbol_table
* symtab
,
605 unsigned int got_count
,
606 unsigned int plt_count
);
608 // Reserve a GOT entry for a local symbol, and regenerate any
609 // necessary dynamic relocations.
611 reserve_local_got_entry(unsigned int got_index
,
612 Sized_relobj
<size
, false>* obj
,
614 unsigned int got_type
);
616 // Reserve a GOT entry for a global symbol, and regenerate any
617 // necessary dynamic relocations.
619 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
620 unsigned int got_type
);
622 // Register an existing PLT entry for a global symbol.
624 register_global_plt_entry(Symbol_table
*, Layout
*, unsigned int plt_index
,
627 // Force a COPY relocation for a given symbol.
629 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
631 // Apply an incremental relocation.
633 apply_relocation(const Relocate_info
<size
, false>* relinfo
,
634 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
636 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
639 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
640 section_size_type view_size
);
642 // Add a new reloc argument, returning the index in the vector.
644 add_tlsdesc_info(Sized_relobj_file
<size
, false>* object
, unsigned int r_sym
)
646 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
647 return this->tlsdesc_reloc_info_
.size() - 1;
650 Output_data_plt_x86_64
<size
>*
651 make_data_plt(Layout
* layout
,
652 Output_data_got
<64, false>* got
,
653 Output_data_got_plt_x86_64
* got_plt
,
654 Output_data_space
* got_irelative
)
656 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
659 Output_data_plt_x86_64
<size
>*
660 make_data_plt(Layout
* layout
,
661 Output_data_got
<64, false>* got
,
662 Output_data_got_plt_x86_64
* got_plt
,
663 Output_data_space
* got_irelative
,
664 unsigned int plt_count
)
666 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
,
670 virtual Output_data_plt_x86_64
<size
>*
671 do_make_data_plt(Layout
* layout
,
672 Output_data_got
<64, false>* got
,
673 Output_data_got_plt_x86_64
* got_plt
,
674 Output_data_space
* got_irelative
)
676 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
680 virtual Output_data_plt_x86_64
<size
>*
681 do_make_data_plt(Layout
* layout
,
682 Output_data_got
<64, false>* got
,
683 Output_data_got_plt_x86_64
* got_plt
,
684 Output_data_space
* got_irelative
,
685 unsigned int plt_count
)
687 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
693 // The class which scans relocations.
698 : issued_non_pic_error_(false)
702 get_reference_flags(unsigned int r_type
);
705 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
706 Sized_relobj_file
<size
, false>* object
,
707 unsigned int data_shndx
,
708 Output_section
* output_section
,
709 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
710 const elfcpp::Sym
<size
, false>& lsym
,
714 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
715 Sized_relobj_file
<size
, false>* object
,
716 unsigned int data_shndx
,
717 Output_section
* output_section
,
718 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
722 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
723 Target_x86_64
* target
,
724 Sized_relobj_file
<size
, false>* object
,
725 unsigned int data_shndx
,
726 Output_section
* output_section
,
727 const elfcpp::Rela
<size
, false>& reloc
,
729 const elfcpp::Sym
<size
, false>& lsym
);
732 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
733 Target_x86_64
* target
,
734 Sized_relobj_file
<size
, false>* object
,
735 unsigned int data_shndx
,
736 Output_section
* output_section
,
737 const elfcpp::Rela
<size
, false>& reloc
,
743 unsupported_reloc_local(Sized_relobj_file
<size
, false>*,
744 unsigned int r_type
);
747 unsupported_reloc_global(Sized_relobj_file
<size
, false>*,
748 unsigned int r_type
, Symbol
*);
751 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
754 possible_function_pointer_reloc(unsigned int r_type
);
757 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, false>*,
758 unsigned int r_type
);
760 // Whether we have issued an error about a non-PIC compilation.
761 bool issued_non_pic_error_
;
764 // The class which implements relocation.
769 : skip_call_tls_get_addr_(false)
774 if (this->skip_call_tls_get_addr_
)
776 // FIXME: This needs to specify the location somehow.
777 gold_error(_("missing expected TLS relocation"));
781 // Do a relocation. Return false if the caller should not issue
782 // any warnings about this relocation.
784 relocate(const Relocate_info
<size
, false>*, unsigned int,
785 Target_x86_64
*, Output_section
*, size_t, const unsigned char*,
786 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
787 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
791 // Do a TLS relocation.
793 relocate_tls(const Relocate_info
<size
, false>*, Target_x86_64
*,
794 size_t relnum
, const elfcpp::Rela
<size
, false>&,
795 unsigned int r_type
, const Sized_symbol
<size
>*,
796 const Symbol_value
<size
>*,
797 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
800 // Do a TLS General-Dynamic to Initial-Exec transition.
802 tls_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
803 Output_segment
* tls_segment
,
804 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
805 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
807 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
808 section_size_type view_size
);
810 // Do a TLS General-Dynamic to Local-Exec transition.
812 tls_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
813 Output_segment
* tls_segment
,
814 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
815 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
817 section_size_type view_size
);
819 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
821 tls_desc_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
822 Output_segment
* tls_segment
,
823 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
824 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
826 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
827 section_size_type view_size
);
829 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
831 tls_desc_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
832 Output_segment
* tls_segment
,
833 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
834 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
836 section_size_type view_size
);
838 // Do a TLS Local-Dynamic to Local-Exec transition.
840 tls_ld_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
841 Output_segment
* tls_segment
,
842 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
843 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
845 section_size_type view_size
);
847 // Do a TLS Initial-Exec to Local-Exec transition.
849 tls_ie_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
850 Output_segment
* tls_segment
,
851 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
852 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
854 section_size_type view_size
);
856 // This is set if we should skip the next reloc, which should be a
857 // PLT32 reloc against ___tls_get_addr.
858 bool skip_call_tls_get_addr_
;
861 // A class which returns the size required for a relocation type,
862 // used while scanning relocs during a relocatable link.
863 class Relocatable_size_for_reloc
867 get_size_for_reloc(unsigned int, Relobj
*);
870 // Check if relocation against this symbol is a candidate for
872 // mov foo@GOTPCREL(%rip), %reg
873 // to lea foo(%rip), %reg.
875 can_convert_mov_to_lea(const Symbol
* gsym
)
877 gold_assert(gsym
!= NULL
);
878 return (gsym
->type() != elfcpp::STT_GNU_IFUNC
879 && !gsym
->is_undefined ()
880 && !gsym
->is_from_dynobj()
881 && !gsym
->is_preemptible()
882 && (!parameters
->options().shared()
883 || (gsym
->visibility() != elfcpp::STV_DEFAULT
884 && gsym
->visibility() != elfcpp::STV_PROTECTED
)
885 || parameters
->options().Bsymbolic())
886 && strcmp(gsym
->name(), "_DYNAMIC") != 0);
889 // Adjust TLS relocation type based on the options and whether this
890 // is a local symbol.
891 static tls::Tls_optimization
892 optimize_tls_reloc(bool is_final
, int r_type
);
894 // Get the GOT section, creating it if necessary.
895 Output_data_got
<64, false>*
896 got_section(Symbol_table
*, Layout
*);
898 // Get the GOT PLT section.
899 Output_data_got_plt_x86_64
*
900 got_plt_section() const
902 gold_assert(this->got_plt_
!= NULL
);
903 return this->got_plt_
;
906 // Get the GOT section for TLSDESC entries.
907 Output_data_got
<64, false>*
908 got_tlsdesc_section() const
910 gold_assert(this->got_tlsdesc_
!= NULL
);
911 return this->got_tlsdesc_
;
914 // Create the PLT section.
916 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
918 // Create a PLT entry for a global symbol.
920 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
922 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
924 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
925 Sized_relobj_file
<size
, false>* relobj
,
926 unsigned int local_sym_index
);
928 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
930 define_tls_base_symbol(Symbol_table
*, Layout
*);
932 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
934 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
936 // Create a GOT entry for the TLS module index.
938 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
939 Sized_relobj_file
<size
, false>* object
);
941 // Get the PLT section.
942 Output_data_plt_x86_64
<size
>*
945 gold_assert(this->plt_
!= NULL
);
949 // Get the dynamic reloc section, creating it if necessary.
951 rela_dyn_section(Layout
*);
953 // Get the section to use for TLSDESC relocations.
955 rela_tlsdesc_section(Layout
*) const;
957 // Get the section to use for IRELATIVE relocations.
959 rela_irelative_section(Layout
*);
961 // Add a potential copy relocation.
963 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
964 Sized_relobj_file
<size
, false>* object
,
965 unsigned int shndx
, Output_section
* output_section
,
966 Symbol
* sym
, const elfcpp::Rela
<size
, false>& reloc
)
968 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
969 this->copy_relocs_
.copy_reloc(symtab
, layout
,
970 symtab
->get_sized_symbol
<size
>(sym
),
971 object
, shndx
, output_section
,
972 r_type
, reloc
.get_r_offset(),
973 reloc
.get_r_addend(),
974 this->rela_dyn_section(layout
));
977 // Information about this specific target which we pass to the
978 // general Target structure.
979 static const Target::Target_info x86_64_info
;
981 // The types of GOT entries needed for this platform.
982 // These values are exposed to the ABI in an incremental link.
983 // Do not renumber existing values without changing the version
984 // number of the .gnu_incremental_inputs section.
987 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
988 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
989 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
990 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
993 // This type is used as the argument to the target specific
994 // relocation routines. The only target specific reloc is
995 // R_X86_64_TLSDESC against a local symbol.
998 Tlsdesc_info(Sized_relobj_file
<size
, false>* a_object
, unsigned int a_r_sym
)
999 : object(a_object
), r_sym(a_r_sym
)
1002 // The object in which the local symbol is defined.
1003 Sized_relobj_file
<size
, false>* object
;
1004 // The local symbol index in the object.
1009 Output_data_got
<64, false>* got_
;
1011 Output_data_plt_x86_64
<size
>* plt_
;
1012 // The GOT PLT section.
1013 Output_data_got_plt_x86_64
* got_plt_
;
1014 // The GOT section for IRELATIVE relocations.
1015 Output_data_space
* got_irelative_
;
1016 // The GOT section for TLSDESC relocations.
1017 Output_data_got
<64, false>* got_tlsdesc_
;
1018 // The _GLOBAL_OFFSET_TABLE_ symbol.
1019 Symbol
* global_offset_table_
;
1020 // The dynamic reloc section.
1021 Reloc_section
* rela_dyn_
;
1022 // The section to use for IRELATIVE relocs.
1023 Reloc_section
* rela_irelative_
;
1024 // Relocs saved to avoid a COPY reloc.
1025 Copy_relocs
<elfcpp::SHT_RELA
, size
, false> copy_relocs_
;
1026 // Offset of the GOT entry for the TLS module index.
1027 unsigned int got_mod_index_offset_
;
1028 // We handle R_X86_64_TLSDESC against a local symbol as a target
1029 // specific relocation. Here we store the object and local symbol
1030 // index for the relocation.
1031 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
1032 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1033 bool tls_base_symbol_defined_
;
1037 const Target::Target_info Target_x86_64
<64>::x86_64_info
=
1040 false, // is_big_endian
1041 elfcpp::EM_X86_64
, // machine_code
1042 false, // has_make_symbol
1043 false, // has_resolve
1044 true, // has_code_fill
1045 true, // is_default_stack_executable
1046 true, // can_icf_inline_merge_sections
1048 "/lib/ld64.so.1", // program interpreter
1049 0x400000, // default_text_segment_address
1050 0x1000, // abi_pagesize (overridable by -z max-page-size)
1051 0x1000, // common_pagesize (overridable by -z common-page-size)
1052 false, // isolate_execinstr
1054 elfcpp::SHN_UNDEF
, // small_common_shndx
1055 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1056 0, // small_common_section_flags
1057 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1058 NULL
, // attributes_section
1059 NULL
, // attributes_vendor
1060 "_start", // entry_symbol_name
1061 32, // hash_entry_size
1065 const Target::Target_info Target_x86_64
<32>::x86_64_info
=
1068 false, // is_big_endian
1069 elfcpp::EM_X86_64
, // machine_code
1070 false, // has_make_symbol
1071 false, // has_resolve
1072 true, // has_code_fill
1073 true, // is_default_stack_executable
1074 true, // can_icf_inline_merge_sections
1076 "/libx32/ldx32.so.1", // program interpreter
1077 0x400000, // default_text_segment_address
1078 0x1000, // abi_pagesize (overridable by -z max-page-size)
1079 0x1000, // common_pagesize (overridable by -z common-page-size)
1080 false, // isolate_execinstr
1082 elfcpp::SHN_UNDEF
, // small_common_shndx
1083 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1084 0, // small_common_section_flags
1085 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1086 NULL
, // attributes_section
1087 NULL
, // attributes_vendor
1088 "_start", // entry_symbol_name
1089 32, // hash_entry_size
1092 // This is called when a new output section is created. This is where
1093 // we handle the SHF_X86_64_LARGE.
1097 Target_x86_64
<size
>::do_new_output_section(Output_section
* os
) const
1099 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
1100 os
->set_is_large_section();
1103 // Get the GOT section, creating it if necessary.
1106 Output_data_got
<64, false>*
1107 Target_x86_64
<size
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
1109 if (this->got_
== NULL
)
1111 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1113 // When using -z now, we can treat .got.plt as a relro section.
1114 // Without -z now, it is modified after program startup by lazy
1116 bool is_got_plt_relro
= parameters
->options().now();
1117 Output_section_order got_order
= (is_got_plt_relro
1119 : ORDER_RELRO_LAST
);
1120 Output_section_order got_plt_order
= (is_got_plt_relro
1122 : ORDER_NON_RELRO_FIRST
);
1124 this->got_
= new Output_data_got
<64, false>();
1126 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1128 | elfcpp::SHF_WRITE
),
1129 this->got_
, got_order
, true);
1131 this->got_plt_
= new Output_data_got_plt_x86_64(layout
);
1132 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1134 | elfcpp::SHF_WRITE
),
1135 this->got_plt_
, got_plt_order
,
1138 // The first three entries are reserved.
1139 this->got_plt_
->set_current_data_size(3 * 8);
1141 if (!is_got_plt_relro
)
1143 // Those bytes can go into the relro segment.
1144 layout
->increase_relro(3 * 8);
1147 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1148 this->global_offset_table_
=
1149 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1150 Symbol_table::PREDEFINED
,
1152 0, 0, elfcpp::STT_OBJECT
,
1154 elfcpp::STV_HIDDEN
, 0,
1157 // If there are any IRELATIVE relocations, they get GOT entries
1158 // in .got.plt after the jump slot entries.
1159 this->got_irelative_
= new Output_data_space(8, "** GOT IRELATIVE PLT");
1160 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1162 | elfcpp::SHF_WRITE
),
1163 this->got_irelative_
,
1164 got_plt_order
, is_got_plt_relro
);
1166 // If there are any TLSDESC relocations, they get GOT entries in
1167 // .got.plt after the jump slot and IRELATIVE entries.
1168 this->got_tlsdesc_
= new Output_data_got
<64, false>();
1169 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1171 | elfcpp::SHF_WRITE
),
1173 got_plt_order
, is_got_plt_relro
);
1179 // Get the dynamic reloc section, creating it if necessary.
1182 typename Target_x86_64
<size
>::Reloc_section
*
1183 Target_x86_64
<size
>::rela_dyn_section(Layout
* layout
)
1185 if (this->rela_dyn_
== NULL
)
1187 gold_assert(layout
!= NULL
);
1188 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1189 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1190 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1191 ORDER_DYNAMIC_RELOCS
, false);
1193 return this->rela_dyn_
;
1196 // Get the section to use for IRELATIVE relocs, creating it if
1197 // necessary. These go in .rela.dyn, but only after all other dynamic
1198 // relocations. They need to follow the other dynamic relocations so
1199 // that they can refer to global variables initialized by those
1203 typename Target_x86_64
<size
>::Reloc_section
*
1204 Target_x86_64
<size
>::rela_irelative_section(Layout
* layout
)
1206 if (this->rela_irelative_
== NULL
)
1208 // Make sure we have already created the dynamic reloc section.
1209 this->rela_dyn_section(layout
);
1210 this->rela_irelative_
= new Reloc_section(false);
1211 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1212 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
1213 ORDER_DYNAMIC_RELOCS
, false);
1214 gold_assert(this->rela_dyn_
->output_section()
1215 == this->rela_irelative_
->output_section());
1217 return this->rela_irelative_
;
1220 // Write the first three reserved words of the .got.plt section.
1221 // The remainder of the section is written while writing the PLT
1222 // in Output_data_plt_i386::do_write.
1225 Output_data_got_plt_x86_64::do_write(Output_file
* of
)
1227 // The first entry in the GOT is the address of the .dynamic section
1228 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1229 // We saved space for them when we created the section in
1230 // Target_x86_64::got_section.
1231 const off_t got_file_offset
= this->offset();
1232 gold_assert(this->data_size() >= 24);
1233 unsigned char* const got_view
= of
->get_output_view(got_file_offset
, 24);
1234 Output_section
* dynamic
= this->layout_
->dynamic_section();
1235 uint64_t dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
1236 elfcpp::Swap
<64, false>::writeval(got_view
, dynamic_addr
);
1237 memset(got_view
+ 8, 0, 16);
1238 of
->write_output_view(got_file_offset
, 24, got_view
);
1241 // Initialize the PLT section.
1245 Output_data_plt_x86_64
<size
>::init(Layout
* layout
)
1247 this->rel_
= new Reloc_section(false);
1248 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1249 elfcpp::SHF_ALLOC
, this->rel_
,
1250 ORDER_DYNAMIC_PLT_RELOCS
, false);
1255 Output_data_plt_x86_64
<size
>::do_adjust_output_section(Output_section
* os
)
1257 os
->set_entsize(this->get_plt_entry_size());
1260 // Add an entry to the PLT.
1264 Output_data_plt_x86_64
<size
>::add_entry(Symbol_table
* symtab
, Layout
* layout
,
1267 gold_assert(!gsym
->has_plt_offset());
1269 unsigned int plt_index
;
1271 section_offset_type got_offset
;
1273 unsigned int* pcount
;
1274 unsigned int offset
;
1275 unsigned int reserved
;
1276 Output_section_data_build
* got
;
1277 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1278 && gsym
->can_use_relative_reloc(false))
1280 pcount
= &this->irelative_count_
;
1283 got
= this->got_irelative_
;
1287 pcount
= &this->count_
;
1290 got
= this->got_plt_
;
1293 if (!this->is_data_size_valid())
1295 // Note that when setting the PLT offset for a non-IRELATIVE
1296 // entry we skip the initial reserved PLT entry.
1297 plt_index
= *pcount
+ offset
;
1298 plt_offset
= plt_index
* this->get_plt_entry_size();
1302 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1303 gold_assert(got_offset
== got
->current_data_size());
1305 // Every PLT entry needs a GOT entry which points back to the PLT
1306 // entry (this will be changed by the dynamic linker, normally
1307 // lazily when the function is called).
1308 got
->set_current_data_size(got_offset
+ 8);
1312 // FIXME: This is probably not correct for IRELATIVE relocs.
1314 // For incremental updates, find an available slot.
1315 plt_offset
= this->free_list_
.allocate(this->get_plt_entry_size(),
1316 this->get_plt_entry_size(), 0);
1317 if (plt_offset
== -1)
1318 gold_fallback(_("out of patch space (PLT);"
1319 " relink with --incremental-full"));
1321 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1322 // can be calculated from the PLT index, adjusting for the three
1323 // reserved entries at the beginning of the GOT.
1324 plt_index
= plt_offset
/ this->get_plt_entry_size() - 1;
1325 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1328 gsym
->set_plt_offset(plt_offset
);
1330 // Every PLT entry needs a reloc.
1331 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
1333 // Note that we don't need to save the symbol. The contents of the
1334 // PLT are independent of which symbols are used. The symbols only
1335 // appear in the relocations.
1338 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1343 Output_data_plt_x86_64
<size
>::add_local_ifunc_entry(
1344 Symbol_table
* symtab
,
1346 Sized_relobj_file
<size
, false>* relobj
,
1347 unsigned int local_sym_index
)
1349 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
1350 ++this->irelative_count_
;
1352 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
1354 // Every PLT entry needs a GOT entry which points back to the PLT
1356 this->got_irelative_
->set_current_data_size(got_offset
+ 8);
1358 // Every PLT entry needs a reloc.
1359 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1360 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
1361 elfcpp::R_X86_64_IRELATIVE
,
1362 this->got_irelative_
, got_offset
, 0);
1367 // Add the relocation for a PLT entry.
1371 Output_data_plt_x86_64
<size
>::add_relocation(Symbol_table
* symtab
,
1374 unsigned int got_offset
)
1376 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1377 && gsym
->can_use_relative_reloc(false))
1379 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1380 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
1381 this->got_irelative_
, got_offset
, 0);
1385 gsym
->set_needs_dynsym_entry();
1386 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
1391 // Return where the TLSDESC relocations should go, creating it if
1392 // necessary. These follow the JUMP_SLOT relocations.
1395 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1396 Output_data_plt_x86_64
<size
>::rela_tlsdesc(Layout
* layout
)
1398 if (this->tlsdesc_rel_
== NULL
)
1400 this->tlsdesc_rel_
= new Reloc_section(false);
1401 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1402 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
1403 ORDER_DYNAMIC_PLT_RELOCS
, false);
1404 gold_assert(this->tlsdesc_rel_
->output_section()
1405 == this->rel_
->output_section());
1407 return this->tlsdesc_rel_
;
1410 // Return where the IRELATIVE relocations should go in the PLT. These
1411 // follow the JUMP_SLOT and the TLSDESC relocations.
1414 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1415 Output_data_plt_x86_64
<size
>::rela_irelative(Symbol_table
* symtab
,
1418 if (this->irelative_rel_
== NULL
)
1420 // Make sure we have a place for the TLSDESC relocations, in
1421 // case we see any later on.
1422 this->rela_tlsdesc(layout
);
1423 this->irelative_rel_
= new Reloc_section(false);
1424 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1425 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
1426 ORDER_DYNAMIC_PLT_RELOCS
, false);
1427 gold_assert(this->irelative_rel_
->output_section()
1428 == this->rel_
->output_section());
1430 if (parameters
->doing_static_link())
1432 // A statically linked executable will only have a .rela.plt
1433 // section to hold R_X86_64_IRELATIVE relocs for
1434 // STT_GNU_IFUNC symbols. The library will use these
1435 // symbols to locate the IRELATIVE relocs at program startup
1437 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
1438 Symbol_table::PREDEFINED
,
1439 this->irelative_rel_
, 0, 0,
1440 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1441 elfcpp::STV_HIDDEN
, 0, false, true);
1442 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
1443 Symbol_table::PREDEFINED
,
1444 this->irelative_rel_
, 0, 0,
1445 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1446 elfcpp::STV_HIDDEN
, 0, true, true);
1449 return this->irelative_rel_
;
1452 // Return the PLT address to use for a global symbol.
1456 Output_data_plt_x86_64
<size
>::address_for_global(const Symbol
* gsym
)
1458 uint64_t offset
= 0;
1459 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1460 && gsym
->can_use_relative_reloc(false))
1461 offset
= (this->count_
+ 1) * this->get_plt_entry_size();
1462 return this->address() + offset
+ gsym
->plt_offset();
1465 // Return the PLT address to use for a local symbol. These are always
1466 // IRELATIVE relocs.
1470 Output_data_plt_x86_64
<size
>::address_for_local(const Relobj
* object
,
1473 return (this->address()
1474 + (this->count_
+ 1) * this->get_plt_entry_size()
1475 + object
->local_plt_offset(r_sym
));
1478 // Set the final size.
1481 Output_data_plt_x86_64
<size
>::set_final_data_size()
1483 unsigned int count
= this->count_
+ this->irelative_count_
;
1484 if (this->has_tlsdesc_entry())
1486 this->set_data_size((count
+ 1) * this->get_plt_entry_size());
1489 // The first entry in the PLT for an executable.
1493 Output_data_plt_x86_64_standard
<size
>::first_plt_entry
[plt_entry_size
] =
1495 // From AMD64 ABI Draft 0.98, page 76
1496 0xff, 0x35, // pushq contents of memory address
1497 0, 0, 0, 0, // replaced with address of .got + 8
1498 0xff, 0x25, // jmp indirect
1499 0, 0, 0, 0, // replaced with address of .got + 16
1500 0x90, 0x90, 0x90, 0x90 // noop (x4)
1505 Output_data_plt_x86_64_standard
<size
>::do_fill_first_plt_entry(
1507 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1508 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
1510 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1511 // We do a jmp relative to the PC at the end of this instruction.
1512 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1514 - (plt_address
+ 6)));
1515 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1517 - (plt_address
+ 12)));
1520 // Subsequent entries in the PLT for an executable.
1524 Output_data_plt_x86_64_standard
<size
>::plt_entry
[plt_entry_size
] =
1526 // From AMD64 ABI Draft 0.98, page 76
1527 0xff, 0x25, // jmpq indirect
1528 0, 0, 0, 0, // replaced with address of symbol in .got
1529 0x68, // pushq immediate
1530 0, 0, 0, 0, // replaced with offset into relocation table
1531 0xe9, // jmpq relative
1532 0, 0, 0, 0 // replaced with offset to start of .plt
1537 Output_data_plt_x86_64_standard
<size
>::do_fill_plt_entry(
1539 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1540 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1541 unsigned int got_offset
,
1542 unsigned int plt_offset
,
1543 unsigned int plt_index
)
1545 // Check PC-relative offset overflow in PLT entry.
1546 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
1547 - (plt_address
+ plt_offset
+ 6));
1548 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
1549 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1552 memcpy(pov
, plt_entry
, plt_entry_size
);
1553 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1554 plt_got_pcrel_offset
);
1556 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1557 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1558 - (plt_offset
+ plt_entry_size
));
1563 // The reserved TLSDESC entry in the PLT for an executable.
1567 Output_data_plt_x86_64_standard
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
1569 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1570 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1571 0xff, 0x35, // pushq x(%rip)
1572 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1573 0xff, 0x25, // jmpq *y(%rip)
1574 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1581 Output_data_plt_x86_64_standard
<size
>::do_fill_tlsdesc_entry(
1583 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1584 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1585 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
1586 unsigned int tlsdesc_got_offset
,
1587 unsigned int plt_offset
)
1589 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1590 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1592 - (plt_address
+ plt_offset
1594 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1596 + tlsdesc_got_offset
1597 - (plt_address
+ plt_offset
1601 // The .eh_frame unwind information for the PLT.
1605 Output_data_plt_x86_64
<size
>::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
1608 'z', // Augmentation: augmentation size included.
1609 'R', // Augmentation: FDE encoding included.
1610 '\0', // End of augmentation string.
1611 1, // Code alignment factor.
1612 0x78, // Data alignment factor.
1613 16, // Return address column.
1614 1, // Augmentation size.
1615 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
1616 | elfcpp::DW_EH_PE_sdata4
),
1617 elfcpp::DW_CFA_def_cfa
, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1618 elfcpp::DW_CFA_offset
+ 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1619 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
1625 Output_data_plt_x86_64_standard
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
1627 0, 0, 0, 0, // Replaced with offset to .plt.
1628 0, 0, 0, 0, // Replaced with size of .plt.
1629 0, // Augmentation size.
1630 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
1631 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
1632 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
1633 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
1634 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
1635 11, // Block length.
1636 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
1637 elfcpp::DW_OP_breg16
, 0, // Push %rip.
1638 elfcpp::DW_OP_lit15
, // Push 0xf.
1639 elfcpp::DW_OP_and
, // & (%rip & 0xf).
1640 elfcpp::DW_OP_lit11
, // Push 0xb.
1641 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 0xb)
1642 elfcpp::DW_OP_lit3
, // Push 3.
1643 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 0xb) << 3)
1644 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1645 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
1651 // Write out the PLT. This uses the hand-coded instructions above,
1652 // and adjusts them as needed. This is specified by the AMD64 ABI.
1656 Output_data_plt_x86_64
<size
>::do_write(Output_file
* of
)
1658 const off_t offset
= this->offset();
1659 const section_size_type oview_size
=
1660 convert_to_section_size_type(this->data_size());
1661 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1663 const off_t got_file_offset
= this->got_plt_
->offset();
1664 gold_assert(parameters
->incremental_update()
1665 || (got_file_offset
+ this->got_plt_
->data_size()
1666 == this->got_irelative_
->offset()));
1667 const section_size_type got_size
=
1668 convert_to_section_size_type(this->got_plt_
->data_size()
1669 + this->got_irelative_
->data_size());
1670 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1673 unsigned char* pov
= oview
;
1675 // The base address of the .plt section.
1676 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
1677 // The base address of the .got section.
1678 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
= this->got_
->address();
1679 // The base address of the PLT portion of the .got section,
1680 // which is where the GOT pointer will point, and where the
1681 // three reserved GOT entries are located.
1682 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
1683 = this->got_plt_
->address();
1685 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
1686 pov
+= this->get_plt_entry_size();
1688 // The first three entries in the GOT are reserved, and are written
1689 // by Output_data_got_plt_x86_64::do_write.
1690 unsigned char* got_pov
= got_view
+ 24;
1692 unsigned int plt_offset
= this->get_plt_entry_size();
1693 unsigned int got_offset
= 24;
1694 const unsigned int count
= this->count_
+ this->irelative_count_
;
1695 for (unsigned int plt_index
= 0;
1698 pov
+= this->get_plt_entry_size(),
1700 plt_offset
+= this->get_plt_entry_size(),
1703 // Set and adjust the PLT entry itself.
1704 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
1705 got_address
, plt_address
,
1706 got_offset
, plt_offset
,
1709 // Set the entry in the GOT.
1710 elfcpp::Swap
<64, false>::writeval(got_pov
,
1711 plt_address
+ plt_offset
+ lazy_offset
);
1714 if (this->has_tlsdesc_entry())
1716 // Set and adjust the reserved TLSDESC PLT entry.
1717 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
1718 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
1719 tlsdesc_got_offset
, plt_offset
);
1720 pov
+= this->get_plt_entry_size();
1723 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1724 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1726 of
->write_output_view(offset
, oview_size
, oview
);
1727 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1730 // Create the PLT section.
1734 Target_x86_64
<size
>::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1736 if (this->plt_
== NULL
)
1738 // Create the GOT sections first.
1739 this->got_section(symtab
, layout
);
1741 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
1742 this->got_irelative_
);
1744 // Add unwind information if requested.
1745 if (parameters
->options().ld_generated_unwind_info())
1746 this->plt_
->add_eh_frame(layout
);
1748 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1750 | elfcpp::SHF_EXECINSTR
),
1751 this->plt_
, ORDER_PLT
, false);
1753 // Make the sh_info field of .rela.plt point to .plt.
1754 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1755 rela_plt_os
->set_info_section(this->plt_
->output_section());
1759 // Return the section for TLSDESC relocations.
1762 typename Target_x86_64
<size
>::Reloc_section
*
1763 Target_x86_64
<size
>::rela_tlsdesc_section(Layout
* layout
) const
1765 return this->plt_section()->rela_tlsdesc(layout
);
1768 // Create a PLT entry for a global symbol.
1772 Target_x86_64
<size
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1775 if (gsym
->has_plt_offset())
1778 if (this->plt_
== NULL
)
1779 this->make_plt_section(symtab
, layout
);
1781 this->plt_
->add_entry(symtab
, layout
, gsym
);
1784 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1788 Target_x86_64
<size
>::make_local_ifunc_plt_entry(
1789 Symbol_table
* symtab
, Layout
* layout
,
1790 Sized_relobj_file
<size
, false>* relobj
,
1791 unsigned int local_sym_index
)
1793 if (relobj
->local_has_plt_offset(local_sym_index
))
1795 if (this->plt_
== NULL
)
1796 this->make_plt_section(symtab
, layout
);
1797 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
1800 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1803 // Return the number of entries in the PLT.
1807 Target_x86_64
<size
>::plt_entry_count() const
1809 if (this->plt_
== NULL
)
1811 return this->plt_
->entry_count();
1814 // Return the offset of the first non-reserved PLT entry.
1818 Target_x86_64
<size
>::first_plt_entry_offset() const
1820 return this->plt_
->first_plt_entry_offset();
1823 // Return the size of each PLT entry.
1827 Target_x86_64
<size
>::plt_entry_size() const
1829 return this->plt_
->get_plt_entry_size();
1832 // Create the GOT and PLT sections for an incremental update.
1835 Output_data_got_base
*
1836 Target_x86_64
<size
>::init_got_plt_for_update(Symbol_table
* symtab
,
1838 unsigned int got_count
,
1839 unsigned int plt_count
)
1841 gold_assert(this->got_
== NULL
);
1843 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
1844 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1846 | elfcpp::SHF_WRITE
),
1847 this->got_
, ORDER_RELRO_LAST
,
1850 // Add the three reserved entries.
1851 this->got_plt_
= new Output_data_got_plt_x86_64(layout
, (plt_count
+ 3) * 8);
1852 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1854 | elfcpp::SHF_WRITE
),
1855 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
1858 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1859 this->global_offset_table_
=
1860 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1861 Symbol_table::PREDEFINED
,
1863 0, 0, elfcpp::STT_OBJECT
,
1865 elfcpp::STV_HIDDEN
, 0,
1868 // If there are any TLSDESC relocations, they get GOT entries in
1869 // .got.plt after the jump slot entries.
1870 // FIXME: Get the count for TLSDESC entries.
1871 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
1872 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1873 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1875 ORDER_NON_RELRO_FIRST
, false);
1877 // If there are any IRELATIVE relocations, they get GOT entries in
1878 // .got.plt after the jump slot and TLSDESC entries.
1879 this->got_irelative_
= new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1880 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1881 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1882 this->got_irelative_
,
1883 ORDER_NON_RELRO_FIRST
, false);
1885 // Create the PLT section.
1886 this->plt_
= this->make_data_plt(layout
, this->got_
,
1888 this->got_irelative_
,
1891 // Add unwind information if requested.
1892 if (parameters
->options().ld_generated_unwind_info())
1893 this->plt_
->add_eh_frame(layout
);
1895 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1896 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1897 this->plt_
, ORDER_PLT
, false);
1899 // Make the sh_info field of .rela.plt point to .plt.
1900 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1901 rela_plt_os
->set_info_section(this->plt_
->output_section());
1903 // Create the rela_dyn section.
1904 this->rela_dyn_section(layout
);
1909 // Reserve a GOT entry for a local symbol, and regenerate any
1910 // necessary dynamic relocations.
1914 Target_x86_64
<size
>::reserve_local_got_entry(
1915 unsigned int got_index
,
1916 Sized_relobj
<size
, false>* obj
,
1918 unsigned int got_type
)
1920 unsigned int got_offset
= got_index
* 8;
1921 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1923 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
1926 case GOT_TYPE_STANDARD
:
1927 if (parameters
->options().output_is_position_independent())
1928 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
1929 this->got_
, got_offset
, 0, false);
1931 case GOT_TYPE_TLS_OFFSET
:
1932 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
1933 this->got_
, got_offset
, 0);
1935 case GOT_TYPE_TLS_PAIR
:
1936 this->got_
->reserve_slot(got_index
+ 1);
1937 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
1938 this->got_
, got_offset
, 0);
1940 case GOT_TYPE_TLS_DESC
:
1941 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1942 // this->got_->reserve_slot(got_index + 1);
1943 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1944 // this->got_, got_offset, 0);
1951 // Reserve a GOT entry for a global symbol, and regenerate any
1952 // necessary dynamic relocations.
1956 Target_x86_64
<size
>::reserve_global_got_entry(unsigned int got_index
,
1958 unsigned int got_type
)
1960 unsigned int got_offset
= got_index
* 8;
1961 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1963 this->got_
->reserve_global(got_index
, gsym
, got_type
);
1966 case GOT_TYPE_STANDARD
:
1967 if (!gsym
->final_value_is_known())
1969 if (gsym
->is_from_dynobj()
1970 || gsym
->is_undefined()
1971 || gsym
->is_preemptible()
1972 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1973 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
1974 this->got_
, got_offset
, 0);
1976 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1977 this->got_
, got_offset
, 0, false);
1980 case GOT_TYPE_TLS_OFFSET
:
1981 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
1982 this->got_
, got_offset
, 0, false);
1984 case GOT_TYPE_TLS_PAIR
:
1985 this->got_
->reserve_slot(got_index
+ 1);
1986 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
1987 this->got_
, got_offset
, 0, false);
1988 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
1989 this->got_
, got_offset
+ 8, 0, false);
1991 case GOT_TYPE_TLS_DESC
:
1992 this->got_
->reserve_slot(got_index
+ 1);
1993 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
1994 this->got_
, got_offset
, 0, false);
2001 // Register an existing PLT entry for a global symbol.
2005 Target_x86_64
<size
>::register_global_plt_entry(Symbol_table
* symtab
,
2007 unsigned int plt_index
,
2010 gold_assert(this->plt_
!= NULL
);
2011 gold_assert(!gsym
->has_plt_offset());
2013 this->plt_
->reserve_slot(plt_index
);
2015 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
2017 unsigned int got_offset
= (plt_index
+ 3) * 8;
2018 this->plt_
->add_relocation(symtab
, layout
, gsym
, got_offset
);
2021 // Force a COPY relocation for a given symbol.
2025 Target_x86_64
<size
>::emit_copy_reloc(
2026 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
2028 this->copy_relocs_
.emit_copy_reloc(symtab
,
2029 symtab
->get_sized_symbol
<size
>(sym
),
2032 this->rela_dyn_section(NULL
));
2035 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2039 Target_x86_64
<size
>::define_tls_base_symbol(Symbol_table
* symtab
,
2042 if (this->tls_base_symbol_defined_
)
2045 Output_segment
* tls_segment
= layout
->tls_segment();
2046 if (tls_segment
!= NULL
)
2048 bool is_exec
= parameters
->options().output_is_executable();
2049 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
2050 Symbol_table::PREDEFINED
,
2054 elfcpp::STV_HIDDEN
, 0,
2056 ? Symbol::SEGMENT_END
2057 : Symbol::SEGMENT_START
),
2060 this->tls_base_symbol_defined_
= true;
2063 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2067 Target_x86_64
<size
>::reserve_tlsdesc_entries(Symbol_table
* symtab
,
2070 if (this->plt_
== NULL
)
2071 this->make_plt_section(symtab
, layout
);
2073 if (!this->plt_
->has_tlsdesc_entry())
2075 // Allocate the TLSDESC_GOT entry.
2076 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2077 unsigned int got_offset
= got
->add_constant(0);
2079 // Allocate the TLSDESC_PLT entry.
2080 this->plt_
->reserve_tlsdesc_entry(got_offset
);
2084 // Create a GOT entry for the TLS module index.
2088 Target_x86_64
<size
>::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
2089 Sized_relobj_file
<size
, false>* object
)
2091 if (this->got_mod_index_offset_
== -1U)
2093 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
2094 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
2095 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2096 unsigned int got_offset
= got
->add_constant(0);
2097 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
2099 got
->add_constant(0);
2100 this->got_mod_index_offset_
= got_offset
;
2102 return this->got_mod_index_offset_
;
2105 // Optimize the TLS relocation type based on what we know about the
2106 // symbol. IS_FINAL is true if the final address of this symbol is
2107 // known at link time.
2110 tls::Tls_optimization
2111 Target_x86_64
<size
>::optimize_tls_reloc(bool is_final
, int r_type
)
2113 // If we are generating a shared library, then we can't do anything
2115 if (parameters
->options().shared())
2116 return tls::TLSOPT_NONE
;
2120 case elfcpp::R_X86_64_TLSGD
:
2121 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2122 case elfcpp::R_X86_64_TLSDESC_CALL
:
2123 // These are General-Dynamic which permits fully general TLS
2124 // access. Since we know that we are generating an executable,
2125 // we can convert this to Initial-Exec. If we also know that
2126 // this is a local symbol, we can further switch to Local-Exec.
2128 return tls::TLSOPT_TO_LE
;
2129 return tls::TLSOPT_TO_IE
;
2131 case elfcpp::R_X86_64_TLSLD
:
2132 // This is Local-Dynamic, which refers to a local symbol in the
2133 // dynamic TLS block. Since we know that we generating an
2134 // executable, we can switch to Local-Exec.
2135 return tls::TLSOPT_TO_LE
;
2137 case elfcpp::R_X86_64_DTPOFF32
:
2138 case elfcpp::R_X86_64_DTPOFF64
:
2139 // Another Local-Dynamic reloc.
2140 return tls::TLSOPT_TO_LE
;
2142 case elfcpp::R_X86_64_GOTTPOFF
:
2143 // These are Initial-Exec relocs which get the thread offset
2144 // from the GOT. If we know that we are linking against the
2145 // local symbol, we can switch to Local-Exec, which links the
2146 // thread offset into the instruction.
2148 return tls::TLSOPT_TO_LE
;
2149 return tls::TLSOPT_NONE
;
2151 case elfcpp::R_X86_64_TPOFF32
:
2152 // When we already have Local-Exec, there is nothing further we
2154 return tls::TLSOPT_NONE
;
2161 // Get the Reference_flags for a particular relocation.
2165 Target_x86_64
<size
>::Scan::get_reference_flags(unsigned int r_type
)
2169 case elfcpp::R_X86_64_NONE
:
2170 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2171 case elfcpp::R_X86_64_GNU_VTENTRY
:
2172 case elfcpp::R_X86_64_GOTPC32
:
2173 case elfcpp::R_X86_64_GOTPC64
:
2174 // No symbol reference.
2177 case elfcpp::R_X86_64_64
:
2178 case elfcpp::R_X86_64_32
:
2179 case elfcpp::R_X86_64_32S
:
2180 case elfcpp::R_X86_64_16
:
2181 case elfcpp::R_X86_64_8
:
2182 return Symbol::ABSOLUTE_REF
;
2184 case elfcpp::R_X86_64_PC64
:
2185 case elfcpp::R_X86_64_PC32
:
2186 case elfcpp::R_X86_64_PC32_BND
:
2187 case elfcpp::R_X86_64_PC16
:
2188 case elfcpp::R_X86_64_PC8
:
2189 case elfcpp::R_X86_64_GOTOFF64
:
2190 return Symbol::RELATIVE_REF
;
2192 case elfcpp::R_X86_64_PLT32
:
2193 case elfcpp::R_X86_64_PLT32_BND
:
2194 case elfcpp::R_X86_64_PLTOFF64
:
2195 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
2197 case elfcpp::R_X86_64_GOT64
:
2198 case elfcpp::R_X86_64_GOT32
:
2199 case elfcpp::R_X86_64_GOTPCREL64
:
2200 case elfcpp::R_X86_64_GOTPCREL
:
2201 case elfcpp::R_X86_64_GOTPCRELX
:
2202 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2203 case elfcpp::R_X86_64_GOTPLT64
:
2205 return Symbol::ABSOLUTE_REF
;
2207 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2208 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2209 case elfcpp::R_X86_64_TLSDESC_CALL
:
2210 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2211 case elfcpp::R_X86_64_DTPOFF32
:
2212 case elfcpp::R_X86_64_DTPOFF64
:
2213 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2214 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2215 return Symbol::TLS_REF
;
2217 case elfcpp::R_X86_64_COPY
:
2218 case elfcpp::R_X86_64_GLOB_DAT
:
2219 case elfcpp::R_X86_64_JUMP_SLOT
:
2220 case elfcpp::R_X86_64_RELATIVE
:
2221 case elfcpp::R_X86_64_IRELATIVE
:
2222 case elfcpp::R_X86_64_TPOFF64
:
2223 case elfcpp::R_X86_64_DTPMOD64
:
2224 case elfcpp::R_X86_64_TLSDESC
:
2225 case elfcpp::R_X86_64_SIZE32
:
2226 case elfcpp::R_X86_64_SIZE64
:
2228 // Not expected. We will give an error later.
2233 // Report an unsupported relocation against a local symbol.
2237 Target_x86_64
<size
>::Scan::unsupported_reloc_local(
2238 Sized_relobj_file
<size
, false>* object
,
2239 unsigned int r_type
)
2241 gold_error(_("%s: unsupported reloc %u against local symbol"),
2242 object
->name().c_str(), r_type
);
2245 // We are about to emit a dynamic relocation of type R_TYPE. If the
2246 // dynamic linker does not support it, issue an error. The GNU linker
2247 // only issues a non-PIC error for an allocated read-only section.
2248 // Here we know the section is allocated, but we don't know that it is
2249 // read-only. But we check for all the relocation types which the
2250 // glibc dynamic linker supports, so it seems appropriate to issue an
2251 // error even if the section is not read-only. If GSYM is not NULL,
2252 // it is the symbol the relocation is against; if it is NULL, the
2253 // relocation is against a local symbol.
2257 Target_x86_64
<size
>::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
2262 // These are the relocation types supported by glibc for x86_64
2263 // which should always work.
2264 case elfcpp::R_X86_64_RELATIVE
:
2265 case elfcpp::R_X86_64_IRELATIVE
:
2266 case elfcpp::R_X86_64_GLOB_DAT
:
2267 case elfcpp::R_X86_64_JUMP_SLOT
:
2268 case elfcpp::R_X86_64_DTPMOD64
:
2269 case elfcpp::R_X86_64_DTPOFF64
:
2270 case elfcpp::R_X86_64_TPOFF64
:
2271 case elfcpp::R_X86_64_64
:
2272 case elfcpp::R_X86_64_COPY
:
2275 // glibc supports these reloc types, but they can overflow.
2276 case elfcpp::R_X86_64_PC32
:
2277 case elfcpp::R_X86_64_PC32_BND
:
2278 // A PC relative reference is OK against a local symbol or if
2279 // the symbol is defined locally.
2281 || (!gsym
->is_from_dynobj()
2282 && !gsym
->is_undefined()
2283 && !gsym
->is_preemptible()))
2286 case elfcpp::R_X86_64_32
:
2287 // R_X86_64_32 is OK for x32.
2288 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2290 if (this->issued_non_pic_error_
)
2292 gold_assert(parameters
->options().output_is_position_independent());
2294 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
2295 "overflow at runtime; recompile with -fPIC"));
2301 case elfcpp::R_X86_64_32
:
2302 r_name
= "R_X86_64_32";
2304 case elfcpp::R_X86_64_PC32
:
2305 r_name
= "R_X86_64_PC32";
2307 case elfcpp::R_X86_64_PC32_BND
:
2308 r_name
= "R_X86_64_PC32_BND";
2314 object
->error(_("requires dynamic %s reloc against '%s' "
2315 "which may overflow at runtime; recompile "
2317 r_name
, gsym
->name());
2319 this->issued_non_pic_error_
= true;
2323 // This prevents us from issuing more than one error per reloc
2324 // section. But we can still wind up issuing more than one
2325 // error per object file.
2326 if (this->issued_non_pic_error_
)
2328 gold_assert(parameters
->options().output_is_position_independent());
2329 object
->error(_("requires unsupported dynamic reloc %u; "
2330 "recompile with -fPIC"),
2332 this->issued_non_pic_error_
= true;
2335 case elfcpp::R_X86_64_NONE
:
2340 // Return whether we need to make a PLT entry for a relocation of the
2341 // given type against a STT_GNU_IFUNC symbol.
2345 Target_x86_64
<size
>::Scan::reloc_needs_plt_for_ifunc(
2346 Sized_relobj_file
<size
, false>* object
,
2347 unsigned int r_type
)
2349 int flags
= Scan::get_reference_flags(r_type
);
2350 if (flags
& Symbol::TLS_REF
)
2351 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2352 object
->name().c_str(), r_type
);
2356 // Scan a relocation for a local symbol.
2360 Target_x86_64
<size
>::Scan::local(Symbol_table
* symtab
,
2362 Target_x86_64
<size
>* target
,
2363 Sized_relobj_file
<size
, false>* object
,
2364 unsigned int data_shndx
,
2365 Output_section
* output_section
,
2366 const elfcpp::Rela
<size
, false>& reloc
,
2367 unsigned int r_type
,
2368 const elfcpp::Sym
<size
, false>& lsym
,
2374 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2375 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
2376 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
2378 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2379 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
2384 case elfcpp::R_X86_64_NONE
:
2385 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2386 case elfcpp::R_X86_64_GNU_VTENTRY
:
2389 case elfcpp::R_X86_64_64
:
2390 // If building a shared library (or a position-independent
2391 // executable), we need to create a dynamic relocation for this
2392 // location. The relocation applied at link time will apply the
2393 // link-time value, so we flag the location with an
2394 // R_X86_64_RELATIVE relocation so the dynamic loader can
2395 // relocate it easily.
2396 if (parameters
->options().output_is_position_independent())
2398 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2399 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2400 rela_dyn
->add_local_relative(object
, r_sym
,
2402 ? elfcpp::R_X86_64_RELATIVE64
2403 : elfcpp::R_X86_64_RELATIVE
),
2404 output_section
, data_shndx
,
2405 reloc
.get_r_offset(),
2406 reloc
.get_r_addend(), is_ifunc
);
2410 case elfcpp::R_X86_64_32
:
2411 case elfcpp::R_X86_64_32S
:
2412 case elfcpp::R_X86_64_16
:
2413 case elfcpp::R_X86_64_8
:
2414 // If building a shared library (or a position-independent
2415 // executable), we need to create a dynamic relocation for this
2416 // location. We can't use an R_X86_64_RELATIVE relocation
2417 // because that is always a 64-bit relocation.
2418 if (parameters
->options().output_is_position_independent())
2420 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2421 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2423 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2424 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2425 rela_dyn
->add_local_relative(object
, r_sym
,
2426 elfcpp::R_X86_64_RELATIVE
,
2427 output_section
, data_shndx
,
2428 reloc
.get_r_offset(),
2429 reloc
.get_r_addend(), is_ifunc
);
2433 this->check_non_pic(object
, r_type
, NULL
);
2435 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2436 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2437 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
2438 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
2439 data_shndx
, reloc
.get_r_offset(),
2440 reloc
.get_r_addend());
2443 gold_assert(lsym
.get_st_value() == 0);
2444 unsigned int shndx
= lsym
.get_st_shndx();
2446 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
2449 object
->error(_("section symbol %u has bad shndx %u"),
2452 rela_dyn
->add_local_section(object
, shndx
,
2453 r_type
, output_section
,
2454 data_shndx
, reloc
.get_r_offset(),
2455 reloc
.get_r_addend());
2460 case elfcpp::R_X86_64_PC64
:
2461 case elfcpp::R_X86_64_PC32
:
2462 case elfcpp::R_X86_64_PC32_BND
:
2463 case elfcpp::R_X86_64_PC16
:
2464 case elfcpp::R_X86_64_PC8
:
2467 case elfcpp::R_X86_64_PLT32
:
2468 case elfcpp::R_X86_64_PLT32_BND
:
2469 // Since we know this is a local symbol, we can handle this as a
2473 case elfcpp::R_X86_64_GOTPC32
:
2474 case elfcpp::R_X86_64_GOTOFF64
:
2475 case elfcpp::R_X86_64_GOTPC64
:
2476 case elfcpp::R_X86_64_PLTOFF64
:
2477 // We need a GOT section.
2478 target
->got_section(symtab
, layout
);
2479 // For PLTOFF64, we'd normally want a PLT section, but since we
2480 // know this is a local symbol, no PLT is needed.
2483 case elfcpp::R_X86_64_GOT64
:
2484 case elfcpp::R_X86_64_GOT32
:
2485 case elfcpp::R_X86_64_GOTPCREL64
:
2486 case elfcpp::R_X86_64_GOTPCREL
:
2487 case elfcpp::R_X86_64_GOTPCRELX
:
2488 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2489 case elfcpp::R_X86_64_GOTPLT64
:
2491 // The symbol requires a GOT section.
2492 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2494 // If the relocation symbol isn't IFUNC,
2495 // and is local, then we will convert
2496 // mov foo@GOTPCREL(%rip), %reg
2497 // to lea foo(%rip), %reg.
2498 // in Relocate::relocate.
2499 if ((r_type
== elfcpp::R_X86_64_GOTPCREL
2500 || r_type
== elfcpp::R_X86_64_GOTPCRELX
2501 || r_type
== elfcpp::R_X86_64_REX_GOTPCRELX
)
2502 && reloc
.get_r_offset() >= 2
2505 section_size_type stype
;
2506 const unsigned char* view
= object
->section_contents(data_shndx
,
2508 if (view
[reloc
.get_r_offset() - 2] == 0x8b)
2513 // The symbol requires a GOT entry.
2514 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2516 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2517 // lets function pointers compare correctly with shared
2518 // libraries. Otherwise we would need an IRELATIVE reloc.
2521 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
2523 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
2526 // If we are generating a shared object, we need to add a
2527 // dynamic relocation for this symbol's GOT entry.
2528 if (parameters
->options().output_is_position_independent())
2530 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2531 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2532 if (r_type
!= elfcpp::R_X86_64_GOT32
)
2534 unsigned int got_offset
=
2535 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
2536 rela_dyn
->add_local_relative(object
, r_sym
,
2537 elfcpp::R_X86_64_RELATIVE
,
2538 got
, got_offset
, 0, is_ifunc
);
2542 this->check_non_pic(object
, r_type
, NULL
);
2544 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
2545 rela_dyn
->add_local(
2546 object
, r_sym
, r_type
, got
,
2547 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
2551 // For GOTPLT64, we'd normally want a PLT section, but since
2552 // we know this is a local symbol, no PLT is needed.
2556 case elfcpp::R_X86_64_COPY
:
2557 case elfcpp::R_X86_64_GLOB_DAT
:
2558 case elfcpp::R_X86_64_JUMP_SLOT
:
2559 case elfcpp::R_X86_64_RELATIVE
:
2560 case elfcpp::R_X86_64_IRELATIVE
:
2561 // These are outstanding tls relocs, which are unexpected when linking
2562 case elfcpp::R_X86_64_TPOFF64
:
2563 case elfcpp::R_X86_64_DTPMOD64
:
2564 case elfcpp::R_X86_64_TLSDESC
:
2565 gold_error(_("%s: unexpected reloc %u in object file"),
2566 object
->name().c_str(), r_type
);
2569 // These are initial tls relocs, which are expected when linking
2570 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2571 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2572 case elfcpp::R_X86_64_TLSDESC_CALL
:
2573 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2574 case elfcpp::R_X86_64_DTPOFF32
:
2575 case elfcpp::R_X86_64_DTPOFF64
:
2576 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2577 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2579 bool output_is_shared
= parameters
->options().shared();
2580 const tls::Tls_optimization optimized_type
2581 = Target_x86_64
<size
>::optimize_tls_reloc(!output_is_shared
,
2585 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2586 if (optimized_type
== tls::TLSOPT_NONE
)
2588 // Create a pair of GOT entries for the module index and
2589 // dtv-relative offset.
2590 Output_data_got
<64, false>* got
2591 = target
->got_section(symtab
, layout
);
2592 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2593 unsigned int shndx
= lsym
.get_st_shndx();
2595 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2597 object
->error(_("local symbol %u has bad shndx %u"),
2600 got
->add_local_pair_with_rel(object
, r_sym
,
2603 target
->rela_dyn_section(layout
),
2604 elfcpp::R_X86_64_DTPMOD64
);
2606 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2607 unsupported_reloc_local(object
, r_type
);
2610 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2611 target
->define_tls_base_symbol(symtab
, layout
);
2612 if (optimized_type
== tls::TLSOPT_NONE
)
2614 // Create reserved PLT and GOT entries for the resolver.
2615 target
->reserve_tlsdesc_entries(symtab
, layout
);
2617 // Generate a double GOT entry with an
2618 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2619 // is resolved lazily, so the GOT entry needs to be in
2620 // an area in .got.plt, not .got. Call got_section to
2621 // make sure the section has been created.
2622 target
->got_section(symtab
, layout
);
2623 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2624 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2625 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
2627 unsigned int got_offset
= got
->add_constant(0);
2628 got
->add_constant(0);
2629 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
2631 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2632 // We store the arguments we need in a vector, and
2633 // use the index into the vector as the parameter
2634 // to pass to the target specific routines.
2635 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
2636 void* arg
= reinterpret_cast<void*>(intarg
);
2637 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
2638 got
, got_offset
, 0);
2641 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2642 unsupported_reloc_local(object
, r_type
);
2645 case elfcpp::R_X86_64_TLSDESC_CALL
:
2648 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2649 if (optimized_type
== tls::TLSOPT_NONE
)
2651 // Create a GOT entry for the module index.
2652 target
->got_mod_index_entry(symtab
, layout
, object
);
2654 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2655 unsupported_reloc_local(object
, r_type
);
2658 case elfcpp::R_X86_64_DTPOFF32
:
2659 case elfcpp::R_X86_64_DTPOFF64
:
2662 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2663 layout
->set_has_static_tls();
2664 if (optimized_type
== tls::TLSOPT_NONE
)
2666 // Create a GOT entry for the tp-relative offset.
2667 Output_data_got
<64, false>* got
2668 = target
->got_section(symtab
, layout
);
2669 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2670 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
2671 target
->rela_dyn_section(layout
),
2672 elfcpp::R_X86_64_TPOFF64
);
2674 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2675 unsupported_reloc_local(object
, r_type
);
2678 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2679 layout
->set_has_static_tls();
2680 if (output_is_shared
)
2681 unsupported_reloc_local(object
, r_type
);
2690 case elfcpp::R_X86_64_SIZE32
:
2691 case elfcpp::R_X86_64_SIZE64
:
2693 gold_error(_("%s: unsupported reloc %u against local symbol"),
2694 object
->name().c_str(), r_type
);
2700 // Report an unsupported relocation against a global symbol.
2704 Target_x86_64
<size
>::Scan::unsupported_reloc_global(
2705 Sized_relobj_file
<size
, false>* object
,
2706 unsigned int r_type
,
2709 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2710 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
2713 // Returns true if this relocation type could be that of a function pointer.
2716 Target_x86_64
<size
>::Scan::possible_function_pointer_reloc(unsigned int r_type
)
2720 case elfcpp::R_X86_64_64
:
2721 case elfcpp::R_X86_64_32
:
2722 case elfcpp::R_X86_64_32S
:
2723 case elfcpp::R_X86_64_16
:
2724 case elfcpp::R_X86_64_8
:
2725 case elfcpp::R_X86_64_GOT64
:
2726 case elfcpp::R_X86_64_GOT32
:
2727 case elfcpp::R_X86_64_GOTPCREL64
:
2728 case elfcpp::R_X86_64_GOTPCREL
:
2729 case elfcpp::R_X86_64_GOTPCRELX
:
2730 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2731 case elfcpp::R_X86_64_GOTPLT64
:
2739 // For safe ICF, scan a relocation for a local symbol to check if it
2740 // corresponds to a function pointer being taken. In that case mark
2741 // the function whose pointer was taken as not foldable.
2745 Target_x86_64
<size
>::Scan::local_reloc_may_be_function_pointer(
2748 Target_x86_64
<size
>* ,
2749 Sized_relobj_file
<size
, false>* ,
2752 const elfcpp::Rela
<size
, false>& ,
2753 unsigned int r_type
,
2754 const elfcpp::Sym
<size
, false>&)
2756 // When building a shared library, do not fold any local symbols as it is
2757 // not possible to distinguish pointer taken versus a call by looking at
2758 // the relocation types.
2759 return (parameters
->options().shared()
2760 || possible_function_pointer_reloc(r_type
));
2763 // For safe ICF, scan a relocation for a global symbol to check if it
2764 // corresponds to a function pointer being taken. In that case mark
2765 // the function whose pointer was taken as not foldable.
2769 Target_x86_64
<size
>::Scan::global_reloc_may_be_function_pointer(
2772 Target_x86_64
<size
>* ,
2773 Sized_relobj_file
<size
, false>* ,
2776 const elfcpp::Rela
<size
, false>& ,
2777 unsigned int r_type
,
2780 // When building a shared library, do not fold symbols whose visibility
2781 // is hidden, internal or protected.
2782 return ((parameters
->options().shared()
2783 && (gsym
->visibility() == elfcpp::STV_INTERNAL
2784 || gsym
->visibility() == elfcpp::STV_PROTECTED
2785 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
2786 || possible_function_pointer_reloc(r_type
));
2789 // Scan a relocation for a global symbol.
2793 Target_x86_64
<size
>::Scan::global(Symbol_table
* symtab
,
2795 Target_x86_64
<size
>* target
,
2796 Sized_relobj_file
<size
, false>* object
,
2797 unsigned int data_shndx
,
2798 Output_section
* output_section
,
2799 const elfcpp::Rela
<size
, false>& reloc
,
2800 unsigned int r_type
,
2803 // A STT_GNU_IFUNC symbol may require a PLT entry.
2804 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2805 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
2806 target
->make_plt_entry(symtab
, layout
, gsym
);
2810 case elfcpp::R_X86_64_NONE
:
2811 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2812 case elfcpp::R_X86_64_GNU_VTENTRY
:
2815 case elfcpp::R_X86_64_64
:
2816 case elfcpp::R_X86_64_32
:
2817 case elfcpp::R_X86_64_32S
:
2818 case elfcpp::R_X86_64_16
:
2819 case elfcpp::R_X86_64_8
:
2821 // Make a PLT entry if necessary.
2822 if (gsym
->needs_plt_entry())
2824 target
->make_plt_entry(symtab
, layout
, gsym
);
2825 // Since this is not a PC-relative relocation, we may be
2826 // taking the address of a function. In that case we need to
2827 // set the entry in the dynamic symbol table to the address of
2829 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
2830 gsym
->set_needs_dynsym_value();
2832 // Make a dynamic relocation if necessary.
2833 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2835 if (!parameters
->options().output_is_position_independent()
2836 && gsym
->may_need_copy_reloc())
2838 target
->copy_reloc(symtab
, layout
, object
,
2839 data_shndx
, output_section
, gsym
, reloc
);
2841 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
2842 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
2843 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2844 && gsym
->can_use_relative_reloc(false)
2845 && !gsym
->is_from_dynobj()
2846 && !gsym
->is_undefined()
2847 && !gsym
->is_preemptible())
2849 // Use an IRELATIVE reloc for a locally defined
2850 // STT_GNU_IFUNC symbol. This makes a function
2851 // address in a PIE executable match the address in a
2852 // shared library that it links against.
2853 Reloc_section
* rela_dyn
=
2854 target
->rela_irelative_section(layout
);
2855 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
2856 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
2857 output_section
, object
,
2859 reloc
.get_r_offset(),
2860 reloc
.get_r_addend());
2862 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
2863 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
2864 && gsym
->can_use_relative_reloc(false))
2866 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2867 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2868 output_section
, object
,
2870 reloc
.get_r_offset(),
2871 reloc
.get_r_addend(), false);
2875 this->check_non_pic(object
, r_type
, gsym
);
2876 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2877 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2878 data_shndx
, reloc
.get_r_offset(),
2879 reloc
.get_r_addend());
2885 case elfcpp::R_X86_64_PC64
:
2886 case elfcpp::R_X86_64_PC32
:
2887 case elfcpp::R_X86_64_PC32_BND
:
2888 case elfcpp::R_X86_64_PC16
:
2889 case elfcpp::R_X86_64_PC8
:
2891 // Make a PLT entry if necessary.
2892 if (gsym
->needs_plt_entry())
2893 target
->make_plt_entry(symtab
, layout
, gsym
);
2894 // Make a dynamic relocation if necessary.
2895 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2897 if (parameters
->options().output_is_executable()
2898 && gsym
->may_need_copy_reloc())
2900 target
->copy_reloc(symtab
, layout
, object
,
2901 data_shndx
, output_section
, gsym
, reloc
);
2905 this->check_non_pic(object
, r_type
, gsym
);
2906 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2907 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2908 data_shndx
, reloc
.get_r_offset(),
2909 reloc
.get_r_addend());
2915 case elfcpp::R_X86_64_GOT64
:
2916 case elfcpp::R_X86_64_GOT32
:
2917 case elfcpp::R_X86_64_GOTPCREL64
:
2918 case elfcpp::R_X86_64_GOTPCREL
:
2919 case elfcpp::R_X86_64_GOTPCRELX
:
2920 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2921 case elfcpp::R_X86_64_GOTPLT64
:
2923 // The symbol requires a GOT entry.
2924 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2926 // If we convert this from
2927 // mov foo@GOTPCREL(%rip), %reg
2928 // to lea foo(%rip), %reg.
2929 // in Relocate::relocate, then there is nothing to do here.
2930 if ((r_type
== elfcpp::R_X86_64_GOTPCREL
2931 || r_type
== elfcpp::R_X86_64_GOTPCRELX
2932 || r_type
== elfcpp::R_X86_64_REX_GOTPCRELX
)
2933 && reloc
.get_r_offset() >= 2
2934 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
))
2936 section_size_type stype
;
2937 const unsigned char* view
= object
->section_contents(data_shndx
,
2939 if (view
[reloc
.get_r_offset() - 2] == 0x8b)
2943 if (gsym
->final_value_is_known())
2945 // For a STT_GNU_IFUNC symbol we want the PLT address.
2946 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
2947 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2949 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2953 // If this symbol is not fully resolved, we need to add a
2954 // dynamic relocation for it.
2955 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2957 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2959 // 1) The symbol may be defined in some other module.
2961 // 2) We are building a shared library and this is a
2962 // protected symbol; using GLOB_DAT means that the dynamic
2963 // linker can use the address of the PLT in the main
2964 // executable when appropriate so that function address
2965 // comparisons work.
2967 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2968 // code, again so that function address comparisons work.
2969 if (gsym
->is_from_dynobj()
2970 || gsym
->is_undefined()
2971 || gsym
->is_preemptible()
2972 || (gsym
->visibility() == elfcpp::STV_PROTECTED
2973 && parameters
->options().shared())
2974 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
2975 && parameters
->options().output_is_position_independent()))
2976 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2977 elfcpp::R_X86_64_GLOB_DAT
);
2980 // For a STT_GNU_IFUNC symbol we want to write the PLT
2981 // offset into the GOT, so that function pointer
2982 // comparisons work correctly.
2984 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
2985 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2988 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2989 // Tell the dynamic linker to use the PLT address
2990 // when resolving relocations.
2991 if (gsym
->is_from_dynobj()
2992 && !parameters
->options().shared())
2993 gsym
->set_needs_dynsym_value();
2997 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2998 rela_dyn
->add_global_relative(gsym
,
2999 elfcpp::R_X86_64_RELATIVE
,
3000 got
, got_off
, 0, false);
3007 case elfcpp::R_X86_64_PLT32
:
3008 case elfcpp::R_X86_64_PLT32_BND
:
3009 // If the symbol is fully resolved, this is just a PC32 reloc.
3010 // Otherwise we need a PLT entry.
3011 if (gsym
->final_value_is_known())
3013 // If building a shared library, we can also skip the PLT entry
3014 // if the symbol is defined in the output file and is protected
3016 if (gsym
->is_defined()
3017 && !gsym
->is_from_dynobj()
3018 && !gsym
->is_preemptible())
3020 target
->make_plt_entry(symtab
, layout
, gsym
);
3023 case elfcpp::R_X86_64_GOTPC32
:
3024 case elfcpp::R_X86_64_GOTOFF64
:
3025 case elfcpp::R_X86_64_GOTPC64
:
3026 case elfcpp::R_X86_64_PLTOFF64
:
3027 // We need a GOT section.
3028 target
->got_section(symtab
, layout
);
3029 // For PLTOFF64, we also need a PLT entry (but only if the
3030 // symbol is not fully resolved).
3031 if (r_type
== elfcpp::R_X86_64_PLTOFF64
3032 && !gsym
->final_value_is_known())
3033 target
->make_plt_entry(symtab
, layout
, gsym
);
3036 case elfcpp::R_X86_64_COPY
:
3037 case elfcpp::R_X86_64_GLOB_DAT
:
3038 case elfcpp::R_X86_64_JUMP_SLOT
:
3039 case elfcpp::R_X86_64_RELATIVE
:
3040 case elfcpp::R_X86_64_IRELATIVE
:
3041 // These are outstanding tls relocs, which are unexpected when linking
3042 case elfcpp::R_X86_64_TPOFF64
:
3043 case elfcpp::R_X86_64_DTPMOD64
:
3044 case elfcpp::R_X86_64_TLSDESC
:
3045 gold_error(_("%s: unexpected reloc %u in object file"),
3046 object
->name().c_str(), r_type
);
3049 // These are initial tls relocs, which are expected for global()
3050 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3051 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3052 case elfcpp::R_X86_64_TLSDESC_CALL
:
3053 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3054 case elfcpp::R_X86_64_DTPOFF32
:
3055 case elfcpp::R_X86_64_DTPOFF64
:
3056 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3057 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3059 // For the Initial-Exec model, we can treat undef symbols as final
3060 // when building an executable.
3061 const bool is_final
= (gsym
->final_value_is_known() ||
3062 (r_type
== elfcpp::R_X86_64_GOTTPOFF
&&
3063 gsym
->is_undefined() &&
3064 parameters
->options().output_is_executable()));
3065 const tls::Tls_optimization optimized_type
3066 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3069 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
3070 if (optimized_type
== tls::TLSOPT_NONE
)
3072 // Create a pair of GOT entries for the module index and
3073 // dtv-relative offset.
3074 Output_data_got
<64, false>* got
3075 = target
->got_section(symtab
, layout
);
3076 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
3077 target
->rela_dyn_section(layout
),
3078 elfcpp::R_X86_64_DTPMOD64
,
3079 elfcpp::R_X86_64_DTPOFF64
);
3081 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3083 // Create a GOT entry for the tp-relative offset.
3084 Output_data_got
<64, false>* got
3085 = target
->got_section(symtab
, layout
);
3086 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3087 target
->rela_dyn_section(layout
),
3088 elfcpp::R_X86_64_TPOFF64
);
3090 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3091 unsupported_reloc_global(object
, r_type
, gsym
);
3094 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3095 target
->define_tls_base_symbol(symtab
, layout
);
3096 if (optimized_type
== tls::TLSOPT_NONE
)
3098 // Create reserved PLT and GOT entries for the resolver.
3099 target
->reserve_tlsdesc_entries(symtab
, layout
);
3101 // Create a double GOT entry with an R_X86_64_TLSDESC
3102 // reloc. The R_X86_64_TLSDESC reloc is resolved
3103 // lazily, so the GOT entry needs to be in an area in
3104 // .got.plt, not .got. Call got_section to make sure
3105 // the section has been created.
3106 target
->got_section(symtab
, layout
);
3107 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
3108 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
3109 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
3110 elfcpp::R_X86_64_TLSDESC
, 0);
3112 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3114 // Create a GOT entry for the tp-relative offset.
3115 Output_data_got
<64, false>* got
3116 = target
->got_section(symtab
, layout
);
3117 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3118 target
->rela_dyn_section(layout
),
3119 elfcpp::R_X86_64_TPOFF64
);
3121 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3122 unsupported_reloc_global(object
, r_type
, gsym
);
3125 case elfcpp::R_X86_64_TLSDESC_CALL
:
3128 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3129 if (optimized_type
== tls::TLSOPT_NONE
)
3131 // Create a GOT entry for the module index.
3132 target
->got_mod_index_entry(symtab
, layout
, object
);
3134 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3135 unsupported_reloc_global(object
, r_type
, gsym
);
3138 case elfcpp::R_X86_64_DTPOFF32
:
3139 case elfcpp::R_X86_64_DTPOFF64
:
3142 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3143 layout
->set_has_static_tls();
3144 if (optimized_type
== tls::TLSOPT_NONE
)
3146 // Create a GOT entry for the tp-relative offset.
3147 Output_data_got
<64, false>* got
3148 = target
->got_section(symtab
, layout
);
3149 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3150 target
->rela_dyn_section(layout
),
3151 elfcpp::R_X86_64_TPOFF64
);
3153 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3154 unsupported_reloc_global(object
, r_type
, gsym
);
3157 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3158 layout
->set_has_static_tls();
3159 if (parameters
->options().shared())
3160 unsupported_reloc_global(object
, r_type
, gsym
);
3169 case elfcpp::R_X86_64_SIZE32
:
3170 case elfcpp::R_X86_64_SIZE64
:
3172 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3173 object
->name().c_str(), r_type
,
3174 gsym
->demangled_name().c_str());
3181 Target_x86_64
<size
>::gc_process_relocs(Symbol_table
* symtab
,
3183 Sized_relobj_file
<size
, false>* object
,
3184 unsigned int data_shndx
,
3185 unsigned int sh_type
,
3186 const unsigned char* prelocs
,
3188 Output_section
* output_section
,
3189 bool needs_special_offset_handling
,
3190 size_t local_symbol_count
,
3191 const unsigned char* plocal_symbols
)
3194 if (sh_type
== elfcpp::SHT_REL
)
3199 gold::gc_process_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
3200 typename Target_x86_64
<size
>::Scan
,
3201 typename Target_x86_64
<size
>::Relocatable_size_for_reloc
>(
3210 needs_special_offset_handling
,
3215 // Scan relocations for a section.
3219 Target_x86_64
<size
>::scan_relocs(Symbol_table
* symtab
,
3221 Sized_relobj_file
<size
, false>* object
,
3222 unsigned int data_shndx
,
3223 unsigned int sh_type
,
3224 const unsigned char* prelocs
,
3226 Output_section
* output_section
,
3227 bool needs_special_offset_handling
,
3228 size_t local_symbol_count
,
3229 const unsigned char* plocal_symbols
)
3231 if (sh_type
== elfcpp::SHT_REL
)
3233 gold_error(_("%s: unsupported REL reloc section"),
3234 object
->name().c_str());
3238 gold::scan_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
3239 typename Target_x86_64
<size
>::Scan
>(
3248 needs_special_offset_handling
,
3253 // Finalize the sections.
3257 Target_x86_64
<size
>::do_finalize_sections(
3259 const Input_objects
*,
3260 Symbol_table
* symtab
)
3262 const Reloc_section
* rel_plt
= (this->plt_
== NULL
3264 : this->plt_
->rela_plt());
3265 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
3266 this->rela_dyn_
, true, false);
3268 // Fill in some more dynamic tags.
3269 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
3272 if (this->plt_
!= NULL
3273 && this->plt_
->output_section() != NULL
3274 && this->plt_
->has_tlsdesc_entry())
3276 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
3277 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
3278 this->got_
->finalize_data_size();
3279 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
3280 this->plt_
, plt_offset
);
3281 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
3282 this->got_
, got_offset
);
3286 // Emit any relocs we saved in an attempt to avoid generating COPY
3288 if (this->copy_relocs_
.any_saved_relocs())
3289 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
3291 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3292 // the .got.plt section.
3293 Symbol
* sym
= this->global_offset_table_
;
3296 uint64_t data_size
= this->got_plt_
->current_data_size();
3297 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
3300 if (parameters
->doing_static_link()
3301 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
3303 // If linking statically, make sure that the __rela_iplt symbols
3304 // were defined if necessary, even if we didn't create a PLT.
3305 static const Define_symbol_in_segment syms
[] =
3308 "__rela_iplt_start", // name
3309 elfcpp::PT_LOAD
, // segment_type
3310 elfcpp::PF_W
, // segment_flags_set
3311 elfcpp::PF(0), // segment_flags_clear
3314 elfcpp::STT_NOTYPE
, // type
3315 elfcpp::STB_GLOBAL
, // binding
3316 elfcpp::STV_HIDDEN
, // visibility
3318 Symbol::SEGMENT_START
, // offset_from_base
3322 "__rela_iplt_end", // name
3323 elfcpp::PT_LOAD
, // segment_type
3324 elfcpp::PF_W
, // segment_flags_set
3325 elfcpp::PF(0), // segment_flags_clear
3328 elfcpp::STT_NOTYPE
, // type
3329 elfcpp::STB_GLOBAL
, // binding
3330 elfcpp::STV_HIDDEN
, // visibility
3332 Symbol::SEGMENT_START
, // offset_from_base
3337 symtab
->define_symbols(layout
, 2, syms
,
3338 layout
->script_options()->saw_sections_clause());
3342 // Perform a relocation.
3346 Target_x86_64
<size
>::Relocate::relocate(
3347 const Relocate_info
<size
, false>* relinfo
,
3349 Target_x86_64
<size
>* target
,
3352 const unsigned char* preloc
,
3353 const Sized_symbol
<size
>* gsym
,
3354 const Symbol_value
<size
>* psymval
,
3355 unsigned char* view
,
3356 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3357 section_size_type view_size
)
3359 const elfcpp::Rela
<size
, false> rela(preloc
);
3360 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
3362 if (this->skip_call_tls_get_addr_
)
3364 if ((r_type
!= elfcpp::R_X86_64_PLT32
3365 && r_type
!= elfcpp::R_X86_64_PLT32_BND
3366 && r_type
!= elfcpp::R_X86_64_PC32_BND
3367 && r_type
!= elfcpp::R_X86_64_PC32
)
3369 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
3371 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3372 _("missing expected TLS relocation"));
3376 this->skip_call_tls_get_addr_
= false;
3384 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3386 // Pick the value to use for symbols defined in the PLT.
3387 Symbol_value
<size
> symval
;
3389 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
3391 symval
.set_output_value(target
->plt_address_for_global(gsym
));
3394 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
3396 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3397 if (object
->local_has_plt_offset(r_sym
))
3399 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
3404 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3406 // Get the GOT offset if needed.
3407 // The GOT pointer points to the end of the GOT section.
3408 // We need to subtract the size of the GOT section to get
3409 // the actual offset to use in the relocation.
3410 bool have_got_offset
= false;
3411 // Since the actual offset is always negative, we use signed int to
3412 // support 64-bit GOT relocations.
3416 case elfcpp::R_X86_64_GOT32
:
3417 case elfcpp::R_X86_64_GOT64
:
3418 case elfcpp::R_X86_64_GOTPLT64
:
3419 case elfcpp::R_X86_64_GOTPCREL64
:
3422 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3423 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
3427 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3428 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3429 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
3430 - target
->got_size());
3432 have_got_offset
= true;
3441 case elfcpp::R_X86_64_NONE
:
3442 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3443 case elfcpp::R_X86_64_GNU_VTENTRY
:
3446 case elfcpp::R_X86_64_64
:
3447 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
, addend
);
3450 case elfcpp::R_X86_64_PC64
:
3451 Relocate_functions
<size
, false>::pcrela64(view
, object
, psymval
, addend
,
3455 case elfcpp::R_X86_64_32
:
3456 // FIXME: we need to verify that value + addend fits into 32 bits:
3457 // uint64_t x = value + addend;
3458 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3459 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
3460 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3463 case elfcpp::R_X86_64_32S
:
3464 // FIXME: we need to verify that value + addend fits into 32 bits:
3465 // int64_t x = value + addend; // note this quantity is signed!
3466 // x == static_cast<int64_t>(static_cast<int32_t>(x))
3467 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3470 case elfcpp::R_X86_64_PC32
:
3471 case elfcpp::R_X86_64_PC32_BND
:
3472 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3476 case elfcpp::R_X86_64_16
:
3477 Relocate_functions
<size
, false>::rela16(view
, object
, psymval
, addend
);
3480 case elfcpp::R_X86_64_PC16
:
3481 Relocate_functions
<size
, false>::pcrela16(view
, object
, psymval
, addend
,
3485 case elfcpp::R_X86_64_8
:
3486 Relocate_functions
<size
, false>::rela8(view
, object
, psymval
, addend
);
3489 case elfcpp::R_X86_64_PC8
:
3490 Relocate_functions
<size
, false>::pcrela8(view
, object
, psymval
, addend
,
3494 case elfcpp::R_X86_64_PLT32
:
3495 case elfcpp::R_X86_64_PLT32_BND
:
3496 gold_assert(gsym
== NULL
3497 || gsym
->has_plt_offset()
3498 || gsym
->final_value_is_known()
3499 || (gsym
->is_defined()
3500 && !gsym
->is_from_dynobj()
3501 && !gsym
->is_preemptible()));
3502 // Note: while this code looks the same as for R_X86_64_PC32, it
3503 // behaves differently because psymval was set to point to
3504 // the PLT entry, rather than the symbol, in Scan::global().
3505 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3509 case elfcpp::R_X86_64_PLTOFF64
:
3512 gold_assert(gsym
->has_plt_offset()
3513 || gsym
->final_value_is_known());
3514 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
;
3515 // This is the address of GLOBAL_OFFSET_TABLE.
3516 got_address
= target
->got_plt_section()->address();
3517 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
,
3518 addend
- got_address
);
3522 case elfcpp::R_X86_64_GOT32
:
3523 gold_assert(have_got_offset
);
3524 Relocate_functions
<size
, false>::rela32(view
, got_offset
, addend
);
3527 case elfcpp::R_X86_64_GOTPC32
:
3530 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3531 value
= target
->got_plt_section()->address();
3532 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3536 case elfcpp::R_X86_64_GOT64
:
3537 case elfcpp::R_X86_64_GOTPLT64
:
3538 // R_X86_64_GOTPLT64 is obsolete and treated the the same as
3540 gold_assert(have_got_offset
);
3541 Relocate_functions
<size
, false>::rela64(view
, got_offset
, addend
);
3544 case elfcpp::R_X86_64_GOTPC64
:
3547 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3548 value
= target
->got_plt_section()->address();
3549 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3553 case elfcpp::R_X86_64_GOTOFF64
:
3555 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3556 value
= (psymval
->value(object
, 0)
3557 - target
->got_plt_section()->address());
3558 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3562 case elfcpp::R_X86_64_GOTPCREL
:
3563 case elfcpp::R_X86_64_GOTPCRELX
:
3564 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3567 // mov foo@GOTPCREL(%rip), %reg
3568 // to lea foo(%rip), %reg.
3570 if (rela
.get_r_offset() >= 2
3572 && ((gsym
== NULL
&& !psymval
->is_ifunc_symbol())
3574 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
))))
3577 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3584 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3585 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
3589 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3590 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3591 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
3592 - target
->got_size());
3594 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3595 value
= target
->got_plt_section()->address() + got_offset
;
3596 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3601 case elfcpp::R_X86_64_GOTPCREL64
:
3603 gold_assert(have_got_offset
);
3604 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3605 value
= target
->got_plt_section()->address() + got_offset
;
3606 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3610 case elfcpp::R_X86_64_COPY
:
3611 case elfcpp::R_X86_64_GLOB_DAT
:
3612 case elfcpp::R_X86_64_JUMP_SLOT
:
3613 case elfcpp::R_X86_64_RELATIVE
:
3614 case elfcpp::R_X86_64_IRELATIVE
:
3615 // These are outstanding tls relocs, which are unexpected when linking
3616 case elfcpp::R_X86_64_TPOFF64
:
3617 case elfcpp::R_X86_64_DTPMOD64
:
3618 case elfcpp::R_X86_64_TLSDESC
:
3619 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3620 _("unexpected reloc %u in object file"),
3624 // These are initial tls relocs, which are expected when linking
3625 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3626 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3627 case elfcpp::R_X86_64_TLSDESC_CALL
:
3628 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3629 case elfcpp::R_X86_64_DTPOFF32
:
3630 case elfcpp::R_X86_64_DTPOFF64
:
3631 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3632 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3633 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
3634 view
, address
, view_size
);
3637 case elfcpp::R_X86_64_SIZE32
:
3638 case elfcpp::R_X86_64_SIZE64
:
3640 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3641 _("unsupported reloc %u"),
3649 // Perform a TLS relocation.
3653 Target_x86_64
<size
>::Relocate::relocate_tls(
3654 const Relocate_info
<size
, false>* relinfo
,
3655 Target_x86_64
<size
>* target
,
3657 const elfcpp::Rela
<size
, false>& rela
,
3658 unsigned int r_type
,
3659 const Sized_symbol
<size
>* gsym
,
3660 const Symbol_value
<size
>* psymval
,
3661 unsigned char* view
,
3662 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3663 section_size_type view_size
)
3665 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
3667 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3668 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3669 elfcpp::Shdr
<size
, false> data_shdr(relinfo
->data_shdr
);
3670 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
3672 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
3674 const bool is_final
= (gsym
== NULL
3675 ? !parameters
->options().shared()
3676 : gsym
->final_value_is_known());
3677 tls::Tls_optimization optimized_type
3678 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3681 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3682 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3684 // If this code sequence is used in a non-executable section,
3685 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3686 // on the assumption that it's being used by itself in a debug
3687 // section. Therefore, in the unlikely event that the code
3688 // sequence appears in a non-executable section, we simply
3689 // leave it unoptimized.
3690 optimized_type
= tls::TLSOPT_NONE
;
3692 if (optimized_type
== tls::TLSOPT_TO_LE
)
3694 if (tls_segment
== NULL
)
3696 gold_assert(parameters
->errors()->error_count() > 0
3697 || issue_undefined_symbol_error(gsym
));
3700 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
3701 rela
, r_type
, value
, view
,
3707 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3708 ? GOT_TYPE_TLS_OFFSET
3709 : GOT_TYPE_TLS_PAIR
);
3710 unsigned int got_offset
;
3713 gold_assert(gsym
->has_got_offset(got_type
));
3714 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
3718 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3719 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3720 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
3721 - target
->got_size());
3723 if (optimized_type
== tls::TLSOPT_TO_IE
)
3725 value
= target
->got_plt_section()->address() + got_offset
;
3726 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3727 value
, view
, address
, view_size
);
3730 else if (optimized_type
== tls::TLSOPT_NONE
)
3732 // Relocate the field with the offset of the pair of GOT
3734 value
= target
->got_plt_section()->address() + got_offset
;
3735 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3740 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3741 _("unsupported reloc %u"), r_type
);
3744 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3745 case elfcpp::R_X86_64_TLSDESC_CALL
:
3746 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3748 // See above comment for R_X86_64_TLSGD.
3749 optimized_type
= tls::TLSOPT_NONE
;
3751 if (optimized_type
== tls::TLSOPT_TO_LE
)
3753 if (tls_segment
== NULL
)
3755 gold_assert(parameters
->errors()->error_count() > 0
3756 || issue_undefined_symbol_error(gsym
));
3759 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
3760 rela
, r_type
, value
, view
,
3766 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3767 ? GOT_TYPE_TLS_OFFSET
3768 : GOT_TYPE_TLS_DESC
);
3769 unsigned int got_offset
= 0;
3770 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
3771 && optimized_type
== tls::TLSOPT_NONE
)
3773 // We created GOT entries in the .got.tlsdesc portion of
3774 // the .got.plt section, but the offset stored in the
3775 // symbol is the offset within .got.tlsdesc.
3776 got_offset
= (target
->got_size()
3777 + target
->got_plt_section()->data_size());
3781 gold_assert(gsym
->has_got_offset(got_type
));
3782 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
3786 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3787 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3788 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
3789 - target
->got_size());
3791 if (optimized_type
== tls::TLSOPT_TO_IE
)
3793 if (tls_segment
== NULL
)
3795 gold_assert(parameters
->errors()->error_count() > 0
3796 || issue_undefined_symbol_error(gsym
));
3799 value
= target
->got_plt_section()->address() + got_offset
;
3800 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
3801 rela
, r_type
, value
, view
, address
,
3805 else if (optimized_type
== tls::TLSOPT_NONE
)
3807 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3809 // Relocate the field with the offset of the pair of GOT
3811 value
= target
->got_plt_section()->address() + got_offset
;
3812 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3818 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3819 _("unsupported reloc %u"), r_type
);
3822 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3823 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3825 // See above comment for R_X86_64_TLSGD.
3826 optimized_type
= tls::TLSOPT_NONE
;
3828 if (optimized_type
== tls::TLSOPT_TO_LE
)
3830 if (tls_segment
== NULL
)
3832 gold_assert(parameters
->errors()->error_count() > 0
3833 || issue_undefined_symbol_error(gsym
));
3836 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3837 value
, view
, view_size
);
3840 else if (optimized_type
== tls::TLSOPT_NONE
)
3842 // Relocate the field with the offset of the GOT entry for
3843 // the module index.
3844 unsigned int got_offset
;
3845 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
3846 - target
->got_size());
3847 value
= target
->got_plt_section()->address() + got_offset
;
3848 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3852 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3853 _("unsupported reloc %u"), r_type
);
3856 case elfcpp::R_X86_64_DTPOFF32
:
3857 // This relocation type is used in debugging information.
3858 // In that case we need to not optimize the value. If the
3859 // section is not executable, then we assume we should not
3860 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3861 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3863 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3865 if (tls_segment
== NULL
)
3867 gold_assert(parameters
->errors()->error_count() > 0
3868 || issue_undefined_symbol_error(gsym
));
3871 value
-= tls_segment
->memsz();
3873 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3876 case elfcpp::R_X86_64_DTPOFF64
:
3877 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3878 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3880 if (tls_segment
== NULL
)
3882 gold_assert(parameters
->errors()->error_count() > 0
3883 || issue_undefined_symbol_error(gsym
));
3886 value
-= tls_segment
->memsz();
3888 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3891 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3893 && gsym
->is_undefined()
3894 && parameters
->options().output_is_executable())
3896 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
3898 r_type
, value
, view
,
3902 else if (optimized_type
== tls::TLSOPT_TO_LE
)
3904 if (tls_segment
== NULL
)
3906 gold_assert(parameters
->errors()->error_count() > 0
3907 || issue_undefined_symbol_error(gsym
));
3910 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
3912 r_type
, value
, view
,
3916 else if (optimized_type
== tls::TLSOPT_NONE
)
3918 // Relocate the field with the offset of the GOT entry for
3919 // the tp-relative offset of the symbol.
3920 unsigned int got_offset
;
3923 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
3924 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
3925 - target
->got_size());
3929 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3930 gold_assert(object
->local_has_got_offset(r_sym
,
3931 GOT_TYPE_TLS_OFFSET
));
3932 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
3933 - target
->got_size());
3935 value
= target
->got_plt_section()->address() + got_offset
;
3936 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3940 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3941 _("unsupported reloc type %u"),
3945 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3946 if (tls_segment
== NULL
)
3948 gold_assert(parameters
->errors()->error_count() > 0
3949 || issue_undefined_symbol_error(gsym
));
3952 value
-= tls_segment
->memsz();
3953 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3958 // Do a relocation in which we convert a TLS General-Dynamic to an
3963 Target_x86_64
<size
>::Relocate::tls_gd_to_ie(
3964 const Relocate_info
<size
, false>* relinfo
,
3967 const elfcpp::Rela
<size
, false>& rela
,
3969 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3970 unsigned char* view
,
3971 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3972 section_size_type view_size
)
3975 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3976 // .word 0x6666; rex64; call __tls_get_addr
3977 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3979 // leaq foo@tlsgd(%rip),%rdi;
3980 // .word 0x6666; rex64; call __tls_get_addr
3981 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
3983 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3984 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3985 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3989 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3991 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3992 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3993 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3998 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4000 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4001 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
4002 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4006 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4007 Relocate_functions
<size
, false>::pcrela32(view
+ 8, value
, addend
- 8,
4010 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4012 this->skip_call_tls_get_addr_
= true;
4015 // Do a relocation in which we convert a TLS General-Dynamic to a
4020 Target_x86_64
<size
>::Relocate::tls_gd_to_le(
4021 const Relocate_info
<size
, false>* relinfo
,
4023 Output_segment
* tls_segment
,
4024 const elfcpp::Rela
<size
, false>& rela
,
4026 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4027 unsigned char* view
,
4028 section_size_type view_size
)
4031 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4032 // .word 0x6666; rex64; call __tls_get_addr
4033 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4035 // leaq foo@tlsgd(%rip),%rdi;
4036 // .word 0x6666; rex64; call __tls_get_addr
4037 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4039 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
4040 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4041 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
4045 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4047 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4048 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
4049 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4054 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4056 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4057 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
4059 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4063 value
-= tls_segment
->memsz();
4064 Relocate_functions
<size
, false>::rela32(view
+ 8, value
, 0);
4066 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4068 this->skip_call_tls_get_addr_
= true;
4071 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4075 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_ie(
4076 const Relocate_info
<size
, false>* relinfo
,
4079 const elfcpp::Rela
<size
, false>& rela
,
4080 unsigned int r_type
,
4081 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4082 unsigned char* view
,
4083 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4084 section_size_type view_size
)
4086 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4088 // leaq foo@tlsdesc(%rip), %rax
4089 // ==> movq foo@gottpoff(%rip), %rax
4090 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4091 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4092 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4093 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
4095 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4096 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
4100 // call *foo@tlscall(%rax)
4102 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
4103 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
4104 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4105 view
[0] == 0xff && view
[1] == 0x10);
4111 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4115 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_le(
4116 const Relocate_info
<size
, false>* relinfo
,
4118 Output_segment
* tls_segment
,
4119 const elfcpp::Rela
<size
, false>& rela
,
4120 unsigned int r_type
,
4121 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4122 unsigned char* view
,
4123 section_size_type view_size
)
4125 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4127 // leaq foo@tlsdesc(%rip), %rax
4128 // ==> movq foo@tpoff, %rax
4129 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4130 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4131 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4132 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
4135 value
-= tls_segment
->memsz();
4136 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
4140 // call *foo@tlscall(%rax)
4142 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
4143 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
4144 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4145 view
[0] == 0xff && view
[1] == 0x10);
4153 Target_x86_64
<size
>::Relocate::tls_ld_to_le(
4154 const Relocate_info
<size
, false>* relinfo
,
4157 const elfcpp::Rela
<size
, false>& rela
,
4159 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
4160 unsigned char* view
,
4161 section_size_type view_size
)
4163 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4165 // ... leq foo@dtpoff(%rax),%reg
4166 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4168 // ... leq foo@dtpoff(%rax),%reg
4169 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4171 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4172 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
4174 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4175 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
4177 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
4180 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4182 memcpy(view
- 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4184 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4186 this->skip_call_tls_get_addr_
= true;
4189 // Do a relocation in which we convert a TLS Initial-Exec to a
4194 Target_x86_64
<size
>::Relocate::tls_ie_to_le(
4195 const Relocate_info
<size
, false>* relinfo
,
4197 Output_segment
* tls_segment
,
4198 const elfcpp::Rela
<size
, false>& rela
,
4200 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4201 unsigned char* view
,
4202 section_size_type view_size
)
4204 // We need to examine the opcodes to figure out which instruction we
4207 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4208 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4210 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4211 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4213 unsigned char op1
= view
[-3];
4214 unsigned char op2
= view
[-2];
4215 unsigned char op3
= view
[-1];
4216 unsigned char reg
= op3
>> 3;
4223 else if (size
== 32 && op1
== 0x44)
4226 view
[-1] = 0xc0 | reg
;
4230 // Special handling for %rsp.
4233 else if (size
== 32 && op1
== 0x44)
4236 view
[-1] = 0xc0 | reg
;
4243 else if (size
== 32 && op1
== 0x44)
4246 view
[-1] = 0x80 | reg
| (reg
<< 3);
4249 if (tls_segment
!= NULL
)
4250 value
-= tls_segment
->memsz();
4251 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
4254 // Relocate section data.
4258 Target_x86_64
<size
>::relocate_section(
4259 const Relocate_info
<size
, false>* relinfo
,
4260 unsigned int sh_type
,
4261 const unsigned char* prelocs
,
4263 Output_section
* output_section
,
4264 bool needs_special_offset_handling
,
4265 unsigned char* view
,
4266 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4267 section_size_type view_size
,
4268 const Reloc_symbol_changes
* reloc_symbol_changes
)
4270 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4272 gold::relocate_section
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
4273 typename Target_x86_64
<size
>::Relocate
,
4274 gold::Default_comdat_behavior
>(
4280 needs_special_offset_handling
,
4284 reloc_symbol_changes
);
4287 // Apply an incremental relocation. Incremental relocations always refer
4288 // to global symbols.
4292 Target_x86_64
<size
>::apply_relocation(
4293 const Relocate_info
<size
, false>* relinfo
,
4294 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
4295 unsigned int r_type
,
4296 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
4298 unsigned char* view
,
4299 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4300 section_size_type view_size
)
4302 gold::apply_relocation
<size
, false, Target_x86_64
<size
>,
4303 typename Target_x86_64
<size
>::Relocate
>(
4315 // Return the size of a relocation while scanning during a relocatable
4320 Target_x86_64
<size
>::Relocatable_size_for_reloc::get_size_for_reloc(
4321 unsigned int r_type
,
4326 case elfcpp::R_X86_64_NONE
:
4327 case elfcpp::R_X86_64_GNU_VTINHERIT
:
4328 case elfcpp::R_X86_64_GNU_VTENTRY
:
4329 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4330 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4331 case elfcpp::R_X86_64_TLSDESC_CALL
:
4332 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4333 case elfcpp::R_X86_64_DTPOFF32
:
4334 case elfcpp::R_X86_64_DTPOFF64
:
4335 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4336 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4339 case elfcpp::R_X86_64_64
:
4340 case elfcpp::R_X86_64_PC64
:
4341 case elfcpp::R_X86_64_GOTOFF64
:
4342 case elfcpp::R_X86_64_GOTPC64
:
4343 case elfcpp::R_X86_64_PLTOFF64
:
4344 case elfcpp::R_X86_64_GOT64
:
4345 case elfcpp::R_X86_64_GOTPCREL64
:
4346 case elfcpp::R_X86_64_GOTPCREL
:
4347 case elfcpp::R_X86_64_GOTPCRELX
:
4348 case elfcpp::R_X86_64_REX_GOTPCRELX
:
4349 case elfcpp::R_X86_64_GOTPLT64
:
4352 case elfcpp::R_X86_64_32
:
4353 case elfcpp::R_X86_64_32S
:
4354 case elfcpp::R_X86_64_PC32
:
4355 case elfcpp::R_X86_64_PC32_BND
:
4356 case elfcpp::R_X86_64_PLT32
:
4357 case elfcpp::R_X86_64_PLT32_BND
:
4358 case elfcpp::R_X86_64_GOTPC32
:
4359 case elfcpp::R_X86_64_GOT32
:
4362 case elfcpp::R_X86_64_16
:
4363 case elfcpp::R_X86_64_PC16
:
4366 case elfcpp::R_X86_64_8
:
4367 case elfcpp::R_X86_64_PC8
:
4370 case elfcpp::R_X86_64_COPY
:
4371 case elfcpp::R_X86_64_GLOB_DAT
:
4372 case elfcpp::R_X86_64_JUMP_SLOT
:
4373 case elfcpp::R_X86_64_RELATIVE
:
4374 case elfcpp::R_X86_64_IRELATIVE
:
4375 // These are outstanding tls relocs, which are unexpected when linking
4376 case elfcpp::R_X86_64_TPOFF64
:
4377 case elfcpp::R_X86_64_DTPMOD64
:
4378 case elfcpp::R_X86_64_TLSDESC
:
4379 object
->error(_("unexpected reloc %u in object file"), r_type
);
4382 case elfcpp::R_X86_64_SIZE32
:
4383 case elfcpp::R_X86_64_SIZE64
:
4385 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
4390 // Scan the relocs during a relocatable link.
4394 Target_x86_64
<size
>::scan_relocatable_relocs(
4395 Symbol_table
* symtab
,
4397 Sized_relobj_file
<size
, false>* object
,
4398 unsigned int data_shndx
,
4399 unsigned int sh_type
,
4400 const unsigned char* prelocs
,
4402 Output_section
* output_section
,
4403 bool needs_special_offset_handling
,
4404 size_t local_symbol_count
,
4405 const unsigned char* plocal_symbols
,
4406 Relocatable_relocs
* rr
)
4408 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4410 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
4411 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
4413 gold::scan_relocatable_relocs
<size
, false, elfcpp::SHT_RELA
,
4414 Scan_relocatable_relocs
>(
4422 needs_special_offset_handling
,
4428 // Relocate a section during a relocatable link.
4432 Target_x86_64
<size
>::relocate_relocs(
4433 const Relocate_info
<size
, false>* relinfo
,
4434 unsigned int sh_type
,
4435 const unsigned char* prelocs
,
4437 Output_section
* output_section
,
4438 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
4439 unsigned char* view
,
4440 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
4441 section_size_type view_size
,
4442 unsigned char* reloc_view
,
4443 section_size_type reloc_view_size
)
4445 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4447 gold::relocate_relocs
<size
, false, elfcpp::SHT_RELA
>(
4452 offset_in_output_section
,
4460 // Return the value to use for a dynamic which requires special
4461 // treatment. This is how we support equality comparisons of function
4462 // pointers across shared library boundaries, as described in the
4463 // processor specific ABI supplement.
4467 Target_x86_64
<size
>::do_dynsym_value(const Symbol
* gsym
) const
4469 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
4470 return this->plt_address_for_global(gsym
);
4473 // Return a string used to fill a code section with nops to take up
4474 // the specified length.
4478 Target_x86_64
<size
>::do_code_fill(section_size_type length
) const
4482 // Build a jmpq instruction to skip over the bytes.
4483 unsigned char jmp
[5];
4485 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
4486 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
4487 + std::string(length
- 5, static_cast<char>(0x90)));
4490 // Nop sequences of various lengths.
4491 const char nop1
[1] = { '\x90' }; // nop
4492 const char nop2
[2] = { '\x66', '\x90' }; // xchg %ax %ax
4493 const char nop3
[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4494 const char nop4
[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4496 const char nop5
[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4498 const char nop6
[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4499 '\x44', '\x00', '\x00' };
4500 const char nop7
[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4501 '\x00', '\x00', '\x00',
4503 const char nop8
[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4504 '\x00', '\x00', '\x00',
4506 const char nop9
[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4507 '\x84', '\x00', '\x00',
4508 '\x00', '\x00', '\x00' };
4509 const char nop10
[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4510 '\x1f', '\x84', '\x00',
4511 '\x00', '\x00', '\x00',
4513 const char nop11
[11] = { '\x66', '\x66', '\x2e', // data16
4514 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4515 '\x00', '\x00', '\x00',
4517 const char nop12
[12] = { '\x66', '\x66', '\x66', // data16; data16
4518 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4519 '\x84', '\x00', '\x00',
4520 '\x00', '\x00', '\x00' };
4521 const char nop13
[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4522 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4523 '\x1f', '\x84', '\x00',
4524 '\x00', '\x00', '\x00',
4526 const char nop14
[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4527 '\x66', '\x66', '\x2e', // data16
4528 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4529 '\x00', '\x00', '\x00',
4531 const char nop15
[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4532 '\x66', '\x66', '\x66', // data16; data16
4533 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4534 '\x84', '\x00', '\x00',
4535 '\x00', '\x00', '\x00' };
4537 const char* nops
[16] = {
4539 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
4540 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
4543 return std::string(nops
[length
], length
);
4546 // Return the addend to use for a target specific relocation. The
4547 // only target specific relocation is R_X86_64_TLSDESC for a local
4548 // symbol. We want to set the addend is the offset of the local
4549 // symbol in the TLS segment.
4553 Target_x86_64
<size
>::do_reloc_addend(void* arg
, unsigned int r_type
,
4556 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
4557 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
4558 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
4559 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
4560 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
4561 gold_assert(psymval
->is_tls_symbol());
4562 // The value of a TLS symbol is the offset in the TLS segment.
4563 return psymval
->value(ti
.object
, 0);
4566 // Return the value to use for the base of a DW_EH_PE_datarel offset
4567 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4568 // assembler can not write out the difference between two labels in
4569 // different sections, so instead of using a pc-relative value they
4570 // use an offset from the GOT.
4574 Target_x86_64
<size
>::do_ehframe_datarel_base() const
4576 gold_assert(this->global_offset_table_
!= NULL
);
4577 Symbol
* sym
= this->global_offset_table_
;
4578 Sized_symbol
<size
>* ssym
= static_cast<Sized_symbol
<size
>*>(sym
);
4579 return ssym
->value();
4582 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4583 // compiled with -fsplit-stack. The function calls non-split-stack
4584 // code. We have to change the function so that it always ensures
4585 // that it has enough stack space to run some random function.
4587 static const unsigned char cmp_insn_32
[] = { 0x64, 0x3b, 0x24, 0x25 };
4588 static const unsigned char lea_r10_insn_32
[] = { 0x44, 0x8d, 0x94, 0x24 };
4589 static const unsigned char lea_r11_insn_32
[] = { 0x44, 0x8d, 0x9c, 0x24 };
4591 static const unsigned char cmp_insn_64
[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
4592 static const unsigned char lea_r10_insn_64
[] = { 0x4c, 0x8d, 0x94, 0x24 };
4593 static const unsigned char lea_r11_insn_64
[] = { 0x4c, 0x8d, 0x9c, 0x24 };
4597 Target_x86_64
<size
>::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
4598 section_offset_type fnoffset
,
4599 section_size_type fnsize
,
4600 unsigned char* view
,
4601 section_size_type view_size
,
4603 std::string
* to
) const
4605 const char* const cmp_insn
= reinterpret_cast<const char*>
4606 (size
== 32 ? cmp_insn_32
: cmp_insn_64
);
4607 const char* const lea_r10_insn
= reinterpret_cast<const char*>
4608 (size
== 32 ? lea_r10_insn_32
: lea_r10_insn_64
);
4609 const char* const lea_r11_insn
= reinterpret_cast<const char*>
4610 (size
== 32 ? lea_r11_insn_32
: lea_r11_insn_64
);
4612 const size_t cmp_insn_len
=
4613 (size
== 32 ? sizeof(cmp_insn_32
) : sizeof(cmp_insn_64
));
4614 const size_t lea_r10_insn_len
=
4615 (size
== 32 ? sizeof(lea_r10_insn_32
) : sizeof(lea_r10_insn_64
));
4616 const size_t lea_r11_insn_len
=
4617 (size
== 32 ? sizeof(lea_r11_insn_32
) : sizeof(lea_r11_insn_64
));
4618 const size_t nop_len
= (size
== 32 ? 7 : 8);
4620 // The function starts with a comparison of the stack pointer and a
4621 // field in the TCB. This is followed by a jump.
4624 if (this->match_view(view
, view_size
, fnoffset
, cmp_insn
, cmp_insn_len
)
4625 && fnsize
> nop_len
+ 1)
4627 // We will call __morestack if the carry flag is set after this
4628 // comparison. We turn the comparison into an stc instruction
4630 view
[fnoffset
] = '\xf9';
4631 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, nop_len
);
4633 // lea NN(%rsp),%r10
4634 // lea NN(%rsp),%r11
4635 else if ((this->match_view(view
, view_size
, fnoffset
,
4636 lea_r10_insn
, lea_r10_insn_len
)
4637 || this->match_view(view
, view_size
, fnoffset
,
4638 lea_r11_insn
, lea_r11_insn_len
))
4641 // This is loading an offset from the stack pointer for a
4642 // comparison. The offset is negative, so we decrease the
4643 // offset by the amount of space we need for the stack. This
4644 // means we will avoid calling __morestack if there happens to
4645 // be plenty of space on the stack already.
4646 unsigned char* pval
= view
+ fnoffset
+ 4;
4647 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
4648 val
-= parameters
->options().split_stack_adjust_size();
4649 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
4653 if (!object
->has_no_split_stack())
4654 object
->error(_("failed to match split-stack sequence at "
4655 "section %u offset %0zx"),
4656 shndx
, static_cast<size_t>(fnoffset
));
4660 // We have to change the function so that it calls
4661 // __morestack_non_split instead of __morestack. The former will
4662 // allocate additional stack space.
4663 *from
= "__morestack";
4664 *to
= "__morestack_non_split";
4667 // The selector for x86_64 object files. Note this is never instantiated
4668 // directly. It's only used in Target_selector_x86_64_nacl, below.
4671 class Target_selector_x86_64
: public Target_selector_freebsd
4674 Target_selector_x86_64()
4675 : Target_selector_freebsd(elfcpp::EM_X86_64
, size
, false,
4677 ? "elf64-x86-64" : "elf32-x86-64"),
4679 ? "elf64-x86-64-freebsd"
4680 : "elf32-x86-64-freebsd"),
4681 (size
== 64 ? "elf_x86_64" : "elf32_x86_64"))
4685 do_instantiate_target()
4686 { return new Target_x86_64
<size
>(); }
4690 // NaCl variant. It uses different PLT contents.
4693 class Output_data_plt_x86_64_nacl
: public Output_data_plt_x86_64
<size
>
4696 Output_data_plt_x86_64_nacl(Layout
* layout
,
4697 Output_data_got
<64, false>* got
,
4698 Output_data_got_plt_x86_64
* got_plt
,
4699 Output_data_space
* got_irelative
)
4700 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4701 got
, got_plt
, got_irelative
)
4704 Output_data_plt_x86_64_nacl(Layout
* layout
,
4705 Output_data_got
<64, false>* got
,
4706 Output_data_got_plt_x86_64
* got_plt
,
4707 Output_data_space
* got_irelative
,
4708 unsigned int plt_count
)
4709 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4710 got
, got_plt
, got_irelative
,
4715 virtual unsigned int
4716 do_get_plt_entry_size() const
4717 { return plt_entry_size
; }
4720 do_add_eh_frame(Layout
* layout
)
4722 layout
->add_eh_frame_for_plt(this,
4723 this->plt_eh_frame_cie
,
4724 this->plt_eh_frame_cie_size
,
4726 plt_eh_frame_fde_size
);
4730 do_fill_first_plt_entry(unsigned char* pov
,
4731 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
4732 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
4734 virtual unsigned int
4735 do_fill_plt_entry(unsigned char* pov
,
4736 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4737 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4738 unsigned int got_offset
,
4739 unsigned int plt_offset
,
4740 unsigned int plt_index
);
4743 do_fill_tlsdesc_entry(unsigned char* pov
,
4744 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4745 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4746 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
4747 unsigned int tlsdesc_got_offset
,
4748 unsigned int plt_offset
);
4751 // The size of an entry in the PLT.
4752 static const int plt_entry_size
= 64;
4754 // The first entry in the PLT.
4755 static const unsigned char first_plt_entry
[plt_entry_size
];
4757 // Other entries in the PLT for an executable.
4758 static const unsigned char plt_entry
[plt_entry_size
];
4760 // The reserved TLSDESC entry in the PLT for an executable.
4761 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
4763 // The .eh_frame unwind information for the PLT.
4764 static const int plt_eh_frame_fde_size
= 32;
4765 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
4769 class Target_x86_64_nacl
: public Target_x86_64
<size
>
4772 Target_x86_64_nacl()
4773 : Target_x86_64
<size
>(&x86_64_nacl_info
)
4776 virtual Output_data_plt_x86_64
<size
>*
4777 do_make_data_plt(Layout
* layout
,
4778 Output_data_got
<64, false>* got
,
4779 Output_data_got_plt_x86_64
* got_plt
,
4780 Output_data_space
* got_irelative
)
4782 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4786 virtual Output_data_plt_x86_64
<size
>*
4787 do_make_data_plt(Layout
* layout
,
4788 Output_data_got
<64, false>* got
,
4789 Output_data_got_plt_x86_64
* got_plt
,
4790 Output_data_space
* got_irelative
,
4791 unsigned int plt_count
)
4793 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4799 do_code_fill(section_size_type length
) const;
4802 static const Target::Target_info x86_64_nacl_info
;
4806 const Target::Target_info Target_x86_64_nacl
<64>::x86_64_nacl_info
=
4809 false, // is_big_endian
4810 elfcpp::EM_X86_64
, // machine_code
4811 false, // has_make_symbol
4812 false, // has_resolve
4813 true, // has_code_fill
4814 true, // is_default_stack_executable
4815 true, // can_icf_inline_merge_sections
4817 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4818 0x20000, // default_text_segment_address
4819 0x10000, // abi_pagesize (overridable by -z max-page-size)
4820 0x10000, // common_pagesize (overridable by -z common-page-size)
4821 true, // isolate_execinstr
4822 0x10000000, // rosegment_gap
4823 elfcpp::SHN_UNDEF
, // small_common_shndx
4824 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
4825 0, // small_common_section_flags
4826 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
4827 NULL
, // attributes_section
4828 NULL
, // attributes_vendor
4829 "_start", // entry_symbol_name
4830 32, // hash_entry_size
4834 const Target::Target_info Target_x86_64_nacl
<32>::x86_64_nacl_info
=
4837 false, // is_big_endian
4838 elfcpp::EM_X86_64
, // machine_code
4839 false, // has_make_symbol
4840 false, // has_resolve
4841 true, // has_code_fill
4842 true, // is_default_stack_executable
4843 true, // can_icf_inline_merge_sections
4845 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
4846 0x20000, // default_text_segment_address
4847 0x10000, // abi_pagesize (overridable by -z max-page-size)
4848 0x10000, // common_pagesize (overridable by -z common-page-size)
4849 true, // isolate_execinstr
4850 0x10000000, // rosegment_gap
4851 elfcpp::SHN_UNDEF
, // small_common_shndx
4852 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
4853 0, // small_common_section_flags
4854 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
4855 NULL
, // attributes_section
4856 NULL
, // attributes_vendor
4857 "_start", // entry_symbol_name
4858 32, // hash_entry_size
4861 #define NACLMASK 0xe0 // 32-byte alignment mask.
4863 // The first entry in the PLT.
4867 Output_data_plt_x86_64_nacl
<size
>::first_plt_entry
[plt_entry_size
] =
4869 0xff, 0x35, // pushq contents of memory address
4870 0, 0, 0, 0, // replaced with address of .got + 8
4871 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
4872 0, 0, 0, 0, // replaced with address of .got + 16
4873 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4874 0x4d, 0x01, 0xfb, // add %r15, %r11
4875 0x41, 0xff, 0xe3, // jmpq *%r11
4877 // 9-byte nop sequence to pad out to the next 32-byte boundary.
4878 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
4880 // 32 bytes of nop to pad out to the standard size
4881 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4882 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4883 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4884 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4885 0x66, // excess data32 prefix
4891 Output_data_plt_x86_64_nacl
<size
>::do_fill_first_plt_entry(
4893 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4894 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
4896 memcpy(pov
, first_plt_entry
, plt_entry_size
);
4897 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
4899 - (plt_address
+ 2 + 4)));
4900 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
4902 - (plt_address
+ 9 + 4)));
4905 // Subsequent entries in the PLT.
4909 Output_data_plt_x86_64_nacl
<size
>::plt_entry
[plt_entry_size
] =
4911 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
4912 0, 0, 0, 0, // replaced with address of symbol in .got
4913 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4914 0x4d, 0x01, 0xfb, // add %r15, %r11
4915 0x41, 0xff, 0xe3, // jmpq *%r11
4917 // 15-byte nop sequence to pad out to the next 32-byte boundary.
4918 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4919 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4921 // Lazy GOT entries point here (32-byte aligned).
4922 0x68, // pushq immediate
4923 0, 0, 0, 0, // replaced with index into relocation table
4924 0xe9, // jmp relative
4925 0, 0, 0, 0, // replaced with offset to start of .plt0
4927 // 22 bytes of nop to pad out to the standard size.
4928 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4929 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4930 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
4935 Output_data_plt_x86_64_nacl
<size
>::do_fill_plt_entry(
4937 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4938 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4939 unsigned int got_offset
,
4940 unsigned int plt_offset
,
4941 unsigned int plt_index
)
4943 memcpy(pov
, plt_entry
, plt_entry_size
);
4944 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3,
4945 (got_address
+ got_offset
4946 - (plt_address
+ plt_offset
4949 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 33, plt_index
);
4950 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 38,
4951 - (plt_offset
+ 38 + 4));
4956 // The reserved TLSDESC entry in the PLT.
4960 Output_data_plt_x86_64_nacl
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
4962 0xff, 0x35, // pushq x(%rip)
4963 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
4964 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
4965 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
4966 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4967 0x4d, 0x01, 0xfb, // add %r15, %r11
4968 0x41, 0xff, 0xe3, // jmpq *%r11
4970 // 41 bytes of nop to pad out to the standard size.
4971 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4972 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4973 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4974 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4975 0x66, 0x66, // excess data32 prefixes
4976 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4981 Output_data_plt_x86_64_nacl
<size
>::do_fill_tlsdesc_entry(
4983 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4984 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4985 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
4986 unsigned int tlsdesc_got_offset
,
4987 unsigned int plt_offset
)
4989 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
4990 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
4992 - (plt_address
+ plt_offset
4994 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
4996 + tlsdesc_got_offset
4997 - (plt_address
+ plt_offset
5001 // The .eh_frame unwind information for the PLT.
5005 Output_data_plt_x86_64_nacl
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
5007 0, 0, 0, 0, // Replaced with offset to .plt.
5008 0, 0, 0, 0, // Replaced with size of .plt.
5009 0, // Augmentation size.
5010 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
5011 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
5012 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
5013 elfcpp::DW_CFA_advance_loc
+ 58, // Advance 58 to __PLT__ + 64.
5014 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
5015 13, // Block length.
5016 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
5017 elfcpp::DW_OP_breg16
, 0, // Push %rip.
5018 elfcpp::DW_OP_const1u
, 63, // Push 0x3f.
5019 elfcpp::DW_OP_and
, // & (%rip & 0x3f).
5020 elfcpp::DW_OP_const1u
, 37, // Push 0x25.
5021 elfcpp::DW_OP_ge
, // >= ((%rip & 0x3f) >= 0x25)
5022 elfcpp::DW_OP_lit3
, // Push 3.
5023 elfcpp::DW_OP_shl
, // << (((%rip & 0x3f) >= 0x25) << 3)
5024 elfcpp::DW_OP_plus
, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5025 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
5029 // Return a string used to fill a code section with nops.
5030 // For NaCl, long NOPs are only valid if they do not cross
5031 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5034 Target_x86_64_nacl
<size
>::do_code_fill(section_size_type length
) const
5036 return std::string(length
, static_cast<char>(0x90));
5039 // The selector for x86_64-nacl object files.
5042 class Target_selector_x86_64_nacl
5043 : public Target_selector_nacl
<Target_selector_x86_64
<size
>,
5044 Target_x86_64_nacl
<size
> >
5047 Target_selector_x86_64_nacl()
5048 : Target_selector_nacl
<Target_selector_x86_64
<size
>,
5049 Target_x86_64_nacl
<size
> >("x86-64",
5051 ? "elf64-x86-64-nacl"
5052 : "elf32-x86-64-nacl",
5055 : "elf32_x86_64_nacl")
5059 Target_selector_x86_64_nacl
<64> target_selector_x86_64
;
5060 Target_selector_x86_64_nacl
<32> target_selector_x32
;
5062 } // End anonymous namespace.