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