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