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