1 // x86_64.cc -- x86_64 target support for gold. 2 3 // Copyright (C) 2006-2018 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 "dwarf.h" 29 #include "parameters.h" 30 #include "reloc.h" 31 #include "x86_64.h" 32 #include "object.h" 33 #include "symtab.h" 34 #include "layout.h" 35 #include "output.h" 36 #include "copy-relocs.h" 37 #include "target.h" 38 #include "target-reloc.h" 39 #include "target-select.h" 40 #include "tls.h" 41 #include "freebsd.h" 42 #include "nacl.h" 43 #include "gc.h" 44 #include "icf.h" 45 46 namespace 47 { 48 49 using namespace gold; 50 51 // A class to handle the .got.plt section. 52 53 class Output_data_got_plt_x86_64 : public Output_section_data_build 54 { 55 public: 56 Output_data_got_plt_x86_64(Layout* layout) 57 : Output_section_data_build(8), 58 layout_(layout) 59 { } 60 61 Output_data_got_plt_x86_64(Layout* layout, off_t data_size) 62 : Output_section_data_build(data_size, 8), 63 layout_(layout) 64 { } 65 66 protected: 67 // Write out the PLT data. 68 void 69 do_write(Output_file*); 70 71 // Write to a map file. 72 void 73 do_print_to_mapfile(Mapfile* mapfile) const 74 { mapfile->print_output_data(this, "** GOT PLT"); } 75 76 private: 77 // A pointer to the Layout class, so that we can find the .dynamic 78 // section when we write out the GOT PLT section. 79 Layout* layout_; 80 }; 81 82 // A class to handle the PLT data. 83 // This is an abstract base class that handles most of the linker details 84 // but does not know the actual contents of PLT entries. The derived 85 // classes below fill in those details. 86 87 template<int size> 88 class Output_data_plt_x86_64 : public Output_section_data 89 { 90 public: 91 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section; 92 93 Output_data_plt_x86_64(Layout* layout, uint64_t addralign, 94 Output_data_got<64, false>* got, 95 Output_data_got_plt_x86_64* got_plt, 96 Output_data_space* got_irelative) 97 : Output_section_data(addralign), tlsdesc_rel_(NULL), 98 irelative_rel_(NULL), got_(got), got_plt_(got_plt), 99 got_irelative_(got_irelative), count_(0), irelative_count_(0), 100 tlsdesc_got_offset_(-1U), free_list_() 101 { this->init(layout); } 102 103 Output_data_plt_x86_64(Layout* layout, uint64_t plt_entry_size, 104 Output_data_got<64, false>* got, 105 Output_data_got_plt_x86_64* got_plt, 106 Output_data_space* got_irelative, 107 unsigned int plt_count) 108 : Output_section_data((plt_count + 1) * plt_entry_size, 109 plt_entry_size, false), 110 tlsdesc_rel_(NULL), irelative_rel_(NULL), got_(got), 111 got_plt_(got_plt), got_irelative_(got_irelative), count_(plt_count), 112 irelative_count_(0), tlsdesc_got_offset_(-1U), free_list_() 113 { 114 this->init(layout); 115 116 // Initialize the free list and reserve the first entry. 117 this->free_list_.init((plt_count + 1) * plt_entry_size, false); 118 this->free_list_.remove(0, plt_entry_size); 119 } 120 121 // Initialize the PLT section. 122 void 123 init(Layout* layout); 124 125 // Add an entry to the PLT. 126 void 127 add_entry(Symbol_table*, Layout*, Symbol* gsym); 128 129 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. 130 unsigned int 131 add_local_ifunc_entry(Symbol_table* symtab, Layout*, 132 Sized_relobj_file<size, false>* relobj, 133 unsigned int local_sym_index); 134 135 // Add the relocation for a PLT entry. 136 void 137 add_relocation(Symbol_table*, Layout*, Symbol* gsym, 138 unsigned int got_offset); 139 140 // Add the reserved TLSDESC_PLT entry to the PLT. 141 void 142 reserve_tlsdesc_entry(unsigned int got_offset) 143 { this->tlsdesc_got_offset_ = got_offset; } 144 145 // Return true if a TLSDESC_PLT entry has been reserved. 146 bool 147 has_tlsdesc_entry() const 148 { return this->tlsdesc_got_offset_ != -1U; } 149 150 // Return the GOT offset for the reserved TLSDESC_PLT entry. 151 unsigned int 152 get_tlsdesc_got_offset() const 153 { return this->tlsdesc_got_offset_; } 154 155 // Return the offset of the reserved TLSDESC_PLT entry. 156 unsigned int 157 get_tlsdesc_plt_offset() const 158 { 159 return ((this->count_ + this->irelative_count_ + 1) 160 * this->get_plt_entry_size()); 161 } 162 163 // Return the .rela.plt section data. 164 Reloc_section* 165 rela_plt() 166 { return this->rel_; } 167 168 // Return where the TLSDESC relocations should go. 169 Reloc_section* 170 rela_tlsdesc(Layout*); 171 172 // Return where the IRELATIVE relocations should go in the PLT 173 // relocations. 174 Reloc_section* 175 rela_irelative(Symbol_table*, Layout*); 176 177 // Return whether we created a section for IRELATIVE relocations. 178 bool 179 has_irelative_section() const 180 { return this->irelative_rel_ != NULL; } 181 182 // Get count of regular PLT entries. 183 unsigned int 184 regular_count() const 185 { return this->count_; } 186 187 // Return the total number of PLT entries. 188 unsigned int 189 entry_count() const 190 { return this->count_ + this->irelative_count_; } 191 192 // Return the offset of the first non-reserved PLT entry. 193 unsigned int 194 first_plt_entry_offset() 195 { return this->get_plt_entry_size(); } 196 197 // Return the size of a PLT entry. 198 unsigned int 199 get_plt_entry_size() const 200 { return this->do_get_plt_entry_size(); } 201 202 // Reserve a slot in the PLT for an existing symbol in an incremental update. 203 void 204 reserve_slot(unsigned int plt_index) 205 { 206 this->free_list_.remove((plt_index + 1) * this->get_plt_entry_size(), 207 (plt_index + 2) * this->get_plt_entry_size()); 208 } 209 210 // Return the PLT address to use for a global symbol. 211 uint64_t 212 address_for_global(const Symbol* sym) 213 { return do_address_for_global(sym); } 214 215 // Return the PLT address to use for a local symbol. 216 uint64_t 217 address_for_local(const Relobj* obj, unsigned int symndx) 218 { return do_address_for_local(obj, symndx); } 219 220 // Add .eh_frame information for the PLT. 221 void 222 add_eh_frame(Layout* layout) 223 { this->do_add_eh_frame(layout); } 224 225 protected: 226 Output_data_got<64, false>* 227 got() const 228 { return this->got_; } 229 230 Output_data_got_plt_x86_64* 231 got_plt() const 232 { return this->got_plt_; } 233 234 Output_data_space* 235 got_irelative() const 236 { return this->got_irelative_; } 237 238 // Fill in the first PLT entry. 239 void 240 fill_first_plt_entry(unsigned char* pov, 241 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 242 typename elfcpp::Elf_types<size>::Elf_Addr plt_address) 243 { this->do_fill_first_plt_entry(pov, got_address, plt_address); } 244 245 // Fill in a normal PLT entry. Returns the offset into the entry that 246 // should be the initial GOT slot value. 247 unsigned int 248 fill_plt_entry(unsigned char* pov, 249 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 250 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 251 unsigned int got_offset, 252 unsigned int plt_offset, 253 unsigned int plt_index) 254 { 255 return this->do_fill_plt_entry(pov, got_address, plt_address, 256 got_offset, plt_offset, plt_index); 257 } 258 259 // Fill in the reserved TLSDESC PLT entry. 260 void 261 fill_tlsdesc_entry(unsigned char* pov, 262 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 263 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 264 typename elfcpp::Elf_types<size>::Elf_Addr got_base, 265 unsigned int tlsdesc_got_offset, 266 unsigned int plt_offset) 267 { 268 this->do_fill_tlsdesc_entry(pov, got_address, plt_address, got_base, 269 tlsdesc_got_offset, plt_offset); 270 } 271 272 virtual unsigned int 273 do_get_plt_entry_size() const = 0; 274 275 virtual void 276 do_fill_first_plt_entry(unsigned char* pov, 277 typename elfcpp::Elf_types<size>::Elf_Addr got_addr, 278 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr) 279 = 0; 280 281 virtual unsigned int 282 do_fill_plt_entry(unsigned char* pov, 283 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 284 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 285 unsigned int got_offset, 286 unsigned int plt_offset, 287 unsigned int plt_index) = 0; 288 289 virtual void 290 do_fill_tlsdesc_entry(unsigned char* pov, 291 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 292 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 293 typename elfcpp::Elf_types<size>::Elf_Addr got_base, 294 unsigned int tlsdesc_got_offset, 295 unsigned int plt_offset) = 0; 296 297 // Return the PLT address to use for a global symbol. 298 virtual uint64_t 299 do_address_for_global(const Symbol* sym); 300 301 // Return the PLT address to use for a local symbol. 302 virtual uint64_t 303 do_address_for_local(const Relobj* obj, unsigned int symndx); 304 305 virtual void 306 do_add_eh_frame(Layout* layout) = 0; 307 308 void 309 do_adjust_output_section(Output_section* os); 310 311 // Write to a map file. 312 void 313 do_print_to_mapfile(Mapfile* mapfile) const 314 { mapfile->print_output_data(this, _("** PLT")); } 315 316 // The CIE of the .eh_frame unwind information for the PLT. 317 static const int plt_eh_frame_cie_size = 16; 318 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size]; 319 320 private: 321 // Set the final size. 322 void 323 set_final_data_size(); 324 325 // Write out the PLT data. 326 void 327 do_write(Output_file*); 328 329 // The reloc section. 330 Reloc_section* rel_; 331 // The TLSDESC relocs, if necessary. These must follow the regular 332 // PLT relocs. 333 Reloc_section* tlsdesc_rel_; 334 // The IRELATIVE relocs, if necessary. These must follow the 335 // regular PLT relocations and the TLSDESC relocations. 336 Reloc_section* irelative_rel_; 337 // The .got section. 338 Output_data_got<64, false>* got_; 339 // The .got.plt section. 340 Output_data_got_plt_x86_64* got_plt_; 341 // The part of the .got.plt section used for IRELATIVE relocs. 342 Output_data_space* got_irelative_; 343 // The number of PLT entries. 344 unsigned int count_; 345 // Number of PLT entries with R_X86_64_IRELATIVE relocs. These 346 // follow the regular PLT entries. 347 unsigned int irelative_count_; 348 // Offset of the reserved TLSDESC_GOT entry when needed. 349 unsigned int tlsdesc_got_offset_; 350 // List of available regions within the section, for incremental 351 // update links. 352 Free_list free_list_; 353 }; 354 355 template<int size> 356 class Output_data_plt_x86_64_standard : public Output_data_plt_x86_64<size> 357 { 358 public: 359 Output_data_plt_x86_64_standard(Layout* layout, 360 Output_data_got<64, false>* got, 361 Output_data_got_plt_x86_64* got_plt, 362 Output_data_space* got_irelative) 363 : Output_data_plt_x86_64<size>(layout, plt_entry_size, 364 got, got_plt, got_irelative) 365 { } 366 367 Output_data_plt_x86_64_standard(Layout* layout, 368 Output_data_got<64, false>* got, 369 Output_data_got_plt_x86_64* got_plt, 370 Output_data_space* got_irelative, 371 unsigned int plt_count) 372 : Output_data_plt_x86_64<size>(layout, plt_entry_size, 373 got, got_plt, got_irelative, 374 plt_count) 375 { } 376 377 protected: 378 virtual unsigned int 379 do_get_plt_entry_size() const 380 { return plt_entry_size; } 381 382 virtual void 383 do_add_eh_frame(Layout* layout) 384 { 385 layout->add_eh_frame_for_plt(this, 386 this->plt_eh_frame_cie, 387 this->plt_eh_frame_cie_size, 388 plt_eh_frame_fde, 389 plt_eh_frame_fde_size); 390 } 391 392 virtual void 393 do_fill_first_plt_entry(unsigned char* pov, 394 typename elfcpp::Elf_types<size>::Elf_Addr got_addr, 395 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr); 396 397 virtual unsigned int 398 do_fill_plt_entry(unsigned char* pov, 399 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 400 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 401 unsigned int got_offset, 402 unsigned int plt_offset, 403 unsigned int plt_index); 404 405 virtual void 406 do_fill_tlsdesc_entry(unsigned char* pov, 407 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 408 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 409 typename elfcpp::Elf_types<size>::Elf_Addr got_base, 410 unsigned int tlsdesc_got_offset, 411 unsigned int plt_offset); 412 413 private: 414 // The size of an entry in the PLT. 415 static const int plt_entry_size = 16; 416 417 // The first entry in the PLT. 418 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same 419 // procedure linkage table for both programs and shared objects." 420 static const unsigned char first_plt_entry[plt_entry_size]; 421 422 // Other entries in the PLT for an executable. 423 static const unsigned char plt_entry[plt_entry_size]; 424 425 // The reserved TLSDESC entry in the PLT for an executable. 426 static const unsigned char tlsdesc_plt_entry[plt_entry_size]; 427 428 // The .eh_frame unwind information for the PLT. 429 static const int plt_eh_frame_fde_size = 32; 430 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size]; 431 }; 432 433 class Output_data_plt_x86_64_bnd : public Output_data_plt_x86_64<64> 434 { 435 public: 436 Output_data_plt_x86_64_bnd(Layout* layout, 437 Output_data_got<64, false>* got, 438 Output_data_got_plt_x86_64* got_plt, 439 Output_data_space* got_irelative) 440 : Output_data_plt_x86_64<64>(layout, plt_entry_size, 441 got, got_plt, got_irelative), 442 aplt_offset_(0) 443 { } 444 445 Output_data_plt_x86_64_bnd(Layout* layout, 446 Output_data_got<64, false>* got, 447 Output_data_got_plt_x86_64* got_plt, 448 Output_data_space* got_irelative, 449 unsigned int plt_count) 450 : Output_data_plt_x86_64<64>(layout, plt_entry_size, 451 got, got_plt, got_irelative, 452 plt_count), 453 aplt_offset_(0) 454 { } 455 456 protected: 457 virtual unsigned int 458 do_get_plt_entry_size() const 459 { return plt_entry_size; } 460 461 // Return the PLT address to use for a global symbol. 462 uint64_t 463 do_address_for_global(const Symbol*); 464 465 // Return the PLT address to use for a local symbol. 466 uint64_t 467 do_address_for_local(const Relobj*, unsigned int symndx); 468 469 virtual void 470 do_add_eh_frame(Layout* layout) 471 { 472 layout->add_eh_frame_for_plt(this, 473 this->plt_eh_frame_cie, 474 this->plt_eh_frame_cie_size, 475 plt_eh_frame_fde, 476 plt_eh_frame_fde_size); 477 } 478 479 virtual void 480 do_fill_first_plt_entry(unsigned char* pov, 481 elfcpp::Elf_types<64>::Elf_Addr got_addr, 482 elfcpp::Elf_types<64>::Elf_Addr plt_addr); 483 484 virtual unsigned int 485 do_fill_plt_entry(unsigned char* pov, 486 elfcpp::Elf_types<64>::Elf_Addr got_address, 487 elfcpp::Elf_types<64>::Elf_Addr plt_address, 488 unsigned int got_offset, 489 unsigned int plt_offset, 490 unsigned int plt_index); 491 492 virtual void 493 do_fill_tlsdesc_entry(unsigned char* pov, 494 elfcpp::Elf_types<64>::Elf_Addr got_address, 495 elfcpp::Elf_types<64>::Elf_Addr plt_address, 496 elfcpp::Elf_types<64>::Elf_Addr got_base, 497 unsigned int tlsdesc_got_offset, 498 unsigned int plt_offset); 499 500 void 501 fill_aplt_entry(unsigned char* pov, 502 elfcpp::Elf_types<64>::Elf_Addr got_address, 503 elfcpp::Elf_types<64>::Elf_Addr plt_address, 504 unsigned int got_offset, 505 unsigned int plt_offset, 506 unsigned int plt_index); 507 508 private: 509 // Set the final size. 510 void 511 set_final_data_size(); 512 513 // Write out the BND PLT data. 514 void 515 do_write(Output_file*); 516 517 // Offset of the Additional PLT (if using -z bndplt). 518 unsigned int aplt_offset_; 519 520 // The size of an entry in the PLT. 521 static const int plt_entry_size = 16; 522 523 // The size of an entry in the additional PLT. 524 static const int aplt_entry_size = 8; 525 526 // The first entry in the PLT. 527 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same 528 // procedure linkage table for both programs and shared objects." 529 static const unsigned char first_plt_entry[plt_entry_size]; 530 531 // Other entries in the PLT for an executable. 532 static const unsigned char plt_entry[plt_entry_size]; 533 534 // Entries in the additional PLT. 535 static const unsigned char aplt_entry[aplt_entry_size]; 536 537 // The reserved TLSDESC entry in the PLT for an executable. 538 static const unsigned char tlsdesc_plt_entry[plt_entry_size]; 539 540 // The .eh_frame unwind information for the PLT. 541 static const int plt_eh_frame_fde_size = 32; 542 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size]; 543 }; 544 545 // We use this PLT when Indirect Branch Tracking (IBT) is enabled. 546 547 template <int size> 548 class Output_data_plt_x86_64_ibt : public Output_data_plt_x86_64<size> 549 { 550 public: 551 Output_data_plt_x86_64_ibt(Layout* layout, 552 Output_data_got<64, false>* got, 553 Output_data_got_plt_x86_64* got_plt, 554 Output_data_space* got_irelative) 555 : Output_data_plt_x86_64<size>(layout, plt_entry_size, 556 got, got_plt, got_irelative), 557 aplt_offset_(0) 558 { } 559 560 Output_data_plt_x86_64_ibt(Layout* layout, 561 Output_data_got<64, false>* got, 562 Output_data_got_plt_x86_64* got_plt, 563 Output_data_space* got_irelative, 564 unsigned int plt_count) 565 : Output_data_plt_x86_64<size>(layout, plt_entry_size, 566 got, got_plt, got_irelative, 567 plt_count), 568 aplt_offset_(0) 569 { } 570 571 protected: 572 virtual unsigned int 573 do_get_plt_entry_size() const 574 { return plt_entry_size; } 575 576 // Return the PLT address to use for a global symbol. 577 uint64_t 578 do_address_for_global(const Symbol*); 579 580 // Return the PLT address to use for a local symbol. 581 uint64_t 582 do_address_for_local(const Relobj*, unsigned int symndx); 583 584 virtual void 585 do_add_eh_frame(Layout* layout) 586 { 587 layout->add_eh_frame_for_plt(this, 588 this->plt_eh_frame_cie, 589 this->plt_eh_frame_cie_size, 590 plt_eh_frame_fde, 591 plt_eh_frame_fde_size); 592 } 593 594 virtual void 595 do_fill_first_plt_entry(unsigned char* pov, 596 typename elfcpp::Elf_types<size>::Elf_Addr got_addr, 597 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr); 598 599 virtual unsigned int 600 do_fill_plt_entry(unsigned char* pov, 601 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 602 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 603 unsigned int got_offset, 604 unsigned int plt_offset, 605 unsigned int plt_index); 606 607 virtual void 608 do_fill_tlsdesc_entry(unsigned char* pov, 609 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 610 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 611 typename elfcpp::Elf_types<size>::Elf_Addr got_base, 612 unsigned int tlsdesc_got_offset, 613 unsigned int plt_offset); 614 615 void 616 fill_aplt_entry(unsigned char* pov, 617 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 618 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 619 unsigned int got_offset, 620 unsigned int plt_offset, 621 unsigned int plt_index); 622 623 private: 624 // Set the final size. 625 void 626 set_final_data_size(); 627 628 // Write out the BND PLT data. 629 void 630 do_write(Output_file*); 631 632 // Offset of the Additional PLT (if using -z bndplt). 633 unsigned int aplt_offset_; 634 635 // The size of an entry in the PLT. 636 static const int plt_entry_size = 16; 637 638 // The size of an entry in the additional PLT. 639 static const int aplt_entry_size = 16; 640 641 // The first entry in the PLT. 642 // From the AMD64 ABI: "Unlike Intel386 ABI, this ABI uses the same 643 // procedure linkage table for both programs and shared objects." 644 static const unsigned char first_plt_entry[plt_entry_size]; 645 646 // Other entries in the PLT for an executable. 647 static const unsigned char plt_entry[plt_entry_size]; 648 649 // Entries in the additional PLT. 650 static const unsigned char aplt_entry[aplt_entry_size]; 651 652 // The reserved TLSDESC entry in the PLT for an executable. 653 static const unsigned char tlsdesc_plt_entry[plt_entry_size]; 654 655 // The .eh_frame unwind information for the PLT. 656 static const int plt_eh_frame_fde_size = 32; 657 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size]; 658 }; 659 660 template<int size> 661 class Lazy_view 662 { 663 public: 664 Lazy_view(Sized_relobj_file<size, false>* object, unsigned int data_shndx) 665 : object_(object), data_shndx_(data_shndx), view_(NULL), view_size_(0) 666 { } 667 668 inline unsigned char 669 operator[](size_t offset) 670 { 671 if (this->view_ == NULL) 672 this->view_ = this->object_->section_contents(this->data_shndx_, 673 &this->view_size_, 674 true); 675 if (offset >= this->view_size_) 676 return 0; 677 return this->view_[offset]; 678 } 679 680 private: 681 Sized_relobj_file<size, false>* object_; 682 unsigned int data_shndx_; 683 const unsigned char* view_; 684 section_size_type view_size_; 685 }; 686 687 // The x86_64 target class. 688 // See the ABI at 689 // http://www.x86-64.org/documentation/abi.pdf 690 // TLS info comes from 691 // http://people.redhat.com/drepper/tls.pdf 692 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt 693 694 template<int size> 695 class Target_x86_64 : public Sized_target<size, false> 696 { 697 public: 698 // In the x86_64 ABI (p 68), it says "The AMD64 ABI architectures 699 // uses only Elf64_Rela relocation entries with explicit addends." 700 typedef Output_data_reloc<elfcpp::SHT_RELA, true, size, false> Reloc_section; 701 702 Target_x86_64(const Target::Target_info* info = &x86_64_info) 703 : Sized_target<size, false>(info), 704 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL), 705 got_tlsdesc_(NULL), global_offset_table_(NULL), rela_dyn_(NULL), 706 rela_irelative_(NULL), copy_relocs_(elfcpp::R_X86_64_COPY), 707 got_mod_index_offset_(-1U), tlsdesc_reloc_info_(), 708 tls_base_symbol_defined_(false), isa_1_used_(0), isa_1_needed_(0), 709 feature_1_(0), object_feature_1_(0), seen_first_object_(false) 710 { } 711 712 // Hook for a new output section. 713 void 714 do_new_output_section(Output_section*) const; 715 716 // Scan the relocations to look for symbol adjustments. 717 void 718 gc_process_relocs(Symbol_table* symtab, 719 Layout* layout, 720 Sized_relobj_file<size, false>* object, 721 unsigned int data_shndx, 722 unsigned int sh_type, 723 const unsigned char* prelocs, 724 size_t reloc_count, 725 Output_section* output_section, 726 bool needs_special_offset_handling, 727 size_t local_symbol_count, 728 const unsigned char* plocal_symbols); 729 730 // Scan the relocations to look for symbol adjustments. 731 void 732 scan_relocs(Symbol_table* symtab, 733 Layout* layout, 734 Sized_relobj_file<size, false>* object, 735 unsigned int data_shndx, 736 unsigned int sh_type, 737 const unsigned char* prelocs, 738 size_t reloc_count, 739 Output_section* output_section, 740 bool needs_special_offset_handling, 741 size_t local_symbol_count, 742 const unsigned char* plocal_symbols); 743 744 // Finalize the sections. 745 void 746 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*); 747 748 // Return the value to use for a dynamic which requires special 749 // treatment. 750 uint64_t 751 do_dynsym_value(const Symbol*) const; 752 753 // Relocate a section. 754 void 755 relocate_section(const Relocate_info<size, false>*, 756 unsigned int sh_type, 757 const unsigned char* prelocs, 758 size_t reloc_count, 759 Output_section* output_section, 760 bool needs_special_offset_handling, 761 unsigned char* view, 762 typename elfcpp::Elf_types<size>::Elf_Addr view_address, 763 section_size_type view_size, 764 const Reloc_symbol_changes*); 765 766 // Scan the relocs during a relocatable link. 767 void 768 scan_relocatable_relocs(Symbol_table* symtab, 769 Layout* layout, 770 Sized_relobj_file<size, false>* object, 771 unsigned int data_shndx, 772 unsigned int sh_type, 773 const unsigned char* prelocs, 774 size_t reloc_count, 775 Output_section* output_section, 776 bool needs_special_offset_handling, 777 size_t local_symbol_count, 778 const unsigned char* plocal_symbols, 779 Relocatable_relocs*); 780 781 // Scan the relocs for --emit-relocs. 782 void 783 emit_relocs_scan(Symbol_table* symtab, 784 Layout* layout, 785 Sized_relobj_file<size, false>* object, 786 unsigned int data_shndx, 787 unsigned int sh_type, 788 const unsigned char* prelocs, 789 size_t reloc_count, 790 Output_section* output_section, 791 bool needs_special_offset_handling, 792 size_t local_symbol_count, 793 const unsigned char* plocal_syms, 794 Relocatable_relocs* rr); 795 796 // Emit relocations for a section. 797 void 798 relocate_relocs( 799 const Relocate_info<size, false>*, 800 unsigned int sh_type, 801 const unsigned char* prelocs, 802 size_t reloc_count, 803 Output_section* output_section, 804 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section, 805 unsigned char* view, 806 typename elfcpp::Elf_types<size>::Elf_Addr view_address, 807 section_size_type view_size, 808 unsigned char* reloc_view, 809 section_size_type reloc_view_size); 810 811 // Return a string used to fill a code section with nops. 812 std::string 813 do_code_fill(section_size_type length) const; 814 815 // Return whether SYM is defined by the ABI. 816 bool 817 do_is_defined_by_abi(const Symbol* sym) const 818 { return strcmp(sym->name(), "__tls_get_addr") == 0; } 819 820 // Return the symbol index to use for a target specific relocation. 821 // The only target specific relocation is R_X86_64_TLSDESC for a 822 // local symbol, which is an absolute reloc. 823 unsigned int 824 do_reloc_symbol_index(void*, unsigned int r_type) const 825 { 826 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC); 827 return 0; 828 } 829 830 // Return the addend to use for a target specific relocation. 831 uint64_t 832 do_reloc_addend(void* arg, unsigned int r_type, uint64_t addend) const; 833 834 // Return the PLT section. 835 uint64_t 836 do_plt_address_for_global(const Symbol* gsym) const 837 { return this->plt_section()->address_for_global(gsym); } 838 839 uint64_t 840 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const 841 { return this->plt_section()->address_for_local(relobj, symndx); } 842 843 // This function should be defined in targets that can use relocation 844 // types to determine (implemented in local_reloc_may_be_function_pointer 845 // and global_reloc_may_be_function_pointer) 846 // if a function's pointer is taken. ICF uses this in safe mode to only 847 // fold those functions whose pointer is defintely not taken. For x86_64 848 // pie binaries, safe ICF cannot be done by looking at only relocation 849 // types, and for certain cases (e.g. R_X86_64_PC32), the instruction 850 // opcode is checked as well to distinguish a function call from taking 851 // a function's pointer. 852 bool 853 do_can_check_for_function_pointers() const 854 { return true; } 855 856 // Return the base for a DW_EH_PE_datarel encoding. 857 uint64_t 858 do_ehframe_datarel_base() const; 859 860 // Adjust -fsplit-stack code which calls non-split-stack code. 861 void 862 do_calls_non_split(Relobj* object, unsigned int shndx, 863 section_offset_type fnoffset, section_size_type fnsize, 864 const unsigned char* prelocs, size_t reloc_count, 865 unsigned char* view, section_size_type view_size, 866 std::string* from, std::string* to) const; 867 868 // Return the size of the GOT section. 869 section_size_type 870 got_size() const 871 { 872 gold_assert(this->got_ != NULL); 873 return this->got_->data_size(); 874 } 875 876 // Return the number of entries in the GOT. 877 unsigned int 878 got_entry_count() const 879 { 880 if (this->got_ == NULL) 881 return 0; 882 return this->got_size() / 8; 883 } 884 885 // Return the number of entries in the PLT. 886 unsigned int 887 plt_entry_count() const; 888 889 // Return the offset of the first non-reserved PLT entry. 890 unsigned int 891 first_plt_entry_offset() const; 892 893 // Return the size of each PLT entry. 894 unsigned int 895 plt_entry_size() const; 896 897 // Return the size of each GOT entry. 898 unsigned int 899 got_entry_size() const 900 { return 8; }; 901 902 // Create the GOT section for an incremental update. 903 Output_data_got_base* 904 init_got_plt_for_update(Symbol_table* symtab, 905 Layout* layout, 906 unsigned int got_count, 907 unsigned int plt_count); 908 909 // Reserve a GOT entry for a local symbol, and regenerate any 910 // necessary dynamic relocations. 911 void 912 reserve_local_got_entry(unsigned int got_index, 913 Sized_relobj<size, false>* obj, 914 unsigned int r_sym, 915 unsigned int got_type); 916 917 // Reserve a GOT entry for a global symbol, and regenerate any 918 // necessary dynamic relocations. 919 void 920 reserve_global_got_entry(unsigned int got_index, Symbol* gsym, 921 unsigned int got_type); 922 923 // Register an existing PLT entry for a global symbol. 924 void 925 register_global_plt_entry(Symbol_table*, Layout*, unsigned int plt_index, 926 Symbol* gsym); 927 928 // Force a COPY relocation for a given symbol. 929 void 930 emit_copy_reloc(Symbol_table*, Symbol*, Output_section*, off_t); 931 932 // Apply an incremental relocation. 933 void 934 apply_relocation(const Relocate_info<size, false>* relinfo, 935 typename elfcpp::Elf_types<size>::Elf_Addr r_offset, 936 unsigned int r_type, 937 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend, 938 const Symbol* gsym, 939 unsigned char* view, 940 typename elfcpp::Elf_types<size>::Elf_Addr address, 941 section_size_type view_size); 942 943 // Add a new reloc argument, returning the index in the vector. 944 size_t 945 add_tlsdesc_info(Sized_relobj_file<size, false>* object, unsigned int r_sym) 946 { 947 this->tlsdesc_reloc_info_.push_back(Tlsdesc_info(object, r_sym)); 948 return this->tlsdesc_reloc_info_.size() - 1; 949 } 950 951 Output_data_plt_x86_64<size>* 952 make_data_plt(Layout* layout, 953 Output_data_got<64, false>* got, 954 Output_data_got_plt_x86_64* got_plt, 955 Output_data_space* got_irelative) 956 { 957 return this->do_make_data_plt(layout, got, got_plt, got_irelative); 958 } 959 960 Output_data_plt_x86_64<size>* 961 make_data_plt(Layout* layout, 962 Output_data_got<64, false>* got, 963 Output_data_got_plt_x86_64* got_plt, 964 Output_data_space* got_irelative, 965 unsigned int plt_count) 966 { 967 return this->do_make_data_plt(layout, got, got_plt, got_irelative, 968 plt_count); 969 } 970 971 virtual Output_data_plt_x86_64<size>* 972 do_make_data_plt(Layout* layout, 973 Output_data_got<64, false>* got, 974 Output_data_got_plt_x86_64* got_plt, 975 Output_data_space* got_irelative); 976 977 virtual Output_data_plt_x86_64<size>* 978 do_make_data_plt(Layout* layout, 979 Output_data_got<64, false>* got, 980 Output_data_got_plt_x86_64* got_plt, 981 Output_data_space* got_irelative, 982 unsigned int plt_count); 983 984 private: 985 // The class which scans relocations. 986 class Scan 987 { 988 public: 989 Scan() 990 : issued_non_pic_error_(false) 991 { } 992 993 static inline int 994 get_reference_flags(unsigned int r_type); 995 996 inline void 997 local(Symbol_table* symtab, Layout* layout, Target_x86_64* target, 998 Sized_relobj_file<size, false>* object, 999 unsigned int data_shndx, 1000 Output_section* output_section, 1001 const elfcpp::Rela<size, false>& reloc, unsigned int r_type, 1002 const elfcpp::Sym<size, false>& lsym, 1003 bool is_discarded); 1004 1005 inline void 1006 global(Symbol_table* symtab, Layout* layout, Target_x86_64* target, 1007 Sized_relobj_file<size, false>* object, 1008 unsigned int data_shndx, 1009 Output_section* output_section, 1010 const elfcpp::Rela<size, false>& reloc, unsigned int r_type, 1011 Symbol* gsym); 1012 1013 inline bool 1014 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout, 1015 Target_x86_64* target, 1016 Sized_relobj_file<size, false>* object, 1017 unsigned int data_shndx, 1018 Output_section* output_section, 1019 const elfcpp::Rela<size, false>& reloc, 1020 unsigned int r_type, 1021 const elfcpp::Sym<size, false>& lsym); 1022 1023 inline bool 1024 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout, 1025 Target_x86_64* target, 1026 Sized_relobj_file<size, false>* object, 1027 unsigned int data_shndx, 1028 Output_section* output_section, 1029 const elfcpp::Rela<size, false>& reloc, 1030 unsigned int r_type, 1031 Symbol* gsym); 1032 1033 private: 1034 static void 1035 unsupported_reloc_local(Sized_relobj_file<size, false>*, 1036 unsigned int r_type); 1037 1038 static void 1039 unsupported_reloc_global(Sized_relobj_file<size, false>*, 1040 unsigned int r_type, Symbol*); 1041 1042 void 1043 check_non_pic(Relobj*, unsigned int r_type, Symbol*); 1044 1045 inline bool 1046 possible_function_pointer_reloc(Sized_relobj_file<size, false>* src_obj, 1047 unsigned int src_indx, 1048 unsigned int r_offset, 1049 unsigned int r_type); 1050 1051 bool 1052 reloc_needs_plt_for_ifunc(Sized_relobj_file<size, false>*, 1053 unsigned int r_type); 1054 1055 // Whether we have issued an error about a non-PIC compilation. 1056 bool issued_non_pic_error_; 1057 }; 1058 1059 // The class which implements relocation. 1060 class Relocate 1061 { 1062 public: 1063 Relocate() 1064 : skip_call_tls_get_addr_(false) 1065 { } 1066 1067 ~Relocate() 1068 { 1069 if (this->skip_call_tls_get_addr_) 1070 { 1071 // FIXME: This needs to specify the location somehow. 1072 gold_error(_("missing expected TLS relocation")); 1073 } 1074 } 1075 1076 // Do a relocation. Return false if the caller should not issue 1077 // any warnings about this relocation. 1078 inline bool 1079 relocate(const Relocate_info<size, false>*, unsigned int, 1080 Target_x86_64*, Output_section*, size_t, const unsigned char*, 1081 const Sized_symbol<size>*, const Symbol_value<size>*, 1082 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr, 1083 section_size_type); 1084 1085 private: 1086 // Do a TLS relocation. 1087 inline void 1088 relocate_tls(const Relocate_info<size, false>*, Target_x86_64*, 1089 size_t relnum, const elfcpp::Rela<size, false>&, 1090 unsigned int r_type, const Sized_symbol<size>*, 1091 const Symbol_value<size>*, 1092 unsigned char*, typename elfcpp::Elf_types<size>::Elf_Addr, 1093 section_size_type); 1094 1095 // Do a TLS General-Dynamic to Initial-Exec transition. 1096 inline void 1097 tls_gd_to_ie(const Relocate_info<size, false>*, size_t relnum, 1098 const elfcpp::Rela<size, false>&, unsigned int r_type, 1099 typename elfcpp::Elf_types<size>::Elf_Addr value, 1100 unsigned char* view, 1101 typename elfcpp::Elf_types<size>::Elf_Addr, 1102 section_size_type view_size); 1103 1104 // Do a TLS General-Dynamic to Local-Exec transition. 1105 inline void 1106 tls_gd_to_le(const Relocate_info<size, false>*, size_t relnum, 1107 Output_segment* tls_segment, 1108 const elfcpp::Rela<size, false>&, unsigned int r_type, 1109 typename elfcpp::Elf_types<size>::Elf_Addr value, 1110 unsigned char* view, 1111 section_size_type view_size); 1112 1113 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition. 1114 inline void 1115 tls_desc_gd_to_ie(const Relocate_info<size, false>*, size_t relnum, 1116 const elfcpp::Rela<size, false>&, unsigned int r_type, 1117 typename elfcpp::Elf_types<size>::Elf_Addr value, 1118 unsigned char* view, 1119 typename elfcpp::Elf_types<size>::Elf_Addr, 1120 section_size_type view_size); 1121 1122 // Do a TLSDESC-style General-Dynamic to Local-Exec transition. 1123 inline void 1124 tls_desc_gd_to_le(const Relocate_info<size, false>*, size_t relnum, 1125 Output_segment* tls_segment, 1126 const elfcpp::Rela<size, false>&, unsigned int r_type, 1127 typename elfcpp::Elf_types<size>::Elf_Addr value, 1128 unsigned char* view, 1129 section_size_type view_size); 1130 1131 // Do a TLS Local-Dynamic to Local-Exec transition. 1132 inline void 1133 tls_ld_to_le(const Relocate_info<size, false>*, size_t relnum, 1134 Output_segment* tls_segment, 1135 const elfcpp::Rela<size, false>&, unsigned int r_type, 1136 typename elfcpp::Elf_types<size>::Elf_Addr value, 1137 unsigned char* view, 1138 section_size_type view_size); 1139 1140 // Do a TLS Initial-Exec to Local-Exec transition. 1141 static inline void 1142 tls_ie_to_le(const Relocate_info<size, false>*, size_t relnum, 1143 Output_segment* tls_segment, 1144 const elfcpp::Rela<size, false>&, unsigned int r_type, 1145 typename elfcpp::Elf_types<size>::Elf_Addr value, 1146 unsigned char* view, 1147 section_size_type view_size); 1148 1149 // This is set if we should skip the next reloc, which should be a 1150 // PLT32 reloc against ___tls_get_addr. 1151 bool skip_call_tls_get_addr_; 1152 }; 1153 1154 // Check if relocation against this symbol is a candidate for 1155 // conversion from 1156 // mov foo@GOTPCREL(%rip), %reg 1157 // to lea foo(%rip), %reg. 1158 template<class View_type> 1159 static inline bool 1160 can_convert_mov_to_lea(const Symbol* gsym, unsigned int r_type, 1161 size_t r_offset, View_type* view) 1162 { 1163 gold_assert(gsym != NULL); 1164 // We cannot do the conversion unless it's one of these relocations. 1165 if (r_type != elfcpp::R_X86_64_GOTPCREL 1166 && r_type != elfcpp::R_X86_64_GOTPCRELX 1167 && r_type != elfcpp::R_X86_64_REX_GOTPCRELX) 1168 return false; 1169 // We cannot convert references to IFUNC symbols, or to symbols that 1170 // are not local to the current module. 1171 // We can't do predefined symbols because they may become undefined 1172 // (e.g., __ehdr_start when the headers aren't mapped to a segment). 1173 if (gsym->type() == elfcpp::STT_GNU_IFUNC 1174 || gsym->is_undefined() 1175 || gsym->is_predefined() 1176 || gsym->is_from_dynobj() 1177 || gsym->is_preemptible()) 1178 return false; 1179 // If we are building a shared object and the symbol is protected, we may 1180 // need to go through the GOT. 1181 if (parameters->options().shared() 1182 && gsym->visibility() == elfcpp::STV_PROTECTED) 1183 return false; 1184 // We cannot convert references to the _DYNAMIC symbol. 1185 if (strcmp(gsym->name(), "_DYNAMIC") == 0) 1186 return false; 1187 // Check for a MOV opcode. 1188 return (*view)[r_offset - 2] == 0x8b; 1189 } 1190 1191 // Convert 1192 // callq *foo@GOTPCRELX(%rip) to 1193 // addr32 callq foo 1194 // and jmpq *foo@GOTPCRELX(%rip) to 1195 // jmpq foo 1196 // nop 1197 template<class View_type> 1198 static inline bool 1199 can_convert_callq_to_direct(const Symbol* gsym, unsigned int r_type, 1200 size_t r_offset, View_type* view) 1201 { 1202 gold_assert(gsym != NULL); 1203 // We cannot do the conversion unless it's a GOTPCRELX relocation. 1204 if (r_type != elfcpp::R_X86_64_GOTPCRELX) 1205 return false; 1206 // We cannot convert references to IFUNC symbols, or to symbols that 1207 // are not local to the current module. 1208 if (gsym->type() == elfcpp::STT_GNU_IFUNC 1209 || gsym->is_undefined () 1210 || gsym->is_from_dynobj() 1211 || gsym->is_preemptible()) 1212 return false; 1213 // Check for a CALLQ or JMPQ opcode. 1214 return ((*view)[r_offset - 2] == 0xff 1215 && ((*view)[r_offset - 1] == 0x15 1216 || (*view)[r_offset - 1] == 0x25)); 1217 } 1218 1219 // Adjust TLS relocation type based on the options and whether this 1220 // is a local symbol. 1221 static tls::Tls_optimization 1222 optimize_tls_reloc(bool is_final, int r_type); 1223 1224 // Get the GOT section, creating it if necessary. 1225 Output_data_got<64, false>* 1226 got_section(Symbol_table*, Layout*); 1227 1228 // Get the GOT PLT section. 1229 Output_data_got_plt_x86_64* 1230 got_plt_section() const 1231 { 1232 gold_assert(this->got_plt_ != NULL); 1233 return this->got_plt_; 1234 } 1235 1236 // Get the GOT section for TLSDESC entries. 1237 Output_data_got<64, false>* 1238 got_tlsdesc_section() const 1239 { 1240 gold_assert(this->got_tlsdesc_ != NULL); 1241 return this->got_tlsdesc_; 1242 } 1243 1244 // Create the PLT section. 1245 void 1246 make_plt_section(Symbol_table* symtab, Layout* layout); 1247 1248 // Create a PLT entry for a global symbol. 1249 void 1250 make_plt_entry(Symbol_table*, Layout*, Symbol*); 1251 1252 // Create a PLT entry for a local STT_GNU_IFUNC symbol. 1253 void 1254 make_local_ifunc_plt_entry(Symbol_table*, Layout*, 1255 Sized_relobj_file<size, false>* relobj, 1256 unsigned int local_sym_index); 1257 1258 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment. 1259 void 1260 define_tls_base_symbol(Symbol_table*, Layout*); 1261 1262 // Create the reserved PLT and GOT entries for the TLS descriptor resolver. 1263 void 1264 reserve_tlsdesc_entries(Symbol_table* symtab, Layout* layout); 1265 1266 // Create a GOT entry for the TLS module index. 1267 unsigned int 1268 got_mod_index_entry(Symbol_table* symtab, Layout* layout, 1269 Sized_relobj_file<size, false>* object); 1270 1271 // Get the PLT section. 1272 Output_data_plt_x86_64<size>* 1273 plt_section() const 1274 { 1275 gold_assert(this->plt_ != NULL); 1276 return this->plt_; 1277 } 1278 1279 // Get the dynamic reloc section, creating it if necessary. 1280 Reloc_section* 1281 rela_dyn_section(Layout*); 1282 1283 // Get the section to use for TLSDESC relocations. 1284 Reloc_section* 1285 rela_tlsdesc_section(Layout*) const; 1286 1287 // Get the section to use for IRELATIVE relocations. 1288 Reloc_section* 1289 rela_irelative_section(Layout*); 1290 1291 // Add a potential copy relocation. 1292 void 1293 copy_reloc(Symbol_table* symtab, Layout* layout, 1294 Sized_relobj_file<size, false>* object, 1295 unsigned int shndx, Output_section* output_section, 1296 Symbol* sym, const elfcpp::Rela<size, false>& reloc) 1297 { 1298 unsigned int r_type = elfcpp::elf_r_type<size>(reloc.get_r_info()); 1299 this->copy_relocs_.copy_reloc(symtab, layout, 1300 symtab->get_sized_symbol<size>(sym), 1301 object, shndx, output_section, 1302 r_type, reloc.get_r_offset(), 1303 reloc.get_r_addend(), 1304 this->rela_dyn_section(layout)); 1305 } 1306 1307 // Record a target-specific program property in the .note.gnu.property 1308 // section. 1309 void 1310 record_gnu_property(int, int, size_t, const unsigned char*, const Object*); 1311 1312 // Merge the target-specific program properties from the current object. 1313 void 1314 merge_gnu_properties(const Object*); 1315 1316 // Finalize the target-specific program properties and add them back to 1317 // the layout. 1318 void 1319 do_finalize_gnu_properties(Layout*) const; 1320 1321 // Information about this specific target which we pass to the 1322 // general Target structure. 1323 static const Target::Target_info x86_64_info; 1324 1325 // The types of GOT entries needed for this platform. 1326 // These values are exposed to the ABI in an incremental link. 1327 // Do not renumber existing values without changing the version 1328 // number of the .gnu_incremental_inputs section. 1329 enum Got_type 1330 { 1331 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol 1332 GOT_TYPE_TLS_OFFSET = 1, // GOT entry for TLS offset 1333 GOT_TYPE_TLS_PAIR = 2, // GOT entry for TLS module/offset pair 1334 GOT_TYPE_TLS_DESC = 3 // GOT entry for TLS_DESC pair 1335 }; 1336 1337 // This type is used as the argument to the target specific 1338 // relocation routines. The only target specific reloc is 1339 // R_X86_64_TLSDESC against a local symbol. 1340 struct Tlsdesc_info 1341 { 1342 Tlsdesc_info(Sized_relobj_file<size, false>* a_object, unsigned int a_r_sym) 1343 : object(a_object), r_sym(a_r_sym) 1344 { } 1345 1346 // The object in which the local symbol is defined. 1347 Sized_relobj_file<size, false>* object; 1348 // The local symbol index in the object. 1349 unsigned int r_sym; 1350 }; 1351 1352 // The GOT section. 1353 Output_data_got<64, false>* got_; 1354 // The PLT section. 1355 Output_data_plt_x86_64<size>* plt_; 1356 // The GOT PLT section. 1357 Output_data_got_plt_x86_64* got_plt_; 1358 // The GOT section for IRELATIVE relocations. 1359 Output_data_space* got_irelative_; 1360 // The GOT section for TLSDESC relocations. 1361 Output_data_got<64, false>* got_tlsdesc_; 1362 // The _GLOBAL_OFFSET_TABLE_ symbol. 1363 Symbol* global_offset_table_; 1364 // The dynamic reloc section. 1365 Reloc_section* rela_dyn_; 1366 // The section to use for IRELATIVE relocs. 1367 Reloc_section* rela_irelative_; 1368 // Relocs saved to avoid a COPY reloc. 1369 Copy_relocs<elfcpp::SHT_RELA, size, false> copy_relocs_; 1370 // Offset of the GOT entry for the TLS module index. 1371 unsigned int got_mod_index_offset_; 1372 // We handle R_X86_64_TLSDESC against a local symbol as a target 1373 // specific relocation. Here we store the object and local symbol 1374 // index for the relocation. 1375 std::vector<Tlsdesc_info> tlsdesc_reloc_info_; 1376 // True if the _TLS_MODULE_BASE_ symbol has been defined. 1377 bool tls_base_symbol_defined_; 1378 // Target-specific program properties, from .note.gnu.property section. 1379 // Each bit represents a specific feature. 1380 uint32_t isa_1_used_; 1381 uint32_t isa_1_needed_; 1382 uint32_t feature_1_; 1383 // Target-specific properties from the current object. 1384 // These bits get ANDed into FEATURE_1_ after all properties for the object 1385 // have been processed. 1386 uint32_t object_feature_1_; 1387 // Whether we have seen our first object, for use in initializing FEATURE_1_. 1388 bool seen_first_object_; 1389 }; 1390 1391 template<> 1392 const Target::Target_info Target_x86_64<64>::x86_64_info = 1393 { 1394 64, // size 1395 false, // is_big_endian 1396 elfcpp::EM_X86_64, // machine_code 1397 false, // has_make_symbol 1398 false, // has_resolve 1399 true, // has_code_fill 1400 true, // is_default_stack_executable 1401 true, // can_icf_inline_merge_sections 1402 '\0', // wrap_char 1403 "/lib/ld64.so.1", // program interpreter 1404 0x400000, // default_text_segment_address 1405 0x1000, // abi_pagesize (overridable by -z max-page-size) 1406 0x1000, // common_pagesize (overridable by -z common-page-size) 1407 false, // isolate_execinstr 1408 0, // rosegment_gap 1409 elfcpp::SHN_UNDEF, // small_common_shndx 1410 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx 1411 0, // small_common_section_flags 1412 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags 1413 NULL, // attributes_section 1414 NULL, // attributes_vendor 1415 "_start", // entry_symbol_name 1416 32, // hash_entry_size 1417 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type 1418 }; 1419 1420 template<> 1421 const Target::Target_info Target_x86_64<32>::x86_64_info = 1422 { 1423 32, // size 1424 false, // is_big_endian 1425 elfcpp::EM_X86_64, // machine_code 1426 false, // has_make_symbol 1427 false, // has_resolve 1428 true, // has_code_fill 1429 true, // is_default_stack_executable 1430 true, // can_icf_inline_merge_sections 1431 '\0', // wrap_char 1432 "/libx32/ldx32.so.1", // program interpreter 1433 0x400000, // default_text_segment_address 1434 0x1000, // abi_pagesize (overridable by -z max-page-size) 1435 0x1000, // common_pagesize (overridable by -z common-page-size) 1436 false, // isolate_execinstr 1437 0, // rosegment_gap 1438 elfcpp::SHN_UNDEF, // small_common_shndx 1439 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx 1440 0, // small_common_section_flags 1441 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags 1442 NULL, // attributes_section 1443 NULL, // attributes_vendor 1444 "_start", // entry_symbol_name 1445 32, // hash_entry_size 1446 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type 1447 }; 1448 1449 // This is called when a new output section is created. This is where 1450 // we handle the SHF_X86_64_LARGE. 1451 1452 template<int size> 1453 void 1454 Target_x86_64<size>::do_new_output_section(Output_section* os) const 1455 { 1456 if ((os->flags() & elfcpp::SHF_X86_64_LARGE) != 0) 1457 os->set_is_large_section(); 1458 } 1459 1460 // Get the GOT section, creating it if necessary. 1461 1462 template<int size> 1463 Output_data_got<64, false>* 1464 Target_x86_64<size>::got_section(Symbol_table* symtab, Layout* layout) 1465 { 1466 if (this->got_ == NULL) 1467 { 1468 gold_assert(symtab != NULL && layout != NULL); 1469 1470 // When using -z now, we can treat .got.plt as a relro section. 1471 // Without -z now, it is modified after program startup by lazy 1472 // PLT relocations. 1473 bool is_got_plt_relro = parameters->options().now(); 1474 Output_section_order got_order = (is_got_plt_relro 1475 ? ORDER_RELRO 1476 : ORDER_RELRO_LAST); 1477 Output_section_order got_plt_order = (is_got_plt_relro 1478 ? ORDER_RELRO 1479 : ORDER_NON_RELRO_FIRST); 1480 1481 this->got_ = new Output_data_got<64, false>(); 1482 1483 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, 1484 (elfcpp::SHF_ALLOC 1485 | elfcpp::SHF_WRITE), 1486 this->got_, got_order, true); 1487 1488 this->got_plt_ = new Output_data_got_plt_x86_64(layout); 1489 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, 1490 (elfcpp::SHF_ALLOC 1491 | elfcpp::SHF_WRITE), 1492 this->got_plt_, got_plt_order, 1493 is_got_plt_relro); 1494 1495 // The first three entries are reserved. 1496 this->got_plt_->set_current_data_size(3 * 8); 1497 1498 if (!is_got_plt_relro) 1499 { 1500 // Those bytes can go into the relro segment. 1501 layout->increase_relro(3 * 8); 1502 } 1503 1504 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. 1505 this->global_offset_table_ = 1506 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, 1507 Symbol_table::PREDEFINED, 1508 this->got_plt_, 1509 0, 0, elfcpp::STT_OBJECT, 1510 elfcpp::STB_LOCAL, 1511 elfcpp::STV_HIDDEN, 0, 1512 false, false); 1513 1514 // If there are any IRELATIVE relocations, they get GOT entries 1515 // in .got.plt after the jump slot entries. 1516 this->got_irelative_ = new Output_data_space(8, "** GOT IRELATIVE PLT"); 1517 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, 1518 (elfcpp::SHF_ALLOC 1519 | elfcpp::SHF_WRITE), 1520 this->got_irelative_, 1521 got_plt_order, is_got_plt_relro); 1522 1523 // If there are any TLSDESC relocations, they get GOT entries in 1524 // .got.plt after the jump slot and IRELATIVE entries. 1525 this->got_tlsdesc_ = new Output_data_got<64, false>(); 1526 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, 1527 (elfcpp::SHF_ALLOC 1528 | elfcpp::SHF_WRITE), 1529 this->got_tlsdesc_, 1530 got_plt_order, is_got_plt_relro); 1531 } 1532 1533 return this->got_; 1534 } 1535 1536 // Get the dynamic reloc section, creating it if necessary. 1537 1538 template<int size> 1539 typename Target_x86_64<size>::Reloc_section* 1540 Target_x86_64<size>::rela_dyn_section(Layout* layout) 1541 { 1542 if (this->rela_dyn_ == NULL) 1543 { 1544 gold_assert(layout != NULL); 1545 this->rela_dyn_ = new Reloc_section(parameters->options().combreloc()); 1546 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, 1547 elfcpp::SHF_ALLOC, this->rela_dyn_, 1548 ORDER_DYNAMIC_RELOCS, false); 1549 } 1550 return this->rela_dyn_; 1551 } 1552 1553 // Get the section to use for IRELATIVE relocs, creating it if 1554 // necessary. These go in .rela.dyn, but only after all other dynamic 1555 // relocations. They need to follow the other dynamic relocations so 1556 // that they can refer to global variables initialized by those 1557 // relocs. 1558 1559 template<int size> 1560 typename Target_x86_64<size>::Reloc_section* 1561 Target_x86_64<size>::rela_irelative_section(Layout* layout) 1562 { 1563 if (this->rela_irelative_ == NULL) 1564 { 1565 // Make sure we have already created the dynamic reloc section. 1566 this->rela_dyn_section(layout); 1567 this->rela_irelative_ = new Reloc_section(false); 1568 layout->add_output_section_data(".rela.dyn", elfcpp::SHT_RELA, 1569 elfcpp::SHF_ALLOC, this->rela_irelative_, 1570 ORDER_DYNAMIC_RELOCS, false); 1571 gold_assert(this->rela_dyn_->output_section() 1572 == this->rela_irelative_->output_section()); 1573 } 1574 return this->rela_irelative_; 1575 } 1576 1577 // Record a target-specific program property from the .note.gnu.property 1578 // section. 1579 template<int size> 1580 void 1581 Target_x86_64<size>::record_gnu_property( 1582 int, int pr_type, 1583 size_t pr_datasz, const unsigned char* pr_data, 1584 const Object* object) 1585 { 1586 uint32_t val = 0; 1587 1588 switch (pr_type) 1589 { 1590 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED: 1591 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED: 1592 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND: 1593 if (pr_datasz != 4) 1594 { 1595 gold_warning(_("%s: corrupt .note.gnu.property section " 1596 "(pr_datasz for property %d is not 4)"), 1597 object->name().c_str(), pr_type); 1598 return; 1599 } 1600 val = elfcpp::Swap<32, false>::readval(pr_data); 1601 break; 1602 default: 1603 gold_warning(_("%s: unknown program property type 0x%x " 1604 "in .note.gnu.property section"), 1605 object->name().c_str(), pr_type); 1606 break; 1607 } 1608 1609 switch (pr_type) 1610 { 1611 case elfcpp::GNU_PROPERTY_X86_ISA_1_USED: 1612 this->isa_1_used_ |= val; 1613 break; 1614 case elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED: 1615 this->isa_1_needed_ |= val; 1616 break; 1617 case elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND: 1618 // If we see multiple feature props in one object, OR them together. 1619 this->object_feature_1_ |= val; 1620 break; 1621 } 1622 } 1623 1624 // Merge the target-specific program properties from the current object. 1625 template<int size> 1626 void 1627 Target_x86_64<size>::merge_gnu_properties(const Object*) 1628 { 1629 if (this->seen_first_object_) 1630 this->feature_1_ &= this->object_feature_1_; 1631 else 1632 { 1633 this->feature_1_ = this->object_feature_1_; 1634 this->seen_first_object_ = true; 1635 } 1636 this->object_feature_1_ = 0; 1637 } 1638 1639 static inline void 1640 add_property(Layout* layout, unsigned int pr_type, uint32_t val) 1641 { 1642 unsigned char buf[4]; 1643 elfcpp::Swap<32, false>::writeval(buf, val); 1644 layout->add_gnu_property(elfcpp::NT_GNU_PROPERTY_TYPE_0, pr_type, 4, buf); 1645 } 1646 1647 // Finalize the target-specific program properties and add them back to 1648 // the layout. 1649 template<int size> 1650 void 1651 Target_x86_64<size>::do_finalize_gnu_properties(Layout* layout) const 1652 { 1653 if (this->isa_1_used_ != 0) 1654 add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_USED, 1655 this->isa_1_used_); 1656 if (this->isa_1_needed_ != 0) 1657 add_property(layout, elfcpp::GNU_PROPERTY_X86_ISA_1_NEEDED, 1658 this->isa_1_needed_); 1659 if (this->feature_1_ != 0) 1660 add_property(layout, elfcpp::GNU_PROPERTY_X86_FEATURE_1_AND, 1661 this->feature_1_); 1662 } 1663 1664 // Write the first three reserved words of the .got.plt section. 1665 // The remainder of the section is written while writing the PLT 1666 // in Output_data_plt_i386::do_write. 1667 1668 void 1669 Output_data_got_plt_x86_64::do_write(Output_file* of) 1670 { 1671 // The first entry in the GOT is the address of the .dynamic section 1672 // aka the PT_DYNAMIC segment. The next two entries are reserved. 1673 // We saved space for them when we created the section in 1674 // Target_x86_64::got_section. 1675 const off_t got_file_offset = this->offset(); 1676 gold_assert(this->data_size() >= 24); 1677 unsigned char* const got_view = of->get_output_view(got_file_offset, 24); 1678 Output_section* dynamic = this->layout_->dynamic_section(); 1679 uint64_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address(); 1680 elfcpp::Swap<64, false>::writeval(got_view, dynamic_addr); 1681 memset(got_view + 8, 0, 16); 1682 of->write_output_view(got_file_offset, 24, got_view); 1683 } 1684 1685 // Initialize the PLT section. 1686 1687 template<int size> 1688 void 1689 Output_data_plt_x86_64<size>::init(Layout* layout) 1690 { 1691 this->rel_ = new Reloc_section(false); 1692 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, 1693 elfcpp::SHF_ALLOC, this->rel_, 1694 ORDER_DYNAMIC_PLT_RELOCS, false); 1695 } 1696 1697 template<int size> 1698 void 1699 Output_data_plt_x86_64<size>::do_adjust_output_section(Output_section* os) 1700 { 1701 os->set_entsize(this->get_plt_entry_size()); 1702 } 1703 1704 // Add an entry to the PLT. 1705 1706 template<int size> 1707 void 1708 Output_data_plt_x86_64<size>::add_entry(Symbol_table* symtab, Layout* layout, 1709 Symbol* gsym) 1710 { 1711 gold_assert(!gsym->has_plt_offset()); 1712 1713 unsigned int plt_index; 1714 off_t plt_offset; 1715 section_offset_type got_offset; 1716 1717 unsigned int* pcount; 1718 unsigned int offset; 1719 unsigned int reserved; 1720 Output_section_data_build* got; 1721 if (gsym->type() == elfcpp::STT_GNU_IFUNC 1722 && gsym->can_use_relative_reloc(false)) 1723 { 1724 pcount = &this->irelative_count_; 1725 offset = 0; 1726 reserved = 0; 1727 got = this->got_irelative_; 1728 } 1729 else 1730 { 1731 pcount = &this->count_; 1732 offset = 1; 1733 reserved = 3; 1734 got = this->got_plt_; 1735 } 1736 1737 if (!this->is_data_size_valid()) 1738 { 1739 // Note that when setting the PLT offset for a non-IRELATIVE 1740 // entry we skip the initial reserved PLT entry. 1741 plt_index = *pcount + offset; 1742 plt_offset = plt_index * this->get_plt_entry_size(); 1743 1744 ++*pcount; 1745 1746 got_offset = (plt_index - offset + reserved) * 8; 1747 gold_assert(got_offset == got->current_data_size()); 1748 1749 // Every PLT entry needs a GOT entry which points back to the PLT 1750 // entry (this will be changed by the dynamic linker, normally 1751 // lazily when the function is called). 1752 got->set_current_data_size(got_offset + 8); 1753 } 1754 else 1755 { 1756 // FIXME: This is probably not correct for IRELATIVE relocs. 1757 1758 // For incremental updates, find an available slot. 1759 plt_offset = this->free_list_.allocate(this->get_plt_entry_size(), 1760 this->get_plt_entry_size(), 0); 1761 if (plt_offset == -1) 1762 gold_fallback(_("out of patch space (PLT);" 1763 " relink with --incremental-full")); 1764 1765 // The GOT and PLT entries have a 1-1 correspondance, so the GOT offset 1766 // can be calculated from the PLT index, adjusting for the three 1767 // reserved entries at the beginning of the GOT. 1768 plt_index = plt_offset / this->get_plt_entry_size() - 1; 1769 got_offset = (plt_index - offset + reserved) * 8; 1770 } 1771 1772 gsym->set_plt_offset(plt_offset); 1773 1774 // Every PLT entry needs a reloc. 1775 this->add_relocation(symtab, layout, gsym, got_offset); 1776 1777 // Note that we don't need to save the symbol. The contents of the 1778 // PLT are independent of which symbols are used. The symbols only 1779 // appear in the relocations. 1780 } 1781 1782 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return 1783 // the PLT offset. 1784 1785 template<int size> 1786 unsigned int 1787 Output_data_plt_x86_64<size>::add_local_ifunc_entry( 1788 Symbol_table* symtab, 1789 Layout* layout, 1790 Sized_relobj_file<size, false>* relobj, 1791 unsigned int local_sym_index) 1792 { 1793 unsigned int plt_offset = this->irelative_count_ * this->get_plt_entry_size(); 1794 ++this->irelative_count_; 1795 1796 section_offset_type got_offset = this->got_irelative_->current_data_size(); 1797 1798 // Every PLT entry needs a GOT entry which points back to the PLT 1799 // entry. 1800 this->got_irelative_->set_current_data_size(got_offset + 8); 1801 1802 // Every PLT entry needs a reloc. 1803 Reloc_section* rela = this->rela_irelative(symtab, layout); 1804 rela->add_symbolless_local_addend(relobj, local_sym_index, 1805 elfcpp::R_X86_64_IRELATIVE, 1806 this->got_irelative_, got_offset, 0); 1807 1808 return plt_offset; 1809 } 1810 1811 // Add the relocation for a PLT entry. 1812 1813 template<int size> 1814 void 1815 Output_data_plt_x86_64<size>::add_relocation(Symbol_table* symtab, 1816 Layout* layout, 1817 Symbol* gsym, 1818 unsigned int got_offset) 1819 { 1820 if (gsym->type() == elfcpp::STT_GNU_IFUNC 1821 && gsym->can_use_relative_reloc(false)) 1822 { 1823 Reloc_section* rela = this->rela_irelative(symtab, layout); 1824 rela->add_symbolless_global_addend(gsym, elfcpp::R_X86_64_IRELATIVE, 1825 this->got_irelative_, got_offset, 0); 1826 } 1827 else 1828 { 1829 gsym->set_needs_dynsym_entry(); 1830 this->rel_->add_global(gsym, elfcpp::R_X86_64_JUMP_SLOT, this->got_plt_, 1831 got_offset, 0); 1832 } 1833 } 1834 1835 // Return where the TLSDESC relocations should go, creating it if 1836 // necessary. These follow the JUMP_SLOT relocations. 1837 1838 template<int size> 1839 typename Output_data_plt_x86_64<size>::Reloc_section* 1840 Output_data_plt_x86_64<size>::rela_tlsdesc(Layout* layout) 1841 { 1842 if (this->tlsdesc_rel_ == NULL) 1843 { 1844 this->tlsdesc_rel_ = new Reloc_section(false); 1845 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, 1846 elfcpp::SHF_ALLOC, this->tlsdesc_rel_, 1847 ORDER_DYNAMIC_PLT_RELOCS, false); 1848 gold_assert(this->tlsdesc_rel_->output_section() 1849 == this->rel_->output_section()); 1850 } 1851 return this->tlsdesc_rel_; 1852 } 1853 1854 // Return where the IRELATIVE relocations should go in the PLT. These 1855 // follow the JUMP_SLOT and the TLSDESC relocations. 1856 1857 template<int size> 1858 typename Output_data_plt_x86_64<size>::Reloc_section* 1859 Output_data_plt_x86_64<size>::rela_irelative(Symbol_table* symtab, 1860 Layout* layout) 1861 { 1862 if (this->irelative_rel_ == NULL) 1863 { 1864 // Make sure we have a place for the TLSDESC relocations, in 1865 // case we see any later on. 1866 this->rela_tlsdesc(layout); 1867 this->irelative_rel_ = new Reloc_section(false); 1868 layout->add_output_section_data(".rela.plt", elfcpp::SHT_RELA, 1869 elfcpp::SHF_ALLOC, this->irelative_rel_, 1870 ORDER_DYNAMIC_PLT_RELOCS, false); 1871 gold_assert(this->irelative_rel_->output_section() 1872 == this->rel_->output_section()); 1873 1874 if (parameters->doing_static_link()) 1875 { 1876 // A statically linked executable will only have a .rela.plt 1877 // section to hold R_X86_64_IRELATIVE relocs for 1878 // STT_GNU_IFUNC symbols. The library will use these 1879 // symbols to locate the IRELATIVE relocs at program startup 1880 // time. 1881 symtab->define_in_output_data("__rela_iplt_start", NULL, 1882 Symbol_table::PREDEFINED, 1883 this->irelative_rel_, 0, 0, 1884 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, 1885 elfcpp::STV_HIDDEN, 0, false, true); 1886 symtab->define_in_output_data("__rela_iplt_end", NULL, 1887 Symbol_table::PREDEFINED, 1888 this->irelative_rel_, 0, 0, 1889 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL, 1890 elfcpp::STV_HIDDEN, 0, true, true); 1891 } 1892 } 1893 return this->irelative_rel_; 1894 } 1895 1896 // Return the PLT address to use for a global symbol. 1897 1898 template<int size> 1899 uint64_t 1900 Output_data_plt_x86_64<size>::do_address_for_global(const Symbol* gsym) 1901 { 1902 uint64_t offset = 0; 1903 if (gsym->type() == elfcpp::STT_GNU_IFUNC 1904 && gsym->can_use_relative_reloc(false)) 1905 offset = (this->count_ + 1) * this->get_plt_entry_size(); 1906 return this->address() + offset + gsym->plt_offset(); 1907 } 1908 1909 // Return the PLT address to use for a local symbol. These are always 1910 // IRELATIVE relocs. 1911 1912 template<int size> 1913 uint64_t 1914 Output_data_plt_x86_64<size>::do_address_for_local(const Relobj* object, 1915 unsigned int r_sym) 1916 { 1917 return (this->address() 1918 + (this->count_ + 1) * this->get_plt_entry_size() 1919 + object->local_plt_offset(r_sym)); 1920 } 1921 1922 // Set the final size. 1923 template<int size> 1924 void 1925 Output_data_plt_x86_64<size>::set_final_data_size() 1926 { 1927 // Number of regular and IFUNC PLT entries, plus the first entry. 1928 unsigned int count = this->count_ + this->irelative_count_ + 1; 1929 // Count the TLSDESC entry, if present. 1930 if (this->has_tlsdesc_entry()) 1931 ++count; 1932 this->set_data_size(count * this->get_plt_entry_size()); 1933 } 1934 1935 // The first entry in the PLT for an executable. 1936 1937 template<int size> 1938 const unsigned char 1939 Output_data_plt_x86_64_standard<size>::first_plt_entry[plt_entry_size] = 1940 { 1941 // From AMD64 ABI Draft 0.98, page 76 1942 0xff, 0x35, // pushq contents of memory address 1943 0, 0, 0, 0, // replaced with address of .got + 8 1944 0xff, 0x25, // jmp indirect 1945 0, 0, 0, 0, // replaced with address of .got + 16 1946 0x90, 0x90, 0x90, 0x90 // noop (x4) 1947 }; 1948 1949 template<int size> 1950 void 1951 Output_data_plt_x86_64_standard<size>::do_fill_first_plt_entry( 1952 unsigned char* pov, 1953 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 1954 typename elfcpp::Elf_types<size>::Elf_Addr plt_address) 1955 { 1956 memcpy(pov, first_plt_entry, plt_entry_size); 1957 // We do a jmp relative to the PC at the end of this instruction. 1958 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, 1959 (got_address + 8 1960 - (plt_address + 6))); 1961 elfcpp::Swap<32, false>::writeval(pov + 8, 1962 (got_address + 16 1963 - (plt_address + 12))); 1964 } 1965 1966 // Subsequent entries in the PLT for an executable. 1967 1968 template<int size> 1969 const unsigned char 1970 Output_data_plt_x86_64_standard<size>::plt_entry[plt_entry_size] = 1971 { 1972 // From AMD64 ABI Draft 0.98, page 76 1973 0xff, 0x25, // jmpq indirect 1974 0, 0, 0, 0, // replaced with address of symbol in .got 1975 0x68, // pushq immediate 1976 0, 0, 0, 0, // replaced with offset into relocation table 1977 0xe9, // jmpq relative 1978 0, 0, 0, 0 // replaced with offset to start of .plt 1979 }; 1980 1981 template<int size> 1982 unsigned int 1983 Output_data_plt_x86_64_standard<size>::do_fill_plt_entry( 1984 unsigned char* pov, 1985 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 1986 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 1987 unsigned int got_offset, 1988 unsigned int plt_offset, 1989 unsigned int plt_index) 1990 { 1991 // Check PC-relative offset overflow in PLT entry. 1992 uint64_t plt_got_pcrel_offset = (got_address + got_offset 1993 - (plt_address + plt_offset + 6)); 1994 if (Bits<32>::has_overflow(plt_got_pcrel_offset)) 1995 gold_error(_("PC-relative offset overflow in PLT entry %d"), 1996 plt_index + 1); 1997 1998 memcpy(pov, plt_entry, plt_entry_size); 1999 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, 2000 plt_got_pcrel_offset); 2001 2002 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_index); 2003 elfcpp::Swap<32, false>::writeval(pov + 12, 2004 - (plt_offset + plt_entry_size)); 2005 2006 return 6; 2007 } 2008 2009 // The reserved TLSDESC entry in the PLT for an executable. 2010 2011 template<int size> 2012 const unsigned char 2013 Output_data_plt_x86_64_standard<size>::tlsdesc_plt_entry[plt_entry_size] = 2014 { 2015 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32 2016 // and AMD64/EM64T", Version 0.9.4 (2005-10-10). 2017 0xff, 0x35, // pushq x(%rip) 2018 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8) 2019 0xff, 0x25, // jmpq *y(%rip) 2020 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry 2021 0x0f, 0x1f, // nop 2022 0x40, 0 2023 }; 2024 2025 template<int size> 2026 void 2027 Output_data_plt_x86_64_standard<size>::do_fill_tlsdesc_entry( 2028 unsigned char* pov, 2029 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 2030 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 2031 typename elfcpp::Elf_types<size>::Elf_Addr got_base, 2032 unsigned int tlsdesc_got_offset, 2033 unsigned int plt_offset) 2034 { 2035 memcpy(pov, tlsdesc_plt_entry, plt_entry_size); 2036 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, 2037 (got_address + 8 2038 - (plt_address + plt_offset 2039 + 6))); 2040 elfcpp::Swap_unaligned<32, false>::writeval(pov + 8, 2041 (got_base 2042 + tlsdesc_got_offset 2043 - (plt_address + plt_offset 2044 + 12))); 2045 } 2046 2047 // Return the APLT address to use for a global symbol (for -z bndplt). 2048 2049 uint64_t 2050 Output_data_plt_x86_64_bnd::do_address_for_global(const Symbol* gsym) 2051 { 2052 uint64_t offset = this->aplt_offset_; 2053 // Convert the PLT offset into an APLT offset. 2054 unsigned int plt_offset = gsym->plt_offset(); 2055 if (gsym->type() == elfcpp::STT_GNU_IFUNC 2056 && gsym->can_use_relative_reloc(false)) 2057 offset += this->regular_count() * aplt_entry_size; 2058 else 2059 plt_offset -= plt_entry_size; 2060 plt_offset = plt_offset / (plt_entry_size / aplt_entry_size); 2061 return this->address() + offset + plt_offset; 2062 } 2063 2064 // Return the PLT address to use for a local symbol. These are always 2065 // IRELATIVE relocs. 2066 2067 uint64_t 2068 Output_data_plt_x86_64_bnd::do_address_for_local(const Relobj* object, 2069 unsigned int r_sym) 2070 { 2071 // Convert the PLT offset into an APLT offset. 2072 unsigned int plt_offset = ((object->local_plt_offset(r_sym) - plt_entry_size) 2073 / (plt_entry_size / aplt_entry_size)); 2074 return (this->address() 2075 + this->aplt_offset_ 2076 + this->regular_count() * aplt_entry_size 2077 + plt_offset); 2078 } 2079 2080 // Set the final size. 2081 void 2082 Output_data_plt_x86_64_bnd::set_final_data_size() 2083 { 2084 // Number of regular and IFUNC PLT entries. 2085 unsigned int count = this->entry_count(); 2086 // Count the first entry and the TLSDESC entry, if present. 2087 unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1; 2088 unsigned int plt_size = (count + extra) * plt_entry_size; 2089 // Offset of the APLT. 2090 this->aplt_offset_ = plt_size; 2091 // Size of the APLT. 2092 plt_size += count * aplt_entry_size; 2093 this->set_data_size(plt_size); 2094 } 2095 2096 // The first entry in the BND PLT. 2097 2098 const unsigned char 2099 Output_data_plt_x86_64_bnd::first_plt_entry[plt_entry_size] = 2100 { 2101 // From AMD64 ABI Draft 0.98, page 76 2102 0xff, 0x35, // pushq contents of memory address 2103 0, 0, 0, 0, // replaced with address of .got + 8 2104 0xf2, 0xff, 0x25, // bnd jmp indirect 2105 0, 0, 0, 0, // replaced with address of .got + 16 2106 0x0f, 0x1f, 0x00 // nop 2107 }; 2108 2109 void 2110 Output_data_plt_x86_64_bnd::do_fill_first_plt_entry( 2111 unsigned char* pov, 2112 elfcpp::Elf_types<64>::Elf_Addr got_address, 2113 elfcpp::Elf_types<64>::Elf_Addr plt_address) 2114 { 2115 memcpy(pov, first_plt_entry, plt_entry_size); 2116 // We do a jmp relative to the PC at the end of this instruction. 2117 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, 2118 (got_address + 8 2119 - (plt_address + 6))); 2120 elfcpp::Swap<32, false>::writeval(pov + 9, 2121 (got_address + 16 2122 - (plt_address + 13))); 2123 } 2124 2125 // Subsequent entries in the BND PLT. 2126 2127 const unsigned char 2128 Output_data_plt_x86_64_bnd::plt_entry[plt_entry_size] = 2129 { 2130 // From AMD64 ABI Draft 0.99.8, page 139 2131 0x68, // pushq immediate 2132 0, 0, 0, 0, // replaced with offset into relocation table 2133 0xf2, 0xe9, // bnd jmpq relative 2134 0, 0, 0, 0, // replaced with offset to start of .plt 2135 0x0f, 0x1f, 0x44, 0, 0 // nop 2136 }; 2137 2138 // Entries in the BND Additional PLT. 2139 2140 const unsigned char 2141 Output_data_plt_x86_64_bnd::aplt_entry[aplt_entry_size] = 2142 { 2143 // From AMD64 ABI Draft 0.99.8, page 139 2144 0xf2, 0xff, 0x25, // bnd jmpq indirect 2145 0, 0, 0, 0, // replaced with address of symbol in .got 2146 0x90, // nop 2147 }; 2148 2149 unsigned int 2150 Output_data_plt_x86_64_bnd::do_fill_plt_entry( 2151 unsigned char* pov, 2152 elfcpp::Elf_types<64>::Elf_Addr, 2153 elfcpp::Elf_types<64>::Elf_Addr, 2154 unsigned int, 2155 unsigned int plt_offset, 2156 unsigned int plt_index) 2157 { 2158 memcpy(pov, plt_entry, plt_entry_size); 2159 elfcpp::Swap_unaligned<32, false>::writeval(pov + 1, plt_index); 2160 elfcpp::Swap<32, false>::writeval(pov + 7, -(plt_offset + 11)); 2161 return 0; 2162 } 2163 2164 void 2165 Output_data_plt_x86_64_bnd::fill_aplt_entry( 2166 unsigned char* pov, 2167 elfcpp::Elf_types<64>::Elf_Addr got_address, 2168 elfcpp::Elf_types<64>::Elf_Addr plt_address, 2169 unsigned int got_offset, 2170 unsigned int plt_offset, 2171 unsigned int plt_index) 2172 { 2173 // Check PC-relative offset overflow in PLT entry. 2174 uint64_t plt_got_pcrel_offset = (got_address + got_offset 2175 - (plt_address + plt_offset + 7)); 2176 if (Bits<32>::has_overflow(plt_got_pcrel_offset)) 2177 gold_error(_("PC-relative offset overflow in APLT entry %d"), 2178 plt_index + 1); 2179 2180 memcpy(pov, aplt_entry, aplt_entry_size); 2181 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3, plt_got_pcrel_offset); 2182 } 2183 2184 // The reserved TLSDESC entry in the PLT for an executable. 2185 2186 const unsigned char 2187 Output_data_plt_x86_64_bnd::tlsdesc_plt_entry[plt_entry_size] = 2188 { 2189 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32 2190 // and AMD64/EM64T", Version 0.9.4 (2005-10-10). 2191 0xff, 0x35, // pushq x(%rip) 2192 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8) 2193 0xf2, 0xff, 0x25, // jmpq *y(%rip) 2194 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry 2195 0x0f, 0x1f, 0 // nop 2196 }; 2197 2198 void 2199 Output_data_plt_x86_64_bnd::do_fill_tlsdesc_entry( 2200 unsigned char* pov, 2201 elfcpp::Elf_types<64>::Elf_Addr got_address, 2202 elfcpp::Elf_types<64>::Elf_Addr plt_address, 2203 elfcpp::Elf_types<64>::Elf_Addr got_base, 2204 unsigned int tlsdesc_got_offset, 2205 unsigned int plt_offset) 2206 { 2207 memcpy(pov, tlsdesc_plt_entry, plt_entry_size); 2208 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, 2209 (got_address + 8 2210 - (plt_address + plt_offset 2211 + 6))); 2212 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9, 2213 (got_base 2214 + tlsdesc_got_offset 2215 - (plt_address + plt_offset 2216 + 13))); 2217 } 2218 2219 // Return the APLT address to use for a global symbol (for IBT). 2220 2221 template<int size> 2222 uint64_t 2223 Output_data_plt_x86_64_ibt<size>::do_address_for_global(const Symbol* gsym) 2224 { 2225 uint64_t offset = this->aplt_offset_; 2226 // Convert the PLT offset into an APLT offset. 2227 unsigned int plt_offset = gsym->plt_offset(); 2228 if (gsym->type() == elfcpp::STT_GNU_IFUNC 2229 && gsym->can_use_relative_reloc(false)) 2230 offset += this->regular_count() * aplt_entry_size; 2231 else 2232 plt_offset -= plt_entry_size; 2233 plt_offset = plt_offset / (plt_entry_size / aplt_entry_size); 2234 return this->address() + offset + plt_offset; 2235 } 2236 2237 // Return the PLT address to use for a local symbol. These are always 2238 // IRELATIVE relocs. 2239 2240 template<int size> 2241 uint64_t 2242 Output_data_plt_x86_64_ibt<size>::do_address_for_local(const Relobj* object, 2243 unsigned int r_sym) 2244 { 2245 // Convert the PLT offset into an APLT offset. 2246 unsigned int plt_offset = ((object->local_plt_offset(r_sym) - plt_entry_size) 2247 / (plt_entry_size / aplt_entry_size)); 2248 return (this->address() 2249 + this->aplt_offset_ 2250 + this->regular_count() * aplt_entry_size 2251 + plt_offset); 2252 } 2253 2254 // Set the final size. 2255 2256 template<int size> 2257 void 2258 Output_data_plt_x86_64_ibt<size>::set_final_data_size() 2259 { 2260 // Number of regular and IFUNC PLT entries. 2261 unsigned int count = this->entry_count(); 2262 // Count the first entry and the TLSDESC entry, if present. 2263 unsigned int extra = this->has_tlsdesc_entry() ? 2 : 1; 2264 unsigned int plt_size = (count + extra) * plt_entry_size; 2265 // Offset of the APLT. 2266 this->aplt_offset_ = plt_size; 2267 // Size of the APLT. 2268 plt_size += count * aplt_entry_size; 2269 this->set_data_size(plt_size); 2270 } 2271 2272 // The first entry in the IBT PLT. 2273 2274 template<> 2275 const unsigned char 2276 Output_data_plt_x86_64_ibt<32>::first_plt_entry[plt_entry_size] = 2277 { 2278 // MPX isn't supported for x32, so we don't need the BND prefix. 2279 // From AMD64 ABI Draft 0.98, page 76 2280 0xff, 0x35, // pushq contents of memory address 2281 0, 0, 0, 0, // replaced with address of .got + 8 2282 0xff, 0x25, // jmp indirect 2283 0, 0, 0, 0, // replaced with address of .got + 16 2284 0x90, 0x90, 0x90, 0x90 // noop (x4) 2285 }; 2286 2287 template<> 2288 const unsigned char 2289 Output_data_plt_x86_64_ibt<64>::first_plt_entry[plt_entry_size] = 2290 { 2291 // Use the BND prefix so that IBT is compatible with MPX. 2292 0xff, 0x35, // pushq contents of memory address 2293 0, 0, 0, 0, // replaced with address of .got + 8 2294 0xf2, 0xff, 0x25, // bnd jmp indirect 2295 0, 0, 0, 0, // replaced with address of .got + 16 2296 0x0f, 0x1f, 0x00 // nop 2297 }; 2298 2299 template<int size> 2300 void 2301 Output_data_plt_x86_64_ibt<size>::do_fill_first_plt_entry( 2302 unsigned char* pov, 2303 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 2304 typename elfcpp::Elf_types<size>::Elf_Addr plt_address) 2305 { 2306 // Offsets to the addresses needing relocation. 2307 const unsigned int roff1 = 2; 2308 const unsigned int roff2 = (size == 32) ? 8 : 9; 2309 2310 memcpy(pov, first_plt_entry, plt_entry_size); 2311 // We do a jmp relative to the PC at the end of this instruction. 2312 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1, 2313 (got_address + 8 2314 - (plt_address + roff1 + 4))); 2315 elfcpp::Swap<32, false>::writeval(pov + roff2, 2316 (got_address + 16 2317 - (plt_address + roff2 + 4))); 2318 } 2319 2320 // Subsequent entries in the IBT PLT. 2321 2322 template<> 2323 const unsigned char 2324 Output_data_plt_x86_64_ibt<32>::plt_entry[plt_entry_size] = 2325 { 2326 // From AMD64 ABI Draft 1.0-rc1, Chapter 13. 2327 0xf3, 0x0f, 0x1e, 0xfa, // endbr64 2328 0x68, // pushq immediate 2329 0, 0, 0, 0, // replaced with offset into relocation table 2330 0xe9, // jmpq relative 2331 0, 0, 0, 0, // replaced with offset to start of .plt 2332 0x90, 0x90 // nop 2333 }; 2334 2335 template<> 2336 const unsigned char 2337 Output_data_plt_x86_64_ibt<64>::plt_entry[plt_entry_size] = 2338 { 2339 // From AMD64 ABI Draft 1.0-rc1, Chapter 13. 2340 0xf3, 0x0f, 0x1e, 0xfa, // endbr64 2341 0x68, // pushq immediate 2342 0, 0, 0, 0, // replaced with offset into relocation table 2343 0xf2, 0xe9, // bnd jmpq relative 2344 0, 0, 0, 0, // replaced with offset to start of .plt 2345 0x90 // nop 2346 }; 2347 2348 // Entries in the IBT Additional PLT. 2349 2350 template<> 2351 const unsigned char 2352 Output_data_plt_x86_64_ibt<32>::aplt_entry[aplt_entry_size] = 2353 { 2354 // From AMD64 ABI Draft 1.0-rc1, Chapter 13. 2355 0xf3, 0x0f, 0x1e, 0xfa, // endbr64 2356 0xff, 0x25, // jmpq indirect 2357 0, 0, 0, 0, // replaced with address of symbol in .got 2358 0x0f, 0x1f, 0x04, 0x00, // nop 2359 0x90, 0x90 // nop 2360 }; 2361 2362 template<> 2363 const unsigned char 2364 Output_data_plt_x86_64_ibt<64>::aplt_entry[aplt_entry_size] = 2365 { 2366 // From AMD64 ABI Draft 1.0-rc1, Chapter 13. 2367 0xf3, 0x0f, 0x1e, 0xfa, // endbr64 2368 0xf2, 0xff, 0x25, // bnd jmpq indirect 2369 0, 0, 0, 0, // replaced with address of symbol in .got 2370 0x0f, 0x1f, 0x04, 0x00, // nop 2371 0x90, // nop 2372 }; 2373 2374 template<int size> 2375 unsigned int 2376 Output_data_plt_x86_64_ibt<size>::do_fill_plt_entry( 2377 unsigned char* pov, 2378 typename elfcpp::Elf_types<size>::Elf_Addr, 2379 typename elfcpp::Elf_types<size>::Elf_Addr, 2380 unsigned int, 2381 unsigned int plt_offset, 2382 unsigned int plt_index) 2383 { 2384 // Offsets to the addresses needing relocation. 2385 const unsigned int roff1 = 5; 2386 const unsigned int roff2 = (size == 32) ? 10 : 11; 2387 2388 memcpy(pov, plt_entry, plt_entry_size); 2389 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff1, plt_index); 2390 elfcpp::Swap<32, false>::writeval(pov + roff2, -(plt_offset + roff2 + 4)); 2391 return 0; 2392 } 2393 2394 template<int size> 2395 void 2396 Output_data_plt_x86_64_ibt<size>::fill_aplt_entry( 2397 unsigned char* pov, 2398 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 2399 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 2400 unsigned int got_offset, 2401 unsigned int plt_offset, 2402 unsigned int plt_index) 2403 { 2404 // Offset to the address needing relocation. 2405 const unsigned int roff = (size == 32) ? 6 : 7; 2406 2407 // Check PC-relative offset overflow in PLT entry. 2408 uint64_t plt_got_pcrel_offset = (got_address + got_offset 2409 - (plt_address + plt_offset + roff + 4)); 2410 if (Bits<32>::has_overflow(plt_got_pcrel_offset)) 2411 gold_error(_("PC-relative offset overflow in APLT entry %d"), 2412 plt_index + 1); 2413 2414 memcpy(pov, aplt_entry, aplt_entry_size); 2415 elfcpp::Swap_unaligned<32, false>::writeval(pov + roff, plt_got_pcrel_offset); 2416 } 2417 2418 // The reserved TLSDESC entry in the IBT PLT for an executable. 2419 2420 template<int size> 2421 const unsigned char 2422 Output_data_plt_x86_64_ibt<size>::tlsdesc_plt_entry[plt_entry_size] = 2423 { 2424 // From Alexandre Oliva, "Thread-Local Storage Descriptors for IA32 2425 // and AMD64/EM64T", Version 0.9.4 (2005-10-10). 2426 0xff, 0x35, // pushq x(%rip) 2427 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8) 2428 0xf2, 0xff, 0x25, // jmpq *y(%rip) 2429 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry 2430 0x0f, 0x1f, 0 // nop 2431 }; 2432 2433 template<int size> 2434 void 2435 Output_data_plt_x86_64_ibt<size>::do_fill_tlsdesc_entry( 2436 unsigned char* pov, 2437 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 2438 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 2439 typename elfcpp::Elf_types<size>::Elf_Addr got_base, 2440 unsigned int tlsdesc_got_offset, 2441 unsigned int plt_offset) 2442 { 2443 memcpy(pov, tlsdesc_plt_entry, plt_entry_size); 2444 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, 2445 (got_address + 8 2446 - (plt_address + plt_offset 2447 + 6))); 2448 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9, 2449 (got_base 2450 + tlsdesc_got_offset 2451 - (plt_address + plt_offset 2452 + 13))); 2453 } 2454 2455 // The .eh_frame unwind information for the PLT. 2456 2457 template<int size> 2458 const unsigned char 2459 Output_data_plt_x86_64<size>::plt_eh_frame_cie[plt_eh_frame_cie_size] = 2460 { 2461 1, // CIE version. 2462 'z', // Augmentation: augmentation size included. 2463 'R', // Augmentation: FDE encoding included. 2464 '\0', // End of augmentation string. 2465 1, // Code alignment factor. 2466 0x78, // Data alignment factor. 2467 16, // Return address column. 2468 1, // Augmentation size. 2469 (elfcpp::DW_EH_PE_pcrel // FDE encoding. 2470 | elfcpp::DW_EH_PE_sdata4), 2471 elfcpp::DW_CFA_def_cfa, 7, 8, // DW_CFA_def_cfa: r7 (rsp) ofs 8. 2472 elfcpp::DW_CFA_offset + 16, 1,// DW_CFA_offset: r16 (rip) at cfa-8. 2473 elfcpp::DW_CFA_nop, // Align to 16 bytes. 2474 elfcpp::DW_CFA_nop 2475 }; 2476 2477 template<int size> 2478 const unsigned char 2479 Output_data_plt_x86_64_standard<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] = 2480 { 2481 0, 0, 0, 0, // Replaced with offset to .plt. 2482 0, 0, 0, 0, // Replaced with size of .plt. 2483 0, // Augmentation size. 2484 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16. 2485 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6. 2486 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24. 2487 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16. 2488 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression. 2489 11, // Block length. 2490 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8. 2491 elfcpp::DW_OP_breg16, 0, // Push %rip. 2492 elfcpp::DW_OP_lit15, // Push 0xf. 2493 elfcpp::DW_OP_and, // & (%rip & 0xf). 2494 elfcpp::DW_OP_lit11, // Push 0xb. 2495 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 0xb) 2496 elfcpp::DW_OP_lit3, // Push 3. 2497 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 0xb) << 3) 2498 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=0xb)<<3)+%rsp+8 2499 elfcpp::DW_CFA_nop, // Align to 32 bytes. 2500 elfcpp::DW_CFA_nop, 2501 elfcpp::DW_CFA_nop, 2502 elfcpp::DW_CFA_nop 2503 }; 2504 2505 // The .eh_frame unwind information for the BND PLT. 2506 const unsigned char 2507 Output_data_plt_x86_64_bnd::plt_eh_frame_fde[plt_eh_frame_fde_size] = 2508 { 2509 0, 0, 0, 0, // Replaced with offset to .plt. 2510 0, 0, 0, 0, // Replaced with size of .plt. 2511 0, // Augmentation size. 2512 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16. 2513 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6. 2514 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24. 2515 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16. 2516 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression. 2517 11, // Block length. 2518 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8. 2519 elfcpp::DW_OP_breg16, 0, // Push %rip. 2520 elfcpp::DW_OP_lit15, // Push 0xf. 2521 elfcpp::DW_OP_and, // & (%rip & 0xf). 2522 elfcpp::DW_OP_lit5, // Push 5. 2523 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 5) 2524 elfcpp::DW_OP_lit3, // Push 3. 2525 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 5) << 3) 2526 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=5)<<3)+%rsp+8 2527 elfcpp::DW_CFA_nop, // Align to 32 bytes. 2528 elfcpp::DW_CFA_nop, 2529 elfcpp::DW_CFA_nop, 2530 elfcpp::DW_CFA_nop 2531 }; 2532 2533 // The .eh_frame unwind information for the BND PLT. 2534 template<int size> 2535 const unsigned char 2536 Output_data_plt_x86_64_ibt<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] = 2537 { 2538 0, 0, 0, 0, // Replaced with offset to .plt. 2539 0, 0, 0, 0, // Replaced with size of .plt. 2540 0, // Augmentation size. 2541 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16. 2542 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6. 2543 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24. 2544 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16. 2545 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression. 2546 11, // Block length. 2547 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8. 2548 elfcpp::DW_OP_breg16, 0, // Push %rip. 2549 elfcpp::DW_OP_lit15, // Push 0xf. 2550 elfcpp::DW_OP_and, // & (%rip & 0xf). 2551 elfcpp::DW_OP_lit9, // Push 9. 2552 elfcpp::DW_OP_ge, // >= ((%rip & 0xf) >= 9) 2553 elfcpp::DW_OP_lit3, // Push 3. 2554 elfcpp::DW_OP_shl, // << (((%rip & 0xf) >= 9) << 3) 2555 elfcpp::DW_OP_plus, // + ((((%rip&0xf)>=9)<<3)+%rsp+8 2556 elfcpp::DW_CFA_nop, // Align to 32 bytes. 2557 elfcpp::DW_CFA_nop, 2558 elfcpp::DW_CFA_nop, 2559 elfcpp::DW_CFA_nop 2560 }; 2561 2562 // Write out the PLT. This uses the hand-coded instructions above, 2563 // and adjusts them as needed. This is specified by the AMD64 ABI. 2564 2565 template<int size> 2566 void 2567 Output_data_plt_x86_64<size>::do_write(Output_file* of) 2568 { 2569 const off_t offset = this->offset(); 2570 const section_size_type oview_size = 2571 convert_to_section_size_type(this->data_size()); 2572 unsigned char* const oview = of->get_output_view(offset, oview_size); 2573 2574 const off_t got_file_offset = this->got_plt_->offset(); 2575 gold_assert(parameters->incremental_update() 2576 || (got_file_offset + this->got_plt_->data_size() 2577 == this->got_irelative_->offset())); 2578 const section_size_type got_size = 2579 convert_to_section_size_type(this->got_plt_->data_size() 2580 + this->got_irelative_->data_size()); 2581 unsigned char* const got_view = of->get_output_view(got_file_offset, 2582 got_size); 2583 2584 unsigned char* pov = oview; 2585 2586 // The base address of the .plt section. 2587 typename elfcpp::Elf_types<size>::Elf_Addr plt_address = this->address(); 2588 // The base address of the .got section. 2589 typename elfcpp::Elf_types<size>::Elf_Addr got_base = this->got_->address(); 2590 // The base address of the PLT portion of the .got section, 2591 // which is where the GOT pointer will point, and where the 2592 // three reserved GOT entries are located. 2593 typename elfcpp::Elf_types<size>::Elf_Addr got_address 2594 = this->got_plt_->address(); 2595 2596 this->fill_first_plt_entry(pov, got_address, plt_address); 2597 pov += this->get_plt_entry_size(); 2598 2599 // The first three entries in the GOT are reserved, and are written 2600 // by Output_data_got_plt_x86_64::do_write. 2601 unsigned char* got_pov = got_view + 24; 2602 2603 unsigned int plt_offset = this->get_plt_entry_size(); 2604 unsigned int got_offset = 24; 2605 const unsigned int count = this->count_ + this->irelative_count_; 2606 for (unsigned int plt_index = 0; 2607 plt_index < count; 2608 ++plt_index, 2609 pov += this->get_plt_entry_size(), 2610 got_pov += 8, 2611 plt_offset += this->get_plt_entry_size(), 2612 got_offset += 8) 2613 { 2614 // Set and adjust the PLT entry itself. 2615 unsigned int lazy_offset = this->fill_plt_entry(pov, 2616 got_address, plt_address, 2617 got_offset, plt_offset, 2618 plt_index); 2619 2620 // Set the entry in the GOT. 2621 elfcpp::Swap<64, false>::writeval(got_pov, 2622 plt_address + plt_offset + lazy_offset); 2623 } 2624 2625 if (this->has_tlsdesc_entry()) 2626 { 2627 // Set and adjust the reserved TLSDESC PLT entry. 2628 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset(); 2629 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base, 2630 tlsdesc_got_offset, plt_offset); 2631 pov += this->get_plt_entry_size(); 2632 } 2633 2634 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); 2635 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size); 2636 2637 of->write_output_view(offset, oview_size, oview); 2638 of->write_output_view(got_file_offset, got_size, got_view); 2639 } 2640 2641 // Write out the BND PLT. 2642 2643 void 2644 Output_data_plt_x86_64_bnd::do_write(Output_file* of) 2645 { 2646 const off_t offset = this->offset(); 2647 const section_size_type oview_size = 2648 convert_to_section_size_type(this->data_size()); 2649 unsigned char* const oview = of->get_output_view(offset, oview_size); 2650 2651 Output_data_got<64, false>* got = this->got(); 2652 Output_data_got_plt_x86_64* got_plt = this->got_plt(); 2653 Output_data_space* got_irelative = this->got_irelative(); 2654 2655 const off_t got_file_offset = got_plt->offset(); 2656 gold_assert(parameters->incremental_update() 2657 || (got_file_offset + got_plt->data_size() 2658 == got_irelative->offset())); 2659 const section_size_type got_size = 2660 convert_to_section_size_type(got_plt->data_size() 2661 + got_irelative->data_size()); 2662 unsigned char* const got_view = of->get_output_view(got_file_offset, 2663 got_size); 2664 2665 unsigned char* pov = oview; 2666 2667 // The base address of the .plt section. 2668 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address(); 2669 // The base address of the .got section. 2670 elfcpp::Elf_types<64>::Elf_Addr got_base = got->address(); 2671 // The base address of the PLT portion of the .got section, 2672 // which is where the GOT pointer will point, and where the 2673 // three reserved GOT entries are located. 2674 elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address(); 2675 2676 this->fill_first_plt_entry(pov, got_address, plt_address); 2677 pov += plt_entry_size; 2678 2679 // The first three entries in the GOT are reserved, and are written 2680 // by Output_data_got_plt_x86_64::do_write. 2681 unsigned char* got_pov = got_view + 24; 2682 2683 unsigned int plt_offset = plt_entry_size; 2684 unsigned int got_offset = 24; 2685 const unsigned int count = this->entry_count(); 2686 for (unsigned int plt_index = 0; 2687 plt_index < count; 2688 ++plt_index, 2689 pov += plt_entry_size, 2690 got_pov += 8, 2691 plt_offset += plt_entry_size, 2692 got_offset += 8) 2693 { 2694 // Set and adjust the PLT entry itself. 2695 unsigned int lazy_offset = this->fill_plt_entry(pov, 2696 got_address, plt_address, 2697 got_offset, plt_offset, 2698 plt_index); 2699 2700 // Set the entry in the GOT. 2701 elfcpp::Swap<64, false>::writeval(got_pov, 2702 plt_address + plt_offset + lazy_offset); 2703 } 2704 2705 if (this->has_tlsdesc_entry()) 2706 { 2707 // Set and adjust the reserved TLSDESC PLT entry. 2708 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset(); 2709 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base, 2710 tlsdesc_got_offset, plt_offset); 2711 pov += this->get_plt_entry_size(); 2712 } 2713 2714 // Write the additional PLT. 2715 got_offset = 24; 2716 for (unsigned int plt_index = 0; 2717 plt_index < count; 2718 ++plt_index, 2719 pov += aplt_entry_size, 2720 plt_offset += aplt_entry_size, 2721 got_offset += 8) 2722 { 2723 // Set and adjust the APLT entry. 2724 this->fill_aplt_entry(pov, got_address, plt_address, got_offset, 2725 plt_offset, plt_index); 2726 } 2727 2728 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); 2729 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size); 2730 2731 of->write_output_view(offset, oview_size, oview); 2732 of->write_output_view(got_file_offset, got_size, got_view); 2733 } 2734 2735 // Write out the IBT PLT. 2736 2737 template<int size> 2738 void 2739 Output_data_plt_x86_64_ibt<size>::do_write(Output_file* of) 2740 { 2741 const off_t offset = this->offset(); 2742 const section_size_type oview_size = 2743 convert_to_section_size_type(this->data_size()); 2744 unsigned char* const oview = of->get_output_view(offset, oview_size); 2745 2746 Output_data_got<64, false>* got = this->got(); 2747 Output_data_got_plt_x86_64* got_plt = this->got_plt(); 2748 Output_data_space* got_irelative = this->got_irelative(); 2749 2750 const off_t got_file_offset = got_plt->offset(); 2751 gold_assert(parameters->incremental_update() 2752 || (got_file_offset + got_plt->data_size() 2753 == got_irelative->offset())); 2754 const section_size_type got_size = 2755 convert_to_section_size_type(got_plt->data_size() 2756 + got_irelative->data_size()); 2757 unsigned char* const got_view = of->get_output_view(got_file_offset, 2758 got_size); 2759 2760 unsigned char* pov = oview; 2761 2762 // The base address of the .plt section. 2763 elfcpp::Elf_types<64>::Elf_Addr plt_address = this->address(); 2764 // The base address of the .got section. 2765 elfcpp::Elf_types<64>::Elf_Addr got_base = got->address(); 2766 // The base address of the PLT portion of the .got section, 2767 // which is where the GOT pointer will point, and where the 2768 // three reserved GOT entries are located. 2769 elfcpp::Elf_types<64>::Elf_Addr got_address = got_plt->address(); 2770 2771 this->fill_first_plt_entry(pov, got_address, plt_address); 2772 pov += plt_entry_size; 2773 2774 // The first three entries in the GOT are reserved, and are written 2775 // by Output_data_got_plt_x86_64::do_write. 2776 unsigned char* got_pov = got_view + 24; 2777 2778 unsigned int plt_offset = plt_entry_size; 2779 unsigned int got_offset = 24; 2780 const unsigned int count = this->entry_count(); 2781 for (unsigned int plt_index = 0; 2782 plt_index < count; 2783 ++plt_index, 2784 pov += plt_entry_size, 2785 got_pov += 8, 2786 plt_offset += plt_entry_size, 2787 got_offset += 8) 2788 { 2789 // Set and adjust the PLT entry itself. 2790 unsigned int lazy_offset = this->fill_plt_entry(pov, 2791 got_address, plt_address, 2792 got_offset, plt_offset, 2793 plt_index); 2794 2795 // Set the entry in the GOT. 2796 elfcpp::Swap<64, false>::writeval(got_pov, 2797 plt_address + plt_offset + lazy_offset); 2798 } 2799 2800 if (this->has_tlsdesc_entry()) 2801 { 2802 // Set and adjust the reserved TLSDESC PLT entry. 2803 unsigned int tlsdesc_got_offset = this->get_tlsdesc_got_offset(); 2804 this->fill_tlsdesc_entry(pov, got_address, plt_address, got_base, 2805 tlsdesc_got_offset, plt_offset); 2806 pov += this->get_plt_entry_size(); 2807 } 2808 2809 // Write the additional PLT. 2810 got_offset = 24; 2811 for (unsigned int plt_index = 0; 2812 plt_index < count; 2813 ++plt_index, 2814 pov += aplt_entry_size, 2815 plt_offset += aplt_entry_size, 2816 got_offset += 8) 2817 { 2818 // Set and adjust the APLT entry. 2819 this->fill_aplt_entry(pov, got_address, plt_address, got_offset, 2820 plt_offset, plt_index); 2821 } 2822 2823 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size); 2824 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size); 2825 2826 of->write_output_view(offset, oview_size, oview); 2827 of->write_output_view(got_file_offset, got_size, got_view); 2828 } 2829 2830 // Create the PLT section. 2831 2832 template<int size> 2833 void 2834 Target_x86_64<size>::make_plt_section(Symbol_table* symtab, Layout* layout) 2835 { 2836 if (this->plt_ == NULL) 2837 { 2838 // Create the GOT sections first. 2839 this->got_section(symtab, layout); 2840 2841 this->plt_ = this->make_data_plt(layout, this->got_, this->got_plt_, 2842 this->got_irelative_); 2843 2844 // Add unwind information if requested. 2845 if (parameters->options().ld_generated_unwind_info()) 2846 this->plt_->add_eh_frame(layout); 2847 2848 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, 2849 (elfcpp::SHF_ALLOC 2850 | elfcpp::SHF_EXECINSTR), 2851 this->plt_, ORDER_PLT, false); 2852 2853 // Make the sh_info field of .rela.plt point to .plt. 2854 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section(); 2855 rela_plt_os->set_info_section(this->plt_->output_section()); 2856 } 2857 } 2858 2859 template<> 2860 Output_data_plt_x86_64<32>* 2861 Target_x86_64<32>::do_make_data_plt(Layout* layout, 2862 Output_data_got<64, false>* got, 2863 Output_data_got_plt_x86_64* got_plt, 2864 Output_data_space* got_irelative) 2865 { 2866 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT) 2867 return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt, 2868 got_irelative); 2869 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt, 2870 got_irelative); 2871 } 2872 2873 template<> 2874 Output_data_plt_x86_64<64>* 2875 Target_x86_64<64>::do_make_data_plt(Layout* layout, 2876 Output_data_got<64, false>* got, 2877 Output_data_got_plt_x86_64* got_plt, 2878 Output_data_space* got_irelative) 2879 { 2880 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT) 2881 return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt, 2882 got_irelative); 2883 else if (parameters->options().bndplt()) 2884 return new Output_data_plt_x86_64_bnd(layout, got, got_plt, 2885 got_irelative); 2886 else 2887 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt, 2888 got_irelative); 2889 } 2890 2891 template<> 2892 Output_data_plt_x86_64<32>* 2893 Target_x86_64<32>::do_make_data_plt(Layout* layout, 2894 Output_data_got<64, false>* got, 2895 Output_data_got_plt_x86_64* got_plt, 2896 Output_data_space* got_irelative, 2897 unsigned int plt_count) 2898 { 2899 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT) 2900 return new Output_data_plt_x86_64_ibt<32>(layout, got, got_plt, 2901 got_irelative, plt_count); 2902 return new Output_data_plt_x86_64_standard<32>(layout, got, got_plt, 2903 got_irelative, plt_count); 2904 } 2905 2906 template<> 2907 Output_data_plt_x86_64<64>* 2908 Target_x86_64<64>::do_make_data_plt(Layout* layout, 2909 Output_data_got<64, false>* got, 2910 Output_data_got_plt_x86_64* got_plt, 2911 Output_data_space* got_irelative, 2912 unsigned int plt_count) 2913 { 2914 if (this->feature_1_ & elfcpp::GNU_PROPERTY_X86_FEATURE_1_IBT) 2915 return new Output_data_plt_x86_64_ibt<64>(layout, got, got_plt, 2916 got_irelative, plt_count); 2917 else if (parameters->options().bndplt()) 2918 return new Output_data_plt_x86_64_bnd(layout, got, got_plt, 2919 got_irelative, plt_count); 2920 else 2921 return new Output_data_plt_x86_64_standard<64>(layout, got, got_plt, 2922 got_irelative, 2923 plt_count); 2924 } 2925 2926 // Return the section for TLSDESC relocations. 2927 2928 template<int size> 2929 typename Target_x86_64<size>::Reloc_section* 2930 Target_x86_64<size>::rela_tlsdesc_section(Layout* layout) const 2931 { 2932 return this->plt_section()->rela_tlsdesc(layout); 2933 } 2934 2935 // Create a PLT entry for a global symbol. 2936 2937 template<int size> 2938 void 2939 Target_x86_64<size>::make_plt_entry(Symbol_table* symtab, Layout* layout, 2940 Symbol* gsym) 2941 { 2942 if (gsym->has_plt_offset()) 2943 return; 2944 2945 if (this->plt_ == NULL) 2946 this->make_plt_section(symtab, layout); 2947 2948 this->plt_->add_entry(symtab, layout, gsym); 2949 } 2950 2951 // Make a PLT entry for a local STT_GNU_IFUNC symbol. 2952 2953 template<int size> 2954 void 2955 Target_x86_64<size>::make_local_ifunc_plt_entry( 2956 Symbol_table* symtab, Layout* layout, 2957 Sized_relobj_file<size, false>* relobj, 2958 unsigned int local_sym_index) 2959 { 2960 if (relobj->local_has_plt_offset(local_sym_index)) 2961 return; 2962 if (this->plt_ == NULL) 2963 this->make_plt_section(symtab, layout); 2964 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout, 2965 relobj, 2966 local_sym_index); 2967 relobj->set_local_plt_offset(local_sym_index, plt_offset); 2968 } 2969 2970 // Return the number of entries in the PLT. 2971 2972 template<int size> 2973 unsigned int 2974 Target_x86_64<size>::plt_entry_count() const 2975 { 2976 if (this->plt_ == NULL) 2977 return 0; 2978 return this->plt_->entry_count(); 2979 } 2980 2981 // Return the offset of the first non-reserved PLT entry. 2982 2983 template<int size> 2984 unsigned int 2985 Target_x86_64<size>::first_plt_entry_offset() const 2986 { 2987 if (this->plt_ == NULL) 2988 return 0; 2989 return this->plt_->first_plt_entry_offset(); 2990 } 2991 2992 // Return the size of each PLT entry. 2993 2994 template<int size> 2995 unsigned int 2996 Target_x86_64<size>::plt_entry_size() const 2997 { 2998 if (this->plt_ == NULL) 2999 return 0; 3000 return this->plt_->get_plt_entry_size(); 3001 } 3002 3003 // Create the GOT and PLT sections for an incremental update. 3004 3005 template<int size> 3006 Output_data_got_base* 3007 Target_x86_64<size>::init_got_plt_for_update(Symbol_table* symtab, 3008 Layout* layout, 3009 unsigned int got_count, 3010 unsigned int plt_count) 3011 { 3012 gold_assert(this->got_ == NULL); 3013 3014 this->got_ = new Output_data_got<64, false>(got_count * 8); 3015 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS, 3016 (elfcpp::SHF_ALLOC 3017 | elfcpp::SHF_WRITE), 3018 this->got_, ORDER_RELRO_LAST, 3019 true); 3020 3021 // Add the three reserved entries. 3022 this->got_plt_ = new Output_data_got_plt_x86_64(layout, (plt_count + 3) * 8); 3023 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, 3024 (elfcpp::SHF_ALLOC 3025 | elfcpp::SHF_WRITE), 3026 this->got_plt_, ORDER_NON_RELRO_FIRST, 3027 false); 3028 3029 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT. 3030 this->global_offset_table_ = 3031 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL, 3032 Symbol_table::PREDEFINED, 3033 this->got_plt_, 3034 0, 0, elfcpp::STT_OBJECT, 3035 elfcpp::STB_LOCAL, 3036 elfcpp::STV_HIDDEN, 0, 3037 false, false); 3038 3039 // If there are any TLSDESC relocations, they get GOT entries in 3040 // .got.plt after the jump slot entries. 3041 // FIXME: Get the count for TLSDESC entries. 3042 this->got_tlsdesc_ = new Output_data_got<64, false>(0); 3043 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, 3044 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, 3045 this->got_tlsdesc_, 3046 ORDER_NON_RELRO_FIRST, false); 3047 3048 // If there are any IRELATIVE relocations, they get GOT entries in 3049 // .got.plt after the jump slot and TLSDESC entries. 3050 this->got_irelative_ = new Output_data_space(0, 8, "** GOT IRELATIVE PLT"); 3051 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS, 3052 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE, 3053 this->got_irelative_, 3054 ORDER_NON_RELRO_FIRST, false); 3055 3056 // Create the PLT section. 3057 this->plt_ = this->make_data_plt(layout, this->got_, 3058 this->got_plt_, 3059 this->got_irelative_, 3060 plt_count); 3061 3062 // Add unwind information if requested. 3063 if (parameters->options().ld_generated_unwind_info()) 3064 this->plt_->add_eh_frame(layout); 3065 3066 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS, 3067 elfcpp::SHF_ALLOC | elfcpp::SHF_EXECINSTR, 3068 this->plt_, ORDER_PLT, false); 3069 3070 // Make the sh_info field of .rela.plt point to .plt. 3071 Output_section* rela_plt_os = this->plt_->rela_plt()->output_section(); 3072 rela_plt_os->set_info_section(this->plt_->output_section()); 3073 3074 // Create the rela_dyn section. 3075 this->rela_dyn_section(layout); 3076 3077 return this->got_; 3078 } 3079 3080 // Reserve a GOT entry for a local symbol, and regenerate any 3081 // necessary dynamic relocations. 3082 3083 template<int size> 3084 void 3085 Target_x86_64<size>::reserve_local_got_entry( 3086 unsigned int got_index, 3087 Sized_relobj<size, false>* obj, 3088 unsigned int r_sym, 3089 unsigned int got_type) 3090 { 3091 unsigned int got_offset = got_index * 8; 3092 Reloc_section* rela_dyn = this->rela_dyn_section(NULL); 3093 3094 this->got_->reserve_local(got_index, obj, r_sym, got_type); 3095 switch (got_type) 3096 { 3097 case GOT_TYPE_STANDARD: 3098 if (parameters->options().output_is_position_independent()) 3099 rela_dyn->add_local_relative(obj, r_sym, elfcpp::R_X86_64_RELATIVE, 3100 this->got_, got_offset, 0, false); 3101 break; 3102 case GOT_TYPE_TLS_OFFSET: 3103 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_TPOFF64, 3104 this->got_, got_offset, 0); 3105 break; 3106 case GOT_TYPE_TLS_PAIR: 3107 this->got_->reserve_slot(got_index + 1); 3108 rela_dyn->add_local(obj, r_sym, elfcpp::R_X86_64_DTPMOD64, 3109 this->got_, got_offset, 0); 3110 break; 3111 case GOT_TYPE_TLS_DESC: 3112 gold_fatal(_("TLS_DESC not yet supported for incremental linking")); 3113 // this->got_->reserve_slot(got_index + 1); 3114 // rela_dyn->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg, 3115 // this->got_, got_offset, 0); 3116 break; 3117 default: 3118 gold_unreachable(); 3119 } 3120 } 3121 3122 // Reserve a GOT entry for a global symbol, and regenerate any 3123 // necessary dynamic relocations. 3124 3125 template<int size> 3126 void 3127 Target_x86_64<size>::reserve_global_got_entry(unsigned int got_index, 3128 Symbol* gsym, 3129 unsigned int got_type) 3130 { 3131 unsigned int got_offset = got_index * 8; 3132 Reloc_section* rela_dyn = this->rela_dyn_section(NULL); 3133 3134 this->got_->reserve_global(got_index, gsym, got_type); 3135 switch (got_type) 3136 { 3137 case GOT_TYPE_STANDARD: 3138 if (!gsym->final_value_is_known()) 3139 { 3140 if (gsym->is_from_dynobj() 3141 || gsym->is_undefined() 3142 || gsym->is_preemptible() 3143 || gsym->type() == elfcpp::STT_GNU_IFUNC) 3144 rela_dyn->add_global(gsym, elfcpp::R_X86_64_GLOB_DAT, 3145 this->got_, got_offset, 0); 3146 else 3147 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE, 3148 this->got_, got_offset, 0, false); 3149 } 3150 break; 3151 case GOT_TYPE_TLS_OFFSET: 3152 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TPOFF64, 3153 this->got_, got_offset, 0, false); 3154 break; 3155 case GOT_TYPE_TLS_PAIR: 3156 this->got_->reserve_slot(got_index + 1); 3157 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPMOD64, 3158 this->got_, got_offset, 0, false); 3159 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_DTPOFF64, 3160 this->got_, got_offset + 8, 0, false); 3161 break; 3162 case GOT_TYPE_TLS_DESC: 3163 this->got_->reserve_slot(got_index + 1); 3164 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_TLSDESC, 3165 this->got_, got_offset, 0, false); 3166 break; 3167 default: 3168 gold_unreachable(); 3169 } 3170 } 3171 3172 // Register an existing PLT entry for a global symbol. 3173 3174 template<int size> 3175 void 3176 Target_x86_64<size>::register_global_plt_entry(Symbol_table* symtab, 3177 Layout* layout, 3178 unsigned int plt_index, 3179 Symbol* gsym) 3180 { 3181 gold_assert(this->plt_ != NULL); 3182 gold_assert(!gsym->has_plt_offset()); 3183 3184 this->plt_->reserve_slot(plt_index); 3185 3186 gsym->set_plt_offset((plt_index + 1) * this->plt_entry_size()); 3187 3188 unsigned int got_offset = (plt_index + 3) * 8; 3189 this->plt_->add_relocation(symtab, layout, gsym, got_offset); 3190 } 3191 3192 // Force a COPY relocation for a given symbol. 3193 3194 template<int size> 3195 void 3196 Target_x86_64<size>::emit_copy_reloc( 3197 Symbol_table* symtab, Symbol* sym, Output_section* os, off_t offset) 3198 { 3199 this->copy_relocs_.emit_copy_reloc(symtab, 3200 symtab->get_sized_symbol<size>(sym), 3201 os, 3202 offset, 3203 this->rela_dyn_section(NULL)); 3204 } 3205 3206 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment. 3207 3208 template<int size> 3209 void 3210 Target_x86_64<size>::define_tls_base_symbol(Symbol_table* symtab, 3211 Layout* layout) 3212 { 3213 if (this->tls_base_symbol_defined_) 3214 return; 3215 3216 Output_segment* tls_segment = layout->tls_segment(); 3217 if (tls_segment != NULL) 3218 { 3219 bool is_exec = parameters->options().output_is_executable(); 3220 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL, 3221 Symbol_table::PREDEFINED, 3222 tls_segment, 0, 0, 3223 elfcpp::STT_TLS, 3224 elfcpp::STB_LOCAL, 3225 elfcpp::STV_HIDDEN, 0, 3226 (is_exec 3227 ? Symbol::SEGMENT_END 3228 : Symbol::SEGMENT_START), 3229 true); 3230 } 3231 this->tls_base_symbol_defined_ = true; 3232 } 3233 3234 // Create the reserved PLT and GOT entries for the TLS descriptor resolver. 3235 3236 template<int size> 3237 void 3238 Target_x86_64<size>::reserve_tlsdesc_entries(Symbol_table* symtab, 3239 Layout* layout) 3240 { 3241 if (this->plt_ == NULL) 3242 this->make_plt_section(symtab, layout); 3243 3244 if (!this->plt_->has_tlsdesc_entry()) 3245 { 3246 // Allocate the TLSDESC_GOT entry. 3247 Output_data_got<64, false>* got = this->got_section(symtab, layout); 3248 unsigned int got_offset = got->add_constant(0); 3249 3250 // Allocate the TLSDESC_PLT entry. 3251 this->plt_->reserve_tlsdesc_entry(got_offset); 3252 } 3253 } 3254 3255 // Create a GOT entry for the TLS module index. 3256 3257 template<int size> 3258 unsigned int 3259 Target_x86_64<size>::got_mod_index_entry(Symbol_table* symtab, Layout* layout, 3260 Sized_relobj_file<size, false>* object) 3261 { 3262 if (this->got_mod_index_offset_ == -1U) 3263 { 3264 gold_assert(symtab != NULL && layout != NULL && object != NULL); 3265 Reloc_section* rela_dyn = this->rela_dyn_section(layout); 3266 Output_data_got<64, false>* got = this->got_section(symtab, layout); 3267 unsigned int got_offset = got->add_constant(0); 3268 rela_dyn->add_local(object, 0, elfcpp::R_X86_64_DTPMOD64, got, 3269 got_offset, 0); 3270 got->add_constant(0); 3271 this->got_mod_index_offset_ = got_offset; 3272 } 3273 return this->got_mod_index_offset_; 3274 } 3275 3276 // Optimize the TLS relocation type based on what we know about the 3277 // symbol. IS_FINAL is true if the final address of this symbol is 3278 // known at link time. 3279 3280 template<int size> 3281 tls::Tls_optimization 3282 Target_x86_64<size>::optimize_tls_reloc(bool is_final, int r_type) 3283 { 3284 // If we are generating a shared library, then we can't do anything 3285 // in the linker. 3286 if (parameters->options().shared()) 3287 return tls::TLSOPT_NONE; 3288 3289 switch (r_type) 3290 { 3291 case elfcpp::R_X86_64_TLSGD: 3292 case elfcpp::R_X86_64_GOTPC32_TLSDESC: 3293 case elfcpp::R_X86_64_TLSDESC_CALL: 3294 // These are General-Dynamic which permits fully general TLS 3295 // access. Since we know that we are generating an executable, 3296 // we can convert this to Initial-Exec. If we also know that 3297 // this is a local symbol, we can further switch to Local-Exec. 3298 if (is_final) 3299 return tls::TLSOPT_TO_LE; 3300 return tls::TLSOPT_TO_IE; 3301 3302 case elfcpp::R_X86_64_TLSLD: 3303 // This is Local-Dynamic, which refers to a local symbol in the 3304 // dynamic TLS block. Since we know that we generating an 3305 // executable, we can switch to Local-Exec. 3306 return tls::TLSOPT_TO_LE; 3307 3308 case elfcpp::R_X86_64_DTPOFF32: 3309 case elfcpp::R_X86_64_DTPOFF64: 3310 // Another Local-Dynamic reloc. 3311 return tls::TLSOPT_TO_LE; 3312 3313 case elfcpp::R_X86_64_GOTTPOFF: 3314 // These are Initial-Exec relocs which get the thread offset 3315 // from the GOT. If we know that we are linking against the 3316 // local symbol, we can switch to Local-Exec, which links the 3317 // thread offset into the instruction. 3318 if (is_final) 3319 return tls::TLSOPT_TO_LE; 3320 return tls::TLSOPT_NONE; 3321 3322 case elfcpp::R_X86_64_TPOFF32: 3323 // When we already have Local-Exec, there is nothing further we 3324 // can do. 3325 return tls::TLSOPT_NONE; 3326 3327 default: 3328 gold_unreachable(); 3329 } 3330 } 3331 3332 // Get the Reference_flags for a particular relocation. 3333 3334 template<int size> 3335 int 3336 Target_x86_64<size>::Scan::get_reference_flags(unsigned int r_type) 3337 { 3338 switch (r_type) 3339 { 3340 case elfcpp::R_X86_64_NONE: 3341 case elfcpp::R_X86_64_GNU_VTINHERIT: 3342 case elfcpp::R_X86_64_GNU_VTENTRY: 3343 case elfcpp::R_X86_64_GOTPC32: 3344 case elfcpp::R_X86_64_GOTPC64: 3345 // No symbol reference. 3346 return 0; 3347 3348 case elfcpp::R_X86_64_64: 3349 case elfcpp::R_X86_64_32: 3350 case elfcpp::R_X86_64_32S: 3351 case elfcpp::R_X86_64_16: 3352 case elfcpp::R_X86_64_8: 3353 return Symbol::ABSOLUTE_REF; 3354 3355 case elfcpp::R_X86_64_PC64: 3356 case elfcpp::R_X86_64_PC32: 3357 case elfcpp::R_X86_64_PC32_BND: 3358 case elfcpp::R_X86_64_PC16: 3359 case elfcpp::R_X86_64_PC8: 3360 case elfcpp::R_X86_64_GOTOFF64: 3361 return Symbol::RELATIVE_REF; 3362 3363 case elfcpp::R_X86_64_PLT32: 3364 case elfcpp::R_X86_64_PLT32_BND: 3365 case elfcpp::R_X86_64_PLTOFF64: 3366 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF; 3367 3368 case elfcpp::R_X86_64_GOT64: 3369 case elfcpp::R_X86_64_GOT32: 3370 case elfcpp::R_X86_64_GOTPCREL64: 3371 case elfcpp::R_X86_64_GOTPCREL: 3372 case elfcpp::R_X86_64_GOTPCRELX: 3373 case elfcpp::R_X86_64_REX_GOTPCRELX: 3374 case elfcpp::R_X86_64_GOTPLT64: 3375 // Absolute in GOT. 3376 return Symbol::ABSOLUTE_REF; 3377 3378 case elfcpp::R_X86_64_TLSGD: // Global-dynamic 3379 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) 3380 case elfcpp::R_X86_64_TLSDESC_CALL: 3381 case elfcpp::R_X86_64_TLSLD: // Local-dynamic 3382 case elfcpp::R_X86_64_DTPOFF32: 3383 case elfcpp::R_X86_64_DTPOFF64: 3384 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec 3385 case elfcpp::R_X86_64_TPOFF32: // Local-exec 3386 return Symbol::TLS_REF; 3387 3388 case elfcpp::R_X86_64_COPY: 3389 case elfcpp::R_X86_64_GLOB_DAT: 3390 case elfcpp::R_X86_64_JUMP_SLOT: 3391 case elfcpp::R_X86_64_RELATIVE: 3392 case elfcpp::R_X86_64_IRELATIVE: 3393 case elfcpp::R_X86_64_TPOFF64: 3394 case elfcpp::R_X86_64_DTPMOD64: 3395 case elfcpp::R_X86_64_TLSDESC: 3396 case elfcpp::R_X86_64_SIZE32: 3397 case elfcpp::R_X86_64_SIZE64: 3398 default: 3399 // Not expected. We will give an error later. 3400 return 0; 3401 } 3402 } 3403 3404 // Report an unsupported relocation against a local symbol. 3405 3406 template<int size> 3407 void 3408 Target_x86_64<size>::Scan::unsupported_reloc_local( 3409 Sized_relobj_file<size, false>* object, 3410 unsigned int r_type) 3411 { 3412 gold_error(_("%s: unsupported reloc %u against local symbol"), 3413 object->name().c_str(), r_type); 3414 } 3415 3416 // We are about to emit a dynamic relocation of type R_TYPE. If the 3417 // dynamic linker does not support it, issue an error. The GNU linker 3418 // only issues a non-PIC error for an allocated read-only section. 3419 // Here we know the section is allocated, but we don't know that it is 3420 // read-only. But we check for all the relocation types which the 3421 // glibc dynamic linker supports, so it seems appropriate to issue an 3422 // error even if the section is not read-only. If GSYM is not NULL, 3423 // it is the symbol the relocation is against; if it is NULL, the 3424 // relocation is against a local symbol. 3425 3426 template<int size> 3427 void 3428 Target_x86_64<size>::Scan::check_non_pic(Relobj* object, unsigned int r_type, 3429 Symbol* gsym) 3430 { 3431 switch (r_type) 3432 { 3433 // These are the relocation types supported by glibc for x86_64 3434 // which should always work. 3435 case elfcpp::R_X86_64_RELATIVE: 3436 case elfcpp::R_X86_64_IRELATIVE: 3437 case elfcpp::R_X86_64_GLOB_DAT: 3438 case elfcpp::R_X86_64_JUMP_SLOT: 3439 case elfcpp::R_X86_64_DTPMOD64: 3440 case elfcpp::R_X86_64_DTPOFF64: 3441 case elfcpp::R_X86_64_TPOFF64: 3442 case elfcpp::R_X86_64_64: 3443 case elfcpp::R_X86_64_COPY: 3444 return; 3445 3446 // glibc supports these reloc types, but they can overflow. 3447 case elfcpp::R_X86_64_PC32: 3448 case elfcpp::R_X86_64_PC32_BND: 3449 // A PC relative reference is OK against a local symbol or if 3450 // the symbol is defined locally. 3451 if (gsym == NULL 3452 || (!gsym->is_from_dynobj() 3453 && !gsym->is_undefined() 3454 && !gsym->is_preemptible())) 3455 return; 3456 // Fall through. 3457 case elfcpp::R_X86_64_32: 3458 // R_X86_64_32 is OK for x32. 3459 if (size == 32 && r_type == elfcpp::R_X86_64_32) 3460 return; 3461 if (this->issued_non_pic_error_) 3462 return; 3463 gold_assert(parameters->options().output_is_position_independent()); 3464 if (gsym == NULL) 3465 object->error(_("requires dynamic R_X86_64_32 reloc which may " 3466 "overflow at runtime; recompile with -fPIC")); 3467 else 3468 { 3469 const char *r_name; 3470 switch (r_type) 3471 { 3472 case elfcpp::R_X86_64_32: 3473 r_name = "R_X86_64_32"; 3474 break; 3475 case elfcpp::R_X86_64_PC32: 3476 r_name = "R_X86_64_PC32"; 3477 break; 3478 case elfcpp::R_X86_64_PC32_BND: 3479 r_name = "R_X86_64_PC32_BND"; 3480 break; 3481 default: 3482 gold_unreachable(); 3483 break; 3484 } 3485 object->error(_("requires dynamic %s reloc against '%s' " 3486 "which may overflow at runtime; recompile " 3487 "with -fPIC"), 3488 r_name, gsym->name()); 3489 } 3490 this->issued_non_pic_error_ = true; 3491 return; 3492 3493 default: 3494 // This prevents us from issuing more than one error per reloc 3495 // section. But we can still wind up issuing more than one 3496 // error per object file. 3497 if (this->issued_non_pic_error_) 3498 return; 3499 gold_assert(parameters->options().output_is_position_independent()); 3500 object->error(_("requires unsupported dynamic reloc %u; " 3501 "recompile with -fPIC"), 3502 r_type); 3503 this->issued_non_pic_error_ = true; 3504 return; 3505 3506 case elfcpp::R_X86_64_NONE: 3507 gold_unreachable(); 3508 } 3509 } 3510 3511 // Return whether we need to make a PLT entry for a relocation of the 3512 // given type against a STT_GNU_IFUNC symbol. 3513 3514 template<int size> 3515 bool 3516 Target_x86_64<size>::Scan::reloc_needs_plt_for_ifunc( 3517 Sized_relobj_file<size, false>* object, 3518 unsigned int r_type) 3519 { 3520 int flags = Scan::get_reference_flags(r_type); 3521 if (flags & Symbol::TLS_REF) 3522 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"), 3523 object->name().c_str(), r_type); 3524 return flags != 0; 3525 } 3526 3527 // Scan a relocation for a local symbol. 3528 3529 template<int size> 3530 inline void 3531 Target_x86_64<size>::Scan::local(Symbol_table* symtab, 3532 Layout* layout, 3533 Target_x86_64<size>* target, 3534 Sized_relobj_file<size, false>* object, 3535 unsigned int data_shndx, 3536 Output_section* output_section, 3537 const elfcpp::Rela<size, false>& reloc, 3538 unsigned int r_type, 3539 const elfcpp::Sym<size, false>& lsym, 3540 bool is_discarded) 3541 { 3542 if (is_discarded) 3543 return; 3544 3545 // A local STT_GNU_IFUNC symbol may require a PLT entry. 3546 bool is_ifunc = lsym.get_st_type() == elfcpp::STT_GNU_IFUNC; 3547 if (is_ifunc && this->reloc_needs_plt_for_ifunc(object, r_type)) 3548 { 3549 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 3550 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym); 3551 } 3552 3553 switch (r_type) 3554 { 3555 case elfcpp::R_X86_64_NONE: 3556 case elfcpp::R_X86_64_GNU_VTINHERIT: 3557 case elfcpp::R_X86_64_GNU_VTENTRY: 3558 break; 3559 3560 case elfcpp::R_X86_64_64: 3561 // If building a shared library (or a position-independent 3562 // executable), we need to create a dynamic relocation for this 3563 // location. The relocation applied at link time will apply the 3564 // link-time value, so we flag the location with an 3565 // R_X86_64_RELATIVE relocation so the dynamic loader can 3566 // relocate it easily. 3567 if (parameters->options().output_is_position_independent()) 3568 { 3569 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 3570 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 3571 rela_dyn->add_local_relative(object, r_sym, 3572 (size == 32 3573 ? elfcpp::R_X86_64_RELATIVE64 3574 : elfcpp::R_X86_64_RELATIVE), 3575 output_section, data_shndx, 3576 reloc.get_r_offset(), 3577 reloc.get_r_addend(), is_ifunc); 3578 } 3579 break; 3580 3581 case elfcpp::R_X86_64_32: 3582 case elfcpp::R_X86_64_32S: 3583 case elfcpp::R_X86_64_16: 3584 case elfcpp::R_X86_64_8: 3585 // If building a shared library (or a position-independent 3586 // executable), we need to create a dynamic relocation for this 3587 // location. We can't use an R_X86_64_RELATIVE relocation 3588 // because that is always a 64-bit relocation. 3589 if (parameters->options().output_is_position_independent()) 3590 { 3591 // Use R_X86_64_RELATIVE relocation for R_X86_64_32 under x32. 3592 if (size == 32 && r_type == elfcpp::R_X86_64_32) 3593 { 3594 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 3595 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 3596 rela_dyn->add_local_relative(object, r_sym, 3597 elfcpp::R_X86_64_RELATIVE, 3598 output_section, data_shndx, 3599 reloc.get_r_offset(), 3600 reloc.get_r_addend(), is_ifunc); 3601 break; 3602 } 3603 3604 this->check_non_pic(object, r_type, NULL); 3605 3606 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 3607 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 3608 if (lsym.get_st_type() != elfcpp::STT_SECTION) 3609 rela_dyn->add_local(object, r_sym, r_type, output_section, 3610 data_shndx, reloc.get_r_offset(), 3611 reloc.get_r_addend()); 3612 else 3613 { 3614 gold_assert(lsym.get_st_value() == 0); 3615 unsigned int shndx = lsym.get_st_shndx(); 3616 bool is_ordinary; 3617 shndx = object->adjust_sym_shndx(r_sym, shndx, 3618 &is_ordinary); 3619 if (!is_ordinary) 3620 object->error(_("section symbol %u has bad shndx %u"), 3621 r_sym, shndx); 3622 else 3623 rela_dyn->add_local_section(object, shndx, 3624 r_type, output_section, 3625 data_shndx, reloc.get_r_offset(), 3626 reloc.get_r_addend()); 3627 } 3628 } 3629 break; 3630 3631 case elfcpp::R_X86_64_PC64: 3632 case elfcpp::R_X86_64_PC32: 3633 case elfcpp::R_X86_64_PC32_BND: 3634 case elfcpp::R_X86_64_PC16: 3635 case elfcpp::R_X86_64_PC8: 3636 break; 3637 3638 case elfcpp::R_X86_64_PLT32: 3639 case elfcpp::R_X86_64_PLT32_BND: 3640 // Since we know this is a local symbol, we can handle this as a 3641 // PC32 reloc. 3642 break; 3643 3644 case elfcpp::R_X86_64_GOTPC32: 3645 case elfcpp::R_X86_64_GOTOFF64: 3646 case elfcpp::R_X86_64_GOTPC64: 3647 case elfcpp::R_X86_64_PLTOFF64: 3648 // We need a GOT section. 3649 target->got_section(symtab, layout); 3650 // For PLTOFF64, we'd normally want a PLT section, but since we 3651 // know this is a local symbol, no PLT is needed. 3652 break; 3653 3654 case elfcpp::R_X86_64_GOT64: 3655 case elfcpp::R_X86_64_GOT32: 3656 case elfcpp::R_X86_64_GOTPCREL64: 3657 case elfcpp::R_X86_64_GOTPCREL: 3658 case elfcpp::R_X86_64_GOTPCRELX: 3659 case elfcpp::R_X86_64_REX_GOTPCRELX: 3660 case elfcpp::R_X86_64_GOTPLT64: 3661 { 3662 // The symbol requires a GOT section. 3663 Output_data_got<64, false>* got = target->got_section(symtab, layout); 3664 3665 // If the relocation symbol isn't IFUNC, 3666 // and is local, then we will convert 3667 // mov foo@GOTPCREL(%rip), %reg 3668 // to lea foo(%rip), %reg. 3669 // in Relocate::relocate. 3670 if (!parameters->incremental() 3671 && (r_type == elfcpp::R_X86_64_GOTPCREL 3672 || r_type == elfcpp::R_X86_64_GOTPCRELX 3673 || r_type == elfcpp::R_X86_64_REX_GOTPCRELX) 3674 && reloc.get_r_offset() >= 2 3675 && !is_ifunc) 3676 { 3677 section_size_type stype; 3678 const unsigned char* view = object->section_contents(data_shndx, 3679 &stype, true); 3680 if (view[reloc.get_r_offset() - 2] == 0x8b) 3681 break; 3682 } 3683 3684 // The symbol requires a GOT entry. 3685 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 3686 3687 // For a STT_GNU_IFUNC symbol we want the PLT offset. That 3688 // lets function pointers compare correctly with shared 3689 // libraries. Otherwise we would need an IRELATIVE reloc. 3690 bool is_new; 3691 if (is_ifunc) 3692 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD); 3693 else 3694 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD); 3695 if (is_new) 3696 { 3697 // If we are generating a shared object, we need to add a 3698 // dynamic relocation for this symbol's GOT entry. 3699 if (parameters->options().output_is_position_independent()) 3700 { 3701 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 3702 // R_X86_64_RELATIVE assumes a 64-bit relocation. 3703 if (r_type != elfcpp::R_X86_64_GOT32) 3704 { 3705 unsigned int got_offset = 3706 object->local_got_offset(r_sym, GOT_TYPE_STANDARD); 3707 rela_dyn->add_local_relative(object, r_sym, 3708 elfcpp::R_X86_64_RELATIVE, 3709 got, got_offset, 0, is_ifunc); 3710 } 3711 else 3712 { 3713 this->check_non_pic(object, r_type, NULL); 3714 3715 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION); 3716 rela_dyn->add_local( 3717 object, r_sym, r_type, got, 3718 object->local_got_offset(r_sym, GOT_TYPE_STANDARD), 0); 3719 } 3720 } 3721 } 3722 // For GOTPLT64, we'd normally want a PLT section, but since 3723 // we know this is a local symbol, no PLT is needed. 3724 } 3725 break; 3726 3727 case elfcpp::R_X86_64_COPY: 3728 case elfcpp::R_X86_64_GLOB_DAT: 3729 case elfcpp::R_X86_64_JUMP_SLOT: 3730 case elfcpp::R_X86_64_RELATIVE: 3731 case elfcpp::R_X86_64_IRELATIVE: 3732 // These are outstanding tls relocs, which are unexpected when linking 3733 case elfcpp::R_X86_64_TPOFF64: 3734 case elfcpp::R_X86_64_DTPMOD64: 3735 case elfcpp::R_X86_64_TLSDESC: 3736 gold_error(_("%s: unexpected reloc %u in object file"), 3737 object->name().c_str(), r_type); 3738 break; 3739 3740 // These are initial tls relocs, which are expected when linking 3741 case elfcpp::R_X86_64_TLSGD: // Global-dynamic 3742 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) 3743 case elfcpp::R_X86_64_TLSDESC_CALL: 3744 case elfcpp::R_X86_64_TLSLD: // Local-dynamic 3745 case elfcpp::R_X86_64_DTPOFF32: 3746 case elfcpp::R_X86_64_DTPOFF64: 3747 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec 3748 case elfcpp::R_X86_64_TPOFF32: // Local-exec 3749 { 3750 bool output_is_shared = parameters->options().shared(); 3751 const tls::Tls_optimization optimized_type 3752 = Target_x86_64<size>::optimize_tls_reloc(!output_is_shared, 3753 r_type); 3754 switch (r_type) 3755 { 3756 case elfcpp::R_X86_64_TLSGD: // General-dynamic 3757 if (optimized_type == tls::TLSOPT_NONE) 3758 { 3759 // Create a pair of GOT entries for the module index and 3760 // dtv-relative offset. 3761 Output_data_got<64, false>* got 3762 = target->got_section(symtab, layout); 3763 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 3764 unsigned int shndx = lsym.get_st_shndx(); 3765 bool is_ordinary; 3766 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); 3767 if (!is_ordinary) 3768 object->error(_("local symbol %u has bad shndx %u"), 3769 r_sym, shndx); 3770 else 3771 got->add_local_pair_with_rel(object, r_sym, 3772 shndx, 3773 GOT_TYPE_TLS_PAIR, 3774 target->rela_dyn_section(layout), 3775 elfcpp::R_X86_64_DTPMOD64); 3776 } 3777 else if (optimized_type != tls::TLSOPT_TO_LE) 3778 unsupported_reloc_local(object, r_type); 3779 break; 3780 3781 case elfcpp::R_X86_64_GOTPC32_TLSDESC: 3782 target->define_tls_base_symbol(symtab, layout); 3783 if (optimized_type == tls::TLSOPT_NONE) 3784 { 3785 // Create reserved PLT and GOT entries for the resolver. 3786 target->reserve_tlsdesc_entries(symtab, layout); 3787 3788 // Generate a double GOT entry with an 3789 // R_X86_64_TLSDESC reloc. The R_X86_64_TLSDESC reloc 3790 // is resolved lazily, so the GOT entry needs to be in 3791 // an area in .got.plt, not .got. Call got_section to 3792 // make sure the section has been created. 3793 target->got_section(symtab, layout); 3794 Output_data_got<64, false>* got = target->got_tlsdesc_section(); 3795 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 3796 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC)) 3797 { 3798 unsigned int got_offset = got->add_constant(0); 3799 got->add_constant(0); 3800 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC, 3801 got_offset); 3802 Reloc_section* rt = target->rela_tlsdesc_section(layout); 3803 // We store the arguments we need in a vector, and 3804 // use the index into the vector as the parameter 3805 // to pass to the target specific routines. 3806 uintptr_t intarg = target->add_tlsdesc_info(object, r_sym); 3807 void* arg = reinterpret_cast<void*>(intarg); 3808 rt->add_target_specific(elfcpp::R_X86_64_TLSDESC, arg, 3809 got, got_offset, 0); 3810 } 3811 } 3812 else if (optimized_type != tls::TLSOPT_TO_LE) 3813 unsupported_reloc_local(object, r_type); 3814 break; 3815 3816 case elfcpp::R_X86_64_TLSDESC_CALL: 3817 break; 3818 3819 case elfcpp::R_X86_64_TLSLD: // Local-dynamic 3820 if (optimized_type == tls::TLSOPT_NONE) 3821 { 3822 // Create a GOT entry for the module index. 3823 target->got_mod_index_entry(symtab, layout, object); 3824 } 3825 else if (optimized_type != tls::TLSOPT_TO_LE) 3826 unsupported_reloc_local(object, r_type); 3827 break; 3828 3829 case elfcpp::R_X86_64_DTPOFF32: 3830 case elfcpp::R_X86_64_DTPOFF64: 3831 break; 3832 3833 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec 3834 layout->set_has_static_tls(); 3835 if (optimized_type == tls::TLSOPT_NONE) 3836 { 3837 // Create a GOT entry for the tp-relative offset. 3838 Output_data_got<64, false>* got 3839 = target->got_section(symtab, layout); 3840 unsigned int r_sym = elfcpp::elf_r_sym<size>(reloc.get_r_info()); 3841 got->add_local_with_rel(object, r_sym, GOT_TYPE_TLS_OFFSET, 3842 target->rela_dyn_section(layout), 3843 elfcpp::R_X86_64_TPOFF64); 3844 } 3845 else if (optimized_type != tls::TLSOPT_TO_LE) 3846 unsupported_reloc_local(object, r_type); 3847 break; 3848 3849 case elfcpp::R_X86_64_TPOFF32: // Local-exec 3850 layout->set_has_static_tls(); 3851 if (output_is_shared) 3852 unsupported_reloc_local(object, r_type); 3853 break; 3854 3855 default: 3856 gold_unreachable(); 3857 } 3858 } 3859 break; 3860 3861 case elfcpp::R_X86_64_SIZE32: 3862 case elfcpp::R_X86_64_SIZE64: 3863 default: 3864 gold_error(_("%s: unsupported reloc %u against local symbol"), 3865 object->name().c_str(), r_type); 3866 break; 3867 } 3868 } 3869 3870 3871 // Report an unsupported relocation against a global symbol. 3872 3873 template<int size> 3874 void 3875 Target_x86_64<size>::Scan::unsupported_reloc_global( 3876 Sized_relobj_file<size, false>* object, 3877 unsigned int r_type, 3878 Symbol* gsym) 3879 { 3880 gold_error(_("%s: unsupported reloc %u against global symbol %s"), 3881 object->name().c_str(), r_type, gsym->demangled_name().c_str()); 3882 } 3883 3884 // Returns true if this relocation type could be that of a function pointer. 3885 template<int size> 3886 inline bool 3887 Target_x86_64<size>::Scan::possible_function_pointer_reloc( 3888 Sized_relobj_file<size, false>* src_obj, 3889 unsigned int src_indx, 3890 unsigned int r_offset, 3891 unsigned int r_type) 3892 { 3893 switch (r_type) 3894 { 3895 case elfcpp::R_X86_64_64: 3896 case elfcpp::R_X86_64_32: 3897 case elfcpp::R_X86_64_32S: 3898 case elfcpp::R_X86_64_16: 3899 case elfcpp::R_X86_64_8: 3900 case elfcpp::R_X86_64_GOT64: 3901 case elfcpp::R_X86_64_GOT32: 3902 case elfcpp::R_X86_64_GOTPCREL64: 3903 case elfcpp::R_X86_64_GOTPCREL: 3904 case elfcpp::R_X86_64_GOTPCRELX: 3905 case elfcpp::R_X86_64_REX_GOTPCRELX: 3906 case elfcpp::R_X86_64_GOTPLT64: 3907 { 3908 return true; 3909 } 3910 case elfcpp::R_X86_64_PC32: 3911 { 3912 // This relocation may be used both for function calls and 3913 // for taking address of a function. We distinguish between 3914 // them by checking the opcodes. 3915 uint64_t sh_flags = src_obj->section_flags(src_indx); 3916 bool is_executable = (sh_flags & elfcpp::SHF_EXECINSTR) != 0; 3917 if (is_executable) 3918 { 3919 section_size_type stype; 3920 const unsigned char* view = src_obj->section_contents(src_indx, 3921 &stype, 3922 true); 3923 3924 // call 3925 if (r_offset >= 1 3926 && view[r_offset - 1] == 0xe8) 3927 return false; 3928 3929 // jmp 3930 if (r_offset >= 1 3931 && view[r_offset - 1] == 0xe9) 3932 return false; 3933 3934 // jo/jno/jb/jnb/je/jne/jna/ja/js/jns/jp/jnp/jl/jge/jle/jg 3935 if (r_offset >= 2 3936 && view[r_offset - 2] == 0x0f 3937 && view[r_offset - 1] >= 0x80 3938 && view[r_offset - 1] <= 0x8f) 3939 return false; 3940 } 3941 3942 // Be conservative and treat all others as function pointers. 3943 return true; 3944 } 3945 } 3946 return false; 3947 } 3948 3949 // For safe ICF, scan a relocation for a local symbol to check if it 3950 // corresponds to a function pointer being taken. In that case mark 3951 // the function whose pointer was taken as not foldable. 3952 3953 template<int size> 3954 inline bool 3955 Target_x86_64<size>::Scan::local_reloc_may_be_function_pointer( 3956 Symbol_table* , 3957 Layout* , 3958 Target_x86_64<size>* , 3959 Sized_relobj_file<size, false>* src_obj, 3960 unsigned int src_indx, 3961 Output_section* , 3962 const elfcpp::Rela<size, false>& reloc, 3963 unsigned int r_type, 3964 const elfcpp::Sym<size, false>&) 3965 { 3966 // When building a shared library, do not fold any local symbols as it is 3967 // not possible to distinguish pointer taken versus a call by looking at 3968 // the relocation types. 3969 if (parameters->options().shared()) 3970 return true; 3971 3972 return possible_function_pointer_reloc(src_obj, src_indx, 3973 reloc.get_r_offset(), r_type); 3974 } 3975 3976 // For safe ICF, scan a relocation for a global symbol to check if it 3977 // corresponds to a function pointer being taken. In that case mark 3978 // the function whose pointer was taken as not foldable. 3979 3980 template<int size> 3981 inline bool 3982 Target_x86_64<size>::Scan::global_reloc_may_be_function_pointer( 3983 Symbol_table*, 3984 Layout* , 3985 Target_x86_64<size>* , 3986 Sized_relobj_file<size, false>* src_obj, 3987 unsigned int src_indx, 3988 Output_section* , 3989 const elfcpp::Rela<size, false>& reloc, 3990 unsigned int r_type, 3991 Symbol* gsym) 3992 { 3993 // When building a shared library, do not fold symbols whose visibility 3994 // is hidden, internal or protected. 3995 if (parameters->options().shared() 3996 && (gsym->visibility() == elfcpp::STV_INTERNAL 3997 || gsym->visibility() == elfcpp::STV_PROTECTED 3998 || gsym->visibility() == elfcpp::STV_HIDDEN)) 3999 return true; 4000 4001 return possible_function_pointer_reloc(src_obj, src_indx, 4002 reloc.get_r_offset(), r_type); 4003 } 4004 4005 // Scan a relocation for a global symbol. 4006 4007 template<int size> 4008 inline void 4009 Target_x86_64<size>::Scan::global(Symbol_table* symtab, 4010 Layout* layout, 4011 Target_x86_64<size>* target, 4012 Sized_relobj_file<size, false>* object, 4013 unsigned int data_shndx, 4014 Output_section* output_section, 4015 const elfcpp::Rela<size, false>& reloc, 4016 unsigned int r_type, 4017 Symbol* gsym) 4018 { 4019 // A STT_GNU_IFUNC symbol may require a PLT entry. 4020 if (gsym->type() == elfcpp::STT_GNU_IFUNC 4021 && this->reloc_needs_plt_for_ifunc(object, r_type)) 4022 target->make_plt_entry(symtab, layout, gsym); 4023 4024 switch (r_type) 4025 { 4026 case elfcpp::R_X86_64_NONE: 4027 case elfcpp::R_X86_64_GNU_VTINHERIT: 4028 case elfcpp::R_X86_64_GNU_VTENTRY: 4029 break; 4030 4031 case elfcpp::R_X86_64_64: 4032 case elfcpp::R_X86_64_32: 4033 case elfcpp::R_X86_64_32S: 4034 case elfcpp::R_X86_64_16: 4035 case elfcpp::R_X86_64_8: 4036 { 4037 // Make a PLT entry if necessary. 4038 if (gsym->needs_plt_entry()) 4039 { 4040 target->make_plt_entry(symtab, layout, gsym); 4041 // Since this is not a PC-relative relocation, we may be 4042 // taking the address of a function. In that case we need to 4043 // set the entry in the dynamic symbol table to the address of 4044 // the PLT entry. 4045 if (gsym->is_from_dynobj() && !parameters->options().shared()) 4046 gsym->set_needs_dynsym_value(); 4047 } 4048 // Make a dynamic relocation if necessary. 4049 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) 4050 { 4051 if (!parameters->options().output_is_position_independent() 4052 && gsym->may_need_copy_reloc()) 4053 { 4054 target->copy_reloc(symtab, layout, object, 4055 data_shndx, output_section, gsym, reloc); 4056 } 4057 else if (((size == 64 && r_type == elfcpp::R_X86_64_64) 4058 || (size == 32 && r_type == elfcpp::R_X86_64_32)) 4059 && gsym->type() == elfcpp::STT_GNU_IFUNC 4060 && gsym->can_use_relative_reloc(false) 4061 && !gsym->is_from_dynobj() 4062 && !gsym->is_undefined() 4063 && !gsym->is_preemptible()) 4064 { 4065 // Use an IRELATIVE reloc for a locally defined 4066 // STT_GNU_IFUNC symbol. This makes a function 4067 // address in a PIE executable match the address in a 4068 // shared library that it links against. 4069 Reloc_section* rela_dyn = 4070 target->rela_irelative_section(layout); 4071 unsigned int r_type = elfcpp::R_X86_64_IRELATIVE; 4072 rela_dyn->add_symbolless_global_addend(gsym, r_type, 4073 output_section, object, 4074 data_shndx, 4075 reloc.get_r_offset(), 4076 reloc.get_r_addend()); 4077 } 4078 else if (((size == 64 && r_type == elfcpp::R_X86_64_64) 4079 || (size == 32 && r_type == elfcpp::R_X86_64_32)) 4080 && gsym->can_use_relative_reloc(false)) 4081 { 4082 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 4083 rela_dyn->add_global_relative(gsym, elfcpp::R_X86_64_RELATIVE, 4084 output_section, object, 4085 data_shndx, 4086 reloc.get_r_offset(), 4087 reloc.get_r_addend(), false); 4088 } 4089 else 4090 { 4091 this->check_non_pic(object, r_type, gsym); 4092 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 4093 rela_dyn->add_global(gsym, r_type, output_section, object, 4094 data_shndx, reloc.get_r_offset(), 4095 reloc.get_r_addend()); 4096 } 4097 } 4098 } 4099 break; 4100 4101 case elfcpp::R_X86_64_PC64: 4102 case elfcpp::R_X86_64_PC32: 4103 case elfcpp::R_X86_64_PC32_BND: 4104 case elfcpp::R_X86_64_PC16: 4105 case elfcpp::R_X86_64_PC8: 4106 { 4107 // Make a PLT entry if necessary. 4108 if (gsym->needs_plt_entry()) 4109 target->make_plt_entry(symtab, layout, gsym); 4110 // Make a dynamic relocation if necessary. 4111 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))) 4112 { 4113 if (parameters->options().output_is_executable() 4114 && gsym->may_need_copy_reloc()) 4115 { 4116 target->copy_reloc(symtab, layout, object, 4117 data_shndx, output_section, gsym, reloc); 4118 } 4119 else 4120 { 4121 this->check_non_pic(object, r_type, gsym); 4122 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 4123 rela_dyn->add_global(gsym, r_type, output_section, object, 4124 data_shndx, reloc.get_r_offset(), 4125 reloc.get_r_addend()); 4126 } 4127 } 4128 } 4129 break; 4130 4131 case elfcpp::R_X86_64_GOT64: 4132 case elfcpp::R_X86_64_GOT32: 4133 case elfcpp::R_X86_64_GOTPCREL64: 4134 case elfcpp::R_X86_64_GOTPCREL: 4135 case elfcpp::R_X86_64_GOTPCRELX: 4136 case elfcpp::R_X86_64_REX_GOTPCRELX: 4137 case elfcpp::R_X86_64_GOTPLT64: 4138 { 4139 // The symbol requires a GOT entry. 4140 Output_data_got<64, false>* got = target->got_section(symtab, layout); 4141 4142 // If we convert this from 4143 // mov foo@GOTPCREL(%rip), %reg 4144 // to lea foo(%rip), %reg. 4145 // OR 4146 // if we convert 4147 // (callq|jmpq) *foo@GOTPCRELX(%rip) to 4148 // (callq|jmpq) foo 4149 // in Relocate::relocate, then there is nothing to do here. 4150 // We cannot make these optimizations in incremental linking mode, 4151 // because we look at the opcode to decide whether or not to make 4152 // change, and during an incremental update, the change may have 4153 // already been applied. 4154 4155 Lazy_view<size> view(object, data_shndx); 4156 size_t r_offset = reloc.get_r_offset(); 4157 if (!parameters->incremental() 4158 && r_offset >= 2 4159 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type, 4160 r_offset, &view)) 4161 break; 4162 4163 if (!parameters->incremental() 4164 && r_offset >= 2 4165 && Target_x86_64<size>::can_convert_callq_to_direct(gsym, r_type, 4166 r_offset, 4167 &view)) 4168 break; 4169 4170 if (gsym->final_value_is_known()) 4171 { 4172 // For a STT_GNU_IFUNC symbol we want the PLT address. 4173 if (gsym->type() == elfcpp::STT_GNU_IFUNC) 4174 got->add_global_plt(gsym, GOT_TYPE_STANDARD); 4175 else 4176 got->add_global(gsym, GOT_TYPE_STANDARD); 4177 } 4178 else 4179 { 4180 // If this symbol is not fully resolved, we need to add a 4181 // dynamic relocation for it. 4182 Reloc_section* rela_dyn = target->rela_dyn_section(layout); 4183 4184 // Use a GLOB_DAT rather than a RELATIVE reloc if: 4185 // 4186 // 1) The symbol may be defined in some other module. 4187 // 4188 // 2) We are building a shared library and this is a 4189 // protected symbol; using GLOB_DAT means that the dynamic 4190 // linker can use the address of the PLT in the main 4191 // executable when appropriate so that function address 4192 // comparisons work. 4193 // 4194 // 3) This is a STT_GNU_IFUNC symbol in position dependent 4195 // code, again so that function address comparisons work. 4196 if (gsym->is_from_dynobj() 4197 || gsym->is_undefined() 4198 || gsym->is_preemptible() 4199 || (gsym->visibility() == elfcpp::STV_PROTECTED 4200 && parameters->options().shared()) 4201 || (gsym->type() == elfcpp::STT_GNU_IFUNC 4202 && parameters->options().output_is_position_independent())) 4203 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD, rela_dyn, 4204 elfcpp::R_X86_64_GLOB_DAT); 4205 else 4206 { 4207 // For a STT_GNU_IFUNC symbol we want to write the PLT 4208 // offset into the GOT, so that function pointer 4209 // comparisons work correctly. 4210 bool is_new; 4211 if (gsym->type() != elfcpp::STT_GNU_IFUNC) 4212 is_new = got->add_global(gsym, GOT_TYPE_STANDARD); 4213 else 4214 { 4215 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD); 4216 // Tell the dynamic linker to use the PLT address 4217 // when resolving relocations. 4218 if (gsym->is_from_dynobj() 4219 && !parameters->options().shared()) 4220 gsym->set_needs_dynsym_value(); 4221 } 4222 if (is_new) 4223 { 4224 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD); 4225 rela_dyn->add_global_relative(gsym, 4226 elfcpp::R_X86_64_RELATIVE, 4227 got, got_off, 0, false); 4228 } 4229 } 4230 } 4231 } 4232 break; 4233 4234 case elfcpp::R_X86_64_PLT32: 4235 case elfcpp::R_X86_64_PLT32_BND: 4236 // If the symbol is fully resolved, this is just a PC32 reloc. 4237 // Otherwise we need a PLT entry. 4238 if (gsym->final_value_is_known()) 4239 break; 4240 // If building a shared library, we can also skip the PLT entry 4241 // if the symbol is defined in the output file and is protected 4242 // or hidden. 4243 if (gsym->is_defined() 4244 && !gsym->is_from_dynobj() 4245 && !gsym->is_preemptible()) 4246 break; 4247 target->make_plt_entry(symtab, layout, gsym); 4248 break; 4249 4250 case elfcpp::R_X86_64_GOTPC32: 4251 case elfcpp::R_X86_64_GOTOFF64: 4252 case elfcpp::R_X86_64_GOTPC64: 4253 case elfcpp::R_X86_64_PLTOFF64: 4254 // We need a GOT section. 4255 target->got_section(symtab, layout); 4256 // For PLTOFF64, we also need a PLT entry (but only if the 4257 // symbol is not fully resolved). 4258 if (r_type == elfcpp::R_X86_64_PLTOFF64 4259 && !gsym->final_value_is_known()) 4260 target->make_plt_entry(symtab, layout, gsym); 4261 break; 4262 4263 case elfcpp::R_X86_64_COPY: 4264 case elfcpp::R_X86_64_GLOB_DAT: 4265 case elfcpp::R_X86_64_JUMP_SLOT: 4266 case elfcpp::R_X86_64_RELATIVE: 4267 case elfcpp::R_X86_64_IRELATIVE: 4268 // These are outstanding tls relocs, which are unexpected when linking 4269 case elfcpp::R_X86_64_TPOFF64: 4270 case elfcpp::R_X86_64_DTPMOD64: 4271 case elfcpp::R_X86_64_TLSDESC: 4272 gold_error(_("%s: unexpected reloc %u in object file"), 4273 object->name().c_str(), r_type); 4274 break; 4275 4276 // These are initial tls relocs, which are expected for global() 4277 case elfcpp::R_X86_64_TLSGD: // Global-dynamic 4278 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) 4279 case elfcpp::R_X86_64_TLSDESC_CALL: 4280 case elfcpp::R_X86_64_TLSLD: // Local-dynamic 4281 case elfcpp::R_X86_64_DTPOFF32: 4282 case elfcpp::R_X86_64_DTPOFF64: 4283 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec 4284 case elfcpp::R_X86_64_TPOFF32: // Local-exec 4285 { 4286 // For the Initial-Exec model, we can treat undef symbols as final 4287 // when building an executable. 4288 const bool is_final = (gsym->final_value_is_known() || 4289 (r_type == elfcpp::R_X86_64_GOTTPOFF && 4290 gsym->is_undefined() && 4291 parameters->options().output_is_executable())); 4292 const tls::Tls_optimization optimized_type 4293 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type); 4294 switch (r_type) 4295 { 4296 case elfcpp::R_X86_64_TLSGD: // General-dynamic 4297 if (optimized_type == tls::TLSOPT_NONE) 4298 { 4299 // Create a pair of GOT entries for the module index and 4300 // dtv-relative offset. 4301 Output_data_got<64, false>* got 4302 = target->got_section(symtab, layout); 4303 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR, 4304 target->rela_dyn_section(layout), 4305 elfcpp::R_X86_64_DTPMOD64, 4306 elfcpp::R_X86_64_DTPOFF64); 4307 } 4308 else if (optimized_type == tls::TLSOPT_TO_IE) 4309 { 4310 // Create a GOT entry for the tp-relative offset. 4311 Output_data_got<64, false>* got 4312 = target->got_section(symtab, layout); 4313 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET, 4314 target->rela_dyn_section(layout), 4315 elfcpp::R_X86_64_TPOFF64); 4316 } 4317 else if (optimized_type != tls::TLSOPT_TO_LE) 4318 unsupported_reloc_global(object, r_type, gsym); 4319 break; 4320 4321 case elfcpp::R_X86_64_GOTPC32_TLSDESC: 4322 target->define_tls_base_symbol(symtab, layout); 4323 if (optimized_type == tls::TLSOPT_NONE) 4324 { 4325 // Create reserved PLT and GOT entries for the resolver. 4326 target->reserve_tlsdesc_entries(symtab, layout); 4327 4328 // Create a double GOT entry with an R_X86_64_TLSDESC 4329 // reloc. The R_X86_64_TLSDESC reloc is resolved 4330 // lazily, so the GOT entry needs to be in an area in 4331 // .got.plt, not .got. Call got_section to make sure 4332 // the section has been created. 4333 target->got_section(symtab, layout); 4334 Output_data_got<64, false>* got = target->got_tlsdesc_section(); 4335 Reloc_section* rt = target->rela_tlsdesc_section(layout); 4336 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt, 4337 elfcpp::R_X86_64_TLSDESC, 0); 4338 } 4339 else if (optimized_type == tls::TLSOPT_TO_IE) 4340 { 4341 // Create a GOT entry for the tp-relative offset. 4342 Output_data_got<64, false>* got 4343 = target->got_section(symtab, layout); 4344 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET, 4345 target->rela_dyn_section(layout), 4346 elfcpp::R_X86_64_TPOFF64); 4347 } 4348 else if (optimized_type != tls::TLSOPT_TO_LE) 4349 unsupported_reloc_global(object, r_type, gsym); 4350 break; 4351 4352 case elfcpp::R_X86_64_TLSDESC_CALL: 4353 break; 4354 4355 case elfcpp::R_X86_64_TLSLD: // Local-dynamic 4356 if (optimized_type == tls::TLSOPT_NONE) 4357 { 4358 // Create a GOT entry for the module index. 4359 target->got_mod_index_entry(symtab, layout, object); 4360 } 4361 else if (optimized_type != tls::TLSOPT_TO_LE) 4362 unsupported_reloc_global(object, r_type, gsym); 4363 break; 4364 4365 case elfcpp::R_X86_64_DTPOFF32: 4366 case elfcpp::R_X86_64_DTPOFF64: 4367 break; 4368 4369 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec 4370 layout->set_has_static_tls(); 4371 if (optimized_type == tls::TLSOPT_NONE) 4372 { 4373 // Create a GOT entry for the tp-relative offset. 4374 Output_data_got<64, false>* got 4375 = target->got_section(symtab, layout); 4376 got->add_global_with_rel(gsym, GOT_TYPE_TLS_OFFSET, 4377 target->rela_dyn_section(layout), 4378 elfcpp::R_X86_64_TPOFF64); 4379 } 4380 else if (optimized_type != tls::TLSOPT_TO_LE) 4381 unsupported_reloc_global(object, r_type, gsym); 4382 break; 4383 4384 case elfcpp::R_X86_64_TPOFF32: // Local-exec 4385 layout->set_has_static_tls(); 4386 if (parameters->options().shared()) 4387 unsupported_reloc_global(object, r_type, gsym); 4388 break; 4389 4390 default: 4391 gold_unreachable(); 4392 } 4393 } 4394 break; 4395 4396 case elfcpp::R_X86_64_SIZE32: 4397 case elfcpp::R_X86_64_SIZE64: 4398 default: 4399 gold_error(_("%s: unsupported reloc %u against global symbol %s"), 4400 object->name().c_str(), r_type, 4401 gsym->demangled_name().c_str()); 4402 break; 4403 } 4404 } 4405 4406 template<int size> 4407 void 4408 Target_x86_64<size>::gc_process_relocs(Symbol_table* symtab, 4409 Layout* layout, 4410 Sized_relobj_file<size, false>* object, 4411 unsigned int data_shndx, 4412 unsigned int sh_type, 4413 const unsigned char* prelocs, 4414 size_t reloc_count, 4415 Output_section* output_section, 4416 bool needs_special_offset_handling, 4417 size_t local_symbol_count, 4418 const unsigned char* plocal_symbols) 4419 { 4420 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> 4421 Classify_reloc; 4422 4423 if (sh_type == elfcpp::SHT_REL) 4424 { 4425 return; 4426 } 4427 4428 gold::gc_process_relocs<size, false, Target_x86_64<size>, Scan, 4429 Classify_reloc>( 4430 symtab, 4431 layout, 4432 this, 4433 object, 4434 data_shndx, 4435 prelocs, 4436 reloc_count, 4437 output_section, 4438 needs_special_offset_handling, 4439 local_symbol_count, 4440 plocal_symbols); 4441 4442 } 4443 // Scan relocations for a section. 4444 4445 template<int size> 4446 void 4447 Target_x86_64<size>::scan_relocs(Symbol_table* symtab, 4448 Layout* layout, 4449 Sized_relobj_file<size, false>* object, 4450 unsigned int data_shndx, 4451 unsigned int sh_type, 4452 const unsigned char* prelocs, 4453 size_t reloc_count, 4454 Output_section* output_section, 4455 bool needs_special_offset_handling, 4456 size_t local_symbol_count, 4457 const unsigned char* plocal_symbols) 4458 { 4459 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> 4460 Classify_reloc; 4461 4462 if (sh_type == elfcpp::SHT_REL) 4463 { 4464 gold_error(_("%s: unsupported REL reloc section"), 4465 object->name().c_str()); 4466 return; 4467 } 4468 4469 gold::scan_relocs<size, false, Target_x86_64<size>, Scan, Classify_reloc>( 4470 symtab, 4471 layout, 4472 this, 4473 object, 4474 data_shndx, 4475 prelocs, 4476 reloc_count, 4477 output_section, 4478 needs_special_offset_handling, 4479 local_symbol_count, 4480 plocal_symbols); 4481 } 4482 4483 // Finalize the sections. 4484 4485 template<int size> 4486 void 4487 Target_x86_64<size>::do_finalize_sections( 4488 Layout* layout, 4489 const Input_objects*, 4490 Symbol_table* symtab) 4491 { 4492 const Reloc_section* rel_plt = (this->plt_ == NULL 4493 ? NULL 4494 : this->plt_->rela_plt()); 4495 layout->add_target_dynamic_tags(false, this->got_plt_, rel_plt, 4496 this->rela_dyn_, true, false); 4497 4498 // Fill in some more dynamic tags. 4499 Output_data_dynamic* const odyn = layout->dynamic_data(); 4500 if (odyn != NULL) 4501 { 4502 if (this->plt_ != NULL 4503 && this->plt_->output_section() != NULL 4504 && this->plt_->has_tlsdesc_entry()) 4505 { 4506 unsigned int plt_offset = this->plt_->get_tlsdesc_plt_offset(); 4507 unsigned int got_offset = this->plt_->get_tlsdesc_got_offset(); 4508 this->got_->finalize_data_size(); 4509 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_PLT, 4510 this->plt_, plt_offset); 4511 odyn->add_section_plus_offset(elfcpp::DT_TLSDESC_GOT, 4512 this->got_, got_offset); 4513 } 4514 } 4515 4516 // Emit any relocs we saved in an attempt to avoid generating COPY 4517 // relocs. 4518 if (this->copy_relocs_.any_saved_relocs()) 4519 this->copy_relocs_.emit(this->rela_dyn_section(layout)); 4520 4521 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of 4522 // the .got.plt section. 4523 Symbol* sym = this->global_offset_table_; 4524 if (sym != NULL) 4525 { 4526 uint64_t data_size = this->got_plt_->current_data_size(); 4527 symtab->get_sized_symbol<size>(sym)->set_symsize(data_size); 4528 } 4529 4530 if (parameters->doing_static_link() 4531 && (this->plt_ == NULL || !this->plt_->has_irelative_section())) 4532 { 4533 // If linking statically, make sure that the __rela_iplt symbols 4534 // were defined if necessary, even if we didn't create a PLT. 4535 static const Define_symbol_in_segment syms[] = 4536 { 4537 { 4538 "__rela_iplt_start", // name 4539 elfcpp::PT_LOAD, // segment_type 4540 elfcpp::PF_W, // segment_flags_set 4541 elfcpp::PF(0), // segment_flags_clear 4542 0, // value 4543 0, // size 4544 elfcpp::STT_NOTYPE, // type 4545 elfcpp::STB_GLOBAL, // binding 4546 elfcpp::STV_HIDDEN, // visibility 4547 0, // nonvis 4548 Symbol::SEGMENT_START, // offset_from_base 4549 true // only_if_ref 4550 }, 4551 { 4552 "__rela_iplt_end", // name 4553 elfcpp::PT_LOAD, // segment_type 4554 elfcpp::PF_W, // segment_flags_set 4555 elfcpp::PF(0), // segment_flags_clear 4556 0, // value 4557 0, // size 4558 elfcpp::STT_NOTYPE, // type 4559 elfcpp::STB_GLOBAL, // binding 4560 elfcpp::STV_HIDDEN, // visibility 4561 0, // nonvis 4562 Symbol::SEGMENT_START, // offset_from_base 4563 true // only_if_ref 4564 } 4565 }; 4566 4567 symtab->define_symbols(layout, 2, syms, 4568 layout->script_options()->saw_sections_clause()); 4569 } 4570 } 4571 4572 // For x32, we need to handle PC-relative relocations using full 64-bit 4573 // arithmetic, so that we can detect relocation overflows properly. 4574 // This class overrides the pcrela32_check methods from the defaults in 4575 // Relocate_functions in reloc.h. 4576 4577 template<int size> 4578 class X86_64_relocate_functions : public Relocate_functions<size, false> 4579 { 4580 public: 4581 typedef Relocate_functions<size, false> Base; 4582 4583 // Do a simple PC relative relocation with the addend in the 4584 // relocation. 4585 static inline typename Base::Reloc_status 4586 pcrela32_check(unsigned char* view, 4587 typename elfcpp::Elf_types<64>::Elf_Addr value, 4588 typename elfcpp::Elf_types<64>::Elf_Swxword addend, 4589 typename elfcpp::Elf_types<64>::Elf_Addr address) 4590 { 4591 typedef typename elfcpp::Swap<32, false>::Valtype Valtype; 4592 Valtype* wv = reinterpret_cast<Valtype*>(view); 4593 value = value + addend - address; 4594 elfcpp::Swap<32, false>::writeval(wv, value); 4595 return (Bits<32>::has_overflow(value) 4596 ? Base::RELOC_OVERFLOW : Base::RELOC_OK); 4597 } 4598 4599 // Do a simple PC relative relocation with a Symbol_value with the 4600 // addend in the relocation. 4601 static inline typename Base::Reloc_status 4602 pcrela32_check(unsigned char* view, 4603 const Sized_relobj_file<size, false>* object, 4604 const Symbol_value<size>* psymval, 4605 typename elfcpp::Elf_types<64>::Elf_Swxword addend, 4606 typename elfcpp::Elf_types<64>::Elf_Addr address) 4607 { 4608 typedef typename elfcpp::Swap<32, false>::Valtype Valtype; 4609 Valtype* wv = reinterpret_cast<Valtype*>(view); 4610 typename elfcpp::Elf_types<64>::Elf_Addr value; 4611 if (addend >= 0) 4612 value = psymval->value(object, addend); 4613 else 4614 { 4615 // For negative addends, get the symbol value without 4616 // the addend, then add the addend using 64-bit arithmetic. 4617 value = psymval->value(object, 0); 4618 value += addend; 4619 } 4620 value -= address; 4621 elfcpp::Swap<32, false>::writeval(wv, value); 4622 return (Bits<32>::has_overflow(value) 4623 ? Base::RELOC_OVERFLOW : Base::RELOC_OK); 4624 } 4625 }; 4626 4627 // Perform a relocation. 4628 4629 template<int size> 4630 inline bool 4631 Target_x86_64<size>::Relocate::relocate( 4632 const Relocate_info<size, false>* relinfo, 4633 unsigned int, 4634 Target_x86_64<size>* target, 4635 Output_section*, 4636 size_t relnum, 4637 const unsigned char* preloc, 4638 const Sized_symbol<size>* gsym, 4639 const Symbol_value<size>* psymval, 4640 unsigned char* view, 4641 typename elfcpp::Elf_types<size>::Elf_Addr address, 4642 section_size_type view_size) 4643 { 4644 typedef X86_64_relocate_functions<size> Reloc_funcs; 4645 const elfcpp::Rela<size, false> rela(preloc); 4646 unsigned int r_type = elfcpp::elf_r_type<size>(rela.get_r_info()); 4647 4648 if (this->skip_call_tls_get_addr_) 4649 { 4650 if ((r_type != elfcpp::R_X86_64_PLT32 4651 && r_type != elfcpp::R_X86_64_GOTPCREL 4652 && r_type != elfcpp::R_X86_64_GOTPCRELX 4653 && r_type != elfcpp::R_X86_64_PLT32_BND 4654 && r_type != elfcpp::R_X86_64_PC32_BND 4655 && r_type != elfcpp::R_X86_64_PC32) 4656 || gsym == NULL 4657 || strcmp(gsym->name(), "__tls_get_addr") != 0) 4658 { 4659 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 4660 _("missing expected TLS relocation")); 4661 this->skip_call_tls_get_addr_ = false; 4662 } 4663 else 4664 { 4665 this->skip_call_tls_get_addr_ = false; 4666 return false; 4667 } 4668 } 4669 4670 if (view == NULL) 4671 return true; 4672 4673 const Sized_relobj_file<size, false>* object = relinfo->object; 4674 4675 // Pick the value to use for symbols defined in the PLT. 4676 Symbol_value<size> symval; 4677 if (gsym != NULL 4678 && gsym->use_plt_offset(Scan::get_reference_flags(r_type))) 4679 { 4680 symval.set_output_value(target->plt_address_for_global(gsym)); 4681 psymval = &symval; 4682 } 4683 else if (gsym == NULL && psymval->is_ifunc_symbol()) 4684 { 4685 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); 4686 if (object->local_has_plt_offset(r_sym)) 4687 { 4688 symval.set_output_value(target->plt_address_for_local(object, r_sym)); 4689 psymval = &symval; 4690 } 4691 } 4692 4693 const elfcpp::Elf_Xword addend = rela.get_r_addend(); 4694 4695 // Get the GOT offset if needed. 4696 // The GOT pointer points to the end of the GOT section. 4697 // We need to subtract the size of the GOT section to get 4698 // the actual offset to use in the relocation. 4699 bool have_got_offset = false; 4700 // Since the actual offset is always negative, we use signed int to 4701 // support 64-bit GOT relocations. 4702 int got_offset = 0; 4703 switch (r_type) 4704 { 4705 case elfcpp::R_X86_64_GOT32: 4706 case elfcpp::R_X86_64_GOT64: 4707 case elfcpp::R_X86_64_GOTPLT64: 4708 case elfcpp::R_X86_64_GOTPCREL64: 4709 if (gsym != NULL) 4710 { 4711 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); 4712 got_offset = gsym->got_offset(GOT_TYPE_STANDARD) - target->got_size(); 4713 } 4714 else 4715 { 4716 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); 4717 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD)); 4718 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD) 4719 - target->got_size()); 4720 } 4721 have_got_offset = true; 4722 break; 4723 4724 default: 4725 break; 4726 } 4727 4728 typename Reloc_funcs::Reloc_status rstatus = Reloc_funcs::RELOC_OK; 4729 4730 switch (r_type) 4731 { 4732 case elfcpp::R_X86_64_NONE: 4733 case elfcpp::R_X86_64_GNU_VTINHERIT: 4734 case elfcpp::R_X86_64_GNU_VTENTRY: 4735 break; 4736 4737 case elfcpp::R_X86_64_64: 4738 Reloc_funcs::rela64(view, object, psymval, addend); 4739 break; 4740 4741 case elfcpp::R_X86_64_PC64: 4742 Reloc_funcs::pcrela64(view, object, psymval, addend, 4743 address); 4744 break; 4745 4746 case elfcpp::R_X86_64_32: 4747 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend, 4748 Reloc_funcs::CHECK_UNSIGNED); 4749 break; 4750 4751 case elfcpp::R_X86_64_32S: 4752 rstatus = Reloc_funcs::rela32_check(view, object, psymval, addend, 4753 Reloc_funcs::CHECK_SIGNED); 4754 break; 4755 4756 case elfcpp::R_X86_64_PC32: 4757 case elfcpp::R_X86_64_PC32_BND: 4758 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend, 4759 address); 4760 break; 4761 4762 case elfcpp::R_X86_64_16: 4763 Reloc_funcs::rela16(view, object, psymval, addend); 4764 break; 4765 4766 case elfcpp::R_X86_64_PC16: 4767 Reloc_funcs::pcrela16(view, object, psymval, addend, address); 4768 break; 4769 4770 case elfcpp::R_X86_64_8: 4771 Reloc_funcs::rela8(view, object, psymval, addend); 4772 break; 4773 4774 case elfcpp::R_X86_64_PC8: 4775 Reloc_funcs::pcrela8(view, object, psymval, addend, address); 4776 break; 4777 4778 case elfcpp::R_X86_64_PLT32: 4779 case elfcpp::R_X86_64_PLT32_BND: 4780 gold_assert(gsym == NULL 4781 || gsym->has_plt_offset() 4782 || gsym->final_value_is_known() 4783 || (gsym->is_defined() 4784 && !gsym->is_from_dynobj() 4785 && !gsym->is_preemptible())); 4786 // Note: while this code looks the same as for R_X86_64_PC32, it 4787 // behaves differently because psymval was set to point to 4788 // the PLT entry, rather than the symbol, in Scan::global(). 4789 rstatus = Reloc_funcs::pcrela32_check(view, object, psymval, addend, 4790 address); 4791 break; 4792 4793 case elfcpp::R_X86_64_PLTOFF64: 4794 { 4795 gold_assert(gsym); 4796 gold_assert(gsym->has_plt_offset() 4797 || gsym->final_value_is_known()); 4798 typename elfcpp::Elf_types<size>::Elf_Addr got_address; 4799 // This is the address of GLOBAL_OFFSET_TABLE. 4800 got_address = target->got_plt_section()->address(); 4801 Reloc_funcs::rela64(view, object, psymval, addend - got_address); 4802 } 4803 break; 4804 4805 case elfcpp::R_X86_64_GOT32: 4806 gold_assert(have_got_offset); 4807 Reloc_funcs::rela32(view, got_offset, addend); 4808 break; 4809 4810 case elfcpp::R_X86_64_GOTPC32: 4811 { 4812 gold_assert(gsym); 4813 typename elfcpp::Elf_types<size>::Elf_Addr value; 4814 value = target->got_plt_section()->address(); 4815 Reloc_funcs::pcrela32_check(view, value, addend, address); 4816 } 4817 break; 4818 4819 case elfcpp::R_X86_64_GOT64: 4820 case elfcpp::R_X86_64_GOTPLT64: 4821 // R_X86_64_GOTPLT64 is obsolete and treated the same as 4822 // GOT64. 4823 gold_assert(have_got_offset); 4824 Reloc_funcs::rela64(view, got_offset, addend); 4825 break; 4826 4827 case elfcpp::R_X86_64_GOTPC64: 4828 { 4829 gold_assert(gsym); 4830 typename elfcpp::Elf_types<size>::Elf_Addr value; 4831 value = target->got_plt_section()->address(); 4832 Reloc_funcs::pcrela64(view, value, addend, address); 4833 } 4834 break; 4835 4836 case elfcpp::R_X86_64_GOTOFF64: 4837 { 4838 typename elfcpp::Elf_types<size>::Elf_Addr value; 4839 value = (psymval->value(object, 0) 4840 - target->got_plt_section()->address()); 4841 Reloc_funcs::rela64(view, value, addend); 4842 } 4843 break; 4844 4845 case elfcpp::R_X86_64_GOTPCREL: 4846 case elfcpp::R_X86_64_GOTPCRELX: 4847 case elfcpp::R_X86_64_REX_GOTPCRELX: 4848 { 4849 // Convert 4850 // mov foo@GOTPCREL(%rip), %reg 4851 // to lea foo(%rip), %reg. 4852 // if possible. 4853 if (!parameters->incremental() 4854 && ((gsym == NULL 4855 && rela.get_r_offset() >= 2 4856 && view[-2] == 0x8b 4857 && !psymval->is_ifunc_symbol()) 4858 || (gsym != NULL 4859 && rela.get_r_offset() >= 2 4860 && Target_x86_64<size>::can_convert_mov_to_lea(gsym, r_type, 4861 0, &view)))) 4862 { 4863 view[-2] = 0x8d; 4864 Reloc_funcs::pcrela32(view, object, psymval, addend, address); 4865 } 4866 // Convert 4867 // callq *foo@GOTPCRELX(%rip) to 4868 // addr32 callq foo 4869 // and jmpq *foo@GOTPCRELX(%rip) to 4870 // jmpq foo 4871 // nop 4872 else if (!parameters->incremental() 4873 && gsym != NULL 4874 && rela.get_r_offset() >= 2 4875 && Target_x86_64<size>::can_convert_callq_to_direct(gsym, 4876 r_type, 4877 0, &view)) 4878 { 4879 if (view[-1] == 0x15) 4880 { 4881 // Convert callq *foo@GOTPCRELX(%rip) to addr32 callq. 4882 // Opcode of addr32 is 0x67 and opcode of direct callq is 0xe8. 4883 view[-2] = 0x67; 4884 view[-1] = 0xe8; 4885 // Convert GOTPCRELX to 32-bit pc relative reloc. 4886 Reloc_funcs::pcrela32(view, object, psymval, addend, address); 4887 } 4888 else 4889 { 4890 // Convert jmpq *foo@GOTPCRELX(%rip) to 4891 // jmpq foo 4892 // nop 4893 // The opcode of direct jmpq is 0xe9. 4894 view[-2] = 0xe9; 4895 // The opcode of nop is 0x90. 4896 view[3] = 0x90; 4897 // Convert GOTPCRELX to 32-bit pc relative reloc. jmpq is rip 4898 // relative and since the instruction following the jmpq is now 4899 // the nop, offset the address by 1 byte. The start of the 4900 // relocation also moves ahead by 1 byte. 4901 Reloc_funcs::pcrela32(&view[-1], object, psymval, addend, 4902 address - 1); 4903 } 4904 } 4905 else 4906 { 4907 if (gsym != NULL) 4908 { 4909 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD)); 4910 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD) 4911 - target->got_size()); 4912 } 4913 else 4914 { 4915 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); 4916 gold_assert(object->local_has_got_offset(r_sym, 4917 GOT_TYPE_STANDARD)); 4918 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD) 4919 - target->got_size()); 4920 } 4921 typename elfcpp::Elf_types<size>::Elf_Addr value; 4922 value = target->got_plt_section()->address() + got_offset; 4923 Reloc_funcs::pcrela32_check(view, value, addend, address); 4924 } 4925 } 4926 break; 4927 4928 case elfcpp::R_X86_64_GOTPCREL64: 4929 { 4930 gold_assert(have_got_offset); 4931 typename elfcpp::Elf_types<size>::Elf_Addr value; 4932 value = target->got_plt_section()->address() + got_offset; 4933 Reloc_funcs::pcrela64(view, value, addend, address); 4934 } 4935 break; 4936 4937 case elfcpp::R_X86_64_COPY: 4938 case elfcpp::R_X86_64_GLOB_DAT: 4939 case elfcpp::R_X86_64_JUMP_SLOT: 4940 case elfcpp::R_X86_64_RELATIVE: 4941 case elfcpp::R_X86_64_IRELATIVE: 4942 // These are outstanding tls relocs, which are unexpected when linking 4943 case elfcpp::R_X86_64_TPOFF64: 4944 case elfcpp::R_X86_64_DTPMOD64: 4945 case elfcpp::R_X86_64_TLSDESC: 4946 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 4947 _("unexpected reloc %u in object file"), 4948 r_type); 4949 break; 4950 4951 // These are initial tls relocs, which are expected when linking 4952 case elfcpp::R_X86_64_TLSGD: // Global-dynamic 4953 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) 4954 case elfcpp::R_X86_64_TLSDESC_CALL: 4955 case elfcpp::R_X86_64_TLSLD: // Local-dynamic 4956 case elfcpp::R_X86_64_DTPOFF32: 4957 case elfcpp::R_X86_64_DTPOFF64: 4958 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec 4959 case elfcpp::R_X86_64_TPOFF32: // Local-exec 4960 this->relocate_tls(relinfo, target, relnum, rela, r_type, gsym, psymval, 4961 view, address, view_size); 4962 break; 4963 4964 case elfcpp::R_X86_64_SIZE32: 4965 case elfcpp::R_X86_64_SIZE64: 4966 default: 4967 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 4968 _("unsupported reloc %u"), 4969 r_type); 4970 break; 4971 } 4972 4973 if (rstatus == Reloc_funcs::RELOC_OVERFLOW) 4974 { 4975 if (gsym == NULL) 4976 { 4977 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); 4978 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 4979 _("relocation overflow: " 4980 "reference to local symbol %u in %s"), 4981 r_sym, object->name().c_str()); 4982 } 4983 else if (gsym->is_defined() && gsym->source() == Symbol::FROM_OBJECT) 4984 { 4985 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 4986 _("relocation overflow: " 4987 "reference to '%s' defined in %s"), 4988 gsym->name(), 4989 gsym->object()->name().c_str()); 4990 } 4991 else 4992 { 4993 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 4994 _("relocation overflow: reference to '%s'"), 4995 gsym->name()); 4996 } 4997 } 4998 4999 return true; 5000 } 5001 5002 // Perform a TLS relocation. 5003 5004 template<int size> 5005 inline void 5006 Target_x86_64<size>::Relocate::relocate_tls( 5007 const Relocate_info<size, false>* relinfo, 5008 Target_x86_64<size>* target, 5009 size_t relnum, 5010 const elfcpp::Rela<size, false>& rela, 5011 unsigned int r_type, 5012 const Sized_symbol<size>* gsym, 5013 const Symbol_value<size>* psymval, 5014 unsigned char* view, 5015 typename elfcpp::Elf_types<size>::Elf_Addr address, 5016 section_size_type view_size) 5017 { 5018 Output_segment* tls_segment = relinfo->layout->tls_segment(); 5019 5020 const Sized_relobj_file<size, false>* object = relinfo->object; 5021 const elfcpp::Elf_Xword addend = rela.get_r_addend(); 5022 elfcpp::Shdr<size, false> data_shdr(relinfo->data_shdr); 5023 bool is_executable = (data_shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0; 5024 5025 typename elfcpp::Elf_types<size>::Elf_Addr value = psymval->value(relinfo->object, 0); 5026 5027 const bool is_final = (gsym == NULL 5028 ? !parameters->options().shared() 5029 : gsym->final_value_is_known()); 5030 tls::Tls_optimization optimized_type 5031 = Target_x86_64<size>::optimize_tls_reloc(is_final, r_type); 5032 switch (r_type) 5033 { 5034 case elfcpp::R_X86_64_TLSGD: // Global-dynamic 5035 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) 5036 { 5037 // If this code sequence is used in a non-executable section, 5038 // we will not optimize the R_X86_64_DTPOFF32/64 relocation, 5039 // on the assumption that it's being used by itself in a debug 5040 // section. Therefore, in the unlikely event that the code 5041 // sequence appears in a non-executable section, we simply 5042 // leave it unoptimized. 5043 optimized_type = tls::TLSOPT_NONE; 5044 } 5045 if (optimized_type == tls::TLSOPT_TO_LE) 5046 { 5047 if (tls_segment == NULL) 5048 { 5049 gold_assert(parameters->errors()->error_count() > 0 5050 || issue_undefined_symbol_error(gsym)); 5051 return; 5052 } 5053 this->tls_gd_to_le(relinfo, relnum, tls_segment, 5054 rela, r_type, value, view, 5055 view_size); 5056 break; 5057 } 5058 else 5059 { 5060 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE 5061 ? GOT_TYPE_TLS_OFFSET 5062 : GOT_TYPE_TLS_PAIR); 5063 unsigned int got_offset; 5064 if (gsym != NULL) 5065 { 5066 gold_assert(gsym->has_got_offset(got_type)); 5067 got_offset = gsym->got_offset(got_type) - target->got_size(); 5068 } 5069 else 5070 { 5071 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); 5072 gold_assert(object->local_has_got_offset(r_sym, got_type)); 5073 got_offset = (object->local_got_offset(r_sym, got_type) 5074 - target->got_size()); 5075 } 5076 if (optimized_type == tls::TLSOPT_TO_IE) 5077 { 5078 value = target->got_plt_section()->address() + got_offset; 5079 this->tls_gd_to_ie(relinfo, relnum, rela, r_type, 5080 value, view, address, view_size); 5081 break; 5082 } 5083 else if (optimized_type == tls::TLSOPT_NONE) 5084 { 5085 // Relocate the field with the offset of the pair of GOT 5086 // entries. 5087 value = target->got_plt_section()->address() + got_offset; 5088 Relocate_functions<size, false>::pcrela32(view, value, addend, 5089 address); 5090 break; 5091 } 5092 } 5093 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 5094 _("unsupported reloc %u"), r_type); 5095 break; 5096 5097 case elfcpp::R_X86_64_GOTPC32_TLSDESC: // Global-dynamic (from ~oliva url) 5098 case elfcpp::R_X86_64_TLSDESC_CALL: 5099 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) 5100 { 5101 // See above comment for R_X86_64_TLSGD. 5102 optimized_type = tls::TLSOPT_NONE; 5103 } 5104 if (optimized_type == tls::TLSOPT_TO_LE) 5105 { 5106 if (tls_segment == NULL) 5107 { 5108 gold_assert(parameters->errors()->error_count() > 0 5109 || issue_undefined_symbol_error(gsym)); 5110 return; 5111 } 5112 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment, 5113 rela, r_type, value, view, 5114 view_size); 5115 break; 5116 } 5117 else 5118 { 5119 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE 5120 ? GOT_TYPE_TLS_OFFSET 5121 : GOT_TYPE_TLS_DESC); 5122 unsigned int got_offset = 0; 5123 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC 5124 && optimized_type == tls::TLSOPT_NONE) 5125 { 5126 // We created GOT entries in the .got.tlsdesc portion of 5127 // the .got.plt section, but the offset stored in the 5128 // symbol is the offset within .got.tlsdesc. 5129 got_offset = (target->got_size() 5130 + target->got_plt_section()->data_size()); 5131 } 5132 if (gsym != NULL) 5133 { 5134 gold_assert(gsym->has_got_offset(got_type)); 5135 got_offset += gsym->got_offset(got_type) - target->got_size(); 5136 } 5137 else 5138 { 5139 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); 5140 gold_assert(object->local_has_got_offset(r_sym, got_type)); 5141 got_offset += (object->local_got_offset(r_sym, got_type) 5142 - target->got_size()); 5143 } 5144 if (optimized_type == tls::TLSOPT_TO_IE) 5145 { 5146 value = target->got_plt_section()->address() + got_offset; 5147 this->tls_desc_gd_to_ie(relinfo, relnum, 5148 rela, r_type, value, view, address, 5149 view_size); 5150 break; 5151 } 5152 else if (optimized_type == tls::TLSOPT_NONE) 5153 { 5154 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) 5155 { 5156 // Relocate the field with the offset of the pair of GOT 5157 // entries. 5158 value = target->got_plt_section()->address() + got_offset; 5159 Relocate_functions<size, false>::pcrela32(view, value, addend, 5160 address); 5161 } 5162 break; 5163 } 5164 } 5165 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 5166 _("unsupported reloc %u"), r_type); 5167 break; 5168 5169 case elfcpp::R_X86_64_TLSLD: // Local-dynamic 5170 if (!is_executable && optimized_type == tls::TLSOPT_TO_LE) 5171 { 5172 // See above comment for R_X86_64_TLSGD. 5173 optimized_type = tls::TLSOPT_NONE; 5174 } 5175 if (optimized_type == tls::TLSOPT_TO_LE) 5176 { 5177 if (tls_segment == NULL) 5178 { 5179 gold_assert(parameters->errors()->error_count() > 0 5180 || issue_undefined_symbol_error(gsym)); 5181 return; 5182 } 5183 this->tls_ld_to_le(relinfo, relnum, tls_segment, rela, r_type, 5184 value, view, view_size); 5185 break; 5186 } 5187 else if (optimized_type == tls::TLSOPT_NONE) 5188 { 5189 // Relocate the field with the offset of the GOT entry for 5190 // the module index. 5191 unsigned int got_offset; 5192 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL) 5193 - target->got_size()); 5194 value = target->got_plt_section()->address() + got_offset; 5195 Relocate_functions<size, false>::pcrela32(view, value, addend, 5196 address); 5197 break; 5198 } 5199 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 5200 _("unsupported reloc %u"), r_type); 5201 break; 5202 5203 case elfcpp::R_X86_64_DTPOFF32: 5204 // This relocation type is used in debugging information. 5205 // In that case we need to not optimize the value. If the 5206 // section is not executable, then we assume we should not 5207 // optimize this reloc. See comments above for R_X86_64_TLSGD, 5208 // R_X86_64_GOTPC32_TLSDESC, R_X86_64_TLSDESC_CALL, and 5209 // R_X86_64_TLSLD. 5210 if (optimized_type == tls::TLSOPT_TO_LE && is_executable) 5211 { 5212 if (tls_segment == NULL) 5213 { 5214 gold_assert(parameters->errors()->error_count() > 0 5215 || issue_undefined_symbol_error(gsym)); 5216 return; 5217 } 5218 value -= tls_segment->memsz(); 5219 } 5220 Relocate_functions<size, false>::rela32(view, value, addend); 5221 break; 5222 5223 case elfcpp::R_X86_64_DTPOFF64: 5224 // See R_X86_64_DTPOFF32, just above, for why we check for is_executable. 5225 if (optimized_type == tls::TLSOPT_TO_LE && is_executable) 5226 { 5227 if (tls_segment == NULL) 5228 { 5229 gold_assert(parameters->errors()->error_count() > 0 5230 || issue_undefined_symbol_error(gsym)); 5231 return; 5232 } 5233 value -= tls_segment->memsz(); 5234 } 5235 Relocate_functions<size, false>::rela64(view, value, addend); 5236 break; 5237 5238 case elfcpp::R_X86_64_GOTTPOFF: // Initial-exec 5239 if (gsym != NULL 5240 && gsym->is_undefined() 5241 && parameters->options().output_is_executable()) 5242 { 5243 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum, 5244 NULL, rela, 5245 r_type, value, view, 5246 view_size); 5247 break; 5248 } 5249 else if (optimized_type == tls::TLSOPT_TO_LE) 5250 { 5251 if (tls_segment == NULL) 5252 { 5253 gold_assert(parameters->errors()->error_count() > 0 5254 || issue_undefined_symbol_error(gsym)); 5255 return; 5256 } 5257 Target_x86_64<size>::Relocate::tls_ie_to_le(relinfo, relnum, 5258 tls_segment, rela, 5259 r_type, value, view, 5260 view_size); 5261 break; 5262 } 5263 else if (optimized_type == tls::TLSOPT_NONE) 5264 { 5265 // Relocate the field with the offset of the GOT entry for 5266 // the tp-relative offset of the symbol. 5267 unsigned int got_offset; 5268 if (gsym != NULL) 5269 { 5270 gold_assert(gsym->has_got_offset(GOT_TYPE_TLS_OFFSET)); 5271 got_offset = (gsym->got_offset(GOT_TYPE_TLS_OFFSET) 5272 - target->got_size()); 5273 } 5274 else 5275 { 5276 unsigned int r_sym = elfcpp::elf_r_sym<size>(rela.get_r_info()); 5277 gold_assert(object->local_has_got_offset(r_sym, 5278 GOT_TYPE_TLS_OFFSET)); 5279 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_TLS_OFFSET) 5280 - target->got_size()); 5281 } 5282 value = target->got_plt_section()->address() + got_offset; 5283 Relocate_functions<size, false>::pcrela32(view, value, addend, 5284 address); 5285 break; 5286 } 5287 gold_error_at_location(relinfo, relnum, rela.get_r_offset(), 5288 _("unsupported reloc type %u"), 5289 r_type); 5290 break; 5291 5292 case elfcpp::R_X86_64_TPOFF32: // Local-exec 5293 if (tls_segment == NULL) 5294 { 5295 gold_assert(parameters->errors()->error_count() > 0 5296 || issue_undefined_symbol_error(gsym)); 5297 return; 5298 } 5299 value -= tls_segment->memsz(); 5300 Relocate_functions<size, false>::rela32(view, value, addend); 5301 break; 5302 } 5303 } 5304 5305 // Do a relocation in which we convert a TLS General-Dynamic to an 5306 // Initial-Exec. 5307 5308 template<int size> 5309 inline void 5310 Target_x86_64<size>::Relocate::tls_gd_to_ie( 5311 const Relocate_info<size, false>* relinfo, 5312 size_t relnum, 5313 const elfcpp::Rela<size, false>& rela, 5314 unsigned int, 5315 typename elfcpp::Elf_types<size>::Elf_Addr value, 5316 unsigned char* view, 5317 typename elfcpp::Elf_types<size>::Elf_Addr address, 5318 section_size_type view_size) 5319 { 5320 // For SIZE == 64: 5321 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; 5322 // .word 0x6666; rex64; call __tls_get_addr@PLT 5323 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax 5324 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; 5325 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip) 5326 // ==> movq %fs:0,%rax; addq x@gottpoff(%rip),%rax 5327 // For SIZE == 32: 5328 // leaq foo@tlsgd(%rip),%rdi; 5329 // .word 0x6666; rex64; call __tls_get_addr@PLT 5330 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax 5331 // leaq foo@tlsgd(%rip),%rdi; 5332 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip) 5333 // ==> movl %fs:0,%eax; addq x@gottpoff(%rip),%rax 5334 5335 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12); 5336 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5337 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0 5338 || memcmp(view + 4, "\x66\x48\xff", 3) == 0)); 5339 5340 if (size == 64) 5341 { 5342 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 5343 -4); 5344 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5345 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0)); 5346 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 5347 16); 5348 } 5349 else 5350 { 5351 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 5352 -3); 5353 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5354 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0)); 5355 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0\0", 5356 15); 5357 } 5358 5359 const elfcpp::Elf_Xword addend = rela.get_r_addend(); 5360 Relocate_functions<size, false>::pcrela32(view + 8, value, addend - 8, 5361 address); 5362 5363 // The next reloc should be a PLT32 reloc against __tls_get_addr. 5364 // We can skip it. 5365 this->skip_call_tls_get_addr_ = true; 5366 } 5367 5368 // Do a relocation in which we convert a TLS General-Dynamic to a 5369 // Local-Exec. 5370 5371 template<int size> 5372 inline void 5373 Target_x86_64<size>::Relocate::tls_gd_to_le( 5374 const Relocate_info<size, false>* relinfo, 5375 size_t relnum, 5376 Output_segment* tls_segment, 5377 const elfcpp::Rela<size, false>& rela, 5378 unsigned int, 5379 typename elfcpp::Elf_types<size>::Elf_Addr value, 5380 unsigned char* view, 5381 section_size_type view_size) 5382 { 5383 // For SIZE == 64: 5384 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; 5385 // .word 0x6666; rex64; call __tls_get_addr@PLT 5386 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax 5387 // .byte 0x66; leaq foo@tlsgd(%rip),%rdi; 5388 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip) 5389 // ==> movq %fs:0,%rax; leaq x@tpoff(%rax),%rax 5390 // For SIZE == 32: 5391 // leaq foo@tlsgd(%rip),%rdi; 5392 // .word 0x6666; rex64; call __tls_get_addr@PLT 5393 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax 5394 // leaq foo@tlsgd(%rip),%rdi; 5395 // .word 0x66; rex64; call *__tls_get_addr@GOTPCREL(%rip) 5396 // ==> movl %fs:0,%eax; leaq x@tpoff(%rax),%rax 5397 5398 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 12); 5399 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5400 (memcmp(view + 4, "\x66\x66\x48\xe8", 4) == 0 5401 || memcmp(view + 4, "\x66\x48\xff", 3) == 0)); 5402 5403 if (size == 64) 5404 { 5405 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 5406 -4); 5407 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5408 (memcmp(view - 4, "\x66\x48\x8d\x3d", 4) == 0)); 5409 memcpy(view - 4, "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 5410 16); 5411 } 5412 else 5413 { 5414 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 5415 -3); 5416 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5417 (memcmp(view - 3, "\x48\x8d\x3d", 3) == 0)); 5418 5419 memcpy(view - 3, "\x64\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0\0", 5420 15); 5421 } 5422 5423 value -= tls_segment->memsz(); 5424 Relocate_functions<size, false>::rela32(view + 8, value, 0); 5425 5426 // The next reloc should be a PLT32 reloc against __tls_get_addr. 5427 // We can skip it. 5428 this->skip_call_tls_get_addr_ = true; 5429 } 5430 5431 // Do a TLSDESC-style General-Dynamic to Initial-Exec transition. 5432 5433 template<int size> 5434 inline void 5435 Target_x86_64<size>::Relocate::tls_desc_gd_to_ie( 5436 const Relocate_info<size, false>* relinfo, 5437 size_t relnum, 5438 const elfcpp::Rela<size, false>& rela, 5439 unsigned int r_type, 5440 typename elfcpp::Elf_types<size>::Elf_Addr value, 5441 unsigned char* view, 5442 typename elfcpp::Elf_types<size>::Elf_Addr address, 5443 section_size_type view_size) 5444 { 5445 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) 5446 { 5447 // leaq foo@tlsdesc(%rip), %rax 5448 // ==> movq foo@gottpoff(%rip), %rax 5449 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); 5450 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); 5451 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5452 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05); 5453 view[-2] = 0x8b; 5454 const elfcpp::Elf_Xword addend = rela.get_r_addend(); 5455 Relocate_functions<size, false>::pcrela32(view, value, addend, address); 5456 } 5457 else 5458 { 5459 // call *foo@tlscall(%rax) 5460 // ==> nop; nop 5461 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL); 5462 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2); 5463 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5464 view[0] == 0xff && view[1] == 0x10); 5465 view[0] = 0x66; 5466 view[1] = 0x90; 5467 } 5468 } 5469 5470 // Do a TLSDESC-style General-Dynamic to Local-Exec transition. 5471 5472 template<int size> 5473 inline void 5474 Target_x86_64<size>::Relocate::tls_desc_gd_to_le( 5475 const Relocate_info<size, false>* relinfo, 5476 size_t relnum, 5477 Output_segment* tls_segment, 5478 const elfcpp::Rela<size, false>& rela, 5479 unsigned int r_type, 5480 typename elfcpp::Elf_types<size>::Elf_Addr value, 5481 unsigned char* view, 5482 section_size_type view_size) 5483 { 5484 if (r_type == elfcpp::R_X86_64_GOTPC32_TLSDESC) 5485 { 5486 // leaq foo@tlsdesc(%rip), %rax 5487 // ==> movq foo@tpoff, %rax 5488 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); 5489 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); 5490 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5491 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x05); 5492 view[-2] = 0xc7; 5493 view[-1] = 0xc0; 5494 value -= tls_segment->memsz(); 5495 Relocate_functions<size, false>::rela32(view, value, 0); 5496 } 5497 else 5498 { 5499 // call *foo@tlscall(%rax) 5500 // ==> nop; nop 5501 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC_CALL); 5502 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 2); 5503 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5504 view[0] == 0xff && view[1] == 0x10); 5505 view[0] = 0x66; 5506 view[1] = 0x90; 5507 } 5508 } 5509 5510 template<int size> 5511 inline void 5512 Target_x86_64<size>::Relocate::tls_ld_to_le( 5513 const Relocate_info<size, false>* relinfo, 5514 size_t relnum, 5515 Output_segment*, 5516 const elfcpp::Rela<size, false>& rela, 5517 unsigned int, 5518 typename elfcpp::Elf_types<size>::Elf_Addr, 5519 unsigned char* view, 5520 section_size_type view_size) 5521 { 5522 // leaq foo@tlsld(%rip),%rdi; call __tls_get_addr@plt; 5523 // For SIZE == 64: 5524 // ... leq foo@dtpoff(%rax),%reg 5525 // ==> .word 0x6666; .byte 0x66; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx 5526 // For SIZE == 32: 5527 // ... leq foo@dtpoff(%rax),%reg 5528 // ==> nopl 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx 5529 // leaq foo@tlsld(%rip),%rdi; call *__tls_get_addr@GOTPCREL(%rip) 5530 // For SIZE == 64: 5531 // ... leq foo@dtpoff(%rax),%reg 5532 // ==> .word 0x6666; .byte 0x6666; movq %fs:0,%rax ... leaq x@tpoff(%rax),%rdx 5533 // For SIZE == 32: 5534 // ... leq foo@dtpoff(%rax),%reg 5535 // ==> nopw 0x0(%rax); movl %fs:0,%eax ... leaq x@tpoff(%rax),%rdx 5536 5537 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); 5538 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 9); 5539 5540 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5541 view[-3] == 0x48 && view[-2] == 0x8d && view[-1] == 0x3d); 5542 5543 tls::check_tls(relinfo, relnum, rela.get_r_offset(), 5544 view[4] == 0xe8 || view[4] == 0xff); 5545 5546 if (view[4] == 0xe8) 5547 { 5548 if (size == 64) 5549 memcpy(view - 3, "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 12); 5550 else 5551 memcpy(view - 3, "\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 12); 5552 } 5553 else 5554 { 5555 if (size == 64) 5556 memcpy(view - 3, "\x66\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0\0", 5557 13); 5558 else 5559 memcpy(view - 3, "\x66\x0f\x1f\x40\x00\x64\x8b\x04\x25\0\0\0\0", 5560 13); 5561 } 5562 5563 // The next reloc should be a PLT32 reloc against __tls_get_addr. 5564 // We can skip it. 5565 this->skip_call_tls_get_addr_ = true; 5566 } 5567 5568 // Do a relocation in which we convert a TLS Initial-Exec to a 5569 // Local-Exec. 5570 5571 template<int size> 5572 inline void 5573 Target_x86_64<size>::Relocate::tls_ie_to_le( 5574 const Relocate_info<size, false>* relinfo, 5575 size_t relnum, 5576 Output_segment* tls_segment, 5577 const elfcpp::Rela<size, false>& rela, 5578 unsigned int, 5579 typename elfcpp::Elf_types<size>::Elf_Addr value, 5580 unsigned char* view, 5581 section_size_type view_size) 5582 { 5583 // We need to examine the opcodes to figure out which instruction we 5584 // are looking at. 5585 5586 // movq foo@gottpoff(%rip),%reg ==> movq $YY,%reg 5587 // addq foo@gottpoff(%rip),%reg ==> addq $YY,%reg 5588 5589 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, -3); 5590 tls::check_range(relinfo, relnum, rela.get_r_offset(), view_size, 4); 5591 5592 unsigned char op1 = view[-3]; 5593 unsigned char op2 = view[-2]; 5594 unsigned char op3 = view[-1]; 5595 unsigned char reg = op3 >> 3; 5596 5597 if (op2 == 0x8b) 5598 { 5599 // movq 5600 if (op1 == 0x4c) 5601 view[-3] = 0x49; 5602 else if (size == 32 && op1 == 0x44) 5603 view[-3] = 0x41; 5604 view[-2] = 0xc7; 5605 view[-1] = 0xc0 | reg; 5606 } 5607 else if (reg == 4) 5608 { 5609 // Special handling for %rsp. 5610 if (op1 == 0x4c) 5611 view[-3] = 0x49; 5612 else if (size == 32 && op1 == 0x44) 5613 view[-3] = 0x41; 5614 view[-2] = 0x81; 5615 view[-1] = 0xc0 | reg; 5616 } 5617 else 5618 { 5619 // addq 5620 if (op1 == 0x4c) 5621 view[-3] = 0x4d; 5622 else if (size == 32 && op1 == 0x44) 5623 view[-3] = 0x45; 5624 view[-2] = 0x8d; 5625 view[-1] = 0x80 | reg | (reg << 3); 5626 } 5627 5628 if (tls_segment != NULL) 5629 value -= tls_segment->memsz(); 5630 Relocate_functions<size, false>::rela32(view, value, 0); 5631 } 5632 5633 // Relocate section data. 5634 5635 template<int size> 5636 void 5637 Target_x86_64<size>::relocate_section( 5638 const Relocate_info<size, false>* relinfo, 5639 unsigned int sh_type, 5640 const unsigned char* prelocs, 5641 size_t reloc_count, 5642 Output_section* output_section, 5643 bool needs_special_offset_handling, 5644 unsigned char* view, 5645 typename elfcpp::Elf_types<size>::Elf_Addr address, 5646 section_size_type view_size, 5647 const Reloc_symbol_changes* reloc_symbol_changes) 5648 { 5649 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> 5650 Classify_reloc; 5651 5652 gold_assert(sh_type == elfcpp::SHT_RELA); 5653 5654 gold::relocate_section<size, false, Target_x86_64<size>, Relocate, 5655 gold::Default_comdat_behavior, Classify_reloc>( 5656 relinfo, 5657 this, 5658 prelocs, 5659 reloc_count, 5660 output_section, 5661 needs_special_offset_handling, 5662 view, 5663 address, 5664 view_size, 5665 reloc_symbol_changes); 5666 } 5667 5668 // Apply an incremental relocation. Incremental relocations always refer 5669 // to global symbols. 5670 5671 template<int size> 5672 void 5673 Target_x86_64<size>::apply_relocation( 5674 const Relocate_info<size, false>* relinfo, 5675 typename elfcpp::Elf_types<size>::Elf_Addr r_offset, 5676 unsigned int r_type, 5677 typename elfcpp::Elf_types<size>::Elf_Swxword r_addend, 5678 const Symbol* gsym, 5679 unsigned char* view, 5680 typename elfcpp::Elf_types<size>::Elf_Addr address, 5681 section_size_type view_size) 5682 { 5683 gold::apply_relocation<size, false, Target_x86_64<size>, 5684 typename Target_x86_64<size>::Relocate>( 5685 relinfo, 5686 this, 5687 r_offset, 5688 r_type, 5689 r_addend, 5690 gsym, 5691 view, 5692 address, 5693 view_size); 5694 } 5695 5696 // Scan the relocs during a relocatable link. 5697 5698 template<int size> 5699 void 5700 Target_x86_64<size>::scan_relocatable_relocs( 5701 Symbol_table* symtab, 5702 Layout* layout, 5703 Sized_relobj_file<size, false>* object, 5704 unsigned int data_shndx, 5705 unsigned int sh_type, 5706 const unsigned char* prelocs, 5707 size_t reloc_count, 5708 Output_section* output_section, 5709 bool needs_special_offset_handling, 5710 size_t local_symbol_count, 5711 const unsigned char* plocal_symbols, 5712 Relocatable_relocs* rr) 5713 { 5714 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> 5715 Classify_reloc; 5716 typedef gold::Default_scan_relocatable_relocs<Classify_reloc> 5717 Scan_relocatable_relocs; 5718 5719 gold_assert(sh_type == elfcpp::SHT_RELA); 5720 5721 gold::scan_relocatable_relocs<size, false, Scan_relocatable_relocs>( 5722 symtab, 5723 layout, 5724 object, 5725 data_shndx, 5726 prelocs, 5727 reloc_count, 5728 output_section, 5729 needs_special_offset_handling, 5730 local_symbol_count, 5731 plocal_symbols, 5732 rr); 5733 } 5734 5735 // Scan the relocs for --emit-relocs. 5736 5737 template<int size> 5738 void 5739 Target_x86_64<size>::emit_relocs_scan( 5740 Symbol_table* symtab, 5741 Layout* layout, 5742 Sized_relobj_file<size, false>* object, 5743 unsigned int data_shndx, 5744 unsigned int sh_type, 5745 const unsigned char* prelocs, 5746 size_t reloc_count, 5747 Output_section* output_section, 5748 bool needs_special_offset_handling, 5749 size_t local_symbol_count, 5750 const unsigned char* plocal_syms, 5751 Relocatable_relocs* rr) 5752 { 5753 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> 5754 Classify_reloc; 5755 typedef gold::Default_emit_relocs_strategy<Classify_reloc> 5756 Emit_relocs_strategy; 5757 5758 gold_assert(sh_type == elfcpp::SHT_RELA); 5759 5760 gold::scan_relocatable_relocs<size, false, Emit_relocs_strategy>( 5761 symtab, 5762 layout, 5763 object, 5764 data_shndx, 5765 prelocs, 5766 reloc_count, 5767 output_section, 5768 needs_special_offset_handling, 5769 local_symbol_count, 5770 plocal_syms, 5771 rr); 5772 } 5773 5774 // Relocate a section during a relocatable link. 5775 5776 template<int size> 5777 void 5778 Target_x86_64<size>::relocate_relocs( 5779 const Relocate_info<size, false>* relinfo, 5780 unsigned int sh_type, 5781 const unsigned char* prelocs, 5782 size_t reloc_count, 5783 Output_section* output_section, 5784 typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section, 5785 unsigned char* view, 5786 typename elfcpp::Elf_types<size>::Elf_Addr view_address, 5787 section_size_type view_size, 5788 unsigned char* reloc_view, 5789 section_size_type reloc_view_size) 5790 { 5791 typedef gold::Default_classify_reloc<elfcpp::SHT_RELA, size, false> 5792 Classify_reloc; 5793 5794 gold_assert(sh_type == elfcpp::SHT_RELA); 5795 5796 gold::relocate_relocs<size, false, Classify_reloc>( 5797 relinfo, 5798 prelocs, 5799 reloc_count, 5800 output_section, 5801 offset_in_output_section, 5802 view, 5803 view_address, 5804 view_size, 5805 reloc_view, 5806 reloc_view_size); 5807 } 5808 5809 // Return the value to use for a dynamic which requires special 5810 // treatment. This is how we support equality comparisons of function 5811 // pointers across shared library boundaries, as described in the 5812 // processor specific ABI supplement. 5813 5814 template<int size> 5815 uint64_t 5816 Target_x86_64<size>::do_dynsym_value(const Symbol* gsym) const 5817 { 5818 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset()); 5819 return this->plt_address_for_global(gsym); 5820 } 5821 5822 // Return a string used to fill a code section with nops to take up 5823 // the specified length. 5824 5825 template<int size> 5826 std::string 5827 Target_x86_64<size>::do_code_fill(section_size_type length) const 5828 { 5829 if (length >= 16) 5830 { 5831 // Build a jmpq instruction to skip over the bytes. 5832 unsigned char jmp[5]; 5833 jmp[0] = 0xe9; 5834 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5); 5835 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5) 5836 + std::string(length - 5, static_cast<char>(0x90))); 5837 } 5838 5839 // Nop sequences of various lengths. 5840 const char nop1[1] = { '\x90' }; // nop 5841 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax 5842 const char nop3[3] = { '\x0f', '\x1f', '\x00' }; // nop (%rax) 5843 const char nop4[4] = { '\x0f', '\x1f', '\x40', // nop 0(%rax) 5844 '\x00'}; 5845 const char nop5[5] = { '\x0f', '\x1f', '\x44', // nop 0(%rax,%rax,1) 5846 '\x00', '\x00' }; 5847 const char nop6[6] = { '\x66', '\x0f', '\x1f', // nopw 0(%rax,%rax,1) 5848 '\x44', '\x00', '\x00' }; 5849 const char nop7[7] = { '\x0f', '\x1f', '\x80', // nopl 0L(%rax) 5850 '\x00', '\x00', '\x00', 5851 '\x00' }; 5852 const char nop8[8] = { '\x0f', '\x1f', '\x84', // nopl 0L(%rax,%rax,1) 5853 '\x00', '\x00', '\x00', 5854 '\x00', '\x00' }; 5855 const char nop9[9] = { '\x66', '\x0f', '\x1f', // nopw 0L(%rax,%rax,1) 5856 '\x84', '\x00', '\x00', 5857 '\x00', '\x00', '\x00' }; 5858 const char nop10[10] = { '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1) 5859 '\x1f', '\x84', '\x00', 5860 '\x00', '\x00', '\x00', 5861 '\x00' }; 5862 const char nop11[11] = { '\x66', '\x66', '\x2e', // data16 5863 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1) 5864 '\x00', '\x00', '\x00', 5865 '\x00', '\x00' }; 5866 const char nop12[12] = { '\x66', '\x66', '\x66', // data16; data16 5867 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1) 5868 '\x84', '\x00', '\x00', 5869 '\x00', '\x00', '\x00' }; 5870 const char nop13[13] = { '\x66', '\x66', '\x66', // data16; data16; data16 5871 '\x66', '\x2e', '\x0f', // nopw %cs:0L(%rax,%rax,1) 5872 '\x1f', '\x84', '\x00', 5873 '\x00', '\x00', '\x00', 5874 '\x00' }; 5875 const char nop14[14] = { '\x66', '\x66', '\x66', // data16; data16; data16 5876 '\x66', '\x66', '\x2e', // data16 5877 '\x0f', '\x1f', '\x84', // nopw %cs:0L(%rax,%rax,1) 5878 '\x00', '\x00', '\x00', 5879 '\x00', '\x00' }; 5880 const char nop15[15] = { '\x66', '\x66', '\x66', // data16; data16; data16 5881 '\x66', '\x66', '\x66', // data16; data16 5882 '\x2e', '\x0f', '\x1f', // nopw %cs:0L(%rax,%rax,1) 5883 '\x84', '\x00', '\x00', 5884 '\x00', '\x00', '\x00' }; 5885 5886 const char* nops[16] = { 5887 NULL, 5888 nop1, nop2, nop3, nop4, nop5, nop6, nop7, 5889 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15 5890 }; 5891 5892 return std::string(nops[length], length); 5893 } 5894 5895 // Return the addend to use for a target specific relocation. The 5896 // only target specific relocation is R_X86_64_TLSDESC for a local 5897 // symbol. We want to set the addend is the offset of the local 5898 // symbol in the TLS segment. 5899 5900 template<int size> 5901 uint64_t 5902 Target_x86_64<size>::do_reloc_addend(void* arg, unsigned int r_type, 5903 uint64_t) const 5904 { 5905 gold_assert(r_type == elfcpp::R_X86_64_TLSDESC); 5906 uintptr_t intarg = reinterpret_cast<uintptr_t>(arg); 5907 gold_assert(intarg < this->tlsdesc_reloc_info_.size()); 5908 const Tlsdesc_info& ti(this->tlsdesc_reloc_info_[intarg]); 5909 const Symbol_value<size>* psymval = ti.object->local_symbol(ti.r_sym); 5910 gold_assert(psymval->is_tls_symbol()); 5911 // The value of a TLS symbol is the offset in the TLS segment. 5912 return psymval->value(ti.object, 0); 5913 } 5914 5915 // Return the value to use for the base of a DW_EH_PE_datarel offset 5916 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their 5917 // assembler can not write out the difference between two labels in 5918 // different sections, so instead of using a pc-relative value they 5919 // use an offset from the GOT. 5920 5921 template<int size> 5922 uint64_t 5923 Target_x86_64<size>::do_ehframe_datarel_base() const 5924 { 5925 gold_assert(this->global_offset_table_ != NULL); 5926 Symbol* sym = this->global_offset_table_; 5927 Sized_symbol<size>* ssym = static_cast<Sized_symbol<size>*>(sym); 5928 return ssym->value(); 5929 } 5930 5931 // FNOFFSET in section SHNDX in OBJECT is the start of a function 5932 // compiled with -fsplit-stack. The function calls non-split-stack 5933 // code. We have to change the function so that it always ensures 5934 // that it has enough stack space to run some random function. 5935 5936 static const unsigned char cmp_insn_32[] = { 0x64, 0x3b, 0x24, 0x25 }; 5937 static const unsigned char lea_r10_insn_32[] = { 0x44, 0x8d, 0x94, 0x24 }; 5938 static const unsigned char lea_r11_insn_32[] = { 0x44, 0x8d, 0x9c, 0x24 }; 5939 5940 static const unsigned char cmp_insn_64[] = { 0x64, 0x48, 0x3b, 0x24, 0x25 }; 5941 static const unsigned char lea_r10_insn_64[] = { 0x4c, 0x8d, 0x94, 0x24 }; 5942 static const unsigned char lea_r11_insn_64[] = { 0x4c, 0x8d, 0x9c, 0x24 }; 5943 5944 template<int size> 5945 void 5946 Target_x86_64<size>::do_calls_non_split(Relobj* object, unsigned int shndx, 5947 section_offset_type fnoffset, 5948 section_size_type fnsize, 5949 const unsigned char*, 5950 size_t, 5951 unsigned char* view, 5952 section_size_type view_size, 5953 std::string* from, 5954 std::string* to) const 5955 { 5956 const char* const cmp_insn = reinterpret_cast<const char*> 5957 (size == 32 ? cmp_insn_32 : cmp_insn_64); 5958 const char* const lea_r10_insn = reinterpret_cast<const char*> 5959 (size == 32 ? lea_r10_insn_32 : lea_r10_insn_64); 5960 const char* const lea_r11_insn = reinterpret_cast<const char*> 5961 (size == 32 ? lea_r11_insn_32 : lea_r11_insn_64); 5962 5963 const size_t cmp_insn_len = 5964 (size == 32 ? sizeof(cmp_insn_32) : sizeof(cmp_insn_64)); 5965 const size_t lea_r10_insn_len = 5966 (size == 32 ? sizeof(lea_r10_insn_32) : sizeof(lea_r10_insn_64)); 5967 const size_t lea_r11_insn_len = 5968 (size == 32 ? sizeof(lea_r11_insn_32) : sizeof(lea_r11_insn_64)); 5969 const size_t nop_len = (size == 32 ? 7 : 8); 5970 5971 // The function starts with a comparison of the stack pointer and a 5972 // field in the TCB. This is followed by a jump. 5973 5974 // cmp %fs:NN,%rsp 5975 if (this->match_view(view, view_size, fnoffset, cmp_insn, cmp_insn_len) 5976 && fnsize > nop_len + 1) 5977 { 5978 // We will call __morestack if the carry flag is set after this 5979 // comparison. We turn the comparison into an stc instruction 5980 // and some nops. 5981 view[fnoffset] = '\xf9'; 5982 this->set_view_to_nop(view, view_size, fnoffset + 1, nop_len); 5983 } 5984 // lea NN(%rsp),%r10 5985 // lea NN(%rsp),%r11 5986 else if ((this->match_view(view, view_size, fnoffset, 5987 lea_r10_insn, lea_r10_insn_len) 5988 || this->match_view(view, view_size, fnoffset, 5989 lea_r11_insn, lea_r11_insn_len)) 5990 && fnsize > 8) 5991 { 5992 // This is loading an offset from the stack pointer for a 5993 // comparison. The offset is negative, so we decrease the 5994 // offset by the amount of space we need for the stack. This 5995 // means we will avoid calling __morestack if there happens to 5996 // be plenty of space on the stack already. 5997 unsigned char* pval = view + fnoffset + 4; 5998 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval); 5999 val -= parameters->options().split_stack_adjust_size(); 6000 elfcpp::Swap_unaligned<32, false>::writeval(pval, val); 6001 } 6002 else 6003 { 6004 if (!object->has_no_split_stack()) 6005 object->error(_("failed to match split-stack sequence at " 6006 "section %u offset %0zx"), 6007 shndx, static_cast<size_t>(fnoffset)); 6008 return; 6009 } 6010 6011 // We have to change the function so that it calls 6012 // __morestack_non_split instead of __morestack. The former will 6013 // allocate additional stack space. 6014 *from = "__morestack"; 6015 *to = "__morestack_non_split"; 6016 } 6017 6018 // The selector for x86_64 object files. Note this is never instantiated 6019 // directly. It's only used in Target_selector_x86_64_nacl, below. 6020 6021 template<int size> 6022 class Target_selector_x86_64 : public Target_selector_freebsd 6023 { 6024 public: 6025 Target_selector_x86_64() 6026 : Target_selector_freebsd(elfcpp::EM_X86_64, size, false, 6027 (size == 64 6028 ? "elf64-x86-64" : "elf32-x86-64"), 6029 (size == 64 6030 ? "elf64-x86-64-freebsd" 6031 : "elf32-x86-64-freebsd"), 6032 (size == 64 ? "elf_x86_64" : "elf32_x86_64")) 6033 { } 6034 6035 Target* 6036 do_instantiate_target() 6037 { return new Target_x86_64<size>(); } 6038 6039 }; 6040 6041 // NaCl variant. It uses different PLT contents. 6042 6043 template<int size> 6044 class Output_data_plt_x86_64_nacl : public Output_data_plt_x86_64<size> 6045 { 6046 public: 6047 Output_data_plt_x86_64_nacl(Layout* layout, 6048 Output_data_got<64, false>* got, 6049 Output_data_got_plt_x86_64* got_plt, 6050 Output_data_space* got_irelative) 6051 : Output_data_plt_x86_64<size>(layout, plt_entry_size, 6052 got, got_plt, got_irelative) 6053 { } 6054 6055 Output_data_plt_x86_64_nacl(Layout* layout, 6056 Output_data_got<64, false>* got, 6057 Output_data_got_plt_x86_64* got_plt, 6058 Output_data_space* got_irelative, 6059 unsigned int plt_count) 6060 : Output_data_plt_x86_64<size>(layout, plt_entry_size, 6061 got, got_plt, got_irelative, 6062 plt_count) 6063 { } 6064 6065 protected: 6066 virtual unsigned int 6067 do_get_plt_entry_size() const 6068 { return plt_entry_size; } 6069 6070 virtual void 6071 do_add_eh_frame(Layout* layout) 6072 { 6073 layout->add_eh_frame_for_plt(this, 6074 this->plt_eh_frame_cie, 6075 this->plt_eh_frame_cie_size, 6076 plt_eh_frame_fde, 6077 plt_eh_frame_fde_size); 6078 } 6079 6080 virtual void 6081 do_fill_first_plt_entry(unsigned char* pov, 6082 typename elfcpp::Elf_types<size>::Elf_Addr got_addr, 6083 typename elfcpp::Elf_types<size>::Elf_Addr plt_addr); 6084 6085 virtual unsigned int 6086 do_fill_plt_entry(unsigned char* pov, 6087 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 6088 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 6089 unsigned int got_offset, 6090 unsigned int plt_offset, 6091 unsigned int plt_index); 6092 6093 virtual void 6094 do_fill_tlsdesc_entry(unsigned char* pov, 6095 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 6096 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 6097 typename elfcpp::Elf_types<size>::Elf_Addr got_base, 6098 unsigned int tlsdesc_got_offset, 6099 unsigned int plt_offset); 6100 6101 private: 6102 // The size of an entry in the PLT. 6103 static const int plt_entry_size = 64; 6104 6105 // The first entry in the PLT. 6106 static const unsigned char first_plt_entry[plt_entry_size]; 6107 6108 // Other entries in the PLT for an executable. 6109 static const unsigned char plt_entry[plt_entry_size]; 6110 6111 // The reserved TLSDESC entry in the PLT for an executable. 6112 static const unsigned char tlsdesc_plt_entry[plt_entry_size]; 6113 6114 // The .eh_frame unwind information for the PLT. 6115 static const int plt_eh_frame_fde_size = 32; 6116 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size]; 6117 }; 6118 6119 template<int size> 6120 class Target_x86_64_nacl : public Target_x86_64<size> 6121 { 6122 public: 6123 Target_x86_64_nacl() 6124 : Target_x86_64<size>(&x86_64_nacl_info) 6125 { } 6126 6127 virtual Output_data_plt_x86_64<size>* 6128 do_make_data_plt(Layout* layout, 6129 Output_data_got<64, false>* got, 6130 Output_data_got_plt_x86_64* got_plt, 6131 Output_data_space* got_irelative) 6132 { 6133 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt, 6134 got_irelative); 6135 } 6136 6137 virtual Output_data_plt_x86_64<size>* 6138 do_make_data_plt(Layout* layout, 6139 Output_data_got<64, false>* got, 6140 Output_data_got_plt_x86_64* got_plt, 6141 Output_data_space* got_irelative, 6142 unsigned int plt_count) 6143 { 6144 return new Output_data_plt_x86_64_nacl<size>(layout, got, got_plt, 6145 got_irelative, 6146 plt_count); 6147 } 6148 6149 virtual std::string 6150 do_code_fill(section_size_type length) const; 6151 6152 private: 6153 static const Target::Target_info x86_64_nacl_info; 6154 }; 6155 6156 template<> 6157 const Target::Target_info Target_x86_64_nacl<64>::x86_64_nacl_info = 6158 { 6159 64, // size 6160 false, // is_big_endian 6161 elfcpp::EM_X86_64, // machine_code 6162 false, // has_make_symbol 6163 false, // has_resolve 6164 true, // has_code_fill 6165 true, // is_default_stack_executable 6166 true, // can_icf_inline_merge_sections 6167 '\0', // wrap_char 6168 "/lib64/ld-nacl-x86-64.so.1", // dynamic_linker 6169 0x20000, // default_text_segment_address 6170 0x10000, // abi_pagesize (overridable by -z max-page-size) 6171 0x10000, // common_pagesize (overridable by -z common-page-size) 6172 true, // isolate_execinstr 6173 0x10000000, // rosegment_gap 6174 elfcpp::SHN_UNDEF, // small_common_shndx 6175 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx 6176 0, // small_common_section_flags 6177 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags 6178 NULL, // attributes_section 6179 NULL, // attributes_vendor 6180 "_start", // entry_symbol_name 6181 32, // hash_entry_size 6182 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type 6183 }; 6184 6185 template<> 6186 const Target::Target_info Target_x86_64_nacl<32>::x86_64_nacl_info = 6187 { 6188 32, // size 6189 false, // is_big_endian 6190 elfcpp::EM_X86_64, // machine_code 6191 false, // has_make_symbol 6192 false, // has_resolve 6193 true, // has_code_fill 6194 true, // is_default_stack_executable 6195 true, // can_icf_inline_merge_sections 6196 '\0', // wrap_char 6197 "/lib/ld-nacl-x86-64.so.1", // dynamic_linker 6198 0x20000, // default_text_segment_address 6199 0x10000, // abi_pagesize (overridable by -z max-page-size) 6200 0x10000, // common_pagesize (overridable by -z common-page-size) 6201 true, // isolate_execinstr 6202 0x10000000, // rosegment_gap 6203 elfcpp::SHN_UNDEF, // small_common_shndx 6204 elfcpp::SHN_X86_64_LCOMMON, // large_common_shndx 6205 0, // small_common_section_flags 6206 elfcpp::SHF_X86_64_LARGE, // large_common_section_flags 6207 NULL, // attributes_section 6208 NULL, // attributes_vendor 6209 "_start", // entry_symbol_name 6210 32, // hash_entry_size 6211 elfcpp::SHT_X86_64_UNWIND, // unwind_section_type 6212 }; 6213 6214 #define NACLMASK 0xe0 // 32-byte alignment mask. 6215 6216 // The first entry in the PLT. 6217 6218 template<int size> 6219 const unsigned char 6220 Output_data_plt_x86_64_nacl<size>::first_plt_entry[plt_entry_size] = 6221 { 6222 0xff, 0x35, // pushq contents of memory address 6223 0, 0, 0, 0, // replaced with address of .got + 8 6224 0x4c, 0x8b, 0x1d, // mov GOT+16(%rip), %r11 6225 0, 0, 0, 0, // replaced with address of .got + 16 6226 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d 6227 0x4d, 0x01, 0xfb, // add %r15, %r11 6228 0x41, 0xff, 0xe3, // jmpq *%r11 6229 6230 // 9-byte nop sequence to pad out to the next 32-byte boundary. 6231 0x66, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw 0x0(%rax,%rax,1) 6232 6233 // 32 bytes of nop to pad out to the standard size 6234 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes 6235 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) 6236 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes 6237 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) 6238 0x66, // excess data32 prefix 6239 0x90 // nop 6240 }; 6241 6242 template<int size> 6243 void 6244 Output_data_plt_x86_64_nacl<size>::do_fill_first_plt_entry( 6245 unsigned char* pov, 6246 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 6247 typename elfcpp::Elf_types<size>::Elf_Addr plt_address) 6248 { 6249 memcpy(pov, first_plt_entry, plt_entry_size); 6250 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, 6251 (got_address + 8 6252 - (plt_address + 2 + 4))); 6253 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9, 6254 (got_address + 16 6255 - (plt_address + 9 + 4))); 6256 } 6257 6258 // Subsequent entries in the PLT. 6259 6260 template<int size> 6261 const unsigned char 6262 Output_data_plt_x86_64_nacl<size>::plt_entry[plt_entry_size] = 6263 { 6264 0x4c, 0x8b, 0x1d, // mov name@GOTPCREL(%rip),%r11 6265 0, 0, 0, 0, // replaced with address of symbol in .got 6266 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d 6267 0x4d, 0x01, 0xfb, // add %r15, %r11 6268 0x41, 0xff, 0xe3, // jmpq *%r11 6269 6270 // 15-byte nop sequence to pad out to the next 32-byte boundary. 6271 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes 6272 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) 6273 6274 // Lazy GOT entries point here (32-byte aligned). 6275 0x68, // pushq immediate 6276 0, 0, 0, 0, // replaced with index into relocation table 6277 0xe9, // jmp relative 6278 0, 0, 0, 0, // replaced with offset to start of .plt0 6279 6280 // 22 bytes of nop to pad out to the standard size. 6281 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes 6282 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) 6283 0x0f, 0x1f, 0x80, 0, 0, 0, 0, // nopl 0x0(%rax) 6284 }; 6285 6286 template<int size> 6287 unsigned int 6288 Output_data_plt_x86_64_nacl<size>::do_fill_plt_entry( 6289 unsigned char* pov, 6290 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 6291 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 6292 unsigned int got_offset, 6293 unsigned int plt_offset, 6294 unsigned int plt_index) 6295 { 6296 memcpy(pov, plt_entry, plt_entry_size); 6297 elfcpp::Swap_unaligned<32, false>::writeval(pov + 3, 6298 (got_address + got_offset 6299 - (plt_address + plt_offset 6300 + 3 + 4))); 6301 6302 elfcpp::Swap_unaligned<32, false>::writeval(pov + 33, plt_index); 6303 elfcpp::Swap_unaligned<32, false>::writeval(pov + 38, 6304 - (plt_offset + 38 + 4)); 6305 6306 return 32; 6307 } 6308 6309 // The reserved TLSDESC entry in the PLT. 6310 6311 template<int size> 6312 const unsigned char 6313 Output_data_plt_x86_64_nacl<size>::tlsdesc_plt_entry[plt_entry_size] = 6314 { 6315 0xff, 0x35, // pushq x(%rip) 6316 0, 0, 0, 0, // replaced with address of linkmap GOT entry (at PLTGOT + 8) 6317 0x4c, 0x8b, 0x1d, // mov y(%rip),%r11 6318 0, 0, 0, 0, // replaced with offset of reserved TLSDESC_GOT entry 6319 0x41, 0x83, 0xe3, NACLMASK, // and $-32, %r11d 6320 0x4d, 0x01, 0xfb, // add %r15, %r11 6321 0x41, 0xff, 0xe3, // jmpq *%r11 6322 6323 // 41 bytes of nop to pad out to the standard size. 6324 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes 6325 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) 6326 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, // excess data32 prefixes 6327 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) 6328 0x66, 0x66, // excess data32 prefixes 6329 0x2e, 0x0f, 0x1f, 0x84, 0, 0, 0, 0, 0, // nopw %cs:0x0(%rax,%rax,1) 6330 }; 6331 6332 template<int size> 6333 void 6334 Output_data_plt_x86_64_nacl<size>::do_fill_tlsdesc_entry( 6335 unsigned char* pov, 6336 typename elfcpp::Elf_types<size>::Elf_Addr got_address, 6337 typename elfcpp::Elf_types<size>::Elf_Addr plt_address, 6338 typename elfcpp::Elf_types<size>::Elf_Addr got_base, 6339 unsigned int tlsdesc_got_offset, 6340 unsigned int plt_offset) 6341 { 6342 memcpy(pov, tlsdesc_plt_entry, plt_entry_size); 6343 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, 6344 (got_address + 8 6345 - (plt_address + plt_offset 6346 + 2 + 4))); 6347 elfcpp::Swap_unaligned<32, false>::writeval(pov + 9, 6348 (got_base 6349 + tlsdesc_got_offset 6350 - (plt_address + plt_offset 6351 + 9 + 4))); 6352 } 6353 6354 // The .eh_frame unwind information for the PLT. 6355 6356 template<int size> 6357 const unsigned char 6358 Output_data_plt_x86_64_nacl<size>::plt_eh_frame_fde[plt_eh_frame_fde_size] = 6359 { 6360 0, 0, 0, 0, // Replaced with offset to .plt. 6361 0, 0, 0, 0, // Replaced with size of .plt. 6362 0, // Augmentation size. 6363 elfcpp::DW_CFA_def_cfa_offset, 16, // DW_CFA_def_cfa_offset: 16. 6364 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6. 6365 elfcpp::DW_CFA_def_cfa_offset, 24, // DW_CFA_def_cfa_offset: 24. 6366 elfcpp::DW_CFA_advance_loc + 58, // Advance 58 to __PLT__ + 64. 6367 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression. 6368 13, // Block length. 6369 elfcpp::DW_OP_breg7, 8, // Push %rsp + 8. 6370 elfcpp::DW_OP_breg16, 0, // Push %rip. 6371 elfcpp::DW_OP_const1u, 63, // Push 0x3f. 6372 elfcpp::DW_OP_and, // & (%rip & 0x3f). 6373 elfcpp::DW_OP_const1u, 37, // Push 0x25. 6374 elfcpp::DW_OP_ge, // >= ((%rip & 0x3f) >= 0x25) 6375 elfcpp::DW_OP_lit3, // Push 3. 6376 elfcpp::DW_OP_shl, // << (((%rip & 0x3f) >= 0x25) << 3) 6377 elfcpp::DW_OP_plus, // + ((((%rip&0x3f)>=0x25)<<3)+%rsp+8 6378 elfcpp::DW_CFA_nop, // Align to 32 bytes. 6379 elfcpp::DW_CFA_nop 6380 }; 6381 6382 // Return a string used to fill a code section with nops. 6383 // For NaCl, long NOPs are only valid if they do not cross 6384 // bundle alignment boundaries, so keep it simple with one-byte NOPs. 6385 template<int size> 6386 std::string 6387 Target_x86_64_nacl<size>::do_code_fill(section_size_type length) const 6388 { 6389 return std::string(length, static_cast<char>(0x90)); 6390 } 6391 6392 // The selector for x86_64-nacl object files. 6393 6394 template<int size> 6395 class Target_selector_x86_64_nacl 6396 : public Target_selector_nacl<Target_selector_x86_64<size>, 6397 Target_x86_64_nacl<size> > 6398 { 6399 public: 6400 Target_selector_x86_64_nacl() 6401 : Target_selector_nacl<Target_selector_x86_64<size>, 6402 Target_x86_64_nacl<size> >("x86-64", 6403 size == 64 6404 ? "elf64-x86-64-nacl" 6405 : "elf32-x86-64-nacl", 6406 size == 64 6407 ? "elf_x86_64_nacl" 6408 : "elf32_x86_64_nacl") 6409 { } 6410 }; 6411 6412 Target_selector_x86_64_nacl<64> target_selector_x86_64; 6413 Target_selector_x86_64_nacl<32> target_selector_x32; 6414 6415 } // End anonymous namespace. 6416