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