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