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