1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2015 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "parameters.h"
36 #include "copy-relocs.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
51 // A class to handle the .got.plt section.
53 class Output_data_got_plt_x86_64
: public Output_section_data_build
56 Output_data_got_plt_x86_64(Layout
* layout
)
57 : Output_section_data_build(8),
61 Output_data_got_plt_x86_64(Layout
* layout
, off_t data_size
)
62 : Output_section_data_build(data_size
, 8),
67 // Write out the PLT data.
69 do_write(Output_file
*);
71 // Write to a map file.
73 do_print_to_mapfile(Mapfile
* mapfile
) const
74 { mapfile
->print_output_data(this, "** GOT PLT"); }
77 // A pointer to the Layout class, so that we can find the .dynamic
78 // section when we write out the GOT PLT section.
82 // A class to handle the PLT data.
83 // This is an abstract base class that handles most of the linker details
84 // but does not know the actual contents of PLT entries. The derived
85 // classes below fill in those details.
88 class Output_data_plt_x86_64
: public Output_section_data
91 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
93 Output_data_plt_x86_64(Layout
* layout
, uint64_t addralign
,
94 Output_data_got
<64, false>* got
,
95 Output_data_got_plt_x86_64
* got_plt
,
96 Output_data_space
* got_irelative
)
97 : Output_section_data(addralign
), tlsdesc_rel_(NULL
),
98 irelative_rel_(NULL
), got_(got
), got_plt_(got_plt
),
99 got_irelative_(got_irelative
), count_(0), irelative_count_(0),
100 tlsdesc_got_offset_(-1U), free_list_()
101 { this->init(layout
); }
103 Output_data_plt_x86_64(Layout
* layout
, uint64_t plt_entry_size
,
104 Output_data_got
<64, false>* got
,
105 Output_data_got_plt_x86_64
* got_plt
,
106 Output_data_space
* got_irelative
,
107 unsigned int plt_count
)
108 : Output_section_data((plt_count
+ 1) * plt_entry_size
,
109 plt_entry_size
, false),
110 tlsdesc_rel_(NULL
), irelative_rel_(NULL
), got_(got
),
111 got_plt_(got_plt
), got_irelative_(got_irelative
), count_(plt_count
),
112 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
116 // Initialize the free list and reserve the first entry.
117 this->free_list_
.init((plt_count
+ 1) * plt_entry_size
, false);
118 this->free_list_
.remove(0, plt_entry_size
);
121 // Initialize the PLT section.
123 init(Layout
* layout
);
125 // Add an entry to the PLT.
127 add_entry(Symbol_table
*, Layout
*, Symbol
* gsym
);
129 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
131 add_local_ifunc_entry(Symbol_table
* symtab
, Layout
*,
132 Sized_relobj_file
<size
, false>* relobj
,
133 unsigned int local_sym_index
);
135 // Add the relocation for a PLT entry.
137 add_relocation(Symbol_table
*, Layout
*, Symbol
* gsym
,
138 unsigned int got_offset
);
140 // Add the reserved TLSDESC_PLT entry to the PLT.
142 reserve_tlsdesc_entry(unsigned int got_offset
)
143 { this->tlsdesc_got_offset_
= got_offset
; }
145 // Return true if a TLSDESC_PLT entry has been reserved.
147 has_tlsdesc_entry() const
148 { return this->tlsdesc_got_offset_
!= -1U; }
150 // Return the GOT offset for the reserved TLSDESC_PLT entry.
152 get_tlsdesc_got_offset() const
153 { return this->tlsdesc_got_offset_
; }
155 // Return the offset of the reserved TLSDESC_PLT entry.
157 get_tlsdesc_plt_offset() const
159 return ((this->count_
+ this->irelative_count_
+ 1)
160 * this->get_plt_entry_size());
163 // Return the .rela.plt section data.
166 { return this->rel_
; }
168 // Return where the TLSDESC relocations should go.
170 rela_tlsdesc(Layout
*);
172 // Return where the IRELATIVE relocations should go in the PLT
175 rela_irelative(Symbol_table
*, Layout
*);
177 // Return whether we created a section for IRELATIVE relocations.
179 has_irelative_section() const
180 { return this->irelative_rel_
!= NULL
; }
182 // Return the number of PLT entries.
185 { return this->count_
+ this->irelative_count_
; }
187 // Return the offset of the first non-reserved PLT entry.
189 first_plt_entry_offset()
190 { return this->get_plt_entry_size(); }
192 // Return the size of a PLT entry.
194 get_plt_entry_size() const
195 { return this->do_get_plt_entry_size(); }
197 // Reserve a slot in the PLT for an existing symbol in an incremental update.
199 reserve_slot(unsigned int plt_index
)
201 this->free_list_
.remove((plt_index
+ 1) * this->get_plt_entry_size(),
202 (plt_index
+ 2) * this->get_plt_entry_size());
205 // Return the PLT address to use for a global symbol.
207 address_for_global(const Symbol
*);
209 // Return the PLT address to use for a local symbol.
211 address_for_local(const Relobj
*, unsigned int symndx
);
213 // Add .eh_frame information for the PLT.
215 add_eh_frame(Layout
* layout
)
216 { this->do_add_eh_frame(layout
); }
219 // Fill in the first PLT entry.
221 fill_first_plt_entry(unsigned char* pov
,
222 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
223 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
224 { this->do_fill_first_plt_entry(pov
, got_address
, plt_address
); }
226 // Fill in a normal PLT entry. Returns the offset into the entry that
227 // should be the initial GOT slot value.
229 fill_plt_entry(unsigned char* pov
,
230 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
231 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
232 unsigned int got_offset
,
233 unsigned int plt_offset
,
234 unsigned int plt_index
)
236 return this->do_fill_plt_entry(pov
, got_address
, plt_address
,
237 got_offset
, plt_offset
, plt_index
);
240 // Fill in the reserved TLSDESC PLT entry.
242 fill_tlsdesc_entry(unsigned char* pov
,
243 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
244 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
245 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
246 unsigned int tlsdesc_got_offset
,
247 unsigned int plt_offset
)
249 this->do_fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
250 tlsdesc_got_offset
, plt_offset
);
254 do_get_plt_entry_size() const = 0;
257 do_fill_first_plt_entry(unsigned char* pov
,
258 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
259 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
)
263 do_fill_plt_entry(unsigned char* pov
,
264 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
265 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
266 unsigned int got_offset
,
267 unsigned int plt_offset
,
268 unsigned int plt_index
) = 0;
271 do_fill_tlsdesc_entry(unsigned char* pov
,
272 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
273 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
274 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
275 unsigned int tlsdesc_got_offset
,
276 unsigned int plt_offset
) = 0;
279 do_add_eh_frame(Layout
* layout
) = 0;
282 do_adjust_output_section(Output_section
* os
);
284 // Write to a map file.
286 do_print_to_mapfile(Mapfile
* mapfile
) const
287 { mapfile
->print_output_data(this, _("** PLT")); }
289 // The CIE of the .eh_frame unwind information for the PLT.
290 static const int plt_eh_frame_cie_size
= 16;
291 static const unsigned char plt_eh_frame_cie
[plt_eh_frame_cie_size
];
294 // Set the final size.
296 set_final_data_size();
298 // Write out the PLT data.
300 do_write(Output_file
*);
302 // The reloc section.
304 // The TLSDESC relocs, if necessary. These must follow the regular
306 Reloc_section
* tlsdesc_rel_
;
307 // The IRELATIVE relocs, if necessary. These must follow the
308 // regular PLT relocations and the TLSDESC relocations.
309 Reloc_section
* irelative_rel_
;
311 Output_data_got
<64, false>* got_
;
312 // The .got.plt section.
313 Output_data_got_plt_x86_64
* got_plt_
;
314 // The part of the .got.plt section used for IRELATIVE relocs.
315 Output_data_space
* got_irelative_
;
316 // The number of PLT entries.
318 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
319 // follow the regular PLT entries.
320 unsigned int irelative_count_
;
321 // Offset of the reserved TLSDESC_GOT entry when needed.
322 unsigned int tlsdesc_got_offset_
;
323 // List of available regions within the section, for incremental
325 Free_list free_list_
;
329 class Output_data_plt_x86_64_standard
: public Output_data_plt_x86_64
<size
>
332 Output_data_plt_x86_64_standard(Layout
* layout
,
333 Output_data_got
<64, false>* got
,
334 Output_data_got_plt_x86_64
* got_plt
,
335 Output_data_space
* got_irelative
)
336 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
337 got
, got_plt
, got_irelative
)
340 Output_data_plt_x86_64_standard(Layout
* layout
,
341 Output_data_got
<64, false>* got
,
342 Output_data_got_plt_x86_64
* got_plt
,
343 Output_data_space
* got_irelative
,
344 unsigned int plt_count
)
345 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
346 got
, got_plt
, got_irelative
,
352 do_get_plt_entry_size() const
353 { return plt_entry_size
; }
356 do_add_eh_frame(Layout
* layout
)
358 layout
->add_eh_frame_for_plt(this,
359 this->plt_eh_frame_cie
,
360 this->plt_eh_frame_cie_size
,
362 plt_eh_frame_fde_size
);
366 do_fill_first_plt_entry(unsigned char* pov
,
367 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
368 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
371 do_fill_plt_entry(unsigned char* pov
,
372 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
373 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
374 unsigned int got_offset
,
375 unsigned int plt_offset
,
376 unsigned int plt_index
);
379 do_fill_tlsdesc_entry(unsigned char* pov
,
380 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
381 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
382 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
383 unsigned int tlsdesc_got_offset
,
384 unsigned int plt_offset
);
387 // The size of an entry in the PLT.
388 static const int plt_entry_size
= 16;
390 // The first entry in the PLT.
391 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
392 // procedure linkage table for both programs and shared objects."
393 static const unsigned char first_plt_entry
[plt_entry_size
];
395 // Other entries in the PLT for an executable.
396 static const unsigned char plt_entry
[plt_entry_size
];
398 // The reserved TLSDESC entry in the PLT for an executable.
399 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
401 // The .eh_frame unwind information for the PLT.
402 static const int plt_eh_frame_fde_size
= 32;
403 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
406 // The x86_64 target class.
408 // http://www.x86-64.org/documentation/abi.pdf
409 // TLS info comes from
410 // http://people.redhat.com/drepper/tls.pdf
411 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
414 class Target_x86_64
: public Sized_target
<size
, false>
417 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
418 // uses only Elf64_Rela relocation entries with explicit addends."
419 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
421 Target_x86_64(const Target::Target_info
* info
= &x86_64_info
)
422 : Sized_target
<size
, false>(info
),
423 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_irelative_(NULL
),
424 got_tlsdesc_(NULL
), global_offset_table_(NULL
), rela_dyn_(NULL
),
425 rela_irelative_(NULL
), copy_relocs_(elfcpp::R_X86_64_COPY
),
426 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
427 tls_base_symbol_defined_(false)
430 // Hook for a new output section.
432 do_new_output_section(Output_section
*) const;
434 // Scan the relocations to look for symbol adjustments.
436 gc_process_relocs(Symbol_table
* symtab
,
438 Sized_relobj_file
<size
, false>* object
,
439 unsigned int data_shndx
,
440 unsigned int sh_type
,
441 const unsigned char* prelocs
,
443 Output_section
* output_section
,
444 bool needs_special_offset_handling
,
445 size_t local_symbol_count
,
446 const unsigned char* plocal_symbols
);
448 // Scan the relocations to look for symbol adjustments.
450 scan_relocs(Symbol_table
* symtab
,
452 Sized_relobj_file
<size
, false>* object
,
453 unsigned int data_shndx
,
454 unsigned int sh_type
,
455 const unsigned char* prelocs
,
457 Output_section
* output_section
,
458 bool needs_special_offset_handling
,
459 size_t local_symbol_count
,
460 const unsigned char* plocal_symbols
);
462 // Finalize the sections.
464 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
466 // Return the value to use for a dynamic which requires special
469 do_dynsym_value(const Symbol
*) const;
471 // Relocate a section.
473 relocate_section(const Relocate_info
<size
, false>*,
474 unsigned int sh_type
,
475 const unsigned char* prelocs
,
477 Output_section
* output_section
,
478 bool needs_special_offset_handling
,
480 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
481 section_size_type view_size
,
482 const Reloc_symbol_changes
*);
484 // Scan the relocs during a relocatable link.
486 scan_relocatable_relocs(Symbol_table
* symtab
,
488 Sized_relobj_file
<size
, false>* object
,
489 unsigned int data_shndx
,
490 unsigned int sh_type
,
491 const unsigned char* prelocs
,
493 Output_section
* output_section
,
494 bool needs_special_offset_handling
,
495 size_t local_symbol_count
,
496 const unsigned char* plocal_symbols
,
497 Relocatable_relocs
*);
499 // Emit relocations for a section.
502 const Relocate_info
<size
, false>*,
503 unsigned int sh_type
,
504 const unsigned char* prelocs
,
506 Output_section
* output_section
,
507 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
508 const Relocatable_relocs
*,
510 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
511 section_size_type view_size
,
512 unsigned char* reloc_view
,
513 section_size_type reloc_view_size
);
515 // Return a string used to fill a code section with nops.
517 do_code_fill(section_size_type length
) const;
519 // Return whether SYM is defined by the ABI.
521 do_is_defined_by_abi(const Symbol
* sym
) const
522 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
524 // Return the symbol index to use for a target specific relocation.
525 // The only target specific relocation is R_X86_64_TLSDESC for a
526 // local symbol, which is an absolute reloc.
528 do_reloc_symbol_index(void*, unsigned int r_type
) const
530 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
534 // Return the addend to use for a target specific relocation.
536 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
538 // Return the PLT section.
540 do_plt_address_for_global(const Symbol
* gsym
) const
541 { return this->plt_section()->address_for_global(gsym
); }
544 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
545 { return this->plt_section()->address_for_local(relobj
, symndx
); }
547 // This function should be defined in targets that can use relocation
548 // types to determine (implemented in local_reloc_may_be_function_pointer
549 // and global_reloc_may_be_function_pointer)
550 // if a function's pointer is taken. ICF uses this in safe mode to only
551 // fold those functions whose pointer is defintely not taken. For x86_64
552 // pie binaries, safe ICF cannot be done by looking at relocation types.
554 do_can_check_for_function_pointers() const
555 { return !parameters
->options().pie(); }
557 // Return the base for a DW_EH_PE_datarel encoding.
559 do_ehframe_datarel_base() const;
561 // Adjust -fsplit-stack code which calls non-split-stack code.
563 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
564 section_offset_type fnoffset
, section_size_type fnsize
,
565 unsigned char* view
, section_size_type view_size
,
566 std::string
* from
, std::string
* to
) const;
568 // Return the size of the GOT section.
572 gold_assert(this->got_
!= NULL
);
573 return this->got_
->data_size();
576 // Return the number of entries in the GOT.
578 got_entry_count() const
580 if (this->got_
== NULL
)
582 return this->got_size() / 8;
585 // Return the number of entries in the PLT.
587 plt_entry_count() const;
589 // Return the offset of the first non-reserved PLT entry.
591 first_plt_entry_offset() const;
593 // Return the size of each PLT entry.
595 plt_entry_size() const;
597 // Return the size of each GOT entry.
599 got_entry_size() const
602 // Create the GOT section for an incremental update.
603 Output_data_got_base
*
604 init_got_plt_for_update(Symbol_table
* symtab
,
606 unsigned int got_count
,
607 unsigned int plt_count
);
609 // Reserve a GOT entry for a local symbol, and regenerate any
610 // necessary dynamic relocations.
612 reserve_local_got_entry(unsigned int got_index
,
613 Sized_relobj
<size
, false>* obj
,
615 unsigned int got_type
);
617 // Reserve a GOT entry for a global symbol, and regenerate any
618 // necessary dynamic relocations.
620 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
621 unsigned int got_type
);
623 // Register an existing PLT entry for a global symbol.
625 register_global_plt_entry(Symbol_table
*, Layout
*, unsigned int plt_index
,
628 // Force a COPY relocation for a given symbol.
630 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
632 // Apply an incremental relocation.
634 apply_relocation(const Relocate_info
<size
, false>* relinfo
,
635 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
637 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
640 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
641 section_size_type view_size
);
643 // Add a new reloc argument, returning the index in the vector.
645 add_tlsdesc_info(Sized_relobj_file
<size
, false>* object
, unsigned int r_sym
)
647 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
648 return this->tlsdesc_reloc_info_
.size() - 1;
651 Output_data_plt_x86_64
<size
>*
652 make_data_plt(Layout
* layout
,
653 Output_data_got
<64, false>* got
,
654 Output_data_got_plt_x86_64
* got_plt
,
655 Output_data_space
* got_irelative
)
657 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
660 Output_data_plt_x86_64
<size
>*
661 make_data_plt(Layout
* layout
,
662 Output_data_got
<64, false>* got
,
663 Output_data_got_plt_x86_64
* got_plt
,
664 Output_data_space
* got_irelative
,
665 unsigned int plt_count
)
667 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
,
671 virtual Output_data_plt_x86_64
<size
>*
672 do_make_data_plt(Layout
* layout
,
673 Output_data_got
<64, false>* got
,
674 Output_data_got_plt_x86_64
* got_plt
,
675 Output_data_space
* got_irelative
)
677 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
681 virtual Output_data_plt_x86_64
<size
>*
682 do_make_data_plt(Layout
* layout
,
683 Output_data_got
<64, false>* got
,
684 Output_data_got_plt_x86_64
* got_plt
,
685 Output_data_space
* got_irelative
,
686 unsigned int plt_count
)
688 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
694 // The class which scans relocations.
699 : issued_non_pic_error_(false)
703 get_reference_flags(unsigned int r_type
);
706 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
707 Sized_relobj_file
<size
, false>* object
,
708 unsigned int data_shndx
,
709 Output_section
* output_section
,
710 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
711 const elfcpp::Sym
<size
, false>& lsym
,
715 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
716 Sized_relobj_file
<size
, false>* object
,
717 unsigned int data_shndx
,
718 Output_section
* output_section
,
719 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
723 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
724 Target_x86_64
* target
,
725 Sized_relobj_file
<size
, false>* object
,
726 unsigned int data_shndx
,
727 Output_section
* output_section
,
728 const elfcpp::Rela
<size
, false>& reloc
,
730 const elfcpp::Sym
<size
, false>& lsym
);
733 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
734 Target_x86_64
* target
,
735 Sized_relobj_file
<size
, false>* object
,
736 unsigned int data_shndx
,
737 Output_section
* output_section
,
738 const elfcpp::Rela
<size
, false>& reloc
,
744 unsupported_reloc_local(Sized_relobj_file
<size
, false>*,
745 unsigned int r_type
);
748 unsupported_reloc_global(Sized_relobj_file
<size
, false>*,
749 unsigned int r_type
, Symbol
*);
752 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
755 possible_function_pointer_reloc(unsigned int r_type
);
758 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, false>*,
759 unsigned int r_type
);
761 // Whether we have issued an error about a non-PIC compilation.
762 bool issued_non_pic_error_
;
765 // The class which implements relocation.
770 : skip_call_tls_get_addr_(false)
775 if (this->skip_call_tls_get_addr_
)
777 // FIXME: This needs to specify the location somehow.
778 gold_error(_("missing expected TLS relocation"));
782 // Do a relocation. Return false if the caller should not issue
783 // any warnings about this relocation.
785 relocate(const Relocate_info
<size
, false>*, Target_x86_64
*,
787 size_t relnum
, const elfcpp::Rela
<size
, false>&,
788 unsigned int r_type
, const Sized_symbol
<size
>*,
789 const Symbol_value
<size
>*,
790 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
794 // Do a TLS relocation.
796 relocate_tls(const Relocate_info
<size
, false>*, Target_x86_64
*,
797 size_t relnum
, const elfcpp::Rela
<size
, false>&,
798 unsigned int r_type
, const Sized_symbol
<size
>*,
799 const Symbol_value
<size
>*,
800 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
803 // Do a TLS General-Dynamic to Initial-Exec transition.
805 tls_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
806 Output_segment
* tls_segment
,
807 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
808 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
810 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
811 section_size_type view_size
);
813 // Do a TLS General-Dynamic to Local-Exec transition.
815 tls_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
816 Output_segment
* tls_segment
,
817 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
818 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
820 section_size_type view_size
);
822 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
824 tls_desc_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
825 Output_segment
* tls_segment
,
826 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
827 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
829 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
830 section_size_type view_size
);
832 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
834 tls_desc_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
835 Output_segment
* tls_segment
,
836 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
837 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
839 section_size_type view_size
);
841 // Do a TLS Local-Dynamic to Local-Exec transition.
843 tls_ld_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
844 Output_segment
* tls_segment
,
845 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
846 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
848 section_size_type view_size
);
850 // Do a TLS Initial-Exec to Local-Exec transition.
852 tls_ie_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
853 Output_segment
* tls_segment
,
854 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
855 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
857 section_size_type view_size
);
859 // This is set if we should skip the next reloc, which should be a
860 // PLT32 reloc against ___tls_get_addr.
861 bool skip_call_tls_get_addr_
;
864 // A class which returns the size required for a relocation type,
865 // used while scanning relocs during a relocatable link.
866 class Relocatable_size_for_reloc
870 get_size_for_reloc(unsigned int, Relobj
*);
873 // Check if relocation against this symbol is a candidate for
875 // mov foo@GOTPCREL(%rip), %reg
876 // to lea foo(%rip), %reg.
878 can_convert_mov_to_lea(const Symbol
* gsym
)
880 gold_assert(gsym
!= NULL
);
881 return (gsym
->type() != elfcpp::STT_GNU_IFUNC
882 && !gsym
->is_undefined ()
883 && !gsym
->is_from_dynobj()
884 && !gsym
->is_preemptible()
885 && (!parameters
->options().shared()
886 || (gsym
->visibility() != elfcpp::STV_DEFAULT
887 && gsym
->visibility() != elfcpp::STV_PROTECTED
)
888 || parameters
->options().Bsymbolic())
889 && strcmp(gsym
->name(), "_DYNAMIC") != 0);
892 // Adjust TLS relocation type based on the options and whether this
893 // is a local symbol.
894 static tls::Tls_optimization
895 optimize_tls_reloc(bool is_final
, int r_type
);
897 // Get the GOT section, creating it if necessary.
898 Output_data_got
<64, false>*
899 got_section(Symbol_table
*, Layout
*);
901 // Get the GOT PLT section.
902 Output_data_got_plt_x86_64
*
903 got_plt_section() const
905 gold_assert(this->got_plt_
!= NULL
);
906 return this->got_plt_
;
909 // Get the GOT section for TLSDESC entries.
910 Output_data_got
<64, false>*
911 got_tlsdesc_section() const
913 gold_assert(this->got_tlsdesc_
!= NULL
);
914 return this->got_tlsdesc_
;
917 // Create the PLT section.
919 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
921 // Create a PLT entry for a global symbol.
923 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
925 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
927 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
928 Sized_relobj_file
<size
, false>* relobj
,
929 unsigned int local_sym_index
);
931 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
933 define_tls_base_symbol(Symbol_table
*, Layout
*);
935 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
937 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
939 // Create a GOT entry for the TLS module index.
941 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
942 Sized_relobj_file
<size
, false>* object
);
944 // Get the PLT section.
945 Output_data_plt_x86_64
<size
>*
948 gold_assert(this->plt_
!= NULL
);
952 // Get the dynamic reloc section, creating it if necessary.
954 rela_dyn_section(Layout
*);
956 // Get the section to use for TLSDESC relocations.
958 rela_tlsdesc_section(Layout
*) const;
960 // Get the section to use for IRELATIVE relocations.
962 rela_irelative_section(Layout
*);
964 // Add a potential copy relocation.
966 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
967 Sized_relobj_file
<size
, false>* object
,
968 unsigned int shndx
, Output_section
* output_section
,
969 Symbol
* sym
, const elfcpp::Rela
<size
, false>& reloc
)
971 this->copy_relocs_
.copy_reloc(symtab
, layout
,
972 symtab
->get_sized_symbol
<size
>(sym
),
973 object
, shndx
, output_section
,
974 reloc
, this->rela_dyn_section(layout
));
977 // Information about this specific target which we pass to the
978 // general Target structure.
979 static const Target::Target_info x86_64_info
;
981 // The types of GOT entries needed for this platform.
982 // These values are exposed to the ABI in an incremental link.
983 // Do not renumber existing values without changing the version
984 // number of the .gnu_incremental_inputs section.
987 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
988 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
989 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
990 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
993 // This type is used as the argument to the target specific
994 // relocation routines. The only target specific reloc is
995 // R_X86_64_TLSDESC against a local symbol.
998 Tlsdesc_info(Sized_relobj_file
<size
, false>* a_object
, unsigned int a_r_sym
)
999 : object(a_object
), r_sym(a_r_sym
)
1002 // The object in which the local symbol is defined.
1003 Sized_relobj_file
<size
, false>* object
;
1004 // The local symbol index in the object.
1009 Output_data_got
<64, false>* got_
;
1011 Output_data_plt_x86_64
<size
>* plt_
;
1012 // The GOT PLT section.
1013 Output_data_got_plt_x86_64
* got_plt_
;
1014 // The GOT section for IRELATIVE relocations.
1015 Output_data_space
* got_irelative_
;
1016 // The GOT section for TLSDESC relocations.
1017 Output_data_got
<64, false>* got_tlsdesc_
;
1018 // The _GLOBAL_OFFSET_TABLE_ symbol.
1019 Symbol
* global_offset_table_
;
1020 // The dynamic reloc section.
1021 Reloc_section
* rela_dyn_
;
1022 // The section to use for IRELATIVE relocs.
1023 Reloc_section
* rela_irelative_
;
1024 // Relocs saved to avoid a COPY reloc.
1025 Copy_relocs
<elfcpp::SHT_RELA
, size
, false> copy_relocs_
;
1026 // Offset of the GOT entry for the TLS module index.
1027 unsigned int got_mod_index_offset_
;
1028 // We handle R_X86_64_TLSDESC against a local symbol as a target
1029 // specific relocation. Here we store the object and local symbol
1030 // index for the relocation.
1031 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
1032 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1033 bool tls_base_symbol_defined_
;
1037 const Target::Target_info Target_x86_64
<64>::x86_64_info
=
1040 false, // is_big_endian
1041 elfcpp::EM_X86_64
, // machine_code
1042 false, // has_make_symbol
1043 false, // has_resolve
1044 true, // has_code_fill
1045 true, // is_default_stack_executable
1046 true, // can_icf_inline_merge_sections
1048 "/lib/ld64.so.1", // program interpreter
1049 0x400000, // default_text_segment_address
1050 0x1000, // abi_pagesize (overridable by -z max-page-size)
1051 0x1000, // common_pagesize (overridable by -z common-page-size)
1052 false, // isolate_execinstr
1054 elfcpp::SHN_UNDEF
, // small_common_shndx
1055 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1056 0, // small_common_section_flags
1057 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1058 NULL
, // attributes_section
1059 NULL
, // attributes_vendor
1060 "_start", // entry_symbol_name
1061 32, // hash_entry_size
1065 const Target::Target_info Target_x86_64
<32>::x86_64_info
=
1068 false, // is_big_endian
1069 elfcpp::EM_X86_64
, // machine_code
1070 false, // has_make_symbol
1071 false, // has_resolve
1072 true, // has_code_fill
1073 true, // is_default_stack_executable
1074 true, // can_icf_inline_merge_sections
1076 "/libx32/ldx32.so.1", // program interpreter
1077 0x400000, // default_text_segment_address
1078 0x1000, // abi_pagesize (overridable by -z max-page-size)
1079 0x1000, // common_pagesize (overridable by -z common-page-size)
1080 false, // isolate_execinstr
1082 elfcpp::SHN_UNDEF
, // small_common_shndx
1083 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1084 0, // small_common_section_flags
1085 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1086 NULL
, // attributes_section
1087 NULL
, // attributes_vendor
1088 "_start", // entry_symbol_name
1089 32, // hash_entry_size
1092 // This is called when a new output section is created. This is where
1093 // we handle the SHF_X86_64_LARGE.
1097 Target_x86_64
<size
>::do_new_output_section(Output_section
* os
) const
1099 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
1100 os
->set_is_large_section();
1103 // Get the GOT section, creating it if necessary.
1106 Output_data_got
<64, false>*
1107 Target_x86_64
<size
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
1109 if (this->got_
== NULL
)
1111 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1113 // When using -z now, we can treat .got.plt as a relro section.
1114 // Without -z now, it is modified after program startup by lazy
1116 bool is_got_plt_relro
= parameters
->options().now();
1117 Output_section_order got_order
= (is_got_plt_relro
1119 : ORDER_RELRO_LAST
);
1120 Output_section_order got_plt_order
= (is_got_plt_relro
1122 : ORDER_NON_RELRO_FIRST
);
1124 this->got_
= new Output_data_got
<64, false>();
1126 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1128 | elfcpp::SHF_WRITE
),
1129 this->got_
, got_order
, true);
1131 this->got_plt_
= new Output_data_got_plt_x86_64(layout
);
1132 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1134 | elfcpp::SHF_WRITE
),
1135 this->got_plt_
, got_plt_order
,
1138 // The first three entries are reserved.
1139 this->got_plt_
->set_current_data_size(3 * 8);
1141 if (!is_got_plt_relro
)
1143 // Those bytes can go into the relro segment.
1144 layout
->increase_relro(3 * 8);
1147 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1148 this->global_offset_table_
=
1149 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1150 Symbol_table::PREDEFINED
,
1152 0, 0, elfcpp::STT_OBJECT
,
1154 elfcpp::STV_HIDDEN
, 0,
1157 // If there are any IRELATIVE relocations, they get GOT entries
1158 // in .got.plt after the jump slot entries.
1159 this->got_irelative_
= new Output_data_space(8, "** GOT IRELATIVE PLT");
1160 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1162 | elfcpp::SHF_WRITE
),
1163 this->got_irelative_
,
1164 got_plt_order
, is_got_plt_relro
);
1166 // If there are any TLSDESC relocations, they get GOT entries in
1167 // .got.plt after the jump slot and IRELATIVE entries.
1168 this->got_tlsdesc_
= new Output_data_got
<64, false>();
1169 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1171 | elfcpp::SHF_WRITE
),
1173 got_plt_order
, is_got_plt_relro
);
1179 // Get the dynamic reloc section, creating it if necessary.
1182 typename Target_x86_64
<size
>::Reloc_section
*
1183 Target_x86_64
<size
>::rela_dyn_section(Layout
* layout
)
1185 if (this->rela_dyn_
== NULL
)
1187 gold_assert(layout
!= NULL
);
1188 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1189 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1190 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1191 ORDER_DYNAMIC_RELOCS
, false);
1193 return this->rela_dyn_
;
1196 // Get the section to use for IRELATIVE relocs, creating it if
1197 // necessary. These go in .rela.dyn, but only after all other dynamic
1198 // relocations. They need to follow the other dynamic relocations so
1199 // that they can refer to global variables initialized by those
1203 typename Target_x86_64
<size
>::Reloc_section
*
1204 Target_x86_64
<size
>::rela_irelative_section(Layout
* layout
)
1206 if (this->rela_irelative_
== NULL
)
1208 // Make sure we have already created the dynamic reloc section.
1209 this->rela_dyn_section(layout
);
1210 this->rela_irelative_
= new Reloc_section(false);
1211 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1212 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
1213 ORDER_DYNAMIC_RELOCS
, false);
1214 gold_assert(this->rela_dyn_
->output_section()
1215 == this->rela_irelative_
->output_section());
1217 return this->rela_irelative_
;
1220 // Write the first three reserved words of the .got.plt section.
1221 // The remainder of the section is written while writing the PLT
1222 // in Output_data_plt_i386::do_write.
1225 Output_data_got_plt_x86_64::do_write(Output_file
* of
)
1227 // The first entry in the GOT is the address of the .dynamic section
1228 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1229 // We saved space for them when we created the section in
1230 // Target_x86_64::got_section.
1231 const off_t got_file_offset
= this->offset();
1232 gold_assert(this->data_size() >= 24);
1233 unsigned char* const got_view
= of
->get_output_view(got_file_offset
, 24);
1234 Output_section
* dynamic
= this->layout_
->dynamic_section();
1235 uint64_t dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
1236 elfcpp::Swap
<64, false>::writeval(got_view
, dynamic_addr
);
1237 memset(got_view
+ 8, 0, 16);
1238 of
->write_output_view(got_file_offset
, 24, got_view
);
1241 // Initialize the PLT section.
1245 Output_data_plt_x86_64
<size
>::init(Layout
* layout
)
1247 this->rel_
= new Reloc_section(false);
1248 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1249 elfcpp::SHF_ALLOC
, this->rel_
,
1250 ORDER_DYNAMIC_PLT_RELOCS
, false);
1255 Output_data_plt_x86_64
<size
>::do_adjust_output_section(Output_section
* os
)
1257 os
->set_entsize(this->get_plt_entry_size());
1260 // Add an entry to the PLT.
1264 Output_data_plt_x86_64
<size
>::add_entry(Symbol_table
* symtab
, Layout
* layout
,
1267 gold_assert(!gsym
->has_plt_offset());
1269 unsigned int plt_index
;
1271 section_offset_type got_offset
;
1273 unsigned int* pcount
;
1274 unsigned int offset
;
1275 unsigned int reserved
;
1276 Output_section_data_build
* got
;
1277 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1278 && gsym
->can_use_relative_reloc(false))
1280 pcount
= &this->irelative_count_
;
1283 got
= this->got_irelative_
;
1287 pcount
= &this->count_
;
1290 got
= this->got_plt_
;
1293 if (!this->is_data_size_valid())
1295 // Note that when setting the PLT offset for a non-IRELATIVE
1296 // entry we skip the initial reserved PLT entry.
1297 plt_index
= *pcount
+ offset
;
1298 plt_offset
= plt_index
* this->get_plt_entry_size();
1302 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1303 gold_assert(got_offset
== got
->current_data_size());
1305 // Every PLT entry needs a GOT entry which points back to the PLT
1306 // entry (this will be changed by the dynamic linker, normally
1307 // lazily when the function is called).
1308 got
->set_current_data_size(got_offset
+ 8);
1312 // FIXME: This is probably not correct for IRELATIVE relocs.
1314 // For incremental updates, find an available slot.
1315 plt_offset
= this->free_list_
.allocate(this->get_plt_entry_size(),
1316 this->get_plt_entry_size(), 0);
1317 if (plt_offset
== -1)
1318 gold_fallback(_("out of patch space (PLT);"
1319 " relink with --incremental-full"));
1321 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1322 // can be calculated from the PLT index, adjusting for the three
1323 // reserved entries at the beginning of the GOT.
1324 plt_index
= plt_offset
/ this->get_plt_entry_size() - 1;
1325 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1328 gsym
->set_plt_offset(plt_offset
);
1330 // Every PLT entry needs a reloc.
1331 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
1333 // Note that we don't need to save the symbol. The contents of the
1334 // PLT are independent of which symbols are used. The symbols only
1335 // appear in the relocations.
1338 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1343 Output_data_plt_x86_64
<size
>::add_local_ifunc_entry(
1344 Symbol_table
* symtab
,
1346 Sized_relobj_file
<size
, false>* relobj
,
1347 unsigned int local_sym_index
)
1349 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
1350 ++this->irelative_count_
;
1352 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
1354 // Every PLT entry needs a GOT entry which points back to the PLT
1356 this->got_irelative_
->set_current_data_size(got_offset
+ 8);
1358 // Every PLT entry needs a reloc.
1359 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1360 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
1361 elfcpp::R_X86_64_IRELATIVE
,
1362 this->got_irelative_
, got_offset
, 0);
1367 // Add the relocation for a PLT entry.
1371 Output_data_plt_x86_64
<size
>::add_relocation(Symbol_table
* symtab
,
1374 unsigned int got_offset
)
1376 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1377 && gsym
->can_use_relative_reloc(false))
1379 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1380 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
1381 this->got_irelative_
, got_offset
, 0);
1385 gsym
->set_needs_dynsym_entry();
1386 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
1391 // Return where the TLSDESC relocations should go, creating it if
1392 // necessary. These follow the JUMP_SLOT relocations.
1395 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1396 Output_data_plt_x86_64
<size
>::rela_tlsdesc(Layout
* layout
)
1398 if (this->tlsdesc_rel_
== NULL
)
1400 this->tlsdesc_rel_
= new Reloc_section(false);
1401 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1402 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
1403 ORDER_DYNAMIC_PLT_RELOCS
, false);
1404 gold_assert(this->tlsdesc_rel_
->output_section()
1405 == this->rel_
->output_section());
1407 return this->tlsdesc_rel_
;
1410 // Return where the IRELATIVE relocations should go in the PLT. These
1411 // follow the JUMP_SLOT and the TLSDESC relocations.
1414 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1415 Output_data_plt_x86_64
<size
>::rela_irelative(Symbol_table
* symtab
,
1418 if (this->irelative_rel_
== NULL
)
1420 // Make sure we have a place for the TLSDESC relocations, in
1421 // case we see any later on.
1422 this->rela_tlsdesc(layout
);
1423 this->irelative_rel_
= new Reloc_section(false);
1424 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1425 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
1426 ORDER_DYNAMIC_PLT_RELOCS
, false);
1427 gold_assert(this->irelative_rel_
->output_section()
1428 == this->rel_
->output_section());
1430 if (parameters
->doing_static_link())
1432 // A statically linked executable will only have a .rela.plt
1433 // section to hold R_X86_64_IRELATIVE relocs for
1434 // STT_GNU_IFUNC symbols. The library will use these
1435 // symbols to locate the IRELATIVE relocs at program startup
1437 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
1438 Symbol_table::PREDEFINED
,
1439 this->irelative_rel_
, 0, 0,
1440 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1441 elfcpp::STV_HIDDEN
, 0, false, true);
1442 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
1443 Symbol_table::PREDEFINED
,
1444 this->irelative_rel_
, 0, 0,
1445 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1446 elfcpp::STV_HIDDEN
, 0, true, true);
1449 return this->irelative_rel_
;
1452 // Return the PLT address to use for a global symbol.
1456 Output_data_plt_x86_64
<size
>::address_for_global(const Symbol
* gsym
)
1458 uint64_t offset
= 0;
1459 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1460 && gsym
->can_use_relative_reloc(false))
1461 offset
= (this->count_
+ 1) * this->get_plt_entry_size();
1462 return this->address() + offset
+ gsym
->plt_offset();
1465 // Return the PLT address to use for a local symbol. These are always
1466 // IRELATIVE relocs.
1470 Output_data_plt_x86_64
<size
>::address_for_local(const Relobj
* object
,
1473 return (this->address()
1474 + (this->count_
+ 1) * this->get_plt_entry_size()
1475 + object
->local_plt_offset(r_sym
));
1478 // Set the final size.
1481 Output_data_plt_x86_64
<size
>::set_final_data_size()
1483 unsigned int count
= this->count_
+ this->irelative_count_
;
1484 if (this->has_tlsdesc_entry())
1486 this->set_data_size((count
+ 1) * this->get_plt_entry_size());
1489 // The first entry in the PLT for an executable.
1493 Output_data_plt_x86_64_standard
<size
>::first_plt_entry
[plt_entry_size
] =
1495 // From AMD64 ABI Draft 0.98, page 76
1496 0xff, 0x35, // pushq contents of memory address
1497 0, 0, 0, 0, // replaced with address of .got + 8
1498 0xff, 0x25, // jmp indirect
1499 0, 0, 0, 0, // replaced with address of .got + 16
1500 0x90, 0x90, 0x90, 0x90 // noop (x4)
1505 Output_data_plt_x86_64_standard
<size
>::do_fill_first_plt_entry(
1507 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1508 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
1510 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1511 // We do a jmp relative to the PC at the end of this instruction.
1512 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1514 - (plt_address
+ 6)));
1515 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1517 - (plt_address
+ 12)));
1520 // Subsequent entries in the PLT for an executable.
1524 Output_data_plt_x86_64_standard
<size
>::plt_entry
[plt_entry_size
] =
1526 // From AMD64 ABI Draft 0.98, page 76
1527 0xff, 0x25, // jmpq indirect
1528 0, 0, 0, 0, // replaced with address of symbol in .got
1529 0x68, // pushq immediate
1530 0, 0, 0, 0, // replaced with offset into relocation table
1531 0xe9, // jmpq relative
1532 0, 0, 0, 0 // replaced with offset to start of .plt
1537 Output_data_plt_x86_64_standard
<size
>::do_fill_plt_entry(
1539 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1540 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1541 unsigned int got_offset
,
1542 unsigned int plt_offset
,
1543 unsigned int plt_index
)
1545 // Check PC-relative offset overflow in PLT entry.
1546 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
1547 - (plt_address
+ plt_offset
+ 6));
1548 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
1549 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1552 memcpy(pov
, plt_entry
, plt_entry_size
);
1553 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1554 plt_got_pcrel_offset
);
1556 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1557 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1558 - (plt_offset
+ plt_entry_size
));
1563 // The reserved TLSDESC entry in the PLT for an executable.
1567 Output_data_plt_x86_64_standard
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
1569 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1570 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1571 0xff, 0x35, // pushq x(%rip)
1572 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1573 0xff, 0x25, // jmpq *y(%rip)
1574 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1581 Output_data_plt_x86_64_standard
<size
>::do_fill_tlsdesc_entry(
1583 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1584 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1585 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
1586 unsigned int tlsdesc_got_offset
,
1587 unsigned int plt_offset
)
1589 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1590 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1592 - (plt_address
+ plt_offset
1594 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1596 + tlsdesc_got_offset
1597 - (plt_address
+ plt_offset
1601 // The .eh_frame unwind information for the PLT.
1605 Output_data_plt_x86_64
<size
>::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
1608 'z', // Augmentation: augmentation size included.
1609 'R', // Augmentation: FDE encoding included.
1610 '\0', // End of augmentation string.
1611 1, // Code alignment factor.
1612 0x78, // Data alignment factor.
1613 16, // Return address column.
1614 1, // Augmentation size.
1615 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
1616 | elfcpp::DW_EH_PE_sdata4
),
1617 elfcpp::DW_CFA_def_cfa
, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1618 elfcpp::DW_CFA_offset
+ 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1619 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
1625 Output_data_plt_x86_64_standard
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
1627 0, 0, 0, 0, // Replaced with offset to .plt.
1628 0, 0, 0, 0, // Replaced with size of .plt.
1629 0, // Augmentation size.
1630 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
1631 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
1632 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
1633 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
1634 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
1635 11, // Block length.
1636 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
1637 elfcpp::DW_OP_breg16
, 0, // Push %rip.
1638 elfcpp::DW_OP_lit15
, // Push 0xf.
1639 elfcpp::DW_OP_and
, // & (%rip & 0xf).
1640 elfcpp::DW_OP_lit11
, // Push 0xb.
1641 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 0xb)
1642 elfcpp::DW_OP_lit3
, // Push 3.
1643 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 0xb) << 3)
1644 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1645 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
1651 // Write out the PLT. This uses the hand-coded instructions above,
1652 // and adjusts them as needed. This is specified by the AMD64 ABI.
1656 Output_data_plt_x86_64
<size
>::do_write(Output_file
* of
)
1658 const off_t offset
= this->offset();
1659 const section_size_type oview_size
=
1660 convert_to_section_size_type(this->data_size());
1661 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1663 const off_t got_file_offset
= this->got_plt_
->offset();
1664 gold_assert(parameters
->incremental_update()
1665 || (got_file_offset
+ this->got_plt_
->data_size()
1666 == this->got_irelative_
->offset()));
1667 const section_size_type got_size
=
1668 convert_to_section_size_type(this->got_plt_
->data_size()
1669 + this->got_irelative_
->data_size());
1670 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1673 unsigned char* pov
= oview
;
1675 // The base address of the .plt section.
1676 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
1677 // The base address of the .got section.
1678 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
= this->got_
->address();
1679 // The base address of the PLT portion of the .got section,
1680 // which is where the GOT pointer will point, and where the
1681 // three reserved GOT entries are located.
1682 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
1683 = this->got_plt_
->address();
1685 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
1686 pov
+= this->get_plt_entry_size();
1688 // The first three entries in the GOT are reserved, and are written
1689 // by Output_data_got_plt_x86_64::do_write.
1690 unsigned char* got_pov
= got_view
+ 24;
1692 unsigned int plt_offset
= this->get_plt_entry_size();
1693 unsigned int got_offset
= 24;
1694 const unsigned int count
= this->count_
+ this->irelative_count_
;
1695 for (unsigned int plt_index
= 0;
1698 pov
+= this->get_plt_entry_size(),
1700 plt_offset
+= this->get_plt_entry_size(),
1703 // Set and adjust the PLT entry itself.
1704 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
1705 got_address
, plt_address
,
1706 got_offset
, plt_offset
,
1709 // Set the entry in the GOT.
1710 elfcpp::Swap
<64, false>::writeval(got_pov
,
1711 plt_address
+ plt_offset
+ lazy_offset
);
1714 if (this->has_tlsdesc_entry())
1716 // Set and adjust the reserved TLSDESC PLT entry.
1717 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
1718 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
1719 tlsdesc_got_offset
, plt_offset
);
1720 pov
+= this->get_plt_entry_size();
1723 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1724 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1726 of
->write_output_view(offset
, oview_size
, oview
);
1727 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1730 // Create the PLT section.
1734 Target_x86_64
<size
>::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1736 if (this->plt_
== NULL
)
1738 // Create the GOT sections first.
1739 this->got_section(symtab
, layout
);
1741 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
1742 this->got_irelative_
);
1744 // Add unwind information if requested.
1745 if (parameters
->options().ld_generated_unwind_info())
1746 this->plt_
->add_eh_frame(layout
);
1748 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1750 | elfcpp::SHF_EXECINSTR
),
1751 this->plt_
, ORDER_PLT
, false);
1753 // Make the sh_info field of .rela.plt point to .plt.
1754 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1755 rela_plt_os
->set_info_section(this->plt_
->output_section());
1759 // Return the section for TLSDESC relocations.
1762 typename Target_x86_64
<size
>::Reloc_section
*
1763 Target_x86_64
<size
>::rela_tlsdesc_section(Layout
* layout
) const
1765 return this->plt_section()->rela_tlsdesc(layout
);
1768 // Create a PLT entry for a global symbol.
1772 Target_x86_64
<size
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1775 if (gsym
->has_plt_offset())
1778 if (this->plt_
== NULL
)
1779 this->make_plt_section(symtab
, layout
);
1781 this->plt_
->add_entry(symtab
, layout
, gsym
);
1784 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1788 Target_x86_64
<size
>::make_local_ifunc_plt_entry(
1789 Symbol_table
* symtab
, Layout
* layout
,
1790 Sized_relobj_file
<size
, false>* relobj
,
1791 unsigned int local_sym_index
)
1793 if (relobj
->local_has_plt_offset(local_sym_index
))
1795 if (this->plt_
== NULL
)
1796 this->make_plt_section(symtab
, layout
);
1797 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
1800 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1803 // Return the number of entries in the PLT.
1807 Target_x86_64
<size
>::plt_entry_count() const
1809 if (this->plt_
== NULL
)
1811 return this->plt_
->entry_count();
1814 // Return the offset of the first non-reserved PLT entry.
1818 Target_x86_64
<size
>::first_plt_entry_offset() const
1820 return this->plt_
->first_plt_entry_offset();
1823 // Return the size of each PLT entry.
1827 Target_x86_64
<size
>::plt_entry_size() const
1829 return this->plt_
->get_plt_entry_size();
1832 // Create the GOT and PLT sections for an incremental update.
1835 Output_data_got_base
*
1836 Target_x86_64
<size
>::init_got_plt_for_update(Symbol_table
* symtab
,
1838 unsigned int got_count
,
1839 unsigned int plt_count
)
1841 gold_assert(this->got_
== NULL
);
1843 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
1844 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1846 | elfcpp::SHF_WRITE
),
1847 this->got_
, ORDER_RELRO_LAST
,
1850 // Add the three reserved entries.
1851 this->got_plt_
= new Output_data_got_plt_x86_64(layout
, (plt_count
+ 3) * 8);
1852 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1854 | elfcpp::SHF_WRITE
),
1855 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
1858 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1859 this->global_offset_table_
=
1860 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1861 Symbol_table::PREDEFINED
,
1863 0, 0, elfcpp::STT_OBJECT
,
1865 elfcpp::STV_HIDDEN
, 0,
1868 // If there are any TLSDESC relocations, they get GOT entries in
1869 // .got.plt after the jump slot entries.
1870 // FIXME: Get the count for TLSDESC entries.
1871 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
1872 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1873 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1875 ORDER_NON_RELRO_FIRST
, false);
1877 // If there are any IRELATIVE relocations, they get GOT entries in
1878 // .got.plt after the jump slot and TLSDESC entries.
1879 this->got_irelative_
= new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1880 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1881 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1882 this->got_irelative_
,
1883 ORDER_NON_RELRO_FIRST
, false);
1885 // Create the PLT section.
1886 this->plt_
= this->make_data_plt(layout
, this->got_
,
1888 this->got_irelative_
,
1891 // Add unwind information if requested.
1892 if (parameters
->options().ld_generated_unwind_info())
1893 this->plt_
->add_eh_frame(layout
);
1895 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1896 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1897 this->plt_
, ORDER_PLT
, false);
1899 // Make the sh_info field of .rela.plt point to .plt.
1900 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1901 rela_plt_os
->set_info_section(this->plt_
->output_section());
1903 // Create the rela_dyn section.
1904 this->rela_dyn_section(layout
);
1909 // Reserve a GOT entry for a local symbol, and regenerate any
1910 // necessary dynamic relocations.
1914 Target_x86_64
<size
>::reserve_local_got_entry(
1915 unsigned int got_index
,
1916 Sized_relobj
<size
, false>* obj
,
1918 unsigned int got_type
)
1920 unsigned int got_offset
= got_index
* 8;
1921 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1923 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
1926 case GOT_TYPE_STANDARD
:
1927 if (parameters
->options().output_is_position_independent())
1928 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
1929 this->got_
, got_offset
, 0, false);
1931 case GOT_TYPE_TLS_OFFSET
:
1932 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
1933 this->got_
, got_offset
, 0);
1935 case GOT_TYPE_TLS_PAIR
:
1936 this->got_
->reserve_slot(got_index
+ 1);
1937 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
1938 this->got_
, got_offset
, 0);
1940 case GOT_TYPE_TLS_DESC
:
1941 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1942 // this->got_->reserve_slot(got_index + 1);
1943 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1944 // this->got_, got_offset, 0);
1951 // Reserve a GOT entry for a global symbol, and regenerate any
1952 // necessary dynamic relocations.
1956 Target_x86_64
<size
>::reserve_global_got_entry(unsigned int got_index
,
1958 unsigned int got_type
)
1960 unsigned int got_offset
= got_index
* 8;
1961 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1963 this->got_
->reserve_global(got_index
, gsym
, got_type
);
1966 case GOT_TYPE_STANDARD
:
1967 if (!gsym
->final_value_is_known())
1969 if (gsym
->is_from_dynobj()
1970 || gsym
->is_undefined()
1971 || gsym
->is_preemptible()
1972 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1973 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
1974 this->got_
, got_offset
, 0);
1976 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1977 this->got_
, got_offset
, 0, false);
1980 case GOT_TYPE_TLS_OFFSET
:
1981 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
1982 this->got_
, got_offset
, 0, false);
1984 case GOT_TYPE_TLS_PAIR
:
1985 this->got_
->reserve_slot(got_index
+ 1);
1986 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
1987 this->got_
, got_offset
, 0, false);
1988 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
1989 this->got_
, got_offset
+ 8, 0, false);
1991 case GOT_TYPE_TLS_DESC
:
1992 this->got_
->reserve_slot(got_index
+ 1);
1993 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
1994 this->got_
, got_offset
, 0, false);
2001 // Register an existing PLT entry for a global symbol.
2005 Target_x86_64
<size
>::register_global_plt_entry(Symbol_table
* symtab
,
2007 unsigned int plt_index
,
2010 gold_assert(this->plt_
!= NULL
);
2011 gold_assert(!gsym
->has_plt_offset());
2013 this->plt_
->reserve_slot(plt_index
);
2015 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
2017 unsigned int got_offset
= (plt_index
+ 3) * 8;
2018 this->plt_
->add_relocation(symtab
, layout
, gsym
, got_offset
);
2021 // Force a COPY relocation for a given symbol.
2025 Target_x86_64
<size
>::emit_copy_reloc(
2026 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
2028 this->copy_relocs_
.emit_copy_reloc(symtab
,
2029 symtab
->get_sized_symbol
<size
>(sym
),
2032 this->rela_dyn_section(NULL
));
2035 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2039 Target_x86_64
<size
>::define_tls_base_symbol(Symbol_table
* symtab
,
2042 if (this->tls_base_symbol_defined_
)
2045 Output_segment
* tls_segment
= layout
->tls_segment();
2046 if (tls_segment
!= NULL
)
2048 bool is_exec
= parameters
->options().output_is_executable();
2049 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
2050 Symbol_table::PREDEFINED
,
2054 elfcpp::STV_HIDDEN
, 0,
2056 ? Symbol::SEGMENT_END
2057 : Symbol::SEGMENT_START
),
2060 this->tls_base_symbol_defined_
= true;
2063 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2067 Target_x86_64
<size
>::reserve_tlsdesc_entries(Symbol_table
* symtab
,
2070 if (this->plt_
== NULL
)
2071 this->make_plt_section(symtab
, layout
);
2073 if (!this->plt_
->has_tlsdesc_entry())
2075 // Allocate the TLSDESC_GOT entry.
2076 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2077 unsigned int got_offset
= got
->add_constant(0);
2079 // Allocate the TLSDESC_PLT entry.
2080 this->plt_
->reserve_tlsdesc_entry(got_offset
);
2084 // Create a GOT entry for the TLS module index.
2088 Target_x86_64
<size
>::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
2089 Sized_relobj_file
<size
, false>* object
)
2091 if (this->got_mod_index_offset_
== -1U)
2093 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
2094 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
2095 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2096 unsigned int got_offset
= got
->add_constant(0);
2097 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
2099 got
->add_constant(0);
2100 this->got_mod_index_offset_
= got_offset
;
2102 return this->got_mod_index_offset_
;
2105 // Optimize the TLS relocation type based on what we know about the
2106 // symbol. IS_FINAL is true if the final address of this symbol is
2107 // known at link time.
2110 tls::Tls_optimization
2111 Target_x86_64
<size
>::optimize_tls_reloc(bool is_final
, int r_type
)
2113 // If we are generating a shared library, then we can't do anything
2115 if (parameters
->options().shared())
2116 return tls::TLSOPT_NONE
;
2120 case elfcpp::R_X86_64_TLSGD
:
2121 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2122 case elfcpp::R_X86_64_TLSDESC_CALL
:
2123 // These are General-Dynamic which permits fully general TLS
2124 // access. Since we know that we are generating an executable,
2125 // we can convert this to Initial-Exec. If we also know that
2126 // this is a local symbol, we can further switch to Local-Exec.
2128 return tls::TLSOPT_TO_LE
;
2129 return tls::TLSOPT_TO_IE
;
2131 case elfcpp::R_X86_64_TLSLD
:
2132 // This is Local-Dynamic, which refers to a local symbol in the
2133 // dynamic TLS block. Since we know that we generating an
2134 // executable, we can switch to Local-Exec.
2135 return tls::TLSOPT_TO_LE
;
2137 case elfcpp::R_X86_64_DTPOFF32
:
2138 case elfcpp::R_X86_64_DTPOFF64
:
2139 // Another Local-Dynamic reloc.
2140 return tls::TLSOPT_TO_LE
;
2142 case elfcpp::R_X86_64_GOTTPOFF
:
2143 // These are Initial-Exec relocs which get the thread offset
2144 // from the GOT. If we know that we are linking against the
2145 // local symbol, we can switch to Local-Exec, which links the
2146 // thread offset into the instruction.
2148 return tls::TLSOPT_TO_LE
;
2149 return tls::TLSOPT_NONE
;
2151 case elfcpp::R_X86_64_TPOFF32
:
2152 // When we already have Local-Exec, there is nothing further we
2154 return tls::TLSOPT_NONE
;
2161 // Get the Reference_flags for a particular relocation.
2165 Target_x86_64
<size
>::Scan::get_reference_flags(unsigned int r_type
)
2169 case elfcpp::R_X86_64_NONE
:
2170 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2171 case elfcpp::R_X86_64_GNU_VTENTRY
:
2172 case elfcpp::R_X86_64_GOTPC32
:
2173 case elfcpp::R_X86_64_GOTPC64
:
2174 // No symbol reference.
2177 case elfcpp::R_X86_64_64
:
2178 case elfcpp::R_X86_64_32
:
2179 case elfcpp::R_X86_64_32S
:
2180 case elfcpp::R_X86_64_16
:
2181 case elfcpp::R_X86_64_8
:
2182 return Symbol::ABSOLUTE_REF
;
2184 case elfcpp::R_X86_64_PC64
:
2185 case elfcpp::R_X86_64_PC32
:
2186 case elfcpp::R_X86_64_PC32_BND
:
2187 case elfcpp::R_X86_64_PC16
:
2188 case elfcpp::R_X86_64_PC8
:
2189 case elfcpp::R_X86_64_GOTOFF64
:
2190 return Symbol::RELATIVE_REF
;
2192 case elfcpp::R_X86_64_PLT32
:
2193 case elfcpp::R_X86_64_PLT32_BND
:
2194 case elfcpp::R_X86_64_PLTOFF64
:
2195 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
2197 case elfcpp::R_X86_64_GOT64
:
2198 case elfcpp::R_X86_64_GOT32
:
2199 case elfcpp::R_X86_64_GOTPCREL64
:
2200 case elfcpp::R_X86_64_GOTPCREL
:
2201 case elfcpp::R_X86_64_GOTPCRELX
:
2202 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2203 case elfcpp::R_X86_64_GOTPLT64
:
2205 return Symbol::ABSOLUTE_REF
;
2207 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2208 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2209 case elfcpp::R_X86_64_TLSDESC_CALL
:
2210 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2211 case elfcpp::R_X86_64_DTPOFF32
:
2212 case elfcpp::R_X86_64_DTPOFF64
:
2213 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2214 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2215 return Symbol::TLS_REF
;
2217 case elfcpp::R_X86_64_COPY
:
2218 case elfcpp::R_X86_64_GLOB_DAT
:
2219 case elfcpp::R_X86_64_JUMP_SLOT
:
2220 case elfcpp::R_X86_64_RELATIVE
:
2221 case elfcpp::R_X86_64_IRELATIVE
:
2222 case elfcpp::R_X86_64_TPOFF64
:
2223 case elfcpp::R_X86_64_DTPMOD64
:
2224 case elfcpp::R_X86_64_TLSDESC
:
2225 case elfcpp::R_X86_64_SIZE32
:
2226 case elfcpp::R_X86_64_SIZE64
:
2228 // Not expected. We will give an error later.
2233 // Report an unsupported relocation against a local symbol.
2237 Target_x86_64
<size
>::Scan::unsupported_reloc_local(
2238 Sized_relobj_file
<size
, false>* object
,
2239 unsigned int r_type
)
2241 gold_error(_("%s: unsupported reloc %u against local symbol"),
2242 object
->name().c_str(), r_type
);
2245 // We are about to emit a dynamic relocation of type R_TYPE. If the
2246 // dynamic linker does not support it, issue an error. The GNU linker
2247 // only issues a non-PIC error for an allocated read-only section.
2248 // Here we know the section is allocated, but we don't know that it is
2249 // read-only. But we check for all the relocation types which the
2250 // glibc dynamic linker supports, so it seems appropriate to issue an
2251 // error even if the section is not read-only. If GSYM is not NULL,
2252 // it is the symbol the relocation is against; if it is NULL, the
2253 // relocation is against a local symbol.
2257 Target_x86_64
<size
>::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
2262 // These are the relocation types supported by glibc for x86_64
2263 // which should always work.
2264 case elfcpp::R_X86_64_RELATIVE
:
2265 case elfcpp::R_X86_64_IRELATIVE
:
2266 case elfcpp::R_X86_64_GLOB_DAT
:
2267 case elfcpp::R_X86_64_JUMP_SLOT
:
2268 case elfcpp::R_X86_64_DTPMOD64
:
2269 case elfcpp::R_X86_64_DTPOFF64
:
2270 case elfcpp::R_X86_64_TPOFF64
:
2271 case elfcpp::R_X86_64_64
:
2272 case elfcpp::R_X86_64_COPY
:
2275 // glibc supports these reloc types, but they can overflow.
2276 case elfcpp::R_X86_64_PC32
:
2277 case elfcpp::R_X86_64_PC32_BND
:
2278 // A PC relative reference is OK against a local symbol or if
2279 // the symbol is defined locally.
2281 || (!gsym
->is_from_dynobj()
2282 && !gsym
->is_undefined()
2283 && !gsym
->is_preemptible()))
2286 case elfcpp::R_X86_64_32
:
2287 // R_X86_64_32 is OK for x32.
2288 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2290 if (this->issued_non_pic_error_
)
2292 gold_assert(parameters
->options().output_is_position_independent());
2294 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
2295 "overflow at runtime; recompile with -fPIC"));
2301 case elfcpp::R_X86_64_32
:
2302 r_name
= "R_X86_64_32";
2304 case elfcpp::R_X86_64_PC32
:
2305 r_name
= "R_X86_64_PC32";
2307 case elfcpp::R_X86_64_PC32_BND
:
2308 r_name
= "R_X86_64_PC32_BND";
2314 object
->error(_("requires dynamic %s reloc against '%s' "
2315 "which may overflow at runtime; recompile "
2317 r_name
, gsym
->name());
2319 this->issued_non_pic_error_
= true;
2323 // This prevents us from issuing more than one error per reloc
2324 // section. But we can still wind up issuing more than one
2325 // error per object file.
2326 if (this->issued_non_pic_error_
)
2328 gold_assert(parameters
->options().output_is_position_independent());
2329 object
->error(_("requires unsupported dynamic reloc %u; "
2330 "recompile with -fPIC"),
2332 this->issued_non_pic_error_
= true;
2335 case elfcpp::R_X86_64_NONE
:
2340 // Return whether we need to make a PLT entry for a relocation of the
2341 // given type against a STT_GNU_IFUNC symbol.
2345 Target_x86_64
<size
>::Scan::reloc_needs_plt_for_ifunc(
2346 Sized_relobj_file
<size
, false>* object
,
2347 unsigned int r_type
)
2349 int flags
= Scan::get_reference_flags(r_type
);
2350 if (flags
& Symbol::TLS_REF
)
2351 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2352 object
->name().c_str(), r_type
);
2356 // Scan a relocation for a local symbol.
2360 Target_x86_64
<size
>::Scan::local(Symbol_table
* symtab
,
2362 Target_x86_64
<size
>* target
,
2363 Sized_relobj_file
<size
, false>* object
,
2364 unsigned int data_shndx
,
2365 Output_section
* output_section
,
2366 const elfcpp::Rela
<size
, false>& reloc
,
2367 unsigned int r_type
,
2368 const elfcpp::Sym
<size
, false>& lsym
,
2374 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2375 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
2376 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
2378 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2379 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
2384 case elfcpp::R_X86_64_NONE
:
2385 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2386 case elfcpp::R_X86_64_GNU_VTENTRY
:
2389 case elfcpp::R_X86_64_64
:
2390 // If building a shared library (or a position-independent
2391 // executable), we need to create a dynamic relocation for this
2392 // location. The relocation applied at link time will apply the
2393 // link-time value, so we flag the location with an
2394 // R_X86_64_RELATIVE relocation so the dynamic loader can
2395 // relocate it easily.
2396 if (parameters
->options().output_is_position_independent())
2398 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2399 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2400 rela_dyn
->add_local_relative(object
, r_sym
,
2402 ? elfcpp::R_X86_64_RELATIVE64
2403 : elfcpp::R_X86_64_RELATIVE
),
2404 output_section
, data_shndx
,
2405 reloc
.get_r_offset(),
2406 reloc
.get_r_addend(), is_ifunc
);
2410 case elfcpp::R_X86_64_32
:
2411 case elfcpp::R_X86_64_32S
:
2412 case elfcpp::R_X86_64_16
:
2413 case elfcpp::R_X86_64_8
:
2414 // If building a shared library (or a position-independent
2415 // executable), we need to create a dynamic relocation for this
2416 // location. We can't use an R_X86_64_RELATIVE relocation
2417 // because that is always a 64-bit relocation.
2418 if (parameters
->options().output_is_position_independent())
2420 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2421 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2423 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2424 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2425 rela_dyn
->add_local_relative(object
, r_sym
,
2426 elfcpp::R_X86_64_RELATIVE
,
2427 output_section
, data_shndx
,
2428 reloc
.get_r_offset(),
2429 reloc
.get_r_addend(), is_ifunc
);
2433 this->check_non_pic(object
, r_type
, NULL
);
2435 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2436 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2437 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
2438 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
2439 data_shndx
, reloc
.get_r_offset(),
2440 reloc
.get_r_addend());
2443 gold_assert(lsym
.get_st_value() == 0);
2444 unsigned int shndx
= lsym
.get_st_shndx();
2446 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
2449 object
->error(_("section symbol %u has bad shndx %u"),
2452 rela_dyn
->add_local_section(object
, shndx
,
2453 r_type
, output_section
,
2454 data_shndx
, reloc
.get_r_offset(),
2455 reloc
.get_r_addend());
2460 case elfcpp::R_X86_64_PC64
:
2461 case elfcpp::R_X86_64_PC32
:
2462 case elfcpp::R_X86_64_PC32_BND
:
2463 case elfcpp::R_X86_64_PC16
:
2464 case elfcpp::R_X86_64_PC8
:
2467 case elfcpp::R_X86_64_PLT32
:
2468 case elfcpp::R_X86_64_PLT32_BND
:
2469 // Since we know this is a local symbol, we can handle this as a
2473 case elfcpp::R_X86_64_GOTPC32
:
2474 case elfcpp::R_X86_64_GOTOFF64
:
2475 case elfcpp::R_X86_64_GOTPC64
:
2476 case elfcpp::R_X86_64_PLTOFF64
:
2477 // We need a GOT section.
2478 target
->got_section(symtab
, layout
);
2479 // For PLTOFF64, we'd normally want a PLT section, but since we
2480 // know this is a local symbol, no PLT is needed.
2483 case elfcpp::R_X86_64_GOT64
:
2484 case elfcpp::R_X86_64_GOT32
:
2485 case elfcpp::R_X86_64_GOTPCREL64
:
2486 case elfcpp::R_X86_64_GOTPCREL
:
2487 case elfcpp::R_X86_64_GOTPCRELX
:
2488 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2489 case elfcpp::R_X86_64_GOTPLT64
:
2491 // The symbol requires a GOT section.
2492 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2494 // If the relocation symbol isn't IFUNC,
2495 // and is local, then we will convert
2496 // mov foo@GOTPCREL(%rip), %reg
2497 // to lea foo(%rip), %reg.
2498 // in Relocate::relocate.
2499 if ((r_type
== elfcpp::R_X86_64_GOTPCREL
2500 || r_type
== elfcpp::R_X86_64_GOTPCRELX
2501 || r_type
== elfcpp::R_X86_64_REX_GOTPCRELX
)
2502 && reloc
.get_r_offset() >= 2
2505 section_size_type stype
;
2506 const unsigned char* view
= object
->section_contents(data_shndx
,
2508 if (view
[reloc
.get_r_offset() - 2] == 0x8b)
2513 // The symbol requires a GOT entry.
2514 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2516 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2517 // lets function pointers compare correctly with shared
2518 // libraries. Otherwise we would need an IRELATIVE reloc.
2521 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
2523 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
2526 // If we are generating a shared object, we need to add a
2527 // dynamic relocation for this symbol's GOT entry.
2528 if (parameters
->options().output_is_position_independent())
2530 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2531 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2532 if (r_type
!= elfcpp::R_X86_64_GOT32
)
2534 unsigned int got_offset
=
2535 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
2536 rela_dyn
->add_local_relative(object
, r_sym
,
2537 elfcpp::R_X86_64_RELATIVE
,
2538 got
, got_offset
, 0, is_ifunc
);
2542 this->check_non_pic(object
, r_type
, NULL
);
2544 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
2545 rela_dyn
->add_local(
2546 object
, r_sym
, r_type
, got
,
2547 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
2551 // For GOTPLT64, we'd normally want a PLT section, but since
2552 // we know this is a local symbol, no PLT is needed.
2556 case elfcpp::R_X86_64_COPY
:
2557 case elfcpp::R_X86_64_GLOB_DAT
:
2558 case elfcpp::R_X86_64_JUMP_SLOT
:
2559 case elfcpp::R_X86_64_RELATIVE
:
2560 case elfcpp::R_X86_64_IRELATIVE
:
2561 // These are outstanding tls relocs, which are unexpected when linking
2562 case elfcpp::R_X86_64_TPOFF64
:
2563 case elfcpp::R_X86_64_DTPMOD64
:
2564 case elfcpp::R_X86_64_TLSDESC
:
2565 gold_error(_("%s: unexpected reloc %u in object file"),
2566 object
->name().c_str(), r_type
);
2569 // These are initial tls relocs, which are expected when linking
2570 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2571 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2572 case elfcpp::R_X86_64_TLSDESC_CALL
:
2573 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2574 case elfcpp::R_X86_64_DTPOFF32
:
2575 case elfcpp::R_X86_64_DTPOFF64
:
2576 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2577 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2579 bool output_is_shared
= parameters
->options().shared();
2580 const tls::Tls_optimization optimized_type
2581 = Target_x86_64
<size
>::optimize_tls_reloc(!output_is_shared
,
2585 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2586 if (optimized_type
== tls::TLSOPT_NONE
)
2588 // Create a pair of GOT entries for the module index and
2589 // dtv-relative offset.
2590 Output_data_got
<64, false>* got
2591 = target
->got_section(symtab
, layout
);
2592 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2593 unsigned int shndx
= lsym
.get_st_shndx();
2595 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2597 object
->error(_("local symbol %u has bad shndx %u"),
2600 got
->add_local_pair_with_rel(object
, r_sym
,
2603 target
->rela_dyn_section(layout
),
2604 elfcpp::R_X86_64_DTPMOD64
);
2606 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2607 unsupported_reloc_local(object
, r_type
);
2610 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2611 target
->define_tls_base_symbol(symtab
, layout
);
2612 if (optimized_type
== tls::TLSOPT_NONE
)
2614 // Create reserved PLT and GOT entries for the resolver.
2615 target
->reserve_tlsdesc_entries(symtab
, layout
);
2617 // Generate a double GOT entry with an
2618 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2619 // is resolved lazily, so the GOT entry needs to be in
2620 // an area in .got.plt, not .got. Call got_section to
2621 // make sure the section has been created.
2622 target
->got_section(symtab
, layout
);
2623 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2624 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2625 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
2627 unsigned int got_offset
= got
->add_constant(0);
2628 got
->add_constant(0);
2629 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
2631 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2632 // We store the arguments we need in a vector, and
2633 // use the index into the vector as the parameter
2634 // to pass to the target specific routines.
2635 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
2636 void* arg
= reinterpret_cast<void*>(intarg
);
2637 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
2638 got
, got_offset
, 0);
2641 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2642 unsupported_reloc_local(object
, r_type
);
2645 case elfcpp::R_X86_64_TLSDESC_CALL
:
2648 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2649 if (optimized_type
== tls::TLSOPT_NONE
)
2651 // Create a GOT entry for the module index.
2652 target
->got_mod_index_entry(symtab
, layout
, object
);
2654 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2655 unsupported_reloc_local(object
, r_type
);
2658 case elfcpp::R_X86_64_DTPOFF32
:
2659 case elfcpp::R_X86_64_DTPOFF64
:
2662 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2663 layout
->set_has_static_tls();
2664 if (optimized_type
== tls::TLSOPT_NONE
)
2666 // Create a GOT entry for the tp-relative offset.
2667 Output_data_got
<64, false>* got
2668 = target
->got_section(symtab
, layout
);
2669 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2670 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
2671 target
->rela_dyn_section(layout
),
2672 elfcpp::R_X86_64_TPOFF64
);
2674 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2675 unsupported_reloc_local(object
, r_type
);
2678 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2679 layout
->set_has_static_tls();
2680 if (output_is_shared
)
2681 unsupported_reloc_local(object
, r_type
);
2690 case elfcpp::R_X86_64_SIZE32
:
2691 case elfcpp::R_X86_64_SIZE64
:
2693 gold_error(_("%s: unsupported reloc %u against local symbol"),
2694 object
->name().c_str(), r_type
);
2700 // Report an unsupported relocation against a global symbol.
2704 Target_x86_64
<size
>::Scan::unsupported_reloc_global(
2705 Sized_relobj_file
<size
, false>* object
,
2706 unsigned int r_type
,
2709 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2710 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
2713 // Returns true if this relocation type could be that of a function pointer.
2716 Target_x86_64
<size
>::Scan::possible_function_pointer_reloc(unsigned int r_type
)
2720 case elfcpp::R_X86_64_64
:
2721 case elfcpp::R_X86_64_32
:
2722 case elfcpp::R_X86_64_32S
:
2723 case elfcpp::R_X86_64_16
:
2724 case elfcpp::R_X86_64_8
:
2725 case elfcpp::R_X86_64_GOT64
:
2726 case elfcpp::R_X86_64_GOT32
:
2727 case elfcpp::R_X86_64_GOTPCREL64
:
2728 case elfcpp::R_X86_64_GOTPCREL
:
2729 case elfcpp::R_X86_64_GOTPCRELX
:
2730 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2731 case elfcpp::R_X86_64_GOTPLT64
:
2739 // For safe ICF, scan a relocation for a local symbol to check if it
2740 // corresponds to a function pointer being taken. In that case mark
2741 // the function whose pointer was taken as not foldable.
2745 Target_x86_64
<size
>::Scan::local_reloc_may_be_function_pointer(
2748 Target_x86_64
<size
>* ,
2749 Sized_relobj_file
<size
, false>* ,
2752 const elfcpp::Rela
<size
, false>& ,
2753 unsigned int r_type
,
2754 const elfcpp::Sym
<size
, false>&)
2756 // When building a shared library, do not fold any local symbols as it is
2757 // not possible to distinguish pointer taken versus a call by looking at
2758 // the relocation types.
2759 return (parameters
->options().shared()
2760 || possible_function_pointer_reloc(r_type
));
2763 // For safe ICF, scan a relocation for a global symbol to check if it
2764 // corresponds to a function pointer being taken. In that case mark
2765 // the function whose pointer was taken as not foldable.
2769 Target_x86_64
<size
>::Scan::global_reloc_may_be_function_pointer(
2772 Target_x86_64
<size
>* ,
2773 Sized_relobj_file
<size
, false>* ,
2776 const elfcpp::Rela
<size
, false>& ,
2777 unsigned int r_type
,
2780 // When building a shared library, do not fold symbols whose visibility
2781 // is hidden, internal or protected.
2782 return ((parameters
->options().shared()
2783 && (gsym
->visibility() == elfcpp::STV_INTERNAL
2784 || gsym
->visibility() == elfcpp::STV_PROTECTED
2785 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
2786 || possible_function_pointer_reloc(r_type
));
2789 // Scan a relocation for a global symbol.
2793 Target_x86_64
<size
>::Scan::global(Symbol_table
* symtab
,
2795 Target_x86_64
<size
>* target
,
2796 Sized_relobj_file
<size
, false>* object
,
2797 unsigned int data_shndx
,
2798 Output_section
* output_section
,
2799 const elfcpp::Rela
<size
, false>& reloc
,
2800 unsigned int r_type
,
2803 // A STT_GNU_IFUNC symbol may require a PLT entry.
2804 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2805 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
2806 target
->make_plt_entry(symtab
, layout
, gsym
);
2810 case elfcpp::R_X86_64_NONE
:
2811 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2812 case elfcpp::R_X86_64_GNU_VTENTRY
:
2815 case elfcpp::R_X86_64_64
:
2816 case elfcpp::R_X86_64_32
:
2817 case elfcpp::R_X86_64_32S
:
2818 case elfcpp::R_X86_64_16
:
2819 case elfcpp::R_X86_64_8
:
2821 // Make a PLT entry if necessary.
2822 if (gsym
->needs_plt_entry())
2824 target
->make_plt_entry(symtab
, layout
, gsym
);
2825 // Since this is not a PC-relative relocation, we may be
2826 // taking the address of a function. In that case we need to
2827 // set the entry in the dynamic symbol table to the address of
2829 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
2830 gsym
->set_needs_dynsym_value();
2832 // Make a dynamic relocation if necessary.
2833 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2835 if (!parameters
->options().output_is_position_independent()
2836 && gsym
->may_need_copy_reloc())
2838 target
->copy_reloc(symtab
, layout
, object
,
2839 data_shndx
, output_section
, gsym
, reloc
);
2841 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
2842 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
2843 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2844 && gsym
->can_use_relative_reloc(false)
2845 && !gsym
->is_from_dynobj()
2846 && !gsym
->is_undefined()
2847 && !gsym
->is_preemptible())
2849 // Use an IRELATIVE reloc for a locally defined
2850 // STT_GNU_IFUNC symbol. This makes a function
2851 // address in a PIE executable match the address in a
2852 // shared library that it links against.
2853 Reloc_section
* rela_dyn
=
2854 target
->rela_irelative_section(layout
);
2855 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
2856 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
2857 output_section
, object
,
2859 reloc
.get_r_offset(),
2860 reloc
.get_r_addend());
2862 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
2863 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
2864 && gsym
->can_use_relative_reloc(false))
2866 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2867 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2868 output_section
, object
,
2870 reloc
.get_r_offset(),
2871 reloc
.get_r_addend(), false);
2875 this->check_non_pic(object
, r_type
, gsym
);
2876 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2877 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2878 data_shndx
, reloc
.get_r_offset(),
2879 reloc
.get_r_addend());
2885 case elfcpp::R_X86_64_PC64
:
2886 case elfcpp::R_X86_64_PC32
:
2887 case elfcpp::R_X86_64_PC32_BND
:
2888 case elfcpp::R_X86_64_PC16
:
2889 case elfcpp::R_X86_64_PC8
:
2891 // Make a PLT entry if necessary.
2892 if (gsym
->needs_plt_entry())
2893 target
->make_plt_entry(symtab
, layout
, gsym
);
2894 // Make a dynamic relocation if necessary.
2895 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2897 if (parameters
->options().output_is_executable()
2898 && gsym
->may_need_copy_reloc())
2900 target
->copy_reloc(symtab
, layout
, object
,
2901 data_shndx
, output_section
, gsym
, reloc
);
2905 this->check_non_pic(object
, r_type
, gsym
);
2906 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2907 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2908 data_shndx
, reloc
.get_r_offset(),
2909 reloc
.get_r_addend());
2915 case elfcpp::R_X86_64_GOT64
:
2916 case elfcpp::R_X86_64_GOT32
:
2917 case elfcpp::R_X86_64_GOTPCREL64
:
2918 case elfcpp::R_X86_64_GOTPCREL
:
2919 case elfcpp::R_X86_64_GOTPCRELX
:
2920 case elfcpp::R_X86_64_REX_GOTPCRELX
:
2921 case elfcpp::R_X86_64_GOTPLT64
:
2923 // The symbol requires a GOT entry.
2924 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2926 // If we convert this from
2927 // mov foo@GOTPCREL(%rip), %reg
2928 // to lea foo(%rip), %reg.
2929 // in Relocate::relocate, then there is nothing to do here.
2930 if ((r_type
== elfcpp::R_X86_64_GOTPCREL
2931 || r_type
== elfcpp::R_X86_64_GOTPCRELX
2932 || r_type
== elfcpp::R_X86_64_REX_GOTPCRELX
)
2933 && reloc
.get_r_offset() >= 2
2934 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
))
2936 section_size_type stype
;
2937 const unsigned char* view
= object
->section_contents(data_shndx
,
2939 if (view
[reloc
.get_r_offset() - 2] == 0x8b)
2943 if (gsym
->final_value_is_known())
2945 // For a STT_GNU_IFUNC symbol we want the PLT address.
2946 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
2947 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2949 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2953 // If this symbol is not fully resolved, we need to add a
2954 // dynamic relocation for it.
2955 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2957 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2959 // 1) The symbol may be defined in some other module.
2961 // 2) We are building a shared library and this is a
2962 // protected symbol; using GLOB_DAT means that the dynamic
2963 // linker can use the address of the PLT in the main
2964 // executable when appropriate so that function address
2965 // comparisons work.
2967 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2968 // code, again so that function address comparisons work.
2969 if (gsym
->is_from_dynobj()
2970 || gsym
->is_undefined()
2971 || gsym
->is_preemptible()
2972 || (gsym
->visibility() == elfcpp::STV_PROTECTED
2973 && parameters
->options().shared())
2974 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
2975 && parameters
->options().output_is_position_independent()))
2976 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2977 elfcpp::R_X86_64_GLOB_DAT
);
2980 // For a STT_GNU_IFUNC symbol we want to write the PLT
2981 // offset into the GOT, so that function pointer
2982 // comparisons work correctly.
2984 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
2985 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2988 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2989 // Tell the dynamic linker to use the PLT address
2990 // when resolving relocations.
2991 if (gsym
->is_from_dynobj()
2992 && !parameters
->options().shared())
2993 gsym
->set_needs_dynsym_value();
2997 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2998 rela_dyn
->add_global_relative(gsym
,
2999 elfcpp::R_X86_64_RELATIVE
,
3000 got
, got_off
, 0, false);
3007 case elfcpp::R_X86_64_PLT32
:
3008 case elfcpp::R_X86_64_PLT32_BND
:
3009 // If the symbol is fully resolved, this is just a PC32 reloc.
3010 // Otherwise we need a PLT entry.
3011 if (gsym
->final_value_is_known())
3013 // If building a shared library, we can also skip the PLT entry
3014 // if the symbol is defined in the output file and is protected
3016 if (gsym
->is_defined()
3017 && !gsym
->is_from_dynobj()
3018 && !gsym
->is_preemptible())
3020 target
->make_plt_entry(symtab
, layout
, gsym
);
3023 case elfcpp::R_X86_64_GOTPC32
:
3024 case elfcpp::R_X86_64_GOTOFF64
:
3025 case elfcpp::R_X86_64_GOTPC64
:
3026 case elfcpp::R_X86_64_PLTOFF64
:
3027 // We need a GOT section.
3028 target
->got_section(symtab
, layout
);
3029 // For PLTOFF64, we also need a PLT entry (but only if the
3030 // symbol is not fully resolved).
3031 if (r_type
== elfcpp::R_X86_64_PLTOFF64
3032 && !gsym
->final_value_is_known())
3033 target
->make_plt_entry(symtab
, layout
, gsym
);
3036 case elfcpp::R_X86_64_COPY
:
3037 case elfcpp::R_X86_64_GLOB_DAT
:
3038 case elfcpp::R_X86_64_JUMP_SLOT
:
3039 case elfcpp::R_X86_64_RELATIVE
:
3040 case elfcpp::R_X86_64_IRELATIVE
:
3041 // These are outstanding tls relocs, which are unexpected when linking
3042 case elfcpp::R_X86_64_TPOFF64
:
3043 case elfcpp::R_X86_64_DTPMOD64
:
3044 case elfcpp::R_X86_64_TLSDESC
:
3045 gold_error(_("%s: unexpected reloc %u in object file"),
3046 object
->name().c_str(), r_type
);
3049 // These are initial tls relocs, which are expected for global()
3050 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3051 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3052 case elfcpp::R_X86_64_TLSDESC_CALL
:
3053 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3054 case elfcpp::R_X86_64_DTPOFF32
:
3055 case elfcpp::R_X86_64_DTPOFF64
:
3056 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3057 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3059 // For the Initial-Exec model, we can treat undef symbols as final
3060 // when building an executable.
3061 const bool is_final
= (gsym
->final_value_is_known() ||
3062 (r_type
== elfcpp::R_X86_64_GOTTPOFF
&&
3063 gsym
->is_undefined() &&
3064 parameters
->options().output_is_executable()));
3065 const tls::Tls_optimization optimized_type
3066 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3069 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
3070 if (optimized_type
== tls::TLSOPT_NONE
)
3072 // Create a pair of GOT entries for the module index and
3073 // dtv-relative offset.
3074 Output_data_got
<64, false>* got
3075 = target
->got_section(symtab
, layout
);
3076 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
3077 target
->rela_dyn_section(layout
),
3078 elfcpp::R_X86_64_DTPMOD64
,
3079 elfcpp::R_X86_64_DTPOFF64
);
3081 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3083 // Create a GOT entry for the tp-relative offset.
3084 Output_data_got
<64, false>* got
3085 = target
->got_section(symtab
, layout
);
3086 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3087 target
->rela_dyn_section(layout
),
3088 elfcpp::R_X86_64_TPOFF64
);
3090 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3091 unsupported_reloc_global(object
, r_type
, gsym
);
3094 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3095 target
->define_tls_base_symbol(symtab
, layout
);
3096 if (optimized_type
== tls::TLSOPT_NONE
)
3098 // Create reserved PLT and GOT entries for the resolver.
3099 target
->reserve_tlsdesc_entries(symtab
, layout
);
3101 // Create a double GOT entry with an R_X86_64_TLSDESC
3102 // reloc. The R_X86_64_TLSDESC reloc is resolved
3103 // lazily, so the GOT entry needs to be in an area in
3104 // .got.plt, not .got. Call got_section to make sure
3105 // the section has been created.
3106 target
->got_section(symtab
, layout
);
3107 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
3108 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
3109 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
3110 elfcpp::R_X86_64_TLSDESC
, 0);
3112 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3114 // Create a GOT entry for the tp-relative offset.
3115 Output_data_got
<64, false>* got
3116 = target
->got_section(symtab
, layout
);
3117 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3118 target
->rela_dyn_section(layout
),
3119 elfcpp::R_X86_64_TPOFF64
);
3121 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3122 unsupported_reloc_global(object
, r_type
, gsym
);
3125 case elfcpp::R_X86_64_TLSDESC_CALL
:
3128 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3129 if (optimized_type
== tls::TLSOPT_NONE
)
3131 // Create a GOT entry for the module index.
3132 target
->got_mod_index_entry(symtab
, layout
, object
);
3134 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3135 unsupported_reloc_global(object
, r_type
, gsym
);
3138 case elfcpp::R_X86_64_DTPOFF32
:
3139 case elfcpp::R_X86_64_DTPOFF64
:
3142 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3143 layout
->set_has_static_tls();
3144 if (optimized_type
== tls::TLSOPT_NONE
)
3146 // Create a GOT entry for the tp-relative offset.
3147 Output_data_got
<64, false>* got
3148 = target
->got_section(symtab
, layout
);
3149 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3150 target
->rela_dyn_section(layout
),
3151 elfcpp::R_X86_64_TPOFF64
);
3153 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3154 unsupported_reloc_global(object
, r_type
, gsym
);
3157 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3158 layout
->set_has_static_tls();
3159 if (parameters
->options().shared())
3160 unsupported_reloc_global(object
, r_type
, gsym
);
3169 case elfcpp::R_X86_64_SIZE32
:
3170 case elfcpp::R_X86_64_SIZE64
:
3172 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3173 object
->name().c_str(), r_type
,
3174 gsym
->demangled_name().c_str());
3181 Target_x86_64
<size
>::gc_process_relocs(Symbol_table
* symtab
,
3183 Sized_relobj_file
<size
, false>* object
,
3184 unsigned int data_shndx
,
3185 unsigned int sh_type
,
3186 const unsigned char* prelocs
,
3188 Output_section
* output_section
,
3189 bool needs_special_offset_handling
,
3190 size_t local_symbol_count
,
3191 const unsigned char* plocal_symbols
)
3194 if (sh_type
== elfcpp::SHT_REL
)
3199 gold::gc_process_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
3200 typename Target_x86_64
<size
>::Scan
,
3201 typename Target_x86_64
<size
>::Relocatable_size_for_reloc
>(
3210 needs_special_offset_handling
,
3215 // Scan relocations for a section.
3219 Target_x86_64
<size
>::scan_relocs(Symbol_table
* symtab
,
3221 Sized_relobj_file
<size
, false>* object
,
3222 unsigned int data_shndx
,
3223 unsigned int sh_type
,
3224 const unsigned char* prelocs
,
3226 Output_section
* output_section
,
3227 bool needs_special_offset_handling
,
3228 size_t local_symbol_count
,
3229 const unsigned char* plocal_symbols
)
3231 if (sh_type
== elfcpp::SHT_REL
)
3233 gold_error(_("%s: unsupported REL reloc section"),
3234 object
->name().c_str());
3238 gold::scan_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
3239 typename Target_x86_64
<size
>::Scan
>(
3248 needs_special_offset_handling
,
3253 // Finalize the sections.
3257 Target_x86_64
<size
>::do_finalize_sections(
3259 const Input_objects
*,
3260 Symbol_table
* symtab
)
3262 const Reloc_section
* rel_plt
= (this->plt_
== NULL
3264 : this->plt_
->rela_plt());
3265 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
3266 this->rela_dyn_
, true, false);
3268 // Fill in some more dynamic tags.
3269 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
3272 if (this->plt_
!= NULL
3273 && this->plt_
->output_section() != NULL
3274 && this->plt_
->has_tlsdesc_entry())
3276 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
3277 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
3278 this->got_
->finalize_data_size();
3279 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
3280 this->plt_
, plt_offset
);
3281 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
3282 this->got_
, got_offset
);
3286 // Emit any relocs we saved in an attempt to avoid generating COPY
3288 if (this->copy_relocs_
.any_saved_relocs())
3289 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
3291 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3292 // the .got.plt section.
3293 Symbol
* sym
= this->global_offset_table_
;
3296 uint64_t data_size
= this->got_plt_
->current_data_size();
3297 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
3300 if (parameters
->doing_static_link()
3301 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
3303 // If linking statically, make sure that the __rela_iplt symbols
3304 // were defined if necessary, even if we didn't create a PLT.
3305 static const Define_symbol_in_segment syms
[] =
3308 "__rela_iplt_start", // name
3309 elfcpp::PT_LOAD
, // segment_type
3310 elfcpp::PF_W
, // segment_flags_set
3311 elfcpp::PF(0), // segment_flags_clear
3314 elfcpp::STT_NOTYPE
, // type
3315 elfcpp::STB_GLOBAL
, // binding
3316 elfcpp::STV_HIDDEN
, // visibility
3318 Symbol::SEGMENT_START
, // offset_from_base
3322 "__rela_iplt_end", // name
3323 elfcpp::PT_LOAD
, // segment_type
3324 elfcpp::PF_W
, // segment_flags_set
3325 elfcpp::PF(0), // segment_flags_clear
3328 elfcpp::STT_NOTYPE
, // type
3329 elfcpp::STB_GLOBAL
, // binding
3330 elfcpp::STV_HIDDEN
, // visibility
3332 Symbol::SEGMENT_START
, // offset_from_base
3337 symtab
->define_symbols(layout
, 2, syms
,
3338 layout
->script_options()->saw_sections_clause());
3342 // Perform a relocation.
3346 Target_x86_64
<size
>::Relocate::relocate(
3347 const Relocate_info
<size
, false>* relinfo
,
3348 Target_x86_64
<size
>* target
,
3351 const elfcpp::Rela
<size
, false>& rela
,
3352 unsigned int r_type
,
3353 const Sized_symbol
<size
>* gsym
,
3354 const Symbol_value
<size
>* psymval
,
3355 unsigned char* view
,
3356 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3357 section_size_type view_size
)
3359 if (this->skip_call_tls_get_addr_
)
3361 if ((r_type
!= elfcpp::R_X86_64_PLT32
3362 && r_type
!= elfcpp::R_X86_64_PLT32_BND
3363 && r_type
!= elfcpp::R_X86_64_PC32_BND
3364 && r_type
!= elfcpp::R_X86_64_PC32
)
3366 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
3368 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3369 _("missing expected TLS relocation"));
3373 this->skip_call_tls_get_addr_
= false;
3381 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3383 // Pick the value to use for symbols defined in the PLT.
3384 Symbol_value
<size
> symval
;
3386 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
3388 symval
.set_output_value(target
->plt_address_for_global(gsym
));
3391 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
3393 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3394 if (object
->local_has_plt_offset(r_sym
))
3396 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
3401 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3403 // Get the GOT offset if needed.
3404 // The GOT pointer points to the end of the GOT section.
3405 // We need to subtract the size of the GOT section to get
3406 // the actual offset to use in the relocation.
3407 bool have_got_offset
= false;
3408 // Since the actual offset is always negative, we use signed int to
3409 // support 64-bit GOT relocations.
3413 case elfcpp::R_X86_64_GOT32
:
3414 case elfcpp::R_X86_64_GOT64
:
3415 case elfcpp::R_X86_64_GOTPLT64
:
3416 case elfcpp::R_X86_64_GOTPCREL64
:
3419 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3420 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
3424 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3425 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3426 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
3427 - target
->got_size());
3429 have_got_offset
= true;
3438 case elfcpp::R_X86_64_NONE
:
3439 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3440 case elfcpp::R_X86_64_GNU_VTENTRY
:
3443 case elfcpp::R_X86_64_64
:
3444 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
, addend
);
3447 case elfcpp::R_X86_64_PC64
:
3448 Relocate_functions
<size
, false>::pcrela64(view
, object
, psymval
, addend
,
3452 case elfcpp::R_X86_64_32
:
3453 // FIXME: we need to verify that value + addend fits into 32 bits:
3454 // uint64_t x = value + addend;
3455 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3456 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
3457 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3460 case elfcpp::R_X86_64_32S
:
3461 // FIXME: we need to verify that value + addend fits into 32 bits:
3462 // int64_t x = value + addend; // note this quantity is signed!
3463 // x == static_cast<int64_t>(static_cast<int32_t>(x))
3464 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3467 case elfcpp::R_X86_64_PC32
:
3468 case elfcpp::R_X86_64_PC32_BND
:
3469 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3473 case elfcpp::R_X86_64_16
:
3474 Relocate_functions
<size
, false>::rela16(view
, object
, psymval
, addend
);
3477 case elfcpp::R_X86_64_PC16
:
3478 Relocate_functions
<size
, false>::pcrela16(view
, object
, psymval
, addend
,
3482 case elfcpp::R_X86_64_8
:
3483 Relocate_functions
<size
, false>::rela8(view
, object
, psymval
, addend
);
3486 case elfcpp::R_X86_64_PC8
:
3487 Relocate_functions
<size
, false>::pcrela8(view
, object
, psymval
, addend
,
3491 case elfcpp::R_X86_64_PLT32
:
3492 case elfcpp::R_X86_64_PLT32_BND
:
3493 gold_assert(gsym
== NULL
3494 || gsym
->has_plt_offset()
3495 || gsym
->final_value_is_known()
3496 || (gsym
->is_defined()
3497 && !gsym
->is_from_dynobj()
3498 && !gsym
->is_preemptible()));
3499 // Note: while this code looks the same as for R_X86_64_PC32, it
3500 // behaves differently because psymval was set to point to
3501 // the PLT entry, rather than the symbol, in Scan::global().
3502 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3506 case elfcpp::R_X86_64_PLTOFF64
:
3509 gold_assert(gsym
->has_plt_offset()
3510 || gsym
->final_value_is_known());
3511 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
;
3512 // This is the address of GLOBAL_OFFSET_TABLE.
3513 got_address
= target
->got_plt_section()->address();
3514 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
,
3515 addend
- got_address
);
3519 case elfcpp::R_X86_64_GOT32
:
3520 gold_assert(have_got_offset
);
3521 Relocate_functions
<size
, false>::rela32(view
, got_offset
, addend
);
3524 case elfcpp::R_X86_64_GOTPC32
:
3527 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3528 value
= target
->got_plt_section()->address();
3529 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3533 case elfcpp::R_X86_64_GOT64
:
3534 case elfcpp::R_X86_64_GOTPLT64
:
3535 // R_X86_64_GOTPLT64 is obsolete and treated the the same as
3537 gold_assert(have_got_offset
);
3538 Relocate_functions
<size
, false>::rela64(view
, got_offset
, addend
);
3541 case elfcpp::R_X86_64_GOTPC64
:
3544 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3545 value
= target
->got_plt_section()->address();
3546 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3550 case elfcpp::R_X86_64_GOTOFF64
:
3552 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3553 value
= (psymval
->value(object
, 0)
3554 - target
->got_plt_section()->address());
3555 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3559 case elfcpp::R_X86_64_GOTPCREL
:
3560 case elfcpp::R_X86_64_GOTPCRELX
:
3561 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3564 // mov foo@GOTPCREL(%rip), %reg
3565 // to lea foo(%rip), %reg.
3567 if (rela
.get_r_offset() >= 2
3569 && ((gsym
== NULL
&& !psymval
->is_ifunc_symbol())
3571 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
))))
3574 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3581 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3582 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
3586 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3587 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3588 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
3589 - target
->got_size());
3591 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3592 value
= target
->got_plt_section()->address() + got_offset
;
3593 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3598 case elfcpp::R_X86_64_GOTPCREL64
:
3600 gold_assert(have_got_offset
);
3601 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3602 value
= target
->got_plt_section()->address() + got_offset
;
3603 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3607 case elfcpp::R_X86_64_COPY
:
3608 case elfcpp::R_X86_64_GLOB_DAT
:
3609 case elfcpp::R_X86_64_JUMP_SLOT
:
3610 case elfcpp::R_X86_64_RELATIVE
:
3611 case elfcpp::R_X86_64_IRELATIVE
:
3612 // These are outstanding tls relocs, which are unexpected when linking
3613 case elfcpp::R_X86_64_TPOFF64
:
3614 case elfcpp::R_X86_64_DTPMOD64
:
3615 case elfcpp::R_X86_64_TLSDESC
:
3616 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3617 _("unexpected reloc %u in object file"),
3621 // These are initial tls relocs, which are expected when linking
3622 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3623 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3624 case elfcpp::R_X86_64_TLSDESC_CALL
:
3625 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3626 case elfcpp::R_X86_64_DTPOFF32
:
3627 case elfcpp::R_X86_64_DTPOFF64
:
3628 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3629 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3630 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
3631 view
, address
, view_size
);
3634 case elfcpp::R_X86_64_SIZE32
:
3635 case elfcpp::R_X86_64_SIZE64
:
3637 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3638 _("unsupported reloc %u"),
3646 // Perform a TLS relocation.
3650 Target_x86_64
<size
>::Relocate::relocate_tls(
3651 const Relocate_info
<size
, false>* relinfo
,
3652 Target_x86_64
<size
>* target
,
3654 const elfcpp::Rela
<size
, false>& rela
,
3655 unsigned int r_type
,
3656 const Sized_symbol
<size
>* gsym
,
3657 const Symbol_value
<size
>* psymval
,
3658 unsigned char* view
,
3659 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3660 section_size_type view_size
)
3662 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
3664 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3665 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3666 elfcpp::Shdr
<size
, false> data_shdr(relinfo
->data_shdr
);
3667 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
3669 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
3671 const bool is_final
= (gsym
== NULL
3672 ? !parameters
->options().shared()
3673 : gsym
->final_value_is_known());
3674 tls::Tls_optimization optimized_type
3675 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3678 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3679 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3681 // If this code sequence is used in a non-executable section,
3682 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3683 // on the assumption that it's being used by itself in a debug
3684 // section. Therefore, in the unlikely event that the code
3685 // sequence appears in a non-executable section, we simply
3686 // leave it unoptimized.
3687 optimized_type
= tls::TLSOPT_NONE
;
3689 if (optimized_type
== tls::TLSOPT_TO_LE
)
3691 if (tls_segment
== NULL
)
3693 gold_assert(parameters
->errors()->error_count() > 0
3694 || issue_undefined_symbol_error(gsym
));
3697 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
3698 rela
, r_type
, value
, view
,
3704 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3705 ? GOT_TYPE_TLS_OFFSET
3706 : GOT_TYPE_TLS_PAIR
);
3707 unsigned int got_offset
;
3710 gold_assert(gsym
->has_got_offset(got_type
));
3711 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
3715 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3716 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3717 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
3718 - target
->got_size());
3720 if (optimized_type
== tls::TLSOPT_TO_IE
)
3722 value
= target
->got_plt_section()->address() + got_offset
;
3723 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3724 value
, view
, address
, view_size
);
3727 else if (optimized_type
== tls::TLSOPT_NONE
)
3729 // Relocate the field with the offset of the pair of GOT
3731 value
= target
->got_plt_section()->address() + got_offset
;
3732 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3737 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3738 _("unsupported reloc %u"), r_type
);
3741 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3742 case elfcpp::R_X86_64_TLSDESC_CALL
:
3743 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3745 // See above comment for R_X86_64_TLSGD.
3746 optimized_type
= tls::TLSOPT_NONE
;
3748 if (optimized_type
== tls::TLSOPT_TO_LE
)
3750 if (tls_segment
== NULL
)
3752 gold_assert(parameters
->errors()->error_count() > 0
3753 || issue_undefined_symbol_error(gsym
));
3756 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
3757 rela
, r_type
, value
, view
,
3763 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3764 ? GOT_TYPE_TLS_OFFSET
3765 : GOT_TYPE_TLS_DESC
);
3766 unsigned int got_offset
= 0;
3767 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
3768 && optimized_type
== tls::TLSOPT_NONE
)
3770 // We created GOT entries in the .got.tlsdesc portion of
3771 // the .got.plt section, but the offset stored in the
3772 // symbol is the offset within .got.tlsdesc.
3773 got_offset
= (target
->got_size()
3774 + target
->got_plt_section()->data_size());
3778 gold_assert(gsym
->has_got_offset(got_type
));
3779 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
3783 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3784 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3785 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
3786 - target
->got_size());
3788 if (optimized_type
== tls::TLSOPT_TO_IE
)
3790 if (tls_segment
== NULL
)
3792 gold_assert(parameters
->errors()->error_count() > 0
3793 || issue_undefined_symbol_error(gsym
));
3796 value
= target
->got_plt_section()->address() + got_offset
;
3797 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
3798 rela
, r_type
, value
, view
, address
,
3802 else if (optimized_type
== tls::TLSOPT_NONE
)
3804 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3806 // Relocate the field with the offset of the pair of GOT
3808 value
= target
->got_plt_section()->address() + got_offset
;
3809 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3815 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3816 _("unsupported reloc %u"), r_type
);
3819 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3820 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3822 // See above comment for R_X86_64_TLSGD.
3823 optimized_type
= tls::TLSOPT_NONE
;
3825 if (optimized_type
== tls::TLSOPT_TO_LE
)
3827 if (tls_segment
== NULL
)
3829 gold_assert(parameters
->errors()->error_count() > 0
3830 || issue_undefined_symbol_error(gsym
));
3833 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3834 value
, view
, view_size
);
3837 else if (optimized_type
== tls::TLSOPT_NONE
)
3839 // Relocate the field with the offset of the GOT entry for
3840 // the module index.
3841 unsigned int got_offset
;
3842 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
3843 - target
->got_size());
3844 value
= target
->got_plt_section()->address() + got_offset
;
3845 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3849 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3850 _("unsupported reloc %u"), r_type
);
3853 case elfcpp::R_X86_64_DTPOFF32
:
3854 // This relocation type is used in debugging information.
3855 // In that case we need to not optimize the value. If the
3856 // section is not executable, then we assume we should not
3857 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3858 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3860 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3862 if (tls_segment
== NULL
)
3864 gold_assert(parameters
->errors()->error_count() > 0
3865 || issue_undefined_symbol_error(gsym
));
3868 value
-= tls_segment
->memsz();
3870 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3873 case elfcpp::R_X86_64_DTPOFF64
:
3874 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3875 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3877 if (tls_segment
== NULL
)
3879 gold_assert(parameters
->errors()->error_count() > 0
3880 || issue_undefined_symbol_error(gsym
));
3883 value
-= tls_segment
->memsz();
3885 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3888 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3890 && gsym
->is_undefined()
3891 && parameters
->options().output_is_executable())
3893 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
3895 r_type
, value
, view
,
3899 else if (optimized_type
== tls::TLSOPT_TO_LE
)
3901 if (tls_segment
== NULL
)
3903 gold_assert(parameters
->errors()->error_count() > 0
3904 || issue_undefined_symbol_error(gsym
));
3907 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
3909 r_type
, value
, view
,
3913 else if (optimized_type
== tls::TLSOPT_NONE
)
3915 // Relocate the field with the offset of the GOT entry for
3916 // the tp-relative offset of the symbol.
3917 unsigned int got_offset
;
3920 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
3921 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
3922 - target
->got_size());
3926 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3927 gold_assert(object
->local_has_got_offset(r_sym
,
3928 GOT_TYPE_TLS_OFFSET
));
3929 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
3930 - target
->got_size());
3932 value
= target
->got_plt_section()->address() + got_offset
;
3933 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3937 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3938 _("unsupported reloc type %u"),
3942 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3943 if (tls_segment
== NULL
)
3945 gold_assert(parameters
->errors()->error_count() > 0
3946 || issue_undefined_symbol_error(gsym
));
3949 value
-= tls_segment
->memsz();
3950 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3955 // Do a relocation in which we convert a TLS General-Dynamic to an
3960 Target_x86_64
<size
>::Relocate::tls_gd_to_ie(
3961 const Relocate_info
<size
, false>* relinfo
,
3964 const elfcpp::Rela
<size
, false>& rela
,
3966 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3967 unsigned char* view
,
3968 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3969 section_size_type view_size
)
3972 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3973 // .word 0x6666; rex64; call __tls_get_addr
3974 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3976 // leaq foo@tlsgd(%rip),%rdi;
3977 // .word 0x6666; rex64; call __tls_get_addr
3978 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
3980 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3981 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3982 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3986 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3988 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3989 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3990 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3995 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3997 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3998 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
3999 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4003 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4004 Relocate_functions
<size
, false>::pcrela32(view
+ 8, value
, addend
- 8,
4007 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4009 this->skip_call_tls_get_addr_
= true;
4012 // Do a relocation in which we convert a TLS General-Dynamic to a
4017 Target_x86_64
<size
>::Relocate::tls_gd_to_le(
4018 const Relocate_info
<size
, false>* relinfo
,
4020 Output_segment
* tls_segment
,
4021 const elfcpp::Rela
<size
, false>& rela
,
4023 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4024 unsigned char* view
,
4025 section_size_type view_size
)
4028 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4029 // .word 0x6666; rex64; call __tls_get_addr
4030 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4032 // leaq foo@tlsgd(%rip),%rdi;
4033 // .word 0x6666; rex64; call __tls_get_addr
4034 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4036 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
4037 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4038 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
4042 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4044 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4045 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
4046 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4051 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
4053 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4054 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
4056 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4060 value
-= tls_segment
->memsz();
4061 Relocate_functions
<size
, false>::rela32(view
+ 8, value
, 0);
4063 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4065 this->skip_call_tls_get_addr_
= true;
4068 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4072 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_ie(
4073 const Relocate_info
<size
, false>* relinfo
,
4076 const elfcpp::Rela
<size
, false>& rela
,
4077 unsigned int r_type
,
4078 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4079 unsigned char* view
,
4080 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4081 section_size_type view_size
)
4083 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4085 // leaq foo@tlsdesc(%rip), %rax
4086 // ==> movq foo@gottpoff(%rip), %rax
4087 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4088 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4089 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4090 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
4092 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4093 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
4097 // call *foo@tlscall(%rax)
4099 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
4100 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
4101 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4102 view
[0] == 0xff && view
[1] == 0x10);
4108 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4112 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_le(
4113 const Relocate_info
<size
, false>* relinfo
,
4115 Output_segment
* tls_segment
,
4116 const elfcpp::Rela
<size
, false>& rela
,
4117 unsigned int r_type
,
4118 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4119 unsigned char* view
,
4120 section_size_type view_size
)
4122 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4124 // leaq foo@tlsdesc(%rip), %rax
4125 // ==> movq foo@tpoff, %rax
4126 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4127 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4128 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4129 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
4132 value
-= tls_segment
->memsz();
4133 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
4137 // call *foo@tlscall(%rax)
4139 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
4140 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
4141 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4142 view
[0] == 0xff && view
[1] == 0x10);
4150 Target_x86_64
<size
>::Relocate::tls_ld_to_le(
4151 const Relocate_info
<size
, false>* relinfo
,
4154 const elfcpp::Rela
<size
, false>& rela
,
4156 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
4157 unsigned char* view
,
4158 section_size_type view_size
)
4160 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4162 // ... leq foo@dtpoff(%rax),%reg
4163 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4165 // ... leq foo@dtpoff(%rax),%reg
4166 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4168 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4169 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
4171 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4172 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
4174 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
4177 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4179 memcpy(view
- 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4181 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4183 this->skip_call_tls_get_addr_
= true;
4186 // Do a relocation in which we convert a TLS Initial-Exec to a
4191 Target_x86_64
<size
>::Relocate::tls_ie_to_le(
4192 const Relocate_info
<size
, false>* relinfo
,
4194 Output_segment
* tls_segment
,
4195 const elfcpp::Rela
<size
, false>& rela
,
4197 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4198 unsigned char* view
,
4199 section_size_type view_size
)
4201 // We need to examine the opcodes to figure out which instruction we
4204 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4205 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4207 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4208 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4210 unsigned char op1
= view
[-3];
4211 unsigned char op2
= view
[-2];
4212 unsigned char op3
= view
[-1];
4213 unsigned char reg
= op3
>> 3;
4220 else if (size
== 32 && op1
== 0x44)
4223 view
[-1] = 0xc0 | reg
;
4227 // Special handling for %rsp.
4230 else if (size
== 32 && op1
== 0x44)
4233 view
[-1] = 0xc0 | reg
;
4240 else if (size
== 32 && op1
== 0x44)
4243 view
[-1] = 0x80 | reg
| (reg
<< 3);
4246 if (tls_segment
!= NULL
)
4247 value
-= tls_segment
->memsz();
4248 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
4251 // Relocate section data.
4255 Target_x86_64
<size
>::relocate_section(
4256 const Relocate_info
<size
, false>* relinfo
,
4257 unsigned int sh_type
,
4258 const unsigned char* prelocs
,
4260 Output_section
* output_section
,
4261 bool needs_special_offset_handling
,
4262 unsigned char* view
,
4263 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4264 section_size_type view_size
,
4265 const Reloc_symbol_changes
* reloc_symbol_changes
)
4267 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4269 gold::relocate_section
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
4270 typename Target_x86_64
<size
>::Relocate
,
4271 gold::Default_comdat_behavior
>(
4277 needs_special_offset_handling
,
4281 reloc_symbol_changes
);
4284 // Apply an incremental relocation. Incremental relocations always refer
4285 // to global symbols.
4289 Target_x86_64
<size
>::apply_relocation(
4290 const Relocate_info
<size
, false>* relinfo
,
4291 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
4292 unsigned int r_type
,
4293 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
4295 unsigned char* view
,
4296 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4297 section_size_type view_size
)
4299 gold::apply_relocation
<size
, false, Target_x86_64
<size
>,
4300 typename Target_x86_64
<size
>::Relocate
>(
4312 // Return the size of a relocation while scanning during a relocatable
4317 Target_x86_64
<size
>::Relocatable_size_for_reloc::get_size_for_reloc(
4318 unsigned int r_type
,
4323 case elfcpp::R_X86_64_NONE
:
4324 case elfcpp::R_X86_64_GNU_VTINHERIT
:
4325 case elfcpp::R_X86_64_GNU_VTENTRY
:
4326 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4327 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4328 case elfcpp::R_X86_64_TLSDESC_CALL
:
4329 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4330 case elfcpp::R_X86_64_DTPOFF32
:
4331 case elfcpp::R_X86_64_DTPOFF64
:
4332 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4333 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4336 case elfcpp::R_X86_64_64
:
4337 case elfcpp::R_X86_64_PC64
:
4338 case elfcpp::R_X86_64_GOTOFF64
:
4339 case elfcpp::R_X86_64_GOTPC64
:
4340 case elfcpp::R_X86_64_PLTOFF64
:
4341 case elfcpp::R_X86_64_GOT64
:
4342 case elfcpp::R_X86_64_GOTPCREL64
:
4343 case elfcpp::R_X86_64_GOTPCREL
:
4344 case elfcpp::R_X86_64_GOTPCRELX
:
4345 case elfcpp::R_X86_64_REX_GOTPCRELX
:
4346 case elfcpp::R_X86_64_GOTPLT64
:
4349 case elfcpp::R_X86_64_32
:
4350 case elfcpp::R_X86_64_32S
:
4351 case elfcpp::R_X86_64_PC32
:
4352 case elfcpp::R_X86_64_PC32_BND
:
4353 case elfcpp::R_X86_64_PLT32
:
4354 case elfcpp::R_X86_64_PLT32_BND
:
4355 case elfcpp::R_X86_64_GOTPC32
:
4356 case elfcpp::R_X86_64_GOT32
:
4359 case elfcpp::R_X86_64_16
:
4360 case elfcpp::R_X86_64_PC16
:
4363 case elfcpp::R_X86_64_8
:
4364 case elfcpp::R_X86_64_PC8
:
4367 case elfcpp::R_X86_64_COPY
:
4368 case elfcpp::R_X86_64_GLOB_DAT
:
4369 case elfcpp::R_X86_64_JUMP_SLOT
:
4370 case elfcpp::R_X86_64_RELATIVE
:
4371 case elfcpp::R_X86_64_IRELATIVE
:
4372 // These are outstanding tls relocs, which are unexpected when linking
4373 case elfcpp::R_X86_64_TPOFF64
:
4374 case elfcpp::R_X86_64_DTPMOD64
:
4375 case elfcpp::R_X86_64_TLSDESC
:
4376 object
->error(_("unexpected reloc %u in object file"), r_type
);
4379 case elfcpp::R_X86_64_SIZE32
:
4380 case elfcpp::R_X86_64_SIZE64
:
4382 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
4387 // Scan the relocs during a relocatable link.
4391 Target_x86_64
<size
>::scan_relocatable_relocs(
4392 Symbol_table
* symtab
,
4394 Sized_relobj_file
<size
, false>* object
,
4395 unsigned int data_shndx
,
4396 unsigned int sh_type
,
4397 const unsigned char* prelocs
,
4399 Output_section
* output_section
,
4400 bool needs_special_offset_handling
,
4401 size_t local_symbol_count
,
4402 const unsigned char* plocal_symbols
,
4403 Relocatable_relocs
* rr
)
4405 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4407 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
4408 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
4410 gold::scan_relocatable_relocs
<size
, false, elfcpp::SHT_RELA
,
4411 Scan_relocatable_relocs
>(
4419 needs_special_offset_handling
,
4425 // Relocate a section during a relocatable link.
4429 Target_x86_64
<size
>::relocate_relocs(
4430 const Relocate_info
<size
, false>* relinfo
,
4431 unsigned int sh_type
,
4432 const unsigned char* prelocs
,
4434 Output_section
* output_section
,
4435 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
4436 const Relocatable_relocs
* rr
,
4437 unsigned char* view
,
4438 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
4439 section_size_type view_size
,
4440 unsigned char* reloc_view
,
4441 section_size_type reloc_view_size
)
4443 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4445 gold::relocate_relocs
<size
, false, elfcpp::SHT_RELA
>(
4450 offset_in_output_section
,
4459 // Return the value to use for a dynamic which requires special
4460 // treatment. This is how we support equality comparisons of function
4461 // pointers across shared library boundaries, as described in the
4462 // processor specific ABI supplement.
4466 Target_x86_64
<size
>::do_dynsym_value(const Symbol
* gsym
) const
4468 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
4469 return this->plt_address_for_global(gsym
);
4472 // Return a string used to fill a code section with nops to take up
4473 // the specified length.
4477 Target_x86_64
<size
>::do_code_fill(section_size_type length
) const
4481 // Build a jmpq instruction to skip over the bytes.
4482 unsigned char jmp
[5];
4484 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
4485 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
4486 + std::string(length
- 5, static_cast<char>(0x90)));
4489 // Nop sequences of various lengths.
4490 const char nop1
[1] = { '\x90' }; // nop
4491 const char nop2
[2] = { '\x66', '\x90' }; // xchg %ax %ax
4492 const char nop3
[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4493 const char nop4
[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4495 const char nop5
[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4497 const char nop6
[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4498 '\x44', '\x00', '\x00' };
4499 const char nop7
[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4500 '\x00', '\x00', '\x00',
4502 const char nop8
[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4503 '\x00', '\x00', '\x00',
4505 const char nop9
[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4506 '\x84', '\x00', '\x00',
4507 '\x00', '\x00', '\x00' };
4508 const char nop10
[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4509 '\x1f', '\x84', '\x00',
4510 '\x00', '\x00', '\x00',
4512 const char nop11
[11] = { '\x66', '\x66', '\x2e', // data16
4513 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4514 '\x00', '\x00', '\x00',
4516 const char nop12
[12] = { '\x66', '\x66', '\x66', // data16; data16
4517 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4518 '\x84', '\x00', '\x00',
4519 '\x00', '\x00', '\x00' };
4520 const char nop13
[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4521 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4522 '\x1f', '\x84', '\x00',
4523 '\x00', '\x00', '\x00',
4525 const char nop14
[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4526 '\x66', '\x66', '\x2e', // data16
4527 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4528 '\x00', '\x00', '\x00',
4530 const char nop15
[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4531 '\x66', '\x66', '\x66', // data16; data16
4532 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4533 '\x84', '\x00', '\x00',
4534 '\x00', '\x00', '\x00' };
4536 const char* nops
[16] = {
4538 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
4539 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
4542 return std::string(nops
[length
], length
);
4545 // Return the addend to use for a target specific relocation. The
4546 // only target specific relocation is R_X86_64_TLSDESC for a local
4547 // symbol. We want to set the addend is the offset of the local
4548 // symbol in the TLS segment.
4552 Target_x86_64
<size
>::do_reloc_addend(void* arg
, unsigned int r_type
,
4555 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
4556 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
4557 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
4558 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
4559 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
4560 gold_assert(psymval
->is_tls_symbol());
4561 // The value of a TLS symbol is the offset in the TLS segment.
4562 return psymval
->value(ti
.object
, 0);
4565 // Return the value to use for the base of a DW_EH_PE_datarel offset
4566 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4567 // assembler can not write out the difference between two labels in
4568 // different sections, so instead of using a pc-relative value they
4569 // use an offset from the GOT.
4573 Target_x86_64
<size
>::do_ehframe_datarel_base() const
4575 gold_assert(this->global_offset_table_
!= NULL
);
4576 Symbol
* sym
= this->global_offset_table_
;
4577 Sized_symbol
<size
>* ssym
= static_cast<Sized_symbol
<size
>*>(sym
);
4578 return ssym
->value();
4581 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4582 // compiled with -fsplit-stack. The function calls non-split-stack
4583 // code. We have to change the function so that it always ensures
4584 // that it has enough stack space to run some random function.
4586 static const unsigned char cmp_insn_32
[] = { 0x64, 0x3b, 0x24, 0x25 };
4587 static const unsigned char lea_r10_insn_32
[] = { 0x44, 0x8d, 0x94, 0x24 };
4588 static const unsigned char lea_r11_insn_32
[] = { 0x44, 0x8d, 0x9c, 0x24 };
4590 static const unsigned char cmp_insn_64
[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
4591 static const unsigned char lea_r10_insn_64
[] = { 0x4c, 0x8d, 0x94, 0x24 };
4592 static const unsigned char lea_r11_insn_64
[] = { 0x4c, 0x8d, 0x9c, 0x24 };
4596 Target_x86_64
<size
>::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
4597 section_offset_type fnoffset
,
4598 section_size_type fnsize
,
4599 unsigned char* view
,
4600 section_size_type view_size
,
4602 std::string
* to
) const
4604 const char* const cmp_insn
= reinterpret_cast<const char*>
4605 (size
== 32 ? cmp_insn_32
: cmp_insn_64
);
4606 const char* const lea_r10_insn
= reinterpret_cast<const char*>
4607 (size
== 32 ? lea_r10_insn_32
: lea_r10_insn_64
);
4608 const char* const lea_r11_insn
= reinterpret_cast<const char*>
4609 (size
== 32 ? lea_r11_insn_32
: lea_r11_insn_64
);
4611 const size_t cmp_insn_len
=
4612 (size
== 32 ? sizeof(cmp_insn_32
) : sizeof(cmp_insn_64
));
4613 const size_t lea_r10_insn_len
=
4614 (size
== 32 ? sizeof(lea_r10_insn_32
) : sizeof(lea_r10_insn_64
));
4615 const size_t lea_r11_insn_len
=
4616 (size
== 32 ? sizeof(lea_r11_insn_32
) : sizeof(lea_r11_insn_64
));
4617 const size_t nop_len
= (size
== 32 ? 7 : 8);
4619 // The function starts with a comparison of the stack pointer and a
4620 // field in the TCB. This is followed by a jump.
4623 if (this->match_view(view
, view_size
, fnoffset
, cmp_insn
, cmp_insn_len
)
4624 && fnsize
> nop_len
+ 1)
4626 // We will call __morestack if the carry flag is set after this
4627 // comparison. We turn the comparison into an stc instruction
4629 view
[fnoffset
] = '\xf9';
4630 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, nop_len
);
4632 // lea NN(%rsp),%r10
4633 // lea NN(%rsp),%r11
4634 else if ((this->match_view(view
, view_size
, fnoffset
,
4635 lea_r10_insn
, lea_r10_insn_len
)
4636 || this->match_view(view
, view_size
, fnoffset
,
4637 lea_r11_insn
, lea_r11_insn_len
))
4640 // This is loading an offset from the stack pointer for a
4641 // comparison. The offset is negative, so we decrease the
4642 // offset by the amount of space we need for the stack. This
4643 // means we will avoid calling __morestack if there happens to
4644 // be plenty of space on the stack already.
4645 unsigned char* pval
= view
+ fnoffset
+ 4;
4646 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
4647 val
-= parameters
->options().split_stack_adjust_size();
4648 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
4652 if (!object
->has_no_split_stack())
4653 object
->error(_("failed to match split-stack sequence at "
4654 "section %u offset %0zx"),
4655 shndx
, static_cast<size_t>(fnoffset
));
4659 // We have to change the function so that it calls
4660 // __morestack_non_split instead of __morestack. The former will
4661 // allocate additional stack space.
4662 *from
= "__morestack";
4663 *to
= "__morestack_non_split";
4666 // The selector for x86_64 object files. Note this is never instantiated
4667 // directly. It's only used in Target_selector_x86_64_nacl, below.
4670 class Target_selector_x86_64
: public Target_selector_freebsd
4673 Target_selector_x86_64()
4674 : Target_selector_freebsd(elfcpp::EM_X86_64
, size
, false,
4676 ? "elf64-x86-64" : "elf32-x86-64"),
4678 ? "elf64-x86-64-freebsd"
4679 : "elf32-x86-64-freebsd"),
4680 (size
== 64 ? "elf_x86_64" : "elf32_x86_64"))
4684 do_instantiate_target()
4685 { return new Target_x86_64
<size
>(); }
4689 // NaCl variant. It uses different PLT contents.
4692 class Output_data_plt_x86_64_nacl
: public Output_data_plt_x86_64
<size
>
4695 Output_data_plt_x86_64_nacl(Layout
* layout
,
4696 Output_data_got
<64, false>* got
,
4697 Output_data_got_plt_x86_64
* got_plt
,
4698 Output_data_space
* got_irelative
)
4699 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4700 got
, got_plt
, got_irelative
)
4703 Output_data_plt_x86_64_nacl(Layout
* layout
,
4704 Output_data_got
<64, false>* got
,
4705 Output_data_got_plt_x86_64
* got_plt
,
4706 Output_data_space
* got_irelative
,
4707 unsigned int plt_count
)
4708 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4709 got
, got_plt
, got_irelative
,
4714 virtual unsigned int
4715 do_get_plt_entry_size() const
4716 { return plt_entry_size
; }
4719 do_add_eh_frame(Layout
* layout
)
4721 layout
->add_eh_frame_for_plt(this,
4722 this->plt_eh_frame_cie
,
4723 this->plt_eh_frame_cie_size
,
4725 plt_eh_frame_fde_size
);
4729 do_fill_first_plt_entry(unsigned char* pov
,
4730 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
4731 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
4733 virtual unsigned int
4734 do_fill_plt_entry(unsigned char* pov
,
4735 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4736 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4737 unsigned int got_offset
,
4738 unsigned int plt_offset
,
4739 unsigned int plt_index
);
4742 do_fill_tlsdesc_entry(unsigned char* pov
,
4743 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4744 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4745 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
4746 unsigned int tlsdesc_got_offset
,
4747 unsigned int plt_offset
);
4750 // The size of an entry in the PLT.
4751 static const int plt_entry_size
= 64;
4753 // The first entry in the PLT.
4754 static const unsigned char first_plt_entry
[plt_entry_size
];
4756 // Other entries in the PLT for an executable.
4757 static const unsigned char plt_entry
[plt_entry_size
];
4759 // The reserved TLSDESC entry in the PLT for an executable.
4760 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
4762 // The .eh_frame unwind information for the PLT.
4763 static const int plt_eh_frame_fde_size
= 32;
4764 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
4768 class Target_x86_64_nacl
: public Target_x86_64
<size
>
4771 Target_x86_64_nacl()
4772 : Target_x86_64
<size
>(&x86_64_nacl_info
)
4775 virtual Output_data_plt_x86_64
<size
>*
4776 do_make_data_plt(Layout
* layout
,
4777 Output_data_got
<64, false>* got
,
4778 Output_data_got_plt_x86_64
* got_plt
,
4779 Output_data_space
* got_irelative
)
4781 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4785 virtual Output_data_plt_x86_64
<size
>*
4786 do_make_data_plt(Layout
* layout
,
4787 Output_data_got
<64, false>* got
,
4788 Output_data_got_plt_x86_64
* got_plt
,
4789 Output_data_space
* got_irelative
,
4790 unsigned int plt_count
)
4792 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4798 do_code_fill(section_size_type length
) const;
4801 static const Target::Target_info x86_64_nacl_info
;
4805 const Target::Target_info Target_x86_64_nacl
<64>::x86_64_nacl_info
=
4808 false, // is_big_endian
4809 elfcpp::EM_X86_64
, // machine_code
4810 false, // has_make_symbol
4811 false, // has_resolve
4812 true, // has_code_fill
4813 true, // is_default_stack_executable
4814 true, // can_icf_inline_merge_sections
4816 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4817 0x20000, // default_text_segment_address
4818 0x10000, // abi_pagesize (overridable by -z max-page-size)
4819 0x10000, // common_pagesize (overridable by -z common-page-size)
4820 true, // isolate_execinstr
4821 0x10000000, // rosegment_gap
4822 elfcpp::SHN_UNDEF
, // small_common_shndx
4823 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
4824 0, // small_common_section_flags
4825 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
4826 NULL
, // attributes_section
4827 NULL
, // attributes_vendor
4828 "_start", // entry_symbol_name
4829 32, // hash_entry_size
4833 const Target::Target_info Target_x86_64_nacl
<32>::x86_64_nacl_info
=
4836 false, // is_big_endian
4837 elfcpp::EM_X86_64
, // machine_code
4838 false, // has_make_symbol
4839 false, // has_resolve
4840 true, // has_code_fill
4841 true, // is_default_stack_executable
4842 true, // can_icf_inline_merge_sections
4844 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
4845 0x20000, // default_text_segment_address
4846 0x10000, // abi_pagesize (overridable by -z max-page-size)
4847 0x10000, // common_pagesize (overridable by -z common-page-size)
4848 true, // isolate_execinstr
4849 0x10000000, // rosegment_gap
4850 elfcpp::SHN_UNDEF
, // small_common_shndx
4851 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
4852 0, // small_common_section_flags
4853 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
4854 NULL
, // attributes_section
4855 NULL
, // attributes_vendor
4856 "_start", // entry_symbol_name
4857 32, // hash_entry_size
4860 #define NACLMASK 0xe0 // 32-byte alignment mask.
4862 // The first entry in the PLT.
4866 Output_data_plt_x86_64_nacl
<size
>::first_plt_entry
[plt_entry_size
] =
4868 0xff, 0x35, // pushq contents of memory address
4869 0, 0, 0, 0, // replaced with address of .got + 8
4870 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
4871 0, 0, 0, 0, // replaced with address of .got + 16
4872 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4873 0x4d, 0x01, 0xfb, // add %r15, %r11
4874 0x41, 0xff, 0xe3, // jmpq *%r11
4876 // 9-byte nop sequence to pad out to the next 32-byte boundary.
4877 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
4879 // 32 bytes of nop to pad out to the standard size
4880 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4881 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4882 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4883 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4884 0x66, // excess data32 prefix
4890 Output_data_plt_x86_64_nacl
<size
>::do_fill_first_plt_entry(
4892 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4893 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
4895 memcpy(pov
, first_plt_entry
, plt_entry_size
);
4896 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
4898 - (plt_address
+ 2 + 4)));
4899 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
4901 - (plt_address
+ 9 + 4)));
4904 // Subsequent entries in the PLT.
4908 Output_data_plt_x86_64_nacl
<size
>::plt_entry
[plt_entry_size
] =
4910 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
4911 0, 0, 0, 0, // replaced with address of symbol in .got
4912 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4913 0x4d, 0x01, 0xfb, // add %r15, %r11
4914 0x41, 0xff, 0xe3, // jmpq *%r11
4916 // 15-byte nop sequence to pad out to the next 32-byte boundary.
4917 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4918 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4920 // Lazy GOT entries point here (32-byte aligned).
4921 0x68, // pushq immediate
4922 0, 0, 0, 0, // replaced with index into relocation table
4923 0xe9, // jmp relative
4924 0, 0, 0, 0, // replaced with offset to start of .plt0
4926 // 22 bytes of nop to pad out to the standard size.
4927 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4928 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4929 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
4934 Output_data_plt_x86_64_nacl
<size
>::do_fill_plt_entry(
4936 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4937 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4938 unsigned int got_offset
,
4939 unsigned int plt_offset
,
4940 unsigned int plt_index
)
4942 memcpy(pov
, plt_entry
, plt_entry_size
);
4943 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3,
4944 (got_address
+ got_offset
4945 - (plt_address
+ plt_offset
4948 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 33, plt_index
);
4949 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 38,
4950 - (plt_offset
+ 38 + 4));
4955 // The reserved TLSDESC entry in the PLT.
4959 Output_data_plt_x86_64_nacl
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
4961 0xff, 0x35, // pushq x(%rip)
4962 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
4963 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
4964 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
4965 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4966 0x4d, 0x01, 0xfb, // add %r15, %r11
4967 0x41, 0xff, 0xe3, // jmpq *%r11
4969 // 41 bytes of nop to pad out to the standard size.
4970 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4971 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4972 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4973 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4974 0x66, 0x66, // excess data32 prefixes
4975 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4980 Output_data_plt_x86_64_nacl
<size
>::do_fill_tlsdesc_entry(
4982 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4983 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4984 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
4985 unsigned int tlsdesc_got_offset
,
4986 unsigned int plt_offset
)
4988 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
4989 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
4991 - (plt_address
+ plt_offset
4993 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
4995 + tlsdesc_got_offset
4996 - (plt_address
+ plt_offset
5000 // The .eh_frame unwind information for the PLT.
5004 Output_data_plt_x86_64_nacl
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
5006 0, 0, 0, 0, // Replaced with offset to .plt.
5007 0, 0, 0, 0, // Replaced with size of .plt.
5008 0, // Augmentation size.
5009 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
5010 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
5011 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
5012 elfcpp::DW_CFA_advance_loc
+ 58, // Advance 58 to __PLT__ + 64.
5013 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
5014 13, // Block length.
5015 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
5016 elfcpp::DW_OP_breg16
, 0, // Push %rip.
5017 elfcpp::DW_OP_const1u
, 63, // Push 0x3f.
5018 elfcpp::DW_OP_and
, // & (%rip & 0x3f).
5019 elfcpp::DW_OP_const1u
, 37, // Push 0x25.
5020 elfcpp::DW_OP_ge
, // >= ((%rip & 0x3f) >= 0x25)
5021 elfcpp::DW_OP_lit3
, // Push 3.
5022 elfcpp::DW_OP_shl
, // << (((%rip & 0x3f) >= 0x25) << 3)
5023 elfcpp::DW_OP_plus
, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5024 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
5028 // Return a string used to fill a code section with nops.
5029 // For NaCl, long NOPs are only valid if they do not cross
5030 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5033 Target_x86_64_nacl
<size
>::do_code_fill(section_size_type length
) const
5035 return std::string(length
, static_cast<char>(0x90));
5038 // The selector for x86_64-nacl object files.
5041 class Target_selector_x86_64_nacl
5042 : public Target_selector_nacl
<Target_selector_x86_64
<size
>,
5043 Target_x86_64_nacl
<size
> >
5046 Target_selector_x86_64_nacl()
5047 : Target_selector_nacl
<Target_selector_x86_64
<size
>,
5048 Target_x86_64_nacl
<size
> >("x86-64",
5050 ? "elf64-x86-64-nacl"
5051 : "elf32-x86-64-nacl",
5054 : "elf32_x86_64_nacl")
5058 Target_selector_x86_64_nacl
<64> target_selector_x86_64
;
5059 Target_selector_x86_64_nacl
<32> target_selector_x32
;
5061 } // End anonymous namespace.