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