PR 10419
[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       gold_assert(tls_segment != NULL);
2123       if (optimized_type == tls::TLSOPT_TO_LE)
2124         {
2125           // This relocation type is used in debugging information.
2126           // In that case we need to not optimize the value.  If we
2127           // haven't seen a TLSLD reloc, then we assume we should not
2128           // optimize this reloc.
2129           if (this->saw_tls_block_reloc_)
2130             value -= tls_segment->memsz();
2131         }
2132       Relocate_functions<64, false>::rela32(view, value, addend);
2133       break;
2134
2135     case elfcpp::R_X86_64_DTPOFF64:
2136       gold_assert(tls_segment != NULL);
2137       if (optimized_type == tls::TLSOPT_TO_LE)
2138         {
2139           // See R_X86_64_DTPOFF32, just above, for why we test this.
2140           if (this->saw_tls_block_reloc_)
2141             value -= tls_segment->memsz();
2142         }
2143       Relocate_functions<64, false>::rela64(view, value, addend);
2144       break;
2145
2146     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2147       if (optimized_type == tls::TLSOPT_TO_LE)
2148         {
2149           gold_assert(tls_segment != NULL);
2150           Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2151                                                 rela, r_type, value, view,
2152                                                 view_size);
2153           break;
2154         }
2155       else if (optimized_type == tls::TLSOPT_NONE)
2156         {
2157           // Relocate the field with the offset of the GOT entry for
2158           // the tp-relative offset of the symbol.
2159           unsigned int got_offset;
2160           if (gsym != NULL)
2161             {
2162               gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2163               got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2164                             - target->got_size());
2165             }
2166           else
2167             {
2168               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2169               gold_assert(object->local_has_got_offset(r_sym,
2170                                                        GOT_TYPE_TLS_OFFSET));
2171               got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2172                             - target->got_size());
2173             }
2174           value = target->got_plt_section()->address() + got_offset;
2175           Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2176           break;
2177         }
2178       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2179                              _("unsupported reloc type %u"),
2180                              r_type);
2181       break;
2182
2183     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2184       value -= tls_segment->memsz();
2185       Relocate_functions<64, false>::rela32(view, value, addend);
2186       break;
2187     }
2188 }
2189
2190 // Do a relocation in which we convert a TLS General-Dynamic to an
2191 // Initial-Exec.
2192
2193 inline void
2194 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2195                                       size_t relnum,
2196                                       Output_segment*,
2197                                       const elfcpp::Rela<64, false>& rela,
2198                                       unsigned int,
2199                                       elfcpp::Elf_types<64>::Elf_Addr value,
2200                                       unsigned char* view,
2201                                       elfcpp::Elf_types<64>::Elf_Addr address,
2202                                       section_size_type view_size)
2203 {
2204   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2205   // .word 0x6666; rex64; call __tls_get_addr
2206   // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2207
2208   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2209   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2210
2211   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2212                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2213   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2214                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2215
2216   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2217
2218   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2219   Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2220
2221   // The next reloc should be a PLT32 reloc against __tls_get_addr.
2222   // We can skip it.
2223   this->skip_call_tls_get_addr_ = true;
2224 }
2225
2226 // Do a relocation in which we convert a TLS General-Dynamic to a
2227 // Local-Exec.
2228
2229 inline void
2230 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2231                                       size_t relnum,
2232                                       Output_segment* tls_segment,
2233                                       const elfcpp::Rela<64, false>& rela,
2234                                       unsigned int,
2235                                       elfcpp::Elf_types<64>::Elf_Addr value,
2236                                       unsigned char* view,
2237                                       section_size_type view_size)
2238 {
2239   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2240   // .word 0x6666; rex64; call __tls_get_addr
2241   // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2242
2243   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2244   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2245
2246   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2247                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2248   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2249                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2250
2251   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2252
2253   value -= tls_segment->memsz();
2254   Relocate_functions<64, false>::rela32(view + 8, value, 0);
2255
2256   // The next reloc should be a PLT32 reloc against __tls_get_addr.
2257   // We can skip it.
2258   this->skip_call_tls_get_addr_ = true;
2259 }
2260
2261 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2262
2263 inline void
2264 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2265     const Relocate_info<64, false>* relinfo,
2266     size_t relnum,
2267     Output_segment*,
2268     const elfcpp::Rela<64, false>& rela,
2269     unsigned int r_type,
2270     elfcpp::Elf_types<64>::Elf_Addr value,
2271     unsigned char* view,
2272     elfcpp::Elf_types<64>::Elf_Addr address,
2273     section_size_type view_size)
2274 {
2275   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2276     {
2277       // leaq foo@tlsdesc(%rip), %rax
2278       // ==> movq foo@gottpoff(%rip), %rax
2279       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2280       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2281       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2282                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2283       view[-2] = 0x8b;
2284       const elfcpp::Elf_Xword addend = rela.get_r_addend();
2285       Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2286     }
2287   else
2288     {
2289       // call *foo@tlscall(%rax)
2290       // ==> nop; nop
2291       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2292       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2293       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2294                      view[0] == 0xff && view[1] == 0x10);
2295       view[0] = 0x66;
2296       view[1] = 0x90;
2297     }
2298 }
2299
2300 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2301
2302 inline void
2303 Target_x86_64::Relocate::tls_desc_gd_to_le(
2304     const Relocate_info<64, false>* relinfo,
2305     size_t relnum,
2306     Output_segment* tls_segment,
2307     const elfcpp::Rela<64, false>& rela,
2308     unsigned int r_type,
2309     elfcpp::Elf_types<64>::Elf_Addr value,
2310     unsigned char* view,
2311     section_size_type view_size)
2312 {
2313   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2314     {
2315       // leaq foo@tlsdesc(%rip), %rax
2316       // ==> movq foo@tpoff, %rax
2317       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2318       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2319       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2320                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2321       view[-2] = 0xc7;
2322       view[-1] = 0xc0;
2323       value -= tls_segment->memsz();
2324       Relocate_functions<64, false>::rela32(view, value, 0);
2325     }
2326   else
2327     {
2328       // call *foo@tlscall(%rax)
2329       // ==> nop; nop
2330       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2331       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2332       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2333                      view[0] == 0xff && view[1] == 0x10);
2334       view[0] = 0x66;
2335       view[1] = 0x90;
2336     }
2337 }
2338
2339 inline void
2340 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2341                                       size_t relnum,
2342                                       Output_segment*,
2343                                       const elfcpp::Rela<64, false>& rela,
2344                                       unsigned int,
2345                                       elfcpp::Elf_types<64>::Elf_Addr,
2346                                       unsigned char* view,
2347                                       section_size_type view_size)
2348 {
2349   // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2350   // ... leq foo@dtpoff(%rax),%reg
2351   // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2352
2353   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2354   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2355
2356   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2357                  view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2358
2359   tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2360
2361   memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2362
2363   // The next reloc should be a PLT32 reloc against __tls_get_addr.
2364   // We can skip it.
2365   this->skip_call_tls_get_addr_ = true;
2366 }
2367
2368 // Do a relocation in which we convert a TLS Initial-Exec to a
2369 // Local-Exec.
2370
2371 inline void
2372 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2373                                       size_t relnum,
2374                                       Output_segment* tls_segment,
2375                                       const elfcpp::Rela<64, false>& rela,
2376                                       unsigned int,
2377                                       elfcpp::Elf_types<64>::Elf_Addr value,
2378                                       unsigned char* view,
2379                                       section_size_type view_size)
2380 {
2381   // We need to examine the opcodes to figure out which instruction we
2382   // are looking at.
2383
2384   // movq foo@gottpoff(%rip),%reg  ==>  movq $YY,%reg
2385   // addq foo@gottpoff(%rip),%reg  ==>  addq $YY,%reg
2386
2387   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2388   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2389
2390   unsigned char op1 = view[-3];
2391   unsigned char op2 = view[-2];
2392   unsigned char op3 = view[-1];
2393   unsigned char reg = op3 >> 3;
2394
2395   if (op2 == 0x8b)
2396     {
2397       // movq
2398       if (op1 == 0x4c)
2399         view[-3] = 0x49;
2400       view[-2] = 0xc7;
2401       view[-1] = 0xc0 | reg;
2402     }
2403   else if (reg == 4)
2404     {
2405       // Special handling for %rsp.
2406       if (op1 == 0x4c)
2407         view[-3] = 0x49;
2408       view[-2] = 0x81;
2409       view[-1] = 0xc0 | reg;
2410     }
2411   else
2412     {
2413       // addq
2414       if (op1 == 0x4c)
2415         view[-3] = 0x4d;
2416       view[-2] = 0x8d;
2417       view[-1] = 0x80 | reg | (reg << 3);
2418     }
2419
2420   value -= tls_segment->memsz();
2421   Relocate_functions<64, false>::rela32(view, value, 0);
2422 }
2423
2424 // Relocate section data.
2425
2426 void
2427 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
2428                                 unsigned int sh_type,
2429                                 const unsigned char* prelocs,
2430                                 size_t reloc_count,
2431                                 Output_section* output_section,
2432                                 bool needs_special_offset_handling,
2433                                 unsigned char* view,
2434                                 elfcpp::Elf_types<64>::Elf_Addr address,
2435                                 section_size_type view_size)
2436 {
2437   gold_assert(sh_type == elfcpp::SHT_RELA);
2438
2439   gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2440                          Target_x86_64::Relocate>(
2441     relinfo,
2442     this,
2443     prelocs,
2444     reloc_count,
2445     output_section,
2446     needs_special_offset_handling,
2447     view,
2448     address,
2449     view_size);
2450 }
2451
2452 // Return the size of a relocation while scanning during a relocatable
2453 // link.
2454
2455 unsigned int
2456 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2457     unsigned int r_type,
2458     Relobj* object)
2459 {
2460   switch (r_type)
2461     {
2462     case elfcpp::R_X86_64_NONE:
2463     case elfcpp::R_386_GNU_VTINHERIT:
2464     case elfcpp::R_386_GNU_VTENTRY:
2465     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2466     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2467     case elfcpp::R_X86_64_TLSDESC_CALL:
2468     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2469     case elfcpp::R_X86_64_DTPOFF32:
2470     case elfcpp::R_X86_64_DTPOFF64:
2471     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2472     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2473       return 0;
2474
2475     case elfcpp::R_X86_64_64:
2476     case elfcpp::R_X86_64_PC64:
2477     case elfcpp::R_X86_64_GOTOFF64:
2478     case elfcpp::R_X86_64_GOTPC64:
2479     case elfcpp::R_X86_64_PLTOFF64:
2480     case elfcpp::R_X86_64_GOT64:
2481     case elfcpp::R_X86_64_GOTPCREL64:
2482     case elfcpp::R_X86_64_GOTPCREL:
2483     case elfcpp::R_X86_64_GOTPLT64:
2484       return 8;
2485
2486     case elfcpp::R_X86_64_32:
2487     case elfcpp::R_X86_64_32S:
2488     case elfcpp::R_X86_64_PC32:
2489     case elfcpp::R_X86_64_PLT32:
2490     case elfcpp::R_X86_64_GOTPC32:
2491     case elfcpp::R_X86_64_GOT32:
2492       return 4;
2493
2494     case elfcpp::R_X86_64_16:
2495     case elfcpp::R_X86_64_PC16:
2496       return 2;
2497
2498     case elfcpp::R_X86_64_8:
2499     case elfcpp::R_X86_64_PC8:
2500       return 1;
2501
2502     case elfcpp::R_X86_64_COPY:
2503     case elfcpp::R_X86_64_GLOB_DAT:
2504     case elfcpp::R_X86_64_JUMP_SLOT:
2505     case elfcpp::R_X86_64_RELATIVE:
2506       // These are outstanding tls relocs, which are unexpected when linking
2507     case elfcpp::R_X86_64_TPOFF64:
2508     case elfcpp::R_X86_64_DTPMOD64:
2509     case elfcpp::R_X86_64_TLSDESC:
2510       object->error(_("unexpected reloc %u in object file"), r_type);
2511       return 0;
2512
2513     case elfcpp::R_X86_64_SIZE32:
2514     case elfcpp::R_X86_64_SIZE64:
2515     default:
2516       object->error(_("unsupported reloc %u against local symbol"), r_type);
2517       return 0;
2518     }
2519 }
2520
2521 // Scan the relocs during a relocatable link.
2522
2523 void
2524 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2525                                        Symbol_table* symtab,
2526                                        Layout* layout,
2527                                        Sized_relobj<64, false>* object,
2528                                        unsigned int data_shndx,
2529                                        unsigned int sh_type,
2530                                        const unsigned char* prelocs,
2531                                        size_t reloc_count,
2532                                        Output_section* output_section,
2533                                        bool needs_special_offset_handling,
2534                                        size_t local_symbol_count,
2535                                        const unsigned char* plocal_symbols,
2536                                        Relocatable_relocs* rr)
2537 {
2538   gold_assert(sh_type == elfcpp::SHT_RELA);
2539
2540   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2541     Relocatable_size_for_reloc> Scan_relocatable_relocs;
2542
2543   gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2544       Scan_relocatable_relocs>(
2545     options,
2546     symtab,
2547     layout,
2548     object,
2549     data_shndx,
2550     prelocs,
2551     reloc_count,
2552     output_section,
2553     needs_special_offset_handling,
2554     local_symbol_count,
2555     plocal_symbols,
2556     rr);
2557 }
2558
2559 // Relocate a section during a relocatable link.
2560
2561 void
2562 Target_x86_64::relocate_for_relocatable(
2563     const Relocate_info<64, false>* relinfo,
2564     unsigned int sh_type,
2565     const unsigned char* prelocs,
2566     size_t reloc_count,
2567     Output_section* output_section,
2568     off_t offset_in_output_section,
2569     const Relocatable_relocs* rr,
2570     unsigned char* view,
2571     elfcpp::Elf_types<64>::Elf_Addr view_address,
2572     section_size_type view_size,
2573     unsigned char* reloc_view,
2574     section_size_type reloc_view_size)
2575 {
2576   gold_assert(sh_type == elfcpp::SHT_RELA);
2577
2578   gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2579     relinfo,
2580     prelocs,
2581     reloc_count,
2582     output_section,
2583     offset_in_output_section,
2584     rr,
2585     view,
2586     view_address,
2587     view_size,
2588     reloc_view,
2589     reloc_view_size);
2590 }
2591
2592 // Return the value to use for a dynamic which requires special
2593 // treatment.  This is how we support equality comparisons of function
2594 // pointers across shared library boundaries, as described in the
2595 // processor specific ABI supplement.
2596
2597 uint64_t
2598 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2599 {
2600   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2601   return this->plt_section()->address() + gsym->plt_offset();
2602 }
2603
2604 // Return a string used to fill a code section with nops to take up
2605 // the specified length.
2606
2607 std::string
2608 Target_x86_64::do_code_fill(section_size_type length) const
2609 {
2610   if (length >= 16)
2611     {
2612       // Build a jmpq instruction to skip over the bytes.
2613       unsigned char jmp[5];
2614       jmp[0] = 0xe9;
2615       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2616       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2617               + std::string(length - 5, '\0'));
2618     }
2619
2620   // Nop sequences of various lengths.
2621   const char nop1[1] = { 0x90 };                   // nop
2622   const char nop2[2] = { 0x66, 0x90 };             // xchg %ax %ax
2623   const char nop3[3] = { 0x0f, 0x1f, 0x00 };       // nop (%rax)
2624   const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00};  // nop 0(%rax)
2625   const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00,   // nop 0(%rax,%rax,1)
2626                          0x00 };
2627   const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44,   // nopw 0(%rax,%rax,1)
2628                          0x00, 0x00 };
2629   const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00,   // nopl 0L(%rax)
2630                          0x00, 0x00, 0x00 };
2631   const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00,   // nopl 0L(%rax,%rax,1)
2632                          0x00, 0x00, 0x00, 0x00 };
2633   const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84,   // nopw 0L(%rax,%rax,1)
2634                          0x00, 0x00, 0x00, 0x00,
2635                          0x00 };
2636   const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
2637                            0x84, 0x00, 0x00, 0x00,
2638                            0x00, 0x00 };
2639   const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
2640                            0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2641                            0x00, 0x00, 0x00 };
2642   const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
2643                            0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2644                            0x00, 0x00, 0x00, 0x00 };
2645   const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2646                            0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
2647                            0x00, 0x00, 0x00, 0x00,
2648                            0x00 };
2649   const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2650                            0x66, 0x2e, 0x0f, 0x1f, // data16
2651                            0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2652                            0x00, 0x00 };
2653   const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2654                            0x66, 0x66, 0x2e, 0x0f, // data16; data16
2655                            0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2656                            0x00, 0x00, 0x00 };
2657
2658   const char* nops[16] = {
2659     NULL,
2660     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2661     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2662   };
2663
2664   return std::string(nops[length], length);
2665 }
2666
2667 // The selector for x86_64 object files.
2668
2669 class Target_selector_x86_64 : public Target_selector_freebsd
2670 {
2671 public:
2672   Target_selector_x86_64()
2673     : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
2674                               "elf64-x86-64-freebsd")
2675   { }
2676
2677   Target*
2678   do_instantiate_target()
2679   { return new Target_x86_64(); }
2680
2681 };
2682
2683 Target_selector_x86_64 target_selector_x86_64;
2684
2685 } // End anonymous namespace.