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