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