OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [binutils-2.20.1/] [gold/] [x86_64.cc] - Blame information for rev 862

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.