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