Disallow --incremental with -pie and force -no-pie for incremental tests.
[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 (!parameters->incremental()
3070             && (r_type == elfcpp::R_X86_64_GOTPCREL
3071                 || r_type == elfcpp::R_X86_64_GOTPCRELX
3072                 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX)
3073             && reloc.get_r_offset() >= 2
3074             && !is_ifunc)
3075           {
3076             section_size_type stype;
3077             const unsigned char* view = object->section_contents(data_shndx,
3078                                                                  &stype, true);
3079             if (view[reloc.get_r_offset() - 2] == 0x8b)
3080               break;
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         // We cannot make these optimizations in incremental linking mode,
3550         // because we look at the opcode to decide whether or not to make
3551         // change, and during an incremental update, the change may have
3552         // already been applied.
3553
3554         Lazy_view<size> view(object, data_shndx);
3555         size_t r_offset = reloc.get_r_offset();
3556         if (!parameters->incremental()
3557             && r_offset >= 2
3558             && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
3559                                                            r_offset, &view))
3560           break;
3561
3562         if (!parameters->incremental()
3563             && r_offset >= 2
3564             && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type,
3565                                                                 r_offset,
3566                                                                 &view))
3567           break;
3568
3569         if (gsym->final_value_is_known())
3570           {
3571             // For a STT_GNU_IFUNC symbol we want the PLT address.
3572             if (gsym->type() == elfcpp::STT_GNU_IFUNC)
3573               got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3574             else
3575               got->add_global(gsym, GOT_TYPE_STANDARD);
3576           }
3577         else
3578           {
3579             // If this symbol is not fully resolved, we need to add a
3580             // dynamic relocation for it.
3581             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
3582
3583             // Use a GLOB_DAT rather than a RELATIVE reloc if:
3584             //
3585             // 1) The symbol may be defined in some other module.
3586             //
3587             // 2) We are building a shared library and this is a
3588             // protected symbol; using GLOB_DAT means that the dynamic
3589             // linker can use the address of the PLT in the main
3590             // executable when appropriate so that function address
3591             // comparisons work.
3592             //
3593             // 3) This is a STT_GNU_IFUNC symbol in position dependent
3594             // code, again so that function address comparisons work.
3595             if (gsym->is_from_dynobj()
3596                 || gsym->is_undefined()
3597                 || gsym->is_preemptible()
3598                 || (gsym->visibility() == elfcpp::STV_PROTECTED
3599                     && parameters->options().shared())
3600                 || (gsym->type() == elfcpp::STT_GNU_IFUNC
3601                     && parameters->options().output_is_position_independent()))
3602               got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn,
3603                                        elfcpp::R_X86_64_GLOB_DAT);
3604             else
3605               {
3606                 // For a STT_GNU_IFUNC symbol we want to write the PLT
3607                 // offset into the GOT, so that function pointer
3608                 // comparisons work correctly.
3609                 bool is_new;
3610                 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
3611                   is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
3612                 else
3613                   {
3614                     is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
3615                     // Tell the dynamic linker to use the PLT address
3616                     // when resolving relocations.
3617                     if (gsym->is_from_dynobj()
3618                         && !parameters->options().shared())
3619                       gsym->set_needs_dynsym_value();
3620                   }
3621                 if (is_new)
3622                   {
3623                     unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
3624                     rela_dyn->add_global_relative(gsym,
3625                                                   elfcpp::R_X86_64_RELATIVE,
3626                                                   got, got_off, 0, false);
3627                   }
3628               }
3629           }
3630       }
3631       break;
3632
3633     case elfcpp::R_X86_64_PLT32:
3634     case elfcpp::R_X86_64_PLT32_BND:
3635       // If the symbol is fully resolved, this is just a PC32 reloc.
3636       // Otherwise we need a PLT entry.
3637       if (gsym->final_value_is_known())
3638         break;
3639       // If building a shared library, we can also skip the PLT entry
3640       // if the symbol is defined in the output file and is protected
3641       // or hidden.
3642       if (gsym->is_defined()
3643           && !gsym->is_from_dynobj()
3644           && !gsym->is_preemptible())
3645         break;
3646       target->make_plt_entry(symtab, layout, gsym);
3647       break;
3648
3649     case elfcpp::R_X86_64_GOTPC32:
3650     case elfcpp::R_X86_64_GOTOFF64:
3651     case elfcpp::R_X86_64_GOTPC64:
3652     case elfcpp::R_X86_64_PLTOFF64:
3653       // We need a GOT section.
3654       target->got_section(symtab, layout);
3655       // For PLTOFF64, we also need a PLT entry (but only if the
3656       // symbol is not fully resolved).
3657       if (r_type == elfcpp::R_X86_64_PLTOFF64
3658           && !gsym->final_value_is_known())
3659         target->make_plt_entry(symtab, layout, gsym);
3660       break;
3661
3662     case elfcpp::R_X86_64_COPY:
3663     case elfcpp::R_X86_64_GLOB_DAT:
3664     case elfcpp::R_X86_64_JUMP_SLOT:
3665     case elfcpp::R_X86_64_RELATIVE:
3666     case elfcpp::R_X86_64_IRELATIVE:
3667       // These are outstanding tls relocs, which are unexpected when linking
3668     case elfcpp::R_X86_64_TPOFF64:
3669     case elfcpp::R_X86_64_DTPMOD64:
3670     case elfcpp::R_X86_64_TLSDESC:
3671       gold_error(_("%s: unexpected reloc %u in object file"),
3672                  object->name().c_str(), r_type);
3673       break;
3674
3675       // These are initial tls relocs, which are expected for global()
3676     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
3677     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
3678     case elfcpp::R_X86_64_TLSDESC_CALL:
3679     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
3680     case elfcpp::R_X86_64_DTPOFF32:
3681     case elfcpp::R_X86_64_DTPOFF64:
3682     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
3683     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
3684       {
3685         // For the Initial-Exec model, we can treat undef symbols as final
3686         // when building an executable.
3687         const bool is_final = (gsym->final_value_is_known() ||
3688                                (r_type == elfcpp::R_X86_64_GOTTPOFF &&
3689                                 gsym->is_undefined() &&
3690                                 parameters->options().output_is_executable()));
3691         const tls::Tls_optimization optimized_type
3692             = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
3693         switch (r_type)
3694           {
3695           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
3696             if (optimized_type == tls::TLSOPT_NONE)
3697               {
3698                 // Create a pair of GOT entries for the module index and
3699                 // dtv-relative offset.
3700                 Output_data_got<64, false>* got
3701                     = target->got_section(symtab, layout);
3702                 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
3703                                               target->rela_dyn_section(layout),
3704                                               elfcpp::R_X86_64_DTPMOD64,
3705                                               elfcpp::R_X86_64_DTPOFF64);
3706               }
3707             else if (optimized_type == tls::TLSOPT_TO_IE)
3708               {
3709                 // Create a GOT entry for the tp-relative offset.
3710                 Output_data_got<64, false>* got
3711                     = target->got_section(symtab, layout);
3712                 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3713                                          target->rela_dyn_section(layout),
3714                                          elfcpp::R_X86_64_TPOFF64);
3715               }
3716             else if (optimized_type != tls::TLSOPT_TO_LE)
3717               unsupported_reloc_global(object, r_type, gsym);
3718             break;
3719
3720           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
3721             target->define_tls_base_symbol(symtab, layout);
3722             if (optimized_type == tls::TLSOPT_NONE)
3723               {
3724                 // Create reserved PLT and GOT entries for the resolver.
3725                 target->reserve_tlsdesc_entries(symtab, layout);
3726
3727                 // Create a double GOT entry with an R_X86_64_TLSDESC
3728                 // reloc.  The R_X86_64_TLSDESC reloc is resolved
3729                 // lazily, so the GOT entry needs to be in an area in
3730                 // .got.plt, not .got.  Call got_section to make sure
3731                 // the section has been created.
3732                 target->got_section(symtab, layout);
3733                 Output_data_got<64, false>* got = target->got_tlsdesc_section();
3734                 Reloc_section* rt = target->rela_tlsdesc_section(layout);
3735                 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
3736                                               elfcpp::R_X86_64_TLSDESC, 0);
3737               }
3738             else if (optimized_type == tls::TLSOPT_TO_IE)
3739               {
3740                 // Create a GOT entry for the tp-relative offset.
3741                 Output_data_got<64, false>* got
3742                     = target->got_section(symtab, layout);
3743                 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3744                                          target->rela_dyn_section(layout),
3745                                          elfcpp::R_X86_64_TPOFF64);
3746               }
3747             else if (optimized_type != tls::TLSOPT_TO_LE)
3748               unsupported_reloc_global(object, r_type, gsym);
3749             break;
3750
3751           case elfcpp::R_X86_64_TLSDESC_CALL:
3752             break;
3753
3754           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
3755             if (optimized_type == tls::TLSOPT_NONE)
3756               {
3757                 // Create a GOT entry for the module index.
3758                 target->got_mod_index_entry(symtab, layout, object);
3759               }
3760             else if (optimized_type != tls::TLSOPT_TO_LE)
3761               unsupported_reloc_global(object, r_type, gsym);
3762             break;
3763
3764           case elfcpp::R_X86_64_DTPOFF32:
3765           case elfcpp::R_X86_64_DTPOFF64:
3766             break;
3767
3768           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
3769             layout->set_has_static_tls();
3770             if (optimized_type == tls::TLSOPT_NONE)
3771               {
3772                 // Create a GOT entry for the tp-relative offset.
3773                 Output_data_got<64, false>* got
3774                     = target->got_section(symtab, layout);
3775                 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET,
3776                                          target->rela_dyn_section(layout),
3777                                          elfcpp::R_X86_64_TPOFF64);
3778               }
3779             else if (optimized_type != tls::TLSOPT_TO_LE)
3780               unsupported_reloc_global(object, r_type, gsym);
3781             break;
3782
3783           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
3784             layout->set_has_static_tls();
3785             if (parameters->options().shared())
3786               unsupported_reloc_global(object, r_type, gsym);
3787             break;
3788
3789           default:
3790             gold_unreachable();
3791           }
3792       }
3793       break;
3794
3795     case elfcpp::R_X86_64_SIZE32:
3796     case elfcpp::R_X86_64_SIZE64:
3797     default:
3798       gold_error(_("%s: unsupported reloc %u against global symbol %s"),
3799                  object->name().c_str(), r_type,
3800                  gsym->demangled_name().c_str());
3801       break;
3802     }
3803 }
3804
3805 template<int size>
3806 void
3807 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab,
3808                                        Layout* layout,
3809                                        Sized_relobj_file<size, false>* object,
3810                                        unsigned int data_shndx,
3811                                        unsigned int sh_type,
3812                                        const unsigned char* prelocs,
3813                                        size_t reloc_count,
3814                                        Output_section* output_section,
3815                                        bool needs_special_offset_handling,
3816                                        size_t local_symbol_count,
3817                                        const unsigned char* plocal_symbols)
3818 {
3819   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3820       Classify_reloc;
3821
3822   if (sh_type == elfcpp::SHT_REL)
3823     {
3824       return;
3825     }
3826
3827    gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan,
3828                            Classify_reloc>(
3829     symtab,
3830     layout,
3831     this,
3832     object,
3833     data_shndx,
3834     prelocs,
3835     reloc_count,
3836     output_section,
3837     needs_special_offset_handling,
3838     local_symbol_count,
3839     plocal_symbols);
3840
3841 }
3842 // Scan relocations for a section.
3843
3844 template<int size>
3845 void
3846 Target_x86_64<size>::scan_relocs(Symbol_table* symtab,
3847                                  Layout* layout,
3848                                  Sized_relobj_file<size, false>* object,
3849                                  unsigned int data_shndx,
3850                                  unsigned int sh_type,
3851                                  const unsigned char* prelocs,
3852                                  size_t reloc_count,
3853                                  Output_section* output_section,
3854                                  bool needs_special_offset_handling,
3855                                  size_t local_symbol_count,
3856                                  const unsigned char* plocal_symbols)
3857 {
3858   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
3859       Classify_reloc;
3860
3861   if (sh_type == elfcpp::SHT_REL)
3862     {
3863       gold_error(_("%s: unsupported REL reloc section"),
3864                  object->name().c_str());
3865       return;
3866     }
3867
3868   gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>(
3869     symtab,
3870     layout,
3871     this,
3872     object,
3873     data_shndx,
3874     prelocs,
3875     reloc_count,
3876     output_section,
3877     needs_special_offset_handling,
3878     local_symbol_count,
3879     plocal_symbols);
3880 }
3881
3882 // Finalize the sections.
3883
3884 template<int size>
3885 void
3886 Target_x86_64<size>::do_finalize_sections(
3887     Layout* layout,
3888     const Input_objects*,
3889     Symbol_table* symtab)
3890 {
3891   const Reloc_section* rel_plt = (this->plt_ == NULL
3892                                   ? NULL
3893                                   : this->plt_->rela_plt());
3894   layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
3895                                   this->rela_dyn_, true, false);
3896
3897   // Fill in some more dynamic tags.
3898   Output_data_dynamic* const odyn = layout->dynamic_data();
3899   if (odyn != NULL)
3900     {
3901       if (this->plt_ != NULL
3902           && this->plt_->output_section() != NULL
3903           && this->plt_->has_tlsdesc_entry())
3904         {
3905           unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
3906           unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
3907           this->got_->finalize_data_size();
3908           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
3909                                         this->plt_, plt_offset);
3910           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
3911                                         this->got_, got_offset);
3912         }
3913     }
3914
3915   // Emit any relocs we saved in an attempt to avoid generating COPY
3916   // relocs.
3917   if (this->copy_relocs_.any_saved_relocs())
3918     this->copy_relocs_.emit(this->rela_dyn_section(layout));
3919
3920   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
3921   // the .got.plt section.
3922   Symbol* sym = this->global_offset_table_;
3923   if (sym != NULL)
3924     {
3925       uint64_t data_size = this->got_plt_->current_data_size();
3926       symtab->get_sized_symbol<size>(sym)->set_symsize(data_size);
3927     }
3928
3929   if (parameters->doing_static_link()
3930       && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
3931     {
3932       // If linking statically, make sure that the __rela_iplt symbols
3933       // were defined if necessary, even if we didn't create a PLT.
3934       static const Define_symbol_in_segment syms[] =
3935         {
3936           {
3937             "__rela_iplt_start",        // name
3938             elfcpp::PT_LOAD,            // segment_type
3939             elfcpp::PF_W,               // segment_flags_set
3940             elfcpp::PF(0),              // segment_flags_clear
3941             0,                          // value
3942             0,                          // size
3943             elfcpp::STT_NOTYPE,         // type
3944             elfcpp::STB_GLOBAL,         // binding
3945             elfcpp::STV_HIDDEN,         // visibility
3946             0,                          // nonvis
3947             Symbol::SEGMENT_START,      // offset_from_base
3948             true                        // only_if_ref
3949           },
3950           {
3951             "__rela_iplt_end",          // name
3952             elfcpp::PT_LOAD,            // segment_type
3953             elfcpp::PF_W,               // segment_flags_set
3954             elfcpp::PF(0),              // segment_flags_clear
3955             0,                          // value
3956             0,                          // size
3957             elfcpp::STT_NOTYPE,         // type
3958             elfcpp::STB_GLOBAL,         // binding
3959             elfcpp::STV_HIDDEN,         // visibility
3960             0,                          // nonvis
3961             Symbol::SEGMENT_START,      // offset_from_base
3962             true                        // only_if_ref
3963           }
3964         };
3965
3966       symtab->define_symbols(layout, 2, syms,
3967                              layout->script_options()->saw_sections_clause());
3968     }
3969 }
3970
3971 // For x32, we need to handle PC-relative relocations using full 64-bit
3972 // arithmetic, so that we can detect relocation overflows properly.
3973 // This class overrides the pcrela32_check methods from the defaults in
3974 // Relocate_functions in reloc.h.
3975
3976 template<int size>
3977 class X86_64_relocate_functions : public Relocate_functions<size, false>
3978 {
3979  public:
3980   typedef Relocate_functions<size, false> Base;
3981
3982   // Do a simple PC relative relocation with the addend in the
3983   // relocation.
3984   static inline typename Base::Reloc_status
3985   pcrela32_check(unsigned char* view,
3986                  typename elfcpp::Elf_types<64>::Elf_Addr value,
3987                  typename elfcpp::Elf_types<64>::Elf_Swxword addend,
3988                  typename elfcpp::Elf_types<64>::Elf_Addr address)
3989   {
3990     typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
3991     Valtype* wv = reinterpret_cast<Valtype*>(view);
3992     value = value + addend - address;
3993     elfcpp::Swap<32, false>::writeval(wv, value);
3994     return (Bits<32>::has_overflow(value)
3995             ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
3996   }
3997
3998   // Do a simple PC relative relocation with a Symbol_value with the
3999   // addend in the relocation.
4000   static inline typename Base::Reloc_status
4001   pcrela32_check(unsigned char* view,
4002                  const Sized_relobj_file<size, false>* object,
4003                  const Symbol_value<size>* psymval,
4004                  typename elfcpp::Elf_types<64>::Elf_Swxword addend,
4005                  typename elfcpp::Elf_types<64>::Elf_Addr address)
4006   {
4007     typedef typename elfcpp::Swap<32, false>::Valtype Valtype;
4008     Valtype* wv = reinterpret_cast<Valtype*>(view);
4009     typename elfcpp::Elf_types<64>::Elf_Addr value;
4010     if (addend >= 0)
4011       value = psymval->value(object, addend);
4012     else
4013       {
4014         // For negative addends, get the symbol value without
4015         // the addend, then add the addend using 64-bit arithmetic.
4016         value = psymval->value(object, 0);
4017         value += addend;
4018       }
4019     value -= address;
4020     elfcpp::Swap<32, false>::writeval(wv, value);
4021     return (Bits<32>::has_overflow(value)
4022             ? Base::RELOC_OVERFLOW : Base::RELOC_OK);
4023   }
4024 };
4025
4026 // Perform a relocation.
4027
4028 template<int size>
4029 inline bool
4030 Target_x86_64<size>::Relocate::relocate(
4031     const Relocate_info<size, false>* relinfo,
4032     unsigned int,
4033     Target_x86_64<size>* target,
4034     Output_section*,
4035     size_t relnum,
4036     const unsigned char* preloc,
4037     const Sized_symbol<size>* gsym,
4038     const Symbol_value<size>* psymval,
4039     unsigned char* view,
4040     typename elfcpp::Elf_types<size>::Elf_Addr address,
4041     section_size_type view_size)
4042 {
4043   typedef X86_64_relocate_functions<size> Reloc_funcs;
4044   const elfcpp::Rela<size, false> rela(preloc);
4045   unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info());
4046
4047   if (this->skip_call_tls_get_addr_)
4048     {
4049       if ((r_type != elfcpp::R_X86_64_PLT32
4050            && r_type != elfcpp::R_X86_64_GOTPCREL
4051            && r_type != elfcpp::R_X86_64_GOTPCRELX
4052            && r_type != elfcpp::R_X86_64_PLT32_BND
4053            && r_type != elfcpp::R_X86_64_PC32_BND
4054            && r_type != elfcpp::R_X86_64_PC32)
4055           || gsym == NULL
4056           || strcmp(gsym->name(), "__tls_get_addr") != 0)
4057         {
4058           gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4059                                  _("missing expected TLS relocation"));
4060           this->skip_call_tls_get_addr_ = false;
4061         }
4062       else
4063         {
4064           this->skip_call_tls_get_addr_ = false;
4065           return false;
4066         }
4067     }
4068
4069   if (view == NULL)
4070     return true;
4071
4072   const Sized_relobj_file<size, false>* object = relinfo->object;
4073
4074   // Pick the value to use for symbols defined in the PLT.
4075   Symbol_value<size> symval;
4076   if (gsym != NULL
4077       && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
4078     {
4079       symval.set_output_value(target->plt_address_for_global(gsym));
4080       psymval = &symval;
4081     }
4082   else if (gsym == NULL && psymval->is_ifunc_symbol())
4083     {
4084       unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4085       if (object->local_has_plt_offset(r_sym))
4086         {
4087           symval.set_output_value(target->plt_address_for_local(object, r_sym));
4088           psymval = &symval;
4089         }
4090     }
4091
4092   const elfcpp::Elf_Xword addend = rela.get_r_addend();
4093
4094   // Get the GOT offset if needed.
4095   // The GOT pointer points to the end of the GOT section.
4096   // We need to subtract the size of the GOT section to get
4097   // the actual offset to use in the relocation.
4098   bool have_got_offset = false;
4099   // Since the actual offset is always negative, we use signed int to
4100   // support 64-bit GOT relocations.
4101   int got_offset = 0;
4102   switch (r_type)
4103     {
4104     case elfcpp::R_X86_64_GOT32:
4105     case elfcpp::R_X86_64_GOT64:
4106     case elfcpp::R_X86_64_GOTPLT64:
4107     case elfcpp::R_X86_64_GOTPCREL64:
4108       if (gsym != NULL)
4109         {
4110           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4111           got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
4112         }
4113       else
4114         {
4115           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4116           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
4117           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4118                         - target->got_size());
4119         }
4120       have_got_offset = true;
4121       break;
4122
4123     default:
4124       break;
4125     }
4126
4127   typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK;
4128
4129   switch (r_type)
4130     {
4131     case elfcpp::R_X86_64_NONE:
4132     case elfcpp::R_X86_64_GNU_VTINHERIT:
4133     case elfcpp::R_X86_64_GNU_VTENTRY:
4134       break;
4135
4136     case elfcpp::R_X86_64_64:
4137       Reloc_funcs::rela64(view, object, psymval, addend);
4138       break;
4139
4140     case elfcpp::R_X86_64_PC64:
4141       Reloc_funcs::pcrela64(view, object, psymval, addend,
4142                                               address);
4143       break;
4144
4145     case elfcpp::R_X86_64_32:
4146       rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4147                                           Reloc_funcs::CHECK_UNSIGNED);
4148       break;
4149
4150     case elfcpp::R_X86_64_32S:
4151       rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend,
4152                                           Reloc_funcs::CHECK_SIGNED);
4153       break;
4154
4155     case elfcpp::R_X86_64_PC32:
4156     case elfcpp::R_X86_64_PC32_BND:
4157       rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4158                                             address);
4159       break;
4160
4161     case elfcpp::R_X86_64_16:
4162       Reloc_funcs::rela16(view, object, psymval, addend);
4163       break;
4164
4165     case elfcpp::R_X86_64_PC16:
4166       Reloc_funcs::pcrela16(view, object, psymval, addend, address);
4167       break;
4168
4169     case elfcpp::R_X86_64_8:
4170       Reloc_funcs::rela8(view, object, psymval, addend);
4171       break;
4172
4173     case elfcpp::R_X86_64_PC8:
4174       Reloc_funcs::pcrela8(view, object, psymval, addend, address);
4175       break;
4176
4177     case elfcpp::R_X86_64_PLT32:
4178     case elfcpp::R_X86_64_PLT32_BND:
4179       gold_assert(gsym == NULL
4180                   || gsym->has_plt_offset()
4181                   || gsym->final_value_is_known()
4182                   || (gsym->is_defined()
4183                       && !gsym->is_from_dynobj()
4184                       && !gsym->is_preemptible()));
4185       // Note: while this code looks the same as for R_X86_64_PC32, it
4186       // behaves differently because psymval was set to point to
4187       // the PLT entry, rather than the symbol, in Scan::global().
4188       rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend,
4189                                             address);
4190       break;
4191
4192     case elfcpp::R_X86_64_PLTOFF64:
4193       {
4194         gold_assert(gsym);
4195         gold_assert(gsym->has_plt_offset()
4196                     || gsym->final_value_is_known());
4197         typename elfcpp::Elf_types<size>::Elf_Addr got_address;
4198         // This is the address of GLOBAL_OFFSET_TABLE.
4199         got_address = target->got_plt_section()->address();
4200         Reloc_funcs::rela64(view, object, psymval, addend - got_address);
4201       }
4202       break;
4203
4204     case elfcpp::R_X86_64_GOT32:
4205       gold_assert(have_got_offset);
4206       Reloc_funcs::rela32(view, got_offset, addend);
4207       break;
4208
4209     case elfcpp::R_X86_64_GOTPC32:
4210       {
4211         gold_assert(gsym);
4212         typename elfcpp::Elf_types<size>::Elf_Addr value;
4213         value = target->got_plt_section()->address();
4214         Reloc_funcs::pcrela32_check(view, value, addend, address);
4215       }
4216       break;
4217
4218     case elfcpp::R_X86_64_GOT64:
4219     case elfcpp::R_X86_64_GOTPLT64:
4220       // R_X86_64_GOTPLT64 is obsolete and treated the same as
4221       // GOT64.
4222       gold_assert(have_got_offset);
4223       Reloc_funcs::rela64(view, got_offset, addend);
4224       break;
4225
4226     case elfcpp::R_X86_64_GOTPC64:
4227       {
4228         gold_assert(gsym);
4229         typename elfcpp::Elf_types<size>::Elf_Addr value;
4230         value = target->got_plt_section()->address();
4231         Reloc_funcs::pcrela64(view, value, addend, address);
4232       }
4233       break;
4234
4235     case elfcpp::R_X86_64_GOTOFF64:
4236       {
4237         typename elfcpp::Elf_types<size>::Elf_Addr value;
4238         value = (psymval->value(object, 0)
4239                  - target->got_plt_section()->address());
4240         Reloc_funcs::rela64(view, value, addend);
4241       }
4242       break;
4243
4244     case elfcpp::R_X86_64_GOTPCREL:
4245     case elfcpp::R_X86_64_GOTPCRELX:
4246     case elfcpp::R_X86_64_REX_GOTPCRELX:
4247       {
4248       // Convert
4249       // mov foo@GOTPCREL(%rip), %reg
4250       // to lea foo(%rip), %reg.
4251       // if possible.
4252       if (!parameters->incremental()
4253           && ((gsym == NULL
4254                && rela.get_r_offset() >= 2
4255                && view[-2] == 0x8b
4256                && !psymval->is_ifunc_symbol())
4257               || (gsym != NULL
4258                   && rela.get_r_offset() >= 2
4259                   && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type,
4260                                                                  0, &view))))
4261         {
4262           view[-2] = 0x8d;
4263           Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4264         }
4265       // Convert
4266       // callq *foo@GOTPCRELX(%rip) to
4267       // addr32 callq foo
4268       // and jmpq *foo@GOTPCRELX(%rip) to
4269       // jmpq foo
4270       // nop
4271       else if (!parameters->incremental()
4272                && gsym != NULL
4273                && rela.get_r_offset() >= 2
4274                && Target_x86_64<size>::can_convert_callq_to_direct(gsym,
4275                                                                    r_type,
4276                                                                    0, &view))
4277         {
4278           if (view[-1] == 0x15)
4279             {
4280               // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq.
4281               // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8.
4282               view[-2] = 0x67;
4283               view[-1] = 0xe8;
4284               // Convert GOTPCRELX to 32-bit pc relative reloc.
4285               Reloc_funcs::pcrela32(view, object, psymval, addend, address);
4286             }
4287           else
4288             {
4289               // Convert jmpq *foo@GOTPCRELX(%rip) to
4290               // jmpq foo
4291               // nop
4292               // The opcode of direct jmpq is 0xe9.
4293               view[-2] = 0xe9;
4294               // The opcode of nop is 0x90.
4295               view[3] = 0x90;
4296               // Convert GOTPCRELX to 32-bit pc relative reloc.  jmpq is rip
4297               // relative and since the instruction following the jmpq is now
4298               // the nop, offset the address by 1 byte.  The start of the
4299               // relocation also moves ahead by 1 byte.
4300               Reloc_funcs::pcrela32(&view[-1], object, psymval, addend,
4301                                     address - 1);
4302             }
4303         }
4304       else
4305         {
4306           if (gsym != NULL)
4307             {
4308               gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
4309               got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
4310                             - target->got_size());
4311             }
4312           else
4313             {
4314               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4315               gold_assert(object->local_has_got_offset(r_sym,
4316                                                        GOT_TYPE_STANDARD));
4317               got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
4318                             - target->got_size());
4319             }
4320           typename elfcpp::Elf_types<size>::Elf_Addr value;
4321           value = target->got_plt_section()->address() + got_offset;
4322           Reloc_funcs::pcrela32_check(view, value, addend, address);
4323         }
4324       }
4325       break;
4326
4327     case elfcpp::R_X86_64_GOTPCREL64:
4328       {
4329         gold_assert(have_got_offset);
4330         typename elfcpp::Elf_types<size>::Elf_Addr value;
4331         value = target->got_plt_section()->address() + got_offset;
4332         Reloc_funcs::pcrela64(view, value, addend, address);
4333       }
4334       break;
4335
4336     case elfcpp::R_X86_64_COPY:
4337     case elfcpp::R_X86_64_GLOB_DAT:
4338     case elfcpp::R_X86_64_JUMP_SLOT:
4339     case elfcpp::R_X86_64_RELATIVE:
4340     case elfcpp::R_X86_64_IRELATIVE:
4341       // These are outstanding tls relocs, which are unexpected when linking
4342     case elfcpp::R_X86_64_TPOFF64:
4343     case elfcpp::R_X86_64_DTPMOD64:
4344     case elfcpp::R_X86_64_TLSDESC:
4345       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4346                              _("unexpected reloc %u in object file"),
4347                              r_type);
4348       break;
4349
4350       // These are initial tls relocs, which are expected when linking
4351     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
4352     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
4353     case elfcpp::R_X86_64_TLSDESC_CALL:
4354     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
4355     case elfcpp::R_X86_64_DTPOFF32:
4356     case elfcpp::R_X86_64_DTPOFF64:
4357     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
4358     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
4359       this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
4360                          view, address, view_size);
4361       break;
4362
4363     case elfcpp::R_X86_64_SIZE32:
4364     case elfcpp::R_X86_64_SIZE64:
4365     default:
4366       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4367                              _("unsupported reloc %u"),
4368                              r_type);
4369       break;
4370     }
4371
4372   if (rstatus == Reloc_funcs::RELOC_OVERFLOW)
4373     {
4374       if (gsym == NULL)
4375         {
4376           unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4377           gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4378                                  _("relocation overflow: "
4379                                    "reference to local symbol %u in %s"),
4380                                  r_sym, object->name().c_str());
4381         }
4382       else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT)
4383         {
4384           gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4385                                  _("relocation overflow: "
4386                                    "reference to '%s' defined in %s"),
4387                                  gsym->name(),
4388                                  gsym->object()->name().c_str());
4389         }
4390       else
4391         {
4392           gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4393                                  _("relocation overflow: reference to '%s'"),
4394                                  gsym->name());
4395         }
4396     }
4397
4398   return true;
4399 }
4400
4401 // Perform a TLS relocation.
4402
4403 template<int size>
4404 inline void
4405 Target_x86_64<size>::Relocate::relocate_tls(
4406     const Relocate_info<size, false>* relinfo,
4407     Target_x86_64<size>* target,
4408     size_t relnum,
4409     const elfcpp::Rela<size, false>& rela,
4410     unsigned int r_type,
4411     const Sized_symbol<size>* gsym,
4412     const Symbol_value<size>* psymval,
4413     unsigned char* view,
4414     typename elfcpp::Elf_types<size>::Elf_Addr address,
4415     section_size_type view_size)
4416 {
4417   Output_segment* tls_segment = relinfo->layout->tls_segment();
4418
4419   const Sized_relobj_file<size, false>* object = relinfo->object;
4420   const elfcpp::Elf_Xword addend = rela.get_r_addend();
4421   elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr);
4422   bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0;
4423
4424   typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0);
4425
4426   const bool is_final = (gsym == NULL
4427                          ? !parameters->options().shared()
4428                          : gsym->final_value_is_known());
4429   tls::Tls_optimization optimized_type
4430       = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type);
4431   switch (r_type)
4432     {
4433     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
4434       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4435         {
4436           // If this code sequence is used in a non-executable section,
4437           // we will not optimize the R_X86_64_DTPOFF32/64 relocation,
4438           // on the assumption that it's being used by itself in a debug
4439           // section.  Therefore, in the unlikely event that the code
4440           // sequence appears in a non-executable section, we simply
4441           // leave it unoptimized.
4442           optimized_type = tls::TLSOPT_NONE;
4443         }
4444       if (optimized_type == tls::TLSOPT_TO_LE)
4445         {
4446           if (tls_segment == NULL)
4447             {
4448               gold_assert(parameters->errors()->error_count() > 0
4449                           || issue_undefined_symbol_error(gsym));
4450               return;
4451             }
4452           this->tls_gd_to_le(relinfo, relnum, tls_segment,
4453                              rela, r_type, value, view,
4454                              view_size);
4455           break;
4456         }
4457       else
4458         {
4459           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4460                                    ? GOT_TYPE_TLS_OFFSET
4461                                    : GOT_TYPE_TLS_PAIR);
4462           unsigned int got_offset;
4463           if (gsym != NULL)
4464             {
4465               gold_assert(gsym->has_got_offset(got_type));
4466               got_offset = gsym->got_offset(got_type) - target->got_size();
4467             }
4468           else
4469             {
4470               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4471               gold_assert(object->local_has_got_offset(r_sym, got_type));
4472               got_offset = (object->local_got_offset(r_sym, got_type)
4473                             - target->got_size());
4474             }
4475           if (optimized_type == tls::TLSOPT_TO_IE)
4476             {
4477               value = target->got_plt_section()->address() + got_offset;
4478               this->tls_gd_to_ie(relinfo, relnum, rela, r_type,
4479                                  value, view, address, view_size);
4480               break;
4481             }
4482           else if (optimized_type == tls::TLSOPT_NONE)
4483             {
4484               // Relocate the field with the offset of the pair of GOT
4485               // entries.
4486               value = target->got_plt_section()->address() + got_offset;
4487               Relocate_functions<size, false>::pcrela32(view, value, addend,
4488                                                         address);
4489               break;
4490             }
4491         }
4492       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4493                              _("unsupported reloc %u"), r_type);
4494       break;
4495
4496     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
4497     case elfcpp::R_X86_64_TLSDESC_CALL:
4498       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4499         {
4500           // See above comment for R_X86_64_TLSGD.
4501           optimized_type = tls::TLSOPT_NONE;
4502         }
4503       if (optimized_type == tls::TLSOPT_TO_LE)
4504         {
4505           if (tls_segment == NULL)
4506             {
4507               gold_assert(parameters->errors()->error_count() > 0
4508                           || issue_undefined_symbol_error(gsym));
4509               return;
4510             }
4511           this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
4512                                   rela, r_type, value, view,
4513                                   view_size);
4514           break;
4515         }
4516       else
4517         {
4518           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
4519                                    ? GOT_TYPE_TLS_OFFSET
4520                                    : GOT_TYPE_TLS_DESC);
4521           unsigned int got_offset = 0;
4522           if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
4523               && optimized_type == tls::TLSOPT_NONE)
4524             {
4525               // We created GOT entries in the .got.tlsdesc portion of
4526               // the .got.plt section, but the offset stored in the
4527               // symbol is the offset within .got.tlsdesc.
4528               got_offset = (target->got_size()
4529                             + target->got_plt_section()->data_size());
4530             }
4531           if (gsym != NULL)
4532             {
4533               gold_assert(gsym->has_got_offset(got_type));
4534               got_offset += gsym->got_offset(got_type) - target->got_size();
4535             }
4536           else
4537             {
4538               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4539               gold_assert(object->local_has_got_offset(r_sym, got_type));
4540               got_offset += (object->local_got_offset(r_sym, got_type)
4541                              - target->got_size());
4542             }
4543           if (optimized_type == tls::TLSOPT_TO_IE)
4544             {
4545               value = target->got_plt_section()->address() + got_offset;
4546               this->tls_desc_gd_to_ie(relinfo, relnum,
4547                                       rela, r_type, value, view, address,
4548                                       view_size);
4549               break;
4550             }
4551           else if (optimized_type == tls::TLSOPT_NONE)
4552             {
4553               if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4554                 {
4555                   // Relocate the field with the offset of the pair of GOT
4556                   // entries.
4557                   value = target->got_plt_section()->address() + got_offset;
4558                   Relocate_functions<size, false>::pcrela32(view, value, addend,
4559                                                             address);
4560                 }
4561               break;
4562             }
4563         }
4564       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4565                              _("unsupported reloc %u"), r_type);
4566       break;
4567
4568     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
4569       if (!is_executable && optimized_type == tls::TLSOPT_TO_LE)
4570         {
4571           // See above comment for R_X86_64_TLSGD.
4572           optimized_type = tls::TLSOPT_NONE;
4573         }
4574       if (optimized_type == tls::TLSOPT_TO_LE)
4575         {
4576           if (tls_segment == NULL)
4577             {
4578               gold_assert(parameters->errors()->error_count() > 0
4579                           || issue_undefined_symbol_error(gsym));
4580               return;
4581             }
4582           this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
4583                              value, view, view_size);
4584           break;
4585         }
4586       else if (optimized_type == tls::TLSOPT_NONE)
4587         {
4588           // Relocate the field with the offset of the GOT entry for
4589           // the module index.
4590           unsigned int got_offset;
4591           got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
4592                         - target->got_size());
4593           value = target->got_plt_section()->address() + got_offset;
4594           Relocate_functions<size, false>::pcrela32(view, value, addend,
4595                                                     address);
4596           break;
4597         }
4598       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4599                              _("unsupported reloc %u"), r_type);
4600       break;
4601
4602     case elfcpp::R_X86_64_DTPOFF32:
4603       // This relocation type is used in debugging information.
4604       // In that case we need to not optimize the value.  If the
4605       // section is not executable, then we assume we should not
4606       // optimize this reloc.  See comments above for R_X86_64_TLSGD,
4607       // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and
4608       // R_X86_64_TLSLD.
4609       if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4610         {
4611           if (tls_segment == NULL)
4612             {
4613               gold_assert(parameters->errors()->error_count() > 0
4614                           || issue_undefined_symbol_error(gsym));
4615               return;
4616             }
4617           value -= tls_segment->memsz();
4618         }
4619       Relocate_functions<size, false>::rela32(view, value, addend);
4620       break;
4621
4622     case elfcpp::R_X86_64_DTPOFF64:
4623       // See R_X86_64_DTPOFF32, just above, for why we check for is_executable.
4624       if (optimized_type == tls::TLSOPT_TO_LE && is_executable)
4625         {
4626           if (tls_segment == NULL)
4627             {
4628               gold_assert(parameters->errors()->error_count() > 0
4629                           || issue_undefined_symbol_error(gsym));
4630               return;
4631             }
4632           value -= tls_segment->memsz();
4633         }
4634       Relocate_functions<size, false>::rela64(view, value, addend);
4635       break;
4636
4637     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
4638       if (gsym != NULL
4639           && gsym->is_undefined()
4640           && parameters->options().output_is_executable())
4641         {
4642           Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4643                                                       NULL, rela,
4644                                                       r_type, value, view,
4645                                                       view_size);
4646           break;
4647         }
4648       else if (optimized_type == tls::TLSOPT_TO_LE)
4649         {
4650           if (tls_segment == NULL)
4651             {
4652               gold_assert(parameters->errors()->error_count() > 0
4653                           || issue_undefined_symbol_error(gsym));
4654               return;
4655             }
4656           Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum,
4657                                                       tls_segment, rela,
4658                                                       r_type, value, view,
4659                                                       view_size);
4660           break;
4661         }
4662       else if (optimized_type == tls::TLSOPT_NONE)
4663         {
4664           // Relocate the field with the offset of the GOT entry for
4665           // the tp-relative offset of the symbol.
4666           unsigned int got_offset;
4667           if (gsym != NULL)
4668             {
4669               gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
4670               got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
4671                             - target->got_size());
4672             }
4673           else
4674             {
4675               unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info());
4676               gold_assert(object->local_has_got_offset(r_sym,
4677                                                        GOT_TYPE_TLS_OFFSET));
4678               got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
4679                             - target->got_size());
4680             }
4681           value = target->got_plt_section()->address() + got_offset;
4682           Relocate_functions<size, false>::pcrela32(view, value, addend,
4683                                                     address);
4684           break;
4685         }
4686       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
4687                              _("unsupported reloc type %u"),
4688                              r_type);
4689       break;
4690
4691     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
4692       if (tls_segment == NULL)
4693         {
4694           gold_assert(parameters->errors()->error_count() > 0
4695                       || issue_undefined_symbol_error(gsym));
4696           return;
4697         }
4698       value -= tls_segment->memsz();
4699       Relocate_functions<size, false>::rela32(view, value, addend);
4700       break;
4701     }
4702 }
4703
4704 // Do a relocation in which we convert a TLS General-Dynamic to an
4705 // Initial-Exec.
4706
4707 template<int size>
4708 inline void
4709 Target_x86_64<size>::Relocate::tls_gd_to_ie(
4710     const Relocate_info<size, false>* relinfo,
4711     size_t relnum,
4712     const elfcpp::Rela<size, false>& rela,
4713     unsigned int,
4714     typename elfcpp::Elf_types<size>::Elf_Addr value,
4715     unsigned char* view,
4716     typename elfcpp::Elf_types<size>::Elf_Addr address,
4717     section_size_type view_size)
4718 {
4719   // For SIZE == 64:
4720   //    .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4721   //    .word 0x6666; rex64; call __tls_get_addr@PLT
4722   //    ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4723   //    .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4724   //    .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4725   //    ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
4726   // For SIZE == 32:
4727   //    leaq foo@tlsgd(%rip),%rdi;
4728   //    .word 0x6666; rex64; call __tls_get_addr@PLT
4729   //    ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4730   //    leaq foo@tlsgd(%rip),%rdi;
4731   //    .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4732   //    ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax
4733
4734   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4735   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4736                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
4737                   || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
4738
4739   if (size == 64)
4740     {
4741       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4742                        -4);
4743       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4744                      (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4745       memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4746              16);
4747     }
4748   else
4749     {
4750       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4751                        -3);
4752       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4753                      (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4754       memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0",
4755              15);
4756     }
4757
4758   const elfcpp::Elf_Xword addend = rela.get_r_addend();
4759   Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8,
4760                                             address);
4761
4762   // The next reloc should be a PLT32 reloc against __tls_get_addr.
4763   // We can skip it.
4764   this->skip_call_tls_get_addr_ = true;
4765 }
4766
4767 // Do a relocation in which we convert a TLS General-Dynamic to a
4768 // Local-Exec.
4769
4770 template<int size>
4771 inline void
4772 Target_x86_64<size>::Relocate::tls_gd_to_le(
4773     const Relocate_info<size, false>* relinfo,
4774     size_t relnum,
4775     Output_segment* tls_segment,
4776     const elfcpp::Rela<size, false>& rela,
4777     unsigned int,
4778     typename elfcpp::Elf_types<size>::Elf_Addr value,
4779     unsigned char* view,
4780     section_size_type view_size)
4781 {
4782   // For SIZE == 64:
4783   //    .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4784   //    .word 0x6666; rex64; call __tls_get_addr@PLT
4785   //    ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4786   //    .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
4787   //    .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4788   //    ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
4789   // For SIZE == 32:
4790   //    leaq foo@tlsgd(%rip),%rdi;
4791   //    .word 0x6666; rex64; call __tls_get_addr@PLT
4792   //    ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4793   //    leaq foo@tlsgd(%rip),%rdi;
4794   //    .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip)
4795   //    ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax
4796
4797   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
4798   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4799                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0
4800                   || memcmp(view + 4, "\x66\x48\xff", 3) == 0));
4801
4802   if (size == 64)
4803     {
4804       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4805                        -4);
4806       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4807                      (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
4808       memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4809              16);
4810     }
4811   else
4812     {
4813       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size,
4814                        -3);
4815       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4816                      (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0));
4817
4818       memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0",
4819              15);
4820     }
4821
4822   value -= tls_segment->memsz();
4823   Relocate_functions<size, false>::rela32(view + 8, value, 0);
4824
4825   // The next reloc should be a PLT32 reloc against __tls_get_addr.
4826   // We can skip it.
4827   this->skip_call_tls_get_addr_ = true;
4828 }
4829
4830 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
4831
4832 template<int size>
4833 inline void
4834 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie(
4835     const Relocate_info<size, false>* relinfo,
4836     size_t relnum,
4837     const elfcpp::Rela<size, false>& rela,
4838     unsigned int r_type,
4839     typename elfcpp::Elf_types<size>::Elf_Addr value,
4840     unsigned char* view,
4841     typename elfcpp::Elf_types<size>::Elf_Addr address,
4842     section_size_type view_size)
4843 {
4844   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4845     {
4846       // leaq foo@tlsdesc(%rip), %rax
4847       // ==> movq foo@gottpoff(%rip), %rax
4848       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4849       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4850       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4851                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4852       view[-2] = 0x8b;
4853       const elfcpp::Elf_Xword addend = rela.get_r_addend();
4854       Relocate_functions<size, false>::pcrela32(view, value, addend, address);
4855     }
4856   else
4857     {
4858       // call *foo@tlscall(%rax)
4859       // ==> nop; nop
4860       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4861       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4862       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4863                      view[0] == 0xff && view[1] == 0x10);
4864       view[0] = 0x66;
4865       view[1] = 0x90;
4866     }
4867 }
4868
4869 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
4870
4871 template<int size>
4872 inline void
4873 Target_x86_64<size>::Relocate::tls_desc_gd_to_le(
4874     const Relocate_info<size, false>* relinfo,
4875     size_t relnum,
4876     Output_segment* tls_segment,
4877     const elfcpp::Rela<size, false>& rela,
4878     unsigned int r_type,
4879     typename elfcpp::Elf_types<size>::Elf_Addr value,
4880     unsigned char* view,
4881     section_size_type view_size)
4882 {
4883   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
4884     {
4885       // leaq foo@tlsdesc(%rip), %rax
4886       // ==> movq foo@tpoff, %rax
4887       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4888       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4889       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4890                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
4891       view[-2] = 0xc7;
4892       view[-1] = 0xc0;
4893       value -= tls_segment->memsz();
4894       Relocate_functions<size, false>::rela32(view, value, 0);
4895     }
4896   else
4897     {
4898       // call *foo@tlscall(%rax)
4899       // ==> nop; nop
4900       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
4901       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
4902       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4903                      view[0] == 0xff && view[1] == 0x10);
4904       view[0] = 0x66;
4905       view[1] = 0x90;
4906     }
4907 }
4908
4909 template<int size>
4910 inline void
4911 Target_x86_64<size>::Relocate::tls_ld_to_le(
4912     const Relocate_info<size, false>* relinfo,
4913     size_t relnum,
4914     Output_segment*,
4915     const elfcpp::Rela<size, false>& rela,
4916     unsigned int,
4917     typename elfcpp::Elf_types<size>::Elf_Addr,
4918     unsigned char* view,
4919     section_size_type view_size)
4920 {
4921   // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
4922   // For SIZE == 64:
4923   // ... leq foo@dtpoff(%rax),%reg
4924   // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4925   // For SIZE == 32:
4926   // ... leq foo@dtpoff(%rax),%reg
4927   // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4928   // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip)
4929   // For SIZE == 64:
4930   // ... leq foo@dtpoff(%rax),%reg
4931   // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
4932   // For SIZE == 32:
4933   // ... leq foo@dtpoff(%rax),%reg
4934   // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx
4935
4936   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4937   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
4938
4939   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4940                  view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
4941
4942   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
4943                  view[4] == 0xe8 || view[4] == 0xff);
4944
4945   if (view[4] == 0xe8)
4946     {
4947       if (size == 64)
4948         memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
4949       else
4950         memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12);
4951     }
4952   else
4953     {
4954       if (size == 64)
4955         memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0",
4956                13);
4957       else
4958         memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0",
4959                13);
4960     }
4961
4962   // The next reloc should be a PLT32 reloc against __tls_get_addr.
4963   // We can skip it.
4964   this->skip_call_tls_get_addr_ = true;
4965 }
4966
4967 // Do a relocation in which we convert a TLS Initial-Exec to a
4968 // Local-Exec.
4969
4970 template<int size>
4971 inline void
4972 Target_x86_64<size>::Relocate::tls_ie_to_le(
4973     const Relocate_info<size, false>* relinfo,
4974     size_t relnum,
4975     Output_segment* tls_segment,
4976     const elfcpp::Rela<size, false>& rela,
4977     unsigned int,
4978     typename elfcpp::Elf_types<size>::Elf_Addr value,
4979     unsigned char* view,
4980     section_size_type view_size)
4981 {
4982   // We need to examine the opcodes to figure out which instruction we
4983   // are looking at.
4984
4985   // movq foo@gottpoff(%rip),%reg  ==>  movq $YY,%reg
4986   // addq foo@gottpoff(%rip),%reg  ==>  addq $YY,%reg
4987
4988   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
4989   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
4990
4991   unsigned char op1 = view[-3];
4992   unsigned char op2 = view[-2];
4993   unsigned char op3 = view[-1];
4994   unsigned char reg = op3 >> 3;
4995
4996   if (op2 == 0x8b)
4997     {
4998       // movq
4999       if (op1 == 0x4c)
5000         view[-3] = 0x49;
5001       else if (size == 32 && op1 == 0x44)
5002         view[-3] = 0x41;
5003       view[-2] = 0xc7;
5004       view[-1] = 0xc0 | reg;
5005     }
5006   else if (reg == 4)
5007     {
5008       // Special handling for %rsp.
5009       if (op1 == 0x4c)
5010         view[-3] = 0x49;
5011       else if (size == 32 && op1 == 0x44)
5012         view[-3] = 0x41;
5013       view[-2] = 0x81;
5014       view[-1] = 0xc0 | reg;
5015     }
5016   else
5017     {
5018       // addq
5019       if (op1 == 0x4c)
5020         view[-3] = 0x4d;
5021       else if (size == 32 && op1 == 0x44)
5022         view[-3] = 0x45;
5023       view[-2] = 0x8d;
5024       view[-1] = 0x80 | reg | (reg << 3);
5025     }
5026
5027   if (tls_segment != NULL)
5028     value -= tls_segment->memsz();
5029   Relocate_functions<size, false>::rela32(view, value, 0);
5030 }
5031
5032 // Relocate section data.
5033
5034 template<int size>
5035 void
5036 Target_x86_64<size>::relocate_section(
5037     const Relocate_info<size, false>* relinfo,
5038     unsigned int sh_type,
5039     const unsigned char* prelocs,
5040     size_t reloc_count,
5041     Output_section* output_section,
5042     bool needs_special_offset_handling,
5043     unsigned char* view,
5044     typename elfcpp::Elf_types<size>::Elf_Addr address,
5045     section_size_type view_size,
5046     const Reloc_symbol_changes* reloc_symbol_changes)
5047 {
5048   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5049       Classify_reloc;
5050
5051   gold_assert(sh_type == elfcpp::SHT_RELA);
5052
5053   gold::relocate_section<size, false, Target_x86_64<size>, Relocate,
5054                          gold::Default_comdat_behavior, Classify_reloc>(
5055     relinfo,
5056     this,
5057     prelocs,
5058     reloc_count,
5059     output_section,
5060     needs_special_offset_handling,
5061     view,
5062     address,
5063     view_size,
5064     reloc_symbol_changes);
5065 }
5066
5067 // Apply an incremental relocation.  Incremental relocations always refer
5068 // to global symbols.
5069
5070 template<int size>
5071 void
5072 Target_x86_64<size>::apply_relocation(
5073     const Relocate_info<size, false>* relinfo,
5074     typename elfcpp::Elf_types<size>::Elf_Addr r_offset,
5075     unsigned int r_type,
5076     typename elfcpp::Elf_types<size>::Elf_Swxword r_addend,
5077     const Symbol* gsym,
5078     unsigned char* view,
5079     typename elfcpp::Elf_types<size>::Elf_Addr address,
5080     section_size_type view_size)
5081 {
5082   gold::apply_relocation<size, false, Target_x86_64<size>,
5083                          typename Target_x86_64<size>::Relocate>(
5084     relinfo,
5085     this,
5086     r_offset,
5087     r_type,
5088     r_addend,
5089     gsym,
5090     view,
5091     address,
5092     view_size);
5093 }
5094
5095 // Scan the relocs during a relocatable link.
5096
5097 template<int size>
5098 void
5099 Target_x86_64<size>::scan_relocatable_relocs(
5100     Symbol_table* symtab,
5101     Layout* layout,
5102     Sized_relobj_file<size, false>* object,
5103     unsigned int data_shndx,
5104     unsigned int sh_type,
5105     const unsigned char* prelocs,
5106     size_t reloc_count,
5107     Output_section* output_section,
5108     bool needs_special_offset_handling,
5109     size_t local_symbol_count,
5110     const unsigned char* plocal_symbols,
5111     Relocatable_relocs* rr)
5112 {
5113   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5114       Classify_reloc;
5115   typedef gold::Default_scan_relocatable_relocs<Classify_reloc>
5116       Scan_relocatable_relocs;
5117
5118   gold_assert(sh_type == elfcpp::SHT_RELA);
5119
5120   gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>(
5121     symtab,
5122     layout,
5123     object,
5124     data_shndx,
5125     prelocs,
5126     reloc_count,
5127     output_section,
5128     needs_special_offset_handling,
5129     local_symbol_count,
5130     plocal_symbols,
5131     rr);
5132 }
5133
5134 // Scan the relocs for --emit-relocs.
5135
5136 template<int size>
5137 void
5138 Target_x86_64<size>::emit_relocs_scan(
5139     Symbol_table* symtab,
5140     Layout* layout,
5141     Sized_relobj_file<size, false>* object,
5142     unsigned int data_shndx,
5143     unsigned int sh_type,
5144     const unsigned char* prelocs,
5145     size_t reloc_count,
5146     Output_section* output_section,
5147     bool needs_special_offset_handling,
5148     size_t local_symbol_count,
5149     const unsigned char* plocal_syms,
5150     Relocatable_relocs* rr)
5151 {
5152   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5153       Classify_reloc;
5154   typedef gold::Default_emit_relocs_strategy<Classify_reloc>
5155       Emit_relocs_strategy;
5156
5157   gold_assert(sh_type == elfcpp::SHT_RELA);
5158
5159   gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>(
5160     symtab,
5161     layout,
5162     object,
5163     data_shndx,
5164     prelocs,
5165     reloc_count,
5166     output_section,
5167     needs_special_offset_handling,
5168     local_symbol_count,
5169     plocal_syms,
5170     rr);
5171 }
5172
5173 // Relocate a section during a relocatable link.
5174
5175 template<int size>
5176 void
5177 Target_x86_64<size>::relocate_relocs(
5178     const Relocate_info<size, false>* relinfo,
5179     unsigned int sh_type,
5180     const unsigned char* prelocs,
5181     size_t reloc_count,
5182     Output_section* output_section,
5183     typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section,
5184     unsigned char* view,
5185     typename elfcpp::Elf_types<size>::Elf_Addr view_address,
5186     section_size_type view_size,
5187     unsigned char* reloc_view,
5188     section_size_type reloc_view_size)
5189 {
5190   typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false>
5191       Classify_reloc;
5192
5193   gold_assert(sh_type == elfcpp::SHT_RELA);
5194
5195   gold::relocate_relocs<size, false, Classify_reloc>(
5196     relinfo,
5197     prelocs,
5198     reloc_count,
5199     output_section,
5200     offset_in_output_section,
5201     view,
5202     view_address,
5203     view_size,
5204     reloc_view,
5205     reloc_view_size);
5206 }
5207
5208 // Return the value to use for a dynamic which requires special
5209 // treatment.  This is how we support equality comparisons of function
5210 // pointers across shared library boundaries, as described in the
5211 // processor specific ABI supplement.
5212
5213 template<int size>
5214 uint64_t
5215 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const
5216 {
5217   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
5218   return this->plt_address_for_global(gsym);
5219 }
5220
5221 // Return a string used to fill a code section with nops to take up
5222 // the specified length.
5223
5224 template<int size>
5225 std::string
5226 Target_x86_64<size>::do_code_fill(section_size_type length) const
5227 {
5228   if (length >= 16)
5229     {
5230       // Build a jmpq instruction to skip over the bytes.
5231       unsigned char jmp[5];
5232       jmp[0] = 0xe9;
5233       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
5234       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
5235               + std::string(length - 5, static_cast<char>(0x90)));
5236     }
5237
5238   // Nop sequences of various lengths.
5239   const char nop1[1] = { '\x90' };                 // nop
5240   const char nop2[2] = { '\x66', '\x90' };         // xchg %ax %ax
5241   const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax)
5242   const char nop4[4] = { '\x0f', '\x1f', '\x40',   // nop 0(%rax)
5243                          '\x00'};
5244   const char nop5[5] = { '\x0f', '\x1f', '\x44',   // nop 0(%rax,%rax,1)
5245                          '\x00', '\x00' };
5246   const char nop6[6] = { '\x66', '\x0f', '\x1f',   // nopw 0(%rax,%rax,1)
5247                          '\x44', '\x00', '\x00' };
5248   const char nop7[7] = { '\x0f', '\x1f', '\x80',   // nopl 0L(%rax)
5249                          '\x00', '\x00', '\x00',
5250                          '\x00' };
5251   const char nop8[8] = { '\x0f', '\x1f', '\x84',   // nopl 0L(%rax,%rax,1)
5252                          '\x00', '\x00', '\x00',
5253                          '\x00', '\x00' };
5254   const char nop9[9] = { '\x66', '\x0f', '\x1f',   // nopw 0L(%rax,%rax,1)
5255                          '\x84', '\x00', '\x00',
5256                          '\x00', '\x00', '\x00' };
5257   const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5258                            '\x1f', '\x84', '\x00',
5259                            '\x00', '\x00', '\x00',
5260                            '\x00' };
5261   const char nop11[11] = { '\x66', '\x66', '\x2e', // data16
5262                            '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5263                            '\x00', '\x00', '\x00',
5264                            '\x00', '\x00' };
5265   const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16
5266                            '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5267                            '\x84', '\x00', '\x00',
5268                            '\x00', '\x00', '\x00' };
5269   const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16
5270                            '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1)
5271                            '\x1f', '\x84', '\x00',
5272                            '\x00', '\x00', '\x00',
5273                            '\x00' };
5274   const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16
5275                            '\x66', '\x66', '\x2e', // data16
5276                            '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1)
5277                            '\x00', '\x00', '\x00',
5278                            '\x00', '\x00' };
5279   const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16
5280                            '\x66', '\x66', '\x66', // data16; data16
5281                            '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1)
5282                            '\x84', '\x00', '\x00',
5283                            '\x00', '\x00', '\x00' };
5284
5285   const char* nops[16] = {
5286     NULL,
5287     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
5288     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
5289   };
5290
5291   return std::string(nops[length], length);
5292 }
5293
5294 // Return the addend to use for a target specific relocation.  The
5295 // only target specific relocation is R_X86_64_TLSDESC for a local
5296 // symbol.  We want to set the addend is the offset of the local
5297 // symbol in the TLS segment.
5298
5299 template<int size>
5300 uint64_t
5301 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type,
5302                                      uint64_t) const
5303 {
5304   gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
5305   uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
5306   gold_assert(intarg < this->tlsdesc_reloc_info_.size());
5307   const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
5308   const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym);
5309   gold_assert(psymval->is_tls_symbol());
5310   // The value of a TLS symbol is the offset in the TLS segment.
5311   return psymval->value(ti.object, 0);
5312 }
5313
5314 // Return the value to use for the base of a DW_EH_PE_datarel offset
5315 // in an FDE.  Solaris and SVR4 use DW_EH_PE_datarel because their
5316 // assembler can not write out the difference between two labels in
5317 // different sections, so instead of using a pc-relative value they
5318 // use an offset from the GOT.
5319
5320 template<int size>
5321 uint64_t
5322 Target_x86_64<size>::do_ehframe_datarel_base() const
5323 {
5324   gold_assert(this->global_offset_table_ != NULL);
5325   Symbol* sym = this->global_offset_table_;
5326   Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym);
5327   return ssym->value();
5328 }
5329
5330 // FNOFFSET in section SHNDX in OBJECT is the start of a function
5331 // compiled with -fsplit-stack.  The function calls non-split-stack
5332 // code.  We have to change the function so that it always ensures
5333 // that it has enough stack space to run some random function.
5334
5335 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 };
5336 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 };
5337 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 };
5338
5339 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 };
5340 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 };
5341 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 };
5342
5343 template<int size>
5344 void
5345 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx,
5346                                         section_offset_type fnoffset,
5347                                         section_size_type fnsize,
5348                                         const unsigned char*,
5349                                         size_t,
5350                                         unsigned char* view,
5351                                         section_size_type view_size,
5352                                         std::string* from,
5353                                         std::string* to) const
5354 {
5355   const char* const cmp_insn = reinterpret_cast<const char*>
5356       (size == 32 ? cmp_insn_32 : cmp_insn_64);
5357   const char* const lea_r10_insn = reinterpret_cast<const char*>
5358       (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64);
5359   const char* const lea_r11_insn = reinterpret_cast<const char*>
5360       (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64);
5361
5362   const size_t cmp_insn_len =
5363       (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64));
5364   const size_t lea_r10_insn_len =
5365       (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64));
5366   const size_t lea_r11_insn_len =
5367       (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64));
5368   const size_t nop_len = (size == 32 ? 7 : 8);
5369
5370   // The function starts with a comparison of the stack pointer and a
5371   // field in the TCB.  This is followed by a jump.
5372
5373   // cmp %fs:NN,%rsp
5374   if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len)
5375       && fnsize > nop_len + 1)
5376     {
5377       // We will call __morestack if the carry flag is set after this
5378       // comparison.  We turn the comparison into an stc instruction
5379       // and some nops.
5380       view[fnoffset] = '\xf9';
5381       this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len);
5382     }
5383   // lea NN(%rsp),%r10
5384   // lea NN(%rsp),%r11
5385   else if ((this->match_view(view, view_size, fnoffset,
5386                              lea_r10_insn, lea_r10_insn_len)
5387             || this->match_view(view, view_size, fnoffset,
5388                                 lea_r11_insn, lea_r11_insn_len))
5389            && fnsize > 8)
5390     {
5391       // This is loading an offset from the stack pointer for a
5392       // comparison.  The offset is negative, so we decrease the
5393       // offset by the amount of space we need for the stack.  This
5394       // means we will avoid calling __morestack if there happens to
5395       // be plenty of space on the stack already.
5396       unsigned char* pval = view + fnoffset + 4;
5397       uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
5398       val -= parameters->options().split_stack_adjust_size();
5399       elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
5400     }
5401   else
5402     {
5403       if (!object->has_no_split_stack())
5404         object->error(_("failed to match split-stack sequence at "
5405                         "section %u offset %0zx"),
5406                       shndx, static_cast<size_t>(fnoffset));
5407       return;
5408     }
5409
5410   // We have to change the function so that it calls
5411   // __morestack_non_split instead of __morestack.  The former will
5412   // allocate additional stack space.
5413   *from = "__morestack";
5414   *to = "__morestack_non_split";
5415 }
5416
5417 // The selector for x86_64 object files.  Note this is never instantiated
5418 // directly.  It's only used in Target_selector_x86_64_nacl, below.
5419
5420 template<int size>
5421 class Target_selector_x86_64 : public Target_selector_freebsd
5422 {
5423 public:
5424   Target_selector_x86_64()
5425     : Target_selector_freebsd(elfcpp::EM_X86_64, size, false,
5426                               (size == 64
5427                                ? "elf64-x86-64" : "elf32-x86-64"),
5428                               (size == 64
5429                                ? "elf64-x86-64-freebsd"
5430                                : "elf32-x86-64-freebsd"),
5431                               (size == 64 ? "elf_x86_64" : "elf32_x86_64"))
5432   { }
5433
5434   Target*
5435   do_instantiate_target()
5436   { return new Target_x86_64<size>(); }
5437
5438 };
5439
5440 // NaCl variant.  It uses different PLT contents.
5441
5442 template<int size>
5443 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size>
5444 {
5445  public:
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     : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5451                                    got, got_plt, got_irelative)
5452   { }
5453
5454   Output_data_plt_x86_64_nacl(Layout* layout,
5455                               Output_data_got<64, false>* got,
5456                               Output_data_got_plt_x86_64* got_plt,
5457                               Output_data_space* got_irelative,
5458                               unsigned int plt_count)
5459     : Output_data_plt_x86_64<size>(layout, plt_entry_size,
5460                                    got, got_plt, got_irelative,
5461                                    plt_count)
5462   { }
5463
5464  protected:
5465   virtual unsigned int
5466   do_get_plt_entry_size() const
5467   { return plt_entry_size; }
5468
5469   virtual void
5470   do_add_eh_frame(Layout* layout)
5471   {
5472     layout->add_eh_frame_for_plt(this,
5473                                  this->plt_eh_frame_cie,
5474                                  this->plt_eh_frame_cie_size,
5475                                  plt_eh_frame_fde,
5476                                  plt_eh_frame_fde_size);
5477   }
5478
5479   virtual void
5480   do_fill_first_plt_entry(unsigned char* pov,
5481                           typename elfcpp::Elf_types<size>::Elf_Addr got_addr,
5482                           typename elfcpp::Elf_types<size>::Elf_Addr plt_addr);
5483
5484   virtual unsigned int
5485   do_fill_plt_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                     unsigned int got_offset,
5489                     unsigned int plt_offset,
5490                     unsigned int plt_index);
5491
5492   virtual void
5493   do_fill_tlsdesc_entry(unsigned char* pov,
5494                         typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5495                         typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5496                         typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5497                         unsigned int tlsdesc_got_offset,
5498                         unsigned int plt_offset);
5499
5500  private:
5501   // The size of an entry in the PLT.
5502   static const int plt_entry_size = 64;
5503
5504   // The first entry in the PLT.
5505   static const unsigned char first_plt_entry[plt_entry_size];
5506
5507   // Other entries in the PLT for an executable.
5508   static const unsigned char plt_entry[plt_entry_size];
5509
5510   // The reserved TLSDESC entry in the PLT for an executable.
5511   static const unsigned char tlsdesc_plt_entry[plt_entry_size];
5512
5513   // The .eh_frame unwind information for the PLT.
5514   static const int plt_eh_frame_fde_size = 32;
5515   static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
5516 };
5517
5518 template<int size>
5519 class Target_x86_64_nacl : public Target_x86_64<size>
5520 {
5521  public:
5522   Target_x86_64_nacl()
5523     : Target_x86_64<size>(&x86_64_nacl_info)
5524   { }
5525
5526   virtual Output_data_plt_x86_64<size>*
5527   do_make_data_plt(Layout* layout,
5528                    Output_data_got<64, false>* got,
5529                    Output_data_got_plt_x86_64* got_plt,
5530                    Output_data_space* got_irelative)
5531   {
5532     return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5533                                                  got_irelative);
5534   }
5535
5536   virtual Output_data_plt_x86_64<size>*
5537   do_make_data_plt(Layout* layout,
5538                    Output_data_got<64, false>* got,
5539                    Output_data_got_plt_x86_64* got_plt,
5540                    Output_data_space* got_irelative,
5541                    unsigned int plt_count)
5542   {
5543     return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt,
5544                                                  got_irelative,
5545                                                  plt_count);
5546   }
5547
5548   virtual std::string
5549   do_code_fill(section_size_type length) const;
5550
5551  private:
5552   static const Target::Target_info x86_64_nacl_info;
5553 };
5554
5555 template<>
5556 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info =
5557 {
5558   64,                   // size
5559   false,                // is_big_endian
5560   elfcpp::EM_X86_64,    // machine_code
5561   false,                // has_make_symbol
5562   false,                // has_resolve
5563   true,                 // has_code_fill
5564   true,                 // is_default_stack_executable
5565   true,                 // can_icf_inline_merge_sections
5566   '\0',                 // wrap_char
5567   "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker
5568   0x20000,              // default_text_segment_address
5569   0x10000,              // abi_pagesize (overridable by -z max-page-size)
5570   0x10000,              // common_pagesize (overridable by -z common-page-size)
5571   true,                 // isolate_execinstr
5572   0x10000000,           // rosegment_gap
5573   elfcpp::SHN_UNDEF,    // small_common_shndx
5574   elfcpp::SHN_X86_64_LCOMMON,   // large_common_shndx
5575   0,                    // small_common_section_flags
5576   elfcpp::SHF_X86_64_LARGE,     // large_common_section_flags
5577   NULL,                 // attributes_section
5578   NULL,                 // attributes_vendor
5579   "_start",             // entry_symbol_name
5580   32,                   // hash_entry_size
5581 };
5582
5583 template<>
5584 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info =
5585 {
5586   32,                   // size
5587   false,                // is_big_endian
5588   elfcpp::EM_X86_64,    // machine_code
5589   false,                // has_make_symbol
5590   false,                // has_resolve
5591   true,                 // has_code_fill
5592   true,                 // is_default_stack_executable
5593   true,                 // can_icf_inline_merge_sections
5594   '\0',                 // wrap_char
5595   "/lib/ld-nacl-x86-64.so.1", // dynamic_linker
5596   0x20000,              // default_text_segment_address
5597   0x10000,              // abi_pagesize (overridable by -z max-page-size)
5598   0x10000,              // common_pagesize (overridable by -z common-page-size)
5599   true,                 // isolate_execinstr
5600   0x10000000,           // rosegment_gap
5601   elfcpp::SHN_UNDEF,    // small_common_shndx
5602   elfcpp::SHN_X86_64_LCOMMON,   // large_common_shndx
5603   0,                    // small_common_section_flags
5604   elfcpp::SHF_X86_64_LARGE,     // large_common_section_flags
5605   NULL,                 // attributes_section
5606   NULL,                 // attributes_vendor
5607   "_start",             // entry_symbol_name
5608   32,                   // hash_entry_size
5609 };
5610
5611 #define NACLMASK        0xe0            // 32-byte alignment mask.
5612
5613 // The first entry in the PLT.
5614
5615 template<int size>
5616 const unsigned char
5617 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] =
5618 {
5619   0xff, 0x35,                         // pushq contents of memory address
5620   0, 0, 0, 0,                         // replaced with address of .got + 8
5621   0x4c, 0x8b, 0x1d,                   // mov GOT+16(%rip), %r11
5622   0, 0, 0, 0,                         // replaced with address of .got + 16
5623   0x41, 0x83, 0xe3, NACLMASK,         // and $-32, %r11d
5624   0x4d, 0x01, 0xfb,                   // add %r15, %r11
5625   0x41, 0xff, 0xe3,                   // jmpq *%r11
5626
5627   // 9-byte nop sequence to pad out to the next 32-byte boundary.
5628   0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1)
5629
5630   // 32 bytes of nop to pad out to the standard size
5631   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
5632   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5633   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
5634   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5635   0x66,                                  // excess data32 prefix
5636   0x90                                   // nop
5637 };
5638
5639 template<int size>
5640 void
5641 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry(
5642     unsigned char* pov,
5643     typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5644     typename elfcpp::Elf_types<size>::Elf_Addr plt_address)
5645 {
5646   memcpy(pov, first_plt_entry, plt_entry_size);
5647   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5648                                               (got_address + 8
5649                                                - (plt_address + 2 + 4)));
5650   elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5651                                               (got_address + 16
5652                                                - (plt_address + 9 + 4)));
5653 }
5654
5655 // Subsequent entries in the PLT.
5656
5657 template<int size>
5658 const unsigned char
5659 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] =
5660 {
5661   0x4c, 0x8b, 0x1d,              // mov name@GOTPCREL(%rip),%r11
5662   0, 0, 0, 0,                    // replaced with address of symbol in .got
5663   0x41, 0x83, 0xe3, NACLMASK,    // and $-32, %r11d
5664   0x4d, 0x01, 0xfb,              // add %r15, %r11
5665   0x41, 0xff, 0xe3,              // jmpq *%r11
5666
5667   // 15-byte nop sequence to pad out to the next 32-byte boundary.
5668   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
5669   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5670
5671   // Lazy GOT entries point here (32-byte aligned).
5672   0x68,                       // pushq immediate
5673   0, 0, 0, 0,                 // replaced with index into relocation table
5674   0xe9,                       // jmp relative
5675   0, 0, 0, 0,                 // replaced with offset to start of .plt0
5676
5677   // 22 bytes of nop to pad out to the standard size.
5678   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
5679   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5680   0x0f, 0x1f, 0x80, 0, 0, 0, 0,          // nopl 0x0(%rax)
5681 };
5682
5683 template<int size>
5684 unsigned int
5685 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry(
5686     unsigned char* pov,
5687     typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5688     typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5689     unsigned int got_offset,
5690     unsigned int plt_offset,
5691     unsigned int plt_index)
5692 {
5693   memcpy(pov, plt_entry, plt_entry_size);
5694   elfcpp::Swap_unaligned<32, false>::writeval(pov + 3,
5695                                               (got_address + got_offset
5696                                                - (plt_address + plt_offset
5697                                                   + 3 + 4)));
5698
5699   elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index);
5700   elfcpp::Swap_unaligned<32, false>::writeval(pov + 38,
5701                                               - (plt_offset + 38 + 4));
5702
5703   return 32;
5704 }
5705
5706 // The reserved TLSDESC entry in the PLT.
5707
5708 template<int size>
5709 const unsigned char
5710 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] =
5711 {
5712   0xff, 0x35,                   // pushq x(%rip)
5713   0, 0, 0, 0,   // replaced with address of linkmap GOT entry (at PLTGOT + 8)
5714   0x4c, 0x8b, 0x1d,             // mov y(%rip),%r11
5715   0, 0, 0, 0,   // replaced with offset of reserved TLSDESC_GOT entry
5716   0x41, 0x83, 0xe3, NACLMASK,   // and $-32, %r11d
5717   0x4d, 0x01, 0xfb,             // add %r15, %r11
5718   0x41, 0xff, 0xe3,             // jmpq *%r11
5719
5720   // 41 bytes of nop to pad out to the standard size.
5721   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
5722   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5723   0x66, 0x66, 0x66, 0x66, 0x66, 0x66,    // excess data32 prefixes
5724   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5725   0x66, 0x66,                            // excess data32 prefixes
5726   0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1)
5727 };
5728
5729 template<int size>
5730 void
5731 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry(
5732     unsigned char* pov,
5733     typename elfcpp::Elf_types<size>::Elf_Addr got_address,
5734     typename elfcpp::Elf_types<size>::Elf_Addr plt_address,
5735     typename elfcpp::Elf_types<size>::Elf_Addr got_base,
5736     unsigned int tlsdesc_got_offset,
5737     unsigned int plt_offset)
5738 {
5739   memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
5740   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
5741                                               (got_address + 8
5742                                                - (plt_address + plt_offset
5743                                                   + 2 + 4)));
5744   elfcpp::Swap_unaligned<32, false>::writeval(pov + 9,
5745                                               (got_base
5746                                                + tlsdesc_got_offset
5747                                                - (plt_address + plt_offset
5748                                                   + 9 + 4)));
5749 }
5750
5751 // The .eh_frame unwind information for the PLT.
5752
5753 template<int size>
5754 const unsigned char
5755 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] =
5756 {
5757   0, 0, 0, 0,                           // Replaced with offset to .plt.
5758   0, 0, 0, 0,                           // Replaced with size of .plt.
5759   0,                                    // Augmentation size.
5760   elfcpp::DW_CFA_def_cfa_offset, 16,    // DW_CFA_def_cfa_offset: 16.
5761   elfcpp::DW_CFA_advance_loc + 6,       // Advance 6 to __PLT__ + 6.
5762   elfcpp::DW_CFA_def_cfa_offset, 24,    // DW_CFA_def_cfa_offset: 24.
5763   elfcpp::DW_CFA_advance_loc + 58,      // Advance 58 to __PLT__ + 64.
5764   elfcpp::DW_CFA_def_cfa_expression,    // DW_CFA_def_cfa_expression.
5765   13,                                   // Block length.
5766   elfcpp::DW_OP_breg7, 8,               // Push %rsp + 8.
5767   elfcpp::DW_OP_breg16, 0,              // Push %rip.
5768   elfcpp::DW_OP_const1u, 63,            // Push 0x3f.
5769   elfcpp::DW_OP_and,                    // & (%rip & 0x3f).
5770   elfcpp::DW_OP_const1u, 37,            // Push 0x25.
5771   elfcpp::DW_OP_ge,                     // >= ((%rip & 0x3f) >= 0x25)
5772   elfcpp::DW_OP_lit3,                   // Push 3.
5773   elfcpp::DW_OP_shl,                    // << (((%rip & 0x3f) >= 0x25) << 3)
5774   elfcpp::DW_OP_plus,                   // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8
5775   elfcpp::DW_CFA_nop,                   // Align to 32 bytes.
5776   elfcpp::DW_CFA_nop
5777 };
5778
5779 // Return a string used to fill a code section with nops.
5780 // For NaCl, long NOPs are only valid if they do not cross
5781 // bundle alignment boundaries, so keep it simple with one-byte NOPs.
5782 template<int size>
5783 std::string
5784 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const
5785 {
5786   return std::string(length, static_cast<char>(0x90));
5787 }
5788
5789 // The selector for x86_64-nacl object files.
5790
5791 template<int size>
5792 class Target_selector_x86_64_nacl
5793   : public Target_selector_nacl<Target_selector_x86_64<size>,
5794                                 Target_x86_64_nacl<size> >
5795 {
5796  public:
5797   Target_selector_x86_64_nacl()
5798     : Target_selector_nacl<Target_selector_x86_64<size>,
5799                            Target_x86_64_nacl<size> >("x86-64",
5800                                                       size == 64
5801                                                       ? "elf64-x86-64-nacl"
5802                                                       : "elf32-x86-64-nacl",
5803                                                       size == 64
5804                                                       ? "elf_x86_64_nacl"
5805                                                       : "elf32_x86_64_nacl")
5806   { }
5807 };
5808
5809 Target_selector_x86_64_nacl<64> target_selector_x86_64;
5810 Target_selector_x86_64_nacl<32> target_selector_x32;
5811
5812 } // End anonymous namespace.