elfcpp:/
[platform/upstream/binutils.git] / gold / x86_64.cc
1 // x86_64.cc -- x86_64 target support for gold.
2
3 // Copyright 2006, 2007, 2008, 2009, 2010 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 "parameters.h"
29 #include "reloc.h"
30 #include "x86_64.h"
31 #include "object.h"
32 #include "symtab.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "copy-relocs.h"
36 #include "target.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
39 #include "tls.h"
40 #include "freebsd.h"
41 #include "gc.h"
42 #include "icf.h"
43
44 namespace
45 {
46
47 using namespace gold;
48
49 class Output_data_plt_x86_64;
50
51 // The x86_64 target class.
52 // See the ABI at
53 //   http://www.x86-64.org/documentation/abi.pdf
54 // TLS info comes from
55 //   http://people.redhat.com/drepper/tls.pdf
56 //   http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
57
58 class Target_x86_64 : public Target_freebsd<64, false>
59 {
60  public:
61   // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures
62   // uses only Elf64_Rela relocation entries with explicit addends."
63   typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
64
65   Target_x86_64()
66     : Target_freebsd<64, false>(&x86_64_info),
67       got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
68       global_offset_table_(NULL), rela_dyn_(NULL),
69       copy_relocs_(elfcpp::R_X86_64_COPY), dynbss_(NULL),
70       got_mod_index_offset_(-1U), tlsdesc_reloc_info_(),
71       tls_base_symbol_defined_(false)
72   { }
73
74   // This function should be defined in targets that can use relocation
75   // types to determine (implemented in local_reloc_may_be_function_pointer
76   // and global_reloc_may_be_function_pointer)
77   // if a function's pointer is taken.  ICF uses this in safe mode to only
78   // fold those functions whose pointer is defintely not taken.  For x86_64
79   // pie binaries, safe ICF cannot be done by looking at relocation types.
80   inline bool
81   can_check_for_function_pointers() const
82   { return !parameters->options().pie(); }
83
84   // Hook for a new output section.
85   void
86   do_new_output_section(Output_section*) const;
87
88   // Scan the relocations to look for symbol adjustments.
89   void
90   gc_process_relocs(Symbol_table* symtab,
91                     Layout* layout,
92                     Sized_relobj<64, false>* object,
93                     unsigned int data_shndx,
94                     unsigned int sh_type,
95                     const unsigned char* prelocs,
96                     size_t reloc_count,
97                     Output_section* output_section,
98                     bool needs_special_offset_handling,
99                     size_t local_symbol_count,
100                     const unsigned char* plocal_symbols);
101
102   // Scan the relocations to look for symbol adjustments.
103   void
104   scan_relocs(Symbol_table* symtab,
105               Layout* layout,
106               Sized_relobj<64, false>* object,
107               unsigned int data_shndx,
108               unsigned int sh_type,
109               const unsigned char* prelocs,
110               size_t reloc_count,
111               Output_section* output_section,
112               bool needs_special_offset_handling,
113               size_t local_symbol_count,
114               const unsigned char* plocal_symbols);
115
116   // Finalize the sections.
117   void
118   do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
119
120   // Return the value to use for a dynamic which requires special
121   // treatment.
122   uint64_t
123   do_dynsym_value(const Symbol*) const;
124
125   // Relocate a section.
126   void
127   relocate_section(const Relocate_info<64, false>*,
128                    unsigned int sh_type,
129                    const unsigned char* prelocs,
130                    size_t reloc_count,
131                    Output_section* output_section,
132                    bool needs_special_offset_handling,
133                    unsigned char* view,
134                    elfcpp::Elf_types<64>::Elf_Addr view_address,
135                    section_size_type view_size,
136                    const Reloc_symbol_changes*);
137
138   // Scan the relocs during a relocatable link.
139   void
140   scan_relocatable_relocs(Symbol_table* symtab,
141                           Layout* layout,
142                           Sized_relobj<64, false>* object,
143                           unsigned int data_shndx,
144                           unsigned int sh_type,
145                           const unsigned char* prelocs,
146                           size_t reloc_count,
147                           Output_section* output_section,
148                           bool needs_special_offset_handling,
149                           size_t local_symbol_count,
150                           const unsigned char* plocal_symbols,
151                           Relocatable_relocs*);
152
153   // Relocate a section during a relocatable link.
154   void
155   relocate_for_relocatable(const Relocate_info<64, false>*,
156                            unsigned int sh_type,
157                            const unsigned char* prelocs,
158                            size_t reloc_count,
159                            Output_section* output_section,
160                            off_t offset_in_output_section,
161                            const Relocatable_relocs*,
162                            unsigned char* view,
163                            elfcpp::Elf_types<64>::Elf_Addr view_address,
164                            section_size_type view_size,
165                            unsigned char* reloc_view,
166                            section_size_type reloc_view_size);
167
168   // Return a string used to fill a code section with nops.
169   std::string
170   do_code_fill(section_size_type length) const;
171
172   // Return whether SYM is defined by the ABI.
173   bool
174   do_is_defined_by_abi(const Symbol* sym) const
175   { return strcmp(sym->name(), "__tls_get_addr") == 0; }
176
177   // Return the symbol index to use for a target specific relocation.
178   // The only target specific relocation is R_X86_64_TLSDESC for a
179   // local symbol, which is an absolute reloc.
180   unsigned int
181   do_reloc_symbol_index(void*, unsigned int r_type) const
182   {
183     gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
184     return 0;
185   }
186
187   // Return the addend to use for a target specific relocation.
188   uint64_t
189   do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const;
190
191   // Adjust -fstack-split code which calls non-stack-split code.
192   void
193   do_calls_non_split(Relobj* object, unsigned int shndx,
194                      section_offset_type fnoffset, section_size_type fnsize,
195                      unsigned char* view, section_size_type view_size,
196                      std::string* from, std::string* to) const;
197
198   // Return the size of the GOT section.
199   section_size_type
200   got_size()
201   {
202     gold_assert(this->got_ != NULL);
203     return this->got_->data_size();
204   }
205
206   // Add a new reloc argument, returning the index in the vector.
207   size_t
208   add_tlsdesc_info(Sized_relobj<64, false>* object, unsigned int r_sym)
209   {
210     this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym));
211     return this->tlsdesc_reloc_info_.size() - 1;
212   }
213
214  private:
215   // The class which scans relocations.
216   class Scan
217   {
218   public:
219     Scan()
220       : issued_non_pic_error_(false)
221     { }
222
223     inline void
224     local(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
225           Sized_relobj<64, false>* object,
226           unsigned int data_shndx,
227           Output_section* output_section,
228           const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
229           const elfcpp::Sym<64, false>& lsym);
230
231     inline void
232     global(Symbol_table* symtab, Layout* layout, Target_x86_64* target,
233            Sized_relobj<64, false>* object,
234            unsigned int data_shndx,
235            Output_section* output_section,
236            const elfcpp::Rela<64, false>& reloc, unsigned int r_type,
237            Symbol* gsym);
238
239     inline bool
240     local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
241                                         Target_x86_64* target,
242                                         Sized_relobj<64, false>* object,
243                                         unsigned int data_shndx,
244                                         Output_section* output_section,
245                                         const elfcpp::Rela<64, false>& reloc,
246                                         unsigned int r_type,
247                                         const elfcpp::Sym<64, false>& lsym);
248
249     inline bool
250     global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
251                                          Target_x86_64* target,
252                                          Sized_relobj<64, false>* object,
253                                          unsigned int data_shndx,
254                                          Output_section* output_section,
255                                          const elfcpp::Rela<64, false>& reloc,
256                                          unsigned int r_type,
257                                          Symbol* gsym);
258
259   private:
260     static void
261     unsupported_reloc_local(Sized_relobj<64, false>*, unsigned int r_type);
262
263     static void
264     unsupported_reloc_global(Sized_relobj<64, false>*, unsigned int r_type,
265                              Symbol*);
266
267     void
268     check_non_pic(Relobj*, unsigned int r_type);
269
270     inline bool
271     possible_function_pointer_reloc(unsigned int r_type);
272
273     // Whether we have issued an error about a non-PIC compilation.
274     bool issued_non_pic_error_;
275   };
276
277   // The class which implements relocation.
278   class Relocate
279   {
280    public:
281     Relocate()
282       : skip_call_tls_get_addr_(false), saw_tls_block_reloc_(false)
283     { }
284
285     ~Relocate()
286     {
287       if (this->skip_call_tls_get_addr_)
288         {
289           // FIXME: This needs to specify the location somehow.
290           gold_error(_("missing expected TLS relocation"));
291         }
292     }
293
294     // Do a relocation.  Return false if the caller should not issue
295     // any warnings about this relocation.
296     inline bool
297     relocate(const Relocate_info<64, false>*, Target_x86_64*, Output_section*,
298              size_t relnum, const elfcpp::Rela<64, false>&,
299              unsigned int r_type, const Sized_symbol<64>*,
300              const Symbol_value<64>*,
301              unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
302              section_size_type);
303
304    private:
305     // Do a TLS relocation.
306     inline void
307     relocate_tls(const Relocate_info<64, false>*, Target_x86_64*,
308                  size_t relnum, const elfcpp::Rela<64, false>&,
309                  unsigned int r_type, const Sized_symbol<64>*,
310                  const Symbol_value<64>*,
311                  unsigned char*, elfcpp::Elf_types<64>::Elf_Addr,
312                  section_size_type);
313
314     // Do a TLS General-Dynamic to Initial-Exec transition.
315     inline void
316     tls_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
317                  Output_segment* tls_segment,
318                  const elfcpp::Rela<64, false>&, unsigned int r_type,
319                  elfcpp::Elf_types<64>::Elf_Addr value,
320                  unsigned char* view,
321                  elfcpp::Elf_types<64>::Elf_Addr,
322                  section_size_type view_size);
323
324     // Do a TLS General-Dynamic to Local-Exec transition.
325     inline void
326     tls_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
327                  Output_segment* tls_segment,
328                  const elfcpp::Rela<64, false>&, unsigned int r_type,
329                  elfcpp::Elf_types<64>::Elf_Addr value,
330                  unsigned char* view,
331                  section_size_type view_size);
332
333     // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
334     inline void
335     tls_desc_gd_to_ie(const Relocate_info<64, false>*, size_t relnum,
336                       Output_segment* tls_segment,
337                       const elfcpp::Rela<64, false>&, unsigned int r_type,
338                       elfcpp::Elf_types<64>::Elf_Addr value,
339                       unsigned char* view,
340                       elfcpp::Elf_types<64>::Elf_Addr,
341                       section_size_type view_size);
342
343     // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
344     inline void
345     tls_desc_gd_to_le(const Relocate_info<64, false>*, size_t relnum,
346                       Output_segment* tls_segment,
347                       const elfcpp::Rela<64, false>&, unsigned int r_type,
348                       elfcpp::Elf_types<64>::Elf_Addr value,
349                       unsigned char* view,
350                       section_size_type view_size);
351
352     // Do a TLS Local-Dynamic to Local-Exec transition.
353     inline void
354     tls_ld_to_le(const Relocate_info<64, false>*, size_t relnum,
355                  Output_segment* tls_segment,
356                  const elfcpp::Rela<64, false>&, unsigned int r_type,
357                  elfcpp::Elf_types<64>::Elf_Addr value,
358                  unsigned char* view,
359                  section_size_type view_size);
360
361     // Do a TLS Initial-Exec to Local-Exec transition.
362     static inline void
363     tls_ie_to_le(const Relocate_info<64, false>*, size_t relnum,
364                  Output_segment* tls_segment,
365                  const elfcpp::Rela<64, false>&, unsigned int r_type,
366                  elfcpp::Elf_types<64>::Elf_Addr value,
367                  unsigned char* view,
368                  section_size_type view_size);
369
370     // This is set if we should skip the next reloc, which should be a
371     // PLT32 reloc against ___tls_get_addr.
372     bool skip_call_tls_get_addr_;
373
374     // This is set if we see a relocation which could load the address
375     // of the TLS block.  Whether we see such a relocation determines
376     // how we handle the R_X86_64_DTPOFF32 relocation, which is used
377     // in debugging sections.
378     bool saw_tls_block_reloc_;
379   };
380
381   // A class which returns the size required for a relocation type,
382   // used while scanning relocs during a relocatable link.
383   class Relocatable_size_for_reloc
384   {
385    public:
386     unsigned int
387     get_size_for_reloc(unsigned int, Relobj*);
388   };
389
390   // Adjust TLS relocation type based on the options and whether this
391   // is a local symbol.
392   static tls::Tls_optimization
393   optimize_tls_reloc(bool is_final, int r_type);
394
395   // Get the GOT section, creating it if necessary.
396   Output_data_got<64, false>*
397   got_section(Symbol_table*, Layout*);
398
399   // Get the GOT PLT section.
400   Output_data_space*
401   got_plt_section() const
402   {
403     gold_assert(this->got_plt_ != NULL);
404     return this->got_plt_;
405   }
406
407   // Get the GOT section for TLSDESC entries.
408   Output_data_got<64, false>*
409   got_tlsdesc_section() const
410   {
411     gold_assert(this->got_tlsdesc_ != NULL);
412     return this->got_tlsdesc_;
413   }
414
415   // Create the PLT section.
416   void
417   make_plt_section(Symbol_table* symtab, Layout* layout);
418
419   // Create a PLT entry for a global symbol.
420   void
421   make_plt_entry(Symbol_table*, Layout*, Symbol*);
422
423   // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
424   void
425   define_tls_base_symbol(Symbol_table*, Layout*);
426
427   // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
428   void
429   reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout);
430
431   // Create a GOT entry for the TLS module index.
432   unsigned int
433   got_mod_index_entry(Symbol_table* symtab, Layout* layout,
434                       Sized_relobj<64, false>* object);
435
436   // Get the PLT section.
437   Output_data_plt_x86_64*
438   plt_section() const
439   {
440     gold_assert(this->plt_ != NULL);
441     return this->plt_;
442   }
443
444   // Get the dynamic reloc section, creating it if necessary.
445   Reloc_section*
446   rela_dyn_section(Layout*);
447
448   // Get the section to use for TLSDESC relocations.
449   Reloc_section*
450   rela_tlsdesc_section(Layout*) const;
451
452   // Add a potential copy relocation.
453   void
454   copy_reloc(Symbol_table* symtab, Layout* layout,
455              Sized_relobj<64, false>* object,
456              unsigned int shndx, Output_section* output_section,
457              Symbol* sym, const elfcpp::Rela<64, false>& reloc)
458   {
459     this->copy_relocs_.copy_reloc(symtab, layout,
460                                   symtab->get_sized_symbol<64>(sym),
461                                   object, shndx, output_section,
462                                   reloc, this->rela_dyn_section(layout));
463   }
464
465   // Information about this specific target which we pass to the
466   // general Target structure.
467   static const Target::Target_info x86_64_info;
468
469   enum Got_type
470   {
471     GOT_TYPE_STANDARD = 0,      // GOT entry for a regular symbol
472     GOT_TYPE_TLS_OFFSET = 1,    // GOT entry for TLS offset
473     GOT_TYPE_TLS_PAIR = 2,      // GOT entry for TLS module/offset pair
474     GOT_TYPE_TLS_DESC = 3       // GOT entry for TLS_DESC pair
475   };
476
477   // This type is used as the argument to the target specific
478   // relocation routines.  The only target specific reloc is
479   // R_X86_64_TLSDESC against a local symbol.
480   struct Tlsdesc_info
481   {
482     Tlsdesc_info(Sized_relobj<64, false>* a_object, unsigned int a_r_sym)
483       : object(a_object), r_sym(a_r_sym)
484     { }
485
486     // The object in which the local symbol is defined.
487     Sized_relobj<64, false>* object;
488     // The local symbol index in the object.
489     unsigned int r_sym;
490   };
491
492   // The GOT section.
493   Output_data_got<64, false>* got_;
494   // The PLT section.
495   Output_data_plt_x86_64* plt_;
496   // The GOT PLT section.
497   Output_data_space* got_plt_;
498   // The GOT section for TLSDESC relocations.
499   Output_data_got<64, false>* got_tlsdesc_;
500   // The _GLOBAL_OFFSET_TABLE_ symbol.
501   Symbol* global_offset_table_;
502   // The dynamic reloc section.
503   Reloc_section* rela_dyn_;
504   // Relocs saved to avoid a COPY reloc.
505   Copy_relocs<elfcpp::SHT_RELA, 64, false> copy_relocs_;
506   // Space for variables copied with a COPY reloc.
507   Output_data_space* dynbss_;
508   // Offset of the GOT entry for the TLS module index.
509   unsigned int got_mod_index_offset_;
510   // We handle R_X86_64_TLSDESC against a local symbol as a target
511   // specific relocation.  Here we store the object and local symbol
512   // index for the relocation.
513   std::vector<Tlsdesc_info> tlsdesc_reloc_info_;
514   // True if the _TLS_MODULE_BASE_ symbol has been defined.
515   bool tls_base_symbol_defined_;
516 };
517
518 const Target::Target_info Target_x86_64::x86_64_info =
519 {
520   64,                   // size
521   false,                // is_big_endian
522   elfcpp::EM_X86_64,    // machine_code
523   false,                // has_make_symbol
524   false,                // has_resolve
525   true,                 // has_code_fill
526   true,                 // is_default_stack_executable
527   '\0',                 // wrap_char
528   "/lib/ld64.so.1",     // program interpreter
529   0x400000,             // default_text_segment_address
530   0x1000,               // abi_pagesize (overridable by -z max-page-size)
531   0x1000,               // common_pagesize (overridable by -z common-page-size)
532   elfcpp::SHN_UNDEF,    // small_common_shndx
533   elfcpp::SHN_X86_64_LCOMMON,   // large_common_shndx
534   0,                    // small_common_section_flags
535   elfcpp::SHF_X86_64_LARGE,     // large_common_section_flags
536   NULL,                 // attributes_section
537   NULL                  // attributes_vendor
538 };
539
540 // This is called when a new output section is created.  This is where
541 // we handle the SHF_X86_64_LARGE.
542
543 void
544 Target_x86_64::do_new_output_section(Output_section *os) const
545 {
546   if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0)
547     os->set_is_large_section();
548 }
549
550 // Get the GOT section, creating it if necessary.
551
552 Output_data_got<64, false>*
553 Target_x86_64::got_section(Symbol_table* symtab, Layout* layout)
554 {
555   if (this->got_ == NULL)
556     {
557       gold_assert(symtab != NULL && layout != NULL);
558
559       this->got_ = new Output_data_got<64, false>();
560
561       layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
562                                       (elfcpp::SHF_ALLOC
563                                        | elfcpp::SHF_WRITE),
564                                       this->got_, ORDER_RELRO_LAST,
565                                       true);
566
567       this->got_plt_ = new Output_data_space(8, "** GOT PLT");
568       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
569                                       (elfcpp::SHF_ALLOC
570                                        | elfcpp::SHF_WRITE),
571                                       this->got_plt_, ORDER_NON_RELRO_FIRST,
572                                       false);
573
574       // The first three entries are reserved.
575       this->got_plt_->set_current_data_size(3 * 8);
576
577       // Those bytes can go into the relro segment.
578       layout->increase_relro(3 * 8);
579
580       // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
581       this->global_offset_table_ =
582         symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
583                                       Symbol_table::PREDEFINED,
584                                       this->got_plt_,
585                                       0, 0, elfcpp::STT_OBJECT,
586                                       elfcpp::STB_LOCAL,
587                                       elfcpp::STV_HIDDEN, 0,
588                                       false, false);
589
590       // If there are any TLSDESC relocations, they get GOT entries in
591       // .got.plt after the jump slot entries.
592       this->got_tlsdesc_ = new Output_data_got<64, false>();
593       layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
594                                       (elfcpp::SHF_ALLOC
595                                        | elfcpp::SHF_WRITE),
596                                       this->got_tlsdesc_,
597                                       ORDER_NON_RELRO_FIRST, false);
598     }
599
600   return this->got_;
601 }
602
603 // Get the dynamic reloc section, creating it if necessary.
604
605 Target_x86_64::Reloc_section*
606 Target_x86_64::rela_dyn_section(Layout* layout)
607 {
608   if (this->rela_dyn_ == NULL)
609     {
610       gold_assert(layout != NULL);
611       this->rela_dyn_ = new Reloc_section(parameters->options().combreloc());
612       layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA,
613                                       elfcpp::SHF_ALLOC, this->rela_dyn_,
614                                       ORDER_DYNAMIC_RELOCS, false);
615     }
616   return this->rela_dyn_;
617 }
618
619 // A class to handle the PLT data.
620
621 class Output_data_plt_x86_64 : public Output_section_data
622 {
623  public:
624   typedef Output_data_reloc<elfcpp::SHT_RELA, true, 64, false> Reloc_section;
625
626   Output_data_plt_x86_64(Layout*, Output_data_got<64, false>*,
627                          Output_data_space*);
628
629   // Add an entry to the PLT.
630   void
631   add_entry(Symbol* gsym);
632
633   // Add the reserved TLSDESC_PLT entry to the PLT.
634   void
635   reserve_tlsdesc_entry(unsigned int got_offset)
636   { this->tlsdesc_got_offset_ = got_offset; }
637
638   // Return true if a TLSDESC_PLT entry has been reserved.
639   bool
640   has_tlsdesc_entry() const
641   { return this->tlsdesc_got_offset_ != -1U; }
642
643   // Return the GOT offset for the reserved TLSDESC_PLT entry.
644   unsigned int
645   get_tlsdesc_got_offset() const
646   { return this->tlsdesc_got_offset_; }
647
648   // Return the offset of the reserved TLSDESC_PLT entry.
649   unsigned int
650   get_tlsdesc_plt_offset() const
651   { return (this->count_ + 1) * plt_entry_size; }
652
653   // Return the .rela.plt section data.
654   const Reloc_section*
655   rela_plt() const
656   { return this->rel_; }
657
658   // Return where the TLSDESC relocations should go.
659   Reloc_section*
660   rela_tlsdesc(Layout*);
661
662  protected:
663   void
664   do_adjust_output_section(Output_section* os);
665
666   // Write to a map file.
667   void
668   do_print_to_mapfile(Mapfile* mapfile) const
669   { mapfile->print_output_data(this, _("** PLT")); }
670
671  private:
672   // The size of an entry in the PLT.
673   static const int plt_entry_size = 16;
674
675   // The first entry in the PLT.
676   // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same
677   // procedure linkage table for both programs and shared objects."
678   static unsigned char first_plt_entry[plt_entry_size];
679
680   // Other entries in the PLT for an executable.
681   static unsigned char plt_entry[plt_entry_size];
682
683   // The reserved TLSDESC entry in the PLT for an executable.
684   static unsigned char tlsdesc_plt_entry[plt_entry_size];
685
686   // Set the final size.
687   void
688   set_final_data_size();
689
690   // Write out the PLT data.
691   void
692   do_write(Output_file*);
693
694   // The reloc section.
695   Reloc_section* rel_;
696   // The TLSDESC relocs, if necessary.  These must follow the regular
697   // PLT relocs.
698   Reloc_section* tlsdesc_rel_;
699   // The .got section.
700   Output_data_got<64, false>* got_;
701   // The .got.plt section.
702   Output_data_space* got_plt_;
703   // The number of PLT entries.
704   unsigned int count_;
705   // Offset of the reserved TLSDESC_GOT entry when needed.
706   unsigned int tlsdesc_got_offset_;
707 };
708
709 // Create the PLT section.  The ordinary .got section is an argument,
710 // since we need to refer to the start.  We also create our own .got
711 // section just for PLT entries.
712
713 Output_data_plt_x86_64::Output_data_plt_x86_64(Layout* layout,
714                                                Output_data_got<64, false>* got,
715                                                Output_data_space* got_plt)
716   : Output_section_data(8), tlsdesc_rel_(NULL), got_(got), got_plt_(got_plt),
717     count_(0), tlsdesc_got_offset_(-1U)
718 {
719   this->rel_ = new Reloc_section(false);
720   layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
721                                   elfcpp::SHF_ALLOC, this->rel_,
722                                   ORDER_DYNAMIC_PLT_RELOCS, false);
723 }
724
725 void
726 Output_data_plt_x86_64::do_adjust_output_section(Output_section* os)
727 {
728   os->set_entsize(plt_entry_size);
729 }
730
731 // Add an entry to the PLT.
732
733 void
734 Output_data_plt_x86_64::add_entry(Symbol* gsym)
735 {
736   gold_assert(!gsym->has_plt_offset());
737
738   // Note that when setting the PLT offset we skip the initial
739   // reserved PLT entry.
740   gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
741
742   ++this->count_;
743
744   section_offset_type got_offset = this->got_plt_->current_data_size();
745
746   // Every PLT entry needs a GOT entry which points back to the PLT
747   // entry (this will be changed by the dynamic linker, normally
748   // lazily when the function is called).
749   this->got_plt_->set_current_data_size(got_offset + 8);
750
751   // Every PLT entry needs a reloc.
752   gsym->set_needs_dynsym_entry();
753   this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_,
754                          got_offset, 0);
755
756   // Note that we don't need to save the symbol.  The contents of the
757   // PLT are independent of which symbols are used.  The symbols only
758   // appear in the relocations.
759 }
760
761 // Return where the TLSDESC relocations should go, creating it if
762 // necessary.  These follow the JUMP_SLOT relocations.
763
764 Output_data_plt_x86_64::Reloc_section*
765 Output_data_plt_x86_64::rela_tlsdesc(Layout* layout)
766 {
767   if (this->tlsdesc_rel_ == NULL)
768     {
769       this->tlsdesc_rel_ = new Reloc_section(false);
770       layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA,
771                                       elfcpp::SHF_ALLOC, this->tlsdesc_rel_,
772                                       ORDER_DYNAMIC_PLT_RELOCS, false);
773       gold_assert(this->tlsdesc_rel_->output_section() ==
774                   this->rel_->output_section());
775     }
776   return this->tlsdesc_rel_;
777 }
778
779 // Set the final size.
780 void
781 Output_data_plt_x86_64::set_final_data_size()
782 {
783   unsigned int count = this->count_;
784   if (this->has_tlsdesc_entry())
785     ++count;
786   this->set_data_size((count + 1) * plt_entry_size);
787 }
788
789 // The first entry in the PLT for an executable.
790
791 unsigned char Output_data_plt_x86_64::first_plt_entry[plt_entry_size] =
792 {
793   // From AMD64 ABI Draft 0.98, page 76
794   0xff, 0x35,   // pushq contents of memory address
795   0, 0, 0, 0,   // replaced with address of .got + 8
796   0xff, 0x25,   // jmp indirect
797   0, 0, 0, 0,   // replaced with address of .got + 16
798   0x90, 0x90, 0x90, 0x90   // noop (x4)
799 };
800
801 // Subsequent entries in the PLT for an executable.
802
803 unsigned char Output_data_plt_x86_64::plt_entry[plt_entry_size] =
804 {
805   // From AMD64 ABI Draft 0.98, page 76
806   0xff, 0x25,   // jmpq indirect
807   0, 0, 0, 0,   // replaced with address of symbol in .got
808   0x68,         // pushq immediate
809   0, 0, 0, 0,   // replaced with offset into relocation table
810   0xe9,         // jmpq relative
811   0, 0, 0, 0    // replaced with offset to start of .plt
812 };
813
814 // The reserved TLSDESC entry in the PLT for an executable.
815
816 unsigned char Output_data_plt_x86_64::tlsdesc_plt_entry[plt_entry_size] =
817 {
818   // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32
819   // and AMD64/EM64T", Version 0.9.4 (2005-10-10).
820   0xff, 0x35,   // pushq x(%rip)
821   0, 0, 0, 0,   // replaced with address of linkmap GOT entry (at PLTGOT + 8)
822   0xff, 0x25,   // jmpq *y(%rip)
823   0, 0, 0, 0,   // replaced with offset of reserved TLSDESC_GOT entry
824   0x0f, 0x1f,   // nop
825   0x40, 0
826 };
827
828 // Write out the PLT.  This uses the hand-coded instructions above,
829 // and adjusts them as needed.  This is specified by the AMD64 ABI.
830
831 void
832 Output_data_plt_x86_64::do_write(Output_file* of)
833 {
834   const off_t offset = this->offset();
835   const section_size_type oview_size =
836     convert_to_section_size_type(this->data_size());
837   unsigned char* const oview = of->get_output_view(offset, oview_size);
838
839   const off_t got_file_offset = this->got_plt_->offset();
840   const section_size_type got_size =
841     convert_to_section_size_type(this->got_plt_->data_size());
842   unsigned char* const got_view = of->get_output_view(got_file_offset,
843                                                       got_size);
844
845   unsigned char* pov = oview;
846
847   // The base address of the .plt section.
848   elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address();
849   // The base address of the .got section.
850   elfcpp::Elf_types<64>::Elf_Addr got_base = this->got_->address();
851   // The base address of the PLT portion of the .got section,
852   // which is where the GOT pointer will point, and where the
853   // three reserved GOT entries are located.
854   elfcpp::Elf_types<64>::Elf_Addr got_address = this->got_plt_->address();
855
856   memcpy(pov, first_plt_entry, plt_entry_size);
857   // We do a jmp relative to the PC at the end of this instruction.
858   elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
859                                               (got_address + 8
860                                                - (plt_address + 6)));
861   elfcpp::Swap<32, false>::writeval(pov + 8,
862                                     (got_address + 16
863                                      - (plt_address + 12)));
864   pov += plt_entry_size;
865
866   unsigned char* got_pov = got_view;
867
868   memset(got_pov, 0, 24);
869   got_pov += 24;
870
871   unsigned int plt_offset = plt_entry_size;
872   unsigned int got_offset = 24;
873   const unsigned int count = this->count_;
874   for (unsigned int plt_index = 0;
875        plt_index < count;
876        ++plt_index,
877          pov += plt_entry_size,
878          got_pov += 8,
879          plt_offset += plt_entry_size,
880          got_offset += 8)
881     {
882       // Set and adjust the PLT entry itself.
883       memcpy(pov, plt_entry, plt_entry_size);
884       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
885                                                   (got_address + got_offset
886                                                    - (plt_address + plt_offset
887                                                       + 6)));
888
889       elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index);
890       elfcpp::Swap<32, false>::writeval(pov + 12,
891                                         - (plt_offset + plt_entry_size));
892
893       // Set the entry in the GOT.
894       elfcpp::Swap<64, false>::writeval(got_pov, plt_address + plt_offset + 6);
895     }
896
897   if (this->has_tlsdesc_entry())
898     {
899       // Set and adjust the reserved TLSDESC PLT entry.
900       unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset();
901       memcpy(pov, tlsdesc_plt_entry, plt_entry_size);
902       elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
903                                                   (got_address + 8
904                                                    - (plt_address + plt_offset
905                                                       + 6)));
906       elfcpp::Swap_unaligned<32, false>::writeval(pov + 8,
907                                                   (got_base
908                                                    + tlsdesc_got_offset
909                                                    - (plt_address + plt_offset
910                                                       + 12)));
911       pov += plt_entry_size;
912     }
913
914   gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
915   gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
916
917   of->write_output_view(offset, oview_size, oview);
918   of->write_output_view(got_file_offset, got_size, got_view);
919 }
920
921 // Create the PLT section.
922
923 void
924 Target_x86_64::make_plt_section(Symbol_table* symtab, Layout* layout)
925 {
926   if (this->plt_ == NULL)
927     {
928       // Create the GOT sections first.
929       this->got_section(symtab, layout);
930
931       this->plt_ = new Output_data_plt_x86_64(layout, this->got_,
932                                               this->got_plt_);
933       layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
934                                       (elfcpp::SHF_ALLOC
935                                        | elfcpp::SHF_EXECINSTR),
936                                       this->plt_, ORDER_PLT, false);
937     }
938 }
939
940 // Return the section for TLSDESC relocations.
941
942 Target_x86_64::Reloc_section*
943 Target_x86_64::rela_tlsdesc_section(Layout* layout) const
944 {
945   return this->plt_section()->rela_tlsdesc(layout);
946 }
947
948 // Create a PLT entry for a global symbol.
949
950 void
951 Target_x86_64::make_plt_entry(Symbol_table* symtab, Layout* layout,
952                               Symbol* gsym)
953 {
954   if (gsym->has_plt_offset())
955     return;
956
957   if (this->plt_ == NULL)
958     this->make_plt_section(symtab, layout);
959
960   this->plt_->add_entry(gsym);
961 }
962
963 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
964
965 void
966 Target_x86_64::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
967 {
968   if (this->tls_base_symbol_defined_)
969     return;
970
971   Output_segment* tls_segment = layout->tls_segment();
972   if (tls_segment != NULL)
973     {
974       bool is_exec = parameters->options().output_is_executable();
975       symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
976                                        Symbol_table::PREDEFINED,
977                                        tls_segment, 0, 0,
978                                        elfcpp::STT_TLS,
979                                        elfcpp::STB_LOCAL,
980                                        elfcpp::STV_HIDDEN, 0,
981                                        (is_exec
982                                         ? Symbol::SEGMENT_END
983                                         : Symbol::SEGMENT_START),
984                                        true);
985     }
986   this->tls_base_symbol_defined_ = true;
987 }
988
989 // Create the reserved PLT and GOT entries for the TLS descriptor resolver.
990
991 void
992 Target_x86_64::reserve_tlsdesc_entries(Symbol_table* symtab,
993                                              Layout* layout)
994 {
995   if (this->plt_ == NULL)
996     this->make_plt_section(symtab, layout);
997
998   if (!this->plt_->has_tlsdesc_entry())
999     {
1000       // Allocate the TLSDESC_GOT entry.
1001       Output_data_got<64, false>* got = this->got_section(symtab, layout);
1002       unsigned int got_offset = got->add_constant(0);
1003
1004       // Allocate the TLSDESC_PLT entry.
1005       this->plt_->reserve_tlsdesc_entry(got_offset);
1006     }
1007 }
1008
1009 // Create a GOT entry for the TLS module index.
1010
1011 unsigned int
1012 Target_x86_64::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1013                                    Sized_relobj<64, false>* object)
1014 {
1015   if (this->got_mod_index_offset_ == -1U)
1016     {
1017       gold_assert(symtab != NULL && layout != NULL && object != NULL);
1018       Reloc_section* rela_dyn = this->rela_dyn_section(layout);
1019       Output_data_got<64, false>* got = this->got_section(symtab, layout);
1020       unsigned int got_offset = got->add_constant(0);
1021       rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got,
1022                           got_offset, 0);
1023       got->add_constant(0);
1024       this->got_mod_index_offset_ = got_offset;
1025     }
1026   return this->got_mod_index_offset_;
1027 }
1028
1029 // Optimize the TLS relocation type based on what we know about the
1030 // symbol.  IS_FINAL is true if the final address of this symbol is
1031 // known at link time.
1032
1033 tls::Tls_optimization
1034 Target_x86_64::optimize_tls_reloc(bool is_final, int r_type)
1035 {
1036   // If we are generating a shared library, then we can't do anything
1037   // in the linker.
1038   if (parameters->options().shared())
1039     return tls::TLSOPT_NONE;
1040
1041   switch (r_type)
1042     {
1043     case elfcpp::R_X86_64_TLSGD:
1044     case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1045     case elfcpp::R_X86_64_TLSDESC_CALL:
1046       // These are General-Dynamic which permits fully general TLS
1047       // access.  Since we know that we are generating an executable,
1048       // we can convert this to Initial-Exec.  If we also know that
1049       // this is a local symbol, we can further switch to Local-Exec.
1050       if (is_final)
1051         return tls::TLSOPT_TO_LE;
1052       return tls::TLSOPT_TO_IE;
1053
1054     case elfcpp::R_X86_64_TLSLD:
1055       // This is Local-Dynamic, which refers to a local symbol in the
1056       // dynamic TLS block.  Since we know that we generating an
1057       // executable, we can switch to Local-Exec.
1058       return tls::TLSOPT_TO_LE;
1059
1060     case elfcpp::R_X86_64_DTPOFF32:
1061     case elfcpp::R_X86_64_DTPOFF64:
1062       // Another Local-Dynamic reloc.
1063       return tls::TLSOPT_TO_LE;
1064
1065     case elfcpp::R_X86_64_GOTTPOFF:
1066       // These are Initial-Exec relocs which get the thread offset
1067       // from the GOT.  If we know that we are linking against the
1068       // local symbol, we can switch to Local-Exec, which links the
1069       // thread offset into the instruction.
1070       if (is_final)
1071         return tls::TLSOPT_TO_LE;
1072       return tls::TLSOPT_NONE;
1073
1074     case elfcpp::R_X86_64_TPOFF32:
1075       // When we already have Local-Exec, there is nothing further we
1076       // can do.
1077       return tls::TLSOPT_NONE;
1078
1079     default:
1080       gold_unreachable();
1081     }
1082 }
1083
1084 // Report an unsupported relocation against a local symbol.
1085
1086 void
1087 Target_x86_64::Scan::unsupported_reloc_local(Sized_relobj<64, false>* object,
1088                                              unsigned int r_type)
1089 {
1090   gold_error(_("%s: unsupported reloc %u against local symbol"),
1091              object->name().c_str(), r_type);
1092 }
1093
1094 // We are about to emit a dynamic relocation of type R_TYPE.  If the
1095 // dynamic linker does not support it, issue an error.  The GNU linker
1096 // only issues a non-PIC error for an allocated read-only section.
1097 // Here we know the section is allocated, but we don't know that it is
1098 // read-only.  But we check for all the relocation types which the
1099 // glibc dynamic linker supports, so it seems appropriate to issue an
1100 // error even if the section is not read-only.
1101
1102 void
1103 Target_x86_64::Scan::check_non_pic(Relobj* object, unsigned int r_type)
1104 {
1105   switch (r_type)
1106     {
1107       // These are the relocation types supported by glibc for x86_64.
1108     case elfcpp::R_X86_64_RELATIVE:
1109     case elfcpp::R_X86_64_GLOB_DAT:
1110     case elfcpp::R_X86_64_JUMP_SLOT:
1111     case elfcpp::R_X86_64_DTPMOD64:
1112     case elfcpp::R_X86_64_DTPOFF64:
1113     case elfcpp::R_X86_64_TPOFF64:
1114     case elfcpp::R_X86_64_64:
1115     case elfcpp::R_X86_64_32:
1116     case elfcpp::R_X86_64_PC32:
1117     case elfcpp::R_X86_64_COPY:
1118       return;
1119
1120     default:
1121       // This prevents us from issuing more than one error per reloc
1122       // section.  But we can still wind up issuing more than one
1123       // error per object file.
1124       if (this->issued_non_pic_error_)
1125         return;
1126       gold_assert(parameters->options().output_is_position_independent());
1127       object->error(_("requires unsupported dynamic reloc; "
1128                       "recompile with -fPIC"));
1129       this->issued_non_pic_error_ = true;
1130       return;
1131
1132     case elfcpp::R_X86_64_NONE:
1133       gold_unreachable();
1134     }
1135 }
1136
1137 // Scan a relocation for a local symbol.
1138
1139 inline void
1140 Target_x86_64::Scan::local(Symbol_table* symtab,
1141                            Layout* layout,
1142                            Target_x86_64* target,
1143                            Sized_relobj<64, false>* object,
1144                            unsigned int data_shndx,
1145                            Output_section* output_section,
1146                            const elfcpp::Rela<64, false>& reloc,
1147                            unsigned int r_type,
1148                            const elfcpp::Sym<64, false>& lsym)
1149 {
1150   switch (r_type)
1151     {
1152     case elfcpp::R_X86_64_NONE:
1153     case elfcpp::R_X86_64_GNU_VTINHERIT:
1154     case elfcpp::R_X86_64_GNU_VTENTRY:
1155       break;
1156
1157     case elfcpp::R_X86_64_64:
1158       // If building a shared library (or a position-independent
1159       // executable), we need to create a dynamic relocation for this
1160       // location.  The relocation applied at link time will apply the
1161       // link-time value, so we flag the location with an
1162       // R_X86_64_RELATIVE relocation so the dynamic loader can
1163       // relocate it easily.
1164       if (parameters->options().output_is_position_independent())
1165         {
1166           unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1167           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1168           rela_dyn->add_local_relative(object, r_sym,
1169                                        elfcpp::R_X86_64_RELATIVE,
1170                                        output_section, data_shndx,
1171                                        reloc.get_r_offset(),
1172                                        reloc.get_r_addend());
1173         }
1174       break;
1175
1176     case elfcpp::R_X86_64_32:
1177     case elfcpp::R_X86_64_32S:
1178     case elfcpp::R_X86_64_16:
1179     case elfcpp::R_X86_64_8:
1180       // If building a shared library (or a position-independent
1181       // executable), we need to create a dynamic relocation for this
1182       // location.  We can't use an R_X86_64_RELATIVE relocation
1183       // because that is always a 64-bit relocation.
1184       if (parameters->options().output_is_position_independent())
1185         {
1186           this->check_non_pic(object, r_type);
1187
1188           Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1189           unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1190           if (lsym.get_st_type() != elfcpp::STT_SECTION)
1191             rela_dyn->add_local(object, r_sym, r_type, output_section,
1192                                 data_shndx, reloc.get_r_offset(),
1193                                 reloc.get_r_addend());
1194           else
1195             {
1196               gold_assert(lsym.get_st_value() == 0);
1197               unsigned int shndx = lsym.get_st_shndx();
1198               bool is_ordinary;
1199               shndx = object->adjust_sym_shndx(r_sym, shndx,
1200                                                &is_ordinary);
1201               if (!is_ordinary)
1202                 object->error(_("section symbol %u has bad shndx %u"),
1203                               r_sym, shndx);
1204               else
1205                 rela_dyn->add_local_section(object, shndx,
1206                                             r_type, output_section,
1207                                             data_shndx, reloc.get_r_offset(),
1208                                             reloc.get_r_addend());
1209             }
1210         }
1211       break;
1212
1213     case elfcpp::R_X86_64_PC64:
1214     case elfcpp::R_X86_64_PC32:
1215     case elfcpp::R_X86_64_PC16:
1216     case elfcpp::R_X86_64_PC8:
1217       break;
1218
1219     case elfcpp::R_X86_64_PLT32:
1220       // Since we know this is a local symbol, we can handle this as a
1221       // PC32 reloc.
1222       break;
1223
1224     case elfcpp::R_X86_64_GOTPC32:
1225     case elfcpp::R_X86_64_GOTOFF64:
1226     case elfcpp::R_X86_64_GOTPC64:
1227     case elfcpp::R_X86_64_PLTOFF64:
1228       // We need a GOT section.
1229       target->got_section(symtab, layout);
1230       // For PLTOFF64, we'd normally want a PLT section, but since we
1231       // know this is a local symbol, no PLT is needed.
1232       break;
1233
1234     case elfcpp::R_X86_64_GOT64:
1235     case elfcpp::R_X86_64_GOT32:
1236     case elfcpp::R_X86_64_GOTPCREL64:
1237     case elfcpp::R_X86_64_GOTPCREL:
1238     case elfcpp::R_X86_64_GOTPLT64:
1239       {
1240         // The symbol requires a GOT entry.
1241         Output_data_got<64, false>* got = target->got_section(symtab, layout);
1242         unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1243         if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1244           {
1245             // If we are generating a shared object, we need to add a
1246             // dynamic relocation for this symbol's GOT entry.
1247             if (parameters->options().output_is_position_independent())
1248               {
1249                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1250                 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1251                 if (r_type != elfcpp::R_X86_64_GOT32)
1252                   rela_dyn->add_local_relative(
1253                       object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1254                       object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1255                 else
1256                   {
1257                     this->check_non_pic(object, r_type);
1258
1259                     gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1260                     rela_dyn->add_local(
1261                         object, r_sym, r_type, got,
1262                         object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
1263                   }
1264               }
1265           }
1266         // For GOTPLT64, we'd normally want a PLT section, but since
1267         // we know this is a local symbol, no PLT is needed.
1268       }
1269       break;
1270
1271     case elfcpp::R_X86_64_COPY:
1272     case elfcpp::R_X86_64_GLOB_DAT:
1273     case elfcpp::R_X86_64_JUMP_SLOT:
1274     case elfcpp::R_X86_64_RELATIVE:
1275       // These are outstanding tls relocs, which are unexpected when linking
1276     case elfcpp::R_X86_64_TPOFF64:
1277     case elfcpp::R_X86_64_DTPMOD64:
1278     case elfcpp::R_X86_64_TLSDESC:
1279       gold_error(_("%s: unexpected reloc %u in object file"),
1280                  object->name().c_str(), r_type);
1281       break;
1282
1283       // These are initial tls relocs, which are expected when linking
1284     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1285     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1286     case elfcpp::R_X86_64_TLSDESC_CALL:
1287     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1288     case elfcpp::R_X86_64_DTPOFF32:
1289     case elfcpp::R_X86_64_DTPOFF64:
1290     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1291     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1292       {
1293         bool output_is_shared = parameters->options().shared();
1294         const tls::Tls_optimization optimized_type
1295             = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
1296         switch (r_type)
1297           {
1298           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
1299             if (optimized_type == tls::TLSOPT_NONE)
1300               {
1301                 // Create a pair of GOT entries for the module index and
1302                 // dtv-relative offset.
1303                 Output_data_got<64, false>* got
1304                     = target->got_section(symtab, layout);
1305                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1306                 unsigned int shndx = lsym.get_st_shndx();
1307                 bool is_ordinary;
1308                 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1309                 if (!is_ordinary)
1310                   object->error(_("local symbol %u has bad shndx %u"),
1311                               r_sym, shndx);
1312                 else
1313                   got->add_local_pair_with_rela(object, r_sym,
1314                                                 shndx,
1315                                                 GOT_TYPE_TLS_PAIR,
1316                                                 target->rela_dyn_section(layout),
1317                                                 elfcpp::R_X86_64_DTPMOD64, 0);
1318               }
1319             else if (optimized_type != tls::TLSOPT_TO_LE)
1320               unsupported_reloc_local(object, r_type);
1321             break;
1322
1323           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1324             target->define_tls_base_symbol(symtab, layout);
1325             if (optimized_type == tls::TLSOPT_NONE)
1326               {
1327                 // Create reserved PLT and GOT entries for the resolver.
1328                 target->reserve_tlsdesc_entries(symtab, layout);
1329
1330                 // Generate a double GOT entry with an
1331                 // R_X86_64_TLSDESC reloc.  The R_X86_64_TLSDESC reloc
1332                 // is resolved lazily, so the GOT entry needs to be in
1333                 // an area in .got.plt, not .got.  Call got_section to
1334                 // make sure the section has been created.
1335                 target->got_section(symtab, layout);
1336                 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1337                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1338                 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1339                   {
1340                     unsigned int got_offset = got->add_constant(0);
1341                     got->add_constant(0);
1342                     object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1343                                                  got_offset);
1344                     Reloc_section* rt = target->rela_tlsdesc_section(layout);
1345                     // We store the arguments we need in a vector, and
1346                     // use the index into the vector as the parameter
1347                     // to pass to the target specific routines.
1348                     uintptr_t intarg = target->add_tlsdesc_info(object, r_sym);
1349                     void* arg = reinterpret_cast<void*>(intarg);
1350                     rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg,
1351                                             got, got_offset, 0);
1352                   }
1353               }
1354             else if (optimized_type != tls::TLSOPT_TO_LE)
1355               unsupported_reloc_local(object, r_type);
1356             break;
1357
1358           case elfcpp::R_X86_64_TLSDESC_CALL:
1359             break;
1360
1361           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
1362             if (optimized_type == tls::TLSOPT_NONE)
1363               {
1364                 // Create a GOT entry for the module index.
1365                 target->got_mod_index_entry(symtab, layout, object);
1366               }
1367             else if (optimized_type != tls::TLSOPT_TO_LE)
1368               unsupported_reloc_local(object, r_type);
1369             break;
1370
1371           case elfcpp::R_X86_64_DTPOFF32:
1372           case elfcpp::R_X86_64_DTPOFF64:
1373             break;
1374
1375           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
1376             layout->set_has_static_tls();
1377             if (optimized_type == tls::TLSOPT_NONE)
1378               {
1379                 // Create a GOT entry for the tp-relative offset.
1380                 Output_data_got<64, false>* got
1381                     = target->got_section(symtab, layout);
1382                 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
1383                 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
1384                                          target->rela_dyn_section(layout),
1385                                          elfcpp::R_X86_64_TPOFF64);
1386               }
1387             else if (optimized_type != tls::TLSOPT_TO_LE)
1388               unsupported_reloc_local(object, r_type);
1389             break;
1390
1391           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
1392             layout->set_has_static_tls();
1393             if (output_is_shared)
1394               unsupported_reloc_local(object, r_type);
1395             break;
1396
1397           default:
1398             gold_unreachable();
1399           }
1400       }
1401       break;
1402
1403     case elfcpp::R_X86_64_SIZE32:
1404     case elfcpp::R_X86_64_SIZE64:
1405     default:
1406       gold_error(_("%s: unsupported reloc %u against local symbol"),
1407                  object->name().c_str(), r_type);
1408       break;
1409     }
1410 }
1411
1412
1413 // Report an unsupported relocation against a global symbol.
1414
1415 void
1416 Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1417                                               unsigned int r_type,
1418                                               Symbol* gsym)
1419 {
1420   gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1421              object->name().c_str(), r_type, gsym->demangled_name().c_str());
1422 }
1423
1424 // Returns true if this relocation type could be that of a function pointer.
1425 inline bool
1426 Target_x86_64::Scan::possible_function_pointer_reloc(unsigned int r_type)
1427 {
1428   switch (r_type)
1429     {
1430     case elfcpp::R_X86_64_64:
1431     case elfcpp::R_X86_64_32:
1432     case elfcpp::R_X86_64_32S:
1433     case elfcpp::R_X86_64_16:
1434     case elfcpp::R_X86_64_8:
1435     case elfcpp::R_X86_64_GOT64:
1436     case elfcpp::R_X86_64_GOT32:
1437     case elfcpp::R_X86_64_GOTPCREL64:
1438     case elfcpp::R_X86_64_GOTPCREL:
1439     case elfcpp::R_X86_64_GOTPLT64:
1440       {
1441         return true;
1442       }
1443     }
1444   return false;
1445 }
1446
1447 // For safe ICF, scan a relocation for a local symbol to check if it
1448 // corresponds to a function pointer being taken.  In that case mark
1449 // the function whose pointer was taken as not foldable.
1450
1451 inline bool
1452 Target_x86_64::Scan::local_reloc_may_be_function_pointer(
1453   Symbol_table* ,
1454   Layout* ,
1455   Target_x86_64* ,
1456   Sized_relobj<64, false>* ,
1457   unsigned int ,
1458   Output_section* ,
1459   const elfcpp::Rela<64, false>& ,
1460   unsigned int r_type,
1461   const elfcpp::Sym<64, false>&)
1462 {
1463   // When building a shared library, do not fold any local symbols as it is
1464   // not possible to distinguish pointer taken versus a call by looking at
1465   // the relocation types.
1466   return (parameters->options().shared()
1467           || possible_function_pointer_reloc(r_type));
1468 }
1469
1470 // For safe ICF, scan a relocation for a global symbol to check if it
1471 // corresponds to a function pointer being taken.  In that case mark
1472 // the function whose pointer was taken as not foldable.
1473
1474 inline bool
1475 Target_x86_64::Scan::global_reloc_may_be_function_pointer(
1476   Symbol_table*,
1477   Layout* ,
1478   Target_x86_64* ,
1479   Sized_relobj<64, false>* ,
1480   unsigned int ,
1481   Output_section* ,
1482   const elfcpp::Rela<64, false>& ,
1483   unsigned int r_type,
1484   Symbol* gsym)
1485 {
1486   // When building a shared library, do not fold symbols whose visibility
1487   // is hidden, internal or protected.
1488   return ((parameters->options().shared()
1489            && (gsym->visibility() == elfcpp::STV_INTERNAL
1490                || gsym->visibility() == elfcpp::STV_PROTECTED
1491                || gsym->visibility() == elfcpp::STV_HIDDEN))
1492           || possible_function_pointer_reloc(r_type));
1493 }
1494
1495 // Scan a relocation for a global symbol.
1496
1497 inline void
1498 Target_x86_64::Scan::global(Symbol_table* symtab,
1499                             Layout* layout,
1500                             Target_x86_64* target,
1501                             Sized_relobj<64, false>* object,
1502                             unsigned int data_shndx,
1503                             Output_section* output_section,
1504                             const elfcpp::Rela<64, false>& reloc,
1505                             unsigned int r_type,
1506                             Symbol* gsym)
1507 {
1508   switch (r_type)
1509     {
1510     case elfcpp::R_X86_64_NONE:
1511     case elfcpp::R_X86_64_GNU_VTINHERIT:
1512     case elfcpp::R_X86_64_GNU_VTENTRY:
1513       break;
1514
1515     case elfcpp::R_X86_64_64:
1516     case elfcpp::R_X86_64_32:
1517     case elfcpp::R_X86_64_32S:
1518     case elfcpp::R_X86_64_16:
1519     case elfcpp::R_X86_64_8:
1520       {
1521         // Make a PLT entry if necessary.
1522         if (gsym->needs_plt_entry())
1523           {
1524             target->make_plt_entry(symtab, layout, gsym);
1525             // Since this is not a PC-relative relocation, we may be
1526             // taking the address of a function. In that case we need to
1527             // set the entry in the dynamic symbol table to the address of
1528             // the PLT entry.
1529             if (gsym->is_from_dynobj() && !parameters->options().shared())
1530               gsym->set_needs_dynsym_value();
1531           }
1532         // Make a dynamic relocation if necessary.
1533         if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1534           {
1535             if (gsym->may_need_copy_reloc())
1536               {
1537                 target->copy_reloc(symtab, layout, object,
1538                                    data_shndx, output_section, gsym, reloc);
1539               }
1540             else if (r_type == elfcpp::R_X86_64_64
1541                      && gsym->can_use_relative_reloc(false))
1542               {
1543                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1544                 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1545                                               output_section, object,
1546                                               data_shndx, reloc.get_r_offset(),
1547                                               reloc.get_r_addend());
1548               }
1549             else
1550               {
1551                 this->check_non_pic(object, r_type);
1552                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1553                 rela_dyn->add_global(gsym, r_type, output_section, object,
1554                                      data_shndx, reloc.get_r_offset(),
1555                                      reloc.get_r_addend());
1556               }
1557           }
1558       }
1559       break;
1560
1561     case elfcpp::R_X86_64_PC64:
1562     case elfcpp::R_X86_64_PC32:
1563     case elfcpp::R_X86_64_PC16:
1564     case elfcpp::R_X86_64_PC8:
1565       {
1566         // Make a PLT entry if necessary.
1567         if (gsym->needs_plt_entry())
1568           target->make_plt_entry(symtab, layout, gsym);
1569         // Make a dynamic relocation if necessary.
1570         int flags = Symbol::NON_PIC_REF;
1571         if (gsym->is_func())
1572           flags |= Symbol::FUNCTION_CALL;
1573         if (gsym->needs_dynamic_reloc(flags))
1574           {
1575             if (gsym->may_need_copy_reloc())
1576               {
1577                 target->copy_reloc(symtab, layout, object,
1578                                    data_shndx, output_section, gsym, reloc);
1579               }
1580             else
1581               {
1582                 this->check_non_pic(object, r_type);
1583                 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1584                 rela_dyn->add_global(gsym, r_type, output_section, object,
1585                                      data_shndx, reloc.get_r_offset(),
1586                                      reloc.get_r_addend());
1587               }
1588           }
1589       }
1590       break;
1591
1592     case elfcpp::R_X86_64_GOT64:
1593     case elfcpp::R_X86_64_GOT32:
1594     case elfcpp::R_X86_64_GOTPCREL64:
1595     case elfcpp::R_X86_64_GOTPCREL:
1596     case elfcpp::R_X86_64_GOTPLT64:
1597       {
1598         // The symbol requires a GOT entry.
1599         Output_data_got<64, false>* got = target->got_section(symtab, layout);
1600         if (gsym->final_value_is_known())
1601           got->add_global(gsym, GOT_TYPE_STANDARD);
1602         else
1603           {
1604             // If this symbol is not fully resolved, we need to add a
1605             // dynamic relocation for it.
1606             Reloc_section* rela_dyn = target->rela_dyn_section(layout);
1607             if (gsym->is_from_dynobj()
1608                 || gsym->is_undefined()
1609                 || gsym->is_preemptible())
1610               got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
1611                                         elfcpp::R_X86_64_GLOB_DAT);
1612             else
1613               {
1614                 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1615                   rela_dyn->add_global_relative(
1616                       gsym, elfcpp::R_X86_64_RELATIVE, got,
1617                       gsym->got_offset(GOT_TYPE_STANDARD), 0);
1618               }
1619           }
1620         // For GOTPLT64, we also need a PLT entry (but only if the
1621         // symbol is not fully resolved).
1622         if (r_type == elfcpp::R_X86_64_GOTPLT64
1623             && !gsym->final_value_is_known())
1624           target->make_plt_entry(symtab, layout, gsym);
1625       }
1626       break;
1627
1628     case elfcpp::R_X86_64_PLT32:
1629       // If the symbol is fully resolved, this is just a PC32 reloc.
1630       // Otherwise we need a PLT entry.
1631       if (gsym->final_value_is_known())
1632         break;
1633       // If building a shared library, we can also skip the PLT entry
1634       // if the symbol is defined in the output file and is protected
1635       // or hidden.
1636       if (gsym->is_defined()
1637           && !gsym->is_from_dynobj()
1638           && !gsym->is_preemptible())
1639         break;
1640       target->make_plt_entry(symtab, layout, gsym);
1641       break;
1642
1643     case elfcpp::R_X86_64_GOTPC32:
1644     case elfcpp::R_X86_64_GOTOFF64:
1645     case elfcpp::R_X86_64_GOTPC64:
1646     case elfcpp::R_X86_64_PLTOFF64:
1647       // We need a GOT section.
1648       target->got_section(symtab, layout);
1649       // For PLTOFF64, we also need a PLT entry (but only if the
1650       // symbol is not fully resolved).
1651       if (r_type == elfcpp::R_X86_64_PLTOFF64
1652           && !gsym->final_value_is_known())
1653         target->make_plt_entry(symtab, layout, gsym);
1654       break;
1655
1656     case elfcpp::R_X86_64_COPY:
1657     case elfcpp::R_X86_64_GLOB_DAT:
1658     case elfcpp::R_X86_64_JUMP_SLOT:
1659     case elfcpp::R_X86_64_RELATIVE:
1660       // These are outstanding tls relocs, which are unexpected when linking
1661     case elfcpp::R_X86_64_TPOFF64:
1662     case elfcpp::R_X86_64_DTPMOD64:
1663     case elfcpp::R_X86_64_TLSDESC:
1664       gold_error(_("%s: unexpected reloc %u in object file"),
1665                  object->name().c_str(), r_type);
1666       break;
1667
1668       // These are initial tls relocs, which are expected for global()
1669     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
1670     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
1671     case elfcpp::R_X86_64_TLSDESC_CALL:
1672     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
1673     case elfcpp::R_X86_64_DTPOFF32:
1674     case elfcpp::R_X86_64_DTPOFF64:
1675     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
1676     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
1677       {
1678         const bool is_final = gsym->final_value_is_known();
1679         const tls::Tls_optimization optimized_type
1680             = Target_x86_64::optimize_tls_reloc(is_final, r_type);
1681         switch (r_type)
1682           {
1683           case elfcpp::R_X86_64_TLSGD:       // General-dynamic
1684             if (optimized_type == tls::TLSOPT_NONE)
1685               {
1686                 // Create a pair of GOT entries for the module index and
1687                 // dtv-relative offset.
1688                 Output_data_got<64, false>* got
1689                     = target->got_section(symtab, layout);
1690                 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1691                                                target->rela_dyn_section(layout),
1692                                                elfcpp::R_X86_64_DTPMOD64,
1693                                                elfcpp::R_X86_64_DTPOFF64);
1694               }
1695             else if (optimized_type == tls::TLSOPT_TO_IE)
1696               {
1697                 // Create a GOT entry for the tp-relative offset.
1698                 Output_data_got<64, false>* got
1699                     = target->got_section(symtab, layout);
1700                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1701                                           target->rela_dyn_section(layout),
1702                                           elfcpp::R_X86_64_TPOFF64);
1703               }
1704             else if (optimized_type != tls::TLSOPT_TO_LE)
1705               unsupported_reloc_global(object, r_type, gsym);
1706             break;
1707
1708           case elfcpp::R_X86_64_GOTPC32_TLSDESC:
1709             target->define_tls_base_symbol(symtab, layout);
1710             if (optimized_type == tls::TLSOPT_NONE)
1711               {
1712                 // Create reserved PLT and GOT entries for the resolver.
1713                 target->reserve_tlsdesc_entries(symtab, layout);
1714
1715                 // Create a double GOT entry with an R_X86_64_TLSDESC
1716                 // reloc.  The R_X86_64_TLSDESC reloc is resolved
1717                 // lazily, so the GOT entry needs to be in an area in
1718                 // .got.plt, not .got.  Call got_section to make sure
1719                 // the section has been created.
1720                 target->got_section(symtab, layout);
1721                 Output_data_got<64, false>* got = target->got_tlsdesc_section();
1722                 Reloc_section *rt = target->rela_tlsdesc_section(layout);
1723                 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC, rt,
1724                                                elfcpp::R_X86_64_TLSDESC, 0);
1725               }
1726             else if (optimized_type == tls::TLSOPT_TO_IE)
1727               {
1728                 // Create a GOT entry for the tp-relative offset.
1729                 Output_data_got<64, false>* got
1730                     = target->got_section(symtab, layout);
1731                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1732                                           target->rela_dyn_section(layout),
1733                                           elfcpp::R_X86_64_TPOFF64);
1734               }
1735             else if (optimized_type != tls::TLSOPT_TO_LE)
1736               unsupported_reloc_global(object, r_type, gsym);
1737             break;
1738
1739           case elfcpp::R_X86_64_TLSDESC_CALL:
1740             break;
1741
1742           case elfcpp::R_X86_64_TLSLD:       // Local-dynamic
1743             if (optimized_type == tls::TLSOPT_NONE)
1744               {
1745                 // Create a GOT entry for the module index.
1746                 target->got_mod_index_entry(symtab, layout, object);
1747               }
1748             else if (optimized_type != tls::TLSOPT_TO_LE)
1749               unsupported_reloc_global(object, r_type, gsym);
1750             break;
1751
1752           case elfcpp::R_X86_64_DTPOFF32:
1753           case elfcpp::R_X86_64_DTPOFF64:
1754             break;
1755
1756           case elfcpp::R_X86_64_GOTTPOFF:    // Initial-exec
1757             layout->set_has_static_tls();
1758             if (optimized_type == tls::TLSOPT_NONE)
1759               {
1760                 // Create a GOT entry for the tp-relative offset.
1761                 Output_data_got<64, false>* got
1762                     = target->got_section(symtab, layout);
1763                 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1764                                           target->rela_dyn_section(layout),
1765                                           elfcpp::R_X86_64_TPOFF64);
1766               }
1767             else if (optimized_type != tls::TLSOPT_TO_LE)
1768               unsupported_reloc_global(object, r_type, gsym);
1769             break;
1770
1771           case elfcpp::R_X86_64_TPOFF32:     // Local-exec
1772             layout->set_has_static_tls();
1773             if (parameters->options().shared())
1774               unsupported_reloc_local(object, r_type);
1775             break;
1776
1777           default:
1778             gold_unreachable();
1779           }
1780       }
1781       break;
1782
1783     case elfcpp::R_X86_64_SIZE32:
1784     case elfcpp::R_X86_64_SIZE64:
1785     default:
1786       gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1787                  object->name().c_str(), r_type,
1788                  gsym->demangled_name().c_str());
1789       break;
1790     }
1791 }
1792
1793 void
1794 Target_x86_64::gc_process_relocs(Symbol_table* symtab,
1795                                  Layout* layout,
1796                                  Sized_relobj<64, false>* object,
1797                                  unsigned int data_shndx,
1798                                  unsigned int sh_type,
1799                                  const unsigned char* prelocs,
1800                                  size_t reloc_count,
1801                                  Output_section* output_section,
1802                                  bool needs_special_offset_handling,
1803                                  size_t local_symbol_count,
1804                                  const unsigned char* plocal_symbols)
1805 {
1806
1807   if (sh_type == elfcpp::SHT_REL)
1808     {
1809       return;
1810     }
1811
1812    gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1813                            Target_x86_64::Scan,
1814                            Target_x86_64::Relocatable_size_for_reloc>(
1815     symtab,
1816     layout,
1817     this,
1818     object,
1819     data_shndx,
1820     prelocs,
1821     reloc_count,
1822     output_section,
1823     needs_special_offset_handling,
1824     local_symbol_count,
1825     plocal_symbols);
1826  
1827 }
1828 // Scan relocations for a section.
1829
1830 void
1831 Target_x86_64::scan_relocs(Symbol_table* symtab,
1832                            Layout* layout,
1833                            Sized_relobj<64, false>* object,
1834                            unsigned int data_shndx,
1835                            unsigned int sh_type,
1836                            const unsigned char* prelocs,
1837                            size_t reloc_count,
1838                            Output_section* output_section,
1839                            bool needs_special_offset_handling,
1840                            size_t local_symbol_count,
1841                            const unsigned char* plocal_symbols)
1842 {
1843   if (sh_type == elfcpp::SHT_REL)
1844     {
1845       gold_error(_("%s: unsupported REL reloc section"),
1846                  object->name().c_str());
1847       return;
1848     }
1849
1850   gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1851       Target_x86_64::Scan>(
1852     symtab,
1853     layout,
1854     this,
1855     object,
1856     data_shndx,
1857     prelocs,
1858     reloc_count,
1859     output_section,
1860     needs_special_offset_handling,
1861     local_symbol_count,
1862     plocal_symbols);
1863 }
1864
1865 // Finalize the sections.
1866
1867 void
1868 Target_x86_64::do_finalize_sections(
1869     Layout* layout,
1870     const Input_objects*,
1871     Symbol_table* symtab)
1872 {
1873   const Reloc_section* rel_plt = (this->plt_ == NULL
1874                                   ? NULL
1875                                   : this->plt_->rela_plt());
1876   layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt,
1877                                   this->rela_dyn_, true, false);
1878                                   
1879   // Fill in some more dynamic tags.
1880   Output_data_dynamic* const odyn = layout->dynamic_data();
1881   if (odyn != NULL)
1882     {
1883       if (this->plt_ != NULL
1884           && this->plt_->output_section() != NULL
1885           && this->plt_->has_tlsdesc_entry())
1886         {
1887           unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1888           unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1889           this->got_->finalize_data_size();
1890           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1891                                         this->plt_, plt_offset);
1892           odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1893                                         this->got_, got_offset);
1894         }
1895     }
1896
1897   // Emit any relocs we saved in an attempt to avoid generating COPY
1898   // relocs.
1899   if (this->copy_relocs_.any_saved_relocs())
1900     this->copy_relocs_.emit(this->rela_dyn_section(layout));
1901
1902   // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1903   // the .got.plt section.
1904   Symbol* sym = this->global_offset_table_;
1905   if (sym != NULL)
1906     {
1907       uint64_t data_size = this->got_plt_->current_data_size();
1908       symtab->get_sized_symbol<64>(sym)->set_symsize(data_size);
1909     }
1910 }
1911
1912 // Perform a relocation.
1913
1914 inline bool
1915 Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1916                                   Target_x86_64* target,
1917                                   Output_section*,
1918                                   size_t relnum,
1919                                   const elfcpp::Rela<64, false>& rela,
1920                                   unsigned int r_type,
1921                                   const Sized_symbol<64>* gsym,
1922                                   const Symbol_value<64>* psymval,
1923                                   unsigned char* view,
1924                                   elfcpp::Elf_types<64>::Elf_Addr address,
1925                                   section_size_type view_size)
1926 {
1927   if (this->skip_call_tls_get_addr_)
1928     {
1929       if ((r_type != elfcpp::R_X86_64_PLT32
1930            && r_type != elfcpp::R_X86_64_PC32)
1931           || gsym == NULL
1932           || strcmp(gsym->name(), "__tls_get_addr") != 0)
1933         {
1934           gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1935                                  _("missing expected TLS relocation"));
1936         }
1937       else
1938         {
1939           this->skip_call_tls_get_addr_ = false;
1940           return false;
1941         }
1942     }
1943
1944   // Pick the value to use for symbols defined in shared objects.
1945   Symbol_value<64> symval;
1946   if (gsym != NULL
1947       && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
1948                               || r_type == elfcpp::R_X86_64_PC32
1949                               || r_type == elfcpp::R_X86_64_PC16
1950                               || r_type == elfcpp::R_X86_64_PC8))
1951     {
1952       symval.set_output_value(target->plt_section()->address()
1953                               + gsym->plt_offset());
1954       psymval = &symval;
1955     }
1956
1957   const Sized_relobj<64, false>* object = relinfo->object;
1958   const elfcpp::Elf_Xword addend = rela.get_r_addend();
1959
1960   // Get the GOT offset if needed.
1961   // The GOT pointer points to the end of the GOT section.
1962   // We need to subtract the size of the GOT section to get
1963   // the actual offset to use in the relocation.
1964   bool have_got_offset = false;
1965   unsigned int got_offset = 0;
1966   switch (r_type)
1967     {
1968     case elfcpp::R_X86_64_GOT32:
1969     case elfcpp::R_X86_64_GOT64:
1970     case elfcpp::R_X86_64_GOTPLT64:
1971     case elfcpp::R_X86_64_GOTPCREL:
1972     case elfcpp::R_X86_64_GOTPCREL64:
1973       if (gsym != NULL)
1974         {
1975           gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1976           got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
1977         }
1978       else
1979         {
1980           unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
1981           gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1982           got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1983                         - target->got_size());
1984         }
1985       have_got_offset = true;
1986       break;
1987
1988     default:
1989       break;
1990     }
1991
1992   switch (r_type)
1993     {
1994     case elfcpp::R_X86_64_NONE:
1995     case elfcpp::R_X86_64_GNU_VTINHERIT:
1996     case elfcpp::R_X86_64_GNU_VTENTRY:
1997       break;
1998
1999     case elfcpp::R_X86_64_64:
2000       Relocate_functions<64, false>::rela64(view, object, psymval, addend);
2001       break;
2002
2003     case elfcpp::R_X86_64_PC64:
2004       Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
2005                                               address);
2006       break;
2007
2008     case elfcpp::R_X86_64_32:
2009       // FIXME: we need to verify that value + addend fits into 32 bits:
2010       //    uint64_t x = value + addend;
2011       //    x == static_cast<uint64_t>(static_cast<uint32_t>(x))
2012       // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2013       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2014       break;
2015
2016     case elfcpp::R_X86_64_32S:
2017       // FIXME: we need to verify that value + addend fits into 32 bits:
2018       //    int64_t x = value + addend;   // note this quantity is signed!
2019       //    x == static_cast<int64_t>(static_cast<int32_t>(x))
2020       Relocate_functions<64, false>::rela32(view, object, psymval, addend);
2021       break;
2022
2023     case elfcpp::R_X86_64_PC32:
2024       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2025                                               address);
2026       break;
2027
2028     case elfcpp::R_X86_64_16:
2029       Relocate_functions<64, false>::rela16(view, object, psymval, addend);
2030       break;
2031
2032     case elfcpp::R_X86_64_PC16:
2033       Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
2034                                               address);
2035       break;
2036
2037     case elfcpp::R_X86_64_8:
2038       Relocate_functions<64, false>::rela8(view, object, psymval, addend);
2039       break;
2040
2041     case elfcpp::R_X86_64_PC8:
2042       Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
2043                                              address);
2044       break;
2045
2046     case elfcpp::R_X86_64_PLT32:
2047       gold_assert(gsym == NULL
2048                   || gsym->has_plt_offset()
2049                   || gsym->final_value_is_known()
2050                   || (gsym->is_defined()
2051                       && !gsym->is_from_dynobj()
2052                       && !gsym->is_preemptible()));
2053       // Note: while this code looks the same as for R_X86_64_PC32, it
2054       // behaves differently because psymval was set to point to
2055       // the PLT entry, rather than the symbol, in Scan::global().
2056       Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
2057                                               address);
2058       break;
2059
2060     case elfcpp::R_X86_64_PLTOFF64:
2061       {
2062         gold_assert(gsym);
2063         gold_assert(gsym->has_plt_offset()
2064                     || gsym->final_value_is_known());
2065         elfcpp::Elf_types<64>::Elf_Addr got_address;
2066         got_address = target->got_section(NULL, NULL)->address();
2067         Relocate_functions<64, false>::rela64(view, object, psymval,
2068                                               addend - got_address);
2069       }
2070
2071     case elfcpp::R_X86_64_GOT32:
2072       gold_assert(have_got_offset);
2073       Relocate_functions<64, false>::rela32(view, got_offset, addend);
2074       break;
2075
2076     case elfcpp::R_X86_64_GOTPC32:
2077       {
2078         gold_assert(gsym);
2079         elfcpp::Elf_types<64>::Elf_Addr value;
2080         value = target->got_plt_section()->address();
2081         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2082       }
2083       break;
2084
2085     case elfcpp::R_X86_64_GOT64:
2086       // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
2087       // Since we always add a PLT entry, this is equivalent.
2088     case elfcpp::R_X86_64_GOTPLT64:
2089       gold_assert(have_got_offset);
2090       Relocate_functions<64, false>::rela64(view, got_offset, addend);
2091       break;
2092
2093     case elfcpp::R_X86_64_GOTPC64:
2094       {
2095         gold_assert(gsym);
2096         elfcpp::Elf_types<64>::Elf_Addr value;
2097         value = target->got_plt_section()->address();
2098         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2099       }
2100       break;
2101
2102     case elfcpp::R_X86_64_GOTOFF64:
2103       {
2104         elfcpp::Elf_types<64>::Elf_Addr value;
2105         value = (psymval->value(object, 0)
2106                  - target->got_plt_section()->address());
2107         Relocate_functions<64, false>::rela64(view, value, addend);
2108       }
2109       break;
2110
2111     case elfcpp::R_X86_64_GOTPCREL:
2112       {
2113         gold_assert(have_got_offset);
2114         elfcpp::Elf_types<64>::Elf_Addr value;
2115         value = target->got_plt_section()->address() + got_offset;
2116         Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2117       }
2118       break;
2119
2120     case elfcpp::R_X86_64_GOTPCREL64:
2121       {
2122         gold_assert(have_got_offset);
2123         elfcpp::Elf_types<64>::Elf_Addr value;
2124         value = target->got_plt_section()->address() + got_offset;
2125         Relocate_functions<64, false>::pcrela64(view, value, addend, address);
2126       }
2127       break;
2128
2129     case elfcpp::R_X86_64_COPY:
2130     case elfcpp::R_X86_64_GLOB_DAT:
2131     case elfcpp::R_X86_64_JUMP_SLOT:
2132     case elfcpp::R_X86_64_RELATIVE:
2133       // These are outstanding tls relocs, which are unexpected when linking
2134     case elfcpp::R_X86_64_TPOFF64:
2135     case elfcpp::R_X86_64_DTPMOD64:
2136     case elfcpp::R_X86_64_TLSDESC:
2137       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2138                              _("unexpected reloc %u in object file"),
2139                              r_type);
2140       break;
2141
2142       // These are initial tls relocs, which are expected when linking
2143     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2144     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2145     case elfcpp::R_X86_64_TLSDESC_CALL:
2146     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2147     case elfcpp::R_X86_64_DTPOFF32:
2148     case elfcpp::R_X86_64_DTPOFF64:
2149     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2150     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2151       this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
2152                          view, address, view_size);
2153       break;
2154
2155     case elfcpp::R_X86_64_SIZE32:
2156     case elfcpp::R_X86_64_SIZE64:
2157     default:
2158       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2159                              _("unsupported reloc %u"),
2160                              r_type);
2161       break;
2162     }
2163
2164   return true;
2165 }
2166
2167 // Perform a TLS relocation.
2168
2169 inline void
2170 Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
2171                                       Target_x86_64* target,
2172                                       size_t relnum,
2173                                       const elfcpp::Rela<64, false>& rela,
2174                                       unsigned int r_type,
2175                                       const Sized_symbol<64>* gsym,
2176                                       const Symbol_value<64>* psymval,
2177                                       unsigned char* view,
2178                                       elfcpp::Elf_types<64>::Elf_Addr address,
2179                                       section_size_type view_size)
2180 {
2181   Output_segment* tls_segment = relinfo->layout->tls_segment();
2182
2183   const Sized_relobj<64, false>* object = relinfo->object;
2184   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2185
2186   elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
2187
2188   const bool is_final = (gsym == NULL
2189                          ? !parameters->options().shared()
2190                          : gsym->final_value_is_known());
2191   const tls::Tls_optimization optimized_type
2192       = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2193   switch (r_type)
2194     {
2195     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2196       this->saw_tls_block_reloc_ = true;
2197       if (optimized_type == tls::TLSOPT_TO_LE)
2198         {
2199           gold_assert(tls_segment != NULL);
2200           this->tls_gd_to_le(relinfo, relnum, tls_segment,
2201                              rela, r_type, value, view,
2202                              view_size);
2203           break;
2204         }
2205       else
2206         {
2207           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2208                                    ? GOT_TYPE_TLS_OFFSET
2209                                    : GOT_TYPE_TLS_PAIR);
2210           unsigned int got_offset;
2211           if (gsym != NULL)
2212             {
2213               gold_assert(gsym->has_got_offset(got_type));
2214               got_offset = gsym->got_offset(got_type) - target->got_size();
2215             }
2216           else
2217             {
2218               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2219               gold_assert(object->local_has_got_offset(r_sym, got_type));
2220               got_offset = (object->local_got_offset(r_sym, got_type)
2221                             - target->got_size());
2222             }
2223           if (optimized_type == tls::TLSOPT_TO_IE)
2224             {
2225               gold_assert(tls_segment != NULL);
2226               value = target->got_plt_section()->address() + got_offset;
2227               this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
2228                                  value, view, address, view_size);
2229               break;
2230             }
2231           else if (optimized_type == tls::TLSOPT_NONE)
2232             {
2233               // Relocate the field with the offset of the pair of GOT
2234               // entries.
2235               value = target->got_plt_section()->address() + got_offset;
2236               Relocate_functions<64, false>::pcrela32(view, value, addend,
2237                                                       address);
2238               break;
2239             }
2240         }
2241       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2242                              _("unsupported reloc %u"), r_type);
2243       break;
2244
2245     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2246     case elfcpp::R_X86_64_TLSDESC_CALL:
2247       this->saw_tls_block_reloc_ = true;
2248       if (optimized_type == tls::TLSOPT_TO_LE)
2249         {
2250           gold_assert(tls_segment != NULL);
2251           this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2252                                   rela, r_type, value, view,
2253                                   view_size);
2254           break;
2255         }
2256       else
2257         {
2258           unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2259                                    ? GOT_TYPE_TLS_OFFSET
2260                                    : GOT_TYPE_TLS_DESC);
2261           unsigned int got_offset = 0;
2262           if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC
2263               && optimized_type == tls::TLSOPT_NONE)
2264             {
2265               // We created GOT entries in the .got.tlsdesc portion of
2266               // the .got.plt section, but the offset stored in the
2267               // symbol is the offset within .got.tlsdesc.
2268               got_offset = (target->got_size()
2269                             + target->got_plt_section()->data_size());
2270             }
2271           if (gsym != NULL)
2272             {
2273               gold_assert(gsym->has_got_offset(got_type));
2274               got_offset += gsym->got_offset(got_type) - target->got_size();
2275             }
2276           else
2277             {
2278               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2279               gold_assert(object->local_has_got_offset(r_sym, got_type));
2280               got_offset += (object->local_got_offset(r_sym, got_type)
2281                              - target->got_size());
2282             }
2283           if (optimized_type == tls::TLSOPT_TO_IE)
2284             {
2285               gold_assert(tls_segment != NULL);
2286               value = target->got_plt_section()->address() + got_offset;
2287               this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2288                                       rela, r_type, value, view, address,
2289                                       view_size);
2290               break;
2291             }
2292           else if (optimized_type == tls::TLSOPT_NONE)
2293             {
2294               if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2295                 {
2296                   // Relocate the field with the offset of the pair of GOT
2297                   // entries.
2298                   value = target->got_plt_section()->address() + got_offset;
2299                   Relocate_functions<64, false>::pcrela32(view, value, addend,
2300                                                           address);
2301                 }
2302               break;
2303             }
2304         }
2305       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2306                              _("unsupported reloc %u"), r_type);
2307       break;
2308
2309     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2310       this->saw_tls_block_reloc_ = true;
2311       if (optimized_type == tls::TLSOPT_TO_LE)
2312         {
2313           gold_assert(tls_segment != NULL);
2314           this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2315                              value, view, view_size);
2316           break;
2317         }
2318       else if (optimized_type == tls::TLSOPT_NONE)
2319         {
2320           // Relocate the field with the offset of the GOT entry for
2321           // the module index.
2322           unsigned int got_offset;
2323           got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2324                         - target->got_size());
2325           value = target->got_plt_section()->address() + got_offset;
2326           Relocate_functions<64, false>::pcrela32(view, value, addend,
2327                                                   address);
2328           break;
2329         }
2330       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2331                              _("unsupported reloc %u"), r_type);
2332       break;
2333
2334     case elfcpp::R_X86_64_DTPOFF32:
2335       if (optimized_type == tls::TLSOPT_TO_LE)
2336         {
2337           // This relocation type is used in debugging information.
2338           // In that case we need to not optimize the value.  If we
2339           // haven't seen a TLSLD reloc, then we assume we should not
2340           // optimize this reloc.
2341           if (this->saw_tls_block_reloc_)
2342             {
2343               gold_assert(tls_segment != NULL);
2344               value -= tls_segment->memsz();
2345             }
2346         }
2347       Relocate_functions<64, false>::rela32(view, value, addend);
2348       break;
2349
2350     case elfcpp::R_X86_64_DTPOFF64:
2351       if (optimized_type == tls::TLSOPT_TO_LE)
2352         {
2353           // See R_X86_64_DTPOFF32, just above, for why we test this.
2354           if (this->saw_tls_block_reloc_)
2355             {
2356               gold_assert(tls_segment != NULL);
2357               value -= tls_segment->memsz();
2358             }
2359         }
2360       Relocate_functions<64, false>::rela64(view, value, addend);
2361       break;
2362
2363     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2364       if (optimized_type == tls::TLSOPT_TO_LE)
2365         {
2366           gold_assert(tls_segment != NULL);
2367           Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2368                                                 rela, r_type, value, view,
2369                                                 view_size);
2370           break;
2371         }
2372       else if (optimized_type == tls::TLSOPT_NONE)
2373         {
2374           // Relocate the field with the offset of the GOT entry for
2375           // the tp-relative offset of the symbol.
2376           unsigned int got_offset;
2377           if (gsym != NULL)
2378             {
2379               gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2380               got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2381                             - target->got_size());
2382             }
2383           else
2384             {
2385               unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2386               gold_assert(object->local_has_got_offset(r_sym,
2387                                                        GOT_TYPE_TLS_OFFSET));
2388               got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
2389                             - target->got_size());
2390             }
2391           value = target->got_plt_section()->address() + got_offset;
2392           Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2393           break;
2394         }
2395       gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2396                              _("unsupported reloc type %u"),
2397                              r_type);
2398       break;
2399
2400     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2401       value -= tls_segment->memsz();
2402       Relocate_functions<64, false>::rela32(view, value, addend);
2403       break;
2404     }
2405 }
2406
2407 // Do a relocation in which we convert a TLS General-Dynamic to an
2408 // Initial-Exec.
2409
2410 inline void
2411 Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2412                                       size_t relnum,
2413                                       Output_segment*,
2414                                       const elfcpp::Rela<64, false>& rela,
2415                                       unsigned int,
2416                                       elfcpp::Elf_types<64>::Elf_Addr value,
2417                                       unsigned char* view,
2418                                       elfcpp::Elf_types<64>::Elf_Addr address,
2419                                       section_size_type view_size)
2420 {
2421   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2422   // .word 0x6666; rex64; call __tls_get_addr
2423   // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2424
2425   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2426   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2427
2428   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2429                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2430   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2431                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2432
2433   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2434
2435   const elfcpp::Elf_Xword addend = rela.get_r_addend();
2436   Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
2437
2438   // The next reloc should be a PLT32 reloc against __tls_get_addr.
2439   // We can skip it.
2440   this->skip_call_tls_get_addr_ = true;
2441 }
2442
2443 // Do a relocation in which we convert a TLS General-Dynamic to a
2444 // Local-Exec.
2445
2446 inline void
2447 Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2448                                       size_t relnum,
2449                                       Output_segment* tls_segment,
2450                                       const elfcpp::Rela<64, false>& rela,
2451                                       unsigned int,
2452                                       elfcpp::Elf_types<64>::Elf_Addr value,
2453                                       unsigned char* view,
2454                                       section_size_type view_size)
2455 {
2456   // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2457   // .word 0x6666; rex64; call __tls_get_addr
2458   // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2459
2460   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2461   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2462
2463   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2464                  (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2465   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2466                  (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2467
2468   memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2469
2470   value -= tls_segment->memsz();
2471   Relocate_functions<64, false>::rela32(view + 8, value, 0);
2472
2473   // The next reloc should be a PLT32 reloc against __tls_get_addr.
2474   // We can skip it.
2475   this->skip_call_tls_get_addr_ = true;
2476 }
2477
2478 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2479
2480 inline void
2481 Target_x86_64::Relocate::tls_desc_gd_to_ie(
2482     const Relocate_info<64, false>* relinfo,
2483     size_t relnum,
2484     Output_segment*,
2485     const elfcpp::Rela<64, false>& rela,
2486     unsigned int r_type,
2487     elfcpp::Elf_types<64>::Elf_Addr value,
2488     unsigned char* view,
2489     elfcpp::Elf_types<64>::Elf_Addr address,
2490     section_size_type view_size)
2491 {
2492   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2493     {
2494       // leaq foo@tlsdesc(%rip), %rax
2495       // ==> movq foo@gottpoff(%rip), %rax
2496       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2497       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2498       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2499                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2500       view[-2] = 0x8b;
2501       const elfcpp::Elf_Xword addend = rela.get_r_addend();
2502       Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2503     }
2504   else
2505     {
2506       // call *foo@tlscall(%rax)
2507       // ==> nop; nop
2508       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2509       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2510       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2511                      view[0] == 0xff && view[1] == 0x10);
2512       view[0] = 0x66;
2513       view[1] = 0x90;
2514     }
2515 }
2516
2517 // Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2518
2519 inline void
2520 Target_x86_64::Relocate::tls_desc_gd_to_le(
2521     const Relocate_info<64, false>* relinfo,
2522     size_t relnum,
2523     Output_segment* tls_segment,
2524     const elfcpp::Rela<64, false>& rela,
2525     unsigned int r_type,
2526     elfcpp::Elf_types<64>::Elf_Addr value,
2527     unsigned char* view,
2528     section_size_type view_size)
2529 {
2530   if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2531     {
2532       // leaq foo@tlsdesc(%rip), %rax
2533       // ==> movq foo@tpoff, %rax
2534       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2535       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2536       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2537                      view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2538       view[-2] = 0xc7;
2539       view[-1] = 0xc0;
2540       value -= tls_segment->memsz();
2541       Relocate_functions<64, false>::rela32(view, value, 0);
2542     }
2543   else
2544     {
2545       // call *foo@tlscall(%rax)
2546       // ==> nop; nop
2547       gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2548       tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2549       tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2550                      view[0] == 0xff && view[1] == 0x10);
2551       view[0] = 0x66;
2552       view[1] = 0x90;
2553     }
2554 }
2555
2556 inline void
2557 Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2558                                       size_t relnum,
2559                                       Output_segment*,
2560                                       const elfcpp::Rela<64, false>& rela,
2561                                       unsigned int,
2562                                       elfcpp::Elf_types<64>::Elf_Addr,
2563                                       unsigned char* view,
2564                                       section_size_type view_size)
2565 {
2566   // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2567   // ... leq foo@dtpoff(%rax),%reg
2568   // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2569
2570   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2571   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2572
2573   tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2574                  view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2575
2576   tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2577
2578   memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2579
2580   // The next reloc should be a PLT32 reloc against __tls_get_addr.
2581   // We can skip it.
2582   this->skip_call_tls_get_addr_ = true;
2583 }
2584
2585 // Do a relocation in which we convert a TLS Initial-Exec to a
2586 // Local-Exec.
2587
2588 inline void
2589 Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2590                                       size_t relnum,
2591                                       Output_segment* tls_segment,
2592                                       const elfcpp::Rela<64, false>& rela,
2593                                       unsigned int,
2594                                       elfcpp::Elf_types<64>::Elf_Addr value,
2595                                       unsigned char* view,
2596                                       section_size_type view_size)
2597 {
2598   // We need to examine the opcodes to figure out which instruction we
2599   // are looking at.
2600
2601   // movq foo@gottpoff(%rip),%reg  ==>  movq $YY,%reg
2602   // addq foo@gottpoff(%rip),%reg  ==>  addq $YY,%reg
2603
2604   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2605   tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2606
2607   unsigned char op1 = view[-3];
2608   unsigned char op2 = view[-2];
2609   unsigned char op3 = view[-1];
2610   unsigned char reg = op3 >> 3;
2611
2612   if (op2 == 0x8b)
2613     {
2614       // movq
2615       if (op1 == 0x4c)
2616         view[-3] = 0x49;
2617       view[-2] = 0xc7;
2618       view[-1] = 0xc0 | reg;
2619     }
2620   else if (reg == 4)
2621     {
2622       // Special handling for %rsp.
2623       if (op1 == 0x4c)
2624         view[-3] = 0x49;
2625       view[-2] = 0x81;
2626       view[-1] = 0xc0 | reg;
2627     }
2628   else
2629     {
2630       // addq
2631       if (op1 == 0x4c)
2632         view[-3] = 0x4d;
2633       view[-2] = 0x8d;
2634       view[-1] = 0x80 | reg | (reg << 3);
2635     }
2636
2637   value -= tls_segment->memsz();
2638   Relocate_functions<64, false>::rela32(view, value, 0);
2639 }
2640
2641 // Relocate section data.
2642
2643 void
2644 Target_x86_64::relocate_section(
2645     const Relocate_info<64, false>* relinfo,
2646     unsigned int sh_type,
2647     const unsigned char* prelocs,
2648     size_t reloc_count,
2649     Output_section* output_section,
2650     bool needs_special_offset_handling,
2651     unsigned char* view,
2652     elfcpp::Elf_types<64>::Elf_Addr address,
2653     section_size_type view_size,
2654     const Reloc_symbol_changes* reloc_symbol_changes)
2655 {
2656   gold_assert(sh_type == elfcpp::SHT_RELA);
2657
2658   gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2659                          Target_x86_64::Relocate>(
2660     relinfo,
2661     this,
2662     prelocs,
2663     reloc_count,
2664     output_section,
2665     needs_special_offset_handling,
2666     view,
2667     address,
2668     view_size,
2669     reloc_symbol_changes);
2670 }
2671
2672 // Return the size of a relocation while scanning during a relocatable
2673 // link.
2674
2675 unsigned int
2676 Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2677     unsigned int r_type,
2678     Relobj* object)
2679 {
2680   switch (r_type)
2681     {
2682     case elfcpp::R_X86_64_NONE:
2683     case elfcpp::R_X86_64_GNU_VTINHERIT:
2684     case elfcpp::R_X86_64_GNU_VTENTRY:
2685     case elfcpp::R_X86_64_TLSGD:            // Global-dynamic
2686     case elfcpp::R_X86_64_GOTPC32_TLSDESC:  // Global-dynamic (from ~oliva url)
2687     case elfcpp::R_X86_64_TLSDESC_CALL:
2688     case elfcpp::R_X86_64_TLSLD:            // Local-dynamic
2689     case elfcpp::R_X86_64_DTPOFF32:
2690     case elfcpp::R_X86_64_DTPOFF64:
2691     case elfcpp::R_X86_64_GOTTPOFF:         // Initial-exec
2692     case elfcpp::R_X86_64_TPOFF32:          // Local-exec
2693       return 0;
2694
2695     case elfcpp::R_X86_64_64:
2696     case elfcpp::R_X86_64_PC64:
2697     case elfcpp::R_X86_64_GOTOFF64:
2698     case elfcpp::R_X86_64_GOTPC64:
2699     case elfcpp::R_X86_64_PLTOFF64:
2700     case elfcpp::R_X86_64_GOT64:
2701     case elfcpp::R_X86_64_GOTPCREL64:
2702     case elfcpp::R_X86_64_GOTPCREL:
2703     case elfcpp::R_X86_64_GOTPLT64:
2704       return 8;
2705
2706     case elfcpp::R_X86_64_32:
2707     case elfcpp::R_X86_64_32S:
2708     case elfcpp::R_X86_64_PC32:
2709     case elfcpp::R_X86_64_PLT32:
2710     case elfcpp::R_X86_64_GOTPC32:
2711     case elfcpp::R_X86_64_GOT32:
2712       return 4;
2713
2714     case elfcpp::R_X86_64_16:
2715     case elfcpp::R_X86_64_PC16:
2716       return 2;
2717
2718     case elfcpp::R_X86_64_8:
2719     case elfcpp::R_X86_64_PC8:
2720       return 1;
2721
2722     case elfcpp::R_X86_64_COPY:
2723     case elfcpp::R_X86_64_GLOB_DAT:
2724     case elfcpp::R_X86_64_JUMP_SLOT:
2725     case elfcpp::R_X86_64_RELATIVE:
2726       // These are outstanding tls relocs, which are unexpected when linking
2727     case elfcpp::R_X86_64_TPOFF64:
2728     case elfcpp::R_X86_64_DTPMOD64:
2729     case elfcpp::R_X86_64_TLSDESC:
2730       object->error(_("unexpected reloc %u in object file"), r_type);
2731       return 0;
2732
2733     case elfcpp::R_X86_64_SIZE32:
2734     case elfcpp::R_X86_64_SIZE64:
2735     default:
2736       object->error(_("unsupported reloc %u against local symbol"), r_type);
2737       return 0;
2738     }
2739 }
2740
2741 // Scan the relocs during a relocatable link.
2742
2743 void
2744 Target_x86_64::scan_relocatable_relocs(Symbol_table* symtab,
2745                                        Layout* layout,
2746                                        Sized_relobj<64, false>* object,
2747                                        unsigned int data_shndx,
2748                                        unsigned int sh_type,
2749                                        const unsigned char* prelocs,
2750                                        size_t reloc_count,
2751                                        Output_section* output_section,
2752                                        bool needs_special_offset_handling,
2753                                        size_t local_symbol_count,
2754                                        const unsigned char* plocal_symbols,
2755                                        Relocatable_relocs* rr)
2756 {
2757   gold_assert(sh_type == elfcpp::SHT_RELA);
2758
2759   typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2760     Relocatable_size_for_reloc> Scan_relocatable_relocs;
2761
2762   gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
2763       Scan_relocatable_relocs>(
2764     symtab,
2765     layout,
2766     object,
2767     data_shndx,
2768     prelocs,
2769     reloc_count,
2770     output_section,
2771     needs_special_offset_handling,
2772     local_symbol_count,
2773     plocal_symbols,
2774     rr);
2775 }
2776
2777 // Relocate a section during a relocatable link.
2778
2779 void
2780 Target_x86_64::relocate_for_relocatable(
2781     const Relocate_info<64, false>* relinfo,
2782     unsigned int sh_type,
2783     const unsigned char* prelocs,
2784     size_t reloc_count,
2785     Output_section* output_section,
2786     off_t offset_in_output_section,
2787     const Relocatable_relocs* rr,
2788     unsigned char* view,
2789     elfcpp::Elf_types<64>::Elf_Addr view_address,
2790     section_size_type view_size,
2791     unsigned char* reloc_view,
2792     section_size_type reloc_view_size)
2793 {
2794   gold_assert(sh_type == elfcpp::SHT_RELA);
2795
2796   gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
2797     relinfo,
2798     prelocs,
2799     reloc_count,
2800     output_section,
2801     offset_in_output_section,
2802     rr,
2803     view,
2804     view_address,
2805     view_size,
2806     reloc_view,
2807     reloc_view_size);
2808 }
2809
2810 // Return the value to use for a dynamic which requires special
2811 // treatment.  This is how we support equality comparisons of function
2812 // pointers across shared library boundaries, as described in the
2813 // processor specific ABI supplement.
2814
2815 uint64_t
2816 Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2817 {
2818   gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2819   return this->plt_section()->address() + gsym->plt_offset();
2820 }
2821
2822 // Return a string used to fill a code section with nops to take up
2823 // the specified length.
2824
2825 std::string
2826 Target_x86_64::do_code_fill(section_size_type length) const
2827 {
2828   if (length >= 16)
2829     {
2830       // Build a jmpq instruction to skip over the bytes.
2831       unsigned char jmp[5];
2832       jmp[0] = 0xe9;
2833       elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2834       return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2835               + std::string(length - 5, '\0'));
2836     }
2837
2838   // Nop sequences of various lengths.
2839   const char nop1[1] = { 0x90 };                   // nop
2840   const char nop2[2] = { 0x66, 0x90 };             // xchg %ax %ax
2841   const char nop3[3] = { 0x0f, 0x1f, 0x00 };       // nop (%rax)
2842   const char nop4[4] = { 0x0f, 0x1f, 0x40, 0x00};  // nop 0(%rax)
2843   const char nop5[5] = { 0x0f, 0x1f, 0x44, 0x00,   // nop 0(%rax,%rax,1)
2844                          0x00 };
2845   const char nop6[6] = { 0x66, 0x0f, 0x1f, 0x44,   // nopw 0(%rax,%rax,1)
2846                          0x00, 0x00 };
2847   const char nop7[7] = { 0x0f, 0x1f, 0x80, 0x00,   // nopl 0L(%rax)
2848                          0x00, 0x00, 0x00 };
2849   const char nop8[8] = { 0x0f, 0x1f, 0x84, 0x00,   // nopl 0L(%rax,%rax,1)
2850                          0x00, 0x00, 0x00, 0x00 };
2851   const char nop9[9] = { 0x66, 0x0f, 0x1f, 0x84,   // nopw 0L(%rax,%rax,1)
2852                          0x00, 0x00, 0x00, 0x00,
2853                          0x00 };
2854   const char nop10[10] = { 0x66, 0x2e, 0x0f, 0x1f, // nopw %cs:0L(%rax,%rax,1)
2855                            0x84, 0x00, 0x00, 0x00,
2856                            0x00, 0x00 };
2857   const char nop11[11] = { 0x66, 0x66, 0x2e, 0x0f, // data16
2858                            0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2859                            0x00, 0x00, 0x00 };
2860   const char nop12[12] = { 0x66, 0x66, 0x66, 0x2e, // data16; data16
2861                            0x0f, 0x1f, 0x84, 0x00, // nopw %cs:0L(%rax,%rax,1)
2862                            0x00, 0x00, 0x00, 0x00 };
2863   const char nop13[13] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2864                            0x2e, 0x0f, 0x1f, 0x84, // nopw %cs:0L(%rax,%rax,1)
2865                            0x00, 0x00, 0x00, 0x00,
2866                            0x00 };
2867   const char nop14[14] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2868                            0x66, 0x2e, 0x0f, 0x1f, // data16
2869                            0x84, 0x00, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2870                            0x00, 0x00 };
2871   const char nop15[15] = { 0x66, 0x66, 0x66, 0x66, // data16; data16; data16
2872                            0x66, 0x66, 0x2e, 0x0f, // data16; data16
2873                            0x1f, 0x84, 0x00, 0x00, // nopw %cs:0L(%rax,%rax,1)
2874                            0x00, 0x00, 0x00 };
2875
2876   const char* nops[16] = {
2877     NULL,
2878     nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2879     nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2880   };
2881
2882   return std::string(nops[length], length);
2883 }
2884
2885 // Return the addend to use for a target specific relocation.  The
2886 // only target specific relocation is R_X86_64_TLSDESC for a local
2887 // symbol.  We want to set the addend is the offset of the local
2888 // symbol in the TLS segment.
2889
2890 uint64_t
2891 Target_x86_64::do_reloc_addend(void* arg, unsigned int r_type,
2892                                uint64_t) const
2893 {
2894   gold_assert(r_type == elfcpp::R_X86_64_TLSDESC);
2895   uintptr_t intarg = reinterpret_cast<uintptr_t>(arg);
2896   gold_assert(intarg < this->tlsdesc_reloc_info_.size());
2897   const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]);
2898   const Symbol_value<64>* psymval = ti.object->local_symbol(ti.r_sym);
2899   gold_assert(psymval->is_tls_symbol());
2900   // The value of a TLS symbol is the offset in the TLS segment.
2901   return psymval->value(ti.object, 0);
2902 }
2903
2904 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2905 // compiled with -fstack-split.  The function calls non-stack-split
2906 // code.  We have to change the function so that it always ensures
2907 // that it has enough stack space to run some random function.
2908
2909 void
2910 Target_x86_64::do_calls_non_split(Relobj* object, unsigned int shndx,
2911                                   section_offset_type fnoffset,
2912                                   section_size_type fnsize,
2913                                   unsigned char* view,
2914                                   section_size_type view_size,
2915                                   std::string* from,
2916                                   std::string* to) const
2917 {
2918   // The function starts with a comparison of the stack pointer and a
2919   // field in the TCB.  This is followed by a jump.
2920
2921   // cmp %fs:NN,%rsp
2922   if (this->match_view(view, view_size, fnoffset, "\x64\x48\x3b\x24\x25", 5)
2923       && fnsize > 9)
2924     {
2925       // We will call __morestack if the carry flag is set after this
2926       // comparison.  We turn the comparison into an stc instruction
2927       // and some nops.
2928       view[fnoffset] = '\xf9';
2929       this->set_view_to_nop(view, view_size, fnoffset + 1, 8);
2930     }
2931   // lea NN(%rsp),%r10
2932   // lea NN(%rsp),%r11
2933   else if ((this->match_view(view, view_size, fnoffset,
2934                              "\x4c\x8d\x94\x24", 4)
2935             || this->match_view(view, view_size, fnoffset,
2936                                 "\x4c\x8d\x9c\x24", 4))
2937            && fnsize > 8)
2938     {
2939       // This is loading an offset from the stack pointer for a
2940       // comparison.  The offset is negative, so we decrease the
2941       // offset by the amount of space we need for the stack.  This
2942       // means we will avoid calling __morestack if there happens to
2943       // be plenty of space on the stack already.
2944       unsigned char* pval = view + fnoffset + 4;
2945       uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2946       val -= parameters->options().split_stack_adjust_size();
2947       elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2948     }
2949   else
2950     {
2951       if (!object->has_no_split_stack())
2952         object->error(_("failed to match split-stack sequence at "
2953                         "section %u offset %0zx"),
2954                       shndx, static_cast<size_t>(fnoffset));
2955       return;
2956     }
2957
2958   // We have to change the function so that it calls
2959   // __morestack_non_split instead of __morestack.  The former will
2960   // allocate additional stack space.
2961   *from = "__morestack";
2962   *to = "__morestack_non_split";
2963 }
2964
2965 // The selector for x86_64 object files.
2966
2967 class Target_selector_x86_64 : public Target_selector_freebsd
2968 {
2969 public:
2970   Target_selector_x86_64()
2971     : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
2972                               "elf64-x86-64-freebsd")
2973   { }
2974
2975   Target*
2976   do_instantiate_target()
2977   { return new Target_x86_64(); }
2978
2979 };
2980
2981 Target_selector_x86_64 target_selector_x86_64;
2982
2983 } // End anonymous namespace.