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