From Craig Silverstein: better organization for TLS code.
[platform/upstream/binutils.git] / gold / x86_64.cc
1 // x86_64.cc -- x86_64 target support for gold.
2
3 // Copyright 2006, 2007, 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
9 // modify it under the terms of the GNU Library General Public License
10 // as published by the Free Software Foundation; either version 2, or
11 // (at your option) any later version.
12
13 // In addition to the permissions in the GNU Library General Public
14 // License, the Free Software Foundation gives you unlimited
15 // permission to link the compiled version of this file into
16 // combinations with other programs, and to distribute those
17 // combinations without any restriction coming from the use of this
18 // file.  (The Library Public License restrictions do apply in other
19 // respects; for example, they cover modification of the file, and
20 /// distribution when not linked into a combined executable.)
21
22 // This program is distributed in the hope that it will be useful, but
23 // WITHOUT ANY WARRANTY; without even the implied warranty of
24 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25 // Library General Public License for more details.
26
27 // You should have received a copy of the GNU Library General Public
28 // License along with this program; if not, write to the Free Software
29 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
30 // 02110-1301, USA.
31
32 #include "gold.h"
33
34 #include <cstring>
35
36 #include "elfcpp.h"
37 #include "parameters.h"
38 #include "reloc.h"
39 #include "x86_64.h"
40 #include "object.h"
41 #include "symtab.h"
42 #include "layout.h"
43 #include "output.h"
44 #include "target.h"
45 #include "target-reloc.h"
46 #include "target-select.h"
47 #include "tls.h"
48
49 namespace
50 {
51
52 using namespace gold;
53
54 class Output_data_plt_x86_64;
55
56 // The x86_64 target class.
57 // See the ABI at
58 //   http://www.x86-64.org/documentation/abi.pdf
59 // TLS info comes from
60 //   http://people.redhat.com/drepper/tls.pdf
61 //   http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
62
63 class Target_x86_64 : public Sized_target<64, false>
64 {
65  public:
66   // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
67   // uses only Elf64_Rela relocation entries with explicit addends."
68   typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
69
70   Target_x86_64()
71     : Sized_target<64, false>(&x86_64_info),
72       got_(NULL), plt_(NULL), got_plt_(NULL), rela_dyn_(NULL),
73       copy_relocs_(NULL), dynbss_(NULL)
74   { }
75
76   // Scan the relocations to look for symbol adjustments.
77   void
78   scan_relocs(const General_options& options,
79               Symbol_table* symtab,
80               Layout* layout,
81               Sized_relobj<64, false>* object,
82               unsigned int data_shndx,
83               unsigned int sh_type,
84               const unsigned char* prelocs,
85               size_t reloc_count,
86               size_t local_symbol_count,
87               const unsigned char* plocal_symbols,
88               Symbol** global_symbols);
89
90   // Finalize the sections.
91   void
92   do_finalize_sections(Layout*);
93
94   // Return the value to use for a dynamic which requires special
95   // treatment.
96   uint64_t
97   do_dynsym_value(const Symbol*) const;
98
99   // Relocate a section.
100   void
101   relocate_section(const Relocate_info<64, false>*,
102                    unsigned int sh_type,
103                    const unsigned char* prelocs,
104                    size_t reloc_count,
105                    unsigned char* view,
106                    elfcpp::Elf_types<64>::Elf_Addr view_address,
107                    off_t view_size);
108
109   // Return a string used to fill a code section with nops.
110   std::string
111   do_code_fill(off_t length);
112
113  private:
114   // The class which scans relocations.
115   struct Scan
116   {
117     inline void
118     local(const General_options& options, Symbol_table* symtab,
119           Layout* layout, Target_x86_64* target,
120           Sized_relobj<64, false>* object,
121           unsigned int data_shndx,
122           const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
123           const elfcpp::Sym<64, false>& lsym);
124
125     inline void
126     global(const General_options& options, Symbol_table* symtab,
127            Layout* layout, Target_x86_64* target,
128            Sized_relobj<64, false>* object,
129            unsigned int data_shndx,
130            const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
131            Symbol* gsym);
132
133     static void
134     unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
135
136     static void
137     unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
138                              Symbol*);
139   };
140
141   // The class which implements relocation.
142   class Relocate
143   {
144    public:
145     Relocate()
146       : skip_call_tls_get_addr_(false)
147     { }
148
149     ~Relocate()
150     {
151       if (this->skip_call_tls_get_addr_)
152         {
153           // FIXME: This needs to specify the location somehow.
154           gold_error(_("missing expected TLS relocation"));
155         }
156     }
157
158     // Do a relocation.  Return false if the caller should not issue
159     // any warnings about this relocation.
160     inline bool
161     relocate(const Relocate_info<64, false>*, Target_x86_64*, size_t relnum,
162              const elfcpp::Rela<64, false>&,
163              unsigned int r_type, const Sized_symbol<64>*,
164              const Symbol_value<64>*,
165              unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
166              off_t);
167
168    private:
169     // Do a TLS relocation.
170     inline void
171     relocate_tls(const Relocate_info<64, false>*, size_t relnum,
172                  const elfcpp::Rela<64, false>&,
173                  unsigned int r_type, const Sized_symbol<64>*,
174                  const Symbol_value<64>*,
175                  unsigned char*, elfcpp::Elf_types<64>::Elf_Addr, off_t);
176
177     // Do a TLS General-Dynamic to Local-Exec transition.
178     inline void
179     tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
180                  Output_segment* tls_segment,
181                  const elfcpp::Rela<64, false>&, unsigned int r_type,
182                  elfcpp::Elf_types<64>::Elf_Addr value,
183                  unsigned char* view,
184                  off_t view_size);
185
186     // Do a TLS Local-Dynamic to Local-Exec transition.
187     inline void
188     tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
189                  Output_segment* tls_segment,
190                  const elfcpp::Rela<64, false>&, unsigned int r_type,
191                  elfcpp::Elf_types<64>::Elf_Addr value,
192                  unsigned char* view,
193                  off_t view_size);
194
195     // Do a TLS Initial-Exec to Local-Exec transition.
196     static inline void
197     tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
198                  Output_segment* tls_segment,
199                  const elfcpp::Rela<64, false>&, unsigned int r_type,
200                  elfcpp::Elf_types<64>::Elf_Addr value,
201                  unsigned char* view,
202                  off_t view_size);
203
204     // This is set if we should skip the next reloc, which should be a
205     // PLT32 reloc against ___tls_get_addr.
206     bool skip_call_tls_get_addr_;
207   };
208
209   // Adjust TLS relocation type based on the options and whether this
210   // is a local symbol.
211   static tls::Tls_optimization
212   optimize_tls_reloc(bool is_final, int r_type);
213
214   // Get the GOT section, creating it if necessary.
215   Output_data_got<64, false>*
216   got_section(Symbol_table*, Layout*);
217
218   // Create a PLT entry for a global symbol.
219   void
220   make_plt_entry(Symbol_table*, Layout*, Symbol*);
221
222   // Get the PLT section.
223   Output_data_plt_x86_64*
224   plt_section() const
225   {
226     gold_assert(this->plt_ != NULL);
227     return this->plt_;
228   }
229
230   // Get the dynamic reloc section, creating it if necessary.
231   Reloc_section*
232   rela_dyn_section(Layout*);
233
234   // Copy a relocation against a global symbol.
235   void
236   copy_reloc(const General_options*, Symbol_table*, Layout*,
237              Sized_relobj<64, false>*, unsigned int,
238              Symbol*, const elfcpp::Rela<64, false>&);
239
240   // Information about this specific target which we pass to the
241   // general Target structure.
242   static const Target::Target_info x86_64_info;
243
244   // The GOT section.
245   Output_data_got<64, false>* got_;
246   // The PLT section.
247   Output_data_plt_x86_64* plt_;
248   // The GOT PLT section.
249   Output_data_space* got_plt_;
250   // The dynamic reloc section.
251   Reloc_section* rela_dyn_;
252   // Relocs saved to avoid a COPY reloc.
253   Copy_relocs<64, false>* copy_relocs_;
254   // Space for variables copied with a COPY reloc.
255   Output_data_space* dynbss_;
256 };
257
258 const Target::Target_info Target_x86_64::x86_64_info =
259 {
260   64,                   // size
261   false,                // is_big_endian
262   elfcpp::EM_X86_64,    // machine_code
263   false,                // has_make_symbol
264   false,                // has_resolve
265   true,                 // has_code_fill
266   true,                 // is_default_stack_executable
267   "/lib/ld64.so.1",     // program interpreter
268   0x400000,             // default_text_segment_address
269   0x1000,               // abi_pagesize
270   0x1000                // common_pagesize
271 };
272
273 // Get the GOT section, creating it if necessary.
274
275 Output_data_got<64, false>*
276 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
277 {
278   if (this->got_ == NULL)
279     {
280       gold_assert(symtab != NULL && layout != NULL);
281
282       this->got_ = new Output_data_got<64, false>();
283
284       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
285                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
286                                       this->got_);
287
288       // The old GNU linker creates a .got.plt section.  We just
289       // create another set of data in the .got section.  Note that we
290       // always create a PLT if we create a GOT, although the PLT
291       // might be empty.
292       // TODO(csilvers): do we really need an alignment of 8?
293       this->got_plt_ = new Output_data_space(8);
294       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
295                                       elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
296                                       this->got_plt_);
297
298       // The first three entries are reserved.
299       this->got_plt_->set_space_size(3 * 8);
300
301       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
302       symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
303                                     this->got_plt_,
304                                     0, 0, elfcpp::STT_OBJECT,
305                                     elfcpp::STB_LOCAL,
306                                     elfcpp::STV_HIDDEN, 0,
307                                     false, false);
308     }
309
310   return this->got_;
311 }
312
313 // Get the dynamic reloc section, creating it if necessary.
314
315 Target_x86_64::Reloc_section*
316 Target_x86_64::rela_dyn_section(Layout* layout)
317 {
318   if (this->rela_dyn_ == NULL)
319     {
320       gold_assert(layout != NULL);
321       this->rela_dyn_ = new Reloc_section();
322       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
323                                       elfcpp::SHF_ALLOC, this->rela_dyn_);
324     }
325   return this->rela_dyn_;
326 }
327
328 // A class to handle the PLT data.
329
330 class Output_data_plt_x86_64 : public Output_section_data
331 {
332  public:
333   typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
334
335   Output_data_plt_x86_64(Layout*, Output_data_space*);
336
337   // Add an entry to the PLT.
338   void
339   add_entry(Symbol* gsym);
340
341   // Return the .rel.plt section data.
342   const Reloc_section*
343   rel_plt() const
344   { return this->rel_; }
345
346  protected:
347   void
348   do_adjust_output_section(Output_section* os);
349
350  private:
351   // The size of an entry in the PLT.
352   static const int plt_entry_size = 16;
353
354   // The first entry in the PLT.
355   // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
356   // procedure linkage table for both programs and shared objects."
357   static unsigned char first_plt_entry[plt_entry_size];
358
359   // Other entries in the PLT for an executable.
360   static unsigned char plt_entry[plt_entry_size];
361
362   // Set the final size.
363   void
364   do_set_address(uint64_t, off_t)
365   { this->set_data_size((this->count_ + 1) * plt_entry_size); }
366
367   // Write out the PLT data.
368   void
369   do_write(Output_file*);
370
371   // The reloc section.
372   Reloc_section* rel_;
373   // The .got.plt section.
374   Output_data_space* got_plt_;
375   // The number of PLT entries.
376   unsigned int count_;
377 };
378
379 // Create the PLT section.  The ordinary .got section is an argument,
380 // since we need to refer to the start.  We also create our own .got
381 // section just for PLT entries.
382
383 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
384                                                Output_data_space* got_plt)
385     // TODO(csilvers): do we really need an alignment of 8?
386   : Output_section_data(8), got_plt_(got_plt), count_(0)
387 {
388   this->rel_ = new Reloc_section();
389   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
390                                   elfcpp::SHF_ALLOC, this->rel_);
391 }
392
393 void
394 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
395 {
396   // UnixWare sets the entsize of .plt to 4, and so does the old GNU
397   // linker, and so do we.
398   os->set_entsize(4);
399 }
400
401 // Add an entry to the PLT.
402
403 void
404 Output_data_plt_x86_64::add_entry(Symbol* gsym)
405 {
406   gold_assert(!gsym->has_plt_offset());
407
408   // Note that when setting the PLT offset we skip the initial
409   // reserved PLT entry.
410   gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
411
412   ++this->count_;
413
414   off_t got_offset = this->got_plt_->data_size();
415
416   // Every PLT entry needs a GOT entry which points back to the PLT
417   // entry (this will be changed by the dynamic linker, normally
418   // lazily when the function is called).
419   this->got_plt_->set_space_size(got_offset + 8);
420
421   // Every PLT entry needs a reloc.
422   gsym->set_needs_dynsym_entry();
423   this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
424                          got_offset, 0);
425
426   // Note that we don't need to save the symbol.  The contents of the
427   // PLT are independent of which symbols are used.  The symbols only
428   // appear in the relocations.
429 }
430
431 // The first entry in the PLT for an executable.
432
433 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
434 {
435   // From AMD64 ABI Draft 0.98, page 76
436   0xff, 0x35,   // pushq contents of memory address
437   0, 0, 0, 0,   // replaced with address of .got + 4
438   0xff, 0x25,   // jmp indirect
439   0, 0, 0, 0,   // replaced with address of .got + 8
440   0x90, 0x90, 0x90, 0x90   // noop (x4)
441 };
442
443 // Subsequent entries in the PLT for an executable.
444
445 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
446 {
447   // From AMD64 ABI Draft 0.98, page 76
448   0xff, 0x25,   // jmpq indirect
449   0, 0, 0, 0,   // replaced with address of symbol in .got
450   0x68,         // pushq immediate
451   0, 0, 0, 0,   // replaced with offset into relocation table
452   0xe9,         // jmpq relative
453   0, 0, 0, 0    // replaced with offset to start of .plt
454 };
455
456 // Write out the PLT.  This uses the hand-coded instructions above,
457 // and adjusts them as needed.  This is specified by the AMD64 ABI.
458
459 void
460 Output_data_plt_x86_64::do_write(Output_file* of)
461 {
462   const off_t offset = this->offset();
463   const off_t oview_size = this->data_size();
464   unsigned char* const oview = of->get_output_view(offset, oview_size);
465
466   const off_t got_file_offset = this->got_plt_->offset();
467   const off_t got_size = this->got_plt_->data_size();
468   unsigned char* const got_view = of->get_output_view(got_file_offset,
469                                                       got_size);
470
471   unsigned char* pov = oview;
472
473   elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
474   elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
475
476   memcpy(pov, first_plt_entry, plt_entry_size);
477   if (!parameters->output_is_shared())
478     {
479       // We do a jmp relative to the PC at the end of this instruction.
480       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 8
481                                                   - (plt_address + 6));
482       elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 16
483                                         - (plt_address + 12));
484     }
485   pov += plt_entry_size;
486
487   unsigned char* got_pov = got_view;
488
489   memset(got_pov, 0, 24);
490   got_pov += 24;
491
492   unsigned int plt_offset = plt_entry_size;
493   unsigned int got_offset = 24;
494   const unsigned int count = this->count_;
495   for (unsigned int plt_index = 0;
496        plt_index < count;
497        ++plt_index,
498          pov += plt_entry_size,
499          got_pov += 8,
500          plt_offset += plt_entry_size,
501          got_offset += 8)
502     {
503       // Set and adjust the PLT entry itself.
504       memcpy(pov, plt_entry, plt_entry_size);
505       if (parameters->output_is_shared())
506         // FIXME(csilvers): what's the right thing to write here?
507         elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
508       else
509         elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
510                                                     (got_address + got_offset
511                                                      - (plt_address + plt_offset
512                                                         + 6)));
513
514       elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
515       elfcpp::Swap<32, false>::writeval(pov + 12,
516                                         - (plt_offset + plt_entry_size));
517
518       // Set the entry in the GOT.
519       elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
520     }
521
522   gold_assert(pov - oview == oview_size);
523   gold_assert(got_pov - got_view == got_size);
524
525   of->write_output_view(offset, oview_size, oview);
526   of->write_output_view(got_file_offset, got_size, got_view);
527 }
528
529 // Create a PLT entry for a global symbol.
530
531 void
532 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
533                               Symbol* gsym)
534 {
535   if (gsym->has_plt_offset())
536     return;
537
538   if (this->plt_ == NULL)
539     {
540       // Create the GOT sections first.
541       this->got_section(symtab, layout);
542
543       this->plt_ = new Output_data_plt_x86_64(layout, this->got_plt_);
544       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
545                                       (elfcpp::SHF_ALLOC
546                                        | elfcpp::SHF_EXECINSTR),
547                                       this->plt_);
548     }
549
550   this->plt_->add_entry(gsym);
551 }
552
553 // Handle a relocation against a non-function symbol defined in a
554 // dynamic object.  The traditional way to handle this is to generate
555 // a COPY relocation to copy the variable at runtime from the shared
556 // object into the executable's data segment.  However, this is
557 // undesirable in general, as if the size of the object changes in the
558 // dynamic object, the executable will no longer work correctly.  If
559 // this relocation is in a writable section, then we can create a
560 // dynamic reloc and the dynamic linker will resolve it to the correct
561 // address at runtime.  However, we do not want do that if the
562 // relocation is in a read-only section, as it would prevent the
563 // readonly segment from being shared.  And if we have to eventually
564 // generate a COPY reloc, then any dynamic relocations will be
565 // useless.  So this means that if this is a writable section, we need
566 // to save the relocation until we see whether we have to create a
567 // COPY relocation for this symbol for any other relocation.
568
569 void
570 Target_x86_64::copy_reloc(const General_options* options,
571                           Symbol_table* symtab,
572                           Layout* layout,
573                           Sized_relobj<64, false>* object,
574                           unsigned int data_shndx, Symbol* gsym,
575                           const elfcpp::Rela<64, false>& rela)
576 {
577   Sized_symbol<64>* ssym;
578   ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
579                                                         SELECT_SIZE(64));
580
581   if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
582                                                data_shndx, ssym))
583     {
584       // So far we do not need a COPY reloc.  Save this relocation.
585       // If it turns out that we never need a COPY reloc for this
586       // symbol, then we will emit the relocation.
587       if (this->copy_relocs_ == NULL)
588         this->copy_relocs_ = new Copy_relocs<64, false>();
589       this->copy_relocs_->save(ssym, object, data_shndx, rela);
590     }
591   else
592     {
593       // Allocate space for this symbol in the .bss section.
594
595       elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
596
597       // There is no defined way to determine the required alignment
598       // of the symbol.  We pick the alignment based on the size.  We
599       // set an arbitrary maximum of 256.
600       unsigned int align;
601       for (align = 1; align < 512; align <<= 1)
602         if ((symsize & align) != 0)
603           break;
604
605       if (this->dynbss_ == NULL)
606         {
607           this->dynbss_ = new Output_data_space(align);
608           layout->add_output_section_data(".bss",
609                                           elfcpp::SHT_NOBITS,
610                                           (elfcpp::SHF_ALLOC
611                                            | elfcpp::SHF_WRITE),
612                                           this->dynbss_);
613         }
614
615       Output_data_space* dynbss = this->dynbss_;
616
617       if (align > dynbss->addralign())
618         dynbss->set_space_alignment(align);
619
620       off_t dynbss_size = dynbss->data_size();
621       dynbss_size = align_address(dynbss_size, align);
622       off_t offset = dynbss_size;
623       dynbss->set_space_size(dynbss_size + symsize);
624
625       symtab->define_with_copy_reloc(this, ssym, dynbss, offset);
626
627       // Add the COPY reloc.
628       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
629       rela_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0);
630     }
631 }
632
633
634 // Optimize the TLS relocation type based on what we know about the
635 // symbol.  IS_FINAL is true if the final address of this symbol is
636 // known at link time.
637
638 tls::Tls_optimization
639 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
640 {
641   // If we are generating a shared library, then we can't do anything
642   // in the linker.
643   if (parameters->output_is_shared())
644     return tls::TLSOPT_NONE;
645
646   switch (r_type)
647     {
648     case elfcpp::R_X86_64_TLSGD:
649     case elfcpp::R_X86_64_GOTPC32_TLSDESC:
650     case elfcpp::R_X86_64_TLSDESC_CALL:
651       // These are General-Dynamic which permits fully general TLS
652       // access.  Since we know that we are generating an executable,
653       // we can convert this to Initial-Exec.  If we also know that
654       // this is a local symbol, we can further switch to Local-Exec.
655       if (is_final)
656         return tls::TLSOPT_TO_LE;
657       return tls::TLSOPT_TO_IE;
658
659     case elfcpp::R_X86_64_TLSLD:
660       // This is Local-Dynamic, which refers to a local symbol in the
661       // dynamic TLS block.  Since we know that we generating an
662       // executable, we can switch to Local-Exec.
663       return tls::TLSOPT_TO_LE;
664
665     case elfcpp::R_X86_64_DTPOFF32:
666     case elfcpp::R_X86_64_DTPOFF64:
667       // Another Local-Dynamic reloc.
668       return tls::TLSOPT_TO_LE;
669
670     case elfcpp::R_X86_64_GOTTPOFF:
671       // These are Initial-Exec relocs which get the thread offset
672       // from the GOT.  If we know that we are linking against the
673       // local symbol, we can switch to Local-Exec, which links the
674       // thread offset into the instruction.
675       if (is_final)
676         return tls::TLSOPT_TO_LE;
677       return tls::TLSOPT_NONE;
678
679     case elfcpp::R_X86_64_TPOFF32:
680       // When we already have Local-Exec, there is nothing further we
681       // can do.
682       return tls::TLSOPT_NONE;
683
684     default:
685       gold_unreachable();
686     }
687 }
688
689 // Report an unsupported relocation against a local symbol.
690
691 void
692 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
693                                              unsigned int r_type)
694 {
695   gold_error(_("%s: unsupported reloc %u against local symbol"),
696              object->name().c_str(), r_type);
697 }
698
699 // Scan a relocation for a local symbol.
700
701 inline void
702 Target_x86_64::Scan::local(const General_options&,
703                            Symbol_table* symtab,
704                            Layout* layout,
705                            Target_x86_64* target,
706                            Sized_relobj<64, false>* object,
707                            unsigned int data_shndx,
708                            const elfcpp::Rela<64, false>& reloc,
709                            unsigned int r_type,
710                            const elfcpp::Sym<64, false>&)
711 {
712   switch (r_type)
713     {
714     case elfcpp::R_X86_64_NONE:
715     case elfcpp::R_386_GNU_VTINHERIT:
716     case elfcpp::R_386_GNU_VTENTRY:
717       break;
718
719     case elfcpp::R_X86_64_64:
720     case elfcpp::R_X86_64_32:
721     case elfcpp::R_X86_64_32S:
722     case elfcpp::R_X86_64_16:
723     case elfcpp::R_X86_64_8:
724       // FIXME: If we are generating a shared object we need to copy
725       // this relocation into the object.
726       gold_assert(!parameters->output_is_shared());
727       break;
728
729     case elfcpp::R_X86_64_PC64:
730     case elfcpp::R_X86_64_PC32:
731     case elfcpp::R_X86_64_PC16:
732     case elfcpp::R_X86_64_PC8:
733       break;
734
735     case elfcpp::R_X86_64_PLT32:
736       // Since we know this is a local symbol, we can handle this as a
737       // PC32 reloc.
738       break;
739
740     case elfcpp::R_X86_64_GOTPC32:  // TODO(csilvers): correct?
741     case elfcpp::R_X86_64_GOTOFF64:
742     case elfcpp::R_X86_64_GOTPC64:  // TODO(csilvers): correct?
743     case elfcpp::R_X86_64_PLTOFF64:  // TODO(csilvers): correct?
744       // We need a GOT section.
745       target->got_section(symtab, layout);
746       break;
747
748     case elfcpp::R_X86_64_GOT64:
749     case elfcpp::R_X86_64_GOT32:
750     case elfcpp::R_X86_64_GOTPCREL64:
751     case elfcpp::R_X86_64_GOTPCREL:
752       {
753         // The symbol requires a GOT entry.
754         Output_data_got<64, false>* got = target->got_section(symtab, layout);
755         unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
756         if (got->add_local(object, r_sym))
757           {
758             // If we are generating a shared object, we need to add a
759             // dynamic RELATIVE relocation for this symbol.
760             if (parameters->output_is_shared())
761               {
762                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
763                 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_RELATIVE,
764                                     data_shndx, reloc.get_r_offset(), 0);
765               }
766           }
767       }
768       break;
769
770     case elfcpp::R_X86_64_COPY:
771     case elfcpp::R_X86_64_GLOB_DAT:
772     case elfcpp::R_X86_64_JUMP_SLOT:
773     case elfcpp::R_X86_64_RELATIVE:
774       // These are outstanding tls relocs, which are unexpected when linking
775     case elfcpp::R_X86_64_TPOFF64:
776     case elfcpp::R_X86_64_DTPMOD64:
777     case elfcpp::R_X86_64_TLSDESC:
778       gold_error(_("%s: unexpected reloc %u in object file"),
779                  object->name().c_str(), r_type);
780       break;
781
782       // These are initial tls relocs, which are expected when linking
783     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
784     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
785     case elfcpp::R_X86_64_TLSDESC_CALL:
786     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
787     case elfcpp::R_X86_64_DTPOFF32:
788     case elfcpp::R_X86_64_DTPOFF64:
789     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
790     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
791       {
792         bool output_is_shared = parameters->output_is_shared();
793         const tls::Tls_optimization optimized_type
794             = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
795         switch (r_type)
796           {
797           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
798           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
799           case elfcpp::R_X86_64_TLSDESC_CALL:
800             // FIXME: If not relaxing to LE, we need to generate
801             // DTPMOD64 and DTPOFF64 relocs.
802             if (optimized_type != tls::TLSOPT_TO_LE)
803               unsupported_reloc_local(object, r_type);
804             break;
805
806           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
807           case elfcpp::R_X86_64_DTPOFF32:
808           case elfcpp::R_X86_64_DTPOFF64:
809             // FIXME: If not relaxing to LE, we need to generate a
810             // DTPMOD64 reloc.
811             if (optimized_type != tls::TLSOPT_TO_LE)
812               unsupported_reloc_local(object, r_type);
813             break;
814
815           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
816             // FIXME: If not relaxing to LE, we need to generate a
817             // TPOFF64 reloc.
818             if (optimized_type != tls::TLSOPT_TO_LE)
819               unsupported_reloc_local(object, r_type);
820             break;
821
822           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
823             // FIXME: If generating a shared object, we need to copy
824             // this relocation into the object.
825             gold_assert(!output_is_shared);
826             break;
827
828           default:
829             gold_unreachable();
830           }
831       }
832       break;
833
834     case elfcpp::R_X86_64_GOTPLT64:
835     case elfcpp::R_X86_64_SIZE32:  // TODO(csilvers): correct?
836     case elfcpp::R_X86_64_SIZE64:  // TODO(csilvers): correct?
837     default:
838       gold_error(_("%s: unsupported reloc %u against local symbol"),
839                  object->name().c_str(), r_type);
840       break;
841     }
842 }
843
844
845 // Report an unsupported relocation against a global symbol.
846
847 void
848 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
849                                               unsigned int r_type,
850                                               Symbol* gsym)
851 {
852   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
853              object->name().c_str(), r_type, gsym->name());
854 }
855
856 // Scan a relocation for a global symbol.
857
858 inline void
859 Target_x86_64::Scan::global(const General_options& options,
860                             Symbol_table* symtab,
861                             Layout* layout,
862                             Target_x86_64* target,
863                             Sized_relobj<64, false>* object,
864                             unsigned int data_shndx,
865                             const elfcpp::Rela<64, false>& reloc,
866                             unsigned int r_type,
867                             Symbol* gsym)
868 {
869   switch (r_type)
870     {
871     case elfcpp::R_X86_64_NONE:
872     case elfcpp::R_386_GNU_VTINHERIT:
873     case elfcpp::R_386_GNU_VTENTRY:
874       break;
875
876     case elfcpp::R_X86_64_64:
877     case elfcpp::R_X86_64_PC64:
878     case elfcpp::R_X86_64_32:
879     case elfcpp::R_X86_64_32S:
880     case elfcpp::R_X86_64_PC32:
881     case elfcpp::R_X86_64_16:
882     case elfcpp::R_X86_64_PC16:
883     case elfcpp::R_X86_64_8:
884     case elfcpp::R_X86_64_PC8:
885       // FIXME: If we are generating a shared object we may need to
886       // copy this relocation into the object.  If this symbol is
887       // defined in a shared object, we may need to copy this
888       // relocation in order to avoid a COPY relocation.
889       gold_assert(!parameters->output_is_shared());
890
891       if (gsym->is_from_dynobj())
892         {
893           // This symbol is defined in a dynamic object.  If it is a
894           // function, we make a PLT entry.  Otherwise we need to
895           // either generate a COPY reloc or copy this reloc.
896           if (gsym->type() == elfcpp::STT_FUNC)
897             {
898               target->make_plt_entry(symtab, layout, gsym);
899
900               // If this is not a PC relative reference, then we may
901               // be taking the address of the function.  In that case
902               // we need to set the entry in the dynamic symbol table
903               // to the address of the PLT entry.
904               if (r_type != elfcpp::R_X86_64_PC64
905                   && r_type != elfcpp::R_X86_64_PC32
906                   && r_type != elfcpp::R_X86_64_PC16
907                   && r_type != elfcpp::R_X86_64_PC8)
908                 gsym->set_needs_dynsym_value();
909             }
910           else
911             target->copy_reloc(&options, symtab, layout, object, data_shndx,
912                                gsym, reloc);
913         }
914
915       break;
916
917     case elfcpp::R_X86_64_GOT64:
918     case elfcpp::R_X86_64_GOT32:
919     case elfcpp::R_X86_64_GOTPCREL64:
920     case elfcpp::R_X86_64_GOTPCREL:
921     case elfcpp::R_X86_64_GOTPLT64:
922       {
923         // The symbol requires a GOT entry.
924         Output_data_got<64, false>* got = target->got_section(symtab, layout);
925         if (got->add_global(gsym))
926           {
927             // If this symbol is not fully resolved, we need to add a
928             // dynamic relocation for it.
929             if (!gsym->final_value_is_known())
930               {
931                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
932                 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, got,
933                                      gsym->got_offset(), 0);
934               }
935           }
936       }
937       break;
938
939     case elfcpp::R_X86_64_PLT32:
940       // If the symbol is fully resolved, this is just a PC32 reloc.
941       // Otherwise we need a PLT entry.
942       if (gsym->final_value_is_known())
943         break;
944       target->make_plt_entry(symtab, layout, gsym);
945       break;
946
947     case elfcpp::R_X86_64_GOTPC32:  // TODO(csilvers): correct?
948     case elfcpp::R_X86_64_GOTOFF64:
949     case elfcpp::R_X86_64_GOTPC64:  // TODO(csilvers): correct?
950     case elfcpp::R_X86_64_PLTOFF64:  // TODO(csilvers): correct?
951       // We need a GOT section.
952       target->got_section(symtab, layout);
953       break;
954
955     case elfcpp::R_X86_64_COPY:
956     case elfcpp::R_X86_64_GLOB_DAT:
957     case elfcpp::R_X86_64_JUMP_SLOT:
958     case elfcpp::R_X86_64_RELATIVE:
959       // These are outstanding tls relocs, which are unexpected when linking
960     case elfcpp::R_X86_64_TPOFF64:
961     case elfcpp::R_X86_64_DTPMOD64:
962     case elfcpp::R_X86_64_TLSDESC:
963       gold_error(_("%s: unexpected reloc %u in object file"),
964                  object->name().c_str(), r_type);
965       break;
966
967       // These are initial tls relocs, which are expected for global()
968     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
969     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
970     case elfcpp::R_X86_64_TLSDESC_CALL:
971     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
972     case elfcpp::R_X86_64_DTPOFF32:
973     case elfcpp::R_X86_64_DTPOFF64:
974     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
975     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
976       {
977         const bool is_final = gsym->final_value_is_known();
978         const tls::Tls_optimization optimized_type
979             = Target_x86_64::optimize_tls_reloc(is_final, r_type);
980         switch (r_type)
981           {
982           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
983           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
984           case elfcpp::R_X86_64_TLSDESC_CALL:
985             // FIXME: If not relaxing to LE, we need to generate
986             // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
987             if (optimized_type != tls::TLSOPT_TO_LE)
988               unsupported_reloc_global(object, r_type, gsym);
989             break;
990
991           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
992           case elfcpp::R_X86_64_DTPOFF32:
993           case elfcpp::R_X86_64_DTPOFF64:
994             // FIXME: If not relaxing to LE, we need to generate a
995             // DTPMOD64 reloc.
996             if (optimized_type != tls::TLSOPT_TO_LE)
997               unsupported_reloc_global(object, r_type, gsym);
998             break;
999
1000           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
1001             // FIXME: If not relaxing to LE, we need to generate a
1002             // TPOFF64 reloc.
1003             if (optimized_type != tls::TLSOPT_TO_LE)
1004               unsupported_reloc_global(object, r_type, gsym);
1005             break;
1006
1007           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
1008             // FIXME: If generating a shared object, we need to copy
1009             // this relocation into the object.
1010             gold_assert(is_final);
1011             break;
1012
1013           default:
1014             gold_unreachable();
1015           }
1016       }
1017       break;
1018     case elfcpp::R_X86_64_SIZE32:  // TODO(csilvers): correct?
1019     case elfcpp::R_X86_64_SIZE64:  // TODO(csilvers): correct?
1020     default:
1021       gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1022                  object->name().c_str(), r_type, gsym->name());
1023       break;
1024     }
1025 }
1026
1027 // Scan relocations for a section.
1028
1029 void
1030 Target_x86_64::scan_relocs(const General_options& options,
1031                            Symbol_table* symtab,
1032                            Layout* layout,
1033                            Sized_relobj<64, false>* object,
1034                            unsigned int data_shndx,
1035                            unsigned int sh_type,
1036                            const unsigned char* prelocs,
1037                            size_t reloc_count,
1038                            size_t local_symbol_count,
1039                            const unsigned char* plocal_symbols,
1040                            Symbol** global_symbols)
1041 {
1042   if (sh_type == elfcpp::SHT_REL)
1043     {
1044       gold_error(_("%s: unsupported REL reloc section"),
1045                  object->name().c_str());
1046       return;
1047     }
1048
1049   gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1050       Target_x86_64::Scan>(
1051     options,
1052     symtab,
1053     layout,
1054     this,
1055     object,
1056     data_shndx,
1057     prelocs,
1058     reloc_count,
1059     local_symbol_count,
1060     plocal_symbols,
1061     global_symbols);
1062 }
1063
1064 // Finalize the sections.
1065
1066 void
1067 Target_x86_64::do_finalize_sections(Layout* layout)
1068 {
1069   // Fill in some more dynamic tags.
1070   Output_data_dynamic* const odyn = layout->dynamic_data();
1071   if (odyn != NULL)
1072     {
1073       if (this->got_plt_ != NULL)
1074         odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1075
1076       if (this->plt_ != NULL)
1077         {
1078           const Output_data* od = this->plt_->rel_plt();
1079           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1080           odyn->add_section_address(elfcpp::DT_JMPREL, od);
1081           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1082         }
1083
1084       if (this->rela_dyn_ != NULL)
1085         {
1086           const Output_data* od = this->rela_dyn_;
1087           odyn->add_section_address(elfcpp::DT_RELA, od);
1088           odyn->add_section_size(elfcpp::DT_RELASZ, od);
1089           odyn->add_constant(elfcpp::DT_RELAENT,
1090                              elfcpp::Elf_sizes<64>::rela_size);
1091         }
1092
1093       if (!parameters->output_is_shared())
1094         {
1095           // The value of the DT_DEBUG tag is filled in by the dynamic
1096           // linker at run time, and used by the debugger.
1097           odyn->add_constant(elfcpp::DT_DEBUG, 0);
1098         }
1099     }
1100
1101   // Emit any relocs we saved in an attempt to avoid generating COPY
1102   // relocs.
1103   if (this->copy_relocs_ == NULL)
1104     return;
1105   if (this->copy_relocs_->any_to_emit())
1106     {
1107       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1108       this->copy_relocs_->emit(rela_dyn);
1109     }
1110   delete this->copy_relocs_;
1111   this->copy_relocs_ = NULL;
1112 }
1113
1114 // Perform a relocation.
1115
1116 inline bool
1117 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1118                                   Target_x86_64* target,
1119                                   size_t relnum,
1120                                   const elfcpp::Rela<64, false>& rela,
1121                                   unsigned int r_type,
1122                                   const Sized_symbol<64>* gsym,
1123                                   const Symbol_value<64>* psymval,
1124                                   unsigned char* view,
1125                                   elfcpp::Elf_types<64>::Elf_Addr address,
1126                                   off_t view_size)
1127 {
1128   if (this->skip_call_tls_get_addr_)
1129     {
1130       if (r_type != elfcpp::R_X86_64_PLT32
1131           || gsym == NULL
1132           || strcmp(gsym->name(), "__tls_get_addr") != 0)
1133         {
1134           gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1135                                  _("missing expected TLS relocation"));
1136         }
1137       else
1138         {
1139           this->skip_call_tls_get_addr_ = false;
1140           return false;
1141         }
1142     }
1143
1144   // Pick the value to use for symbols defined in shared objects.
1145   Symbol_value<64> symval;
1146   if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
1147     {
1148       symval.set_output_value(target->plt_section()->address()
1149                               + gsym->plt_offset());
1150       psymval = &symval;
1151     }
1152
1153   const Sized_relobj<64, false>* object = relinfo->object;
1154   const elfcpp::Elf_Xword addend = rela.get_r_addend();
1155
1156   // Get the GOT offset if needed.
1157   bool have_got_offset = false;
1158   unsigned int got_offset = 0;
1159   switch (r_type)
1160     {
1161     case elfcpp::R_X86_64_GOT32:
1162     case elfcpp::R_X86_64_GOT64:
1163     case elfcpp::R_X86_64_GOTPLT64:
1164     case elfcpp::R_X86_64_GOTPCREL:
1165     case elfcpp::R_X86_64_GOTPCREL64:
1166       if (gsym != NULL)
1167         {
1168           gold_assert(gsym->has_got_offset());
1169           got_offset = gsym->got_offset();
1170         }
1171       else
1172         {
1173           unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1174           got_offset = object->local_got_offset(r_sym);
1175         }
1176       have_got_offset = true;
1177       break;
1178
1179     default:
1180       break;
1181     }
1182
1183   switch (r_type)
1184     {
1185     case elfcpp::R_X86_64_NONE:
1186     case elfcpp::R_386_GNU_VTINHERIT:
1187     case elfcpp::R_386_GNU_VTENTRY:
1188       break;
1189
1190     case elfcpp::R_X86_64_64:
1191       Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1192       break;
1193
1194     case elfcpp::R_X86_64_PC64:
1195       Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1196                                               address);
1197       break;
1198
1199     case elfcpp::R_X86_64_32:
1200       // FIXME: we need to verify that value + addend fits into 32 bits:
1201       //    uint64_t x = value + addend;
1202       //    x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1203       // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1204       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1205       break;
1206
1207     case elfcpp::R_X86_64_32S:
1208       // FIXME: we need to verify that value + addend fits into 32 bits:
1209       //    int64_t x = value + addend;   // note this quantity is signed!
1210       //    x == static_cast<int64_t>(static_cast<int32_t>(x))
1211       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1212       break;
1213
1214     case elfcpp::R_X86_64_PC32:
1215       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1216                                               address);
1217       break;
1218
1219     case elfcpp::R_X86_64_16:
1220       Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1221       break;
1222
1223     case elfcpp::R_X86_64_PC16:
1224       Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1225                                               address);
1226       break;
1227
1228     case elfcpp::R_X86_64_8:
1229       Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1230       break;
1231
1232     case elfcpp::R_X86_64_PC8:
1233       Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1234                                              address);
1235       break;
1236
1237     case elfcpp::R_X86_64_PLT32:
1238       gold_assert(gsym == NULL
1239                   || gsym->has_plt_offset()
1240                   || gsym->final_value_is_known());
1241       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1242                                               address);
1243       break;
1244
1245     case elfcpp::R_X86_64_GOT32:
1246       gold_assert(have_got_offset);
1247       Relocate_functions<64, false>::rela32(view, got_offset, addend);
1248       break;
1249
1250     case elfcpp::R_X86_64_GOTPC32:
1251       {
1252         gold_assert(gsym);
1253         elfcpp::Elf_types<64>::Elf_Addr value;
1254         value = target->got_section(NULL, NULL)->address();
1255         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1256       }
1257       break;
1258
1259     case elfcpp::R_X86_64_GOT64:
1260       // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1261       // Since we always add a PLT entry, this is equivalent.
1262     case elfcpp::R_X86_64_GOTPLT64:  // TODO(csilvers): correct?
1263       gold_assert(have_got_offset);
1264       Relocate_functions<64, false>::rela64(view, got_offset, addend);
1265       break;
1266
1267     case elfcpp::R_X86_64_GOTPC64:
1268       {
1269         gold_assert(gsym);
1270         elfcpp::Elf_types<64>::Elf_Addr value;
1271         value = target->got_section(NULL, NULL)->address();
1272         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1273       }
1274       break;
1275
1276     case elfcpp::R_X86_64_GOTOFF64:
1277       {
1278         elfcpp::Elf_types<64>::Elf_Addr value;
1279         value = (psymval->value(object, 0)
1280                  - target->got_section(NULL, NULL)->address());
1281         Relocate_functions<64, false>::rela64(view, value, addend);
1282       }
1283       break;
1284
1285     case elfcpp::R_X86_64_GOTPCREL:
1286       {
1287         gold_assert(have_got_offset);
1288         elfcpp::Elf_types<64>::Elf_Addr value;
1289         value = target->got_section(NULL, NULL)->address() + got_offset;
1290         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1291       }
1292       break;
1293
1294     case elfcpp::R_X86_64_GOTPCREL64:
1295       {
1296         gold_assert(have_got_offset);
1297         elfcpp::Elf_types<64>::Elf_Addr value;
1298         value = target->got_section(NULL, NULL)->address() + got_offset;
1299         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1300       }
1301       break;
1302
1303     case elfcpp::R_X86_64_COPY:
1304     case elfcpp::R_X86_64_GLOB_DAT:
1305     case elfcpp::R_X86_64_JUMP_SLOT:
1306     case elfcpp::R_X86_64_RELATIVE:
1307       // These are outstanding tls relocs, which are unexpected when linking
1308     case elfcpp::R_X86_64_TPOFF64:
1309     case elfcpp::R_X86_64_DTPMOD64:
1310     case elfcpp::R_X86_64_TLSDESC:
1311       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1312                              _("unexpected reloc %u in object file"),
1313                              r_type);
1314       break;
1315
1316       // These are initial tls relocs, which are expected when linking
1317     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1318     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1319     case elfcpp::R_X86_64_TLSDESC_CALL:
1320     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1321     case elfcpp::R_X86_64_DTPOFF32:
1322     case elfcpp::R_X86_64_DTPOFF64:
1323     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1324     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1325       this->relocate_tls(relinfo, relnum, rela, r_type, gsym, psymval, view,
1326                          address, view_size);
1327       break;
1328
1329     case elfcpp::R_X86_64_SIZE32:  // TODO(csilvers): correct?
1330     case elfcpp::R_X86_64_SIZE64:  // TODO(csilvers): correct?
1331     case elfcpp::R_X86_64_PLTOFF64:  // TODO(csilvers): implement me!
1332     default:
1333       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1334                              _("unsupported reloc %u"),
1335                              r_type);
1336       break;
1337     }
1338
1339   return true;
1340 }
1341
1342 // Perform a TLS relocation.
1343
1344 inline void
1345 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1346                                       size_t relnum,
1347                                       const elfcpp::Rela<64, false>& rela,
1348                                       unsigned int r_type,
1349                                       const Sized_symbol<64>* gsym,
1350                                       const Symbol_value<64>* psymval,
1351                                       unsigned char* view,
1352                                       elfcpp::Elf_types<64>::Elf_Addr,
1353                                       off_t view_size)
1354 {
1355   Output_segment* tls_segment = relinfo->layout->tls_segment();
1356   if (tls_segment == NULL)
1357     {
1358       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1359                              _("TLS reloc but no TLS segment"));
1360       return;
1361     }
1362
1363   elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1364
1365   const bool is_final = (gsym == NULL
1366                          ? !parameters->output_is_shared()
1367                          : gsym->final_value_is_known());
1368   const tls::Tls_optimization optimized_type
1369       = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1370   switch (r_type)
1371     {
1372     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1373     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1374     case elfcpp::R_X86_64_TLSDESC_CALL:
1375       if (optimized_type == tls::TLSOPT_TO_LE)
1376         {
1377           this->tls_gd_to_le(relinfo, relnum, tls_segment,
1378                              rela, r_type, value, view,
1379                              view_size);
1380           break;
1381         }
1382       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1383                              _("unsupported reloc %u"), r_type);
1384       break;
1385
1386     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1387       if (optimized_type == tls::TLSOPT_TO_LE)
1388         {
1389           this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
1390                              value, view, view_size);
1391           break;
1392         }
1393       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1394                              _("unsupported reloc %u"), r_type);
1395       break;
1396
1397     case elfcpp::R_X86_64_DTPOFF32:
1398       if (optimized_type == tls::TLSOPT_TO_LE)
1399         value = value - (tls_segment->vaddr() + tls_segment->memsz());
1400       else
1401         value = value - tls_segment->vaddr();
1402       Relocate_functions<64, false>::rel32(view, value);
1403       break;
1404
1405     case elfcpp::R_X86_64_DTPOFF64:
1406       if (optimized_type == tls::TLSOPT_TO_LE)
1407         value = value - (tls_segment->vaddr() + tls_segment->memsz());
1408       else
1409         value = value - tls_segment->vaddr();
1410       Relocate_functions<64, false>::rel64(view, value);
1411       break;
1412
1413     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1414       if (optimized_type == tls::TLSOPT_TO_LE)
1415         {
1416           Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1417                                                 rela, r_type, value, view,
1418                                                 view_size);
1419           break;
1420         }
1421       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1422                              _("unsupported reloc type %u"),
1423                              r_type);
1424       break;
1425
1426     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1427       value = value - (tls_segment->vaddr() + tls_segment->memsz());
1428       Relocate_functions<64, false>::rel32(view, value);
1429       break;
1430     }
1431 }
1432
1433 // Do a relocation in which we convert a TLS General-Dynamic to a
1434 // Local-Exec.
1435
1436 inline void
1437 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
1438                                       size_t relnum,
1439                                       Output_segment* tls_segment,
1440                                       const elfcpp::Rela<64, false>& rela,
1441                                       unsigned int,
1442                                       elfcpp::Elf_types<64>::Elf_Addr value,
1443                                       unsigned char* view,
1444                                       off_t view_size)
1445 {
1446   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1447   // .word 0x6666; rex64; call __tls_get_addr
1448   // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1449
1450   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1451   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1452
1453   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1454                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1455   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1456                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1457
1458   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1459
1460   value = value - (tls_segment->vaddr() + tls_segment->memsz());
1461   Relocate_functions<64, false>::rela32(view + 8, value, 0);
1462
1463   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1464   // We can skip it.
1465   this->skip_call_tls_get_addr_ = true;
1466 }
1467
1468 inline void
1469 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
1470                                       size_t relnum,
1471                                       Output_segment*,
1472                                       const elfcpp::Rela<64, false>& rela,
1473                                       unsigned int,
1474                                       elfcpp::Elf_types<64>::Elf_Addr,
1475                                       unsigned char* view,
1476                                       off_t view_size)
1477 {
1478   // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1479   // ... leq foo@dtpoff(%rax),%reg
1480   // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1481
1482   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1483   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
1484
1485   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1486                  view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
1487
1488   tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
1489
1490   memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1491
1492   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1493   // We can skip it.
1494   this->skip_call_tls_get_addr_ = true;
1495 }
1496
1497 // Do a relocation in which we convert a TLS Initial-Exec to a
1498 // Local-Exec.
1499
1500 inline void
1501 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
1502                                       size_t relnum,
1503                                       Output_segment* tls_segment,
1504                                       const elfcpp::Rela<64, false>& rela,
1505                                       unsigned int,
1506                                       elfcpp::Elf_types<64>::Elf_Addr value,
1507                                       unsigned char* view,
1508                                       off_t view_size)
1509 {
1510   // We need to examine the opcodes to figure out which instruction we
1511   // are looking at.
1512
1513   // movq foo@gottpoff(%rip),%reg  ==>  movq $YY,%reg
1514   // addq foo@gottpoff(%rip),%reg  ==>  addq $YY,%reg
1515
1516   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1517   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
1518
1519   unsigned char op1 = view[-3];
1520   unsigned char op2 = view[-2];
1521   unsigned char op3 = view[-1];
1522   unsigned char reg = op3 >> 3;
1523
1524   if (op2 == 0x8b)
1525     {
1526       // movq
1527       if (op1 == 0x4c)
1528         view[-3] = 0x49;
1529       view[-2] = 0xc7;
1530       view[-1] = 0xc0 | reg;
1531     }
1532   else if (reg == 4)
1533     {
1534       // Special handling for %rsp.
1535       if (op1 == 0x4c)
1536         view[-3] = 0x49;
1537       view[-2] = 0x81;
1538       view[-1] = 0xc0 | reg;
1539     }
1540   else
1541     {
1542       // addq
1543       if (op1 == 0x4c)
1544         view[-3] = 0x4d;
1545       view[-2] = 0x8d;
1546       view[-1] = 0x80 | reg | (reg << 3);
1547     }
1548
1549   value = value - (tls_segment->vaddr() + tls_segment->memsz());
1550   Relocate_functions<64, false>::rela32(view, value, 0);
1551 }
1552
1553 // Relocate section data.
1554
1555 void
1556 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1557                                 unsigned int sh_type,
1558                                 const unsigned char* prelocs,
1559                                 size_t reloc_count,
1560                                 unsigned char* view,
1561                                 elfcpp::Elf_types<64>::Elf_Addr address,
1562                                 off_t view_size)
1563 {
1564   gold_assert(sh_type == elfcpp::SHT_RELA);
1565
1566   gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1567                          Target_x86_64::Relocate>(
1568     relinfo,
1569     this,
1570     prelocs,
1571     reloc_count,
1572     view,
1573     address,
1574     view_size);
1575 }
1576
1577 // Return the value to use for a dynamic which requires special
1578 // treatment.  This is how we support equality comparisons of function
1579 // pointers across shared library boundaries, as described in the
1580 // processor specific ABI supplement.
1581
1582 uint64_t
1583 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
1584 {
1585   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1586   return this->plt_section()->address() + gsym->plt_offset();
1587 }
1588
1589 // Return a string used to fill a code section with nops to take up
1590 // the specified length.
1591
1592 std::string
1593 Target_x86_64::do_code_fill(off_t length)
1594 {
1595   if (length >= 16)
1596     {
1597       // Build a jmpq instruction to skip over the bytes.
1598       unsigned char jmp[5];
1599       jmp[0] = 0xe9;
1600       elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
1601       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1602               + std::string(length - 5, '\0'));
1603     }
1604
1605   // Nop sequences of various lengths.
1606   const char nop1[1] = { 0x90 };                   // nop
1607   const char nop2[2] = { 0x66, 0x90 };             // xchg %ax %ax
1608   const char nop3[3] = { 0x8d, 0x76, 0x00 };       // leal 0(%esi),%esi
1609   const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00};  // leal 0(%esi,1),%esi
1610   const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26,   // nop
1611                          0x00 };                   // leal 0(%esi,1),%esi
1612   const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00,   // leal 0L(%esi),%esi
1613                          0x00, 0x00 };
1614   const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00,   // leal 0L(%esi,1),%esi
1615                          0x00, 0x00, 0x00 };
1616   const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26,   // nop
1617                          0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1618   const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc,   // movl %esi,%esi
1619                          0x27, 0x00, 0x00, 0x00,   // leal 0L(%edi,1),%edi
1620                          0x00 };
1621   const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1622                            0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1623                            0x00, 0x00 };
1624   const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1625                            0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1626                            0x00, 0x00, 0x00 };
1627   const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1628                            0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1629                            0x00, 0x00, 0x00, 0x00 };
1630   const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1631                            0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1632                            0x27, 0x00, 0x00, 0x00,
1633                            0x00 };
1634   const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1635                            0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1636                            0xbc, 0x27, 0x00, 0x00,
1637                            0x00, 0x00 };
1638   const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1639                            0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1640                            0x90, 0x90, 0x90, 0x90,
1641                            0x90, 0x90, 0x90 };
1642
1643   const char* nops[16] = {
1644     NULL,
1645     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1646     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1647   };
1648
1649   return std::string(nops[length], length);
1650 }
1651
1652 // The selector for x86_64 object files.
1653
1654 class Target_selector_x86_64 : public Target_selector
1655 {
1656 public:
1657   Target_selector_x86_64()
1658     : Target_selector(elfcpp::EM_X86_64, 64, false)
1659   { }
1660
1661   Target*
1662   recognize(int machine, int osabi, int abiversion);
1663
1664  private:
1665   Target_x86_64* target_;
1666 };
1667
1668 // Recognize an x86_64 object file when we already know that the machine
1669 // number is EM_X86_64.
1670
1671 Target*
1672 Target_selector_x86_64::recognize(int, int, int)
1673 {
1674   if (this->target_ == NULL)
1675     this->target_ = new Target_x86_64();
1676   return this->target_;
1677 }
1678
1679 Target_selector_x86_64 target_selector_x86_64;
1680
1681 } // End anonymous namespace.