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