From Craig Silverstein: implement -z max-page-size and -z
[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);
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->output_is_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;
687   ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(64) (gsym
688                                                         SELECT_SIZE(64));
689
690   if (!Copy_relocs<64, false>::need_copy_reloc(options, object,
691                                                data_shndx, ssym))
692     {
693       // So far we do not need a COPY reloc.  Save this relocation.
694       // If it turns out that we never need a COPY reloc for this
695       // symbol, then we will emit the relocation.
696       if (this->copy_relocs_ == NULL)
697         this->copy_relocs_ = new Copy_relocs<64, false>();
698       this->copy_relocs_->save(ssym, object, data_shndx, output_section, rela);
699     }
700   else
701     {
702       // Allocate space for this symbol in the .bss section.
703
704       elfcpp::Elf_types<64>::Elf_WXword symsize = ssym->symsize();
705
706       // There is no defined way to determine the required alignment
707       // of the symbol.  We pick the alignment based on the size.  We
708       // set an arbitrary maximum of 256.
709       unsigned int align;
710       for (align = 1; align < 512; align <<= 1)
711         if ((symsize & align) != 0)
712           break;
713
714       if (this->dynbss_ == NULL)
715         {
716           this->dynbss_ = new Output_data_space(align);
717           layout->add_output_section_data(".bss",
718                                           elfcpp::SHT_NOBITS,
719                                           (elfcpp::SHF_ALLOC
720                                            | elfcpp::SHF_WRITE),
721                                           this->dynbss_);
722         }
723
724       Output_data_space* dynbss = this->dynbss_;
725
726       if (align > dynbss->addralign())
727         dynbss->set_space_alignment(align);
728
729       section_size_type dynbss_size = dynbss->current_data_size();
730       dynbss_size = align_address(dynbss_size, align);
731       section_size_type offset = dynbss_size;
732       dynbss->set_current_data_size(dynbss_size + symsize);
733
734       symtab->define_with_copy_reloc(ssym, dynbss, offset);
735
736       // Add the COPY reloc.
737       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
738       rela_dyn->add_global(ssym, elfcpp::R_X86_64_COPY, dynbss, offset, 0);
739     }
740 }
741
742
743 // Optimize the TLS relocation type based on what we know about the
744 // symbol.  IS_FINAL is true if the final address of this symbol is
745 // known at link time.
746
747 tls::Tls_optimization
748 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
749 {
750   // If we are generating a shared library, then we can't do anything
751   // in the linker.
752   if (parameters->output_is_shared())
753     return tls::TLSOPT_NONE;
754
755   switch (r_type)
756     {
757     case elfcpp::R_X86_64_TLSGD:
758     case elfcpp::R_X86_64_GOTPC32_TLSDESC:
759     case elfcpp::R_X86_64_TLSDESC_CALL:
760       // These are General-Dynamic which permits fully general TLS
761       // access.  Since we know that we are generating an executable,
762       // we can convert this to Initial-Exec.  If we also know that
763       // this is a local symbol, we can further switch to Local-Exec.
764       if (is_final)
765         return tls::TLSOPT_TO_LE;
766       return tls::TLSOPT_TO_IE;
767
768     case elfcpp::R_X86_64_TLSLD:
769       // This is Local-Dynamic, which refers to a local symbol in the
770       // dynamic TLS block.  Since we know that we generating an
771       // executable, we can switch to Local-Exec.
772       return tls::TLSOPT_TO_LE;
773
774     case elfcpp::R_X86_64_DTPOFF32:
775     case elfcpp::R_X86_64_DTPOFF64:
776       // Another Local-Dynamic reloc.
777       return tls::TLSOPT_TO_LE;
778
779     case elfcpp::R_X86_64_GOTTPOFF:
780       // These are Initial-Exec relocs which get the thread offset
781       // from the GOT.  If we know that we are linking against the
782       // local symbol, we can switch to Local-Exec, which links the
783       // thread offset into the instruction.
784       if (is_final)
785         return tls::TLSOPT_TO_LE;
786       return tls::TLSOPT_NONE;
787
788     case elfcpp::R_X86_64_TPOFF32:
789       // When we already have Local-Exec, there is nothing further we
790       // can do.
791       return tls::TLSOPT_NONE;
792
793     default:
794       gold_unreachable();
795     }
796 }
797
798 // Report an unsupported relocation against a local symbol.
799
800 void
801 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
802                                              unsigned int r_type)
803 {
804   gold_error(_("%s: unsupported reloc %u against local symbol"),
805              object->name().c_str(), r_type);
806 }
807
808 // Scan a relocation for a local symbol.
809
810 inline void
811 Target_x86_64::Scan::local(const General_options&,
812                            Symbol_table* symtab,
813                            Layout* layout,
814                            Target_x86_64* target,
815                            Sized_relobj<64, false>* object,
816                            unsigned int data_shndx,
817                            Output_section* output_section,
818                            const elfcpp::Rela<64, false>& reloc,
819                            unsigned int r_type,
820                            const elfcpp::Sym<64, false>& lsym)
821 {
822   switch (r_type)
823     {
824     case elfcpp::R_X86_64_NONE:
825     case elfcpp::R_386_GNU_VTINHERIT:
826     case elfcpp::R_386_GNU_VTENTRY:
827       break;
828
829     case elfcpp::R_X86_64_64:
830       // If building a shared library (or a position-independent
831       // executable), we need to create a dynamic relocation for this
832       // location.  The relocation applied at link time will apply the
833       // link-time value, so we flag the location with an
834       // R_X86_64_RELATIVE relocation so the dynamic loader can
835       // relocate it easily.
836       if (parameters->output_is_position_independent())
837         {
838           unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
839           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
840           rela_dyn->add_local_relative(object, r_sym,
841                                        elfcpp::R_X86_64_RELATIVE,
842                                        output_section, data_shndx,
843                                        reloc.get_r_offset(),
844                                        reloc.get_r_addend());
845         }
846       break;
847
848     case elfcpp::R_X86_64_32:
849     case elfcpp::R_X86_64_32S:
850     case elfcpp::R_X86_64_16:
851     case elfcpp::R_X86_64_8:
852       // If building a shared library (or a position-independent
853       // executable), we need to create a dynamic relocation for this
854       // location.  We can't use an R_X86_64_RELATIVE relocation
855       // because that is always a 64-bit relocation.
856       if (parameters->output_is_position_independent())
857         {
858           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
859           if (lsym.get_st_type() != elfcpp::STT_SECTION)
860             {
861               unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
862               rela_dyn->add_local(object, r_sym, r_type, output_section,
863                                   data_shndx, reloc.get_r_offset(),
864                                   reloc.get_r_addend());
865             }
866           else
867             {
868               gold_assert(lsym.get_st_value() == 0);
869               rela_dyn->add_local_section(object, lsym.get_st_shndx(),
870                                           r_type, output_section,
871                                           data_shndx, reloc.get_r_offset(),
872                                           reloc.get_r_addend());
873             }
874         }
875       break;
876
877     case elfcpp::R_X86_64_PC64:
878     case elfcpp::R_X86_64_PC32:
879     case elfcpp::R_X86_64_PC16:
880     case elfcpp::R_X86_64_PC8:
881       break;
882
883     case elfcpp::R_X86_64_PLT32:
884       // Since we know this is a local symbol, we can handle this as a
885       // PC32 reloc.
886       break;
887
888     case elfcpp::R_X86_64_GOTPC32:
889     case elfcpp::R_X86_64_GOTOFF64:
890     case elfcpp::R_X86_64_GOTPC64:
891     case elfcpp::R_X86_64_PLTOFF64:
892       // We need a GOT section.
893       target->got_section(symtab, layout);
894       // For PLTOFF64, we'd normally want a PLT section, but since we
895       // know this is a local symbol, no PLT is needed.
896       break;
897
898     case elfcpp::R_X86_64_GOT64:
899     case elfcpp::R_X86_64_GOT32:
900     case elfcpp::R_X86_64_GOTPCREL64:
901     case elfcpp::R_X86_64_GOTPCREL:
902     case elfcpp::R_X86_64_GOTPLT64:
903       {
904         // The symbol requires a GOT entry.
905         Output_data_got<64, false>* got = target->got_section(symtab, layout);
906         unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
907         if (got->add_local(object, r_sym))
908           {
909             // If we are generating a shared object, we need to add a
910             // dynamic relocation for this symbol's GOT entry.
911             if (parameters->output_is_position_independent())
912               {
913                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
914                 // R_X86_64_RELATIVE assumes a 64-bit relocation.
915                 if (r_type != elfcpp::R_X86_64_GOT32)
916                   rela_dyn->add_local_relative(object, r_sym,
917                                                elfcpp::R_X86_64_RELATIVE, got,
918                                                object->local_got_offset(r_sym),
919                                                0);
920                 else
921                   {
922                     gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
923                     rela_dyn->add_local(object, r_sym, r_type,
924                                         got, object->local_got_offset(r_sym),
925                                         0);
926                   }
927               }
928           }
929         // For GOTPLT64, we'd normally want a PLT section, but since
930         // we know this is a local symbol, no PLT is needed.
931       }
932       break;
933
934     case elfcpp::R_X86_64_COPY:
935     case elfcpp::R_X86_64_GLOB_DAT:
936     case elfcpp::R_X86_64_JUMP_SLOT:
937     case elfcpp::R_X86_64_RELATIVE:
938       // These are outstanding tls relocs, which are unexpected when linking
939     case elfcpp::R_X86_64_TPOFF64:
940     case elfcpp::R_X86_64_DTPMOD64:
941     case elfcpp::R_X86_64_TLSDESC:
942       gold_error(_("%s: unexpected reloc %u in object file"),
943                  object->name().c_str(), r_type);
944       break;
945
946       // These are initial tls relocs, which are expected when linking
947     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
948     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
949     case elfcpp::R_X86_64_TLSDESC_CALL:
950     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
951     case elfcpp::R_X86_64_DTPOFF32:
952     case elfcpp::R_X86_64_DTPOFF64:
953     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
954     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
955       {
956         bool output_is_shared = parameters->output_is_shared();
957         const tls::Tls_optimization optimized_type
958             = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
959         switch (r_type)
960           {
961           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
962             if (optimized_type == tls::TLSOPT_NONE)
963               {
964                 // Create a pair of GOT entries for the module index and
965                 // dtv-relative offset.
966                 Output_data_got<64, false>* got
967                     = target->got_section(symtab, layout);
968                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
969                 got->add_local_tls_with_rela(object, r_sym,
970                                              lsym.get_st_shndx(), true,
971                                              target->rela_dyn_section(layout),
972                                              elfcpp::R_X86_64_DTPMOD64);
973               }
974             else if (optimized_type != tls::TLSOPT_TO_LE)
975               unsupported_reloc_local(object, r_type);
976             break;
977
978           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
979           case elfcpp::R_X86_64_TLSDESC_CALL:
980             // FIXME: If not relaxing to LE, we need to generate
981             // a GOT entry with a R_x86_64_TLSDESC reloc.
982             if (optimized_type != tls::TLSOPT_TO_LE)
983               unsupported_reloc_local(object, r_type);
984             break;
985
986           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
987             if (optimized_type == tls::TLSOPT_NONE)
988               {
989                 // Create a GOT entry for the module index.
990                 target->got_mod_index_entry(symtab, layout, object);
991               }
992             else if (optimized_type != tls::TLSOPT_TO_LE)
993               unsupported_reloc_local(object, r_type);
994             break;
995
996           case elfcpp::R_X86_64_DTPOFF32:
997           case elfcpp::R_X86_64_DTPOFF64:
998             break;
999
1000           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
1001             layout->set_has_static_tls();
1002             if (optimized_type == tls::TLSOPT_NONE)
1003               {
1004                 // Create a GOT entry for the tp-relative offset.
1005                 Output_data_got<64, false>* got
1006                     = target->got_section(symtab, layout);
1007                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1008                 got->add_local_with_rela(object, r_sym,
1009                                          target->rela_dyn_section(layout),
1010                                          elfcpp::R_X86_64_TPOFF64);
1011               }
1012             else if (optimized_type != tls::TLSOPT_TO_LE)
1013               unsupported_reloc_local(object, r_type);
1014             break;
1015
1016           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
1017             layout->set_has_static_tls();
1018             if (output_is_shared)
1019               unsupported_reloc_local(object, r_type);
1020             break;
1021
1022           default:
1023             gold_unreachable();
1024           }
1025       }
1026       break;
1027
1028     case elfcpp::R_X86_64_SIZE32:
1029     case elfcpp::R_X86_64_SIZE64:
1030     default:
1031       gold_error(_("%s: unsupported reloc %u against local symbol"),
1032                  object->name().c_str(), r_type);
1033       break;
1034     }
1035 }
1036
1037
1038 // Report an unsupported relocation against a global symbol.
1039
1040 void
1041 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1042                                               unsigned int r_type,
1043                                               Symbol* gsym)
1044 {
1045   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1046              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1047 }
1048
1049 // Scan a relocation for a global symbol.
1050
1051 inline void
1052 Target_x86_64::Scan::global(const General_options& options,
1053                             Symbol_table* symtab,
1054                             Layout* layout,
1055                             Target_x86_64* target,
1056                             Sized_relobj<64, false>* object,
1057                             unsigned int data_shndx,
1058                             Output_section* output_section,
1059                             const elfcpp::Rela<64, false>& reloc,
1060                             unsigned int r_type,
1061                             Symbol* gsym)
1062 {
1063   switch (r_type)
1064     {
1065     case elfcpp::R_X86_64_NONE:
1066     case elfcpp::R_386_GNU_VTINHERIT:
1067     case elfcpp::R_386_GNU_VTENTRY:
1068       break;
1069
1070     case elfcpp::R_X86_64_64:
1071     case elfcpp::R_X86_64_32:
1072     case elfcpp::R_X86_64_32S:
1073     case elfcpp::R_X86_64_16:
1074     case elfcpp::R_X86_64_8:
1075       {
1076         // Make a PLT entry if necessary.
1077         if (gsym->needs_plt_entry())
1078           {
1079             target->make_plt_entry(symtab, layout, gsym);
1080             // Since this is not a PC-relative relocation, we may be
1081             // taking the address of a function. In that case we need to
1082             // set the entry in the dynamic symbol table to the address of
1083             // the PLT entry.
1084             if (gsym->is_from_dynobj() && !parameters->output_is_shared())
1085               gsym->set_needs_dynsym_value();
1086           }
1087         // Make a dynamic relocation if necessary.
1088         if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1089           {
1090             if (target->may_need_copy_reloc(gsym))
1091               {
1092                 target->copy_reloc(&options, symtab, layout, object,
1093                                    data_shndx, output_section, gsym, reloc);
1094               }
1095             else if (r_type == elfcpp::R_X86_64_64
1096                      && gsym->can_use_relative_reloc(false))
1097               {
1098                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1099                 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1100                                               output_section, object,
1101                                               data_shndx, reloc.get_r_offset(),
1102                                               reloc.get_r_addend());
1103               }
1104             else
1105               {
1106                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1107                 rela_dyn->add_global(gsym, r_type, output_section, object,
1108                                      data_shndx, reloc.get_r_offset(),
1109                                      reloc.get_r_addend());
1110               }
1111           }
1112       }
1113       break;
1114
1115     case elfcpp::R_X86_64_PC64:
1116     case elfcpp::R_X86_64_PC32:
1117     case elfcpp::R_X86_64_PC16:
1118     case elfcpp::R_X86_64_PC8:
1119       {
1120         // Make a PLT entry if necessary.
1121         if (gsym->needs_plt_entry())
1122           target->make_plt_entry(symtab, layout, gsym);
1123         // Make a dynamic relocation if necessary.
1124         int flags = Symbol::NON_PIC_REF;
1125         if (gsym->type() == elfcpp::STT_FUNC)
1126           flags |= Symbol::FUNCTION_CALL;
1127         if (gsym->needs_dynamic_reloc(flags))
1128           {
1129             if (target->may_need_copy_reloc(gsym))
1130               {
1131                 target->copy_reloc(&options, symtab, layout, object,
1132                                    data_shndx, output_section, gsym, reloc);
1133               }
1134             else
1135               {
1136                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1137                 rela_dyn->add_global(gsym, r_type, output_section, object,
1138                                      data_shndx, reloc.get_r_offset(),
1139                                      reloc.get_r_addend());
1140               }
1141           }
1142       }
1143       break;
1144
1145     case elfcpp::R_X86_64_GOT64:
1146     case elfcpp::R_X86_64_GOT32:
1147     case elfcpp::R_X86_64_GOTPCREL64:
1148     case elfcpp::R_X86_64_GOTPCREL:
1149     case elfcpp::R_X86_64_GOTPLT64:
1150       {
1151         // The symbol requires a GOT entry.
1152         Output_data_got<64, false>* got = target->got_section(symtab, layout);
1153         if (gsym->final_value_is_known())
1154           got->add_global(gsym);
1155         else
1156           {
1157             // If this symbol is not fully resolved, we need to add a
1158             // dynamic relocation for it.
1159             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1160             if (gsym->is_from_dynobj()
1161                 || gsym->is_undefined()
1162                 || gsym->is_preemptible())
1163               got->add_global_with_rela(gsym, rela_dyn,
1164                                         elfcpp::R_X86_64_GLOB_DAT);
1165             else
1166               {
1167                 if (got->add_global(gsym))
1168                   rela_dyn->add_global_relative(gsym,
1169                                                 elfcpp::R_X86_64_RELATIVE,
1170                                                 got, gsym->got_offset(), 0);
1171               }
1172           }
1173         // For GOTPLT64, we also need a PLT entry (but only if the
1174         // symbol is not fully resolved).
1175         if (r_type == elfcpp::R_X86_64_GOTPLT64
1176             && !gsym->final_value_is_known())
1177           target->make_plt_entry(symtab, layout, gsym);
1178       }
1179       break;
1180
1181     case elfcpp::R_X86_64_PLT32:
1182       // If the symbol is fully resolved, this is just a PC32 reloc.
1183       // Otherwise we need a PLT entry.
1184       if (gsym->final_value_is_known())
1185         break;
1186       // If building a shared library, we can also skip the PLT entry
1187       // if the symbol is defined in the output file and is protected
1188       // or hidden.
1189       if (gsym->is_defined()
1190           && !gsym->is_from_dynobj()
1191           && !gsym->is_preemptible())
1192         break;
1193       target->make_plt_entry(symtab, layout, gsym);
1194       break;
1195
1196     case elfcpp::R_X86_64_GOTPC32:
1197     case elfcpp::R_X86_64_GOTOFF64:
1198     case elfcpp::R_X86_64_GOTPC64:
1199     case elfcpp::R_X86_64_PLTOFF64:
1200       // We need a GOT section.
1201       target->got_section(symtab, layout);
1202       // For PLTOFF64, we also need a PLT entry (but only if the
1203       // symbol is not fully resolved).
1204       if (r_type == elfcpp::R_X86_64_PLTOFF64
1205           && !gsym->final_value_is_known())
1206         target->make_plt_entry(symtab, layout, gsym);
1207       break;
1208
1209     case elfcpp::R_X86_64_COPY:
1210     case elfcpp::R_X86_64_GLOB_DAT:
1211     case elfcpp::R_X86_64_JUMP_SLOT:
1212     case elfcpp::R_X86_64_RELATIVE:
1213       // These are outstanding tls relocs, which are unexpected when linking
1214     case elfcpp::R_X86_64_TPOFF64:
1215     case elfcpp::R_X86_64_DTPMOD64:
1216     case elfcpp::R_X86_64_TLSDESC:
1217       gold_error(_("%s: unexpected reloc %u in object file"),
1218                  object->name().c_str(), r_type);
1219       break;
1220
1221       // These are initial tls relocs, which are expected for global()
1222     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1223     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1224     case elfcpp::R_X86_64_TLSDESC_CALL:
1225     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1226     case elfcpp::R_X86_64_DTPOFF32:
1227     case elfcpp::R_X86_64_DTPOFF64:
1228     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1229     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1230       {
1231         const bool is_final = gsym->final_value_is_known();
1232         const tls::Tls_optimization optimized_type
1233             = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1234         switch (r_type)
1235           {
1236           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
1237             if (optimized_type == tls::TLSOPT_NONE)
1238               {
1239                 // Create a pair of GOT entries for the module index and
1240                 // dtv-relative offset.
1241                 Output_data_got<64, false>* got
1242                     = target->got_section(symtab, layout);
1243                 got->add_global_tls_with_rela(gsym,
1244                                               target->rela_dyn_section(layout),
1245                                               elfcpp::R_X86_64_DTPMOD64,
1246                                               elfcpp::R_X86_64_DTPOFF64);
1247               }
1248             else if (optimized_type == tls::TLSOPT_TO_IE)
1249               {
1250                 // Create a GOT entry for the tp-relative offset.
1251                 Output_data_got<64, false>* got
1252                     = target->got_section(symtab, layout);
1253                 got->add_global_with_rela(gsym,
1254                                           target->rela_dyn_section(layout),
1255                                           elfcpp::R_X86_64_TPOFF64);
1256               }
1257             else if (optimized_type != tls::TLSOPT_TO_LE)
1258               unsupported_reloc_global(object, r_type, gsym);
1259             break;
1260
1261           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1262           case elfcpp::R_X86_64_TLSDESC_CALL:
1263             // FIXME: If not relaxing to LE, we need to generate
1264             // DTPMOD64 and DTPOFF64, or TLSDESC, relocs.
1265             if (optimized_type != tls::TLSOPT_TO_LE)
1266               unsupported_reloc_global(object, r_type, gsym);
1267             break;
1268
1269           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
1270             if (optimized_type == tls::TLSOPT_NONE)
1271               {
1272                 // Create a GOT entry for the module index.
1273                 target->got_mod_index_entry(symtab, layout, object);
1274               }
1275             else if (optimized_type != tls::TLSOPT_TO_LE)
1276               unsupported_reloc_global(object, r_type, gsym);
1277             break;
1278
1279           case elfcpp::R_X86_64_DTPOFF32:
1280           case elfcpp::R_X86_64_DTPOFF64:
1281             break;
1282
1283           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
1284             layout->set_has_static_tls();
1285             if (optimized_type == tls::TLSOPT_NONE)
1286               {
1287                 // Create a GOT entry for the tp-relative offset.
1288                 Output_data_got<64, false>* got
1289                     = target->got_section(symtab, layout);
1290                 got->add_global_with_rela(gsym,
1291                                           target->rela_dyn_section(layout),
1292                                           elfcpp::R_X86_64_TPOFF64);
1293               }
1294             else if (optimized_type != tls::TLSOPT_TO_LE)
1295               unsupported_reloc_global(object, r_type, gsym);
1296             break;
1297
1298           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
1299             layout->set_has_static_tls();
1300             if (parameters->output_is_shared())
1301               unsupported_reloc_local(object, r_type);
1302             break;
1303
1304           default:
1305             gold_unreachable();
1306           }
1307       }
1308       break;
1309
1310     case elfcpp::R_X86_64_SIZE32:
1311     case elfcpp::R_X86_64_SIZE64:
1312     default:
1313       gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1314                  object->name().c_str(), r_type,
1315                  gsym->demangled_name().c_str());
1316       break;
1317     }
1318 }
1319
1320 // Scan relocations for a section.
1321
1322 void
1323 Target_x86_64::scan_relocs(const General_options& options,
1324                            Symbol_table* symtab,
1325                            Layout* layout,
1326                            Sized_relobj<64, false>* object,
1327                            unsigned int data_shndx,
1328                            unsigned int sh_type,
1329                            const unsigned char* prelocs,
1330                            size_t reloc_count,
1331                            Output_section* output_section,
1332                            bool needs_special_offset_handling,
1333                            size_t local_symbol_count,
1334                            const unsigned char* plocal_symbols)
1335 {
1336   if (sh_type == elfcpp::SHT_REL)
1337     {
1338       gold_error(_("%s: unsupported REL reloc section"),
1339                  object->name().c_str());
1340       return;
1341     }
1342
1343   gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1344       Target_x86_64::Scan>(
1345     options,
1346     symtab,
1347     layout,
1348     this,
1349     object,
1350     data_shndx,
1351     prelocs,
1352     reloc_count,
1353     output_section,
1354     needs_special_offset_handling,
1355     local_symbol_count,
1356     plocal_symbols);
1357 }
1358
1359 // Finalize the sections.
1360
1361 void
1362 Target_x86_64::do_finalize_sections(Layout* layout)
1363 {
1364   // Fill in some more dynamic tags.
1365   Output_data_dynamic* const odyn = layout->dynamic_data();
1366   if (odyn != NULL)
1367     {
1368       if (this->got_plt_ != NULL)
1369         odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1370
1371       if (this->plt_ != NULL)
1372         {
1373           const Output_data* od = this->plt_->rel_plt();
1374           odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1375           odyn->add_section_address(elfcpp::DT_JMPREL, od);
1376           odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
1377         }
1378
1379       if (this->rela_dyn_ != NULL)
1380         {
1381           const Output_data* od = this->rela_dyn_;
1382           odyn->add_section_address(elfcpp::DT_RELA, od);
1383           odyn->add_section_size(elfcpp::DT_RELASZ, od);
1384           odyn->add_constant(elfcpp::DT_RELAENT,
1385                              elfcpp::Elf_sizes<64>::rela_size);
1386         }
1387
1388       if (!parameters->output_is_shared())
1389         {
1390           // The value of the DT_DEBUG tag is filled in by the dynamic
1391           // linker at run time, and used by the debugger.
1392           odyn->add_constant(elfcpp::DT_DEBUG, 0);
1393         }
1394     }
1395
1396   // Emit any relocs we saved in an attempt to avoid generating COPY
1397   // relocs.
1398   if (this->copy_relocs_ == NULL)
1399     return;
1400   if (this->copy_relocs_->any_to_emit())
1401     {
1402       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1403       this->copy_relocs_->emit(rela_dyn);
1404     }
1405   delete this->copy_relocs_;
1406   this->copy_relocs_ = NULL;
1407 }
1408
1409 // Perform a relocation.
1410
1411 inline bool
1412 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1413                                   Target_x86_64* target,
1414                                   size_t relnum,
1415                                   const elfcpp::Rela<64, false>& rela,
1416                                   unsigned int r_type,
1417                                   const Sized_symbol<64>* gsym,
1418                                   const Symbol_value<64>* psymval,
1419                                   unsigned char* view,
1420                                   elfcpp::Elf_types<64>::Elf_Addr address,
1421                                   section_size_type view_size)
1422 {
1423   if (this->skip_call_tls_get_addr_)
1424     {
1425       if (r_type != elfcpp::R_X86_64_PLT32
1426           || gsym == NULL
1427           || strcmp(gsym->name(), "__tls_get_addr") != 0)
1428         {
1429           gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1430                                  _("missing expected TLS relocation"));
1431         }
1432       else
1433         {
1434           this->skip_call_tls_get_addr_ = false;
1435           return false;
1436         }
1437     }
1438
1439   // Pick the value to use for symbols defined in shared objects.
1440   Symbol_value<64> symval;
1441   if (gsym != NULL
1442       && (gsym->is_from_dynobj()
1443           || (parameters->output_is_shared()
1444               && (gsym->is_undefined() || gsym->is_preemptible())))
1445       && gsym->has_plt_offset())
1446     {
1447       symval.set_output_value(target->plt_section()->address()
1448                               + gsym->plt_offset());
1449       psymval = &symval;
1450     }
1451
1452   const Sized_relobj<64, false>* object = relinfo->object;
1453   const elfcpp::Elf_Xword addend = rela.get_r_addend();
1454
1455   // Get the GOT offset if needed.
1456   // The GOT pointer points to the end of the GOT section.
1457   // We need to subtract the size of the GOT section to get
1458   // the actual offset to use in the relocation.
1459   bool have_got_offset = false;
1460   unsigned int got_offset = 0;
1461   switch (r_type)
1462     {
1463     case elfcpp::R_X86_64_GOT32:
1464     case elfcpp::R_X86_64_GOT64:
1465     case elfcpp::R_X86_64_GOTPLT64:
1466     case elfcpp::R_X86_64_GOTPCREL:
1467     case elfcpp::R_X86_64_GOTPCREL64:
1468       if (gsym != NULL)
1469         {
1470           gold_assert(gsym->has_got_offset());
1471           got_offset = gsym->got_offset() - target->got_size();
1472         }
1473       else
1474         {
1475           unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1476           gold_assert(object->local_has_got_offset(r_sym));
1477           got_offset = object->local_got_offset(r_sym) - target->got_size();
1478         }
1479       have_got_offset = true;
1480       break;
1481
1482     default:
1483       break;
1484     }
1485
1486   switch (r_type)
1487     {
1488     case elfcpp::R_X86_64_NONE:
1489     case elfcpp::R_386_GNU_VTINHERIT:
1490     case elfcpp::R_386_GNU_VTENTRY:
1491       break;
1492
1493     case elfcpp::R_X86_64_64:
1494       Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1495       break;
1496
1497     case elfcpp::R_X86_64_PC64:
1498       Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1499                                               address);
1500       break;
1501
1502     case elfcpp::R_X86_64_32:
1503       // FIXME: we need to verify that value + addend fits into 32 bits:
1504       //    uint64_t x = value + addend;
1505       //    x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1506       // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
1507       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1508       break;
1509
1510     case elfcpp::R_X86_64_32S:
1511       // FIXME: we need to verify that value + addend fits into 32 bits:
1512       //    int64_t x = value + addend;   // note this quantity is signed!
1513       //    x == static_cast<int64_t>(static_cast<int32_t>(x))
1514       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1515       break;
1516
1517     case elfcpp::R_X86_64_PC32:
1518       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1519                                               address);
1520       break;
1521
1522     case elfcpp::R_X86_64_16:
1523       Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1524       break;
1525
1526     case elfcpp::R_X86_64_PC16:
1527       Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1528                                               address);
1529       break;
1530
1531     case elfcpp::R_X86_64_8:
1532       Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1533       break;
1534
1535     case elfcpp::R_X86_64_PC8:
1536       Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1537                                              address);
1538       break;
1539
1540     case elfcpp::R_X86_64_PLT32:
1541       gold_assert(gsym == NULL
1542                   || gsym->has_plt_offset()
1543                   || gsym->final_value_is_known()
1544                   || (gsym->is_defined()
1545                       && !gsym->is_from_dynobj()
1546                       && !gsym->is_preemptible()));
1547       // Note: while this code looks the same as for R_X86_64_PC32, it
1548       // behaves differently because psymval was set to point to
1549       // the PLT entry, rather than the symbol, in Scan::global().
1550       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1551                                               address);
1552       break;
1553
1554     case elfcpp::R_X86_64_PLTOFF64:
1555       {
1556         gold_assert(gsym);
1557         gold_assert(gsym->has_plt_offset()
1558                     || gsym->final_value_is_known());
1559         elfcpp::Elf_types<64>::Elf_Addr got_address;
1560         got_address = target->got_section(NULL, NULL)->address();
1561         Relocate_functions<64, false>::rela64(view, object, psymval,
1562                                               addend - got_address);
1563       }
1564
1565     case elfcpp::R_X86_64_GOT32:
1566       gold_assert(have_got_offset);
1567       Relocate_functions<64, false>::rela32(view, got_offset, addend);
1568       break;
1569
1570     case elfcpp::R_X86_64_GOTPC32:
1571       {
1572         gold_assert(gsym);
1573         elfcpp::Elf_types<64>::Elf_Addr value;
1574         value = target->got_plt_section()->address();
1575         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1576       }
1577       break;
1578
1579     case elfcpp::R_X86_64_GOT64:
1580       // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1581       // Since we always add a PLT entry, this is equivalent.
1582     case elfcpp::R_X86_64_GOTPLT64:
1583       gold_assert(have_got_offset);
1584       Relocate_functions<64, false>::rela64(view, got_offset, addend);
1585       break;
1586
1587     case elfcpp::R_X86_64_GOTPC64:
1588       {
1589         gold_assert(gsym);
1590         elfcpp::Elf_types<64>::Elf_Addr value;
1591         value = target->got_plt_section()->address();
1592         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1593       }
1594       break;
1595
1596     case elfcpp::R_X86_64_GOTOFF64:
1597       {
1598         elfcpp::Elf_types<64>::Elf_Addr value;
1599         value = (psymval->value(object, 0)
1600                  - target->got_plt_section()->address());
1601         Relocate_functions<64, false>::rela64(view, value, addend);
1602       }
1603       break;
1604
1605     case elfcpp::R_X86_64_GOTPCREL:
1606       {
1607         gold_assert(have_got_offset);
1608         elfcpp::Elf_types<64>::Elf_Addr value;
1609         value = target->got_plt_section()->address() + got_offset;
1610         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1611       }
1612       break;
1613
1614     case elfcpp::R_X86_64_GOTPCREL64:
1615       {
1616         gold_assert(have_got_offset);
1617         elfcpp::Elf_types<64>::Elf_Addr value;
1618         value = target->got_plt_section()->address() + got_offset;
1619         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1620       }
1621       break;
1622
1623     case elfcpp::R_X86_64_COPY:
1624     case elfcpp::R_X86_64_GLOB_DAT:
1625     case elfcpp::R_X86_64_JUMP_SLOT:
1626     case elfcpp::R_X86_64_RELATIVE:
1627       // These are outstanding tls relocs, which are unexpected when linking
1628     case elfcpp::R_X86_64_TPOFF64:
1629     case elfcpp::R_X86_64_DTPMOD64:
1630     case elfcpp::R_X86_64_TLSDESC:
1631       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1632                              _("unexpected reloc %u in object file"),
1633                              r_type);
1634       break;
1635
1636       // These are initial tls relocs, which are expected when linking
1637     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1638     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1639     case elfcpp::R_X86_64_TLSDESC_CALL:
1640     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1641     case elfcpp::R_X86_64_DTPOFF32:
1642     case elfcpp::R_X86_64_DTPOFF64:
1643     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1644     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1645       this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1646                          view, address, view_size);
1647       break;
1648
1649     case elfcpp::R_X86_64_SIZE32:
1650     case elfcpp::R_X86_64_SIZE64:
1651     default:
1652       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1653                              _("unsupported reloc %u"),
1654                              r_type);
1655       break;
1656     }
1657
1658   return true;
1659 }
1660
1661 // Perform a TLS relocation.
1662
1663 inline void
1664 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
1665                                       Target_x86_64* target,
1666                                       size_t relnum,
1667                                       const elfcpp::Rela<64, false>& rela,
1668                                       unsigned int r_type,
1669                                       const Sized_symbol<64>* gsym,
1670                                       const Symbol_value<64>* psymval,
1671                                       unsigned char* view,
1672                                       elfcpp::Elf_types<64>::Elf_Addr address,
1673                                       section_size_type view_size)
1674 {
1675   Output_segment* tls_segment = relinfo->layout->tls_segment();
1676
1677   const Sized_relobj<64, false>* object = relinfo->object;
1678   const elfcpp::Elf_Xword addend = rela.get_r_addend();
1679
1680   elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1681
1682   const bool is_final = (gsym == NULL
1683                          ? !parameters->output_is_position_independent()
1684                          : gsym->final_value_is_known());
1685   const tls::Tls_optimization optimized_type
1686       = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1687   switch (r_type)
1688     {
1689     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1690     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1691     case elfcpp::R_X86_64_TLSDESC_CALL:
1692       if (optimized_type == tls::TLSOPT_TO_LE)
1693         {
1694           gold_assert(tls_segment != NULL);
1695           this->tls_gd_to_le(relinfo, relnum, tls_segment,
1696                              rela, r_type, value, view,
1697                              view_size);
1698           break;
1699         }
1700       else
1701         {
1702           unsigned int got_offset;
1703           if (gsym != NULL)
1704             {
1705               gold_assert(gsym->has_tls_got_offset(true));
1706               got_offset = gsym->tls_got_offset(true) - target->got_size();
1707             }
1708           else
1709             {
1710               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1711               gold_assert(object->local_has_tls_got_offset(r_sym, true));
1712               got_offset = (object->local_tls_got_offset(r_sym, true)
1713                             - target->got_size());
1714             }
1715           if (optimized_type == tls::TLSOPT_TO_IE)
1716             {
1717               gold_assert(tls_segment != NULL);
1718               this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
1719                                  got_offset, view, view_size);
1720               break;
1721             }
1722           else if (optimized_type == tls::TLSOPT_NONE)
1723             {
1724               // Relocate the field with the offset of the pair of GOT
1725               // entries.
1726               value = target->got_plt_section()->address() + got_offset;
1727               Relocate_functions<64, false>::pcrela32(view, value, addend,
1728                                                       address);
1729               break;
1730             }
1731         }
1732       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1733                              _("unsupported reloc %u"), r_type);
1734       break;
1735
1736     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1737       if (optimized_type == tls::TLSOPT_TO_LE)
1738         {
1739           gold_assert(tls_segment != NULL);
1740           this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
1741                              value, view, view_size);
1742           break;
1743         }
1744       else if (optimized_type == tls::TLSOPT_NONE)
1745         {
1746           // Relocate the field with the offset of the GOT entry for
1747           // the module index.
1748           unsigned int got_offset;
1749           got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1750                         - target->got_size());
1751           value = target->got_plt_section()->address() + got_offset;
1752           Relocate_functions<64, false>::pcrela32(view, value, addend,
1753                                                   address);
1754           break;
1755         }
1756       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1757                              _("unsupported reloc %u"), r_type);
1758       break;
1759
1760     case elfcpp::R_X86_64_DTPOFF32:
1761       gold_assert(tls_segment != NULL);
1762       if (optimized_type == tls::TLSOPT_TO_LE)
1763         value -= tls_segment->memsz();
1764       Relocate_functions<64, false>::rela32(view, value, 0);
1765       break;
1766
1767     case elfcpp::R_X86_64_DTPOFF64:
1768       gold_assert(tls_segment != NULL);
1769       if (optimized_type == tls::TLSOPT_TO_LE)
1770         value -= tls_segment->memsz();
1771       Relocate_functions<64, false>::rela64(view, value, 0);
1772       break;
1773
1774     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1775       if (optimized_type == tls::TLSOPT_TO_LE)
1776         {
1777           gold_assert(tls_segment != NULL);
1778           Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1779                                                 rela, r_type, value, view,
1780                                                 view_size);
1781           break;
1782         }
1783       else if (optimized_type == tls::TLSOPT_NONE)
1784         {
1785           // Relocate the field with the offset of the GOT entry for
1786           // the tp-relative offset of the symbol.
1787           unsigned int got_offset;
1788           if (gsym != NULL)
1789             {
1790               gold_assert(gsym->has_got_offset());
1791               got_offset = gsym->got_offset() - target->got_size();
1792             }
1793           else
1794             {
1795               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1796               gold_assert(object->local_has_got_offset(r_sym));
1797               got_offset = (object->local_got_offset(r_sym)
1798                             - target->got_size());
1799             }
1800           value = target->got_plt_section()->address() + got_offset;
1801           Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1802           break;
1803         }
1804       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1805                              _("unsupported reloc type %u"),
1806                              r_type);
1807       break;
1808
1809     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1810       value -= tls_segment->memsz();
1811       Relocate_functions<64, false>::rela32(view, value, 0);
1812       break;
1813     }
1814 }
1815
1816 // Do a relocation in which we convert a TLS General-Dynamic to an
1817 // Initial-Exec.
1818
1819 inline void
1820 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
1821                                       size_t relnum,
1822                                       Output_segment* tls_segment,
1823                                       const elfcpp::Rela<64, false>& rela,
1824                                       unsigned int,
1825                                       elfcpp::Elf_types<64>::Elf_Addr value,
1826                                       unsigned char* view,
1827                                       section_size_type view_size)
1828 {
1829   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1830   // .word 0x6666; rex64; call __tls_get_addr
1831   // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
1832
1833   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1834   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1835
1836   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1837                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1838   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1839                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1840
1841   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
1842
1843   value -= tls_segment->memsz();
1844   Relocate_functions<64, false>::rela32(view + 8, value, 0);
1845
1846   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1847   // We can skip it.
1848   this->skip_call_tls_get_addr_ = true;
1849 }
1850
1851 // Do a relocation in which we convert a TLS General-Dynamic to a
1852 // Local-Exec.
1853
1854 inline void
1855 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
1856                                       size_t relnum,
1857                                       Output_segment* tls_segment,
1858                                       const elfcpp::Rela<64, false>& rela,
1859                                       unsigned int,
1860                                       elfcpp::Elf_types<64>::Elf_Addr value,
1861                                       unsigned char* view,
1862                                       section_size_type view_size)
1863 {
1864   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
1865   // .word 0x6666; rex64; call __tls_get_addr
1866   // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
1867
1868   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
1869   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
1870
1871   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1872                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
1873   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1874                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
1875
1876   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
1877
1878   value -= tls_segment->memsz();
1879   Relocate_functions<64, false>::rela32(view + 8, value, 0);
1880
1881   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1882   // We can skip it.
1883   this->skip_call_tls_get_addr_ = true;
1884 }
1885
1886 inline void
1887 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
1888                                       size_t relnum,
1889                                       Output_segment*,
1890                                       const elfcpp::Rela<64, false>& rela,
1891                                       unsigned int,
1892                                       elfcpp::Elf_types<64>::Elf_Addr,
1893                                       unsigned char* view,
1894                                       section_size_type view_size)
1895 {
1896   // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
1897   // ... leq foo@dtpoff(%rax),%reg
1898   // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
1899
1900   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1901   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
1902
1903   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
1904                  view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
1905
1906   tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
1907
1908   memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
1909
1910   // The next reloc should be a PLT32 reloc against __tls_get_addr.
1911   // We can skip it.
1912   this->skip_call_tls_get_addr_ = true;
1913 }
1914
1915 // Do a relocation in which we convert a TLS Initial-Exec to a
1916 // Local-Exec.
1917
1918 inline void
1919 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
1920                                       size_t relnum,
1921                                       Output_segment* tls_segment,
1922                                       const elfcpp::Rela<64, false>& rela,
1923                                       unsigned int,
1924                                       elfcpp::Elf_types<64>::Elf_Addr value,
1925                                       unsigned char* view,
1926                                       section_size_type view_size)
1927 {
1928   // We need to examine the opcodes to figure out which instruction we
1929   // are looking at.
1930
1931   // movq foo@gottpoff(%rip),%reg  ==>  movq $YY,%reg
1932   // addq foo@gottpoff(%rip),%reg  ==>  addq $YY,%reg
1933
1934   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
1935   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
1936
1937   unsigned char op1 = view[-3];
1938   unsigned char op2 = view[-2];
1939   unsigned char op3 = view[-1];
1940   unsigned char reg = op3 >> 3;
1941
1942   if (op2 == 0x8b)
1943     {
1944       // movq
1945       if (op1 == 0x4c)
1946         view[-3] = 0x49;
1947       view[-2] = 0xc7;
1948       view[-1] = 0xc0 | reg;
1949     }
1950   else if (reg == 4)
1951     {
1952       // Special handling for %rsp.
1953       if (op1 == 0x4c)
1954         view[-3] = 0x49;
1955       view[-2] = 0x81;
1956       view[-1] = 0xc0 | reg;
1957     }
1958   else
1959     {
1960       // addq
1961       if (op1 == 0x4c)
1962         view[-3] = 0x4d;
1963       view[-2] = 0x8d;
1964       view[-1] = 0x80 | reg | (reg << 3);
1965     }
1966
1967   value -= tls_segment->memsz();
1968   Relocate_functions<64, false>::rela32(view, value, 0);
1969 }
1970
1971 // Relocate section data.
1972
1973 void
1974 Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
1975                                 unsigned int sh_type,
1976                                 const unsigned char* prelocs,
1977                                 size_t reloc_count,
1978                                 Output_section* output_section,
1979                                 bool needs_special_offset_handling,
1980                                 unsigned char* view,
1981                                 elfcpp::Elf_types<64>::Elf_Addr address,
1982                                 section_size_type view_size)
1983 {
1984   gold_assert(sh_type == elfcpp::SHT_RELA);
1985
1986   gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
1987                          Target_x86_64::Relocate>(
1988     relinfo,
1989     this,
1990     prelocs,
1991     reloc_count,
1992     output_section,
1993     needs_special_offset_handling,
1994     view,
1995     address,
1996     view_size);
1997 }
1998
1999 // Return the size of a relocation while scanning during a relocatable
2000 // link.
2001
2002 unsigned int
2003 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2004     unsigned int r_type,
2005     Relobj* object)
2006 {
2007   switch (r_type)
2008     {
2009     case elfcpp::R_X86_64_NONE:
2010     case elfcpp::R_386_GNU_VTINHERIT:
2011     case elfcpp::R_386_GNU_VTENTRY:
2012     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2013     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2014     case elfcpp::R_X86_64_TLSDESC_CALL:
2015     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2016     case elfcpp::R_X86_64_DTPOFF32:
2017     case elfcpp::R_X86_64_DTPOFF64:
2018     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2019     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2020       return 0;
2021
2022     case elfcpp::R_X86_64_64:
2023     case elfcpp::R_X86_64_PC64:
2024     case elfcpp::R_X86_64_GOTOFF64:
2025     case elfcpp::R_X86_64_GOTPC64:
2026     case elfcpp::R_X86_64_PLTOFF64:
2027     case elfcpp::R_X86_64_GOT64:
2028     case elfcpp::R_X86_64_GOTPCREL64:
2029     case elfcpp::R_X86_64_GOTPCREL:
2030     case elfcpp::R_X86_64_GOTPLT64:
2031       return 8;
2032
2033     case elfcpp::R_X86_64_32:
2034     case elfcpp::R_X86_64_32S:
2035     case elfcpp::R_X86_64_PC32:
2036     case elfcpp::R_X86_64_PLT32:
2037     case elfcpp::R_X86_64_GOTPC32:
2038     case elfcpp::R_X86_64_GOT32:
2039       return 4;
2040
2041     case elfcpp::R_X86_64_16:
2042     case elfcpp::R_X86_64_PC16:
2043       return 2;
2044
2045     case elfcpp::R_X86_64_8:
2046     case elfcpp::R_X86_64_PC8:
2047       return 1;
2048
2049     case elfcpp::R_X86_64_COPY:
2050     case elfcpp::R_X86_64_GLOB_DAT:
2051     case elfcpp::R_X86_64_JUMP_SLOT:
2052     case elfcpp::R_X86_64_RELATIVE:
2053       // These are outstanding tls relocs, which are unexpected when linking
2054     case elfcpp::R_X86_64_TPOFF64:
2055     case elfcpp::R_X86_64_DTPMOD64:
2056     case elfcpp::R_X86_64_TLSDESC:
2057       object->error(_("unexpected reloc %u in object file"), r_type);
2058       return 0;
2059
2060     case elfcpp::R_X86_64_SIZE32:
2061     case elfcpp::R_X86_64_SIZE64:
2062     default:
2063       object->error(_("unsupported reloc %u against local symbol"), r_type);
2064       return 0;
2065     }
2066 }
2067
2068 // Scan the relocs during a relocatable link.
2069
2070 void
2071 Target_x86_64::scan_relocatable_relocs(const General_options& options,
2072                                        Symbol_table* symtab,
2073                                        Layout* layout,
2074                                        Sized_relobj<64, false>* object,
2075                                        unsigned int data_shndx,
2076                                        unsigned int sh_type,
2077                                        const unsigned char* prelocs,
2078                                        size_t reloc_count,
2079                                        Output_section* output_section,
2080                                        bool needs_special_offset_handling,
2081                                        size_t local_symbol_count,
2082                                        const unsigned char* plocal_symbols,
2083                                        Relocatable_relocs* rr)
2084 {
2085   gold_assert(sh_type == elfcpp::SHT_RELA);
2086
2087   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2088     Relocatable_size_for_reloc> Scan_relocatable_relocs;
2089
2090   gold::scan_relocatable_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2091       Scan_relocatable_relocs>(
2092     options,
2093     symtab,
2094     layout,
2095     object,
2096     data_shndx,
2097     prelocs,
2098     reloc_count,
2099     output_section,
2100     needs_special_offset_handling,
2101     local_symbol_count,
2102     plocal_symbols,
2103     rr);
2104 }
2105
2106 // Relocate a section during a relocatable link.
2107
2108 void
2109 Target_x86_64::relocate_for_relocatable(
2110     const Relocate_info<64, false>* relinfo,
2111     unsigned int sh_type,
2112     const unsigned char* prelocs,
2113     size_t reloc_count,
2114     Output_section* output_section,
2115     off_t offset_in_output_section,
2116     const Relocatable_relocs* rr,
2117     unsigned char* view,
2118     elfcpp::Elf_types<64>::Elf_Addr view_address,
2119     section_size_type view_size,
2120     unsigned char* reloc_view,
2121     section_size_type reloc_view_size)
2122 {
2123   gold_assert(sh_type == elfcpp::SHT_RELA);
2124
2125   gold::relocate_for_relocatable<64, false, Target_x86_64, elfcpp::SHT_RELA>(
2126     relinfo,
2127     prelocs,
2128     reloc_count,
2129     output_section,
2130     offset_in_output_section,
2131     rr,
2132     view,
2133     view_address,
2134     view_size,
2135     reloc_view,
2136     reloc_view_size);
2137 }
2138
2139 // Return the value to use for a dynamic which requires special
2140 // treatment.  This is how we support equality comparisons of function
2141 // pointers across shared library boundaries, as described in the
2142 // processor specific ABI supplement.
2143
2144 uint64_t
2145 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2146 {
2147   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2148   return this->plt_section()->address() + gsym->plt_offset();
2149 }
2150
2151 // Return a string used to fill a code section with nops to take up
2152 // the specified length.
2153
2154 std::string
2155 Target_x86_64::do_code_fill(section_size_type length)
2156 {
2157   if (length >= 16)
2158     {
2159       // Build a jmpq instruction to skip over the bytes.
2160       unsigned char jmp[5];
2161       jmp[0] = 0xe9;
2162       elfcpp::Swap_unaligned<64, false>::writeval(jmp + 1, length - 5);
2163       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2164               + std::string(length - 5, '\0'));
2165     }
2166
2167   // Nop sequences of various lengths.
2168   const char nop1[1] = { 0x90 };                   // nop
2169   const char nop2[2] = { 0x66, 0x90 };             // xchg %ax %ax
2170   const char nop3[3] = { 0x8d, 0x76, 0x00 };       // leal 0(%esi),%esi
2171   const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00};  // leal 0(%esi,1),%esi
2172   const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26,   // nop
2173                          0x00 };                   // leal 0(%esi,1),%esi
2174   const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00,   // leal 0L(%esi),%esi
2175                          0x00, 0x00 };
2176   const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00,   // leal 0L(%esi,1),%esi
2177                          0x00, 0x00, 0x00 };
2178   const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26,   // nop
2179                          0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2180   const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc,   // movl %esi,%esi
2181                          0x27, 0x00, 0x00, 0x00,   // leal 0L(%edi,1),%edi
2182                          0x00 };
2183   const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2184                            0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2185                            0x00, 0x00 };
2186   const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2187                            0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2188                            0x00, 0x00, 0x00 };
2189   const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2190                            0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2191                            0x00, 0x00, 0x00, 0x00 };
2192   const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2193                            0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2194                            0x27, 0x00, 0x00, 0x00,
2195                            0x00 };
2196   const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2197                            0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2198                            0xbc, 0x27, 0x00, 0x00,
2199                            0x00, 0x00 };
2200   const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2201                            0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2202                            0x90, 0x90, 0x90, 0x90,
2203                            0x90, 0x90, 0x90 };
2204
2205   const char* nops[16] = {
2206     NULL,
2207     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2208     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2209   };
2210
2211   return std::string(nops[length], length);
2212 }
2213
2214 // The selector for x86_64 object files.
2215
2216 class Target_selector_x86_64 : public Target_selector
2217 {
2218 public:
2219   Target_selector_x86_64()
2220     : Target_selector(elfcpp::EM_X86_64, 64, false)
2221   { }
2222
2223   Target*
2224   recognize(int machine, int osabi, int abiversion);
2225
2226   Target*
2227   recognize_by_name(const char*);
2228
2229  private:
2230   Target_x86_64* target_;
2231 };
2232
2233 // Recognize an x86_64 object file when we already know that the machine
2234 // number is EM_X86_64.
2235
2236 Target*
2237 Target_selector_x86_64::recognize(int, int, int)
2238 {
2239   if (this->target_ == NULL)
2240     this->target_ = new Target_x86_64();
2241   return this->target_;
2242 }
2243
2244 Target*
2245 Target_selector_x86_64::recognize_by_name(const char* name)
2246 {
2247   if (strcmp(name, "elf64-x86-64") != 0)
2248     return NULL;
2249   if (this->target_ == NULL)
2250     this->target_ = new Target_x86_64();
2251   return this->target_;
2252 }
2253
2254 Target_selector_x86_64 target_selector_x86_64;
2255
2256 } // End anonymous namespace.