* x86_64.cc (Target_x86_64::saw_tls_block_reloc_): Remove member.
[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, 2010 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 #include "gc.h"
42 #include "icf.h"
43
44 namespace
45 {
46
47 using namespace gold;
48
49 // A class to handle the PLT data.
50
51 class Output_data_plt_x86_64 : public Output_section_data
52 {
53  public:
54   typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
55
56   Output_data_plt_x86_64(Symbol_table*, Layout*, Output_data_got<64, false>*,
57                          Output_data_space*);
58
59   // Add an entry to the PLT.
60   void
61   add_entry(Symbol* gsym);
62
63   // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
64   unsigned int
65   add_local_ifunc_entry(Sized_relobj<64, false>* relobj,
66                         unsigned int local_sym_index);
67
68   // Add the reserved TLSDESC_PLT entry to the PLT.
69   void
70   reserve_tlsdesc_entry(unsigned int got_offset)
71   { this->tlsdesc_got_offset_ = got_offset; }
72
73   // Return true if a TLSDESC_PLT entry has been reserved.
74   bool
75   has_tlsdesc_entry() const
76   { return this->tlsdesc_got_offset_ != -1U; }
77
78   // Return the GOT offset for the reserved TLSDESC_PLT entry.
79   unsigned int
80   get_tlsdesc_got_offset() const
81   { return this->tlsdesc_got_offset_; }
82
83   // Return the offset of the reserved TLSDESC_PLT entry.
84   unsigned int
85   get_tlsdesc_plt_offset() const
86   { return (this->count_ + 1) * plt_entry_size; }
87
88   // Return the .rela.plt section data.
89   Reloc_section*
90   rela_plt()
91   { return this->rel_; }
92
93   // Return where the TLSDESC relocations should go.
94   Reloc_section*
95   rela_tlsdesc(Layout*);
96
97   // Return the number of PLT entries.
98   unsigned int
99   entry_count() const
100   { return this->count_; }
101
102   // Return the offset of the first non-reserved PLT entry.
103   static unsigned int
104   first_plt_entry_offset()
105   { return plt_entry_size; }
106
107   // Return the size of a PLT entry.
108   static unsigned int
109   get_plt_entry_size()
110   { return plt_entry_size; }
111
112  protected:
113   void
114   do_adjust_output_section(Output_section* os);
115
116   // Write to a map file.
117   void
118   do_print_to_mapfile(Mapfile* mapfile) const
119   { mapfile->print_output_data(this, _("** PLT")); }
120
121  private:
122   // The size of an entry in the PLT.
123   static const int plt_entry_size = 16;
124
125   // The first entry in the PLT.
126   // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
127   // procedure linkage table for both programs and shared objects."
128   static unsigned char first_plt_entry[plt_entry_size];
129
130   // Other entries in the PLT for an executable.
131   static unsigned char plt_entry[plt_entry_size];
132
133   // The reserved TLSDESC entry in the PLT for an executable.
134   static unsigned char tlsdesc_plt_entry[plt_entry_size];
135
136   // Set the final size.
137   void
138   set_final_data_size();
139
140   // Write out the PLT data.
141   void
142   do_write(Output_file*);
143
144   // The reloc section.
145   Reloc_section* rel_;
146   // The TLSDESC relocs, if necessary.  These must follow the regular
147   // PLT relocs.
148   Reloc_section* tlsdesc_rel_;
149   // The .got section.
150   Output_data_got<64, false>* got_;
151   // The .got.plt section.
152   Output_data_space* got_plt_;
153   // The number of PLT entries.
154   unsigned int count_;
155   // Offset of the reserved TLSDESC_GOT entry when needed.
156   unsigned int tlsdesc_got_offset_;
157 };
158
159 // The x86_64 target class.
160 // See the ABI at
161 //   http://www.x86-64.org/documentation/abi.pdf
162 // TLS info comes from
163 //   http://people.redhat.com/drepper/tls.pdf
164 //   http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
165
166 class Target_x86_64 : public Target_freebsd<64, false>
167 {
168  public:
169   // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
170   // uses only Elf64_Rela relocation entries with explicit addends."
171   typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
172
173   Target_x86_64()
174     : Target_freebsd<64, false>(&x86_64_info),
175       got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
176       global_offset_table_(NULL), rela_dyn_(NULL),
177       copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
178       got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
179       tls_base_symbol_defined_(false)
180   { }
181
182   // This function should be defined in targets that can use relocation
183   // types to determine (implemented in local_reloc_may_be_function_pointer
184   // and global_reloc_may_be_function_pointer)
185   // if a function's pointer is taken.  ICF uses this in safe mode to only
186   // fold those functions whose pointer is defintely not taken.  For x86_64
187   // pie binaries, safe ICF cannot be done by looking at relocation types.
188   inline bool
189   can_check_for_function_pointers() const
190   { return !parameters->options().pie(); }
191
192   // Hook for a new output section.
193   void
194   do_new_output_section(Output_section*) const;
195
196   // Scan the relocations to look for symbol adjustments.
197   void
198   gc_process_relocs(Symbol_table* symtab,
199                     Layout* layout,
200                     Sized_relobj<64, false>* object,
201                     unsigned int data_shndx,
202                     unsigned int sh_type,
203                     const unsigned char* prelocs,
204                     size_t reloc_count,
205                     Output_section* output_section,
206                     bool needs_special_offset_handling,
207                     size_t local_symbol_count,
208                     const unsigned char* plocal_symbols);
209
210   // Scan the relocations to look for symbol adjustments.
211   void
212   scan_relocs(Symbol_table* symtab,
213               Layout* layout,
214               Sized_relobj<64, false>* object,
215               unsigned int data_shndx,
216               unsigned int sh_type,
217               const unsigned char* prelocs,
218               size_t reloc_count,
219               Output_section* output_section,
220               bool needs_special_offset_handling,
221               size_t local_symbol_count,
222               const unsigned char* plocal_symbols);
223
224   // Finalize the sections.
225   void
226   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
227
228   // Return the value to use for a dynamic which requires special
229   // treatment.
230   uint64_t
231   do_dynsym_value(const Symbol*) const;
232
233   // Relocate a section.
234   void
235   relocate_section(const Relocate_info<64, false>*,
236                    unsigned int sh_type,
237                    const unsigned char* prelocs,
238                    size_t reloc_count,
239                    Output_section* output_section,
240                    bool needs_special_offset_handling,
241                    unsigned char* view,
242                    elfcpp::Elf_types<64>::Elf_Addr view_address,
243                    section_size_type view_size,
244                    const Reloc_symbol_changes*);
245
246   // Scan the relocs during a relocatable link.
247   void
248   scan_relocatable_relocs(Symbol_table* symtab,
249                           Layout* layout,
250                           Sized_relobj<64, false>* object,
251                           unsigned int data_shndx,
252                           unsigned int sh_type,
253                           const unsigned char* prelocs,
254                           size_t reloc_count,
255                           Output_section* output_section,
256                           bool needs_special_offset_handling,
257                           size_t local_symbol_count,
258                           const unsigned char* plocal_symbols,
259                           Relocatable_relocs*);
260
261   // Relocate a section during a relocatable link.
262   void
263   relocate_for_relocatable(const Relocate_info<64, false>*,
264                            unsigned int sh_type,
265                            const unsigned char* prelocs,
266                            size_t reloc_count,
267                            Output_section* output_section,
268                            off_t offset_in_output_section,
269                            const Relocatable_relocs*,
270                            unsigned char* view,
271                            elfcpp::Elf_types<64>::Elf_Addr view_address,
272                            section_size_type view_size,
273                            unsigned char* reloc_view,
274                            section_size_type reloc_view_size);
275
276   // Return a string used to fill a code section with nops.
277   std::string
278   do_code_fill(section_size_type length) const;
279
280   // Return whether SYM is defined by the ABI.
281   bool
282   do_is_defined_by_abi(const Symbol* sym) const
283   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
284
285   // Return the symbol index to use for a target specific relocation.
286   // The only target specific relocation is R_X86_64_TLSDESC for a
287   // local symbol, which is an absolute reloc.
288   unsigned int
289   do_reloc_symbol_index(void*, unsigned int r_type) const
290   {
291     gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
292     return 0;
293   }
294
295   // Return the addend to use for a target specific relocation.
296   uint64_t
297   do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
298
299   // Return the PLT section.
300   Output_data*
301   do_plt_section_for_global(const Symbol*) const
302   { return this->plt_section(); }
303
304   Output_data*
305   do_plt_section_for_local(const Relobj*, unsigned int) const
306   { return this->plt_section(); }
307
308   // Adjust -fstack-split code which calls non-stack-split code.
309   void
310   do_calls_non_split(Relobj* object, unsigned int shndx,
311                      section_offset_type fnoffset, section_size_type fnsize,
312                      unsigned char* view, section_size_type view_size,
313                      std::string* from, std::string* to) const;
314
315   // Return the size of the GOT section.
316   section_size_type
317   got_size() const
318   {
319     gold_assert(this->got_ != NULL);
320     return this->got_->data_size();
321   }
322
323   // Return the number of entries in the GOT.
324   unsigned int
325   got_entry_count() const
326   {
327     if (this->got_ == NULL)
328       return 0;
329     return this->got_size() / 8;
330   }
331
332   // Return the number of entries in the PLT.
333   unsigned int
334   plt_entry_count() const;
335
336   // Return the offset of the first non-reserved PLT entry.
337   unsigned int
338   first_plt_entry_offset() const;
339
340   // Return the size of each PLT entry.
341   unsigned int
342   plt_entry_size() const;
343
344   // Add a new reloc argument, returning the index in the vector.
345   size_t
346   add_tlsdesc_info(Sized_relobj<64, false>* object, unsigned int r_sym)
347   {
348     this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
349     return this->tlsdesc_reloc_info_.size() - 1;
350   }
351
352  private:
353   // The class which scans relocations.
354   class Scan
355   {
356   public:
357     Scan()
358       : issued_non_pic_error_(false)
359     { }
360
361     inline void
362     local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
363           Sized_relobj<64, false>* object,
364           unsigned int data_shndx,
365           Output_section* output_section,
366           const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
367           const elfcpp::Sym<64, false>& lsym);
368
369     inline void
370     global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
371            Sized_relobj<64, false>* object,
372            unsigned int data_shndx,
373            Output_section* output_section,
374            const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
375            Symbol* gsym);
376
377     inline bool
378     local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
379                                         Target_x86_64* target,
380                                         Sized_relobj<64, false>* object,
381                                         unsigned int data_shndx,
382                                         Output_section* output_section,
383                                         const elfcpp::Rela<64, false>& reloc,
384                                         unsigned int r_type,
385                                         const elfcpp::Sym<64, false>& lsym);
386
387     inline bool
388     global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
389                                          Target_x86_64* target,
390                                          Sized_relobj<64, false>* object,
391                                          unsigned int data_shndx,
392                                          Output_section* output_section,
393                                          const elfcpp::Rela<64, false>& reloc,
394                                          unsigned int r_type,
395                                          Symbol* gsym);
396
397   private:
398     static void
399     unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
400
401     static void
402     unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
403                              Symbol*);
404
405     void
406     check_non_pic(Relobj*, unsigned int r_type);
407
408     inline bool
409     possible_function_pointer_reloc(unsigned int r_type);
410
411     bool
412     reloc_needs_plt_for_ifunc(Sized_relobj<64, false>*, unsigned int r_type);
413
414     // Whether we have issued an error about a non-PIC compilation.
415     bool issued_non_pic_error_;
416   };
417
418   // The class which implements relocation.
419   class Relocate
420   {
421    public:
422     Relocate()
423       : skip_call_tls_get_addr_(false)
424     { }
425
426     ~Relocate()
427     {
428       if (this->skip_call_tls_get_addr_)
429         {
430           // FIXME: This needs to specify the location somehow.
431           gold_error(_("missing expected TLS relocation"));
432         }
433     }
434
435     // Do a relocation.  Return false if the caller should not issue
436     // any warnings about this relocation.
437     inline bool
438     relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
439              size_t relnum, const elfcpp::Rela<64, false>&,
440              unsigned int r_type, const Sized_symbol<64>*,
441              const Symbol_value<64>*,
442              unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
443              section_size_type);
444
445    private:
446     // Do a TLS relocation.
447     inline void
448     relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
449                  size_t relnum, const elfcpp::Rela<64, false>&,
450                  unsigned int r_type, const Sized_symbol<64>*,
451                  const Symbol_value<64>*,
452                  unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
453                  section_size_type);
454
455     // Do a TLS General-Dynamic to Initial-Exec transition.
456     inline void
457     tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
458                  Output_segment* tls_segment,
459                  const elfcpp::Rela<64, false>&, unsigned int r_type,
460                  elfcpp::Elf_types<64>::Elf_Addr value,
461                  unsigned char* view,
462                  elfcpp::Elf_types<64>::Elf_Addr,
463                  section_size_type view_size);
464
465     // Do a TLS General-Dynamic to Local-Exec transition.
466     inline void
467     tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
468                  Output_segment* tls_segment,
469                  const elfcpp::Rela<64, false>&, unsigned int r_type,
470                  elfcpp::Elf_types<64>::Elf_Addr value,
471                  unsigned char* view,
472                  section_size_type view_size);
473
474     // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
475     inline void
476     tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
477                       Output_segment* tls_segment,
478                       const elfcpp::Rela<64, false>&, unsigned int r_type,
479                       elfcpp::Elf_types<64>::Elf_Addr value,
480                       unsigned char* view,
481                       elfcpp::Elf_types<64>::Elf_Addr,
482                       section_size_type view_size);
483
484     // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
485     inline void
486     tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
487                       Output_segment* tls_segment,
488                       const elfcpp::Rela<64, false>&, unsigned int r_type,
489                       elfcpp::Elf_types<64>::Elf_Addr value,
490                       unsigned char* view,
491                       section_size_type view_size);
492
493     // Do a TLS Local-Dynamic to Local-Exec transition.
494     inline void
495     tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
496                  Output_segment* tls_segment,
497                  const elfcpp::Rela<64, false>&, unsigned int r_type,
498                  elfcpp::Elf_types<64>::Elf_Addr value,
499                  unsigned char* view,
500                  section_size_type view_size);
501
502     // Do a TLS Initial-Exec to Local-Exec transition.
503     static inline void
504     tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
505                  Output_segment* tls_segment,
506                  const elfcpp::Rela<64, false>&, unsigned int r_type,
507                  elfcpp::Elf_types<64>::Elf_Addr value,
508                  unsigned char* view,
509                  section_size_type view_size);
510
511     // This is set if we should skip the next reloc, which should be a
512     // PLT32 reloc against ___tls_get_addr.
513     bool skip_call_tls_get_addr_;
514   };
515
516   // A class which returns the size required for a relocation type,
517   // used while scanning relocs during a relocatable link.
518   class Relocatable_size_for_reloc
519   {
520    public:
521     unsigned int
522     get_size_for_reloc(unsigned int, Relobj*);
523   };
524
525   // Adjust TLS relocation type based on the options and whether this
526   // is a local symbol.
527   static tls::Tls_optimization
528   optimize_tls_reloc(bool is_final, int r_type);
529
530   // Get the GOT section, creating it if necessary.
531   Output_data_got<64, false>*
532   got_section(Symbol_table*, Layout*);
533
534   // Get the GOT PLT section.
535   Output_data_space*
536   got_plt_section() const
537   {
538     gold_assert(this->got_plt_ != NULL);
539     return this->got_plt_;
540   }
541
542   // Get the GOT section for TLSDESC entries.
543   Output_data_got<64, false>*
544   got_tlsdesc_section() const
545   {
546     gold_assert(this->got_tlsdesc_ != NULL);
547     return this->got_tlsdesc_;
548   }
549
550   // Create the PLT section.
551   void
552   make_plt_section(Symbol_table* symtab, Layout* layout);
553
554   // Create a PLT entry for a global symbol.
555   void
556   make_plt_entry(Symbol_table*, Layout*, Symbol*);
557
558   // Create a PLT entry for a local STT_GNU_IFUNC symbol.
559   void
560   make_local_ifunc_plt_entry(Symbol_table*, Layout*,
561                              Sized_relobj<64, false>* relobj,
562                              unsigned int local_sym_index);
563
564   // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
565   void
566   define_tls_base_symbol(Symbol_table*, Layout*);
567
568   // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
569   void
570   reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
571
572   // Create a GOT entry for the TLS module index.
573   unsigned int
574   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
575                       Sized_relobj<64, false>* object);
576
577   // Get the PLT section.
578   Output_data_plt_x86_64*
579   plt_section() const
580   {
581     gold_assert(this->plt_ != NULL);
582     return this->plt_;
583   }
584
585   // Get the dynamic reloc section, creating it if necessary.
586   Reloc_section*
587   rela_dyn_section(Layout*);
588
589   // Get the section to use for TLSDESC relocations.
590   Reloc_section*
591   rela_tlsdesc_section(Layout*) const;
592
593   // Add a potential copy relocation.
594   void
595   copy_reloc(Symbol_table* symtab, Layout* layout,
596              Sized_relobj<64, false>* object,
597              unsigned int shndx, Output_section* output_section,
598              Symbol* sym, const elfcpp::Rela<64, false>& reloc)
599   {
600     this->copy_relocs_.copy_reloc(symtab, layout,
601                                   symtab->get_sized_symbol<64>(sym),
602                                   object, shndx, output_section,
603                                   reloc, this->rela_dyn_section(layout));
604   }
605
606   // Information about this specific target which we pass to the
607   // general Target structure.
608   static const Target::Target_info x86_64_info;
609
610   // The types of GOT entries needed for this platform.
611   // These values are exposed to the ABI in an incremental link.
612   // Do not renumber existing values without changing the version
613   // number of the .gnu_incremental_inputs section.
614   enum Got_type
615   {
616     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
617     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
618     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
619     GOT_TYPE_TLS_DESC = 3       // GOT entry for TLS_DESC pair
620   };
621
622   // This type is used as the argument to the target specific
623   // relocation routines.  The only target specific reloc is
624   // R_X86_64_TLSDESC against a local symbol.
625   struct Tlsdesc_info
626   {
627     Tlsdesc_info(Sized_relobj<64, false>* a_object, unsigned int a_r_sym)
628       : object(a_object), r_sym(a_r_sym)
629     { }
630
631     // The object in which the local symbol is defined.
632     Sized_relobj<64, false>* object;
633     // The local symbol index in the object.
634     unsigned int r_sym;
635   };
636
637   // The GOT section.
638   Output_data_got<64, false>* got_;
639   // The PLT section.
640   Output_data_plt_x86_64* plt_;
641   // The GOT PLT section.
642   Output_data_space* got_plt_;
643   // The GOT section for TLSDESC relocations.
644   Output_data_got<64, false>* got_tlsdesc_;
645   // The _GLOBAL_OFFSET_TABLE_ symbol.
646   Symbol* global_offset_table_;
647   // The dynamic reloc section.
648   Reloc_section* rela_dyn_;
649   // Relocs saved to avoid a COPY reloc.
650   Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
651   // Space for variables copied with a COPY reloc.
652   Output_data_space* dynbss_;
653   // Offset of the GOT entry for the TLS module index.
654   unsigned int got_mod_index_offset_;
655   // We handle R_X86_64_TLSDESC against a local symbol as a target
656   // specific relocation.  Here we store the object and local symbol
657   // index for the relocation.
658   std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
659   // True if the _TLS_MODULE_BASE_ symbol has been defined.
660   bool tls_base_symbol_defined_;
661 };
662
663 const Target::Target_info Target_x86_64::x86_64_info =
664 {
665   64,                   // size
666   false,                // is_big_endian
667   elfcpp::EM_X86_64,    // machine_code
668   false,                // has_make_symbol
669   false,                // has_resolve
670   true,                 // has_code_fill
671   true,                 // is_default_stack_executable
672   '\0',                 // wrap_char
673   "/lib/ld64.so.1",     // program interpreter
674   0x400000,             // default_text_segment_address
675   0x1000,               // abi_pagesize (overridable by -z max-page-size)
676   0x1000,               // common_pagesize (overridable by -z common-page-size)
677   elfcpp::SHN_UNDEF,    // small_common_shndx
678   elfcpp::SHN_X86_64_LCOMMON,   // large_common_shndx
679   0,                    // small_common_section_flags
680   elfcpp::SHF_X86_64_LARGE,     // large_common_section_flags
681   NULL,                 // attributes_section
682   NULL                  // attributes_vendor
683 };
684
685 // This is called when a new output section is created.  This is where
686 // we handle the SHF_X86_64_LARGE.
687
688 void
689 Target_x86_64::do_new_output_section(Output_section* os) const
690 {
691   if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
692     os->set_is_large_section();
693 }
694
695 // Get the GOT section, creating it if necessary.
696
697 Output_data_got<64, false>*
698 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
699 {
700   if (this->got_ == NULL)
701     {
702       gold_assert(symtab != NULL && layout != NULL);
703
704       this->got_ = new Output_data_got<64, false>();
705
706       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
707                                       (elfcpp::SHF_ALLOC
708                                        | elfcpp::SHF_WRITE),
709                                       this->got_, ORDER_RELRO_LAST,
710                                       true);
711
712       this->got_plt_ = new Output_data_space(8, "** GOT PLT");
713       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
714                                       (elfcpp::SHF_ALLOC
715                                        | elfcpp::SHF_WRITE),
716                                       this->got_plt_, ORDER_NON_RELRO_FIRST,
717                                       false);
718
719       // The first three entries are reserved.
720       this->got_plt_->set_current_data_size(3 * 8);
721
722       // Those bytes can go into the relro segment.
723       layout->increase_relro(3 * 8);
724
725       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
726       this->global_offset_table_ =
727         symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
728                                       Symbol_table::PREDEFINED,
729                                       this->got_plt_,
730                                       0, 0, elfcpp::STT_OBJECT,
731                                       elfcpp::STB_LOCAL,
732                                       elfcpp::STV_HIDDEN, 0,
733                                       false, false);
734
735       // If there are any TLSDESC relocations, they get GOT entries in
736       // .got.plt after the jump slot entries.
737       this->got_tlsdesc_ = new Output_data_got<64, false>();
738       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
739                                       (elfcpp::SHF_ALLOC
740                                        | elfcpp::SHF_WRITE),
741                                       this->got_tlsdesc_,
742                                       ORDER_NON_RELRO_FIRST, false);
743     }
744
745   return this->got_;
746 }
747
748 // Get the dynamic reloc section, creating it if necessary.
749
750 Target_x86_64::Reloc_section*
751 Target_x86_64::rela_dyn_section(Layout* layout)
752 {
753   if (this->rela_dyn_ == NULL)
754     {
755       gold_assert(layout != NULL);
756       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
757       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
758                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
759                                       ORDER_DYNAMIC_RELOCS, false);
760     }
761   return this->rela_dyn_;
762 }
763
764 // Create the PLT section.  The ordinary .got section is an argument,
765 // since we need to refer to the start.  We also create our own .got
766 // section just for PLT entries.
767
768 Output_data_plt_x86_64::Output_data_plt_x86_64(Symbol_table* symtab,
769                                                Layout* layout,
770                                                Output_data_got<64, false>* got,
771                                                Output_data_space* got_plt)
772   : Output_section_data(8), tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
773     count_(0), tlsdesc_got_offset_(-1U)
774 {
775   this->rel_ = new Reloc_section(false);
776   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
777                                   elfcpp::SHF_ALLOC, this->rel_,
778                                   ORDER_DYNAMIC_PLT_RELOCS, false);
779
780   if (parameters->doing_static_link())
781     {
782       // A statically linked executable will only have a .rela.plt
783       // section to hold R_X86_64_IRELATIVE relocs for STT_GNU_IFUNC
784       // symbols.  The library will use these symbols to locate the
785       // IRELATIVE relocs at program startup time.
786       symtab->define_in_output_data("__rela_iplt_start", NULL,
787                                     Symbol_table::PREDEFINED,
788                                     this->rel_, 0, 0, elfcpp::STT_NOTYPE,
789                                     elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
790                                     0, false, true);
791       symtab->define_in_output_data("__rela_iplt_end", NULL,
792                                     Symbol_table::PREDEFINED,
793                                     this->rel_, 0, 0, elfcpp::STT_NOTYPE,
794                                     elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
795                                     0, true, true);
796     }
797 }
798
799 void
800 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
801 {
802   os->set_entsize(plt_entry_size);
803 }
804
805 // Add an entry to the PLT.
806
807 void
808 Output_data_plt_x86_64::add_entry(Symbol* gsym)
809 {
810   gold_assert(!gsym->has_plt_offset());
811
812   // Note that when setting the PLT offset we skip the initial
813   // reserved PLT entry.
814   gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
815
816   ++this->count_;
817
818   section_offset_type got_offset = this->got_plt_->current_data_size();
819
820   // Every PLT entry needs a GOT entry which points back to the PLT
821   // entry (this will be changed by the dynamic linker, normally
822   // lazily when the function is called).
823   this->got_plt_->set_current_data_size(got_offset + 8);
824
825   // Every PLT entry needs a reloc.
826   if (gsym->type() == elfcpp::STT_GNU_IFUNC
827       && gsym->can_use_relative_reloc(false))
828     this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE,
829                                              this->got_plt_, got_offset, 0);
830   else
831     {
832       gsym->set_needs_dynsym_entry();
833       this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
834                              got_offset, 0);
835     }
836
837   // Note that we don't need to save the symbol.  The contents of the
838   // PLT are independent of which symbols are used.  The symbols only
839   // appear in the relocations.
840 }
841
842 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.  Return
843 // the PLT offset.
844
845 unsigned int
846 Output_data_plt_x86_64::add_local_ifunc_entry(Sized_relobj<64, false>* relobj,
847                                               unsigned int local_sym_index)
848 {
849   unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
850   ++this->count_;
851
852   section_offset_type got_offset = this->got_plt_->current_data_size();
853
854   // Every PLT entry needs a GOT entry which points back to the PLT
855   // entry.
856   this->got_plt_->set_current_data_size(got_offset + 8);
857
858   // Every PLT entry needs a reloc.
859   this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
860                                           elfcpp::R_X86_64_IRELATIVE,
861                                           this->got_plt_, got_offset, 0);
862
863   return plt_offset;
864 }
865
866 // Return where the TLSDESC relocations should go, creating it if
867 // necessary.  These follow the JUMP_SLOT relocations.
868
869 Output_data_plt_x86_64::Reloc_section*
870 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
871 {
872   if (this->tlsdesc_rel_ == NULL)
873     {
874       this->tlsdesc_rel_ = new Reloc_section(false);
875       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
876                                       elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
877                                       ORDER_DYNAMIC_PLT_RELOCS, false);
878       gold_assert(this->tlsdesc_rel_->output_section() ==
879                   this->rel_->output_section());
880     }
881   return this->tlsdesc_rel_;
882 }
883
884 // Set the final size.
885 void
886 Output_data_plt_x86_64::set_final_data_size()
887 {
888   unsigned int count = this->count_;
889   if (this->has_tlsdesc_entry())
890     ++count;
891   this->set_data_size((count + 1) * plt_entry_size);
892 }
893
894 // The first entry in the PLT for an executable.
895
896 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
897 {
898   // From AMD64 ABI Draft 0.98, page 76
899   0xff, 0x35,   // pushq contents of memory address
900   0, 0, 0, 0,   // replaced with address of .got + 8
901   0xff, 0x25,   // jmp indirect
902   0, 0, 0, 0,   // replaced with address of .got + 16
903   0x90, 0x90, 0x90, 0x90   // noop (x4)
904 };
905
906 // Subsequent entries in the PLT for an executable.
907
908 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
909 {
910   // From AMD64 ABI Draft 0.98, page 76
911   0xff, 0x25,   // jmpq indirect
912   0, 0, 0, 0,   // replaced with address of symbol in .got
913   0x68,         // pushq immediate
914   0, 0, 0, 0,   // replaced with offset into relocation table
915   0xe9,         // jmpq relative
916   0, 0, 0, 0    // replaced with offset to start of .plt
917 };
918
919 // The reserved TLSDESC entry in the PLT for an executable.
920
921 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
922 {
923   // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
924   // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
925   0xff, 0x35,   // pushq x(%rip)
926   0, 0, 0, 0,   // replaced with address of linkmap GOT entry (at PLTGOT + 8)
927   0xff, 0x25,   // jmpq *y(%rip)
928   0, 0, 0, 0,   // replaced with offset of reserved TLSDESC_GOT entry
929   0x0f, 0x1f,   // nop
930   0x40, 0
931 };
932
933 // Write out the PLT.  This uses the hand-coded instructions above,
934 // and adjusts them as needed.  This is specified by the AMD64 ABI.
935
936 void
937 Output_data_plt_x86_64::do_write(Output_file* of)
938 {
939   const off_t offset = this->offset();
940   const section_size_type oview_size =
941     convert_to_section_size_type(this->data_size());
942   unsigned char* const oview = of->get_output_view(offset, oview_size);
943
944   const off_t got_file_offset = this->got_plt_->offset();
945   const section_size_type got_size =
946     convert_to_section_size_type(this->got_plt_->data_size());
947   unsigned char* const got_view = of->get_output_view(got_file_offset,
948                                                       got_size);
949
950   unsigned char* pov = oview;
951
952   // The base address of the .plt section.
953   elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
954   // The base address of the .got section.
955   elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
956   // The base address of the PLT portion of the .got section,
957   // which is where the GOT pointer will point, and where the
958   // three reserved GOT entries are located.
959   elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
960
961   memcpy(pov, first_plt_entry, plt_entry_size);
962   // We do a jmp relative to the PC at the end of this instruction.
963   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
964                                               (got_address + 8
965                                                - (plt_address + 6)));
966   elfcpp::Swap<32, false>::writeval(pov + 8,
967                                     (got_address + 16
968                                      - (plt_address + 12)));
969   pov += plt_entry_size;
970
971   unsigned char* got_pov = got_view;
972
973   memset(got_pov, 0, 24);
974   got_pov += 24;
975
976   unsigned int plt_offset = plt_entry_size;
977   unsigned int got_offset = 24;
978   const unsigned int count = this->count_;
979   for (unsigned int plt_index = 0;
980        plt_index < count;
981        ++plt_index,
982          pov += plt_entry_size,
983          got_pov += 8,
984          plt_offset += plt_entry_size,
985          got_offset += 8)
986     {
987       // Set and adjust the PLT entry itself.
988       memcpy(pov, plt_entry, plt_entry_size);
989       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
990                                                   (got_address + got_offset
991                                                    - (plt_address + plt_offset
992                                                       + 6)));
993
994       elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
995       elfcpp::Swap<32, false>::writeval(pov + 12,
996                                         - (plt_offset + plt_entry_size));
997
998       // Set the entry in the GOT.
999       elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
1000     }
1001
1002   if (this->has_tlsdesc_entry())
1003     {
1004       // Set and adjust the reserved TLSDESC PLT entry.
1005       unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
1006       memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
1007       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1008                                                   (got_address + 8
1009                                                    - (plt_address + plt_offset
1010                                                       + 6)));
1011       elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
1012                                                   (got_base
1013                                                    + tlsdesc_got_offset
1014                                                    - (plt_address + plt_offset
1015                                                       + 12)));
1016       pov += plt_entry_size;
1017     }
1018
1019   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1020   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1021
1022   of->write_output_view(offset, oview_size, oview);
1023   of->write_output_view(got_file_offset, got_size, got_view);
1024 }
1025
1026 // Create the PLT section.
1027
1028 void
1029 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
1030 {
1031   if (this->plt_ == NULL)
1032     {
1033       // Create the GOT sections first.
1034       this->got_section(symtab, layout);
1035
1036       this->plt_ = new Output_data_plt_x86_64(symtab, layout, this->got_,
1037                                               this->got_plt_);
1038       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1039                                       (elfcpp::SHF_ALLOC
1040                                        | elfcpp::SHF_EXECINSTR),
1041                                       this->plt_, ORDER_PLT, false);
1042
1043       // Make the sh_info field of .rela.plt point to .plt.
1044       Output_section* rela_plt_os = this->plt_->rela_plt()->output_section();
1045       rela_plt_os->set_info_section(this->plt_->output_section());
1046     }
1047 }
1048
1049 // Return the section for TLSDESC relocations.
1050
1051 Target_x86_64::Reloc_section*
1052 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
1053 {
1054   return this->plt_section()->rela_tlsdesc(layout);
1055 }
1056
1057 // Create a PLT entry for a global symbol.
1058
1059 void
1060 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
1061                               Symbol* gsym)
1062 {
1063   if (gsym->has_plt_offset())
1064     return;
1065
1066   if (this->plt_ == NULL)
1067     this->make_plt_section(symtab, layout);
1068
1069   this->plt_->add_entry(gsym);
1070 }
1071
1072 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1073
1074 void
1075 Target_x86_64::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1076                                           Sized_relobj<64, false>* relobj,
1077                                           unsigned int local_sym_index)
1078 {
1079   if (relobj->local_has_plt_offset(local_sym_index))
1080     return;
1081   if (this->plt_ == NULL)
1082     this->make_plt_section(symtab, layout);
1083   unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1084                                                               local_sym_index);
1085   relobj->set_local_plt_offset(local_sym_index, plt_offset);
1086 }
1087
1088 // Return the number of entries in the PLT.
1089
1090 unsigned int
1091 Target_x86_64::plt_entry_count() const
1092 {
1093   if (this->plt_ == NULL)
1094     return 0;
1095   return this->plt_->entry_count();
1096 }
1097
1098 // Return the offset of the first non-reserved PLT entry.
1099
1100 unsigned int
1101 Target_x86_64::first_plt_entry_offset() const
1102 {
1103   return Output_data_plt_x86_64::first_plt_entry_offset();
1104 }
1105
1106 // Return the size of each PLT entry.
1107
1108 unsigned int
1109 Target_x86_64::plt_entry_size() const
1110 {
1111   return Output_data_plt_x86_64::get_plt_entry_size();
1112 }
1113
1114 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1115
1116 void
1117 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1118 {
1119   if (this->tls_base_symbol_defined_)
1120     return;
1121
1122   Output_segment* tls_segment = layout->tls_segment();
1123   if (tls_segment != NULL)
1124     {
1125       bool is_exec = parameters->options().output_is_executable();
1126       symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1127                                        Symbol_table::PREDEFINED,
1128                                        tls_segment, 0, 0,
1129                                        elfcpp::STT_TLS,
1130                                        elfcpp::STB_LOCAL,
1131                                        elfcpp::STV_HIDDEN, 0,
1132                                        (is_exec
1133                                         ? Symbol::SEGMENT_END
1134                                         : Symbol::SEGMENT_START),
1135                                        true);
1136     }
1137   this->tls_base_symbol_defined_ = true;
1138 }
1139
1140 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
1141
1142 void
1143 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
1144                                              Layout* layout)
1145 {
1146   if (this->plt_ == NULL)
1147     this->make_plt_section(symtab, layout);
1148
1149   if (!this->plt_->has_tlsdesc_entry())
1150     {
1151       // Allocate the TLSDESC_GOT entry.
1152       Output_data_got<64, false>* got = this->got_section(symtab, layout);
1153       unsigned int got_offset = got->add_constant(0);
1154
1155       // Allocate the TLSDESC_PLT entry.
1156       this->plt_->reserve_tlsdesc_entry(got_offset);
1157     }
1158 }
1159
1160 // Create a GOT entry for the TLS module index.
1161
1162 unsigned int
1163 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1164                                    Sized_relobj<64, false>* object)
1165 {
1166   if (this->got_mod_index_offset_ == -1U)
1167     {
1168       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1169       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1170       Output_data_got<64, false>* got = this->got_section(symtab, layout);
1171       unsigned int got_offset = got->add_constant(0);
1172       rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1173                           got_offset, 0);
1174       got->add_constant(0);
1175       this->got_mod_index_offset_ = got_offset;
1176     }
1177   return this->got_mod_index_offset_;
1178 }
1179
1180 // Optimize the TLS relocation type based on what we know about the
1181 // symbol.  IS_FINAL is true if the final address of this symbol is
1182 // known at link time.
1183
1184 tls::Tls_optimization
1185 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1186 {
1187   // If we are generating a shared library, then we can't do anything
1188   // in the linker.
1189   if (parameters->options().shared())
1190     return tls::TLSOPT_NONE;
1191
1192   switch (r_type)
1193     {
1194     case elfcpp::R_X86_64_TLSGD:
1195     case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1196     case elfcpp::R_X86_64_TLSDESC_CALL:
1197       // These are General-Dynamic which permits fully general TLS
1198       // access.  Since we know that we are generating an executable,
1199       // we can convert this to Initial-Exec.  If we also know that
1200       // this is a local symbol, we can further switch to Local-Exec.
1201       if (is_final)
1202         return tls::TLSOPT_TO_LE;
1203       return tls::TLSOPT_TO_IE;
1204
1205     case elfcpp::R_X86_64_TLSLD:
1206       // This is Local-Dynamic, which refers to a local symbol in the
1207       // dynamic TLS block.  Since we know that we generating an
1208       // executable, we can switch to Local-Exec.
1209       return tls::TLSOPT_TO_LE;
1210
1211     case elfcpp::R_X86_64_DTPOFF32:
1212     case elfcpp::R_X86_64_DTPOFF64:
1213       // Another Local-Dynamic reloc.
1214       return tls::TLSOPT_TO_LE;
1215
1216     case elfcpp::R_X86_64_GOTTPOFF:
1217       // These are Initial-Exec relocs which get the thread offset
1218       // from the GOT.  If we know that we are linking against the
1219       // local symbol, we can switch to Local-Exec, which links the
1220       // thread offset into the instruction.
1221       if (is_final)
1222         return tls::TLSOPT_TO_LE;
1223       return tls::TLSOPT_NONE;
1224
1225     case elfcpp::R_X86_64_TPOFF32:
1226       // When we already have Local-Exec, there is nothing further we
1227       // can do.
1228       return tls::TLSOPT_NONE;
1229
1230     default:
1231       gold_unreachable();
1232     }
1233 }
1234
1235 // Report an unsupported relocation against a local symbol.
1236
1237 void
1238 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
1239                                              unsigned int r_type)
1240 {
1241   gold_error(_("%s: unsupported reloc %u against local symbol"),
1242              object->name().c_str(), r_type);
1243 }
1244
1245 // We are about to emit a dynamic relocation of type R_TYPE.  If the
1246 // dynamic linker does not support it, issue an error.  The GNU linker
1247 // only issues a non-PIC error for an allocated read-only section.
1248 // Here we know the section is allocated, but we don't know that it is
1249 // read-only.  But we check for all the relocation types which the
1250 // glibc dynamic linker supports, so it seems appropriate to issue an
1251 // error even if the section is not read-only.
1252
1253 void
1254 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1255 {
1256   switch (r_type)
1257     {
1258       // These are the relocation types supported by glibc for x86_64.
1259     case elfcpp::R_X86_64_RELATIVE:
1260     case elfcpp::R_X86_64_IRELATIVE:
1261     case elfcpp::R_X86_64_GLOB_DAT:
1262     case elfcpp::R_X86_64_JUMP_SLOT:
1263     case elfcpp::R_X86_64_DTPMOD64:
1264     case elfcpp::R_X86_64_DTPOFF64:
1265     case elfcpp::R_X86_64_TPOFF64:
1266     case elfcpp::R_X86_64_64:
1267     case elfcpp::R_X86_64_32:
1268     case elfcpp::R_X86_64_PC32:
1269     case elfcpp::R_X86_64_COPY:
1270       return;
1271
1272     default:
1273       // This prevents us from issuing more than one error per reloc
1274       // section.  But we can still wind up issuing more than one
1275       // error per object file.
1276       if (this->issued_non_pic_error_)
1277         return;
1278       gold_assert(parameters->options().output_is_position_independent());
1279       object->error(_("requires unsupported dynamic reloc; "
1280                       "recompile with -fPIC"));
1281       this->issued_non_pic_error_ = true;
1282       return;
1283
1284     case elfcpp::R_X86_64_NONE:
1285       gold_unreachable();
1286     }
1287 }
1288
1289 // Return whether we need to make a PLT entry for a relocation of the
1290 // given type against a STT_GNU_IFUNC symbol.
1291
1292 bool
1293 Target_x86_64::Scan::reloc_needs_plt_for_ifunc(Sized_relobj<64, false>* object,
1294                                                unsigned int r_type)
1295 {
1296   switch (r_type)
1297     {
1298     case elfcpp::R_X86_64_NONE:
1299     case elfcpp::R_X86_64_GNU_VTINHERIT:
1300     case elfcpp::R_X86_64_GNU_VTENTRY:
1301       return false;
1302
1303     case elfcpp::R_X86_64_64:
1304     case elfcpp::R_X86_64_32:
1305     case elfcpp::R_X86_64_32S:
1306     case elfcpp::R_X86_64_16:
1307     case elfcpp::R_X86_64_8:
1308     case elfcpp::R_X86_64_PC64:
1309     case elfcpp::R_X86_64_PC32:
1310     case elfcpp::R_X86_64_PC16:
1311     case elfcpp::R_X86_64_PC8:
1312     case elfcpp::R_X86_64_PLT32:
1313     case elfcpp::R_X86_64_GOTPC32:
1314     case elfcpp::R_X86_64_GOTOFF64:
1315     case elfcpp::R_X86_64_GOTPC64:
1316     case elfcpp::R_X86_64_PLTOFF64:
1317     case elfcpp::R_X86_64_GOT64:
1318     case elfcpp::R_X86_64_GOT32:
1319     case elfcpp::R_X86_64_GOTPCREL64:
1320     case elfcpp::R_X86_64_GOTPCREL:
1321     case elfcpp::R_X86_64_GOTPLT64:
1322       return true;
1323
1324     case elfcpp::R_X86_64_COPY:
1325     case elfcpp::R_X86_64_GLOB_DAT:
1326     case elfcpp::R_X86_64_JUMP_SLOT:
1327     case elfcpp::R_X86_64_RELATIVE:
1328     case elfcpp::R_X86_64_IRELATIVE:
1329     case elfcpp::R_X86_64_TPOFF64:
1330     case elfcpp::R_X86_64_DTPMOD64:
1331     case elfcpp::R_X86_64_TLSDESC:
1332       // We will give an error later.
1333       return false;
1334
1335     case elfcpp::R_X86_64_TLSGD:
1336     case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1337     case elfcpp::R_X86_64_TLSDESC_CALL:
1338     case elfcpp::R_X86_64_TLSLD:
1339     case elfcpp::R_X86_64_DTPOFF32:
1340     case elfcpp::R_X86_64_DTPOFF64:
1341     case elfcpp::R_X86_64_GOTTPOFF:
1342     case elfcpp::R_X86_64_TPOFF32:
1343       gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1344                  object->name().c_str(), r_type);
1345       return false;
1346
1347     case elfcpp::R_X86_64_SIZE32:
1348     case elfcpp::R_X86_64_SIZE64:
1349     default:
1350       // We will give an error later.
1351       return false;
1352     }
1353 }
1354
1355 // Scan a relocation for a local symbol.
1356
1357 inline void
1358 Target_x86_64::Scan::local(Symbol_table* symtab,
1359                            Layout* layout,
1360                            Target_x86_64* target,
1361                            Sized_relobj<64, false>* object,
1362                            unsigned int data_shndx,
1363                            Output_section* output_section,
1364                            const elfcpp::Rela<64, false>& reloc,
1365                            unsigned int r_type,
1366                            const elfcpp::Sym<64, false>& lsym)
1367 {
1368   // A local STT_GNU_IFUNC symbol may require a PLT entry.
1369   if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1370       && this->reloc_needs_plt_for_ifunc(object, r_type))
1371     {
1372       unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1373       target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1374     }
1375
1376   switch (r_type)
1377     {
1378     case elfcpp::R_X86_64_NONE:
1379     case elfcpp::R_X86_64_GNU_VTINHERIT:
1380     case elfcpp::R_X86_64_GNU_VTENTRY:
1381       break;
1382
1383     case elfcpp::R_X86_64_64:
1384       // If building a shared library (or a position-independent
1385       // executable), we need to create a dynamic relocation for this
1386       // location.  The relocation applied at link time will apply the
1387       // link-time value, so we flag the location with an
1388       // R_X86_64_RELATIVE relocation so the dynamic loader can
1389       // relocate it easily.
1390       if (parameters->options().output_is_position_independent())
1391         {
1392           unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1393           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1394           rela_dyn->add_local_relative(object, r_sym,
1395                                        elfcpp::R_X86_64_RELATIVE,
1396                                        output_section, data_shndx,
1397                                        reloc.get_r_offset(),
1398                                        reloc.get_r_addend());
1399         }
1400       break;
1401
1402     case elfcpp::R_X86_64_32:
1403     case elfcpp::R_X86_64_32S:
1404     case elfcpp::R_X86_64_16:
1405     case elfcpp::R_X86_64_8:
1406       // If building a shared library (or a position-independent
1407       // executable), we need to create a dynamic relocation for this
1408       // location.  We can't use an R_X86_64_RELATIVE relocation
1409       // because that is always a 64-bit relocation.
1410       if (parameters->options().output_is_position_independent())
1411         {
1412           this->check_non_pic(object, r_type);
1413
1414           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1415           unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1416           if (lsym.get_st_type() != elfcpp::STT_SECTION)
1417             rela_dyn->add_local(object, r_sym, r_type, output_section,
1418                                 data_shndx, reloc.get_r_offset(),
1419                                 reloc.get_r_addend());
1420           else
1421             {
1422               gold_assert(lsym.get_st_value() == 0);
1423               unsigned int shndx = lsym.get_st_shndx();
1424               bool is_ordinary;
1425               shndx = object->adjust_sym_shndx(r_sym, shndx,
1426                                                &is_ordinary);
1427               if (!is_ordinary)
1428                 object->error(_("section symbol %u has bad shndx %u"),
1429                               r_sym, shndx);
1430               else
1431                 rela_dyn->add_local_section(object, shndx,
1432                                             r_type, output_section,
1433                                             data_shndx, reloc.get_r_offset(),
1434                                             reloc.get_r_addend());
1435             }
1436         }
1437       break;
1438
1439     case elfcpp::R_X86_64_PC64:
1440     case elfcpp::R_X86_64_PC32:
1441     case elfcpp::R_X86_64_PC16:
1442     case elfcpp::R_X86_64_PC8:
1443       break;
1444
1445     case elfcpp::R_X86_64_PLT32:
1446       // Since we know this is a local symbol, we can handle this as a
1447       // PC32 reloc.
1448       break;
1449
1450     case elfcpp::R_X86_64_GOTPC32:
1451     case elfcpp::R_X86_64_GOTOFF64:
1452     case elfcpp::R_X86_64_GOTPC64:
1453     case elfcpp::R_X86_64_PLTOFF64:
1454       // We need a GOT section.
1455       target->got_section(symtab, layout);
1456       // For PLTOFF64, we'd normally want a PLT section, but since we
1457       // know this is a local symbol, no PLT is needed.
1458       break;
1459
1460     case elfcpp::R_X86_64_GOT64:
1461     case elfcpp::R_X86_64_GOT32:
1462     case elfcpp::R_X86_64_GOTPCREL64:
1463     case elfcpp::R_X86_64_GOTPCREL:
1464     case elfcpp::R_X86_64_GOTPLT64:
1465       {
1466         // The symbol requires a GOT entry.
1467         Output_data_got<64, false>* got = target->got_section(symtab, layout);
1468         unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1469
1470         // For a STT_GNU_IFUNC symbol we want the PLT offset.  That
1471         // lets function pointers compare correctly with shared
1472         // libraries.  Otherwise we would need an IRELATIVE reloc.
1473         bool is_new;
1474         if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1475           is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1476         else
1477           is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1478         if (is_new)
1479           {
1480             // If we are generating a shared object, we need to add a
1481             // dynamic relocation for this symbol's GOT entry.
1482             if (parameters->options().output_is_position_independent())
1483               {
1484                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1485                 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1486                 if (r_type != elfcpp::R_X86_64_GOT32)
1487                   {
1488                     unsigned int got_offset =
1489                       object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1490                     rela_dyn->add_local_relative(object, r_sym,
1491                                                  elfcpp::R_X86_64_RELATIVE,
1492                                                  got, got_offset, 0);
1493                   }
1494                 else
1495                   {
1496                     this->check_non_pic(object, r_type);
1497
1498                     gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1499                     rela_dyn->add_local(
1500                         object, r_sym, r_type, got,
1501                         object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1502                   }
1503               }
1504           }
1505         // For GOTPLT64, we'd normally want a PLT section, but since
1506         // we know this is a local symbol, no PLT is needed.
1507       }
1508       break;
1509
1510     case elfcpp::R_X86_64_COPY:
1511     case elfcpp::R_X86_64_GLOB_DAT:
1512     case elfcpp::R_X86_64_JUMP_SLOT:
1513     case elfcpp::R_X86_64_RELATIVE:
1514     case elfcpp::R_X86_64_IRELATIVE:
1515       // These are outstanding tls relocs, which are unexpected when linking
1516     case elfcpp::R_X86_64_TPOFF64:
1517     case elfcpp::R_X86_64_DTPMOD64:
1518     case elfcpp::R_X86_64_TLSDESC:
1519       gold_error(_("%s: unexpected reloc %u in object file"),
1520                  object->name().c_str(), r_type);
1521       break;
1522
1523       // These are initial tls relocs, which are expected when linking
1524     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1525     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1526     case elfcpp::R_X86_64_TLSDESC_CALL:
1527     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1528     case elfcpp::R_X86_64_DTPOFF32:
1529     case elfcpp::R_X86_64_DTPOFF64:
1530     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1531     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1532       {
1533         bool output_is_shared = parameters->options().shared();
1534         const tls::Tls_optimization optimized_type
1535             = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1536         switch (r_type)
1537           {
1538           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
1539             if (optimized_type == tls::TLSOPT_NONE)
1540               {
1541                 // Create a pair of GOT entries for the module index and
1542                 // dtv-relative offset.
1543                 Output_data_got<64, false>* got
1544                     = target->got_section(symtab, layout);
1545                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1546                 unsigned int shndx = lsym.get_st_shndx();
1547                 bool is_ordinary;
1548                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1549                 if (!is_ordinary)
1550                   object->error(_("local symbol %u has bad shndx %u"),
1551                               r_sym, shndx);
1552                 else
1553                   got->add_local_pair_with_rela(object, r_sym,
1554                                                 shndx,
1555                                                 GOT_TYPE_TLS_PAIR,
1556                                                 target->rela_dyn_section(layout),
1557                                                 elfcpp::R_X86_64_DTPMOD64, 0);
1558               }
1559             else if (optimized_type != tls::TLSOPT_TO_LE)
1560               unsupported_reloc_local(object, r_type);
1561             break;
1562
1563           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1564             target->define_tls_base_symbol(symtab, layout);
1565             if (optimized_type == tls::TLSOPT_NONE)
1566               {
1567                 // Create reserved PLT and GOT entries for the resolver.
1568                 target->reserve_tlsdesc_entries(symtab, layout);
1569
1570                 // Generate a double GOT entry with an
1571                 // R_X86_64_TLSDESC reloc.  The R_X86_64_TLSDESC reloc
1572                 // is resolved lazily, so the GOT entry needs to be in
1573                 // an area in .got.plt, not .got.  Call got_section to
1574                 // make sure the section has been created.
1575                 target->got_section(symtab, layout);
1576                 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1577                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1578                 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1579                   {
1580                     unsigned int got_offset = got->add_constant(0);
1581                     got->add_constant(0);
1582                     object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1583                                                  got_offset);
1584                     Reloc_section* rt = target->rela_tlsdesc_section(layout);
1585                     // We store the arguments we need in a vector, and
1586                     // use the index into the vector as the parameter
1587                     // to pass to the target specific routines.
1588                     uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1589                     void* arg = reinterpret_cast<void*>(intarg);
1590                     rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1591                                             got, got_offset, 0);
1592                   }
1593               }
1594             else if (optimized_type != tls::TLSOPT_TO_LE)
1595               unsupported_reloc_local(object, r_type);
1596             break;
1597
1598           case elfcpp::R_X86_64_TLSDESC_CALL:
1599             break;
1600
1601           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
1602             if (optimized_type == tls::TLSOPT_NONE)
1603               {
1604                 // Create a GOT entry for the module index.
1605                 target->got_mod_index_entry(symtab, layout, object);
1606               }
1607             else if (optimized_type != tls::TLSOPT_TO_LE)
1608               unsupported_reloc_local(object, r_type);
1609             break;
1610
1611           case elfcpp::R_X86_64_DTPOFF32:
1612           case elfcpp::R_X86_64_DTPOFF64:
1613             break;
1614
1615           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
1616             layout->set_has_static_tls();
1617             if (optimized_type == tls::TLSOPT_NONE)
1618               {
1619                 // Create a GOT entry for the tp-relative offset.
1620                 Output_data_got<64, false>* got
1621                     = target->got_section(symtab, layout);
1622                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1623                 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1624                                          target->rela_dyn_section(layout),
1625                                          elfcpp::R_X86_64_TPOFF64);
1626               }
1627             else if (optimized_type != tls::TLSOPT_TO_LE)
1628               unsupported_reloc_local(object, r_type);
1629             break;
1630
1631           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
1632             layout->set_has_static_tls();
1633             if (output_is_shared)
1634               unsupported_reloc_local(object, r_type);
1635             break;
1636
1637           default:
1638             gold_unreachable();
1639           }
1640       }
1641       break;
1642
1643     case elfcpp::R_X86_64_SIZE32:
1644     case elfcpp::R_X86_64_SIZE64:
1645     default:
1646       gold_error(_("%s: unsupported reloc %u against local symbol"),
1647                  object->name().c_str(), r_type);
1648       break;
1649     }
1650 }
1651
1652
1653 // Report an unsupported relocation against a global symbol.
1654
1655 void
1656 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1657                                               unsigned int r_type,
1658                                               Symbol* gsym)
1659 {
1660   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1661              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1662 }
1663
1664 // Returns true if this relocation type could be that of a function pointer.
1665 inline bool
1666 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
1667 {
1668   switch (r_type)
1669     {
1670     case elfcpp::R_X86_64_64:
1671     case elfcpp::R_X86_64_32:
1672     case elfcpp::R_X86_64_32S:
1673     case elfcpp::R_X86_64_16:
1674     case elfcpp::R_X86_64_8:
1675     case elfcpp::R_X86_64_GOT64:
1676     case elfcpp::R_X86_64_GOT32:
1677     case elfcpp::R_X86_64_GOTPCREL64:
1678     case elfcpp::R_X86_64_GOTPCREL:
1679     case elfcpp::R_X86_64_GOTPLT64:
1680       {
1681         return true;
1682       }
1683     }
1684   return false;
1685 }
1686
1687 // For safe ICF, scan a relocation for a local symbol to check if it
1688 // corresponds to a function pointer being taken.  In that case mark
1689 // the function whose pointer was taken as not foldable.
1690
1691 inline bool
1692 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
1693   Symbol_table* ,
1694   Layout* ,
1695   Target_x86_64* ,
1696   Sized_relobj<64, false>* ,
1697   unsigned int ,
1698   Output_section* ,
1699   const elfcpp::Rela<64, false>& ,
1700   unsigned int r_type,
1701   const elfcpp::Sym<64, false>&)
1702 {
1703   // When building a shared library, do not fold any local symbols as it is
1704   // not possible to distinguish pointer taken versus a call by looking at
1705   // the relocation types.
1706   return (parameters->options().shared()
1707           || possible_function_pointer_reloc(r_type));
1708 }
1709
1710 // For safe ICF, scan a relocation for a global symbol to check if it
1711 // corresponds to a function pointer being taken.  In that case mark
1712 // the function whose pointer was taken as not foldable.
1713
1714 inline bool
1715 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
1716   Symbol_table*,
1717   Layout* ,
1718   Target_x86_64* ,
1719   Sized_relobj<64, false>* ,
1720   unsigned int ,
1721   Output_section* ,
1722   const elfcpp::Rela<64, false>& ,
1723   unsigned int r_type,
1724   Symbol* gsym)
1725 {
1726   // When building a shared library, do not fold symbols whose visibility
1727   // is hidden, internal or protected.
1728   return ((parameters->options().shared()
1729            && (gsym->visibility() == elfcpp::STV_INTERNAL
1730                || gsym->visibility() == elfcpp::STV_PROTECTED
1731                || gsym->visibility() == elfcpp::STV_HIDDEN))
1732           || possible_function_pointer_reloc(r_type));
1733 }
1734
1735 // Scan a relocation for a global symbol.
1736
1737 inline void
1738 Target_x86_64::Scan::global(Symbol_table* symtab,
1739                             Layout* layout,
1740                             Target_x86_64* target,
1741                             Sized_relobj<64, false>* object,
1742                             unsigned int data_shndx,
1743                             Output_section* output_section,
1744                             const elfcpp::Rela<64, false>& reloc,
1745                             unsigned int r_type,
1746                             Symbol* gsym)
1747 {
1748   // A STT_GNU_IFUNC symbol may require a PLT entry.
1749   if (gsym->type() == elfcpp::STT_GNU_IFUNC
1750       && this->reloc_needs_plt_for_ifunc(object, r_type))
1751     target->make_plt_entry(symtab, layout, gsym);
1752
1753   switch (r_type)
1754     {
1755     case elfcpp::R_X86_64_NONE:
1756     case elfcpp::R_X86_64_GNU_VTINHERIT:
1757     case elfcpp::R_X86_64_GNU_VTENTRY:
1758       break;
1759
1760     case elfcpp::R_X86_64_64:
1761     case elfcpp::R_X86_64_32:
1762     case elfcpp::R_X86_64_32S:
1763     case elfcpp::R_X86_64_16:
1764     case elfcpp::R_X86_64_8:
1765       {
1766         // Make a PLT entry if necessary.
1767         if (gsym->needs_plt_entry())
1768           {
1769             target->make_plt_entry(symtab, layout, gsym);
1770             // Since this is not a PC-relative relocation, we may be
1771             // taking the address of a function. In that case we need to
1772             // set the entry in the dynamic symbol table to the address of
1773             // the PLT entry.
1774             if (gsym->is_from_dynobj() && !parameters->options().shared())
1775               gsym->set_needs_dynsym_value();
1776           }
1777         // Make a dynamic relocation if necessary.
1778         if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1779           {
1780             if (gsym->may_need_copy_reloc())
1781               {
1782                 target->copy_reloc(symtab, layout, object,
1783                                    data_shndx, output_section, gsym, reloc);
1784               }
1785             else if (r_type == elfcpp::R_X86_64_64
1786                      && gsym->type() == elfcpp::STT_GNU_IFUNC
1787                      && gsym->can_use_relative_reloc(false)
1788                      && !gsym->is_from_dynobj()
1789                      && !gsym->is_undefined()
1790                      && !gsym->is_preemptible())
1791               {
1792                 // Use an IRELATIVE reloc for a locally defined
1793                 // STT_GNU_IFUNC symbol.  This makes a function
1794                 // address in a PIE executable match the address in a
1795                 // shared library that it links against.
1796                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1797                 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE;
1798                 rela_dyn->add_symbolless_global_addend(gsym, r_type,
1799                                                        output_section, object,
1800                                                        data_shndx,
1801                                                        reloc.get_r_offset(),
1802                                                        reloc.get_r_addend());
1803               }
1804             else if (r_type == elfcpp::R_X86_64_64
1805                      && gsym->can_use_relative_reloc(false))
1806               {
1807                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1808                 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1809                                               output_section, object,
1810                                               data_shndx,
1811                                               reloc.get_r_offset(),
1812                                               reloc.get_r_addend());
1813               }
1814             else
1815               {
1816                 this->check_non_pic(object, r_type);
1817                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1818                 rela_dyn->add_global(gsym, r_type, output_section, object,
1819                                      data_shndx, reloc.get_r_offset(),
1820                                      reloc.get_r_addend());
1821               }
1822           }
1823       }
1824       break;
1825
1826     case elfcpp::R_X86_64_PC64:
1827     case elfcpp::R_X86_64_PC32:
1828     case elfcpp::R_X86_64_PC16:
1829     case elfcpp::R_X86_64_PC8:
1830       {
1831         // Make a PLT entry if necessary.
1832         if (gsym->needs_plt_entry())
1833           target->make_plt_entry(symtab, layout, gsym);
1834         // Make a dynamic relocation if necessary.
1835         int flags = Symbol::NON_PIC_REF;
1836         if (gsym->is_func())
1837           flags |= Symbol::FUNCTION_CALL;
1838         if (gsym->needs_dynamic_reloc(flags))
1839           {
1840             if (gsym->may_need_copy_reloc())
1841               {
1842                 target->copy_reloc(symtab, layout, object,
1843                                    data_shndx, output_section, gsym, reloc);
1844               }
1845             else
1846               {
1847                 this->check_non_pic(object, r_type);
1848                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1849                 rela_dyn->add_global(gsym, r_type, output_section, object,
1850                                      data_shndx, reloc.get_r_offset(),
1851                                      reloc.get_r_addend());
1852               }
1853           }
1854       }
1855       break;
1856
1857     case elfcpp::R_X86_64_GOT64:
1858     case elfcpp::R_X86_64_GOT32:
1859     case elfcpp::R_X86_64_GOTPCREL64:
1860     case elfcpp::R_X86_64_GOTPCREL:
1861     case elfcpp::R_X86_64_GOTPLT64:
1862       {
1863         // The symbol requires a GOT entry.
1864         Output_data_got<64, false>* got = target->got_section(symtab, layout);
1865         if (gsym->final_value_is_known())
1866           {
1867             // For a STT_GNU_IFUNC symbol we want the PLT address.
1868             if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1869               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1870             else
1871               got->add_global(gsym, GOT_TYPE_STANDARD);
1872           }
1873         else
1874           {
1875             // If this symbol is not fully resolved, we need to add a
1876             // dynamic relocation for it.
1877             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1878             if (gsym->is_from_dynobj()
1879                 || gsym->is_undefined()
1880                 || gsym->is_preemptible()
1881                 || (gsym->type() == elfcpp::STT_GNU_IFUNC
1882                     && parameters->options().output_is_position_independent()))
1883               got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1884                                         elfcpp::R_X86_64_GLOB_DAT);
1885             else
1886               {
1887                 // For a STT_GNU_IFUNC symbol we want to write the PLT
1888                 // offset into the GOT, so that function pointer
1889                 // comparisons work correctly.
1890                 bool is_new;
1891                 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
1892                   is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
1893                 else
1894                   {
1895                     is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1896                     // Tell the dynamic linker to use the PLT address
1897                     // when resolving relocations.
1898                     if (gsym->is_from_dynobj()
1899                         && !parameters->options().shared())
1900                       gsym->set_needs_dynsym_value();
1901                   }
1902                 if (is_new)
1903                   {
1904                     unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
1905                     rela_dyn->add_global_relative(gsym,
1906                                                   elfcpp::R_X86_64_RELATIVE,
1907                                                   got, got_off, 0);
1908                   }
1909               }
1910           }
1911         // For GOTPLT64, we also need a PLT entry (but only if the
1912         // symbol is not fully resolved).
1913         if (r_type == elfcpp::R_X86_64_GOTPLT64
1914             && !gsym->final_value_is_known())
1915           target->make_plt_entry(symtab, layout, gsym);
1916       }
1917       break;
1918
1919     case elfcpp::R_X86_64_PLT32:
1920       // If the symbol is fully resolved, this is just a PC32 reloc.
1921       // Otherwise we need a PLT entry.
1922       if (gsym->final_value_is_known())
1923         break;
1924       // If building a shared library, we can also skip the PLT entry
1925       // if the symbol is defined in the output file and is protected
1926       // or hidden.
1927       if (gsym->is_defined()
1928           && !gsym->is_from_dynobj()
1929           && !gsym->is_preemptible())
1930         break;
1931       target->make_plt_entry(symtab, layout, gsym);
1932       break;
1933
1934     case elfcpp::R_X86_64_GOTPC32:
1935     case elfcpp::R_X86_64_GOTOFF64:
1936     case elfcpp::R_X86_64_GOTPC64:
1937     case elfcpp::R_X86_64_PLTOFF64:
1938       // We need a GOT section.
1939       target->got_section(symtab, layout);
1940       // For PLTOFF64, we also need a PLT entry (but only if the
1941       // symbol is not fully resolved).
1942       if (r_type == elfcpp::R_X86_64_PLTOFF64
1943           && !gsym->final_value_is_known())
1944         target->make_plt_entry(symtab, layout, gsym);
1945       break;
1946
1947     case elfcpp::R_X86_64_COPY:
1948     case elfcpp::R_X86_64_GLOB_DAT:
1949     case elfcpp::R_X86_64_JUMP_SLOT:
1950     case elfcpp::R_X86_64_RELATIVE:
1951     case elfcpp::R_X86_64_IRELATIVE:
1952       // These are outstanding tls relocs, which are unexpected when linking
1953     case elfcpp::R_X86_64_TPOFF64:
1954     case elfcpp::R_X86_64_DTPMOD64:
1955     case elfcpp::R_X86_64_TLSDESC:
1956       gold_error(_("%s: unexpected reloc %u in object file"),
1957                  object->name().c_str(), r_type);
1958       break;
1959
1960       // These are initial tls relocs, which are expected for global()
1961     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1962     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1963     case elfcpp::R_X86_64_TLSDESC_CALL:
1964     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1965     case elfcpp::R_X86_64_DTPOFF32:
1966     case elfcpp::R_X86_64_DTPOFF64:
1967     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1968     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1969       {
1970         const bool is_final = gsym->final_value_is_known();
1971         const tls::Tls_optimization optimized_type
1972             = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1973         switch (r_type)
1974           {
1975           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
1976             if (optimized_type == tls::TLSOPT_NONE)
1977               {
1978                 // Create a pair of GOT entries for the module index and
1979                 // dtv-relative offset.
1980                 Output_data_got<64, false>* got
1981                     = target->got_section(symtab, layout);
1982                 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1983                                                target->rela_dyn_section(layout),
1984                                                elfcpp::R_X86_64_DTPMOD64,
1985                                                elfcpp::R_X86_64_DTPOFF64);
1986               }
1987             else if (optimized_type == tls::TLSOPT_TO_IE)
1988               {
1989                 // Create a GOT entry for the tp-relative offset.
1990                 Output_data_got<64, false>* got
1991                     = target->got_section(symtab, layout);
1992                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1993                                           target->rela_dyn_section(layout),
1994                                           elfcpp::R_X86_64_TPOFF64);
1995               }
1996             else if (optimized_type != tls::TLSOPT_TO_LE)
1997               unsupported_reloc_global(object, r_type, gsym);
1998             break;
1999
2000           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
2001             target->define_tls_base_symbol(symtab, layout);
2002             if (optimized_type == tls::TLSOPT_NONE)
2003               {
2004                 // Create reserved PLT and GOT entries for the resolver.
2005                 target->reserve_tlsdesc_entries(symtab, layout);
2006
2007                 // Create a double GOT entry with an R_X86_64_TLSDESC
2008                 // reloc.  The R_X86_64_TLSDESC reloc is resolved
2009                 // lazily, so the GOT entry needs to be in an area in
2010                 // .got.plt, not .got.  Call got_section to make sure
2011                 // the section has been created.
2012                 target->got_section(symtab, layout);
2013                 Output_data_got<64, false>* got = target->got_tlsdesc_section();
2014                 Reloc_section* rt = target->rela_tlsdesc_section(layout);
2015                 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
2016                                                elfcpp::R_X86_64_TLSDESC, 0);
2017               }
2018             else if (optimized_type == tls::TLSOPT_TO_IE)
2019               {
2020                 // Create a GOT entry for the tp-relative offset.
2021                 Output_data_got<64, false>* got
2022                     = target->got_section(symtab, layout);
2023                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2024                                           target->rela_dyn_section(layout),
2025                                           elfcpp::R_X86_64_TPOFF64);
2026               }
2027             else if (optimized_type != tls::TLSOPT_TO_LE)
2028               unsupported_reloc_global(object, r_type, gsym);
2029             break;
2030
2031           case elfcpp::R_X86_64_TLSDESC_CALL:
2032             break;
2033
2034           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
2035             if (optimized_type == tls::TLSOPT_NONE)
2036               {
2037                 // Create a GOT entry for the module index.
2038                 target->got_mod_index_entry(symtab, layout, object);
2039               }
2040             else if (optimized_type != tls::TLSOPT_TO_LE)
2041               unsupported_reloc_global(object, r_type, gsym);
2042             break;
2043
2044           case elfcpp::R_X86_64_DTPOFF32:
2045           case elfcpp::R_X86_64_DTPOFF64:
2046             break;
2047
2048           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
2049             layout->set_has_static_tls();
2050             if (optimized_type == tls::TLSOPT_NONE)
2051               {
2052                 // Create a GOT entry for the tp-relative offset.
2053                 Output_data_got<64, false>* got
2054                     = target->got_section(symtab, layout);
2055                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
2056                                           target->rela_dyn_section(layout),
2057                                           elfcpp::R_X86_64_TPOFF64);
2058               }
2059             else if (optimized_type != tls::TLSOPT_TO_LE)
2060               unsupported_reloc_global(object, r_type, gsym);
2061             break;
2062
2063           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
2064             layout->set_has_static_tls();
2065             if (parameters->options().shared())
2066               unsupported_reloc_local(object, r_type);
2067             break;
2068
2069           default:
2070             gold_unreachable();
2071           }
2072       }
2073       break;
2074
2075     case elfcpp::R_X86_64_SIZE32:
2076     case elfcpp::R_X86_64_SIZE64:
2077     default:
2078       gold_error(_("%s: unsupported reloc %u against global symbol %s"),
2079                  object->name().c_str(), r_type,
2080                  gsym->demangled_name().c_str());
2081       break;
2082     }
2083 }
2084
2085 void
2086 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
2087                                  Layout* layout,
2088                                  Sized_relobj<64, false>* object,
2089                                  unsigned int data_shndx,
2090                                  unsigned int sh_type,
2091                                  const unsigned char* prelocs,
2092                                  size_t reloc_count,
2093                                  Output_section* output_section,
2094                                  bool needs_special_offset_handling,
2095                                  size_t local_symbol_count,
2096                                  const unsigned char* plocal_symbols)
2097 {
2098
2099   if (sh_type == elfcpp::SHT_REL)
2100     {
2101       return;
2102     }
2103
2104    gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2105                            Target_x86_64::Scan,
2106                            Target_x86_64::Relocatable_size_for_reloc>(
2107     symtab,
2108     layout,
2109     this,
2110     object,
2111     data_shndx,
2112     prelocs,
2113     reloc_count,
2114     output_section,
2115     needs_special_offset_handling,
2116     local_symbol_count,
2117     plocal_symbols);
2118  
2119 }
2120 // Scan relocations for a section.
2121
2122 void
2123 Target_x86_64::scan_relocs(Symbol_table* symtab,
2124                            Layout* layout,
2125                            Sized_relobj<64, false>* object,
2126                            unsigned int data_shndx,
2127                            unsigned int sh_type,
2128                            const unsigned char* prelocs,
2129                            size_t reloc_count,
2130                            Output_section* output_section,
2131                            bool needs_special_offset_handling,
2132                            size_t local_symbol_count,
2133                            const unsigned char* plocal_symbols)
2134 {
2135   if (sh_type == elfcpp::SHT_REL)
2136     {
2137       gold_error(_("%s: unsupported REL reloc section"),
2138                  object->name().c_str());
2139       return;
2140     }
2141
2142   gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
2143       Target_x86_64::Scan>(
2144     symtab,
2145     layout,
2146     this,
2147     object,
2148     data_shndx,
2149     prelocs,
2150     reloc_count,
2151     output_section,
2152     needs_special_offset_handling,
2153     local_symbol_count,
2154     plocal_symbols);
2155 }
2156
2157 // Finalize the sections.
2158
2159 void
2160 Target_x86_64::do_finalize_sections(
2161     Layout* layout,
2162     const Input_objects*,
2163     Symbol_table* symtab)
2164 {
2165   const Reloc_section* rel_plt = (this->plt_ == NULL
2166                                   ? NULL
2167                                   : this->plt_->rela_plt());
2168   layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
2169                                   this->rela_dyn_, true, false);
2170                                   
2171   // Fill in some more dynamic tags.
2172   Output_data_dynamic* const odyn = layout->dynamic_data();
2173   if (odyn != NULL)
2174     {
2175       if (this->plt_ != NULL
2176           && this->plt_->output_section() != NULL
2177           && this->plt_->has_tlsdesc_entry())
2178         {
2179           unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
2180           unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
2181           this->got_->finalize_data_size();
2182           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
2183                                         this->plt_, plt_offset);
2184           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
2185                                         this->got_, got_offset);
2186         }
2187     }
2188
2189   // Emit any relocs we saved in an attempt to avoid generating COPY
2190   // relocs.
2191   if (this->copy_relocs_.any_saved_relocs())
2192     this->copy_relocs_.emit(this->rela_dyn_section(layout));
2193
2194   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2195   // the .got.plt section.
2196   Symbol* sym = this->global_offset_table_;
2197   if (sym != NULL)
2198     {
2199       uint64_t data_size = this->got_plt_->current_data_size();
2200       symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
2201     }
2202 }
2203
2204 // Perform a relocation.
2205
2206 inline bool
2207 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
2208                                   Target_x86_64* target,
2209                                   Output_section*,
2210                                   size_t relnum,
2211                                   const elfcpp::Rela<64, false>& rela,
2212                                   unsigned int r_type,
2213                                   const Sized_symbol<64>* gsym,
2214                                   const Symbol_value<64>* psymval,
2215                                   unsigned char* view,
2216                                   elfcpp::Elf_types<64>::Elf_Addr address,
2217                                   section_size_type view_size)
2218 {
2219   if (this->skip_call_tls_get_addr_)
2220     {
2221       if ((r_type != elfcpp::R_X86_64_PLT32
2222            && r_type != elfcpp::R_X86_64_PC32)
2223           || gsym == NULL
2224           || strcmp(gsym->name(), "__tls_get_addr") != 0)
2225         {
2226           gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2227                                  _("missing expected TLS relocation"));
2228         }
2229       else
2230         {
2231           this->skip_call_tls_get_addr_ = false;
2232           return false;
2233         }
2234     }
2235
2236   const Sized_relobj<64, false>* object = relinfo->object;
2237
2238   // Pick the value to use for symbols defined in the PLT.
2239   Symbol_value<64> symval;
2240   if (gsym != NULL
2241       && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
2242                               || r_type == elfcpp::R_X86_64_PC32
2243                               || r_type == elfcpp::R_X86_64_PC16
2244                               || r_type == elfcpp::R_X86_64_PC8))
2245     {
2246       symval.set_output_value(target->plt_section()->address()
2247                               + gsym->plt_offset());
2248       psymval = &symval;
2249     }
2250   else if (gsym == NULL && psymval->is_ifunc_symbol())
2251     {
2252       unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2253       if (object->local_has_plt_offset(r_sym))
2254         {
2255           symval.set_output_value(target->plt_section()->address()
2256                                   + object->local_plt_offset(r_sym));
2257           psymval = &symval;
2258         }
2259     }
2260
2261   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2262
2263   // Get the GOT offset if needed.
2264   // The GOT pointer points to the end of the GOT section.
2265   // We need to subtract the size of the GOT section to get
2266   // the actual offset to use in the relocation.
2267   bool have_got_offset = false;
2268   unsigned int got_offset = 0;
2269   switch (r_type)
2270     {
2271     case elfcpp::R_X86_64_GOT32:
2272     case elfcpp::R_X86_64_GOT64:
2273     case elfcpp::R_X86_64_GOTPLT64:
2274     case elfcpp::R_X86_64_GOTPCREL:
2275     case elfcpp::R_X86_64_GOTPCREL64:
2276       if (gsym != NULL)
2277         {
2278           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2279           got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
2280         }
2281       else
2282         {
2283           unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2284           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2285           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2286                         - target->got_size());
2287         }
2288       have_got_offset = true;
2289       break;
2290
2291     default:
2292       break;
2293     }
2294
2295   switch (r_type)
2296     {
2297     case elfcpp::R_X86_64_NONE:
2298     case elfcpp::R_X86_64_GNU_VTINHERIT:
2299     case elfcpp::R_X86_64_GNU_VTENTRY:
2300       break;
2301
2302     case elfcpp::R_X86_64_64:
2303       Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2304       break;
2305
2306     case elfcpp::R_X86_64_PC64:
2307       Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2308                                               address);
2309       break;
2310
2311     case elfcpp::R_X86_64_32:
2312       // FIXME: we need to verify that value + addend fits into 32 bits:
2313       //    uint64_t x = value + addend;
2314       //    x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2315       // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2316       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2317       break;
2318
2319     case elfcpp::R_X86_64_32S:
2320       // FIXME: we need to verify that value + addend fits into 32 bits:
2321       //    int64_t x = value + addend;   // note this quantity is signed!
2322       //    x == static_cast<int64_t>(static_cast<int32_t>(x))
2323       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2324       break;
2325
2326     case elfcpp::R_X86_64_PC32:
2327       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2328                                               address);
2329       break;
2330
2331     case elfcpp::R_X86_64_16:
2332       Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2333       break;
2334
2335     case elfcpp::R_X86_64_PC16:
2336       Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2337                                               address);
2338       break;
2339
2340     case elfcpp::R_X86_64_8:
2341       Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2342       break;
2343
2344     case elfcpp::R_X86_64_PC8:
2345       Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2346                                              address);
2347       break;
2348
2349     case elfcpp::R_X86_64_PLT32:
2350       gold_assert(gsym == NULL
2351                   || gsym->has_plt_offset()
2352                   || gsym->final_value_is_known()
2353                   || (gsym->is_defined()
2354                       && !gsym->is_from_dynobj()
2355                       && !gsym->is_preemptible()));
2356       // Note: while this code looks the same as for R_X86_64_PC32, it
2357       // behaves differently because psymval was set to point to
2358       // the PLT entry, rather than the symbol, in Scan::global().
2359       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2360                                               address);
2361       break;
2362
2363     case elfcpp::R_X86_64_PLTOFF64:
2364       {
2365         gold_assert(gsym);
2366         gold_assert(gsym->has_plt_offset()
2367                     || gsym->final_value_is_known());
2368         elfcpp::Elf_types<64>::Elf_Addr got_address;
2369         got_address = target->got_section(NULL, NULL)->address();
2370         Relocate_functions<64, false>::rela64(view, object, psymval,
2371                                               addend - got_address);
2372       }
2373
2374     case elfcpp::R_X86_64_GOT32:
2375       gold_assert(have_got_offset);
2376       Relocate_functions<64, false>::rela32(view, got_offset, addend);
2377       break;
2378
2379     case elfcpp::R_X86_64_GOTPC32:
2380       {
2381         gold_assert(gsym);
2382         elfcpp::Elf_types<64>::Elf_Addr value;
2383         value = target->got_plt_section()->address();
2384         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2385       }
2386       break;
2387
2388     case elfcpp::R_X86_64_GOT64:
2389       // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2390       // Since we always add a PLT entry, this is equivalent.
2391     case elfcpp::R_X86_64_GOTPLT64:
2392       gold_assert(have_got_offset);
2393       Relocate_functions<64, false>::rela64(view, got_offset, addend);
2394       break;
2395
2396     case elfcpp::R_X86_64_GOTPC64:
2397       {
2398         gold_assert(gsym);
2399         elfcpp::Elf_types<64>::Elf_Addr value;
2400         value = target->got_plt_section()->address();
2401         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2402       }
2403       break;
2404
2405     case elfcpp::R_X86_64_GOTOFF64:
2406       {
2407         elfcpp::Elf_types<64>::Elf_Addr value;
2408         value = (psymval->value(object, 0)
2409                  - target->got_plt_section()->address());
2410         Relocate_functions<64, false>::rela64(view, value, addend);
2411       }
2412       break;
2413
2414     case elfcpp::R_X86_64_GOTPCREL:
2415       {
2416         gold_assert(have_got_offset);
2417         elfcpp::Elf_types<64>::Elf_Addr value;
2418         value = target->got_plt_section()->address() + got_offset;
2419         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2420       }
2421       break;
2422
2423     case elfcpp::R_X86_64_GOTPCREL64:
2424       {
2425         gold_assert(have_got_offset);
2426         elfcpp::Elf_types<64>::Elf_Addr value;
2427         value = target->got_plt_section()->address() + got_offset;
2428         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2429       }
2430       break;
2431
2432     case elfcpp::R_X86_64_COPY:
2433     case elfcpp::R_X86_64_GLOB_DAT:
2434     case elfcpp::R_X86_64_JUMP_SLOT:
2435     case elfcpp::R_X86_64_RELATIVE:
2436     case elfcpp::R_X86_64_IRELATIVE:
2437       // These are outstanding tls relocs, which are unexpected when linking
2438     case elfcpp::R_X86_64_TPOFF64:
2439     case elfcpp::R_X86_64_DTPMOD64:
2440     case elfcpp::R_X86_64_TLSDESC:
2441       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2442                              _("unexpected reloc %u in object file"),
2443                              r_type);
2444       break;
2445
2446       // These are initial tls relocs, which are expected when linking
2447     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2448     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2449     case elfcpp::R_X86_64_TLSDESC_CALL:
2450     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2451     case elfcpp::R_X86_64_DTPOFF32:
2452     case elfcpp::R_X86_64_DTPOFF64:
2453     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2454     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2455       this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2456                          view, address, view_size);
2457       break;
2458
2459     case elfcpp::R_X86_64_SIZE32:
2460     case elfcpp::R_X86_64_SIZE64:
2461     default:
2462       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2463                              _("unsupported reloc %u"),
2464                              r_type);
2465       break;
2466     }
2467
2468   return true;
2469 }
2470
2471 // Perform a TLS relocation.
2472
2473 inline void
2474 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
2475                                       Target_x86_64* target,
2476                                       size_t relnum,
2477                                       const elfcpp::Rela<64, false>& rela,
2478                                       unsigned int r_type,
2479                                       const Sized_symbol<64>* gsym,
2480                                       const Symbol_value<64>* psymval,
2481                                       unsigned char* view,
2482                                       elfcpp::Elf_types<64>::Elf_Addr address,
2483                                       section_size_type view_size)
2484 {
2485   Output_segment* tls_segment = relinfo->layout->tls_segment();
2486
2487   const Sized_relobj<64, false>* object = relinfo->object;
2488   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2489   elfcpp::Shdr<64, false> data_shdr(relinfo->data_shdr);
2490   bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
2491
2492   elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2493
2494   const bool is_final = (gsym == NULL
2495                          ? !parameters->options().shared()
2496                          : gsym->final_value_is_known());
2497   tls::Tls_optimization optimized_type
2498       = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2499   switch (r_type)
2500     {
2501     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2502       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2503         {
2504           // If this code sequence is used in a non-executable section,
2505           // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
2506           // on the assumption that it's being used by itself in a debug
2507           // section.  Therefore, in the unlikely event that the code
2508           // sequence appears in a non-executable section, we simply
2509           // leave it unoptimized.
2510           optimized_type = tls::TLSOPT_NONE;
2511         }
2512       if (optimized_type == tls::TLSOPT_TO_LE)
2513         {
2514           gold_assert(tls_segment != NULL);
2515           this->tls_gd_to_le(relinfo, relnum, tls_segment,
2516                              rela, r_type, value, view,
2517                              view_size);
2518           break;
2519         }
2520       else
2521         {
2522           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2523                                    ? GOT_TYPE_TLS_OFFSET
2524                                    : GOT_TYPE_TLS_PAIR);
2525           unsigned int got_offset;
2526           if (gsym != NULL)
2527             {
2528               gold_assert(gsym->has_got_offset(got_type));
2529               got_offset = gsym->got_offset(got_type) - target->got_size();
2530             }
2531           else
2532             {
2533               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2534               gold_assert(object->local_has_got_offset(r_sym, got_type));
2535               got_offset = (object->local_got_offset(r_sym, got_type)
2536                             - target->got_size());
2537             }
2538           if (optimized_type == tls::TLSOPT_TO_IE)
2539             {
2540               gold_assert(tls_segment != NULL);
2541               value = target->got_plt_section()->address() + got_offset;
2542               this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2543                                  value, view, address, view_size);
2544               break;
2545             }
2546           else if (optimized_type == tls::TLSOPT_NONE)
2547             {
2548               // Relocate the field with the offset of the pair of GOT
2549               // entries.
2550               value = target->got_plt_section()->address() + got_offset;
2551               Relocate_functions<64, false>::pcrela32(view, value, addend,
2552                                                       address);
2553               break;
2554             }
2555         }
2556       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2557                              _("unsupported reloc %u"), r_type);
2558       break;
2559
2560     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2561     case elfcpp::R_X86_64_TLSDESC_CALL:
2562       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2563         {
2564           // See above comment for R_X86_64_TLSGD.
2565           optimized_type = tls::TLSOPT_NONE;
2566         }
2567       if (optimized_type == tls::TLSOPT_TO_LE)
2568         {
2569           gold_assert(tls_segment != NULL);
2570           this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2571                                   rela, r_type, value, view,
2572                                   view_size);
2573           break;
2574         }
2575       else
2576         {
2577           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2578                                    ? GOT_TYPE_TLS_OFFSET
2579                                    : GOT_TYPE_TLS_DESC);
2580           unsigned int got_offset = 0;
2581           if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2582               && optimized_type == tls::TLSOPT_NONE)
2583             {
2584               // We created GOT entries in the .got.tlsdesc portion of
2585               // the .got.plt section, but the offset stored in the
2586               // symbol is the offset within .got.tlsdesc.
2587               got_offset = (target->got_size()
2588                             + target->got_plt_section()->data_size());
2589             }
2590           if (gsym != NULL)
2591             {
2592               gold_assert(gsym->has_got_offset(got_type));
2593               got_offset += gsym->got_offset(got_type) - target->got_size();
2594             }
2595           else
2596             {
2597               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2598               gold_assert(object->local_has_got_offset(r_sym, got_type));
2599               got_offset += (object->local_got_offset(r_sym, got_type)
2600                              - target->got_size());
2601             }
2602           if (optimized_type == tls::TLSOPT_TO_IE)
2603             {
2604               gold_assert(tls_segment != NULL);
2605               value = target->got_plt_section()->address() + got_offset;
2606               this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2607                                       rela, r_type, value, view, address,
2608                                       view_size);
2609               break;
2610             }
2611           else if (optimized_type == tls::TLSOPT_NONE)
2612             {
2613               if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2614                 {
2615                   // Relocate the field with the offset of the pair of GOT
2616                   // entries.
2617                   value = target->got_plt_section()->address() + got_offset;
2618                   Relocate_functions<64, false>::pcrela32(view, value, addend,
2619                                                           address);
2620                 }
2621               break;
2622             }
2623         }
2624       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2625                              _("unsupported reloc %u"), r_type);
2626       break;
2627
2628     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2629       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
2630         {
2631           // See above comment for R_X86_64_TLSGD.
2632           optimized_type = tls::TLSOPT_NONE;
2633         }
2634       if (optimized_type == tls::TLSOPT_TO_LE)
2635         {
2636           gold_assert(tls_segment != NULL);
2637           this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2638                              value, view, view_size);
2639           break;
2640         }
2641       else if (optimized_type == tls::TLSOPT_NONE)
2642         {
2643           // Relocate the field with the offset of the GOT entry for
2644           // the module index.
2645           unsigned int got_offset;
2646           got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2647                         - target->got_size());
2648           value = target->got_plt_section()->address() + got_offset;
2649           Relocate_functions<64, false>::pcrela32(view, value, addend,
2650                                                   address);
2651           break;
2652         }
2653       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2654                              _("unsupported reloc %u"), r_type);
2655       break;
2656
2657     case elfcpp::R_X86_64_DTPOFF32:
2658       // This relocation type is used in debugging information.
2659       // In that case we need to not optimize the value.  If the
2660       // section is not executable, then we assume we should not
2661       // optimize this reloc.  See comments above for R_X86_64_TLSGD,
2662       // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
2663       // R_X86_64_TLSLD.
2664       if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2665         {
2666           gold_assert(tls_segment != NULL);
2667           value -= tls_segment->memsz();
2668         }
2669       Relocate_functions<64, false>::rela32(view, value, addend);
2670       break;
2671
2672     case elfcpp::R_X86_64_DTPOFF64:
2673       // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
2674       if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
2675         {
2676           gold_assert(tls_segment != NULL);
2677           value -= tls_segment->memsz();
2678         }
2679       Relocate_functions<64, false>::rela64(view, value, addend);
2680       break;
2681
2682     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2683       if (optimized_type == tls::TLSOPT_TO_LE)
2684         {
2685           gold_assert(tls_segment != NULL);
2686           Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2687                                                 rela, r_type, value, view,
2688                                                 view_size);
2689           break;
2690         }
2691       else if (optimized_type == tls::TLSOPT_NONE)
2692         {
2693           // Relocate the field with the offset of the GOT entry for
2694           // the tp-relative offset of the symbol.
2695           unsigned int got_offset;
2696           if (gsym != NULL)
2697             {
2698               gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2699               got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2700                             - target->got_size());
2701             }
2702           else
2703             {
2704               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2705               gold_assert(object->local_has_got_offset(r_sym,
2706                                                        GOT_TYPE_TLS_OFFSET));
2707               got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2708                             - target->got_size());
2709             }
2710           value = target->got_plt_section()->address() + got_offset;
2711           Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2712           break;
2713         }
2714       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2715                              _("unsupported reloc type %u"),
2716                              r_type);
2717       break;
2718
2719     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2720       value -= tls_segment->memsz();
2721       Relocate_functions<64, false>::rela32(view, value, addend);
2722       break;
2723     }
2724 }
2725
2726 // Do a relocation in which we convert a TLS General-Dynamic to an
2727 // Initial-Exec.
2728
2729 inline void
2730 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2731                                       size_t relnum,
2732                                       Output_segment*,
2733                                       const elfcpp::Rela<64, false>& rela,
2734                                       unsigned int,
2735                                       elfcpp::Elf_types<64>::Elf_Addr value,
2736                                       unsigned char* view,
2737                                       elfcpp::Elf_types<64>::Elf_Addr address,
2738                                       section_size_type view_size)
2739 {
2740   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2741   // .word 0x6666; rex64; call __tls_get_addr
2742   // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2743
2744   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2745   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2746
2747   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2748                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2749   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2750                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2751
2752   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2753
2754   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2755   Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2756
2757   // The next reloc should be a PLT32 reloc against __tls_get_addr.
2758   // We can skip it.
2759   this->skip_call_tls_get_addr_ = true;
2760 }
2761
2762 // Do a relocation in which we convert a TLS General-Dynamic to a
2763 // Local-Exec.
2764
2765 inline void
2766 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2767                                       size_t relnum,
2768                                       Output_segment* tls_segment,
2769                                       const elfcpp::Rela<64, false>& rela,
2770                                       unsigned int,
2771                                       elfcpp::Elf_types<64>::Elf_Addr value,
2772                                       unsigned char* view,
2773                                       section_size_type view_size)
2774 {
2775   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2776   // .word 0x6666; rex64; call __tls_get_addr
2777   // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2778
2779   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2780   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2781
2782   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2783                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2784   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2785                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2786
2787   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2788
2789   value -= tls_segment->memsz();
2790   Relocate_functions<64, false>::rela32(view + 8, value, 0);
2791
2792   // The next reloc should be a PLT32 reloc against __tls_get_addr.
2793   // We can skip it.
2794   this->skip_call_tls_get_addr_ = true;
2795 }
2796
2797 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2798
2799 inline void
2800 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2801     const Relocate_info<64, false>* relinfo,
2802     size_t relnum,
2803     Output_segment*,
2804     const elfcpp::Rela<64, false>& rela,
2805     unsigned int r_type,
2806     elfcpp::Elf_types<64>::Elf_Addr value,
2807     unsigned char* view,
2808     elfcpp::Elf_types<64>::Elf_Addr address,
2809     section_size_type view_size)
2810 {
2811   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2812     {
2813       // leaq foo@tlsdesc(%rip), %rax
2814       // ==> movq foo@gottpoff(%rip), %rax
2815       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2816       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2817       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2818                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2819       view[-2] = 0x8b;
2820       const elfcpp::Elf_Xword addend = rela.get_r_addend();
2821       Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2822     }
2823   else
2824     {
2825       // call *foo@tlscall(%rax)
2826       // ==> nop; nop
2827       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2828       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2829       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2830                      view[0] == 0xff && view[1] == 0x10);
2831       view[0] = 0x66;
2832       view[1] = 0x90;
2833     }
2834 }
2835
2836 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2837
2838 inline void
2839 Target_x86_64::Relocate::tls_desc_gd_to_le(
2840     const Relocate_info<64, false>* relinfo,
2841     size_t relnum,
2842     Output_segment* tls_segment,
2843     const elfcpp::Rela<64, false>& rela,
2844     unsigned int r_type,
2845     elfcpp::Elf_types<64>::Elf_Addr value,
2846     unsigned char* view,
2847     section_size_type view_size)
2848 {
2849   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2850     {
2851       // leaq foo@tlsdesc(%rip), %rax
2852       // ==> movq foo@tpoff, %rax
2853       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2854       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2855       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2856                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2857       view[-2] = 0xc7;
2858       view[-1] = 0xc0;
2859       value -= tls_segment->memsz();
2860       Relocate_functions<64, false>::rela32(view, value, 0);
2861     }
2862   else
2863     {
2864       // call *foo@tlscall(%rax)
2865       // ==> nop; nop
2866       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2867       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2868       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2869                      view[0] == 0xff && view[1] == 0x10);
2870       view[0] = 0x66;
2871       view[1] = 0x90;
2872     }
2873 }
2874
2875 inline void
2876 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2877                                       size_t relnum,
2878                                       Output_segment*,
2879                                       const elfcpp::Rela<64, false>& rela,
2880                                       unsigned int,
2881                                       elfcpp::Elf_types<64>::Elf_Addr,
2882                                       unsigned char* view,
2883                                       section_size_type view_size)
2884 {
2885   // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2886   // ... leq foo@dtpoff(%rax),%reg
2887   // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2888
2889   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2890   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2891
2892   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2893                  view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2894
2895   tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2896
2897   memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2898
2899   // The next reloc should be a PLT32 reloc against __tls_get_addr.
2900   // We can skip it.
2901   this->skip_call_tls_get_addr_ = true;
2902 }
2903
2904 // Do a relocation in which we convert a TLS Initial-Exec to a
2905 // Local-Exec.
2906
2907 inline void
2908 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2909                                       size_t relnum,
2910                                       Output_segment* tls_segment,
2911                                       const elfcpp::Rela<64, false>& rela,
2912                                       unsigned int,
2913                                       elfcpp::Elf_types<64>::Elf_Addr value,
2914                                       unsigned char* view,
2915                                       section_size_type view_size)
2916 {
2917   // We need to examine the opcodes to figure out which instruction we
2918   // are looking at.
2919
2920   // movq foo@gottpoff(%rip),%reg  ==>  movq $YY,%reg
2921   // addq foo@gottpoff(%rip),%reg  ==>  addq $YY,%reg
2922
2923   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2924   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2925
2926   unsigned char op1 = view[-3];
2927   unsigned char op2 = view[-2];
2928   unsigned char op3 = view[-1];
2929   unsigned char reg = op3 >> 3;
2930
2931   if (op2 == 0x8b)
2932     {
2933       // movq
2934       if (op1 == 0x4c)
2935         view[-3] = 0x49;
2936       view[-2] = 0xc7;
2937       view[-1] = 0xc0 | reg;
2938     }
2939   else if (reg == 4)
2940     {
2941       // Special handling for %rsp.
2942       if (op1 == 0x4c)
2943         view[-3] = 0x49;
2944       view[-2] = 0x81;
2945       view[-1] = 0xc0 | reg;
2946     }
2947   else
2948     {
2949       // addq
2950       if (op1 == 0x4c)
2951         view[-3] = 0x4d;
2952       view[-2] = 0x8d;
2953       view[-1] = 0x80 | reg | (reg << 3);
2954     }
2955
2956   value -= tls_segment->memsz();
2957   Relocate_functions<64, false>::rela32(view, value, 0);
2958 }
2959
2960 // Relocate section data.
2961
2962 void
2963 Target_x86_64::relocate_section(
2964     const Relocate_info<64, false>* relinfo,
2965     unsigned int sh_type,
2966     const unsigned char* prelocs,
2967     size_t reloc_count,
2968     Output_section* output_section,
2969     bool needs_special_offset_handling,
2970     unsigned char* view,
2971     elfcpp::Elf_types<64>::Elf_Addr address,
2972     section_size_type view_size,
2973     const Reloc_symbol_changes* reloc_symbol_changes)
2974 {
2975   gold_assert(sh_type == elfcpp::SHT_RELA);
2976
2977   gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2978                          Target_x86_64::Relocate>(
2979     relinfo,
2980     this,
2981     prelocs,
2982     reloc_count,
2983     output_section,
2984     needs_special_offset_handling,
2985     view,
2986     address,
2987     view_size,
2988     reloc_symbol_changes);
2989 }
2990
2991 // Return the size of a relocation while scanning during a relocatable
2992 // link.
2993
2994 unsigned int
2995 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2996     unsigned int r_type,
2997     Relobj* object)
2998 {
2999   switch (r_type)
3000     {
3001     case elfcpp::R_X86_64_NONE:
3002     case elfcpp::R_X86_64_GNU_VTINHERIT:
3003     case elfcpp::R_X86_64_GNU_VTENTRY:
3004     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
3005     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
3006     case elfcpp::R_X86_64_TLSDESC_CALL:
3007     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
3008     case elfcpp::R_X86_64_DTPOFF32:
3009     case elfcpp::R_X86_64_DTPOFF64:
3010     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
3011     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
3012       return 0;
3013
3014     case elfcpp::R_X86_64_64:
3015     case elfcpp::R_X86_64_PC64:
3016     case elfcpp::R_X86_64_GOTOFF64:
3017     case elfcpp::R_X86_64_GOTPC64:
3018     case elfcpp::R_X86_64_PLTOFF64:
3019     case elfcpp::R_X86_64_GOT64:
3020     case elfcpp::R_X86_64_GOTPCREL64:
3021     case elfcpp::R_X86_64_GOTPCREL:
3022     case elfcpp::R_X86_64_GOTPLT64:
3023       return 8;
3024
3025     case elfcpp::R_X86_64_32:
3026     case elfcpp::R_X86_64_32S:
3027     case elfcpp::R_X86_64_PC32:
3028     case elfcpp::R_X86_64_PLT32:
3029     case elfcpp::R_X86_64_GOTPC32:
3030     case elfcpp::R_X86_64_GOT32:
3031       return 4;
3032
3033     case elfcpp::R_X86_64_16:
3034     case elfcpp::R_X86_64_PC16:
3035       return 2;
3036
3037     case elfcpp::R_X86_64_8:
3038     case elfcpp::R_X86_64_PC8:
3039       return 1;
3040
3041     case elfcpp::R_X86_64_COPY:
3042     case elfcpp::R_X86_64_GLOB_DAT:
3043     case elfcpp::R_X86_64_JUMP_SLOT:
3044     case elfcpp::R_X86_64_RELATIVE:
3045     case elfcpp::R_X86_64_IRELATIVE:
3046       // These are outstanding tls relocs, which are unexpected when linking
3047     case elfcpp::R_X86_64_TPOFF64:
3048     case elfcpp::R_X86_64_DTPMOD64:
3049     case elfcpp::R_X86_64_TLSDESC:
3050       object->error(_("unexpected reloc %u in object file"), r_type);
3051       return 0;
3052
3053     case elfcpp::R_X86_64_SIZE32:
3054     case elfcpp::R_X86_64_SIZE64:
3055     default:
3056       object->error(_("unsupported reloc %u against local symbol"), r_type);
3057       return 0;
3058     }
3059 }
3060
3061 // Scan the relocs during a relocatable link.
3062
3063 void
3064 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
3065                                        Layout* layout,
3066                                        Sized_relobj<64, false>* object,
3067                                        unsigned int data_shndx,
3068                                        unsigned int sh_type,
3069                                        const unsigned char* prelocs,
3070                                        size_t reloc_count,
3071                                        Output_section* output_section,
3072                                        bool needs_special_offset_handling,
3073                                        size_t local_symbol_count,
3074                                        const unsigned char* plocal_symbols,
3075                                        Relocatable_relocs* rr)
3076 {
3077   gold_assert(sh_type == elfcpp::SHT_RELA);
3078
3079   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
3080     Relocatable_size_for_reloc> Scan_relocatable_relocs;
3081
3082   gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
3083       Scan_relocatable_relocs>(
3084     symtab,
3085     layout,
3086     object,
3087     data_shndx,
3088     prelocs,
3089     reloc_count,
3090     output_section,
3091     needs_special_offset_handling,
3092     local_symbol_count,
3093     plocal_symbols,
3094     rr);
3095 }
3096
3097 // Relocate a section during a relocatable link.
3098
3099 void
3100 Target_x86_64::relocate_for_relocatable(
3101     const Relocate_info<64, false>* relinfo,
3102     unsigned int sh_type,
3103     const unsigned char* prelocs,
3104     size_t reloc_count,
3105     Output_section* output_section,
3106     off_t offset_in_output_section,
3107     const Relocatable_relocs* rr,
3108     unsigned char* view,
3109     elfcpp::Elf_types<64>::Elf_Addr view_address,
3110     section_size_type view_size,
3111     unsigned char* reloc_view,
3112     section_size_type reloc_view_size)
3113 {
3114   gold_assert(sh_type == elfcpp::SHT_RELA);
3115
3116   gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
3117     relinfo,
3118     prelocs,
3119     reloc_count,
3120     output_section,
3121     offset_in_output_section,
3122     rr,
3123     view,
3124     view_address,
3125     view_size,
3126     reloc_view,
3127     reloc_view_size);
3128 }
3129
3130 // Return the value to use for a dynamic which requires special
3131 // treatment.  This is how we support equality comparisons of function
3132 // pointers across shared library boundaries, as described in the
3133 // processor specific ABI supplement.
3134
3135 uint64_t
3136 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
3137 {
3138   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3139   return this->plt_section()->address() + gsym->plt_offset();
3140 }
3141
3142 // Return a string used to fill a code section with nops to take up
3143 // the specified length.
3144
3145 std::string
3146 Target_x86_64::do_code_fill(section_size_type length) const
3147 {
3148   if (length >= 16)
3149     {
3150       // Build a jmpq instruction to skip over the bytes.
3151       unsigned char jmp[5];
3152       jmp[0] = 0xe9;
3153       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3154       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3155               + std::string(length - 5, '\0'));
3156     }
3157
3158   // Nop sequences of various lengths.
3159   const char nop1[1] = { 0x90 };                   // nop
3160   const char nop2[2] = { 0x66, 0x90 };             // xchg %ax %ax
3161   const char nop3[3] = { 0x0f, 0x1f, 0x00 };       // nop (%rax)
3162   const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00};  // nop 0(%rax)
3163   const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00,   // nop 0(%rax,%rax,1)
3164                          0x00 };
3165   const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44,   // nopw 0(%rax,%rax,1)
3166                          0x00, 0x00 };
3167   const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00,   // nopl 0L(%rax)
3168                          0x00, 0x00, 0x00 };
3169   const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00,   // nopl 0L(%rax,%rax,1)
3170                          0x00, 0x00, 0x00, 0x00 };
3171   const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84,   // nopw 0L(%rax,%rax,1)
3172                          0x00, 0x00, 0x00, 0x00,
3173                          0x00 };
3174   const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
3175                            0x84, 0x00, 0x00, 0x00,
3176                            0x00, 0x00 };
3177   const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
3178                            0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3179                            0x00, 0x00, 0x00 };
3180   const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
3181                            0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
3182                            0x00, 0x00, 0x00, 0x00 };
3183   const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3184                            0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
3185                            0x00, 0x00, 0x00, 0x00,
3186                            0x00 };
3187   const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3188                            0x66, 0x2e, 0x0f, 0x1f, // data16
3189                            0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3190                            0x00, 0x00 };
3191   const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
3192                            0x66, 0x66, 0x2e, 0x0f, // data16; data16
3193                            0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
3194                            0x00, 0x00, 0x00 };
3195
3196   const char* nops[16] = {
3197     NULL,
3198     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3199     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3200   };
3201
3202   return std::string(nops[length], length);
3203 }
3204
3205 // Return the addend to use for a target specific relocation.  The
3206 // only target specific relocation is R_X86_64_TLSDESC for a local
3207 // symbol.  We want to set the addend is the offset of the local
3208 // symbol in the TLS segment.
3209
3210 uint64_t
3211 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
3212                                uint64_t) const
3213 {
3214   gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
3215   uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
3216   gold_assert(intarg < this->tlsdesc_reloc_info_.size());
3217   const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
3218   const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
3219   gold_assert(psymval->is_tls_symbol());
3220   // The value of a TLS symbol is the offset in the TLS segment.
3221   return psymval->value(ti.object, 0);
3222 }
3223
3224 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3225 // compiled with -fstack-split.  The function calls non-stack-split
3226 // code.  We have to change the function so that it always ensures
3227 // that it has enough stack space to run some random function.
3228
3229 void
3230 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
3231                                   section_offset_type fnoffset,
3232                                   section_size_type fnsize,
3233                                   unsigned char* view,
3234                                   section_size_type view_size,
3235                                   std::string* from,
3236                                   std::string* to) const
3237 {
3238   // The function starts with a comparison of the stack pointer and a
3239   // field in the TCB.  This is followed by a jump.
3240
3241   // cmp %fs:NN,%rsp
3242   if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
3243       && fnsize > 9)
3244     {
3245       // We will call __morestack if the carry flag is set after this
3246       // comparison.  We turn the comparison into an stc instruction
3247       // and some nops.
3248       view[fnoffset] = '\xf9';
3249       this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
3250     }
3251   // lea NN(%rsp),%r10
3252   // lea NN(%rsp),%r11
3253   else if ((this->match_view(view, view_size, fnoffset,
3254                              "\x4c\x8d\x94\x24", 4)
3255             || this->match_view(view, view_size, fnoffset,
3256                                 "\x4c\x8d\x9c\x24", 4))
3257            && fnsize > 8)
3258     {
3259       // This is loading an offset from the stack pointer for a
3260       // comparison.  The offset is negative, so we decrease the
3261       // offset by the amount of space we need for the stack.  This
3262       // means we will avoid calling __morestack if there happens to
3263       // be plenty of space on the stack already.
3264       unsigned char* pval = view + fnoffset + 4;
3265       uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3266       val -= parameters->options().split_stack_adjust_size();
3267       elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3268     }
3269   else
3270     {
3271       if (!object->has_no_split_stack())
3272         object->error(_("failed to match split-stack sequence at "
3273                         "section %u offset %0zx"),
3274                       shndx, static_cast<size_t>(fnoffset));
3275       return;
3276     }
3277
3278   // We have to change the function so that it calls
3279   // __morestack_non_split instead of __morestack.  The former will
3280   // allocate additional stack space.
3281   *from = "__morestack";
3282   *to = "__morestack_non_split";
3283 }
3284
3285 // The selector for x86_64 object files.
3286
3287 class Target_selector_x86_64 : public Target_selector_freebsd
3288 {
3289 public:
3290   Target_selector_x86_64()
3291     : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
3292                               "elf64-x86-64-freebsd")
3293   { }
3294
3295   Target*
3296   do_instantiate_target()
3297   { return new Target_x86_64(); }
3298
3299 };
3300
3301 Target_selector_x86_64 target_selector_x86_64;
3302
3303 } // End anonymous namespace.