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