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