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