1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2014 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 // Create the GOT section for an incremental update.
598 Output_data_got_base
*
599 init_got_plt_for_update(Symbol_table
* symtab
,
601 unsigned int got_count
,
602 unsigned int plt_count
);
604 // Reserve a GOT entry for a local symbol, and regenerate any
605 // necessary dynamic relocations.
607 reserve_local_got_entry(unsigned int got_index
,
608 Sized_relobj
<size
, false>* obj
,
610 unsigned int got_type
);
612 // Reserve a GOT entry for a global symbol, and regenerate any
613 // necessary dynamic relocations.
615 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
616 unsigned int got_type
);
618 // Register an existing PLT entry for a global symbol.
620 register_global_plt_entry(Symbol_table
*, Layout
*, unsigned int plt_index
,
623 // Force a COPY relocation for a given symbol.
625 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
627 // Apply an incremental relocation.
629 apply_relocation(const Relocate_info
<size
, false>* relinfo
,
630 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
632 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
635 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
636 section_size_type view_size
);
638 // Add a new reloc argument, returning the index in the vector.
640 add_tlsdesc_info(Sized_relobj_file
<size
, false>* object
, unsigned int r_sym
)
642 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
643 return this->tlsdesc_reloc_info_
.size() - 1;
646 Output_data_plt_x86_64
<size
>*
647 make_data_plt(Layout
* layout
,
648 Output_data_got
<64, false>* got
,
649 Output_data_got_plt_x86_64
* got_plt
,
650 Output_data_space
* got_irelative
)
652 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
655 Output_data_plt_x86_64
<size
>*
656 make_data_plt(Layout
* layout
,
657 Output_data_got
<64, false>* got
,
658 Output_data_got_plt_x86_64
* got_plt
,
659 Output_data_space
* got_irelative
,
660 unsigned int plt_count
)
662 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
,
666 virtual Output_data_plt_x86_64
<size
>*
667 do_make_data_plt(Layout
* layout
,
668 Output_data_got
<64, false>* got
,
669 Output_data_got_plt_x86_64
* got_plt
,
670 Output_data_space
* got_irelative
)
672 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
676 virtual Output_data_plt_x86_64
<size
>*
677 do_make_data_plt(Layout
* layout
,
678 Output_data_got
<64, false>* got
,
679 Output_data_got_plt_x86_64
* got_plt
,
680 Output_data_space
* got_irelative
,
681 unsigned int plt_count
)
683 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
689 // The class which scans relocations.
694 : issued_non_pic_error_(false)
698 get_reference_flags(unsigned int r_type
);
701 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
702 Sized_relobj_file
<size
, false>* object
,
703 unsigned int data_shndx
,
704 Output_section
* output_section
,
705 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
706 const elfcpp::Sym
<size
, false>& lsym
,
710 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
711 Sized_relobj_file
<size
, false>* object
,
712 unsigned int data_shndx
,
713 Output_section
* output_section
,
714 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
718 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
719 Target_x86_64
* target
,
720 Sized_relobj_file
<size
, false>* object
,
721 unsigned int data_shndx
,
722 Output_section
* output_section
,
723 const elfcpp::Rela
<size
, false>& reloc
,
725 const elfcpp::Sym
<size
, false>& lsym
);
728 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
729 Target_x86_64
* target
,
730 Sized_relobj_file
<size
, false>* object
,
731 unsigned int data_shndx
,
732 Output_section
* output_section
,
733 const elfcpp::Rela
<size
, false>& reloc
,
739 unsupported_reloc_local(Sized_relobj_file
<size
, false>*,
740 unsigned int r_type
);
743 unsupported_reloc_global(Sized_relobj_file
<size
, false>*,
744 unsigned int r_type
, Symbol
*);
747 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
750 possible_function_pointer_reloc(unsigned int r_type
);
753 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, false>*,
754 unsigned int r_type
);
756 // Whether we have issued an error about a non-PIC compilation.
757 bool issued_non_pic_error_
;
760 // The class which implements relocation.
765 : skip_call_tls_get_addr_(false)
770 if (this->skip_call_tls_get_addr_
)
772 // FIXME: This needs to specify the location somehow.
773 gold_error(_("missing expected TLS relocation"));
777 // Do a relocation. Return false if the caller should not issue
778 // any warnings about this relocation.
780 relocate(const Relocate_info
<size
, false>*, Target_x86_64
*,
782 size_t relnum
, const elfcpp::Rela
<size
, false>&,
783 unsigned int r_type
, const Sized_symbol
<size
>*,
784 const Symbol_value
<size
>*,
785 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
789 // Do a TLS relocation.
791 relocate_tls(const Relocate_info
<size
, false>*, Target_x86_64
*,
792 size_t relnum
, const elfcpp::Rela
<size
, false>&,
793 unsigned int r_type
, const Sized_symbol
<size
>*,
794 const Symbol_value
<size
>*,
795 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
798 // Do a TLS General-Dynamic to Initial-Exec transition.
800 tls_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
801 Output_segment
* tls_segment
,
802 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
803 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
805 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
806 section_size_type view_size
);
808 // Do a TLS General-Dynamic to Local-Exec transition.
810 tls_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
811 Output_segment
* tls_segment
,
812 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
813 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
815 section_size_type view_size
);
817 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
819 tls_desc_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
820 Output_segment
* tls_segment
,
821 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
822 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
824 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
825 section_size_type view_size
);
827 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
829 tls_desc_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
830 Output_segment
* tls_segment
,
831 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
832 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
834 section_size_type view_size
);
836 // Do a TLS Local-Dynamic to Local-Exec transition.
838 tls_ld_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
839 Output_segment
* tls_segment
,
840 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
841 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
843 section_size_type view_size
);
845 // Do a TLS Initial-Exec to Local-Exec transition.
847 tls_ie_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
848 Output_segment
* tls_segment
,
849 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
850 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
852 section_size_type view_size
);
854 // This is set if we should skip the next reloc, which should be a
855 // PLT32 reloc against ___tls_get_addr.
856 bool skip_call_tls_get_addr_
;
859 // A class which returns the size required for a relocation type,
860 // used while scanning relocs during a relocatable link.
861 class Relocatable_size_for_reloc
865 get_size_for_reloc(unsigned int, Relobj
*);
868 // Adjust TLS relocation type based on the options and whether this
869 // is a local symbol.
870 static tls::Tls_optimization
871 optimize_tls_reloc(bool is_final
, int r_type
);
873 // Get the GOT section, creating it if necessary.
874 Output_data_got
<64, false>*
875 got_section(Symbol_table
*, Layout
*);
877 // Get the GOT PLT section.
878 Output_data_got_plt_x86_64
*
879 got_plt_section() const
881 gold_assert(this->got_plt_
!= NULL
);
882 return this->got_plt_
;
885 // Get the GOT section for TLSDESC entries.
886 Output_data_got
<64, false>*
887 got_tlsdesc_section() const
889 gold_assert(this->got_tlsdesc_
!= NULL
);
890 return this->got_tlsdesc_
;
893 // Create the PLT section.
895 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
897 // Create a PLT entry for a global symbol.
899 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
901 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
903 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
904 Sized_relobj_file
<size
, false>* relobj
,
905 unsigned int local_sym_index
);
907 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
909 define_tls_base_symbol(Symbol_table
*, Layout
*);
911 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
913 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
915 // Create a GOT entry for the TLS module index.
917 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
918 Sized_relobj_file
<size
, false>* object
);
920 // Get the PLT section.
921 Output_data_plt_x86_64
<size
>*
924 gold_assert(this->plt_
!= NULL
);
928 // Get the dynamic reloc section, creating it if necessary.
930 rela_dyn_section(Layout
*);
932 // Get the section to use for TLSDESC relocations.
934 rela_tlsdesc_section(Layout
*) const;
936 // Get the section to use for IRELATIVE relocations.
938 rela_irelative_section(Layout
*);
940 // Add a potential copy relocation.
942 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
943 Sized_relobj_file
<size
, false>* object
,
944 unsigned int shndx
, Output_section
* output_section
,
945 Symbol
* sym
, const elfcpp::Rela
<size
, false>& reloc
)
947 this->copy_relocs_
.copy_reloc(symtab
, layout
,
948 symtab
->get_sized_symbol
<size
>(sym
),
949 object
, shndx
, output_section
,
950 reloc
, this->rela_dyn_section(layout
));
953 // Information about this specific target which we pass to the
954 // general Target structure.
955 static const Target::Target_info x86_64_info
;
957 // The types of GOT entries needed for this platform.
958 // These values are exposed to the ABI in an incremental link.
959 // Do not renumber existing values without changing the version
960 // number of the .gnu_incremental_inputs section.
963 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
964 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
965 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
966 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
969 // This type is used as the argument to the target specific
970 // relocation routines. The only target specific reloc is
971 // R_X86_64_TLSDESC against a local symbol.
974 Tlsdesc_info(Sized_relobj_file
<size
, false>* a_object
, unsigned int a_r_sym
)
975 : object(a_object
), r_sym(a_r_sym
)
978 // The object in which the local symbol is defined.
979 Sized_relobj_file
<size
, false>* object
;
980 // The local symbol index in the object.
985 Output_data_got
<64, false>* got_
;
987 Output_data_plt_x86_64
<size
>* plt_
;
988 // The GOT PLT section.
989 Output_data_got_plt_x86_64
* got_plt_
;
990 // The GOT section for IRELATIVE relocations.
991 Output_data_space
* got_irelative_
;
992 // The GOT section for TLSDESC relocations.
993 Output_data_got
<64, false>* got_tlsdesc_
;
994 // The _GLOBAL_OFFSET_TABLE_ symbol.
995 Symbol
* global_offset_table_
;
996 // The dynamic reloc section.
997 Reloc_section
* rela_dyn_
;
998 // The section to use for IRELATIVE relocs.
999 Reloc_section
* rela_irelative_
;
1000 // Relocs saved to avoid a COPY reloc.
1001 Copy_relocs
<elfcpp::SHT_RELA
, size
, false> copy_relocs_
;
1002 // Offset of the GOT entry for the TLS module index.
1003 unsigned int got_mod_index_offset_
;
1004 // We handle R_X86_64_TLSDESC against a local symbol as a target
1005 // specific relocation. Here we store the object and local symbol
1006 // index for the relocation.
1007 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
1008 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1009 bool tls_base_symbol_defined_
;
1013 const Target::Target_info Target_x86_64
<64>::x86_64_info
=
1016 false, // is_big_endian
1017 elfcpp::EM_X86_64
, // machine_code
1018 false, // has_make_symbol
1019 false, // has_resolve
1020 true, // has_code_fill
1021 true, // is_default_stack_executable
1022 true, // can_icf_inline_merge_sections
1024 "/libexec/ld-elf.so.2", // program interpreter
1025 0x400000, // default_text_segment_address
1026 0x1000, // abi_pagesize (overridable by -z max-page-size)
1027 0x1000, // common_pagesize (overridable by -z common-page-size)
1028 false, // isolate_execinstr
1030 elfcpp::SHN_UNDEF
, // small_common_shndx
1031 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1032 0, // small_common_section_flags
1033 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1034 NULL
, // attributes_section
1035 NULL
, // attributes_vendor
1036 "_start" // entry_symbol_name
1040 const Target::Target_info Target_x86_64
<32>::x86_64_info
=
1043 false, // is_big_endian
1044 elfcpp::EM_X86_64
, // machine_code
1045 false, // has_make_symbol
1046 false, // has_resolve
1047 true, // has_code_fill
1048 true, // is_default_stack_executable
1049 true, // can_icf_inline_merge_sections
1051 "/libx32/ldx32.so.1", // program interpreter
1052 0x400000, // default_text_segment_address
1053 0x1000, // abi_pagesize (overridable by -z max-page-size)
1054 0x1000, // common_pagesize (overridable by -z common-page-size)
1055 false, // isolate_execinstr
1057 elfcpp::SHN_UNDEF
, // small_common_shndx
1058 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1059 0, // small_common_section_flags
1060 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1061 NULL
, // attributes_section
1062 NULL
, // attributes_vendor
1063 "_start" // entry_symbol_name
1066 // This is called when a new output section is created. This is where
1067 // we handle the SHF_X86_64_LARGE.
1071 Target_x86_64
<size
>::do_new_output_section(Output_section
* os
) const
1073 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
1074 os
->set_is_large_section();
1077 // Get the GOT section, creating it if necessary.
1080 Output_data_got
<64, false>*
1081 Target_x86_64
<size
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
1083 if (this->got_
== NULL
)
1085 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1087 // When using -z now, we can treat .got.plt as a relro section.
1088 // Without -z now, it is modified after program startup by lazy
1090 bool is_got_plt_relro
= parameters
->options().now();
1091 Output_section_order got_order
= (is_got_plt_relro
1093 : ORDER_RELRO_LAST
);
1094 Output_section_order got_plt_order
= (is_got_plt_relro
1096 : ORDER_NON_RELRO_FIRST
);
1098 this->got_
= new Output_data_got
<64, false>();
1100 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1102 | elfcpp::SHF_WRITE
),
1103 this->got_
, got_order
, true);
1105 this->got_plt_
= new Output_data_got_plt_x86_64(layout
);
1106 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1108 | elfcpp::SHF_WRITE
),
1109 this->got_plt_
, got_plt_order
,
1112 // The first three entries are reserved.
1113 this->got_plt_
->set_current_data_size(3 * 8);
1115 if (!is_got_plt_relro
)
1117 // Those bytes can go into the relro segment.
1118 layout
->increase_relro(3 * 8);
1121 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1122 this->global_offset_table_
=
1123 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1124 Symbol_table::PREDEFINED
,
1126 0, 0, elfcpp::STT_OBJECT
,
1128 elfcpp::STV_HIDDEN
, 0,
1131 // If there are any IRELATIVE relocations, they get GOT entries
1132 // in .got.plt after the jump slot entries.
1133 this->got_irelative_
= new Output_data_space(8, "** GOT IRELATIVE PLT");
1134 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1136 | elfcpp::SHF_WRITE
),
1137 this->got_irelative_
,
1138 got_plt_order
, is_got_plt_relro
);
1140 // If there are any TLSDESC relocations, they get GOT entries in
1141 // .got.plt after the jump slot and IRELATIVE entries.
1142 this->got_tlsdesc_
= new Output_data_got
<64, false>();
1143 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1145 | elfcpp::SHF_WRITE
),
1147 got_plt_order
, is_got_plt_relro
);
1153 // Get the dynamic reloc section, creating it if necessary.
1156 typename Target_x86_64
<size
>::Reloc_section
*
1157 Target_x86_64
<size
>::rela_dyn_section(Layout
* layout
)
1159 if (this->rela_dyn_
== NULL
)
1161 gold_assert(layout
!= NULL
);
1162 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1163 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1164 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1165 ORDER_DYNAMIC_RELOCS
, false);
1167 return this->rela_dyn_
;
1170 // Get the section to use for IRELATIVE relocs, creating it if
1171 // necessary. These go in .rela.dyn, but only after all other dynamic
1172 // relocations. They need to follow the other dynamic relocations so
1173 // that they can refer to global variables initialized by those
1177 typename Target_x86_64
<size
>::Reloc_section
*
1178 Target_x86_64
<size
>::rela_irelative_section(Layout
* layout
)
1180 if (this->rela_irelative_
== NULL
)
1182 // Make sure we have already created the dynamic reloc section.
1183 this->rela_dyn_section(layout
);
1184 this->rela_irelative_
= new Reloc_section(false);
1185 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1186 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
1187 ORDER_DYNAMIC_RELOCS
, false);
1188 gold_assert(this->rela_dyn_
->output_section()
1189 == this->rela_irelative_
->output_section());
1191 return this->rela_irelative_
;
1194 // Write the first three reserved words of the .got.plt section.
1195 // The remainder of the section is written while writing the PLT
1196 // in Output_data_plt_i386::do_write.
1199 Output_data_got_plt_x86_64::do_write(Output_file
* of
)
1201 // The first entry in the GOT is the address of the .dynamic section
1202 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1203 // We saved space for them when we created the section in
1204 // Target_x86_64::got_section.
1205 const off_t got_file_offset
= this->offset();
1206 gold_assert(this->data_size() >= 24);
1207 unsigned char* const got_view
= of
->get_output_view(got_file_offset
, 24);
1208 Output_section
* dynamic
= this->layout_
->dynamic_section();
1209 uint64_t dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
1210 elfcpp::Swap
<64, false>::writeval(got_view
, dynamic_addr
);
1211 memset(got_view
+ 8, 0, 16);
1212 of
->write_output_view(got_file_offset
, 24, got_view
);
1215 // Initialize the PLT section.
1219 Output_data_plt_x86_64
<size
>::init(Layout
* layout
)
1221 this->rel_
= new Reloc_section(false);
1222 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1223 elfcpp::SHF_ALLOC
, this->rel_
,
1224 ORDER_DYNAMIC_PLT_RELOCS
, false);
1229 Output_data_plt_x86_64
<size
>::do_adjust_output_section(Output_section
* os
)
1231 os
->set_entsize(this->get_plt_entry_size());
1234 // Add an entry to the PLT.
1238 Output_data_plt_x86_64
<size
>::add_entry(Symbol_table
* symtab
, Layout
* layout
,
1241 gold_assert(!gsym
->has_plt_offset());
1243 unsigned int plt_index
;
1245 section_offset_type got_offset
;
1247 unsigned int* pcount
;
1248 unsigned int offset
;
1249 unsigned int reserved
;
1250 Output_section_data_build
* got
;
1251 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1252 && gsym
->can_use_relative_reloc(false))
1254 pcount
= &this->irelative_count_
;
1257 got
= this->got_irelative_
;
1261 pcount
= &this->count_
;
1264 got
= this->got_plt_
;
1267 if (!this->is_data_size_valid())
1269 // Note that when setting the PLT offset for a non-IRELATIVE
1270 // entry we skip the initial reserved PLT entry.
1271 plt_index
= *pcount
+ offset
;
1272 plt_offset
= plt_index
* this->get_plt_entry_size();
1276 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1277 gold_assert(got_offset
== got
->current_data_size());
1279 // Every PLT entry needs a GOT entry which points back to the PLT
1280 // entry (this will be changed by the dynamic linker, normally
1281 // lazily when the function is called).
1282 got
->set_current_data_size(got_offset
+ 8);
1286 // FIXME: This is probably not correct for IRELATIVE relocs.
1288 // For incremental updates, find an available slot.
1289 plt_offset
= this->free_list_
.allocate(this->get_plt_entry_size(),
1290 this->get_plt_entry_size(), 0);
1291 if (plt_offset
== -1)
1292 gold_fallback(_("out of patch space (PLT);"
1293 " relink with --incremental-full"));
1295 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1296 // can be calculated from the PLT index, adjusting for the three
1297 // reserved entries at the beginning of the GOT.
1298 plt_index
= plt_offset
/ this->get_plt_entry_size() - 1;
1299 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1302 gsym
->set_plt_offset(plt_offset
);
1304 // Every PLT entry needs a reloc.
1305 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
1307 // Note that we don't need to save the symbol. The contents of the
1308 // PLT are independent of which symbols are used. The symbols only
1309 // appear in the relocations.
1312 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1317 Output_data_plt_x86_64
<size
>::add_local_ifunc_entry(
1318 Symbol_table
* symtab
,
1320 Sized_relobj_file
<size
, false>* relobj
,
1321 unsigned int local_sym_index
)
1323 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
1324 ++this->irelative_count_
;
1326 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
1328 // Every PLT entry needs a GOT entry which points back to the PLT
1330 this->got_irelative_
->set_current_data_size(got_offset
+ 8);
1332 // Every PLT entry needs a reloc.
1333 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1334 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
1335 elfcpp::R_X86_64_IRELATIVE
,
1336 this->got_irelative_
, got_offset
, 0);
1341 // Add the relocation for a PLT entry.
1345 Output_data_plt_x86_64
<size
>::add_relocation(Symbol_table
* symtab
,
1348 unsigned int got_offset
)
1350 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1351 && gsym
->can_use_relative_reloc(false))
1353 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1354 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
1355 this->got_irelative_
, got_offset
, 0);
1359 gsym
->set_needs_dynsym_entry();
1360 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
1365 // Return where the TLSDESC relocations should go, creating it if
1366 // necessary. These follow the JUMP_SLOT relocations.
1369 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1370 Output_data_plt_x86_64
<size
>::rela_tlsdesc(Layout
* layout
)
1372 if (this->tlsdesc_rel_
== NULL
)
1374 this->tlsdesc_rel_
= new Reloc_section(false);
1375 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1376 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
1377 ORDER_DYNAMIC_PLT_RELOCS
, false);
1378 gold_assert(this->tlsdesc_rel_
->output_section()
1379 == this->rel_
->output_section());
1381 return this->tlsdesc_rel_
;
1384 // Return where the IRELATIVE relocations should go in the PLT. These
1385 // follow the JUMP_SLOT and the TLSDESC relocations.
1388 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1389 Output_data_plt_x86_64
<size
>::rela_irelative(Symbol_table
* symtab
,
1392 if (this->irelative_rel_
== NULL
)
1394 // Make sure we have a place for the TLSDESC relocations, in
1395 // case we see any later on.
1396 this->rela_tlsdesc(layout
);
1397 this->irelative_rel_
= new Reloc_section(false);
1398 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1399 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
1400 ORDER_DYNAMIC_PLT_RELOCS
, false);
1401 gold_assert(this->irelative_rel_
->output_section()
1402 == this->rel_
->output_section());
1404 if (parameters
->doing_static_link())
1406 // A statically linked executable will only have a .rela.plt
1407 // section to hold R_X86_64_IRELATIVE relocs for
1408 // STT_GNU_IFUNC symbols. The library will use these
1409 // symbols to locate the IRELATIVE relocs at program startup
1411 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
1412 Symbol_table::PREDEFINED
,
1413 this->irelative_rel_
, 0, 0,
1414 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1415 elfcpp::STV_HIDDEN
, 0, false, true);
1416 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
1417 Symbol_table::PREDEFINED
,
1418 this->irelative_rel_
, 0, 0,
1419 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1420 elfcpp::STV_HIDDEN
, 0, true, true);
1423 return this->irelative_rel_
;
1426 // Return the PLT address to use for a global symbol.
1430 Output_data_plt_x86_64
<size
>::address_for_global(const Symbol
* gsym
)
1432 uint64_t offset
= 0;
1433 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1434 && gsym
->can_use_relative_reloc(false))
1435 offset
= (this->count_
+ 1) * this->get_plt_entry_size();
1436 return this->address() + offset
+ gsym
->plt_offset();
1439 // Return the PLT address to use for a local symbol. These are always
1440 // IRELATIVE relocs.
1444 Output_data_plt_x86_64
<size
>::address_for_local(const Relobj
* object
,
1447 return (this->address()
1448 + (this->count_
+ 1) * this->get_plt_entry_size()
1449 + object
->local_plt_offset(r_sym
));
1452 // Set the final size.
1455 Output_data_plt_x86_64
<size
>::set_final_data_size()
1457 unsigned int count
= this->count_
+ this->irelative_count_
;
1458 if (this->has_tlsdesc_entry())
1460 this->set_data_size((count
+ 1) * this->get_plt_entry_size());
1463 // The first entry in the PLT for an executable.
1467 Output_data_plt_x86_64_standard
<size
>::first_plt_entry
[plt_entry_size
] =
1469 // From AMD64 ABI Draft 0.98, page 76
1470 0xff, 0x35, // pushq contents of memory address
1471 0, 0, 0, 0, // replaced with address of .got + 8
1472 0xff, 0x25, // jmp indirect
1473 0, 0, 0, 0, // replaced with address of .got + 16
1474 0x90, 0x90, 0x90, 0x90 // noop (x4)
1479 Output_data_plt_x86_64_standard
<size
>::do_fill_first_plt_entry(
1481 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1482 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
1484 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1485 // We do a jmp relative to the PC at the end of this instruction.
1486 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1488 - (plt_address
+ 6)));
1489 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1491 - (plt_address
+ 12)));
1494 // Subsequent entries in the PLT for an executable.
1498 Output_data_plt_x86_64_standard
<size
>::plt_entry
[plt_entry_size
] =
1500 // From AMD64 ABI Draft 0.98, page 76
1501 0xff, 0x25, // jmpq indirect
1502 0, 0, 0, 0, // replaced with address of symbol in .got
1503 0x68, // pushq immediate
1504 0, 0, 0, 0, // replaced with offset into relocation table
1505 0xe9, // jmpq relative
1506 0, 0, 0, 0 // replaced with offset to start of .plt
1511 Output_data_plt_x86_64_standard
<size
>::do_fill_plt_entry(
1513 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1514 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1515 unsigned int got_offset
,
1516 unsigned int plt_offset
,
1517 unsigned int plt_index
)
1519 memcpy(pov
, plt_entry
, plt_entry_size
);
1520 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1521 (got_address
+ got_offset
1522 - (plt_address
+ plt_offset
1525 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1526 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1527 - (plt_offset
+ plt_entry_size
));
1532 // The reserved TLSDESC entry in the PLT for an executable.
1536 Output_data_plt_x86_64_standard
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
1538 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1539 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1540 0xff, 0x35, // pushq x(%rip)
1541 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1542 0xff, 0x25, // jmpq *y(%rip)
1543 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1550 Output_data_plt_x86_64_standard
<size
>::do_fill_tlsdesc_entry(
1552 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1553 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1554 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
1555 unsigned int tlsdesc_got_offset
,
1556 unsigned int plt_offset
)
1558 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1559 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1561 - (plt_address
+ plt_offset
1563 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1565 + tlsdesc_got_offset
1566 - (plt_address
+ plt_offset
1570 // The .eh_frame unwind information for the PLT.
1574 Output_data_plt_x86_64
<size
>::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
1577 'z', // Augmentation: augmentation size included.
1578 'R', // Augmentation: FDE encoding included.
1579 '\0', // End of augmentation string.
1580 1, // Code alignment factor.
1581 0x78, // Data alignment factor.
1582 16, // Return address column.
1583 1, // Augmentation size.
1584 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
1585 | elfcpp::DW_EH_PE_sdata4
),
1586 elfcpp::DW_CFA_def_cfa
, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1587 elfcpp::DW_CFA_offset
+ 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1588 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
1594 Output_data_plt_x86_64_standard
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
1596 0, 0, 0, 0, // Replaced with offset to .plt.
1597 0, 0, 0, 0, // Replaced with size of .plt.
1598 0, // Augmentation size.
1599 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
1600 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
1601 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
1602 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
1603 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
1604 11, // Block length.
1605 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
1606 elfcpp::DW_OP_breg16
, 0, // Push %rip.
1607 elfcpp::DW_OP_lit15
, // Push 0xf.
1608 elfcpp::DW_OP_and
, // & (%rip & 0xf).
1609 elfcpp::DW_OP_lit11
, // Push 0xb.
1610 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 0xb)
1611 elfcpp::DW_OP_lit3
, // Push 3.
1612 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 0xb) << 3)
1613 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1614 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
1620 // Write out the PLT. This uses the hand-coded instructions above,
1621 // and adjusts them as needed. This is specified by the AMD64 ABI.
1625 Output_data_plt_x86_64
<size
>::do_write(Output_file
* of
)
1627 const off_t offset
= this->offset();
1628 const section_size_type oview_size
=
1629 convert_to_section_size_type(this->data_size());
1630 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1632 const off_t got_file_offset
= this->got_plt_
->offset();
1633 gold_assert(parameters
->incremental_update()
1634 || (got_file_offset
+ this->got_plt_
->data_size()
1635 == this->got_irelative_
->offset()));
1636 const section_size_type got_size
=
1637 convert_to_section_size_type(this->got_plt_
->data_size()
1638 + this->got_irelative_
->data_size());
1639 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1642 unsigned char* pov
= oview
;
1644 // The base address of the .plt section.
1645 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
1646 // The base address of the .got section.
1647 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
= this->got_
->address();
1648 // The base address of the PLT portion of the .got section,
1649 // which is where the GOT pointer will point, and where the
1650 // three reserved GOT entries are located.
1651 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
1652 = this->got_plt_
->address();
1654 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
1655 pov
+= this->get_plt_entry_size();
1657 // The first three entries in the GOT are reserved, and are written
1658 // by Output_data_got_plt_x86_64::do_write.
1659 unsigned char* got_pov
= got_view
+ 24;
1661 unsigned int plt_offset
= this->get_plt_entry_size();
1662 unsigned int got_offset
= 24;
1663 const unsigned int count
= this->count_
+ this->irelative_count_
;
1664 for (unsigned int plt_index
= 0;
1667 pov
+= this->get_plt_entry_size(),
1669 plt_offset
+= this->get_plt_entry_size(),
1672 // Set and adjust the PLT entry itself.
1673 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
1674 got_address
, plt_address
,
1675 got_offset
, plt_offset
,
1678 // Set the entry in the GOT.
1679 elfcpp::Swap
<64, false>::writeval(got_pov
,
1680 plt_address
+ plt_offset
+ lazy_offset
);
1683 if (this->has_tlsdesc_entry())
1685 // Set and adjust the reserved TLSDESC PLT entry.
1686 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
1687 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
1688 tlsdesc_got_offset
, plt_offset
);
1689 pov
+= this->get_plt_entry_size();
1692 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1693 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1695 of
->write_output_view(offset
, oview_size
, oview
);
1696 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1699 // Create the PLT section.
1703 Target_x86_64
<size
>::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1705 if (this->plt_
== NULL
)
1707 // Create the GOT sections first.
1708 this->got_section(symtab
, layout
);
1710 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
1711 this->got_irelative_
);
1713 // Add unwind information if requested.
1714 if (parameters
->options().ld_generated_unwind_info())
1715 this->plt_
->add_eh_frame(layout
);
1717 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1719 | elfcpp::SHF_EXECINSTR
),
1720 this->plt_
, ORDER_PLT
, false);
1722 // Make the sh_info field of .rela.plt point to .plt.
1723 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1724 rela_plt_os
->set_info_section(this->plt_
->output_section());
1728 // Return the section for TLSDESC relocations.
1731 typename Target_x86_64
<size
>::Reloc_section
*
1732 Target_x86_64
<size
>::rela_tlsdesc_section(Layout
* layout
) const
1734 return this->plt_section()->rela_tlsdesc(layout
);
1737 // Create a PLT entry for a global symbol.
1741 Target_x86_64
<size
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1744 if (gsym
->has_plt_offset())
1747 if (this->plt_
== NULL
)
1748 this->make_plt_section(symtab
, layout
);
1750 this->plt_
->add_entry(symtab
, layout
, gsym
);
1753 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1757 Target_x86_64
<size
>::make_local_ifunc_plt_entry(
1758 Symbol_table
* symtab
, Layout
* layout
,
1759 Sized_relobj_file
<size
, false>* relobj
,
1760 unsigned int local_sym_index
)
1762 if (relobj
->local_has_plt_offset(local_sym_index
))
1764 if (this->plt_
== NULL
)
1765 this->make_plt_section(symtab
, layout
);
1766 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
1769 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1772 // Return the number of entries in the PLT.
1776 Target_x86_64
<size
>::plt_entry_count() const
1778 if (this->plt_
== NULL
)
1780 return this->plt_
->entry_count();
1783 // Return the offset of the first non-reserved PLT entry.
1787 Target_x86_64
<size
>::first_plt_entry_offset() const
1789 return this->plt_
->first_plt_entry_offset();
1792 // Return the size of each PLT entry.
1796 Target_x86_64
<size
>::plt_entry_size() const
1798 return this->plt_
->get_plt_entry_size();
1801 // Create the GOT and PLT sections for an incremental update.
1804 Output_data_got_base
*
1805 Target_x86_64
<size
>::init_got_plt_for_update(Symbol_table
* symtab
,
1807 unsigned int got_count
,
1808 unsigned int plt_count
)
1810 gold_assert(this->got_
== NULL
);
1812 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
1813 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1815 | elfcpp::SHF_WRITE
),
1816 this->got_
, ORDER_RELRO_LAST
,
1819 // Add the three reserved entries.
1820 this->got_plt_
= new Output_data_got_plt_x86_64(layout
, (plt_count
+ 3) * 8);
1821 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1823 | elfcpp::SHF_WRITE
),
1824 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
1827 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1828 this->global_offset_table_
=
1829 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1830 Symbol_table::PREDEFINED
,
1832 0, 0, elfcpp::STT_OBJECT
,
1834 elfcpp::STV_HIDDEN
, 0,
1837 // If there are any TLSDESC relocations, they get GOT entries in
1838 // .got.plt after the jump slot entries.
1839 // FIXME: Get the count for TLSDESC entries.
1840 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
1841 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1842 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1844 ORDER_NON_RELRO_FIRST
, false);
1846 // If there are any IRELATIVE relocations, they get GOT entries in
1847 // .got.plt after the jump slot and TLSDESC entries.
1848 this->got_irelative_
= new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1849 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1850 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1851 this->got_irelative_
,
1852 ORDER_NON_RELRO_FIRST
, false);
1854 // Create the PLT section.
1855 this->plt_
= this->make_data_plt(layout
, this->got_
,
1857 this->got_irelative_
,
1860 // Add unwind information if requested.
1861 if (parameters
->options().ld_generated_unwind_info())
1862 this->plt_
->add_eh_frame(layout
);
1864 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1865 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1866 this->plt_
, ORDER_PLT
, false);
1868 // Make the sh_info field of .rela.plt point to .plt.
1869 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1870 rela_plt_os
->set_info_section(this->plt_
->output_section());
1872 // Create the rela_dyn section.
1873 this->rela_dyn_section(layout
);
1878 // Reserve a GOT entry for a local symbol, and regenerate any
1879 // necessary dynamic relocations.
1883 Target_x86_64
<size
>::reserve_local_got_entry(
1884 unsigned int got_index
,
1885 Sized_relobj
<size
, false>* obj
,
1887 unsigned int got_type
)
1889 unsigned int got_offset
= got_index
* 8;
1890 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1892 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
1895 case GOT_TYPE_STANDARD
:
1896 if (parameters
->options().output_is_position_independent())
1897 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
1898 this->got_
, got_offset
, 0, false);
1900 case GOT_TYPE_TLS_OFFSET
:
1901 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
1902 this->got_
, got_offset
, 0);
1904 case GOT_TYPE_TLS_PAIR
:
1905 this->got_
->reserve_slot(got_index
+ 1);
1906 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
1907 this->got_
, got_offset
, 0);
1909 case GOT_TYPE_TLS_DESC
:
1910 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1911 // this->got_->reserve_slot(got_index + 1);
1912 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1913 // this->got_, got_offset, 0);
1920 // Reserve a GOT entry for a global symbol, and regenerate any
1921 // necessary dynamic relocations.
1925 Target_x86_64
<size
>::reserve_global_got_entry(unsigned int got_index
,
1927 unsigned int got_type
)
1929 unsigned int got_offset
= got_index
* 8;
1930 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1932 this->got_
->reserve_global(got_index
, gsym
, got_type
);
1935 case GOT_TYPE_STANDARD
:
1936 if (!gsym
->final_value_is_known())
1938 if (gsym
->is_from_dynobj()
1939 || gsym
->is_undefined()
1940 || gsym
->is_preemptible()
1941 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1942 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
1943 this->got_
, got_offset
, 0);
1945 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1946 this->got_
, got_offset
, 0, false);
1949 case GOT_TYPE_TLS_OFFSET
:
1950 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
1951 this->got_
, got_offset
, 0, false);
1953 case GOT_TYPE_TLS_PAIR
:
1954 this->got_
->reserve_slot(got_index
+ 1);
1955 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
1956 this->got_
, got_offset
, 0, false);
1957 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
1958 this->got_
, got_offset
+ 8, 0, false);
1960 case GOT_TYPE_TLS_DESC
:
1961 this->got_
->reserve_slot(got_index
+ 1);
1962 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
1963 this->got_
, got_offset
, 0, false);
1970 // Register an existing PLT entry for a global symbol.
1974 Target_x86_64
<size
>::register_global_plt_entry(Symbol_table
* symtab
,
1976 unsigned int plt_index
,
1979 gold_assert(this->plt_
!= NULL
);
1980 gold_assert(!gsym
->has_plt_offset());
1982 this->plt_
->reserve_slot(plt_index
);
1984 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
1986 unsigned int got_offset
= (plt_index
+ 3) * 8;
1987 this->plt_
->add_relocation(symtab
, layout
, gsym
, got_offset
);
1990 // Force a COPY relocation for a given symbol.
1994 Target_x86_64
<size
>::emit_copy_reloc(
1995 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
1997 this->copy_relocs_
.emit_copy_reloc(symtab
,
1998 symtab
->get_sized_symbol
<size
>(sym
),
2001 this->rela_dyn_section(NULL
));
2004 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
2008 Target_x86_64
<size
>::define_tls_base_symbol(Symbol_table
* symtab
,
2011 if (this->tls_base_symbol_defined_
)
2014 Output_segment
* tls_segment
= layout
->tls_segment();
2015 if (tls_segment
!= NULL
)
2017 bool is_exec
= parameters
->options().output_is_executable();
2018 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
2019 Symbol_table::PREDEFINED
,
2023 elfcpp::STV_HIDDEN
, 0,
2025 ? Symbol::SEGMENT_END
2026 : Symbol::SEGMENT_START
),
2029 this->tls_base_symbol_defined_
= true;
2032 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
2036 Target_x86_64
<size
>::reserve_tlsdesc_entries(Symbol_table
* symtab
,
2039 if (this->plt_
== NULL
)
2040 this->make_plt_section(symtab
, layout
);
2042 if (!this->plt_
->has_tlsdesc_entry())
2044 // Allocate the TLSDESC_GOT entry.
2045 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2046 unsigned int got_offset
= got
->add_constant(0);
2048 // Allocate the TLSDESC_PLT entry.
2049 this->plt_
->reserve_tlsdesc_entry(got_offset
);
2053 // Create a GOT entry for the TLS module index.
2057 Target_x86_64
<size
>::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
2058 Sized_relobj_file
<size
, false>* object
)
2060 if (this->got_mod_index_offset_
== -1U)
2062 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
2063 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
2064 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2065 unsigned int got_offset
= got
->add_constant(0);
2066 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
2068 got
->add_constant(0);
2069 this->got_mod_index_offset_
= got_offset
;
2071 return this->got_mod_index_offset_
;
2074 // Optimize the TLS relocation type based on what we know about the
2075 // symbol. IS_FINAL is true if the final address of this symbol is
2076 // known at link time.
2079 tls::Tls_optimization
2080 Target_x86_64
<size
>::optimize_tls_reloc(bool is_final
, int r_type
)
2082 // If we are generating a shared library, then we can't do anything
2084 if (parameters
->options().shared())
2085 return tls::TLSOPT_NONE
;
2089 case elfcpp::R_X86_64_TLSGD
:
2090 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2091 case elfcpp::R_X86_64_TLSDESC_CALL
:
2092 // These are General-Dynamic which permits fully general TLS
2093 // access. Since we know that we are generating an executable,
2094 // we can convert this to Initial-Exec. If we also know that
2095 // this is a local symbol, we can further switch to Local-Exec.
2097 return tls::TLSOPT_TO_LE
;
2098 return tls::TLSOPT_TO_IE
;
2100 case elfcpp::R_X86_64_TLSLD
:
2101 // This is Local-Dynamic, which refers to a local symbol in the
2102 // dynamic TLS block. Since we know that we generating an
2103 // executable, we can switch to Local-Exec.
2104 return tls::TLSOPT_TO_LE
;
2106 case elfcpp::R_X86_64_DTPOFF32
:
2107 case elfcpp::R_X86_64_DTPOFF64
:
2108 // Another Local-Dynamic reloc.
2109 return tls::TLSOPT_TO_LE
;
2111 case elfcpp::R_X86_64_GOTTPOFF
:
2112 // These are Initial-Exec relocs which get the thread offset
2113 // from the GOT. If we know that we are linking against the
2114 // local symbol, we can switch to Local-Exec, which links the
2115 // thread offset into the instruction.
2117 return tls::TLSOPT_TO_LE
;
2118 return tls::TLSOPT_NONE
;
2120 case elfcpp::R_X86_64_TPOFF32
:
2121 // When we already have Local-Exec, there is nothing further we
2123 return tls::TLSOPT_NONE
;
2130 // Get the Reference_flags for a particular relocation.
2134 Target_x86_64
<size
>::Scan::get_reference_flags(unsigned int r_type
)
2138 case elfcpp::R_X86_64_NONE
:
2139 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2140 case elfcpp::R_X86_64_GNU_VTENTRY
:
2141 case elfcpp::R_X86_64_GOTPC32
:
2142 case elfcpp::R_X86_64_GOTPC64
:
2143 // No symbol reference.
2146 case elfcpp::R_X86_64_64
:
2147 case elfcpp::R_X86_64_32
:
2148 case elfcpp::R_X86_64_32S
:
2149 case elfcpp::R_X86_64_16
:
2150 case elfcpp::R_X86_64_8
:
2151 return Symbol::ABSOLUTE_REF
;
2153 case elfcpp::R_X86_64_PC64
:
2154 case elfcpp::R_X86_64_PC32
:
2155 case elfcpp::R_X86_64_PC32_BND
:
2156 case elfcpp::R_X86_64_PC16
:
2157 case elfcpp::R_X86_64_PC8
:
2158 case elfcpp::R_X86_64_GOTOFF64
:
2159 return Symbol::RELATIVE_REF
;
2161 case elfcpp::R_X86_64_PLT32
:
2162 case elfcpp::R_X86_64_PLT32_BND
:
2163 case elfcpp::R_X86_64_PLTOFF64
:
2164 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
2166 case elfcpp::R_X86_64_GOT64
:
2167 case elfcpp::R_X86_64_GOT32
:
2168 case elfcpp::R_X86_64_GOTPCREL64
:
2169 case elfcpp::R_X86_64_GOTPCREL
:
2170 case elfcpp::R_X86_64_GOTPLT64
:
2172 return Symbol::ABSOLUTE_REF
;
2174 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2175 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2176 case elfcpp::R_X86_64_TLSDESC_CALL
:
2177 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2178 case elfcpp::R_X86_64_DTPOFF32
:
2179 case elfcpp::R_X86_64_DTPOFF64
:
2180 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2181 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2182 return Symbol::TLS_REF
;
2184 case elfcpp::R_X86_64_COPY
:
2185 case elfcpp::R_X86_64_GLOB_DAT
:
2186 case elfcpp::R_X86_64_JUMP_SLOT
:
2187 case elfcpp::R_X86_64_RELATIVE
:
2188 case elfcpp::R_X86_64_IRELATIVE
:
2189 case elfcpp::R_X86_64_TPOFF64
:
2190 case elfcpp::R_X86_64_DTPMOD64
:
2191 case elfcpp::R_X86_64_TLSDESC
:
2192 case elfcpp::R_X86_64_SIZE32
:
2193 case elfcpp::R_X86_64_SIZE64
:
2195 // Not expected. We will give an error later.
2200 // Report an unsupported relocation against a local symbol.
2204 Target_x86_64
<size
>::Scan::unsupported_reloc_local(
2205 Sized_relobj_file
<size
, false>* object
,
2206 unsigned int r_type
)
2208 gold_error(_("%s: unsupported reloc %u against local symbol"),
2209 object
->name().c_str(), r_type
);
2212 // We are about to emit a dynamic relocation of type R_TYPE. If the
2213 // dynamic linker does not support it, issue an error. The GNU linker
2214 // only issues a non-PIC error for an allocated read-only section.
2215 // Here we know the section is allocated, but we don't know that it is
2216 // read-only. But we check for all the relocation types which the
2217 // glibc dynamic linker supports, so it seems appropriate to issue an
2218 // error even if the section is not read-only. If GSYM is not NULL,
2219 // it is the symbol the relocation is against; if it is NULL, the
2220 // relocation is against a local symbol.
2224 Target_x86_64
<size
>::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
2229 // These are the relocation types supported by glibc for x86_64
2230 // which should always work.
2231 case elfcpp::R_X86_64_RELATIVE
:
2232 case elfcpp::R_X86_64_IRELATIVE
:
2233 case elfcpp::R_X86_64_GLOB_DAT
:
2234 case elfcpp::R_X86_64_JUMP_SLOT
:
2235 case elfcpp::R_X86_64_DTPMOD64
:
2236 case elfcpp::R_X86_64_DTPOFF64
:
2237 case elfcpp::R_X86_64_TPOFF64
:
2238 case elfcpp::R_X86_64_64
:
2239 case elfcpp::R_X86_64_COPY
:
2242 // glibc supports these reloc types, but they can overflow.
2243 case elfcpp::R_X86_64_PC32
:
2244 case elfcpp::R_X86_64_PC32_BND
:
2245 // A PC relative reference is OK against a local symbol or if
2246 // the symbol is defined locally.
2248 || (!gsym
->is_from_dynobj()
2249 && !gsym
->is_undefined()
2250 && !gsym
->is_preemptible()))
2253 case elfcpp::R_X86_64_32
:
2254 // R_X86_64_32 is OK for x32.
2255 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2257 if (this->issued_non_pic_error_
)
2259 gold_assert(parameters
->options().output_is_position_independent());
2261 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
2262 "overflow at runtime; recompile with -fPIC"));
2268 case elfcpp::R_X86_64_32
:
2269 r_name
= "R_X86_64_32";
2271 case elfcpp::R_X86_64_PC32
:
2272 r_name
= "R_X86_64_PC32";
2274 case elfcpp::R_X86_64_PC32_BND
:
2275 r_name
= "R_X86_64_PC32_BND";
2281 object
->error(_("requires dynamic %s reloc against '%s' "
2282 "which may overflow at runtime; recompile "
2284 r_name
, gsym
->name());
2286 this->issued_non_pic_error_
= true;
2290 // This prevents us from issuing more than one error per reloc
2291 // section. But we can still wind up issuing more than one
2292 // error per object file.
2293 if (this->issued_non_pic_error_
)
2295 gold_assert(parameters
->options().output_is_position_independent());
2296 object
->error(_("requires unsupported dynamic reloc %u; "
2297 "recompile with -fPIC"),
2299 this->issued_non_pic_error_
= true;
2302 case elfcpp::R_X86_64_NONE
:
2307 // Return whether we need to make a PLT entry for a relocation of the
2308 // given type against a STT_GNU_IFUNC symbol.
2312 Target_x86_64
<size
>::Scan::reloc_needs_plt_for_ifunc(
2313 Sized_relobj_file
<size
, false>* object
,
2314 unsigned int r_type
)
2316 int flags
= Scan::get_reference_flags(r_type
);
2317 if (flags
& Symbol::TLS_REF
)
2318 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2319 object
->name().c_str(), r_type
);
2323 // Scan a relocation for a local symbol.
2327 Target_x86_64
<size
>::Scan::local(Symbol_table
* symtab
,
2329 Target_x86_64
<size
>* target
,
2330 Sized_relobj_file
<size
, false>* object
,
2331 unsigned int data_shndx
,
2332 Output_section
* output_section
,
2333 const elfcpp::Rela
<size
, false>& reloc
,
2334 unsigned int r_type
,
2335 const elfcpp::Sym
<size
, false>& lsym
,
2341 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2342 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
2343 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
2345 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2346 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
2351 case elfcpp::R_X86_64_NONE
:
2352 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2353 case elfcpp::R_X86_64_GNU_VTENTRY
:
2356 case elfcpp::R_X86_64_64
:
2357 // If building a shared library (or a position-independent
2358 // executable), we need to create a dynamic relocation for this
2359 // location. The relocation applied at link time will apply the
2360 // link-time value, so we flag the location with an
2361 // R_X86_64_RELATIVE relocation so the dynamic loader can
2362 // relocate it easily.
2363 if (parameters
->options().output_is_position_independent())
2365 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2366 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2367 rela_dyn
->add_local_relative(object
, r_sym
,
2369 ? elfcpp::R_X86_64_RELATIVE64
2370 : elfcpp::R_X86_64_RELATIVE
),
2371 output_section
, data_shndx
,
2372 reloc
.get_r_offset(),
2373 reloc
.get_r_addend(), is_ifunc
);
2377 case elfcpp::R_X86_64_32
:
2378 case elfcpp::R_X86_64_32S
:
2379 case elfcpp::R_X86_64_16
:
2380 case elfcpp::R_X86_64_8
:
2381 // If building a shared library (or a position-independent
2382 // executable), we need to create a dynamic relocation for this
2383 // location. We can't use an R_X86_64_RELATIVE relocation
2384 // because that is always a 64-bit relocation.
2385 if (parameters
->options().output_is_position_independent())
2387 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2388 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2390 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2391 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2392 rela_dyn
->add_local_relative(object
, r_sym
,
2393 elfcpp::R_X86_64_RELATIVE
,
2394 output_section
, data_shndx
,
2395 reloc
.get_r_offset(),
2396 reloc
.get_r_addend(), is_ifunc
);
2400 this->check_non_pic(object
, r_type
, NULL
);
2402 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2403 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2404 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
2405 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
2406 data_shndx
, reloc
.get_r_offset(),
2407 reloc
.get_r_addend());
2410 gold_assert(lsym
.get_st_value() == 0);
2411 unsigned int shndx
= lsym
.get_st_shndx();
2413 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
2416 object
->error(_("section symbol %u has bad shndx %u"),
2419 rela_dyn
->add_local_section(object
, shndx
,
2420 r_type
, output_section
,
2421 data_shndx
, reloc
.get_r_offset(),
2422 reloc
.get_r_addend());
2427 case elfcpp::R_X86_64_PC64
:
2428 case elfcpp::R_X86_64_PC32
:
2429 case elfcpp::R_X86_64_PC32_BND
:
2430 case elfcpp::R_X86_64_PC16
:
2431 case elfcpp::R_X86_64_PC8
:
2434 case elfcpp::R_X86_64_PLT32
:
2435 case elfcpp::R_X86_64_PLT32_BND
:
2436 // Since we know this is a local symbol, we can handle this as a
2440 case elfcpp::R_X86_64_GOTPC32
:
2441 case elfcpp::R_X86_64_GOTOFF64
:
2442 case elfcpp::R_X86_64_GOTPC64
:
2443 case elfcpp::R_X86_64_PLTOFF64
:
2444 // We need a GOT section.
2445 target
->got_section(symtab
, layout
);
2446 // For PLTOFF64, we'd normally want a PLT section, but since we
2447 // know this is a local symbol, no PLT is needed.
2450 case elfcpp::R_X86_64_GOT64
:
2451 case elfcpp::R_X86_64_GOT32
:
2452 case elfcpp::R_X86_64_GOTPCREL64
:
2453 case elfcpp::R_X86_64_GOTPCREL
:
2454 case elfcpp::R_X86_64_GOTPLT64
:
2456 // The symbol requires a GOT entry.
2457 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2458 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2460 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2461 // lets function pointers compare correctly with shared
2462 // libraries. Otherwise we would need an IRELATIVE reloc.
2465 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
2467 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
2470 // If we are generating a shared object, we need to add a
2471 // dynamic relocation for this symbol's GOT entry.
2472 if (parameters
->options().output_is_position_independent())
2474 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2475 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2476 if (r_type
!= elfcpp::R_X86_64_GOT32
)
2478 unsigned int got_offset
=
2479 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
2480 rela_dyn
->add_local_relative(object
, r_sym
,
2481 elfcpp::R_X86_64_RELATIVE
,
2482 got
, got_offset
, 0, is_ifunc
);
2486 this->check_non_pic(object
, r_type
, NULL
);
2488 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
2489 rela_dyn
->add_local(
2490 object
, r_sym
, r_type
, got
,
2491 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
2495 // For GOTPLT64, we'd normally want a PLT section, but since
2496 // we know this is a local symbol, no PLT is needed.
2500 case elfcpp::R_X86_64_COPY
:
2501 case elfcpp::R_X86_64_GLOB_DAT
:
2502 case elfcpp::R_X86_64_JUMP_SLOT
:
2503 case elfcpp::R_X86_64_RELATIVE
:
2504 case elfcpp::R_X86_64_IRELATIVE
:
2505 // These are outstanding tls relocs, which are unexpected when linking
2506 case elfcpp::R_X86_64_TPOFF64
:
2507 case elfcpp::R_X86_64_DTPMOD64
:
2508 case elfcpp::R_X86_64_TLSDESC
:
2509 gold_error(_("%s: unexpected reloc %u in object file"),
2510 object
->name().c_str(), r_type
);
2513 // These are initial tls relocs, which are expected when linking
2514 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2515 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2516 case elfcpp::R_X86_64_TLSDESC_CALL
:
2517 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2518 case elfcpp::R_X86_64_DTPOFF32
:
2519 case elfcpp::R_X86_64_DTPOFF64
:
2520 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2521 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2523 bool output_is_shared
= parameters
->options().shared();
2524 const tls::Tls_optimization optimized_type
2525 = Target_x86_64
<size
>::optimize_tls_reloc(!output_is_shared
,
2529 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2530 if (optimized_type
== tls::TLSOPT_NONE
)
2532 // Create a pair of GOT entries for the module index and
2533 // dtv-relative offset.
2534 Output_data_got
<64, false>* got
2535 = target
->got_section(symtab
, layout
);
2536 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2537 unsigned int shndx
= lsym
.get_st_shndx();
2539 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2541 object
->error(_("local symbol %u has bad shndx %u"),
2544 got
->add_local_pair_with_rel(object
, r_sym
,
2547 target
->rela_dyn_section(layout
),
2548 elfcpp::R_X86_64_DTPMOD64
);
2550 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2551 unsupported_reloc_local(object
, r_type
);
2554 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2555 target
->define_tls_base_symbol(symtab
, layout
);
2556 if (optimized_type
== tls::TLSOPT_NONE
)
2558 // Create reserved PLT and GOT entries for the resolver.
2559 target
->reserve_tlsdesc_entries(symtab
, layout
);
2561 // Generate a double GOT entry with an
2562 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2563 // is resolved lazily, so the GOT entry needs to be in
2564 // an area in .got.plt, not .got. Call got_section to
2565 // make sure the section has been created.
2566 target
->got_section(symtab
, layout
);
2567 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2568 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2569 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
2571 unsigned int got_offset
= got
->add_constant(0);
2572 got
->add_constant(0);
2573 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
2575 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2576 // We store the arguments we need in a vector, and
2577 // use the index into the vector as the parameter
2578 // to pass to the target specific routines.
2579 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
2580 void* arg
= reinterpret_cast<void*>(intarg
);
2581 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
2582 got
, got_offset
, 0);
2585 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2586 unsupported_reloc_local(object
, r_type
);
2589 case elfcpp::R_X86_64_TLSDESC_CALL
:
2592 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2593 if (optimized_type
== tls::TLSOPT_NONE
)
2595 // Create a GOT entry for the module index.
2596 target
->got_mod_index_entry(symtab
, layout
, object
);
2598 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2599 unsupported_reloc_local(object
, r_type
);
2602 case elfcpp::R_X86_64_DTPOFF32
:
2603 case elfcpp::R_X86_64_DTPOFF64
:
2606 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2607 layout
->set_has_static_tls();
2608 if (optimized_type
== tls::TLSOPT_NONE
)
2610 // Create a GOT entry for the tp-relative offset.
2611 Output_data_got
<64, false>* got
2612 = target
->got_section(symtab
, layout
);
2613 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2614 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
2615 target
->rela_dyn_section(layout
),
2616 elfcpp::R_X86_64_TPOFF64
);
2618 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2619 unsupported_reloc_local(object
, r_type
);
2622 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2623 layout
->set_has_static_tls();
2624 if (output_is_shared
)
2625 unsupported_reloc_local(object
, r_type
);
2634 case elfcpp::R_X86_64_SIZE32
:
2635 case elfcpp::R_X86_64_SIZE64
:
2637 gold_error(_("%s: unsupported reloc %u against local symbol"),
2638 object
->name().c_str(), r_type
);
2644 // Report an unsupported relocation against a global symbol.
2648 Target_x86_64
<size
>::Scan::unsupported_reloc_global(
2649 Sized_relobj_file
<size
, false>* object
,
2650 unsigned int r_type
,
2653 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2654 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
2657 // Returns true if this relocation type could be that of a function pointer.
2660 Target_x86_64
<size
>::Scan::possible_function_pointer_reloc(unsigned int r_type
)
2664 case elfcpp::R_X86_64_64
:
2665 case elfcpp::R_X86_64_32
:
2666 case elfcpp::R_X86_64_32S
:
2667 case elfcpp::R_X86_64_16
:
2668 case elfcpp::R_X86_64_8
:
2669 case elfcpp::R_X86_64_GOT64
:
2670 case elfcpp::R_X86_64_GOT32
:
2671 case elfcpp::R_X86_64_GOTPCREL64
:
2672 case elfcpp::R_X86_64_GOTPCREL
:
2673 case elfcpp::R_X86_64_GOTPLT64
:
2681 // For safe ICF, scan a relocation for a local symbol to check if it
2682 // corresponds to a function pointer being taken. In that case mark
2683 // the function whose pointer was taken as not foldable.
2687 Target_x86_64
<size
>::Scan::local_reloc_may_be_function_pointer(
2690 Target_x86_64
<size
>* ,
2691 Sized_relobj_file
<size
, false>* ,
2694 const elfcpp::Rela
<size
, false>& ,
2695 unsigned int r_type
,
2696 const elfcpp::Sym
<size
, false>&)
2698 // When building a shared library, do not fold any local symbols as it is
2699 // not possible to distinguish pointer taken versus a call by looking at
2700 // the relocation types.
2701 return (parameters
->options().shared()
2702 || possible_function_pointer_reloc(r_type
));
2705 // For safe ICF, scan a relocation for a global symbol to check if it
2706 // corresponds to a function pointer being taken. In that case mark
2707 // the function whose pointer was taken as not foldable.
2711 Target_x86_64
<size
>::Scan::global_reloc_may_be_function_pointer(
2714 Target_x86_64
<size
>* ,
2715 Sized_relobj_file
<size
, false>* ,
2718 const elfcpp::Rela
<size
, false>& ,
2719 unsigned int r_type
,
2722 // When building a shared library, do not fold symbols whose visibility
2723 // is hidden, internal or protected.
2724 return ((parameters
->options().shared()
2725 && (gsym
->visibility() == elfcpp::STV_INTERNAL
2726 || gsym
->visibility() == elfcpp::STV_PROTECTED
2727 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
2728 || possible_function_pointer_reloc(r_type
));
2731 // Scan a relocation for a global symbol.
2735 Target_x86_64
<size
>::Scan::global(Symbol_table
* symtab
,
2737 Target_x86_64
<size
>* target
,
2738 Sized_relobj_file
<size
, false>* object
,
2739 unsigned int data_shndx
,
2740 Output_section
* output_section
,
2741 const elfcpp::Rela
<size
, false>& reloc
,
2742 unsigned int r_type
,
2745 // A STT_GNU_IFUNC symbol may require a PLT entry.
2746 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2747 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
2748 target
->make_plt_entry(symtab
, layout
, gsym
);
2752 case elfcpp::R_X86_64_NONE
:
2753 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2754 case elfcpp::R_X86_64_GNU_VTENTRY
:
2757 case elfcpp::R_X86_64_64
:
2758 case elfcpp::R_X86_64_32
:
2759 case elfcpp::R_X86_64_32S
:
2760 case elfcpp::R_X86_64_16
:
2761 case elfcpp::R_X86_64_8
:
2763 // Make a PLT entry if necessary.
2764 if (gsym
->needs_plt_entry())
2766 target
->make_plt_entry(symtab
, layout
, gsym
);
2767 // Since this is not a PC-relative relocation, we may be
2768 // taking the address of a function. In that case we need to
2769 // set the entry in the dynamic symbol table to the address of
2771 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
2772 gsym
->set_needs_dynsym_value();
2774 // Make a dynamic relocation if necessary.
2775 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2777 if (!parameters
->options().output_is_position_independent()
2778 && gsym
->may_need_copy_reloc())
2780 target
->copy_reloc(symtab
, layout
, object
,
2781 data_shndx
, output_section
, gsym
, reloc
);
2783 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
2784 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
2785 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2786 && gsym
->can_use_relative_reloc(false)
2787 && !gsym
->is_from_dynobj()
2788 && !gsym
->is_undefined()
2789 && !gsym
->is_preemptible())
2791 // Use an IRELATIVE reloc for a locally defined
2792 // STT_GNU_IFUNC symbol. This makes a function
2793 // address in a PIE executable match the address in a
2794 // shared library that it links against.
2795 Reloc_section
* rela_dyn
=
2796 target
->rela_irelative_section(layout
);
2797 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
2798 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
2799 output_section
, object
,
2801 reloc
.get_r_offset(),
2802 reloc
.get_r_addend());
2804 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
2805 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
2806 && gsym
->can_use_relative_reloc(false))
2808 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2809 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2810 output_section
, object
,
2812 reloc
.get_r_offset(),
2813 reloc
.get_r_addend(), false);
2817 this->check_non_pic(object
, r_type
, gsym
);
2818 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2819 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2820 data_shndx
, reloc
.get_r_offset(),
2821 reloc
.get_r_addend());
2827 case elfcpp::R_X86_64_PC64
:
2828 case elfcpp::R_X86_64_PC32
:
2829 case elfcpp::R_X86_64_PC32_BND
:
2830 case elfcpp::R_X86_64_PC16
:
2831 case elfcpp::R_X86_64_PC8
:
2833 // Make a PLT entry if necessary.
2834 if (gsym
->needs_plt_entry())
2835 target
->make_plt_entry(symtab
, layout
, gsym
);
2836 // Make a dynamic relocation if necessary.
2837 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2839 if (parameters
->options().output_is_executable()
2840 && gsym
->may_need_copy_reloc())
2842 target
->copy_reloc(symtab
, layout
, object
,
2843 data_shndx
, output_section
, gsym
, reloc
);
2847 this->check_non_pic(object
, r_type
, gsym
);
2848 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2849 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2850 data_shndx
, reloc
.get_r_offset(),
2851 reloc
.get_r_addend());
2857 case elfcpp::R_X86_64_GOT64
:
2858 case elfcpp::R_X86_64_GOT32
:
2859 case elfcpp::R_X86_64_GOTPCREL64
:
2860 case elfcpp::R_X86_64_GOTPCREL
:
2861 case elfcpp::R_X86_64_GOTPLT64
:
2863 // The symbol requires a GOT entry.
2864 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2865 if (gsym
->final_value_is_known())
2867 // For a STT_GNU_IFUNC symbol we want the PLT address.
2868 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
2869 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2871 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2875 // If this symbol is not fully resolved, we need to add a
2876 // dynamic relocation for it.
2877 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2879 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2881 // 1) The symbol may be defined in some other module.
2883 // 2) We are building a shared library and this is a
2884 // protected symbol; using GLOB_DAT means that the dynamic
2885 // linker can use the address of the PLT in the main
2886 // executable when appropriate so that function address
2887 // comparisons work.
2889 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2890 // code, again so that function address comparisons work.
2891 if (gsym
->is_from_dynobj()
2892 || gsym
->is_undefined()
2893 || gsym
->is_preemptible()
2894 || (gsym
->visibility() == elfcpp::STV_PROTECTED
2895 && parameters
->options().shared())
2896 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
2897 && parameters
->options().output_is_position_independent()))
2898 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2899 elfcpp::R_X86_64_GLOB_DAT
);
2902 // For a STT_GNU_IFUNC symbol we want to write the PLT
2903 // offset into the GOT, so that function pointer
2904 // comparisons work correctly.
2906 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
2907 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2910 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2911 // Tell the dynamic linker to use the PLT address
2912 // when resolving relocations.
2913 if (gsym
->is_from_dynobj()
2914 && !parameters
->options().shared())
2915 gsym
->set_needs_dynsym_value();
2919 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2920 rela_dyn
->add_global_relative(gsym
,
2921 elfcpp::R_X86_64_RELATIVE
,
2922 got
, got_off
, 0, false);
2926 // For GOTPLT64, we also need a PLT entry (but only if the
2927 // symbol is not fully resolved).
2928 if (r_type
== elfcpp::R_X86_64_GOTPLT64
2929 && !gsym
->final_value_is_known())
2930 target
->make_plt_entry(symtab
, layout
, gsym
);
2934 case elfcpp::R_X86_64_PLT32
:
2935 case elfcpp::R_X86_64_PLT32_BND
:
2936 // If the symbol is fully resolved, this is just a PC32 reloc.
2937 // Otherwise we need a PLT entry.
2938 if (gsym
->final_value_is_known())
2940 // If building a shared library, we can also skip the PLT entry
2941 // if the symbol is defined in the output file and is protected
2943 if (gsym
->is_defined()
2944 && !gsym
->is_from_dynobj()
2945 && !gsym
->is_preemptible())
2947 target
->make_plt_entry(symtab
, layout
, gsym
);
2950 case elfcpp::R_X86_64_GOTPC32
:
2951 case elfcpp::R_X86_64_GOTOFF64
:
2952 case elfcpp::R_X86_64_GOTPC64
:
2953 case elfcpp::R_X86_64_PLTOFF64
:
2954 // We need a GOT section.
2955 target
->got_section(symtab
, layout
);
2956 // For PLTOFF64, we also need a PLT entry (but only if the
2957 // symbol is not fully resolved).
2958 if (r_type
== elfcpp::R_X86_64_PLTOFF64
2959 && !gsym
->final_value_is_known())
2960 target
->make_plt_entry(symtab
, layout
, gsym
);
2963 case elfcpp::R_X86_64_COPY
:
2964 case elfcpp::R_X86_64_GLOB_DAT
:
2965 case elfcpp::R_X86_64_JUMP_SLOT
:
2966 case elfcpp::R_X86_64_RELATIVE
:
2967 case elfcpp::R_X86_64_IRELATIVE
:
2968 // These are outstanding tls relocs, which are unexpected when linking
2969 case elfcpp::R_X86_64_TPOFF64
:
2970 case elfcpp::R_X86_64_DTPMOD64
:
2971 case elfcpp::R_X86_64_TLSDESC
:
2972 gold_error(_("%s: unexpected reloc %u in object file"),
2973 object
->name().c_str(), r_type
);
2976 // These are initial tls relocs, which are expected for global()
2977 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2978 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2979 case elfcpp::R_X86_64_TLSDESC_CALL
:
2980 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2981 case elfcpp::R_X86_64_DTPOFF32
:
2982 case elfcpp::R_X86_64_DTPOFF64
:
2983 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2984 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2986 const bool is_final
= gsym
->final_value_is_known();
2987 const tls::Tls_optimization optimized_type
2988 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
2991 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2992 if (optimized_type
== tls::TLSOPT_NONE
)
2994 // Create a pair of GOT entries for the module index and
2995 // dtv-relative offset.
2996 Output_data_got
<64, false>* got
2997 = target
->got_section(symtab
, layout
);
2998 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
2999 target
->rela_dyn_section(layout
),
3000 elfcpp::R_X86_64_DTPMOD64
,
3001 elfcpp::R_X86_64_DTPOFF64
);
3003 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3005 // Create a GOT entry for the tp-relative offset.
3006 Output_data_got
<64, false>* got
3007 = target
->got_section(symtab
, layout
);
3008 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3009 target
->rela_dyn_section(layout
),
3010 elfcpp::R_X86_64_TPOFF64
);
3012 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3013 unsupported_reloc_global(object
, r_type
, gsym
);
3016 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3017 target
->define_tls_base_symbol(symtab
, layout
);
3018 if (optimized_type
== tls::TLSOPT_NONE
)
3020 // Create reserved PLT and GOT entries for the resolver.
3021 target
->reserve_tlsdesc_entries(symtab
, layout
);
3023 // Create a double GOT entry with an R_X86_64_TLSDESC
3024 // reloc. The R_X86_64_TLSDESC reloc is resolved
3025 // lazily, so the GOT entry needs to be in an area in
3026 // .got.plt, not .got. Call got_section to make sure
3027 // the section has been created.
3028 target
->got_section(symtab
, layout
);
3029 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
3030 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
3031 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
3032 elfcpp::R_X86_64_TLSDESC
, 0);
3034 else if (optimized_type
== tls::TLSOPT_TO_IE
)
3036 // Create a GOT entry for the tp-relative offset.
3037 Output_data_got
<64, false>* got
3038 = target
->got_section(symtab
, layout
);
3039 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3040 target
->rela_dyn_section(layout
),
3041 elfcpp::R_X86_64_TPOFF64
);
3043 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3044 unsupported_reloc_global(object
, r_type
, gsym
);
3047 case elfcpp::R_X86_64_TLSDESC_CALL
:
3050 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3051 if (optimized_type
== tls::TLSOPT_NONE
)
3053 // Create a GOT entry for the module index.
3054 target
->got_mod_index_entry(symtab
, layout
, object
);
3056 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3057 unsupported_reloc_global(object
, r_type
, gsym
);
3060 case elfcpp::R_X86_64_DTPOFF32
:
3061 case elfcpp::R_X86_64_DTPOFF64
:
3064 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3065 layout
->set_has_static_tls();
3066 if (optimized_type
== tls::TLSOPT_NONE
)
3068 // Create a GOT entry for the tp-relative offset.
3069 Output_data_got
<64, false>* got
3070 = target
->got_section(symtab
, layout
);
3071 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3072 target
->rela_dyn_section(layout
),
3073 elfcpp::R_X86_64_TPOFF64
);
3075 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3076 unsupported_reloc_global(object
, r_type
, gsym
);
3079 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3080 layout
->set_has_static_tls();
3081 if (parameters
->options().shared())
3082 unsupported_reloc_global(object
, r_type
, gsym
);
3091 case elfcpp::R_X86_64_SIZE32
:
3092 case elfcpp::R_X86_64_SIZE64
:
3094 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3095 object
->name().c_str(), r_type
,
3096 gsym
->demangled_name().c_str());
3103 Target_x86_64
<size
>::gc_process_relocs(Symbol_table
* symtab
,
3105 Sized_relobj_file
<size
, false>* object
,
3106 unsigned int data_shndx
,
3107 unsigned int sh_type
,
3108 const unsigned char* prelocs
,
3110 Output_section
* output_section
,
3111 bool needs_special_offset_handling
,
3112 size_t local_symbol_count
,
3113 const unsigned char* plocal_symbols
)
3116 if (sh_type
== elfcpp::SHT_REL
)
3121 gold::gc_process_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
3122 typename Target_x86_64
<size
>::Scan
,
3123 typename Target_x86_64
<size
>::Relocatable_size_for_reloc
>(
3132 needs_special_offset_handling
,
3137 // Scan relocations for a section.
3141 Target_x86_64
<size
>::scan_relocs(Symbol_table
* symtab
,
3143 Sized_relobj_file
<size
, false>* object
,
3144 unsigned int data_shndx
,
3145 unsigned int sh_type
,
3146 const unsigned char* prelocs
,
3148 Output_section
* output_section
,
3149 bool needs_special_offset_handling
,
3150 size_t local_symbol_count
,
3151 const unsigned char* plocal_symbols
)
3153 if (sh_type
== elfcpp::SHT_REL
)
3155 gold_error(_("%s: unsupported REL reloc section"),
3156 object
->name().c_str());
3160 gold::scan_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
3161 typename Target_x86_64
<size
>::Scan
>(
3170 needs_special_offset_handling
,
3175 // Finalize the sections.
3179 Target_x86_64
<size
>::do_finalize_sections(
3181 const Input_objects
*,
3182 Symbol_table
* symtab
)
3184 const Reloc_section
* rel_plt
= (this->plt_
== NULL
3186 : this->plt_
->rela_plt());
3187 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
3188 this->rela_dyn_
, true, false);
3190 // Fill in some more dynamic tags.
3191 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
3194 if (this->plt_
!= NULL
3195 && this->plt_
->output_section() != NULL
3196 && this->plt_
->has_tlsdesc_entry())
3198 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
3199 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
3200 this->got_
->finalize_data_size();
3201 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
3202 this->plt_
, plt_offset
);
3203 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
3204 this->got_
, got_offset
);
3208 // Emit any relocs we saved in an attempt to avoid generating COPY
3210 if (this->copy_relocs_
.any_saved_relocs())
3211 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
3213 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3214 // the .got.plt section.
3215 Symbol
* sym
= this->global_offset_table_
;
3218 uint64_t data_size
= this->got_plt_
->current_data_size();
3219 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
3222 if (parameters
->doing_static_link()
3223 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
3225 // If linking statically, make sure that the __rela_iplt symbols
3226 // were defined if necessary, even if we didn't create a PLT.
3227 static const Define_symbol_in_segment syms
[] =
3230 "__rela_iplt_start", // name
3231 elfcpp::PT_LOAD
, // segment_type
3232 elfcpp::PF_W
, // segment_flags_set
3233 elfcpp::PF(0), // segment_flags_clear
3236 elfcpp::STT_NOTYPE
, // type
3237 elfcpp::STB_GLOBAL
, // binding
3238 elfcpp::STV_HIDDEN
, // visibility
3240 Symbol::SEGMENT_START
, // offset_from_base
3244 "__rela_iplt_end", // name
3245 elfcpp::PT_LOAD
, // segment_type
3246 elfcpp::PF_W
, // segment_flags_set
3247 elfcpp::PF(0), // segment_flags_clear
3250 elfcpp::STT_NOTYPE
, // type
3251 elfcpp::STB_GLOBAL
, // binding
3252 elfcpp::STV_HIDDEN
, // visibility
3254 Symbol::SEGMENT_START
, // offset_from_base
3259 symtab
->define_symbols(layout
, 2, syms
,
3260 layout
->script_options()->saw_sections_clause());
3264 // Perform a relocation.
3268 Target_x86_64
<size
>::Relocate::relocate(
3269 const Relocate_info
<size
, false>* relinfo
,
3270 Target_x86_64
<size
>* target
,
3273 const elfcpp::Rela
<size
, false>& rela
,
3274 unsigned int r_type
,
3275 const Sized_symbol
<size
>* gsym
,
3276 const Symbol_value
<size
>* psymval
,
3277 unsigned char* view
,
3278 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3279 section_size_type view_size
)
3281 if (this->skip_call_tls_get_addr_
)
3283 if ((r_type
!= elfcpp::R_X86_64_PLT32
3284 && r_type
!= elfcpp::R_X86_64_PLT32_BND
3285 && r_type
!= elfcpp::R_X86_64_PC32_BND
3286 && r_type
!= elfcpp::R_X86_64_PC32
)
3288 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
3290 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3291 _("missing expected TLS relocation"));
3295 this->skip_call_tls_get_addr_
= false;
3303 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3305 // Pick the value to use for symbols defined in the PLT.
3306 Symbol_value
<size
> symval
;
3308 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
3310 symval
.set_output_value(target
->plt_address_for_global(gsym
));
3313 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
3315 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3316 if (object
->local_has_plt_offset(r_sym
))
3318 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
3323 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3325 // Get the GOT offset if needed.
3326 // The GOT pointer points to the end of the GOT section.
3327 // We need to subtract the size of the GOT section to get
3328 // the actual offset to use in the relocation.
3329 bool have_got_offset
= false;
3330 // Since the actual offset is always negative, we use signed int to
3331 // support 64-bit GOT relocations.
3335 case elfcpp::R_X86_64_GOT32
:
3336 case elfcpp::R_X86_64_GOT64
:
3337 case elfcpp::R_X86_64_GOTPLT64
:
3338 case elfcpp::R_X86_64_GOTPCREL
:
3339 case elfcpp::R_X86_64_GOTPCREL64
:
3342 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3343 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
3347 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3348 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3349 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
3350 - target
->got_size());
3352 have_got_offset
= true;
3361 case elfcpp::R_X86_64_NONE
:
3362 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3363 case elfcpp::R_X86_64_GNU_VTENTRY
:
3366 case elfcpp::R_X86_64_64
:
3367 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
, addend
);
3370 case elfcpp::R_X86_64_PC64
:
3371 Relocate_functions
<size
, false>::pcrela64(view
, object
, psymval
, addend
,
3375 case elfcpp::R_X86_64_32
:
3376 // FIXME: we need to verify that value + addend fits into 32 bits:
3377 // uint64_t x = value + addend;
3378 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3379 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
3380 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3383 case elfcpp::R_X86_64_32S
:
3384 // FIXME: we need to verify that value + addend fits into 32 bits:
3385 // int64_t x = value + addend; // note this quantity is signed!
3386 // x == static_cast<int64_t>(static_cast<int32_t>(x))
3387 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3390 case elfcpp::R_X86_64_PC32
:
3391 case elfcpp::R_X86_64_PC32_BND
:
3392 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3396 case elfcpp::R_X86_64_16
:
3397 Relocate_functions
<size
, false>::rela16(view
, object
, psymval
, addend
);
3400 case elfcpp::R_X86_64_PC16
:
3401 Relocate_functions
<size
, false>::pcrela16(view
, object
, psymval
, addend
,
3405 case elfcpp::R_X86_64_8
:
3406 Relocate_functions
<size
, false>::rela8(view
, object
, psymval
, addend
);
3409 case elfcpp::R_X86_64_PC8
:
3410 Relocate_functions
<size
, false>::pcrela8(view
, object
, psymval
, addend
,
3414 case elfcpp::R_X86_64_PLT32
:
3415 case elfcpp::R_X86_64_PLT32_BND
:
3416 gold_assert(gsym
== NULL
3417 || gsym
->has_plt_offset()
3418 || gsym
->final_value_is_known()
3419 || (gsym
->is_defined()
3420 && !gsym
->is_from_dynobj()
3421 && !gsym
->is_preemptible()));
3422 // Note: while this code looks the same as for R_X86_64_PC32, it
3423 // behaves differently because psymval was set to point to
3424 // the PLT entry, rather than the symbol, in Scan::global().
3425 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3429 case elfcpp::R_X86_64_PLTOFF64
:
3432 gold_assert(gsym
->has_plt_offset()
3433 || gsym
->final_value_is_known());
3434 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
;
3435 // This is the address of GLOBAL_OFFSET_TABLE.
3436 got_address
= target
->got_plt_section()->address();
3437 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
,
3438 addend
- got_address
);
3442 case elfcpp::R_X86_64_GOT32
:
3443 gold_assert(have_got_offset
);
3444 Relocate_functions
<size
, false>::rela32(view
, got_offset
, addend
);
3447 case elfcpp::R_X86_64_GOTPC32
:
3450 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3451 value
= target
->got_plt_section()->address();
3452 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3456 case elfcpp::R_X86_64_GOT64
:
3457 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
3458 // Since we always add a PLT entry, this is equivalent.
3459 case elfcpp::R_X86_64_GOTPLT64
:
3460 gold_assert(have_got_offset
);
3461 Relocate_functions
<size
, false>::rela64(view
, got_offset
, addend
);
3464 case elfcpp::R_X86_64_GOTPC64
:
3467 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3468 value
= target
->got_plt_section()->address();
3469 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3473 case elfcpp::R_X86_64_GOTOFF64
:
3475 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3476 value
= (psymval
->value(object
, 0)
3477 - target
->got_plt_section()->address());
3478 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3482 case elfcpp::R_X86_64_GOTPCREL
:
3484 gold_assert(have_got_offset
);
3485 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3486 value
= target
->got_plt_section()->address() + got_offset
;
3487 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3491 case elfcpp::R_X86_64_GOTPCREL64
:
3493 gold_assert(have_got_offset
);
3494 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3495 value
= target
->got_plt_section()->address() + got_offset
;
3496 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3500 case elfcpp::R_X86_64_COPY
:
3501 case elfcpp::R_X86_64_GLOB_DAT
:
3502 case elfcpp::R_X86_64_JUMP_SLOT
:
3503 case elfcpp::R_X86_64_RELATIVE
:
3504 case elfcpp::R_X86_64_IRELATIVE
:
3505 // These are outstanding tls relocs, which are unexpected when linking
3506 case elfcpp::R_X86_64_TPOFF64
:
3507 case elfcpp::R_X86_64_DTPMOD64
:
3508 case elfcpp::R_X86_64_TLSDESC
:
3509 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3510 _("unexpected reloc %u in object file"),
3514 // These are initial tls relocs, which are expected when linking
3515 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3516 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3517 case elfcpp::R_X86_64_TLSDESC_CALL
:
3518 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3519 case elfcpp::R_X86_64_DTPOFF32
:
3520 case elfcpp::R_X86_64_DTPOFF64
:
3521 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3522 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3523 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
3524 view
, address
, view_size
);
3527 case elfcpp::R_X86_64_SIZE32
:
3528 case elfcpp::R_X86_64_SIZE64
:
3530 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3531 _("unsupported reloc %u"),
3539 // Perform a TLS relocation.
3543 Target_x86_64
<size
>::Relocate::relocate_tls(
3544 const Relocate_info
<size
, false>* relinfo
,
3545 Target_x86_64
<size
>* target
,
3547 const elfcpp::Rela
<size
, false>& rela
,
3548 unsigned int r_type
,
3549 const Sized_symbol
<size
>* gsym
,
3550 const Symbol_value
<size
>* psymval
,
3551 unsigned char* view
,
3552 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3553 section_size_type view_size
)
3555 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
3557 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3558 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3559 elfcpp::Shdr
<size
, false> data_shdr(relinfo
->data_shdr
);
3560 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
3562 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
3564 const bool is_final
= (gsym
== NULL
3565 ? !parameters
->options().shared()
3566 : gsym
->final_value_is_known());
3567 tls::Tls_optimization optimized_type
3568 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3571 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3572 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3574 // If this code sequence is used in a non-executable section,
3575 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3576 // on the assumption that it's being used by itself in a debug
3577 // section. Therefore, in the unlikely event that the code
3578 // sequence appears in a non-executable section, we simply
3579 // leave it unoptimized.
3580 optimized_type
= tls::TLSOPT_NONE
;
3582 if (optimized_type
== tls::TLSOPT_TO_LE
)
3584 if (tls_segment
== NULL
)
3586 gold_assert(parameters
->errors()->error_count() > 0
3587 || issue_undefined_symbol_error(gsym
));
3590 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
3591 rela
, r_type
, value
, view
,
3597 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3598 ? GOT_TYPE_TLS_OFFSET
3599 : GOT_TYPE_TLS_PAIR
);
3600 unsigned int got_offset
;
3603 gold_assert(gsym
->has_got_offset(got_type
));
3604 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
3608 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3609 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3610 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
3611 - target
->got_size());
3613 if (optimized_type
== tls::TLSOPT_TO_IE
)
3615 value
= target
->got_plt_section()->address() + got_offset
;
3616 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3617 value
, view
, address
, view_size
);
3620 else if (optimized_type
== tls::TLSOPT_NONE
)
3622 // Relocate the field with the offset of the pair of GOT
3624 value
= target
->got_plt_section()->address() + got_offset
;
3625 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3630 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3631 _("unsupported reloc %u"), r_type
);
3634 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3635 case elfcpp::R_X86_64_TLSDESC_CALL
:
3636 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3638 // See above comment for R_X86_64_TLSGD.
3639 optimized_type
= tls::TLSOPT_NONE
;
3641 if (optimized_type
== tls::TLSOPT_TO_LE
)
3643 if (tls_segment
== NULL
)
3645 gold_assert(parameters
->errors()->error_count() > 0
3646 || issue_undefined_symbol_error(gsym
));
3649 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
3650 rela
, r_type
, value
, view
,
3656 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3657 ? GOT_TYPE_TLS_OFFSET
3658 : GOT_TYPE_TLS_DESC
);
3659 unsigned int got_offset
= 0;
3660 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
3661 && optimized_type
== tls::TLSOPT_NONE
)
3663 // We created GOT entries in the .got.tlsdesc portion of
3664 // the .got.plt section, but the offset stored in the
3665 // symbol is the offset within .got.tlsdesc.
3666 got_offset
= (target
->got_size()
3667 + target
->got_plt_section()->data_size());
3671 gold_assert(gsym
->has_got_offset(got_type
));
3672 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
3676 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3677 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3678 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
3679 - target
->got_size());
3681 if (optimized_type
== tls::TLSOPT_TO_IE
)
3683 if (tls_segment
== NULL
)
3685 gold_assert(parameters
->errors()->error_count() > 0
3686 || issue_undefined_symbol_error(gsym
));
3689 value
= target
->got_plt_section()->address() + got_offset
;
3690 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
3691 rela
, r_type
, value
, view
, address
,
3695 else if (optimized_type
== tls::TLSOPT_NONE
)
3697 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3699 // Relocate the field with the offset of the pair of GOT
3701 value
= target
->got_plt_section()->address() + got_offset
;
3702 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3708 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3709 _("unsupported reloc %u"), r_type
);
3712 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3713 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3715 // See above comment for R_X86_64_TLSGD.
3716 optimized_type
= tls::TLSOPT_NONE
;
3718 if (optimized_type
== tls::TLSOPT_TO_LE
)
3720 if (tls_segment
== NULL
)
3722 gold_assert(parameters
->errors()->error_count() > 0
3723 || issue_undefined_symbol_error(gsym
));
3726 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3727 value
, view
, view_size
);
3730 else if (optimized_type
== tls::TLSOPT_NONE
)
3732 // Relocate the field with the offset of the GOT entry for
3733 // the module index.
3734 unsigned int got_offset
;
3735 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
3736 - target
->got_size());
3737 value
= target
->got_plt_section()->address() + got_offset
;
3738 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3742 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3743 _("unsupported reloc %u"), r_type
);
3746 case elfcpp::R_X86_64_DTPOFF32
:
3747 // This relocation type is used in debugging information.
3748 // In that case we need to not optimize the value. If the
3749 // section is not executable, then we assume we should not
3750 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3751 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3753 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3755 if (tls_segment
== NULL
)
3757 gold_assert(parameters
->errors()->error_count() > 0
3758 || issue_undefined_symbol_error(gsym
));
3761 value
-= tls_segment
->memsz();
3763 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3766 case elfcpp::R_X86_64_DTPOFF64
:
3767 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3768 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3770 if (tls_segment
== NULL
)
3772 gold_assert(parameters
->errors()->error_count() > 0
3773 || issue_undefined_symbol_error(gsym
));
3776 value
-= tls_segment
->memsz();
3778 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3781 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3782 if (optimized_type
== tls::TLSOPT_TO_LE
)
3784 if (tls_segment
== NULL
)
3786 gold_assert(parameters
->errors()->error_count() > 0
3787 || issue_undefined_symbol_error(gsym
));
3790 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
3792 r_type
, value
, view
,
3796 else if (optimized_type
== tls::TLSOPT_NONE
)
3798 // Relocate the field with the offset of the GOT entry for
3799 // the tp-relative offset of the symbol.
3800 unsigned int got_offset
;
3803 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
3804 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
3805 - target
->got_size());
3809 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3810 gold_assert(object
->local_has_got_offset(r_sym
,
3811 GOT_TYPE_TLS_OFFSET
));
3812 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
3813 - target
->got_size());
3815 value
= target
->got_plt_section()->address() + got_offset
;
3816 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3820 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3821 _("unsupported reloc type %u"),
3825 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3826 if (tls_segment
== NULL
)
3828 gold_assert(parameters
->errors()->error_count() > 0
3829 || issue_undefined_symbol_error(gsym
));
3832 value
-= tls_segment
->memsz();
3833 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3838 // Do a relocation in which we convert a TLS General-Dynamic to an
3843 Target_x86_64
<size
>::Relocate::tls_gd_to_ie(
3844 const Relocate_info
<size
, false>* relinfo
,
3847 const elfcpp::Rela
<size
, false>& rela
,
3849 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3850 unsigned char* view
,
3851 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3852 section_size_type view_size
)
3855 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3856 // .word 0x6666; rex64; call __tls_get_addr
3857 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3859 // leaq foo@tlsgd(%rip),%rdi;
3860 // .word 0x6666; rex64; call __tls_get_addr
3861 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
3863 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3864 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3865 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3869 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3871 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3872 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3873 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3878 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3880 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3881 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
3882 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3886 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3887 Relocate_functions
<size
, false>::pcrela32(view
+ 8, value
, addend
- 8,
3890 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3892 this->skip_call_tls_get_addr_
= true;
3895 // Do a relocation in which we convert a TLS General-Dynamic to a
3900 Target_x86_64
<size
>::Relocate::tls_gd_to_le(
3901 const Relocate_info
<size
, false>* relinfo
,
3903 Output_segment
* tls_segment
,
3904 const elfcpp::Rela
<size
, false>& rela
,
3906 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3907 unsigned char* view
,
3908 section_size_type view_size
)
3911 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3912 // .word 0x6666; rex64; call __tls_get_addr
3913 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3915 // leaq foo@tlsgd(%rip),%rdi;
3916 // .word 0x6666; rex64; call __tls_get_addr
3917 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
3919 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3920 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3921 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3925 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3927 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3928 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3929 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
3934 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3936 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3937 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
3939 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
3943 value
-= tls_segment
->memsz();
3944 Relocate_functions
<size
, false>::rela32(view
+ 8, value
, 0);
3946 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3948 this->skip_call_tls_get_addr_
= true;
3951 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3955 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_ie(
3956 const Relocate_info
<size
, false>* relinfo
,
3959 const elfcpp::Rela
<size
, false>& rela
,
3960 unsigned int r_type
,
3961 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3962 unsigned char* view
,
3963 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3964 section_size_type view_size
)
3966 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3968 // leaq foo@tlsdesc(%rip), %rax
3969 // ==> movq foo@gottpoff(%rip), %rax
3970 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3971 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3972 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3973 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
3975 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3976 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3980 // call *foo@tlscall(%rax)
3982 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
3983 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
3984 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3985 view
[0] == 0xff && view
[1] == 0x10);
3991 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3995 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_le(
3996 const Relocate_info
<size
, false>* relinfo
,
3998 Output_segment
* tls_segment
,
3999 const elfcpp::Rela
<size
, false>& rela
,
4000 unsigned int r_type
,
4001 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4002 unsigned char* view
,
4003 section_size_type view_size
)
4005 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
4007 // leaq foo@tlsdesc(%rip), %rax
4008 // ==> movq foo@tpoff, %rax
4009 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4010 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4011 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4012 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
4015 value
-= tls_segment
->memsz();
4016 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
4020 // call *foo@tlscall(%rax)
4022 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
4023 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
4024 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4025 view
[0] == 0xff && view
[1] == 0x10);
4033 Target_x86_64
<size
>::Relocate::tls_ld_to_le(
4034 const Relocate_info
<size
, false>* relinfo
,
4037 const elfcpp::Rela
<size
, false>& rela
,
4039 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
4040 unsigned char* view
,
4041 section_size_type view_size
)
4043 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4045 // ... leq foo@dtpoff(%rax),%reg
4046 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4048 // ... leq foo@dtpoff(%rax),%reg
4049 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4051 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4052 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
4054 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4055 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
4057 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
4060 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4062 memcpy(view
- 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4064 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4066 this->skip_call_tls_get_addr_
= true;
4069 // Do a relocation in which we convert a TLS Initial-Exec to a
4074 Target_x86_64
<size
>::Relocate::tls_ie_to_le(
4075 const Relocate_info
<size
, false>* relinfo
,
4077 Output_segment
* tls_segment
,
4078 const elfcpp::Rela
<size
, false>& rela
,
4080 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4081 unsigned char* view
,
4082 section_size_type view_size
)
4084 // We need to examine the opcodes to figure out which instruction we
4087 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4088 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4090 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4091 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4093 unsigned char op1
= view
[-3];
4094 unsigned char op2
= view
[-2];
4095 unsigned char op3
= view
[-1];
4096 unsigned char reg
= op3
>> 3;
4103 else if (size
== 32 && op1
== 0x44)
4106 view
[-1] = 0xc0 | reg
;
4110 // Special handling for %rsp.
4113 else if (size
== 32 && op1
== 0x44)
4116 view
[-1] = 0xc0 | reg
;
4123 else if (size
== 32 && op1
== 0x44)
4126 view
[-1] = 0x80 | reg
| (reg
<< 3);
4129 value
-= tls_segment
->memsz();
4130 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
4133 // Relocate section data.
4137 Target_x86_64
<size
>::relocate_section(
4138 const Relocate_info
<size
, false>* relinfo
,
4139 unsigned int sh_type
,
4140 const unsigned char* prelocs
,
4142 Output_section
* output_section
,
4143 bool needs_special_offset_handling
,
4144 unsigned char* view
,
4145 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4146 section_size_type view_size
,
4147 const Reloc_symbol_changes
* reloc_symbol_changes
)
4149 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4151 gold::relocate_section
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
4152 typename Target_x86_64
<size
>::Relocate
,
4153 gold::Default_comdat_behavior
>(
4159 needs_special_offset_handling
,
4163 reloc_symbol_changes
);
4166 // Apply an incremental relocation. Incremental relocations always refer
4167 // to global symbols.
4171 Target_x86_64
<size
>::apply_relocation(
4172 const Relocate_info
<size
, false>* relinfo
,
4173 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
4174 unsigned int r_type
,
4175 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
4177 unsigned char* view
,
4178 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4179 section_size_type view_size
)
4181 gold::apply_relocation
<size
, false, Target_x86_64
<size
>,
4182 typename Target_x86_64
<size
>::Relocate
>(
4194 // Return the size of a relocation while scanning during a relocatable
4199 Target_x86_64
<size
>::Relocatable_size_for_reloc::get_size_for_reloc(
4200 unsigned int r_type
,
4205 case elfcpp::R_X86_64_NONE
:
4206 case elfcpp::R_X86_64_GNU_VTINHERIT
:
4207 case elfcpp::R_X86_64_GNU_VTENTRY
:
4208 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4209 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4210 case elfcpp::R_X86_64_TLSDESC_CALL
:
4211 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4212 case elfcpp::R_X86_64_DTPOFF32
:
4213 case elfcpp::R_X86_64_DTPOFF64
:
4214 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4215 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4218 case elfcpp::R_X86_64_64
:
4219 case elfcpp::R_X86_64_PC64
:
4220 case elfcpp::R_X86_64_GOTOFF64
:
4221 case elfcpp::R_X86_64_GOTPC64
:
4222 case elfcpp::R_X86_64_PLTOFF64
:
4223 case elfcpp::R_X86_64_GOT64
:
4224 case elfcpp::R_X86_64_GOTPCREL64
:
4225 case elfcpp::R_X86_64_GOTPCREL
:
4226 case elfcpp::R_X86_64_GOTPLT64
:
4229 case elfcpp::R_X86_64_32
:
4230 case elfcpp::R_X86_64_32S
:
4231 case elfcpp::R_X86_64_PC32
:
4232 case elfcpp::R_X86_64_PC32_BND
:
4233 case elfcpp::R_X86_64_PLT32
:
4234 case elfcpp::R_X86_64_PLT32_BND
:
4235 case elfcpp::R_X86_64_GOTPC32
:
4236 case elfcpp::R_X86_64_GOT32
:
4239 case elfcpp::R_X86_64_16
:
4240 case elfcpp::R_X86_64_PC16
:
4243 case elfcpp::R_X86_64_8
:
4244 case elfcpp::R_X86_64_PC8
:
4247 case elfcpp::R_X86_64_COPY
:
4248 case elfcpp::R_X86_64_GLOB_DAT
:
4249 case elfcpp::R_X86_64_JUMP_SLOT
:
4250 case elfcpp::R_X86_64_RELATIVE
:
4251 case elfcpp::R_X86_64_IRELATIVE
:
4252 // These are outstanding tls relocs, which are unexpected when linking
4253 case elfcpp::R_X86_64_TPOFF64
:
4254 case elfcpp::R_X86_64_DTPMOD64
:
4255 case elfcpp::R_X86_64_TLSDESC
:
4256 object
->error(_("unexpected reloc %u in object file"), r_type
);
4259 case elfcpp::R_X86_64_SIZE32
:
4260 case elfcpp::R_X86_64_SIZE64
:
4262 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
4267 // Scan the relocs during a relocatable link.
4271 Target_x86_64
<size
>::scan_relocatable_relocs(
4272 Symbol_table
* symtab
,
4274 Sized_relobj_file
<size
, false>* object
,
4275 unsigned int data_shndx
,
4276 unsigned int sh_type
,
4277 const unsigned char* prelocs
,
4279 Output_section
* output_section
,
4280 bool needs_special_offset_handling
,
4281 size_t local_symbol_count
,
4282 const unsigned char* plocal_symbols
,
4283 Relocatable_relocs
* rr
)
4285 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4287 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
4288 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
4290 gold::scan_relocatable_relocs
<size
, false, elfcpp::SHT_RELA
,
4291 Scan_relocatable_relocs
>(
4299 needs_special_offset_handling
,
4305 // Relocate a section during a relocatable link.
4309 Target_x86_64
<size
>::relocate_relocs(
4310 const Relocate_info
<size
, false>* relinfo
,
4311 unsigned int sh_type
,
4312 const unsigned char* prelocs
,
4314 Output_section
* output_section
,
4315 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
4316 const Relocatable_relocs
* rr
,
4317 unsigned char* view
,
4318 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
4319 section_size_type view_size
,
4320 unsigned char* reloc_view
,
4321 section_size_type reloc_view_size
)
4323 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4325 gold::relocate_relocs
<size
, false, elfcpp::SHT_RELA
>(
4330 offset_in_output_section
,
4339 // Return the value to use for a dynamic which requires special
4340 // treatment. This is how we support equality comparisons of function
4341 // pointers across shared library boundaries, as described in the
4342 // processor specific ABI supplement.
4346 Target_x86_64
<size
>::do_dynsym_value(const Symbol
* gsym
) const
4348 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
4349 return this->plt_address_for_global(gsym
);
4352 // Return a string used to fill a code section with nops to take up
4353 // the specified length.
4357 Target_x86_64
<size
>::do_code_fill(section_size_type length
) const
4361 // Build a jmpq instruction to skip over the bytes.
4362 unsigned char jmp
[5];
4364 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
4365 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
4366 + std::string(length
- 5, static_cast<char>(0x90)));
4369 // Nop sequences of various lengths.
4370 const char nop1
[1] = { '\x90' }; // nop
4371 const char nop2
[2] = { '\x66', '\x90' }; // xchg %ax %ax
4372 const char nop3
[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4373 const char nop4
[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4375 const char nop5
[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4377 const char nop6
[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4378 '\x44', '\x00', '\x00' };
4379 const char nop7
[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4380 '\x00', '\x00', '\x00',
4382 const char nop8
[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4383 '\x00', '\x00', '\x00',
4385 const char nop9
[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4386 '\x84', '\x00', '\x00',
4387 '\x00', '\x00', '\x00' };
4388 const char nop10
[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4389 '\x1f', '\x84', '\x00',
4390 '\x00', '\x00', '\x00',
4392 const char nop11
[11] = { '\x66', '\x66', '\x2e', // data16
4393 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4394 '\x00', '\x00', '\x00',
4396 const char nop12
[12] = { '\x66', '\x66', '\x66', // data16; data16
4397 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4398 '\x84', '\x00', '\x00',
4399 '\x00', '\x00', '\x00' };
4400 const char nop13
[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4401 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4402 '\x1f', '\x84', '\x00',
4403 '\x00', '\x00', '\x00',
4405 const char nop14
[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4406 '\x66', '\x66', '\x2e', // data16
4407 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4408 '\x00', '\x00', '\x00',
4410 const char nop15
[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4411 '\x66', '\x66', '\x66', // data16; data16
4412 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4413 '\x84', '\x00', '\x00',
4414 '\x00', '\x00', '\x00' };
4416 const char* nops
[16] = {
4418 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
4419 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
4422 return std::string(nops
[length
], length
);
4425 // Return the addend to use for a target specific relocation. The
4426 // only target specific relocation is R_X86_64_TLSDESC for a local
4427 // symbol. We want to set the addend is the offset of the local
4428 // symbol in the TLS segment.
4432 Target_x86_64
<size
>::do_reloc_addend(void* arg
, unsigned int r_type
,
4435 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
4436 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
4437 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
4438 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
4439 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
4440 gold_assert(psymval
->is_tls_symbol());
4441 // The value of a TLS symbol is the offset in the TLS segment.
4442 return psymval
->value(ti
.object
, 0);
4445 // Return the value to use for the base of a DW_EH_PE_datarel offset
4446 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4447 // assembler can not write out the difference between two labels in
4448 // different sections, so instead of using a pc-relative value they
4449 // use an offset from the GOT.
4453 Target_x86_64
<size
>::do_ehframe_datarel_base() const
4455 gold_assert(this->global_offset_table_
!= NULL
);
4456 Symbol
* sym
= this->global_offset_table_
;
4457 Sized_symbol
<size
>* ssym
= static_cast<Sized_symbol
<size
>*>(sym
);
4458 return ssym
->value();
4461 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4462 // compiled with -fsplit-stack. The function calls non-split-stack
4463 // code. We have to change the function so that it always ensures
4464 // that it has enough stack space to run some random function.
4466 static const unsigned char cmp_insn_32
[] = { 0x64, 0x3b, 0x24, 0x25 };
4467 static const unsigned char lea_r10_insn_32
[] = { 0x44, 0x8d, 0x94, 0x24 };
4468 static const unsigned char lea_r11_insn_32
[] = { 0x44, 0x8d, 0x9c, 0x24 };
4470 static const unsigned char cmp_insn_64
[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
4471 static const unsigned char lea_r10_insn_64
[] = { 0x4c, 0x8d, 0x94, 0x24 };
4472 static const unsigned char lea_r11_insn_64
[] = { 0x4c, 0x8d, 0x9c, 0x24 };
4476 Target_x86_64
<size
>::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
4477 section_offset_type fnoffset
,
4478 section_size_type fnsize
,
4479 unsigned char* view
,
4480 section_size_type view_size
,
4482 std::string
* to
) const
4484 const char* const cmp_insn
= reinterpret_cast<const char*>
4485 (size
== 32 ? cmp_insn_32
: cmp_insn_64
);
4486 const char* const lea_r10_insn
= reinterpret_cast<const char*>
4487 (size
== 32 ? lea_r10_insn_32
: lea_r10_insn_64
);
4488 const char* const lea_r11_insn
= reinterpret_cast<const char*>
4489 (size
== 32 ? lea_r11_insn_32
: lea_r11_insn_64
);
4491 const size_t cmp_insn_len
=
4492 (size
== 32 ? sizeof(cmp_insn_32
) : sizeof(cmp_insn_64
));
4493 const size_t lea_r10_insn_len
=
4494 (size
== 32 ? sizeof(lea_r10_insn_32
) : sizeof(lea_r10_insn_64
));
4495 const size_t lea_r11_insn_len
=
4496 (size
== 32 ? sizeof(lea_r11_insn_32
) : sizeof(lea_r11_insn_64
));
4497 const size_t nop_len
= (size
== 32 ? 7 : 8);
4499 // The function starts with a comparison of the stack pointer and a
4500 // field in the TCB. This is followed by a jump.
4503 if (this->match_view(view
, view_size
, fnoffset
, cmp_insn
, cmp_insn_len
)
4504 && fnsize
> nop_len
+ 1)
4506 // We will call __morestack if the carry flag is set after this
4507 // comparison. We turn the comparison into an stc instruction
4509 view
[fnoffset
] = '\xf9';
4510 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, nop_len
);
4512 // lea NN(%rsp),%r10
4513 // lea NN(%rsp),%r11
4514 else if ((this->match_view(view
, view_size
, fnoffset
,
4515 lea_r10_insn
, lea_r10_insn_len
)
4516 || this->match_view(view
, view_size
, fnoffset
,
4517 lea_r11_insn
, lea_r11_insn_len
))
4520 // This is loading an offset from the stack pointer for a
4521 // comparison. The offset is negative, so we decrease the
4522 // offset by the amount of space we need for the stack. This
4523 // means we will avoid calling __morestack if there happens to
4524 // be plenty of space on the stack already.
4525 unsigned char* pval
= view
+ fnoffset
+ 4;
4526 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
4527 val
-= parameters
->options().split_stack_adjust_size();
4528 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
4532 if (!object
->has_no_split_stack())
4533 object
->error(_("failed to match split-stack sequence at "
4534 "section %u offset %0zx"),
4535 shndx
, static_cast<size_t>(fnoffset
));
4539 // We have to change the function so that it calls
4540 // __morestack_non_split instead of __morestack. The former will
4541 // allocate additional stack space.
4542 *from
= "__morestack";
4543 *to
= "__morestack_non_split";
4546 // The selector for x86_64 object files. Note this is never instantiated
4547 // directly. It's only used in Target_selector_x86_64_nacl, below.
4550 class Target_selector_x86_64
: public Target_selector_freebsd
4553 Target_selector_x86_64()
4554 : Target_selector_freebsd(elfcpp::EM_X86_64
, size
, false,
4556 ? "elf64-x86-64" : "elf32-x86-64"),
4558 ? "elf64-x86-64-freebsd"
4559 : "elf32-x86-64-freebsd"),
4560 (size
== 64 ? "elf_x86_64" : "elf32_x86_64"))
4564 do_instantiate_target()
4565 { return new Target_x86_64
<size
>(); }
4569 // NaCl variant. It uses different PLT contents.
4572 class Output_data_plt_x86_64_nacl
: public Output_data_plt_x86_64
<size
>
4575 Output_data_plt_x86_64_nacl(Layout
* layout
,
4576 Output_data_got
<64, false>* got
,
4577 Output_data_got_plt_x86_64
* got_plt
,
4578 Output_data_space
* got_irelative
)
4579 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4580 got
, got_plt
, got_irelative
)
4583 Output_data_plt_x86_64_nacl(Layout
* layout
,
4584 Output_data_got
<64, false>* got
,
4585 Output_data_got_plt_x86_64
* got_plt
,
4586 Output_data_space
* got_irelative
,
4587 unsigned int plt_count
)
4588 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4589 got
, got_plt
, got_irelative
,
4594 virtual unsigned int
4595 do_get_plt_entry_size() const
4596 { return plt_entry_size
; }
4599 do_add_eh_frame(Layout
* layout
)
4601 layout
->add_eh_frame_for_plt(this,
4602 this->plt_eh_frame_cie
,
4603 this->plt_eh_frame_cie_size
,
4605 plt_eh_frame_fde_size
);
4609 do_fill_first_plt_entry(unsigned char* pov
,
4610 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
4611 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
4613 virtual unsigned int
4614 do_fill_plt_entry(unsigned char* pov
,
4615 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4616 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4617 unsigned int got_offset
,
4618 unsigned int plt_offset
,
4619 unsigned int plt_index
);
4622 do_fill_tlsdesc_entry(unsigned char* pov
,
4623 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4624 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4625 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
4626 unsigned int tlsdesc_got_offset
,
4627 unsigned int plt_offset
);
4630 // The size of an entry in the PLT.
4631 static const int plt_entry_size
= 64;
4633 // The first entry in the PLT.
4634 static const unsigned char first_plt_entry
[plt_entry_size
];
4636 // Other entries in the PLT for an executable.
4637 static const unsigned char plt_entry
[plt_entry_size
];
4639 // The reserved TLSDESC entry in the PLT for an executable.
4640 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
4642 // The .eh_frame unwind information for the PLT.
4643 static const int plt_eh_frame_fde_size
= 32;
4644 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
4648 class Target_x86_64_nacl
: public Target_x86_64
<size
>
4651 Target_x86_64_nacl()
4652 : Target_x86_64
<size
>(&x86_64_nacl_info
)
4655 virtual Output_data_plt_x86_64
<size
>*
4656 do_make_data_plt(Layout
* layout
,
4657 Output_data_got
<64, false>* got
,
4658 Output_data_got_plt_x86_64
* got_plt
,
4659 Output_data_space
* got_irelative
)
4661 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4665 virtual Output_data_plt_x86_64
<size
>*
4666 do_make_data_plt(Layout
* layout
,
4667 Output_data_got
<64, false>* got
,
4668 Output_data_got_plt_x86_64
* got_plt
,
4669 Output_data_space
* got_irelative
,
4670 unsigned int plt_count
)
4672 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4678 do_code_fill(section_size_type length
) const;
4681 static const Target::Target_info x86_64_nacl_info
;
4685 const Target::Target_info Target_x86_64_nacl
<64>::x86_64_nacl_info
=
4688 false, // is_big_endian
4689 elfcpp::EM_X86_64
, // machine_code
4690 false, // has_make_symbol
4691 false, // has_resolve
4692 true, // has_code_fill
4693 true, // is_default_stack_executable
4694 true, // can_icf_inline_merge_sections
4696 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4697 0x20000, // default_text_segment_address
4698 0x10000, // abi_pagesize (overridable by -z max-page-size)
4699 0x10000, // common_pagesize (overridable by -z common-page-size)
4700 true, // isolate_execinstr
4701 0x10000000, // rosegment_gap
4702 elfcpp::SHN_UNDEF
, // small_common_shndx
4703 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
4704 0, // small_common_section_flags
4705 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
4706 NULL
, // attributes_section
4707 NULL
, // attributes_vendor
4708 "_start" // entry_symbol_name
4712 const Target::Target_info Target_x86_64_nacl
<32>::x86_64_nacl_info
=
4715 false, // is_big_endian
4716 elfcpp::EM_X86_64
, // machine_code
4717 false, // has_make_symbol
4718 false, // has_resolve
4719 true, // has_code_fill
4720 true, // is_default_stack_executable
4721 true, // can_icf_inline_merge_sections
4723 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
4724 0x20000, // default_text_segment_address
4725 0x10000, // abi_pagesize (overridable by -z max-page-size)
4726 0x10000, // common_pagesize (overridable by -z common-page-size)
4727 true, // isolate_execinstr
4728 0x10000000, // rosegment_gap
4729 elfcpp::SHN_UNDEF
, // small_common_shndx
4730 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
4731 0, // small_common_section_flags
4732 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
4733 NULL
, // attributes_section
4734 NULL
, // attributes_vendor
4735 "_start" // entry_symbol_name
4738 #define NACLMASK 0xe0 // 32-byte alignment mask.
4740 // The first entry in the PLT.
4744 Output_data_plt_x86_64_nacl
<size
>::first_plt_entry
[plt_entry_size
] =
4746 0xff, 0x35, // pushq contents of memory address
4747 0, 0, 0, 0, // replaced with address of .got + 8
4748 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
4749 0, 0, 0, 0, // replaced with address of .got + 16
4750 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4751 0x4d, 0x01, 0xfb, // add %r15, %r11
4752 0x41, 0xff, 0xe3, // jmpq *%r11
4754 // 9-byte nop sequence to pad out to the next 32-byte boundary.
4755 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
4757 // 32 bytes of nop to pad out to the standard size
4758 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4759 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4760 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4761 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4762 0x66, // excess data32 prefix
4768 Output_data_plt_x86_64_nacl
<size
>::do_fill_first_plt_entry(
4770 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4771 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
4773 memcpy(pov
, first_plt_entry
, plt_entry_size
);
4774 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
4776 - (plt_address
+ 2 + 4)));
4777 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
4779 - (plt_address
+ 9 + 4)));
4782 // Subsequent entries in the PLT.
4786 Output_data_plt_x86_64_nacl
<size
>::plt_entry
[plt_entry_size
] =
4788 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
4789 0, 0, 0, 0, // replaced with address of symbol in .got
4790 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4791 0x4d, 0x01, 0xfb, // add %r15, %r11
4792 0x41, 0xff, 0xe3, // jmpq *%r11
4794 // 15-byte nop sequence to pad out to the next 32-byte boundary.
4795 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4796 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4798 // Lazy GOT entries point here (32-byte aligned).
4799 0x68, // pushq immediate
4800 0, 0, 0, 0, // replaced with index into relocation table
4801 0xe9, // jmp relative
4802 0, 0, 0, 0, // replaced with offset to start of .plt0
4804 // 22 bytes of nop to pad out to the standard size.
4805 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4806 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4807 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
4812 Output_data_plt_x86_64_nacl
<size
>::do_fill_plt_entry(
4814 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4815 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4816 unsigned int got_offset
,
4817 unsigned int plt_offset
,
4818 unsigned int plt_index
)
4820 memcpy(pov
, plt_entry
, plt_entry_size
);
4821 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3,
4822 (got_address
+ got_offset
4823 - (plt_address
+ plt_offset
4826 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 33, plt_index
);
4827 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 38,
4828 - (plt_offset
+ 38 + 4));
4833 // The reserved TLSDESC entry in the PLT.
4837 Output_data_plt_x86_64_nacl
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
4839 0xff, 0x35, // pushq x(%rip)
4840 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
4841 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
4842 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
4843 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4844 0x4d, 0x01, 0xfb, // add %r15, %r11
4845 0x41, 0xff, 0xe3, // jmpq *%r11
4847 // 41 bytes of nop to pad out to the standard size.
4848 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4849 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4850 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4851 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4852 0x66, 0x66, // excess data32 prefixes
4853 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4858 Output_data_plt_x86_64_nacl
<size
>::do_fill_tlsdesc_entry(
4860 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4861 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4862 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
4863 unsigned int tlsdesc_got_offset
,
4864 unsigned int plt_offset
)
4866 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
4867 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
4869 - (plt_address
+ plt_offset
4871 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
4873 + tlsdesc_got_offset
4874 - (plt_address
+ plt_offset
4878 // The .eh_frame unwind information for the PLT.
4882 Output_data_plt_x86_64_nacl
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
4884 0, 0, 0, 0, // Replaced with offset to .plt.
4885 0, 0, 0, 0, // Replaced with size of .plt.
4886 0, // Augmentation size.
4887 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
4888 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
4889 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
4890 elfcpp::DW_CFA_advance_loc
+ 58, // Advance 58 to __PLT__ + 64.
4891 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
4892 13, // Block length.
4893 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
4894 elfcpp::DW_OP_breg16
, 0, // Push %rip.
4895 elfcpp::DW_OP_const1u
, 63, // Push 0x3f.
4896 elfcpp::DW_OP_and
, // & (%rip & 0x3f).
4897 elfcpp::DW_OP_const1u
, 37, // Push 0x25.
4898 elfcpp::DW_OP_ge
, // >= ((%rip & 0x3f) >= 0x25)
4899 elfcpp::DW_OP_lit3
, // Push 3.
4900 elfcpp::DW_OP_shl
, // << (((%rip & 0x3f) >= 0x25) << 3)
4901 elfcpp::DW_OP_plus
, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
4902 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
4906 // Return a string used to fill a code section with nops.
4907 // For NaCl, long NOPs are only valid if they do not cross
4908 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
4911 Target_x86_64_nacl
<size
>::do_code_fill(section_size_type length
) const
4913 return std::string(length
, static_cast<char>(0x90));
4916 // The selector for x86_64-nacl object files.
4919 class Target_selector_x86_64_nacl
4920 : public Target_selector_nacl
<Target_selector_x86_64
<size
>,
4921 Target_x86_64_nacl
<size
> >
4924 Target_selector_x86_64_nacl()
4925 : Target_selector_nacl
<Target_selector_x86_64
<size
>,
4926 Target_x86_64_nacl
<size
> >("x86-64",
4928 ? "elf64-x86-64-nacl"
4929 : "elf32-x86-64-nacl",
4932 : "elf32_x86_64_nacl")
4936 Target_selector_x86_64_nacl
<64> target_selector_x86_64
;
4937 Target_selector_x86_64_nacl
<32> target_selector_x32
;
4939 } // End anonymous namespace.