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