* target.h (Target::can_check_for_function_pointers): Rewrite.
[platform/upstream/binutils.git] / gold / x86_64.cc
1 // x86_64.cc -- x86_64 target support for gold.
2
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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   // Hook for a new output section.
221   void
222   do_new_output_section(Output_section*) const;
223
224   // Scan the relocations to look for symbol adjustments.
225   void
226   gc_process_relocs(Symbol_table* symtab,
227                     Layout* layout,
228                     Sized_relobj_file<64, false>* object,
229                     unsigned int data_shndx,
230                     unsigned int sh_type,
231                     const unsigned char* prelocs,
232                     size_t reloc_count,
233                     Output_section* output_section,
234                     bool needs_special_offset_handling,
235                     size_t local_symbol_count,
236                     const unsigned char* plocal_symbols);
237
238   // Scan the relocations to look for symbol adjustments.
239   void
240   scan_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   // Finalize the sections.
253   void
254   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
255
256   // Return the value to use for a dynamic which requires special
257   // treatment.
258   uint64_t
259   do_dynsym_value(const Symbol*) const;
260
261   // Relocate a section.
262   void
263   relocate_section(const Relocate_info<64, false>*,
264                    unsigned int sh_type,
265                    const unsigned char* prelocs,
266                    size_t reloc_count,
267                    Output_section* output_section,
268                    bool needs_special_offset_handling,
269                    unsigned char* view,
270                    elfcpp::Elf_types<64>::Elf_Addr view_address,
271                    section_size_type view_size,
272                    const Reloc_symbol_changes*);
273
274   // Scan the relocs during a relocatable link.
275   void
276   scan_relocatable_relocs(Symbol_table* symtab,
277                           Layout* layout,
278                           Sized_relobj_file<64, false>* object,
279                           unsigned int data_shndx,
280                           unsigned int sh_type,
281                           const unsigned char* prelocs,
282                           size_t reloc_count,
283                           Output_section* output_section,
284                           bool needs_special_offset_handling,
285                           size_t local_symbol_count,
286                           const unsigned char* plocal_symbols,
287                           Relocatable_relocs*);
288
289   // Relocate a section during a relocatable link.
290   void
291   relocate_for_relocatable(const Relocate_info<64, false>*,
292                            unsigned int sh_type,
293                            const unsigned char* prelocs,
294                            size_t reloc_count,
295                            Output_section* output_section,
296                            off_t offset_in_output_section,
297                            const Relocatable_relocs*,
298                            unsigned char* view,
299                            elfcpp::Elf_types<64>::Elf_Addr view_address,
300                            section_size_type view_size,
301                            unsigned char* reloc_view,
302                            section_size_type reloc_view_size);
303
304   // Return a string used to fill a code section with nops.
305   std::string
306   do_code_fill(section_size_type length) const;
307
308   // Return whether SYM is defined by the ABI.
309   bool
310   do_is_defined_by_abi(const Symbol* sym) const
311   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
312
313   // Return the symbol index to use for a target specific relocation.
314   // The only target specific relocation is R_X86_64_TLSDESC for a
315   // local symbol, which is an absolute reloc.
316   unsigned int
317   do_reloc_symbol_index(void*, unsigned int r_type) const
318   {
319     gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
320     return 0;
321   }
322
323   // Return the addend to use for a target specific relocation.
324   uint64_t
325   do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
326
327   // Return the PLT section.
328   Output_data*
329   do_plt_section_for_global(const Symbol*) const
330   { return this->plt_section(); }
331
332   Output_data*
333   do_plt_section_for_local(const Relobj*, unsigned int) const
334   { return this->plt_section(); }
335
336   // This function should be defined in targets that can use relocation
337   // types to determine (implemented in local_reloc_may_be_function_pointer
338   // and global_reloc_may_be_function_pointer)
339   // if a function's pointer is taken.  ICF uses this in safe mode to only
340   // fold those functions whose pointer is defintely not taken.  For x86_64
341   // pie binaries, safe ICF cannot be done by looking at relocation types.
342   bool
343   do_can_check_for_function_pointers() const
344   { return !parameters->options().pie(); }
345
346   // Adjust -fsplit-stack code which calls non-split-stack code.
347   void
348   do_calls_non_split(Relobj* object, unsigned int shndx,
349                      section_offset_type fnoffset, section_size_type fnsize,
350                      unsigned char* view, section_size_type view_size,
351                      std::string* from, std::string* to) const;
352
353   // Return the size of the GOT section.
354   section_size_type
355   got_size() const
356   {
357     gold_assert(this->got_ != NULL);
358     return this->got_->data_size();
359   }
360
361   // Return the number of entries in the GOT.
362   unsigned int
363   got_entry_count() const
364   {
365     if (this->got_ == NULL)
366       return 0;
367     return this->got_size() / 8;
368   }
369
370   // Return the number of entries in the PLT.
371   unsigned int
372   plt_entry_count() const;
373
374   // Return the offset of the first non-reserved PLT entry.
375   unsigned int
376   first_plt_entry_offset() const;
377
378   // Return the size of each PLT entry.
379   unsigned int
380   plt_entry_size() const;
381
382   // Create the GOT section for an incremental update.
383   Output_data_got<64, false>*
384   init_got_plt_for_update(Symbol_table* symtab,
385                           Layout* layout,
386                           unsigned int got_count,
387                           unsigned int plt_count);
388
389   // Reserve a GOT entry for a local symbol, and regenerate any
390   // necessary dynamic relocations.
391   void
392   reserve_local_got_entry(unsigned int got_index,
393                           Sized_relobj<64, false>* obj,
394                           unsigned int r_sym,
395                           unsigned int got_type);
396
397   // Reserve a GOT entry for a global symbol, and regenerate any
398   // necessary dynamic relocations.
399   void
400   reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
401                            unsigned int got_type);
402
403   // Register an existing PLT entry for a global symbol.
404   void
405   register_global_plt_entry(unsigned int plt_index, Symbol* gsym);
406
407   // Force a COPY relocation for a given symbol.
408   void
409   emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
410
411   // Apply an incremental relocation.
412   void
413   apply_relocation(const Relocate_info<64, false>* relinfo,
414                    elfcpp::Elf_types<64>::Elf_Addr r_offset,
415                    unsigned int r_type,
416                    elfcpp::Elf_types<64>::Elf_Swxword r_addend,
417                    const Symbol* gsym,
418                    unsigned char* view,
419                    elfcpp::Elf_types<64>::Elf_Addr address,
420                    section_size_type view_size);
421
422   // Add a new reloc argument, returning the index in the vector.
423   size_t
424   add_tlsdesc_info(Sized_relobj_file<64, false>* object, unsigned int r_sym)
425   {
426     this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
427     return this->tlsdesc_reloc_info_.size() - 1;
428   }
429
430  private:
431   // The class which scans relocations.
432   class Scan
433   {
434   public:
435     Scan()
436       : issued_non_pic_error_(false)
437     { }
438
439     static inline int
440     get_reference_flags(unsigned int r_type);
441
442     inline void
443     local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
444           Sized_relobj_file<64, false>* object,
445           unsigned int data_shndx,
446           Output_section* output_section,
447           const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
448           const elfcpp::Sym<64, false>& lsym);
449
450     inline void
451     global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
452            Sized_relobj_file<64, false>* object,
453            unsigned int data_shndx,
454            Output_section* output_section,
455            const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
456            Symbol* gsym);
457
458     inline bool
459     local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
460                                         Target_x86_64* target,
461                                         Sized_relobj_file<64, false>* object,
462                                         unsigned int data_shndx,
463                                         Output_section* output_section,
464                                         const elfcpp::Rela<64, false>& reloc,
465                                         unsigned int r_type,
466                                         const elfcpp::Sym<64, false>& lsym);
467
468     inline bool
469     global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
470                                          Target_x86_64* target,
471                                          Sized_relobj_file<64, false>* object,
472                                          unsigned int data_shndx,
473                                          Output_section* output_section,
474                                          const elfcpp::Rela<64, false>& reloc,
475                                          unsigned int r_type,
476                                          Symbol* gsym);
477
478   private:
479     static void
480     unsupported_reloc_local(Sized_relobj_file<64, false>*, unsigned int r_type);
481
482     static void
483     unsupported_reloc_global(Sized_relobj_file<64, false>*, unsigned int r_type,
484                              Symbol*);
485
486     void
487     check_non_pic(Relobj*, unsigned int r_type, Symbol*);
488
489     inline bool
490     possible_function_pointer_reloc(unsigned int r_type);
491
492     bool
493     reloc_needs_plt_for_ifunc(Sized_relobj_file<64, false>*,
494                               unsigned int r_type);
495
496     // Whether we have issued an error about a non-PIC compilation.
497     bool issued_non_pic_error_;
498   };
499
500   // The class which implements relocation.
501   class Relocate
502   {
503    public:
504     Relocate()
505       : skip_call_tls_get_addr_(false)
506     { }
507
508     ~Relocate()
509     {
510       if (this->skip_call_tls_get_addr_)
511         {
512           // FIXME: This needs to specify the location somehow.
513           gold_error(_("missing expected TLS relocation"));
514         }
515     }
516
517     // Do a relocation.  Return false if the caller should not issue
518     // any warnings about this relocation.
519     inline bool
520     relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
521              size_t relnum, const elfcpp::Rela<64, false>&,
522              unsigned int r_type, const Sized_symbol<64>*,
523              const Symbol_value<64>*,
524              unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
525              section_size_type);
526
527    private:
528     // Do a TLS relocation.
529     inline void
530     relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
531                  size_t relnum, const elfcpp::Rela<64, false>&,
532                  unsigned int r_type, const Sized_symbol<64>*,
533                  const Symbol_value<64>*,
534                  unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
535                  section_size_type);
536
537     // Do a TLS General-Dynamic to Initial-Exec transition.
538     inline void
539     tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
540                  Output_segment* tls_segment,
541                  const elfcpp::Rela<64, false>&, unsigned int r_type,
542                  elfcpp::Elf_types<64>::Elf_Addr value,
543                  unsigned char* view,
544                  elfcpp::Elf_types<64>::Elf_Addr,
545                  section_size_type view_size);
546
547     // Do a TLS General-Dynamic to Local-Exec transition.
548     inline void
549     tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
550                  Output_segment* tls_segment,
551                  const elfcpp::Rela<64, false>&, unsigned int r_type,
552                  elfcpp::Elf_types<64>::Elf_Addr value,
553                  unsigned char* view,
554                  section_size_type view_size);
555
556     // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
557     inline void
558     tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
559                       Output_segment* tls_segment,
560                       const elfcpp::Rela<64, false>&, unsigned int r_type,
561                       elfcpp::Elf_types<64>::Elf_Addr value,
562                       unsigned char* view,
563                       elfcpp::Elf_types<64>::Elf_Addr,
564                       section_size_type view_size);
565
566     // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
567     inline void
568     tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
569                       Output_segment* tls_segment,
570                       const elfcpp::Rela<64, false>&, unsigned int r_type,
571                       elfcpp::Elf_types<64>::Elf_Addr value,
572                       unsigned char* view,
573                       section_size_type view_size);
574
575     // Do a TLS Local-Dynamic to Local-Exec transition.
576     inline void
577     tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
578                  Output_segment* tls_segment,
579                  const elfcpp::Rela<64, false>&, unsigned int r_type,
580                  elfcpp::Elf_types<64>::Elf_Addr value,
581                  unsigned char* view,
582                  section_size_type view_size);
583
584     // Do a TLS Initial-Exec to Local-Exec transition.
585     static inline void
586     tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
587                  Output_segment* tls_segment,
588                  const elfcpp::Rela<64, false>&, unsigned int r_type,
589                  elfcpp::Elf_types<64>::Elf_Addr value,
590                  unsigned char* view,
591                  section_size_type view_size);
592
593     // This is set if we should skip the next reloc, which should be a
594     // PLT32 reloc against ___tls_get_addr.
595     bool skip_call_tls_get_addr_;
596   };
597
598   // A class which returns the size required for a relocation type,
599   // used while scanning relocs during a relocatable link.
600   class Relocatable_size_for_reloc
601   {
602    public:
603     unsigned int
604     get_size_for_reloc(unsigned int, Relobj*);
605   };
606
607   // Adjust TLS relocation type based on the options and whether this
608   // is a local symbol.
609   static tls::Tls_optimization
610   optimize_tls_reloc(bool is_final, int r_type);
611
612   // Get the GOT section, creating it if necessary.
613   Output_data_got<64, false>*
614   got_section(Symbol_table*, Layout*);
615
616   // Get the GOT PLT section.
617   Output_data_space*
618   got_plt_section() const
619   {
620     gold_assert(this->got_plt_ != NULL);
621     return this->got_plt_;
622   }
623
624   // Get the GOT section for TLSDESC entries.
625   Output_data_got<64, false>*
626   got_tlsdesc_section() const
627   {
628     gold_assert(this->got_tlsdesc_ != NULL);
629     return this->got_tlsdesc_;
630   }
631
632   // Create the PLT section.
633   void
634   make_plt_section(Symbol_table* symtab, Layout* layout);
635
636   // Create a PLT entry for a global symbol.
637   void
638   make_plt_entry(Symbol_table*, Layout*, Symbol*);
639
640   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
641   void
642   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
643                              Sized_relobj_file<64, false>* relobj,
644                              unsigned int local_sym_index);
645
646   // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
647   void
648   define_tls_base_symbol(Symbol_table*, Layout*);
649
650   // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
651   void
652   reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
653
654   // Create a GOT entry for the TLS module index.
655   unsigned int
656   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
657                       Sized_relobj_file<64, false>* object);
658
659   // Get the PLT section.
660   Output_data_plt_x86_64*
661   plt_section() const
662   {
663     gold_assert(this->plt_ != NULL);
664     return this->plt_;
665   }
666
667   // Get the dynamic reloc section, creating it if necessary.
668   Reloc_section*
669   rela_dyn_section(Layout*);
670
671   // Get the section to use for TLSDESC relocations.
672   Reloc_section*
673   rela_tlsdesc_section(Layout*) const;
674
675   // Add a potential copy relocation.
676   void
677   copy_reloc(Symbol_table* symtab, Layout* layout,
678              Sized_relobj_file<64, false>* object,
679              unsigned int shndx, Output_section* output_section,
680              Symbol* sym, const elfcpp::Rela<64, false>& reloc)
681   {
682     this->copy_relocs_.copy_reloc(symtab, layout,
683                                   symtab->get_sized_symbol<64>(sym),
684                                   object, shndx, output_section,
685                                   reloc, this->rela_dyn_section(layout));
686   }
687
688   // Information about this specific target which we pass to the
689   // general Target structure.
690   static const Target::Target_info x86_64_info;
691
692   // The types of GOT entries needed for this platform.
693   // These values are exposed to the ABI in an incremental link.
694   // Do not renumber existing values without changing the version
695   // number of the .gnu_incremental_inputs section.
696   enum Got_type
697   {
698     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
699     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
700     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
701     GOT_TYPE_TLS_DESC = 3       // GOT entry for TLS_DESC pair
702   };
703
704   // This type is used as the argument to the target specific
705   // relocation routines.  The only target specific reloc is
706   // R_X86_64_TLSDESC against a local symbol.
707   struct Tlsdesc_info
708   {
709     Tlsdesc_info(Sized_relobj_file<64, false>* a_object, unsigned int a_r_sym)
710       : object(a_object), r_sym(a_r_sym)
711     { }
712
713     // The object in which the local symbol is defined.
714     Sized_relobj_file<64, false>* object;
715     // The local symbol index in the object.
716     unsigned int r_sym;
717   };
718
719   // The GOT section.
720   Output_data_got<64, false>* got_;
721   // The PLT section.
722   Output_data_plt_x86_64* plt_;
723   // The GOT PLT section.
724   Output_data_space* got_plt_;
725   // The GOT section for TLSDESC relocations.
726   Output_data_got<64, false>* got_tlsdesc_;
727   // The _GLOBAL_OFFSET_TABLE_ symbol.
728   Symbol* global_offset_table_;
729   // The dynamic reloc section.
730   Reloc_section* rela_dyn_;
731   // Relocs saved to avoid a COPY reloc.
732   Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
733   // Space for variables copied with a COPY reloc.
734   Output_data_space* dynbss_;
735   // Offset of the GOT entry for the TLS module index.
736   unsigned int got_mod_index_offset_;
737   // We handle R_X86_64_TLSDESC against a local symbol as a target
738   // specific relocation.  Here we store the object and local symbol
739   // index for the relocation.
740   std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
741   // True if the _TLS_MODULE_BASE_ symbol has been defined.
742   bool tls_base_symbol_defined_;
743 };
744
745 const Target::Target_info Target_x86_64::x86_64_info =
746 {
747   64,                   // size
748   false,                // is_big_endian
749   elfcpp::EM_X86_64,    // machine_code
750   false,                // has_make_symbol
751   false,                // has_resolve
752   true,                 // has_code_fill
753   true,                 // is_default_stack_executable
754   true,                 // can_icf_inline_merge_sections
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_fallback(_("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 // Force a COPY relocation for a given symbol.
1393
1394 void
1395 Target_x86_64::emit_copy_reloc(
1396     Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
1397 {
1398   this->copy_relocs_.emit_copy_reloc(symtab,
1399                                      symtab->get_sized_symbol<64>(sym),
1400                                      os,
1401                                      offset,
1402                                      this->rela_dyn_section(NULL));
1403 }
1404
1405 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1406
1407 void
1408 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1409 {
1410   if (this->tls_base_symbol_defined_)
1411     return;
1412
1413   Output_segment* tls_segment = layout->tls_segment();
1414   if (tls_segment != NULL)
1415     {
1416       bool is_exec = parameters->options().output_is_executable();
1417       symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1418                                        Symbol_table::PREDEFINED,
1419                                        tls_segment, 0, 0,
1420                                        elfcpp::STT_TLS,
1421                                        elfcpp::STB_LOCAL,
1422                                        elfcpp::STV_HIDDEN, 0,
1423                                        (is_exec
1424                                         ? Symbol::SEGMENT_END
1425                                         : Symbol::SEGMENT_START),
1426                                        true);
1427     }
1428   this->tls_base_symbol_defined_ = true;
1429 }
1430
1431 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1432
1433 void
1434 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1435                                              Layout* layout)
1436 {
1437   if (this->plt_ == NULL)
1438     this->make_plt_section(symtab, layout);
1439
1440   if (!this->plt_->has_tlsdesc_entry())
1441     {
1442       // Allocate the TLSDESC_GOT entry.
1443       Output_data_got<64, false>* got = this->got_section(symtab, layout);
1444       unsigned int got_offset = got->add_constant(0);
1445
1446       // Allocate the TLSDESC_PLT entry.
1447       this->plt_->reserve_tlsdesc_entry(got_offset);
1448     }
1449 }
1450
1451 // Create a GOT entry for the TLS module index.
1452
1453 unsigned int
1454 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1455                                    Sized_relobj_file<64, false>* object)
1456 {
1457   if (this->got_mod_index_offset_ == -1U)
1458     {
1459       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1460       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1461       Output_data_got<64, false>* got = this->got_section(symtab, layout);
1462       unsigned int got_offset = got->add_constant(0);
1463       rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1464                           got_offset, 0);
1465       got->add_constant(0);
1466       this->got_mod_index_offset_ = got_offset;
1467     }
1468   return this->got_mod_index_offset_;
1469 }
1470
1471 // Optimize the TLS relocation type based on what we know about the
1472 // symbol.  IS_FINAL is true if the final address of this symbol is
1473 // known at link time.
1474
1475 tls::Tls_optimization
1476 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1477 {
1478   // If we are generating a shared library, then we can't do anything
1479   // in the linker.
1480   if (parameters->options().shared())
1481     return tls::TLSOPT_NONE;
1482
1483   switch (r_type)
1484     {
1485     case elfcpp::R_X86_64_TLSGD:
1486     case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1487     case elfcpp::R_X86_64_TLSDESC_CALL:
1488       // These are General-Dynamic which permits fully general TLS
1489       // access.  Since we know that we are generating an executable,
1490       // we can convert this to Initial-Exec.  If we also know that
1491       // this is a local symbol, we can further switch to Local-Exec.
1492       if (is_final)
1493         return tls::TLSOPT_TO_LE;
1494       return tls::TLSOPT_TO_IE;
1495
1496     case elfcpp::R_X86_64_TLSLD:
1497       // This is Local-Dynamic, which refers to a local symbol in the
1498       // dynamic TLS block.  Since we know that we generating an
1499       // executable, we can switch to Local-Exec.
1500       return tls::TLSOPT_TO_LE;
1501
1502     case elfcpp::R_X86_64_DTPOFF32:
1503     case elfcpp::R_X86_64_DTPOFF64:
1504       // Another Local-Dynamic reloc.
1505       return tls::TLSOPT_TO_LE;
1506
1507     case elfcpp::R_X86_64_GOTTPOFF:
1508       // These are Initial-Exec relocs which get the thread offset
1509       // from the GOT.  If we know that we are linking against the
1510       // local symbol, we can switch to Local-Exec, which links the
1511       // thread offset into the instruction.
1512       if (is_final)
1513         return tls::TLSOPT_TO_LE;
1514       return tls::TLSOPT_NONE;
1515
1516     case elfcpp::R_X86_64_TPOFF32:
1517       // When we already have Local-Exec, there is nothing further we
1518       // can do.
1519       return tls::TLSOPT_NONE;
1520
1521     default:
1522       gold_unreachable();
1523     }
1524 }
1525
1526 // Get the Reference_flags for a particular relocation.
1527
1528 int
1529 Target_x86_64::Scan::get_reference_flags(unsigned int r_type)
1530 {
1531   switch (r_type)
1532     {
1533     case elfcpp::R_X86_64_NONE:
1534     case elfcpp::R_X86_64_GNU_VTINHERIT:
1535     case elfcpp::R_X86_64_GNU_VTENTRY:
1536     case elfcpp::R_X86_64_GOTPC32:
1537     case elfcpp::R_X86_64_GOTPC64:
1538       // No symbol reference.
1539       return 0;
1540
1541     case elfcpp::R_X86_64_64:
1542     case elfcpp::R_X86_64_32:
1543     case elfcpp::R_X86_64_32S:
1544     case elfcpp::R_X86_64_16:
1545     case elfcpp::R_X86_64_8:
1546       return Symbol::ABSOLUTE_REF;
1547
1548     case elfcpp::R_X86_64_PC64:
1549     case elfcpp::R_X86_64_PC32:
1550     case elfcpp::R_X86_64_PC16:
1551     case elfcpp::R_X86_64_PC8:
1552     case elfcpp::R_X86_64_GOTOFF64:
1553       return Symbol::RELATIVE_REF;
1554
1555     case elfcpp::R_X86_64_PLT32:
1556     case elfcpp::R_X86_64_PLTOFF64:
1557       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1558
1559     case elfcpp::R_X86_64_GOT64:
1560     case elfcpp::R_X86_64_GOT32:
1561     case elfcpp::R_X86_64_GOTPCREL64:
1562     case elfcpp::R_X86_64_GOTPCREL:
1563     case elfcpp::R_X86_64_GOTPLT64:
1564       // Absolute in GOT.
1565       return Symbol::ABSOLUTE_REF;
1566
1567     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1568     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1569     case elfcpp::R_X86_64_TLSDESC_CALL:
1570     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1571     case elfcpp::R_X86_64_DTPOFF32:
1572     case elfcpp::R_X86_64_DTPOFF64:
1573     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1574     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1575       return Symbol::TLS_REF;
1576
1577     case elfcpp::R_X86_64_COPY:
1578     case elfcpp::R_X86_64_GLOB_DAT:
1579     case elfcpp::R_X86_64_JUMP_SLOT:
1580     case elfcpp::R_X86_64_RELATIVE:
1581     case elfcpp::R_X86_64_IRELATIVE:
1582     case elfcpp::R_X86_64_TPOFF64:
1583     case elfcpp::R_X86_64_DTPMOD64:
1584     case elfcpp::R_X86_64_TLSDESC:
1585     case elfcpp::R_X86_64_SIZE32:
1586     case elfcpp::R_X86_64_SIZE64:
1587     default:
1588       // Not expected.  We will give an error later.
1589       return 0;
1590     }
1591 }
1592
1593 // Report an unsupported relocation against a local symbol.
1594
1595 void
1596 Target_x86_64::Scan::unsupported_reloc_local(
1597      Sized_relobj_file<64, false>* object,
1598      unsigned int r_type)
1599 {
1600   gold_error(_("%s: unsupported reloc %u against local symbol"),
1601              object->name().c_str(), r_type);
1602 }
1603
1604 // We are about to emit a dynamic relocation of type R_TYPE.  If the
1605 // dynamic linker does not support it, issue an error.  The GNU linker
1606 // only issues a non-PIC error for an allocated read-only section.
1607 // Here we know the section is allocated, but we don't know that it is
1608 // read-only.  But we check for all the relocation types which the
1609 // glibc dynamic linker supports, so it seems appropriate to issue an
1610 // error even if the section is not read-only.  If GSYM is not NULL,
1611 // it is the symbol the relocation is against; if it is NULL, the
1612 // relocation is against a local symbol.
1613
1614 void
1615 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type,
1616                                    Symbol* gsym)
1617 {
1618   switch (r_type)
1619     {
1620       // These are the relocation types supported by glibc for x86_64
1621       // which should always work.
1622     case elfcpp::R_X86_64_RELATIVE:
1623     case elfcpp::R_X86_64_IRELATIVE:
1624     case elfcpp::R_X86_64_GLOB_DAT:
1625     case elfcpp::R_X86_64_JUMP_SLOT:
1626     case elfcpp::R_X86_64_DTPMOD64:
1627     case elfcpp::R_X86_64_DTPOFF64:
1628     case elfcpp::R_X86_64_TPOFF64:
1629     case elfcpp::R_X86_64_64:
1630     case elfcpp::R_X86_64_COPY:
1631       return;
1632
1633       // glibc supports these reloc types, but they can overflow.
1634     case elfcpp::R_X86_64_PC32:
1635       // A PC relative reference is OK against a local symbol or if
1636       // the symbol is defined locally.
1637       if (gsym == NULL
1638           || (!gsym->is_from_dynobj()
1639               && !gsym->is_undefined()
1640               && !gsym->is_preemptible()))
1641         return;
1642       /* Fall through.  */
1643     case elfcpp::R_X86_64_32:
1644       if (this->issued_non_pic_error_)
1645         return;
1646       gold_assert(parameters->options().output_is_position_independent());
1647       if (gsym == NULL)
1648         object->error(_("requires dynamic R_X86_64_32 reloc which may "
1649                         "overflow at runtime; recompile with -fPIC"));
1650       else
1651         object->error(_("requires dynamic %s reloc against '%s' which may "
1652                         "overflow at runtime; recompile with -fPIC"),
1653                       (r_type == elfcpp::R_X86_64_32
1654                        ? "R_X86_64_32"
1655                        : "R_X86_64_PC32"),
1656                       gsym->name());
1657       this->issued_non_pic_error_ = true;
1658       return;
1659
1660     default:
1661       // This prevents us from issuing more than one error per reloc
1662       // section.  But we can still wind up issuing more than one
1663       // error per object file.
1664       if (this->issued_non_pic_error_)
1665         return;
1666       gold_assert(parameters->options().output_is_position_independent());
1667       object->error(_("requires unsupported dynamic reloc %u; "
1668                       "recompile with -fPIC"),
1669                     r_type);
1670       this->issued_non_pic_error_ = true;
1671       return;
1672
1673     case elfcpp::R_X86_64_NONE:
1674       gold_unreachable();
1675     }
1676 }
1677
1678 // Return whether we need to make a PLT entry for a relocation of the
1679 // given type against a STT_GNU_IFUNC symbol.
1680
1681 bool
1682 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(
1683      Sized_relobj_file<64, false>* object,
1684      unsigned int r_type)
1685 {
1686   int flags = Scan::get_reference_flags(r_type);
1687   if (flags & Symbol::TLS_REF)
1688     gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1689                object->name().c_str(), r_type);
1690   return flags != 0;
1691 }
1692
1693 // Scan a relocation for a local symbol.
1694
1695 inline void
1696 Target_x86_64::Scan::local(Symbol_table* symtab,
1697                            Layout* layout,
1698                            Target_x86_64* target,
1699                            Sized_relobj_file<64, false>* object,
1700                            unsigned int data_shndx,
1701                            Output_section* output_section,
1702                            const elfcpp::Rela<64, false>& reloc,
1703                            unsigned int r_type,
1704                            const elfcpp::Sym<64, false>& lsym)
1705 {
1706   // A local STT_GNU_IFUNC symbol may require a PLT entry.
1707   if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1708       && this->reloc_needs_plt_for_ifunc(object, r_type))
1709     {
1710       unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1711       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1712     }
1713
1714   switch (r_type)
1715     {
1716     case elfcpp::R_X86_64_NONE:
1717     case elfcpp::R_X86_64_GNU_VTINHERIT:
1718     case elfcpp::R_X86_64_GNU_VTENTRY:
1719       break;
1720
1721     case elfcpp::R_X86_64_64:
1722       // If building a shared library (or a position-independent
1723       // executable), we need to create a dynamic relocation for this
1724       // location.  The relocation applied at link time will apply the
1725       // link-time value, so we flag the location with an
1726       // R_X86_64_RELATIVE relocation so the dynamic loader can
1727       // relocate it easily.
1728       if (parameters->options().output_is_position_independent())
1729         {
1730           unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1731           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1732           rela_dyn->add_local_relative(object, r_sym,
1733                                        elfcpp::R_X86_64_RELATIVE,
1734                                        output_section, data_shndx,
1735                                        reloc.get_r_offset(),
1736                                        reloc.get_r_addend());
1737         }
1738       break;
1739
1740     case elfcpp::R_X86_64_32:
1741     case elfcpp::R_X86_64_32S:
1742     case elfcpp::R_X86_64_16:
1743     case elfcpp::R_X86_64_8:
1744       // If building a shared library (or a position-independent
1745       // executable), we need to create a dynamic relocation for this
1746       // location.  We can't use an R_X86_64_RELATIVE relocation
1747       // because that is always a 64-bit relocation.
1748       if (parameters->options().output_is_position_independent())
1749         {
1750           this->check_non_pic(object, r_type, NULL);
1751
1752           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1753           unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1754           if (lsym.get_st_type() != elfcpp::STT_SECTION)
1755             rela_dyn->add_local(object, r_sym, r_type, output_section,
1756                                 data_shndx, reloc.get_r_offset(),
1757                                 reloc.get_r_addend());
1758           else
1759             {
1760               gold_assert(lsym.get_st_value() == 0);
1761               unsigned int shndx = lsym.get_st_shndx();
1762               bool is_ordinary;
1763               shndx = object->adjust_sym_shndx(r_sym, shndx,
1764                                                &is_ordinary);
1765               if (!is_ordinary)
1766                 object->error(_("section symbol %u has bad shndx %u"),
1767                               r_sym, shndx);
1768               else
1769                 rela_dyn->add_local_section(object, shndx,
1770                                             r_type, output_section,
1771                                             data_shndx, reloc.get_r_offset(),
1772                                             reloc.get_r_addend());
1773             }
1774         }
1775       break;
1776
1777     case elfcpp::R_X86_64_PC64:
1778     case elfcpp::R_X86_64_PC32:
1779     case elfcpp::R_X86_64_PC16:
1780     case elfcpp::R_X86_64_PC8:
1781       break;
1782
1783     case elfcpp::R_X86_64_PLT32:
1784       // Since we know this is a local symbol, we can handle this as a
1785       // PC32 reloc.
1786       break;
1787
1788     case elfcpp::R_X86_64_GOTPC32:
1789     case elfcpp::R_X86_64_GOTOFF64:
1790     case elfcpp::R_X86_64_GOTPC64:
1791     case elfcpp::R_X86_64_PLTOFF64:
1792       // We need a GOT section.
1793       target->got_section(symtab, layout);
1794       // For PLTOFF64, we'd normally want a PLT section, but since we
1795       // know this is a local symbol, no PLT is needed.
1796       break;
1797
1798     case elfcpp::R_X86_64_GOT64:
1799     case elfcpp::R_X86_64_GOT32:
1800     case elfcpp::R_X86_64_GOTPCREL64:
1801     case elfcpp::R_X86_64_GOTPCREL:
1802     case elfcpp::R_X86_64_GOTPLT64:
1803       {
1804         // The symbol requires a GOT entry.
1805         Output_data_got<64, false>* got = target->got_section(symtab, layout);
1806         unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1807
1808         // For a STT_GNU_IFUNC symbol we want the PLT offset.  That
1809         // lets function pointers compare correctly with shared
1810         // libraries.  Otherwise we would need an IRELATIVE reloc.
1811         bool is_new;
1812         if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1813           is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1814         else
1815           is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1816         if (is_new)
1817           {
1818             // If we are generating a shared object, we need to add a
1819             // dynamic relocation for this symbol's GOT entry.
1820             if (parameters->options().output_is_position_independent())
1821               {
1822                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1823                 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1824                 if (r_type != elfcpp::R_X86_64_GOT32)
1825                   {
1826                     unsigned int got_offset =
1827                       object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1828                     rela_dyn->add_local_relative(object, r_sym,
1829                                                  elfcpp::R_X86_64_RELATIVE,
1830                                                  got, got_offset, 0);
1831                   }
1832                 else
1833                   {
1834                     this->check_non_pic(object, r_type, NULL);
1835
1836                     gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1837                     rela_dyn->add_local(
1838                         object, r_sym, r_type, got,
1839                         object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1840                   }
1841               }
1842           }
1843         // For GOTPLT64, we'd normally want a PLT section, but since
1844         // we know this is a local symbol, no PLT is needed.
1845       }
1846       break;
1847
1848     case elfcpp::R_X86_64_COPY:
1849     case elfcpp::R_X86_64_GLOB_DAT:
1850     case elfcpp::R_X86_64_JUMP_SLOT:
1851     case elfcpp::R_X86_64_RELATIVE:
1852     case elfcpp::R_X86_64_IRELATIVE:
1853       // These are outstanding tls relocs, which are unexpected when linking
1854     case elfcpp::R_X86_64_TPOFF64:
1855     case elfcpp::R_X86_64_DTPMOD64:
1856     case elfcpp::R_X86_64_TLSDESC:
1857       gold_error(_("%s: unexpected reloc %u in object file"),
1858                  object->name().c_str(), r_type);
1859       break;
1860
1861       // These are initial tls relocs, which are expected when linking
1862     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1863     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1864     case elfcpp::R_X86_64_TLSDESC_CALL:
1865     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1866     case elfcpp::R_X86_64_DTPOFF32:
1867     case elfcpp::R_X86_64_DTPOFF64:
1868     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1869     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1870       {
1871         bool output_is_shared = parameters->options().shared();
1872         const tls::Tls_optimization optimized_type
1873             = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1874         switch (r_type)
1875           {
1876           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
1877             if (optimized_type == tls::TLSOPT_NONE)
1878               {
1879                 // Create a pair of GOT entries for the module index and
1880                 // dtv-relative offset.
1881                 Output_data_got<64, false>* got
1882                     = target->got_section(symtab, layout);
1883                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1884                 unsigned int shndx = lsym.get_st_shndx();
1885                 bool is_ordinary;
1886                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1887                 if (!is_ordinary)
1888                   object->error(_("local symbol %u has bad shndx %u"),
1889                               r_sym, shndx);
1890                 else
1891                   got->add_local_pair_with_rela(object, r_sym,
1892                                                 shndx,
1893                                                 GOT_TYPE_TLS_PAIR,
1894                                                 target->rela_dyn_section(layout),
1895                                                 elfcpp::R_X86_64_DTPMOD64, 0);
1896               }
1897             else if (optimized_type != tls::TLSOPT_TO_LE)
1898               unsupported_reloc_local(object, r_type);
1899             break;
1900
1901           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1902             target->define_tls_base_symbol(symtab, layout);
1903             if (optimized_type == tls::TLSOPT_NONE)
1904               {
1905                 // Create reserved PLT and GOT entries for the resolver.
1906                 target->reserve_tlsdesc_entries(symtab, layout);
1907
1908                 // Generate a double GOT entry with an
1909                 // R_X86_64_TLSDESC reloc.  The R_X86_64_TLSDESC reloc
1910                 // is resolved lazily, so the GOT entry needs to be in
1911                 // an area in .got.plt, not .got.  Call got_section to
1912                 // make sure the section has been created.
1913                 target->got_section(symtab, layout);
1914                 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1915                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1916                 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1917                   {
1918                     unsigned int got_offset = got->add_constant(0);
1919                     got->add_constant(0);
1920                     object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1921                                                  got_offset);
1922                     Reloc_section* rt = target->rela_tlsdesc_section(layout);
1923                     // We store the arguments we need in a vector, and
1924                     // use the index into the vector as the parameter
1925                     // to pass to the target specific routines.
1926                     uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1927                     void* arg = reinterpret_cast<void*>(intarg);
1928                     rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1929                                             got, got_offset, 0);
1930                   }
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_TLSDESC_CALL:
1937             break;
1938
1939           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
1940             if (optimized_type == tls::TLSOPT_NONE)
1941               {
1942                 // Create a GOT entry for the module index.
1943                 target->got_mod_index_entry(symtab, layout, object);
1944               }
1945             else if (optimized_type != tls::TLSOPT_TO_LE)
1946               unsupported_reloc_local(object, r_type);
1947             break;
1948
1949           case elfcpp::R_X86_64_DTPOFF32:
1950           case elfcpp::R_X86_64_DTPOFF64:
1951             break;
1952
1953           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
1954             layout->set_has_static_tls();
1955             if (optimized_type == tls::TLSOPT_NONE)
1956               {
1957                 // Create a GOT entry for the tp-relative offset.
1958                 Output_data_got<64, false>* got
1959                     = target->got_section(symtab, layout);
1960                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1961                 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1962                                          target->rela_dyn_section(layout),
1963                                          elfcpp::R_X86_64_TPOFF64);
1964               }
1965             else if (optimized_type != tls::TLSOPT_TO_LE)
1966               unsupported_reloc_local(object, r_type);
1967             break;
1968
1969           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
1970             layout->set_has_static_tls();
1971             if (output_is_shared)
1972               unsupported_reloc_local(object, r_type);
1973             break;
1974
1975           default:
1976             gold_unreachable();
1977           }
1978       }
1979       break;
1980
1981     case elfcpp::R_X86_64_SIZE32:
1982     case elfcpp::R_X86_64_SIZE64:
1983     default:
1984       gold_error(_("%s: unsupported reloc %u against local symbol"),
1985                  object->name().c_str(), r_type);
1986       break;
1987     }
1988 }
1989
1990
1991 // Report an unsupported relocation against a global symbol.
1992
1993 void
1994 Target_x86_64::Scan::unsupported_reloc_global(
1995     Sized_relobj_file<64, false>* object,
1996     unsigned int r_type,
1997     Symbol* gsym)
1998 {
1999   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2000              object->name().c_str(), r_type, gsym->demangled_name().c_str());
2001 }
2002
2003 // Returns true if this relocation type could be that of a function pointer.
2004 inline bool
2005 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
2006 {
2007   switch (r_type)
2008     {
2009     case elfcpp::R_X86_64_64:
2010     case elfcpp::R_X86_64_32:
2011     case elfcpp::R_X86_64_32S:
2012     case elfcpp::R_X86_64_16:
2013     case elfcpp::R_X86_64_8:
2014     case elfcpp::R_X86_64_GOT64:
2015     case elfcpp::R_X86_64_GOT32:
2016     case elfcpp::R_X86_64_GOTPCREL64:
2017     case elfcpp::R_X86_64_GOTPCREL:
2018     case elfcpp::R_X86_64_GOTPLT64:
2019       {
2020         return true;
2021       }
2022     }
2023   return false;
2024 }
2025
2026 // For safe ICF, scan a relocation for a local symbol to check if it
2027 // corresponds to a function pointer being taken.  In that case mark
2028 // the function whose pointer was taken as not foldable.
2029
2030 inline bool
2031 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
2032   Symbol_table* ,
2033   Layout* ,
2034   Target_x86_64* ,
2035   Sized_relobj_file<64, false>* ,
2036   unsigned int ,
2037   Output_section* ,
2038   const elfcpp::Rela<64, false>& ,
2039   unsigned int r_type,
2040   const elfcpp::Sym<64, false>&)
2041 {
2042   // When building a shared library, do not fold any local symbols as it is
2043   // not possible to distinguish pointer taken versus a call by looking at
2044   // the relocation types.
2045   return (parameters->options().shared()
2046           || possible_function_pointer_reloc(r_type));
2047 }
2048
2049 // For safe ICF, scan a relocation for a global symbol to check if it
2050 // corresponds to a function pointer being taken.  In that case mark
2051 // the function whose pointer was taken as not foldable.
2052
2053 inline bool
2054 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
2055   Symbol_table*,
2056   Layout* ,
2057   Target_x86_64* ,
2058   Sized_relobj_file<64, false>* ,
2059   unsigned int ,
2060   Output_section* ,
2061   const elfcpp::Rela<64, false>& ,
2062   unsigned int r_type,
2063   Symbol* gsym)
2064 {
2065   // When building a shared library, do not fold symbols whose visibility
2066   // is hidden, internal or protected.
2067   return ((parameters->options().shared()
2068            && (gsym->visibility() == elfcpp::STV_INTERNAL
2069                || gsym->visibility() == elfcpp::STV_PROTECTED
2070                || gsym->visibility() == elfcpp::STV_HIDDEN))
2071           || possible_function_pointer_reloc(r_type));
2072 }
2073
2074 // Scan a relocation for a global symbol.
2075
2076 inline void
2077 Target_x86_64::Scan::global(Symbol_table* symtab,
2078                             Layout* layout,
2079                             Target_x86_64* target,
2080                             Sized_relobj_file<64, false>* object,
2081                             unsigned int data_shndx,
2082                             Output_section* output_section,
2083                             const elfcpp::Rela<64, false>& reloc,
2084                             unsigned int r_type,
2085                             Symbol* gsym)
2086 {
2087   // A STT_GNU_IFUNC symbol may require a PLT entry.
2088   if (gsym->type() == elfcpp::STT_GNU_IFUNC
2089       && this->reloc_needs_plt_for_ifunc(object, r_type))
2090     target->make_plt_entry(symtab, layout, gsym);
2091
2092   switch (r_type)
2093     {
2094     case elfcpp::R_X86_64_NONE:
2095     case elfcpp::R_X86_64_GNU_VTINHERIT:
2096     case elfcpp::R_X86_64_GNU_VTENTRY:
2097       break;
2098
2099     case elfcpp::R_X86_64_64:
2100     case elfcpp::R_X86_64_32:
2101     case elfcpp::R_X86_64_32S:
2102     case elfcpp::R_X86_64_16:
2103     case elfcpp::R_X86_64_8:
2104       {
2105         // Make a PLT entry if necessary.
2106         if (gsym->needs_plt_entry())
2107           {
2108             target->make_plt_entry(symtab, layout, gsym);
2109             // Since this is not a PC-relative relocation, we may be
2110             // taking the address of a function. In that case we need to
2111             // set the entry in the dynamic symbol table to the address of
2112             // the PLT entry.
2113             if (gsym->is_from_dynobj() && !parameters->options().shared())
2114               gsym->set_needs_dynsym_value();
2115           }
2116         // Make a dynamic relocation if necessary.
2117         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2118           {
2119             if (gsym->may_need_copy_reloc())
2120               {
2121                 target->copy_reloc(symtab, layout, object,
2122                                    data_shndx, output_section, gsym, reloc);
2123               }
2124             else if (r_type == elfcpp::R_X86_64_64
2125                      && gsym->type() == elfcpp::STT_GNU_IFUNC
2126                      && gsym->can_use_relative_reloc(false)
2127                      && !gsym->is_from_dynobj()
2128                      && !gsym->is_undefined()
2129                      && !gsym->is_preemptible())
2130               {
2131                 // Use an IRELATIVE reloc for a locally defined
2132                 // STT_GNU_IFUNC symbol.  This makes a function
2133                 // address in a PIE executable match the address in a
2134                 // shared library that it links against.
2135                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2136                 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
2137                 rela_dyn->add_symbolless_global_addend(gsym, r_type,
2138                                                        output_section, object,
2139                                                        data_shndx,
2140                                                        reloc.get_r_offset(),
2141                                                        reloc.get_r_addend());
2142               }
2143             else if (r_type == elfcpp::R_X86_64_64
2144                      && gsym->can_use_relative_reloc(false))
2145               {
2146                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2147                 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
2148                                               output_section, object,
2149                                               data_shndx,
2150                                               reloc.get_r_offset(),
2151                                               reloc.get_r_addend());
2152               }
2153             else
2154               {
2155                 this->check_non_pic(object, r_type, gsym);
2156                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2157                 rela_dyn->add_global(gsym, r_type, output_section, object,
2158                                      data_shndx, reloc.get_r_offset(),
2159                                      reloc.get_r_addend());
2160               }
2161           }
2162       }
2163       break;
2164
2165     case elfcpp::R_X86_64_PC64:
2166     case elfcpp::R_X86_64_PC32:
2167     case elfcpp::R_X86_64_PC16:
2168     case elfcpp::R_X86_64_PC8:
2169       {
2170         // Make a PLT entry if necessary.
2171         if (gsym->needs_plt_entry())
2172           target->make_plt_entry(symtab, layout, gsym);
2173         // Make a dynamic relocation if necessary.
2174         if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
2175           {
2176             if (gsym->may_need_copy_reloc())
2177               {
2178                 target->copy_reloc(symtab, layout, object,
2179                                    data_shndx, output_section, gsym, reloc);
2180               }
2181             else
2182               {
2183                 this->check_non_pic(object, r_type, gsym);
2184                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2185                 rela_dyn->add_global(gsym, r_type, output_section, object,
2186                                      data_shndx, reloc.get_r_offset(),
2187                                      reloc.get_r_addend());
2188               }
2189           }
2190       }
2191       break;
2192
2193     case elfcpp::R_X86_64_GOT64:
2194     case elfcpp::R_X86_64_GOT32:
2195     case elfcpp::R_X86_64_GOTPCREL64:
2196     case elfcpp::R_X86_64_GOTPCREL:
2197     case elfcpp::R_X86_64_GOTPLT64:
2198       {
2199         // The symbol requires a GOT entry.
2200         Output_data_got<64, false>* got = target->got_section(symtab, layout);
2201         if (gsym->final_value_is_known())
2202           {
2203             // For a STT_GNU_IFUNC symbol we want the PLT address.
2204             if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2205               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2206             else
2207               got->add_global(gsym, GOT_TYPE_STANDARD);
2208           }
2209         else
2210           {
2211             // If this symbol is not fully resolved, we need to add a
2212             // dynamic relocation for it.
2213             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
2214             if (gsym->is_from_dynobj()
2215                 || gsym->is_undefined()
2216                 || gsym->is_preemptible()
2217                 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2218                     && parameters->options().output_is_position_independent()))
2219               got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
2220                                         elfcpp::R_X86_64_GLOB_DAT);
2221             else
2222               {
2223                 // For a STT_GNU_IFUNC symbol we want to write the PLT
2224                 // offset into the GOT, so that function pointer
2225                 // comparisons work correctly.
2226                 bool is_new;
2227                 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2228                   is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2229                 else
2230                   {
2231                     is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2232                     // Tell the dynamic linker to use the PLT address
2233                     // when resolving relocations.
2234                     if (gsym->is_from_dynobj()
2235                         && !parameters->options().shared())
2236                       gsym->set_needs_dynsym_value();
2237                   }
2238                 if (is_new)
2239                   {
2240                     unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2241                     rela_dyn->add_global_relative(gsym,
2242                                                   elfcpp::R_X86_64_RELATIVE,
2243                                                   got, got_off, 0);
2244                   }
2245               }
2246           }
2247         // For GOTPLT64, we also need a PLT entry (but only if the
2248         // symbol is not fully resolved).
2249         if (r_type == elfcpp::R_X86_64_GOTPLT64
2250             && !gsym->final_value_is_known())
2251           target->make_plt_entry(symtab, layout, gsym);
2252       }
2253       break;
2254
2255     case elfcpp::R_X86_64_PLT32:
2256       // If the symbol is fully resolved, this is just a PC32 reloc.
2257       // Otherwise we need a PLT entry.
2258       if (gsym->final_value_is_known())
2259         break;
2260       // If building a shared library, we can also skip the PLT entry
2261       // if the symbol is defined in the output file and is protected
2262       // or hidden.
2263       if (gsym->is_defined()
2264           && !gsym->is_from_dynobj()
2265           && !gsym->is_preemptible())
2266         break;
2267       target->make_plt_entry(symtab, layout, gsym);
2268       break;
2269
2270     case elfcpp::R_X86_64_GOTPC32:
2271     case elfcpp::R_X86_64_GOTOFF64:
2272     case elfcpp::R_X86_64_GOTPC64:
2273     case elfcpp::R_X86_64_PLTOFF64:
2274       // We need a GOT section.
2275       target->got_section(symtab, layout);
2276       // For PLTOFF64, we also need a PLT entry (but only if the
2277       // symbol is not fully resolved).
2278       if (r_type == elfcpp::R_X86_64_PLTOFF64
2279           && !gsym->final_value_is_known())
2280         target->make_plt_entry(symtab, layout, gsym);
2281       break;
2282
2283     case elfcpp::R_X86_64_COPY:
2284     case elfcpp::R_X86_64_GLOB_DAT:
2285     case elfcpp::R_X86_64_JUMP_SLOT:
2286     case elfcpp::R_X86_64_RELATIVE:
2287     case elfcpp::R_X86_64_IRELATIVE:
2288       // These are outstanding tls relocs, which are unexpected when linking
2289     case elfcpp::R_X86_64_TPOFF64:
2290     case elfcpp::R_X86_64_DTPMOD64:
2291     case elfcpp::R_X86_64_TLSDESC:
2292       gold_error(_("%s: unexpected reloc %u in object file"),
2293                  object->name().c_str(), r_type);
2294       break;
2295
2296       // These are initial tls relocs, which are expected for global()
2297     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2298     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2299     case elfcpp::R_X86_64_TLSDESC_CALL:
2300     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2301     case elfcpp::R_X86_64_DTPOFF32:
2302     case elfcpp::R_X86_64_DTPOFF64:
2303     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2304     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2305       {
2306         const bool is_final = gsym->final_value_is_known();
2307         const tls::Tls_optimization optimized_type
2308             = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2309         switch (r_type)
2310           {
2311           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
2312             if (optimized_type == tls::TLSOPT_NONE)
2313               {
2314                 // Create a pair of GOT entries for the module index and
2315                 // dtv-relative offset.
2316                 Output_data_got<64, false>* got
2317                     = target->got_section(symtab, layout);
2318                 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
2319                                                target->rela_dyn_section(layout),
2320                                                elfcpp::R_X86_64_DTPMOD64,
2321                                                elfcpp::R_X86_64_DTPOFF64);
2322               }
2323             else if (optimized_type == tls::TLSOPT_TO_IE)
2324               {
2325                 // Create a GOT entry for the tp-relative offset.
2326                 Output_data_got<64, false>* got
2327                     = target->got_section(symtab, layout);
2328                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2329                                           target->rela_dyn_section(layout),
2330                                           elfcpp::R_X86_64_TPOFF64);
2331               }
2332             else if (optimized_type != tls::TLSOPT_TO_LE)
2333               unsupported_reloc_global(object, r_type, gsym);
2334             break;
2335
2336           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2337             target->define_tls_base_symbol(symtab, layout);
2338             if (optimized_type == tls::TLSOPT_NONE)
2339               {
2340                 // Create reserved PLT and GOT entries for the resolver.
2341                 target->reserve_tlsdesc_entries(symtab, layout);
2342
2343                 // Create a double GOT entry with an R_X86_64_TLSDESC
2344                 // reloc.  The R_X86_64_TLSDESC reloc is resolved
2345                 // lazily, so the GOT entry needs to be in an area in
2346                 // .got.plt, not .got.  Call got_section to make sure
2347                 // the section has been created.
2348                 target->got_section(symtab, layout);
2349                 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2350                 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2351                 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
2352                                                elfcpp::R_X86_64_TLSDESC, 0);
2353               }
2354             else if (optimized_type == tls::TLSOPT_TO_IE)
2355               {
2356                 // Create a GOT entry for the tp-relative offset.
2357                 Output_data_got<64, false>* got
2358                     = target->got_section(symtab, layout);
2359                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2360                                           target->rela_dyn_section(layout),
2361                                           elfcpp::R_X86_64_TPOFF64);
2362               }
2363             else if (optimized_type != tls::TLSOPT_TO_LE)
2364               unsupported_reloc_global(object, r_type, gsym);
2365             break;
2366
2367           case elfcpp::R_X86_64_TLSDESC_CALL:
2368             break;
2369
2370           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
2371             if (optimized_type == tls::TLSOPT_NONE)
2372               {
2373                 // Create a GOT entry for the module index.
2374                 target->got_mod_index_entry(symtab, layout, object);
2375               }
2376             else if (optimized_type != tls::TLSOPT_TO_LE)
2377               unsupported_reloc_global(object, r_type, gsym);
2378             break;
2379
2380           case elfcpp::R_X86_64_DTPOFF32:
2381           case elfcpp::R_X86_64_DTPOFF64:
2382             break;
2383
2384           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
2385             layout->set_has_static_tls();
2386             if (optimized_type == tls::TLSOPT_NONE)
2387               {
2388                 // Create a GOT entry for the tp-relative offset.
2389                 Output_data_got<64, false>* got
2390                     = target->got_section(symtab, layout);
2391                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2392                                           target->rela_dyn_section(layout),
2393                                           elfcpp::R_X86_64_TPOFF64);
2394               }
2395             else if (optimized_type != tls::TLSOPT_TO_LE)
2396               unsupported_reloc_global(object, r_type, gsym);
2397             break;
2398
2399           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
2400             layout->set_has_static_tls();
2401             if (parameters->options().shared())
2402               unsupported_reloc_local(object, r_type);
2403             break;
2404
2405           default:
2406             gold_unreachable();
2407           }
2408       }
2409       break;
2410
2411     case elfcpp::R_X86_64_SIZE32:
2412     case elfcpp::R_X86_64_SIZE64:
2413     default:
2414       gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2415                  object->name().c_str(), r_type,
2416                  gsym->demangled_name().c_str());
2417       break;
2418     }
2419 }
2420
2421 void
2422 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
2423                                  Layout* layout,
2424                                  Sized_relobj_file<64, false>* object,
2425                                  unsigned int data_shndx,
2426                                  unsigned int sh_type,
2427                                  const unsigned char* prelocs,
2428                                  size_t reloc_count,
2429                                  Output_section* output_section,
2430                                  bool needs_special_offset_handling,
2431                                  size_t local_symbol_count,
2432                                  const unsigned char* plocal_symbols)
2433 {
2434
2435   if (sh_type == elfcpp::SHT_REL)
2436     {
2437       return;
2438     }
2439
2440    gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2441                            Target_x86_64::Scan,
2442                            Target_x86_64::Relocatable_size_for_reloc>(
2443     symtab,
2444     layout,
2445     this,
2446     object,
2447     data_shndx,
2448     prelocs,
2449     reloc_count,
2450     output_section,
2451     needs_special_offset_handling,
2452     local_symbol_count,
2453     plocal_symbols);
2454  
2455 }
2456 // Scan relocations for a section.
2457
2458 void
2459 Target_x86_64::scan_relocs(Symbol_table* symtab,
2460                            Layout* layout,
2461                            Sized_relobj_file<64, false>* object,
2462                            unsigned int data_shndx,
2463                            unsigned int sh_type,
2464                            const unsigned char* prelocs,
2465                            size_t reloc_count,
2466                            Output_section* output_section,
2467                            bool needs_special_offset_handling,
2468                            size_t local_symbol_count,
2469                            const unsigned char* plocal_symbols)
2470 {
2471   if (sh_type == elfcpp::SHT_REL)
2472     {
2473       gold_error(_("%s: unsupported REL reloc section"),
2474                  object->name().c_str());
2475       return;
2476     }
2477
2478   gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2479       Target_x86_64::Scan>(
2480     symtab,
2481     layout,
2482     this,
2483     object,
2484     data_shndx,
2485     prelocs,
2486     reloc_count,
2487     output_section,
2488     needs_special_offset_handling,
2489     local_symbol_count,
2490     plocal_symbols);
2491 }
2492
2493 // Finalize the sections.
2494
2495 void
2496 Target_x86_64::do_finalize_sections(
2497     Layout* layout,
2498     const Input_objects*,
2499     Symbol_table* symtab)
2500 {
2501   const Reloc_section* rel_plt = (this->plt_ == NULL
2502                                   ? NULL
2503                                   : this->plt_->rela_plt());
2504   layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2505                                   this->rela_dyn_, true, false);
2506                                   
2507   // Fill in some more dynamic tags.
2508   Output_data_dynamic* const odyn = layout->dynamic_data();
2509   if (odyn != NULL)
2510     {
2511       if (this->plt_ != NULL
2512           && this->plt_->output_section() != NULL
2513           && this->plt_->has_tlsdesc_entry())
2514         {
2515           unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2516           unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2517           this->got_->finalize_data_size();
2518           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2519                                         this->plt_, plt_offset);
2520           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2521                                         this->got_, got_offset);
2522         }
2523     }
2524
2525   // Emit any relocs we saved in an attempt to avoid generating COPY
2526   // relocs.
2527   if (this->copy_relocs_.any_saved_relocs())
2528     this->copy_relocs_.emit(this->rela_dyn_section(layout));
2529
2530   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2531   // the .got.plt section.
2532   Symbol* sym = this->global_offset_table_;
2533   if (sym != NULL)
2534     {
2535       uint64_t data_size = this->got_plt_->current_data_size();
2536       symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2537     }
2538 }
2539
2540 // Perform a relocation.
2541
2542 inline bool
2543 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2544                                   Target_x86_64* target,
2545                                   Output_section*,
2546                                   size_t relnum,
2547                                   const elfcpp::Rela<64, false>& rela,
2548                                   unsigned int r_type,
2549                                   const Sized_symbol<64>* gsym,
2550                                   const Symbol_value<64>* psymval,
2551                                   unsigned char* view,
2552                                   elfcpp::Elf_types<64>::Elf_Addr address,
2553                                   section_size_type view_size)
2554 {
2555   if (this->skip_call_tls_get_addr_)
2556     {
2557       if ((r_type != elfcpp::R_X86_64_PLT32
2558            && r_type != elfcpp::R_X86_64_PC32)
2559           || gsym == NULL
2560           || strcmp(gsym->name(), "__tls_get_addr") != 0)
2561         {
2562           gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2563                                  _("missing expected TLS relocation"));
2564         }
2565       else
2566         {
2567           this->skip_call_tls_get_addr_ = false;
2568           return false;
2569         }
2570     }
2571
2572   const Sized_relobj_file<64, false>* object = relinfo->object;
2573
2574   // Pick the value to use for symbols defined in the PLT.
2575   Symbol_value<64> symval;
2576   if (gsym != NULL
2577       && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2578     {
2579       symval.set_output_value(target->plt_section()->address()
2580                               + gsym->plt_offset());
2581       psymval = &symval;
2582     }
2583   else if (gsym == NULL && psymval->is_ifunc_symbol())
2584     {
2585       unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2586       if (object->local_has_plt_offset(r_sym))
2587         {
2588           symval.set_output_value(target->plt_section()->address()
2589                                   + object->local_plt_offset(r_sym));
2590           psymval = &symval;
2591         }
2592     }
2593
2594   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2595
2596   // Get the GOT offset if needed.
2597   // The GOT pointer points to the end of the GOT section.
2598   // We need to subtract the size of the GOT section to get
2599   // the actual offset to use in the relocation.
2600   bool have_got_offset = false;
2601   unsigned int got_offset = 0;
2602   switch (r_type)
2603     {
2604     case elfcpp::R_X86_64_GOT32:
2605     case elfcpp::R_X86_64_GOT64:
2606     case elfcpp::R_X86_64_GOTPLT64:
2607     case elfcpp::R_X86_64_GOTPCREL:
2608     case elfcpp::R_X86_64_GOTPCREL64:
2609       if (gsym != NULL)
2610         {
2611           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2612           got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
2613         }
2614       else
2615         {
2616           unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2617           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2618           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2619                         - target->got_size());
2620         }
2621       have_got_offset = true;
2622       break;
2623
2624     default:
2625       break;
2626     }
2627
2628   switch (r_type)
2629     {
2630     case elfcpp::R_X86_64_NONE:
2631     case elfcpp::R_X86_64_GNU_VTINHERIT:
2632     case elfcpp::R_X86_64_GNU_VTENTRY:
2633       break;
2634
2635     case elfcpp::R_X86_64_64:
2636       Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2637       break;
2638
2639     case elfcpp::R_X86_64_PC64:
2640       Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2641                                               address);
2642       break;
2643
2644     case elfcpp::R_X86_64_32:
2645       // FIXME: we need to verify that value + addend fits into 32 bits:
2646       //    uint64_t x = value + addend;
2647       //    x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2648       // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2649       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2650       break;
2651
2652     case elfcpp::R_X86_64_32S:
2653       // FIXME: we need to verify that value + addend fits into 32 bits:
2654       //    int64_t x = value + addend;   // note this quantity is signed!
2655       //    x == static_cast<int64_t>(static_cast<int32_t>(x))
2656       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2657       break;
2658
2659     case elfcpp::R_X86_64_PC32:
2660       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2661                                               address);
2662       break;
2663
2664     case elfcpp::R_X86_64_16:
2665       Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2666       break;
2667
2668     case elfcpp::R_X86_64_PC16:
2669       Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2670                                               address);
2671       break;
2672
2673     case elfcpp::R_X86_64_8:
2674       Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2675       break;
2676
2677     case elfcpp::R_X86_64_PC8:
2678       Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2679                                              address);
2680       break;
2681
2682     case elfcpp::R_X86_64_PLT32:
2683       gold_assert(gsym == NULL
2684                   || gsym->has_plt_offset()
2685                   || gsym->final_value_is_known()
2686                   || (gsym->is_defined()
2687                       && !gsym->is_from_dynobj()
2688                       && !gsym->is_preemptible()));
2689       // Note: while this code looks the same as for R_X86_64_PC32, it
2690       // behaves differently because psymval was set to point to
2691       // the PLT entry, rather than the symbol, in Scan::global().
2692       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2693                                               address);
2694       break;
2695
2696     case elfcpp::R_X86_64_PLTOFF64:
2697       {
2698         gold_assert(gsym);
2699         gold_assert(gsym->has_plt_offset()
2700                     || gsym->final_value_is_known());
2701         elfcpp::Elf_types<64>::Elf_Addr got_address;
2702         got_address = target->got_section(NULL, NULL)->address();
2703         Relocate_functions<64, false>::rela64(view, object, psymval,
2704                                               addend - got_address);
2705       }
2706
2707     case elfcpp::R_X86_64_GOT32:
2708       gold_assert(have_got_offset);
2709       Relocate_functions<64, false>::rela32(view, got_offset, addend);
2710       break;
2711
2712     case elfcpp::R_X86_64_GOTPC32:
2713       {
2714         gold_assert(gsym);
2715         elfcpp::Elf_types<64>::Elf_Addr value;
2716         value = target->got_plt_section()->address();
2717         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2718       }
2719       break;
2720
2721     case elfcpp::R_X86_64_GOT64:
2722       // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2723       // Since we always add a PLT entry, this is equivalent.
2724     case elfcpp::R_X86_64_GOTPLT64:
2725       gold_assert(have_got_offset);
2726       Relocate_functions<64, false>::rela64(view, got_offset, addend);
2727       break;
2728
2729     case elfcpp::R_X86_64_GOTPC64:
2730       {
2731         gold_assert(gsym);
2732         elfcpp::Elf_types<64>::Elf_Addr value;
2733         value = target->got_plt_section()->address();
2734         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2735       }
2736       break;
2737
2738     case elfcpp::R_X86_64_GOTOFF64:
2739       {
2740         elfcpp::Elf_types<64>::Elf_Addr value;
2741         value = (psymval->value(object, 0)
2742                  - target->got_plt_section()->address());
2743         Relocate_functions<64, false>::rela64(view, value, addend);
2744       }
2745       break;
2746
2747     case elfcpp::R_X86_64_GOTPCREL:
2748       {
2749         gold_assert(have_got_offset);
2750         elfcpp::Elf_types<64>::Elf_Addr value;
2751         value = target->got_plt_section()->address() + got_offset;
2752         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2753       }
2754       break;
2755
2756     case elfcpp::R_X86_64_GOTPCREL64:
2757       {
2758         gold_assert(have_got_offset);
2759         elfcpp::Elf_types<64>::Elf_Addr value;
2760         value = target->got_plt_section()->address() + got_offset;
2761         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2762       }
2763       break;
2764
2765     case elfcpp::R_X86_64_COPY:
2766     case elfcpp::R_X86_64_GLOB_DAT:
2767     case elfcpp::R_X86_64_JUMP_SLOT:
2768     case elfcpp::R_X86_64_RELATIVE:
2769     case elfcpp::R_X86_64_IRELATIVE:
2770       // These are outstanding tls relocs, which are unexpected when linking
2771     case elfcpp::R_X86_64_TPOFF64:
2772     case elfcpp::R_X86_64_DTPMOD64:
2773     case elfcpp::R_X86_64_TLSDESC:
2774       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2775                              _("unexpected reloc %u in object file"),
2776                              r_type);
2777       break;
2778
2779       // These are initial tls relocs, which are expected when linking
2780     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2781     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2782     case elfcpp::R_X86_64_TLSDESC_CALL:
2783     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2784     case elfcpp::R_X86_64_DTPOFF32:
2785     case elfcpp::R_X86_64_DTPOFF64:
2786     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2787     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2788       this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2789                          view, address, view_size);
2790       break;
2791
2792     case elfcpp::R_X86_64_SIZE32:
2793     case elfcpp::R_X86_64_SIZE64:
2794     default:
2795       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2796                              _("unsupported reloc %u"),
2797                              r_type);
2798       break;
2799     }
2800
2801   return true;
2802 }
2803
2804 // Perform a TLS relocation.
2805
2806 inline void
2807 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
2808                                       Target_x86_64* target,
2809                                       size_t relnum,
2810                                       const elfcpp::Rela<64, false>& rela,
2811                                       unsigned int r_type,
2812                                       const Sized_symbol<64>* gsym,
2813                                       const Symbol_value<64>* psymval,
2814                                       unsigned char* view,
2815                                       elfcpp::Elf_types<64>::Elf_Addr address,
2816                                       section_size_type view_size)
2817 {
2818   Output_segment* tls_segment = relinfo->layout->tls_segment();
2819
2820   const Sized_relobj_file<64, false>* object = relinfo->object;
2821   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2822   elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
2823   bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2824
2825   elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2826
2827   const bool is_final = (gsym == NULL
2828                          ? !parameters->options().shared()
2829                          : gsym->final_value_is_known());
2830   tls::Tls_optimization optimized_type
2831       = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2832   switch (r_type)
2833     {
2834     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2835       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2836         {
2837           // If this code sequence is used in a non-executable section,
2838           // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2839           // on the assumption that it's being used by itself in a debug
2840           // section.  Therefore, in the unlikely event that the code
2841           // sequence appears in a non-executable section, we simply
2842           // leave it unoptimized.
2843           optimized_type = tls::TLSOPT_NONE;
2844         }
2845       if (optimized_type == tls::TLSOPT_TO_LE)
2846         {
2847           gold_assert(tls_segment != NULL);
2848           this->tls_gd_to_le(relinfo, relnum, tls_segment,
2849                              rela, r_type, value, view,
2850                              view_size);
2851           break;
2852         }
2853       else
2854         {
2855           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2856                                    ? GOT_TYPE_TLS_OFFSET
2857                                    : GOT_TYPE_TLS_PAIR);
2858           unsigned int got_offset;
2859           if (gsym != NULL)
2860             {
2861               gold_assert(gsym->has_got_offset(got_type));
2862               got_offset = gsym->got_offset(got_type) - target->got_size();
2863             }
2864           else
2865             {
2866               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2867               gold_assert(object->local_has_got_offset(r_sym, got_type));
2868               got_offset = (object->local_got_offset(r_sym, got_type)
2869                             - target->got_size());
2870             }
2871           if (optimized_type == tls::TLSOPT_TO_IE)
2872             {
2873               gold_assert(tls_segment != NULL);
2874               value = target->got_plt_section()->address() + got_offset;
2875               this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2876                                  value, view, address, view_size);
2877               break;
2878             }
2879           else if (optimized_type == tls::TLSOPT_NONE)
2880             {
2881               // Relocate the field with the offset of the pair of GOT
2882               // entries.
2883               value = target->got_plt_section()->address() + got_offset;
2884               Relocate_functions<64, false>::pcrela32(view, value, addend,
2885                                                       address);
2886               break;
2887             }
2888         }
2889       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2890                              _("unsupported reloc %u"), r_type);
2891       break;
2892
2893     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2894     case elfcpp::R_X86_64_TLSDESC_CALL:
2895       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2896         {
2897           // See above comment for R_X86_64_TLSGD.
2898           optimized_type = tls::TLSOPT_NONE;
2899         }
2900       if (optimized_type == tls::TLSOPT_TO_LE)
2901         {
2902           gold_assert(tls_segment != NULL);
2903           this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2904                                   rela, r_type, value, view,
2905                                   view_size);
2906           break;
2907         }
2908       else
2909         {
2910           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2911                                    ? GOT_TYPE_TLS_OFFSET
2912                                    : GOT_TYPE_TLS_DESC);
2913           unsigned int got_offset = 0;
2914           if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2915               && optimized_type == tls::TLSOPT_NONE)
2916             {
2917               // We created GOT entries in the .got.tlsdesc portion of
2918               // the .got.plt section, but the offset stored in the
2919               // symbol is the offset within .got.tlsdesc.
2920               got_offset = (target->got_size()
2921                             + target->got_plt_section()->data_size());
2922             }
2923           if (gsym != NULL)
2924             {
2925               gold_assert(gsym->has_got_offset(got_type));
2926               got_offset += gsym->got_offset(got_type) - target->got_size();
2927             }
2928           else
2929             {
2930               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2931               gold_assert(object->local_has_got_offset(r_sym, got_type));
2932               got_offset += (object->local_got_offset(r_sym, got_type)
2933                              - target->got_size());
2934             }
2935           if (optimized_type == tls::TLSOPT_TO_IE)
2936             {
2937               gold_assert(tls_segment != NULL);
2938               value = target->got_plt_section()->address() + got_offset;
2939               this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2940                                       rela, r_type, value, view, address,
2941                                       view_size);
2942               break;
2943             }
2944           else if (optimized_type == tls::TLSOPT_NONE)
2945             {
2946               if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2947                 {
2948                   // Relocate the field with the offset of the pair of GOT
2949                   // entries.
2950                   value = target->got_plt_section()->address() + got_offset;
2951                   Relocate_functions<64, false>::pcrela32(view, value, addend,
2952                                                           address);
2953                 }
2954               break;
2955             }
2956         }
2957       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2958                              _("unsupported reloc %u"), r_type);
2959       break;
2960
2961     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2962       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2963         {
2964           // See above comment for R_X86_64_TLSGD.
2965           optimized_type = tls::TLSOPT_NONE;
2966         }
2967       if (optimized_type == tls::TLSOPT_TO_LE)
2968         {
2969           gold_assert(tls_segment != NULL);
2970           this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2971                              value, view, view_size);
2972           break;
2973         }
2974       else if (optimized_type == tls::TLSOPT_NONE)
2975         {
2976           // Relocate the field with the offset of the GOT entry for
2977           // the module index.
2978           unsigned int got_offset;
2979           got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2980                         - target->got_size());
2981           value = target->got_plt_section()->address() + got_offset;
2982           Relocate_functions<64, false>::pcrela32(view, value, addend,
2983                                                   address);
2984           break;
2985         }
2986       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2987                              _("unsupported reloc %u"), r_type);
2988       break;
2989
2990     case elfcpp::R_X86_64_DTPOFF32:
2991       // This relocation type is used in debugging information.
2992       // In that case we need to not optimize the value.  If the
2993       // section is not executable, then we assume we should not
2994       // optimize this reloc.  See comments above for R_X86_64_TLSGD,
2995       // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
2996       // R_X86_64_TLSLD.
2997       if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2998         {
2999           gold_assert(tls_segment != NULL);
3000           value -= tls_segment->memsz();
3001         }
3002       Relocate_functions<64, false>::rela32(view, value, addend);
3003       break;
3004
3005     case elfcpp::R_X86_64_DTPOFF64:
3006       // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
3007       if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
3008         {
3009           gold_assert(tls_segment != NULL);
3010           value -= tls_segment->memsz();
3011         }
3012       Relocate_functions<64, false>::rela64(view, value, addend);
3013       break;
3014
3015     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
3016       if (optimized_type == tls::TLSOPT_TO_LE)
3017         {
3018           gold_assert(tls_segment != NULL);
3019           Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
3020                                                 rela, r_type, value, view,
3021                                                 view_size);
3022           break;
3023         }
3024       else if (optimized_type == tls::TLSOPT_NONE)
3025         {
3026           // Relocate the field with the offset of the GOT entry for
3027           // the tp-relative offset of the symbol.
3028           unsigned int got_offset;
3029           if (gsym != NULL)
3030             {
3031               gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
3032               got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
3033                             - target->got_size());
3034             }
3035           else
3036             {
3037               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
3038               gold_assert(object->local_has_got_offset(r_sym,
3039                                                        GOT_TYPE_TLS_OFFSET));
3040               got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
3041                             - target->got_size());
3042             }
3043           value = target->got_plt_section()->address() + got_offset;
3044           Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3045           break;
3046         }
3047       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
3048                              _("unsupported reloc type %u"),
3049                              r_type);
3050       break;
3051
3052     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
3053       value -= tls_segment->memsz();
3054       Relocate_functions<64, false>::rela32(view, value, addend);
3055       break;
3056     }
3057 }
3058
3059 // Do a relocation in which we convert a TLS General-Dynamic to an
3060 // Initial-Exec.
3061
3062 inline void
3063 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
3064                                       size_t relnum,
3065                                       Output_segment*,
3066                                       const elfcpp::Rela<64, false>& rela,
3067                                       unsigned int,
3068                                       elfcpp::Elf_types<64>::Elf_Addr value,
3069                                       unsigned char* view,
3070                                       elfcpp::Elf_types<64>::Elf_Addr address,
3071                                       section_size_type view_size)
3072 {
3073   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3074   // .word 0x6666; rex64; call __tls_get_addr
3075   // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
3076
3077   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3078   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3079
3080   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3081                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3082   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3083                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3084
3085   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
3086
3087   const elfcpp::Elf_Xword addend = rela.get_r_addend();
3088   Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
3089
3090   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3091   // We can skip it.
3092   this->skip_call_tls_get_addr_ = true;
3093 }
3094
3095 // Do a relocation in which we convert a TLS General-Dynamic to a
3096 // Local-Exec.
3097
3098 inline void
3099 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
3100                                       size_t relnum,
3101                                       Output_segment* tls_segment,
3102                                       const elfcpp::Rela<64, false>& rela,
3103                                       unsigned int,
3104                                       elfcpp::Elf_types<64>::Elf_Addr value,
3105                                       unsigned char* view,
3106                                       section_size_type view_size)
3107 {
3108   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
3109   // .word 0x6666; rex64; call __tls_get_addr
3110   // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
3111
3112   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
3113   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
3114
3115   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3116                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
3117   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3118                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
3119
3120   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
3121
3122   value -= tls_segment->memsz();
3123   Relocate_functions<64, false>::rela32(view + 8, value, 0);
3124
3125   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3126   // We can skip it.
3127   this->skip_call_tls_get_addr_ = true;
3128 }
3129
3130 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
3131
3132 inline void
3133 Target_x86_64::Relocate::tls_desc_gd_to_ie(
3134     const Relocate_info<64, false>* relinfo,
3135     size_t relnum,
3136     Output_segment*,
3137     const elfcpp::Rela<64, false>& rela,
3138     unsigned int r_type,
3139     elfcpp::Elf_types<64>::Elf_Addr value,
3140     unsigned char* view,
3141     elfcpp::Elf_types<64>::Elf_Addr address,
3142     section_size_type view_size)
3143 {
3144   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3145     {
3146       // leaq foo@tlsdesc(%rip), %rax
3147       // ==> movq foo@gottpoff(%rip), %rax
3148       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3149       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3150       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3151                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3152       view[-2] = 0x8b;
3153       const elfcpp::Elf_Xword addend = rela.get_r_addend();
3154       Relocate_functions<64, false>::pcrela32(view, value, addend, address);
3155     }
3156   else
3157     {
3158       // call *foo@tlscall(%rax)
3159       // ==> nop; nop
3160       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3161       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3162       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3163                      view[0] == 0xff && view[1] == 0x10);
3164       view[0] = 0x66;
3165       view[1] = 0x90;
3166     }
3167 }
3168
3169 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
3170
3171 inline void
3172 Target_x86_64::Relocate::tls_desc_gd_to_le(
3173     const Relocate_info<64, false>* relinfo,
3174     size_t relnum,
3175     Output_segment* tls_segment,
3176     const elfcpp::Rela<64, false>& rela,
3177     unsigned int r_type,
3178     elfcpp::Elf_types<64>::Elf_Addr value,
3179     unsigned char* view,
3180     section_size_type view_size)
3181 {
3182   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
3183     {
3184       // leaq foo@tlsdesc(%rip), %rax
3185       // ==> movq foo@tpoff, %rax
3186       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3187       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3188       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3189                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
3190       view[-2] = 0xc7;
3191       view[-1] = 0xc0;
3192       value -= tls_segment->memsz();
3193       Relocate_functions<64, false>::rela32(view, value, 0);
3194     }
3195   else
3196     {
3197       // call *foo@tlscall(%rax)
3198       // ==> nop; nop
3199       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
3200       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
3201       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3202                      view[0] == 0xff && view[1] == 0x10);
3203       view[0] = 0x66;
3204       view[1] = 0x90;
3205     }
3206 }
3207
3208 inline void
3209 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
3210                                       size_t relnum,
3211                                       Output_segment*,
3212                                       const elfcpp::Rela<64, false>& rela,
3213                                       unsigned int,
3214                                       elfcpp::Elf_types<64>::Elf_Addr,
3215                                       unsigned char* view,
3216                                       section_size_type view_size)
3217 {
3218   // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
3219   // ... leq foo@dtpoff(%rax),%reg
3220   // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
3221
3222   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3223   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
3224
3225   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
3226                  view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
3227
3228   tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
3229
3230   memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
3231
3232   // The next reloc should be a PLT32 reloc against __tls_get_addr.
3233   // We can skip it.
3234   this->skip_call_tls_get_addr_ = true;
3235 }
3236
3237 // Do a relocation in which we convert a TLS Initial-Exec to a
3238 // Local-Exec.
3239
3240 inline void
3241 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
3242                                       size_t relnum,
3243                                       Output_segment* tls_segment,
3244                                       const elfcpp::Rela<64, false>& rela,
3245                                       unsigned int,
3246                                       elfcpp::Elf_types<64>::Elf_Addr value,
3247                                       unsigned char* view,
3248                                       section_size_type view_size)
3249 {
3250   // We need to examine the opcodes to figure out which instruction we
3251   // are looking at.
3252
3253   // movq foo@gottpoff(%rip),%reg  ==>  movq $YY,%reg
3254   // addq foo@gottpoff(%rip),%reg  ==>  addq $YY,%reg
3255
3256   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
3257   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
3258
3259   unsigned char op1 = view[-3];
3260   unsigned char op2 = view[-2];
3261   unsigned char op3 = view[-1];
3262   unsigned char reg = op3 >> 3;
3263
3264   if (op2 == 0x8b)
3265     {
3266       // movq
3267       if (op1 == 0x4c)
3268         view[-3] = 0x49;
3269       view[-2] = 0xc7;
3270       view[-1] = 0xc0 | reg;
3271     }
3272   else if (reg == 4)
3273     {
3274       // Special handling for %rsp.
3275       if (op1 == 0x4c)
3276         view[-3] = 0x49;
3277       view[-2] = 0x81;
3278       view[-1] = 0xc0 | reg;
3279     }
3280   else
3281     {
3282       // addq
3283       if (op1 == 0x4c)
3284         view[-3] = 0x4d;
3285       view[-2] = 0x8d;
3286       view[-1] = 0x80 | reg | (reg << 3);
3287     }
3288
3289   value -= tls_segment->memsz();
3290   Relocate_functions<64, false>::rela32(view, value, 0);
3291 }
3292
3293 // Relocate section data.
3294
3295 void
3296 Target_x86_64::relocate_section(
3297     const Relocate_info<64, false>* relinfo,
3298     unsigned int sh_type,
3299     const unsigned char* prelocs,
3300     size_t reloc_count,
3301     Output_section* output_section,
3302     bool needs_special_offset_handling,
3303     unsigned char* view,
3304     elfcpp::Elf_types<64>::Elf_Addr address,
3305     section_size_type view_size,
3306     const Reloc_symbol_changes* reloc_symbol_changes)
3307 {
3308   gold_assert(sh_type == elfcpp::SHT_RELA);
3309
3310   gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
3311                          Target_x86_64::Relocate>(
3312     relinfo,
3313     this,
3314     prelocs,
3315     reloc_count,
3316     output_section,
3317     needs_special_offset_handling,
3318     view,
3319     address,
3320     view_size,
3321     reloc_symbol_changes);
3322 }
3323
3324 // Apply an incremental relocation.  Incremental relocations always refer
3325 // to global symbols.
3326
3327 void
3328 Target_x86_64::apply_relocation(
3329     const Relocate_info<64, false>* relinfo,
3330     elfcpp::Elf_types<64>::Elf_Addr r_offset,
3331     unsigned int r_type,
3332     elfcpp::Elf_types<64>::Elf_Swxword r_addend,
3333     const Symbol* gsym,
3334     unsigned char* view,
3335     elfcpp::Elf_types<64>::Elf_Addr address,
3336     section_size_type view_size)
3337 {
3338   gold::apply_relocation<64, false, Target_x86_64, Target_x86_64::Relocate>(
3339     relinfo,
3340     this,
3341     r_offset,
3342     r_type,
3343     r_addend,
3344     gsym,
3345     view,
3346     address,
3347     view_size);
3348 }
3349
3350 // Return the size of a relocation while scanning during a relocatable
3351 // link.
3352
3353 unsigned int
3354 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
3355     unsigned int r_type,
3356     Relobj* object)
3357 {
3358   switch (r_type)
3359     {
3360     case elfcpp::R_X86_64_NONE:
3361     case elfcpp::R_X86_64_GNU_VTINHERIT:
3362     case elfcpp::R_X86_64_GNU_VTENTRY:
3363     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
3364     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
3365     case elfcpp::R_X86_64_TLSDESC_CALL:
3366     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
3367     case elfcpp::R_X86_64_DTPOFF32:
3368     case elfcpp::R_X86_64_DTPOFF64:
3369     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
3370     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
3371       return 0;
3372
3373     case elfcpp::R_X86_64_64:
3374     case elfcpp::R_X86_64_PC64:
3375     case elfcpp::R_X86_64_GOTOFF64:
3376     case elfcpp::R_X86_64_GOTPC64:
3377     case elfcpp::R_X86_64_PLTOFF64:
3378     case elfcpp::R_X86_64_GOT64:
3379     case elfcpp::R_X86_64_GOTPCREL64:
3380     case elfcpp::R_X86_64_GOTPCREL:
3381     case elfcpp::R_X86_64_GOTPLT64:
3382       return 8;
3383
3384     case elfcpp::R_X86_64_32:
3385     case elfcpp::R_X86_64_32S:
3386     case elfcpp::R_X86_64_PC32:
3387     case elfcpp::R_X86_64_PLT32:
3388     case elfcpp::R_X86_64_GOTPC32:
3389     case elfcpp::R_X86_64_GOT32:
3390       return 4;
3391
3392     case elfcpp::R_X86_64_16:
3393     case elfcpp::R_X86_64_PC16:
3394       return 2;
3395
3396     case elfcpp::R_X86_64_8:
3397     case elfcpp::R_X86_64_PC8:
3398       return 1;
3399
3400     case elfcpp::R_X86_64_COPY:
3401     case elfcpp::R_X86_64_GLOB_DAT:
3402     case elfcpp::R_X86_64_JUMP_SLOT:
3403     case elfcpp::R_X86_64_RELATIVE:
3404     case elfcpp::R_X86_64_IRELATIVE:
3405       // These are outstanding tls relocs, which are unexpected when linking
3406     case elfcpp::R_X86_64_TPOFF64:
3407     case elfcpp::R_X86_64_DTPMOD64:
3408     case elfcpp::R_X86_64_TLSDESC:
3409       object->error(_("unexpected reloc %u in object file"), r_type);
3410       return 0;
3411
3412     case elfcpp::R_X86_64_SIZE32:
3413     case elfcpp::R_X86_64_SIZE64:
3414     default:
3415       object->error(_("unsupported reloc %u against local symbol"), r_type);
3416       return 0;
3417     }
3418 }
3419
3420 // Scan the relocs during a relocatable link.
3421
3422 void
3423 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
3424                                        Layout* layout,
3425                                        Sized_relobj_file<64, false>* object,
3426                                        unsigned int data_shndx,
3427                                        unsigned int sh_type,
3428                                        const unsigned char* prelocs,
3429                                        size_t reloc_count,
3430                                        Output_section* output_section,
3431                                        bool needs_special_offset_handling,
3432                                        size_t local_symbol_count,
3433                                        const unsigned char* plocal_symbols,
3434                                        Relocatable_relocs* rr)
3435 {
3436   gold_assert(sh_type == elfcpp::SHT_RELA);
3437
3438   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3439     Relocatable_size_for_reloc> Scan_relocatable_relocs;
3440
3441   gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
3442       Scan_relocatable_relocs>(
3443     symtab,
3444     layout,
3445     object,
3446     data_shndx,
3447     prelocs,
3448     reloc_count,
3449     output_section,
3450     needs_special_offset_handling,
3451     local_symbol_count,
3452     plocal_symbols,
3453     rr);
3454 }
3455
3456 // Relocate a section during a relocatable link.
3457
3458 void
3459 Target_x86_64::relocate_for_relocatable(
3460     const Relocate_info<64, false>* relinfo,
3461     unsigned int sh_type,
3462     const unsigned char* prelocs,
3463     size_t reloc_count,
3464     Output_section* output_section,
3465     off_t offset_in_output_section,
3466     const Relocatable_relocs* rr,
3467     unsigned char* view,
3468     elfcpp::Elf_types<64>::Elf_Addr view_address,
3469     section_size_type view_size,
3470     unsigned char* reloc_view,
3471     section_size_type reloc_view_size)
3472 {
3473   gold_assert(sh_type == elfcpp::SHT_RELA);
3474
3475   gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
3476     relinfo,
3477     prelocs,
3478     reloc_count,
3479     output_section,
3480     offset_in_output_section,
3481     rr,
3482     view,
3483     view_address,
3484     view_size,
3485     reloc_view,
3486     reloc_view_size);
3487 }
3488
3489 // Return the value to use for a dynamic which requires special
3490 // treatment.  This is how we support equality comparisons of function
3491 // pointers across shared library boundaries, as described in the
3492 // processor specific ABI supplement.
3493
3494 uint64_t
3495 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3496 {
3497   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3498   return this->plt_section()->address() + gsym->plt_offset();
3499 }
3500
3501 // Return a string used to fill a code section with nops to take up
3502 // the specified length.
3503
3504 std::string
3505 Target_x86_64::do_code_fill(section_size_type length) const
3506 {
3507   if (length >= 16)
3508     {
3509       // Build a jmpq instruction to skip over the bytes.
3510       unsigned char jmp[5];
3511       jmp[0] = 0xe9;
3512       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3513       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3514               + std::string(length - 5, '\0'));
3515     }
3516
3517   // Nop sequences of various lengths.
3518   const char nop1[1] = { 0x90 };                   // nop
3519   const char nop2[2] = { 0x66, 0x90 };             // xchg %ax %ax
3520   const char nop3[3] = { 0x0f, 0x1f, 0x00 };       // nop (%rax)
3521   const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00};  // nop 0(%rax)
3522   const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00,   // nop 0(%rax,%rax,1)
3523                          0x00 };
3524   const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44,   // nopw 0(%rax,%rax,1)
3525                          0x00, 0x00 };
3526   const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00,   // nopl 0L(%rax)
3527                          0x00, 0x00, 0x00 };
3528   const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00,   // nopl 0L(%rax,%rax,1)
3529                          0x00, 0x00, 0x00, 0x00 };
3530   const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84,   // nopw 0L(%rax,%rax,1)
3531                          0x00, 0x00, 0x00, 0x00,
3532                          0x00 };
3533   const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3534                            0x84, 0x00, 0x00, 0x00,
3535                            0x00, 0x00 };
3536   const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3537                            0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3538                            0x00, 0x00, 0x00 };
3539   const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3540                            0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3541                            0x00, 0x00, 0x00, 0x00 };
3542   const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3543                            0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3544                            0x00, 0x00, 0x00, 0x00,
3545                            0x00 };
3546   const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3547                            0x66, 0x2e, 0x0f, 0x1f, // data16
3548                            0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3549                            0x00, 0x00 };
3550   const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3551                            0x66, 0x66, 0x2e, 0x0f, // data16; data16
3552                            0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3553                            0x00, 0x00, 0x00 };
3554
3555   const char* nops[16] = {
3556     NULL,
3557     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3558     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3559   };
3560
3561   return std::string(nops[length], length);
3562 }
3563
3564 // Return the addend to use for a target specific relocation.  The
3565 // only target specific relocation is R_X86_64_TLSDESC for a local
3566 // symbol.  We want to set the addend is the offset of the local
3567 // symbol in the TLS segment.
3568
3569 uint64_t
3570 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3571                                uint64_t) const
3572 {
3573   gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3574   uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3575   gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3576   const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3577   const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3578   gold_assert(psymval->is_tls_symbol());
3579   // The value of a TLS symbol is the offset in the TLS segment.
3580   return psymval->value(ti.object, 0);
3581 }
3582
3583 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3584 // compiled with -fsplit-stack.  The function calls non-split-stack
3585 // code.  We have to change the function so that it always ensures
3586 // that it has enough stack space to run some random function.
3587
3588 void
3589 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3590                                   section_offset_type fnoffset,
3591                                   section_size_type fnsize,
3592                                   unsigned char* view,
3593                                   section_size_type view_size,
3594                                   std::string* from,
3595                                   std::string* to) const
3596 {
3597   // The function starts with a comparison of the stack pointer and a
3598   // field in the TCB.  This is followed by a jump.
3599
3600   // cmp %fs:NN,%rsp
3601   if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3602       && fnsize > 9)
3603     {
3604       // We will call __morestack if the carry flag is set after this
3605       // comparison.  We turn the comparison into an stc instruction
3606       // and some nops.
3607       view[fnoffset] = '\xf9';
3608       this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3609     }
3610   // lea NN(%rsp),%r10
3611   // lea NN(%rsp),%r11
3612   else if ((this->match_view(view, view_size, fnoffset,
3613                              "\x4c\x8d\x94\x24", 4)
3614             || this->match_view(view, view_size, fnoffset,
3615                                 "\x4c\x8d\x9c\x24", 4))
3616            && fnsize > 8)
3617     {
3618       // This is loading an offset from the stack pointer for a
3619       // comparison.  The offset is negative, so we decrease the
3620       // offset by the amount of space we need for the stack.  This
3621       // means we will avoid calling __morestack if there happens to
3622       // be plenty of space on the stack already.
3623       unsigned char* pval = view + fnoffset + 4;
3624       uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3625       val -= parameters->options().split_stack_adjust_size();
3626       elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3627     }
3628   else
3629     {
3630       if (!object->has_no_split_stack())
3631         object->error(_("failed to match split-stack sequence at "
3632                         "section %u offset %0zx"),
3633                       shndx, static_cast<size_t>(fnoffset));
3634       return;
3635     }
3636
3637   // We have to change the function so that it calls
3638   // __morestack_non_split instead of __morestack.  The former will
3639   // allocate additional stack space.
3640   *from = "__morestack";
3641   *to = "__morestack_non_split";
3642 }
3643
3644 // The selector for x86_64 object files.
3645
3646 class Target_selector_x86_64 : public Target_selector_freebsd
3647 {
3648 public:
3649   Target_selector_x86_64()
3650     : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
3651                               "elf64-x86-64-freebsd")
3652   { }
3653
3654   Target*
3655   do_instantiate_target()
3656   { return new Target_x86_64(); }
3657
3658 };
3659
3660 Target_selector_x86_64 target_selector_x86_64;
3661
3662 } // End anonymous namespace.