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