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 PLT data.
52 // This is an abstract base class that handles most of the linker details
53 // but does not know the actual contents of PLT entries. The derived
54 // classes below fill in those details.
57 class Output_data_plt_x86_64
: public Output_section_data
60 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
62 Output_data_plt_x86_64(Layout
* layout
, uint64_t addralign
,
63 Output_data_got
<64, false>* got
,
64 Output_data_space
* got_plt
,
65 Output_data_space
* got_irelative
)
66 : Output_section_data(addralign
), layout_(layout
), tlsdesc_rel_(NULL
),
67 irelative_rel_(NULL
), got_(got
), got_plt_(got_plt
),
68 got_irelative_(got_irelative
), count_(0), irelative_count_(0),
69 tlsdesc_got_offset_(-1U), free_list_()
70 { this->init(layout
); }
72 Output_data_plt_x86_64(Layout
* layout
, uint64_t plt_entry_size
,
73 Output_data_got
<64, false>* got
,
74 Output_data_space
* got_plt
,
75 Output_data_space
* got_irelative
,
76 unsigned int plt_count
)
77 : Output_section_data((plt_count
+ 1) * plt_entry_size
,
78 plt_entry_size
, false),
79 layout_(layout
), tlsdesc_rel_(NULL
), irelative_rel_(NULL
), got_(got
),
80 got_plt_(got_plt
), got_irelative_(got_irelative
), count_(plt_count
),
81 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
85 // Initialize the free list and reserve the first entry.
86 this->free_list_
.init((plt_count
+ 1) * plt_entry_size
, false);
87 this->free_list_
.remove(0, plt_entry_size
);
90 // Initialize the PLT section.
94 // Add an entry to the PLT.
96 add_entry(Symbol_table
*, Layout
*, Symbol
* gsym
);
98 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
100 add_local_ifunc_entry(Symbol_table
* symtab
, Layout
*,
101 Sized_relobj_file
<size
, false>* relobj
,
102 unsigned int local_sym_index
);
104 // Add the relocation for a PLT entry.
106 add_relocation(Symbol_table
*, Layout
*, Symbol
* gsym
,
107 unsigned int got_offset
);
109 // Add the reserved TLSDESC_PLT entry to the PLT.
111 reserve_tlsdesc_entry(unsigned int got_offset
)
112 { this->tlsdesc_got_offset_
= got_offset
; }
114 // Return true if a TLSDESC_PLT entry has been reserved.
116 has_tlsdesc_entry() const
117 { return this->tlsdesc_got_offset_
!= -1U; }
119 // Return the GOT offset for the reserved TLSDESC_PLT entry.
121 get_tlsdesc_got_offset() const
122 { return this->tlsdesc_got_offset_
; }
124 // Return the offset of the reserved TLSDESC_PLT entry.
126 get_tlsdesc_plt_offset() const
128 return ((this->count_
+ this->irelative_count_
+ 1)
129 * this->get_plt_entry_size());
132 // Return the .rela.plt section data.
135 { return this->rel_
; }
137 // Return where the TLSDESC relocations should go.
139 rela_tlsdesc(Layout
*);
141 // Return where the IRELATIVE relocations should go in the PLT
144 rela_irelative(Symbol_table
*, Layout
*);
146 // Return whether we created a section for IRELATIVE relocations.
148 has_irelative_section() const
149 { return this->irelative_rel_
!= NULL
; }
151 // Return the number of PLT entries.
154 { return this->count_
+ this->irelative_count_
; }
156 // Return the offset of the first non-reserved PLT entry.
158 first_plt_entry_offset()
159 { return this->get_plt_entry_size(); }
161 // Return the size of a PLT entry.
163 get_plt_entry_size() const
164 { return this->do_get_plt_entry_size(); }
166 // Reserve a slot in the PLT for an existing symbol in an incremental update.
168 reserve_slot(unsigned int plt_index
)
170 this->free_list_
.remove((plt_index
+ 1) * this->get_plt_entry_size(),
171 (plt_index
+ 2) * this->get_plt_entry_size());
174 // Return the PLT address to use for a global symbol.
176 address_for_global(const Symbol
*);
178 // Return the PLT address to use for a local symbol.
180 address_for_local(const Relobj
*, unsigned int symndx
);
182 // Add .eh_frame information for the PLT.
184 add_eh_frame(Layout
* layout
)
185 { this->do_add_eh_frame(layout
); }
188 // Fill in the first PLT entry.
190 fill_first_plt_entry(unsigned char* pov
,
191 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
192 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
193 { this->do_fill_first_plt_entry(pov
, got_address
, plt_address
); }
195 // Fill in a normal PLT entry. Returns the offset into the entry that
196 // should be the initial GOT slot value.
198 fill_plt_entry(unsigned char* pov
,
199 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
200 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
201 unsigned int got_offset
,
202 unsigned int plt_offset
,
203 unsigned int plt_index
)
205 return this->do_fill_plt_entry(pov
, got_address
, plt_address
,
206 got_offset
, plt_offset
, plt_index
);
209 // Fill in the reserved TLSDESC PLT entry.
211 fill_tlsdesc_entry(unsigned char* pov
,
212 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
213 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
214 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
215 unsigned int tlsdesc_got_offset
,
216 unsigned int plt_offset
)
218 this->do_fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
219 tlsdesc_got_offset
, plt_offset
);
223 do_get_plt_entry_size() const = 0;
226 do_fill_first_plt_entry(unsigned char* pov
,
227 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
228 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
)
232 do_fill_plt_entry(unsigned char* pov
,
233 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
234 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
235 unsigned int got_offset
,
236 unsigned int plt_offset
,
237 unsigned int plt_index
) = 0;
240 do_fill_tlsdesc_entry(unsigned char* pov
,
241 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
242 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
243 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
244 unsigned int tlsdesc_got_offset
,
245 unsigned int plt_offset
) = 0;
248 do_add_eh_frame(Layout
* layout
) = 0;
251 do_adjust_output_section(Output_section
* os
);
253 // Write to a map file.
255 do_print_to_mapfile(Mapfile
* mapfile
) const
256 { mapfile
->print_output_data(this, _("** PLT")); }
258 // The CIE of the .eh_frame unwind information for the PLT.
259 static const int plt_eh_frame_cie_size
= 16;
260 static const unsigned char plt_eh_frame_cie
[plt_eh_frame_cie_size
];
263 // Set the final size.
265 set_final_data_size();
267 // Write out the PLT data.
269 do_write(Output_file
*);
271 // A pointer to the Layout class, so that we can find the .dynamic
272 // section when we write out the GOT PLT section.
274 // The reloc section.
276 // The TLSDESC relocs, if necessary. These must follow the regular
278 Reloc_section
* tlsdesc_rel_
;
279 // The IRELATIVE relocs, if necessary. These must follow the
280 // regular PLT relocations and the TLSDESC relocations.
281 Reloc_section
* irelative_rel_
;
283 Output_data_got
<64, false>* got_
;
284 // The .got.plt section.
285 Output_data_space
* got_plt_
;
286 // The part of the .got.plt section used for IRELATIVE relocs.
287 Output_data_space
* got_irelative_
;
288 // The number of PLT entries.
290 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
291 // follow the regular PLT entries.
292 unsigned int irelative_count_
;
293 // Offset of the reserved TLSDESC_GOT entry when needed.
294 unsigned int tlsdesc_got_offset_
;
295 // List of available regions within the section, for incremental
297 Free_list free_list_
;
301 class Output_data_plt_x86_64_standard
: public Output_data_plt_x86_64
<size
>
304 Output_data_plt_x86_64_standard(Layout
* layout
,
305 Output_data_got
<64, false>* got
,
306 Output_data_space
* got_plt
,
307 Output_data_space
* got_irelative
)
308 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
309 got
, got_plt
, got_irelative
)
312 Output_data_plt_x86_64_standard(Layout
* layout
,
313 Output_data_got
<64, false>* got
,
314 Output_data_space
* got_plt
,
315 Output_data_space
* got_irelative
,
316 unsigned int plt_count
)
317 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
318 got
, got_plt
, got_irelative
,
324 do_get_plt_entry_size() const
325 { return plt_entry_size
; }
328 do_add_eh_frame(Layout
* layout
)
330 layout
->add_eh_frame_for_plt(this,
331 this->plt_eh_frame_cie
,
332 this->plt_eh_frame_cie_size
,
334 plt_eh_frame_fde_size
);
338 do_fill_first_plt_entry(unsigned char* pov
,
339 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
340 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
343 do_fill_plt_entry(unsigned char* pov
,
344 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
345 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
346 unsigned int got_offset
,
347 unsigned int plt_offset
,
348 unsigned int plt_index
);
351 do_fill_tlsdesc_entry(unsigned char* pov
,
352 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
353 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
354 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
355 unsigned int tlsdesc_got_offset
,
356 unsigned int plt_offset
);
359 // The size of an entry in the PLT.
360 static const int plt_entry_size
= 16;
362 // The first entry in the PLT.
363 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
364 // procedure linkage table for both programs and shared objects."
365 static const unsigned char first_plt_entry
[plt_entry_size
];
367 // Other entries in the PLT for an executable.
368 static const unsigned char plt_entry
[plt_entry_size
];
370 // The reserved TLSDESC entry in the PLT for an executable.
371 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
373 // The .eh_frame unwind information for the PLT.
374 static const int plt_eh_frame_fde_size
= 32;
375 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
378 // The x86_64 target class.
380 // http://www.x86-64.org/documentation/abi.pdf
381 // TLS info comes from
382 // http://people.redhat.com/drepper/tls.pdf
383 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
386 class Target_x86_64
: public Sized_target
<size
, false>
389 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
390 // uses only Elf64_Rela relocation entries with explicit addends."
391 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
393 Target_x86_64(const Target::Target_info
* info
= &x86_64_info
)
394 : Sized_target
<size
, false>(info
),
395 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_irelative_(NULL
),
396 got_tlsdesc_(NULL
), global_offset_table_(NULL
), rela_dyn_(NULL
),
397 rela_irelative_(NULL
), copy_relocs_(elfcpp::R_X86_64_COPY
),
398 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
399 tls_base_symbol_defined_(false)
402 // Hook for a new output section.
404 do_new_output_section(Output_section
*) const;
406 // Scan the relocations to look for symbol adjustments.
408 gc_process_relocs(Symbol_table
* symtab
,
410 Sized_relobj_file
<size
, false>* object
,
411 unsigned int data_shndx
,
412 unsigned int sh_type
,
413 const unsigned char* prelocs
,
415 Output_section
* output_section
,
416 bool needs_special_offset_handling
,
417 size_t local_symbol_count
,
418 const unsigned char* plocal_symbols
);
420 // Scan the relocations to look for symbol adjustments.
422 scan_relocs(Symbol_table
* symtab
,
424 Sized_relobj_file
<size
, false>* object
,
425 unsigned int data_shndx
,
426 unsigned int sh_type
,
427 const unsigned char* prelocs
,
429 Output_section
* output_section
,
430 bool needs_special_offset_handling
,
431 size_t local_symbol_count
,
432 const unsigned char* plocal_symbols
);
434 // Finalize the sections.
436 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
438 // Return the value to use for a dynamic which requires special
441 do_dynsym_value(const Symbol
*) const;
443 // Relocate a section.
445 relocate_section(const Relocate_info
<size
, false>*,
446 unsigned int sh_type
,
447 const unsigned char* prelocs
,
449 Output_section
* output_section
,
450 bool needs_special_offset_handling
,
452 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
453 section_size_type view_size
,
454 const Reloc_symbol_changes
*);
456 // Scan the relocs during a relocatable link.
458 scan_relocatable_relocs(Symbol_table
* symtab
,
460 Sized_relobj_file
<size
, false>* object
,
461 unsigned int data_shndx
,
462 unsigned int sh_type
,
463 const unsigned char* prelocs
,
465 Output_section
* output_section
,
466 bool needs_special_offset_handling
,
467 size_t local_symbol_count
,
468 const unsigned char* plocal_symbols
,
469 Relocatable_relocs
*);
471 // Emit relocations for a section.
474 const Relocate_info
<size
, false>*,
475 unsigned int sh_type
,
476 const unsigned char* prelocs
,
478 Output_section
* output_section
,
479 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
480 const Relocatable_relocs
*,
482 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
483 section_size_type view_size
,
484 unsigned char* reloc_view
,
485 section_size_type reloc_view_size
);
487 // Return a string used to fill a code section with nops.
489 do_code_fill(section_size_type length
) const;
491 // Return whether SYM is defined by the ABI.
493 do_is_defined_by_abi(const Symbol
* sym
) const
494 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
496 // Return the symbol index to use for a target specific relocation.
497 // The only target specific relocation is R_X86_64_TLSDESC for a
498 // local symbol, which is an absolute reloc.
500 do_reloc_symbol_index(void*, unsigned int r_type
) const
502 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
506 // Return the addend to use for a target specific relocation.
508 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
510 // Return the PLT section.
512 do_plt_address_for_global(const Symbol
* gsym
) const
513 { return this->plt_section()->address_for_global(gsym
); }
516 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
517 { return this->plt_section()->address_for_local(relobj
, symndx
); }
519 // This function should be defined in targets that can use relocation
520 // types to determine (implemented in local_reloc_may_be_function_pointer
521 // and global_reloc_may_be_function_pointer)
522 // if a function's pointer is taken. ICF uses this in safe mode to only
523 // fold those functions whose pointer is defintely not taken. For x86_64
524 // pie binaries, safe ICF cannot be done by looking at relocation types.
526 do_can_check_for_function_pointers() const
527 { return !parameters
->options().pie(); }
529 // Return the base for a DW_EH_PE_datarel encoding.
531 do_ehframe_datarel_base() const;
533 // Adjust -fsplit-stack code which calls non-split-stack code.
535 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
536 section_offset_type fnoffset
, section_size_type fnsize
,
537 unsigned char* view
, section_size_type view_size
,
538 std::string
* from
, std::string
* to
) const;
540 // Return the size of the GOT section.
544 gold_assert(this->got_
!= NULL
);
545 return this->got_
->data_size();
548 // Return the number of entries in the GOT.
550 got_entry_count() const
552 if (this->got_
== NULL
)
554 return this->got_size() / 8;
557 // Return the number of entries in the PLT.
559 plt_entry_count() const;
561 // Return the offset of the first non-reserved PLT entry.
563 first_plt_entry_offset() const;
565 // Return the size of each PLT entry.
567 plt_entry_size() const;
569 // Create the GOT section for an incremental update.
570 Output_data_got_base
*
571 init_got_plt_for_update(Symbol_table
* symtab
,
573 unsigned int got_count
,
574 unsigned int plt_count
);
576 // Reserve a GOT entry for a local symbol, and regenerate any
577 // necessary dynamic relocations.
579 reserve_local_got_entry(unsigned int got_index
,
580 Sized_relobj
<size
, false>* obj
,
582 unsigned int got_type
);
584 // Reserve a GOT entry for a global symbol, and regenerate any
585 // necessary dynamic relocations.
587 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
588 unsigned int got_type
);
590 // Register an existing PLT entry for a global symbol.
592 register_global_plt_entry(Symbol_table
*, Layout
*, unsigned int plt_index
,
595 // Force a COPY relocation for a given symbol.
597 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
599 // Apply an incremental relocation.
601 apply_relocation(const Relocate_info
<size
, false>* relinfo
,
602 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
604 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
607 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
608 section_size_type view_size
);
610 // Add a new reloc argument, returning the index in the vector.
612 add_tlsdesc_info(Sized_relobj_file
<size
, false>* object
, unsigned int r_sym
)
614 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
615 return this->tlsdesc_reloc_info_
.size() - 1;
618 Output_data_plt_x86_64
<size
>*
619 make_data_plt(Layout
* layout
,
620 Output_data_got
<64, false>* got
,
621 Output_data_space
* got_plt
,
622 Output_data_space
* got_irelative
)
624 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
627 Output_data_plt_x86_64
<size
>*
628 make_data_plt(Layout
* layout
,
629 Output_data_got
<64, false>* got
,
630 Output_data_space
* got_plt
,
631 Output_data_space
* got_irelative
,
632 unsigned int plt_count
)
634 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
,
638 virtual Output_data_plt_x86_64
<size
>*
639 do_make_data_plt(Layout
* layout
,
640 Output_data_got
<64, false>* got
,
641 Output_data_space
* got_plt
,
642 Output_data_space
* got_irelative
)
644 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
648 virtual Output_data_plt_x86_64
<size
>*
649 do_make_data_plt(Layout
* layout
,
650 Output_data_got
<64, false>* got
,
651 Output_data_space
* got_plt
,
652 Output_data_space
* got_irelative
,
653 unsigned int plt_count
)
655 return new Output_data_plt_x86_64_standard
<size
>(layout
, got
, got_plt
,
661 // The class which scans relocations.
666 : issued_non_pic_error_(false)
670 get_reference_flags(unsigned int r_type
);
673 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
674 Sized_relobj_file
<size
, false>* object
,
675 unsigned int data_shndx
,
676 Output_section
* output_section
,
677 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
678 const elfcpp::Sym
<size
, false>& lsym
,
682 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
683 Sized_relobj_file
<size
, false>* object
,
684 unsigned int data_shndx
,
685 Output_section
* output_section
,
686 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
690 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
691 Target_x86_64
* target
,
692 Sized_relobj_file
<size
, false>* object
,
693 unsigned int data_shndx
,
694 Output_section
* output_section
,
695 const elfcpp::Rela
<size
, false>& reloc
,
697 const elfcpp::Sym
<size
, false>& lsym
);
700 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
701 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
,
711 unsupported_reloc_local(Sized_relobj_file
<size
, false>*,
712 unsigned int r_type
);
715 unsupported_reloc_global(Sized_relobj_file
<size
, false>*,
716 unsigned int r_type
, Symbol
*);
719 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
722 possible_function_pointer_reloc(unsigned int r_type
);
725 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, false>*,
726 unsigned int r_type
);
728 // Whether we have issued an error about a non-PIC compilation.
729 bool issued_non_pic_error_
;
732 // The class which implements relocation.
737 : skip_call_tls_get_addr_(false)
742 if (this->skip_call_tls_get_addr_
)
744 // FIXME: This needs to specify the location somehow.
745 gold_error(_("missing expected TLS relocation"));
749 // Do a relocation. Return false if the caller should not issue
750 // any warnings about this relocation.
752 relocate(const Relocate_info
<size
, false>*, Target_x86_64
*,
754 size_t relnum
, const elfcpp::Rela
<size
, false>&,
755 unsigned int r_type
, const Sized_symbol
<size
>*,
756 const Symbol_value
<size
>*,
757 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
761 // Do a TLS relocation.
763 relocate_tls(const Relocate_info
<size
, false>*, Target_x86_64
*,
764 size_t relnum
, const elfcpp::Rela
<size
, false>&,
765 unsigned int r_type
, const Sized_symbol
<size
>*,
766 const Symbol_value
<size
>*,
767 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
770 // Do a TLS General-Dynamic to Initial-Exec transition.
772 tls_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
773 Output_segment
* tls_segment
,
774 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
775 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
777 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
778 section_size_type view_size
);
780 // Do a TLS General-Dynamic to Local-Exec transition.
782 tls_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
783 Output_segment
* tls_segment
,
784 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
785 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
787 section_size_type view_size
);
789 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
791 tls_desc_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
792 Output_segment
* tls_segment
,
793 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
794 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
796 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
797 section_size_type view_size
);
799 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
801 tls_desc_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
802 Output_segment
* tls_segment
,
803 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
804 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
806 section_size_type view_size
);
808 // Do a TLS Local-Dynamic to Local-Exec transition.
810 tls_ld_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 TLS Initial-Exec to Local-Exec transition.
819 tls_ie_to_le(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 section_size_type view_size
);
826 // This is set if we should skip the next reloc, which should be a
827 // PLT32 reloc against ___tls_get_addr.
828 bool skip_call_tls_get_addr_
;
831 // A class which returns the size required for a relocation type,
832 // used while scanning relocs during a relocatable link.
833 class Relocatable_size_for_reloc
837 get_size_for_reloc(unsigned int, Relobj
*);
840 // Adjust TLS relocation type based on the options and whether this
841 // is a local symbol.
842 static tls::Tls_optimization
843 optimize_tls_reloc(bool is_final
, int r_type
);
845 // Get the GOT section, creating it if necessary.
846 Output_data_got
<64, false>*
847 got_section(Symbol_table
*, Layout
*);
849 // Get the GOT PLT section.
851 got_plt_section() const
853 gold_assert(this->got_plt_
!= NULL
);
854 return this->got_plt_
;
857 // Get the GOT section for TLSDESC entries.
858 Output_data_got
<64, false>*
859 got_tlsdesc_section() const
861 gold_assert(this->got_tlsdesc_
!= NULL
);
862 return this->got_tlsdesc_
;
865 // Create the PLT section.
867 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
869 // Create a PLT entry for a global symbol.
871 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
873 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
875 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
876 Sized_relobj_file
<size
, false>* relobj
,
877 unsigned int local_sym_index
);
879 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
881 define_tls_base_symbol(Symbol_table
*, Layout
*);
883 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
885 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
887 // Create a GOT entry for the TLS module index.
889 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
890 Sized_relobj_file
<size
, false>* object
);
892 // Get the PLT section.
893 Output_data_plt_x86_64
<size
>*
896 gold_assert(this->plt_
!= NULL
);
900 // Get the dynamic reloc section, creating it if necessary.
902 rela_dyn_section(Layout
*);
904 // Get the section to use for TLSDESC relocations.
906 rela_tlsdesc_section(Layout
*) const;
908 // Get the section to use for IRELATIVE relocations.
910 rela_irelative_section(Layout
*);
912 // Add a potential copy relocation.
914 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
915 Sized_relobj_file
<size
, false>* object
,
916 unsigned int shndx
, Output_section
* output_section
,
917 Symbol
* sym
, const elfcpp::Rela
<size
, false>& reloc
)
919 this->copy_relocs_
.copy_reloc(symtab
, layout
,
920 symtab
->get_sized_symbol
<size
>(sym
),
921 object
, shndx
, output_section
,
922 reloc
, this->rela_dyn_section(layout
));
925 // Information about this specific target which we pass to the
926 // general Target structure.
927 static const Target::Target_info x86_64_info
;
929 // The types of GOT entries needed for this platform.
930 // These values are exposed to the ABI in an incremental link.
931 // Do not renumber existing values without changing the version
932 // number of the .gnu_incremental_inputs section.
935 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
936 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
937 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
938 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
941 // This type is used as the argument to the target specific
942 // relocation routines. The only target specific reloc is
943 // R_X86_64_TLSDESC against a local symbol.
946 Tlsdesc_info(Sized_relobj_file
<size
, false>* a_object
, unsigned int a_r_sym
)
947 : object(a_object
), r_sym(a_r_sym
)
950 // The object in which the local symbol is defined.
951 Sized_relobj_file
<size
, false>* object
;
952 // The local symbol index in the object.
957 Output_data_got
<64, false>* got_
;
959 Output_data_plt_x86_64
<size
>* plt_
;
960 // The GOT PLT section.
961 Output_data_space
* got_plt_
;
962 // The GOT section for IRELATIVE relocations.
963 Output_data_space
* got_irelative_
;
964 // The GOT section for TLSDESC relocations.
965 Output_data_got
<64, false>* got_tlsdesc_
;
966 // The _GLOBAL_OFFSET_TABLE_ symbol.
967 Symbol
* global_offset_table_
;
968 // The dynamic reloc section.
969 Reloc_section
* rela_dyn_
;
970 // The section to use for IRELATIVE relocs.
971 Reloc_section
* rela_irelative_
;
972 // Relocs saved to avoid a COPY reloc.
973 Copy_relocs
<elfcpp::SHT_RELA
, size
, false> copy_relocs_
;
974 // Offset of the GOT entry for the TLS module index.
975 unsigned int got_mod_index_offset_
;
976 // We handle R_X86_64_TLSDESC against a local symbol as a target
977 // specific relocation. Here we store the object and local symbol
978 // index for the relocation.
979 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
980 // True if the _TLS_MODULE_BASE_ symbol has been defined.
981 bool tls_base_symbol_defined_
;
985 const Target::Target_info Target_x86_64
<64>::x86_64_info
=
988 false, // is_big_endian
989 elfcpp::EM_X86_64
, // machine_code
990 false, // has_make_symbol
991 false, // has_resolve
992 true, // has_code_fill
993 true, // is_default_stack_executable
994 true, // can_icf_inline_merge_sections
996 "/lib/ld64.so.1", // program interpreter
997 0x400000, // default_text_segment_address
998 0x1000, // abi_pagesize (overridable by -z max-page-size)
999 0x1000, // common_pagesize (overridable by -z common-page-size)
1000 false, // isolate_execinstr
1002 elfcpp::SHN_UNDEF
, // small_common_shndx
1003 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1004 0, // small_common_section_flags
1005 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1006 NULL
, // attributes_section
1007 NULL
, // attributes_vendor
1008 "_start" // entry_symbol_name
1012 const Target::Target_info Target_x86_64
<32>::x86_64_info
=
1015 false, // is_big_endian
1016 elfcpp::EM_X86_64
, // machine_code
1017 false, // has_make_symbol
1018 false, // has_resolve
1019 true, // has_code_fill
1020 true, // is_default_stack_executable
1021 true, // can_icf_inline_merge_sections
1023 "/libx32/ldx32.so.1", // program interpreter
1024 0x400000, // default_text_segment_address
1025 0x1000, // abi_pagesize (overridable by -z max-page-size)
1026 0x1000, // common_pagesize (overridable by -z common-page-size)
1027 false, // isolate_execinstr
1029 elfcpp::SHN_UNDEF
, // small_common_shndx
1030 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1031 0, // small_common_section_flags
1032 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1033 NULL
, // attributes_section
1034 NULL
, // attributes_vendor
1035 "_start" // entry_symbol_name
1038 // This is called when a new output section is created. This is where
1039 // we handle the SHF_X86_64_LARGE.
1043 Target_x86_64
<size
>::do_new_output_section(Output_section
* os
) const
1045 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
1046 os
->set_is_large_section();
1049 // Get the GOT section, creating it if necessary.
1052 Output_data_got
<64, false>*
1053 Target_x86_64
<size
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
1055 if (this->got_
== NULL
)
1057 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1059 // When using -z now, we can treat .got.plt as a relro section.
1060 // Without -z now, it is modified after program startup by lazy
1062 bool is_got_plt_relro
= parameters
->options().now();
1063 Output_section_order got_order
= (is_got_plt_relro
1065 : ORDER_RELRO_LAST
);
1066 Output_section_order got_plt_order
= (is_got_plt_relro
1068 : ORDER_NON_RELRO_FIRST
);
1070 this->got_
= new Output_data_got
<64, false>();
1072 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1074 | elfcpp::SHF_WRITE
),
1075 this->got_
, got_order
, true);
1077 this->got_plt_
= new Output_data_space(8, "** GOT PLT");
1078 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1080 | elfcpp::SHF_WRITE
),
1081 this->got_plt_
, got_plt_order
,
1084 // The first three entries are reserved.
1085 this->got_plt_
->set_current_data_size(3 * 8);
1087 if (!is_got_plt_relro
)
1089 // Those bytes can go into the relro segment.
1090 layout
->increase_relro(3 * 8);
1093 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1094 this->global_offset_table_
=
1095 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1096 Symbol_table::PREDEFINED
,
1098 0, 0, elfcpp::STT_OBJECT
,
1100 elfcpp::STV_HIDDEN
, 0,
1103 // If there are any IRELATIVE relocations, they get GOT entries
1104 // in .got.plt after the jump slot entries.
1105 this->got_irelative_
= new Output_data_space(8, "** GOT IRELATIVE PLT");
1106 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1108 | elfcpp::SHF_WRITE
),
1109 this->got_irelative_
,
1110 got_plt_order
, is_got_plt_relro
);
1112 // If there are any TLSDESC relocations, they get GOT entries in
1113 // .got.plt after the jump slot and IRELATIVE entries.
1114 this->got_tlsdesc_
= new Output_data_got
<64, false>();
1115 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1117 | elfcpp::SHF_WRITE
),
1119 got_plt_order
, is_got_plt_relro
);
1125 // Get the dynamic reloc section, creating it if necessary.
1128 typename Target_x86_64
<size
>::Reloc_section
*
1129 Target_x86_64
<size
>::rela_dyn_section(Layout
* layout
)
1131 if (this->rela_dyn_
== NULL
)
1133 gold_assert(layout
!= NULL
);
1134 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1135 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1136 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1137 ORDER_DYNAMIC_RELOCS
, false);
1139 return this->rela_dyn_
;
1142 // Get the section to use for IRELATIVE relocs, creating it if
1143 // necessary. These go in .rela.dyn, but only after all other dynamic
1144 // relocations. They need to follow the other dynamic relocations so
1145 // that they can refer to global variables initialized by those
1149 typename Target_x86_64
<size
>::Reloc_section
*
1150 Target_x86_64
<size
>::rela_irelative_section(Layout
* layout
)
1152 if (this->rela_irelative_
== NULL
)
1154 // Make sure we have already created the dynamic reloc section.
1155 this->rela_dyn_section(layout
);
1156 this->rela_irelative_
= new Reloc_section(false);
1157 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1158 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
1159 ORDER_DYNAMIC_RELOCS
, false);
1160 gold_assert(this->rela_dyn_
->output_section()
1161 == this->rela_irelative_
->output_section());
1163 return this->rela_irelative_
;
1166 // Initialize the PLT section.
1170 Output_data_plt_x86_64
<size
>::init(Layout
* layout
)
1172 this->rel_
= new Reloc_section(false);
1173 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1174 elfcpp::SHF_ALLOC
, this->rel_
,
1175 ORDER_DYNAMIC_PLT_RELOCS
, false);
1180 Output_data_plt_x86_64
<size
>::do_adjust_output_section(Output_section
* os
)
1182 os
->set_entsize(this->get_plt_entry_size());
1185 // Add an entry to the PLT.
1189 Output_data_plt_x86_64
<size
>::add_entry(Symbol_table
* symtab
, Layout
* layout
,
1192 gold_assert(!gsym
->has_plt_offset());
1194 unsigned int plt_index
;
1196 section_offset_type got_offset
;
1198 unsigned int* pcount
;
1199 unsigned int offset
;
1200 unsigned int reserved
;
1201 Output_data_space
* got
;
1202 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1203 && gsym
->can_use_relative_reloc(false))
1205 pcount
= &this->irelative_count_
;
1208 got
= this->got_irelative_
;
1212 pcount
= &this->count_
;
1215 got
= this->got_plt_
;
1218 if (!this->is_data_size_valid())
1220 // Note that when setting the PLT offset for a non-IRELATIVE
1221 // entry we skip the initial reserved PLT entry.
1222 plt_index
= *pcount
+ offset
;
1223 plt_offset
= plt_index
* this->get_plt_entry_size();
1227 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1228 gold_assert(got_offset
== got
->current_data_size());
1230 // Every PLT entry needs a GOT entry which points back to the PLT
1231 // entry (this will be changed by the dynamic linker, normally
1232 // lazily when the function is called).
1233 got
->set_current_data_size(got_offset
+ 8);
1237 // FIXME: This is probably not correct for IRELATIVE relocs.
1239 // For incremental updates, find an available slot.
1240 plt_offset
= this->free_list_
.allocate(this->get_plt_entry_size(),
1241 this->get_plt_entry_size(), 0);
1242 if (plt_offset
== -1)
1243 gold_fallback(_("out of patch space (PLT);"
1244 " relink with --incremental-full"));
1246 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1247 // can be calculated from the PLT index, adjusting for the three
1248 // reserved entries at the beginning of the GOT.
1249 plt_index
= plt_offset
/ this->get_plt_entry_size() - 1;
1250 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1253 gsym
->set_plt_offset(plt_offset
);
1255 // Every PLT entry needs a reloc.
1256 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
1258 // Note that we don't need to save the symbol. The contents of the
1259 // PLT are independent of which symbols are used. The symbols only
1260 // appear in the relocations.
1263 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1268 Output_data_plt_x86_64
<size
>::add_local_ifunc_entry(
1269 Symbol_table
* symtab
,
1271 Sized_relobj_file
<size
, false>* relobj
,
1272 unsigned int local_sym_index
)
1274 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
1275 ++this->irelative_count_
;
1277 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
1279 // Every PLT entry needs a GOT entry which points back to the PLT
1281 this->got_irelative_
->set_current_data_size(got_offset
+ 8);
1283 // Every PLT entry needs a reloc.
1284 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1285 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
1286 elfcpp::R_X86_64_IRELATIVE
,
1287 this->got_irelative_
, got_offset
, 0);
1292 // Add the relocation for a PLT entry.
1296 Output_data_plt_x86_64
<size
>::add_relocation(Symbol_table
* symtab
,
1299 unsigned int got_offset
)
1301 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1302 && gsym
->can_use_relative_reloc(false))
1304 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1305 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
1306 this->got_irelative_
, got_offset
, 0);
1310 gsym
->set_needs_dynsym_entry();
1311 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
1316 // Return where the TLSDESC relocations should go, creating it if
1317 // necessary. These follow the JUMP_SLOT relocations.
1320 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1321 Output_data_plt_x86_64
<size
>::rela_tlsdesc(Layout
* layout
)
1323 if (this->tlsdesc_rel_
== NULL
)
1325 this->tlsdesc_rel_
= new Reloc_section(false);
1326 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1327 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
1328 ORDER_DYNAMIC_PLT_RELOCS
, false);
1329 gold_assert(this->tlsdesc_rel_
->output_section()
1330 == this->rel_
->output_section());
1332 return this->tlsdesc_rel_
;
1335 // Return where the IRELATIVE relocations should go in the PLT. These
1336 // follow the JUMP_SLOT and the TLSDESC relocations.
1339 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1340 Output_data_plt_x86_64
<size
>::rela_irelative(Symbol_table
* symtab
,
1343 if (this->irelative_rel_
== NULL
)
1345 // Make sure we have a place for the TLSDESC relocations, in
1346 // case we see any later on.
1347 this->rela_tlsdesc(layout
);
1348 this->irelative_rel_
= new Reloc_section(false);
1349 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1350 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
1351 ORDER_DYNAMIC_PLT_RELOCS
, false);
1352 gold_assert(this->irelative_rel_
->output_section()
1353 == this->rel_
->output_section());
1355 if (parameters
->doing_static_link())
1357 // A statically linked executable will only have a .rela.plt
1358 // section to hold R_X86_64_IRELATIVE relocs for
1359 // STT_GNU_IFUNC symbols. The library will use these
1360 // symbols to locate the IRELATIVE relocs at program startup
1362 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
1363 Symbol_table::PREDEFINED
,
1364 this->irelative_rel_
, 0, 0,
1365 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1366 elfcpp::STV_HIDDEN
, 0, false, true);
1367 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
1368 Symbol_table::PREDEFINED
,
1369 this->irelative_rel_
, 0, 0,
1370 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1371 elfcpp::STV_HIDDEN
, 0, true, true);
1374 return this->irelative_rel_
;
1377 // Return the PLT address to use for a global symbol.
1381 Output_data_plt_x86_64
<size
>::address_for_global(const Symbol
* gsym
)
1383 uint64_t offset
= 0;
1384 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1385 && gsym
->can_use_relative_reloc(false))
1386 offset
= (this->count_
+ 1) * this->get_plt_entry_size();
1387 return this->address() + offset
+ gsym
->plt_offset();
1390 // Return the PLT address to use for a local symbol. These are always
1391 // IRELATIVE relocs.
1395 Output_data_plt_x86_64
<size
>::address_for_local(const Relobj
* object
,
1398 return (this->address()
1399 + (this->count_
+ 1) * this->get_plt_entry_size()
1400 + object
->local_plt_offset(r_sym
));
1403 // Set the final size.
1406 Output_data_plt_x86_64
<size
>::set_final_data_size()
1408 unsigned int count
= this->count_
+ this->irelative_count_
;
1409 if (this->has_tlsdesc_entry())
1411 this->set_data_size((count
+ 1) * this->get_plt_entry_size());
1414 // The first entry in the PLT for an executable.
1418 Output_data_plt_x86_64_standard
<size
>::first_plt_entry
[plt_entry_size
] =
1420 // From AMD64 ABI Draft 0.98, page 76
1421 0xff, 0x35, // pushq contents of memory address
1422 0, 0, 0, 0, // replaced with address of .got + 8
1423 0xff, 0x25, // jmp indirect
1424 0, 0, 0, 0, // replaced with address of .got + 16
1425 0x90, 0x90, 0x90, 0x90 // noop (x4)
1430 Output_data_plt_x86_64_standard
<size
>::do_fill_first_plt_entry(
1432 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1433 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
1435 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1436 // We do a jmp relative to the PC at the end of this instruction.
1437 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1439 - (plt_address
+ 6)));
1440 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1442 - (plt_address
+ 12)));
1445 // Subsequent entries in the PLT for an executable.
1449 Output_data_plt_x86_64_standard
<size
>::plt_entry
[plt_entry_size
] =
1451 // From AMD64 ABI Draft 0.98, page 76
1452 0xff, 0x25, // jmpq indirect
1453 0, 0, 0, 0, // replaced with address of symbol in .got
1454 0x68, // pushq immediate
1455 0, 0, 0, 0, // replaced with offset into relocation table
1456 0xe9, // jmpq relative
1457 0, 0, 0, 0 // replaced with offset to start of .plt
1462 Output_data_plt_x86_64_standard
<size
>::do_fill_plt_entry(
1464 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1465 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1466 unsigned int got_offset
,
1467 unsigned int plt_offset
,
1468 unsigned int plt_index
)
1470 memcpy(pov
, plt_entry
, plt_entry_size
);
1471 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1472 (got_address
+ got_offset
1473 - (plt_address
+ plt_offset
1476 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
1477 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1478 - (plt_offset
+ plt_entry_size
));
1483 // The reserved TLSDESC entry in the PLT for an executable.
1487 Output_data_plt_x86_64_standard
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
1489 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1490 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1491 0xff, 0x35, // pushq x(%rip)
1492 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1493 0xff, 0x25, // jmpq *y(%rip)
1494 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
1501 Output_data_plt_x86_64_standard
<size
>::do_fill_tlsdesc_entry(
1503 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1504 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1505 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
1506 unsigned int tlsdesc_got_offset
,
1507 unsigned int plt_offset
)
1509 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
1510 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1512 - (plt_address
+ plt_offset
1514 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
1516 + tlsdesc_got_offset
1517 - (plt_address
+ plt_offset
1521 // The .eh_frame unwind information for the PLT.
1525 Output_data_plt_x86_64
<size
>::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
1528 'z', // Augmentation: augmentation size included.
1529 'R', // Augmentation: FDE encoding included.
1530 '\0', // End of augmentation string.
1531 1, // Code alignment factor.
1532 0x78, // Data alignment factor.
1533 16, // Return address column.
1534 1, // Augmentation size.
1535 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
1536 | elfcpp::DW_EH_PE_sdata4
),
1537 elfcpp::DW_CFA_def_cfa
, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
1538 elfcpp::DW_CFA_offset
+ 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
1539 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
1545 Output_data_plt_x86_64_standard
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
1547 0, 0, 0, 0, // Replaced with offset to .plt.
1548 0, 0, 0, 0, // Replaced with size of .plt.
1549 0, // Augmentation size.
1550 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
1551 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
1552 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
1553 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
1554 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
1555 11, // Block length.
1556 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
1557 elfcpp::DW_OP_breg16
, 0, // Push %rip.
1558 elfcpp::DW_OP_lit15
, // Push 0xf.
1559 elfcpp::DW_OP_and
, // & (%rip & 0xf).
1560 elfcpp::DW_OP_lit11
, // Push 0xb.
1561 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 0xb)
1562 elfcpp::DW_OP_lit3
, // Push 3.
1563 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 0xb) << 3)
1564 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
1565 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
1571 // Write out the PLT. This uses the hand-coded instructions above,
1572 // and adjusts them as needed. This is specified by the AMD64 ABI.
1576 Output_data_plt_x86_64
<size
>::do_write(Output_file
* of
)
1578 const off_t offset
= this->offset();
1579 const section_size_type oview_size
=
1580 convert_to_section_size_type(this->data_size());
1581 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1583 const off_t got_file_offset
= this->got_plt_
->offset();
1584 gold_assert(parameters
->incremental_update()
1585 || (got_file_offset
+ this->got_plt_
->data_size()
1586 == this->got_irelative_
->offset()));
1587 const section_size_type got_size
=
1588 convert_to_section_size_type(this->got_plt_
->data_size()
1589 + this->got_irelative_
->data_size());
1590 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1593 unsigned char* pov
= oview
;
1595 // The base address of the .plt section.
1596 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
1597 // The base address of the .got section.
1598 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
= this->got_
->address();
1599 // The base address of the PLT portion of the .got section,
1600 // which is where the GOT pointer will point, and where the
1601 // three reserved GOT entries are located.
1602 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
1603 = this->got_plt_
->address();
1605 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
1606 pov
+= this->get_plt_entry_size();
1608 unsigned char* got_pov
= got_view
;
1610 // The first entry in the GOT is the address of the .dynamic section
1611 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1612 // We saved space for them when we created the section in
1613 // Target_x86_64::got_section.
1614 Output_section
* dynamic
= this->layout_
->dynamic_section();
1615 uint32_t dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
1616 elfcpp::Swap
<64, false>::writeval(got_pov
, dynamic_addr
);
1618 memset(got_pov
, 0, 16);
1621 unsigned int plt_offset
= this->get_plt_entry_size();
1622 unsigned int got_offset
= 24;
1623 const unsigned int count
= this->count_
+ this->irelative_count_
;
1624 for (unsigned int plt_index
= 0;
1627 pov
+= this->get_plt_entry_size(),
1629 plt_offset
+= this->get_plt_entry_size(),
1632 // Set and adjust the PLT entry itself.
1633 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
1634 got_address
, plt_address
,
1635 got_offset
, plt_offset
,
1638 // Set the entry in the GOT.
1639 elfcpp::Swap
<64, false>::writeval(got_pov
,
1640 plt_address
+ plt_offset
+ lazy_offset
);
1643 if (this->has_tlsdesc_entry())
1645 // Set and adjust the reserved TLSDESC PLT entry.
1646 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
1647 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
1648 tlsdesc_got_offset
, plt_offset
);
1649 pov
+= this->get_plt_entry_size();
1652 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1653 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1655 of
->write_output_view(offset
, oview_size
, oview
);
1656 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1659 // Create the PLT section.
1663 Target_x86_64
<size
>::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1665 if (this->plt_
== NULL
)
1667 // Create the GOT sections first.
1668 this->got_section(symtab
, layout
);
1670 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
1671 this->got_irelative_
);
1673 // Add unwind information if requested.
1674 if (parameters
->options().ld_generated_unwind_info())
1675 this->plt_
->add_eh_frame(layout
);
1677 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1679 | elfcpp::SHF_EXECINSTR
),
1680 this->plt_
, ORDER_PLT
, false);
1682 // Make the sh_info field of .rela.plt point to .plt.
1683 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1684 rela_plt_os
->set_info_section(this->plt_
->output_section());
1688 // Return the section for TLSDESC relocations.
1691 typename Target_x86_64
<size
>::Reloc_section
*
1692 Target_x86_64
<size
>::rela_tlsdesc_section(Layout
* layout
) const
1694 return this->plt_section()->rela_tlsdesc(layout
);
1697 // Create a PLT entry for a global symbol.
1701 Target_x86_64
<size
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1704 if (gsym
->has_plt_offset())
1707 if (this->plt_
== NULL
)
1708 this->make_plt_section(symtab
, layout
);
1710 this->plt_
->add_entry(symtab
, layout
, gsym
);
1713 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1717 Target_x86_64
<size
>::make_local_ifunc_plt_entry(
1718 Symbol_table
* symtab
, Layout
* layout
,
1719 Sized_relobj_file
<size
, false>* relobj
,
1720 unsigned int local_sym_index
)
1722 if (relobj
->local_has_plt_offset(local_sym_index
))
1724 if (this->plt_
== NULL
)
1725 this->make_plt_section(symtab
, layout
);
1726 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
1729 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1732 // Return the number of entries in the PLT.
1736 Target_x86_64
<size
>::plt_entry_count() const
1738 if (this->plt_
== NULL
)
1740 return this->plt_
->entry_count();
1743 // Return the offset of the first non-reserved PLT entry.
1747 Target_x86_64
<size
>::first_plt_entry_offset() const
1749 return this->plt_
->first_plt_entry_offset();
1752 // Return the size of each PLT entry.
1756 Target_x86_64
<size
>::plt_entry_size() const
1758 return this->plt_
->get_plt_entry_size();
1761 // Create the GOT and PLT sections for an incremental update.
1764 Output_data_got_base
*
1765 Target_x86_64
<size
>::init_got_plt_for_update(Symbol_table
* symtab
,
1767 unsigned int got_count
,
1768 unsigned int plt_count
)
1770 gold_assert(this->got_
== NULL
);
1772 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
1773 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1775 | elfcpp::SHF_WRITE
),
1776 this->got_
, ORDER_RELRO_LAST
,
1779 // Add the three reserved entries.
1780 this->got_plt_
= new Output_data_space((plt_count
+ 3) * 8, 8, "** GOT PLT");
1781 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1783 | elfcpp::SHF_WRITE
),
1784 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
1787 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1788 this->global_offset_table_
=
1789 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1790 Symbol_table::PREDEFINED
,
1792 0, 0, elfcpp::STT_OBJECT
,
1794 elfcpp::STV_HIDDEN
, 0,
1797 // If there are any TLSDESC relocations, they get GOT entries in
1798 // .got.plt after the jump slot entries.
1799 // FIXME: Get the count for TLSDESC entries.
1800 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
1801 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1802 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1804 ORDER_NON_RELRO_FIRST
, false);
1806 // If there are any IRELATIVE relocations, they get GOT entries in
1807 // .got.plt after the jump slot and TLSDESC entries.
1808 this->got_irelative_
= new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
1809 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1810 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
1811 this->got_irelative_
,
1812 ORDER_NON_RELRO_FIRST
, false);
1814 // Create the PLT section.
1815 this->plt_
= this->make_data_plt(layout
, this->got_
,
1817 this->got_irelative_
,
1820 // Add unwind information if requested.
1821 if (parameters
->options().ld_generated_unwind_info())
1822 this->plt_
->add_eh_frame(layout
);
1824 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1825 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
1826 this->plt_
, ORDER_PLT
, false);
1828 // Make the sh_info field of .rela.plt point to .plt.
1829 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
1830 rela_plt_os
->set_info_section(this->plt_
->output_section());
1832 // Create the rela_dyn section.
1833 this->rela_dyn_section(layout
);
1838 // Reserve a GOT entry for a local symbol, and regenerate any
1839 // necessary dynamic relocations.
1843 Target_x86_64
<size
>::reserve_local_got_entry(
1844 unsigned int got_index
,
1845 Sized_relobj
<size
, false>* obj
,
1847 unsigned int got_type
)
1849 unsigned int got_offset
= got_index
* 8;
1850 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
1852 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
1855 case GOT_TYPE_STANDARD
:
1856 if (parameters
->options().output_is_position_independent())
1857 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
1858 this->got_
, got_offset
, 0, false);
1860 case GOT_TYPE_TLS_OFFSET
:
1861 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
1862 this->got_
, got_offset
, 0);
1864 case GOT_TYPE_TLS_PAIR
:
1865 this->got_
->reserve_slot(got_index
+ 1);
1866 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
1867 this->got_
, got_offset
, 0);
1869 case GOT_TYPE_TLS_DESC
:
1870 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1871 // this->got_->reserve_slot(got_index + 1);
1872 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1873 // this->got_, got_offset, 0);
1880 // Reserve a GOT entry for a global symbol, and regenerate any
1881 // necessary dynamic relocations.
1885 Target_x86_64
<size
>::reserve_global_got_entry(unsigned int got_index
,
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_global(got_index
, gsym
, got_type
);
1895 case GOT_TYPE_STANDARD
:
1896 if (!gsym
->final_value_is_known())
1898 if (gsym
->is_from_dynobj()
1899 || gsym
->is_undefined()
1900 || gsym
->is_preemptible()
1901 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1902 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
1903 this->got_
, got_offset
, 0);
1905 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
1906 this->got_
, got_offset
, 0, false);
1909 case GOT_TYPE_TLS_OFFSET
:
1910 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
1911 this->got_
, got_offset
, 0, false);
1913 case GOT_TYPE_TLS_PAIR
:
1914 this->got_
->reserve_slot(got_index
+ 1);
1915 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
1916 this->got_
, got_offset
, 0, false);
1917 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
1918 this->got_
, got_offset
+ 8, 0, false);
1920 case GOT_TYPE_TLS_DESC
:
1921 this->got_
->reserve_slot(got_index
+ 1);
1922 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
1923 this->got_
, got_offset
, 0, false);
1930 // Register an existing PLT entry for a global symbol.
1934 Target_x86_64
<size
>::register_global_plt_entry(Symbol_table
* symtab
,
1936 unsigned int plt_index
,
1939 gold_assert(this->plt_
!= NULL
);
1940 gold_assert(!gsym
->has_plt_offset());
1942 this->plt_
->reserve_slot(plt_index
);
1944 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
1946 unsigned int got_offset
= (plt_index
+ 3) * 8;
1947 this->plt_
->add_relocation(symtab
, layout
, gsym
, got_offset
);
1950 // Force a COPY relocation for a given symbol.
1954 Target_x86_64
<size
>::emit_copy_reloc(
1955 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
1957 this->copy_relocs_
.emit_copy_reloc(symtab
,
1958 symtab
->get_sized_symbol
<size
>(sym
),
1961 this->rela_dyn_section(NULL
));
1964 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1968 Target_x86_64
<size
>::define_tls_base_symbol(Symbol_table
* symtab
,
1971 if (this->tls_base_symbol_defined_
)
1974 Output_segment
* tls_segment
= layout
->tls_segment();
1975 if (tls_segment
!= NULL
)
1977 bool is_exec
= parameters
->options().output_is_executable();
1978 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1979 Symbol_table::PREDEFINED
,
1983 elfcpp::STV_HIDDEN
, 0,
1985 ? Symbol::SEGMENT_END
1986 : Symbol::SEGMENT_START
),
1989 this->tls_base_symbol_defined_
= true;
1992 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1996 Target_x86_64
<size
>::reserve_tlsdesc_entries(Symbol_table
* symtab
,
1999 if (this->plt_
== NULL
)
2000 this->make_plt_section(symtab
, layout
);
2002 if (!this->plt_
->has_tlsdesc_entry())
2004 // Allocate the TLSDESC_GOT entry.
2005 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2006 unsigned int got_offset
= got
->add_constant(0);
2008 // Allocate the TLSDESC_PLT entry.
2009 this->plt_
->reserve_tlsdesc_entry(got_offset
);
2013 // Create a GOT entry for the TLS module index.
2017 Target_x86_64
<size
>::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
2018 Sized_relobj_file
<size
, false>* object
)
2020 if (this->got_mod_index_offset_
== -1U)
2022 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
2023 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
2024 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
2025 unsigned int got_offset
= got
->add_constant(0);
2026 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
2028 got
->add_constant(0);
2029 this->got_mod_index_offset_
= got_offset
;
2031 return this->got_mod_index_offset_
;
2034 // Optimize the TLS relocation type based on what we know about the
2035 // symbol. IS_FINAL is true if the final address of this symbol is
2036 // known at link time.
2039 tls::Tls_optimization
2040 Target_x86_64
<size
>::optimize_tls_reloc(bool is_final
, int r_type
)
2042 // If we are generating a shared library, then we can't do anything
2044 if (parameters
->options().shared())
2045 return tls::TLSOPT_NONE
;
2049 case elfcpp::R_X86_64_TLSGD
:
2050 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2051 case elfcpp::R_X86_64_TLSDESC_CALL
:
2052 // These are General-Dynamic which permits fully general TLS
2053 // access. Since we know that we are generating an executable,
2054 // we can convert this to Initial-Exec. If we also know that
2055 // this is a local symbol, we can further switch to Local-Exec.
2057 return tls::TLSOPT_TO_LE
;
2058 return tls::TLSOPT_TO_IE
;
2060 case elfcpp::R_X86_64_TLSLD
:
2061 // This is Local-Dynamic, which refers to a local symbol in the
2062 // dynamic TLS block. Since we know that we generating an
2063 // executable, we can switch to Local-Exec.
2064 return tls::TLSOPT_TO_LE
;
2066 case elfcpp::R_X86_64_DTPOFF32
:
2067 case elfcpp::R_X86_64_DTPOFF64
:
2068 // Another Local-Dynamic reloc.
2069 return tls::TLSOPT_TO_LE
;
2071 case elfcpp::R_X86_64_GOTTPOFF
:
2072 // These are Initial-Exec relocs which get the thread offset
2073 // from the GOT. If we know that we are linking against the
2074 // local symbol, we can switch to Local-Exec, which links the
2075 // thread offset into the instruction.
2077 return tls::TLSOPT_TO_LE
;
2078 return tls::TLSOPT_NONE
;
2080 case elfcpp::R_X86_64_TPOFF32
:
2081 // When we already have Local-Exec, there is nothing further we
2083 return tls::TLSOPT_NONE
;
2090 // Get the Reference_flags for a particular relocation.
2094 Target_x86_64
<size
>::Scan::get_reference_flags(unsigned int r_type
)
2098 case elfcpp::R_X86_64_NONE
:
2099 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2100 case elfcpp::R_X86_64_GNU_VTENTRY
:
2101 case elfcpp::R_X86_64_GOTPC32
:
2102 case elfcpp::R_X86_64_GOTPC64
:
2103 // No symbol reference.
2106 case elfcpp::R_X86_64_64
:
2107 case elfcpp::R_X86_64_32
:
2108 case elfcpp::R_X86_64_32S
:
2109 case elfcpp::R_X86_64_16
:
2110 case elfcpp::R_X86_64_8
:
2111 return Symbol::ABSOLUTE_REF
;
2113 case elfcpp::R_X86_64_PC64
:
2114 case elfcpp::R_X86_64_PC32
:
2115 case elfcpp::R_X86_64_PC32_BND
:
2116 case elfcpp::R_X86_64_PC16
:
2117 case elfcpp::R_X86_64_PC8
:
2118 case elfcpp::R_X86_64_GOTOFF64
:
2119 return Symbol::RELATIVE_REF
;
2121 case elfcpp::R_X86_64_PLT32
:
2122 case elfcpp::R_X86_64_PLT32_BND
:
2123 case elfcpp::R_X86_64_PLTOFF64
:
2124 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
2126 case elfcpp::R_X86_64_GOT64
:
2127 case elfcpp::R_X86_64_GOT32
:
2128 case elfcpp::R_X86_64_GOTPCREL64
:
2129 case elfcpp::R_X86_64_GOTPCREL
:
2130 case elfcpp::R_X86_64_GOTPLT64
:
2132 return Symbol::ABSOLUTE_REF
;
2134 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2135 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2136 case elfcpp::R_X86_64_TLSDESC_CALL
:
2137 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2138 case elfcpp::R_X86_64_DTPOFF32
:
2139 case elfcpp::R_X86_64_DTPOFF64
:
2140 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2141 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2142 return Symbol::TLS_REF
;
2144 case elfcpp::R_X86_64_COPY
:
2145 case elfcpp::R_X86_64_GLOB_DAT
:
2146 case elfcpp::R_X86_64_JUMP_SLOT
:
2147 case elfcpp::R_X86_64_RELATIVE
:
2148 case elfcpp::R_X86_64_IRELATIVE
:
2149 case elfcpp::R_X86_64_TPOFF64
:
2150 case elfcpp::R_X86_64_DTPMOD64
:
2151 case elfcpp::R_X86_64_TLSDESC
:
2152 case elfcpp::R_X86_64_SIZE32
:
2153 case elfcpp::R_X86_64_SIZE64
:
2155 // Not expected. We will give an error later.
2160 // Report an unsupported relocation against a local symbol.
2164 Target_x86_64
<size
>::Scan::unsupported_reloc_local(
2165 Sized_relobj_file
<size
, false>* object
,
2166 unsigned int r_type
)
2168 gold_error(_("%s: unsupported reloc %u against local symbol"),
2169 object
->name().c_str(), r_type
);
2172 // We are about to emit a dynamic relocation of type R_TYPE. If the
2173 // dynamic linker does not support it, issue an error. The GNU linker
2174 // only issues a non-PIC error for an allocated read-only section.
2175 // Here we know the section is allocated, but we don't know that it is
2176 // read-only. But we check for all the relocation types which the
2177 // glibc dynamic linker supports, so it seems appropriate to issue an
2178 // error even if the section is not read-only. If GSYM is not NULL,
2179 // it is the symbol the relocation is against; if it is NULL, the
2180 // relocation is against a local symbol.
2184 Target_x86_64
<size
>::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
2189 // These are the relocation types supported by glibc for x86_64
2190 // which should always work.
2191 case elfcpp::R_X86_64_RELATIVE
:
2192 case elfcpp::R_X86_64_IRELATIVE
:
2193 case elfcpp::R_X86_64_GLOB_DAT
:
2194 case elfcpp::R_X86_64_JUMP_SLOT
:
2195 case elfcpp::R_X86_64_DTPMOD64
:
2196 case elfcpp::R_X86_64_DTPOFF64
:
2197 case elfcpp::R_X86_64_TPOFF64
:
2198 case elfcpp::R_X86_64_64
:
2199 case elfcpp::R_X86_64_COPY
:
2202 // glibc supports these reloc types, but they can overflow.
2203 case elfcpp::R_X86_64_PC32
:
2204 case elfcpp::R_X86_64_PC32_BND
:
2205 // A PC relative reference is OK against a local symbol or if
2206 // the symbol is defined locally.
2208 || (!gsym
->is_from_dynobj()
2209 && !gsym
->is_undefined()
2210 && !gsym
->is_preemptible()))
2213 case elfcpp::R_X86_64_32
:
2214 // R_X86_64_32 is OK for x32.
2215 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2217 if (this->issued_non_pic_error_
)
2219 gold_assert(parameters
->options().output_is_position_independent());
2221 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
2222 "overflow at runtime; recompile with -fPIC"));
2228 case elfcpp::R_X86_64_32
:
2229 r_name
= "R_X86_64_32";
2231 case elfcpp::R_X86_64_PC32
:
2232 r_name
= "R_X86_64_PC32";
2234 case elfcpp::R_X86_64_PC32_BND
:
2235 r_name
= "R_X86_64_PC32_BND";
2241 object
->error(_("requires dynamic %s reloc against '%s' "
2242 "which may overflow at runtime; recompile "
2244 r_name
, gsym
->name());
2246 this->issued_non_pic_error_
= true;
2250 // This prevents us from issuing more than one error per reloc
2251 // section. But we can still wind up issuing more than one
2252 // error per object file.
2253 if (this->issued_non_pic_error_
)
2255 gold_assert(parameters
->options().output_is_position_independent());
2256 object
->error(_("requires unsupported dynamic reloc %u; "
2257 "recompile with -fPIC"),
2259 this->issued_non_pic_error_
= true;
2262 case elfcpp::R_X86_64_NONE
:
2267 // Return whether we need to make a PLT entry for a relocation of the
2268 // given type against a STT_GNU_IFUNC symbol.
2272 Target_x86_64
<size
>::Scan::reloc_needs_plt_for_ifunc(
2273 Sized_relobj_file
<size
, false>* object
,
2274 unsigned int r_type
)
2276 int flags
= Scan::get_reference_flags(r_type
);
2277 if (flags
& Symbol::TLS_REF
)
2278 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
2279 object
->name().c_str(), r_type
);
2283 // Scan a relocation for a local symbol.
2287 Target_x86_64
<size
>::Scan::local(Symbol_table
* symtab
,
2289 Target_x86_64
<size
>* target
,
2290 Sized_relobj_file
<size
, false>* object
,
2291 unsigned int data_shndx
,
2292 Output_section
* output_section
,
2293 const elfcpp::Rela
<size
, false>& reloc
,
2294 unsigned int r_type
,
2295 const elfcpp::Sym
<size
, false>& lsym
,
2301 // A local STT_GNU_IFUNC symbol may require a PLT entry.
2302 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
2303 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
2305 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2306 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
2311 case elfcpp::R_X86_64_NONE
:
2312 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2313 case elfcpp::R_X86_64_GNU_VTENTRY
:
2316 case elfcpp::R_X86_64_64
:
2317 // If building a shared library (or a position-independent
2318 // executable), we need to create a dynamic relocation for this
2319 // location. The relocation applied at link time will apply the
2320 // link-time value, so we flag the location with an
2321 // R_X86_64_RELATIVE relocation so the dynamic loader can
2322 // relocate it easily.
2323 if (parameters
->options().output_is_position_independent())
2325 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2326 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2327 rela_dyn
->add_local_relative(object
, r_sym
,
2329 ? elfcpp::R_X86_64_RELATIVE64
2330 : elfcpp::R_X86_64_RELATIVE
),
2331 output_section
, data_shndx
,
2332 reloc
.get_r_offset(),
2333 reloc
.get_r_addend(), is_ifunc
);
2337 case elfcpp::R_X86_64_32
:
2338 case elfcpp::R_X86_64_32S
:
2339 case elfcpp::R_X86_64_16
:
2340 case elfcpp::R_X86_64_8
:
2341 // If building a shared library (or a position-independent
2342 // executable), we need to create a dynamic relocation for this
2343 // location. We can't use an R_X86_64_RELATIVE relocation
2344 // because that is always a 64-bit relocation.
2345 if (parameters
->options().output_is_position_independent())
2347 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
2348 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
2350 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2351 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2352 rela_dyn
->add_local_relative(object
, r_sym
,
2353 elfcpp::R_X86_64_RELATIVE
,
2354 output_section
, data_shndx
,
2355 reloc
.get_r_offset(),
2356 reloc
.get_r_addend(), is_ifunc
);
2360 this->check_non_pic(object
, r_type
, NULL
);
2362 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2363 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2364 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
2365 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
2366 data_shndx
, reloc
.get_r_offset(),
2367 reloc
.get_r_addend());
2370 gold_assert(lsym
.get_st_value() == 0);
2371 unsigned int shndx
= lsym
.get_st_shndx();
2373 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
2376 object
->error(_("section symbol %u has bad shndx %u"),
2379 rela_dyn
->add_local_section(object
, shndx
,
2380 r_type
, output_section
,
2381 data_shndx
, reloc
.get_r_offset(),
2382 reloc
.get_r_addend());
2387 case elfcpp::R_X86_64_PC64
:
2388 case elfcpp::R_X86_64_PC32
:
2389 case elfcpp::R_X86_64_PC32_BND
:
2390 case elfcpp::R_X86_64_PC16
:
2391 case elfcpp::R_X86_64_PC8
:
2394 case elfcpp::R_X86_64_PLT32
:
2395 case elfcpp::R_X86_64_PLT32_BND
:
2396 // Since we know this is a local symbol, we can handle this as a
2400 case elfcpp::R_X86_64_GOTPC32
:
2401 case elfcpp::R_X86_64_GOTOFF64
:
2402 case elfcpp::R_X86_64_GOTPC64
:
2403 case elfcpp::R_X86_64_PLTOFF64
:
2404 // We need a GOT section.
2405 target
->got_section(symtab
, layout
);
2406 // For PLTOFF64, we'd normally want a PLT section, but since we
2407 // know this is a local symbol, no PLT is needed.
2410 case elfcpp::R_X86_64_GOT64
:
2411 case elfcpp::R_X86_64_GOT32
:
2412 case elfcpp::R_X86_64_GOTPCREL64
:
2413 case elfcpp::R_X86_64_GOTPCREL
:
2414 case elfcpp::R_X86_64_GOTPLT64
:
2416 // The symbol requires a GOT entry.
2417 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2418 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2420 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
2421 // lets function pointers compare correctly with shared
2422 // libraries. Otherwise we would need an IRELATIVE reloc.
2425 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
2427 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
2430 // If we are generating a shared object, we need to add a
2431 // dynamic relocation for this symbol's GOT entry.
2432 if (parameters
->options().output_is_position_independent())
2434 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2435 // R_X86_64_RELATIVE assumes a 64-bit relocation.
2436 if (r_type
!= elfcpp::R_X86_64_GOT32
)
2438 unsigned int got_offset
=
2439 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
2440 rela_dyn
->add_local_relative(object
, r_sym
,
2441 elfcpp::R_X86_64_RELATIVE
,
2442 got
, got_offset
, 0, is_ifunc
);
2446 this->check_non_pic(object
, r_type
, NULL
);
2448 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
2449 rela_dyn
->add_local(
2450 object
, r_sym
, r_type
, got
,
2451 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
2455 // For GOTPLT64, we'd normally want a PLT section, but since
2456 // we know this is a local symbol, no PLT is needed.
2460 case elfcpp::R_X86_64_COPY
:
2461 case elfcpp::R_X86_64_GLOB_DAT
:
2462 case elfcpp::R_X86_64_JUMP_SLOT
:
2463 case elfcpp::R_X86_64_RELATIVE
:
2464 case elfcpp::R_X86_64_IRELATIVE
:
2465 // These are outstanding tls relocs, which are unexpected when linking
2466 case elfcpp::R_X86_64_TPOFF64
:
2467 case elfcpp::R_X86_64_DTPMOD64
:
2468 case elfcpp::R_X86_64_TLSDESC
:
2469 gold_error(_("%s: unexpected reloc %u in object file"),
2470 object
->name().c_str(), r_type
);
2473 // These are initial tls relocs, which are expected when linking
2474 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2475 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2476 case elfcpp::R_X86_64_TLSDESC_CALL
:
2477 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2478 case elfcpp::R_X86_64_DTPOFF32
:
2479 case elfcpp::R_X86_64_DTPOFF64
:
2480 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2481 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2483 bool output_is_shared
= parameters
->options().shared();
2484 const tls::Tls_optimization optimized_type
2485 = Target_x86_64
<size
>::optimize_tls_reloc(!output_is_shared
,
2489 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2490 if (optimized_type
== tls::TLSOPT_NONE
)
2492 // Create a pair of GOT entries for the module index and
2493 // dtv-relative offset.
2494 Output_data_got
<64, false>* got
2495 = target
->got_section(symtab
, layout
);
2496 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2497 unsigned int shndx
= lsym
.get_st_shndx();
2499 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
2501 object
->error(_("local symbol %u has bad shndx %u"),
2504 got
->add_local_pair_with_rel(object
, r_sym
,
2507 target
->rela_dyn_section(layout
),
2508 elfcpp::R_X86_64_DTPMOD64
);
2510 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2511 unsupported_reloc_local(object
, r_type
);
2514 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2515 target
->define_tls_base_symbol(symtab
, layout
);
2516 if (optimized_type
== tls::TLSOPT_NONE
)
2518 // Create reserved PLT and GOT entries for the resolver.
2519 target
->reserve_tlsdesc_entries(symtab
, layout
);
2521 // Generate a double GOT entry with an
2522 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
2523 // is resolved lazily, so the GOT entry needs to be in
2524 // an area in .got.plt, not .got. Call got_section to
2525 // make sure the section has been created.
2526 target
->got_section(symtab
, layout
);
2527 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2528 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2529 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
2531 unsigned int got_offset
= got
->add_constant(0);
2532 got
->add_constant(0);
2533 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
2535 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2536 // We store the arguments we need in a vector, and
2537 // use the index into the vector as the parameter
2538 // to pass to the target specific routines.
2539 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
2540 void* arg
= reinterpret_cast<void*>(intarg
);
2541 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
2542 got
, got_offset
, 0);
2545 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2546 unsupported_reloc_local(object
, r_type
);
2549 case elfcpp::R_X86_64_TLSDESC_CALL
:
2552 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2553 if (optimized_type
== tls::TLSOPT_NONE
)
2555 // Create a GOT entry for the module index.
2556 target
->got_mod_index_entry(symtab
, layout
, object
);
2558 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2559 unsupported_reloc_local(object
, r_type
);
2562 case elfcpp::R_X86_64_DTPOFF32
:
2563 case elfcpp::R_X86_64_DTPOFF64
:
2566 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2567 layout
->set_has_static_tls();
2568 if (optimized_type
== tls::TLSOPT_NONE
)
2570 // Create a GOT entry for the tp-relative offset.
2571 Output_data_got
<64, false>* got
2572 = target
->got_section(symtab
, layout
);
2573 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
2574 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
2575 target
->rela_dyn_section(layout
),
2576 elfcpp::R_X86_64_TPOFF64
);
2578 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2579 unsupported_reloc_local(object
, r_type
);
2582 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2583 layout
->set_has_static_tls();
2584 if (output_is_shared
)
2585 unsupported_reloc_local(object
, r_type
);
2594 case elfcpp::R_X86_64_SIZE32
:
2595 case elfcpp::R_X86_64_SIZE64
:
2597 gold_error(_("%s: unsupported reloc %u against local symbol"),
2598 object
->name().c_str(), r_type
);
2604 // Report an unsupported relocation against a global symbol.
2608 Target_x86_64
<size
>::Scan::unsupported_reloc_global(
2609 Sized_relobj_file
<size
, false>* object
,
2610 unsigned int r_type
,
2613 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2614 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
2617 // Returns true if this relocation type could be that of a function pointer.
2620 Target_x86_64
<size
>::Scan::possible_function_pointer_reloc(unsigned int r_type
)
2624 case elfcpp::R_X86_64_64
:
2625 case elfcpp::R_X86_64_32
:
2626 case elfcpp::R_X86_64_32S
:
2627 case elfcpp::R_X86_64_16
:
2628 case elfcpp::R_X86_64_8
:
2629 case elfcpp::R_X86_64_GOT64
:
2630 case elfcpp::R_X86_64_GOT32
:
2631 case elfcpp::R_X86_64_GOTPCREL64
:
2632 case elfcpp::R_X86_64_GOTPCREL
:
2633 case elfcpp::R_X86_64_GOTPLT64
:
2641 // For safe ICF, scan a relocation for a local symbol to check if it
2642 // corresponds to a function pointer being taken. In that case mark
2643 // the function whose pointer was taken as not foldable.
2647 Target_x86_64
<size
>::Scan::local_reloc_may_be_function_pointer(
2650 Target_x86_64
<size
>* ,
2651 Sized_relobj_file
<size
, false>* ,
2654 const elfcpp::Rela
<size
, false>& ,
2655 unsigned int r_type
,
2656 const elfcpp::Sym
<size
, false>&)
2658 // When building a shared library, do not fold any local symbols as it is
2659 // not possible to distinguish pointer taken versus a call by looking at
2660 // the relocation types.
2661 return (parameters
->options().shared()
2662 || possible_function_pointer_reloc(r_type
));
2665 // For safe ICF, scan a relocation for a global symbol to check if it
2666 // corresponds to a function pointer being taken. In that case mark
2667 // the function whose pointer was taken as not foldable.
2671 Target_x86_64
<size
>::Scan::global_reloc_may_be_function_pointer(
2674 Target_x86_64
<size
>* ,
2675 Sized_relobj_file
<size
, false>* ,
2678 const elfcpp::Rela
<size
, false>& ,
2679 unsigned int r_type
,
2682 // When building a shared library, do not fold symbols whose visibility
2683 // is hidden, internal or protected.
2684 return ((parameters
->options().shared()
2685 && (gsym
->visibility() == elfcpp::STV_INTERNAL
2686 || gsym
->visibility() == elfcpp::STV_PROTECTED
2687 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
2688 || possible_function_pointer_reloc(r_type
));
2691 // Scan a relocation for a global symbol.
2695 Target_x86_64
<size
>::Scan::global(Symbol_table
* symtab
,
2697 Target_x86_64
<size
>* target
,
2698 Sized_relobj_file
<size
, false>* object
,
2699 unsigned int data_shndx
,
2700 Output_section
* output_section
,
2701 const elfcpp::Rela
<size
, false>& reloc
,
2702 unsigned int r_type
,
2705 // A STT_GNU_IFUNC symbol may require a PLT entry.
2706 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2707 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
2708 target
->make_plt_entry(symtab
, layout
, gsym
);
2712 case elfcpp::R_X86_64_NONE
:
2713 case elfcpp::R_X86_64_GNU_VTINHERIT
:
2714 case elfcpp::R_X86_64_GNU_VTENTRY
:
2717 case elfcpp::R_X86_64_64
:
2718 case elfcpp::R_X86_64_32
:
2719 case elfcpp::R_X86_64_32S
:
2720 case elfcpp::R_X86_64_16
:
2721 case elfcpp::R_X86_64_8
:
2723 // Make a PLT entry if necessary.
2724 if (gsym
->needs_plt_entry())
2726 target
->make_plt_entry(symtab
, layout
, gsym
);
2727 // Since this is not a PC-relative relocation, we may be
2728 // taking the address of a function. In that case we need to
2729 // set the entry in the dynamic symbol table to the address of
2731 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
2732 gsym
->set_needs_dynsym_value();
2734 // Make a dynamic relocation if necessary.
2735 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2737 if (gsym
->may_need_copy_reloc())
2739 target
->copy_reloc(symtab
, layout
, object
,
2740 data_shndx
, output_section
, gsym
, reloc
);
2742 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
2743 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
2744 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2745 && gsym
->can_use_relative_reloc(false)
2746 && !gsym
->is_from_dynobj()
2747 && !gsym
->is_undefined()
2748 && !gsym
->is_preemptible())
2750 // Use an IRELATIVE reloc for a locally defined
2751 // STT_GNU_IFUNC symbol. This makes a function
2752 // address in a PIE executable match the address in a
2753 // shared library that it links against.
2754 Reloc_section
* rela_dyn
=
2755 target
->rela_irelative_section(layout
);
2756 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
2757 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
2758 output_section
, object
,
2760 reloc
.get_r_offset(),
2761 reloc
.get_r_addend());
2763 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
2764 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
2765 && gsym
->can_use_relative_reloc(false))
2767 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2768 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
2769 output_section
, object
,
2771 reloc
.get_r_offset(),
2772 reloc
.get_r_addend(), false);
2776 this->check_non_pic(object
, r_type
, gsym
);
2777 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2778 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2779 data_shndx
, reloc
.get_r_offset(),
2780 reloc
.get_r_addend());
2786 case elfcpp::R_X86_64_PC64
:
2787 case elfcpp::R_X86_64_PC32
:
2788 case elfcpp::R_X86_64_PC32_BND
:
2789 case elfcpp::R_X86_64_PC16
:
2790 case elfcpp::R_X86_64_PC8
:
2792 // Make a PLT entry if necessary.
2793 if (gsym
->needs_plt_entry())
2794 target
->make_plt_entry(symtab
, layout
, gsym
);
2795 // Make a dynamic relocation if necessary.
2796 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
2798 if (gsym
->may_need_copy_reloc())
2800 target
->copy_reloc(symtab
, layout
, object
,
2801 data_shndx
, output_section
, gsym
, reloc
);
2805 this->check_non_pic(object
, r_type
, gsym
);
2806 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2807 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
2808 data_shndx
, reloc
.get_r_offset(),
2809 reloc
.get_r_addend());
2815 case elfcpp::R_X86_64_GOT64
:
2816 case elfcpp::R_X86_64_GOT32
:
2817 case elfcpp::R_X86_64_GOTPCREL64
:
2818 case elfcpp::R_X86_64_GOTPCREL
:
2819 case elfcpp::R_X86_64_GOTPLT64
:
2821 // The symbol requires a GOT entry.
2822 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
2823 if (gsym
->final_value_is_known())
2825 // For a STT_GNU_IFUNC symbol we want the PLT address.
2826 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
2827 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2829 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2833 // If this symbol is not fully resolved, we need to add a
2834 // dynamic relocation for it.
2835 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
2837 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2839 // 1) The symbol may be defined in some other module.
2841 // 2) We are building a shared library and this is a
2842 // protected symbol; using GLOB_DAT means that the dynamic
2843 // linker can use the address of the PLT in the main
2844 // executable when appropriate so that function address
2845 // comparisons work.
2847 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2848 // code, again so that function address comparisons work.
2849 if (gsym
->is_from_dynobj()
2850 || gsym
->is_undefined()
2851 || gsym
->is_preemptible()
2852 || (gsym
->visibility() == elfcpp::STV_PROTECTED
2853 && parameters
->options().shared())
2854 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
2855 && parameters
->options().output_is_position_independent()))
2856 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
2857 elfcpp::R_X86_64_GLOB_DAT
);
2860 // For a STT_GNU_IFUNC symbol we want to write the PLT
2861 // offset into the GOT, so that function pointer
2862 // comparisons work correctly.
2864 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
2865 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2868 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2869 // Tell the dynamic linker to use the PLT address
2870 // when resolving relocations.
2871 if (gsym
->is_from_dynobj()
2872 && !parameters
->options().shared())
2873 gsym
->set_needs_dynsym_value();
2877 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2878 rela_dyn
->add_global_relative(gsym
,
2879 elfcpp::R_X86_64_RELATIVE
,
2880 got
, got_off
, 0, false);
2884 // For GOTPLT64, we also need a PLT entry (but only if the
2885 // symbol is not fully resolved).
2886 if (r_type
== elfcpp::R_X86_64_GOTPLT64
2887 && !gsym
->final_value_is_known())
2888 target
->make_plt_entry(symtab
, layout
, gsym
);
2892 case elfcpp::R_X86_64_PLT32
:
2893 case elfcpp::R_X86_64_PLT32_BND
:
2894 // If the symbol is fully resolved, this is just a PC32 reloc.
2895 // Otherwise we need a PLT entry.
2896 if (gsym
->final_value_is_known())
2898 // If building a shared library, we can also skip the PLT entry
2899 // if the symbol is defined in the output file and is protected
2901 if (gsym
->is_defined()
2902 && !gsym
->is_from_dynobj()
2903 && !gsym
->is_preemptible())
2905 target
->make_plt_entry(symtab
, layout
, gsym
);
2908 case elfcpp::R_X86_64_GOTPC32
:
2909 case elfcpp::R_X86_64_GOTOFF64
:
2910 case elfcpp::R_X86_64_GOTPC64
:
2911 case elfcpp::R_X86_64_PLTOFF64
:
2912 // We need a GOT section.
2913 target
->got_section(symtab
, layout
);
2914 // For PLTOFF64, we also need a PLT entry (but only if the
2915 // symbol is not fully resolved).
2916 if (r_type
== elfcpp::R_X86_64_PLTOFF64
2917 && !gsym
->final_value_is_known())
2918 target
->make_plt_entry(symtab
, layout
, gsym
);
2921 case elfcpp::R_X86_64_COPY
:
2922 case elfcpp::R_X86_64_GLOB_DAT
:
2923 case elfcpp::R_X86_64_JUMP_SLOT
:
2924 case elfcpp::R_X86_64_RELATIVE
:
2925 case elfcpp::R_X86_64_IRELATIVE
:
2926 // These are outstanding tls relocs, which are unexpected when linking
2927 case elfcpp::R_X86_64_TPOFF64
:
2928 case elfcpp::R_X86_64_DTPMOD64
:
2929 case elfcpp::R_X86_64_TLSDESC
:
2930 gold_error(_("%s: unexpected reloc %u in object file"),
2931 object
->name().c_str(), r_type
);
2934 // These are initial tls relocs, which are expected for global()
2935 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
2936 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
2937 case elfcpp::R_X86_64_TLSDESC_CALL
:
2938 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
2939 case elfcpp::R_X86_64_DTPOFF32
:
2940 case elfcpp::R_X86_64_DTPOFF64
:
2941 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
2942 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
2944 const bool is_final
= gsym
->final_value_is_known();
2945 const tls::Tls_optimization optimized_type
2946 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
2949 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
2950 if (optimized_type
== tls::TLSOPT_NONE
)
2952 // Create a pair of GOT entries for the module index and
2953 // dtv-relative offset.
2954 Output_data_got
<64, false>* got
2955 = target
->got_section(symtab
, layout
);
2956 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
2957 target
->rela_dyn_section(layout
),
2958 elfcpp::R_X86_64_DTPMOD64
,
2959 elfcpp::R_X86_64_DTPOFF64
);
2961 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2963 // Create a GOT entry for the tp-relative offset.
2964 Output_data_got
<64, false>* got
2965 = target
->got_section(symtab
, layout
);
2966 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
2967 target
->rela_dyn_section(layout
),
2968 elfcpp::R_X86_64_TPOFF64
);
2970 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2971 unsupported_reloc_global(object
, r_type
, gsym
);
2974 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
2975 target
->define_tls_base_symbol(symtab
, layout
);
2976 if (optimized_type
== tls::TLSOPT_NONE
)
2978 // Create reserved PLT and GOT entries for the resolver.
2979 target
->reserve_tlsdesc_entries(symtab
, layout
);
2981 // Create a double GOT entry with an R_X86_64_TLSDESC
2982 // reloc. The R_X86_64_TLSDESC reloc is resolved
2983 // lazily, so the GOT entry needs to be in an area in
2984 // .got.plt, not .got. Call got_section to make sure
2985 // the section has been created.
2986 target
->got_section(symtab
, layout
);
2987 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
2988 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
2989 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
2990 elfcpp::R_X86_64_TLSDESC
, 0);
2992 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2994 // Create a GOT entry for the tp-relative offset.
2995 Output_data_got
<64, false>* got
2996 = target
->got_section(symtab
, layout
);
2997 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
2998 target
->rela_dyn_section(layout
),
2999 elfcpp::R_X86_64_TPOFF64
);
3001 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3002 unsupported_reloc_global(object
, r_type
, gsym
);
3005 case elfcpp::R_X86_64_TLSDESC_CALL
:
3008 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3009 if (optimized_type
== tls::TLSOPT_NONE
)
3011 // Create a GOT entry for the module index.
3012 target
->got_mod_index_entry(symtab
, layout
, object
);
3014 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3015 unsupported_reloc_global(object
, r_type
, gsym
);
3018 case elfcpp::R_X86_64_DTPOFF32
:
3019 case elfcpp::R_X86_64_DTPOFF64
:
3022 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3023 layout
->set_has_static_tls();
3024 if (optimized_type
== tls::TLSOPT_NONE
)
3026 // Create a GOT entry for the tp-relative offset.
3027 Output_data_got
<64, false>* got
3028 = target
->got_section(symtab
, layout
);
3029 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
3030 target
->rela_dyn_section(layout
),
3031 elfcpp::R_X86_64_TPOFF64
);
3033 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3034 unsupported_reloc_global(object
, r_type
, gsym
);
3037 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3038 layout
->set_has_static_tls();
3039 if (parameters
->options().shared())
3040 unsupported_reloc_global(object
, r_type
, gsym
);
3049 case elfcpp::R_X86_64_SIZE32
:
3050 case elfcpp::R_X86_64_SIZE64
:
3052 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3053 object
->name().c_str(), r_type
,
3054 gsym
->demangled_name().c_str());
3061 Target_x86_64
<size
>::gc_process_relocs(Symbol_table
* symtab
,
3063 Sized_relobj_file
<size
, false>* object
,
3064 unsigned int data_shndx
,
3065 unsigned int sh_type
,
3066 const unsigned char* prelocs
,
3068 Output_section
* output_section
,
3069 bool needs_special_offset_handling
,
3070 size_t local_symbol_count
,
3071 const unsigned char* plocal_symbols
)
3074 if (sh_type
== elfcpp::SHT_REL
)
3079 gold::gc_process_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
3080 typename Target_x86_64
<size
>::Scan
,
3081 typename Target_x86_64
<size
>::Relocatable_size_for_reloc
>(
3090 needs_special_offset_handling
,
3095 // Scan relocations for a section.
3099 Target_x86_64
<size
>::scan_relocs(Symbol_table
* symtab
,
3101 Sized_relobj_file
<size
, false>* object
,
3102 unsigned int data_shndx
,
3103 unsigned int sh_type
,
3104 const unsigned char* prelocs
,
3106 Output_section
* output_section
,
3107 bool needs_special_offset_handling
,
3108 size_t local_symbol_count
,
3109 const unsigned char* plocal_symbols
)
3111 if (sh_type
== elfcpp::SHT_REL
)
3113 gold_error(_("%s: unsupported REL reloc section"),
3114 object
->name().c_str());
3118 gold::scan_relocs
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
3119 typename Target_x86_64
<size
>::Scan
>(
3128 needs_special_offset_handling
,
3133 // Finalize the sections.
3137 Target_x86_64
<size
>::do_finalize_sections(
3139 const Input_objects
*,
3140 Symbol_table
* symtab
)
3142 const Reloc_section
* rel_plt
= (this->plt_
== NULL
3144 : this->plt_
->rela_plt());
3145 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
3146 this->rela_dyn_
, true, false);
3148 // Fill in some more dynamic tags.
3149 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
3152 if (this->plt_
!= NULL
3153 && this->plt_
->output_section() != NULL
3154 && this->plt_
->has_tlsdesc_entry())
3156 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
3157 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
3158 this->got_
->finalize_data_size();
3159 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
3160 this->plt_
, plt_offset
);
3161 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
3162 this->got_
, got_offset
);
3166 // Emit any relocs we saved in an attempt to avoid generating COPY
3168 if (this->copy_relocs_
.any_saved_relocs())
3169 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
3171 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3172 // the .got.plt section.
3173 Symbol
* sym
= this->global_offset_table_
;
3176 uint64_t data_size
= this->got_plt_
->current_data_size();
3177 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
3180 if (parameters
->doing_static_link()
3181 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
3183 // If linking statically, make sure that the __rela_iplt symbols
3184 // were defined if necessary, even if we didn't create a PLT.
3185 static const Define_symbol_in_segment syms
[] =
3188 "__rela_iplt_start", // name
3189 elfcpp::PT_LOAD
, // segment_type
3190 elfcpp::PF_W
, // segment_flags_set
3191 elfcpp::PF(0), // segment_flags_clear
3194 elfcpp::STT_NOTYPE
, // type
3195 elfcpp::STB_GLOBAL
, // binding
3196 elfcpp::STV_HIDDEN
, // visibility
3198 Symbol::SEGMENT_START
, // offset_from_base
3202 "__rela_iplt_end", // name
3203 elfcpp::PT_LOAD
, // segment_type
3204 elfcpp::PF_W
, // segment_flags_set
3205 elfcpp::PF(0), // segment_flags_clear
3208 elfcpp::STT_NOTYPE
, // type
3209 elfcpp::STB_GLOBAL
, // binding
3210 elfcpp::STV_HIDDEN
, // visibility
3212 Symbol::SEGMENT_START
, // offset_from_base
3217 symtab
->define_symbols(layout
, 2, syms
,
3218 layout
->script_options()->saw_sections_clause());
3222 // Perform a relocation.
3226 Target_x86_64
<size
>::Relocate::relocate(
3227 const Relocate_info
<size
, false>* relinfo
,
3228 Target_x86_64
<size
>* target
,
3231 const elfcpp::Rela
<size
, false>& rela
,
3232 unsigned int r_type
,
3233 const Sized_symbol
<size
>* gsym
,
3234 const Symbol_value
<size
>* psymval
,
3235 unsigned char* view
,
3236 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3237 section_size_type view_size
)
3239 if (this->skip_call_tls_get_addr_
)
3241 if ((r_type
!= elfcpp::R_X86_64_PLT32
3242 && r_type
!= elfcpp::R_X86_64_PLT32_BND
3243 && r_type
!= elfcpp::R_X86_64_PC32_BND
3244 && r_type
!= elfcpp::R_X86_64_PC32
)
3246 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
3248 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3249 _("missing expected TLS relocation"));
3253 this->skip_call_tls_get_addr_
= false;
3261 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3263 // Pick the value to use for symbols defined in the PLT.
3264 Symbol_value
<size
> symval
;
3266 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
3268 symval
.set_output_value(target
->plt_address_for_global(gsym
));
3271 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
3273 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3274 if (object
->local_has_plt_offset(r_sym
))
3276 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
3281 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3283 // Get the GOT offset if needed.
3284 // The GOT pointer points to the end of the GOT section.
3285 // We need to subtract the size of the GOT section to get
3286 // the actual offset to use in the relocation.
3287 bool have_got_offset
= false;
3288 unsigned int got_offset
= 0;
3291 case elfcpp::R_X86_64_GOT32
:
3292 case elfcpp::R_X86_64_GOT64
:
3293 case elfcpp::R_X86_64_GOTPLT64
:
3294 case elfcpp::R_X86_64_GOTPCREL
:
3295 case elfcpp::R_X86_64_GOTPCREL64
:
3298 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
3299 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
3303 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3304 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
3305 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
3306 - target
->got_size());
3308 have_got_offset
= true;
3317 case elfcpp::R_X86_64_NONE
:
3318 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3319 case elfcpp::R_X86_64_GNU_VTENTRY
:
3322 case elfcpp::R_X86_64_64
:
3323 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
, addend
);
3326 case elfcpp::R_X86_64_PC64
:
3327 Relocate_functions
<size
, false>::pcrela64(view
, object
, psymval
, addend
,
3331 case elfcpp::R_X86_64_32
:
3332 // FIXME: we need to verify that value + addend fits into 32 bits:
3333 // uint64_t x = value + addend;
3334 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
3335 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
3336 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3339 case elfcpp::R_X86_64_32S
:
3340 // FIXME: we need to verify that value + addend fits into 32 bits:
3341 // int64_t x = value + addend; // note this quantity is signed!
3342 // x == static_cast<int64_t>(static_cast<int32_t>(x))
3343 Relocate_functions
<size
, false>::rela32(view
, object
, psymval
, addend
);
3346 case elfcpp::R_X86_64_PC32
:
3347 case elfcpp::R_X86_64_PC32_BND
:
3348 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3352 case elfcpp::R_X86_64_16
:
3353 Relocate_functions
<size
, false>::rela16(view
, object
, psymval
, addend
);
3356 case elfcpp::R_X86_64_PC16
:
3357 Relocate_functions
<size
, false>::pcrela16(view
, object
, psymval
, addend
,
3361 case elfcpp::R_X86_64_8
:
3362 Relocate_functions
<size
, false>::rela8(view
, object
, psymval
, addend
);
3365 case elfcpp::R_X86_64_PC8
:
3366 Relocate_functions
<size
, false>::pcrela8(view
, object
, psymval
, addend
,
3370 case elfcpp::R_X86_64_PLT32
:
3371 case elfcpp::R_X86_64_PLT32_BND
:
3372 gold_assert(gsym
== NULL
3373 || gsym
->has_plt_offset()
3374 || gsym
->final_value_is_known()
3375 || (gsym
->is_defined()
3376 && !gsym
->is_from_dynobj()
3377 && !gsym
->is_preemptible()));
3378 // Note: while this code looks the same as for R_X86_64_PC32, it
3379 // behaves differently because psymval was set to point to
3380 // the PLT entry, rather than the symbol, in Scan::global().
3381 Relocate_functions
<size
, false>::pcrela32(view
, object
, psymval
, addend
,
3385 case elfcpp::R_X86_64_PLTOFF64
:
3388 gold_assert(gsym
->has_plt_offset()
3389 || gsym
->final_value_is_known());
3390 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
;
3391 got_address
= target
->got_section(NULL
, NULL
)->address();
3392 Relocate_functions
<size
, false>::rela64(view
, object
, psymval
,
3393 addend
- got_address
);
3397 case elfcpp::R_X86_64_GOT32
:
3398 gold_assert(have_got_offset
);
3399 Relocate_functions
<size
, false>::rela32(view
, got_offset
, addend
);
3402 case elfcpp::R_X86_64_GOTPC32
:
3405 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3406 value
= target
->got_plt_section()->address();
3407 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3411 case elfcpp::R_X86_64_GOT64
:
3412 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
3413 // Since we always add a PLT entry, this is equivalent.
3414 case elfcpp::R_X86_64_GOTPLT64
:
3415 gold_assert(have_got_offset
);
3416 Relocate_functions
<size
, false>::rela64(view
, got_offset
, addend
);
3419 case elfcpp::R_X86_64_GOTPC64
:
3422 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3423 value
= target
->got_plt_section()->address();
3424 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3428 case elfcpp::R_X86_64_GOTOFF64
:
3430 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3431 value
= (psymval
->value(object
, 0)
3432 - target
->got_plt_section()->address());
3433 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3437 case elfcpp::R_X86_64_GOTPCREL
:
3439 gold_assert(have_got_offset
);
3440 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3441 value
= target
->got_plt_section()->address() + got_offset
;
3442 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3446 case elfcpp::R_X86_64_GOTPCREL64
:
3448 gold_assert(have_got_offset
);
3449 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
3450 value
= target
->got_plt_section()->address() + got_offset
;
3451 Relocate_functions
<size
, false>::pcrela64(view
, value
, addend
, address
);
3455 case elfcpp::R_X86_64_COPY
:
3456 case elfcpp::R_X86_64_GLOB_DAT
:
3457 case elfcpp::R_X86_64_JUMP_SLOT
:
3458 case elfcpp::R_X86_64_RELATIVE
:
3459 case elfcpp::R_X86_64_IRELATIVE
:
3460 // These are outstanding tls relocs, which are unexpected when linking
3461 case elfcpp::R_X86_64_TPOFF64
:
3462 case elfcpp::R_X86_64_DTPMOD64
:
3463 case elfcpp::R_X86_64_TLSDESC
:
3464 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3465 _("unexpected reloc %u in object file"),
3469 // These are initial tls relocs, which are expected when linking
3470 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3471 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3472 case elfcpp::R_X86_64_TLSDESC_CALL
:
3473 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3474 case elfcpp::R_X86_64_DTPOFF32
:
3475 case elfcpp::R_X86_64_DTPOFF64
:
3476 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3477 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3478 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
3479 view
, address
, view_size
);
3482 case elfcpp::R_X86_64_SIZE32
:
3483 case elfcpp::R_X86_64_SIZE64
:
3485 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3486 _("unsupported reloc %u"),
3494 // Perform a TLS relocation.
3498 Target_x86_64
<size
>::Relocate::relocate_tls(
3499 const Relocate_info
<size
, false>* relinfo
,
3500 Target_x86_64
<size
>* target
,
3502 const elfcpp::Rela
<size
, false>& rela
,
3503 unsigned int r_type
,
3504 const Sized_symbol
<size
>* gsym
,
3505 const Symbol_value
<size
>* psymval
,
3506 unsigned char* view
,
3507 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3508 section_size_type view_size
)
3510 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
3512 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
3513 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3514 elfcpp::Shdr
<size
, false> data_shdr(relinfo
->data_shdr
);
3515 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
3517 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
3519 const bool is_final
= (gsym
== NULL
3520 ? !parameters
->options().shared()
3521 : gsym
->final_value_is_known());
3522 tls::Tls_optimization optimized_type
3523 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
3526 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3527 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3529 // If this code sequence is used in a non-executable section,
3530 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
3531 // on the assumption that it's being used by itself in a debug
3532 // section. Therefore, in the unlikely event that the code
3533 // sequence appears in a non-executable section, we simply
3534 // leave it unoptimized.
3535 optimized_type
= tls::TLSOPT_NONE
;
3537 if (optimized_type
== tls::TLSOPT_TO_LE
)
3539 if (tls_segment
== NULL
)
3541 gold_assert(parameters
->errors()->error_count() > 0
3542 || issue_undefined_symbol_error(gsym
));
3545 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
3546 rela
, r_type
, value
, view
,
3552 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3553 ? GOT_TYPE_TLS_OFFSET
3554 : GOT_TYPE_TLS_PAIR
);
3555 unsigned int got_offset
;
3558 gold_assert(gsym
->has_got_offset(got_type
));
3559 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
3563 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3564 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3565 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
3566 - target
->got_size());
3568 if (optimized_type
== tls::TLSOPT_TO_IE
)
3570 value
= target
->got_plt_section()->address() + got_offset
;
3571 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3572 value
, view
, address
, view_size
);
3575 else if (optimized_type
== tls::TLSOPT_NONE
)
3577 // Relocate the field with the offset of the pair of GOT
3579 value
= target
->got_plt_section()->address() + got_offset
;
3580 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3585 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3586 _("unsupported reloc %u"), r_type
);
3589 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3590 case elfcpp::R_X86_64_TLSDESC_CALL
:
3591 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3593 // See above comment for R_X86_64_TLSGD.
3594 optimized_type
= tls::TLSOPT_NONE
;
3596 if (optimized_type
== tls::TLSOPT_TO_LE
)
3598 if (tls_segment
== NULL
)
3600 gold_assert(parameters
->errors()->error_count() > 0
3601 || issue_undefined_symbol_error(gsym
));
3604 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
3605 rela
, r_type
, value
, view
,
3611 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
3612 ? GOT_TYPE_TLS_OFFSET
3613 : GOT_TYPE_TLS_DESC
);
3614 unsigned int got_offset
= 0;
3615 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
3616 && optimized_type
== tls::TLSOPT_NONE
)
3618 // We created GOT entries in the .got.tlsdesc portion of
3619 // the .got.plt section, but the offset stored in the
3620 // symbol is the offset within .got.tlsdesc.
3621 got_offset
= (target
->got_size()
3622 + target
->got_plt_section()->data_size());
3626 gold_assert(gsym
->has_got_offset(got_type
));
3627 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
3631 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3632 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
3633 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
3634 - target
->got_size());
3636 if (optimized_type
== tls::TLSOPT_TO_IE
)
3638 if (tls_segment
== NULL
)
3640 gold_assert(parameters
->errors()->error_count() > 0
3641 || issue_undefined_symbol_error(gsym
));
3644 value
= target
->got_plt_section()->address() + got_offset
;
3645 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
,
3646 rela
, r_type
, value
, view
, address
,
3650 else if (optimized_type
== tls::TLSOPT_NONE
)
3652 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3654 // Relocate the field with the offset of the pair of GOT
3656 value
= target
->got_plt_section()->address() + got_offset
;
3657 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3663 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3664 _("unsupported reloc %u"), r_type
);
3667 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3668 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
3670 // See above comment for R_X86_64_TLSGD.
3671 optimized_type
= tls::TLSOPT_NONE
;
3673 if (optimized_type
== tls::TLSOPT_TO_LE
)
3675 if (tls_segment
== NULL
)
3677 gold_assert(parameters
->errors()->error_count() > 0
3678 || issue_undefined_symbol_error(gsym
));
3681 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
3682 value
, view
, view_size
);
3685 else if (optimized_type
== tls::TLSOPT_NONE
)
3687 // Relocate the field with the offset of the GOT entry for
3688 // the module index.
3689 unsigned int got_offset
;
3690 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
3691 - target
->got_size());
3692 value
= target
->got_plt_section()->address() + got_offset
;
3693 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3697 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3698 _("unsupported reloc %u"), r_type
);
3701 case elfcpp::R_X86_64_DTPOFF32
:
3702 // This relocation type is used in debugging information.
3703 // In that case we need to not optimize the value. If the
3704 // section is not executable, then we assume we should not
3705 // optimize this reloc. See comments above for R_X86_64_TLSGD,
3706 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
3708 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3710 if (tls_segment
== NULL
)
3712 gold_assert(parameters
->errors()->error_count() > 0
3713 || issue_undefined_symbol_error(gsym
));
3716 value
-= tls_segment
->memsz();
3718 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3721 case elfcpp::R_X86_64_DTPOFF64
:
3722 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3723 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
3725 if (tls_segment
== NULL
)
3727 gold_assert(parameters
->errors()->error_count() > 0
3728 || issue_undefined_symbol_error(gsym
));
3731 value
-= tls_segment
->memsz();
3733 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
3736 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3737 if (optimized_type
== tls::TLSOPT_TO_LE
)
3739 if (tls_segment
== NULL
)
3741 gold_assert(parameters
->errors()->error_count() > 0
3742 || issue_undefined_symbol_error(gsym
));
3745 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
3747 r_type
, value
, view
,
3751 else if (optimized_type
== tls::TLSOPT_NONE
)
3753 // Relocate the field with the offset of the GOT entry for
3754 // the tp-relative offset of the symbol.
3755 unsigned int got_offset
;
3758 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
3759 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
3760 - target
->got_size());
3764 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
3765 gold_assert(object
->local_has_got_offset(r_sym
,
3766 GOT_TYPE_TLS_OFFSET
));
3767 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
3768 - target
->got_size());
3770 value
= target
->got_plt_section()->address() + got_offset
;
3771 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
3775 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
3776 _("unsupported reloc type %u"),
3780 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3781 if (tls_segment
== NULL
)
3783 gold_assert(parameters
->errors()->error_count() > 0
3784 || issue_undefined_symbol_error(gsym
));
3787 value
-= tls_segment
->memsz();
3788 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
3793 // Do a relocation in which we convert a TLS General-Dynamic to an
3798 Target_x86_64
<size
>::Relocate::tls_gd_to_ie(
3799 const Relocate_info
<size
, false>* relinfo
,
3802 const elfcpp::Rela
<size
, false>& rela
,
3804 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3805 unsigned char* view
,
3806 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3807 section_size_type view_size
)
3810 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3811 // .word 0x6666; rex64; call __tls_get_addr
3812 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3814 // leaq foo@tlsgd(%rip),%rdi;
3815 // .word 0x6666; rex64; call __tls_get_addr
3816 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
3818 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3819 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3820 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3824 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3826 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3827 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3828 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3833 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3835 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3836 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
3837 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
3841 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3842 Relocate_functions
<size
, false>::pcrela32(view
+ 8, value
, addend
- 8,
3845 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3847 this->skip_call_tls_get_addr_
= true;
3850 // Do a relocation in which we convert a TLS General-Dynamic to a
3855 Target_x86_64
<size
>::Relocate::tls_gd_to_le(
3856 const Relocate_info
<size
, false>* relinfo
,
3858 Output_segment
* tls_segment
,
3859 const elfcpp::Rela
<size
, false>& rela
,
3861 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3862 unsigned char* view
,
3863 section_size_type view_size
)
3866 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3867 // .word 0x6666; rex64; call __tls_get_addr
3868 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3870 // leaq foo@tlsgd(%rip),%rdi;
3871 // .word 0x6666; rex64; call __tls_get_addr
3872 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
3874 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
3875 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3876 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0));
3880 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3882 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3883 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
3884 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
3889 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
3891 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3892 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
3894 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
3898 value
-= tls_segment
->memsz();
3899 Relocate_functions
<size
, false>::rela32(view
+ 8, value
, 0);
3901 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3903 this->skip_call_tls_get_addr_
= true;
3906 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3910 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_ie(
3911 const Relocate_info
<size
, false>* relinfo
,
3914 const elfcpp::Rela
<size
, false>& rela
,
3915 unsigned int r_type
,
3916 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3917 unsigned char* view
,
3918 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
3919 section_size_type view_size
)
3921 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3923 // leaq foo@tlsdesc(%rip), %rax
3924 // ==> movq foo@gottpoff(%rip), %rax
3925 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3926 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3927 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3928 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
3930 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
3931 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
3935 // call *foo@tlscall(%rax)
3937 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
3938 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
3939 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3940 view
[0] == 0xff && view
[1] == 0x10);
3946 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3950 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_le(
3951 const Relocate_info
<size
, false>* relinfo
,
3953 Output_segment
* tls_segment
,
3954 const elfcpp::Rela
<size
, false>& rela
,
3955 unsigned int r_type
,
3956 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
3957 unsigned char* view
,
3958 section_size_type view_size
)
3960 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
3962 // leaq foo@tlsdesc(%rip), %rax
3963 // ==> movq foo@tpoff, %rax
3964 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
3965 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
3966 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3967 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
3970 value
-= tls_segment
->memsz();
3971 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
3975 // call *foo@tlscall(%rax)
3977 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
3978 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
3979 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
3980 view
[0] == 0xff && view
[1] == 0x10);
3988 Target_x86_64
<size
>::Relocate::tls_ld_to_le(
3989 const Relocate_info
<size
, false>* relinfo
,
3992 const elfcpp::Rela
<size
, false>& rela
,
3994 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
3995 unsigned char* view
,
3996 section_size_type view_size
)
3998 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4000 // ... leq foo@dtpoff(%rax),%reg
4001 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4003 // ... leq foo@dtpoff(%rax),%reg
4004 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4006 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4007 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
4009 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
4010 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
4012 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(), view
[4] == 0xe8);
4015 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4017 memcpy(view
- 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4019 // The next reloc should be a PLT32 reloc against __tls_get_addr.
4021 this->skip_call_tls_get_addr_
= true;
4024 // Do a relocation in which we convert a TLS Initial-Exec to a
4029 Target_x86_64
<size
>::Relocate::tls_ie_to_le(
4030 const Relocate_info
<size
, false>* relinfo
,
4032 Output_segment
* tls_segment
,
4033 const elfcpp::Rela
<size
, false>& rela
,
4035 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
4036 unsigned char* view
,
4037 section_size_type view_size
)
4039 // We need to examine the opcodes to figure out which instruction we
4042 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
4043 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
4045 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
4046 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
4048 unsigned char op1
= view
[-3];
4049 unsigned char op2
= view
[-2];
4050 unsigned char op3
= view
[-1];
4051 unsigned char reg
= op3
>> 3;
4059 view
[-1] = 0xc0 | reg
;
4063 // Special handling for %rsp.
4067 view
[-1] = 0xc0 | reg
;
4075 view
[-1] = 0x80 | reg
| (reg
<< 3);
4078 value
-= tls_segment
->memsz();
4079 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
4082 // Relocate section data.
4086 Target_x86_64
<size
>::relocate_section(
4087 const Relocate_info
<size
, false>* relinfo
,
4088 unsigned int sh_type
,
4089 const unsigned char* prelocs
,
4091 Output_section
* output_section
,
4092 bool needs_special_offset_handling
,
4093 unsigned char* view
,
4094 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4095 section_size_type view_size
,
4096 const Reloc_symbol_changes
* reloc_symbol_changes
)
4098 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4100 gold::relocate_section
<size
, false, Target_x86_64
<size
>, elfcpp::SHT_RELA
,
4101 typename Target_x86_64
<size
>::Relocate
,
4102 gold::Default_comdat_behavior
>(
4108 needs_special_offset_handling
,
4112 reloc_symbol_changes
);
4115 // Apply an incremental relocation. Incremental relocations always refer
4116 // to global symbols.
4120 Target_x86_64
<size
>::apply_relocation(
4121 const Relocate_info
<size
, false>* relinfo
,
4122 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
4123 unsigned int r_type
,
4124 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
4126 unsigned char* view
,
4127 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4128 section_size_type view_size
)
4130 gold::apply_relocation
<size
, false, Target_x86_64
<size
>,
4131 typename Target_x86_64
<size
>::Relocate
>(
4143 // Return the size of a relocation while scanning during a relocatable
4148 Target_x86_64
<size
>::Relocatable_size_for_reloc::get_size_for_reloc(
4149 unsigned int r_type
,
4154 case elfcpp::R_X86_64_NONE
:
4155 case elfcpp::R_X86_64_GNU_VTINHERIT
:
4156 case elfcpp::R_X86_64_GNU_VTENTRY
:
4157 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4158 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4159 case elfcpp::R_X86_64_TLSDESC_CALL
:
4160 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4161 case elfcpp::R_X86_64_DTPOFF32
:
4162 case elfcpp::R_X86_64_DTPOFF64
:
4163 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4164 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4167 case elfcpp::R_X86_64_64
:
4168 case elfcpp::R_X86_64_PC64
:
4169 case elfcpp::R_X86_64_GOTOFF64
:
4170 case elfcpp::R_X86_64_GOTPC64
:
4171 case elfcpp::R_X86_64_PLTOFF64
:
4172 case elfcpp::R_X86_64_GOT64
:
4173 case elfcpp::R_X86_64_GOTPCREL64
:
4174 case elfcpp::R_X86_64_GOTPCREL
:
4175 case elfcpp::R_X86_64_GOTPLT64
:
4178 case elfcpp::R_X86_64_32
:
4179 case elfcpp::R_X86_64_32S
:
4180 case elfcpp::R_X86_64_PC32
:
4181 case elfcpp::R_X86_64_PC32_BND
:
4182 case elfcpp::R_X86_64_PLT32
:
4183 case elfcpp::R_X86_64_PLT32_BND
:
4184 case elfcpp::R_X86_64_GOTPC32
:
4185 case elfcpp::R_X86_64_GOT32
:
4188 case elfcpp::R_X86_64_16
:
4189 case elfcpp::R_X86_64_PC16
:
4192 case elfcpp::R_X86_64_8
:
4193 case elfcpp::R_X86_64_PC8
:
4196 case elfcpp::R_X86_64_COPY
:
4197 case elfcpp::R_X86_64_GLOB_DAT
:
4198 case elfcpp::R_X86_64_JUMP_SLOT
:
4199 case elfcpp::R_X86_64_RELATIVE
:
4200 case elfcpp::R_X86_64_IRELATIVE
:
4201 // These are outstanding tls relocs, which are unexpected when linking
4202 case elfcpp::R_X86_64_TPOFF64
:
4203 case elfcpp::R_X86_64_DTPMOD64
:
4204 case elfcpp::R_X86_64_TLSDESC
:
4205 object
->error(_("unexpected reloc %u in object file"), r_type
);
4208 case elfcpp::R_X86_64_SIZE32
:
4209 case elfcpp::R_X86_64_SIZE64
:
4211 object
->error(_("unsupported reloc %u against local symbol"), r_type
);
4216 // Scan the relocs during a relocatable link.
4220 Target_x86_64
<size
>::scan_relocatable_relocs(
4221 Symbol_table
* symtab
,
4223 Sized_relobj_file
<size
, false>* object
,
4224 unsigned int data_shndx
,
4225 unsigned int sh_type
,
4226 const unsigned char* prelocs
,
4228 Output_section
* output_section
,
4229 bool needs_special_offset_handling
,
4230 size_t local_symbol_count
,
4231 const unsigned char* plocal_symbols
,
4232 Relocatable_relocs
* rr
)
4234 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4236 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_RELA
,
4237 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
4239 gold::scan_relocatable_relocs
<size
, false, elfcpp::SHT_RELA
,
4240 Scan_relocatable_relocs
>(
4248 needs_special_offset_handling
,
4254 // Relocate a section during a relocatable link.
4258 Target_x86_64
<size
>::relocate_relocs(
4259 const Relocate_info
<size
, false>* relinfo
,
4260 unsigned int sh_type
,
4261 const unsigned char* prelocs
,
4263 Output_section
* output_section
,
4264 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
4265 const Relocatable_relocs
* rr
,
4266 unsigned char* view
,
4267 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
4268 section_size_type view_size
,
4269 unsigned char* reloc_view
,
4270 section_size_type reloc_view_size
)
4272 gold_assert(sh_type
== elfcpp::SHT_RELA
);
4274 gold::relocate_relocs
<size
, false, elfcpp::SHT_RELA
>(
4279 offset_in_output_section
,
4288 // Return the value to use for a dynamic which requires special
4289 // treatment. This is how we support equality comparisons of function
4290 // pointers across shared library boundaries, as described in the
4291 // processor specific ABI supplement.
4295 Target_x86_64
<size
>::do_dynsym_value(const Symbol
* gsym
) const
4297 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
4298 return this->plt_address_for_global(gsym
);
4301 // Return a string used to fill a code section with nops to take up
4302 // the specified length.
4306 Target_x86_64
<size
>::do_code_fill(section_size_type length
) const
4310 // Build a jmpq instruction to skip over the bytes.
4311 unsigned char jmp
[5];
4313 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
4314 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
4315 + std::string(length
- 5, static_cast<char>(0x90)));
4318 // Nop sequences of various lengths.
4319 const char nop1
[1] = { '\x90' }; // nop
4320 const char nop2
[2] = { '\x66', '\x90' }; // xchg %ax %ax
4321 const char nop3
[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
4322 const char nop4
[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
4324 const char nop5
[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
4326 const char nop6
[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
4327 '\x44', '\x00', '\x00' };
4328 const char nop7
[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
4329 '\x00', '\x00', '\x00',
4331 const char nop8
[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
4332 '\x00', '\x00', '\x00',
4334 const char nop9
[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
4335 '\x84', '\x00', '\x00',
4336 '\x00', '\x00', '\x00' };
4337 const char nop10
[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4338 '\x1f', '\x84', '\x00',
4339 '\x00', '\x00', '\x00',
4341 const char nop11
[11] = { '\x66', '\x66', '\x2e', // data16
4342 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4343 '\x00', '\x00', '\x00',
4345 const char nop12
[12] = { '\x66', '\x66', '\x66', // data16; data16
4346 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4347 '\x84', '\x00', '\x00',
4348 '\x00', '\x00', '\x00' };
4349 const char nop13
[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
4350 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
4351 '\x1f', '\x84', '\x00',
4352 '\x00', '\x00', '\x00',
4354 const char nop14
[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
4355 '\x66', '\x66', '\x2e', // data16
4356 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
4357 '\x00', '\x00', '\x00',
4359 const char nop15
[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
4360 '\x66', '\x66', '\x66', // data16; data16
4361 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
4362 '\x84', '\x00', '\x00',
4363 '\x00', '\x00', '\x00' };
4365 const char* nops
[16] = {
4367 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
4368 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
4371 return std::string(nops
[length
], length
);
4374 // Return the addend to use for a target specific relocation. The
4375 // only target specific relocation is R_X86_64_TLSDESC for a local
4376 // symbol. We want to set the addend is the offset of the local
4377 // symbol in the TLS segment.
4381 Target_x86_64
<size
>::do_reloc_addend(void* arg
, unsigned int r_type
,
4384 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
4385 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
4386 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
4387 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
4388 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
4389 gold_assert(psymval
->is_tls_symbol());
4390 // The value of a TLS symbol is the offset in the TLS segment.
4391 return psymval
->value(ti
.object
, 0);
4394 // Return the value to use for the base of a DW_EH_PE_datarel offset
4395 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
4396 // assembler can not write out the difference between two labels in
4397 // different sections, so instead of using a pc-relative value they
4398 // use an offset from the GOT.
4402 Target_x86_64
<size
>::do_ehframe_datarel_base() const
4404 gold_assert(this->global_offset_table_
!= NULL
);
4405 Symbol
* sym
= this->global_offset_table_
;
4406 Sized_symbol
<size
>* ssym
= static_cast<Sized_symbol
<size
>*>(sym
);
4407 return ssym
->value();
4410 // FNOFFSET in section SHNDX in OBJECT is the start of a function
4411 // compiled with -fsplit-stack. The function calls non-split-stack
4412 // code. We have to change the function so that it always ensures
4413 // that it has enough stack space to run some random function.
4417 Target_x86_64
<size
>::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
4418 section_offset_type fnoffset
,
4419 section_size_type fnsize
,
4420 unsigned char* view
,
4421 section_size_type view_size
,
4423 std::string
* to
) const
4425 // The function starts with a comparison of the stack pointer and a
4426 // field in the TCB. This is followed by a jump.
4429 if (this->match_view(view
, view_size
, fnoffset
, "\x64\x48\x3b\x24\x25", 5)
4432 // We will call __morestack if the carry flag is set after this
4433 // comparison. We turn the comparison into an stc instruction
4435 view
[fnoffset
] = '\xf9';
4436 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 8);
4438 // lea NN(%rsp),%r10
4439 // lea NN(%rsp),%r11
4440 else if ((this->match_view(view
, view_size
, fnoffset
,
4441 "\x4c\x8d\x94\x24", 4)
4442 || this->match_view(view
, view_size
, fnoffset
,
4443 "\x4c\x8d\x9c\x24", 4))
4446 // This is loading an offset from the stack pointer for a
4447 // comparison. The offset is negative, so we decrease the
4448 // offset by the amount of space we need for the stack. This
4449 // means we will avoid calling __morestack if there happens to
4450 // be plenty of space on the stack already.
4451 unsigned char* pval
= view
+ fnoffset
+ 4;
4452 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
4453 val
-= parameters
->options().split_stack_adjust_size();
4454 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
4458 if (!object
->has_no_split_stack())
4459 object
->error(_("failed to match split-stack sequence at "
4460 "section %u offset %0zx"),
4461 shndx
, static_cast<size_t>(fnoffset
));
4465 // We have to change the function so that it calls
4466 // __morestack_non_split instead of __morestack. The former will
4467 // allocate additional stack space.
4468 *from
= "__morestack";
4469 *to
= "__morestack_non_split";
4472 // The selector for x86_64 object files. Note this is never instantiated
4473 // directly. It's only used in Target_selector_x86_64_nacl, below.
4476 class Target_selector_x86_64
: public Target_selector_freebsd
4479 Target_selector_x86_64()
4480 : Target_selector_freebsd(elfcpp::EM_X86_64
, size
, false,
4482 ? "elf64-x86-64" : "elf32-x86-64"),
4484 ? "elf64-x86-64-freebsd"
4485 : "elf32-x86-64-freebsd"),
4486 (size
== 64 ? "elf_x86_64" : "elf32_x86_64"))
4490 do_instantiate_target()
4491 { return new Target_x86_64
<size
>(); }
4495 // NaCl variant. It uses different PLT contents.
4498 class Output_data_plt_x86_64_nacl
: public Output_data_plt_x86_64
<size
>
4501 Output_data_plt_x86_64_nacl(Layout
* layout
,
4502 Output_data_got
<64, false>* got
,
4503 Output_data_space
* got_plt
,
4504 Output_data_space
* got_irelative
)
4505 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4506 got
, got_plt
, got_irelative
)
4509 Output_data_plt_x86_64_nacl(Layout
* layout
,
4510 Output_data_got
<64, false>* got
,
4511 Output_data_space
* got_plt
,
4512 Output_data_space
* got_irelative
,
4513 unsigned int plt_count
)
4514 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
4515 got
, got_plt
, got_irelative
,
4520 virtual unsigned int
4521 do_get_plt_entry_size() const
4522 { return plt_entry_size
; }
4525 do_add_eh_frame(Layout
* layout
)
4527 layout
->add_eh_frame_for_plt(this,
4528 this->plt_eh_frame_cie
,
4529 this->plt_eh_frame_cie_size
,
4531 plt_eh_frame_fde_size
);
4535 do_fill_first_plt_entry(unsigned char* pov
,
4536 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
4537 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
4539 virtual unsigned int
4540 do_fill_plt_entry(unsigned char* pov
,
4541 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4542 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4543 unsigned int got_offset
,
4544 unsigned int plt_offset
,
4545 unsigned int plt_index
);
4548 do_fill_tlsdesc_entry(unsigned char* pov
,
4549 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4550 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4551 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
4552 unsigned int tlsdesc_got_offset
,
4553 unsigned int plt_offset
);
4556 // The size of an entry in the PLT.
4557 static const int plt_entry_size
= 64;
4559 // The first entry in the PLT.
4560 static const unsigned char first_plt_entry
[plt_entry_size
];
4562 // Other entries in the PLT for an executable.
4563 static const unsigned char plt_entry
[plt_entry_size
];
4565 // The reserved TLSDESC entry in the PLT for an executable.
4566 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
4568 // The .eh_frame unwind information for the PLT.
4569 static const int plt_eh_frame_fde_size
= 32;
4570 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
4574 class Target_x86_64_nacl
: public Target_x86_64
<size
>
4577 Target_x86_64_nacl()
4578 : Target_x86_64
<size
>(&x86_64_nacl_info
)
4581 virtual Output_data_plt_x86_64
<size
>*
4582 do_make_data_plt(Layout
* layout
,
4583 Output_data_got
<64, false>* got
,
4584 Output_data_space
* got_plt
,
4585 Output_data_space
* got_irelative
)
4587 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4591 virtual Output_data_plt_x86_64
<size
>*
4592 do_make_data_plt(Layout
* layout
,
4593 Output_data_got
<64, false>* got
,
4594 Output_data_space
* got_plt
,
4595 Output_data_space
* got_irelative
,
4596 unsigned int plt_count
)
4598 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
4604 do_code_fill(section_size_type length
) const;
4607 static const Target::Target_info x86_64_nacl_info
;
4611 const Target::Target_info Target_x86_64_nacl
<64>::x86_64_nacl_info
=
4614 false, // is_big_endian
4615 elfcpp::EM_X86_64
, // machine_code
4616 false, // has_make_symbol
4617 false, // has_resolve
4618 true, // has_code_fill
4619 true, // is_default_stack_executable
4620 true, // can_icf_inline_merge_sections
4622 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
4623 0x20000, // default_text_segment_address
4624 0x10000, // abi_pagesize (overridable by -z max-page-size)
4625 0x10000, // common_pagesize (overridable by -z common-page-size)
4626 true, // isolate_execinstr
4627 0x10000000, // rosegment_gap
4628 elfcpp::SHN_UNDEF
, // small_common_shndx
4629 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
4630 0, // small_common_section_flags
4631 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
4632 NULL
, // attributes_section
4633 NULL
, // attributes_vendor
4634 "_start" // entry_symbol_name
4638 const Target::Target_info Target_x86_64_nacl
<32>::x86_64_nacl_info
=
4641 false, // is_big_endian
4642 elfcpp::EM_X86_64
, // machine_code
4643 false, // has_make_symbol
4644 false, // has_resolve
4645 true, // has_code_fill
4646 true, // is_default_stack_executable
4647 true, // can_icf_inline_merge_sections
4649 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
4650 0x20000, // default_text_segment_address
4651 0x10000, // abi_pagesize (overridable by -z max-page-size)
4652 0x10000, // common_pagesize (overridable by -z common-page-size)
4653 true, // isolate_execinstr
4654 0x10000000, // rosegment_gap
4655 elfcpp::SHN_UNDEF
, // small_common_shndx
4656 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
4657 0, // small_common_section_flags
4658 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
4659 NULL
, // attributes_section
4660 NULL
, // attributes_vendor
4661 "_start" // entry_symbol_name
4664 #define NACLMASK 0xe0 // 32-byte alignment mask.
4666 // The first entry in the PLT.
4670 Output_data_plt_x86_64_nacl
<size
>::first_plt_entry
[plt_entry_size
] =
4672 0xff, 0x35, // pushq contents of memory address
4673 0, 0, 0, 0, // replaced with address of .got + 8
4674 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
4675 0, 0, 0, 0, // replaced with address of .got + 16
4676 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4677 0x4d, 0x01, 0xfb, // add %r15, %r11
4678 0x41, 0xff, 0xe3, // jmpq *%r11
4680 // 9-byte nop sequence to pad out to the next 32-byte boundary.
4681 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
4683 // 32 bytes of nop to pad out to the standard size
4684 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4685 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4686 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4687 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4688 0x66, // excess data32 prefix
4694 Output_data_plt_x86_64_nacl
<size
>::do_fill_first_plt_entry(
4696 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4697 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
4699 memcpy(pov
, first_plt_entry
, plt_entry_size
);
4700 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
4702 - (plt_address
+ 2 + 4)));
4703 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
4705 - (plt_address
+ 9 + 4)));
4708 // Subsequent entries in the PLT.
4712 Output_data_plt_x86_64_nacl
<size
>::plt_entry
[plt_entry_size
] =
4714 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
4715 0, 0, 0, 0, // replaced with address of symbol in .got
4716 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4717 0x4d, 0x01, 0xfb, // add %r15, %r11
4718 0x41, 0xff, 0xe3, // jmpq *%r11
4720 // 15-byte nop sequence to pad out to the next 32-byte boundary.
4721 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4722 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4724 // Lazy GOT entries point here (32-byte aligned).
4725 0x68, // pushq immediate
4726 0, 0, 0, 0, // replaced with index into relocation table
4727 0xe9, // jmp relative
4728 0, 0, 0, 0, // replaced with offset to start of .plt0
4730 // 22 bytes of nop to pad out to the standard size.
4731 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4732 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4733 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
4738 Output_data_plt_x86_64_nacl
<size
>::do_fill_plt_entry(
4740 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4741 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4742 unsigned int got_offset
,
4743 unsigned int plt_offset
,
4744 unsigned int plt_index
)
4746 memcpy(pov
, plt_entry
, plt_entry_size
);
4747 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3,
4748 (got_address
+ got_offset
4749 - (plt_address
+ plt_offset
4752 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 33, plt_index
);
4753 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 38,
4754 - (plt_offset
+ 38 + 4));
4759 // The reserved TLSDESC entry in the PLT.
4763 Output_data_plt_x86_64_nacl
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
4765 0xff, 0x35, // pushq x(%rip)
4766 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
4767 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
4768 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
4769 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
4770 0x4d, 0x01, 0xfb, // add %r15, %r11
4771 0x41, 0xff, 0xe3, // jmpq *%r11
4773 // 41 bytes of nop to pad out to the standard size.
4774 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4775 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4776 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
4777 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4778 0x66, 0x66, // excess data32 prefixes
4779 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
4784 Output_data_plt_x86_64_nacl
<size
>::do_fill_tlsdesc_entry(
4786 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
4787 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
4788 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
4789 unsigned int tlsdesc_got_offset
,
4790 unsigned int plt_offset
)
4792 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
4793 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
4795 - (plt_address
+ plt_offset
4797 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
4799 + tlsdesc_got_offset
4800 - (plt_address
+ plt_offset
4804 // The .eh_frame unwind information for the PLT.
4808 Output_data_plt_x86_64_nacl
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
4810 0, 0, 0, 0, // Replaced with offset to .plt.
4811 0, 0, 0, 0, // Replaced with size of .plt.
4812 0, // Augmentation size.
4813 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
4814 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
4815 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
4816 elfcpp::DW_CFA_advance_loc
+ 58, // Advance 58 to __PLT__ + 64.
4817 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
4818 13, // Block length.
4819 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
4820 elfcpp::DW_OP_breg16
, 0, // Push %rip.
4821 elfcpp::DW_OP_const1u
, 63, // Push 0x3f.
4822 elfcpp::DW_OP_and
, // & (%rip & 0x3f).
4823 elfcpp::DW_OP_const1u
, 37, // Push 0x25.
4824 elfcpp::DW_OP_ge
, // >= ((%rip & 0x3f) >= 0x25)
4825 elfcpp::DW_OP_lit3
, // Push 3.
4826 elfcpp::DW_OP_shl
, // << (((%rip & 0x3f) >= 0x25) << 3)
4827 elfcpp::DW_OP_plus
, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
4828 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
4832 // Return a string used to fill a code section with nops.
4833 // For NaCl, long NOPs are only valid if they do not cross
4834 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
4837 Target_x86_64_nacl
<size
>::do_code_fill(section_size_type length
) const
4839 return std::string(length
, static_cast<char>(0x90));
4842 // The selector for x86_64-nacl object files.
4845 class Target_selector_x86_64_nacl
4846 : public Target_selector_nacl
<Target_selector_x86_64
<size
>,
4847 Target_x86_64_nacl
<size
> >
4850 Target_selector_x86_64_nacl()
4851 : Target_selector_nacl
<Target_selector_x86_64
<size
>,
4852 Target_x86_64_nacl
<size
> >("x86-64",
4854 ? "elf64-x86-64-nacl"
4855 : "elf32-x86-64-nacl",
4858 : "elf32_x86_64_nacl")
4862 Target_selector_x86_64_nacl
<64> target_selector_x86_64
;
4863 Target_selector_x86_64_nacl
<32> target_selector_x32
;
4865 } // End anonymous namespace.