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