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