1 // x86_64.cc -- x86_64 target support for gold.
3 // Copyright (C) 2006-2018 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_feature_1_(0), seen_first_object_(false)
712 // Hook for a new output section.
714 do_new_output_section(Output_section
*) const;
716 // Scan the relocations to look for symbol adjustments.
718 gc_process_relocs(Symbol_table
* symtab
,
720 Sized_relobj_file
<size
, false>* object
,
721 unsigned int data_shndx
,
722 unsigned int sh_type
,
723 const unsigned char* prelocs
,
725 Output_section
* output_section
,
726 bool needs_special_offset_handling
,
727 size_t local_symbol_count
,
728 const unsigned char* plocal_symbols
);
730 // Scan the relocations to look for symbol adjustments.
732 scan_relocs(Symbol_table
* symtab
,
734 Sized_relobj_file
<size
, false>* object
,
735 unsigned int data_shndx
,
736 unsigned int sh_type
,
737 const unsigned char* prelocs
,
739 Output_section
* output_section
,
740 bool needs_special_offset_handling
,
741 size_t local_symbol_count
,
742 const unsigned char* plocal_symbols
);
744 // Finalize the sections.
746 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
748 // Return the value to use for a dynamic which requires special
751 do_dynsym_value(const Symbol
*) const;
753 // Relocate a section.
755 relocate_section(const Relocate_info
<size
, false>*,
756 unsigned int sh_type
,
757 const unsigned char* prelocs
,
759 Output_section
* output_section
,
760 bool needs_special_offset_handling
,
762 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
763 section_size_type view_size
,
764 const Reloc_symbol_changes
*);
766 // Scan the relocs during a relocatable link.
768 scan_relocatable_relocs(Symbol_table
* symtab
,
770 Sized_relobj_file
<size
, false>* object
,
771 unsigned int data_shndx
,
772 unsigned int sh_type
,
773 const unsigned char* prelocs
,
775 Output_section
* output_section
,
776 bool needs_special_offset_handling
,
777 size_t local_symbol_count
,
778 const unsigned char* plocal_symbols
,
779 Relocatable_relocs
*);
781 // Scan the relocs for --emit-relocs.
783 emit_relocs_scan(Symbol_table
* symtab
,
785 Sized_relobj_file
<size
, false>* object
,
786 unsigned int data_shndx
,
787 unsigned int sh_type
,
788 const unsigned char* prelocs
,
790 Output_section
* output_section
,
791 bool needs_special_offset_handling
,
792 size_t local_symbol_count
,
793 const unsigned char* plocal_syms
,
794 Relocatable_relocs
* rr
);
796 // Emit relocations for a section.
799 const Relocate_info
<size
, false>*,
800 unsigned int sh_type
,
801 const unsigned char* prelocs
,
803 Output_section
* output_section
,
804 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
806 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
807 section_size_type view_size
,
808 unsigned char* reloc_view
,
809 section_size_type reloc_view_size
);
811 // Return a string used to fill a code section with nops.
813 do_code_fill(section_size_type length
) const;
815 // Return whether SYM is defined by the ABI.
817 do_is_defined_by_abi(const Symbol
* sym
) const
818 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
820 // Return the symbol index to use for a target specific relocation.
821 // The only target specific relocation is R_X86_64_TLSDESC for a
822 // local symbol, which is an absolute reloc.
824 do_reloc_symbol_index(void*, unsigned int r_type
) const
826 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
830 // Return the addend to use for a target specific relocation.
832 do_reloc_addend(void* arg
, unsigned int r_type
, uint64_t addend
) const;
834 // Return the PLT section.
836 do_plt_address_for_global(const Symbol
* gsym
) const
837 { return this->plt_section()->address_for_global(gsym
); }
840 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
841 { return this->plt_section()->address_for_local(relobj
, symndx
); }
843 // This function should be defined in targets that can use relocation
844 // types to determine (implemented in local_reloc_may_be_function_pointer
845 // and global_reloc_may_be_function_pointer)
846 // if a function's pointer is taken. ICF uses this in safe mode to only
847 // fold those functions whose pointer is defintely not taken. For x86_64
848 // pie binaries, safe ICF cannot be done by looking at only relocation
849 // types, and for certain cases (e.g. R_X86_64_PC32), the instruction
850 // opcode is checked as well to distinguish a function call from taking
851 // a function's pointer.
853 do_can_check_for_function_pointers() const
856 // Return the base for a DW_EH_PE_datarel encoding.
858 do_ehframe_datarel_base() const;
860 // Adjust -fsplit-stack code which calls non-split-stack code.
862 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
863 section_offset_type fnoffset
, section_size_type fnsize
,
864 const unsigned char* prelocs
, size_t reloc_count
,
865 unsigned char* view
, section_size_type view_size
,
866 std::string
* from
, std::string
* to
) const;
868 // Return the size of the GOT section.
872 gold_assert(this->got_
!= NULL
);
873 return this->got_
->data_size();
876 // Return the number of entries in the GOT.
878 got_entry_count() const
880 if (this->got_
== NULL
)
882 return this->got_size() / 8;
885 // Return the number of entries in the PLT.
887 plt_entry_count() const;
889 // Return the offset of the first non-reserved PLT entry.
891 first_plt_entry_offset() const;
893 // Return the size of each PLT entry.
895 plt_entry_size() const;
897 // Return the size of each GOT entry.
899 got_entry_size() const
902 // Create the GOT section for an incremental update.
903 Output_data_got_base
*
904 init_got_plt_for_update(Symbol_table
* symtab
,
906 unsigned int got_count
,
907 unsigned int plt_count
);
909 // Reserve a GOT entry for a local symbol, and regenerate any
910 // necessary dynamic relocations.
912 reserve_local_got_entry(unsigned int got_index
,
913 Sized_relobj
<size
, false>* obj
,
915 unsigned int got_type
);
917 // Reserve a GOT entry for a global symbol, and regenerate any
918 // necessary dynamic relocations.
920 reserve_global_got_entry(unsigned int got_index
, Symbol
* gsym
,
921 unsigned int got_type
);
923 // Register an existing PLT entry for a global symbol.
925 register_global_plt_entry(Symbol_table
*, Layout
*, unsigned int plt_index
,
928 // Force a COPY relocation for a given symbol.
930 emit_copy_reloc(Symbol_table
*, Symbol
*, Output_section
*, off_t
);
932 // Apply an incremental relocation.
934 apply_relocation(const Relocate_info
<size
, false>* relinfo
,
935 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
937 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
940 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
941 section_size_type view_size
);
943 // Add a new reloc argument, returning the index in the vector.
945 add_tlsdesc_info(Sized_relobj_file
<size
, false>* object
, unsigned int r_sym
)
947 this->tlsdesc_reloc_info_
.push_back(Tlsdesc_info(object
, r_sym
));
948 return this->tlsdesc_reloc_info_
.size() - 1;
951 Output_data_plt_x86_64
<size
>*
952 make_data_plt(Layout
* layout
,
953 Output_data_got
<64, false>* got
,
954 Output_data_got_plt_x86_64
* got_plt
,
955 Output_data_space
* got_irelative
)
957 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
);
960 Output_data_plt_x86_64
<size
>*
961 make_data_plt(Layout
* layout
,
962 Output_data_got
<64, false>* got
,
963 Output_data_got_plt_x86_64
* got_plt
,
964 Output_data_space
* got_irelative
,
965 unsigned int plt_count
)
967 return this->do_make_data_plt(layout
, got
, got_plt
, got_irelative
,
971 virtual Output_data_plt_x86_64
<size
>*
972 do_make_data_plt(Layout
* layout
,
973 Output_data_got
<64, false>* got
,
974 Output_data_got_plt_x86_64
* got_plt
,
975 Output_data_space
* got_irelative
);
977 virtual Output_data_plt_x86_64
<size
>*
978 do_make_data_plt(Layout
* layout
,
979 Output_data_got
<64, false>* got
,
980 Output_data_got_plt_x86_64
* got_plt
,
981 Output_data_space
* got_irelative
,
982 unsigned int plt_count
);
985 // The class which scans relocations.
990 : issued_non_pic_error_(false)
994 get_reference_flags(unsigned int r_type
);
997 local(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
998 Sized_relobj_file
<size
, false>* object
,
999 unsigned int data_shndx
,
1000 Output_section
* output_section
,
1001 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
1002 const elfcpp::Sym
<size
, false>& lsym
,
1006 global(Symbol_table
* symtab
, Layout
* layout
, Target_x86_64
* target
,
1007 Sized_relobj_file
<size
, false>* object
,
1008 unsigned int data_shndx
,
1009 Output_section
* output_section
,
1010 const elfcpp::Rela
<size
, false>& reloc
, unsigned int r_type
,
1014 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
1015 Target_x86_64
* target
,
1016 Sized_relobj_file
<size
, false>* object
,
1017 unsigned int data_shndx
,
1018 Output_section
* output_section
,
1019 const elfcpp::Rela
<size
, false>& reloc
,
1020 unsigned int r_type
,
1021 const elfcpp::Sym
<size
, false>& lsym
);
1024 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
1025 Target_x86_64
* target
,
1026 Sized_relobj_file
<size
, false>* object
,
1027 unsigned int data_shndx
,
1028 Output_section
* output_section
,
1029 const elfcpp::Rela
<size
, false>& reloc
,
1030 unsigned int r_type
,
1035 unsupported_reloc_local(Sized_relobj_file
<size
, false>*,
1036 unsigned int r_type
);
1039 unsupported_reloc_global(Sized_relobj_file
<size
, false>*,
1040 unsigned int r_type
, Symbol
*);
1043 check_non_pic(Relobj
*, unsigned int r_type
, Symbol
*);
1046 possible_function_pointer_reloc(Sized_relobj_file
<size
, false>* src_obj
,
1047 unsigned int src_indx
,
1048 unsigned int r_offset
,
1049 unsigned int r_type
);
1052 reloc_needs_plt_for_ifunc(Sized_relobj_file
<size
, false>*,
1053 unsigned int r_type
);
1055 // Whether we have issued an error about a non-PIC compilation.
1056 bool issued_non_pic_error_
;
1059 // The class which implements relocation.
1064 : skip_call_tls_get_addr_(false)
1069 if (this->skip_call_tls_get_addr_
)
1071 // FIXME: This needs to specify the location somehow.
1072 gold_error(_("missing expected TLS relocation"));
1076 // Do a relocation. Return false if the caller should not issue
1077 // any warnings about this relocation.
1079 relocate(const Relocate_info
<size
, false>*, unsigned int,
1080 Target_x86_64
*, Output_section
*, size_t, const unsigned char*,
1081 const Sized_symbol
<size
>*, const Symbol_value
<size
>*,
1082 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1086 // Do a TLS relocation.
1088 relocate_tls(const Relocate_info
<size
, false>*, Target_x86_64
*,
1089 size_t relnum
, const elfcpp::Rela
<size
, false>&,
1090 unsigned int r_type
, const Sized_symbol
<size
>*,
1091 const Symbol_value
<size
>*,
1092 unsigned char*, typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1095 // Do a TLS General-Dynamic to Initial-Exec transition.
1097 tls_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
1098 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1099 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1100 unsigned char* view
,
1101 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1102 section_size_type view_size
);
1104 // Do a TLS General-Dynamic to Local-Exec transition.
1106 tls_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1107 Output_segment
* tls_segment
,
1108 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1109 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1110 unsigned char* view
,
1111 section_size_type view_size
);
1113 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
1115 tls_desc_gd_to_ie(const Relocate_info
<size
, false>*, size_t relnum
,
1116 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1117 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1118 unsigned char* view
,
1119 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
1120 section_size_type view_size
);
1122 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
1124 tls_desc_gd_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1125 Output_segment
* tls_segment
,
1126 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1127 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1128 unsigned char* view
,
1129 section_size_type view_size
);
1131 // Do a TLS Local-Dynamic to Local-Exec transition.
1133 tls_ld_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1134 Output_segment
* tls_segment
,
1135 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1136 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1137 unsigned char* view
,
1138 section_size_type view_size
);
1140 // Do a TLS Initial-Exec to Local-Exec transition.
1142 tls_ie_to_le(const Relocate_info
<size
, false>*, size_t relnum
,
1143 Output_segment
* tls_segment
,
1144 const elfcpp::Rela
<size
, false>&, unsigned int r_type
,
1145 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
1146 unsigned char* view
,
1147 section_size_type view_size
);
1149 // This is set if we should skip the next reloc, which should be a
1150 // PLT32 reloc against ___tls_get_addr.
1151 bool skip_call_tls_get_addr_
;
1154 // Check if relocation against this symbol is a candidate for
1156 // mov foo@GOTPCREL(%rip), %reg
1157 // to lea foo(%rip), %reg.
1158 template<class View_type
>
1160 can_convert_mov_to_lea(const Symbol
* gsym
, unsigned int r_type
,
1161 size_t r_offset
, View_type
* view
)
1163 gold_assert(gsym
!= NULL
);
1164 // We cannot do the conversion unless it's one of these relocations.
1165 if (r_type
!= elfcpp::R_X86_64_GOTPCREL
1166 && r_type
!= elfcpp::R_X86_64_GOTPCRELX
1167 && r_type
!= elfcpp::R_X86_64_REX_GOTPCRELX
)
1169 // We cannot convert references to IFUNC symbols, or to symbols that
1170 // are not local to the current module.
1171 // We can't do predefined symbols because they may become undefined
1172 // (e.g., __ehdr_start when the headers aren't mapped to a segment).
1173 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1174 || gsym
->is_undefined()
1175 || gsym
->is_predefined()
1176 || gsym
->is_from_dynobj()
1177 || gsym
->is_preemptible())
1179 // If we are building a shared object and the symbol is protected, we may
1180 // need to go through the GOT.
1181 if (parameters
->options().shared()
1182 && gsym
->visibility() == elfcpp::STV_PROTECTED
)
1184 // We cannot convert references to the _DYNAMIC symbol.
1185 if (strcmp(gsym
->name(), "_DYNAMIC") == 0)
1187 // Check for a MOV opcode.
1188 return (*view
)[r_offset
- 2] == 0x8b;
1192 // callq *foo@GOTPCRELX(%rip) to
1194 // and jmpq *foo@GOTPCRELX(%rip) to
1197 template<class View_type
>
1199 can_convert_callq_to_direct(const Symbol
* gsym
, unsigned int r_type
,
1200 size_t r_offset
, View_type
* view
)
1202 gold_assert(gsym
!= NULL
);
1203 // We cannot do the conversion unless it's a GOTPCRELX relocation.
1204 if (r_type
!= elfcpp::R_X86_64_GOTPCRELX
)
1206 // We cannot convert references to IFUNC symbols, or to symbols that
1207 // are not local to the current module.
1208 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1209 || gsym
->is_undefined ()
1210 || gsym
->is_from_dynobj()
1211 || gsym
->is_preemptible())
1213 // Check for a CALLQ or JMPQ opcode.
1214 return ((*view
)[r_offset
- 2] == 0xff
1215 && ((*view
)[r_offset
- 1] == 0x15
1216 || (*view
)[r_offset
- 1] == 0x25));
1219 // Adjust TLS relocation type based on the options and whether this
1220 // is a local symbol.
1221 static tls::Tls_optimization
1222 optimize_tls_reloc(bool is_final
, int r_type
);
1224 // Get the GOT section, creating it if necessary.
1225 Output_data_got
<64, false>*
1226 got_section(Symbol_table
*, Layout
*);
1228 // Get the GOT PLT section.
1229 Output_data_got_plt_x86_64
*
1230 got_plt_section() const
1232 gold_assert(this->got_plt_
!= NULL
);
1233 return this->got_plt_
;
1236 // Get the GOT section for TLSDESC entries.
1237 Output_data_got
<64, false>*
1238 got_tlsdesc_section() const
1240 gold_assert(this->got_tlsdesc_
!= NULL
);
1241 return this->got_tlsdesc_
;
1244 // Create the PLT section.
1246 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
1248 // Create a PLT entry for a global symbol.
1250 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
1252 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
1254 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
1255 Sized_relobj_file
<size
, false>* relobj
,
1256 unsigned int local_sym_index
);
1258 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1260 define_tls_base_symbol(Symbol_table
*, Layout
*);
1262 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1264 reserve_tlsdesc_entries(Symbol_table
* symtab
, Layout
* layout
);
1266 // Create a GOT entry for the TLS module index.
1268 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1269 Sized_relobj_file
<size
, false>* object
);
1271 // Get the PLT section.
1272 Output_data_plt_x86_64
<size
>*
1275 gold_assert(this->plt_
!= NULL
);
1279 // Get the dynamic reloc section, creating it if necessary.
1281 rela_dyn_section(Layout
*);
1283 // Get the section to use for TLSDESC relocations.
1285 rela_tlsdesc_section(Layout
*) const;
1287 // Get the section to use for IRELATIVE relocations.
1289 rela_irelative_section(Layout
*);
1291 // Add a potential copy relocation.
1293 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
1294 Sized_relobj_file
<size
, false>* object
,
1295 unsigned int shndx
, Output_section
* output_section
,
1296 Symbol
* sym
, const elfcpp::Rela
<size
, false>& reloc
)
1298 unsigned int r_type
= elfcpp::elf_r_type
<size
>(reloc
.get_r_info());
1299 this->copy_relocs_
.copy_reloc(symtab
, layout
,
1300 symtab
->get_sized_symbol
<size
>(sym
),
1301 object
, shndx
, output_section
,
1302 r_type
, reloc
.get_r_offset(),
1303 reloc
.get_r_addend(),
1304 this->rela_dyn_section(layout
));
1307 // Record a target-specific program property in the .note.gnu.property
1310 record_gnu_property(unsigned int, unsigned int, size_t,
1311 const unsigned char*, const Object
*);
1313 // Merge the target-specific program properties from the current object.
1315 merge_gnu_properties(const Object
*);
1317 // Finalize the target-specific program properties and add them back to
1320 do_finalize_gnu_properties(Layout
*) const;
1322 // Information about this specific target which we pass to the
1323 // general Target structure.
1324 static const Target::Target_info x86_64_info
;
1326 // The types of GOT entries needed for this platform.
1327 // These values are exposed to the ABI in an incremental link.
1328 // Do not renumber existing values without changing the version
1329 // number of the .gnu_incremental_inputs section.
1332 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
1333 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
1334 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
1335 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
1338 // This type is used as the argument to the target specific
1339 // relocation routines. The only target specific reloc is
1340 // R_X86_64_TLSDESC against a local symbol.
1343 Tlsdesc_info(Sized_relobj_file
<size
, false>* a_object
, unsigned int a_r_sym
)
1344 : object(a_object
), r_sym(a_r_sym
)
1347 // The object in which the local symbol is defined.
1348 Sized_relobj_file
<size
, false>* object
;
1349 // The local symbol index in the object.
1354 Output_data_got
<64, false>* got_
;
1356 Output_data_plt_x86_64
<size
>* plt_
;
1357 // The GOT PLT section.
1358 Output_data_got_plt_x86_64
* got_plt_
;
1359 // The GOT section for IRELATIVE relocations.
1360 Output_data_space
* got_irelative_
;
1361 // The GOT section for TLSDESC relocations.
1362 Output_data_got
<64, false>* got_tlsdesc_
;
1363 // The _GLOBAL_OFFSET_TABLE_ symbol.
1364 Symbol
* global_offset_table_
;
1365 // The dynamic reloc section.
1366 Reloc_section
* rela_dyn_
;
1367 // The section to use for IRELATIVE relocs.
1368 Reloc_section
* rela_irelative_
;
1369 // Relocs saved to avoid a COPY reloc.
1370 Copy_relocs
<elfcpp::SHT_RELA
, size
, false> copy_relocs_
;
1371 // Offset of the GOT entry for the TLS module index.
1372 unsigned int got_mod_index_offset_
;
1373 // We handle R_X86_64_TLSDESC against a local symbol as a target
1374 // specific relocation. Here we store the object and local symbol
1375 // index for the relocation.
1376 std::vector
<Tlsdesc_info
> tlsdesc_reloc_info_
;
1377 // True if the _TLS_MODULE_BASE_ symbol has been defined.
1378 bool tls_base_symbol_defined_
;
1379 // Target-specific program properties, from .note.gnu.property section.
1380 // Each bit represents a specific feature.
1381 uint32_t isa_1_used_
;
1382 uint32_t isa_1_needed_
;
1383 uint32_t feature_1_
;
1384 // Target-specific properties from the current object.
1385 // These bits get ANDed into FEATURE_1_ after all properties for the object
1386 // have been processed.
1387 uint32_t object_feature_1_
;
1388 // Whether we have seen our first object, for use in initializing FEATURE_1_.
1389 bool seen_first_object_
;
1393 const Target::Target_info Target_x86_64
<64>::x86_64_info
=
1396 false, // is_big_endian
1397 elfcpp::EM_X86_64
, // machine_code
1398 false, // has_make_symbol
1399 false, // has_resolve
1400 true, // has_code_fill
1401 true, // is_default_stack_executable
1402 true, // can_icf_inline_merge_sections
1404 "/lib/ld64.so.1", // program interpreter
1405 0x400000, // default_text_segment_address
1406 0x1000, // abi_pagesize (overridable by -z max-page-size)
1407 0x1000, // common_pagesize (overridable by -z common-page-size)
1408 false, // isolate_execinstr
1410 elfcpp::SHN_UNDEF
, // small_common_shndx
1411 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1412 0, // small_common_section_flags
1413 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1414 NULL
, // attributes_section
1415 NULL
, // attributes_vendor
1416 "_start", // entry_symbol_name
1417 32, // hash_entry_size
1418 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
1422 const Target::Target_info Target_x86_64
<32>::x86_64_info
=
1425 false, // is_big_endian
1426 elfcpp::EM_X86_64
, // machine_code
1427 false, // has_make_symbol
1428 false, // has_resolve
1429 true, // has_code_fill
1430 true, // is_default_stack_executable
1431 true, // can_icf_inline_merge_sections
1433 "/libx32/ldx32.so.1", // program interpreter
1434 0x400000, // default_text_segment_address
1435 0x1000, // abi_pagesize (overridable by -z max-page-size)
1436 0x1000, // common_pagesize (overridable by -z common-page-size)
1437 false, // isolate_execinstr
1439 elfcpp::SHN_UNDEF
, // small_common_shndx
1440 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
1441 0, // small_common_section_flags
1442 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
1443 NULL
, // attributes_section
1444 NULL
, // attributes_vendor
1445 "_start", // entry_symbol_name
1446 32, // hash_entry_size
1447 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
1450 // This is called when a new output section is created. This is where
1451 // we handle the SHF_X86_64_LARGE.
1455 Target_x86_64
<size
>::do_new_output_section(Output_section
* os
) const
1457 if ((os
->flags() & elfcpp::SHF_X86_64_LARGE
) != 0)
1458 os
->set_is_large_section();
1461 // Get the GOT section, creating it if necessary.
1464 Output_data_got
<64, false>*
1465 Target_x86_64
<size
>::got_section(Symbol_table
* symtab
, Layout
* layout
)
1467 if (this->got_
== NULL
)
1469 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
1471 // When using -z now, we can treat .got.plt as a relro section.
1472 // Without -z now, it is modified after program startup by lazy
1474 bool is_got_plt_relro
= parameters
->options().now();
1475 Output_section_order got_order
= (is_got_plt_relro
1477 : ORDER_RELRO_LAST
);
1478 Output_section_order got_plt_order
= (is_got_plt_relro
1480 : ORDER_NON_RELRO_FIRST
);
1482 this->got_
= new Output_data_got
<64, false>();
1484 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
1486 | elfcpp::SHF_WRITE
),
1487 this->got_
, got_order
, true);
1489 this->got_plt_
= new Output_data_got_plt_x86_64(layout
);
1490 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1492 | elfcpp::SHF_WRITE
),
1493 this->got_plt_
, got_plt_order
,
1496 // The first three entries are reserved.
1497 this->got_plt_
->set_current_data_size(3 * 8);
1499 if (!is_got_plt_relro
)
1501 // Those bytes can go into the relro segment.
1502 layout
->increase_relro(3 * 8);
1505 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1506 this->global_offset_table_
=
1507 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
1508 Symbol_table::PREDEFINED
,
1510 0, 0, elfcpp::STT_OBJECT
,
1512 elfcpp::STV_HIDDEN
, 0,
1515 // If there are any IRELATIVE relocations, they get GOT entries
1516 // in .got.plt after the jump slot entries.
1517 this->got_irelative_
= new Output_data_space(8, "** GOT IRELATIVE PLT");
1518 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1520 | elfcpp::SHF_WRITE
),
1521 this->got_irelative_
,
1522 got_plt_order
, is_got_plt_relro
);
1524 // If there are any TLSDESC relocations, they get GOT entries in
1525 // .got.plt after the jump slot and IRELATIVE entries.
1526 this->got_tlsdesc_
= new Output_data_got
<64, false>();
1527 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
1529 | elfcpp::SHF_WRITE
),
1531 got_plt_order
, is_got_plt_relro
);
1537 // Get the dynamic reloc section, creating it if necessary.
1540 typename Target_x86_64
<size
>::Reloc_section
*
1541 Target_x86_64
<size
>::rela_dyn_section(Layout
* layout
)
1543 if (this->rela_dyn_
== NULL
)
1545 gold_assert(layout
!= NULL
);
1546 this->rela_dyn_
= new Reloc_section(parameters
->options().combreloc());
1547 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1548 elfcpp::SHF_ALLOC
, this->rela_dyn_
,
1549 ORDER_DYNAMIC_RELOCS
, false);
1551 return this->rela_dyn_
;
1554 // Get the section to use for IRELATIVE relocs, creating it if
1555 // necessary. These go in .rela.dyn, but only after all other dynamic
1556 // relocations. They need to follow the other dynamic relocations so
1557 // that they can refer to global variables initialized by those
1561 typename Target_x86_64
<size
>::Reloc_section
*
1562 Target_x86_64
<size
>::rela_irelative_section(Layout
* layout
)
1564 if (this->rela_irelative_
== NULL
)
1566 // Make sure we have already created the dynamic reloc section.
1567 this->rela_dyn_section(layout
);
1568 this->rela_irelative_
= new Reloc_section(false);
1569 layout
->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA
,
1570 elfcpp::SHF_ALLOC
, this->rela_irelative_
,
1571 ORDER_DYNAMIC_RELOCS
, false);
1572 gold_assert(this->rela_dyn_
->output_section()
1573 == this->rela_irelative_
->output_section());
1575 return this->rela_irelative_
;
1578 // Record a target-specific program property from the .note.gnu.property
1582 Target_x86_64
<size
>::record_gnu_property(
1583 unsigned int, unsigned int pr_type
,
1584 size_t pr_datasz
, const unsigned char* pr_data
,
1585 const Object
* object
)
1591 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED
:
1592 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED
:
1593 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND
:
1596 gold_warning(_("%s: corrupt .note.gnu.property section "
1597 "(pr_datasz for property %d is not 4)"),
1598 object
->name().c_str(), pr_type
);
1601 val
= elfcpp::Swap
<32, false>::readval(pr_data
);
1604 gold_warning(_("%s: unknown program property type 0x%x "
1605 "in .note.gnu.property section"),
1606 object
->name().c_str(), pr_type
);
1612 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED
:
1613 this->isa_1_used_
|= val
;
1615 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED
:
1616 this->isa_1_needed_
|= val
;
1618 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND
:
1619 // If we see multiple feature props in one object, OR them together.
1620 this->object_feature_1_
|= val
;
1625 // Merge the target-specific program properties from the current object.
1628 Target_x86_64
<size
>::merge_gnu_properties(const Object
*)
1630 if (this->seen_first_object_
)
1631 this->feature_1_
&= this->object_feature_1_
;
1634 this->feature_1_
= this->object_feature_1_
;
1635 this->seen_first_object_
= true;
1637 this->object_feature_1_
= 0;
1641 add_property(Layout
* layout
, unsigned int pr_type
, uint32_t val
)
1643 unsigned char buf
[4];
1644 elfcpp::Swap
<32, false>::writeval(buf
, val
);
1645 layout
->add_gnu_property(elfcpp::NT_GNU_PROPERTY_TYPE_0
, pr_type
, 4, buf
);
1648 // Finalize the target-specific program properties and add them back to
1652 Target_x86_64
<size
>::do_finalize_gnu_properties(Layout
* layout
) const
1654 if (this->isa_1_used_
!= 0)
1655 add_property(layout
, elfcpp::GNU_PROPERTY_X86_ISA_1_USED
,
1657 if (this->isa_1_needed_
!= 0)
1658 add_property(layout
, elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED
,
1659 this->isa_1_needed_
);
1660 if (this->feature_1_
!= 0)
1661 add_property(layout
, elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND
,
1665 // Write the first three reserved words of the .got.plt section.
1666 // The remainder of the section is written while writing the PLT
1667 // in Output_data_plt_i386::do_write.
1670 Output_data_got_plt_x86_64::do_write(Output_file
* of
)
1672 // The first entry in the GOT is the address of the .dynamic section
1673 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1674 // We saved space for them when we created the section in
1675 // Target_x86_64::got_section.
1676 const off_t got_file_offset
= this->offset();
1677 gold_assert(this->data_size() >= 24);
1678 unsigned char* const got_view
= of
->get_output_view(got_file_offset
, 24);
1679 Output_section
* dynamic
= this->layout_
->dynamic_section();
1680 uint64_t dynamic_addr
= dynamic
== NULL
? 0 : dynamic
->address();
1681 elfcpp::Swap
<64, false>::writeval(got_view
, dynamic_addr
);
1682 memset(got_view
+ 8, 0, 16);
1683 of
->write_output_view(got_file_offset
, 24, got_view
);
1686 // Initialize the PLT section.
1690 Output_data_plt_x86_64
<size
>::init(Layout
* layout
)
1692 this->rel_
= new Reloc_section(false);
1693 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1694 elfcpp::SHF_ALLOC
, this->rel_
,
1695 ORDER_DYNAMIC_PLT_RELOCS
, false);
1700 Output_data_plt_x86_64
<size
>::do_adjust_output_section(Output_section
* os
)
1702 os
->set_entsize(this->get_plt_entry_size());
1705 // Add an entry to the PLT.
1709 Output_data_plt_x86_64
<size
>::add_entry(Symbol_table
* symtab
, Layout
* layout
,
1712 gold_assert(!gsym
->has_plt_offset());
1714 unsigned int plt_index
;
1716 section_offset_type got_offset
;
1718 unsigned int* pcount
;
1719 unsigned int offset
;
1720 unsigned int reserved
;
1721 Output_section_data_build
* got
;
1722 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1723 && gsym
->can_use_relative_reloc(false))
1725 pcount
= &this->irelative_count_
;
1728 got
= this->got_irelative_
;
1732 pcount
= &this->count_
;
1735 got
= this->got_plt_
;
1738 if (!this->is_data_size_valid())
1740 // Note that when setting the PLT offset for a non-IRELATIVE
1741 // entry we skip the initial reserved PLT entry.
1742 plt_index
= *pcount
+ offset
;
1743 plt_offset
= plt_index
* this->get_plt_entry_size();
1747 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1748 gold_assert(got_offset
== got
->current_data_size());
1750 // Every PLT entry needs a GOT entry which points back to the PLT
1751 // entry (this will be changed by the dynamic linker, normally
1752 // lazily when the function is called).
1753 got
->set_current_data_size(got_offset
+ 8);
1757 // FIXME: This is probably not correct for IRELATIVE relocs.
1759 // For incremental updates, find an available slot.
1760 plt_offset
= this->free_list_
.allocate(this->get_plt_entry_size(),
1761 this->get_plt_entry_size(), 0);
1762 if (plt_offset
== -1)
1763 gold_fallback(_("out of patch space (PLT);"
1764 " relink with --incremental-full"));
1766 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
1767 // can be calculated from the PLT index, adjusting for the three
1768 // reserved entries at the beginning of the GOT.
1769 plt_index
= plt_offset
/ this->get_plt_entry_size() - 1;
1770 got_offset
= (plt_index
- offset
+ reserved
) * 8;
1773 gsym
->set_plt_offset(plt_offset
);
1775 // Every PLT entry needs a reloc.
1776 this->add_relocation(symtab
, layout
, gsym
, got_offset
);
1778 // Note that we don't need to save the symbol. The contents of the
1779 // PLT are independent of which symbols are used. The symbols only
1780 // appear in the relocations.
1783 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
1788 Output_data_plt_x86_64
<size
>::add_local_ifunc_entry(
1789 Symbol_table
* symtab
,
1791 Sized_relobj_file
<size
, false>* relobj
,
1792 unsigned int local_sym_index
)
1794 unsigned int plt_offset
= this->irelative_count_
* this->get_plt_entry_size();
1795 ++this->irelative_count_
;
1797 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
1799 // Every PLT entry needs a GOT entry which points back to the PLT
1801 this->got_irelative_
->set_current_data_size(got_offset
+ 8);
1803 // Every PLT entry needs a reloc.
1804 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1805 rela
->add_symbolless_local_addend(relobj
, local_sym_index
,
1806 elfcpp::R_X86_64_IRELATIVE
,
1807 this->got_irelative_
, got_offset
, 0);
1812 // Add the relocation for a PLT entry.
1816 Output_data_plt_x86_64
<size
>::add_relocation(Symbol_table
* symtab
,
1819 unsigned int got_offset
)
1821 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1822 && gsym
->can_use_relative_reloc(false))
1824 Reloc_section
* rela
= this->rela_irelative(symtab
, layout
);
1825 rela
->add_symbolless_global_addend(gsym
, elfcpp::R_X86_64_IRELATIVE
,
1826 this->got_irelative_
, got_offset
, 0);
1830 gsym
->set_needs_dynsym_entry();
1831 this->rel_
->add_global(gsym
, elfcpp::R_X86_64_JUMP_SLOT
, this->got_plt_
,
1836 // Return where the TLSDESC relocations should go, creating it if
1837 // necessary. These follow the JUMP_SLOT relocations.
1840 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1841 Output_data_plt_x86_64
<size
>::rela_tlsdesc(Layout
* layout
)
1843 if (this->tlsdesc_rel_
== NULL
)
1845 this->tlsdesc_rel_
= new Reloc_section(false);
1846 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1847 elfcpp::SHF_ALLOC
, this->tlsdesc_rel_
,
1848 ORDER_DYNAMIC_PLT_RELOCS
, false);
1849 gold_assert(this->tlsdesc_rel_
->output_section()
1850 == this->rel_
->output_section());
1852 return this->tlsdesc_rel_
;
1855 // Return where the IRELATIVE relocations should go in the PLT. These
1856 // follow the JUMP_SLOT and the TLSDESC relocations.
1859 typename Output_data_plt_x86_64
<size
>::Reloc_section
*
1860 Output_data_plt_x86_64
<size
>::rela_irelative(Symbol_table
* symtab
,
1863 if (this->irelative_rel_
== NULL
)
1865 // Make sure we have a place for the TLSDESC relocations, in
1866 // case we see any later on.
1867 this->rela_tlsdesc(layout
);
1868 this->irelative_rel_
= new Reloc_section(false);
1869 layout
->add_output_section_data(".rela.plt", elfcpp::SHT_RELA
,
1870 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
1871 ORDER_DYNAMIC_PLT_RELOCS
, false);
1872 gold_assert(this->irelative_rel_
->output_section()
1873 == this->rel_
->output_section());
1875 if (parameters
->doing_static_link())
1877 // A statically linked executable will only have a .rela.plt
1878 // section to hold R_X86_64_IRELATIVE relocs for
1879 // STT_GNU_IFUNC symbols. The library will use these
1880 // symbols to locate the IRELATIVE relocs at program startup
1882 symtab
->define_in_output_data("__rela_iplt_start", NULL
,
1883 Symbol_table::PREDEFINED
,
1884 this->irelative_rel_
, 0, 0,
1885 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1886 elfcpp::STV_HIDDEN
, 0, false, true);
1887 symtab
->define_in_output_data("__rela_iplt_end", NULL
,
1888 Symbol_table::PREDEFINED
,
1889 this->irelative_rel_
, 0, 0,
1890 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
1891 elfcpp::STV_HIDDEN
, 0, true, true);
1894 return this->irelative_rel_
;
1897 // Return the PLT address to use for a global symbol.
1901 Output_data_plt_x86_64
<size
>::do_address_for_global(const Symbol
* gsym
)
1903 uint64_t offset
= 0;
1904 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1905 && gsym
->can_use_relative_reloc(false))
1906 offset
= (this->count_
+ 1) * this->get_plt_entry_size();
1907 return this->address() + offset
+ gsym
->plt_offset();
1910 // Return the PLT address to use for a local symbol. These are always
1911 // IRELATIVE relocs.
1915 Output_data_plt_x86_64
<size
>::do_address_for_local(const Relobj
* object
,
1918 return (this->address()
1919 + (this->count_
+ 1) * this->get_plt_entry_size()
1920 + object
->local_plt_offset(r_sym
));
1923 // Set the final size.
1926 Output_data_plt_x86_64
<size
>::set_final_data_size()
1928 // Number of regular and IFUNC PLT entries, plus the first entry.
1929 unsigned int count
= this->count_
+ this->irelative_count_
+ 1;
1930 // Count the TLSDESC entry, if present.
1931 if (this->has_tlsdesc_entry())
1933 this->set_data_size(count
* this->get_plt_entry_size());
1936 // The first entry in the PLT for an executable.
1940 Output_data_plt_x86_64_standard
<size
>::first_plt_entry
[plt_entry_size
] =
1942 // From AMD64 ABI Draft 0.98, page 76
1943 0xff, 0x35, // pushq contents of memory address
1944 0, 0, 0, 0, // replaced with address of .got + 8
1945 0xff, 0x25, // jmp indirect
1946 0, 0, 0, 0, // replaced with address of .got + 16
1947 0x90, 0x90, 0x90, 0x90 // noop (x4)
1952 Output_data_plt_x86_64_standard
<size
>::do_fill_first_plt_entry(
1954 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1955 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
1957 memcpy(pov
, first_plt_entry
, plt_entry_size
);
1958 // We do a jmp relative to the PC at the end of this instruction.
1959 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1961 - (plt_address
+ 6)));
1962 elfcpp::Swap
<32, false>::writeval(pov
+ 8,
1964 - (plt_address
+ 12)));
1967 // Subsequent entries in the PLT for an executable.
1971 Output_data_plt_x86_64_standard
<size
>::plt_entry
[plt_entry_size
] =
1973 // From AMD64 ABI Draft 0.98, page 76
1974 0xff, 0x25, // jmpq indirect
1975 0, 0, 0, 0, // replaced with address of symbol in .got
1976 0x68, // pushq immediate
1977 0, 0, 0, 0, // replaced with offset into relocation table
1978 0xe9, // jmpq relative
1979 0, 0, 0, 0 // replaced with offset to start of .plt
1984 Output_data_plt_x86_64_standard
<size
>::do_fill_plt_entry(
1986 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
1987 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
1988 unsigned int got_offset
,
1989 unsigned int plt_offset
,
1990 unsigned int plt_index
)
1992 // Check PC-relative offset overflow in PLT entry.
1993 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
1994 - (plt_address
+ plt_offset
+ 6));
1995 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
1996 gold_error(_("PC-relative offset overflow in PLT entry %d"),
1999 memcpy(pov
, plt_entry
, plt_entry_size
);
2000 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2001 plt_got_pcrel_offset
);
2003 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_index
);
2004 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
2005 - (plt_offset
+ plt_entry_size
));
2010 // The reserved TLSDESC entry in the PLT for an executable.
2014 Output_data_plt_x86_64_standard
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
2016 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2017 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2018 0xff, 0x35, // pushq x(%rip)
2019 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2020 0xff, 0x25, // jmpq *y(%rip)
2021 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2028 Output_data_plt_x86_64_standard
<size
>::do_fill_tlsdesc_entry(
2030 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
2031 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
2032 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
2033 unsigned int tlsdesc_got_offset
,
2034 unsigned int plt_offset
)
2036 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
2037 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2039 - (plt_address
+ plt_offset
2041 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 8,
2043 + tlsdesc_got_offset
2044 - (plt_address
+ plt_offset
2048 // Return the APLT address to use for a global symbol (for -z bndplt).
2051 Output_data_plt_x86_64_bnd::do_address_for_global(const Symbol
* gsym
)
2053 uint64_t offset
= this->aplt_offset_
;
2054 // Convert the PLT offset into an APLT offset.
2055 unsigned int plt_offset
= gsym
->plt_offset();
2056 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2057 && gsym
->can_use_relative_reloc(false))
2058 offset
+= this->regular_count() * aplt_entry_size
;
2060 plt_offset
-= plt_entry_size
;
2061 plt_offset
= plt_offset
/ (plt_entry_size
/ aplt_entry_size
);
2062 return this->address() + offset
+ plt_offset
;
2065 // Return the PLT address to use for a local symbol. These are always
2066 // IRELATIVE relocs.
2069 Output_data_plt_x86_64_bnd::do_address_for_local(const Relobj
* object
,
2072 // Convert the PLT offset into an APLT offset.
2073 unsigned int plt_offset
= ((object
->local_plt_offset(r_sym
) - plt_entry_size
)
2074 / (plt_entry_size
/ aplt_entry_size
));
2075 return (this->address()
2076 + this->aplt_offset_
2077 + this->regular_count() * aplt_entry_size
2081 // Set the final size.
2083 Output_data_plt_x86_64_bnd::set_final_data_size()
2085 // Number of regular and IFUNC PLT entries.
2086 unsigned int count
= this->entry_count();
2087 // Count the first entry and the TLSDESC entry, if present.
2088 unsigned int extra
= this->has_tlsdesc_entry() ? 2 : 1;
2089 unsigned int plt_size
= (count
+ extra
) * plt_entry_size
;
2090 // Offset of the APLT.
2091 this->aplt_offset_
= plt_size
;
2092 // Size of the APLT.
2093 plt_size
+= count
* aplt_entry_size
;
2094 this->set_data_size(plt_size
);
2097 // The first entry in the BND PLT.
2100 Output_data_plt_x86_64_bnd::first_plt_entry
[plt_entry_size
] =
2102 // From AMD64 ABI Draft 0.98, page 76
2103 0xff, 0x35, // pushq contents of memory address
2104 0, 0, 0, 0, // replaced with address of .got + 8
2105 0xf2, 0xff, 0x25, // bnd jmp indirect
2106 0, 0, 0, 0, // replaced with address of .got + 16
2107 0x0f, 0x1f, 0x00 // nop
2111 Output_data_plt_x86_64_bnd::do_fill_first_plt_entry(
2113 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
2114 elfcpp::Elf_types
<64>::Elf_Addr plt_address
)
2116 memcpy(pov
, first_plt_entry
, plt_entry_size
);
2117 // We do a jmp relative to the PC at the end of this instruction.
2118 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2120 - (plt_address
+ 6)));
2121 elfcpp::Swap
<32, false>::writeval(pov
+ 9,
2123 - (plt_address
+ 13)));
2126 // Subsequent entries in the BND PLT.
2129 Output_data_plt_x86_64_bnd::plt_entry
[plt_entry_size
] =
2131 // From AMD64 ABI Draft 0.99.8, page 139
2132 0x68, // pushq immediate
2133 0, 0, 0, 0, // replaced with offset into relocation table
2134 0xf2, 0xe9, // bnd jmpq relative
2135 0, 0, 0, 0, // replaced with offset to start of .plt
2136 0x0f, 0x1f, 0x44, 0, 0 // nop
2139 // Entries in the BND Additional PLT.
2142 Output_data_plt_x86_64_bnd::aplt_entry
[aplt_entry_size
] =
2144 // From AMD64 ABI Draft 0.99.8, page 139
2145 0xf2, 0xff, 0x25, // bnd jmpq indirect
2146 0, 0, 0, 0, // replaced with address of symbol in .got
2151 Output_data_plt_x86_64_bnd::do_fill_plt_entry(
2153 elfcpp::Elf_types
<64>::Elf_Addr
,
2154 elfcpp::Elf_types
<64>::Elf_Addr
,
2156 unsigned int plt_offset
,
2157 unsigned int plt_index
)
2159 memcpy(pov
, plt_entry
, plt_entry_size
);
2160 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 1, plt_index
);
2161 elfcpp::Swap
<32, false>::writeval(pov
+ 7, -(plt_offset
+ 11));
2166 Output_data_plt_x86_64_bnd::fill_aplt_entry(
2168 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
2169 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
2170 unsigned int got_offset
,
2171 unsigned int plt_offset
,
2172 unsigned int plt_index
)
2174 // Check PC-relative offset overflow in PLT entry.
2175 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
2176 - (plt_address
+ plt_offset
+ 7));
2177 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
2178 gold_error(_("PC-relative offset overflow in APLT entry %d"),
2181 memcpy(pov
, aplt_entry
, aplt_entry_size
);
2182 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3, plt_got_pcrel_offset
);
2185 // The reserved TLSDESC entry in the PLT for an executable.
2188 Output_data_plt_x86_64_bnd::tlsdesc_plt_entry
[plt_entry_size
] =
2190 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2191 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2192 0xff, 0x35, // pushq x(%rip)
2193 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2194 0xf2, 0xff, 0x25, // jmpq *y(%rip)
2195 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2196 0x0f, 0x1f, 0 // nop
2200 Output_data_plt_x86_64_bnd::do_fill_tlsdesc_entry(
2202 elfcpp::Elf_types
<64>::Elf_Addr got_address
,
2203 elfcpp::Elf_types
<64>::Elf_Addr plt_address
,
2204 elfcpp::Elf_types
<64>::Elf_Addr got_base
,
2205 unsigned int tlsdesc_got_offset
,
2206 unsigned int plt_offset
)
2208 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
2209 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2211 - (plt_address
+ plt_offset
2213 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
2215 + tlsdesc_got_offset
2216 - (plt_address
+ plt_offset
2220 // Return the APLT address to use for a global symbol (for IBT).
2224 Output_data_plt_x86_64_ibt
<size
>::do_address_for_global(const Symbol
* gsym
)
2226 uint64_t offset
= this->aplt_offset_
;
2227 // Convert the PLT offset into an APLT offset.
2228 unsigned int plt_offset
= gsym
->plt_offset();
2229 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
2230 && gsym
->can_use_relative_reloc(false))
2231 offset
+= this->regular_count() * aplt_entry_size
;
2233 plt_offset
-= plt_entry_size
;
2234 plt_offset
= plt_offset
/ (plt_entry_size
/ aplt_entry_size
);
2235 return this->address() + offset
+ plt_offset
;
2238 // Return the PLT address to use for a local symbol. These are always
2239 // IRELATIVE relocs.
2243 Output_data_plt_x86_64_ibt
<size
>::do_address_for_local(const Relobj
* object
,
2246 // Convert the PLT offset into an APLT offset.
2247 unsigned int plt_offset
= ((object
->local_plt_offset(r_sym
) - plt_entry_size
)
2248 / (plt_entry_size
/ aplt_entry_size
));
2249 return (this->address()
2250 + this->aplt_offset_
2251 + this->regular_count() * aplt_entry_size
2255 // Set the final size.
2259 Output_data_plt_x86_64_ibt
<size
>::set_final_data_size()
2261 // Number of regular and IFUNC PLT entries.
2262 unsigned int count
= this->entry_count();
2263 // Count the first entry and the TLSDESC entry, if present.
2264 unsigned int extra
= this->has_tlsdesc_entry() ? 2 : 1;
2265 unsigned int plt_size
= (count
+ extra
) * plt_entry_size
;
2266 // Offset of the APLT.
2267 this->aplt_offset_
= plt_size
;
2268 // Size of the APLT.
2269 plt_size
+= count
* aplt_entry_size
;
2270 this->set_data_size(plt_size
);
2273 // The first entry in the IBT PLT.
2277 Output_data_plt_x86_64_ibt
<32>::first_plt_entry
[plt_entry_size
] =
2279 // MPX isn't supported for x32, so we don't need the BND prefix.
2280 // From AMD64 ABI Draft 0.98, page 76
2281 0xff, 0x35, // pushq contents of memory address
2282 0, 0, 0, 0, // replaced with address of .got + 8
2283 0xff, 0x25, // jmp indirect
2284 0, 0, 0, 0, // replaced with address of .got + 16
2285 0x90, 0x90, 0x90, 0x90 // noop (x4)
2290 Output_data_plt_x86_64_ibt
<64>::first_plt_entry
[plt_entry_size
] =
2292 // Use the BND prefix so that IBT is compatible with MPX.
2293 0xff, 0x35, // pushq contents of memory address
2294 0, 0, 0, 0, // replaced with address of .got + 8
2295 0xf2, 0xff, 0x25, // bnd jmp indirect
2296 0, 0, 0, 0, // replaced with address of .got + 16
2297 0x0f, 0x1f, 0x00 // nop
2302 Output_data_plt_x86_64_ibt
<size
>::do_fill_first_plt_entry(
2304 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
2305 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
2307 // Offsets to the addresses needing relocation.
2308 const unsigned int roff1
= 2;
2309 const unsigned int roff2
= (size
== 32) ? 8 : 9;
2311 memcpy(pov
, first_plt_entry
, plt_entry_size
);
2312 // We do a jmp relative to the PC at the end of this instruction.
2313 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ roff1
,
2315 - (plt_address
+ roff1
+ 4)));
2316 elfcpp::Swap
<32, false>::writeval(pov
+ roff2
,
2318 - (plt_address
+ roff2
+ 4)));
2321 // Subsequent entries in the IBT PLT.
2325 Output_data_plt_x86_64_ibt
<32>::plt_entry
[plt_entry_size
] =
2327 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2328 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2329 0x68, // pushq immediate
2330 0, 0, 0, 0, // replaced with offset into relocation table
2331 0xe9, // jmpq relative
2332 0, 0, 0, 0, // replaced with offset to start of .plt
2338 Output_data_plt_x86_64_ibt
<64>::plt_entry
[plt_entry_size
] =
2340 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2341 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2342 0x68, // pushq immediate
2343 0, 0, 0, 0, // replaced with offset into relocation table
2344 0xf2, 0xe9, // bnd jmpq relative
2345 0, 0, 0, 0, // replaced with offset to start of .plt
2349 // Entries in the IBT Additional PLT.
2353 Output_data_plt_x86_64_ibt
<32>::aplt_entry
[aplt_entry_size
] =
2355 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2356 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2357 0xff, 0x25, // jmpq indirect
2358 0, 0, 0, 0, // replaced with address of symbol in .got
2359 0x0f, 0x1f, 0x04, 0x00, // nop
2365 Output_data_plt_x86_64_ibt
<64>::aplt_entry
[aplt_entry_size
] =
2367 // From AMD64 ABI Draft 1.0-rc1, Chapter 13.
2368 0xf3, 0x0f, 0x1e, 0xfa, // endbr64
2369 0xf2, 0xff, 0x25, // bnd jmpq indirect
2370 0, 0, 0, 0, // replaced with address of symbol in .got
2371 0x0f, 0x1f, 0x04, 0x00, // nop
2377 Output_data_plt_x86_64_ibt
<size
>::do_fill_plt_entry(
2379 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
2380 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
2382 unsigned int plt_offset
,
2383 unsigned int plt_index
)
2385 // Offsets to the addresses needing relocation.
2386 const unsigned int roff1
= 5;
2387 const unsigned int roff2
= (size
== 32) ? 10 : 11;
2389 memcpy(pov
, plt_entry
, plt_entry_size
);
2390 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ roff1
, plt_index
);
2391 elfcpp::Swap
<32, false>::writeval(pov
+ roff2
, -(plt_offset
+ roff2
+ 4));
2397 Output_data_plt_x86_64_ibt
<size
>::fill_aplt_entry(
2399 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
2400 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
2401 unsigned int got_offset
,
2402 unsigned int plt_offset
,
2403 unsigned int plt_index
)
2405 // Offset to the address needing relocation.
2406 const unsigned int roff
= (size
== 32) ? 6 : 7;
2408 // Check PC-relative offset overflow in PLT entry.
2409 uint64_t plt_got_pcrel_offset
= (got_address
+ got_offset
2410 - (plt_address
+ plt_offset
+ roff
+ 4));
2411 if (Bits
<32>::has_overflow(plt_got_pcrel_offset
))
2412 gold_error(_("PC-relative offset overflow in APLT entry %d"),
2415 memcpy(pov
, aplt_entry
, aplt_entry_size
);
2416 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ roff
, plt_got_pcrel_offset
);
2419 // The reserved TLSDESC entry in the IBT PLT for an executable.
2423 Output_data_plt_x86_64_ibt
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
2425 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
2426 // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
2427 0xff, 0x35, // pushq x(%rip)
2428 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
2429 0xf2, 0xff, 0x25, // jmpq *y(%rip)
2430 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
2431 0x0f, 0x1f, 0 // nop
2436 Output_data_plt_x86_64_ibt
<size
>::do_fill_tlsdesc_entry(
2438 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
2439 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
2440 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
2441 unsigned int tlsdesc_got_offset
,
2442 unsigned int plt_offset
)
2444 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
2445 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
2447 - (plt_address
+ plt_offset
2449 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
2451 + tlsdesc_got_offset
2452 - (plt_address
+ plt_offset
2456 // The .eh_frame unwind information for the PLT.
2460 Output_data_plt_x86_64
<size
>::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
2463 'z', // Augmentation: augmentation size included.
2464 'R', // Augmentation: FDE encoding included.
2465 '\0', // End of augmentation string.
2466 1, // Code alignment factor.
2467 0x78, // Data alignment factor.
2468 16, // Return address column.
2469 1, // Augmentation size.
2470 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
2471 | elfcpp::DW_EH_PE_sdata4
),
2472 elfcpp::DW_CFA_def_cfa
, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8.
2473 elfcpp::DW_CFA_offset
+ 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
2474 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
2480 Output_data_plt_x86_64_standard
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
2482 0, 0, 0, 0, // Replaced with offset to .plt.
2483 0, 0, 0, 0, // Replaced with size of .plt.
2484 0, // Augmentation size.
2485 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
2486 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
2487 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
2488 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
2489 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
2490 11, // Block length.
2491 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
2492 elfcpp::DW_OP_breg16
, 0, // Push %rip.
2493 elfcpp::DW_OP_lit15
, // Push 0xf.
2494 elfcpp::DW_OP_and
, // & (%rip & 0xf).
2495 elfcpp::DW_OP_lit11
, // Push 0xb.
2496 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 0xb)
2497 elfcpp::DW_OP_lit3
, // Push 3.
2498 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 0xb) << 3)
2499 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
2500 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
2506 // The .eh_frame unwind information for the BND PLT.
2508 Output_data_plt_x86_64_bnd::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
2510 0, 0, 0, 0, // Replaced with offset to .plt.
2511 0, 0, 0, 0, // Replaced with size of .plt.
2512 0, // Augmentation size.
2513 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
2514 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
2515 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
2516 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
2517 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
2518 11, // Block length.
2519 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
2520 elfcpp::DW_OP_breg16
, 0, // Push %rip.
2521 elfcpp::DW_OP_lit15
, // Push 0xf.
2522 elfcpp::DW_OP_and
, // & (%rip & 0xf).
2523 elfcpp::DW_OP_lit5
, // Push 5.
2524 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 5)
2525 elfcpp::DW_OP_lit3
, // Push 3.
2526 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 5) << 3)
2527 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=5)<<3)+%rsp+8
2528 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
2534 // The .eh_frame unwind information for the BND PLT.
2537 Output_data_plt_x86_64_ibt
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
2539 0, 0, 0, 0, // Replaced with offset to .plt.
2540 0, 0, 0, 0, // Replaced with size of .plt.
2541 0, // Augmentation size.
2542 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
2543 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
2544 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
2545 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
2546 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
2547 11, // Block length.
2548 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
2549 elfcpp::DW_OP_breg16
, 0, // Push %rip.
2550 elfcpp::DW_OP_lit15
, // Push 0xf.
2551 elfcpp::DW_OP_and
, // & (%rip & 0xf).
2552 elfcpp::DW_OP_lit9
, // Push 9.
2553 elfcpp::DW_OP_ge
, // >= ((%rip & 0xf) >= 9)
2554 elfcpp::DW_OP_lit3
, // Push 3.
2555 elfcpp::DW_OP_shl
, // << (((%rip & 0xf) >= 9) << 3)
2556 elfcpp::DW_OP_plus
, // + ((((%rip&0xf)>=9)<<3)+%rsp+8
2557 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
2563 // Write out the PLT. This uses the hand-coded instructions above,
2564 // and adjusts them as needed. This is specified by the AMD64 ABI.
2568 Output_data_plt_x86_64
<size
>::do_write(Output_file
* of
)
2570 const off_t offset
= this->offset();
2571 const section_size_type oview_size
=
2572 convert_to_section_size_type(this->data_size());
2573 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2575 const off_t got_file_offset
= this->got_plt_
->offset();
2576 gold_assert(parameters
->incremental_update()
2577 || (got_file_offset
+ this->got_plt_
->data_size()
2578 == this->got_irelative_
->offset()));
2579 const section_size_type got_size
=
2580 convert_to_section_size_type(this->got_plt_
->data_size()
2581 + this->got_irelative_
->data_size());
2582 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
2585 unsigned char* pov
= oview
;
2587 // The base address of the .plt section.
2588 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
= this->address();
2589 // The base address of the .got section.
2590 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
= this->got_
->address();
2591 // The base address of the PLT portion of the .got section,
2592 // which is where the GOT pointer will point, and where the
2593 // three reserved GOT entries are located.
2594 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
2595 = this->got_plt_
->address();
2597 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
2598 pov
+= this->get_plt_entry_size();
2600 // The first three entries in the GOT are reserved, and are written
2601 // by Output_data_got_plt_x86_64::do_write.
2602 unsigned char* got_pov
= got_view
+ 24;
2604 unsigned int plt_offset
= this->get_plt_entry_size();
2605 unsigned int got_offset
= 24;
2606 const unsigned int count
= this->count_
+ this->irelative_count_
;
2607 for (unsigned int plt_index
= 0;
2610 pov
+= this->get_plt_entry_size(),
2612 plt_offset
+= this->get_plt_entry_size(),
2615 // Set and adjust the PLT entry itself.
2616 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
2617 got_address
, plt_address
,
2618 got_offset
, plt_offset
,
2621 // Set the entry in the GOT.
2622 elfcpp::Swap
<64, false>::writeval(got_pov
,
2623 plt_address
+ plt_offset
+ lazy_offset
);
2626 if (this->has_tlsdesc_entry())
2628 // Set and adjust the reserved TLSDESC PLT entry.
2629 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
2630 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
2631 tlsdesc_got_offset
, plt_offset
);
2632 pov
+= this->get_plt_entry_size();
2635 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
2636 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
2638 of
->write_output_view(offset
, oview_size
, oview
);
2639 of
->write_output_view(got_file_offset
, got_size
, got_view
);
2642 // Write out the BND PLT.
2645 Output_data_plt_x86_64_bnd::do_write(Output_file
* of
)
2647 const off_t offset
= this->offset();
2648 const section_size_type oview_size
=
2649 convert_to_section_size_type(this->data_size());
2650 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2652 Output_data_got
<64, false>* got
= this->got();
2653 Output_data_got_plt_x86_64
* got_plt
= this->got_plt();
2654 Output_data_space
* got_irelative
= this->got_irelative();
2656 const off_t got_file_offset
= got_plt
->offset();
2657 gold_assert(parameters
->incremental_update()
2658 || (got_file_offset
+ got_plt
->data_size()
2659 == got_irelative
->offset()));
2660 const section_size_type got_size
=
2661 convert_to_section_size_type(got_plt
->data_size()
2662 + got_irelative
->data_size());
2663 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
2666 unsigned char* pov
= oview
;
2668 // The base address of the .plt section.
2669 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
2670 // The base address of the .got section.
2671 elfcpp::Elf_types
<64>::Elf_Addr got_base
= got
->address();
2672 // The base address of the PLT portion of the .got section,
2673 // which is where the GOT pointer will point, and where the
2674 // three reserved GOT entries are located.
2675 elfcpp::Elf_types
<64>::Elf_Addr got_address
= got_plt
->address();
2677 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
2678 pov
+= plt_entry_size
;
2680 // The first three entries in the GOT are reserved, and are written
2681 // by Output_data_got_plt_x86_64::do_write.
2682 unsigned char* got_pov
= got_view
+ 24;
2684 unsigned int plt_offset
= plt_entry_size
;
2685 unsigned int got_offset
= 24;
2686 const unsigned int count
= this->entry_count();
2687 for (unsigned int plt_index
= 0;
2690 pov
+= plt_entry_size
,
2692 plt_offset
+= plt_entry_size
,
2695 // Set and adjust the PLT entry itself.
2696 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
2697 got_address
, plt_address
,
2698 got_offset
, plt_offset
,
2701 // Set the entry in the GOT.
2702 elfcpp::Swap
<64, false>::writeval(got_pov
,
2703 plt_address
+ plt_offset
+ lazy_offset
);
2706 if (this->has_tlsdesc_entry())
2708 // Set and adjust the reserved TLSDESC PLT entry.
2709 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
2710 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
2711 tlsdesc_got_offset
, plt_offset
);
2712 pov
+= this->get_plt_entry_size();
2715 // Write the additional PLT.
2717 for (unsigned int plt_index
= 0;
2720 pov
+= aplt_entry_size
,
2721 plt_offset
+= aplt_entry_size
,
2724 // Set and adjust the APLT entry.
2725 this->fill_aplt_entry(pov
, got_address
, plt_address
, got_offset
,
2726 plt_offset
, plt_index
);
2729 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
2730 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
2732 of
->write_output_view(offset
, oview_size
, oview
);
2733 of
->write_output_view(got_file_offset
, got_size
, got_view
);
2736 // Write out the IBT PLT.
2740 Output_data_plt_x86_64_ibt
<size
>::do_write(Output_file
* of
)
2742 const off_t offset
= this->offset();
2743 const section_size_type oview_size
=
2744 convert_to_section_size_type(this->data_size());
2745 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
2747 Output_data_got
<64, false>* got
= this->got();
2748 Output_data_got_plt_x86_64
* got_plt
= this->got_plt();
2749 Output_data_space
* got_irelative
= this->got_irelative();
2751 const off_t got_file_offset
= got_plt
->offset();
2752 gold_assert(parameters
->incremental_update()
2753 || (got_file_offset
+ got_plt
->data_size()
2754 == got_irelative
->offset()));
2755 const section_size_type got_size
=
2756 convert_to_section_size_type(got_plt
->data_size()
2757 + got_irelative
->data_size());
2758 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
2761 unsigned char* pov
= oview
;
2763 // The base address of the .plt section.
2764 elfcpp::Elf_types
<64>::Elf_Addr plt_address
= this->address();
2765 // The base address of the .got section.
2766 elfcpp::Elf_types
<64>::Elf_Addr got_base
= got
->address();
2767 // The base address of the PLT portion of the .got section,
2768 // which is where the GOT pointer will point, and where the
2769 // three reserved GOT entries are located.
2770 elfcpp::Elf_types
<64>::Elf_Addr got_address
= got_plt
->address();
2772 this->fill_first_plt_entry(pov
, got_address
, plt_address
);
2773 pov
+= plt_entry_size
;
2775 // The first three entries in the GOT are reserved, and are written
2776 // by Output_data_got_plt_x86_64::do_write.
2777 unsigned char* got_pov
= got_view
+ 24;
2779 unsigned int plt_offset
= plt_entry_size
;
2780 unsigned int got_offset
= 24;
2781 const unsigned int count
= this->entry_count();
2782 for (unsigned int plt_index
= 0;
2785 pov
+= plt_entry_size
,
2787 plt_offset
+= plt_entry_size
,
2790 // Set and adjust the PLT entry itself.
2791 unsigned int lazy_offset
= this->fill_plt_entry(pov
,
2792 got_address
, plt_address
,
2793 got_offset
, plt_offset
,
2796 // Set the entry in the GOT.
2797 elfcpp::Swap
<64, false>::writeval(got_pov
,
2798 plt_address
+ plt_offset
+ lazy_offset
);
2801 if (this->has_tlsdesc_entry())
2803 // Set and adjust the reserved TLSDESC PLT entry.
2804 unsigned int tlsdesc_got_offset
= this->get_tlsdesc_got_offset();
2805 this->fill_tlsdesc_entry(pov
, got_address
, plt_address
, got_base
,
2806 tlsdesc_got_offset
, plt_offset
);
2807 pov
+= this->get_plt_entry_size();
2810 // Write the additional PLT.
2812 for (unsigned int plt_index
= 0;
2815 pov
+= aplt_entry_size
,
2816 plt_offset
+= aplt_entry_size
,
2819 // Set and adjust the APLT entry.
2820 this->fill_aplt_entry(pov
, got_address
, plt_address
, got_offset
,
2821 plt_offset
, plt_index
);
2824 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
2825 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
2827 of
->write_output_view(offset
, oview_size
, oview
);
2828 of
->write_output_view(got_file_offset
, got_size
, got_view
);
2831 // Create the PLT section.
2835 Target_x86_64
<size
>::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
2837 if (this->plt_
== NULL
)
2839 // Create the GOT sections first.
2840 this->got_section(symtab
, layout
);
2842 this->plt_
= this->make_data_plt(layout
, this->got_
, this->got_plt_
,
2843 this->got_irelative_
);
2845 // Add unwind information if requested.
2846 if (parameters
->options().ld_generated_unwind_info())
2847 this->plt_
->add_eh_frame(layout
);
2849 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
2851 | elfcpp::SHF_EXECINSTR
),
2852 this->plt_
, ORDER_PLT
, false);
2854 // Make the sh_info field of .rela.plt point to .plt.
2855 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
2856 rela_plt_os
->set_info_section(this->plt_
->output_section());
2861 Output_data_plt_x86_64
<32>*
2862 Target_x86_64
<32>::do_make_data_plt(Layout
* layout
,
2863 Output_data_got
<64, false>* got
,
2864 Output_data_got_plt_x86_64
* got_plt
,
2865 Output_data_space
* got_irelative
)
2867 if (this->feature_1_
& elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT
)
2868 return new Output_data_plt_x86_64_ibt
<32>(layout
, got
, got_plt
,
2870 return new Output_data_plt_x86_64_standard
<32>(layout
, got
, got_plt
,
2875 Output_data_plt_x86_64
<64>*
2876 Target_x86_64
<64>::do_make_data_plt(Layout
* layout
,
2877 Output_data_got
<64, false>* got
,
2878 Output_data_got_plt_x86_64
* got_plt
,
2879 Output_data_space
* got_irelative
)
2881 if (this->feature_1_
& elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT
)
2882 return new Output_data_plt_x86_64_ibt
<64>(layout
, got
, got_plt
,
2884 else if (parameters
->options().bndplt())
2885 return new Output_data_plt_x86_64_bnd(layout
, got
, got_plt
,
2888 return new Output_data_plt_x86_64_standard
<64>(layout
, got
, got_plt
,
2893 Output_data_plt_x86_64
<32>*
2894 Target_x86_64
<32>::do_make_data_plt(Layout
* layout
,
2895 Output_data_got
<64, false>* got
,
2896 Output_data_got_plt_x86_64
* got_plt
,
2897 Output_data_space
* got_irelative
,
2898 unsigned int plt_count
)
2900 if (this->feature_1_
& elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT
)
2901 return new Output_data_plt_x86_64_ibt
<32>(layout
, got
, got_plt
,
2902 got_irelative
, plt_count
);
2903 return new Output_data_plt_x86_64_standard
<32>(layout
, got
, got_plt
,
2904 got_irelative
, plt_count
);
2908 Output_data_plt_x86_64
<64>*
2909 Target_x86_64
<64>::do_make_data_plt(Layout
* layout
,
2910 Output_data_got
<64, false>* got
,
2911 Output_data_got_plt_x86_64
* got_plt
,
2912 Output_data_space
* got_irelative
,
2913 unsigned int plt_count
)
2915 if (this->feature_1_
& elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT
)
2916 return new Output_data_plt_x86_64_ibt
<64>(layout
, got
, got_plt
,
2917 got_irelative
, plt_count
);
2918 else if (parameters
->options().bndplt())
2919 return new Output_data_plt_x86_64_bnd(layout
, got
, got_plt
,
2920 got_irelative
, plt_count
);
2922 return new Output_data_plt_x86_64_standard
<64>(layout
, got
, got_plt
,
2927 // Return the section for TLSDESC relocations.
2930 typename Target_x86_64
<size
>::Reloc_section
*
2931 Target_x86_64
<size
>::rela_tlsdesc_section(Layout
* layout
) const
2933 return this->plt_section()->rela_tlsdesc(layout
);
2936 // Create a PLT entry for a global symbol.
2940 Target_x86_64
<size
>::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
2943 if (gsym
->has_plt_offset())
2946 if (this->plt_
== NULL
)
2947 this->make_plt_section(symtab
, layout
);
2949 this->plt_
->add_entry(symtab
, layout
, gsym
);
2952 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
2956 Target_x86_64
<size
>::make_local_ifunc_plt_entry(
2957 Symbol_table
* symtab
, Layout
* layout
,
2958 Sized_relobj_file
<size
, false>* relobj
,
2959 unsigned int local_sym_index
)
2961 if (relobj
->local_has_plt_offset(local_sym_index
))
2963 if (this->plt_
== NULL
)
2964 this->make_plt_section(symtab
, layout
);
2965 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
2968 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
2971 // Return the number of entries in the PLT.
2975 Target_x86_64
<size
>::plt_entry_count() const
2977 if (this->plt_
== NULL
)
2979 return this->plt_
->entry_count();
2982 // Return the offset of the first non-reserved PLT entry.
2986 Target_x86_64
<size
>::first_plt_entry_offset() const
2988 if (this->plt_
== NULL
)
2990 return this->plt_
->first_plt_entry_offset();
2993 // Return the size of each PLT entry.
2997 Target_x86_64
<size
>::plt_entry_size() const
2999 if (this->plt_
== NULL
)
3001 return this->plt_
->get_plt_entry_size();
3004 // Create the GOT and PLT sections for an incremental update.
3007 Output_data_got_base
*
3008 Target_x86_64
<size
>::init_got_plt_for_update(Symbol_table
* symtab
,
3010 unsigned int got_count
,
3011 unsigned int plt_count
)
3013 gold_assert(this->got_
== NULL
);
3015 this->got_
= new Output_data_got
<64, false>(got_count
* 8);
3016 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
3018 | elfcpp::SHF_WRITE
),
3019 this->got_
, ORDER_RELRO_LAST
,
3022 // Add the three reserved entries.
3023 this->got_plt_
= new Output_data_got_plt_x86_64(layout
, (plt_count
+ 3) * 8);
3024 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3026 | elfcpp::SHF_WRITE
),
3027 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
3030 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
3031 this->global_offset_table_
=
3032 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
3033 Symbol_table::PREDEFINED
,
3035 0, 0, elfcpp::STT_OBJECT
,
3037 elfcpp::STV_HIDDEN
, 0,
3040 // If there are any TLSDESC relocations, they get GOT entries in
3041 // .got.plt after the jump slot entries.
3042 // FIXME: Get the count for TLSDESC entries.
3043 this->got_tlsdesc_
= new Output_data_got
<64, false>(0);
3044 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3045 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3047 ORDER_NON_RELRO_FIRST
, false);
3049 // If there are any IRELATIVE relocations, they get GOT entries in
3050 // .got.plt after the jump slot and TLSDESC entries.
3051 this->got_irelative_
= new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
3052 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
3053 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
3054 this->got_irelative_
,
3055 ORDER_NON_RELRO_FIRST
, false);
3057 // Create the PLT section.
3058 this->plt_
= this->make_data_plt(layout
, this->got_
,
3060 this->got_irelative_
,
3063 // Add unwind information if requested.
3064 if (parameters
->options().ld_generated_unwind_info())
3065 this->plt_
->add_eh_frame(layout
);
3067 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
3068 elfcpp::SHF_ALLOC
| elfcpp::SHF_EXECINSTR
,
3069 this->plt_
, ORDER_PLT
, false);
3071 // Make the sh_info field of .rela.plt point to .plt.
3072 Output_section
* rela_plt_os
= this->plt_
->rela_plt()->output_section();
3073 rela_plt_os
->set_info_section(this->plt_
->output_section());
3075 // Create the rela_dyn section.
3076 this->rela_dyn_section(layout
);
3081 // Reserve a GOT entry for a local symbol, and regenerate any
3082 // necessary dynamic relocations.
3086 Target_x86_64
<size
>::reserve_local_got_entry(
3087 unsigned int got_index
,
3088 Sized_relobj
<size
, false>* obj
,
3090 unsigned int got_type
)
3092 unsigned int got_offset
= got_index
* 8;
3093 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
3095 this->got_
->reserve_local(got_index
, obj
, r_sym
, got_type
);
3098 case GOT_TYPE_STANDARD
:
3099 if (parameters
->options().output_is_position_independent())
3100 rela_dyn
->add_local_relative(obj
, r_sym
, elfcpp::R_X86_64_RELATIVE
,
3101 this->got_
, got_offset
, 0, false);
3103 case GOT_TYPE_TLS_OFFSET
:
3104 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_TPOFF64
,
3105 this->got_
, got_offset
, 0);
3107 case GOT_TYPE_TLS_PAIR
:
3108 this->got_
->reserve_slot(got_index
+ 1);
3109 rela_dyn
->add_local(obj
, r_sym
, elfcpp::R_X86_64_DTPMOD64
,
3110 this->got_
, got_offset
, 0);
3112 case GOT_TYPE_TLS_DESC
:
3113 gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
3114 // this->got_->reserve_slot(got_index + 1);
3115 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
3116 // this->got_, got_offset, 0);
3123 // Reserve a GOT entry for a global symbol, and regenerate any
3124 // necessary dynamic relocations.
3128 Target_x86_64
<size
>::reserve_global_got_entry(unsigned int got_index
,
3130 unsigned int got_type
)
3132 unsigned int got_offset
= got_index
* 8;
3133 Reloc_section
* rela_dyn
= this->rela_dyn_section(NULL
);
3135 this->got_
->reserve_global(got_index
, gsym
, got_type
);
3138 case GOT_TYPE_STANDARD
:
3139 if (!gsym
->final_value_is_known())
3141 if (gsym
->is_from_dynobj()
3142 || gsym
->is_undefined()
3143 || gsym
->is_preemptible()
3144 || gsym
->type() == elfcpp::STT_GNU_IFUNC
)
3145 rela_dyn
->add_global(gsym
, elfcpp::R_X86_64_GLOB_DAT
,
3146 this->got_
, got_offset
, 0);
3148 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
3149 this->got_
, got_offset
, 0, false);
3152 case GOT_TYPE_TLS_OFFSET
:
3153 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TPOFF64
,
3154 this->got_
, got_offset
, 0, false);
3156 case GOT_TYPE_TLS_PAIR
:
3157 this->got_
->reserve_slot(got_index
+ 1);
3158 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPMOD64
,
3159 this->got_
, got_offset
, 0, false);
3160 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_DTPOFF64
,
3161 this->got_
, got_offset
+ 8, 0, false);
3163 case GOT_TYPE_TLS_DESC
:
3164 this->got_
->reserve_slot(got_index
+ 1);
3165 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_TLSDESC
,
3166 this->got_
, got_offset
, 0, false);
3173 // Register an existing PLT entry for a global symbol.
3177 Target_x86_64
<size
>::register_global_plt_entry(Symbol_table
* symtab
,
3179 unsigned int plt_index
,
3182 gold_assert(this->plt_
!= NULL
);
3183 gold_assert(!gsym
->has_plt_offset());
3185 this->plt_
->reserve_slot(plt_index
);
3187 gsym
->set_plt_offset((plt_index
+ 1) * this->plt_entry_size());
3189 unsigned int got_offset
= (plt_index
+ 3) * 8;
3190 this->plt_
->add_relocation(symtab
, layout
, gsym
, got_offset
);
3193 // Force a COPY relocation for a given symbol.
3197 Target_x86_64
<size
>::emit_copy_reloc(
3198 Symbol_table
* symtab
, Symbol
* sym
, Output_section
* os
, off_t offset
)
3200 this->copy_relocs_
.emit_copy_reloc(symtab
,
3201 symtab
->get_sized_symbol
<size
>(sym
),
3204 this->rela_dyn_section(NULL
));
3207 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
3211 Target_x86_64
<size
>::define_tls_base_symbol(Symbol_table
* symtab
,
3214 if (this->tls_base_symbol_defined_
)
3217 Output_segment
* tls_segment
= layout
->tls_segment();
3218 if (tls_segment
!= NULL
)
3220 bool is_exec
= parameters
->options().output_is_executable();
3221 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
3222 Symbol_table::PREDEFINED
,
3226 elfcpp::STV_HIDDEN
, 0,
3228 ? Symbol::SEGMENT_END
3229 : Symbol::SEGMENT_START
),
3232 this->tls_base_symbol_defined_
= true;
3235 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
3239 Target_x86_64
<size
>::reserve_tlsdesc_entries(Symbol_table
* symtab
,
3242 if (this->plt_
== NULL
)
3243 this->make_plt_section(symtab
, layout
);
3245 if (!this->plt_
->has_tlsdesc_entry())
3247 // Allocate the TLSDESC_GOT entry.
3248 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
3249 unsigned int got_offset
= got
->add_constant(0);
3251 // Allocate the TLSDESC_PLT entry.
3252 this->plt_
->reserve_tlsdesc_entry(got_offset
);
3256 // Create a GOT entry for the TLS module index.
3260 Target_x86_64
<size
>::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
3261 Sized_relobj_file
<size
, false>* object
)
3263 if (this->got_mod_index_offset_
== -1U)
3265 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
3266 Reloc_section
* rela_dyn
= this->rela_dyn_section(layout
);
3267 Output_data_got
<64, false>* got
= this->got_section(symtab
, layout
);
3268 unsigned int got_offset
= got
->add_constant(0);
3269 rela_dyn
->add_local(object
, 0, elfcpp::R_X86_64_DTPMOD64
, got
,
3271 got
->add_constant(0);
3272 this->got_mod_index_offset_
= got_offset
;
3274 return this->got_mod_index_offset_
;
3277 // Optimize the TLS relocation type based on what we know about the
3278 // symbol. IS_FINAL is true if the final address of this symbol is
3279 // known at link time.
3282 tls::Tls_optimization
3283 Target_x86_64
<size
>::optimize_tls_reloc(bool is_final
, int r_type
)
3285 // If we are generating a shared library, then we can't do anything
3287 if (parameters
->options().shared())
3288 return tls::TLSOPT_NONE
;
3292 case elfcpp::R_X86_64_TLSGD
:
3293 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3294 case elfcpp::R_X86_64_TLSDESC_CALL
:
3295 // These are General-Dynamic which permits fully general TLS
3296 // access. Since we know that we are generating an executable,
3297 // we can convert this to Initial-Exec. If we also know that
3298 // this is a local symbol, we can further switch to Local-Exec.
3300 return tls::TLSOPT_TO_LE
;
3301 return tls::TLSOPT_TO_IE
;
3303 case elfcpp::R_X86_64_TLSLD
:
3304 // This is Local-Dynamic, which refers to a local symbol in the
3305 // dynamic TLS block. Since we know that we generating an
3306 // executable, we can switch to Local-Exec.
3307 return tls::TLSOPT_TO_LE
;
3309 case elfcpp::R_X86_64_DTPOFF32
:
3310 case elfcpp::R_X86_64_DTPOFF64
:
3311 // Another Local-Dynamic reloc.
3312 return tls::TLSOPT_TO_LE
;
3314 case elfcpp::R_X86_64_GOTTPOFF
:
3315 // These are Initial-Exec relocs which get the thread offset
3316 // from the GOT. If we know that we are linking against the
3317 // local symbol, we can switch to Local-Exec, which links the
3318 // thread offset into the instruction.
3320 return tls::TLSOPT_TO_LE
;
3321 return tls::TLSOPT_NONE
;
3323 case elfcpp::R_X86_64_TPOFF32
:
3324 // When we already have Local-Exec, there is nothing further we
3326 return tls::TLSOPT_NONE
;
3333 // Get the Reference_flags for a particular relocation.
3337 Target_x86_64
<size
>::Scan::get_reference_flags(unsigned int r_type
)
3341 case elfcpp::R_X86_64_NONE
:
3342 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3343 case elfcpp::R_X86_64_GNU_VTENTRY
:
3344 case elfcpp::R_X86_64_GOTPC32
:
3345 case elfcpp::R_X86_64_GOTPC64
:
3346 // No symbol reference.
3349 case elfcpp::R_X86_64_64
:
3350 case elfcpp::R_X86_64_32
:
3351 case elfcpp::R_X86_64_32S
:
3352 case elfcpp::R_X86_64_16
:
3353 case elfcpp::R_X86_64_8
:
3354 return Symbol::ABSOLUTE_REF
;
3356 case elfcpp::R_X86_64_PC64
:
3357 case elfcpp::R_X86_64_PC32
:
3358 case elfcpp::R_X86_64_PC32_BND
:
3359 case elfcpp::R_X86_64_PC16
:
3360 case elfcpp::R_X86_64_PC8
:
3361 case elfcpp::R_X86_64_GOTOFF64
:
3362 return Symbol::RELATIVE_REF
;
3364 case elfcpp::R_X86_64_PLT32
:
3365 case elfcpp::R_X86_64_PLT32_BND
:
3366 case elfcpp::R_X86_64_PLTOFF64
:
3367 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
3369 case elfcpp::R_X86_64_GOT64
:
3370 case elfcpp::R_X86_64_GOT32
:
3371 case elfcpp::R_X86_64_GOTPCREL64
:
3372 case elfcpp::R_X86_64_GOTPCREL
:
3373 case elfcpp::R_X86_64_GOTPCRELX
:
3374 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3375 case elfcpp::R_X86_64_GOTPLT64
:
3377 return Symbol::ABSOLUTE_REF
;
3379 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3380 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3381 case elfcpp::R_X86_64_TLSDESC_CALL
:
3382 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3383 case elfcpp::R_X86_64_DTPOFF32
:
3384 case elfcpp::R_X86_64_DTPOFF64
:
3385 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3386 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3387 return Symbol::TLS_REF
;
3389 case elfcpp::R_X86_64_COPY
:
3390 case elfcpp::R_X86_64_GLOB_DAT
:
3391 case elfcpp::R_X86_64_JUMP_SLOT
:
3392 case elfcpp::R_X86_64_RELATIVE
:
3393 case elfcpp::R_X86_64_IRELATIVE
:
3394 case elfcpp::R_X86_64_TPOFF64
:
3395 case elfcpp::R_X86_64_DTPMOD64
:
3396 case elfcpp::R_X86_64_TLSDESC
:
3397 case elfcpp::R_X86_64_SIZE32
:
3398 case elfcpp::R_X86_64_SIZE64
:
3400 // Not expected. We will give an error later.
3405 // Report an unsupported relocation against a local symbol.
3409 Target_x86_64
<size
>::Scan::unsupported_reloc_local(
3410 Sized_relobj_file
<size
, false>* object
,
3411 unsigned int r_type
)
3413 gold_error(_("%s: unsupported reloc %u against local symbol"),
3414 object
->name().c_str(), r_type
);
3417 // We are about to emit a dynamic relocation of type R_TYPE. If the
3418 // dynamic linker does not support it, issue an error. The GNU linker
3419 // only issues a non-PIC error for an allocated read-only section.
3420 // Here we know the section is allocated, but we don't know that it is
3421 // read-only. But we check for all the relocation types which the
3422 // glibc dynamic linker supports, so it seems appropriate to issue an
3423 // error even if the section is not read-only. If GSYM is not NULL,
3424 // it is the symbol the relocation is against; if it is NULL, the
3425 // relocation is against a local symbol.
3429 Target_x86_64
<size
>::Scan::check_non_pic(Relobj
* object
, unsigned int r_type
,
3434 // These are the relocation types supported by glibc for x86_64
3435 // which should always work.
3436 case elfcpp::R_X86_64_RELATIVE
:
3437 case elfcpp::R_X86_64_IRELATIVE
:
3438 case elfcpp::R_X86_64_GLOB_DAT
:
3439 case elfcpp::R_X86_64_JUMP_SLOT
:
3440 case elfcpp::R_X86_64_DTPMOD64
:
3441 case elfcpp::R_X86_64_DTPOFF64
:
3442 case elfcpp::R_X86_64_TPOFF64
:
3443 case elfcpp::R_X86_64_64
:
3444 case elfcpp::R_X86_64_COPY
:
3447 // glibc supports these reloc types, but they can overflow.
3448 case elfcpp::R_X86_64_PC32
:
3449 case elfcpp::R_X86_64_PC32_BND
:
3450 // A PC relative reference is OK against a local symbol or if
3451 // the symbol is defined locally.
3453 || (!gsym
->is_from_dynobj()
3454 && !gsym
->is_undefined()
3455 && !gsym
->is_preemptible()))
3458 case elfcpp::R_X86_64_32
:
3459 // R_X86_64_32 is OK for x32.
3460 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
3462 if (this->issued_non_pic_error_
)
3464 gold_assert(parameters
->options().output_is_position_independent());
3466 object
->error(_("requires dynamic R_X86_64_32 reloc which may "
3467 "overflow at runtime; recompile with -fPIC"));
3473 case elfcpp::R_X86_64_32
:
3474 r_name
= "R_X86_64_32";
3476 case elfcpp::R_X86_64_PC32
:
3477 r_name
= "R_X86_64_PC32";
3479 case elfcpp::R_X86_64_PC32_BND
:
3480 r_name
= "R_X86_64_PC32_BND";
3486 object
->error(_("requires dynamic %s reloc against '%s' "
3487 "which may overflow at runtime; recompile "
3489 r_name
, gsym
->name());
3491 this->issued_non_pic_error_
= true;
3495 // This prevents us from issuing more than one error per reloc
3496 // section. But we can still wind up issuing more than one
3497 // error per object file.
3498 if (this->issued_non_pic_error_
)
3500 gold_assert(parameters
->options().output_is_position_independent());
3501 object
->error(_("requires unsupported dynamic reloc %u; "
3502 "recompile with -fPIC"),
3504 this->issued_non_pic_error_
= true;
3507 case elfcpp::R_X86_64_NONE
:
3512 // Return whether we need to make a PLT entry for a relocation of the
3513 // given type against a STT_GNU_IFUNC symbol.
3517 Target_x86_64
<size
>::Scan::reloc_needs_plt_for_ifunc(
3518 Sized_relobj_file
<size
, false>* object
,
3519 unsigned int r_type
)
3521 int flags
= Scan::get_reference_flags(r_type
);
3522 if (flags
& Symbol::TLS_REF
)
3523 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
3524 object
->name().c_str(), r_type
);
3528 // Scan a relocation for a local symbol.
3532 Target_x86_64
<size
>::Scan::local(Symbol_table
* symtab
,
3534 Target_x86_64
<size
>* target
,
3535 Sized_relobj_file
<size
, false>* object
,
3536 unsigned int data_shndx
,
3537 Output_section
* output_section
,
3538 const elfcpp::Rela
<size
, false>& reloc
,
3539 unsigned int r_type
,
3540 const elfcpp::Sym
<size
, false>& lsym
,
3546 // A local STT_GNU_IFUNC symbol may require a PLT entry.
3547 bool is_ifunc
= lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
;
3548 if (is_ifunc
&& this->reloc_needs_plt_for_ifunc(object
, r_type
))
3550 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3551 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
3556 case elfcpp::R_X86_64_NONE
:
3557 case elfcpp::R_X86_64_GNU_VTINHERIT
:
3558 case elfcpp::R_X86_64_GNU_VTENTRY
:
3561 case elfcpp::R_X86_64_64
:
3562 // If building a shared library (or a position-independent
3563 // executable), we need to create a dynamic relocation for this
3564 // location. The relocation applied at link time will apply the
3565 // link-time value, so we flag the location with an
3566 // R_X86_64_RELATIVE relocation so the dynamic loader can
3567 // relocate it easily.
3568 if (parameters
->options().output_is_position_independent())
3570 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3571 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3572 rela_dyn
->add_local_relative(object
, r_sym
,
3574 ? elfcpp::R_X86_64_RELATIVE64
3575 : elfcpp::R_X86_64_RELATIVE
),
3576 output_section
, data_shndx
,
3577 reloc
.get_r_offset(),
3578 reloc
.get_r_addend(), is_ifunc
);
3582 case elfcpp::R_X86_64_32
:
3583 case elfcpp::R_X86_64_32S
:
3584 case elfcpp::R_X86_64_16
:
3585 case elfcpp::R_X86_64_8
:
3586 // If building a shared library (or a position-independent
3587 // executable), we need to create a dynamic relocation for this
3588 // location. We can't use an R_X86_64_RELATIVE relocation
3589 // because that is always a 64-bit relocation.
3590 if (parameters
->options().output_is_position_independent())
3592 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
3593 if (size
== 32 && r_type
== elfcpp::R_X86_64_32
)
3595 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3596 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3597 rela_dyn
->add_local_relative(object
, r_sym
,
3598 elfcpp::R_X86_64_RELATIVE
,
3599 output_section
, data_shndx
,
3600 reloc
.get_r_offset(),
3601 reloc
.get_r_addend(), is_ifunc
);
3605 this->check_non_pic(object
, r_type
, NULL
);
3607 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3608 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3609 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
3610 rela_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
3611 data_shndx
, reloc
.get_r_offset(),
3612 reloc
.get_r_addend());
3615 gold_assert(lsym
.get_st_value() == 0);
3616 unsigned int shndx
= lsym
.get_st_shndx();
3618 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
3621 object
->error(_("section symbol %u has bad shndx %u"),
3624 rela_dyn
->add_local_section(object
, shndx
,
3625 r_type
, output_section
,
3626 data_shndx
, reloc
.get_r_offset(),
3627 reloc
.get_r_addend());
3632 case elfcpp::R_X86_64_PC64
:
3633 case elfcpp::R_X86_64_PC32
:
3634 case elfcpp::R_X86_64_PC32_BND
:
3635 case elfcpp::R_X86_64_PC16
:
3636 case elfcpp::R_X86_64_PC8
:
3639 case elfcpp::R_X86_64_PLT32
:
3640 case elfcpp::R_X86_64_PLT32_BND
:
3641 // Since we know this is a local symbol, we can handle this as a
3645 case elfcpp::R_X86_64_GOTPC32
:
3646 case elfcpp::R_X86_64_GOTOFF64
:
3647 case elfcpp::R_X86_64_GOTPC64
:
3648 case elfcpp::R_X86_64_PLTOFF64
:
3649 // We need a GOT section.
3650 target
->got_section(symtab
, layout
);
3651 // For PLTOFF64, we'd normally want a PLT section, but since we
3652 // know this is a local symbol, no PLT is needed.
3655 case elfcpp::R_X86_64_GOT64
:
3656 case elfcpp::R_X86_64_GOT32
:
3657 case elfcpp::R_X86_64_GOTPCREL64
:
3658 case elfcpp::R_X86_64_GOTPCREL
:
3659 case elfcpp::R_X86_64_GOTPCRELX
:
3660 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3661 case elfcpp::R_X86_64_GOTPLT64
:
3663 // The symbol requires a GOT section.
3664 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
3666 // If the relocation symbol isn't IFUNC,
3667 // and is local, then we will convert
3668 // mov foo@GOTPCREL(%rip), %reg
3669 // to lea foo(%rip), %reg.
3670 // in Relocate::relocate.
3671 if (!parameters
->incremental()
3672 && (r_type
== elfcpp::R_X86_64_GOTPCREL
3673 || r_type
== elfcpp::R_X86_64_GOTPCRELX
3674 || r_type
== elfcpp::R_X86_64_REX_GOTPCRELX
)
3675 && reloc
.get_r_offset() >= 2
3678 section_size_type stype
;
3679 const unsigned char* view
= object
->section_contents(data_shndx
,
3681 if (view
[reloc
.get_r_offset() - 2] == 0x8b)
3685 // The symbol requires a GOT entry.
3686 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3688 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
3689 // lets function pointers compare correctly with shared
3690 // libraries. Otherwise we would need an IRELATIVE reloc.
3693 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
3695 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
3698 // If we are generating a shared object, we need to add a
3699 // dynamic relocation for this symbol's GOT entry.
3700 if (parameters
->options().output_is_position_independent())
3702 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
3703 // R_X86_64_RELATIVE assumes a 64-bit relocation.
3704 if (r_type
!= elfcpp::R_X86_64_GOT32
)
3706 unsigned int got_offset
=
3707 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
3708 rela_dyn
->add_local_relative(object
, r_sym
,
3709 elfcpp::R_X86_64_RELATIVE
,
3710 got
, got_offset
, 0, is_ifunc
);
3714 this->check_non_pic(object
, r_type
, NULL
);
3716 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
3717 rela_dyn
->add_local(
3718 object
, r_sym
, r_type
, got
,
3719 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
), 0);
3723 // For GOTPLT64, we'd normally want a PLT section, but since
3724 // we know this is a local symbol, no PLT is needed.
3728 case elfcpp::R_X86_64_COPY
:
3729 case elfcpp::R_X86_64_GLOB_DAT
:
3730 case elfcpp::R_X86_64_JUMP_SLOT
:
3731 case elfcpp::R_X86_64_RELATIVE
:
3732 case elfcpp::R_X86_64_IRELATIVE
:
3733 // These are outstanding tls relocs, which are unexpected when linking
3734 case elfcpp::R_X86_64_TPOFF64
:
3735 case elfcpp::R_X86_64_DTPMOD64
:
3736 case elfcpp::R_X86_64_TLSDESC
:
3737 gold_error(_("%s: unexpected reloc %u in object file"),
3738 object
->name().c_str(), r_type
);
3741 // These are initial tls relocs, which are expected when linking
3742 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
3743 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
3744 case elfcpp::R_X86_64_TLSDESC_CALL
:
3745 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3746 case elfcpp::R_X86_64_DTPOFF32
:
3747 case elfcpp::R_X86_64_DTPOFF64
:
3748 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3749 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3751 bool output_is_shared
= parameters
->options().shared();
3752 const tls::Tls_optimization optimized_type
3753 = Target_x86_64
<size
>::optimize_tls_reloc(!output_is_shared
,
3757 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
3758 if (optimized_type
== tls::TLSOPT_NONE
)
3760 // Create a pair of GOT entries for the module index and
3761 // dtv-relative offset.
3762 Output_data_got
<64, false>* got
3763 = target
->got_section(symtab
, layout
);
3764 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3765 unsigned int shndx
= lsym
.get_st_shndx();
3767 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
3769 object
->error(_("local symbol %u has bad shndx %u"),
3772 got
->add_local_pair_with_rel(object
, r_sym
,
3775 target
->rela_dyn_section(layout
),
3776 elfcpp::R_X86_64_DTPMOD64
);
3778 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3779 unsupported_reloc_local(object
, r_type
);
3782 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
3783 target
->define_tls_base_symbol(symtab
, layout
);
3784 if (optimized_type
== tls::TLSOPT_NONE
)
3786 // Create reserved PLT and GOT entries for the resolver.
3787 target
->reserve_tlsdesc_entries(symtab
, layout
);
3789 // Generate a double GOT entry with an
3790 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc
3791 // is resolved lazily, so the GOT entry needs to be in
3792 // an area in .got.plt, not .got. Call got_section to
3793 // make sure the section has been created.
3794 target
->got_section(symtab
, layout
);
3795 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
3796 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3797 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
3799 unsigned int got_offset
= got
->add_constant(0);
3800 got
->add_constant(0);
3801 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
3803 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
3804 // We store the arguments we need in a vector, and
3805 // use the index into the vector as the parameter
3806 // to pass to the target specific routines.
3807 uintptr_t intarg
= target
->add_tlsdesc_info(object
, r_sym
);
3808 void* arg
= reinterpret_cast<void*>(intarg
);
3809 rt
->add_target_specific(elfcpp::R_X86_64_TLSDESC
, arg
,
3810 got
, got_offset
, 0);
3813 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3814 unsupported_reloc_local(object
, r_type
);
3817 case elfcpp::R_X86_64_TLSDESC_CALL
:
3820 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
3821 if (optimized_type
== tls::TLSOPT_NONE
)
3823 // Create a GOT entry for the module index.
3824 target
->got_mod_index_entry(symtab
, layout
, object
);
3826 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3827 unsupported_reloc_local(object
, r_type
);
3830 case elfcpp::R_X86_64_DTPOFF32
:
3831 case elfcpp::R_X86_64_DTPOFF64
:
3834 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
3835 layout
->set_has_static_tls();
3836 if (optimized_type
== tls::TLSOPT_NONE
)
3838 // Create a GOT entry for the tp-relative offset.
3839 Output_data_got
<64, false>* got
3840 = target
->got_section(symtab
, layout
);
3841 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(reloc
.get_r_info());
3842 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
3843 target
->rela_dyn_section(layout
),
3844 elfcpp::R_X86_64_TPOFF64
);
3846 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
3847 unsupported_reloc_local(object
, r_type
);
3850 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
3851 layout
->set_has_static_tls();
3852 if (output_is_shared
)
3853 unsupported_reloc_local(object
, r_type
);
3862 case elfcpp::R_X86_64_SIZE32
:
3863 case elfcpp::R_X86_64_SIZE64
:
3865 gold_error(_("%s: unsupported reloc %u against local symbol"),
3866 object
->name().c_str(), r_type
);
3872 // Report an unsupported relocation against a global symbol.
3876 Target_x86_64
<size
>::Scan::unsupported_reloc_global(
3877 Sized_relobj_file
<size
, false>* object
,
3878 unsigned int r_type
,
3881 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3882 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
3885 // Returns true if this relocation type could be that of a function pointer.
3888 Target_x86_64
<size
>::Scan::possible_function_pointer_reloc(
3889 Sized_relobj_file
<size
, false>* src_obj
,
3890 unsigned int src_indx
,
3891 unsigned int r_offset
,
3892 unsigned int r_type
)
3896 case elfcpp::R_X86_64_64
:
3897 case elfcpp::R_X86_64_32
:
3898 case elfcpp::R_X86_64_32S
:
3899 case elfcpp::R_X86_64_16
:
3900 case elfcpp::R_X86_64_8
:
3901 case elfcpp::R_X86_64_GOT64
:
3902 case elfcpp::R_X86_64_GOT32
:
3903 case elfcpp::R_X86_64_GOTPCREL64
:
3904 case elfcpp::R_X86_64_GOTPCREL
:
3905 case elfcpp::R_X86_64_GOTPCRELX
:
3906 case elfcpp::R_X86_64_REX_GOTPCRELX
:
3907 case elfcpp::R_X86_64_GOTPLT64
:
3911 case elfcpp::R_X86_64_PC32
:
3913 // This relocation may be used both for function calls and
3914 // for taking address of a function. We distinguish between
3915 // them by checking the opcodes.
3916 uint64_t sh_flags
= src_obj
->section_flags(src_indx
);
3917 bool is_executable
= (sh_flags
& elfcpp::SHF_EXECINSTR
) != 0;
3920 section_size_type stype
;
3921 const unsigned char* view
= src_obj
->section_contents(src_indx
,
3927 && view
[r_offset
- 1] == 0xe8)
3932 && view
[r_offset
- 1] == 0xe9)
3935 // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
3937 && view
[r_offset
- 2] == 0x0f
3938 && view
[r_offset
- 1] >= 0x80
3939 && view
[r_offset
- 1] <= 0x8f)
3943 // Be conservative and treat all others as function pointers.
3950 // For safe ICF, scan a relocation for a local symbol to check if it
3951 // corresponds to a function pointer being taken. In that case mark
3952 // the function whose pointer was taken as not foldable.
3956 Target_x86_64
<size
>::Scan::local_reloc_may_be_function_pointer(
3959 Target_x86_64
<size
>* ,
3960 Sized_relobj_file
<size
, false>* src_obj
,
3961 unsigned int src_indx
,
3963 const elfcpp::Rela
<size
, false>& reloc
,
3964 unsigned int r_type
,
3965 const elfcpp::Sym
<size
, false>&)
3967 // When building a shared library, do not fold any local symbols as it is
3968 // not possible to distinguish pointer taken versus a call by looking at
3969 // the relocation types.
3970 if (parameters
->options().shared())
3973 return possible_function_pointer_reloc(src_obj
, src_indx
,
3974 reloc
.get_r_offset(), r_type
);
3977 // For safe ICF, scan a relocation for a global symbol to check if it
3978 // corresponds to a function pointer being taken. In that case mark
3979 // the function whose pointer was taken as not foldable.
3983 Target_x86_64
<size
>::Scan::global_reloc_may_be_function_pointer(
3986 Target_x86_64
<size
>* ,
3987 Sized_relobj_file
<size
, false>* src_obj
,
3988 unsigned int src_indx
,
3990 const elfcpp::Rela
<size
, false>& reloc
,
3991 unsigned int r_type
,
3994 // When building a shared library, do not fold symbols whose visibility
3995 // is hidden, internal or protected.
3996 if (parameters
->options().shared()
3997 && (gsym
->visibility() == elfcpp::STV_INTERNAL
3998 || gsym
->visibility() == elfcpp::STV_PROTECTED
3999 || gsym
->visibility() == elfcpp::STV_HIDDEN
))
4002 return possible_function_pointer_reloc(src_obj
, src_indx
,
4003 reloc
.get_r_offset(), r_type
);
4006 // Scan a relocation for a global symbol.
4010 Target_x86_64
<size
>::Scan::global(Symbol_table
* symtab
,
4012 Target_x86_64
<size
>* target
,
4013 Sized_relobj_file
<size
, false>* object
,
4014 unsigned int data_shndx
,
4015 Output_section
* output_section
,
4016 const elfcpp::Rela
<size
, false>& reloc
,
4017 unsigned int r_type
,
4020 // A STT_GNU_IFUNC symbol may require a PLT entry.
4021 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
4022 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
4023 target
->make_plt_entry(symtab
, layout
, gsym
);
4027 case elfcpp::R_X86_64_NONE
:
4028 case elfcpp::R_X86_64_GNU_VTINHERIT
:
4029 case elfcpp::R_X86_64_GNU_VTENTRY
:
4032 case elfcpp::R_X86_64_64
:
4033 case elfcpp::R_X86_64_32
:
4034 case elfcpp::R_X86_64_32S
:
4035 case elfcpp::R_X86_64_16
:
4036 case elfcpp::R_X86_64_8
:
4038 // Make a PLT entry if necessary.
4039 if (gsym
->needs_plt_entry())
4041 target
->make_plt_entry(symtab
, layout
, gsym
);
4042 // Since this is not a PC-relative relocation, we may be
4043 // taking the address of a function. In that case we need to
4044 // set the entry in the dynamic symbol table to the address of
4046 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
4047 gsym
->set_needs_dynsym_value();
4049 // Make a dynamic relocation if necessary.
4050 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
4052 if (!parameters
->options().output_is_position_independent()
4053 && gsym
->may_need_copy_reloc())
4055 target
->copy_reloc(symtab
, layout
, object
,
4056 data_shndx
, output_section
, gsym
, reloc
);
4058 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
4059 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
4060 && gsym
->type() == elfcpp::STT_GNU_IFUNC
4061 && gsym
->can_use_relative_reloc(false)
4062 && !gsym
->is_from_dynobj()
4063 && !gsym
->is_undefined()
4064 && !gsym
->is_preemptible())
4066 // Use an IRELATIVE reloc for a locally defined
4067 // STT_GNU_IFUNC symbol. This makes a function
4068 // address in a PIE executable match the address in a
4069 // shared library that it links against.
4070 Reloc_section
* rela_dyn
=
4071 target
->rela_irelative_section(layout
);
4072 unsigned int r_type
= elfcpp::R_X86_64_IRELATIVE
;
4073 rela_dyn
->add_symbolless_global_addend(gsym
, r_type
,
4074 output_section
, object
,
4076 reloc
.get_r_offset(),
4077 reloc
.get_r_addend());
4079 else if (((size
== 64 && r_type
== elfcpp::R_X86_64_64
)
4080 || (size
== 32 && r_type
== elfcpp::R_X86_64_32
))
4081 && gsym
->can_use_relative_reloc(false))
4083 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4084 rela_dyn
->add_global_relative(gsym
, elfcpp::R_X86_64_RELATIVE
,
4085 output_section
, object
,
4087 reloc
.get_r_offset(),
4088 reloc
.get_r_addend(), false);
4092 this->check_non_pic(object
, r_type
, gsym
);
4093 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4094 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4095 data_shndx
, reloc
.get_r_offset(),
4096 reloc
.get_r_addend());
4102 case elfcpp::R_X86_64_PC64
:
4103 case elfcpp::R_X86_64_PC32
:
4104 case elfcpp::R_X86_64_PC32_BND
:
4105 case elfcpp::R_X86_64_PC16
:
4106 case elfcpp::R_X86_64_PC8
:
4108 // Make a PLT entry if necessary.
4109 if (gsym
->needs_plt_entry())
4110 target
->make_plt_entry(symtab
, layout
, gsym
);
4111 // Make a dynamic relocation if necessary.
4112 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
4114 if (parameters
->options().output_is_executable()
4115 && gsym
->may_need_copy_reloc())
4117 target
->copy_reloc(symtab
, layout
, object
,
4118 data_shndx
, output_section
, gsym
, reloc
);
4122 this->check_non_pic(object
, r_type
, gsym
);
4123 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4124 rela_dyn
->add_global(gsym
, r_type
, output_section
, object
,
4125 data_shndx
, reloc
.get_r_offset(),
4126 reloc
.get_r_addend());
4132 case elfcpp::R_X86_64_GOT64
:
4133 case elfcpp::R_X86_64_GOT32
:
4134 case elfcpp::R_X86_64_GOTPCREL64
:
4135 case elfcpp::R_X86_64_GOTPCREL
:
4136 case elfcpp::R_X86_64_GOTPCRELX
:
4137 case elfcpp::R_X86_64_REX_GOTPCRELX
:
4138 case elfcpp::R_X86_64_GOTPLT64
:
4140 // The symbol requires a GOT entry.
4141 Output_data_got
<64, false>* got
= target
->got_section(symtab
, layout
);
4143 // If we convert this from
4144 // mov foo@GOTPCREL(%rip), %reg
4145 // to lea foo(%rip), %reg.
4148 // (callq|jmpq) *foo@GOTPCRELX(%rip) to
4150 // in Relocate::relocate, then there is nothing to do here.
4151 // We cannot make these optimizations in incremental linking mode,
4152 // because we look at the opcode to decide whether or not to make
4153 // change, and during an incremental update, the change may have
4154 // already been applied.
4156 Lazy_view
<size
> view(object
, data_shndx
);
4157 size_t r_offset
= reloc
.get_r_offset();
4158 if (!parameters
->incremental()
4160 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
, r_type
,
4164 if (!parameters
->incremental()
4166 && Target_x86_64
<size
>::can_convert_callq_to_direct(gsym
, r_type
,
4171 if (gsym
->final_value_is_known())
4173 // For a STT_GNU_IFUNC symbol we want the PLT address.
4174 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
4175 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
4177 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4181 // If this symbol is not fully resolved, we need to add a
4182 // dynamic relocation for it.
4183 Reloc_section
* rela_dyn
= target
->rela_dyn_section(layout
);
4185 // Use a GLOB_DAT rather than a RELATIVE reloc if:
4187 // 1) The symbol may be defined in some other module.
4189 // 2) We are building a shared library and this is a
4190 // protected symbol; using GLOB_DAT means that the dynamic
4191 // linker can use the address of the PLT in the main
4192 // executable when appropriate so that function address
4193 // comparisons work.
4195 // 3) This is a STT_GNU_IFUNC symbol in position dependent
4196 // code, again so that function address comparisons work.
4197 if (gsym
->is_from_dynobj()
4198 || gsym
->is_undefined()
4199 || gsym
->is_preemptible()
4200 || (gsym
->visibility() == elfcpp::STV_PROTECTED
4201 && parameters
->options().shared())
4202 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
4203 && parameters
->options().output_is_position_independent()))
4204 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
, rela_dyn
,
4205 elfcpp::R_X86_64_GLOB_DAT
);
4208 // For a STT_GNU_IFUNC symbol we want to write the PLT
4209 // offset into the GOT, so that function pointer
4210 // comparisons work correctly.
4212 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
4213 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
4216 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
4217 // Tell the dynamic linker to use the PLT address
4218 // when resolving relocations.
4219 if (gsym
->is_from_dynobj()
4220 && !parameters
->options().shared())
4221 gsym
->set_needs_dynsym_value();
4225 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
4226 rela_dyn
->add_global_relative(gsym
,
4227 elfcpp::R_X86_64_RELATIVE
,
4228 got
, got_off
, 0, false);
4235 case elfcpp::R_X86_64_PLT32
:
4236 case elfcpp::R_X86_64_PLT32_BND
:
4237 // If the symbol is fully resolved, this is just a PC32 reloc.
4238 // Otherwise we need a PLT entry.
4239 if (gsym
->final_value_is_known())
4241 // If building a shared library, we can also skip the PLT entry
4242 // if the symbol is defined in the output file and is protected
4244 if (gsym
->is_defined()
4245 && !gsym
->is_from_dynobj()
4246 && !gsym
->is_preemptible())
4248 target
->make_plt_entry(symtab
, layout
, gsym
);
4251 case elfcpp::R_X86_64_GOTPC32
:
4252 case elfcpp::R_X86_64_GOTOFF64
:
4253 case elfcpp::R_X86_64_GOTPC64
:
4254 case elfcpp::R_X86_64_PLTOFF64
:
4255 // We need a GOT section.
4256 target
->got_section(symtab
, layout
);
4257 // For PLTOFF64, we also need a PLT entry (but only if the
4258 // symbol is not fully resolved).
4259 if (r_type
== elfcpp::R_X86_64_PLTOFF64
4260 && !gsym
->final_value_is_known())
4261 target
->make_plt_entry(symtab
, layout
, gsym
);
4264 case elfcpp::R_X86_64_COPY
:
4265 case elfcpp::R_X86_64_GLOB_DAT
:
4266 case elfcpp::R_X86_64_JUMP_SLOT
:
4267 case elfcpp::R_X86_64_RELATIVE
:
4268 case elfcpp::R_X86_64_IRELATIVE
:
4269 // These are outstanding tls relocs, which are unexpected when linking
4270 case elfcpp::R_X86_64_TPOFF64
:
4271 case elfcpp::R_X86_64_DTPMOD64
:
4272 case elfcpp::R_X86_64_TLSDESC
:
4273 gold_error(_("%s: unexpected reloc %u in object file"),
4274 object
->name().c_str(), r_type
);
4277 // These are initial tls relocs, which are expected for global()
4278 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4279 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4280 case elfcpp::R_X86_64_TLSDESC_CALL
:
4281 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4282 case elfcpp::R_X86_64_DTPOFF32
:
4283 case elfcpp::R_X86_64_DTPOFF64
:
4284 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4285 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4287 // For the Initial-Exec model, we can treat undef symbols as final
4288 // when building an executable.
4289 const bool is_final
= (gsym
->final_value_is_known() ||
4290 (r_type
== elfcpp::R_X86_64_GOTTPOFF
&&
4291 gsym
->is_undefined() &&
4292 parameters
->options().output_is_executable()));
4293 const tls::Tls_optimization optimized_type
4294 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
4297 case elfcpp::R_X86_64_TLSGD
: // General-dynamic
4298 if (optimized_type
== tls::TLSOPT_NONE
)
4300 // Create a pair of GOT entries for the module index and
4301 // dtv-relative offset.
4302 Output_data_got
<64, false>* got
4303 = target
->got_section(symtab
, layout
);
4304 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
4305 target
->rela_dyn_section(layout
),
4306 elfcpp::R_X86_64_DTPMOD64
,
4307 elfcpp::R_X86_64_DTPOFF64
);
4309 else if (optimized_type
== tls::TLSOPT_TO_IE
)
4311 // Create a GOT entry for the tp-relative offset.
4312 Output_data_got
<64, false>* got
4313 = target
->got_section(symtab
, layout
);
4314 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
4315 target
->rela_dyn_section(layout
),
4316 elfcpp::R_X86_64_TPOFF64
);
4318 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
4319 unsupported_reloc_global(object
, r_type
, gsym
);
4322 case elfcpp::R_X86_64_GOTPC32_TLSDESC
:
4323 target
->define_tls_base_symbol(symtab
, layout
);
4324 if (optimized_type
== tls::TLSOPT_NONE
)
4326 // Create reserved PLT and GOT entries for the resolver.
4327 target
->reserve_tlsdesc_entries(symtab
, layout
);
4329 // Create a double GOT entry with an R_X86_64_TLSDESC
4330 // reloc. The R_X86_64_TLSDESC reloc is resolved
4331 // lazily, so the GOT entry needs to be in an area in
4332 // .got.plt, not .got. Call got_section to make sure
4333 // the section has been created.
4334 target
->got_section(symtab
, layout
);
4335 Output_data_got
<64, false>* got
= target
->got_tlsdesc_section();
4336 Reloc_section
* rt
= target
->rela_tlsdesc_section(layout
);
4337 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
4338 elfcpp::R_X86_64_TLSDESC
, 0);
4340 else if (optimized_type
== tls::TLSOPT_TO_IE
)
4342 // Create a GOT entry for the tp-relative offset.
4343 Output_data_got
<64, false>* got
4344 = target
->got_section(symtab
, layout
);
4345 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
4346 target
->rela_dyn_section(layout
),
4347 elfcpp::R_X86_64_TPOFF64
);
4349 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
4350 unsupported_reloc_global(object
, r_type
, gsym
);
4353 case elfcpp::R_X86_64_TLSDESC_CALL
:
4356 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4357 if (optimized_type
== tls::TLSOPT_NONE
)
4359 // Create a GOT entry for the module index.
4360 target
->got_mod_index_entry(symtab
, layout
, object
);
4362 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
4363 unsupported_reloc_global(object
, r_type
, gsym
);
4366 case elfcpp::R_X86_64_DTPOFF32
:
4367 case elfcpp::R_X86_64_DTPOFF64
:
4370 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4371 layout
->set_has_static_tls();
4372 if (optimized_type
== tls::TLSOPT_NONE
)
4374 // Create a GOT entry for the tp-relative offset.
4375 Output_data_got
<64, false>* got
4376 = target
->got_section(symtab
, layout
);
4377 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
4378 target
->rela_dyn_section(layout
),
4379 elfcpp::R_X86_64_TPOFF64
);
4381 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
4382 unsupported_reloc_global(object
, r_type
, gsym
);
4385 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4386 layout
->set_has_static_tls();
4387 if (parameters
->options().shared())
4388 unsupported_reloc_global(object
, r_type
, gsym
);
4397 case elfcpp::R_X86_64_SIZE32
:
4398 case elfcpp::R_X86_64_SIZE64
:
4400 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
4401 object
->name().c_str(), r_type
,
4402 gsym
->demangled_name().c_str());
4409 Target_x86_64
<size
>::gc_process_relocs(Symbol_table
* symtab
,
4411 Sized_relobj_file
<size
, false>* object
,
4412 unsigned int data_shndx
,
4413 unsigned int sh_type
,
4414 const unsigned char* prelocs
,
4416 Output_section
* output_section
,
4417 bool needs_special_offset_handling
,
4418 size_t local_symbol_count
,
4419 const unsigned char* plocal_symbols
)
4421 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
4424 if (sh_type
== elfcpp::SHT_REL
)
4429 gold::gc_process_relocs
<size
, false, Target_x86_64
<size
>, Scan
,
4439 needs_special_offset_handling
,
4444 // Scan relocations for a section.
4448 Target_x86_64
<size
>::scan_relocs(Symbol_table
* symtab
,
4450 Sized_relobj_file
<size
, false>* object
,
4451 unsigned int data_shndx
,
4452 unsigned int sh_type
,
4453 const unsigned char* prelocs
,
4455 Output_section
* output_section
,
4456 bool needs_special_offset_handling
,
4457 size_t local_symbol_count
,
4458 const unsigned char* plocal_symbols
)
4460 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
4463 if (sh_type
== elfcpp::SHT_REL
)
4465 gold_error(_("%s: unsupported REL reloc section"),
4466 object
->name().c_str());
4470 gold::scan_relocs
<size
, false, Target_x86_64
<size
>, Scan
, Classify_reloc
>(
4479 needs_special_offset_handling
,
4484 // Finalize the sections.
4488 Target_x86_64
<size
>::do_finalize_sections(
4490 const Input_objects
*,
4491 Symbol_table
* symtab
)
4493 const Reloc_section
* rel_plt
= (this->plt_
== NULL
4495 : this->plt_
->rela_plt());
4496 layout
->add_target_dynamic_tags(false, this->got_plt_
, rel_plt
,
4497 this->rela_dyn_
, true, false);
4499 // Fill in some more dynamic tags.
4500 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
4503 if (this->plt_
!= NULL
4504 && this->plt_
->output_section() != NULL
4505 && this->plt_
->has_tlsdesc_entry())
4507 unsigned int plt_offset
= this->plt_
->get_tlsdesc_plt_offset();
4508 unsigned int got_offset
= this->plt_
->get_tlsdesc_got_offset();
4509 this->got_
->finalize_data_size();
4510 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT
,
4511 this->plt_
, plt_offset
);
4512 odyn
->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT
,
4513 this->got_
, got_offset
);
4517 // Emit any relocs we saved in an attempt to avoid generating COPY
4519 if (this->copy_relocs_
.any_saved_relocs())
4520 this->copy_relocs_
.emit(this->rela_dyn_section(layout
));
4522 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
4523 // the .got.plt section.
4524 Symbol
* sym
= this->global_offset_table_
;
4527 uint64_t data_size
= this->got_plt_
->current_data_size();
4528 symtab
->get_sized_symbol
<size
>(sym
)->set_symsize(data_size
);
4531 if (parameters
->doing_static_link()
4532 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
4534 // If linking statically, make sure that the __rela_iplt symbols
4535 // were defined if necessary, even if we didn't create a PLT.
4536 static const Define_symbol_in_segment syms
[] =
4539 "__rela_iplt_start", // name
4540 elfcpp::PT_LOAD
, // segment_type
4541 elfcpp::PF_W
, // segment_flags_set
4542 elfcpp::PF(0), // segment_flags_clear
4545 elfcpp::STT_NOTYPE
, // type
4546 elfcpp::STB_GLOBAL
, // binding
4547 elfcpp::STV_HIDDEN
, // visibility
4549 Symbol::SEGMENT_START
, // offset_from_base
4553 "__rela_iplt_end", // name
4554 elfcpp::PT_LOAD
, // segment_type
4555 elfcpp::PF_W
, // segment_flags_set
4556 elfcpp::PF(0), // segment_flags_clear
4559 elfcpp::STT_NOTYPE
, // type
4560 elfcpp::STB_GLOBAL
, // binding
4561 elfcpp::STV_HIDDEN
, // visibility
4563 Symbol::SEGMENT_START
, // offset_from_base
4568 symtab
->define_symbols(layout
, 2, syms
,
4569 layout
->script_options()->saw_sections_clause());
4573 // For x32, we need to handle PC-relative relocations using full 64-bit
4574 // arithmetic, so that we can detect relocation overflows properly.
4575 // This class overrides the pcrela32_check methods from the defaults in
4576 // Relocate_functions in reloc.h.
4579 class X86_64_relocate_functions
: public Relocate_functions
<size
, false>
4582 typedef Relocate_functions
<size
, false> Base
;
4584 // Do a simple PC relative relocation with the addend in the
4586 static inline typename
Base::Reloc_status
4587 pcrela32_check(unsigned char* view
,
4588 typename
elfcpp::Elf_types
<64>::Elf_Addr value
,
4589 typename
elfcpp::Elf_types
<64>::Elf_Swxword addend
,
4590 typename
elfcpp::Elf_types
<64>::Elf_Addr address
)
4592 typedef typename
elfcpp::Swap
<32, false>::Valtype Valtype
;
4593 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
4594 value
= value
+ addend
- address
;
4595 elfcpp::Swap
<32, false>::writeval(wv
, value
);
4596 return (Bits
<32>::has_overflow(value
)
4597 ? Base::RELOC_OVERFLOW
: Base::RELOC_OK
);
4600 // Do a simple PC relative relocation with a Symbol_value with the
4601 // addend in the relocation.
4602 static inline typename
Base::Reloc_status
4603 pcrela32_check(unsigned char* view
,
4604 const Sized_relobj_file
<size
, false>* object
,
4605 const Symbol_value
<size
>* psymval
,
4606 typename
elfcpp::Elf_types
<64>::Elf_Swxword addend
,
4607 typename
elfcpp::Elf_types
<64>::Elf_Addr address
)
4609 typedef typename
elfcpp::Swap
<32, false>::Valtype Valtype
;
4610 Valtype
* wv
= reinterpret_cast<Valtype
*>(view
);
4611 typename
elfcpp::Elf_types
<64>::Elf_Addr value
;
4613 value
= psymval
->value(object
, addend
);
4616 // For negative addends, get the symbol value without
4617 // the addend, then add the addend using 64-bit arithmetic.
4618 value
= psymval
->value(object
, 0);
4622 elfcpp::Swap
<32, false>::writeval(wv
, value
);
4623 return (Bits
<32>::has_overflow(value
)
4624 ? Base::RELOC_OVERFLOW
: Base::RELOC_OK
);
4628 // Perform a relocation.
4632 Target_x86_64
<size
>::Relocate::relocate(
4633 const Relocate_info
<size
, false>* relinfo
,
4635 Target_x86_64
<size
>* target
,
4638 const unsigned char* preloc
,
4639 const Sized_symbol
<size
>* gsym
,
4640 const Symbol_value
<size
>* psymval
,
4641 unsigned char* view
,
4642 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
4643 section_size_type view_size
)
4645 typedef X86_64_relocate_functions
<size
> Reloc_funcs
;
4646 const elfcpp::Rela
<size
, false> rela(preloc
);
4647 unsigned int r_type
= elfcpp::elf_r_type
<size
>(rela
.get_r_info());
4649 if (this->skip_call_tls_get_addr_
)
4651 if ((r_type
!= elfcpp::R_X86_64_PLT32
4652 && r_type
!= elfcpp::R_X86_64_GOTPCREL
4653 && r_type
!= elfcpp::R_X86_64_GOTPCRELX
4654 && r_type
!= elfcpp::R_X86_64_PLT32_BND
4655 && r_type
!= elfcpp::R_X86_64_PC32_BND
4656 && r_type
!= elfcpp::R_X86_64_PC32
)
4658 || strcmp(gsym
->name(), "__tls_get_addr") != 0)
4660 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4661 _("missing expected TLS relocation"));
4662 this->skip_call_tls_get_addr_
= false;
4666 this->skip_call_tls_get_addr_
= false;
4674 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
4676 // Pick the value to use for symbols defined in the PLT.
4677 Symbol_value
<size
> symval
;
4679 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
4681 symval
.set_output_value(target
->plt_address_for_global(gsym
));
4684 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
4686 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4687 if (object
->local_has_plt_offset(r_sym
))
4689 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
));
4694 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
4696 // Get the GOT offset if needed.
4697 // The GOT pointer points to the end of the GOT section.
4698 // We need to subtract the size of the GOT section to get
4699 // the actual offset to use in the relocation.
4700 bool have_got_offset
= false;
4701 // Since the actual offset is always negative, we use signed int to
4702 // support 64-bit GOT relocations.
4706 case elfcpp::R_X86_64_GOT32
:
4707 case elfcpp::R_X86_64_GOT64
:
4708 case elfcpp::R_X86_64_GOTPLT64
:
4709 case elfcpp::R_X86_64_GOTPCREL64
:
4712 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4713 got_offset
= gsym
->got_offset(GOT_TYPE_STANDARD
) - target
->got_size();
4717 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4718 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
4719 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4720 - target
->got_size());
4722 have_got_offset
= true;
4729 typename
Reloc_funcs::Reloc_status rstatus
= Reloc_funcs::RELOC_OK
;
4733 case elfcpp::R_X86_64_NONE
:
4734 case elfcpp::R_X86_64_GNU_VTINHERIT
:
4735 case elfcpp::R_X86_64_GNU_VTENTRY
:
4738 case elfcpp::R_X86_64_64
:
4739 Reloc_funcs::rela64(view
, object
, psymval
, addend
);
4742 case elfcpp::R_X86_64_PC64
:
4743 Reloc_funcs::pcrela64(view
, object
, psymval
, addend
,
4747 case elfcpp::R_X86_64_32
:
4748 rstatus
= Reloc_funcs::rela32_check(view
, object
, psymval
, addend
,
4749 Reloc_funcs::CHECK_UNSIGNED
);
4752 case elfcpp::R_X86_64_32S
:
4753 rstatus
= Reloc_funcs::rela32_check(view
, object
, psymval
, addend
,
4754 Reloc_funcs::CHECK_SIGNED
);
4757 case elfcpp::R_X86_64_PC32
:
4758 case elfcpp::R_X86_64_PC32_BND
:
4759 rstatus
= Reloc_funcs::pcrela32_check(view
, object
, psymval
, addend
,
4763 case elfcpp::R_X86_64_16
:
4764 Reloc_funcs::rela16(view
, object
, psymval
, addend
);
4767 case elfcpp::R_X86_64_PC16
:
4768 Reloc_funcs::pcrela16(view
, object
, psymval
, addend
, address
);
4771 case elfcpp::R_X86_64_8
:
4772 Reloc_funcs::rela8(view
, object
, psymval
, addend
);
4775 case elfcpp::R_X86_64_PC8
:
4776 Reloc_funcs::pcrela8(view
, object
, psymval
, addend
, address
);
4779 case elfcpp::R_X86_64_PLT32
:
4780 case elfcpp::R_X86_64_PLT32_BND
:
4781 gold_assert(gsym
== NULL
4782 || gsym
->has_plt_offset()
4783 || gsym
->final_value_is_known()
4784 || (gsym
->is_defined()
4785 && !gsym
->is_from_dynobj()
4786 && !gsym
->is_preemptible()));
4787 // Note: while this code looks the same as for R_X86_64_PC32, it
4788 // behaves differently because psymval was set to point to
4789 // the PLT entry, rather than the symbol, in Scan::global().
4790 rstatus
= Reloc_funcs::pcrela32_check(view
, object
, psymval
, addend
,
4794 case elfcpp::R_X86_64_PLTOFF64
:
4797 gold_assert(gsym
->has_plt_offset()
4798 || gsym
->final_value_is_known());
4799 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
;
4800 // This is the address of GLOBAL_OFFSET_TABLE.
4801 got_address
= target
->got_plt_section()->address();
4802 Reloc_funcs::rela64(view
, object
, psymval
, addend
- got_address
);
4806 case elfcpp::R_X86_64_GOT32
:
4807 gold_assert(have_got_offset
);
4808 Reloc_funcs::rela32(view
, got_offset
, addend
);
4811 case elfcpp::R_X86_64_GOTPC32
:
4814 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4815 value
= target
->got_plt_section()->address();
4816 Reloc_funcs::pcrela32_check(view
, value
, addend
, address
);
4820 case elfcpp::R_X86_64_GOT64
:
4821 case elfcpp::R_X86_64_GOTPLT64
:
4822 // R_X86_64_GOTPLT64 is obsolete and treated the same as
4824 gold_assert(have_got_offset
);
4825 Reloc_funcs::rela64(view
, got_offset
, addend
);
4828 case elfcpp::R_X86_64_GOTPC64
:
4831 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4832 value
= target
->got_plt_section()->address();
4833 Reloc_funcs::pcrela64(view
, value
, addend
, address
);
4837 case elfcpp::R_X86_64_GOTOFF64
:
4839 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4840 value
= (psymval
->value(object
, 0)
4841 - target
->got_plt_section()->address());
4842 Reloc_funcs::rela64(view
, value
, addend
);
4846 case elfcpp::R_X86_64_GOTPCREL
:
4847 case elfcpp::R_X86_64_GOTPCRELX
:
4848 case elfcpp::R_X86_64_REX_GOTPCRELX
:
4851 // mov foo@GOTPCREL(%rip), %reg
4852 // to lea foo(%rip), %reg.
4854 if (!parameters
->incremental()
4856 && rela
.get_r_offset() >= 2
4858 && !psymval
->is_ifunc_symbol())
4860 && rela
.get_r_offset() >= 2
4861 && Target_x86_64
<size
>::can_convert_mov_to_lea(gsym
, r_type
,
4865 Reloc_funcs::pcrela32(view
, object
, psymval
, addend
, address
);
4868 // callq *foo@GOTPCRELX(%rip) to
4870 // and jmpq *foo@GOTPCRELX(%rip) to
4873 else if (!parameters
->incremental()
4875 && rela
.get_r_offset() >= 2
4876 && Target_x86_64
<size
>::can_convert_callq_to_direct(gsym
,
4880 if (view
[-1] == 0x15)
4882 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4883 // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
4886 // Convert GOTPCRELX to 32-bit pc relative reloc.
4887 Reloc_funcs::pcrela32(view
, object
, psymval
, addend
, address
);
4891 // Convert jmpq *foo@GOTPCRELX(%rip) to
4894 // The opcode of direct jmpq is 0xe9.
4896 // The opcode of nop is 0x90.
4898 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip
4899 // relative and since the instruction following the jmpq is now
4900 // the nop, offset the address by 1 byte. The start of the
4901 // relocation also moves ahead by 1 byte.
4902 Reloc_funcs::pcrela32(&view
[-1], object
, psymval
, addend
,
4910 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
4911 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
4912 - target
->got_size());
4916 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4917 gold_assert(object
->local_has_got_offset(r_sym
,
4918 GOT_TYPE_STANDARD
));
4919 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
4920 - target
->got_size());
4922 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4923 value
= target
->got_plt_section()->address() + got_offset
;
4924 Reloc_funcs::pcrela32_check(view
, value
, addend
, address
);
4929 case elfcpp::R_X86_64_GOTPCREL64
:
4931 gold_assert(have_got_offset
);
4932 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
4933 value
= target
->got_plt_section()->address() + got_offset
;
4934 Reloc_funcs::pcrela64(view
, value
, addend
, address
);
4938 case elfcpp::R_X86_64_COPY
:
4939 case elfcpp::R_X86_64_GLOB_DAT
:
4940 case elfcpp::R_X86_64_JUMP_SLOT
:
4941 case elfcpp::R_X86_64_RELATIVE
:
4942 case elfcpp::R_X86_64_IRELATIVE
:
4943 // These are outstanding tls relocs, which are unexpected when linking
4944 case elfcpp::R_X86_64_TPOFF64
:
4945 case elfcpp::R_X86_64_DTPMOD64
:
4946 case elfcpp::R_X86_64_TLSDESC
:
4947 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4948 _("unexpected reloc %u in object file"),
4952 // These are initial tls relocs, which are expected when linking
4953 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
4954 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
4955 case elfcpp::R_X86_64_TLSDESC_CALL
:
4956 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
4957 case elfcpp::R_X86_64_DTPOFF32
:
4958 case elfcpp::R_X86_64_DTPOFF64
:
4959 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
4960 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
4961 this->relocate_tls(relinfo
, target
, relnum
, rela
, r_type
, gsym
, psymval
,
4962 view
, address
, view_size
);
4965 case elfcpp::R_X86_64_SIZE32
:
4966 case elfcpp::R_X86_64_SIZE64
:
4968 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4969 _("unsupported reloc %u"),
4974 if (rstatus
== Reloc_funcs::RELOC_OVERFLOW
)
4978 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
4979 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4980 _("relocation overflow: "
4981 "reference to local symbol %u in %s"),
4982 r_sym
, object
->name().c_str());
4984 else if (gsym
->is_defined() && gsym
->source() == Symbol::FROM_OBJECT
)
4986 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4987 _("relocation overflow: "
4988 "reference to '%s' defined in %s"),
4990 gsym
->object()->name().c_str());
4994 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
4995 _("relocation overflow: reference to '%s'"),
5003 // Perform a TLS relocation.
5007 Target_x86_64
<size
>::Relocate::relocate_tls(
5008 const Relocate_info
<size
, false>* relinfo
,
5009 Target_x86_64
<size
>* target
,
5011 const elfcpp::Rela
<size
, false>& rela
,
5012 unsigned int r_type
,
5013 const Sized_symbol
<size
>* gsym
,
5014 const Symbol_value
<size
>* psymval
,
5015 unsigned char* view
,
5016 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5017 section_size_type view_size
)
5019 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
5021 const Sized_relobj_file
<size
, false>* object
= relinfo
->object
;
5022 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
5023 elfcpp::Shdr
<size
, false> data_shdr(relinfo
->data_shdr
);
5024 bool is_executable
= (data_shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0;
5026 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
5028 const bool is_final
= (gsym
== NULL
5029 ? !parameters
->options().shared()
5030 : gsym
->final_value_is_known());
5031 tls::Tls_optimization optimized_type
5032 = Target_x86_64
<size
>::optimize_tls_reloc(is_final
, r_type
);
5035 case elfcpp::R_X86_64_TLSGD
: // Global-dynamic
5036 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
5038 // If this code sequence is used in a non-executable section,
5039 // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
5040 // on the assumption that it's being used by itself in a debug
5041 // section. Therefore, in the unlikely event that the code
5042 // sequence appears in a non-executable section, we simply
5043 // leave it unoptimized.
5044 optimized_type
= tls::TLSOPT_NONE
;
5046 if (optimized_type
== tls::TLSOPT_TO_LE
)
5048 if (tls_segment
== NULL
)
5050 gold_assert(parameters
->errors()->error_count() > 0
5051 || issue_undefined_symbol_error(gsym
));
5054 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
5055 rela
, r_type
, value
, view
,
5061 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
5062 ? GOT_TYPE_TLS_OFFSET
5063 : GOT_TYPE_TLS_PAIR
);
5064 unsigned int got_offset
;
5067 gold_assert(gsym
->has_got_offset(got_type
));
5068 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
5072 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5073 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
5074 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
5075 - target
->got_size());
5077 if (optimized_type
== tls::TLSOPT_TO_IE
)
5079 value
= target
->got_plt_section()->address() + got_offset
;
5080 this->tls_gd_to_ie(relinfo
, relnum
, rela
, r_type
,
5081 value
, view
, address
, view_size
);
5084 else if (optimized_type
== tls::TLSOPT_NONE
)
5086 // Relocate the field with the offset of the pair of GOT
5088 value
= target
->got_plt_section()->address() + got_offset
;
5089 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
5094 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5095 _("unsupported reloc %u"), r_type
);
5098 case elfcpp::R_X86_64_GOTPC32_TLSDESC
: // Global-dynamic (from ~oliva url)
5099 case elfcpp::R_X86_64_TLSDESC_CALL
:
5100 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
5102 // See above comment for R_X86_64_TLSGD.
5103 optimized_type
= tls::TLSOPT_NONE
;
5105 if (optimized_type
== tls::TLSOPT_TO_LE
)
5107 if (tls_segment
== NULL
)
5109 gold_assert(parameters
->errors()->error_count() > 0
5110 || issue_undefined_symbol_error(gsym
));
5113 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
5114 rela
, r_type
, value
, view
,
5120 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
5121 ? GOT_TYPE_TLS_OFFSET
5122 : GOT_TYPE_TLS_DESC
);
5123 unsigned int got_offset
= 0;
5124 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
5125 && optimized_type
== tls::TLSOPT_NONE
)
5127 // We created GOT entries in the .got.tlsdesc portion of
5128 // the .got.plt section, but the offset stored in the
5129 // symbol is the offset within .got.tlsdesc.
5130 got_offset
= (target
->got_size()
5131 + target
->got_plt_section()->data_size());
5135 gold_assert(gsym
->has_got_offset(got_type
));
5136 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
5140 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5141 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
5142 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
5143 - target
->got_size());
5145 if (optimized_type
== tls::TLSOPT_TO_IE
)
5147 value
= target
->got_plt_section()->address() + got_offset
;
5148 this->tls_desc_gd_to_ie(relinfo
, relnum
,
5149 rela
, r_type
, value
, view
, address
,
5153 else if (optimized_type
== tls::TLSOPT_NONE
)
5155 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
5157 // Relocate the field with the offset of the pair of GOT
5159 value
= target
->got_plt_section()->address() + got_offset
;
5160 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
5166 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5167 _("unsupported reloc %u"), r_type
);
5170 case elfcpp::R_X86_64_TLSLD
: // Local-dynamic
5171 if (!is_executable
&& optimized_type
== tls::TLSOPT_TO_LE
)
5173 // See above comment for R_X86_64_TLSGD.
5174 optimized_type
= tls::TLSOPT_NONE
;
5176 if (optimized_type
== tls::TLSOPT_TO_LE
)
5178 if (tls_segment
== NULL
)
5180 gold_assert(parameters
->errors()->error_count() > 0
5181 || issue_undefined_symbol_error(gsym
));
5184 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rela
, r_type
,
5185 value
, view
, view_size
);
5188 else if (optimized_type
== tls::TLSOPT_NONE
)
5190 // Relocate the field with the offset of the GOT entry for
5191 // the module index.
5192 unsigned int got_offset
;
5193 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
5194 - target
->got_size());
5195 value
= target
->got_plt_section()->address() + got_offset
;
5196 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
5200 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5201 _("unsupported reloc %u"), r_type
);
5204 case elfcpp::R_X86_64_DTPOFF32
:
5205 // This relocation type is used in debugging information.
5206 // In that case we need to not optimize the value. If the
5207 // section is not executable, then we assume we should not
5208 // optimize this reloc. See comments above for R_X86_64_TLSGD,
5209 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
5211 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
5213 if (tls_segment
== NULL
)
5215 gold_assert(parameters
->errors()->error_count() > 0
5216 || issue_undefined_symbol_error(gsym
));
5219 value
-= tls_segment
->memsz();
5221 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
5224 case elfcpp::R_X86_64_DTPOFF64
:
5225 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
5226 if (optimized_type
== tls::TLSOPT_TO_LE
&& is_executable
)
5228 if (tls_segment
== NULL
)
5230 gold_assert(parameters
->errors()->error_count() > 0
5231 || issue_undefined_symbol_error(gsym
));
5234 value
-= tls_segment
->memsz();
5236 Relocate_functions
<size
, false>::rela64(view
, value
, addend
);
5239 case elfcpp::R_X86_64_GOTTPOFF
: // Initial-exec
5241 && gsym
->is_undefined()
5242 && parameters
->options().output_is_executable())
5244 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
5246 r_type
, value
, view
,
5250 else if (optimized_type
== tls::TLSOPT_TO_LE
)
5252 if (tls_segment
== NULL
)
5254 gold_assert(parameters
->errors()->error_count() > 0
5255 || issue_undefined_symbol_error(gsym
));
5258 Target_x86_64
<size
>::Relocate::tls_ie_to_le(relinfo
, relnum
,
5260 r_type
, value
, view
,
5264 else if (optimized_type
== tls::TLSOPT_NONE
)
5266 // Relocate the field with the offset of the GOT entry for
5267 // the tp-relative offset of the symbol.
5268 unsigned int got_offset
;
5271 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
5272 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_OFFSET
)
5273 - target
->got_size());
5277 unsigned int r_sym
= elfcpp::elf_r_sym
<size
>(rela
.get_r_info());
5278 gold_assert(object
->local_has_got_offset(r_sym
,
5279 GOT_TYPE_TLS_OFFSET
));
5280 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_OFFSET
)
5281 - target
->got_size());
5283 value
= target
->got_plt_section()->address() + got_offset
;
5284 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
,
5288 gold_error_at_location(relinfo
, relnum
, rela
.get_r_offset(),
5289 _("unsupported reloc type %u"),
5293 case elfcpp::R_X86_64_TPOFF32
: // Local-exec
5294 if (tls_segment
== NULL
)
5296 gold_assert(parameters
->errors()->error_count() > 0
5297 || issue_undefined_symbol_error(gsym
));
5300 value
-= tls_segment
->memsz();
5301 Relocate_functions
<size
, false>::rela32(view
, value
, addend
);
5306 // Do a relocation in which we convert a TLS General-Dynamic to an
5311 Target_x86_64
<size
>::Relocate::tls_gd_to_ie(
5312 const Relocate_info
<size
, false>* relinfo
,
5314 const elfcpp::Rela
<size
, false>& rela
,
5316 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
5317 unsigned char* view
,
5318 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5319 section_size_type view_size
)
5322 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5323 // .word 0x6666; rex64; call __tls_get_addr@PLT
5324 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
5325 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5326 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5327 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
5329 // leaq foo@tlsgd(%rip),%rdi;
5330 // .word 0x6666; rex64; call __tls_get_addr@PLT
5331 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
5332 // leaq foo@tlsgd(%rip),%rdi;
5333 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5334 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
5336 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
5337 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5338 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0
5339 || memcmp(view
+ 4, "\x66\x48\xff", 3) == 0));
5343 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
5345 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5346 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
5347 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5352 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
5354 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5355 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
5356 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
5360 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
5361 Relocate_functions
<size
, false>::pcrela32(view
+ 8, value
, addend
- 8,
5364 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5366 this->skip_call_tls_get_addr_
= true;
5369 // Do a relocation in which we convert a TLS General-Dynamic to a
5374 Target_x86_64
<size
>::Relocate::tls_gd_to_le(
5375 const Relocate_info
<size
, false>* relinfo
,
5377 Output_segment
* tls_segment
,
5378 const elfcpp::Rela
<size
, false>& rela
,
5380 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
5381 unsigned char* view
,
5382 section_size_type view_size
)
5385 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5386 // .word 0x6666; rex64; call __tls_get_addr@PLT
5387 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5388 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
5389 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5390 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
5392 // leaq foo@tlsgd(%rip),%rdi;
5393 // .word 0x6666; rex64; call __tls_get_addr@PLT
5394 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5395 // leaq foo@tlsgd(%rip),%rdi;
5396 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
5397 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
5399 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 12);
5400 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5401 (memcmp(view
+ 4, "\x66\x66\x48\xe8", 4) == 0
5402 || memcmp(view
+ 4, "\x66\x48\xff", 3) == 0));
5406 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
5408 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5409 (memcmp(view
- 4, "\x66\x48\x8d\x3d", 4) == 0));
5410 memcpy(view
- 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5415 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
,
5417 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5418 (memcmp(view
- 3, "\x48\x8d\x3d", 3) == 0));
5420 memcpy(view
- 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
5424 value
-= tls_segment
->memsz();
5425 Relocate_functions
<size
, false>::rela32(view
+ 8, value
, 0);
5427 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5429 this->skip_call_tls_get_addr_
= true;
5432 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
5436 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_ie(
5437 const Relocate_info
<size
, false>* relinfo
,
5439 const elfcpp::Rela
<size
, false>& rela
,
5440 unsigned int r_type
,
5441 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
5442 unsigned char* view
,
5443 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5444 section_size_type view_size
)
5446 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
5448 // leaq foo@tlsdesc(%rip), %rax
5449 // ==> movq foo@gottpoff(%rip), %rax
5450 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
5451 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
5452 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5453 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
5455 const elfcpp::Elf_Xword addend
= rela
.get_r_addend();
5456 Relocate_functions
<size
, false>::pcrela32(view
, value
, addend
, address
);
5460 // call *foo@tlscall(%rax)
5462 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
5463 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
5464 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5465 view
[0] == 0xff && view
[1] == 0x10);
5471 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
5475 Target_x86_64
<size
>::Relocate::tls_desc_gd_to_le(
5476 const Relocate_info
<size
, false>* relinfo
,
5478 Output_segment
* tls_segment
,
5479 const elfcpp::Rela
<size
, false>& rela
,
5480 unsigned int r_type
,
5481 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
5482 unsigned char* view
,
5483 section_size_type view_size
)
5485 if (r_type
== elfcpp::R_X86_64_GOTPC32_TLSDESC
)
5487 // leaq foo@tlsdesc(%rip), %rax
5488 // ==> movq foo@tpoff, %rax
5489 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
5490 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
5491 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5492 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x05);
5495 value
-= tls_segment
->memsz();
5496 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
5500 // call *foo@tlscall(%rax)
5502 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC_CALL
);
5503 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 2);
5504 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5505 view
[0] == 0xff && view
[1] == 0x10);
5513 Target_x86_64
<size
>::Relocate::tls_ld_to_le(
5514 const Relocate_info
<size
, false>* relinfo
,
5517 const elfcpp::Rela
<size
, false>& rela
,
5519 typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
5520 unsigned char* view
,
5521 section_size_type view_size
)
5523 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
5525 // ... leq foo@dtpoff(%rax),%reg
5526 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5528 // ... leq foo@dtpoff(%rax),%reg
5529 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5530 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
5532 // ... leq foo@dtpoff(%rax),%reg
5533 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
5535 // ... leq foo@dtpoff(%rax),%reg
5536 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
5538 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
5539 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 9);
5541 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5542 view
[-3] == 0x48 && view
[-2] == 0x8d && view
[-1] == 0x3d);
5544 tls::check_tls(relinfo
, relnum
, rela
.get_r_offset(),
5545 view
[4] == 0xe8 || view
[4] == 0xff);
5547 if (view
[4] == 0xe8)
5550 memcpy(view
- 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
5552 memcpy(view
- 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
5557 memcpy(view
- 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
5560 memcpy(view
- 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
5564 // The next reloc should be a PLT32 reloc against __tls_get_addr.
5566 this->skip_call_tls_get_addr_
= true;
5569 // Do a relocation in which we convert a TLS Initial-Exec to a
5574 Target_x86_64
<size
>::Relocate::tls_ie_to_le(
5575 const Relocate_info
<size
, false>* relinfo
,
5577 Output_segment
* tls_segment
,
5578 const elfcpp::Rela
<size
, false>& rela
,
5580 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
,
5581 unsigned char* view
,
5582 section_size_type view_size
)
5584 // We need to examine the opcodes to figure out which instruction we
5587 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
5588 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
5590 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, -3);
5591 tls::check_range(relinfo
, relnum
, rela
.get_r_offset(), view_size
, 4);
5593 unsigned char op1
= view
[-3];
5594 unsigned char op2
= view
[-2];
5595 unsigned char op3
= view
[-1];
5596 unsigned char reg
= op3
>> 3;
5603 else if (size
== 32 && op1
== 0x44)
5606 view
[-1] = 0xc0 | reg
;
5610 // Special handling for %rsp.
5613 else if (size
== 32 && op1
== 0x44)
5616 view
[-1] = 0xc0 | reg
;
5623 else if (size
== 32 && op1
== 0x44)
5626 view
[-1] = 0x80 | reg
| (reg
<< 3);
5629 if (tls_segment
!= NULL
)
5630 value
-= tls_segment
->memsz();
5631 Relocate_functions
<size
, false>::rela32(view
, value
, 0);
5634 // Relocate section data.
5638 Target_x86_64
<size
>::relocate_section(
5639 const Relocate_info
<size
, false>* relinfo
,
5640 unsigned int sh_type
,
5641 const unsigned char* prelocs
,
5643 Output_section
* output_section
,
5644 bool needs_special_offset_handling
,
5645 unsigned char* view
,
5646 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5647 section_size_type view_size
,
5648 const Reloc_symbol_changes
* reloc_symbol_changes
)
5650 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5653 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5655 gold::relocate_section
<size
, false, Target_x86_64
<size
>, Relocate
,
5656 gold::Default_comdat_behavior
, Classify_reloc
>(
5662 needs_special_offset_handling
,
5666 reloc_symbol_changes
);
5669 // Apply an incremental relocation. Incremental relocations always refer
5670 // to global symbols.
5674 Target_x86_64
<size
>::apply_relocation(
5675 const Relocate_info
<size
, false>* relinfo
,
5676 typename
elfcpp::Elf_types
<size
>::Elf_Addr r_offset
,
5677 unsigned int r_type
,
5678 typename
elfcpp::Elf_types
<size
>::Elf_Swxword r_addend
,
5680 unsigned char* view
,
5681 typename
elfcpp::Elf_types
<size
>::Elf_Addr address
,
5682 section_size_type view_size
)
5684 gold::apply_relocation
<size
, false, Target_x86_64
<size
>,
5685 typename Target_x86_64
<size
>::Relocate
>(
5697 // Scan the relocs during a relocatable link.
5701 Target_x86_64
<size
>::scan_relocatable_relocs(
5702 Symbol_table
* symtab
,
5704 Sized_relobj_file
<size
, false>* object
,
5705 unsigned int data_shndx
,
5706 unsigned int sh_type
,
5707 const unsigned char* prelocs
,
5709 Output_section
* output_section
,
5710 bool needs_special_offset_handling
,
5711 size_t local_symbol_count
,
5712 const unsigned char* plocal_symbols
,
5713 Relocatable_relocs
* rr
)
5715 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5717 typedef gold::Default_scan_relocatable_relocs
<Classify_reloc
>
5718 Scan_relocatable_relocs
;
5720 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5722 gold::scan_relocatable_relocs
<size
, false, Scan_relocatable_relocs
>(
5730 needs_special_offset_handling
,
5736 // Scan the relocs for --emit-relocs.
5740 Target_x86_64
<size
>::emit_relocs_scan(
5741 Symbol_table
* symtab
,
5743 Sized_relobj_file
<size
, false>* object
,
5744 unsigned int data_shndx
,
5745 unsigned int sh_type
,
5746 const unsigned char* prelocs
,
5748 Output_section
* output_section
,
5749 bool needs_special_offset_handling
,
5750 size_t local_symbol_count
,
5751 const unsigned char* plocal_syms
,
5752 Relocatable_relocs
* rr
)
5754 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5756 typedef gold::Default_emit_relocs_strategy
<Classify_reloc
>
5757 Emit_relocs_strategy
;
5759 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5761 gold::scan_relocatable_relocs
<size
, false, Emit_relocs_strategy
>(
5769 needs_special_offset_handling
,
5775 // Relocate a section during a relocatable link.
5779 Target_x86_64
<size
>::relocate_relocs(
5780 const Relocate_info
<size
, false>* relinfo
,
5781 unsigned int sh_type
,
5782 const unsigned char* prelocs
,
5784 Output_section
* output_section
,
5785 typename
elfcpp::Elf_types
<size
>::Elf_Off offset_in_output_section
,
5786 unsigned char* view
,
5787 typename
elfcpp::Elf_types
<size
>::Elf_Addr view_address
,
5788 section_size_type view_size
,
5789 unsigned char* reloc_view
,
5790 section_size_type reloc_view_size
)
5792 typedef gold::Default_classify_reloc
<elfcpp::SHT_RELA
, size
, false>
5795 gold_assert(sh_type
== elfcpp::SHT_RELA
);
5797 gold::relocate_relocs
<size
, false, Classify_reloc
>(
5802 offset_in_output_section
,
5810 // Return the value to use for a dynamic which requires special
5811 // treatment. This is how we support equality comparisons of function
5812 // pointers across shared library boundaries, as described in the
5813 // processor specific ABI supplement.
5817 Target_x86_64
<size
>::do_dynsym_value(const Symbol
* gsym
) const
5819 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
5820 return this->plt_address_for_global(gsym
);
5823 // Return a string used to fill a code section with nops to take up
5824 // the specified length.
5828 Target_x86_64
<size
>::do_code_fill(section_size_type length
) const
5832 // Build a jmpq instruction to skip over the bytes.
5833 unsigned char jmp
[5];
5835 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
5836 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
5837 + std::string(length
- 5, static_cast<char>(0x90)));
5840 // Nop sequences of various lengths.
5841 const char nop1
[1] = { '\x90' }; // nop
5842 const char nop2
[2] = { '\x66', '\x90' }; // xchg %ax %ax
5843 const char nop3
[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5844 const char nop4
[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax)
5846 const char nop5
[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1)
5848 const char nop6
[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1)
5849 '\x44', '\x00', '\x00' };
5850 const char nop7
[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax)
5851 '\x00', '\x00', '\x00',
5853 const char nop8
[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1)
5854 '\x00', '\x00', '\x00',
5856 const char nop9
[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1)
5857 '\x84', '\x00', '\x00',
5858 '\x00', '\x00', '\x00' };
5859 const char nop10
[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5860 '\x1f', '\x84', '\x00',
5861 '\x00', '\x00', '\x00',
5863 const char nop11
[11] = { '\x66', '\x66', '\x2e', // data16
5864 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5865 '\x00', '\x00', '\x00',
5867 const char nop12
[12] = { '\x66', '\x66', '\x66', // data16; data16
5868 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5869 '\x84', '\x00', '\x00',
5870 '\x00', '\x00', '\x00' };
5871 const char nop13
[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5872 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5873 '\x1f', '\x84', '\x00',
5874 '\x00', '\x00', '\x00',
5876 const char nop14
[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5877 '\x66', '\x66', '\x2e', // data16
5878 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5879 '\x00', '\x00', '\x00',
5881 const char nop15
[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5882 '\x66', '\x66', '\x66', // data16; data16
5883 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5884 '\x84', '\x00', '\x00',
5885 '\x00', '\x00', '\x00' };
5887 const char* nops
[16] = {
5889 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
5890 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
5893 return std::string(nops
[length
], length
);
5896 // Return the addend to use for a target specific relocation. The
5897 // only target specific relocation is R_X86_64_TLSDESC for a local
5898 // symbol. We want to set the addend is the offset of the local
5899 // symbol in the TLS segment.
5903 Target_x86_64
<size
>::do_reloc_addend(void* arg
, unsigned int r_type
,
5906 gold_assert(r_type
== elfcpp::R_X86_64_TLSDESC
);
5907 uintptr_t intarg
= reinterpret_cast<uintptr_t>(arg
);
5908 gold_assert(intarg
< this->tlsdesc_reloc_info_
.size());
5909 const Tlsdesc_info
& ti(this->tlsdesc_reloc_info_
[intarg
]);
5910 const Symbol_value
<size
>* psymval
= ti
.object
->local_symbol(ti
.r_sym
);
5911 gold_assert(psymval
->is_tls_symbol());
5912 // The value of a TLS symbol is the offset in the TLS segment.
5913 return psymval
->value(ti
.object
, 0);
5916 // Return the value to use for the base of a DW_EH_PE_datarel offset
5917 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
5918 // assembler can not write out the difference between two labels in
5919 // different sections, so instead of using a pc-relative value they
5920 // use an offset from the GOT.
5924 Target_x86_64
<size
>::do_ehframe_datarel_base() const
5926 gold_assert(this->global_offset_table_
!= NULL
);
5927 Symbol
* sym
= this->global_offset_table_
;
5928 Sized_symbol
<size
>* ssym
= static_cast<Sized_symbol
<size
>*>(sym
);
5929 return ssym
->value();
5932 // FNOFFSET in section SHNDX in OBJECT is the start of a function
5933 // compiled with -fsplit-stack. The function calls non-split-stack
5934 // code. We have to change the function so that it always ensures
5935 // that it has enough stack space to run some random function.
5937 static const unsigned char cmp_insn_32
[] = { 0x64, 0x3b, 0x24, 0x25 };
5938 static const unsigned char lea_r10_insn_32
[] = { 0x44, 0x8d, 0x94, 0x24 };
5939 static const unsigned char lea_r11_insn_32
[] = { 0x44, 0x8d, 0x9c, 0x24 };
5941 static const unsigned char cmp_insn_64
[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
5942 static const unsigned char lea_r10_insn_64
[] = { 0x4c, 0x8d, 0x94, 0x24 };
5943 static const unsigned char lea_r11_insn_64
[] = { 0x4c, 0x8d, 0x9c, 0x24 };
5947 Target_x86_64
<size
>::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
5948 section_offset_type fnoffset
,
5949 section_size_type fnsize
,
5950 const unsigned char*,
5952 unsigned char* view
,
5953 section_size_type view_size
,
5955 std::string
* to
) const
5957 const char* const cmp_insn
= reinterpret_cast<const char*>
5958 (size
== 32 ? cmp_insn_32
: cmp_insn_64
);
5959 const char* const lea_r10_insn
= reinterpret_cast<const char*>
5960 (size
== 32 ? lea_r10_insn_32
: lea_r10_insn_64
);
5961 const char* const lea_r11_insn
= reinterpret_cast<const char*>
5962 (size
== 32 ? lea_r11_insn_32
: lea_r11_insn_64
);
5964 const size_t cmp_insn_len
=
5965 (size
== 32 ? sizeof(cmp_insn_32
) : sizeof(cmp_insn_64
));
5966 const size_t lea_r10_insn_len
=
5967 (size
== 32 ? sizeof(lea_r10_insn_32
) : sizeof(lea_r10_insn_64
));
5968 const size_t lea_r11_insn_len
=
5969 (size
== 32 ? sizeof(lea_r11_insn_32
) : sizeof(lea_r11_insn_64
));
5970 const size_t nop_len
= (size
== 32 ? 7 : 8);
5972 // The function starts with a comparison of the stack pointer and a
5973 // field in the TCB. This is followed by a jump.
5976 if (this->match_view(view
, view_size
, fnoffset
, cmp_insn
, cmp_insn_len
)
5977 && fnsize
> nop_len
+ 1)
5979 // We will call __morestack if the carry flag is set after this
5980 // comparison. We turn the comparison into an stc instruction
5982 view
[fnoffset
] = '\xf9';
5983 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, nop_len
);
5985 // lea NN(%rsp),%r10
5986 // lea NN(%rsp),%r11
5987 else if ((this->match_view(view
, view_size
, fnoffset
,
5988 lea_r10_insn
, lea_r10_insn_len
)
5989 || this->match_view(view
, view_size
, fnoffset
,
5990 lea_r11_insn
, lea_r11_insn_len
))
5993 // This is loading an offset from the stack pointer for a
5994 // comparison. The offset is negative, so we decrease the
5995 // offset by the amount of space we need for the stack. This
5996 // means we will avoid calling __morestack if there happens to
5997 // be plenty of space on the stack already.
5998 unsigned char* pval
= view
+ fnoffset
+ 4;
5999 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
6000 val
-= parameters
->options().split_stack_adjust_size();
6001 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
6005 if (!object
->has_no_split_stack())
6006 object
->error(_("failed to match split-stack sequence at "
6007 "section %u offset %0zx"),
6008 shndx
, static_cast<size_t>(fnoffset
));
6012 // We have to change the function so that it calls
6013 // __morestack_non_split instead of __morestack. The former will
6014 // allocate additional stack space.
6015 *from
= "__morestack";
6016 *to
= "__morestack_non_split";
6019 // The selector for x86_64 object files. Note this is never instantiated
6020 // directly. It's only used in Target_selector_x86_64_nacl, below.
6023 class Target_selector_x86_64
: public Target_selector_freebsd
6026 Target_selector_x86_64()
6027 : Target_selector_freebsd(elfcpp::EM_X86_64
, size
, false,
6029 ? "elf64-x86-64" : "elf32-x86-64"),
6031 ? "elf64-x86-64-freebsd"
6032 : "elf32-x86-64-freebsd"),
6033 (size
== 64 ? "elf_x86_64" : "elf32_x86_64"))
6037 do_instantiate_target()
6038 { return new Target_x86_64
<size
>(); }
6042 // NaCl variant. It uses different PLT contents.
6045 class Output_data_plt_x86_64_nacl
: public Output_data_plt_x86_64
<size
>
6048 Output_data_plt_x86_64_nacl(Layout
* layout
,
6049 Output_data_got
<64, false>* got
,
6050 Output_data_got_plt_x86_64
* got_plt
,
6051 Output_data_space
* got_irelative
)
6052 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
6053 got
, got_plt
, got_irelative
)
6056 Output_data_plt_x86_64_nacl(Layout
* layout
,
6057 Output_data_got
<64, false>* got
,
6058 Output_data_got_plt_x86_64
* got_plt
,
6059 Output_data_space
* got_irelative
,
6060 unsigned int plt_count
)
6061 : Output_data_plt_x86_64
<size
>(layout
, plt_entry_size
,
6062 got
, got_plt
, got_irelative
,
6067 virtual unsigned int
6068 do_get_plt_entry_size() const
6069 { return plt_entry_size
; }
6072 do_add_eh_frame(Layout
* layout
)
6074 layout
->add_eh_frame_for_plt(this,
6075 this->plt_eh_frame_cie
,
6076 this->plt_eh_frame_cie_size
,
6078 plt_eh_frame_fde_size
);
6082 do_fill_first_plt_entry(unsigned char* pov
,
6083 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_addr
,
6084 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_addr
);
6086 virtual unsigned int
6087 do_fill_plt_entry(unsigned char* pov
,
6088 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
6089 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
6090 unsigned int got_offset
,
6091 unsigned int plt_offset
,
6092 unsigned int plt_index
);
6095 do_fill_tlsdesc_entry(unsigned char* pov
,
6096 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
6097 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
6098 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
6099 unsigned int tlsdesc_got_offset
,
6100 unsigned int plt_offset
);
6103 // The size of an entry in the PLT.
6104 static const int plt_entry_size
= 64;
6106 // The first entry in the PLT.
6107 static const unsigned char first_plt_entry
[plt_entry_size
];
6109 // Other entries in the PLT for an executable.
6110 static const unsigned char plt_entry
[plt_entry_size
];
6112 // The reserved TLSDESC entry in the PLT for an executable.
6113 static const unsigned char tlsdesc_plt_entry
[plt_entry_size
];
6115 // The .eh_frame unwind information for the PLT.
6116 static const int plt_eh_frame_fde_size
= 32;
6117 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
6121 class Target_x86_64_nacl
: public Target_x86_64
<size
>
6124 Target_x86_64_nacl()
6125 : Target_x86_64
<size
>(&x86_64_nacl_info
)
6128 virtual Output_data_plt_x86_64
<size
>*
6129 do_make_data_plt(Layout
* layout
,
6130 Output_data_got
<64, false>* got
,
6131 Output_data_got_plt_x86_64
* got_plt
,
6132 Output_data_space
* got_irelative
)
6134 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
6138 virtual Output_data_plt_x86_64
<size
>*
6139 do_make_data_plt(Layout
* layout
,
6140 Output_data_got
<64, false>* got
,
6141 Output_data_got_plt_x86_64
* got_plt
,
6142 Output_data_space
* got_irelative
,
6143 unsigned int plt_count
)
6145 return new Output_data_plt_x86_64_nacl
<size
>(layout
, got
, got_plt
,
6151 do_code_fill(section_size_type length
) const;
6154 static const Target::Target_info x86_64_nacl_info
;
6158 const Target::Target_info Target_x86_64_nacl
<64>::x86_64_nacl_info
=
6161 false, // is_big_endian
6162 elfcpp::EM_X86_64
, // machine_code
6163 false, // has_make_symbol
6164 false, // has_resolve
6165 true, // has_code_fill
6166 true, // is_default_stack_executable
6167 true, // can_icf_inline_merge_sections
6169 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
6170 0x20000, // default_text_segment_address
6171 0x10000, // abi_pagesize (overridable by -z max-page-size)
6172 0x10000, // common_pagesize (overridable by -z common-page-size)
6173 true, // isolate_execinstr
6174 0x10000000, // rosegment_gap
6175 elfcpp::SHN_UNDEF
, // small_common_shndx
6176 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
6177 0, // small_common_section_flags
6178 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
6179 NULL
, // attributes_section
6180 NULL
, // attributes_vendor
6181 "_start", // entry_symbol_name
6182 32, // hash_entry_size
6183 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
6187 const Target::Target_info Target_x86_64_nacl
<32>::x86_64_nacl_info
=
6190 false, // is_big_endian
6191 elfcpp::EM_X86_64
, // machine_code
6192 false, // has_make_symbol
6193 false, // has_resolve
6194 true, // has_code_fill
6195 true, // is_default_stack_executable
6196 true, // can_icf_inline_merge_sections
6198 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
6199 0x20000, // default_text_segment_address
6200 0x10000, // abi_pagesize (overridable by -z max-page-size)
6201 0x10000, // common_pagesize (overridable by -z common-page-size)
6202 true, // isolate_execinstr
6203 0x10000000, // rosegment_gap
6204 elfcpp::SHN_UNDEF
, // small_common_shndx
6205 elfcpp::SHN_X86_64_LCOMMON
, // large_common_shndx
6206 0, // small_common_section_flags
6207 elfcpp::SHF_X86_64_LARGE
, // large_common_section_flags
6208 NULL
, // attributes_section
6209 NULL
, // attributes_vendor
6210 "_start", // entry_symbol_name
6211 32, // hash_entry_size
6212 elfcpp::SHT_X86_64_UNWIND
, // unwind_section_type
6215 #define NACLMASK 0xe0 // 32-byte alignment mask.
6217 // The first entry in the PLT.
6221 Output_data_plt_x86_64_nacl
<size
>::first_plt_entry
[plt_entry_size
] =
6223 0xff, 0x35, // pushq contents of memory address
6224 0, 0, 0, 0, // replaced with address of .got + 8
6225 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11
6226 0, 0, 0, 0, // replaced with address of .got + 16
6227 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
6228 0x4d, 0x01, 0xfb, // add %r15, %r11
6229 0x41, 0xff, 0xe3, // jmpq *%r11
6231 // 9-byte nop sequence to pad out to the next 32-byte boundary.
6232 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
6234 // 32 bytes of nop to pad out to the standard size
6235 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6236 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6237 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6238 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6239 0x66, // excess data32 prefix
6245 Output_data_plt_x86_64_nacl
<size
>::do_fill_first_plt_entry(
6247 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
6248 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
)
6250 memcpy(pov
, first_plt_entry
, plt_entry_size
);
6251 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
6253 - (plt_address
+ 2 + 4)));
6254 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
6256 - (plt_address
+ 9 + 4)));
6259 // Subsequent entries in the PLT.
6263 Output_data_plt_x86_64_nacl
<size
>::plt_entry
[plt_entry_size
] =
6265 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11
6266 0, 0, 0, 0, // replaced with address of symbol in .got
6267 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
6268 0x4d, 0x01, 0xfb, // add %r15, %r11
6269 0x41, 0xff, 0xe3, // jmpq *%r11
6271 // 15-byte nop sequence to pad out to the next 32-byte boundary.
6272 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6273 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6275 // Lazy GOT entries point here (32-byte aligned).
6276 0x68, // pushq immediate
6277 0, 0, 0, 0, // replaced with index into relocation table
6278 0xe9, // jmp relative
6279 0, 0, 0, 0, // replaced with offset to start of .plt0
6281 // 22 bytes of nop to pad out to the standard size.
6282 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6283 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6284 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax)
6289 Output_data_plt_x86_64_nacl
<size
>::do_fill_plt_entry(
6291 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
6292 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
6293 unsigned int got_offset
,
6294 unsigned int plt_offset
,
6295 unsigned int plt_index
)
6297 memcpy(pov
, plt_entry
, plt_entry_size
);
6298 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 3,
6299 (got_address
+ got_offset
6300 - (plt_address
+ plt_offset
6303 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 33, plt_index
);
6304 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 38,
6305 - (plt_offset
+ 38 + 4));
6310 // The reserved TLSDESC entry in the PLT.
6314 Output_data_plt_x86_64_nacl
<size
>::tlsdesc_plt_entry
[plt_entry_size
] =
6316 0xff, 0x35, // pushq x(%rip)
6317 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8)
6318 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11
6319 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry
6320 0x41, 0x83, 0xe3, NACLMASK
, // and $-32, %r11d
6321 0x4d, 0x01, 0xfb, // add %r15, %r11
6322 0x41, 0xff, 0xe3, // jmpq *%r11
6324 // 41 bytes of nop to pad out to the standard size.
6325 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6326 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6327 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes
6328 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6329 0x66, 0x66, // excess data32 prefixes
6330 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
6335 Output_data_plt_x86_64_nacl
<size
>::do_fill_tlsdesc_entry(
6337 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_address
,
6338 typename
elfcpp::Elf_types
<size
>::Elf_Addr plt_address
,
6339 typename
elfcpp::Elf_types
<size
>::Elf_Addr got_base
,
6340 unsigned int tlsdesc_got_offset
,
6341 unsigned int plt_offset
)
6343 memcpy(pov
, tlsdesc_plt_entry
, plt_entry_size
);
6344 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
6346 - (plt_address
+ plt_offset
6348 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 9,
6350 + tlsdesc_got_offset
6351 - (plt_address
+ plt_offset
6355 // The .eh_frame unwind information for the PLT.
6359 Output_data_plt_x86_64_nacl
<size
>::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
6361 0, 0, 0, 0, // Replaced with offset to .plt.
6362 0, 0, 0, 0, // Replaced with size of .plt.
6363 0, // Augmentation size.
6364 elfcpp::DW_CFA_def_cfa_offset
, 16, // DW_CFA_def_cfa_offset: 16.
6365 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
6366 elfcpp::DW_CFA_def_cfa_offset
, 24, // DW_CFA_def_cfa_offset: 24.
6367 elfcpp::DW_CFA_advance_loc
+ 58, // Advance 58 to __PLT__ + 64.
6368 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
6369 13, // Block length.
6370 elfcpp::DW_OP_breg7
, 8, // Push %rsp + 8.
6371 elfcpp::DW_OP_breg16
, 0, // Push %rip.
6372 elfcpp::DW_OP_const1u
, 63, // Push 0x3f.
6373 elfcpp::DW_OP_and
, // & (%rip & 0x3f).
6374 elfcpp::DW_OP_const1u
, 37, // Push 0x25.
6375 elfcpp::DW_OP_ge
, // >= ((%rip & 0x3f) >= 0x25)
6376 elfcpp::DW_OP_lit3
, // Push 3.
6377 elfcpp::DW_OP_shl
, // << (((%rip & 0x3f) >= 0x25) << 3)
6378 elfcpp::DW_OP_plus
, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
6379 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
6383 // Return a string used to fill a code section with nops.
6384 // For NaCl, long NOPs are only valid if they do not cross
6385 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
6388 Target_x86_64_nacl
<size
>::do_code_fill(section_size_type length
) const
6390 return std::string(length
, static_cast<char>(0x90));
6393 // The selector for x86_64-nacl object files.
6396 class Target_selector_x86_64_nacl
6397 : public Target_selector_nacl
<Target_selector_x86_64
<size
>,
6398 Target_x86_64_nacl
<size
> >
6401 Target_selector_x86_64_nacl()
6402 : Target_selector_nacl
<Target_selector_x86_64
<size
>,
6403 Target_x86_64_nacl
<size
> >("x86-64",
6405 ? "elf64-x86-64-nacl"
6406 : "elf32-x86-64-nacl",
6409 : "elf32_x86_64_nacl")
6413 Target_selector_x86_64_nacl
<64> target_selector_x86_64
;
6414 Target_selector_x86_64_nacl
<32> target_selector_x32
;
6416 } // End anonymous namespace.