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