1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2020 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
29 #include "parameters.h"
36 #include "copy-relocs.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
51 // A class to handle the .got.plt section.
53 class Output_data_got_plt_x86_64
: public Output_section_data_build
56 Output_data_got_plt_x86_64(Layout
* layout
)
57 : Output_section_data_build(8),
61 Output_data_got_plt_x86_64(Layout
* layout
, off_t data_size
)
62 : Output_section_data_build(data_size
, 8),
67 // Write out the PLT data.
69 do_write(Output_file
*);
71 // Write to a map file.
73 do_print_to_mapfile(Mapfile
* mapfile
) const
74 { mapfile
->print_output_data(this, "** GOT PLT"); }
77 // A pointer to the Layout class, so that we can find the .dynamic
78 // section when we write out the GOT PLT section.
82 // A class to handle the PLT data.
83 // This is an abstract base class that handles most of the linker details
84 // but does not know the actual contents of PLT entries. The derived
85 // classes below fill in those details.
88 class Output_data_plt_x86_64
: public Output_section_data
91 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
93 Output_data_plt_x86_64(Layout
* layout
, uint64_t addralign
,
94 Output_data_got
<64, false>* got
,
95 Output_data_got_plt_x86_64
* got_plt
,
96 Output_data_space
* got_irelative
)
97 : Output_section_data(addralign
), tlsdesc_rel_(NULL
),
98 irelative_rel_(NULL
), got_(got
), got_plt_(got_plt
),
99 got_irelative_(got_irelative
), count_(0), irelative_count_(0),
100 tlsdesc_got_offset_(-1U), free_list_()
101 { this->init(layout
); }
103 Output_data_plt_x86_64(Layout
* layout
, uint64_t plt_entry_size
,
104 Output_data_got
<64, false>* got
,
105 Output_data_got_plt_x86_64
* got_plt
,
106 Output_data_space
* got_irelative
,
107 unsigned int plt_count
)
108 : Output_section_data((plt_count
+ 1) * plt_entry_size
,
109 plt_entry_size
, false),
110 tlsdesc_rel_(NULL
), irelative_rel_(NULL
), got_(got
),
111 got_plt_(got_plt
), got_irelative_(got_irelative
), count_(plt_count
),
112 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
116 // Initialize the free list and reserve the first entry.
117 this->free_list_
.init((plt_count
+ 1) * plt_entry_size
, false);
118 this->free_list_
.remove(0, plt_entry_size
);
121 // Initialize the PLT section.
123 init(Layout
* layout
);
125 // Add an entry to the PLT.
127 add_entry(Symbol_table
*, Layout
*, Symbol
* gsym
);
129 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
131 add_local_ifunc_entry(Symbol_table
* symtab
, Layout
*,
132 Sized_relobj_file
<size
, false>* relobj
,
133 unsigned int local_sym_index
);
135 // Add the relocation for a PLT entry.
137 add_relocation(Symbol_table
*, Layout
*, Symbol
* gsym
,
138 unsigned int got_offset
);
140 // Add the reserved TLSDESC_PLT entry to the PLT.
142 reserve_tlsdesc_entry(unsigned int got_offset
)
143 { this->tlsdesc_got_offset_
= got_offset
; }
145 // Return true if a TLSDESC_PLT entry has been reserved.
147 has_tlsdesc_entry() const
148 { return this->tlsdesc_got_offset_
!= -1U; }
150 // Return the GOT offset for the reserved TLSDESC_PLT entry.
152 get_tlsdesc_got_offset() const
153 { return this->tlsdesc_got_offset_
; }
155 // Return the offset of the reserved TLSDESC_PLT entry.
157 get_tlsdesc_plt_offset() const
159 return ((this->count_
+ this->irelative_count_
+ 1)
160 * this->get_plt_entry_size());
163 // Return the .rela.plt section data.
166 { return this->rel_
; }
168 // Return where the TLSDESC relocations should go.
170 rela_tlsdesc(Layout
*);
172 // Return where the IRELATIVE relocations should go in the PLT
175 rela_irelative(Symbol_table
*, Layout
*);
177 // Return whether we created a section for IRELATIVE relocations.
179 has_irelative_section() const
180 { return this->irelative_rel_
!= NULL
; }
182 // Get count of regular PLT entries.
184 regular_count() const
185 { return this->count_
; }
187 // Return the total number of PLT entries.
190 { return this->count_
+ this->irelative_count_
; }
192 // Return the offset of the first non-reserved PLT entry.
194 first_plt_entry_offset()
195 { return this->get_plt_entry_size(); }
197 // Return the size of a PLT entry.
199 get_plt_entry_size() const
200 { return this->do_get_plt_entry_size(); }
202 // Reserve a slot in the PLT for an existing symbol in an incremental update.
204 reserve_slot(unsigned int plt_index
)
206 this->free_list_
.remove((plt_index
+ 1) * this->get_plt_entry_size(),
207 (plt_index
+ 2) * this->get_plt_entry_size());
210 // Return the PLT address to use for a global symbol.
212 address_for_global(const Symbol
* sym
)
213 { return do_address_for_global(sym
); }
215 // Return the PLT address to use for a local symbol.
217 address_for_local(const Relobj
* obj
, unsigned int symndx
)
218 { return do_address_for_local(obj
, symndx
); }
220 // Add .eh_frame information for the PLT.
222 add_eh_frame(Layout
* layout
)
223 { this->do_add_eh_frame(layout
); }
226 Output_data_got
<64, false>*
228 { return this->got_
; }
230 Output_data_got_plt_x86_64
*
232 { return this->got_plt_
; }
235 got_irelative() const
236 { return this->got_irelative_
; }
238 // Fill in the first PLT entry.
240 fill_first_plt_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 { this->do_fill_first_plt_entry(pov
, got_address
, plt_address
); }
245 // Fill in a normal PLT entry. Returns the offset into the entry that
246 // should be the initial GOT slot value.
248 fill_plt_entry(unsigned char* pov
,
249 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
250 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
251 unsigned int got_offset
,
252 unsigned int plt_offset
,
253 unsigned int plt_index
)
255 return this->do_fill_plt_entry(pov
, got_address
, plt_address
,
256 got_offset
, plt_offset
, plt_index
);
259 // Fill in the reserved TLSDESC PLT entry.
261 fill_tlsdesc_entry(unsigned char* pov
,
262 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
263 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
264 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
265 unsigned int tlsdesc_got_offset
,
266 unsigned int plt_offset
)
268 this->do_fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
269 tlsdesc_got_offset
, plt_offset
);
273 do_get_plt_entry_size() const = 0;
276 do_fill_first_plt_entry(unsigned char* pov
,
277 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
278 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
)
282 do_fill_plt_entry(unsigned char* pov
,
283 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
284 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
285 unsigned int got_offset
,
286 unsigned int plt_offset
,
287 unsigned int plt_index
) = 0;
290 do_fill_tlsdesc_entry(unsigned char* pov
,
291 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
292 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
293 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
294 unsigned int tlsdesc_got_offset
,
295 unsigned int plt_offset
) = 0;
297 // Return the PLT address to use for a global symbol.
299 do_address_for_global(const Symbol
* sym
);
301 // Return the PLT address to use for a local symbol.
303 do_address_for_local(const Relobj
* obj
, unsigned int symndx
);
306 do_add_eh_frame(Layout
* layout
) = 0;
309 do_adjust_output_section(Output_section
* os
);
311 // Write to a map file.
313 do_print_to_mapfile(Mapfile
* mapfile
) const
314 { mapfile
->print_output_data(this, _("** PLT")); }
316 // The CIE of the .eh_frame unwind information for the PLT.
317 static const int plt_eh_frame_cie_size
= 16;
318 static const unsigned char plt_eh_frame_cie
[plt_eh_frame_cie_size
];
321 // Set the final size.
323 set_final_data_size();
325 // Write out the PLT data.
327 do_write(Output_file
*);
329 // The reloc section.
331 // The TLSDESC relocs, if necessary. These must follow the regular
333 Reloc_section
* tlsdesc_rel_
;
334 // The IRELATIVE relocs, if necessary. These must follow the
335 // regular PLT relocations and the TLSDESC relocations.
336 Reloc_section
* irelative_rel_
;
338 Output_data_got
<64, false>* got_
;
339 // The .got.plt section.
340 Output_data_got_plt_x86_64
* got_plt_
;
341 // The part of the .got.plt section used for IRELATIVE relocs.
342 Output_data_space
* got_irelative_
;
343 // The number of PLT entries.
345 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These
346 // follow the regular PLT entries.
347 unsigned int irelative_count_
;
348 // Offset of the reserved TLSDESC_GOT entry when needed.
349 unsigned int tlsdesc_got_offset_
;
350 // List of available regions within the section, for incremental
352 Free_list free_list_
;
356 class Output_data_plt_x86_64_standard
: public Output_data_plt_x86_64
<size
>
359 Output_data_plt_x86_64_standard(Layout
* layout
,
360 Output_data_got
<64, false>* got
,
361 Output_data_got_plt_x86_64
* got_plt
,
362 Output_data_space
* got_irelative
)
363 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
364 got
, got_plt
, got_irelative
)
367 Output_data_plt_x86_64_standard(Layout
* layout
,
368 Output_data_got
<64, false>* got
,
369 Output_data_got_plt_x86_64
* got_plt
,
370 Output_data_space
* got_irelative
,
371 unsigned int plt_count
)
372 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
373 got
, got_plt
, got_irelative
,
379 do_get_plt_entry_size() const
380 { return plt_entry_size
; }
383 do_add_eh_frame(Layout
* layout
)
385 layout
->add_eh_frame_for_plt(this,
386 this->plt_eh_frame_cie
,
387 this->plt_eh_frame_cie_size
,
389 plt_eh_frame_fde_size
);
393 do_fill_first_plt_entry(unsigned char* pov
,
394 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
395 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
398 do_fill_plt_entry(unsigned char* pov
,
399 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
400 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
401 unsigned int got_offset
,
402 unsigned int plt_offset
,
403 unsigned int plt_index
);
406 do_fill_tlsdesc_entry(unsigned char* pov
,
407 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
408 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
409 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
410 unsigned int tlsdesc_got_offset
,
411 unsigned int plt_offset
);
414 // The size of an entry in the PLT.
415 static const int plt_entry_size
= 16;
417 // The first entry in the PLT.
418 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
419 // procedure linkage table for both programs and shared objects."
420 static const unsigned char first_plt_entry
[plt_entry_size
];
422 // Other entries in the PLT for an executable.
423 static const unsigned char plt_entry
[plt_entry_size
];
425 // The reserved TLSDESC entry in the PLT for an executable.
426 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
428 // The .eh_frame unwind information for the PLT.
429 static const int plt_eh_frame_fde_size
= 32;
430 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
433 class Output_data_plt_x86_64_bnd
: public Output_data_plt_x86_64
<64>
436 Output_data_plt_x86_64_bnd(Layout
* layout
,
437 Output_data_got
<64, false>* got
,
438 Output_data_got_plt_x86_64
* got_plt
,
439 Output_data_space
* got_irelative
)
440 : Output_data_plt_x86_64
<64>(layout
, plt_entry_size
,
441 got
, got_plt
, got_irelative
),
445 Output_data_plt_x86_64_bnd(Layout
* layout
,
446 Output_data_got
<64, false>* got
,
447 Output_data_got_plt_x86_64
* got_plt
,
448 Output_data_space
* got_irelative
,
449 unsigned int plt_count
)
450 : Output_data_plt_x86_64
<64>(layout
, plt_entry_size
,
451 got
, got_plt
, got_irelative
,
458 do_get_plt_entry_size() const
459 { return plt_entry_size
; }
461 // Return the PLT address to use for a global symbol.
463 do_address_for_global(const Symbol
*);
465 // Return the PLT address to use for a local symbol.
467 do_address_for_local(const Relobj
*, unsigned int symndx
);
470 do_add_eh_frame(Layout
* layout
)
472 layout
->add_eh_frame_for_plt(this,
473 this->plt_eh_frame_cie
,
474 this->plt_eh_frame_cie_size
,
476 plt_eh_frame_fde_size
);
480 do_fill_first_plt_entry(unsigned char* pov
,
481 elfcpp::Elf_types
<64>::Elf_Addr got_addr
,
482 elfcpp::Elf_types
<64>::Elf_Addr plt_addr
);
485 do_fill_plt_entry(unsigned char* pov
,
486 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
487 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
488 unsigned int got_offset
,
489 unsigned int plt_offset
,
490 unsigned int plt_index
);
493 do_fill_tlsdesc_entry(unsigned char* pov
,
494 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
495 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
496 elfcpp::Elf_types
<64>::Elf_Addr got_base
,
497 unsigned int tlsdesc_got_offset
,
498 unsigned int plt_offset
);
501 fill_aplt_entry(unsigned char* pov
,
502 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
503 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
504 unsigned int got_offset
,
505 unsigned int plt_offset
,
506 unsigned int plt_index
);
509 // Set the final size.
511 set_final_data_size();
513 // Write out the BND PLT data.
515 do_write(Output_file
*);
517 // Offset of the Additional PLT (if using -z bndplt).
518 unsigned int aplt_offset_
;
520 // The size of an entry in the PLT.
521 static const int plt_entry_size
= 16;
523 // The size of an entry in the additional PLT.
524 static const int aplt_entry_size
= 8;
526 // The first entry in the PLT.
527 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
528 // procedure linkage table for both programs and shared objects."
529 static const unsigned char first_plt_entry
[plt_entry_size
];
531 // Other entries in the PLT for an executable.
532 static const unsigned char plt_entry
[plt_entry_size
];
534 // Entries in the additional PLT.
535 static const unsigned char aplt_entry
[aplt_entry_size
];
537 // The reserved TLSDESC entry in the PLT for an executable.
538 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
540 // The .eh_frame unwind information for the PLT.
541 static const int plt_eh_frame_fde_size
= 32;
542 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
545 // We use this PLT when Indirect Branch Tracking (IBT) is enabled.
548 class Output_data_plt_x86_64_ibt
: public Output_data_plt_x86_64
<size
>
551 Output_data_plt_x86_64_ibt(Layout
* layout
,
552 Output_data_got
<64, false>* got
,
553 Output_data_got_plt_x86_64
* got_plt
,
554 Output_data_space
* got_irelative
)
555 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
556 got
, got_plt
, got_irelative
),
560 Output_data_plt_x86_64_ibt(Layout
* layout
,
561 Output_data_got
<64, false>* got
,
562 Output_data_got_plt_x86_64
* got_plt
,
563 Output_data_space
* got_irelative
,
564 unsigned int plt_count
)
565 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
566 got
, got_plt
, got_irelative
,
573 do_get_plt_entry_size() const
574 { return plt_entry_size
; }
576 // Return the PLT address to use for a global symbol.
578 do_address_for_global(const Symbol
*);
580 // Return the PLT address to use for a local symbol.
582 do_address_for_local(const Relobj
*, unsigned int symndx
);
585 do_add_eh_frame(Layout
* layout
)
587 layout
->add_eh_frame_for_plt(this,
588 this->plt_eh_frame_cie
,
589 this->plt_eh_frame_cie_size
,
591 plt_eh_frame_fde_size
);
595 do_fill_first_plt_entry(unsigned char* pov
,
596 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
597 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
600 do_fill_plt_entry(unsigned char* pov
,
601 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
602 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
603 unsigned int got_offset
,
604 unsigned int plt_offset
,
605 unsigned int plt_index
);
608 do_fill_tlsdesc_entry(unsigned char* pov
,
609 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
610 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
611 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
612 unsigned int tlsdesc_got_offset
,
613 unsigned int plt_offset
);
616 fill_aplt_entry(unsigned char* pov
,
617 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
618 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
619 unsigned int got_offset
,
620 unsigned int plt_offset
,
621 unsigned int plt_index
);
624 // Set the final size.
626 set_final_data_size();
628 // Write out the BND PLT data.
630 do_write(Output_file
*);
632 // Offset of the Additional PLT (if using -z bndplt).
633 unsigned int aplt_offset_
;
635 // The size of an entry in the PLT.
636 static const int plt_entry_size
= 16;
638 // The size of an entry in the additional PLT.
639 static const int aplt_entry_size
= 16;
641 // The first entry in the PLT.
642 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
643 // procedure linkage table for both programs and shared objects."
644 static const unsigned char first_plt_entry
[plt_entry_size
];
646 // Other entries in the PLT for an executable.
647 static const unsigned char plt_entry
[plt_entry_size
];
649 // Entries in the additional PLT.
650 static const unsigned char aplt_entry
[aplt_entry_size
];
652 // The reserved TLSDESC entry in the PLT for an executable.
653 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
655 // The .eh_frame unwind information for the PLT.
656 static const int plt_eh_frame_fde_size
= 32;
657 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
664 Lazy_view(Sized_relobj_file
<size
, false>* object
, unsigned int data_shndx
)
665 : object_(object
), data_shndx_(data_shndx
), view_(NULL
), view_size_(0)
669 operator[](size_t offset
)
671 if (this->view_
== NULL
)
672 this->view_
= this->object_
->section_contents(this->data_shndx_
,
675 if (offset
>= this->view_size_
)
677 return this->view_
[offset
];
681 Sized_relobj_file
<size
, false>* object_
;
682 unsigned int data_shndx_
;
683 const unsigned char* view_
;
684 section_size_type view_size_
;
687 // The x86_64 target class.
689 // http://www.x86-64.org/documentation/abi.pdf
690 // TLS info comes from
691 // http://people.redhat.com/drepper/tls.pdf
692 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
695 class Target_x86_64
: public Sized_target
<size
, false>
698 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
699 // uses only Elf64_Rela relocation entries with explicit addends."
700 typedef Output_data_reloc
<elfcpp::SHT_RELA
, true, size
, false> Reloc_section
;
702 Target_x86_64(const Target::Target_info
* info
= &x86_64_info
)
703 : Sized_target
<size
, false>(info
),
704 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_irelative_(NULL
),
705 got_tlsdesc_(NULL
), global_offset_table_(NULL
), rela_dyn_(NULL
),
706 rela_irelative_(NULL
), copy_relocs_(elfcpp::R_X86_64_COPY
),
707 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
708 tls_base_symbol_defined_(false), isa_1_used_(0), isa_1_needed_(0),
709 feature_1_(0), object_isa_1_used_(0), object_feature_1_(0),
710 seen_first_object_(false)
713 // Hook for a new output section.
715 do_new_output_section(Output_section
*) const;
717 // Scan the relocations to look for symbol adjustments.
719 gc_process_relocs(Symbol_table
* symtab
,
721 Sized_relobj_file
<size
, false>* object
,
722 unsigned int data_shndx
,
723 unsigned int sh_type
,
724 const unsigned char* prelocs
,
726 Output_section
* output_section
,
727 bool needs_special_offset_handling
,
728 size_t local_symbol_count
,
729 const unsigned char* plocal_symbols
);
731 // Scan the relocations to look for symbol adjustments.
733 scan_relocs(Symbol_table
* symtab
,
735 Sized_relobj_file
<size
, false>* object
,
736 unsigned int data_shndx
,
737 unsigned int sh_type
,
738 const unsigned char* prelocs
,
740 Output_section
* output_section
,
741 bool needs_special_offset_handling
,
742 size_t local_symbol_count
,
743 const unsigned char* plocal_symbols
);
745 // Finalize the sections.
747 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
749 // Return the value to use for a dynamic which requires special
752 do_dynsym_value(const Symbol
*) const;
754 // Relocate a section.
756 relocate_section(const Relocate_info
<size
, false>*,
757 unsigned int sh_type
,
758 const unsigned char* prelocs
,
760 Output_section
* output_section
,
761 bool needs_special_offset_handling
,
763 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
764 section_size_type view_size
,
765 const Reloc_symbol_changes
*);
767 // Scan the relocs during a relocatable link.
769 scan_relocatable_relocs(Symbol_table
* symtab
,
771 Sized_relobj_file
<size
, false>* object
,
772 unsigned int data_shndx
,
773 unsigned int sh_type
,
774 const unsigned char* prelocs
,
776 Output_section
* output_section
,
777 bool needs_special_offset_handling
,
778 size_t local_symbol_count
,
779 const unsigned char* plocal_symbols
,
780 Relocatable_relocs
*);
782 // Scan the relocs for --emit-relocs.
784 emit_relocs_scan(Symbol_table
* symtab
,
786 Sized_relobj_file
<size
, false>* object
,
787 unsigned int data_shndx
,
788 unsigned int sh_type
,
789 const unsigned char* prelocs
,
791 Output_section
* output_section
,
792 bool needs_special_offset_handling
,
793 size_t local_symbol_count
,
794 const unsigned char* plocal_syms
,
795 Relocatable_relocs
* rr
);
797 // Emit relocations for a section.
800 const Relocate_info
<size
, false>*,
801 unsigned int sh_type
,
802 const unsigned char* prelocs
,
804 Output_section
* output_section
,
805 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
807 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
808 section_size_type view_size
,
809 unsigned char* reloc_view
,
810 section_size_type reloc_view_size
);
812 // Return a string used to fill a code section with nops.
814 do_code_fill(section_size_type length
) const;
816 // Return whether SYM is defined by the ABI.
818 do_is_defined_by_abi(const Symbol
* sym
) const
819 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
821 // Return the symbol index to use for a target specific relocation.
822 // The only target specific relocation is R_X86_64_TLSDESC for a
823 // local symbol, which is an absolute reloc.
825 do_reloc_symbol_index(void*, unsigned int r_type
) const
827 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
831 // Return the addend to use for a target specific relocation.
833 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
835 // Return the PLT section.
837 do_plt_address_for_global(const Symbol
* gsym
) const
838 { return this->plt_section()->address_for_global(gsym
); }
841 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
842 { return this->plt_section()->address_for_local(relobj
, symndx
); }
844 // This function should be defined in targets that can use relocation
845 // types to determine (implemented in local_reloc_may_be_function_pointer
846 // and global_reloc_may_be_function_pointer)
847 // if a function's pointer is taken. ICF uses this in safe mode to only
848 // fold those functions whose pointer is defintely not taken. For x86_64
849 // pie binaries, safe ICF cannot be done by looking at only relocation
850 // types, and for certain cases (e.g. R_X86_64_PC32), the instruction
851 // opcode is checked as well to distinguish a function call from taking
852 // a function's pointer.
854 do_can_check_for_function_pointers() const
857 // Return the base for a DW_EH_PE_datarel encoding.
859 do_ehframe_datarel_base() const;
861 // Adjust -fsplit-stack code which calls non-split-stack code.
863 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
864 section_offset_type fnoffset
, section_size_type fnsize
,
865 const unsigned char* prelocs
, size_t reloc_count
,
866 unsigned char* view
, section_size_type view_size
,
867 std::string
* from
, std::string
* to
) const;
869 // Return the size of the GOT section.
873 gold_assert(this->got_
!= NULL
);
874 return this->got_
->data_size();
877 // Return the number of entries in the GOT.
879 got_entry_count() const
881 if (this->got_
== NULL
)
883 return this->got_size() / 8;
886 // Return the number of entries in the PLT.
888 plt_entry_count() const;
890 // Return the offset of the first non-reserved PLT entry.
892 first_plt_entry_offset() const;
894 // Return the size of each PLT entry.
896 plt_entry_size() const;
898 // Return the size of each GOT entry.
900 got_entry_size() const
903 // Create the GOT section for an incremental update.
904 Output_data_got_base
*
905 init_got_plt_for_update(Symbol_table
* symtab
,
907 unsigned int got_count
,
908 unsigned int plt_count
);
910 // Reserve a GOT entry for a local symbol, and regenerate any
911 // necessary dynamic relocations.
913 reserve_local_got_entry(unsigned int got_index
,
914 Sized_relobj
<size
, false>* obj
,
916 unsigned int got_type
);
918 // Reserve a GOT entry for a global symbol, and regenerate any
919 // necessary dynamic relocations.
921 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
922 unsigned int got_type
);
924 // Register an existing PLT entry for a global symbol.
926 register_global_plt_entry(Symbol_table
*, Layout
*, unsigned int plt_index
,
929 // Force a COPY relocation for a given symbol.
931 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
933 // Apply an incremental relocation.
935 apply_relocation(const Relocate_info
<size
, false>* relinfo
,
936 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
938 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
941 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
942 section_size_type view_size
);
944 // Add a new reloc argument, returning the index in the vector.
946 add_tlsdesc_info(Sized_relobj_file
<size
, false>* object
, unsigned int r_sym
)
948 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
949 return this->tlsdesc_reloc_info_
.size() - 1;
952 Output_data_plt_x86_64
<size
>*
953 make_data_plt(Layout
* layout
,
954 Output_data_got
<64, false>* got
,
955 Output_data_got_plt_x86_64
* got_plt
,
956 Output_data_space
* got_irelative
)
958 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
961 Output_data_plt_x86_64
<size
>*
962 make_data_plt(Layout
* layout
,
963 Output_data_got
<64, false>* got
,
964 Output_data_got_plt_x86_64
* got_plt
,
965 Output_data_space
* got_irelative
,
966 unsigned int plt_count
)
968 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
,
972 virtual Output_data_plt_x86_64
<size
>*
973 do_make_data_plt(Layout
* layout
,
974 Output_data_got
<64, false>* got
,
975 Output_data_got_plt_x86_64
* got_plt
,
976 Output_data_space
* got_irelative
);
978 virtual Output_data_plt_x86_64
<size
>*
979 do_make_data_plt(Layout
* layout
,
980 Output_data_got
<64, false>* got
,
981 Output_data_got_plt_x86_64
* got_plt
,
982 Output_data_space
* got_irelative
,
983 unsigned int plt_count
);
986 // The class which scans relocations.
991 : issued_non_pic_error_(false)
995 get_reference_flags(unsigned int r_type
);
998 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
999 Sized_relobj_file
<size
, false>* object
,
1000 unsigned int data_shndx
,
1001 Output_section
* output_section
,
1002 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
1003 const elfcpp::Sym
<size
, false>& lsym
,
1007 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
1008 Sized_relobj_file
<size
, false>* object
,
1009 unsigned int data_shndx
,
1010 Output_section
* output_section
,
1011 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
1015 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
1016 Target_x86_64
* target
,
1017 Sized_relobj_file
<size
, false>* object
,
1018 unsigned int data_shndx
,
1019 Output_section
* output_section
,
1020 const elfcpp::Rela
<size
, false>& reloc
,
1021 unsigned int r_type
,
1022 const elfcpp::Sym
<size
, false>& lsym
);
1025 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
1026 Target_x86_64
* target
,
1027 Sized_relobj_file
<size
, false>* object
,
1028 unsigned int data_shndx
,
1029 Output_section
* output_section
,
1030 const elfcpp::Rela
<size
, false>& reloc
,
1031 unsigned int r_type
,
1036 unsupported_reloc_local(Sized_relobj_file
<size
, false>*,
1037 unsigned int r_type
);
1040 unsupported_reloc_global(Sized_relobj_file
<size
, false>*,
1041 unsigned int r_type
, Symbol
*);
1044 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
1047 possible_function_pointer_reloc(Sized_relobj_file
<size
, false>* src_obj
,
1048 unsigned int src_indx
,
1049 unsigned int r_offset
,
1050 unsigned int r_type
);
1053 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, false>*,
1054 unsigned int r_type
);
1056 // Whether we have issued an error about a non-PIC compilation.
1057 bool issued_non_pic_error_
;
1060 // The class which implements relocation.
1065 : skip_call_tls_get_addr_(false)
1070 if (this->skip_call_tls_get_addr_
)
1072 // FIXME: This needs to specify the location somehow.
1073 gold_error(_("missing expected TLS relocation"));
1077 // Do a relocation. Return false if the caller should not issue
1078 // any warnings about this relocation.
1080 relocate(const Relocate_info
<size
, false>*, unsigned int,
1081 Target_x86_64
*, Output_section
*, size_t, const unsigned char*,
1082 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
1083 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1087 // Do a TLS relocation.
1089 relocate_tls(const Relocate_info
<size
, false>*, Target_x86_64
*,
1090 size_t relnum
, const elfcpp::Rela
<size
, false>&,
1091 unsigned int r_type
, const Sized_symbol
<size
>*,
1092 const Symbol_value
<size
>*,
1093 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1096 // Do a TLS General-Dynamic to Initial-Exec transition.
1098 tls_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
1099 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1100 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1101 unsigned char* view
,
1102 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1103 section_size_type view_size
);
1105 // Do a TLS General-Dynamic to Local-Exec transition.
1107 tls_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1108 Output_segment
* tls_segment
,
1109 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1110 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1111 unsigned char* view
,
1112 section_size_type view_size
);
1114 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
1116 tls_desc_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
1117 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1118 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1119 unsigned char* view
,
1120 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1121 section_size_type view_size
);
1123 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
1125 tls_desc_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1126 Output_segment
* tls_segment
,
1127 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1128 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1129 unsigned char* view
,
1130 section_size_type view_size
);
1132 // Do a TLS Local-Dynamic to Local-Exec transition.
1134 tls_ld_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1135 Output_segment
* tls_segment
,
1136 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1137 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1138 unsigned char* view
,
1139 section_size_type view_size
);
1141 // Do a TLS Initial-Exec to Local-Exec transition.
1143 tls_ie_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1144 Output_segment
* tls_segment
,
1145 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1146 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1147 unsigned char* view
,
1148 section_size_type view_size
);
1150 // This is set if we should skip the next reloc, which should be a
1151 // PLT32 reloc against ___tls_get_addr.
1152 bool skip_call_tls_get_addr_
;
1155 // Check if relocation against this symbol is a candidate for
1157 // mov foo@GOTPCREL(%rip), %reg
1158 // to lea foo(%rip), %reg.
1159 template<class View_type
>
1161 can_convert_mov_to_lea(const Symbol
* gsym
, unsigned int r_type
,
1162 size_t r_offset
, View_type
* view
)
1164 gold_assert(gsym
!= NULL
);
1165 // We cannot do the conversion unless it's one of these relocations.
1166 if (r_type
!= elfcpp::R_X86_64_GOTPCREL
1167 && r_type
!= elfcpp::R_X86_64_GOTPCRELX
1168 && r_type
!= elfcpp::R_X86_64_REX_GOTPCRELX
)
1170 // We cannot convert references to IFUNC symbols, or to symbols that
1171 // are not local to the current module.
1172 // We can't do predefined symbols because they may become undefined
1173 // (e.g., __ehdr_start when the headers aren't mapped to a segment).
1174 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1175 || gsym
->is_undefined()
1176 || gsym
->is_predefined()
1177 || gsym
->is_from_dynobj()
1178 || gsym
->is_preemptible())
1180 // If we are building a shared object and the symbol is protected, we may
1181 // need to go through the GOT.
1182 if (parameters
->options().shared()
1183 && gsym
->visibility() == elfcpp::STV_PROTECTED
)
1185 // We cannot convert references to the _DYNAMIC symbol.
1186 if (strcmp(gsym
->name(), "_DYNAMIC") == 0)
1188 // Check for a MOV opcode.
1189 return (*view
)[r_offset
- 2] == 0x8b;
1193 // callq *foo@GOTPCRELX(%rip) to
1195 // and jmpq *foo@GOTPCRELX(%rip) to
1198 template<class View_type
>
1200 can_convert_callq_to_direct(const Symbol
* gsym
, unsigned int r_type
,
1201 size_t r_offset
, View_type
* view
)
1203 gold_assert(gsym
!= NULL
);
1204 // We cannot do the conversion unless it's a GOTPCRELX relocation.
1205 if (r_type
!= elfcpp::R_X86_64_GOTPCRELX
)
1207 // We cannot convert references to IFUNC symbols, or to symbols that
1208 // are not local to the current module.
1209 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1210 || gsym
->is_undefined ()
1211 || gsym
->is_from_dynobj()
1212 || gsym
->is_preemptible())
1214 // Check for a CALLQ or JMPQ opcode.
1215 return ((*view
)[r_offset
- 2] == 0xff
1216 && ((*view
)[r_offset
- 1] == 0x15
1217 || (*view
)[r_offset
- 1] == 0x25));
1220 // Adjust TLS relocation type based on the options and whether this
1221 // is a local symbol.
1222 static tls::Tls_optimization
1223 optimize_tls_reloc(bool is_final
, int r_type
);
1225 // Get the GOT section, creating it if necessary.
1226 Output_data_got
<64, false>*
1227 got_section(Symbol_table
*, Layout
*);
1229 // Get the GOT PLT section.
1230 Output_data_got_plt_x86_64
*
1231 got_plt_section() const
1233 gold_assert(this->got_plt_
!= NULL
);
1234 return this->got_plt_
;
1237 // Get the GOT section for TLSDESC entries.
1238 Output_data_got
<64, false>*
1239 got_tlsdesc_section() const
1241 gold_assert(this->got_tlsdesc_
!= NULL
);
1242 return this->got_tlsdesc_
;
1245 // Create the PLT section.
1247 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
1249 // Create a PLT entry for a global symbol.
1251 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1253 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
1255 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1256 Sized_relobj_file
<size
, false>* relobj
,
1257 unsigned int local_sym_index
);
1259 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1261 define_tls_base_symbol(Symbol_table
*, Layout
*);
1263 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1265 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
1267 // Create a GOT entry for the TLS module index.
1269 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1270 Sized_relobj_file
<size
, false>* object
);
1272 // Get the PLT section.
1273 Output_data_plt_x86_64
<size
>*
1276 gold_assert(this->plt_
!= NULL
);
1280 // Get the dynamic reloc section, creating it if necessary.
1282 rela_dyn_section(Layout
*);
1284 // Get the section to use for TLSDESC relocations.
1286 rela_tlsdesc_section(Layout
*) const;
1288 // Get the section to use for IRELATIVE relocations.
1290 rela_irelative_section(Layout
*);
1292 // Add a potential copy relocation.
1294 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1295 Sized_relobj_file
<size
, false>* object
,
1296 unsigned int shndx
, Output_section
* output_section
,
1297 Symbol
* sym
, const elfcpp::Rela
<size
, false>& reloc
)
1299 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1300 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1301 symtab
->get_sized_symbol
<size
>(sym
),
1302 object
, shndx
, output_section
,
1303 r_type
, reloc
.get_r_offset(),
1304 reloc
.get_r_addend(),
1305 this->rela_dyn_section(layout
));
1308 // Record a target-specific program property in the .note.gnu.property
1311 record_gnu_property(unsigned int, unsigned int, size_t,
1312 const unsigned char*, const Object
*);
1314 // Merge the target-specific program properties from the current object.
1316 merge_gnu_properties(const Object
*);
1318 // Finalize the target-specific program properties and add them back to
1321 do_finalize_gnu_properties(Layout
*) const;
1323 // Information about this specific target which we pass to the
1324 // general Target structure.
1325 static const Target::Target_info x86_64_info
;
1327 // The types of GOT entries needed for this platform.
1328 // These values are exposed to the ABI in an incremental link.
1329 // Do not renumber existing values without changing the version
1330 // number of the .gnu_incremental_inputs section.
1333 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
1334 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
1335 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
1336 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
1339 // This type is used as the argument to the target specific
1340 // relocation routines. The only target specific reloc is
1341 // R_X86_64_TLSDESC against a local symbol.
1344 Tlsdesc_info(Sized_relobj_file
<size
, false>* a_object
, unsigned int a_r_sym
)
1345 : object(a_object
), r_sym(a_r_sym
)
1348 // The object in which the local symbol is defined.
1349 Sized_relobj_file
<size
, false>* object
;
1350 // The local symbol index in the object.
1355 Output_data_got
<64, false>* got_
;
1357 Output_data_plt_x86_64
<size
>* plt_
;
1358 // The GOT PLT section.
1359 Output_data_got_plt_x86_64
* got_plt_
;
1360 // The GOT section for IRELATIVE relocations.
1361 Output_data_space
* got_irelative_
;
1362 // The GOT section for TLSDESC relocations.
1363 Output_data_got
<64, false>* got_tlsdesc_
;
1364 // The _GLOBAL_OFFSET_TABLE_ symbol.
1365 Symbol
* global_offset_table_
;
1366 // The dynamic reloc section.
1367 Reloc_section
* rela_dyn_
;
1368 // The section to use for IRELATIVE relocs.
1369 Reloc_section
* rela_irelative_
;
1370 // Relocs saved to avoid a COPY reloc.
1371 Copy_relocs
<elfcpp::SHT_RELA
, size
, false> copy_relocs_
;
1372 // Offset of the GOT entry for the TLS module index.
1373 unsigned int got_mod_index_offset_
;
1374 // We handle R_X86_64_TLSDESC against a local symbol as a target
1375 // specific relocation. Here we store the object and local symbol
1376 // index for the relocation.
1377 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
1378 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1379 bool tls_base_symbol_defined_
;
1380 // Target-specific program properties, from .note.gnu.property section.
1381 // Each bit represents a specific feature.
1382 uint32_t isa_1_used_
;
1383 uint32_t isa_1_needed_
;
1384 uint32_t feature_1_
;
1385 // Target-specific properties from the current object.
1386 // These bits get ORed into ISA_1_USED_ after all properties for the object
1387 // have been processed. But if either is all zeroes (as when the property
1388 // is absent from an object), the result should be all zeroes.
1389 // (See PR ld/23486.)
1390 uint32_t object_isa_1_used_
;
1391 // These bits get ANDed into FEATURE_1_ after all properties for the object
1392 // have been processed.
1393 uint32_t object_feature_1_
;
1394 // Whether we have seen our first object, for use in initializing FEATURE_1_.
1395 bool seen_first_object_
;
1399 const Target::Target_info Target_x86_64
<64>::x86_64_info
=
1402 false, // is_big_endian
1403 elfcpp::EM_X86_64
, // machine_code
1404 false, // has_make_symbol
1405 false, // has_resolve
1406 true, // has_code_fill
1407 true, // is_default_stack_executable
1408 true, // can_icf_inline_merge_sections
1410 "/lib/ld64.so.1", // program interpreter
1411 0x400000, // default_text_segment_address
1412 0x1000, // abi_pagesize (overridable by -z max-page-size)
1413 0x1000, // common_pagesize (overridable by -z common-page-size)
1414 false, // isolate_execinstr
1416 elfcpp::SHN_UNDEF
, // small_common_shndx
1417 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1418 0, // small_common_section_flags
1419 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1420 NULL
, // attributes_section
1421 NULL
, // attributes_vendor
1422 "_start", // entry_symbol_name
1423 32, // hash_entry_size
1424 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
1428 const Target::Target_info Target_x86_64
<32>::x86_64_info
=
1431 false, // is_big_endian
1432 elfcpp::EM_X86_64
, // machine_code
1433 false, // has_make_symbol
1434 false, // has_resolve
1435 true, // has_code_fill
1436 true, // is_default_stack_executable
1437 true, // can_icf_inline_merge_sections
1439 "/libx32/ldx32.so.1", // program interpreter
1440 0x400000, // default_text_segment_address
1441 0x1000, // abi_pagesize (overridable by -z max-page-size)
1442 0x1000, // common_pagesize (overridable by -z common-page-size)
1443 false, // isolate_execinstr
1445 elfcpp::SHN_UNDEF
, // small_common_shndx
1446 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1447 0, // small_common_section_flags
1448 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1449 NULL
, // attributes_section
1450 NULL
, // attributes_vendor
1451 "_start", // entry_symbol_name
1452 32, // hash_entry_size
1453 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
1456 // This is called when a new output section is created. This is where
1457 // we handle the SHF_X86_64_LARGE.
1461 Target_x86_64
<size
>::do_new_output_section(Output_section
* os
) const
1463 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
1464 os
->set_is_large_section();
1467 // Get the GOT section, creating it if necessary.
1470 Output_data_got
<64, false>*
1471 Target_x86_64
<size
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
1473 if (this->got_
== NULL
)
1475 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1477 // When using -z now, we can treat .got.plt as a relro section.
1478 // Without -z now, it is modified after program startup by lazy
1480 bool is_got_plt_relro
= parameters
->options().now();
1481 Output_section_order got_order
= (is_got_plt_relro
1483 : ORDER_RELRO_LAST
);
1484 Output_section_order got_plt_order
= (is_got_plt_relro
1486 : ORDER_NON_RELRO_FIRST
);
1488 this->got_
= new Output_data_got
<64, false>();
1490 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1492 | elfcpp::SHF_WRITE
),
1493 this->got_
, got_order
, true);
1495 this->got_plt_
= new Output_data_got_plt_x86_64(layout
);
1496 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1498 | elfcpp::SHF_WRITE
),
1499 this->got_plt_
, got_plt_order
,
1502 // The first three entries are reserved.
1503 this->got_plt_
->set_current_data_size(3 * 8);
1505 if (!is_got_plt_relro
)
1507 // Those bytes can go into the relro segment.
1508 layout
->increase_relro(3 * 8);
1511 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1512 this->global_offset_table_
=
1513 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1514 Symbol_table::PREDEFINED
,
1516 0, 0, elfcpp::STT_OBJECT
,
1518 elfcpp::STV_HIDDEN
, 0,
1521 // If there are any IRELATIVE relocations, they get GOT entries
1522 // in .got.plt after the jump slot entries.
1523 this->got_irelative_
= new Output_data_space(8, "** GOT IRELATIVE PLT");
1524 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1526 | elfcpp::SHF_WRITE
),
1527 this->got_irelative_
,
1528 got_plt_order
, is_got_plt_relro
);
1530 // If there are any TLSDESC relocations, they get GOT entries in
1531 // .got.plt after the jump slot and IRELATIVE entries.
1532 this->got_tlsdesc_
= new Output_data_got
<64, false>();
1533 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1535 | elfcpp::SHF_WRITE
),
1537 got_plt_order
, is_got_plt_relro
);
1543 // Get the dynamic reloc section, creating it if necessary.
1546 typename Target_x86_64
<size
>::Reloc_section
*
1547 Target_x86_64
<size
>::rela_dyn_section(Layout
* layout
)
1549 if (this->rela_dyn_
== NULL
)
1551 gold_assert(layout
!= NULL
);
1552 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1553 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1554 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1555 ORDER_DYNAMIC_RELOCS
, false);
1557 return this->rela_dyn_
;
1560 // Get the section to use for IRELATIVE relocs, creating it if
1561 // necessary. These go in .rela.dyn, but only after all other dynamic
1562 // relocations. They need to follow the other dynamic relocations so
1563 // that they can refer to global variables initialized by those
1567 typename Target_x86_64
<size
>::Reloc_section
*
1568 Target_x86_64
<size
>::rela_irelative_section(Layout
* layout
)
1570 if (this->rela_irelative_
== NULL
)
1572 // Make sure we have already created the dynamic reloc section.
1573 this->rela_dyn_section(layout
);
1574 this->rela_irelative_
= new Reloc_section(false);
1575 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1576 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
1577 ORDER_DYNAMIC_RELOCS
, false);
1578 gold_assert(this->rela_dyn_
->output_section()
1579 == this->rela_irelative_
->output_section());
1581 return this->rela_irelative_
;
1584 // Record a target-specific program property from the .note.gnu.property
1588 Target_x86_64
<size
>::record_gnu_property(
1589 unsigned int, unsigned int pr_type
,
1590 size_t pr_datasz
, const unsigned char* pr_data
,
1591 const Object
* object
)
1597 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED
:
1598 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED
:
1599 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND
:
1602 gold_warning(_("%s: corrupt .note.gnu.property section "
1603 "(pr_datasz for property %d is not 4)"),
1604 object
->name().c_str(), pr_type
);
1607 val
= elfcpp::Swap
<32, false>::readval(pr_data
);
1610 gold_warning(_("%s: unknown program property type 0x%x "
1611 "in .note.gnu.property section"),
1612 object
->name().c_str(), pr_type
);
1618 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED
:
1619 this->object_isa_1_used_
|= val
;
1621 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED
:
1622 this->isa_1_needed_
|= val
;
1624 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND
:
1625 // If we see multiple feature props in one object, OR them together.
1626 this->object_feature_1_
|= val
;
1631 // Merge the target-specific program properties from the current object.
1634 Target_x86_64
<size
>::merge_gnu_properties(const Object
*)
1636 if (this->seen_first_object_
)
1638 // If any object is missing the ISA_1_USED property, we must omit
1639 // it from the output file.
1640 if (this->object_isa_1_used_
== 0)
1641 this->isa_1_used_
= 0;
1642 else if (this->isa_1_used_
!= 0)
1643 this->isa_1_used_
|= this->object_isa_1_used_
;
1644 this->feature_1_
&= this->object_feature_1_
;
1648 this->isa_1_used_
= this->object_isa_1_used_
;
1649 this->feature_1_
= this->object_feature_1_
;
1650 this->seen_first_object_
= true;
1652 this->object_isa_1_used_
= 0;
1653 this->object_feature_1_
= 0;
1657 add_property(Layout
* layout
, unsigned int pr_type
, uint32_t val
)
1659 unsigned char buf
[4];
1660 elfcpp::Swap
<32, false>::writeval(buf
, val
);
1661 layout
->add_gnu_property(elfcpp::NT_GNU_PROPERTY_TYPE_0
, pr_type
, 4, buf
);
1664 // Finalize the target-specific program properties and add them back to
1668 Target_x86_64
<size
>::do_finalize_gnu_properties(Layout
* layout
) const
1670 if (this->isa_1_used_
!= 0)
1671 add_property(layout
, elfcpp::GNU_PROPERTY_X86_ISA_1_USED
,
1673 if (this->isa_1_needed_
!= 0)
1674 add_property(layout
, elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED
,
1675 this->isa_1_needed_
);
1676 if (this->feature_1_
!= 0)
1677 add_property(layout
, elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND
,
1681 // Write the first three reserved words of the .got.plt section.
1682 // The remainder of the section is written while writing the PLT
1683 // in Output_data_plt_i386::do_write.
1686 Output_data_got_plt_x86_64::do_write(Output_file
* of
)
1688 // The first entry in the GOT is the address of the .dynamic section
1689 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1690 // We saved space for them when we created the section in
1691 // Target_x86_64::got_section.
1692 const off_t got_file_offset
= this->offset();
1693 gold_assert(this->data_size() >= 24);
1694 unsigned char* const got_view
= of
->get_output_view(got_file_offset
, 24);
1695 Output_section
* dynamic
= this->layout_
->dynamic_section();
1696 uint64_t dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
1697 elfcpp::Swap
<64, false>::writeval(got_view
, dynamic_addr
);
1698 memset(got_view
+ 8, 0, 16);
1699 of
->write_output_view(got_file_offset
, 24, got_view
);
1702 // Initialize the PLT section.
1706 Output_data_plt_x86_64
<size
>::init(Layout
* layout
)
1708 this->rel_
= new Reloc_section(false);
1709 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1710 elfcpp::SHF_ALLOC
, this->rel_
,
1711 ORDER_DYNAMIC_PLT_RELOCS
, false);
1716 Output_data_plt_x86_64
<size
>::do_adjust_output_section(Output_section
* os
)
1718 os
->set_entsize(this->get_plt_entry_size());
1721 // Add an entry to the PLT.
1725 Output_data_plt_x86_64
<size
>::add_entry(Symbol_table
* symtab
, Layout
* layout
,
1728 gold_assert(!gsym
->has_plt_offset());
1730 unsigned int plt_index
;
1732 section_offset_type got_offset
;
1734 unsigned int* pcount
;
1735 unsigned int offset
;
1736 unsigned int reserved
;
1737 Output_section_data_build
* got
;
1738 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1739 && gsym
->can_use_relative_reloc(false))
1741 pcount
= &this->irelative_count_
;
1744 got
= this->got_irelative_
;
1748 pcount
= &this->count_
;
1751 got
= this->got_plt_
;
1754 if (!this->is_data_size_valid())
1756 // Note that when setting the PLT offset for a non-IRELATIVE
1757 // entry we skip the initial reserved PLT entry.
1758 plt_index
= *pcount
+ offset
;
1759 plt_offset
= plt_index
* this->get_plt_entry_size();
1763 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1764 gold_assert(got_offset
== got
->current_data_size());
1766 // Every PLT entry needs a GOT entry which points back to the PLT
1767 // entry (this will be changed by the dynamic linker, normally
1768 // lazily when the function is called).
1769 got
->set_current_data_size(got_offset
+ 8);
1773 // FIXME: This is probably not correct for IRELATIVE relocs.
1775 // For incremental updates, find an available slot.
1776 plt_offset
= this->free_list_
.allocate(this->get_plt_entry_size(),
1777 this->get_plt_entry_size(), 0);
1778 if (plt_offset
== -1)
1779 gold_fallback(_("out of patch space (PLT);"
1780 " relink with --incremental-full"));
1782 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1783 // can be calculated from the PLT index, adjusting for the three
1784 // reserved entries at the beginning of the GOT.
1785 plt_index
= plt_offset
/ this->get_plt_entry_size() - 1;
1786 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1789 gsym
->set_plt_offset(plt_offset
);
1791 // Every PLT entry needs a reloc.
1792 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
1794 // Note that we don't need to save the symbol. The contents of the
1795 // PLT are independent of which symbols are used. The symbols only
1796 // appear in the relocations.
1799 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1804 Output_data_plt_x86_64
<size
>::add_local_ifunc_entry(
1805 Symbol_table
* symtab
,
1807 Sized_relobj_file
<size
, false>* relobj
,
1808 unsigned int local_sym_index
)
1810 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
1811 ++this->irelative_count_
;
1813 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
1815 // Every PLT entry needs a GOT entry which points back to the PLT
1817 this->got_irelative_
->set_current_data_size(got_offset
+ 8);
1819 // Every PLT entry needs a reloc.
1820 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1821 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
1822 elfcpp::R_X86_64_IRELATIVE
,
1823 this->got_irelative_
, got_offset
, 0);
1828 // Add the relocation for a PLT entry.
1832 Output_data_plt_x86_64
<size
>::add_relocation(Symbol_table
* symtab
,
1835 unsigned int got_offset
)
1837 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1838 && gsym
->can_use_relative_reloc(false))
1840 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1841 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
1842 this->got_irelative_
, got_offset
, 0);
1846 gsym
->set_needs_dynsym_entry();
1847 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
1852 // Return where the TLSDESC relocations should go, creating it if
1853 // necessary. These follow the JUMP_SLOT relocations.
1856 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1857 Output_data_plt_x86_64
<size
>::rela_tlsdesc(Layout
* layout
)
1859 if (this->tlsdesc_rel_
== NULL
)
1861 this->tlsdesc_rel_
= new Reloc_section(false);
1862 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1863 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
1864 ORDER_DYNAMIC_PLT_RELOCS
, false);
1865 gold_assert(this->tlsdesc_rel_
->output_section()
1866 == this->rel_
->output_section());
1868 return this->tlsdesc_rel_
;
1871 // Return where the IRELATIVE relocations should go in the PLT. These
1872 // follow the JUMP_SLOT and the TLSDESC relocations.
1875 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1876 Output_data_plt_x86_64
<size
>::rela_irelative(Symbol_table
* symtab
,
1879 if (this->irelative_rel_
== NULL
)
1881 // Make sure we have a place for the TLSDESC relocations, in
1882 // case we see any later on.
1883 this->rela_tlsdesc(layout
);
1884 this->irelative_rel_
= new Reloc_section(false);
1885 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1886 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
1887 ORDER_DYNAMIC_PLT_RELOCS
, false);
1888 gold_assert(this->irelative_rel_
->output_section()
1889 == this->rel_
->output_section());
1891 if (parameters
->doing_static_link())
1893 // A statically linked executable will only have a .rela.plt
1894 // section to hold R_X86_64_IRELATIVE relocs for
1895 // STT_GNU_IFUNC symbols. The library will use these
1896 // symbols to locate the IRELATIVE relocs at program startup
1898 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
1899 Symbol_table::PREDEFINED
,
1900 this->irelative_rel_
, 0, 0,
1901 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1902 elfcpp::STV_HIDDEN
, 0, false, true);
1903 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
1904 Symbol_table::PREDEFINED
,
1905 this->irelative_rel_
, 0, 0,
1906 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1907 elfcpp::STV_HIDDEN
, 0, true, true);
1910 return this->irelative_rel_
;
1913 // Return the PLT address to use for a global symbol.
1917 Output_data_plt_x86_64
<size
>::do_address_for_global(const Symbol
* gsym
)
1919 uint64_t offset
= 0;
1920 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1921 && gsym
->can_use_relative_reloc(false))
1922 offset
= (this->count_
+ 1) * this->get_plt_entry_size();
1923 return this->address() + offset
+ gsym
->plt_offset();
1926 // Return the PLT address to use for a local symbol. These are always
1927 // IRELATIVE relocs.
1931 Output_data_plt_x86_64
<size
>::do_address_for_local(const Relobj
* object
,
1934 return (this->address()
1935 + (this->count_
+ 1) * this->get_plt_entry_size()
1936 + object
->local_plt_offset(r_sym
));
1939 // Set the final size.
1942 Output_data_plt_x86_64
<size
>::set_final_data_size()
1944 // Number of regular and IFUNC PLT entries, plus the first entry.
1945 unsigned int count
= this->count_
+ this->irelative_count_
+ 1;
1946 // Count the TLSDESC entry, if present.
1947 if (this->has_tlsdesc_entry())
1949 this->set_data_size(count
* this->get_plt_entry_size());
1952 // The first entry in the PLT for an executable.
1956 Output_data_plt_x86_64_standard
<size
>::first_plt_entry
[plt_entry_size
] =
1958 // From AMD64 ABI Draft 0.98, page 76
1959 0xff, 0x35, // pushq contents of memory address
1960 0, 0, 0, 0, // replaced with address of .got + 8
1961 0xff, 0x25, // jmp indirect
1962 0, 0, 0, 0, // replaced with address of .got + 16
1963 0x90, 0x90, 0x90, 0x90 // noop (x4)
1968 Output_data_plt_x86_64_standard
<size
>::do_fill_first_plt_entry(
1970 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1971 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
1973 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1974 // We do a jmp relative to the PC at the end of this instruction.
1975 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1977 - (plt_address
+ 6)));
1978 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1980 - (plt_address
+ 12)));
1983 // Subsequent entries in the PLT for an executable.
1987 Output_data_plt_x86_64_standard
<size
>::plt_entry
[plt_entry_size
] =
1989 // From AMD64 ABI Draft 0.98, page 76
1990 0xff, 0x25, // jmpq indirect
1991 0, 0, 0, 0, // replaced with address of symbol in .got
1992 0x68, // pushq immediate
1993 0, 0, 0, 0, // replaced with offset into relocation table
1994 0xe9, // jmpq relative
1995 0, 0, 0, 0 // replaced with offset to start of .plt
2000 Output_data_plt_x86_64_standard
<size
>::do_fill_plt_entry(
2002 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
2003 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
2004 unsigned int got_offset
,
2005 unsigned int plt_offset
,
2006 unsigned int plt_index
)
2008 // Check PC-relative offset overflow in PLT entry.
2009 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
2010 - (plt_address
+ plt_offset
+ 6));
2011 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
2012 gold_error(_("PC-relative offset overflow in PLT entry %d"),
2015 memcpy(pov
, plt_entry
, plt_entry_size
);
2016 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2017 plt_got_pcrel_offset
);
2019 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
2020 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
2021 - (plt_offset
+ plt_entry_size
));
2026 // The reserved TLSDESC entry in the PLT for an executable.
2030 Output_data_plt_x86_64_standard
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
2032 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2033 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2034 0xff, 0x35, // pushq x(%rip)
2035 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2036 0xff, 0x25, // jmpq *y(%rip)
2037 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2044 Output_data_plt_x86_64_standard
<size
>::do_fill_tlsdesc_entry(
2046 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
2047 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
2048 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
2049 unsigned int tlsdesc_got_offset
,
2050 unsigned int plt_offset
)
2052 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
2053 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2055 - (plt_address
+ plt_offset
2057 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
2059 + tlsdesc_got_offset
2060 - (plt_address
+ plt_offset
2064 // Return the APLT address to use for a global symbol (for -z bndplt).
2067 Output_data_plt_x86_64_bnd::do_address_for_global(const Symbol
* gsym
)
2069 uint64_t offset
= this->aplt_offset_
;
2070 // Convert the PLT offset into an APLT offset.
2071 unsigned int plt_offset
= gsym
->plt_offset();
2072 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2073 && gsym
->can_use_relative_reloc(false))
2074 offset
+= this->regular_count() * aplt_entry_size
;
2076 plt_offset
-= plt_entry_size
;
2077 plt_offset
= plt_offset
/ (plt_entry_size
/ aplt_entry_size
);
2078 return this->address() + offset
+ plt_offset
;
2081 // Return the PLT address to use for a local symbol. These are always
2082 // IRELATIVE relocs.
2085 Output_data_plt_x86_64_bnd::do_address_for_local(const Relobj
* object
,
2088 // Convert the PLT offset into an APLT offset.
2089 const Sized_relobj_file
<64, false>* sized_relobj
=
2090 static_cast<const Sized_relobj_file
<64, false>*>(object
);
2091 const Symbol_value
<64>* psymval
= sized_relobj
->local_symbol(r_sym
);
2092 unsigned int plt_offset
= ((object
->local_plt_offset(r_sym
)
2093 - (psymval
->is_ifunc_symbol()
2094 ? 0 : plt_entry_size
))
2095 / (plt_entry_size
/ aplt_entry_size
));
2096 return (this->address()
2097 + this->aplt_offset_
2098 + this->regular_count() * aplt_entry_size
2102 // Set the final size.
2104 Output_data_plt_x86_64_bnd::set_final_data_size()
2106 // Number of regular and IFUNC PLT entries.
2107 unsigned int count
= this->entry_count();
2108 // Count the first entry and the TLSDESC entry, if present.
2109 unsigned int extra
= this->has_tlsdesc_entry() ? 2 : 1;
2110 unsigned int plt_size
= (count
+ extra
) * plt_entry_size
;
2111 // Offset of the APLT.
2112 this->aplt_offset_
= plt_size
;
2113 // Size of the APLT.
2114 plt_size
+= count
* aplt_entry_size
;
2115 this->set_data_size(plt_size
);
2118 // The first entry in the BND PLT.
2121 Output_data_plt_x86_64_bnd::first_plt_entry
[plt_entry_size
] =
2123 // From AMD64 ABI Draft 0.98, page 76
2124 0xff, 0x35, // pushq contents of memory address
2125 0, 0, 0, 0, // replaced with address of .got + 8
2126 0xf2, 0xff, 0x25, // bnd jmp indirect
2127 0, 0, 0, 0, // replaced with address of .got + 16
2128 0x0f, 0x1f, 0x00 // nop
2132 Output_data_plt_x86_64_bnd::do_fill_first_plt_entry(
2134 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
2135 elfcpp::Elf_types
<64>::Elf_Addr plt_address
)
2137 memcpy(pov
, first_plt_entry
, plt_entry_size
);
2138 // We do a jmp relative to the PC at the end of this instruction.
2139 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2141 - (plt_address
+ 6)));
2142 elfcpp::Swap
<32, false>::writeval(pov
+ 9,
2144 - (plt_address
+ 13)));
2147 // Subsequent entries in the BND PLT.
2150 Output_data_plt_x86_64_bnd::plt_entry
[plt_entry_size
] =
2152 // From AMD64 ABI Draft 0.99.8, page 139
2153 0x68, // pushq immediate
2154 0, 0, 0, 0, // replaced with offset into relocation table
2155 0xf2, 0xe9, // bnd jmpq relative
2156 0, 0, 0, 0, // replaced with offset to start of .plt
2157 0x0f, 0x1f, 0x44, 0, 0 // nop
2160 // Entries in the BND Additional PLT.
2163 Output_data_plt_x86_64_bnd::aplt_entry
[aplt_entry_size
] =
2165 // From AMD64 ABI Draft 0.99.8, page 139
2166 0xf2, 0xff, 0x25, // bnd jmpq indirect
2167 0, 0, 0, 0, // replaced with address of symbol in .got
2172 Output_data_plt_x86_64_bnd::do_fill_plt_entry(
2174 elfcpp::Elf_types
<64>::Elf_Addr
,
2175 elfcpp::Elf_types
<64>::Elf_Addr
,
2177 unsigned int plt_offset
,
2178 unsigned int plt_index
)
2180 memcpy(pov
, plt_entry
, plt_entry_size
);
2181 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 1, plt_index
);
2182 elfcpp::Swap
<32, false>::writeval(pov
+ 7, -(plt_offset
+ 11));
2187 Output_data_plt_x86_64_bnd::fill_aplt_entry(
2189 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
2190 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
2191 unsigned int got_offset
,
2192 unsigned int plt_offset
,
2193 unsigned int plt_index
)
2195 // Check PC-relative offset overflow in PLT entry.
2196 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
2197 - (plt_address
+ plt_offset
+ 7));
2198 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
2199 gold_error(_("PC-relative offset overflow in APLT entry %d"),
2202 memcpy(pov
, aplt_entry
, aplt_entry_size
);
2203 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3, plt_got_pcrel_offset
);
2206 // The reserved TLSDESC entry in the PLT for an executable.
2209 Output_data_plt_x86_64_bnd::tlsdesc_plt_entry
[plt_entry_size
] =
2211 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2212 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2213 0xff, 0x35, // pushq x(%rip)
2214 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2215 0xf2, 0xff, 0x25, // jmpq *y(%rip)
2216 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2217 0x0f, 0x1f, 0 // nop
2221 Output_data_plt_x86_64_bnd::do_fill_tlsdesc_entry(
2223 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
2224 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
2225 elfcpp::Elf_types
<64>::Elf_Addr got_base
,
2226 unsigned int tlsdesc_got_offset
,
2227 unsigned int plt_offset
)
2229 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
2230 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2232 - (plt_address
+ plt_offset
2234 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
2236 + tlsdesc_got_offset
2237 - (plt_address
+ plt_offset
2241 // Return the APLT address to use for a global symbol (for IBT).
2245 Output_data_plt_x86_64_ibt
<size
>::do_address_for_global(const Symbol
* gsym
)
2247 uint64_t offset
= this->aplt_offset_
;
2248 // Convert the PLT offset into an APLT offset.
2249 unsigned int plt_offset
= gsym
->plt_offset();
2250 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2251 && gsym
->can_use_relative_reloc(false))
2252 offset
+= this->regular_count() * aplt_entry_size
;
2254 plt_offset
-= plt_entry_size
;
2255 plt_offset
= plt_offset
/ (plt_entry_size
/ aplt_entry_size
);
2256 return this->address() + offset
+ plt_offset
;
2259 // Return the PLT address to use for a local symbol. These are always
2260 // IRELATIVE relocs.
2264 Output_data_plt_x86_64_ibt
<size
>::do_address_for_local(const Relobj
* object
,
2267 // Convert the PLT offset into an APLT offset.
2268 const Sized_relobj_file
<size
, false>* sized_relobj
=
2269 static_cast<const Sized_relobj_file
<size
, false>*>(object
);
2270 const Symbol_value
<size
>* psymval
= sized_relobj
->local_symbol(r_sym
);
2271 unsigned int plt_offset
= ((object
->local_plt_offset(r_sym
)
2272 - (psymval
->is_ifunc_symbol()
2273 ? 0 : plt_entry_size
))
2274 / (plt_entry_size
/ aplt_entry_size
));
2275 return (this->address()
2276 + this->aplt_offset_
2277 + this->regular_count() * aplt_entry_size
2281 // Set the final size.
2285 Output_data_plt_x86_64_ibt
<size
>::set_final_data_size()
2287 // Number of regular and IFUNC PLT entries.
2288 unsigned int count
= this->entry_count();
2289 // Count the first entry and the TLSDESC entry, if present.
2290 unsigned int extra
= this->has_tlsdesc_entry() ? 2 : 1;
2291 unsigned int plt_size
= (count
+ extra
) * plt_entry_size
;
2292 // Offset of the APLT.
2293 this->aplt_offset_
= plt_size
;
2294 // Size of the APLT.
2295 plt_size
+= count
* aplt_entry_size
;
2296 this->set_data_size(plt_size
);
2299 // The first entry in the IBT PLT.
2303 Output_data_plt_x86_64_ibt
<32>::first_plt_entry
[plt_entry_size
] =
2305 // MPX isn't supported for x32, so we don't need the BND prefix.
2306 // From AMD64 ABI Draft 0.98, page 76
2307 0xff, 0x35, // pushq contents of memory address
2308 0, 0, 0, 0, // replaced with address of .got + 8
2309 0xff, 0x25, // jmp indirect
2310 0, 0, 0, 0, // replaced with address of .got + 16
2311 0x90, 0x90, 0x90, 0x90 // noop (x4)
2316 Output_data_plt_x86_64_ibt
<64>::first_plt_entry
[plt_entry_size
] =
2318 // Use the BND prefix so that IBT is compatible with MPX.
2319 0xff, 0x35, // pushq contents of memory address
2320 0, 0, 0, 0, // replaced with address of .got + 8
2321 0xf2, 0xff, 0x25, // bnd jmp indirect
2322 0, 0, 0, 0, // replaced with address of .got + 16
2323 0x0f, 0x1f, 0x00 // nop
2328 Output_data_plt_x86_64_ibt
<size
>::do_fill_first_plt_entry(
2330 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
2331 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
2333 // Offsets to the addresses needing relocation.
2334 const unsigned int roff1
= 2;
2335 const unsigned int roff2
= (size
== 32) ? 8 : 9;
2337 memcpy(pov
, first_plt_entry
, plt_entry_size
);
2338 // We do a jmp relative to the PC at the end of this instruction.
2339 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ roff1
,
2341 - (plt_address
+ roff1
+ 4)));
2342 elfcpp::Swap
<32, false>::writeval(pov
+ roff2
,
2344 - (plt_address
+ roff2
+ 4)));
2347 // Subsequent entries in the IBT PLT.
2351 Output_data_plt_x86_64_ibt
<32>::plt_entry
[plt_entry_size
] =
2353 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2354 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2355 0x68, // pushq immediate
2356 0, 0, 0, 0, // replaced with offset into relocation table
2357 0xe9, // jmpq relative
2358 0, 0, 0, 0, // replaced with offset to start of .plt
2364 Output_data_plt_x86_64_ibt
<64>::plt_entry
[plt_entry_size
] =
2366 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2367 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2368 0x68, // pushq immediate
2369 0, 0, 0, 0, // replaced with offset into relocation table
2370 0xf2, 0xe9, // bnd jmpq relative
2371 0, 0, 0, 0, // replaced with offset to start of .plt
2375 // Entries in the IBT Additional PLT.
2379 Output_data_plt_x86_64_ibt
<32>::aplt_entry
[aplt_entry_size
] =
2381 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2382 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2383 0xff, 0x25, // jmpq indirect
2384 0, 0, 0, 0, // replaced with address of symbol in .got
2385 0x0f, 0x1f, 0x04, 0x00, // nop
2391 Output_data_plt_x86_64_ibt
<64>::aplt_entry
[aplt_entry_size
] =
2393 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2394 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2395 0xf2, 0xff, 0x25, // bnd jmpq indirect
2396 0, 0, 0, 0, // replaced with address of symbol in .got
2397 0x0f, 0x1f, 0x04, 0x00, // nop
2403 Output_data_plt_x86_64_ibt
<size
>::do_fill_plt_entry(
2405 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
2406 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
2408 unsigned int plt_offset
,
2409 unsigned int plt_index
)
2411 // Offsets to the addresses needing relocation.
2412 const unsigned int roff1
= 5;
2413 const unsigned int roff2
= (size
== 32) ? 10 : 11;
2415 memcpy(pov
, plt_entry
, plt_entry_size
);
2416 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ roff1
, plt_index
);
2417 elfcpp::Swap
<32, false>::writeval(pov
+ roff2
, -(plt_offset
+ roff2
+ 4));
2423 Output_data_plt_x86_64_ibt
<size
>::fill_aplt_entry(
2425 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
2426 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
2427 unsigned int got_offset
,
2428 unsigned int plt_offset
,
2429 unsigned int plt_index
)
2431 // Offset to the address needing relocation.
2432 const unsigned int roff
= (size
== 32) ? 6 : 7;
2434 // Check PC-relative offset overflow in PLT entry.
2435 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
2436 - (plt_address
+ plt_offset
+ roff
+ 4));
2437 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
2438 gold_error(_("PC-relative offset overflow in APLT entry %d"),
2441 memcpy(pov
, aplt_entry
, aplt_entry_size
);
2442 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ roff
, plt_got_pcrel_offset
);
2445 // The reserved TLSDESC entry in the IBT PLT for an executable.
2449 Output_data_plt_x86_64_ibt
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
2451 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2452 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2453 0xff, 0x35, // pushq x(%rip)
2454 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2455 0xf2, 0xff, 0x25, // jmpq *y(%rip)
2456 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2457 0x0f, 0x1f, 0 // nop
2462 Output_data_plt_x86_64_ibt
<size
>::do_fill_tlsdesc_entry(
2464 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
2465 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
2466 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
2467 unsigned int tlsdesc_got_offset
,
2468 unsigned int plt_offset
)
2470 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
2471 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2473 - (plt_address
+ plt_offset
2475 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
2477 + tlsdesc_got_offset
2478 - (plt_address
+ plt_offset
2482 // The .eh_frame unwind information for the PLT.
2486 Output_data_plt_x86_64
<size
>::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
2489 'z', // Augmentation: augmentation size included.
2490 'R', // Augmentation: FDE encoding included.
2491 '\0', // End of augmentation string.
2492 1, // Code alignment factor.
2493 0x78, // Data alignment factor.
2494 16, // Return address column.
2495 1, // Augmentation size.
2496 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
2497 | elfcpp::DW_EH_PE_sdata4
),
2498 elfcpp::DW_CFA_def_cfa
, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
2499 elfcpp::DW_CFA_offset
+ 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
2500 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
2506 Output_data_plt_x86_64_standard
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
2508 0, 0, 0, 0, // Replaced with offset to .plt.
2509 0, 0, 0, 0, // Replaced with size of .plt.
2510 0, // Augmentation size.
2511 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
2512 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
2513 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
2514 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
2515 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
2516 11, // Block length.
2517 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
2518 elfcpp::DW_OP_breg16
, 0, // Push %rip.
2519 elfcpp::DW_OP_lit15
, // Push 0xf.
2520 elfcpp::DW_OP_and
, // & (%rip & 0xf).
2521 elfcpp::DW_OP_lit11
, // Push 0xb.
2522 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 0xb)
2523 elfcpp::DW_OP_lit3
, // Push 3.
2524 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 0xb) << 3)
2525 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
2526 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
2532 // The .eh_frame unwind information for the BND PLT.
2534 Output_data_plt_x86_64_bnd::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
2536 0, 0, 0, 0, // Replaced with offset to .plt.
2537 0, 0, 0, 0, // Replaced with size of .plt.
2538 0, // Augmentation size.
2539 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
2540 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
2541 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
2542 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
2543 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
2544 11, // Block length.
2545 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
2546 elfcpp::DW_OP_breg16
, 0, // Push %rip.
2547 elfcpp::DW_OP_lit15
, // Push 0xf.
2548 elfcpp::DW_OP_and
, // & (%rip & 0xf).
2549 elfcpp::DW_OP_lit5
, // Push 5.
2550 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 5)
2551 elfcpp::DW_OP_lit3
, // Push 3.
2552 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 5) << 3)
2553 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=5)<<3)+%rsp+8
2554 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
2560 // The .eh_frame unwind information for the BND PLT.
2563 Output_data_plt_x86_64_ibt
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
2565 0, 0, 0, 0, // Replaced with offset to .plt.
2566 0, 0, 0, 0, // Replaced with size of .plt.
2567 0, // Augmentation size.
2568 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
2569 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
2570 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
2571 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
2572 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
2573 11, // Block length.
2574 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
2575 elfcpp::DW_OP_breg16
, 0, // Push %rip.
2576 elfcpp::DW_OP_lit15
, // Push 0xf.
2577 elfcpp::DW_OP_and
, // & (%rip & 0xf).
2578 elfcpp::DW_OP_lit9
, // Push 9.
2579 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 9)
2580 elfcpp::DW_OP_lit3
, // Push 3.
2581 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 9) << 3)
2582 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=9)<<3)+%rsp+8
2583 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
2589 // Write out the PLT. This uses the hand-coded instructions above,
2590 // and adjusts them as needed. This is specified by the AMD64 ABI.
2594 Output_data_plt_x86_64
<size
>::do_write(Output_file
* of
)
2596 const off_t offset
= this->offset();
2597 const section_size_type oview_size
=
2598 convert_to_section_size_type(this->data_size());
2599 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2601 const off_t got_file_offset
= this->got_plt_
->offset();
2602 gold_assert(parameters
->incremental_update()
2603 || (got_file_offset
+ this->got_plt_
->data_size()
2604 == this->got_irelative_
->offset()));
2605 const section_size_type got_size
=
2606 convert_to_section_size_type(this->got_plt_
->data_size()
2607 + this->got_irelative_
->data_size());
2608 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
2611 unsigned char* pov
= oview
;
2613 // The base address of the .plt section.
2614 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
2615 // The base address of the .got section.
2616 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
= this->got_
->address();
2617 // The base address of the PLT portion of the .got section,
2618 // which is where the GOT pointer will point, and where the
2619 // three reserved GOT entries are located.
2620 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
2621 = this->got_plt_
->address();
2623 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
2624 pov
+= this->get_plt_entry_size();
2626 // The first three entries in the GOT are reserved, and are written
2627 // by Output_data_got_plt_x86_64::do_write.
2628 unsigned char* got_pov
= got_view
+ 24;
2630 unsigned int plt_offset
= this->get_plt_entry_size();
2631 unsigned int got_offset
= 24;
2632 const unsigned int count
= this->count_
+ this->irelative_count_
;
2633 for (unsigned int plt_index
= 0;
2636 pov
+= this->get_plt_entry_size(),
2638 plt_offset
+= this->get_plt_entry_size(),
2641 // Set and adjust the PLT entry itself.
2642 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
2643 got_address
, plt_address
,
2644 got_offset
, plt_offset
,
2647 // Set the entry in the GOT.
2648 elfcpp::Swap
<64, false>::writeval(got_pov
,
2649 plt_address
+ plt_offset
+ lazy_offset
);
2652 if (this->has_tlsdesc_entry())
2654 // Set and adjust the reserved TLSDESC PLT entry.
2655 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
2656 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
2657 tlsdesc_got_offset
, plt_offset
);
2658 pov
+= this->get_plt_entry_size();
2661 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
2662 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
2664 of
->write_output_view(offset
, oview_size
, oview
);
2665 of
->write_output_view(got_file_offset
, got_size
, got_view
);
2668 // Write out the BND PLT.
2671 Output_data_plt_x86_64_bnd::do_write(Output_file
* of
)
2673 const off_t offset
= this->offset();
2674 const section_size_type oview_size
=
2675 convert_to_section_size_type(this->data_size());
2676 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2678 Output_data_got
<64, false>* got
= this->got();
2679 Output_data_got_plt_x86_64
* got_plt
= this->got_plt();
2680 Output_data_space
* got_irelative
= this->got_irelative();
2682 const off_t got_file_offset
= got_plt
->offset();
2683 gold_assert(parameters
->incremental_update()
2684 || (got_file_offset
+ got_plt
->data_size()
2685 == got_irelative
->offset()));
2686 const section_size_type got_size
=
2687 convert_to_section_size_type(got_plt
->data_size()
2688 + got_irelative
->data_size());
2689 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
2692 unsigned char* pov
= oview
;
2694 // The base address of the .plt section.
2695 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
2696 // The base address of the .got section.
2697 elfcpp::Elf_types
<64>::Elf_Addr got_base
= got
->address();
2698 // The base address of the PLT portion of the .got section,
2699 // which is where the GOT pointer will point, and where the
2700 // three reserved GOT entries are located.
2701 elfcpp::Elf_types
<64>::Elf_Addr got_address
= got_plt
->address();
2703 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
2704 pov
+= plt_entry_size
;
2706 // The first three entries in the GOT are reserved, and are written
2707 // by Output_data_got_plt_x86_64::do_write.
2708 unsigned char* got_pov
= got_view
+ 24;
2710 unsigned int plt_offset
= plt_entry_size
;
2711 unsigned int got_offset
= 24;
2712 const unsigned int count
= this->entry_count();
2713 for (unsigned int plt_index
= 0;
2716 pov
+= plt_entry_size
,
2718 plt_offset
+= plt_entry_size
,
2721 // Set and adjust the PLT entry itself.
2722 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
2723 got_address
, plt_address
,
2724 got_offset
, plt_offset
,
2727 // Set the entry in the GOT.
2728 elfcpp::Swap
<64, false>::writeval(got_pov
,
2729 plt_address
+ plt_offset
+ lazy_offset
);
2732 if (this->has_tlsdesc_entry())
2734 // Set and adjust the reserved TLSDESC PLT entry.
2735 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
2736 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
2737 tlsdesc_got_offset
, plt_offset
);
2738 pov
+= this->get_plt_entry_size();
2739 plt_offset
+= plt_entry_size
;
2742 // Write the additional PLT.
2744 for (unsigned int plt_index
= 0;
2747 pov
+= aplt_entry_size
,
2748 plt_offset
+= aplt_entry_size
,
2751 // Set and adjust the APLT entry.
2752 this->fill_aplt_entry(pov
, got_address
, plt_address
, got_offset
,
2753 plt_offset
, plt_index
);
2756 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
2757 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
2759 of
->write_output_view(offset
, oview_size
, oview
);
2760 of
->write_output_view(got_file_offset
, got_size
, got_view
);
2763 // Write out the IBT PLT.
2767 Output_data_plt_x86_64_ibt
<size
>::do_write(Output_file
* of
)
2769 const off_t offset
= this->offset();
2770 const section_size_type oview_size
=
2771 convert_to_section_size_type(this->data_size());
2772 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2774 Output_data_got
<64, false>* got
= this->got();
2775 Output_data_got_plt_x86_64
* got_plt
= this->got_plt();
2776 Output_data_space
* got_irelative
= this->got_irelative();
2778 const off_t got_file_offset
= got_plt
->offset();
2779 gold_assert(parameters
->incremental_update()
2780 || (got_file_offset
+ got_plt
->data_size()
2781 == got_irelative
->offset()));
2782 const section_size_type got_size
=
2783 convert_to_section_size_type(got_plt
->data_size()
2784 + got_irelative
->data_size());
2785 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
2788 unsigned char* pov
= oview
;
2790 // The base address of the .plt section.
2791 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
2792 // The base address of the .got section.
2793 elfcpp::Elf_types
<64>::Elf_Addr got_base
= got
->address();
2794 // The base address of the PLT portion of the .got section,
2795 // which is where the GOT pointer will point, and where the
2796 // three reserved GOT entries are located.
2797 elfcpp::Elf_types
<64>::Elf_Addr got_address
= got_plt
->address();
2799 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
2800 pov
+= plt_entry_size
;
2802 // The first three entries in the GOT are reserved, and are written
2803 // by Output_data_got_plt_x86_64::do_write.
2804 unsigned char* got_pov
= got_view
+ 24;
2806 unsigned int plt_offset
= plt_entry_size
;
2807 unsigned int got_offset
= 24;
2808 const unsigned int count
= this->entry_count();
2809 for (unsigned int plt_index
= 0;
2812 pov
+= plt_entry_size
,
2814 plt_offset
+= plt_entry_size
,
2817 // Set and adjust the PLT entry itself.
2818 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
2819 got_address
, plt_address
,
2820 got_offset
, plt_offset
,
2823 // Set the entry in the GOT.
2824 elfcpp::Swap
<64, false>::writeval(got_pov
,
2825 plt_address
+ plt_offset
+ lazy_offset
);
2828 if (this->has_tlsdesc_entry())
2830 // Set and adjust the reserved TLSDESC PLT entry.
2831 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
2832 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
2833 tlsdesc_got_offset
, plt_offset
);
2834 pov
+= this->get_plt_entry_size();
2835 plt_offset
+= plt_entry_size
;
2838 // Write the additional PLT.
2840 for (unsigned int plt_index
= 0;
2843 pov
+= aplt_entry_size
,
2844 plt_offset
+= aplt_entry_size
,
2847 // Set and adjust the APLT entry.
2848 this->fill_aplt_entry(pov
, got_address
, plt_address
, got_offset
,
2849 plt_offset
, plt_index
);
2852 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
2853 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
2855 of
->write_output_view(offset
, oview_size
, oview
);
2856 of
->write_output_view(got_file_offset
, got_size
, got_view
);
2859 // Create the PLT section.
2863 Target_x86_64
<size
>::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
2865 if (this->plt_
== NULL
)
2867 // Create the GOT sections first.
2868 this->got_section(symtab
, layout
);
2870 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
2871 this->got_irelative_
);
2873 // Add unwind information if requested.
2874 if (parameters
->options().ld_generated_unwind_info())
2875 this->plt_
->add_eh_frame(layout
);
2877 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
2879 | elfcpp::SHF_EXECINSTR
),
2880 this->plt_
, ORDER_PLT
, false);
2882 // Make the sh_info field of .rela.plt point to .plt.
2883 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
2884 rela_plt_os
->set_info_section(this->plt_
->output_section());
2889 Output_data_plt_x86_64
<32>*
2890 Target_x86_64
<32>::do_make_data_plt(Layout
* layout
,
2891 Output_data_got
<64, false>* got
,
2892 Output_data_got_plt_x86_64
* got_plt
,
2893 Output_data_space
* got_irelative
)
2895 if (this->feature_1_
& elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT
)
2896 return new Output_data_plt_x86_64_ibt
<32>(layout
, got
, got_plt
,
2898 return new Output_data_plt_x86_64_standard
<32>(layout
, got
, got_plt
,
2903 Output_data_plt_x86_64
<64>*
2904 Target_x86_64
<64>::do_make_data_plt(Layout
* layout
,
2905 Output_data_got
<64, false>* got
,
2906 Output_data_got_plt_x86_64
* got_plt
,
2907 Output_data_space
* got_irelative
)
2909 if (this->feature_1_
& elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT
)
2910 return new Output_data_plt_x86_64_ibt
<64>(layout
, got
, got_plt
,
2912 else if (parameters
->options().bndplt())
2913 return new Output_data_plt_x86_64_bnd(layout
, got
, got_plt
,
2916 return new Output_data_plt_x86_64_standard
<64>(layout
, got
, got_plt
,
2921 Output_data_plt_x86_64
<32>*
2922 Target_x86_64
<32>::do_make_data_plt(Layout
* layout
,
2923 Output_data_got
<64, false>* got
,
2924 Output_data_got_plt_x86_64
* got_plt
,
2925 Output_data_space
* got_irelative
,
2926 unsigned int plt_count
)
2928 if (this->feature_1_
& elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT
)
2929 return new Output_data_plt_x86_64_ibt
<32>(layout
, got
, got_plt
,
2930 got_irelative
, plt_count
);
2931 return new Output_data_plt_x86_64_standard
<32>(layout
, got
, got_plt
,
2932 got_irelative
, plt_count
);
2936 Output_data_plt_x86_64
<64>*
2937 Target_x86_64
<64>::do_make_data_plt(Layout
* layout
,
2938 Output_data_got
<64, false>* got
,
2939 Output_data_got_plt_x86_64
* got_plt
,
2940 Output_data_space
* got_irelative
,
2941 unsigned int plt_count
)
2943 if (this->feature_1_
& elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT
)
2944 return new Output_data_plt_x86_64_ibt
<64>(layout
, got
, got_plt
,
2945 got_irelative
, plt_count
);
2946 else if (parameters
->options().bndplt())
2947 return new Output_data_plt_x86_64_bnd(layout
, got
, got_plt
,
2948 got_irelative
, plt_count
);
2950 return new Output_data_plt_x86_64_standard
<64>(layout
, got
, got_plt
,
2955 // Return the section for TLSDESC relocations.
2958 typename Target_x86_64
<size
>::Reloc_section
*
2959 Target_x86_64
<size
>::rela_tlsdesc_section(Layout
* layout
) const
2961 return this->plt_section()->rela_tlsdesc(layout
);
2964 // Create a PLT entry for a global symbol.
2968 Target_x86_64
<size
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
2971 if (gsym
->has_plt_offset())
2974 if (this->plt_
== NULL
)
2975 this->make_plt_section(symtab
, layout
);
2977 this->plt_
->add_entry(symtab
, layout
, gsym
);
2980 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2984 Target_x86_64
<size
>::make_local_ifunc_plt_entry(
2985 Symbol_table
* symtab
, Layout
* layout
,
2986 Sized_relobj_file
<size
, false>* relobj
,
2987 unsigned int local_sym_index
)
2989 if (relobj
->local_has_plt_offset(local_sym_index
))
2991 if (this->plt_
== NULL
)
2992 this->make_plt_section(symtab
, layout
);
2993 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
2996 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
2999 // Return the number of entries in the PLT.
3003 Target_x86_64
<size
>::plt_entry_count() const
3005 if (this->plt_
== NULL
)
3007 return this->plt_
->entry_count();
3010 // Return the offset of the first non-reserved PLT entry.
3014 Target_x86_64
<size
>::first_plt_entry_offset() const
3016 if (this->plt_
== NULL
)
3018 return this->plt_
->first_plt_entry_offset();
3021 // Return the size of each PLT entry.
3025 Target_x86_64
<size
>::plt_entry_size() const
3027 if (this->plt_
== NULL
)
3029 return this->plt_
->get_plt_entry_size();
3032 // Create the GOT and PLT sections for an incremental update.
3035 Output_data_got_base
*
3036 Target_x86_64
<size
>::init_got_plt_for_update(Symbol_table
* symtab
,
3038 unsigned int got_count
,
3039 unsigned int plt_count
)
3041 gold_assert(this->got_
== NULL
);
3043 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
3044 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3046 | elfcpp::SHF_WRITE
),
3047 this->got_
, ORDER_RELRO_LAST
,
3050 // Add the three reserved entries.
3051 this->got_plt_
= new Output_data_got_plt_x86_64(layout
, (plt_count
+ 3) * 8);
3052 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3054 | elfcpp::SHF_WRITE
),
3055 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
3058 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3059 this->global_offset_table_
=
3060 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
3061 Symbol_table::PREDEFINED
,
3063 0, 0, elfcpp::STT_OBJECT
,
3065 elfcpp::STV_HIDDEN
, 0,
3068 // If there are any TLSDESC relocations, they get GOT entries in
3069 // .got.plt after the jump slot entries.
3070 // FIXME: Get the count for TLSDESC entries.
3071 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
3072 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3073 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3075 ORDER_NON_RELRO_FIRST
, false);
3077 // If there are any IRELATIVE relocations, they get GOT entries in
3078 // .got.plt after the jump slot and TLSDESC entries.
3079 this->got_irelative_
= new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
3080 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3081 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3082 this->got_irelative_
,
3083 ORDER_NON_RELRO_FIRST
, false);
3085 // Create the PLT section.
3086 this->plt_
= this->make_data_plt(layout
, this->got_
,
3088 this->got_irelative_
,
3091 // Add unwind information if requested.
3092 if (parameters
->options().ld_generated_unwind_info())
3093 this->plt_
->add_eh_frame(layout
);
3095 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
3096 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
3097 this->plt_
, ORDER_PLT
, false);
3099 // Make the sh_info field of .rela.plt point to .plt.
3100 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
3101 rela_plt_os
->set_info_section(this->plt_
->output_section());
3103 // Create the rela_dyn section.
3104 this->rela_dyn_section(layout
);
3109 // Reserve a GOT entry for a local symbol, and regenerate any
3110 // necessary dynamic relocations.
3114 Target_x86_64
<size
>::reserve_local_got_entry(
3115 unsigned int got_index
,
3116 Sized_relobj
<size
, false>* obj
,
3118 unsigned int got_type
)
3120 unsigned int got_offset
= got_index
* 8;
3121 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
3123 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
3126 case GOT_TYPE_STANDARD
:
3127 if (parameters
->options().output_is_position_independent())
3128 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
3129 this->got_
, got_offset
, 0, false);
3131 case GOT_TYPE_TLS_OFFSET
:
3132 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
3133 this->got_
, got_offset
, 0);
3135 case GOT_TYPE_TLS_PAIR
:
3136 this->got_
->reserve_slot(got_index
+ 1);
3137 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
3138 this->got_
, got_offset
, 0);
3140 case GOT_TYPE_TLS_DESC
:
3141 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
3142 // this->got_->reserve_slot(got_index + 1);
3143 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3144 // this->got_, got_offset, 0);
3151 // Reserve a GOT entry for a global symbol, and regenerate any
3152 // necessary dynamic relocations.
3156 Target_x86_64
<size
>::reserve_global_got_entry(unsigned int got_index
,
3158 unsigned int got_type
)
3160 unsigned int got_offset
= got_index
* 8;
3161 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
3163 this->got_
->reserve_global(got_index
, gsym
, got_type
);
3166 case GOT_TYPE_STANDARD
:
3167 if (!gsym
->final_value_is_known())
3169 if (gsym
->is_from_dynobj()
3170 || gsym
->is_undefined()
3171 || gsym
->is_preemptible()
3172 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
3173 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
3174 this->got_
, got_offset
, 0);
3176 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
3177 this->got_
, got_offset
, 0, false);
3180 case GOT_TYPE_TLS_OFFSET
:
3181 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
3182 this->got_
, got_offset
, 0, false);
3184 case GOT_TYPE_TLS_PAIR
:
3185 this->got_
->reserve_slot(got_index
+ 1);
3186 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
3187 this->got_
, got_offset
, 0, false);
3188 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
3189 this->got_
, got_offset
+ 8, 0, false);
3191 case GOT_TYPE_TLS_DESC
:
3192 this->got_
->reserve_slot(got_index
+ 1);
3193 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
3194 this->got_
, got_offset
, 0, false);
3201 // Register an existing PLT entry for a global symbol.
3205 Target_x86_64
<size
>::register_global_plt_entry(Symbol_table
* symtab
,
3207 unsigned int plt_index
,
3210 gold_assert(this->plt_
!= NULL
);
3211 gold_assert(!gsym
->has_plt_offset());
3213 this->plt_
->reserve_slot(plt_index
);
3215 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
3217 unsigned int got_offset
= (plt_index
+ 3) * 8;
3218 this->plt_
->add_relocation(symtab
, layout
, gsym
, got_offset
);
3221 // Force a COPY relocation for a given symbol.
3225 Target_x86_64
<size
>::emit_copy_reloc(
3226 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
3228 this->copy_relocs_
.emit_copy_reloc(symtab
,
3229 symtab
->get_sized_symbol
<size
>(sym
),
3232 this->rela_dyn_section(NULL
));
3235 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3239 Target_x86_64
<size
>::define_tls_base_symbol(Symbol_table
* symtab
,
3242 if (this->tls_base_symbol_defined_
)
3245 Output_segment
* tls_segment
= layout
->tls_segment();
3246 if (tls_segment
!= NULL
)
3248 bool is_exec
= parameters
->options().output_is_executable();
3249 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
3250 Symbol_table::PREDEFINED
,
3254 elfcpp::STV_HIDDEN
, 0,
3256 ? Symbol::SEGMENT_END
3257 : Symbol::SEGMENT_START
),
3260 this->tls_base_symbol_defined_
= true;
3263 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3267 Target_x86_64
<size
>::reserve_tlsdesc_entries(Symbol_table
* symtab
,
3270 if (this->plt_
== NULL
)
3271 this->make_plt_section(symtab
, layout
);
3273 if (!this->plt_
->has_tlsdesc_entry())
3275 // Allocate the TLSDESC_GOT entry.
3276 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
3277 unsigned int got_offset
= got
->add_constant(0);
3279 // Allocate the TLSDESC_PLT entry.
3280 this->plt_
->reserve_tlsdesc_entry(got_offset
);
3284 // Create a GOT entry for the TLS module index.
3288 Target_x86_64
<size
>::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
3289 Sized_relobj_file
<size
, false>* object
)
3291 if (this->got_mod_index_offset_
== -1U)
3293 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
3294 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
3295 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
3296 unsigned int got_offset
= got
->add_constant(0);
3297 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
3299 got
->add_constant(0);
3300 this->got_mod_index_offset_
= got_offset
;
3302 return this->got_mod_index_offset_
;
3305 // Optimize the TLS relocation type based on what we know about the
3306 // symbol. IS_FINAL is true if the final address of this symbol is
3307 // known at link time.
3310 tls::Tls_optimization
3311 Target_x86_64
<size
>::optimize_tls_reloc(bool is_final
, int r_type
)
3313 // If we are generating a shared library, then we can't do anything
3315 if (parameters
->options().shared())
3316 return tls::TLSOPT_NONE
;
3320 case elfcpp::R_X86_64_TLSGD
:
3321 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3322 case elfcpp::R_X86_64_TLSDESC_CALL
:
3323 // These are General-Dynamic which permits fully general TLS
3324 // access. Since we know that we are generating an executable,
3325 // we can convert this to Initial-Exec. If we also know that
3326 // this is a local symbol, we can further switch to Local-Exec.
3328 return tls::TLSOPT_TO_LE
;
3329 return tls::TLSOPT_TO_IE
;
3331 case elfcpp::R_X86_64_TLSLD
:
3332 // This is Local-Dynamic, which refers to a local symbol in the
3333 // dynamic TLS block. Since we know that we generating an
3334 // executable, we can switch to Local-Exec.
3335 return tls::TLSOPT_TO_LE
;
3337 case elfcpp::R_X86_64_DTPOFF32
:
3338 case elfcpp::R_X86_64_DTPOFF64
:
3339 // Another Local-Dynamic reloc.
3340 return tls::TLSOPT_TO_LE
;
3342 case elfcpp::R_X86_64_GOTTPOFF
:
3343 // These are Initial-Exec relocs which get the thread offset
3344 // from the GOT. If we know that we are linking against the
3345 // local symbol, we can switch to Local-Exec, which links the
3346 // thread offset into the instruction.
3348 return tls::TLSOPT_TO_LE
;
3349 return tls::TLSOPT_NONE
;
3351 case elfcpp::R_X86_64_TPOFF32
:
3352 // When we already have Local-Exec, there is nothing further we
3354 return tls::TLSOPT_NONE
;
3361 // Get the Reference_flags for a particular relocation.
3365 Target_x86_64
<size
>::Scan::get_reference_flags(unsigned int r_type
)
3369 case elfcpp::R_X86_64_NONE
:
3370 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3371 case elfcpp::R_X86_64_GNU_VTENTRY
:
3372 case elfcpp::R_X86_64_GOTPC32
:
3373 case elfcpp::R_X86_64_GOTPC64
:
3374 // No symbol reference.
3377 case elfcpp::R_X86_64_64
:
3378 case elfcpp::R_X86_64_32
:
3379 case elfcpp::R_X86_64_32S
:
3380 case elfcpp::R_X86_64_16
:
3381 case elfcpp::R_X86_64_8
:
3382 return Symbol::ABSOLUTE_REF
;
3384 case elfcpp::R_X86_64_PC64
:
3385 case elfcpp::R_X86_64_PC32
:
3386 case elfcpp::R_X86_64_PC32_BND
:
3387 case elfcpp::R_X86_64_PC16
:
3388 case elfcpp::R_X86_64_PC8
:
3389 case elfcpp::R_X86_64_GOTOFF64
:
3390 return Symbol::RELATIVE_REF
;
3392 case elfcpp::R_X86_64_PLT32
:
3393 case elfcpp::R_X86_64_PLT32_BND
:
3394 case elfcpp::R_X86_64_PLTOFF64
:
3395 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
3397 case elfcpp::R_X86_64_GOT64
:
3398 case elfcpp::R_X86_64_GOT32
:
3399 case elfcpp::R_X86_64_GOTPCREL64
:
3400 case elfcpp::R_X86_64_GOTPCREL
:
3401 case elfcpp::R_X86_64_GOTPCRELX
:
3402 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3403 case elfcpp::R_X86_64_GOTPLT64
:
3405 return Symbol::ABSOLUTE_REF
;
3407 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3408 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3409 case elfcpp::R_X86_64_TLSDESC_CALL
:
3410 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3411 case elfcpp::R_X86_64_DTPOFF32
:
3412 case elfcpp::R_X86_64_DTPOFF64
:
3413 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3414 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3415 return Symbol::TLS_REF
;
3417 case elfcpp::R_X86_64_COPY
:
3418 case elfcpp::R_X86_64_GLOB_DAT
:
3419 case elfcpp::R_X86_64_JUMP_SLOT
:
3420 case elfcpp::R_X86_64_RELATIVE
:
3421 case elfcpp::R_X86_64_IRELATIVE
:
3422 case elfcpp::R_X86_64_TPOFF64
:
3423 case elfcpp::R_X86_64_DTPMOD64
:
3424 case elfcpp::R_X86_64_TLSDESC
:
3425 case elfcpp::R_X86_64_SIZE32
:
3426 case elfcpp::R_X86_64_SIZE64
:
3428 // Not expected. We will give an error later.
3433 // Report an unsupported relocation against a local symbol.
3437 Target_x86_64
<size
>::Scan::unsupported_reloc_local(
3438 Sized_relobj_file
<size
, false>* object
,
3439 unsigned int r_type
)
3441 gold_error(_("%s: unsupported reloc %u against local symbol"),
3442 object
->name().c_str(), r_type
);
3445 // We are about to emit a dynamic relocation of type R_TYPE. If the
3446 // dynamic linker does not support it, issue an error. The GNU linker
3447 // only issues a non-PIC error for an allocated read-only section.
3448 // Here we know the section is allocated, but we don't know that it is
3449 // read-only. But we check for all the relocation types which the
3450 // glibc dynamic linker supports, so it seems appropriate to issue an
3451 // error even if the section is not read-only. If GSYM is not NULL,
3452 // it is the symbol the relocation is against; if it is NULL, the
3453 // relocation is against a local symbol.
3457 Target_x86_64
<size
>::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
3462 // These are the relocation types supported by glibc for x86_64
3463 // which should always work.
3464 case elfcpp::R_X86_64_RELATIVE
:
3465 case elfcpp::R_X86_64_IRELATIVE
:
3466 case elfcpp::R_X86_64_GLOB_DAT
:
3467 case elfcpp::R_X86_64_JUMP_SLOT
:
3468 case elfcpp::R_X86_64_DTPMOD64
:
3469 case elfcpp::R_X86_64_DTPOFF64
:
3470 case elfcpp::R_X86_64_TPOFF64
:
3471 case elfcpp::R_X86_64_64
:
3472 case elfcpp::R_X86_64_COPY
:
3475 // glibc supports these reloc types, but they can overflow.
3476 case elfcpp::R_X86_64_PC32
:
3477 case elfcpp::R_X86_64_PC32_BND
:
3478 // A PC relative reference is OK against a local symbol or if
3479 // the symbol is defined locally.
3481 || (!gsym
->is_from_dynobj()
3482 && !gsym
->is_undefined()
3483 && !gsym
->is_preemptible()))
3486 case elfcpp::R_X86_64_32
:
3487 // R_X86_64_32 is OK for x32.
3488 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
3490 if (this->issued_non_pic_error_
)
3492 gold_assert(parameters
->options().output_is_position_independent());
3494 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
3495 "overflow at runtime; recompile with -fPIC"));
3501 case elfcpp::R_X86_64_32
:
3502 r_name
= "R_X86_64_32";
3504 case elfcpp::R_X86_64_PC32
:
3505 r_name
= "R_X86_64_PC32";
3507 case elfcpp::R_X86_64_PC32_BND
:
3508 r_name
= "R_X86_64_PC32_BND";
3514 object
->error(_("requires dynamic %s reloc against '%s' "
3515 "which may overflow at runtime; recompile "
3517 r_name
, gsym
->name());
3519 this->issued_non_pic_error_
= true;
3523 // This prevents us from issuing more than one error per reloc
3524 // section. But we can still wind up issuing more than one
3525 // error per object file.
3526 if (this->issued_non_pic_error_
)
3528 gold_assert(parameters
->options().output_is_position_independent());
3529 object
->error(_("requires unsupported dynamic reloc %u; "
3530 "recompile with -fPIC"),
3532 this->issued_non_pic_error_
= true;
3535 case elfcpp::R_X86_64_NONE
:
3540 // Return whether we need to make a PLT entry for a relocation of the
3541 // given type against a STT_GNU_IFUNC symbol.
3545 Target_x86_64
<size
>::Scan::reloc_needs_plt_for_ifunc(
3546 Sized_relobj_file
<size
, false>* object
,
3547 unsigned int r_type
)
3549 int flags
= Scan::get_reference_flags(r_type
);
3550 if (flags
& Symbol::TLS_REF
)
3551 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
3552 object
->name().c_str(), r_type
);
3556 // Scan a relocation for a local symbol.
3560 Target_x86_64
<size
>::Scan::local(Symbol_table
* symtab
,
3562 Target_x86_64
<size
>* target
,
3563 Sized_relobj_file
<size
, false>* object
,
3564 unsigned int data_shndx
,
3565 Output_section
* output_section
,
3566 const elfcpp::Rela
<size
, false>& reloc
,
3567 unsigned int r_type
,
3568 const elfcpp::Sym
<size
, false>& lsym
,
3574 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3575 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
3576 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
3578 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3579 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
3584 case elfcpp::R_X86_64_NONE
:
3585 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3586 case elfcpp::R_X86_64_GNU_VTENTRY
:
3589 case elfcpp::R_X86_64_64
:
3590 // If building a shared library (or a position-independent
3591 // executable), we need to create a dynamic relocation for this
3592 // location. The relocation applied at link time will apply the
3593 // link-time value, so we flag the location with an
3594 // R_X86_64_RELATIVE relocation so the dynamic loader can
3595 // relocate it easily.
3596 if (parameters
->options().output_is_position_independent())
3598 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3599 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3600 rela_dyn
->add_local_relative(object
, r_sym
,
3602 ? elfcpp::R_X86_64_RELATIVE64
3603 : elfcpp::R_X86_64_RELATIVE
),
3604 output_section
, data_shndx
,
3605 reloc
.get_r_offset(),
3606 reloc
.get_r_addend(), is_ifunc
);
3610 case elfcpp::R_X86_64_32
:
3611 case elfcpp::R_X86_64_32S
:
3612 case elfcpp::R_X86_64_16
:
3613 case elfcpp::R_X86_64_8
:
3614 // If building a shared library (or a position-independent
3615 // executable), we need to create a dynamic relocation for this
3616 // location. We can't use an R_X86_64_RELATIVE relocation
3617 // because that is always a 64-bit relocation.
3618 if (parameters
->options().output_is_position_independent())
3620 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
3621 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
3623 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3624 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3625 rela_dyn
->add_local_relative(object
, r_sym
,
3626 elfcpp::R_X86_64_RELATIVE
,
3627 output_section
, data_shndx
,
3628 reloc
.get_r_offset(),
3629 reloc
.get_r_addend(), is_ifunc
);
3633 this->check_non_pic(object
, r_type
, NULL
);
3635 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3636 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3637 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
3638 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
3639 data_shndx
, reloc
.get_r_offset(),
3640 reloc
.get_r_addend());
3643 gold_assert(lsym
.get_st_value() == 0);
3644 unsigned int shndx
= lsym
.get_st_shndx();
3646 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
3649 object
->error(_("section symbol %u has bad shndx %u"),
3652 rela_dyn
->add_local_section(object
, shndx
,
3653 r_type
, output_section
,
3654 data_shndx
, reloc
.get_r_offset(),
3655 reloc
.get_r_addend());
3660 case elfcpp::R_X86_64_PC64
:
3661 case elfcpp::R_X86_64_PC32
:
3662 case elfcpp::R_X86_64_PC32_BND
:
3663 case elfcpp::R_X86_64_PC16
:
3664 case elfcpp::R_X86_64_PC8
:
3667 case elfcpp::R_X86_64_PLT32
:
3668 case elfcpp::R_X86_64_PLT32_BND
:
3669 // Since we know this is a local symbol, we can handle this as a
3673 case elfcpp::R_X86_64_GOTPC32
:
3674 case elfcpp::R_X86_64_GOTOFF64
:
3675 case elfcpp::R_X86_64_GOTPC64
:
3676 case elfcpp::R_X86_64_PLTOFF64
:
3677 // We need a GOT section.
3678 target
->got_section(symtab
, layout
);
3679 // For PLTOFF64, we'd normally want a PLT section, but since we
3680 // know this is a local symbol, no PLT is needed.
3683 case elfcpp::R_X86_64_GOT64
:
3684 case elfcpp::R_X86_64_GOT32
:
3685 case elfcpp::R_X86_64_GOTPCREL64
:
3686 case elfcpp::R_X86_64_GOTPCREL
:
3687 case elfcpp::R_X86_64_GOTPCRELX
:
3688 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3689 case elfcpp::R_X86_64_GOTPLT64
:
3691 // The symbol requires a GOT section.
3692 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
3694 // If the relocation symbol isn't IFUNC,
3695 // and is local, then we will convert
3696 // mov foo@GOTPCREL(%rip), %reg
3697 // to lea foo(%rip), %reg.
3698 // in Relocate::relocate.
3699 if (!parameters
->incremental()
3700 && (r_type
== elfcpp::R_X86_64_GOTPCREL
3701 || r_type
== elfcpp::R_X86_64_GOTPCRELX
3702 || r_type
== elfcpp::R_X86_64_REX_GOTPCRELX
)
3703 && reloc
.get_r_offset() >= 2
3706 section_size_type stype
;
3707 const unsigned char* view
= object
->section_contents(data_shndx
,
3709 if (view
[reloc
.get_r_offset() - 2] == 0x8b)
3713 // The symbol requires a GOT entry.
3714 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3716 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3717 // lets function pointers compare correctly with shared
3718 // libraries. Otherwise we would need an IRELATIVE reloc.
3721 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
3723 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
3726 // If we are generating a shared object, we need to add a
3727 // dynamic relocation for this symbol's GOT entry.
3728 if (parameters
->options().output_is_position_independent())
3730 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3731 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3732 if (r_type
!= elfcpp::R_X86_64_GOT32
)
3734 unsigned int got_offset
=
3735 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
3736 rela_dyn
->add_local_relative(object
, r_sym
,
3737 elfcpp::R_X86_64_RELATIVE
,
3738 got
, got_offset
, 0, is_ifunc
);
3742 this->check_non_pic(object
, r_type
, NULL
);
3744 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
3745 rela_dyn
->add_local(
3746 object
, r_sym
, r_type
, got
,
3747 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
3751 // For GOTPLT64, we'd normally want a PLT section, but since
3752 // we know this is a local symbol, no PLT is needed.
3756 case elfcpp::R_X86_64_COPY
:
3757 case elfcpp::R_X86_64_GLOB_DAT
:
3758 case elfcpp::R_X86_64_JUMP_SLOT
:
3759 case elfcpp::R_X86_64_RELATIVE
:
3760 case elfcpp::R_X86_64_IRELATIVE
:
3761 // These are outstanding tls relocs, which are unexpected when linking
3762 case elfcpp::R_X86_64_TPOFF64
:
3763 case elfcpp::R_X86_64_DTPMOD64
:
3764 case elfcpp::R_X86_64_TLSDESC
:
3765 gold_error(_("%s: unexpected reloc %u in object file"),
3766 object
->name().c_str(), r_type
);
3769 // These are initial tls relocs, which are expected when linking
3770 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3771 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3772 case elfcpp::R_X86_64_TLSDESC_CALL
:
3773 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3774 case elfcpp::R_X86_64_DTPOFF32
:
3775 case elfcpp::R_X86_64_DTPOFF64
:
3776 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3777 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3779 bool output_is_shared
= parameters
->options().shared();
3780 const tls::Tls_optimization optimized_type
3781 = Target_x86_64
<size
>::optimize_tls_reloc(!output_is_shared
,
3785 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
3786 if (optimized_type
== tls::TLSOPT_NONE
)
3788 // Create a pair of GOT entries for the module index and
3789 // dtv-relative offset.
3790 Output_data_got
<64, false>* got
3791 = target
->got_section(symtab
, layout
);
3792 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3793 unsigned int shndx
= lsym
.get_st_shndx();
3795 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
3797 object
->error(_("local symbol %u has bad shndx %u"),
3800 got
->add_local_pair_with_rel(object
, r_sym
,
3803 target
->rela_dyn_section(layout
),
3804 elfcpp::R_X86_64_DTPMOD64
);
3806 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3807 unsupported_reloc_local(object
, r_type
);
3810 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3811 target
->define_tls_base_symbol(symtab
, layout
);
3812 if (optimized_type
== tls::TLSOPT_NONE
)
3814 // Create reserved PLT and GOT entries for the resolver.
3815 target
->reserve_tlsdesc_entries(symtab
, layout
);
3817 // Generate a double GOT entry with an
3818 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3819 // is resolved lazily, so the GOT entry needs to be in
3820 // an area in .got.plt, not .got. Call got_section to
3821 // make sure the section has been created.
3822 target
->got_section(symtab
, layout
);
3823 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
3824 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3825 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
3827 unsigned int got_offset
= got
->add_constant(0);
3828 got
->add_constant(0);
3829 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
3831 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
3832 // We store the arguments we need in a vector, and
3833 // use the index into the vector as the parameter
3834 // to pass to the target specific routines.
3835 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
3836 void* arg
= reinterpret_cast<void*>(intarg
);
3837 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
3838 got
, got_offset
, 0);
3841 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3842 unsupported_reloc_local(object
, r_type
);
3845 case elfcpp::R_X86_64_TLSDESC_CALL
:
3848 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3849 if (optimized_type
== tls::TLSOPT_NONE
)
3851 // Create a GOT entry for the module index.
3852 target
->got_mod_index_entry(symtab
, layout
, object
);
3854 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3855 unsupported_reloc_local(object
, r_type
);
3858 case elfcpp::R_X86_64_DTPOFF32
:
3859 case elfcpp::R_X86_64_DTPOFF64
:
3862 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3863 layout
->set_has_static_tls();
3864 if (optimized_type
== tls::TLSOPT_NONE
)
3866 // Create a GOT entry for the tp-relative offset.
3867 Output_data_got
<64, false>* got
3868 = target
->got_section(symtab
, layout
);
3869 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3870 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
3871 target
->rela_dyn_section(layout
),
3872 elfcpp::R_X86_64_TPOFF64
);
3874 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3875 unsupported_reloc_local(object
, r_type
);
3878 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3879 layout
->set_has_static_tls();
3880 if (output_is_shared
)
3881 unsupported_reloc_local(object
, r_type
);
3890 case elfcpp::R_X86_64_SIZE32
:
3891 case elfcpp::R_X86_64_SIZE64
:
3893 gold_error(_("%s: unsupported reloc %u against local symbol"),
3894 object
->name().c_str(), r_type
);
3900 // Report an unsupported relocation against a global symbol.
3904 Target_x86_64
<size
>::Scan::unsupported_reloc_global(
3905 Sized_relobj_file
<size
, false>* object
,
3906 unsigned int r_type
,
3909 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3910 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
3913 // Returns true if this relocation type could be that of a function pointer.
3916 Target_x86_64
<size
>::Scan::possible_function_pointer_reloc(
3917 Sized_relobj_file
<size
, false>* src_obj
,
3918 unsigned int src_indx
,
3919 unsigned int r_offset
,
3920 unsigned int r_type
)
3924 case elfcpp::R_X86_64_64
:
3925 case elfcpp::R_X86_64_32
:
3926 case elfcpp::R_X86_64_32S
:
3927 case elfcpp::R_X86_64_16
:
3928 case elfcpp::R_X86_64_8
:
3929 case elfcpp::R_X86_64_GOT64
:
3930 case elfcpp::R_X86_64_GOT32
:
3931 case elfcpp::R_X86_64_GOTPCREL64
:
3932 case elfcpp::R_X86_64_GOTPCREL
:
3933 case elfcpp::R_X86_64_GOTPCRELX
:
3934 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3935 case elfcpp::R_X86_64_GOTPLT64
:
3939 case elfcpp::R_X86_64_PC32
:
3941 // This relocation may be used both for function calls and
3942 // for taking address of a function. We distinguish between
3943 // them by checking the opcodes.
3944 uint64_t sh_flags
= src_obj
->section_flags(src_indx
);
3945 bool is_executable
= (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0;
3948 section_size_type stype
;
3949 const unsigned char* view
= src_obj
->section_contents(src_indx
,
3955 && view
[r_offset
- 1] == 0xe8)
3960 && view
[r_offset
- 1] == 0xe9)
3963 // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
3965 && view
[r_offset
- 2] == 0x0f
3966 && view
[r_offset
- 1] >= 0x80
3967 && view
[r_offset
- 1] <= 0x8f)
3971 // Be conservative and treat all others as function pointers.
3978 // For safe ICF, scan a relocation for a local symbol to check if it
3979 // corresponds to a function pointer being taken. In that case mark
3980 // the function whose pointer was taken as not foldable.
3984 Target_x86_64
<size
>::Scan::local_reloc_may_be_function_pointer(
3987 Target_x86_64
<size
>* ,
3988 Sized_relobj_file
<size
, false>* src_obj
,
3989 unsigned int src_indx
,
3991 const elfcpp::Rela
<size
, false>& reloc
,
3992 unsigned int r_type
,
3993 const elfcpp::Sym
<size
, false>&)
3995 // When building a shared library, do not fold any local symbols as it is
3996 // not possible to distinguish pointer taken versus a call by looking at
3997 // the relocation types.
3998 if (parameters
->options().shared())
4001 return possible_function_pointer_reloc(src_obj
, src_indx
,
4002 reloc
.get_r_offset(), r_type
);
4005 // For safe ICF, scan a relocation for a global symbol to check if it
4006 // corresponds to a function pointer being taken. In that case mark
4007 // the function whose pointer was taken as not foldable.
4011 Target_x86_64
<size
>::Scan::global_reloc_may_be_function_pointer(
4014 Target_x86_64
<size
>* ,
4015 Sized_relobj_file
<size
, false>* src_obj
,
4016 unsigned int src_indx
,
4018 const elfcpp::Rela
<size
, false>& reloc
,
4019 unsigned int r_type
,
4022 // When building a shared library, do not fold symbols whose visibility
4023 // is hidden, internal or protected.
4024 if (parameters
->options().shared()
4025 && (gsym
->visibility() == elfcpp::STV_INTERNAL
4026 || gsym
->visibility() == elfcpp::STV_PROTECTED
4027 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
4030 return possible_function_pointer_reloc(src_obj
, src_indx
,
4031 reloc
.get_r_offset(), r_type
);
4034 // Scan a relocation for a global symbol.
4038 Target_x86_64
<size
>::Scan::global(Symbol_table
* symtab
,
4040 Target_x86_64
<size
>* target
,
4041 Sized_relobj_file
<size
, false>* object
,
4042 unsigned int data_shndx
,
4043 Output_section
* output_section
,
4044 const elfcpp::Rela
<size
, false>& reloc
,
4045 unsigned int r_type
,
4048 // A STT_GNU_IFUNC symbol may require a PLT entry.
4049 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4050 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
4051 target
->make_plt_entry(symtab
, layout
, gsym
);
4055 case elfcpp::R_X86_64_NONE
:
4056 case elfcpp::R_X86_64_GNU_VTINHERIT
:
4057 case elfcpp::R_X86_64_GNU_VTENTRY
:
4060 case elfcpp::R_X86_64_64
:
4061 case elfcpp::R_X86_64_32
:
4062 case elfcpp::R_X86_64_32S
:
4063 case elfcpp::R_X86_64_16
:
4064 case elfcpp::R_X86_64_8
:
4066 // Make a PLT entry if necessary.
4067 if (gsym
->needs_plt_entry())
4069 target
->make_plt_entry(symtab
, layout
, gsym
);
4070 // Since this is not a PC-relative relocation, we may be
4071 // taking the address of a function. In that case we need to
4072 // set the entry in the dynamic symbol table to the address of
4074 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
4075 gsym
->set_needs_dynsym_value();
4077 // Make a dynamic relocation if necessary.
4078 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
4080 if (!parameters
->options().output_is_position_independent()
4081 && gsym
->may_need_copy_reloc())
4083 target
->copy_reloc(symtab
, layout
, object
,
4084 data_shndx
, output_section
, gsym
, reloc
);
4086 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
4087 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
4088 && gsym
->type() == elfcpp::STT_GNU_IFUNC
4089 && gsym
->can_use_relative_reloc(false)
4090 && !gsym
->is_from_dynobj()
4091 && !gsym
->is_undefined()
4092 && !gsym
->is_preemptible())
4094 // Use an IRELATIVE reloc for a locally defined
4095 // STT_GNU_IFUNC symbol. This makes a function
4096 // address in a PIE executable match the address in a
4097 // shared library that it links against.
4098 Reloc_section
* rela_dyn
=
4099 target
->rela_irelative_section(layout
);
4100 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
4101 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
4102 output_section
, object
,
4104 reloc
.get_r_offset(),
4105 reloc
.get_r_addend());
4107 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
4108 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
4109 && gsym
->can_use_relative_reloc(false))
4111 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4112 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
4113 output_section
, object
,
4115 reloc
.get_r_offset(),
4116 reloc
.get_r_addend(), false);
4120 this->check_non_pic(object
, r_type
, gsym
);
4121 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4122 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4123 data_shndx
, reloc
.get_r_offset(),
4124 reloc
.get_r_addend());
4130 case elfcpp::R_X86_64_PC64
:
4131 case elfcpp::R_X86_64_PC32
:
4132 case elfcpp::R_X86_64_PC32_BND
:
4133 case elfcpp::R_X86_64_PC16
:
4134 case elfcpp::R_X86_64_PC8
:
4136 // Make a PLT entry if necessary.
4137 if (gsym
->needs_plt_entry())
4138 target
->make_plt_entry(symtab
, layout
, gsym
);
4139 // Make a dynamic relocation if necessary.
4140 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
4142 if (parameters
->options().output_is_executable()
4143 && gsym
->may_need_copy_reloc())
4145 target
->copy_reloc(symtab
, layout
, object
,
4146 data_shndx
, output_section
, gsym
, reloc
);
4150 this->check_non_pic(object
, r_type
, gsym
);
4151 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4152 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4153 data_shndx
, reloc
.get_r_offset(),
4154 reloc
.get_r_addend());
4160 case elfcpp::R_X86_64_GOT64
:
4161 case elfcpp::R_X86_64_GOT32
:
4162 case elfcpp::R_X86_64_GOTPCREL64
:
4163 case elfcpp::R_X86_64_GOTPCREL
:
4164 case elfcpp::R_X86_64_GOTPCRELX
:
4165 case elfcpp::R_X86_64_REX_GOTPCRELX
:
4166 case elfcpp::R_X86_64_GOTPLT64
:
4168 // The symbol requires a GOT entry.
4169 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
4171 // If we convert this from
4172 // mov foo@GOTPCREL(%rip), %reg
4173 // to lea foo(%rip), %reg.
4176 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
4178 // in Relocate::relocate, then there is nothing to do here.
4179 // We cannot make these optimizations in incremental linking mode,
4180 // because we look at the opcode to decide whether or not to make
4181 // change, and during an incremental update, the change may have
4182 // already been applied.
4184 Lazy_view
<size
> view(object
, data_shndx
);
4185 size_t r_offset
= reloc
.get_r_offset();
4186 if (!parameters
->incremental()
4188 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
, r_type
,
4192 if (!parameters
->incremental()
4194 && Target_x86_64
<size
>::can_convert_callq_to_direct(gsym
, r_type
,
4199 if (gsym
->final_value_is_known())
4201 // For a STT_GNU_IFUNC symbol we want the PLT address.
4202 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
4203 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
4205 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4209 // If this symbol is not fully resolved, we need to add a
4210 // dynamic relocation for it.
4211 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4213 // Use a GLOB_DAT rather than a RELATIVE reloc if:
4215 // 1) The symbol may be defined in some other module.
4217 // 2) We are building a shared library and this is a
4218 // protected symbol; using GLOB_DAT means that the dynamic
4219 // linker can use the address of the PLT in the main
4220 // executable when appropriate so that function address
4221 // comparisons work.
4223 // 3) This is a STT_GNU_IFUNC symbol in position dependent
4224 // code, again so that function address comparisons work.
4225 if (gsym
->is_from_dynobj()
4226 || gsym
->is_undefined()
4227 || gsym
->is_preemptible()
4228 || (gsym
->visibility() == elfcpp::STV_PROTECTED
4229 && parameters
->options().shared())
4230 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
4231 && parameters
->options().output_is_position_independent()))
4232 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
4233 elfcpp::R_X86_64_GLOB_DAT
);
4236 // For a STT_GNU_IFUNC symbol we want to write the PLT
4237 // offset into the GOT, so that function pointer
4238 // comparisons work correctly.
4240 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
4241 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4244 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
4245 // Tell the dynamic linker to use the PLT address
4246 // when resolving relocations.
4247 if (gsym
->is_from_dynobj()
4248 && !parameters
->options().shared())
4249 gsym
->set_needs_dynsym_value();
4253 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
4254 rela_dyn
->add_global_relative(gsym
,
4255 elfcpp::R_X86_64_RELATIVE
,
4256 got
, got_off
, 0, false);
4263 case elfcpp::R_X86_64_PLT32
:
4264 case elfcpp::R_X86_64_PLT32_BND
:
4265 // If the symbol is fully resolved, this is just a PC32 reloc.
4266 // Otherwise we need a PLT entry.
4267 if (gsym
->final_value_is_known())
4269 // If building a shared library, we can also skip the PLT entry
4270 // if the symbol is defined in the output file and is protected
4272 if (gsym
->is_defined()
4273 && !gsym
->is_from_dynobj()
4274 && !gsym
->is_preemptible())
4276 target
->make_plt_entry(symtab
, layout
, gsym
);
4279 case elfcpp::R_X86_64_GOTPC32
:
4280 case elfcpp::R_X86_64_GOTOFF64
:
4281 case elfcpp::R_X86_64_GOTPC64
:
4282 case elfcpp::R_X86_64_PLTOFF64
:
4283 // We need a GOT section.
4284 target
->got_section(symtab
, layout
);
4285 // For PLTOFF64, we also need a PLT entry (but only if the
4286 // symbol is not fully resolved).
4287 if (r_type
== elfcpp::R_X86_64_PLTOFF64
4288 && !gsym
->final_value_is_known())
4289 target
->make_plt_entry(symtab
, layout
, gsym
);
4292 case elfcpp::R_X86_64_COPY
:
4293 case elfcpp::R_X86_64_GLOB_DAT
:
4294 case elfcpp::R_X86_64_JUMP_SLOT
:
4295 case elfcpp::R_X86_64_RELATIVE
:
4296 case elfcpp::R_X86_64_IRELATIVE
:
4297 // These are outstanding tls relocs, which are unexpected when linking
4298 case elfcpp::R_X86_64_TPOFF64
:
4299 case elfcpp::R_X86_64_DTPMOD64
:
4300 case elfcpp::R_X86_64_TLSDESC
:
4301 gold_error(_("%s: unexpected reloc %u in object file"),
4302 object
->name().c_str(), r_type
);
4305 // These are initial tls relocs, which are expected for global()
4306 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4307 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4308 case elfcpp::R_X86_64_TLSDESC_CALL
:
4309 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4310 case elfcpp::R_X86_64_DTPOFF32
:
4311 case elfcpp::R_X86_64_DTPOFF64
:
4312 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4313 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4315 // For the Initial-Exec model, we can treat undef symbols as final
4316 // when building an executable.
4317 const bool is_final
= (gsym
->final_value_is_known() ||
4318 (r_type
== elfcpp::R_X86_64_GOTTPOFF
&&
4319 gsym
->is_undefined() &&
4320 parameters
->options().output_is_executable()));
4321 const tls::Tls_optimization optimized_type
4322 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
4325 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
4326 if (optimized_type
== tls::TLSOPT_NONE
)
4328 // Create a pair of GOT entries for the module index and
4329 // dtv-relative offset.
4330 Output_data_got
<64, false>* got
4331 = target
->got_section(symtab
, layout
);
4332 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
4333 target
->rela_dyn_section(layout
),
4334 elfcpp::R_X86_64_DTPMOD64
,
4335 elfcpp::R_X86_64_DTPOFF64
);
4337 else if (optimized_type
== tls::TLSOPT_TO_IE
)
4339 // Create a GOT entry for the tp-relative offset.
4340 Output_data_got
<64, false>* got
4341 = target
->got_section(symtab
, layout
);
4342 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
4343 target
->rela_dyn_section(layout
),
4344 elfcpp::R_X86_64_TPOFF64
);
4346 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
4347 unsupported_reloc_global(object
, r_type
, gsym
);
4350 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
4351 target
->define_tls_base_symbol(symtab
, layout
);
4352 if (optimized_type
== tls::TLSOPT_NONE
)
4354 // Create reserved PLT and GOT entries for the resolver.
4355 target
->reserve_tlsdesc_entries(symtab
, layout
);
4357 // Create a double GOT entry with an R_X86_64_TLSDESC
4358 // reloc. The R_X86_64_TLSDESC reloc is resolved
4359 // lazily, so the GOT entry needs to be in an area in
4360 // .got.plt, not .got. Call got_section to make sure
4361 // the section has been created.
4362 target
->got_section(symtab
, layout
);
4363 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
4364 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
4365 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
4366 elfcpp::R_X86_64_TLSDESC
, 0);
4368 else if (optimized_type
== tls::TLSOPT_TO_IE
)
4370 // Create a GOT entry for the tp-relative offset.
4371 Output_data_got
<64, false>* got
4372 = target
->got_section(symtab
, layout
);
4373 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
4374 target
->rela_dyn_section(layout
),
4375 elfcpp::R_X86_64_TPOFF64
);
4377 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
4378 unsupported_reloc_global(object
, r_type
, gsym
);
4381 case elfcpp::R_X86_64_TLSDESC_CALL
:
4384 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4385 if (optimized_type
== tls::TLSOPT_NONE
)
4387 // Create a GOT entry for the module index.
4388 target
->got_mod_index_entry(symtab
, layout
, object
);
4390 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
4391 unsupported_reloc_global(object
, r_type
, gsym
);
4394 case elfcpp::R_X86_64_DTPOFF32
:
4395 case elfcpp::R_X86_64_DTPOFF64
:
4398 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4399 layout
->set_has_static_tls();
4400 if (optimized_type
== tls::TLSOPT_NONE
)
4402 // Create a GOT entry for the tp-relative offset.
4403 Output_data_got
<64, false>* got
4404 = target
->got_section(symtab
, layout
);
4405 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
4406 target
->rela_dyn_section(layout
),
4407 elfcpp::R_X86_64_TPOFF64
);
4409 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
4410 unsupported_reloc_global(object
, r_type
, gsym
);
4413 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4414 layout
->set_has_static_tls();
4415 if (parameters
->options().shared())
4416 unsupported_reloc_global(object
, r_type
, gsym
);
4425 case elfcpp::R_X86_64_SIZE32
:
4426 case elfcpp::R_X86_64_SIZE64
:
4428 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4429 object
->name().c_str(), r_type
,
4430 gsym
->demangled_name().c_str());
4437 Target_x86_64
<size
>::gc_process_relocs(Symbol_table
* symtab
,
4439 Sized_relobj_file
<size
, false>* object
,
4440 unsigned int data_shndx
,
4441 unsigned int sh_type
,
4442 const unsigned char* prelocs
,
4444 Output_section
* output_section
,
4445 bool needs_special_offset_handling
,
4446 size_t local_symbol_count
,
4447 const unsigned char* plocal_symbols
)
4449 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
4452 if (sh_type
== elfcpp::SHT_REL
)
4457 gold::gc_process_relocs
<size
, false, Target_x86_64
<size
>, Scan
,
4467 needs_special_offset_handling
,
4472 // Scan relocations for a section.
4476 Target_x86_64
<size
>::scan_relocs(Symbol_table
* symtab
,
4478 Sized_relobj_file
<size
, false>* object
,
4479 unsigned int data_shndx
,
4480 unsigned int sh_type
,
4481 const unsigned char* prelocs
,
4483 Output_section
* output_section
,
4484 bool needs_special_offset_handling
,
4485 size_t local_symbol_count
,
4486 const unsigned char* plocal_symbols
)
4488 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
4491 if (sh_type
== elfcpp::SHT_REL
)
4493 gold_error(_("%s: unsupported REL reloc section"),
4494 object
->name().c_str());
4498 gold::scan_relocs
<size
, false, Target_x86_64
<size
>, Scan
, Classify_reloc
>(
4507 needs_special_offset_handling
,
4512 // Finalize the sections.
4516 Target_x86_64
<size
>::do_finalize_sections(
4518 const Input_objects
*,
4519 Symbol_table
* symtab
)
4521 const Reloc_section
* rel_plt
= (this->plt_
== NULL
4523 : this->plt_
->rela_plt());
4524 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
4525 this->rela_dyn_
, true, false);
4527 // Fill in some more dynamic tags.
4528 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
4531 if (this->plt_
!= NULL
4532 && this->plt_
->output_section() != NULL
4533 && this->plt_
->has_tlsdesc_entry())
4535 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
4536 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
4537 this->got_
->finalize_data_size();
4538 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
4539 this->plt_
, plt_offset
);
4540 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
4541 this->got_
, got_offset
);
4545 // Emit any relocs we saved in an attempt to avoid generating COPY
4547 if (this->copy_relocs_
.any_saved_relocs())
4548 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
4550 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
4551 // the .got.plt section.
4552 Symbol
* sym
= this->global_offset_table_
;
4555 uint64_t data_size
= this->got_plt_
->current_data_size();
4556 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
4559 if (parameters
->doing_static_link()
4560 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
4562 // If linking statically, make sure that the __rela_iplt symbols
4563 // were defined if necessary, even if we didn't create a PLT.
4564 static const Define_symbol_in_segment syms
[] =
4567 "__rela_iplt_start", // name
4568 elfcpp::PT_LOAD
, // segment_type
4569 elfcpp::PF_W
, // segment_flags_set
4570 elfcpp::PF(0), // segment_flags_clear
4573 elfcpp::STT_NOTYPE
, // type
4574 elfcpp::STB_GLOBAL
, // binding
4575 elfcpp::STV_HIDDEN
, // visibility
4577 Symbol::SEGMENT_START
, // offset_from_base
4581 "__rela_iplt_end", // name
4582 elfcpp::PT_LOAD
, // segment_type
4583 elfcpp::PF_W
, // segment_flags_set
4584 elfcpp::PF(0), // segment_flags_clear
4587 elfcpp::STT_NOTYPE
, // type
4588 elfcpp::STB_GLOBAL
, // binding
4589 elfcpp::STV_HIDDEN
, // visibility
4591 Symbol::SEGMENT_START
, // offset_from_base
4596 symtab
->define_symbols(layout
, 2, syms
,
4597 layout
->script_options()->saw_sections_clause());
4601 // For x32, we need to handle PC-relative relocations using full 64-bit
4602 // arithmetic, so that we can detect relocation overflows properly.
4603 // This class overrides the pcrela32_check methods from the defaults in
4604 // Relocate_functions in reloc.h.
4607 class X86_64_relocate_functions
: public Relocate_functions
<size
, false>
4610 typedef Relocate_functions
<size
, false> Base
;
4612 // Do a simple PC relative relocation with the addend in the
4614 static inline typename
Base::Reloc_status
4615 pcrela32_check(unsigned char* view
,
4616 typename
elfcpp::Elf_types
<64>::Elf_Addr value
,
4617 typename
elfcpp::Elf_types
<64>::Elf_Swxword addend
,
4618 typename
elfcpp::Elf_types
<64>::Elf_Addr address
)
4620 typedef typename
elfcpp::Swap
<32, false>::Valtype Valtype
;
4621 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
4622 value
= value
+ addend
- address
;
4623 elfcpp::Swap
<32, false>::writeval(wv
, value
);
4624 return (Bits
<32>::has_overflow(value
)
4625 ? Base::RELOC_OVERFLOW
: Base::RELOC_OK
);
4628 // Do a simple PC relative relocation with a Symbol_value with the
4629 // addend in the relocation.
4630 static inline typename
Base::Reloc_status
4631 pcrela32_check(unsigned char* view
,
4632 const Sized_relobj_file
<size
, false>* object
,
4633 const Symbol_value
<size
>* psymval
,
4634 typename
elfcpp::Elf_types
<64>::Elf_Swxword addend
,
4635 typename
elfcpp::Elf_types
<64>::Elf_Addr address
)
4637 typedef typename
elfcpp::Swap
<32, false>::Valtype Valtype
;
4638 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
4639 typename
elfcpp::Elf_types
<64>::Elf_Addr value
;
4641 value
= psymval
->value(object
, addend
);
4644 // For negative addends, get the symbol value without
4645 // the addend, then add the addend using 64-bit arithmetic.
4646 value
= psymval
->value(object
, 0);
4650 elfcpp::Swap
<32, false>::writeval(wv
, value
);
4651 return (Bits
<32>::has_overflow(value
)
4652 ? Base::RELOC_OVERFLOW
: Base::RELOC_OK
);
4656 // Perform a relocation.
4660 Target_x86_64
<size
>::Relocate::relocate(
4661 const Relocate_info
<size
, false>* relinfo
,
4663 Target_x86_64
<size
>* target
,
4666 const unsigned char* preloc
,
4667 const Sized_symbol
<size
>* gsym
,
4668 const Symbol_value
<size
>* psymval
,
4669 unsigned char* view
,
4670 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4671 section_size_type view_size
)
4673 typedef X86_64_relocate_functions
<size
> Reloc_funcs
;
4674 const elfcpp::Rela
<size
, false> rela(preloc
);
4675 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
4677 if (this->skip_call_tls_get_addr_
)
4679 if ((r_type
!= elfcpp::R_X86_64_PLT32
4680 && r_type
!= elfcpp::R_X86_64_GOTPCREL
4681 && r_type
!= elfcpp::R_X86_64_GOTPCRELX
4682 && r_type
!= elfcpp::R_X86_64_PLT32_BND
4683 && r_type
!= elfcpp::R_X86_64_PC32_BND
4684 && r_type
!= elfcpp::R_X86_64_PC32
)
4686 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
4688 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4689 _("missing expected TLS relocation"));
4690 this->skip_call_tls_get_addr_
= false;
4694 this->skip_call_tls_get_addr_
= false;
4702 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
4704 // Pick the value to use for symbols defined in the PLT.
4705 Symbol_value
<size
> symval
;
4707 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
4709 symval
.set_output_value(target
->plt_address_for_global(gsym
));
4712 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
4714 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4715 if (object
->local_has_plt_offset(r_sym
))
4717 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
4722 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4724 // Get the GOT offset if needed.
4725 // The GOT pointer points to the end of the GOT section.
4726 // We need to subtract the size of the GOT section to get
4727 // the actual offset to use in the relocation.
4728 bool have_got_offset
= false;
4729 // Since the actual offset is always negative, we use signed int to
4730 // support 64-bit GOT relocations.
4734 case elfcpp::R_X86_64_GOT32
:
4735 case elfcpp::R_X86_64_GOT64
:
4736 case elfcpp::R_X86_64_GOTPLT64
:
4737 case elfcpp::R_X86_64_GOTPCREL64
:
4740 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4741 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
4745 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4746 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4747 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4748 - target
->got_size());
4750 have_got_offset
= true;
4757 typename
Reloc_funcs::Reloc_status rstatus
= Reloc_funcs::RELOC_OK
;
4761 case elfcpp::R_X86_64_NONE
:
4762 case elfcpp::R_X86_64_GNU_VTINHERIT
:
4763 case elfcpp::R_X86_64_GNU_VTENTRY
:
4766 case elfcpp::R_X86_64_64
:
4767 Reloc_funcs::rela64(view
, object
, psymval
, addend
);
4770 case elfcpp::R_X86_64_PC64
:
4771 Reloc_funcs::pcrela64(view
, object
, psymval
, addend
,
4775 case elfcpp::R_X86_64_32
:
4776 rstatus
= Reloc_funcs::rela32_check(view
, object
, psymval
, addend
,
4777 Reloc_funcs::CHECK_UNSIGNED
);
4780 case elfcpp::R_X86_64_32S
:
4781 rstatus
= Reloc_funcs::rela32_check(view
, object
, psymval
, addend
,
4782 Reloc_funcs::CHECK_SIGNED
);
4785 case elfcpp::R_X86_64_PC32
:
4786 case elfcpp::R_X86_64_PC32_BND
:
4787 rstatus
= Reloc_funcs::pcrela32_check(view
, object
, psymval
, addend
,
4791 case elfcpp::R_X86_64_16
:
4792 Reloc_funcs::rela16(view
, object
, psymval
, addend
);
4795 case elfcpp::R_X86_64_PC16
:
4796 Reloc_funcs::pcrela16(view
, object
, psymval
, addend
, address
);
4799 case elfcpp::R_X86_64_8
:
4800 Reloc_funcs::rela8(view
, object
, psymval
, addend
);
4803 case elfcpp::R_X86_64_PC8
:
4804 Reloc_funcs::pcrela8(view
, object
, psymval
, addend
, address
);
4807 case elfcpp::R_X86_64_PLT32
:
4808 case elfcpp::R_X86_64_PLT32_BND
:
4809 gold_assert(gsym
== NULL
4810 || gsym
->has_plt_offset()
4811 || gsym
->final_value_is_known()
4812 || (gsym
->is_defined()
4813 && !gsym
->is_from_dynobj()
4814 && !gsym
->is_preemptible()));
4815 // Note: while this code looks the same as for R_X86_64_PC32, it
4816 // behaves differently because psymval was set to point to
4817 // the PLT entry, rather than the symbol, in Scan::global().
4818 rstatus
= Reloc_funcs::pcrela32_check(view
, object
, psymval
, addend
,
4822 case elfcpp::R_X86_64_PLTOFF64
:
4825 gold_assert(gsym
->has_plt_offset()
4826 || gsym
->final_value_is_known());
4827 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
;
4828 // This is the address of GLOBAL_OFFSET_TABLE.
4829 got_address
= target
->got_plt_section()->address();
4830 Reloc_funcs::rela64(view
, object
, psymval
, addend
- got_address
);
4834 case elfcpp::R_X86_64_GOT32
:
4835 gold_assert(have_got_offset
);
4836 Reloc_funcs::rela32(view
, got_offset
, addend
);
4839 case elfcpp::R_X86_64_GOTPC32
:
4842 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4843 value
= target
->got_plt_section()->address();
4844 Reloc_funcs::pcrela32_check(view
, value
, addend
, address
);
4848 case elfcpp::R_X86_64_GOT64
:
4849 case elfcpp::R_X86_64_GOTPLT64
:
4850 // R_X86_64_GOTPLT64 is obsolete and treated the same as
4852 gold_assert(have_got_offset
);
4853 Reloc_funcs::rela64(view
, got_offset
, addend
);
4856 case elfcpp::R_X86_64_GOTPC64
:
4859 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4860 value
= target
->got_plt_section()->address();
4861 Reloc_funcs::pcrela64(view
, value
, addend
, address
);
4865 case elfcpp::R_X86_64_GOTOFF64
:
4867 typename
elfcpp::Elf_types
<size
>::Elf_Addr reladdr
;
4868 reladdr
= target
->got_plt_section()->address();
4869 Reloc_funcs::pcrela64(view
, object
, psymval
, addend
, reladdr
);
4873 case elfcpp::R_X86_64_GOTPCREL
:
4874 case elfcpp::R_X86_64_GOTPCRELX
:
4875 case elfcpp::R_X86_64_REX_GOTPCRELX
:
4878 // mov foo@GOTPCREL(%rip), %reg
4879 // to lea foo(%rip), %reg.
4881 if (!parameters
->incremental()
4883 && rela
.get_r_offset() >= 2
4885 && !psymval
->is_ifunc_symbol())
4887 && rela
.get_r_offset() >= 2
4888 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
, r_type
,
4892 Reloc_funcs::pcrela32(view
, object
, psymval
, addend
, address
);
4895 // callq *foo@GOTPCRELX(%rip) to
4897 // and jmpq *foo@GOTPCRELX(%rip) to
4900 else if (!parameters
->incremental()
4902 && rela
.get_r_offset() >= 2
4903 && Target_x86_64
<size
>::can_convert_callq_to_direct(gsym
,
4907 if (view
[-1] == 0x15)
4909 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4910 // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
4913 // Convert GOTPCRELX to 32-bit pc relative reloc.
4914 Reloc_funcs::pcrela32(view
, object
, psymval
, addend
, address
);
4918 // Convert jmpq *foo@GOTPCRELX(%rip) to
4921 // The opcode of direct jmpq is 0xe9.
4923 // The opcode of nop is 0x90.
4925 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip
4926 // relative and since the instruction following the jmpq is now
4927 // the nop, offset the address by 1 byte. The start of the
4928 // relocation also moves ahead by 1 byte.
4929 Reloc_funcs::pcrela32(&view
[-1], object
, psymval
, addend
,
4937 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4938 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
4939 - target
->got_size());
4943 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4944 gold_assert(object
->local_has_got_offset(r_sym
,
4945 GOT_TYPE_STANDARD
));
4946 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4947 - target
->got_size());
4949 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4950 value
= target
->got_plt_section()->address() + got_offset
;
4951 Reloc_funcs::pcrela32_check(view
, value
, addend
, address
);
4956 case elfcpp::R_X86_64_GOTPCREL64
:
4958 gold_assert(have_got_offset
);
4959 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4960 value
= target
->got_plt_section()->address() + got_offset
;
4961 Reloc_funcs::pcrela64(view
, value
, addend
, address
);
4965 case elfcpp::R_X86_64_COPY
:
4966 case elfcpp::R_X86_64_GLOB_DAT
:
4967 case elfcpp::R_X86_64_JUMP_SLOT
:
4968 case elfcpp::R_X86_64_RELATIVE
:
4969 case elfcpp::R_X86_64_IRELATIVE
:
4970 // These are outstanding tls relocs, which are unexpected when linking
4971 case elfcpp::R_X86_64_TPOFF64
:
4972 case elfcpp::R_X86_64_DTPMOD64
:
4973 case elfcpp::R_X86_64_TLSDESC
:
4974 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4975 _("unexpected reloc %u in object file"),
4979 // These are initial tls relocs, which are expected when linking
4980 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4981 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4982 case elfcpp::R_X86_64_TLSDESC_CALL
:
4983 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4984 case elfcpp::R_X86_64_DTPOFF32
:
4985 case elfcpp::R_X86_64_DTPOFF64
:
4986 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4987 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4988 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
4989 view
, address
, view_size
);
4992 case elfcpp::R_X86_64_SIZE32
:
4993 case elfcpp::R_X86_64_SIZE64
:
4995 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4996 _("unsupported reloc %u"),
5001 if (rstatus
== Reloc_funcs::RELOC_OVERFLOW
)
5005 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5006 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5007 _("relocation overflow: "
5008 "reference to local symbol %u in %s"),
5009 r_sym
, object
->name().c_str());
5011 else if (gsym
->is_defined() && gsym
->source() == Symbol::FROM_OBJECT
)
5013 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5014 _("relocation overflow: "
5015 "reference to '%s' defined in %s"),
5017 gsym
->object()->name().c_str());
5021 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5022 _("relocation overflow: reference to '%s'"),
5030 // Perform a TLS relocation.
5034 Target_x86_64
<size
>::Relocate::relocate_tls(
5035 const Relocate_info
<size
, false>* relinfo
,
5036 Target_x86_64
<size
>* target
,
5038 const elfcpp::Rela
<size
, false>& rela
,
5039 unsigned int r_type
,
5040 const Sized_symbol
<size
>* gsym
,
5041 const Symbol_value
<size
>* psymval
,
5042 unsigned char* view
,
5043 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5044 section_size_type view_size
)
5046 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
5048 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
5049 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
5050 elfcpp::Shdr
<size
, false> data_shdr(relinfo
->data_shdr
);
5051 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
5053 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
5055 const bool is_final
= (gsym
== NULL
5056 ? !parameters
->options().shared()
5057 : gsym
->final_value_is_known());
5058 tls::Tls_optimization optimized_type
5059 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
5062 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
5063 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
5065 // If this code sequence is used in a non-executable section,
5066 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
5067 // on the assumption that it's being used by itself in a debug
5068 // section. Therefore, in the unlikely event that the code
5069 // sequence appears in a non-executable section, we simply
5070 // leave it unoptimized.
5071 optimized_type
= tls::TLSOPT_NONE
;
5073 if (optimized_type
== tls::TLSOPT_TO_LE
)
5075 if (tls_segment
== NULL
)
5077 gold_assert(parameters
->errors()->error_count() > 0
5078 || issue_undefined_symbol_error(gsym
));
5081 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
5082 rela
, r_type
, value
, view
,
5088 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
5089 ? GOT_TYPE_TLS_OFFSET
5090 : GOT_TYPE_TLS_PAIR
);
5091 unsigned int got_offset
;
5094 gold_assert(gsym
->has_got_offset(got_type
));
5095 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
5099 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5100 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
5101 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
5102 - target
->got_size());
5104 if (optimized_type
== tls::TLSOPT_TO_IE
)
5106 value
= target
->got_plt_section()->address() + got_offset
;
5107 this->tls_gd_to_ie(relinfo
, relnum
, rela
, r_type
,
5108 value
, view
, address
, view_size
);
5111 else if (optimized_type
== tls::TLSOPT_NONE
)
5113 // Relocate the field with the offset of the pair of GOT
5115 value
= target
->got_plt_section()->address() + got_offset
;
5116 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
5121 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5122 _("unsupported reloc %u"), r_type
);
5125 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
5126 case elfcpp::R_X86_64_TLSDESC_CALL
:
5127 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
5129 // See above comment for R_X86_64_TLSGD.
5130 optimized_type
= tls::TLSOPT_NONE
;
5132 if (optimized_type
== tls::TLSOPT_TO_LE
)
5134 if (tls_segment
== NULL
)
5136 gold_assert(parameters
->errors()->error_count() > 0
5137 || issue_undefined_symbol_error(gsym
));
5140 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
5141 rela
, r_type
, value
, view
,
5147 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
5148 ? GOT_TYPE_TLS_OFFSET
5149 : GOT_TYPE_TLS_DESC
);
5150 unsigned int got_offset
= 0;
5151 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
5152 && optimized_type
== tls::TLSOPT_NONE
)
5154 // We created GOT entries in the .got.tlsdesc portion of
5155 // the .got.plt section, but the offset stored in the
5156 // symbol is the offset within .got.tlsdesc.
5157 got_offset
= (target
->got_size()
5158 + target
->got_plt_section()->data_size());
5162 gold_assert(gsym
->has_got_offset(got_type
));
5163 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
5167 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5168 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
5169 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
5170 - target
->got_size());
5172 if (optimized_type
== tls::TLSOPT_TO_IE
)
5174 value
= target
->got_plt_section()->address() + got_offset
;
5175 this->tls_desc_gd_to_ie(relinfo
, relnum
,
5176 rela
, r_type
, value
, view
, address
,
5180 else if (optimized_type
== tls::TLSOPT_NONE
)
5182 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
5184 // Relocate the field with the offset of the pair of GOT
5186 value
= target
->got_plt_section()->address() + got_offset
;
5187 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
5193 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5194 _("unsupported reloc %u"), r_type
);
5197 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
5198 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
5200 // See above comment for R_X86_64_TLSGD.
5201 optimized_type
= tls::TLSOPT_NONE
;
5203 if (optimized_type
== tls::TLSOPT_TO_LE
)
5205 if (tls_segment
== NULL
)
5207 gold_assert(parameters
->errors()->error_count() > 0
5208 || issue_undefined_symbol_error(gsym
));
5211 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
5212 value
, view
, view_size
);
5215 else if (optimized_type
== tls::TLSOPT_NONE
)
5217 // Relocate the field with the offset of the GOT entry for
5218 // the module index.
5219 unsigned int got_offset
;
5220 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
5221 - target
->got_size());
5222 value
= target
->got_plt_section()->address() + got_offset
;
5223 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
5227 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5228 _("unsupported reloc %u"), r_type
);
5231 case elfcpp::R_X86_64_DTPOFF32
:
5232 // This relocation type is used in debugging information.
5233 // In that case we need to not optimize the value. If the
5234 // section is not executable, then we assume we should not
5235 // optimize this reloc. See comments above for R_X86_64_TLSGD,
5236 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
5238 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
5240 if (tls_segment
== NULL
)
5242 gold_assert(parameters
->errors()->error_count() > 0
5243 || issue_undefined_symbol_error(gsym
));
5246 value
-= tls_segment
->memsz();
5248 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
5251 case elfcpp::R_X86_64_DTPOFF64
:
5252 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
5253 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
5255 if (tls_segment
== NULL
)
5257 gold_assert(parameters
->errors()->error_count() > 0
5258 || issue_undefined_symbol_error(gsym
));
5261 value
-= tls_segment
->memsz();
5263 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
5266 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
5268 && gsym
->is_undefined()
5269 && parameters
->options().output_is_executable())
5271 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
5273 r_type
, value
, view
,
5277 else if (optimized_type
== tls::TLSOPT_TO_LE
)
5279 if (tls_segment
== NULL
)
5281 gold_assert(parameters
->errors()->error_count() > 0
5282 || issue_undefined_symbol_error(gsym
));
5285 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
5287 r_type
, value
, view
,
5291 else if (optimized_type
== tls::TLSOPT_NONE
)
5293 // Relocate the field with the offset of the GOT entry for
5294 // the tp-relative offset of the symbol.
5295 unsigned int got_offset
;
5298 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
5299 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
5300 - target
->got_size());
5304 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5305 gold_assert(object
->local_has_got_offset(r_sym
,
5306 GOT_TYPE_TLS_OFFSET
));
5307 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
5308 - target
->got_size());
5310 value
= target
->got_plt_section()->address() + got_offset
;
5311 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
5315 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5316 _("unsupported reloc type %u"),
5320 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
5321 if (tls_segment
== NULL
)
5323 gold_assert(parameters
->errors()->error_count() > 0
5324 || issue_undefined_symbol_error(gsym
));
5327 value
-= tls_segment
->memsz();
5328 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
5333 // Do a relocation in which we convert a TLS General-Dynamic to an
5338 Target_x86_64
<size
>::Relocate::tls_gd_to_ie(
5339 const Relocate_info
<size
, false>* relinfo
,
5341 const elfcpp::Rela
<size
, false>& rela
,
5343 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
5344 unsigned char* view
,
5345 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5346 section_size_type view_size
)
5349 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5350 // .word 0x6666; rex64; call __tls_get_addr@PLT
5351 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
5352 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5353 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5354 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
5356 // leaq foo@tlsgd(%rip),%rdi;
5357 // .word 0x6666; rex64; call __tls_get_addr@PLT
5358 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
5359 // leaq foo@tlsgd(%rip),%rdi;
5360 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5361 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
5363 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
5364 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5365 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0
5366 || memcmp(view
+ 4, "\x66\x48\xff", 3) == 0));
5370 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
5372 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5373 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
5374 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5379 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
5381 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5382 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
5383 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5387 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
5388 Relocate_functions
<size
, false>::pcrela32(view
+ 8, value
, addend
- 8,
5391 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5393 this->skip_call_tls_get_addr_
= true;
5396 // Do a relocation in which we convert a TLS General-Dynamic to a
5401 Target_x86_64
<size
>::Relocate::tls_gd_to_le(
5402 const Relocate_info
<size
, false>* relinfo
,
5404 Output_segment
* tls_segment
,
5405 const elfcpp::Rela
<size
, false>& rela
,
5407 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
5408 unsigned char* view
,
5409 section_size_type view_size
)
5412 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5413 // .word 0x6666; rex64; call __tls_get_addr@PLT
5414 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5415 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5416 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5417 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5419 // leaq foo@tlsgd(%rip),%rdi;
5420 // .word 0x6666; rex64; call __tls_get_addr@PLT
5421 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5422 // leaq foo@tlsgd(%rip),%rdi;
5423 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5424 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5426 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
5427 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5428 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0
5429 || memcmp(view
+ 4, "\x66\x48\xff", 3) == 0));
5433 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
5435 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5436 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
5437 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5442 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
5444 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5445 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
5447 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5451 value
-= tls_segment
->memsz();
5452 Relocate_functions
<size
, false>::rela32(view
+ 8, value
, 0);
5454 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5456 this->skip_call_tls_get_addr_
= true;
5459 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
5463 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_ie(
5464 const Relocate_info
<size
, false>* relinfo
,
5466 const elfcpp::Rela
<size
, false>& rela
,
5467 unsigned int r_type
,
5468 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
5469 unsigned char* view
,
5470 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5471 section_size_type view_size
)
5473 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
5475 // LP64: leaq foo@tlsdesc(%rip), %rax
5476 // ==> movq foo@gottpoff(%rip), %rax
5477 // X32: rex leal foo@tlsdesc(%rip), %eax
5478 // ==> rex movl foo@gottpoff(%rip), %eax
5479 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
5480 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
5481 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5482 (((view
[-3] & 0xfb) == 0x48
5483 || (size
== 32 && (view
[-3] & 0xfb) == 0x40))
5485 && (view
[-1] & 0xc7) == 0x05));
5487 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
5488 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
5492 // LP64: call *foo@tlscall(%rax)
5493 // ==> xchg %ax, %ax
5494 // X32: call *foo@tlscall(%eax)
5496 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
5497 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
5499 if (size
== 32 && view
[0] == 0x67)
5501 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(),
5505 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5506 view
[prefix
] == 0xff && view
[prefix
+ 1] == 0x10);
5521 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
5525 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_le(
5526 const Relocate_info
<size
, false>* relinfo
,
5528 Output_segment
* tls_segment
,
5529 const elfcpp::Rela
<size
, false>& rela
,
5530 unsigned int r_type
,
5531 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
5532 unsigned char* view
,
5533 section_size_type view_size
)
5535 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
5537 // LP64: leaq foo@tlsdesc(%rip), %rax
5538 // ==> movq foo@tpoff, %rax
5539 // X32: rex leal foo@tlsdesc(%rip), %eax
5540 // ==> rex movl foo@tpoff, %eax
5541 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
5542 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
5543 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5544 (((view
[-3] & 0xfb) == 0x48
5545 || (size
== 32 && (view
[-3] & 0xfb) == 0x40))
5547 && (view
[-1] & 0xc7) == 0x05));
5548 view
[-3] = (view
[-3] & 0x48) | ((view
[-3] >> 2) & 1);
5550 view
[-1] = 0xc0 | ((view
[-1] >> 3) & 7);
5551 value
-= tls_segment
->memsz();
5552 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
5556 // LP64: call *foo@tlscall(%rax)
5557 // ==> xchg %ax, %ax
5558 // X32: call *foo@tlscall(%eax)
5560 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
5561 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
5563 if (size
== 32 && view
[0] == 0x67)
5565 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(),
5569 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5570 view
[prefix
] == 0xff && view
[prefix
+ 1] == 0x10);
5587 Target_x86_64
<size
>::Relocate::tls_ld_to_le(
5588 const Relocate_info
<size
, false>* relinfo
,
5591 const elfcpp::Rela
<size
, false>& rela
,
5593 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
5594 unsigned char* view
,
5595 section_size_type view_size
)
5597 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
5599 // ... leq foo@dtpoff(%rax),%reg
5600 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5602 // ... leq foo@dtpoff(%rax),%reg
5603 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5604 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
5606 // ... leq foo@dtpoff(%rax),%reg
5607 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5609 // ... leq foo@dtpoff(%rax),%reg
5610 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5612 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
5613 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
5615 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5616 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
5618 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5619 view
[4] == 0xe8 || view
[4] == 0xff);
5621 if (view
[4] == 0xe8)
5624 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
5626 memcpy(view
- 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
5631 memcpy(view
- 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
5634 memcpy(view
- 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
5638 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5640 this->skip_call_tls_get_addr_
= true;
5643 // Do a relocation in which we convert a TLS Initial-Exec to a
5648 Target_x86_64
<size
>::Relocate::tls_ie_to_le(
5649 const Relocate_info
<size
, false>* relinfo
,
5651 Output_segment
* tls_segment
,
5652 const elfcpp::Rela
<size
, false>& rela
,
5654 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
5655 unsigned char* view
,
5656 section_size_type view_size
)
5658 // We need to examine the opcodes to figure out which instruction we
5661 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
5662 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
5664 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
5665 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
5667 unsigned char op1
= view
[-3];
5668 unsigned char op2
= view
[-2];
5669 unsigned char op3
= view
[-1];
5670 unsigned char reg
= op3
>> 3;
5677 else if (size
== 32 && op1
== 0x44)
5680 view
[-1] = 0xc0 | reg
;
5684 // Special handling for %rsp.
5687 else if (size
== 32 && op1
== 0x44)
5690 view
[-1] = 0xc0 | reg
;
5697 else if (size
== 32 && op1
== 0x44)
5700 view
[-1] = 0x80 | reg
| (reg
<< 3);
5703 if (tls_segment
!= NULL
)
5704 value
-= tls_segment
->memsz();
5705 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
5708 // Relocate section data.
5712 Target_x86_64
<size
>::relocate_section(
5713 const Relocate_info
<size
, false>* relinfo
,
5714 unsigned int sh_type
,
5715 const unsigned char* prelocs
,
5717 Output_section
* output_section
,
5718 bool needs_special_offset_handling
,
5719 unsigned char* view
,
5720 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5721 section_size_type view_size
,
5722 const Reloc_symbol_changes
* reloc_symbol_changes
)
5724 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5727 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5729 gold::relocate_section
<size
, false, Target_x86_64
<size
>, Relocate
,
5730 gold::Default_comdat_behavior
, Classify_reloc
>(
5736 needs_special_offset_handling
,
5740 reloc_symbol_changes
);
5743 // Apply an incremental relocation. Incremental relocations always refer
5744 // to global symbols.
5748 Target_x86_64
<size
>::apply_relocation(
5749 const Relocate_info
<size
, false>* relinfo
,
5750 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
5751 unsigned int r_type
,
5752 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
5754 unsigned char* view
,
5755 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5756 section_size_type view_size
)
5758 gold::apply_relocation
<size
, false, Target_x86_64
<size
>,
5759 typename Target_x86_64
<size
>::Relocate
>(
5771 // Scan the relocs during a relocatable link.
5775 Target_x86_64
<size
>::scan_relocatable_relocs(
5776 Symbol_table
* symtab
,
5778 Sized_relobj_file
<size
, false>* object
,
5779 unsigned int data_shndx
,
5780 unsigned int sh_type
,
5781 const unsigned char* prelocs
,
5783 Output_section
* output_section
,
5784 bool needs_special_offset_handling
,
5785 size_t local_symbol_count
,
5786 const unsigned char* plocal_symbols
,
5787 Relocatable_relocs
* rr
)
5789 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5791 typedef gold::Default_scan_relocatable_relocs
<Classify_reloc
>
5792 Scan_relocatable_relocs
;
5794 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5796 gold::scan_relocatable_relocs
<size
, false, Scan_relocatable_relocs
>(
5804 needs_special_offset_handling
,
5810 // Scan the relocs for --emit-relocs.
5814 Target_x86_64
<size
>::emit_relocs_scan(
5815 Symbol_table
* symtab
,
5817 Sized_relobj_file
<size
, false>* object
,
5818 unsigned int data_shndx
,
5819 unsigned int sh_type
,
5820 const unsigned char* prelocs
,
5822 Output_section
* output_section
,
5823 bool needs_special_offset_handling
,
5824 size_t local_symbol_count
,
5825 const unsigned char* plocal_syms
,
5826 Relocatable_relocs
* rr
)
5828 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5830 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
5831 Emit_relocs_strategy
;
5833 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5835 gold::scan_relocatable_relocs
<size
, false, Emit_relocs_strategy
>(
5843 needs_special_offset_handling
,
5849 // Relocate a section during a relocatable link.
5853 Target_x86_64
<size
>::relocate_relocs(
5854 const Relocate_info
<size
, false>* relinfo
,
5855 unsigned int sh_type
,
5856 const unsigned char* prelocs
,
5858 Output_section
* output_section
,
5859 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
5860 unsigned char* view
,
5861 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
5862 section_size_type view_size
,
5863 unsigned char* reloc_view
,
5864 section_size_type reloc_view_size
)
5866 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5869 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5871 gold::relocate_relocs
<size
, false, Classify_reloc
>(
5876 offset_in_output_section
,
5884 // Return the value to use for a dynamic which requires special
5885 // treatment. This is how we support equality comparisons of function
5886 // pointers across shared library boundaries, as described in the
5887 // processor specific ABI supplement.
5891 Target_x86_64
<size
>::do_dynsym_value(const Symbol
* gsym
) const
5893 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
5894 return this->plt_address_for_global(gsym
);
5897 // Return a string used to fill a code section with nops to take up
5898 // the specified length.
5902 Target_x86_64
<size
>::do_code_fill(section_size_type length
) const
5906 // Build a jmpq instruction to skip over the bytes.
5907 unsigned char jmp
[5];
5909 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
5910 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
5911 + std::string(length
- 5, static_cast<char>(0x90)));
5914 // Nop sequences of various lengths.
5915 const char nop1
[1] = { '\x90' }; // nop
5916 const char nop2
[2] = { '\x66', '\x90' }; // xchg %ax %ax
5917 const char nop3
[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5918 const char nop4
[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5920 const char nop5
[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5922 const char nop6
[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5923 '\x44', '\x00', '\x00' };
5924 const char nop7
[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5925 '\x00', '\x00', '\x00',
5927 const char nop8
[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5928 '\x00', '\x00', '\x00',
5930 const char nop9
[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5931 '\x84', '\x00', '\x00',
5932 '\x00', '\x00', '\x00' };
5933 const char nop10
[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5934 '\x1f', '\x84', '\x00',
5935 '\x00', '\x00', '\x00',
5937 const char nop11
[11] = { '\x66', '\x66', '\x2e', // data16
5938 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5939 '\x00', '\x00', '\x00',
5941 const char nop12
[12] = { '\x66', '\x66', '\x66', // data16; data16
5942 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5943 '\x84', '\x00', '\x00',
5944 '\x00', '\x00', '\x00' };
5945 const char nop13
[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5946 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5947 '\x1f', '\x84', '\x00',
5948 '\x00', '\x00', '\x00',
5950 const char nop14
[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5951 '\x66', '\x66', '\x2e', // data16
5952 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5953 '\x00', '\x00', '\x00',
5955 const char nop15
[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5956 '\x66', '\x66', '\x66', // data16; data16
5957 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5958 '\x84', '\x00', '\x00',
5959 '\x00', '\x00', '\x00' };
5961 const char* nops
[16] = {
5963 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
5964 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
5967 return std::string(nops
[length
], length
);
5970 // Return the addend to use for a target specific relocation. The
5971 // only target specific relocation is R_X86_64_TLSDESC for a local
5972 // symbol. We want to set the addend is the offset of the local
5973 // symbol in the TLS segment.
5977 Target_x86_64
<size
>::do_reloc_addend(void* arg
, unsigned int r_type
,
5980 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
5981 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
5982 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
5983 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
5984 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
5985 gold_assert(psymval
->is_tls_symbol());
5986 // The value of a TLS symbol is the offset in the TLS segment.
5987 return psymval
->value(ti
.object
, 0);
5990 // Return the value to use for the base of a DW_EH_PE_datarel offset
5991 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
5992 // assembler can not write out the difference between two labels in
5993 // different sections, so instead of using a pc-relative value they
5994 // use an offset from the GOT.
5998 Target_x86_64
<size
>::do_ehframe_datarel_base() const
6000 gold_assert(this->global_offset_table_
!= NULL
);
6001 Symbol
* sym
= this->global_offset_table_
;
6002 Sized_symbol
<size
>* ssym
= static_cast<Sized_symbol
<size
>*>(sym
);
6003 return ssym
->value();
6006 // FNOFFSET in section SHNDX in OBJECT is the start of a function
6007 // compiled with -fsplit-stack. The function calls non-split-stack
6008 // code. We have to change the function so that it always ensures
6009 // that it has enough stack space to run some random function.
6011 static const unsigned char cmp_insn_32
[] = { 0x64, 0x3b, 0x24, 0x25 };
6012 static const unsigned char lea_r10_insn_32
[] = { 0x44, 0x8d, 0x94, 0x24 };
6013 static const unsigned char lea_r11_insn_32
[] = { 0x44, 0x8d, 0x9c, 0x24 };
6015 static const unsigned char cmp_insn_64
[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
6016 static const unsigned char lea_r10_insn_64
[] = { 0x4c, 0x8d, 0x94, 0x24 };
6017 static const unsigned char lea_r11_insn_64
[] = { 0x4c, 0x8d, 0x9c, 0x24 };
6021 Target_x86_64
<size
>::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
6022 section_offset_type fnoffset
,
6023 section_size_type fnsize
,
6024 const unsigned char*,
6026 unsigned char* view
,
6027 section_size_type view_size
,
6029 std::string
* to
) const
6031 const char* const cmp_insn
= reinterpret_cast<const char*>
6032 (size
== 32 ? cmp_insn_32
: cmp_insn_64
);
6033 const char* const lea_r10_insn
= reinterpret_cast<const char*>
6034 (size
== 32 ? lea_r10_insn_32
: lea_r10_insn_64
);
6035 const char* const lea_r11_insn
= reinterpret_cast<const char*>
6036 (size
== 32 ? lea_r11_insn_32
: lea_r11_insn_64
);
6038 const size_t cmp_insn_len
=
6039 (size
== 32 ? sizeof(cmp_insn_32
) : sizeof(cmp_insn_64
));
6040 const size_t lea_r10_insn_len
=
6041 (size
== 32 ? sizeof(lea_r10_insn_32
) : sizeof(lea_r10_insn_64
));
6042 const size_t lea_r11_insn_len
=
6043 (size
== 32 ? sizeof(lea_r11_insn_32
) : sizeof(lea_r11_insn_64
));
6044 const size_t nop_len
= (size
== 32 ? 7 : 8);
6046 // The function starts with a comparison of the stack pointer and a
6047 // field in the TCB. This is followed by a jump.
6050 if (this->match_view(view
, view_size
, fnoffset
, cmp_insn
, cmp_insn_len
)
6051 && fnsize
> nop_len
+ 1)
6053 // We will call __morestack if the carry flag is set after this
6054 // comparison. We turn the comparison into an stc instruction
6056 view
[fnoffset
] = '\xf9';
6057 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, nop_len
);
6059 // lea NN(%rsp),%r10
6060 // lea NN(%rsp),%r11
6061 else if ((this->match_view(view
, view_size
, fnoffset
,
6062 lea_r10_insn
, lea_r10_insn_len
)
6063 || this->match_view(view
, view_size
, fnoffset
,
6064 lea_r11_insn
, lea_r11_insn_len
))
6067 // This is loading an offset from the stack pointer for a
6068 // comparison. The offset is negative, so we decrease the
6069 // offset by the amount of space we need for the stack. This
6070 // means we will avoid calling __morestack if there happens to
6071 // be plenty of space on the stack already.
6072 unsigned char* pval
= view
+ fnoffset
+ 4;
6073 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
6074 val
-= parameters
->options().split_stack_adjust_size();
6075 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
6079 if (!object
->has_no_split_stack())
6080 object
->error(_("failed to match split-stack sequence at "
6081 "section %u offset %0zx"),
6082 shndx
, static_cast<size_t>(fnoffset
));
6086 // We have to change the function so that it calls
6087 // __morestack_non_split instead of __morestack. The former will
6088 // allocate additional stack space.
6089 *from
= "__morestack";
6090 *to
= "__morestack_non_split";
6093 // The selector for x86_64 object files. Note this is never instantiated
6094 // directly. It's only used in Target_selector_x86_64_nacl, below.
6097 class Target_selector_x86_64
: public Target_selector_freebsd
6100 Target_selector_x86_64()
6101 : Target_selector_freebsd(elfcpp::EM_X86_64
, size
, false,
6103 ? "elf64-x86-64" : "elf32-x86-64"),
6105 ? "elf64-x86-64-freebsd"
6106 : "elf32-x86-64-freebsd"),
6107 (size
== 64 ? "elf_x86_64" : "elf32_x86_64"))
6111 do_instantiate_target()
6112 { return new Target_x86_64
<size
>(); }
6116 // NaCl variant. It uses different PLT contents.
6119 class Output_data_plt_x86_64_nacl
: public Output_data_plt_x86_64
<size
>
6122 Output_data_plt_x86_64_nacl(Layout
* layout
,
6123 Output_data_got
<64, false>* got
,
6124 Output_data_got_plt_x86_64
* got_plt
,
6125 Output_data_space
* got_irelative
)
6126 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
6127 got
, got_plt
, got_irelative
)
6130 Output_data_plt_x86_64_nacl(Layout
* layout
,
6131 Output_data_got
<64, false>* got
,
6132 Output_data_got_plt_x86_64
* got_plt
,
6133 Output_data_space
* got_irelative
,
6134 unsigned int plt_count
)
6135 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
6136 got
, got_plt
, got_irelative
,
6141 virtual unsigned int
6142 do_get_plt_entry_size() const
6143 { return plt_entry_size
; }
6146 do_add_eh_frame(Layout
* layout
)
6148 layout
->add_eh_frame_for_plt(this,
6149 this->plt_eh_frame_cie
,
6150 this->plt_eh_frame_cie_size
,
6152 plt_eh_frame_fde_size
);
6156 do_fill_first_plt_entry(unsigned char* pov
,
6157 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
6158 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
6160 virtual unsigned int
6161 do_fill_plt_entry(unsigned char* pov
,
6162 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
6163 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
6164 unsigned int got_offset
,
6165 unsigned int plt_offset
,
6166 unsigned int plt_index
);
6169 do_fill_tlsdesc_entry(unsigned char* pov
,
6170 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
6171 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
6172 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
6173 unsigned int tlsdesc_got_offset
,
6174 unsigned int plt_offset
);
6177 // The size of an entry in the PLT.
6178 static const int plt_entry_size
= 64;
6180 // The first entry in the PLT.
6181 static const unsigned char first_plt_entry
[plt_entry_size
];
6183 // Other entries in the PLT for an executable.
6184 static const unsigned char plt_entry
[plt_entry_size
];
6186 // The reserved TLSDESC entry in the PLT for an executable.
6187 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
6189 // The .eh_frame unwind information for the PLT.
6190 static const int plt_eh_frame_fde_size
= 32;
6191 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
6195 class Target_x86_64_nacl
: public Target_x86_64
<size
>
6198 Target_x86_64_nacl()
6199 : Target_x86_64
<size
>(&x86_64_nacl_info
)
6202 virtual Output_data_plt_x86_64
<size
>*
6203 do_make_data_plt(Layout
* layout
,
6204 Output_data_got
<64, false>* got
,
6205 Output_data_got_plt_x86_64
* got_plt
,
6206 Output_data_space
* got_irelative
)
6208 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
6212 virtual Output_data_plt_x86_64
<size
>*
6213 do_make_data_plt(Layout
* layout
,
6214 Output_data_got
<64, false>* got
,
6215 Output_data_got_plt_x86_64
* got_plt
,
6216 Output_data_space
* got_irelative
,
6217 unsigned int plt_count
)
6219 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
6225 do_code_fill(section_size_type length
) const;
6228 static const Target::Target_info x86_64_nacl_info
;
6232 const Target::Target_info Target_x86_64_nacl
<64>::x86_64_nacl_info
=
6235 false, // is_big_endian
6236 elfcpp::EM_X86_64
, // machine_code
6237 false, // has_make_symbol
6238 false, // has_resolve
6239 true, // has_code_fill
6240 true, // is_default_stack_executable
6241 true, // can_icf_inline_merge_sections
6243 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
6244 0x20000, // default_text_segment_address
6245 0x10000, // abi_pagesize (overridable by -z max-page-size)
6246 0x10000, // common_pagesize (overridable by -z common-page-size)
6247 true, // isolate_execinstr
6248 0x10000000, // rosegment_gap
6249 elfcpp::SHN_UNDEF
, // small_common_shndx
6250 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
6251 0, // small_common_section_flags
6252 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
6253 NULL
, // attributes_section
6254 NULL
, // attributes_vendor
6255 "_start", // entry_symbol_name
6256 32, // hash_entry_size
6257 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
6261 const Target::Target_info Target_x86_64_nacl
<32>::x86_64_nacl_info
=
6264 false, // is_big_endian
6265 elfcpp::EM_X86_64
, // machine_code
6266 false, // has_make_symbol
6267 false, // has_resolve
6268 true, // has_code_fill
6269 true, // is_default_stack_executable
6270 true, // can_icf_inline_merge_sections
6272 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
6273 0x20000, // default_text_segment_address
6274 0x10000, // abi_pagesize (overridable by -z max-page-size)
6275 0x10000, // common_pagesize (overridable by -z common-page-size)
6276 true, // isolate_execinstr
6277 0x10000000, // rosegment_gap
6278 elfcpp::SHN_UNDEF
, // small_common_shndx
6279 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
6280 0, // small_common_section_flags
6281 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
6282 NULL
, // attributes_section
6283 NULL
, // attributes_vendor
6284 "_start", // entry_symbol_name
6285 32, // hash_entry_size
6286 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
6289 #define NACLMASK 0xe0 // 32-byte alignment mask.
6291 // The first entry in the PLT.
6295 Output_data_plt_x86_64_nacl
<size
>::first_plt_entry
[plt_entry_size
] =
6297 0xff, 0x35, // pushq contents of memory address
6298 0, 0, 0, 0, // replaced with address of .got + 8
6299 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
6300 0, 0, 0, 0, // replaced with address of .got + 16
6301 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
6302 0x4d, 0x01, 0xfb, // add %r15, %r11
6303 0x41, 0xff, 0xe3, // jmpq *%r11
6305 // 9-byte nop sequence to pad out to the next 32-byte boundary.
6306 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
6308 // 32 bytes of nop to pad out to the standard size
6309 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6310 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6311 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6312 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6313 0x66, // excess data32 prefix
6319 Output_data_plt_x86_64_nacl
<size
>::do_fill_first_plt_entry(
6321 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
6322 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
6324 memcpy(pov
, first_plt_entry
, plt_entry_size
);
6325 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
6327 - (plt_address
+ 2 + 4)));
6328 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
6330 - (plt_address
+ 9 + 4)));
6333 // Subsequent entries in the PLT.
6337 Output_data_plt_x86_64_nacl
<size
>::plt_entry
[plt_entry_size
] =
6339 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
6340 0, 0, 0, 0, // replaced with address of symbol in .got
6341 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
6342 0x4d, 0x01, 0xfb, // add %r15, %r11
6343 0x41, 0xff, 0xe3, // jmpq *%r11
6345 // 15-byte nop sequence to pad out to the next 32-byte boundary.
6346 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6347 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6349 // Lazy GOT entries point here (32-byte aligned).
6350 0x68, // pushq immediate
6351 0, 0, 0, 0, // replaced with index into relocation table
6352 0xe9, // jmp relative
6353 0, 0, 0, 0, // replaced with offset to start of .plt0
6355 // 22 bytes of nop to pad out to the standard size.
6356 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6357 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6358 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
6363 Output_data_plt_x86_64_nacl
<size
>::do_fill_plt_entry(
6365 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
6366 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
6367 unsigned int got_offset
,
6368 unsigned int plt_offset
,
6369 unsigned int plt_index
)
6371 memcpy(pov
, plt_entry
, plt_entry_size
);
6372 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3,
6373 (got_address
+ got_offset
6374 - (plt_address
+ plt_offset
6377 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 33, plt_index
);
6378 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 38,
6379 - (plt_offset
+ 38 + 4));
6384 // The reserved TLSDESC entry in the PLT.
6388 Output_data_plt_x86_64_nacl
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
6390 0xff, 0x35, // pushq x(%rip)
6391 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
6392 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
6393 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
6394 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
6395 0x4d, 0x01, 0xfb, // add %r15, %r11
6396 0x41, 0xff, 0xe3, // jmpq *%r11
6398 // 41 bytes of nop to pad out to the standard size.
6399 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6400 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6401 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6402 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6403 0x66, 0x66, // excess data32 prefixes
6404 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6409 Output_data_plt_x86_64_nacl
<size
>::do_fill_tlsdesc_entry(
6411 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
6412 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
6413 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
6414 unsigned int tlsdesc_got_offset
,
6415 unsigned int plt_offset
)
6417 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
6418 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
6420 - (plt_address
+ plt_offset
6422 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
6424 + tlsdesc_got_offset
6425 - (plt_address
+ plt_offset
6429 // The .eh_frame unwind information for the PLT.
6433 Output_data_plt_x86_64_nacl
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
6435 0, 0, 0, 0, // Replaced with offset to .plt.
6436 0, 0, 0, 0, // Replaced with size of .plt.
6437 0, // Augmentation size.
6438 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
6439 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
6440 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
6441 elfcpp::DW_CFA_advance_loc
+ 58, // Advance 58 to __PLT__ + 64.
6442 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
6443 13, // Block length.
6444 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
6445 elfcpp::DW_OP_breg16
, 0, // Push %rip.
6446 elfcpp::DW_OP_const1u
, 63, // Push 0x3f.
6447 elfcpp::DW_OP_and
, // & (%rip & 0x3f).
6448 elfcpp::DW_OP_const1u
, 37, // Push 0x25.
6449 elfcpp::DW_OP_ge
, // >= ((%rip & 0x3f) >= 0x25)
6450 elfcpp::DW_OP_lit3
, // Push 3.
6451 elfcpp::DW_OP_shl
, // << (((%rip & 0x3f) >= 0x25) << 3)
6452 elfcpp::DW_OP_plus
, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
6453 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
6457 // Return a string used to fill a code section with nops.
6458 // For NaCl, long NOPs are only valid if they do not cross
6459 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
6462 Target_x86_64_nacl
<size
>::do_code_fill(section_size_type length
) const
6464 return std::string(length
, static_cast<char>(0x90));
6467 // The selector for x86_64-nacl object files.
6470 class Target_selector_x86_64_nacl
6471 : public Target_selector_nacl
<Target_selector_x86_64
<size
>,
6472 Target_x86_64_nacl
<size
> >
6475 Target_selector_x86_64_nacl()
6476 : Target_selector_nacl
<Target_selector_x86_64
<size
>,
6477 Target_x86_64_nacl
<size
> >("x86-64",
6479 ? "elf64-x86-64-nacl"
6480 : "elf32-x86-64-nacl",
6483 : "elf32_x86_64_nacl")
6487 Target_selector_x86_64_nacl
<64> target_selector_x86_64
;
6488 Target_selector_x86_64_nacl
<32> target_selector_x32
;
6490 } // End anonymous namespace.