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