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