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