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