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