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