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