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