* incremental-dump.cc (dump_incremental_inputs): Print dynamic reloc
[external/binutils.git] / gold / x86_64.cc
1 // x86_64.cc -- x86_64 target support for gold.
2
3 // Copyright 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <cstring>
26
27 #include "elfcpp.h"
28 #include "parameters.h"
29 #include "reloc.h"
30 #include "x86_64.h"
31 #include "object.h"
32 #include "symtab.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "copy-relocs.h"
36 #include "target.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
39 #include "tls.h"
40 #include "freebsd.h"
41 #include "gc.h"
42 #include "icf.h"
43
44 namespace
45 {
46
47 using namespace gold;
48
49 // A class to handle the PLT data.
50
51 class Output_data_plt_x86_64 : public Output_section_data
52 {
53  public:
54   typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
55
56   Output_data_plt_x86_64(Symbol_table* symtab, Layout* layout,
57                          Output_data_got<64, false>* got,
58                          Output_data_space* got_plt)
59     : Output_section_data(8), tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
60       count_(0), tlsdesc_got_offset_(-1U), free_list_()
61   { this->init(symtab, layout); }
62
63   Output_data_plt_x86_64(Symbol_table* symtab, Layout* layout,
64                          Output_data_got<64, false>* got,
65                          Output_data_space* got_plt,
66                          unsigned int plt_count)
67     : Output_section_data((plt_count + 1) * plt_entry_size, 8, false),
68       tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
69       count_(plt_count), tlsdesc_got_offset_(-1U), free_list_()
70   {
71     this->init(symtab, layout);
72
73     // Initialize the free list and reserve the first entry.
74     this->free_list_.init((plt_count + 1) * plt_entry_size, false);
75     this->free_list_.remove(0, plt_entry_size);
76   }
77
78   // Initialize the PLT section.
79   void
80   init(Symbol_table* symtab, Layout* layout);
81
82   // Add an entry to the PLT.
83   void
84   add_entry(Symbol* gsym);
85
86   // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
87   unsigned int
88   add_local_ifunc_entry(Sized_relobj_file<64, false>* relobj,
89                         unsigned int local_sym_index);
90
91   // Add the relocation for a PLT entry.
92   void
93   add_relocation(Symbol* gsym, unsigned int got_offset);
94
95   // Add the reserved TLSDESC_PLT entry to the PLT.
96   void
97   reserve_tlsdesc_entry(unsigned int got_offset)
98   { this->tlsdesc_got_offset_ = got_offset; }
99
100   // Return true if a TLSDESC_PLT entry has been reserved.
101   bool
102   has_tlsdesc_entry() const
103   { return this->tlsdesc_got_offset_ != -1U; }
104
105   // Return the GOT offset for the reserved TLSDESC_PLT entry.
106   unsigned int
107   get_tlsdesc_got_offset() const
108   { return this->tlsdesc_got_offset_; }
109
110   // Return the offset of the reserved TLSDESC_PLT entry.
111   unsigned int
112   get_tlsdesc_plt_offset() const
113   { return (this->count_ + 1) * plt_entry_size; }
114
115   // Return the .rela.plt section data.
116   Reloc_section*
117   rela_plt()
118   { return this->rel_; }
119
120   // Return where the TLSDESC relocations should go.
121   Reloc_section*
122   rela_tlsdesc(Layout*);
123
124   // Return the number of PLT entries.
125   unsigned int
126   entry_count() const
127   { return this->count_; }
128
129   // Return the offset of the first non-reserved PLT entry.
130   static unsigned int
131   first_plt_entry_offset()
132   { return plt_entry_size; }
133
134   // Return the size of a PLT entry.
135   static unsigned int
136   get_plt_entry_size()
137   { return plt_entry_size; }
138
139   // Reserve a slot in the PLT for an existing symbol in an incremental update.
140   void
141   reserve_slot(unsigned int plt_index)
142   {
143     this->free_list_.remove((plt_index + 1) * plt_entry_size,
144                             (plt_index + 2) * plt_entry_size);
145   }
146
147  protected:
148   void
149   do_adjust_output_section(Output_section* os);
150
151   // Write to a map file.
152   void
153   do_print_to_mapfile(Mapfile* mapfile) const
154   { mapfile->print_output_data(this, _("** PLT")); }
155
156  private:
157   // The size of an entry in the PLT.
158   static const int plt_entry_size = 16;
159
160   // The first entry in the PLT.
161   // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
162   // procedure linkage table for both programs and shared objects."
163   static unsigned char first_plt_entry[plt_entry_size];
164
165   // Other entries in the PLT for an executable.
166   static unsigned char plt_entry[plt_entry_size];
167
168   // The reserved TLSDESC entry in the PLT for an executable.
169   static unsigned char tlsdesc_plt_entry[plt_entry_size];
170
171   // Set the final size.
172   void
173   set_final_data_size();
174
175   // Write out the PLT data.
176   void
177   do_write(Output_file*);
178
179   // The reloc section.
180   Reloc_section* rel_;
181   // The TLSDESC relocs, if necessary.  These must follow the regular
182   // PLT relocs.
183   Reloc_section* tlsdesc_rel_;
184   // The .got section.
185   Output_data_got<64, false>* got_;
186   // The .got.plt section.
187   Output_data_space* got_plt_;
188   // The number of PLT entries.
189   unsigned int count_;
190   // Offset of the reserved TLSDESC_GOT entry when needed.
191   unsigned int tlsdesc_got_offset_;
192   // List of available regions within the section, for incremental
193   // update links.
194   Free_list free_list_;
195 };
196
197 // The x86_64 target class.
198 // See the ABI at
199 //   http://www.x86-64.org/documentation/abi.pdf
200 // TLS info comes from
201 //   http://people.redhat.com/drepper/tls.pdf
202 //   http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
203
204 class Target_x86_64 : public Target_freebsd<64, false>
205 {
206  public:
207   // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
208   // uses only Elf64_Rela relocation entries with explicit addends."
209   typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
210
211   Target_x86_64()
212     : Target_freebsd<64, false>(&x86_64_info),
213       got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
214       global_offset_table_(NULL), rela_dyn_(NULL),
215       copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
216       got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
217       tls_base_symbol_defined_(false)
218   { }
219
220   // This function should be defined in targets that can use relocation
221   // types to determine (implemented in local_reloc_may_be_function_pointer
222   // and global_reloc_may_be_function_pointer)
223   // if a function's pointer is taken.  ICF uses this in safe mode to only
224   // fold those functions whose pointer is defintely not taken.  For x86_64
225   // pie binaries, safe ICF cannot be done by looking at relocation types.
226   inline bool
227   can_check_for_function_pointers() const
228   { return !parameters->options().pie(); }
229
230   virtual bool
231   can_icf_inline_merge_sections () const
232   { return true; }
233
234   // Hook for a new output section.
235   void
236   do_new_output_section(Output_section*) const;
237
238   // Scan the relocations to look for symbol adjustments.
239   void
240   gc_process_relocs(Symbol_table* symtab,
241                     Layout* layout,
242                     Sized_relobj_file<64, false>* object,
243                     unsigned int data_shndx,
244                     unsigned int sh_type,
245                     const unsigned char* prelocs,
246                     size_t reloc_count,
247                     Output_section* output_section,
248                     bool needs_special_offset_handling,
249                     size_t local_symbol_count,
250                     const unsigned char* plocal_symbols);
251
252   // Scan the relocations to look for symbol adjustments.
253   void
254   scan_relocs(Symbol_table* symtab,
255               Layout* layout,
256               Sized_relobj_file<64, false>* object,
257               unsigned int data_shndx,
258               unsigned int sh_type,
259               const unsigned char* prelocs,
260               size_t reloc_count,
261               Output_section* output_section,
262               bool needs_special_offset_handling,
263               size_t local_symbol_count,
264               const unsigned char* plocal_symbols);
265
266   // Finalize the sections.
267   void
268   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
269
270   // Return the value to use for a dynamic which requires special
271   // treatment.
272   uint64_t
273   do_dynsym_value(const Symbol*) const;
274
275   // Relocate a section.
276   void
277   relocate_section(const Relocate_info<64, false>*,
278                    unsigned int sh_type,
279                    const unsigned char* prelocs,
280                    size_t reloc_count,
281                    Output_section* output_section,
282                    bool needs_special_offset_handling,
283                    unsigned char* view,
284                    elfcpp::Elf_types<64>::Elf_Addr view_address,
285                    section_size_type view_size,
286                    const Reloc_symbol_changes*);
287
288   // Scan the relocs during a relocatable link.
289   void
290   scan_relocatable_relocs(Symbol_table* symtab,
291                           Layout* layout,
292                           Sized_relobj_file<64, false>* object,
293                           unsigned int data_shndx,
294                           unsigned int sh_type,
295                           const unsigned char* prelocs,
296                           size_t reloc_count,
297                           Output_section* output_section,
298                           bool needs_special_offset_handling,
299                           size_t local_symbol_count,
300                           const unsigned char* plocal_symbols,
301                           Relocatable_relocs*);
302
303   // Relocate a section during a relocatable link.
304   void
305   relocate_for_relocatable(const Relocate_info<64, false>*,
306                            unsigned int sh_type,
307                            const unsigned char* prelocs,
308                            size_t reloc_count,
309                            Output_section* output_section,
310                            off_t offset_in_output_section,
311                            const Relocatable_relocs*,
312                            unsigned char* view,
313                            elfcpp::Elf_types<64>::Elf_Addr view_address,
314                            section_size_type view_size,
315                            unsigned char* reloc_view,
316                            section_size_type reloc_view_size);
317
318   // Return a string used to fill a code section with nops.
319   std::string
320   do_code_fill(section_size_type length) const;
321
322   // Return whether SYM is defined by the ABI.
323   bool
324   do_is_defined_by_abi(const Symbol* sym) const
325   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
326
327   // Return the symbol index to use for a target specific relocation.
328   // The only target specific relocation is R_X86_64_TLSDESC for a
329   // local symbol, which is an absolute reloc.
330   unsigned int
331   do_reloc_symbol_index(void*, unsigned int r_type) const
332   {
333     gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
334     return 0;
335   }
336
337   // Return the addend to use for a target specific relocation.
338   uint64_t
339   do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
340
341   // Return the PLT section.
342   Output_data*
343   do_plt_section_for_global(const Symbol*) const
344   { return this->plt_section(); }
345
346   Output_data*
347   do_plt_section_for_local(const Relobj*, unsigned int) const
348   { return this->plt_section(); }
349
350   // Adjust -fsplit-stack code which calls non-split-stack code.
351   void
352   do_calls_non_split(Relobj* object, unsigned int shndx,
353                      section_offset_type fnoffset, section_size_type fnsize,
354                      unsigned char* view, section_size_type view_size,
355                      std::string* from, std::string* to) const;
356
357   // Return the size of the GOT section.
358   section_size_type
359   got_size() const
360   {
361     gold_assert(this->got_ != NULL);
362     return this->got_->data_size();
363   }
364
365   // Return the number of entries in the GOT.
366   unsigned int
367   got_entry_count() const
368   {
369     if (this->got_ == NULL)
370       return 0;
371     return this->got_size() / 8;
372   }
373
374   // Return the number of entries in the PLT.
375   unsigned int
376   plt_entry_count() const;
377
378   // Return the offset of the first non-reserved PLT entry.
379   unsigned int
380   first_plt_entry_offset() const;
381
382   // Return the size of each PLT entry.
383   unsigned int
384   plt_entry_size() const;
385
386   // Create the GOT section for an incremental update.
387   Output_data_got<64, false>*
388   init_got_plt_for_update(Symbol_table* symtab,
389                           Layout* layout,
390                           unsigned int got_count,
391                           unsigned int plt_count);
392
393   // Reserve a GOT entry for a local symbol, and regenerate any
394   // necessary dynamic relocations.
395   void
396   reserve_local_got_entry(unsigned int got_index,
397                           Sized_relobj<64, false>* obj,
398                           unsigned int r_sym,
399                           unsigned int got_type);
400
401   // Reserve a GOT entry for a global symbol, and regenerate any
402   // necessary dynamic relocations.
403   void
404   reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
405                            unsigned int got_type);
406
407   // Register an existing PLT entry for a global symbol.
408   // A target needs to implement this to support incremental linking.
409   void
410   register_global_plt_entry(unsigned int plt_index, Symbol* gsym);
411
412   // Apply an incremental relocation.
413   void
414   apply_relocation(const Relocate_info<64, false>* relinfo,
415                    elfcpp::Elf_types<64>::Elf_Addr r_offset,
416                    unsigned int r_type,
417                    elfcpp::Elf_types<64>::Elf_Swxword r_addend,
418                    const Symbol* gsym,
419                    unsigned char* view,
420                    elfcpp::Elf_types<64>::Elf_Addr address,
421                    section_size_type view_size);
422
423   // Add a new reloc argument, returning the index in the vector.
424   size_t
425   add_tlsdesc_info(Sized_relobj_file<64, false>* object, unsigned int r_sym)
426   {
427     this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
428     return this->tlsdesc_reloc_info_.size() - 1;
429   }
430
431  private:
432   // The class which scans relocations.
433   class Scan
434   {
435   public:
436     Scan()
437       : issued_non_pic_error_(false)
438     { }
439
440     static inline int
441     get_reference_flags(unsigned int r_type);
442
443     inline void
444     local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
445           Sized_relobj_file<64, false>* object,
446           unsigned int data_shndx,
447           Output_section* output_section,
448           const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
449           const elfcpp::Sym<64, false>& lsym);
450
451     inline void
452     global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
453            Sized_relobj_file<64, false>* object,
454            unsigned int data_shndx,
455            Output_section* output_section,
456            const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
457            Symbol* gsym);
458
459     inline bool
460     local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
461                                         Target_x86_64* target,
462                                         Sized_relobj_file<64, false>* object,
463                                         unsigned int data_shndx,
464                                         Output_section* output_section,
465                                         const elfcpp::Rela<64, false>& reloc,
466                                         unsigned int r_type,
467                                         const elfcpp::Sym<64, false>& lsym);
468
469     inline bool
470     global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
471                                          Target_x86_64* target,
472                                          Sized_relobj_file<64, false>* object,
473                                          unsigned int data_shndx,
474                                          Output_section* output_section,
475                                          const elfcpp::Rela<64, false>& reloc,
476                                          unsigned int r_type,
477                                          Symbol* gsym);
478
479   private:
480     static void
481     unsupported_reloc_local(Sized_relobj_file<64, false>*, unsigned int r_type);
482
483     static void
484     unsupported_reloc_global(Sized_relobj_file<64, false>*, unsigned int r_type,
485                              Symbol*);
486
487     void
488     check_non_pic(Relobj*, unsigned int r_type);
489
490     inline bool
491     possible_function_pointer_reloc(unsigned int r_type);
492
493     bool
494     reloc_needs_plt_for_ifunc(Sized_relobj_file<64, false>*,
495                               unsigned int r_type);
496
497     // Whether we have issued an error about a non-PIC compilation.
498     bool issued_non_pic_error_;
499   };
500
501   // The class which implements relocation.
502   class Relocate
503   {
504    public:
505     Relocate()
506       : skip_call_tls_get_addr_(false)
507     { }
508
509     ~Relocate()
510     {
511       if (this->skip_call_tls_get_addr_)
512         {
513           // FIXME: This needs to specify the location somehow.
514           gold_error(_("missing expected TLS relocation"));
515         }
516     }
517
518     // Do a relocation.  Return false if the caller should not issue
519     // any warnings about this relocation.
520     inline bool
521     relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
522              size_t relnum, const elfcpp::Rela<64, false>&,
523              unsigned int r_type, const Sized_symbol<64>*,
524              const Symbol_value<64>*,
525              unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
526              section_size_type);
527
528    private:
529     // Do a TLS relocation.
530     inline void
531     relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
532                  size_t relnum, const elfcpp::Rela<64, false>&,
533                  unsigned int r_type, const Sized_symbol<64>*,
534                  const Symbol_value<64>*,
535                  unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
536                  section_size_type);
537
538     // Do a TLS General-Dynamic to Initial-Exec transition.
539     inline void
540     tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
541                  Output_segment* tls_segment,
542                  const elfcpp::Rela<64, false>&, unsigned int r_type,
543                  elfcpp::Elf_types<64>::Elf_Addr value,
544                  unsigned char* view,
545                  elfcpp::Elf_types<64>::Elf_Addr,
546                  section_size_type view_size);
547
548     // Do a TLS General-Dynamic to Local-Exec transition.
549     inline void
550     tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
551                  Output_segment* tls_segment,
552                  const elfcpp::Rela<64, false>&, unsigned int r_type,
553                  elfcpp::Elf_types<64>::Elf_Addr value,
554                  unsigned char* view,
555                  section_size_type view_size);
556
557     // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
558     inline void
559     tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
560                       Output_segment* tls_segment,
561                       const elfcpp::Rela<64, false>&, unsigned int r_type,
562                       elfcpp::Elf_types<64>::Elf_Addr value,
563                       unsigned char* view,
564                       elfcpp::Elf_types<64>::Elf_Addr,
565                       section_size_type view_size);
566
567     // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
568     inline void
569     tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
570                       Output_segment* tls_segment,
571                       const elfcpp::Rela<64, false>&, unsigned int r_type,
572                       elfcpp::Elf_types<64>::Elf_Addr value,
573                       unsigned char* view,
574                       section_size_type view_size);
575
576     // Do a TLS Local-Dynamic to Local-Exec transition.
577     inline void
578     tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
579                  Output_segment* tls_segment,
580                  const elfcpp::Rela<64, false>&, unsigned int r_type,
581                  elfcpp::Elf_types<64>::Elf_Addr value,
582                  unsigned char* view,
583                  section_size_type view_size);
584
585     // Do a TLS Initial-Exec to Local-Exec transition.
586     static inline void
587     tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
588                  Output_segment* tls_segment,
589                  const elfcpp::Rela<64, false>&, unsigned int r_type,
590                  elfcpp::Elf_types<64>::Elf_Addr value,
591                  unsigned char* view,
592                  section_size_type view_size);
593
594     // This is set if we should skip the next reloc, which should be a
595     // PLT32 reloc against ___tls_get_addr.
596     bool skip_call_tls_get_addr_;
597   };
598
599   // A class which returns the size required for a relocation type,
600   // used while scanning relocs during a relocatable link.
601   class Relocatable_size_for_reloc
602   {
603    public:
604     unsigned int
605     get_size_for_reloc(unsigned int, Relobj*);
606   };
607
608   // Adjust TLS relocation type based on the options and whether this
609   // is a local symbol.
610   static tls::Tls_optimization
611   optimize_tls_reloc(bool is_final, int r_type);
612
613   // Get the GOT section, creating it if necessary.
614   Output_data_got<64, false>*
615   got_section(Symbol_table*, Layout*);
616
617   // Get the GOT PLT section.
618   Output_data_space*
619   got_plt_section() const
620   {
621     gold_assert(this->got_plt_ != NULL);
622     return this->got_plt_;
623   }
624
625   // Get the GOT section for TLSDESC entries.
626   Output_data_got<64, false>*
627   got_tlsdesc_section() const
628   {
629     gold_assert(this->got_tlsdesc_ != NULL);
630     return this->got_tlsdesc_;
631   }
632
633   // Create the PLT section.
634   void
635   make_plt_section(Symbol_table* symtab, Layout* layout);
636
637   // Create a PLT entry for a global symbol.
638   void
639   make_plt_entry(Symbol_table*, Layout*, Symbol*);
640
641   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
642   void
643   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
644                              Sized_relobj_file<64, false>* relobj,
645                              unsigned int local_sym_index);
646
647   // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
648   void
649   define_tls_base_symbol(Symbol_table*, Layout*);
650
651   // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
652   void
653   reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
654
655   // Create a GOT entry for the TLS module index.
656   unsigned int
657   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
658                       Sized_relobj_file<64, false>* object);
659
660   // Get the PLT section.
661   Output_data_plt_x86_64*
662   plt_section() const
663   {
664     gold_assert(this->plt_ != NULL);
665     return this->plt_;
666   }
667
668   // Get the dynamic reloc section, creating it if necessary.
669   Reloc_section*
670   rela_dyn_section(Layout*);
671
672   // Get the section to use for TLSDESC relocations.
673   Reloc_section*
674   rela_tlsdesc_section(Layout*) const;
675
676   // Add a potential copy relocation.
677   void
678   copy_reloc(Symbol_table* symtab, Layout* layout,
679              Sized_relobj_file<64, false>* object,
680              unsigned int shndx, Output_section* output_section,
681              Symbol* sym, const elfcpp::Rela<64, false>& reloc)
682   {
683     this->copy_relocs_.copy_reloc(symtab, layout,
684                                   symtab->get_sized_symbol<64>(sym),
685                                   object, shndx, output_section,
686                                   reloc, this->rela_dyn_section(layout));
687   }
688
689   // Information about this specific target which we pass to the
690   // general Target structure.
691   static const Target::Target_info x86_64_info;
692
693   // The types of GOT entries needed for this platform.
694   // These values are exposed to the ABI in an incremental link.
695   // Do not renumber existing values without changing the version
696   // number of the .gnu_incremental_inputs section.
697   enum Got_type
698   {
699     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
700     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
701     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
702     GOT_TYPE_TLS_DESC = 3       // GOT entry for TLS_DESC pair
703   };
704
705   // This type is used as the argument to the target specific
706   // relocation routines.  The only target specific reloc is
707   // R_X86_64_TLSDESC against a local symbol.
708   struct Tlsdesc_info
709   {
710     Tlsdesc_info(Sized_relobj_file<64, false>* a_object, unsigned int a_r_sym)
711       : object(a_object), r_sym(a_r_sym)
712     { }
713
714     // The object in which the local symbol is defined.
715     Sized_relobj_file<64, false>* object;
716     // The local symbol index in the object.
717     unsigned int r_sym;
718   };
719
720   // The GOT section.
721   Output_data_got<64, false>* got_;
722   // The PLT section.
723   Output_data_plt_x86_64* plt_;
724   // The GOT PLT section.
725   Output_data_space* got_plt_;
726   // The GOT section for TLSDESC relocations.
727   Output_data_got<64, false>* got_tlsdesc_;
728   // The _GLOBAL_OFFSET_TABLE_ symbol.
729   Symbol* global_offset_table_;
730   // The dynamic reloc section.
731   Reloc_section* rela_dyn_;
732   // Relocs saved to avoid a COPY reloc.
733   Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
734   // Space for variables copied with a COPY reloc.
735   Output_data_space* dynbss_;
736   // Offset of the GOT entry for the TLS module index.
737   unsigned int got_mod_index_offset_;
738   // We handle R_X86_64_TLSDESC against a local symbol as a target
739   // specific relocation.  Here we store the object and local symbol
740   // index for the relocation.
741   std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
742   // True if the _TLS_MODULE_BASE_ symbol has been defined.
743   bool tls_base_symbol_defined_;
744 };
745
746 const Target::Target_info Target_x86_64::x86_64_info =
747 {
748   64,                   // size
749   false,                // is_big_endian
750   elfcpp::EM_X86_64,    // machine_code
751   false,                // has_make_symbol
752   false,                // has_resolve
753   true,                 // has_code_fill
754   true,                 // is_default_stack_executable
755   '\0',                 // wrap_char
756   "/lib/ld64.so.1",     // program interpreter
757   0x400000,             // default_text_segment_address
758   0x1000,               // abi_pagesize (overridable by -z max-page-size)
759   0x1000,               // common_pagesize (overridable by -z common-page-size)
760   elfcpp::SHN_UNDEF,    // small_common_shndx
761   elfcpp::SHN_X86_64_LCOMMON,   // large_common_shndx
762   0,                    // small_common_section_flags
763   elfcpp::SHF_X86_64_LARGE,     // large_common_section_flags
764   NULL,                 // attributes_section
765   NULL                  // attributes_vendor
766 };
767
768 // This is called when a new output section is created.  This is where
769 // we handle the SHF_X86_64_LARGE.
770
771 void
772 Target_x86_64::do_new_output_section(Output_section* os) const
773 {
774   if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
775     os->set_is_large_section();
776 }
777
778 // Get the GOT section, creating it if necessary.
779
780 Output_data_got<64, false>*
781 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
782 {
783   if (this->got_ == NULL)
784     {
785       gold_assert(symtab != NULL && layout != NULL);
786
787       this->got_ = new Output_data_got<64, false>();
788
789       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
790                                       (elfcpp::SHF_ALLOC
791                                        | elfcpp::SHF_WRITE),
792                                       this->got_, ORDER_RELRO_LAST,
793                                       true);
794
795       this->got_plt_ = new Output_data_space(8, "** GOT PLT");
796       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
797                                       (elfcpp::SHF_ALLOC
798                                        | elfcpp::SHF_WRITE),
799                                       this->got_plt_, ORDER_NON_RELRO_FIRST,
800                                       false);
801
802       // The first three entries are reserved.
803       this->got_plt_->set_current_data_size(3 * 8);
804
805       // Those bytes can go into the relro segment.
806       layout->increase_relro(3 * 8);
807
808       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
809       this->global_offset_table_ =
810         symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
811                                       Symbol_table::PREDEFINED,
812                                       this->got_plt_,
813                                       0, 0, elfcpp::STT_OBJECT,
814                                       elfcpp::STB_LOCAL,
815                                       elfcpp::STV_HIDDEN, 0,
816                                       false, false);
817
818       // If there are any TLSDESC relocations, they get GOT entries in
819       // .got.plt after the jump slot entries.
820       this->got_tlsdesc_ = new Output_data_got<64, false>();
821       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
822                                       (elfcpp::SHF_ALLOC
823                                        | elfcpp::SHF_WRITE),
824                                       this->got_tlsdesc_,
825                                       ORDER_NON_RELRO_FIRST, false);
826     }
827
828   return this->got_;
829 }
830
831 // Get the dynamic reloc section, creating it if necessary.
832
833 Target_x86_64::Reloc_section*
834 Target_x86_64::rela_dyn_section(Layout* layout)
835 {
836   if (this->rela_dyn_ == NULL)
837     {
838       gold_assert(layout != NULL);
839       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
840       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
841                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
842                                       ORDER_DYNAMIC_RELOCS, false);
843     }
844   return this->rela_dyn_;
845 }
846
847 // Initialize the PLT section.
848
849 void
850 Output_data_plt_x86_64::init(Symbol_table* symtab, Layout* layout)
851 {
852   this->rel_ = new Reloc_section(false);
853   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
854                                   elfcpp::SHF_ALLOC, this->rel_,
855                                   ORDER_DYNAMIC_PLT_RELOCS, false);
856
857   if (parameters->doing_static_link())
858     {
859       // A statically linked executable will only have a .rela.plt
860       // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
861       // symbols.  The library will use these symbols to locate the
862       // IRELATIVE relocs at program startup time.
863       symtab->define_in_output_data("__rela_iplt_start", NULL,
864                                     Symbol_table::PREDEFINED,
865                                     this->rel_, 0, 0, elfcpp::STT_NOTYPE,
866                                     elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
867                                     0, false, true);
868       symtab->define_in_output_data("__rela_iplt_end", NULL,
869                                     Symbol_table::PREDEFINED,
870                                     this->rel_, 0, 0, elfcpp::STT_NOTYPE,
871                                     elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
872                                     0, true, true);
873     }
874 }
875
876 void
877 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
878 {
879   os->set_entsize(plt_entry_size);
880 }
881
882 // Add an entry to the PLT.
883
884 void
885 Output_data_plt_x86_64::add_entry(Symbol* gsym)
886 {
887   gold_assert(!gsym->has_plt_offset());
888
889   unsigned int plt_index;
890   off_t plt_offset;
891   section_offset_type got_offset;
892
893   if (!this->is_data_size_valid())
894     {
895       // Note that when setting the PLT offset we skip the initial
896       // reserved PLT entry.
897       plt_index = this->count_ + 1;
898       plt_offset = plt_index * plt_entry_size;
899
900       ++this->count_;
901
902       got_offset = (plt_index - 1 + 3) * 8;
903       gold_assert(got_offset == this->got_plt_->current_data_size());
904
905       // Every PLT entry needs a GOT entry which points back to the PLT
906       // entry (this will be changed by the dynamic linker, normally
907       // lazily when the function is called).
908       this->got_plt_->set_current_data_size(got_offset + 8);
909     }
910   else
911     {
912       // For incremental updates, find an available slot.
913       plt_offset = this->free_list_.allocate(plt_entry_size, plt_entry_size, 0);
914       if (plt_offset == -1)
915         gold_fatal(_("out of patch space (PLT);"
916                      " relink with --incremental-full"));
917
918       // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
919       // can be calculated from the PLT index, adjusting for the three
920       // reserved entries at the beginning of the GOT.
921       plt_index = plt_offset / plt_entry_size - 1;
922       got_offset = (plt_index - 1 + 3) * 8;
923     }
924
925   gsym->set_plt_offset(plt_offset);
926
927   // Every PLT entry needs a reloc.
928   this->add_relocation(gsym, got_offset);
929
930   // Note that we don't need to save the symbol.  The contents of the
931   // PLT are independent of which symbols are used.  The symbols only
932   // appear in the relocations.
933 }
934
935 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.  Return
936 // the PLT offset.
937
938 unsigned int
939 Output_data_plt_x86_64::add_local_ifunc_entry(
940     Sized_relobj_file<64, false>* relobj,
941     unsigned int local_sym_index)
942 {
943   unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
944   ++this->count_;
945
946   section_offset_type got_offset = this->got_plt_->current_data_size();
947
948   // Every PLT entry needs a GOT entry which points back to the PLT
949   // entry.
950   this->got_plt_->set_current_data_size(got_offset + 8);
951
952   // Every PLT entry needs a reloc.
953   this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
954                                           elfcpp::R_X86_64_IRELATIVE,
955                                           this->got_plt_, got_offset, 0);
956
957   return plt_offset;
958 }
959
960 // Add the relocation for a PLT entry.
961
962 void
963 Output_data_plt_x86_64::add_relocation(Symbol* gsym, unsigned int got_offset)
964 {
965   if (gsym->type() == elfcpp::STT_GNU_IFUNC
966       && gsym->can_use_relative_reloc(false))
967     this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
968                                              this->got_plt_, got_offset, 0);
969   else
970     {
971       gsym->set_needs_dynsym_entry();
972       this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
973                              got_offset, 0);
974     }
975 }
976
977 // Return where the TLSDESC relocations should go, creating it if
978 // necessary.  These follow the JUMP_SLOT relocations.
979
980 Output_data_plt_x86_64::Reloc_section*
981 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
982 {
983   if (this->tlsdesc_rel_ == NULL)
984     {
985       this->tlsdesc_rel_ = new Reloc_section(false);
986       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
987                                       elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
988                                       ORDER_DYNAMIC_PLT_RELOCS, false);
989       gold_assert(this->tlsdesc_rel_->output_section() ==
990                   this->rel_->output_section());
991     }
992   return this->tlsdesc_rel_;
993 }
994
995 // Set the final size.
996 void
997 Output_data_plt_x86_64::set_final_data_size()
998 {
999   unsigned int count = this->count_;
1000   if (this->has_tlsdesc_entry())
1001     ++count;
1002   this->set_data_size((count + 1) * plt_entry_size);
1003 }
1004
1005 // The first entry in the PLT for an executable.
1006
1007 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
1008 {
1009   // From AMD64 ABI Draft 0.98, page 76
1010   0xff, 0x35,   // pushq contents of memory address
1011   0, 0, 0, 0,   // replaced with address of .got + 8
1012   0xff, 0x25,   // jmp indirect
1013   0, 0, 0, 0,   // replaced with address of .got + 16
1014   0x90, 0x90, 0x90, 0x90   // noop (x4)
1015 };
1016
1017 // Subsequent entries in the PLT for an executable.
1018
1019 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
1020 {
1021   // From AMD64 ABI Draft 0.98, page 76
1022   0xff, 0x25,   // jmpq indirect
1023   0, 0, 0, 0,   // replaced with address of symbol in .got
1024   0x68,         // pushq immediate
1025   0, 0, 0, 0,   // replaced with offset into relocation table
1026   0xe9,         // jmpq relative
1027   0, 0, 0, 0    // replaced with offset to start of .plt
1028 };
1029
1030 // The reserved TLSDESC entry in the PLT for an executable.
1031
1032 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
1033 {
1034   // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
1035   // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
1036   0xff, 0x35,   // pushq x(%rip)
1037   0, 0, 0, 0,   // replaced with address of linkmap GOT entry (at PLTGOT + 8)
1038   0xff, 0x25,   // jmpq *y(%rip)
1039   0, 0, 0, 0,   // replaced with offset of reserved TLSDESC_GOT entry
1040   0x0f, 0x1f,   // nop
1041   0x40, 0
1042 };
1043
1044 // Write out the PLT.  This uses the hand-coded instructions above,
1045 // and adjusts them as needed.  This is specified by the AMD64 ABI.
1046
1047 void
1048 Output_data_plt_x86_64::do_write(Output_file* of)
1049 {
1050   const off_t offset = this->offset();
1051   const section_size_type oview_size =
1052     convert_to_section_size_type(this->data_size());
1053   unsigned char* const oview = of->get_output_view(offset, oview_size);
1054
1055   const off_t got_file_offset = this->got_plt_->offset();
1056   const section_size_type got_size =
1057     convert_to_section_size_type(this->got_plt_->data_size());
1058   unsigned char* const got_view = of->get_output_view(got_file_offset,
1059                                                       got_size);
1060
1061   unsigned char* pov = oview;
1062
1063   // The base address of the .plt section.
1064   elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
1065   // The base address of the .got section.
1066   elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
1067   // The base address of the PLT portion of the .got section,
1068   // which is where the GOT pointer will point, and where the
1069   // three reserved GOT entries are located.
1070   elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
1071
1072   memcpy(pov, first_plt_entry, plt_entry_size);
1073   // We do a jmp relative to the PC at the end of this instruction.
1074   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1075                                               (got_address + 8
1076                                                - (plt_address + 6)));
1077   elfcpp::Swap<32, false>::writeval(pov + 8,
1078                                     (got_address + 16
1079                                      - (plt_address + 12)));
1080   pov += plt_entry_size;
1081
1082   unsigned char* got_pov = got_view;
1083
1084   memset(got_pov, 0, 24);
1085   got_pov += 24;
1086
1087   unsigned int plt_offset = plt_entry_size;
1088   unsigned int got_offset = 24;
1089   const unsigned int count = this->count_;
1090   for (unsigned int plt_index = 0;
1091        plt_index < count;
1092        ++plt_index,
1093          pov += plt_entry_size,
1094          got_pov += 8,
1095          plt_offset += plt_entry_size,
1096          got_offset += 8)
1097     {
1098       // Set and adjust the PLT entry itself.
1099       memcpy(pov, plt_entry, plt_entry_size);
1100       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1101                                                   (got_address + got_offset
1102                                                    - (plt_address + plt_offset
1103                                                       + 6)));
1104
1105       elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
1106       elfcpp::Swap<32, false>::writeval(pov + 12,
1107                                         - (plt_offset + plt_entry_size));
1108
1109       // Set the entry in the GOT.
1110       elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1111     }
1112
1113   if (this->has_tlsdesc_entry())
1114     {
1115       // Set and adjust the reserved TLSDESC PLT entry.
1116       unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1117       memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1118       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1119                                                   (got_address + 8
1120                                                    - (plt_address + plt_offset
1121                                                       + 6)));
1122       elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1123                                                   (got_base
1124                                                    + tlsdesc_got_offset
1125                                                    - (plt_address + plt_offset
1126                                                       + 12)));
1127       pov += plt_entry_size;
1128     }
1129
1130   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1131   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1132
1133   of->write_output_view(offset, oview_size, oview);
1134   of->write_output_view(got_file_offset, got_size, got_view);
1135 }
1136
1137 // Create the PLT section.
1138
1139 void
1140 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
1141 {
1142   if (this->plt_ == NULL)
1143     {
1144       // Create the GOT sections first.
1145       this->got_section(symtab, layout);
1146
1147       this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1148                                               this->got_plt_);
1149       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1150                                       (elfcpp::SHF_ALLOC
1151                                        | elfcpp::SHF_EXECINSTR),
1152                                       this->plt_, ORDER_PLT, false);
1153
1154       // Make the sh_info field of .rela.plt point to .plt.
1155       Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1156       rela_plt_os->set_info_section(this->plt_->output_section());
1157     }
1158 }
1159
1160 // Return the section for TLSDESC relocations.
1161
1162 Target_x86_64::Reloc_section*
1163 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1164 {
1165   return this->plt_section()->rela_tlsdesc(layout);
1166 }
1167
1168 // Create a PLT entry for a global symbol.
1169
1170 void
1171 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
1172                               Symbol* gsym)
1173 {
1174   if (gsym->has_plt_offset())
1175     return;
1176
1177   if (this->plt_ == NULL)
1178     this->make_plt_section(symtab, layout);
1179
1180   this->plt_->add_entry(gsym);
1181 }
1182
1183 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1184
1185 void
1186 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1187                                           Sized_relobj_file<64, false>* relobj,
1188                                           unsigned int local_sym_index)
1189 {
1190   if (relobj->local_has_plt_offset(local_sym_index))
1191     return;
1192   if (this->plt_ == NULL)
1193     this->make_plt_section(symtab, layout);
1194   unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1195                                                               local_sym_index);
1196   relobj->set_local_plt_offset(local_sym_index, plt_offset);
1197 }
1198
1199 // Return the number of entries in the PLT.
1200
1201 unsigned int
1202 Target_x86_64::plt_entry_count() const
1203 {
1204   if (this->plt_ == NULL)
1205     return 0;
1206   return this->plt_->entry_count();
1207 }
1208
1209 // Return the offset of the first non-reserved PLT entry.
1210
1211 unsigned int
1212 Target_x86_64::first_plt_entry_offset() const
1213 {
1214   return Output_data_plt_x86_64::first_plt_entry_offset();
1215 }
1216
1217 // Return the size of each PLT entry.
1218
1219 unsigned int
1220 Target_x86_64::plt_entry_size() const
1221 {
1222   return Output_data_plt_x86_64::get_plt_entry_size();
1223 }
1224
1225 // Create the GOT and PLT sections for an incremental update.
1226
1227 Output_data_got<64, false>*
1228 Target_x86_64::init_got_plt_for_update(Symbol_table* symtab,
1229                                        Layout* layout,
1230                                        unsigned int got_count,
1231                                        unsigned int plt_count)
1232 {
1233   gold_assert(this->got_ == NULL);
1234
1235   this->got_ = new Output_data_got<64, false>(got_count * 8);
1236   layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1237                                   (elfcpp::SHF_ALLOC
1238                                    | elfcpp::SHF_WRITE),
1239                                   this->got_, ORDER_RELRO_LAST,
1240                                   true);
1241
1242   // Add the three reserved entries.
1243   this->got_plt_ = new Output_data_space((plt_count + 3) * 8, 8, "** GOT PLT");
1244   layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1245                                   (elfcpp::SHF_ALLOC
1246                                    | elfcpp::SHF_WRITE),
1247                                   this->got_plt_, ORDER_NON_RELRO_FIRST,
1248                                   false);
1249
1250   // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1251   this->global_offset_table_ =
1252     symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1253                                   Symbol_table::PREDEFINED,
1254                                   this->got_plt_,
1255                                   0, 0, elfcpp::STT_OBJECT,
1256                                   elfcpp::STB_LOCAL,
1257                                   elfcpp::STV_HIDDEN, 0,
1258                                   false, false);
1259
1260   // If there are any TLSDESC relocations, they get GOT entries in
1261   // .got.plt after the jump slot entries.
1262   // FIXME: Get the count for TLSDESC entries.
1263   this->got_tlsdesc_ = new Output_data_got<64, false>(0);
1264   layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
1265                                   elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
1266                                   this->got_tlsdesc_,
1267                                   ORDER_NON_RELRO_FIRST, false);
1268
1269   // Create the PLT section.
1270   this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1271                                           this->got_plt_, plt_count);
1272   layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1273                                   elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
1274                                   this->plt_, ORDER_PLT, false);
1275
1276   // Make the sh_info field of .rela.plt point to .plt.
1277   Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1278   rela_plt_os->set_info_section(this->plt_->output_section());
1279
1280   // Create the rela_dyn section.
1281   this->rela_dyn_section(layout);
1282
1283   return this->got_;
1284 }
1285
1286 // Reserve a GOT entry for a local symbol, and regenerate any
1287 // necessary dynamic relocations.
1288
1289 void
1290 Target_x86_64::reserve_local_got_entry(
1291     unsigned int got_index,
1292     Sized_relobj<64, false>* obj,
1293     unsigned int r_sym,
1294     unsigned int got_type)
1295 {
1296   unsigned int got_offset = got_index * 8;
1297   Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1298
1299   this->got_->reserve_local(got_index, obj, r_sym, got_type);
1300   switch (got_type)
1301     {
1302     case GOT_TYPE_STANDARD:
1303       if (parameters->options().output_is_position_independent())
1304         rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
1305                                      this->got_, got_offset, 0);
1306       break;
1307     case GOT_TYPE_TLS_OFFSET:
1308       rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
1309                           this->got_, got_offset, 0);
1310       break;
1311     case GOT_TYPE_TLS_PAIR:
1312       this->got_->reserve_slot(got_index + 1);
1313       rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
1314                           this->got_, got_offset, 0);
1315       break;
1316     case GOT_TYPE_TLS_DESC:
1317       gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
1318       // this->got_->reserve_slot(got_index + 1);
1319       // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1320       //                               this->got_, got_offset, 0);
1321       break;
1322     default:
1323       gold_unreachable();
1324     }
1325 }
1326
1327 // Reserve a GOT entry for a global symbol, and regenerate any
1328 // necessary dynamic relocations.
1329
1330 void
1331 Target_x86_64::reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
1332                                         unsigned int got_type)
1333 {
1334   unsigned int got_offset = got_index * 8;
1335   Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
1336
1337   this->got_->reserve_global(got_index, gsym, got_type);
1338   switch (got_type)
1339     {
1340     case GOT_TYPE_STANDARD:
1341       if (!gsym->final_value_is_known())
1342         {
1343           if (gsym->is_from_dynobj()
1344               || gsym->is_undefined()
1345               || gsym->is_preemptible()
1346               || gsym->type() == elfcpp::STT_GNU_IFUNC)
1347             rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
1348                                  this->got_, got_offset, 0);
1349           else
1350             rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1351                                           this->got_, got_offset, 0);
1352         }
1353       break;
1354     case GOT_TYPE_TLS_OFFSET:
1355       rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
1356                                     this->got_, got_offset, 0);
1357       break;
1358     case GOT_TYPE_TLS_PAIR:
1359       this->got_->reserve_slot(got_index + 1);
1360       rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
1361                                     this->got_, got_offset, 0);
1362       rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
1363                                     this->got_, got_offset + 8, 0);
1364       break;
1365     case GOT_TYPE_TLS_DESC:
1366       this->got_->reserve_slot(got_index + 1);
1367       rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
1368                                     this->got_, got_offset, 0);
1369       break;
1370     default:
1371       gold_unreachable();
1372     }
1373 }
1374
1375 // Register an existing PLT entry for a global symbol.
1376
1377 void
1378 Target_x86_64::register_global_plt_entry(unsigned int plt_index,
1379                                          Symbol* gsym)
1380 {
1381   gold_assert(this->plt_ != NULL);
1382   gold_assert(!gsym->has_plt_offset());
1383
1384   this->plt_->reserve_slot(plt_index);
1385
1386   gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
1387
1388   unsigned int got_offset = (plt_index + 3) * 8;
1389   this->plt_->add_relocation(gsym, got_offset);
1390 }
1391
1392 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1393
1394 void
1395 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1396 {
1397   if (this->tls_base_symbol_defined_)
1398     return;
1399
1400   Output_segment* tls_segment = layout->tls_segment();
1401   if (tls_segment != NULL)
1402     {
1403       bool is_exec = parameters->options().output_is_executable();
1404       symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1405                                        Symbol_table::PREDEFINED,
1406                                        tls_segment, 0, 0,
1407                                        elfcpp::STT_TLS,
1408                                        elfcpp::STB_LOCAL,
1409                                        elfcpp::STV_HIDDEN, 0,
1410                                        (is_exec
1411                                         ? Symbol::SEGMENT_END
1412                                         : Symbol::SEGMENT_START),
1413                                        true);
1414     }
1415   this->tls_base_symbol_defined_ = true;
1416 }
1417
1418 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1419
1420 void
1421 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1422                                              Layout* layout)
1423 {
1424   if (this->plt_ == NULL)
1425     this->make_plt_section(symtab, layout);
1426
1427   if (!this->plt_->has_tlsdesc_entry())
1428     {
1429       // Allocate the TLSDESC_GOT entry.
1430       Output_data_got<64, false>* got = this->got_section(symtab, layout);
1431       unsigned int got_offset = got->add_constant(0);
1432
1433       // Allocate the TLSDESC_PLT entry.
1434       this->plt_->reserve_tlsdesc_entry(got_offset);
1435     }
1436 }
1437
1438 // Create a GOT entry for the TLS module index.
1439
1440 unsigned int
1441 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1442                                    Sized_relobj_file<64, false>* object)
1443 {
1444   if (this->got_mod_index_offset_ == -1U)
1445     {
1446       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1447       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1448       Output_data_got<64, false>* got = this->got_section(symtab, layout);
1449       unsigned int got_offset = got->add_constant(0);
1450       rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1451                           got_offset, 0);
1452       got->add_constant(0);
1453       this->got_mod_index_offset_ = got_offset;
1454     }
1455   return this->got_mod_index_offset_;
1456 }
1457
1458 // Optimize the TLS relocation type based on what we know about the
1459 // symbol.  IS_FINAL is true if the final address of this symbol is
1460 // known at link time.
1461
1462 tls::Tls_optimization
1463 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1464 {
1465   // If we are generating a shared library, then we can't do anything
1466   // in the linker.
1467   if (parameters->options().shared())
1468     return tls::TLSOPT_NONE;
1469
1470   switch (r_type)
1471     {
1472     case elfcpp::R_X86_64_TLSGD:
1473     case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1474     case elfcpp::R_X86_64_TLSDESC_CALL:
1475       // These are General-Dynamic which permits fully general TLS
1476       // access.  Since we know that we are generating an executable,
1477       // we can convert this to Initial-Exec.  If we also know that
1478       // this is a local symbol, we can further switch to Local-Exec.
1479       if (is_final)
1480         return tls::TLSOPT_TO_LE;
1481       return tls::TLSOPT_TO_IE;
1482
1483     case elfcpp::R_X86_64_TLSLD:
1484       // This is Local-Dynamic, which refers to a local symbol in the
1485       // dynamic TLS block.  Since we know that we generating an
1486       // executable, we can switch to Local-Exec.
1487       return tls::TLSOPT_TO_LE;
1488
1489     case elfcpp::R_X86_64_DTPOFF32:
1490     case elfcpp::R_X86_64_DTPOFF64:
1491       // Another Local-Dynamic reloc.
1492       return tls::TLSOPT_TO_LE;
1493
1494     case elfcpp::R_X86_64_GOTTPOFF:
1495       // These are Initial-Exec relocs which get the thread offset
1496       // from the GOT.  If we know that we are linking against the
1497       // local symbol, we can switch to Local-Exec, which links the
1498       // thread offset into the instruction.
1499       if (is_final)
1500         return tls::TLSOPT_TO_LE;
1501       return tls::TLSOPT_NONE;
1502
1503     case elfcpp::R_X86_64_TPOFF32:
1504       // When we already have Local-Exec, there is nothing further we
1505       // can do.
1506       return tls::TLSOPT_NONE;
1507
1508     default:
1509       gold_unreachable();
1510     }
1511 }
1512
1513 // Get the Reference_flags for a particular relocation.
1514
1515 int
1516 Target_x86_64::Scan::get_reference_flags(unsigned int r_type)
1517 {
1518   switch (r_type)
1519     {
1520     case elfcpp::R_X86_64_NONE:
1521     case elfcpp::R_X86_64_GNU_VTINHERIT:
1522     case elfcpp::R_X86_64_GNU_VTENTRY:
1523     case elfcpp::R_X86_64_GOTPC32:
1524     case elfcpp::R_X86_64_GOTPC64:
1525       // No symbol reference.
1526       return 0;
1527
1528     case elfcpp::R_X86_64_64:
1529     case elfcpp::R_X86_64_32:
1530     case elfcpp::R_X86_64_32S:
1531     case elfcpp::R_X86_64_16:
1532     case elfcpp::R_X86_64_8:
1533       return Symbol::ABSOLUTE_REF;
1534
1535     case elfcpp::R_X86_64_PC64:
1536     case elfcpp::R_X86_64_PC32:
1537     case elfcpp::R_X86_64_PC16:
1538     case elfcpp::R_X86_64_PC8:
1539     case elfcpp::R_X86_64_GOTOFF64:
1540       return Symbol::RELATIVE_REF;
1541
1542     case elfcpp::R_X86_64_PLT32:
1543     case elfcpp::R_X86_64_PLTOFF64:
1544       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1545
1546     case elfcpp::R_X86_64_GOT64:
1547     case elfcpp::R_X86_64_GOT32:
1548     case elfcpp::R_X86_64_GOTPCREL64:
1549     case elfcpp::R_X86_64_GOTPCREL:
1550     case elfcpp::R_X86_64_GOTPLT64:
1551       // Absolute in GOT.
1552       return Symbol::ABSOLUTE_REF;
1553
1554     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1555     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1556     case elfcpp::R_X86_64_TLSDESC_CALL:
1557     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1558     case elfcpp::R_X86_64_DTPOFF32:
1559     case elfcpp::R_X86_64_DTPOFF64:
1560     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1561     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1562       return Symbol::TLS_REF;
1563
1564     case elfcpp::R_X86_64_COPY:
1565     case elfcpp::R_X86_64_GLOB_DAT:
1566     case elfcpp::R_X86_64_JUMP_SLOT:
1567     case elfcpp::R_X86_64_RELATIVE:
1568     case elfcpp::R_X86_64_IRELATIVE:
1569     case elfcpp::R_X86_64_TPOFF64:
1570     case elfcpp::R_X86_64_DTPMOD64:
1571     case elfcpp::R_X86_64_TLSDESC:
1572     case elfcpp::R_X86_64_SIZE32:
1573     case elfcpp::R_X86_64_SIZE64:
1574     default:
1575       // Not expected.  We will give an error later.
1576       return 0;
1577     }
1578 }
1579
1580 // Report an unsupported relocation against a local symbol.
1581
1582 void
1583 Target_x86_64::Scan::unsupported_reloc_local(
1584      Sized_relobj_file<64, false>* object,
1585      unsigned int r_type)
1586 {
1587   gold_error(_("%s: unsupported reloc %u against local symbol"),
1588              object->name().c_str(), r_type);
1589 }
1590
1591 // We are about to emit a dynamic relocation of type R_TYPE.  If the
1592 // dynamic linker does not support it, issue an error.  The GNU linker
1593 // only issues a non-PIC error for an allocated read-only section.
1594 // Here we know the section is allocated, but we don't know that it is
1595 // read-only.  But we check for all the relocation types which the
1596 // glibc dynamic linker supports, so it seems appropriate to issue an
1597 // error even if the section is not read-only.
1598
1599 void
1600 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1601 {
1602   switch (r_type)
1603     {
1604       // These are the relocation types supported by glibc for x86_64
1605       // which should always work.
1606     case elfcpp::R_X86_64_RELATIVE:
1607     case elfcpp::R_X86_64_IRELATIVE:
1608     case elfcpp::R_X86_64_GLOB_DAT:
1609     case elfcpp::R_X86_64_JUMP_SLOT:
1610     case elfcpp::R_X86_64_DTPMOD64:
1611     case elfcpp::R_X86_64_DTPOFF64:
1612     case elfcpp::R_X86_64_TPOFF64:
1613     case elfcpp::R_X86_64_64:
1614     case elfcpp::R_X86_64_COPY:
1615       return;
1616
1617       // glibc supports these reloc types, but they can overflow.
1618     case elfcpp::R_X86_64_32:
1619     case elfcpp::R_X86_64_PC32:
1620       if (this->issued_non_pic_error_)
1621         return;
1622       gold_assert(parameters->options().output_is_position_independent());
1623       object->error(_("requires dynamic reloc which may overflow at runtime; "
1624                       "recompile with -fPIC"));
1625       this->issued_non_pic_error_ = true;
1626       return;
1627
1628     default:
1629       // This prevents us from issuing more than one error per reloc
1630       // section.  But we can still wind up issuing more than one
1631       // error per object file.
1632       if (this->issued_non_pic_error_)
1633         return;
1634       gold_assert(parameters->options().output_is_position_independent());
1635       object->error(_("requires unsupported dynamic reloc; "
1636                       "recompile with -fPIC"));
1637       this->issued_non_pic_error_ = true;
1638       return;
1639
1640     case elfcpp::R_X86_64_NONE:
1641       gold_unreachable();
1642     }
1643 }
1644
1645 // Return whether we need to make a PLT entry for a relocation of the
1646 // given type against a STT_GNU_IFUNC symbol.
1647
1648 bool
1649 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1650      Sized_relobj_file<64, false>* object,
1651      unsigned int r_type)
1652 {
1653   int flags = Scan::get_reference_flags(r_type);
1654   if (flags & Symbol::TLS_REF)
1655     gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1656                object->name().c_str(), r_type);
1657   return flags != 0;
1658 }
1659
1660 // Scan a relocation for a local symbol.
1661
1662 inline void
1663 Target_x86_64::Scan::local(Symbol_table* symtab,
1664                            Layout* layout,
1665                            Target_x86_64* target,
1666                            Sized_relobj_file<64, false>* object,
1667                            unsigned int data_shndx,
1668                            Output_section* output_section,
1669                            const elfcpp::Rela<64, false>& reloc,
1670                            unsigned int r_type,
1671                            const elfcpp::Sym<64, false>& lsym)
1672 {
1673   // A local STT_GNU_IFUNC symbol may require a PLT entry.
1674   if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1675       && this->reloc_needs_plt_for_ifunc(object, r_type))
1676     {
1677       unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1678       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1679     }
1680
1681   switch (r_type)
1682     {
1683     case elfcpp::R_X86_64_NONE:
1684     case elfcpp::R_X86_64_GNU_VTINHERIT:
1685     case elfcpp::R_X86_64_GNU_VTENTRY:
1686       break;
1687
1688     case elfcpp::R_X86_64_64:
1689       // If building a shared library (or a position-independent
1690       // executable), we need to create a dynamic relocation for this
1691       // location.  The relocation applied at link time will apply the
1692       // link-time value, so we flag the location with an
1693       // R_X86_64_RELATIVE relocation so the dynamic loader can
1694       // relocate it easily.
1695       if (parameters->options().output_is_position_independent())
1696         {
1697           unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1698           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1699           rela_dyn->add_local_relative(object, r_sym,
1700                                        elfcpp::R_X86_64_RELATIVE,
1701                                        output_section, data_shndx,
1702                                        reloc.get_r_offset(),
1703                                        reloc.get_r_addend());
1704         }
1705       break;
1706
1707     case elfcpp::R_X86_64_32:
1708     case elfcpp::R_X86_64_32S:
1709     case elfcpp::R_X86_64_16:
1710     case elfcpp::R_X86_64_8:
1711       // If building a shared library (or a position-independent
1712       // executable), we need to create a dynamic relocation for this
1713       // location.  We can't use an R_X86_64_RELATIVE relocation
1714       // because that is always a 64-bit relocation.
1715       if (parameters->options().output_is_position_independent())
1716         {
1717           this->check_non_pic(object, r_type);
1718
1719           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1720           unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1721           if (lsym.get_st_type() != elfcpp::STT_SECTION)
1722             rela_dyn->add_local(object, r_sym, r_type, output_section,
1723                                 data_shndx, reloc.get_r_offset(),
1724                                 reloc.get_r_addend());
1725           else
1726             {
1727               gold_assert(lsym.get_st_value() == 0);
1728               unsigned int shndx = lsym.get_st_shndx();
1729               bool is_ordinary;
1730               shndx = object->adjust_sym_shndx(r_sym, shndx,
1731                                                &is_ordinary);
1732               if (!is_ordinary)
1733                 object->error(_("section symbol %u has bad shndx %u"),
1734                               r_sym, shndx);
1735               else
1736                 rela_dyn->add_local_section(object, shndx,
1737                                             r_type, output_section,
1738                                             data_shndx, reloc.get_r_offset(),
1739                                             reloc.get_r_addend());
1740             }
1741         }
1742       break;
1743
1744     case elfcpp::R_X86_64_PC64:
1745     case elfcpp::R_X86_64_PC32:
1746     case elfcpp::R_X86_64_PC16:
1747     case elfcpp::R_X86_64_PC8:
1748       break;
1749
1750     case elfcpp::R_X86_64_PLT32:
1751       // Since we know this is a local symbol, we can handle this as a
1752       // PC32 reloc.
1753       break;
1754
1755     case elfcpp::R_X86_64_GOTPC32:
1756     case elfcpp::R_X86_64_GOTOFF64:
1757     case elfcpp::R_X86_64_GOTPC64:
1758     case elfcpp::R_X86_64_PLTOFF64:
1759       // We need a GOT section.
1760       target->got_section(symtab, layout);
1761       // For PLTOFF64, we'd normally want a PLT section, but since we
1762       // know this is a local symbol, no PLT is needed.
1763       break;
1764
1765     case elfcpp::R_X86_64_GOT64:
1766     case elfcpp::R_X86_64_GOT32:
1767     case elfcpp::R_X86_64_GOTPCREL64:
1768     case elfcpp::R_X86_64_GOTPCREL:
1769     case elfcpp::R_X86_64_GOTPLT64:
1770       {
1771         // The symbol requires a GOT entry.
1772         Output_data_got<64, false>* got = target->got_section(symtab, layout);
1773         unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1774
1775         // For a STT_GNU_IFUNC symbol we want the PLT offset.  That
1776         // lets function pointers compare correctly with shared
1777         // libraries.  Otherwise we would need an IRELATIVE reloc.
1778         bool is_new;
1779         if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1780           is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1781         else
1782           is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1783         if (is_new)
1784           {
1785             // If we are generating a shared object, we need to add a
1786             // dynamic relocation for this symbol's GOT entry.
1787             if (parameters->options().output_is_position_independent())
1788               {
1789                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1790                 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1791                 if (r_type != elfcpp::R_X86_64_GOT32)
1792                   {
1793                     unsigned int got_offset =
1794                       object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1795                     rela_dyn->add_local_relative(object, r_sym,
1796                                                  elfcpp::R_X86_64_RELATIVE,
1797                                                  got, got_offset, 0);
1798                   }
1799                 else
1800                   {
1801                     this->check_non_pic(object, r_type);
1802
1803                     gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1804                     rela_dyn->add_local(
1805                         object, r_sym, r_type, got,
1806                         object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1807                   }
1808               }
1809           }
1810         // For GOTPLT64, we'd normally want a PLT section, but since
1811         // we know this is a local symbol, no PLT is needed.
1812       }
1813       break;
1814
1815     case elfcpp::R_X86_64_COPY:
1816     case elfcpp::R_X86_64_GLOB_DAT:
1817     case elfcpp::R_X86_64_JUMP_SLOT:
1818     case elfcpp::R_X86_64_RELATIVE:
1819     case elfcpp::R_X86_64_IRELATIVE:
1820       // These are outstanding tls relocs, which are unexpected when linking
1821     case elfcpp::R_X86_64_TPOFF64:
1822     case elfcpp::R_X86_64_DTPMOD64:
1823     case elfcpp::R_X86_64_TLSDESC:
1824       gold_error(_("%s: unexpected reloc %u in object file"),
1825                  object->name().c_str(), r_type);
1826       break;
1827
1828       // These are initial tls relocs, which are expected when linking
1829     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1830     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1831     case elfcpp::R_X86_64_TLSDESC_CALL:
1832     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1833     case elfcpp::R_X86_64_DTPOFF32:
1834     case elfcpp::R_X86_64_DTPOFF64:
1835     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1836     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1837       {
1838         bool output_is_shared = parameters->options().shared();
1839         const tls::Tls_optimization optimized_type
1840             = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1841         switch (r_type)
1842           {
1843           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
1844             if (optimized_type == tls::TLSOPT_NONE)
1845               {
1846                 // Create a pair of GOT entries for the module index and
1847                 // dtv-relative offset.
1848                 Output_data_got<64, false>* got
1849                     = target->got_section(symtab, layout);
1850                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1851                 unsigned int shndx = lsym.get_st_shndx();
1852                 bool is_ordinary;
1853                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1854                 if (!is_ordinary)
1855                   object->error(_("local symbol %u has bad shndx %u"),
1856                               r_sym, shndx);
1857                 else
1858                   got->add_local_pair_with_rela(object, r_sym,
1859                                                 shndx,
1860                                                 GOT_TYPE_TLS_PAIR,
1861                                                 target->rela_dyn_section(layout),
1862                                                 elfcpp::R_X86_64_DTPMOD64, 0);
1863               }
1864             else if (optimized_type != tls::TLSOPT_TO_LE)
1865               unsupported_reloc_local(object, r_type);
1866             break;
1867
1868           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1869             target->define_tls_base_symbol(symtab, layout);
1870             if (optimized_type == tls::TLSOPT_NONE)
1871               {
1872                 // Create reserved PLT and GOT entries for the resolver.
1873                 target->reserve_tlsdesc_entries(symtab, layout);
1874
1875                 // Generate a double GOT entry with an
1876                 // R_X86_64_TLSDESC reloc.  The R_X86_64_TLSDESC reloc
1877                 // is resolved lazily, so the GOT entry needs to be in
1878                 // an area in .got.plt, not .got.  Call got_section to
1879                 // make sure the section has been created.
1880                 target->got_section(symtab, layout);
1881                 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1882                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1883                 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1884                   {
1885                     unsigned int got_offset = got->add_constant(0);
1886                     got->add_constant(0);
1887                     object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1888                                                  got_offset);
1889                     Reloc_section* rt = target->rela_tlsdesc_section(layout);
1890                     // We store the arguments we need in a vector, and
1891                     // use the index into the vector as the parameter
1892                     // to pass to the target specific routines.
1893                     uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1894                     void* arg = reinterpret_cast<void*>(intarg);
1895                     rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1896                                             got, got_offset, 0);
1897                   }
1898               }
1899             else if (optimized_type != tls::TLSOPT_TO_LE)
1900               unsupported_reloc_local(object, r_type);
1901             break;
1902
1903           case elfcpp::R_X86_64_TLSDESC_CALL:
1904             break;
1905
1906           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
1907             if (optimized_type == tls::TLSOPT_NONE)
1908               {
1909                 // Create a GOT entry for the module index.
1910                 target->got_mod_index_entry(symtab, layout, object);
1911               }
1912             else if (optimized_type != tls::TLSOPT_TO_LE)
1913               unsupported_reloc_local(object, r_type);
1914             break;
1915
1916           case elfcpp::R_X86_64_DTPOFF32:
1917           case elfcpp::R_X86_64_DTPOFF64:
1918             break;
1919
1920           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
1921             layout->set_has_static_tls();
1922             if (optimized_type == tls::TLSOPT_NONE)
1923               {
1924                 // Create a GOT entry for the tp-relative offset.
1925                 Output_data_got<64, false>* got
1926                     = target->got_section(symtab, layout);
1927                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1928                 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1929                                          target->rela_dyn_section(layout),
1930                                          elfcpp::R_X86_64_TPOFF64);
1931               }
1932             else if (optimized_type != tls::TLSOPT_TO_LE)
1933               unsupported_reloc_local(object, r_type);
1934             break;
1935
1936           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
1937             layout->set_has_static_tls();
1938             if (output_is_shared)
1939               unsupported_reloc_local(object, r_type);
1940             break;
1941
1942           default:
1943             gold_unreachable();
1944           }
1945       }
1946       break;
1947
1948     case elfcpp::R_X86_64_SIZE32:
1949     case elfcpp::R_X86_64_SIZE64:
1950     default:
1951       gold_error(_("%s: unsupported reloc %u against local symbol"),
1952                  object->name().c_str(), r_type);
1953       break;
1954     }
1955 }
1956
1957
1958 // Report an unsupported relocation against a global symbol.
1959
1960 void
1961 Target_x86_64::Scan::unsupported_reloc_global(
1962     Sized_relobj_file<64, false>* object,
1963     unsigned int r_type,
1964     Symbol* gsym)
1965 {
1966   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1967              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1968 }
1969
1970 // Returns true if this relocation type could be that of a function pointer.
1971 inline bool
1972 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
1973 {
1974   switch (r_type)
1975     {
1976     case elfcpp::R_X86_64_64:
1977     case elfcpp::R_X86_64_32:
1978     case elfcpp::R_X86_64_32S:
1979     case elfcpp::R_X86_64_16:
1980     case elfcpp::R_X86_64_8:
1981     case elfcpp::R_X86_64_GOT64:
1982     case elfcpp::R_X86_64_GOT32:
1983     case elfcpp::R_X86_64_GOTPCREL64:
1984     case elfcpp::R_X86_64_GOTPCREL:
1985     case elfcpp::R_X86_64_GOTPLT64:
1986       {
1987         return true;
1988       }
1989     }
1990   return false;
1991 }
1992
1993 // For safe ICF, scan a relocation for a local symbol to check if it
1994 // corresponds to a function pointer being taken.  In that case mark
1995 // the function whose pointer was taken as not foldable.
1996
1997 inline bool
1998 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
1999   Symbol_table* ,
2000   Layout* ,
2001   Target_x86_64* ,
2002   Sized_relobj_file<64, false>* ,
2003   unsigned int ,
2004   Output_section* ,
2005   const elfcpp::Rela<64, false>& ,
2006   unsigned int r_type,
2007   const elfcpp::Sym<64, false>&)
2008 {
2009   // When building a shared library, do not fold any local symbols as it is
2010   // not possible to distinguish pointer taken versus a call by looking at
2011   // the relocation types.
2012   return (parameters->options().shared()
2013           || possible_function_pointer_reloc(r_type));
2014 }
2015
2016 // For safe ICF, scan a relocation for a global symbol to check if it
2017 // corresponds to a function pointer being taken.  In that case mark
2018 // the function whose pointer was taken as not foldable.
2019
2020 inline bool
2021 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2022   Symbol_table*,
2023   Layout* ,
2024   Target_x86_64* ,
2025   Sized_relobj_file<64, false>* ,
2026   unsigned int ,
2027   Output_section* ,
2028   const elfcpp::Rela<64, false>& ,
2029   unsigned int r_type,
2030   Symbol* gsym)
2031 {
2032   // When building a shared library, do not fold symbols whose visibility
2033   // is hidden, internal or protected.
2034   return ((parameters->options().shared()
2035            && (gsym->visibility() == elfcpp::STV_INTERNAL
2036                || gsym->visibility() == elfcpp::STV_PROTECTED
2037                || gsym->visibility() == elfcpp::STV_HIDDEN))
2038           || possible_function_pointer_reloc(r_type));
2039 }
2040
2041 // Scan a relocation for a global symbol.
2042
2043 inline void
2044 Target_x86_64::Scan::global(Symbol_table* symtab,
2045                             Layout* layout,
2046                             Target_x86_64* target,
2047                             Sized_relobj_file<64, false>* object,
2048                             unsigned int data_shndx,
2049                             Output_section* output_section,
2050                             const elfcpp::Rela<64, false>& reloc,
2051                             unsigned int r_type,
2052                             Symbol* gsym)
2053 {
2054   // A STT_GNU_IFUNC symbol may require a PLT entry.
2055   if (gsym->type() == elfcpp::STT_GNU_IFUNC
2056       && this->reloc_needs_plt_for_ifunc(object, r_type))
2057     target->make_plt_entry(symtab, layout, gsym);
2058
2059   switch (r_type)
2060     {
2061     case elfcpp::R_X86_64_NONE:
2062     case elfcpp::R_X86_64_GNU_VTINHERIT:
2063     case elfcpp::R_X86_64_GNU_VTENTRY:
2064       break;
2065
2066     case elfcpp::R_X86_64_64:
2067     case elfcpp::R_X86_64_32:
2068     case elfcpp::R_X86_64_32S:
2069     case elfcpp::R_X86_64_16:
2070     case elfcpp::R_X86_64_8:
2071       {
2072         // Make a PLT entry if necessary.
2073         if (gsym->needs_plt_entry())
2074           {
2075             target->make_plt_entry(symtab, layout, gsym);
2076             // Since this is not a PC-relative relocation, we may be
2077             // taking the address of a function. In that case we need to
2078             // set the entry in the dynamic symbol table to the address of
2079             // the PLT entry.
2080             if (gsym->is_from_dynobj() && !parameters->options().shared())
2081               gsym->set_needs_dynsym_value();
2082           }
2083         // Make a dynamic relocation if necessary.
2084         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2085           {
2086             if (gsym->may_need_copy_reloc())
2087               {
2088                 target->copy_reloc(symtab, layout, object,
2089                                    data_shndx, output_section, gsym, reloc);
2090               }
2091             else if (r_type == elfcpp::R_X86_64_64
2092                      && gsym->type() == elfcpp::STT_GNU_IFUNC
2093                      && gsym->can_use_relative_reloc(false)
2094                      && !gsym->is_from_dynobj()
2095                      && !gsym->is_undefined()
2096                      && !gsym->is_preemptible())
2097               {
2098                 // Use an IRELATIVE reloc for a locally defined
2099                 // STT_GNU_IFUNC symbol.  This makes a function
2100                 // address in a PIE executable match the address in a
2101                 // shared library that it links against.
2102                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2103                 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2104                 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2105                                                        output_section, object,
2106                                                        data_shndx,
2107                                                        reloc.get_r_offset(),
2108                                                        reloc.get_r_addend());
2109               }
2110             else if (r_type == elfcpp::R_X86_64_64
2111                      && gsym->can_use_relative_reloc(false))
2112               {
2113                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2114                 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2115                                               output_section, object,
2116                                               data_shndx,
2117                                               reloc.get_r_offset(),
2118                                               reloc.get_r_addend());
2119               }
2120             else
2121               {
2122                 this->check_non_pic(object, r_type);
2123                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2124                 rela_dyn->add_global(gsym, r_type, output_section, object,
2125                                      data_shndx, reloc.get_r_offset(),
2126                                      reloc.get_r_addend());
2127               }
2128           }
2129       }
2130       break;
2131
2132     case elfcpp::R_X86_64_PC64:
2133     case elfcpp::R_X86_64_PC32:
2134     case elfcpp::R_X86_64_PC16:
2135     case elfcpp::R_X86_64_PC8:
2136       {
2137         // Make a PLT entry if necessary.
2138         if (gsym->needs_plt_entry())
2139           target->make_plt_entry(symtab, layout, gsym);
2140         // Make a dynamic relocation if necessary.
2141         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2142           {
2143             if (gsym->may_need_copy_reloc())
2144               {
2145                 target->copy_reloc(symtab, layout, object,
2146                                    data_shndx, output_section, gsym, reloc);
2147               }
2148             else
2149               {
2150                 this->check_non_pic(object, r_type);
2151                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2152                 rela_dyn->add_global(gsym, r_type, output_section, object,
2153                                      data_shndx, reloc.get_r_offset(),
2154                                      reloc.get_r_addend());
2155               }
2156           }
2157       }
2158       break;
2159
2160     case elfcpp::R_X86_64_GOT64:
2161     case elfcpp::R_X86_64_GOT32:
2162     case elfcpp::R_X86_64_GOTPCREL64:
2163     case elfcpp::R_X86_64_GOTPCREL:
2164     case elfcpp::R_X86_64_GOTPLT64:
2165       {
2166         // The symbol requires a GOT entry.
2167         Output_data_got<64, false>* got = target->got_section(symtab, layout);
2168         if (gsym->final_value_is_known())
2169           {
2170             // For a STT_GNU_IFUNC symbol we want the PLT address.
2171             if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2172               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2173             else
2174               got->add_global(gsym, GOT_TYPE_STANDARD);
2175           }
2176         else
2177           {
2178             // If this symbol is not fully resolved, we need to add a
2179             // dynamic relocation for it.
2180             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2181             if (gsym->is_from_dynobj()
2182                 || gsym->is_undefined()
2183                 || gsym->is_preemptible()
2184                 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2185                     && parameters->options().output_is_position_independent()))
2186               got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2187                                         elfcpp::R_X86_64_GLOB_DAT);
2188             else
2189               {
2190                 // For a STT_GNU_IFUNC symbol we want to write the PLT
2191                 // offset into the GOT, so that function pointer
2192                 // comparisons work correctly.
2193                 bool is_new;
2194                 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2195                   is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2196                 else
2197                   {
2198                     is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2199                     // Tell the dynamic linker to use the PLT address
2200                     // when resolving relocations.
2201                     if (gsym->is_from_dynobj()
2202                         && !parameters->options().shared())
2203                       gsym->set_needs_dynsym_value();
2204                   }
2205                 if (is_new)
2206                   {
2207                     unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2208                     rela_dyn->add_global_relative(gsym,
2209                                                   elfcpp::R_X86_64_RELATIVE,
2210                                                   got, got_off, 0);
2211                   }
2212               }
2213           }
2214         // For GOTPLT64, we also need a PLT entry (but only if the
2215         // symbol is not fully resolved).
2216         if (r_type == elfcpp::R_X86_64_GOTPLT64
2217             && !gsym->final_value_is_known())
2218           target->make_plt_entry(symtab, layout, gsym);
2219       }
2220       break;
2221
2222     case elfcpp::R_X86_64_PLT32:
2223       // If the symbol is fully resolved, this is just a PC32 reloc.
2224       // Otherwise we need a PLT entry.
2225       if (gsym->final_value_is_known())
2226         break;
2227       // If building a shared library, we can also skip the PLT entry
2228       // if the symbol is defined in the output file and is protected
2229       // or hidden.
2230       if (gsym->is_defined()
2231           && !gsym->is_from_dynobj()
2232           && !gsym->is_preemptible())
2233         break;
2234       target->make_plt_entry(symtab, layout, gsym);
2235       break;
2236
2237     case elfcpp::R_X86_64_GOTPC32:
2238     case elfcpp::R_X86_64_GOTOFF64:
2239     case elfcpp::R_X86_64_GOTPC64:
2240     case elfcpp::R_X86_64_PLTOFF64:
2241       // We need a GOT section.
2242       target->got_section(symtab, layout);
2243       // For PLTOFF64, we also need a PLT entry (but only if the
2244       // symbol is not fully resolved).
2245       if (r_type == elfcpp::R_X86_64_PLTOFF64
2246           && !gsym->final_value_is_known())
2247         target->make_plt_entry(symtab, layout, gsym);
2248       break;
2249
2250     case elfcpp::R_X86_64_COPY:
2251     case elfcpp::R_X86_64_GLOB_DAT:
2252     case elfcpp::R_X86_64_JUMP_SLOT:
2253     case elfcpp::R_X86_64_RELATIVE:
2254     case elfcpp::R_X86_64_IRELATIVE:
2255       // These are outstanding tls relocs, which are unexpected when linking
2256     case elfcpp::R_X86_64_TPOFF64:
2257     case elfcpp::R_X86_64_DTPMOD64:
2258     case elfcpp::R_X86_64_TLSDESC:
2259       gold_error(_("%s: unexpected reloc %u in object file"),
2260                  object->name().c_str(), r_type);
2261       break;
2262
2263       // These are initial tls relocs, which are expected for global()
2264     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2265     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2266     case elfcpp::R_X86_64_TLSDESC_CALL:
2267     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2268     case elfcpp::R_X86_64_DTPOFF32:
2269     case elfcpp::R_X86_64_DTPOFF64:
2270     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2271     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2272       {
2273         const bool is_final = gsym->final_value_is_known();
2274         const tls::Tls_optimization optimized_type
2275             = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2276         switch (r_type)
2277           {
2278           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
2279             if (optimized_type == tls::TLSOPT_NONE)
2280               {
2281                 // Create a pair of GOT entries for the module index and
2282                 // dtv-relative offset.
2283                 Output_data_got<64, false>* got
2284                     = target->got_section(symtab, layout);
2285                 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2286                                                target->rela_dyn_section(layout),
2287                                                elfcpp::R_X86_64_DTPMOD64,
2288                                                elfcpp::R_X86_64_DTPOFF64);
2289               }
2290             else if (optimized_type == tls::TLSOPT_TO_IE)
2291               {
2292                 // Create a GOT entry for the tp-relative offset.
2293                 Output_data_got<64, false>* got
2294                     = target->got_section(symtab, layout);
2295                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2296                                           target->rela_dyn_section(layout),
2297                                           elfcpp::R_X86_64_TPOFF64);
2298               }
2299             else if (optimized_type != tls::TLSOPT_TO_LE)
2300               unsupported_reloc_global(object, r_type, gsym);
2301             break;
2302
2303           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2304             target->define_tls_base_symbol(symtab, layout);
2305             if (optimized_type == tls::TLSOPT_NONE)
2306               {
2307                 // Create reserved PLT and GOT entries for the resolver.
2308                 target->reserve_tlsdesc_entries(symtab, layout);
2309
2310                 // Create a double GOT entry with an R_X86_64_TLSDESC
2311                 // reloc.  The R_X86_64_TLSDESC reloc is resolved
2312                 // lazily, so the GOT entry needs to be in an area in
2313                 // .got.plt, not .got.  Call got_section to make sure
2314                 // the section has been created.
2315                 target->got_section(symtab, layout);
2316                 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2317                 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2318                 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
2319                                                elfcpp::R_X86_64_TLSDESC, 0);
2320               }
2321             else if (optimized_type == tls::TLSOPT_TO_IE)
2322               {
2323                 // Create a GOT entry for the tp-relative offset.
2324                 Output_data_got<64, false>* got
2325                     = target->got_section(symtab, layout);
2326                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2327                                           target->rela_dyn_section(layout),
2328                                           elfcpp::R_X86_64_TPOFF64);
2329               }
2330             else if (optimized_type != tls::TLSOPT_TO_LE)
2331               unsupported_reloc_global(object, r_type, gsym);
2332             break;
2333
2334           case elfcpp::R_X86_64_TLSDESC_CALL:
2335             break;
2336
2337           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
2338             if (optimized_type == tls::TLSOPT_NONE)
2339               {
2340                 // Create a GOT entry for the module index.
2341                 target->got_mod_index_entry(symtab, layout, object);
2342               }
2343             else if (optimized_type != tls::TLSOPT_TO_LE)
2344               unsupported_reloc_global(object, r_type, gsym);
2345             break;
2346
2347           case elfcpp::R_X86_64_DTPOFF32:
2348           case elfcpp::R_X86_64_DTPOFF64:
2349             break;
2350
2351           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
2352             layout->set_has_static_tls();
2353             if (optimized_type == tls::TLSOPT_NONE)
2354               {
2355                 // Create a GOT entry for the tp-relative offset.
2356                 Output_data_got<64, false>* got
2357                     = target->got_section(symtab, layout);
2358                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2359                                           target->rela_dyn_section(layout),
2360                                           elfcpp::R_X86_64_TPOFF64);
2361               }
2362             else if (optimized_type != tls::TLSOPT_TO_LE)
2363               unsupported_reloc_global(object, r_type, gsym);
2364             break;
2365
2366           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
2367             layout->set_has_static_tls();
2368             if (parameters->options().shared())
2369               unsupported_reloc_local(object, r_type);
2370             break;
2371
2372           default:
2373             gold_unreachable();
2374           }
2375       }
2376       break;
2377
2378     case elfcpp::R_X86_64_SIZE32:
2379     case elfcpp::R_X86_64_SIZE64:
2380     default:
2381       gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2382                  object->name().c_str(), r_type,
2383                  gsym->demangled_name().c_str());
2384       break;
2385     }
2386 }
2387
2388 void
2389 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
2390                                  Layout* layout,
2391                                  Sized_relobj_file<64, false>* object,
2392                                  unsigned int data_shndx,
2393                                  unsigned int sh_type,
2394                                  const unsigned char* prelocs,
2395                                  size_t reloc_count,
2396                                  Output_section* output_section,
2397                                  bool needs_special_offset_handling,
2398                                  size_t local_symbol_count,
2399                                  const unsigned char* plocal_symbols)
2400 {
2401
2402   if (sh_type == elfcpp::SHT_REL)
2403     {
2404       return;
2405     }
2406
2407    gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2408                            Target_x86_64::Scan,
2409                            Target_x86_64::Relocatable_size_for_reloc>(
2410     symtab,
2411     layout,
2412     this,
2413     object,
2414     data_shndx,
2415     prelocs,
2416     reloc_count,
2417     output_section,
2418     needs_special_offset_handling,
2419     local_symbol_count,
2420     plocal_symbols);
2421  
2422 }
2423 // Scan relocations for a section.
2424
2425 void
2426 Target_x86_64::scan_relocs(Symbol_table* symtab,
2427                            Layout* layout,
2428                            Sized_relobj_file<64, false>* object,
2429                            unsigned int data_shndx,
2430                            unsigned int sh_type,
2431                            const unsigned char* prelocs,
2432                            size_t reloc_count,
2433                            Output_section* output_section,
2434                            bool needs_special_offset_handling,
2435                            size_t local_symbol_count,
2436                            const unsigned char* plocal_symbols)
2437 {
2438   if (sh_type == elfcpp::SHT_REL)
2439     {
2440       gold_error(_("%s: unsupported REL reloc section"),
2441                  object->name().c_str());
2442       return;
2443     }
2444
2445   gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2446       Target_x86_64::Scan>(
2447     symtab,
2448     layout,
2449     this,
2450     object,
2451     data_shndx,
2452     prelocs,
2453     reloc_count,
2454     output_section,
2455     needs_special_offset_handling,
2456     local_symbol_count,
2457     plocal_symbols);
2458 }
2459
2460 // Finalize the sections.
2461
2462 void
2463 Target_x86_64::do_finalize_sections(
2464     Layout* layout,
2465     const Input_objects*,
2466     Symbol_table* symtab)
2467 {
2468   const Reloc_section* rel_plt = (this->plt_ == NULL
2469                                   ? NULL
2470                                   : this->plt_->rela_plt());
2471   layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2472                                   this->rela_dyn_, true, false);
2473                                   
2474   // Fill in some more dynamic tags.
2475   Output_data_dynamic* const odyn = layout->dynamic_data();
2476   if (odyn != NULL)
2477     {
2478       if (this->plt_ != NULL
2479           && this->plt_->output_section() != NULL
2480           && this->plt_->has_tlsdesc_entry())
2481         {
2482           unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2483           unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2484           this->got_->finalize_data_size();
2485           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2486                                         this->plt_, plt_offset);
2487           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2488                                         this->got_, got_offset);
2489         }
2490     }
2491
2492   // Emit any relocs we saved in an attempt to avoid generating COPY
2493   // relocs.
2494   if (this->copy_relocs_.any_saved_relocs())
2495     this->copy_relocs_.emit(this->rela_dyn_section(layout));
2496
2497   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2498   // the .got.plt section.
2499   Symbol* sym = this->global_offset_table_;
2500   if (sym != NULL)
2501     {
2502       uint64_t data_size = this->got_plt_->current_data_size();
2503       symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2504     }
2505 }
2506
2507 // Perform a relocation.
2508
2509 inline bool
2510 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2511                                   Target_x86_64* target,
2512                                   Output_section*,
2513                                   size_t relnum,
2514                                   const elfcpp::Rela<64, false>& rela,
2515                                   unsigned int r_type,
2516                                   const Sized_symbol<64>* gsym,
2517                                   const Symbol_value<64>* psymval,
2518                                   unsigned char* view,
2519                                   elfcpp::Elf_types<64>::Elf_Addr address,
2520                                   section_size_type view_size)
2521 {
2522   if (this->skip_call_tls_get_addr_)
2523     {
2524       if ((r_type != elfcpp::R_X86_64_PLT32
2525            && r_type != elfcpp::R_X86_64_PC32)
2526           || gsym == NULL
2527           || strcmp(gsym->name(), "__tls_get_addr") != 0)
2528         {
2529           gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2530                                  _("missing expected TLS relocation"));
2531         }
2532       else
2533         {
2534           this->skip_call_tls_get_addr_ = false;
2535           return false;
2536         }
2537     }
2538
2539   const Sized_relobj_file<64, false>* object = relinfo->object;
2540
2541   // Pick the value to use for symbols defined in the PLT.
2542   Symbol_value<64> symval;
2543   if (gsym != NULL
2544       && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2545     {
2546       symval.set_output_value(target->plt_section()->address()
2547                               + gsym->plt_offset());
2548       psymval = &symval;
2549     }
2550   else if (gsym == NULL && psymval->is_ifunc_symbol())
2551     {
2552       unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2553       if (object->local_has_plt_offset(r_sym))
2554         {
2555           symval.set_output_value(target->plt_section()->address()
2556                                   + object->local_plt_offset(r_sym));
2557           psymval = &symval;
2558         }
2559     }
2560
2561   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2562
2563   // Get the GOT offset if needed.
2564   // The GOT pointer points to the end of the GOT section.
2565   // We need to subtract the size of the GOT section to get
2566   // the actual offset to use in the relocation.
2567   bool have_got_offset = false;
2568   unsigned int got_offset = 0;
2569   switch (r_type)
2570     {
2571     case elfcpp::R_X86_64_GOT32:
2572     case elfcpp::R_X86_64_GOT64:
2573     case elfcpp::R_X86_64_GOTPLT64:
2574     case elfcpp::R_X86_64_GOTPCREL:
2575     case elfcpp::R_X86_64_GOTPCREL64:
2576       if (gsym != NULL)
2577         {
2578           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2579           got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
2580         }
2581       else
2582         {
2583           unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2584           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2585           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2586                         - target->got_size());
2587         }
2588       have_got_offset = true;
2589       break;
2590
2591     default:
2592       break;
2593     }
2594
2595   switch (r_type)
2596     {
2597     case elfcpp::R_X86_64_NONE:
2598     case elfcpp::R_X86_64_GNU_VTINHERIT:
2599     case elfcpp::R_X86_64_GNU_VTENTRY:
2600       break;
2601
2602     case elfcpp::R_X86_64_64:
2603       Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2604       break;
2605
2606     case elfcpp::R_X86_64_PC64:
2607       Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2608                                               address);
2609       break;
2610
2611     case elfcpp::R_X86_64_32:
2612       // FIXME: we need to verify that value + addend fits into 32 bits:
2613       //    uint64_t x = value + addend;
2614       //    x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2615       // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2616       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2617       break;
2618
2619     case elfcpp::R_X86_64_32S:
2620       // FIXME: we need to verify that value + addend fits into 32 bits:
2621       //    int64_t x = value + addend;   // note this quantity is signed!
2622       //    x == static_cast<int64_t>(static_cast<int32_t>(x))
2623       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2624       break;
2625
2626     case elfcpp::R_X86_64_PC32:
2627       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2628                                               address);
2629       break;
2630
2631     case elfcpp::R_X86_64_16:
2632       Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2633       break;
2634
2635     case elfcpp::R_X86_64_PC16:
2636       Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2637                                               address);
2638       break;
2639
2640     case elfcpp::R_X86_64_8:
2641       Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2642       break;
2643
2644     case elfcpp::R_X86_64_PC8:
2645       Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2646                                              address);
2647       break;
2648
2649     case elfcpp::R_X86_64_PLT32:
2650       gold_assert(gsym == NULL
2651                   || gsym->has_plt_offset()
2652                   || gsym->final_value_is_known()
2653                   || (gsym->is_defined()
2654                       && !gsym->is_from_dynobj()
2655                       && !gsym->is_preemptible()));
2656       // Note: while this code looks the same as for R_X86_64_PC32, it
2657       // behaves differently because psymval was set to point to
2658       // the PLT entry, rather than the symbol, in Scan::global().
2659       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2660                                               address);
2661       break;
2662
2663     case elfcpp::R_X86_64_PLTOFF64:
2664       {
2665         gold_assert(gsym);
2666         gold_assert(gsym->has_plt_offset()
2667                     || gsym->final_value_is_known());
2668         elfcpp::Elf_types<64>::Elf_Addr got_address;
2669         got_address = target->got_section(NULL, NULL)->address();
2670         Relocate_functions<64, false>::rela64(view, object, psymval,
2671                                               addend - got_address);
2672       }
2673
2674     case elfcpp::R_X86_64_GOT32:
2675       gold_assert(have_got_offset);
2676       Relocate_functions<64, false>::rela32(view, got_offset, addend);
2677       break;
2678
2679     case elfcpp::R_X86_64_GOTPC32:
2680       {
2681         gold_assert(gsym);
2682         elfcpp::Elf_types<64>::Elf_Addr value;
2683         value = target->got_plt_section()->address();
2684         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2685       }
2686       break;
2687
2688     case elfcpp::R_X86_64_GOT64:
2689       // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2690       // Since we always add a PLT entry, this is equivalent.
2691     case elfcpp::R_X86_64_GOTPLT64:
2692       gold_assert(have_got_offset);
2693       Relocate_functions<64, false>::rela64(view, got_offset, addend);
2694       break;
2695
2696     case elfcpp::R_X86_64_GOTPC64:
2697       {
2698         gold_assert(gsym);
2699         elfcpp::Elf_types<64>::Elf_Addr value;
2700         value = target->got_plt_section()->address();
2701         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2702       }
2703       break;
2704
2705     case elfcpp::R_X86_64_GOTOFF64:
2706       {
2707         elfcpp::Elf_types<64>::Elf_Addr value;
2708         value = (psymval->value(object, 0)
2709                  - target->got_plt_section()->address());
2710         Relocate_functions<64, false>::rela64(view, value, addend);
2711       }
2712       break;
2713
2714     case elfcpp::R_X86_64_GOTPCREL:
2715       {
2716         gold_assert(have_got_offset);
2717         elfcpp::Elf_types<64>::Elf_Addr value;
2718         value = target->got_plt_section()->address() + got_offset;
2719         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2720       }
2721       break;
2722
2723     case elfcpp::R_X86_64_GOTPCREL64:
2724       {
2725         gold_assert(have_got_offset);
2726         elfcpp::Elf_types<64>::Elf_Addr value;
2727         value = target->got_plt_section()->address() + got_offset;
2728         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2729       }
2730       break;
2731
2732     case elfcpp::R_X86_64_COPY:
2733     case elfcpp::R_X86_64_GLOB_DAT:
2734     case elfcpp::R_X86_64_JUMP_SLOT:
2735     case elfcpp::R_X86_64_RELATIVE:
2736     case elfcpp::R_X86_64_IRELATIVE:
2737       // These are outstanding tls relocs, which are unexpected when linking
2738     case elfcpp::R_X86_64_TPOFF64:
2739     case elfcpp::R_X86_64_DTPMOD64:
2740     case elfcpp::R_X86_64_TLSDESC:
2741       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2742                              _("unexpected reloc %u in object file"),
2743                              r_type);
2744       break;
2745
2746       // These are initial tls relocs, which are expected when linking
2747     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2748     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2749     case elfcpp::R_X86_64_TLSDESC_CALL:
2750     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2751     case elfcpp::R_X86_64_DTPOFF32:
2752     case elfcpp::R_X86_64_DTPOFF64:
2753     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2754     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2755       this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2756                          view, address, view_size);
2757       break;
2758
2759     case elfcpp::R_X86_64_SIZE32:
2760     case elfcpp::R_X86_64_SIZE64:
2761     default:
2762       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2763                              _("unsupported reloc %u"),
2764                              r_type);
2765       break;
2766     }
2767
2768   return true;
2769 }
2770
2771 // Perform a TLS relocation.
2772
2773 inline void
2774 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
2775                                       Target_x86_64* target,
2776                                       size_t relnum,
2777                                       const elfcpp::Rela<64, false>& rela,
2778                                       unsigned int r_type,
2779                                       const Sized_symbol<64>* gsym,
2780                                       const Symbol_value<64>* psymval,
2781                                       unsigned char* view,
2782                                       elfcpp::Elf_types<64>::Elf_Addr address,
2783                                       section_size_type view_size)
2784 {
2785   Output_segment* tls_segment = relinfo->layout->tls_segment();
2786
2787   const Sized_relobj_file<64, false>* object = relinfo->object;
2788   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2789   elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
2790   bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2791
2792   elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2793
2794   const bool is_final = (gsym == NULL
2795                          ? !parameters->options().shared()
2796                          : gsym->final_value_is_known());
2797   tls::Tls_optimization optimized_type
2798       = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2799   switch (r_type)
2800     {
2801     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2802       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2803         {
2804           // If this code sequence is used in a non-executable section,
2805           // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2806           // on the assumption that it's being used by itself in a debug
2807           // section.  Therefore, in the unlikely event that the code
2808           // sequence appears in a non-executable section, we simply
2809           // leave it unoptimized.
2810           optimized_type = tls::TLSOPT_NONE;
2811         }
2812       if (optimized_type == tls::TLSOPT_TO_LE)
2813         {
2814           gold_assert(tls_segment != NULL);
2815           this->tls_gd_to_le(relinfo, relnum, tls_segment,
2816                              rela, r_type, value, view,
2817                              view_size);
2818           break;
2819         }
2820       else
2821         {
2822           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2823                                    ? GOT_TYPE_TLS_OFFSET
2824                                    : GOT_TYPE_TLS_PAIR);
2825           unsigned int got_offset;
2826           if (gsym != NULL)
2827             {
2828               gold_assert(gsym->has_got_offset(got_type));
2829               got_offset = gsym->got_offset(got_type) - target->got_size();
2830             }
2831           else
2832             {
2833               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2834               gold_assert(object->local_has_got_offset(r_sym, got_type));
2835               got_offset = (object->local_got_offset(r_sym, got_type)
2836                             - target->got_size());
2837             }
2838           if (optimized_type == tls::TLSOPT_TO_IE)
2839             {
2840               gold_assert(tls_segment != NULL);
2841               value = target->got_plt_section()->address() + got_offset;
2842               this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2843                                  value, view, address, view_size);
2844               break;
2845             }
2846           else if (optimized_type == tls::TLSOPT_NONE)
2847             {
2848               // Relocate the field with the offset of the pair of GOT
2849               // entries.
2850               value = target->got_plt_section()->address() + got_offset;
2851               Relocate_functions<64, false>::pcrela32(view, value, addend,
2852                                                       address);
2853               break;
2854             }
2855         }
2856       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2857                              _("unsupported reloc %u"), r_type);
2858       break;
2859
2860     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2861     case elfcpp::R_X86_64_TLSDESC_CALL:
2862       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2863         {
2864           // See above comment for R_X86_64_TLSGD.
2865           optimized_type = tls::TLSOPT_NONE;
2866         }
2867       if (optimized_type == tls::TLSOPT_TO_LE)
2868         {
2869           gold_assert(tls_segment != NULL);
2870           this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2871                                   rela, r_type, value, view,
2872                                   view_size);
2873           break;
2874         }
2875       else
2876         {
2877           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2878                                    ? GOT_TYPE_TLS_OFFSET
2879                                    : GOT_TYPE_TLS_DESC);
2880           unsigned int got_offset = 0;
2881           if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2882               && optimized_type == tls::TLSOPT_NONE)
2883             {
2884               // We created GOT entries in the .got.tlsdesc portion of
2885               // the .got.plt section, but the offset stored in the
2886               // symbol is the offset within .got.tlsdesc.
2887               got_offset = (target->got_size()
2888                             + target->got_plt_section()->data_size());
2889             }
2890           if (gsym != NULL)
2891             {
2892               gold_assert(gsym->has_got_offset(got_type));
2893               got_offset += gsym->got_offset(got_type) - target->got_size();
2894             }
2895           else
2896             {
2897               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2898               gold_assert(object->local_has_got_offset(r_sym, got_type));
2899               got_offset += (object->local_got_offset(r_sym, got_type)
2900                              - target->got_size());
2901             }
2902           if (optimized_type == tls::TLSOPT_TO_IE)
2903             {
2904               gold_assert(tls_segment != NULL);
2905               value = target->got_plt_section()->address() + got_offset;
2906               this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2907                                       rela, r_type, value, view, address,
2908                                       view_size);
2909               break;
2910             }
2911           else if (optimized_type == tls::TLSOPT_NONE)
2912             {
2913               if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2914                 {
2915                   // Relocate the field with the offset of the pair of GOT
2916                   // entries.
2917                   value = target->got_plt_section()->address() + got_offset;
2918                   Relocate_functions<64, false>::pcrela32(view, value, addend,
2919                                                           address);
2920                 }
2921               break;
2922             }
2923         }
2924       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2925                              _("unsupported reloc %u"), r_type);
2926       break;
2927
2928     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2929       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2930         {
2931           // See above comment for R_X86_64_TLSGD.
2932           optimized_type = tls::TLSOPT_NONE;
2933         }
2934       if (optimized_type == tls::TLSOPT_TO_LE)
2935         {
2936           gold_assert(tls_segment != NULL);
2937           this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2938                              value, view, view_size);
2939           break;
2940         }
2941       else if (optimized_type == tls::TLSOPT_NONE)
2942         {
2943           // Relocate the field with the offset of the GOT entry for
2944           // the module index.
2945           unsigned int got_offset;
2946           got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2947                         - target->got_size());
2948           value = target->got_plt_section()->address() + got_offset;
2949           Relocate_functions<64, false>::pcrela32(view, value, addend,
2950                                                   address);
2951           break;
2952         }
2953       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2954                              _("unsupported reloc %u"), r_type);
2955       break;
2956
2957     case elfcpp::R_X86_64_DTPOFF32:
2958       // This relocation type is used in debugging information.
2959       // In that case we need to not optimize the value.  If the
2960       // section is not executable, then we assume we should not
2961       // optimize this reloc.  See comments above for R_X86_64_TLSGD,
2962       // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
2963       // R_X86_64_TLSLD.
2964       if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2965         {
2966           gold_assert(tls_segment != NULL);
2967           value -= tls_segment->memsz();
2968         }
2969       Relocate_functions<64, false>::rela32(view, value, addend);
2970       break;
2971
2972     case elfcpp::R_X86_64_DTPOFF64:
2973       // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
2974       if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2975         {
2976           gold_assert(tls_segment != NULL);
2977           value -= tls_segment->memsz();
2978         }
2979       Relocate_functions<64, false>::rela64(view, value, addend);
2980       break;
2981
2982     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2983       if (optimized_type == tls::TLSOPT_TO_LE)
2984         {
2985           gold_assert(tls_segment != NULL);
2986           Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2987                                                 rela, r_type, value, view,
2988                                                 view_size);
2989           break;
2990         }
2991       else if (optimized_type == tls::TLSOPT_NONE)
2992         {
2993           // Relocate the field with the offset of the GOT entry for
2994           // the tp-relative offset of the symbol.
2995           unsigned int got_offset;
2996           if (gsym != NULL)
2997             {
2998               gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2999               got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3000                             - target->got_size());
3001             }
3002           else
3003             {
3004               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3005               gold_assert(object->local_has_got_offset(r_sym,
3006                                                        GOT_TYPE_TLS_OFFSET));
3007               got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3008                             - target->got_size());
3009             }
3010           value = target->got_plt_section()->address() + got_offset;
3011           Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3012           break;
3013         }
3014       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3015                              _("unsupported reloc type %u"),
3016                              r_type);
3017       break;
3018
3019     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
3020       value -= tls_segment->memsz();
3021       Relocate_functions<64, false>::rela32(view, value, addend);
3022       break;
3023     }
3024 }
3025
3026 // Do a relocation in which we convert a TLS General-Dynamic to an
3027 // Initial-Exec.
3028
3029 inline void
3030 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
3031                                       size_t relnum,
3032                                       Output_segment*,
3033                                       const elfcpp::Rela<64, false>& rela,
3034                                       unsigned int,
3035                                       elfcpp::Elf_types<64>::Elf_Addr value,
3036                                       unsigned char* view,
3037                                       elfcpp::Elf_types<64>::Elf_Addr address,
3038                                       section_size_type view_size)
3039 {
3040   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3041   // .word 0x6666; rex64; call __tls_get_addr
3042   // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3043
3044   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3045   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3046
3047   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3048                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3049   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3050                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3051
3052   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3053
3054   const elfcpp::Elf_Xword addend = rela.get_r_addend();
3055   Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
3056
3057   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3058   // We can skip it.
3059   this->skip_call_tls_get_addr_ = true;
3060 }
3061
3062 // Do a relocation in which we convert a TLS General-Dynamic to a
3063 // Local-Exec.
3064
3065 inline void
3066 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
3067                                       size_t relnum,
3068                                       Output_segment* tls_segment,
3069                                       const elfcpp::Rela<64, false>& rela,
3070                                       unsigned int,
3071                                       elfcpp::Elf_types<64>::Elf_Addr value,
3072                                       unsigned char* view,
3073                                       section_size_type view_size)
3074 {
3075   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3076   // .word 0x6666; rex64; call __tls_get_addr
3077   // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3078
3079   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3080   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3081
3082   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3083                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3084   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3085                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3086
3087   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
3088
3089   value -= tls_segment->memsz();
3090   Relocate_functions<64, false>::rela32(view + 8, value, 0);
3091
3092   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3093   // We can skip it.
3094   this->skip_call_tls_get_addr_ = true;
3095 }
3096
3097 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3098
3099 inline void
3100 Target_x86_64::Relocate::tls_desc_gd_to_ie(
3101     const Relocate_info<64, false>* relinfo,
3102     size_t relnum,
3103     Output_segment*,
3104     const elfcpp::Rela<64, false>& rela,
3105     unsigned int r_type,
3106     elfcpp::Elf_types<64>::Elf_Addr value,
3107     unsigned char* view,
3108     elfcpp::Elf_types<64>::Elf_Addr address,
3109     section_size_type view_size)
3110 {
3111   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3112     {
3113       // leaq foo@tlsdesc(%rip), %rax
3114       // ==> movq foo@gottpoff(%rip), %rax
3115       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3116       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3117       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3118                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3119       view[-2] = 0x8b;
3120       const elfcpp::Elf_Xword addend = rela.get_r_addend();
3121       Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3122     }
3123   else
3124     {
3125       // call *foo@tlscall(%rax)
3126       // ==> nop; nop
3127       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3128       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3129       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3130                      view[0] == 0xff && view[1] == 0x10);
3131       view[0] = 0x66;
3132       view[1] = 0x90;
3133     }
3134 }
3135
3136 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3137
3138 inline void
3139 Target_x86_64::Relocate::tls_desc_gd_to_le(
3140     const Relocate_info<64, false>* relinfo,
3141     size_t relnum,
3142     Output_segment* tls_segment,
3143     const elfcpp::Rela<64, false>& rela,
3144     unsigned int r_type,
3145     elfcpp::Elf_types<64>::Elf_Addr value,
3146     unsigned char* view,
3147     section_size_type view_size)
3148 {
3149   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3150     {
3151       // leaq foo@tlsdesc(%rip), %rax
3152       // ==> movq foo@tpoff, %rax
3153       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3154       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3155       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3156                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3157       view[-2] = 0xc7;
3158       view[-1] = 0xc0;
3159       value -= tls_segment->memsz();
3160       Relocate_functions<64, false>::rela32(view, value, 0);
3161     }
3162   else
3163     {
3164       // call *foo@tlscall(%rax)
3165       // ==> nop; nop
3166       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3167       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3168       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3169                      view[0] == 0xff && view[1] == 0x10);
3170       view[0] = 0x66;
3171       view[1] = 0x90;
3172     }
3173 }
3174
3175 inline void
3176 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
3177                                       size_t relnum,
3178                                       Output_segment*,
3179                                       const elfcpp::Rela<64, false>& rela,
3180                                       unsigned int,
3181                                       elfcpp::Elf_types<64>::Elf_Addr,
3182                                       unsigned char* view,
3183                                       section_size_type view_size)
3184 {
3185   // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3186   // ... leq foo@dtpoff(%rax),%reg
3187   // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3188
3189   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3190   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
3191
3192   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3193                  view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3194
3195   tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3196
3197   memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3198
3199   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3200   // We can skip it.
3201   this->skip_call_tls_get_addr_ = true;
3202 }
3203
3204 // Do a relocation in which we convert a TLS Initial-Exec to a
3205 // Local-Exec.
3206
3207 inline void
3208 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
3209                                       size_t relnum,
3210                                       Output_segment* tls_segment,
3211                                       const elfcpp::Rela<64, false>& rela,
3212                                       unsigned int,
3213                                       elfcpp::Elf_types<64>::Elf_Addr value,
3214                                       unsigned char* view,
3215                                       section_size_type view_size)
3216 {
3217   // We need to examine the opcodes to figure out which instruction we
3218   // are looking at.
3219
3220   // movq foo@gottpoff(%rip),%reg  ==>  movq $YY,%reg
3221   // addq foo@gottpoff(%rip),%reg  ==>  addq $YY,%reg
3222
3223   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3224   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3225
3226   unsigned char op1 = view[-3];
3227   unsigned char op2 = view[-2];
3228   unsigned char op3 = view[-1];
3229   unsigned char reg = op3 >> 3;
3230
3231   if (op2 == 0x8b)
3232     {
3233       // movq
3234       if (op1 == 0x4c)
3235         view[-3] = 0x49;
3236       view[-2] = 0xc7;
3237       view[-1] = 0xc0 | reg;
3238     }
3239   else if (reg == 4)
3240     {
3241       // Special handling for %rsp.
3242       if (op1 == 0x4c)
3243         view[-3] = 0x49;
3244       view[-2] = 0x81;
3245       view[-1] = 0xc0 | reg;
3246     }
3247   else
3248     {
3249       // addq
3250       if (op1 == 0x4c)
3251         view[-3] = 0x4d;
3252       view[-2] = 0x8d;
3253       view[-1] = 0x80 | reg | (reg << 3);
3254     }
3255
3256   value -= tls_segment->memsz();
3257   Relocate_functions<64, false>::rela32(view, value, 0);
3258 }
3259
3260 // Relocate section data.
3261
3262 void
3263 Target_x86_64::relocate_section(
3264     const Relocate_info<64, false>* relinfo,
3265     unsigned int sh_type,
3266     const unsigned char* prelocs,
3267     size_t reloc_count,
3268     Output_section* output_section,
3269     bool needs_special_offset_handling,
3270     unsigned char* view,
3271     elfcpp::Elf_types<64>::Elf_Addr address,
3272     section_size_type view_size,
3273     const Reloc_symbol_changes* reloc_symbol_changes)
3274 {
3275   gold_assert(sh_type == elfcpp::SHT_RELA);
3276
3277   gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3278                          Target_x86_64::Relocate>(
3279     relinfo,
3280     this,
3281     prelocs,
3282     reloc_count,
3283     output_section,
3284     needs_special_offset_handling,
3285     view,
3286     address,
3287     view_size,
3288     reloc_symbol_changes);
3289 }
3290
3291 // Apply an incremental relocation.  Incremental relocations always refer
3292 // to global symbols.
3293
3294 void
3295 Target_x86_64::apply_relocation(
3296     const Relocate_info<64, false>* relinfo,
3297     elfcpp::Elf_types<64>::Elf_Addr r_offset,
3298     unsigned int r_type,
3299     elfcpp::Elf_types<64>::Elf_Swxword r_addend,
3300     const Symbol* gsym,
3301     unsigned char* view,
3302     elfcpp::Elf_types<64>::Elf_Addr address,
3303     section_size_type view_size)
3304 {
3305   gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>(
3306     relinfo,
3307     this,
3308     r_offset,
3309     r_type,
3310     r_addend,
3311     gsym,
3312     view,
3313     address,
3314     view_size);
3315 }
3316
3317 // Return the size of a relocation while scanning during a relocatable
3318 // link.
3319
3320 unsigned int
3321 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3322     unsigned int r_type,
3323     Relobj* object)
3324 {
3325   switch (r_type)
3326     {
3327     case elfcpp::R_X86_64_NONE:
3328     case elfcpp::R_X86_64_GNU_VTINHERIT:
3329     case elfcpp::R_X86_64_GNU_VTENTRY:
3330     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
3331     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
3332     case elfcpp::R_X86_64_TLSDESC_CALL:
3333     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
3334     case elfcpp::R_X86_64_DTPOFF32:
3335     case elfcpp::R_X86_64_DTPOFF64:
3336     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
3337     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
3338       return 0;
3339
3340     case elfcpp::R_X86_64_64:
3341     case elfcpp::R_X86_64_PC64:
3342     case elfcpp::R_X86_64_GOTOFF64:
3343     case elfcpp::R_X86_64_GOTPC64:
3344     case elfcpp::R_X86_64_PLTOFF64:
3345     case elfcpp::R_X86_64_GOT64:
3346     case elfcpp::R_X86_64_GOTPCREL64:
3347     case elfcpp::R_X86_64_GOTPCREL:
3348     case elfcpp::R_X86_64_GOTPLT64:
3349       return 8;
3350
3351     case elfcpp::R_X86_64_32:
3352     case elfcpp::R_X86_64_32S:
3353     case elfcpp::R_X86_64_PC32:
3354     case elfcpp::R_X86_64_PLT32:
3355     case elfcpp::R_X86_64_GOTPC32:
3356     case elfcpp::R_X86_64_GOT32:
3357       return 4;
3358
3359     case elfcpp::R_X86_64_16:
3360     case elfcpp::R_X86_64_PC16:
3361       return 2;
3362
3363     case elfcpp::R_X86_64_8:
3364     case elfcpp::R_X86_64_PC8:
3365       return 1;
3366
3367     case elfcpp::R_X86_64_COPY:
3368     case elfcpp::R_X86_64_GLOB_DAT:
3369     case elfcpp::R_X86_64_JUMP_SLOT:
3370     case elfcpp::R_X86_64_RELATIVE:
3371     case elfcpp::R_X86_64_IRELATIVE:
3372       // These are outstanding tls relocs, which are unexpected when linking
3373     case elfcpp::R_X86_64_TPOFF64:
3374     case elfcpp::R_X86_64_DTPMOD64:
3375     case elfcpp::R_X86_64_TLSDESC:
3376       object->error(_("unexpected reloc %u in object file"), r_type);
3377       return 0;
3378
3379     case elfcpp::R_X86_64_SIZE32:
3380     case elfcpp::R_X86_64_SIZE64:
3381     default:
3382       object->error(_("unsupported reloc %u against local symbol"), r_type);
3383       return 0;
3384     }
3385 }
3386
3387 // Scan the relocs during a relocatable link.
3388
3389 void
3390 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
3391                                        Layout* layout,
3392                                        Sized_relobj_file<64, false>* object,
3393                                        unsigned int data_shndx,
3394                                        unsigned int sh_type,
3395                                        const unsigned char* prelocs,
3396                                        size_t reloc_count,
3397                                        Output_section* output_section,
3398                                        bool needs_special_offset_handling,
3399                                        size_t local_symbol_count,
3400                                        const unsigned char* plocal_symbols,
3401                                        Relocatable_relocs* rr)
3402 {
3403   gold_assert(sh_type == elfcpp::SHT_RELA);
3404
3405   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3406     Relocatable_size_for_reloc> Scan_relocatable_relocs;
3407
3408   gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
3409       Scan_relocatable_relocs>(
3410     symtab,
3411     layout,
3412     object,
3413     data_shndx,
3414     prelocs,
3415     reloc_count,
3416     output_section,
3417     needs_special_offset_handling,
3418     local_symbol_count,
3419     plocal_symbols,
3420     rr);
3421 }
3422
3423 // Relocate a section during a relocatable link.
3424
3425 void
3426 Target_x86_64::relocate_for_relocatable(
3427     const Relocate_info<64, false>* relinfo,
3428     unsigned int sh_type,
3429     const unsigned char* prelocs,
3430     size_t reloc_count,
3431     Output_section* output_section,
3432     off_t offset_in_output_section,
3433     const Relocatable_relocs* rr,
3434     unsigned char* view,
3435     elfcpp::Elf_types<64>::Elf_Addr view_address,
3436     section_size_type view_size,
3437     unsigned char* reloc_view,
3438     section_size_type reloc_view_size)
3439 {
3440   gold_assert(sh_type == elfcpp::SHT_RELA);
3441
3442   gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
3443     relinfo,
3444     prelocs,
3445     reloc_count,
3446     output_section,
3447     offset_in_output_section,
3448     rr,
3449     view,
3450     view_address,
3451     view_size,
3452     reloc_view,
3453     reloc_view_size);
3454 }
3455
3456 // Return the value to use for a dynamic which requires special
3457 // treatment.  This is how we support equality comparisons of function
3458 // pointers across shared library boundaries, as described in the
3459 // processor specific ABI supplement.
3460
3461 uint64_t
3462 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3463 {
3464   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3465   return this->plt_section()->address() + gsym->plt_offset();
3466 }
3467
3468 // Return a string used to fill a code section with nops to take up
3469 // the specified length.
3470
3471 std::string
3472 Target_x86_64::do_code_fill(section_size_type length) const
3473 {
3474   if (length >= 16)
3475     {
3476       // Build a jmpq instruction to skip over the bytes.
3477       unsigned char jmp[5];
3478       jmp[0] = 0xe9;
3479       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3480       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3481               + std::string(length - 5, '\0'));
3482     }
3483
3484   // Nop sequences of various lengths.
3485   const char nop1[1] = { 0x90 };                   // nop
3486   const char nop2[2] = { 0x66, 0x90 };             // xchg %ax %ax
3487   const char nop3[3] = { 0x0f, 0x1f, 0x00 };       // nop (%rax)
3488   const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00};  // nop 0(%rax)
3489   const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00,   // nop 0(%rax,%rax,1)
3490                          0x00 };
3491   const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44,   // nopw 0(%rax,%rax,1)
3492                          0x00, 0x00 };
3493   const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00,   // nopl 0L(%rax)
3494                          0x00, 0x00, 0x00 };
3495   const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00,   // nopl 0L(%rax,%rax,1)
3496                          0x00, 0x00, 0x00, 0x00 };
3497   const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84,   // nopw 0L(%rax,%rax,1)
3498                          0x00, 0x00, 0x00, 0x00,
3499                          0x00 };
3500   const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3501                            0x84, 0x00, 0x00, 0x00,
3502                            0x00, 0x00 };
3503   const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3504                            0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3505                            0x00, 0x00, 0x00 };
3506   const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3507                            0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3508                            0x00, 0x00, 0x00, 0x00 };
3509   const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3510                            0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3511                            0x00, 0x00, 0x00, 0x00,
3512                            0x00 };
3513   const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3514                            0x66, 0x2e, 0x0f, 0x1f, // data16
3515                            0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3516                            0x00, 0x00 };
3517   const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3518                            0x66, 0x66, 0x2e, 0x0f, // data16; data16
3519                            0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3520                            0x00, 0x00, 0x00 };
3521
3522   const char* nops[16] = {
3523     NULL,
3524     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3525     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3526   };
3527
3528   return std::string(nops[length], length);
3529 }
3530
3531 // Return the addend to use for a target specific relocation.  The
3532 // only target specific relocation is R_X86_64_TLSDESC for a local
3533 // symbol.  We want to set the addend is the offset of the local
3534 // symbol in the TLS segment.
3535
3536 uint64_t
3537 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3538                                uint64_t) const
3539 {
3540   gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3541   uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3542   gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3543   const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3544   const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3545   gold_assert(psymval->is_tls_symbol());
3546   // The value of a TLS symbol is the offset in the TLS segment.
3547   return psymval->value(ti.object, 0);
3548 }
3549
3550 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3551 // compiled with -fsplit-stack.  The function calls non-split-stack
3552 // code.  We have to change the function so that it always ensures
3553 // that it has enough stack space to run some random function.
3554
3555 void
3556 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3557                                   section_offset_type fnoffset,
3558                                   section_size_type fnsize,
3559                                   unsigned char* view,
3560                                   section_size_type view_size,
3561                                   std::string* from,
3562                                   std::string* to) const
3563 {
3564   // The function starts with a comparison of the stack pointer and a
3565   // field in the TCB.  This is followed by a jump.
3566
3567   // cmp %fs:NN,%rsp
3568   if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3569       && fnsize > 9)
3570     {
3571       // We will call __morestack if the carry flag is set after this
3572       // comparison.  We turn the comparison into an stc instruction
3573       // and some nops.
3574       view[fnoffset] = '\xf9';
3575       this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3576     }
3577   // lea NN(%rsp),%r10
3578   // lea NN(%rsp),%r11
3579   else if ((this->match_view(view, view_size, fnoffset,
3580                              "\x4c\x8d\x94\x24", 4)
3581             || this->match_view(view, view_size, fnoffset,
3582                                 "\x4c\x8d\x9c\x24", 4))
3583            && fnsize > 8)
3584     {
3585       // This is loading an offset from the stack pointer for a
3586       // comparison.  The offset is negative, so we decrease the
3587       // offset by the amount of space we need for the stack.  This
3588       // means we will avoid calling __morestack if there happens to
3589       // be plenty of space on the stack already.
3590       unsigned char* pval = view + fnoffset + 4;
3591       uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3592       val -= parameters->options().split_stack_adjust_size();
3593       elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3594     }
3595   else
3596     {
3597       if (!object->has_no_split_stack())
3598         object->error(_("failed to match split-stack sequence at "
3599                         "section %u offset %0zx"),
3600                       shndx, static_cast<size_t>(fnoffset));
3601       return;
3602     }
3603
3604   // We have to change the function so that it calls
3605   // __morestack_non_split instead of __morestack.  The former will
3606   // allocate additional stack space.
3607   *from = "__morestack";
3608   *to = "__morestack_non_split";
3609 }
3610
3611 // The selector for x86_64 object files.
3612
3613 class Target_selector_x86_64 : public Target_selector_freebsd
3614 {
3615 public:
3616   Target_selector_x86_64()
3617     : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
3618                               "elf64-x86-64-freebsd")
3619   { }
3620
3621   Target*
3622   do_instantiate_target()
3623   { return new Target_x86_64(); }
3624
3625 };
3626
3627 Target_selector_x86_64 target_selector_x86_64;
3628
3629 } // End anonymous namespace.