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