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