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