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