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