1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
4 // Free Software Foundation, Inc.
5 // Written by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
30 #include "parameters.h"
37 #include "copy-relocs.h"
39 #include "target-reloc.h"
40 #include "target-select.h"
52 // A class to handle the PLT data.
53 // This is an abstract base class that handles most of the linker details
54 // but does not know the actual contents of PLT entries. The derived
55 // classes below fill in those details.
58 class Output_data_plt_x86_64
: public Output_section_data
61 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
63 Output_data_plt_x86_64(Layout
* layout
, uint64_t addralign
,
64 Output_data_got
<64, false>* got
,
65 Output_data_space
* got_plt
,
66 Output_data_space
* got_irelative
)
67 : Output_section_data(addralign
), layout_(layout
), tlsdesc_rel_(NULL
),
68 irelative_rel_(NULL
), got_(got
), got_plt_(got_plt
),
69 got_irelative_(got_irelative
), count_(0), irelative_count_(0),
70 tlsdesc_got_offset_(-1U), free_list_()
71 { this->init(layout
); }
73 Output_data_plt_x86_64(Layout
* layout
, uint64_t plt_entry_size
,
74 Output_data_got
<64, false>* got
,
75 Output_data_space
* got_plt
,
76 Output_data_space
* got_irelative
,
77 unsigned int plt_count
)
78 : Output_section_data((plt_count
+ 1) * plt_entry_size
,
79 plt_entry_size
, false),
80 layout_(layout
), tlsdesc_rel_(NULL
), irelative_rel_(NULL
), got_(got
),
81 got_plt_(got_plt
), got_irelative_(got_irelative
), count_(plt_count
),
82 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
86 // Initialize the free list and reserve the first entry.
87 this->free_list_
.init((plt_count
+ 1) * plt_entry_size
, false);
88 this->free_list_
.remove(0, plt_entry_size
);
91 // Initialize the PLT section.
95 // Add an entry to the PLT.
97 add_entry(Symbol_table
*, Layout
*, Symbol
* gsym
);
99 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
101 add_local_ifunc_entry(Symbol_table
* symtab
, Layout
*,
102 Sized_relobj_file
<size
, false>* relobj
,
103 unsigned int local_sym_index
);
105 // Add the relocation for a PLT entry.
107 add_relocation(Symbol_table
*, Layout
*, Symbol
* gsym
,
108 unsigned int got_offset
);
110 // Add the reserved TLSDESC_PLT entry to the PLT.
112 reserve_tlsdesc_entry(unsigned int got_offset
)
113 { this->tlsdesc_got_offset_
= got_offset
; }
115 // Return true if a TLSDESC_PLT entry has been reserved.
117 has_tlsdesc_entry() const
118 { return this->tlsdesc_got_offset_
!= -1U; }
120 // Return the GOT offset for the reserved TLSDESC_PLT entry.
122 get_tlsdesc_got_offset() const
123 { return this->tlsdesc_got_offset_
; }
125 // Return the offset of the reserved TLSDESC_PLT entry.
127 get_tlsdesc_plt_offset() const
129 return ((this->count_
+ this->irelative_count_
+ 1)
130 * this->get_plt_entry_size());
133 // Return the .rela.plt section data.
136 { return this->rel_
; }
138 // Return where the TLSDESC relocations should go.
140 rela_tlsdesc(Layout
*);
142 // Return where the IRELATIVE relocations should go in the PLT
145 rela_irelative(Symbol_table
*, Layout
*);
147 // Return whether we created a section for IRELATIVE relocations.
149 has_irelative_section() const
150 { return this->irelative_rel_
!= NULL
; }
152 // Return the number of PLT entries.
155 { return this->count_
+ this->irelative_count_
; }
157 // Return the offset of the first non-reserved PLT entry.
159 first_plt_entry_offset()
160 { return this->get_plt_entry_size(); }
162 // Return the size of a PLT entry.
164 get_plt_entry_size() const
165 { return this->do_get_plt_entry_size(); }
167 // Reserve a slot in the PLT for an existing symbol in an incremental update.
169 reserve_slot(unsigned int plt_index
)
171 this->free_list_
.remove((plt_index
+ 1) * this->get_plt_entry_size(),
172 (plt_index
+ 2) * this->get_plt_entry_size());
175 // Return the PLT address to use for a global symbol.
177 address_for_global(const Symbol
*);
179 // Return the PLT address to use for a local symbol.
181 address_for_local(const Relobj
*, unsigned int symndx
);
183 // Add .eh_frame information for the PLT.
185 add_eh_frame(Layout
* layout
)
186 { this->do_add_eh_frame(layout
); }
189 // Fill in the first PLT entry.
191 fill_first_plt_entry(unsigned char* pov
,
192 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
193 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
194 { this->do_fill_first_plt_entry(pov
, got_address
, plt_address
); }
196 // Fill in a normal PLT entry. Returns the offset into the entry that
197 // should be the initial GOT slot value.
199 fill_plt_entry(unsigned char* pov
,
200 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
201 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
202 unsigned int got_offset
,
203 unsigned int plt_offset
,
204 unsigned int plt_index
)
206 return this->do_fill_plt_entry(pov
, got_address
, plt_address
,
207 got_offset
, plt_offset
, plt_index
);
210 // Fill in the reserved TLSDESC PLT entry.
212 fill_tlsdesc_entry(unsigned char* pov
,
213 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
214 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
215 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
216 unsigned int tlsdesc_got_offset
,
217 unsigned int plt_offset
)
219 this->do_fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
220 tlsdesc_got_offset
, plt_offset
);
224 do_get_plt_entry_size() const = 0;
227 do_fill_first_plt_entry(unsigned char* pov
,
228 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
229 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
)
233 do_fill_plt_entry(unsigned char* pov
,
234 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
235 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
236 unsigned int got_offset
,
237 unsigned int plt_offset
,
238 unsigned int plt_index
) = 0;
241 do_fill_tlsdesc_entry(unsigned char* pov
,
242 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
243 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
244 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
245 unsigned int tlsdesc_got_offset
,
246 unsigned int plt_offset
) = 0;
249 do_add_eh_frame(Layout
* layout
) = 0;
252 do_adjust_output_section(Output_section
* os
);
254 // Write to a map file.
256 do_print_to_mapfile(Mapfile
* mapfile
) const
257 { mapfile
->print_output_data(this, _("** PLT")); }
259 // The CIE of the .eh_frame unwind information for the PLT.
260 static const int plt_eh_frame_cie_size
= 16;
261 static const unsigned char plt_eh_frame_cie
[plt_eh_frame_cie_size
];
264 // Set the final size.
266 set_final_data_size();
268 // Write out the PLT data.
270 do_write(Output_file
*);
272 // A pointer to the Layout class, so that we can find the .dynamic
273 // section when we write out the GOT PLT section.
275 // The reloc section.
277 // The TLSDESC relocs, if necessary. These must follow the regular
279 Reloc_section
* tlsdesc_rel_
;
280 // The IRELATIVE relocs, if necessary. These must follow the
281 // regular PLT relocations and the TLSDESC relocations.
282 Reloc_section
* irelative_rel_
;
284 Output_data_got
<64, false>* got_
;
285 // The .got.plt section.
286 Output_data_space
* got_plt_
;
287 // The part of the .got.plt section used for IRELATIVE relocs.
288 Output_data_space
* got_irelative_
;
289 // The number of PLT entries.
291 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
292 // follow the regular PLT entries.
293 unsigned int irelative_count_
;
294 // Offset of the reserved TLSDESC_GOT entry when needed.
295 unsigned int tlsdesc_got_offset_
;
296 // List of available regions within the section, for incremental
298 Free_list free_list_
;
302 class Output_data_plt_x86_64_standard
: public Output_data_plt_x86_64
<size
>
305 Output_data_plt_x86_64_standard(Layout
* layout
,
306 Output_data_got
<64, false>* got
,
307 Output_data_space
* got_plt
,
308 Output_data_space
* got_irelative
)
309 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
310 got
, got_plt
, got_irelative
)
313 Output_data_plt_x86_64_standard(Layout
* layout
,
314 Output_data_got
<64, false>* got
,
315 Output_data_space
* got_plt
,
316 Output_data_space
* got_irelative
,
317 unsigned int plt_count
)
318 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
319 got
, got_plt
, got_irelative
,
325 do_get_plt_entry_size() const
326 { return plt_entry_size
; }
329 do_add_eh_frame(Layout
* layout
)
331 layout
->add_eh_frame_for_plt(this,
332 this->plt_eh_frame_cie
,
333 this->plt_eh_frame_cie_size
,
335 plt_eh_frame_fde_size
);
339 do_fill_first_plt_entry(unsigned char* pov
,
340 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
341 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
344 do_fill_plt_entry(unsigned char* pov
,
345 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
346 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
347 unsigned int got_offset
,
348 unsigned int plt_offset
,
349 unsigned int plt_index
);
352 do_fill_tlsdesc_entry(unsigned char* pov
,
353 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
354 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
355 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
356 unsigned int tlsdesc_got_offset
,
357 unsigned int plt_offset
);
360 // The size of an entry in the PLT.
361 static const int plt_entry_size
= 16;
363 // The first entry in the PLT.
364 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
365 // procedure linkage table for both programs and shared objects."
366 static const unsigned char first_plt_entry
[plt_entry_size
];
368 // Other entries in the PLT for an executable.
369 static const unsigned char plt_entry
[plt_entry_size
];
371 // The reserved TLSDESC entry in the PLT for an executable.
372 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
374 // The .eh_frame unwind information for the PLT.
375 static const int plt_eh_frame_fde_size
= 32;
376 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
379 // The x86_64 target class.
381 // http://www.x86-64.org/documentation/abi.pdf
382 // TLS info comes from
383 // http://people.redhat.com/drepper/tls.pdf
384 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
387 class Target_x86_64
: public Sized_target
<size
, false>
390 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
391 // uses only Elf64_Rela relocation entries with explicit addends."
392 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
394 Target_x86_64(const Target::Target_info
* info
= &x86_64_info
)
395 : Sized_target
<size
, false>(info
),
396 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_irelative_(NULL
),
397 got_tlsdesc_(NULL
), global_offset_table_(NULL
), rela_dyn_(NULL
),
398 rela_irelative_(NULL
), copy_relocs_(elfcpp::R_X86_64_COPY
),
399 dynbss_(NULL
), got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
400 tls_base_symbol_defined_(false)
403 // Hook for a new output section.
405 do_new_output_section(Output_section
*) const;
407 // Scan the relocations to look for symbol adjustments.
409 gc_process_relocs(Symbol_table
* symtab
,
411 Sized_relobj_file
<size
, false>* object
,
412 unsigned int data_shndx
,
413 unsigned int sh_type
,
414 const unsigned char* prelocs
,
416 Output_section
* output_section
,
417 bool needs_special_offset_handling
,
418 size_t local_symbol_count
,
419 const unsigned char* plocal_symbols
);
421 // Scan the relocations to look for symbol adjustments.
423 scan_relocs(Symbol_table
* symtab
,
425 Sized_relobj_file
<size
, false>* object
,
426 unsigned int data_shndx
,
427 unsigned int sh_type
,
428 const unsigned char* prelocs
,
430 Output_section
* output_section
,
431 bool needs_special_offset_handling
,
432 size_t local_symbol_count
,
433 const unsigned char* plocal_symbols
);
435 // Finalize the sections.
437 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
439 // Return the value to use for a dynamic which requires special
442 do_dynsym_value(const Symbol
*) const;
444 // Relocate a section.
446 relocate_section(const Relocate_info
<size
, false>*,
447 unsigned int sh_type
,
448 const unsigned char* prelocs
,
450 Output_section
* output_section
,
451 bool needs_special_offset_handling
,
453 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
454 section_size_type view_size
,
455 const Reloc_symbol_changes
*);
457 // Scan the relocs during a relocatable link.
459 scan_relocatable_relocs(Symbol_table
* symtab
,
461 Sized_relobj_file
<size
, false>* object
,
462 unsigned int data_shndx
,
463 unsigned int sh_type
,
464 const unsigned char* prelocs
,
466 Output_section
* output_section
,
467 bool needs_special_offset_handling
,
468 size_t local_symbol_count
,
469 const unsigned char* plocal_symbols
,
470 Relocatable_relocs
*);
472 // Emit relocations for a section.
475 const Relocate_info
<size
, false>*,
476 unsigned int sh_type
,
477 const unsigned char* prelocs
,
479 Output_section
* output_section
,
480 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
481 const Relocatable_relocs
*,
483 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
484 section_size_type view_size
,
485 unsigned char* reloc_view
,
486 section_size_type reloc_view_size
);
488 // Return a string used to fill a code section with nops.
490 do_code_fill(section_size_type length
) const;
492 // Return whether SYM is defined by the ABI.
494 do_is_defined_by_abi(const Symbol
* sym
) const
495 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
497 // Return the symbol index to use for a target specific relocation.
498 // The only target specific relocation is R_X86_64_TLSDESC for a
499 // local symbol, which is an absolute reloc.
501 do_reloc_symbol_index(void*, unsigned int r_type
) const
503 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
507 // Return the addend to use for a target specific relocation.
509 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
511 // Return the PLT section.
513 do_plt_address_for_global(const Symbol
* gsym
) const
514 { return this->plt_section()->address_for_global(gsym
); }
517 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
518 { return this->plt_section()->address_for_local(relobj
, symndx
); }
520 // This function should be defined in targets that can use relocation
521 // types to determine (implemented in local_reloc_may_be_function_pointer
522 // and global_reloc_may_be_function_pointer)
523 // if a function's pointer is taken. ICF uses this in safe mode to only
524 // fold those functions whose pointer is defintely not taken. For x86_64
525 // pie binaries, safe ICF cannot be done by looking at relocation types.
527 do_can_check_for_function_pointers() const
528 { return !parameters
->options().pie(); }
530 // Return the base for a DW_EH_PE_datarel encoding.
532 do_ehframe_datarel_base() const;
534 // Adjust -fsplit-stack code which calls non-split-stack code.
536 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
537 section_offset_type fnoffset
, section_size_type fnsize
,
538 unsigned char* view
, section_size_type view_size
,
539 std::string
* from
, std::string
* to
) const;
541 // Return the size of the GOT section.
545 gold_assert(this->got_
!= NULL
);
546 return this->got_
->data_size();
549 // Return the number of entries in the GOT.
551 got_entry_count() const
553 if (this->got_
== NULL
)
555 return this->got_size() / 8;
558 // Return the number of entries in the PLT.
560 plt_entry_count() const;
562 // Return the offset of the first non-reserved PLT entry.
564 first_plt_entry_offset() const;
566 // Return the size of each PLT entry.
568 plt_entry_size() const;
570 // Create the GOT section for an incremental update.
571 Output_data_got_base
*
572 init_got_plt_for_update(Symbol_table
* symtab
,
574 unsigned int got_count
,
575 unsigned int plt_count
);
577 // Reserve a GOT entry for a local symbol, and regenerate any
578 // necessary dynamic relocations.
580 reserve_local_got_entry(unsigned int got_index
,
581 Sized_relobj
<size
, false>* obj
,
583 unsigned int got_type
);
585 // Reserve a GOT entry for a global symbol, and regenerate any
586 // necessary dynamic relocations.
588 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
589 unsigned int got_type
);
591 // Register an existing PLT entry for a global symbol.
593 register_global_plt_entry(Symbol_table
*, Layout
*, unsigned int plt_index
,
596 // Force a COPY relocation for a given symbol.
598 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
600 // Apply an incremental relocation.
602 apply_relocation(const Relocate_info
<size
, false>* relinfo
,
603 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
605 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
608 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
609 section_size_type view_size
);
611 // Add a new reloc argument, returning the index in the vector.
613 add_tlsdesc_info(Sized_relobj_file
<size
, false>* object
, unsigned int r_sym
)
615 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
616 return this->tlsdesc_reloc_info_
.size() - 1;
619 Output_data_plt_x86_64
<size
>*
620 make_data_plt(Layout
* layout
,
621 Output_data_got
<64, false>* got
,
622 Output_data_space
* got_plt
,
623 Output_data_space
* got_irelative
)
625 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
628 Output_data_plt_x86_64
<size
>*
629 make_data_plt(Layout
* layout
,
630 Output_data_got
<64, false>* got
,
631 Output_data_space
* got_plt
,
632 Output_data_space
* got_irelative
,
633 unsigned int plt_count
)
635 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
,
639 virtual Output_data_plt_x86_64
<size
>*
640 do_make_data_plt(Layout
* layout
,
641 Output_data_got
<64, false>* got
,
642 Output_data_space
* got_plt
,
643 Output_data_space
* got_irelative
)
645 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
649 virtual Output_data_plt_x86_64
<size
>*
650 do_make_data_plt(Layout
* layout
,
651 Output_data_got
<64, false>* got
,
652 Output_data_space
* got_plt
,
653 Output_data_space
* got_irelative
,
654 unsigned int plt_count
)
656 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
662 // The class which scans relocations.
667 : issued_non_pic_error_(false)
671 get_reference_flags(unsigned int r_type
);
674 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
675 Sized_relobj_file
<size
, false>* object
,
676 unsigned int data_shndx
,
677 Output_section
* output_section
,
678 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
679 const elfcpp::Sym
<size
, false>& lsym
,
683 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
684 Sized_relobj_file
<size
, false>* object
,
685 unsigned int data_shndx
,
686 Output_section
* output_section
,
687 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
691 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
692 Target_x86_64
* target
,
693 Sized_relobj_file
<size
, false>* object
,
694 unsigned int data_shndx
,
695 Output_section
* output_section
,
696 const elfcpp::Rela
<size
, false>& reloc
,
698 const elfcpp::Sym
<size
, false>& lsym
);
701 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
702 Target_x86_64
* target
,
703 Sized_relobj_file
<size
, false>* object
,
704 unsigned int data_shndx
,
705 Output_section
* output_section
,
706 const elfcpp::Rela
<size
, false>& reloc
,
712 unsupported_reloc_local(Sized_relobj_file
<size
, false>*,
713 unsigned int r_type
);
716 unsupported_reloc_global(Sized_relobj_file
<size
, false>*,
717 unsigned int r_type
, Symbol
*);
720 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
723 possible_function_pointer_reloc(unsigned int r_type
);
726 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, false>*,
727 unsigned int r_type
);
729 // Whether we have issued an error about a non-PIC compilation.
730 bool issued_non_pic_error_
;
733 // The class which implements relocation.
738 : skip_call_tls_get_addr_(false)
743 if (this->skip_call_tls_get_addr_
)
745 // FIXME: This needs to specify the location somehow.
746 gold_error(_("missing expected TLS relocation"));
750 // Do a relocation. Return false if the caller should not issue
751 // any warnings about this relocation.
753 relocate(const Relocate_info
<size
, false>*, Target_x86_64
*,
755 size_t relnum
, const elfcpp::Rela
<size
, false>&,
756 unsigned int r_type
, const Sized_symbol
<size
>*,
757 const Symbol_value
<size
>*,
758 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
762 // Do a TLS relocation.
764 relocate_tls(const Relocate_info
<size
, false>*, Target_x86_64
*,
765 size_t relnum
, const elfcpp::Rela
<size
, false>&,
766 unsigned int r_type
, const Sized_symbol
<size
>*,
767 const Symbol_value
<size
>*,
768 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
771 // Do a TLS General-Dynamic to Initial-Exec transition.
773 tls_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
774 Output_segment
* tls_segment
,
775 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
776 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
778 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
779 section_size_type view_size
);
781 // Do a TLS General-Dynamic to Local-Exec transition.
783 tls_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
784 Output_segment
* tls_segment
,
785 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
786 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
788 section_size_type view_size
);
790 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
792 tls_desc_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
793 Output_segment
* tls_segment
,
794 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
795 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
797 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
798 section_size_type view_size
);
800 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
802 tls_desc_gd_to_le(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 section_size_type view_size
);
809 // Do a TLS Local-Dynamic to Local-Exec transition.
811 tls_ld_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
812 Output_segment
* tls_segment
,
813 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
814 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
816 section_size_type view_size
);
818 // Do a TLS Initial-Exec to Local-Exec transition.
820 tls_ie_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
821 Output_segment
* tls_segment
,
822 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
823 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
825 section_size_type view_size
);
827 // This is set if we should skip the next reloc, which should be a
828 // PLT32 reloc against ___tls_get_addr.
829 bool skip_call_tls_get_addr_
;
832 // A class which returns the size required for a relocation type,
833 // used while scanning relocs during a relocatable link.
834 class Relocatable_size_for_reloc
838 get_size_for_reloc(unsigned int, Relobj
*);
841 // Adjust TLS relocation type based on the options and whether this
842 // is a local symbol.
843 static tls::Tls_optimization
844 optimize_tls_reloc(bool is_final
, int r_type
);
846 // Get the GOT section, creating it if necessary.
847 Output_data_got
<64, false>*
848 got_section(Symbol_table
*, Layout
*);
850 // Get the GOT PLT section.
852 got_plt_section() const
854 gold_assert(this->got_plt_
!= NULL
);
855 return this->got_plt_
;
858 // Get the GOT section for TLSDESC entries.
859 Output_data_got
<64, false>*
860 got_tlsdesc_section() const
862 gold_assert(this->got_tlsdesc_
!= NULL
);
863 return this->got_tlsdesc_
;
866 // Create the PLT section.
868 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
870 // Create a PLT entry for a global symbol.
872 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
874 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
876 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
877 Sized_relobj_file
<size
, false>* relobj
,
878 unsigned int local_sym_index
);
880 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
882 define_tls_base_symbol(Symbol_table
*, Layout
*);
884 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
886 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
888 // Create a GOT entry for the TLS module index.
890 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
891 Sized_relobj_file
<size
, false>* object
);
893 // Get the PLT section.
894 Output_data_plt_x86_64
<size
>*
897 gold_assert(this->plt_
!= NULL
);
901 // Get the dynamic reloc section, creating it if necessary.
903 rela_dyn_section(Layout
*);
905 // Get the section to use for TLSDESC relocations.
907 rela_tlsdesc_section(Layout
*) const;
909 // Get the section to use for IRELATIVE relocations.
911 rela_irelative_section(Layout
*);
913 // Add a potential copy relocation.
915 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
916 Sized_relobj_file
<size
, false>* object
,
917 unsigned int shndx
, Output_section
* output_section
,
918 Symbol
* sym
, const elfcpp::Rela
<size
, false>& reloc
)
920 this->copy_relocs_
.copy_reloc(symtab
, layout
,
921 symtab
->get_sized_symbol
<size
>(sym
),
922 object
, shndx
, output_section
,
923 reloc
, this->rela_dyn_section(layout
));
926 // Information about this specific target which we pass to the
927 // general Target structure.
928 static const Target::Target_info x86_64_info
;
930 // The types of GOT entries needed for this platform.
931 // These values are exposed to the ABI in an incremental link.
932 // Do not renumber existing values without changing the version
933 // number of the .gnu_incremental_inputs section.
936 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
937 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
938 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
939 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
942 // This type is used as the argument to the target specific
943 // relocation routines. The only target specific reloc is
944 // R_X86_64_TLSDESC against a local symbol.
947 Tlsdesc_info(Sized_relobj_file
<size
, false>* a_object
, unsigned int a_r_sym
)
948 : object(a_object
), r_sym(a_r_sym
)
951 // The object in which the local symbol is defined.
952 Sized_relobj_file
<size
, false>* object
;
953 // The local symbol index in the object.
958 Output_data_got
<64, false>* got_
;
960 Output_data_plt_x86_64
<size
>* plt_
;
961 // The GOT PLT section.
962 Output_data_space
* got_plt_
;
963 // The GOT section for IRELATIVE relocations.
964 Output_data_space
* got_irelative_
;
965 // The GOT section for TLSDESC relocations.
966 Output_data_got
<64, false>* got_tlsdesc_
;
967 // The _GLOBAL_OFFSET_TABLE_ symbol.
968 Symbol
* global_offset_table_
;
969 // The dynamic reloc section.
970 Reloc_section
* rela_dyn_
;
971 // The section to use for IRELATIVE relocs.
972 Reloc_section
* rela_irelative_
;
973 // Relocs saved to avoid a COPY reloc.
974 Copy_relocs
<elfcpp::SHT_RELA
, size
, false> copy_relocs_
;
975 // Space for variables copied with a COPY reloc.
976 Output_data_space
* dynbss_
;
977 // Offset of the GOT entry for the TLS module index.
978 unsigned int got_mod_index_offset_
;
979 // We handle R_X86_64_TLSDESC against a local symbol as a target
980 // specific relocation. Here we store the object and local symbol
981 // index for the relocation.
982 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
983 // True if the _TLS_MODULE_BASE_ symbol has been defined.
984 bool tls_base_symbol_defined_
;
988 const Target::Target_info Target_x86_64
<64>::x86_64_info
=
991 false, // is_big_endian
992 elfcpp::EM_X86_64
, // machine_code
993 false, // has_make_symbol
994 false, // has_resolve
995 true, // has_code_fill
996 true, // is_default_stack_executable
997 true, // can_icf_inline_merge_sections
999 "/lib/ld64.so.1", // program interpreter
1000 0x400000, // default_text_segment_address
1001 0x1000, // abi_pagesize (overridable by -z max-page-size)
1002 0x1000, // common_pagesize (overridable by -z common-page-size)
1003 false, // isolate_execinstr
1005 elfcpp::SHN_UNDEF
, // small_common_shndx
1006 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1007 0, // small_common_section_flags
1008 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1009 NULL
, // attributes_section
1010 NULL
, // attributes_vendor
1011 "_start" // entry_symbol_name
1015 const Target::Target_info Target_x86_64
<32>::x86_64_info
=
1018 false, // is_big_endian
1019 elfcpp::EM_X86_64
, // machine_code
1020 false, // has_make_symbol
1021 false, // has_resolve
1022 true, // has_code_fill
1023 true, // is_default_stack_executable
1024 true, // can_icf_inline_merge_sections
1026 "/libx32/ldx32.so.1", // program interpreter
1027 0x400000, // default_text_segment_address
1028 0x1000, // abi_pagesize (overridable by -z max-page-size)
1029 0x1000, // common_pagesize (overridable by -z common-page-size)
1030 false, // isolate_execinstr
1032 elfcpp::SHN_UNDEF
, // small_common_shndx
1033 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1034 0, // small_common_section_flags
1035 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1036 NULL
, // attributes_section
1037 NULL
, // attributes_vendor
1038 "_start" // entry_symbol_name
1041 // This is called when a new output section is created. This is where
1042 // we handle the SHF_X86_64_LARGE.
1046 Target_x86_64
<size
>::do_new_output_section(Output_section
* os
) const
1048 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
1049 os
->set_is_large_section();
1052 // Get the GOT section, creating it if necessary.
1055 Output_data_got
<64, false>*
1056 Target_x86_64
<size
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
1058 if (this->got_
== NULL
)
1060 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1062 // When using -z now, we can treat .got.plt as a relro section.
1063 // Without -z now, it is modified after program startup by lazy
1065 bool is_got_plt_relro
= parameters
->options().now();
1066 Output_section_order got_order
= (is_got_plt_relro
1068 : ORDER_RELRO_LAST
);
1069 Output_section_order got_plt_order
= (is_got_plt_relro
1071 : ORDER_NON_RELRO_FIRST
);
1073 this->got_
= new Output_data_got
<64, false>();
1075 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1077 | elfcpp::SHF_WRITE
),
1078 this->got_
, got_order
, true);
1080 this->got_plt_
= new Output_data_space(8, "** GOT PLT");
1081 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1083 | elfcpp::SHF_WRITE
),
1084 this->got_plt_
, got_plt_order
,
1087 // The first three entries are reserved.
1088 this->got_plt_
->set_current_data_size(3 * 8);
1090 if (!is_got_plt_relro
)
1092 // Those bytes can go into the relro segment.
1093 layout
->increase_relro(3 * 8);
1096 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1097 this->global_offset_table_
=
1098 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1099 Symbol_table::PREDEFINED
,
1101 0, 0, elfcpp::STT_OBJECT
,
1103 elfcpp::STV_HIDDEN
, 0,
1106 // If there are any IRELATIVE relocations, they get GOT entries
1107 // in .got.plt after the jump slot entries.
1108 this->got_irelative_
= new Output_data_space(8, "** GOT IRELATIVE PLT");
1109 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1111 | elfcpp::SHF_WRITE
),
1112 this->got_irelative_
,
1113 got_plt_order
, is_got_plt_relro
);
1115 // If there are any TLSDESC relocations, they get GOT entries in
1116 // .got.plt after the jump slot and IRELATIVE entries.
1117 this->got_tlsdesc_
= new Output_data_got
<64, false>();
1118 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1120 | elfcpp::SHF_WRITE
),
1122 got_plt_order
, is_got_plt_relro
);
1128 // Get the dynamic reloc section, creating it if necessary.
1131 typename Target_x86_64
<size
>::Reloc_section
*
1132 Target_x86_64
<size
>::rela_dyn_section(Layout
* layout
)
1134 if (this->rela_dyn_
== NULL
)
1136 gold_assert(layout
!= NULL
);
1137 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1138 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1139 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1140 ORDER_DYNAMIC_RELOCS
, false);
1142 return this->rela_dyn_
;
1145 // Get the section to use for IRELATIVE relocs, creating it if
1146 // necessary. These go in .rela.dyn, but only after all other dynamic
1147 // relocations. They need to follow the other dynamic relocations so
1148 // that they can refer to global variables initialized by those
1152 typename Target_x86_64
<size
>::Reloc_section
*
1153 Target_x86_64
<size
>::rela_irelative_section(Layout
* layout
)
1155 if (this->rela_irelative_
== NULL
)
1157 // Make sure we have already created the dynamic reloc section.
1158 this->rela_dyn_section(layout
);
1159 this->rela_irelative_
= new Reloc_section(false);
1160 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1161 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
1162 ORDER_DYNAMIC_RELOCS
, false);
1163 gold_assert(this->rela_dyn_
->output_section()
1164 == this->rela_irelative_
->output_section());
1166 return this->rela_irelative_
;
1169 // Initialize the PLT section.
1173 Output_data_plt_x86_64
<size
>::init(Layout
* layout
)
1175 this->rel_
= new Reloc_section(false);
1176 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1177 elfcpp::SHF_ALLOC
, this->rel_
,
1178 ORDER_DYNAMIC_PLT_RELOCS
, false);
1183 Output_data_plt_x86_64
<size
>::do_adjust_output_section(Output_section
* os
)
1185 os
->set_entsize(this->get_plt_entry_size());
1188 // Add an entry to the PLT.
1192 Output_data_plt_x86_64
<size
>::add_entry(Symbol_table
* symtab
, Layout
* layout
,
1195 gold_assert(!gsym
->has_plt_offset());
1197 unsigned int plt_index
;
1199 section_offset_type got_offset
;
1201 unsigned int* pcount
;
1202 unsigned int offset
;
1203 unsigned int reserved
;
1204 Output_data_space
* got
;
1205 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1206 && gsym
->can_use_relative_reloc(false))
1208 pcount
= &this->irelative_count_
;
1211 got
= this->got_irelative_
;
1215 pcount
= &this->count_
;
1218 got
= this->got_plt_
;
1221 if (!this->is_data_size_valid())
1223 // Note that when setting the PLT offset for a non-IRELATIVE
1224 // entry we skip the initial reserved PLT entry.
1225 plt_index
= *pcount
+ offset
;
1226 plt_offset
= plt_index
* this->get_plt_entry_size();
1230 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1231 gold_assert(got_offset
== got
->current_data_size());
1233 // Every PLT entry needs a GOT entry which points back to the PLT
1234 // entry (this will be changed by the dynamic linker, normally
1235 // lazily when the function is called).
1236 got
->set_current_data_size(got_offset
+ 8);
1240 // FIXME: This is probably not correct for IRELATIVE relocs.
1242 // For incremental updates, find an available slot.
1243 plt_offset
= this->free_list_
.allocate(this->get_plt_entry_size(),
1244 this->get_plt_entry_size(), 0);
1245 if (plt_offset
== -1)
1246 gold_fallback(_("out of patch space (PLT);"
1247 " relink with --incremental-full"));
1249 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1250 // can be calculated from the PLT index, adjusting for the three
1251 // reserved entries at the beginning of the GOT.
1252 plt_index
= plt_offset
/ this->get_plt_entry_size() - 1;
1253 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1256 gsym
->set_plt_offset(plt_offset
);
1258 // Every PLT entry needs a reloc.
1259 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
1261 // Note that we don't need to save the symbol. The contents of the
1262 // PLT are independent of which symbols are used. The symbols only
1263 // appear in the relocations.
1266 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1271 Output_data_plt_x86_64
<size
>::add_local_ifunc_entry(
1272 Symbol_table
* symtab
,
1274 Sized_relobj_file
<size
, false>* relobj
,
1275 unsigned int local_sym_index
)
1277 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
1278 ++this->irelative_count_
;
1280 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
1282 // Every PLT entry needs a GOT entry which points back to the PLT
1284 this->got_irelative_
->set_current_data_size(got_offset
+ 8);
1286 // Every PLT entry needs a reloc.
1287 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1288 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
1289 elfcpp::R_X86_64_IRELATIVE
,
1290 this->got_irelative_
, got_offset
, 0);
1295 // Add the relocation for a PLT entry.
1299 Output_data_plt_x86_64
<size
>::add_relocation(Symbol_table
* symtab
,
1302 unsigned int got_offset
)
1304 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1305 && gsym
->can_use_relative_reloc(false))
1307 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1308 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
1309 this->got_irelative_
, got_offset
, 0);
1313 gsym
->set_needs_dynsym_entry();
1314 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
1319 // Return where the TLSDESC relocations should go, creating it if
1320 // necessary. These follow the JUMP_SLOT relocations.
1323 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1324 Output_data_plt_x86_64
<size
>::rela_tlsdesc(Layout
* layout
)
1326 if (this->tlsdesc_rel_
== NULL
)
1328 this->tlsdesc_rel_
= new Reloc_section(false);
1329 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1330 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
1331 ORDER_DYNAMIC_PLT_RELOCS
, false);
1332 gold_assert(this->tlsdesc_rel_
->output_section()
1333 == this->rel_
->output_section());
1335 return this->tlsdesc_rel_
;
1338 // Return where the IRELATIVE relocations should go in the PLT. These
1339 // follow the JUMP_SLOT and the TLSDESC relocations.
1342 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1343 Output_data_plt_x86_64
<size
>::rela_irelative(Symbol_table
* symtab
,
1346 if (this->irelative_rel_
== NULL
)
1348 // Make sure we have a place for the TLSDESC relocations, in
1349 // case we see any later on.
1350 this->rela_tlsdesc(layout
);
1351 this->irelative_rel_
= new Reloc_section(false);
1352 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1353 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
1354 ORDER_DYNAMIC_PLT_RELOCS
, false);
1355 gold_assert(this->irelative_rel_
->output_section()
1356 == this->rel_
->output_section());
1358 if (parameters
->doing_static_link())
1360 // A statically linked executable will only have a .rela.plt
1361 // section to hold R_X86_64_IRELATIVE relocs for
1362 // STT_GNU_IFUNC symbols. The library will use these
1363 // symbols to locate the IRELATIVE relocs at program startup
1365 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
1366 Symbol_table::PREDEFINED
,
1367 this->irelative_rel_
, 0, 0,
1368 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1369 elfcpp::STV_HIDDEN
, 0, false, true);
1370 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
1371 Symbol_table::PREDEFINED
,
1372 this->irelative_rel_
, 0, 0,
1373 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1374 elfcpp::STV_HIDDEN
, 0, true, true);
1377 return this->irelative_rel_
;
1380 // Return the PLT address to use for a global symbol.
1384 Output_data_plt_x86_64
<size
>::address_for_global(const Symbol
* gsym
)
1386 uint64_t offset
= 0;
1387 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1388 && gsym
->can_use_relative_reloc(false))
1389 offset
= (this->count_
+ 1) * this->get_plt_entry_size();
1390 return this->address() + offset
+ gsym
->plt_offset();
1393 // Return the PLT address to use for a local symbol. These are always
1394 // IRELATIVE relocs.
1398 Output_data_plt_x86_64
<size
>::address_for_local(const Relobj
* object
,
1401 return (this->address()
1402 + (this->count_
+ 1) * this->get_plt_entry_size()
1403 + object
->local_plt_offset(r_sym
));
1406 // Set the final size.
1409 Output_data_plt_x86_64
<size
>::set_final_data_size()
1411 unsigned int count
= this->count_
+ this->irelative_count_
;
1412 if (this->has_tlsdesc_entry())
1414 this->set_data_size((count
+ 1) * this->get_plt_entry_size());
1417 // The first entry in the PLT for an executable.
1421 Output_data_plt_x86_64_standard
<size
>::first_plt_entry
[plt_entry_size
] =
1423 // From AMD64 ABI Draft 0.98, page 76
1424 0xff, 0x35, // pushq contents of memory address
1425 0, 0, 0, 0, // replaced with address of .got + 8
1426 0xff, 0x25, // jmp indirect
1427 0, 0, 0, 0, // replaced with address of .got + 16
1428 0x90, 0x90, 0x90, 0x90 // noop (x4)
1433 Output_data_plt_x86_64_standard
<size
>::do_fill_first_plt_entry(
1435 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1436 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
1438 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1439 // We do a jmp relative to the PC at the end of this instruction.
1440 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1442 - (plt_address
+ 6)));
1443 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1445 - (plt_address
+ 12)));
1448 // Subsequent entries in the PLT for an executable.
1452 Output_data_plt_x86_64_standard
<size
>::plt_entry
[plt_entry_size
] =
1454 // From AMD64 ABI Draft 0.98, page 76
1455 0xff, 0x25, // jmpq indirect
1456 0, 0, 0, 0, // replaced with address of symbol in .got
1457 0x68, // pushq immediate
1458 0, 0, 0, 0, // replaced with offset into relocation table
1459 0xe9, // jmpq relative
1460 0, 0, 0, 0 // replaced with offset to start of .plt
1465 Output_data_plt_x86_64_standard
<size
>::do_fill_plt_entry(
1467 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1468 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1469 unsigned int got_offset
,
1470 unsigned int plt_offset
,
1471 unsigned int plt_index
)
1473 memcpy(pov
, plt_entry
, plt_entry_size
);
1474 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1475 (got_address
+ got_offset
1476 - (plt_address
+ plt_offset
1479 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1480 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1481 - (plt_offset
+ plt_entry_size
));
1486 // The reserved TLSDESC entry in the PLT for an executable.
1490 Output_data_plt_x86_64_standard
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
1492 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1493 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1494 0xff, 0x35, // pushq x(%rip)
1495 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1496 0xff, 0x25, // jmpq *y(%rip)
1497 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1504 Output_data_plt_x86_64_standard
<size
>::do_fill_tlsdesc_entry(
1506 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1507 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1508 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
1509 unsigned int tlsdesc_got_offset
,
1510 unsigned int plt_offset
)
1512 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1513 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1515 - (plt_address
+ plt_offset
1517 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1519 + tlsdesc_got_offset
1520 - (plt_address
+ plt_offset
1524 // The .eh_frame unwind information for the PLT.
1528 Output_data_plt_x86_64
<size
>::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
1531 'z', // Augmentation: augmentation size included.
1532 'R', // Augmentation: FDE encoding included.
1533 '\0', // End of augmentation string.
1534 1, // Code alignment factor.
1535 0x78, // Data alignment factor.
1536 16, // Return address column.
1537 1, // Augmentation size.
1538 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
1539 | elfcpp::DW_EH_PE_sdata4
),
1540 elfcpp::DW_CFA_def_cfa
, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1541 elfcpp::DW_CFA_offset
+ 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1542 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
1548 Output_data_plt_x86_64_standard
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
1550 0, 0, 0, 0, // Replaced with offset to .plt.
1551 0, 0, 0, 0, // Replaced with size of .plt.
1552 0, // Augmentation size.
1553 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
1554 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
1555 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
1556 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
1557 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
1558 11, // Block length.
1559 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
1560 elfcpp::DW_OP_breg16
, 0, // Push %rip.
1561 elfcpp::DW_OP_lit15
, // Push 0xf.
1562 elfcpp::DW_OP_and
, // & (%rip & 0xf).
1563 elfcpp::DW_OP_lit11
, // Push 0xb.
1564 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 0xb)
1565 elfcpp::DW_OP_lit3
, // Push 3.
1566 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 0xb) << 3)
1567 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1568 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
1574 // Write out the PLT. This uses the hand-coded instructions above,
1575 // and adjusts them as needed. This is specified by the AMD64 ABI.
1579 Output_data_plt_x86_64
<size
>::do_write(Output_file
* of
)
1581 const off_t offset
= this->offset();
1582 const section_size_type oview_size
=
1583 convert_to_section_size_type(this->data_size());
1584 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1586 const off_t got_file_offset
= this->got_plt_
->offset();
1587 gold_assert(parameters
->incremental_update()
1588 || (got_file_offset
+ this->got_plt_
->data_size()
1589 == this->got_irelative_
->offset()));
1590 const section_size_type got_size
=
1591 convert_to_section_size_type(this->got_plt_
->data_size()
1592 + this->got_irelative_
->data_size());
1593 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1596 unsigned char* pov
= oview
;
1598 // The base address of the .plt section.
1599 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
1600 // The base address of the .got section.
1601 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
= this->got_
->address();
1602 // The base address of the PLT portion of the .got section,
1603 // which is where the GOT pointer will point, and where the
1604 // three reserved GOT entries are located.
1605 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
1606 = this->got_plt_
->address();
1608 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
1609 pov
+= this->get_plt_entry_size();
1611 unsigned char* got_pov
= got_view
;
1613 // The first entry in the GOT is the address of the .dynamic section
1614 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1615 // We saved space for them when we created the section in
1616 // Target_x86_64::got_section.
1617 Output_section
* dynamic
= this->layout_
->dynamic_section();
1618 uint32_t dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
1619 elfcpp::Swap
<64, false>::writeval(got_pov
, dynamic_addr
);
1621 memset(got_pov
, 0, 16);
1624 unsigned int plt_offset
= this->get_plt_entry_size();
1625 unsigned int got_offset
= 24;
1626 const unsigned int count
= this->count_
+ this->irelative_count_
;
1627 for (unsigned int plt_index
= 0;
1630 pov
+= this->get_plt_entry_size(),
1632 plt_offset
+= this->get_plt_entry_size(),
1635 // Set and adjust the PLT entry itself.
1636 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
1637 got_address
, plt_address
,
1638 got_offset
, plt_offset
,
1641 // Set the entry in the GOT.
1642 elfcpp::Swap
<64, false>::writeval(got_pov
,
1643 plt_address
+ plt_offset
+ lazy_offset
);
1646 if (this->has_tlsdesc_entry())
1648 // Set and adjust the reserved TLSDESC PLT entry.
1649 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
1650 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
1651 tlsdesc_got_offset
, plt_offset
);
1652 pov
+= this->get_plt_entry_size();
1655 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1656 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1658 of
->write_output_view(offset
, oview_size
, oview
);
1659 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1662 // Create the PLT section.
1666 Target_x86_64
<size
>::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1668 if (this->plt_
== NULL
)
1670 // Create the GOT sections first.
1671 this->got_section(symtab
, layout
);
1673 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
1674 this->got_irelative_
);
1676 // Add unwind information if requested.
1677 if (parameters
->options().ld_generated_unwind_info())
1678 this->plt_
->add_eh_frame(layout
);
1680 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1682 | elfcpp::SHF_EXECINSTR
),
1683 this->plt_
, ORDER_PLT
, false);
1685 // Make the sh_info field of .rela.plt point to .plt.
1686 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1687 rela_plt_os
->set_info_section(this->plt_
->output_section());
1691 // Return the section for TLSDESC relocations.
1694 typename Target_x86_64
<size
>::Reloc_section
*
1695 Target_x86_64
<size
>::rela_tlsdesc_section(Layout
* layout
) const
1697 return this->plt_section()->rela_tlsdesc(layout
);
1700 // Create a PLT entry for a global symbol.
1704 Target_x86_64
<size
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1707 if (gsym
->has_plt_offset())
1710 if (this->plt_
== NULL
)
1711 this->make_plt_section(symtab
, layout
);
1713 this->plt_
->add_entry(symtab
, layout
, gsym
);
1716 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1720 Target_x86_64
<size
>::make_local_ifunc_plt_entry(
1721 Symbol_table
* symtab
, Layout
* layout
,
1722 Sized_relobj_file
<size
, false>* relobj
,
1723 unsigned int local_sym_index
)
1725 if (relobj
->local_has_plt_offset(local_sym_index
))
1727 if (this->plt_
== NULL
)
1728 this->make_plt_section(symtab
, layout
);
1729 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
1732 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1735 // Return the number of entries in the PLT.
1739 Target_x86_64
<size
>::plt_entry_count() const
1741 if (this->plt_
== NULL
)
1743 return this->plt_
->entry_count();
1746 // Return the offset of the first non-reserved PLT entry.
1750 Target_x86_64
<size
>::first_plt_entry_offset() const
1752 return this->plt_
->first_plt_entry_offset();
1755 // Return the size of each PLT entry.
1759 Target_x86_64
<size
>::plt_entry_size() const
1761 return this->plt_
->get_plt_entry_size();
1764 // Create the GOT and PLT sections for an incremental update.
1767 Output_data_got_base
*
1768 Target_x86_64
<size
>::init_got_plt_for_update(Symbol_table
* symtab
,
1770 unsigned int got_count
,
1771 unsigned int plt_count
)
1773 gold_assert(this->got_
== NULL
);
1775 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
1776 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1778 | elfcpp::SHF_WRITE
),
1779 this->got_
, ORDER_RELRO_LAST
,
1782 // Add the three reserved entries.
1783 this->got_plt_
= new Output_data_space((plt_count
+ 3) * 8, 8, "** GOT PLT");
1784 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1786 | elfcpp::SHF_WRITE
),
1787 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
1790 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1791 this->global_offset_table_
=
1792 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1793 Symbol_table::PREDEFINED
,
1795 0, 0, elfcpp::STT_OBJECT
,
1797 elfcpp::STV_HIDDEN
, 0,
1800 // If there are any TLSDESC relocations, they get GOT entries in
1801 // .got.plt after the jump slot entries.
1802 // FIXME: Get the count for TLSDESC entries.
1803 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
1804 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1805 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1807 ORDER_NON_RELRO_FIRST
, false);
1809 // If there are any IRELATIVE relocations, they get GOT entries in
1810 // .got.plt after the jump slot and TLSDESC entries.
1811 this->got_irelative_
= new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1812 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1813 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1814 this->got_irelative_
,
1815 ORDER_NON_RELRO_FIRST
, false);
1817 // Create the PLT section.
1818 this->plt_
= this->make_data_plt(layout
, this->got_
,
1820 this->got_irelative_
,
1823 // Add unwind information if requested.
1824 if (parameters
->options().ld_generated_unwind_info())
1825 this->plt_
->add_eh_frame(layout
);
1827 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1828 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1829 this->plt_
, ORDER_PLT
, false);
1831 // Make the sh_info field of .rela.plt point to .plt.
1832 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1833 rela_plt_os
->set_info_section(this->plt_
->output_section());
1835 // Create the rela_dyn section.
1836 this->rela_dyn_section(layout
);
1841 // Reserve a GOT entry for a local symbol, and regenerate any
1842 // necessary dynamic relocations.
1846 Target_x86_64
<size
>::reserve_local_got_entry(
1847 unsigned int got_index
,
1848 Sized_relobj
<size
, false>* obj
,
1850 unsigned int got_type
)
1852 unsigned int got_offset
= got_index
* 8;
1853 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1855 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
1858 case GOT_TYPE_STANDARD
:
1859 if (parameters
->options().output_is_position_independent())
1860 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
1861 this->got_
, got_offset
, 0, false);
1863 case GOT_TYPE_TLS_OFFSET
:
1864 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
1865 this->got_
, got_offset
, 0);
1867 case GOT_TYPE_TLS_PAIR
:
1868 this->got_
->reserve_slot(got_index
+ 1);
1869 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
1870 this->got_
, got_offset
, 0);
1872 case GOT_TYPE_TLS_DESC
:
1873 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1874 // this->got_->reserve_slot(got_index + 1);
1875 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1876 // this->got_, got_offset, 0);
1883 // Reserve a GOT entry for a global symbol, and regenerate any
1884 // necessary dynamic relocations.
1888 Target_x86_64
<size
>::reserve_global_got_entry(unsigned int got_index
,
1890 unsigned int got_type
)
1892 unsigned int got_offset
= got_index
* 8;
1893 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1895 this->got_
->reserve_global(got_index
, gsym
, got_type
);
1898 case GOT_TYPE_STANDARD
:
1899 if (!gsym
->final_value_is_known())
1901 if (gsym
->is_from_dynobj()
1902 || gsym
->is_undefined()
1903 || gsym
->is_preemptible()
1904 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1905 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
1906 this->got_
, got_offset
, 0);
1908 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1909 this->got_
, got_offset
, 0, false);
1912 case GOT_TYPE_TLS_OFFSET
:
1913 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
1914 this->got_
, got_offset
, 0, false);
1916 case GOT_TYPE_TLS_PAIR
:
1917 this->got_
->reserve_slot(got_index
+ 1);
1918 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
1919 this->got_
, got_offset
, 0, false);
1920 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
1921 this->got_
, got_offset
+ 8, 0, false);
1923 case GOT_TYPE_TLS_DESC
:
1924 this->got_
->reserve_slot(got_index
+ 1);
1925 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
1926 this->got_
, got_offset
, 0, false);
1933 // Register an existing PLT entry for a global symbol.
1937 Target_x86_64
<size
>::register_global_plt_entry(Symbol_table
* symtab
,
1939 unsigned int plt_index
,
1942 gold_assert(this->plt_
!= NULL
);
1943 gold_assert(!gsym
->has_plt_offset());
1945 this->plt_
->reserve_slot(plt_index
);
1947 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
1949 unsigned int got_offset
= (plt_index
+ 3) * 8;
1950 this->plt_
->add_relocation(symtab
, layout
, gsym
, got_offset
);
1953 // Force a COPY relocation for a given symbol.
1957 Target_x86_64
<size
>::emit_copy_reloc(
1958 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
1960 this->copy_relocs_
.emit_copy_reloc(symtab
,
1961 symtab
->get_sized_symbol
<size
>(sym
),
1964 this->rela_dyn_section(NULL
));
1967 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1971 Target_x86_64
<size
>::define_tls_base_symbol(Symbol_table
* symtab
,
1974 if (this->tls_base_symbol_defined_
)
1977 Output_segment
* tls_segment
= layout
->tls_segment();
1978 if (tls_segment
!= NULL
)
1980 bool is_exec
= parameters
->options().output_is_executable();
1981 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1982 Symbol_table::PREDEFINED
,
1986 elfcpp::STV_HIDDEN
, 0,
1988 ? Symbol::SEGMENT_END
1989 : Symbol::SEGMENT_START
),
1992 this->tls_base_symbol_defined_
= true;
1995 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1999 Target_x86_64
<size
>::reserve_tlsdesc_entries(Symbol_table
* symtab
,
2002 if (this->plt_
== NULL
)
2003 this->make_plt_section(symtab
, layout
);
2005 if (!this->plt_
->has_tlsdesc_entry())
2007 // Allocate the TLSDESC_GOT entry.
2008 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2009 unsigned int got_offset
= got
->add_constant(0);
2011 // Allocate the TLSDESC_PLT entry.
2012 this->plt_
->reserve_tlsdesc_entry(got_offset
);
2016 // Create a GOT entry for the TLS module index.
2020 Target_x86_64
<size
>::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
2021 Sized_relobj_file
<size
, false>* object
)
2023 if (this->got_mod_index_offset_
== -1U)
2025 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
2026 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
2027 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2028 unsigned int got_offset
= got
->add_constant(0);
2029 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
2031 got
->add_constant(0);
2032 this->got_mod_index_offset_
= got_offset
;
2034 return this->got_mod_index_offset_
;
2037 // Optimize the TLS relocation type based on what we know about the
2038 // symbol. IS_FINAL is true if the final address of this symbol is
2039 // known at link time.
2042 tls::Tls_optimization
2043 Target_x86_64
<size
>::optimize_tls_reloc(bool is_final
, int r_type
)
2045 // If we are generating a shared library, then we can't do anything
2047 if (parameters
->options().shared())
2048 return tls::TLSOPT_NONE
;
2052 case elfcpp::R_X86_64_TLSGD
:
2053 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2054 case elfcpp::R_X86_64_TLSDESC_CALL
:
2055 // These are General-Dynamic which permits fully general TLS
2056 // access. Since we know that we are generating an executable,
2057 // we can convert this to Initial-Exec. If we also know that
2058 // this is a local symbol, we can further switch to Local-Exec.
2060 return tls::TLSOPT_TO_LE
;
2061 return tls::TLSOPT_TO_IE
;
2063 case elfcpp::R_X86_64_TLSLD
:
2064 // This is Local-Dynamic, which refers to a local symbol in the
2065 // dynamic TLS block. Since we know that we generating an
2066 // executable, we can switch to Local-Exec.
2067 return tls::TLSOPT_TO_LE
;
2069 case elfcpp::R_X86_64_DTPOFF32
:
2070 case elfcpp::R_X86_64_DTPOFF64
:
2071 // Another Local-Dynamic reloc.
2072 return tls::TLSOPT_TO_LE
;
2074 case elfcpp::R_X86_64_GOTTPOFF
:
2075 // These are Initial-Exec relocs which get the thread offset
2076 // from the GOT. If we know that we are linking against the
2077 // local symbol, we can switch to Local-Exec, which links the
2078 // thread offset into the instruction.
2080 return tls::TLSOPT_TO_LE
;
2081 return tls::TLSOPT_NONE
;
2083 case elfcpp::R_X86_64_TPOFF32
:
2084 // When we already have Local-Exec, there is nothing further we
2086 return tls::TLSOPT_NONE
;
2093 // Get the Reference_flags for a particular relocation.
2097 Target_x86_64
<size
>::Scan::get_reference_flags(unsigned int r_type
)
2101 case elfcpp::R_X86_64_NONE
:
2102 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2103 case elfcpp::R_X86_64_GNU_VTENTRY
:
2104 case elfcpp::R_X86_64_GOTPC32
:
2105 case elfcpp::R_X86_64_GOTPC64
:
2106 // No symbol reference.
2109 case elfcpp::R_X86_64_64
:
2110 case elfcpp::R_X86_64_32
:
2111 case elfcpp::R_X86_64_32S
:
2112 case elfcpp::R_X86_64_16
:
2113 case elfcpp::R_X86_64_8
:
2114 return Symbol::ABSOLUTE_REF
;
2116 case elfcpp::R_X86_64_PC64
:
2117 case elfcpp::R_X86_64_PC32
:
2118 case elfcpp::R_X86_64_PC16
:
2119 case elfcpp::R_X86_64_PC8
:
2120 case elfcpp::R_X86_64_GOTOFF64
:
2121 return Symbol::RELATIVE_REF
;
2123 case elfcpp::R_X86_64_PLT32
:
2124 case elfcpp::R_X86_64_PLTOFF64
:
2125 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
2127 case elfcpp::R_X86_64_GOT64
:
2128 case elfcpp::R_X86_64_GOT32
:
2129 case elfcpp::R_X86_64_GOTPCREL64
:
2130 case elfcpp::R_X86_64_GOTPCREL
:
2131 case elfcpp::R_X86_64_GOTPLT64
:
2133 return Symbol::ABSOLUTE_REF
;
2135 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2136 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2137 case elfcpp::R_X86_64_TLSDESC_CALL
:
2138 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2139 case elfcpp::R_X86_64_DTPOFF32
:
2140 case elfcpp::R_X86_64_DTPOFF64
:
2141 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2142 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2143 return Symbol::TLS_REF
;
2145 case elfcpp::R_X86_64_COPY
:
2146 case elfcpp::R_X86_64_GLOB_DAT
:
2147 case elfcpp::R_X86_64_JUMP_SLOT
:
2148 case elfcpp::R_X86_64_RELATIVE
:
2149 case elfcpp::R_X86_64_IRELATIVE
:
2150 case elfcpp::R_X86_64_TPOFF64
:
2151 case elfcpp::R_X86_64_DTPMOD64
:
2152 case elfcpp::R_X86_64_TLSDESC
:
2153 case elfcpp::R_X86_64_SIZE32
:
2154 case elfcpp::R_X86_64_SIZE64
:
2156 // Not expected. We will give an error later.
2161 // Report an unsupported relocation against a local symbol.
2165 Target_x86_64
<size
>::Scan::unsupported_reloc_local(
2166 Sized_relobj_file
<size
, false>* object
,
2167 unsigned int r_type
)
2169 gold_error(_("%s: unsupported reloc %u against local symbol"),
2170 object
->name().c_str(), r_type
);
2173 // We are about to emit a dynamic relocation of type R_TYPE. If the
2174 // dynamic linker does not support it, issue an error. The GNU linker
2175 // only issues a non-PIC error for an allocated read-only section.
2176 // Here we know the section is allocated, but we don't know that it is
2177 // read-only. But we check for all the relocation types which the
2178 // glibc dynamic linker supports, so it seems appropriate to issue an
2179 // error even if the section is not read-only. If GSYM is not NULL,
2180 // it is the symbol the relocation is against; if it is NULL, the
2181 // relocation is against a local symbol.
2185 Target_x86_64
<size
>::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
2190 // These are the relocation types supported by glibc for x86_64
2191 // which should always work.
2192 case elfcpp::R_X86_64_RELATIVE
:
2193 case elfcpp::R_X86_64_IRELATIVE
:
2194 case elfcpp::R_X86_64_GLOB_DAT
:
2195 case elfcpp::R_X86_64_JUMP_SLOT
:
2196 case elfcpp::R_X86_64_DTPMOD64
:
2197 case elfcpp::R_X86_64_DTPOFF64
:
2198 case elfcpp::R_X86_64_TPOFF64
:
2199 case elfcpp::R_X86_64_64
:
2200 case elfcpp::R_X86_64_COPY
:
2203 // glibc supports these reloc types, but they can overflow.
2204 case elfcpp::R_X86_64_PC32
:
2205 // A PC relative reference is OK against a local symbol or if
2206 // the symbol is defined locally.
2208 || (!gsym
->is_from_dynobj()
2209 && !gsym
->is_undefined()
2210 && !gsym
->is_preemptible()))
2213 case elfcpp::R_X86_64_32
:
2214 // R_X86_64_32 is OK for x32.
2215 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2217 if (this->issued_non_pic_error_
)
2219 gold_assert(parameters
->options().output_is_position_independent());
2221 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
2222 "overflow at runtime; recompile with -fPIC"));
2224 object
->error(_("requires dynamic %s reloc against '%s' which may "
2225 "overflow at runtime; recompile with -fPIC"),
2226 (r_type
== elfcpp::R_X86_64_32
2230 this->issued_non_pic_error_
= true;
2234 // This prevents us from issuing more than one error per reloc
2235 // section. But we can still wind up issuing more than one
2236 // error per object file.
2237 if (this->issued_non_pic_error_
)
2239 gold_assert(parameters
->options().output_is_position_independent());
2240 object
->error(_("requires unsupported dynamic reloc %u; "
2241 "recompile with -fPIC"),
2243 this->issued_non_pic_error_
= true;
2246 case elfcpp::R_X86_64_NONE
:
2251 // Return whether we need to make a PLT entry for a relocation of the
2252 // given type against a STT_GNU_IFUNC symbol.
2256 Target_x86_64
<size
>::Scan::reloc_needs_plt_for_ifunc(
2257 Sized_relobj_file
<size
, false>* object
,
2258 unsigned int r_type
)
2260 int flags
= Scan::get_reference_flags(r_type
);
2261 if (flags
& Symbol::TLS_REF
)
2262 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2263 object
->name().c_str(), r_type
);
2267 // Scan a relocation for a local symbol.
2271 Target_x86_64
<size
>::Scan::local(Symbol_table
* symtab
,
2273 Target_x86_64
<size
>* target
,
2274 Sized_relobj_file
<size
, false>* object
,
2275 unsigned int data_shndx
,
2276 Output_section
* output_section
,
2277 const elfcpp::Rela
<size
, false>& reloc
,
2278 unsigned int r_type
,
2279 const elfcpp::Sym
<size
, false>& lsym
,
2285 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2286 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
2287 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
2289 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2290 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
2295 case elfcpp::R_X86_64_NONE
:
2296 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2297 case elfcpp::R_X86_64_GNU_VTENTRY
:
2300 case elfcpp::R_X86_64_64
:
2301 // If building a shared library (or a position-independent
2302 // executable), we need to create a dynamic relocation for this
2303 // location. The relocation applied at link time will apply the
2304 // link-time value, so we flag the location with an
2305 // R_X86_64_RELATIVE relocation so the dynamic loader can
2306 // relocate it easily.
2307 if (parameters
->options().output_is_position_independent())
2309 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2310 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2311 rela_dyn
->add_local_relative(object
, r_sym
,
2313 ? elfcpp::R_X86_64_RELATIVE64
2314 : elfcpp::R_X86_64_RELATIVE
),
2315 output_section
, data_shndx
,
2316 reloc
.get_r_offset(),
2317 reloc
.get_r_addend(), is_ifunc
);
2321 case elfcpp::R_X86_64_32
:
2322 case elfcpp::R_X86_64_32S
:
2323 case elfcpp::R_X86_64_16
:
2324 case elfcpp::R_X86_64_8
:
2325 // If building a shared library (or a position-independent
2326 // executable), we need to create a dynamic relocation for this
2327 // location. We can't use an R_X86_64_RELATIVE relocation
2328 // because that is always a 64-bit relocation.
2329 if (parameters
->options().output_is_position_independent())
2331 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2332 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2334 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2335 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2336 rela_dyn
->add_local_relative(object
, r_sym
,
2337 elfcpp::R_X86_64_RELATIVE
,
2338 output_section
, data_shndx
,
2339 reloc
.get_r_offset(),
2340 reloc
.get_r_addend(), is_ifunc
);
2344 this->check_non_pic(object
, r_type
, NULL
);
2346 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2347 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2348 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
2349 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
2350 data_shndx
, reloc
.get_r_offset(),
2351 reloc
.get_r_addend());
2354 gold_assert(lsym
.get_st_value() == 0);
2355 unsigned int shndx
= lsym
.get_st_shndx();
2357 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
2360 object
->error(_("section symbol %u has bad shndx %u"),
2363 rela_dyn
->add_local_section(object
, shndx
,
2364 r_type
, output_section
,
2365 data_shndx
, reloc
.get_r_offset(),
2366 reloc
.get_r_addend());
2371 case elfcpp::R_X86_64_PC64
:
2372 case elfcpp::R_X86_64_PC32
:
2373 case elfcpp::R_X86_64_PC16
:
2374 case elfcpp::R_X86_64_PC8
:
2377 case elfcpp::R_X86_64_PLT32
:
2378 // Since we know this is a local symbol, we can handle this as a
2382 case elfcpp::R_X86_64_GOTPC32
:
2383 case elfcpp::R_X86_64_GOTOFF64
:
2384 case elfcpp::R_X86_64_GOTPC64
:
2385 case elfcpp::R_X86_64_PLTOFF64
:
2386 // We need a GOT section.
2387 target
->got_section(symtab
, layout
);
2388 // For PLTOFF64, we'd normally want a PLT section, but since we
2389 // know this is a local symbol, no PLT is needed.
2392 case elfcpp::R_X86_64_GOT64
:
2393 case elfcpp::R_X86_64_GOT32
:
2394 case elfcpp::R_X86_64_GOTPCREL64
:
2395 case elfcpp::R_X86_64_GOTPCREL
:
2396 case elfcpp::R_X86_64_GOTPLT64
:
2398 // The symbol requires a GOT entry.
2399 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2400 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2402 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2403 // lets function pointers compare correctly with shared
2404 // libraries. Otherwise we would need an IRELATIVE reloc.
2407 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
2409 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
2412 // If we are generating a shared object, we need to add a
2413 // dynamic relocation for this symbol's GOT entry.
2414 if (parameters
->options().output_is_position_independent())
2416 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2417 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2418 if (r_type
!= elfcpp::R_X86_64_GOT32
)
2420 unsigned int got_offset
=
2421 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
2422 rela_dyn
->add_local_relative(object
, r_sym
,
2423 elfcpp::R_X86_64_RELATIVE
,
2424 got
, got_offset
, 0, is_ifunc
);
2428 this->check_non_pic(object
, r_type
, NULL
);
2430 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
2431 rela_dyn
->add_local(
2432 object
, r_sym
, r_type
, got
,
2433 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
2437 // For GOTPLT64, we'd normally want a PLT section, but since
2438 // we know this is a local symbol, no PLT is needed.
2442 case elfcpp::R_X86_64_COPY
:
2443 case elfcpp::R_X86_64_GLOB_DAT
:
2444 case elfcpp::R_X86_64_JUMP_SLOT
:
2445 case elfcpp::R_X86_64_RELATIVE
:
2446 case elfcpp::R_X86_64_IRELATIVE
:
2447 // These are outstanding tls relocs, which are unexpected when linking
2448 case elfcpp::R_X86_64_TPOFF64
:
2449 case elfcpp::R_X86_64_DTPMOD64
:
2450 case elfcpp::R_X86_64_TLSDESC
:
2451 gold_error(_("%s: unexpected reloc %u in object file"),
2452 object
->name().c_str(), r_type
);
2455 // These are initial tls relocs, which are expected when linking
2456 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2457 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2458 case elfcpp::R_X86_64_TLSDESC_CALL
:
2459 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2460 case elfcpp::R_X86_64_DTPOFF32
:
2461 case elfcpp::R_X86_64_DTPOFF64
:
2462 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2463 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2465 bool output_is_shared
= parameters
->options().shared();
2466 const tls::Tls_optimization optimized_type
2467 = Target_x86_64
<size
>::optimize_tls_reloc(!output_is_shared
,
2471 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2472 if (optimized_type
== tls::TLSOPT_NONE
)
2474 // Create a pair of GOT entries for the module index and
2475 // dtv-relative offset.
2476 Output_data_got
<64, false>* got
2477 = target
->got_section(symtab
, layout
);
2478 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2479 unsigned int shndx
= lsym
.get_st_shndx();
2481 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2483 object
->error(_("local symbol %u has bad shndx %u"),
2486 got
->add_local_pair_with_rel(object
, r_sym
,
2489 target
->rela_dyn_section(layout
),
2490 elfcpp::R_X86_64_DTPMOD64
);
2492 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2493 unsupported_reloc_local(object
, r_type
);
2496 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2497 target
->define_tls_base_symbol(symtab
, layout
);
2498 if (optimized_type
== tls::TLSOPT_NONE
)
2500 // Create reserved PLT and GOT entries for the resolver.
2501 target
->reserve_tlsdesc_entries(symtab
, layout
);
2503 // Generate a double GOT entry with an
2504 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2505 // is resolved lazily, so the GOT entry needs to be in
2506 // an area in .got.plt, not .got. Call got_section to
2507 // make sure the section has been created.
2508 target
->got_section(symtab
, layout
);
2509 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2510 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2511 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
2513 unsigned int got_offset
= got
->add_constant(0);
2514 got
->add_constant(0);
2515 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
2517 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2518 // We store the arguments we need in a vector, and
2519 // use the index into the vector as the parameter
2520 // to pass to the target specific routines.
2521 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
2522 void* arg
= reinterpret_cast<void*>(intarg
);
2523 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
2524 got
, got_offset
, 0);
2527 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2528 unsupported_reloc_local(object
, r_type
);
2531 case elfcpp::R_X86_64_TLSDESC_CALL
:
2534 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2535 if (optimized_type
== tls::TLSOPT_NONE
)
2537 // Create a GOT entry for the module index.
2538 target
->got_mod_index_entry(symtab
, layout
, object
);
2540 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2541 unsupported_reloc_local(object
, r_type
);
2544 case elfcpp::R_X86_64_DTPOFF32
:
2545 case elfcpp::R_X86_64_DTPOFF64
:
2548 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2549 layout
->set_has_static_tls();
2550 if (optimized_type
== tls::TLSOPT_NONE
)
2552 // Create a GOT entry for the tp-relative offset.
2553 Output_data_got
<64, false>* got
2554 = target
->got_section(symtab
, layout
);
2555 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2556 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
2557 target
->rela_dyn_section(layout
),
2558 elfcpp::R_X86_64_TPOFF64
);
2560 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2561 unsupported_reloc_local(object
, r_type
);
2564 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2565 layout
->set_has_static_tls();
2566 if (output_is_shared
)
2567 unsupported_reloc_local(object
, r_type
);
2576 case elfcpp::R_X86_64_SIZE32
:
2577 case elfcpp::R_X86_64_SIZE64
:
2579 gold_error(_("%s: unsupported reloc %u against local symbol"),
2580 object
->name().c_str(), r_type
);
2586 // Report an unsupported relocation against a global symbol.
2590 Target_x86_64
<size
>::Scan::unsupported_reloc_global(
2591 Sized_relobj_file
<size
, false>* object
,
2592 unsigned int r_type
,
2595 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2596 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
2599 // Returns true if this relocation type could be that of a function pointer.
2602 Target_x86_64
<size
>::Scan::possible_function_pointer_reloc(unsigned int r_type
)
2606 case elfcpp::R_X86_64_64
:
2607 case elfcpp::R_X86_64_32
:
2608 case elfcpp::R_X86_64_32S
:
2609 case elfcpp::R_X86_64_16
:
2610 case elfcpp::R_X86_64_8
:
2611 case elfcpp::R_X86_64_GOT64
:
2612 case elfcpp::R_X86_64_GOT32
:
2613 case elfcpp::R_X86_64_GOTPCREL64
:
2614 case elfcpp::R_X86_64_GOTPCREL
:
2615 case elfcpp::R_X86_64_GOTPLT64
:
2623 // For safe ICF, scan a relocation for a local symbol to check if it
2624 // corresponds to a function pointer being taken. In that case mark
2625 // the function whose pointer was taken as not foldable.
2629 Target_x86_64
<size
>::Scan::local_reloc_may_be_function_pointer(
2632 Target_x86_64
<size
>* ,
2633 Sized_relobj_file
<size
, false>* ,
2636 const elfcpp::Rela
<size
, false>& ,
2637 unsigned int r_type
,
2638 const elfcpp::Sym
<size
, false>&)
2640 // When building a shared library, do not fold any local symbols as it is
2641 // not possible to distinguish pointer taken versus a call by looking at
2642 // the relocation types.
2643 return (parameters
->options().shared()
2644 || possible_function_pointer_reloc(r_type
));
2647 // For safe ICF, scan a relocation for a global symbol to check if it
2648 // corresponds to a function pointer being taken. In that case mark
2649 // the function whose pointer was taken as not foldable.
2653 Target_x86_64
<size
>::Scan::global_reloc_may_be_function_pointer(
2656 Target_x86_64
<size
>* ,
2657 Sized_relobj_file
<size
, false>* ,
2660 const elfcpp::Rela
<size
, false>& ,
2661 unsigned int r_type
,
2664 // When building a shared library, do not fold symbols whose visibility
2665 // is hidden, internal or protected.
2666 return ((parameters
->options().shared()
2667 && (gsym
->visibility() == elfcpp::STV_INTERNAL
2668 || gsym
->visibility() == elfcpp::STV_PROTECTED
2669 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
2670 || possible_function_pointer_reloc(r_type
));
2673 // Scan a relocation for a global symbol.
2677 Target_x86_64
<size
>::Scan::global(Symbol_table
* symtab
,
2679 Target_x86_64
<size
>* target
,
2680 Sized_relobj_file
<size
, false>* object
,
2681 unsigned int data_shndx
,
2682 Output_section
* output_section
,
2683 const elfcpp::Rela
<size
, false>& reloc
,
2684 unsigned int r_type
,
2687 // A STT_GNU_IFUNC symbol may require a PLT entry.
2688 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2689 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
2690 target
->make_plt_entry(symtab
, layout
, gsym
);
2694 case elfcpp::R_X86_64_NONE
:
2695 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2696 case elfcpp::R_X86_64_GNU_VTENTRY
:
2699 case elfcpp::R_X86_64_64
:
2700 case elfcpp::R_X86_64_32
:
2701 case elfcpp::R_X86_64_32S
:
2702 case elfcpp::R_X86_64_16
:
2703 case elfcpp::R_X86_64_8
:
2705 // Make a PLT entry if necessary.
2706 if (gsym
->needs_plt_entry())
2708 target
->make_plt_entry(symtab
, layout
, gsym
);
2709 // Since this is not a PC-relative relocation, we may be
2710 // taking the address of a function. In that case we need to
2711 // set the entry in the dynamic symbol table to the address of
2713 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
2714 gsym
->set_needs_dynsym_value();
2716 // Make a dynamic relocation if necessary.
2717 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2719 if (gsym
->may_need_copy_reloc())
2721 target
->copy_reloc(symtab
, layout
, object
,
2722 data_shndx
, output_section
, gsym
, reloc
);
2724 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
2725 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
2726 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2727 && gsym
->can_use_relative_reloc(false)
2728 && !gsym
->is_from_dynobj()
2729 && !gsym
->is_undefined()
2730 && !gsym
->is_preemptible())
2732 // Use an IRELATIVE reloc for a locally defined
2733 // STT_GNU_IFUNC symbol. This makes a function
2734 // address in a PIE executable match the address in a
2735 // shared library that it links against.
2736 Reloc_section
* rela_dyn
=
2737 target
->rela_irelative_section(layout
);
2738 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
2739 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
2740 output_section
, object
,
2742 reloc
.get_r_offset(),
2743 reloc
.get_r_addend());
2745 else if (r_type
== elfcpp::R_X86_64_64
2746 && gsym
->can_use_relative_reloc(false))
2748 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2749 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2750 output_section
, object
,
2752 reloc
.get_r_offset(),
2753 reloc
.get_r_addend(), false);
2757 this->check_non_pic(object
, r_type
, gsym
);
2758 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2759 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2760 data_shndx
, reloc
.get_r_offset(),
2761 reloc
.get_r_addend());
2767 case elfcpp::R_X86_64_PC64
:
2768 case elfcpp::R_X86_64_PC32
:
2769 case elfcpp::R_X86_64_PC16
:
2770 case elfcpp::R_X86_64_PC8
:
2772 // Make a PLT entry if necessary.
2773 if (gsym
->needs_plt_entry())
2774 target
->make_plt_entry(symtab
, layout
, gsym
);
2775 // Make a dynamic relocation if necessary.
2776 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2778 if (gsym
->may_need_copy_reloc())
2780 target
->copy_reloc(symtab
, layout
, object
,
2781 data_shndx
, output_section
, gsym
, reloc
);
2785 this->check_non_pic(object
, r_type
, gsym
);
2786 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2787 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2788 data_shndx
, reloc
.get_r_offset(),
2789 reloc
.get_r_addend());
2795 case elfcpp::R_X86_64_GOT64
:
2796 case elfcpp::R_X86_64_GOT32
:
2797 case elfcpp::R_X86_64_GOTPCREL64
:
2798 case elfcpp::R_X86_64_GOTPCREL
:
2799 case elfcpp::R_X86_64_GOTPLT64
:
2801 // The symbol requires a GOT entry.
2802 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2803 if (gsym
->final_value_is_known())
2805 // For a STT_GNU_IFUNC symbol we want the PLT address.
2806 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
2807 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2809 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2813 // If this symbol is not fully resolved, we need to add a
2814 // dynamic relocation for it.
2815 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2817 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2819 // 1) The symbol may be defined in some other module.
2821 // 2) We are building a shared library and this is a
2822 // protected symbol; using GLOB_DAT means that the dynamic
2823 // linker can use the address of the PLT in the main
2824 // executable when appropriate so that function address
2825 // comparisons work.
2827 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2828 // code, again so that function address comparisons work.
2829 if (gsym
->is_from_dynobj()
2830 || gsym
->is_undefined()
2831 || gsym
->is_preemptible()
2832 || (gsym
->visibility() == elfcpp::STV_PROTECTED
2833 && parameters
->options().shared())
2834 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
2835 && parameters
->options().output_is_position_independent()))
2836 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2837 elfcpp::R_X86_64_GLOB_DAT
);
2840 // For a STT_GNU_IFUNC symbol we want to write the PLT
2841 // offset into the GOT, so that function pointer
2842 // comparisons work correctly.
2844 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
2845 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2848 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2849 // Tell the dynamic linker to use the PLT address
2850 // when resolving relocations.
2851 if (gsym
->is_from_dynobj()
2852 && !parameters
->options().shared())
2853 gsym
->set_needs_dynsym_value();
2857 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2858 rela_dyn
->add_global_relative(gsym
,
2859 elfcpp::R_X86_64_RELATIVE
,
2860 got
, got_off
, 0, false);
2864 // For GOTPLT64, we also need a PLT entry (but only if the
2865 // symbol is not fully resolved).
2866 if (r_type
== elfcpp::R_X86_64_GOTPLT64
2867 && !gsym
->final_value_is_known())
2868 target
->make_plt_entry(symtab
, layout
, gsym
);
2872 case elfcpp::R_X86_64_PLT32
:
2873 // If the symbol is fully resolved, this is just a PC32 reloc.
2874 // Otherwise we need a PLT entry.
2875 if (gsym
->final_value_is_known())
2877 // If building a shared library, we can also skip the PLT entry
2878 // if the symbol is defined in the output file and is protected
2880 if (gsym
->is_defined()
2881 && !gsym
->is_from_dynobj()
2882 && !gsym
->is_preemptible())
2884 target
->make_plt_entry(symtab
, layout
, gsym
);
2887 case elfcpp::R_X86_64_GOTPC32
:
2888 case elfcpp::R_X86_64_GOTOFF64
:
2889 case elfcpp::R_X86_64_GOTPC64
:
2890 case elfcpp::R_X86_64_PLTOFF64
:
2891 // We need a GOT section.
2892 target
->got_section(symtab
, layout
);
2893 // For PLTOFF64, we also need a PLT entry (but only if the
2894 // symbol is not fully resolved).
2895 if (r_type
== elfcpp::R_X86_64_PLTOFF64
2896 && !gsym
->final_value_is_known())
2897 target
->make_plt_entry(symtab
, layout
, gsym
);
2900 case elfcpp::R_X86_64_COPY
:
2901 case elfcpp::R_X86_64_GLOB_DAT
:
2902 case elfcpp::R_X86_64_JUMP_SLOT
:
2903 case elfcpp::R_X86_64_RELATIVE
:
2904 case elfcpp::R_X86_64_IRELATIVE
:
2905 // These are outstanding tls relocs, which are unexpected when linking
2906 case elfcpp::R_X86_64_TPOFF64
:
2907 case elfcpp::R_X86_64_DTPMOD64
:
2908 case elfcpp::R_X86_64_TLSDESC
:
2909 gold_error(_("%s: unexpected reloc %u in object file"),
2910 object
->name().c_str(), r_type
);
2913 // These are initial tls relocs, which are expected for global()
2914 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2915 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2916 case elfcpp::R_X86_64_TLSDESC_CALL
:
2917 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2918 case elfcpp::R_X86_64_DTPOFF32
:
2919 case elfcpp::R_X86_64_DTPOFF64
:
2920 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2921 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2923 const bool is_final
= gsym
->final_value_is_known();
2924 const tls::Tls_optimization optimized_type
2925 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
2928 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2929 if (optimized_type
== tls::TLSOPT_NONE
)
2931 // Create a pair of GOT entries for the module index and
2932 // dtv-relative offset.
2933 Output_data_got
<64, false>* got
2934 = target
->got_section(symtab
, layout
);
2935 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
2936 target
->rela_dyn_section(layout
),
2937 elfcpp::R_X86_64_DTPMOD64
,
2938 elfcpp::R_X86_64_DTPOFF64
);
2940 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2942 // Create a GOT entry for the tp-relative offset.
2943 Output_data_got
<64, false>* got
2944 = target
->got_section(symtab
, layout
);
2945 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
2946 target
->rela_dyn_section(layout
),
2947 elfcpp::R_X86_64_TPOFF64
);
2949 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2950 unsupported_reloc_global(object
, r_type
, gsym
);
2953 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2954 target
->define_tls_base_symbol(symtab
, layout
);
2955 if (optimized_type
== tls::TLSOPT_NONE
)
2957 // Create reserved PLT and GOT entries for the resolver.
2958 target
->reserve_tlsdesc_entries(symtab
, layout
);
2960 // Create a double GOT entry with an R_X86_64_TLSDESC
2961 // reloc. The R_X86_64_TLSDESC reloc is resolved
2962 // lazily, so the GOT entry needs to be in an area in
2963 // .got.plt, not .got. Call got_section to make sure
2964 // the section has been created.
2965 target
->got_section(symtab
, layout
);
2966 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2967 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2968 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
2969 elfcpp::R_X86_64_TLSDESC
, 0);
2971 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2973 // Create a GOT entry for the tp-relative offset.
2974 Output_data_got
<64, false>* got
2975 = target
->got_section(symtab
, layout
);
2976 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
2977 target
->rela_dyn_section(layout
),
2978 elfcpp::R_X86_64_TPOFF64
);
2980 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2981 unsupported_reloc_global(object
, r_type
, gsym
);
2984 case elfcpp::R_X86_64_TLSDESC_CALL
:
2987 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2988 if (optimized_type
== tls::TLSOPT_NONE
)
2990 // Create a GOT entry for the module index.
2991 target
->got_mod_index_entry(symtab
, layout
, object
);
2993 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2994 unsupported_reloc_global(object
, r_type
, gsym
);
2997 case elfcpp::R_X86_64_DTPOFF32
:
2998 case elfcpp::R_X86_64_DTPOFF64
:
3001 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3002 layout
->set_has_static_tls();
3003 if (optimized_type
== tls::TLSOPT_NONE
)
3005 // Create a GOT entry for the tp-relative offset.
3006 Output_data_got
<64, false>* got
3007 = target
->got_section(symtab
, layout
);
3008 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3009 target
->rela_dyn_section(layout
),
3010 elfcpp::R_X86_64_TPOFF64
);
3012 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3013 unsupported_reloc_global(object
, r_type
, gsym
);
3016 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3017 layout
->set_has_static_tls();
3018 if (parameters
->options().shared())
3019 unsupported_reloc_global(object
, r_type
, gsym
);
3028 case elfcpp::R_X86_64_SIZE32
:
3029 case elfcpp::R_X86_64_SIZE64
:
3031 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3032 object
->name().c_str(), r_type
,
3033 gsym
->demangled_name().c_str());
3040 Target_x86_64
<size
>::gc_process_relocs(Symbol_table
* symtab
,
3042 Sized_relobj_file
<size
, false>* object
,
3043 unsigned int data_shndx
,
3044 unsigned int sh_type
,
3045 const unsigned char* prelocs
,
3047 Output_section
* output_section
,
3048 bool needs_special_offset_handling
,
3049 size_t local_symbol_count
,
3050 const unsigned char* plocal_symbols
)
3053 if (sh_type
== elfcpp::SHT_REL
)
3058 gold::gc_process_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
3059 typename Target_x86_64
<size
>::Scan
,
3060 typename Target_x86_64
<size
>::Relocatable_size_for_reloc
>(
3069 needs_special_offset_handling
,
3074 // Scan relocations for a section.
3078 Target_x86_64
<size
>::scan_relocs(Symbol_table
* symtab
,
3080 Sized_relobj_file
<size
, false>* object
,
3081 unsigned int data_shndx
,
3082 unsigned int sh_type
,
3083 const unsigned char* prelocs
,
3085 Output_section
* output_section
,
3086 bool needs_special_offset_handling
,
3087 size_t local_symbol_count
,
3088 const unsigned char* plocal_symbols
)
3090 if (sh_type
== elfcpp::SHT_REL
)
3092 gold_error(_("%s: unsupported REL reloc section"),
3093 object
->name().c_str());
3097 gold::scan_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
3098 typename Target_x86_64
<size
>::Scan
>(
3107 needs_special_offset_handling
,
3112 // Finalize the sections.
3116 Target_x86_64
<size
>::do_finalize_sections(
3118 const Input_objects
*,
3119 Symbol_table
* symtab
)
3121 const Reloc_section
* rel_plt
= (this->plt_
== NULL
3123 : this->plt_
->rela_plt());
3124 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
3125 this->rela_dyn_
, true, false);
3127 // Fill in some more dynamic tags.
3128 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
3131 if (this->plt_
!= NULL
3132 && this->plt_
->output_section() != NULL
3133 && this->plt_
->has_tlsdesc_entry())
3135 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
3136 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
3137 this->got_
->finalize_data_size();
3138 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
3139 this->plt_
, plt_offset
);
3140 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
3141 this->got_
, got_offset
);
3145 // Emit any relocs we saved in an attempt to avoid generating COPY
3147 if (this->copy_relocs_
.any_saved_relocs())
3148 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
3150 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3151 // the .got.plt section.
3152 Symbol
* sym
= this->global_offset_table_
;
3155 uint64_t data_size
= this->got_plt_
->current_data_size();
3156 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
3159 if (parameters
->doing_static_link()
3160 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
3162 // If linking statically, make sure that the __rela_iplt symbols
3163 // were defined if necessary, even if we didn't create a PLT.
3164 static const Define_symbol_in_segment syms
[] =
3167 "__rela_iplt_start", // name
3168 elfcpp::PT_LOAD
, // segment_type
3169 elfcpp::PF_W
, // segment_flags_set
3170 elfcpp::PF(0), // segment_flags_clear
3173 elfcpp::STT_NOTYPE
, // type
3174 elfcpp::STB_GLOBAL
, // binding
3175 elfcpp::STV_HIDDEN
, // visibility
3177 Symbol::SEGMENT_START
, // offset_from_base
3181 "__rela_iplt_end", // name
3182 elfcpp::PT_LOAD
, // segment_type
3183 elfcpp::PF_W
, // segment_flags_set
3184 elfcpp::PF(0), // segment_flags_clear
3187 elfcpp::STT_NOTYPE
, // type
3188 elfcpp::STB_GLOBAL
, // binding
3189 elfcpp::STV_HIDDEN
, // visibility
3191 Symbol::SEGMENT_START
, // offset_from_base
3196 symtab
->define_symbols(layout
, 2, syms
,
3197 layout
->script_options()->saw_sections_clause());
3201 // Perform a relocation.
3205 Target_x86_64
<size
>::Relocate::relocate(
3206 const Relocate_info
<size
, false>* relinfo
,
3207 Target_x86_64
<size
>* target
,
3210 const elfcpp::Rela
<size
, false>& rela
,
3211 unsigned int r_type
,
3212 const Sized_symbol
<size
>* gsym
,
3213 const Symbol_value
<size
>* psymval
,
3214 unsigned char* view
,
3215 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3216 section_size_type view_size
)
3218 if (this->skip_call_tls_get_addr_
)
3220 if ((r_type
!= elfcpp::R_X86_64_PLT32
3221 && r_type
!= elfcpp::R_X86_64_PC32
)
3223 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
3225 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3226 _("missing expected TLS relocation"));
3230 this->skip_call_tls_get_addr_
= false;
3238 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3240 // Pick the value to use for symbols defined in the PLT.
3241 Symbol_value
<size
> symval
;
3243 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
3245 symval
.set_output_value(target
->plt_address_for_global(gsym
));
3248 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
3250 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3251 if (object
->local_has_plt_offset(r_sym
))
3253 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
3258 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3260 // Get the GOT offset if needed.
3261 // The GOT pointer points to the end of the GOT section.
3262 // We need to subtract the size of the GOT section to get
3263 // the actual offset to use in the relocation.
3264 bool have_got_offset
= false;
3265 unsigned int got_offset
= 0;
3268 case elfcpp::R_X86_64_GOT32
:
3269 case elfcpp::R_X86_64_GOT64
:
3270 case elfcpp::R_X86_64_GOTPLT64
:
3271 case elfcpp::R_X86_64_GOTPCREL
:
3272 case elfcpp::R_X86_64_GOTPCREL64
:
3275 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3276 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
3280 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3281 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3282 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
3283 - target
->got_size());
3285 have_got_offset
= true;
3294 case elfcpp::R_X86_64_NONE
:
3295 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3296 case elfcpp::R_X86_64_GNU_VTENTRY
:
3299 case elfcpp::R_X86_64_64
:
3300 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
, addend
);
3303 case elfcpp::R_X86_64_PC64
:
3304 Relocate_functions
<size
, false>::pcrela64(view
, object
, psymval
, addend
,
3308 case elfcpp::R_X86_64_32
:
3309 // FIXME: we need to verify that value + addend fits into 32 bits:
3310 // uint64_t x = value + addend;
3311 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3312 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
3313 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3316 case elfcpp::R_X86_64_32S
:
3317 // FIXME: we need to verify that value + addend fits into 32 bits:
3318 // int64_t x = value + addend; // note this quantity is signed!
3319 // x == static_cast<int64_t>(static_cast<int32_t>(x))
3320 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3323 case elfcpp::R_X86_64_PC32
:
3324 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3328 case elfcpp::R_X86_64_16
:
3329 Relocate_functions
<size
, false>::rela16(view
, object
, psymval
, addend
);
3332 case elfcpp::R_X86_64_PC16
:
3333 Relocate_functions
<size
, false>::pcrela16(view
, object
, psymval
, addend
,
3337 case elfcpp::R_X86_64_8
:
3338 Relocate_functions
<size
, false>::rela8(view
, object
, psymval
, addend
);
3341 case elfcpp::R_X86_64_PC8
:
3342 Relocate_functions
<size
, false>::pcrela8(view
, object
, psymval
, addend
,
3346 case elfcpp::R_X86_64_PLT32
:
3347 gold_assert(gsym
== NULL
3348 || gsym
->has_plt_offset()
3349 || gsym
->final_value_is_known()
3350 || (gsym
->is_defined()
3351 && !gsym
->is_from_dynobj()
3352 && !gsym
->is_preemptible()));
3353 // Note: while this code looks the same as for R_X86_64_PC32, it
3354 // behaves differently because psymval was set to point to
3355 // the PLT entry, rather than the symbol, in Scan::global().
3356 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3360 case elfcpp::R_X86_64_PLTOFF64
:
3363 gold_assert(gsym
->has_plt_offset()
3364 || gsym
->final_value_is_known());
3365 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
;
3366 got_address
= target
->got_section(NULL
, NULL
)->address();
3367 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
,
3368 addend
- got_address
);
3371 case elfcpp::R_X86_64_GOT32
:
3372 gold_assert(have_got_offset
);
3373 Relocate_functions
<size
, false>::rela32(view
, got_offset
, addend
);
3376 case elfcpp::R_X86_64_GOTPC32
:
3379 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3380 value
= target
->got_plt_section()->address();
3381 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3385 case elfcpp::R_X86_64_GOT64
:
3386 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
3387 // Since we always add a PLT entry, this is equivalent.
3388 case elfcpp::R_X86_64_GOTPLT64
:
3389 gold_assert(have_got_offset
);
3390 Relocate_functions
<size
, false>::rela64(view
, got_offset
, addend
);
3393 case elfcpp::R_X86_64_GOTPC64
:
3396 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3397 value
= target
->got_plt_section()->address();
3398 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3402 case elfcpp::R_X86_64_GOTOFF64
:
3404 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3405 value
= (psymval
->value(object
, 0)
3406 - target
->got_plt_section()->address());
3407 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3411 case elfcpp::R_X86_64_GOTPCREL
:
3413 gold_assert(have_got_offset
);
3414 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3415 value
= target
->got_plt_section()->address() + got_offset
;
3416 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3420 case elfcpp::R_X86_64_GOTPCREL64
:
3422 gold_assert(have_got_offset
);
3423 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3424 value
= target
->got_plt_section()->address() + got_offset
;
3425 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3429 case elfcpp::R_X86_64_COPY
:
3430 case elfcpp::R_X86_64_GLOB_DAT
:
3431 case elfcpp::R_X86_64_JUMP_SLOT
:
3432 case elfcpp::R_X86_64_RELATIVE
:
3433 case elfcpp::R_X86_64_IRELATIVE
:
3434 // These are outstanding tls relocs, which are unexpected when linking
3435 case elfcpp::R_X86_64_TPOFF64
:
3436 case elfcpp::R_X86_64_DTPMOD64
:
3437 case elfcpp::R_X86_64_TLSDESC
:
3438 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3439 _("unexpected reloc %u in object file"),
3443 // These are initial tls relocs, which are expected when linking
3444 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3445 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3446 case elfcpp::R_X86_64_TLSDESC_CALL
:
3447 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3448 case elfcpp::R_X86_64_DTPOFF32
:
3449 case elfcpp::R_X86_64_DTPOFF64
:
3450 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3451 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3452 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
3453 view
, address
, view_size
);
3456 case elfcpp::R_X86_64_SIZE32
:
3457 case elfcpp::R_X86_64_SIZE64
:
3459 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3460 _("unsupported reloc %u"),
3468 // Perform a TLS relocation.
3472 Target_x86_64
<size
>::Relocate::relocate_tls(
3473 const Relocate_info
<size
, false>* relinfo
,
3474 Target_x86_64
<size
>* target
,
3476 const elfcpp::Rela
<size
, false>& rela
,
3477 unsigned int r_type
,
3478 const Sized_symbol
<size
>* gsym
,
3479 const Symbol_value
<size
>* psymval
,
3480 unsigned char* view
,
3481 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3482 section_size_type view_size
)
3484 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
3486 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3487 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3488 elfcpp::Shdr
<size
, false> data_shdr(relinfo
->data_shdr
);
3489 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
3491 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
3493 const bool is_final
= (gsym
== NULL
3494 ? !parameters
->options().shared()
3495 : gsym
->final_value_is_known());
3496 tls::Tls_optimization optimized_type
3497 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3500 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3501 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3503 // If this code sequence is used in a non-executable section,
3504 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3505 // on the assumption that it's being used by itself in a debug
3506 // section. Therefore, in the unlikely event that the code
3507 // sequence appears in a non-executable section, we simply
3508 // leave it unoptimized.
3509 optimized_type
= tls::TLSOPT_NONE
;
3511 if (optimized_type
== tls::TLSOPT_TO_LE
)
3513 if (tls_segment
== NULL
)
3515 gold_assert(parameters
->errors()->error_count() > 0
3516 || issue_undefined_symbol_error(gsym
));
3519 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
3520 rela
, r_type
, value
, view
,
3526 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3527 ? GOT_TYPE_TLS_OFFSET
3528 : GOT_TYPE_TLS_PAIR
);
3529 unsigned int got_offset
;
3532 gold_assert(gsym
->has_got_offset(got_type
));
3533 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
3537 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3538 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3539 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
3540 - target
->got_size());
3542 if (optimized_type
== tls::TLSOPT_TO_IE
)
3544 value
= target
->got_plt_section()->address() + got_offset
;
3545 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3546 value
, view
, address
, view_size
);
3549 else if (optimized_type
== tls::TLSOPT_NONE
)
3551 // Relocate the field with the offset of the pair of GOT
3553 value
= target
->got_plt_section()->address() + got_offset
;
3554 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3559 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3560 _("unsupported reloc %u"), r_type
);
3563 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3564 case elfcpp::R_X86_64_TLSDESC_CALL
:
3565 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3567 // See above comment for R_X86_64_TLSGD.
3568 optimized_type
= tls::TLSOPT_NONE
;
3570 if (optimized_type
== tls::TLSOPT_TO_LE
)
3572 if (tls_segment
== NULL
)
3574 gold_assert(parameters
->errors()->error_count() > 0
3575 || issue_undefined_symbol_error(gsym
));
3578 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
3579 rela
, r_type
, value
, view
,
3585 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3586 ? GOT_TYPE_TLS_OFFSET
3587 : GOT_TYPE_TLS_DESC
);
3588 unsigned int got_offset
= 0;
3589 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
3590 && optimized_type
== tls::TLSOPT_NONE
)
3592 // We created GOT entries in the .got.tlsdesc portion of
3593 // the .got.plt section, but the offset stored in the
3594 // symbol is the offset within .got.tlsdesc.
3595 got_offset
= (target
->got_size()
3596 + target
->got_plt_section()->data_size());
3600 gold_assert(gsym
->has_got_offset(got_type
));
3601 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
3605 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3606 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3607 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
3608 - target
->got_size());
3610 if (optimized_type
== tls::TLSOPT_TO_IE
)
3612 if (tls_segment
== NULL
)
3614 gold_assert(parameters
->errors()->error_count() > 0
3615 || issue_undefined_symbol_error(gsym
));
3618 value
= target
->got_plt_section()->address() + got_offset
;
3619 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
3620 rela
, r_type
, value
, view
, address
,
3624 else if (optimized_type
== tls::TLSOPT_NONE
)
3626 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3628 // Relocate the field with the offset of the pair of GOT
3630 value
= target
->got_plt_section()->address() + got_offset
;
3631 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3637 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3638 _("unsupported reloc %u"), r_type
);
3641 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3642 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3644 // See above comment for R_X86_64_TLSGD.
3645 optimized_type
= tls::TLSOPT_NONE
;
3647 if (optimized_type
== tls::TLSOPT_TO_LE
)
3649 if (tls_segment
== NULL
)
3651 gold_assert(parameters
->errors()->error_count() > 0
3652 || issue_undefined_symbol_error(gsym
));
3655 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3656 value
, view
, view_size
);
3659 else if (optimized_type
== tls::TLSOPT_NONE
)
3661 // Relocate the field with the offset of the GOT entry for
3662 // the module index.
3663 unsigned int got_offset
;
3664 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
3665 - target
->got_size());
3666 value
= target
->got_plt_section()->address() + got_offset
;
3667 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3671 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3672 _("unsupported reloc %u"), r_type
);
3675 case elfcpp::R_X86_64_DTPOFF32
:
3676 // This relocation type is used in debugging information.
3677 // In that case we need to not optimize the value. If the
3678 // section is not executable, then we assume we should not
3679 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3680 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3682 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3684 if (tls_segment
== NULL
)
3686 gold_assert(parameters
->errors()->error_count() > 0
3687 || issue_undefined_symbol_error(gsym
));
3690 value
-= tls_segment
->memsz();
3692 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3695 case elfcpp::R_X86_64_DTPOFF64
:
3696 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3697 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3699 if (tls_segment
== NULL
)
3701 gold_assert(parameters
->errors()->error_count() > 0
3702 || issue_undefined_symbol_error(gsym
));
3705 value
-= tls_segment
->memsz();
3707 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3710 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3711 if (optimized_type
== tls::TLSOPT_TO_LE
)
3713 if (tls_segment
== NULL
)
3715 gold_assert(parameters
->errors()->error_count() > 0
3716 || issue_undefined_symbol_error(gsym
));
3719 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
3721 r_type
, value
, view
,
3725 else if (optimized_type
== tls::TLSOPT_NONE
)
3727 // Relocate the field with the offset of the GOT entry for
3728 // the tp-relative offset of the symbol.
3729 unsigned int got_offset
;
3732 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
3733 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
3734 - target
->got_size());
3738 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3739 gold_assert(object
->local_has_got_offset(r_sym
,
3740 GOT_TYPE_TLS_OFFSET
));
3741 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
3742 - target
->got_size());
3744 value
= target
->got_plt_section()->address() + got_offset
;
3745 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3749 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3750 _("unsupported reloc type %u"),
3754 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3755 if (tls_segment
== NULL
)
3757 gold_assert(parameters
->errors()->error_count() > 0
3758 || issue_undefined_symbol_error(gsym
));
3761 value
-= tls_segment
->memsz();
3762 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3767 // Do a relocation in which we convert a TLS General-Dynamic to an
3772 Target_x86_64
<size
>::Relocate::tls_gd_to_ie(
3773 const Relocate_info
<size
, false>* relinfo
,
3776 const elfcpp::Rela
<size
, false>& rela
,
3778 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3779 unsigned char* view
,
3780 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3781 section_size_type view_size
)
3784 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3785 // .word 0x6666; rex64; call __tls_get_addr
3786 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3788 // leaq foo@tlsgd(%rip),%rdi;
3789 // .word 0x6666; rex64; call __tls_get_addr
3790 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
3792 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3793 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3794 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3798 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3800 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3801 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3802 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3807 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3809 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3810 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
3811 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3815 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3816 Relocate_functions
<size
, false>::pcrela32(view
+ 8, value
, addend
- 8,
3819 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3821 this->skip_call_tls_get_addr_
= true;
3824 // Do a relocation in which we convert a TLS General-Dynamic to a
3829 Target_x86_64
<size
>::Relocate::tls_gd_to_le(
3830 const Relocate_info
<size
, false>* relinfo
,
3832 Output_segment
* tls_segment
,
3833 const elfcpp::Rela
<size
, false>& rela
,
3835 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3836 unsigned char* view
,
3837 section_size_type view_size
)
3840 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3841 // .word 0x6666; rex64; call __tls_get_addr
3842 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3844 // leaq foo@tlsgd(%rip),%rdi;
3845 // .word 0x6666; rex64; call __tls_get_addr
3846 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
3848 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3849 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3850 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3854 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3856 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3857 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3858 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
3863 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3865 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3866 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
3868 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
3872 value
-= tls_segment
->memsz();
3873 Relocate_functions
<size
, false>::rela32(view
+ 8, value
, 0);
3875 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3877 this->skip_call_tls_get_addr_
= true;
3880 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3884 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_ie(
3885 const Relocate_info
<size
, false>* relinfo
,
3888 const elfcpp::Rela
<size
, false>& rela
,
3889 unsigned int r_type
,
3890 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3891 unsigned char* view
,
3892 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3893 section_size_type view_size
)
3895 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3897 // leaq foo@tlsdesc(%rip), %rax
3898 // ==> movq foo@gottpoff(%rip), %rax
3899 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3900 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3901 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3902 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
3904 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3905 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3909 // call *foo@tlscall(%rax)
3911 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
3912 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
3913 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3914 view
[0] == 0xff && view
[1] == 0x10);
3920 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3924 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_le(
3925 const Relocate_info
<size
, false>* relinfo
,
3927 Output_segment
* tls_segment
,
3928 const elfcpp::Rela
<size
, false>& rela
,
3929 unsigned int r_type
,
3930 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3931 unsigned char* view
,
3932 section_size_type view_size
)
3934 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3936 // leaq foo@tlsdesc(%rip), %rax
3937 // ==> movq foo@tpoff, %rax
3938 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3939 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3940 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3941 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
3944 value
-= tls_segment
->memsz();
3945 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
3949 // call *foo@tlscall(%rax)
3951 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
3952 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
3953 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3954 view
[0] == 0xff && view
[1] == 0x10);
3962 Target_x86_64
<size
>::Relocate::tls_ld_to_le(
3963 const Relocate_info
<size
, false>* relinfo
,
3966 const elfcpp::Rela
<size
, false>& rela
,
3968 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
3969 unsigned char* view
,
3970 section_size_type view_size
)
3972 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3974 // ... leq foo@dtpoff(%rax),%reg
3975 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3977 // ... leq foo@dtpoff(%rax),%reg
3978 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
3980 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3981 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
3983 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3984 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
3986 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
3989 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3991 memcpy(view
- 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
3993 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3995 this->skip_call_tls_get_addr_
= true;
3998 // Do a relocation in which we convert a TLS Initial-Exec to a
4003 Target_x86_64
<size
>::Relocate::tls_ie_to_le(
4004 const Relocate_info
<size
, false>* relinfo
,
4006 Output_segment
* tls_segment
,
4007 const elfcpp::Rela
<size
, false>& rela
,
4009 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4010 unsigned char* view
,
4011 section_size_type view_size
)
4013 // We need to examine the opcodes to figure out which instruction we
4016 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4017 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4019 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4020 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4022 unsigned char op1
= view
[-3];
4023 unsigned char op2
= view
[-2];
4024 unsigned char op3
= view
[-1];
4025 unsigned char reg
= op3
>> 3;
4033 view
[-1] = 0xc0 | reg
;
4037 // Special handling for %rsp.
4041 view
[-1] = 0xc0 | reg
;
4049 view
[-1] = 0x80 | reg
| (reg
<< 3);
4052 value
-= tls_segment
->memsz();
4053 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
4056 // Relocate section data.
4060 Target_x86_64
<size
>::relocate_section(
4061 const Relocate_info
<size
, false>* relinfo
,
4062 unsigned int sh_type
,
4063 const unsigned char* prelocs
,
4065 Output_section
* output_section
,
4066 bool needs_special_offset_handling
,
4067 unsigned char* view
,
4068 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4069 section_size_type view_size
,
4070 const Reloc_symbol_changes
* reloc_symbol_changes
)
4072 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4074 gold::relocate_section
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
4075 typename Target_x86_64
<size
>::Relocate
,
4076 gold::Default_comdat_behavior
>(
4082 needs_special_offset_handling
,
4086 reloc_symbol_changes
);
4089 // Apply an incremental relocation. Incremental relocations always refer
4090 // to global symbols.
4094 Target_x86_64
<size
>::apply_relocation(
4095 const Relocate_info
<size
, false>* relinfo
,
4096 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
4097 unsigned int r_type
,
4098 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
4100 unsigned char* view
,
4101 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4102 section_size_type view_size
)
4104 gold::apply_relocation
<size
, false, Target_x86_64
<size
>,
4105 typename Target_x86_64
<size
>::Relocate
>(
4117 // Return the size of a relocation while scanning during a relocatable
4122 Target_x86_64
<size
>::Relocatable_size_for_reloc::get_size_for_reloc(
4123 unsigned int r_type
,
4128 case elfcpp::R_X86_64_NONE
:
4129 case elfcpp::R_X86_64_GNU_VTINHERIT
:
4130 case elfcpp::R_X86_64_GNU_VTENTRY
:
4131 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4132 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4133 case elfcpp::R_X86_64_TLSDESC_CALL
:
4134 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4135 case elfcpp::R_X86_64_DTPOFF32
:
4136 case elfcpp::R_X86_64_DTPOFF64
:
4137 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4138 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4141 case elfcpp::R_X86_64_64
:
4142 case elfcpp::R_X86_64_PC64
:
4143 case elfcpp::R_X86_64_GOTOFF64
:
4144 case elfcpp::R_X86_64_GOTPC64
:
4145 case elfcpp::R_X86_64_PLTOFF64
:
4146 case elfcpp::R_X86_64_GOT64
:
4147 case elfcpp::R_X86_64_GOTPCREL64
:
4148 case elfcpp::R_X86_64_GOTPCREL
:
4149 case elfcpp::R_X86_64_GOTPLT64
:
4152 case elfcpp::R_X86_64_32
:
4153 case elfcpp::R_X86_64_32S
:
4154 case elfcpp::R_X86_64_PC32
:
4155 case elfcpp::R_X86_64_PLT32
:
4156 case elfcpp::R_X86_64_GOTPC32
:
4157 case elfcpp::R_X86_64_GOT32
:
4160 case elfcpp::R_X86_64_16
:
4161 case elfcpp::R_X86_64_PC16
:
4164 case elfcpp::R_X86_64_8
:
4165 case elfcpp::R_X86_64_PC8
:
4168 case elfcpp::R_X86_64_COPY
:
4169 case elfcpp::R_X86_64_GLOB_DAT
:
4170 case elfcpp::R_X86_64_JUMP_SLOT
:
4171 case elfcpp::R_X86_64_RELATIVE
:
4172 case elfcpp::R_X86_64_IRELATIVE
:
4173 // These are outstanding tls relocs, which are unexpected when linking
4174 case elfcpp::R_X86_64_TPOFF64
:
4175 case elfcpp::R_X86_64_DTPMOD64
:
4176 case elfcpp::R_X86_64_TLSDESC
:
4177 object
->error(_("unexpected reloc %u in object file"), r_type
);
4180 case elfcpp::R_X86_64_SIZE32
:
4181 case elfcpp::R_X86_64_SIZE64
:
4183 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
4188 // Scan the relocs during a relocatable link.
4192 Target_x86_64
<size
>::scan_relocatable_relocs(
4193 Symbol_table
* symtab
,
4195 Sized_relobj_file
<size
, false>* object
,
4196 unsigned int data_shndx
,
4197 unsigned int sh_type
,
4198 const unsigned char* prelocs
,
4200 Output_section
* output_section
,
4201 bool needs_special_offset_handling
,
4202 size_t local_symbol_count
,
4203 const unsigned char* plocal_symbols
,
4204 Relocatable_relocs
* rr
)
4206 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4208 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
4209 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
4211 gold::scan_relocatable_relocs
<size
, false, elfcpp::SHT_RELA
,
4212 Scan_relocatable_relocs
>(
4220 needs_special_offset_handling
,
4226 // Relocate a section during a relocatable link.
4230 Target_x86_64
<size
>::relocate_relocs(
4231 const Relocate_info
<size
, false>* relinfo
,
4232 unsigned int sh_type
,
4233 const unsigned char* prelocs
,
4235 Output_section
* output_section
,
4236 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
4237 const Relocatable_relocs
* rr
,
4238 unsigned char* view
,
4239 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
4240 section_size_type view_size
,
4241 unsigned char* reloc_view
,
4242 section_size_type reloc_view_size
)
4244 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4246 gold::relocate_relocs
<size
, false, elfcpp::SHT_RELA
>(
4251 offset_in_output_section
,
4260 // Return the value to use for a dynamic which requires special
4261 // treatment. This is how we support equality comparisons of function
4262 // pointers across shared library boundaries, as described in the
4263 // processor specific ABI supplement.
4267 Target_x86_64
<size
>::do_dynsym_value(const Symbol
* gsym
) const
4269 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
4270 return this->plt_address_for_global(gsym
);
4273 // Return a string used to fill a code section with nops to take up
4274 // the specified length.
4278 Target_x86_64
<size
>::do_code_fill(section_size_type length
) const
4282 // Build a jmpq instruction to skip over the bytes.
4283 unsigned char jmp
[5];
4285 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
4286 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
4287 + std::string(length
- 5, static_cast<char>(0x90)));
4290 // Nop sequences of various lengths.
4291 const char nop1
[1] = { '\x90' }; // nop
4292 const char nop2
[2] = { '\x66', '\x90' }; // xchg %ax %ax
4293 const char nop3
[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4294 const char nop4
[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4296 const char nop5
[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4298 const char nop6
[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4299 '\x44', '\x00', '\x00' };
4300 const char nop7
[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4301 '\x00', '\x00', '\x00',
4303 const char nop8
[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4304 '\x00', '\x00', '\x00',
4306 const char nop9
[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4307 '\x84', '\x00', '\x00',
4308 '\x00', '\x00', '\x00' };
4309 const char nop10
[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4310 '\x1f', '\x84', '\x00',
4311 '\x00', '\x00', '\x00',
4313 const char nop11
[11] = { '\x66', '\x66', '\x2e', // data16
4314 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4315 '\x00', '\x00', '\x00',
4317 const char nop12
[12] = { '\x66', '\x66', '\x66', // data16; data16
4318 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4319 '\x84', '\x00', '\x00',
4320 '\x00', '\x00', '\x00' };
4321 const char nop13
[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4322 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4323 '\x1f', '\x84', '\x00',
4324 '\x00', '\x00', '\x00',
4326 const char nop14
[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4327 '\x66', '\x66', '\x2e', // data16
4328 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4329 '\x00', '\x00', '\x00',
4331 const char nop15
[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4332 '\x66', '\x66', '\x66', // data16; data16
4333 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4334 '\x84', '\x00', '\x00',
4335 '\x00', '\x00', '\x00' };
4337 const char* nops
[16] = {
4339 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
4340 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
4343 return std::string(nops
[length
], length
);
4346 // Return the addend to use for a target specific relocation. The
4347 // only target specific relocation is R_X86_64_TLSDESC for a local
4348 // symbol. We want to set the addend is the offset of the local
4349 // symbol in the TLS segment.
4353 Target_x86_64
<size
>::do_reloc_addend(void* arg
, unsigned int r_type
,
4356 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
4357 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
4358 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
4359 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
4360 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
4361 gold_assert(psymval
->is_tls_symbol());
4362 // The value of a TLS symbol is the offset in the TLS segment.
4363 return psymval
->value(ti
.object
, 0);
4366 // Return the value to use for the base of a DW_EH_PE_datarel offset
4367 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4368 // assembler can not write out the difference between two labels in
4369 // different sections, so instead of using a pc-relative value they
4370 // use an offset from the GOT.
4374 Target_x86_64
<size
>::do_ehframe_datarel_base() const
4376 gold_assert(this->global_offset_table_
!= NULL
);
4377 Symbol
* sym
= this->global_offset_table_
;
4378 Sized_symbol
<size
>* ssym
= static_cast<Sized_symbol
<size
>*>(sym
);
4379 return ssym
->value();
4382 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4383 // compiled with -fsplit-stack. The function calls non-split-stack
4384 // code. We have to change the function so that it always ensures
4385 // that it has enough stack space to run some random function.
4389 Target_x86_64
<size
>::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
4390 section_offset_type fnoffset
,
4391 section_size_type fnsize
,
4392 unsigned char* view
,
4393 section_size_type view_size
,
4395 std::string
* to
) const
4397 // The function starts with a comparison of the stack pointer and a
4398 // field in the TCB. This is followed by a jump.
4401 if (this->match_view(view
, view_size
, fnoffset
, "\x64\x48\x3b\x24\x25", 5)
4404 // We will call __morestack if the carry flag is set after this
4405 // comparison. We turn the comparison into an stc instruction
4407 view
[fnoffset
] = '\xf9';
4408 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 8);
4410 // lea NN(%rsp),%r10
4411 // lea NN(%rsp),%r11
4412 else if ((this->match_view(view
, view_size
, fnoffset
,
4413 "\x4c\x8d\x94\x24", 4)
4414 || this->match_view(view
, view_size
, fnoffset
,
4415 "\x4c\x8d\x9c\x24", 4))
4418 // This is loading an offset from the stack pointer for a
4419 // comparison. The offset is negative, so we decrease the
4420 // offset by the amount of space we need for the stack. This
4421 // means we will avoid calling __morestack if there happens to
4422 // be plenty of space on the stack already.
4423 unsigned char* pval
= view
+ fnoffset
+ 4;
4424 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
4425 val
-= parameters
->options().split_stack_adjust_size();
4426 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
4430 if (!object
->has_no_split_stack())
4431 object
->error(_("failed to match split-stack sequence at "
4432 "section %u offset %0zx"),
4433 shndx
, static_cast<size_t>(fnoffset
));
4437 // We have to change the function so that it calls
4438 // __morestack_non_split instead of __morestack. The former will
4439 // allocate additional stack space.
4440 *from
= "__morestack";
4441 *to
= "__morestack_non_split";
4444 // The selector for x86_64 object files. Note this is never instantiated
4445 // directly. It's only used in Target_selector_x86_64_nacl, below.
4448 class Target_selector_x86_64
: public Target_selector_freebsd
4451 Target_selector_x86_64()
4452 : Target_selector_freebsd(elfcpp::EM_X86_64
, size
, false,
4454 ? "elf64-x86-64" : "elf32-x86-64"),
4456 ? "elf64-x86-64-freebsd"
4457 : "elf32-x86-64-freebsd"),
4458 (size
== 64 ? "elf_x86_64" : "elf32_x86_64"))
4462 do_instantiate_target()
4463 { return new Target_x86_64
<size
>(); }
4467 // NaCl variant. It uses different PLT contents.
4470 class Output_data_plt_x86_64_nacl
: public Output_data_plt_x86_64
<size
>
4473 Output_data_plt_x86_64_nacl(Layout
* layout
,
4474 Output_data_got
<64, false>* got
,
4475 Output_data_space
* got_plt
,
4476 Output_data_space
* got_irelative
)
4477 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4478 got
, got_plt
, got_irelative
)
4481 Output_data_plt_x86_64_nacl(Layout
* layout
,
4482 Output_data_got
<64, false>* got
,
4483 Output_data_space
* got_plt
,
4484 Output_data_space
* got_irelative
,
4485 unsigned int plt_count
)
4486 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4487 got
, got_plt
, got_irelative
,
4492 virtual unsigned int
4493 do_get_plt_entry_size() const
4494 { return plt_entry_size
; }
4497 do_add_eh_frame(Layout
* layout
)
4499 layout
->add_eh_frame_for_plt(this,
4500 this->plt_eh_frame_cie
,
4501 this->plt_eh_frame_cie_size
,
4503 plt_eh_frame_fde_size
);
4507 do_fill_first_plt_entry(unsigned char* pov
,
4508 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
4509 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
4511 virtual unsigned int
4512 do_fill_plt_entry(unsigned char* pov
,
4513 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4514 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4515 unsigned int got_offset
,
4516 unsigned int plt_offset
,
4517 unsigned int plt_index
);
4520 do_fill_tlsdesc_entry(unsigned char* pov
,
4521 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4522 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4523 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
4524 unsigned int tlsdesc_got_offset
,
4525 unsigned int plt_offset
);
4528 // The size of an entry in the PLT.
4529 static const int plt_entry_size
= 64;
4531 // The first entry in the PLT.
4532 static const unsigned char first_plt_entry
[plt_entry_size
];
4534 // Other entries in the PLT for an executable.
4535 static const unsigned char plt_entry
[plt_entry_size
];
4537 // The reserved TLSDESC entry in the PLT for an executable.
4538 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
4540 // The .eh_frame unwind information for the PLT.
4541 static const int plt_eh_frame_fde_size
= 32;
4542 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
4546 class Target_x86_64_nacl
: public Target_x86_64
<size
>
4549 Target_x86_64_nacl()
4550 : Target_x86_64
<size
>(&x86_64_nacl_info
)
4553 virtual Output_data_plt_x86_64
<size
>*
4554 do_make_data_plt(Layout
* layout
,
4555 Output_data_got
<64, false>* got
,
4556 Output_data_space
* got_plt
,
4557 Output_data_space
* got_irelative
)
4559 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4563 virtual Output_data_plt_x86_64
<size
>*
4564 do_make_data_plt(Layout
* layout
,
4565 Output_data_got
<64, false>* got
,
4566 Output_data_space
* got_plt
,
4567 Output_data_space
* got_irelative
,
4568 unsigned int plt_count
)
4570 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4576 do_code_fill(section_size_type length
) const;
4579 static const Target::Target_info x86_64_nacl_info
;
4583 const Target::Target_info Target_x86_64_nacl
<64>::x86_64_nacl_info
=
4586 false, // is_big_endian
4587 elfcpp::EM_X86_64
, // machine_code
4588 false, // has_make_symbol
4589 false, // has_resolve
4590 true, // has_code_fill
4591 true, // is_default_stack_executable
4592 true, // can_icf_inline_merge_sections
4594 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4595 0x20000, // default_text_segment_address
4596 0x10000, // abi_pagesize (overridable by -z max-page-size)
4597 0x10000, // common_pagesize (overridable by -z common-page-size)
4598 true, // isolate_execinstr
4599 0x10000000, // rosegment_gap
4600 elfcpp::SHN_UNDEF
, // small_common_shndx
4601 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
4602 0, // small_common_section_flags
4603 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
4604 NULL
, // attributes_section
4605 NULL
, // attributes_vendor
4606 "_start" // entry_symbol_name
4610 const Target::Target_info Target_x86_64_nacl
<32>::x86_64_nacl_info
=
4613 false, // is_big_endian
4614 elfcpp::EM_X86_64
, // machine_code
4615 false, // has_make_symbol
4616 false, // has_resolve
4617 true, // has_code_fill
4618 true, // is_default_stack_executable
4619 true, // can_icf_inline_merge_sections
4621 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
4622 0x20000, // default_text_segment_address
4623 0x10000, // abi_pagesize (overridable by -z max-page-size)
4624 0x10000, // common_pagesize (overridable by -z common-page-size)
4625 true, // isolate_execinstr
4626 0x10000000, // rosegment_gap
4627 elfcpp::SHN_UNDEF
, // small_common_shndx
4628 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
4629 0, // small_common_section_flags
4630 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
4631 NULL
, // attributes_section
4632 NULL
, // attributes_vendor
4633 "_start" // entry_symbol_name
4636 #define NACLMASK 0xe0 // 32-byte alignment mask.
4638 // The first entry in the PLT.
4642 Output_data_plt_x86_64_nacl
<size
>::first_plt_entry
[plt_entry_size
] =
4644 0xff, 0x35, // pushq contents of memory address
4645 0, 0, 0, 0, // replaced with address of .got + 8
4646 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
4647 0, 0, 0, 0, // replaced with address of .got + 16
4648 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4649 0x4d, 0x01, 0xfb, // add %r15, %r11
4650 0x41, 0xff, 0xe3, // jmpq *%r11
4652 // 9-byte nop sequence to pad out to the next 32-byte boundary.
4653 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopl %cs:0x0(%rax,%rax,1)
4655 // 32 bytes of nop to pad out to the standard size
4656 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4657 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4658 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4659 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4660 0x66, // excess data32 prefix
4666 Output_data_plt_x86_64_nacl
<size
>::do_fill_first_plt_entry(
4668 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4669 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
4671 memcpy(pov
, first_plt_entry
, plt_entry_size
);
4672 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
4674 - (plt_address
+ 2 + 4)));
4675 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
4677 - (plt_address
+ 9 + 4)));
4680 // Subsequent entries in the PLT.
4684 Output_data_plt_x86_64_nacl
<size
>::plt_entry
[plt_entry_size
] =
4686 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
4687 0, 0, 0, 0, // replaced with address of symbol in .got
4688 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4689 0x4d, 0x01, 0xfb, // add %r15, %r11
4690 0x41, 0xff, 0xe3, // jmpq *%r11
4692 // 15-byte nop sequence to pad out to the next 32-byte boundary.
4693 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4694 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4696 // Lazy GOT entries point here (32-byte aligned).
4697 0x68, // pushq immediate
4698 0, 0, 0, 0, // replaced with index into relocation table
4699 0xe9, // jmp relative
4700 0, 0, 0, 0, // replaced with offset to start of .plt0
4702 // 22 bytes of nop to pad out to the standard size.
4703 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4704 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4705 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
4710 Output_data_plt_x86_64_nacl
<size
>::do_fill_plt_entry(
4712 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4713 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4714 unsigned int got_offset
,
4715 unsigned int plt_offset
,
4716 unsigned int plt_index
)
4718 memcpy(pov
, plt_entry
, plt_entry_size
);
4719 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3,
4720 (got_address
+ got_offset
4721 - (plt_address
+ plt_offset
4724 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 33, plt_index
);
4725 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 38,
4726 - (plt_offset
+ 38 + 4));
4731 // The reserved TLSDESC entry in the PLT.
4735 Output_data_plt_x86_64_nacl
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
4737 0xff, 0x35, // pushq x(%rip)
4738 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
4739 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
4740 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
4741 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4742 0x4d, 0x01, 0xfb, // add %r15, %r11
4743 0x41, 0xff, 0xe3, // jmpq *%r11
4745 // 41 bytes of nop to pad out to the standard size.
4746 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4747 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4748 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4749 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4750 0x66, 0x66, // excess data32 prefixes
4751 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4756 Output_data_plt_x86_64_nacl
<size
>::do_fill_tlsdesc_entry(
4758 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4759 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4760 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
4761 unsigned int tlsdesc_got_offset
,
4762 unsigned int plt_offset
)
4764 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
4765 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
4767 - (plt_address
+ plt_offset
4769 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
4771 + tlsdesc_got_offset
4772 - (plt_address
+ plt_offset
4776 // The .eh_frame unwind information for the PLT.
4780 Output_data_plt_x86_64_nacl
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
4782 0, 0, 0, 0, // Replaced with offset to .plt.
4783 0, 0, 0, 0, // Replaced with size of .plt.
4784 0, // Augmentation size.
4785 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
4786 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
4787 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
4788 elfcpp::DW_CFA_advance_loc
+ 58, // Advance 58 to __PLT__ + 64.
4789 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
4790 13, // Block length.
4791 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
4792 elfcpp::DW_OP_breg16
, 0, // Push %rip.
4793 elfcpp::DW_OP_const1u
, 63, // Push 0x3f.
4794 elfcpp::DW_OP_and
, // & (%rip & 0x3f).
4795 elfcpp::DW_OP_const1u
, 37, // Push 0x25.
4796 elfcpp::DW_OP_ge
, // >= ((%rip & 0x3f) >= 0x25)
4797 elfcpp::DW_OP_lit3
, // Push 3.
4798 elfcpp::DW_OP_shl
, // << (((%rip & 0x3f) >= 0x25) << 3)
4799 elfcpp::DW_OP_plus
, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
4800 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
4804 // Return a string used to fill a code section with nops.
4805 // For NaCl, long NOPs are only valid if they do not cross
4806 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
4809 Target_x86_64_nacl
<size
>::do_code_fill(section_size_type length
) const
4811 return std::string(length
, static_cast<char>(0x90));
4814 // The selector for x86_64-nacl object files.
4817 class Target_selector_x86_64_nacl
4818 : public Target_selector_nacl
<Target_selector_x86_64
<size
>,
4819 Target_x86_64_nacl
<size
> >
4822 Target_selector_x86_64_nacl()
4823 : Target_selector_nacl
<Target_selector_x86_64
<size
>,
4824 Target_x86_64_nacl
<size
> >("x86-64",
4826 ? "elf64-x86-64-nacl"
4827 : "elf32-x86-64-nacl",
4830 : "elf32_x86_64_nacl")
4834 Target_selector_x86_64_nacl
<64> target_selector_x86_64
;
4835 Target_selector_x86_64_nacl
<32> target_selector_x32
;
4837 } // End anonymous namespace.