* observer.sh: Set LANG/LC_ALL to C, not c.
[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;
33aea2fd 977 gold_assert(parameters->options().output_is_position_independent());
a036edd8
ILT
978 object->error(_("requires unsupported dynamic reloc; "
979 "recompile with -fPIC"));
980 this->issued_non_pic_error_ = true;
981 return;
982
983 case elfcpp::R_X86_64_NONE:
984 gold_unreachable();
985 }
986}
987
2e30d253
ILT
988// Scan a relocation for a local symbol.
989
990inline void
991Target_x86_64::Scan::local(const General_options&,
d61c17ea
ILT
992 Symbol_table* symtab,
993 Layout* layout,
994 Target_x86_64* target,
995 Sized_relobj<64, false>* object,
0ffd9845 996 unsigned int data_shndx,
4f4c5f80 997 Output_section* output_section,
0ffd9845 998 const elfcpp::Rela<64, false>& reloc,
d61c17ea 999 unsigned int r_type,
7bf1f802 1000 const elfcpp::Sym<64, false>& lsym)
2e30d253
ILT
1001{
1002 switch (r_type)
1003 {
1004 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
1005 case elfcpp::R_386_GNU_VTINHERIT:
1006 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
1007 break;
1008
1009 case elfcpp::R_X86_64_64:
d61c6bd4 1010 // If building a shared library (or a position-independent
dceae3c1
ILT
1011 // executable), we need to create a dynamic relocation for this
1012 // location. The relocation applied at link time will apply the
1013 // link-time value, so we flag the location with an
1014 // R_X86_64_RELATIVE relocation so the dynamic loader can
d61c6bd4 1015 // relocate it easily.
8851ecca 1016 if (parameters->options().output_is_position_independent())
d61c6bd4 1017 {
e8c846c3 1018 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d61c6bd4 1019 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
e8c846c3
ILT
1020 rela_dyn->add_local_relative(object, r_sym,
1021 elfcpp::R_X86_64_RELATIVE,
1022 output_section, data_shndx,
1023 reloc.get_r_offset(),
1024 reloc.get_r_addend());
d61c6bd4
ILT
1025 }
1026 break;
1027
2e30d253
ILT
1028 case elfcpp::R_X86_64_32:
1029 case elfcpp::R_X86_64_32S:
1030 case elfcpp::R_X86_64_16:
1031 case elfcpp::R_X86_64_8:
96f2030e 1032 // If building a shared library (or a position-independent
dceae3c1
ILT
1033 // executable), we need to create a dynamic relocation for this
1034 // location. We can't use an R_X86_64_RELATIVE relocation
1035 // because that is always a 64-bit relocation.
8851ecca 1036 if (parameters->options().output_is_position_independent())
96f2030e 1037 {
a036edd8
ILT
1038 this->check_non_pic(object, r_type);
1039
96f2030e 1040 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
d491d34e 1041 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
dceae3c1 1042 if (lsym.get_st_type() != elfcpp::STT_SECTION)
d491d34e
ILT
1043 rela_dyn->add_local(object, r_sym, r_type, output_section,
1044 data_shndx, reloc.get_r_offset(),
1045 reloc.get_r_addend());
dceae3c1
ILT
1046 else
1047 {
1048 gold_assert(lsym.get_st_value() == 0);
d491d34e
ILT
1049 unsigned int shndx = lsym.get_st_shndx();
1050 bool is_ordinary;
1051 shndx = object->adjust_sym_shndx(r_sym, shndx,
1052 &is_ordinary);
1053 if (!is_ordinary)
1054 object->error(_("section symbol %u has bad shndx %u"),
1055 r_sym, shndx);
1056 else
1057 rela_dyn->add_local_section(object, shndx,
1058 r_type, output_section,
1059 data_shndx, reloc.get_r_offset(),
1060 reloc.get_r_addend());
dceae3c1 1061 }
96f2030e 1062 }
2e30d253
ILT
1063 break;
1064
1065 case elfcpp::R_X86_64_PC64:
1066 case elfcpp::R_X86_64_PC32:
1067 case elfcpp::R_X86_64_PC16:
1068 case elfcpp::R_X86_64_PC8:
1069 break;
1070
f389a824
ILT
1071 case elfcpp::R_X86_64_PLT32:
1072 // Since we know this is a local symbol, we can handle this as a
1073 // PC32 reloc.
1074 break;
1075
fdc2f80f 1076 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1077 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1078 case elfcpp::R_X86_64_GOTPC64:
1079 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1080 // We need a GOT section.
1081 target->got_section(symtab, layout);
ee9e9e86
ILT
1082 // For PLTOFF64, we'd normally want a PLT section, but since we
1083 // know this is a local symbol, no PLT is needed.
2e30d253
ILT
1084 break;
1085
0ffd9845
ILT
1086 case elfcpp::R_X86_64_GOT64:
1087 case elfcpp::R_X86_64_GOT32:
1088 case elfcpp::R_X86_64_GOTPCREL64:
1089 case elfcpp::R_X86_64_GOTPCREL:
ee9e9e86 1090 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
1091 {
1092 // The symbol requires a GOT entry.
1093 Output_data_got<64, false>* got = target->got_section(symtab, layout);
1094 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7 1095 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
0ffd9845
ILT
1096 {
1097 // If we are generating a shared object, we need to add a
7bf1f802 1098 // dynamic relocation for this symbol's GOT entry.
8851ecca 1099 if (parameters->options().output_is_position_independent())
0ffd9845
ILT
1100 {
1101 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
7bf1f802
ILT
1102 // R_X86_64_RELATIVE assumes a 64-bit relocation.
1103 if (r_type != elfcpp::R_X86_64_GOT32)
0a65a3a7
CC
1104 rela_dyn->add_local_relative(
1105 object, r_sym, elfcpp::R_X86_64_RELATIVE, got,
1106 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
7bf1f802 1107 else
dceae3c1 1108 {
a036edd8
ILT
1109 this->check_non_pic(object, r_type);
1110
dceae3c1 1111 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
0a65a3a7
CC
1112 rela_dyn->add_local(
1113 object, r_sym, r_type, got,
1114 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0);
dceae3c1 1115 }
0ffd9845
ILT
1116 }
1117 }
ee9e9e86
ILT
1118 // For GOTPLT64, we'd normally want a PLT section, but since
1119 // we know this is a local symbol, no PLT is needed.
0ffd9845
ILT
1120 }
1121 break;
1122
2e30d253
ILT
1123 case elfcpp::R_X86_64_COPY:
1124 case elfcpp::R_X86_64_GLOB_DAT:
1125 case elfcpp::R_X86_64_JUMP_SLOT:
1126 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1127 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 1128 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1129 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 1130 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1131 gold_error(_("%s: unexpected reloc %u in object file"),
1132 object->name().c_str(), r_type);
2e30d253
ILT
1133 break;
1134
d61c17ea 1135 // These are initial tls relocs, which are expected when linking
56622147
ILT
1136 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1137 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1138 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1139 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1140 case elfcpp::R_X86_64_DTPOFF32:
1141 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1142 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1143 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253 1144 {
8851ecca 1145 bool output_is_shared = parameters->options().shared();
e041f13d
ILT
1146 const tls::Tls_optimization optimized_type
1147 = Target_x86_64::optimize_tls_reloc(!output_is_shared, r_type);
2e30d253
ILT
1148 switch (r_type)
1149 {
56622147 1150 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
1151 if (optimized_type == tls::TLSOPT_NONE)
1152 {
1153 // Create a pair of GOT entries for the module index and
1154 // dtv-relative offset.
1155 Output_data_got<64, false>* got
1156 = target->got_section(symtab, layout);
1157 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d491d34e
ILT
1158 unsigned int shndx = lsym.get_st_shndx();
1159 bool is_ordinary;
1160 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1161 if (!is_ordinary)
1162 object->error(_("local symbol %u has bad shndx %u"),
1163 r_sym, shndx);
1164 else
1165 got->add_local_pair_with_rela(object, r_sym,
1166 shndx,
1167 GOT_TYPE_TLS_PAIR,
1168 target->rela_dyn_section(layout),
1169 elfcpp::R_X86_64_DTPMOD64, 0);
7bf1f802
ILT
1170 }
1171 else if (optimized_type != tls::TLSOPT_TO_LE)
1172 unsupported_reloc_local(object, r_type);
1173 break;
1174
56622147 1175 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 1176 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
1177 if (optimized_type == tls::TLSOPT_NONE)
1178 {
1179 // Create reserved PLT and GOT entries for the resolver.
1180 target->reserve_tlsdesc_entries(symtab, layout);
1181
1182 // Generate a double GOT entry with an R_X86_64_TLSDESC reloc.
1183 Output_data_got<64, false>* got
1184 = target->got_section(symtab, layout);
1185 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
d491d34e
ILT
1186 unsigned int shndx = lsym.get_st_shndx();
1187 bool is_ordinary;
1188 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1189 if (!is_ordinary)
1190 object->error(_("local symbol %u has bad shndx %u"),
1191 r_sym, shndx);
1192 else
1193 got->add_local_pair_with_rela(object, r_sym,
1194 shndx,
1195 GOT_TYPE_TLS_DESC,
1196 target->rela_dyn_section(layout),
1197 elfcpp::R_X86_64_TLSDESC, 0);
c2b45e22
CC
1198 }
1199 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 1200 unsupported_reloc_local(object, r_type);
2e30d253
ILT
1201 break;
1202
c2b45e22
CC
1203 case elfcpp::R_X86_64_TLSDESC_CALL:
1204 break;
1205
e041f13d 1206 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
1207 if (optimized_type == tls::TLSOPT_NONE)
1208 {
1209 // Create a GOT entry for the module index.
31d60480 1210 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
1211 }
1212 else if (optimized_type != tls::TLSOPT_TO_LE)
1213 unsupported_reloc_local(object, r_type);
1214 break;
1215
0ffd9845
ILT
1216 case elfcpp::R_X86_64_DTPOFF32:
1217 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
1218 break;
1219
56622147 1220 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 1221 layout->set_has_static_tls();
7bf1f802
ILT
1222 if (optimized_type == tls::TLSOPT_NONE)
1223 {
1224 // Create a GOT entry for the tp-relative offset.
1225 Output_data_got<64, false>* got
1226 = target->got_section(symtab, layout);
1227 unsigned int r_sym = elfcpp::elf_r_sym<64>(reloc.get_r_info());
0a65a3a7 1228 got->add_local_with_rela(object, r_sym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1229 target->rela_dyn_section(layout),
1230 elfcpp::R_X86_64_TPOFF64);
1231 }
1232 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
1233 unsupported_reloc_local(object, r_type);
1234 break;
0ffd9845 1235
56622147 1236 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 1237 layout->set_has_static_tls();
7bf1f802
ILT
1238 if (output_is_shared)
1239 unsupported_reloc_local(object, r_type);
2e30d253 1240 break;
e041f13d
ILT
1241
1242 default:
1243 gold_unreachable();
2e30d253
ILT
1244 }
1245 }
1246 break;
2e30d253 1247
fdc2f80f
ILT
1248 case elfcpp::R_X86_64_SIZE32:
1249 case elfcpp::R_X86_64_SIZE64:
2e30d253 1250 default:
75f2446e
ILT
1251 gold_error(_("%s: unsupported reloc %u against local symbol"),
1252 object->name().c_str(), r_type);
2e30d253
ILT
1253 break;
1254 }
1255}
1256
1257
e041f13d
ILT
1258// Report an unsupported relocation against a global symbol.
1259
1260void
1261Target_x86_64::Scan::unsupported_reloc_global(Sized_relobj<64, false>* object,
1262 unsigned int r_type,
1263 Symbol* gsym)
1264{
75f2446e 1265 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12 1266 object->name().c_str(), r_type, gsym->demangled_name().c_str());
e041f13d
ILT
1267}
1268
2e30d253
ILT
1269// Scan a relocation for a global symbol.
1270
1271inline void
12c0daef 1272Target_x86_64::Scan::global(const General_options&,
d61c17ea
ILT
1273 Symbol_table* symtab,
1274 Layout* layout,
1275 Target_x86_64* target,
1276 Sized_relobj<64, false>* object,
1277 unsigned int data_shndx,
4f4c5f80 1278 Output_section* output_section,
d61c17ea
ILT
1279 const elfcpp::Rela<64, false>& reloc,
1280 unsigned int r_type,
1281 Symbol* gsym)
2e30d253
ILT
1282{
1283 switch (r_type)
1284 {
1285 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
1286 case elfcpp::R_386_GNU_VTINHERIT:
1287 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
1288 break;
1289
1290 case elfcpp::R_X86_64_64:
2e30d253
ILT
1291 case elfcpp::R_X86_64_32:
1292 case elfcpp::R_X86_64_32S:
2e30d253 1293 case elfcpp::R_X86_64_16:
2e30d253 1294 case elfcpp::R_X86_64_8:
96f2030e 1295 {
d61c6bd4
ILT
1296 // Make a PLT entry if necessary.
1297 if (gsym->needs_plt_entry())
1298 {
1299 target->make_plt_entry(symtab, layout, gsym);
1300 // Since this is not a PC-relative relocation, we may be
1301 // taking the address of a function. In that case we need to
1302 // set the entry in the dynamic symbol table to the address of
1303 // the PLT entry.
8851ecca 1304 if (gsym->is_from_dynobj() && !parameters->options().shared())
d61c6bd4
ILT
1305 gsym->set_needs_dynsym_value();
1306 }
1307 // Make a dynamic relocation if necessary.
0700cf32 1308 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
d61c6bd4
ILT
1309 {
1310 if (target->may_need_copy_reloc(gsym))
1311 {
12c0daef 1312 target->copy_reloc(symtab, layout, object,
7bf1f802 1313 data_shndx, output_section, gsym, reloc);
d61c6bd4
ILT
1314 }
1315 else if (r_type == elfcpp::R_X86_64_64
1316 && gsym->can_use_relative_reloc(false))
1317 {
1318 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
e8c846c3
ILT
1319 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE,
1320 output_section, object,
1321 data_shndx, reloc.get_r_offset(),
1322 reloc.get_r_addend());
d61c6bd4
ILT
1323 }
1324 else
1325 {
a036edd8 1326 this->check_non_pic(object, r_type);
96f2030e 1327 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
1328 rela_dyn->add_global(gsym, r_type, output_section, object,
1329 data_shndx, reloc.get_r_offset(),
96f2030e 1330 reloc.get_r_addend());
d61c6bd4
ILT
1331 }
1332 }
1333 }
1334 break;
1335
1336 case elfcpp::R_X86_64_PC64:
1337 case elfcpp::R_X86_64_PC32:
1338 case elfcpp::R_X86_64_PC16:
1339 case elfcpp::R_X86_64_PC8:
1340 {
1341 // Make a PLT entry if necessary.
1342 if (gsym->needs_plt_entry())
1343 target->make_plt_entry(symtab, layout, gsym);
1344 // Make a dynamic relocation if necessary.
0700cf32
ILT
1345 int flags = Symbol::NON_PIC_REF;
1346 if (gsym->type() == elfcpp::STT_FUNC)
1347 flags |= Symbol::FUNCTION_CALL;
1348 if (gsym->needs_dynamic_reloc(flags))
86849f1f 1349 {
d61c6bd4
ILT
1350 if (target->may_need_copy_reloc(gsym))
1351 {
12c0daef 1352 target->copy_reloc(symtab, layout, object,
7bf1f802 1353 data_shndx, output_section, gsym, reloc);
d61c6bd4 1354 }
86849f1f 1355 else
d61c6bd4 1356 {
a036edd8 1357 this->check_non_pic(object, r_type);
d61c6bd4 1358 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
4f4c5f80
ILT
1359 rela_dyn->add_global(gsym, r_type, output_section, object,
1360 data_shndx, reloc.get_r_offset(),
d61c6bd4
ILT
1361 reloc.get_r_addend());
1362 }
86849f1f 1363 }
d61c6bd4 1364 }
2e30d253
ILT
1365 break;
1366
ff006520 1367 case elfcpp::R_X86_64_GOT64:
2e30d253 1368 case elfcpp::R_X86_64_GOT32:
ff006520
ILT
1369 case elfcpp::R_X86_64_GOTPCREL64:
1370 case elfcpp::R_X86_64_GOTPCREL:
1371 case elfcpp::R_X86_64_GOTPLT64:
2e30d253
ILT
1372 {
1373 // The symbol requires a GOT entry.
1374 Output_data_got<64, false>* got = target->got_section(symtab, layout);
7bf1f802 1375 if (gsym->final_value_is_known())
0a65a3a7 1376 got->add_global(gsym, GOT_TYPE_STANDARD);
7bf1f802
ILT
1377 else
1378 {
2e30d253
ILT
1379 // If this symbol is not fully resolved, we need to add a
1380 // dynamic relocation for it.
7bf1f802 1381 Reloc_section* rela_dyn = target->rela_dyn_section(layout);
8fc19601
ILT
1382 if (gsym->is_from_dynobj()
1383 || gsym->is_undefined()
1384 || gsym->is_preemptible())
0a65a3a7 1385 got->add_global_with_rela(gsym, GOT_TYPE_STANDARD, rela_dyn,
7bf1f802
ILT
1386 elfcpp::R_X86_64_GLOB_DAT);
1387 else
2e30d253 1388 {
0a65a3a7
CC
1389 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1390 rela_dyn->add_global_relative(
1391 gsym, elfcpp::R_X86_64_RELATIVE, got,
1392 gsym->got_offset(GOT_TYPE_STANDARD), 0);
2e30d253
ILT
1393 }
1394 }
ee9e9e86
ILT
1395 // For GOTPLT64, we also need a PLT entry (but only if the
1396 // symbol is not fully resolved).
1397 if (r_type == elfcpp::R_X86_64_GOTPLT64
1398 && !gsym->final_value_is_known())
1399 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1400 }
1401 break;
1402
1403 case elfcpp::R_X86_64_PLT32:
1404 // If the symbol is fully resolved, this is just a PC32 reloc.
1405 // Otherwise we need a PLT entry.
1406 if (gsym->final_value_is_known())
1407 break;
96f2030e
ILT
1408 // If building a shared library, we can also skip the PLT entry
1409 // if the symbol is defined in the output file and is protected
1410 // or hidden.
1411 if (gsym->is_defined()
1412 && !gsym->is_from_dynobj()
1413 && !gsym->is_preemptible())
1414 break;
2e30d253
ILT
1415 target->make_plt_entry(symtab, layout, gsym);
1416 break;
1417
fdc2f80f 1418 case elfcpp::R_X86_64_GOTPC32:
e822f2b1 1419 case elfcpp::R_X86_64_GOTOFF64:
fdc2f80f
ILT
1420 case elfcpp::R_X86_64_GOTPC64:
1421 case elfcpp::R_X86_64_PLTOFF64:
2e30d253
ILT
1422 // We need a GOT section.
1423 target->got_section(symtab, layout);
ee9e9e86
ILT
1424 // For PLTOFF64, we also need a PLT entry (but only if the
1425 // symbol is not fully resolved).
1426 if (r_type == elfcpp::R_X86_64_PLTOFF64
1427 && !gsym->final_value_is_known())
1428 target->make_plt_entry(symtab, layout, gsym);
2e30d253
ILT
1429 break;
1430
2e30d253
ILT
1431 case elfcpp::R_X86_64_COPY:
1432 case elfcpp::R_X86_64_GLOB_DAT:
1433 case elfcpp::R_X86_64_JUMP_SLOT:
1434 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1435 // These are outstanding tls relocs, which are unexpected when linking
e822f2b1 1436 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1437 case elfcpp::R_X86_64_DTPMOD64:
e822f2b1 1438 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1439 gold_error(_("%s: unexpected reloc %u in object file"),
1440 object->name().c_str(), r_type);
2e30d253 1441 break;
2e30d253 1442
d61c17ea 1443 // These are initial tls relocs, which are expected for global()
56622147
ILT
1444 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1445 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1446 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1447 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1448 case elfcpp::R_X86_64_DTPOFF32:
1449 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1450 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1451 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2e30d253
ILT
1452 {
1453 const bool is_final = gsym->final_value_is_known();
e041f13d
ILT
1454 const tls::Tls_optimization optimized_type
1455 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
1456 switch (r_type)
1457 {
56622147 1458 case elfcpp::R_X86_64_TLSGD: // General-dynamic
7bf1f802
ILT
1459 if (optimized_type == tls::TLSOPT_NONE)
1460 {
1461 // Create a pair of GOT entries for the module index and
1462 // dtv-relative offset.
1463 Output_data_got<64, false>* got
1464 = target->got_section(symtab, layout);
0a65a3a7
CC
1465 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_PAIR,
1466 target->rela_dyn_section(layout),
1467 elfcpp::R_X86_64_DTPMOD64,
1468 elfcpp::R_X86_64_DTPOFF64);
7bf1f802
ILT
1469 }
1470 else if (optimized_type == tls::TLSOPT_TO_IE)
1471 {
1472 // Create a GOT entry for the tp-relative offset.
1473 Output_data_got<64, false>* got
1474 = target->got_section(symtab, layout);
0a65a3a7 1475 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1476 target->rela_dyn_section(layout),
1477 elfcpp::R_X86_64_TPOFF64);
1478 }
1479 else if (optimized_type != tls::TLSOPT_TO_LE)
1480 unsupported_reloc_global(object, r_type, gsym);
1481 break;
1482
56622147 1483 case elfcpp::R_X86_64_GOTPC32_TLSDESC:
edfbb029 1484 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
1485 if (optimized_type == tls::TLSOPT_NONE)
1486 {
1487 // Create reserved PLT and GOT entries for the resolver.
1488 target->reserve_tlsdesc_entries(symtab, layout);
1489
1490 // Create a double GOT entry with an R_X86_64_TLSDESC reloc.
1491 Output_data_got<64, false>* got
1492 = target->got_section(symtab, layout);
1493 got->add_global_pair_with_rela(gsym, GOT_TYPE_TLS_DESC,
1494 target->rela_dyn_section(layout),
1495 elfcpp::R_X86_64_TLSDESC, 0);
1496 }
1497 else if (optimized_type == tls::TLSOPT_TO_IE)
1498 {
1499 // Create a GOT entry for the tp-relative offset.
1500 Output_data_got<64, false>* got
1501 = target->got_section(symtab, layout);
1502 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
1503 target->rela_dyn_section(layout),
1504 elfcpp::R_X86_64_TPOFF64);
1505 }
1506 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147 1507 unsupported_reloc_global(object, r_type, gsym);
2e30d253
ILT
1508 break;
1509
c2b45e22
CC
1510 case elfcpp::R_X86_64_TLSDESC_CALL:
1511 break;
1512
e041f13d 1513 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
7bf1f802
ILT
1514 if (optimized_type == tls::TLSOPT_NONE)
1515 {
1516 // Create a GOT entry for the module index.
31d60480 1517 target->got_mod_index_entry(symtab, layout, object);
7bf1f802
ILT
1518 }
1519 else if (optimized_type != tls::TLSOPT_TO_LE)
1520 unsupported_reloc_global(object, r_type, gsym);
1521 break;
1522
0ffd9845
ILT
1523 case elfcpp::R_X86_64_DTPOFF32:
1524 case elfcpp::R_X86_64_DTPOFF64:
e041f13d
ILT
1525 break;
1526
56622147 1527 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
535890bb 1528 layout->set_has_static_tls();
7bf1f802
ILT
1529 if (optimized_type == tls::TLSOPT_NONE)
1530 {
1531 // Create a GOT entry for the tp-relative offset.
1532 Output_data_got<64, false>* got
1533 = target->got_section(symtab, layout);
0a65a3a7 1534 got->add_global_with_rela(gsym, GOT_TYPE_TLS_OFFSET,
7bf1f802
ILT
1535 target->rela_dyn_section(layout),
1536 elfcpp::R_X86_64_TPOFF64);
1537 }
1538 else if (optimized_type != tls::TLSOPT_TO_LE)
56622147
ILT
1539 unsupported_reloc_global(object, r_type, gsym);
1540 break;
0ffd9845 1541
56622147 1542 case elfcpp::R_X86_64_TPOFF32: // Local-exec
535890bb 1543 layout->set_has_static_tls();
8851ecca 1544 if (parameters->options().shared())
7bf1f802 1545 unsupported_reloc_local(object, r_type);
2e30d253 1546 break;
e041f13d
ILT
1547
1548 default:
1549 gold_unreachable();
2e30d253
ILT
1550 }
1551 }
1552 break;
fdc2f80f
ILT
1553
1554 case elfcpp::R_X86_64_SIZE32:
1555 case elfcpp::R_X86_64_SIZE64:
2e30d253 1556 default:
75f2446e 1557 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12
ILT
1558 object->name().c_str(), r_type,
1559 gsym->demangled_name().c_str());
2e30d253
ILT
1560 break;
1561 }
1562}
1563
6d03d481
ST
1564void
1565Target_x86_64::gc_process_relocs(const General_options& options,
1566 Symbol_table* symtab,
1567 Layout* layout,
1568 Sized_relobj<64, false>* object,
1569 unsigned int data_shndx,
1570 unsigned int sh_type,
1571 const unsigned char* prelocs,
1572 size_t reloc_count,
1573 Output_section* output_section,
1574 bool needs_special_offset_handling,
1575 size_t local_symbol_count,
1576 const unsigned char* plocal_symbols)
1577{
1578
1579 if (sh_type == elfcpp::SHT_REL)
1580 {
1581 return;
1582 }
1583
1584 gold::gc_process_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1585 Target_x86_64::Scan>(
1586 options,
1587 symtab,
1588 layout,
1589 this,
1590 object,
1591 data_shndx,
1592 prelocs,
1593 reloc_count,
1594 output_section,
1595 needs_special_offset_handling,
1596 local_symbol_count,
1597 plocal_symbols);
1598
1599}
2e30d253
ILT
1600// Scan relocations for a section.
1601
1602void
1603Target_x86_64::scan_relocs(const General_options& options,
d61c17ea
ILT
1604 Symbol_table* symtab,
1605 Layout* layout,
1606 Sized_relobj<64, false>* object,
1607 unsigned int data_shndx,
1608 unsigned int sh_type,
1609 const unsigned char* prelocs,
1610 size_t reloc_count,
730cdc88
ILT
1611 Output_section* output_section,
1612 bool needs_special_offset_handling,
d61c17ea 1613 size_t local_symbol_count,
730cdc88 1614 const unsigned char* plocal_symbols)
2e30d253
ILT
1615{
1616 if (sh_type == elfcpp::SHT_REL)
1617 {
75f2446e
ILT
1618 gold_error(_("%s: unsupported REL reloc section"),
1619 object->name().c_str());
1620 return;
2e30d253
ILT
1621 }
1622
1623 gold::scan_relocs<64, false, Target_x86_64, elfcpp::SHT_RELA,
1624 Target_x86_64::Scan>(
1625 options,
1626 symtab,
1627 layout,
1628 this,
1629 object,
1630 data_shndx,
1631 prelocs,
1632 reloc_count,
730cdc88
ILT
1633 output_section,
1634 needs_special_offset_handling,
2e30d253 1635 local_symbol_count,
730cdc88 1636 plocal_symbols);
2e30d253
ILT
1637}
1638
1639// Finalize the sections.
1640
1641void
1642Target_x86_64::do_finalize_sections(Layout* layout)
1643{
1644 // Fill in some more dynamic tags.
1645 Output_data_dynamic* const odyn = layout->dynamic_data();
1646 if (odyn != NULL)
1647 {
1648 if (this->got_plt_ != NULL)
1649 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1650
1651 if (this->plt_ != NULL)
1652 {
1653 const Output_data* od = this->plt_->rel_plt();
1654 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1655 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1656 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_RELA);
c2b45e22
CC
1657 if (this->plt_->has_tlsdesc_entry())
1658 {
1659 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset();
1660 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset();
1661 this->got_->finalize_data_size();
1662 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT,
1663 this->plt_, plt_offset);
1664 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT,
1665 this->got_, got_offset);
1666 }
2e30d253
ILT
1667 }
1668
0ffd9845 1669 if (this->rela_dyn_ != NULL)
2e30d253 1670 {
0ffd9845 1671 const Output_data* od = this->rela_dyn_;
2e30d253 1672 odyn->add_section_address(elfcpp::DT_RELA, od);
e84992bb 1673 odyn->add_section_size(elfcpp::DT_RELASZ, od);
2e30d253 1674 odyn->add_constant(elfcpp::DT_RELAENT,
e84992bb 1675 elfcpp::Elf_sizes<64>::rela_size);
2e30d253
ILT
1676 }
1677
8851ecca 1678 if (!parameters->options().shared())
2e30d253
ILT
1679 {
1680 // The value of the DT_DEBUG tag is filled in by the dynamic
1681 // linker at run time, and used by the debugger.
1682 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1683 }
1684 }
1685
1686 // Emit any relocs we saved in an attempt to avoid generating COPY
1687 // relocs.
12c0daef
ILT
1688 if (this->copy_relocs_.any_saved_relocs())
1689 this->copy_relocs_.emit(this->rela_dyn_section(layout));
2e30d253
ILT
1690}
1691
1692// Perform a relocation.
1693
1694inline bool
1695Target_x86_64::Relocate::relocate(const Relocate_info<64, false>* relinfo,
1696 Target_x86_64* target,
031cdbed 1697 Output_section*,
2e30d253 1698 size_t relnum,
0ffd9845 1699 const elfcpp::Rela<64, false>& rela,
2e30d253
ILT
1700 unsigned int r_type,
1701 const Sized_symbol<64>* gsym,
1702 const Symbol_value<64>* psymval,
1703 unsigned char* view,
1704 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 1705 section_size_type view_size)
2e30d253
ILT
1706{
1707 if (this->skip_call_tls_get_addr_)
1708 {
5efc7cd2
CC
1709 if ((r_type != elfcpp::R_X86_64_PLT32
1710 && r_type != elfcpp::R_X86_64_PC32)
2e30d253 1711 || gsym == NULL
0ffd9845 1712 || strcmp(gsym->name(), "__tls_get_addr") != 0)
2e30d253 1713 {
75f2446e
ILT
1714 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1715 _("missing expected TLS relocation"));
1716 }
1717 else
1718 {
1719 this->skip_call_tls_get_addr_ = false;
1720 return false;
2e30d253 1721 }
2e30d253
ILT
1722 }
1723
1724 // Pick the value to use for symbols defined in shared objects.
1725 Symbol_value<64> symval;
96f2030e 1726 if (gsym != NULL
de4c45bd
ILT
1727 && gsym->use_plt_offset(r_type == elfcpp::R_X86_64_PC64
1728 || r_type == elfcpp::R_X86_64_PC32
1729 || r_type == elfcpp::R_X86_64_PC16
1730 || r_type == elfcpp::R_X86_64_PC8))
2e30d253
ILT
1731 {
1732 symval.set_output_value(target->plt_section()->address()
1733 + gsym->plt_offset());
1734 psymval = &symval;
1735 }
1736
1737 const Sized_relobj<64, false>* object = relinfo->object;
0ffd9845
ILT
1738 const elfcpp::Elf_Xword addend = rela.get_r_addend();
1739
1740 // Get the GOT offset if needed.
96f2030e
ILT
1741 // The GOT pointer points to the end of the GOT section.
1742 // We need to subtract the size of the GOT section to get
1743 // the actual offset to use in the relocation.
0ffd9845
ILT
1744 bool have_got_offset = false;
1745 unsigned int got_offset = 0;
1746 switch (r_type)
1747 {
1748 case elfcpp::R_X86_64_GOT32:
1749 case elfcpp::R_X86_64_GOT64:
1750 case elfcpp::R_X86_64_GOTPLT64:
1751 case elfcpp::R_X86_64_GOTPCREL:
1752 case elfcpp::R_X86_64_GOTPCREL64:
1753 if (gsym != NULL)
1754 {
0a65a3a7
CC
1755 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1756 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size();
0ffd9845
ILT
1757 }
1758 else
1759 {
1760 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
1761 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1762 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1763 - target->got_size());
0ffd9845
ILT
1764 }
1765 have_got_offset = true;
1766 break;
1767
1768 default:
1769 break;
1770 }
2e30d253
ILT
1771
1772 switch (r_type)
1773 {
1774 case elfcpp::R_X86_64_NONE:
e822f2b1
ILT
1775 case elfcpp::R_386_GNU_VTINHERIT:
1776 case elfcpp::R_386_GNU_VTENTRY:
2e30d253
ILT
1777 break;
1778
1779 case elfcpp::R_X86_64_64:
1780 Relocate_functions<64, false>::rela64(view, object, psymval, addend);
1781 break;
1782
1783 case elfcpp::R_X86_64_PC64:
1784 Relocate_functions<64, false>::pcrela64(view, object, psymval, addend,
1785 address);
1786 break;
1787
1788 case elfcpp::R_X86_64_32:
7bb3655e
ILT
1789 // FIXME: we need to verify that value + addend fits into 32 bits:
1790 // uint64_t x = value + addend;
1791 // x == static_cast<uint64_t>(static_cast<uint32_t>(x))
1792 // Likewise for other <=32-bit relocations (but see R_X86_64_32S).
2e30d253
ILT
1793 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1794 break;
1795
1796 case elfcpp::R_X86_64_32S:
7bb3655e
ILT
1797 // FIXME: we need to verify that value + addend fits into 32 bits:
1798 // int64_t x = value + addend; // note this quantity is signed!
1799 // x == static_cast<int64_t>(static_cast<int32_t>(x))
2e30d253
ILT
1800 Relocate_functions<64, false>::rela32(view, object, psymval, addend);
1801 break;
1802
1803 case elfcpp::R_X86_64_PC32:
1804 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1805 address);
1806 break;
1807
1808 case elfcpp::R_X86_64_16:
1809 Relocate_functions<64, false>::rela16(view, object, psymval, addend);
1810 break;
1811
1812 case elfcpp::R_X86_64_PC16:
1813 Relocate_functions<64, false>::pcrela16(view, object, psymval, addend,
1814 address);
1815 break;
1816
1817 case elfcpp::R_X86_64_8:
1818 Relocate_functions<64, false>::rela8(view, object, psymval, addend);
1819 break;
1820
1821 case elfcpp::R_X86_64_PC8:
1822 Relocate_functions<64, false>::pcrela8(view, object, psymval, addend,
1823 address);
1824 break;
1825
1826 case elfcpp::R_X86_64_PLT32:
f389a824
ILT
1827 gold_assert(gsym == NULL
1828 || gsym->has_plt_offset()
99f8faca
ILT
1829 || gsym->final_value_is_known()
1830 || (gsym->is_defined()
1831 && !gsym->is_from_dynobj()
1832 && !gsym->is_preemptible()));
ee9e9e86
ILT
1833 // Note: while this code looks the same as for R_X86_64_PC32, it
1834 // behaves differently because psymval was set to point to
1835 // the PLT entry, rather than the symbol, in Scan::global().
2e30d253
ILT
1836 Relocate_functions<64, false>::pcrela32(view, object, psymval, addend,
1837 address);
1838 break;
1839
ee9e9e86
ILT
1840 case elfcpp::R_X86_64_PLTOFF64:
1841 {
1842 gold_assert(gsym);
1843 gold_assert(gsym->has_plt_offset()
1844 || gsym->final_value_is_known());
1845 elfcpp::Elf_types<64>::Elf_Addr got_address;
1846 got_address = target->got_section(NULL, NULL)->address();
c1866bd5
ILT
1847 Relocate_functions<64, false>::rela64(view, object, psymval,
1848 addend - got_address);
ee9e9e86
ILT
1849 }
1850
2e30d253 1851 case elfcpp::R_X86_64_GOT32:
0ffd9845
ILT
1852 gold_assert(have_got_offset);
1853 Relocate_functions<64, false>::rela32(view, got_offset, addend);
2e30d253
ILT
1854 break;
1855
e822f2b1
ILT
1856 case elfcpp::R_X86_64_GOTPC32:
1857 {
1858 gold_assert(gsym);
1859 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1860 value = target->got_plt_section()->address();
e822f2b1
ILT
1861 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
1862 }
1863 break;
1864
1865 case elfcpp::R_X86_64_GOT64:
1866 // The ABI doc says "Like GOT64, but indicates a PLT entry is needed."
1867 // Since we always add a PLT entry, this is equivalent.
fdc2f80f 1868 case elfcpp::R_X86_64_GOTPLT64:
0ffd9845
ILT
1869 gold_assert(have_got_offset);
1870 Relocate_functions<64, false>::rela64(view, got_offset, addend);
e822f2b1
ILT
1871 break;
1872
1873 case elfcpp::R_X86_64_GOTPC64:
1874 {
1875 gold_assert(gsym);
1876 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1877 value = target->got_plt_section()->address();
e822f2b1
ILT
1878 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
1879 }
1880 break;
1881
2e30d253
ILT
1882 case elfcpp::R_X86_64_GOTOFF64:
1883 {
1884 elfcpp::Elf_types<64>::Elf_Addr value;
1885 value = (psymval->value(object, 0)
96f2030e 1886 - target->got_plt_section()->address());
2e30d253
ILT
1887 Relocate_functions<64, false>::rela64(view, value, addend);
1888 }
1889 break;
1890
1891 case elfcpp::R_X86_64_GOTPCREL:
1892 {
0ffd9845
ILT
1893 gold_assert(have_got_offset);
1894 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1895 value = target->got_plt_section()->address() + got_offset;
0ffd9845 1896 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2e30d253
ILT
1897 }
1898 break;
1899
e822f2b1
ILT
1900 case elfcpp::R_X86_64_GOTPCREL64:
1901 {
0ffd9845
ILT
1902 gold_assert(have_got_offset);
1903 elfcpp::Elf_types<64>::Elf_Addr value;
96f2030e 1904 value = target->got_plt_section()->address() + got_offset;
0ffd9845 1905 Relocate_functions<64, false>::pcrela64(view, value, addend, address);
e822f2b1
ILT
1906 }
1907 break;
1908
2e30d253
ILT
1909 case elfcpp::R_X86_64_COPY:
1910 case elfcpp::R_X86_64_GLOB_DAT:
1911 case elfcpp::R_X86_64_JUMP_SLOT:
1912 case elfcpp::R_X86_64_RELATIVE:
d61c17ea 1913 // These are outstanding tls relocs, which are unexpected when linking
2e30d253 1914 case elfcpp::R_X86_64_TPOFF64:
2e30d253 1915 case elfcpp::R_X86_64_DTPMOD64:
2e30d253 1916 case elfcpp::R_X86_64_TLSDESC:
75f2446e
ILT
1917 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1918 _("unexpected reloc %u in object file"),
1919 r_type);
2e30d253
ILT
1920 break;
1921
d61c17ea 1922 // These are initial tls relocs, which are expected when linking
56622147
ILT
1923 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
1924 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
e041f13d 1925 case elfcpp::R_X86_64_TLSDESC_CALL:
56622147 1926 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
0ffd9845
ILT
1927 case elfcpp::R_X86_64_DTPOFF32:
1928 case elfcpp::R_X86_64_DTPOFF64:
56622147
ILT
1929 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
1930 case elfcpp::R_X86_64_TPOFF32: // Local-exec
7bf1f802
ILT
1931 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval,
1932 view, address, view_size);
2e30d253 1933 break;
2e30d253 1934
fdc2f80f
ILT
1935 case elfcpp::R_X86_64_SIZE32:
1936 case elfcpp::R_X86_64_SIZE64:
2e30d253 1937 default:
75f2446e
ILT
1938 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
1939 _("unsupported reloc %u"),
1940 r_type);
2e30d253
ILT
1941 break;
1942 }
1943
1944 return true;
1945}
1946
1947// Perform a TLS relocation.
1948
1949inline void
d61c17ea 1950Target_x86_64::Relocate::relocate_tls(const Relocate_info<64, false>* relinfo,
7bf1f802 1951 Target_x86_64* target,
d61c17ea 1952 size_t relnum,
72ec2876 1953 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
1954 unsigned int r_type,
1955 const Sized_symbol<64>* gsym,
1956 const Symbol_value<64>* psymval,
1957 unsigned char* view,
6a41d30b 1958 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 1959 section_size_type view_size)
2e30d253 1960{
2e30d253 1961 Output_segment* tls_segment = relinfo->layout->tls_segment();
7bf1f802
ILT
1962
1963 const Sized_relobj<64, false>* object = relinfo->object;
6a41d30b 1964 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2e30d253
ILT
1965
1966 elfcpp::Elf_types<64>::Elf_Addr value = psymval->value(relinfo->object, 0);
1967
1968 const bool is_final = (gsym == NULL
8851ecca 1969 ? !parameters->options().output_is_position_independent()
2e30d253 1970 : gsym->final_value_is_known());
e041f13d
ILT
1971 const tls::Tls_optimization optimized_type
1972 = Target_x86_64::optimize_tls_reloc(is_final, r_type);
2e30d253
ILT
1973 switch (r_type)
1974 {
56622147 1975 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
497897f9 1976 this->saw_tls_block_reloc_ = true;
e041f13d 1977 if (optimized_type == tls::TLSOPT_TO_LE)
2e30d253 1978 {
7bf1f802 1979 gold_assert(tls_segment != NULL);
2e30d253 1980 this->tls_gd_to_le(relinfo, relnum, tls_segment,
72ec2876 1981 rela, r_type, value, view,
2e30d253
ILT
1982 view_size);
1983 break;
1984 }
7bf1f802
ILT
1985 else
1986 {
c2b45e22
CC
1987 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1988 ? GOT_TYPE_TLS_OFFSET
1989 : GOT_TYPE_TLS_PAIR);
7bf1f802
ILT
1990 unsigned int got_offset;
1991 if (gsym != NULL)
1992 {
c2b45e22
CC
1993 gold_assert(gsym->has_got_offset(got_type));
1994 got_offset = gsym->got_offset(got_type) - target->got_size();
7bf1f802
ILT
1995 }
1996 else
1997 {
1998 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
c2b45e22
CC
1999 gold_assert(object->local_has_got_offset(r_sym, got_type));
2000 got_offset = (object->local_got_offset(r_sym, got_type)
7bf1f802
ILT
2001 - target->got_size());
2002 }
2003 if (optimized_type == tls::TLSOPT_TO_IE)
2004 {
2005 gold_assert(tls_segment != NULL);
c2b45e22 2006 value = target->got_plt_section()->address() + got_offset;
7bf1f802 2007 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rela, r_type,
c2b45e22 2008 value, view, address, view_size);
7bf1f802
ILT
2009 break;
2010 }
2011 else if (optimized_type == tls::TLSOPT_NONE)
2012 {
2013 // Relocate the field with the offset of the pair of GOT
2014 // entries.
6a41d30b
ILT
2015 value = target->got_plt_section()->address() + got_offset;
2016 Relocate_functions<64, false>::pcrela32(view, value, addend,
2017 address);
7bf1f802
ILT
2018 break;
2019 }
2020 }
72ec2876 2021 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 2022 _("unsupported reloc %u"), r_type);
2e30d253
ILT
2023 break;
2024
c2b45e22
CC
2025 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2026 case elfcpp::R_X86_64_TLSDESC_CALL:
497897f9 2027 this->saw_tls_block_reloc_ = true;
c2b45e22
CC
2028 if (optimized_type == tls::TLSOPT_TO_LE)
2029 {
2030 gold_assert(tls_segment != NULL);
2031 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2032 rela, r_type, value, view,
2033 view_size);
2034 break;
2035 }
2036 else
2037 {
2038 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2039 ? GOT_TYPE_TLS_OFFSET
2040 : GOT_TYPE_TLS_DESC);
2041 unsigned int got_offset;
2042 if (gsym != NULL)
2043 {
2044 gold_assert(gsym->has_got_offset(got_type));
2045 got_offset = gsym->got_offset(got_type) - target->got_size();
2046 }
2047 else
2048 {
2049 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
2050 gold_assert(object->local_has_got_offset(r_sym, got_type));
2051 got_offset = (object->local_got_offset(r_sym, got_type)
2052 - target->got_size());
2053 }
2054 if (optimized_type == tls::TLSOPT_TO_IE)
2055 {
2056 gold_assert(tls_segment != NULL);
2057 value = target->got_plt_section()->address() + got_offset;
2058 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment,
2059 rela, r_type, value, view, address,
2060 view_size);
2061 break;
2062 }
2063 else if (optimized_type == tls::TLSOPT_NONE)
2064 {
2065 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2066 {
2067 // Relocate the field with the offset of the pair of GOT
2068 // entries.
2069 value = target->got_plt_section()->address() + got_offset;
2070 Relocate_functions<64, false>::pcrela32(view, value, addend,
2071 address);
2072 }
2073 break;
2074 }
2075 }
2076 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2077 _("unsupported reloc %u"), r_type);
2078 break;
2079
56622147 2080 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
497897f9 2081 this->saw_tls_block_reloc_ = true;
e041f13d
ILT
2082 if (optimized_type == tls::TLSOPT_TO_LE)
2083 {
7bf1f802 2084 gold_assert(tls_segment != NULL);
72ec2876
ILT
2085 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type,
2086 value, view, view_size);
2087 break;
e041f13d 2088 }
7bf1f802
ILT
2089 else if (optimized_type == tls::TLSOPT_NONE)
2090 {
2091 // Relocate the field with the offset of the GOT entry for
2092 // the module index.
2093 unsigned int got_offset;
31d60480
ILT
2094 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2095 - target->got_size());
6a41d30b
ILT
2096 value = target->got_plt_section()->address() + got_offset;
2097 Relocate_functions<64, false>::pcrela32(view, value, addend,
2098 address);
7bf1f802
ILT
2099 break;
2100 }
72ec2876 2101 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
75f2446e 2102 _("unsupported reloc %u"), r_type);
2e30d253 2103 break;
0ffd9845
ILT
2104
2105 case elfcpp::R_X86_64_DTPOFF32:
7bf1f802 2106 gold_assert(tls_segment != NULL);
e041f13d 2107 if (optimized_type == tls::TLSOPT_TO_LE)
497897f9
ILT
2108 {
2109 // This relocation type is used in debugging information.
2110 // In that case we need to not optimize the value. If we
2111 // haven't seen a TLSLD reloc, then we assume we should not
2112 // optimize this reloc.
2113 if (this->saw_tls_block_reloc_)
2114 value -= tls_segment->memsz();
2115 }
d85c80a3 2116 Relocate_functions<64, false>::rela32(view, value, addend);
0ffd9845
ILT
2117 break;
2118
2119 case elfcpp::R_X86_64_DTPOFF64:
7bf1f802 2120 gold_assert(tls_segment != NULL);
e041f13d 2121 if (optimized_type == tls::TLSOPT_TO_LE)
497897f9
ILT
2122 {
2123 // See R_X86_64_DTPOFF32, just above, for why we test this.
2124 if (this->saw_tls_block_reloc_)
2125 value -= tls_segment->memsz();
2126 }
d85c80a3 2127 Relocate_functions<64, false>::rela64(view, value, addend);
0ffd9845 2128 break;
2e30d253 2129
56622147
ILT
2130 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2131 if (optimized_type == tls::TLSOPT_TO_LE)
2132 {
7bf1f802 2133 gold_assert(tls_segment != NULL);
56622147
ILT
2134 Target_x86_64::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2135 rela, r_type, value, view,
2136 view_size);
2137 break;
2138 }
7bf1f802
ILT
2139 else if (optimized_type == tls::TLSOPT_NONE)
2140 {
2141 // Relocate the field with the offset of the GOT entry for
2142 // the tp-relative offset of the symbol.
2143 unsigned int got_offset;
2144 if (gsym != NULL)
2145 {
0a65a3a7
CC
2146 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET));
2147 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET)
2148 - target->got_size());
7bf1f802
ILT
2149 }
2150 else
2151 {
2152 unsigned int r_sym = elfcpp::elf_r_sym<64>(rela.get_r_info());
0a65a3a7
CC
2153 gold_assert(object->local_has_got_offset(r_sym,
2154 GOT_TYPE_TLS_OFFSET));
2155 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET)
7bf1f802
ILT
2156 - target->got_size());
2157 }
6a41d30b
ILT
2158 value = target->got_plt_section()->address() + got_offset;
2159 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
7bf1f802
ILT
2160 break;
2161 }
56622147
ILT
2162 gold_error_at_location(relinfo, relnum, rela.get_r_offset(),
2163 _("unsupported reloc type %u"),
2164 r_type);
2165 break;
0ffd9845 2166
56622147 2167 case elfcpp::R_X86_64_TPOFF32: // Local-exec
6a41d30b 2168 value -= tls_segment->memsz();
d85c80a3 2169 Relocate_functions<64, false>::rela32(view, value, addend);
56622147 2170 break;
2e30d253 2171 }
2e30d253
ILT
2172}
2173
7bf1f802
ILT
2174// Do a relocation in which we convert a TLS General-Dynamic to an
2175// Initial-Exec.
2176
2177inline void
2178Target_x86_64::Relocate::tls_gd_to_ie(const Relocate_info<64, false>* relinfo,
2179 size_t relnum,
c2b45e22 2180 Output_segment*,
7bf1f802
ILT
2181 const elfcpp::Rela<64, false>& rela,
2182 unsigned int,
2183 elfcpp::Elf_types<64>::Elf_Addr value,
2184 unsigned char* view,
c2b45e22 2185 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2186 section_size_type view_size)
7bf1f802
ILT
2187{
2188 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2189 // .word 0x6666; rex64; call __tls_get_addr
2190 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax
2191
2192 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2193 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2194
2195 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2196 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2197 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2198 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2199
2200 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 16);
2201
c2b45e22
CC
2202 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2203 Relocate_functions<64, false>::pcrela32(view + 8, value, addend - 8, address);
7bf1f802
ILT
2204
2205 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2206 // We can skip it.
2207 this->skip_call_tls_get_addr_ = true;
2208}
2209
e041f13d 2210// Do a relocation in which we convert a TLS General-Dynamic to a
2e30d253
ILT
2211// Local-Exec.
2212
2213inline void
d61c17ea
ILT
2214Target_x86_64::Relocate::tls_gd_to_le(const Relocate_info<64, false>* relinfo,
2215 size_t relnum,
2216 Output_segment* tls_segment,
72ec2876 2217 const elfcpp::Rela<64, false>& rela,
d61c17ea
ILT
2218 unsigned int,
2219 elfcpp::Elf_types<64>::Elf_Addr value,
2220 unsigned char* view,
fe8718a4 2221 section_size_type view_size)
2e30d253 2222{
0ffd9845
ILT
2223 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi;
2224 // .word 0x6666; rex64; call __tls_get_addr
2225 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax
2e30d253 2226
72ec2876
ILT
2227 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -4);
2228 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12);
2e30d253 2229
72ec2876
ILT
2230 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2231 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0));
2232 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2233 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0));
2e30d253 2234
0ffd9845 2235 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 16);
2e30d253 2236
6a41d30b 2237 value -= tls_segment->memsz();
0ffd9845 2238 Relocate_functions<64, false>::rela32(view + 8, value, 0);
2e30d253
ILT
2239
2240 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2241 // We can skip it.
2242 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
2243}
2244
c2b45e22
CC
2245// Do a TLSDESC-style General-Dynamic to Initial-Exec transition.
2246
2247inline void
2248Target_x86_64::Relocate::tls_desc_gd_to_ie(
2249 const Relocate_info<64, false>* relinfo,
2250 size_t relnum,
2251 Output_segment*,
2252 const elfcpp::Rela<64, false>& rela,
2253 unsigned int r_type,
2254 elfcpp::Elf_types<64>::Elf_Addr value,
2255 unsigned char* view,
2256 elfcpp::Elf_types<64>::Elf_Addr address,
2257 section_size_type view_size)
2258{
2259 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2260 {
2261 // leaq foo@tlsdesc(%rip), %rax
2262 // ==> movq foo@gottpoff(%rip), %rax
2263 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2264 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2265 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2266 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2267 view[-2] = 0x8b;
2268 const elfcpp::Elf_Xword addend = rela.get_r_addend();
2269 Relocate_functions<64, false>::pcrela32(view, value, addend, address);
2270 }
2271 else
2272 {
2273 // call *foo@tlscall(%rax)
2274 // ==> nop; nop
2275 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2276 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2277 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2278 view[0] == 0xff && view[1] == 0x10);
2279 view[0] = 0x66;
2280 view[1] = 0x90;
2281 }
2282}
2283
2284// Do a TLSDESC-style General-Dynamic to Local-Exec transition.
2285
2286inline void
2287Target_x86_64::Relocate::tls_desc_gd_to_le(
2288 const Relocate_info<64, false>* relinfo,
2289 size_t relnum,
2290 Output_segment* tls_segment,
2291 const elfcpp::Rela<64, false>& rela,
2292 unsigned int r_type,
2293 elfcpp::Elf_types<64>::Elf_Addr value,
2294 unsigned char* view,
2295 section_size_type view_size)
2296{
2297 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC)
2298 {
2299 // leaq foo@tlsdesc(%rip), %rax
2300 // ==> movq foo@tpoff, %rax
2301 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2302 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2303 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2304 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05);
2305 view[-2] = 0xc7;
2306 view[-1] = 0xc0;
2307 value -= tls_segment->memsz();
2308 Relocate_functions<64, false>::rela32(view, value, 0);
2309 }
2310 else
2311 {
2312 // call *foo@tlscall(%rax)
2313 // ==> nop; nop
2314 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL);
2315 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2);
2316 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2317 view[0] == 0xff && view[1] == 0x10);
2318 view[0] = 0x66;
2319 view[1] = 0x90;
2320 }
2321}
2322
2e30d253 2323inline void
72ec2876
ILT
2324Target_x86_64::Relocate::tls_ld_to_le(const Relocate_info<64, false>* relinfo,
2325 size_t relnum,
2326 Output_segment*,
2327 const elfcpp::Rela<64, false>& rela,
2328 unsigned int,
2329 elfcpp::Elf_types<64>::Elf_Addr,
2330 unsigned char* view,
fe8718a4 2331 section_size_type view_size)
2e30d253 2332{
72ec2876
ILT
2333 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt;
2334 // ... leq foo@dtpoff(%rax),%reg
2335 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx
2e30d253 2336
72ec2876
ILT
2337 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2338 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9);
2e30d253 2339
72ec2876
ILT
2340 tls::check_tls(relinfo, relnum, rela.get_r_offset(),
2341 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d);
2342
2343 tls::check_tls(relinfo, relnum, rela.get_r_offset(), view[4] == 0xe8);
2344
2345 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12);
2346
2347 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2348 // We can skip it.
2349 this->skip_call_tls_get_addr_ = true;
2e30d253
ILT
2350}
2351
56622147
ILT
2352// Do a relocation in which we convert a TLS Initial-Exec to a
2353// Local-Exec.
2354
2355inline void
2356Target_x86_64::Relocate::tls_ie_to_le(const Relocate_info<64, false>* relinfo,
2357 size_t relnum,
2358 Output_segment* tls_segment,
2359 const elfcpp::Rela<64, false>& rela,
2360 unsigned int,
2361 elfcpp::Elf_types<64>::Elf_Addr value,
2362 unsigned char* view,
fe8718a4 2363 section_size_type view_size)
56622147
ILT
2364{
2365 // We need to examine the opcodes to figure out which instruction we
2366 // are looking at.
2367
2368 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg
2369 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg
2370
2371 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3);
2372 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4);
2373
2374 unsigned char op1 = view[-3];
2375 unsigned char op2 = view[-2];
2376 unsigned char op3 = view[-1];
2377 unsigned char reg = op3 >> 3;
2378
2379 if (op2 == 0x8b)
2380 {
2381 // movq
2382 if (op1 == 0x4c)
2383 view[-3] = 0x49;
2384 view[-2] = 0xc7;
2385 view[-1] = 0xc0 | reg;
2386 }
2387 else if (reg == 4)
2388 {
2389 // Special handling for %rsp.
2390 if (op1 == 0x4c)
2391 view[-3] = 0x49;
2392 view[-2] = 0x81;
2393 view[-1] = 0xc0 | reg;
2394 }
2395 else
2396 {
2397 // addq
2398 if (op1 == 0x4c)
2399 view[-3] = 0x4d;
2400 view[-2] = 0x8d;
2401 view[-1] = 0x80 | reg | (reg << 3);
2402 }
2403
6a41d30b 2404 value -= tls_segment->memsz();
56622147
ILT
2405 Relocate_functions<64, false>::rela32(view, value, 0);
2406}
2407
2e30d253
ILT
2408// Relocate section data.
2409
2410void
2411Target_x86_64::relocate_section(const Relocate_info<64, false>* relinfo,
d61c17ea
ILT
2412 unsigned int sh_type,
2413 const unsigned char* prelocs,
2414 size_t reloc_count,
730cdc88
ILT
2415 Output_section* output_section,
2416 bool needs_special_offset_handling,
d61c17ea
ILT
2417 unsigned char* view,
2418 elfcpp::Elf_types<64>::Elf_Addr address,
fe8718a4 2419 section_size_type view_size)
2e30d253
ILT
2420{
2421 gold_assert(sh_type == elfcpp::SHT_RELA);
2422
2423 gold::relocate_section<64, false, Target_x86_64, elfcpp::SHT_RELA,
2424 Target_x86_64::Relocate>(
2425 relinfo,
2426 this,
2427 prelocs,
2428 reloc_count,
730cdc88
ILT
2429 output_section,
2430 needs_special_offset_handling,
2e30d253
ILT
2431 view,
2432 address,
2433 view_size);
2434}
2435
6a74a719
ILT
2436// Return the size of a relocation while scanning during a relocatable
2437// link.
2438
2439unsigned int
2440Target_x86_64::Relocatable_size_for_reloc::get_size_for_reloc(
2441 unsigned int r_type,
2442 Relobj* object)
2443{
2444 switch (r_type)
2445 {
2446 case elfcpp::R_X86_64_NONE:
2447 case elfcpp::R_386_GNU_VTINHERIT:
2448 case elfcpp::R_386_GNU_VTENTRY:
2449 case elfcpp::R_X86_64_TLSGD: // Global-dynamic
2450 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url)
2451 case elfcpp::R_X86_64_TLSDESC_CALL:
2452 case elfcpp::R_X86_64_TLSLD: // Local-dynamic
2453 case elfcpp::R_X86_64_DTPOFF32:
2454 case elfcpp::R_X86_64_DTPOFF64:
2455 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec
2456 case elfcpp::R_X86_64_TPOFF32: // Local-exec
2457 return 0;
2458
2459 case elfcpp::R_X86_64_64:
2460 case elfcpp::R_X86_64_PC64:
2461 case elfcpp::R_X86_64_GOTOFF64:
2462 case elfcpp::R_X86_64_GOTPC64:
2463 case elfcpp::R_X86_64_PLTOFF64:
2464 case elfcpp::R_X86_64_GOT64:
2465 case elfcpp::R_X86_64_GOTPCREL64:
2466 case elfcpp::R_X86_64_GOTPCREL:
2467 case elfcpp::R_X86_64_GOTPLT64:
2468 return 8;
2469
2470 case elfcpp::R_X86_64_32:
2471 case elfcpp::R_X86_64_32S:
2472 case elfcpp::R_X86_64_PC32:
2473 case elfcpp::R_X86_64_PLT32:
2474 case elfcpp::R_X86_64_GOTPC32:
2475 case elfcpp::R_X86_64_GOT32:
2476 return 4;
2477
2478 case elfcpp::R_X86_64_16:
2479 case elfcpp::R_X86_64_PC16:
2480 return 2;
2481
2482 case elfcpp::R_X86_64_8:
2483 case elfcpp::R_X86_64_PC8:
2484 return 1;
2485
2486 case elfcpp::R_X86_64_COPY:
2487 case elfcpp::R_X86_64_GLOB_DAT:
2488 case elfcpp::R_X86_64_JUMP_SLOT:
2489 case elfcpp::R_X86_64_RELATIVE:
2490 // These are outstanding tls relocs, which are unexpected when linking
2491 case elfcpp::R_X86_64_TPOFF64:
2492 case elfcpp::R_X86_64_DTPMOD64:
2493 case elfcpp::R_X86_64_TLSDESC:
2494 object->error(_("unexpected reloc %u in object file"), r_type);
2495 return 0;
2496
2497 case elfcpp::R_X86_64_SIZE32:
2498 case elfcpp::R_X86_64_SIZE64:
2499 default:
2500 object->error(_("unsupported reloc %u against local symbol"), r_type);
2501 return 0;
2502 }
2503}
2504
2505// Scan the relocs during a relocatable link.
2506
2507void
2508Target_x86_64::scan_relocatable_relocs(const General_options& options,
2509 Symbol_table* symtab,
2510 Layout* layout,
2511 Sized_relobj<64, false>* object,
2512 unsigned int data_shndx,
2513 unsigned int sh_type,
2514 const unsigned char* prelocs,
2515 size_t reloc_count,
2516 Output_section* output_section,
2517 bool needs_special_offset_handling,
2518 size_t local_symbol_count,
2519 const unsigned char* plocal_symbols,
2520 Relocatable_relocs* rr)
2521{
2522 gold_assert(sh_type == elfcpp::SHT_RELA);
2523
2524 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_RELA,
2525 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2526
7019cd25 2527 gold::scan_relocatable_relocs<64, false, elfcpp::SHT_RELA,
6a74a719
ILT
2528 Scan_relocatable_relocs>(
2529 options,
2530 symtab,
2531 layout,
2532 object,
2533 data_shndx,
2534 prelocs,
2535 reloc_count,
2536 output_section,
2537 needs_special_offset_handling,
2538 local_symbol_count,
2539 plocal_symbols,
2540 rr);
2541}
2542
2543// Relocate a section during a relocatable link.
2544
2545void
2546Target_x86_64::relocate_for_relocatable(
2547 const Relocate_info<64, false>* relinfo,
2548 unsigned int sh_type,
2549 const unsigned char* prelocs,
2550 size_t reloc_count,
2551 Output_section* output_section,
2552 off_t offset_in_output_section,
2553 const Relocatable_relocs* rr,
2554 unsigned char* view,
2555 elfcpp::Elf_types<64>::Elf_Addr view_address,
2556 section_size_type view_size,
2557 unsigned char* reloc_view,
2558 section_size_type reloc_view_size)
2559{
2560 gold_assert(sh_type == elfcpp::SHT_RELA);
2561
7019cd25 2562 gold::relocate_for_relocatable<64, false, elfcpp::SHT_RELA>(
6a74a719
ILT
2563 relinfo,
2564 prelocs,
2565 reloc_count,
2566 output_section,
2567 offset_in_output_section,
2568 rr,
2569 view,
2570 view_address,
2571 view_size,
2572 reloc_view,
2573 reloc_view_size);
2574}
2575
4fb6c25d
ILT
2576// Return the value to use for a dynamic which requires special
2577// treatment. This is how we support equality comparisons of function
2578// pointers across shared library boundaries, as described in the
2579// processor specific ABI supplement.
2580
2581uint64_t
2582Target_x86_64::do_dynsym_value(const Symbol* gsym) const
2583{
2584 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2585 return this->plt_section()->address() + gsym->plt_offset();
2586}
2587
2e30d253
ILT
2588// Return a string used to fill a code section with nops to take up
2589// the specified length.
2590
2591std::string
8851ecca 2592Target_x86_64::do_code_fill(section_size_type length) const
2e30d253
ILT
2593{
2594 if (length >= 16)
2595 {
2596 // Build a jmpq instruction to skip over the bytes.
2597 unsigned char jmp[5];
2598 jmp[0] = 0xe9;
04bf7072 2599 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2e30d253
ILT
2600 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2601 + std::string(length - 5, '\0'));
2602 }
2603
2604 // Nop sequences of various lengths.
2605 const char nop1[1] = { 0x90 }; // nop
2606 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2607 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2608 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2609 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2610 0x00 }; // leal 0(%esi,1),%esi
2611 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2612 0x00, 0x00 };
2613 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2614 0x00, 0x00, 0x00 };
2615 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2616 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2617 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2618 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2619 0x00 };
2620 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2621 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2622 0x00, 0x00 };
2623 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2624 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2625 0x00, 0x00, 0x00 };
2626 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2627 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2628 0x00, 0x00, 0x00, 0x00 };
2629 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2630 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2631 0x27, 0x00, 0x00, 0x00,
2632 0x00 };
2633 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2634 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2635 0xbc, 0x27, 0x00, 0x00,
2636 0x00, 0x00 };
2637 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2638 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2639 0x90, 0x90, 0x90, 0x90,
2640 0x90, 0x90, 0x90 };
2641
2642 const char* nops[16] = {
2643 NULL,
2644 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2645 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2646 };
2647
2648 return std::string(nops[length], length);
2649}
2650
2651// The selector for x86_64 object files.
2652
36959681 2653class Target_selector_x86_64 : public Target_selector_freebsd
2e30d253
ILT
2654{
2655public:
2656 Target_selector_x86_64()
36959681
ILT
2657 : Target_selector_freebsd(elfcpp::EM_X86_64, 64, false, "elf64-x86-64",
2658 "elf64-x86-64-freebsd")
2e30d253
ILT
2659 { }
2660
2661 Target*
e96caa79
ILT
2662 do_instantiate_target()
2663 { return new Target_x86_64(); }
36959681 2664
2e30d253
ILT
2665};
2666
2e30d253
ILT
2667Target_selector_x86_64 target_selector_x86_64;
2668
2669} // End anonymous namespace.
This page took 0.277924 seconds and 4 git commands to generate.