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

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gold/] [x86_64.cc] - Blame information for rev 253

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

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

powered by: WebSVN 2.1.0

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