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