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