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