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