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