Home | History | Annotate | Download | only in gold
      1 // x86_64.cc -- x86_64 target support for gold.
      2 
      3 // Copyright (C) 2006-2016 Free Software Foundation, Inc.
      4 // Written by Ian Lance Taylor <iant (at) 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 "dwarf.h"
     29 #include "parameters.h"
     30 #include "reloc.h"
     31 #include "x86_64.h"
     32 #include "object.h"
     33 #include "symtab.h"
     34 #include "layout.h"
     35 #include "output.h"
     36 #include "copy-relocs.h"
     37 #include "target.h"
     38 #include "target-reloc.h"
     39 #include "target-select.h"
     40 #include "tls.h"
     41 #include "freebsd.h"
     42 #include "nacl.h"
     43 #include "gc.h"
     44 #include "icf.h"
     45 
     46 namespace
     47 {
     48 
     49 using namespace gold;
     50 
     51 // A class to handle the .got.plt section.
     52 
     53 class Output_data_got_plt_x86_64 : public Output_section_data_build
     54 {
     55  public:
     56   Output_data_got_plt_x86_64(Layout* layout)
     57     : Output_section_data_build(8),
     58       layout_(layout)
     59   { }
     60 
     61   Output_data_got_plt_x86_64(Layout* layout, off_t data_size)
     62     : Output_section_data_build(data_size, 8),
     63       layout_(layout)
     64   { }
     65 
     66  protected:
     67   // Write out the PLT data.
     68   void
     69   do_write(Output_file*);
     70 
     71   // Write to a map file.
     72   void
     73   do_print_to_mapfile(Mapfile* mapfile) const
     74   { mapfile->print_output_data(this, "** GOT PLT"); }
     75 
     76  private:
     77   // A pointer to the Layout class, so that we can find the .dynamic
     78   // section when we write out the GOT PLT section.
     79   Layout* layout_;
     80 };
     81 
     82 // A class to handle the PLT data.
     83 // This is an abstract base class that handles most of the linker details
     84 // but does not know the actual contents of PLT entries.  The derived
     85 // classes below fill in those details.
     86 
     87 template<int size>
     88 class Output_data_plt_x86_64 : public Output_section_data
     89 {
     90  public:
     91   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
     92 
     93   Output_data_plt_x86_64(Layout* layout, uint64_t addralign,
     94 			 Output_data_got<64, false>* got,
     95 			 Output_data_got_plt_x86_64* got_plt,
     96 			 Output_data_space* got_irelative)
     97     : Output_section_data(addralign), tlsdesc_rel_(NULL),
     98       irelative_rel_(NULL), got_(got), got_plt_(got_plt),
     99       got_irelative_(got_irelative), count_(0), irelative_count_(0),
    100       tlsdesc_got_offset_(-1U), free_list_()
    101   { this->init(layout); }
    102 
    103   Output_data_plt_x86_64(Layout* layout, uint64_t plt_entry_size,
    104 			 Output_data_got<64, false>* got,
    105 			 Output_data_got_plt_x86_64* got_plt,
    106 			 Output_data_space* got_irelative,
    107 			 unsigned int plt_count)
    108     : Output_section_data((plt_count + 1) * plt_entry_size,
    109 			  plt_entry_size, false),
    110       tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got),
    111       got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count),
    112       irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_()
    113   {
    114     this->init(layout);
    115 
    116     // Initialize the free list and reserve the first entry.
    117     this->free_list_.init((plt_count + 1) * plt_entry_size, false);
    118     this->free_list_.remove(0, plt_entry_size);
    119   }
    120 
    121   // Initialize the PLT section.
    122   void
    123   init(Layout* layout);
    124 
    125   // Add an entry to the PLT.
    126   void
    127   add_entry(Symbol_table*, Layout*, Symbol* gsym);
    128 
    129   // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
    130   unsigned int
    131   add_local_ifunc_entry(Symbol_table* symtab, Layout*,
    132 			Sized_relobj_file<size, false>* relobj,
    133 			unsigned int local_sym_index);
    134 
    135   // Add the relocation for a PLT entry.
    136   void
    137   add_relocation(Symbol_table*, Layout*, Symbol* gsym,
    138 		 unsigned int got_offset);
    139 
    140   // Add the reserved TLSDESC_PLT entry to the PLT.
    141   void
    142   reserve_tlsdesc_entry(unsigned int got_offset)
    143   { this->tlsdesc_got_offset_ = got_offset; }
    144 
    145   // Return true if a TLSDESC_PLT entry has been reserved.
    146   bool
    147   has_tlsdesc_entry() const
    148   { return this->tlsdesc_got_offset_ != -1U; }
    149 
    150   // Return the GOT offset for the reserved TLSDESC_PLT entry.
    151   unsigned int
    152   get_tlsdesc_got_offset() const
    153   { return this->tlsdesc_got_offset_; }
    154 
    155   // Return the offset of the reserved TLSDESC_PLT entry.
    156   unsigned int
    157   get_tlsdesc_plt_offset() const
    158   {
    159     return ((this->count_ + this->irelative_count_ + 1)
    160 	    * this->get_plt_entry_size());
    161   }
    162 
    163   // Return the .rela.plt section data.
    164   Reloc_section*
    165   rela_plt()
    166   { return this->rel_; }
    167 
    168   // Return where the TLSDESC relocations should go.
    169   Reloc_section*
    170   rela_tlsdesc(Layout*);
    171 
    172   // Return where the IRELATIVE relocations should go in the PLT
    173   // relocations.
    174   Reloc_section*
    175   rela_irelative(Symbol_table*, Layout*);
    176 
    177   // Return whether we created a section for IRELATIVE relocations.
    178   bool
    179   has_irelative_section() const
    180   { return this->irelative_rel_ != NULL; }
    181 
    182   // Return the number of PLT entries.
    183   unsigned int
    184   entry_count() const
    185   { return this->count_ + this->irelative_count_; }
    186 
    187   // Return the offset of the first non-reserved PLT entry.
    188   unsigned int
    189   first_plt_entry_offset()
    190   { return this->get_plt_entry_size(); }
    191 
    192   // Return the size of a PLT entry.
    193   unsigned int
    194   get_plt_entry_size() const
    195   { return this->do_get_plt_entry_size(); }
    196 
    197   // Reserve a slot in the PLT for an existing symbol in an incremental update.
    198   void
    199   reserve_slot(unsigned int plt_index)
    200   {
    201     this->free_list_.remove((plt_index + 1) * this->get_plt_entry_size(),
    202 			    (plt_index + 2) * this->get_plt_entry_size());
    203   }
    204 
    205   // Return the PLT address to use for a global symbol.
    206   uint64_t
    207   address_for_global(const Symbol*);
    208 
    209   // Return the PLT address to use for a local symbol.
    210   uint64_t
    211   address_for_local(const Relobj*, unsigned int symndx);
    212 
    213   // Add .eh_frame information for the PLT.
    214   void
    215   add_eh_frame(Layout* layout)
    216   { this->do_add_eh_frame(layout); }
    217 
    218  protected:
    219   // Fill in the first PLT entry.
    220   void
    221   fill_first_plt_entry(unsigned char* pov,
    222 		       typename elfcpp::Elf_types<size>::Elf_Addr got_address,
    223 		       typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
    224   { this->do_fill_first_plt_entry(pov, got_address, plt_address); }
    225 
    226   // Fill in a normal PLT entry.  Returns the offset into the entry that
    227   // should be the initial GOT slot value.
    228   unsigned int
    229   fill_plt_entry(unsigned char* pov,
    230 		 typename elfcpp::Elf_types<size>::Elf_Addr got_address,
    231 		 typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
    232 		 unsigned int got_offset,
    233 		 unsigned int plt_offset,
    234 		 unsigned int plt_index)
    235   {
    236     return this->do_fill_plt_entry(pov, got_address, plt_address,
    237 				   got_offset, plt_offset, plt_index);
    238   }
    239 
    240   // Fill in the reserved TLSDESC PLT entry.
    241   void
    242   fill_tlsdesc_entry(unsigned char* pov,
    243 		     typename elfcpp::Elf_types<size>::Elf_Addr got_address,
    244 		     typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
    245 		     typename elfcpp::Elf_types<size>::Elf_Addr got_base,
    246 		     unsigned int tlsdesc_got_offset,
    247 		     unsigned int plt_offset)
    248   {
    249     this->do_fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
    250 				tlsdesc_got_offset, plt_offset);
    251   }
    252 
    253   virtual unsigned int
    254   do_get_plt_entry_size() const = 0;
    255 
    256   virtual void
    257   do_fill_first_plt_entry(unsigned char* pov,
    258 			  typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
    259 			  typename elfcpp::Elf_types<size>::Elf_Addr plt_addr)
    260     = 0;
    261 
    262   virtual unsigned int
    263   do_fill_plt_entry(unsigned char* pov,
    264 		    typename elfcpp::Elf_types<size>::Elf_Addr got_address,
    265 		    typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
    266 		    unsigned int got_offset,
    267 		    unsigned int plt_offset,
    268 		    unsigned int plt_index) = 0;
    269 
    270   virtual void
    271   do_fill_tlsdesc_entry(unsigned char* pov,
    272 			typename elfcpp::Elf_types<size>::Elf_Addr got_address,
    273 			typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
    274 			typename elfcpp::Elf_types<size>::Elf_Addr got_base,
    275 			unsigned int tlsdesc_got_offset,
    276 			unsigned int plt_offset) = 0;
    277 
    278   virtual void
    279   do_add_eh_frame(Layout* layout) = 0;
    280 
    281   void
    282   do_adjust_output_section(Output_section* os);
    283 
    284   // Write to a map file.
    285   void
    286   do_print_to_mapfile(Mapfile* mapfile) const
    287   { mapfile->print_output_data(this, _("** PLT")); }
    288 
    289   // The CIE of the .eh_frame unwind information for the PLT.
    290   static const int plt_eh_frame_cie_size = 16;
    291   static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
    292 
    293  private:
    294   // Set the final size.
    295   void
    296   set_final_data_size();
    297 
    298   // Write out the PLT data.
    299   void
    300   do_write(Output_file*);
    301 
    302   // The reloc section.
    303   Reloc_section* rel_;
    304   // The TLSDESC relocs, if necessary.  These must follow the regular
    305   // PLT relocs.
    306   Reloc_section* tlsdesc_rel_;
    307   // The IRELATIVE relocs, if necessary.  These must follow the
    308   // regular PLT relocations and the TLSDESC relocations.
    309   Reloc_section* irelative_rel_;
    310   // The .got section.
    311   Output_data_got<64, false>* got_;
    312   // The .got.plt section.
    313   Output_data_got_plt_x86_64* got_plt_;
    314   // The part of the .got.plt section used for IRELATIVE relocs.
    315   Output_data_space* got_irelative_;
    316   // The number of PLT entries.
    317   unsigned int count_;
    318   // Number of PLT entries with R_X86_64_IRELATIVE relocs.  These
    319   // follow the regular PLT entries.
    320   unsigned int irelative_count_;
    321   // Offset of the reserved TLSDESC_GOT entry when needed.
    322   unsigned int tlsdesc_got_offset_;
    323   // List of available regions within the section, for incremental
    324   // update links.
    325   Free_list free_list_;
    326 };
    327 
    328 template<int size>
    329 class Output_data_plt_x86_64_standard : public Output_data_plt_x86_64<size>
    330 {
    331  public:
    332   Output_data_plt_x86_64_standard(Layout* layout,
    333 				  Output_data_got<64, false>* got,
    334 				  Output_data_got_plt_x86_64* got_plt,
    335 				  Output_data_space* got_irelative)
    336     : Output_data_plt_x86_64<size>(layout, plt_entry_size,
    337 				   got, got_plt, got_irelative)
    338   { }
    339 
    340   Output_data_plt_x86_64_standard(Layout* layout,
    341 				  Output_data_got<64, false>* got,
    342 				  Output_data_got_plt_x86_64* got_plt,
    343 				  Output_data_space* got_irelative,
    344 				  unsigned int plt_count)
    345     : Output_data_plt_x86_64<size>(layout, plt_entry_size,
    346 				   got, got_plt, got_irelative,
    347 				   plt_count)
    348   { }
    349 
    350  protected:
    351   virtual unsigned int
    352   do_get_plt_entry_size() const
    353   { return plt_entry_size; }
    354 
    355   virtual void
    356   do_add_eh_frame(Layout* layout)
    357   {
    358     layout->add_eh_frame_for_plt(this,
    359 				 this->plt_eh_frame_cie,
    360 				 this->plt_eh_frame_cie_size,
    361 				 plt_eh_frame_fde,
    362 				 plt_eh_frame_fde_size);
    363   }
    364 
    365   virtual void
    366   do_fill_first_plt_entry(unsigned char* pov,
    367 			  typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
    368 			  typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
    369 
    370   virtual unsigned int
    371   do_fill_plt_entry(unsigned char* pov,
    372 		    typename elfcpp::Elf_types<size>::Elf_Addr got_address,
    373 		    typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
    374 		    unsigned int got_offset,
    375 		    unsigned int plt_offset,
    376 		    unsigned int plt_index);
    377 
    378   virtual void
    379   do_fill_tlsdesc_entry(unsigned char* pov,
    380 			typename elfcpp::Elf_types<size>::Elf_Addr got_address,
    381 			typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
    382 			typename elfcpp::Elf_types<size>::Elf_Addr got_base,
    383 			unsigned int tlsdesc_got_offset,
    384 			unsigned int plt_offset);
    385 
    386  private:
    387   // The size of an entry in the PLT.
    388   static const int plt_entry_size = 16;
    389 
    390   // The first entry in the PLT.
    391   // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
    392   // procedure linkage table for both programs and shared objects."
    393   static const unsigned char first_plt_entry[plt_entry_size];
    394 
    395   // Other entries in the PLT for an executable.
    396   static const unsigned char plt_entry[plt_entry_size];
    397 
    398   // The reserved TLSDESC entry in the PLT for an executable.
    399   static const unsigned char tlsdesc_plt_entry[plt_entry_size];
    400 
    401   // The .eh_frame unwind information for the PLT.
    402   static const int plt_eh_frame_fde_size = 32;
    403   static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
    404 };
    405 
    406 template<int size>
    407 class Lazy_view
    408 {
    409  public:
    410   Lazy_view(Sized_relobj_file<size, false>* object, unsigned int data_shndx)
    411     : object_(object), data_shndx_(data_shndx), view_(NULL), view_size_(0)
    412   { }
    413 
    414   inline unsigned char
    415   operator[](size_t offset)
    416   {
    417     if (this->view_ == NULL)
    418       this->view_ = this->object_->section_contents(this->data_shndx_,
    419                                                     &this->view_size_,
    420                                                     true);
    421     if (offset >= this->view_size_)
    422       return 0;
    423     return this->view_[offset];
    424   }
    425 
    426  private:
    427   Sized_relobj_file<size, false>* object_;
    428   unsigned int data_shndx_;
    429   const unsigned char* view_;
    430   section_size_type view_size_;
    431 };
    432 
    433 // The x86_64 target class.
    434 // See the ABI at
    435 //   http://www.x86-64.org/documentation/abi.pdf
    436 // TLS info comes from
    437 //   http://people.redhat.com/drepper/tls.pdf
    438 //   http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
    439 
    440 template<int size>
    441 class Target_x86_64 : public Sized_target<size, false>
    442 {
    443  public:
    444   // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
    445   // uses only Elf64_Rela relocation entries with explicit addends."
    446   typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section;
    447   typedef Output_data_reloc<elfcpp::SHT_RELR, true, size, false> Relr_section;
    448 
    449   Target_x86_64(const Target::Target_info* info = &x86_64_info)
    450     : Sized_target<size, false>(info),
    451       got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
    452       got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL),
    453       rela_irelative_(NULL), relr_dyn_(NULL),
    454       copy_relocs_(elfcpp::R_X86_64_COPY), got_mod_index_offset_(-1U),
    455       tlsdesc_reloc_info_(), tls_base_symbol_defined_(false)
    456   { }
    457 
    458   // The safe value for data segment size for PIE links.
    459   uint64_t
    460   max_pie_data_segment_size() const
    461   { return (128 * 1024 * 1024); }
    462 
    463   // Hook for a new output section.
    464   void
    465   do_new_output_section(Output_section*) const;
    466 
    467   // Scan the relocations to look for symbol adjustments.
    468   void
    469   gc_process_relocs(Symbol_table* symtab,
    470 		    Layout* layout,
    471 		    Sized_relobj_file<size, false>* object,
    472 		    unsigned int data_shndx,
    473 		    unsigned int sh_type,
    474 		    const unsigned char* prelocs,
    475 		    size_t reloc_count,
    476 		    Output_section* output_section,
    477 		    bool needs_special_offset_handling,
    478 		    size_t local_symbol_count,
    479 		    const unsigned char* plocal_symbols);
    480 
    481   // Scan the relocations to look for symbol adjustments.
    482   void
    483   scan_relocs(Symbol_table* symtab,
    484 	      Layout* layout,
    485 	      Sized_relobj_file<size, false>* object,
    486 	      unsigned int data_shndx,
    487 	      unsigned int sh_type,
    488 	      const unsigned char* prelocs,
    489 	      size_t reloc_count,
    490 	      Output_section* output_section,
    491 	      bool needs_special_offset_handling,
    492 	      size_t local_symbol_count,
    493 	      const unsigned char* plocal_symbols);
    494 
    495   // Finalize the sections.
    496   void
    497   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
    498 
    499   // Return the value to use for a dynamic which requires special
    500   // treatment.
    501   uint64_t
    502   do_dynsym_value(const Symbol*) const;
    503 
    504   // Relocate a section.
    505   void
    506   relocate_section(const Relocate_info<size, false>*,
    507 		   unsigned int sh_type,
    508 		   const unsigned char* prelocs,
    509 		   size_t reloc_count,
    510 		   Output_section* output_section,
    511 		   bool needs_special_offset_handling,
    512 		   unsigned char* view,
    513 		   typename elfcpp::Elf_types<size>::Elf_Addr view_address,
    514 		   section_size_type view_size,
    515 		   const Reloc_symbol_changes*);
    516 
    517   // Scan the relocs during a relocatable link.
    518   void
    519   scan_relocatable_relocs(Symbol_table* symtab,
    520 			  Layout* layout,
    521 			  Sized_relobj_file<size, false>* object,
    522 			  unsigned int data_shndx,
    523 			  unsigned int sh_type,
    524 			  const unsigned char* prelocs,
    525 			  size_t reloc_count,
    526 			  Output_section* output_section,
    527 			  bool needs_special_offset_handling,
    528 			  size_t local_symbol_count,
    529 			  const unsigned char* plocal_symbols,
    530 			  Relocatable_relocs*);
    531 
    532   // Scan the relocs for --emit-relocs.
    533   void
    534   emit_relocs_scan(Symbol_table* symtab,
    535 		   Layout* layout,
    536 		   Sized_relobj_file<size, false>* object,
    537 		   unsigned int data_shndx,
    538 		   unsigned int sh_type,
    539 		   const unsigned char* prelocs,
    540 		   size_t reloc_count,
    541 		   Output_section* output_section,
    542 		   bool needs_special_offset_handling,
    543 		   size_t local_symbol_count,
    544 		   const unsigned char* plocal_syms,
    545 		   Relocatable_relocs* rr);
    546 
    547   // Emit relocations for a section.
    548   void
    549   relocate_relocs(
    550       const Relocate_info<size, false>*,
    551       unsigned int sh_type,
    552       const unsigned char* prelocs,
    553       size_t reloc_count,
    554       Output_section* output_section,
    555       typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
    556       unsigned char* view,
    557       typename elfcpp::Elf_types<size>::Elf_Addr view_address,
    558       section_size_type view_size,
    559       unsigned char* reloc_view,
    560       section_size_type reloc_view_size);
    561 
    562   // Return a string used to fill a code section with nops.
    563   std::string
    564   do_code_fill(section_size_type length) const;
    565 
    566   // Return whether SYM is defined by the ABI.
    567   bool
    568   do_is_defined_by_abi(const Symbol* sym) const
    569   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
    570 
    571   // Return the symbol index to use for a target specific relocation.
    572   // The only target specific relocation is R_X86_64_TLSDESC for a
    573   // local symbol, which is an absolute reloc.
    574   unsigned int
    575   do_reloc_symbol_index(void*, unsigned int r_type) const
    576   {
    577     gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
    578     return 0;
    579   }
    580 
    581   // Return the addend to use for a target specific relocation.
    582   uint64_t
    583   do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
    584 
    585   // Return the PLT section.
    586   uint64_t
    587   do_plt_address_for_global(const Symbol* gsym) const
    588   { return this->plt_section()->address_for_global(gsym); }
    589 
    590   uint64_t
    591   do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
    592   { return this->plt_section()->address_for_local(relobj, symndx); }
    593 
    594   // This function should be defined in targets that can use relocation
    595   // types to determine (implemented in local_reloc_may_be_function_pointer
    596   // and global_reloc_may_be_function_pointer)
    597   // if a function's pointer is taken.  ICF uses this in safe mode to only
    598   // fold those functions whose pointer is defintely not taken.  For x86_64
    599   // pie binaries, safe ICF cannot be done by looking at only relocation
    600   // types, and for certain cases (e.g. R_X86_64_PC32), the instruction
    601   // opcode is checked as well to distinguish a function call from taking
    602   // a function's pointer.
    603   bool
    604   do_can_check_for_function_pointers() const
    605   { return true; }
    606 
    607   // Return the base for a DW_EH_PE_datarel encoding.
    608   uint64_t
    609   do_ehframe_datarel_base() const;
    610 
    611   // Adjust -fsplit-stack code which calls non-split-stack code.
    612   void
    613   do_calls_non_split(Relobj* object, unsigned int shndx,
    614 		     section_offset_type fnoffset, section_size_type fnsize,
    615 		     const unsigned char* prelocs, size_t reloc_count,
    616 		     unsigned char* view, section_size_type view_size,
    617 		     std::string* from, std::string* to) const;
    618 
    619   // Return the size of the GOT section.
    620   section_size_type
    621   got_size() const
    622   {
    623     gold_assert(this->got_ != NULL);
    624     return this->got_->data_size();
    625   }
    626 
    627   // Return the number of entries in the GOT.
    628   unsigned int
    629   got_entry_count() const
    630   {
    631     if (this->got_ == NULL)
    632       return 0;
    633     return this->got_size() / 8;
    634   }
    635 
    636   // Return the number of entries in the PLT.
    637   unsigned int
    638   plt_entry_count() const;
    639 
    640   // Return the offset of the first non-reserved PLT entry.
    641   unsigned int
    642   first_plt_entry_offset() const;
    643 
    644   // Return the size of each PLT entry.
    645   unsigned int
    646   plt_entry_size() const;
    647 
    648   // Return the size of each GOT entry.
    649   unsigned int
    650   got_entry_size() const
    651   { return 8; };
    652 
    653   // Create the GOT section for an incremental update.
    654   Output_data_got_base*
    655   init_got_plt_for_update(Symbol_table* symtab,
    656 			  Layout* layout,
    657 			  unsigned int got_count,
    658 			  unsigned int plt_count);
    659 
    660   // Reserve a GOT entry for a local symbol, and regenerate any
    661   // necessary dynamic relocations.
    662   void
    663   reserve_local_got_entry(unsigned int got_index,
    664 			  Sized_relobj<size, false>* obj,
    665 			  unsigned int r_sym,
    666 			  unsigned int got_type);
    667 
    668   // Reserve a GOT entry for a global symbol, and regenerate any
    669   // necessary dynamic relocations.
    670   void
    671   reserve_global_got_entry(unsigned int got_index, Symbol* gsym,
    672 			   unsigned int got_type);
    673 
    674   // Register an existing PLT entry for a global symbol.
    675   void
    676   register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index,
    677 			    Symbol* gsym);
    678 
    679   // Force a COPY relocation for a given symbol.
    680   void
    681   emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t);
    682 
    683   // Apply an incremental relocation.
    684   void
    685   apply_relocation(const Relocate_info<size, false>* relinfo,
    686 		   typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
    687 		   unsigned int r_type,
    688 		   typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
    689 		   const Symbol* gsym,
    690 		   unsigned char* view,
    691 		   typename elfcpp::Elf_types<size>::Elf_Addr address,
    692 		   section_size_type view_size);
    693 
    694   // Add a new reloc argument, returning the index in the vector.
    695   size_t
    696   add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym)
    697   {
    698     this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
    699     return this->tlsdesc_reloc_info_.size() - 1;
    700   }
    701 
    702   Output_data_plt_x86_64<size>*
    703   make_data_plt(Layout* layout,
    704 		Output_data_got<64, false>* got,
    705 		Output_data_got_plt_x86_64* got_plt,
    706 		Output_data_space* got_irelative)
    707   {
    708     return this->do_make_data_plt(layout, got, got_plt, got_irelative);
    709   }
    710 
    711   Output_data_plt_x86_64<size>*
    712   make_data_plt(Layout* layout,
    713 		Output_data_got<64, false>* got,
    714 		Output_data_got_plt_x86_64* got_plt,
    715 		Output_data_space* got_irelative,
    716 		unsigned int plt_count)
    717   {
    718     return this->do_make_data_plt(layout, got, got_plt, got_irelative,
    719 				  plt_count);
    720   }
    721 
    722   virtual Output_data_plt_x86_64<size>*
    723   do_make_data_plt(Layout* layout,
    724 		   Output_data_got<64, false>* got,
    725 		   Output_data_got_plt_x86_64* got_plt,
    726 		   Output_data_space* got_irelative)
    727   {
    728     return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt,
    729 						     got_irelative);
    730   }
    731 
    732   virtual Output_data_plt_x86_64<size>*
    733   do_make_data_plt(Layout* layout,
    734 		   Output_data_got<64, false>* got,
    735 		   Output_data_got_plt_x86_64* got_plt,
    736 		   Output_data_space* got_irelative,
    737 		   unsigned int plt_count)
    738   {
    739     return new Output_data_plt_x86_64_standard<size>(layout, got, got_plt,
    740 						     got_irelative,
    741 						     plt_count);
    742   }
    743 
    744   bool
    745   do_may_relax() const
    746   {
    747     // If generating '.relr.dyn' section, we need a relaxation pass
    748     // to do the shrinking after all the offsets have been populated.
    749     return parameters->options().experimental_use_relr();
    750   }
    751 
    752   bool
    753   do_relax (int pass, const Input_objects*, Symbol_table*, Layout* layout, const Task*)
    754   {
    755     if (pass > 1)
    756       return false;
    757 
    758     Layout::Section_list::const_iterator p = layout->section_list().begin();
    759     for ( ; p != layout->section_list().end(); ++p)
    760       {
    761 	if (is_prefix_of(".relr.dyn", (*p)->name()))
    762 	  break;
    763       }
    764 
    765     if (p == layout->section_list().end())
    766       return false;
    767 
    768     Output_section * const os = *p;
    769     for (Output_section::Input_section_list::iterator ip = os->input_sections().begin();
    770          ip != os->input_sections().end();
    771          ++ip)
    772       {
    773         Relr_section *od = static_cast<Relr_section *>(ip->output_section_data());
    774         od->shrink_relocs();
    775       }
    776 
    777     return true;
    778   }
    779 
    780 
    781  private:
    782   // The class which scans relocations.
    783   class Scan
    784   {
    785   public:
    786     Scan()
    787       : issued_non_pic_error_(false)
    788     { }
    789 
    790     static inline int
    791     get_reference_flags(unsigned int r_type);
    792 
    793     inline void
    794     local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
    795 	  Sized_relobj_file<size, false>* object,
    796 	  unsigned int data_shndx,
    797 	  Output_section* output_section,
    798 	  const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
    799 	  const elfcpp::Sym<size, false>& lsym,
    800 	  bool is_discarded);
    801 
    802     inline void
    803     global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
    804 	   Sized_relobj_file<size, false>* object,
    805 	   unsigned int data_shndx,
    806 	   Output_section* output_section,
    807 	   const elfcpp::Rela<size, false>& reloc, unsigned int r_type,
    808 	   Symbol* gsym);
    809 
    810     inline bool
    811     local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
    812 					Target_x86_64* target,
    813 					Sized_relobj_file<size, false>* object,
    814 					unsigned int data_shndx,
    815 					Output_section* output_section,
    816 					const elfcpp::Rela<size, false>& reloc,
    817 					unsigned int r_type,
    818 					const elfcpp::Sym<size, false>& lsym);
    819 
    820     inline bool
    821     global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
    822 					 Target_x86_64* target,
    823 					 Sized_relobj_file<size, false>* object,
    824 					 unsigned int data_shndx,
    825 					 Output_section* output_section,
    826 					 const elfcpp::Rela<size, false>& reloc,
    827 					 unsigned int r_type,
    828 					 Symbol* gsym);
    829 
    830   private:
    831     static void
    832     unsupported_reloc_local(Sized_relobj_file<size, false>*,
    833 			    unsigned int r_type);
    834 
    835     static void
    836     unsupported_reloc_global(Sized_relobj_file<size, false>*,
    837 			     unsigned int r_type, Symbol*);
    838 
    839     void
    840     check_non_pic(Relobj*, unsigned int r_type, Symbol*);
    841 
    842     inline bool
    843     possible_function_pointer_reloc(Sized_relobj_file<size, false>* src_obj,
    844                                     unsigned int src_indx,
    845                                     unsigned int r_offset,
    846                                     unsigned int r_type);
    847 
    848     bool
    849     reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*,
    850 			      unsigned int r_type);
    851 
    852     // Whether we have issued an error about a non-PIC compilation.
    853     bool issued_non_pic_error_;
    854   };
    855 
    856   // The class which implements relocation.
    857   class Relocate
    858   {
    859    public:
    860     Relocate()
    861       : skip_call_tls_get_addr_(false)
    862     { }
    863 
    864     ~Relocate()
    865     {
    866       if (this->skip_call_tls_get_addr_)
    867 	{
    868 	  // FIXME: This needs to specify the location somehow.
    869 	  gold_error(_("missing expected TLS relocation"));
    870 	}
    871     }
    872 
    873     // Do a relocation.  Return false if the caller should not issue
    874     // any warnings about this relocation.
    875     inline bool
    876     relocate(const Relocate_info<size, false>*, unsigned int,
    877 	     Target_x86_64*, Output_section*, size_t, const unsigned char*,
    878 	     const Sized_symbol<size>*, const Symbol_value<size>*,
    879 	     unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
    880 	     section_size_type);
    881 
    882    private:
    883     // Do a TLS relocation.
    884     inline void
    885     relocate_tls(const Relocate_info<size, false>*, Target_x86_64*,
    886 		 size_t relnum, const elfcpp::Rela<size, false>&,
    887 		 unsigned int r_type, const Sized_symbol<size>*,
    888 		 const Symbol_value<size>*,
    889 		 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr,
    890 		 section_size_type);
    891 
    892     // Do a TLS General-Dynamic to Initial-Exec transition.
    893     inline void
    894     tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
    895 		 const elfcpp::Rela<size, false>&, unsigned int r_type,
    896 		 typename elfcpp::Elf_types<size>::Elf_Addr value,
    897 		 unsigned char* view,
    898 		 typename elfcpp::Elf_types<size>::Elf_Addr,
    899 		 section_size_type view_size);
    900 
    901     // Do a TLS General-Dynamic to Local-Exec transition.
    902     inline void
    903     tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
    904 		 Output_segment* tls_segment,
    905 		 const elfcpp::Rela<size, false>&, unsigned int r_type,
    906 		 typename elfcpp::Elf_types<size>::Elf_Addr value,
    907 		 unsigned char* view,
    908 		 section_size_type view_size);
    909 
    910     // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
    911     inline void
    912     tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum,
    913 		      const elfcpp::Rela<size, false>&, unsigned int r_type,
    914 		      typename elfcpp::Elf_types<size>::Elf_Addr value,
    915 		      unsigned char* view,
    916 		      typename elfcpp::Elf_types<size>::Elf_Addr,
    917 		      section_size_type view_size);
    918 
    919     // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
    920     inline void
    921     tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum,
    922 		      Output_segment* tls_segment,
    923 		      const elfcpp::Rela<size, false>&, unsigned int r_type,
    924 		      typename elfcpp::Elf_types<size>::Elf_Addr value,
    925 		      unsigned char* view,
    926 		      section_size_type view_size);
    927 
    928     // Do a TLS Local-Dynamic to Local-Exec transition.
    929     inline void
    930     tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum,
    931 		 Output_segment* tls_segment,
    932 		 const elfcpp::Rela<size, false>&, unsigned int r_type,
    933 		 typename elfcpp::Elf_types<size>::Elf_Addr value,
    934 		 unsigned char* view,
    935 		 section_size_type view_size);
    936 
    937     // Do a TLS Initial-Exec to Local-Exec transition.
    938     static inline void
    939     tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum,
    940 		 Output_segment* tls_segment,
    941 		 const elfcpp::Rela<size, false>&, unsigned int r_type,
    942 		 typename elfcpp::Elf_types<size>::Elf_Addr value,
    943 		 unsigned char* view,
    944 		 section_size_type view_size);
    945 
    946     // This is set if we should skip the next reloc, which should be a
    947     // PLT32 reloc against ___tls_get_addr.
    948     bool skip_call_tls_get_addr_;
    949   };
    950 
    951   // Check if relocation against this symbol is a candidate for
    952   // conversion from
    953   // mov foo@GOTPCREL(%rip), %reg
    954   // to lea foo(%rip), %reg.
    955   template<class View_type>
    956   static inline bool
    957   can_convert_mov_to_lea(const Symbol* gsym, unsigned int r_type,
    958                          size_t r_offset, View_type* view)
    959   {
    960     gold_assert(gsym != NULL);
    961     // We cannot do the conversion unless it's one of these relocations.
    962     if (r_type != elfcpp::R_X86_64_GOTPCREL
    963         && r_type != elfcpp::R_X86_64_GOTPCRELX
    964         && r_type != elfcpp::R_X86_64_REX_GOTPCRELX)
    965       return false;
    966     // We cannot convert references to IFUNC symbols, or to symbols that
    967     // are not local to the current module.
    968     if (gsym->type() == elfcpp::STT_GNU_IFUNC
    969         || gsym->is_undefined ()
    970         || gsym->is_from_dynobj()
    971         || gsym->is_preemptible())
    972       return false;
    973     // If we are building a shared object and the symbol is protected, we may
    974     // need to go through the GOT.
    975     if (parameters->options().shared()
    976         && gsym->visibility() == elfcpp::STV_PROTECTED)
    977       return false;
    978     // We cannot convert references to the _DYNAMIC symbol.
    979     if (strcmp(gsym->name(), "_DYNAMIC") == 0)
    980       return false;
    981     // Check for a MOV opcode.
    982     return (*view)[r_offset - 2] == 0x8b;
    983   }
    984 
    985   // Convert
    986   // callq *foo@GOTPCRELX(%rip) to
    987   // addr32 callq foo
    988   // and jmpq *foo@GOTPCRELX(%rip) to
    989   // jmpq foo
    990   // nop
    991   template<class View_type>
    992   static inline bool
    993   can_convert_callq_to_direct(const Symbol* gsym, unsigned int r_type,
    994 			      size_t r_offset, View_type* view)
    995   {
    996     gold_assert(gsym != NULL);
    997     // We cannot do the conversion unless it's a GOTPCRELX relocation.
    998     if (r_type != elfcpp::R_X86_64_GOTPCRELX)
    999       return false;
   1000     // We cannot convert references to IFUNC symbols, or to symbols that
   1001     // are not local to the current module.
   1002     if (gsym->type() == elfcpp::STT_GNU_IFUNC
   1003         || gsym->is_undefined ()
   1004         || gsym->is_from_dynobj()
   1005         || gsym->is_preemptible())
   1006       return false;
   1007     // Check for a CALLQ or JMPQ opcode.
   1008     return ((*view)[r_offset - 2] == 0xff
   1009             && ((*view)[r_offset - 1] == 0x15
   1010                 || (*view)[r_offset - 1] == 0x25));
   1011   }
   1012 
   1013   // Adjust TLS relocation type based on the options and whether this
   1014   // is a local symbol.
   1015   static tls::Tls_optimization
   1016   optimize_tls_reloc(bool is_final, int r_type);
   1017 
   1018   // Get the GOT section, creating it if necessary.
   1019   Output_data_got<64, false>*
   1020   got_section(Symbol_table*, Layout*);
   1021 
   1022   // Get the GOT PLT section.
   1023   Output_data_got_plt_x86_64*
   1024   got_plt_section() const
   1025   {
   1026     gold_assert(this->got_plt_ != NULL);
   1027     return this->got_plt_;
   1028   }
   1029 
   1030   // Get the GOT section for TLSDESC entries.
   1031   Output_data_got<64, false>*
   1032   got_tlsdesc_section() const
   1033   {
   1034     gold_assert(this->got_tlsdesc_ != NULL);
   1035     return this->got_tlsdesc_;
   1036   }
   1037 
   1038   // Create the PLT section.
   1039   void
   1040   make_plt_section(Symbol_table* symtab, Layout* layout);
   1041 
   1042   // Create a PLT entry for a global symbol.
   1043   void
   1044   make_plt_entry(Symbol_table*, Layout*, Symbol*);
   1045 
   1046   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
   1047   void
   1048   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
   1049 			     Sized_relobj_file<size, false>* relobj,
   1050 			     unsigned int local_sym_index);
   1051 
   1052   // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
   1053   void
   1054   define_tls_base_symbol(Symbol_table*, Layout*);
   1055 
   1056   // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
   1057   void
   1058   reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
   1059 
   1060   // Create a GOT entry for the TLS module index.
   1061   unsigned int
   1062   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
   1063 		      Sized_relobj_file<size, false>* object);
   1064 
   1065   // Get the PLT section.
   1066   Output_data_plt_x86_64<size>*
   1067   plt_section() const
   1068   {
   1069     gold_assert(this->plt_ != NULL);
   1070     return this->plt_;
   1071   }
   1072 
   1073   // Get the dynamic reloc section, creating it if necessary.
   1074   Reloc_section*
   1075   rela_dyn_section(Layout*);
   1076 
   1077   // Get the section to use for TLSDESC relocations.
   1078   Reloc_section*
   1079   rela_tlsdesc_section(Layout*) const;
   1080 
   1081   // Get the section to use for IRELATIVE relocations.
   1082   Reloc_section*
   1083   rela_irelative_section(Layout*);
   1084 
   1085   // Get the RELR dynamic reloc section, creating it if necessary.
   1086   Relr_section*
   1087   relr_dyn_section(Layout*);
   1088 
   1089   // Add a potential copy relocation.
   1090   void
   1091   copy_reloc(Symbol_table* symtab, Layout* layout,
   1092 	     Sized_relobj_file<size, false>* object,
   1093 	     unsigned int shndx, Output_section* output_section,
   1094 	     Symbol* sym, const elfcpp::Rela<size, false>& reloc)
   1095   {
   1096     unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info());
   1097     this->copy_relocs_.copy_reloc(symtab, layout,
   1098 				  symtab->get_sized_symbol<size>(sym),
   1099 				  object, shndx, output_section,
   1100 				  r_type, reloc.get_r_offset(),
   1101 				  reloc.get_r_addend(),
   1102 				  this->rela_dyn_section(layout));
   1103   }
   1104 
   1105   // Information about this specific target which we pass to the
   1106   // general Target structure.
   1107   static const Target::Target_info x86_64_info;
   1108 
   1109   // The types of GOT entries needed for this platform.
   1110   // These values are exposed to the ABI in an incremental link.
   1111   // Do not renumber existing values without changing the version
   1112   // number of the .gnu_incremental_inputs section.
   1113   enum Got_type
   1114   {
   1115     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
   1116     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
   1117     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
   1118     GOT_TYPE_TLS_DESC = 3       // GOT entry for TLS_DESC pair
   1119   };
   1120 
   1121   // This type is used as the argument to the target specific
   1122   // relocation routines.  The only target specific reloc is
   1123   // R_X86_64_TLSDESC against a local symbol.
   1124   struct Tlsdesc_info
   1125   {
   1126     Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym)
   1127       : object(a_object), r_sym(a_r_sym)
   1128     { }
   1129 
   1130     // The object in which the local symbol is defined.
   1131     Sized_relobj_file<size, false>* object;
   1132     // The local symbol index in the object.
   1133     unsigned int r_sym;
   1134   };
   1135 
   1136   // The GOT section.
   1137   Output_data_got<64, false>* got_;
   1138   // The PLT section.
   1139   Output_data_plt_x86_64<size>* plt_;
   1140   // The GOT PLT section.
   1141   Output_data_got_plt_x86_64* got_plt_;
   1142   // The GOT section for IRELATIVE relocations.
   1143   Output_data_space* got_irelative_;
   1144   // The GOT section for TLSDESC relocations.
   1145   Output_data_got<64, false>* got_tlsdesc_;
   1146   // The _GLOBAL_OFFSET_TABLE_ symbol.
   1147   Symbol* global_offset_table_;
   1148   // The dynamic reloc section.
   1149   Reloc_section* rela_dyn_;
   1150   // The section to use for IRELATIVE relocs.
   1151   Reloc_section* rela_irelative_;
   1152   // The RELR dynamic reloc section.
   1153   Relr_section* relr_dyn_;
   1154   // Relocs saved to avoid a COPY reloc.
   1155   Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_;
   1156   // Offset of the GOT entry for the TLS module index.
   1157   unsigned int got_mod_index_offset_;
   1158   // We handle R_X86_64_TLSDESC against a local symbol as a target
   1159   // specific relocation.  Here we store the object and local symbol
   1160   // index for the relocation.
   1161   std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
   1162   // True if the _TLS_MODULE_BASE_ symbol has been defined.
   1163   bool tls_base_symbol_defined_;
   1164 };
   1165 
   1166 template<>
   1167 const Target::Target_info Target_x86_64<64>::x86_64_info =
   1168 {
   1169   64,			// size
   1170   false,		// is_big_endian
   1171   elfcpp::EM_X86_64,	// machine_code
   1172   false,		// has_make_symbol
   1173   false,		// has_resolve
   1174   true,			// has_code_fill
   1175   true,			// is_default_stack_executable
   1176   true,			// can_icf_inline_merge_sections
   1177   '\0',			// wrap_char
   1178   "/lib/ld64.so.1",     // program interpreter
   1179   0x400000,		// default_text_segment_address
   1180   0x1000,		// abi_pagesize (overridable by -z max-page-size)
   1181   0x1000,		// common_pagesize (overridable by -z common-page-size)
   1182   false,                // isolate_execinstr
   1183   0,                    // rosegment_gap
   1184   elfcpp::SHN_UNDEF,	// small_common_shndx
   1185   elfcpp::SHN_X86_64_LCOMMON,	// large_common_shndx
   1186   0,			// small_common_section_flags
   1187   elfcpp::SHF_X86_64_LARGE,	// large_common_section_flags
   1188   NULL,			// attributes_section
   1189   NULL,			// attributes_vendor
   1190   "_start",		// entry_symbol_name
   1191   32,			// hash_entry_size
   1192 };
   1193 
   1194 template<>
   1195 const Target::Target_info Target_x86_64<32>::x86_64_info =
   1196 {
   1197   32,			// size
   1198   false,		// is_big_endian
   1199   elfcpp::EM_X86_64,	// machine_code
   1200   false,		// has_make_symbol
   1201   false,		// has_resolve
   1202   true,			// has_code_fill
   1203   true,			// is_default_stack_executable
   1204   true,			// can_icf_inline_merge_sections
   1205   '\0',			// wrap_char
   1206   "/libx32/ldx32.so.1", // program interpreter
   1207   0x400000,		// default_text_segment_address
   1208   0x1000,		// abi_pagesize (overridable by -z max-page-size)
   1209   0x1000,		// common_pagesize (overridable by -z common-page-size)
   1210   false,                // isolate_execinstr
   1211   0,                    // rosegment_gap
   1212   elfcpp::SHN_UNDEF,	// small_common_shndx
   1213   elfcpp::SHN_X86_64_LCOMMON,	// large_common_shndx
   1214   0,			// small_common_section_flags
   1215   elfcpp::SHF_X86_64_LARGE,	// large_common_section_flags
   1216   NULL,			// attributes_section
   1217   NULL,			// attributes_vendor
   1218   "_start",		// entry_symbol_name
   1219   32,			// hash_entry_size
   1220 };
   1221 
   1222 // This is called when a new output section is created.  This is where
   1223 // we handle the SHF_X86_64_LARGE.
   1224 
   1225 template<int size>
   1226 void
   1227 Target_x86_64<size>::do_new_output_section(Output_section* os) const
   1228 {
   1229   if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
   1230     os->set_is_large_section();
   1231 }
   1232 
   1233 // Get the GOT section, creating it if necessary.
   1234 
   1235 template<int size>
   1236 Output_data_got<64, false>*
   1237 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout)
   1238 {
   1239   if (this->got_ == NULL)
   1240     {
   1241       gold_assert(symtab != NULL && layout != NULL);
   1242 
   1243       // When using -z now, we can treat .got.plt as a relro section.
   1244       // Without -z now, it is modified after program startup by lazy
   1245       // PLT relocations.
   1246       bool is_got_plt_relro = parameters->options().now();
   1247       Output_section_order got_order = (is_got_plt_relro
   1248 					? ORDER_RELRO
   1249 					: ORDER_RELRO_LAST);
   1250       Output_section_order got_plt_order = (is_got_plt_relro
   1251 					    ? ORDER_RELRO
   1252 					    : ORDER_NON_RELRO_FIRST);
   1253 
   1254       this->got_ = new Output_data_got<64, false>();
   1255 
   1256       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
   1257 				      (elfcpp::SHF_ALLOC
   1258 				       | elfcpp::SHF_WRITE),
   1259 				      this->got_, got_order, true);
   1260 
   1261       this->got_plt_ = new Output_data_got_plt_x86_64(layout);
   1262       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
   1263 				      (elfcpp::SHF_ALLOC
   1264 				       | elfcpp::SHF_WRITE),
   1265 				      this->got_plt_, got_plt_order,
   1266 				      is_got_plt_relro);
   1267 
   1268       // The first three entries are reserved.
   1269       this->got_plt_->set_current_data_size(3 * 8);
   1270 
   1271       if (!is_got_plt_relro)
   1272 	{
   1273 	  // Those bytes can go into the relro segment.
   1274 	  layout->increase_relro(3 * 8);
   1275 	}
   1276 
   1277       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
   1278       this->global_offset_table_ =
   1279 	symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
   1280 				      Symbol_table::PREDEFINED,
   1281 				      this->got_plt_,
   1282 				      0, 0, elfcpp::STT_OBJECT,
   1283 				      elfcpp::STB_LOCAL,
   1284 				      elfcpp::STV_HIDDEN, 0,
   1285 				      false, false);
   1286 
   1287       // If there are any IRELATIVE relocations, they get GOT entries
   1288       // in .got.plt after the jump slot entries.
   1289       this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT");
   1290       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
   1291 				      (elfcpp::SHF_ALLOC
   1292 				       | elfcpp::SHF_WRITE),
   1293 				      this->got_irelative_,
   1294 				      got_plt_order, is_got_plt_relro);
   1295 
   1296       // If there are any TLSDESC relocations, they get GOT entries in
   1297       // .got.plt after the jump slot and IRELATIVE entries.
   1298       this->got_tlsdesc_ = new Output_data_got<64, false>();
   1299       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
   1300 				      (elfcpp::SHF_ALLOC
   1301 				       | elfcpp::SHF_WRITE),
   1302 				      this->got_tlsdesc_,
   1303 				      got_plt_order, is_got_plt_relro);
   1304     }
   1305 
   1306   return this->got_;
   1307 }
   1308 
   1309 // Get the dynamic reloc section, creating it if necessary.
   1310 
   1311 template<int size>
   1312 typename Target_x86_64<size>::Reloc_section*
   1313 Target_x86_64<size>::rela_dyn_section(Layout* layout)
   1314 {
   1315   if (this->rela_dyn_ == NULL)
   1316     {
   1317       gold_assert(layout != NULL);
   1318       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
   1319       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
   1320 				      elfcpp::SHF_ALLOC, this->rela_dyn_,
   1321 				      ORDER_DYNAMIC_RELOCS, false);
   1322     }
   1323   return this->rela_dyn_;
   1324 }
   1325 
   1326 // Get the section to use for IRELATIVE relocs, creating it if
   1327 // necessary.  These go in .rela.dyn, but only after all other dynamic
   1328 // relocations.  They need to follow the other dynamic relocations so
   1329 // that they can refer to global variables initialized by those
   1330 // relocs.
   1331 
   1332 template<int size>
   1333 typename Target_x86_64<size>::Reloc_section*
   1334 Target_x86_64<size>::rela_irelative_section(Layout* layout)
   1335 {
   1336   if (this->rela_irelative_ == NULL)
   1337     {
   1338       // Make sure we have already created the dynamic reloc section.
   1339       this->rela_dyn_section(layout);
   1340       this->rela_irelative_ = new Reloc_section(false);
   1341       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
   1342 				      elfcpp::SHF_ALLOC, this->rela_irelative_,
   1343 				      ORDER_DYNAMIC_RELOCS, false);
   1344       gold_assert(this->rela_dyn_->output_section()
   1345 		  == this->rela_irelative_->output_section());
   1346     }
   1347   return this->rela_irelative_;
   1348 }
   1349 
   1350 // Get the RELR dynamic reloc section, creating it if necessary.
   1351 
   1352 template<int size>
   1353 typename Target_x86_64<size>::Relr_section*
   1354 Target_x86_64<size>::relr_dyn_section(Layout* layout)
   1355 {
   1356   if (this->relr_dyn_ == NULL)
   1357     {
   1358       gold_assert(layout != NULL);
   1359       this->relr_dyn_ = new Relr_section();
   1360       layout->add_output_section_data(".relr.dyn", elfcpp::SHT_RELR,
   1361 				      elfcpp::SHF_ALLOC, this->relr_dyn_,
   1362 				      ORDER_DYNAMIC_RELOCS, false);
   1363     }
   1364   return this->relr_dyn_;
   1365 }
   1366 
   1367 // Write the first three reserved words of the .got.plt section.
   1368 // The remainder of the section is written while writing the PLT
   1369 // in Output_data_plt_i386::do_write.
   1370 
   1371 void
   1372 Output_data_got_plt_x86_64::do_write(Output_file* of)
   1373 {
   1374   // The first entry in the GOT is the address of the .dynamic section
   1375   // aka the PT_DYNAMIC segment.  The next two entries are reserved.
   1376   // We saved space for them when we created the section in
   1377   // Target_x86_64::got_section.
   1378   const off_t got_file_offset = this->offset();
   1379   gold_assert(this->data_size() >= 24);
   1380   unsigned char* const got_view = of->get_output_view(got_file_offset, 24);
   1381   Output_section* dynamic = this->layout_->dynamic_section();
   1382   uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
   1383   elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr);
   1384   memset(got_view + 8, 0, 16);
   1385   of->write_output_view(got_file_offset, 24, got_view);
   1386 }
   1387 
   1388 // Initialize the PLT section.
   1389 
   1390 template<int size>
   1391 void
   1392 Output_data_plt_x86_64<size>::init(Layout* layout)
   1393 {
   1394   this->rel_ = new Reloc_section(false);
   1395   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
   1396 				  elfcpp::SHF_ALLOC, this->rel_,
   1397 				  ORDER_DYNAMIC_PLT_RELOCS, false);
   1398 }
   1399 
   1400 template<int size>
   1401 void
   1402 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os)
   1403 {
   1404   os->set_entsize(this->get_plt_entry_size());
   1405 }
   1406 
   1407 // Add an entry to the PLT.
   1408 
   1409 template<int size>
   1410 void
   1411 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout,
   1412 					Symbol* gsym)
   1413 {
   1414   gold_assert(!gsym->has_plt_offset());
   1415 
   1416   unsigned int plt_index;
   1417   off_t plt_offset;
   1418   section_offset_type got_offset;
   1419 
   1420   unsigned int* pcount;
   1421   unsigned int offset;
   1422   unsigned int reserved;
   1423   Output_section_data_build* got;
   1424   if (gsym->type() == elfcpp::STT_GNU_IFUNC
   1425       && gsym->can_use_relative_reloc(false))
   1426     {
   1427       pcount = &this->irelative_count_;
   1428       offset = 0;
   1429       reserved = 0;
   1430       got = this->got_irelative_;
   1431     }
   1432   else
   1433     {
   1434       pcount = &this->count_;
   1435       offset = 1;
   1436       reserved = 3;
   1437       got = this->got_plt_;
   1438     }
   1439 
   1440   if (!this->is_data_size_valid())
   1441     {
   1442       // Note that when setting the PLT offset for a non-IRELATIVE
   1443       // entry we skip the initial reserved PLT entry.
   1444       plt_index = *pcount + offset;
   1445       plt_offset = plt_index * this->get_plt_entry_size();
   1446 
   1447       ++*pcount;
   1448 
   1449       got_offset = (plt_index - offset + reserved) * 8;
   1450       gold_assert(got_offset == got->current_data_size());
   1451 
   1452       // Every PLT entry needs a GOT entry which points back to the PLT
   1453       // entry (this will be changed by the dynamic linker, normally
   1454       // lazily when the function is called).
   1455       got->set_current_data_size(got_offset + 8);
   1456     }
   1457   else
   1458     {
   1459       // FIXME: This is probably not correct for IRELATIVE relocs.
   1460 
   1461       // For incremental updates, find an available slot.
   1462       plt_offset = this->free_list_.allocate(this->get_plt_entry_size(),
   1463 					     this->get_plt_entry_size(), 0);
   1464       if (plt_offset == -1)
   1465 	gold_fallback(_("out of patch space (PLT);"
   1466 			" relink with --incremental-full"));
   1467 
   1468       // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset
   1469       // can be calculated from the PLT index, adjusting for the three
   1470       // reserved entries at the beginning of the GOT.
   1471       plt_index = plt_offset / this->get_plt_entry_size() - 1;
   1472       got_offset = (plt_index - offset + reserved) * 8;
   1473     }
   1474 
   1475   gsym->set_plt_offset(plt_offset);
   1476 
   1477   // Every PLT entry needs a reloc.
   1478   this->add_relocation(symtab, layout, gsym, got_offset);
   1479 
   1480   // Note that we don't need to save the symbol.  The contents of the
   1481   // PLT are independent of which symbols are used.  The symbols only
   1482   // appear in the relocations.
   1483 }
   1484 
   1485 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.  Return
   1486 // the PLT offset.
   1487 
   1488 template<int size>
   1489 unsigned int
   1490 Output_data_plt_x86_64<size>::add_local_ifunc_entry(
   1491     Symbol_table* symtab,
   1492     Layout* layout,
   1493     Sized_relobj_file<size, false>* relobj,
   1494     unsigned int local_sym_index)
   1495 {
   1496   unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size();
   1497   ++this->irelative_count_;
   1498 
   1499   section_offset_type got_offset = this->got_irelative_->current_data_size();
   1500 
   1501   // Every PLT entry needs a GOT entry which points back to the PLT
   1502   // entry.
   1503   this->got_irelative_->set_current_data_size(got_offset + 8);
   1504 
   1505   // Every PLT entry needs a reloc.
   1506   Reloc_section* rela = this->rela_irelative(symtab, layout);
   1507   rela->add_symbolless_local_addend(relobj, local_sym_index,
   1508 				    elfcpp::R_X86_64_IRELATIVE,
   1509 				    this->got_irelative_, got_offset, 0);
   1510 
   1511   return plt_offset;
   1512 }
   1513 
   1514 // Add the relocation for a PLT entry.
   1515 
   1516 template<int size>
   1517 void
   1518 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab,
   1519 					     Layout* layout,
   1520 					     Symbol* gsym,
   1521 					     unsigned int got_offset)
   1522 {
   1523   if (gsym->type() == elfcpp::STT_GNU_IFUNC
   1524       && gsym->can_use_relative_reloc(false))
   1525     {
   1526       Reloc_section* rela = this->rela_irelative(symtab, layout);
   1527       rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
   1528 					 this->got_irelative_, got_offset, 0);
   1529     }
   1530   else
   1531     {
   1532       gsym->set_needs_dynsym_entry();
   1533       this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
   1534 			     got_offset, 0);
   1535     }
   1536 }
   1537 
   1538 // Return where the TLSDESC relocations should go, creating it if
   1539 // necessary.  These follow the JUMP_SLOT relocations.
   1540 
   1541 template<int size>
   1542 typename Output_data_plt_x86_64<size>::Reloc_section*
   1543 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout)
   1544 {
   1545   if (this->tlsdesc_rel_ == NULL)
   1546     {
   1547       this->tlsdesc_rel_ = new Reloc_section(false);
   1548       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
   1549 				      elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
   1550 				      ORDER_DYNAMIC_PLT_RELOCS, false);
   1551       gold_assert(this->tlsdesc_rel_->output_section()
   1552 		  == this->rel_->output_section());
   1553     }
   1554   return this->tlsdesc_rel_;
   1555 }
   1556 
   1557 // Return where the IRELATIVE relocations should go in the PLT.  These
   1558 // follow the JUMP_SLOT and the TLSDESC relocations.
   1559 
   1560 template<int size>
   1561 typename Output_data_plt_x86_64<size>::Reloc_section*
   1562 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab,
   1563 					     Layout* layout)
   1564 {
   1565   if (this->irelative_rel_ == NULL)
   1566     {
   1567       // Make sure we have a place for the TLSDESC relocations, in
   1568       // case we see any later on.
   1569       this->rela_tlsdesc(layout);
   1570       this->irelative_rel_ = new Reloc_section(false);
   1571       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
   1572 				      elfcpp::SHF_ALLOC, this->irelative_rel_,
   1573 				      ORDER_DYNAMIC_PLT_RELOCS, false);
   1574       gold_assert(this->irelative_rel_->output_section()
   1575 		  == this->rel_->output_section());
   1576 
   1577       if (parameters->doing_static_link())
   1578 	{
   1579 	  // A statically linked executable will only have a .rela.plt
   1580 	  // section to hold R_X86_64_IRELATIVE relocs for
   1581 	  // STT_GNU_IFUNC symbols.  The library will use these
   1582 	  // symbols to locate the IRELATIVE relocs at program startup
   1583 	  // time.
   1584 	  symtab->define_in_output_data("__rela_iplt_start", NULL,
   1585 					Symbol_table::PREDEFINED,
   1586 					this->irelative_rel_, 0, 0,
   1587 					elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
   1588 					elfcpp::STV_HIDDEN, 0, false, true);
   1589 	  symtab->define_in_output_data("__rela_iplt_end", NULL,
   1590 					Symbol_table::PREDEFINED,
   1591 					this->irelative_rel_, 0, 0,
   1592 					elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
   1593 					elfcpp::STV_HIDDEN, 0, true, true);
   1594 	}
   1595     }
   1596   return this->irelative_rel_;
   1597 }
   1598 
   1599 // Return the PLT address to use for a global symbol.
   1600 
   1601 template<int size>
   1602 uint64_t
   1603 Output_data_plt_x86_64<size>::address_for_global(const Symbol* gsym)
   1604 {
   1605   uint64_t offset = 0;
   1606   if (gsym->type() == elfcpp::STT_GNU_IFUNC
   1607       && gsym->can_use_relative_reloc(false))
   1608     offset = (this->count_ + 1) * this->get_plt_entry_size();
   1609   return this->address() + offset + gsym->plt_offset();
   1610 }
   1611 
   1612 // Return the PLT address to use for a local symbol.  These are always
   1613 // IRELATIVE relocs.
   1614 
   1615 template<int size>
   1616 uint64_t
   1617 Output_data_plt_x86_64<size>::address_for_local(const Relobj* object,
   1618 						unsigned int r_sym)
   1619 {
   1620   return (this->address()
   1621 	  + (this->count_ + 1) * this->get_plt_entry_size()
   1622 	  + object->local_plt_offset(r_sym));
   1623 }
   1624 
   1625 // Set the final size.
   1626 template<int size>
   1627 void
   1628 Output_data_plt_x86_64<size>::set_final_data_size()
   1629 {
   1630   unsigned int count = this->count_ + this->irelative_count_;
   1631   if (this->has_tlsdesc_entry())
   1632     ++count;
   1633   this->set_data_size((count + 1) * this->get_plt_entry_size());
   1634 }
   1635 
   1636 // The first entry in the PLT for an executable.
   1637 
   1638 template<int size>
   1639 const unsigned char
   1640 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] =
   1641 {
   1642   // From AMD64 ABI Draft 0.98, page 76
   1643   0xff, 0x35,	// pushq contents of memory address
   1644   0, 0, 0, 0,	// replaced with address of .got + 8
   1645   0xff, 0x25,	// jmp indirect
   1646   0, 0, 0, 0,	// replaced with address of .got + 16
   1647   0x90, 0x90, 0x90, 0x90   // noop (x4)
   1648 };
   1649 
   1650 template<int size>
   1651 void
   1652 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry(
   1653     unsigned char* pov,
   1654     typename elfcpp::Elf_types<size>::Elf_Addr got_address,
   1655     typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
   1656 {
   1657   memcpy(pov, first_plt_entry, plt_entry_size);
   1658   // We do a jmp relative to the PC at the end of this instruction.
   1659   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
   1660 					      (got_address + 8
   1661 					       - (plt_address + 6)));
   1662   elfcpp::Swap<32, false>::writeval(pov + 8,
   1663 				    (got_address + 16
   1664 				     - (plt_address + 12)));
   1665 }
   1666 
   1667 // Subsequent entries in the PLT for an executable.
   1668 
   1669 template<int size>
   1670 const unsigned char
   1671 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] =
   1672 {
   1673   // From AMD64 ABI Draft 0.98, page 76
   1674   0xff, 0x25,	// jmpq indirect
   1675   0, 0, 0, 0,	// replaced with address of symbol in .got
   1676   0x68,		// pushq immediate
   1677   0, 0, 0, 0,	// replaced with offset into relocation table
   1678   0xe9,		// jmpq relative
   1679   0, 0, 0, 0	// replaced with offset to start of .plt
   1680 };
   1681 
   1682 template<int size>
   1683 unsigned int
   1684 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry(
   1685     unsigned char* pov,
   1686     typename elfcpp::Elf_types<size>::Elf_Addr got_address,
   1687     typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
   1688     unsigned int got_offset,
   1689     unsigned int plt_offset,
   1690     unsigned int plt_index)
   1691 {
   1692   // Check PC-relative offset overflow in PLT entry.
   1693   uint64_t plt_got_pcrel_offset = (got_address + got_offset
   1694 				   - (plt_address + plt_offset + 6));
   1695   if (Bits<32>::has_overflow(plt_got_pcrel_offset))
   1696     gold_error(_("PC-relative offset overflow in PLT entry %d"),
   1697 	       plt_index + 1);
   1698 
   1699   memcpy(pov, plt_entry, plt_entry_size);
   1700   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
   1701 					      plt_got_pcrel_offset);
   1702 
   1703   elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
   1704   elfcpp::Swap<32, false>::writeval(pov + 12,
   1705 				    - (plt_offset + plt_entry_size));
   1706 
   1707   return 6;
   1708 }
   1709 
   1710 // The reserved TLSDESC entry in the PLT for an executable.
   1711 
   1712 template<int size>
   1713 const unsigned char
   1714 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] =
   1715 {
   1716   // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
   1717   // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
   1718   0xff, 0x35,	// pushq x(%rip)
   1719   0, 0, 0, 0,	// replaced with address of linkmap GOT entry (at PLTGOT + 8)
   1720   0xff,	0x25,	// jmpq *y(%rip)
   1721   0, 0, 0, 0,	// replaced with offset of reserved TLSDESC_GOT entry
   1722   0x0f,	0x1f,	// nop
   1723   0x40, 0
   1724 };
   1725 
   1726 template<int size>
   1727 void
   1728 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry(
   1729     unsigned char* pov,
   1730     typename elfcpp::Elf_types<size>::Elf_Addr got_address,
   1731     typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
   1732     typename elfcpp::Elf_types<size>::Elf_Addr got_base,
   1733     unsigned int tlsdesc_got_offset,
   1734     unsigned int plt_offset)
   1735 {
   1736   memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
   1737   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
   1738 					      (got_address + 8
   1739 					       - (plt_address + plt_offset
   1740 						  + 6)));
   1741   elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
   1742 					      (got_base
   1743 					       + tlsdesc_got_offset
   1744 					       - (plt_address + plt_offset
   1745 						  + 12)));
   1746 }
   1747 
   1748 // The .eh_frame unwind information for the PLT.
   1749 
   1750 template<int size>
   1751 const unsigned char
   1752 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] =
   1753 {
   1754   1,				// CIE version.
   1755   'z',				// Augmentation: augmentation size included.
   1756   'R',				// Augmentation: FDE encoding included.
   1757   '\0',				// End of augmentation string.
   1758   1,				// Code alignment factor.
   1759   0x78,				// Data alignment factor.
   1760   16,				// Return address column.
   1761   1,				// Augmentation size.
   1762   (elfcpp::DW_EH_PE_pcrel	// FDE encoding.
   1763    | elfcpp::DW_EH_PE_sdata4),
   1764   elfcpp::DW_CFA_def_cfa, 7, 8,	// DW_CFA_def_cfa: r7 (rsp) ofs 8.
   1765   elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8.
   1766   elfcpp::DW_CFA_nop,		// Align to 16 bytes.
   1767   elfcpp::DW_CFA_nop
   1768 };
   1769 
   1770 template<int size>
   1771 const unsigned char
   1772 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
   1773 {
   1774   0, 0, 0, 0,				// Replaced with offset to .plt.
   1775   0, 0, 0, 0,				// Replaced with size of .plt.
   1776   0,					// Augmentation size.
   1777   elfcpp::DW_CFA_def_cfa_offset, 16,	// DW_CFA_def_cfa_offset: 16.
   1778   elfcpp::DW_CFA_advance_loc + 6,	// Advance 6 to __PLT__ + 6.
   1779   elfcpp::DW_CFA_def_cfa_offset, 24,	// DW_CFA_def_cfa_offset: 24.
   1780   elfcpp::DW_CFA_advance_loc + 10,	// Advance 10 to __PLT__ + 16.
   1781   elfcpp::DW_CFA_def_cfa_expression,	// DW_CFA_def_cfa_expression.
   1782   11,					// Block length.
   1783   elfcpp::DW_OP_breg7, 8,		// Push %rsp + 8.
   1784   elfcpp::DW_OP_breg16, 0,		// Push %rip.
   1785   elfcpp::DW_OP_lit15,			// Push 0xf.
   1786   elfcpp::DW_OP_and,			// & (%rip & 0xf).
   1787   elfcpp::DW_OP_lit11,			// Push 0xb.
   1788   elfcpp::DW_OP_ge,			// >= ((%rip & 0xf) >= 0xb)
   1789   elfcpp::DW_OP_lit3,			// Push 3.
   1790   elfcpp::DW_OP_shl,			// << (((%rip & 0xf) >= 0xb) << 3)
   1791   elfcpp::DW_OP_plus,			// + ((((%rip&0xf)>=0xb)<<3)+%rsp+8
   1792   elfcpp::DW_CFA_nop,			// Align to 32 bytes.
   1793   elfcpp::DW_CFA_nop,
   1794   elfcpp::DW_CFA_nop,
   1795   elfcpp::DW_CFA_nop
   1796 };
   1797 
   1798 // Write out the PLT.  This uses the hand-coded instructions above,
   1799 // and adjusts them as needed.  This is specified by the AMD64 ABI.
   1800 
   1801 template<int size>
   1802 void
   1803 Output_data_plt_x86_64<size>::do_write(Output_file* of)
   1804 {
   1805   const off_t offset = this->offset();
   1806   const section_size_type oview_size =
   1807     convert_to_section_size_type(this->data_size());
   1808   unsigned char* const oview = of->get_output_view(offset, oview_size);
   1809 
   1810   const off_t got_file_offset = this->got_plt_->offset();
   1811   gold_assert(parameters->incremental_update()
   1812 	      || (got_file_offset + this->got_plt_->data_size()
   1813 		  == this->got_irelative_->offset()));
   1814   const section_size_type got_size =
   1815     convert_to_section_size_type(this->got_plt_->data_size()
   1816 				 + this->got_irelative_->data_size());
   1817   unsigned char* const got_view = of->get_output_view(got_file_offset,
   1818 						      got_size);
   1819 
   1820   unsigned char* pov = oview;
   1821 
   1822   // The base address of the .plt section.
   1823   typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address();
   1824   // The base address of the .got section.
   1825   typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address();
   1826   // The base address of the PLT portion of the .got section,
   1827   // which is where the GOT pointer will point, and where the
   1828   // three reserved GOT entries are located.
   1829   typename elfcpp::Elf_types<size>::Elf_Addr got_address
   1830     = this->got_plt_->address();
   1831 
   1832   this->fill_first_plt_entry(pov, got_address, plt_address);
   1833   pov += this->get_plt_entry_size();
   1834 
   1835   // The first three entries in the GOT are reserved, and are written
   1836   // by Output_data_got_plt_x86_64::do_write.
   1837   unsigned char* got_pov = got_view + 24;
   1838 
   1839   unsigned int plt_offset = this->get_plt_entry_size();
   1840   unsigned int got_offset = 24;
   1841   const unsigned int count = this->count_ + this->irelative_count_;
   1842   for (unsigned int plt_index = 0;
   1843        plt_index < count;
   1844        ++plt_index,
   1845 	 pov += this->get_plt_entry_size(),
   1846 	 got_pov += 8,
   1847 	 plt_offset += this->get_plt_entry_size(),
   1848 	 got_offset += 8)
   1849     {
   1850       // Set and adjust the PLT entry itself.
   1851       unsigned int lazy_offset = this->fill_plt_entry(pov,
   1852 						      got_address, plt_address,
   1853 						      got_offset, plt_offset,
   1854 						      plt_index);
   1855 
   1856       // Set the entry in the GOT.
   1857       elfcpp::Swap<64, false>::writeval(got_pov,
   1858 					plt_address + plt_offset + lazy_offset);
   1859     }
   1860 
   1861   if (this->has_tlsdesc_entry())
   1862     {
   1863       // Set and adjust the reserved TLSDESC PLT entry.
   1864       unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
   1865       this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base,
   1866 			       tlsdesc_got_offset, plt_offset);
   1867       pov += this->get_plt_entry_size();
   1868     }
   1869 
   1870   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
   1871   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
   1872 
   1873   of->write_output_view(offset, oview_size, oview);
   1874   of->write_output_view(got_file_offset, got_size, got_view);
   1875 }
   1876 
   1877 // Create the PLT section.
   1878 
   1879 template<int size>
   1880 void
   1881 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout)
   1882 {
   1883   if (this->plt_ == NULL)
   1884     {
   1885       // Create the GOT sections first.
   1886       this->got_section(symtab, layout);
   1887 
   1888       this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_,
   1889 				       this->got_irelative_);
   1890 
   1891       // Add unwind information if requested.
   1892       if (parameters->options().ld_generated_unwind_info())
   1893 	this->plt_->add_eh_frame(layout);
   1894 
   1895       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
   1896 				      (elfcpp::SHF_ALLOC
   1897 				       | elfcpp::SHF_EXECINSTR),
   1898 				      this->plt_, ORDER_PLT, false);
   1899 
   1900       // Make the sh_info field of .rela.plt point to .plt.
   1901       Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
   1902       rela_plt_os->set_info_section(this->plt_->output_section());
   1903     }
   1904 }
   1905 
   1906 // Return the section for TLSDESC relocations.
   1907 
   1908 template<int size>
   1909 typename Target_x86_64<size>::Reloc_section*
   1910 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const
   1911 {
   1912   return this->plt_section()->rela_tlsdesc(layout);
   1913 }
   1914 
   1915 // Create a PLT entry for a global symbol.
   1916 
   1917 template<int size>
   1918 void
   1919 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout,
   1920 				    Symbol* gsym)
   1921 {
   1922   if (gsym->has_plt_offset())
   1923     return;
   1924 
   1925   if (this->plt_ == NULL)
   1926     this->make_plt_section(symtab, layout);
   1927 
   1928   this->plt_->add_entry(symtab, layout, gsym);
   1929 }
   1930 
   1931 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
   1932 
   1933 template<int size>
   1934 void
   1935 Target_x86_64<size>::make_local_ifunc_plt_entry(
   1936     Symbol_table* symtab, Layout* layout,
   1937     Sized_relobj_file<size, false>* relobj,
   1938     unsigned int local_sym_index)
   1939 {
   1940   if (relobj->local_has_plt_offset(local_sym_index))
   1941     return;
   1942   if (this->plt_ == NULL)
   1943     this->make_plt_section(symtab, layout);
   1944   unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
   1945 							      relobj,
   1946 							      local_sym_index);
   1947   relobj->set_local_plt_offset(local_sym_index, plt_offset);
   1948 }
   1949 
   1950 // Return the number of entries in the PLT.
   1951 
   1952 template<int size>
   1953 unsigned int
   1954 Target_x86_64<size>::plt_entry_count() const
   1955 {
   1956   if (this->plt_ == NULL)
   1957     return 0;
   1958   return this->plt_->entry_count();
   1959 }
   1960 
   1961 // Return the offset of the first non-reserved PLT entry.
   1962 
   1963 template<int size>
   1964 unsigned int
   1965 Target_x86_64<size>::first_plt_entry_offset() const
   1966 {
   1967   if (this->plt_ == NULL)
   1968     return 0;
   1969   return this->plt_->first_plt_entry_offset();
   1970 }
   1971 
   1972 // Return the size of each PLT entry.
   1973 
   1974 template<int size>
   1975 unsigned int
   1976 Target_x86_64<size>::plt_entry_size() const
   1977 {
   1978   if (this->plt_ == NULL)
   1979     return 0;
   1980   return this->plt_->get_plt_entry_size();
   1981 }
   1982 
   1983 // Create the GOT and PLT sections for an incremental update.
   1984 
   1985 template<int size>
   1986 Output_data_got_base*
   1987 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab,
   1988 				       Layout* layout,
   1989 				       unsigned int got_count,
   1990 				       unsigned int plt_count)
   1991 {
   1992   gold_assert(this->got_ == NULL);
   1993 
   1994   this->got_ = new Output_data_got<64, false>(got_count * 8);
   1995   layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
   1996 				  (elfcpp::SHF_ALLOC
   1997 				   | elfcpp::SHF_WRITE),
   1998 				  this->got_, ORDER_RELRO_LAST,
   1999 				  true);
   2000 
   2001   // Add the three reserved entries.
   2002   this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8);
   2003   layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
   2004 				  (elfcpp::SHF_ALLOC
   2005 				   | elfcpp::SHF_WRITE),
   2006 				  this->got_plt_, ORDER_NON_RELRO_FIRST,
   2007 				  false);
   2008 
   2009   // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
   2010   this->global_offset_table_ =
   2011     symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
   2012 				  Symbol_table::PREDEFINED,
   2013 				  this->got_plt_,
   2014 				  0, 0, elfcpp::STT_OBJECT,
   2015 				  elfcpp::STB_LOCAL,
   2016 				  elfcpp::STV_HIDDEN, 0,
   2017 				  false, false);
   2018 
   2019   // If there are any TLSDESC relocations, they get GOT entries in
   2020   // .got.plt after the jump slot entries.
   2021   // FIXME: Get the count for TLSDESC entries.
   2022   this->got_tlsdesc_ = new Output_data_got<64, false>(0);
   2023   layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
   2024 				  elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
   2025 				  this->got_tlsdesc_,
   2026 				  ORDER_NON_RELRO_FIRST, false);
   2027 
   2028   // If there are any IRELATIVE relocations, they get GOT entries in
   2029   // .got.plt after the jump slot and TLSDESC entries.
   2030   this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT");
   2031   layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
   2032 				  elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
   2033 				  this->got_irelative_,
   2034 				  ORDER_NON_RELRO_FIRST, false);
   2035 
   2036   // Create the PLT section.
   2037   this->plt_ = this->make_data_plt(layout, this->got_,
   2038 				   this->got_plt_,
   2039 				   this->got_irelative_,
   2040 				   plt_count);
   2041 
   2042   // Add unwind information if requested.
   2043   if (parameters->options().ld_generated_unwind_info())
   2044     this->plt_->add_eh_frame(layout);
   2045 
   2046   layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
   2047 				  elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR,
   2048 				  this->plt_, ORDER_PLT, false);
   2049 
   2050   // Make the sh_info field of .rela.plt point to .plt.
   2051   Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
   2052   rela_plt_os->set_info_section(this->plt_->output_section());
   2053 
   2054   // Create the rela_dyn section.
   2055   this->rela_dyn_section(layout);
   2056 
   2057   return this->got_;
   2058 }
   2059 
   2060 // Reserve a GOT entry for a local symbol, and regenerate any
   2061 // necessary dynamic relocations.
   2062 
   2063 template<int size>
   2064 void
   2065 Target_x86_64<size>::reserve_local_got_entry(
   2066     unsigned int got_index,
   2067     Sized_relobj<size, false>* obj,
   2068     unsigned int r_sym,
   2069     unsigned int got_type)
   2070 {
   2071   unsigned int got_offset = got_index * 8;
   2072   Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
   2073 
   2074   this->got_->reserve_local(got_index, obj, r_sym, got_type);
   2075   switch (got_type)
   2076     {
   2077     case GOT_TYPE_STANDARD:
   2078       if (parameters->options().output_is_position_independent())
   2079 	rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE,
   2080 				     this->got_, got_offset, 0, false);
   2081       break;
   2082     case GOT_TYPE_TLS_OFFSET:
   2083       rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64,
   2084 			  this->got_, got_offset, 0);
   2085       break;
   2086     case GOT_TYPE_TLS_PAIR:
   2087       this->got_->reserve_slot(got_index + 1);
   2088       rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64,
   2089 			  this->got_, got_offset, 0);
   2090       break;
   2091     case GOT_TYPE_TLS_DESC:
   2092       gold_fatal(_("TLS_DESC not yet supported for incremental linking"));
   2093       // this->got_->reserve_slot(got_index + 1);
   2094       // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
   2095       //			       this->got_, got_offset, 0);
   2096       break;
   2097     default:
   2098       gold_unreachable();
   2099     }
   2100 }
   2101 
   2102 // Reserve a GOT entry for a global symbol, and regenerate any
   2103 // necessary dynamic relocations.
   2104 
   2105 template<int size>
   2106 void
   2107 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index,
   2108 					      Symbol* gsym,
   2109 					      unsigned int got_type)
   2110 {
   2111   unsigned int got_offset = got_index * 8;
   2112   Reloc_section* rela_dyn = this->rela_dyn_section(NULL);
   2113 
   2114   this->got_->reserve_global(got_index, gsym, got_type);
   2115   switch (got_type)
   2116     {
   2117     case GOT_TYPE_STANDARD:
   2118       if (!gsym->final_value_is_known())
   2119 	{
   2120 	  if (gsym->is_from_dynobj()
   2121 	      || gsym->is_undefined()
   2122 	      || gsym->is_preemptible()
   2123 	      || gsym->type() == elfcpp::STT_GNU_IFUNC)
   2124 	    rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT,
   2125 				 this->got_, got_offset, 0);
   2126 	  else
   2127 	    rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
   2128 					  this->got_, got_offset, 0, false);
   2129 	}
   2130       break;
   2131     case GOT_TYPE_TLS_OFFSET:
   2132       rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64,
   2133 				    this->got_, got_offset, 0, false);
   2134       break;
   2135     case GOT_TYPE_TLS_PAIR:
   2136       this->got_->reserve_slot(got_index + 1);
   2137       rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64,
   2138 				    this->got_, got_offset, 0, false);
   2139       rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64,
   2140 				    this->got_, got_offset + 8, 0, false);
   2141       break;
   2142     case GOT_TYPE_TLS_DESC:
   2143       this->got_->reserve_slot(got_index + 1);
   2144       rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC,
   2145 				    this->got_, got_offset, 0, false);
   2146       break;
   2147     default:
   2148       gold_unreachable();
   2149     }
   2150 }
   2151 
   2152 // Register an existing PLT entry for a global symbol.
   2153 
   2154 template<int size>
   2155 void
   2156 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab,
   2157 					       Layout* layout,
   2158 					       unsigned int plt_index,
   2159 					       Symbol* gsym)
   2160 {
   2161   gold_assert(this->plt_ != NULL);
   2162   gold_assert(!gsym->has_plt_offset());
   2163 
   2164   this->plt_->reserve_slot(plt_index);
   2165 
   2166   gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size());
   2167 
   2168   unsigned int got_offset = (plt_index + 3) * 8;
   2169   this->plt_->add_relocation(symtab, layout, gsym, got_offset);
   2170 }
   2171 
   2172 // Force a COPY relocation for a given symbol.
   2173 
   2174 template<int size>
   2175 void
   2176 Target_x86_64<size>::emit_copy_reloc(
   2177     Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset)
   2178 {
   2179   this->copy_relocs_.emit_copy_reloc(symtab,
   2180 				     symtab->get_sized_symbol<size>(sym),
   2181 				     os,
   2182 				     offset,
   2183 				     this->rela_dyn_section(NULL));
   2184 }
   2185 
   2186 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
   2187 
   2188 template<int size>
   2189 void
   2190 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab,
   2191 					    Layout* layout)
   2192 {
   2193   if (this->tls_base_symbol_defined_)
   2194     return;
   2195 
   2196   Output_segment* tls_segment = layout->tls_segment();
   2197   if (tls_segment != NULL)
   2198     {
   2199       bool is_exec = parameters->options().output_is_executable();
   2200       symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
   2201 				       Symbol_table::PREDEFINED,
   2202 				       tls_segment, 0, 0,
   2203 				       elfcpp::STT_TLS,
   2204 				       elfcpp::STB_LOCAL,
   2205 				       elfcpp::STV_HIDDEN, 0,
   2206 				       (is_exec
   2207 					? Symbol::SEGMENT_END
   2208 					: Symbol::SEGMENT_START),
   2209 				       true);
   2210     }
   2211   this->tls_base_symbol_defined_ = true;
   2212 }
   2213 
   2214 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
   2215 
   2216 template<int size>
   2217 void
   2218 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab,
   2219 					     Layout* layout)
   2220 {
   2221   if (this->plt_ == NULL)
   2222     this->make_plt_section(symtab, layout);
   2223 
   2224   if (!this->plt_->has_tlsdesc_entry())
   2225     {
   2226       // Allocate the TLSDESC_GOT entry.
   2227       Output_data_got<64, false>* got = this->got_section(symtab, layout);
   2228       unsigned int got_offset = got->add_constant(0);
   2229 
   2230       // Allocate the TLSDESC_PLT entry.
   2231       this->plt_->reserve_tlsdesc_entry(got_offset);
   2232     }
   2233 }
   2234 
   2235 // Create a GOT entry for the TLS module index.
   2236 
   2237 template<int size>
   2238 unsigned int
   2239 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
   2240 					 Sized_relobj_file<size, false>* object)
   2241 {
   2242   if (this->got_mod_index_offset_ == -1U)
   2243     {
   2244       gold_assert(symtab != NULL && layout != NULL && object != NULL);
   2245       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
   2246       Output_data_got<64, false>* got = this->got_section(symtab, layout);
   2247       unsigned int got_offset = got->add_constant(0);
   2248       rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
   2249 			  got_offset, 0);
   2250       got->add_constant(0);
   2251       this->got_mod_index_offset_ = got_offset;
   2252     }
   2253   return this->got_mod_index_offset_;
   2254 }
   2255 
   2256 // Optimize the TLS relocation type based on what we know about the
   2257 // symbol.  IS_FINAL is true if the final address of this symbol is
   2258 // known at link time.
   2259 
   2260 template<int size>
   2261 tls::Tls_optimization
   2262 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type)
   2263 {
   2264   // If we are generating a shared library, then we can't do anything
   2265   // in the linker.
   2266   if (parameters->options().shared())
   2267     return tls::TLSOPT_NONE;
   2268 
   2269   switch (r_type)
   2270     {
   2271     case elfcpp::R_X86_64_TLSGD:
   2272     case elfcpp::R_X86_64_GOTPC32_TLSDESC:
   2273     case elfcpp::R_X86_64_TLSDESC_CALL:
   2274       // These are General-Dynamic which permits fully general TLS
   2275       // access.  Since we know that we are generating an executable,
   2276       // we can convert this to Initial-Exec.  If we also know that
   2277       // this is a local symbol, we can further switch to Local-Exec.
   2278       if (is_final)
   2279 	return tls::TLSOPT_TO_LE;
   2280       return tls::TLSOPT_TO_IE;
   2281 
   2282     case elfcpp::R_X86_64_TLSLD:
   2283       // This is Local-Dynamic, which refers to a local symbol in the
   2284       // dynamic TLS block.  Since we know that we generating an
   2285       // executable, we can switch to Local-Exec.
   2286       return tls::TLSOPT_TO_LE;
   2287 
   2288     case elfcpp::R_X86_64_DTPOFF32:
   2289     case elfcpp::R_X86_64_DTPOFF64:
   2290       // Another Local-Dynamic reloc.
   2291       return tls::TLSOPT_TO_LE;
   2292 
   2293     case elfcpp::R_X86_64_GOTTPOFF:
   2294       // These are Initial-Exec relocs which get the thread offset
   2295       // from the GOT.  If we know that we are linking against the
   2296       // local symbol, we can switch to Local-Exec, which links the
   2297       // thread offset into the instruction.
   2298       if (is_final)
   2299 	return tls::TLSOPT_TO_LE;
   2300       return tls::TLSOPT_NONE;
   2301 
   2302     case elfcpp::R_X86_64_TPOFF32:
   2303       // When we already have Local-Exec, there is nothing further we
   2304       // can do.
   2305       return tls::TLSOPT_NONE;
   2306 
   2307     default:
   2308       gold_unreachable();
   2309     }
   2310 }
   2311 
   2312 // Get the Reference_flags for a particular relocation.
   2313 
   2314 template<int size>
   2315 int
   2316 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type)
   2317 {
   2318   switch (r_type)
   2319     {
   2320     case elfcpp::R_X86_64_NONE:
   2321     case elfcpp::R_X86_64_GNU_VTINHERIT:
   2322     case elfcpp::R_X86_64_GNU_VTENTRY:
   2323     case elfcpp::R_X86_64_GOTPC32:
   2324     case elfcpp::R_X86_64_GOTPC64:
   2325       // No symbol reference.
   2326       return 0;
   2327 
   2328     case elfcpp::R_X86_64_64:
   2329     case elfcpp::R_X86_64_32:
   2330     case elfcpp::R_X86_64_32S:
   2331     case elfcpp::R_X86_64_16:
   2332     case elfcpp::R_X86_64_8:
   2333       return Symbol::ABSOLUTE_REF;
   2334 
   2335     case elfcpp::R_X86_64_PC64:
   2336     case elfcpp::R_X86_64_PC32:
   2337     case elfcpp::R_X86_64_PC32_BND:
   2338     case elfcpp::R_X86_64_PC16:
   2339     case elfcpp::R_X86_64_PC8:
   2340     case elfcpp::R_X86_64_GOTOFF64:
   2341       return Symbol::RELATIVE_REF;
   2342 
   2343     case elfcpp::R_X86_64_PLT32:
   2344     case elfcpp::R_X86_64_PLT32_BND:
   2345     case elfcpp::R_X86_64_PLTOFF64:
   2346       return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
   2347 
   2348     case elfcpp::R_X86_64_GOT64:
   2349     case elfcpp::R_X86_64_GOT32:
   2350     case elfcpp::R_X86_64_GOTPCREL64:
   2351     case elfcpp::R_X86_64_GOTPCREL:
   2352     case elfcpp::R_X86_64_GOTPCRELX:
   2353     case elfcpp::R_X86_64_REX_GOTPCRELX:
   2354     case elfcpp::R_X86_64_GOTPLT64:
   2355       // Absolute in GOT.
   2356       return Symbol::ABSOLUTE_REF;
   2357 
   2358     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
   2359     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
   2360     case elfcpp::R_X86_64_TLSDESC_CALL:
   2361     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
   2362     case elfcpp::R_X86_64_DTPOFF32:
   2363     case elfcpp::R_X86_64_DTPOFF64:
   2364     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
   2365     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
   2366       return Symbol::TLS_REF;
   2367 
   2368     case elfcpp::R_X86_64_COPY:
   2369     case elfcpp::R_X86_64_GLOB_DAT:
   2370     case elfcpp::R_X86_64_JUMP_SLOT:
   2371     case elfcpp::R_X86_64_RELATIVE:
   2372     case elfcpp::R_X86_64_IRELATIVE:
   2373     case elfcpp::R_X86_64_TPOFF64:
   2374     case elfcpp::R_X86_64_DTPMOD64:
   2375     case elfcpp::R_X86_64_TLSDESC:
   2376     case elfcpp::R_X86_64_SIZE32:
   2377     case elfcpp::R_X86_64_SIZE64:
   2378     default:
   2379       // Not expected.  We will give an error later.
   2380       return 0;
   2381     }
   2382 }
   2383 
   2384 // Report an unsupported relocation against a local symbol.
   2385 
   2386 template<int size>
   2387 void
   2388 Target_x86_64<size>::Scan::unsupported_reloc_local(
   2389      Sized_relobj_file<size, false>* object,
   2390      unsigned int r_type)
   2391 {
   2392   gold_error(_("%s: unsupported reloc %u against local symbol"),
   2393 	     object->name().c_str(), r_type);
   2394 }
   2395 
   2396 // We are about to emit a dynamic relocation of type R_TYPE.  If the
   2397 // dynamic linker does not support it, issue an error.  The GNU linker
   2398 // only issues a non-PIC error for an allocated read-only section.
   2399 // Here we know the section is allocated, but we don't know that it is
   2400 // read-only.  But we check for all the relocation types which the
   2401 // glibc dynamic linker supports, so it seems appropriate to issue an
   2402 // error even if the section is not read-only.  If GSYM is not NULL,
   2403 // it is the symbol the relocation is against; if it is NULL, the
   2404 // relocation is against a local symbol.
   2405 
   2406 template<int size>
   2407 void
   2408 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type,
   2409 					 Symbol* gsym)
   2410 {
   2411   switch (r_type)
   2412     {
   2413       // These are the relocation types supported by glibc for x86_64
   2414       // which should always work.
   2415     case elfcpp::R_X86_64_RELATIVE:
   2416     case elfcpp::R_X86_64_IRELATIVE:
   2417     case elfcpp::R_X86_64_GLOB_DAT:
   2418     case elfcpp::R_X86_64_JUMP_SLOT:
   2419     case elfcpp::R_X86_64_DTPMOD64:
   2420     case elfcpp::R_X86_64_DTPOFF64:
   2421     case elfcpp::R_X86_64_TPOFF64:
   2422     case elfcpp::R_X86_64_64:
   2423     case elfcpp::R_X86_64_COPY:
   2424       return;
   2425 
   2426       // glibc supports these reloc types, but they can overflow.
   2427     case elfcpp::R_X86_64_PC32:
   2428     case elfcpp::R_X86_64_PC32_BND:
   2429       // A PC relative reference is OK against a local symbol or if
   2430       // the symbol is defined locally.
   2431       if (gsym == NULL
   2432 	  || (!gsym->is_from_dynobj()
   2433 	      && !gsym->is_undefined()
   2434 	      && !gsym->is_preemptible()))
   2435 	return;
   2436       /* Fall through.  */
   2437     case elfcpp::R_X86_64_32:
   2438       // R_X86_64_32 is OK for x32.
   2439       if (size == 32 && r_type == elfcpp::R_X86_64_32)
   2440 	return;
   2441       if (this->issued_non_pic_error_)
   2442 	return;
   2443       gold_assert(parameters->options().output_is_position_independent());
   2444       if (gsym == NULL)
   2445 	object->error(_("requires dynamic R_X86_64_32 reloc which may "
   2446 			"overflow at runtime; recompile with -fPIC"));
   2447       else
   2448 	{
   2449 	  const char *r_name;
   2450 	  switch (r_type)
   2451 	    {
   2452 	    case elfcpp::R_X86_64_32:
   2453 	      r_name = "R_X86_64_32";
   2454 	      break;
   2455 	    case elfcpp::R_X86_64_PC32:
   2456 	      r_name = "R_X86_64_PC32";
   2457 	      break;
   2458 	    case elfcpp::R_X86_64_PC32_BND:
   2459 	      r_name = "R_X86_64_PC32_BND";
   2460 	      break;
   2461 	    default:
   2462 	      gold_unreachable();
   2463 	      break;
   2464 	    }
   2465 	  object->error(_("requires dynamic %s reloc against '%s' "
   2466 			  "which may overflow at runtime; recompile "
   2467 			  "with -fPIC"),
   2468 			r_name, gsym->name());
   2469 	}
   2470       this->issued_non_pic_error_ = true;
   2471       return;
   2472 
   2473     default:
   2474       // This prevents us from issuing more than one error per reloc
   2475       // section.  But we can still wind up issuing more than one
   2476       // error per object file.
   2477       if (this->issued_non_pic_error_)
   2478 	return;
   2479       gold_assert(parameters->options().output_is_position_independent());
   2480       object->error(_("requires unsupported dynamic reloc %u; "
   2481 		      "recompile with -fPIC"),
   2482 		    r_type);
   2483       this->issued_non_pic_error_ = true;
   2484       return;
   2485 
   2486     case elfcpp::R_X86_64_NONE:
   2487       gold_unreachable();
   2488     }
   2489 }
   2490 
   2491 // Return whether we need to make a PLT entry for a relocation of the
   2492 // given type against a STT_GNU_IFUNC symbol.
   2493 
   2494 template<int size>
   2495 bool
   2496 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc(
   2497      Sized_relobj_file<size, false>* object,
   2498      unsigned int r_type)
   2499 {
   2500   int flags = Scan::get_reference_flags(r_type);
   2501   if (flags & Symbol::TLS_REF)
   2502     gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
   2503 	       object->name().c_str(), r_type);
   2504   return flags != 0;
   2505 }
   2506 
   2507 // Scan a relocation for a local symbol.
   2508 
   2509 template<int size>
   2510 inline void
   2511 Target_x86_64<size>::Scan::local(Symbol_table* symtab,
   2512 				 Layout* layout,
   2513 				 Target_x86_64<size>* target,
   2514 				 Sized_relobj_file<size, false>* object,
   2515 				 unsigned int data_shndx,
   2516 				 Output_section* output_section,
   2517 				 const elfcpp::Rela<size, false>& reloc,
   2518 				 unsigned int r_type,
   2519 				 const elfcpp::Sym<size, false>& lsym,
   2520 				 bool is_discarded)
   2521 {
   2522   if (is_discarded)
   2523     return;
   2524 
   2525   // A local STT_GNU_IFUNC symbol may require a PLT entry.
   2526   bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC;
   2527   if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type))
   2528     {
   2529       unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
   2530       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
   2531     }
   2532 
   2533   switch (r_type)
   2534     {
   2535     case elfcpp::R_X86_64_NONE:
   2536     case elfcpp::R_X86_64_GNU_VTINHERIT:
   2537     case elfcpp::R_X86_64_GNU_VTENTRY:
   2538       break;
   2539 
   2540     case elfcpp::R_X86_64_64:
   2541       // If building a shared library (or a position-independent
   2542       // executable), we need to create a dynamic relocation for this
   2543       // location.  The relocation applied at link time will apply the
   2544       // link-time value, so we flag the location with an
   2545       // R_X86_64_RELATIVE relocation so the dynamic loader can
   2546       // relocate it easily.
   2547       if (parameters->options().output_is_position_independent())
   2548 	{
   2549 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
   2550 	  if (size == 64
   2551 	      && !is_ifunc
   2552 	      && parameters->options().experimental_use_relr())
   2553 	    {
   2554 	      Relr_section* relr_dyn = target->relr_dyn_section(layout);
   2555 	      relr_dyn->add_local_relative(object, r_sym, output_section,
   2556 					   data_shndx, reloc.get_r_offset());
   2557 	    }
   2558 	  else
   2559 	    {
   2560 	      Reloc_section* rela_dyn = target->rela_dyn_section(layout);
   2561 	      rela_dyn->add_local_relative(object, r_sym,
   2562 					   (size == 32
   2563 					    ? elfcpp::R_X86_64_RELATIVE64
   2564 					    : elfcpp::R_X86_64_RELATIVE),
   2565 					   output_section, data_shndx,
   2566 					   reloc.get_r_offset(),
   2567 					   reloc.get_r_addend(), is_ifunc);
   2568 	    }
   2569 	}
   2570       break;
   2571 
   2572     case elfcpp::R_X86_64_32:
   2573     case elfcpp::R_X86_64_32S:
   2574     case elfcpp::R_X86_64_16:
   2575     case elfcpp::R_X86_64_8:
   2576       // If building a shared library (or a position-independent
   2577       // executable), we need to create a dynamic relocation for this
   2578       // location.  We can't use an R_X86_64_RELATIVE relocation
   2579       // because that is always a 64-bit relocation.
   2580       if (parameters->options().output_is_position_independent())
   2581 	{
   2582 	  // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32.
   2583 	  if (size == 32 && r_type == elfcpp::R_X86_64_32)
   2584 	    {
   2585 	      unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
   2586 	      if (!is_ifunc && parameters->options().experimental_use_relr())
   2587 		{
   2588 		  Relr_section* relr_dyn = target->relr_dyn_section(layout);
   2589 		  relr_dyn->add_local_relative(object, r_sym, output_section,
   2590 					       data_shndx,
   2591 					       reloc.get_r_offset());
   2592 		}
   2593 	      else
   2594 		{
   2595 		  Reloc_section* rela_dyn = target->rela_dyn_section(layout);
   2596 		  rela_dyn->add_local_relative(object, r_sym,
   2597 					       elfcpp::R_X86_64_RELATIVE,
   2598 					       output_section, data_shndx,
   2599 					       reloc.get_r_offset(),
   2600 					       reloc.get_r_addend(), is_ifunc);
   2601 		}
   2602 	      break;
   2603 	    }
   2604 
   2605 	  this->check_non_pic(object, r_type, NULL);
   2606 
   2607 	  Reloc_section* rela_dyn = target->rela_dyn_section(layout);
   2608 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
   2609 	  if (lsym.get_st_type() != elfcpp::STT_SECTION)
   2610 	    rela_dyn->add_local(object, r_sym, r_type, output_section,
   2611 				data_shndx, reloc.get_r_offset(),
   2612 				reloc.get_r_addend());
   2613 	  else
   2614 	    {
   2615 	      gold_assert(lsym.get_st_value() == 0);
   2616 	      unsigned int shndx = lsym.get_st_shndx();
   2617 	      bool is_ordinary;
   2618 	      shndx = object->adjust_sym_shndx(r_sym, shndx,
   2619 					       &is_ordinary);
   2620 	      if (!is_ordinary)
   2621 		object->error(_("section symbol %u has bad shndx %u"),
   2622 			      r_sym, shndx);
   2623 	      else
   2624 		rela_dyn->add_local_section(object, shndx,
   2625 					    r_type, output_section,
   2626 					    data_shndx, reloc.get_r_offset(),
   2627 					    reloc.get_r_addend());
   2628 	    }
   2629 	}
   2630       break;
   2631 
   2632     case elfcpp::R_X86_64_PC64:
   2633     case elfcpp::R_X86_64_PC32:
   2634     case elfcpp::R_X86_64_PC32_BND:
   2635     case elfcpp::R_X86_64_PC16:
   2636     case elfcpp::R_X86_64_PC8:
   2637       break;
   2638 
   2639     case elfcpp::R_X86_64_PLT32:
   2640     case elfcpp::R_X86_64_PLT32_BND:
   2641       // Since we know this is a local symbol, we can handle this as a
   2642       // PC32 reloc.
   2643       break;
   2644 
   2645     case elfcpp::R_X86_64_GOTPC32:
   2646     case elfcpp::R_X86_64_GOTOFF64:
   2647     case elfcpp::R_X86_64_GOTPC64:
   2648     case elfcpp::R_X86_64_PLTOFF64:
   2649       // We need a GOT section.
   2650       target->got_section(symtab, layout);
   2651       // For PLTOFF64, we'd normally want a PLT section, but since we
   2652       // know this is a local symbol, no PLT is needed.
   2653       break;
   2654 
   2655     case elfcpp::R_X86_64_GOT64:
   2656     case elfcpp::R_X86_64_GOT32:
   2657     case elfcpp::R_X86_64_GOTPCREL64:
   2658     case elfcpp::R_X86_64_GOTPCREL:
   2659     case elfcpp::R_X86_64_GOTPCRELX:
   2660     case elfcpp::R_X86_64_REX_GOTPCRELX:
   2661     case elfcpp::R_X86_64_GOTPLT64:
   2662       {
   2663 	// The symbol requires a GOT section.
   2664 	Output_data_got<64, false>* got = target->got_section(symtab, layout);
   2665 
   2666 	// If the relocation symbol isn't IFUNC,
   2667 	// and is local, then we will convert
   2668 	// mov foo@GOTPCREL(%rip), %reg
   2669 	// to lea foo(%rip), %reg.
   2670 	// in Relocate::relocate.
   2671 	if ((r_type == elfcpp::R_X86_64_GOTPCREL
   2672 	     || r_type == elfcpp::R_X86_64_GOTPCRELX
   2673 	     || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
   2674 	    && reloc.get_r_offset() >= 2
   2675 	    && !is_ifunc)
   2676 	  {
   2677 	    section_size_type stype;
   2678 	    const unsigned char* view = object->section_contents(data_shndx,
   2679 								 &stype, true);
   2680 	    if (view[reloc.get_r_offset() - 2] == 0x8b)
   2681 	      break;
   2682 	  }
   2683 
   2684 
   2685 	// The symbol requires a GOT entry.
   2686 	unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
   2687 
   2688 	// For a STT_GNU_IFUNC symbol we want the PLT offset.  That
   2689 	// lets function pointers compare correctly with shared
   2690 	// libraries.  Otherwise we would need an IRELATIVE reloc.
   2691 	bool is_new;
   2692 	if (is_ifunc)
   2693 	  is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
   2694 	else
   2695 	  is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
   2696 	if (is_new)
   2697 	  {
   2698 	    // If we are generating a shared object, we need to add a
   2699 	    // dynamic relocation for this symbol's GOT entry.
   2700 	    if (parameters->options().output_is_position_independent())
   2701 	      {
   2702 		unsigned int got_offset =
   2703 		  object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
   2704 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
   2705 		// R_X86_64_RELATIVE assumes a 64-bit relocation.
   2706 		if (r_type == elfcpp::R_X86_64_GOT32)
   2707 		  {
   2708 		    this->check_non_pic(object, r_type, NULL);
   2709 
   2710 		    gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
   2711 		    rela_dyn->add_local(
   2712 			object, r_sym, r_type, got,
   2713 			got_offset, 0);
   2714 		  }
   2715 		else if (size == 64
   2716 			 && !is_ifunc
   2717 			 && parameters->options().experimental_use_relr())
   2718 		  {
   2719 		    Relr_section* relr_dyn =
   2720 		      target->relr_dyn_section(layout);
   2721 		    relr_dyn->add_local_relative(object, r_sym,
   2722 						 got, got_offset);
   2723 		  }
   2724 		else
   2725 		  {
   2726 		    rela_dyn->add_local_relative(object, r_sym,
   2727 						 elfcpp::R_X86_64_RELATIVE,
   2728 						 got, got_offset, 0, is_ifunc);
   2729 		  }
   2730 	      }
   2731 	  }
   2732 	// For GOTPLT64, we'd normally want a PLT section, but since
   2733 	// we know this is a local symbol, no PLT is needed.
   2734       }
   2735       break;
   2736 
   2737     case elfcpp::R_X86_64_COPY:
   2738     case elfcpp::R_X86_64_GLOB_DAT:
   2739     case elfcpp::R_X86_64_JUMP_SLOT:
   2740     case elfcpp::R_X86_64_RELATIVE:
   2741     case elfcpp::R_X86_64_IRELATIVE:
   2742       // These are outstanding tls relocs, which are unexpected when linking
   2743     case elfcpp::R_X86_64_TPOFF64:
   2744     case elfcpp::R_X86_64_DTPMOD64:
   2745     case elfcpp::R_X86_64_TLSDESC:
   2746       gold_error(_("%s: unexpected reloc %u in object file"),
   2747 		 object->name().c_str(), r_type);
   2748       break;
   2749 
   2750       // These are initial tls relocs, which are expected when linking
   2751     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
   2752     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
   2753     case elfcpp::R_X86_64_TLSDESC_CALL:
   2754     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
   2755     case elfcpp::R_X86_64_DTPOFF32:
   2756     case elfcpp::R_X86_64_DTPOFF64:
   2757     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
   2758     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
   2759       {
   2760 	bool output_is_shared = parameters->options().shared();
   2761 	const tls::Tls_optimization optimized_type
   2762 	    = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared,
   2763 						      r_type);
   2764 	switch (r_type)
   2765 	  {
   2766 	  case elfcpp::R_X86_64_TLSGD:       // General-dynamic
   2767 	    if (optimized_type == tls::TLSOPT_NONE)
   2768 	      {
   2769 		// Create a pair of GOT entries for the module index and
   2770 		// dtv-relative offset.
   2771 		Output_data_got<64, false>* got
   2772 		    = target->got_section(symtab, layout);
   2773 		unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
   2774 		unsigned int shndx = lsym.get_st_shndx();
   2775 		bool is_ordinary;
   2776 		shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
   2777 		if (!is_ordinary)
   2778 		  object->error(_("local symbol %u has bad shndx %u"),
   2779 			      r_sym, shndx);
   2780 		else
   2781 		  got->add_local_pair_with_rel(object, r_sym,
   2782 					       shndx,
   2783 					       GOT_TYPE_TLS_PAIR,
   2784 					       target->rela_dyn_section(layout),
   2785 					       elfcpp::R_X86_64_DTPMOD64);
   2786 	      }
   2787 	    else if (optimized_type != tls::TLSOPT_TO_LE)
   2788 	      unsupported_reloc_local(object, r_type);
   2789 	    break;
   2790 
   2791 	  case elfcpp::R_X86_64_GOTPC32_TLSDESC:
   2792 	    target->define_tls_base_symbol(symtab, layout);
   2793 	    if (optimized_type == tls::TLSOPT_NONE)
   2794 	      {
   2795 		// Create reserved PLT and GOT entries for the resolver.
   2796 		target->reserve_tlsdesc_entries(symtab, layout);
   2797 
   2798 		// Generate a double GOT entry with an
   2799 		// R_X86_64_TLSDESC reloc.  The R_X86_64_TLSDESC reloc
   2800 		// is resolved lazily, so the GOT entry needs to be in
   2801 		// an area in .got.plt, not .got.  Call got_section to
   2802 		// make sure the section has been created.
   2803 		target->got_section(symtab, layout);
   2804 		Output_data_got<64, false>* got = target->got_tlsdesc_section();
   2805 		unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
   2806 		if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
   2807 		  {
   2808 		    unsigned int got_offset = got->add_constant(0);
   2809 		    got->add_constant(0);
   2810 		    object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
   2811 						 got_offset);
   2812 		    Reloc_section* rt = target->rela_tlsdesc_section(layout);
   2813 		    // We store the arguments we need in a vector, and
   2814 		    // use the index into the vector as the parameter
   2815 		    // to pass to the target specific routines.
   2816 		    uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
   2817 		    void* arg = reinterpret_cast<void*>(intarg);
   2818 		    rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
   2819 					    got, got_offset, 0);
   2820 		  }
   2821 	      }
   2822 	    else if (optimized_type != tls::TLSOPT_TO_LE)
   2823 	      unsupported_reloc_local(object, r_type);
   2824 	    break;
   2825 
   2826 	  case elfcpp::R_X86_64_TLSDESC_CALL:
   2827 	    break;
   2828 
   2829 	  case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
   2830 	    if (optimized_type == tls::TLSOPT_NONE)
   2831 	      {
   2832 		// Create a GOT entry for the module index.
   2833 		target->got_mod_index_entry(symtab, layout, object);
   2834 	      }
   2835 	    else if (optimized_type != tls::TLSOPT_TO_LE)
   2836 	      unsupported_reloc_local(object, r_type);
   2837 	    break;
   2838 
   2839 	  case elfcpp::R_X86_64_DTPOFF32:
   2840 	  case elfcpp::R_X86_64_DTPOFF64:
   2841 	    break;
   2842 
   2843 	  case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
   2844 	    layout->set_has_static_tls();
   2845 	    if (optimized_type == tls::TLSOPT_NONE)
   2846 	      {
   2847 		// Create a GOT entry for the tp-relative offset.
   2848 		Output_data_got<64, false>* got
   2849 		    = target->got_section(symtab, layout);
   2850 		unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info());
   2851 		got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET,
   2852 					target->rela_dyn_section(layout),
   2853 					elfcpp::R_X86_64_TPOFF64);
   2854 	      }
   2855 	    else if (optimized_type != tls::TLSOPT_TO_LE)
   2856 	      unsupported_reloc_local(object, r_type);
   2857 	    break;
   2858 
   2859 	  case elfcpp::R_X86_64_TPOFF32:     // Local-exec
   2860 	    layout->set_has_static_tls();
   2861 	    if (output_is_shared)
   2862 	      unsupported_reloc_local(object, r_type);
   2863 	    break;
   2864 
   2865 	  default:
   2866 	    gold_unreachable();
   2867 	  }
   2868       }
   2869       break;
   2870 
   2871     case elfcpp::R_X86_64_SIZE32:
   2872     case elfcpp::R_X86_64_SIZE64:
   2873     default:
   2874       gold_error(_("%s: unsupported reloc %u against local symbol"),
   2875 		 object->name().c_str(), r_type);
   2876       break;
   2877     }
   2878 }
   2879 
   2880 
   2881 // Report an unsupported relocation against a global symbol.
   2882 
   2883 template<int size>
   2884 void
   2885 Target_x86_64<size>::Scan::unsupported_reloc_global(
   2886     Sized_relobj_file<size, false>* object,
   2887     unsigned int r_type,
   2888     Symbol* gsym)
   2889 {
   2890   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
   2891 	     object->name().c_str(), r_type, gsym->demangled_name().c_str());
   2892 }
   2893 
   2894 // Returns true if this relocation type could be that of a function pointer.
   2895 template<int size>
   2896 inline bool
   2897 Target_x86_64<size>::Scan::possible_function_pointer_reloc(
   2898     Sized_relobj_file<size, false>* src_obj,
   2899     unsigned int src_indx,
   2900     unsigned int r_offset,
   2901     unsigned int r_type)
   2902 {
   2903   switch (r_type)
   2904     {
   2905     case elfcpp::R_X86_64_64:
   2906     case elfcpp::R_X86_64_32:
   2907     case elfcpp::R_X86_64_32S:
   2908     case elfcpp::R_X86_64_16:
   2909     case elfcpp::R_X86_64_8:
   2910     case elfcpp::R_X86_64_GOT64:
   2911     case elfcpp::R_X86_64_GOT32:
   2912     case elfcpp::R_X86_64_GOTPCREL64:
   2913     case elfcpp::R_X86_64_GOTPCREL:
   2914     case elfcpp::R_X86_64_GOTPCRELX:
   2915     case elfcpp::R_X86_64_REX_GOTPCRELX:
   2916     case elfcpp::R_X86_64_GOTPLT64:
   2917       {
   2918 	return true;
   2919       }
   2920     case elfcpp::R_X86_64_PC32:
   2921       {
   2922         // This relocation may be used both for function calls and
   2923         // for taking address of a function. We distinguish between
   2924         // them by checking the opcodes.
   2925         uint64_t sh_flags = src_obj->section_flags(src_indx);
   2926         bool is_executable = (sh_flags & elfcpp::SHF_EXECINSTR) != 0;
   2927         if (is_executable)
   2928           {
   2929             section_size_type stype;
   2930             const unsigned char* view = src_obj->section_contents(src_indx,
   2931                                                                   &stype,
   2932                                                                   true);
   2933 
   2934             // call
   2935             if (r_offset >= 1
   2936                 && view[r_offset - 1] == 0xe8)
   2937               return false;
   2938 
   2939             // jmp
   2940             if (r_offset >= 1
   2941                 && view[r_offset - 1] == 0xe9)
   2942               return false;
   2943 
   2944             // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg
   2945             if (r_offset >= 2
   2946                 && view[r_offset - 2] == 0x0f
   2947                 && view[r_offset - 1] >= 0x80
   2948                 && view[r_offset - 1] <= 0x8f)
   2949               return false;
   2950           }
   2951 
   2952         // Be conservative and treat all others as function pointers.
   2953         return true;
   2954       }
   2955     }
   2956   return false;
   2957 }
   2958 
   2959 // For safe ICF, scan a relocation for a local symbol to check if it
   2960 // corresponds to a function pointer being taken.  In that case mark
   2961 // the function whose pointer was taken as not foldable.
   2962 
   2963 template<int size>
   2964 inline bool
   2965 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer(
   2966   Symbol_table* ,
   2967   Layout* ,
   2968   Target_x86_64<size>* ,
   2969   Sized_relobj_file<size, false>* src_obj,
   2970   unsigned int src_indx,
   2971   Output_section* ,
   2972   const elfcpp::Rela<size, false>& reloc,
   2973   unsigned int r_type,
   2974   const elfcpp::Sym<size, false>&)
   2975 {
   2976   // When building a shared library, do not fold any local symbols as it is
   2977   // not possible to distinguish pointer taken versus a call by looking at
   2978   // the relocation types.
   2979   if (parameters->options().shared())
   2980     return true;
   2981 
   2982   return possible_function_pointer_reloc(src_obj, src_indx,
   2983                                          reloc.get_r_offset(), r_type);
   2984 }
   2985 
   2986 // For safe ICF, scan a relocation for a global symbol to check if it
   2987 // corresponds to a function pointer being taken.  In that case mark
   2988 // the function whose pointer was taken as not foldable.
   2989 
   2990 template<int size>
   2991 inline bool
   2992 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer(
   2993   Symbol_table*,
   2994   Layout* ,
   2995   Target_x86_64<size>* ,
   2996   Sized_relobj_file<size, false>* src_obj,
   2997   unsigned int src_indx,
   2998   Output_section* ,
   2999   const elfcpp::Rela<size, false>& reloc,
   3000   unsigned int r_type,
   3001   Symbol* gsym)
   3002 {
   3003   // When building a shared library, do not fold symbols whose visibility
   3004   // is hidden, internal or protected.
   3005   if (parameters->options().shared()
   3006       && (gsym->visibility() == elfcpp::STV_INTERNAL
   3007 	  || gsym->visibility() == elfcpp::STV_PROTECTED
   3008 	  || gsym->visibility() == elfcpp::STV_HIDDEN))
   3009     return true;
   3010 
   3011   return possible_function_pointer_reloc(src_obj, src_indx,
   3012                                          reloc.get_r_offset(), r_type);
   3013 }
   3014 
   3015 // Scan a relocation for a global symbol.
   3016 
   3017 template<int size>
   3018 inline void
   3019 Target_x86_64<size>::Scan::global(Symbol_table* symtab,
   3020 			    Layout* layout,
   3021 			    Target_x86_64<size>* target,
   3022 			    Sized_relobj_file<size, false>* object,
   3023 			    unsigned int data_shndx,
   3024 			    Output_section* output_section,
   3025 			    const elfcpp::Rela<size, false>& reloc,
   3026 			    unsigned int r_type,
   3027 			    Symbol* gsym)
   3028 {
   3029   // A STT_GNU_IFUNC symbol may require a PLT entry.
   3030   if (gsym->type() == elfcpp::STT_GNU_IFUNC
   3031       && this->reloc_needs_plt_for_ifunc(object, r_type))
   3032     target->make_plt_entry(symtab, layout, gsym);
   3033 
   3034   switch (r_type)
   3035     {
   3036     case elfcpp::R_X86_64_NONE:
   3037     case elfcpp::R_X86_64_GNU_VTINHERIT:
   3038     case elfcpp::R_X86_64_GNU_VTENTRY:
   3039       break;
   3040 
   3041     case elfcpp::R_X86_64_64:
   3042     case elfcpp::R_X86_64_32:
   3043     case elfcpp::R_X86_64_32S:
   3044     case elfcpp::R_X86_64_16:
   3045     case elfcpp::R_X86_64_8:
   3046       {
   3047 	// Make a PLT entry if necessary.
   3048 	if (gsym->needs_plt_entry())
   3049 	  {
   3050 	    target->make_plt_entry(symtab, layout, gsym);
   3051 	    // Since this is not a PC-relative relocation, we may be
   3052 	    // taking the address of a function. In that case we need to
   3053 	    // set the entry in the dynamic symbol table to the address of
   3054 	    // the PLT entry.
   3055 	    if (gsym->is_from_dynobj() && !parameters->options().shared())
   3056 	      gsym->set_needs_dynsym_value();
   3057 	  }
   3058 	// Make a dynamic relocation if necessary.
   3059 	if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
   3060 	  {
   3061 	    if (!parameters->options().output_is_position_independent()
   3062 		&& gsym->may_need_copy_reloc())
   3063 	      {
   3064 		target->copy_reloc(symtab, layout, object,
   3065 				   data_shndx, output_section, gsym, reloc);
   3066 	      }
   3067 	    else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
   3068 		      || (size == 32 && r_type == elfcpp::R_X86_64_32))
   3069 		     && gsym->type() == elfcpp::STT_GNU_IFUNC
   3070 		     && gsym->can_use_relative_reloc(false)
   3071 		     && !gsym->is_from_dynobj()
   3072 		     && !gsym->is_undefined()
   3073 		     && !gsym->is_preemptible())
   3074 	      {
   3075 		// Use an IRELATIVE reloc for a locally defined
   3076 		// STT_GNU_IFUNC symbol.  This makes a function
   3077 		// address in a PIE executable match the address in a
   3078 		// shared library that it links against.
   3079 		Reloc_section* rela_dyn =
   3080 		  target->rela_irelative_section(layout);
   3081 		unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
   3082 		rela_dyn->add_symbolless_global_addend(gsym, r_type,
   3083 						       output_section, object,
   3084 						       data_shndx,
   3085 						       reloc.get_r_offset(),
   3086 						       reloc.get_r_addend());
   3087 	      }
   3088 	    else if (((size == 64 && r_type == elfcpp::R_X86_64_64)
   3089 		      || (size == 32 && r_type == elfcpp::R_X86_64_32))
   3090 		     && gsym->can_use_relative_reloc(false))
   3091 	      {
   3092 		if (parameters->options().experimental_use_relr())
   3093 		  {
   3094 		    Relr_section* relr_dyn =
   3095 		      target->relr_dyn_section(layout);
   3096 		    relr_dyn->add_global_relative(gsym, output_section,
   3097 						  object, data_shndx,
   3098 						  reloc.get_r_offset());
   3099 		  }
   3100 		else
   3101 		  {
   3102 		    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
   3103 		    rela_dyn->add_global_relative(gsym,
   3104 						  elfcpp::R_X86_64_RELATIVE,
   3105 						  output_section, object,
   3106 						  data_shndx,
   3107 						  reloc.get_r_offset(),
   3108 						  reloc.get_r_addend(), false);
   3109 		  }
   3110 	      }
   3111 	    else
   3112 	      {
   3113 		this->check_non_pic(object, r_type, gsym);
   3114 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
   3115 		rela_dyn->add_global(gsym, r_type, output_section, object,
   3116 				     data_shndx, reloc.get_r_offset(),
   3117 				     reloc.get_r_addend());
   3118 	      }
   3119 	  }
   3120       }
   3121       break;
   3122 
   3123     case elfcpp::R_X86_64_PC64:
   3124     case elfcpp::R_X86_64_PC32:
   3125     case elfcpp::R_X86_64_PC32_BND:
   3126     case elfcpp::R_X86_64_PC16:
   3127     case elfcpp::R_X86_64_PC8:
   3128       {
   3129 	// Make a PLT entry if necessary.
   3130 	if (gsym->needs_plt_entry())
   3131 	  target->make_plt_entry(symtab, layout, gsym);
   3132 	// Make a dynamic relocation if necessary.
   3133 	if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
   3134 	  {
   3135 	    if (parameters->options().output_is_executable()
   3136 		&& gsym->may_need_copy_reloc())
   3137 	      {
   3138 		target->copy_reloc(symtab, layout, object,
   3139 				   data_shndx, output_section, gsym, reloc);
   3140 	      }
   3141 	    else
   3142 	      {
   3143 		this->check_non_pic(object, r_type, gsym);
   3144 		Reloc_section* rela_dyn = target->rela_dyn_section(layout);
   3145 		rela_dyn->add_global(gsym, r_type, output_section, object,
   3146 				     data_shndx, reloc.get_r_offset(),
   3147 				     reloc.get_r_addend());
   3148 	      }
   3149 	  }
   3150       }
   3151       break;
   3152 
   3153     case elfcpp::R_X86_64_GOT64:
   3154     case elfcpp::R_X86_64_GOT32:
   3155     case elfcpp::R_X86_64_GOTPCREL64:
   3156     case elfcpp::R_X86_64_GOTPCREL:
   3157     case elfcpp::R_X86_64_GOTPCRELX:
   3158     case elfcpp::R_X86_64_REX_GOTPCRELX:
   3159     case elfcpp::R_X86_64_GOTPLT64:
   3160       {
   3161 	// The symbol requires a GOT entry.
   3162 	Output_data_got<64, false>* got = target->got_section(symtab, layout);
   3163 
   3164 	// If we convert this from
   3165 	// mov foo@GOTPCREL(%rip), %reg
   3166 	// to lea foo(%rip), %reg.
   3167 	// OR
   3168 	// if we convert
   3169 	// (callq|jmpq) *foo@GOTPCRELX(%rip) to
   3170 	// (callq|jmpq) foo
   3171 	// in Relocate::relocate, then there is nothing to do here.
   3172 
   3173         Lazy_view<size> view(object, data_shndx);
   3174         size_t r_offset = reloc.get_r_offset();
   3175         if (r_offset >= 2
   3176             && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
   3177                                                            r_offset, &view))
   3178           break;
   3179 
   3180 	if (r_offset >= 2
   3181 	    && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type,
   3182 								r_offset,
   3183 								&view))
   3184           break;
   3185 
   3186 	if (gsym->final_value_is_known())
   3187 	  {
   3188 	    // For a STT_GNU_IFUNC symbol we want the PLT address.
   3189 	    if (gsym->type() == elfcpp::STT_GNU_IFUNC)
   3190 	      got->add_global_plt(gsym, GOT_TYPE_STANDARD);
   3191 	    else
   3192 	      got->add_global(gsym, GOT_TYPE_STANDARD);
   3193 	  }
   3194 	else
   3195 	  {
   3196 	    // If this symbol is not fully resolved, we need to add a
   3197 	    // dynamic relocation for it.
   3198 	    Reloc_section* rela_dyn = target->rela_dyn_section(layout);
   3199 
   3200 	    // Use a GLOB_DAT rather than a RELATIVE reloc if:
   3201 	    //
   3202 	    // 1) The symbol may be defined in some other module.
   3203 	    //
   3204 	    // 2) We are building a shared library and this is a
   3205 	    // protected symbol; using GLOB_DAT means that the dynamic
   3206 	    // linker can use the address of the PLT in the main
   3207 	    // executable when appropriate so that function address
   3208 	    // comparisons work.
   3209 	    //
   3210 	    // 3) This is a STT_GNU_IFUNC symbol in position dependent
   3211 	    // code, again so that function address comparisons work.
   3212 	    if (gsym->is_from_dynobj()
   3213 		|| gsym->is_undefined()
   3214 		|| gsym->is_preemptible()
   3215 		|| (gsym->visibility() == elfcpp::STV_PROTECTED
   3216 		    && parameters->options().shared())
   3217 		|| (gsym->type() == elfcpp::STT_GNU_IFUNC
   3218 		    && parameters->options().output_is_position_independent()))
   3219 	      got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
   3220 				       elfcpp::R_X86_64_GLOB_DAT);
   3221 	    else
   3222 	      {
   3223 		// For a STT_GNU_IFUNC symbol we want to write the PLT
   3224 		// offset into the GOT, so that function pointer
   3225 		// comparisons work correctly.
   3226 		bool is_new;
   3227 		if (gsym->type() != elfcpp::STT_GNU_IFUNC)
   3228 		  is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
   3229 		else
   3230 		  {
   3231 		    is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
   3232 		    // Tell the dynamic linker to use the PLT address
   3233 		    // when resolving relocations.
   3234 		    if (gsym->is_from_dynobj()
   3235 			&& !parameters->options().shared())
   3236 		      gsym->set_needs_dynsym_value();
   3237 		  }
   3238 		if (is_new)
   3239 		  {
   3240 		    unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
   3241 		    if (parameters->options().experimental_use_relr())
   3242 		      {
   3243 			Relr_section* relr_dyn =
   3244 			  target->relr_dyn_section(layout);
   3245 			relr_dyn->add_global_relative(gsym, got, got_off);
   3246 		      }
   3247 		    else
   3248 		      {
   3249 			rela_dyn->add_global_relative(gsym,
   3250 						      elfcpp::R_X86_64_RELATIVE,
   3251 						      got, got_off, 0, false);
   3252 		      }
   3253 		  }
   3254 	      }
   3255 	  }
   3256       }
   3257       break;
   3258 
   3259     case elfcpp::R_X86_64_PLT32:
   3260     case elfcpp::R_X86_64_PLT32_BND:
   3261       // If the symbol is fully resolved, this is just a PC32 reloc.
   3262       // Otherwise we need a PLT entry.
   3263       if (gsym->final_value_is_known())
   3264 	break;
   3265       // If building a shared library, we can also skip the PLT entry
   3266       // if the symbol is defined in the output file and is protected
   3267       // or hidden.
   3268       if (gsym->is_defined()
   3269 	  && !gsym->is_from_dynobj()
   3270 	  && !gsym->is_preemptible())
   3271 	break;
   3272       target->make_plt_entry(symtab, layout, gsym);
   3273       break;
   3274 
   3275     case elfcpp::R_X86_64_GOTPC32:
   3276     case elfcpp::R_X86_64_GOTOFF64:
   3277     case elfcpp::R_X86_64_GOTPC64:
   3278     case elfcpp::R_X86_64_PLTOFF64:
   3279       // We need a GOT section.
   3280       target->got_section(symtab, layout);
   3281       // For PLTOFF64, we also need a PLT entry (but only if the
   3282       // symbol is not fully resolved).
   3283       if (r_type == elfcpp::R_X86_64_PLTOFF64
   3284 	  && !gsym->final_value_is_known())
   3285 	target->make_plt_entry(symtab, layout, gsym);
   3286       break;
   3287 
   3288     case elfcpp::R_X86_64_COPY:
   3289     case elfcpp::R_X86_64_GLOB_DAT:
   3290     case elfcpp::R_X86_64_JUMP_SLOT:
   3291     case elfcpp::R_X86_64_RELATIVE:
   3292     case elfcpp::R_X86_64_IRELATIVE:
   3293       // These are outstanding tls relocs, which are unexpected when linking
   3294     case elfcpp::R_X86_64_TPOFF64:
   3295     case elfcpp::R_X86_64_DTPMOD64:
   3296     case elfcpp::R_X86_64_TLSDESC:
   3297       gold_error(_("%s: unexpected reloc %u in object file"),
   3298 		 object->name().c_str(), r_type);
   3299       break;
   3300 
   3301       // These are initial tls relocs, which are expected for global()
   3302     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
   3303     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
   3304     case elfcpp::R_X86_64_TLSDESC_CALL:
   3305     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
   3306     case elfcpp::R_X86_64_DTPOFF32:
   3307     case elfcpp::R_X86_64_DTPOFF64:
   3308     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
   3309     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
   3310       {
   3311 	// For the Initial-Exec model, we can treat undef symbols as final
   3312 	// when building an executable.
   3313 	const bool is_final = (gsym->final_value_is_known() ||
   3314 			       (r_type == elfcpp::R_X86_64_GOTTPOFF &&
   3315 			        gsym->is_undefined() &&
   3316 				parameters->options().output_is_executable()));
   3317 	const tls::Tls_optimization optimized_type
   3318 	    = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
   3319 	switch (r_type)
   3320 	  {
   3321 	  case elfcpp::R_X86_64_TLSGD:       // General-dynamic
   3322 	    if (optimized_type == tls::TLSOPT_NONE)
   3323 	      {
   3324 		// Create a pair of GOT entries for the module index and
   3325 		// dtv-relative offset.
   3326 		Output_data_got<64, false>* got
   3327 		    = target->got_section(symtab, layout);
   3328 		got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
   3329 					      target->rela_dyn_section(layout),
   3330 					      elfcpp::R_X86_64_DTPMOD64,
   3331 					      elfcpp::R_X86_64_DTPOFF64);
   3332 	      }
   3333 	    else if (optimized_type == tls::TLSOPT_TO_IE)
   3334 	      {
   3335 		// Create a GOT entry for the tp-relative offset.
   3336 		Output_data_got<64, false>* got
   3337 		    = target->got_section(symtab, layout);
   3338 		got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
   3339 					 target->rela_dyn_section(layout),
   3340 					 elfcpp::R_X86_64_TPOFF64);
   3341 	      }
   3342 	    else if (optimized_type != tls::TLSOPT_TO_LE)
   3343 	      unsupported_reloc_global(object, r_type, gsym);
   3344 	    break;
   3345 
   3346 	  case elfcpp::R_X86_64_GOTPC32_TLSDESC:
   3347 	    target->define_tls_base_symbol(symtab, layout);
   3348 	    if (optimized_type == tls::TLSOPT_NONE)
   3349 	      {
   3350 		// Create reserved PLT and GOT entries for the resolver.
   3351 		target->reserve_tlsdesc_entries(symtab, layout);
   3352 
   3353 		// Create a double GOT entry with an R_X86_64_TLSDESC
   3354 		// reloc.  The R_X86_64_TLSDESC reloc is resolved
   3355 		// lazily, so the GOT entry needs to be in an area in
   3356 		// .got.plt, not .got.  Call got_section to make sure
   3357 		// the section has been created.
   3358 		target->got_section(symtab, layout);
   3359 		Output_data_got<64, false>* got = target->got_tlsdesc_section();
   3360 		Reloc_section* rt = target->rela_tlsdesc_section(layout);
   3361 		got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
   3362 					      elfcpp::R_X86_64_TLSDESC, 0);
   3363 	      }
   3364 	    else if (optimized_type == tls::TLSOPT_TO_IE)
   3365 	      {
   3366 		// Create a GOT entry for the tp-relative offset.
   3367 		Output_data_got<64, false>* got
   3368 		    = target->got_section(symtab, layout);
   3369 		got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
   3370 					 target->rela_dyn_section(layout),
   3371 					 elfcpp::R_X86_64_TPOFF64);
   3372 	      }
   3373 	    else if (optimized_type != tls::TLSOPT_TO_LE)
   3374 	      unsupported_reloc_global(object, r_type, gsym);
   3375 	    break;
   3376 
   3377 	  case elfcpp::R_X86_64_TLSDESC_CALL:
   3378 	    break;
   3379 
   3380 	  case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
   3381 	    if (optimized_type == tls::TLSOPT_NONE)
   3382 	      {
   3383 		// Create a GOT entry for the module index.
   3384 		target->got_mod_index_entry(symtab, layout, object);
   3385 	      }
   3386 	    else if (optimized_type != tls::TLSOPT_TO_LE)
   3387 	      unsupported_reloc_global(object, r_type, gsym);
   3388 	    break;
   3389 
   3390 	  case elfcpp::R_X86_64_DTPOFF32:
   3391 	  case elfcpp::R_X86_64_DTPOFF64:
   3392 	    break;
   3393 
   3394 	  case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
   3395 	    layout->set_has_static_tls();
   3396 	    if (optimized_type == tls::TLSOPT_NONE)
   3397 	      {
   3398 		// Create a GOT entry for the tp-relative offset.
   3399 		Output_data_got<64, false>* got
   3400 		    = target->got_section(symtab, layout);
   3401 		got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
   3402 					 target->rela_dyn_section(layout),
   3403 					 elfcpp::R_X86_64_TPOFF64);
   3404 	      }
   3405 	    else if (optimized_type != tls::TLSOPT_TO_LE)
   3406 	      unsupported_reloc_global(object, r_type, gsym);
   3407 	    break;
   3408 
   3409 	  case elfcpp::R_X86_64_TPOFF32:     // Local-exec
   3410 	    layout->set_has_static_tls();
   3411 	    if (parameters->options().shared())
   3412 	      unsupported_reloc_global(object, r_type, gsym);
   3413 	    break;
   3414 
   3415 	  default:
   3416 	    gold_unreachable();
   3417 	  }
   3418       }
   3419       break;
   3420 
   3421     case elfcpp::R_X86_64_SIZE32:
   3422     case elfcpp::R_X86_64_SIZE64:
   3423     default:
   3424       gold_error(_("%s: unsupported reloc %u against global symbol %s"),
   3425 		 object->name().c_str(), r_type,
   3426 		 gsym->demangled_name().c_str());
   3427       break;
   3428     }
   3429 }
   3430 
   3431 template<int size>
   3432 void
   3433 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
   3434 				       Layout* layout,
   3435 				       Sized_relobj_file<size, false>* object,
   3436 				       unsigned int data_shndx,
   3437 				       unsigned int sh_type,
   3438 				       const unsigned char* prelocs,
   3439 				       size_t reloc_count,
   3440 				       Output_section* output_section,
   3441 				       bool needs_special_offset_handling,
   3442 				       size_t local_symbol_count,
   3443 				       const unsigned char* plocal_symbols)
   3444 {
   3445   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
   3446       Classify_reloc;
   3447 
   3448   if (sh_type == elfcpp::SHT_REL)
   3449     {
   3450       return;
   3451     }
   3452 
   3453    gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
   3454 			   Classify_reloc>(
   3455     symtab,
   3456     layout,
   3457     this,
   3458     object,
   3459     data_shndx,
   3460     prelocs,
   3461     reloc_count,
   3462     output_section,
   3463     needs_special_offset_handling,
   3464     local_symbol_count,
   3465     plocal_symbols);
   3466 
   3467 }
   3468 // Scan relocations for a section.
   3469 
   3470 template<int size>
   3471 void
   3472 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
   3473 				 Layout* layout,
   3474 				 Sized_relobj_file<size, false>* object,
   3475 				 unsigned int data_shndx,
   3476 				 unsigned int sh_type,
   3477 				 const unsigned char* prelocs,
   3478 				 size_t reloc_count,
   3479 				 Output_section* output_section,
   3480 				 bool needs_special_offset_handling,
   3481 				 size_t local_symbol_count,
   3482 				 const unsigned char* plocal_symbols)
   3483 {
   3484   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
   3485       Classify_reloc;
   3486 
   3487   if (sh_type == elfcpp::SHT_REL)
   3488     {
   3489       gold_error(_("%s: unsupported REL reloc section"),
   3490 		 object->name().c_str());
   3491       return;
   3492     }
   3493 
   3494   gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
   3495     symtab,
   3496     layout,
   3497     this,
   3498     object,
   3499     data_shndx,
   3500     prelocs,
   3501     reloc_count,
   3502     output_section,
   3503     needs_special_offset_handling,
   3504     local_symbol_count,
   3505     plocal_symbols);
   3506 }
   3507 
   3508 // Finalize the sections.
   3509 
   3510 template<int size>
   3511 void
   3512 Target_x86_64<size>::do_finalize_sections(
   3513     Layout* layout,
   3514     const Input_objects*,
   3515     Symbol_table* symtab)
   3516 {
   3517   const Reloc_section* rel_plt = (this->plt_ == NULL
   3518 				  ? NULL
   3519 				  : this->plt_->rela_plt());
   3520   layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
   3521 				  this->rela_dyn_, true, false,
   3522 				  this->relr_dyn_);
   3523 
   3524   // Fill in some more dynamic tags.
   3525   Output_data_dynamic* const odyn = layout->dynamic_data();
   3526   if (odyn != NULL)
   3527     {
   3528       if (this->plt_ != NULL
   3529 	  && this->plt_->output_section() != NULL
   3530 	  && this->plt_->has_tlsdesc_entry())
   3531 	{
   3532 	  unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
   3533 	  unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
   3534 	  this->got_->finalize_data_size();
   3535 	  odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
   3536 					this->plt_, plt_offset);
   3537 	  odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
   3538 					this->got_, got_offset);
   3539 	}
   3540     }
   3541 
   3542   // Emit any relocs we saved in an attempt to avoid generating COPY
   3543   // relocs.
   3544   if (this->copy_relocs_.any_saved_relocs())
   3545     this->copy_relocs_.emit(this->rela_dyn_section(layout));
   3546 
   3547   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
   3548   // the .got.plt section.
   3549   Symbol* sym = this->global_offset_table_;
   3550   if (sym != NULL)
   3551     {
   3552       uint64_t data_size = this->got_plt_->current_data_size();
   3553       symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
   3554     }
   3555 
   3556   if (parameters->doing_static_link()
   3557       && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
   3558     {
   3559       // If linking statically, make sure that the __rela_iplt symbols
   3560       // were defined if necessary, even if we didn't create a PLT.
   3561       static const Define_symbol_in_segment syms[] =
   3562 	{
   3563 	  {
   3564 	    "__rela_iplt_start",	// name
   3565 	    elfcpp::PT_LOAD,		// segment_type
   3566 	    elfcpp::PF_W,		// segment_flags_set
   3567 	    elfcpp::PF(0),		// segment_flags_clear
   3568 	    0,				// value
   3569 	    0,				// size
   3570 	    elfcpp::STT_NOTYPE,		// type
   3571 	    elfcpp::STB_GLOBAL,		// binding
   3572 	    elfcpp::STV_HIDDEN,		// visibility
   3573 	    0,				// nonvis
   3574 	    Symbol::SEGMENT_START,	// offset_from_base
   3575 	    true			// only_if_ref
   3576 	  },
   3577 	  {
   3578 	    "__rela_iplt_end",		// name
   3579 	    elfcpp::PT_LOAD,		// segment_type
   3580 	    elfcpp::PF_W,		// segment_flags_set
   3581 	    elfcpp::PF(0),		// segment_flags_clear
   3582 	    0,				// value
   3583 	    0,				// size
   3584 	    elfcpp::STT_NOTYPE,		// type
   3585 	    elfcpp::STB_GLOBAL,		// binding
   3586 	    elfcpp::STV_HIDDEN,		// visibility
   3587 	    0,				// nonvis
   3588 	    Symbol::SEGMENT_START,	// offset_from_base
   3589 	    true			// only_if_ref
   3590 	  }
   3591 	};
   3592 
   3593       symtab->define_symbols(layout, 2, syms,
   3594 			     layout->script_options()->saw_sections_clause());
   3595     }
   3596 }
   3597 
   3598 // For x32, we need to handle PC-relative relocations using full 64-bit
   3599 // arithmetic, so that we can detect relocation overflows properly.
   3600 // This class overrides the pcrela32_check methods from the defaults in
   3601 // Relocate_functions in reloc.h.
   3602 
   3603 template<int size>
   3604 class X86_64_relocate_functions : public Relocate_functions<size, false>
   3605 {
   3606  public:
   3607   typedef Relocate_functions<size, false> Base;
   3608 
   3609   // Do a simple PC relative relocation with the addend in the
   3610   // relocation.
   3611   static inline typename Base::Reloc_status
   3612   pcrela32_check(unsigned char* view,
   3613 		 typename elfcpp::Elf_types<64>::Elf_Addr value,
   3614 		 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
   3615 		 typename elfcpp::Elf_types<64>::Elf_Addr address)
   3616   {
   3617     typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
   3618     Valtype* wv = reinterpret_cast<Valtype*>(view);
   3619     value = value + addend - address;
   3620     elfcpp::Swap<32, false>::writeval(wv, value);
   3621     return (Bits<32>::has_overflow(value)
   3622 	    ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
   3623   }
   3624 
   3625   // Do a simple PC relative relocation with a Symbol_value with the
   3626   // addend in the relocation.
   3627   static inline typename Base::Reloc_status
   3628   pcrela32_check(unsigned char* view,
   3629 		 const Sized_relobj_file<size, false>* object,
   3630 		 const Symbol_value<size>* psymval,
   3631 		 typename elfcpp::Elf_types<64>::Elf_Swxword addend,
   3632 		 typename elfcpp::Elf_types<64>::Elf_Addr address)
   3633   {
   3634     typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
   3635     Valtype* wv = reinterpret_cast<Valtype*>(view);
   3636     typename elfcpp::Elf_types<64>::Elf_Addr value;
   3637     if (addend >= 0)
   3638       value = psymval->value(object, addend);
   3639     else
   3640       {
   3641 	// For negative addends, get the symbol value without
   3642 	// the addend, then add the addend using 64-bit arithmetic.
   3643 	value = psymval->value(object, 0);
   3644 	value += addend;
   3645       }
   3646     value -= address;
   3647     elfcpp::Swap<32, false>::writeval(wv, value);
   3648     return (Bits<32>::has_overflow(value)
   3649 	    ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
   3650   }
   3651 };
   3652 
   3653 // Perform a relocation.
   3654 
   3655 template<int size>
   3656 inline bool
   3657 Target_x86_64<size>::Relocate::relocate(
   3658     const Relocate_info<size, false>* relinfo,
   3659     unsigned int,
   3660     Target_x86_64<size>* target,
   3661     Output_section*,
   3662     size_t relnum,
   3663     const unsigned char* preloc,
   3664     const Sized_symbol<size>* gsym,
   3665     const Symbol_value<size>* psymval,
   3666     unsigned char* view,
   3667     typename elfcpp::Elf_types<size>::Elf_Addr address,
   3668     section_size_type view_size)
   3669 {
   3670   typedef X86_64_relocate_functions<size> Reloc_funcs;
   3671   const elfcpp::Rela<size, false> rela(preloc);
   3672   unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
   3673 
   3674   if (this->skip_call_tls_get_addr_)
   3675     {
   3676       if ((r_type != elfcpp::R_X86_64_PLT32
   3677 	   && r_type != elfcpp::R_X86_64_GOTPCREL
   3678 	   && r_type != elfcpp::R_X86_64_GOTPCRELX
   3679 	   && r_type != elfcpp::R_X86_64_PLT32_BND
   3680 	   && r_type != elfcpp::R_X86_64_PC32_BND
   3681 	   && r_type != elfcpp::R_X86_64_PC32)
   3682 	  || gsym == NULL
   3683 	  || strcmp(gsym->name(), "__tls_get_addr") != 0)
   3684 	{
   3685 	  gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
   3686 				 _("missing expected TLS relocation"));
   3687 	  this->skip_call_tls_get_addr_ = false;
   3688 	}
   3689       else
   3690 	{
   3691 	  this->skip_call_tls_get_addr_ = false;
   3692 	  return false;
   3693 	}
   3694     }
   3695 
   3696   if (view == NULL)
   3697     return true;
   3698 
   3699   const Sized_relobj_file<size, false>* object = relinfo->object;
   3700 
   3701   // Pick the value to use for symbols defined in the PLT.
   3702   Symbol_value<size> symval;
   3703   if (gsym != NULL
   3704       && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
   3705     {
   3706       symval.set_output_value(target->plt_address_for_global(gsym));
   3707       psymval = &symval;
   3708     }
   3709   else if (gsym == NULL && psymval->is_ifunc_symbol())
   3710     {
   3711       unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
   3712       if (object->local_has_plt_offset(r_sym))
   3713 	{
   3714 	  symval.set_output_value(target->plt_address_for_local(object, r_sym));
   3715 	  psymval = &symval;
   3716 	}
   3717     }
   3718 
   3719   const elfcpp::Elf_Xword addend = rela.get_r_addend();
   3720 
   3721   // Get the GOT offset if needed.
   3722   // The GOT pointer points to the end of the GOT section.
   3723   // We need to subtract the size of the GOT section to get
   3724   // the actual offset to use in the relocation.
   3725   bool have_got_offset = false;
   3726   // Since the actual offset is always negative, we use signed int to
   3727   // support 64-bit GOT relocations.
   3728   int got_offset = 0;
   3729   switch (r_type)
   3730     {
   3731     case elfcpp::R_X86_64_GOT32:
   3732     case elfcpp::R_X86_64_GOT64:
   3733     case elfcpp::R_X86_64_GOTPLT64:
   3734     case elfcpp::R_X86_64_GOTPCREL64:
   3735       if (gsym != NULL)
   3736 	{
   3737 	  gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
   3738 	  got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
   3739 	}
   3740       else
   3741 	{
   3742 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
   3743 	  gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
   3744 	  got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
   3745 			- target->got_size());
   3746 	}
   3747       have_got_offset = true;
   3748       break;
   3749 
   3750     default:
   3751       break;
   3752     }
   3753 
   3754   typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
   3755 
   3756   switch (r_type)
   3757     {
   3758     case elfcpp::R_X86_64_NONE:
   3759     case elfcpp::R_X86_64_GNU_VTINHERIT:
   3760     case elfcpp::R_X86_64_GNU_VTENTRY:
   3761       break;
   3762 
   3763     case elfcpp::R_X86_64_64:
   3764       Reloc_funcs::rela64(view, object, psymval, addend);
   3765       break;
   3766 
   3767     case elfcpp::R_X86_64_PC64:
   3768       Reloc_funcs::pcrela64(view, object, psymval, addend,
   3769 					      address);
   3770       break;
   3771 
   3772     case elfcpp::R_X86_64_32:
   3773       rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
   3774 					  Reloc_funcs::CHECK_UNSIGNED);
   3775       break;
   3776 
   3777     case elfcpp::R_X86_64_32S:
   3778       rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
   3779 					  Reloc_funcs::CHECK_SIGNED);
   3780       break;
   3781 
   3782     case elfcpp::R_X86_64_PC32:
   3783     case elfcpp::R_X86_64_PC32_BND:
   3784       rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
   3785 					    address);
   3786       break;
   3787 
   3788     case elfcpp::R_X86_64_16:
   3789       Reloc_funcs::rela16(view, object, psymval, addend);
   3790       break;
   3791 
   3792     case elfcpp::R_X86_64_PC16:
   3793       Reloc_funcs::pcrela16(view, object, psymval, addend, address);
   3794       break;
   3795 
   3796     case elfcpp::R_X86_64_8:
   3797       Reloc_funcs::rela8(view, object, psymval, addend);
   3798       break;
   3799 
   3800     case elfcpp::R_X86_64_PC8:
   3801       Reloc_funcs::pcrela8(view, object, psymval, addend, address);
   3802       break;
   3803 
   3804     case elfcpp::R_X86_64_PLT32:
   3805     case elfcpp::R_X86_64_PLT32_BND:
   3806       gold_assert(gsym == NULL
   3807 		  || gsym->has_plt_offset()
   3808 		  || gsym->final_value_is_known()
   3809 		  || (gsym->is_defined()
   3810 		      && !gsym->is_from_dynobj()
   3811 		      && !gsym->is_preemptible()));
   3812       // Note: while this code looks the same as for R_X86_64_PC32, it
   3813       // behaves differently because psymval was set to point to
   3814       // the PLT entry, rather than the symbol, in Scan::global().
   3815       rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
   3816 					    address);
   3817       break;
   3818 
   3819     case elfcpp::R_X86_64_PLTOFF64:
   3820       {
   3821 	gold_assert(gsym);
   3822 	gold_assert(gsym->has_plt_offset()
   3823 		    || gsym->final_value_is_known());
   3824 	typename elfcpp::Elf_types<size>::Elf_Addr got_address;
   3825 	// This is the address of GLOBAL_OFFSET_TABLE.
   3826 	got_address = target->got_plt_section()->address();
   3827 	Reloc_funcs::rela64(view, object, psymval, addend - got_address);
   3828       }
   3829       break;
   3830 
   3831     case elfcpp::R_X86_64_GOT32:
   3832       gold_assert(have_got_offset);
   3833       Reloc_funcs::rela32(view, got_offset, addend);
   3834       break;
   3835 
   3836     case elfcpp::R_X86_64_GOTPC32:
   3837       {
   3838 	gold_assert(gsym);
   3839 	typename elfcpp::Elf_types<size>::Elf_Addr value;
   3840 	value = target->got_plt_section()->address();
   3841 	Reloc_funcs::pcrela32_check(view, value, addend, address);
   3842       }
   3843       break;
   3844 
   3845     case elfcpp::R_X86_64_GOT64:
   3846     case elfcpp::R_X86_64_GOTPLT64:
   3847       // R_X86_64_GOTPLT64 is obsolete and treated the the same as
   3848       // GOT64.
   3849       gold_assert(have_got_offset);
   3850       Reloc_funcs::rela64(view, got_offset, addend);
   3851       break;
   3852 
   3853     case elfcpp::R_X86_64_GOTPC64:
   3854       {
   3855 	gold_assert(gsym);
   3856 	typename elfcpp::Elf_types<size>::Elf_Addr value;
   3857 	value = target->got_plt_section()->address();
   3858 	Reloc_funcs::pcrela64(view, value, addend, address);
   3859       }
   3860       break;
   3861 
   3862     case elfcpp::R_X86_64_GOTOFF64:
   3863       {
   3864 	typename elfcpp::Elf_types<size>::Elf_Addr value;
   3865 	value = (psymval->value(object, 0)
   3866 		 - target->got_plt_section()->address());
   3867 	Reloc_funcs::rela64(view, value, addend);
   3868       }
   3869       break;
   3870 
   3871     case elfcpp::R_X86_64_GOTPCREL:
   3872     case elfcpp::R_X86_64_GOTPCRELX:
   3873     case elfcpp::R_X86_64_REX_GOTPCRELX:
   3874       {
   3875       // Convert
   3876       // mov foo@GOTPCREL(%rip), %reg
   3877       // to lea foo(%rip), %reg.
   3878       // if possible.
   3879        if ((gsym == NULL
   3880              && rela.get_r_offset() >= 2
   3881              && view[-2] == 0x8b
   3882              && !psymval->is_ifunc_symbol())
   3883             || (gsym != NULL
   3884                 && rela.get_r_offset() >= 2
   3885                 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
   3886                                                                0, &view)))
   3887 	{
   3888 	  view[-2] = 0x8d;
   3889 	  Reloc_funcs::pcrela32(view, object, psymval, addend, address);
   3890 	}
   3891       // Convert
   3892       // callq *foo@GOTPCRELX(%rip) to
   3893       // addr32 callq foo
   3894       // and jmpq *foo@GOTPCRELX(%rip) to
   3895       // jmpq foo
   3896       // nop
   3897       else if (gsym != NULL
   3898 	       && rela.get_r_offset() >= 2
   3899 	       && Target_x86_64<size>::can_convert_callq_to_direct(gsym,
   3900 								   r_type,
   3901 								   0, &view))
   3902 	{
   3903 	  if (view[-1] == 0x15)
   3904 	    {
   3905 	      // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
   3906 	      // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
   3907 	      view[-2] = 0x67;
   3908 	      view[-1] = 0xe8;
   3909 	      // Convert GOTPCRELX to 32-bit pc relative reloc.
   3910 	      Reloc_funcs::pcrela32(view, object, psymval, addend, address);
   3911 	    }
   3912 	  else
   3913 	    {
   3914 	      // Convert jmpq *foo@GOTPCRELX(%rip) to
   3915 	      // jmpq foo
   3916 	      // nop
   3917 	      // The opcode of direct jmpq is 0xe9.
   3918 	      view[-2] = 0xe9;
   3919 	      // The opcode of nop is 0x90.
   3920 	      view[3] = 0x90;
   3921 	      // Convert GOTPCRELX to 32-bit pc relative reloc.  jmpq is rip
   3922 	      // relative and since the instruction following the jmpq is now
   3923 	      // the nop, offset the address by 1 byte.  The start of the
   3924               // relocation also moves ahead by 1 byte.
   3925 	      Reloc_funcs::pcrela32(&view[-1], object, psymval, addend,
   3926 				    address - 1);
   3927 	    }
   3928 	}
   3929       else
   3930 	{
   3931 	  if (gsym != NULL)
   3932 	    {
   3933 	      gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
   3934 	      got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
   3935 	    }
   3936 	  else
   3937 	    {
   3938 	      unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
   3939 	      gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
   3940 	      got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
   3941 			    - target->got_size());
   3942 	    }
   3943 	  typename elfcpp::Elf_types<size>::Elf_Addr value;
   3944 	  value = target->got_plt_section()->address() + got_offset;
   3945 	  Reloc_funcs::pcrela32_check(view, value, addend, address);
   3946 	}
   3947       }
   3948       break;
   3949 
   3950     case elfcpp::R_X86_64_GOTPCREL64:
   3951       {
   3952 	gold_assert(have_got_offset);
   3953 	typename elfcpp::Elf_types<size>::Elf_Addr value;
   3954 	value = target->got_plt_section()->address() + got_offset;
   3955 	Reloc_funcs::pcrela64(view, value, addend, address);
   3956       }
   3957       break;
   3958 
   3959     case elfcpp::R_X86_64_COPY:
   3960     case elfcpp::R_X86_64_GLOB_DAT:
   3961     case elfcpp::R_X86_64_JUMP_SLOT:
   3962     case elfcpp::R_X86_64_RELATIVE:
   3963     case elfcpp::R_X86_64_IRELATIVE:
   3964       // These are outstanding tls relocs, which are unexpected when linking
   3965     case elfcpp::R_X86_64_TPOFF64:
   3966     case elfcpp::R_X86_64_DTPMOD64:
   3967     case elfcpp::R_X86_64_TLSDESC:
   3968       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
   3969 			     _("unexpected reloc %u in object file"),
   3970 			     r_type);
   3971       break;
   3972 
   3973       // These are initial tls relocs, which are expected when linking
   3974     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
   3975     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
   3976     case elfcpp::R_X86_64_TLSDESC_CALL:
   3977     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
   3978     case elfcpp::R_X86_64_DTPOFF32:
   3979     case elfcpp::R_X86_64_DTPOFF64:
   3980     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
   3981     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
   3982       this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
   3983 			 view, address, view_size);
   3984       break;
   3985 
   3986     case elfcpp::R_X86_64_SIZE32:
   3987     case elfcpp::R_X86_64_SIZE64:
   3988     default:
   3989       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
   3990 			     _("unsupported reloc %u"),
   3991 			     r_type);
   3992       break;
   3993     }
   3994 
   3995   if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
   3996     {
   3997       if (gsym == NULL)
   3998         {
   3999 	  unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
   4000 	  gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
   4001 				 _("relocation overflow: "
   4002 				   "reference to local symbol %u in %s"),
   4003 				 r_sym, object->name().c_str());
   4004         }
   4005       else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
   4006         {
   4007 	  gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
   4008 				 _("relocation overflow: "
   4009 				   "reference to '%s' defined in %s"),
   4010 				 gsym->name(),
   4011 				 gsym->object()->name().c_str());
   4012         }
   4013       else
   4014         {
   4015 	  gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
   4016 				 _("relocation overflow: reference to '%s'"),
   4017 				 gsym->name());
   4018         }
   4019     }
   4020 
   4021   return true;
   4022 }
   4023 
   4024 // Perform a TLS relocation.
   4025 
   4026 template<int size>
   4027 inline void
   4028 Target_x86_64<size>::Relocate::relocate_tls(
   4029     const Relocate_info<size, false>* relinfo,
   4030     Target_x86_64<size>* target,
   4031     size_t relnum,
   4032     const elfcpp::Rela<size, false>& rela,
   4033     unsigned int r_type,
   4034     const Sized_symbol<size>* gsym,
   4035     const Symbol_value<size>* psymval,
   4036     unsigned char* view,
   4037     typename elfcpp::Elf_types<size>::Elf_Addr address,
   4038     section_size_type view_size)
   4039 {
   4040   Output_segment* tls_segment = relinfo->layout->tls_segment();
   4041 
   4042   const Sized_relobj_file<size, false>* object = relinfo->object;
   4043   const elfcpp::Elf_Xword addend = rela.get_r_addend();
   4044   elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
   4045   bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
   4046 
   4047   typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
   4048 
   4049   const bool is_final = (gsym == NULL
   4050 			 ? !parameters->options().shared()
   4051 			 : gsym->final_value_is_known());
   4052   tls::Tls_optimization optimized_type
   4053       = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
   4054   switch (r_type)
   4055     {
   4056     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
   4057       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
   4058 	{
   4059 	  // If this code sequence is used in a non-executable section,
   4060 	  // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
   4061 	  // on the assumption that it's being used by itself in a debug
   4062 	  // section.  Therefore, in the unlikely event that the code
   4063 	  // sequence appears in a non-executable section, we simply
   4064 	  // leave it unoptimized.
   4065 	  optimized_type = tls::TLSOPT_NONE;
   4066 	}
   4067       if (optimized_type == tls::TLSOPT_TO_LE)
   4068 	{
   4069 	  if (tls_segment == NULL)
   4070 	    {
   4071 	      gold_assert(parameters->errors()->error_count() > 0
   4072 			  || issue_undefined_symbol_error(gsym));
   4073 	      return;
   4074 	    }
   4075 	  this->tls_gd_to_le(relinfo, relnum, tls_segment,
   4076 			     rela, r_type, value, view,
   4077 			     view_size);
   4078 	  break;
   4079 	}
   4080       else
   4081 	{
   4082 	  unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
   4083 				   ? GOT_TYPE_TLS_OFFSET
   4084 				   : GOT_TYPE_TLS_PAIR);
   4085 	  unsigned int got_offset;
   4086 	  if (gsym != NULL)
   4087 	    {
   4088 	      gold_assert(gsym->has_got_offset(got_type));
   4089 	      got_offset = gsym->got_offset(got_type) - target->got_size();
   4090 	    }
   4091 	  else
   4092 	    {
   4093 	      unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
   4094 	      gold_assert(object->local_has_got_offset(r_sym, got_type));
   4095 	      got_offset = (object->local_got_offset(r_sym, got_type)
   4096 			    - target->got_size());
   4097 	    }
   4098 	  if (optimized_type == tls::TLSOPT_TO_IE)
   4099 	    {
   4100 	      value = target->got_plt_section()->address() + got_offset;
   4101 	      this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
   4102 				 value, view, address, view_size);
   4103 	      break;
   4104 	    }
   4105 	  else if (optimized_type == tls::TLSOPT_NONE)
   4106 	    {
   4107 	      // Relocate the field with the offset of the pair of GOT
   4108 	      // entries.
   4109 	      value = target->got_plt_section()->address() + got_offset;
   4110 	      Relocate_functions<size, false>::pcrela32(view, value, addend,
   4111 							address);
   4112 	      break;
   4113 	    }
   4114 	}
   4115       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
   4116 			     _("unsupported reloc %u"), r_type);
   4117       break;
   4118 
   4119     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
   4120     case elfcpp::R_X86_64_TLSDESC_CALL:
   4121       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
   4122 	{
   4123 	  // See above comment for R_X86_64_TLSGD.
   4124 	  optimized_type = tls::TLSOPT_NONE;
   4125 	}
   4126       if (optimized_type == tls::TLSOPT_TO_LE)
   4127 	{
   4128 	  if (tls_segment == NULL)
   4129 	    {
   4130 	      gold_assert(parameters->errors()->error_count() > 0
   4131 			  || issue_undefined_symbol_error(gsym));
   4132 	      return;
   4133 	    }
   4134 	  this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
   4135 				  rela, r_type, value, view,
   4136 				  view_size);
   4137 	  break;
   4138 	}
   4139       else
   4140 	{
   4141 	  unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
   4142 				   ? GOT_TYPE_TLS_OFFSET
   4143 				   : GOT_TYPE_TLS_DESC);
   4144 	  unsigned int got_offset = 0;
   4145 	  if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
   4146 	      && optimized_type == tls::TLSOPT_NONE)
   4147 	    {
   4148 	      // We created GOT entries in the .got.tlsdesc portion of
   4149 	      // the .got.plt section, but the offset stored in the
   4150 	      // symbol is the offset within .got.tlsdesc.
   4151 	      got_offset = (target->got_size()
   4152 			    + target->got_plt_section()->data_size());
   4153 	    }
   4154 	  if (gsym != NULL)
   4155 	    {
   4156 	      gold_assert(gsym->has_got_offset(got_type));
   4157 	      got_offset += gsym->got_offset(got_type) - target->got_size();
   4158 	    }
   4159 	  else
   4160 	    {
   4161 	      unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
   4162 	      gold_assert(object->local_has_got_offset(r_sym, got_type));
   4163 	      got_offset += (object->local_got_offset(r_sym, got_type)
   4164 			     - target->got_size());
   4165 	    }
   4166 	  if (optimized_type == tls::TLSOPT_TO_IE)
   4167 	    {
   4168 	      value = target->got_plt_section()->address() + got_offset;
   4169 	      this->tls_desc_gd_to_ie(relinfo, relnum,
   4170 				      rela, r_type, value, view, address,
   4171 				      view_size);
   4172 	      break;
   4173 	    }
   4174 	  else if (optimized_type == tls::TLSOPT_NONE)
   4175 	    {
   4176 	      if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
   4177 		{
   4178 		  // Relocate the field with the offset of the pair of GOT
   4179 		  // entries.
   4180 		  value = target->got_plt_section()->address() + got_offset;
   4181 		  Relocate_functions<size, false>::pcrela32(view, value, addend,
   4182 							    address);
   4183 		}
   4184 	      break;
   4185 	    }
   4186 	}
   4187       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
   4188 			     _("unsupported reloc %u"), r_type);
   4189       break;
   4190 
   4191     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
   4192       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
   4193 	{
   4194 	  // See above comment for R_X86_64_TLSGD.
   4195 	  optimized_type = tls::TLSOPT_NONE;
   4196 	}
   4197       if (optimized_type == tls::TLSOPT_TO_LE)
   4198 	{
   4199 	  if (tls_segment == NULL)
   4200 	    {
   4201 	      gold_assert(parameters->errors()->error_count() > 0
   4202 			  || issue_undefined_symbol_error(gsym));
   4203 	      return;
   4204 	    }
   4205 	  this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
   4206 			     value, view, view_size);
   4207 	  break;
   4208 	}
   4209       else if (optimized_type == tls::TLSOPT_NONE)
   4210 	{
   4211 	  // Relocate the field with the offset of the GOT entry for
   4212 	  // the module index.
   4213 	  unsigned int got_offset;
   4214 	  got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
   4215 			- target->got_size());
   4216 	  value = target->got_plt_section()->address() + got_offset;
   4217 	  Relocate_functions<size, false>::pcrela32(view, value, addend,
   4218 						    address);
   4219 	  break;
   4220 	}
   4221       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
   4222 			     _("unsupported reloc %u"), r_type);
   4223       break;
   4224 
   4225     case elfcpp::R_X86_64_DTPOFF32:
   4226       // This relocation type is used in debugging information.
   4227       // In that case we need to not optimize the value.  If the
   4228       // section is not executable, then we assume we should not
   4229       // optimize this reloc.  See comments above for R_X86_64_TLSGD,
   4230       // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
   4231       // R_X86_64_TLSLD.
   4232       if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
   4233 	{
   4234 	  if (tls_segment == NULL)
   4235 	    {
   4236 	      gold_assert(parameters->errors()->error_count() > 0
   4237 			  || issue_undefined_symbol_error(gsym));
   4238 	      return;
   4239 	    }
   4240 	  value -= tls_segment->memsz();
   4241 	}
   4242       Relocate_functions<size, false>::rela32(view, value, addend);
   4243       break;
   4244 
   4245     case elfcpp::R_X86_64_DTPOFF64:
   4246       // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
   4247       if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
   4248 	{
   4249 	  if (tls_segment == NULL)
   4250 	    {
   4251 	      gold_assert(parameters->errors()->error_count() > 0
   4252 			  || issue_undefined_symbol_error(gsym));
   4253 	      return;
   4254 	    }
   4255 	  value -= tls_segment->memsz();
   4256 	}
   4257       Relocate_functions<size, false>::rela64(view, value, addend);
   4258       break;
   4259 
   4260     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
   4261       if (gsym != NULL
   4262 	  && gsym->is_undefined()
   4263 	  && parameters->options().output_is_executable())
   4264 	{
   4265 	  Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
   4266 						      NULL, rela,
   4267 						      r_type, value, view,
   4268 						      view_size);
   4269 	  break;
   4270 	}
   4271       else if (optimized_type == tls::TLSOPT_TO_LE)
   4272 	{
   4273 	  if (tls_segment == NULL)
   4274 	    {
   4275 	      gold_assert(parameters->errors()->error_count() > 0
   4276 			  || issue_undefined_symbol_error(gsym));
   4277 	      return;
   4278 	    }
   4279 	  Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
   4280 						      tls_segment, rela,
   4281 						      r_type, value, view,
   4282 						      view_size);
   4283 	  break;
   4284 	}
   4285       else if (optimized_type == tls::TLSOPT_NONE)
   4286 	{
   4287 	  // Relocate the field with the offset of the GOT entry for
   4288 	  // the tp-relative offset of the symbol.
   4289 	  unsigned int got_offset;
   4290 	  if (gsym != NULL)
   4291 	    {
   4292 	      gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
   4293 	      got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
   4294 			    - target->got_size());
   4295 	    }
   4296 	  else
   4297 	    {
   4298 	      unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
   4299 	      gold_assert(object->local_has_got_offset(r_sym,
   4300 						       GOT_TYPE_TLS_OFFSET));
   4301 	      got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
   4302 			    - target->got_size());
   4303 	    }
   4304 	  value = target->got_plt_section()->address() + got_offset;
   4305 	  Relocate_functions<size, false>::pcrela32(view, value, addend,
   4306 						    address);
   4307 	  break;
   4308 	}
   4309       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
   4310 			     _("unsupported reloc type %u"),
   4311 			     r_type);
   4312       break;
   4313 
   4314     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
   4315       if (tls_segment == NULL)
   4316 	{
   4317 	  gold_assert(parameters->errors()->error_count() > 0
   4318 		      || issue_undefined_symbol_error(gsym));
   4319 	  return;
   4320 	}
   4321       value -= tls_segment->memsz();
   4322       Relocate_functions<size, false>::rela32(view, value, addend);
   4323       break;
   4324     }
   4325 }
   4326 
   4327 // Do a relocation in which we convert a TLS General-Dynamic to an
   4328 // Initial-Exec.
   4329 
   4330 template<int size>
   4331 inline void
   4332 Target_x86_64<size>::Relocate::tls_gd_to_ie(
   4333     const Relocate_info<size, false>* relinfo,
   4334     size_t relnum,
   4335     const elfcpp::Rela<size, false>& rela,
   4336     unsigned int,
   4337     typename elfcpp::Elf_types<size>::Elf_Addr value,
   4338     unsigned char* view,
   4339     typename elfcpp::Elf_types<size>::Elf_Addr address,
   4340     section_size_type view_size)
   4341 {
   4342   // For SIZE == 64:
   4343   //	.byte 0x66; leaq foo@tlsgd(%rip),%rdi;
   4344   //	.word 0x6666; rex64; call __tls_get_addr@PLT
   4345   //	==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
   4346   //	.byte 0x66; leaq foo@tlsgd(%rip),%rdi;
   4347   //	.word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
   4348   //	==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
   4349   // For SIZE == 32:
   4350   //	leaq foo@tlsgd(%rip),%rdi;
   4351   //	.word 0x6666; rex64; call __tls_get_addr@PLT
   4352   //	==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
   4353   //	leaq foo@tlsgd(%rip),%rdi;
   4354   //	.word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
   4355   //	==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
   4356 
   4357   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
   4358   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4359 		 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
   4360 		  || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
   4361 
   4362   if (size == 64)
   4363     {
   4364       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
   4365 		       -4);
   4366       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4367 		     (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
   4368       memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
   4369 	     16);
   4370     }
   4371   else
   4372     {
   4373       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
   4374 		       -3);
   4375       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4376 		     (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
   4377       memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
   4378 	     15);
   4379     }
   4380 
   4381   const elfcpp::Elf_Xword addend = rela.get_r_addend();
   4382   Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
   4383 					    address);
   4384 
   4385   // The next reloc should be a PLT32 reloc against __tls_get_addr.
   4386   // We can skip it.
   4387   this->skip_call_tls_get_addr_ = true;
   4388 }
   4389 
   4390 // Do a relocation in which we convert a TLS General-Dynamic to a
   4391 // Local-Exec.
   4392 
   4393 template<int size>
   4394 inline void
   4395 Target_x86_64<size>::Relocate::tls_gd_to_le(
   4396     const Relocate_info<size, false>* relinfo,
   4397     size_t relnum,
   4398     Output_segment* tls_segment,
   4399     const elfcpp::Rela<size, false>& rela,
   4400     unsigned int,
   4401     typename elfcpp::Elf_types<size>::Elf_Addr value,
   4402     unsigned char* view,
   4403     section_size_type view_size)
   4404 {
   4405   // For SIZE == 64:
   4406   //	.byte 0x66; leaq foo@tlsgd(%rip),%rdi;
   4407   //	.word 0x6666; rex64; call __tls_get_addr@PLT
   4408   //	==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
   4409   //	.byte 0x66; leaq foo@tlsgd(%rip),%rdi;
   4410   //	.word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
   4411   //	==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
   4412   // For SIZE == 32:
   4413   //	leaq foo@tlsgd(%rip),%rdi;
   4414   //	.word 0x6666; rex64; call __tls_get_addr@PLT
   4415   //	==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
   4416   //	leaq foo@tlsgd(%rip),%rdi;
   4417   //	.word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
   4418   //	==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
   4419 
   4420   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
   4421   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4422 		 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
   4423 		  || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
   4424 
   4425   if (size == 64)
   4426     {
   4427       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
   4428 		       -4);
   4429       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4430 		     (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
   4431       memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
   4432 	     16);
   4433     }
   4434   else
   4435     {
   4436       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
   4437 		       -3);
   4438       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4439 		     (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
   4440 
   4441       memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
   4442 	     15);
   4443     }
   4444 
   4445   value -= tls_segment->memsz();
   4446   Relocate_functions<size, false>::rela32(view + 8, value, 0);
   4447 
   4448   // The next reloc should be a PLT32 reloc against __tls_get_addr.
   4449   // We can skip it.
   4450   this->skip_call_tls_get_addr_ = true;
   4451 }
   4452 
   4453 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
   4454 
   4455 template<int size>
   4456 inline void
   4457 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
   4458     const Relocate_info<size, false>* relinfo,
   4459     size_t relnum,
   4460     const elfcpp::Rela<size, false>& rela,
   4461     unsigned int r_type,
   4462     typename elfcpp::Elf_types<size>::Elf_Addr value,
   4463     unsigned char* view,
   4464     typename elfcpp::Elf_types<size>::Elf_Addr address,
   4465     section_size_type view_size)
   4466 {
   4467   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
   4468     {
   4469       // leaq foo@tlsdesc(%rip), %rax
   4470       // ==> movq foo@gottpoff(%rip), %rax
   4471       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
   4472       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
   4473       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4474 		     view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
   4475       view[-2] = 0x8b;
   4476       const elfcpp::Elf_Xword addend = rela.get_r_addend();
   4477       Relocate_functions<size, false>::pcrela32(view, value, addend, address);
   4478     }
   4479   else
   4480     {
   4481       // call *foo@tlscall(%rax)
   4482       // ==> nop; nop
   4483       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
   4484       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
   4485       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4486 		     view[0] == 0xff && view[1] == 0x10);
   4487       view[0] = 0x66;
   4488       view[1] = 0x90;
   4489     }
   4490 }
   4491 
   4492 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
   4493 
   4494 template<int size>
   4495 inline void
   4496 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
   4497     const Relocate_info<size, false>* relinfo,
   4498     size_t relnum,
   4499     Output_segment* tls_segment,
   4500     const elfcpp::Rela<size, false>& rela,
   4501     unsigned int r_type,
   4502     typename elfcpp::Elf_types<size>::Elf_Addr value,
   4503     unsigned char* view,
   4504     section_size_type view_size)
   4505 {
   4506   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
   4507     {
   4508       // leaq foo@tlsdesc(%rip), %rax
   4509       // ==> movq foo@tpoff, %rax
   4510       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
   4511       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
   4512       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4513 		     view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
   4514       view[-2] = 0xc7;
   4515       view[-1] = 0xc0;
   4516       value -= tls_segment->memsz();
   4517       Relocate_functions<size, false>::rela32(view, value, 0);
   4518     }
   4519   else
   4520     {
   4521       // call *foo@tlscall(%rax)
   4522       // ==> nop; nop
   4523       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
   4524       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
   4525       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4526 		     view[0] == 0xff && view[1] == 0x10);
   4527       view[0] = 0x66;
   4528       view[1] = 0x90;
   4529     }
   4530 }
   4531 
   4532 template<int size>
   4533 inline void
   4534 Target_x86_64<size>::Relocate::tls_ld_to_le(
   4535     const Relocate_info<size, false>* relinfo,
   4536     size_t relnum,
   4537     Output_segment*,
   4538     const elfcpp::Rela<size, false>& rela,
   4539     unsigned int,
   4540     typename elfcpp::Elf_types<size>::Elf_Addr,
   4541     unsigned char* view,
   4542     section_size_type view_size)
   4543 {
   4544   // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
   4545   // For SIZE == 64:
   4546   // ... leq foo@dtpoff(%rax),%reg
   4547   // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
   4548   // For SIZE == 32:
   4549   // ... leq foo@dtpoff(%rax),%reg
   4550   // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
   4551   // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
   4552   // For SIZE == 64:
   4553   // ... leq foo@dtpoff(%rax),%reg
   4554   // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
   4555   // For SIZE == 32:
   4556   // ... leq foo@dtpoff(%rax),%reg
   4557   // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
   4558 
   4559   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
   4560   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
   4561 
   4562   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4563 		 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
   4564 
   4565   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
   4566 		 view[4] == 0xe8 || view[4] == 0xff);
   4567 
   4568   if (view[4] == 0xe8)
   4569     {
   4570       if (size == 64)
   4571 	memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
   4572       else
   4573 	memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
   4574     }
   4575   else
   4576     {
   4577       if (size == 64)
   4578 	memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
   4579 	       13);
   4580       else
   4581 	memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
   4582 	       13);
   4583     }
   4584 
   4585   // The next reloc should be a PLT32 reloc against __tls_get_addr.
   4586   // We can skip it.
   4587   this->skip_call_tls_get_addr_ = true;
   4588 }
   4589 
   4590 // Do a relocation in which we convert a TLS Initial-Exec to a
   4591 // Local-Exec.
   4592 
   4593 template<int size>
   4594 inline void
   4595 Target_x86_64<size>::Relocate::tls_ie_to_le(
   4596     const Relocate_info<size, false>* relinfo,
   4597     size_t relnum,
   4598     Output_segment* tls_segment,
   4599     const elfcpp::Rela<size, false>& rela,
   4600     unsigned int,
   4601     typename elfcpp::Elf_types<size>::Elf_Addr value,
   4602     unsigned char* view,
   4603     section_size_type view_size)
   4604 {
   4605   // We need to examine the opcodes to figure out which instruction we
   4606   // are looking at.
   4607 
   4608   // movq foo@gottpoff(%rip),%reg  ==>  movq $YY,%reg
   4609   // addq foo@gottpoff(%rip),%reg  ==>  addq $YY,%reg
   4610 
   4611   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
   4612   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
   4613 
   4614   unsigned char op1 = view[-3];
   4615   unsigned char op2 = view[-2];
   4616   unsigned char op3 = view[-1];
   4617   unsigned char reg = op3 >> 3;
   4618 
   4619   if (op2 == 0x8b)
   4620     {
   4621       // movq
   4622       if (op1 == 0x4c)
   4623 	view[-3] = 0x49;
   4624       else if (size == 32 && op1 == 0x44)
   4625 	view[-3] = 0x41;
   4626       view[-2] = 0xc7;
   4627       view[-1] = 0xc0 | reg;
   4628     }
   4629   else if (reg == 4)
   4630     {
   4631       // Special handling for %rsp.
   4632       if (op1 == 0x4c)
   4633 	view[-3] = 0x49;
   4634       else if (size == 32 && op1 == 0x44)
   4635 	view[-3] = 0x41;
   4636       view[-2] = 0x81;
   4637       view[-1] = 0xc0 | reg;
   4638     }
   4639   else
   4640     {
   4641       // addq
   4642       if (op1 == 0x4c)
   4643 	view[-3] = 0x4d;
   4644       else if (size == 32 && op1 == 0x44)
   4645 	view[-3] = 0x45;
   4646       view[-2] = 0x8d;
   4647       view[-1] = 0x80 | reg | (reg << 3);
   4648     }
   4649 
   4650   if (tls_segment != NULL)
   4651     value -= tls_segment->memsz();
   4652   Relocate_functions<size, false>::rela32(view, value, 0);
   4653 }
   4654 
   4655 // Relocate section data.
   4656 
   4657 template<int size>
   4658 void
   4659 Target_x86_64<size>::relocate_section(
   4660     const Relocate_info<size, false>* relinfo,
   4661     unsigned int sh_type,
   4662     const unsigned char* prelocs,
   4663     size_t reloc_count,
   4664     Output_section* output_section,
   4665     bool needs_special_offset_handling,
   4666     unsigned char* view,
   4667     typename elfcpp::Elf_types<size>::Elf_Addr address,
   4668     section_size_type view_size,
   4669     const Reloc_symbol_changes* reloc_symbol_changes)
   4670 {
   4671   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
   4672       Classify_reloc;
   4673 
   4674   gold_assert(sh_type == elfcpp::SHT_RELA);
   4675 
   4676   gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
   4677 			 gold::Default_comdat_behavior, Classify_reloc>(
   4678     relinfo,
   4679     this,
   4680     prelocs,
   4681     reloc_count,
   4682     output_section,
   4683     needs_special_offset_handling,
   4684     view,
   4685     address,
   4686     view_size,
   4687     reloc_symbol_changes);
   4688 }
   4689 
   4690 // Apply an incremental relocation.  Incremental relocations always refer
   4691 // to global symbols.
   4692 
   4693 template<int size>
   4694 void
   4695 Target_x86_64<size>::apply_relocation(
   4696     const Relocate_info<size, false>* relinfo,
   4697     typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
   4698     unsigned int r_type,
   4699     typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
   4700     const Symbol* gsym,
   4701     unsigned char* view,
   4702     typename elfcpp::Elf_types<size>::Elf_Addr address,
   4703     section_size_type view_size)
   4704 {
   4705   gold::apply_relocation<size, false, Target_x86_64<size>,
   4706 			 typename Target_x86_64<size>::Relocate>(
   4707     relinfo,
   4708     this,
   4709     r_offset,
   4710     r_type,
   4711     r_addend,
   4712     gsym,
   4713     view,
   4714     address,
   4715     view_size);
   4716 }
   4717 
   4718 // Scan the relocs during a relocatable link.
   4719 
   4720 template<int size>
   4721 void
   4722 Target_x86_64<size>::scan_relocatable_relocs(
   4723     Symbol_table* symtab,
   4724     Layout* layout,
   4725     Sized_relobj_file<size, false>* object,
   4726     unsigned int data_shndx,
   4727     unsigned int sh_type,
   4728     const unsigned char* prelocs,
   4729     size_t reloc_count,
   4730     Output_section* output_section,
   4731     bool needs_special_offset_handling,
   4732     size_t local_symbol_count,
   4733     const unsigned char* plocal_symbols,
   4734     Relocatable_relocs* rr)
   4735 {
   4736   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
   4737       Classify_reloc;
   4738   typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
   4739       Scan_relocatable_relocs;
   4740 
   4741   gold_assert(sh_type == elfcpp::SHT_RELA);
   4742 
   4743   gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
   4744     symtab,
   4745     layout,
   4746     object,
   4747     data_shndx,
   4748     prelocs,
   4749     reloc_count,
   4750     output_section,
   4751     needs_special_offset_handling,
   4752     local_symbol_count,
   4753     plocal_symbols,
   4754     rr);
   4755 }
   4756 
   4757 // Scan the relocs for --emit-relocs.
   4758 
   4759 template<int size>
   4760 void
   4761 Target_x86_64<size>::emit_relocs_scan(
   4762     Symbol_table* symtab,
   4763     Layout* layout,
   4764     Sized_relobj_file<size, false>* object,
   4765     unsigned int data_shndx,
   4766     unsigned int sh_type,
   4767     const unsigned char* prelocs,
   4768     size_t reloc_count,
   4769     Output_section* output_section,
   4770     bool needs_special_offset_handling,
   4771     size_t local_symbol_count,
   4772     const unsigned char* plocal_syms,
   4773     Relocatable_relocs* rr)
   4774 {
   4775   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
   4776       Classify_reloc;
   4777   typedef gold::Default_emit_relocs_strategy<Classify_reloc>
   4778       Emit_relocs_strategy;
   4779 
   4780   gold_assert(sh_type == elfcpp::SHT_RELA);
   4781 
   4782   gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
   4783     symtab,
   4784     layout,
   4785     object,
   4786     data_shndx,
   4787     prelocs,
   4788     reloc_count,
   4789     output_section,
   4790     needs_special_offset_handling,
   4791     local_symbol_count,
   4792     plocal_syms,
   4793     rr);
   4794 }
   4795 
   4796 // Relocate a section during a relocatable link.
   4797 
   4798 template<int size>
   4799 void
   4800 Target_x86_64<size>::relocate_relocs(
   4801     const Relocate_info<size, false>* relinfo,
   4802     unsigned int sh_type,
   4803     const unsigned char* prelocs,
   4804     size_t reloc_count,
   4805     Output_section* output_section,
   4806     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
   4807     unsigned char* view,
   4808     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
   4809     section_size_type view_size,
   4810     unsigned char* reloc_view,
   4811     section_size_type reloc_view_size)
   4812 {
   4813   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
   4814       Classify_reloc;
   4815 
   4816   gold_assert(sh_type == elfcpp::SHT_RELA);
   4817 
   4818   gold::relocate_relocs<size, false, Classify_reloc>(
   4819     relinfo,
   4820     prelocs,
   4821     reloc_count,
   4822     output_section,
   4823     offset_in_output_section,
   4824     view,
   4825     view_address,
   4826     view_size,
   4827     reloc_view,
   4828     reloc_view_size);
   4829 }
   4830 
   4831 // Return the value to use for a dynamic which requires special
   4832 // treatment.  This is how we support equality comparisons of function
   4833 // pointers across shared library boundaries, as described in the
   4834 // processor specific ABI supplement.
   4835 
   4836 template<int size>
   4837 uint64_t
   4838 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
   4839 {
   4840   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
   4841   return this->plt_address_for_global(gsym);
   4842 }
   4843 
   4844 // Return a string used to fill a code section with nops to take up
   4845 // the specified length.
   4846 
   4847 template<int size>
   4848 std::string
   4849 Target_x86_64<size>::do_code_fill(section_size_type length) const
   4850 {
   4851   if (length >= 16)
   4852     {
   4853       // Build a jmpq instruction to skip over the bytes.
   4854       unsigned char jmp[5];
   4855       jmp[0] = 0xe9;
   4856       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
   4857       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
   4858 	      + std::string(length - 5, static_cast<char>(0x90)));
   4859     }
   4860 
   4861   // Nop sequences of various lengths.
   4862   const char nop1[1] = { '\x90' };                 // nop
   4863   const char nop2[2] = { '\x66', '\x90' };         // xchg %ax %ax
   4864   const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
   4865   const char nop4[4] = { '\x0f', '\x1f', '\x40',   // nop 0(%rax)
   4866 			 '\x00'};
   4867   const char nop5[5] = { '\x0f', '\x1f', '\x44',   // nop 0(%rax,%rax,1)
   4868 			 '\x00', '\x00' };
   4869   const char nop6[6] = { '\x66', '\x0f', '\x1f',   // nopw 0(%rax,%rax,1)
   4870 			 '\x44', '\x00', '\x00' };
   4871   const char nop7[7] = { '\x0f', '\x1f', '\x80',   // nopl 0L(%rax)
   4872 			 '\x00', '\x00', '\x00',
   4873 			 '\x00' };
   4874   const char nop8[8] = { '\x0f', '\x1f', '\x84',   // nopl 0L(%rax,%rax,1)
   4875 			 '\x00', '\x00', '\x00',
   4876 			 '\x00', '\x00' };
   4877   const char nop9[9] = { '\x66', '\x0f', '\x1f',   // nopw 0L(%rax,%rax,1)
   4878 			 '\x84', '\x00', '\x00',
   4879 			 '\x00', '\x00', '\x00' };
   4880   const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
   4881 			   '\x1f', '\x84', '\x00',
   4882 			   '\x00', '\x00', '\x00',
   4883 			   '\x00' };
   4884   const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
   4885 			   '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
   4886 			   '\x00', '\x00', '\x00',
   4887 			   '\x00', '\x00' };
   4888   const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
   4889 			   '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
   4890 			   '\x84', '\x00', '\x00',
   4891 			   '\x00', '\x00', '\x00' };
   4892   const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
   4893 			   '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
   4894 			   '\x1f', '\x84', '\x00',
   4895 			   '\x00', '\x00', '\x00',
   4896 			   '\x00' };
   4897   const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
   4898 			   '\x66', '\x66', '\x2e', // data16
   4899 			   '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
   4900 			   '\x00', '\x00', '\x00',
   4901 			   '\x00', '\x00' };
   4902   const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
   4903 			   '\x66', '\x66', '\x66', // data16; data16
   4904 			   '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
   4905 			   '\x84', '\x00', '\x00',
   4906 			   '\x00', '\x00', '\x00' };
   4907 
   4908   const char* nops[16] = {
   4909     NULL,
   4910     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
   4911     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
   4912   };
   4913 
   4914   return std::string(nops[length], length);
   4915 }
   4916 
   4917 // Return the addend to use for a target specific relocation.  The
   4918 // only target specific relocation is R_X86_64_TLSDESC for a local
   4919 // symbol.  We want to set the addend is the offset of the local
   4920 // symbol in the TLS segment.
   4921 
   4922 template<int size>
   4923 uint64_t
   4924 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
   4925 				     uint64_t) const
   4926 {
   4927   gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
   4928   uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
   4929   gold_assert(intarg < this->tlsdesc_reloc_info_.size());
   4930   const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
   4931   const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
   4932   gold_assert(psymval->is_tls_symbol());
   4933   // The value of a TLS symbol is the offset in the TLS segment.
   4934   return psymval->value(ti.object, 0);
   4935 }
   4936 
   4937 // Return the value to use for the base of a DW_EH_PE_datarel offset
   4938 // in an FDE.  Solaris and SVR4 use DW_EH_PE_datarel because their
   4939 // assembler can not write out the difference between two labels in
   4940 // different sections, so instead of using a pc-relative value they
   4941 // use an offset from the GOT.
   4942 
   4943 template<int size>
   4944 uint64_t
   4945 Target_x86_64<size>::do_ehframe_datarel_base() const
   4946 {
   4947   gold_assert(this->global_offset_table_ != NULL);
   4948   Symbol* sym = this->global_offset_table_;
   4949   Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
   4950   return ssym->value();
   4951 }
   4952 
   4953 // FNOFFSET in section SHNDX in OBJECT is the start of a function
   4954 // compiled with -fsplit-stack.  The function calls non-split-stack
   4955 // code.  We have to change the function so that it always ensures
   4956 // that it has enough stack space to run some random function.
   4957 
   4958 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
   4959 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
   4960 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
   4961 
   4962 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
   4963 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
   4964 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
   4965 
   4966 template<int size>
   4967 void
   4968 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
   4969 					section_offset_type fnoffset,
   4970 					section_size_type fnsize,
   4971 					const unsigned char*,
   4972 					size_t,
   4973 					unsigned char* view,
   4974 					section_size_type view_size,
   4975 					std::string* from,
   4976 					std::string* to) const
   4977 {
   4978   const char* const cmp_insn = reinterpret_cast<const char*>
   4979       (size == 32 ? cmp_insn_32 : cmp_insn_64);
   4980   const char* const lea_r10_insn = reinterpret_cast<const char*>
   4981       (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
   4982   const char* const lea_r11_insn = reinterpret_cast<const char*>
   4983       (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
   4984 
   4985   const size_t cmp_insn_len =
   4986       (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
   4987   const size_t lea_r10_insn_len =
   4988       (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
   4989   const size_t lea_r11_insn_len =
   4990       (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
   4991   const size_t nop_len = (size == 32 ? 7 : 8);
   4992 
   4993   // The function starts with a comparison of the stack pointer and a
   4994   // field in the TCB.  This is followed by a jump.
   4995 
   4996   // cmp %fs:NN,%rsp
   4997   if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
   4998       && fnsize > nop_len + 1)
   4999     {
   5000       // We will call __morestack if the carry flag is set after this
   5001       // comparison.  We turn the comparison into an stc instruction
   5002       // and some nops.
   5003       view[fnoffset] = '\xf9';
   5004       this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
   5005     }
   5006   // lea NN(%rsp),%r10
   5007   // lea NN(%rsp),%r11
   5008   else if ((this->match_view(view, view_size, fnoffset,
   5009 			     lea_r10_insn, lea_r10_insn_len)
   5010 	    || this->match_view(view, view_size, fnoffset,
   5011 				lea_r11_insn, lea_r11_insn_len))
   5012 	   && fnsize > 8)
   5013     {
   5014       // This is loading an offset from the stack pointer for a
   5015       // comparison.  The offset is negative, so we decrease the
   5016       // offset by the amount of space we need for the stack.  This
   5017       // means we will avoid calling __morestack if there happens to
   5018       // be plenty of space on the stack already.
   5019       unsigned char* pval = view + fnoffset + 4;
   5020       uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
   5021       val -= parameters->options().split_stack_adjust_size();
   5022       elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
   5023     }
   5024   else
   5025     {
   5026       if (!object->has_no_split_stack())
   5027 	object->error(_("failed to match split-stack sequence at "
   5028 			"section %u offset %0zx"),
   5029 		      shndx, static_cast<size_t>(fnoffset));
   5030       return;
   5031     }
   5032 
   5033   // We have to change the function so that it calls
   5034   // __morestack_non_split instead of __morestack.  The former will
   5035   // allocate additional stack space.
   5036   *from = "__morestack";
   5037   *to = "__morestack_non_split";
   5038 }
   5039 
   5040 // The selector for x86_64 object files.  Note this is never instantiated
   5041 // directly.  It's only used in Target_selector_x86_64_nacl, below.
   5042 
   5043 template<int size>
   5044 class Target_selector_x86_64 : public Target_selector_freebsd
   5045 {
   5046 public:
   5047   Target_selector_x86_64()
   5048     : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
   5049 			      (size == 64
   5050 			       ? "elf64-x86-64" : "elf32-x86-64"),
   5051 			      (size == 64
   5052 			       ? "elf64-x86-64-freebsd"
   5053 			       : "elf32-x86-64-freebsd"),
   5054 			      (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
   5055   { }
   5056 
   5057   Target*
   5058   do_instantiate_target()
   5059   { return new Target_x86_64<size>(); }
   5060 
   5061 };
   5062 
   5063 // NaCl variant.  It uses different PLT contents.
   5064 
   5065 template<int size>
   5066 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
   5067 {
   5068  public:
   5069   Output_data_plt_x86_64_nacl(Layout* layout,
   5070 			      Output_data_got<64, false>* got,
   5071 			      Output_data_got_plt_x86_64* got_plt,
   5072 			      Output_data_space* got_irelative)
   5073     : Output_data_plt_x86_64<size>(layout, plt_entry_size,
   5074 				   got, got_plt, got_irelative)
   5075   { }
   5076 
   5077   Output_data_plt_x86_64_nacl(Layout* layout,
   5078 			      Output_data_got<64, false>* got,
   5079 			      Output_data_got_plt_x86_64* got_plt,
   5080 			      Output_data_space* got_irelative,
   5081 			      unsigned int plt_count)
   5082     : Output_data_plt_x86_64<size>(layout, plt_entry_size,
   5083 				   got, got_plt, got_irelative,
   5084 				   plt_count)
   5085   { }
   5086 
   5087  protected:
   5088   virtual unsigned int
   5089   do_get_plt_entry_size() const
   5090   { return plt_entry_size; }
   5091 
   5092   virtual void
   5093   do_add_eh_frame(Layout* layout)
   5094   {
   5095     layout->add_eh_frame_for_plt(this,
   5096 				 this->plt_eh_frame_cie,
   5097 				 this->plt_eh_frame_cie_size,
   5098 				 plt_eh_frame_fde,
   5099 				 plt_eh_frame_fde_size);
   5100   }
   5101 
   5102   virtual void
   5103   do_fill_first_plt_entry(unsigned char* pov,
   5104 			  typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
   5105 			  typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
   5106 
   5107   virtual unsigned int
   5108   do_fill_plt_entry(unsigned char* pov,
   5109 		    typename elfcpp::Elf_types<size>::Elf_Addr got_address,
   5110 		    typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
   5111 		    unsigned int got_offset,
   5112 		    unsigned int plt_offset,
   5113 		    unsigned int plt_index);
   5114 
   5115   virtual void
   5116   do_fill_tlsdesc_entry(unsigned char* pov,
   5117 			typename elfcpp::Elf_types<size>::Elf_Addr got_address,
   5118 			typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
   5119 			typename elfcpp::Elf_types<size>::Elf_Addr got_base,
   5120 			unsigned int tlsdesc_got_offset,
   5121 			unsigned int plt_offset);
   5122 
   5123  private:
   5124   // The size of an entry in the PLT.
   5125   static const int plt_entry_size = 64;
   5126 
   5127   // The first entry in the PLT.
   5128   static const unsigned char first_plt_entry[plt_entry_size];
   5129 
   5130   // Other entries in the PLT for an executable.
   5131   static const unsigned char plt_entry[plt_entry_size];
   5132 
   5133   // The reserved TLSDESC entry in the PLT for an executable.
   5134   static const unsigned char tlsdesc_plt_entry[plt_entry_size];
   5135 
   5136   // The .eh_frame unwind information for the PLT.
   5137   static const int plt_eh_frame_fde_size = 32;
   5138   static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
   5139 };
   5140 
   5141 template<int size>
   5142 class Target_x86_64_nacl : public Target_x86_64<size>
   5143 {
   5144  public:
   5145   Target_x86_64_nacl()
   5146     : Target_x86_64<size>(&x86_64_nacl_info)
   5147   { }
   5148 
   5149   virtual Output_data_plt_x86_64<size>*
   5150   do_make_data_plt(Layout* layout,
   5151 		   Output_data_got<64, false>* got,
   5152 		   Output_data_got_plt_x86_64* got_plt,
   5153 		   Output_data_space* got_irelative)
   5154   {
   5155     return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
   5156 						 got_irelative);
   5157   }
   5158 
   5159   virtual Output_data_plt_x86_64<size>*
   5160   do_make_data_plt(Layout* layout,
   5161 		   Output_data_got<64, false>* got,
   5162 		   Output_data_got_plt_x86_64* got_plt,
   5163 		   Output_data_space* got_irelative,
   5164 		   unsigned int plt_count)
   5165   {
   5166     return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
   5167 						 got_irelative,
   5168 						 plt_count);
   5169   }
   5170 
   5171   virtual std::string
   5172   do_code_fill(section_size_type length) const;
   5173 
   5174  private:
   5175   static const Target::Target_info x86_64_nacl_info;
   5176 };
   5177 
   5178 template<>
   5179 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
   5180 {
   5181   64,			// size
   5182   false,		// is_big_endian
   5183   elfcpp::EM_X86_64,	// machine_code
   5184   false,		// has_make_symbol
   5185   false,		// has_resolve
   5186   true,			// has_code_fill
   5187   true,			// is_default_stack_executable
   5188   true,			// can_icf_inline_merge_sections
   5189   '\0',			// wrap_char
   5190   "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
   5191   0x20000,		// default_text_segment_address
   5192   0x10000,		// abi_pagesize (overridable by -z max-page-size)
   5193   0x10000,		// common_pagesize (overridable by -z common-page-size)
   5194   true,                 // isolate_execinstr
   5195   0x10000000,           // rosegment_gap
   5196   elfcpp::SHN_UNDEF,	// small_common_shndx
   5197   elfcpp::SHN_X86_64_LCOMMON,	// large_common_shndx
   5198   0,			// small_common_section_flags
   5199   elfcpp::SHF_X86_64_LARGE,	// large_common_section_flags
   5200   NULL,			// attributes_section
   5201   NULL,			// attributes_vendor
   5202   "_start",		// entry_symbol_name
   5203   32,			// hash_entry_size
   5204 };
   5205 
   5206 template<>
   5207 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
   5208 {
   5209   32,			// size
   5210   false,		// is_big_endian
   5211   elfcpp::EM_X86_64,	// machine_code
   5212   false,		// has_make_symbol
   5213   false,		// has_resolve
   5214   true,			// has_code_fill
   5215   true,			// is_default_stack_executable
   5216   true,			// can_icf_inline_merge_sections
   5217   '\0',			// wrap_char
   5218   "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
   5219   0x20000,		// default_text_segment_address
   5220   0x10000,		// abi_pagesize (overridable by -z max-page-size)
   5221   0x10000,		// common_pagesize (overridable by -z common-page-size)
   5222   true,                 // isolate_execinstr
   5223   0x10000000,           // rosegment_gap
   5224   elfcpp::SHN_UNDEF,	// small_common_shndx
   5225   elfcpp::SHN_X86_64_LCOMMON,	// large_common_shndx
   5226   0,			// small_common_section_flags
   5227   elfcpp::SHF_X86_64_LARGE,	// large_common_section_flags
   5228   NULL,			// attributes_section
   5229   NULL,			// attributes_vendor
   5230   "_start",		// entry_symbol_name
   5231   32,			// hash_entry_size
   5232 };
   5233 
   5234 #define	NACLMASK	0xe0            // 32-byte alignment mask.
   5235 
   5236 // The first entry in the PLT.
   5237 
   5238 template<int size>
   5239 const unsigned char
   5240 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
   5241 {
   5242   0xff, 0x35,                         // pushq contents of memory address
   5243   0, 0, 0, 0,                         // replaced with address of .got + 8
   5244   0x4c, 0x8b, 0x1d,                   // mov GOT+16(%rip), %r11
   5245   0, 0, 0, 0,                         // replaced with address of .got + 16
   5246   0x41, 0x83, 0xe3, NACLMASK,         // and $-32, %r11d
   5247   0x4d, 0x01, 0xfb,                   // add %r15, %r11
   5248   0x41, 0xff, 0xe3,                   // jmpq *%r11
   5249 
   5250   // 9-byte nop sequence to pad out to the next 32-byte boundary.
   5251   0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
   5252 
   5253   // 32 bytes of nop to pad out to the standard size
   5254   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
   5255   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
   5256   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
   5257   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
   5258   0x66,                                  // excess data32 prefix
   5259   0x90                                   // nop
   5260 };
   5261 
   5262 template<int size>
   5263 void
   5264 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
   5265     unsigned char* pov,
   5266     typename elfcpp::Elf_types<size>::Elf_Addr got_address,
   5267     typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
   5268 {
   5269   memcpy(pov, first_plt_entry, plt_entry_size);
   5270   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
   5271 					      (got_address + 8
   5272 					       - (plt_address + 2 + 4)));
   5273   elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
   5274 					      (got_address + 16
   5275 					       - (plt_address + 9 + 4)));
   5276 }
   5277 
   5278 // Subsequent entries in the PLT.
   5279 
   5280 template<int size>
   5281 const unsigned char
   5282 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
   5283 {
   5284   0x4c, 0x8b, 0x1d,              // mov name@GOTPCREL(%rip),%r11
   5285   0, 0, 0, 0,                    // replaced with address of symbol in .got
   5286   0x41, 0x83, 0xe3, NACLMASK,    // and $-32, %r11d
   5287   0x4d, 0x01, 0xfb,              // add %r15, %r11
   5288   0x41, 0xff, 0xe3,              // jmpq *%r11
   5289 
   5290   // 15-byte nop sequence to pad out to the next 32-byte boundary.
   5291   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
   5292   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
   5293 
   5294   // Lazy GOT entries point here (32-byte aligned).
   5295   0x68,                       // pushq immediate
   5296   0, 0, 0, 0,                 // replaced with index into relocation table
   5297   0xe9,                       // jmp relative
   5298   0, 0, 0, 0,                 // replaced with offset to start of .plt0
   5299 
   5300   // 22 bytes of nop to pad out to the standard size.
   5301   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
   5302   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
   5303   0x0f, 0x1f, 0x80, 0, 0, 0, 0,          // nopl 0x0(%rax)
   5304 };
   5305 
   5306 template<int size>
   5307 unsigned int
   5308 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
   5309     unsigned char* pov,
   5310     typename elfcpp::Elf_types<size>::Elf_Addr got_address,
   5311     typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
   5312     unsigned int got_offset,
   5313     unsigned int plt_offset,
   5314     unsigned int plt_index)
   5315 {
   5316   memcpy(pov, plt_entry, plt_entry_size);
   5317   elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
   5318 					      (got_address + got_offset
   5319 					       - (plt_address + plt_offset
   5320 						  + 3 + 4)));
   5321 
   5322   elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
   5323   elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
   5324 					      - (plt_offset + 38 + 4));
   5325 
   5326   return 32;
   5327 }
   5328 
   5329 // The reserved TLSDESC entry in the PLT.
   5330 
   5331 template<int size>
   5332 const unsigned char
   5333 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
   5334 {
   5335   0xff, 0x35,			// pushq x(%rip)
   5336   0, 0, 0, 0,	// replaced with address of linkmap GOT entry (at PLTGOT + 8)
   5337   0x4c, 0x8b, 0x1d,		// mov y(%rip),%r11
   5338   0, 0, 0, 0,	// replaced with offset of reserved TLSDESC_GOT entry
   5339   0x41, 0x83, 0xe3, NACLMASK,	// and $-32, %r11d
   5340   0x4d, 0x01, 0xfb,             // add %r15, %r11
   5341   0x41, 0xff, 0xe3,             // jmpq *%r11
   5342 
   5343   // 41 bytes of nop to pad out to the standard size.
   5344   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
   5345   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
   5346   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
   5347   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
   5348   0x66, 0x66,                            // excess data32 prefixes
   5349   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
   5350 };
   5351 
   5352 template<int size>
   5353 void
   5354 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
   5355     unsigned char* pov,
   5356     typename elfcpp::Elf_types<size>::Elf_Addr got_address,
   5357     typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
   5358     typename elfcpp::Elf_types<size>::Elf_Addr got_base,
   5359     unsigned int tlsdesc_got_offset,
   5360     unsigned int plt_offset)
   5361 {
   5362   memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
   5363   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
   5364 					      (got_address + 8
   5365 					       - (plt_address + plt_offset
   5366 						  + 2 + 4)));
   5367   elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
   5368 					      (got_base
   5369 					       + tlsdesc_got_offset
   5370 					       - (plt_address + plt_offset
   5371 						  + 9 + 4)));
   5372 }
   5373 
   5374 // The .eh_frame unwind information for the PLT.
   5375 
   5376 template<int size>
   5377 const unsigned char
   5378 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
   5379 {
   5380   0, 0, 0, 0,				// Replaced with offset to .plt.
   5381   0, 0, 0, 0,				// Replaced with size of .plt.
   5382   0,					// Augmentation size.
   5383   elfcpp::DW_CFA_def_cfa_offset, 16,	// DW_CFA_def_cfa_offset: 16.
   5384   elfcpp::DW_CFA_advance_loc + 6,	// Advance 6 to __PLT__ + 6.
   5385   elfcpp::DW_CFA_def_cfa_offset, 24,	// DW_CFA_def_cfa_offset: 24.
   5386   elfcpp::DW_CFA_advance_loc + 58,	// Advance 58 to __PLT__ + 64.
   5387   elfcpp::DW_CFA_def_cfa_expression,	// DW_CFA_def_cfa_expression.
   5388   13,					// Block length.
   5389   elfcpp::DW_OP_breg7, 8,		// Push %rsp + 8.
   5390   elfcpp::DW_OP_breg16, 0,		// Push %rip.
   5391   elfcpp::DW_OP_const1u, 63,		// Push 0x3f.
   5392   elfcpp::DW_OP_and,			// & (%rip & 0x3f).
   5393   elfcpp::DW_OP_const1u, 37,            // Push 0x25.
   5394   elfcpp::DW_OP_ge,			// >= ((%rip & 0x3f) >= 0x25)
   5395   elfcpp::DW_OP_lit3,			// Push 3.
   5396   elfcpp::DW_OP_shl,			// << (((%rip & 0x3f) >= 0x25) << 3)
   5397   elfcpp::DW_OP_plus,			// + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
   5398   elfcpp::DW_CFA_nop,			// Align to 32 bytes.
   5399   elfcpp::DW_CFA_nop
   5400 };
   5401 
   5402 // Return a string used to fill a code section with nops.
   5403 // For NaCl, long NOPs are only valid if they do not cross
   5404 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
   5405 template<int size>
   5406 std::string
   5407 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
   5408 {
   5409   return std::string(length, static_cast<char>(0x90));
   5410 }
   5411 
   5412 // The selector for x86_64-nacl object files.
   5413 
   5414 template<int size>
   5415 class Target_selector_x86_64_nacl
   5416   : public Target_selector_nacl<Target_selector_x86_64<size>,
   5417 				Target_x86_64_nacl<size> >
   5418 {
   5419  public:
   5420   Target_selector_x86_64_nacl()
   5421     : Target_selector_nacl<Target_selector_x86_64<size>,
   5422 			   Target_x86_64_nacl<size> >("x86-64",
   5423 						      size == 64
   5424 						      ? "elf64-x86-64-nacl"
   5425 						      : "elf32-x86-64-nacl",
   5426 						      size == 64
   5427 						      ? "elf_x86_64_nacl"
   5428 						      : "elf32_x86_64_nacl")
   5429   { }
   5430 };
   5431 
   5432 Target_selector_x86_64_nacl<64> target_selector_x86_64;
   5433 Target_selector_x86_64_nacl<32> target_selector_x32;
   5434 
   5435 } // End anonymous namespace.
   5436