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