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